ieee80211_output.c revision 289162
164499Swollman/*-
22742Swollman * Copyright (c) 2001 Atsushi Onoe
352770Sgrog * 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.
1158787Sru * 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.
1858787Sru * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1958787Sru * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2058787Sru * 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
239908Swollman * (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.
2530711Swollman */
262742Swollman
279908Swollman#include <sys/cdefs.h>
282742Swollman__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_output.c 289162 2015-10-12 03:27:08Z adrian $");
2958787Sru
3058787Sru#include "opt_inet.h"
3114343Swollman#include "opt_inet6.h"
3214343Swollman#include "opt_wlan.h"
3314343Swollman
3414343Swollman#include <sys/param.h>
3514343Swollman#include <sys/systm.h>
362742Swollman#include <sys/mbuf.h>
379908Swollman#include <sys/kernel.h>
3820094Swollman#include <sys/endian.h>
3920094Swollman
4020094Swollman#include <sys/socket.h>
4120094Swollman
4220094Swollman#include <net/bpf.h>
4320094Swollman#include <net/ethernet.h>
4420094Swollman#include <net/if.h>
4520094Swollman#include <net/if_var.h>
4620094Swollman#include <net/if_llc.h>
4720094Swollman#include <net/if_media.h>
4820094Swollman#include <net/if_vlan_var.h>
4920094Swollman
5020094Swollman#include <net80211/ieee80211_var.h>
5158787Sru#include <net80211/ieee80211_regdomain.h>
5258787Sru#ifdef IEEE80211_SUPPORT_SUPERG
5321217Swollman#include <net80211/ieee80211_superg.h>
5421217Swollman#endif
5558787Sru#ifdef IEEE80211_SUPPORT_TDMA
5658787Sru#include <net80211/ieee80211_tdma.h>
572742Swollman#endif
5858787Sru#include <net80211/ieee80211_wds.h>
5921217Swollman#include <net80211/ieee80211_mesh.h>
6020094Swollman
6158787Sru#if defined(INET) || defined(INET6)
6258787Sru#include <netinet/in.h>
6320094Swollman#endif
642742Swollman
659908Swollman#ifdef INET
662742Swollman#include <netinet/if_ether.h>
6714343Swollman#include <netinet/in_systm.h>
6814343Swollman#include <netinet/ip.h>
6914343Swollman#endif
7014343Swollman#ifdef INET6
7114343Swollman#include <netinet/ip6.h>
7214343Swollman#endif
7364499Swollman
7464499Swollman#include <security/mac/mac_framework.h>
7564499Swollman
7664499Swollman#define	ETHER_HEADER_COPY(dst, src) \
7764499Swollman	memcpy(dst, src, sizeof(struct ether_header))
7814343Swollman
792742Swollman/* unalligned little endian access */
802742Swollman#define LE_WRITE_2(p, v) do {				\
812742Swollman	((uint8_t *)(p))[0] = (v) & 0xff;		\
8258787Sru	((uint8_t *)(p))[1] = ((v) >> 8) & 0xff;	\
832742Swollman} while (0)
842742Swollman#define LE_WRITE_4(p, v) do {				\
859908Swollman	((uint8_t *)(p))[0] = (v) & 0xff;		\
862742Swollman	((uint8_t *)(p))[1] = ((v) >> 8) & 0xff;	\
8758787Sru	((uint8_t *)(p))[2] = ((v) >> 16) & 0xff;	\
8858787Sru	((uint8_t *)(p))[3] = ((v) >> 24) & 0xff;	\
8914343Swollman} while (0)
9014343Swollman
9158787Srustatic int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
9214343Swollman	u_int hdrsize, u_int ciphdrsize, u_int mtu);
9314343Swollmanstatic	void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
9414343Swollman
9558787Sru#ifdef IEEE80211_DEBUG
9614343Swollman/*
9758787Sru * Decide if an outbound management frame should be
9858787Sru * printed when debugging is enabled.  This filters some
9958787Sru * of the less interesting frames that come frequently
10014343Swollman * (e.g. beacons).
10158787Sru */
10258787Srustatic __inline int
1032742Swollmandoprint(struct ieee80211vap *vap, int subtype)
1042742Swollman{
10558787Sru	switch (subtype) {
10658787Sru	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
10758787Sru		return (vap->iv_opmode == IEEE80211_M_IBSS);
1082742Swollman	}
1092742Swollman	return 1;
1109908Swollman}
1112742Swollman#endif
11214343Swollman
11358787Sru/*
11414343Swollman * Transmit a frame to the given destination on the given VAP.
11514343Swollman *
11658787Sru * It's up to the caller to figure out the details of who this
11758787Sru * is going to and resolving the node.
11814343Swollman *
11914343Swollman * This routine takes care of queuing it for power save,
12058787Sru * A-MPDU state stuff, fast-frames state stuff, encapsulation
12143543Swollman * if required, then passing it up to the driver layer.
1222742Swollman *
1232742Swollman * This routine (for now) consumes the mbuf and frees the node
12458787Sru * reference; it ideally will return a TX status which reflects
1252742Swollman * whether the mbuf was consumed or not, so the caller can
1262742Swollman * free the mbuf (if appropriate) and the node reference (again,
1279908Swollman * if appropriate.)
1282742Swollman */
12914343Swollmanint
13014343Swollmanieee80211_vap_pkt_send_dest(struct ieee80211vap *vap, struct mbuf *m,
13114343Swollman    struct ieee80211_node *ni)
13214343Swollman{
13314343Swollman	struct ieee80211com *ic = vap->iv_ic;
13414343Swollman	struct ifnet *ifp = vap->iv_ifp;
13514343Swollman	int error, len, mcast;
13643543Swollman
13714343Swollman	if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
13814343Swollman	    (m->m_flags & M_PWR_SAV) == 0) {
13958787Sru		/*
14043543Swollman		 * Station in power save mode; pass the frame
1412742Swollman		 * to the 802.11 layer and continue.  We'll get
1422742Swollman		 * the frame back when the time is right.
14358787Sru		 * XXX lose WDS vap linkage?
1442742Swollman		 */
1452742Swollman		if (ieee80211_pwrsave(ni, m) != 0)
1462742Swollman			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1472742Swollman		ieee80211_free_node(ni);
14858787Sru
14958787Sru		/*
15058787Sru		 * We queued it fine, so tell the upper layer
1518029Swollman		 * that we consumed it.
15214343Swollman		 */
15314343Swollman		return (0);
15414343Swollman	}
15514343Swollman	/* calculate priority so drivers can find the tx queue */
15614343Swollman	if (ieee80211_classify(ni, m)) {
15714343Swollman		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
15814343Swollman		    ni->ni_macaddr, NULL,
15958787Sru		    "%s", "classification failure");
16014343Swollman		vap->iv_stats.is_tx_classify++;
16114343Swollman		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
16258787Sru		m_freem(m);
16358787Sru		ieee80211_free_node(ni);
1642742Swollman
1652742Swollman		/* XXX better status? */
16614343Swollman		return (0);
1678029Swollman	}
16814343Swollman	/*
1692742Swollman	 * Stash the node pointer.  Note that we do this after
1702742Swollman	 * any call to ieee80211_dwds_mcast because that code
17114343Swollman	 * uses any existing value for rcvif to identify the
17258787Sru	 * interface it (might have been) received on.
1732742Swollman	 */
17414343Swollman	m->m_pkthdr.rcvif = (void *)ni;
17514343Swollman	mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1: 0;
17614343Swollman	len = m->m_pkthdr.len;
17714343Swollman
17830711Swollman	BPF_MTAP(ifp, m);		/* 802.3 tx */
17930711Swollman
18058787Sru	/*
18158787Sru	 * Check if A-MPDU tx aggregation is setup or if we
1822742Swollman	 * should try to enable it.  The sta must be associated
18343014Swollman	 * with HT and A-MPDU enabled for use.  When the policy
18443014Swollman	 * routine decides we should enable A-MPDU we issue an
18543014Swollman	 * ADDBA request and wait for a reply.  The frame being
18643014Swollman	 * encapsulated will go out w/o using A-MPDU, or possibly
1872742Swollman	 * it might be collected by the driver and held/retransmit.
1882742Swollman	 * The default ic_ampdu_enable routine handles staggering
18958787Sru	 * ADDBA requests in case the receiver NAK's us or we are
1902742Swollman	 * otherwise unable to establish a BA stream.
19119878Swollman	 */
19243014Swollman	if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
19343014Swollman	    (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX) &&
1942742Swollman	    (m->m_flags & M_EAPOL) == 0) {
1952742Swollman		int tid = WME_AC_TO_TID(M_WME_GETAC(m));
19619878Swollman		struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[tid];
19719878Swollman
1982742Swollman		ieee80211_txampdu_count_packet(tap);
1992742Swollman		if (IEEE80211_AMPDU_RUNNING(tap)) {
2002742Swollman			/*
2012742Swollman			 * Operational, mark frame for aggregation.
20219878Swollman			 *
2032742Swollman			 * XXX do tx aggregation here
2042742Swollman			 */
20543543Swollman			m->m_flags |= M_AMPDU_MPDU;
20643543Swollman		} else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
20743543Swollman		    ic->ic_ampdu_enable(ni, tap)) {
2082742Swollman			/*
2092742Swollman			 * Not negotiated yet, request service.
21043543Swollman			 */
2112742Swollman			ieee80211_ampdu_request(ni, tap);
2122742Swollman			/* XXX hold frame for reply? */
2132742Swollman		}
2142742Swollman	}
21519878Swollman
2162742Swollman	/*
21719878Swollman	 * XXX If we aren't doing AMPDU TX then we /could/ do
2182742Swollman	 * fast-frames encapsulation, however right now this
21919878Swollman	 * output logic doesn't handle that case.
22043014Swollman	 *
22143014Swollman	 * So we'll be limited to "fast-frames" xmit for non-11n STA
2222742Swollman	 * and "no fast frames" xmit for 11n STAs.
2232742Swollman	 * It'd be nice to eventually test fast-frames out by
2242742Swollman	 * gracefully falling from failing A-MPDU transmission
2252742Swollman	 * (driver says no, fail to negotiate it with peer) to
2262742Swollman	 * using fast-frames.
2272742Swollman	 *
2282742Swollman	 * Note: we can actually put A-MSDU's inside an A-MPDU,
2292742Swollman	 * so hopefully we can figure out how to make that particular
2302742Swollman	 * combination work right.
23119878Swollman	 */
2322742Swollman#ifdef IEEE80211_SUPPORT_SUPERG
23319878Swollman	else if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF)) {
23419878Swollman		m = ieee80211_ff_check(ni, m);
23519878Swollman		if (m == NULL) {
2362742Swollman			/* NB: any ni ref held on stageq */
23719878Swollman			return (0);
23819878Swollman		}
23919878Swollman	}
2402742Swollman#endif /* IEEE80211_SUPPORT_SUPERG */
24114343Swollman
24214343Swollman	/*
24314343Swollman	 * Grab the TX lock - serialise the TX process from this
24419878Swollman	 * point (where TX state is being checked/modified)
24519878Swollman	 * through to driver queue.
24614343Swollman	 */
24714343Swollman	IEEE80211_TX_LOCK(ic);
24814343Swollman
24914343Swollman	/*
25019878Swollman	 * XXX make the encap and transmit code a separate function
25119878Swollman	 * so things like the FF (and later A-MSDU) path can just call
25214343Swollman	 * it for flushed frames.
25319878Swollman	 */
25419878Swollman	if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
25519878Swollman		/*
25614343Swollman		 * Encapsulate the packet in prep for transmission.
25714343Swollman		 */
25814343Swollman		m = ieee80211_encap(vap, ni, m);
25914343Swollman		if (m == NULL) {
26019878Swollman			/* NB: stat+msg handled in ieee80211_encap */
26119878Swollman			IEEE80211_TX_UNLOCK(ic);
26214343Swollman			ieee80211_free_node(ni);
26319878Swollman			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
26414343Swollman			return (ENOBUFS);
26519878Swollman		}
26614343Swollman	}
26758787Sru	error = ieee80211_parent_xmitpkt(ic, m);
26858787Sru
26958787Sru	/*
27014343Swollman	 * Unlock at this point - no need to hold it across
2712742Swollman	 * ieee80211_free_node() (ie, the comlock)
2722742Swollman	 */
2732742Swollman	IEEE80211_TX_UNLOCK(ic);
27419878Swollman	if (error != 0) {
2752742Swollman		/* NB: IFQ_HANDOFF reclaims mbuf */
27619878Swollman		ieee80211_free_node(ni);
27719878Swollman		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2782742Swollman	}
2792742Swollman	ic->ic_lastdata = ticks;
2802742Swollman
28119878Swollman	return (0);
28219878Swollman}
28343014Swollman
28458787Sru
28543014Swollman
2862742Swollman/*
2872742Swollman * Send the given mbuf through the given vap.
2882742Swollman *
2892742Swollman * This consumes the mbuf regardless of whether the transmit
2902742Swollman * was successful or not.
2912742Swollman *
2922742Swollman * This does none of the initial checks that ieee80211_start()
2932742Swollman * does (eg CAC timeout, interface wakeup) - the caller must
2942742Swollman * do this first.
2952742Swollman */
2962742Swollmanstatic int
2972742Swollmanieee80211_start_pkt(struct ieee80211vap *vap, struct mbuf *m)
29814343Swollman{
2992742Swollman#define	IS_DWDS(vap) \
30014343Swollman	(vap->iv_opmode == IEEE80211_M_WDS && \
30114343Swollman	 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
3022742Swollman	struct ieee80211com *ic = vap->iv_ic;
30314343Swollman	struct ifnet *ifp = vap->iv_ifp;
30443014Swollman	struct ieee80211_node *ni;
30514343Swollman	struct ether_header *eh;
30614343Swollman
30714343Swollman	/*
3089908Swollman	 * Cancel any background scan.
3099908Swollman	 */
3109908Swollman	if (ic->ic_flags & IEEE80211_F_SCAN)
3119908Swollman		ieee80211_cancel_anyscan(vap);
3129908Swollman	/*
3139908Swollman	 * Find the node for the destination so we can do
3149908Swollman	 * things like power save and fast frames aggregation.
31520094Swollman	 *
31620094Swollman	 * NB: past this point various code assumes the first
3172742Swollman	 *     mbuf has the 802.3 header present (and contiguous).
3182742Swollman	 */
31914343Swollman	ni = NULL;
3202742Swollman	if (m->m_len < sizeof(struct ether_header) &&
32120094Swollman	   (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
3222742Swollman		IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
3238029Swollman		    "discard frame, %s\n", "m_pullup failed");
32430711Swollman		vap->iv_stats.is_tx_nobuf++;	/* XXX */
32558787Sru		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
32658787Sru		return (ENOBUFS);
3272742Swollman	}
32830711Swollman	eh = mtod(m, struct ether_header *);
32958787Sru	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
33058787Sru		if (IS_DWDS(vap)) {
33158787Sru			/*
33230711Swollman			 * Only unicast frames from the above go out
33330711Swollman			 * DWDS vaps; multicast frames are handled by
3342742Swollman			 * dispatching the frame as it comes through
3352742Swollman			 * the AP vap (see below).
3362742Swollman			 */
3372742Swollman			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
3382742Swollman			    eh->ether_dhost, "mcast", "%s", "on DWDS");
3392742Swollman			vap->iv_stats.is_dwds_mcast++;
34019878Swollman			m_freem(m);
34119878Swollman			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
34219878Swollman			/* XXX better status? */
3432742Swollman			return (ENOBUFS);
3442742Swollman		}
3452742Swollman		if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
3462742Swollman			/*
34719878Swollman			 * Spam DWDS vap's w/ multicast traffic.
34819878Swollman			 */
3492742Swollman			/* XXX only if dwds in use? */
35058787Sru			ieee80211_dwds_mcast(vap, m);
3519908Swollman		}
3529908Swollman	}
35319878Swollman#ifdef IEEE80211_SUPPORT_MESH
3549908Swollman	if (vap->iv_opmode != IEEE80211_M_MBSS) {
3552742Swollman#endif
3562742Swollman		ni = ieee80211_find_txnode(vap, eh->ether_dhost);
3572742Swollman		if (ni == NULL) {
35819878Swollman			/* NB: ieee80211_find_txnode does stat+msg */
35919878Swollman			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3602742Swollman			m_freem(m);
3612742Swollman			/* XXX better status? */
3622742Swollman			return (ENOBUFS);
3632742Swollman		}
36458787Sru		if (ni->ni_associd == 0 &&
36558787Sru		    (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
3662742Swollman			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
36714343Swollman			    eh->ether_dhost, NULL,
36814343Swollman			    "sta not associated (type 0x%04x)",
36914343Swollman			    htons(eh->ether_type));
37019878Swollman			vap->iv_stats.is_tx_notassoc++;
37114343Swollman			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
37214343Swollman			m_freem(m);
37314343Swollman			ieee80211_free_node(ni);
37414343Swollman			/* XXX better status? */
37514343Swollman			return (ENOBUFS);
37614343Swollman		}
37714343Swollman#ifdef IEEE80211_SUPPORT_MESH
37819878Swollman	} else {
37919878Swollman		if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) {
38014343Swollman			/*
3812742Swollman			 * Proxy station only if configured.
3822742Swollman			 */
3832742Swollman			if (!ieee80211_mesh_isproxyena(vap)) {
3842742Swollman				IEEE80211_DISCARD_MAC(vap,
38519878Swollman				    IEEE80211_MSG_OUTPUT |
3862742Swollman				    IEEE80211_MSG_MESH,
3872742Swollman				    eh->ether_dhost, NULL,
3882742Swollman				    "%s", "proxy not enabled");
3892742Swollman				vap->iv_stats.is_mesh_notproxy++;
39019878Swollman				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3912742Swollman				m_freem(m);
3922742Swollman				/* XXX better status? */
39358787Sru				return (ENOBUFS);
39458787Sru			}
39558787Sru			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
3962742Swollman			    "forward frame from DS SA(%6D), DA(%6D)\n",
3972742Swollman			    eh->ether_shost, ":",
39858787Sru			    eh->ether_dhost, ":");
39958787Sru			ieee80211_mesh_proxy_check(vap, eh->ether_shost);
40058787Sru		}
4012742Swollman		ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m);
4022742Swollman		if (ni == NULL) {
4032742Swollman			/*
4042742Swollman			 * NB: ieee80211_mesh_discover holds/disposes
40519878Swollman			 * frame (e.g. queueing on path discovery).
4062742Swollman			 */
40743014Swollman			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
40843014Swollman			/* XXX better status? */
40943014Swollman			return (ENOBUFS);
41043014Swollman		}
41143014Swollman	}
41243014Swollman#endif
41343014Swollman
41443014Swollman	/*
41543014Swollman	 * We've resolved the sender, so attempt to transmit it.
41643014Swollman	 */
41743014Swollman
41843014Swollman	if (vap->iv_state == IEEE80211_S_SLEEP) {
41943014Swollman		/*
42043014Swollman		 * In power save; queue frame and then  wakeup device
42143014Swollman		 * for transmit.
42243014Swollman		 */
42343014Swollman		ic->ic_lastdata = ticks;
42443014Swollman		if (ieee80211_pwrsave(ni, m) != 0)
42543014Swollman			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
42643014Swollman		ieee80211_free_node(ni);
42743014Swollman		ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
42843014Swollman		return (0);
42943014Swollman	}
43043014Swollman
43143014Swollman	if (ieee80211_vap_pkt_send_dest(vap, m, ni) != 0)
43243014Swollman		return (ENOBUFS);
43343014Swollman	return (0);
43443014Swollman#undef	IS_DWDS
43543014Swollman}
43643014Swollman
43743014Swollman/*
43843014Swollman * Start method for vap's.  All packets from the stack come
43943014Swollman * through here.  We handle common processing of the packets
4402742Swollman * before dispatching them to the underlying device.
4412742Swollman *
44219878Swollman * if_transmit() requires that the mbuf be consumed by this call
44319878Swollman * regardless of the return condition.
44419878Swollman */
44520094Swollmanint
44620094Swollmanieee80211_vap_transmit(struct ifnet *ifp, struct mbuf *m)
44720094Swollman{
4482742Swollman	struct ieee80211vap *vap = ifp->if_softc;
4492742Swollman	struct ieee80211com *ic = vap->iv_ic;
45019878Swollman
4512742Swollman	/*
4522742Swollman	 * No data frames go out unless we're running.
4532742Swollman	 * Note in particular this covers CAC and CSA
4542742Swollman	 * states (though maybe we should check muting
45519878Swollman	 * for CSA).
4562742Swollman	 */
4572742Swollman	if (vap->iv_state != IEEE80211_S_RUN &&
4582742Swollman	    vap->iv_state != IEEE80211_S_SLEEP) {
4592742Swollman		IEEE80211_LOCK(ic);
4602742Swollman		/* re-check under the com lock to avoid races */
4612742Swollman		if (vap->iv_state != IEEE80211_S_RUN &&
4622742Swollman		    vap->iv_state != IEEE80211_S_SLEEP) {
4632742Swollman			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
4642742Swollman			    "%s: ignore queue, in %s state\n",
46558787Sru			    __func__, ieee80211_state_name[vap->iv_state]);
4662742Swollman			vap->iv_stats.is_tx_badstate++;
46758787Sru			IEEE80211_UNLOCK(ic);
46858787Sru			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
4692742Swollman			m_freem(m);
47058787Sru			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
47120094Swollman			return (ENETDOWN);
47220094Swollman		}
47320094Swollman		IEEE80211_UNLOCK(ic);
47420094Swollman	}
47520094Swollman
47620094Swollman	/*
47720094Swollman	 * Sanitize mbuf flags for net80211 use.  We cannot
47820094Swollman	 * clear M_PWR_SAV or M_MORE_DATA because these may
4792742Swollman	 * be set for frames that are re-submitted from the
4802742Swollman	 * power save queue.
4812742Swollman	 *
4822742Swollman	 * NB: This must be done before ieee80211_classify as
4832742Swollman	 *     it marks EAPOL in frames with M_EAPOL.
48458787Sru	 */
4852742Swollman	m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
4862742Swollman
4872742Swollman	/*
4882742Swollman	 * Bump to the packet transmission path.
4892742Swollman	 * The mbuf will be consumed here.
4902742Swollman	 */
4912742Swollman	return (ieee80211_start_pkt(vap, m));
4922742Swollman}
4932742Swollman
4942742Swollmanvoid
49519878Swollmanieee80211_vap_qflush(struct ifnet *ifp)
4962742Swollman{
4972742Swollman
49819878Swollman	/* Empty for now */
4992742Swollman}
50014343Swollman
50158787Sru/*
5022742Swollman * 802.11 raw output routine.
50314343Swollman *
5049908Swollman * XXX TODO: this (and other send routines) should correctly
5052742Swollman * XXX keep the pwr mgmt bit set if it decides to call into the
5062742Swollman * XXX driver to send a frame whilst the state is SLEEP.
5072742Swollman *
5082742Swollman * Otherwise the peer may decide that we're awake and flood us
5092742Swollman * with traffic we are still too asleep to receive!
51058787Sru */
51158787Sruint
51258787Sruieee80211_raw_output(struct ieee80211vap *vap, struct ieee80211_node *ni,
51358787Sru    struct mbuf *m, const struct ieee80211_bpf_params *params)
51419878Swollman{
5152742Swollman	struct ieee80211com *ic = vap->iv_ic;
5162742Swollman	int error;
5172742Swollman
5182742Swollman	/*
5192742Swollman	 * Set node - the caller has taken a reference, so ensure
5202742Swollman	 * that the mbuf has the same node value that
5212742Swollman	 * it would if it were going via the normal path.
5222742Swollman	 */
5232742Swollman	m->m_pkthdr.rcvif = (void *)ni;
5242742Swollman
5252742Swollman	/*
5262742Swollman	 * Attempt to add bpf transmit parameters.
5272742Swollman	 *
5282742Swollman	 * For now it's ok to fail; the raw_xmit api still takes
5292742Swollman	 * them as an option.
5302742Swollman	 *
5312742Swollman	 * Later on when ic_raw_xmit() has params removed,
5322742Swollman	 * they'll have to be added - so fail the transmit if
5332742Swollman	 * they can't be.
5342742Swollman	 */
5352742Swollman	if (params)
5362742Swollman		(void) ieee80211_add_xmit_params(m, params);
5372742Swollman
53814343Swollman	error = ic->ic_raw_xmit(ni, m, params);
5399908Swollman	if (error)
5409908Swollman		if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, 1);
5419908Swollman	return (error);
5429908Swollman}
5439908Swollman
54414343Swollman/*
5452742Swollman * 802.11 output routine. This is (currently) used only to
54643014Swollman * connect bpf write calls to the 802.11 layer for injecting
54743014Swollman * raw 802.11 frames.
54843014Swollman */
54943014Swollmanint
55043014Swollmanieee80211_output(struct ifnet *ifp, struct mbuf *m,
55143014Swollman	const struct sockaddr *dst, struct route *ro)
55243014Swollman{
55343014Swollman#define senderr(e) do { error = (e); goto bad;} while (0)
55443014Swollman	struct ieee80211_node *ni = NULL;
55543014Swollman	struct ieee80211vap *vap;
55643014Swollman	struct ieee80211_frame *wh;
55758787Sru	struct ieee80211com *ic = NULL;
55843014Swollman	int error;
55943014Swollman	int ret;
56043014Swollman
56143014Swollman	if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
56243014Swollman		/*
56343014Swollman		 * Short-circuit requests if the vap is marked OACTIVE
56443014Swollman		 * as this can happen because a packet came down through
5659908Swollman		 * ieee80211_start before the vap entered RUN state in
5669908Swollman		 * which case it's ok to just drop the frame.  This
56719878Swollman		 * should not be necessary but callers of if_output don't
5682742Swollman		 * check OACTIVE.
5692742Swollman		 */
5702742Swollman		senderr(ENETDOWN);
5712742Swollman	}
5729908Swollman	vap = ifp->if_softc;
5732742Swollman	ic = vap->iv_ic;
57419878Swollman	/*
5752742Swollman	 * Hand to the 802.3 code if not tagged as
5762742Swollman	 * a raw 802.11 frame.
5772742Swollman	 */
5789908Swollman	if (dst->sa_family != AF_IEEE80211)
5792742Swollman		return vap->iv_output(ifp, m, dst, ro);
58019878Swollman#ifdef MAC
5812742Swollman	error = mac_ifnet_check_transmit(ifp, m);
5822742Swollman	if (error)
5832742Swollman		senderr(error);
5842742Swollman#endif
5852742Swollman	if (ifp->if_flags & IFF_MONITOR)
5862742Swollman		senderr(ENETDOWN);
5872742Swollman	if (!IFNET_IS_UP_RUNNING(ifp))
5882742Swollman		senderr(ENETDOWN);
5892742Swollman	if (vap->iv_state == IEEE80211_S_CAC) {
5902742Swollman		IEEE80211_DPRINTF(vap,
5912742Swollman		    IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
5922742Swollman		    "block %s frame in CAC state\n", "raw data");
5932742Swollman		vap->iv_stats.is_tx_badstate++;
5942742Swollman		senderr(EIO);		/* XXX */
59519878Swollman	} else if (vap->iv_state == IEEE80211_S_SCAN)
5962742Swollman		senderr(EIO);
5972742Swollman	/* XXX bypass bridge, pfil, carp, etc. */
5982742Swollman
59919878Swollman	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
6002742Swollman		senderr(EIO);	/* XXX */
6012742Swollman	wh = mtod(m, struct ieee80211_frame *);
6022742Swollman	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
6032742Swollman	    IEEE80211_FC0_VERSION_0)
6042742Swollman		senderr(EIO);	/* XXX */
6052742Swollman
60619878Swollman	/* locate destination node */
6072742Swollman	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
6082742Swollman	case IEEE80211_FC1_DIR_NODS:
6092742Swollman	case IEEE80211_FC1_DIR_FROMDS:
61058787Sru		ni = ieee80211_find_txnode(vap, wh->i_addr1);
61158787Sru		break;
61258787Sru	case IEEE80211_FC1_DIR_TODS:
61358787Sru	case IEEE80211_FC1_DIR_DSTODS:
61458787Sru		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame))
6159908Swollman			senderr(EIO);	/* XXX */
61619878Swollman		ni = ieee80211_find_txnode(vap, wh->i_addr3);
6172742Swollman		break;
6182742Swollman	default:
6192742Swollman		senderr(EIO);	/* XXX */
6202742Swollman	}
6212742Swollman	if (ni == NULL) {
6222742Swollman		/*
6232742Swollman		 * Permit packets w/ bpf params through regardless
6242742Swollman		 * (see below about sa_len).
6252742Swollman		 */
6262742Swollman		if (dst->sa_len == 0)
62719878Swollman			senderr(EHOSTUNREACH);
6282742Swollman		ni = ieee80211_ref_node(vap->iv_bss);
6292742Swollman	}
6302742Swollman
63119878Swollman	/*
6322742Swollman	 * Sanitize mbuf for net80211 flags leaked from above.
6332742Swollman	 *
6342742Swollman	 * NB: This must be done before ieee80211_classify as
6352742Swollman	 *     it marks EAPOL in frames with M_EAPOL.
63619878Swollman	 */
6372742Swollman	m->m_flags &= ~M_80211_TX;
6382742Swollman
6392742Swollman	/* calculate priority so drivers can find the tx queue */
6402742Swollman	/* XXX assumes an 802.3 frame */
64119878Swollman	if (ieee80211_classify(ni, m))
6422742Swollman		senderr(EIO);		/* XXX */
6432742Swollman
6442742Swollman	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
6452742Swollman	IEEE80211_NODE_STAT(ni, tx_data);
6462742Swollman	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
6472742Swollman		IEEE80211_NODE_STAT(ni, tx_mcast);
6482742Swollman		m->m_flags |= M_MCAST;
64919878Swollman	} else
6502742Swollman		IEEE80211_NODE_STAT(ni, tx_ucast);
6512742Swollman	/* NB: ieee80211_encap does not include 802.11 header */
6529908Swollman	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, m->m_pkthdr.len);
6532742Swollman
65419878Swollman	IEEE80211_TX_LOCK(ic);
6552742Swollman
6562742Swollman	/*
6572742Swollman	 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
6582742Swollman	 * present by setting the sa_len field of the sockaddr (yes,
6592742Swollman	 * this is a hack).
6602742Swollman	 * NB: we assume sa_data is suitably aligned to cast.
6612742Swollman	 */
6622742Swollman	ret = ieee80211_raw_output(vap, ni, m,
6632742Swollman	    (const struct ieee80211_bpf_params *)(dst->sa_len ?
6642742Swollman		dst->sa_data : NULL));
6652742Swollman	IEEE80211_TX_UNLOCK(ic);
6662742Swollman	return (ret);
6672742Swollmanbad:
6682742Swollman	if (m != NULL)
6692742Swollman		m_freem(m);
6702742Swollman	if (ni != NULL)
6712742Swollman		ieee80211_free_node(ni);
67219878Swollman	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
6732742Swollman	return error;
6742742Swollman#undef senderr
6752742Swollman}
6762742Swollman
6772742Swollman/*
6782742Swollman * Set the direction field and address fields of an outgoing
6792742Swollman * frame.  Note this should be called early on in constructing
6809908Swollman * a frame as it sets i_fc[1]; other bits can then be or'd in.
6812742Swollman */
68219878Swollmanvoid
6832742Swollmanieee80211_send_setup(
6842742Swollman	struct ieee80211_node *ni,
6852742Swollman	struct mbuf *m,
6862742Swollman	int type, int tid,
68719878Swollman	const uint8_t sa[IEEE80211_ADDR_LEN],
6882742Swollman	const uint8_t da[IEEE80211_ADDR_LEN],
6892742Swollman	const uint8_t bssid[IEEE80211_ADDR_LEN])
6902742Swollman{
6912742Swollman#define	WH4(wh)	((struct ieee80211_frame_addr4 *)wh)
6922742Swollman	struct ieee80211vap *vap = ni->ni_vap;
6932742Swollman	struct ieee80211_tx_ampdu *tap;
6942742Swollman	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
6952742Swollman	ieee80211_seq seqno;
6962742Swollman
6972742Swollman	IEEE80211_TX_LOCK_ASSERT(ni->ni_ic);
69819878Swollman
6992742Swollman	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
7002742Swollman	if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
7012742Swollman		switch (vap->iv_opmode) {
7022742Swollman		case IEEE80211_M_STA:
70319878Swollman			wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
7042742Swollman			IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
7052742Swollman			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
7062742Swollman			IEEE80211_ADDR_COPY(wh->i_addr3, da);
7072742Swollman			break;
7082742Swollman		case IEEE80211_M_IBSS:
70919878Swollman		case IEEE80211_M_AHDEMO:
7102742Swollman			wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
7112742Swollman			IEEE80211_ADDR_COPY(wh->i_addr1, da);
7122742Swollman			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
7132742Swollman			IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
71419878Swollman			break;
7152742Swollman		case IEEE80211_M_HOSTAP:
7162742Swollman			wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
7172742Swollman			IEEE80211_ADDR_COPY(wh->i_addr1, da);
7182742Swollman			IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
7199908Swollman			IEEE80211_ADDR_COPY(wh->i_addr3, sa);
7209908Swollman			break;
7219908Swollman		case IEEE80211_M_WDS:
7229908Swollman			wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
7232742Swollman			IEEE80211_ADDR_COPY(wh->i_addr1, da);
7249908Swollman			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
7259908Swollman			IEEE80211_ADDR_COPY(wh->i_addr3, da);
72658787Sru			IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
72758787Sru			break;
7282742Swollman		case IEEE80211_M_MBSS:
7292742Swollman#ifdef IEEE80211_SUPPORT_MESH
7302742Swollman			if (IEEE80211_IS_MULTICAST(da)) {
73119878Swollman				wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
7322742Swollman				/* XXX next hop */
7332742Swollman				IEEE80211_ADDR_COPY(wh->i_addr1, da);
7342742Swollman				IEEE80211_ADDR_COPY(wh->i_addr2,
7352742Swollman				    vap->iv_myaddr);
73658787Sru			} else {
73758787Sru				wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
73858787Sru				IEEE80211_ADDR_COPY(wh->i_addr1, da);
73958787Sru				IEEE80211_ADDR_COPY(wh->i_addr2,
74058787Sru				    vap->iv_myaddr);
74158787Sru				IEEE80211_ADDR_COPY(wh->i_addr3, da);
74258787Sru				IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
74364499Swollman			}
74464499Swollman#endif
74564499Swollman			break;
7469908Swollman		case IEEE80211_M_MONITOR:	/* NB: to quiet compiler */
7472742Swollman			break;
74858787Sru		}
74958787Sru	} else {
7502742Swollman		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
7512742Swollman		IEEE80211_ADDR_COPY(wh->i_addr1, da);
7522742Swollman		IEEE80211_ADDR_COPY(wh->i_addr2, sa);
7539908Swollman#ifdef IEEE80211_SUPPORT_MESH
7542742Swollman		if (vap->iv_opmode == IEEE80211_M_MBSS)
7552742Swollman			IEEE80211_ADDR_COPY(wh->i_addr3, sa);
7562742Swollman		else
7572742Swollman#endif
7582742Swollman			IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
7592742Swollman	}
7602742Swollman	*(uint16_t *)&wh->i_dur[0] = 0;
7612742Swollman
7622742Swollman	tap = &ni->ni_tx_ampdu[tid];
7632742Swollman	if (tid != IEEE80211_NONQOS_TID && IEEE80211_AMPDU_RUNNING(tap))
7642742Swollman		m->m_flags |= M_AMPDU_MPDU;
7652742Swollman	else {
76658787Sru		if (IEEE80211_HAS_SEQ(type & IEEE80211_FC0_TYPE_MASK,
76758787Sru				      type & IEEE80211_FC0_SUBTYPE_MASK))
76858787Sru			seqno = ni->ni_txseqs[tid]++;
76958787Sru		else
77058787Sru			seqno = 0;
77158787Sru
77258787Sru		*(uint16_t *)&wh->i_seq[0] =
77358787Sru		    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
77458787Sru		M_SEQNO_SET(m, seqno);
77558787Sru	}
7762742Swollman
77758787Sru	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
77858787Sru		m->m_flags |= M_MCAST;
77958787Sru#undef WH4
78058787Sru}
78158787Sru
78258787Sru/*
78358787Sru * Send a management frame to the specified node.  The node pointer
7842742Swollman * must have a reference as the pointer will be passed to the driver
78558787Sru * and potentially held for a long time.  If the frame is successfully
78658787Sru * dispatched to the driver, then it is responsible for freeing the
78758787Sru * reference (and potentially free'ing up any associated storage);
78858787Sru * otherwise deal with reclaiming any reference (on error).
78958787Sru */
79058787Sruint
79158787Sruieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
79258787Sru	struct ieee80211_bpf_params *params)
79358787Sru{
79458787Sru	struct ieee80211vap *vap = ni->ni_vap;
79558787Sru	struct ieee80211com *ic = ni->ni_ic;
79658787Sru	struct ieee80211_frame *wh;
79758787Sru	int ret;
79858787Sru
79958787Sru	KASSERT(ni != NULL, ("null node"));
80058787Sru
80158787Sru	if (vap->iv_state == IEEE80211_S_CAC) {
80258787Sru		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
80358787Sru		    ni, "block %s frame in CAC state",
80458787Sru			ieee80211_mgt_subtype_name[
80558787Sru			    (type & IEEE80211_FC0_SUBTYPE_MASK) >>
80658787Sru				IEEE80211_FC0_SUBTYPE_SHIFT]);
80758787Sru		vap->iv_stats.is_tx_badstate++;
80858787Sru		ieee80211_free_node(ni);
80958787Sru		m_freem(m);
81058787Sru		return EIO;		/* XXX */
81158787Sru	}
81258787Sru
81358787Sru	M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
81458787Sru	if (m == NULL) {
81558787Sru		ieee80211_free_node(ni);
81658787Sru		return ENOMEM;
81758787Sru	}
81858787Sru
81958787Sru	IEEE80211_TX_LOCK(ic);
82058787Sru
8212742Swollman	wh = mtod(m, struct ieee80211_frame *);
82243543Swollman	ieee80211_send_setup(ni, m,
82343543Swollman	     IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
82458787Sru	     vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
82543543Swollman	if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
8269908Swollman		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
8272742Swollman		    "encrypting frame (%s)", __func__);
82819878Swollman		wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
8292742Swollman	}
8302742Swollman	m->m_flags |= M_ENCAP;		/* mark encapsulated */
83119878Swollman
8322742Swollman	KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
8332742Swollman	M_WME_SETAC(m, params->ibp_pri);
8342742Swollman
83543014Swollman#ifdef IEEE80211_DEBUG
8362742Swollman	/* avoid printing too many frames */
8372742Swollman	if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
8382742Swollman	    ieee80211_msg_dumppkts(vap)) {
8392742Swollman		printf("[%s] send %s on channel %u\n",
8402742Swollman		    ether_sprintf(wh->i_addr1),
8412742Swollman		    ieee80211_mgt_subtype_name[
8422742Swollman			(type & IEEE80211_FC0_SUBTYPE_MASK) >>
8432742Swollman				IEEE80211_FC0_SUBTYPE_SHIFT],
8442742Swollman		    ieee80211_chan2ieee(ic, ic->ic_curchan));
8452742Swollman	}
8462742Swollman#endif
8479908Swollman	IEEE80211_NODE_STAT(ni, tx_mgmt);
8482742Swollman
84919878Swollman	ret = ieee80211_raw_output(vap, ni, m, params);
8502742Swollman	IEEE80211_TX_UNLOCK(ic);
8512742Swollman	return (ret);
8522742Swollman}
8532742Swollman
8542742Swollman/*
85558787Sru * Send a null data frame to the specified node.  If the station
85658787Sru * is setup for QoS then a QoS Null Data frame is constructed.
85758787Sru * If this is a WDS station then a 4-address frame is constructed.
85858787Sru *
85958787Sru * NB: the caller is assumed to have setup a node reference
86058787Sru *     for use; this is necessary to deal with a race condition
86158787Sru *     when probing for inactive stations.  Like ieee80211_mgmt_output
86258787Sru *     we must cleanup any node reference on error;  however we
86358787Sru *     can safely just unref it as we know it will never be the
86458787Sru *     last reference to the node.
86514343Swollman */
86658787Sruint
86758787Sruieee80211_send_nulldata(struct ieee80211_node *ni)
86858787Sru{
8692742Swollman	struct ieee80211vap *vap = ni->ni_vap;
8702742Swollman	struct ieee80211com *ic = ni->ni_ic;
87158787Sru	struct mbuf *m;
8722742Swollman	struct ieee80211_frame *wh;
87319878Swollman	int hdrlen;
8742742Swollman	uint8_t *frm;
8752742Swollman	int ret;
8762742Swollman
8772742Swollman	if (vap->iv_state == IEEE80211_S_CAC) {
8782742Swollman		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
87919878Swollman		    ni, "block %s frame in CAC state", "null data");
8802742Swollman		ieee80211_unref_node(&ni);
8812742Swollman		vap->iv_stats.is_tx_badstate++;
8822742Swollman		return EIO;		/* XXX */
8832742Swollman	}
8842742Swollman
8852742Swollman	if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
8862742Swollman		hdrlen = sizeof(struct ieee80211_qosframe);
8872742Swollman	else
8882742Swollman		hdrlen = sizeof(struct ieee80211_frame);
8892742Swollman	/* NB: only WDS vap's get 4-address frames */
8902742Swollman	if (vap->iv_opmode == IEEE80211_M_WDS)
8912742Swollman		hdrlen += IEEE80211_ADDR_LEN;
8922742Swollman	if (ic->ic_flags & IEEE80211_F_DATAPAD)
89319878Swollman		hdrlen = roundup(hdrlen, sizeof(uint32_t));
8942742Swollman
8952742Swollman	m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
8962742Swollman	if (m == NULL) {
89714343Swollman		/* XXX debug msg */
89814343Swollman		ieee80211_unref_node(&ni);
89914343Swollman		vap->iv_stats.is_tx_nobuf++;
90014343Swollman		return ENOMEM;
90114343Swollman	}
90214343Swollman	KASSERT(M_LEADINGSPACE(m) >= hdrlen,
90314343Swollman	    ("leading space %zd", M_LEADINGSPACE(m)));
90458787Sru	M_PREPEND(m, hdrlen, M_NOWAIT);
90558787Sru	if (m == NULL) {
90658787Sru		/* NB: cannot happen */
90720094Swollman		ieee80211_free_node(ni);
90858787Sru		return ENOMEM;
90914343Swollman	}
9102742Swollman
9112742Swollman	IEEE80211_TX_LOCK(ic);
91258787Sru
9132742Swollman	wh = mtod(m, struct ieee80211_frame *);		/* NB: a little lie */
9142742Swollman	if (ni->ni_flags & IEEE80211_NODE_QOS) {
91558787Sru		const int tid = WME_AC_TO_TID(WME_AC_BE);
91658787Sru		uint8_t *qos;
91758787Sru
9182742Swollman		ieee80211_send_setup(ni, m,
91943543Swollman		    IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
92043543Swollman		    tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
92143543Swollman
92243543Swollman		if (vap->iv_opmode == IEEE80211_M_WDS)
92343543Swollman			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
92458787Sru		else
92558787Sru			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
92658787Sru		qos[0] = tid & IEEE80211_QOS_TID;
92743543Swollman		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
92843543Swollman			qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
92943543Swollman		qos[1] = 0;
93043543Swollman	} else {
93143543Swollman		ieee80211_send_setup(ni, m,
93243543Swollman		    IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
93343543Swollman		    IEEE80211_NONQOS_TID,
93458787Sru		    vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
93514343Swollman	}
93614343Swollman	if (vap->iv_opmode != IEEE80211_M_WDS) {
93714343Swollman		/* NB: power management bit is never sent by an AP */
93814343Swollman		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
93958787Sru		    vap->iv_opmode != IEEE80211_M_HOSTAP)
94017200Swollman			wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
94117200Swollman	}
94217200Swollman	m->m_len = m->m_pkthdr.len = hdrlen;
94317200Swollman	m->m_flags |= M_ENCAP;		/* mark encapsulated */
94417200Swollman
94517200Swollman	M_WME_SETAC(m, WME_AC_BE);
94617200Swollman
94758787Sru	IEEE80211_NODE_STAT(ni, tx_data);
9482742Swollman
9492742Swollman	IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
9502742Swollman	    "send %snull data frame on channel %u, pwr mgt %s",
9512742Swollman	    ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
95219878Swollman	    ieee80211_chan2ieee(ic, ic->ic_curchan),
9532742Swollman	    wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
9542742Swollman
9552742Swollman	ret = ieee80211_raw_output(vap, ni, m, NULL);
95658787Sru	IEEE80211_TX_UNLOCK(ic);
95714343Swollman	return (ret);
9582742Swollman}
95958787Sru
9602742Swollman/*
9612742Swollman * Assign priority to a frame based on any vlan tag assigned
9622742Swollman * to the station and/or any Diffserv setting in an IP header.
96314343Swollman * Finally, if an ACM policy is setup (in station mode) it's
96458787Sru * applied.
96517200Swollman */
96617200Swollmanint
96717200Swollmanieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
96817200Swollman{
96917200Swollman	const struct ether_header *eh = mtod(m, struct ether_header *);
97017200Swollman	int v_wme_ac, d_wme_ac, ac;
97158787Sru
97217200Swollman	/*
97317200Swollman	 * Always promote PAE/EAPOL frames to high priority.
97458787Sru	 */
97558787Sru	if (eh->ether_type == htons(ETHERTYPE_PAE)) {
97658787Sru		/* NB: mark so others don't need to check header */
97758787Sru		m->m_flags |= M_EAPOL;
97858787Sru		ac = WME_AC_VO;
97958787Sru		goto done;
98058787Sru	}
98158787Sru	/*
98258787Sru	 * Non-qos traffic goes to BE.
98358787Sru	 */
98458787Sru	if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
98558787Sru		ac = WME_AC_BE;
98658787Sru		goto done;
98758787Sru	}
98858787Sru
98958787Sru	/*
99058787Sru	 * If node has a vlan tag then all traffic
99158787Sru	 * to it must have a matching tag.
99258787Sru	 */
99358787Sru	v_wme_ac = 0;
99458787Sru	if (ni->ni_vlan != 0) {
99558787Sru		 if ((m->m_flags & M_VLANTAG) == 0) {
99658787Sru			IEEE80211_NODE_STAT(ni, tx_novlantag);
99758787Sru			return 1;
99858787Sru		}
99958787Sru		if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
100058787Sru		    EVL_VLANOFTAG(ni->ni_vlan)) {
100158787Sru			IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
100258787Sru			return 1;
100358787Sru		}
100458787Sru		/* map vlan priority to AC */
100558787Sru		v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
100658787Sru	}
100714343Swollman
100814343Swollman	/* XXX m_copydata may be too slow for fast path */
100958787Sru#ifdef INET
101058787Sru	if (eh->ether_type == htons(ETHERTYPE_IP)) {
101114343Swollman		uint8_t tos;
101214343Swollman		/*
101314343Swollman		 * IP frame, map the DSCP bits from the TOS field.
101417200Swollman		 */
101558787Sru		/* NB: ip header may not be in first mbuf */
101617200Swollman		m_copydata(m, sizeof(struct ether_header) +
101717200Swollman		    offsetof(struct ip, ip_tos), sizeof(tos), &tos);
101817200Swollman		tos >>= 5;		/* NB: ECN + low 3 bits of DSCP */
101917200Swollman		d_wme_ac = TID_TO_WME_AC(tos);
102017200Swollman	} else {
102117200Swollman#endif /* INET */
102258787Sru#ifdef INET6
102358787Sru	if (eh->ether_type == htons(ETHERTYPE_IPV6)) {
102458787Sru		uint32_t flow;
102558787Sru		uint8_t tos;
102658787Sru		/*
102758787Sru		 * IPv6 frame, map the DSCP bits from the traffic class field.
102858787Sru		 */
102958787Sru		m_copydata(m, sizeof(struct ether_header) +
103058787Sru		    offsetof(struct ip6_hdr, ip6_flow), sizeof(flow),
103158787Sru		    (caddr_t) &flow);
103258787Sru		tos = (uint8_t)(ntohl(flow) >> 20);
103358787Sru		tos >>= 5;		/* NB: ECN + low 3 bits of DSCP */
103458787Sru		d_wme_ac = TID_TO_WME_AC(tos);
103558787Sru	} else {
103658787Sru#endif /* INET6 */
103758787Sru		d_wme_ac = WME_AC_BE;
103858787Sru#ifdef INET6
103958787Sru	}
104058787Sru#endif
104158787Sru#ifdef INET
104258787Sru	}
104358787Sru#endif
104458787Sru	/*
104558787Sru	 * Use highest priority AC.
104658787Sru	 */
104758787Sru	if (v_wme_ac > d_wme_ac)
104858787Sru		ac = v_wme_ac;
104958787Sru	else
105058787Sru		ac = d_wme_ac;
105158787Sru
105258787Sru	/*
105358787Sru	 * Apply ACM policy.
105458787Sru	 */
105558787Sru	if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
105658787Sru		static const int acmap[4] = {
105758787Sru			WME_AC_BK,	/* WME_AC_BE */
105858787Sru			WME_AC_BK,	/* WME_AC_BK */
105958787Sru			WME_AC_BE,	/* WME_AC_VI */
106058787Sru			WME_AC_VI,	/* WME_AC_VO */
106158787Sru		};
106258787Sru		struct ieee80211com *ic = ni->ni_ic;
106358787Sru
106458787Sru		while (ac != WME_AC_BK &&
106558787Sru		    ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
106658787Sru			ac = acmap[ac];
106758787Sru	}
106858787Srudone:
106958787Sru	M_WME_SETAC(m, ac);
107058787Sru	return 0;
107158787Sru}
107258787Sru
107358787Sru/*
107458787Sru * Insure there is sufficient contiguous space to encapsulate the
107558787Sru * 802.11 data frame.  If room isn't already there, arrange for it.
107658787Sru * Drivers and cipher modules assume we have done the necessary work
107758787Sru * and fail rudely if they don't find the space they need.
107858787Sru */
107958787Srustruct mbuf *
108058787Sruieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
108158787Sru	struct ieee80211_key *key, struct mbuf *m)
108258787Sru{
108358787Sru#define	TO_BE_RECLAIMED	(sizeof(struct ether_header) - sizeof(struct llc))
108458787Sru	int needed_space = vap->iv_ic->ic_headroom + hdrsize;
108558787Sru
108658787Sru	if (key != NULL) {
108758787Sru		/* XXX belongs in crypto code? */
108858787Sru		needed_space += key->wk_cipher->ic_header;
108958787Sru		/* XXX frags */
109058787Sru		/*
109158787Sru		 * When crypto is being done in the host we must insure
109258787Sru		 * the data are writable for the cipher routines; clone
109358787Sru		 * a writable mbuf chain.
109458787Sru		 * XXX handle SWMIC specially
109558787Sru		 */
109658787Sru		if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
109758787Sru			m = m_unshare(m, M_NOWAIT);
109858787Sru			if (m == NULL) {
109958787Sru				IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
110058787Sru				    "%s: cannot get writable mbuf\n", __func__);
110158787Sru				vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
110258787Sru				return NULL;
110358787Sru			}
110458787Sru		}
110558787Sru	}
110658787Sru	/*
110758787Sru	 * We know we are called just before stripping an Ethernet
110858787Sru	 * header and prepending an LLC header.  This means we know
110958787Sru	 * there will be
111058787Sru	 *	sizeof(struct ether_header) - sizeof(struct llc)
111158787Sru	 * bytes recovered to which we need additional space for the
111258787Sru	 * 802.11 header and any crypto header.
1113	 */
1114	/* XXX check trailing space and copy instead? */
1115	if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
1116		struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
1117		if (n == NULL) {
1118			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
1119			    "%s: cannot expand storage\n", __func__);
1120			vap->iv_stats.is_tx_nobuf++;
1121			m_freem(m);
1122			return NULL;
1123		}
1124		KASSERT(needed_space <= MHLEN,
1125		    ("not enough room, need %u got %d\n", needed_space, MHLEN));
1126		/*
1127		 * Setup new mbuf to have leading space to prepend the
1128		 * 802.11 header and any crypto header bits that are
1129		 * required (the latter are added when the driver calls
1130		 * back to ieee80211_crypto_encap to do crypto encapsulation).
1131		 */
1132		/* NB: must be first 'cuz it clobbers m_data */
1133		m_move_pkthdr(n, m);
1134		n->m_len = 0;			/* NB: m_gethdr does not set */
1135		n->m_data += needed_space;
1136		/*
1137		 * Pull up Ethernet header to create the expected layout.
1138		 * We could use m_pullup but that's overkill (i.e. we don't
1139		 * need the actual data) and it cannot fail so do it inline
1140		 * for speed.
1141		 */
1142		/* NB: struct ether_header is known to be contiguous */
1143		n->m_len += sizeof(struct ether_header);
1144		m->m_len -= sizeof(struct ether_header);
1145		m->m_data += sizeof(struct ether_header);
1146		/*
1147		 * Replace the head of the chain.
1148		 */
1149		n->m_next = m;
1150		m = n;
1151	}
1152	return m;
1153#undef TO_BE_RECLAIMED
1154}
1155
1156/*
1157 * Return the transmit key to use in sending a unicast frame.
1158 * If a unicast key is set we use that.  When no unicast key is set
1159 * we fall back to the default transmit key.
1160 */
1161static __inline struct ieee80211_key *
1162ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
1163	struct ieee80211_node *ni)
1164{
1165	if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
1166		if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1167		    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1168			return NULL;
1169		return &vap->iv_nw_keys[vap->iv_def_txkey];
1170	} else {
1171		return &ni->ni_ucastkey;
1172	}
1173}
1174
1175/*
1176 * Return the transmit key to use in sending a multicast frame.
1177 * Multicast traffic always uses the group key which is installed as
1178 * the default tx key.
1179 */
1180static __inline struct ieee80211_key *
1181ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
1182	struct ieee80211_node *ni)
1183{
1184	if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1185	    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1186		return NULL;
1187	return &vap->iv_nw_keys[vap->iv_def_txkey];
1188}
1189
1190/*
1191 * Encapsulate an outbound data frame.  The mbuf chain is updated.
1192 * If an error is encountered NULL is returned.  The caller is required
1193 * to provide a node reference and pullup the ethernet header in the
1194 * first mbuf.
1195 *
1196 * NB: Packet is assumed to be processed by ieee80211_classify which
1197 *     marked EAPOL frames w/ M_EAPOL.
1198 */
1199struct mbuf *
1200ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
1201    struct mbuf *m)
1202{
1203#define	WH4(wh)	((struct ieee80211_frame_addr4 *)(wh))
1204#define MC01(mc)	((struct ieee80211_meshcntl_ae01 *)mc)
1205	struct ieee80211com *ic = ni->ni_ic;
1206#ifdef IEEE80211_SUPPORT_MESH
1207	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1208	struct ieee80211_meshcntl_ae10 *mc;
1209	struct ieee80211_mesh_route *rt = NULL;
1210	int dir = -1;
1211#endif
1212	struct ether_header eh;
1213	struct ieee80211_frame *wh;
1214	struct ieee80211_key *key;
1215	struct llc *llc;
1216	int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr;
1217	ieee80211_seq seqno;
1218	int meshhdrsize, meshae;
1219	uint8_t *qos;
1220
1221	IEEE80211_TX_LOCK_ASSERT(ic);
1222
1223	/*
1224	 * Copy existing Ethernet header to a safe place.  The
1225	 * rest of the code assumes it's ok to strip it when
1226	 * reorganizing state for the final encapsulation.
1227	 */
1228	KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
1229	ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
1230
1231	/*
1232	 * Insure space for additional headers.  First identify
1233	 * transmit key to use in calculating any buffer adjustments
1234	 * required.  This is also used below to do privacy
1235	 * encapsulation work.  Then calculate the 802.11 header
1236	 * size and any padding required by the driver.
1237	 *
1238	 * Note key may be NULL if we fall back to the default
1239	 * transmit key and that is not set.  In that case the
1240	 * buffer may not be expanded as needed by the cipher
1241	 * routines, but they will/should discard it.
1242	 */
1243	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1244		if (vap->iv_opmode == IEEE80211_M_STA ||
1245		    !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
1246		    (vap->iv_opmode == IEEE80211_M_WDS &&
1247		     (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
1248			key = ieee80211_crypto_getucastkey(vap, ni);
1249		else
1250			key = ieee80211_crypto_getmcastkey(vap, ni);
1251		if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
1252			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
1253			    eh.ether_dhost,
1254			    "no default transmit key (%s) deftxkey %u",
1255			    __func__, vap->iv_def_txkey);
1256			vap->iv_stats.is_tx_nodefkey++;
1257			goto bad;
1258		}
1259	} else
1260		key = NULL;
1261	/*
1262	 * XXX Some ap's don't handle QoS-encapsulated EAPOL
1263	 * frames so suppress use.  This may be an issue if other
1264	 * ap's require all data frames to be QoS-encapsulated
1265	 * once negotiated in which case we'll need to make this
1266	 * configurable.
1267	 * NB: mesh data frames are QoS.
1268	 */
1269	addqos = ((ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) ||
1270	    (vap->iv_opmode == IEEE80211_M_MBSS)) &&
1271	    (m->m_flags & M_EAPOL) == 0;
1272	if (addqos)
1273		hdrsize = sizeof(struct ieee80211_qosframe);
1274	else
1275		hdrsize = sizeof(struct ieee80211_frame);
1276#ifdef IEEE80211_SUPPORT_MESH
1277	if (vap->iv_opmode == IEEE80211_M_MBSS) {
1278		/*
1279		 * Mesh data frames are encapsulated according to the
1280		 * rules of Section 11B.8.5 (p.139 of D3.0 spec).
1281		 * o Group Addressed data (aka multicast) originating
1282		 *   at the local sta are sent w/ 3-address format and
1283		 *   address extension mode 00
1284		 * o Individually Addressed data (aka unicast) originating
1285		 *   at the local sta are sent w/ 4-address format and
1286		 *   address extension mode 00
1287		 * o Group Addressed data forwarded from a non-mesh sta are
1288		 *   sent w/ 3-address format and address extension mode 01
1289		 * o Individually Address data from another sta are sent
1290		 *   w/ 4-address format and address extension mode 10
1291		 */
1292		is4addr = 0;		/* NB: don't use, disable */
1293		if (!IEEE80211_IS_MULTICAST(eh.ether_dhost)) {
1294			rt = ieee80211_mesh_rt_find(vap, eh.ether_dhost);
1295			KASSERT(rt != NULL, ("route is NULL"));
1296			dir = IEEE80211_FC1_DIR_DSTODS;
1297			hdrsize += IEEE80211_ADDR_LEN;
1298			if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
1299				if (IEEE80211_ADDR_EQ(rt->rt_mesh_gate,
1300				    vap->iv_myaddr)) {
1301					IEEE80211_NOTE_MAC(vap,
1302					    IEEE80211_MSG_MESH,
1303					    eh.ether_dhost,
1304					    "%s", "trying to send to ourself");
1305					goto bad;
1306				}
1307				meshae = IEEE80211_MESH_AE_10;
1308				meshhdrsize =
1309				    sizeof(struct ieee80211_meshcntl_ae10);
1310			} else {
1311				meshae = IEEE80211_MESH_AE_00;
1312				meshhdrsize =
1313				    sizeof(struct ieee80211_meshcntl);
1314			}
1315		} else {
1316			dir = IEEE80211_FC1_DIR_FROMDS;
1317			if (!IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) {
1318				/* proxy group */
1319				meshae = IEEE80211_MESH_AE_01;
1320				meshhdrsize =
1321				    sizeof(struct ieee80211_meshcntl_ae01);
1322			} else {
1323				/* group */
1324				meshae = IEEE80211_MESH_AE_00;
1325				meshhdrsize = sizeof(struct ieee80211_meshcntl);
1326			}
1327		}
1328	} else {
1329#endif
1330		/*
1331		 * 4-address frames need to be generated for:
1332		 * o packets sent through a WDS vap (IEEE80211_M_WDS)
1333		 * o packets sent through a vap marked for relaying
1334		 *   (e.g. a station operating with dynamic WDS)
1335		 */
1336		is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
1337		    ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
1338		     !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
1339		if (is4addr)
1340			hdrsize += IEEE80211_ADDR_LEN;
1341		meshhdrsize = meshae = 0;
1342#ifdef IEEE80211_SUPPORT_MESH
1343	}
1344#endif
1345	/*
1346	 * Honor driver DATAPAD requirement.
1347	 */
1348	if (ic->ic_flags & IEEE80211_F_DATAPAD)
1349		hdrspace = roundup(hdrsize, sizeof(uint32_t));
1350	else
1351		hdrspace = hdrsize;
1352
1353	if (__predict_true((m->m_flags & M_FF) == 0)) {
1354		/*
1355		 * Normal frame.
1356		 */
1357		m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m);
1358		if (m == NULL) {
1359			/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
1360			goto bad;
1361		}
1362		/* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
1363		m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
1364		llc = mtod(m, struct llc *);
1365		llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1366		llc->llc_control = LLC_UI;
1367		llc->llc_snap.org_code[0] = 0;
1368		llc->llc_snap.org_code[1] = 0;
1369		llc->llc_snap.org_code[2] = 0;
1370		llc->llc_snap.ether_type = eh.ether_type;
1371	} else {
1372#ifdef IEEE80211_SUPPORT_SUPERG
1373		/*
1374		 * Aggregated frame.
1375		 */
1376		m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key);
1377		if (m == NULL)
1378#endif
1379			goto bad;
1380	}
1381	datalen = m->m_pkthdr.len;		/* NB: w/o 802.11 header */
1382
1383	M_PREPEND(m, hdrspace + meshhdrsize, M_NOWAIT);
1384	if (m == NULL) {
1385		vap->iv_stats.is_tx_nobuf++;
1386		goto bad;
1387	}
1388	wh = mtod(m, struct ieee80211_frame *);
1389	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1390	*(uint16_t *)wh->i_dur = 0;
1391	qos = NULL;	/* NB: quiet compiler */
1392	if (is4addr) {
1393		wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1394		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1395		IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1396		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1397		IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1398	} else switch (vap->iv_opmode) {
1399	case IEEE80211_M_STA:
1400		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1401		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
1402		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1403		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1404		break;
1405	case IEEE80211_M_IBSS:
1406	case IEEE80211_M_AHDEMO:
1407		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1408		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1409		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1410		/*
1411		 * NB: always use the bssid from iv_bss as the
1412		 *     neighbor's may be stale after an ibss merge
1413		 */
1414		IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
1415		break;
1416	case IEEE80211_M_HOSTAP:
1417		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1418		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1419		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
1420		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1421		break;
1422#ifdef IEEE80211_SUPPORT_MESH
1423	case IEEE80211_M_MBSS:
1424		/* NB: offset by hdrspace to deal with DATAPAD */
1425		mc = (struct ieee80211_meshcntl_ae10 *)
1426		     (mtod(m, uint8_t *) + hdrspace);
1427		wh->i_fc[1] = dir;
1428		switch (meshae) {
1429		case IEEE80211_MESH_AE_00:	/* no proxy */
1430			mc->mc_flags = 0;
1431			if (dir == IEEE80211_FC1_DIR_DSTODS) { /* ucast */
1432				IEEE80211_ADDR_COPY(wh->i_addr1,
1433				    ni->ni_macaddr);
1434				IEEE80211_ADDR_COPY(wh->i_addr2,
1435				    vap->iv_myaddr);
1436				IEEE80211_ADDR_COPY(wh->i_addr3,
1437				    eh.ether_dhost);
1438				IEEE80211_ADDR_COPY(WH4(wh)->i_addr4,
1439				    eh.ether_shost);
1440				qos =((struct ieee80211_qosframe_addr4 *)
1441				    wh)->i_qos;
1442			} else if (dir == IEEE80211_FC1_DIR_FROMDS) {
1443				 /* mcast */
1444				IEEE80211_ADDR_COPY(wh->i_addr1,
1445				    eh.ether_dhost);
1446				IEEE80211_ADDR_COPY(wh->i_addr2,
1447				    vap->iv_myaddr);
1448				IEEE80211_ADDR_COPY(wh->i_addr3,
1449				    eh.ether_shost);
1450				qos = ((struct ieee80211_qosframe *)
1451				    wh)->i_qos;
1452			}
1453			break;
1454		case IEEE80211_MESH_AE_01:	/* mcast, proxy */
1455			wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1456			IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1457			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1458			IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr);
1459			mc->mc_flags = 1;
1460			IEEE80211_ADDR_COPY(MC01(mc)->mc_addr4,
1461			    eh.ether_shost);
1462			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1463			break;
1464		case IEEE80211_MESH_AE_10:	/* ucast, proxy */
1465			KASSERT(rt != NULL, ("route is NULL"));
1466			IEEE80211_ADDR_COPY(wh->i_addr1, rt->rt_nexthop);
1467			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1468			IEEE80211_ADDR_COPY(wh->i_addr3, rt->rt_mesh_gate);
1469			IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr);
1470			mc->mc_flags = IEEE80211_MESH_AE_10;
1471			IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_dhost);
1472			IEEE80211_ADDR_COPY(mc->mc_addr6, eh.ether_shost);
1473			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1474			break;
1475		default:
1476			KASSERT(0, ("meshae %d", meshae));
1477			break;
1478		}
1479		mc->mc_ttl = ms->ms_ttl;
1480		ms->ms_seq++;
1481		LE_WRITE_4(mc->mc_seq, ms->ms_seq);
1482		break;
1483#endif
1484	case IEEE80211_M_WDS:		/* NB: is4addr should always be true */
1485	default:
1486		goto bad;
1487	}
1488	if (m->m_flags & M_MORE_DATA)
1489		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
1490	if (addqos) {
1491		int ac, tid;
1492
1493		if (is4addr) {
1494			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1495		/* NB: mesh case handled earlier */
1496		} else if (vap->iv_opmode != IEEE80211_M_MBSS)
1497			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1498		ac = M_WME_GETAC(m);
1499		/* map from access class/queue to 11e header priorty value */
1500		tid = WME_AC_TO_TID(ac);
1501		qos[0] = tid & IEEE80211_QOS_TID;
1502		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
1503			qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1504#ifdef IEEE80211_SUPPORT_MESH
1505		if (vap->iv_opmode == IEEE80211_M_MBSS)
1506			qos[1] = IEEE80211_QOS_MC;
1507		else
1508#endif
1509			qos[1] = 0;
1510		wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
1511
1512		if ((m->m_flags & M_AMPDU_MPDU) == 0) {
1513			/*
1514			 * NB: don't assign a sequence # to potential
1515			 * aggregates; we expect this happens at the
1516			 * point the frame comes off any aggregation q
1517			 * as otherwise we may introduce holes in the
1518			 * BA sequence space and/or make window accouting
1519			 * more difficult.
1520			 *
1521			 * XXX may want to control this with a driver
1522			 * capability; this may also change when we pull
1523			 * aggregation up into net80211
1524			 */
1525			seqno = ni->ni_txseqs[tid]++;
1526			*(uint16_t *)wh->i_seq =
1527			    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1528			M_SEQNO_SET(m, seqno);
1529		}
1530	} else {
1531		seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
1532		*(uint16_t *)wh->i_seq =
1533		    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1534		M_SEQNO_SET(m, seqno);
1535	}
1536
1537
1538	/* check if xmit fragmentation is required */
1539	txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
1540	    !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1541	    (vap->iv_caps & IEEE80211_C_TXFRAG) &&
1542	    (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
1543	if (key != NULL) {
1544		/*
1545		 * IEEE 802.1X: send EAPOL frames always in the clear.
1546		 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
1547		 */
1548		if ((m->m_flags & M_EAPOL) == 0 ||
1549		    ((vap->iv_flags & IEEE80211_F_WPA) &&
1550		     (vap->iv_opmode == IEEE80211_M_STA ?
1551		      !IEEE80211_KEY_UNDEFINED(key) :
1552		      !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
1553			wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
1554			if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1555				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1556				    eh.ether_dhost,
1557				    "%s", "enmic failed, discard frame");
1558				vap->iv_stats.is_crypto_enmicfail++;
1559				goto bad;
1560			}
1561		}
1562	}
1563	if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1564	    key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
1565		goto bad;
1566
1567	m->m_flags |= M_ENCAP;		/* mark encapsulated */
1568
1569	IEEE80211_NODE_STAT(ni, tx_data);
1570	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1571		IEEE80211_NODE_STAT(ni, tx_mcast);
1572		m->m_flags |= M_MCAST;
1573	} else
1574		IEEE80211_NODE_STAT(ni, tx_ucast);
1575	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
1576
1577	return m;
1578bad:
1579	if (m != NULL)
1580		m_freem(m);
1581	return NULL;
1582#undef WH4
1583#undef MC01
1584}
1585
1586void
1587ieee80211_free_mbuf(struct mbuf *m)
1588{
1589	struct mbuf *next;
1590
1591	if (m == NULL)
1592		return;
1593
1594	do {
1595		next = m->m_nextpkt;
1596		m->m_nextpkt = NULL;
1597		m_freem(m);
1598	} while ((m = next) != NULL);
1599}
1600
1601/*
1602 * Fragment the frame according to the specified mtu.
1603 * The size of the 802.11 header (w/o padding) is provided
1604 * so we don't need to recalculate it.  We create a new
1605 * mbuf for each fragment and chain it through m_nextpkt;
1606 * we might be able to optimize this by reusing the original
1607 * packet's mbufs but that is significantly more complicated.
1608 */
1609static int
1610ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
1611	u_int hdrsize, u_int ciphdrsize, u_int mtu)
1612{
1613	struct ieee80211com *ic = vap->iv_ic;
1614	struct ieee80211_frame *wh, *whf;
1615	struct mbuf *m, *prev;
1616	u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
1617	u_int hdrspace;
1618
1619	KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
1620	KASSERT(m0->m_pkthdr.len > mtu,
1621		("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
1622
1623	/*
1624	 * Honor driver DATAPAD requirement.
1625	 */
1626	if (ic->ic_flags & IEEE80211_F_DATAPAD)
1627		hdrspace = roundup(hdrsize, sizeof(uint32_t));
1628	else
1629		hdrspace = hdrsize;
1630
1631	wh = mtod(m0, struct ieee80211_frame *);
1632	/* NB: mark the first frag; it will be propagated below */
1633	wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
1634	totalhdrsize = hdrspace + ciphdrsize;
1635	fragno = 1;
1636	off = mtu - ciphdrsize;
1637	remainder = m0->m_pkthdr.len - off;
1638	prev = m0;
1639	do {
1640		fragsize = totalhdrsize + remainder;
1641		if (fragsize > mtu)
1642			fragsize = mtu;
1643		/* XXX fragsize can be >2048! */
1644		KASSERT(fragsize < MCLBYTES,
1645			("fragment size %u too big!", fragsize));
1646		if (fragsize > MHLEN)
1647			m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1648		else
1649			m = m_gethdr(M_NOWAIT, MT_DATA);
1650		if (m == NULL)
1651			goto bad;
1652		/* leave room to prepend any cipher header */
1653		m_align(m, fragsize - ciphdrsize);
1654
1655		/*
1656		 * Form the header in the fragment.  Note that since
1657		 * we mark the first fragment with the MORE_FRAG bit
1658		 * it automatically is propagated to each fragment; we
1659		 * need only clear it on the last fragment (done below).
1660		 * NB: frag 1+ dont have Mesh Control field present.
1661		 */
1662		whf = mtod(m, struct ieee80211_frame *);
1663		memcpy(whf, wh, hdrsize);
1664#ifdef IEEE80211_SUPPORT_MESH
1665		if (vap->iv_opmode == IEEE80211_M_MBSS) {
1666			if (IEEE80211_IS_DSTODS(wh))
1667				((struct ieee80211_qosframe_addr4 *)
1668				    whf)->i_qos[1] &= ~IEEE80211_QOS_MC;
1669			else
1670				((struct ieee80211_qosframe *)
1671				    whf)->i_qos[1] &= ~IEEE80211_QOS_MC;
1672		}
1673#endif
1674		*(uint16_t *)&whf->i_seq[0] |= htole16(
1675			(fragno & IEEE80211_SEQ_FRAG_MASK) <<
1676				IEEE80211_SEQ_FRAG_SHIFT);
1677		fragno++;
1678
1679		payload = fragsize - totalhdrsize;
1680		/* NB: destination is known to be contiguous */
1681
1682		m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrspace);
1683		m->m_len = hdrspace + payload;
1684		m->m_pkthdr.len = hdrspace + payload;
1685		m->m_flags |= M_FRAG;
1686
1687		/* chain up the fragment */
1688		prev->m_nextpkt = m;
1689		prev = m;
1690
1691		/* deduct fragment just formed */
1692		remainder -= payload;
1693		off += payload;
1694	} while (remainder != 0);
1695
1696	/* set the last fragment */
1697	m->m_flags |= M_LASTFRAG;
1698	whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
1699
1700	/* strip first mbuf now that everything has been copied */
1701	m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
1702	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
1703
1704	vap->iv_stats.is_tx_fragframes++;
1705	vap->iv_stats.is_tx_frags += fragno-1;
1706
1707	return 1;
1708bad:
1709	/* reclaim fragments but leave original frame for caller to free */
1710	ieee80211_free_mbuf(m0->m_nextpkt);
1711	m0->m_nextpkt = NULL;
1712	return 0;
1713}
1714
1715/*
1716 * Add a supported rates element id to a frame.
1717 */
1718uint8_t *
1719ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
1720{
1721	int nrates;
1722
1723	*frm++ = IEEE80211_ELEMID_RATES;
1724	nrates = rs->rs_nrates;
1725	if (nrates > IEEE80211_RATE_SIZE)
1726		nrates = IEEE80211_RATE_SIZE;
1727	*frm++ = nrates;
1728	memcpy(frm, rs->rs_rates, nrates);
1729	return frm + nrates;
1730}
1731
1732/*
1733 * Add an extended supported rates element id to a frame.
1734 */
1735uint8_t *
1736ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
1737{
1738	/*
1739	 * Add an extended supported rates element if operating in 11g mode.
1740	 */
1741	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
1742		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
1743		*frm++ = IEEE80211_ELEMID_XRATES;
1744		*frm++ = nrates;
1745		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
1746		frm += nrates;
1747	}
1748	return frm;
1749}
1750
1751/*
1752 * Add an ssid element to a frame.
1753 */
1754uint8_t *
1755ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
1756{
1757	*frm++ = IEEE80211_ELEMID_SSID;
1758	*frm++ = len;
1759	memcpy(frm, ssid, len);
1760	return frm + len;
1761}
1762
1763/*
1764 * Add an erp element to a frame.
1765 */
1766static uint8_t *
1767ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic)
1768{
1769	uint8_t erp;
1770
1771	*frm++ = IEEE80211_ELEMID_ERP;
1772	*frm++ = 1;
1773	erp = 0;
1774	if (ic->ic_nonerpsta != 0)
1775		erp |= IEEE80211_ERP_NON_ERP_PRESENT;
1776	if (ic->ic_flags & IEEE80211_F_USEPROT)
1777		erp |= IEEE80211_ERP_USE_PROTECTION;
1778	if (ic->ic_flags & IEEE80211_F_USEBARKER)
1779		erp |= IEEE80211_ERP_LONG_PREAMBLE;
1780	*frm++ = erp;
1781	return frm;
1782}
1783
1784/*
1785 * Add a CFParams element to a frame.
1786 */
1787static uint8_t *
1788ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
1789{
1790#define	ADDSHORT(frm, v) do {	\
1791	LE_WRITE_2(frm, v);	\
1792	frm += 2;		\
1793} while (0)
1794	*frm++ = IEEE80211_ELEMID_CFPARMS;
1795	*frm++ = 6;
1796	*frm++ = 0;		/* CFP count */
1797	*frm++ = 2;		/* CFP period */
1798	ADDSHORT(frm, 0);	/* CFP MaxDuration (TU) */
1799	ADDSHORT(frm, 0);	/* CFP CurRemaining (TU) */
1800	return frm;
1801#undef ADDSHORT
1802}
1803
1804static __inline uint8_t *
1805add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
1806{
1807	memcpy(frm, ie->ie_data, ie->ie_len);
1808	return frm + ie->ie_len;
1809}
1810
1811static __inline uint8_t *
1812add_ie(uint8_t *frm, const uint8_t *ie)
1813{
1814	memcpy(frm, ie, 2 + ie[1]);
1815	return frm + 2 + ie[1];
1816}
1817
1818#define	WME_OUI_BYTES		0x00, 0x50, 0xf2
1819/*
1820 * Add a WME information element to a frame.
1821 */
1822uint8_t *
1823ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme)
1824{
1825	static const struct ieee80211_wme_info info = {
1826		.wme_id		= IEEE80211_ELEMID_VENDOR,
1827		.wme_len	= sizeof(struct ieee80211_wme_info) - 2,
1828		.wme_oui	= { WME_OUI_BYTES },
1829		.wme_type	= WME_OUI_TYPE,
1830		.wme_subtype	= WME_INFO_OUI_SUBTYPE,
1831		.wme_version	= WME_VERSION,
1832		.wme_info	= 0,
1833	};
1834	memcpy(frm, &info, sizeof(info));
1835	return frm + sizeof(info);
1836}
1837
1838/*
1839 * Add a WME parameters element to a frame.
1840 */
1841static uint8_t *
1842ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme)
1843{
1844#define	SM(_v, _f)	(((_v) << _f##_S) & _f)
1845#define	ADDSHORT(frm, v) do {	\
1846	LE_WRITE_2(frm, v);	\
1847	frm += 2;		\
1848} while (0)
1849	/* NB: this works 'cuz a param has an info at the front */
1850	static const struct ieee80211_wme_info param = {
1851		.wme_id		= IEEE80211_ELEMID_VENDOR,
1852		.wme_len	= sizeof(struct ieee80211_wme_param) - 2,
1853		.wme_oui	= { WME_OUI_BYTES },
1854		.wme_type	= WME_OUI_TYPE,
1855		.wme_subtype	= WME_PARAM_OUI_SUBTYPE,
1856		.wme_version	= WME_VERSION,
1857	};
1858	int i;
1859
1860	memcpy(frm, &param, sizeof(param));
1861	frm += __offsetof(struct ieee80211_wme_info, wme_info);
1862	*frm++ = wme->wme_bssChanParams.cap_info;	/* AC info */
1863	*frm++ = 0;					/* reserved field */
1864	for (i = 0; i < WME_NUM_AC; i++) {
1865		const struct wmeParams *ac =
1866		       &wme->wme_bssChanParams.cap_wmeParams[i];
1867		*frm++ = SM(i, WME_PARAM_ACI)
1868		       | SM(ac->wmep_acm, WME_PARAM_ACM)
1869		       | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
1870		       ;
1871		*frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
1872		       | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
1873		       ;
1874		ADDSHORT(frm, ac->wmep_txopLimit);
1875	}
1876	return frm;
1877#undef SM
1878#undef ADDSHORT
1879}
1880#undef WME_OUI_BYTES
1881
1882/*
1883 * Add an 11h Power Constraint element to a frame.
1884 */
1885static uint8_t *
1886ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
1887{
1888	const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
1889	/* XXX per-vap tx power limit? */
1890	int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
1891
1892	frm[0] = IEEE80211_ELEMID_PWRCNSTR;
1893	frm[1] = 1;
1894	frm[2] = c->ic_maxregpower > limit ?  c->ic_maxregpower - limit : 0;
1895	return frm + 3;
1896}
1897
1898/*
1899 * Add an 11h Power Capability element to a frame.
1900 */
1901static uint8_t *
1902ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
1903{
1904	frm[0] = IEEE80211_ELEMID_PWRCAP;
1905	frm[1] = 2;
1906	frm[2] = c->ic_minpower;
1907	frm[3] = c->ic_maxpower;
1908	return frm + 4;
1909}
1910
1911/*
1912 * Add an 11h Supported Channels element to a frame.
1913 */
1914static uint8_t *
1915ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
1916{
1917	static const int ielen = 26;
1918
1919	frm[0] = IEEE80211_ELEMID_SUPPCHAN;
1920	frm[1] = ielen;
1921	/* XXX not correct */
1922	memcpy(frm+2, ic->ic_chan_avail, ielen);
1923	return frm + 2 + ielen;
1924}
1925
1926/*
1927 * Add an 11h Quiet time element to a frame.
1928 */
1929static uint8_t *
1930ieee80211_add_quiet(uint8_t *frm, struct ieee80211vap *vap)
1931{
1932	struct ieee80211_quiet_ie *quiet = (struct ieee80211_quiet_ie *) frm;
1933
1934	quiet->quiet_ie = IEEE80211_ELEMID_QUIET;
1935	quiet->len = 6;
1936	if (vap->iv_quiet_count_value == 1)
1937		vap->iv_quiet_count_value = vap->iv_quiet_count;
1938	else if (vap->iv_quiet_count_value > 1)
1939		vap->iv_quiet_count_value--;
1940
1941	if (vap->iv_quiet_count_value == 0) {
1942		/* value 0 is reserved as per 802.11h standerd */
1943		vap->iv_quiet_count_value = 1;
1944	}
1945
1946	quiet->tbttcount = vap->iv_quiet_count_value;
1947	quiet->period = vap->iv_quiet_period;
1948	quiet->duration = htole16(vap->iv_quiet_duration);
1949	quiet->offset = htole16(vap->iv_quiet_offset);
1950	return frm + sizeof(*quiet);
1951}
1952
1953/*
1954 * Add an 11h Channel Switch Announcement element to a frame.
1955 * Note that we use the per-vap CSA count to adjust the global
1956 * counter so we can use this routine to form probe response
1957 * frames and get the current count.
1958 */
1959static uint8_t *
1960ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
1961{
1962	struct ieee80211com *ic = vap->iv_ic;
1963	struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
1964
1965	csa->csa_ie = IEEE80211_ELEMID_CSA;
1966	csa->csa_len = 3;
1967	csa->csa_mode = 1;		/* XXX force quiet on channel */
1968	csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
1969	csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
1970	return frm + sizeof(*csa);
1971}
1972
1973/*
1974 * Add an 11h country information element to a frame.
1975 */
1976static uint8_t *
1977ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
1978{
1979
1980	if (ic->ic_countryie == NULL ||
1981	    ic->ic_countryie_chan != ic->ic_bsschan) {
1982		/*
1983		 * Handle lazy construction of ie.  This is done on
1984		 * first use and after a channel change that requires
1985		 * re-calculation.
1986		 */
1987		if (ic->ic_countryie != NULL)
1988			IEEE80211_FREE(ic->ic_countryie, M_80211_NODE_IE);
1989		ic->ic_countryie = ieee80211_alloc_countryie(ic);
1990		if (ic->ic_countryie == NULL)
1991			return frm;
1992		ic->ic_countryie_chan = ic->ic_bsschan;
1993	}
1994	return add_appie(frm, ic->ic_countryie);
1995}
1996
1997uint8_t *
1998ieee80211_add_wpa(uint8_t *frm, const struct ieee80211vap *vap)
1999{
2000	if (vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL)
2001		return (add_ie(frm, vap->iv_wpa_ie));
2002	else {
2003		/* XXX else complain? */
2004		return (frm);
2005	}
2006}
2007
2008uint8_t *
2009ieee80211_add_rsn(uint8_t *frm, const struct ieee80211vap *vap)
2010{
2011	if (vap->iv_flags & IEEE80211_F_WPA2 && vap->iv_rsn_ie != NULL)
2012		return (add_ie(frm, vap->iv_rsn_ie));
2013	else {
2014		/* XXX else complain? */
2015		return (frm);
2016	}
2017}
2018
2019uint8_t *
2020ieee80211_add_qos(uint8_t *frm, const struct ieee80211_node *ni)
2021{
2022	if (ni->ni_flags & IEEE80211_NODE_QOS) {
2023		*frm++ = IEEE80211_ELEMID_QOS;
2024		*frm++ = 1;
2025		*frm++ = 0;
2026	}
2027
2028	return (frm);
2029}
2030
2031/*
2032 * Send a probe request frame with the specified ssid
2033 * and any optional information element data.
2034 */
2035int
2036ieee80211_send_probereq(struct ieee80211_node *ni,
2037	const uint8_t sa[IEEE80211_ADDR_LEN],
2038	const uint8_t da[IEEE80211_ADDR_LEN],
2039	const uint8_t bssid[IEEE80211_ADDR_LEN],
2040	const uint8_t *ssid, size_t ssidlen)
2041{
2042	struct ieee80211vap *vap = ni->ni_vap;
2043	struct ieee80211com *ic = ni->ni_ic;
2044	const struct ieee80211_txparam *tp;
2045	struct ieee80211_bpf_params params;
2046	struct ieee80211_frame *wh;
2047	const struct ieee80211_rateset *rs;
2048	struct mbuf *m;
2049	uint8_t *frm;
2050	int ret;
2051
2052	if (vap->iv_state == IEEE80211_S_CAC) {
2053		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
2054		    "block %s frame in CAC state", "probe request");
2055		vap->iv_stats.is_tx_badstate++;
2056		return EIO;		/* XXX */
2057	}
2058
2059	/*
2060	 * Hold a reference on the node so it doesn't go away until after
2061	 * the xmit is complete all the way in the driver.  On error we
2062	 * will remove our reference.
2063	 */
2064	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2065		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2066		__func__, __LINE__,
2067		ni, ether_sprintf(ni->ni_macaddr),
2068		ieee80211_node_refcnt(ni)+1);
2069	ieee80211_ref_node(ni);
2070
2071	/*
2072	 * prreq frame format
2073	 *	[tlv] ssid
2074	 *	[tlv] supported rates
2075	 *	[tlv] RSN (optional)
2076	 *	[tlv] extended supported rates
2077	 *	[tlv] WPA (optional)
2078	 *	[tlv] user-specified ie's
2079	 */
2080	m = ieee80211_getmgtframe(&frm,
2081		 ic->ic_headroom + sizeof(struct ieee80211_frame),
2082	       	 2 + IEEE80211_NWID_LEN
2083	       + 2 + IEEE80211_RATE_SIZE
2084	       + sizeof(struct ieee80211_ie_wpa)
2085	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2086	       + sizeof(struct ieee80211_ie_wpa)
2087	       + (vap->iv_appie_probereq != NULL ?
2088		   vap->iv_appie_probereq->ie_len : 0)
2089	);
2090	if (m == NULL) {
2091		vap->iv_stats.is_tx_nobuf++;
2092		ieee80211_free_node(ni);
2093		return ENOMEM;
2094	}
2095
2096	frm = ieee80211_add_ssid(frm, ssid, ssidlen);
2097	rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2098	frm = ieee80211_add_rates(frm, rs);
2099	frm = ieee80211_add_rsn(frm, vap);
2100	frm = ieee80211_add_xrates(frm, rs);
2101	frm = ieee80211_add_wpa(frm, vap);
2102	if (vap->iv_appie_probereq != NULL)
2103		frm = add_appie(frm, vap->iv_appie_probereq);
2104	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2105
2106	KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
2107	    ("leading space %zd", M_LEADINGSPACE(m)));
2108	M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
2109	if (m == NULL) {
2110		/* NB: cannot happen */
2111		ieee80211_free_node(ni);
2112		return ENOMEM;
2113	}
2114
2115	IEEE80211_TX_LOCK(ic);
2116	wh = mtod(m, struct ieee80211_frame *);
2117	ieee80211_send_setup(ni, m,
2118	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
2119	     IEEE80211_NONQOS_TID, sa, da, bssid);
2120	/* XXX power management? */
2121	m->m_flags |= M_ENCAP;		/* mark encapsulated */
2122
2123	M_WME_SETAC(m, WME_AC_BE);
2124
2125	IEEE80211_NODE_STAT(ni, tx_probereq);
2126	IEEE80211_NODE_STAT(ni, tx_mgmt);
2127
2128	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2129	    "send probe req on channel %u bssid %s ssid \"%.*s\"\n",
2130	    ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(bssid),
2131	    ssidlen, ssid);
2132
2133	memset(&params, 0, sizeof(params));
2134	params.ibp_pri = M_WME_GETAC(m);
2135	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2136	params.ibp_rate0 = tp->mgmtrate;
2137	if (IEEE80211_IS_MULTICAST(da)) {
2138		params.ibp_flags |= IEEE80211_BPF_NOACK;
2139		params.ibp_try0 = 1;
2140	} else
2141		params.ibp_try0 = tp->maxretry;
2142	params.ibp_power = ni->ni_txpower;
2143	ret = ieee80211_raw_output(vap, ni, m, &params);
2144	IEEE80211_TX_UNLOCK(ic);
2145	return (ret);
2146}
2147
2148/*
2149 * Calculate capability information for mgt frames.
2150 */
2151uint16_t
2152ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
2153{
2154	struct ieee80211com *ic = vap->iv_ic;
2155	uint16_t capinfo;
2156
2157	KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
2158
2159	if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2160		capinfo = IEEE80211_CAPINFO_ESS;
2161	else if (vap->iv_opmode == IEEE80211_M_IBSS)
2162		capinfo = IEEE80211_CAPINFO_IBSS;
2163	else
2164		capinfo = 0;
2165	if (vap->iv_flags & IEEE80211_F_PRIVACY)
2166		capinfo |= IEEE80211_CAPINFO_PRIVACY;
2167	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2168	    IEEE80211_IS_CHAN_2GHZ(chan))
2169		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2170	if (ic->ic_flags & IEEE80211_F_SHSLOT)
2171		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2172	if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
2173		capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2174	return capinfo;
2175}
2176
2177/*
2178 * Send a management frame.  The node is for the destination (or ic_bss
2179 * when in station mode).  Nodes other than ic_bss have their reference
2180 * count bumped to reflect our use for an indeterminant time.
2181 */
2182int
2183ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
2184{
2185#define	HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
2186#define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2187	struct ieee80211vap *vap = ni->ni_vap;
2188	struct ieee80211com *ic = ni->ni_ic;
2189	struct ieee80211_node *bss = vap->iv_bss;
2190	struct ieee80211_bpf_params params;
2191	struct mbuf *m;
2192	uint8_t *frm;
2193	uint16_t capinfo;
2194	int has_challenge, is_shared_key, ret, status;
2195
2196	KASSERT(ni != NULL, ("null node"));
2197
2198	/*
2199	 * Hold a reference on the node so it doesn't go away until after
2200	 * the xmit is complete all the way in the driver.  On error we
2201	 * will remove our reference.
2202	 */
2203	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2204		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2205		__func__, __LINE__,
2206		ni, ether_sprintf(ni->ni_macaddr),
2207		ieee80211_node_refcnt(ni)+1);
2208	ieee80211_ref_node(ni);
2209
2210	memset(&params, 0, sizeof(params));
2211	switch (type) {
2212
2213	case IEEE80211_FC0_SUBTYPE_AUTH:
2214		status = arg >> 16;
2215		arg &= 0xffff;
2216		has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
2217		    arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
2218		    ni->ni_challenge != NULL);
2219
2220		/*
2221		 * Deduce whether we're doing open authentication or
2222		 * shared key authentication.  We do the latter if
2223		 * we're in the middle of a shared key authentication
2224		 * handshake or if we're initiating an authentication
2225		 * request and configured to use shared key.
2226		 */
2227		is_shared_key = has_challenge ||
2228		     arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
2229		     (arg == IEEE80211_AUTH_SHARED_REQUEST &&
2230		      bss->ni_authmode == IEEE80211_AUTH_SHARED);
2231
2232		m = ieee80211_getmgtframe(&frm,
2233			  ic->ic_headroom + sizeof(struct ieee80211_frame),
2234			  3 * sizeof(uint16_t)
2235			+ (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
2236				sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)
2237		);
2238		if (m == NULL)
2239			senderr(ENOMEM, is_tx_nobuf);
2240
2241		((uint16_t *)frm)[0] =
2242		    (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
2243		                    : htole16(IEEE80211_AUTH_ALG_OPEN);
2244		((uint16_t *)frm)[1] = htole16(arg);	/* sequence number */
2245		((uint16_t *)frm)[2] = htole16(status);/* status */
2246
2247		if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
2248			((uint16_t *)frm)[3] =
2249			    htole16((IEEE80211_CHALLENGE_LEN << 8) |
2250			    IEEE80211_ELEMID_CHALLENGE);
2251			memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
2252			    IEEE80211_CHALLENGE_LEN);
2253			m->m_pkthdr.len = m->m_len =
2254				4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
2255			if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
2256				IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2257				    "request encrypt frame (%s)", __func__);
2258				/* mark frame for encryption */
2259				params.ibp_flags |= IEEE80211_BPF_CRYPTO;
2260			}
2261		} else
2262			m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
2263
2264		/* XXX not right for shared key */
2265		if (status == IEEE80211_STATUS_SUCCESS)
2266			IEEE80211_NODE_STAT(ni, tx_auth);
2267		else
2268			IEEE80211_NODE_STAT(ni, tx_auth_fail);
2269
2270		if (vap->iv_opmode == IEEE80211_M_STA)
2271			ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2272				(void *) vap->iv_state);
2273		break;
2274
2275	case IEEE80211_FC0_SUBTYPE_DEAUTH:
2276		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2277		    "send station deauthenticate (reason %d)", arg);
2278		m = ieee80211_getmgtframe(&frm,
2279			ic->ic_headroom + sizeof(struct ieee80211_frame),
2280			sizeof(uint16_t));
2281		if (m == NULL)
2282			senderr(ENOMEM, is_tx_nobuf);
2283		*(uint16_t *)frm = htole16(arg);	/* reason */
2284		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2285
2286		IEEE80211_NODE_STAT(ni, tx_deauth);
2287		IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
2288
2289		ieee80211_node_unauthorize(ni);		/* port closed */
2290		break;
2291
2292	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2293	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2294		/*
2295		 * asreq frame format
2296		 *	[2] capability information
2297		 *	[2] listen interval
2298		 *	[6*] current AP address (reassoc only)
2299		 *	[tlv] ssid
2300		 *	[tlv] supported rates
2301		 *	[tlv] extended supported rates
2302		 *	[4] power capability (optional)
2303		 *	[28] supported channels (optional)
2304		 *	[tlv] HT capabilities
2305		 *	[tlv] WME (optional)
2306		 *	[tlv] Vendor OUI HT capabilities (optional)
2307		 *	[tlv] Atheros capabilities (if negotiated)
2308		 *	[tlv] AppIE's (optional)
2309		 */
2310		m = ieee80211_getmgtframe(&frm,
2311			 ic->ic_headroom + sizeof(struct ieee80211_frame),
2312			 sizeof(uint16_t)
2313		       + sizeof(uint16_t)
2314		       + IEEE80211_ADDR_LEN
2315		       + 2 + IEEE80211_NWID_LEN
2316		       + 2 + IEEE80211_RATE_SIZE
2317		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2318		       + 4
2319		       + 2 + 26
2320		       + sizeof(struct ieee80211_wme_info)
2321		       + sizeof(struct ieee80211_ie_htcap)
2322		       + 4 + sizeof(struct ieee80211_ie_htcap)
2323#ifdef IEEE80211_SUPPORT_SUPERG
2324		       + sizeof(struct ieee80211_ath_ie)
2325#endif
2326		       + (vap->iv_appie_wpa != NULL ?
2327				vap->iv_appie_wpa->ie_len : 0)
2328		       + (vap->iv_appie_assocreq != NULL ?
2329				vap->iv_appie_assocreq->ie_len : 0)
2330		);
2331		if (m == NULL)
2332			senderr(ENOMEM, is_tx_nobuf);
2333
2334		KASSERT(vap->iv_opmode == IEEE80211_M_STA,
2335		    ("wrong mode %u", vap->iv_opmode));
2336		capinfo = IEEE80211_CAPINFO_ESS;
2337		if (vap->iv_flags & IEEE80211_F_PRIVACY)
2338			capinfo |= IEEE80211_CAPINFO_PRIVACY;
2339		/*
2340		 * NB: Some 11a AP's reject the request when
2341		 *     short premable is set.
2342		 */
2343		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2344		    IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2345			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2346		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
2347		    (ic->ic_caps & IEEE80211_C_SHSLOT))
2348			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2349		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
2350		    (vap->iv_flags & IEEE80211_F_DOTH))
2351			capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2352		*(uint16_t *)frm = htole16(capinfo);
2353		frm += 2;
2354
2355		KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
2356		*(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
2357						    bss->ni_intval));
2358		frm += 2;
2359
2360		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2361			IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
2362			frm += IEEE80211_ADDR_LEN;
2363		}
2364
2365		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
2366		frm = ieee80211_add_rates(frm, &ni->ni_rates);
2367		frm = ieee80211_add_rsn(frm, vap);
2368		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2369		if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
2370			frm = ieee80211_add_powercapability(frm,
2371			    ic->ic_curchan);
2372			frm = ieee80211_add_supportedchannels(frm, ic);
2373		}
2374
2375		/*
2376		 * Check the channel - we may be using an 11n NIC with an
2377		 * 11n capable station, but we're configured to be an 11b
2378		 * channel.
2379		 */
2380		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2381		    IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2382		    ni->ni_ies.htcap_ie != NULL &&
2383		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP) {
2384			frm = ieee80211_add_htcap(frm, ni);
2385		}
2386		frm = ieee80211_add_wpa(frm, vap);
2387		if ((ic->ic_flags & IEEE80211_F_WME) &&
2388		    ni->ni_ies.wme_ie != NULL)
2389			frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
2390
2391		/*
2392		 * Same deal - only send HT info if we're on an 11n
2393		 * capable channel.
2394		 */
2395		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2396		    IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2397		    ni->ni_ies.htcap_ie != NULL &&
2398		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR) {
2399			frm = ieee80211_add_htcap_vendor(frm, ni);
2400		}
2401#ifdef IEEE80211_SUPPORT_SUPERG
2402		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
2403			frm = ieee80211_add_ath(frm,
2404				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2405				((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2406				 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2407				vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2408		}
2409#endif /* IEEE80211_SUPPORT_SUPERG */
2410		if (vap->iv_appie_assocreq != NULL)
2411			frm = add_appie(frm, vap->iv_appie_assocreq);
2412		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2413
2414		ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2415			(void *) vap->iv_state);
2416		break;
2417
2418	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2419	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2420		/*
2421		 * asresp frame format
2422		 *	[2] capability information
2423		 *	[2] status
2424		 *	[2] association ID
2425		 *	[tlv] supported rates
2426		 *	[tlv] extended supported rates
2427		 *	[tlv] HT capabilities (standard, if STA enabled)
2428		 *	[tlv] HT information (standard, if STA enabled)
2429		 *	[tlv] WME (if configured and STA enabled)
2430		 *	[tlv] HT capabilities (vendor OUI, if STA enabled)
2431		 *	[tlv] HT information (vendor OUI, if STA enabled)
2432		 *	[tlv] Atheros capabilities (if STA enabled)
2433		 *	[tlv] AppIE's (optional)
2434		 */
2435		m = ieee80211_getmgtframe(&frm,
2436			 ic->ic_headroom + sizeof(struct ieee80211_frame),
2437			 sizeof(uint16_t)
2438		       + sizeof(uint16_t)
2439		       + sizeof(uint16_t)
2440		       + 2 + IEEE80211_RATE_SIZE
2441		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2442		       + sizeof(struct ieee80211_ie_htcap) + 4
2443		       + sizeof(struct ieee80211_ie_htinfo) + 4
2444		       + sizeof(struct ieee80211_wme_param)
2445#ifdef IEEE80211_SUPPORT_SUPERG
2446		       + sizeof(struct ieee80211_ath_ie)
2447#endif
2448		       + (vap->iv_appie_assocresp != NULL ?
2449				vap->iv_appie_assocresp->ie_len : 0)
2450		);
2451		if (m == NULL)
2452			senderr(ENOMEM, is_tx_nobuf);
2453
2454		capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2455		*(uint16_t *)frm = htole16(capinfo);
2456		frm += 2;
2457
2458		*(uint16_t *)frm = htole16(arg);	/* status */
2459		frm += 2;
2460
2461		if (arg == IEEE80211_STATUS_SUCCESS) {
2462			*(uint16_t *)frm = htole16(ni->ni_associd);
2463			IEEE80211_NODE_STAT(ni, tx_assoc);
2464		} else
2465			IEEE80211_NODE_STAT(ni, tx_assoc_fail);
2466		frm += 2;
2467
2468		frm = ieee80211_add_rates(frm, &ni->ni_rates);
2469		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2470		/* NB: respond according to what we received */
2471		if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
2472			frm = ieee80211_add_htcap(frm, ni);
2473			frm = ieee80211_add_htinfo(frm, ni);
2474		}
2475		if ((vap->iv_flags & IEEE80211_F_WME) &&
2476		    ni->ni_ies.wme_ie != NULL)
2477			frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2478		if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
2479			frm = ieee80211_add_htcap_vendor(frm, ni);
2480			frm = ieee80211_add_htinfo_vendor(frm, ni);
2481		}
2482#ifdef IEEE80211_SUPPORT_SUPERG
2483		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
2484			frm = ieee80211_add_ath(frm,
2485				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2486				((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2487				 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2488				vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2489#endif /* IEEE80211_SUPPORT_SUPERG */
2490		if (vap->iv_appie_assocresp != NULL)
2491			frm = add_appie(frm, vap->iv_appie_assocresp);
2492		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2493		break;
2494
2495	case IEEE80211_FC0_SUBTYPE_DISASSOC:
2496		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2497		    "send station disassociate (reason %d)", arg);
2498		m = ieee80211_getmgtframe(&frm,
2499			ic->ic_headroom + sizeof(struct ieee80211_frame),
2500			sizeof(uint16_t));
2501		if (m == NULL)
2502			senderr(ENOMEM, is_tx_nobuf);
2503		*(uint16_t *)frm = htole16(arg);	/* reason */
2504		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2505
2506		IEEE80211_NODE_STAT(ni, tx_disassoc);
2507		IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
2508		break;
2509
2510	default:
2511		IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
2512		    "invalid mgmt frame type %u", type);
2513		senderr(EINVAL, is_tx_unknownmgt);
2514		/* NOTREACHED */
2515	}
2516
2517	/* NB: force non-ProbeResp frames to the highest queue */
2518	params.ibp_pri = WME_AC_VO;
2519	params.ibp_rate0 = bss->ni_txparms->mgmtrate;
2520	/* NB: we know all frames are unicast */
2521	params.ibp_try0 = bss->ni_txparms->maxretry;
2522	params.ibp_power = bss->ni_txpower;
2523	return ieee80211_mgmt_output(ni, m, type, &params);
2524bad:
2525	ieee80211_free_node(ni);
2526	return ret;
2527#undef senderr
2528#undef HTFLAGS
2529}
2530
2531/*
2532 * Return an mbuf with a probe response frame in it.
2533 * Space is left to prepend and 802.11 header at the
2534 * front but it's left to the caller to fill in.
2535 */
2536struct mbuf *
2537ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
2538{
2539	struct ieee80211vap *vap = bss->ni_vap;
2540	struct ieee80211com *ic = bss->ni_ic;
2541	const struct ieee80211_rateset *rs;
2542	struct mbuf *m;
2543	uint16_t capinfo;
2544	uint8_t *frm;
2545
2546	/*
2547	 * probe response frame format
2548	 *	[8] time stamp
2549	 *	[2] beacon interval
2550	 *	[2] cabability information
2551	 *	[tlv] ssid
2552	 *	[tlv] supported rates
2553	 *	[tlv] parameter set (FH/DS)
2554	 *	[tlv] parameter set (IBSS)
2555	 *	[tlv] country (optional)
2556	 *	[3] power control (optional)
2557	 *	[5] channel switch announcement (CSA) (optional)
2558	 *	[tlv] extended rate phy (ERP)
2559	 *	[tlv] extended supported rates
2560	 *	[tlv] RSN (optional)
2561	 *	[tlv] HT capabilities
2562	 *	[tlv] HT information
2563	 *	[tlv] WPA (optional)
2564	 *	[tlv] WME (optional)
2565	 *	[tlv] Vendor OUI HT capabilities (optional)
2566	 *	[tlv] Vendor OUI HT information (optional)
2567	 *	[tlv] Atheros capabilities
2568	 *	[tlv] AppIE's (optional)
2569	 *	[tlv] Mesh ID (MBSS)
2570	 *	[tlv] Mesh Conf (MBSS)
2571	 */
2572	m = ieee80211_getmgtframe(&frm,
2573		 ic->ic_headroom + sizeof(struct ieee80211_frame),
2574		 8
2575	       + sizeof(uint16_t)
2576	       + sizeof(uint16_t)
2577	       + 2 + IEEE80211_NWID_LEN
2578	       + 2 + IEEE80211_RATE_SIZE
2579	       + 7	/* max(7,3) */
2580	       + IEEE80211_COUNTRY_MAX_SIZE
2581	       + 3
2582	       + sizeof(struct ieee80211_csa_ie)
2583	       + sizeof(struct ieee80211_quiet_ie)
2584	       + 3
2585	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2586	       + sizeof(struct ieee80211_ie_wpa)
2587	       + sizeof(struct ieee80211_ie_htcap)
2588	       + sizeof(struct ieee80211_ie_htinfo)
2589	       + sizeof(struct ieee80211_ie_wpa)
2590	       + sizeof(struct ieee80211_wme_param)
2591	       + 4 + sizeof(struct ieee80211_ie_htcap)
2592	       + 4 + sizeof(struct ieee80211_ie_htinfo)
2593#ifdef IEEE80211_SUPPORT_SUPERG
2594	       + sizeof(struct ieee80211_ath_ie)
2595#endif
2596#ifdef IEEE80211_SUPPORT_MESH
2597	       + 2 + IEEE80211_MESHID_LEN
2598	       + sizeof(struct ieee80211_meshconf_ie)
2599#endif
2600	       + (vap->iv_appie_proberesp != NULL ?
2601			vap->iv_appie_proberesp->ie_len : 0)
2602	);
2603	if (m == NULL) {
2604		vap->iv_stats.is_tx_nobuf++;
2605		return NULL;
2606	}
2607
2608	memset(frm, 0, 8);	/* timestamp should be filled later */
2609	frm += 8;
2610	*(uint16_t *)frm = htole16(bss->ni_intval);
2611	frm += 2;
2612	capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2613	*(uint16_t *)frm = htole16(capinfo);
2614	frm += 2;
2615
2616	frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
2617	rs = ieee80211_get_suprates(ic, bss->ni_chan);
2618	frm = ieee80211_add_rates(frm, rs);
2619
2620	if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
2621		*frm++ = IEEE80211_ELEMID_FHPARMS;
2622		*frm++ = 5;
2623		*frm++ = bss->ni_fhdwell & 0x00ff;
2624		*frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
2625		*frm++ = IEEE80211_FH_CHANSET(
2626		    ieee80211_chan2ieee(ic, bss->ni_chan));
2627		*frm++ = IEEE80211_FH_CHANPAT(
2628		    ieee80211_chan2ieee(ic, bss->ni_chan));
2629		*frm++ = bss->ni_fhindex;
2630	} else {
2631		*frm++ = IEEE80211_ELEMID_DSPARMS;
2632		*frm++ = 1;
2633		*frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
2634	}
2635
2636	if (vap->iv_opmode == IEEE80211_M_IBSS) {
2637		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
2638		*frm++ = 2;
2639		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
2640	}
2641	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2642	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2643		frm = ieee80211_add_countryie(frm, ic);
2644	if (vap->iv_flags & IEEE80211_F_DOTH) {
2645		if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
2646			frm = ieee80211_add_powerconstraint(frm, vap);
2647		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2648			frm = ieee80211_add_csa(frm, vap);
2649	}
2650	if (vap->iv_flags & IEEE80211_F_DOTH) {
2651		if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
2652		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
2653			if (vap->iv_quiet)
2654				frm = ieee80211_add_quiet(frm, vap);
2655		}
2656	}
2657	if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
2658		frm = ieee80211_add_erp(frm, ic);
2659	frm = ieee80211_add_xrates(frm, rs);
2660	frm = ieee80211_add_rsn(frm, vap);
2661	/*
2662	 * NB: legacy 11b clients do not get certain ie's.
2663	 *     The caller identifies such clients by passing
2664	 *     a token in legacy to us.  Could expand this to be
2665	 *     any legacy client for stuff like HT ie's.
2666	 */
2667	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2668	    legacy != IEEE80211_SEND_LEGACY_11B) {
2669		frm = ieee80211_add_htcap(frm, bss);
2670		frm = ieee80211_add_htinfo(frm, bss);
2671	}
2672	frm = ieee80211_add_wpa(frm, vap);
2673	if (vap->iv_flags & IEEE80211_F_WME)
2674		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2675	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2676	    (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
2677	    legacy != IEEE80211_SEND_LEGACY_11B) {
2678		frm = ieee80211_add_htcap_vendor(frm, bss);
2679		frm = ieee80211_add_htinfo_vendor(frm, bss);
2680	}
2681#ifdef IEEE80211_SUPPORT_SUPERG
2682	if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
2683	    legacy != IEEE80211_SEND_LEGACY_11B)
2684		frm = ieee80211_add_athcaps(frm, bss);
2685#endif
2686	if (vap->iv_appie_proberesp != NULL)
2687		frm = add_appie(frm, vap->iv_appie_proberesp);
2688#ifdef IEEE80211_SUPPORT_MESH
2689	if (vap->iv_opmode == IEEE80211_M_MBSS) {
2690		frm = ieee80211_add_meshid(frm, vap);
2691		frm = ieee80211_add_meshconf(frm, vap);
2692	}
2693#endif
2694	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2695
2696	return m;
2697}
2698
2699/*
2700 * Send a probe response frame to the specified mac address.
2701 * This does not go through the normal mgt frame api so we
2702 * can specify the destination address and re-use the bss node
2703 * for the sta reference.
2704 */
2705int
2706ieee80211_send_proberesp(struct ieee80211vap *vap,
2707	const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
2708{
2709	struct ieee80211_node *bss = vap->iv_bss;
2710	struct ieee80211com *ic = vap->iv_ic;
2711	struct ieee80211_frame *wh;
2712	struct mbuf *m;
2713	int ret;
2714
2715	if (vap->iv_state == IEEE80211_S_CAC) {
2716		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
2717		    "block %s frame in CAC state", "probe response");
2718		vap->iv_stats.is_tx_badstate++;
2719		return EIO;		/* XXX */
2720	}
2721
2722	/*
2723	 * Hold a reference on the node so it doesn't go away until after
2724	 * the xmit is complete all the way in the driver.  On error we
2725	 * will remove our reference.
2726	 */
2727	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2728	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2729	    __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
2730	    ieee80211_node_refcnt(bss)+1);
2731	ieee80211_ref_node(bss);
2732
2733	m = ieee80211_alloc_proberesp(bss, legacy);
2734	if (m == NULL) {
2735		ieee80211_free_node(bss);
2736		return ENOMEM;
2737	}
2738
2739	M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
2740	KASSERT(m != NULL, ("no room for header"));
2741
2742	IEEE80211_TX_LOCK(ic);
2743	wh = mtod(m, struct ieee80211_frame *);
2744	ieee80211_send_setup(bss, m,
2745	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
2746	     IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
2747	/* XXX power management? */
2748	m->m_flags |= M_ENCAP;		/* mark encapsulated */
2749
2750	M_WME_SETAC(m, WME_AC_BE);
2751
2752	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2753	    "send probe resp on channel %u to %s%s\n",
2754	    ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
2755	    legacy ? " <legacy>" : "");
2756	IEEE80211_NODE_STAT(bss, tx_mgmt);
2757
2758	ret = ieee80211_raw_output(vap, bss, m, NULL);
2759	IEEE80211_TX_UNLOCK(ic);
2760	return (ret);
2761}
2762
2763/*
2764 * Allocate and build a RTS (Request To Send) control frame.
2765 */
2766struct mbuf *
2767ieee80211_alloc_rts(struct ieee80211com *ic,
2768	const uint8_t ra[IEEE80211_ADDR_LEN],
2769	const uint8_t ta[IEEE80211_ADDR_LEN],
2770	uint16_t dur)
2771{
2772	struct ieee80211_frame_rts *rts;
2773	struct mbuf *m;
2774
2775	/* XXX honor ic_headroom */
2776	m = m_gethdr(M_NOWAIT, MT_DATA);
2777	if (m != NULL) {
2778		rts = mtod(m, struct ieee80211_frame_rts *);
2779		rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2780			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
2781		rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2782		*(u_int16_t *)rts->i_dur = htole16(dur);
2783		IEEE80211_ADDR_COPY(rts->i_ra, ra);
2784		IEEE80211_ADDR_COPY(rts->i_ta, ta);
2785
2786		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
2787	}
2788	return m;
2789}
2790
2791/*
2792 * Allocate and build a CTS (Clear To Send) control frame.
2793 */
2794struct mbuf *
2795ieee80211_alloc_cts(struct ieee80211com *ic,
2796	const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
2797{
2798	struct ieee80211_frame_cts *cts;
2799	struct mbuf *m;
2800
2801	/* XXX honor ic_headroom */
2802	m = m_gethdr(M_NOWAIT, MT_DATA);
2803	if (m != NULL) {
2804		cts = mtod(m, struct ieee80211_frame_cts *);
2805		cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2806			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
2807		cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2808		*(u_int16_t *)cts->i_dur = htole16(dur);
2809		IEEE80211_ADDR_COPY(cts->i_ra, ra);
2810
2811		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
2812	}
2813	return m;
2814}
2815
2816static void
2817ieee80211_tx_mgt_timeout(void *arg)
2818{
2819	struct ieee80211vap *vap = arg;
2820
2821	IEEE80211_LOCK(vap->iv_ic);
2822	if (vap->iv_state != IEEE80211_S_INIT &&
2823	    (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
2824		/*
2825		 * NB: it's safe to specify a timeout as the reason here;
2826		 *     it'll only be used in the right state.
2827		 */
2828		ieee80211_new_state_locked(vap, IEEE80211_S_SCAN,
2829			IEEE80211_SCAN_FAIL_TIMEOUT);
2830	}
2831	IEEE80211_UNLOCK(vap->iv_ic);
2832}
2833
2834/*
2835 * This is the callback set on net80211-sourced transmitted
2836 * authentication request frames.
2837 *
2838 * This does a couple of things:
2839 *
2840 * + If the frame transmitted was a success, it schedules a future
2841 *   event which will transition the interface to scan.
2842 *   If a state transition _then_ occurs before that event occurs,
2843 *   said state transition will cancel this callout.
2844 *
2845 * + If the frame transmit was a failure, it immediately schedules
2846 *   the transition back to scan.
2847 */
2848static void
2849ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
2850{
2851	struct ieee80211vap *vap = ni->ni_vap;
2852	enum ieee80211_state ostate = (enum ieee80211_state) arg;
2853
2854	/*
2855	 * Frame transmit completed; arrange timer callback.  If
2856	 * transmit was successfuly we wait for response.  Otherwise
2857	 * we arrange an immediate callback instead of doing the
2858	 * callback directly since we don't know what state the driver
2859	 * is in (e.g. what locks it is holding).  This work should
2860	 * not be too time-critical and not happen too often so the
2861	 * added overhead is acceptable.
2862	 *
2863	 * XXX what happens if !acked but response shows up before callback?
2864	 */
2865	if (vap->iv_state == ostate) {
2866		callout_reset(&vap->iv_mgtsend,
2867			status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
2868			ieee80211_tx_mgt_timeout, vap);
2869	}
2870}
2871
2872static void
2873ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
2874	struct ieee80211_node *ni)
2875{
2876	struct ieee80211vap *vap = ni->ni_vap;
2877	struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
2878	struct ieee80211com *ic = ni->ni_ic;
2879	struct ieee80211_rateset *rs = &ni->ni_rates;
2880	uint16_t capinfo;
2881
2882	/*
2883	 * beacon frame format
2884	 *	[8] time stamp
2885	 *	[2] beacon interval
2886	 *	[2] cabability information
2887	 *	[tlv] ssid
2888	 *	[tlv] supported rates
2889	 *	[3] parameter set (DS)
2890	 *	[8] CF parameter set (optional)
2891	 *	[tlv] parameter set (IBSS/TIM)
2892	 *	[tlv] country (optional)
2893	 *	[3] power control (optional)
2894	 *	[5] channel switch announcement (CSA) (optional)
2895	 *	[tlv] extended rate phy (ERP)
2896	 *	[tlv] extended supported rates
2897	 *	[tlv] RSN parameters
2898	 *	[tlv] HT capabilities
2899	 *	[tlv] HT information
2900	 * XXX Vendor-specific OIDs (e.g. Atheros)
2901	 *	[tlv] WPA parameters
2902	 *	[tlv] WME parameters
2903	 *	[tlv] Vendor OUI HT capabilities (optional)
2904	 *	[tlv] Vendor OUI HT information (optional)
2905	 *	[tlv] Atheros capabilities (optional)
2906	 *	[tlv] TDMA parameters (optional)
2907	 *	[tlv] Mesh ID (MBSS)
2908	 *	[tlv] Mesh Conf (MBSS)
2909	 *	[tlv] application data (optional)
2910	 */
2911
2912	memset(bo, 0, sizeof(*bo));
2913
2914	memset(frm, 0, 8);	/* XXX timestamp is set by hardware/driver */
2915	frm += 8;
2916	*(uint16_t *)frm = htole16(ni->ni_intval);
2917	frm += 2;
2918	capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
2919	bo->bo_caps = (uint16_t *)frm;
2920	*(uint16_t *)frm = htole16(capinfo);
2921	frm += 2;
2922	*frm++ = IEEE80211_ELEMID_SSID;
2923	if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
2924		*frm++ = ni->ni_esslen;
2925		memcpy(frm, ni->ni_essid, ni->ni_esslen);
2926		frm += ni->ni_esslen;
2927	} else
2928		*frm++ = 0;
2929	frm = ieee80211_add_rates(frm, rs);
2930	if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
2931		*frm++ = IEEE80211_ELEMID_DSPARMS;
2932		*frm++ = 1;
2933		*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2934	}
2935	if (ic->ic_flags & IEEE80211_F_PCF) {
2936		bo->bo_cfp = frm;
2937		frm = ieee80211_add_cfparms(frm, ic);
2938	}
2939	bo->bo_tim = frm;
2940	if (vap->iv_opmode == IEEE80211_M_IBSS) {
2941		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
2942		*frm++ = 2;
2943		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
2944		bo->bo_tim_len = 0;
2945	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2946	    vap->iv_opmode == IEEE80211_M_MBSS) {
2947		/* TIM IE is the same for Mesh and Hostap */
2948		struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
2949
2950		tie->tim_ie = IEEE80211_ELEMID_TIM;
2951		tie->tim_len = 4;	/* length */
2952		tie->tim_count = 0;	/* DTIM count */
2953		tie->tim_period = vap->iv_dtim_period;	/* DTIM period */
2954		tie->tim_bitctl = 0;	/* bitmap control */
2955		tie->tim_bitmap[0] = 0;	/* Partial Virtual Bitmap */
2956		frm += sizeof(struct ieee80211_tim_ie);
2957		bo->bo_tim_len = 1;
2958	}
2959	bo->bo_tim_trailer = frm;
2960	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2961	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2962		frm = ieee80211_add_countryie(frm, ic);
2963	if (vap->iv_flags & IEEE80211_F_DOTH) {
2964		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
2965			frm = ieee80211_add_powerconstraint(frm, vap);
2966		bo->bo_csa = frm;
2967		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2968			frm = ieee80211_add_csa(frm, vap);
2969	} else
2970		bo->bo_csa = frm;
2971
2972	if (vap->iv_flags & IEEE80211_F_DOTH) {
2973		bo->bo_quiet = frm;
2974		if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
2975		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
2976			if (vap->iv_quiet)
2977				frm = ieee80211_add_quiet(frm,vap);
2978		}
2979	} else
2980		bo->bo_quiet = frm;
2981
2982	if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
2983		bo->bo_erp = frm;
2984		frm = ieee80211_add_erp(frm, ic);
2985	}
2986	frm = ieee80211_add_xrates(frm, rs);
2987	frm = ieee80211_add_rsn(frm, vap);
2988	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
2989		frm = ieee80211_add_htcap(frm, ni);
2990		bo->bo_htinfo = frm;
2991		frm = ieee80211_add_htinfo(frm, ni);
2992	}
2993	frm = ieee80211_add_wpa(frm, vap);
2994	if (vap->iv_flags & IEEE80211_F_WME) {
2995		bo->bo_wme = frm;
2996		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2997	}
2998	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2999	    (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
3000		frm = ieee80211_add_htcap_vendor(frm, ni);
3001		frm = ieee80211_add_htinfo_vendor(frm, ni);
3002	}
3003#ifdef IEEE80211_SUPPORT_SUPERG
3004	if (vap->iv_flags & IEEE80211_F_ATHEROS) {
3005		bo->bo_ath = frm;
3006		frm = ieee80211_add_athcaps(frm, ni);
3007	}
3008#endif
3009#ifdef IEEE80211_SUPPORT_TDMA
3010	if (vap->iv_caps & IEEE80211_C_TDMA) {
3011		bo->bo_tdma = frm;
3012		frm = ieee80211_add_tdma(frm, vap);
3013	}
3014#endif
3015	if (vap->iv_appie_beacon != NULL) {
3016		bo->bo_appie = frm;
3017		bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
3018		frm = add_appie(frm, vap->iv_appie_beacon);
3019	}
3020#ifdef IEEE80211_SUPPORT_MESH
3021	if (vap->iv_opmode == IEEE80211_M_MBSS) {
3022		frm = ieee80211_add_meshid(frm, vap);
3023		bo->bo_meshconf = frm;
3024		frm = ieee80211_add_meshconf(frm, vap);
3025	}
3026#endif
3027	bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
3028	bo->bo_csa_trailer_len = frm - bo->bo_csa;
3029	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3030}
3031
3032/*
3033 * Allocate a beacon frame and fillin the appropriate bits.
3034 */
3035struct mbuf *
3036ieee80211_beacon_alloc(struct ieee80211_node *ni)
3037{
3038	struct ieee80211vap *vap = ni->ni_vap;
3039	struct ieee80211com *ic = ni->ni_ic;
3040	struct ifnet *ifp = vap->iv_ifp;
3041	struct ieee80211_frame *wh;
3042	struct mbuf *m;
3043	int pktlen;
3044	uint8_t *frm;
3045
3046	/*
3047	 * beacon frame format
3048	 *	[8] time stamp
3049	 *	[2] beacon interval
3050	 *	[2] cabability information
3051	 *	[tlv] ssid
3052	 *	[tlv] supported rates
3053	 *	[3] parameter set (DS)
3054	 *	[8] CF parameter set (optional)
3055	 *	[tlv] parameter set (IBSS/TIM)
3056	 *	[tlv] country (optional)
3057	 *	[3] power control (optional)
3058	 *	[5] channel switch announcement (CSA) (optional)
3059	 *	[tlv] extended rate phy (ERP)
3060	 *	[tlv] extended supported rates
3061	 *	[tlv] RSN parameters
3062	 *	[tlv] HT capabilities
3063	 *	[tlv] HT information
3064	 *	[tlv] Vendor OUI HT capabilities (optional)
3065	 *	[tlv] Vendor OUI HT information (optional)
3066	 * XXX Vendor-specific OIDs (e.g. Atheros)
3067	 *	[tlv] WPA parameters
3068	 *	[tlv] WME parameters
3069	 *	[tlv] TDMA parameters (optional)
3070	 *	[tlv] Mesh ID (MBSS)
3071	 *	[tlv] Mesh Conf (MBSS)
3072	 *	[tlv] application data (optional)
3073	 * NB: we allocate the max space required for the TIM bitmap.
3074	 * XXX how big is this?
3075	 */
3076	pktlen =   8					/* time stamp */
3077		 + sizeof(uint16_t)			/* beacon interval */
3078		 + sizeof(uint16_t)			/* capabilities */
3079		 + 2 + ni->ni_esslen			/* ssid */
3080	         + 2 + IEEE80211_RATE_SIZE		/* supported rates */
3081	         + 2 + 1				/* DS parameters */
3082		 + 2 + 6				/* CF parameters */
3083		 + 2 + 4 + vap->iv_tim_len		/* DTIM/IBSSPARMS */
3084		 + IEEE80211_COUNTRY_MAX_SIZE		/* country */
3085		 + 2 + 1				/* power control */
3086		 + sizeof(struct ieee80211_csa_ie)	/* CSA */
3087		 + sizeof(struct ieee80211_quiet_ie)	/* Quiet */
3088		 + 2 + 1				/* ERP */
3089	         + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
3090		 + (vap->iv_caps & IEEE80211_C_WPA ?	/* WPA 1+2 */
3091			2*sizeof(struct ieee80211_ie_wpa) : 0)
3092		 /* XXX conditional? */
3093		 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
3094		 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
3095		 + (vap->iv_caps & IEEE80211_C_WME ?	/* WME */
3096			sizeof(struct ieee80211_wme_param) : 0)
3097#ifdef IEEE80211_SUPPORT_SUPERG
3098		 + sizeof(struct ieee80211_ath_ie)	/* ATH */
3099#endif
3100#ifdef IEEE80211_SUPPORT_TDMA
3101		 + (vap->iv_caps & IEEE80211_C_TDMA ?	/* TDMA */
3102			sizeof(struct ieee80211_tdma_param) : 0)
3103#endif
3104#ifdef IEEE80211_SUPPORT_MESH
3105		 + 2 + ni->ni_meshidlen
3106		 + sizeof(struct ieee80211_meshconf_ie)
3107#endif
3108		 + IEEE80211_MAX_APPIE
3109		 ;
3110	m = ieee80211_getmgtframe(&frm,
3111		ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
3112	if (m == NULL) {
3113		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
3114			"%s: cannot get buf; size %u\n", __func__, pktlen);
3115		vap->iv_stats.is_tx_nobuf++;
3116		return NULL;
3117	}
3118	ieee80211_beacon_construct(m, frm, ni);
3119
3120	M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
3121	KASSERT(m != NULL, ("no space for 802.11 header?"));
3122	wh = mtod(m, struct ieee80211_frame *);
3123	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
3124	    IEEE80211_FC0_SUBTYPE_BEACON;
3125	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3126	*(uint16_t *)wh->i_dur = 0;
3127	IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
3128	IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
3129	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
3130	*(uint16_t *)wh->i_seq = 0;
3131
3132	return m;
3133}
3134
3135/*
3136 * Update the dynamic parts of a beacon frame based on the current state.
3137 */
3138int
3139ieee80211_beacon_update(struct ieee80211_node *ni, struct mbuf *m, int mcast)
3140{
3141	struct ieee80211vap *vap = ni->ni_vap;
3142	struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
3143	struct ieee80211com *ic = ni->ni_ic;
3144	int len_changed = 0;
3145	uint16_t capinfo;
3146	struct ieee80211_frame *wh;
3147	ieee80211_seq seqno;
3148
3149	IEEE80211_LOCK(ic);
3150	/*
3151	 * Handle 11h channel change when we've reached the count.
3152	 * We must recalculate the beacon frame contents to account
3153	 * for the new channel.  Note we do this only for the first
3154	 * vap that reaches this point; subsequent vaps just update
3155	 * their beacon state to reflect the recalculated channel.
3156	 */
3157	if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
3158	    vap->iv_csa_count == ic->ic_csa_count) {
3159		vap->iv_csa_count = 0;
3160		/*
3161		 * Effect channel change before reconstructing the beacon
3162		 * frame contents as many places reference ni_chan.
3163		 */
3164		if (ic->ic_csa_newchan != NULL)
3165			ieee80211_csa_completeswitch(ic);
3166		/*
3167		 * NB: ieee80211_beacon_construct clears all pending
3168		 * updates in bo_flags so we don't need to explicitly
3169		 * clear IEEE80211_BEACON_CSA.
3170		 */
3171		ieee80211_beacon_construct(m,
3172		    mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
3173
3174		/* XXX do WME aggressive mode processing? */
3175		IEEE80211_UNLOCK(ic);
3176		return 1;		/* just assume length changed */
3177	}
3178
3179	wh = mtod(m, struct ieee80211_frame *);
3180	seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
3181	*(uint16_t *)&wh->i_seq[0] =
3182		htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
3183	M_SEQNO_SET(m, seqno);
3184
3185	/* XXX faster to recalculate entirely or just changes? */
3186	capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
3187	*bo->bo_caps = htole16(capinfo);
3188
3189	if (vap->iv_flags & IEEE80211_F_WME) {
3190		struct ieee80211_wme_state *wme = &ic->ic_wme;
3191
3192		/*
3193		 * Check for agressive mode change.  When there is
3194		 * significant high priority traffic in the BSS
3195		 * throttle back BE traffic by using conservative
3196		 * parameters.  Otherwise BE uses agressive params
3197		 * to optimize performance of legacy/non-QoS traffic.
3198		 */
3199		if (wme->wme_flags & WME_F_AGGRMODE) {
3200			if (wme->wme_hipri_traffic >
3201			    wme->wme_hipri_switch_thresh) {
3202				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3203				    "%s: traffic %u, disable aggressive mode\n",
3204				    __func__, wme->wme_hipri_traffic);
3205				wme->wme_flags &= ~WME_F_AGGRMODE;
3206				ieee80211_wme_updateparams_locked(vap);
3207				wme->wme_hipri_traffic =
3208					wme->wme_hipri_switch_hysteresis;
3209			} else
3210				wme->wme_hipri_traffic = 0;
3211		} else {
3212			if (wme->wme_hipri_traffic <=
3213			    wme->wme_hipri_switch_thresh) {
3214				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3215				    "%s: traffic %u, enable aggressive mode\n",
3216				    __func__, wme->wme_hipri_traffic);
3217				wme->wme_flags |= WME_F_AGGRMODE;
3218				ieee80211_wme_updateparams_locked(vap);
3219				wme->wme_hipri_traffic = 0;
3220			} else
3221				wme->wme_hipri_traffic =
3222					wme->wme_hipri_switch_hysteresis;
3223		}
3224		if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
3225			(void) ieee80211_add_wme_param(bo->bo_wme, wme);
3226			clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
3227		}
3228	}
3229
3230	if (isset(bo->bo_flags,  IEEE80211_BEACON_HTINFO)) {
3231		ieee80211_ht_update_beacon(vap, bo);
3232		clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
3233	}
3234#ifdef IEEE80211_SUPPORT_TDMA
3235	if (vap->iv_caps & IEEE80211_C_TDMA) {
3236		/*
3237		 * NB: the beacon is potentially updated every TBTT.
3238		 */
3239		ieee80211_tdma_update_beacon(vap, bo);
3240	}
3241#endif
3242#ifdef IEEE80211_SUPPORT_MESH
3243	if (vap->iv_opmode == IEEE80211_M_MBSS)
3244		ieee80211_mesh_update_beacon(vap, bo);
3245#endif
3246
3247	if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
3248	    vap->iv_opmode == IEEE80211_M_MBSS) {	/* NB: no IBSS support*/
3249		struct ieee80211_tim_ie *tie =
3250			(struct ieee80211_tim_ie *) bo->bo_tim;
3251		if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
3252			u_int timlen, timoff, i;
3253			/*
3254			 * ATIM/DTIM needs updating.  If it fits in the
3255			 * current space allocated then just copy in the
3256			 * new bits.  Otherwise we need to move any trailing
3257			 * data to make room.  Note that we know there is
3258			 * contiguous space because ieee80211_beacon_allocate
3259			 * insures there is space in the mbuf to write a
3260			 * maximal-size virtual bitmap (based on iv_max_aid).
3261			 */
3262			/*
3263			 * Calculate the bitmap size and offset, copy any
3264			 * trailer out of the way, and then copy in the
3265			 * new bitmap and update the information element.
3266			 * Note that the tim bitmap must contain at least
3267			 * one byte and any offset must be even.
3268			 */
3269			if (vap->iv_ps_pending != 0) {
3270				timoff = 128;		/* impossibly large */
3271				for (i = 0; i < vap->iv_tim_len; i++)
3272					if (vap->iv_tim_bitmap[i]) {
3273						timoff = i &~ 1;
3274						break;
3275					}
3276				KASSERT(timoff != 128, ("tim bitmap empty!"));
3277				for (i = vap->iv_tim_len-1; i >= timoff; i--)
3278					if (vap->iv_tim_bitmap[i])
3279						break;
3280				timlen = 1 + (i - timoff);
3281			} else {
3282				timoff = 0;
3283				timlen = 1;
3284			}
3285			if (timlen != bo->bo_tim_len) {
3286				/* copy up/down trailer */
3287				int adjust = tie->tim_bitmap+timlen
3288					   - bo->bo_tim_trailer;
3289				ovbcopy(bo->bo_tim_trailer,
3290				    bo->bo_tim_trailer+adjust,
3291				    bo->bo_tim_trailer_len);
3292				bo->bo_tim_trailer += adjust;
3293				bo->bo_erp += adjust;
3294				bo->bo_htinfo += adjust;
3295#ifdef IEEE80211_SUPPORT_SUPERG
3296				bo->bo_ath += adjust;
3297#endif
3298#ifdef IEEE80211_SUPPORT_TDMA
3299				bo->bo_tdma += adjust;
3300#endif
3301#ifdef IEEE80211_SUPPORT_MESH
3302				bo->bo_meshconf += adjust;
3303#endif
3304				bo->bo_appie += adjust;
3305				bo->bo_wme += adjust;
3306				bo->bo_csa += adjust;
3307				bo->bo_quiet += adjust;
3308				bo->bo_tim_len = timlen;
3309
3310				/* update information element */
3311				tie->tim_len = 3 + timlen;
3312				tie->tim_bitctl = timoff;
3313				len_changed = 1;
3314			}
3315			memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
3316				bo->bo_tim_len);
3317
3318			clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
3319
3320			IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
3321				"%s: TIM updated, pending %u, off %u, len %u\n",
3322				__func__, vap->iv_ps_pending, timoff, timlen);
3323		}
3324		/* count down DTIM period */
3325		if (tie->tim_count == 0)
3326			tie->tim_count = tie->tim_period - 1;
3327		else
3328			tie->tim_count--;
3329		/* update state for buffered multicast frames on DTIM */
3330		if (mcast && tie->tim_count == 0)
3331			tie->tim_bitctl |= 1;
3332		else
3333			tie->tim_bitctl &= ~1;
3334		if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
3335			struct ieee80211_csa_ie *csa =
3336			    (struct ieee80211_csa_ie *) bo->bo_csa;
3337
3338			/*
3339			 * Insert or update CSA ie.  If we're just starting
3340			 * to count down to the channel switch then we need
3341			 * to insert the CSA ie.  Otherwise we just need to
3342			 * drop the count.  The actual change happens above
3343			 * when the vap's count reaches the target count.
3344			 */
3345			if (vap->iv_csa_count == 0) {
3346				memmove(&csa[1], csa, bo->bo_csa_trailer_len);
3347				bo->bo_erp += sizeof(*csa);
3348				bo->bo_htinfo += sizeof(*csa);
3349				bo->bo_wme += sizeof(*csa);
3350#ifdef IEEE80211_SUPPORT_SUPERG
3351				bo->bo_ath += sizeof(*csa);
3352#endif
3353#ifdef IEEE80211_SUPPORT_TDMA
3354				bo->bo_tdma += sizeof(*csa);
3355#endif
3356#ifdef IEEE80211_SUPPORT_MESH
3357				bo->bo_meshconf += sizeof(*csa);
3358#endif
3359				bo->bo_appie += sizeof(*csa);
3360				bo->bo_csa_trailer_len += sizeof(*csa);
3361				bo->bo_quiet += sizeof(*csa);
3362				bo->bo_tim_trailer_len += sizeof(*csa);
3363				m->m_len += sizeof(*csa);
3364				m->m_pkthdr.len += sizeof(*csa);
3365
3366				ieee80211_add_csa(bo->bo_csa, vap);
3367			} else
3368				csa->csa_count--;
3369			vap->iv_csa_count++;
3370			/* NB: don't clear IEEE80211_BEACON_CSA */
3371		}
3372		if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
3373		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS) ){
3374			if (vap->iv_quiet)
3375				ieee80211_add_quiet(bo->bo_quiet, vap);
3376		}
3377		if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
3378			/*
3379			 * ERP element needs updating.
3380			 */
3381			(void) ieee80211_add_erp(bo->bo_erp, ic);
3382			clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
3383		}
3384#ifdef IEEE80211_SUPPORT_SUPERG
3385		if (isset(bo->bo_flags,  IEEE80211_BEACON_ATH)) {
3386			ieee80211_add_athcaps(bo->bo_ath, ni);
3387			clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
3388		}
3389#endif
3390	}
3391	if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
3392		const struct ieee80211_appie *aie = vap->iv_appie_beacon;
3393		int aielen;
3394		uint8_t *frm;
3395
3396		aielen = 0;
3397		if (aie != NULL)
3398			aielen += aie->ie_len;
3399		if (aielen != bo->bo_appie_len) {
3400			/* copy up/down trailer */
3401			int adjust = aielen - bo->bo_appie_len;
3402			ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
3403				bo->bo_tim_trailer_len);
3404			bo->bo_tim_trailer += adjust;
3405			bo->bo_appie += adjust;
3406			bo->bo_appie_len = aielen;
3407
3408			len_changed = 1;
3409		}
3410		frm = bo->bo_appie;
3411		if (aie != NULL)
3412			frm  = add_appie(frm, aie);
3413		clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
3414	}
3415	IEEE80211_UNLOCK(ic);
3416
3417	return len_changed;
3418}
3419
3420/*
3421 * Do Ethernet-LLC encapsulation for each payload in a fast frame
3422 * tunnel encapsulation.  The frame is assumed to have an Ethernet
3423 * header at the front that must be stripped before prepending the
3424 * LLC followed by the Ethernet header passed in (with an Ethernet
3425 * type that specifies the payload size).
3426 */
3427struct mbuf *
3428ieee80211_ff_encap1(struct ieee80211vap *vap, struct mbuf *m,
3429	const struct ether_header *eh)
3430{
3431	struct llc *llc;
3432	uint16_t payload;
3433
3434	/* XXX optimize by combining m_adj+M_PREPEND */
3435	m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
3436	llc = mtod(m, struct llc *);
3437	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
3438	llc->llc_control = LLC_UI;
3439	llc->llc_snap.org_code[0] = 0;
3440	llc->llc_snap.org_code[1] = 0;
3441	llc->llc_snap.org_code[2] = 0;
3442	llc->llc_snap.ether_type = eh->ether_type;
3443	payload = m->m_pkthdr.len;		/* NB: w/o Ethernet header */
3444
3445	M_PREPEND(m, sizeof(struct ether_header), M_NOWAIT);
3446	if (m == NULL) {		/* XXX cannot happen */
3447		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
3448			"%s: no space for ether_header\n", __func__);
3449		vap->iv_stats.is_tx_nobuf++;
3450		return NULL;
3451	}
3452	ETHER_HEADER_COPY(mtod(m, void *), eh);
3453	mtod(m, struct ether_header *)->ether_type = htons(payload);
3454	return m;
3455}
3456
3457/*
3458 * Complete an mbuf transmission.
3459 *
3460 * For now, this simply processes a completed frame after the
3461 * driver has completed it's transmission and/or retransmission.
3462 * It assumes the frame is an 802.11 encapsulated frame.
3463 *
3464 * Later on it will grow to become the exit path for a given frame
3465 * from the driver and, depending upon how it's been encapsulated
3466 * and already transmitted, it may end up doing A-MPDU retransmission,
3467 * power save requeuing, etc.
3468 *
3469 * In order for the above to work, the driver entry point to this
3470 * must not hold any driver locks.  Thus, the driver needs to delay
3471 * any actual mbuf completion until it can release said locks.
3472 *
3473 * This frees the mbuf and if the mbuf has a node reference,
3474 * the node reference will be freed.
3475 */
3476void
3477ieee80211_tx_complete(struct ieee80211_node *ni, struct mbuf *m, int status)
3478{
3479
3480	if (ni != NULL) {
3481		struct ifnet *ifp = ni->ni_vap->iv_ifp;
3482
3483		if (status == 0) {
3484			if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
3485			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
3486			if (m->m_flags & M_MCAST)
3487				if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
3488		} else
3489			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3490		if (m->m_flags & M_TXCB)
3491			ieee80211_process_callback(ni, m, status);
3492		ieee80211_free_node(ni);
3493	}
3494	m_freem(m);
3495}
3496