ieee80211_output.c revision 298359
12742Swollman/*-
22742Swollman * Copyright (c) 2001 Atsushi Onoe
32742Swollman * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
42742Swollman * All rights reserved.
52742Swollman *
62742Swollman * Redistribution and use in source and binary forms, with or without
72742Swollman * modification, are permitted provided that the following conditions
82742Swollman * are met:
92742Swollman * 1. Redistributions of source code must retain the above copyright
102742Swollman *    notice, this list of conditions and the following disclaimer.
112742Swollman * 2. Redistributions in binary form must reproduce the above copyright
122742Swollman *    notice, this list of conditions and the following disclaimer in the
132742Swollman *    documentation and/or other materials provided with the distribution.
142742Swollman *
152742Swollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
162742Swollman * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
172742Swollman * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
182742Swollman * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
192742Swollman * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
202742Swollman * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
212742Swollman * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
222742Swollman * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
232742Swollman * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
242742Swollman * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
252742Swollman */
262742Swollman
278029Swollman#include <sys/cdefs.h>
288029Swollman__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_output.c 298359 2016-04-20 18:29:30Z avos $");
298029Swollman
302742Swollman#include "opt_inet.h"
312742Swollman#include "opt_inet6.h"
322742Swollman#include "opt_wlan.h"
332742Swollman
342742Swollman#include <sys/param.h>
352742Swollman#include <sys/systm.h>
362742Swollman#include <sys/kernel.h>
372742Swollman#include <sys/malloc.h>
382742Swollman#include <sys/mbuf.h>
398029Swollman#include <sys/endian.h>
408029Swollman
418029Swollman#include <sys/socket.h>
422742Swollman
432742Swollman#include <net/bpf.h>
442742Swollman#include <net/ethernet.h>
452742Swollman#include <net/if.h>
462742Swollman#include <net/if_var.h>
472742Swollman#include <net/if_llc.h>
482742Swollman#include <net/if_media.h>
492742Swollman#include <net/if_vlan_var.h>
502742Swollman
512742Swollman#include <net80211/ieee80211_var.h>
522742Swollman#include <net80211/ieee80211_regdomain.h>
532742Swollman#ifdef IEEE80211_SUPPORT_SUPERG
548029Swollman#include <net80211/ieee80211_superg.h>
558029Swollman#endif
562742Swollman#ifdef IEEE80211_SUPPORT_TDMA
572742Swollman#include <net80211/ieee80211_tdma.h>
582742Swollman#endif
592742Swollman#include <net80211/ieee80211_wds.h>
602742Swollman#include <net80211/ieee80211_mesh.h>
612742Swollman
622742Swollman#if defined(INET) || defined(INET6)
632742Swollman#include <netinet/in.h>
642742Swollman#endif
652742Swollman
662742Swollman#ifdef INET
672742Swollman#include <netinet/if_ether.h>
682742Swollman#include <netinet/in_systm.h>
692742Swollman#include <netinet/ip.h>
702742Swollman#endif
712742Swollman#ifdef INET6
728029Swollman#include <netinet/ip6.h>
738029Swollman#endif
742742Swollman
752742Swollman#include <security/mac/mac_framework.h>
762742Swollman
772742Swollman#define	ETHER_HEADER_COPY(dst, src) \
782742Swollman	memcpy(dst, src, sizeof(struct ether_header))
792742Swollman
802742Swollmanstatic int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
812742Swollman	u_int hdrsize, u_int ciphdrsize, u_int mtu);
822742Swollmanstatic	void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
832742Swollman
842742Swollman#ifdef IEEE80211_DEBUG
852742Swollman/*
862742Swollman * Decide if an outbound management frame should be
872742Swollman * printed when debugging is enabled.  This filters some
882742Swollman * of the less interesting frames that come frequently
892742Swollman * (e.g. beacons).
902742Swollman */
912742Swollmanstatic __inline int
922742Swollmandoprint(struct ieee80211vap *vap, int subtype)
932742Swollman{
948029Swollman	switch (subtype) {
958029Swollman	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
962742Swollman		return (vap->iv_opmode == IEEE80211_M_IBSS);
972742Swollman	}
982742Swollman	return 1;
992742Swollman}
1002742Swollman#endif
1012742Swollman
1022742Swollman/*
1032742Swollman * Transmit a frame to the given destination on the given VAP.
1042742Swollman *
1052742Swollman * It's up to the caller to figure out the details of who this
1062742Swollman * is going to and resolving the node.
1072742Swollman *
1082742Swollman * This routine takes care of queuing it for power save,
1092742Swollman * A-MPDU state stuff, fast-frames state stuff, encapsulation
1108029Swollman * if required, then passing it up to the driver layer.
1118029Swollman *
1122742Swollman * This routine (for now) consumes the mbuf and frees the node
1132742Swollman * reference; it ideally will return a TX status which reflects
1142742Swollman * whether the mbuf was consumed or not, so the caller can
1152742Swollman * free the mbuf (if appropriate) and the node reference (again,
1162742Swollman * if appropriate.)
1172742Swollman */
1182742Swollmanint
1192742Swollmanieee80211_vap_pkt_send_dest(struct ieee80211vap *vap, struct mbuf *m,
1202742Swollman    struct ieee80211_node *ni)
1212742Swollman{
1222742Swollman	struct ieee80211com *ic = vap->iv_ic;
1232742Swollman	struct ifnet *ifp = vap->iv_ifp;
1242742Swollman	int len, mcast;
1252742Swollman
1262742Swollman	if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
1278029Swollman	    (m->m_flags & M_PWR_SAV) == 0) {
1288029Swollman		/*
1298029Swollman		 * Station in power save mode; pass the frame
1302742Swollman		 * to the 802.11 layer and continue.  We'll get
1312742Swollman		 * the frame back when the time is right.
1322742Swollman		 * XXX lose WDS vap linkage?
1332742Swollman		 */
1342742Swollman		if (ieee80211_pwrsave(ni, m) != 0)
1352742Swollman			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1368029Swollman		ieee80211_free_node(ni);
1378029Swollman
1388029Swollman		/*
1392742Swollman		 * We queued it fine, so tell the upper layer
1402742Swollman		 * that we consumed it.
1412742Swollman		 */
1428029Swollman		return (0);
1438029Swollman	}
1448029Swollman	/* calculate priority so drivers can find the tx queue */
1452742Swollman	if (ieee80211_classify(ni, m)) {
1462742Swollman		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
1472742Swollman		    ni->ni_macaddr, NULL,
1488029Swollman		    "%s", "classification failure");
1498029Swollman		vap->iv_stats.is_tx_classify++;
1502742Swollman		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1512742Swollman		m_freem(m);
1522742Swollman		ieee80211_free_node(ni);
1532742Swollman
1542742Swollman		/* XXX better status? */
1552742Swollman		return (0);
1562742Swollman	}
1572742Swollman	/*
1582742Swollman	 * Stash the node pointer.  Note that we do this after
1592742Swollman	 * any call to ieee80211_dwds_mcast because that code
1602742Swollman	 * uses any existing value for rcvif to identify the
1612742Swollman	 * interface it (might have been) received on.
1628029Swollman	 */
1638029Swollman	m->m_pkthdr.rcvif = (void *)ni;
1642742Swollman	mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1: 0;
1652742Swollman	len = m->m_pkthdr.len;
1662742Swollman
1672742Swollman	BPF_MTAP(ifp, m);		/* 802.3 tx */
1682742Swollman
1692742Swollman	/*
1702742Swollman	 * Check if A-MPDU tx aggregation is setup or if we
1712742Swollman	 * should try to enable it.  The sta must be associated
1722742Swollman	 * with HT and A-MPDU enabled for use.  When the policy
1732742Swollman	 * routine decides we should enable A-MPDU we issue an
1748029Swollman	 * ADDBA request and wait for a reply.  The frame being
1758029Swollman	 * encapsulated will go out w/o using A-MPDU, or possibly
1762742Swollman	 * it might be collected by the driver and held/retransmit.
1772742Swollman	 * The default ic_ampdu_enable routine handles staggering
1782742Swollman	 * ADDBA requests in case the receiver NAK's us or we are
1792742Swollman	 * otherwise unable to establish a BA stream.
1802742Swollman	 */
1818029Swollman	if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
1828029Swollman	    (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX)) {
1832742Swollman		if ((m->m_flags & M_EAPOL) == 0) {
1842742Swollman			int tid = WME_AC_TO_TID(M_WME_GETAC(m));
1852742Swollman			struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[tid];
1862742Swollman
1872742Swollman			ieee80211_txampdu_count_packet(tap);
1888029Swollman			if (IEEE80211_AMPDU_RUNNING(tap)) {
1898029Swollman				/*
1902742Swollman				 * Operational, mark frame for aggregation.
1912742Swollman				 *
1922742Swollman				 * XXX do tx aggregation here
1932742Swollman				 */
1942742Swollman				m->m_flags |= M_AMPDU_MPDU;
1952742Swollman			} else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
1962742Swollman			    ic->ic_ampdu_enable(ni, tap)) {
1972742Swollman				/*
1982742Swollman				 * Not negotiated yet, request service.
1998029Swollman				 */
2008029Swollman				ieee80211_ampdu_request(ni, tap);
2018029Swollman				/* XXX hold frame for reply? */
2028029Swollman			}
2032742Swollman		}
2042742Swollman	}
2052742Swollman
2062742Swollman#ifdef IEEE80211_SUPPORT_SUPERG
2072742Swollman	/*
2088029Swollman	 * Check for AMSDU/FF; queue for aggregation
2098029Swollman	 *
2102742Swollman	 * Note: we don't bother trying to do fast frames or
2112742Swollman	 * A-MSDU encapsulation for 802.3 drivers.  Now, we
2122742Swollman	 * likely could do it for FF (because it's a magic
2132742Swollman	 * atheros tunnel LLC type) but I don't think we're going
2142742Swollman	 * to really need to.  For A-MSDU we'd have to set the
2152742Swollman	 * A-MSDU QoS bit in the wifi header, so we just plain
2162742Swollman	 * can't do it.
2172742Swollman	 *
2182742Swollman	 * Strictly speaking, we could actually /do/ A-MSDU / FF
2192742Swollman	 * with A-MPDU together which for certain circumstances
2202742Swollman	 * is beneficial (eg A-MSDU of TCK ACKs.)  However,
2212742Swollman	 * I'll ignore that for now so existing behaviour is maintained.
2222742Swollman	 * Later on it would be good to make "amsdu + ampdu" configurable.
2232742Swollman	 */
2242742Swollman	else if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
2252742Swollman		if ((! mcast) && ieee80211_amsdu_tx_ok(ni)) {
2262742Swollman			m = ieee80211_amsdu_check(ni, m);
2272742Swollman			if (m == NULL) {
2282742Swollman				/* NB: any ni ref held on stageq */
2292742Swollman				IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
2308029Swollman				    "%s: amsdu_check queued frame\n",
2318029Swollman				    __func__);
2328029Swollman				return (0);
2338029Swollman			}
2342742Swollman		} else if ((! mcast) && IEEE80211_ATH_CAP(vap, ni,
2352742Swollman		    IEEE80211_NODE_FF)) {
2362742Swollman			m = ieee80211_ff_check(ni, m);
2372742Swollman			if (m == NULL) {
2382742Swollman				/* NB: any ni ref held on stageq */
2392742Swollman				IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
2402742Swollman				    "%s: ff_check queued frame\n",
2412742Swollman				    __func__);
2428029Swollman				return (0);
2438029Swollman			}
2442742Swollman		}
2452742Swollman	}
2462742Swollman#endif /* IEEE80211_SUPPORT_SUPERG */
2472742Swollman
2482742Swollman	/*
2492742Swollman	 * Grab the TX lock - serialise the TX process from this
2502742Swollman	 * point (where TX state is being checked/modified)
2512742Swollman	 * through to driver queue.
2522742Swollman	 */
2538029Swollman	IEEE80211_TX_LOCK(ic);
2542742Swollman
2552742Swollman	/*
2562742Swollman	 * XXX make the encap and transmit code a separate function
2572742Swollman	 * so things like the FF (and later A-MSDU) path can just call
2582742Swollman	 * it for flushed frames.
2592742Swollman	 */
2602742Swollman	if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
2612742Swollman		/*
2622742Swollman		 * Encapsulate the packet in prep for transmission.
2632742Swollman		 */
2642742Swollman		m = ieee80211_encap(vap, ni, m);
2652742Swollman		if (m == NULL) {
2662742Swollman			/* NB: stat+msg handled in ieee80211_encap */
2672742Swollman			IEEE80211_TX_UNLOCK(ic);
2682742Swollman			ieee80211_free_node(ni);
2692742Swollman			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2702742Swollman			return (ENOBUFS);
2712742Swollman		}
2722742Swollman	}
2732742Swollman	(void) ieee80211_parent_xmitpkt(ic, m);
2742742Swollman
2752742Swollman	/*
2762742Swollman	 * Unlock at this point - no need to hold it across
2772742Swollman	 * ieee80211_free_node() (ie, the comlock)
2782742Swollman	 */
2792742Swollman	IEEE80211_TX_UNLOCK(ic);
2802742Swollman	ic->ic_lastdata = ticks;
2812742Swollman
2822742Swollman	return (0);
2832742Swollman}
2842742Swollman
2852742Swollman
2862742Swollman
2872742Swollman/*
2888029Swollman * Send the given mbuf through the given vap.
2898029Swollman *
2908029Swollman * This consumes the mbuf regardless of whether the transmit
2912742Swollman * was successful or not.
2922742Swollman *
2932742Swollman * This does none of the initial checks that ieee80211_start()
2942742Swollman * does (eg CAC timeout, interface wakeup) - the caller must
2952742Swollman * do this first.
2962742Swollman */
2972742Swollmanstatic int
2982742Swollmanieee80211_start_pkt(struct ieee80211vap *vap, struct mbuf *m)
2992742Swollman{
3002742Swollman#define	IS_DWDS(vap) \
3012742Swollman	(vap->iv_opmode == IEEE80211_M_WDS && \
3022742Swollman	 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
3032742Swollman	struct ieee80211com *ic = vap->iv_ic;
3048029Swollman	struct ifnet *ifp = vap->iv_ifp;
3058029Swollman	struct ieee80211_node *ni;
3062742Swollman	struct ether_header *eh;
3072742Swollman
3082742Swollman	/*
3092742Swollman	 * Cancel any background scan.
3102742Swollman	 */
3112742Swollman	if (ic->ic_flags & IEEE80211_F_SCAN)
3128029Swollman		ieee80211_cancel_anyscan(vap);
3138029Swollman	/*
3142742Swollman	 * Find the node for the destination so we can do
3152742Swollman	 * things like power save and fast frames aggregation.
3162742Swollman	 *
3172742Swollman	 * NB: past this point various code assumes the first
3182742Swollman	 *     mbuf has the 802.3 header present (and contiguous).
3192742Swollman	 */
3202742Swollman	ni = NULL;
3212742Swollman	if (m->m_len < sizeof(struct ether_header) &&
3222742Swollman	   (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
3232742Swollman		IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
3242742Swollman		    "discard frame, %s\n", "m_pullup failed");
3252742Swollman		vap->iv_stats.is_tx_nobuf++;	/* XXX */
3262742Swollman		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3272742Swollman		return (ENOBUFS);
3282742Swollman	}
3292742Swollman	eh = mtod(m, struct ether_header *);
3302742Swollman	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
3312742Swollman		if (IS_DWDS(vap)) {
3328029Swollman			/*
3338029Swollman			 * Only unicast frames from the above go out
3348029Swollman			 * DWDS vaps; multicast frames are handled by
3358029Swollman			 * dispatching the frame as it comes through
3368029Swollman			 * the AP vap (see below).
3378029Swollman			 */
3382742Swollman			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
3392742Swollman			    eh->ether_dhost, "mcast", "%s", "on DWDS");
3402742Swollman			vap->iv_stats.is_dwds_mcast++;
3412742Swollman			m_freem(m);
3422742Swollman			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3432742Swollman			/* XXX better status? */
3442742Swollman			return (ENOBUFS);
3452742Swollman		}
3462742Swollman		if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
3478029Swollman			/*
3488029Swollman			 * Spam DWDS vap's w/ multicast traffic.
3492742Swollman			 */
3502742Swollman			/* XXX only if dwds in use? */
3512742Swollman			ieee80211_dwds_mcast(vap, m);
3522742Swollman		}
3532742Swollman	}
3548029Swollman#ifdef IEEE80211_SUPPORT_MESH
3558029Swollman	if (vap->iv_opmode != IEEE80211_M_MBSS) {
3562742Swollman#endif
3572742Swollman		ni = ieee80211_find_txnode(vap, eh->ether_dhost);
3582742Swollman		if (ni == NULL) {
3592742Swollman			/* NB: ieee80211_find_txnode does stat+msg */
3602742Swollman			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3612742Swollman			m_freem(m);
3628029Swollman			/* XXX better status? */
3638029Swollman			return (ENOBUFS);
3648029Swollman		}
3652742Swollman		if (ni->ni_associd == 0 &&
3662742Swollman		    (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
3672742Swollman			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
3682742Swollman			    eh->ether_dhost, NULL,
3692742Swollman			    "sta not associated (type 0x%04x)",
3708029Swollman			    htons(eh->ether_type));
3718029Swollman			vap->iv_stats.is_tx_notassoc++;
3722742Swollman			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3732742Swollman			m_freem(m);
3742742Swollman			ieee80211_free_node(ni);
3752742Swollman			/* XXX better status? */
3762742Swollman			return (ENOBUFS);
3772742Swollman		}
3788029Swollman#ifdef IEEE80211_SUPPORT_MESH
3798029Swollman	} else {
3802742Swollman		if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) {
3812742Swollman			/*
3822742Swollman			 * Proxy station only if configured.
3832742Swollman			 */
3842742Swollman			if (!ieee80211_mesh_isproxyena(vap)) {
3858029Swollman				IEEE80211_DISCARD_MAC(vap,
3868029Swollman				    IEEE80211_MSG_OUTPUT |
3872742Swollman				    IEEE80211_MSG_MESH,
3882742Swollman				    eh->ether_dhost, NULL,
3892742Swollman				    "%s", "proxy not enabled");
3902742Swollman				vap->iv_stats.is_mesh_notproxy++;
3912742Swollman				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3922742Swollman				m_freem(m);
3932742Swollman				/* XXX better status? */
3942742Swollman				return (ENOBUFS);
3952742Swollman			}
3962742Swollman			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
3972742Swollman			    "forward frame from DS SA(%6D), DA(%6D)\n",
3988029Swollman			    eh->ether_shost, ":",
3998029Swollman			    eh->ether_dhost, ":");
4002742Swollman			ieee80211_mesh_proxy_check(vap, eh->ether_shost);
4012742Swollman		}
4022742Swollman		ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m);
4032742Swollman		if (ni == NULL) {
4042742Swollman			/*
4058029Swollman			 * NB: ieee80211_mesh_discover holds/disposes
4068029Swollman			 * frame (e.g. queueing on path discovery).
4072742Swollman			 */
4082742Swollman			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
4092742Swollman			/* XXX better status? */
4102742Swollman			return (ENOBUFS);
4112742Swollman		}
4128029Swollman	}
4138029Swollman#endif
4142742Swollman
4152742Swollman	/*
4162742Swollman	 * We've resolved the sender, so attempt to transmit it.
4172742Swollman	 */
4182742Swollman
4192742Swollman	if (vap->iv_state == IEEE80211_S_SLEEP) {
4202742Swollman		/*
4212742Swollman		 * In power save; queue frame and then  wakeup device
4222742Swollman		 * for transmit.
4232742Swollman		 */
4242742Swollman		ic->ic_lastdata = ticks;
4252742Swollman		if (ieee80211_pwrsave(ni, m) != 0)
4262742Swollman			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
4272742Swollman		ieee80211_free_node(ni);
4282742Swollman		ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
4292742Swollman		return (0);
4302742Swollman	}
4312742Swollman
4322742Swollman	if (ieee80211_vap_pkt_send_dest(vap, m, ni) != 0)
4332742Swollman		return (ENOBUFS);
4342742Swollman	return (0);
4352742Swollman#undef	IS_DWDS
4362742Swollman}
4372742Swollman
4382742Swollman/*
4392742Swollman * Start method for vap's.  All packets from the stack come
4402742Swollman * through here.  We handle common processing of the packets
4412742Swollman * before dispatching them to the underlying device.
4422742Swollman *
4432742Swollman * if_transmit() requires that the mbuf be consumed by this call
4442742Swollman * regardless of the return condition.
4452742Swollman */
4462742Swollmanint
4472742Swollmanieee80211_vap_transmit(struct ifnet *ifp, struct mbuf *m)
4482742Swollman{
4492742Swollman	struct ieee80211vap *vap = ifp->if_softc;
4502742Swollman	struct ieee80211com *ic = vap->iv_ic;
4512742Swollman
4522742Swollman	/*
4532742Swollman	 * No data frames go out unless we're running.
4542742Swollman	 * Note in particular this covers CAC and CSA
4552742Swollman	 * states (though maybe we should check muting
4562742Swollman	 * for CSA).
4572742Swollman	 */
4582742Swollman	if (vap->iv_state != IEEE80211_S_RUN &&
4592742Swollman	    vap->iv_state != IEEE80211_S_SLEEP) {
4602742Swollman		IEEE80211_LOCK(ic);
4612742Swollman		/* re-check under the com lock to avoid races */
4622742Swollman		if (vap->iv_state != IEEE80211_S_RUN &&
4632742Swollman		    vap->iv_state != IEEE80211_S_SLEEP) {
4642742Swollman			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
4652742Swollman			    "%s: ignore queue, in %s state\n",
4662742Swollman			    __func__, ieee80211_state_name[vap->iv_state]);
4672742Swollman			vap->iv_stats.is_tx_badstate++;
4682742Swollman			IEEE80211_UNLOCK(ic);
4692742Swollman			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
4702742Swollman			m_freem(m);
4712742Swollman			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
4722742Swollman			return (ENETDOWN);
4732742Swollman		}
4742742Swollman		IEEE80211_UNLOCK(ic);
4752742Swollman	}
4762742Swollman
4772742Swollman	/*
4782742Swollman	 * Sanitize mbuf flags for net80211 use.  We cannot
4792742Swollman	 * clear M_PWR_SAV or M_MORE_DATA because these may
4802742Swollman	 * be set for frames that are re-submitted from the
4812742Swollman	 * power save queue.
4822742Swollman	 *
4832742Swollman	 * NB: This must be done before ieee80211_classify as
4842742Swollman	 *     it marks EAPOL in frames with M_EAPOL.
4852742Swollman	 */
4862742Swollman	m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
4872742Swollman
4882742Swollman	/*
4892742Swollman	 * Bump to the packet transmission path.
4902742Swollman	 * The mbuf will be consumed here.
4912742Swollman	 */
4922742Swollman	return (ieee80211_start_pkt(vap, m));
4932742Swollman}
4942742Swollman
4952742Swollmanvoid
4962742Swollmanieee80211_vap_qflush(struct ifnet *ifp)
4972742Swollman{
4982742Swollman
4992742Swollman	/* Empty for now */
5002742Swollman}
5012742Swollman
5022742Swollman/*
5032742Swollman * 802.11 raw output routine.
5042742Swollman *
5052742Swollman * XXX TODO: this (and other send routines) should correctly
5062742Swollman * XXX keep the pwr mgmt bit set if it decides to call into the
5072742Swollman * XXX driver to send a frame whilst the state is SLEEP.
5082742Swollman *
5092742Swollman * Otherwise the peer may decide that we're awake and flood us
5102742Swollman * with traffic we are still too asleep to receive!
5112742Swollman */
5122742Swollmanint
5132742Swollmanieee80211_raw_output(struct ieee80211vap *vap, struct ieee80211_node *ni,
5142742Swollman    struct mbuf *m, const struct ieee80211_bpf_params *params)
5152742Swollman{
5162742Swollman	struct ieee80211com *ic = vap->iv_ic;
5172742Swollman	int error;
5182742Swollman
5192742Swollman	/*
5202742Swollman	 * Set node - the caller has taken a reference, so ensure
5212742Swollman	 * that the mbuf has the same node value that
5222742Swollman	 * it would if it were going via the normal path.
5232742Swollman	 */
5242742Swollman	m->m_pkthdr.rcvif = (void *)ni;
5252742Swollman
5262742Swollman	/*
5272742Swollman	 * Attempt to add bpf transmit parameters.
5282742Swollman	 *
5292742Swollman	 * For now it's ok to fail; the raw_xmit api still takes
5302742Swollman	 * them as an option.
5312742Swollman	 *
5322742Swollman	 * Later on when ic_raw_xmit() has params removed,
5332742Swollman	 * they'll have to be added - so fail the transmit if
5342742Swollman	 * they can't be.
5352742Swollman	 */
5362742Swollman	if (params)
5372742Swollman		(void) ieee80211_add_xmit_params(m, params);
5382742Swollman
5392742Swollman	error = ic->ic_raw_xmit(ni, m, params);
5402742Swollman	if (error) {
5412742Swollman		if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, 1);
5422742Swollman		ieee80211_free_node(ni);
5432742Swollman	}
5442742Swollman	return (error);
5452742Swollman}
5462742Swollman
5472742Swollman/*
5482742Swollman * 802.11 output routine. This is (currently) used only to
5492742Swollman * connect bpf write calls to the 802.11 layer for injecting
5502742Swollman * raw 802.11 frames.
5512742Swollman */
5522742Swollmanint
5532742Swollmanieee80211_output(struct ifnet *ifp, struct mbuf *m,
5542742Swollman	const struct sockaddr *dst, struct route *ro)
5552742Swollman{
5562742Swollman#define senderr(e) do { error = (e); goto bad;} while (0)
5572742Swollman	struct ieee80211_node *ni = NULL;
5582742Swollman	struct ieee80211vap *vap;
5592742Swollman	struct ieee80211_frame *wh;
5602742Swollman	struct ieee80211com *ic = NULL;
5612742Swollman	int error;
5622742Swollman	int ret;
5632742Swollman
5642742Swollman	if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
5652742Swollman		/*
5662742Swollman		 * Short-circuit requests if the vap is marked OACTIVE
5672742Swollman		 * as this can happen because a packet came down through
5682742Swollman		 * ieee80211_start before the vap entered RUN state in
5692742Swollman		 * which case it's ok to just drop the frame.  This
5702742Swollman		 * should not be necessary but callers of if_output don't
5712742Swollman		 * check OACTIVE.
5722742Swollman		 */
5732742Swollman		senderr(ENETDOWN);
5742742Swollman	}
5752742Swollman	vap = ifp->if_softc;
5762742Swollman	ic = vap->iv_ic;
5772742Swollman	/*
5782742Swollman	 * Hand to the 802.3 code if not tagged as
5792742Swollman	 * a raw 802.11 frame.
5802742Swollman	 */
5812742Swollman	if (dst->sa_family != AF_IEEE80211)
5822742Swollman		return vap->iv_output(ifp, m, dst, ro);
5832742Swollman#ifdef MAC
5842742Swollman	error = mac_ifnet_check_transmit(ifp, m);
5852742Swollman	if (error)
5862742Swollman		senderr(error);
5872742Swollman#endif
5882742Swollman	if (ifp->if_flags & IFF_MONITOR)
5892742Swollman		senderr(ENETDOWN);
5902742Swollman	if (!IFNET_IS_UP_RUNNING(ifp))
5912742Swollman		senderr(ENETDOWN);
5922742Swollman	if (vap->iv_state == IEEE80211_S_CAC) {
5932742Swollman		IEEE80211_DPRINTF(vap,
5942742Swollman		    IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
5952742Swollman		    "block %s frame in CAC state\n", "raw data");
5962742Swollman		vap->iv_stats.is_tx_badstate++;
5972742Swollman		senderr(EIO);		/* XXX */
5982742Swollman	} else if (vap->iv_state == IEEE80211_S_SCAN)
5992742Swollman		senderr(EIO);
6002742Swollman	/* XXX bypass bridge, pfil, carp, etc. */
6012742Swollman
6022742Swollman	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
6032742Swollman		senderr(EIO);	/* XXX */
6042742Swollman	wh = mtod(m, struct ieee80211_frame *);
6052742Swollman	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
6062742Swollman	    IEEE80211_FC0_VERSION_0)
6072742Swollman		senderr(EIO);	/* XXX */
6082742Swollman
6092742Swollman	/* locate destination node */
6102742Swollman	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
6112742Swollman	case IEEE80211_FC1_DIR_NODS:
6122742Swollman	case IEEE80211_FC1_DIR_FROMDS:
6132742Swollman		ni = ieee80211_find_txnode(vap, wh->i_addr1);
6142742Swollman		break;
6152742Swollman	case IEEE80211_FC1_DIR_TODS:
6162742Swollman	case IEEE80211_FC1_DIR_DSTODS:
6172742Swollman		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame))
6182742Swollman			senderr(EIO);	/* XXX */
6192742Swollman		ni = ieee80211_find_txnode(vap, wh->i_addr3);
6202742Swollman		break;
6212742Swollman	default:
6222742Swollman		senderr(EIO);	/* XXX */
6232742Swollman	}
6242742Swollman	if (ni == NULL) {
6252742Swollman		/*
6262742Swollman		 * Permit packets w/ bpf params through regardless
6272742Swollman		 * (see below about sa_len).
6282742Swollman		 */
6292742Swollman		if (dst->sa_len == 0)
6302742Swollman			senderr(EHOSTUNREACH);
6312742Swollman		ni = ieee80211_ref_node(vap->iv_bss);
6322742Swollman	}
6332742Swollman
6342742Swollman	/*
6352742Swollman	 * Sanitize mbuf for net80211 flags leaked from above.
6362742Swollman	 *
6372742Swollman	 * NB: This must be done before ieee80211_classify as
6382742Swollman	 *     it marks EAPOL in frames with M_EAPOL.
6392742Swollman	 */
6402742Swollman	m->m_flags &= ~M_80211_TX;
6412742Swollman
6422742Swollman	/* calculate priority so drivers can find the tx queue */
6432742Swollman	/* XXX assumes an 802.3 frame */
6442742Swollman	if (ieee80211_classify(ni, m))
6452742Swollman		senderr(EIO);		/* XXX */
6462742Swollman
6472742Swollman	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
6482742Swollman	IEEE80211_NODE_STAT(ni, tx_data);
6492742Swollman	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
6502742Swollman		IEEE80211_NODE_STAT(ni, tx_mcast);
6512742Swollman		m->m_flags |= M_MCAST;
6522742Swollman	} else
6532742Swollman		IEEE80211_NODE_STAT(ni, tx_ucast);
6542742Swollman	/* NB: ieee80211_encap does not include 802.11 header */
6552742Swollman	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, m->m_pkthdr.len);
6562742Swollman
6572742Swollman	IEEE80211_TX_LOCK(ic);
6582742Swollman
6592742Swollman	/*
6602742Swollman	 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
6612742Swollman	 * present by setting the sa_len field of the sockaddr (yes,
6622742Swollman	 * this is a hack).
6632742Swollman	 * NB: we assume sa_data is suitably aligned to cast.
6642742Swollman	 */
6652742Swollman	ret = ieee80211_raw_output(vap, ni, m,
6662742Swollman	    (const struct ieee80211_bpf_params *)(dst->sa_len ?
6672742Swollman		dst->sa_data : NULL));
6682742Swollman	IEEE80211_TX_UNLOCK(ic);
6692742Swollman	return (ret);
6702742Swollmanbad:
6712742Swollman	if (m != NULL)
6722742Swollman		m_freem(m);
6732742Swollman	if (ni != NULL)
6742742Swollman		ieee80211_free_node(ni);
6752742Swollman	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
6762742Swollman	return error;
6772742Swollman#undef senderr
6782742Swollman}
6792742Swollman
6802742Swollman/*
6812742Swollman * Set the direction field and address fields of an outgoing
6822742Swollman * frame.  Note this should be called early on in constructing
6832742Swollman * a frame as it sets i_fc[1]; other bits can then be or'd in.
6842742Swollman */
6852742Swollmanvoid
6862742Swollmanieee80211_send_setup(
6872742Swollman	struct ieee80211_node *ni,
6882742Swollman	struct mbuf *m,
6892742Swollman	int type, int tid,
6902742Swollman	const uint8_t sa[IEEE80211_ADDR_LEN],
6912742Swollman	const uint8_t da[IEEE80211_ADDR_LEN],
6922742Swollman	const uint8_t bssid[IEEE80211_ADDR_LEN])
6932742Swollman{
6942742Swollman#define	WH4(wh)	((struct ieee80211_frame_addr4 *)wh)
6952742Swollman	struct ieee80211vap *vap = ni->ni_vap;
6962742Swollman	struct ieee80211_tx_ampdu *tap;
6972742Swollman	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
6982742Swollman	ieee80211_seq seqno;
6992742Swollman
7002742Swollman	IEEE80211_TX_LOCK_ASSERT(ni->ni_ic);
7012742Swollman
7022742Swollman	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
7032742Swollman	if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
7042742Swollman		switch (vap->iv_opmode) {
7052742Swollman		case IEEE80211_M_STA:
7062742Swollman			wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
7072742Swollman			IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
7082742Swollman			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
7092742Swollman			IEEE80211_ADDR_COPY(wh->i_addr3, da);
7102742Swollman			break;
7112742Swollman		case IEEE80211_M_IBSS:
7122742Swollman		case IEEE80211_M_AHDEMO:
7132742Swollman			wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
7142742Swollman			IEEE80211_ADDR_COPY(wh->i_addr1, da);
7152742Swollman			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
7162742Swollman			IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
7172742Swollman			break;
7182742Swollman		case IEEE80211_M_HOSTAP:
7192742Swollman			wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
7202742Swollman			IEEE80211_ADDR_COPY(wh->i_addr1, da);
7212742Swollman			IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
7222742Swollman			IEEE80211_ADDR_COPY(wh->i_addr3, sa);
7232742Swollman			break;
7242742Swollman		case IEEE80211_M_WDS:
7252742Swollman			wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
7262742Swollman			IEEE80211_ADDR_COPY(wh->i_addr1, da);
7272742Swollman			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
7282742Swollman			IEEE80211_ADDR_COPY(wh->i_addr3, da);
7292742Swollman			IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
7302742Swollman			break;
7312742Swollman		case IEEE80211_M_MBSS:
7322742Swollman#ifdef IEEE80211_SUPPORT_MESH
7332742Swollman			if (IEEE80211_IS_MULTICAST(da)) {
7342742Swollman				wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
7352742Swollman				/* XXX next hop */
7362742Swollman				IEEE80211_ADDR_COPY(wh->i_addr1, da);
7372742Swollman				IEEE80211_ADDR_COPY(wh->i_addr2,
7382742Swollman				    vap->iv_myaddr);
7392742Swollman			} else {
7402742Swollman				wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
7412742Swollman				IEEE80211_ADDR_COPY(wh->i_addr1, da);
7422742Swollman				IEEE80211_ADDR_COPY(wh->i_addr2,
7432742Swollman				    vap->iv_myaddr);
7442742Swollman				IEEE80211_ADDR_COPY(wh->i_addr3, da);
7452742Swollman				IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
7462742Swollman			}
7472742Swollman#endif
7482742Swollman			break;
7492742Swollman		case IEEE80211_M_MONITOR:	/* NB: to quiet compiler */
7502742Swollman			break;
7512742Swollman		}
7522742Swollman	} else {
7532742Swollman		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
7542742Swollman		IEEE80211_ADDR_COPY(wh->i_addr1, da);
7552742Swollman		IEEE80211_ADDR_COPY(wh->i_addr2, sa);
7562742Swollman#ifdef IEEE80211_SUPPORT_MESH
7572742Swollman		if (vap->iv_opmode == IEEE80211_M_MBSS)
7582742Swollman			IEEE80211_ADDR_COPY(wh->i_addr3, sa);
7592742Swollman		else
7602742Swollman#endif
7612742Swollman			IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
7622742Swollman	}
7632742Swollman	*(uint16_t *)&wh->i_dur[0] = 0;
7642742Swollman
7652742Swollman	tap = &ni->ni_tx_ampdu[tid];
7662742Swollman	if (tid != IEEE80211_NONQOS_TID && IEEE80211_AMPDU_RUNNING(tap))
7672742Swollman		m->m_flags |= M_AMPDU_MPDU;
7682742Swollman	else {
7692742Swollman		if (IEEE80211_HAS_SEQ(type & IEEE80211_FC0_TYPE_MASK,
7702742Swollman				      type & IEEE80211_FC0_SUBTYPE_MASK))
7712742Swollman			seqno = ni->ni_txseqs[tid]++;
7722742Swollman		else
7732742Swollman			seqno = 0;
7742742Swollman
7752742Swollman		*(uint16_t *)&wh->i_seq[0] =
7762742Swollman		    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
7772742Swollman		M_SEQNO_SET(m, seqno);
7782742Swollman	}
7792742Swollman
7802742Swollman	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
7812742Swollman		m->m_flags |= M_MCAST;
7822742Swollman#undef WH4
7832742Swollman}
7842742Swollman
7852742Swollman/*
7862742Swollman * Send a management frame to the specified node.  The node pointer
7872742Swollman * must have a reference as the pointer will be passed to the driver
7882742Swollman * and potentially held for a long time.  If the frame is successfully
7892742Swollman * dispatched to the driver, then it is responsible for freeing the
7902742Swollman * reference (and potentially free'ing up any associated storage);
7912742Swollman * otherwise deal with reclaiming any reference (on error).
7922742Swollman */
7932742Swollmanint
7942742Swollmanieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
7952742Swollman	struct ieee80211_bpf_params *params)
7962742Swollman{
7972742Swollman	struct ieee80211vap *vap = ni->ni_vap;
7982742Swollman	struct ieee80211com *ic = ni->ni_ic;
7992742Swollman	struct ieee80211_frame *wh;
8002742Swollman	int ret;
8012742Swollman
8022742Swollman	KASSERT(ni != NULL, ("null node"));
8032742Swollman
8042742Swollman	if (vap->iv_state == IEEE80211_S_CAC) {
8052742Swollman		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
8062742Swollman		    ni, "block %s frame in CAC state",
8072742Swollman			ieee80211_mgt_subtype_name[
8082742Swollman			    (type & IEEE80211_FC0_SUBTYPE_MASK) >>
8092742Swollman				IEEE80211_FC0_SUBTYPE_SHIFT]);
8102742Swollman		vap->iv_stats.is_tx_badstate++;
8112742Swollman		ieee80211_free_node(ni);
8122742Swollman		m_freem(m);
8132742Swollman		return EIO;		/* XXX */
8142742Swollman	}
8152742Swollman
8162742Swollman	M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
8172742Swollman	if (m == NULL) {
8182742Swollman		ieee80211_free_node(ni);
8192742Swollman		return ENOMEM;
8202742Swollman	}
8212742Swollman
8222742Swollman	IEEE80211_TX_LOCK(ic);
8232742Swollman
8242742Swollman	wh = mtod(m, struct ieee80211_frame *);
825	ieee80211_send_setup(ni, m,
826	     IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
827	     vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
828	if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
829		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
830		    "encrypting frame (%s)", __func__);
831		wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
832	}
833	m->m_flags |= M_ENCAP;		/* mark encapsulated */
834
835	KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
836	M_WME_SETAC(m, params->ibp_pri);
837
838#ifdef IEEE80211_DEBUG
839	/* avoid printing too many frames */
840	if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
841	    ieee80211_msg_dumppkts(vap)) {
842		printf("[%s] send %s on channel %u\n",
843		    ether_sprintf(wh->i_addr1),
844		    ieee80211_mgt_subtype_name[
845			(type & IEEE80211_FC0_SUBTYPE_MASK) >>
846				IEEE80211_FC0_SUBTYPE_SHIFT],
847		    ieee80211_chan2ieee(ic, ic->ic_curchan));
848	}
849#endif
850	IEEE80211_NODE_STAT(ni, tx_mgmt);
851
852	ret = ieee80211_raw_output(vap, ni, m, params);
853	IEEE80211_TX_UNLOCK(ic);
854	return (ret);
855}
856
857static void
858ieee80211_nulldata_transmitted(struct ieee80211_node *ni, void *arg,
859    int status)
860{
861	struct ieee80211vap *vap = ni->ni_vap;
862
863	wakeup(vap);
864}
865
866/*
867 * Send a null data frame to the specified node.  If the station
868 * is setup for QoS then a QoS Null Data frame is constructed.
869 * If this is a WDS station then a 4-address frame is constructed.
870 *
871 * NB: the caller is assumed to have setup a node reference
872 *     for use; this is necessary to deal with a race condition
873 *     when probing for inactive stations.  Like ieee80211_mgmt_output
874 *     we must cleanup any node reference on error;  however we
875 *     can safely just unref it as we know it will never be the
876 *     last reference to the node.
877 */
878int
879ieee80211_send_nulldata(struct ieee80211_node *ni)
880{
881	struct ieee80211vap *vap = ni->ni_vap;
882	struct ieee80211com *ic = ni->ni_ic;
883	struct mbuf *m;
884	struct ieee80211_frame *wh;
885	int hdrlen;
886	uint8_t *frm;
887	int ret;
888
889	if (vap->iv_state == IEEE80211_S_CAC) {
890		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
891		    ni, "block %s frame in CAC state", "null data");
892		ieee80211_unref_node(&ni);
893		vap->iv_stats.is_tx_badstate++;
894		return EIO;		/* XXX */
895	}
896
897	if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
898		hdrlen = sizeof(struct ieee80211_qosframe);
899	else
900		hdrlen = sizeof(struct ieee80211_frame);
901	/* NB: only WDS vap's get 4-address frames */
902	if (vap->iv_opmode == IEEE80211_M_WDS)
903		hdrlen += IEEE80211_ADDR_LEN;
904	if (ic->ic_flags & IEEE80211_F_DATAPAD)
905		hdrlen = roundup(hdrlen, sizeof(uint32_t));
906
907	m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
908	if (m == NULL) {
909		/* XXX debug msg */
910		ieee80211_unref_node(&ni);
911		vap->iv_stats.is_tx_nobuf++;
912		return ENOMEM;
913	}
914	KASSERT(M_LEADINGSPACE(m) >= hdrlen,
915	    ("leading space %zd", M_LEADINGSPACE(m)));
916	M_PREPEND(m, hdrlen, M_NOWAIT);
917	if (m == NULL) {
918		/* NB: cannot happen */
919		ieee80211_free_node(ni);
920		return ENOMEM;
921	}
922
923	IEEE80211_TX_LOCK(ic);
924
925	wh = mtod(m, struct ieee80211_frame *);		/* NB: a little lie */
926	if (ni->ni_flags & IEEE80211_NODE_QOS) {
927		const int tid = WME_AC_TO_TID(WME_AC_BE);
928		uint8_t *qos;
929
930		ieee80211_send_setup(ni, m,
931		    IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
932		    tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
933
934		if (vap->iv_opmode == IEEE80211_M_WDS)
935			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
936		else
937			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
938		qos[0] = tid & IEEE80211_QOS_TID;
939		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
940			qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
941		qos[1] = 0;
942	} else {
943		ieee80211_send_setup(ni, m,
944		    IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
945		    IEEE80211_NONQOS_TID,
946		    vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
947	}
948	if (vap->iv_opmode != IEEE80211_M_WDS) {
949		/* NB: power management bit is never sent by an AP */
950		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
951		    vap->iv_opmode != IEEE80211_M_HOSTAP)
952			wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
953	}
954	if ((ic->ic_flags & IEEE80211_F_SCAN) &&
955	    (ni->ni_flags & IEEE80211_NODE_PWR_MGT)) {
956		ieee80211_add_callback(m, ieee80211_nulldata_transmitted,
957		    NULL);
958	}
959	m->m_len = m->m_pkthdr.len = hdrlen;
960	m->m_flags |= M_ENCAP;		/* mark encapsulated */
961
962	M_WME_SETAC(m, WME_AC_BE);
963
964	IEEE80211_NODE_STAT(ni, tx_data);
965
966	IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
967	    "send %snull data frame on channel %u, pwr mgt %s",
968	    ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
969	    ieee80211_chan2ieee(ic, ic->ic_curchan),
970	    wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
971
972	ret = ieee80211_raw_output(vap, ni, m, NULL);
973	IEEE80211_TX_UNLOCK(ic);
974	return (ret);
975}
976
977/*
978 * Assign priority to a frame based on any vlan tag assigned
979 * to the station and/or any Diffserv setting in an IP header.
980 * Finally, if an ACM policy is setup (in station mode) it's
981 * applied.
982 */
983int
984ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
985{
986	const struct ether_header *eh = mtod(m, struct ether_header *);
987	int v_wme_ac, d_wme_ac, ac;
988
989	/*
990	 * Always promote PAE/EAPOL frames to high priority.
991	 */
992	if (eh->ether_type == htons(ETHERTYPE_PAE)) {
993		/* NB: mark so others don't need to check header */
994		m->m_flags |= M_EAPOL;
995		ac = WME_AC_VO;
996		goto done;
997	}
998	/*
999	 * Non-qos traffic goes to BE.
1000	 */
1001	if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
1002		ac = WME_AC_BE;
1003		goto done;
1004	}
1005
1006	/*
1007	 * If node has a vlan tag then all traffic
1008	 * to it must have a matching tag.
1009	 */
1010	v_wme_ac = 0;
1011	if (ni->ni_vlan != 0) {
1012		 if ((m->m_flags & M_VLANTAG) == 0) {
1013			IEEE80211_NODE_STAT(ni, tx_novlantag);
1014			return 1;
1015		}
1016		if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
1017		    EVL_VLANOFTAG(ni->ni_vlan)) {
1018			IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
1019			return 1;
1020		}
1021		/* map vlan priority to AC */
1022		v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
1023	}
1024
1025	/* XXX m_copydata may be too slow for fast path */
1026#ifdef INET
1027	if (eh->ether_type == htons(ETHERTYPE_IP)) {
1028		uint8_t tos;
1029		/*
1030		 * IP frame, map the DSCP bits from the TOS field.
1031		 */
1032		/* NB: ip header may not be in first mbuf */
1033		m_copydata(m, sizeof(struct ether_header) +
1034		    offsetof(struct ip, ip_tos), sizeof(tos), &tos);
1035		tos >>= 5;		/* NB: ECN + low 3 bits of DSCP */
1036		d_wme_ac = TID_TO_WME_AC(tos);
1037	} else {
1038#endif /* INET */
1039#ifdef INET6
1040	if (eh->ether_type == htons(ETHERTYPE_IPV6)) {
1041		uint32_t flow;
1042		uint8_t tos;
1043		/*
1044		 * IPv6 frame, map the DSCP bits from the traffic class field.
1045		 */
1046		m_copydata(m, sizeof(struct ether_header) +
1047		    offsetof(struct ip6_hdr, ip6_flow), sizeof(flow),
1048		    (caddr_t) &flow);
1049		tos = (uint8_t)(ntohl(flow) >> 20);
1050		tos >>= 5;		/* NB: ECN + low 3 bits of DSCP */
1051		d_wme_ac = TID_TO_WME_AC(tos);
1052	} else {
1053#endif /* INET6 */
1054		d_wme_ac = WME_AC_BE;
1055#ifdef INET6
1056	}
1057#endif
1058#ifdef INET
1059	}
1060#endif
1061	/*
1062	 * Use highest priority AC.
1063	 */
1064	if (v_wme_ac > d_wme_ac)
1065		ac = v_wme_ac;
1066	else
1067		ac = d_wme_ac;
1068
1069	/*
1070	 * Apply ACM policy.
1071	 */
1072	if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
1073		static const int acmap[4] = {
1074			WME_AC_BK,	/* WME_AC_BE */
1075			WME_AC_BK,	/* WME_AC_BK */
1076			WME_AC_BE,	/* WME_AC_VI */
1077			WME_AC_VI,	/* WME_AC_VO */
1078		};
1079		struct ieee80211com *ic = ni->ni_ic;
1080
1081		while (ac != WME_AC_BK &&
1082		    ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
1083			ac = acmap[ac];
1084	}
1085done:
1086	M_WME_SETAC(m, ac);
1087	return 0;
1088}
1089
1090/*
1091 * Insure there is sufficient contiguous space to encapsulate the
1092 * 802.11 data frame.  If room isn't already there, arrange for it.
1093 * Drivers and cipher modules assume we have done the necessary work
1094 * and fail rudely if they don't find the space they need.
1095 */
1096struct mbuf *
1097ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
1098	struct ieee80211_key *key, struct mbuf *m)
1099{
1100#define	TO_BE_RECLAIMED	(sizeof(struct ether_header) - sizeof(struct llc))
1101	int needed_space = vap->iv_ic->ic_headroom + hdrsize;
1102
1103	if (key != NULL) {
1104		/* XXX belongs in crypto code? */
1105		needed_space += key->wk_cipher->ic_header;
1106		/* XXX frags */
1107		/*
1108		 * When crypto is being done in the host we must insure
1109		 * the data are writable for the cipher routines; clone
1110		 * a writable mbuf chain.
1111		 * XXX handle SWMIC specially
1112		 */
1113		if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
1114			m = m_unshare(m, M_NOWAIT);
1115			if (m == NULL) {
1116				IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
1117				    "%s: cannot get writable mbuf\n", __func__);
1118				vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
1119				return NULL;
1120			}
1121		}
1122	}
1123	/*
1124	 * We know we are called just before stripping an Ethernet
1125	 * header and prepending an LLC header.  This means we know
1126	 * there will be
1127	 *	sizeof(struct ether_header) - sizeof(struct llc)
1128	 * bytes recovered to which we need additional space for the
1129	 * 802.11 header and any crypto header.
1130	 */
1131	/* XXX check trailing space and copy instead? */
1132	if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
1133		struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
1134		if (n == NULL) {
1135			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
1136			    "%s: cannot expand storage\n", __func__);
1137			vap->iv_stats.is_tx_nobuf++;
1138			m_freem(m);
1139			return NULL;
1140		}
1141		KASSERT(needed_space <= MHLEN,
1142		    ("not enough room, need %u got %d\n", needed_space, MHLEN));
1143		/*
1144		 * Setup new mbuf to have leading space to prepend the
1145		 * 802.11 header and any crypto header bits that are
1146		 * required (the latter are added when the driver calls
1147		 * back to ieee80211_crypto_encap to do crypto encapsulation).
1148		 */
1149		/* NB: must be first 'cuz it clobbers m_data */
1150		m_move_pkthdr(n, m);
1151		n->m_len = 0;			/* NB: m_gethdr does not set */
1152		n->m_data += needed_space;
1153		/*
1154		 * Pull up Ethernet header to create the expected layout.
1155		 * We could use m_pullup but that's overkill (i.e. we don't
1156		 * need the actual data) and it cannot fail so do it inline
1157		 * for speed.
1158		 */
1159		/* NB: struct ether_header is known to be contiguous */
1160		n->m_len += sizeof(struct ether_header);
1161		m->m_len -= sizeof(struct ether_header);
1162		m->m_data += sizeof(struct ether_header);
1163		/*
1164		 * Replace the head of the chain.
1165		 */
1166		n->m_next = m;
1167		m = n;
1168	}
1169	return m;
1170#undef TO_BE_RECLAIMED
1171}
1172
1173/*
1174 * Return the transmit key to use in sending a unicast frame.
1175 * If a unicast key is set we use that.  When no unicast key is set
1176 * we fall back to the default transmit key.
1177 */
1178static __inline struct ieee80211_key *
1179ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
1180	struct ieee80211_node *ni)
1181{
1182	if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
1183		if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1184		    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1185			return NULL;
1186		return &vap->iv_nw_keys[vap->iv_def_txkey];
1187	} else {
1188		return &ni->ni_ucastkey;
1189	}
1190}
1191
1192/*
1193 * Return the transmit key to use in sending a multicast frame.
1194 * Multicast traffic always uses the group key which is installed as
1195 * the default tx key.
1196 */
1197static __inline struct ieee80211_key *
1198ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
1199	struct ieee80211_node *ni)
1200{
1201	if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1202	    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1203		return NULL;
1204	return &vap->iv_nw_keys[vap->iv_def_txkey];
1205}
1206
1207/*
1208 * Encapsulate an outbound data frame.  The mbuf chain is updated.
1209 * If an error is encountered NULL is returned.  The caller is required
1210 * to provide a node reference and pullup the ethernet header in the
1211 * first mbuf.
1212 *
1213 * NB: Packet is assumed to be processed by ieee80211_classify which
1214 *     marked EAPOL frames w/ M_EAPOL.
1215 */
1216struct mbuf *
1217ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
1218    struct mbuf *m)
1219{
1220#define	WH4(wh)	((struct ieee80211_frame_addr4 *)(wh))
1221#define MC01(mc)	((struct ieee80211_meshcntl_ae01 *)mc)
1222	struct ieee80211com *ic = ni->ni_ic;
1223#ifdef IEEE80211_SUPPORT_MESH
1224	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1225	struct ieee80211_meshcntl_ae10 *mc;
1226	struct ieee80211_mesh_route *rt = NULL;
1227	int dir = -1;
1228#endif
1229	struct ether_header eh;
1230	struct ieee80211_frame *wh;
1231	struct ieee80211_key *key;
1232	struct llc *llc;
1233	int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr;
1234	ieee80211_seq seqno;
1235	int meshhdrsize, meshae;
1236	uint8_t *qos;
1237	int is_amsdu = 0;
1238
1239	IEEE80211_TX_LOCK_ASSERT(ic);
1240
1241	/*
1242	 * Copy existing Ethernet header to a safe place.  The
1243	 * rest of the code assumes it's ok to strip it when
1244	 * reorganizing state for the final encapsulation.
1245	 */
1246	KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
1247	ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
1248
1249	/*
1250	 * Insure space for additional headers.  First identify
1251	 * transmit key to use in calculating any buffer adjustments
1252	 * required.  This is also used below to do privacy
1253	 * encapsulation work.  Then calculate the 802.11 header
1254	 * size and any padding required by the driver.
1255	 *
1256	 * Note key may be NULL if we fall back to the default
1257	 * transmit key and that is not set.  In that case the
1258	 * buffer may not be expanded as needed by the cipher
1259	 * routines, but they will/should discard it.
1260	 */
1261	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1262		if (vap->iv_opmode == IEEE80211_M_STA ||
1263		    !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
1264		    (vap->iv_opmode == IEEE80211_M_WDS &&
1265		     (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
1266			key = ieee80211_crypto_getucastkey(vap, ni);
1267		else
1268			key = ieee80211_crypto_getmcastkey(vap, ni);
1269		if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
1270			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
1271			    eh.ether_dhost,
1272			    "no default transmit key (%s) deftxkey %u",
1273			    __func__, vap->iv_def_txkey);
1274			vap->iv_stats.is_tx_nodefkey++;
1275			goto bad;
1276		}
1277	} else
1278		key = NULL;
1279	/*
1280	 * XXX Some ap's don't handle QoS-encapsulated EAPOL
1281	 * frames so suppress use.  This may be an issue if other
1282	 * ap's require all data frames to be QoS-encapsulated
1283	 * once negotiated in which case we'll need to make this
1284	 * configurable.
1285	 * NB: mesh data frames are QoS.
1286	 */
1287	addqos = ((ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) ||
1288	    (vap->iv_opmode == IEEE80211_M_MBSS)) &&
1289	    (m->m_flags & M_EAPOL) == 0;
1290	if (addqos)
1291		hdrsize = sizeof(struct ieee80211_qosframe);
1292	else
1293		hdrsize = sizeof(struct ieee80211_frame);
1294#ifdef IEEE80211_SUPPORT_MESH
1295	if (vap->iv_opmode == IEEE80211_M_MBSS) {
1296		/*
1297		 * Mesh data frames are encapsulated according to the
1298		 * rules of Section 11B.8.5 (p.139 of D3.0 spec).
1299		 * o Group Addressed data (aka multicast) originating
1300		 *   at the local sta are sent w/ 3-address format and
1301		 *   address extension mode 00
1302		 * o Individually Addressed data (aka unicast) originating
1303		 *   at the local sta are sent w/ 4-address format and
1304		 *   address extension mode 00
1305		 * o Group Addressed data forwarded from a non-mesh sta are
1306		 *   sent w/ 3-address format and address extension mode 01
1307		 * o Individually Address data from another sta are sent
1308		 *   w/ 4-address format and address extension mode 10
1309		 */
1310		is4addr = 0;		/* NB: don't use, disable */
1311		if (!IEEE80211_IS_MULTICAST(eh.ether_dhost)) {
1312			rt = ieee80211_mesh_rt_find(vap, eh.ether_dhost);
1313			KASSERT(rt != NULL, ("route is NULL"));
1314			dir = IEEE80211_FC1_DIR_DSTODS;
1315			hdrsize += IEEE80211_ADDR_LEN;
1316			if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
1317				if (IEEE80211_ADDR_EQ(rt->rt_mesh_gate,
1318				    vap->iv_myaddr)) {
1319					IEEE80211_NOTE_MAC(vap,
1320					    IEEE80211_MSG_MESH,
1321					    eh.ether_dhost,
1322					    "%s", "trying to send to ourself");
1323					goto bad;
1324				}
1325				meshae = IEEE80211_MESH_AE_10;
1326				meshhdrsize =
1327				    sizeof(struct ieee80211_meshcntl_ae10);
1328			} else {
1329				meshae = IEEE80211_MESH_AE_00;
1330				meshhdrsize =
1331				    sizeof(struct ieee80211_meshcntl);
1332			}
1333		} else {
1334			dir = IEEE80211_FC1_DIR_FROMDS;
1335			if (!IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) {
1336				/* proxy group */
1337				meshae = IEEE80211_MESH_AE_01;
1338				meshhdrsize =
1339				    sizeof(struct ieee80211_meshcntl_ae01);
1340			} else {
1341				/* group */
1342				meshae = IEEE80211_MESH_AE_00;
1343				meshhdrsize = sizeof(struct ieee80211_meshcntl);
1344			}
1345		}
1346	} else {
1347#endif
1348		/*
1349		 * 4-address frames need to be generated for:
1350		 * o packets sent through a WDS vap (IEEE80211_M_WDS)
1351		 * o packets sent through a vap marked for relaying
1352		 *   (e.g. a station operating with dynamic WDS)
1353		 */
1354		is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
1355		    ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
1356		     !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
1357		if (is4addr)
1358			hdrsize += IEEE80211_ADDR_LEN;
1359		meshhdrsize = meshae = 0;
1360#ifdef IEEE80211_SUPPORT_MESH
1361	}
1362#endif
1363	/*
1364	 * Honor driver DATAPAD requirement.
1365	 */
1366	if (ic->ic_flags & IEEE80211_F_DATAPAD)
1367		hdrspace = roundup(hdrsize, sizeof(uint32_t));
1368	else
1369		hdrspace = hdrsize;
1370
1371	if (__predict_true((m->m_flags & M_FF) == 0)) {
1372		/*
1373		 * Normal frame.
1374		 */
1375		m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m);
1376		if (m == NULL) {
1377			/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
1378			goto bad;
1379		}
1380		/* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
1381		m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
1382		llc = mtod(m, struct llc *);
1383		llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1384		llc->llc_control = LLC_UI;
1385		llc->llc_snap.org_code[0] = 0;
1386		llc->llc_snap.org_code[1] = 0;
1387		llc->llc_snap.org_code[2] = 0;
1388		llc->llc_snap.ether_type = eh.ether_type;
1389	} else {
1390#ifdef IEEE80211_SUPPORT_SUPERG
1391		/*
1392		 * Aggregated frame.  Check if it's for AMSDU or FF.
1393		 *
1394		 * XXX TODO: IEEE80211_NODE_AMSDU* isn't implemented
1395		 * anywhere for some reason.  But, since 11n requires
1396		 * AMSDU RX, we can just assume "11n" == "AMSDU".
1397		 */
1398		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, "%s: called; M_FF\n", __func__);
1399		if (ieee80211_amsdu_tx_ok(ni)) {
1400			m = ieee80211_amsdu_encap(vap, m, hdrspace + meshhdrsize, key);
1401			is_amsdu = 1;
1402		} else {
1403			m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key);
1404		}
1405		if (m == NULL)
1406#endif
1407			goto bad;
1408	}
1409	datalen = m->m_pkthdr.len;		/* NB: w/o 802.11 header */
1410
1411	M_PREPEND(m, hdrspace + meshhdrsize, M_NOWAIT);
1412	if (m == NULL) {
1413		vap->iv_stats.is_tx_nobuf++;
1414		goto bad;
1415	}
1416	wh = mtod(m, struct ieee80211_frame *);
1417	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1418	*(uint16_t *)wh->i_dur = 0;
1419	qos = NULL;	/* NB: quiet compiler */
1420	if (is4addr) {
1421		wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1422		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1423		IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1424		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1425		IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1426	} else switch (vap->iv_opmode) {
1427	case IEEE80211_M_STA:
1428		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1429		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
1430		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1431		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1432		break;
1433	case IEEE80211_M_IBSS:
1434	case IEEE80211_M_AHDEMO:
1435		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1436		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1437		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1438		/*
1439		 * NB: always use the bssid from iv_bss as the
1440		 *     neighbor's may be stale after an ibss merge
1441		 */
1442		IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
1443		break;
1444	case IEEE80211_M_HOSTAP:
1445		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1446		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1447		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
1448		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1449		break;
1450#ifdef IEEE80211_SUPPORT_MESH
1451	case IEEE80211_M_MBSS:
1452		/* NB: offset by hdrspace to deal with DATAPAD */
1453		mc = (struct ieee80211_meshcntl_ae10 *)
1454		     (mtod(m, uint8_t *) + hdrspace);
1455		wh->i_fc[1] = dir;
1456		switch (meshae) {
1457		case IEEE80211_MESH_AE_00:	/* no proxy */
1458			mc->mc_flags = 0;
1459			if (dir == IEEE80211_FC1_DIR_DSTODS) { /* ucast */
1460				IEEE80211_ADDR_COPY(wh->i_addr1,
1461				    ni->ni_macaddr);
1462				IEEE80211_ADDR_COPY(wh->i_addr2,
1463				    vap->iv_myaddr);
1464				IEEE80211_ADDR_COPY(wh->i_addr3,
1465				    eh.ether_dhost);
1466				IEEE80211_ADDR_COPY(WH4(wh)->i_addr4,
1467				    eh.ether_shost);
1468				qos =((struct ieee80211_qosframe_addr4 *)
1469				    wh)->i_qos;
1470			} else if (dir == IEEE80211_FC1_DIR_FROMDS) {
1471				 /* mcast */
1472				IEEE80211_ADDR_COPY(wh->i_addr1,
1473				    eh.ether_dhost);
1474				IEEE80211_ADDR_COPY(wh->i_addr2,
1475				    vap->iv_myaddr);
1476				IEEE80211_ADDR_COPY(wh->i_addr3,
1477				    eh.ether_shost);
1478				qos = ((struct ieee80211_qosframe *)
1479				    wh)->i_qos;
1480			}
1481			break;
1482		case IEEE80211_MESH_AE_01:	/* mcast, proxy */
1483			wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1484			IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1485			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1486			IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr);
1487			mc->mc_flags = 1;
1488			IEEE80211_ADDR_COPY(MC01(mc)->mc_addr4,
1489			    eh.ether_shost);
1490			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1491			break;
1492		case IEEE80211_MESH_AE_10:	/* ucast, proxy */
1493			KASSERT(rt != NULL, ("route is NULL"));
1494			IEEE80211_ADDR_COPY(wh->i_addr1, rt->rt_nexthop);
1495			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1496			IEEE80211_ADDR_COPY(wh->i_addr3, rt->rt_mesh_gate);
1497			IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr);
1498			mc->mc_flags = IEEE80211_MESH_AE_10;
1499			IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_dhost);
1500			IEEE80211_ADDR_COPY(mc->mc_addr6, eh.ether_shost);
1501			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1502			break;
1503		default:
1504			KASSERT(0, ("meshae %d", meshae));
1505			break;
1506		}
1507		mc->mc_ttl = ms->ms_ttl;
1508		ms->ms_seq++;
1509		le32enc(mc->mc_seq, ms->ms_seq);
1510		break;
1511#endif
1512	case IEEE80211_M_WDS:		/* NB: is4addr should always be true */
1513	default:
1514		goto bad;
1515	}
1516	if (m->m_flags & M_MORE_DATA)
1517		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
1518	if (addqos) {
1519		int ac, tid;
1520
1521		if (is4addr) {
1522			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1523		/* NB: mesh case handled earlier */
1524		} else if (vap->iv_opmode != IEEE80211_M_MBSS)
1525			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1526		ac = M_WME_GETAC(m);
1527		/* map from access class/queue to 11e header priorty value */
1528		tid = WME_AC_TO_TID(ac);
1529		qos[0] = tid & IEEE80211_QOS_TID;
1530		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
1531			qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1532#ifdef IEEE80211_SUPPORT_MESH
1533		if (vap->iv_opmode == IEEE80211_M_MBSS)
1534			qos[1] = IEEE80211_QOS_MC;
1535		else
1536#endif
1537			qos[1] = 0;
1538		wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
1539
1540		/*
1541		 * If this is an A-MSDU then ensure we set the
1542		 * relevant field.
1543		 */
1544		if (is_amsdu)
1545			qos[0] |= IEEE80211_QOS_AMSDU;
1546
1547		if ((m->m_flags & M_AMPDU_MPDU) == 0) {
1548			/*
1549			 * NB: don't assign a sequence # to potential
1550			 * aggregates; we expect this happens at the
1551			 * point the frame comes off any aggregation q
1552			 * as otherwise we may introduce holes in the
1553			 * BA sequence space and/or make window accouting
1554			 * more difficult.
1555			 *
1556			 * XXX may want to control this with a driver
1557			 * capability; this may also change when we pull
1558			 * aggregation up into net80211
1559			 */
1560			seqno = ni->ni_txseqs[tid]++;
1561			*(uint16_t *)wh->i_seq =
1562			    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1563			M_SEQNO_SET(m, seqno);
1564		}
1565	} else {
1566		seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
1567		*(uint16_t *)wh->i_seq =
1568		    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1569		M_SEQNO_SET(m, seqno);
1570
1571		/*
1572		 * XXX TODO: we shouldn't allow EAPOL, etc that would
1573		 * be forced to be non-QoS traffic to be A-MSDU encapsulated.
1574		 */
1575		if (is_amsdu)
1576			printf("%s: XXX ERROR: is_amsdu set; not QoS!\n",
1577			    __func__);
1578	}
1579
1580
1581	/* check if xmit fragmentation is required */
1582	txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
1583	    !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1584	    (vap->iv_caps & IEEE80211_C_TXFRAG) &&
1585	    (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
1586	if (key != NULL) {
1587		/*
1588		 * IEEE 802.1X: send EAPOL frames always in the clear.
1589		 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
1590		 */
1591		if ((m->m_flags & M_EAPOL) == 0 ||
1592		    ((vap->iv_flags & IEEE80211_F_WPA) &&
1593		     (vap->iv_opmode == IEEE80211_M_STA ?
1594		      !IEEE80211_KEY_UNDEFINED(key) :
1595		      !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
1596			wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
1597			if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1598				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1599				    eh.ether_dhost,
1600				    "%s", "enmic failed, discard frame");
1601				vap->iv_stats.is_crypto_enmicfail++;
1602				goto bad;
1603			}
1604		}
1605	}
1606	if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1607	    key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
1608		goto bad;
1609
1610	m->m_flags |= M_ENCAP;		/* mark encapsulated */
1611
1612	IEEE80211_NODE_STAT(ni, tx_data);
1613	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1614		IEEE80211_NODE_STAT(ni, tx_mcast);
1615		m->m_flags |= M_MCAST;
1616	} else
1617		IEEE80211_NODE_STAT(ni, tx_ucast);
1618	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
1619
1620	return m;
1621bad:
1622	if (m != NULL)
1623		m_freem(m);
1624	return NULL;
1625#undef WH4
1626#undef MC01
1627}
1628
1629void
1630ieee80211_free_mbuf(struct mbuf *m)
1631{
1632	struct mbuf *next;
1633
1634	if (m == NULL)
1635		return;
1636
1637	do {
1638		next = m->m_nextpkt;
1639		m->m_nextpkt = NULL;
1640		m_freem(m);
1641	} while ((m = next) != NULL);
1642}
1643
1644/*
1645 * Fragment the frame according to the specified mtu.
1646 * The size of the 802.11 header (w/o padding) is provided
1647 * so we don't need to recalculate it.  We create a new
1648 * mbuf for each fragment and chain it through m_nextpkt;
1649 * we might be able to optimize this by reusing the original
1650 * packet's mbufs but that is significantly more complicated.
1651 */
1652static int
1653ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
1654	u_int hdrsize, u_int ciphdrsize, u_int mtu)
1655{
1656	struct ieee80211com *ic = vap->iv_ic;
1657	struct ieee80211_frame *wh, *whf;
1658	struct mbuf *m, *prev;
1659	u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
1660	u_int hdrspace;
1661
1662	KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
1663	KASSERT(m0->m_pkthdr.len > mtu,
1664		("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
1665
1666	/*
1667	 * Honor driver DATAPAD requirement.
1668	 */
1669	if (ic->ic_flags & IEEE80211_F_DATAPAD)
1670		hdrspace = roundup(hdrsize, sizeof(uint32_t));
1671	else
1672		hdrspace = hdrsize;
1673
1674	wh = mtod(m0, struct ieee80211_frame *);
1675	/* NB: mark the first frag; it will be propagated below */
1676	wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
1677	totalhdrsize = hdrspace + ciphdrsize;
1678	fragno = 1;
1679	off = mtu - ciphdrsize;
1680	remainder = m0->m_pkthdr.len - off;
1681	prev = m0;
1682	do {
1683		fragsize = totalhdrsize + remainder;
1684		if (fragsize > mtu)
1685			fragsize = mtu;
1686		/* XXX fragsize can be >2048! */
1687		KASSERT(fragsize < MCLBYTES,
1688			("fragment size %u too big!", fragsize));
1689		if (fragsize > MHLEN)
1690			m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1691		else
1692			m = m_gethdr(M_NOWAIT, MT_DATA);
1693		if (m == NULL)
1694			goto bad;
1695		/* leave room to prepend any cipher header */
1696		m_align(m, fragsize - ciphdrsize);
1697
1698		/*
1699		 * Form the header in the fragment.  Note that since
1700		 * we mark the first fragment with the MORE_FRAG bit
1701		 * it automatically is propagated to each fragment; we
1702		 * need only clear it on the last fragment (done below).
1703		 * NB: frag 1+ dont have Mesh Control field present.
1704		 */
1705		whf = mtod(m, struct ieee80211_frame *);
1706		memcpy(whf, wh, hdrsize);
1707#ifdef IEEE80211_SUPPORT_MESH
1708		if (vap->iv_opmode == IEEE80211_M_MBSS) {
1709			if (IEEE80211_IS_DSTODS(wh))
1710				((struct ieee80211_qosframe_addr4 *)
1711				    whf)->i_qos[1] &= ~IEEE80211_QOS_MC;
1712			else
1713				((struct ieee80211_qosframe *)
1714				    whf)->i_qos[1] &= ~IEEE80211_QOS_MC;
1715		}
1716#endif
1717		*(uint16_t *)&whf->i_seq[0] |= htole16(
1718			(fragno & IEEE80211_SEQ_FRAG_MASK) <<
1719				IEEE80211_SEQ_FRAG_SHIFT);
1720		fragno++;
1721
1722		payload = fragsize - totalhdrsize;
1723		/* NB: destination is known to be contiguous */
1724
1725		m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrspace);
1726		m->m_len = hdrspace + payload;
1727		m->m_pkthdr.len = hdrspace + payload;
1728		m->m_flags |= M_FRAG;
1729
1730		/* chain up the fragment */
1731		prev->m_nextpkt = m;
1732		prev = m;
1733
1734		/* deduct fragment just formed */
1735		remainder -= payload;
1736		off += payload;
1737	} while (remainder != 0);
1738
1739	/* set the last fragment */
1740	m->m_flags |= M_LASTFRAG;
1741	whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
1742
1743	/* strip first mbuf now that everything has been copied */
1744	m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
1745	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
1746
1747	vap->iv_stats.is_tx_fragframes++;
1748	vap->iv_stats.is_tx_frags += fragno-1;
1749
1750	return 1;
1751bad:
1752	/* reclaim fragments but leave original frame for caller to free */
1753	ieee80211_free_mbuf(m0->m_nextpkt);
1754	m0->m_nextpkt = NULL;
1755	return 0;
1756}
1757
1758/*
1759 * Add a supported rates element id to a frame.
1760 */
1761uint8_t *
1762ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
1763{
1764	int nrates;
1765
1766	*frm++ = IEEE80211_ELEMID_RATES;
1767	nrates = rs->rs_nrates;
1768	if (nrates > IEEE80211_RATE_SIZE)
1769		nrates = IEEE80211_RATE_SIZE;
1770	*frm++ = nrates;
1771	memcpy(frm, rs->rs_rates, nrates);
1772	return frm + nrates;
1773}
1774
1775/*
1776 * Add an extended supported rates element id to a frame.
1777 */
1778uint8_t *
1779ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
1780{
1781	/*
1782	 * Add an extended supported rates element if operating in 11g mode.
1783	 */
1784	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
1785		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
1786		*frm++ = IEEE80211_ELEMID_XRATES;
1787		*frm++ = nrates;
1788		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
1789		frm += nrates;
1790	}
1791	return frm;
1792}
1793
1794/*
1795 * Add an ssid element to a frame.
1796 */
1797uint8_t *
1798ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
1799{
1800	*frm++ = IEEE80211_ELEMID_SSID;
1801	*frm++ = len;
1802	memcpy(frm, ssid, len);
1803	return frm + len;
1804}
1805
1806/*
1807 * Add an erp element to a frame.
1808 */
1809static uint8_t *
1810ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic)
1811{
1812	uint8_t erp;
1813
1814	*frm++ = IEEE80211_ELEMID_ERP;
1815	*frm++ = 1;
1816	erp = 0;
1817	if (ic->ic_nonerpsta != 0)
1818		erp |= IEEE80211_ERP_NON_ERP_PRESENT;
1819	if (ic->ic_flags & IEEE80211_F_USEPROT)
1820		erp |= IEEE80211_ERP_USE_PROTECTION;
1821	if (ic->ic_flags & IEEE80211_F_USEBARKER)
1822		erp |= IEEE80211_ERP_LONG_PREAMBLE;
1823	*frm++ = erp;
1824	return frm;
1825}
1826
1827/*
1828 * Add a CFParams element to a frame.
1829 */
1830static uint8_t *
1831ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
1832{
1833#define	ADDSHORT(frm, v) do {	\
1834	le16enc(frm, v);	\
1835	frm += 2;		\
1836} while (0)
1837	*frm++ = IEEE80211_ELEMID_CFPARMS;
1838	*frm++ = 6;
1839	*frm++ = 0;		/* CFP count */
1840	*frm++ = 2;		/* CFP period */
1841	ADDSHORT(frm, 0);	/* CFP MaxDuration (TU) */
1842	ADDSHORT(frm, 0);	/* CFP CurRemaining (TU) */
1843	return frm;
1844#undef ADDSHORT
1845}
1846
1847static __inline uint8_t *
1848add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
1849{
1850	memcpy(frm, ie->ie_data, ie->ie_len);
1851	return frm + ie->ie_len;
1852}
1853
1854static __inline uint8_t *
1855add_ie(uint8_t *frm, const uint8_t *ie)
1856{
1857	memcpy(frm, ie, 2 + ie[1]);
1858	return frm + 2 + ie[1];
1859}
1860
1861#define	WME_OUI_BYTES		0x00, 0x50, 0xf2
1862/*
1863 * Add a WME information element to a frame.
1864 */
1865uint8_t *
1866ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme)
1867{
1868	static const struct ieee80211_wme_info info = {
1869		.wme_id		= IEEE80211_ELEMID_VENDOR,
1870		.wme_len	= sizeof(struct ieee80211_wme_info) - 2,
1871		.wme_oui	= { WME_OUI_BYTES },
1872		.wme_type	= WME_OUI_TYPE,
1873		.wme_subtype	= WME_INFO_OUI_SUBTYPE,
1874		.wme_version	= WME_VERSION,
1875		.wme_info	= 0,
1876	};
1877	memcpy(frm, &info, sizeof(info));
1878	return frm + sizeof(info);
1879}
1880
1881/*
1882 * Add a WME parameters element to a frame.
1883 */
1884static uint8_t *
1885ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme)
1886{
1887#define	SM(_v, _f)	(((_v) << _f##_S) & _f)
1888#define	ADDSHORT(frm, v) do {	\
1889	le16enc(frm, v);	\
1890	frm += 2;		\
1891} while (0)
1892	/* NB: this works 'cuz a param has an info at the front */
1893	static const struct ieee80211_wme_info param = {
1894		.wme_id		= IEEE80211_ELEMID_VENDOR,
1895		.wme_len	= sizeof(struct ieee80211_wme_param) - 2,
1896		.wme_oui	= { WME_OUI_BYTES },
1897		.wme_type	= WME_OUI_TYPE,
1898		.wme_subtype	= WME_PARAM_OUI_SUBTYPE,
1899		.wme_version	= WME_VERSION,
1900	};
1901	int i;
1902
1903	memcpy(frm, &param, sizeof(param));
1904	frm += __offsetof(struct ieee80211_wme_info, wme_info);
1905	*frm++ = wme->wme_bssChanParams.cap_info;	/* AC info */
1906	*frm++ = 0;					/* reserved field */
1907	for (i = 0; i < WME_NUM_AC; i++) {
1908		const struct wmeParams *ac =
1909		       &wme->wme_bssChanParams.cap_wmeParams[i];
1910		*frm++ = SM(i, WME_PARAM_ACI)
1911		       | SM(ac->wmep_acm, WME_PARAM_ACM)
1912		       | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
1913		       ;
1914		*frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
1915		       | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
1916		       ;
1917		ADDSHORT(frm, ac->wmep_txopLimit);
1918	}
1919	return frm;
1920#undef SM
1921#undef ADDSHORT
1922}
1923#undef WME_OUI_BYTES
1924
1925/*
1926 * Add an 11h Power Constraint element to a frame.
1927 */
1928static uint8_t *
1929ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
1930{
1931	const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
1932	/* XXX per-vap tx power limit? */
1933	int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
1934
1935	frm[0] = IEEE80211_ELEMID_PWRCNSTR;
1936	frm[1] = 1;
1937	frm[2] = c->ic_maxregpower > limit ?  c->ic_maxregpower - limit : 0;
1938	return frm + 3;
1939}
1940
1941/*
1942 * Add an 11h Power Capability element to a frame.
1943 */
1944static uint8_t *
1945ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
1946{
1947	frm[0] = IEEE80211_ELEMID_PWRCAP;
1948	frm[1] = 2;
1949	frm[2] = c->ic_minpower;
1950	frm[3] = c->ic_maxpower;
1951	return frm + 4;
1952}
1953
1954/*
1955 * Add an 11h Supported Channels element to a frame.
1956 */
1957static uint8_t *
1958ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
1959{
1960	static const int ielen = 26;
1961
1962	frm[0] = IEEE80211_ELEMID_SUPPCHAN;
1963	frm[1] = ielen;
1964	/* XXX not correct */
1965	memcpy(frm+2, ic->ic_chan_avail, ielen);
1966	return frm + 2 + ielen;
1967}
1968
1969/*
1970 * Add an 11h Quiet time element to a frame.
1971 */
1972static uint8_t *
1973ieee80211_add_quiet(uint8_t *frm, struct ieee80211vap *vap)
1974{
1975	struct ieee80211_quiet_ie *quiet = (struct ieee80211_quiet_ie *) frm;
1976
1977	quiet->quiet_ie = IEEE80211_ELEMID_QUIET;
1978	quiet->len = 6;
1979	if (vap->iv_quiet_count_value == 1)
1980		vap->iv_quiet_count_value = vap->iv_quiet_count;
1981	else if (vap->iv_quiet_count_value > 1)
1982		vap->iv_quiet_count_value--;
1983
1984	if (vap->iv_quiet_count_value == 0) {
1985		/* value 0 is reserved as per 802.11h standerd */
1986		vap->iv_quiet_count_value = 1;
1987	}
1988
1989	quiet->tbttcount = vap->iv_quiet_count_value;
1990	quiet->period = vap->iv_quiet_period;
1991	quiet->duration = htole16(vap->iv_quiet_duration);
1992	quiet->offset = htole16(vap->iv_quiet_offset);
1993	return frm + sizeof(*quiet);
1994}
1995
1996/*
1997 * Add an 11h Channel Switch Announcement element to a frame.
1998 * Note that we use the per-vap CSA count to adjust the global
1999 * counter so we can use this routine to form probe response
2000 * frames and get the current count.
2001 */
2002static uint8_t *
2003ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
2004{
2005	struct ieee80211com *ic = vap->iv_ic;
2006	struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
2007
2008	csa->csa_ie = IEEE80211_ELEMID_CSA;
2009	csa->csa_len = 3;
2010	csa->csa_mode = 1;		/* XXX force quiet on channel */
2011	csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
2012	csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
2013	return frm + sizeof(*csa);
2014}
2015
2016/*
2017 * Add an 11h country information element to a frame.
2018 */
2019static uint8_t *
2020ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
2021{
2022
2023	if (ic->ic_countryie == NULL ||
2024	    ic->ic_countryie_chan != ic->ic_bsschan) {
2025		/*
2026		 * Handle lazy construction of ie.  This is done on
2027		 * first use and after a channel change that requires
2028		 * re-calculation.
2029		 */
2030		if (ic->ic_countryie != NULL)
2031			IEEE80211_FREE(ic->ic_countryie, M_80211_NODE_IE);
2032		ic->ic_countryie = ieee80211_alloc_countryie(ic);
2033		if (ic->ic_countryie == NULL)
2034			return frm;
2035		ic->ic_countryie_chan = ic->ic_bsschan;
2036	}
2037	return add_appie(frm, ic->ic_countryie);
2038}
2039
2040uint8_t *
2041ieee80211_add_wpa(uint8_t *frm, const struct ieee80211vap *vap)
2042{
2043	if (vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL)
2044		return (add_ie(frm, vap->iv_wpa_ie));
2045	else {
2046		/* XXX else complain? */
2047		return (frm);
2048	}
2049}
2050
2051uint8_t *
2052ieee80211_add_rsn(uint8_t *frm, const struct ieee80211vap *vap)
2053{
2054	if (vap->iv_flags & IEEE80211_F_WPA2 && vap->iv_rsn_ie != NULL)
2055		return (add_ie(frm, vap->iv_rsn_ie));
2056	else {
2057		/* XXX else complain? */
2058		return (frm);
2059	}
2060}
2061
2062uint8_t *
2063ieee80211_add_qos(uint8_t *frm, const struct ieee80211_node *ni)
2064{
2065	if (ni->ni_flags & IEEE80211_NODE_QOS) {
2066		*frm++ = IEEE80211_ELEMID_QOS;
2067		*frm++ = 1;
2068		*frm++ = 0;
2069	}
2070
2071	return (frm);
2072}
2073
2074/*
2075 * Send a probe request frame with the specified ssid
2076 * and any optional information element data.
2077 */
2078int
2079ieee80211_send_probereq(struct ieee80211_node *ni,
2080	const uint8_t sa[IEEE80211_ADDR_LEN],
2081	const uint8_t da[IEEE80211_ADDR_LEN],
2082	const uint8_t bssid[IEEE80211_ADDR_LEN],
2083	const uint8_t *ssid, size_t ssidlen)
2084{
2085	struct ieee80211vap *vap = ni->ni_vap;
2086	struct ieee80211com *ic = ni->ni_ic;
2087	const struct ieee80211_txparam *tp;
2088	struct ieee80211_bpf_params params;
2089	struct ieee80211_frame *wh;
2090	const struct ieee80211_rateset *rs;
2091	struct mbuf *m;
2092	uint8_t *frm;
2093	int ret;
2094
2095	if (vap->iv_state == IEEE80211_S_CAC) {
2096		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
2097		    "block %s frame in CAC state", "probe request");
2098		vap->iv_stats.is_tx_badstate++;
2099		return EIO;		/* XXX */
2100	}
2101
2102	/*
2103	 * Hold a reference on the node so it doesn't go away until after
2104	 * the xmit is complete all the way in the driver.  On error we
2105	 * will remove our reference.
2106	 */
2107	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2108		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2109		__func__, __LINE__,
2110		ni, ether_sprintf(ni->ni_macaddr),
2111		ieee80211_node_refcnt(ni)+1);
2112	ieee80211_ref_node(ni);
2113
2114	/*
2115	 * prreq frame format
2116	 *	[tlv] ssid
2117	 *	[tlv] supported rates
2118	 *	[tlv] RSN (optional)
2119	 *	[tlv] extended supported rates
2120	 *	[tlv] WPA (optional)
2121	 *	[tlv] user-specified ie's
2122	 */
2123	m = ieee80211_getmgtframe(&frm,
2124		 ic->ic_headroom + sizeof(struct ieee80211_frame),
2125	       	 2 + IEEE80211_NWID_LEN
2126	       + 2 + IEEE80211_RATE_SIZE
2127	       + sizeof(struct ieee80211_ie_wpa)
2128	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2129	       + sizeof(struct ieee80211_ie_wpa)
2130	       + (vap->iv_appie_probereq != NULL ?
2131		   vap->iv_appie_probereq->ie_len : 0)
2132	);
2133	if (m == NULL) {
2134		vap->iv_stats.is_tx_nobuf++;
2135		ieee80211_free_node(ni);
2136		return ENOMEM;
2137	}
2138
2139	frm = ieee80211_add_ssid(frm, ssid, ssidlen);
2140	rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2141	frm = ieee80211_add_rates(frm, rs);
2142	frm = ieee80211_add_rsn(frm, vap);
2143	frm = ieee80211_add_xrates(frm, rs);
2144	frm = ieee80211_add_wpa(frm, vap);
2145	if (vap->iv_appie_probereq != NULL)
2146		frm = add_appie(frm, vap->iv_appie_probereq);
2147	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2148
2149	KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
2150	    ("leading space %zd", M_LEADINGSPACE(m)));
2151	M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
2152	if (m == NULL) {
2153		/* NB: cannot happen */
2154		ieee80211_free_node(ni);
2155		return ENOMEM;
2156	}
2157
2158	IEEE80211_TX_LOCK(ic);
2159	wh = mtod(m, struct ieee80211_frame *);
2160	ieee80211_send_setup(ni, m,
2161	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
2162	     IEEE80211_NONQOS_TID, sa, da, bssid);
2163	/* XXX power management? */
2164	m->m_flags |= M_ENCAP;		/* mark encapsulated */
2165
2166	M_WME_SETAC(m, WME_AC_BE);
2167
2168	IEEE80211_NODE_STAT(ni, tx_probereq);
2169	IEEE80211_NODE_STAT(ni, tx_mgmt);
2170
2171	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2172	    "send probe req on channel %u bssid %s ssid \"%.*s\"\n",
2173	    ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(bssid),
2174	    ssidlen, ssid);
2175
2176	memset(&params, 0, sizeof(params));
2177	params.ibp_pri = M_WME_GETAC(m);
2178	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2179	params.ibp_rate0 = tp->mgmtrate;
2180	if (IEEE80211_IS_MULTICAST(da)) {
2181		params.ibp_flags |= IEEE80211_BPF_NOACK;
2182		params.ibp_try0 = 1;
2183	} else
2184		params.ibp_try0 = tp->maxretry;
2185	params.ibp_power = ni->ni_txpower;
2186	ret = ieee80211_raw_output(vap, ni, m, &params);
2187	IEEE80211_TX_UNLOCK(ic);
2188	return (ret);
2189}
2190
2191/*
2192 * Calculate capability information for mgt frames.
2193 */
2194uint16_t
2195ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
2196{
2197	struct ieee80211com *ic = vap->iv_ic;
2198	uint16_t capinfo;
2199
2200	KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
2201
2202	if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2203		capinfo = IEEE80211_CAPINFO_ESS;
2204	else if (vap->iv_opmode == IEEE80211_M_IBSS)
2205		capinfo = IEEE80211_CAPINFO_IBSS;
2206	else
2207		capinfo = 0;
2208	if (vap->iv_flags & IEEE80211_F_PRIVACY)
2209		capinfo |= IEEE80211_CAPINFO_PRIVACY;
2210	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2211	    IEEE80211_IS_CHAN_2GHZ(chan))
2212		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2213	if (ic->ic_flags & IEEE80211_F_SHSLOT)
2214		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2215	if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
2216		capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2217	return capinfo;
2218}
2219
2220/*
2221 * Send a management frame.  The node is for the destination (or ic_bss
2222 * when in station mode).  Nodes other than ic_bss have their reference
2223 * count bumped to reflect our use for an indeterminant time.
2224 */
2225int
2226ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
2227{
2228#define	HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
2229#define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2230	struct ieee80211vap *vap = ni->ni_vap;
2231	struct ieee80211com *ic = ni->ni_ic;
2232	struct ieee80211_node *bss = vap->iv_bss;
2233	struct ieee80211_bpf_params params;
2234	struct mbuf *m;
2235	uint8_t *frm;
2236	uint16_t capinfo;
2237	int has_challenge, is_shared_key, ret, status;
2238
2239	KASSERT(ni != NULL, ("null node"));
2240
2241	/*
2242	 * Hold a reference on the node so it doesn't go away until after
2243	 * the xmit is complete all the way in the driver.  On error we
2244	 * will remove our reference.
2245	 */
2246	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2247		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2248		__func__, __LINE__,
2249		ni, ether_sprintf(ni->ni_macaddr),
2250		ieee80211_node_refcnt(ni)+1);
2251	ieee80211_ref_node(ni);
2252
2253	memset(&params, 0, sizeof(params));
2254	switch (type) {
2255
2256	case IEEE80211_FC0_SUBTYPE_AUTH:
2257		status = arg >> 16;
2258		arg &= 0xffff;
2259		has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
2260		    arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
2261		    ni->ni_challenge != NULL);
2262
2263		/*
2264		 * Deduce whether we're doing open authentication or
2265		 * shared key authentication.  We do the latter if
2266		 * we're in the middle of a shared key authentication
2267		 * handshake or if we're initiating an authentication
2268		 * request and configured to use shared key.
2269		 */
2270		is_shared_key = has_challenge ||
2271		     arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
2272		     (arg == IEEE80211_AUTH_SHARED_REQUEST &&
2273		      bss->ni_authmode == IEEE80211_AUTH_SHARED);
2274
2275		m = ieee80211_getmgtframe(&frm,
2276			  ic->ic_headroom + sizeof(struct ieee80211_frame),
2277			  3 * sizeof(uint16_t)
2278			+ (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
2279				sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)
2280		);
2281		if (m == NULL)
2282			senderr(ENOMEM, is_tx_nobuf);
2283
2284		((uint16_t *)frm)[0] =
2285		    (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
2286		                    : htole16(IEEE80211_AUTH_ALG_OPEN);
2287		((uint16_t *)frm)[1] = htole16(arg);	/* sequence number */
2288		((uint16_t *)frm)[2] = htole16(status);/* status */
2289
2290		if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
2291			((uint16_t *)frm)[3] =
2292			    htole16((IEEE80211_CHALLENGE_LEN << 8) |
2293			    IEEE80211_ELEMID_CHALLENGE);
2294			memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
2295			    IEEE80211_CHALLENGE_LEN);
2296			m->m_pkthdr.len = m->m_len =
2297				4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
2298			if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
2299				IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2300				    "request encrypt frame (%s)", __func__);
2301				/* mark frame for encryption */
2302				params.ibp_flags |= IEEE80211_BPF_CRYPTO;
2303			}
2304		} else
2305			m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
2306
2307		/* XXX not right for shared key */
2308		if (status == IEEE80211_STATUS_SUCCESS)
2309			IEEE80211_NODE_STAT(ni, tx_auth);
2310		else
2311			IEEE80211_NODE_STAT(ni, tx_auth_fail);
2312
2313		if (vap->iv_opmode == IEEE80211_M_STA)
2314			ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2315				(void *) vap->iv_state);
2316		break;
2317
2318	case IEEE80211_FC0_SUBTYPE_DEAUTH:
2319		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2320		    "send station deauthenticate (reason %d)", arg);
2321		m = ieee80211_getmgtframe(&frm,
2322			ic->ic_headroom + sizeof(struct ieee80211_frame),
2323			sizeof(uint16_t));
2324		if (m == NULL)
2325			senderr(ENOMEM, is_tx_nobuf);
2326		*(uint16_t *)frm = htole16(arg);	/* reason */
2327		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2328
2329		IEEE80211_NODE_STAT(ni, tx_deauth);
2330		IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
2331
2332		ieee80211_node_unauthorize(ni);		/* port closed */
2333		break;
2334
2335	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2336	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2337		/*
2338		 * asreq frame format
2339		 *	[2] capability information
2340		 *	[2] listen interval
2341		 *	[6*] current AP address (reassoc only)
2342		 *	[tlv] ssid
2343		 *	[tlv] supported rates
2344		 *	[tlv] extended supported rates
2345		 *	[4] power capability (optional)
2346		 *	[28] supported channels (optional)
2347		 *	[tlv] HT capabilities
2348		 *	[tlv] WME (optional)
2349		 *	[tlv] Vendor OUI HT capabilities (optional)
2350		 *	[tlv] Atheros capabilities (if negotiated)
2351		 *	[tlv] AppIE's (optional)
2352		 */
2353		m = ieee80211_getmgtframe(&frm,
2354			 ic->ic_headroom + sizeof(struct ieee80211_frame),
2355			 sizeof(uint16_t)
2356		       + sizeof(uint16_t)
2357		       + IEEE80211_ADDR_LEN
2358		       + 2 + IEEE80211_NWID_LEN
2359		       + 2 + IEEE80211_RATE_SIZE
2360		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2361		       + 4
2362		       + 2 + 26
2363		       + sizeof(struct ieee80211_wme_info)
2364		       + sizeof(struct ieee80211_ie_htcap)
2365		       + 4 + sizeof(struct ieee80211_ie_htcap)
2366#ifdef IEEE80211_SUPPORT_SUPERG
2367		       + sizeof(struct ieee80211_ath_ie)
2368#endif
2369		       + (vap->iv_appie_wpa != NULL ?
2370				vap->iv_appie_wpa->ie_len : 0)
2371		       + (vap->iv_appie_assocreq != NULL ?
2372				vap->iv_appie_assocreq->ie_len : 0)
2373		);
2374		if (m == NULL)
2375			senderr(ENOMEM, is_tx_nobuf);
2376
2377		KASSERT(vap->iv_opmode == IEEE80211_M_STA,
2378		    ("wrong mode %u", vap->iv_opmode));
2379		capinfo = IEEE80211_CAPINFO_ESS;
2380		if (vap->iv_flags & IEEE80211_F_PRIVACY)
2381			capinfo |= IEEE80211_CAPINFO_PRIVACY;
2382		/*
2383		 * NB: Some 11a AP's reject the request when
2384		 *     short premable is set.
2385		 */
2386		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2387		    IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2388			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2389		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
2390		    (ic->ic_caps & IEEE80211_C_SHSLOT))
2391			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2392		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
2393		    (vap->iv_flags & IEEE80211_F_DOTH))
2394			capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2395		*(uint16_t *)frm = htole16(capinfo);
2396		frm += 2;
2397
2398		KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
2399		*(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
2400						    bss->ni_intval));
2401		frm += 2;
2402
2403		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2404			IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
2405			frm += IEEE80211_ADDR_LEN;
2406		}
2407
2408		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
2409		frm = ieee80211_add_rates(frm, &ni->ni_rates);
2410		frm = ieee80211_add_rsn(frm, vap);
2411		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2412		if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
2413			frm = ieee80211_add_powercapability(frm,
2414			    ic->ic_curchan);
2415			frm = ieee80211_add_supportedchannels(frm, ic);
2416		}
2417
2418		/*
2419		 * Check the channel - we may be using an 11n NIC with an
2420		 * 11n capable station, but we're configured to be an 11b
2421		 * channel.
2422		 */
2423		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2424		    IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2425		    ni->ni_ies.htcap_ie != NULL &&
2426		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP) {
2427			frm = ieee80211_add_htcap(frm, ni);
2428		}
2429		frm = ieee80211_add_wpa(frm, vap);
2430		if ((ic->ic_flags & IEEE80211_F_WME) &&
2431		    ni->ni_ies.wme_ie != NULL)
2432			frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
2433
2434		/*
2435		 * Same deal - only send HT info if we're on an 11n
2436		 * capable channel.
2437		 */
2438		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2439		    IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2440		    ni->ni_ies.htcap_ie != NULL &&
2441		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR) {
2442			frm = ieee80211_add_htcap_vendor(frm, ni);
2443		}
2444#ifdef IEEE80211_SUPPORT_SUPERG
2445		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
2446			frm = ieee80211_add_ath(frm,
2447				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2448				((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2449				 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2450				vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2451		}
2452#endif /* IEEE80211_SUPPORT_SUPERG */
2453		if (vap->iv_appie_assocreq != NULL)
2454			frm = add_appie(frm, vap->iv_appie_assocreq);
2455		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2456
2457		ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2458			(void *) vap->iv_state);
2459		break;
2460
2461	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2462	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2463		/*
2464		 * asresp frame format
2465		 *	[2] capability information
2466		 *	[2] status
2467		 *	[2] association ID
2468		 *	[tlv] supported rates
2469		 *	[tlv] extended supported rates
2470		 *	[tlv] HT capabilities (standard, if STA enabled)
2471		 *	[tlv] HT information (standard, if STA enabled)
2472		 *	[tlv] WME (if configured and STA enabled)
2473		 *	[tlv] HT capabilities (vendor OUI, if STA enabled)
2474		 *	[tlv] HT information (vendor OUI, if STA enabled)
2475		 *	[tlv] Atheros capabilities (if STA enabled)
2476		 *	[tlv] AppIE's (optional)
2477		 */
2478		m = ieee80211_getmgtframe(&frm,
2479			 ic->ic_headroom + sizeof(struct ieee80211_frame),
2480			 sizeof(uint16_t)
2481		       + sizeof(uint16_t)
2482		       + sizeof(uint16_t)
2483		       + 2 + IEEE80211_RATE_SIZE
2484		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2485		       + sizeof(struct ieee80211_ie_htcap) + 4
2486		       + sizeof(struct ieee80211_ie_htinfo) + 4
2487		       + sizeof(struct ieee80211_wme_param)
2488#ifdef IEEE80211_SUPPORT_SUPERG
2489		       + sizeof(struct ieee80211_ath_ie)
2490#endif
2491		       + (vap->iv_appie_assocresp != NULL ?
2492				vap->iv_appie_assocresp->ie_len : 0)
2493		);
2494		if (m == NULL)
2495			senderr(ENOMEM, is_tx_nobuf);
2496
2497		capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2498		*(uint16_t *)frm = htole16(capinfo);
2499		frm += 2;
2500
2501		*(uint16_t *)frm = htole16(arg);	/* status */
2502		frm += 2;
2503
2504		if (arg == IEEE80211_STATUS_SUCCESS) {
2505			*(uint16_t *)frm = htole16(ni->ni_associd);
2506			IEEE80211_NODE_STAT(ni, tx_assoc);
2507		} else
2508			IEEE80211_NODE_STAT(ni, tx_assoc_fail);
2509		frm += 2;
2510
2511		frm = ieee80211_add_rates(frm, &ni->ni_rates);
2512		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2513		/* NB: respond according to what we received */
2514		if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
2515			frm = ieee80211_add_htcap(frm, ni);
2516			frm = ieee80211_add_htinfo(frm, ni);
2517		}
2518		if ((vap->iv_flags & IEEE80211_F_WME) &&
2519		    ni->ni_ies.wme_ie != NULL)
2520			frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2521		if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
2522			frm = ieee80211_add_htcap_vendor(frm, ni);
2523			frm = ieee80211_add_htinfo_vendor(frm, ni);
2524		}
2525#ifdef IEEE80211_SUPPORT_SUPERG
2526		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
2527			frm = ieee80211_add_ath(frm,
2528				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2529				((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2530				 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2531				vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2532#endif /* IEEE80211_SUPPORT_SUPERG */
2533		if (vap->iv_appie_assocresp != NULL)
2534			frm = add_appie(frm, vap->iv_appie_assocresp);
2535		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2536		break;
2537
2538	case IEEE80211_FC0_SUBTYPE_DISASSOC:
2539		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2540		    "send station disassociate (reason %d)", arg);
2541		m = ieee80211_getmgtframe(&frm,
2542			ic->ic_headroom + sizeof(struct ieee80211_frame),
2543			sizeof(uint16_t));
2544		if (m == NULL)
2545			senderr(ENOMEM, is_tx_nobuf);
2546		*(uint16_t *)frm = htole16(arg);	/* reason */
2547		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2548
2549		IEEE80211_NODE_STAT(ni, tx_disassoc);
2550		IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
2551		break;
2552
2553	default:
2554		IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
2555		    "invalid mgmt frame type %u", type);
2556		senderr(EINVAL, is_tx_unknownmgt);
2557		/* NOTREACHED */
2558	}
2559
2560	/* NB: force non-ProbeResp frames to the highest queue */
2561	params.ibp_pri = WME_AC_VO;
2562	params.ibp_rate0 = bss->ni_txparms->mgmtrate;
2563	/* NB: we know all frames are unicast */
2564	params.ibp_try0 = bss->ni_txparms->maxretry;
2565	params.ibp_power = bss->ni_txpower;
2566	return ieee80211_mgmt_output(ni, m, type, &params);
2567bad:
2568	ieee80211_free_node(ni);
2569	return ret;
2570#undef senderr
2571#undef HTFLAGS
2572}
2573
2574/*
2575 * Return an mbuf with a probe response frame in it.
2576 * Space is left to prepend and 802.11 header at the
2577 * front but it's left to the caller to fill in.
2578 */
2579struct mbuf *
2580ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
2581{
2582	struct ieee80211vap *vap = bss->ni_vap;
2583	struct ieee80211com *ic = bss->ni_ic;
2584	const struct ieee80211_rateset *rs;
2585	struct mbuf *m;
2586	uint16_t capinfo;
2587	uint8_t *frm;
2588
2589	/*
2590	 * probe response frame format
2591	 *	[8] time stamp
2592	 *	[2] beacon interval
2593	 *	[2] cabability information
2594	 *	[tlv] ssid
2595	 *	[tlv] supported rates
2596	 *	[tlv] parameter set (FH/DS)
2597	 *	[tlv] parameter set (IBSS)
2598	 *	[tlv] country (optional)
2599	 *	[3] power control (optional)
2600	 *	[5] channel switch announcement (CSA) (optional)
2601	 *	[tlv] extended rate phy (ERP)
2602	 *	[tlv] extended supported rates
2603	 *	[tlv] RSN (optional)
2604	 *	[tlv] HT capabilities
2605	 *	[tlv] HT information
2606	 *	[tlv] WPA (optional)
2607	 *	[tlv] WME (optional)
2608	 *	[tlv] Vendor OUI HT capabilities (optional)
2609	 *	[tlv] Vendor OUI HT information (optional)
2610	 *	[tlv] Atheros capabilities
2611	 *	[tlv] AppIE's (optional)
2612	 *	[tlv] Mesh ID (MBSS)
2613	 *	[tlv] Mesh Conf (MBSS)
2614	 */
2615	m = ieee80211_getmgtframe(&frm,
2616		 ic->ic_headroom + sizeof(struct ieee80211_frame),
2617		 8
2618	       + sizeof(uint16_t)
2619	       + sizeof(uint16_t)
2620	       + 2 + IEEE80211_NWID_LEN
2621	       + 2 + IEEE80211_RATE_SIZE
2622	       + 7	/* max(7,3) */
2623	       + IEEE80211_COUNTRY_MAX_SIZE
2624	       + 3
2625	       + sizeof(struct ieee80211_csa_ie)
2626	       + sizeof(struct ieee80211_quiet_ie)
2627	       + 3
2628	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2629	       + sizeof(struct ieee80211_ie_wpa)
2630	       + sizeof(struct ieee80211_ie_htcap)
2631	       + sizeof(struct ieee80211_ie_htinfo)
2632	       + sizeof(struct ieee80211_ie_wpa)
2633	       + sizeof(struct ieee80211_wme_param)
2634	       + 4 + sizeof(struct ieee80211_ie_htcap)
2635	       + 4 + sizeof(struct ieee80211_ie_htinfo)
2636#ifdef IEEE80211_SUPPORT_SUPERG
2637	       + sizeof(struct ieee80211_ath_ie)
2638#endif
2639#ifdef IEEE80211_SUPPORT_MESH
2640	       + 2 + IEEE80211_MESHID_LEN
2641	       + sizeof(struct ieee80211_meshconf_ie)
2642#endif
2643	       + (vap->iv_appie_proberesp != NULL ?
2644			vap->iv_appie_proberesp->ie_len : 0)
2645	);
2646	if (m == NULL) {
2647		vap->iv_stats.is_tx_nobuf++;
2648		return NULL;
2649	}
2650
2651	memset(frm, 0, 8);	/* timestamp should be filled later */
2652	frm += 8;
2653	*(uint16_t *)frm = htole16(bss->ni_intval);
2654	frm += 2;
2655	capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2656	*(uint16_t *)frm = htole16(capinfo);
2657	frm += 2;
2658
2659	frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
2660	rs = ieee80211_get_suprates(ic, bss->ni_chan);
2661	frm = ieee80211_add_rates(frm, rs);
2662
2663	if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
2664		*frm++ = IEEE80211_ELEMID_FHPARMS;
2665		*frm++ = 5;
2666		*frm++ = bss->ni_fhdwell & 0x00ff;
2667		*frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
2668		*frm++ = IEEE80211_FH_CHANSET(
2669		    ieee80211_chan2ieee(ic, bss->ni_chan));
2670		*frm++ = IEEE80211_FH_CHANPAT(
2671		    ieee80211_chan2ieee(ic, bss->ni_chan));
2672		*frm++ = bss->ni_fhindex;
2673	} else {
2674		*frm++ = IEEE80211_ELEMID_DSPARMS;
2675		*frm++ = 1;
2676		*frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
2677	}
2678
2679	if (vap->iv_opmode == IEEE80211_M_IBSS) {
2680		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
2681		*frm++ = 2;
2682		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
2683	}
2684	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2685	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2686		frm = ieee80211_add_countryie(frm, ic);
2687	if (vap->iv_flags & IEEE80211_F_DOTH) {
2688		if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
2689			frm = ieee80211_add_powerconstraint(frm, vap);
2690		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2691			frm = ieee80211_add_csa(frm, vap);
2692	}
2693	if (vap->iv_flags & IEEE80211_F_DOTH) {
2694		if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
2695		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
2696			if (vap->iv_quiet)
2697				frm = ieee80211_add_quiet(frm, vap);
2698		}
2699	}
2700	if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
2701		frm = ieee80211_add_erp(frm, ic);
2702	frm = ieee80211_add_xrates(frm, rs);
2703	frm = ieee80211_add_rsn(frm, vap);
2704	/*
2705	 * NB: legacy 11b clients do not get certain ie's.
2706	 *     The caller identifies such clients by passing
2707	 *     a token in legacy to us.  Could expand this to be
2708	 *     any legacy client for stuff like HT ie's.
2709	 */
2710	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2711	    legacy != IEEE80211_SEND_LEGACY_11B) {
2712		frm = ieee80211_add_htcap(frm, bss);
2713		frm = ieee80211_add_htinfo(frm, bss);
2714	}
2715	frm = ieee80211_add_wpa(frm, vap);
2716	if (vap->iv_flags & IEEE80211_F_WME)
2717		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2718	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2719	    (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
2720	    legacy != IEEE80211_SEND_LEGACY_11B) {
2721		frm = ieee80211_add_htcap_vendor(frm, bss);
2722		frm = ieee80211_add_htinfo_vendor(frm, bss);
2723	}
2724#ifdef IEEE80211_SUPPORT_SUPERG
2725	if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
2726	    legacy != IEEE80211_SEND_LEGACY_11B)
2727		frm = ieee80211_add_athcaps(frm, bss);
2728#endif
2729	if (vap->iv_appie_proberesp != NULL)
2730		frm = add_appie(frm, vap->iv_appie_proberesp);
2731#ifdef IEEE80211_SUPPORT_MESH
2732	if (vap->iv_opmode == IEEE80211_M_MBSS) {
2733		frm = ieee80211_add_meshid(frm, vap);
2734		frm = ieee80211_add_meshconf(frm, vap);
2735	}
2736#endif
2737	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2738
2739	return m;
2740}
2741
2742/*
2743 * Send a probe response frame to the specified mac address.
2744 * This does not go through the normal mgt frame api so we
2745 * can specify the destination address and re-use the bss node
2746 * for the sta reference.
2747 */
2748int
2749ieee80211_send_proberesp(struct ieee80211vap *vap,
2750	const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
2751{
2752	struct ieee80211_node *bss = vap->iv_bss;
2753	struct ieee80211com *ic = vap->iv_ic;
2754	struct ieee80211_frame *wh;
2755	struct mbuf *m;
2756	int ret;
2757
2758	if (vap->iv_state == IEEE80211_S_CAC) {
2759		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
2760		    "block %s frame in CAC state", "probe response");
2761		vap->iv_stats.is_tx_badstate++;
2762		return EIO;		/* XXX */
2763	}
2764
2765	/*
2766	 * Hold a reference on the node so it doesn't go away until after
2767	 * the xmit is complete all the way in the driver.  On error we
2768	 * will remove our reference.
2769	 */
2770	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2771	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2772	    __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
2773	    ieee80211_node_refcnt(bss)+1);
2774	ieee80211_ref_node(bss);
2775
2776	m = ieee80211_alloc_proberesp(bss, legacy);
2777	if (m == NULL) {
2778		ieee80211_free_node(bss);
2779		return ENOMEM;
2780	}
2781
2782	M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
2783	KASSERT(m != NULL, ("no room for header"));
2784
2785	IEEE80211_TX_LOCK(ic);
2786	wh = mtod(m, struct ieee80211_frame *);
2787	ieee80211_send_setup(bss, m,
2788	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
2789	     IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
2790	/* XXX power management? */
2791	m->m_flags |= M_ENCAP;		/* mark encapsulated */
2792
2793	M_WME_SETAC(m, WME_AC_BE);
2794
2795	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2796	    "send probe resp on channel %u to %s%s\n",
2797	    ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
2798	    legacy ? " <legacy>" : "");
2799	IEEE80211_NODE_STAT(bss, tx_mgmt);
2800
2801	ret = ieee80211_raw_output(vap, bss, m, NULL);
2802	IEEE80211_TX_UNLOCK(ic);
2803	return (ret);
2804}
2805
2806/*
2807 * Allocate and build a RTS (Request To Send) control frame.
2808 */
2809struct mbuf *
2810ieee80211_alloc_rts(struct ieee80211com *ic,
2811	const uint8_t ra[IEEE80211_ADDR_LEN],
2812	const uint8_t ta[IEEE80211_ADDR_LEN],
2813	uint16_t dur)
2814{
2815	struct ieee80211_frame_rts *rts;
2816	struct mbuf *m;
2817
2818	/* XXX honor ic_headroom */
2819	m = m_gethdr(M_NOWAIT, MT_DATA);
2820	if (m != NULL) {
2821		rts = mtod(m, struct ieee80211_frame_rts *);
2822		rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2823			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
2824		rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2825		*(u_int16_t *)rts->i_dur = htole16(dur);
2826		IEEE80211_ADDR_COPY(rts->i_ra, ra);
2827		IEEE80211_ADDR_COPY(rts->i_ta, ta);
2828
2829		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
2830	}
2831	return m;
2832}
2833
2834/*
2835 * Allocate and build a CTS (Clear To Send) control frame.
2836 */
2837struct mbuf *
2838ieee80211_alloc_cts(struct ieee80211com *ic,
2839	const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
2840{
2841	struct ieee80211_frame_cts *cts;
2842	struct mbuf *m;
2843
2844	/* XXX honor ic_headroom */
2845	m = m_gethdr(M_NOWAIT, MT_DATA);
2846	if (m != NULL) {
2847		cts = mtod(m, struct ieee80211_frame_cts *);
2848		cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2849			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
2850		cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2851		*(u_int16_t *)cts->i_dur = htole16(dur);
2852		IEEE80211_ADDR_COPY(cts->i_ra, ra);
2853
2854		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
2855	}
2856	return m;
2857}
2858
2859static void
2860ieee80211_tx_mgt_timeout(void *arg)
2861{
2862	struct ieee80211vap *vap = arg;
2863
2864	IEEE80211_LOCK(vap->iv_ic);
2865	if (vap->iv_state != IEEE80211_S_INIT &&
2866	    (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
2867		/*
2868		 * NB: it's safe to specify a timeout as the reason here;
2869		 *     it'll only be used in the right state.
2870		 */
2871		ieee80211_new_state_locked(vap, IEEE80211_S_SCAN,
2872			IEEE80211_SCAN_FAIL_TIMEOUT);
2873	}
2874	IEEE80211_UNLOCK(vap->iv_ic);
2875}
2876
2877/*
2878 * This is the callback set on net80211-sourced transmitted
2879 * authentication request frames.
2880 *
2881 * This does a couple of things:
2882 *
2883 * + If the frame transmitted was a success, it schedules a future
2884 *   event which will transition the interface to scan.
2885 *   If a state transition _then_ occurs before that event occurs,
2886 *   said state transition will cancel this callout.
2887 *
2888 * + If the frame transmit was a failure, it immediately schedules
2889 *   the transition back to scan.
2890 */
2891static void
2892ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
2893{
2894	struct ieee80211vap *vap = ni->ni_vap;
2895	enum ieee80211_state ostate = (enum ieee80211_state) arg;
2896
2897	/*
2898	 * Frame transmit completed; arrange timer callback.  If
2899	 * transmit was successfuly we wait for response.  Otherwise
2900	 * we arrange an immediate callback instead of doing the
2901	 * callback directly since we don't know what state the driver
2902	 * is in (e.g. what locks it is holding).  This work should
2903	 * not be too time-critical and not happen too often so the
2904	 * added overhead is acceptable.
2905	 *
2906	 * XXX what happens if !acked but response shows up before callback?
2907	 */
2908	if (vap->iv_state == ostate) {
2909		callout_reset(&vap->iv_mgtsend,
2910			status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
2911			ieee80211_tx_mgt_timeout, vap);
2912	}
2913}
2914
2915static void
2916ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
2917	struct ieee80211_node *ni)
2918{
2919	struct ieee80211vap *vap = ni->ni_vap;
2920	struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
2921	struct ieee80211com *ic = ni->ni_ic;
2922	struct ieee80211_rateset *rs = &ni->ni_rates;
2923	uint16_t capinfo;
2924
2925	/*
2926	 * beacon frame format
2927	 *	[8] time stamp
2928	 *	[2] beacon interval
2929	 *	[2] cabability information
2930	 *	[tlv] ssid
2931	 *	[tlv] supported rates
2932	 *	[3] parameter set (DS)
2933	 *	[8] CF parameter set (optional)
2934	 *	[tlv] parameter set (IBSS/TIM)
2935	 *	[tlv] country (optional)
2936	 *	[3] power control (optional)
2937	 *	[5] channel switch announcement (CSA) (optional)
2938	 *	[tlv] extended rate phy (ERP)
2939	 *	[tlv] extended supported rates
2940	 *	[tlv] RSN parameters
2941	 *	[tlv] HT capabilities
2942	 *	[tlv] HT information
2943	 * XXX Vendor-specific OIDs (e.g. Atheros)
2944	 *	[tlv] WPA parameters
2945	 *	[tlv] WME parameters
2946	 *	[tlv] Vendor OUI HT capabilities (optional)
2947	 *	[tlv] Vendor OUI HT information (optional)
2948	 *	[tlv] Atheros capabilities (optional)
2949	 *	[tlv] TDMA parameters (optional)
2950	 *	[tlv] Mesh ID (MBSS)
2951	 *	[tlv] Mesh Conf (MBSS)
2952	 *	[tlv] application data (optional)
2953	 */
2954
2955	memset(bo, 0, sizeof(*bo));
2956
2957	memset(frm, 0, 8);	/* XXX timestamp is set by hardware/driver */
2958	frm += 8;
2959	*(uint16_t *)frm = htole16(ni->ni_intval);
2960	frm += 2;
2961	capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
2962	bo->bo_caps = (uint16_t *)frm;
2963	*(uint16_t *)frm = htole16(capinfo);
2964	frm += 2;
2965	*frm++ = IEEE80211_ELEMID_SSID;
2966	if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
2967		*frm++ = ni->ni_esslen;
2968		memcpy(frm, ni->ni_essid, ni->ni_esslen);
2969		frm += ni->ni_esslen;
2970	} else
2971		*frm++ = 0;
2972	frm = ieee80211_add_rates(frm, rs);
2973	if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
2974		*frm++ = IEEE80211_ELEMID_DSPARMS;
2975		*frm++ = 1;
2976		*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2977	}
2978	if (ic->ic_flags & IEEE80211_F_PCF) {
2979		bo->bo_cfp = frm;
2980		frm = ieee80211_add_cfparms(frm, ic);
2981	}
2982	bo->bo_tim = frm;
2983	if (vap->iv_opmode == IEEE80211_M_IBSS) {
2984		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
2985		*frm++ = 2;
2986		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
2987		bo->bo_tim_len = 0;
2988	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2989	    vap->iv_opmode == IEEE80211_M_MBSS) {
2990		/* TIM IE is the same for Mesh and Hostap */
2991		struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
2992
2993		tie->tim_ie = IEEE80211_ELEMID_TIM;
2994		tie->tim_len = 4;	/* length */
2995		tie->tim_count = 0;	/* DTIM count */
2996		tie->tim_period = vap->iv_dtim_period;	/* DTIM period */
2997		tie->tim_bitctl = 0;	/* bitmap control */
2998		tie->tim_bitmap[0] = 0;	/* Partial Virtual Bitmap */
2999		frm += sizeof(struct ieee80211_tim_ie);
3000		bo->bo_tim_len = 1;
3001	}
3002	bo->bo_tim_trailer = frm;
3003	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
3004	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
3005		frm = ieee80211_add_countryie(frm, ic);
3006	if (vap->iv_flags & IEEE80211_F_DOTH) {
3007		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
3008			frm = ieee80211_add_powerconstraint(frm, vap);
3009		bo->bo_csa = frm;
3010		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
3011			frm = ieee80211_add_csa(frm, vap);
3012	} else
3013		bo->bo_csa = frm;
3014
3015	if (vap->iv_flags & IEEE80211_F_DOTH) {
3016		bo->bo_quiet = frm;
3017		if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
3018		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
3019			if (vap->iv_quiet)
3020				frm = ieee80211_add_quiet(frm,vap);
3021		}
3022	} else
3023		bo->bo_quiet = frm;
3024
3025	if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
3026		bo->bo_erp = frm;
3027		frm = ieee80211_add_erp(frm, ic);
3028	}
3029	frm = ieee80211_add_xrates(frm, rs);
3030	frm = ieee80211_add_rsn(frm, vap);
3031	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
3032		frm = ieee80211_add_htcap(frm, ni);
3033		bo->bo_htinfo = frm;
3034		frm = ieee80211_add_htinfo(frm, ni);
3035	}
3036	frm = ieee80211_add_wpa(frm, vap);
3037	if (vap->iv_flags & IEEE80211_F_WME) {
3038		bo->bo_wme = frm;
3039		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
3040	}
3041	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
3042	    (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
3043		frm = ieee80211_add_htcap_vendor(frm, ni);
3044		frm = ieee80211_add_htinfo_vendor(frm, ni);
3045	}
3046#ifdef IEEE80211_SUPPORT_SUPERG
3047	if (vap->iv_flags & IEEE80211_F_ATHEROS) {
3048		bo->bo_ath = frm;
3049		frm = ieee80211_add_athcaps(frm, ni);
3050	}
3051#endif
3052#ifdef IEEE80211_SUPPORT_TDMA
3053	if (vap->iv_caps & IEEE80211_C_TDMA) {
3054		bo->bo_tdma = frm;
3055		frm = ieee80211_add_tdma(frm, vap);
3056	}
3057#endif
3058	if (vap->iv_appie_beacon != NULL) {
3059		bo->bo_appie = frm;
3060		bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
3061		frm = add_appie(frm, vap->iv_appie_beacon);
3062	}
3063#ifdef IEEE80211_SUPPORT_MESH
3064	if (vap->iv_opmode == IEEE80211_M_MBSS) {
3065		frm = ieee80211_add_meshid(frm, vap);
3066		bo->bo_meshconf = frm;
3067		frm = ieee80211_add_meshconf(frm, vap);
3068	}
3069#endif
3070	bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
3071	bo->bo_csa_trailer_len = frm - bo->bo_csa;
3072	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3073}
3074
3075/*
3076 * Allocate a beacon frame and fillin the appropriate bits.
3077 */
3078struct mbuf *
3079ieee80211_beacon_alloc(struct ieee80211_node *ni)
3080{
3081	struct ieee80211vap *vap = ni->ni_vap;
3082	struct ieee80211com *ic = ni->ni_ic;
3083	struct ifnet *ifp = vap->iv_ifp;
3084	struct ieee80211_frame *wh;
3085	struct mbuf *m;
3086	int pktlen;
3087	uint8_t *frm;
3088
3089	/*
3090	 * beacon frame format
3091	 *	[8] time stamp
3092	 *	[2] beacon interval
3093	 *	[2] cabability information
3094	 *	[tlv] ssid
3095	 *	[tlv] supported rates
3096	 *	[3] parameter set (DS)
3097	 *	[8] CF parameter set (optional)
3098	 *	[tlv] parameter set (IBSS/TIM)
3099	 *	[tlv] country (optional)
3100	 *	[3] power control (optional)
3101	 *	[5] channel switch announcement (CSA) (optional)
3102	 *	[tlv] extended rate phy (ERP)
3103	 *	[tlv] extended supported rates
3104	 *	[tlv] RSN parameters
3105	 *	[tlv] HT capabilities
3106	 *	[tlv] HT information
3107	 *	[tlv] Vendor OUI HT capabilities (optional)
3108	 *	[tlv] Vendor OUI HT information (optional)
3109	 * XXX Vendor-specific OIDs (e.g. Atheros)
3110	 *	[tlv] WPA parameters
3111	 *	[tlv] WME parameters
3112	 *	[tlv] TDMA parameters (optional)
3113	 *	[tlv] Mesh ID (MBSS)
3114	 *	[tlv] Mesh Conf (MBSS)
3115	 *	[tlv] application data (optional)
3116	 * NB: we allocate the max space required for the TIM bitmap.
3117	 * XXX how big is this?
3118	 */
3119	pktlen =   8					/* time stamp */
3120		 + sizeof(uint16_t)			/* beacon interval */
3121		 + sizeof(uint16_t)			/* capabilities */
3122		 + 2 + ni->ni_esslen			/* ssid */
3123	         + 2 + IEEE80211_RATE_SIZE		/* supported rates */
3124	         + 2 + 1				/* DS parameters */
3125		 + 2 + 6				/* CF parameters */
3126		 + 2 + 4 + vap->iv_tim_len		/* DTIM/IBSSPARMS */
3127		 + IEEE80211_COUNTRY_MAX_SIZE		/* country */
3128		 + 2 + 1				/* power control */
3129		 + sizeof(struct ieee80211_csa_ie)	/* CSA */
3130		 + sizeof(struct ieee80211_quiet_ie)	/* Quiet */
3131		 + 2 + 1				/* ERP */
3132	         + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
3133		 + (vap->iv_caps & IEEE80211_C_WPA ?	/* WPA 1+2 */
3134			2*sizeof(struct ieee80211_ie_wpa) : 0)
3135		 /* XXX conditional? */
3136		 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
3137		 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
3138		 + (vap->iv_caps & IEEE80211_C_WME ?	/* WME */
3139			sizeof(struct ieee80211_wme_param) : 0)
3140#ifdef IEEE80211_SUPPORT_SUPERG
3141		 + sizeof(struct ieee80211_ath_ie)	/* ATH */
3142#endif
3143#ifdef IEEE80211_SUPPORT_TDMA
3144		 + (vap->iv_caps & IEEE80211_C_TDMA ?	/* TDMA */
3145			sizeof(struct ieee80211_tdma_param) : 0)
3146#endif
3147#ifdef IEEE80211_SUPPORT_MESH
3148		 + 2 + ni->ni_meshidlen
3149		 + sizeof(struct ieee80211_meshconf_ie)
3150#endif
3151		 + IEEE80211_MAX_APPIE
3152		 ;
3153	m = ieee80211_getmgtframe(&frm,
3154		ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
3155	if (m == NULL) {
3156		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
3157			"%s: cannot get buf; size %u\n", __func__, pktlen);
3158		vap->iv_stats.is_tx_nobuf++;
3159		return NULL;
3160	}
3161	ieee80211_beacon_construct(m, frm, ni);
3162
3163	M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
3164	KASSERT(m != NULL, ("no space for 802.11 header?"));
3165	wh = mtod(m, struct ieee80211_frame *);
3166	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
3167	    IEEE80211_FC0_SUBTYPE_BEACON;
3168	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3169	*(uint16_t *)wh->i_dur = 0;
3170	IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
3171	IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
3172	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
3173	*(uint16_t *)wh->i_seq = 0;
3174
3175	return m;
3176}
3177
3178/*
3179 * Update the dynamic parts of a beacon frame based on the current state.
3180 */
3181int
3182ieee80211_beacon_update(struct ieee80211_node *ni, struct mbuf *m, int mcast)
3183{
3184	struct ieee80211vap *vap = ni->ni_vap;
3185	struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
3186	struct ieee80211com *ic = ni->ni_ic;
3187	int len_changed = 0;
3188	uint16_t capinfo;
3189	struct ieee80211_frame *wh;
3190	ieee80211_seq seqno;
3191
3192	IEEE80211_LOCK(ic);
3193	/*
3194	 * Handle 11h channel change when we've reached the count.
3195	 * We must recalculate the beacon frame contents to account
3196	 * for the new channel.  Note we do this only for the first
3197	 * vap that reaches this point; subsequent vaps just update
3198	 * their beacon state to reflect the recalculated channel.
3199	 */
3200	if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
3201	    vap->iv_csa_count == ic->ic_csa_count) {
3202		vap->iv_csa_count = 0;
3203		/*
3204		 * Effect channel change before reconstructing the beacon
3205		 * frame contents as many places reference ni_chan.
3206		 */
3207		if (ic->ic_csa_newchan != NULL)
3208			ieee80211_csa_completeswitch(ic);
3209		/*
3210		 * NB: ieee80211_beacon_construct clears all pending
3211		 * updates in bo_flags so we don't need to explicitly
3212		 * clear IEEE80211_BEACON_CSA.
3213		 */
3214		ieee80211_beacon_construct(m,
3215		    mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
3216
3217		/* XXX do WME aggressive mode processing? */
3218		IEEE80211_UNLOCK(ic);
3219		return 1;		/* just assume length changed */
3220	}
3221
3222	wh = mtod(m, struct ieee80211_frame *);
3223	seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
3224	*(uint16_t *)&wh->i_seq[0] =
3225		htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
3226	M_SEQNO_SET(m, seqno);
3227
3228	/* XXX faster to recalculate entirely or just changes? */
3229	capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
3230	*bo->bo_caps = htole16(capinfo);
3231
3232	if (vap->iv_flags & IEEE80211_F_WME) {
3233		struct ieee80211_wme_state *wme = &ic->ic_wme;
3234
3235		/*
3236		 * Check for agressive mode change.  When there is
3237		 * significant high priority traffic in the BSS
3238		 * throttle back BE traffic by using conservative
3239		 * parameters.  Otherwise BE uses agressive params
3240		 * to optimize performance of legacy/non-QoS traffic.
3241		 */
3242		if (wme->wme_flags & WME_F_AGGRMODE) {
3243			if (wme->wme_hipri_traffic >
3244			    wme->wme_hipri_switch_thresh) {
3245				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3246				    "%s: traffic %u, disable aggressive mode\n",
3247				    __func__, wme->wme_hipri_traffic);
3248				wme->wme_flags &= ~WME_F_AGGRMODE;
3249				ieee80211_wme_updateparams_locked(vap);
3250				wme->wme_hipri_traffic =
3251					wme->wme_hipri_switch_hysteresis;
3252			} else
3253				wme->wme_hipri_traffic = 0;
3254		} else {
3255			if (wme->wme_hipri_traffic <=
3256			    wme->wme_hipri_switch_thresh) {
3257				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3258				    "%s: traffic %u, enable aggressive mode\n",
3259				    __func__, wme->wme_hipri_traffic);
3260				wme->wme_flags |= WME_F_AGGRMODE;
3261				ieee80211_wme_updateparams_locked(vap);
3262				wme->wme_hipri_traffic = 0;
3263			} else
3264				wme->wme_hipri_traffic =
3265					wme->wme_hipri_switch_hysteresis;
3266		}
3267		if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
3268			(void) ieee80211_add_wme_param(bo->bo_wme, wme);
3269			clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
3270		}
3271	}
3272
3273	if (isset(bo->bo_flags,  IEEE80211_BEACON_HTINFO)) {
3274		ieee80211_ht_update_beacon(vap, bo);
3275		clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
3276	}
3277#ifdef IEEE80211_SUPPORT_TDMA
3278	if (vap->iv_caps & IEEE80211_C_TDMA) {
3279		/*
3280		 * NB: the beacon is potentially updated every TBTT.
3281		 */
3282		ieee80211_tdma_update_beacon(vap, bo);
3283	}
3284#endif
3285#ifdef IEEE80211_SUPPORT_MESH
3286	if (vap->iv_opmode == IEEE80211_M_MBSS)
3287		ieee80211_mesh_update_beacon(vap, bo);
3288#endif
3289
3290	if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
3291	    vap->iv_opmode == IEEE80211_M_MBSS) {	/* NB: no IBSS support*/
3292		struct ieee80211_tim_ie *tie =
3293			(struct ieee80211_tim_ie *) bo->bo_tim;
3294		if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
3295			u_int timlen, timoff, i;
3296			/*
3297			 * ATIM/DTIM needs updating.  If it fits in the
3298			 * current space allocated then just copy in the
3299			 * new bits.  Otherwise we need to move any trailing
3300			 * data to make room.  Note that we know there is
3301			 * contiguous space because ieee80211_beacon_allocate
3302			 * insures there is space in the mbuf to write a
3303			 * maximal-size virtual bitmap (based on iv_max_aid).
3304			 */
3305			/*
3306			 * Calculate the bitmap size and offset, copy any
3307			 * trailer out of the way, and then copy in the
3308			 * new bitmap and update the information element.
3309			 * Note that the tim bitmap must contain at least
3310			 * one byte and any offset must be even.
3311			 */
3312			if (vap->iv_ps_pending != 0) {
3313				timoff = 128;		/* impossibly large */
3314				for (i = 0; i < vap->iv_tim_len; i++)
3315					if (vap->iv_tim_bitmap[i]) {
3316						timoff = i &~ 1;
3317						break;
3318					}
3319				KASSERT(timoff != 128, ("tim bitmap empty!"));
3320				for (i = vap->iv_tim_len-1; i >= timoff; i--)
3321					if (vap->iv_tim_bitmap[i])
3322						break;
3323				timlen = 1 + (i - timoff);
3324			} else {
3325				timoff = 0;
3326				timlen = 1;
3327			}
3328			if (timlen != bo->bo_tim_len) {
3329				/* copy up/down trailer */
3330				int adjust = tie->tim_bitmap+timlen
3331					   - bo->bo_tim_trailer;
3332				ovbcopy(bo->bo_tim_trailer,
3333				    bo->bo_tim_trailer+adjust,
3334				    bo->bo_tim_trailer_len);
3335				bo->bo_tim_trailer += adjust;
3336				bo->bo_erp += adjust;
3337				bo->bo_htinfo += adjust;
3338#ifdef IEEE80211_SUPPORT_SUPERG
3339				bo->bo_ath += adjust;
3340#endif
3341#ifdef IEEE80211_SUPPORT_TDMA
3342				bo->bo_tdma += adjust;
3343#endif
3344#ifdef IEEE80211_SUPPORT_MESH
3345				bo->bo_meshconf += adjust;
3346#endif
3347				bo->bo_appie += adjust;
3348				bo->bo_wme += adjust;
3349				bo->bo_csa += adjust;
3350				bo->bo_quiet += adjust;
3351				bo->bo_tim_len = timlen;
3352
3353				/* update information element */
3354				tie->tim_len = 3 + timlen;
3355				tie->tim_bitctl = timoff;
3356				len_changed = 1;
3357			}
3358			memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
3359				bo->bo_tim_len);
3360
3361			clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
3362
3363			IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
3364				"%s: TIM updated, pending %u, off %u, len %u\n",
3365				__func__, vap->iv_ps_pending, timoff, timlen);
3366		}
3367		/* count down DTIM period */
3368		if (tie->tim_count == 0)
3369			tie->tim_count = tie->tim_period - 1;
3370		else
3371			tie->tim_count--;
3372		/* update state for buffered multicast frames on DTIM */
3373		if (mcast && tie->tim_count == 0)
3374			tie->tim_bitctl |= 1;
3375		else
3376			tie->tim_bitctl &= ~1;
3377		if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
3378			struct ieee80211_csa_ie *csa =
3379			    (struct ieee80211_csa_ie *) bo->bo_csa;
3380
3381			/*
3382			 * Insert or update CSA ie.  If we're just starting
3383			 * to count down to the channel switch then we need
3384			 * to insert the CSA ie.  Otherwise we just need to
3385			 * drop the count.  The actual change happens above
3386			 * when the vap's count reaches the target count.
3387			 */
3388			if (vap->iv_csa_count == 0) {
3389				memmove(&csa[1], csa, bo->bo_csa_trailer_len);
3390				bo->bo_erp += sizeof(*csa);
3391				bo->bo_htinfo += sizeof(*csa);
3392				bo->bo_wme += sizeof(*csa);
3393#ifdef IEEE80211_SUPPORT_SUPERG
3394				bo->bo_ath += sizeof(*csa);
3395#endif
3396#ifdef IEEE80211_SUPPORT_TDMA
3397				bo->bo_tdma += sizeof(*csa);
3398#endif
3399#ifdef IEEE80211_SUPPORT_MESH
3400				bo->bo_meshconf += sizeof(*csa);
3401#endif
3402				bo->bo_appie += sizeof(*csa);
3403				bo->bo_csa_trailer_len += sizeof(*csa);
3404				bo->bo_quiet += sizeof(*csa);
3405				bo->bo_tim_trailer_len += sizeof(*csa);
3406				m->m_len += sizeof(*csa);
3407				m->m_pkthdr.len += sizeof(*csa);
3408
3409				ieee80211_add_csa(bo->bo_csa, vap);
3410			} else
3411				csa->csa_count--;
3412			vap->iv_csa_count++;
3413			/* NB: don't clear IEEE80211_BEACON_CSA */
3414		}
3415		if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
3416		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS) ){
3417			if (vap->iv_quiet)
3418				ieee80211_add_quiet(bo->bo_quiet, vap);
3419		}
3420		if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
3421			/*
3422			 * ERP element needs updating.
3423			 */
3424			(void) ieee80211_add_erp(bo->bo_erp, ic);
3425			clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
3426		}
3427#ifdef IEEE80211_SUPPORT_SUPERG
3428		if (isset(bo->bo_flags,  IEEE80211_BEACON_ATH)) {
3429			ieee80211_add_athcaps(bo->bo_ath, ni);
3430			clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
3431		}
3432#endif
3433	}
3434	if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
3435		const struct ieee80211_appie *aie = vap->iv_appie_beacon;
3436		int aielen;
3437		uint8_t *frm;
3438
3439		aielen = 0;
3440		if (aie != NULL)
3441			aielen += aie->ie_len;
3442		if (aielen != bo->bo_appie_len) {
3443			/* copy up/down trailer */
3444			int adjust = aielen - bo->bo_appie_len;
3445			ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
3446				bo->bo_tim_trailer_len);
3447			bo->bo_tim_trailer += adjust;
3448			bo->bo_appie += adjust;
3449			bo->bo_appie_len = aielen;
3450
3451			len_changed = 1;
3452		}
3453		frm = bo->bo_appie;
3454		if (aie != NULL)
3455			frm  = add_appie(frm, aie);
3456		clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
3457	}
3458	IEEE80211_UNLOCK(ic);
3459
3460	return len_changed;
3461}
3462
3463/*
3464 * Do Ethernet-LLC encapsulation for each payload in a fast frame
3465 * tunnel encapsulation.  The frame is assumed to have an Ethernet
3466 * header at the front that must be stripped before prepending the
3467 * LLC followed by the Ethernet header passed in (with an Ethernet
3468 * type that specifies the payload size).
3469 */
3470struct mbuf *
3471ieee80211_ff_encap1(struct ieee80211vap *vap, struct mbuf *m,
3472	const struct ether_header *eh)
3473{
3474	struct llc *llc;
3475	uint16_t payload;
3476
3477	/* XXX optimize by combining m_adj+M_PREPEND */
3478	m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
3479	llc = mtod(m, struct llc *);
3480	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
3481	llc->llc_control = LLC_UI;
3482	llc->llc_snap.org_code[0] = 0;
3483	llc->llc_snap.org_code[1] = 0;
3484	llc->llc_snap.org_code[2] = 0;
3485	llc->llc_snap.ether_type = eh->ether_type;
3486	payload = m->m_pkthdr.len;		/* NB: w/o Ethernet header */
3487
3488	M_PREPEND(m, sizeof(struct ether_header), M_NOWAIT);
3489	if (m == NULL) {		/* XXX cannot happen */
3490		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
3491			"%s: no space for ether_header\n", __func__);
3492		vap->iv_stats.is_tx_nobuf++;
3493		return NULL;
3494	}
3495	ETHER_HEADER_COPY(mtod(m, void *), eh);
3496	mtod(m, struct ether_header *)->ether_type = htons(payload);
3497	return m;
3498}
3499
3500/*
3501 * Complete an mbuf transmission.
3502 *
3503 * For now, this simply processes a completed frame after the
3504 * driver has completed it's transmission and/or retransmission.
3505 * It assumes the frame is an 802.11 encapsulated frame.
3506 *
3507 * Later on it will grow to become the exit path for a given frame
3508 * from the driver and, depending upon how it's been encapsulated
3509 * and already transmitted, it may end up doing A-MPDU retransmission,
3510 * power save requeuing, etc.
3511 *
3512 * In order for the above to work, the driver entry point to this
3513 * must not hold any driver locks.  Thus, the driver needs to delay
3514 * any actual mbuf completion until it can release said locks.
3515 *
3516 * This frees the mbuf and if the mbuf has a node reference,
3517 * the node reference will be freed.
3518 */
3519void
3520ieee80211_tx_complete(struct ieee80211_node *ni, struct mbuf *m, int status)
3521{
3522
3523	if (ni != NULL) {
3524		struct ifnet *ifp = ni->ni_vap->iv_ifp;
3525
3526		if (status == 0) {
3527			if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
3528			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
3529			if (m->m_flags & M_MCAST)
3530				if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
3531		} else
3532			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3533		if (m->m_flags & M_TXCB)
3534			ieee80211_process_callback(ni, m, status);
3535		ieee80211_free_node(ni);
3536	}
3537	m_freem(m);
3538}
3539