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

Cache object: 574da5205b66d85596ba29c222e70260


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