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