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