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_output.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2001 Atsushi Onoe
    3  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD$");
   29 
   30 #include "opt_inet.h"
   31 #include "opt_inet6.h"
   32 #include "opt_wlan.h"
   33 
   34 #include <sys/param.h>
   35 #include <sys/systm.h> 
   36 #include <sys/kernel.h>
   37 #include <sys/malloc.h>
   38 #include <sys/mbuf.h>   
   39 #include <sys/endian.h>
   40 
   41 #include <sys/socket.h>
   42  
   43 #include <net/bpf.h>
   44 #include <net/ethernet.h>
   45 #include <net/if.h>
   46 #include <net/if_var.h>
   47 #include <net/if_llc.h>
   48 #include <net/if_media.h>
   49 #include <net/if_vlan_var.h>
   50 
   51 #include <net80211/ieee80211_var.h>
   52 #include <net80211/ieee80211_regdomain.h>
   53 #ifdef IEEE80211_SUPPORT_SUPERG
   54 #include <net80211/ieee80211_superg.h>
   55 #endif
   56 #ifdef IEEE80211_SUPPORT_TDMA
   57 #include <net80211/ieee80211_tdma.h>
   58 #endif
   59 #include <net80211/ieee80211_wds.h>
   60 #include <net80211/ieee80211_mesh.h>
   61 
   62 #if defined(INET) || defined(INET6)
   63 #include <netinet/in.h> 
   64 #endif
   65 
   66 #ifdef INET
   67 #include <netinet/if_ether.h>
   68 #include <netinet/in_systm.h>
   69 #include <netinet/ip.h>
   70 #endif
   71 #ifdef INET6
   72 #include <netinet/ip6.h>
   73 #endif
   74 
   75 #include <security/mac/mac_framework.h>
   76 
   77 #define ETHER_HEADER_COPY(dst, src) \
   78         memcpy(dst, src, sizeof(struct ether_header))
   79 
   80 static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
   81         u_int hdrsize, u_int ciphdrsize, u_int mtu);
   82 static  void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
   83 
   84 #ifdef IEEE80211_DEBUG
   85 /*
   86  * Decide if an outbound management frame should be
   87  * printed when debugging is enabled.  This filters some
   88  * of the less interesting frames that come frequently
   89  * (e.g. beacons).
   90  */
   91 static __inline int
   92 doprint(struct ieee80211vap *vap, int subtype)
   93 {
   94         switch (subtype) {
   95         case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
   96                 return (vap->iv_opmode == IEEE80211_M_IBSS);
   97         }
   98         return 1;
   99 }
  100 #endif
  101 
  102 /*
  103  * Transmit a frame to the given destination on the given VAP.
  104  *
  105  * It's up to the caller to figure out the details of who this
  106  * is going to and resolving the node.
  107  *
  108  * This routine takes care of queuing it for power save,
  109  * A-MPDU state stuff, fast-frames state stuff, encapsulation
  110  * if required, then passing it up to the driver layer.
  111  *
  112  * This routine (for now) consumes the mbuf and frees the node
  113  * reference; it ideally will return a TX status which reflects
  114  * whether the mbuf was consumed or not, so the caller can
  115  * free the mbuf (if appropriate) and the node reference (again,
  116  * if appropriate.)
  117  */
  118 int
  119 ieee80211_vap_pkt_send_dest(struct ieee80211vap *vap, struct mbuf *m,
  120     struct ieee80211_node *ni)
  121 {
  122         struct ieee80211com *ic = vap->iv_ic;
  123         struct ifnet *ifp = vap->iv_ifp;
  124 #ifdef IEEE80211_SUPPORT_SUPERG
  125         int mcast;
  126 #endif
  127 
  128         if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
  129             (m->m_flags & M_PWR_SAV) == 0) {
  130                 /*
  131                  * Station in power save mode; pass the frame
  132                  * to the 802.11 layer and continue.  We'll get
  133                  * the frame back when the time is right.
  134                  * XXX lose WDS vap linkage?
  135                  */
  136                 if (ieee80211_pwrsave(ni, m) != 0)
  137                         if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  138                 ieee80211_free_node(ni);
  139 
  140                 /*
  141                  * We queued it fine, so tell the upper layer
  142                  * that we consumed it.
  143                  */
  144                 return (0);
  145         }
  146         /* calculate priority so drivers can find the tx queue */
  147         if (ieee80211_classify(ni, m)) {
  148                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
  149                     ni->ni_macaddr, NULL,
  150                     "%s", "classification failure");
  151                 vap->iv_stats.is_tx_classify++;
  152                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  153                 m_freem(m);
  154                 ieee80211_free_node(ni);
  155 
  156                 /* XXX better status? */
  157                 return (0);
  158         }
  159         /*
  160          * Stash the node pointer.  Note that we do this after
  161          * any call to ieee80211_dwds_mcast because that code
  162          * uses any existing value for rcvif to identify the
  163          * interface it (might have been) received on.
  164          */
  165         m->m_pkthdr.rcvif = (void *)ni;
  166 #ifdef IEEE80211_SUPPORT_SUPERG
  167         mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1: 0;
  168 #endif
  169 
  170         BPF_MTAP(ifp, m);               /* 802.3 tx */
  171 
  172         /*
  173          * Check if A-MPDU tx aggregation is setup or if we
  174          * should try to enable it.  The sta must be associated
  175          * with HT and A-MPDU enabled for use.  When the policy
  176          * routine decides we should enable A-MPDU we issue an
  177          * ADDBA request and wait for a reply.  The frame being
  178          * encapsulated will go out w/o using A-MPDU, or possibly
  179          * it might be collected by the driver and held/retransmit.
  180          * The default ic_ampdu_enable routine handles staggering
  181          * ADDBA requests in case the receiver NAK's us or we are
  182          * otherwise unable to establish a BA stream.
  183          */
  184         if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
  185             (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX)) {
  186                 if ((m->m_flags & M_EAPOL) == 0) {
  187                         int tid = WME_AC_TO_TID(M_WME_GETAC(m));
  188                         struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[tid];
  189 
  190                         ieee80211_txampdu_count_packet(tap);
  191                         if (IEEE80211_AMPDU_RUNNING(tap)) {
  192                                 /*
  193                                  * Operational, mark frame for aggregation.
  194                                  *
  195                                  * XXX do tx aggregation here
  196                                  */
  197                                 m->m_flags |= M_AMPDU_MPDU;
  198                         } else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
  199                             ic->ic_ampdu_enable(ni, tap)) {
  200                                 /*
  201                                  * Not negotiated yet, request service.
  202                                  */
  203                                 ieee80211_ampdu_request(ni, tap);
  204                                 /* XXX hold frame for reply? */
  205                         }
  206                 }
  207         }
  208 
  209 #ifdef IEEE80211_SUPPORT_SUPERG
  210         /*
  211          * Check for AMSDU/FF; queue for aggregation
  212          *
  213          * Note: we don't bother trying to do fast frames or
  214          * A-MSDU encapsulation for 802.3 drivers.  Now, we
  215          * likely could do it for FF (because it's a magic
  216          * atheros tunnel LLC type) but I don't think we're going
  217          * to really need to.  For A-MSDU we'd have to set the
  218          * A-MSDU QoS bit in the wifi header, so we just plain
  219          * can't do it.
  220          *
  221          * Strictly speaking, we could actually /do/ A-MSDU / FF
  222          * with A-MPDU together which for certain circumstances
  223          * is beneficial (eg A-MSDU of TCK ACKs.)  However,
  224          * I'll ignore that for now so existing behaviour is maintained.
  225          * Later on it would be good to make "amsdu + ampdu" configurable.
  226          */
  227         else if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
  228                 if ((! mcast) && ieee80211_amsdu_tx_ok(ni)) {
  229                         m = ieee80211_amsdu_check(ni, m);
  230                         if (m == NULL) {
  231                                 /* NB: any ni ref held on stageq */
  232                                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
  233                                     "%s: amsdu_check queued frame\n",
  234                                     __func__);
  235                                 return (0);
  236                         }
  237                 } else if ((! mcast) && IEEE80211_ATH_CAP(vap, ni,
  238                     IEEE80211_NODE_FF)) {
  239                         m = ieee80211_ff_check(ni, m);
  240                         if (m == NULL) {
  241                                 /* NB: any ni ref held on stageq */
  242                                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
  243                                     "%s: ff_check queued frame\n",
  244                                     __func__);
  245                                 return (0);
  246                         }
  247                 }
  248         }
  249 #endif /* IEEE80211_SUPPORT_SUPERG */
  250 
  251         /*
  252          * Grab the TX lock - serialise the TX process from this
  253          * point (where TX state is being checked/modified)
  254          * through to driver queue.
  255          */
  256         IEEE80211_TX_LOCK(ic);
  257 
  258         /*
  259          * XXX make the encap and transmit code a separate function
  260          * so things like the FF (and later A-MSDU) path can just call
  261          * it for flushed frames.
  262          */
  263         if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
  264                 /*
  265                  * Encapsulate the packet in prep for transmission.
  266                  */
  267                 m = ieee80211_encap(vap, ni, m);
  268                 if (m == NULL) {
  269                         /* NB: stat+msg handled in ieee80211_encap */
  270                         IEEE80211_TX_UNLOCK(ic);
  271                         ieee80211_free_node(ni);
  272                         if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  273                         return (ENOBUFS);
  274                 }
  275         }
  276         (void) ieee80211_parent_xmitpkt(ic, m);
  277 
  278         /*
  279          * Unlock at this point - no need to hold it across
  280          * ieee80211_free_node() (ie, the comlock)
  281          */
  282         IEEE80211_TX_UNLOCK(ic);
  283         ic->ic_lastdata = ticks;
  284 
  285         return (0);
  286 }
  287 
  288 
  289 
  290 /*
  291  * Send the given mbuf through the given vap.
  292  *
  293  * This consumes the mbuf regardless of whether the transmit
  294  * was successful or not.
  295  *
  296  * This does none of the initial checks that ieee80211_start()
  297  * does (eg CAC timeout, interface wakeup) - the caller must
  298  * do this first.
  299  */
  300 static int
  301 ieee80211_start_pkt(struct ieee80211vap *vap, struct mbuf *m)
  302 {
  303 #define IS_DWDS(vap) \
  304         (vap->iv_opmode == IEEE80211_M_WDS && \
  305          (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
  306         struct ieee80211com *ic = vap->iv_ic;
  307         struct ifnet *ifp = vap->iv_ifp;
  308         struct ieee80211_node *ni;
  309         struct ether_header *eh;
  310 
  311         /*
  312          * Cancel any background scan.
  313          */
  314         if (ic->ic_flags & IEEE80211_F_SCAN)
  315                 ieee80211_cancel_anyscan(vap);
  316         /* 
  317          * Find the node for the destination so we can do
  318          * things like power save and fast frames aggregation.
  319          *
  320          * NB: past this point various code assumes the first
  321          *     mbuf has the 802.3 header present (and contiguous).
  322          */
  323         ni = NULL;
  324         if (m->m_len < sizeof(struct ether_header) &&
  325            (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
  326                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
  327                     "discard frame, %s\n", "m_pullup failed");
  328                 vap->iv_stats.is_tx_nobuf++;    /* XXX */
  329                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  330                 return (ENOBUFS);
  331         }
  332         eh = mtod(m, struct ether_header *);
  333         if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
  334                 if (IS_DWDS(vap)) {
  335                         /*
  336                          * Only unicast frames from the above go out
  337                          * DWDS vaps; multicast frames are handled by
  338                          * dispatching the frame as it comes through
  339                          * the AP vap (see below).
  340                          */
  341                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
  342                             eh->ether_dhost, "mcast", "%s", "on DWDS");
  343                         vap->iv_stats.is_dwds_mcast++;
  344                         m_freem(m);
  345                         if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  346                         /* XXX better status? */
  347                         return (ENOBUFS);
  348                 }
  349                 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
  350                         /*
  351                          * Spam DWDS vap's w/ multicast traffic.
  352                          */
  353                         /* XXX only if dwds in use? */
  354                         ieee80211_dwds_mcast(vap, m);
  355                 }
  356         }
  357 #ifdef IEEE80211_SUPPORT_MESH
  358         if (vap->iv_opmode != IEEE80211_M_MBSS) {
  359 #endif
  360                 ni = ieee80211_find_txnode(vap, eh->ether_dhost);
  361                 if (ni == NULL) {
  362                         /* NB: ieee80211_find_txnode does stat+msg */
  363                         if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  364                         m_freem(m);
  365                         /* XXX better status? */
  366                         return (ENOBUFS);
  367                 }
  368                 if (ni->ni_associd == 0 &&
  369                     (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
  370                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
  371                             eh->ether_dhost, NULL,
  372                             "sta not associated (type 0x%04x)",
  373                             htons(eh->ether_type));
  374                         vap->iv_stats.is_tx_notassoc++;
  375                         if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  376                         m_freem(m);
  377                         ieee80211_free_node(ni);
  378                         /* XXX better status? */
  379                         return (ENOBUFS);
  380                 }
  381 #ifdef IEEE80211_SUPPORT_MESH
  382         } else {
  383                 if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) {
  384                         /*
  385                          * Proxy station only if configured.
  386                          */
  387                         if (!ieee80211_mesh_isproxyena(vap)) {
  388                                 IEEE80211_DISCARD_MAC(vap,
  389                                     IEEE80211_MSG_OUTPUT |
  390                                     IEEE80211_MSG_MESH,
  391                                     eh->ether_dhost, NULL,
  392                                     "%s", "proxy not enabled");
  393                                 vap->iv_stats.is_mesh_notproxy++;
  394                                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  395                                 m_freem(m);
  396                                 /* XXX better status? */
  397                                 return (ENOBUFS);
  398                         }
  399                         IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
  400                             "forward frame from DS SA(%6D), DA(%6D)\n",
  401                             eh->ether_shost, ":",
  402                             eh->ether_dhost, ":");
  403                         ieee80211_mesh_proxy_check(vap, eh->ether_shost);
  404                 }
  405                 ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m);
  406                 if (ni == NULL) {
  407                         /*
  408                          * NB: ieee80211_mesh_discover holds/disposes
  409                          * frame (e.g. queueing on path discovery).
  410                          */
  411                         if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  412                         /* XXX better status? */
  413                         return (ENOBUFS);
  414                 }
  415         }
  416 #endif
  417 
  418         /*
  419          * We've resolved the sender, so attempt to transmit it.
  420          */
  421 
  422         if (vap->iv_state == IEEE80211_S_SLEEP) {
  423                 /*
  424                  * In power save; queue frame and then  wakeup device
  425                  * for transmit.
  426                  */
  427                 ic->ic_lastdata = ticks;
  428                 if (ieee80211_pwrsave(ni, m) != 0)
  429                         if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  430                 ieee80211_free_node(ni);
  431                 ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
  432                 return (0);
  433         }
  434 
  435         if (ieee80211_vap_pkt_send_dest(vap, m, ni) != 0)
  436                 return (ENOBUFS);
  437         return (0);
  438 #undef  IS_DWDS
  439 }
  440 
  441 /*
  442  * Start method for vap's.  All packets from the stack come
  443  * through here.  We handle common processing of the packets
  444  * before dispatching them to the underlying device.
  445  *
  446  * if_transmit() requires that the mbuf be consumed by this call
  447  * regardless of the return condition.
  448  */
  449 int
  450 ieee80211_vap_transmit(struct ifnet *ifp, struct mbuf *m)
  451 {
  452         struct ieee80211vap *vap = ifp->if_softc;
  453         struct ieee80211com *ic = vap->iv_ic;
  454 
  455         /*
  456          * No data frames go out unless we're running.
  457          * Note in particular this covers CAC and CSA
  458          * states (though maybe we should check muting
  459          * for CSA).
  460          */
  461         if (vap->iv_state != IEEE80211_S_RUN &&
  462             vap->iv_state != IEEE80211_S_SLEEP) {
  463                 IEEE80211_LOCK(ic);
  464                 /* re-check under the com lock to avoid races */
  465                 if (vap->iv_state != IEEE80211_S_RUN &&
  466                     vap->iv_state != IEEE80211_S_SLEEP) {
  467                         IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
  468                             "%s: ignore queue, in %s state\n",
  469                             __func__, ieee80211_state_name[vap->iv_state]);
  470                         vap->iv_stats.is_tx_badstate++;
  471                         IEEE80211_UNLOCK(ic);
  472                         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
  473                         m_freem(m);
  474                         if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  475                         return (ENETDOWN);
  476                 }
  477                 IEEE80211_UNLOCK(ic);
  478         }
  479 
  480         /*
  481          * Sanitize mbuf flags for net80211 use.  We cannot
  482          * clear M_PWR_SAV or M_MORE_DATA because these may
  483          * be set for frames that are re-submitted from the
  484          * power save queue.
  485          *
  486          * NB: This must be done before ieee80211_classify as
  487          *     it marks EAPOL in frames with M_EAPOL.
  488          */
  489         m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
  490 
  491         /*
  492          * Bump to the packet transmission path.
  493          * The mbuf will be consumed here.
  494          */
  495         return (ieee80211_start_pkt(vap, m));
  496 }
  497 
  498 void
  499 ieee80211_vap_qflush(struct ifnet *ifp)
  500 {
  501 
  502         /* Empty for now */
  503 }
  504 
  505 /*
  506  * 802.11 raw output routine.
  507  *
  508  * XXX TODO: this (and other send routines) should correctly
  509  * XXX keep the pwr mgmt bit set if it decides to call into the
  510  * XXX driver to send a frame whilst the state is SLEEP.
  511  *
  512  * Otherwise the peer may decide that we're awake and flood us
  513  * with traffic we are still too asleep to receive!
  514  */
  515 int
  516 ieee80211_raw_output(struct ieee80211vap *vap, struct ieee80211_node *ni,
  517     struct mbuf *m, const struct ieee80211_bpf_params *params)
  518 {
  519         struct ieee80211com *ic = vap->iv_ic;
  520         int error;
  521 
  522         /*
  523          * Set node - the caller has taken a reference, so ensure
  524          * that the mbuf has the same node value that
  525          * it would if it were going via the normal path.
  526          */
  527         m->m_pkthdr.rcvif = (void *)ni;
  528 
  529         /*
  530          * Attempt to add bpf transmit parameters.
  531          *
  532          * For now it's ok to fail; the raw_xmit api still takes
  533          * them as an option.
  534          *
  535          * Later on when ic_raw_xmit() has params removed,
  536          * they'll have to be added - so fail the transmit if
  537          * they can't be.
  538          */
  539         if (params)
  540                 (void) ieee80211_add_xmit_params(m, params);
  541 
  542         error = ic->ic_raw_xmit(ni, m, params);
  543         if (error) {
  544                 if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, 1);
  545                 ieee80211_free_node(ni);
  546         }
  547         return (error);
  548 }
  549 
  550 /*
  551  * 802.11 output routine. This is (currently) used only to
  552  * connect bpf write calls to the 802.11 layer for injecting
  553  * raw 802.11 frames.
  554  */
  555 int
  556 ieee80211_output(struct ifnet *ifp, struct mbuf *m,
  557         const struct sockaddr *dst, struct route *ro)
  558 {
  559 #define senderr(e) do { error = (e); goto bad;} while (0)
  560         struct ieee80211_node *ni = NULL;
  561         struct ieee80211vap *vap;
  562         struct ieee80211_frame *wh;
  563         struct ieee80211com *ic = NULL;
  564         int error;
  565         int ret;
  566 
  567         if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
  568                 /*
  569                  * Short-circuit requests if the vap is marked OACTIVE
  570                  * as this can happen because a packet came down through
  571                  * ieee80211_start before the vap entered RUN state in
  572                  * which case it's ok to just drop the frame.  This
  573                  * should not be necessary but callers of if_output don't
  574                  * check OACTIVE.
  575                  */
  576                 senderr(ENETDOWN);
  577         }
  578         vap = ifp->if_softc;
  579         ic = vap->iv_ic;
  580         /*
  581          * Hand to the 802.3 code if not tagged as
  582          * a raw 802.11 frame.
  583          */
  584         if (dst->sa_family != AF_IEEE80211)
  585                 return vap->iv_output(ifp, m, dst, ro);
  586 #ifdef MAC
  587         error = mac_ifnet_check_transmit(ifp, m);
  588         if (error)
  589                 senderr(error);
  590 #endif
  591         if (ifp->if_flags & IFF_MONITOR)
  592                 senderr(ENETDOWN);
  593         if (!IFNET_IS_UP_RUNNING(ifp))
  594                 senderr(ENETDOWN);
  595         if (vap->iv_state == IEEE80211_S_CAC) {
  596                 IEEE80211_DPRINTF(vap,
  597                     IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
  598                     "block %s frame in CAC state\n", "raw data");
  599                 vap->iv_stats.is_tx_badstate++;
  600                 senderr(EIO);           /* XXX */
  601         } else if (vap->iv_state == IEEE80211_S_SCAN)
  602                 senderr(EIO);
  603         /* XXX bypass bridge, pfil, carp, etc. */
  604 
  605         if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
  606                 senderr(EIO);   /* XXX */
  607         wh = mtod(m, struct ieee80211_frame *);
  608         if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
  609             IEEE80211_FC0_VERSION_0)
  610                 senderr(EIO);   /* XXX */
  611         if (m->m_pkthdr.len < ieee80211_anyhdrsize(wh))
  612                 senderr(EIO);   /* XXX */
  613 
  614         /* locate destination node */
  615         switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
  616         case IEEE80211_FC1_DIR_NODS:
  617         case IEEE80211_FC1_DIR_FROMDS:
  618                 ni = ieee80211_find_txnode(vap, wh->i_addr1);
  619                 break;
  620         case IEEE80211_FC1_DIR_TODS:
  621         case IEEE80211_FC1_DIR_DSTODS:
  622                 ni = ieee80211_find_txnode(vap, wh->i_addr3);
  623                 break;
  624         default:
  625                 senderr(EIO);   /* XXX */
  626         }
  627         if (ni == NULL) {
  628                 /*
  629                  * Permit packets w/ bpf params through regardless
  630                  * (see below about sa_len).
  631                  */
  632                 if (dst->sa_len == 0)
  633                         senderr(EHOSTUNREACH);
  634                 ni = ieee80211_ref_node(vap->iv_bss);
  635         }
  636 
  637         /*
  638          * Sanitize mbuf for net80211 flags leaked from above.
  639          *
  640          * NB: This must be done before ieee80211_classify as
  641          *     it marks EAPOL in frames with M_EAPOL.
  642          */
  643         m->m_flags &= ~M_80211_TX;
  644 
  645         /* calculate priority so drivers can find the tx queue */
  646         /* XXX assumes an 802.3 frame */
  647         if (ieee80211_classify(ni, m))
  648                 senderr(EIO);           /* XXX */
  649 
  650         IEEE80211_NODE_STAT(ni, tx_data);
  651         if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
  652                 IEEE80211_NODE_STAT(ni, tx_mcast);
  653                 m->m_flags |= M_MCAST;
  654         } else
  655                 IEEE80211_NODE_STAT(ni, tx_ucast);
  656         /* NB: ieee80211_encap does not include 802.11 header */
  657         IEEE80211_NODE_STAT_ADD(ni, tx_bytes, m->m_pkthdr.len);
  658 
  659         IEEE80211_TX_LOCK(ic);
  660 
  661         /*
  662          * NB: DLT_IEEE802_11_RADIO identifies the parameters are
  663          * present by setting the sa_len field of the sockaddr (yes,
  664          * this is a hack).
  665          * NB: we assume sa_data is suitably aligned to cast.
  666          */
  667         ret = ieee80211_raw_output(vap, ni, m,
  668             (const struct ieee80211_bpf_params *)(dst->sa_len ?
  669                 dst->sa_data : NULL));
  670         IEEE80211_TX_UNLOCK(ic);
  671         return (ret);
  672 bad:
  673         if (m != NULL)
  674                 m_freem(m);
  675         if (ni != NULL)
  676                 ieee80211_free_node(ni);
  677         if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  678         return error;
  679 #undef senderr
  680 }
  681 
  682 /*
  683  * Set the direction field and address fields of an outgoing
  684  * frame.  Note this should be called early on in constructing
  685  * a frame as it sets i_fc[1]; other bits can then be or'd in.
  686  */
  687 void
  688 ieee80211_send_setup(
  689         struct ieee80211_node *ni,
  690         struct mbuf *m,
  691         int type, int tid,
  692         const uint8_t sa[IEEE80211_ADDR_LEN],
  693         const uint8_t da[IEEE80211_ADDR_LEN],
  694         const uint8_t bssid[IEEE80211_ADDR_LEN])
  695 {
  696 #define WH4(wh) ((struct ieee80211_frame_addr4 *)wh)
  697         struct ieee80211vap *vap = ni->ni_vap;
  698         struct ieee80211_tx_ampdu *tap;
  699         struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
  700         ieee80211_seq seqno;
  701 
  702         IEEE80211_TX_LOCK_ASSERT(ni->ni_ic);
  703 
  704         wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
  705         if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
  706                 switch (vap->iv_opmode) {
  707                 case IEEE80211_M_STA:
  708                         wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
  709                         IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
  710                         IEEE80211_ADDR_COPY(wh->i_addr2, sa);
  711                         IEEE80211_ADDR_COPY(wh->i_addr3, da);
  712                         break;
  713                 case IEEE80211_M_IBSS:
  714                 case IEEE80211_M_AHDEMO:
  715                         wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
  716                         IEEE80211_ADDR_COPY(wh->i_addr1, da);
  717                         IEEE80211_ADDR_COPY(wh->i_addr2, sa);
  718                         IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
  719                         break;
  720                 case IEEE80211_M_HOSTAP:
  721                         wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
  722                         IEEE80211_ADDR_COPY(wh->i_addr1, da);
  723                         IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
  724                         IEEE80211_ADDR_COPY(wh->i_addr3, sa);
  725                         break;
  726                 case IEEE80211_M_WDS:
  727                         wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
  728                         IEEE80211_ADDR_COPY(wh->i_addr1, da);
  729                         IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
  730                         IEEE80211_ADDR_COPY(wh->i_addr3, da);
  731                         IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
  732                         break;
  733                 case IEEE80211_M_MBSS:
  734 #ifdef IEEE80211_SUPPORT_MESH
  735                         if (IEEE80211_IS_MULTICAST(da)) {
  736                                 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
  737                                 /* XXX next hop */
  738                                 IEEE80211_ADDR_COPY(wh->i_addr1, da);
  739                                 IEEE80211_ADDR_COPY(wh->i_addr2,
  740                                     vap->iv_myaddr);
  741                         } else {
  742                                 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
  743                                 IEEE80211_ADDR_COPY(wh->i_addr1, da);
  744                                 IEEE80211_ADDR_COPY(wh->i_addr2,
  745                                     vap->iv_myaddr);
  746                                 IEEE80211_ADDR_COPY(wh->i_addr3, da);
  747                                 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
  748                         }
  749 #endif
  750                         break;
  751                 case IEEE80211_M_MONITOR:       /* NB: to quiet compiler */
  752                         break;
  753                 }
  754         } else {
  755                 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
  756                 IEEE80211_ADDR_COPY(wh->i_addr1, da);
  757                 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
  758 #ifdef IEEE80211_SUPPORT_MESH
  759                 if (vap->iv_opmode == IEEE80211_M_MBSS)
  760                         IEEE80211_ADDR_COPY(wh->i_addr3, sa);
  761                 else
  762 #endif
  763                         IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
  764         }
  765         *(uint16_t *)&wh->i_dur[0] = 0;
  766 
  767         tap = &ni->ni_tx_ampdu[tid];
  768         if (tid != IEEE80211_NONQOS_TID && IEEE80211_AMPDU_RUNNING(tap))
  769                 m->m_flags |= M_AMPDU_MPDU;
  770         else {
  771                 if (IEEE80211_HAS_SEQ(type & IEEE80211_FC0_TYPE_MASK,
  772                                       type & IEEE80211_FC0_SUBTYPE_MASK))
  773                         seqno = ni->ni_txseqs[tid]++;
  774                 else
  775                         seqno = 0;
  776 
  777                 *(uint16_t *)&wh->i_seq[0] =
  778                     htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
  779                 M_SEQNO_SET(m, seqno);
  780         }
  781 
  782         if (IEEE80211_IS_MULTICAST(wh->i_addr1))
  783                 m->m_flags |= M_MCAST;
  784 #undef WH4
  785 }
  786 
  787 /*
  788  * Send a management frame to the specified node.  The node pointer
  789  * must have a reference as the pointer will be passed to the driver
  790  * and potentially held for a long time.  If the frame is successfully
  791  * dispatched to the driver, then it is responsible for freeing the
  792  * reference (and potentially free'ing up any associated storage);
  793  * otherwise deal with reclaiming any reference (on error).
  794  */
  795 int
  796 ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
  797         struct ieee80211_bpf_params *params)
  798 {
  799         struct ieee80211vap *vap = ni->ni_vap;
  800         struct ieee80211com *ic = ni->ni_ic;
  801         struct ieee80211_frame *wh;
  802         int ret;
  803 
  804         KASSERT(ni != NULL, ("null node"));
  805 
  806         if (vap->iv_state == IEEE80211_S_CAC) {
  807                 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
  808                     ni, "block %s frame in CAC state",
  809                         ieee80211_mgt_subtype_name(type));
  810                 vap->iv_stats.is_tx_badstate++;
  811                 ieee80211_free_node(ni);
  812                 m_freem(m);
  813                 return EIO;             /* XXX */
  814         }
  815 
  816         M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
  817         if (m == NULL) {
  818                 ieee80211_free_node(ni);
  819                 return ENOMEM;
  820         }
  821 
  822         IEEE80211_TX_LOCK(ic);
  823 
  824         wh = mtod(m, struct ieee80211_frame *);
  825         ieee80211_send_setup(ni, m,
  826              IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
  827              vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
  828         if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
  829                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
  830                     "encrypting frame (%s)", __func__);
  831                 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
  832         }
  833         m->m_flags |= M_ENCAP;          /* mark encapsulated */
  834 
  835         KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
  836         M_WME_SETAC(m, params->ibp_pri);
  837 
  838 #ifdef IEEE80211_DEBUG
  839         /* avoid printing too many frames */
  840         if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
  841             ieee80211_msg_dumppkts(vap)) {
  842                 printf("[%s] send %s on channel %u\n",
  843                     ether_sprintf(wh->i_addr1),
  844                     ieee80211_mgt_subtype_name(type),
  845                     ieee80211_chan2ieee(ic, ic->ic_curchan));
  846         }
  847 #endif
  848         IEEE80211_NODE_STAT(ni, tx_mgmt);
  849 
  850         ret = ieee80211_raw_output(vap, ni, m, params);
  851         IEEE80211_TX_UNLOCK(ic);
  852         return (ret);
  853 }
  854 
  855 static void
  856 ieee80211_nulldata_transmitted(struct ieee80211_node *ni, void *arg,
  857     int status)
  858 {
  859         struct ieee80211vap *vap = ni->ni_vap;
  860 
  861         wakeup(vap);
  862 }
  863 
  864 /*
  865  * Send a null data frame to the specified node.  If the station
  866  * is setup for QoS then a QoS Null Data frame is constructed.
  867  * If this is a WDS station then a 4-address frame is constructed.
  868  *
  869  * NB: the caller is assumed to have setup a node reference
  870  *     for use; this is necessary to deal with a race condition
  871  *     when probing for inactive stations.  Like ieee80211_mgmt_output
  872  *     we must cleanup any node reference on error;  however we
  873  *     can safely just unref it as we know it will never be the
  874  *     last reference to the node.
  875  */
  876 int
  877 ieee80211_send_nulldata(struct ieee80211_node *ni)
  878 {
  879         struct ieee80211vap *vap = ni->ni_vap;
  880         struct ieee80211com *ic = ni->ni_ic;
  881         struct mbuf *m;
  882         struct ieee80211_frame *wh;
  883         int hdrlen;
  884         uint8_t *frm;
  885         int ret;
  886 
  887         if (vap->iv_state == IEEE80211_S_CAC) {
  888                 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
  889                     ni, "block %s frame in CAC state", "null data");
  890                 ieee80211_unref_node(&ni);
  891                 vap->iv_stats.is_tx_badstate++;
  892                 return EIO;             /* XXX */
  893         }
  894 
  895         if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
  896                 hdrlen = sizeof(struct ieee80211_qosframe);
  897         else
  898                 hdrlen = sizeof(struct ieee80211_frame);
  899         /* NB: only WDS vap's get 4-address frames */
  900         if (vap->iv_opmode == IEEE80211_M_WDS)
  901                 hdrlen += IEEE80211_ADDR_LEN;
  902         if (ic->ic_flags & IEEE80211_F_DATAPAD)
  903                 hdrlen = roundup(hdrlen, sizeof(uint32_t));
  904 
  905         m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
  906         if (m == NULL) {
  907                 /* XXX debug msg */
  908                 ieee80211_unref_node(&ni);
  909                 vap->iv_stats.is_tx_nobuf++;
  910                 return ENOMEM;
  911         }
  912         KASSERT(M_LEADINGSPACE(m) >= hdrlen,
  913             ("leading space %zd", M_LEADINGSPACE(m)));
  914         M_PREPEND(m, hdrlen, M_NOWAIT);
  915         if (m == NULL) {
  916                 /* NB: cannot happen */
  917                 ieee80211_free_node(ni);
  918                 return ENOMEM;
  919         }
  920 
  921         IEEE80211_TX_LOCK(ic);
  922 
  923         wh = mtod(m, struct ieee80211_frame *);         /* NB: a little lie */
  924         if (ni->ni_flags & IEEE80211_NODE_QOS) {
  925                 const int tid = WME_AC_TO_TID(WME_AC_BE);
  926                 uint8_t *qos;
  927 
  928                 ieee80211_send_setup(ni, m,
  929                     IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
  930                     tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
  931 
  932                 if (vap->iv_opmode == IEEE80211_M_WDS)
  933                         qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
  934                 else
  935                         qos = ((struct ieee80211_qosframe *) wh)->i_qos;
  936                 qos[0] = tid & IEEE80211_QOS_TID;
  937                 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
  938                         qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
  939                 qos[1] = 0;
  940         } else {
  941                 ieee80211_send_setup(ni, m,
  942                     IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
  943                     IEEE80211_NONQOS_TID,
  944                     vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
  945         }
  946         if (vap->iv_opmode != IEEE80211_M_WDS) {
  947                 /* NB: power management bit is never sent by an AP */
  948                 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
  949                     vap->iv_opmode != IEEE80211_M_HOSTAP)
  950                         wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
  951         }
  952         if ((ic->ic_flags & IEEE80211_F_SCAN) &&
  953             (ni->ni_flags & IEEE80211_NODE_PWR_MGT)) {
  954                 ieee80211_add_callback(m, ieee80211_nulldata_transmitted,
  955                     NULL);
  956         }
  957         m->m_len = m->m_pkthdr.len = hdrlen;
  958         m->m_flags |= M_ENCAP;          /* mark encapsulated */
  959 
  960         M_WME_SETAC(m, WME_AC_BE);
  961 
  962         IEEE80211_NODE_STAT(ni, tx_data);
  963 
  964         IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
  965             "send %snull data frame on channel %u, pwr mgt %s",
  966             ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
  967             ieee80211_chan2ieee(ic, ic->ic_curchan),
  968             wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
  969 
  970         ret = ieee80211_raw_output(vap, ni, m, NULL);
  971         IEEE80211_TX_UNLOCK(ic);
  972         return (ret);
  973 }
  974 
  975 /* 
  976  * Assign priority to a frame based on any vlan tag assigned
  977  * to the station and/or any Diffserv setting in an IP header.
  978  * Finally, if an ACM policy is setup (in station mode) it's
  979  * applied.
  980  */
  981 int
  982 ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
  983 {
  984         const struct ether_header *eh = mtod(m, struct ether_header *);
  985         int v_wme_ac, d_wme_ac, ac;
  986 
  987         /*
  988          * Always promote PAE/EAPOL frames to high priority.
  989          */
  990         if (eh->ether_type == htons(ETHERTYPE_PAE)) {
  991                 /* NB: mark so others don't need to check header */
  992                 m->m_flags |= M_EAPOL;
  993                 ac = WME_AC_VO;
  994                 goto done;
  995         }
  996         /*
  997          * Non-qos traffic goes to BE.
  998          */
  999         if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
 1000                 ac = WME_AC_BE;
 1001                 goto done;
 1002         }
 1003 
 1004         /* 
 1005          * If node has a vlan tag then all traffic
 1006          * to it must have a matching tag.
 1007          */
 1008         v_wme_ac = 0;
 1009         if (ni->ni_vlan != 0) {
 1010                  if ((m->m_flags & M_VLANTAG) == 0) {
 1011                         IEEE80211_NODE_STAT(ni, tx_novlantag);
 1012                         return 1;
 1013                 }
 1014                 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
 1015                     EVL_VLANOFTAG(ni->ni_vlan)) {
 1016                         IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
 1017                         return 1;
 1018                 }
 1019                 /* map vlan priority to AC */
 1020                 v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
 1021         }
 1022 
 1023         /* XXX m_copydata may be too slow for fast path */
 1024 #ifdef INET
 1025         if (eh->ether_type == htons(ETHERTYPE_IP)) {
 1026                 uint8_t tos;
 1027                 /*
 1028                  * IP frame, map the DSCP bits from the TOS field.
 1029                  */
 1030                 /* NB: ip header may not be in first mbuf */
 1031                 m_copydata(m, sizeof(struct ether_header) +
 1032                     offsetof(struct ip, ip_tos), sizeof(tos), &tos);
 1033                 tos >>= 5;              /* NB: ECN + low 3 bits of DSCP */
 1034                 d_wme_ac = TID_TO_WME_AC(tos);
 1035         } else {
 1036 #endif /* INET */
 1037 #ifdef INET6
 1038         if (eh->ether_type == htons(ETHERTYPE_IPV6)) {
 1039                 uint32_t flow;
 1040                 uint8_t tos;
 1041                 /*
 1042                  * IPv6 frame, map the DSCP bits from the traffic class field.
 1043                  */
 1044                 m_copydata(m, sizeof(struct ether_header) +
 1045                     offsetof(struct ip6_hdr, ip6_flow), sizeof(flow),
 1046                     (caddr_t) &flow);
 1047                 tos = (uint8_t)(ntohl(flow) >> 20);
 1048                 tos >>= 5;              /* NB: ECN + low 3 bits of DSCP */
 1049                 d_wme_ac = TID_TO_WME_AC(tos);
 1050         } else {
 1051 #endif /* INET6 */
 1052                 d_wme_ac = WME_AC_BE;
 1053 #ifdef INET6
 1054         }
 1055 #endif
 1056 #ifdef INET
 1057         }
 1058 #endif
 1059         /*
 1060          * Use highest priority AC.
 1061          */
 1062         if (v_wme_ac > d_wme_ac)
 1063                 ac = v_wme_ac;
 1064         else
 1065                 ac = d_wme_ac;
 1066 
 1067         /*
 1068          * Apply ACM policy.
 1069          */
 1070         if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
 1071                 static const int acmap[4] = {
 1072                         WME_AC_BK,      /* WME_AC_BE */
 1073                         WME_AC_BK,      /* WME_AC_BK */
 1074                         WME_AC_BE,      /* WME_AC_VI */
 1075                         WME_AC_VI,      /* WME_AC_VO */
 1076                 };
 1077                 struct ieee80211com *ic = ni->ni_ic;
 1078 
 1079                 while (ac != WME_AC_BK &&
 1080                     ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
 1081                         ac = acmap[ac];
 1082         }
 1083 done:
 1084         M_WME_SETAC(m, ac);
 1085         return 0;
 1086 }
 1087 
 1088 /*
 1089  * Insure there is sufficient contiguous space to encapsulate the
 1090  * 802.11 data frame.  If room isn't already there, arrange for it.
 1091  * Drivers and cipher modules assume we have done the necessary work
 1092  * and fail rudely if they don't find the space they need.
 1093  */
 1094 struct mbuf *
 1095 ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
 1096         struct ieee80211_key *key, struct mbuf *m)
 1097 {
 1098 #define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc))
 1099         int needed_space = vap->iv_ic->ic_headroom + hdrsize;
 1100 
 1101         if (key != NULL) {
 1102                 /* XXX belongs in crypto code? */
 1103                 needed_space += key->wk_cipher->ic_header;
 1104                 /* XXX frags */
 1105                 /*
 1106                  * When crypto is being done in the host we must insure
 1107                  * the data are writable for the cipher routines; clone
 1108                  * a writable mbuf chain.
 1109                  * XXX handle SWMIC specially
 1110                  */
 1111                 if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
 1112                         m = m_unshare(m, M_NOWAIT);
 1113                         if (m == NULL) {
 1114                                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
 1115                                     "%s: cannot get writable mbuf\n", __func__);
 1116                                 vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
 1117                                 return NULL;
 1118                         }
 1119                 }
 1120         }
 1121         /*
 1122          * We know we are called just before stripping an Ethernet
 1123          * header and prepending an LLC header.  This means we know
 1124          * there will be
 1125          *      sizeof(struct ether_header) - sizeof(struct llc)
 1126          * bytes recovered to which we need additional space for the
 1127          * 802.11 header and any crypto header.
 1128          */
 1129         /* XXX check trailing space and copy instead? */
 1130         if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
 1131                 struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
 1132                 if (n == NULL) {
 1133                         IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
 1134                             "%s: cannot expand storage\n", __func__);
 1135                         vap->iv_stats.is_tx_nobuf++;
 1136                         m_freem(m);
 1137                         return NULL;
 1138                 }
 1139                 KASSERT(needed_space <= MHLEN,
 1140                     ("not enough room, need %u got %d\n", needed_space, MHLEN));
 1141                 /*
 1142                  * Setup new mbuf to have leading space to prepend the
 1143                  * 802.11 header and any crypto header bits that are
 1144                  * required (the latter are added when the driver calls
 1145                  * back to ieee80211_crypto_encap to do crypto encapsulation).
 1146                  */
 1147                 /* NB: must be first 'cuz it clobbers m_data */
 1148                 m_move_pkthdr(n, m);
 1149                 n->m_len = 0;                   /* NB: m_gethdr does not set */
 1150                 n->m_data += needed_space;
 1151                 /*
 1152                  * Pull up Ethernet header to create the expected layout.
 1153                  * We could use m_pullup but that's overkill (i.e. we don't
 1154                  * need the actual data) and it cannot fail so do it inline
 1155                  * for speed.
 1156                  */
 1157                 /* NB: struct ether_header is known to be contiguous */
 1158                 n->m_len += sizeof(struct ether_header);
 1159                 m->m_len -= sizeof(struct ether_header);
 1160                 m->m_data += sizeof(struct ether_header);
 1161                 /*
 1162                  * Replace the head of the chain.
 1163                  */
 1164                 n->m_next = m;
 1165                 m = n;
 1166         }
 1167         return m;
 1168 #undef TO_BE_RECLAIMED
 1169 }
 1170 
 1171 /*
 1172  * Return the transmit key to use in sending a unicast frame.
 1173  * If a unicast key is set we use that.  When no unicast key is set
 1174  * we fall back to the default transmit key.
 1175  */ 
 1176 static __inline struct ieee80211_key *
 1177 ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
 1178         struct ieee80211_node *ni)
 1179 {
 1180         if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
 1181                 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
 1182                     IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
 1183                         return NULL;
 1184                 return &vap->iv_nw_keys[vap->iv_def_txkey];
 1185         } else {
 1186                 return &ni->ni_ucastkey;
 1187         }
 1188 }
 1189 
 1190 /*
 1191  * Return the transmit key to use in sending a multicast frame.
 1192  * Multicast traffic always uses the group key which is installed as
 1193  * the default tx key.
 1194  */ 
 1195 static __inline struct ieee80211_key *
 1196 ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
 1197         struct ieee80211_node *ni)
 1198 {
 1199         if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
 1200             IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
 1201                 return NULL;
 1202         return &vap->iv_nw_keys[vap->iv_def_txkey];
 1203 }
 1204 
 1205 /*
 1206  * Encapsulate an outbound data frame.  The mbuf chain is updated.
 1207  * If an error is encountered NULL is returned.  The caller is required
 1208  * to provide a node reference and pullup the ethernet header in the
 1209  * first mbuf.
 1210  *
 1211  * NB: Packet is assumed to be processed by ieee80211_classify which
 1212  *     marked EAPOL frames w/ M_EAPOL.
 1213  */
 1214 struct mbuf *
 1215 ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
 1216     struct mbuf *m)
 1217 {
 1218 #define WH4(wh) ((struct ieee80211_frame_addr4 *)(wh))
 1219 #define MC01(mc)        ((struct ieee80211_meshcntl_ae01 *)mc)
 1220         struct ieee80211com *ic = ni->ni_ic;
 1221 #ifdef IEEE80211_SUPPORT_MESH
 1222         struct ieee80211_mesh_state *ms = vap->iv_mesh;
 1223         struct ieee80211_meshcntl_ae10 *mc;
 1224         struct ieee80211_mesh_route *rt = NULL;
 1225         int dir = -1;
 1226 #endif
 1227         struct ether_header eh;
 1228         struct ieee80211_frame *wh;
 1229         struct ieee80211_key *key;
 1230         struct llc *llc;
 1231         int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr;
 1232         ieee80211_seq seqno;
 1233         int meshhdrsize, meshae;
 1234         uint8_t *qos;
 1235         int is_amsdu = 0;
 1236         
 1237         IEEE80211_TX_LOCK_ASSERT(ic);
 1238 
 1239         /*
 1240          * Copy existing Ethernet header to a safe place.  The
 1241          * rest of the code assumes it's ok to strip it when
 1242          * reorganizing state for the final encapsulation.
 1243          */
 1244         KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
 1245         ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
 1246 
 1247         /*
 1248          * Insure space for additional headers.  First identify
 1249          * transmit key to use in calculating any buffer adjustments
 1250          * required.  This is also used below to do privacy
 1251          * encapsulation work.  Then calculate the 802.11 header
 1252          * size and any padding required by the driver.
 1253          *
 1254          * Note key may be NULL if we fall back to the default
 1255          * transmit key and that is not set.  In that case the
 1256          * buffer may not be expanded as needed by the cipher
 1257          * routines, but they will/should discard it.
 1258          */
 1259         if (vap->iv_flags & IEEE80211_F_PRIVACY) {
 1260                 if (vap->iv_opmode == IEEE80211_M_STA ||
 1261                     !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
 1262                     (vap->iv_opmode == IEEE80211_M_WDS &&
 1263                      (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
 1264                         key = ieee80211_crypto_getucastkey(vap, ni);
 1265                 else
 1266                         key = ieee80211_crypto_getmcastkey(vap, ni);
 1267                 if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
 1268                         IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
 1269                             eh.ether_dhost,
 1270                             "no default transmit key (%s) deftxkey %u",
 1271                             __func__, vap->iv_def_txkey);
 1272                         vap->iv_stats.is_tx_nodefkey++;
 1273                         goto bad;
 1274                 }
 1275         } else
 1276                 key = NULL;
 1277         /*
 1278          * XXX Some ap's don't handle QoS-encapsulated EAPOL
 1279          * frames so suppress use.  This may be an issue if other
 1280          * ap's require all data frames to be QoS-encapsulated
 1281          * once negotiated in which case we'll need to make this
 1282          * configurable.
 1283          * NB: mesh data frames are QoS.
 1284          */
 1285         addqos = ((ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) ||
 1286             (vap->iv_opmode == IEEE80211_M_MBSS)) &&
 1287             (m->m_flags & M_EAPOL) == 0;
 1288         if (addqos)
 1289                 hdrsize = sizeof(struct ieee80211_qosframe);
 1290         else
 1291                 hdrsize = sizeof(struct ieee80211_frame);
 1292 #ifdef IEEE80211_SUPPORT_MESH
 1293         if (vap->iv_opmode == IEEE80211_M_MBSS) {
 1294                 /*
 1295                  * Mesh data frames are encapsulated according to the
 1296                  * rules of Section 11B.8.5 (p.139 of D3.0 spec).
 1297                  * o Group Addressed data (aka multicast) originating
 1298                  *   at the local sta are sent w/ 3-address format and
 1299                  *   address extension mode 00
 1300                  * o Individually Addressed data (aka unicast) originating
 1301                  *   at the local sta are sent w/ 4-address format and
 1302                  *   address extension mode 00
 1303                  * o Group Addressed data forwarded from a non-mesh sta are
 1304                  *   sent w/ 3-address format and address extension mode 01
 1305                  * o Individually Address data from another sta are sent
 1306                  *   w/ 4-address format and address extension mode 10
 1307                  */
 1308                 is4addr = 0;            /* NB: don't use, disable */
 1309                 if (!IEEE80211_IS_MULTICAST(eh.ether_dhost)) {
 1310                         rt = ieee80211_mesh_rt_find(vap, eh.ether_dhost);
 1311                         KASSERT(rt != NULL, ("route is NULL"));
 1312                         dir = IEEE80211_FC1_DIR_DSTODS;
 1313                         hdrsize += IEEE80211_ADDR_LEN;
 1314                         if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
 1315                                 if (IEEE80211_ADDR_EQ(rt->rt_mesh_gate,
 1316                                     vap->iv_myaddr)) {
 1317                                         IEEE80211_NOTE_MAC(vap,
 1318                                             IEEE80211_MSG_MESH,
 1319                                             eh.ether_dhost,
 1320                                             "%s", "trying to send to ourself");
 1321                                         goto bad;
 1322                                 }
 1323                                 meshae = IEEE80211_MESH_AE_10;
 1324                                 meshhdrsize =
 1325                                     sizeof(struct ieee80211_meshcntl_ae10);
 1326                         } else {
 1327                                 meshae = IEEE80211_MESH_AE_00;
 1328                                 meshhdrsize =
 1329                                     sizeof(struct ieee80211_meshcntl);
 1330                         }
 1331                 } else {
 1332                         dir = IEEE80211_FC1_DIR_FROMDS;
 1333                         if (!IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) {
 1334                                 /* proxy group */
 1335                                 meshae = IEEE80211_MESH_AE_01;
 1336                                 meshhdrsize =
 1337                                     sizeof(struct ieee80211_meshcntl_ae01);
 1338                         } else {
 1339                                 /* group */
 1340                                 meshae = IEEE80211_MESH_AE_00;
 1341                                 meshhdrsize = sizeof(struct ieee80211_meshcntl);
 1342                         }
 1343                 }
 1344         } else {
 1345 #endif
 1346                 /*
 1347                  * 4-address frames need to be generated for:
 1348                  * o packets sent through a WDS vap (IEEE80211_M_WDS)
 1349                  * o packets sent through a vap marked for relaying
 1350                  *   (e.g. a station operating with dynamic WDS)
 1351                  */
 1352                 is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
 1353                     ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
 1354                      !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
 1355                 if (is4addr)
 1356                         hdrsize += IEEE80211_ADDR_LEN;
 1357                 meshhdrsize = meshae = 0;
 1358 #ifdef IEEE80211_SUPPORT_MESH
 1359         }
 1360 #endif
 1361         /*
 1362          * Honor driver DATAPAD requirement.
 1363          */
 1364         if (ic->ic_flags & IEEE80211_F_DATAPAD)
 1365                 hdrspace = roundup(hdrsize, sizeof(uint32_t));
 1366         else
 1367                 hdrspace = hdrsize;
 1368 
 1369         if (__predict_true((m->m_flags & M_FF) == 0)) {
 1370                 /*
 1371                  * Normal frame.
 1372                  */
 1373                 m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m);
 1374                 if (m == NULL) {
 1375                         /* NB: ieee80211_mbuf_adjust handles msgs+statistics */
 1376                         goto bad;
 1377                 }
 1378                 /* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
 1379                 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
 1380                 llc = mtod(m, struct llc *);
 1381                 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
 1382                 llc->llc_control = LLC_UI;
 1383                 llc->llc_snap.org_code[0] = 0;
 1384                 llc->llc_snap.org_code[1] = 0;
 1385                 llc->llc_snap.org_code[2] = 0;
 1386                 llc->llc_snap.ether_type = eh.ether_type;
 1387         } else {
 1388 #ifdef IEEE80211_SUPPORT_SUPERG
 1389                 /*
 1390                  * Aggregated frame.  Check if it's for AMSDU or FF.
 1391                  *
 1392                  * XXX TODO: IEEE80211_NODE_AMSDU* isn't implemented
 1393                  * anywhere for some reason.  But, since 11n requires
 1394                  * AMSDU RX, we can just assume "11n" == "AMSDU".
 1395                  */
 1396                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, "%s: called; M_FF\n", __func__);
 1397                 if (ieee80211_amsdu_tx_ok(ni)) {
 1398                         m = ieee80211_amsdu_encap(vap, m, hdrspace + meshhdrsize, key);
 1399                         is_amsdu = 1;
 1400                 } else {
 1401                         m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key);
 1402                 }
 1403                 if (m == NULL)
 1404 #endif
 1405                         goto bad;
 1406         }
 1407         datalen = m->m_pkthdr.len;              /* NB: w/o 802.11 header */
 1408 
 1409         M_PREPEND(m, hdrspace + meshhdrsize, M_NOWAIT);
 1410         if (m == NULL) {
 1411                 vap->iv_stats.is_tx_nobuf++;
 1412                 goto bad;
 1413         }
 1414         wh = mtod(m, struct ieee80211_frame *);
 1415         wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
 1416         *(uint16_t *)wh->i_dur = 0;
 1417         qos = NULL;     /* NB: quiet compiler */
 1418         if (is4addr) {
 1419                 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
 1420                 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
 1421                 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
 1422                 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
 1423                 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
 1424         } else switch (vap->iv_opmode) {
 1425         case IEEE80211_M_STA:
 1426                 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
 1427                 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
 1428                 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
 1429                 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
 1430                 break;
 1431         case IEEE80211_M_IBSS:
 1432         case IEEE80211_M_AHDEMO:
 1433                 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
 1434                 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
 1435                 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
 1436                 /*
 1437                  * NB: always use the bssid from iv_bss as the
 1438                  *     neighbor's may be stale after an ibss merge
 1439                  */
 1440                 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
 1441                 break;
 1442         case IEEE80211_M_HOSTAP:
 1443                 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
 1444                 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
 1445                 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
 1446                 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
 1447                 break;
 1448 #ifdef IEEE80211_SUPPORT_MESH
 1449         case IEEE80211_M_MBSS:
 1450                 /* NB: offset by hdrspace to deal with DATAPAD */
 1451                 mc = (struct ieee80211_meshcntl_ae10 *)
 1452                      (mtod(m, uint8_t *) + hdrspace);
 1453                 wh->i_fc[1] = dir;
 1454                 switch (meshae) {
 1455                 case IEEE80211_MESH_AE_00:      /* no proxy */
 1456                         mc->mc_flags = 0;
 1457                         if (dir == IEEE80211_FC1_DIR_DSTODS) { /* ucast */
 1458                                 IEEE80211_ADDR_COPY(wh->i_addr1,
 1459                                     ni->ni_macaddr);
 1460                                 IEEE80211_ADDR_COPY(wh->i_addr2,
 1461                                     vap->iv_myaddr);
 1462                                 IEEE80211_ADDR_COPY(wh->i_addr3,
 1463                                     eh.ether_dhost);
 1464                                 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4,
 1465                                     eh.ether_shost);
 1466                                 qos =((struct ieee80211_qosframe_addr4 *)
 1467                                     wh)->i_qos;
 1468                         } else if (dir == IEEE80211_FC1_DIR_FROMDS) {
 1469                                  /* mcast */
 1470                                 IEEE80211_ADDR_COPY(wh->i_addr1,
 1471                                     eh.ether_dhost);
 1472                                 IEEE80211_ADDR_COPY(wh->i_addr2,
 1473                                     vap->iv_myaddr);
 1474                                 IEEE80211_ADDR_COPY(wh->i_addr3,
 1475                                     eh.ether_shost);
 1476                                 qos = ((struct ieee80211_qosframe *)
 1477                                     wh)->i_qos;
 1478                         }
 1479                         break;
 1480                 case IEEE80211_MESH_AE_01:      /* mcast, proxy */
 1481                         wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
 1482                         IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
 1483                         IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
 1484                         IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr);
 1485                         mc->mc_flags = 1;
 1486                         IEEE80211_ADDR_COPY(MC01(mc)->mc_addr4,
 1487                             eh.ether_shost);
 1488                         qos = ((struct ieee80211_qosframe *) wh)->i_qos;
 1489                         break;
 1490                 case IEEE80211_MESH_AE_10:      /* ucast, proxy */
 1491                         KASSERT(rt != NULL, ("route is NULL"));
 1492                         IEEE80211_ADDR_COPY(wh->i_addr1, rt->rt_nexthop);
 1493                         IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
 1494                         IEEE80211_ADDR_COPY(wh->i_addr3, rt->rt_mesh_gate);
 1495                         IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr);
 1496                         mc->mc_flags = IEEE80211_MESH_AE_10;
 1497                         IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_dhost);
 1498                         IEEE80211_ADDR_COPY(mc->mc_addr6, eh.ether_shost);
 1499                         qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
 1500                         break;
 1501                 default:
 1502                         KASSERT(0, ("meshae %d", meshae));
 1503                         break;
 1504                 }
 1505                 mc->mc_ttl = ms->ms_ttl;
 1506                 ms->ms_seq++;
 1507                 le32enc(mc->mc_seq, ms->ms_seq);
 1508                 break;
 1509 #endif
 1510         case IEEE80211_M_WDS:           /* NB: is4addr should always be true */
 1511         default:
 1512                 goto bad;
 1513         }
 1514         if (m->m_flags & M_MORE_DATA)
 1515                 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
 1516         if (addqos) {
 1517                 int ac, tid;
 1518 
 1519                 if (is4addr) {
 1520                         qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
 1521                 /* NB: mesh case handled earlier */
 1522                 } else if (vap->iv_opmode != IEEE80211_M_MBSS)
 1523                         qos = ((struct ieee80211_qosframe *) wh)->i_qos;
 1524                 ac = M_WME_GETAC(m);
 1525                 /* map from access class/queue to 11e header priorty value */
 1526                 tid = WME_AC_TO_TID(ac);
 1527                 qos[0] = tid & IEEE80211_QOS_TID;
 1528                 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
 1529                         qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
 1530 #ifdef IEEE80211_SUPPORT_MESH
 1531                 if (vap->iv_opmode == IEEE80211_M_MBSS)
 1532                         qos[1] = IEEE80211_QOS_MC;
 1533                 else
 1534 #endif
 1535                         qos[1] = 0;
 1536                 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
 1537 
 1538                 /*
 1539                  * If this is an A-MSDU then ensure we set the
 1540                  * relevant field.
 1541                  */
 1542                 if (is_amsdu)
 1543                         qos[0] |= IEEE80211_QOS_AMSDU;
 1544 
 1545                 if ((m->m_flags & M_AMPDU_MPDU) == 0) {
 1546                         /*
 1547                          * NB: don't assign a sequence # to potential
 1548                          * aggregates; we expect this happens at the
 1549                          * point the frame comes off any aggregation q
 1550                          * as otherwise we may introduce holes in the
 1551                          * BA sequence space and/or make window accouting
 1552                          * more difficult.
 1553                          *
 1554                          * XXX may want to control this with a driver
 1555                          * capability; this may also change when we pull
 1556                          * aggregation up into net80211
 1557                          */
 1558                         seqno = ni->ni_txseqs[tid]++;
 1559                         *(uint16_t *)wh->i_seq =
 1560                             htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
 1561                         M_SEQNO_SET(m, seqno);
 1562                 }
 1563         } else {
 1564                 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
 1565                 *(uint16_t *)wh->i_seq =
 1566                     htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
 1567                 M_SEQNO_SET(m, seqno);
 1568 
 1569                 /*
 1570                  * XXX TODO: we shouldn't allow EAPOL, etc that would
 1571                  * be forced to be non-QoS traffic to be A-MSDU encapsulated.
 1572                  */
 1573                 if (is_amsdu)
 1574                         printf("%s: XXX ERROR: is_amsdu set; not QoS!\n",
 1575                             __func__);
 1576         }
 1577 
 1578 
 1579         /* check if xmit fragmentation is required */
 1580         txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
 1581             !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
 1582             (vap->iv_caps & IEEE80211_C_TXFRAG) &&
 1583             (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
 1584         if (key != NULL) {
 1585                 /*
 1586                  * IEEE 802.1X: send EAPOL frames always in the clear.
 1587                  * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
 1588                  */
 1589                 if ((m->m_flags & M_EAPOL) == 0 ||
 1590                     ((vap->iv_flags & IEEE80211_F_WPA) &&
 1591                      (vap->iv_opmode == IEEE80211_M_STA ?
 1592                       !IEEE80211_KEY_UNDEFINED(key) :
 1593                       !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
 1594                         wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
 1595                         if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
 1596                                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
 1597                                     eh.ether_dhost,
 1598                                     "%s", "enmic failed, discard frame");
 1599                                 vap->iv_stats.is_crypto_enmicfail++;
 1600                                 goto bad;
 1601                         }
 1602                 }
 1603         }
 1604         if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
 1605             key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
 1606                 goto bad;
 1607 
 1608         m->m_flags |= M_ENCAP;          /* mark encapsulated */
 1609 
 1610         IEEE80211_NODE_STAT(ni, tx_data);
 1611         if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
 1612                 IEEE80211_NODE_STAT(ni, tx_mcast);
 1613                 m->m_flags |= M_MCAST;
 1614         } else
 1615                 IEEE80211_NODE_STAT(ni, tx_ucast);
 1616         IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
 1617 
 1618         return m;
 1619 bad:
 1620         if (m != NULL)
 1621                 m_freem(m);
 1622         return NULL;
 1623 #undef WH4
 1624 #undef MC01
 1625 }
 1626 
 1627 void
 1628 ieee80211_free_mbuf(struct mbuf *m)
 1629 {
 1630         struct mbuf *next;
 1631 
 1632         if (m == NULL)
 1633                 return;
 1634 
 1635         do {
 1636                 next = m->m_nextpkt;
 1637                 m->m_nextpkt = NULL;
 1638                 m_freem(m);
 1639         } while ((m = next) != NULL);
 1640 }
 1641 
 1642 /*
 1643  * Fragment the frame according to the specified mtu.
 1644  * The size of the 802.11 header (w/o padding) is provided
 1645  * so we don't need to recalculate it.  We create a new
 1646  * mbuf for each fragment and chain it through m_nextpkt;
 1647  * we might be able to optimize this by reusing the original
 1648  * packet's mbufs but that is significantly more complicated.
 1649  */
 1650 static int
 1651 ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
 1652         u_int hdrsize, u_int ciphdrsize, u_int mtu)
 1653 {
 1654         struct ieee80211com *ic = vap->iv_ic;
 1655         struct ieee80211_frame *wh, *whf;
 1656         struct mbuf *m, *prev;
 1657         u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
 1658         u_int hdrspace;
 1659 
 1660         KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
 1661         KASSERT(m0->m_pkthdr.len > mtu,
 1662                 ("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
 1663 
 1664         /*
 1665          * Honor driver DATAPAD requirement.
 1666          */
 1667         if (ic->ic_flags & IEEE80211_F_DATAPAD)
 1668                 hdrspace = roundup(hdrsize, sizeof(uint32_t));
 1669         else
 1670                 hdrspace = hdrsize;
 1671 
 1672         wh = mtod(m0, struct ieee80211_frame *);
 1673         /* NB: mark the first frag; it will be propagated below */
 1674         wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
 1675         totalhdrsize = hdrspace + ciphdrsize;
 1676         fragno = 1;
 1677         off = mtu - ciphdrsize;
 1678         remainder = m0->m_pkthdr.len - off;
 1679         prev = m0;
 1680         do {
 1681                 fragsize = MIN(totalhdrsize + remainder, mtu);
 1682                 m = m_get2(fragsize, M_NOWAIT, MT_DATA, M_PKTHDR);
 1683                 if (m == NULL)
 1684                         goto bad;
 1685                 /* leave room to prepend any cipher header */
 1686                 m_align(m, fragsize - ciphdrsize);
 1687 
 1688                 /*
 1689                  * Form the header in the fragment.  Note that since
 1690                  * we mark the first fragment with the MORE_FRAG bit
 1691                  * it automatically is propagated to each fragment; we
 1692                  * need only clear it on the last fragment (done below).
 1693                  * NB: frag 1+ dont have Mesh Control field present.
 1694                  */
 1695                 whf = mtod(m, struct ieee80211_frame *);
 1696                 memcpy(whf, wh, hdrsize);
 1697 #ifdef IEEE80211_SUPPORT_MESH
 1698                 if (vap->iv_opmode == IEEE80211_M_MBSS)
 1699                         ieee80211_getqos(wh)[1] &= ~IEEE80211_QOS_MC;
 1700 #endif
 1701                 *(uint16_t *)&whf->i_seq[0] |= htole16(
 1702                         (fragno & IEEE80211_SEQ_FRAG_MASK) <<
 1703                                 IEEE80211_SEQ_FRAG_SHIFT);
 1704                 fragno++;
 1705 
 1706                 payload = fragsize - totalhdrsize;
 1707                 /* NB: destination is known to be contiguous */
 1708 
 1709                 m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrspace);
 1710                 m->m_len = hdrspace + payload;
 1711                 m->m_pkthdr.len = hdrspace + payload;
 1712                 m->m_flags |= M_FRAG;
 1713 
 1714                 /* chain up the fragment */
 1715                 prev->m_nextpkt = m;
 1716                 prev = m;
 1717 
 1718                 /* deduct fragment just formed */
 1719                 remainder -= payload;
 1720                 off += payload;
 1721         } while (remainder != 0);
 1722 
 1723         /* set the last fragment */
 1724         m->m_flags |= M_LASTFRAG;
 1725         whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
 1726 
 1727         /* strip first mbuf now that everything has been copied */
 1728         m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
 1729         m0->m_flags |= M_FIRSTFRAG | M_FRAG;
 1730 
 1731         vap->iv_stats.is_tx_fragframes++;
 1732         vap->iv_stats.is_tx_frags += fragno-1;
 1733 
 1734         return 1;
 1735 bad:
 1736         /* reclaim fragments but leave original frame for caller to free */
 1737         ieee80211_free_mbuf(m0->m_nextpkt);
 1738         m0->m_nextpkt = NULL;
 1739         return 0;
 1740 }
 1741 
 1742 /*
 1743  * Add a supported rates element id to a frame.
 1744  */
 1745 uint8_t *
 1746 ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
 1747 {
 1748         int nrates;
 1749 
 1750         *frm++ = IEEE80211_ELEMID_RATES;
 1751         nrates = rs->rs_nrates;
 1752         if (nrates > IEEE80211_RATE_SIZE)
 1753                 nrates = IEEE80211_RATE_SIZE;
 1754         *frm++ = nrates;
 1755         memcpy(frm, rs->rs_rates, nrates);
 1756         return frm + nrates;
 1757 }
 1758 
 1759 /*
 1760  * Add an extended supported rates element id to a frame.
 1761  */
 1762 uint8_t *
 1763 ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
 1764 {
 1765         /*
 1766          * Add an extended supported rates element if operating in 11g mode.
 1767          */
 1768         if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
 1769                 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
 1770                 *frm++ = IEEE80211_ELEMID_XRATES;
 1771                 *frm++ = nrates;
 1772                 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
 1773                 frm += nrates;
 1774         }
 1775         return frm;
 1776 }
 1777 
 1778 /* 
 1779  * Add an ssid element to a frame.
 1780  */
 1781 uint8_t *
 1782 ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
 1783 {
 1784         *frm++ = IEEE80211_ELEMID_SSID;
 1785         *frm++ = len;
 1786         memcpy(frm, ssid, len);
 1787         return frm + len;
 1788 }
 1789 
 1790 /*
 1791  * Add an erp element to a frame.
 1792  */
 1793 static uint8_t *
 1794 ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic)
 1795 {
 1796         uint8_t erp;
 1797 
 1798         *frm++ = IEEE80211_ELEMID_ERP;
 1799         *frm++ = 1;
 1800         erp = 0;
 1801         if (ic->ic_nonerpsta != 0)
 1802                 erp |= IEEE80211_ERP_NON_ERP_PRESENT;
 1803         if (ic->ic_flags & IEEE80211_F_USEPROT)
 1804                 erp |= IEEE80211_ERP_USE_PROTECTION;
 1805         if (ic->ic_flags & IEEE80211_F_USEBARKER)
 1806                 erp |= IEEE80211_ERP_LONG_PREAMBLE;
 1807         *frm++ = erp;
 1808         return frm;
 1809 }
 1810 
 1811 /*
 1812  * Add a CFParams element to a frame.
 1813  */
 1814 static uint8_t *
 1815 ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
 1816 {
 1817 #define ADDSHORT(frm, v) do {   \
 1818         le16enc(frm, v);        \
 1819         frm += 2;               \
 1820 } while (0)
 1821         *frm++ = IEEE80211_ELEMID_CFPARMS;
 1822         *frm++ = 6;
 1823         *frm++ = 0;             /* CFP count */
 1824         *frm++ = 2;             /* CFP period */
 1825         ADDSHORT(frm, 0);       /* CFP MaxDuration (TU) */
 1826         ADDSHORT(frm, 0);       /* CFP CurRemaining (TU) */
 1827         return frm;
 1828 #undef ADDSHORT
 1829 }
 1830 
 1831 static __inline uint8_t *
 1832 add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
 1833 {
 1834         memcpy(frm, ie->ie_data, ie->ie_len);
 1835         return frm + ie->ie_len;
 1836 }
 1837 
 1838 static __inline uint8_t *
 1839 add_ie(uint8_t *frm, const uint8_t *ie)
 1840 {
 1841         memcpy(frm, ie, 2 + ie[1]);
 1842         return frm + 2 + ie[1];
 1843 }
 1844 
 1845 #define WME_OUI_BYTES           0x00, 0x50, 0xf2
 1846 /*
 1847  * Add a WME information element to a frame.
 1848  */
 1849 uint8_t *
 1850 ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme)
 1851 {
 1852         static const struct ieee80211_wme_info info = {
 1853                 .wme_id         = IEEE80211_ELEMID_VENDOR,
 1854                 .wme_len        = sizeof(struct ieee80211_wme_info) - 2,
 1855                 .wme_oui        = { WME_OUI_BYTES },
 1856                 .wme_type       = WME_OUI_TYPE,
 1857                 .wme_subtype    = WME_INFO_OUI_SUBTYPE,
 1858                 .wme_version    = WME_VERSION,
 1859                 .wme_info       = 0,
 1860         };
 1861         memcpy(frm, &info, sizeof(info));
 1862         return frm + sizeof(info); 
 1863 }
 1864 
 1865 /*
 1866  * Add a WME parameters element to a frame.
 1867  */
 1868 static uint8_t *
 1869 ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme)
 1870 {
 1871 #define SM(_v, _f)      (((_v) << _f##_S) & _f)
 1872 #define ADDSHORT(frm, v) do {   \
 1873         le16enc(frm, v);        \
 1874         frm += 2;               \
 1875 } while (0)
 1876         /* NB: this works 'cuz a param has an info at the front */
 1877         static const struct ieee80211_wme_info param = {
 1878                 .wme_id         = IEEE80211_ELEMID_VENDOR,
 1879                 .wme_len        = sizeof(struct ieee80211_wme_param) - 2,
 1880                 .wme_oui        = { WME_OUI_BYTES },
 1881                 .wme_type       = WME_OUI_TYPE,
 1882                 .wme_subtype    = WME_PARAM_OUI_SUBTYPE,
 1883                 .wme_version    = WME_VERSION,
 1884         };
 1885         int i;
 1886 
 1887         memcpy(frm, &param, sizeof(param));
 1888         frm += __offsetof(struct ieee80211_wme_info, wme_info);
 1889         *frm++ = wme->wme_bssChanParams.cap_info;       /* AC info */
 1890         *frm++ = 0;                                     /* reserved field */
 1891         for (i = 0; i < WME_NUM_AC; i++) {
 1892                 const struct wmeParams *ac =
 1893                        &wme->wme_bssChanParams.cap_wmeParams[i];
 1894                 *frm++ = SM(i, WME_PARAM_ACI)
 1895                        | SM(ac->wmep_acm, WME_PARAM_ACM)
 1896                        | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
 1897                        ;
 1898                 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
 1899                        | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
 1900                        ;
 1901                 ADDSHORT(frm, ac->wmep_txopLimit);
 1902         }
 1903         return frm;
 1904 #undef SM
 1905 #undef ADDSHORT
 1906 }
 1907 #undef WME_OUI_BYTES
 1908 
 1909 /*
 1910  * Add an 11h Power Constraint element to a frame.
 1911  */
 1912 static uint8_t *
 1913 ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
 1914 {
 1915         const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
 1916         /* XXX per-vap tx power limit? */
 1917         int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
 1918 
 1919         frm[0] = IEEE80211_ELEMID_PWRCNSTR;
 1920         frm[1] = 1;
 1921         frm[2] = c->ic_maxregpower > limit ?  c->ic_maxregpower - limit : 0;
 1922         return frm + 3;
 1923 }
 1924 
 1925 /*
 1926  * Add an 11h Power Capability element to a frame.
 1927  */
 1928 static uint8_t *
 1929 ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
 1930 {
 1931         frm[0] = IEEE80211_ELEMID_PWRCAP;
 1932         frm[1] = 2;
 1933         frm[2] = c->ic_minpower;
 1934         frm[3] = c->ic_maxpower;
 1935         return frm + 4;
 1936 }
 1937 
 1938 /*
 1939  * Add an 11h Supported Channels element to a frame.
 1940  */
 1941 static uint8_t *
 1942 ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
 1943 {
 1944         static const int ielen = 26;
 1945 
 1946         frm[0] = IEEE80211_ELEMID_SUPPCHAN;
 1947         frm[1] = ielen;
 1948         /* XXX not correct */
 1949         memcpy(frm+2, ic->ic_chan_avail, ielen);
 1950         return frm + 2 + ielen;
 1951 }
 1952 
 1953 /*
 1954  * Add an 11h Quiet time element to a frame.
 1955  */
 1956 static uint8_t *
 1957 ieee80211_add_quiet(uint8_t *frm, struct ieee80211vap *vap)
 1958 {
 1959         struct ieee80211_quiet_ie *quiet = (struct ieee80211_quiet_ie *) frm;
 1960 
 1961         quiet->quiet_ie = IEEE80211_ELEMID_QUIET;
 1962         quiet->len = 6;
 1963         if (vap->iv_quiet_count_value == 1)
 1964                 vap->iv_quiet_count_value = vap->iv_quiet_count;
 1965         else if (vap->iv_quiet_count_value > 1)
 1966                 vap->iv_quiet_count_value--;
 1967 
 1968         if (vap->iv_quiet_count_value == 0) {
 1969                 /* value 0 is reserved as per 802.11h standerd */
 1970                 vap->iv_quiet_count_value = 1;
 1971         }
 1972 
 1973         quiet->tbttcount = vap->iv_quiet_count_value;
 1974         quiet->period = vap->iv_quiet_period;
 1975         quiet->duration = htole16(vap->iv_quiet_duration);
 1976         quiet->offset = htole16(vap->iv_quiet_offset);
 1977         return frm + sizeof(*quiet);
 1978 }
 1979 
 1980 /*
 1981  * Add an 11h Channel Switch Announcement element to a frame.
 1982  * Note that we use the per-vap CSA count to adjust the global
 1983  * counter so we can use this routine to form probe response
 1984  * frames and get the current count.
 1985  */
 1986 static uint8_t *
 1987 ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
 1988 {
 1989         struct ieee80211com *ic = vap->iv_ic;
 1990         struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
 1991 
 1992         csa->csa_ie = IEEE80211_ELEMID_CSA;
 1993         csa->csa_len = 3;
 1994         csa->csa_mode = 1;              /* XXX force quiet on channel */
 1995         csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
 1996         csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
 1997         return frm + sizeof(*csa);
 1998 }
 1999 
 2000 /*
 2001  * Add an 11h country information element to a frame.
 2002  */
 2003 static uint8_t *
 2004 ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
 2005 {
 2006 
 2007         if (ic->ic_countryie == NULL ||
 2008             ic->ic_countryie_chan != ic->ic_bsschan) {
 2009                 /*
 2010                  * Handle lazy construction of ie.  This is done on
 2011                  * first use and after a channel change that requires
 2012                  * re-calculation.
 2013                  */
 2014                 if (ic->ic_countryie != NULL)
 2015                         IEEE80211_FREE(ic->ic_countryie, M_80211_NODE_IE);
 2016                 ic->ic_countryie = ieee80211_alloc_countryie(ic);
 2017                 if (ic->ic_countryie == NULL)
 2018                         return frm;
 2019                 ic->ic_countryie_chan = ic->ic_bsschan;
 2020         }
 2021         return add_appie(frm, ic->ic_countryie);
 2022 }
 2023 
 2024 uint8_t *
 2025 ieee80211_add_wpa(uint8_t *frm, const struct ieee80211vap *vap)
 2026 {
 2027         if (vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL)
 2028                 return (add_ie(frm, vap->iv_wpa_ie));
 2029         else {
 2030                 /* XXX else complain? */
 2031                 return (frm);
 2032         }
 2033 }
 2034 
 2035 uint8_t *
 2036 ieee80211_add_rsn(uint8_t *frm, const struct ieee80211vap *vap)
 2037 {
 2038         if (vap->iv_flags & IEEE80211_F_WPA2 && vap->iv_rsn_ie != NULL)
 2039                 return (add_ie(frm, vap->iv_rsn_ie));
 2040         else {
 2041                 /* XXX else complain? */
 2042                 return (frm);
 2043         }
 2044 }
 2045 
 2046 uint8_t *
 2047 ieee80211_add_qos(uint8_t *frm, const struct ieee80211_node *ni)
 2048 {
 2049         if (ni->ni_flags & IEEE80211_NODE_QOS) {
 2050                 *frm++ = IEEE80211_ELEMID_QOS;
 2051                 *frm++ = 1;
 2052                 *frm++ = 0;
 2053         }
 2054 
 2055         return (frm);
 2056 }
 2057 
 2058 /*
 2059  * Send a probe request frame with the specified ssid
 2060  * and any optional information element data.
 2061  */
 2062 int
 2063 ieee80211_send_probereq(struct ieee80211_node *ni,
 2064         const uint8_t sa[IEEE80211_ADDR_LEN],
 2065         const uint8_t da[IEEE80211_ADDR_LEN],
 2066         const uint8_t bssid[IEEE80211_ADDR_LEN],
 2067         const uint8_t *ssid, size_t ssidlen)
 2068 {
 2069         struct ieee80211vap *vap = ni->ni_vap;
 2070         struct ieee80211com *ic = ni->ni_ic;
 2071         const struct ieee80211_txparam *tp;
 2072         struct ieee80211_bpf_params params;
 2073         const struct ieee80211_rateset *rs;
 2074         struct mbuf *m;
 2075         uint8_t *frm;
 2076         int ret;
 2077 
 2078         if (vap->iv_state == IEEE80211_S_CAC) {
 2079                 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
 2080                     "block %s frame in CAC state", "probe request");
 2081                 vap->iv_stats.is_tx_badstate++;
 2082                 return EIO;             /* XXX */
 2083         }
 2084 
 2085         /*
 2086          * Hold a reference on the node so it doesn't go away until after
 2087          * the xmit is complete all the way in the driver.  On error we
 2088          * will remove our reference.
 2089          */
 2090         IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
 2091                 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
 2092                 __func__, __LINE__,
 2093                 ni, ether_sprintf(ni->ni_macaddr),
 2094                 ieee80211_node_refcnt(ni)+1);
 2095         ieee80211_ref_node(ni);
 2096 
 2097         /*
 2098          * prreq frame format
 2099          *      [tlv] ssid
 2100          *      [tlv] supported rates
 2101          *      [tlv] RSN (optional)
 2102          *      [tlv] extended supported rates
 2103          *      [tlv] WPA (optional)
 2104          *      [tlv] user-specified ie's
 2105          */
 2106         m = ieee80211_getmgtframe(&frm,
 2107                  ic->ic_headroom + sizeof(struct ieee80211_frame),
 2108                  2 + IEEE80211_NWID_LEN
 2109                + 2 + IEEE80211_RATE_SIZE
 2110                + sizeof(struct ieee80211_ie_wpa)
 2111                + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
 2112                + sizeof(struct ieee80211_ie_wpa)
 2113                + (vap->iv_appie_probereq != NULL ?
 2114                    vap->iv_appie_probereq->ie_len : 0)
 2115         );
 2116         if (m == NULL) {
 2117                 vap->iv_stats.is_tx_nobuf++;
 2118                 ieee80211_free_node(ni);
 2119                 return ENOMEM;
 2120         }
 2121 
 2122         frm = ieee80211_add_ssid(frm, ssid, ssidlen);
 2123         rs = ieee80211_get_suprates(ic, ic->ic_curchan);
 2124         frm = ieee80211_add_rates(frm, rs);
 2125         frm = ieee80211_add_rsn(frm, vap);
 2126         frm = ieee80211_add_xrates(frm, rs);
 2127         frm = ieee80211_add_wpa(frm, vap);
 2128         if (vap->iv_appie_probereq != NULL)
 2129                 frm = add_appie(frm, vap->iv_appie_probereq);
 2130         m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
 2131 
 2132         KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
 2133             ("leading space %zd", M_LEADINGSPACE(m)));
 2134         M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
 2135         if (m == NULL) {
 2136                 /* NB: cannot happen */
 2137                 ieee80211_free_node(ni);
 2138                 return ENOMEM;
 2139         }
 2140 
 2141         IEEE80211_TX_LOCK(ic);
 2142         ieee80211_send_setup(ni, m,
 2143              IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
 2144              IEEE80211_NONQOS_TID, sa, da, bssid);
 2145         /* XXX power management? */
 2146         m->m_flags |= M_ENCAP;          /* mark encapsulated */
 2147 
 2148         M_WME_SETAC(m, WME_AC_BE);
 2149 
 2150         IEEE80211_NODE_STAT(ni, tx_probereq);
 2151         IEEE80211_NODE_STAT(ni, tx_mgmt);
 2152 
 2153         IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
 2154             "send probe req on channel %u bssid %s ssid \"%.*s\"\n",
 2155             ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(bssid),
 2156             ssidlen, ssid);
 2157 
 2158         memset(&params, 0, sizeof(params));
 2159         params.ibp_pri = M_WME_GETAC(m);
 2160         tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
 2161         params.ibp_rate0 = tp->mgmtrate;
 2162         if (IEEE80211_IS_MULTICAST(da)) {
 2163                 params.ibp_flags |= IEEE80211_BPF_NOACK;
 2164                 params.ibp_try0 = 1;
 2165         } else
 2166                 params.ibp_try0 = tp->maxretry;
 2167         params.ibp_power = ni->ni_txpower;
 2168         ret = ieee80211_raw_output(vap, ni, m, &params);
 2169         IEEE80211_TX_UNLOCK(ic);
 2170         return (ret);
 2171 }
 2172 
 2173 /*
 2174  * Calculate capability information for mgt frames.
 2175  */
 2176 uint16_t
 2177 ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
 2178 {
 2179         struct ieee80211com *ic = vap->iv_ic;
 2180         uint16_t capinfo;
 2181 
 2182         KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
 2183 
 2184         if (vap->iv_opmode == IEEE80211_M_HOSTAP)
 2185                 capinfo = IEEE80211_CAPINFO_ESS;
 2186         else if (vap->iv_opmode == IEEE80211_M_IBSS)
 2187                 capinfo = IEEE80211_CAPINFO_IBSS;
 2188         else
 2189                 capinfo = 0;
 2190         if (vap->iv_flags & IEEE80211_F_PRIVACY)
 2191                 capinfo |= IEEE80211_CAPINFO_PRIVACY;
 2192         if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
 2193             IEEE80211_IS_CHAN_2GHZ(chan))
 2194                 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
 2195         if (ic->ic_flags & IEEE80211_F_SHSLOT)
 2196                 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
 2197         if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
 2198                 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
 2199         return capinfo;
 2200 }
 2201 
 2202 /*
 2203  * Send a management frame.  The node is for the destination (or ic_bss
 2204  * when in station mode).  Nodes other than ic_bss have their reference
 2205  * count bumped to reflect our use for an indeterminant time.
 2206  */
 2207 int
 2208 ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
 2209 {
 2210 #define HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
 2211 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
 2212         struct ieee80211vap *vap = ni->ni_vap;
 2213         struct ieee80211com *ic = ni->ni_ic;
 2214         struct ieee80211_node *bss = vap->iv_bss;
 2215         struct ieee80211_bpf_params params;
 2216         struct mbuf *m;
 2217         uint8_t *frm;
 2218         uint16_t capinfo;
 2219         int has_challenge, is_shared_key, ret, status;
 2220 
 2221         KASSERT(ni != NULL, ("null node"));
 2222 
 2223         /*
 2224          * Hold a reference on the node so it doesn't go away until after
 2225          * the xmit is complete all the way in the driver.  On error we
 2226          * will remove our reference.
 2227          */
 2228         IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
 2229                 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
 2230                 __func__, __LINE__,
 2231                 ni, ether_sprintf(ni->ni_macaddr),
 2232                 ieee80211_node_refcnt(ni)+1);
 2233         ieee80211_ref_node(ni);
 2234 
 2235         memset(&params, 0, sizeof(params));
 2236         switch (type) {
 2237 
 2238         case IEEE80211_FC0_SUBTYPE_AUTH:
 2239                 status = arg >> 16;
 2240                 arg &= 0xffff;
 2241                 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
 2242                     arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
 2243                     ni->ni_challenge != NULL);
 2244 
 2245                 /*
 2246                  * Deduce whether we're doing open authentication or
 2247                  * shared key authentication.  We do the latter if
 2248                  * we're in the middle of a shared key authentication
 2249                  * handshake or if we're initiating an authentication
 2250                  * request and configured to use shared key.
 2251                  */
 2252                 is_shared_key = has_challenge ||
 2253                      arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
 2254                      (arg == IEEE80211_AUTH_SHARED_REQUEST &&
 2255                       bss->ni_authmode == IEEE80211_AUTH_SHARED);
 2256 
 2257                 m = ieee80211_getmgtframe(&frm,
 2258                           ic->ic_headroom + sizeof(struct ieee80211_frame),
 2259                           3 * sizeof(uint16_t)
 2260                         + (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
 2261                                 sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)
 2262                 );
 2263                 if (m == NULL)
 2264                         senderr(ENOMEM, is_tx_nobuf);
 2265 
 2266                 ((uint16_t *)frm)[0] =
 2267                     (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
 2268                                     : htole16(IEEE80211_AUTH_ALG_OPEN);
 2269                 ((uint16_t *)frm)[1] = htole16(arg);    /* sequence number */
 2270                 ((uint16_t *)frm)[2] = htole16(status);/* status */
 2271 
 2272                 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
 2273                         ((uint16_t *)frm)[3] =
 2274                             htole16((IEEE80211_CHALLENGE_LEN << 8) |
 2275                             IEEE80211_ELEMID_CHALLENGE);
 2276                         memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
 2277                             IEEE80211_CHALLENGE_LEN);
 2278                         m->m_pkthdr.len = m->m_len =
 2279                                 4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
 2280                         if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
 2281                                 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
 2282                                     "request encrypt frame (%s)", __func__);
 2283                                 /* mark frame for encryption */
 2284                                 params.ibp_flags |= IEEE80211_BPF_CRYPTO;
 2285                         }
 2286                 } else
 2287                         m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
 2288 
 2289                 /* XXX not right for shared key */
 2290                 if (status == IEEE80211_STATUS_SUCCESS)
 2291                         IEEE80211_NODE_STAT(ni, tx_auth);
 2292                 else
 2293                         IEEE80211_NODE_STAT(ni, tx_auth_fail);
 2294 
 2295                 if (vap->iv_opmode == IEEE80211_M_STA)
 2296                         ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
 2297                                 (void *) vap->iv_state);
 2298                 break;
 2299 
 2300         case IEEE80211_FC0_SUBTYPE_DEAUTH:
 2301                 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
 2302                     "send station deauthenticate (reason: %d (%s))", arg,
 2303                     ieee80211_reason_to_string(arg));
 2304                 m = ieee80211_getmgtframe(&frm,
 2305                         ic->ic_headroom + sizeof(struct ieee80211_frame),
 2306                         sizeof(uint16_t));
 2307                 if (m == NULL)
 2308                         senderr(ENOMEM, is_tx_nobuf);
 2309                 *(uint16_t *)frm = htole16(arg);        /* reason */
 2310                 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
 2311 
 2312                 IEEE80211_NODE_STAT(ni, tx_deauth);
 2313                 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
 2314 
 2315                 ieee80211_node_unauthorize(ni);         /* port closed */
 2316                 break;
 2317 
 2318         case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
 2319         case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
 2320                 /*
 2321                  * asreq frame format
 2322                  *      [2] capability information
 2323                  *      [2] listen interval
 2324                  *      [6*] current AP address (reassoc only)
 2325                  *      [tlv] ssid
 2326                  *      [tlv] supported rates
 2327                  *      [tlv] extended supported rates
 2328                  *      [4] power capability (optional)
 2329                  *      [28] supported channels (optional)
 2330                  *      [tlv] HT capabilities
 2331                  *      [tlv] WME (optional)
 2332                  *      [tlv] Vendor OUI HT capabilities (optional)
 2333                  *      [tlv] Atheros capabilities (if negotiated)
 2334                  *      [tlv] AppIE's (optional)
 2335                  */
 2336                 m = ieee80211_getmgtframe(&frm,
 2337                          ic->ic_headroom + sizeof(struct ieee80211_frame),
 2338                          sizeof(uint16_t)
 2339                        + sizeof(uint16_t)
 2340                        + IEEE80211_ADDR_LEN
 2341                        + 2 + IEEE80211_NWID_LEN
 2342                        + 2 + IEEE80211_RATE_SIZE
 2343                        + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
 2344                        + 4
 2345                        + 2 + 26
 2346                        + sizeof(struct ieee80211_wme_info)
 2347                        + sizeof(struct ieee80211_ie_htcap)
 2348                        + 4 + sizeof(struct ieee80211_ie_htcap)
 2349 #ifdef IEEE80211_SUPPORT_SUPERG
 2350                        + sizeof(struct ieee80211_ath_ie)
 2351 #endif
 2352                        + (vap->iv_appie_wpa != NULL ?
 2353                                 vap->iv_appie_wpa->ie_len : 0)
 2354                        + (vap->iv_appie_assocreq != NULL ?
 2355                                 vap->iv_appie_assocreq->ie_len : 0)
 2356                 );
 2357                 if (m == NULL)
 2358                         senderr(ENOMEM, is_tx_nobuf);
 2359 
 2360                 KASSERT(vap->iv_opmode == IEEE80211_M_STA,
 2361                     ("wrong mode %u", vap->iv_opmode));
 2362                 capinfo = IEEE80211_CAPINFO_ESS;
 2363                 if (vap->iv_flags & IEEE80211_F_PRIVACY)
 2364                         capinfo |= IEEE80211_CAPINFO_PRIVACY;
 2365                 /*
 2366                  * NB: Some 11a AP's reject the request when
 2367                  *     short premable is set.
 2368                  */
 2369                 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
 2370                     IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
 2371                         capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
 2372                 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
 2373                     (ic->ic_caps & IEEE80211_C_SHSLOT))
 2374                         capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
 2375                 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
 2376                     (vap->iv_flags & IEEE80211_F_DOTH))
 2377                         capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
 2378                 *(uint16_t *)frm = htole16(capinfo);
 2379                 frm += 2;
 2380 
 2381                 KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
 2382                 *(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
 2383                                                     bss->ni_intval));
 2384                 frm += 2;
 2385 
 2386                 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
 2387                         IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
 2388                         frm += IEEE80211_ADDR_LEN;
 2389                 }
 2390 
 2391                 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
 2392                 frm = ieee80211_add_rates(frm, &ni->ni_rates);
 2393                 frm = ieee80211_add_rsn(frm, vap);
 2394                 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
 2395                 if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
 2396                         frm = ieee80211_add_powercapability(frm,
 2397                             ic->ic_curchan);
 2398                         frm = ieee80211_add_supportedchannels(frm, ic);
 2399                 }
 2400 
 2401                 /*
 2402                  * Check the channel - we may be using an 11n NIC with an
 2403                  * 11n capable station, but we're configured to be an 11b
 2404                  * channel.
 2405                  */
 2406                 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
 2407                     IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
 2408                     ni->ni_ies.htcap_ie != NULL &&
 2409                     ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP) {
 2410                         frm = ieee80211_add_htcap(frm, ni);
 2411                 }
 2412                 frm = ieee80211_add_wpa(frm, vap);
 2413                 if ((ic->ic_flags & IEEE80211_F_WME) &&
 2414                     ni->ni_ies.wme_ie != NULL)
 2415                         frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
 2416 
 2417                 /*
 2418                  * Same deal - only send HT info if we're on an 11n
 2419                  * capable channel.
 2420                  */
 2421                 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
 2422                     IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
 2423                     ni->ni_ies.htcap_ie != NULL &&
 2424                     ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR) {
 2425                         frm = ieee80211_add_htcap_vendor(frm, ni);
 2426                 }
 2427 #ifdef IEEE80211_SUPPORT_SUPERG
 2428                 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
 2429                         frm = ieee80211_add_ath(frm, 
 2430                                 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
 2431                                 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
 2432                                  ni->ni_authmode != IEEE80211_AUTH_8021X) ?
 2433                                 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
 2434                 }
 2435 #endif /* IEEE80211_SUPPORT_SUPERG */
 2436                 if (vap->iv_appie_assocreq != NULL)
 2437                         frm = add_appie(frm, vap->iv_appie_assocreq);
 2438                 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
 2439 
 2440                 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
 2441                         (void *) vap->iv_state);
 2442                 break;
 2443 
 2444         case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
 2445         case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
 2446                 /*
 2447                  * asresp frame format
 2448                  *      [2] capability information
 2449                  *      [2] status
 2450                  *      [2] association ID
 2451                  *      [tlv] supported rates
 2452                  *      [tlv] extended supported rates
 2453                  *      [tlv] HT capabilities (standard, if STA enabled)
 2454                  *      [tlv] HT information (standard, if STA enabled)
 2455                  *      [tlv] WME (if configured and STA enabled)
 2456                  *      [tlv] HT capabilities (vendor OUI, if STA enabled)
 2457                  *      [tlv] HT information (vendor OUI, if STA enabled)
 2458                  *      [tlv] Atheros capabilities (if STA enabled)
 2459                  *      [tlv] AppIE's (optional)
 2460                  */
 2461                 m = ieee80211_getmgtframe(&frm,
 2462                          ic->ic_headroom + sizeof(struct ieee80211_frame),
 2463                          sizeof(uint16_t)
 2464                        + sizeof(uint16_t)
 2465                        + sizeof(uint16_t)
 2466                        + 2 + IEEE80211_RATE_SIZE
 2467                        + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
 2468                        + sizeof(struct ieee80211_ie_htcap) + 4
 2469                        + sizeof(struct ieee80211_ie_htinfo) + 4
 2470                        + sizeof(struct ieee80211_wme_param)
 2471 #ifdef IEEE80211_SUPPORT_SUPERG
 2472                        + sizeof(struct ieee80211_ath_ie)
 2473 #endif
 2474                        + (vap->iv_appie_assocresp != NULL ?
 2475                                 vap->iv_appie_assocresp->ie_len : 0)
 2476                 );
 2477                 if (m == NULL)
 2478                         senderr(ENOMEM, is_tx_nobuf);
 2479 
 2480                 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
 2481                 *(uint16_t *)frm = htole16(capinfo);
 2482                 frm += 2;
 2483 
 2484                 *(uint16_t *)frm = htole16(arg);        /* status */
 2485                 frm += 2;
 2486 
 2487                 if (arg == IEEE80211_STATUS_SUCCESS) {
 2488                         *(uint16_t *)frm = htole16(ni->ni_associd);
 2489                         IEEE80211_NODE_STAT(ni, tx_assoc);
 2490                 } else
 2491                         IEEE80211_NODE_STAT(ni, tx_assoc_fail);
 2492                 frm += 2;
 2493 
 2494                 frm = ieee80211_add_rates(frm, &ni->ni_rates);
 2495                 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
 2496                 /* NB: respond according to what we received */
 2497                 if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
 2498                         frm = ieee80211_add_htcap(frm, ni);
 2499                         frm = ieee80211_add_htinfo(frm, ni);
 2500                 }
 2501                 if ((vap->iv_flags & IEEE80211_F_WME) &&
 2502                     ni->ni_ies.wme_ie != NULL)
 2503                         frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
 2504                 if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
 2505                         frm = ieee80211_add_htcap_vendor(frm, ni);
 2506                         frm = ieee80211_add_htinfo_vendor(frm, ni);
 2507                 }
 2508 #ifdef IEEE80211_SUPPORT_SUPERG
 2509                 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
 2510                         frm = ieee80211_add_ath(frm, 
 2511                                 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
 2512                                 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
 2513                                  ni->ni_authmode != IEEE80211_AUTH_8021X) ?
 2514                                 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
 2515 #endif /* IEEE80211_SUPPORT_SUPERG */
 2516                 if (vap->iv_appie_assocresp != NULL)
 2517                         frm = add_appie(frm, vap->iv_appie_assocresp);
 2518                 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
 2519                 break;
 2520 
 2521         case IEEE80211_FC0_SUBTYPE_DISASSOC:
 2522                 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
 2523                     "send station disassociate (reason: %d (%s))", arg,
 2524                     ieee80211_reason_to_string(arg));
 2525                 m = ieee80211_getmgtframe(&frm,
 2526                         ic->ic_headroom + sizeof(struct ieee80211_frame),
 2527                         sizeof(uint16_t));
 2528                 if (m == NULL)
 2529                         senderr(ENOMEM, is_tx_nobuf);
 2530                 *(uint16_t *)frm = htole16(arg);        /* reason */
 2531                 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
 2532 
 2533                 IEEE80211_NODE_STAT(ni, tx_disassoc);
 2534                 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
 2535                 break;
 2536 
 2537         default:
 2538                 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
 2539                     "invalid mgmt frame type %u", type);
 2540                 senderr(EINVAL, is_tx_unknownmgt);
 2541                 /* NOTREACHED */
 2542         }
 2543 
 2544         /* NB: force non-ProbeResp frames to the highest queue */
 2545         params.ibp_pri = WME_AC_VO;
 2546         params.ibp_rate0 = bss->ni_txparms->mgmtrate;
 2547         /* NB: we know all frames are unicast */
 2548         params.ibp_try0 = bss->ni_txparms->maxretry;
 2549         params.ibp_power = bss->ni_txpower;
 2550         return ieee80211_mgmt_output(ni, m, type, &params);
 2551 bad:
 2552         ieee80211_free_node(ni);
 2553         return ret;
 2554 #undef senderr
 2555 #undef HTFLAGS
 2556 }
 2557 
 2558 /*
 2559  * Return an mbuf with a probe response frame in it.
 2560  * Space is left to prepend and 802.11 header at the
 2561  * front but it's left to the caller to fill in.
 2562  */
 2563 struct mbuf *
 2564 ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
 2565 {
 2566         struct ieee80211vap *vap = bss->ni_vap;
 2567         struct ieee80211com *ic = bss->ni_ic;
 2568         const struct ieee80211_rateset *rs;
 2569         struct mbuf *m;
 2570         uint16_t capinfo;
 2571         uint8_t *frm;
 2572 
 2573         /*
 2574          * probe response frame format
 2575          *      [8] time stamp
 2576          *      [2] beacon interval
 2577          *      [2] cabability information
 2578          *      [tlv] ssid
 2579          *      [tlv] supported rates
 2580          *      [tlv] parameter set (FH/DS)
 2581          *      [tlv] parameter set (IBSS)
 2582          *      [tlv] country (optional)
 2583          *      [3] power control (optional)
 2584          *      [5] channel switch announcement (CSA) (optional)
 2585          *      [tlv] extended rate phy (ERP)
 2586          *      [tlv] extended supported rates
 2587          *      [tlv] RSN (optional)
 2588          *      [tlv] HT capabilities
 2589          *      [tlv] HT information
 2590          *      [tlv] WPA (optional)
 2591          *      [tlv] WME (optional)
 2592          *      [tlv] Vendor OUI HT capabilities (optional)
 2593          *      [tlv] Vendor OUI HT information (optional)
 2594          *      [tlv] Atheros capabilities
 2595          *      [tlv] AppIE's (optional)
 2596          *      [tlv] Mesh ID (MBSS)
 2597          *      [tlv] Mesh Conf (MBSS)
 2598          */
 2599         m = ieee80211_getmgtframe(&frm,
 2600                  ic->ic_headroom + sizeof(struct ieee80211_frame),
 2601                  8
 2602                + sizeof(uint16_t)
 2603                + sizeof(uint16_t)
 2604                + 2 + IEEE80211_NWID_LEN
 2605                + 2 + IEEE80211_RATE_SIZE
 2606                + 7      /* max(7,3) */
 2607                + IEEE80211_COUNTRY_MAX_SIZE
 2608                + 3
 2609                + sizeof(struct ieee80211_csa_ie)
 2610                + sizeof(struct ieee80211_quiet_ie)
 2611                + 3
 2612                + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
 2613                + sizeof(struct ieee80211_ie_wpa)
 2614                + sizeof(struct ieee80211_ie_htcap)
 2615                + sizeof(struct ieee80211_ie_htinfo)
 2616                + sizeof(struct ieee80211_ie_wpa)
 2617                + sizeof(struct ieee80211_wme_param)
 2618                + 4 + sizeof(struct ieee80211_ie_htcap)
 2619                + 4 + sizeof(struct ieee80211_ie_htinfo)
 2620 #ifdef IEEE80211_SUPPORT_SUPERG
 2621                + sizeof(struct ieee80211_ath_ie)
 2622 #endif
 2623 #ifdef IEEE80211_SUPPORT_MESH
 2624                + 2 + IEEE80211_MESHID_LEN
 2625                + sizeof(struct ieee80211_meshconf_ie)
 2626 #endif
 2627                + (vap->iv_appie_proberesp != NULL ?
 2628                         vap->iv_appie_proberesp->ie_len : 0)
 2629         );
 2630         if (m == NULL) {
 2631                 vap->iv_stats.is_tx_nobuf++;
 2632                 return NULL;
 2633         }
 2634 
 2635         memset(frm, 0, 8);      /* timestamp should be filled later */
 2636         frm += 8;
 2637         *(uint16_t *)frm = htole16(bss->ni_intval);
 2638         frm += 2;
 2639         capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
 2640         *(uint16_t *)frm = htole16(capinfo);
 2641         frm += 2;
 2642 
 2643         frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
 2644         rs = ieee80211_get_suprates(ic, bss->ni_chan);
 2645         frm = ieee80211_add_rates(frm, rs);
 2646 
 2647         if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
 2648                 *frm++ = IEEE80211_ELEMID_FHPARMS;
 2649                 *frm++ = 5;
 2650                 *frm++ = bss->ni_fhdwell & 0x00ff;
 2651                 *frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
 2652                 *frm++ = IEEE80211_FH_CHANSET(
 2653                     ieee80211_chan2ieee(ic, bss->ni_chan));
 2654                 *frm++ = IEEE80211_FH_CHANPAT(
 2655                     ieee80211_chan2ieee(ic, bss->ni_chan));
 2656                 *frm++ = bss->ni_fhindex;
 2657         } else {
 2658                 *frm++ = IEEE80211_ELEMID_DSPARMS;
 2659                 *frm++ = 1;
 2660                 *frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
 2661         }
 2662 
 2663         if (vap->iv_opmode == IEEE80211_M_IBSS) {
 2664                 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
 2665                 *frm++ = 2;
 2666                 *frm++ = 0; *frm++ = 0;         /* TODO: ATIM window */
 2667         }
 2668         if ((vap->iv_flags & IEEE80211_F_DOTH) ||
 2669             (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
 2670                 frm = ieee80211_add_countryie(frm, ic);
 2671         if (vap->iv_flags & IEEE80211_F_DOTH) {
 2672                 if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
 2673                         frm = ieee80211_add_powerconstraint(frm, vap);
 2674                 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
 2675                         frm = ieee80211_add_csa(frm, vap);
 2676         }
 2677         if (vap->iv_flags & IEEE80211_F_DOTH) {
 2678                 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
 2679                     (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
 2680                         if (vap->iv_quiet)
 2681                                 frm = ieee80211_add_quiet(frm, vap);
 2682                 }
 2683         }
 2684         if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
 2685                 frm = ieee80211_add_erp(frm, ic);
 2686         frm = ieee80211_add_xrates(frm, rs);
 2687         frm = ieee80211_add_rsn(frm, vap);
 2688         /*
 2689          * NB: legacy 11b clients do not get certain ie's.
 2690          *     The caller identifies such clients by passing
 2691          *     a token in legacy to us.  Could expand this to be
 2692          *     any legacy client for stuff like HT ie's.
 2693          */
 2694         if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
 2695             legacy != IEEE80211_SEND_LEGACY_11B) {
 2696                 frm = ieee80211_add_htcap(frm, bss);
 2697                 frm = ieee80211_add_htinfo(frm, bss);
 2698         }
 2699         frm = ieee80211_add_wpa(frm, vap);
 2700         if (vap->iv_flags & IEEE80211_F_WME)
 2701                 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
 2702         if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
 2703             (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
 2704             legacy != IEEE80211_SEND_LEGACY_11B) {
 2705                 frm = ieee80211_add_htcap_vendor(frm, bss);
 2706                 frm = ieee80211_add_htinfo_vendor(frm, bss);
 2707         }
 2708 #ifdef IEEE80211_SUPPORT_SUPERG
 2709         if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
 2710             legacy != IEEE80211_SEND_LEGACY_11B)
 2711                 frm = ieee80211_add_athcaps(frm, bss);
 2712 #endif
 2713         if (vap->iv_appie_proberesp != NULL)
 2714                 frm = add_appie(frm, vap->iv_appie_proberesp);
 2715 #ifdef IEEE80211_SUPPORT_MESH
 2716         if (vap->iv_opmode == IEEE80211_M_MBSS) {
 2717                 frm = ieee80211_add_meshid(frm, vap);
 2718                 frm = ieee80211_add_meshconf(frm, vap);
 2719         }
 2720 #endif
 2721         m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
 2722 
 2723         return m;
 2724 }
 2725 
 2726 /*
 2727  * Send a probe response frame to the specified mac address.
 2728  * This does not go through the normal mgt frame api so we
 2729  * can specify the destination address and re-use the bss node
 2730  * for the sta reference.
 2731  */
 2732 int
 2733 ieee80211_send_proberesp(struct ieee80211vap *vap,
 2734         const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
 2735 {
 2736         struct ieee80211_node *bss = vap->iv_bss;
 2737         struct ieee80211com *ic = vap->iv_ic;
 2738         struct mbuf *m;
 2739         int ret;
 2740 
 2741         if (vap->iv_state == IEEE80211_S_CAC) {
 2742                 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
 2743                     "block %s frame in CAC state", "probe response");
 2744                 vap->iv_stats.is_tx_badstate++;
 2745                 return EIO;             /* XXX */
 2746         }
 2747 
 2748         /*
 2749          * Hold a reference on the node so it doesn't go away until after
 2750          * the xmit is complete all the way in the driver.  On error we
 2751          * will remove our reference.
 2752          */
 2753         IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
 2754             "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
 2755             __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
 2756             ieee80211_node_refcnt(bss)+1);
 2757         ieee80211_ref_node(bss);
 2758 
 2759         m = ieee80211_alloc_proberesp(bss, legacy);
 2760         if (m == NULL) {
 2761                 ieee80211_free_node(bss);
 2762                 return ENOMEM;
 2763         }
 2764 
 2765         M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
 2766         KASSERT(m != NULL, ("no room for header"));
 2767 
 2768         IEEE80211_TX_LOCK(ic);
 2769         ieee80211_send_setup(bss, m,
 2770              IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
 2771              IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
 2772         /* XXX power management? */
 2773         m->m_flags |= M_ENCAP;          /* mark encapsulated */
 2774 
 2775         M_WME_SETAC(m, WME_AC_BE);
 2776 
 2777         IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
 2778             "send probe resp on channel %u to %s%s\n",
 2779             ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
 2780             legacy ? " <legacy>" : "");
 2781         IEEE80211_NODE_STAT(bss, tx_mgmt);
 2782 
 2783         ret = ieee80211_raw_output(vap, bss, m, NULL);
 2784         IEEE80211_TX_UNLOCK(ic);
 2785         return (ret);
 2786 }
 2787 
 2788 /*
 2789  * Allocate and build a RTS (Request To Send) control frame.
 2790  */
 2791 struct mbuf *
 2792 ieee80211_alloc_rts(struct ieee80211com *ic,
 2793         const uint8_t ra[IEEE80211_ADDR_LEN],
 2794         const uint8_t ta[IEEE80211_ADDR_LEN],
 2795         uint16_t dur)
 2796 {
 2797         struct ieee80211_frame_rts *rts;
 2798         struct mbuf *m;
 2799 
 2800         /* XXX honor ic_headroom */
 2801         m = m_gethdr(M_NOWAIT, MT_DATA);
 2802         if (m != NULL) {
 2803                 rts = mtod(m, struct ieee80211_frame_rts *);
 2804                 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
 2805                         IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
 2806                 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
 2807                 *(u_int16_t *)rts->i_dur = htole16(dur);
 2808                 IEEE80211_ADDR_COPY(rts->i_ra, ra);
 2809                 IEEE80211_ADDR_COPY(rts->i_ta, ta);
 2810 
 2811                 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
 2812         }
 2813         return m;
 2814 }
 2815 
 2816 /*
 2817  * Allocate and build a CTS (Clear To Send) control frame.
 2818  */
 2819 struct mbuf *
 2820 ieee80211_alloc_cts(struct ieee80211com *ic,
 2821         const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
 2822 {
 2823         struct ieee80211_frame_cts *cts;
 2824         struct mbuf *m;
 2825 
 2826         /* XXX honor ic_headroom */
 2827         m = m_gethdr(M_NOWAIT, MT_DATA);
 2828         if (m != NULL) {
 2829                 cts = mtod(m, struct ieee80211_frame_cts *);
 2830                 cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
 2831                         IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
 2832                 cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
 2833                 *(u_int16_t *)cts->i_dur = htole16(dur);
 2834                 IEEE80211_ADDR_COPY(cts->i_ra, ra);
 2835 
 2836                 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
 2837         }
 2838         return m;
 2839 }
 2840 
 2841 static void
 2842 ieee80211_tx_mgt_timeout(void *arg)
 2843 {
 2844         struct ieee80211vap *vap = arg;
 2845 
 2846         IEEE80211_LOCK(vap->iv_ic);
 2847         if (vap->iv_state != IEEE80211_S_INIT &&
 2848             (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
 2849                 /*
 2850                  * NB: it's safe to specify a timeout as the reason here;
 2851                  *     it'll only be used in the right state.
 2852                  */
 2853                 ieee80211_new_state_locked(vap, IEEE80211_S_SCAN,
 2854                         IEEE80211_SCAN_FAIL_TIMEOUT);
 2855         }
 2856         IEEE80211_UNLOCK(vap->iv_ic);
 2857 }
 2858 
 2859 /*
 2860  * This is the callback set on net80211-sourced transmitted
 2861  * authentication request frames.
 2862  *
 2863  * This does a couple of things:
 2864  *
 2865  * + If the frame transmitted was a success, it schedules a future
 2866  *   event which will transition the interface to scan.
 2867  *   If a state transition _then_ occurs before that event occurs,
 2868  *   said state transition will cancel this callout.
 2869  *
 2870  * + If the frame transmit was a failure, it immediately schedules
 2871  *   the transition back to scan.
 2872  */
 2873 static void
 2874 ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
 2875 {
 2876         struct ieee80211vap *vap = ni->ni_vap;
 2877         enum ieee80211_state ostate = (enum ieee80211_state)(uintptr_t)arg;
 2878 
 2879         /*
 2880          * Frame transmit completed; arrange timer callback.  If
 2881          * transmit was successfully we wait for response.  Otherwise
 2882          * we arrange an immediate callback instead of doing the
 2883          * callback directly since we don't know what state the driver
 2884          * is in (e.g. what locks it is holding).  This work should
 2885          * not be too time-critical and not happen too often so the
 2886          * added overhead is acceptable.
 2887          *
 2888          * XXX what happens if !acked but response shows up before callback?
 2889          */
 2890         if (vap->iv_state == ostate) {
 2891                 callout_reset(&vap->iv_mgtsend,
 2892                         status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
 2893                         ieee80211_tx_mgt_timeout, vap);
 2894         }
 2895 }
 2896 
 2897 static void
 2898 ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
 2899         struct ieee80211_node *ni)
 2900 {
 2901         struct ieee80211vap *vap = ni->ni_vap;
 2902         struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
 2903         struct ieee80211com *ic = ni->ni_ic;
 2904         struct ieee80211_rateset *rs = &ni->ni_rates;
 2905         uint16_t capinfo;
 2906 
 2907         /*
 2908          * beacon frame format
 2909          *      [8] time stamp
 2910          *      [2] beacon interval
 2911          *      [2] cabability information
 2912          *      [tlv] ssid
 2913          *      [tlv] supported rates
 2914          *      [3] parameter set (DS)
 2915          *      [8] CF parameter set (optional)
 2916          *      [tlv] parameter set (IBSS/TIM)
 2917          *      [tlv] country (optional)
 2918          *      [3] power control (optional)
 2919          *      [5] channel switch announcement (CSA) (optional)
 2920          *      [tlv] extended rate phy (ERP)
 2921          *      [tlv] extended supported rates
 2922          *      [tlv] RSN parameters
 2923          *      [tlv] HT capabilities
 2924          *      [tlv] HT information
 2925          * XXX Vendor-specific OIDs (e.g. Atheros)
 2926          *      [tlv] WPA parameters
 2927          *      [tlv] WME parameters
 2928          *      [tlv] Vendor OUI HT capabilities (optional)
 2929          *      [tlv] Vendor OUI HT information (optional)
 2930          *      [tlv] Atheros capabilities (optional)
 2931          *      [tlv] TDMA parameters (optional)
 2932          *      [tlv] Mesh ID (MBSS)
 2933          *      [tlv] Mesh Conf (MBSS)
 2934          *      [tlv] application data (optional)
 2935          */
 2936 
 2937         memset(bo, 0, sizeof(*bo));
 2938 
 2939         memset(frm, 0, 8);      /* XXX timestamp is set by hardware/driver */
 2940         frm += 8;
 2941         *(uint16_t *)frm = htole16(ni->ni_intval);
 2942         frm += 2;
 2943         capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
 2944         bo->bo_caps = (uint16_t *)frm;
 2945         *(uint16_t *)frm = htole16(capinfo);
 2946         frm += 2;
 2947         *frm++ = IEEE80211_ELEMID_SSID;
 2948         if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
 2949                 *frm++ = ni->ni_esslen;
 2950                 memcpy(frm, ni->ni_essid, ni->ni_esslen);
 2951                 frm += ni->ni_esslen;
 2952         } else
 2953                 *frm++ = 0;
 2954         frm = ieee80211_add_rates(frm, rs);
 2955         if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
 2956                 *frm++ = IEEE80211_ELEMID_DSPARMS;
 2957                 *frm++ = 1;
 2958                 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
 2959         }
 2960         if (ic->ic_flags & IEEE80211_F_PCF) {
 2961                 bo->bo_cfp = frm;
 2962                 frm = ieee80211_add_cfparms(frm, ic);
 2963         }
 2964         bo->bo_tim = frm;
 2965         if (vap->iv_opmode == IEEE80211_M_IBSS) {
 2966                 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
 2967                 *frm++ = 2;
 2968                 *frm++ = 0; *frm++ = 0;         /* TODO: ATIM window */
 2969                 bo->bo_tim_len = 0;
 2970         } else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
 2971             vap->iv_opmode == IEEE80211_M_MBSS) {
 2972                 /* TIM IE is the same for Mesh and Hostap */
 2973                 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
 2974 
 2975                 tie->tim_ie = IEEE80211_ELEMID_TIM;
 2976                 tie->tim_len = 4;       /* length */
 2977                 tie->tim_count = 0;     /* DTIM count */ 
 2978                 tie->tim_period = vap->iv_dtim_period;  /* DTIM period */
 2979                 tie->tim_bitctl = 0;    /* bitmap control */
 2980                 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */
 2981                 frm += sizeof(struct ieee80211_tim_ie);
 2982                 bo->bo_tim_len = 1;
 2983         }
 2984         bo->bo_tim_trailer = frm;
 2985         if ((vap->iv_flags & IEEE80211_F_DOTH) ||
 2986             (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
 2987                 frm = ieee80211_add_countryie(frm, ic);
 2988         if (vap->iv_flags & IEEE80211_F_DOTH) {
 2989                 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
 2990                         frm = ieee80211_add_powerconstraint(frm, vap);
 2991                 bo->bo_csa = frm;
 2992                 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
 2993                         frm = ieee80211_add_csa(frm, vap);      
 2994         } else
 2995                 bo->bo_csa = frm;
 2996 
 2997         if (vap->iv_flags & IEEE80211_F_DOTH) {
 2998                 bo->bo_quiet = frm;
 2999                 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
 3000                     (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
 3001                         if (vap->iv_quiet)
 3002                                 frm = ieee80211_add_quiet(frm,vap);
 3003                 }
 3004         } else
 3005                 bo->bo_quiet = frm;
 3006 
 3007         if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
 3008                 bo->bo_erp = frm;
 3009                 frm = ieee80211_add_erp(frm, ic);
 3010         }
 3011         frm = ieee80211_add_xrates(frm, rs);
 3012         frm = ieee80211_add_rsn(frm, vap);
 3013         if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
 3014                 frm = ieee80211_add_htcap(frm, ni);
 3015                 bo->bo_htinfo = frm;
 3016                 frm = ieee80211_add_htinfo(frm, ni);
 3017         }
 3018         frm = ieee80211_add_wpa(frm, vap);
 3019         if (vap->iv_flags & IEEE80211_F_WME) {
 3020                 bo->bo_wme = frm;
 3021                 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
 3022         }
 3023         if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
 3024             (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
 3025                 frm = ieee80211_add_htcap_vendor(frm, ni);
 3026                 frm = ieee80211_add_htinfo_vendor(frm, ni);
 3027         }
 3028 #ifdef IEEE80211_SUPPORT_SUPERG
 3029         if (vap->iv_flags & IEEE80211_F_ATHEROS) {
 3030                 bo->bo_ath = frm;
 3031                 frm = ieee80211_add_athcaps(frm, ni);
 3032         }
 3033 #endif
 3034 #ifdef IEEE80211_SUPPORT_TDMA
 3035         if (vap->iv_caps & IEEE80211_C_TDMA) {
 3036                 bo->bo_tdma = frm;
 3037                 frm = ieee80211_add_tdma(frm, vap);
 3038         }
 3039 #endif
 3040         if (vap->iv_appie_beacon != NULL) {
 3041                 bo->bo_appie = frm;
 3042                 bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
 3043                 frm = add_appie(frm, vap->iv_appie_beacon);
 3044         }
 3045 #ifdef IEEE80211_SUPPORT_MESH
 3046         if (vap->iv_opmode == IEEE80211_M_MBSS) {
 3047                 frm = ieee80211_add_meshid(frm, vap);
 3048                 bo->bo_meshconf = frm;
 3049                 frm = ieee80211_add_meshconf(frm, vap);
 3050         }
 3051 #endif
 3052         bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
 3053         bo->bo_csa_trailer_len = frm - bo->bo_csa;
 3054         m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
 3055 }
 3056 
 3057 /*
 3058  * Allocate a beacon frame and fillin the appropriate bits.
 3059  */
 3060 struct mbuf *
 3061 ieee80211_beacon_alloc(struct ieee80211_node *ni)
 3062 {
 3063         struct ieee80211vap *vap = ni->ni_vap;
 3064         struct ieee80211com *ic = ni->ni_ic;
 3065         struct ifnet *ifp = vap->iv_ifp;
 3066         struct ieee80211_frame *wh;
 3067         struct mbuf *m;
 3068         int pktlen;
 3069         uint8_t *frm;
 3070 
 3071         /*
 3072          * beacon frame format
 3073          *      [8] time stamp
 3074          *      [2] beacon interval
 3075          *      [2] cabability information
 3076          *      [tlv] ssid
 3077          *      [tlv] supported rates
 3078          *      [3] parameter set (DS)
 3079          *      [8] CF parameter set (optional)
 3080          *      [tlv] parameter set (IBSS/TIM)
 3081          *      [tlv] country (optional)
 3082          *      [3] power control (optional)
 3083          *      [5] channel switch announcement (CSA) (optional)
 3084          *      [tlv] extended rate phy (ERP)
 3085          *      [tlv] extended supported rates
 3086          *      [tlv] RSN parameters
 3087          *      [tlv] HT capabilities
 3088          *      [tlv] HT information
 3089          *      [tlv] Vendor OUI HT capabilities (optional)
 3090          *      [tlv] Vendor OUI HT information (optional)
 3091          * XXX Vendor-specific OIDs (e.g. Atheros)
 3092          *      [tlv] WPA parameters
 3093          *      [tlv] WME parameters
 3094          *      [tlv] TDMA parameters (optional)
 3095          *      [tlv] Mesh ID (MBSS)
 3096          *      [tlv] Mesh Conf (MBSS)
 3097          *      [tlv] application data (optional)
 3098          * NB: we allocate the max space required for the TIM bitmap.
 3099          * XXX how big is this?
 3100          */
 3101         pktlen =   8                                    /* time stamp */
 3102                  + sizeof(uint16_t)                     /* beacon interval */
 3103                  + sizeof(uint16_t)                     /* capabilities */
 3104                  + 2 + ni->ni_esslen                    /* ssid */
 3105                  + 2 + IEEE80211_RATE_SIZE              /* supported rates */
 3106                  + 2 + 1                                /* DS parameters */
 3107                  + 2 + 6                                /* CF parameters */
 3108                  + 2 + 4 + vap->iv_tim_len              /* DTIM/IBSSPARMS */
 3109                  + IEEE80211_COUNTRY_MAX_SIZE           /* country */
 3110                  + 2 + 1                                /* power control */
 3111                  + sizeof(struct ieee80211_csa_ie)      /* CSA */
 3112                  + sizeof(struct ieee80211_quiet_ie)    /* Quiet */
 3113                  + 2 + 1                                /* ERP */
 3114                  + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
 3115                  + (vap->iv_caps & IEEE80211_C_WPA ?    /* WPA 1+2 */
 3116                         2*sizeof(struct ieee80211_ie_wpa) : 0)
 3117                  /* XXX conditional? */
 3118                  + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
 3119                  + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
 3120                  + (vap->iv_caps & IEEE80211_C_WME ?    /* WME */
 3121                         sizeof(struct ieee80211_wme_param) : 0)
 3122 #ifdef IEEE80211_SUPPORT_SUPERG
 3123                  + sizeof(struct ieee80211_ath_ie)      /* ATH */
 3124 #endif
 3125 #ifdef IEEE80211_SUPPORT_TDMA
 3126                  + (vap->iv_caps & IEEE80211_C_TDMA ?   /* TDMA */
 3127                         sizeof(struct ieee80211_tdma_param) : 0)
 3128 #endif
 3129 #ifdef IEEE80211_SUPPORT_MESH
 3130                  + 2 + ni->ni_meshidlen
 3131                  + sizeof(struct ieee80211_meshconf_ie)
 3132 #endif
 3133                  + IEEE80211_MAX_APPIE
 3134                  ;
 3135         m = ieee80211_getmgtframe(&frm,
 3136                 ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
 3137         if (m == NULL) {
 3138                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
 3139                         "%s: cannot get buf; size %u\n", __func__, pktlen);
 3140                 vap->iv_stats.is_tx_nobuf++;
 3141                 return NULL;
 3142         }
 3143         ieee80211_beacon_construct(m, frm, ni);
 3144 
 3145         M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
 3146         KASSERT(m != NULL, ("no space for 802.11 header?"));
 3147         wh = mtod(m, struct ieee80211_frame *);
 3148         wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
 3149             IEEE80211_FC0_SUBTYPE_BEACON;
 3150         wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
 3151         *(uint16_t *)wh->i_dur = 0;
 3152         IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
 3153         IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
 3154         IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
 3155         *(uint16_t *)wh->i_seq = 0;
 3156 
 3157         return m;
 3158 }
 3159 
 3160 /*
 3161  * Update the dynamic parts of a beacon frame based on the current state.
 3162  */
 3163 int
 3164 ieee80211_beacon_update(struct ieee80211_node *ni, struct mbuf *m, int mcast)
 3165 {
 3166         struct ieee80211vap *vap = ni->ni_vap;
 3167         struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
 3168         struct ieee80211com *ic = ni->ni_ic;
 3169         int len_changed = 0;
 3170         uint16_t capinfo;
 3171         struct ieee80211_frame *wh;
 3172         ieee80211_seq seqno;
 3173 
 3174         IEEE80211_LOCK(ic);
 3175         /*
 3176          * Handle 11h channel change when we've reached the count.
 3177          * We must recalculate the beacon frame contents to account
 3178          * for the new channel.  Note we do this only for the first
 3179          * vap that reaches this point; subsequent vaps just update
 3180          * their beacon state to reflect the recalculated channel.
 3181          */
 3182         if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
 3183             vap->iv_csa_count == ic->ic_csa_count) {
 3184                 vap->iv_csa_count = 0;
 3185                 /*
 3186                  * Effect channel change before reconstructing the beacon
 3187                  * frame contents as many places reference ni_chan.
 3188                  */
 3189                 if (ic->ic_csa_newchan != NULL)
 3190                         ieee80211_csa_completeswitch(ic);
 3191                 /*
 3192                  * NB: ieee80211_beacon_construct clears all pending
 3193                  * updates in bo_flags so we don't need to explicitly
 3194                  * clear IEEE80211_BEACON_CSA.
 3195                  */
 3196                 ieee80211_beacon_construct(m,
 3197                     mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
 3198 
 3199                 /* XXX do WME aggressive mode processing? */
 3200                 IEEE80211_UNLOCK(ic);
 3201                 return 1;               /* just assume length changed */
 3202         }
 3203 
 3204         wh = mtod(m, struct ieee80211_frame *);
 3205         seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
 3206         *(uint16_t *)&wh->i_seq[0] =
 3207                 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
 3208         M_SEQNO_SET(m, seqno);
 3209 
 3210         /* XXX faster to recalculate entirely or just changes? */
 3211         capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
 3212         *bo->bo_caps = htole16(capinfo);
 3213 
 3214         if (vap->iv_flags & IEEE80211_F_WME) {
 3215                 struct ieee80211_wme_state *wme = &ic->ic_wme;
 3216 
 3217                 /*
 3218                  * Check for aggressive mode change.  When there is
 3219                  * significant high priority traffic in the BSS
 3220                  * throttle back BE traffic by using conservative
 3221                  * parameters.  Otherwise BE uses aggressive params
 3222                  * to optimize performance of legacy/non-QoS traffic.
 3223                  */
 3224                 if (wme->wme_flags & WME_F_AGGRMODE) {
 3225                         if (wme->wme_hipri_traffic >
 3226                             wme->wme_hipri_switch_thresh) {
 3227                                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
 3228                                     "%s: traffic %u, disable aggressive mode\n",
 3229                                     __func__, wme->wme_hipri_traffic);
 3230                                 wme->wme_flags &= ~WME_F_AGGRMODE;
 3231                                 ieee80211_wme_updateparams_locked(vap);
 3232                                 wme->wme_hipri_traffic =
 3233                                         wme->wme_hipri_switch_hysteresis;
 3234                         } else
 3235                                 wme->wme_hipri_traffic = 0;
 3236                 } else {
 3237                         if (wme->wme_hipri_traffic <=
 3238                             wme->wme_hipri_switch_thresh) {
 3239                                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
 3240                                     "%s: traffic %u, enable aggressive mode\n",
 3241                                     __func__, wme->wme_hipri_traffic);
 3242                                 wme->wme_flags |= WME_F_AGGRMODE;
 3243                                 ieee80211_wme_updateparams_locked(vap);
 3244                                 wme->wme_hipri_traffic = 0;
 3245                         } else
 3246                                 wme->wme_hipri_traffic =
 3247                                         wme->wme_hipri_switch_hysteresis;
 3248                 }
 3249                 if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
 3250                         (void) ieee80211_add_wme_param(bo->bo_wme, wme);
 3251                         clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
 3252                 }
 3253         }
 3254 
 3255         if (isset(bo->bo_flags,  IEEE80211_BEACON_HTINFO)) {
 3256                 ieee80211_ht_update_beacon(vap, bo);
 3257                 clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
 3258         }
 3259 #ifdef IEEE80211_SUPPORT_TDMA
 3260         if (vap->iv_caps & IEEE80211_C_TDMA) {
 3261                 /*
 3262                  * NB: the beacon is potentially updated every TBTT.
 3263                  */
 3264                 ieee80211_tdma_update_beacon(vap, bo);
 3265         }
 3266 #endif
 3267 #ifdef IEEE80211_SUPPORT_MESH
 3268         if (vap->iv_opmode == IEEE80211_M_MBSS)
 3269                 ieee80211_mesh_update_beacon(vap, bo);
 3270 #endif
 3271 
 3272         if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
 3273             vap->iv_opmode == IEEE80211_M_MBSS) {       /* NB: no IBSS support*/
 3274                 struct ieee80211_tim_ie *tie =
 3275                         (struct ieee80211_tim_ie *) bo->bo_tim;
 3276                 if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
 3277                         u_int timlen, timoff, i;
 3278                         /* 
 3279                          * ATIM/DTIM needs updating.  If it fits in the
 3280                          * current space allocated then just copy in the
 3281                          * new bits.  Otherwise we need to move any trailing
 3282                          * data to make room.  Note that we know there is
 3283                          * contiguous space because ieee80211_beacon_allocate
 3284                          * insures there is space in the mbuf to write a
 3285                          * maximal-size virtual bitmap (based on iv_max_aid).
 3286                          */
 3287                         /*
 3288                          * Calculate the bitmap size and offset, copy any
 3289                          * trailer out of the way, and then copy in the
 3290                          * new bitmap and update the information element.
 3291                          * Note that the tim bitmap must contain at least
 3292                          * one byte and any offset must be even.
 3293                          */
 3294                         if (vap->iv_ps_pending != 0) {
 3295                                 timoff = 128;           /* impossibly large */
 3296                                 for (i = 0; i < vap->iv_tim_len; i++)
 3297                                         if (vap->iv_tim_bitmap[i]) {
 3298                                                 timoff = i &~ 1;
 3299                                                 break;
 3300                                         }
 3301                                 KASSERT(timoff != 128, ("tim bitmap empty!"));
 3302                                 for (i = vap->iv_tim_len-1; i >= timoff; i--)
 3303                                         if (vap->iv_tim_bitmap[i])
 3304                                                 break;
 3305                                 timlen = 1 + (i - timoff);
 3306                         } else {
 3307                                 timoff = 0;
 3308                                 timlen = 1;
 3309                         }
 3310                         if (timlen != bo->bo_tim_len) {
 3311                                 /* copy up/down trailer */
 3312                                 int adjust = tie->tim_bitmap+timlen
 3313                                            - bo->bo_tim_trailer;
 3314                                 ovbcopy(bo->bo_tim_trailer,
 3315                                     bo->bo_tim_trailer+adjust,
 3316                                     bo->bo_tim_trailer_len);
 3317                                 bo->bo_tim_trailer += adjust;
 3318                                 bo->bo_erp += adjust;
 3319                                 bo->bo_htinfo += adjust;
 3320 #ifdef IEEE80211_SUPPORT_SUPERG
 3321                                 bo->bo_ath += adjust;
 3322 #endif
 3323 #ifdef IEEE80211_SUPPORT_TDMA
 3324                                 bo->bo_tdma += adjust;
 3325 #endif
 3326 #ifdef IEEE80211_SUPPORT_MESH
 3327                                 bo->bo_meshconf += adjust;
 3328 #endif
 3329                                 bo->bo_appie += adjust;
 3330                                 bo->bo_wme += adjust;
 3331                                 bo->bo_csa += adjust;
 3332                                 bo->bo_quiet += adjust;
 3333                                 bo->bo_tim_len = timlen;
 3334 
 3335                                 /* update information element */
 3336                                 tie->tim_len = 3 + timlen;
 3337                                 tie->tim_bitctl = timoff;
 3338                                 len_changed = 1;
 3339                         }
 3340                         memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
 3341                                 bo->bo_tim_len);
 3342 
 3343                         clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
 3344 
 3345                         IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
 3346                                 "%s: TIM updated, pending %u, off %u, len %u\n",
 3347                                 __func__, vap->iv_ps_pending, timoff, timlen);
 3348                 }
 3349                 /* count down DTIM period */
 3350                 if (tie->tim_count == 0)
 3351                         tie->tim_count = tie->tim_period - 1;
 3352                 else
 3353                         tie->tim_count--;
 3354                 /* update state for buffered multicast frames on DTIM */
 3355                 if (mcast && tie->tim_count == 0)
 3356                         tie->tim_bitctl |= 1;
 3357                 else
 3358                         tie->tim_bitctl &= ~1;
 3359                 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
 3360                         struct ieee80211_csa_ie *csa =
 3361                             (struct ieee80211_csa_ie *) bo->bo_csa;
 3362 
 3363                         /*
 3364                          * Insert or update CSA ie.  If we're just starting
 3365                          * to count down to the channel switch then we need
 3366                          * to insert the CSA ie.  Otherwise we just need to
 3367                          * drop the count.  The actual change happens above
 3368                          * when the vap's count reaches the target count.
 3369                          */
 3370                         if (vap->iv_csa_count == 0) {
 3371                                 memmove(&csa[1], csa, bo->bo_csa_trailer_len);
 3372                                 bo->bo_erp += sizeof(*csa);
 3373                                 bo->bo_htinfo += sizeof(*csa);
 3374                                 bo->bo_wme += sizeof(*csa);
 3375 #ifdef IEEE80211_SUPPORT_SUPERG
 3376                                 bo->bo_ath += sizeof(*csa);
 3377 #endif
 3378 #ifdef IEEE80211_SUPPORT_TDMA
 3379                                 bo->bo_tdma += sizeof(*csa);
 3380 #endif
 3381 #ifdef IEEE80211_SUPPORT_MESH
 3382                                 bo->bo_meshconf += sizeof(*csa);
 3383 #endif
 3384                                 bo->bo_appie += sizeof(*csa);
 3385                                 bo->bo_csa_trailer_len += sizeof(*csa);
 3386                                 bo->bo_quiet += sizeof(*csa);
 3387                                 bo->bo_tim_trailer_len += sizeof(*csa);
 3388                                 m->m_len += sizeof(*csa);
 3389                                 m->m_pkthdr.len += sizeof(*csa);
 3390 
 3391                                 ieee80211_add_csa(bo->bo_csa, vap);
 3392                         } else
 3393                                 csa->csa_count--;
 3394                         vap->iv_csa_count++;
 3395                         /* NB: don't clear IEEE80211_BEACON_CSA */
 3396                 }
 3397                 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
 3398                     (vap->iv_flags_ext & IEEE80211_FEXT_DFS) ){
 3399                         if (vap->iv_quiet)
 3400                                 ieee80211_add_quiet(bo->bo_quiet, vap);
 3401                 }
 3402                 if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
 3403                         /*
 3404                          * ERP element needs updating.
 3405                          */
 3406                         (void) ieee80211_add_erp(bo->bo_erp, ic);
 3407                         clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
 3408                 }
 3409 #ifdef IEEE80211_SUPPORT_SUPERG
 3410                 if (isset(bo->bo_flags,  IEEE80211_BEACON_ATH)) {
 3411                         ieee80211_add_athcaps(bo->bo_ath, ni);
 3412                         clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
 3413                 }
 3414 #endif
 3415         }
 3416         if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
 3417                 const struct ieee80211_appie *aie = vap->iv_appie_beacon;
 3418                 int aielen;
 3419                 uint8_t *frm;
 3420 
 3421                 aielen = 0;
 3422                 if (aie != NULL)
 3423                         aielen += aie->ie_len;
 3424                 if (aielen != bo->bo_appie_len) {
 3425                         /* copy up/down trailer */
 3426                         int adjust = aielen - bo->bo_appie_len;
 3427                         ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
 3428                                 bo->bo_tim_trailer_len);
 3429                         bo->bo_tim_trailer += adjust;
 3430                         bo->bo_appie += adjust;
 3431                         bo->bo_appie_len = aielen;
 3432 
 3433                         len_changed = 1;
 3434                 }
 3435                 frm = bo->bo_appie;
 3436                 if (aie != NULL)
 3437                         frm  = add_appie(frm, aie);
 3438                 clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
 3439         }
 3440         IEEE80211_UNLOCK(ic);
 3441 
 3442         return len_changed;
 3443 }
 3444 
 3445 /*
 3446  * Do Ethernet-LLC encapsulation for each payload in a fast frame
 3447  * tunnel encapsulation.  The frame is assumed to have an Ethernet
 3448  * header at the front that must be stripped before prepending the
 3449  * LLC followed by the Ethernet header passed in (with an Ethernet
 3450  * type that specifies the payload size).
 3451  */
 3452 struct mbuf *
 3453 ieee80211_ff_encap1(struct ieee80211vap *vap, struct mbuf *m,
 3454         const struct ether_header *eh)
 3455 {
 3456         struct llc *llc;
 3457         uint16_t payload;
 3458 
 3459         /* XXX optimize by combining m_adj+M_PREPEND */
 3460         m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
 3461         llc = mtod(m, struct llc *);
 3462         llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
 3463         llc->llc_control = LLC_UI;
 3464         llc->llc_snap.org_code[0] = 0;
 3465         llc->llc_snap.org_code[1] = 0;
 3466         llc->llc_snap.org_code[2] = 0;
 3467         llc->llc_snap.ether_type = eh->ether_type;
 3468         payload = m->m_pkthdr.len;              /* NB: w/o Ethernet header */
 3469 
 3470         M_PREPEND(m, sizeof(struct ether_header), M_NOWAIT);
 3471         if (m == NULL) {                /* XXX cannot happen */
 3472                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
 3473                         "%s: no space for ether_header\n", __func__);
 3474                 vap->iv_stats.is_tx_nobuf++;
 3475                 return NULL;
 3476         }
 3477         ETHER_HEADER_COPY(mtod(m, void *), eh);
 3478         mtod(m, struct ether_header *)->ether_type = htons(payload);
 3479         return m;
 3480 }
 3481 
 3482 /*
 3483  * Complete an mbuf transmission.
 3484  *
 3485  * For now, this simply processes a completed frame after the
 3486  * driver has completed it's transmission and/or retransmission.
 3487  * It assumes the frame is an 802.11 encapsulated frame.
 3488  *
 3489  * Later on it will grow to become the exit path for a given frame
 3490  * from the driver and, depending upon how it's been encapsulated
 3491  * and already transmitted, it may end up doing A-MPDU retransmission,
 3492  * power save requeuing, etc.
 3493  *
 3494  * In order for the above to work, the driver entry point to this
 3495  * must not hold any driver locks.  Thus, the driver needs to delay
 3496  * any actual mbuf completion until it can release said locks.
 3497  *
 3498  * This frees the mbuf and if the mbuf has a node reference,
 3499  * the node reference will be freed.
 3500  */
 3501 void
 3502 ieee80211_tx_complete(struct ieee80211_node *ni, struct mbuf *m, int status)
 3503 {
 3504 
 3505         if (ni != NULL) {
 3506                 struct ifnet *ifp = ni->ni_vap->iv_ifp;
 3507 
 3508                 if (status == 0) {
 3509                         if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
 3510                         if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
 3511                         if (m->m_flags & M_MCAST)
 3512                                 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
 3513                 } else
 3514                         if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
 3515                 if (m->m_flags & M_TXCB)
 3516                         ieee80211_process_callback(ni, m, status);
 3517                 ieee80211_free_node(ni);
 3518         }
 3519         m_freem(m);
 3520 }

Cache object: a9fe591cfbf1e1ca29a6445522ba0319


[ 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.