xref: /freebsd/sys/net80211/ieee80211_output.c (revision 3d0d5b21c9b2cfd7e48209340a090f63ca4bce03)
11a1e1d21SSam Leffler /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3fe267a55SPedro F. Giffuni  *
47535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
510ad9a77SSam Leffler  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
61a1e1d21SSam Leffler  * All rights reserved.
71a1e1d21SSam Leffler  *
81a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
91a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
101a1e1d21SSam Leffler  * are met:
111a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
127535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
137535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
147535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
157535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
161a1e1d21SSam Leffler  *
177535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
187535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
197535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
207535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
217535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
227535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
237535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
247535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
257535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
267535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
271a1e1d21SSam Leffler  */
281a1e1d21SSam Leffler 
291a1e1d21SSam Leffler #include <sys/cdefs.h>
301a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
311a1e1d21SSam Leffler 
321a1e1d21SSam Leffler #include "opt_inet.h"
33e755a73dSSam Leffler #include "opt_inet6.h"
34b032f27cSSam Leffler #include "opt_wlan.h"
351a1e1d21SSam Leffler 
361a1e1d21SSam Leffler #include <sys/param.h>
371a1e1d21SSam Leffler #include <sys/systm.h>
381a1e1d21SSam Leffler #include <sys/kernel.h>
398ec07310SGleb Smirnoff #include <sys/malloc.h>
408ec07310SGleb Smirnoff #include <sys/mbuf.h>
411a1e1d21SSam Leffler #include <sys/endian.h>
421a1e1d21SSam Leffler 
438a1b9b6aSSam Leffler #include <sys/socket.h>
441a1e1d21SSam Leffler 
451a1e1d21SSam Leffler #include <net/bpf.h>
468a1b9b6aSSam Leffler #include <net/ethernet.h>
478a1b9b6aSSam Leffler #include <net/if.h>
4876039bc8SGleb Smirnoff #include <net/if_var.h>
498a1b9b6aSSam Leffler #include <net/if_llc.h>
508a1b9b6aSSam Leffler #include <net/if_media.h>
51*3d0d5b21SJustin Hibbits #include <net/if_private.h>
528a1b9b6aSSam Leffler #include <net/if_vlan_var.h>
538a1b9b6aSSam Leffler 
548a1b9b6aSSam Leffler #include <net80211/ieee80211_var.h>
5568e8e04eSSam Leffler #include <net80211/ieee80211_regdomain.h>
56616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
57616190d0SSam Leffler #include <net80211/ieee80211_superg.h>
58616190d0SSam Leffler #endif
5910ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
6010ad9a77SSam Leffler #include <net80211/ieee80211_tdma.h>
6110ad9a77SSam Leffler #endif
62b032f27cSSam Leffler #include <net80211/ieee80211_wds.h>
6359aa14a9SRui Paulo #include <net80211/ieee80211_mesh.h>
6451172f62SAdrian Chadd #include <net80211/ieee80211_vht.h>
651a1e1d21SSam Leffler 
668355d59dSBjoern A. Zeeb #if defined(INET) || defined(INET6)
671a1e1d21SSam Leffler #include <netinet/in.h>
688355d59dSBjoern A. Zeeb #endif
698355d59dSBjoern A. Zeeb 
708355d59dSBjoern A. Zeeb #ifdef INET
711a1e1d21SSam Leffler #include <netinet/if_ether.h>
728a1b9b6aSSam Leffler #include <netinet/in_systm.h>
738a1b9b6aSSam Leffler #include <netinet/ip.h>
748a1b9b6aSSam Leffler #endif
75e755a73dSSam Leffler #ifdef INET6
76e755a73dSSam Leffler #include <netinet/ip6.h>
77e755a73dSSam Leffler #endif
788a1b9b6aSSam Leffler 
79c27b9cdbSRobert Watson #include <security/mac/mac_framework.h>
80c27b9cdbSRobert Watson 
8168e8e04eSSam Leffler #define	ETHER_HEADER_COPY(dst, src) \
8268e8e04eSSam Leffler 	memcpy(dst, src, sizeof(struct ether_header))
8368e8e04eSSam Leffler 
84b032f27cSSam Leffler static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
8568e8e04eSSam Leffler 	u_int hdrsize, u_int ciphdrsize, u_int mtu);
8668e8e04eSSam Leffler static	void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
8768e8e04eSSam Leffler 
888a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
898a1b9b6aSSam Leffler /*
908a1b9b6aSSam Leffler  * Decide if an outbound management frame should be
918a1b9b6aSSam Leffler  * printed when debugging is enabled.  This filters some
928a1b9b6aSSam Leffler  * of the less interesting frames that come frequently
938a1b9b6aSSam Leffler  * (e.g. beacons).
948a1b9b6aSSam Leffler  */
958a1b9b6aSSam Leffler static __inline int
96b032f27cSSam Leffler doprint(struct ieee80211vap *vap, int subtype)
978a1b9b6aSSam Leffler {
988a1b9b6aSSam Leffler 	switch (subtype) {
998a1b9b6aSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
100b032f27cSSam Leffler 		return (vap->iv_opmode == IEEE80211_M_IBSS);
1018a1b9b6aSSam Leffler 	}
1028a1b9b6aSSam Leffler 	return 1;
1038a1b9b6aSSam Leffler }
1041a1e1d21SSam Leffler #endif
1051a1e1d21SSam Leffler 
1060a915fadSSam Leffler /*
107363a2c3cSAdrian Chadd  * Transmit a frame to the given destination on the given VAP.
108363a2c3cSAdrian Chadd  *
109363a2c3cSAdrian Chadd  * It's up to the caller to figure out the details of who this
110363a2c3cSAdrian Chadd  * is going to and resolving the node.
111363a2c3cSAdrian Chadd  *
112363a2c3cSAdrian Chadd  * This routine takes care of queuing it for power save,
113363a2c3cSAdrian Chadd  * A-MPDU state stuff, fast-frames state stuff, encapsulation
114363a2c3cSAdrian Chadd  * if required, then passing it up to the driver layer.
115363a2c3cSAdrian Chadd  *
116363a2c3cSAdrian Chadd  * This routine (for now) consumes the mbuf and frees the node
117363a2c3cSAdrian Chadd  * reference; it ideally will return a TX status which reflects
118363a2c3cSAdrian Chadd  * whether the mbuf was consumed or not, so the caller can
119363a2c3cSAdrian Chadd  * free the mbuf (if appropriate) and the node reference (again,
120363a2c3cSAdrian Chadd  * if appropriate.)
121363a2c3cSAdrian Chadd  */
122363a2c3cSAdrian Chadd int
123363a2c3cSAdrian Chadd ieee80211_vap_pkt_send_dest(struct ieee80211vap *vap, struct mbuf *m,
124363a2c3cSAdrian Chadd     struct ieee80211_node *ni)
125363a2c3cSAdrian Chadd {
126363a2c3cSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
127363a2c3cSAdrian Chadd 	struct ifnet *ifp = vap->iv_ifp;
1282e9090dfSBjoern A. Zeeb 	int mcast;
129b3d0e274SAdrian Chadd 	int do_ampdu = 0;
13005ea7a3eSBjoern A. Zeeb #ifdef IEEE80211_SUPPORT_SUPERG
131b3d0e274SAdrian Chadd 	int do_amsdu = 0;
132b3d0e274SAdrian Chadd 	int do_ampdu_amsdu = 0;
133b3d0e274SAdrian Chadd 	int no_ampdu = 1; /* Will be set to 0 if ampdu is active */
134b3d0e274SAdrian Chadd 	int do_ff = 0;
13505ea7a3eSBjoern A. Zeeb #endif
136363a2c3cSAdrian Chadd 
137363a2c3cSAdrian Chadd 	if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
138363a2c3cSAdrian Chadd 	    (m->m_flags & M_PWR_SAV) == 0) {
139363a2c3cSAdrian Chadd 		/*
140363a2c3cSAdrian Chadd 		 * Station in power save mode; pass the frame
141363a2c3cSAdrian Chadd 		 * to the 802.11 layer and continue.  We'll get
142363a2c3cSAdrian Chadd 		 * the frame back when the time is right.
143363a2c3cSAdrian Chadd 		 * XXX lose WDS vap linkage?
144363a2c3cSAdrian Chadd 		 */
14579077edcSAndrey V. Elsukov 		if (ieee80211_pwrsave(ni, m) != 0)
14679077edcSAndrey V. Elsukov 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
147363a2c3cSAdrian Chadd 		ieee80211_free_node(ni);
148c6d5b600SAdrian Chadd 
149c6d5b600SAdrian Chadd 		/*
150c6d5b600SAdrian Chadd 		 * We queued it fine, so tell the upper layer
151c6d5b600SAdrian Chadd 		 * that we consumed it.
152c6d5b600SAdrian Chadd 		 */
153c6d5b600SAdrian Chadd 		return (0);
154363a2c3cSAdrian Chadd 	}
155363a2c3cSAdrian Chadd 	/* calculate priority so drivers can find the tx queue */
156363a2c3cSAdrian Chadd 	if (ieee80211_classify(ni, m)) {
157363a2c3cSAdrian Chadd 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
158363a2c3cSAdrian Chadd 		    ni->ni_macaddr, NULL,
159363a2c3cSAdrian Chadd 		    "%s", "classification failure");
160363a2c3cSAdrian Chadd 		vap->iv_stats.is_tx_classify++;
161dea45121SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
162363a2c3cSAdrian Chadd 		m_freem(m);
163363a2c3cSAdrian Chadd 		ieee80211_free_node(ni);
164c6d5b600SAdrian Chadd 
165363a2c3cSAdrian Chadd 		/* XXX better status? */
166c6d5b600SAdrian Chadd 		return (0);
167363a2c3cSAdrian Chadd 	}
168363a2c3cSAdrian Chadd 	/*
169363a2c3cSAdrian Chadd 	 * Stash the node pointer.  Note that we do this after
170363a2c3cSAdrian Chadd 	 * any call to ieee80211_dwds_mcast because that code
171363a2c3cSAdrian Chadd 	 * uses any existing value for rcvif to identify the
172363a2c3cSAdrian Chadd 	 * interface it (might have been) received on.
173363a2c3cSAdrian Chadd 	 */
174fb3bc596SJohn Baldwin 	MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
175363a2c3cSAdrian Chadd 	m->m_pkthdr.rcvif = (void *)ni;
1762e9090dfSBjoern A. Zeeb 	mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1: 0;
177363a2c3cSAdrian Chadd 
178363a2c3cSAdrian Chadd 	BPF_MTAP(ifp, m);		/* 802.3 tx */
179363a2c3cSAdrian Chadd 
180b3d0e274SAdrian Chadd 	/*
181b3d0e274SAdrian Chadd 	 * Figure out if we can do A-MPDU, A-MSDU or FF.
182b3d0e274SAdrian Chadd 	 *
183b3d0e274SAdrian Chadd 	 * A-MPDU depends upon vap/node config.
184b3d0e274SAdrian Chadd 	 * A-MSDU depends upon vap/node config.
185b3d0e274SAdrian Chadd 	 * FF depends upon vap config, IE and whether
186b3d0e274SAdrian Chadd 	 *  it's 11abg (and not 11n/11ac/etc.)
187b3d0e274SAdrian Chadd 	 *
188b3d0e274SAdrian Chadd 	 * Note that these flags indiciate whether we can do
189b3d0e274SAdrian Chadd 	 * it at all, rather than the situation (eg traffic type.)
190b3d0e274SAdrian Chadd 	 */
191b3d0e274SAdrian Chadd 	do_ampdu = ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
192b3d0e274SAdrian Chadd 	    (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX));
19305ea7a3eSBjoern A. Zeeb #ifdef IEEE80211_SUPPORT_SUPERG
194b3d0e274SAdrian Chadd 	do_amsdu = ((ni->ni_flags & IEEE80211_NODE_AMSDU_TX) &&
195b3d0e274SAdrian Chadd 	    (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_TX));
196b3d0e274SAdrian Chadd 	do_ff =
197b3d0e274SAdrian Chadd 	    ((ni->ni_flags & IEEE80211_NODE_HT) == 0) &&
198b3d0e274SAdrian Chadd 	    ((ni->ni_flags & IEEE80211_NODE_VHT) == 0) &&
199b3d0e274SAdrian Chadd 	    (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF));
20005ea7a3eSBjoern A. Zeeb #endif
201b3d0e274SAdrian Chadd 
202363a2c3cSAdrian Chadd 	/*
203363a2c3cSAdrian Chadd 	 * Check if A-MPDU tx aggregation is setup or if we
204363a2c3cSAdrian Chadd 	 * should try to enable it.  The sta must be associated
205363a2c3cSAdrian Chadd 	 * with HT and A-MPDU enabled for use.  When the policy
206363a2c3cSAdrian Chadd 	 * routine decides we should enable A-MPDU we issue an
207363a2c3cSAdrian Chadd 	 * ADDBA request and wait for a reply.  The frame being
208363a2c3cSAdrian Chadd 	 * encapsulated will go out w/o using A-MPDU, or possibly
209363a2c3cSAdrian Chadd 	 * it might be collected by the driver and held/retransmit.
210363a2c3cSAdrian Chadd 	 * The default ic_ampdu_enable routine handles staggering
211363a2c3cSAdrian Chadd 	 * ADDBA requests in case the receiver NAK's us or we are
212363a2c3cSAdrian Chadd 	 * otherwise unable to establish a BA stream.
2139764ef21SAdrian Chadd 	 *
2149764ef21SAdrian Chadd 	 * Don't treat group-addressed frames as candidates for aggregation;
2159764ef21SAdrian Chadd 	 * net80211 doesn't support 802.11aa-2012 and so group addressed
2169764ef21SAdrian Chadd 	 * frames will always have sequence numbers allocated from the NON_QOS
2179764ef21SAdrian Chadd 	 * TID.
218363a2c3cSAdrian Chadd 	 */
219b3d0e274SAdrian Chadd 	if (do_ampdu) {
2209764ef21SAdrian Chadd 		if ((m->m_flags & M_EAPOL) == 0 && (! mcast)) {
221363a2c3cSAdrian Chadd 			int tid = WME_AC_TO_TID(M_WME_GETAC(m));
222363a2c3cSAdrian Chadd 			struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[tid];
223363a2c3cSAdrian Chadd 
224363a2c3cSAdrian Chadd 			ieee80211_txampdu_count_packet(tap);
225363a2c3cSAdrian Chadd 			if (IEEE80211_AMPDU_RUNNING(tap)) {
226363a2c3cSAdrian Chadd 				/*
227363a2c3cSAdrian Chadd 				 * Operational, mark frame for aggregation.
228363a2c3cSAdrian Chadd 				 *
229363a2c3cSAdrian Chadd 				 * XXX do tx aggregation here
230363a2c3cSAdrian Chadd 				 */
231363a2c3cSAdrian Chadd 				m->m_flags |= M_AMPDU_MPDU;
232363a2c3cSAdrian Chadd 			} else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
233363a2c3cSAdrian Chadd 			    ic->ic_ampdu_enable(ni, tap)) {
234363a2c3cSAdrian Chadd 				/*
235363a2c3cSAdrian Chadd 				 * Not negotiated yet, request service.
236363a2c3cSAdrian Chadd 				 */
237363a2c3cSAdrian Chadd 				ieee80211_ampdu_request(ni, tap);
238363a2c3cSAdrian Chadd 				/* XXX hold frame for reply? */
239363a2c3cSAdrian Chadd 			}
240b3d0e274SAdrian Chadd 			/*
241b3d0e274SAdrian Chadd 			 * Now update the no-ampdu flag.  A-MPDU may have been
242b3d0e274SAdrian Chadd 			 * started or administratively disabled above; so now we
243b3d0e274SAdrian Chadd 			 * know whether we're running yet or not.
244b3d0e274SAdrian Chadd 			 *
245b3d0e274SAdrian Chadd 			 * This will let us know whether we should be doing A-MSDU
246b3d0e274SAdrian Chadd 			 * at this point.  We only do A-MSDU if we're either not
247b3d0e274SAdrian Chadd 			 * doing A-MPDU, or A-MPDU is NACKed, or A-MPDU + A-MSDU
248b3d0e274SAdrian Chadd 			 * is available.
249b3d0e274SAdrian Chadd 			 *
250b3d0e274SAdrian Chadd 			 * Whilst here, update the amsdu-ampdu flag.  The above may
251b3d0e274SAdrian Chadd 			 * have also set or cleared the amsdu-in-ampdu txa_flags
252b3d0e274SAdrian Chadd 			 * combination so we can correctly do A-MPDU + A-MSDU.
253b3d0e274SAdrian Chadd 			 */
25405ea7a3eSBjoern A. Zeeb #ifdef IEEE80211_SUPPORT_SUPERG
255b3d0e274SAdrian Chadd 			no_ampdu = (! IEEE80211_AMPDU_RUNNING(tap)
256b3d0e274SAdrian Chadd 			    || (IEEE80211_AMPDU_NACKED(tap)));
257b3d0e274SAdrian Chadd 			do_ampdu_amsdu = IEEE80211_AMPDU_RUNNING_AMSDU(tap);
25805ea7a3eSBjoern A. Zeeb #endif
259363a2c3cSAdrian Chadd 		}
2601c7b0c84SAdrian Chadd 	}
261363a2c3cSAdrian Chadd 
262363a2c3cSAdrian Chadd #ifdef IEEE80211_SUPPORT_SUPERG
2631c7b0c84SAdrian Chadd 	/*
2641c7b0c84SAdrian Chadd 	 * Check for AMSDU/FF; queue for aggregation
2651c7b0c84SAdrian Chadd 	 *
2661c7b0c84SAdrian Chadd 	 * Note: we don't bother trying to do fast frames or
2671c7b0c84SAdrian Chadd 	 * A-MSDU encapsulation for 802.3 drivers.  Now, we
2681c7b0c84SAdrian Chadd 	 * likely could do it for FF (because it's a magic
2691c7b0c84SAdrian Chadd 	 * atheros tunnel LLC type) but I don't think we're going
2701c7b0c84SAdrian Chadd 	 * to really need to.  For A-MSDU we'd have to set the
2711c7b0c84SAdrian Chadd 	 * A-MSDU QoS bit in the wifi header, so we just plain
2721c7b0c84SAdrian Chadd 	 * can't do it.
2731c7b0c84SAdrian Chadd 	 */
274b3d0e274SAdrian Chadd 	if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
275b3d0e274SAdrian Chadd 		if ((! mcast) &&
276b3d0e274SAdrian Chadd 		    (do_ampdu_amsdu || (no_ampdu && do_amsdu)) &&
277b3d0e274SAdrian Chadd 		    ieee80211_amsdu_tx_ok(ni)) {
2781c7b0c84SAdrian Chadd 			m = ieee80211_amsdu_check(ni, m);
2791c7b0c84SAdrian Chadd 			if (m == NULL) {
2801c7b0c84SAdrian Chadd 				/* NB: any ni ref held on stageq */
2811c7b0c84SAdrian Chadd 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
2821c7b0c84SAdrian Chadd 				    "%s: amsdu_check queued frame\n",
2831c7b0c84SAdrian Chadd 				    __func__);
2841c7b0c84SAdrian Chadd 				return (0);
2851c7b0c84SAdrian Chadd 			}
286b3d0e274SAdrian Chadd 		} else if ((! mcast) && do_ff) {
287363a2c3cSAdrian Chadd 			m = ieee80211_ff_check(ni, m);
288363a2c3cSAdrian Chadd 			if (m == NULL) {
289363a2c3cSAdrian Chadd 				/* NB: any ni ref held on stageq */
2901c7b0c84SAdrian Chadd 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
2911c7b0c84SAdrian Chadd 				    "%s: ff_check queued frame\n",
2921c7b0c84SAdrian Chadd 				    __func__);
29311e0ddb1SAdrian Chadd 				return (0);
294363a2c3cSAdrian Chadd 			}
295363a2c3cSAdrian Chadd 		}
2961c7b0c84SAdrian Chadd 	}
297363a2c3cSAdrian Chadd #endif /* IEEE80211_SUPPORT_SUPERG */
298363a2c3cSAdrian Chadd 
299363a2c3cSAdrian Chadd 	/*
300363a2c3cSAdrian Chadd 	 * Grab the TX lock - serialise the TX process from this
301363a2c3cSAdrian Chadd 	 * point (where TX state is being checked/modified)
302363a2c3cSAdrian Chadd 	 * through to driver queue.
303363a2c3cSAdrian Chadd 	 */
304363a2c3cSAdrian Chadd 	IEEE80211_TX_LOCK(ic);
305363a2c3cSAdrian Chadd 
306f9128a1bSAdrian Chadd 	/*
307f9128a1bSAdrian Chadd 	 * XXX make the encap and transmit code a separate function
308f9128a1bSAdrian Chadd 	 * so things like the FF (and later A-MSDU) path can just call
309f9128a1bSAdrian Chadd 	 * it for flushed frames.
310f9128a1bSAdrian Chadd 	 */
311363a2c3cSAdrian Chadd 	if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
312363a2c3cSAdrian Chadd 		/*
313363a2c3cSAdrian Chadd 		 * Encapsulate the packet in prep for transmission.
314363a2c3cSAdrian Chadd 		 */
315363a2c3cSAdrian Chadd 		m = ieee80211_encap(vap, ni, m);
316363a2c3cSAdrian Chadd 		if (m == NULL) {
317363a2c3cSAdrian Chadd 			/* NB: stat+msg handled in ieee80211_encap */
318363a2c3cSAdrian Chadd 			IEEE80211_TX_UNLOCK(ic);
319363a2c3cSAdrian Chadd 			ieee80211_free_node(ni);
32079077edcSAndrey V. Elsukov 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
321363a2c3cSAdrian Chadd 			return (ENOBUFS);
322363a2c3cSAdrian Chadd 		}
323363a2c3cSAdrian Chadd 	}
324d3a4ade3SAdrian Chadd 	(void) ieee80211_parent_xmitpkt(ic, m);
325363a2c3cSAdrian Chadd 
326363a2c3cSAdrian Chadd 	/*
327363a2c3cSAdrian Chadd 	 * Unlock at this point - no need to hold it across
328363a2c3cSAdrian Chadd 	 * ieee80211_free_node() (ie, the comlock)
329363a2c3cSAdrian Chadd 	 */
330363a2c3cSAdrian Chadd 	IEEE80211_TX_UNLOCK(ic);
331363a2c3cSAdrian Chadd 	ic->ic_lastdata = ticks;
332363a2c3cSAdrian Chadd 
333363a2c3cSAdrian Chadd 	return (0);
334363a2c3cSAdrian Chadd }
335363a2c3cSAdrian Chadd 
336363a2c3cSAdrian Chadd /*
3375cda6006SAdrian Chadd  * Send the given mbuf through the given vap.
3385cda6006SAdrian Chadd  *
3395cda6006SAdrian Chadd  * This consumes the mbuf regardless of whether the transmit
3405cda6006SAdrian Chadd  * was successful or not.
3415cda6006SAdrian Chadd  *
3425cda6006SAdrian Chadd  * This does none of the initial checks that ieee80211_start()
3435cda6006SAdrian Chadd  * does (eg CAC timeout, interface wakeup) - the caller must
3445cda6006SAdrian Chadd  * do this first.
3455cda6006SAdrian Chadd  */
3465cda6006SAdrian Chadd static int
3475cda6006SAdrian Chadd ieee80211_start_pkt(struct ieee80211vap *vap, struct mbuf *m)
3485cda6006SAdrian Chadd {
3495cda6006SAdrian Chadd #define	IS_DWDS(vap) \
3505cda6006SAdrian Chadd 	(vap->iv_opmode == IEEE80211_M_WDS && \
3515cda6006SAdrian Chadd 	 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
3525cda6006SAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
3535cda6006SAdrian Chadd 	struct ifnet *ifp = vap->iv_ifp;
3545cda6006SAdrian Chadd 	struct ieee80211_node *ni;
3555cda6006SAdrian Chadd 	struct ether_header *eh;
3565cda6006SAdrian Chadd 
3575cda6006SAdrian Chadd 	/*
3585cda6006SAdrian Chadd 	 * Cancel any background scan.
3595cda6006SAdrian Chadd 	 */
3605cda6006SAdrian Chadd 	if (ic->ic_flags & IEEE80211_F_SCAN)
3615cda6006SAdrian Chadd 		ieee80211_cancel_anyscan(vap);
3625cda6006SAdrian Chadd 	/*
3635cda6006SAdrian Chadd 	 * Find the node for the destination so we can do
3645cda6006SAdrian Chadd 	 * things like power save and fast frames aggregation.
3655cda6006SAdrian Chadd 	 *
3665cda6006SAdrian Chadd 	 * NB: past this point various code assumes the first
3675cda6006SAdrian Chadd 	 *     mbuf has the 802.3 header present (and contiguous).
3685cda6006SAdrian Chadd 	 */
3695cda6006SAdrian Chadd 	ni = NULL;
3705cda6006SAdrian Chadd 	if (m->m_len < sizeof(struct ether_header) &&
3715cda6006SAdrian Chadd 	   (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
3725cda6006SAdrian Chadd 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
3735cda6006SAdrian Chadd 		    "discard frame, %s\n", "m_pullup failed");
3745cda6006SAdrian Chadd 		vap->iv_stats.is_tx_nobuf++;	/* XXX */
375dea45121SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3765cda6006SAdrian Chadd 		return (ENOBUFS);
3775cda6006SAdrian Chadd 	}
3785cda6006SAdrian Chadd 	eh = mtod(m, struct ether_header *);
3795cda6006SAdrian Chadd 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
3805cda6006SAdrian Chadd 		if (IS_DWDS(vap)) {
3815cda6006SAdrian Chadd 			/*
3825cda6006SAdrian Chadd 			 * Only unicast frames from the above go out
3835cda6006SAdrian Chadd 			 * DWDS vaps; multicast frames are handled by
3845cda6006SAdrian Chadd 			 * dispatching the frame as it comes through
3855cda6006SAdrian Chadd 			 * the AP vap (see below).
3865cda6006SAdrian Chadd 			 */
3875cda6006SAdrian Chadd 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
3885cda6006SAdrian Chadd 			    eh->ether_dhost, "mcast", "%s", "on DWDS");
3895cda6006SAdrian Chadd 			vap->iv_stats.is_dwds_mcast++;
3905cda6006SAdrian Chadd 			m_freem(m);
39179077edcSAndrey V. Elsukov 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3925cda6006SAdrian Chadd 			/* XXX better status? */
3935cda6006SAdrian Chadd 			return (ENOBUFS);
3945cda6006SAdrian Chadd 		}
3955cda6006SAdrian Chadd 		if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
3965cda6006SAdrian Chadd 			/*
3975cda6006SAdrian Chadd 			 * Spam DWDS vap's w/ multicast traffic.
3985cda6006SAdrian Chadd 			 */
3995cda6006SAdrian Chadd 			/* XXX only if dwds in use? */
4005cda6006SAdrian Chadd 			ieee80211_dwds_mcast(vap, m);
4015cda6006SAdrian Chadd 		}
4025cda6006SAdrian Chadd 	}
4035cda6006SAdrian Chadd #ifdef IEEE80211_SUPPORT_MESH
4045cda6006SAdrian Chadd 	if (vap->iv_opmode != IEEE80211_M_MBSS) {
4055cda6006SAdrian Chadd #endif
4065cda6006SAdrian Chadd 		ni = ieee80211_find_txnode(vap, eh->ether_dhost);
4075cda6006SAdrian Chadd 		if (ni == NULL) {
4085cda6006SAdrian Chadd 			/* NB: ieee80211_find_txnode does stat+msg */
409dea45121SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
4105cda6006SAdrian Chadd 			m_freem(m);
4115cda6006SAdrian Chadd 			/* XXX better status? */
4125cda6006SAdrian Chadd 			return (ENOBUFS);
4135cda6006SAdrian Chadd 		}
4145cda6006SAdrian Chadd 		if (ni->ni_associd == 0 &&
4155cda6006SAdrian Chadd 		    (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
4165cda6006SAdrian Chadd 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
4175cda6006SAdrian Chadd 			    eh->ether_dhost, NULL,
4185cda6006SAdrian Chadd 			    "sta not associated (type 0x%04x)",
4195cda6006SAdrian Chadd 			    htons(eh->ether_type));
4205cda6006SAdrian Chadd 			vap->iv_stats.is_tx_notassoc++;
421dea45121SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
4225cda6006SAdrian Chadd 			m_freem(m);
4235cda6006SAdrian Chadd 			ieee80211_free_node(ni);
4245cda6006SAdrian Chadd 			/* XXX better status? */
4255cda6006SAdrian Chadd 			return (ENOBUFS);
4265cda6006SAdrian Chadd 		}
4275cda6006SAdrian Chadd #ifdef IEEE80211_SUPPORT_MESH
4285cda6006SAdrian Chadd 	} else {
4295cda6006SAdrian Chadd 		if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) {
4305cda6006SAdrian Chadd 			/*
4315cda6006SAdrian Chadd 			 * Proxy station only if configured.
4325cda6006SAdrian Chadd 			 */
4335cda6006SAdrian Chadd 			if (!ieee80211_mesh_isproxyena(vap)) {
4345cda6006SAdrian Chadd 				IEEE80211_DISCARD_MAC(vap,
4355cda6006SAdrian Chadd 				    IEEE80211_MSG_OUTPUT |
4365cda6006SAdrian Chadd 				    IEEE80211_MSG_MESH,
4375cda6006SAdrian Chadd 				    eh->ether_dhost, NULL,
4385cda6006SAdrian Chadd 				    "%s", "proxy not enabled");
4395cda6006SAdrian Chadd 				vap->iv_stats.is_mesh_notproxy++;
440dea45121SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
4415cda6006SAdrian Chadd 				m_freem(m);
4425cda6006SAdrian Chadd 				/* XXX better status? */
4435cda6006SAdrian Chadd 				return (ENOBUFS);
4445cda6006SAdrian Chadd 			}
4455cda6006SAdrian Chadd 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
4465cda6006SAdrian Chadd 			    "forward frame from DS SA(%6D), DA(%6D)\n",
4475cda6006SAdrian Chadd 			    eh->ether_shost, ":",
4485cda6006SAdrian Chadd 			    eh->ether_dhost, ":");
4495cda6006SAdrian Chadd 			ieee80211_mesh_proxy_check(vap, eh->ether_shost);
4505cda6006SAdrian Chadd 		}
4515cda6006SAdrian Chadd 		ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m);
4525cda6006SAdrian Chadd 		if (ni == NULL) {
4535cda6006SAdrian Chadd 			/*
4545cda6006SAdrian Chadd 			 * NB: ieee80211_mesh_discover holds/disposes
4555cda6006SAdrian Chadd 			 * frame (e.g. queueing on path discovery).
4565cda6006SAdrian Chadd 			 */
457dea45121SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
4585cda6006SAdrian Chadd 			/* XXX better status? */
4595cda6006SAdrian Chadd 			return (ENOBUFS);
4605cda6006SAdrian Chadd 		}
4615cda6006SAdrian Chadd 	}
4625cda6006SAdrian Chadd #endif
463363a2c3cSAdrian Chadd 
4645cda6006SAdrian Chadd 	/*
465363a2c3cSAdrian Chadd 	 * We've resolved the sender, so attempt to transmit it.
4665cda6006SAdrian Chadd 	 */
467ddd9ebbcSAdrian Chadd 
468ddd9ebbcSAdrian Chadd 	if (vap->iv_state == IEEE80211_S_SLEEP) {
469ddd9ebbcSAdrian Chadd 		/*
470ddd9ebbcSAdrian Chadd 		 * In power save; queue frame and then  wakeup device
471ddd9ebbcSAdrian Chadd 		 * for transmit.
472ddd9ebbcSAdrian Chadd 		 */
473ddd9ebbcSAdrian Chadd 		ic->ic_lastdata = ticks;
47479077edcSAndrey V. Elsukov 		if (ieee80211_pwrsave(ni, m) != 0)
47579077edcSAndrey V. Elsukov 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
476ddd9ebbcSAdrian Chadd 		ieee80211_free_node(ni);
477ddd9ebbcSAdrian Chadd 		ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
478ddd9ebbcSAdrian Chadd 		return (0);
479ddd9ebbcSAdrian Chadd 	}
480ddd9ebbcSAdrian Chadd 
481363a2c3cSAdrian Chadd 	if (ieee80211_vap_pkt_send_dest(vap, m, ni) != 0)
4825cda6006SAdrian Chadd 		return (ENOBUFS);
4835cda6006SAdrian Chadd 	return (0);
4845cda6006SAdrian Chadd #undef	IS_DWDS
4855cda6006SAdrian Chadd }
4865cda6006SAdrian Chadd 
4875cda6006SAdrian Chadd /*
488b032f27cSSam Leffler  * Start method for vap's.  All packets from the stack come
489b032f27cSSam Leffler  * through here.  We handle common processing of the packets
490b032f27cSSam Leffler  * before dispatching them to the underlying device.
4911df885c8SAdrian Chadd  *
4921df885c8SAdrian Chadd  * if_transmit() requires that the mbuf be consumed by this call
4931df885c8SAdrian Chadd  * regardless of the return condition.
494b032f27cSSam Leffler  */
495e7495198SAdrian Chadd int
496e7495198SAdrian Chadd ieee80211_vap_transmit(struct ifnet *ifp, struct mbuf *m)
497b032f27cSSam Leffler {
498b032f27cSSam Leffler 	struct ieee80211vap *vap = ifp->if_softc;
499b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
500ddd9ebbcSAdrian Chadd 
501b032f27cSSam Leffler 	/*
502b032f27cSSam Leffler 	 * No data frames go out unless we're running.
503b032f27cSSam Leffler 	 * Note in particular this covers CAC and CSA
504b032f27cSSam Leffler 	 * states (though maybe we should check muting
505b032f27cSSam Leffler 	 * for CSA).
506b032f27cSSam Leffler 	 */
507ddd9ebbcSAdrian Chadd 	if (vap->iv_state != IEEE80211_S_RUN &&
508ddd9ebbcSAdrian Chadd 	    vap->iv_state != IEEE80211_S_SLEEP) {
509b032f27cSSam Leffler 		IEEE80211_LOCK(ic);
510b032f27cSSam Leffler 		/* re-check under the com lock to avoid races */
511ddd9ebbcSAdrian Chadd 		if (vap->iv_state != IEEE80211_S_RUN &&
512ddd9ebbcSAdrian Chadd 		    vap->iv_state != IEEE80211_S_SLEEP) {
513b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
514b032f27cSSam Leffler 			    "%s: ignore queue, in %s state\n",
515b032f27cSSam Leffler 			    __func__, ieee80211_state_name[vap->iv_state]);
516b032f27cSSam Leffler 			vap->iv_stats.is_tx_badstate++;
517b032f27cSSam Leffler 			IEEE80211_UNLOCK(ic);
51870e0b5acSAdrian Chadd 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
5191df885c8SAdrian Chadd 			m_freem(m);
52079077edcSAndrey V. Elsukov 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
52179077edcSAndrey V. Elsukov 			return (ENETDOWN);
522b032f27cSSam Leffler 		}
523b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
524b032f27cSSam Leffler 	}
5255cda6006SAdrian Chadd 
526b032f27cSSam Leffler 	/*
527b032f27cSSam Leffler 	 * Sanitize mbuf flags for net80211 use.  We cannot
528927ef5ffSSam Leffler 	 * clear M_PWR_SAV or M_MORE_DATA because these may
529927ef5ffSSam Leffler 	 * be set for frames that are re-submitted from the
530927ef5ffSSam Leffler 	 * power save queue.
531b032f27cSSam Leffler 	 *
532b032f27cSSam Leffler 	 * NB: This must be done before ieee80211_classify as
533b032f27cSSam Leffler 	 *     it marks EAPOL in frames with M_EAPOL.
534b032f27cSSam Leffler 	 */
535927ef5ffSSam Leffler 	m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
536e7495198SAdrian Chadd 
537b032f27cSSam Leffler 	/*
5385cda6006SAdrian Chadd 	 * Bump to the packet transmission path.
539e7495198SAdrian Chadd 	 * The mbuf will be consumed here.
540b032f27cSSam Leffler 	 */
541e7495198SAdrian Chadd 	return (ieee80211_start_pkt(vap, m));
542b032f27cSSam Leffler }
543e7495198SAdrian Chadd 
544e7495198SAdrian Chadd void
545e7495198SAdrian Chadd ieee80211_vap_qflush(struct ifnet *ifp)
546e7495198SAdrian Chadd {
547e7495198SAdrian Chadd 
548e7495198SAdrian Chadd 	/* Empty for now */
549b032f27cSSam Leffler }
550b032f27cSSam Leffler 
551168f582eSSam Leffler /*
5525cda6006SAdrian Chadd  * 802.11 raw output routine.
553ddd9ebbcSAdrian Chadd  *
554ddd9ebbcSAdrian Chadd  * XXX TODO: this (and other send routines) should correctly
555ddd9ebbcSAdrian Chadd  * XXX keep the pwr mgmt bit set if it decides to call into the
556ddd9ebbcSAdrian Chadd  * XXX driver to send a frame whilst the state is SLEEP.
557ddd9ebbcSAdrian Chadd  *
558ddd9ebbcSAdrian Chadd  * Otherwise the peer may decide that we're awake and flood us
559ddd9ebbcSAdrian Chadd  * with traffic we are still too asleep to receive!
560168f582eSSam Leffler  */
5615cda6006SAdrian Chadd int
5625cda6006SAdrian Chadd ieee80211_raw_output(struct ieee80211vap *vap, struct ieee80211_node *ni,
5635cda6006SAdrian Chadd     struct mbuf *m, const struct ieee80211_bpf_params *params)
5645cda6006SAdrian Chadd {
5655cda6006SAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
5667a79cebfSGleb Smirnoff 	int error;
567168f582eSSam Leffler 
568ff09e23fSAdrian Chadd 	/*
569ff09e23fSAdrian Chadd 	 * Set node - the caller has taken a reference, so ensure
570ff09e23fSAdrian Chadd 	 * that the mbuf has the same node value that
571ff09e23fSAdrian Chadd 	 * it would if it were going via the normal path.
572ff09e23fSAdrian Chadd 	 */
573fb3bc596SJohn Baldwin 	MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
574ff09e23fSAdrian Chadd 	m->m_pkthdr.rcvif = (void *)ni;
575ff09e23fSAdrian Chadd 
576ff09e23fSAdrian Chadd 	/*
577ff09e23fSAdrian Chadd 	 * Attempt to add bpf transmit parameters.
578ff09e23fSAdrian Chadd 	 *
579ff09e23fSAdrian Chadd 	 * For now it's ok to fail; the raw_xmit api still takes
580ff09e23fSAdrian Chadd 	 * them as an option.
581ff09e23fSAdrian Chadd 	 *
582ff09e23fSAdrian Chadd 	 * Later on when ic_raw_xmit() has params removed,
583ff09e23fSAdrian Chadd 	 * they'll have to be added - so fail the transmit if
584ff09e23fSAdrian Chadd 	 * they can't be.
585ff09e23fSAdrian Chadd 	 */
5863a1da00bSAdrian Chadd 	if (params)
587ff09e23fSAdrian Chadd 		(void) ieee80211_add_xmit_params(m, params);
588ff09e23fSAdrian Chadd 
5897a79cebfSGleb Smirnoff 	error = ic->ic_raw_xmit(ni, m, params);
590d957a93aSAdrian Chadd 	if (error) {
5917a79cebfSGleb Smirnoff 		if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, 1);
592d957a93aSAdrian Chadd 		ieee80211_free_node(ni);
593d957a93aSAdrian Chadd 	}
5947a79cebfSGleb Smirnoff 	return (error);
595b032f27cSSam Leffler }
596b032f27cSSam Leffler 
597bcabc908SAndriy Voskoboinyk static int
598bcabc908SAndriy Voskoboinyk ieee80211_validate_frame(struct mbuf *m,
599bcabc908SAndriy Voskoboinyk     const struct ieee80211_bpf_params *params)
600bcabc908SAndriy Voskoboinyk {
601bcabc908SAndriy Voskoboinyk 	struct ieee80211_frame *wh;
602bcabc908SAndriy Voskoboinyk 	int type;
603bcabc908SAndriy Voskoboinyk 
604bcabc908SAndriy Voskoboinyk 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
605bcabc908SAndriy Voskoboinyk 		return (EINVAL);
606bcabc908SAndriy Voskoboinyk 
607bcabc908SAndriy Voskoboinyk 	wh = mtod(m, struct ieee80211_frame *);
608bcabc908SAndriy Voskoboinyk 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
609bcabc908SAndriy Voskoboinyk 	    IEEE80211_FC0_VERSION_0)
610bcabc908SAndriy Voskoboinyk 		return (EINVAL);
611bcabc908SAndriy Voskoboinyk 
612bcabc908SAndriy Voskoboinyk 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
613bcabc908SAndriy Voskoboinyk 	if (type != IEEE80211_FC0_TYPE_DATA) {
614bcabc908SAndriy Voskoboinyk 		if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
615bcabc908SAndriy Voskoboinyk 		    IEEE80211_FC1_DIR_NODS)
616bcabc908SAndriy Voskoboinyk 			return (EINVAL);
617bcabc908SAndriy Voskoboinyk 
618bcabc908SAndriy Voskoboinyk 		if (type != IEEE80211_FC0_TYPE_MGT &&
619bcabc908SAndriy Voskoboinyk 		    (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) != 0)
620bcabc908SAndriy Voskoboinyk 			return (EINVAL);
621bcabc908SAndriy Voskoboinyk 
622bcabc908SAndriy Voskoboinyk 		/* XXX skip other field checks? */
623bcabc908SAndriy Voskoboinyk 	}
624bcabc908SAndriy Voskoboinyk 
625bcabc908SAndriy Voskoboinyk 	if ((params && (params->ibp_flags & IEEE80211_BPF_CRYPTO) != 0) ||
6262889cbe2SAdrian Chadd 	    (IEEE80211_IS_PROTECTED(wh))) {
627bcabc908SAndriy Voskoboinyk 		int subtype;
628bcabc908SAndriy Voskoboinyk 
629bcabc908SAndriy Voskoboinyk 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
630bcabc908SAndriy Voskoboinyk 
631bcabc908SAndriy Voskoboinyk 		/*
632bcabc908SAndriy Voskoboinyk 		 * See IEEE Std 802.11-2012,
633bcabc908SAndriy Voskoboinyk 		 * 8.2.4.1.9 'Protected Frame field'
634bcabc908SAndriy Voskoboinyk 		 */
635bcabc908SAndriy Voskoboinyk 		/* XXX no support for robust management frames yet. */
636bcabc908SAndriy Voskoboinyk 		if (!(type == IEEE80211_FC0_TYPE_DATA ||
637bcabc908SAndriy Voskoboinyk 		    (type == IEEE80211_FC0_TYPE_MGT &&
638bcabc908SAndriy Voskoboinyk 		     subtype == IEEE80211_FC0_SUBTYPE_AUTH)))
639bcabc908SAndriy Voskoboinyk 			return (EINVAL);
640bcabc908SAndriy Voskoboinyk 
641bcabc908SAndriy Voskoboinyk 		wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
642bcabc908SAndriy Voskoboinyk 	}
643bcabc908SAndriy Voskoboinyk 
644bcabc908SAndriy Voskoboinyk 	if (m->m_pkthdr.len < ieee80211_anyhdrsize(wh))
645bcabc908SAndriy Voskoboinyk 		return (EINVAL);
646bcabc908SAndriy Voskoboinyk 
647bcabc908SAndriy Voskoboinyk 	return (0);
648bcabc908SAndriy Voskoboinyk }
649bcabc908SAndriy Voskoboinyk 
650e42e878bSAndriy Voskoboinyk static int
651e42e878bSAndriy Voskoboinyk ieee80211_validate_rate(struct ieee80211_node *ni, uint8_t rate)
652e42e878bSAndriy Voskoboinyk {
653e42e878bSAndriy Voskoboinyk 	struct ieee80211com *ic = ni->ni_ic;
654e42e878bSAndriy Voskoboinyk 
655e42e878bSAndriy Voskoboinyk 	if (IEEE80211_IS_HT_RATE(rate)) {
656e42e878bSAndriy Voskoboinyk 		if ((ic->ic_htcaps & IEEE80211_HTC_HT) == 0)
657e42e878bSAndriy Voskoboinyk 			return (EINVAL);
658e42e878bSAndriy Voskoboinyk 
659e42e878bSAndriy Voskoboinyk 		rate = IEEE80211_RV(rate);
660e42e878bSAndriy Voskoboinyk 		if (rate <= 31) {
661e42e878bSAndriy Voskoboinyk 			if (rate > ic->ic_txstream * 8 - 1)
662e42e878bSAndriy Voskoboinyk 				return (EINVAL);
663e42e878bSAndriy Voskoboinyk 
664e42e878bSAndriy Voskoboinyk 			return (0);
665e42e878bSAndriy Voskoboinyk 		}
666e42e878bSAndriy Voskoboinyk 
667e42e878bSAndriy Voskoboinyk 		if (rate == 32) {
668e42e878bSAndriy Voskoboinyk 			if ((ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
669e42e878bSAndriy Voskoboinyk 				return (EINVAL);
670e42e878bSAndriy Voskoboinyk 
671e42e878bSAndriy Voskoboinyk 			return (0);
672e42e878bSAndriy Voskoboinyk 		}
673e42e878bSAndriy Voskoboinyk 
674e42e878bSAndriy Voskoboinyk 		if ((ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) == 0)
675e42e878bSAndriy Voskoboinyk 			return (EINVAL);
676e42e878bSAndriy Voskoboinyk 
677e42e878bSAndriy Voskoboinyk 		switch (ic->ic_txstream) {
678e42e878bSAndriy Voskoboinyk 		case 0:
679e42e878bSAndriy Voskoboinyk 		case 1:
680e42e878bSAndriy Voskoboinyk 			return (EINVAL);
681e42e878bSAndriy Voskoboinyk 		case 2:
682e42e878bSAndriy Voskoboinyk 			if (rate > 38)
683e42e878bSAndriy Voskoboinyk 				return (EINVAL);
684e42e878bSAndriy Voskoboinyk 
685e42e878bSAndriy Voskoboinyk 			return (0);
686e42e878bSAndriy Voskoboinyk 		case 3:
687e42e878bSAndriy Voskoboinyk 			if (rate > 52)
688e42e878bSAndriy Voskoboinyk 				return (EINVAL);
689e42e878bSAndriy Voskoboinyk 
690e42e878bSAndriy Voskoboinyk 			return (0);
691e42e878bSAndriy Voskoboinyk 		case 4:
692e42e878bSAndriy Voskoboinyk 		default:
693e42e878bSAndriy Voskoboinyk 			if (rate > 76)
694e42e878bSAndriy Voskoboinyk 				return (EINVAL);
695e42e878bSAndriy Voskoboinyk 
696e42e878bSAndriy Voskoboinyk 			return (0);
697e42e878bSAndriy Voskoboinyk 		}
698e42e878bSAndriy Voskoboinyk 	}
699e42e878bSAndriy Voskoboinyk 
700e42e878bSAndriy Voskoboinyk 	if (!ieee80211_isratevalid(ic->ic_rt, rate))
701e42e878bSAndriy Voskoboinyk 		return (EINVAL);
702e42e878bSAndriy Voskoboinyk 
703e42e878bSAndriy Voskoboinyk 	return (0);
704e42e878bSAndriy Voskoboinyk }
705e42e878bSAndriy Voskoboinyk 
706e42e878bSAndriy Voskoboinyk static int
707e42e878bSAndriy Voskoboinyk ieee80211_sanitize_rates(struct ieee80211_node *ni, struct mbuf *m,
708e42e878bSAndriy Voskoboinyk     const struct ieee80211_bpf_params *params)
709e42e878bSAndriy Voskoboinyk {
710e42e878bSAndriy Voskoboinyk 	int error;
711e42e878bSAndriy Voskoboinyk 
712e42e878bSAndriy Voskoboinyk 	if (!params)
713e42e878bSAndriy Voskoboinyk 		return (0);	/* nothing to do */
714e42e878bSAndriy Voskoboinyk 
715e42e878bSAndriy Voskoboinyk 	/* NB: most drivers assume that ibp_rate0 is set (!= 0). */
716e42e878bSAndriy Voskoboinyk 	if (params->ibp_rate0 != 0) {
717e42e878bSAndriy Voskoboinyk 		error = ieee80211_validate_rate(ni, params->ibp_rate0);
718e42e878bSAndriy Voskoboinyk 		if (error != 0)
719e42e878bSAndriy Voskoboinyk 			return (error);
720e42e878bSAndriy Voskoboinyk 	} else {
721e42e878bSAndriy Voskoboinyk 		/* XXX pre-setup some default (e.g., mgmt / mcast) rate */
722e42e878bSAndriy Voskoboinyk 		/* XXX __DECONST? */
723e42e878bSAndriy Voskoboinyk 		(void) m;
724e42e878bSAndriy Voskoboinyk 	}
725e42e878bSAndriy Voskoboinyk 
726e42e878bSAndriy Voskoboinyk 	if (params->ibp_rate1 != 0 &&
727e42e878bSAndriy Voskoboinyk 	    (error = ieee80211_validate_rate(ni, params->ibp_rate1)) != 0)
728e42e878bSAndriy Voskoboinyk 		return (error);
729e42e878bSAndriy Voskoboinyk 
730e42e878bSAndriy Voskoboinyk 	if (params->ibp_rate2 != 0 &&
731e42e878bSAndriy Voskoboinyk 	    (error = ieee80211_validate_rate(ni, params->ibp_rate2)) != 0)
732e42e878bSAndriy Voskoboinyk 		return (error);
733e42e878bSAndriy Voskoboinyk 
734e42e878bSAndriy Voskoboinyk 	if (params->ibp_rate3 != 0 &&
735e42e878bSAndriy Voskoboinyk 	    (error = ieee80211_validate_rate(ni, params->ibp_rate3)) != 0)
736e42e878bSAndriy Voskoboinyk 		return (error);
737e42e878bSAndriy Voskoboinyk 
738e42e878bSAndriy Voskoboinyk 	return (0);
739e42e878bSAndriy Voskoboinyk }
740e42e878bSAndriy Voskoboinyk 
741b032f27cSSam Leffler /*
742b032f27cSSam Leffler  * 802.11 output routine. This is (currently) used only to
743b032f27cSSam Leffler  * connect bpf write calls to the 802.11 layer for injecting
744fad788b1SSam Leffler  * raw 802.11 frames.
745b032f27cSSam Leffler  */
746b032f27cSSam Leffler int
747b032f27cSSam Leffler ieee80211_output(struct ifnet *ifp, struct mbuf *m,
74847e8d432SGleb Smirnoff 	const struct sockaddr *dst, struct route *ro)
749b032f27cSSam Leffler {
750b032f27cSSam Leffler #define senderr(e) do { error = (e); goto bad;} while (0)
751bcabc908SAndriy Voskoboinyk 	const struct ieee80211_bpf_params *params = NULL;
752b032f27cSSam Leffler 	struct ieee80211_node *ni = NULL;
7538ed1835dSSam Leffler 	struct ieee80211vap *vap;
754b032f27cSSam Leffler 	struct ieee80211_frame *wh;
7555cda6006SAdrian Chadd 	struct ieee80211com *ic = NULL;
756b032f27cSSam Leffler 	int error;
7575cda6006SAdrian Chadd 	int ret;
758b032f27cSSam Leffler 
7598ed1835dSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
7608ed1835dSSam Leffler 		/*
7618ed1835dSSam Leffler 		 * Short-circuit requests if the vap is marked OACTIVE
7628ed1835dSSam Leffler 		 * as this can happen because a packet came down through
7638ed1835dSSam Leffler 		 * ieee80211_start before the vap entered RUN state in
7648ed1835dSSam Leffler 		 * which case it's ok to just drop the frame.  This
7658ed1835dSSam Leffler 		 * should not be necessary but callers of if_output don't
7668ed1835dSSam Leffler 		 * check OACTIVE.
7678ed1835dSSam Leffler 		 */
7688ed1835dSSam Leffler 		senderr(ENETDOWN);
7698ed1835dSSam Leffler 	}
7708ed1835dSSam Leffler 	vap = ifp->if_softc;
7715cda6006SAdrian Chadd 	ic = vap->iv_ic;
772b032f27cSSam Leffler 	/*
773b032f27cSSam Leffler 	 * Hand to the 802.3 code if not tagged as
774b032f27cSSam Leffler 	 * a raw 802.11 frame.
775b032f27cSSam Leffler 	 */
776b032f27cSSam Leffler 	if (dst->sa_family != AF_IEEE80211)
777279aa3d4SKip Macy 		return vap->iv_output(ifp, m, dst, ro);
778b032f27cSSam Leffler #ifdef MAC
779c27b9cdbSRobert Watson 	error = mac_ifnet_check_transmit(ifp, m);
780b032f27cSSam Leffler 	if (error)
781b032f27cSSam Leffler 		senderr(error);
782b032f27cSSam Leffler #endif
783b032f27cSSam Leffler 	if (ifp->if_flags & IFF_MONITOR)
784b032f27cSSam Leffler 		senderr(ENETDOWN);
785b032f27cSSam Leffler 	if (!IFNET_IS_UP_RUNNING(ifp))
786b032f27cSSam Leffler 		senderr(ENETDOWN);
787b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
788b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap,
789b032f27cSSam Leffler 		    IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
790b032f27cSSam Leffler 		    "block %s frame in CAC state\n", "raw data");
791b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
792b032f27cSSam Leffler 		senderr(EIO);		/* XXX */
7930d9aed8aSBernhard Schmidt 	} else if (vap->iv_state == IEEE80211_S_SCAN)
7940d9aed8aSBernhard Schmidt 		senderr(EIO);
795b032f27cSSam Leffler 	/* XXX bypass bridge, pfil, carp, etc. */
796b032f27cSSam Leffler 
797bcabc908SAndriy Voskoboinyk 	/*
798bcabc908SAndriy Voskoboinyk 	 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
799bcabc908SAndriy Voskoboinyk 	 * present by setting the sa_len field of the sockaddr (yes,
800bcabc908SAndriy Voskoboinyk 	 * this is a hack).
801bcabc908SAndriy Voskoboinyk 	 * NB: we assume sa_data is suitably aligned to cast.
802bcabc908SAndriy Voskoboinyk 	 */
803bcabc908SAndriy Voskoboinyk 	if (dst->sa_len != 0)
804bcabc908SAndriy Voskoboinyk 		params = (const struct ieee80211_bpf_params *)dst->sa_data;
805bcabc908SAndriy Voskoboinyk 
806bcabc908SAndriy Voskoboinyk 	error = ieee80211_validate_frame(m, params);
807bcabc908SAndriy Voskoboinyk 	if (error != 0)
808bcabc908SAndriy Voskoboinyk 		senderr(error);
809bcabc908SAndriy Voskoboinyk 
810b032f27cSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
811b032f27cSSam Leffler 
812b032f27cSSam Leffler 	/* locate destination node */
813b032f27cSSam Leffler 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
814b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_NODS:
815b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_FROMDS:
816b032f27cSSam Leffler 		ni = ieee80211_find_txnode(vap, wh->i_addr1);
817b032f27cSSam Leffler 		break;
818b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_TODS:
819b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_DSTODS:
820b032f27cSSam Leffler 		ni = ieee80211_find_txnode(vap, wh->i_addr3);
821b032f27cSSam Leffler 		break;
822b032f27cSSam Leffler 	default:
823bcabc908SAndriy Voskoboinyk 		senderr(EDOOFUS);
824b032f27cSSam Leffler 	}
825b032f27cSSam Leffler 	if (ni == NULL) {
826b032f27cSSam Leffler 		/*
827b032f27cSSam Leffler 		 * Permit packets w/ bpf params through regardless
828b032f27cSSam Leffler 		 * (see below about sa_len).
829b032f27cSSam Leffler 		 */
830b032f27cSSam Leffler 		if (dst->sa_len == 0)
831b032f27cSSam Leffler 			senderr(EHOSTUNREACH);
832b032f27cSSam Leffler 		ni = ieee80211_ref_node(vap->iv_bss);
833b032f27cSSam Leffler 	}
834b032f27cSSam Leffler 
835b032f27cSSam Leffler 	/*
836b032f27cSSam Leffler 	 * Sanitize mbuf for net80211 flags leaked from above.
837b032f27cSSam Leffler 	 *
838b032f27cSSam Leffler 	 * NB: This must be done before ieee80211_classify as
839b032f27cSSam Leffler 	 *     it marks EAPOL in frames with M_EAPOL.
840b032f27cSSam Leffler 	 */
841b032f27cSSam Leffler 	m->m_flags &= ~M_80211_TX;
842bcabc908SAndriy Voskoboinyk 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
843b032f27cSSam Leffler 
844bcabc908SAndriy Voskoboinyk 	if (IEEE80211_IS_DATA(wh)) {
845b032f27cSSam Leffler 		/* calculate priority so drivers can find the tx queue */
846b032f27cSSam Leffler 		if (ieee80211_classify(ni, m))
847b032f27cSSam Leffler 			senderr(EIO);		/* XXX */
848b032f27cSSam Leffler 
849bcabc908SAndriy Voskoboinyk 		/* NB: ieee80211_encap does not include 802.11 header */
850bcabc908SAndriy Voskoboinyk 		IEEE80211_NODE_STAT_ADD(ni, tx_bytes,
851bcabc908SAndriy Voskoboinyk 		    m->m_pkthdr.len - ieee80211_hdrsize(wh));
852bcabc908SAndriy Voskoboinyk 	} else
853bcabc908SAndriy Voskoboinyk 		M_WME_SETAC(m, WME_AC_BE);
854bcabc908SAndriy Voskoboinyk 
855e42e878bSAndriy Voskoboinyk 	error = ieee80211_sanitize_rates(ni, m, params);
856e42e878bSAndriy Voskoboinyk 	if (error != 0)
857e42e878bSAndriy Voskoboinyk 		senderr(error);
858e42e878bSAndriy Voskoboinyk 
8590e910c94SSam Leffler 	IEEE80211_NODE_STAT(ni, tx_data);
8600e910c94SSam Leffler 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
8610e910c94SSam Leffler 		IEEE80211_NODE_STAT(ni, tx_mcast);
8620e910c94SSam Leffler 		m->m_flags |= M_MCAST;
8630e910c94SSam Leffler 	} else
8640e910c94SSam Leffler 		IEEE80211_NODE_STAT(ni, tx_ucast);
865b032f27cSSam Leffler 
8665cda6006SAdrian Chadd 	IEEE80211_TX_LOCK(ic);
867bcabc908SAndriy Voskoboinyk 	ret = ieee80211_raw_output(vap, ni, m, params);
8685cda6006SAdrian Chadd 	IEEE80211_TX_UNLOCK(ic);
8695cda6006SAdrian Chadd 	return (ret);
870b032f27cSSam Leffler bad:
871b032f27cSSam Leffler 	if (m != NULL)
872b032f27cSSam Leffler 		m_freem(m);
873b032f27cSSam Leffler 	if (ni != NULL)
874b032f27cSSam Leffler 		ieee80211_free_node(ni);
875dea45121SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
876b032f27cSSam Leffler 	return error;
877b032f27cSSam Leffler #undef senderr
878b032f27cSSam Leffler }
879b032f27cSSam Leffler 
880b032f27cSSam Leffler /*
881add59d08SSam Leffler  * Set the direction field and address fields of an outgoing
8820e66722dSSam Leffler  * frame.  Note this should be called early on in constructing
8830e66722dSSam Leffler  * a frame as it sets i_fc[1]; other bits can then be or'd in.
884add59d08SSam Leffler  */
88559aa14a9SRui Paulo void
886b032f27cSSam Leffler ieee80211_send_setup(
887add59d08SSam Leffler 	struct ieee80211_node *ni,
8889e80b1dfSSam Leffler 	struct mbuf *m,
8898ac160cdSSam Leffler 	int type, int tid,
89068e8e04eSSam Leffler 	const uint8_t sa[IEEE80211_ADDR_LEN],
89168e8e04eSSam Leffler 	const uint8_t da[IEEE80211_ADDR_LEN],
89268e8e04eSSam Leffler 	const uint8_t bssid[IEEE80211_ADDR_LEN])
893add59d08SSam Leffler {
894add59d08SSam Leffler #define	WH4(wh)	((struct ieee80211_frame_addr4 *)wh)
89559aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
89650cfec0eSBernhard Schmidt 	struct ieee80211_tx_ampdu *tap;
8979e80b1dfSSam Leffler 	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
8989e80b1dfSSam Leffler 	ieee80211_seq seqno;
899add59d08SSam Leffler 
9006ce4aeb8SAdrian Chadd 	IEEE80211_TX_LOCK_ASSERT(ni->ni_ic);
9015cda6006SAdrian Chadd 
902add59d08SSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
903add59d08SSam Leffler 	if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
904b032f27cSSam Leffler 		switch (vap->iv_opmode) {
905add59d08SSam Leffler 		case IEEE80211_M_STA:
906add59d08SSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
907add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
908add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
909add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, da);
910add59d08SSam Leffler 			break;
911add59d08SSam Leffler 		case IEEE80211_M_IBSS:
912add59d08SSam Leffler 		case IEEE80211_M_AHDEMO:
913add59d08SSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
914add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
915add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
916add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
917add59d08SSam Leffler 			break;
918add59d08SSam Leffler 		case IEEE80211_M_HOSTAP:
919add59d08SSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
920add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
921add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
922add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, sa);
923add59d08SSam Leffler 			break;
92468e8e04eSSam Leffler 		case IEEE80211_M_WDS:
92568e8e04eSSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
926b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
927b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
92868e8e04eSSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, da);
92968e8e04eSSam Leffler 			IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
93068e8e04eSSam Leffler 			break;
93159aa14a9SRui Paulo 		case IEEE80211_M_MBSS:
93259aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
93359aa14a9SRui Paulo 			if (IEEE80211_IS_MULTICAST(da)) {
93459aa14a9SRui Paulo 				wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
93559aa14a9SRui Paulo 				/* XXX next hop */
93659aa14a9SRui Paulo 				IEEE80211_ADDR_COPY(wh->i_addr1, da);
93759aa14a9SRui Paulo 				IEEE80211_ADDR_COPY(wh->i_addr2,
93859aa14a9SRui Paulo 				    vap->iv_myaddr);
93959aa14a9SRui Paulo 			} else {
94059aa14a9SRui Paulo 				wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
94159aa14a9SRui Paulo 				IEEE80211_ADDR_COPY(wh->i_addr1, da);
94259aa14a9SRui Paulo 				IEEE80211_ADDR_COPY(wh->i_addr2,
94359aa14a9SRui Paulo 				    vap->iv_myaddr);
94459aa14a9SRui Paulo 				IEEE80211_ADDR_COPY(wh->i_addr3, da);
94559aa14a9SRui Paulo 				IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
94659aa14a9SRui Paulo 			}
94759aa14a9SRui Paulo #endif
94859aa14a9SRui Paulo 			break;
949add59d08SSam Leffler 		case IEEE80211_M_MONITOR:	/* NB: to quiet compiler */
950add59d08SSam Leffler 			break;
951add59d08SSam Leffler 		}
952add59d08SSam Leffler 	} else {
953add59d08SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
954add59d08SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, da);
955add59d08SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, sa);
95659aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
95759aa14a9SRui Paulo 		if (vap->iv_opmode == IEEE80211_M_MBSS)
95859aa14a9SRui Paulo 			IEEE80211_ADDR_COPY(wh->i_addr3, sa);
95959aa14a9SRui Paulo 		else
96059aa14a9SRui Paulo #endif
961add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
962add59d08SSam Leffler 	}
96368e8e04eSSam Leffler 	*(uint16_t *)&wh->i_dur[0] = 0;
9649e80b1dfSSam Leffler 
96551172f62SAdrian Chadd 	/*
96651172f62SAdrian Chadd 	 * XXX TODO: this is what the TX lock is for.
96751172f62SAdrian Chadd 	 * Here we're incrementing sequence numbers, and they
96851172f62SAdrian Chadd 	 * need to be in lock-step with what the driver is doing
96951172f62SAdrian Chadd 	 * both in TX ordering and crypto encap (IV increment.)
97051172f62SAdrian Chadd 	 *
97151172f62SAdrian Chadd 	 * If the driver does seqno itself, then we can skip
97251172f62SAdrian Chadd 	 * assigning sequence numbers here, and we can avoid
97351172f62SAdrian Chadd 	 * requiring the TX lock.
97451172f62SAdrian Chadd 	 */
9752aa563dfSAdrian Chadd 	tap = &ni->ni_tx_ampdu[tid];
9769764ef21SAdrian Chadd 	if (tid != IEEE80211_NONQOS_TID && IEEE80211_AMPDU_RUNNING(tap)) {
97750cfec0eSBernhard Schmidt 		m->m_flags |= M_AMPDU_MPDU;
9782db223f9SAndriy Voskoboinyk 
9792db223f9SAndriy Voskoboinyk 		/* NB: zero out i_seq field (for s/w encryption etc) */
9802db223f9SAndriy Voskoboinyk 		*(uint16_t *)&wh->i_seq[0] = 0;
9819764ef21SAdrian Chadd 	} else {
982c3ebe019SAdrian Chadd 		if (IEEE80211_HAS_SEQ(type & IEEE80211_FC0_TYPE_MASK,
983c3ebe019SAdrian Chadd 				      type & IEEE80211_FC0_SUBTYPE_MASK))
9849764ef21SAdrian Chadd 			/*
9859764ef21SAdrian Chadd 			 * 802.11-2012 9.3.2.10 - QoS multicast frames
9869764ef21SAdrian Chadd 			 * come out of a different seqno space.
9879764ef21SAdrian Chadd 			 */
9889764ef21SAdrian Chadd 			if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
9899764ef21SAdrian Chadd 				seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
9909764ef21SAdrian Chadd 			} else {
9919e80b1dfSSam Leffler 				seqno = ni->ni_txseqs[tid]++;
9929764ef21SAdrian Chadd 			}
993c3ebe019SAdrian Chadd 		else
994c3ebe019SAdrian Chadd 			seqno = 0;
995c3ebe019SAdrian Chadd 
99650cfec0eSBernhard Schmidt 		*(uint16_t *)&wh->i_seq[0] =
99750cfec0eSBernhard Schmidt 		    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
9989a841c7fSSam Leffler 		M_SEQNO_SET(m, seqno);
99950cfec0eSBernhard Schmidt 	}
10009e80b1dfSSam Leffler 
10019e80b1dfSSam Leffler 	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
10029e80b1dfSSam Leffler 		m->m_flags |= M_MCAST;
1003add59d08SSam Leffler #undef WH4
1004add59d08SSam Leffler }
1005add59d08SSam Leffler 
1006add59d08SSam Leffler /*
10070a915fadSSam Leffler  * Send a management frame to the specified node.  The node pointer
10080a915fadSSam Leffler  * must have a reference as the pointer will be passed to the driver
10090a915fadSSam Leffler  * and potentially held for a long time.  If the frame is successfully
10100a915fadSSam Leffler  * dispatched to the driver, then it is responsible for freeing the
1011b032f27cSSam Leffler  * reference (and potentially free'ing up any associated storage);
1012b032f27cSSam Leffler  * otherwise deal with reclaiming any reference (on error).
10130a915fadSSam Leffler  */
101468e8e04eSSam Leffler int
10158ac160cdSSam Leffler ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
10168ac160cdSSam Leffler 	struct ieee80211_bpf_params *params)
10171a1e1d21SSam Leffler {
1018b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1019b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
10201a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
10215cda6006SAdrian Chadd 	int ret;
10221a1e1d21SSam Leffler 
10230a915fadSSam Leffler 	KASSERT(ni != NULL, ("null node"));
10241a1e1d21SSam Leffler 
1025b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
1026b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
1027b032f27cSSam Leffler 		    ni, "block %s frame in CAC state",
10284357a5d1SAndriy Voskoboinyk 			ieee80211_mgt_subtype_name(type));
1029b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
1030b032f27cSSam Leffler 		ieee80211_free_node(ni);
1031b032f27cSSam Leffler 		m_freem(m);
1032b032f27cSSam Leffler 		return EIO;		/* XXX */
1033b032f27cSSam Leffler 	}
1034b032f27cSSam Leffler 
1035bd29f817SBjoern A. Zeeb 	M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT);
1036b032f27cSSam Leffler 	if (m == NULL) {
1037b032f27cSSam Leffler 		ieee80211_free_node(ni);
10381a1e1d21SSam Leffler 		return ENOMEM;
1039b032f27cSSam Leffler 	}
10400a915fadSSam Leffler 
10415cda6006SAdrian Chadd 	IEEE80211_TX_LOCK(ic);
10425cda6006SAdrian Chadd 
10431a1e1d21SSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
10449e80b1dfSSam Leffler 	ieee80211_send_setup(ni, m,
10458ac160cdSSam Leffler 	     IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
1046b032f27cSSam Leffler 	     vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
10478ac160cdSSam Leffler 	if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
1048b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
1049b032f27cSSam Leffler 		    "encrypting frame (%s)", __func__);
10505945b5f5SKevin Lo 		wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
10518a1b9b6aSSam Leffler 	}
1052c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
10538ac160cdSSam Leffler 
10548ac160cdSSam Leffler 	KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
10558ac160cdSSam Leffler 	M_WME_SETAC(m, params->ibp_pri);
10568ac160cdSSam Leffler 
10578a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
10588a1b9b6aSSam Leffler 	/* avoid printing too many frames */
1059b032f27cSSam Leffler 	if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
1060b032f27cSSam Leffler 	    ieee80211_msg_dumppkts(vap)) {
10612e59c9c7SBjoern A. Zeeb 		ieee80211_note(vap, "[%s] send %s on channel %u\n",
10628a1b9b6aSSam Leffler 		    ether_sprintf(wh->i_addr1),
10634357a5d1SAndriy Voskoboinyk 		    ieee80211_mgt_subtype_name(type),
1064b5c99415SSam Leffler 		    ieee80211_chan2ieee(ic, ic->ic_curchan));
10658a1b9b6aSSam Leffler 	}
10668a1b9b6aSSam Leffler #endif
10678a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_mgmt);
106868e8e04eSSam Leffler 
10695cda6006SAdrian Chadd 	ret = ieee80211_raw_output(vap, ni, m, params);
10705cda6006SAdrian Chadd 	IEEE80211_TX_UNLOCK(ic);
10715cda6006SAdrian Chadd 	return (ret);
1072246b5467SSam Leffler }
1073246b5467SSam Leffler 
10749f82bedaSAndriy Voskoboinyk static void
10759f82bedaSAndriy Voskoboinyk ieee80211_nulldata_transmitted(struct ieee80211_node *ni, void *arg,
10769f82bedaSAndriy Voskoboinyk     int status)
10779f82bedaSAndriy Voskoboinyk {
10789f82bedaSAndriy Voskoboinyk 	struct ieee80211vap *vap = ni->ni_vap;
10799f82bedaSAndriy Voskoboinyk 
10809f82bedaSAndriy Voskoboinyk 	wakeup(vap);
10819f82bedaSAndriy Voskoboinyk }
10829f82bedaSAndriy Voskoboinyk 
1083246b5467SSam Leffler /*
10846683931eSSam Leffler  * Send a null data frame to the specified node.  If the station
10856683931eSSam Leffler  * is setup for QoS then a QoS Null Data frame is constructed.
10866683931eSSam Leffler  * If this is a WDS station then a 4-address frame is constructed.
108719ad2dd7SSam Leffler  *
108819ad2dd7SSam Leffler  * NB: the caller is assumed to have setup a node reference
108919ad2dd7SSam Leffler  *     for use; this is necessary to deal with a race condition
1090b032f27cSSam Leffler  *     when probing for inactive stations.  Like ieee80211_mgmt_output
1091b032f27cSSam Leffler  *     we must cleanup any node reference on error;  however we
1092b032f27cSSam Leffler  *     can safely just unref it as we know it will never be the
1093b032f27cSSam Leffler  *     last reference to the node.
10948a1b9b6aSSam Leffler  */
10958a1b9b6aSSam Leffler int
1096f62121ceSSam Leffler ieee80211_send_nulldata(struct ieee80211_node *ni)
10978a1b9b6aSSam Leffler {
1098b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1099f62121ceSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
11008a1b9b6aSSam Leffler 	struct mbuf *m;
11018a1b9b6aSSam Leffler 	struct ieee80211_frame *wh;
11026683931eSSam Leffler 	int hdrlen;
11036683931eSSam Leffler 	uint8_t *frm;
11045cda6006SAdrian Chadd 	int ret;
11058a1b9b6aSSam Leffler 
1106b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
1107b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
1108b032f27cSSam Leffler 		    ni, "block %s frame in CAC state", "null data");
1109b032f27cSSam Leffler 		ieee80211_unref_node(&ni);
1110b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
1111b032f27cSSam Leffler 		return EIO;		/* XXX */
1112b032f27cSSam Leffler 	}
1113b032f27cSSam Leffler 
11146683931eSSam Leffler 	if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
11156683931eSSam Leffler 		hdrlen = sizeof(struct ieee80211_qosframe);
11166683931eSSam Leffler 	else
11176683931eSSam Leffler 		hdrlen = sizeof(struct ieee80211_frame);
11186683931eSSam Leffler 	/* NB: only WDS vap's get 4-address frames */
11196683931eSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_WDS)
11206683931eSSam Leffler 		hdrlen += IEEE80211_ADDR_LEN;
11216683931eSSam Leffler 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
11226683931eSSam Leffler 		hdrlen = roundup(hdrlen, sizeof(uint32_t));
11236683931eSSam Leffler 
11246683931eSSam Leffler 	m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
11258a1b9b6aSSam Leffler 	if (m == NULL) {
11268a1b9b6aSSam Leffler 		/* XXX debug msg */
112719ad2dd7SSam Leffler 		ieee80211_unref_node(&ni);
1128b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
11298a1b9b6aSSam Leffler 		return ENOMEM;
11308a1b9b6aSSam Leffler 	}
11316683931eSSam Leffler 	KASSERT(M_LEADINGSPACE(m) >= hdrlen,
11326683931eSSam Leffler 	    ("leading space %zd", M_LEADINGSPACE(m)));
1133bd29f817SBjoern A. Zeeb 	M_PREPEND(m, hdrlen, IEEE80211_M_NOWAIT);
11346683931eSSam Leffler 	if (m == NULL) {
11356683931eSSam Leffler 		/* NB: cannot happen */
11366683931eSSam Leffler 		ieee80211_free_node(ni);
11376683931eSSam Leffler 		return ENOMEM;
11386683931eSSam Leffler 	}
11398a1b9b6aSSam Leffler 
11405cda6006SAdrian Chadd 	IEEE80211_TX_LOCK(ic);
11415cda6006SAdrian Chadd 
11426683931eSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);		/* NB: a little lie */
11436683931eSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
11446683931eSSam Leffler 		const int tid = WME_AC_TO_TID(WME_AC_BE);
11456683931eSSam Leffler 		uint8_t *qos;
11466683931eSSam Leffler 
11479e80b1dfSSam Leffler 		ieee80211_send_setup(ni, m,
11486683931eSSam Leffler 		    IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
11496683931eSSam Leffler 		    tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
11506683931eSSam Leffler 
11516683931eSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_WDS)
11526683931eSSam Leffler 			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
11536683931eSSam Leffler 		else
11546683931eSSam Leffler 			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
11556683931eSSam Leffler 		qos[0] = tid & IEEE80211_QOS_TID;
11566683931eSSam Leffler 		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
11576683931eSSam Leffler 			qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
11586683931eSSam Leffler 		qos[1] = 0;
11596683931eSSam Leffler 	} else {
11609e80b1dfSSam Leffler 		ieee80211_send_setup(ni, m,
1161add59d08SSam Leffler 		    IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
11628ac160cdSSam Leffler 		    IEEE80211_NONQOS_TID,
1163b032f27cSSam Leffler 		    vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
11646683931eSSam Leffler 	}
1165b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_WDS) {
1166add59d08SSam Leffler 		/* NB: power management bit is never sent by an AP */
1167add59d08SSam Leffler 		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
1168b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_HOSTAP)
1169add59d08SSam Leffler 			wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
1170b032f27cSSam Leffler 	}
11719f82bedaSAndriy Voskoboinyk 	if ((ic->ic_flags & IEEE80211_F_SCAN) &&
11729f82bedaSAndriy Voskoboinyk 	    (ni->ni_flags & IEEE80211_NODE_PWR_MGT)) {
11739f82bedaSAndriy Voskoboinyk 		ieee80211_add_callback(m, ieee80211_nulldata_transmitted,
11749f82bedaSAndriy Voskoboinyk 		    NULL);
11759f82bedaSAndriy Voskoboinyk 	}
11766683931eSSam Leffler 	m->m_len = m->m_pkthdr.len = hdrlen;
1177c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
11786683931eSSam Leffler 
1179bb239ce9SSam Leffler 	M_WME_SETAC(m, WME_AC_BE);
11808a1b9b6aSSam Leffler 
11818a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_data);
11828a1b9b6aSSam Leffler 
1183b032f27cSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
11846683931eSSam Leffler 	    "send %snull data frame on channel %u, pwr mgt %s",
11856683931eSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
1186b5c99415SSam Leffler 	    ieee80211_chan2ieee(ic, ic->ic_curchan),
1187add59d08SSam Leffler 	    wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
1188add59d08SSam Leffler 
11895cda6006SAdrian Chadd 	ret = ieee80211_raw_output(vap, ni, m, NULL);
11905cda6006SAdrian Chadd 	IEEE80211_TX_UNLOCK(ic);
11915cda6006SAdrian Chadd 	return (ret);
11928a1b9b6aSSam Leffler }
11938a1b9b6aSSam Leffler 
11948a1b9b6aSSam Leffler /*
11958a1b9b6aSSam Leffler  * Assign priority to a frame based on any vlan tag assigned
11968a1b9b6aSSam Leffler  * to the station and/or any Diffserv setting in an IP header.
11978a1b9b6aSSam Leffler  * Finally, if an ACM policy is setup (in station mode) it's
11988a1b9b6aSSam Leffler  * applied.
11998a1b9b6aSSam Leffler  */
12008a1b9b6aSSam Leffler int
1201b032f27cSSam Leffler ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
12028a1b9b6aSSam Leffler {
1203bcabc908SAndriy Voskoboinyk 	const struct ether_header *eh = NULL;
1204bcabc908SAndriy Voskoboinyk 	uint16_t ether_type;
12058a1b9b6aSSam Leffler 	int v_wme_ac, d_wme_ac, ac;
12068a1b9b6aSSam Leffler 
1207bcabc908SAndriy Voskoboinyk 	if (__predict_false(m->m_flags & M_ENCAP)) {
1208bcabc908SAndriy Voskoboinyk 		struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
1209bcabc908SAndriy Voskoboinyk 		struct llc *llc;
1210bcabc908SAndriy Voskoboinyk 		int hdrlen, subtype;
1211bcabc908SAndriy Voskoboinyk 
1212bcabc908SAndriy Voskoboinyk 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1213bcabc908SAndriy Voskoboinyk 		if (subtype & IEEE80211_FC0_SUBTYPE_NODATA) {
1214bcabc908SAndriy Voskoboinyk 			ac = WME_AC_BE;
1215bcabc908SAndriy Voskoboinyk 			goto done;
1216bcabc908SAndriy Voskoboinyk 		}
1217bcabc908SAndriy Voskoboinyk 
1218bcabc908SAndriy Voskoboinyk 		hdrlen = ieee80211_hdrsize(wh);
1219bcabc908SAndriy Voskoboinyk 		if (m->m_pkthdr.len < hdrlen + sizeof(*llc))
1220bcabc908SAndriy Voskoboinyk 			return 1;
1221bcabc908SAndriy Voskoboinyk 
1222bcabc908SAndriy Voskoboinyk 		llc = (struct llc *)mtodo(m, hdrlen);
1223bcabc908SAndriy Voskoboinyk 		if (llc->llc_dsap != LLC_SNAP_LSAP ||
1224bcabc908SAndriy Voskoboinyk 		    llc->llc_ssap != LLC_SNAP_LSAP ||
1225bcabc908SAndriy Voskoboinyk 		    llc->llc_control != LLC_UI ||
1226bcabc908SAndriy Voskoboinyk 		    llc->llc_snap.org_code[0] != 0 ||
1227bcabc908SAndriy Voskoboinyk 		    llc->llc_snap.org_code[1] != 0 ||
1228bcabc908SAndriy Voskoboinyk 		    llc->llc_snap.org_code[2] != 0)
1229bcabc908SAndriy Voskoboinyk 			return 1;
1230bcabc908SAndriy Voskoboinyk 
1231bcabc908SAndriy Voskoboinyk 		ether_type = llc->llc_snap.ether_type;
1232bcabc908SAndriy Voskoboinyk 	} else {
1233bcabc908SAndriy Voskoboinyk 		eh = mtod(m, struct ether_header *);
1234bcabc908SAndriy Voskoboinyk 		ether_type = eh->ether_type;
1235bcabc908SAndriy Voskoboinyk 	}
1236bcabc908SAndriy Voskoboinyk 
1237b032f27cSSam Leffler 	/*
1238b032f27cSSam Leffler 	 * Always promote PAE/EAPOL frames to high priority.
1239b032f27cSSam Leffler 	 */
1240bcabc908SAndriy Voskoboinyk 	if (ether_type == htons(ETHERTYPE_PAE)) {
1241b032f27cSSam Leffler 		/* NB: mark so others don't need to check header */
1242b032f27cSSam Leffler 		m->m_flags |= M_EAPOL;
1243b032f27cSSam Leffler 		ac = WME_AC_VO;
1244b032f27cSSam Leffler 		goto done;
1245b032f27cSSam Leffler 	}
1246b032f27cSSam Leffler 	/*
1247b032f27cSSam Leffler 	 * Non-qos traffic goes to BE.
1248b032f27cSSam Leffler 	 */
12498a1b9b6aSSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
12508a1b9b6aSSam Leffler 		ac = WME_AC_BE;
12518a1b9b6aSSam Leffler 		goto done;
12528a1b9b6aSSam Leffler 	}
12538a1b9b6aSSam Leffler 
12548a1b9b6aSSam Leffler 	/*
12558a1b9b6aSSam Leffler 	 * If node has a vlan tag then all traffic
12568a1b9b6aSSam Leffler 	 * to it must have a matching tag.
12578a1b9b6aSSam Leffler 	 */
12588a1b9b6aSSam Leffler 	v_wme_ac = 0;
12598a1b9b6aSSam Leffler 	if (ni->ni_vlan != 0) {
126078ba57b9SAndre Oppermann 		 if ((m->m_flags & M_VLANTAG) == 0) {
12618a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_novlantag);
12628a1b9b6aSSam Leffler 			return 1;
12638a1b9b6aSSam Leffler 		}
126478ba57b9SAndre Oppermann 		if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
12658a1b9b6aSSam Leffler 		    EVL_VLANOFTAG(ni->ni_vlan)) {
12668a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
12678a1b9b6aSSam Leffler 			return 1;
12688a1b9b6aSSam Leffler 		}
12698a1b9b6aSSam Leffler 		/* map vlan priority to AC */
1270f4558c9aSSam Leffler 		v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
12718a1b9b6aSSam Leffler 	}
12728a1b9b6aSSam Leffler 
12738def06d5SBjoern A. Zeeb 	if (eh == NULL)
12748def06d5SBjoern A. Zeeb 		goto no_eh;
12758def06d5SBjoern A. Zeeb 
1276e755a73dSSam Leffler 	/* XXX m_copydata may be too slow for fast path */
12778def06d5SBjoern A. Zeeb 	switch (ntohs(eh->ether_type)) {
12788a1b9b6aSSam Leffler #ifdef INET
12798def06d5SBjoern A. Zeeb 	case ETHERTYPE_IP:
12808def06d5SBjoern A. Zeeb 	{
1281f4558c9aSSam Leffler 		uint8_t tos;
12828a1b9b6aSSam Leffler 		/*
1283f4558c9aSSam Leffler 		 * IP frame, map the DSCP bits from the TOS field.
12848a1b9b6aSSam Leffler 		 */
1285f4558c9aSSam Leffler 		/* NB: ip header may not be in first mbuf */
1286f4558c9aSSam Leffler 		m_copydata(m, sizeof(struct ether_header) +
1287f4558c9aSSam Leffler 		    offsetof(struct ip, ip_tos), sizeof(tos), &tos);
1288f4558c9aSSam Leffler 		tos >>= 5;		/* NB: ECN + low 3 bits of DSCP */
1289f4558c9aSSam Leffler 		d_wme_ac = TID_TO_WME_AC(tos);
12908def06d5SBjoern A. Zeeb 		break;
12918def06d5SBjoern A. Zeeb 	}
12928def06d5SBjoern A. Zeeb #endif
1293e755a73dSSam Leffler #ifdef INET6
12948def06d5SBjoern A. Zeeb 	case ETHERTYPE_IPV6:
12958def06d5SBjoern A. Zeeb 	{
1296e755a73dSSam Leffler 		uint32_t flow;
1297e755a73dSSam Leffler 		uint8_t tos;
1298e755a73dSSam Leffler 		/*
12996ffd0ca9SBjoern A. Zeeb 		 * IPv6 frame, map the DSCP bits from the traffic class field.
1300e755a73dSSam Leffler 		 */
1301e755a73dSSam Leffler 		m_copydata(m, sizeof(struct ether_header) +
1302e755a73dSSam Leffler 		    offsetof(struct ip6_hdr, ip6_flow), sizeof(flow),
1303e755a73dSSam Leffler 		    (caddr_t) &flow);
1304e755a73dSSam Leffler 		tos = (uint8_t)(ntohl(flow) >> 20);
1305e755a73dSSam Leffler 		tos >>= 5;		/* NB: ECN + low 3 bits of DSCP */
1306e755a73dSSam Leffler 		d_wme_ac = TID_TO_WME_AC(tos);
13078def06d5SBjoern A. Zeeb 		break;
13088def06d5SBjoern A. Zeeb 	}
13098def06d5SBjoern A. Zeeb #endif
13108def06d5SBjoern A. Zeeb 	default:
13118def06d5SBjoern A. Zeeb no_eh:
13128a1b9b6aSSam Leffler 		d_wme_ac = WME_AC_BE;
13138def06d5SBjoern A. Zeeb 		break;
1314e755a73dSSam Leffler 	}
13158def06d5SBjoern A. Zeeb 
13168a1b9b6aSSam Leffler 	/*
13178a1b9b6aSSam Leffler 	 * Use highest priority AC.
13188a1b9b6aSSam Leffler 	 */
13198a1b9b6aSSam Leffler 	if (v_wme_ac > d_wme_ac)
13208a1b9b6aSSam Leffler 		ac = v_wme_ac;
13218a1b9b6aSSam Leffler 	else
13228a1b9b6aSSam Leffler 		ac = d_wme_ac;
13238a1b9b6aSSam Leffler 
13248a1b9b6aSSam Leffler 	/*
13258a1b9b6aSSam Leffler 	 * Apply ACM policy.
13268a1b9b6aSSam Leffler 	 */
1327b032f27cSSam Leffler 	if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
13288a1b9b6aSSam Leffler 		static const int acmap[4] = {
13298a1b9b6aSSam Leffler 			WME_AC_BK,	/* WME_AC_BE */
13308a1b9b6aSSam Leffler 			WME_AC_BK,	/* WME_AC_BK */
13318a1b9b6aSSam Leffler 			WME_AC_BE,	/* WME_AC_VI */
13328a1b9b6aSSam Leffler 			WME_AC_VI,	/* WME_AC_VO */
13338a1b9b6aSSam Leffler 		};
1334b032f27cSSam Leffler 		struct ieee80211com *ic = ni->ni_ic;
1335b032f27cSSam Leffler 
13368a1b9b6aSSam Leffler 		while (ac != WME_AC_BK &&
13378a1b9b6aSSam Leffler 		    ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
13388a1b9b6aSSam Leffler 			ac = acmap[ac];
13398a1b9b6aSSam Leffler 	}
13408a1b9b6aSSam Leffler done:
13418a1b9b6aSSam Leffler 	M_WME_SETAC(m, ac);
13428a1b9b6aSSam Leffler 	return 0;
13438a1b9b6aSSam Leffler }
13448a1b9b6aSSam Leffler 
13458a1b9b6aSSam Leffler /*
13465e923d2eSSam Leffler  * Insure there is sufficient contiguous space to encapsulate the
13475e923d2eSSam Leffler  * 802.11 data frame.  If room isn't already there, arrange for it.
13485e923d2eSSam Leffler  * Drivers and cipher modules assume we have done the necessary work
13495e923d2eSSam Leffler  * and fail rudely if they don't find the space they need.
13505e923d2eSSam Leffler  */
1351616190d0SSam Leffler struct mbuf *
1352b032f27cSSam Leffler ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
13535e923d2eSSam Leffler 	struct ieee80211_key *key, struct mbuf *m)
13545e923d2eSSam Leffler {
1355ab96db10SSam Leffler #define	TO_BE_RECLAIMED	(sizeof(struct ether_header) - sizeof(struct llc))
1356b032f27cSSam Leffler 	int needed_space = vap->iv_ic->ic_headroom + hdrsize;
13575e923d2eSSam Leffler 
13585e923d2eSSam Leffler 	if (key != NULL) {
13595e923d2eSSam Leffler 		/* XXX belongs in crypto code? */
13605e923d2eSSam Leffler 		needed_space += key->wk_cipher->ic_header;
13615e923d2eSSam Leffler 		/* XXX frags */
136283a244dbSSam Leffler 		/*
136383a244dbSSam Leffler 		 * When crypto is being done in the host we must insure
136483a244dbSSam Leffler 		 * the data are writable for the cipher routines; clone
136583a244dbSSam Leffler 		 * a writable mbuf chain.
136683a244dbSSam Leffler 		 * XXX handle SWMIC specially
136783a244dbSSam Leffler 		 */
13685c1f7f19SSam Leffler 		if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
1369bd29f817SBjoern A. Zeeb 			m = m_unshare(m, IEEE80211_M_NOWAIT);
137083a244dbSSam Leffler 			if (m == NULL) {
1371b032f27cSSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
137283a244dbSSam Leffler 				    "%s: cannot get writable mbuf\n", __func__);
1373b032f27cSSam Leffler 				vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
137483a244dbSSam Leffler 				return NULL;
137583a244dbSSam Leffler 			}
137683a244dbSSam Leffler 		}
13775e923d2eSSam Leffler 	}
13785e923d2eSSam Leffler 	/*
13795e923d2eSSam Leffler 	 * We know we are called just before stripping an Ethernet
13805e923d2eSSam Leffler 	 * header and prepending an LLC header.  This means we know
13815e923d2eSSam Leffler 	 * there will be
1382ab96db10SSam Leffler 	 *	sizeof(struct ether_header) - sizeof(struct llc)
13835e923d2eSSam Leffler 	 * bytes recovered to which we need additional space for the
13845e923d2eSSam Leffler 	 * 802.11 header and any crypto header.
13855e923d2eSSam Leffler 	 */
13865e923d2eSSam Leffler 	/* XXX check trailing space and copy instead? */
13875e923d2eSSam Leffler 	if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
1388bd29f817SBjoern A. Zeeb 		struct mbuf *n = m_gethdr(IEEE80211_M_NOWAIT, m->m_type);
13895e923d2eSSam Leffler 		if (n == NULL) {
1390b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
13915e923d2eSSam Leffler 			    "%s: cannot expand storage\n", __func__);
1392b032f27cSSam Leffler 			vap->iv_stats.is_tx_nobuf++;
13935e923d2eSSam Leffler 			m_freem(m);
13945e923d2eSSam Leffler 			return NULL;
13955e923d2eSSam Leffler 		}
13965e923d2eSSam Leffler 		KASSERT(needed_space <= MHLEN,
1397d7b5c50bSAlfred Perlstein 		    ("not enough room, need %u got %d\n", needed_space, MHLEN));
13985e923d2eSSam Leffler 		/*
13995e923d2eSSam Leffler 		 * Setup new mbuf to have leading space to prepend the
14005e923d2eSSam Leffler 		 * 802.11 header and any crypto header bits that are
14015e923d2eSSam Leffler 		 * required (the latter are added when the driver calls
14025e923d2eSSam Leffler 		 * back to ieee80211_crypto_encap to do crypto encapsulation).
14035e923d2eSSam Leffler 		 */
14045e923d2eSSam Leffler 		/* NB: must be first 'cuz it clobbers m_data */
14055e923d2eSSam Leffler 		m_move_pkthdr(n, m);
14065e923d2eSSam Leffler 		n->m_len = 0;			/* NB: m_gethdr does not set */
14075e923d2eSSam Leffler 		n->m_data += needed_space;
14085e923d2eSSam Leffler 		/*
14095e923d2eSSam Leffler 		 * Pull up Ethernet header to create the expected layout.
14105e923d2eSSam Leffler 		 * We could use m_pullup but that's overkill (i.e. we don't
14115e923d2eSSam Leffler 		 * need the actual data) and it cannot fail so do it inline
14125e923d2eSSam Leffler 		 * for speed.
14135e923d2eSSam Leffler 		 */
14145e923d2eSSam Leffler 		/* NB: struct ether_header is known to be contiguous */
14155e923d2eSSam Leffler 		n->m_len += sizeof(struct ether_header);
14165e923d2eSSam Leffler 		m->m_len -= sizeof(struct ether_header);
14175e923d2eSSam Leffler 		m->m_data += sizeof(struct ether_header);
14185e923d2eSSam Leffler 		/*
14195e923d2eSSam Leffler 		 * Replace the head of the chain.
14205e923d2eSSam Leffler 		 */
14215e923d2eSSam Leffler 		n->m_next = m;
14225e923d2eSSam Leffler 		m = n;
14235e923d2eSSam Leffler 	}
14245e923d2eSSam Leffler 	return m;
14255e923d2eSSam Leffler #undef TO_BE_RECLAIMED
14265e923d2eSSam Leffler }
14275e923d2eSSam Leffler 
14285e923d2eSSam Leffler /*
14295e923d2eSSam Leffler  * Return the transmit key to use in sending a unicast frame.
14305e923d2eSSam Leffler  * If a unicast key is set we use that.  When no unicast key is set
14315e923d2eSSam Leffler  * we fall back to the default transmit key.
14328a1b9b6aSSam Leffler  */
14338a1b9b6aSSam Leffler static __inline struct ieee80211_key *
1434b032f27cSSam Leffler ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
1435b032f27cSSam Leffler 	struct ieee80211_node *ni)
14368a1b9b6aSSam Leffler {
1437cda15ce1SSam Leffler 	if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
1438b032f27cSSam Leffler 		if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1439b032f27cSSam Leffler 		    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
14408a1b9b6aSSam Leffler 			return NULL;
1441b032f27cSSam Leffler 		return &vap->iv_nw_keys[vap->iv_def_txkey];
14428a1b9b6aSSam Leffler 	} else {
14438a1b9b6aSSam Leffler 		return &ni->ni_ucastkey;
14448a1b9b6aSSam Leffler 	}
14455e923d2eSSam Leffler }
14465e923d2eSSam Leffler 
14475e923d2eSSam Leffler /*
14485e923d2eSSam Leffler  * Return the transmit key to use in sending a multicast frame.
14495e923d2eSSam Leffler  * Multicast traffic always uses the group key which is installed as
14505e923d2eSSam Leffler  * the default tx key.
14515e923d2eSSam Leffler  */
14525e923d2eSSam Leffler static __inline struct ieee80211_key *
1453b032f27cSSam Leffler ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
1454b032f27cSSam Leffler 	struct ieee80211_node *ni)
14555e923d2eSSam Leffler {
1456b032f27cSSam Leffler 	if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1457b032f27cSSam Leffler 	    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
14585e923d2eSSam Leffler 		return NULL;
1459b032f27cSSam Leffler 	return &vap->iv_nw_keys[vap->iv_def_txkey];
14608a1b9b6aSSam Leffler }
14618a1b9b6aSSam Leffler 
14628a1b9b6aSSam Leffler /*
14638a1b9b6aSSam Leffler  * Encapsulate an outbound data frame.  The mbuf chain is updated.
14648a1b9b6aSSam Leffler  * If an error is encountered NULL is returned.  The caller is required
14658a1b9b6aSSam Leffler  * to provide a node reference and pullup the ethernet header in the
14668a1b9b6aSSam Leffler  * first mbuf.
1467b032f27cSSam Leffler  *
1468b032f27cSSam Leffler  * NB: Packet is assumed to be processed by ieee80211_classify which
1469b032f27cSSam Leffler  *     marked EAPOL frames w/ M_EAPOL.
14700a915fadSSam Leffler  */
14711a1e1d21SSam Leffler struct mbuf *
1472339ccfb3SSam Leffler ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
1473339ccfb3SSam Leffler     struct mbuf *m)
14741a1e1d21SSam Leffler {
1475b032f27cSSam Leffler #define	WH4(wh)	((struct ieee80211_frame_addr4 *)(wh))
14763c314f6dSMonthadar Al Jaberi #define MC01(mc)	((struct ieee80211_meshcntl_ae01 *)mc)
1477b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
147859aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
147959aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1480c104cff2SRui Paulo 	struct ieee80211_meshcntl_ae10 *mc;
14813c314f6dSMonthadar Al Jaberi 	struct ieee80211_mesh_route *rt = NULL;
14823c314f6dSMonthadar Al Jaberi 	int dir = -1;
148359aa14a9SRui Paulo #endif
14841a1e1d21SSam Leffler 	struct ether_header eh;
14851a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
14868a1b9b6aSSam Leffler 	struct ieee80211_key *key;
14871a1e1d21SSam Leffler 	struct llc *llc;
14889764ef21SAdrian Chadd 	int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr, is_mcast;
14899e80b1dfSSam Leffler 	ieee80211_seq seqno;
149059aa14a9SRui Paulo 	int meshhdrsize, meshae;
149159aa14a9SRui Paulo 	uint8_t *qos;
14921c7b0c84SAdrian Chadd 	int is_amsdu = 0;
14931a1e1d21SSam Leffler 
14945cda6006SAdrian Chadd 	IEEE80211_TX_LOCK_ASSERT(ic);
14955cda6006SAdrian Chadd 
14969764ef21SAdrian Chadd 	is_mcast = !! (m->m_flags & (M_MCAST | M_BCAST));
14979764ef21SAdrian Chadd 
149868e8e04eSSam Leffler 	/*
149968e8e04eSSam Leffler 	 * Copy existing Ethernet header to a safe place.  The
150068e8e04eSSam Leffler 	 * rest of the code assumes it's ok to strip it when
150168e8e04eSSam Leffler 	 * reorganizing state for the final encapsulation.
150268e8e04eSSam Leffler 	 */
15038a1b9b6aSSam Leffler 	KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
1504b032f27cSSam Leffler 	ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
15051a1e1d21SSam Leffler 
15068a1b9b6aSSam Leffler 	/*
15078a1b9b6aSSam Leffler 	 * Insure space for additional headers.  First identify
15088a1b9b6aSSam Leffler 	 * transmit key to use in calculating any buffer adjustments
15098a1b9b6aSSam Leffler 	 * required.  This is also used below to do privacy
15108a1b9b6aSSam Leffler 	 * encapsulation work.  Then calculate the 802.11 header
15118a1b9b6aSSam Leffler 	 * size and any padding required by the driver.
15128a1b9b6aSSam Leffler 	 *
15138a1b9b6aSSam Leffler 	 * Note key may be NULL if we fall back to the default
15148a1b9b6aSSam Leffler 	 * transmit key and that is not set.  In that case the
15158a1b9b6aSSam Leffler 	 * buffer may not be expanded as needed by the cipher
15168a1b9b6aSSam Leffler 	 * routines, but they will/should discard it.
15178a1b9b6aSSam Leffler 	 */
1518b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1519b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA ||
1520b032f27cSSam Leffler 		    !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
1521b032f27cSSam Leffler 		    (vap->iv_opmode == IEEE80211_M_WDS &&
15222172664cSAdrian Chadd 		     (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY))) {
1523b032f27cSSam Leffler 			key = ieee80211_crypto_getucastkey(vap, ni);
15242172664cSAdrian Chadd 		} else if ((vap->iv_opmode == IEEE80211_M_WDS) &&
15252172664cSAdrian Chadd 		    (! (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY))) {
15262172664cSAdrian Chadd 			/*
15272172664cSAdrian Chadd 			 * Use ucastkey for DWDS transmit nodes, multicast
15282172664cSAdrian Chadd 			 * or otherwise.
15292172664cSAdrian Chadd 			 *
15302172664cSAdrian Chadd 			 * This is required to ensure that multicast frames
15312172664cSAdrian Chadd 			 * from a DWDS AP to a DWDS STA is encrypted with
15322172664cSAdrian Chadd 			 * a key that can actually work.
15332172664cSAdrian Chadd 			 *
15342172664cSAdrian Chadd 			 * There's no default key for multicast traffic
15352172664cSAdrian Chadd 			 * on a DWDS WDS VAP node (note NOT the DWDS enabled
15362172664cSAdrian Chadd 			 * AP VAP, the dynamically created per-STA WDS node)
15372172664cSAdrian Chadd 			 * so encap fails and transmit fails.
15382172664cSAdrian Chadd 			 */
15392172664cSAdrian Chadd 			key = ieee80211_crypto_getucastkey(vap, ni);
15402172664cSAdrian Chadd 		} else {
1541b032f27cSSam Leffler 			key = ieee80211_crypto_getmcastkey(vap, ni);
15422172664cSAdrian Chadd 		}
1543b032f27cSSam Leffler 		if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
1544b032f27cSSam Leffler 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
1545b032f27cSSam Leffler 			    eh.ether_dhost,
1546b032f27cSSam Leffler 			    "no default transmit key (%s) deftxkey %u",
1547b032f27cSSam Leffler 			    __func__, vap->iv_def_txkey);
1548b032f27cSSam Leffler 			vap->iv_stats.is_tx_nodefkey++;
1549d72c7253SSam Leffler 			goto bad;
15508a1b9b6aSSam Leffler 		}
15518a1b9b6aSSam Leffler 	} else
15528a1b9b6aSSam Leffler 		key = NULL;
15535e923d2eSSam Leffler 	/*
15545e923d2eSSam Leffler 	 * XXX Some ap's don't handle QoS-encapsulated EAPOL
15555e923d2eSSam Leffler 	 * frames so suppress use.  This may be an issue if other
15565e923d2eSSam Leffler 	 * ap's require all data frames to be QoS-encapsulated
15575e923d2eSSam Leffler 	 * once negotiated in which case we'll need to make this
15585e923d2eSSam Leffler 	 * configurable.
15599764ef21SAdrian Chadd 	 *
15609764ef21SAdrian Chadd 	 * Don't send multicast QoS frames.
15619764ef21SAdrian Chadd 	 * Technically multicast frames can be QoS if all stations in the
15629764ef21SAdrian Chadd 	 * BSS are also QoS.
15639764ef21SAdrian Chadd 	 *
15649764ef21SAdrian Chadd 	 * NB: mesh data frames are QoS, including multicast frames.
15655e923d2eSSam Leffler 	 */
15669764ef21SAdrian Chadd 	addqos =
15679764ef21SAdrian Chadd 	    (((is_mcast == 0) && (ni->ni_flags &
15689764ef21SAdrian Chadd 	     (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))) ||
156991216c71SAdrian Chadd 	    (vap->iv_opmode == IEEE80211_M_MBSS)) &&
1570b032f27cSSam Leffler 	    (m->m_flags & M_EAPOL) == 0;
15719764ef21SAdrian Chadd 
15725e923d2eSSam Leffler 	if (addqos)
15738a1b9b6aSSam Leffler 		hdrsize = sizeof(struct ieee80211_qosframe);
15748a1b9b6aSSam Leffler 	else
15758a1b9b6aSSam Leffler 		hdrsize = sizeof(struct ieee80211_frame);
157659aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
157759aa14a9SRui Paulo 	if (vap->iv_opmode == IEEE80211_M_MBSS) {
157859aa14a9SRui Paulo 		/*
157959aa14a9SRui Paulo 		 * Mesh data frames are encapsulated according to the
158059aa14a9SRui Paulo 		 * rules of Section 11B.8.5 (p.139 of D3.0 spec).
158159aa14a9SRui Paulo 		 * o Group Addressed data (aka multicast) originating
158259aa14a9SRui Paulo 		 *   at the local sta are sent w/ 3-address format and
158359aa14a9SRui Paulo 		 *   address extension mode 00
158459aa14a9SRui Paulo 		 * o Individually Addressed data (aka unicast) originating
158559aa14a9SRui Paulo 		 *   at the local sta are sent w/ 4-address format and
158659aa14a9SRui Paulo 		 *   address extension mode 00
158759aa14a9SRui Paulo 		 * o Group Addressed data forwarded from a non-mesh sta are
158859aa14a9SRui Paulo 		 *   sent w/ 3-address format and address extension mode 01
158959aa14a9SRui Paulo 		 * o Individually Address data from another sta are sent
159059aa14a9SRui Paulo 		 *   w/ 4-address format and address extension mode 10
159159aa14a9SRui Paulo 		 */
159259aa14a9SRui Paulo 		is4addr = 0;		/* NB: don't use, disable */
15933c314f6dSMonthadar Al Jaberi 		if (!IEEE80211_IS_MULTICAST(eh.ether_dhost)) {
15943c314f6dSMonthadar Al Jaberi 			rt = ieee80211_mesh_rt_find(vap, eh.ether_dhost);
15953c314f6dSMonthadar Al Jaberi 			KASSERT(rt != NULL, ("route is NULL"));
15963c314f6dSMonthadar Al Jaberi 			dir = IEEE80211_FC1_DIR_DSTODS;
15973c314f6dSMonthadar Al Jaberi 			hdrsize += IEEE80211_ADDR_LEN;
15983c314f6dSMonthadar Al Jaberi 			if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
15993c314f6dSMonthadar Al Jaberi 				if (IEEE80211_ADDR_EQ(rt->rt_mesh_gate,
16003c314f6dSMonthadar Al Jaberi 				    vap->iv_myaddr)) {
16013c314f6dSMonthadar Al Jaberi 					IEEE80211_NOTE_MAC(vap,
16023c314f6dSMonthadar Al Jaberi 					    IEEE80211_MSG_MESH,
16033c314f6dSMonthadar Al Jaberi 					    eh.ether_dhost,
16043c314f6dSMonthadar Al Jaberi 					    "%s", "trying to send to ourself");
16053c314f6dSMonthadar Al Jaberi 					goto bad;
16063c314f6dSMonthadar Al Jaberi 				}
16073c314f6dSMonthadar Al Jaberi 				meshae = IEEE80211_MESH_AE_10;
16083c314f6dSMonthadar Al Jaberi 				meshhdrsize =
16093c314f6dSMonthadar Al Jaberi 				    sizeof(struct ieee80211_meshcntl_ae10);
161059aa14a9SRui Paulo 			} else {
16113c314f6dSMonthadar Al Jaberi 				meshae = IEEE80211_MESH_AE_00;
16123c314f6dSMonthadar Al Jaberi 				meshhdrsize =
16133c314f6dSMonthadar Al Jaberi 				    sizeof(struct ieee80211_meshcntl);
16143c314f6dSMonthadar Al Jaberi 			}
16153c314f6dSMonthadar Al Jaberi 		} else {
16163c314f6dSMonthadar Al Jaberi 			dir = IEEE80211_FC1_DIR_FROMDS;
16173c314f6dSMonthadar Al Jaberi 			if (!IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) {
16183c314f6dSMonthadar Al Jaberi 				/* proxy group */
16193c314f6dSMonthadar Al Jaberi 				meshae = IEEE80211_MESH_AE_01;
16203c314f6dSMonthadar Al Jaberi 				meshhdrsize =
16213c314f6dSMonthadar Al Jaberi 				    sizeof(struct ieee80211_meshcntl_ae01);
16223c314f6dSMonthadar Al Jaberi 			} else {
16233c314f6dSMonthadar Al Jaberi 				/* group */
16243c314f6dSMonthadar Al Jaberi 				meshae = IEEE80211_MESH_AE_00;
16253c314f6dSMonthadar Al Jaberi 				meshhdrsize = sizeof(struct ieee80211_meshcntl);
16263c314f6dSMonthadar Al Jaberi 			}
162759aa14a9SRui Paulo 		}
162859aa14a9SRui Paulo 	} else {
162959aa14a9SRui Paulo #endif
1630b032f27cSSam Leffler 		/*
1631b032f27cSSam Leffler 		 * 4-address frames need to be generated for:
16326437e6daSSam Leffler 		 * o packets sent through a WDS vap (IEEE80211_M_WDS)
1633f2a6a13cSSam Leffler 		 * o packets sent through a vap marked for relaying
1634f2a6a13cSSam Leffler 		 *   (e.g. a station operating with dynamic WDS)
1635b032f27cSSam Leffler 		 */
16366437e6daSSam Leffler 		is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
1637f2a6a13cSSam Leffler 		    ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
1638b032f27cSSam Leffler 		     !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
1639b032f27cSSam Leffler 		if (is4addr)
1640b032f27cSSam Leffler 			hdrsize += IEEE80211_ADDR_LEN;
164159aa14a9SRui Paulo 		meshhdrsize = meshae = 0;
164259aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
164359aa14a9SRui Paulo 	}
164459aa14a9SRui Paulo #endif
1645b032f27cSSam Leffler 	/*
1646b032f27cSSam Leffler 	 * Honor driver DATAPAD requirement.
1647b032f27cSSam Leffler 	 */
16488a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
1649b032f27cSSam Leffler 		hdrspace = roundup(hdrsize, sizeof(uint32_t));
1650b032f27cSSam Leffler 	else
1651b032f27cSSam Leffler 		hdrspace = hdrsize;
165268e8e04eSSam Leffler 
1653616190d0SSam Leffler 	if (__predict_true((m->m_flags & M_FF) == 0)) {
165468e8e04eSSam Leffler 		/*
165568e8e04eSSam Leffler 		 * Normal frame.
165668e8e04eSSam Leffler 		 */
165759aa14a9SRui Paulo 		m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m);
16588a1b9b6aSSam Leffler 		if (m == NULL) {
16598a1b9b6aSSam Leffler 			/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
16600a915fadSSam Leffler 			goto bad;
16610a915fadSSam Leffler 		}
166268e8e04eSSam Leffler 		/* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
1663ab96db10SSam Leffler 		m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
16641a1e1d21SSam Leffler 		llc = mtod(m, struct llc *);
16651a1e1d21SSam Leffler 		llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
16661a1e1d21SSam Leffler 		llc->llc_control = LLC_UI;
16671a1e1d21SSam Leffler 		llc->llc_snap.org_code[0] = 0;
16681a1e1d21SSam Leffler 		llc->llc_snap.org_code[1] = 0;
16691a1e1d21SSam Leffler 		llc->llc_snap.org_code[2] = 0;
16701a1e1d21SSam Leffler 		llc->llc_snap.ether_type = eh.ether_type;
1671616190d0SSam Leffler 	} else {
1672616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1673339ccfb3SSam Leffler 		/*
16741c7b0c84SAdrian Chadd 		 * Aggregated frame.  Check if it's for AMSDU or FF.
16751c7b0c84SAdrian Chadd 		 *
16761c7b0c84SAdrian Chadd 		 * XXX TODO: IEEE80211_NODE_AMSDU* isn't implemented
16771c7b0c84SAdrian Chadd 		 * anywhere for some reason.  But, since 11n requires
16781c7b0c84SAdrian Chadd 		 * AMSDU RX, we can just assume "11n" == "AMSDU".
1679339ccfb3SSam Leffler 		 */
16801c7b0c84SAdrian Chadd 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, "%s: called; M_FF\n", __func__);
16811c7b0c84SAdrian Chadd 		if (ieee80211_amsdu_tx_ok(ni)) {
16821c7b0c84SAdrian Chadd 			m = ieee80211_amsdu_encap(vap, m, hdrspace + meshhdrsize, key);
16831c7b0c84SAdrian Chadd 			is_amsdu = 1;
16841c7b0c84SAdrian Chadd 		} else {
168559aa14a9SRui Paulo 			m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key);
16861c7b0c84SAdrian Chadd 		}
1687616190d0SSam Leffler 		if (m == NULL)
1688616190d0SSam Leffler #endif
1689616190d0SSam Leffler 			goto bad;
169068e8e04eSSam Leffler 	}
16918a1b9b6aSSam Leffler 	datalen = m->m_pkthdr.len;		/* NB: w/o 802.11 header */
16928a1b9b6aSSam Leffler 
1693bd29f817SBjoern A. Zeeb 	M_PREPEND(m, hdrspace + meshhdrsize, IEEE80211_M_NOWAIT);
16941be50176SSam Leffler 	if (m == NULL) {
1695b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
16960a915fadSSam Leffler 		goto bad;
16971be50176SSam Leffler 	}
16981a1e1d21SSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
16991a1e1d21SSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
170068e8e04eSSam Leffler 	*(uint16_t *)wh->i_dur = 0;
170159aa14a9SRui Paulo 	qos = NULL;	/* NB: quiet compiler */
1702b032f27cSSam Leffler 	if (is4addr) {
1703b032f27cSSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1704b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1705b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1706b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1707b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1708b032f27cSSam Leffler 	} else switch (vap->iv_opmode) {
17091a1e1d21SSam Leffler 	case IEEE80211_M_STA:
17101a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
17111a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
17121a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
17131a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
17141a1e1d21SSam Leffler 		break;
17151a1e1d21SSam Leffler 	case IEEE80211_M_IBSS:
17161a1e1d21SSam Leffler 	case IEEE80211_M_AHDEMO:
17171a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
17181a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
17191a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1720a8b16e87SSam Leffler 		/*
1721b032f27cSSam Leffler 		 * NB: always use the bssid from iv_bss as the
1722a8b16e87SSam Leffler 		 *     neighbor's may be stale after an ibss merge
1723a8b16e87SSam Leffler 		 */
1724b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
17251a1e1d21SSam Leffler 		break;
17261a1e1d21SSam Leffler 	case IEEE80211_M_HOSTAP:
17271a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
17281a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
17291a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
17301a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
17311a1e1d21SSam Leffler 		break;
173259aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
173359aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
173459aa14a9SRui Paulo 		/* NB: offset by hdrspace to deal with DATAPAD */
1735c104cff2SRui Paulo 		mc = (struct ieee80211_meshcntl_ae10 *)
173659aa14a9SRui Paulo 		     (mtod(m, uint8_t *) + hdrspace);
17373c314f6dSMonthadar Al Jaberi 		wh->i_fc[1] = dir;
173859aa14a9SRui Paulo 		switch (meshae) {
17393c314f6dSMonthadar Al Jaberi 		case IEEE80211_MESH_AE_00:	/* no proxy */
174059aa14a9SRui Paulo 			mc->mc_flags = 0;
17413c314f6dSMonthadar Al Jaberi 			if (dir == IEEE80211_FC1_DIR_DSTODS) { /* ucast */
17423c314f6dSMonthadar Al Jaberi 				IEEE80211_ADDR_COPY(wh->i_addr1,
17433c314f6dSMonthadar Al Jaberi 				    ni->ni_macaddr);
17443c314f6dSMonthadar Al Jaberi 				IEEE80211_ADDR_COPY(wh->i_addr2,
17453c314f6dSMonthadar Al Jaberi 				    vap->iv_myaddr);
17463c314f6dSMonthadar Al Jaberi 				IEEE80211_ADDR_COPY(wh->i_addr3,
17473c314f6dSMonthadar Al Jaberi 				    eh.ether_dhost);
17483c314f6dSMonthadar Al Jaberi 				IEEE80211_ADDR_COPY(WH4(wh)->i_addr4,
17493c314f6dSMonthadar Al Jaberi 				    eh.ether_shost);
17503c314f6dSMonthadar Al Jaberi 				qos =((struct ieee80211_qosframe_addr4 *)
17513c314f6dSMonthadar Al Jaberi 				    wh)->i_qos;
17523c314f6dSMonthadar Al Jaberi 			} else if (dir == IEEE80211_FC1_DIR_FROMDS) {
17533c314f6dSMonthadar Al Jaberi 				 /* mcast */
17543c314f6dSMonthadar Al Jaberi 				IEEE80211_ADDR_COPY(wh->i_addr1,
17553c314f6dSMonthadar Al Jaberi 				    eh.ether_dhost);
17563c314f6dSMonthadar Al Jaberi 				IEEE80211_ADDR_COPY(wh->i_addr2,
17573c314f6dSMonthadar Al Jaberi 				    vap->iv_myaddr);
17583c314f6dSMonthadar Al Jaberi 				IEEE80211_ADDR_COPY(wh->i_addr3,
17593c314f6dSMonthadar Al Jaberi 				    eh.ether_shost);
17603c314f6dSMonthadar Al Jaberi 				qos = ((struct ieee80211_qosframe *)
17613c314f6dSMonthadar Al Jaberi 				    wh)->i_qos;
17623c314f6dSMonthadar Al Jaberi 			}
176359aa14a9SRui Paulo 			break;
17643c314f6dSMonthadar Al Jaberi 		case IEEE80211_MESH_AE_01:	/* mcast, proxy */
176559aa14a9SRui Paulo 			wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
176659aa14a9SRui Paulo 			IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
176759aa14a9SRui Paulo 			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1768c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr);
176959aa14a9SRui Paulo 			mc->mc_flags = 1;
17703c314f6dSMonthadar Al Jaberi 			IEEE80211_ADDR_COPY(MC01(mc)->mc_addr4,
17713c314f6dSMonthadar Al Jaberi 			    eh.ether_shost);
177259aa14a9SRui Paulo 			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
177359aa14a9SRui Paulo 			break;
17743c314f6dSMonthadar Al Jaberi 		case IEEE80211_MESH_AE_10:	/* ucast, proxy */
17753c314f6dSMonthadar Al Jaberi 			KASSERT(rt != NULL, ("route is NULL"));
17763c314f6dSMonthadar Al Jaberi 			IEEE80211_ADDR_COPY(wh->i_addr1, rt->rt_nexthop);
177759aa14a9SRui Paulo 			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
17783c314f6dSMonthadar Al Jaberi 			IEEE80211_ADDR_COPY(wh->i_addr3, rt->rt_mesh_gate);
1779c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr);
17803c314f6dSMonthadar Al Jaberi 			mc->mc_flags = IEEE80211_MESH_AE_10;
17813c314f6dSMonthadar Al Jaberi 			IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_dhost);
17823c314f6dSMonthadar Al Jaberi 			IEEE80211_ADDR_COPY(mc->mc_addr6, eh.ether_shost);
178359aa14a9SRui Paulo 			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
178459aa14a9SRui Paulo 			break;
178559aa14a9SRui Paulo 		default:
178659aa14a9SRui Paulo 			KASSERT(0, ("meshae %d", meshae));
178759aa14a9SRui Paulo 			break;
178859aa14a9SRui Paulo 		}
178959aa14a9SRui Paulo 		mc->mc_ttl = ms->ms_ttl;
179059aa14a9SRui Paulo 		ms->ms_seq++;
179131021a2bSAndriy Voskoboinyk 		le32enc(mc->mc_seq, ms->ms_seq);
179259aa14a9SRui Paulo 		break;
179359aa14a9SRui Paulo #endif
1794b032f27cSSam Leffler 	case IEEE80211_M_WDS:		/* NB: is4addr should always be true */
179559aa14a9SRui Paulo 	default:
17960a915fadSSam Leffler 		goto bad;
17971a1e1d21SSam Leffler 	}
1798bc5627d9SSam Leffler 	if (m->m_flags & M_MORE_DATA)
1799bc5627d9SSam Leffler 		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
18005e923d2eSSam Leffler 	if (addqos) {
18018a1b9b6aSSam Leffler 		int ac, tid;
18028a1b9b6aSSam Leffler 
1803b032f27cSSam Leffler 		if (is4addr) {
1804b032f27cSSam Leffler 			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
180559aa14a9SRui Paulo 		/* NB: mesh case handled earlier */
180659aa14a9SRui Paulo 		} else if (vap->iv_opmode != IEEE80211_M_MBSS)
1807b032f27cSSam Leffler 			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
18088a1b9b6aSSam Leffler 		ac = M_WME_GETAC(m);
18098a1b9b6aSSam Leffler 		/* map from access class/queue to 11e header priorty value */
18108a1b9b6aSSam Leffler 		tid = WME_AC_TO_TID(ac);
1811b032f27cSSam Leffler 		qos[0] = tid & IEEE80211_QOS_TID;
18128a1b9b6aSSam Leffler 		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
1813b032f27cSSam Leffler 			qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
181491216c71SAdrian Chadd #ifdef IEEE80211_SUPPORT_MESH
1815d566999aSMonthadar Al Jaberi 		if (vap->iv_opmode == IEEE80211_M_MBSS)
1816d566999aSMonthadar Al Jaberi 			qos[1] = IEEE80211_QOS_MC;
1817d566999aSMonthadar Al Jaberi 		else
181891216c71SAdrian Chadd #endif
1819b032f27cSSam Leffler 			qos[1] = 0;
1820c9b7e9dfSBjoern A. Zeeb 		wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS_DATA;
18218a1b9b6aSSam Leffler 
18221c7b0c84SAdrian Chadd 		/*
18231c7b0c84SAdrian Chadd 		 * If this is an A-MSDU then ensure we set the
18241c7b0c84SAdrian Chadd 		 * relevant field.
18251c7b0c84SAdrian Chadd 		 */
18261c7b0c84SAdrian Chadd 		if (is_amsdu)
18271c7b0c84SAdrian Chadd 			qos[0] |= IEEE80211_QOS_AMSDU;
18281c7b0c84SAdrian Chadd 
182951172f62SAdrian Chadd 		/*
183051172f62SAdrian Chadd 		 * XXX TODO TX lock is needed for atomic updates of sequence
183151172f62SAdrian Chadd 		 * numbers.  If the driver does it, then don't do it here;
183251172f62SAdrian Chadd 		 * and we don't need the TX lock held.
183351172f62SAdrian Chadd 		 */
183445f856e3SSam Leffler 		if ((m->m_flags & M_AMPDU_MPDU) == 0) {
183545f856e3SSam Leffler 			/*
18369764ef21SAdrian Chadd 			 * 802.11-2012 9.3.2.10 -
18379764ef21SAdrian Chadd 			 *
18389764ef21SAdrian Chadd 			 * If this is a multicast frame then we need
18399764ef21SAdrian Chadd 			 * to ensure that the sequence number comes from
18409764ef21SAdrian Chadd 			 * a separate seqno space and not the TID space.
18419764ef21SAdrian Chadd 			 *
18429764ef21SAdrian Chadd 			 * Otherwise multicast frames may actually cause
18439764ef21SAdrian Chadd 			 * holes in the TX blockack window space and
18449764ef21SAdrian Chadd 			 * upset various things.
18459764ef21SAdrian Chadd 			 */
18469764ef21SAdrian Chadd 			if (IEEE80211_IS_MULTICAST(wh->i_addr1))
18479764ef21SAdrian Chadd 				seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
18489764ef21SAdrian Chadd 			else
18499764ef21SAdrian Chadd 				seqno = ni->ni_txseqs[tid]++;
18509764ef21SAdrian Chadd 
18519764ef21SAdrian Chadd 			/*
185245f856e3SSam Leffler 			 * NB: don't assign a sequence # to potential
185345f856e3SSam Leffler 			 * aggregates; we expect this happens at the
185445f856e3SSam Leffler 			 * point the frame comes off any aggregation q
185545f856e3SSam Leffler 			 * as otherwise we may introduce holes in the
185645f856e3SSam Leffler 			 * BA sequence space and/or make window accouting
185745f856e3SSam Leffler 			 * more difficult.
185845f856e3SSam Leffler 			 *
185945f856e3SSam Leffler 			 * XXX may want to control this with a driver
186045f856e3SSam Leffler 			 * capability; this may also change when we pull
186145f856e3SSam Leffler 			 * aggregation up into net80211
186245f856e3SSam Leffler 			 */
186368e8e04eSSam Leffler 			*(uint16_t *)wh->i_seq =
18649e80b1dfSSam Leffler 			    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
18659a841c7fSSam Leffler 			M_SEQNO_SET(m, seqno);
18662db223f9SAndriy Voskoboinyk 		} else {
18672db223f9SAndriy Voskoboinyk 			/* NB: zero out i_seq field (for s/w encryption etc) */
18682db223f9SAndriy Voskoboinyk 			*(uint16_t *)wh->i_seq = 0;
186945f856e3SSam Leffler 		}
18708a1b9b6aSSam Leffler 	} else {
187151172f62SAdrian Chadd 		/*
187251172f62SAdrian Chadd 		 * XXX TODO TX lock is needed for atomic updates of sequence
187351172f62SAdrian Chadd 		 * numbers.  If the driver does it, then don't do it here;
187451172f62SAdrian Chadd 		 * and we don't need the TX lock held.
187551172f62SAdrian Chadd 		 */
18769e80b1dfSSam Leffler 		seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
187768e8e04eSSam Leffler 		*(uint16_t *)wh->i_seq =
18789e80b1dfSSam Leffler 		    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
18799a841c7fSSam Leffler 		M_SEQNO_SET(m, seqno);
18801c7b0c84SAdrian Chadd 
18811c7b0c84SAdrian Chadd 		/*
18821c7b0c84SAdrian Chadd 		 * XXX TODO: we shouldn't allow EAPOL, etc that would
18831c7b0c84SAdrian Chadd 		 * be forced to be non-QoS traffic to be A-MSDU encapsulated.
18841c7b0c84SAdrian Chadd 		 */
18851c7b0c84SAdrian Chadd 		if (is_amsdu)
18861c7b0c84SAdrian Chadd 			printf("%s: XXX ERROR: is_amsdu set; not QoS!\n",
18871c7b0c84SAdrian Chadd 			    __func__);
18888a1b9b6aSSam Leffler 	}
18899a841c7fSSam Leffler 
189043eafd0dSAdrian Chadd 	/*
189143eafd0dSAdrian Chadd 	 * Check if xmit fragmentation is required.
189243eafd0dSAdrian Chadd 	 *
189343eafd0dSAdrian Chadd 	 * If the hardware does fragmentation offload, then don't bother
189443eafd0dSAdrian Chadd 	 * doing it here.
189543eafd0dSAdrian Chadd 	 */
189643eafd0dSAdrian Chadd 	if (IEEE80211_CONF_FRAG_OFFLOAD(ic))
189743eafd0dSAdrian Chadd 		txfrag = 0;
189843eafd0dSAdrian Chadd 	else
1899b032f27cSSam Leffler 		txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
190068e8e04eSSam Leffler 		    !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1901b032f27cSSam Leffler 		    (vap->iv_caps & IEEE80211_C_TXFRAG) &&
1902e9cfc1f5SSam Leffler 		    (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
190343eafd0dSAdrian Chadd 
19045e923d2eSSam Leffler 	if (key != NULL) {
19055e923d2eSSam Leffler 		/*
19065e923d2eSSam Leffler 		 * IEEE 802.1X: send EAPOL frames always in the clear.
19075e923d2eSSam Leffler 		 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
19085e923d2eSSam Leffler 		 */
1909b032f27cSSam Leffler 		if ((m->m_flags & M_EAPOL) == 0 ||
1910b032f27cSSam Leffler 		    ((vap->iv_flags & IEEE80211_F_WPA) &&
1911b032f27cSSam Leffler 		     (vap->iv_opmode == IEEE80211_M_STA ?
1912cda15ce1SSam Leffler 		      !IEEE80211_KEY_UNDEFINED(key) :
1913cda15ce1SSam Leffler 		      !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
19145945b5f5SKevin Lo 			wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
1915b032f27cSSam Leffler 			if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1916b032f27cSSam Leffler 				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1917b032f27cSSam Leffler 				    eh.ether_dhost,
1918b032f27cSSam Leffler 				    "%s", "enmic failed, discard frame");
1919b032f27cSSam Leffler 				vap->iv_stats.is_crypto_enmicfail++;
19205e923d2eSSam Leffler 				goto bad;
19215e923d2eSSam Leffler 			}
19225e923d2eSSam Leffler 		}
19235e923d2eSSam Leffler 	}
1924b032f27cSSam Leffler 	if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1925b032f27cSSam Leffler 	    key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
192668e8e04eSSam Leffler 		goto bad;
19278a1b9b6aSSam Leffler 
1928c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
1929c1af44bdSSam Leffler 
19308a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_data);
19319e80b1dfSSam Leffler 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1932b6e9b119SSam Leffler 		IEEE80211_NODE_STAT(ni, tx_mcast);
19339e80b1dfSSam Leffler 		m->m_flags |= M_MCAST;
19349e80b1dfSSam Leffler 	} else
1935b6e9b119SSam Leffler 		IEEE80211_NODE_STAT(ni, tx_ucast);
19368a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
19378a1b9b6aSSam Leffler 
19381a1e1d21SSam Leffler 	return m;
19390a915fadSSam Leffler bad:
19400a915fadSSam Leffler 	if (m != NULL)
19410a915fadSSam Leffler 		m_freem(m);
19420a915fadSSam Leffler 	return NULL;
1943b032f27cSSam Leffler #undef WH4
19443c314f6dSMonthadar Al Jaberi #undef MC01
19451a1e1d21SSam Leffler }
19461a1e1d21SSam Leffler 
1947d07be335SAdrian Chadd void
1948d07be335SAdrian Chadd ieee80211_free_mbuf(struct mbuf *m)
1949d07be335SAdrian Chadd {
1950d07be335SAdrian Chadd 	struct mbuf *next;
1951d07be335SAdrian Chadd 
1952d07be335SAdrian Chadd 	if (m == NULL)
1953d07be335SAdrian Chadd 		return;
1954d07be335SAdrian Chadd 
1955d07be335SAdrian Chadd 	do {
1956d07be335SAdrian Chadd 		next = m->m_nextpkt;
1957d07be335SAdrian Chadd 		m->m_nextpkt = NULL;
1958d07be335SAdrian Chadd 		m_freem(m);
1959d07be335SAdrian Chadd 	} while ((m = next) != NULL);
1960d07be335SAdrian Chadd }
1961d07be335SAdrian Chadd 
19621a1e1d21SSam Leffler /*
196368e8e04eSSam Leffler  * Fragment the frame according to the specified mtu.
196468e8e04eSSam Leffler  * The size of the 802.11 header (w/o padding) is provided
196568e8e04eSSam Leffler  * so we don't need to recalculate it.  We create a new
196668e8e04eSSam Leffler  * mbuf for each fragment and chain it through m_nextpkt;
196768e8e04eSSam Leffler  * we might be able to optimize this by reusing the original
196868e8e04eSSam Leffler  * packet's mbufs but that is significantly more complicated.
196968e8e04eSSam Leffler  */
197068e8e04eSSam Leffler static int
1971b032f27cSSam Leffler ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
197268e8e04eSSam Leffler 	u_int hdrsize, u_int ciphdrsize, u_int mtu)
197368e8e04eSSam Leffler {
19748d46c25dSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
197568e8e04eSSam Leffler 	struct ieee80211_frame *wh, *whf;
1976d07be335SAdrian Chadd 	struct mbuf *m, *prev;
197768e8e04eSSam Leffler 	u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
19788d46c25dSAdrian Chadd 	u_int hdrspace;
197968e8e04eSSam Leffler 
198068e8e04eSSam Leffler 	KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
198168e8e04eSSam Leffler 	KASSERT(m0->m_pkthdr.len > mtu,
198268e8e04eSSam Leffler 		("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
198368e8e04eSSam Leffler 
19848d46c25dSAdrian Chadd 	/*
19858d46c25dSAdrian Chadd 	 * Honor driver DATAPAD requirement.
19868d46c25dSAdrian Chadd 	 */
19878d46c25dSAdrian Chadd 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
19888d46c25dSAdrian Chadd 		hdrspace = roundup(hdrsize, sizeof(uint32_t));
19898d46c25dSAdrian Chadd 	else
19908d46c25dSAdrian Chadd 		hdrspace = hdrsize;
19918d46c25dSAdrian Chadd 
199268e8e04eSSam Leffler 	wh = mtod(m0, struct ieee80211_frame *);
199368e8e04eSSam Leffler 	/* NB: mark the first frag; it will be propagated below */
199468e8e04eSSam Leffler 	wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
19958d46c25dSAdrian Chadd 	totalhdrsize = hdrspace + ciphdrsize;
199668e8e04eSSam Leffler 	fragno = 1;
199768e8e04eSSam Leffler 	off = mtu - ciphdrsize;
199868e8e04eSSam Leffler 	remainder = m0->m_pkthdr.len - off;
199968e8e04eSSam Leffler 	prev = m0;
200068e8e04eSSam Leffler 	do {
200118d20be0SAndriy Voskoboinyk 		fragsize = MIN(totalhdrsize + remainder, mtu);
2002bd29f817SBjoern A. Zeeb 		m = m_get2(fragsize, IEEE80211_M_NOWAIT, MT_DATA, M_PKTHDR);
200368e8e04eSSam Leffler 		if (m == NULL)
200468e8e04eSSam Leffler 			goto bad;
200568e8e04eSSam Leffler 		/* leave room to prepend any cipher header */
200668e8e04eSSam Leffler 		m_align(m, fragsize - ciphdrsize);
200768e8e04eSSam Leffler 
200868e8e04eSSam Leffler 		/*
200968e8e04eSSam Leffler 		 * Form the header in the fragment.  Note that since
201068e8e04eSSam Leffler 		 * we mark the first fragment with the MORE_FRAG bit
201168e8e04eSSam Leffler 		 * it automatically is propagated to each fragment; we
201268e8e04eSSam Leffler 		 * need only clear it on the last fragment (done below).
201391216c71SAdrian Chadd 		 * NB: frag 1+ dont have Mesh Control field present.
201468e8e04eSSam Leffler 		 */
201568e8e04eSSam Leffler 		whf = mtod(m, struct ieee80211_frame *);
201668e8e04eSSam Leffler 		memcpy(whf, wh, hdrsize);
201791216c71SAdrian Chadd #ifdef IEEE80211_SUPPORT_MESH
2018f3f08e16SAndriy Voskoboinyk 		if (vap->iv_opmode == IEEE80211_M_MBSS)
2019f3f08e16SAndriy Voskoboinyk 			ieee80211_getqos(wh)[1] &= ~IEEE80211_QOS_MC;
202091216c71SAdrian Chadd #endif
202168e8e04eSSam Leffler 		*(uint16_t *)&whf->i_seq[0] |= htole16(
202268e8e04eSSam Leffler 			(fragno & IEEE80211_SEQ_FRAG_MASK) <<
202368e8e04eSSam Leffler 				IEEE80211_SEQ_FRAG_SHIFT);
202468e8e04eSSam Leffler 		fragno++;
202568e8e04eSSam Leffler 
202668e8e04eSSam Leffler 		payload = fragsize - totalhdrsize;
202768e8e04eSSam Leffler 		/* NB: destination is known to be contiguous */
20288d46c25dSAdrian Chadd 
20298d46c25dSAdrian Chadd 		m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrspace);
20308d46c25dSAdrian Chadd 		m->m_len = hdrspace + payload;
20318d46c25dSAdrian Chadd 		m->m_pkthdr.len = hdrspace + payload;
203268e8e04eSSam Leffler 		m->m_flags |= M_FRAG;
203368e8e04eSSam Leffler 
203468e8e04eSSam Leffler 		/* chain up the fragment */
203568e8e04eSSam Leffler 		prev->m_nextpkt = m;
203668e8e04eSSam Leffler 		prev = m;
203768e8e04eSSam Leffler 
203868e8e04eSSam Leffler 		/* deduct fragment just formed */
203968e8e04eSSam Leffler 		remainder -= payload;
204068e8e04eSSam Leffler 		off += payload;
204168e8e04eSSam Leffler 	} while (remainder != 0);
204251cec121SWeongyo Jeong 
204351cec121SWeongyo Jeong 	/* set the last fragment */
204451cec121SWeongyo Jeong 	m->m_flags |= M_LASTFRAG;
204568e8e04eSSam Leffler 	whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
204668e8e04eSSam Leffler 
204768e8e04eSSam Leffler 	/* strip first mbuf now that everything has been copied */
204868e8e04eSSam Leffler 	m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
204968e8e04eSSam Leffler 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
205068e8e04eSSam Leffler 
2051b032f27cSSam Leffler 	vap->iv_stats.is_tx_fragframes++;
2052b032f27cSSam Leffler 	vap->iv_stats.is_tx_frags += fragno-1;
205368e8e04eSSam Leffler 
205468e8e04eSSam Leffler 	return 1;
205568e8e04eSSam Leffler bad:
205668e8e04eSSam Leffler 	/* reclaim fragments but leave original frame for caller to free */
2057d07be335SAdrian Chadd 	ieee80211_free_mbuf(m0->m_nextpkt);
205868e8e04eSSam Leffler 	m0->m_nextpkt = NULL;
205968e8e04eSSam Leffler 	return 0;
206068e8e04eSSam Leffler }
206168e8e04eSSam Leffler 
206268e8e04eSSam Leffler /*
20631a1e1d21SSam Leffler  * Add a supported rates element id to a frame.
20641a1e1d21SSam Leffler  */
206559aa14a9SRui Paulo uint8_t *
206668e8e04eSSam Leffler ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
20671a1e1d21SSam Leffler {
20681a1e1d21SSam Leffler 	int nrates;
20691a1e1d21SSam Leffler 
20701a1e1d21SSam Leffler 	*frm++ = IEEE80211_ELEMID_RATES;
20711a1e1d21SSam Leffler 	nrates = rs->rs_nrates;
20721a1e1d21SSam Leffler 	if (nrates > IEEE80211_RATE_SIZE)
20731a1e1d21SSam Leffler 		nrates = IEEE80211_RATE_SIZE;
20741a1e1d21SSam Leffler 	*frm++ = nrates;
20751a1e1d21SSam Leffler 	memcpy(frm, rs->rs_rates, nrates);
20761a1e1d21SSam Leffler 	return frm + nrates;
20771a1e1d21SSam Leffler }
20781a1e1d21SSam Leffler 
20791a1e1d21SSam Leffler /*
20801a1e1d21SSam Leffler  * Add an extended supported rates element id to a frame.
20811a1e1d21SSam Leffler  */
208259aa14a9SRui Paulo uint8_t *
208368e8e04eSSam Leffler ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
20841a1e1d21SSam Leffler {
20851a1e1d21SSam Leffler 	/*
20861a1e1d21SSam Leffler 	 * Add an extended supported rates element if operating in 11g mode.
20871a1e1d21SSam Leffler 	 */
20881a1e1d21SSam Leffler 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
20891a1e1d21SSam Leffler 		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
20901a1e1d21SSam Leffler 		*frm++ = IEEE80211_ELEMID_XRATES;
20911a1e1d21SSam Leffler 		*frm++ = nrates;
20921a1e1d21SSam Leffler 		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
20931a1e1d21SSam Leffler 		frm += nrates;
20941a1e1d21SSam Leffler 	}
20951a1e1d21SSam Leffler 	return frm;
20961a1e1d21SSam Leffler }
20971a1e1d21SSam Leffler 
20981a1e1d21SSam Leffler /*
2099b032f27cSSam Leffler  * Add an ssid element to a frame.
21001a1e1d21SSam Leffler  */
2101de981aecSAdrian Chadd uint8_t *
210268e8e04eSSam Leffler ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
21031a1e1d21SSam Leffler {
21041a1e1d21SSam Leffler 	*frm++ = IEEE80211_ELEMID_SSID;
21051a1e1d21SSam Leffler 	*frm++ = len;
21061a1e1d21SSam Leffler 	memcpy(frm, ssid, len);
21071a1e1d21SSam Leffler 	return frm + len;
21081a1e1d21SSam Leffler }
21091a1e1d21SSam Leffler 
21108a1b9b6aSSam Leffler /*
21118a1b9b6aSSam Leffler  * Add an erp element to a frame.
21128a1b9b6aSSam Leffler  */
211368e8e04eSSam Leffler static uint8_t *
2114f1481c8dSAdrian Chadd ieee80211_add_erp(uint8_t *frm, struct ieee80211vap *vap)
21151a1e1d21SSam Leffler {
2116f1481c8dSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
211768e8e04eSSam Leffler 	uint8_t erp;
21181a1e1d21SSam Leffler 
21198a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_ERP;
21208a1b9b6aSSam Leffler 	*frm++ = 1;
21218a1b9b6aSSam Leffler 	erp = 0;
2122f1481c8dSAdrian Chadd 
2123f1481c8dSAdrian Chadd 	/*
2124f1481c8dSAdrian Chadd 	 * TODO:  This uses the global flags for now because
2125f1481c8dSAdrian Chadd 	 * the per-VAP flags are fine for per-VAP, but don't
2126f1481c8dSAdrian Chadd 	 * take into account which VAPs share the same channel
2127f1481c8dSAdrian Chadd 	 * and which are on different channels.
2128f1481c8dSAdrian Chadd 	 *
2129f1481c8dSAdrian Chadd 	 * ERP and HT/VHT protection mode is a function of
2130f1481c8dSAdrian Chadd 	 * how many stations are on a channel, not specifically
2131f1481c8dSAdrian Chadd 	 * the VAP or global.  But, until we grow that status,
2132f1481c8dSAdrian Chadd 	 * the global flag will have to do.
2133f1481c8dSAdrian Chadd 	 */
2134f1481c8dSAdrian Chadd 	if (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR)
21358a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_NON_ERP_PRESENT;
2136f1481c8dSAdrian Chadd 
2137f1481c8dSAdrian Chadd 	/*
2138f1481c8dSAdrian Chadd 	 * TODO: same as above; these should be based not
2139f1481c8dSAdrian Chadd 	 * on the vap or ic flags, but instead on a combination
2140f1481c8dSAdrian Chadd 	 * of per-VAP and channels.
2141f1481c8dSAdrian Chadd 	 */
21428a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_USEPROT)
21438a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_USE_PROTECTION;
21448a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_USEBARKER)
21458a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_LONG_PREAMBLE;
21468a1b9b6aSSam Leffler 	*frm++ = erp;
21478a1b9b6aSSam Leffler 	return frm;
21481a1e1d21SSam Leffler }
21491a1e1d21SSam Leffler 
21508a1b9b6aSSam Leffler /*
2151b032f27cSSam Leffler  * Add a CFParams element to a frame.
21528a1b9b6aSSam Leffler  */
215368e8e04eSSam Leffler static uint8_t *
2154b032f27cSSam Leffler ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
21558a1b9b6aSSam Leffler {
2156b032f27cSSam Leffler #define	ADDSHORT(frm, v) do {	\
215731021a2bSAndriy Voskoboinyk 	le16enc(frm, v);	\
2158b032f27cSSam Leffler 	frm += 2;		\
2159b032f27cSSam Leffler } while (0)
2160b032f27cSSam Leffler 	*frm++ = IEEE80211_ELEMID_CFPARMS;
2161b032f27cSSam Leffler 	*frm++ = 6;
2162b032f27cSSam Leffler 	*frm++ = 0;		/* CFP count */
2163b032f27cSSam Leffler 	*frm++ = 2;		/* CFP period */
2164b032f27cSSam Leffler 	ADDSHORT(frm, 0);	/* CFP MaxDuration (TU) */
2165b032f27cSSam Leffler 	ADDSHORT(frm, 0);	/* CFP CurRemaining (TU) */
21668a1b9b6aSSam Leffler 	return frm;
2167b032f27cSSam Leffler #undef ADDSHORT
2168b032f27cSSam Leffler }
2169b032f27cSSam Leffler 
2170b032f27cSSam Leffler static __inline uint8_t *
2171b032f27cSSam Leffler add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
2172b032f27cSSam Leffler {
2173b032f27cSSam Leffler 	memcpy(frm, ie->ie_data, ie->ie_len);
2174b032f27cSSam Leffler 	return frm + ie->ie_len;
2175b032f27cSSam Leffler }
2176b032f27cSSam Leffler 
2177b032f27cSSam Leffler static __inline uint8_t *
2178b032f27cSSam Leffler add_ie(uint8_t *frm, const uint8_t *ie)
2179b032f27cSSam Leffler {
2180b032f27cSSam Leffler 	memcpy(frm, ie, 2 + ie[1]);
2181b032f27cSSam Leffler 	return frm + 2 + ie[1];
21828a1b9b6aSSam Leffler }
21838a1b9b6aSSam Leffler 
21848a1b9b6aSSam Leffler #define	WME_OUI_BYTES		0x00, 0x50, 0xf2
21858a1b9b6aSSam Leffler /*
21868a1b9b6aSSam Leffler  * Add a WME information element to a frame.
21878a1b9b6aSSam Leffler  */
21884eadefc2SAdrian Chadd uint8_t *
21898379e8dbSAdrian Chadd ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme,
21908379e8dbSAdrian Chadd     struct ieee80211_node *ni)
21918a1b9b6aSSam Leffler {
21928379e8dbSAdrian Chadd 	static const uint8_t oui[4] = { WME_OUI_BYTES, WME_OUI_TYPE };
21938379e8dbSAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
21948379e8dbSAdrian Chadd 
21958379e8dbSAdrian Chadd 	*frm++ = IEEE80211_ELEMID_VENDOR;
21968379e8dbSAdrian Chadd 	*frm++ = sizeof(struct ieee80211_wme_info) - 2;
21978379e8dbSAdrian Chadd 	memcpy(frm, oui, sizeof(oui));
21988379e8dbSAdrian Chadd 	frm += sizeof(oui);
21998379e8dbSAdrian Chadd 	*frm++ = WME_INFO_OUI_SUBTYPE;
22008379e8dbSAdrian Chadd 	*frm++ = WME_VERSION;
22018379e8dbSAdrian Chadd 
22028379e8dbSAdrian Chadd 	/* QoS info field depends upon operating mode */
22038379e8dbSAdrian Chadd 	switch (vap->iv_opmode) {
22048379e8dbSAdrian Chadd 	case IEEE80211_M_HOSTAP:
22058379e8dbSAdrian Chadd 		*frm = wme->wme_bssChanParams.cap_info;
22068379e8dbSAdrian Chadd 		if (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD)
22078379e8dbSAdrian Chadd 			*frm |= WME_CAPINFO_UAPSD_EN;
22088379e8dbSAdrian Chadd 		frm++;
22098379e8dbSAdrian Chadd 		break;
22108379e8dbSAdrian Chadd 	case IEEE80211_M_STA:
22118379e8dbSAdrian Chadd 		/*
22128379e8dbSAdrian Chadd 		 * NB: UAPSD drivers must set this up in their
22138379e8dbSAdrian Chadd 		 * VAP creation method.
22148379e8dbSAdrian Chadd 		 */
22158379e8dbSAdrian Chadd 		*frm++ = vap->iv_uapsdinfo;
22168379e8dbSAdrian Chadd 		break;
22178379e8dbSAdrian Chadd 	default:
22188379e8dbSAdrian Chadd 		*frm++ = 0;
22198379e8dbSAdrian Chadd 		break;
22208379e8dbSAdrian Chadd 	}
22218379e8dbSAdrian Chadd 
22228379e8dbSAdrian Chadd 	return frm;
22238a1b9b6aSSam Leffler }
22248a1b9b6aSSam Leffler 
22258a1b9b6aSSam Leffler /*
22268a1b9b6aSSam Leffler  * Add a WME parameters element to a frame.
22278a1b9b6aSSam Leffler  */
222868e8e04eSSam Leffler static uint8_t *
22298379e8dbSAdrian Chadd ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme,
22308379e8dbSAdrian Chadd     int uapsd_enable)
22318a1b9b6aSSam Leffler {
22328a1b9b6aSSam Leffler #define	ADDSHORT(frm, v) do {	\
223331021a2bSAndriy Voskoboinyk 	le16enc(frm, v);	\
22348a1b9b6aSSam Leffler 	frm += 2;		\
22358a1b9b6aSSam Leffler } while (0)
22368a1b9b6aSSam Leffler 	/* NB: this works 'cuz a param has an info at the front */
22378a1b9b6aSSam Leffler 	static const struct ieee80211_wme_info param = {
22388a1b9b6aSSam Leffler 		.wme_id		= IEEE80211_ELEMID_VENDOR,
22398a1b9b6aSSam Leffler 		.wme_len	= sizeof(struct ieee80211_wme_param) - 2,
22408a1b9b6aSSam Leffler 		.wme_oui	= { WME_OUI_BYTES },
22418a1b9b6aSSam Leffler 		.wme_type	= WME_OUI_TYPE,
22428a1b9b6aSSam Leffler 		.wme_subtype	= WME_PARAM_OUI_SUBTYPE,
22438a1b9b6aSSam Leffler 		.wme_version	= WME_VERSION,
22448a1b9b6aSSam Leffler 	};
22458a1b9b6aSSam Leffler 	int i;
22468a1b9b6aSSam Leffler 
22478a1b9b6aSSam Leffler 	memcpy(frm, &param, sizeof(param));
22488a1b9b6aSSam Leffler 	frm += __offsetof(struct ieee80211_wme_info, wme_info);
22498379e8dbSAdrian Chadd 	*frm = wme->wme_bssChanParams.cap_info;	/* AC info */
22508379e8dbSAdrian Chadd 	if (uapsd_enable)
22518379e8dbSAdrian Chadd 		*frm |= WME_CAPINFO_UAPSD_EN;
22528379e8dbSAdrian Chadd 	frm++;
22538a1b9b6aSSam Leffler 	*frm++ = 0;					/* reserved field */
22548379e8dbSAdrian Chadd 	/* XXX TODO - U-APSD bits - SP, flags below */
22558a1b9b6aSSam Leffler 	for (i = 0; i < WME_NUM_AC; i++) {
22568a1b9b6aSSam Leffler 		const struct wmeParams *ac =
22578a1b9b6aSSam Leffler 		       &wme->wme_bssChanParams.cap_wmeParams[i];
2258fe5ebb23SBjoern A. Zeeb 		*frm++ = _IEEE80211_SHIFTMASK(i, WME_PARAM_ACI)
2259fe5ebb23SBjoern A. Zeeb 		       | _IEEE80211_SHIFTMASK(ac->wmep_acm, WME_PARAM_ACM)
2260fe5ebb23SBjoern A. Zeeb 		       | _IEEE80211_SHIFTMASK(ac->wmep_aifsn, WME_PARAM_AIFSN)
22618a1b9b6aSSam Leffler 		       ;
2262fe5ebb23SBjoern A. Zeeb 		*frm++ = _IEEE80211_SHIFTMASK(ac->wmep_logcwmax,
2263fe5ebb23SBjoern A. Zeeb 			    WME_PARAM_LOGCWMAX)
2264fe5ebb23SBjoern A. Zeeb 		       | _IEEE80211_SHIFTMASK(ac->wmep_logcwmin,
2265fe5ebb23SBjoern A. Zeeb 			    WME_PARAM_LOGCWMIN)
22668a1b9b6aSSam Leffler 		       ;
22678a1b9b6aSSam Leffler 		ADDSHORT(frm, ac->wmep_txopLimit);
22688a1b9b6aSSam Leffler 	}
22698a1b9b6aSSam Leffler 	return frm;
22708a1b9b6aSSam Leffler #undef ADDSHORT
22718a1b9b6aSSam Leffler }
22728a1b9b6aSSam Leffler #undef WME_OUI_BYTES
22738a1b9b6aSSam Leffler 
22740a915fadSSam Leffler /*
2275b032f27cSSam Leffler  * Add an 11h Power Constraint element to a frame.
2276b032f27cSSam Leffler  */
2277b032f27cSSam Leffler static uint8_t *
2278b032f27cSSam Leffler ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
2279b032f27cSSam Leffler {
2280b032f27cSSam Leffler 	const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
2281b032f27cSSam Leffler 	/* XXX per-vap tx power limit? */
2282b032f27cSSam Leffler 	int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
2283b032f27cSSam Leffler 
2284b032f27cSSam Leffler 	frm[0] = IEEE80211_ELEMID_PWRCNSTR;
2285b032f27cSSam Leffler 	frm[1] = 1;
2286b032f27cSSam Leffler 	frm[2] = c->ic_maxregpower > limit ?  c->ic_maxregpower - limit : 0;
2287b032f27cSSam Leffler 	return frm + 3;
2288b032f27cSSam Leffler }
2289b032f27cSSam Leffler 
2290b032f27cSSam Leffler /*
2291b032f27cSSam Leffler  * Add an 11h Power Capability element to a frame.
2292b032f27cSSam Leffler  */
2293b032f27cSSam Leffler static uint8_t *
2294b032f27cSSam Leffler ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
2295b032f27cSSam Leffler {
2296b032f27cSSam Leffler 	frm[0] = IEEE80211_ELEMID_PWRCAP;
2297b032f27cSSam Leffler 	frm[1] = 2;
2298b032f27cSSam Leffler 	frm[2] = c->ic_minpower;
2299b032f27cSSam Leffler 	frm[3] = c->ic_maxpower;
2300b032f27cSSam Leffler 	return frm + 4;
2301b032f27cSSam Leffler }
2302b032f27cSSam Leffler 
2303b032f27cSSam Leffler /*
2304b032f27cSSam Leffler  * Add an 11h Supported Channels element to a frame.
2305b032f27cSSam Leffler  */
2306b032f27cSSam Leffler static uint8_t *
2307b032f27cSSam Leffler ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
2308b032f27cSSam Leffler {
2309b032f27cSSam Leffler 	static const int ielen = 26;
2310b032f27cSSam Leffler 
2311b032f27cSSam Leffler 	frm[0] = IEEE80211_ELEMID_SUPPCHAN;
2312b032f27cSSam Leffler 	frm[1] = ielen;
2313b032f27cSSam Leffler 	/* XXX not correct */
2314b032f27cSSam Leffler 	memcpy(frm+2, ic->ic_chan_avail, ielen);
2315b032f27cSSam Leffler 	return frm + 2 + ielen;
2316b032f27cSSam Leffler }
2317b032f27cSSam Leffler 
2318b032f27cSSam Leffler /*
231932b0e64bSAdrian Chadd  * Add an 11h Quiet time element to a frame.
232032b0e64bSAdrian Chadd  */
232132b0e64bSAdrian Chadd static uint8_t *
2322ce4552cdSAdrian Chadd ieee80211_add_quiet(uint8_t *frm, struct ieee80211vap *vap, int update)
232332b0e64bSAdrian Chadd {
232432b0e64bSAdrian Chadd 	struct ieee80211_quiet_ie *quiet = (struct ieee80211_quiet_ie *) frm;
232532b0e64bSAdrian Chadd 
232632b0e64bSAdrian Chadd 	quiet->quiet_ie = IEEE80211_ELEMID_QUIET;
232732b0e64bSAdrian Chadd 	quiet->len = 6;
2328ce4552cdSAdrian Chadd 
2329ce4552cdSAdrian Chadd 	/*
2330ce4552cdSAdrian Chadd 	 * Only update every beacon interval - otherwise probe responses
2331ce4552cdSAdrian Chadd 	 * would update the quiet count value.
2332ce4552cdSAdrian Chadd 	 */
2333ce4552cdSAdrian Chadd 	if (update) {
233432b0e64bSAdrian Chadd 		if (vap->iv_quiet_count_value == 1)
233532b0e64bSAdrian Chadd 			vap->iv_quiet_count_value = vap->iv_quiet_count;
233632b0e64bSAdrian Chadd 		else if (vap->iv_quiet_count_value > 1)
233732b0e64bSAdrian Chadd 			vap->iv_quiet_count_value--;
2338ce4552cdSAdrian Chadd 	}
233932b0e64bSAdrian Chadd 
234032b0e64bSAdrian Chadd 	if (vap->iv_quiet_count_value == 0) {
234132b0e64bSAdrian Chadd 		/* value 0 is reserved as per 802.11h standerd */
234232b0e64bSAdrian Chadd 		vap->iv_quiet_count_value = 1;
234332b0e64bSAdrian Chadd 	}
234432b0e64bSAdrian Chadd 
234532b0e64bSAdrian Chadd 	quiet->tbttcount = vap->iv_quiet_count_value;
234632b0e64bSAdrian Chadd 	quiet->period = vap->iv_quiet_period;
234732b0e64bSAdrian Chadd 	quiet->duration = htole16(vap->iv_quiet_duration);
234832b0e64bSAdrian Chadd 	quiet->offset = htole16(vap->iv_quiet_offset);
234932b0e64bSAdrian Chadd 	return frm + sizeof(*quiet);
235032b0e64bSAdrian Chadd }
235132b0e64bSAdrian Chadd 
235232b0e64bSAdrian Chadd /*
2353b032f27cSSam Leffler  * Add an 11h Channel Switch Announcement element to a frame.
2354b032f27cSSam Leffler  * Note that we use the per-vap CSA count to adjust the global
2355b032f27cSSam Leffler  * counter so we can use this routine to form probe response
2356b032f27cSSam Leffler  * frames and get the current count.
2357b032f27cSSam Leffler  */
2358b032f27cSSam Leffler static uint8_t *
2359b032f27cSSam Leffler ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
2360b032f27cSSam Leffler {
2361b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2362b032f27cSSam Leffler 	struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
2363b032f27cSSam Leffler 
2364c70761e6SSam Leffler 	csa->csa_ie = IEEE80211_ELEMID_CSA;
2365b032f27cSSam Leffler 	csa->csa_len = 3;
2366b032f27cSSam Leffler 	csa->csa_mode = 1;		/* XXX force quiet on channel */
2367b032f27cSSam Leffler 	csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
2368b032f27cSSam Leffler 	csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
2369b032f27cSSam Leffler 	return frm + sizeof(*csa);
2370b032f27cSSam Leffler }
2371b032f27cSSam Leffler 
2372b032f27cSSam Leffler /*
2373b032f27cSSam Leffler  * Add an 11h country information element to a frame.
2374b032f27cSSam Leffler  */
2375b032f27cSSam Leffler static uint8_t *
2376b032f27cSSam Leffler ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
2377b032f27cSSam Leffler {
2378b032f27cSSam Leffler 
2379b032f27cSSam Leffler 	if (ic->ic_countryie == NULL ||
2380b032f27cSSam Leffler 	    ic->ic_countryie_chan != ic->ic_bsschan) {
2381b032f27cSSam Leffler 		/*
2382b032f27cSSam Leffler 		 * Handle lazy construction of ie.  This is done on
2383b032f27cSSam Leffler 		 * first use and after a channel change that requires
2384b032f27cSSam Leffler 		 * re-calculation.
2385b032f27cSSam Leffler 		 */
2386b032f27cSSam Leffler 		if (ic->ic_countryie != NULL)
2387b9b53389SAdrian Chadd 			IEEE80211_FREE(ic->ic_countryie, M_80211_NODE_IE);
2388b032f27cSSam Leffler 		ic->ic_countryie = ieee80211_alloc_countryie(ic);
2389b032f27cSSam Leffler 		if (ic->ic_countryie == NULL)
2390b032f27cSSam Leffler 			return frm;
2391b032f27cSSam Leffler 		ic->ic_countryie_chan = ic->ic_bsschan;
2392b032f27cSSam Leffler 	}
2393b032f27cSSam Leffler 	return add_appie(frm, ic->ic_countryie);
2394b032f27cSSam Leffler }
2395b032f27cSSam Leffler 
2396d8df5f3dSRui Paulo uint8_t *
2397d8df5f3dSRui Paulo ieee80211_add_wpa(uint8_t *frm, const struct ieee80211vap *vap)
2398d8df5f3dSRui Paulo {
2399d8df5f3dSRui Paulo 	if (vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL)
2400d8df5f3dSRui Paulo 		return (add_ie(frm, vap->iv_wpa_ie));
2401d8df5f3dSRui Paulo 	else {
2402d8df5f3dSRui Paulo 		/* XXX else complain? */
2403d8df5f3dSRui Paulo 		return (frm);
2404d8df5f3dSRui Paulo 	}
2405d8df5f3dSRui Paulo }
2406d8df5f3dSRui Paulo 
2407d8df5f3dSRui Paulo uint8_t *
2408d8df5f3dSRui Paulo ieee80211_add_rsn(uint8_t *frm, const struct ieee80211vap *vap)
2409d8df5f3dSRui Paulo {
2410d8df5f3dSRui Paulo 	if (vap->iv_flags & IEEE80211_F_WPA2 && vap->iv_rsn_ie != NULL)
2411d8df5f3dSRui Paulo 		return (add_ie(frm, vap->iv_rsn_ie));
2412d8df5f3dSRui Paulo 	else {
2413d8df5f3dSRui Paulo 		/* XXX else complain? */
2414d8df5f3dSRui Paulo 		return (frm);
2415d8df5f3dSRui Paulo 	}
2416d8df5f3dSRui Paulo }
2417d8df5f3dSRui Paulo 
2418d8df5f3dSRui Paulo uint8_t *
2419d8df5f3dSRui Paulo ieee80211_add_qos(uint8_t *frm, const struct ieee80211_node *ni)
2420d8df5f3dSRui Paulo {
2421d8df5f3dSRui Paulo 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
2422d8df5f3dSRui Paulo 		*frm++ = IEEE80211_ELEMID_QOS;
2423d8df5f3dSRui Paulo 		*frm++ = 1;
2424d8df5f3dSRui Paulo 		*frm++ = 0;
2425d8df5f3dSRui Paulo 	}
2426d8df5f3dSRui Paulo 
2427d8df5f3dSRui Paulo 	return (frm);
2428d8df5f3dSRui Paulo }
2429d8df5f3dSRui Paulo 
2430b032f27cSSam Leffler /*
2431c338cf2cSBjoern A. Zeeb  * ieee80211_send_probereq(): send a probe request frame with the specified ssid
2432c338cf2cSBjoern A. Zeeb  * and any optional information element data;  some helper functions as FW based
2433c338cf2cSBjoern A. Zeeb  * HW scans need some of that information passed too.
2434af8418dcSSam Leffler  */
2435c338cf2cSBjoern A. Zeeb static uint32_t
2436c338cf2cSBjoern A. Zeeb ieee80211_probereq_ie_len(struct ieee80211vap *vap, struct ieee80211com *ic)
2437af8418dcSSam Leffler {
243841b3c790SSam Leffler 	const struct ieee80211_rateset *rs;
2439af8418dcSSam Leffler 
2440c338cf2cSBjoern A. Zeeb 	rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2441af8418dcSSam Leffler 
2442af8418dcSSam Leffler 	/*
2443af8418dcSSam Leffler 	 * prreq frame format
2444af8418dcSSam Leffler 	 *	[tlv] ssid
2445af8418dcSSam Leffler 	 *	[tlv] supported rates
2446c338cf2cSBjoern A. Zeeb 	 *	[tlv] extended supported rates (if needed)
2447f8a67728SAdrian Chadd 	 *	[tlv] HT cap (optional)
244851172f62SAdrian Chadd 	 *	[tlv] VHT cap (optional)
2449b032f27cSSam Leffler 	 *	[tlv] WPA (optional)
2450af8418dcSSam Leffler 	 *	[tlv] user-specified ie's
2451af8418dcSSam Leffler 	 */
2452c338cf2cSBjoern A. Zeeb 	return ( 2 + IEEE80211_NWID_LEN
2453af8418dcSSam Leffler 	       + 2 + IEEE80211_RATE_SIZE
2454c338cf2cSBjoern A. Zeeb 	       + ((rs->rs_nrates > IEEE80211_RATE_SIZE) ?
2455c338cf2cSBjoern A. Zeeb 	           2 + (rs->rs_nrates - IEEE80211_RATE_SIZE) : 0)
2456c338cf2cSBjoern A. Zeeb 	       + (((vap->iv_opmode == IEEE80211_M_IBSS) &&
2457c338cf2cSBjoern A. Zeeb 		    (vap->iv_flags_ht & IEEE80211_FHT_HT)) ?
2458c338cf2cSBjoern A. Zeeb 	                sizeof(struct ieee80211_ie_htcap) : 0)
2459c338cf2cSBjoern A. Zeeb #ifdef notyet
246051172f62SAdrian Chadd 	       + sizeof(struct ieee80211_ie_htinfo)	/* XXX not needed? */
2461c338cf2cSBjoern A. Zeeb 	       + sizeof(struct ieee80211_ie_vhtcap)
2462c338cf2cSBjoern A. Zeeb #endif
2463c338cf2cSBjoern A. Zeeb 	       + ((vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL) ?
2464c338cf2cSBjoern A. Zeeb 	           vap->iv_wpa_ie[1] : 0)
2465b032f27cSSam Leffler 	       + (vap->iv_appie_probereq != NULL ?
2466b032f27cSSam Leffler 		   vap->iv_appie_probereq->ie_len : 0)
2467af8418dcSSam Leffler 	);
2468af8418dcSSam Leffler }
2469af8418dcSSam Leffler 
2470c338cf2cSBjoern A. Zeeb int
2471c338cf2cSBjoern A. Zeeb ieee80211_probereq_ie(struct ieee80211vap *vap, struct ieee80211com *ic,
2472c338cf2cSBjoern A. Zeeb     uint8_t **frmp, uint32_t *frmlen, const uint8_t *ssid, size_t ssidlen,
2473c338cf2cSBjoern A. Zeeb     bool alloc)
2474c338cf2cSBjoern A. Zeeb {
2475c338cf2cSBjoern A. Zeeb 	const struct ieee80211_rateset *rs;
2476c338cf2cSBjoern A. Zeeb 	uint8_t	*frm;
2477c338cf2cSBjoern A. Zeeb 	uint32_t len;
2478c338cf2cSBjoern A. Zeeb 
2479c338cf2cSBjoern A. Zeeb 	if (!alloc && (frmp == NULL || frmlen == NULL))
2480c338cf2cSBjoern A. Zeeb 		return (EINVAL);
2481c338cf2cSBjoern A. Zeeb 
2482c338cf2cSBjoern A. Zeeb 	len = ieee80211_probereq_ie_len(vap, ic);
2483c338cf2cSBjoern A. Zeeb 	if (!alloc && len > *frmlen)
2484c338cf2cSBjoern A. Zeeb 		return (ENOBUFS);
2485c338cf2cSBjoern A. Zeeb 
2486243b9597SBjoern A. Zeeb 	/* For HW scans we usually do not pass in the SSID as IE. */
2487243b9597SBjoern A. Zeeb 	if (ssidlen == -1)
2488243b9597SBjoern A. Zeeb 		len -= (2 + IEEE80211_NWID_LEN);
2489243b9597SBjoern A. Zeeb 
2490c338cf2cSBjoern A. Zeeb 	if (alloc) {
2491bd29f817SBjoern A. Zeeb 		frm = IEEE80211_MALLOC(len, M_80211_VAP,
2492bd29f817SBjoern A. Zeeb 		    IEEE80211_M_WAITOK | IEEE80211_M_ZERO);
2493c338cf2cSBjoern A. Zeeb 		*frmp = frm;
2494c338cf2cSBjoern A. Zeeb 		*frmlen = len;
2495c338cf2cSBjoern A. Zeeb 	} else
2496c338cf2cSBjoern A. Zeeb 		frm = *frmp;
2497c338cf2cSBjoern A. Zeeb 
2498243b9597SBjoern A. Zeeb 	if (ssidlen != -1)
2499af8418dcSSam Leffler 		frm = ieee80211_add_ssid(frm, ssid, ssidlen);
250041b3c790SSam Leffler 	rs = ieee80211_get_suprates(ic, ic->ic_curchan);
250141b3c790SSam Leffler 	frm = ieee80211_add_rates(frm, rs);
2502b032f27cSSam Leffler 	frm = ieee80211_add_xrates(frm, rs);
2503f8a67728SAdrian Chadd 
2504f8a67728SAdrian Chadd 	/*
2505f8a67728SAdrian Chadd 	 * Note: we can't use bss; we don't have one yet.
2506f8a67728SAdrian Chadd 	 *
2507f8a67728SAdrian Chadd 	 * So, we should announce our capabilities
2508f8a67728SAdrian Chadd 	 * in this channel mode (2g/5g), not the
2509f8a67728SAdrian Chadd 	 * channel details itself.
2510f8a67728SAdrian Chadd 	 */
2511f8a67728SAdrian Chadd 	if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
2512f8a67728SAdrian Chadd 	    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
2513f8a67728SAdrian Chadd 		struct ieee80211_channel *c;
2514f8a67728SAdrian Chadd 
2515f8a67728SAdrian Chadd 		/*
2516f8a67728SAdrian Chadd 		 * Get the HT channel that we should try upgrading to.
2517f8a67728SAdrian Chadd 		 * If we can do 40MHz then this'll upgrade it appropriately.
2518f8a67728SAdrian Chadd 		 */
2519f8a67728SAdrian Chadd 		c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
2520f8a67728SAdrian Chadd 		    vap->iv_flags_ht);
2521f8a67728SAdrian Chadd 		frm = ieee80211_add_htcap_ch(frm, vap, c);
2522f8a67728SAdrian Chadd 	}
2523f8a67728SAdrian Chadd 
252451172f62SAdrian Chadd 	/*
252551172f62SAdrian Chadd 	 * XXX TODO: need to figure out what/how to update the
252651172f62SAdrian Chadd 	 * VHT channel.
252751172f62SAdrian Chadd 	 */
2528c338cf2cSBjoern A. Zeeb #ifdef notyet
2529c338cf2cSBjoern A. Zeeb 	if (vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
253051172f62SAdrian Chadd 		struct ieee80211_channel *c;
253151172f62SAdrian Chadd 
253251172f62SAdrian Chadd 		c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
253351172f62SAdrian Chadd 		    vap->iv_flags_ht);
253451172f62SAdrian Chadd 		c = ieee80211_vht_adjust_channel(ic, c, vap->iv_flags_vht);
253551172f62SAdrian Chadd 		frm = ieee80211_add_vhtcap_ch(frm, vap, c);
253651172f62SAdrian Chadd 	}
253751172f62SAdrian Chadd #endif
253851172f62SAdrian Chadd 
2539d8df5f3dSRui Paulo 	frm = ieee80211_add_wpa(frm, vap);
2540b032f27cSSam Leffler 	if (vap->iv_appie_probereq != NULL)
2541b032f27cSSam Leffler 		frm = add_appie(frm, vap->iv_appie_probereq);
2542af8418dcSSam Leffler 
2543c338cf2cSBjoern A. Zeeb 	if (!alloc) {
2544c338cf2cSBjoern A. Zeeb 		*frmp = frm;
2545c338cf2cSBjoern A. Zeeb 		*frmlen = len;
2546c338cf2cSBjoern A. Zeeb 	}
2547c338cf2cSBjoern A. Zeeb 
2548c338cf2cSBjoern A. Zeeb 	return (0);
2549c338cf2cSBjoern A. Zeeb }
2550c338cf2cSBjoern A. Zeeb 
2551c338cf2cSBjoern A. Zeeb int
2552c338cf2cSBjoern A. Zeeb ieee80211_send_probereq(struct ieee80211_node *ni,
2553c338cf2cSBjoern A. Zeeb 	const uint8_t sa[IEEE80211_ADDR_LEN],
2554c338cf2cSBjoern A. Zeeb 	const uint8_t da[IEEE80211_ADDR_LEN],
2555c338cf2cSBjoern A. Zeeb 	const uint8_t bssid[IEEE80211_ADDR_LEN],
2556c338cf2cSBjoern A. Zeeb 	const uint8_t *ssid, size_t ssidlen)
2557c338cf2cSBjoern A. Zeeb {
2558c338cf2cSBjoern A. Zeeb 	struct ieee80211vap *vap = ni->ni_vap;
2559c338cf2cSBjoern A. Zeeb 	struct ieee80211com *ic = ni->ni_ic;
2560c338cf2cSBjoern A. Zeeb 	struct ieee80211_node *bss;
2561c338cf2cSBjoern A. Zeeb 	const struct ieee80211_txparam *tp;
2562c338cf2cSBjoern A. Zeeb 	struct ieee80211_bpf_params params;
2563c338cf2cSBjoern A. Zeeb 	struct mbuf *m;
2564c338cf2cSBjoern A. Zeeb 	uint8_t *frm;
2565c338cf2cSBjoern A. Zeeb 	uint32_t frmlen;
2566c338cf2cSBjoern A. Zeeb 	int ret;
2567c338cf2cSBjoern A. Zeeb 
2568c338cf2cSBjoern A. Zeeb 	bss = ieee80211_ref_node(vap->iv_bss);
2569c338cf2cSBjoern A. Zeeb 
2570c338cf2cSBjoern A. Zeeb 	if (vap->iv_state == IEEE80211_S_CAC) {
2571c338cf2cSBjoern A. Zeeb 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
2572c338cf2cSBjoern A. Zeeb 		    "block %s frame in CAC state", "probe request");
2573c338cf2cSBjoern A. Zeeb 		vap->iv_stats.is_tx_badstate++;
2574c338cf2cSBjoern A. Zeeb 		ieee80211_free_node(bss);
2575c338cf2cSBjoern A. Zeeb 		return EIO;		/* XXX */
2576c338cf2cSBjoern A. Zeeb 	}
2577c338cf2cSBjoern A. Zeeb 
2578c338cf2cSBjoern A. Zeeb 	/*
2579c338cf2cSBjoern A. Zeeb 	 * Hold a reference on the node so it doesn't go away until after
2580c338cf2cSBjoern A. Zeeb 	 * the xmit is complete all the way in the driver.  On error we
2581c338cf2cSBjoern A. Zeeb 	 * will remove our reference.
2582c338cf2cSBjoern A. Zeeb 	 */
2583c338cf2cSBjoern A. Zeeb 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2584c338cf2cSBjoern A. Zeeb 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2585c338cf2cSBjoern A. Zeeb 		__func__, __LINE__,
2586c338cf2cSBjoern A. Zeeb 		ni, ether_sprintf(ni->ni_macaddr),
2587c338cf2cSBjoern A. Zeeb 		ieee80211_node_refcnt(ni)+1);
2588c338cf2cSBjoern A. Zeeb 	ieee80211_ref_node(ni);
2589c338cf2cSBjoern A. Zeeb 
2590c338cf2cSBjoern A. Zeeb 	/* See comments above for entire frame format. */
2591c338cf2cSBjoern A. Zeeb 	frmlen = ieee80211_probereq_ie_len(vap, ic);
2592c338cf2cSBjoern A. Zeeb 	m = ieee80211_getmgtframe(&frm,
2593c338cf2cSBjoern A. Zeeb 	    ic->ic_headroom + sizeof(struct ieee80211_frame), frmlen);
2594c338cf2cSBjoern A. Zeeb 	if (m == NULL) {
2595c338cf2cSBjoern A. Zeeb 		vap->iv_stats.is_tx_nobuf++;
2596c338cf2cSBjoern A. Zeeb 		ieee80211_free_node(ni);
2597c338cf2cSBjoern A. Zeeb 		ieee80211_free_node(bss);
2598c338cf2cSBjoern A. Zeeb 		return ENOMEM;
2599c338cf2cSBjoern A. Zeeb 	}
2600c338cf2cSBjoern A. Zeeb 
2601c338cf2cSBjoern A. Zeeb 	ret = ieee80211_probereq_ie(vap, ic, &frm, &frmlen, ssid, ssidlen,
2602c338cf2cSBjoern A. Zeeb 	    false);
2603c338cf2cSBjoern A. Zeeb 	KASSERT(ret == 0,
260449c220b0SBjoern A. Zeeb 	    ("%s: ieee80211_probereq_ie failed: %d\n", __func__, ret));
2605c338cf2cSBjoern A. Zeeb 
2606c338cf2cSBjoern A. Zeeb 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
260716d7cbb1SSam Leffler 	KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
260816d7cbb1SSam Leffler 	    ("leading space %zd", M_LEADINGSPACE(m)));
2609bd29f817SBjoern A. Zeeb 	M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT);
261016d7cbb1SSam Leffler 	if (m == NULL) {
261116d7cbb1SSam Leffler 		/* NB: cannot happen */
261216d7cbb1SSam Leffler 		ieee80211_free_node(ni);
2613f8a67728SAdrian Chadd 		ieee80211_free_node(bss);
2614af8418dcSSam Leffler 		return ENOMEM;
261516d7cbb1SSam Leffler 	}
2616af8418dcSSam Leffler 
26175cda6006SAdrian Chadd 	IEEE80211_TX_LOCK(ic);
26189e80b1dfSSam Leffler 	ieee80211_send_setup(ni, m,
2619af8418dcSSam Leffler 	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
26208ac160cdSSam Leffler 	     IEEE80211_NONQOS_TID, sa, da, bssid);
2621af8418dcSSam Leffler 	/* XXX power management? */
2622c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
2623af8418dcSSam Leffler 
2624b032f27cSSam Leffler 	M_WME_SETAC(m, WME_AC_BE);
2625b032f27cSSam Leffler 
2626af8418dcSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_probereq);
2627af8418dcSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_mgmt);
2628af8418dcSSam Leffler 
2629b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2630f8a67728SAdrian Chadd 	    "send probe req on channel %u bssid %s sa %6D da %6D ssid \"%.*s\"\n",
2631f8a67728SAdrian Chadd 	    ieee80211_chan2ieee(ic, ic->ic_curchan),
2632f8a67728SAdrian Chadd 	    ether_sprintf(bssid),
2633f8a67728SAdrian Chadd 	    sa, ":",
2634f8a67728SAdrian Chadd 	    da, ":",
2635b032f27cSSam Leffler 	    ssidlen, ssid);
2636af8418dcSSam Leffler 
263716d7cbb1SSam Leffler 	memset(&params, 0, sizeof(params));
263816d7cbb1SSam Leffler 	params.ibp_pri = M_WME_GETAC(m);
263916d7cbb1SSam Leffler 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
264016d7cbb1SSam Leffler 	params.ibp_rate0 = tp->mgmtrate;
264116d7cbb1SSam Leffler 	if (IEEE80211_IS_MULTICAST(da)) {
264216d7cbb1SSam Leffler 		params.ibp_flags |= IEEE80211_BPF_NOACK;
264316d7cbb1SSam Leffler 		params.ibp_try0 = 1;
264416d7cbb1SSam Leffler 	} else
264516d7cbb1SSam Leffler 		params.ibp_try0 = tp->maxretry;
264616d7cbb1SSam Leffler 	params.ibp_power = ni->ni_txpower;
26475cda6006SAdrian Chadd 	ret = ieee80211_raw_output(vap, ni, m, &params);
26485cda6006SAdrian Chadd 	IEEE80211_TX_UNLOCK(ic);
2649f8a67728SAdrian Chadd 	ieee80211_free_node(bss);
26505cda6006SAdrian Chadd 	return (ret);
2651af8418dcSSam Leffler }
2652af8418dcSSam Leffler 
2653af8418dcSSam Leffler /*
2654667dad55SSam Leffler  * Calculate capability information for mgt frames.
2655667dad55SSam Leffler  */
265659aa14a9SRui Paulo uint16_t
265759aa14a9SRui Paulo ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
2658667dad55SSam Leffler {
265968e8e04eSSam Leffler 	uint16_t capinfo;
2660667dad55SSam Leffler 
2661b032f27cSSam Leffler 	KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
2662667dad55SSam Leffler 
2663b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2664667dad55SSam Leffler 		capinfo = IEEE80211_CAPINFO_ESS;
2665b032f27cSSam Leffler 	else if (vap->iv_opmode == IEEE80211_M_IBSS)
2666667dad55SSam Leffler 		capinfo = IEEE80211_CAPINFO_IBSS;
2667667dad55SSam Leffler 	else
2668667dad55SSam Leffler 		capinfo = 0;
2669b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY)
2670667dad55SSam Leffler 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
2671f1481c8dSAdrian Chadd 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) &&
2672667dad55SSam Leffler 	    IEEE80211_IS_CHAN_2GHZ(chan))
2673667dad55SSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2674d20ff6e6SAdrian Chadd 	if (vap->iv_flags & IEEE80211_F_SHSLOT)
2675667dad55SSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2676b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
2677b032f27cSSam Leffler 		capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2678667dad55SSam Leffler 	return capinfo;
2679667dad55SSam Leffler }
2680667dad55SSam Leffler 
2681667dad55SSam Leffler /*
26820a915fadSSam Leffler  * Send a management frame.  The node is for the destination (or ic_bss
26830a915fadSSam Leffler  * when in station mode).  Nodes other than ic_bss have their reference
26840a915fadSSam Leffler  * count bumped to reflect our use for an indeterminant time.
26850a915fadSSam Leffler  */
26861a1e1d21SSam Leffler int
2687b032f27cSSam Leffler ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
26881a1e1d21SSam Leffler {
26891b6167d2SSam Leffler #define	HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
2690b032f27cSSam Leffler #define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2691b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2692b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2693b032f27cSSam Leffler 	struct ieee80211_node *bss = vap->iv_bss;
26948ac160cdSSam Leffler 	struct ieee80211_bpf_params params;
26951a1e1d21SSam Leffler 	struct mbuf *m;
269668e8e04eSSam Leffler 	uint8_t *frm;
269768e8e04eSSam Leffler 	uint16_t capinfo;
269868e8e04eSSam Leffler 	int has_challenge, is_shared_key, ret, status;
26991a1e1d21SSam Leffler 
27000a915fadSSam Leffler 	KASSERT(ni != NULL, ("null node"));
27010a915fadSSam Leffler 
27020a915fadSSam Leffler 	/*
27030a915fadSSam Leffler 	 * Hold a reference on the node so it doesn't go away until after
27040a915fadSSam Leffler 	 * the xmit is complete all the way in the driver.  On error we
27050a915fadSSam Leffler 	 * will remove our reference.
27060a915fadSSam Leffler 	 */
2707b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
270849a15236SSam Leffler 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
27098a1b9b6aSSam Leffler 		__func__, __LINE__,
271049a15236SSam Leffler 		ni, ether_sprintf(ni->ni_macaddr),
27118a1b9b6aSSam Leffler 		ieee80211_node_refcnt(ni)+1);
27120a915fadSSam Leffler 	ieee80211_ref_node(ni);
27138a1b9b6aSSam Leffler 
27148ac160cdSSam Leffler 	memset(&params, 0, sizeof(params));
27151a1e1d21SSam Leffler 	switch (type) {
27161a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_AUTH:
27178a1b9b6aSSam Leffler 		status = arg >> 16;
27188a1b9b6aSSam Leffler 		arg &= 0xffff;
27198a1b9b6aSSam Leffler 		has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
27208a1b9b6aSSam Leffler 		    arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
27218a1b9b6aSSam Leffler 		    ni->ni_challenge != NULL);
27228a1b9b6aSSam Leffler 
27238a1b9b6aSSam Leffler 		/*
27248a1b9b6aSSam Leffler 		 * Deduce whether we're doing open authentication or
27258a1b9b6aSSam Leffler 		 * shared key authentication.  We do the latter if
27268a1b9b6aSSam Leffler 		 * we're in the middle of a shared key authentication
27278a1b9b6aSSam Leffler 		 * handshake or if we're initiating an authentication
27288a1b9b6aSSam Leffler 		 * request and configured to use shared key.
27298a1b9b6aSSam Leffler 		 */
27308a1b9b6aSSam Leffler 		is_shared_key = has_challenge ||
27318a1b9b6aSSam Leffler 		     arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
27328a1b9b6aSSam Leffler 		     (arg == IEEE80211_AUTH_SHARED_REQUEST &&
2733b032f27cSSam Leffler 		      bss->ni_authmode == IEEE80211_AUTH_SHARED);
27348a1b9b6aSSam Leffler 
27358a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
273668e8e04eSSam Leffler 			  ic->ic_headroom + sizeof(struct ieee80211_frame),
273768e8e04eSSam Leffler 			  3 * sizeof(uint16_t)
27388a1b9b6aSSam Leffler 			+ (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
273949c220b0SBjoern A. Zeeb 				sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0));
27401a1e1d21SSam Leffler 		if (m == NULL)
27418a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
27428a1b9b6aSSam Leffler 
274368e8e04eSSam Leffler 		((uint16_t *)frm)[0] =
27448a1b9b6aSSam Leffler 		    (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
27458a1b9b6aSSam Leffler 		                    : htole16(IEEE80211_AUTH_ALG_OPEN);
274668e8e04eSSam Leffler 		((uint16_t *)frm)[1] = htole16(arg);	/* sequence number */
274768e8e04eSSam Leffler 		((uint16_t *)frm)[2] = htole16(status);/* status */
27488a1b9b6aSSam Leffler 
27498a1b9b6aSSam Leffler 		if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
275068e8e04eSSam Leffler 			((uint16_t *)frm)[3] =
27518a1b9b6aSSam Leffler 			    htole16((IEEE80211_CHALLENGE_LEN << 8) |
27528a1b9b6aSSam Leffler 			    IEEE80211_ELEMID_CHALLENGE);
275368e8e04eSSam Leffler 			memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
27548a1b9b6aSSam Leffler 			    IEEE80211_CHALLENGE_LEN);
27558a1b9b6aSSam Leffler 			m->m_pkthdr.len = m->m_len =
275668e8e04eSSam Leffler 				4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
27578a1b9b6aSSam Leffler 			if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
2758b032f27cSSam Leffler 				IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2759b032f27cSSam Leffler 				    "request encrypt frame (%s)", __func__);
27608ac160cdSSam Leffler 				/* mark frame for encryption */
27618ac160cdSSam Leffler 				params.ibp_flags |= IEEE80211_BPF_CRYPTO;
27628a1b9b6aSSam Leffler 			}
27638a1b9b6aSSam Leffler 		} else
276468e8e04eSSam Leffler 			m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
27658a1b9b6aSSam Leffler 
27668a1b9b6aSSam Leffler 		/* XXX not right for shared key */
27678a1b9b6aSSam Leffler 		if (status == IEEE80211_STATUS_SUCCESS)
27688a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_auth);
27698a1b9b6aSSam Leffler 		else
27708a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_auth_fail);
27718a1b9b6aSSam Leffler 
2772b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA)
277368e8e04eSSam Leffler 			ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2774b032f27cSSam Leffler 				(void *) vap->iv_state);
27751a1e1d21SSam Leffler 		break;
27761a1e1d21SSam Leffler 
27771a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
2778b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2779d72d72d3SAndriy Voskoboinyk 		    "send station deauthenticate (reason: %d (%s))", arg,
2780d72d72d3SAndriy Voskoboinyk 		    ieee80211_reason_to_string(arg));
278168e8e04eSSam Leffler 		m = ieee80211_getmgtframe(&frm,
278268e8e04eSSam Leffler 			ic->ic_headroom + sizeof(struct ieee80211_frame),
278368e8e04eSSam Leffler 			sizeof(uint16_t));
27841a1e1d21SSam Leffler 		if (m == NULL)
27858a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
278668e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(arg);	/* reason */
278768e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
27888a1b9b6aSSam Leffler 
27898a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, tx_deauth);
27908a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
27918a1b9b6aSSam Leffler 
2792e4918ecdSSam Leffler 		ieee80211_node_unauthorize(ni);		/* port closed */
27931a1e1d21SSam Leffler 		break;
27941a1e1d21SSam Leffler 
27951a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
27961a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
27971a1e1d21SSam Leffler 		/*
27981a1e1d21SSam Leffler 		 * asreq frame format
27991a1e1d21SSam Leffler 		 *	[2] capability information
28001a1e1d21SSam Leffler 		 *	[2] listen interval
28011a1e1d21SSam Leffler 		 *	[6*] current AP address (reassoc only)
28021a1e1d21SSam Leffler 		 *	[tlv] ssid
28031a1e1d21SSam Leffler 		 *	[tlv] supported rates
28041a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
2805b032f27cSSam Leffler 		 *	[4] power capability (optional)
2806b032f27cSSam Leffler 		 *	[28] supported channels (optional)
280768e8e04eSSam Leffler 		 *	[tlv] HT capabilities
280851172f62SAdrian Chadd 		 *	[tlv] VHT capabilities
2809b032f27cSSam Leffler 		 *	[tlv] WME (optional)
281068e8e04eSSam Leffler 		 *	[tlv] Vendor OUI HT capabilities (optional)
281168e8e04eSSam Leffler 		 *	[tlv] Atheros capabilities (if negotiated)
2812b032f27cSSam Leffler 		 *	[tlv] AppIE's (optional)
28131a1e1d21SSam Leffler 		 */
28148a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
281568e8e04eSSam Leffler 			 ic->ic_headroom + sizeof(struct ieee80211_frame),
281668e8e04eSSam Leffler 			 sizeof(uint16_t)
281768e8e04eSSam Leffler 		       + sizeof(uint16_t)
28181a1e1d21SSam Leffler 		       + IEEE80211_ADDR_LEN
28198a1b9b6aSSam Leffler 		       + 2 + IEEE80211_NWID_LEN
28201a1e1d21SSam Leffler 		       + 2 + IEEE80211_RATE_SIZE
28218a1b9b6aSSam Leffler 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2822b032f27cSSam Leffler 		       + 4
2823b032f27cSSam Leffler 		       + 2 + 26
28248a1b9b6aSSam Leffler 		       + sizeof(struct ieee80211_wme_info)
2825b032f27cSSam Leffler 		       + sizeof(struct ieee80211_ie_htcap)
282651172f62SAdrian Chadd 		       + sizeof(struct ieee80211_ie_vhtcap)
2827b032f27cSSam Leffler 		       + 4 + sizeof(struct ieee80211_ie_htcap)
2828616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
282968e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ath_ie)
2830616190d0SSam Leffler #endif
2831b032f27cSSam Leffler 		       + (vap->iv_appie_wpa != NULL ?
2832b032f27cSSam Leffler 				vap->iv_appie_wpa->ie_len : 0)
2833b032f27cSSam Leffler 		       + (vap->iv_appie_assocreq != NULL ?
2834b032f27cSSam Leffler 				vap->iv_appie_assocreq->ie_len : 0)
28358a1b9b6aSSam Leffler 		);
28361a1e1d21SSam Leffler 		if (m == NULL)
28378a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
28381a1e1d21SSam Leffler 
2839b032f27cSSam Leffler 		KASSERT(vap->iv_opmode == IEEE80211_M_STA,
2840b032f27cSSam Leffler 		    ("wrong mode %u", vap->iv_opmode));
2841667dad55SSam Leffler 		capinfo = IEEE80211_CAPINFO_ESS;
2842b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_PRIVACY)
28431a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
28444f2e09c4SSam Leffler 		/*
28454f2e09c4SSam Leffler 		 * NB: Some 11a AP's reject the request when
2846191ccdf5SAndriy Voskoboinyk 		 *     short preamble is set.
28474f2e09c4SSam Leffler 		 */
2848f1481c8dSAdrian Chadd 		if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) &&
2849b5c99415SSam Leffler 		    IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
28501a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
285168e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
28528a1b9b6aSSam Leffler 		    (ic->ic_caps & IEEE80211_C_SHSLOT))
28531a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
28541b6167d2SSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
2855b032f27cSSam Leffler 		    (vap->iv_flags & IEEE80211_F_DOTH))
28561b6167d2SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
285768e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(capinfo);
28581a1e1d21SSam Leffler 		frm += 2;
28591a1e1d21SSam Leffler 
2860b032f27cSSam Leffler 		KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
286168e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
2862b032f27cSSam Leffler 						    bss->ni_intval));
28631a1e1d21SSam Leffler 		frm += 2;
28641a1e1d21SSam Leffler 
28651a1e1d21SSam Leffler 		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2866b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
28671a1e1d21SSam Leffler 			frm += IEEE80211_ADDR_LEN;
28681a1e1d21SSam Leffler 		}
28691a1e1d21SSam Leffler 
28701a1e1d21SSam Leffler 		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
28711a1e1d21SSam Leffler 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
2872d8df5f3dSRui Paulo 		frm = ieee80211_add_rsn(frm, vap);
2873b032f27cSSam Leffler 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2874b032f27cSSam Leffler 		if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
2875b032f27cSSam Leffler 			frm = ieee80211_add_powercapability(frm,
2876b032f27cSSam Leffler 			    ic->ic_curchan);
2877b032f27cSSam Leffler 			frm = ieee80211_add_supportedchannels(frm, ic);
2878b032f27cSSam Leffler 		}
2879b83391bfSAdrian Chadd 
2880b83391bfSAdrian Chadd 		/*
2881b83391bfSAdrian Chadd 		 * Check the channel - we may be using an 11n NIC with an
2882b83391bfSAdrian Chadd 		 * 11n capable station, but we're configured to be an 11b
2883b83391bfSAdrian Chadd 		 * channel.
2884b83391bfSAdrian Chadd 		 */
28852bfc8a91SSam Leffler 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2886b83391bfSAdrian Chadd 		    IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2887b032f27cSSam Leffler 		    ni->ni_ies.htcap_ie != NULL &&
2888b83391bfSAdrian Chadd 		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP) {
2889b032f27cSSam Leffler 			frm = ieee80211_add_htcap(frm, ni);
2890b83391bfSAdrian Chadd 		}
289151172f62SAdrian Chadd 
289251172f62SAdrian Chadd 		if ((vap->iv_flags_vht & IEEE80211_FVHT_VHT) &&
289351172f62SAdrian Chadd 		    IEEE80211_IS_CHAN_VHT(ni->ni_chan) &&
289451172f62SAdrian Chadd 		    ni->ni_ies.vhtcap_ie != NULL &&
289551172f62SAdrian Chadd 		    ni->ni_ies.vhtcap_ie[0] == IEEE80211_ELEMID_VHT_CAP) {
289651172f62SAdrian Chadd 			frm = ieee80211_add_vhtcap(frm, ni);
289751172f62SAdrian Chadd 		}
289851172f62SAdrian Chadd 
2899d8df5f3dSRui Paulo 		frm = ieee80211_add_wpa(frm, vap);
2900f82316f6SBjoern A. Zeeb 		if ((vap->iv_flags & IEEE80211_F_WME) &&
2901b032f27cSSam Leffler 		    ni->ni_ies.wme_ie != NULL)
29028379e8dbSAdrian Chadd 			frm = ieee80211_add_wme_info(frm, &ic->ic_wme, ni);
2903b83391bfSAdrian Chadd 
2904b83391bfSAdrian Chadd 		/*
2905b83391bfSAdrian Chadd 		 * Same deal - only send HT info if we're on an 11n
2906b83391bfSAdrian Chadd 		 * capable channel.
2907b83391bfSAdrian Chadd 		 */
29082bfc8a91SSam Leffler 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2909b83391bfSAdrian Chadd 		    IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2910b032f27cSSam Leffler 		    ni->ni_ies.htcap_ie != NULL &&
2911b83391bfSAdrian Chadd 		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR) {
2912b032f27cSSam Leffler 			frm = ieee80211_add_htcap_vendor(frm, ni);
2913b83391bfSAdrian Chadd 		}
2914616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
29154207227cSSam Leffler 		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
2916b032f27cSSam Leffler 			frm = ieee80211_add_ath(frm,
2917b032f27cSSam Leffler 				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
29184207227cSSam Leffler 				((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
29194207227cSSam Leffler 				 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
29204207227cSSam Leffler 				vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
29214207227cSSam Leffler 		}
2922616190d0SSam Leffler #endif /* IEEE80211_SUPPORT_SUPERG */
2923b032f27cSSam Leffler 		if (vap->iv_appie_assocreq != NULL)
2924b032f27cSSam Leffler 			frm = add_appie(frm, vap->iv_appie_assocreq);
292568e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
29261a1e1d21SSam Leffler 
292768e8e04eSSam Leffler 		ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2928b032f27cSSam Leffler 			(void *) vap->iv_state);
29291a1e1d21SSam Leffler 		break;
29301a1e1d21SSam Leffler 
29311a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
29321a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
29331a1e1d21SSam Leffler 		/*
293468e8e04eSSam Leffler 		 * asresp frame format
29351a1e1d21SSam Leffler 		 *	[2] capability information
29361a1e1d21SSam Leffler 		 *	[2] status
29371a1e1d21SSam Leffler 		 *	[2] association ID
29381a1e1d21SSam Leffler 		 *	[tlv] supported rates
29391a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
2940b032f27cSSam Leffler 		 *	[tlv] HT capabilities (standard, if STA enabled)
2941b032f27cSSam Leffler 		 *	[tlv] HT information (standard, if STA enabled)
294251172f62SAdrian Chadd 		 *	[tlv] VHT capabilities (standard, if STA enabled)
294351172f62SAdrian Chadd 		 *	[tlv] VHT information (standard, if STA enabled)
2944b032f27cSSam Leffler 		 *	[tlv] WME (if configured and STA enabled)
2945b032f27cSSam Leffler 		 *	[tlv] HT capabilities (vendor OUI, if STA enabled)
2946b032f27cSSam Leffler 		 *	[tlv] HT information (vendor OUI, if STA enabled)
2947b032f27cSSam Leffler 		 *	[tlv] Atheros capabilities (if STA enabled)
2948b032f27cSSam Leffler 		 *	[tlv] AppIE's (optional)
29491a1e1d21SSam Leffler 		 */
29508a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
295168e8e04eSSam Leffler 			 ic->ic_headroom + sizeof(struct ieee80211_frame),
295268e8e04eSSam Leffler 			 sizeof(uint16_t)
295368e8e04eSSam Leffler 		       + sizeof(uint16_t)
295468e8e04eSSam Leffler 		       + sizeof(uint16_t)
29551a1e1d21SSam Leffler 		       + 2 + IEEE80211_RATE_SIZE
29568a1b9b6aSSam Leffler 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
295768e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ie_htcap) + 4
295868e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ie_htinfo) + 4
295951172f62SAdrian Chadd 		       + sizeof(struct ieee80211_ie_vhtcap)
296051172f62SAdrian Chadd 		       + sizeof(struct ieee80211_ie_vht_operation)
2961b032f27cSSam Leffler 		       + sizeof(struct ieee80211_wme_param)
2962616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
296368e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ath_ie)
2964616190d0SSam Leffler #endif
2965b032f27cSSam Leffler 		       + (vap->iv_appie_assocresp != NULL ?
2966b032f27cSSam Leffler 				vap->iv_appie_assocresp->ie_len : 0)
29678a1b9b6aSSam Leffler 		);
29681a1e1d21SSam Leffler 		if (m == NULL)
29698a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
29701a1e1d21SSam Leffler 
297159aa14a9SRui Paulo 		capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
297268e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(capinfo);
29731a1e1d21SSam Leffler 		frm += 2;
29741a1e1d21SSam Leffler 
297568e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(arg);	/* status */
29761a1e1d21SSam Leffler 		frm += 2;
29771a1e1d21SSam Leffler 
29788a1b9b6aSSam Leffler 		if (arg == IEEE80211_STATUS_SUCCESS) {
297968e8e04eSSam Leffler 			*(uint16_t *)frm = htole16(ni->ni_associd);
29808a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_assoc);
29818a1b9b6aSSam Leffler 		} else
29828a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_assoc_fail);
29831a1e1d21SSam Leffler 		frm += 2;
29841a1e1d21SSam Leffler 
29851a1e1d21SSam Leffler 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
29861a1e1d21SSam Leffler 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
298768e8e04eSSam Leffler 		/* NB: respond according to what we received */
29881b6167d2SSam Leffler 		if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
298968e8e04eSSam Leffler 			frm = ieee80211_add_htcap(frm, ni);
299068e8e04eSSam Leffler 			frm = ieee80211_add_htinfo(frm, ni);
299168e8e04eSSam Leffler 		}
2992b032f27cSSam Leffler 		if ((vap->iv_flags & IEEE80211_F_WME) &&
2993b032f27cSSam Leffler 		    ni->ni_ies.wme_ie != NULL)
29948379e8dbSAdrian Chadd 			frm = ieee80211_add_wme_param(frm, &ic->ic_wme,
29958379e8dbSAdrian Chadd 			    !! (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD));
29961b6167d2SSam Leffler 		if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
29971b6167d2SSam Leffler 			frm = ieee80211_add_htcap_vendor(frm, ni);
29981b6167d2SSam Leffler 			frm = ieee80211_add_htinfo_vendor(frm, ni);
299968e8e04eSSam Leffler 		}
300051172f62SAdrian Chadd 		if (ni->ni_flags & IEEE80211_NODE_VHT) {
300151172f62SAdrian Chadd 			frm = ieee80211_add_vhtcap(frm, ni);
300251172f62SAdrian Chadd 			frm = ieee80211_add_vhtinfo(frm, ni);
300351172f62SAdrian Chadd 		}
3004616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
3005b032f27cSSam Leffler 		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
300668e8e04eSSam Leffler 			frm = ieee80211_add_ath(frm,
3007b032f27cSSam Leffler 				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
30084207227cSSam Leffler 				((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
30094207227cSSam Leffler 				 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
30104207227cSSam Leffler 				vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
3011616190d0SSam Leffler #endif /* IEEE80211_SUPPORT_SUPERG */
3012b032f27cSSam Leffler 		if (vap->iv_appie_assocresp != NULL)
3013b032f27cSSam Leffler 			frm = add_appie(frm, vap->iv_appie_assocresp);
301468e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
30151a1e1d21SSam Leffler 		break;
30161a1e1d21SSam Leffler 
30171a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
3018b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
3019d72d72d3SAndriy Voskoboinyk 		    "send station disassociate (reason: %d (%s))", arg,
3020d72d72d3SAndriy Voskoboinyk 		    ieee80211_reason_to_string(arg));
302168e8e04eSSam Leffler 		m = ieee80211_getmgtframe(&frm,
302268e8e04eSSam Leffler 			ic->ic_headroom + sizeof(struct ieee80211_frame),
302368e8e04eSSam Leffler 			sizeof(uint16_t));
30241a1e1d21SSam Leffler 		if (m == NULL)
30258a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
302668e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(arg);	/* reason */
302768e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
30288a1b9b6aSSam Leffler 
30298a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, tx_disassoc);
30308a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
30311a1e1d21SSam Leffler 		break;
30321a1e1d21SSam Leffler 
30331a1e1d21SSam Leffler 	default:
3034b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
3035b032f27cSSam Leffler 		    "invalid mgmt frame type %u", type);
30361be50176SSam Leffler 		senderr(EINVAL, is_tx_unknownmgt);
30370a915fadSSam Leffler 		/* NOTREACHED */
30381a1e1d21SSam Leffler 	}
303968e8e04eSSam Leffler 
30408ac160cdSSam Leffler 	/* NB: force non-ProbeResp frames to the highest queue */
30418ac160cdSSam Leffler 	params.ibp_pri = WME_AC_VO;
30428ac160cdSSam Leffler 	params.ibp_rate0 = bss->ni_txparms->mgmtrate;
30438ac160cdSSam Leffler 	/* NB: we know all frames are unicast */
30448ac160cdSSam Leffler 	params.ibp_try0 = bss->ni_txparms->maxretry;
30458ac160cdSSam Leffler 	params.ibp_power = bss->ni_txpower;
30468ac160cdSSam Leffler 	return ieee80211_mgmt_output(ni, m, type, &params);
30470a915fadSSam Leffler bad:
30488a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
30491a1e1d21SSam Leffler 	return ret;
30500a915fadSSam Leffler #undef senderr
30511b6167d2SSam Leffler #undef HTFLAGS
30521a1e1d21SSam Leffler }
30538a1b9b6aSSam Leffler 
3054b032f27cSSam Leffler /*
3055b032f27cSSam Leffler  * Return an mbuf with a probe response frame in it.
3056b032f27cSSam Leffler  * Space is left to prepend and 802.11 header at the
3057b032f27cSSam Leffler  * front but it's left to the caller to fill in.
3058b032f27cSSam Leffler  */
3059b032f27cSSam Leffler struct mbuf *
3060b032f27cSSam Leffler ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
3061b032f27cSSam Leffler {
3062b032f27cSSam Leffler 	struct ieee80211vap *vap = bss->ni_vap;
3063b032f27cSSam Leffler 	struct ieee80211com *ic = bss->ni_ic;
3064b032f27cSSam Leffler 	const struct ieee80211_rateset *rs;
3065b032f27cSSam Leffler 	struct mbuf *m;
3066b032f27cSSam Leffler 	uint16_t capinfo;
3067b032f27cSSam Leffler 	uint8_t *frm;
3068b032f27cSSam Leffler 
3069b032f27cSSam Leffler 	/*
3070b032f27cSSam Leffler 	 * probe response frame format
3071b032f27cSSam Leffler 	 *	[8] time stamp
3072b032f27cSSam Leffler 	 *	[2] beacon interval
3073b032f27cSSam Leffler 	 *	[2] cabability information
3074b032f27cSSam Leffler 	 *	[tlv] ssid
3075b032f27cSSam Leffler 	 *	[tlv] supported rates
3076b032f27cSSam Leffler 	 *	[tlv] parameter set (FH/DS)
3077b032f27cSSam Leffler 	 *	[tlv] parameter set (IBSS)
3078b032f27cSSam Leffler 	 *	[tlv] country (optional)
3079b032f27cSSam Leffler 	 *	[3] power control (optional)
3080b032f27cSSam Leffler 	 *	[5] channel switch announcement (CSA) (optional)
3081b032f27cSSam Leffler 	 *	[tlv] extended rate phy (ERP)
3082b032f27cSSam Leffler 	 *	[tlv] extended supported rates
3083688fe74dSSam Leffler 	 *	[tlv] RSN (optional)
3084b032f27cSSam Leffler 	 *	[tlv] HT capabilities
3085b032f27cSSam Leffler 	 *	[tlv] HT information
3086ac0b7d32SAdrian Chadd 	 *	[tlv] VHT capabilities
3087ac0b7d32SAdrian Chadd 	 *	[tlv] VHT information
3088b032f27cSSam Leffler 	 *	[tlv] WPA (optional)
3089b032f27cSSam Leffler 	 *	[tlv] WME (optional)
3090b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT capabilities (optional)
3091b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT information (optional)
3092b032f27cSSam Leffler 	 *	[tlv] Atheros capabilities
3093b032f27cSSam Leffler 	 *	[tlv] AppIE's (optional)
309459aa14a9SRui Paulo 	 *	[tlv] Mesh ID (MBSS)
309559aa14a9SRui Paulo 	 *	[tlv] Mesh Conf (MBSS)
3096b032f27cSSam Leffler 	 */
3097b032f27cSSam Leffler 	m = ieee80211_getmgtframe(&frm,
3098b032f27cSSam Leffler 		 ic->ic_headroom + sizeof(struct ieee80211_frame),
3099b032f27cSSam Leffler 		 8
3100b032f27cSSam Leffler 	       + sizeof(uint16_t)
3101b032f27cSSam Leffler 	       + sizeof(uint16_t)
3102b032f27cSSam Leffler 	       + 2 + IEEE80211_NWID_LEN
3103b032f27cSSam Leffler 	       + 2 + IEEE80211_RATE_SIZE
3104b032f27cSSam Leffler 	       + 7	/* max(7,3) */
3105b032f27cSSam Leffler 	       + IEEE80211_COUNTRY_MAX_SIZE
3106b032f27cSSam Leffler 	       + 3
3107b032f27cSSam Leffler 	       + sizeof(struct ieee80211_csa_ie)
310832b0e64bSAdrian Chadd 	       + sizeof(struct ieee80211_quiet_ie)
3109b032f27cSSam Leffler 	       + 3
3110b032f27cSSam Leffler 	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
3111688fe74dSSam Leffler 	       + sizeof(struct ieee80211_ie_wpa)
3112b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_htcap)
3113b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_htinfo)
3114b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_wpa)
3115b032f27cSSam Leffler 	       + sizeof(struct ieee80211_wme_param)
3116b032f27cSSam Leffler 	       + 4 + sizeof(struct ieee80211_ie_htcap)
3117b032f27cSSam Leffler 	       + 4 + sizeof(struct ieee80211_ie_htinfo)
3118ac0b7d32SAdrian Chadd 	       +  sizeof(struct ieee80211_ie_vhtcap)
3119ac0b7d32SAdrian Chadd 	       +  sizeof(struct ieee80211_ie_vht_operation)
3120616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
3121b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ath_ie)
3122616190d0SSam Leffler #endif
312359aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
312459aa14a9SRui Paulo 	       + 2 + IEEE80211_MESHID_LEN
312559aa14a9SRui Paulo 	       + sizeof(struct ieee80211_meshconf_ie)
312659aa14a9SRui Paulo #endif
3127b032f27cSSam Leffler 	       + (vap->iv_appie_proberesp != NULL ?
3128b032f27cSSam Leffler 			vap->iv_appie_proberesp->ie_len : 0)
3129b032f27cSSam Leffler 	);
3130b032f27cSSam Leffler 	if (m == NULL) {
3131b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
3132b032f27cSSam Leffler 		return NULL;
3133b032f27cSSam Leffler 	}
3134b032f27cSSam Leffler 
3135b032f27cSSam Leffler 	memset(frm, 0, 8);	/* timestamp should be filled later */
3136b032f27cSSam Leffler 	frm += 8;
3137b032f27cSSam Leffler 	*(uint16_t *)frm = htole16(bss->ni_intval);
3138b032f27cSSam Leffler 	frm += 2;
313959aa14a9SRui Paulo 	capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
3140b032f27cSSam Leffler 	*(uint16_t *)frm = htole16(capinfo);
3141b032f27cSSam Leffler 	frm += 2;
3142b032f27cSSam Leffler 
3143b032f27cSSam Leffler 	frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
3144b032f27cSSam Leffler 	rs = ieee80211_get_suprates(ic, bss->ni_chan);
3145b032f27cSSam Leffler 	frm = ieee80211_add_rates(frm, rs);
3146b032f27cSSam Leffler 
3147b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
3148b032f27cSSam Leffler 		*frm++ = IEEE80211_ELEMID_FHPARMS;
3149b032f27cSSam Leffler 		*frm++ = 5;
3150b032f27cSSam Leffler 		*frm++ = bss->ni_fhdwell & 0x00ff;
3151b032f27cSSam Leffler 		*frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
3152b032f27cSSam Leffler 		*frm++ = IEEE80211_FH_CHANSET(
3153b032f27cSSam Leffler 		    ieee80211_chan2ieee(ic, bss->ni_chan));
3154b032f27cSSam Leffler 		*frm++ = IEEE80211_FH_CHANPAT(
3155b032f27cSSam Leffler 		    ieee80211_chan2ieee(ic, bss->ni_chan));
3156b032f27cSSam Leffler 		*frm++ = bss->ni_fhindex;
3157b032f27cSSam Leffler 	} else {
3158b032f27cSSam Leffler 		*frm++ = IEEE80211_ELEMID_DSPARMS;
3159b032f27cSSam Leffler 		*frm++ = 1;
3160b032f27cSSam Leffler 		*frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
3161b032f27cSSam Leffler 	}
3162b032f27cSSam Leffler 
3163b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
3164b032f27cSSam Leffler 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
3165b032f27cSSam Leffler 		*frm++ = 2;
3166b032f27cSSam Leffler 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
3167b032f27cSSam Leffler 	}
3168b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
3169b032f27cSSam Leffler 	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
3170b032f27cSSam Leffler 		frm = ieee80211_add_countryie(frm, ic);
3171b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_DOTH) {
3172b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
3173b032f27cSSam Leffler 			frm = ieee80211_add_powerconstraint(frm, vap);
3174b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
3175b032f27cSSam Leffler 			frm = ieee80211_add_csa(frm, vap);
3176b032f27cSSam Leffler 	}
317732b0e64bSAdrian Chadd 	if (vap->iv_flags & IEEE80211_F_DOTH) {
317832b0e64bSAdrian Chadd 		if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
317932b0e64bSAdrian Chadd 		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
318032b0e64bSAdrian Chadd 			if (vap->iv_quiet)
3181ce4552cdSAdrian Chadd 				frm = ieee80211_add_quiet(frm, vap, 0);
318232b0e64bSAdrian Chadd 		}
318332b0e64bSAdrian Chadd 	}
3184b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
3185f1481c8dSAdrian Chadd 		frm = ieee80211_add_erp(frm, vap);
3186b032f27cSSam Leffler 	frm = ieee80211_add_xrates(frm, rs);
3187d8df5f3dSRui Paulo 	frm = ieee80211_add_rsn(frm, vap);
3188b032f27cSSam Leffler 	/*
3189b032f27cSSam Leffler 	 * NB: legacy 11b clients do not get certain ie's.
3190b032f27cSSam Leffler 	 *     The caller identifies such clients by passing
3191b032f27cSSam Leffler 	 *     a token in legacy to us.  Could expand this to be
3192b032f27cSSam Leffler 	 *     any legacy client for stuff like HT ie's.
3193b032f27cSSam Leffler 	 */
3194b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
3195b032f27cSSam Leffler 	    legacy != IEEE80211_SEND_LEGACY_11B) {
3196b032f27cSSam Leffler 		frm = ieee80211_add_htcap(frm, bss);
3197b032f27cSSam Leffler 		frm = ieee80211_add_htinfo(frm, bss);
3198b032f27cSSam Leffler 	}
3199ac0b7d32SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT(bss->ni_chan) &&
3200ac0b7d32SAdrian Chadd 	    legacy != IEEE80211_SEND_LEGACY_11B) {
3201ac0b7d32SAdrian Chadd 		frm = ieee80211_add_vhtcap(frm, bss);
3202ac0b7d32SAdrian Chadd 		frm = ieee80211_add_vhtinfo(frm, bss);
3203ac0b7d32SAdrian Chadd 	}
3204d8df5f3dSRui Paulo 	frm = ieee80211_add_wpa(frm, vap);
3205b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WME)
32068379e8dbSAdrian Chadd 		frm = ieee80211_add_wme_param(frm, &ic->ic_wme,
32078379e8dbSAdrian Chadd 		    !! (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD));
3208b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
32092bfc8a91SSam Leffler 	    (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
3210b032f27cSSam Leffler 	    legacy != IEEE80211_SEND_LEGACY_11B) {
3211b032f27cSSam Leffler 		frm = ieee80211_add_htcap_vendor(frm, bss);
3212b032f27cSSam Leffler 		frm = ieee80211_add_htinfo_vendor(frm, bss);
3213b032f27cSSam Leffler 	}
3214616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
32154207227cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
32164207227cSSam Leffler 	    legacy != IEEE80211_SEND_LEGACY_11B)
32174207227cSSam Leffler 		frm = ieee80211_add_athcaps(frm, bss);
3218616190d0SSam Leffler #endif
3219b032f27cSSam Leffler 	if (vap->iv_appie_proberesp != NULL)
3220b032f27cSSam Leffler 		frm = add_appie(frm, vap->iv_appie_proberesp);
322159aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
322259aa14a9SRui Paulo 	if (vap->iv_opmode == IEEE80211_M_MBSS) {
322359aa14a9SRui Paulo 		frm = ieee80211_add_meshid(frm, vap);
322459aa14a9SRui Paulo 		frm = ieee80211_add_meshconf(frm, vap);
322559aa14a9SRui Paulo 	}
322659aa14a9SRui Paulo #endif
3227b032f27cSSam Leffler 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3228b032f27cSSam Leffler 
3229b032f27cSSam Leffler 	return m;
3230b032f27cSSam Leffler }
3231b032f27cSSam Leffler 
3232b032f27cSSam Leffler /*
3233b032f27cSSam Leffler  * Send a probe response frame to the specified mac address.
3234b032f27cSSam Leffler  * This does not go through the normal mgt frame api so we
3235b032f27cSSam Leffler  * can specify the destination address and re-use the bss node
3236b032f27cSSam Leffler  * for the sta reference.
3237b032f27cSSam Leffler  */
3238b032f27cSSam Leffler int
3239b032f27cSSam Leffler ieee80211_send_proberesp(struct ieee80211vap *vap,
3240b032f27cSSam Leffler 	const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
3241b032f27cSSam Leffler {
3242b032f27cSSam Leffler 	struct ieee80211_node *bss = vap->iv_bss;
3243b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
3244b032f27cSSam Leffler 	struct mbuf *m;
32455cda6006SAdrian Chadd 	int ret;
3246b032f27cSSam Leffler 
3247b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
3248b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
3249b032f27cSSam Leffler 		    "block %s frame in CAC state", "probe response");
3250b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
3251b032f27cSSam Leffler 		return EIO;		/* XXX */
3252b032f27cSSam Leffler 	}
3253b032f27cSSam Leffler 
3254b032f27cSSam Leffler 	/*
3255b032f27cSSam Leffler 	 * Hold a reference on the node so it doesn't go away until after
3256b032f27cSSam Leffler 	 * the xmit is complete all the way in the driver.  On error we
3257b032f27cSSam Leffler 	 * will remove our reference.
3258b032f27cSSam Leffler 	 */
3259b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
3260b032f27cSSam Leffler 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
3261b032f27cSSam Leffler 	    __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
3262b032f27cSSam Leffler 	    ieee80211_node_refcnt(bss)+1);
3263b032f27cSSam Leffler 	ieee80211_ref_node(bss);
3264b032f27cSSam Leffler 
3265b032f27cSSam Leffler 	m = ieee80211_alloc_proberesp(bss, legacy);
3266b032f27cSSam Leffler 	if (m == NULL) {
3267b032f27cSSam Leffler 		ieee80211_free_node(bss);
3268b032f27cSSam Leffler 		return ENOMEM;
3269b032f27cSSam Leffler 	}
3270b032f27cSSam Leffler 
3271bd29f817SBjoern A. Zeeb 	M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT);
3272b032f27cSSam Leffler 	KASSERT(m != NULL, ("no room for header"));
3273b032f27cSSam Leffler 
32745cda6006SAdrian Chadd 	IEEE80211_TX_LOCK(ic);
32759e80b1dfSSam Leffler 	ieee80211_send_setup(bss, m,
3276b032f27cSSam Leffler 	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
32778ac160cdSSam Leffler 	     IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
3278b032f27cSSam Leffler 	/* XXX power management? */
3279c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
3280b032f27cSSam Leffler 
3281b032f27cSSam Leffler 	M_WME_SETAC(m, WME_AC_BE);
3282b032f27cSSam Leffler 
3283b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
3284b032f27cSSam Leffler 	    "send probe resp on channel %u to %s%s\n",
3285b032f27cSSam Leffler 	    ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
3286b032f27cSSam Leffler 	    legacy ? " <legacy>" : "");
3287b032f27cSSam Leffler 	IEEE80211_NODE_STAT(bss, tx_mgmt);
3288b032f27cSSam Leffler 
32895cda6006SAdrian Chadd 	ret = ieee80211_raw_output(vap, bss, m, NULL);
32905cda6006SAdrian Chadd 	IEEE80211_TX_UNLOCK(ic);
32915cda6006SAdrian Chadd 	return (ret);
3292b032f27cSSam Leffler }
3293b032f27cSSam Leffler 
3294b032f27cSSam Leffler /*
3295b032f27cSSam Leffler  * Allocate and build a RTS (Request To Send) control frame.
3296b032f27cSSam Leffler  */
3297b032f27cSSam Leffler struct mbuf *
3298b032f27cSSam Leffler ieee80211_alloc_rts(struct ieee80211com *ic,
3299b032f27cSSam Leffler 	const uint8_t ra[IEEE80211_ADDR_LEN],
3300b032f27cSSam Leffler 	const uint8_t ta[IEEE80211_ADDR_LEN],
3301b032f27cSSam Leffler 	uint16_t dur)
3302b032f27cSSam Leffler {
3303b032f27cSSam Leffler 	struct ieee80211_frame_rts *rts;
3304b032f27cSSam Leffler 	struct mbuf *m;
3305b032f27cSSam Leffler 
3306b032f27cSSam Leffler 	/* XXX honor ic_headroom */
3307bd29f817SBjoern A. Zeeb 	m = m_gethdr(IEEE80211_M_NOWAIT, MT_DATA);
3308b032f27cSSam Leffler 	if (m != NULL) {
3309b032f27cSSam Leffler 		rts = mtod(m, struct ieee80211_frame_rts *);
3310b032f27cSSam Leffler 		rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
3311b032f27cSSam Leffler 			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
3312b032f27cSSam Leffler 		rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3313b032f27cSSam Leffler 		*(u_int16_t *)rts->i_dur = htole16(dur);
3314b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(rts->i_ra, ra);
3315b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(rts->i_ta, ta);
3316b032f27cSSam Leffler 
3317b032f27cSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
3318b032f27cSSam Leffler 	}
3319b032f27cSSam Leffler 	return m;
3320b032f27cSSam Leffler }
3321b032f27cSSam Leffler 
3322b032f27cSSam Leffler /*
3323b032f27cSSam Leffler  * Allocate and build a CTS (Clear To Send) control frame.
3324b032f27cSSam Leffler  */
3325b032f27cSSam Leffler struct mbuf *
3326b032f27cSSam Leffler ieee80211_alloc_cts(struct ieee80211com *ic,
3327b032f27cSSam Leffler 	const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
3328b032f27cSSam Leffler {
3329b032f27cSSam Leffler 	struct ieee80211_frame_cts *cts;
3330b032f27cSSam Leffler 	struct mbuf *m;
3331b032f27cSSam Leffler 
3332b032f27cSSam Leffler 	/* XXX honor ic_headroom */
3333bd29f817SBjoern A. Zeeb 	m = m_gethdr(IEEE80211_M_NOWAIT, MT_DATA);
3334b032f27cSSam Leffler 	if (m != NULL) {
3335b032f27cSSam Leffler 		cts = mtod(m, struct ieee80211_frame_cts *);
3336b032f27cSSam Leffler 		cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
3337b032f27cSSam Leffler 			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
3338b032f27cSSam Leffler 		cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3339b032f27cSSam Leffler 		*(u_int16_t *)cts->i_dur = htole16(dur);
3340b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(cts->i_ra, ra);
3341b032f27cSSam Leffler 
3342b032f27cSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
3343b032f27cSSam Leffler 	}
3344b032f27cSSam Leffler 	return m;
3345b032f27cSSam Leffler }
3346b032f27cSSam Leffler 
3347d1b67106SAndriy Voskoboinyk /*
3348d1b67106SAndriy Voskoboinyk  * Wrapper for CTS/RTS frame allocation.
3349d1b67106SAndriy Voskoboinyk  */
3350d1b67106SAndriy Voskoboinyk struct mbuf *
3351d1b67106SAndriy Voskoboinyk ieee80211_alloc_prot(struct ieee80211_node *ni, const struct mbuf *m,
3352d1b67106SAndriy Voskoboinyk     uint8_t rate, int prot)
3353d1b67106SAndriy Voskoboinyk {
3354d1b67106SAndriy Voskoboinyk 	struct ieee80211com *ic = ni->ni_ic;
3355f1481c8dSAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
3356d1b67106SAndriy Voskoboinyk 	const struct ieee80211_frame *wh;
3357d1b67106SAndriy Voskoboinyk 	struct mbuf *mprot;
3358d1b67106SAndriy Voskoboinyk 	uint16_t dur;
3359d1b67106SAndriy Voskoboinyk 	int pktlen, isshort;
3360d1b67106SAndriy Voskoboinyk 
3361d1b67106SAndriy Voskoboinyk 	KASSERT(prot == IEEE80211_PROT_RTSCTS ||
3362d1b67106SAndriy Voskoboinyk 	    prot == IEEE80211_PROT_CTSONLY,
3363d1b67106SAndriy Voskoboinyk 	    ("wrong protection type %d", prot));
3364d1b67106SAndriy Voskoboinyk 
3365d1b67106SAndriy Voskoboinyk 	wh = mtod(m, const struct ieee80211_frame *);
3366d1b67106SAndriy Voskoboinyk 	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
3367f1481c8dSAdrian Chadd 	isshort = (vap->iv_flags & IEEE80211_F_SHPREAMBLE) != 0;
3368d1b67106SAndriy Voskoboinyk 	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
3369d1b67106SAndriy Voskoboinyk 	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3370d1b67106SAndriy Voskoboinyk 
3371d1b67106SAndriy Voskoboinyk 	if (prot == IEEE80211_PROT_RTSCTS) {
3372d1b67106SAndriy Voskoboinyk 		/* NB: CTS is the same size as an ACK */
3373d1b67106SAndriy Voskoboinyk 		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3374d1b67106SAndriy Voskoboinyk 		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
3375d1b67106SAndriy Voskoboinyk 	} else
3376f1481c8dSAdrian Chadd 		mprot = ieee80211_alloc_cts(ic, vap->iv_myaddr, dur);
3377d1b67106SAndriy Voskoboinyk 
3378d1b67106SAndriy Voskoboinyk 	return (mprot);
3379d1b67106SAndriy Voskoboinyk }
3380d1b67106SAndriy Voskoboinyk 
338168e8e04eSSam Leffler static void
338268e8e04eSSam Leffler ieee80211_tx_mgt_timeout(void *arg)
338368e8e04eSSam Leffler {
3384e94527beSAdrian Chadd 	struct ieee80211vap *vap = arg;
338568e8e04eSSam Leffler 
338604efa18fSBjoern A. Zeeb 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
338704efa18fSBjoern A. Zeeb 	    "vap %p mode %s state %s flags %#x & %#x\n", vap,
338804efa18fSBjoern A. Zeeb 	    ieee80211_opmode_name[vap->iv_opmode],
338904efa18fSBjoern A. Zeeb 	    ieee80211_state_name[vap->iv_state],
339004efa18fSBjoern A. Zeeb 	    vap->iv_ic->ic_flags, IEEE80211_F_SCAN);
339104efa18fSBjoern A. Zeeb 
3392e94527beSAdrian Chadd 	IEEE80211_LOCK(vap->iv_ic);
3393b032f27cSSam Leffler 	if (vap->iv_state != IEEE80211_S_INIT &&
3394b032f27cSSam Leffler 	    (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
339568e8e04eSSam Leffler 		/*
339668e8e04eSSam Leffler 		 * NB: it's safe to specify a timeout as the reason here;
339768e8e04eSSam Leffler 		 *     it'll only be used in the right state.
339868e8e04eSSam Leffler 		 */
3399e94527beSAdrian Chadd 		ieee80211_new_state_locked(vap, IEEE80211_S_SCAN,
340068e8e04eSSam Leffler 			IEEE80211_SCAN_FAIL_TIMEOUT);
340168e8e04eSSam Leffler 	}
3402e94527beSAdrian Chadd 	IEEE80211_UNLOCK(vap->iv_ic);
340368e8e04eSSam Leffler }
340468e8e04eSSam Leffler 
3405e94527beSAdrian Chadd /*
3406e94527beSAdrian Chadd  * This is the callback set on net80211-sourced transmitted
3407e94527beSAdrian Chadd  * authentication request frames.
3408e94527beSAdrian Chadd  *
3409e94527beSAdrian Chadd  * This does a couple of things:
3410e94527beSAdrian Chadd  *
3411e94527beSAdrian Chadd  * + If the frame transmitted was a success, it schedules a future
3412e94527beSAdrian Chadd  *   event which will transition the interface to scan.
3413e94527beSAdrian Chadd  *   If a state transition _then_ occurs before that event occurs,
3414e94527beSAdrian Chadd  *   said state transition will cancel this callout.
3415e94527beSAdrian Chadd  *
3416e94527beSAdrian Chadd  * + If the frame transmit was a failure, it immediately schedules
3417e94527beSAdrian Chadd  *   the transition back to scan.
3418e94527beSAdrian Chadd  */
341968e8e04eSSam Leffler static void
342068e8e04eSSam Leffler ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
342168e8e04eSSam Leffler {
3422b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
34236671366aSBrooks Davis 	enum ieee80211_state ostate = (enum ieee80211_state)(uintptr_t)arg;
342468e8e04eSSam Leffler 
342568e8e04eSSam Leffler 	/*
342668e8e04eSSam Leffler 	 * Frame transmit completed; arrange timer callback.  If
3427a4641f4eSPedro F. Giffuni 	 * transmit was successfully we wait for response.  Otherwise
342868e8e04eSSam Leffler 	 * we arrange an immediate callback instead of doing the
342968e8e04eSSam Leffler 	 * callback directly since we don't know what state the driver
343068e8e04eSSam Leffler 	 * is in (e.g. what locks it is holding).  This work should
343168e8e04eSSam Leffler 	 * not be too time-critical and not happen too often so the
343268e8e04eSSam Leffler 	 * added overhead is acceptable.
343368e8e04eSSam Leffler 	 *
343468e8e04eSSam Leffler 	 * XXX what happens if !acked but response shows up before callback?
343568e8e04eSSam Leffler 	 */
3436e94527beSAdrian Chadd 	if (vap->iv_state == ostate) {
343704efa18fSBjoern A. Zeeb 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
34384f158a44SCy Schubert 		    "ni %p mode %s state %s arg %p status %d\n", ni,
343904efa18fSBjoern A. Zeeb 		    ieee80211_opmode_name[vap->iv_opmode],
34404f158a44SCy Schubert 		    ieee80211_state_name[vap->iv_state], arg, status);
344104efa18fSBjoern A. Zeeb 
3442b032f27cSSam Leffler 		callout_reset(&vap->iv_mgtsend,
344368e8e04eSSam Leffler 			status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
3444e94527beSAdrian Chadd 			ieee80211_tx_mgt_timeout, vap);
3445e94527beSAdrian Chadd 	}
344668e8e04eSSam Leffler }
344768e8e04eSSam Leffler 
3448b032f27cSSam Leffler static void
3449b032f27cSSam Leffler ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
3450210ab3c2SAdrian Chadd 	struct ieee80211_node *ni)
34518a1b9b6aSSam Leffler {
3452b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
3453210ab3c2SAdrian Chadd 	struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
3454b105a069SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
3455b032f27cSSam Leffler 	struct ieee80211_rateset *rs = &ni->ni_rates;
345668e8e04eSSam Leffler 	uint16_t capinfo;
34578a1b9b6aSSam Leffler 
34588a1b9b6aSSam Leffler 	/*
34598a1b9b6aSSam Leffler 	 * beacon frame format
346051172f62SAdrian Chadd 	 *
346151172f62SAdrian Chadd 	 * TODO: update to 802.11-2012; a lot of stuff has changed;
346251172f62SAdrian Chadd 	 * vendor extensions should be at the end, etc.
346351172f62SAdrian Chadd 	 *
34648a1b9b6aSSam Leffler 	 *	[8] time stamp
34658a1b9b6aSSam Leffler 	 *	[2] beacon interval
34668a1b9b6aSSam Leffler 	 *	[2] cabability information
34678a1b9b6aSSam Leffler 	 *	[tlv] ssid
34688a1b9b6aSSam Leffler 	 *	[tlv] supported rates
34698a1b9b6aSSam Leffler 	 *	[3] parameter set (DS)
3470b032f27cSSam Leffler 	 *	[8] CF parameter set (optional)
34718a1b9b6aSSam Leffler 	 *	[tlv] parameter set (IBSS/TIM)
3472b032f27cSSam Leffler 	 *	[tlv] country (optional)
3473b032f27cSSam Leffler 	 *	[3] power control (optional)
3474b032f27cSSam Leffler 	 *	[5] channel switch announcement (CSA) (optional)
347551172f62SAdrian Chadd 	 * XXX TODO: Quiet
347651172f62SAdrian Chadd 	 * XXX TODO: IBSS DFS
347751172f62SAdrian Chadd 	 * XXX TODO: TPC report
34788a1b9b6aSSam Leffler 	 *	[tlv] extended rate phy (ERP)
34798a1b9b6aSSam Leffler 	 *	[tlv] extended supported rates
3480688fe74dSSam Leffler 	 *	[tlv] RSN parameters
348151172f62SAdrian Chadd 	 * XXX TODO: BSSLOAD
348251172f62SAdrian Chadd 	 * (XXX EDCA parameter set, QoS capability?)
348351172f62SAdrian Chadd 	 * XXX TODO: AP channel report
348451172f62SAdrian Chadd 	 *
348568e8e04eSSam Leffler 	 *	[tlv] HT capabilities
348668e8e04eSSam Leffler 	 *	[tlv] HT information
348751172f62SAdrian Chadd 	 *	XXX TODO: 20/40 BSS coexistence
348851172f62SAdrian Chadd 	 * Mesh:
348951172f62SAdrian Chadd 	 * XXX TODO: Meshid
349051172f62SAdrian Chadd 	 * XXX TODO: mesh config
349151172f62SAdrian Chadd 	 * XXX TODO: mesh awake window
349251172f62SAdrian Chadd 	 * XXX TODO: beacon timing (mesh, etc)
349351172f62SAdrian Chadd 	 * XXX TODO: MCCAOP Advertisement Overview
349451172f62SAdrian Chadd 	 * XXX TODO: MCCAOP Advertisement
349551172f62SAdrian Chadd 	 * XXX TODO: Mesh channel switch parameters
349651172f62SAdrian Chadd 	 * VHT:
349751172f62SAdrian Chadd 	 * XXX TODO: VHT capabilities
349851172f62SAdrian Chadd 	 * XXX TODO: VHT operation
349951172f62SAdrian Chadd 	 * XXX TODO: VHT transmit power envelope
350051172f62SAdrian Chadd 	 * XXX TODO: channel switch wrapper element
350151172f62SAdrian Chadd 	 * XXX TODO: extended BSS load element
350251172f62SAdrian Chadd 	 *
3503b032f27cSSam Leffler 	 * XXX Vendor-specific OIDs (e.g. Atheros)
3504b032f27cSSam Leffler 	 *	[tlv] WPA parameters
3505b032f27cSSam Leffler 	 *	[tlv] WME parameters
350668e8e04eSSam Leffler 	 *	[tlv] Vendor OUI HT capabilities (optional)
350768e8e04eSSam Leffler 	 *	[tlv] Vendor OUI HT information (optional)
35084207227cSSam Leffler 	 *	[tlv] Atheros capabilities (optional)
350910ad9a77SSam Leffler 	 *	[tlv] TDMA parameters (optional)
351059aa14a9SRui Paulo 	 *	[tlv] Mesh ID (MBSS)
351159aa14a9SRui Paulo 	 *	[tlv] Mesh Conf (MBSS)
3512b032f27cSSam Leffler 	 *	[tlv] application data (optional)
35138a1b9b6aSSam Leffler 	 */
35148a1b9b6aSSam Leffler 
35151b6167d2SSam Leffler 	memset(bo, 0, sizeof(*bo));
35161b6167d2SSam Leffler 
35178a1b9b6aSSam Leffler 	memset(frm, 0, 8);	/* XXX timestamp is set by hardware/driver */
35188a1b9b6aSSam Leffler 	frm += 8;
351968e8e04eSSam Leffler 	*(uint16_t *)frm = htole16(ni->ni_intval);
35208a1b9b6aSSam Leffler 	frm += 2;
352159aa14a9SRui Paulo 	capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
352268e8e04eSSam Leffler 	bo->bo_caps = (uint16_t *)frm;
352368e8e04eSSam Leffler 	*(uint16_t *)frm = htole16(capinfo);
35248a1b9b6aSSam Leffler 	frm += 2;
35258a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_SSID;
3526b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
35278a1b9b6aSSam Leffler 		*frm++ = ni->ni_esslen;
35288a1b9b6aSSam Leffler 		memcpy(frm, ni->ni_essid, ni->ni_esslen);
35298a1b9b6aSSam Leffler 		frm += ni->ni_esslen;
35308a1b9b6aSSam Leffler 	} else
35318a1b9b6aSSam Leffler 		*frm++ = 0;
35328a1b9b6aSSam Leffler 	frm = ieee80211_add_rates(frm, rs);
3533b032f27cSSam Leffler 	if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
35348a1b9b6aSSam Leffler 		*frm++ = IEEE80211_ELEMID_DSPARMS;
35358a1b9b6aSSam Leffler 		*frm++ = 1;
3536b032f27cSSam Leffler 		*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
3537b032f27cSSam Leffler 	}
3538b032f27cSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PCF) {
3539b032f27cSSam Leffler 		bo->bo_cfp = frm;
3540b032f27cSSam Leffler 		frm = ieee80211_add_cfparms(frm, ic);
35418a1b9b6aSSam Leffler 	}
35428a1b9b6aSSam Leffler 	bo->bo_tim = frm;
3543b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
35448a1b9b6aSSam Leffler 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
35458a1b9b6aSSam Leffler 		*frm++ = 2;
35468a1b9b6aSSam Leffler 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
35478a1b9b6aSSam Leffler 		bo->bo_tim_len = 0;
354859aa14a9SRui Paulo 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
354959aa14a9SRui Paulo 	    vap->iv_opmode == IEEE80211_M_MBSS) {
355059aa14a9SRui Paulo 		/* TIM IE is the same for Mesh and Hostap */
35518a1b9b6aSSam Leffler 		struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
35528a1b9b6aSSam Leffler 
35538a1b9b6aSSam Leffler 		tie->tim_ie = IEEE80211_ELEMID_TIM;
35548a1b9b6aSSam Leffler 		tie->tim_len = 4;	/* length */
35558a1b9b6aSSam Leffler 		tie->tim_count = 0;	/* DTIM count */
3556b032f27cSSam Leffler 		tie->tim_period = vap->iv_dtim_period;	/* DTIM period */
35578a1b9b6aSSam Leffler 		tie->tim_bitctl = 0;	/* bitmap control */
35588a1b9b6aSSam Leffler 		tie->tim_bitmap[0] = 0;	/* Partial Virtual Bitmap */
35598a1b9b6aSSam Leffler 		frm += sizeof(struct ieee80211_tim_ie);
35608a1b9b6aSSam Leffler 		bo->bo_tim_len = 1;
35618a1b9b6aSSam Leffler 	}
3562b105a069SSam Leffler 	bo->bo_tim_trailer = frm;
3563b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
3564b032f27cSSam Leffler 	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
3565b032f27cSSam Leffler 		frm = ieee80211_add_countryie(frm, ic);
3566b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_DOTH) {
3567b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
3568b032f27cSSam Leffler 			frm = ieee80211_add_powerconstraint(frm, vap);
3569b032f27cSSam Leffler 		bo->bo_csa = frm;
3570b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
3571b032f27cSSam Leffler 			frm = ieee80211_add_csa(frm, vap);
3572b032f27cSSam Leffler 	} else
3573b032f27cSSam Leffler 		bo->bo_csa = frm;
357432b0e64bSAdrian Chadd 
35754d3dcce5SAdrian Chadd 	bo->bo_quiet = NULL;
357632b0e64bSAdrian Chadd 	if (vap->iv_flags & IEEE80211_F_DOTH) {
357732b0e64bSAdrian Chadd 		if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
35784d3dcce5SAdrian Chadd 		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
35794d3dcce5SAdrian Chadd 		    (vap->iv_quiet == 1)) {
35804d3dcce5SAdrian Chadd 			/*
35814d3dcce5SAdrian Chadd 			 * We only insert the quiet IE offset if
35824d3dcce5SAdrian Chadd 			 * the quiet IE is enabled.  Otherwise don't
35834d3dcce5SAdrian Chadd 			 * put it here or we'll just overwrite
35844d3dcce5SAdrian Chadd 			 * some other beacon contents.
35854d3dcce5SAdrian Chadd 			 */
35864d3dcce5SAdrian Chadd 			if (vap->iv_quiet) {
35874d3dcce5SAdrian Chadd 				bo->bo_quiet = frm;
3588ce4552cdSAdrian Chadd 				frm = ieee80211_add_quiet(frm,vap, 0);
358932b0e64bSAdrian Chadd 			}
35904d3dcce5SAdrian Chadd 		}
35914d3dcce5SAdrian Chadd 	}
359232b0e64bSAdrian Chadd 
3593b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
35940912bac9SSam Leffler 		bo->bo_erp = frm;
3595f1481c8dSAdrian Chadd 		frm = ieee80211_add_erp(frm, vap);
35961b6167d2SSam Leffler 	}
359768e8e04eSSam Leffler 	frm = ieee80211_add_xrates(frm, rs);
3598d8df5f3dSRui Paulo 	frm = ieee80211_add_rsn(frm, vap);
3599b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
360068e8e04eSSam Leffler 		frm = ieee80211_add_htcap(frm, ni);
360168e8e04eSSam Leffler 		bo->bo_htinfo = frm;
360268e8e04eSSam Leffler 		frm = ieee80211_add_htinfo(frm, ni);
36031b6167d2SSam Leffler 	}
360451172f62SAdrian Chadd 
360551172f62SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
360651172f62SAdrian Chadd 		frm = ieee80211_add_vhtcap(frm, ni);
360751172f62SAdrian Chadd 		bo->bo_vhtinfo = frm;
360851172f62SAdrian Chadd 		frm = ieee80211_add_vhtinfo(frm, ni);
360951172f62SAdrian Chadd 		/* Transmit power envelope */
361051172f62SAdrian Chadd 		/* Channel switch wrapper element */
361151172f62SAdrian Chadd 		/* Extended bss load element */
361251172f62SAdrian Chadd 	}
361351172f62SAdrian Chadd 
3614d8df5f3dSRui Paulo 	frm = ieee80211_add_wpa(frm, vap);
3615b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WME) {
36161b6167d2SSam Leffler 		bo->bo_wme = frm;
36178379e8dbSAdrian Chadd 		frm = ieee80211_add_wme_param(frm, &ic->ic_wme,
36188379e8dbSAdrian Chadd 		    !! (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD));
36191b6167d2SSam Leffler 	}
3620b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
36212bfc8a91SSam Leffler 	    (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
362268e8e04eSSam Leffler 		frm = ieee80211_add_htcap_vendor(frm, ni);
362368e8e04eSSam Leffler 		frm = ieee80211_add_htinfo_vendor(frm, ni);
36240912bac9SSam Leffler 	}
362551172f62SAdrian Chadd 
36264207227cSSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
36274207227cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_ATHEROS) {
36284207227cSSam Leffler 		bo->bo_ath = frm;
36294207227cSSam Leffler 		frm = ieee80211_add_athcaps(frm, ni);
36304207227cSSam Leffler 	}
36314207227cSSam Leffler #endif
363210ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
363310ad9a77SSam Leffler 	if (vap->iv_caps & IEEE80211_C_TDMA) {
363410ad9a77SSam Leffler 		bo->bo_tdma = frm;
363510ad9a77SSam Leffler 		frm = ieee80211_add_tdma(frm, vap);
363610ad9a77SSam Leffler 	}
363710ad9a77SSam Leffler #endif
3638b032f27cSSam Leffler 	if (vap->iv_appie_beacon != NULL) {
3639b032f27cSSam Leffler 		bo->bo_appie = frm;
3640b032f27cSSam Leffler 		bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
3641b032f27cSSam Leffler 		frm = add_appie(frm, vap->iv_appie_beacon);
3642b032f27cSSam Leffler 	}
364351172f62SAdrian Chadd 
364451172f62SAdrian Chadd 	/* XXX TODO: move meshid/meshconf up to before vendor extensions? */
364559aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
364659aa14a9SRui Paulo 	if (vap->iv_opmode == IEEE80211_M_MBSS) {
364759aa14a9SRui Paulo 		frm = ieee80211_add_meshid(frm, vap);
3648d093681cSRui Paulo 		bo->bo_meshconf = frm;
364959aa14a9SRui Paulo 		frm = ieee80211_add_meshconf(frm, vap);
365059aa14a9SRui Paulo 	}
365159aa14a9SRui Paulo #endif
3652b105a069SSam Leffler 	bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
3653b032f27cSSam Leffler 	bo->bo_csa_trailer_len = frm - bo->bo_csa;
365468e8e04eSSam Leffler 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3655b032f27cSSam Leffler }
3656b032f27cSSam Leffler 
3657b032f27cSSam Leffler /*
3658b032f27cSSam Leffler  * Allocate a beacon frame and fillin the appropriate bits.
3659b032f27cSSam Leffler  */
3660b032f27cSSam Leffler struct mbuf *
3661210ab3c2SAdrian Chadd ieee80211_beacon_alloc(struct ieee80211_node *ni)
3662b032f27cSSam Leffler {
3663b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
3664b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
3665b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
3666b032f27cSSam Leffler 	struct ieee80211_frame *wh;
3667b032f27cSSam Leffler 	struct mbuf *m;
3668b032f27cSSam Leffler 	int pktlen;
3669b032f27cSSam Leffler 	uint8_t *frm;
3670b032f27cSSam Leffler 
3671b032f27cSSam Leffler 	/*
36724d3dcce5SAdrian Chadd 	 * Update the "We're putting the quiet IE in the beacon" state.
36734d3dcce5SAdrian Chadd 	 */
36744d3dcce5SAdrian Chadd 	if (vap->iv_quiet == 1)
36754d3dcce5SAdrian Chadd 		vap->iv_flags_ext |= IEEE80211_FEXT_QUIET_IE;
36764d3dcce5SAdrian Chadd 	else if (vap->iv_quiet == 0)
36774d3dcce5SAdrian Chadd 		vap->iv_flags_ext &= ~IEEE80211_FEXT_QUIET_IE;
36784d3dcce5SAdrian Chadd 
36794d3dcce5SAdrian Chadd 	/*
3680b032f27cSSam Leffler 	 * beacon frame format
368151172f62SAdrian Chadd 	 *
368251172f62SAdrian Chadd 	 * Note: This needs updating for 802.11-2012.
368351172f62SAdrian Chadd 	 *
3684b032f27cSSam Leffler 	 *	[8] time stamp
3685b032f27cSSam Leffler 	 *	[2] beacon interval
3686b032f27cSSam Leffler 	 *	[2] cabability information
3687b032f27cSSam Leffler 	 *	[tlv] ssid
3688b032f27cSSam Leffler 	 *	[tlv] supported rates
3689b032f27cSSam Leffler 	 *	[3] parameter set (DS)
3690b032f27cSSam Leffler 	 *	[8] CF parameter set (optional)
3691b032f27cSSam Leffler 	 *	[tlv] parameter set (IBSS/TIM)
3692b032f27cSSam Leffler 	 *	[tlv] country (optional)
3693b032f27cSSam Leffler 	 *	[3] power control (optional)
3694b032f27cSSam Leffler 	 *	[5] channel switch announcement (CSA) (optional)
3695b032f27cSSam Leffler 	 *	[tlv] extended rate phy (ERP)
3696b032f27cSSam Leffler 	 *	[tlv] extended supported rates
3697b032f27cSSam Leffler 	 *	[tlv] RSN parameters
3698b032f27cSSam Leffler 	 *	[tlv] HT capabilities
3699b032f27cSSam Leffler 	 *	[tlv] HT information
370051172f62SAdrian Chadd 	 *	[tlv] VHT capabilities
370151172f62SAdrian Chadd 	 *	[tlv] VHT operation
3702b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT capabilities (optional)
3703b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT information (optional)
3704b032f27cSSam Leffler 	 * XXX Vendor-specific OIDs (e.g. Atheros)
3705b032f27cSSam Leffler 	 *	[tlv] WPA parameters
3706b032f27cSSam Leffler 	 *	[tlv] WME parameters
370710ad9a77SSam Leffler 	 *	[tlv] TDMA parameters (optional)
370859aa14a9SRui Paulo 	 *	[tlv] Mesh ID (MBSS)
370959aa14a9SRui Paulo 	 *	[tlv] Mesh Conf (MBSS)
3710b032f27cSSam Leffler 	 *	[tlv] application data (optional)
3711b032f27cSSam Leffler 	 * NB: we allocate the max space required for the TIM bitmap.
3712b032f27cSSam Leffler 	 * XXX how big is this?
3713b032f27cSSam Leffler 	 */
3714b032f27cSSam Leffler 	pktlen =   8					/* time stamp */
3715b032f27cSSam Leffler 		 + sizeof(uint16_t)			/* beacon interval */
3716b032f27cSSam Leffler 		 + sizeof(uint16_t)			/* capabilities */
3717b032f27cSSam Leffler 		 + 2 + ni->ni_esslen			/* ssid */
3718b032f27cSSam Leffler 	         + 2 + IEEE80211_RATE_SIZE		/* supported rates */
3719b032f27cSSam Leffler 	         + 2 + 1				/* DS parameters */
3720b032f27cSSam Leffler 		 + 2 + 6				/* CF parameters */
3721b032f27cSSam Leffler 		 + 2 + 4 + vap->iv_tim_len		/* DTIM/IBSSPARMS */
3722b032f27cSSam Leffler 		 + IEEE80211_COUNTRY_MAX_SIZE		/* country */
3723b032f27cSSam Leffler 		 + 2 + 1				/* power control */
3724b032f27cSSam Leffler 		 + sizeof(struct ieee80211_csa_ie)	/* CSA */
372532b0e64bSAdrian Chadd 		 + sizeof(struct ieee80211_quiet_ie)	/* Quiet */
3726b032f27cSSam Leffler 		 + 2 + 1				/* ERP */
3727b032f27cSSam Leffler 	         + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
3728b032f27cSSam Leffler 		 + (vap->iv_caps & IEEE80211_C_WPA ?	/* WPA 1+2 */
3729b032f27cSSam Leffler 			2*sizeof(struct ieee80211_ie_wpa) : 0)
3730b032f27cSSam Leffler 		 /* XXX conditional? */
3731b032f27cSSam Leffler 		 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
3732b032f27cSSam Leffler 		 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
373351172f62SAdrian Chadd 		 + sizeof(struct ieee80211_ie_vhtcap)/* VHT caps */
373451172f62SAdrian Chadd 		 + sizeof(struct ieee80211_ie_vht_operation)/* VHT info */
3735b032f27cSSam Leffler 		 + (vap->iv_caps & IEEE80211_C_WME ?	/* WME */
3736b032f27cSSam Leffler 			sizeof(struct ieee80211_wme_param) : 0)
37374207227cSSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
37384207227cSSam Leffler 		 + sizeof(struct ieee80211_ath_ie)	/* ATH */
37394207227cSSam Leffler #endif
374010ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
374110ad9a77SSam Leffler 		 + (vap->iv_caps & IEEE80211_C_TDMA ?	/* TDMA */
374210ad9a77SSam Leffler 			sizeof(struct ieee80211_tdma_param) : 0)
374310ad9a77SSam Leffler #endif
374459aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
374559aa14a9SRui Paulo 		 + 2 + ni->ni_meshidlen
374659aa14a9SRui Paulo 		 + sizeof(struct ieee80211_meshconf_ie)
374759aa14a9SRui Paulo #endif
3748b032f27cSSam Leffler 		 + IEEE80211_MAX_APPIE
3749b032f27cSSam Leffler 		 ;
3750b032f27cSSam Leffler 	m = ieee80211_getmgtframe(&frm,
3751b032f27cSSam Leffler 		ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
3752b032f27cSSam Leffler 	if (m == NULL) {
3753b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
3754b032f27cSSam Leffler 			"%s: cannot get buf; size %u\n", __func__, pktlen);
3755b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
3756b032f27cSSam Leffler 		return NULL;
3757b032f27cSSam Leffler 	}
3758210ab3c2SAdrian Chadd 	ieee80211_beacon_construct(m, frm, ni);
37598a1b9b6aSSam Leffler 
3760bd29f817SBjoern A. Zeeb 	M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT);
37618a1b9b6aSSam Leffler 	KASSERT(m != NULL, ("no space for 802.11 header?"));
37628a1b9b6aSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
37638a1b9b6aSSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
37648a1b9b6aSSam Leffler 	    IEEE80211_FC0_SUBTYPE_BEACON;
37658a1b9b6aSSam Leffler 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
376668e8e04eSSam Leffler 	*(uint16_t *)wh->i_dur = 0;
37678a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
3768b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
37698a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
377068e8e04eSSam Leffler 	*(uint16_t *)wh->i_seq = 0;
37718a1b9b6aSSam Leffler 
37728a1b9b6aSSam Leffler 	return m;
37738a1b9b6aSSam Leffler }
37748a1b9b6aSSam Leffler 
37758a1b9b6aSSam Leffler /*
37768a1b9b6aSSam Leffler  * Update the dynamic parts of a beacon frame based on the current state.
37778a1b9b6aSSam Leffler  */
37788a1b9b6aSSam Leffler int
3779210ab3c2SAdrian Chadd ieee80211_beacon_update(struct ieee80211_node *ni, struct mbuf *m, int mcast)
37808a1b9b6aSSam Leffler {
3781b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
3782210ab3c2SAdrian Chadd 	struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
3783b105a069SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
37848a1b9b6aSSam Leffler 	int len_changed = 0;
378568e8e04eSSam Leffler 	uint16_t capinfo;
3786fa3324c9SAdrian Chadd 	struct ieee80211_frame *wh;
3787fa3324c9SAdrian Chadd 	ieee80211_seq seqno;
37888a1b9b6aSSam Leffler 
3789b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
3790b032f27cSSam Leffler 	/*
3791b032f27cSSam Leffler 	 * Handle 11h channel change when we've reached the count.
3792b032f27cSSam Leffler 	 * We must recalculate the beacon frame contents to account
3793b032f27cSSam Leffler 	 * for the new channel.  Note we do this only for the first
3794b032f27cSSam Leffler 	 * vap that reaches this point; subsequent vaps just update
3795b032f27cSSam Leffler 	 * their beacon state to reflect the recalculated channel.
3796b032f27cSSam Leffler 	 */
3797b032f27cSSam Leffler 	if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
3798b032f27cSSam Leffler 	    vap->iv_csa_count == ic->ic_csa_count) {
3799b032f27cSSam Leffler 		vap->iv_csa_count = 0;
3800b032f27cSSam Leffler 		/*
3801b032f27cSSam Leffler 		 * Effect channel change before reconstructing the beacon
3802b032f27cSSam Leffler 		 * frame contents as many places reference ni_chan.
3803b032f27cSSam Leffler 		 */
3804b032f27cSSam Leffler 		if (ic->ic_csa_newchan != NULL)
3805b032f27cSSam Leffler 			ieee80211_csa_completeswitch(ic);
3806b032f27cSSam Leffler 		/*
3807b032f27cSSam Leffler 		 * NB: ieee80211_beacon_construct clears all pending
3808b032f27cSSam Leffler 		 * updates in bo_flags so we don't need to explicitly
3809b032f27cSSam Leffler 		 * clear IEEE80211_BEACON_CSA.
3810b032f27cSSam Leffler 		 */
3811b032f27cSSam Leffler 		ieee80211_beacon_construct(m,
3812210ab3c2SAdrian Chadd 		    mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
3813b032f27cSSam Leffler 
3814b032f27cSSam Leffler 		/* XXX do WME aggressive mode processing? */
3815b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
3816b032f27cSSam Leffler 		return 1;		/* just assume length changed */
3817b032f27cSSam Leffler 	}
3818b032f27cSSam Leffler 
38194d3dcce5SAdrian Chadd 	/*
38204d3dcce5SAdrian Chadd 	 * Handle the quiet time element being added and removed.
38214d3dcce5SAdrian Chadd 	 * Again, for now we just cheat and reconstruct the whole
38224d3dcce5SAdrian Chadd 	 * beacon - that way the gap is provided as appropriate.
38234d3dcce5SAdrian Chadd 	 *
38244d3dcce5SAdrian Chadd 	 * So, track whether we have already added the IE versus
38254d3dcce5SAdrian Chadd 	 * whether we want to be adding the IE.
38264d3dcce5SAdrian Chadd 	 */
38274d3dcce5SAdrian Chadd 	if ((vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE) &&
38284d3dcce5SAdrian Chadd 	    (vap->iv_quiet == 0)) {
38294d3dcce5SAdrian Chadd 		/*
38304d3dcce5SAdrian Chadd 		 * Quiet time beacon IE enabled, but it's disabled;
38314d3dcce5SAdrian Chadd 		 * recalc
38324d3dcce5SAdrian Chadd 		 */
38334d3dcce5SAdrian Chadd 		vap->iv_flags_ext &= ~IEEE80211_FEXT_QUIET_IE;
38344d3dcce5SAdrian Chadd 		ieee80211_beacon_construct(m,
38354d3dcce5SAdrian Chadd 		    mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
38364d3dcce5SAdrian Chadd 		/* XXX do WME aggressive mode processing? */
38374d3dcce5SAdrian Chadd 		IEEE80211_UNLOCK(ic);
38384d3dcce5SAdrian Chadd 		return 1;		/* just assume length changed */
38394d3dcce5SAdrian Chadd 	}
38404d3dcce5SAdrian Chadd 
38414d3dcce5SAdrian Chadd 	if (((vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE) == 0) &&
38424d3dcce5SAdrian Chadd 	    (vap->iv_quiet == 1)) {
38434d3dcce5SAdrian Chadd 		/*
38444d3dcce5SAdrian Chadd 		 * Quiet time beacon IE disabled, but it's now enabled;
38454d3dcce5SAdrian Chadd 		 * recalc
38464d3dcce5SAdrian Chadd 		 */
38474d3dcce5SAdrian Chadd 		vap->iv_flags_ext |= IEEE80211_FEXT_QUIET_IE;
38484d3dcce5SAdrian Chadd 		ieee80211_beacon_construct(m,
38494d3dcce5SAdrian Chadd 		    mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
38504d3dcce5SAdrian Chadd 		/* XXX do WME aggressive mode processing? */
38514d3dcce5SAdrian Chadd 		IEEE80211_UNLOCK(ic);
38524d3dcce5SAdrian Chadd 		return 1;		/* just assume length changed */
38534d3dcce5SAdrian Chadd 	}
38544d3dcce5SAdrian Chadd 
3855fa3324c9SAdrian Chadd 	wh = mtod(m, struct ieee80211_frame *);
385651172f62SAdrian Chadd 
385751172f62SAdrian Chadd 	/*
385851172f62SAdrian Chadd 	 * XXX TODO Strictly speaking this should be incremented with the TX
385951172f62SAdrian Chadd 	 * lock held so as to serialise access to the non-qos TID sequence
386051172f62SAdrian Chadd 	 * number space.
386151172f62SAdrian Chadd 	 *
386251172f62SAdrian Chadd 	 * If the driver identifies it does its own TX seqno management then
386351172f62SAdrian Chadd 	 * we can skip this (and still not do the TX seqno.)
386451172f62SAdrian Chadd 	 */
3865fa3324c9SAdrian Chadd 	seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
3866fa3324c9SAdrian Chadd 	*(uint16_t *)&wh->i_seq[0] =
3867fa3324c9SAdrian Chadd 		htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
3868fa3324c9SAdrian Chadd 	M_SEQNO_SET(m, seqno);
3869fa3324c9SAdrian Chadd 
38708a1b9b6aSSam Leffler 	/* XXX faster to recalculate entirely or just changes? */
387159aa14a9SRui Paulo 	capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
38728a1b9b6aSSam Leffler 	*bo->bo_caps = htole16(capinfo);
38738a1b9b6aSSam Leffler 
3874b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WME) {
38758a1b9b6aSSam Leffler 		struct ieee80211_wme_state *wme = &ic->ic_wme;
38768a1b9b6aSSam Leffler 
38778a1b9b6aSSam Leffler 		/*
3878a4641f4eSPedro F. Giffuni 		 * Check for aggressive mode change.  When there is
38798a1b9b6aSSam Leffler 		 * significant high priority traffic in the BSS
38808a1b9b6aSSam Leffler 		 * throttle back BE traffic by using conservative
3881a4641f4eSPedro F. Giffuni 		 * parameters.  Otherwise BE uses aggressive params
38828a1b9b6aSSam Leffler 		 * to optimize performance of legacy/non-QoS traffic.
38838a1b9b6aSSam Leffler 		 */
38848a1b9b6aSSam Leffler 		if (wme->wme_flags & WME_F_AGGRMODE) {
38858a1b9b6aSSam Leffler 			if (wme->wme_hipri_traffic >
38868a1b9b6aSSam Leffler 			    wme->wme_hipri_switch_thresh) {
3887b032f27cSSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
38888a1b9b6aSSam Leffler 				    "%s: traffic %u, disable aggressive mode\n",
38898a1b9b6aSSam Leffler 				    __func__, wme->wme_hipri_traffic);
38908a1b9b6aSSam Leffler 				wme->wme_flags &= ~WME_F_AGGRMODE;
3891b032f27cSSam Leffler 				ieee80211_wme_updateparams_locked(vap);
38928a1b9b6aSSam Leffler 				wme->wme_hipri_traffic =
38938a1b9b6aSSam Leffler 					wme->wme_hipri_switch_hysteresis;
38948a1b9b6aSSam Leffler 			} else
38958a1b9b6aSSam Leffler 				wme->wme_hipri_traffic = 0;
38968a1b9b6aSSam Leffler 		} else {
38978a1b9b6aSSam Leffler 			if (wme->wme_hipri_traffic <=
38988a1b9b6aSSam Leffler 			    wme->wme_hipri_switch_thresh) {
3899b032f27cSSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
39008a1b9b6aSSam Leffler 				    "%s: traffic %u, enable aggressive mode\n",
39018a1b9b6aSSam Leffler 				    __func__, wme->wme_hipri_traffic);
39028a1b9b6aSSam Leffler 				wme->wme_flags |= WME_F_AGGRMODE;
3903b032f27cSSam Leffler 				ieee80211_wme_updateparams_locked(vap);
39048a1b9b6aSSam Leffler 				wme->wme_hipri_traffic = 0;
39058a1b9b6aSSam Leffler 			} else
39068a1b9b6aSSam Leffler 				wme->wme_hipri_traffic =
39078a1b9b6aSSam Leffler 					wme->wme_hipri_switch_hysteresis;
39088a1b9b6aSSam Leffler 		}
3909b105a069SSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
39108379e8dbSAdrian Chadd 			(void) ieee80211_add_wme_param(bo->bo_wme, wme,
39118379e8dbSAdrian Chadd 			  vap->iv_flags_ext & IEEE80211_FEXT_UAPSD);
3912b105a069SSam Leffler 			clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
39138a1b9b6aSSam Leffler 		}
39148a1b9b6aSSam Leffler 	}
39158a1b9b6aSSam Leffler 
3916b105a069SSam Leffler 	if (isset(bo->bo_flags,  IEEE80211_BEACON_HTINFO)) {
3917b032f27cSSam Leffler 		ieee80211_ht_update_beacon(vap, bo);
3918b105a069SSam Leffler 		clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
391968e8e04eSSam Leffler 	}
392010ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
392110ad9a77SSam Leffler 	if (vap->iv_caps & IEEE80211_C_TDMA) {
392210ad9a77SSam Leffler 		/*
392310ad9a77SSam Leffler 		 * NB: the beacon is potentially updated every TBTT.
392410ad9a77SSam Leffler 		 */
392510ad9a77SSam Leffler 		ieee80211_tdma_update_beacon(vap, bo);
392610ad9a77SSam Leffler 	}
392710ad9a77SSam Leffler #endif
3928d093681cSRui Paulo #ifdef IEEE80211_SUPPORT_MESH
3929d093681cSRui Paulo 	if (vap->iv_opmode == IEEE80211_M_MBSS)
3930d093681cSRui Paulo 		ieee80211_mesh_update_beacon(vap, bo);
3931d093681cSRui Paulo #endif
3932d093681cSRui Paulo 
393359aa14a9SRui Paulo 	if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
393459aa14a9SRui Paulo 	    vap->iv_opmode == IEEE80211_M_MBSS) {	/* NB: no IBSS support*/
39358a1b9b6aSSam Leffler 		struct ieee80211_tim_ie *tie =
39368a1b9b6aSSam Leffler 			(struct ieee80211_tim_ie *) bo->bo_tim;
3937b105a069SSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
39388a1b9b6aSSam Leffler 			u_int timlen, timoff, i;
39398a1b9b6aSSam Leffler 			/*
39408a1b9b6aSSam Leffler 			 * ATIM/DTIM needs updating.  If it fits in the
39418a1b9b6aSSam Leffler 			 * current space allocated then just copy in the
39428a1b9b6aSSam Leffler 			 * new bits.  Otherwise we need to move any trailing
39438a1b9b6aSSam Leffler 			 * data to make room.  Note that we know there is
39448a1b9b6aSSam Leffler 			 * contiguous space because ieee80211_beacon_allocate
39458a1b9b6aSSam Leffler 			 * insures there is space in the mbuf to write a
3946b032f27cSSam Leffler 			 * maximal-size virtual bitmap (based on iv_max_aid).
39478a1b9b6aSSam Leffler 			 */
39488a1b9b6aSSam Leffler 			/*
39498a1b9b6aSSam Leffler 			 * Calculate the bitmap size and offset, copy any
39508a1b9b6aSSam Leffler 			 * trailer out of the way, and then copy in the
39518a1b9b6aSSam Leffler 			 * new bitmap and update the information element.
39528a1b9b6aSSam Leffler 			 * Note that the tim bitmap must contain at least
39538a1b9b6aSSam Leffler 			 * one byte and any offset must be even.
39548a1b9b6aSSam Leffler 			 */
3955b032f27cSSam Leffler 			if (vap->iv_ps_pending != 0) {
39568a1b9b6aSSam Leffler 				timoff = 128;		/* impossibly large */
3957b032f27cSSam Leffler 				for (i = 0; i < vap->iv_tim_len; i++)
3958b032f27cSSam Leffler 					if (vap->iv_tim_bitmap[i]) {
39598a1b9b6aSSam Leffler 						timoff = i &~ 1;
39608a1b9b6aSSam Leffler 						break;
39618a1b9b6aSSam Leffler 					}
39628a1b9b6aSSam Leffler 				KASSERT(timoff != 128, ("tim bitmap empty!"));
3963b032f27cSSam Leffler 				for (i = vap->iv_tim_len-1; i >= timoff; i--)
3964b032f27cSSam Leffler 					if (vap->iv_tim_bitmap[i])
39658a1b9b6aSSam Leffler 						break;
39668a1b9b6aSSam Leffler 				timlen = 1 + (i - timoff);
39678a1b9b6aSSam Leffler 			} else {
39688a1b9b6aSSam Leffler 				timoff = 0;
39698a1b9b6aSSam Leffler 				timlen = 1;
39708a1b9b6aSSam Leffler 			}
397151172f62SAdrian Chadd 
397251172f62SAdrian Chadd 			/*
397351172f62SAdrian Chadd 			 * TODO: validate this!
397451172f62SAdrian Chadd 			 */
39758a1b9b6aSSam Leffler 			if (timlen != bo->bo_tim_len) {
39768a1b9b6aSSam Leffler 				/* copy up/down trailer */
39770912bac9SSam Leffler 				int adjust = tie->tim_bitmap+timlen
3978b105a069SSam Leffler 					   - bo->bo_tim_trailer;
3979b105a069SSam Leffler 				ovbcopy(bo->bo_tim_trailer,
3980b105a069SSam Leffler 				    bo->bo_tim_trailer+adjust,
3981b105a069SSam Leffler 				    bo->bo_tim_trailer_len);
3982b105a069SSam Leffler 				bo->bo_tim_trailer += adjust;
39830912bac9SSam Leffler 				bo->bo_erp += adjust;
398468e8e04eSSam Leffler 				bo->bo_htinfo += adjust;
398551172f62SAdrian Chadd 				bo->bo_vhtinfo += adjust;
398602e69b54SBernhard Schmidt #ifdef IEEE80211_SUPPORT_SUPERG
39874207227cSSam Leffler 				bo->bo_ath += adjust;
39884207227cSSam Leffler #endif
398902e69b54SBernhard Schmidt #ifdef IEEE80211_SUPPORT_TDMA
399092e870edSSam Leffler 				bo->bo_tdma += adjust;
399192e870edSSam Leffler #endif
399202e69b54SBernhard Schmidt #ifdef IEEE80211_SUPPORT_MESH
3993d093681cSRui Paulo 				bo->bo_meshconf += adjust;
3994d093681cSRui Paulo #endif
3995b032f27cSSam Leffler 				bo->bo_appie += adjust;
3996b032f27cSSam Leffler 				bo->bo_wme += adjust;
3997b032f27cSSam Leffler 				bo->bo_csa += adjust;
399832b0e64bSAdrian Chadd 				bo->bo_quiet += adjust;
39998a1b9b6aSSam Leffler 				bo->bo_tim_len = timlen;
40008a1b9b6aSSam Leffler 
40018a1b9b6aSSam Leffler 				/* update information element */
40028a1b9b6aSSam Leffler 				tie->tim_len = 3 + timlen;
40038a1b9b6aSSam Leffler 				tie->tim_bitctl = timoff;
40048a1b9b6aSSam Leffler 				len_changed = 1;
40058a1b9b6aSSam Leffler 			}
4006b032f27cSSam Leffler 			memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
40078a1b9b6aSSam Leffler 				bo->bo_tim_len);
40088a1b9b6aSSam Leffler 
4009b105a069SSam Leffler 			clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
40108a1b9b6aSSam Leffler 
4011b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
40128a1b9b6aSSam Leffler 				"%s: TIM updated, pending %u, off %u, len %u\n",
4013b032f27cSSam Leffler 				__func__, vap->iv_ps_pending, timoff, timlen);
40148a1b9b6aSSam Leffler 		}
40158a1b9b6aSSam Leffler 		/* count down DTIM period */
40168a1b9b6aSSam Leffler 		if (tie->tim_count == 0)
40178a1b9b6aSSam Leffler 			tie->tim_count = tie->tim_period - 1;
40188a1b9b6aSSam Leffler 		else
40198a1b9b6aSSam Leffler 			tie->tim_count--;
40208a1b9b6aSSam Leffler 		/* update state for buffered multicast frames on DTIM */
4021a196b35fSSam Leffler 		if (mcast && tie->tim_count == 0)
40228a1b9b6aSSam Leffler 			tie->tim_bitctl |= 1;
40238a1b9b6aSSam Leffler 		else
40248a1b9b6aSSam Leffler 			tie->tim_bitctl &= ~1;
4025b032f27cSSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
4026b032f27cSSam Leffler 			struct ieee80211_csa_ie *csa =
4027b032f27cSSam Leffler 			    (struct ieee80211_csa_ie *) bo->bo_csa;
4028b032f27cSSam Leffler 
4029b032f27cSSam Leffler 			/*
4030b032f27cSSam Leffler 			 * Insert or update CSA ie.  If we're just starting
4031b032f27cSSam Leffler 			 * to count down to the channel switch then we need
4032b032f27cSSam Leffler 			 * to insert the CSA ie.  Otherwise we just need to
4033b032f27cSSam Leffler 			 * drop the count.  The actual change happens above
4034b032f27cSSam Leffler 			 * when the vap's count reaches the target count.
4035b032f27cSSam Leffler 			 */
4036b032f27cSSam Leffler 			if (vap->iv_csa_count == 0) {
4037b032f27cSSam Leffler 				memmove(&csa[1], csa, bo->bo_csa_trailer_len);
4038b032f27cSSam Leffler 				bo->bo_erp += sizeof(*csa);
4039d01b3c26SSam Leffler 				bo->bo_htinfo += sizeof(*csa);
404051172f62SAdrian Chadd 				bo->bo_vhtinfo += sizeof(*csa);
4041b032f27cSSam Leffler 				bo->bo_wme += sizeof(*csa);
404202e69b54SBernhard Schmidt #ifdef IEEE80211_SUPPORT_SUPERG
40434207227cSSam Leffler 				bo->bo_ath += sizeof(*csa);
40444207227cSSam Leffler #endif
404502e69b54SBernhard Schmidt #ifdef IEEE80211_SUPPORT_TDMA
404692e870edSSam Leffler 				bo->bo_tdma += sizeof(*csa);
404792e870edSSam Leffler #endif
404802e69b54SBernhard Schmidt #ifdef IEEE80211_SUPPORT_MESH
4049d093681cSRui Paulo 				bo->bo_meshconf += sizeof(*csa);
4050d093681cSRui Paulo #endif
4051b032f27cSSam Leffler 				bo->bo_appie += sizeof(*csa);
4052b032f27cSSam Leffler 				bo->bo_csa_trailer_len += sizeof(*csa);
405332b0e64bSAdrian Chadd 				bo->bo_quiet += sizeof(*csa);
4054b032f27cSSam Leffler 				bo->bo_tim_trailer_len += sizeof(*csa);
4055b032f27cSSam Leffler 				m->m_len += sizeof(*csa);
4056b032f27cSSam Leffler 				m->m_pkthdr.len += sizeof(*csa);
4057b032f27cSSam Leffler 
4058b032f27cSSam Leffler 				ieee80211_add_csa(bo->bo_csa, vap);
4059b032f27cSSam Leffler 			} else
4060b032f27cSSam Leffler 				csa->csa_count--;
4061b032f27cSSam Leffler 			vap->iv_csa_count++;
4062b032f27cSSam Leffler 			/* NB: don't clear IEEE80211_BEACON_CSA */
4063b032f27cSSam Leffler 		}
40644d3dcce5SAdrian Chadd 
40654d3dcce5SAdrian Chadd 		/*
40664d3dcce5SAdrian Chadd 		 * Only add the quiet time IE if we've enabled it
40674d3dcce5SAdrian Chadd 		 * as appropriate.
40684d3dcce5SAdrian Chadd 		 */
406932b0e64bSAdrian Chadd 		if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
407032b0e64bSAdrian Chadd 		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
40714d3dcce5SAdrian Chadd 			if (vap->iv_quiet &&
40724d3dcce5SAdrian Chadd 			    (vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE)) {
4073ce4552cdSAdrian Chadd 				ieee80211_add_quiet(bo->bo_quiet, vap, 1);
407432b0e64bSAdrian Chadd 			}
40754d3dcce5SAdrian Chadd 		}
4076b105a069SSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
40770912bac9SSam Leffler 			/*
40780912bac9SSam Leffler 			 * ERP element needs updating.
40790912bac9SSam Leffler 			 */
4080f1481c8dSAdrian Chadd 			(void) ieee80211_add_erp(bo->bo_erp, vap);
4081b105a069SSam Leffler 			clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
40820912bac9SSam Leffler 		}
40834207227cSSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
40844207227cSSam Leffler 		if (isset(bo->bo_flags,  IEEE80211_BEACON_ATH)) {
40854207227cSSam Leffler 			ieee80211_add_athcaps(bo->bo_ath, ni);
40864207227cSSam Leffler 			clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
40874207227cSSam Leffler 		}
40884207227cSSam Leffler #endif
40898a1b9b6aSSam Leffler 	}
4090b032f27cSSam Leffler 	if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
4091b032f27cSSam Leffler 		const struct ieee80211_appie *aie = vap->iv_appie_beacon;
4092b032f27cSSam Leffler 		int aielen;
4093b032f27cSSam Leffler 		uint8_t *frm;
4094b032f27cSSam Leffler 
4095b032f27cSSam Leffler 		aielen = 0;
4096b032f27cSSam Leffler 		if (aie != NULL)
4097b032f27cSSam Leffler 			aielen += aie->ie_len;
4098b032f27cSSam Leffler 		if (aielen != bo->bo_appie_len) {
4099b032f27cSSam Leffler 			/* copy up/down trailer */
4100b032f27cSSam Leffler 			int adjust = aielen - bo->bo_appie_len;
4101b032f27cSSam Leffler 			ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
4102b032f27cSSam Leffler 				bo->bo_tim_trailer_len);
4103b032f27cSSam Leffler 			bo->bo_tim_trailer += adjust;
4104b032f27cSSam Leffler 			bo->bo_appie += adjust;
4105b032f27cSSam Leffler 			bo->bo_appie_len = aielen;
4106b032f27cSSam Leffler 
4107b032f27cSSam Leffler 			len_changed = 1;
4108b032f27cSSam Leffler 		}
4109b032f27cSSam Leffler 		frm = bo->bo_appie;
4110b032f27cSSam Leffler 		if (aie != NULL)
4111b032f27cSSam Leffler 			frm  = add_appie(frm, aie);
4112b032f27cSSam Leffler 		clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
4113b032f27cSSam Leffler 	}
4114b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
41158a1b9b6aSSam Leffler 
41168a1b9b6aSSam Leffler 	return len_changed;
41178a1b9b6aSSam Leffler }
411874b4c76eSAdrian Chadd 
411974b4c76eSAdrian Chadd /*
412074b4c76eSAdrian Chadd  * Do Ethernet-LLC encapsulation for each payload in a fast frame
412174b4c76eSAdrian Chadd  * tunnel encapsulation.  The frame is assumed to have an Ethernet
412274b4c76eSAdrian Chadd  * header at the front that must be stripped before prepending the
412374b4c76eSAdrian Chadd  * LLC followed by the Ethernet header passed in (with an Ethernet
412474b4c76eSAdrian Chadd  * type that specifies the payload size).
412574b4c76eSAdrian Chadd  */
412674b4c76eSAdrian Chadd struct mbuf *
412774b4c76eSAdrian Chadd ieee80211_ff_encap1(struct ieee80211vap *vap, struct mbuf *m,
412874b4c76eSAdrian Chadd 	const struct ether_header *eh)
412974b4c76eSAdrian Chadd {
413074b4c76eSAdrian Chadd 	struct llc *llc;
413174b4c76eSAdrian Chadd 	uint16_t payload;
413274b4c76eSAdrian Chadd 
413374b4c76eSAdrian Chadd 	/* XXX optimize by combining m_adj+M_PREPEND */
413474b4c76eSAdrian Chadd 	m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
413574b4c76eSAdrian Chadd 	llc = mtod(m, struct llc *);
413674b4c76eSAdrian Chadd 	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
413774b4c76eSAdrian Chadd 	llc->llc_control = LLC_UI;
413874b4c76eSAdrian Chadd 	llc->llc_snap.org_code[0] = 0;
413974b4c76eSAdrian Chadd 	llc->llc_snap.org_code[1] = 0;
414074b4c76eSAdrian Chadd 	llc->llc_snap.org_code[2] = 0;
414174b4c76eSAdrian Chadd 	llc->llc_snap.ether_type = eh->ether_type;
414274b4c76eSAdrian Chadd 	payload = m->m_pkthdr.len;		/* NB: w/o Ethernet header */
414374b4c76eSAdrian Chadd 
4144bd29f817SBjoern A. Zeeb 	M_PREPEND(m, sizeof(struct ether_header), IEEE80211_M_NOWAIT);
414574b4c76eSAdrian Chadd 	if (m == NULL) {		/* XXX cannot happen */
414674b4c76eSAdrian Chadd 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
414774b4c76eSAdrian Chadd 			"%s: no space for ether_header\n", __func__);
414874b4c76eSAdrian Chadd 		vap->iv_stats.is_tx_nobuf++;
414974b4c76eSAdrian Chadd 		return NULL;
415074b4c76eSAdrian Chadd 	}
415174b4c76eSAdrian Chadd 	ETHER_HEADER_COPY(mtod(m, void *), eh);
415274b4c76eSAdrian Chadd 	mtod(m, struct ether_header *)->ether_type = htons(payload);
415374b4c76eSAdrian Chadd 	return m;
415474b4c76eSAdrian Chadd }
415536ee7775SAdrian Chadd 
415636ee7775SAdrian Chadd /*
415736ee7775SAdrian Chadd  * Complete an mbuf transmission.
415836ee7775SAdrian Chadd  *
415936ee7775SAdrian Chadd  * For now, this simply processes a completed frame after the
416036ee7775SAdrian Chadd  * driver has completed it's transmission and/or retransmission.
416136ee7775SAdrian Chadd  * It assumes the frame is an 802.11 encapsulated frame.
416236ee7775SAdrian Chadd  *
416336ee7775SAdrian Chadd  * Later on it will grow to become the exit path for a given frame
416436ee7775SAdrian Chadd  * from the driver and, depending upon how it's been encapsulated
416536ee7775SAdrian Chadd  * and already transmitted, it may end up doing A-MPDU retransmission,
416636ee7775SAdrian Chadd  * power save requeuing, etc.
416736ee7775SAdrian Chadd  *
416836ee7775SAdrian Chadd  * In order for the above to work, the driver entry point to this
416936ee7775SAdrian Chadd  * must not hold any driver locks.  Thus, the driver needs to delay
417036ee7775SAdrian Chadd  * any actual mbuf completion until it can release said locks.
417136ee7775SAdrian Chadd  *
417236ee7775SAdrian Chadd  * This frees the mbuf and if the mbuf has a node reference,
417336ee7775SAdrian Chadd  * the node reference will be freed.
417436ee7775SAdrian Chadd  */
417536ee7775SAdrian Chadd void
417636ee7775SAdrian Chadd ieee80211_tx_complete(struct ieee80211_node *ni, struct mbuf *m, int status)
417736ee7775SAdrian Chadd {
417836ee7775SAdrian Chadd 
417936ee7775SAdrian Chadd 	if (ni != NULL) {
41807a79cebfSGleb Smirnoff 		struct ifnet *ifp = ni->ni_vap->iv_ifp;
41817a79cebfSGleb Smirnoff 
41827a79cebfSGleb Smirnoff 		if (status == 0) {
41837a79cebfSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
41847a79cebfSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
41857a79cebfSGleb Smirnoff 			if (m->m_flags & M_MCAST)
41867a79cebfSGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
41877a79cebfSGleb Smirnoff 		} else
41887a79cebfSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
418904efa18fSBjoern A. Zeeb 		if (m->m_flags & M_TXCB) {
419004efa18fSBjoern A. Zeeb 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
419104efa18fSBjoern A. Zeeb 			   "ni %p vap %p mode %s state %s m %p status %d\n", ni, ni->ni_vap,
419204efa18fSBjoern A. Zeeb 			   ieee80211_opmode_name[ni->ni_vap->iv_opmode],
419304efa18fSBjoern A. Zeeb 			   ieee80211_state_name[ni->ni_vap->iv_state], m, status);
419436ee7775SAdrian Chadd 			ieee80211_process_callback(ni, m, status);
419504efa18fSBjoern A. Zeeb 		}
419636ee7775SAdrian Chadd 		ieee80211_free_node(ni);
419736ee7775SAdrian Chadd 	}
419836ee7775SAdrian Chadd 	m_freem(m);
419936ee7775SAdrian Chadd }
4200