xref: /freebsd/sys/net80211/ieee80211_output.c (revision 37e54466cf7a7c60f3d57c23c09b832db876e2fc)
11a1e1d21SSam Leffler /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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 #include "opt_inet.h"
31e755a73dSSam Leffler #include "opt_inet6.h"
32b032f27cSSam Leffler #include "opt_wlan.h"
331a1e1d21SSam Leffler 
341a1e1d21SSam Leffler #include <sys/param.h>
351a1e1d21SSam Leffler #include <sys/systm.h>
361a1e1d21SSam Leffler #include <sys/kernel.h>
378ec07310SGleb Smirnoff #include <sys/malloc.h>
388ec07310SGleb Smirnoff #include <sys/mbuf.h>
391a1e1d21SSam Leffler #include <sys/endian.h>
401a1e1d21SSam Leffler 
418a1b9b6aSSam Leffler #include <sys/socket.h>
421a1e1d21SSam Leffler 
431a1e1d21SSam Leffler #include <net/bpf.h>
448a1b9b6aSSam Leffler #include <net/ethernet.h>
458a1b9b6aSSam Leffler #include <net/if.h>
4676039bc8SGleb Smirnoff #include <net/if_var.h>
478a1b9b6aSSam Leffler #include <net/if_llc.h>
488a1b9b6aSSam Leffler #include <net/if_media.h>
493d0d5b21SJustin Hibbits #include <net/if_private.h>
508a1b9b6aSSam Leffler #include <net/if_vlan_var.h>
518a1b9b6aSSam Leffler 
528a1b9b6aSSam Leffler #include <net80211/ieee80211_var.h>
5368e8e04eSSam Leffler #include <net80211/ieee80211_regdomain.h>
54616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
55616190d0SSam Leffler #include <net80211/ieee80211_superg.h>
56616190d0SSam Leffler #endif
5710ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
5810ad9a77SSam Leffler #include <net80211/ieee80211_tdma.h>
5910ad9a77SSam Leffler #endif
60b032f27cSSam Leffler #include <net80211/ieee80211_wds.h>
6159aa14a9SRui Paulo #include <net80211/ieee80211_mesh.h>
6251172f62SAdrian Chadd #include <net80211/ieee80211_vht.h>
631a1e1d21SSam Leffler 
648355d59dSBjoern A. Zeeb #if defined(INET) || defined(INET6)
651a1e1d21SSam Leffler #include <netinet/in.h>
668355d59dSBjoern A. Zeeb #endif
678355d59dSBjoern A. Zeeb 
688355d59dSBjoern A. Zeeb #ifdef INET
691a1e1d21SSam Leffler #include <netinet/if_ether.h>
708a1b9b6aSSam Leffler #include <netinet/in_systm.h>
718a1b9b6aSSam Leffler #include <netinet/ip.h>
728a1b9b6aSSam Leffler #endif
73e755a73dSSam Leffler #ifdef INET6
74e755a73dSSam Leffler #include <netinet/ip6.h>
75e755a73dSSam Leffler #endif
768a1b9b6aSSam Leffler 
77c27b9cdbSRobert Watson #include <security/mac/mac_framework.h>
78c27b9cdbSRobert Watson 
7968e8e04eSSam Leffler #define	ETHER_HEADER_COPY(dst, src) \
8068e8e04eSSam Leffler 	memcpy(dst, src, sizeof(struct ether_header))
8168e8e04eSSam Leffler 
82b032f27cSSam Leffler static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
8368e8e04eSSam Leffler 	u_int hdrsize, u_int ciphdrsize, u_int mtu);
8468e8e04eSSam Leffler static	void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
8568e8e04eSSam Leffler 
868a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
878a1b9b6aSSam Leffler /*
888a1b9b6aSSam Leffler  * Decide if an outbound management frame should be
898a1b9b6aSSam Leffler  * printed when debugging is enabled.  This filters some
908a1b9b6aSSam Leffler  * of the less interesting frames that come frequently
918a1b9b6aSSam Leffler  * (e.g. beacons).
928a1b9b6aSSam Leffler  */
938a1b9b6aSSam Leffler static __inline int
doprint(struct ieee80211vap * vap,int subtype)94b032f27cSSam Leffler doprint(struct ieee80211vap *vap, int subtype)
958a1b9b6aSSam Leffler {
968a1b9b6aSSam Leffler 	switch (subtype) {
978a1b9b6aSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
98b032f27cSSam Leffler 		return (vap->iv_opmode == IEEE80211_M_IBSS);
998a1b9b6aSSam Leffler 	}
1008a1b9b6aSSam Leffler 	return 1;
1018a1b9b6aSSam Leffler }
1021a1e1d21SSam Leffler #endif
1031a1e1d21SSam Leffler 
1040a915fadSSam Leffler /*
105363a2c3cSAdrian Chadd  * Transmit a frame to the given destination on the given VAP.
106363a2c3cSAdrian Chadd  *
107363a2c3cSAdrian Chadd  * It's up to the caller to figure out the details of who this
108363a2c3cSAdrian Chadd  * is going to and resolving the node.
109363a2c3cSAdrian Chadd  *
110363a2c3cSAdrian Chadd  * This routine takes care of queuing it for power save,
111363a2c3cSAdrian Chadd  * A-MPDU state stuff, fast-frames state stuff, encapsulation
112363a2c3cSAdrian Chadd  * if required, then passing it up to the driver layer.
113363a2c3cSAdrian Chadd  *
114363a2c3cSAdrian Chadd  * This routine (for now) consumes the mbuf and frees the node
115363a2c3cSAdrian Chadd  * reference; it ideally will return a TX status which reflects
116363a2c3cSAdrian Chadd  * whether the mbuf was consumed or not, so the caller can
117363a2c3cSAdrian Chadd  * free the mbuf (if appropriate) and the node reference (again,
118363a2c3cSAdrian Chadd  * if appropriate.)
119363a2c3cSAdrian Chadd  */
120363a2c3cSAdrian Chadd int
ieee80211_vap_pkt_send_dest(struct ieee80211vap * vap,struct mbuf * m,struct ieee80211_node * ni)121363a2c3cSAdrian Chadd ieee80211_vap_pkt_send_dest(struct ieee80211vap *vap, struct mbuf *m,
122363a2c3cSAdrian Chadd     struct ieee80211_node *ni)
123363a2c3cSAdrian Chadd {
124363a2c3cSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
125363a2c3cSAdrian Chadd 	struct ifnet *ifp = vap->iv_ifp;
1262e9090dfSBjoern A. Zeeb 	int mcast;
127b3d0e274SAdrian Chadd 	int do_ampdu = 0;
12805ea7a3eSBjoern A. Zeeb #ifdef IEEE80211_SUPPORT_SUPERG
129b3d0e274SAdrian Chadd 	int do_amsdu = 0;
130b3d0e274SAdrian Chadd 	int do_ampdu_amsdu = 0;
131b3d0e274SAdrian Chadd 	int no_ampdu = 1; /* Will be set to 0 if ampdu is active */
132b3d0e274SAdrian Chadd 	int do_ff = 0;
13305ea7a3eSBjoern A. Zeeb #endif
134363a2c3cSAdrian Chadd 
135363a2c3cSAdrian Chadd 	if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
136363a2c3cSAdrian Chadd 	    (m->m_flags & M_PWR_SAV) == 0) {
137363a2c3cSAdrian Chadd 		/*
138363a2c3cSAdrian Chadd 		 * Station in power save mode; pass the frame
139363a2c3cSAdrian Chadd 		 * to the 802.11 layer and continue.  We'll get
140363a2c3cSAdrian Chadd 		 * the frame back when the time is right.
141363a2c3cSAdrian Chadd 		 * XXX lose WDS vap linkage?
142363a2c3cSAdrian Chadd 		 */
14379077edcSAndrey V. Elsukov 		if (ieee80211_pwrsave(ni, m) != 0)
14479077edcSAndrey V. Elsukov 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
145363a2c3cSAdrian Chadd 		ieee80211_free_node(ni);
146c6d5b600SAdrian Chadd 
147c6d5b600SAdrian Chadd 		/*
148c6d5b600SAdrian Chadd 		 * We queued it fine, so tell the upper layer
149c6d5b600SAdrian Chadd 		 * that we consumed it.
150c6d5b600SAdrian Chadd 		 */
151c6d5b600SAdrian Chadd 		return (0);
152363a2c3cSAdrian Chadd 	}
153363a2c3cSAdrian Chadd 	/* calculate priority so drivers can find the tx queue */
154363a2c3cSAdrian Chadd 	if (ieee80211_classify(ni, m)) {
155363a2c3cSAdrian Chadd 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
156363a2c3cSAdrian Chadd 		    ni->ni_macaddr, NULL,
157363a2c3cSAdrian Chadd 		    "%s", "classification failure");
158363a2c3cSAdrian Chadd 		vap->iv_stats.is_tx_classify++;
159dea45121SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
160363a2c3cSAdrian Chadd 		m_freem(m);
161363a2c3cSAdrian Chadd 		ieee80211_free_node(ni);
162c6d5b600SAdrian Chadd 
163363a2c3cSAdrian Chadd 		/* XXX better status? */
164c6d5b600SAdrian Chadd 		return (0);
165363a2c3cSAdrian Chadd 	}
166363a2c3cSAdrian Chadd 	/*
167363a2c3cSAdrian Chadd 	 * Stash the node pointer.  Note that we do this after
168363a2c3cSAdrian Chadd 	 * any call to ieee80211_dwds_mcast because that code
169363a2c3cSAdrian Chadd 	 * uses any existing value for rcvif to identify the
170363a2c3cSAdrian Chadd 	 * interface it (might have been) received on.
171363a2c3cSAdrian Chadd 	 */
172fb3bc596SJohn Baldwin 	MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
173363a2c3cSAdrian Chadd 	m->m_pkthdr.rcvif = (void *)ni;
1742e9090dfSBjoern A. Zeeb 	mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1: 0;
175363a2c3cSAdrian Chadd 
176363a2c3cSAdrian Chadd 	BPF_MTAP(ifp, m);		/* 802.3 tx */
177363a2c3cSAdrian Chadd 
178b3d0e274SAdrian Chadd 	/*
179b3d0e274SAdrian Chadd 	 * Figure out if we can do A-MPDU, A-MSDU or FF.
180b3d0e274SAdrian Chadd 	 *
181b3d0e274SAdrian Chadd 	 * A-MPDU depends upon vap/node config.
182b3d0e274SAdrian Chadd 	 * A-MSDU depends upon vap/node config.
183b3d0e274SAdrian Chadd 	 * FF depends upon vap config, IE and whether
184b3d0e274SAdrian Chadd 	 *  it's 11abg (and not 11n/11ac/etc.)
185b3d0e274SAdrian Chadd 	 *
186b3d0e274SAdrian Chadd 	 * Note that these flags indiciate whether we can do
187b3d0e274SAdrian Chadd 	 * it at all, rather than the situation (eg traffic type.)
188b3d0e274SAdrian Chadd 	 */
189b3d0e274SAdrian Chadd 	do_ampdu = ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
190b3d0e274SAdrian Chadd 	    (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX));
19105ea7a3eSBjoern A. Zeeb #ifdef IEEE80211_SUPPORT_SUPERG
192b3d0e274SAdrian Chadd 	do_amsdu = ((ni->ni_flags & IEEE80211_NODE_AMSDU_TX) &&
193b3d0e274SAdrian Chadd 	    (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_TX));
194b3d0e274SAdrian Chadd 	do_ff =
195b3d0e274SAdrian Chadd 	    ((ni->ni_flags & IEEE80211_NODE_HT) == 0) &&
196b3d0e274SAdrian Chadd 	    ((ni->ni_flags & IEEE80211_NODE_VHT) == 0) &&
197b3d0e274SAdrian Chadd 	    (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF));
19805ea7a3eSBjoern A. Zeeb #endif
199b3d0e274SAdrian Chadd 
200363a2c3cSAdrian Chadd 	/*
201363a2c3cSAdrian Chadd 	 * Check if A-MPDU tx aggregation is setup or if we
202363a2c3cSAdrian Chadd 	 * should try to enable it.  The sta must be associated
203363a2c3cSAdrian Chadd 	 * with HT and A-MPDU enabled for use.  When the policy
204363a2c3cSAdrian Chadd 	 * routine decides we should enable A-MPDU we issue an
205363a2c3cSAdrian Chadd 	 * ADDBA request and wait for a reply.  The frame being
206363a2c3cSAdrian Chadd 	 * encapsulated will go out w/o using A-MPDU, or possibly
207363a2c3cSAdrian Chadd 	 * it might be collected by the driver and held/retransmit.
208363a2c3cSAdrian Chadd 	 * The default ic_ampdu_enable routine handles staggering
209363a2c3cSAdrian Chadd 	 * ADDBA requests in case the receiver NAK's us or we are
210363a2c3cSAdrian Chadd 	 * otherwise unable to establish a BA stream.
2119764ef21SAdrian Chadd 	 *
2129764ef21SAdrian Chadd 	 * Don't treat group-addressed frames as candidates for aggregation;
2139764ef21SAdrian Chadd 	 * net80211 doesn't support 802.11aa-2012 and so group addressed
2149764ef21SAdrian Chadd 	 * frames will always have sequence numbers allocated from the NON_QOS
2159764ef21SAdrian Chadd 	 * TID.
216363a2c3cSAdrian Chadd 	 */
217b3d0e274SAdrian Chadd 	if (do_ampdu) {
2189764ef21SAdrian Chadd 		if ((m->m_flags & M_EAPOL) == 0 && (! mcast)) {
219363a2c3cSAdrian Chadd 			int tid = WME_AC_TO_TID(M_WME_GETAC(m));
220363a2c3cSAdrian Chadd 			struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[tid];
221363a2c3cSAdrian Chadd 
222363a2c3cSAdrian Chadd 			ieee80211_txampdu_count_packet(tap);
223363a2c3cSAdrian Chadd 			if (IEEE80211_AMPDU_RUNNING(tap)) {
224363a2c3cSAdrian Chadd 				/*
225363a2c3cSAdrian Chadd 				 * Operational, mark frame for aggregation.
226363a2c3cSAdrian Chadd 				 *
227363a2c3cSAdrian Chadd 				 * XXX do tx aggregation here
228363a2c3cSAdrian Chadd 				 */
229363a2c3cSAdrian Chadd 				m->m_flags |= M_AMPDU_MPDU;
230363a2c3cSAdrian Chadd 			} else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
231363a2c3cSAdrian Chadd 			    ic->ic_ampdu_enable(ni, tap)) {
232363a2c3cSAdrian Chadd 				/*
233363a2c3cSAdrian Chadd 				 * Not negotiated yet, request service.
234363a2c3cSAdrian Chadd 				 */
235363a2c3cSAdrian Chadd 				ieee80211_ampdu_request(ni, tap);
236363a2c3cSAdrian Chadd 				/* XXX hold frame for reply? */
237363a2c3cSAdrian Chadd 			}
238b3d0e274SAdrian Chadd 			/*
239b3d0e274SAdrian Chadd 			 * Now update the no-ampdu flag.  A-MPDU may have been
240b3d0e274SAdrian Chadd 			 * started or administratively disabled above; so now we
241b3d0e274SAdrian Chadd 			 * know whether we're running yet or not.
242b3d0e274SAdrian Chadd 			 *
243b3d0e274SAdrian Chadd 			 * This will let us know whether we should be doing A-MSDU
244b3d0e274SAdrian Chadd 			 * at this point.  We only do A-MSDU if we're either not
245b3d0e274SAdrian Chadd 			 * doing A-MPDU, or A-MPDU is NACKed, or A-MPDU + A-MSDU
246b3d0e274SAdrian Chadd 			 * is available.
247b3d0e274SAdrian Chadd 			 *
248b3d0e274SAdrian Chadd 			 * Whilst here, update the amsdu-ampdu flag.  The above may
249b3d0e274SAdrian Chadd 			 * have also set or cleared the amsdu-in-ampdu txa_flags
250b3d0e274SAdrian Chadd 			 * combination so we can correctly do A-MPDU + A-MSDU.
251b3d0e274SAdrian Chadd 			 */
25205ea7a3eSBjoern A. Zeeb #ifdef IEEE80211_SUPPORT_SUPERG
253b3d0e274SAdrian Chadd 			no_ampdu = (! IEEE80211_AMPDU_RUNNING(tap)
254b3d0e274SAdrian Chadd 			    || (IEEE80211_AMPDU_NACKED(tap)));
255b3d0e274SAdrian Chadd 			do_ampdu_amsdu = IEEE80211_AMPDU_RUNNING_AMSDU(tap);
25605ea7a3eSBjoern A. Zeeb #endif
257363a2c3cSAdrian Chadd 		}
2581c7b0c84SAdrian Chadd 	}
259363a2c3cSAdrian Chadd 
260363a2c3cSAdrian Chadd #ifdef IEEE80211_SUPPORT_SUPERG
2611c7b0c84SAdrian Chadd 	/*
2621c7b0c84SAdrian Chadd 	 * Check for AMSDU/FF; queue for aggregation
2631c7b0c84SAdrian Chadd 	 *
2641c7b0c84SAdrian Chadd 	 * Note: we don't bother trying to do fast frames or
2651c7b0c84SAdrian Chadd 	 * A-MSDU encapsulation for 802.3 drivers.  Now, we
2661c7b0c84SAdrian Chadd 	 * likely could do it for FF (because it's a magic
2671c7b0c84SAdrian Chadd 	 * atheros tunnel LLC type) but I don't think we're going
2681c7b0c84SAdrian Chadd 	 * to really need to.  For A-MSDU we'd have to set the
2691c7b0c84SAdrian Chadd 	 * A-MSDU QoS bit in the wifi header, so we just plain
2701c7b0c84SAdrian Chadd 	 * can't do it.
2711c7b0c84SAdrian Chadd 	 */
272b3d0e274SAdrian Chadd 	if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
273b3d0e274SAdrian Chadd 		if ((! mcast) &&
274b3d0e274SAdrian Chadd 		    (do_ampdu_amsdu || (no_ampdu && do_amsdu)) &&
275b3d0e274SAdrian Chadd 		    ieee80211_amsdu_tx_ok(ni)) {
2761c7b0c84SAdrian Chadd 			m = ieee80211_amsdu_check(ni, m);
2771c7b0c84SAdrian Chadd 			if (m == NULL) {
2781c7b0c84SAdrian Chadd 				/* NB: any ni ref held on stageq */
2791c7b0c84SAdrian Chadd 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
2801c7b0c84SAdrian Chadd 				    "%s: amsdu_check queued frame\n",
2811c7b0c84SAdrian Chadd 				    __func__);
2821c7b0c84SAdrian Chadd 				return (0);
2831c7b0c84SAdrian Chadd 			}
284b3d0e274SAdrian Chadd 		} else if ((! mcast) && do_ff) {
285363a2c3cSAdrian Chadd 			m = ieee80211_ff_check(ni, m);
286363a2c3cSAdrian Chadd 			if (m == NULL) {
287363a2c3cSAdrian Chadd 				/* NB: any ni ref held on stageq */
2881c7b0c84SAdrian Chadd 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
2891c7b0c84SAdrian Chadd 				    "%s: ff_check queued frame\n",
2901c7b0c84SAdrian Chadd 				    __func__);
29111e0ddb1SAdrian Chadd 				return (0);
292363a2c3cSAdrian Chadd 			}
293363a2c3cSAdrian Chadd 		}
2941c7b0c84SAdrian Chadd 	}
295363a2c3cSAdrian Chadd #endif /* IEEE80211_SUPPORT_SUPERG */
296363a2c3cSAdrian Chadd 
297363a2c3cSAdrian Chadd 	/*
298363a2c3cSAdrian Chadd 	 * Grab the TX lock - serialise the TX process from this
299363a2c3cSAdrian Chadd 	 * point (where TX state is being checked/modified)
300363a2c3cSAdrian Chadd 	 * through to driver queue.
301363a2c3cSAdrian Chadd 	 */
302363a2c3cSAdrian Chadd 	IEEE80211_TX_LOCK(ic);
303363a2c3cSAdrian Chadd 
304f9128a1bSAdrian Chadd 	/*
305f9128a1bSAdrian Chadd 	 * XXX make the encap and transmit code a separate function
306f9128a1bSAdrian Chadd 	 * so things like the FF (and later A-MSDU) path can just call
307f9128a1bSAdrian Chadd 	 * it for flushed frames.
308f9128a1bSAdrian Chadd 	 */
309363a2c3cSAdrian Chadd 	if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
310363a2c3cSAdrian Chadd 		/*
311363a2c3cSAdrian Chadd 		 * Encapsulate the packet in prep for transmission.
312363a2c3cSAdrian Chadd 		 */
313363a2c3cSAdrian Chadd 		m = ieee80211_encap(vap, ni, m);
314363a2c3cSAdrian Chadd 		if (m == NULL) {
315363a2c3cSAdrian Chadd 			/* NB: stat+msg handled in ieee80211_encap */
316363a2c3cSAdrian Chadd 			IEEE80211_TX_UNLOCK(ic);
317363a2c3cSAdrian Chadd 			ieee80211_free_node(ni);
31879077edcSAndrey V. Elsukov 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
319363a2c3cSAdrian Chadd 			return (ENOBUFS);
320363a2c3cSAdrian Chadd 		}
321363a2c3cSAdrian Chadd 	}
322d3a4ade3SAdrian Chadd 	(void) ieee80211_parent_xmitpkt(ic, m);
323363a2c3cSAdrian Chadd 
324363a2c3cSAdrian Chadd 	/*
325363a2c3cSAdrian Chadd 	 * Unlock at this point - no need to hold it across
326363a2c3cSAdrian Chadd 	 * ieee80211_free_node() (ie, the comlock)
327363a2c3cSAdrian Chadd 	 */
328363a2c3cSAdrian Chadd 	IEEE80211_TX_UNLOCK(ic);
329363a2c3cSAdrian Chadd 	ic->ic_lastdata = ticks;
330363a2c3cSAdrian Chadd 
331363a2c3cSAdrian Chadd 	return (0);
332363a2c3cSAdrian Chadd }
333363a2c3cSAdrian Chadd 
334363a2c3cSAdrian Chadd /*
3355cda6006SAdrian Chadd  * Send the given mbuf through the given vap.
3365cda6006SAdrian Chadd  *
3375cda6006SAdrian Chadd  * This consumes the mbuf regardless of whether the transmit
3385cda6006SAdrian Chadd  * was successful or not.
3395cda6006SAdrian Chadd  *
3405cda6006SAdrian Chadd  * This does none of the initial checks that ieee80211_start()
3415cda6006SAdrian Chadd  * does (eg CAC timeout, interface wakeup) - the caller must
3425cda6006SAdrian Chadd  * do this first.
3435cda6006SAdrian Chadd  */
3445cda6006SAdrian Chadd static int
ieee80211_start_pkt(struct ieee80211vap * vap,struct mbuf * m)3455cda6006SAdrian Chadd ieee80211_start_pkt(struct ieee80211vap *vap, struct mbuf *m)
3465cda6006SAdrian Chadd {
3475cda6006SAdrian Chadd #define	IS_DWDS(vap) \
3485cda6006SAdrian Chadd 	(vap->iv_opmode == IEEE80211_M_WDS && \
3495cda6006SAdrian Chadd 	 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
3505cda6006SAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
3515cda6006SAdrian Chadd 	struct ifnet *ifp = vap->iv_ifp;
3525cda6006SAdrian Chadd 	struct ieee80211_node *ni;
3535cda6006SAdrian Chadd 	struct ether_header *eh;
3545cda6006SAdrian Chadd 
3555cda6006SAdrian Chadd 	/*
3565cda6006SAdrian Chadd 	 * Cancel any background scan.
3575cda6006SAdrian Chadd 	 */
3585cda6006SAdrian Chadd 	if (ic->ic_flags & IEEE80211_F_SCAN)
3595cda6006SAdrian Chadd 		ieee80211_cancel_anyscan(vap);
3605cda6006SAdrian Chadd 	/*
3615cda6006SAdrian Chadd 	 * Find the node for the destination so we can do
3625cda6006SAdrian Chadd 	 * things like power save and fast frames aggregation.
3635cda6006SAdrian Chadd 	 *
3645cda6006SAdrian Chadd 	 * NB: past this point various code assumes the first
3655cda6006SAdrian Chadd 	 *     mbuf has the 802.3 header present (and contiguous).
3665cda6006SAdrian Chadd 	 */
3675cda6006SAdrian Chadd 	ni = NULL;
3685cda6006SAdrian Chadd 	if (m->m_len < sizeof(struct ether_header) &&
3695cda6006SAdrian Chadd 	   (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
3705cda6006SAdrian Chadd 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
3715cda6006SAdrian Chadd 		    "discard frame, %s\n", "m_pullup failed");
3725cda6006SAdrian Chadd 		vap->iv_stats.is_tx_nobuf++;	/* XXX */
373dea45121SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3745cda6006SAdrian Chadd 		return (ENOBUFS);
3755cda6006SAdrian Chadd 	}
3765cda6006SAdrian Chadd 	eh = mtod(m, struct ether_header *);
3775cda6006SAdrian Chadd 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
3785cda6006SAdrian Chadd 		if (IS_DWDS(vap)) {
3795cda6006SAdrian Chadd 			/*
3805cda6006SAdrian Chadd 			 * Only unicast frames from the above go out
3815cda6006SAdrian Chadd 			 * DWDS vaps; multicast frames are handled by
3825cda6006SAdrian Chadd 			 * dispatching the frame as it comes through
3835cda6006SAdrian Chadd 			 * the AP vap (see below).
3845cda6006SAdrian Chadd 			 */
3855cda6006SAdrian Chadd 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
3865cda6006SAdrian Chadd 			    eh->ether_dhost, "mcast", "%s", "on DWDS");
3875cda6006SAdrian Chadd 			vap->iv_stats.is_dwds_mcast++;
3885cda6006SAdrian Chadd 			m_freem(m);
38979077edcSAndrey V. Elsukov 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3905cda6006SAdrian Chadd 			/* XXX better status? */
3915cda6006SAdrian Chadd 			return (ENOBUFS);
3925cda6006SAdrian Chadd 		}
3935cda6006SAdrian Chadd 		if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
3945cda6006SAdrian Chadd 			/*
3955cda6006SAdrian Chadd 			 * Spam DWDS vap's w/ multicast traffic.
3965cda6006SAdrian Chadd 			 */
3975cda6006SAdrian Chadd 			/* XXX only if dwds in use? */
3985cda6006SAdrian Chadd 			ieee80211_dwds_mcast(vap, m);
3995cda6006SAdrian Chadd 		}
4005cda6006SAdrian Chadd 	}
4015cda6006SAdrian Chadd #ifdef IEEE80211_SUPPORT_MESH
4025cda6006SAdrian Chadd 	if (vap->iv_opmode != IEEE80211_M_MBSS) {
4035cda6006SAdrian Chadd #endif
4045cda6006SAdrian Chadd 		ni = ieee80211_find_txnode(vap, eh->ether_dhost);
4055cda6006SAdrian Chadd 		if (ni == NULL) {
4065cda6006SAdrian Chadd 			/* NB: ieee80211_find_txnode does stat+msg */
407dea45121SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
4085cda6006SAdrian Chadd 			m_freem(m);
4095cda6006SAdrian Chadd 			/* XXX better status? */
4105cda6006SAdrian Chadd 			return (ENOBUFS);
4115cda6006SAdrian Chadd 		}
4125cda6006SAdrian Chadd 		if (ni->ni_associd == 0 &&
4135cda6006SAdrian Chadd 		    (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
4145cda6006SAdrian Chadd 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
4155cda6006SAdrian Chadd 			    eh->ether_dhost, NULL,
4165cda6006SAdrian Chadd 			    "sta not associated (type 0x%04x)",
4175cda6006SAdrian Chadd 			    htons(eh->ether_type));
4185cda6006SAdrian Chadd 			vap->iv_stats.is_tx_notassoc++;
419dea45121SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
4205cda6006SAdrian Chadd 			m_freem(m);
4215cda6006SAdrian Chadd 			ieee80211_free_node(ni);
4225cda6006SAdrian Chadd 			/* XXX better status? */
4235cda6006SAdrian Chadd 			return (ENOBUFS);
4245cda6006SAdrian Chadd 		}
4255cda6006SAdrian Chadd #ifdef IEEE80211_SUPPORT_MESH
4265cda6006SAdrian Chadd 	} else {
4275cda6006SAdrian Chadd 		if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) {
4285cda6006SAdrian Chadd 			/*
4295cda6006SAdrian Chadd 			 * Proxy station only if configured.
4305cda6006SAdrian Chadd 			 */
4315cda6006SAdrian Chadd 			if (!ieee80211_mesh_isproxyena(vap)) {
4325cda6006SAdrian Chadd 				IEEE80211_DISCARD_MAC(vap,
4335cda6006SAdrian Chadd 				    IEEE80211_MSG_OUTPUT |
4345cda6006SAdrian Chadd 				    IEEE80211_MSG_MESH,
4355cda6006SAdrian Chadd 				    eh->ether_dhost, NULL,
4365cda6006SAdrian Chadd 				    "%s", "proxy not enabled");
4375cda6006SAdrian Chadd 				vap->iv_stats.is_mesh_notproxy++;
438dea45121SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
4395cda6006SAdrian Chadd 				m_freem(m);
4405cda6006SAdrian Chadd 				/* XXX better status? */
4415cda6006SAdrian Chadd 				return (ENOBUFS);
4425cda6006SAdrian Chadd 			}
4435cda6006SAdrian Chadd 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
4445cda6006SAdrian Chadd 			    "forward frame from DS SA(%6D), DA(%6D)\n",
4455cda6006SAdrian Chadd 			    eh->ether_shost, ":",
4465cda6006SAdrian Chadd 			    eh->ether_dhost, ":");
4475cda6006SAdrian Chadd 			ieee80211_mesh_proxy_check(vap, eh->ether_shost);
4485cda6006SAdrian Chadd 		}
4495cda6006SAdrian Chadd 		ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m);
4505cda6006SAdrian Chadd 		if (ni == NULL) {
4515cda6006SAdrian Chadd 			/*
4525cda6006SAdrian Chadd 			 * NB: ieee80211_mesh_discover holds/disposes
4535cda6006SAdrian Chadd 			 * frame (e.g. queueing on path discovery).
4545cda6006SAdrian Chadd 			 */
455dea45121SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
4565cda6006SAdrian Chadd 			/* XXX better status? */
4575cda6006SAdrian Chadd 			return (ENOBUFS);
4585cda6006SAdrian Chadd 		}
4595cda6006SAdrian Chadd 	}
4605cda6006SAdrian Chadd #endif
461363a2c3cSAdrian Chadd 
4625cda6006SAdrian Chadd 	/*
463363a2c3cSAdrian Chadd 	 * We've resolved the sender, so attempt to transmit it.
4645cda6006SAdrian Chadd 	 */
465ddd9ebbcSAdrian Chadd 
466ddd9ebbcSAdrian Chadd 	if (vap->iv_state == IEEE80211_S_SLEEP) {
467ddd9ebbcSAdrian Chadd 		/*
468ddd9ebbcSAdrian Chadd 		 * In power save; queue frame and then  wakeup device
469ddd9ebbcSAdrian Chadd 		 * for transmit.
470ddd9ebbcSAdrian Chadd 		 */
471ddd9ebbcSAdrian Chadd 		ic->ic_lastdata = ticks;
47279077edcSAndrey V. Elsukov 		if (ieee80211_pwrsave(ni, m) != 0)
47379077edcSAndrey V. Elsukov 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
474ddd9ebbcSAdrian Chadd 		ieee80211_free_node(ni);
475ddd9ebbcSAdrian Chadd 		ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
476ddd9ebbcSAdrian Chadd 		return (0);
477ddd9ebbcSAdrian Chadd 	}
478ddd9ebbcSAdrian Chadd 
479363a2c3cSAdrian Chadd 	if (ieee80211_vap_pkt_send_dest(vap, m, ni) != 0)
4805cda6006SAdrian Chadd 		return (ENOBUFS);
4815cda6006SAdrian Chadd 	return (0);
4825cda6006SAdrian Chadd #undef	IS_DWDS
4835cda6006SAdrian Chadd }
4845cda6006SAdrian Chadd 
4855cda6006SAdrian Chadd /*
486b032f27cSSam Leffler  * Start method for vap's.  All packets from the stack come
487b032f27cSSam Leffler  * through here.  We handle common processing of the packets
488b032f27cSSam Leffler  * before dispatching them to the underlying device.
4891df885c8SAdrian Chadd  *
4901df885c8SAdrian Chadd  * if_transmit() requires that the mbuf be consumed by this call
4911df885c8SAdrian Chadd  * regardless of the return condition.
492b032f27cSSam Leffler  */
493e7495198SAdrian Chadd int
ieee80211_vap_transmit(struct ifnet * ifp,struct mbuf * m)494e7495198SAdrian Chadd ieee80211_vap_transmit(struct ifnet *ifp, struct mbuf *m)
495b032f27cSSam Leffler {
496b032f27cSSam Leffler 	struct ieee80211vap *vap = ifp->if_softc;
497b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
498ddd9ebbcSAdrian Chadd 
499b032f27cSSam Leffler 	/*
500b032f27cSSam Leffler 	 * No data frames go out unless we're running.
501b032f27cSSam Leffler 	 * Note in particular this covers CAC and CSA
502b032f27cSSam Leffler 	 * states (though maybe we should check muting
503b032f27cSSam Leffler 	 * for CSA).
504b032f27cSSam Leffler 	 */
505ddd9ebbcSAdrian Chadd 	if (vap->iv_state != IEEE80211_S_RUN &&
506ddd9ebbcSAdrian Chadd 	    vap->iv_state != IEEE80211_S_SLEEP) {
507b032f27cSSam Leffler 		IEEE80211_LOCK(ic);
508b032f27cSSam Leffler 		/* re-check under the com lock to avoid races */
509ddd9ebbcSAdrian Chadd 		if (vap->iv_state != IEEE80211_S_RUN &&
510ddd9ebbcSAdrian Chadd 		    vap->iv_state != IEEE80211_S_SLEEP) {
511b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
512b032f27cSSam Leffler 			    "%s: ignore queue, in %s state\n",
513b032f27cSSam Leffler 			    __func__, ieee80211_state_name[vap->iv_state]);
514b032f27cSSam Leffler 			vap->iv_stats.is_tx_badstate++;
515b032f27cSSam Leffler 			IEEE80211_UNLOCK(ic);
51670e0b5acSAdrian Chadd 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
5171df885c8SAdrian Chadd 			m_freem(m);
51879077edcSAndrey V. Elsukov 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
51979077edcSAndrey V. Elsukov 			return (ENETDOWN);
520b032f27cSSam Leffler 		}
521b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
522b032f27cSSam Leffler 	}
5235cda6006SAdrian Chadd 
524b032f27cSSam Leffler 	/*
525b032f27cSSam Leffler 	 * Sanitize mbuf flags for net80211 use.  We cannot
526927ef5ffSSam Leffler 	 * clear M_PWR_SAV or M_MORE_DATA because these may
527927ef5ffSSam Leffler 	 * be set for frames that are re-submitted from the
528927ef5ffSSam Leffler 	 * power save queue.
529b032f27cSSam Leffler 	 *
530b032f27cSSam Leffler 	 * NB: This must be done before ieee80211_classify as
531b032f27cSSam Leffler 	 *     it marks EAPOL in frames with M_EAPOL.
532b032f27cSSam Leffler 	 */
533927ef5ffSSam Leffler 	m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
534e7495198SAdrian Chadd 
535b032f27cSSam Leffler 	/*
5365cda6006SAdrian Chadd 	 * Bump to the packet transmission path.
537e7495198SAdrian Chadd 	 * The mbuf will be consumed here.
538b032f27cSSam Leffler 	 */
539e7495198SAdrian Chadd 	return (ieee80211_start_pkt(vap, m));
540b032f27cSSam Leffler }
541e7495198SAdrian Chadd 
542e7495198SAdrian Chadd void
ieee80211_vap_qflush(struct ifnet * ifp)543e7495198SAdrian Chadd ieee80211_vap_qflush(struct ifnet *ifp)
544e7495198SAdrian Chadd {
545e7495198SAdrian Chadd 
546e7495198SAdrian Chadd 	/* Empty for now */
547b032f27cSSam Leffler }
548b032f27cSSam Leffler 
549168f582eSSam Leffler /*
5505cda6006SAdrian Chadd  * 802.11 raw output routine.
551ddd9ebbcSAdrian Chadd  *
552ddd9ebbcSAdrian Chadd  * XXX TODO: this (and other send routines) should correctly
553ddd9ebbcSAdrian Chadd  * XXX keep the pwr mgmt bit set if it decides to call into the
554ddd9ebbcSAdrian Chadd  * XXX driver to send a frame whilst the state is SLEEP.
555ddd9ebbcSAdrian Chadd  *
556ddd9ebbcSAdrian Chadd  * Otherwise the peer may decide that we're awake and flood us
557ddd9ebbcSAdrian Chadd  * with traffic we are still too asleep to receive!
558168f582eSSam Leffler  */
5595cda6006SAdrian Chadd int
ieee80211_raw_output(struct ieee80211vap * vap,struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_bpf_params * params)5605cda6006SAdrian Chadd ieee80211_raw_output(struct ieee80211vap *vap, struct ieee80211_node *ni,
5615cda6006SAdrian Chadd     struct mbuf *m, const struct ieee80211_bpf_params *params)
5625cda6006SAdrian Chadd {
5635cda6006SAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
5647a79cebfSGleb Smirnoff 	int error;
565168f582eSSam Leffler 
566ff09e23fSAdrian Chadd 	/*
567ff09e23fSAdrian Chadd 	 * Set node - the caller has taken a reference, so ensure
568ff09e23fSAdrian Chadd 	 * that the mbuf has the same node value that
569ff09e23fSAdrian Chadd 	 * it would if it were going via the normal path.
570ff09e23fSAdrian Chadd 	 */
571fb3bc596SJohn Baldwin 	MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
572ff09e23fSAdrian Chadd 	m->m_pkthdr.rcvif = (void *)ni;
573ff09e23fSAdrian Chadd 
574ff09e23fSAdrian Chadd 	/*
575ff09e23fSAdrian Chadd 	 * Attempt to add bpf transmit parameters.
576ff09e23fSAdrian Chadd 	 *
577ff09e23fSAdrian Chadd 	 * For now it's ok to fail; the raw_xmit api still takes
578ff09e23fSAdrian Chadd 	 * them as an option.
579ff09e23fSAdrian Chadd 	 *
580ff09e23fSAdrian Chadd 	 * Later on when ic_raw_xmit() has params removed,
581ff09e23fSAdrian Chadd 	 * they'll have to be added - so fail the transmit if
582ff09e23fSAdrian Chadd 	 * they can't be.
583ff09e23fSAdrian Chadd 	 */
5843a1da00bSAdrian Chadd 	if (params)
585ff09e23fSAdrian Chadd 		(void) ieee80211_add_xmit_params(m, params);
586ff09e23fSAdrian Chadd 
5877a79cebfSGleb Smirnoff 	error = ic->ic_raw_xmit(ni, m, params);
588d957a93aSAdrian Chadd 	if (error) {
5897a79cebfSGleb Smirnoff 		if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, 1);
590d957a93aSAdrian Chadd 		ieee80211_free_node(ni);
591d957a93aSAdrian Chadd 	}
5927a79cebfSGleb Smirnoff 	return (error);
593b032f27cSSam Leffler }
594b032f27cSSam Leffler 
595bcabc908SAndriy Voskoboinyk static int
ieee80211_validate_frame(struct mbuf * m,const struct ieee80211_bpf_params * params)596bcabc908SAndriy Voskoboinyk ieee80211_validate_frame(struct mbuf *m,
597bcabc908SAndriy Voskoboinyk     const struct ieee80211_bpf_params *params)
598bcabc908SAndriy Voskoboinyk {
599bcabc908SAndriy Voskoboinyk 	struct ieee80211_frame *wh;
600bcabc908SAndriy Voskoboinyk 	int type;
601bcabc908SAndriy Voskoboinyk 
602bcabc908SAndriy Voskoboinyk 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
603bcabc908SAndriy Voskoboinyk 		return (EINVAL);
604bcabc908SAndriy Voskoboinyk 
605bcabc908SAndriy Voskoboinyk 	wh = mtod(m, struct ieee80211_frame *);
606*37e54466SAdrian Chadd 	if (!IEEE80211_IS_FC0_CHECK_VER(wh, IEEE80211_FC0_VERSION_0))
607bcabc908SAndriy Voskoboinyk 		return (EINVAL);
608bcabc908SAndriy Voskoboinyk 
609bcabc908SAndriy Voskoboinyk 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
610bcabc908SAndriy Voskoboinyk 	if (type != IEEE80211_FC0_TYPE_DATA) {
611bcabc908SAndriy Voskoboinyk 		if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
612bcabc908SAndriy Voskoboinyk 		    IEEE80211_FC1_DIR_NODS)
613bcabc908SAndriy Voskoboinyk 			return (EINVAL);
614bcabc908SAndriy Voskoboinyk 
615bcabc908SAndriy Voskoboinyk 		if (type != IEEE80211_FC0_TYPE_MGT &&
616bcabc908SAndriy Voskoboinyk 		    (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) != 0)
617bcabc908SAndriy Voskoboinyk 			return (EINVAL);
618bcabc908SAndriy Voskoboinyk 
619bcabc908SAndriy Voskoboinyk 		/* XXX skip other field checks? */
620bcabc908SAndriy Voskoboinyk 	}
621bcabc908SAndriy Voskoboinyk 
622bcabc908SAndriy Voskoboinyk 	if ((params && (params->ibp_flags & IEEE80211_BPF_CRYPTO) != 0) ||
6232889cbe2SAdrian Chadd 	    (IEEE80211_IS_PROTECTED(wh))) {
624bcabc908SAndriy Voskoboinyk 		int subtype;
625bcabc908SAndriy Voskoboinyk 
626bcabc908SAndriy Voskoboinyk 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
627bcabc908SAndriy Voskoboinyk 
628bcabc908SAndriy Voskoboinyk 		/*
629bcabc908SAndriy Voskoboinyk 		 * See IEEE Std 802.11-2012,
630bcabc908SAndriy Voskoboinyk 		 * 8.2.4.1.9 'Protected Frame field'
631bcabc908SAndriy Voskoboinyk 		 */
632bcabc908SAndriy Voskoboinyk 		/* XXX no support for robust management frames yet. */
633bcabc908SAndriy Voskoboinyk 		if (!(type == IEEE80211_FC0_TYPE_DATA ||
634bcabc908SAndriy Voskoboinyk 		    (type == IEEE80211_FC0_TYPE_MGT &&
635bcabc908SAndriy Voskoboinyk 		     subtype == IEEE80211_FC0_SUBTYPE_AUTH)))
636bcabc908SAndriy Voskoboinyk 			return (EINVAL);
637bcabc908SAndriy Voskoboinyk 
638bcabc908SAndriy Voskoboinyk 		wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
639bcabc908SAndriy Voskoboinyk 	}
640bcabc908SAndriy Voskoboinyk 
641bcabc908SAndriy Voskoboinyk 	if (m->m_pkthdr.len < ieee80211_anyhdrsize(wh))
642bcabc908SAndriy Voskoboinyk 		return (EINVAL);
643bcabc908SAndriy Voskoboinyk 
644bcabc908SAndriy Voskoboinyk 	return (0);
645bcabc908SAndriy Voskoboinyk }
646bcabc908SAndriy Voskoboinyk 
647e42e878bSAndriy Voskoboinyk static int
ieee80211_validate_rate(struct ieee80211_node * ni,uint8_t rate)648e42e878bSAndriy Voskoboinyk ieee80211_validate_rate(struct ieee80211_node *ni, uint8_t rate)
649e42e878bSAndriy Voskoboinyk {
650e42e878bSAndriy Voskoboinyk 	struct ieee80211com *ic = ni->ni_ic;
651e42e878bSAndriy Voskoboinyk 
652e42e878bSAndriy Voskoboinyk 	if (IEEE80211_IS_HT_RATE(rate)) {
653e42e878bSAndriy Voskoboinyk 		if ((ic->ic_htcaps & IEEE80211_HTC_HT) == 0)
654e42e878bSAndriy Voskoboinyk 			return (EINVAL);
655e42e878bSAndriy Voskoboinyk 
656e42e878bSAndriy Voskoboinyk 		rate = IEEE80211_RV(rate);
657e42e878bSAndriy Voskoboinyk 		if (rate <= 31) {
658e42e878bSAndriy Voskoboinyk 			if (rate > ic->ic_txstream * 8 - 1)
659e42e878bSAndriy Voskoboinyk 				return (EINVAL);
660e42e878bSAndriy Voskoboinyk 
661e42e878bSAndriy Voskoboinyk 			return (0);
662e42e878bSAndriy Voskoboinyk 		}
663e42e878bSAndriy Voskoboinyk 
664e42e878bSAndriy Voskoboinyk 		if (rate == 32) {
665e42e878bSAndriy Voskoboinyk 			if ((ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
666e42e878bSAndriy Voskoboinyk 				return (EINVAL);
667e42e878bSAndriy Voskoboinyk 
668e42e878bSAndriy Voskoboinyk 			return (0);
669e42e878bSAndriy Voskoboinyk 		}
670e42e878bSAndriy Voskoboinyk 
671e42e878bSAndriy Voskoboinyk 		if ((ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) == 0)
672e42e878bSAndriy Voskoboinyk 			return (EINVAL);
673e42e878bSAndriy Voskoboinyk 
674e42e878bSAndriy Voskoboinyk 		switch (ic->ic_txstream) {
675e42e878bSAndriy Voskoboinyk 		case 0:
676e42e878bSAndriy Voskoboinyk 		case 1:
677e42e878bSAndriy Voskoboinyk 			return (EINVAL);
678e42e878bSAndriy Voskoboinyk 		case 2:
679e42e878bSAndriy Voskoboinyk 			if (rate > 38)
680e42e878bSAndriy Voskoboinyk 				return (EINVAL);
681e42e878bSAndriy Voskoboinyk 
682e42e878bSAndriy Voskoboinyk 			return (0);
683e42e878bSAndriy Voskoboinyk 		case 3:
684e42e878bSAndriy Voskoboinyk 			if (rate > 52)
685e42e878bSAndriy Voskoboinyk 				return (EINVAL);
686e42e878bSAndriy Voskoboinyk 
687e42e878bSAndriy Voskoboinyk 			return (0);
688e42e878bSAndriy Voskoboinyk 		case 4:
689e42e878bSAndriy Voskoboinyk 		default:
690e42e878bSAndriy Voskoboinyk 			if (rate > 76)
691e42e878bSAndriy Voskoboinyk 				return (EINVAL);
692e42e878bSAndriy Voskoboinyk 
693e42e878bSAndriy Voskoboinyk 			return (0);
694e42e878bSAndriy Voskoboinyk 		}
695e42e878bSAndriy Voskoboinyk 	}
696e42e878bSAndriy Voskoboinyk 
697e42e878bSAndriy Voskoboinyk 	if (!ieee80211_isratevalid(ic->ic_rt, rate))
698e42e878bSAndriy Voskoboinyk 		return (EINVAL);
699e42e878bSAndriy Voskoboinyk 
700e42e878bSAndriy Voskoboinyk 	return (0);
701e42e878bSAndriy Voskoboinyk }
702e42e878bSAndriy Voskoboinyk 
703e42e878bSAndriy Voskoboinyk static int
ieee80211_sanitize_rates(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_bpf_params * params)704e42e878bSAndriy Voskoboinyk ieee80211_sanitize_rates(struct ieee80211_node *ni, struct mbuf *m,
705e42e878bSAndriy Voskoboinyk     const struct ieee80211_bpf_params *params)
706e42e878bSAndriy Voskoboinyk {
707e42e878bSAndriy Voskoboinyk 	int error;
708e42e878bSAndriy Voskoboinyk 
709e42e878bSAndriy Voskoboinyk 	if (!params)
710e42e878bSAndriy Voskoboinyk 		return (0);	/* nothing to do */
711e42e878bSAndriy Voskoboinyk 
712e42e878bSAndriy Voskoboinyk 	/* NB: most drivers assume that ibp_rate0 is set (!= 0). */
713e42e878bSAndriy Voskoboinyk 	if (params->ibp_rate0 != 0) {
714e42e878bSAndriy Voskoboinyk 		error = ieee80211_validate_rate(ni, params->ibp_rate0);
715e42e878bSAndriy Voskoboinyk 		if (error != 0)
716e42e878bSAndriy Voskoboinyk 			return (error);
717e42e878bSAndriy Voskoboinyk 	} else {
718e42e878bSAndriy Voskoboinyk 		/* XXX pre-setup some default (e.g., mgmt / mcast) rate */
719e42e878bSAndriy Voskoboinyk 		/* XXX __DECONST? */
720e42e878bSAndriy Voskoboinyk 		(void) m;
721e42e878bSAndriy Voskoboinyk 	}
722e42e878bSAndriy Voskoboinyk 
723e42e878bSAndriy Voskoboinyk 	if (params->ibp_rate1 != 0 &&
724e42e878bSAndriy Voskoboinyk 	    (error = ieee80211_validate_rate(ni, params->ibp_rate1)) != 0)
725e42e878bSAndriy Voskoboinyk 		return (error);
726e42e878bSAndriy Voskoboinyk 
727e42e878bSAndriy Voskoboinyk 	if (params->ibp_rate2 != 0 &&
728e42e878bSAndriy Voskoboinyk 	    (error = ieee80211_validate_rate(ni, params->ibp_rate2)) != 0)
729e42e878bSAndriy Voskoboinyk 		return (error);
730e42e878bSAndriy Voskoboinyk 
731e42e878bSAndriy Voskoboinyk 	if (params->ibp_rate3 != 0 &&
732e42e878bSAndriy Voskoboinyk 	    (error = ieee80211_validate_rate(ni, params->ibp_rate3)) != 0)
733e42e878bSAndriy Voskoboinyk 		return (error);
734e42e878bSAndriy Voskoboinyk 
735e42e878bSAndriy Voskoboinyk 	return (0);
736e42e878bSAndriy Voskoboinyk }
737e42e878bSAndriy Voskoboinyk 
738b032f27cSSam Leffler /*
739b032f27cSSam Leffler  * 802.11 output routine. This is (currently) used only to
740b032f27cSSam Leffler  * connect bpf write calls to the 802.11 layer for injecting
741fad788b1SSam Leffler  * raw 802.11 frames.
742b032f27cSSam Leffler  */
743b032f27cSSam Leffler int
ieee80211_output(struct ifnet * ifp,struct mbuf * m,const struct sockaddr * dst,struct route * ro)744b032f27cSSam Leffler ieee80211_output(struct ifnet *ifp, struct mbuf *m,
74547e8d432SGleb Smirnoff 	const struct sockaddr *dst, struct route *ro)
746b032f27cSSam Leffler {
747b032f27cSSam Leffler #define senderr(e) do { error = (e); goto bad;} while (0)
748bcabc908SAndriy Voskoboinyk 	const struct ieee80211_bpf_params *params = NULL;
749b032f27cSSam Leffler 	struct ieee80211_node *ni = NULL;
7508ed1835dSSam Leffler 	struct ieee80211vap *vap;
751b032f27cSSam Leffler 	struct ieee80211_frame *wh;
7525cda6006SAdrian Chadd 	struct ieee80211com *ic = NULL;
753b032f27cSSam Leffler 	int error;
7545cda6006SAdrian Chadd 	int ret;
755b032f27cSSam Leffler 
7568ed1835dSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
7578ed1835dSSam Leffler 		/*
7588ed1835dSSam Leffler 		 * Short-circuit requests if the vap is marked OACTIVE
7598ed1835dSSam Leffler 		 * as this can happen because a packet came down through
7608ed1835dSSam Leffler 		 * ieee80211_start before the vap entered RUN state in
7618ed1835dSSam Leffler 		 * which case it's ok to just drop the frame.  This
7628ed1835dSSam Leffler 		 * should not be necessary but callers of if_output don't
7638ed1835dSSam Leffler 		 * check OACTIVE.
7648ed1835dSSam Leffler 		 */
7658ed1835dSSam Leffler 		senderr(ENETDOWN);
7668ed1835dSSam Leffler 	}
7678ed1835dSSam Leffler 	vap = ifp->if_softc;
7685cda6006SAdrian Chadd 	ic = vap->iv_ic;
769b032f27cSSam Leffler 	/*
770b032f27cSSam Leffler 	 * Hand to the 802.3 code if not tagged as
771b032f27cSSam Leffler 	 * a raw 802.11 frame.
772b032f27cSSam Leffler 	 */
773b032f27cSSam Leffler 	if (dst->sa_family != AF_IEEE80211)
774279aa3d4SKip Macy 		return vap->iv_output(ifp, m, dst, ro);
775b032f27cSSam Leffler #ifdef MAC
776c27b9cdbSRobert Watson 	error = mac_ifnet_check_transmit(ifp, m);
777b032f27cSSam Leffler 	if (error)
778b032f27cSSam Leffler 		senderr(error);
779b032f27cSSam Leffler #endif
780b032f27cSSam Leffler 	if (ifp->if_flags & IFF_MONITOR)
781b032f27cSSam Leffler 		senderr(ENETDOWN);
782b032f27cSSam Leffler 	if (!IFNET_IS_UP_RUNNING(ifp))
783b032f27cSSam Leffler 		senderr(ENETDOWN);
784b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
785b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap,
786b032f27cSSam Leffler 		    IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
787b032f27cSSam Leffler 		    "block %s frame in CAC state\n", "raw data");
788b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
789b032f27cSSam Leffler 		senderr(EIO);		/* XXX */
7900d9aed8aSBernhard Schmidt 	} else if (vap->iv_state == IEEE80211_S_SCAN)
7910d9aed8aSBernhard Schmidt 		senderr(EIO);
792b032f27cSSam Leffler 	/* XXX bypass bridge, pfil, carp, etc. */
793b032f27cSSam Leffler 
794bcabc908SAndriy Voskoboinyk 	/*
795bcabc908SAndriy Voskoboinyk 	 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
796bcabc908SAndriy Voskoboinyk 	 * present by setting the sa_len field of the sockaddr (yes,
797bcabc908SAndriy Voskoboinyk 	 * this is a hack).
798bcabc908SAndriy Voskoboinyk 	 * NB: we assume sa_data is suitably aligned to cast.
799bcabc908SAndriy Voskoboinyk 	 */
800bcabc908SAndriy Voskoboinyk 	if (dst->sa_len != 0)
801bcabc908SAndriy Voskoboinyk 		params = (const struct ieee80211_bpf_params *)dst->sa_data;
802bcabc908SAndriy Voskoboinyk 
803bcabc908SAndriy Voskoboinyk 	error = ieee80211_validate_frame(m, params);
804bcabc908SAndriy Voskoboinyk 	if (error != 0)
805bcabc908SAndriy Voskoboinyk 		senderr(error);
806bcabc908SAndriy Voskoboinyk 
807b032f27cSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
808b032f27cSSam Leffler 
809b032f27cSSam Leffler 	/* locate destination node */
810b032f27cSSam Leffler 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
811b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_NODS:
812b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_FROMDS:
813b032f27cSSam Leffler 		ni = ieee80211_find_txnode(vap, wh->i_addr1);
814b032f27cSSam Leffler 		break;
815b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_TODS:
816b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_DSTODS:
817b032f27cSSam Leffler 		ni = ieee80211_find_txnode(vap, wh->i_addr3);
818b032f27cSSam Leffler 		break;
819b032f27cSSam Leffler 	default:
820bcabc908SAndriy Voskoboinyk 		senderr(EDOOFUS);
821b032f27cSSam Leffler 	}
822b032f27cSSam Leffler 	if (ni == NULL) {
823b032f27cSSam Leffler 		/*
824b032f27cSSam Leffler 		 * Permit packets w/ bpf params through regardless
825b032f27cSSam Leffler 		 * (see below about sa_len).
826b032f27cSSam Leffler 		 */
827b032f27cSSam Leffler 		if (dst->sa_len == 0)
828b032f27cSSam Leffler 			senderr(EHOSTUNREACH);
829b032f27cSSam Leffler 		ni = ieee80211_ref_node(vap->iv_bss);
830b032f27cSSam Leffler 	}
831b032f27cSSam Leffler 
832b032f27cSSam Leffler 	/*
833b032f27cSSam Leffler 	 * Sanitize mbuf for net80211 flags leaked from above.
834b032f27cSSam Leffler 	 *
835b032f27cSSam Leffler 	 * NB: This must be done before ieee80211_classify as
836b032f27cSSam Leffler 	 *     it marks EAPOL in frames with M_EAPOL.
837b032f27cSSam Leffler 	 */
838b032f27cSSam Leffler 	m->m_flags &= ~M_80211_TX;
839bcabc908SAndriy Voskoboinyk 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
840b032f27cSSam Leffler 
841bcabc908SAndriy Voskoboinyk 	if (IEEE80211_IS_DATA(wh)) {
842b032f27cSSam Leffler 		/* calculate priority so drivers can find the tx queue */
843b032f27cSSam Leffler 		if (ieee80211_classify(ni, m))
844b032f27cSSam Leffler 			senderr(EIO);		/* XXX */
845b032f27cSSam Leffler 
846bcabc908SAndriy Voskoboinyk 		/* NB: ieee80211_encap does not include 802.11 header */
847bcabc908SAndriy Voskoboinyk 		IEEE80211_NODE_STAT_ADD(ni, tx_bytes,
848bcabc908SAndriy Voskoboinyk 		    m->m_pkthdr.len - ieee80211_hdrsize(wh));
849bcabc908SAndriy Voskoboinyk 	} else
850bcabc908SAndriy Voskoboinyk 		M_WME_SETAC(m, WME_AC_BE);
851bcabc908SAndriy Voskoboinyk 
852e42e878bSAndriy Voskoboinyk 	error = ieee80211_sanitize_rates(ni, m, params);
853e42e878bSAndriy Voskoboinyk 	if (error != 0)
854e42e878bSAndriy Voskoboinyk 		senderr(error);
855e42e878bSAndriy Voskoboinyk 
8560e910c94SSam Leffler 	IEEE80211_NODE_STAT(ni, tx_data);
8570e910c94SSam Leffler 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
8580e910c94SSam Leffler 		IEEE80211_NODE_STAT(ni, tx_mcast);
8590e910c94SSam Leffler 		m->m_flags |= M_MCAST;
8600e910c94SSam Leffler 	} else
8610e910c94SSam Leffler 		IEEE80211_NODE_STAT(ni, tx_ucast);
862b032f27cSSam Leffler 
8635cda6006SAdrian Chadd 	IEEE80211_TX_LOCK(ic);
864bcabc908SAndriy Voskoboinyk 	ret = ieee80211_raw_output(vap, ni, m, params);
8655cda6006SAdrian Chadd 	IEEE80211_TX_UNLOCK(ic);
8665cda6006SAdrian Chadd 	return (ret);
867b032f27cSSam Leffler bad:
868b032f27cSSam Leffler 	if (m != NULL)
869b032f27cSSam Leffler 		m_freem(m);
870b032f27cSSam Leffler 	if (ni != NULL)
871b032f27cSSam Leffler 		ieee80211_free_node(ni);
872dea45121SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
873b032f27cSSam Leffler 	return error;
874b032f27cSSam Leffler #undef senderr
875b032f27cSSam Leffler }
876b032f27cSSam Leffler 
877b032f27cSSam Leffler /*
878add59d08SSam Leffler  * Set the direction field and address fields of an outgoing
8790e66722dSSam Leffler  * frame.  Note this should be called early on in constructing
8800e66722dSSam Leffler  * a frame as it sets i_fc[1]; other bits can then be or'd in.
881add59d08SSam Leffler  */
88259aa14a9SRui Paulo void
ieee80211_send_setup(struct ieee80211_node * ni,struct mbuf * m,int type,int tid,const uint8_t sa[IEEE80211_ADDR_LEN],const uint8_t da[IEEE80211_ADDR_LEN],const uint8_t bssid[IEEE80211_ADDR_LEN])883b032f27cSSam Leffler ieee80211_send_setup(
884add59d08SSam Leffler 	struct ieee80211_node *ni,
8859e80b1dfSSam Leffler 	struct mbuf *m,
8868ac160cdSSam Leffler 	int type, int tid,
88768e8e04eSSam Leffler 	const uint8_t sa[IEEE80211_ADDR_LEN],
88868e8e04eSSam Leffler 	const uint8_t da[IEEE80211_ADDR_LEN],
88968e8e04eSSam Leffler 	const uint8_t bssid[IEEE80211_ADDR_LEN])
890add59d08SSam Leffler {
891add59d08SSam Leffler #define	WH4(wh)	((struct ieee80211_frame_addr4 *)wh)
89259aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
89350cfec0eSBernhard Schmidt 	struct ieee80211_tx_ampdu *tap;
8949e80b1dfSSam Leffler 	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
8959e80b1dfSSam Leffler 	ieee80211_seq seqno;
896add59d08SSam Leffler 
8976ce4aeb8SAdrian Chadd 	IEEE80211_TX_LOCK_ASSERT(ni->ni_ic);
8985cda6006SAdrian Chadd 
899add59d08SSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
900add59d08SSam Leffler 	if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
901b032f27cSSam Leffler 		switch (vap->iv_opmode) {
902add59d08SSam Leffler 		case IEEE80211_M_STA:
903add59d08SSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
904add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
905add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
906add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, da);
907add59d08SSam Leffler 			break;
908add59d08SSam Leffler 		case IEEE80211_M_IBSS:
909add59d08SSam Leffler 		case IEEE80211_M_AHDEMO:
910add59d08SSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
911add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
912add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
913add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
914add59d08SSam Leffler 			break;
915add59d08SSam Leffler 		case IEEE80211_M_HOSTAP:
916add59d08SSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
917add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
918add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
919add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, sa);
920add59d08SSam Leffler 			break;
92168e8e04eSSam Leffler 		case IEEE80211_M_WDS:
92268e8e04eSSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
923b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
924b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
92568e8e04eSSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, da);
92668e8e04eSSam Leffler 			IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
92768e8e04eSSam Leffler 			break;
92859aa14a9SRui Paulo 		case IEEE80211_M_MBSS:
92959aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
93059aa14a9SRui Paulo 			if (IEEE80211_IS_MULTICAST(da)) {
93159aa14a9SRui Paulo 				wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
93259aa14a9SRui Paulo 				/* XXX next hop */
93359aa14a9SRui Paulo 				IEEE80211_ADDR_COPY(wh->i_addr1, da);
93459aa14a9SRui Paulo 				IEEE80211_ADDR_COPY(wh->i_addr2,
93559aa14a9SRui Paulo 				    vap->iv_myaddr);
93659aa14a9SRui Paulo 			} else {
93759aa14a9SRui Paulo 				wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
93859aa14a9SRui Paulo 				IEEE80211_ADDR_COPY(wh->i_addr1, da);
93959aa14a9SRui Paulo 				IEEE80211_ADDR_COPY(wh->i_addr2,
94059aa14a9SRui Paulo 				    vap->iv_myaddr);
94159aa14a9SRui Paulo 				IEEE80211_ADDR_COPY(wh->i_addr3, da);
94259aa14a9SRui Paulo 				IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
94359aa14a9SRui Paulo 			}
94459aa14a9SRui Paulo #endif
94559aa14a9SRui Paulo 			break;
946add59d08SSam Leffler 		case IEEE80211_M_MONITOR:	/* NB: to quiet compiler */
947add59d08SSam Leffler 			break;
948add59d08SSam Leffler 		}
949add59d08SSam Leffler 	} else {
950add59d08SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
951add59d08SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, da);
952add59d08SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, sa);
95359aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
95459aa14a9SRui Paulo 		if (vap->iv_opmode == IEEE80211_M_MBSS)
95559aa14a9SRui Paulo 			IEEE80211_ADDR_COPY(wh->i_addr3, sa);
95659aa14a9SRui Paulo 		else
95759aa14a9SRui Paulo #endif
958add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
959add59d08SSam Leffler 	}
96068e8e04eSSam Leffler 	*(uint16_t *)&wh->i_dur[0] = 0;
9619e80b1dfSSam Leffler 
96251172f62SAdrian Chadd 	/*
96351172f62SAdrian Chadd 	 * XXX TODO: this is what the TX lock is for.
96451172f62SAdrian Chadd 	 * Here we're incrementing sequence numbers, and they
96551172f62SAdrian Chadd 	 * need to be in lock-step with what the driver is doing
96651172f62SAdrian Chadd 	 * both in TX ordering and crypto encap (IV increment.)
96751172f62SAdrian Chadd 	 *
96851172f62SAdrian Chadd 	 * If the driver does seqno itself, then we can skip
96951172f62SAdrian Chadd 	 * assigning sequence numbers here, and we can avoid
97051172f62SAdrian Chadd 	 * requiring the TX lock.
97151172f62SAdrian Chadd 	 */
9722aa563dfSAdrian Chadd 	tap = &ni->ni_tx_ampdu[tid];
9739764ef21SAdrian Chadd 	if (tid != IEEE80211_NONQOS_TID && IEEE80211_AMPDU_RUNNING(tap)) {
97450cfec0eSBernhard Schmidt 		m->m_flags |= M_AMPDU_MPDU;
9752db223f9SAndriy Voskoboinyk 
9762db223f9SAndriy Voskoboinyk 		/* NB: zero out i_seq field (for s/w encryption etc) */
9772db223f9SAndriy Voskoboinyk 		*(uint16_t *)&wh->i_seq[0] = 0;
9789764ef21SAdrian Chadd 	} else {
979c3ebe019SAdrian Chadd 		if (IEEE80211_HAS_SEQ(type & IEEE80211_FC0_TYPE_MASK,
980c3ebe019SAdrian Chadd 				      type & IEEE80211_FC0_SUBTYPE_MASK))
9819764ef21SAdrian Chadd 			/*
9829764ef21SAdrian Chadd 			 * 802.11-2012 9.3.2.10 - QoS multicast frames
9839764ef21SAdrian Chadd 			 * come out of a different seqno space.
9849764ef21SAdrian Chadd 			 */
9859764ef21SAdrian Chadd 			if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
9869764ef21SAdrian Chadd 				seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
9879764ef21SAdrian Chadd 			} else {
9889e80b1dfSSam Leffler 				seqno = ni->ni_txseqs[tid]++;
9899764ef21SAdrian Chadd 			}
990c3ebe019SAdrian Chadd 		else
991c3ebe019SAdrian Chadd 			seqno = 0;
992c3ebe019SAdrian Chadd 
99350cfec0eSBernhard Schmidt 		*(uint16_t *)&wh->i_seq[0] =
99450cfec0eSBernhard Schmidt 		    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
9959a841c7fSSam Leffler 		M_SEQNO_SET(m, seqno);
99650cfec0eSBernhard Schmidt 	}
9979e80b1dfSSam Leffler 
9989e80b1dfSSam Leffler 	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
9999e80b1dfSSam Leffler 		m->m_flags |= M_MCAST;
1000add59d08SSam Leffler #undef WH4
1001add59d08SSam Leffler }
1002add59d08SSam Leffler 
1003add59d08SSam Leffler /*
10040a915fadSSam Leffler  * Send a management frame to the specified node.  The node pointer
10050a915fadSSam Leffler  * must have a reference as the pointer will be passed to the driver
10060a915fadSSam Leffler  * and potentially held for a long time.  If the frame is successfully
10070a915fadSSam Leffler  * dispatched to the driver, then it is responsible for freeing the
1008b032f27cSSam Leffler  * reference (and potentially free'ing up any associated storage);
1009b032f27cSSam Leffler  * otherwise deal with reclaiming any reference (on error).
10100a915fadSSam Leffler  */
101168e8e04eSSam Leffler int
ieee80211_mgmt_output(struct ieee80211_node * ni,struct mbuf * m,int type,struct ieee80211_bpf_params * params)10128ac160cdSSam Leffler ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
10138ac160cdSSam Leffler 	struct ieee80211_bpf_params *params)
10141a1e1d21SSam Leffler {
1015b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1016b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
10171a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
10185cda6006SAdrian Chadd 	int ret;
10191a1e1d21SSam Leffler 
10200a915fadSSam Leffler 	KASSERT(ni != NULL, ("null node"));
10211a1e1d21SSam Leffler 
1022b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
1023b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
1024b032f27cSSam Leffler 		    ni, "block %s frame in CAC state",
10254357a5d1SAndriy Voskoboinyk 			ieee80211_mgt_subtype_name(type));
1026b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
1027b032f27cSSam Leffler 		ieee80211_free_node(ni);
1028b032f27cSSam Leffler 		m_freem(m);
1029b032f27cSSam Leffler 		return EIO;		/* XXX */
1030b032f27cSSam Leffler 	}
1031b032f27cSSam Leffler 
1032bd29f817SBjoern A. Zeeb 	M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT);
1033b032f27cSSam Leffler 	if (m == NULL) {
1034b032f27cSSam Leffler 		ieee80211_free_node(ni);
10351a1e1d21SSam Leffler 		return ENOMEM;
1036b032f27cSSam Leffler 	}
10370a915fadSSam Leffler 
10385cda6006SAdrian Chadd 	IEEE80211_TX_LOCK(ic);
10395cda6006SAdrian Chadd 
10401a1e1d21SSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
10419e80b1dfSSam Leffler 	ieee80211_send_setup(ni, m,
10428ac160cdSSam Leffler 	     IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
1043b032f27cSSam Leffler 	     vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
10448ac160cdSSam Leffler 	if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
1045b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
1046b032f27cSSam Leffler 		    "encrypting frame (%s)", __func__);
10475945b5f5SKevin Lo 		wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
10488a1b9b6aSSam Leffler 	}
1049c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
10508ac160cdSSam Leffler 
10518ac160cdSSam Leffler 	KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
10528ac160cdSSam Leffler 	M_WME_SETAC(m, params->ibp_pri);
10538ac160cdSSam Leffler 
10548a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
10558a1b9b6aSSam Leffler 	/* avoid printing too many frames */
1056b032f27cSSam Leffler 	if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
1057b032f27cSSam Leffler 	    ieee80211_msg_dumppkts(vap)) {
10582e59c9c7SBjoern A. Zeeb 		ieee80211_note(vap, "[%s] send %s on channel %u\n",
10598a1b9b6aSSam Leffler 		    ether_sprintf(wh->i_addr1),
10604357a5d1SAndriy Voskoboinyk 		    ieee80211_mgt_subtype_name(type),
1061b5c99415SSam Leffler 		    ieee80211_chan2ieee(ic, ic->ic_curchan));
10628a1b9b6aSSam Leffler 	}
10638a1b9b6aSSam Leffler #endif
10648a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_mgmt);
106568e8e04eSSam Leffler 
10665cda6006SAdrian Chadd 	ret = ieee80211_raw_output(vap, ni, m, params);
10675cda6006SAdrian Chadd 	IEEE80211_TX_UNLOCK(ic);
10685cda6006SAdrian Chadd 	return (ret);
1069246b5467SSam Leffler }
1070246b5467SSam Leffler 
10719f82bedaSAndriy Voskoboinyk static void
ieee80211_nulldata_transmitted(struct ieee80211_node * ni,void * arg,int status)10729f82bedaSAndriy Voskoboinyk ieee80211_nulldata_transmitted(struct ieee80211_node *ni, void *arg,
10739f82bedaSAndriy Voskoboinyk     int status)
10749f82bedaSAndriy Voskoboinyk {
10759f82bedaSAndriy Voskoboinyk 	struct ieee80211vap *vap = ni->ni_vap;
10769f82bedaSAndriy Voskoboinyk 
10779f82bedaSAndriy Voskoboinyk 	wakeup(vap);
10789f82bedaSAndriy Voskoboinyk }
10799f82bedaSAndriy Voskoboinyk 
1080246b5467SSam Leffler /*
10816683931eSSam Leffler  * Send a null data frame to the specified node.  If the station
10826683931eSSam Leffler  * is setup for QoS then a QoS Null Data frame is constructed.
10836683931eSSam Leffler  * If this is a WDS station then a 4-address frame is constructed.
108419ad2dd7SSam Leffler  *
108519ad2dd7SSam Leffler  * NB: the caller is assumed to have setup a node reference
108619ad2dd7SSam Leffler  *     for use; this is necessary to deal with a race condition
1087b032f27cSSam Leffler  *     when probing for inactive stations.  Like ieee80211_mgmt_output
1088b032f27cSSam Leffler  *     we must cleanup any node reference on error;  however we
1089b032f27cSSam Leffler  *     can safely just unref it as we know it will never be the
1090b032f27cSSam Leffler  *     last reference to the node.
10918a1b9b6aSSam Leffler  */
10928a1b9b6aSSam Leffler int
ieee80211_send_nulldata(struct ieee80211_node * ni)1093f62121ceSSam Leffler ieee80211_send_nulldata(struct ieee80211_node *ni)
10948a1b9b6aSSam Leffler {
1095b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1096f62121ceSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
10978a1b9b6aSSam Leffler 	struct mbuf *m;
10988a1b9b6aSSam Leffler 	struct ieee80211_frame *wh;
10996683931eSSam Leffler 	int hdrlen;
11006683931eSSam Leffler 	uint8_t *frm;
11015cda6006SAdrian Chadd 	int ret;
11028a1b9b6aSSam Leffler 
1103b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
1104b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
1105b032f27cSSam Leffler 		    ni, "block %s frame in CAC state", "null data");
11061137943bSBjoern A. Zeeb 		ieee80211_node_decref(ni);
1107b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
1108b032f27cSSam Leffler 		return EIO;		/* XXX */
1109b032f27cSSam Leffler 	}
1110b032f27cSSam Leffler 
11116683931eSSam Leffler 	if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
11126683931eSSam Leffler 		hdrlen = sizeof(struct ieee80211_qosframe);
11136683931eSSam Leffler 	else
11146683931eSSam Leffler 		hdrlen = sizeof(struct ieee80211_frame);
11156683931eSSam Leffler 	/* NB: only WDS vap's get 4-address frames */
11166683931eSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_WDS)
11176683931eSSam Leffler 		hdrlen += IEEE80211_ADDR_LEN;
11186683931eSSam Leffler 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
11196683931eSSam Leffler 		hdrlen = roundup(hdrlen, sizeof(uint32_t));
11206683931eSSam Leffler 
11216683931eSSam Leffler 	m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
11228a1b9b6aSSam Leffler 	if (m == NULL) {
11238a1b9b6aSSam Leffler 		/* XXX debug msg */
11241137943bSBjoern A. Zeeb 		ieee80211_node_decref(ni);
1125b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
11268a1b9b6aSSam Leffler 		return ENOMEM;
11278a1b9b6aSSam Leffler 	}
11286683931eSSam Leffler 	KASSERT(M_LEADINGSPACE(m) >= hdrlen,
11296683931eSSam Leffler 	    ("leading space %zd", M_LEADINGSPACE(m)));
1130bd29f817SBjoern A. Zeeb 	M_PREPEND(m, hdrlen, IEEE80211_M_NOWAIT);
11316683931eSSam Leffler 	if (m == NULL) {
11326683931eSSam Leffler 		/* NB: cannot happen */
11336683931eSSam Leffler 		ieee80211_free_node(ni);
11346683931eSSam Leffler 		return ENOMEM;
11356683931eSSam Leffler 	}
11368a1b9b6aSSam Leffler 
11375cda6006SAdrian Chadd 	IEEE80211_TX_LOCK(ic);
11385cda6006SAdrian Chadd 
11396683931eSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);		/* NB: a little lie */
11406683931eSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
11416683931eSSam Leffler 		const int tid = WME_AC_TO_TID(WME_AC_BE);
11426683931eSSam Leffler 		uint8_t *qos;
11436683931eSSam Leffler 
11449e80b1dfSSam Leffler 		ieee80211_send_setup(ni, m,
11456683931eSSam Leffler 		    IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
11466683931eSSam Leffler 		    tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
11476683931eSSam Leffler 
11486683931eSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_WDS)
11496683931eSSam Leffler 			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
11506683931eSSam Leffler 		else
11516683931eSSam Leffler 			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
11526683931eSSam Leffler 		qos[0] = tid & IEEE80211_QOS_TID;
11536683931eSSam Leffler 		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
11546683931eSSam Leffler 			qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
11556683931eSSam Leffler 		qos[1] = 0;
11566683931eSSam Leffler 	} else {
11579e80b1dfSSam Leffler 		ieee80211_send_setup(ni, m,
1158add59d08SSam Leffler 		    IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
11598ac160cdSSam Leffler 		    IEEE80211_NONQOS_TID,
1160b032f27cSSam Leffler 		    vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
11616683931eSSam Leffler 	}
1162b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_WDS) {
1163add59d08SSam Leffler 		/* NB: power management bit is never sent by an AP */
1164add59d08SSam Leffler 		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
1165b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_HOSTAP)
1166add59d08SSam Leffler 			wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
1167b032f27cSSam Leffler 	}
11689f82bedaSAndriy Voskoboinyk 	if ((ic->ic_flags & IEEE80211_F_SCAN) &&
11699f82bedaSAndriy Voskoboinyk 	    (ni->ni_flags & IEEE80211_NODE_PWR_MGT)) {
11709f82bedaSAndriy Voskoboinyk 		ieee80211_add_callback(m, ieee80211_nulldata_transmitted,
11719f82bedaSAndriy Voskoboinyk 		    NULL);
11729f82bedaSAndriy Voskoboinyk 	}
11736683931eSSam Leffler 	m->m_len = m->m_pkthdr.len = hdrlen;
1174c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
11756683931eSSam Leffler 
1176bb239ce9SSam Leffler 	M_WME_SETAC(m, WME_AC_BE);
11778a1b9b6aSSam Leffler 
11788a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_data);
11798a1b9b6aSSam Leffler 
1180b032f27cSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
11816683931eSSam Leffler 	    "send %snull data frame on channel %u, pwr mgt %s",
11826683931eSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
1183b5c99415SSam Leffler 	    ieee80211_chan2ieee(ic, ic->ic_curchan),
1184add59d08SSam Leffler 	    wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
1185add59d08SSam Leffler 
11865cda6006SAdrian Chadd 	ret = ieee80211_raw_output(vap, ni, m, NULL);
11875cda6006SAdrian Chadd 	IEEE80211_TX_UNLOCK(ic);
11885cda6006SAdrian Chadd 	return (ret);
11898a1b9b6aSSam Leffler }
11908a1b9b6aSSam Leffler 
11918a1b9b6aSSam Leffler /*
11928a1b9b6aSSam Leffler  * Assign priority to a frame based on any vlan tag assigned
11938a1b9b6aSSam Leffler  * to the station and/or any Diffserv setting in an IP header.
11948a1b9b6aSSam Leffler  * Finally, if an ACM policy is setup (in station mode) it's
11958a1b9b6aSSam Leffler  * applied.
11968a1b9b6aSSam Leffler  */
11978a1b9b6aSSam Leffler int
ieee80211_classify(struct ieee80211_node * ni,struct mbuf * m)1198b032f27cSSam Leffler ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
11998a1b9b6aSSam Leffler {
1200bcabc908SAndriy Voskoboinyk 	const struct ether_header *eh = NULL;
1201bcabc908SAndriy Voskoboinyk 	uint16_t ether_type;
12028a1b9b6aSSam Leffler 	int v_wme_ac, d_wme_ac, ac;
12038a1b9b6aSSam Leffler 
1204bcabc908SAndriy Voskoboinyk 	if (__predict_false(m->m_flags & M_ENCAP)) {
1205bcabc908SAndriy Voskoboinyk 		struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
1206bcabc908SAndriy Voskoboinyk 		struct llc *llc;
1207bcabc908SAndriy Voskoboinyk 		int hdrlen, subtype;
1208bcabc908SAndriy Voskoboinyk 
1209bcabc908SAndriy Voskoboinyk 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1210bcabc908SAndriy Voskoboinyk 		if (subtype & IEEE80211_FC0_SUBTYPE_NODATA) {
1211bcabc908SAndriy Voskoboinyk 			ac = WME_AC_BE;
1212bcabc908SAndriy Voskoboinyk 			goto done;
1213bcabc908SAndriy Voskoboinyk 		}
1214bcabc908SAndriy Voskoboinyk 
1215bcabc908SAndriy Voskoboinyk 		hdrlen = ieee80211_hdrsize(wh);
1216bcabc908SAndriy Voskoboinyk 		if (m->m_pkthdr.len < hdrlen + sizeof(*llc))
1217bcabc908SAndriy Voskoboinyk 			return 1;
1218bcabc908SAndriy Voskoboinyk 
1219bcabc908SAndriy Voskoboinyk 		llc = (struct llc *)mtodo(m, hdrlen);
1220bcabc908SAndriy Voskoboinyk 		if (llc->llc_dsap != LLC_SNAP_LSAP ||
1221bcabc908SAndriy Voskoboinyk 		    llc->llc_ssap != LLC_SNAP_LSAP ||
1222bcabc908SAndriy Voskoboinyk 		    llc->llc_control != LLC_UI ||
1223bcabc908SAndriy Voskoboinyk 		    llc->llc_snap.org_code[0] != 0 ||
1224bcabc908SAndriy Voskoboinyk 		    llc->llc_snap.org_code[1] != 0 ||
1225bcabc908SAndriy Voskoboinyk 		    llc->llc_snap.org_code[2] != 0)
1226bcabc908SAndriy Voskoboinyk 			return 1;
1227bcabc908SAndriy Voskoboinyk 
1228bcabc908SAndriy Voskoboinyk 		ether_type = llc->llc_snap.ether_type;
1229bcabc908SAndriy Voskoboinyk 	} else {
1230bcabc908SAndriy Voskoboinyk 		eh = mtod(m, struct ether_header *);
1231bcabc908SAndriy Voskoboinyk 		ether_type = eh->ether_type;
1232bcabc908SAndriy Voskoboinyk 	}
1233bcabc908SAndriy Voskoboinyk 
1234b032f27cSSam Leffler 	/*
1235b032f27cSSam Leffler 	 * Always promote PAE/EAPOL frames to high priority.
1236b032f27cSSam Leffler 	 */
1237bcabc908SAndriy Voskoboinyk 	if (ether_type == htons(ETHERTYPE_PAE)) {
1238b032f27cSSam Leffler 		/* NB: mark so others don't need to check header */
1239b032f27cSSam Leffler 		m->m_flags |= M_EAPOL;
1240b032f27cSSam Leffler 		ac = WME_AC_VO;
1241b032f27cSSam Leffler 		goto done;
1242b032f27cSSam Leffler 	}
1243b032f27cSSam Leffler 	/*
1244b032f27cSSam Leffler 	 * Non-qos traffic goes to BE.
1245b032f27cSSam Leffler 	 */
12468a1b9b6aSSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
12478a1b9b6aSSam Leffler 		ac = WME_AC_BE;
12488a1b9b6aSSam Leffler 		goto done;
12498a1b9b6aSSam Leffler 	}
12508a1b9b6aSSam Leffler 
12518a1b9b6aSSam Leffler 	/*
12528a1b9b6aSSam Leffler 	 * If node has a vlan tag then all traffic
12538a1b9b6aSSam Leffler 	 * to it must have a matching tag.
12548a1b9b6aSSam Leffler 	 */
12558a1b9b6aSSam Leffler 	v_wme_ac = 0;
12568a1b9b6aSSam Leffler 	if (ni->ni_vlan != 0) {
125778ba57b9SAndre Oppermann 		 if ((m->m_flags & M_VLANTAG) == 0) {
12588a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_novlantag);
12598a1b9b6aSSam Leffler 			return 1;
12608a1b9b6aSSam Leffler 		}
126178ba57b9SAndre Oppermann 		if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
12628a1b9b6aSSam Leffler 		    EVL_VLANOFTAG(ni->ni_vlan)) {
12638a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
12648a1b9b6aSSam Leffler 			return 1;
12658a1b9b6aSSam Leffler 		}
12668a1b9b6aSSam Leffler 		/* map vlan priority to AC */
1267f4558c9aSSam Leffler 		v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
12688a1b9b6aSSam Leffler 	}
12698a1b9b6aSSam Leffler 
12708def06d5SBjoern A. Zeeb 	if (eh == NULL)
12718def06d5SBjoern A. Zeeb 		goto no_eh;
12728def06d5SBjoern A. Zeeb 
1273e755a73dSSam Leffler 	/* XXX m_copydata may be too slow for fast path */
12748def06d5SBjoern A. Zeeb 	switch (ntohs(eh->ether_type)) {
12758a1b9b6aSSam Leffler #ifdef INET
12768def06d5SBjoern A. Zeeb 	case ETHERTYPE_IP:
12778def06d5SBjoern A. Zeeb 	{
1278f4558c9aSSam Leffler 		uint8_t tos;
12798a1b9b6aSSam Leffler 		/*
1280f4558c9aSSam Leffler 		 * IP frame, map the DSCP bits from the TOS field.
12818a1b9b6aSSam Leffler 		 */
1282f4558c9aSSam Leffler 		/* NB: ip header may not be in first mbuf */
1283f4558c9aSSam Leffler 		m_copydata(m, sizeof(struct ether_header) +
1284f4558c9aSSam Leffler 		    offsetof(struct ip, ip_tos), sizeof(tos), &tos);
1285f4558c9aSSam Leffler 		tos >>= 5;		/* NB: ECN + low 3 bits of DSCP */
1286f4558c9aSSam Leffler 		d_wme_ac = TID_TO_WME_AC(tos);
12878def06d5SBjoern A. Zeeb 		break;
12888def06d5SBjoern A. Zeeb 	}
12898def06d5SBjoern A. Zeeb #endif
1290e755a73dSSam Leffler #ifdef INET6
12918def06d5SBjoern A. Zeeb 	case ETHERTYPE_IPV6:
12928def06d5SBjoern A. Zeeb 	{
1293e755a73dSSam Leffler 		uint32_t flow;
1294e755a73dSSam Leffler 		uint8_t tos;
1295e755a73dSSam Leffler 		/*
12966ffd0ca9SBjoern A. Zeeb 		 * IPv6 frame, map the DSCP bits from the traffic class field.
1297e755a73dSSam Leffler 		 */
1298e755a73dSSam Leffler 		m_copydata(m, sizeof(struct ether_header) +
1299e755a73dSSam Leffler 		    offsetof(struct ip6_hdr, ip6_flow), sizeof(flow),
1300e755a73dSSam Leffler 		    (caddr_t) &flow);
1301e755a73dSSam Leffler 		tos = (uint8_t)(ntohl(flow) >> 20);
1302e755a73dSSam Leffler 		tos >>= 5;		/* NB: ECN + low 3 bits of DSCP */
1303e755a73dSSam Leffler 		d_wme_ac = TID_TO_WME_AC(tos);
13048def06d5SBjoern A. Zeeb 		break;
13058def06d5SBjoern A. Zeeb 	}
13068def06d5SBjoern A. Zeeb #endif
13078def06d5SBjoern A. Zeeb 	default:
13088def06d5SBjoern A. Zeeb no_eh:
13098a1b9b6aSSam Leffler 		d_wme_ac = WME_AC_BE;
13108def06d5SBjoern A. Zeeb 		break;
1311e755a73dSSam Leffler 	}
13128def06d5SBjoern A. Zeeb 
13138a1b9b6aSSam Leffler 	/*
13148a1b9b6aSSam Leffler 	 * Use highest priority AC.
13158a1b9b6aSSam Leffler 	 */
13168a1b9b6aSSam Leffler 	if (v_wme_ac > d_wme_ac)
13178a1b9b6aSSam Leffler 		ac = v_wme_ac;
13188a1b9b6aSSam Leffler 	else
13198a1b9b6aSSam Leffler 		ac = d_wme_ac;
13208a1b9b6aSSam Leffler 
13218a1b9b6aSSam Leffler 	/*
13228a1b9b6aSSam Leffler 	 * Apply ACM policy.
13238a1b9b6aSSam Leffler 	 */
1324b032f27cSSam Leffler 	if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
13258a1b9b6aSSam Leffler 		static const int acmap[4] = {
13268a1b9b6aSSam Leffler 			WME_AC_BK,	/* WME_AC_BE */
13278a1b9b6aSSam Leffler 			WME_AC_BK,	/* WME_AC_BK */
13288a1b9b6aSSam Leffler 			WME_AC_BE,	/* WME_AC_VI */
13298a1b9b6aSSam Leffler 			WME_AC_VI,	/* WME_AC_VO */
13308a1b9b6aSSam Leffler 		};
1331b032f27cSSam Leffler 		struct ieee80211com *ic = ni->ni_ic;
1332b032f27cSSam Leffler 
13338a1b9b6aSSam Leffler 		while (ac != WME_AC_BK &&
13348a1b9b6aSSam Leffler 		    ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
13358a1b9b6aSSam Leffler 			ac = acmap[ac];
13368a1b9b6aSSam Leffler 	}
13378a1b9b6aSSam Leffler done:
13388a1b9b6aSSam Leffler 	M_WME_SETAC(m, ac);
13398a1b9b6aSSam Leffler 	return 0;
13408a1b9b6aSSam Leffler }
13418a1b9b6aSSam Leffler 
13428a1b9b6aSSam Leffler /*
13435e923d2eSSam Leffler  * Insure there is sufficient contiguous space to encapsulate the
13445e923d2eSSam Leffler  * 802.11 data frame.  If room isn't already there, arrange for it.
13455e923d2eSSam Leffler  * Drivers and cipher modules assume we have done the necessary work
13465e923d2eSSam Leffler  * and fail rudely if they don't find the space they need.
13475e923d2eSSam Leffler  */
1348616190d0SSam Leffler struct mbuf *
ieee80211_mbuf_adjust(struct ieee80211vap * vap,int hdrsize,struct ieee80211_key * key,struct mbuf * m)1349b032f27cSSam Leffler ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
13505e923d2eSSam Leffler 	struct ieee80211_key *key, struct mbuf *m)
13515e923d2eSSam Leffler {
1352ab96db10SSam Leffler #define	TO_BE_RECLAIMED	(sizeof(struct ether_header) - sizeof(struct llc))
1353b032f27cSSam Leffler 	int needed_space = vap->iv_ic->ic_headroom + hdrsize;
13545e923d2eSSam Leffler 
13555e923d2eSSam Leffler 	if (key != NULL) {
13565e923d2eSSam Leffler 		/* XXX belongs in crypto code? */
13575e923d2eSSam Leffler 		needed_space += key->wk_cipher->ic_header;
13585e923d2eSSam Leffler 		/* XXX frags */
135983a244dbSSam Leffler 		/*
136083a244dbSSam Leffler 		 * When crypto is being done in the host we must insure
136183a244dbSSam Leffler 		 * the data are writable for the cipher routines; clone
136283a244dbSSam Leffler 		 * a writable mbuf chain.
136383a244dbSSam Leffler 		 * XXX handle SWMIC specially
136483a244dbSSam Leffler 		 */
13655c1f7f19SSam Leffler 		if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
1366bd29f817SBjoern A. Zeeb 			m = m_unshare(m, IEEE80211_M_NOWAIT);
136783a244dbSSam Leffler 			if (m == NULL) {
1368b032f27cSSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
136983a244dbSSam Leffler 				    "%s: cannot get writable mbuf\n", __func__);
1370b032f27cSSam Leffler 				vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
137183a244dbSSam Leffler 				return NULL;
137283a244dbSSam Leffler 			}
137383a244dbSSam Leffler 		}
13745e923d2eSSam Leffler 	}
13755e923d2eSSam Leffler 	/*
13765e923d2eSSam Leffler 	 * We know we are called just before stripping an Ethernet
13775e923d2eSSam Leffler 	 * header and prepending an LLC header.  This means we know
13785e923d2eSSam Leffler 	 * there will be
1379ab96db10SSam Leffler 	 *	sizeof(struct ether_header) - sizeof(struct llc)
13805e923d2eSSam Leffler 	 * bytes recovered to which we need additional space for the
13815e923d2eSSam Leffler 	 * 802.11 header and any crypto header.
13825e923d2eSSam Leffler 	 */
13835e923d2eSSam Leffler 	/* XXX check trailing space and copy instead? */
13845e923d2eSSam Leffler 	if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
1385bd29f817SBjoern A. Zeeb 		struct mbuf *n = m_gethdr(IEEE80211_M_NOWAIT, m->m_type);
13865e923d2eSSam Leffler 		if (n == NULL) {
1387b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
13885e923d2eSSam Leffler 			    "%s: cannot expand storage\n", __func__);
1389b032f27cSSam Leffler 			vap->iv_stats.is_tx_nobuf++;
13905e923d2eSSam Leffler 			m_freem(m);
13915e923d2eSSam Leffler 			return NULL;
13925e923d2eSSam Leffler 		}
13935e923d2eSSam Leffler 		KASSERT(needed_space <= MHLEN,
1394d7b5c50bSAlfred Perlstein 		    ("not enough room, need %u got %d\n", needed_space, MHLEN));
13955e923d2eSSam Leffler 		/*
13965e923d2eSSam Leffler 		 * Setup new mbuf to have leading space to prepend the
13975e923d2eSSam Leffler 		 * 802.11 header and any crypto header bits that are
13985e923d2eSSam Leffler 		 * required (the latter are added when the driver calls
13995e923d2eSSam Leffler 		 * back to ieee80211_crypto_encap to do crypto encapsulation).
14005e923d2eSSam Leffler 		 */
14015e923d2eSSam Leffler 		/* NB: must be first 'cuz it clobbers m_data */
14025e923d2eSSam Leffler 		m_move_pkthdr(n, m);
14035e923d2eSSam Leffler 		n->m_len = 0;			/* NB: m_gethdr does not set */
14045e923d2eSSam Leffler 		n->m_data += needed_space;
14055e923d2eSSam Leffler 		/*
14065e923d2eSSam Leffler 		 * Pull up Ethernet header to create the expected layout.
14075e923d2eSSam Leffler 		 * We could use m_pullup but that's overkill (i.e. we don't
14085e923d2eSSam Leffler 		 * need the actual data) and it cannot fail so do it inline
14095e923d2eSSam Leffler 		 * for speed.
14105e923d2eSSam Leffler 		 */
14115e923d2eSSam Leffler 		/* NB: struct ether_header is known to be contiguous */
14125e923d2eSSam Leffler 		n->m_len += sizeof(struct ether_header);
14135e923d2eSSam Leffler 		m->m_len -= sizeof(struct ether_header);
14145e923d2eSSam Leffler 		m->m_data += sizeof(struct ether_header);
14155e923d2eSSam Leffler 		/*
14165e923d2eSSam Leffler 		 * Replace the head of the chain.
14175e923d2eSSam Leffler 		 */
14185e923d2eSSam Leffler 		n->m_next = m;
14195e923d2eSSam Leffler 		m = n;
14205e923d2eSSam Leffler 	}
14215e923d2eSSam Leffler 	return m;
14225e923d2eSSam Leffler #undef TO_BE_RECLAIMED
14235e923d2eSSam Leffler }
14245e923d2eSSam Leffler 
14255e923d2eSSam Leffler /*
14265e923d2eSSam Leffler  * Return the transmit key to use in sending a unicast frame.
14275e923d2eSSam Leffler  * If a unicast key is set we use that.  When no unicast key is set
14285e923d2eSSam Leffler  * we fall back to the default transmit key.
14298a1b9b6aSSam Leffler  */
14308a1b9b6aSSam Leffler static __inline struct ieee80211_key *
ieee80211_crypto_getucastkey(struct ieee80211vap * vap,struct ieee80211_node * ni)1431b032f27cSSam Leffler ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
1432b032f27cSSam Leffler 	struct ieee80211_node *ni)
14338a1b9b6aSSam Leffler {
1434cda15ce1SSam Leffler 	if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
1435b032f27cSSam Leffler 		if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1436b032f27cSSam Leffler 		    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
14378a1b9b6aSSam Leffler 			return NULL;
1438b032f27cSSam Leffler 		return &vap->iv_nw_keys[vap->iv_def_txkey];
14398a1b9b6aSSam Leffler 	} else {
14408a1b9b6aSSam Leffler 		return &ni->ni_ucastkey;
14418a1b9b6aSSam Leffler 	}
14425e923d2eSSam Leffler }
14435e923d2eSSam Leffler 
14445e923d2eSSam Leffler /*
14455e923d2eSSam Leffler  * Return the transmit key to use in sending a multicast frame.
14465e923d2eSSam Leffler  * Multicast traffic always uses the group key which is installed as
14475e923d2eSSam Leffler  * the default tx key.
14485e923d2eSSam Leffler  */
14495e923d2eSSam Leffler static __inline struct ieee80211_key *
ieee80211_crypto_getmcastkey(struct ieee80211vap * vap,struct ieee80211_node * ni)1450b032f27cSSam Leffler ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
1451b032f27cSSam Leffler 	struct ieee80211_node *ni)
14525e923d2eSSam Leffler {
1453b032f27cSSam Leffler 	if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1454b032f27cSSam Leffler 	    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
14555e923d2eSSam Leffler 		return NULL;
1456b032f27cSSam Leffler 	return &vap->iv_nw_keys[vap->iv_def_txkey];
14578a1b9b6aSSam Leffler }
14588a1b9b6aSSam Leffler 
14598a1b9b6aSSam Leffler /*
14608a1b9b6aSSam Leffler  * Encapsulate an outbound data frame.  The mbuf chain is updated.
14618a1b9b6aSSam Leffler  * If an error is encountered NULL is returned.  The caller is required
14628a1b9b6aSSam Leffler  * to provide a node reference and pullup the ethernet header in the
14638a1b9b6aSSam Leffler  * first mbuf.
1464b032f27cSSam Leffler  *
1465b032f27cSSam Leffler  * NB: Packet is assumed to be processed by ieee80211_classify which
1466b032f27cSSam Leffler  *     marked EAPOL frames w/ M_EAPOL.
14670a915fadSSam Leffler  */
14681a1e1d21SSam Leffler struct mbuf *
ieee80211_encap(struct ieee80211vap * vap,struct ieee80211_node * ni,struct mbuf * m)1469339ccfb3SSam Leffler ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
1470339ccfb3SSam Leffler     struct mbuf *m)
14711a1e1d21SSam Leffler {
1472b032f27cSSam Leffler #define	WH4(wh)	((struct ieee80211_frame_addr4 *)(wh))
14733c314f6dSMonthadar Al Jaberi #define MC01(mc)	((struct ieee80211_meshcntl_ae01 *)mc)
1474b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
147559aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
147659aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1477c104cff2SRui Paulo 	struct ieee80211_meshcntl_ae10 *mc;
14783c314f6dSMonthadar Al Jaberi 	struct ieee80211_mesh_route *rt = NULL;
14793c314f6dSMonthadar Al Jaberi 	int dir = -1;
148059aa14a9SRui Paulo #endif
14811a1e1d21SSam Leffler 	struct ether_header eh;
14821a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
14838a1b9b6aSSam Leffler 	struct ieee80211_key *key;
14841a1e1d21SSam Leffler 	struct llc *llc;
14859764ef21SAdrian Chadd 	int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr, is_mcast;
14869e80b1dfSSam Leffler 	ieee80211_seq seqno;
148759aa14a9SRui Paulo 	int meshhdrsize, meshae;
148859aa14a9SRui Paulo 	uint8_t *qos;
14891c7b0c84SAdrian Chadd 	int is_amsdu = 0;
14901a1e1d21SSam Leffler 
14915cda6006SAdrian Chadd 	IEEE80211_TX_LOCK_ASSERT(ic);
14925cda6006SAdrian Chadd 
14939764ef21SAdrian Chadd 	is_mcast = !! (m->m_flags & (M_MCAST | M_BCAST));
14949764ef21SAdrian Chadd 
149568e8e04eSSam Leffler 	/*
149668e8e04eSSam Leffler 	 * Copy existing Ethernet header to a safe place.  The
149768e8e04eSSam Leffler 	 * rest of the code assumes it's ok to strip it when
149868e8e04eSSam Leffler 	 * reorganizing state for the final encapsulation.
149968e8e04eSSam Leffler 	 */
15008a1b9b6aSSam Leffler 	KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
1501b032f27cSSam Leffler 	ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
15021a1e1d21SSam Leffler 
15038a1b9b6aSSam Leffler 	/*
15048a1b9b6aSSam Leffler 	 * Insure space for additional headers.  First identify
15058a1b9b6aSSam Leffler 	 * transmit key to use in calculating any buffer adjustments
15068a1b9b6aSSam Leffler 	 * required.  This is also used below to do privacy
15078a1b9b6aSSam Leffler 	 * encapsulation work.  Then calculate the 802.11 header
15088a1b9b6aSSam Leffler 	 * size and any padding required by the driver.
15098a1b9b6aSSam Leffler 	 *
15108a1b9b6aSSam Leffler 	 * Note key may be NULL if we fall back to the default
15118a1b9b6aSSam Leffler 	 * transmit key and that is not set.  In that case the
15128a1b9b6aSSam Leffler 	 * buffer may not be expanded as needed by the cipher
15138a1b9b6aSSam Leffler 	 * routines, but they will/should discard it.
15148a1b9b6aSSam Leffler 	 */
1515b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1516b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA ||
1517b032f27cSSam Leffler 		    !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
1518b032f27cSSam Leffler 		    (vap->iv_opmode == IEEE80211_M_WDS &&
15192172664cSAdrian Chadd 		     (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY))) {
1520b032f27cSSam Leffler 			key = ieee80211_crypto_getucastkey(vap, ni);
15212172664cSAdrian Chadd 		} else if ((vap->iv_opmode == IEEE80211_M_WDS) &&
15222172664cSAdrian Chadd 		    (! (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY))) {
15232172664cSAdrian Chadd 			/*
15242172664cSAdrian Chadd 			 * Use ucastkey for DWDS transmit nodes, multicast
15252172664cSAdrian Chadd 			 * or otherwise.
15262172664cSAdrian Chadd 			 *
15272172664cSAdrian Chadd 			 * This is required to ensure that multicast frames
15282172664cSAdrian Chadd 			 * from a DWDS AP to a DWDS STA is encrypted with
15292172664cSAdrian Chadd 			 * a key that can actually work.
15302172664cSAdrian Chadd 			 *
15312172664cSAdrian Chadd 			 * There's no default key for multicast traffic
15322172664cSAdrian Chadd 			 * on a DWDS WDS VAP node (note NOT the DWDS enabled
15332172664cSAdrian Chadd 			 * AP VAP, the dynamically created per-STA WDS node)
15342172664cSAdrian Chadd 			 * so encap fails and transmit fails.
15352172664cSAdrian Chadd 			 */
15362172664cSAdrian Chadd 			key = ieee80211_crypto_getucastkey(vap, ni);
15372172664cSAdrian Chadd 		} else {
1538b032f27cSSam Leffler 			key = ieee80211_crypto_getmcastkey(vap, ni);
15392172664cSAdrian Chadd 		}
1540b032f27cSSam Leffler 		if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
1541b032f27cSSam Leffler 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
1542b032f27cSSam Leffler 			    eh.ether_dhost,
1543b032f27cSSam Leffler 			    "no default transmit key (%s) deftxkey %u",
1544b032f27cSSam Leffler 			    __func__, vap->iv_def_txkey);
1545b032f27cSSam Leffler 			vap->iv_stats.is_tx_nodefkey++;
1546d72c7253SSam Leffler 			goto bad;
15478a1b9b6aSSam Leffler 		}
15488a1b9b6aSSam Leffler 	} else
15498a1b9b6aSSam Leffler 		key = NULL;
15505e923d2eSSam Leffler 	/*
15515e923d2eSSam Leffler 	 * XXX Some ap's don't handle QoS-encapsulated EAPOL
15525e923d2eSSam Leffler 	 * frames so suppress use.  This may be an issue if other
15535e923d2eSSam Leffler 	 * ap's require all data frames to be QoS-encapsulated
15545e923d2eSSam Leffler 	 * once negotiated in which case we'll need to make this
15555e923d2eSSam Leffler 	 * configurable.
15569764ef21SAdrian Chadd 	 *
15579764ef21SAdrian Chadd 	 * Don't send multicast QoS frames.
15589764ef21SAdrian Chadd 	 * Technically multicast frames can be QoS if all stations in the
15599764ef21SAdrian Chadd 	 * BSS are also QoS.
15609764ef21SAdrian Chadd 	 *
15619764ef21SAdrian Chadd 	 * NB: mesh data frames are QoS, including multicast frames.
15625e923d2eSSam Leffler 	 */
15639764ef21SAdrian Chadd 	addqos =
15649764ef21SAdrian Chadd 	    (((is_mcast == 0) && (ni->ni_flags &
15659764ef21SAdrian Chadd 	     (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))) ||
156691216c71SAdrian Chadd 	    (vap->iv_opmode == IEEE80211_M_MBSS)) &&
1567b032f27cSSam Leffler 	    (m->m_flags & M_EAPOL) == 0;
15689764ef21SAdrian Chadd 
15695e923d2eSSam Leffler 	if (addqos)
15708a1b9b6aSSam Leffler 		hdrsize = sizeof(struct ieee80211_qosframe);
15718a1b9b6aSSam Leffler 	else
15728a1b9b6aSSam Leffler 		hdrsize = sizeof(struct ieee80211_frame);
157359aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
157459aa14a9SRui Paulo 	if (vap->iv_opmode == IEEE80211_M_MBSS) {
157559aa14a9SRui Paulo 		/*
157659aa14a9SRui Paulo 		 * Mesh data frames are encapsulated according to the
157759aa14a9SRui Paulo 		 * rules of Section 11B.8.5 (p.139 of D3.0 spec).
157859aa14a9SRui Paulo 		 * o Group Addressed data (aka multicast) originating
157959aa14a9SRui Paulo 		 *   at the local sta are sent w/ 3-address format and
158059aa14a9SRui Paulo 		 *   address extension mode 00
158159aa14a9SRui Paulo 		 * o Individually Addressed data (aka unicast) originating
158259aa14a9SRui Paulo 		 *   at the local sta are sent w/ 4-address format and
158359aa14a9SRui Paulo 		 *   address extension mode 00
158459aa14a9SRui Paulo 		 * o Group Addressed data forwarded from a non-mesh sta are
158559aa14a9SRui Paulo 		 *   sent w/ 3-address format and address extension mode 01
158659aa14a9SRui Paulo 		 * o Individually Address data from another sta are sent
158759aa14a9SRui Paulo 		 *   w/ 4-address format and address extension mode 10
158859aa14a9SRui Paulo 		 */
158959aa14a9SRui Paulo 		is4addr = 0;		/* NB: don't use, disable */
15903c314f6dSMonthadar Al Jaberi 		if (!IEEE80211_IS_MULTICAST(eh.ether_dhost)) {
15913c314f6dSMonthadar Al Jaberi 			rt = ieee80211_mesh_rt_find(vap, eh.ether_dhost);
15923c314f6dSMonthadar Al Jaberi 			KASSERT(rt != NULL, ("route is NULL"));
15933c314f6dSMonthadar Al Jaberi 			dir = IEEE80211_FC1_DIR_DSTODS;
15943c314f6dSMonthadar Al Jaberi 			hdrsize += IEEE80211_ADDR_LEN;
15953c314f6dSMonthadar Al Jaberi 			if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
15963c314f6dSMonthadar Al Jaberi 				if (IEEE80211_ADDR_EQ(rt->rt_mesh_gate,
15973c314f6dSMonthadar Al Jaberi 				    vap->iv_myaddr)) {
15983c314f6dSMonthadar Al Jaberi 					IEEE80211_NOTE_MAC(vap,
15993c314f6dSMonthadar Al Jaberi 					    IEEE80211_MSG_MESH,
16003c314f6dSMonthadar Al Jaberi 					    eh.ether_dhost,
16013c314f6dSMonthadar Al Jaberi 					    "%s", "trying to send to ourself");
16023c314f6dSMonthadar Al Jaberi 					goto bad;
16033c314f6dSMonthadar Al Jaberi 				}
16043c314f6dSMonthadar Al Jaberi 				meshae = IEEE80211_MESH_AE_10;
16053c314f6dSMonthadar Al Jaberi 				meshhdrsize =
16063c314f6dSMonthadar Al Jaberi 				    sizeof(struct ieee80211_meshcntl_ae10);
160759aa14a9SRui Paulo 			} else {
16083c314f6dSMonthadar Al Jaberi 				meshae = IEEE80211_MESH_AE_00;
16093c314f6dSMonthadar Al Jaberi 				meshhdrsize =
16103c314f6dSMonthadar Al Jaberi 				    sizeof(struct ieee80211_meshcntl);
16113c314f6dSMonthadar Al Jaberi 			}
16123c314f6dSMonthadar Al Jaberi 		} else {
16133c314f6dSMonthadar Al Jaberi 			dir = IEEE80211_FC1_DIR_FROMDS;
16143c314f6dSMonthadar Al Jaberi 			if (!IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) {
16153c314f6dSMonthadar Al Jaberi 				/* proxy group */
16163c314f6dSMonthadar Al Jaberi 				meshae = IEEE80211_MESH_AE_01;
16173c314f6dSMonthadar Al Jaberi 				meshhdrsize =
16183c314f6dSMonthadar Al Jaberi 				    sizeof(struct ieee80211_meshcntl_ae01);
16193c314f6dSMonthadar Al Jaberi 			} else {
16203c314f6dSMonthadar Al Jaberi 				/* group */
16213c314f6dSMonthadar Al Jaberi 				meshae = IEEE80211_MESH_AE_00;
16223c314f6dSMonthadar Al Jaberi 				meshhdrsize = sizeof(struct ieee80211_meshcntl);
16233c314f6dSMonthadar Al Jaberi 			}
162459aa14a9SRui Paulo 		}
162559aa14a9SRui Paulo 	} else {
162659aa14a9SRui Paulo #endif
1627b032f27cSSam Leffler 		/*
1628b032f27cSSam Leffler 		 * 4-address frames need to be generated for:
16296437e6daSSam Leffler 		 * o packets sent through a WDS vap (IEEE80211_M_WDS)
1630f2a6a13cSSam Leffler 		 * o packets sent through a vap marked for relaying
1631f2a6a13cSSam Leffler 		 *   (e.g. a station operating with dynamic WDS)
1632b032f27cSSam Leffler 		 */
16336437e6daSSam Leffler 		is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
1634f2a6a13cSSam Leffler 		    ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
1635b032f27cSSam Leffler 		     !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
1636b032f27cSSam Leffler 		if (is4addr)
1637b032f27cSSam Leffler 			hdrsize += IEEE80211_ADDR_LEN;
163859aa14a9SRui Paulo 		meshhdrsize = meshae = 0;
163959aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
164059aa14a9SRui Paulo 	}
164159aa14a9SRui Paulo #endif
1642b032f27cSSam Leffler 	/*
1643b032f27cSSam Leffler 	 * Honor driver DATAPAD requirement.
1644b032f27cSSam Leffler 	 */
16458a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
1646b032f27cSSam Leffler 		hdrspace = roundup(hdrsize, sizeof(uint32_t));
1647b032f27cSSam Leffler 	else
1648b032f27cSSam Leffler 		hdrspace = hdrsize;
164968e8e04eSSam Leffler 
1650616190d0SSam Leffler 	if (__predict_true((m->m_flags & M_FF) == 0)) {
165168e8e04eSSam Leffler 		/*
165268e8e04eSSam Leffler 		 * Normal frame.
165368e8e04eSSam Leffler 		 */
165459aa14a9SRui Paulo 		m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m);
16558a1b9b6aSSam Leffler 		if (m == NULL) {
16568a1b9b6aSSam Leffler 			/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
16570a915fadSSam Leffler 			goto bad;
16580a915fadSSam Leffler 		}
165968e8e04eSSam Leffler 		/* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
1660ab96db10SSam Leffler 		m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
16611a1e1d21SSam Leffler 		llc = mtod(m, struct llc *);
16621a1e1d21SSam Leffler 		llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
16631a1e1d21SSam Leffler 		llc->llc_control = LLC_UI;
16641a1e1d21SSam Leffler 		llc->llc_snap.org_code[0] = 0;
16651a1e1d21SSam Leffler 		llc->llc_snap.org_code[1] = 0;
16661a1e1d21SSam Leffler 		llc->llc_snap.org_code[2] = 0;
16671a1e1d21SSam Leffler 		llc->llc_snap.ether_type = eh.ether_type;
1668616190d0SSam Leffler 	} else {
1669616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1670339ccfb3SSam Leffler 		/*
16711c7b0c84SAdrian Chadd 		 * Aggregated frame.  Check if it's for AMSDU or FF.
16721c7b0c84SAdrian Chadd 		 *
16731c7b0c84SAdrian Chadd 		 * XXX TODO: IEEE80211_NODE_AMSDU* isn't implemented
16741c7b0c84SAdrian Chadd 		 * anywhere for some reason.  But, since 11n requires
16751c7b0c84SAdrian Chadd 		 * AMSDU RX, we can just assume "11n" == "AMSDU".
1676339ccfb3SSam Leffler 		 */
16771c7b0c84SAdrian Chadd 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, "%s: called; M_FF\n", __func__);
16781c7b0c84SAdrian Chadd 		if (ieee80211_amsdu_tx_ok(ni)) {
16791c7b0c84SAdrian Chadd 			m = ieee80211_amsdu_encap(vap, m, hdrspace + meshhdrsize, key);
16801c7b0c84SAdrian Chadd 			is_amsdu = 1;
16811c7b0c84SAdrian Chadd 		} else {
168259aa14a9SRui Paulo 			m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key);
16831c7b0c84SAdrian Chadd 		}
1684616190d0SSam Leffler 		if (m == NULL)
1685616190d0SSam Leffler #endif
1686616190d0SSam Leffler 			goto bad;
168768e8e04eSSam Leffler 	}
16888a1b9b6aSSam Leffler 	datalen = m->m_pkthdr.len;		/* NB: w/o 802.11 header */
16898a1b9b6aSSam Leffler 
1690bd29f817SBjoern A. Zeeb 	M_PREPEND(m, hdrspace + meshhdrsize, IEEE80211_M_NOWAIT);
16911be50176SSam Leffler 	if (m == NULL) {
1692b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
16930a915fadSSam Leffler 		goto bad;
16941be50176SSam Leffler 	}
16951a1e1d21SSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
16961a1e1d21SSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
169768e8e04eSSam Leffler 	*(uint16_t *)wh->i_dur = 0;
169859aa14a9SRui Paulo 	qos = NULL;	/* NB: quiet compiler */
1699b032f27cSSam Leffler 	if (is4addr) {
1700b032f27cSSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1701b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1702b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1703b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1704b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1705b032f27cSSam Leffler 	} else switch (vap->iv_opmode) {
17061a1e1d21SSam Leffler 	case IEEE80211_M_STA:
17071a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
17081a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
17091a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
17101a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
17111a1e1d21SSam Leffler 		break;
17121a1e1d21SSam Leffler 	case IEEE80211_M_IBSS:
17131a1e1d21SSam Leffler 	case IEEE80211_M_AHDEMO:
17141a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
17151a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
17161a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1717a8b16e87SSam Leffler 		/*
1718b032f27cSSam Leffler 		 * NB: always use the bssid from iv_bss as the
1719a8b16e87SSam Leffler 		 *     neighbor's may be stale after an ibss merge
1720a8b16e87SSam Leffler 		 */
1721b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
17221a1e1d21SSam Leffler 		break;
17231a1e1d21SSam Leffler 	case IEEE80211_M_HOSTAP:
17241a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
17251a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
17261a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
17271a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
17281a1e1d21SSam Leffler 		break;
172959aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
173059aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
173159aa14a9SRui Paulo 		/* NB: offset by hdrspace to deal with DATAPAD */
1732c104cff2SRui Paulo 		mc = (struct ieee80211_meshcntl_ae10 *)
173359aa14a9SRui Paulo 		     (mtod(m, uint8_t *) + hdrspace);
17343c314f6dSMonthadar Al Jaberi 		wh->i_fc[1] = dir;
173559aa14a9SRui Paulo 		switch (meshae) {
17363c314f6dSMonthadar Al Jaberi 		case IEEE80211_MESH_AE_00:	/* no proxy */
173759aa14a9SRui Paulo 			mc->mc_flags = 0;
17383c314f6dSMonthadar Al Jaberi 			if (dir == IEEE80211_FC1_DIR_DSTODS) { /* ucast */
17393c314f6dSMonthadar Al Jaberi 				IEEE80211_ADDR_COPY(wh->i_addr1,
17403c314f6dSMonthadar Al Jaberi 				    ni->ni_macaddr);
17413c314f6dSMonthadar Al Jaberi 				IEEE80211_ADDR_COPY(wh->i_addr2,
17423c314f6dSMonthadar Al Jaberi 				    vap->iv_myaddr);
17433c314f6dSMonthadar Al Jaberi 				IEEE80211_ADDR_COPY(wh->i_addr3,
17443c314f6dSMonthadar Al Jaberi 				    eh.ether_dhost);
17453c314f6dSMonthadar Al Jaberi 				IEEE80211_ADDR_COPY(WH4(wh)->i_addr4,
17463c314f6dSMonthadar Al Jaberi 				    eh.ether_shost);
17473c314f6dSMonthadar Al Jaberi 				qos =((struct ieee80211_qosframe_addr4 *)
17483c314f6dSMonthadar Al Jaberi 				    wh)->i_qos;
17493c314f6dSMonthadar Al Jaberi 			} else if (dir == IEEE80211_FC1_DIR_FROMDS) {
17503c314f6dSMonthadar Al Jaberi 				 /* mcast */
17513c314f6dSMonthadar Al Jaberi 				IEEE80211_ADDR_COPY(wh->i_addr1,
17523c314f6dSMonthadar Al Jaberi 				    eh.ether_dhost);
17533c314f6dSMonthadar Al Jaberi 				IEEE80211_ADDR_COPY(wh->i_addr2,
17543c314f6dSMonthadar Al Jaberi 				    vap->iv_myaddr);
17553c314f6dSMonthadar Al Jaberi 				IEEE80211_ADDR_COPY(wh->i_addr3,
17563c314f6dSMonthadar Al Jaberi 				    eh.ether_shost);
17573c314f6dSMonthadar Al Jaberi 				qos = ((struct ieee80211_qosframe *)
17583c314f6dSMonthadar Al Jaberi 				    wh)->i_qos;
17593c314f6dSMonthadar Al Jaberi 			}
176059aa14a9SRui Paulo 			break;
17613c314f6dSMonthadar Al Jaberi 		case IEEE80211_MESH_AE_01:	/* mcast, proxy */
176259aa14a9SRui Paulo 			wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
176359aa14a9SRui Paulo 			IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
176459aa14a9SRui Paulo 			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1765c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr);
176659aa14a9SRui Paulo 			mc->mc_flags = 1;
17673c314f6dSMonthadar Al Jaberi 			IEEE80211_ADDR_COPY(MC01(mc)->mc_addr4,
17683c314f6dSMonthadar Al Jaberi 			    eh.ether_shost);
176959aa14a9SRui Paulo 			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
177059aa14a9SRui Paulo 			break;
17713c314f6dSMonthadar Al Jaberi 		case IEEE80211_MESH_AE_10:	/* ucast, proxy */
17723c314f6dSMonthadar Al Jaberi 			KASSERT(rt != NULL, ("route is NULL"));
17733c314f6dSMonthadar Al Jaberi 			IEEE80211_ADDR_COPY(wh->i_addr1, rt->rt_nexthop);
177459aa14a9SRui Paulo 			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
17753c314f6dSMonthadar Al Jaberi 			IEEE80211_ADDR_COPY(wh->i_addr3, rt->rt_mesh_gate);
1776c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr);
17773c314f6dSMonthadar Al Jaberi 			mc->mc_flags = IEEE80211_MESH_AE_10;
17783c314f6dSMonthadar Al Jaberi 			IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_dhost);
17793c314f6dSMonthadar Al Jaberi 			IEEE80211_ADDR_COPY(mc->mc_addr6, eh.ether_shost);
178059aa14a9SRui Paulo 			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
178159aa14a9SRui Paulo 			break;
178259aa14a9SRui Paulo 		default:
178359aa14a9SRui Paulo 			KASSERT(0, ("meshae %d", meshae));
178459aa14a9SRui Paulo 			break;
178559aa14a9SRui Paulo 		}
178659aa14a9SRui Paulo 		mc->mc_ttl = ms->ms_ttl;
178759aa14a9SRui Paulo 		ms->ms_seq++;
178831021a2bSAndriy Voskoboinyk 		le32enc(mc->mc_seq, ms->ms_seq);
178959aa14a9SRui Paulo 		break;
179059aa14a9SRui Paulo #endif
1791b032f27cSSam Leffler 	case IEEE80211_M_WDS:		/* NB: is4addr should always be true */
179259aa14a9SRui Paulo 	default:
17930a915fadSSam Leffler 		goto bad;
17941a1e1d21SSam Leffler 	}
1795bc5627d9SSam Leffler 	if (m->m_flags & M_MORE_DATA)
1796bc5627d9SSam Leffler 		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
17975e923d2eSSam Leffler 	if (addqos) {
17988a1b9b6aSSam Leffler 		int ac, tid;
17998a1b9b6aSSam Leffler 
1800b032f27cSSam Leffler 		if (is4addr) {
1801b032f27cSSam Leffler 			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
180259aa14a9SRui Paulo 		/* NB: mesh case handled earlier */
180359aa14a9SRui Paulo 		} else if (vap->iv_opmode != IEEE80211_M_MBSS)
1804b032f27cSSam Leffler 			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
18058a1b9b6aSSam Leffler 		ac = M_WME_GETAC(m);
18068a1b9b6aSSam Leffler 		/* map from access class/queue to 11e header priorty value */
18078a1b9b6aSSam Leffler 		tid = WME_AC_TO_TID(ac);
1808b032f27cSSam Leffler 		qos[0] = tid & IEEE80211_QOS_TID;
18098a1b9b6aSSam Leffler 		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
1810b032f27cSSam Leffler 			qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
181191216c71SAdrian Chadd #ifdef IEEE80211_SUPPORT_MESH
1812d566999aSMonthadar Al Jaberi 		if (vap->iv_opmode == IEEE80211_M_MBSS)
1813d566999aSMonthadar Al Jaberi 			qos[1] = IEEE80211_QOS_MC;
1814d566999aSMonthadar Al Jaberi 		else
181591216c71SAdrian Chadd #endif
1816b032f27cSSam Leffler 			qos[1] = 0;
1817c9b7e9dfSBjoern A. Zeeb 		wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS_DATA;
18188a1b9b6aSSam Leffler 
18191c7b0c84SAdrian Chadd 		/*
18201c7b0c84SAdrian Chadd 		 * If this is an A-MSDU then ensure we set the
18211c7b0c84SAdrian Chadd 		 * relevant field.
18221c7b0c84SAdrian Chadd 		 */
18231c7b0c84SAdrian Chadd 		if (is_amsdu)
18241c7b0c84SAdrian Chadd 			qos[0] |= IEEE80211_QOS_AMSDU;
18251c7b0c84SAdrian Chadd 
182651172f62SAdrian Chadd 		/*
182751172f62SAdrian Chadd 		 * XXX TODO TX lock is needed for atomic updates of sequence
182851172f62SAdrian Chadd 		 * numbers.  If the driver does it, then don't do it here;
182951172f62SAdrian Chadd 		 * and we don't need the TX lock held.
183051172f62SAdrian Chadd 		 */
183145f856e3SSam Leffler 		if ((m->m_flags & M_AMPDU_MPDU) == 0) {
183245f856e3SSam Leffler 			/*
18339764ef21SAdrian Chadd 			 * 802.11-2012 9.3.2.10 -
18349764ef21SAdrian Chadd 			 *
18359764ef21SAdrian Chadd 			 * If this is a multicast frame then we need
18369764ef21SAdrian Chadd 			 * to ensure that the sequence number comes from
18379764ef21SAdrian Chadd 			 * a separate seqno space and not the TID space.
18389764ef21SAdrian Chadd 			 *
18399764ef21SAdrian Chadd 			 * Otherwise multicast frames may actually cause
18409764ef21SAdrian Chadd 			 * holes in the TX blockack window space and
18419764ef21SAdrian Chadd 			 * upset various things.
18429764ef21SAdrian Chadd 			 */
18439764ef21SAdrian Chadd 			if (IEEE80211_IS_MULTICAST(wh->i_addr1))
18449764ef21SAdrian Chadd 				seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
18459764ef21SAdrian Chadd 			else
18469764ef21SAdrian Chadd 				seqno = ni->ni_txseqs[tid]++;
18479764ef21SAdrian Chadd 
18489764ef21SAdrian Chadd 			/*
184945f856e3SSam Leffler 			 * NB: don't assign a sequence # to potential
185045f856e3SSam Leffler 			 * aggregates; we expect this happens at the
185145f856e3SSam Leffler 			 * point the frame comes off any aggregation q
185245f856e3SSam Leffler 			 * as otherwise we may introduce holes in the
185345f856e3SSam Leffler 			 * BA sequence space and/or make window accouting
185445f856e3SSam Leffler 			 * more difficult.
185545f856e3SSam Leffler 			 *
185645f856e3SSam Leffler 			 * XXX may want to control this with a driver
185745f856e3SSam Leffler 			 * capability; this may also change when we pull
185845f856e3SSam Leffler 			 * aggregation up into net80211
185945f856e3SSam Leffler 			 */
186068e8e04eSSam Leffler 			*(uint16_t *)wh->i_seq =
18619e80b1dfSSam Leffler 			    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
18629a841c7fSSam Leffler 			M_SEQNO_SET(m, seqno);
18632db223f9SAndriy Voskoboinyk 		} else {
18642db223f9SAndriy Voskoboinyk 			/* NB: zero out i_seq field (for s/w encryption etc) */
18652db223f9SAndriy Voskoboinyk 			*(uint16_t *)wh->i_seq = 0;
186645f856e3SSam Leffler 		}
18678a1b9b6aSSam Leffler 	} else {
186851172f62SAdrian Chadd 		/*
186951172f62SAdrian Chadd 		 * XXX TODO TX lock is needed for atomic updates of sequence
187051172f62SAdrian Chadd 		 * numbers.  If the driver does it, then don't do it here;
187151172f62SAdrian Chadd 		 * and we don't need the TX lock held.
187251172f62SAdrian Chadd 		 */
18739e80b1dfSSam Leffler 		seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
187468e8e04eSSam Leffler 		*(uint16_t *)wh->i_seq =
18759e80b1dfSSam Leffler 		    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
18769a841c7fSSam Leffler 		M_SEQNO_SET(m, seqno);
18771c7b0c84SAdrian Chadd 
18781c7b0c84SAdrian Chadd 		/*
18791c7b0c84SAdrian Chadd 		 * XXX TODO: we shouldn't allow EAPOL, etc that would
18801c7b0c84SAdrian Chadd 		 * be forced to be non-QoS traffic to be A-MSDU encapsulated.
18811c7b0c84SAdrian Chadd 		 */
18821c7b0c84SAdrian Chadd 		if (is_amsdu)
18831c7b0c84SAdrian Chadd 			printf("%s: XXX ERROR: is_amsdu set; not QoS!\n",
18841c7b0c84SAdrian Chadd 			    __func__);
18858a1b9b6aSSam Leffler 	}
18869a841c7fSSam Leffler 
188743eafd0dSAdrian Chadd 	/*
188843eafd0dSAdrian Chadd 	 * Check if xmit fragmentation is required.
188943eafd0dSAdrian Chadd 	 *
189043eafd0dSAdrian Chadd 	 * If the hardware does fragmentation offload, then don't bother
189143eafd0dSAdrian Chadd 	 * doing it here.
189243eafd0dSAdrian Chadd 	 */
189343eafd0dSAdrian Chadd 	if (IEEE80211_CONF_FRAG_OFFLOAD(ic))
189443eafd0dSAdrian Chadd 		txfrag = 0;
189543eafd0dSAdrian Chadd 	else
1896b032f27cSSam Leffler 		txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
189768e8e04eSSam Leffler 		    !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1898b032f27cSSam Leffler 		    (vap->iv_caps & IEEE80211_C_TXFRAG) &&
1899e9cfc1f5SSam Leffler 		    (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
190043eafd0dSAdrian Chadd 
19015e923d2eSSam Leffler 	if (key != NULL) {
19025e923d2eSSam Leffler 		/*
19035e923d2eSSam Leffler 		 * IEEE 802.1X: send EAPOL frames always in the clear.
19045e923d2eSSam Leffler 		 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
19055e923d2eSSam Leffler 		 */
1906b032f27cSSam Leffler 		if ((m->m_flags & M_EAPOL) == 0 ||
1907b032f27cSSam Leffler 		    ((vap->iv_flags & IEEE80211_F_WPA) &&
1908b032f27cSSam Leffler 		     (vap->iv_opmode == IEEE80211_M_STA ?
1909cda15ce1SSam Leffler 		      !IEEE80211_KEY_UNDEFINED(key) :
1910cda15ce1SSam Leffler 		      !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
19115945b5f5SKevin Lo 			wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
1912b032f27cSSam Leffler 			if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1913b032f27cSSam Leffler 				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1914b032f27cSSam Leffler 				    eh.ether_dhost,
1915b032f27cSSam Leffler 				    "%s", "enmic failed, discard frame");
1916b032f27cSSam Leffler 				vap->iv_stats.is_crypto_enmicfail++;
19175e923d2eSSam Leffler 				goto bad;
19185e923d2eSSam Leffler 			}
19195e923d2eSSam Leffler 		}
19205e923d2eSSam Leffler 	}
1921b032f27cSSam Leffler 	if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1922b032f27cSSam Leffler 	    key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
192368e8e04eSSam Leffler 		goto bad;
19248a1b9b6aSSam Leffler 
1925c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
1926c1af44bdSSam Leffler 
19278a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_data);
19289e80b1dfSSam Leffler 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1929b6e9b119SSam Leffler 		IEEE80211_NODE_STAT(ni, tx_mcast);
19309e80b1dfSSam Leffler 		m->m_flags |= M_MCAST;
19319e80b1dfSSam Leffler 	} else
1932b6e9b119SSam Leffler 		IEEE80211_NODE_STAT(ni, tx_ucast);
19338a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
19348a1b9b6aSSam Leffler 
19351a1e1d21SSam Leffler 	return m;
19360a915fadSSam Leffler bad:
19370a915fadSSam Leffler 	if (m != NULL)
19380a915fadSSam Leffler 		m_freem(m);
19390a915fadSSam Leffler 	return NULL;
1940b032f27cSSam Leffler #undef WH4
19413c314f6dSMonthadar Al Jaberi #undef MC01
19421a1e1d21SSam Leffler }
19431a1e1d21SSam Leffler 
1944d07be335SAdrian Chadd void
ieee80211_free_mbuf(struct mbuf * m)1945d07be335SAdrian Chadd ieee80211_free_mbuf(struct mbuf *m)
1946d07be335SAdrian Chadd {
1947d07be335SAdrian Chadd 	struct mbuf *next;
1948d07be335SAdrian Chadd 
1949d07be335SAdrian Chadd 	if (m == NULL)
1950d07be335SAdrian Chadd 		return;
1951d07be335SAdrian Chadd 
1952d07be335SAdrian Chadd 	do {
1953d07be335SAdrian Chadd 		next = m->m_nextpkt;
1954d07be335SAdrian Chadd 		m->m_nextpkt = NULL;
1955d07be335SAdrian Chadd 		m_freem(m);
1956d07be335SAdrian Chadd 	} while ((m = next) != NULL);
1957d07be335SAdrian Chadd }
1958d07be335SAdrian Chadd 
19591a1e1d21SSam Leffler /*
196068e8e04eSSam Leffler  * Fragment the frame according to the specified mtu.
196168e8e04eSSam Leffler  * The size of the 802.11 header (w/o padding) is provided
196268e8e04eSSam Leffler  * so we don't need to recalculate it.  We create a new
196368e8e04eSSam Leffler  * mbuf for each fragment and chain it through m_nextpkt;
196468e8e04eSSam Leffler  * we might be able to optimize this by reusing the original
196568e8e04eSSam Leffler  * packet's mbufs but that is significantly more complicated.
196668e8e04eSSam Leffler  */
196768e8e04eSSam Leffler static int
ieee80211_fragment(struct ieee80211vap * vap,struct mbuf * m0,u_int hdrsize,u_int ciphdrsize,u_int mtu)1968b032f27cSSam Leffler ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
196968e8e04eSSam Leffler 	u_int hdrsize, u_int ciphdrsize, u_int mtu)
197068e8e04eSSam Leffler {
19718d46c25dSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
197268e8e04eSSam Leffler 	struct ieee80211_frame *wh, *whf;
1973d07be335SAdrian Chadd 	struct mbuf *m, *prev;
197468e8e04eSSam Leffler 	u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
19758d46c25dSAdrian Chadd 	u_int hdrspace;
197668e8e04eSSam Leffler 
197768e8e04eSSam Leffler 	KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
197868e8e04eSSam Leffler 	KASSERT(m0->m_pkthdr.len > mtu,
197968e8e04eSSam Leffler 		("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
198068e8e04eSSam Leffler 
19818d46c25dSAdrian Chadd 	/*
19828d46c25dSAdrian Chadd 	 * Honor driver DATAPAD requirement.
19838d46c25dSAdrian Chadd 	 */
19848d46c25dSAdrian Chadd 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
19858d46c25dSAdrian Chadd 		hdrspace = roundup(hdrsize, sizeof(uint32_t));
19868d46c25dSAdrian Chadd 	else
19878d46c25dSAdrian Chadd 		hdrspace = hdrsize;
19888d46c25dSAdrian Chadd 
198968e8e04eSSam Leffler 	wh = mtod(m0, struct ieee80211_frame *);
199068e8e04eSSam Leffler 	/* NB: mark the first frag; it will be propagated below */
199168e8e04eSSam Leffler 	wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
19928d46c25dSAdrian Chadd 	totalhdrsize = hdrspace + ciphdrsize;
199368e8e04eSSam Leffler 	fragno = 1;
199468e8e04eSSam Leffler 	off = mtu - ciphdrsize;
199568e8e04eSSam Leffler 	remainder = m0->m_pkthdr.len - off;
199668e8e04eSSam Leffler 	prev = m0;
199768e8e04eSSam Leffler 	do {
199818d20be0SAndriy Voskoboinyk 		fragsize = MIN(totalhdrsize + remainder, mtu);
1999bd29f817SBjoern A. Zeeb 		m = m_get2(fragsize, IEEE80211_M_NOWAIT, MT_DATA, M_PKTHDR);
200068e8e04eSSam Leffler 		if (m == NULL)
200168e8e04eSSam Leffler 			goto bad;
200268e8e04eSSam Leffler 		/* leave room to prepend any cipher header */
200368e8e04eSSam Leffler 		m_align(m, fragsize - ciphdrsize);
200468e8e04eSSam Leffler 
200568e8e04eSSam Leffler 		/*
200668e8e04eSSam Leffler 		 * Form the header in the fragment.  Note that since
200768e8e04eSSam Leffler 		 * we mark the first fragment with the MORE_FRAG bit
200868e8e04eSSam Leffler 		 * it automatically is propagated to each fragment; we
200968e8e04eSSam Leffler 		 * need only clear it on the last fragment (done below).
201091216c71SAdrian Chadd 		 * NB: frag 1+ dont have Mesh Control field present.
201168e8e04eSSam Leffler 		 */
201268e8e04eSSam Leffler 		whf = mtod(m, struct ieee80211_frame *);
201368e8e04eSSam Leffler 		memcpy(whf, wh, hdrsize);
201491216c71SAdrian Chadd #ifdef IEEE80211_SUPPORT_MESH
2015f3f08e16SAndriy Voskoboinyk 		if (vap->iv_opmode == IEEE80211_M_MBSS)
2016f3f08e16SAndriy Voskoboinyk 			ieee80211_getqos(wh)[1] &= ~IEEE80211_QOS_MC;
201791216c71SAdrian Chadd #endif
201868e8e04eSSam Leffler 		*(uint16_t *)&whf->i_seq[0] |= htole16(
201968e8e04eSSam Leffler 			(fragno & IEEE80211_SEQ_FRAG_MASK) <<
202068e8e04eSSam Leffler 				IEEE80211_SEQ_FRAG_SHIFT);
202168e8e04eSSam Leffler 		fragno++;
202268e8e04eSSam Leffler 
202368e8e04eSSam Leffler 		payload = fragsize - totalhdrsize;
202468e8e04eSSam Leffler 		/* NB: destination is known to be contiguous */
20258d46c25dSAdrian Chadd 
20268d46c25dSAdrian Chadd 		m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrspace);
20278d46c25dSAdrian Chadd 		m->m_len = hdrspace + payload;
20288d46c25dSAdrian Chadd 		m->m_pkthdr.len = hdrspace + payload;
202968e8e04eSSam Leffler 		m->m_flags |= M_FRAG;
203068e8e04eSSam Leffler 
203168e8e04eSSam Leffler 		/* chain up the fragment */
203268e8e04eSSam Leffler 		prev->m_nextpkt = m;
203368e8e04eSSam Leffler 		prev = m;
203468e8e04eSSam Leffler 
203568e8e04eSSam Leffler 		/* deduct fragment just formed */
203668e8e04eSSam Leffler 		remainder -= payload;
203768e8e04eSSam Leffler 		off += payload;
203868e8e04eSSam Leffler 	} while (remainder != 0);
203951cec121SWeongyo Jeong 
204051cec121SWeongyo Jeong 	/* set the last fragment */
204151cec121SWeongyo Jeong 	m->m_flags |= M_LASTFRAG;
204268e8e04eSSam Leffler 	whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
204368e8e04eSSam Leffler 
204468e8e04eSSam Leffler 	/* strip first mbuf now that everything has been copied */
204568e8e04eSSam Leffler 	m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
204668e8e04eSSam Leffler 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
204768e8e04eSSam Leffler 
2048b032f27cSSam Leffler 	vap->iv_stats.is_tx_fragframes++;
2049b032f27cSSam Leffler 	vap->iv_stats.is_tx_frags += fragno-1;
205068e8e04eSSam Leffler 
205168e8e04eSSam Leffler 	return 1;
205268e8e04eSSam Leffler bad:
205368e8e04eSSam Leffler 	/* reclaim fragments but leave original frame for caller to free */
2054d07be335SAdrian Chadd 	ieee80211_free_mbuf(m0->m_nextpkt);
205568e8e04eSSam Leffler 	m0->m_nextpkt = NULL;
205668e8e04eSSam Leffler 	return 0;
205768e8e04eSSam Leffler }
205868e8e04eSSam Leffler 
205968e8e04eSSam Leffler /*
20601a1e1d21SSam Leffler  * Add a supported rates element id to a frame.
20611a1e1d21SSam Leffler  */
206259aa14a9SRui Paulo uint8_t *
ieee80211_add_rates(uint8_t * frm,const struct ieee80211_rateset * rs)206368e8e04eSSam Leffler ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
20641a1e1d21SSam Leffler {
20651a1e1d21SSam Leffler 	int nrates;
20661a1e1d21SSam Leffler 
20671a1e1d21SSam Leffler 	*frm++ = IEEE80211_ELEMID_RATES;
20681a1e1d21SSam Leffler 	nrates = rs->rs_nrates;
20691a1e1d21SSam Leffler 	if (nrates > IEEE80211_RATE_SIZE)
20701a1e1d21SSam Leffler 		nrates = IEEE80211_RATE_SIZE;
20711a1e1d21SSam Leffler 	*frm++ = nrates;
20721a1e1d21SSam Leffler 	memcpy(frm, rs->rs_rates, nrates);
20731a1e1d21SSam Leffler 	return frm + nrates;
20741a1e1d21SSam Leffler }
20751a1e1d21SSam Leffler 
20761a1e1d21SSam Leffler /*
20771a1e1d21SSam Leffler  * Add an extended supported rates element id to a frame.
20781a1e1d21SSam Leffler  */
207959aa14a9SRui Paulo uint8_t *
ieee80211_add_xrates(uint8_t * frm,const struct ieee80211_rateset * rs)208068e8e04eSSam Leffler ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
20811a1e1d21SSam Leffler {
20821a1e1d21SSam Leffler 	/*
20831a1e1d21SSam Leffler 	 * Add an extended supported rates element if operating in 11g mode.
20841a1e1d21SSam Leffler 	 */
20851a1e1d21SSam Leffler 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
20861a1e1d21SSam Leffler 		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
20871a1e1d21SSam Leffler 		*frm++ = IEEE80211_ELEMID_XRATES;
20881a1e1d21SSam Leffler 		*frm++ = nrates;
20891a1e1d21SSam Leffler 		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
20901a1e1d21SSam Leffler 		frm += nrates;
20911a1e1d21SSam Leffler 	}
20921a1e1d21SSam Leffler 	return frm;
20931a1e1d21SSam Leffler }
20941a1e1d21SSam Leffler 
20951a1e1d21SSam Leffler /*
2096b032f27cSSam Leffler  * Add an ssid element to a frame.
20971a1e1d21SSam Leffler  */
2098de981aecSAdrian Chadd uint8_t *
ieee80211_add_ssid(uint8_t * frm,const uint8_t * ssid,u_int len)209968e8e04eSSam Leffler ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
21001a1e1d21SSam Leffler {
21011a1e1d21SSam Leffler 	*frm++ = IEEE80211_ELEMID_SSID;
21021a1e1d21SSam Leffler 	*frm++ = len;
21031a1e1d21SSam Leffler 	memcpy(frm, ssid, len);
21041a1e1d21SSam Leffler 	return frm + len;
21051a1e1d21SSam Leffler }
21061a1e1d21SSam Leffler 
21078a1b9b6aSSam Leffler /*
21088a1b9b6aSSam Leffler  * Add an erp element to a frame.
21098a1b9b6aSSam Leffler  */
211068e8e04eSSam Leffler static uint8_t *
ieee80211_add_erp(uint8_t * frm,struct ieee80211vap * vap)2111f1481c8dSAdrian Chadd ieee80211_add_erp(uint8_t *frm, struct ieee80211vap *vap)
21121a1e1d21SSam Leffler {
2113f1481c8dSAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
211468e8e04eSSam Leffler 	uint8_t erp;
21151a1e1d21SSam Leffler 
21168a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_ERP;
21178a1b9b6aSSam Leffler 	*frm++ = 1;
21188a1b9b6aSSam Leffler 	erp = 0;
2119f1481c8dSAdrian Chadd 
2120f1481c8dSAdrian Chadd 	/*
2121f1481c8dSAdrian Chadd 	 * TODO:  This uses the global flags for now because
2122f1481c8dSAdrian Chadd 	 * the per-VAP flags are fine for per-VAP, but don't
2123f1481c8dSAdrian Chadd 	 * take into account which VAPs share the same channel
2124f1481c8dSAdrian Chadd 	 * and which are on different channels.
2125f1481c8dSAdrian Chadd 	 *
2126f1481c8dSAdrian Chadd 	 * ERP and HT/VHT protection mode is a function of
2127f1481c8dSAdrian Chadd 	 * how many stations are on a channel, not specifically
2128f1481c8dSAdrian Chadd 	 * the VAP or global.  But, until we grow that status,
2129f1481c8dSAdrian Chadd 	 * the global flag will have to do.
2130f1481c8dSAdrian Chadd 	 */
2131f1481c8dSAdrian Chadd 	if (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR)
21328a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_NON_ERP_PRESENT;
2133f1481c8dSAdrian Chadd 
2134f1481c8dSAdrian Chadd 	/*
2135f1481c8dSAdrian Chadd 	 * TODO: same as above; these should be based not
2136f1481c8dSAdrian Chadd 	 * on the vap or ic flags, but instead on a combination
2137f1481c8dSAdrian Chadd 	 * of per-VAP and channels.
2138f1481c8dSAdrian Chadd 	 */
21398a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_USEPROT)
21408a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_USE_PROTECTION;
21418a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_USEBARKER)
21428a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_LONG_PREAMBLE;
21438a1b9b6aSSam Leffler 	*frm++ = erp;
21448a1b9b6aSSam Leffler 	return frm;
21451a1e1d21SSam Leffler }
21461a1e1d21SSam Leffler 
21478a1b9b6aSSam Leffler /*
2148b032f27cSSam Leffler  * Add a CFParams element to a frame.
21498a1b9b6aSSam Leffler  */
215068e8e04eSSam Leffler static uint8_t *
ieee80211_add_cfparms(uint8_t * frm,struct ieee80211com * ic)2151b032f27cSSam Leffler ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
21528a1b9b6aSSam Leffler {
2153b032f27cSSam Leffler #define	ADDSHORT(frm, v) do {	\
215431021a2bSAndriy Voskoboinyk 	le16enc(frm, v);	\
2155b032f27cSSam Leffler 	frm += 2;		\
2156b032f27cSSam Leffler } while (0)
2157b032f27cSSam Leffler 	*frm++ = IEEE80211_ELEMID_CFPARMS;
2158b032f27cSSam Leffler 	*frm++ = 6;
2159b032f27cSSam Leffler 	*frm++ = 0;		/* CFP count */
2160b032f27cSSam Leffler 	*frm++ = 2;		/* CFP period */
2161b032f27cSSam Leffler 	ADDSHORT(frm, 0);	/* CFP MaxDuration (TU) */
2162b032f27cSSam Leffler 	ADDSHORT(frm, 0);	/* CFP CurRemaining (TU) */
21638a1b9b6aSSam Leffler 	return frm;
2164b032f27cSSam Leffler #undef ADDSHORT
2165b032f27cSSam Leffler }
2166b032f27cSSam Leffler 
2167b032f27cSSam Leffler static __inline uint8_t *
add_appie(uint8_t * frm,const struct ieee80211_appie * ie)2168b032f27cSSam Leffler add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
2169b032f27cSSam Leffler {
2170b032f27cSSam Leffler 	memcpy(frm, ie->ie_data, ie->ie_len);
2171b032f27cSSam Leffler 	return frm + ie->ie_len;
2172b032f27cSSam Leffler }
2173b032f27cSSam Leffler 
2174b032f27cSSam Leffler static __inline uint8_t *
add_ie(uint8_t * frm,const uint8_t * ie)2175b032f27cSSam Leffler add_ie(uint8_t *frm, const uint8_t *ie)
2176b032f27cSSam Leffler {
2177b032f27cSSam Leffler 	memcpy(frm, ie, 2 + ie[1]);
2178b032f27cSSam Leffler 	return frm + 2 + ie[1];
21798a1b9b6aSSam Leffler }
21808a1b9b6aSSam Leffler 
21818a1b9b6aSSam Leffler #define	WME_OUI_BYTES		0x00, 0x50, 0xf2
21828a1b9b6aSSam Leffler /*
21838a1b9b6aSSam Leffler  * Add a WME information element to a frame.
21848a1b9b6aSSam Leffler  */
21854eadefc2SAdrian Chadd uint8_t *
ieee80211_add_wme_info(uint8_t * frm,struct ieee80211_wme_state * wme,struct ieee80211_node * ni)21868379e8dbSAdrian Chadd ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme,
21878379e8dbSAdrian Chadd     struct ieee80211_node *ni)
21888a1b9b6aSSam Leffler {
21898379e8dbSAdrian Chadd 	static const uint8_t oui[4] = { WME_OUI_BYTES, WME_OUI_TYPE };
21908379e8dbSAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
21918379e8dbSAdrian Chadd 
21928379e8dbSAdrian Chadd 	*frm++ = IEEE80211_ELEMID_VENDOR;
21938379e8dbSAdrian Chadd 	*frm++ = sizeof(struct ieee80211_wme_info) - 2;
21948379e8dbSAdrian Chadd 	memcpy(frm, oui, sizeof(oui));
21958379e8dbSAdrian Chadd 	frm += sizeof(oui);
21968379e8dbSAdrian Chadd 	*frm++ = WME_INFO_OUI_SUBTYPE;
21978379e8dbSAdrian Chadd 	*frm++ = WME_VERSION;
21988379e8dbSAdrian Chadd 
21998379e8dbSAdrian Chadd 	/* QoS info field depends upon operating mode */
22008379e8dbSAdrian Chadd 	switch (vap->iv_opmode) {
22018379e8dbSAdrian Chadd 	case IEEE80211_M_HOSTAP:
22028379e8dbSAdrian Chadd 		*frm = wme->wme_bssChanParams.cap_info;
22038379e8dbSAdrian Chadd 		if (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD)
22048379e8dbSAdrian Chadd 			*frm |= WME_CAPINFO_UAPSD_EN;
22058379e8dbSAdrian Chadd 		frm++;
22068379e8dbSAdrian Chadd 		break;
22078379e8dbSAdrian Chadd 	case IEEE80211_M_STA:
22088379e8dbSAdrian Chadd 		/*
22098379e8dbSAdrian Chadd 		 * NB: UAPSD drivers must set this up in their
22108379e8dbSAdrian Chadd 		 * VAP creation method.
22118379e8dbSAdrian Chadd 		 */
22128379e8dbSAdrian Chadd 		*frm++ = vap->iv_uapsdinfo;
22138379e8dbSAdrian Chadd 		break;
22148379e8dbSAdrian Chadd 	default:
22158379e8dbSAdrian Chadd 		*frm++ = 0;
22168379e8dbSAdrian Chadd 		break;
22178379e8dbSAdrian Chadd 	}
22188379e8dbSAdrian Chadd 
22198379e8dbSAdrian Chadd 	return frm;
22208a1b9b6aSSam Leffler }
22218a1b9b6aSSam Leffler 
22228a1b9b6aSSam Leffler /*
22238a1b9b6aSSam Leffler  * Add a WME parameters element to a frame.
22248a1b9b6aSSam Leffler  */
222568e8e04eSSam Leffler static uint8_t *
ieee80211_add_wme_param(uint8_t * frm,struct ieee80211_wme_state * wme,int uapsd_enable)22268379e8dbSAdrian Chadd ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme,
22278379e8dbSAdrian Chadd     int uapsd_enable)
22288a1b9b6aSSam Leffler {
22298a1b9b6aSSam Leffler #define	ADDSHORT(frm, v) do {	\
223031021a2bSAndriy Voskoboinyk 	le16enc(frm, v);	\
22318a1b9b6aSSam Leffler 	frm += 2;		\
22328a1b9b6aSSam Leffler } while (0)
22338a1b9b6aSSam Leffler 	/* NB: this works 'cuz a param has an info at the front */
22348a1b9b6aSSam Leffler 	static const struct ieee80211_wme_info param = {
22358a1b9b6aSSam Leffler 		.wme_id		= IEEE80211_ELEMID_VENDOR,
22368a1b9b6aSSam Leffler 		.wme_len	= sizeof(struct ieee80211_wme_param) - 2,
22378a1b9b6aSSam Leffler 		.wme_oui	= { WME_OUI_BYTES },
22388a1b9b6aSSam Leffler 		.wme_type	= WME_OUI_TYPE,
22398a1b9b6aSSam Leffler 		.wme_subtype	= WME_PARAM_OUI_SUBTYPE,
22408a1b9b6aSSam Leffler 		.wme_version	= WME_VERSION,
22418a1b9b6aSSam Leffler 	};
22428a1b9b6aSSam Leffler 	int i;
22438a1b9b6aSSam Leffler 
22448a1b9b6aSSam Leffler 	memcpy(frm, &param, sizeof(param));
22458a1b9b6aSSam Leffler 	frm += __offsetof(struct ieee80211_wme_info, wme_info);
22468379e8dbSAdrian Chadd 	*frm = wme->wme_bssChanParams.cap_info;	/* AC info */
22478379e8dbSAdrian Chadd 	if (uapsd_enable)
22488379e8dbSAdrian Chadd 		*frm |= WME_CAPINFO_UAPSD_EN;
22498379e8dbSAdrian Chadd 	frm++;
22508a1b9b6aSSam Leffler 	*frm++ = 0;					/* reserved field */
22518379e8dbSAdrian Chadd 	/* XXX TODO - U-APSD bits - SP, flags below */
22528a1b9b6aSSam Leffler 	for (i = 0; i < WME_NUM_AC; i++) {
22538a1b9b6aSSam Leffler 		const struct wmeParams *ac =
22548a1b9b6aSSam Leffler 		       &wme->wme_bssChanParams.cap_wmeParams[i];
2255fe5ebb23SBjoern A. Zeeb 		*frm++ = _IEEE80211_SHIFTMASK(i, WME_PARAM_ACI)
2256fe5ebb23SBjoern A. Zeeb 		       | _IEEE80211_SHIFTMASK(ac->wmep_acm, WME_PARAM_ACM)
2257fe5ebb23SBjoern A. Zeeb 		       | _IEEE80211_SHIFTMASK(ac->wmep_aifsn, WME_PARAM_AIFSN)
22588a1b9b6aSSam Leffler 		       ;
2259fe5ebb23SBjoern A. Zeeb 		*frm++ = _IEEE80211_SHIFTMASK(ac->wmep_logcwmax,
2260fe5ebb23SBjoern A. Zeeb 			    WME_PARAM_LOGCWMAX)
2261fe5ebb23SBjoern A. Zeeb 		       | _IEEE80211_SHIFTMASK(ac->wmep_logcwmin,
2262fe5ebb23SBjoern A. Zeeb 			    WME_PARAM_LOGCWMIN)
22638a1b9b6aSSam Leffler 		       ;
22648a1b9b6aSSam Leffler 		ADDSHORT(frm, ac->wmep_txopLimit);
22658a1b9b6aSSam Leffler 	}
22668a1b9b6aSSam Leffler 	return frm;
22678a1b9b6aSSam Leffler #undef ADDSHORT
22688a1b9b6aSSam Leffler }
22698a1b9b6aSSam Leffler #undef WME_OUI_BYTES
22708a1b9b6aSSam Leffler 
22710a915fadSSam Leffler /*
2272b032f27cSSam Leffler  * Add an 11h Power Constraint element to a frame.
2273b032f27cSSam Leffler  */
2274b032f27cSSam Leffler static uint8_t *
ieee80211_add_powerconstraint(uint8_t * frm,struct ieee80211vap * vap)2275b032f27cSSam Leffler ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
2276b032f27cSSam Leffler {
2277b032f27cSSam Leffler 	const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
2278b032f27cSSam Leffler 	/* XXX per-vap tx power limit? */
2279b032f27cSSam Leffler 	int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
2280b032f27cSSam Leffler 
2281b032f27cSSam Leffler 	frm[0] = IEEE80211_ELEMID_PWRCNSTR;
2282b032f27cSSam Leffler 	frm[1] = 1;
2283b032f27cSSam Leffler 	frm[2] = c->ic_maxregpower > limit ?  c->ic_maxregpower - limit : 0;
2284b032f27cSSam Leffler 	return frm + 3;
2285b032f27cSSam Leffler }
2286b032f27cSSam Leffler 
2287b032f27cSSam Leffler /*
2288b032f27cSSam Leffler  * Add an 11h Power Capability element to a frame.
2289b032f27cSSam Leffler  */
2290b032f27cSSam Leffler static uint8_t *
ieee80211_add_powercapability(uint8_t * frm,const struct ieee80211_channel * c)2291b032f27cSSam Leffler ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
2292b032f27cSSam Leffler {
2293b032f27cSSam Leffler 	frm[0] = IEEE80211_ELEMID_PWRCAP;
2294b032f27cSSam Leffler 	frm[1] = 2;
2295b032f27cSSam Leffler 	frm[2] = c->ic_minpower;
2296b032f27cSSam Leffler 	frm[3] = c->ic_maxpower;
2297b032f27cSSam Leffler 	return frm + 4;
2298b032f27cSSam Leffler }
2299b032f27cSSam Leffler 
2300b032f27cSSam Leffler /*
2301b032f27cSSam Leffler  * Add an 11h Supported Channels element to a frame.
2302b032f27cSSam Leffler  */
2303b032f27cSSam Leffler static uint8_t *
ieee80211_add_supportedchannels(uint8_t * frm,struct ieee80211com * ic)2304b032f27cSSam Leffler ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
2305b032f27cSSam Leffler {
2306b032f27cSSam Leffler 	static const int ielen = 26;
2307b032f27cSSam Leffler 
2308b032f27cSSam Leffler 	frm[0] = IEEE80211_ELEMID_SUPPCHAN;
2309b032f27cSSam Leffler 	frm[1] = ielen;
2310b032f27cSSam Leffler 	/* XXX not correct */
2311b032f27cSSam Leffler 	memcpy(frm+2, ic->ic_chan_avail, ielen);
2312b032f27cSSam Leffler 	return frm + 2 + ielen;
2313b032f27cSSam Leffler }
2314b032f27cSSam Leffler 
2315b032f27cSSam Leffler /*
231632b0e64bSAdrian Chadd  * Add an 11h Quiet time element to a frame.
231732b0e64bSAdrian Chadd  */
231832b0e64bSAdrian Chadd static uint8_t *
ieee80211_add_quiet(uint8_t * frm,struct ieee80211vap * vap,int update)2319ce4552cdSAdrian Chadd ieee80211_add_quiet(uint8_t *frm, struct ieee80211vap *vap, int update)
232032b0e64bSAdrian Chadd {
232132b0e64bSAdrian Chadd 	struct ieee80211_quiet_ie *quiet = (struct ieee80211_quiet_ie *) frm;
232232b0e64bSAdrian Chadd 
232332b0e64bSAdrian Chadd 	quiet->quiet_ie = IEEE80211_ELEMID_QUIET;
232432b0e64bSAdrian Chadd 	quiet->len = 6;
2325ce4552cdSAdrian Chadd 
2326ce4552cdSAdrian Chadd 	/*
2327ce4552cdSAdrian Chadd 	 * Only update every beacon interval - otherwise probe responses
2328ce4552cdSAdrian Chadd 	 * would update the quiet count value.
2329ce4552cdSAdrian Chadd 	 */
2330ce4552cdSAdrian Chadd 	if (update) {
233132b0e64bSAdrian Chadd 		if (vap->iv_quiet_count_value == 1)
233232b0e64bSAdrian Chadd 			vap->iv_quiet_count_value = vap->iv_quiet_count;
233332b0e64bSAdrian Chadd 		else if (vap->iv_quiet_count_value > 1)
233432b0e64bSAdrian Chadd 			vap->iv_quiet_count_value--;
2335ce4552cdSAdrian Chadd 	}
233632b0e64bSAdrian Chadd 
233732b0e64bSAdrian Chadd 	if (vap->iv_quiet_count_value == 0) {
233832b0e64bSAdrian Chadd 		/* value 0 is reserved as per 802.11h standerd */
233932b0e64bSAdrian Chadd 		vap->iv_quiet_count_value = 1;
234032b0e64bSAdrian Chadd 	}
234132b0e64bSAdrian Chadd 
234232b0e64bSAdrian Chadd 	quiet->tbttcount = vap->iv_quiet_count_value;
234332b0e64bSAdrian Chadd 	quiet->period = vap->iv_quiet_period;
234432b0e64bSAdrian Chadd 	quiet->duration = htole16(vap->iv_quiet_duration);
234532b0e64bSAdrian Chadd 	quiet->offset = htole16(vap->iv_quiet_offset);
234632b0e64bSAdrian Chadd 	return frm + sizeof(*quiet);
234732b0e64bSAdrian Chadd }
234832b0e64bSAdrian Chadd 
234932b0e64bSAdrian Chadd /*
2350b032f27cSSam Leffler  * Add an 11h Channel Switch Announcement element to a frame.
2351b032f27cSSam Leffler  * Note that we use the per-vap CSA count to adjust the global
2352b032f27cSSam Leffler  * counter so we can use this routine to form probe response
2353b032f27cSSam Leffler  * frames and get the current count.
2354b032f27cSSam Leffler  */
2355b032f27cSSam Leffler static uint8_t *
ieee80211_add_csa(uint8_t * frm,struct ieee80211vap * vap)2356b032f27cSSam Leffler ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
2357b032f27cSSam Leffler {
2358b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2359b032f27cSSam Leffler 	struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
2360b032f27cSSam Leffler 
2361c70761e6SSam Leffler 	csa->csa_ie = IEEE80211_ELEMID_CSA;
2362b032f27cSSam Leffler 	csa->csa_len = 3;
2363b032f27cSSam Leffler 	csa->csa_mode = 1;		/* XXX force quiet on channel */
2364b032f27cSSam Leffler 	csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
2365b032f27cSSam Leffler 	csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
2366b032f27cSSam Leffler 	return frm + sizeof(*csa);
2367b032f27cSSam Leffler }
2368b032f27cSSam Leffler 
2369b032f27cSSam Leffler /*
2370b032f27cSSam Leffler  * Add an 11h country information element to a frame.
2371b032f27cSSam Leffler  */
2372b032f27cSSam Leffler static uint8_t *
ieee80211_add_countryie(uint8_t * frm,struct ieee80211com * ic)2373b032f27cSSam Leffler ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
2374b032f27cSSam Leffler {
2375b032f27cSSam Leffler 
2376b032f27cSSam Leffler 	if (ic->ic_countryie == NULL ||
2377b032f27cSSam Leffler 	    ic->ic_countryie_chan != ic->ic_bsschan) {
2378b032f27cSSam Leffler 		/*
2379b032f27cSSam Leffler 		 * Handle lazy construction of ie.  This is done on
2380b032f27cSSam Leffler 		 * first use and after a channel change that requires
2381b032f27cSSam Leffler 		 * re-calculation.
2382b032f27cSSam Leffler 		 */
2383b032f27cSSam Leffler 		if (ic->ic_countryie != NULL)
2384b9b53389SAdrian Chadd 			IEEE80211_FREE(ic->ic_countryie, M_80211_NODE_IE);
2385b032f27cSSam Leffler 		ic->ic_countryie = ieee80211_alloc_countryie(ic);
2386b032f27cSSam Leffler 		if (ic->ic_countryie == NULL)
2387b032f27cSSam Leffler 			return frm;
2388b032f27cSSam Leffler 		ic->ic_countryie_chan = ic->ic_bsschan;
2389b032f27cSSam Leffler 	}
2390b032f27cSSam Leffler 	return add_appie(frm, ic->ic_countryie);
2391b032f27cSSam Leffler }
2392b032f27cSSam Leffler 
2393d8df5f3dSRui Paulo uint8_t *
ieee80211_add_wpa(uint8_t * frm,const struct ieee80211vap * vap)2394d8df5f3dSRui Paulo ieee80211_add_wpa(uint8_t *frm, const struct ieee80211vap *vap)
2395d8df5f3dSRui Paulo {
2396d8df5f3dSRui Paulo 	if (vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL)
2397d8df5f3dSRui Paulo 		return (add_ie(frm, vap->iv_wpa_ie));
2398d8df5f3dSRui Paulo 	else {
2399d8df5f3dSRui Paulo 		/* XXX else complain? */
2400d8df5f3dSRui Paulo 		return (frm);
2401d8df5f3dSRui Paulo 	}
2402d8df5f3dSRui Paulo }
2403d8df5f3dSRui Paulo 
2404d8df5f3dSRui Paulo uint8_t *
ieee80211_add_rsn(uint8_t * frm,const struct ieee80211vap * vap)2405d8df5f3dSRui Paulo ieee80211_add_rsn(uint8_t *frm, const struct ieee80211vap *vap)
2406d8df5f3dSRui Paulo {
2407d8df5f3dSRui Paulo 	if (vap->iv_flags & IEEE80211_F_WPA2 && vap->iv_rsn_ie != NULL)
2408d8df5f3dSRui Paulo 		return (add_ie(frm, vap->iv_rsn_ie));
2409d8df5f3dSRui Paulo 	else {
2410d8df5f3dSRui Paulo 		/* XXX else complain? */
2411d8df5f3dSRui Paulo 		return (frm);
2412d8df5f3dSRui Paulo 	}
2413d8df5f3dSRui Paulo }
2414d8df5f3dSRui Paulo 
2415d8df5f3dSRui Paulo uint8_t *
ieee80211_add_qos(uint8_t * frm,const struct ieee80211_node * ni)2416d8df5f3dSRui Paulo ieee80211_add_qos(uint8_t *frm, const struct ieee80211_node *ni)
2417d8df5f3dSRui Paulo {
2418d8df5f3dSRui Paulo 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
2419d8df5f3dSRui Paulo 		*frm++ = IEEE80211_ELEMID_QOS;
2420d8df5f3dSRui Paulo 		*frm++ = 1;
2421d8df5f3dSRui Paulo 		*frm++ = 0;
2422d8df5f3dSRui Paulo 	}
2423d8df5f3dSRui Paulo 
2424d8df5f3dSRui Paulo 	return (frm);
2425d8df5f3dSRui Paulo }
2426d8df5f3dSRui Paulo 
2427b032f27cSSam Leffler /*
2428c338cf2cSBjoern A. Zeeb  * ieee80211_send_probereq(): send a probe request frame with the specified ssid
2429c338cf2cSBjoern A. Zeeb  * and any optional information element data;  some helper functions as FW based
2430c338cf2cSBjoern A. Zeeb  * HW scans need some of that information passed too.
2431af8418dcSSam Leffler  */
2432c338cf2cSBjoern A. Zeeb static uint32_t
ieee80211_probereq_ie_len(struct ieee80211vap * vap,struct ieee80211com * ic)2433c338cf2cSBjoern A. Zeeb ieee80211_probereq_ie_len(struct ieee80211vap *vap, struct ieee80211com *ic)
2434af8418dcSSam Leffler {
243541b3c790SSam Leffler 	const struct ieee80211_rateset *rs;
2436af8418dcSSam Leffler 
2437c338cf2cSBjoern A. Zeeb 	rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2438af8418dcSSam Leffler 
2439af8418dcSSam Leffler 	/*
2440af8418dcSSam Leffler 	 * prreq frame format
2441af8418dcSSam Leffler 	 *	[tlv] ssid
2442af8418dcSSam Leffler 	 *	[tlv] supported rates
2443c338cf2cSBjoern A. Zeeb 	 *	[tlv] extended supported rates (if needed)
2444f8a67728SAdrian Chadd 	 *	[tlv] HT cap (optional)
244551172f62SAdrian Chadd 	 *	[tlv] VHT cap (optional)
2446b032f27cSSam Leffler 	 *	[tlv] WPA (optional)
2447af8418dcSSam Leffler 	 *	[tlv] user-specified ie's
2448af8418dcSSam Leffler 	 */
2449c338cf2cSBjoern A. Zeeb 	return ( 2 + IEEE80211_NWID_LEN
2450af8418dcSSam Leffler 	       + 2 + IEEE80211_RATE_SIZE
2451c338cf2cSBjoern A. Zeeb 	       + ((rs->rs_nrates > IEEE80211_RATE_SIZE) ?
2452c338cf2cSBjoern A. Zeeb 	           2 + (rs->rs_nrates - IEEE80211_RATE_SIZE) : 0)
2453c338cf2cSBjoern A. Zeeb 	       + (((vap->iv_opmode == IEEE80211_M_IBSS) &&
2454c338cf2cSBjoern A. Zeeb 		    (vap->iv_flags_ht & IEEE80211_FHT_HT)) ?
2455c338cf2cSBjoern A. Zeeb 	                sizeof(struct ieee80211_ie_htcap) : 0)
2456c338cf2cSBjoern A. Zeeb #ifdef notyet
245751172f62SAdrian Chadd 	       + sizeof(struct ieee80211_ie_htinfo)	/* XXX not needed? */
2458e85eb4c8SBjoern A. Zeeb 	       + 2 + sizeof(struct ieee80211_vht_cap)
2459c338cf2cSBjoern A. Zeeb #endif
2460c338cf2cSBjoern A. Zeeb 	       + ((vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL) ?
2461c338cf2cSBjoern A. Zeeb 	           vap->iv_wpa_ie[1] : 0)
2462b032f27cSSam Leffler 	       + (vap->iv_appie_probereq != NULL ?
2463b032f27cSSam Leffler 		   vap->iv_appie_probereq->ie_len : 0)
2464af8418dcSSam Leffler 	);
2465af8418dcSSam Leffler }
2466af8418dcSSam Leffler 
2467c338cf2cSBjoern A. Zeeb int
ieee80211_probereq_ie(struct ieee80211vap * vap,struct ieee80211com * ic,uint8_t ** frmp,uint32_t * frmlen,const uint8_t * ssid,size_t ssidlen,bool alloc)2468c338cf2cSBjoern A. Zeeb ieee80211_probereq_ie(struct ieee80211vap *vap, struct ieee80211com *ic,
2469c338cf2cSBjoern A. Zeeb     uint8_t **frmp, uint32_t *frmlen, const uint8_t *ssid, size_t ssidlen,
2470c338cf2cSBjoern A. Zeeb     bool alloc)
2471c338cf2cSBjoern A. Zeeb {
2472c338cf2cSBjoern A. Zeeb 	const struct ieee80211_rateset *rs;
2473c338cf2cSBjoern A. Zeeb 	uint8_t	*frm;
2474c338cf2cSBjoern A. Zeeb 	uint32_t len;
2475c338cf2cSBjoern A. Zeeb 
2476c338cf2cSBjoern A. Zeeb 	if (!alloc && (frmp == NULL || frmlen == NULL))
2477c338cf2cSBjoern A. Zeeb 		return (EINVAL);
2478c338cf2cSBjoern A. Zeeb 
2479c338cf2cSBjoern A. Zeeb 	len = ieee80211_probereq_ie_len(vap, ic);
2480c338cf2cSBjoern A. Zeeb 	if (!alloc && len > *frmlen)
2481c338cf2cSBjoern A. Zeeb 		return (ENOBUFS);
2482c338cf2cSBjoern A. Zeeb 
2483243b9597SBjoern A. Zeeb 	/* For HW scans we usually do not pass in the SSID as IE. */
2484243b9597SBjoern A. Zeeb 	if (ssidlen == -1)
2485243b9597SBjoern A. Zeeb 		len -= (2 + IEEE80211_NWID_LEN);
2486243b9597SBjoern A. Zeeb 
2487c338cf2cSBjoern A. Zeeb 	if (alloc) {
2488bd29f817SBjoern A. Zeeb 		frm = IEEE80211_MALLOC(len, M_80211_VAP,
2489bd29f817SBjoern A. Zeeb 		    IEEE80211_M_WAITOK | IEEE80211_M_ZERO);
2490c338cf2cSBjoern A. Zeeb 		*frmp = frm;
2491c338cf2cSBjoern A. Zeeb 		*frmlen = len;
2492c338cf2cSBjoern A. Zeeb 	} else
2493c338cf2cSBjoern A. Zeeb 		frm = *frmp;
2494c338cf2cSBjoern A. Zeeb 
2495243b9597SBjoern A. Zeeb 	if (ssidlen != -1)
2496af8418dcSSam Leffler 		frm = ieee80211_add_ssid(frm, ssid, ssidlen);
249741b3c790SSam Leffler 	rs = ieee80211_get_suprates(ic, ic->ic_curchan);
249841b3c790SSam Leffler 	frm = ieee80211_add_rates(frm, rs);
2499b032f27cSSam Leffler 	frm = ieee80211_add_xrates(frm, rs);
2500f8a67728SAdrian Chadd 
2501f8a67728SAdrian Chadd 	/*
2502f8a67728SAdrian Chadd 	 * Note: we can't use bss; we don't have one yet.
2503f8a67728SAdrian Chadd 	 *
2504f8a67728SAdrian Chadd 	 * So, we should announce our capabilities
2505f8a67728SAdrian Chadd 	 * in this channel mode (2g/5g), not the
2506f8a67728SAdrian Chadd 	 * channel details itself.
2507f8a67728SAdrian Chadd 	 */
2508f8a67728SAdrian Chadd 	if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
2509f8a67728SAdrian Chadd 	    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
2510f8a67728SAdrian Chadd 		struct ieee80211_channel *c;
2511f8a67728SAdrian Chadd 
2512f8a67728SAdrian Chadd 		/*
2513f8a67728SAdrian Chadd 		 * Get the HT channel that we should try upgrading to.
2514f8a67728SAdrian Chadd 		 * If we can do 40MHz then this'll upgrade it appropriately.
2515f8a67728SAdrian Chadd 		 */
2516f8a67728SAdrian Chadd 		c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
2517f8a67728SAdrian Chadd 		    vap->iv_flags_ht);
2518f8a67728SAdrian Chadd 		frm = ieee80211_add_htcap_ch(frm, vap, c);
2519f8a67728SAdrian Chadd 	}
2520f8a67728SAdrian Chadd 
252151172f62SAdrian Chadd 	/*
252251172f62SAdrian Chadd 	 * XXX TODO: need to figure out what/how to update the
252351172f62SAdrian Chadd 	 * VHT channel.
252451172f62SAdrian Chadd 	 */
2525c338cf2cSBjoern A. Zeeb #ifdef notyet
2526ef48d4faSBjoern A. Zeeb 	if (vap->iv_vht_flags & IEEE80211_FVHT_VHT) {
252751172f62SAdrian Chadd 		struct ieee80211_channel *c;
252851172f62SAdrian Chadd 
252951172f62SAdrian Chadd 		c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
253051172f62SAdrian Chadd 		    vap->iv_flags_ht);
2531ef48d4faSBjoern A. Zeeb 		c = ieee80211_vht_adjust_channel(ic, c, vap->iv_vht_flags);
253251172f62SAdrian Chadd 		frm = ieee80211_add_vhtcap_ch(frm, vap, c);
253351172f62SAdrian Chadd 	}
253451172f62SAdrian Chadd #endif
253551172f62SAdrian Chadd 
2536d8df5f3dSRui Paulo 	frm = ieee80211_add_wpa(frm, vap);
2537b032f27cSSam Leffler 	if (vap->iv_appie_probereq != NULL)
2538b032f27cSSam Leffler 		frm = add_appie(frm, vap->iv_appie_probereq);
2539af8418dcSSam Leffler 
2540c338cf2cSBjoern A. Zeeb 	if (!alloc) {
2541c338cf2cSBjoern A. Zeeb 		*frmp = frm;
2542c338cf2cSBjoern A. Zeeb 		*frmlen = len;
2543c338cf2cSBjoern A. Zeeb 	}
2544c338cf2cSBjoern A. Zeeb 
2545c338cf2cSBjoern A. Zeeb 	return (0);
2546c338cf2cSBjoern A. Zeeb }
2547c338cf2cSBjoern A. Zeeb 
2548c338cf2cSBjoern A. Zeeb int
ieee80211_send_probereq(struct ieee80211_node * ni,const uint8_t sa[IEEE80211_ADDR_LEN],const uint8_t da[IEEE80211_ADDR_LEN],const uint8_t bssid[IEEE80211_ADDR_LEN],const uint8_t * ssid,size_t ssidlen)2549c338cf2cSBjoern A. Zeeb ieee80211_send_probereq(struct ieee80211_node *ni,
2550c338cf2cSBjoern A. Zeeb 	const uint8_t sa[IEEE80211_ADDR_LEN],
2551c338cf2cSBjoern A. Zeeb 	const uint8_t da[IEEE80211_ADDR_LEN],
2552c338cf2cSBjoern A. Zeeb 	const uint8_t bssid[IEEE80211_ADDR_LEN],
2553c338cf2cSBjoern A. Zeeb 	const uint8_t *ssid, size_t ssidlen)
2554c338cf2cSBjoern A. Zeeb {
2555c338cf2cSBjoern A. Zeeb 	struct ieee80211vap *vap = ni->ni_vap;
2556c338cf2cSBjoern A. Zeeb 	struct ieee80211com *ic = ni->ni_ic;
2557c338cf2cSBjoern A. Zeeb 	struct ieee80211_node *bss;
2558c338cf2cSBjoern A. Zeeb 	const struct ieee80211_txparam *tp;
2559c338cf2cSBjoern A. Zeeb 	struct ieee80211_bpf_params params;
2560c338cf2cSBjoern A. Zeeb 	struct mbuf *m;
2561c338cf2cSBjoern A. Zeeb 	uint8_t *frm;
2562c338cf2cSBjoern A. Zeeb 	uint32_t frmlen;
2563c338cf2cSBjoern A. Zeeb 	int ret;
2564c338cf2cSBjoern A. Zeeb 
2565c338cf2cSBjoern A. Zeeb 	bss = ieee80211_ref_node(vap->iv_bss);
2566c338cf2cSBjoern A. Zeeb 
2567c338cf2cSBjoern A. Zeeb 	if (vap->iv_state == IEEE80211_S_CAC) {
2568c338cf2cSBjoern A. Zeeb 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
2569c338cf2cSBjoern A. Zeeb 		    "block %s frame in CAC state", "probe request");
2570c338cf2cSBjoern A. Zeeb 		vap->iv_stats.is_tx_badstate++;
2571c338cf2cSBjoern A. Zeeb 		ieee80211_free_node(bss);
2572c338cf2cSBjoern A. Zeeb 		return EIO;		/* XXX */
2573c338cf2cSBjoern A. Zeeb 	}
2574c338cf2cSBjoern A. Zeeb 
2575c338cf2cSBjoern A. Zeeb 	/*
2576c338cf2cSBjoern A. Zeeb 	 * Hold a reference on the node so it doesn't go away until after
2577c338cf2cSBjoern A. Zeeb 	 * the xmit is complete all the way in the driver.  On error we
2578c338cf2cSBjoern A. Zeeb 	 * will remove our reference.
2579c338cf2cSBjoern A. Zeeb 	 */
2580c338cf2cSBjoern A. Zeeb 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2581c338cf2cSBjoern A. Zeeb 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2582c338cf2cSBjoern A. Zeeb 		__func__, __LINE__,
2583c338cf2cSBjoern A. Zeeb 		ni, ether_sprintf(ni->ni_macaddr),
2584c338cf2cSBjoern A. Zeeb 		ieee80211_node_refcnt(ni)+1);
2585c338cf2cSBjoern A. Zeeb 	ieee80211_ref_node(ni);
2586c338cf2cSBjoern A. Zeeb 
2587c338cf2cSBjoern A. Zeeb 	/* See comments above for entire frame format. */
2588c338cf2cSBjoern A. Zeeb 	frmlen = ieee80211_probereq_ie_len(vap, ic);
2589c338cf2cSBjoern A. Zeeb 	m = ieee80211_getmgtframe(&frm,
2590c338cf2cSBjoern A. Zeeb 	    ic->ic_headroom + sizeof(struct ieee80211_frame), frmlen);
2591c338cf2cSBjoern A. Zeeb 	if (m == NULL) {
2592c338cf2cSBjoern A. Zeeb 		vap->iv_stats.is_tx_nobuf++;
2593c338cf2cSBjoern A. Zeeb 		ieee80211_free_node(ni);
2594c338cf2cSBjoern A. Zeeb 		ieee80211_free_node(bss);
2595c338cf2cSBjoern A. Zeeb 		return ENOMEM;
2596c338cf2cSBjoern A. Zeeb 	}
2597c338cf2cSBjoern A. Zeeb 
2598c338cf2cSBjoern A. Zeeb 	ret = ieee80211_probereq_ie(vap, ic, &frm, &frmlen, ssid, ssidlen,
2599c338cf2cSBjoern A. Zeeb 	    false);
2600c338cf2cSBjoern A. Zeeb 	KASSERT(ret == 0,
260149c220b0SBjoern A. Zeeb 	    ("%s: ieee80211_probereq_ie failed: %d\n", __func__, ret));
2602c338cf2cSBjoern A. Zeeb 
2603c338cf2cSBjoern A. Zeeb 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
260416d7cbb1SSam Leffler 	KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
260516d7cbb1SSam Leffler 	    ("leading space %zd", M_LEADINGSPACE(m)));
2606bd29f817SBjoern A. Zeeb 	M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT);
260716d7cbb1SSam Leffler 	if (m == NULL) {
260816d7cbb1SSam Leffler 		/* NB: cannot happen */
260916d7cbb1SSam Leffler 		ieee80211_free_node(ni);
2610f8a67728SAdrian Chadd 		ieee80211_free_node(bss);
2611af8418dcSSam Leffler 		return ENOMEM;
261216d7cbb1SSam Leffler 	}
2613af8418dcSSam Leffler 
26145cda6006SAdrian Chadd 	IEEE80211_TX_LOCK(ic);
26159e80b1dfSSam Leffler 	ieee80211_send_setup(ni, m,
2616af8418dcSSam Leffler 	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
26178ac160cdSSam Leffler 	     IEEE80211_NONQOS_TID, sa, da, bssid);
2618af8418dcSSam Leffler 	/* XXX power management? */
2619c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
2620af8418dcSSam Leffler 
2621b032f27cSSam Leffler 	M_WME_SETAC(m, WME_AC_BE);
2622b032f27cSSam Leffler 
2623af8418dcSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_probereq);
2624af8418dcSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_mgmt);
2625af8418dcSSam Leffler 
2626b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2627f8a67728SAdrian Chadd 	    "send probe req on channel %u bssid %s sa %6D da %6D ssid \"%.*s\"\n",
2628f8a67728SAdrian Chadd 	    ieee80211_chan2ieee(ic, ic->ic_curchan),
2629f8a67728SAdrian Chadd 	    ether_sprintf(bssid),
2630f8a67728SAdrian Chadd 	    sa, ":",
2631f8a67728SAdrian Chadd 	    da, ":",
2632b032f27cSSam Leffler 	    ssidlen, ssid);
2633af8418dcSSam Leffler 
263416d7cbb1SSam Leffler 	memset(&params, 0, sizeof(params));
263516d7cbb1SSam Leffler 	params.ibp_pri = M_WME_GETAC(m);
263616d7cbb1SSam Leffler 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
263716d7cbb1SSam Leffler 	params.ibp_rate0 = tp->mgmtrate;
263816d7cbb1SSam Leffler 	if (IEEE80211_IS_MULTICAST(da)) {
263916d7cbb1SSam Leffler 		params.ibp_flags |= IEEE80211_BPF_NOACK;
264016d7cbb1SSam Leffler 		params.ibp_try0 = 1;
264116d7cbb1SSam Leffler 	} else
264216d7cbb1SSam Leffler 		params.ibp_try0 = tp->maxretry;
264316d7cbb1SSam Leffler 	params.ibp_power = ni->ni_txpower;
26445cda6006SAdrian Chadd 	ret = ieee80211_raw_output(vap, ni, m, &params);
26455cda6006SAdrian Chadd 	IEEE80211_TX_UNLOCK(ic);
2646f8a67728SAdrian Chadd 	ieee80211_free_node(bss);
26475cda6006SAdrian Chadd 	return (ret);
2648af8418dcSSam Leffler }
2649af8418dcSSam Leffler 
2650af8418dcSSam Leffler /*
2651667dad55SSam Leffler  * Calculate capability information for mgt frames.
2652667dad55SSam Leffler  */
265359aa14a9SRui Paulo uint16_t
ieee80211_getcapinfo(struct ieee80211vap * vap,struct ieee80211_channel * chan)265459aa14a9SRui Paulo ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
2655667dad55SSam Leffler {
265668e8e04eSSam Leffler 	uint16_t capinfo;
2657667dad55SSam Leffler 
2658b032f27cSSam Leffler 	KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
2659667dad55SSam Leffler 
2660b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2661667dad55SSam Leffler 		capinfo = IEEE80211_CAPINFO_ESS;
2662b032f27cSSam Leffler 	else if (vap->iv_opmode == IEEE80211_M_IBSS)
2663667dad55SSam Leffler 		capinfo = IEEE80211_CAPINFO_IBSS;
2664667dad55SSam Leffler 	else
2665667dad55SSam Leffler 		capinfo = 0;
2666b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY)
2667667dad55SSam Leffler 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
2668f1481c8dSAdrian Chadd 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) &&
2669667dad55SSam Leffler 	    IEEE80211_IS_CHAN_2GHZ(chan))
2670667dad55SSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2671d20ff6e6SAdrian Chadd 	if (vap->iv_flags & IEEE80211_F_SHSLOT)
2672667dad55SSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2673b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
2674b032f27cSSam Leffler 		capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2675667dad55SSam Leffler 	return capinfo;
2676667dad55SSam Leffler }
2677667dad55SSam Leffler 
2678667dad55SSam Leffler /*
26790a915fadSSam Leffler  * Send a management frame.  The node is for the destination (or ic_bss
26800a915fadSSam Leffler  * when in station mode).  Nodes other than ic_bss have their reference
26810a915fadSSam Leffler  * count bumped to reflect our use for an indeterminant time.
26820a915fadSSam Leffler  */
26831a1e1d21SSam Leffler int
ieee80211_send_mgmt(struct ieee80211_node * ni,int type,int arg)2684b032f27cSSam Leffler ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
26851a1e1d21SSam Leffler {
26861b6167d2SSam Leffler #define	HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
2687b032f27cSSam Leffler #define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2688b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2689b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2690b032f27cSSam Leffler 	struct ieee80211_node *bss = vap->iv_bss;
26918ac160cdSSam Leffler 	struct ieee80211_bpf_params params;
26921a1e1d21SSam Leffler 	struct mbuf *m;
269368e8e04eSSam Leffler 	uint8_t *frm;
269468e8e04eSSam Leffler 	uint16_t capinfo;
269568e8e04eSSam Leffler 	int has_challenge, is_shared_key, ret, status;
26961a1e1d21SSam Leffler 
26970a915fadSSam Leffler 	KASSERT(ni != NULL, ("null node"));
26980a915fadSSam Leffler 
26990a915fadSSam Leffler 	/*
27000a915fadSSam Leffler 	 * Hold a reference on the node so it doesn't go away until after
27010a915fadSSam Leffler 	 * the xmit is complete all the way in the driver.  On error we
27020a915fadSSam Leffler 	 * will remove our reference.
27030a915fadSSam Leffler 	 */
2704b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
270549a15236SSam Leffler 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
27068a1b9b6aSSam Leffler 		__func__, __LINE__,
270749a15236SSam Leffler 		ni, ether_sprintf(ni->ni_macaddr),
27088a1b9b6aSSam Leffler 		ieee80211_node_refcnt(ni)+1);
27090a915fadSSam Leffler 	ieee80211_ref_node(ni);
27108a1b9b6aSSam Leffler 
27118ac160cdSSam Leffler 	memset(&params, 0, sizeof(params));
27121a1e1d21SSam Leffler 	switch (type) {
27131a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_AUTH:
27148a1b9b6aSSam Leffler 		status = arg >> 16;
27158a1b9b6aSSam Leffler 		arg &= 0xffff;
27168a1b9b6aSSam Leffler 		has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
27178a1b9b6aSSam Leffler 		    arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
27188a1b9b6aSSam Leffler 		    ni->ni_challenge != NULL);
27198a1b9b6aSSam Leffler 
27208a1b9b6aSSam Leffler 		/*
27218a1b9b6aSSam Leffler 		 * Deduce whether we're doing open authentication or
27228a1b9b6aSSam Leffler 		 * shared key authentication.  We do the latter if
27238a1b9b6aSSam Leffler 		 * we're in the middle of a shared key authentication
27248a1b9b6aSSam Leffler 		 * handshake or if we're initiating an authentication
27258a1b9b6aSSam Leffler 		 * request and configured to use shared key.
27268a1b9b6aSSam Leffler 		 */
27278a1b9b6aSSam Leffler 		is_shared_key = has_challenge ||
27288a1b9b6aSSam Leffler 		     arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
27298a1b9b6aSSam Leffler 		     (arg == IEEE80211_AUTH_SHARED_REQUEST &&
2730b032f27cSSam Leffler 		      bss->ni_authmode == IEEE80211_AUTH_SHARED);
27318a1b9b6aSSam Leffler 
27328a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
273368e8e04eSSam Leffler 			  ic->ic_headroom + sizeof(struct ieee80211_frame),
273468e8e04eSSam Leffler 			  3 * sizeof(uint16_t)
27358a1b9b6aSSam Leffler 			+ (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
273649c220b0SBjoern A. Zeeb 				sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0));
27371a1e1d21SSam Leffler 		if (m == NULL)
27388a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
27398a1b9b6aSSam Leffler 
274068e8e04eSSam Leffler 		((uint16_t *)frm)[0] =
27418a1b9b6aSSam Leffler 		    (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
27428a1b9b6aSSam Leffler 		                    : htole16(IEEE80211_AUTH_ALG_OPEN);
274368e8e04eSSam Leffler 		((uint16_t *)frm)[1] = htole16(arg);	/* sequence number */
274468e8e04eSSam Leffler 		((uint16_t *)frm)[2] = htole16(status);/* status */
27458a1b9b6aSSam Leffler 
27468a1b9b6aSSam Leffler 		if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
274768e8e04eSSam Leffler 			((uint16_t *)frm)[3] =
27488a1b9b6aSSam Leffler 			    htole16((IEEE80211_CHALLENGE_LEN << 8) |
27498a1b9b6aSSam Leffler 			    IEEE80211_ELEMID_CHALLENGE);
275068e8e04eSSam Leffler 			memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
27518a1b9b6aSSam Leffler 			    IEEE80211_CHALLENGE_LEN);
27528a1b9b6aSSam Leffler 			m->m_pkthdr.len = m->m_len =
275368e8e04eSSam Leffler 				4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
27548a1b9b6aSSam Leffler 			if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
2755b032f27cSSam Leffler 				IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2756b032f27cSSam Leffler 				    "request encrypt frame (%s)", __func__);
27578ac160cdSSam Leffler 				/* mark frame for encryption */
27588ac160cdSSam Leffler 				params.ibp_flags |= IEEE80211_BPF_CRYPTO;
27598a1b9b6aSSam Leffler 			}
27608a1b9b6aSSam Leffler 		} else
276168e8e04eSSam Leffler 			m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
27628a1b9b6aSSam Leffler 
27638a1b9b6aSSam Leffler 		/* XXX not right for shared key */
27648a1b9b6aSSam Leffler 		if (status == IEEE80211_STATUS_SUCCESS)
27658a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_auth);
27668a1b9b6aSSam Leffler 		else
27678a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_auth_fail);
27688a1b9b6aSSam Leffler 
2769b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA)
277068e8e04eSSam Leffler 			ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2771b032f27cSSam Leffler 				(void *) vap->iv_state);
27721a1e1d21SSam Leffler 		break;
27731a1e1d21SSam Leffler 
27741a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
2775b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2776d72d72d3SAndriy Voskoboinyk 		    "send station deauthenticate (reason: %d (%s))", arg,
2777d72d72d3SAndriy Voskoboinyk 		    ieee80211_reason_to_string(arg));
277868e8e04eSSam Leffler 		m = ieee80211_getmgtframe(&frm,
277968e8e04eSSam Leffler 			ic->ic_headroom + sizeof(struct ieee80211_frame),
278068e8e04eSSam Leffler 			sizeof(uint16_t));
27811a1e1d21SSam Leffler 		if (m == NULL)
27828a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
278368e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(arg);	/* reason */
278468e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
27858a1b9b6aSSam Leffler 
27868a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, tx_deauth);
27878a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
27888a1b9b6aSSam Leffler 
2789e4918ecdSSam Leffler 		ieee80211_node_unauthorize(ni);		/* port closed */
27901a1e1d21SSam Leffler 		break;
27911a1e1d21SSam Leffler 
27921a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
27931a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
27941a1e1d21SSam Leffler 		/*
27951a1e1d21SSam Leffler 		 * asreq frame format
27961a1e1d21SSam Leffler 		 *	[2] capability information
27971a1e1d21SSam Leffler 		 *	[2] listen interval
27981a1e1d21SSam Leffler 		 *	[6*] current AP address (reassoc only)
27991a1e1d21SSam Leffler 		 *	[tlv] ssid
28001a1e1d21SSam Leffler 		 *	[tlv] supported rates
28011a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
2802b032f27cSSam Leffler 		 *	[4] power capability (optional)
2803b032f27cSSam Leffler 		 *	[28] supported channels (optional)
280468e8e04eSSam Leffler 		 *	[tlv] HT capabilities
280551172f62SAdrian Chadd 		 *	[tlv] VHT capabilities
2806b032f27cSSam Leffler 		 *	[tlv] WME (optional)
280768e8e04eSSam Leffler 		 *	[tlv] Vendor OUI HT capabilities (optional)
280868e8e04eSSam Leffler 		 *	[tlv] Atheros capabilities (if negotiated)
2809b032f27cSSam Leffler 		 *	[tlv] AppIE's (optional)
28101a1e1d21SSam Leffler 		 */
28118a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
281268e8e04eSSam Leffler 			 ic->ic_headroom + sizeof(struct ieee80211_frame),
281368e8e04eSSam Leffler 			 sizeof(uint16_t)
281468e8e04eSSam Leffler 		       + sizeof(uint16_t)
28151a1e1d21SSam Leffler 		       + IEEE80211_ADDR_LEN
28168a1b9b6aSSam Leffler 		       + 2 + IEEE80211_NWID_LEN
28171a1e1d21SSam Leffler 		       + 2 + IEEE80211_RATE_SIZE
28188a1b9b6aSSam Leffler 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2819b032f27cSSam Leffler 		       + 4
2820b032f27cSSam Leffler 		       + 2 + 26
28218a1b9b6aSSam Leffler 		       + sizeof(struct ieee80211_wme_info)
2822b032f27cSSam Leffler 		       + sizeof(struct ieee80211_ie_htcap)
2823e85eb4c8SBjoern A. Zeeb 		       + 2 + sizeof(struct ieee80211_vht_cap)
2824b032f27cSSam Leffler 		       + 4 + sizeof(struct ieee80211_ie_htcap)
2825616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
282668e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ath_ie)
2827616190d0SSam Leffler #endif
2828b032f27cSSam Leffler 		       + (vap->iv_appie_wpa != NULL ?
2829b032f27cSSam Leffler 				vap->iv_appie_wpa->ie_len : 0)
2830b032f27cSSam Leffler 		       + (vap->iv_appie_assocreq != NULL ?
2831b032f27cSSam Leffler 				vap->iv_appie_assocreq->ie_len : 0)
28328a1b9b6aSSam Leffler 		);
28331a1e1d21SSam Leffler 		if (m == NULL)
28348a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
28351a1e1d21SSam Leffler 
2836b032f27cSSam Leffler 		KASSERT(vap->iv_opmode == IEEE80211_M_STA,
2837b032f27cSSam Leffler 		    ("wrong mode %u", vap->iv_opmode));
2838667dad55SSam Leffler 		capinfo = IEEE80211_CAPINFO_ESS;
2839b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_PRIVACY)
28401a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
28414f2e09c4SSam Leffler 		/*
28424f2e09c4SSam Leffler 		 * NB: Some 11a AP's reject the request when
2843191ccdf5SAndriy Voskoboinyk 		 *     short preamble is set.
28444f2e09c4SSam Leffler 		 */
2845f1481c8dSAdrian Chadd 		if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) &&
2846b5c99415SSam Leffler 		    IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
28471a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
284868e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
28498a1b9b6aSSam Leffler 		    (ic->ic_caps & IEEE80211_C_SHSLOT))
28501a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
28511b6167d2SSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
2852b032f27cSSam Leffler 		    (vap->iv_flags & IEEE80211_F_DOTH))
28531b6167d2SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
285468e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(capinfo);
28551a1e1d21SSam Leffler 		frm += 2;
28561a1e1d21SSam Leffler 
2857b032f27cSSam Leffler 		KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
285868e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
2859b032f27cSSam Leffler 						    bss->ni_intval));
28601a1e1d21SSam Leffler 		frm += 2;
28611a1e1d21SSam Leffler 
28621a1e1d21SSam Leffler 		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2863b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
28641a1e1d21SSam Leffler 			frm += IEEE80211_ADDR_LEN;
28651a1e1d21SSam Leffler 		}
28661a1e1d21SSam Leffler 
28671a1e1d21SSam Leffler 		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
28681a1e1d21SSam Leffler 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
2869d8df5f3dSRui Paulo 		frm = ieee80211_add_rsn(frm, vap);
2870b032f27cSSam Leffler 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2871b032f27cSSam Leffler 		if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
2872b032f27cSSam Leffler 			frm = ieee80211_add_powercapability(frm,
2873b032f27cSSam Leffler 			    ic->ic_curchan);
2874b032f27cSSam Leffler 			frm = ieee80211_add_supportedchannels(frm, ic);
2875b032f27cSSam Leffler 		}
2876b83391bfSAdrian Chadd 
2877b83391bfSAdrian Chadd 		/*
2878b83391bfSAdrian Chadd 		 * Check the channel - we may be using an 11n NIC with an
2879b83391bfSAdrian Chadd 		 * 11n capable station, but we're configured to be an 11b
2880b83391bfSAdrian Chadd 		 * channel.
2881b83391bfSAdrian Chadd 		 */
28822bfc8a91SSam Leffler 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2883b83391bfSAdrian Chadd 		    IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2884b032f27cSSam Leffler 		    ni->ni_ies.htcap_ie != NULL &&
2885b83391bfSAdrian Chadd 		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP) {
2886b032f27cSSam Leffler 			frm = ieee80211_add_htcap(frm, ni);
2887b83391bfSAdrian Chadd 		}
288851172f62SAdrian Chadd 
2889ef48d4faSBjoern A. Zeeb 		if ((vap->iv_vht_flags & IEEE80211_FVHT_VHT) &&
289051172f62SAdrian Chadd 		    IEEE80211_IS_CHAN_VHT(ni->ni_chan) &&
289151172f62SAdrian Chadd 		    ni->ni_ies.vhtcap_ie != NULL &&
289251172f62SAdrian Chadd 		    ni->ni_ies.vhtcap_ie[0] == IEEE80211_ELEMID_VHT_CAP) {
289351172f62SAdrian Chadd 			frm = ieee80211_add_vhtcap(frm, ni);
289451172f62SAdrian Chadd 		}
289551172f62SAdrian Chadd 
2896d8df5f3dSRui Paulo 		frm = ieee80211_add_wpa(frm, vap);
2897f82316f6SBjoern A. Zeeb 		if ((vap->iv_flags & IEEE80211_F_WME) &&
2898b032f27cSSam Leffler 		    ni->ni_ies.wme_ie != NULL)
28998379e8dbSAdrian Chadd 			frm = ieee80211_add_wme_info(frm, &ic->ic_wme, ni);
2900b83391bfSAdrian Chadd 
2901b83391bfSAdrian Chadd 		/*
2902b83391bfSAdrian Chadd 		 * Same deal - only send HT info if we're on an 11n
2903b83391bfSAdrian Chadd 		 * capable channel.
2904b83391bfSAdrian Chadd 		 */
29052bfc8a91SSam Leffler 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2906b83391bfSAdrian Chadd 		    IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2907b032f27cSSam Leffler 		    ni->ni_ies.htcap_ie != NULL &&
2908b83391bfSAdrian Chadd 		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR) {
2909b032f27cSSam Leffler 			frm = ieee80211_add_htcap_vendor(frm, ni);
2910b83391bfSAdrian Chadd 		}
2911616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
29124207227cSSam Leffler 		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
2913b032f27cSSam Leffler 			frm = ieee80211_add_ath(frm,
2914b032f27cSSam Leffler 				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
29154207227cSSam Leffler 				((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
29164207227cSSam Leffler 				 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
29174207227cSSam Leffler 				vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
29184207227cSSam Leffler 		}
2919616190d0SSam Leffler #endif /* IEEE80211_SUPPORT_SUPERG */
2920b032f27cSSam Leffler 		if (vap->iv_appie_assocreq != NULL)
2921b032f27cSSam Leffler 			frm = add_appie(frm, vap->iv_appie_assocreq);
292268e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
29231a1e1d21SSam Leffler 
292468e8e04eSSam Leffler 		ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2925b032f27cSSam Leffler 			(void *) vap->iv_state);
29261a1e1d21SSam Leffler 		break;
29271a1e1d21SSam Leffler 
29281a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
29291a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
29301a1e1d21SSam Leffler 		/*
293168e8e04eSSam Leffler 		 * asresp frame format
29321a1e1d21SSam Leffler 		 *	[2] capability information
29331a1e1d21SSam Leffler 		 *	[2] status
29341a1e1d21SSam Leffler 		 *	[2] association ID
29351a1e1d21SSam Leffler 		 *	[tlv] supported rates
29361a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
2937b032f27cSSam Leffler 		 *	[tlv] HT capabilities (standard, if STA enabled)
2938b032f27cSSam Leffler 		 *	[tlv] HT information (standard, if STA enabled)
293951172f62SAdrian Chadd 		 *	[tlv] VHT capabilities (standard, if STA enabled)
294051172f62SAdrian Chadd 		 *	[tlv] VHT information (standard, if STA enabled)
2941b032f27cSSam Leffler 		 *	[tlv] WME (if configured and STA enabled)
2942b032f27cSSam Leffler 		 *	[tlv] HT capabilities (vendor OUI, if STA enabled)
2943b032f27cSSam Leffler 		 *	[tlv] HT information (vendor OUI, if STA enabled)
2944b032f27cSSam Leffler 		 *	[tlv] Atheros capabilities (if STA enabled)
2945b032f27cSSam Leffler 		 *	[tlv] AppIE's (optional)
29461a1e1d21SSam Leffler 		 */
29478a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
294868e8e04eSSam Leffler 			 ic->ic_headroom + sizeof(struct ieee80211_frame),
294968e8e04eSSam Leffler 			 sizeof(uint16_t)
295068e8e04eSSam Leffler 		       + sizeof(uint16_t)
295168e8e04eSSam Leffler 		       + sizeof(uint16_t)
29521a1e1d21SSam Leffler 		       + 2 + IEEE80211_RATE_SIZE
29538a1b9b6aSSam Leffler 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
295468e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ie_htcap) + 4
295568e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ie_htinfo) + 4
2956e85eb4c8SBjoern A. Zeeb 		       + 2 + sizeof(struct ieee80211_vht_cap)
2957e85eb4c8SBjoern A. Zeeb 		       + 2 + sizeof(struct ieee80211_vht_operation)
2958b032f27cSSam Leffler 		       + sizeof(struct ieee80211_wme_param)
2959616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
296068e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ath_ie)
2961616190d0SSam Leffler #endif
2962b032f27cSSam Leffler 		       + (vap->iv_appie_assocresp != NULL ?
2963b032f27cSSam Leffler 				vap->iv_appie_assocresp->ie_len : 0)
29648a1b9b6aSSam Leffler 		);
29651a1e1d21SSam Leffler 		if (m == NULL)
29668a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
29671a1e1d21SSam Leffler 
296859aa14a9SRui Paulo 		capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
296968e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(capinfo);
29701a1e1d21SSam Leffler 		frm += 2;
29711a1e1d21SSam Leffler 
297268e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(arg);	/* status */
29731a1e1d21SSam Leffler 		frm += 2;
29741a1e1d21SSam Leffler 
29758a1b9b6aSSam Leffler 		if (arg == IEEE80211_STATUS_SUCCESS) {
297668e8e04eSSam Leffler 			*(uint16_t *)frm = htole16(ni->ni_associd);
29778a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_assoc);
29788a1b9b6aSSam Leffler 		} else
29798a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_assoc_fail);
29801a1e1d21SSam Leffler 		frm += 2;
29811a1e1d21SSam Leffler 
29821a1e1d21SSam Leffler 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
29831a1e1d21SSam Leffler 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
298468e8e04eSSam Leffler 		/* NB: respond according to what we received */
29851b6167d2SSam Leffler 		if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
298668e8e04eSSam Leffler 			frm = ieee80211_add_htcap(frm, ni);
298768e8e04eSSam Leffler 			frm = ieee80211_add_htinfo(frm, ni);
298868e8e04eSSam Leffler 		}
2989b032f27cSSam Leffler 		if ((vap->iv_flags & IEEE80211_F_WME) &&
2990b032f27cSSam Leffler 		    ni->ni_ies.wme_ie != NULL)
29918379e8dbSAdrian Chadd 			frm = ieee80211_add_wme_param(frm, &ic->ic_wme,
29928379e8dbSAdrian Chadd 			    !! (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD));
29931b6167d2SSam Leffler 		if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
29941b6167d2SSam Leffler 			frm = ieee80211_add_htcap_vendor(frm, ni);
29951b6167d2SSam Leffler 			frm = ieee80211_add_htinfo_vendor(frm, ni);
299668e8e04eSSam Leffler 		}
299751172f62SAdrian Chadd 		if (ni->ni_flags & IEEE80211_NODE_VHT) {
299851172f62SAdrian Chadd 			frm = ieee80211_add_vhtcap(frm, ni);
299951172f62SAdrian Chadd 			frm = ieee80211_add_vhtinfo(frm, ni);
300051172f62SAdrian Chadd 		}
3001616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
3002b032f27cSSam Leffler 		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
300368e8e04eSSam Leffler 			frm = ieee80211_add_ath(frm,
3004b032f27cSSam Leffler 				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
30054207227cSSam Leffler 				((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
30064207227cSSam Leffler 				 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
30074207227cSSam Leffler 				vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
3008616190d0SSam Leffler #endif /* IEEE80211_SUPPORT_SUPERG */
3009b032f27cSSam Leffler 		if (vap->iv_appie_assocresp != NULL)
3010b032f27cSSam Leffler 			frm = add_appie(frm, vap->iv_appie_assocresp);
301168e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
30121a1e1d21SSam Leffler 		break;
30131a1e1d21SSam Leffler 
30141a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
3015b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
3016d72d72d3SAndriy Voskoboinyk 		    "send station disassociate (reason: %d (%s))", arg,
3017d72d72d3SAndriy Voskoboinyk 		    ieee80211_reason_to_string(arg));
301868e8e04eSSam Leffler 		m = ieee80211_getmgtframe(&frm,
301968e8e04eSSam Leffler 			ic->ic_headroom + sizeof(struct ieee80211_frame),
302068e8e04eSSam Leffler 			sizeof(uint16_t));
30211a1e1d21SSam Leffler 		if (m == NULL)
30228a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
302368e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(arg);	/* reason */
302468e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
30258a1b9b6aSSam Leffler 
30268a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, tx_disassoc);
30278a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
30281a1e1d21SSam Leffler 		break;
30291a1e1d21SSam Leffler 
30301a1e1d21SSam Leffler 	default:
3031b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
3032b032f27cSSam Leffler 		    "invalid mgmt frame type %u", type);
30331be50176SSam Leffler 		senderr(EINVAL, is_tx_unknownmgt);
30340a915fadSSam Leffler 		/* NOTREACHED */
30351a1e1d21SSam Leffler 	}
303668e8e04eSSam Leffler 
30378ac160cdSSam Leffler 	/* NB: force non-ProbeResp frames to the highest queue */
30388ac160cdSSam Leffler 	params.ibp_pri = WME_AC_VO;
30398ac160cdSSam Leffler 	params.ibp_rate0 = bss->ni_txparms->mgmtrate;
30408ac160cdSSam Leffler 	/* NB: we know all frames are unicast */
30418ac160cdSSam Leffler 	params.ibp_try0 = bss->ni_txparms->maxretry;
30428ac160cdSSam Leffler 	params.ibp_power = bss->ni_txpower;
30438ac160cdSSam Leffler 	return ieee80211_mgmt_output(ni, m, type, &params);
30440a915fadSSam Leffler bad:
30458a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
30461a1e1d21SSam Leffler 	return ret;
30470a915fadSSam Leffler #undef senderr
30481b6167d2SSam Leffler #undef HTFLAGS
30491a1e1d21SSam Leffler }
30508a1b9b6aSSam Leffler 
3051b032f27cSSam Leffler /*
3052b032f27cSSam Leffler  * Return an mbuf with a probe response frame in it.
3053b032f27cSSam Leffler  * Space is left to prepend and 802.11 header at the
3054b032f27cSSam Leffler  * front but it's left to the caller to fill in.
3055b032f27cSSam Leffler  */
3056b032f27cSSam Leffler struct mbuf *
ieee80211_alloc_proberesp(struct ieee80211_node * bss,int legacy)3057b032f27cSSam Leffler ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
3058b032f27cSSam Leffler {
3059b032f27cSSam Leffler 	struct ieee80211vap *vap = bss->ni_vap;
3060b032f27cSSam Leffler 	struct ieee80211com *ic = bss->ni_ic;
3061b032f27cSSam Leffler 	const struct ieee80211_rateset *rs;
3062b032f27cSSam Leffler 	struct mbuf *m;
3063b032f27cSSam Leffler 	uint16_t capinfo;
3064b032f27cSSam Leffler 	uint8_t *frm;
3065b032f27cSSam Leffler 
3066b032f27cSSam Leffler 	/*
3067b032f27cSSam Leffler 	 * probe response frame format
3068b032f27cSSam Leffler 	 *	[8] time stamp
3069b032f27cSSam Leffler 	 *	[2] beacon interval
3070b032f27cSSam Leffler 	 *	[2] cabability information
3071b032f27cSSam Leffler 	 *	[tlv] ssid
3072b032f27cSSam Leffler 	 *	[tlv] supported rates
3073b032f27cSSam Leffler 	 *	[tlv] parameter set (FH/DS)
3074b032f27cSSam Leffler 	 *	[tlv] parameter set (IBSS)
3075b032f27cSSam Leffler 	 *	[tlv] country (optional)
3076b032f27cSSam Leffler 	 *	[3] power control (optional)
3077b032f27cSSam Leffler 	 *	[5] channel switch announcement (CSA) (optional)
3078b032f27cSSam Leffler 	 *	[tlv] extended rate phy (ERP)
3079b032f27cSSam Leffler 	 *	[tlv] extended supported rates
3080688fe74dSSam Leffler 	 *	[tlv] RSN (optional)
3081b032f27cSSam Leffler 	 *	[tlv] HT capabilities
3082b032f27cSSam Leffler 	 *	[tlv] HT information
3083ac0b7d32SAdrian Chadd 	 *	[tlv] VHT capabilities
3084ac0b7d32SAdrian Chadd 	 *	[tlv] VHT information
3085b032f27cSSam Leffler 	 *	[tlv] WPA (optional)
3086b032f27cSSam Leffler 	 *	[tlv] WME (optional)
3087b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT capabilities (optional)
3088b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT information (optional)
3089b032f27cSSam Leffler 	 *	[tlv] Atheros capabilities
3090b032f27cSSam Leffler 	 *	[tlv] AppIE's (optional)
309159aa14a9SRui Paulo 	 *	[tlv] Mesh ID (MBSS)
309259aa14a9SRui Paulo 	 *	[tlv] Mesh Conf (MBSS)
3093b032f27cSSam Leffler 	 */
3094b032f27cSSam Leffler 	m = ieee80211_getmgtframe(&frm,
3095b032f27cSSam Leffler 		 ic->ic_headroom + sizeof(struct ieee80211_frame),
3096b032f27cSSam Leffler 		 8
3097b032f27cSSam Leffler 	       + sizeof(uint16_t)
3098b032f27cSSam Leffler 	       + sizeof(uint16_t)
3099b032f27cSSam Leffler 	       + 2 + IEEE80211_NWID_LEN
3100b032f27cSSam Leffler 	       + 2 + IEEE80211_RATE_SIZE
3101b032f27cSSam Leffler 	       + 7	/* max(7,3) */
3102b032f27cSSam Leffler 	       + IEEE80211_COUNTRY_MAX_SIZE
3103b032f27cSSam Leffler 	       + 3
3104b032f27cSSam Leffler 	       + sizeof(struct ieee80211_csa_ie)
310532b0e64bSAdrian Chadd 	       + sizeof(struct ieee80211_quiet_ie)
3106b032f27cSSam Leffler 	       + 3
3107b032f27cSSam Leffler 	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
3108688fe74dSSam Leffler 	       + sizeof(struct ieee80211_ie_wpa)
3109b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_htcap)
3110b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_htinfo)
3111b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_wpa)
3112b032f27cSSam Leffler 	       + sizeof(struct ieee80211_wme_param)
3113b032f27cSSam Leffler 	       + 4 + sizeof(struct ieee80211_ie_htcap)
3114b032f27cSSam Leffler 	       + 4 + sizeof(struct ieee80211_ie_htinfo)
3115e85eb4c8SBjoern A. Zeeb 	       + 2 + sizeof(struct ieee80211_vht_cap)
3116e85eb4c8SBjoern A. Zeeb 	       + 2 + sizeof(struct ieee80211_vht_operation)
3117616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
3118b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ath_ie)
3119616190d0SSam Leffler #endif
312059aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
312159aa14a9SRui Paulo 	       + 2 + IEEE80211_MESHID_LEN
312259aa14a9SRui Paulo 	       + sizeof(struct ieee80211_meshconf_ie)
312359aa14a9SRui Paulo #endif
3124b032f27cSSam Leffler 	       + (vap->iv_appie_proberesp != NULL ?
3125b032f27cSSam Leffler 			vap->iv_appie_proberesp->ie_len : 0)
3126b032f27cSSam Leffler 	);
3127b032f27cSSam Leffler 	if (m == NULL) {
3128b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
3129b032f27cSSam Leffler 		return NULL;
3130b032f27cSSam Leffler 	}
3131b032f27cSSam Leffler 
3132b032f27cSSam Leffler 	memset(frm, 0, 8);	/* timestamp should be filled later */
3133b032f27cSSam Leffler 	frm += 8;
3134b032f27cSSam Leffler 	*(uint16_t *)frm = htole16(bss->ni_intval);
3135b032f27cSSam Leffler 	frm += 2;
313659aa14a9SRui Paulo 	capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
3137b032f27cSSam Leffler 	*(uint16_t *)frm = htole16(capinfo);
3138b032f27cSSam Leffler 	frm += 2;
3139b032f27cSSam Leffler 
3140b032f27cSSam Leffler 	frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
3141b032f27cSSam Leffler 	rs = ieee80211_get_suprates(ic, bss->ni_chan);
3142b032f27cSSam Leffler 	frm = ieee80211_add_rates(frm, rs);
3143b032f27cSSam Leffler 
3144b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
3145b032f27cSSam Leffler 		*frm++ = IEEE80211_ELEMID_FHPARMS;
3146b032f27cSSam Leffler 		*frm++ = 5;
3147b032f27cSSam Leffler 		*frm++ = bss->ni_fhdwell & 0x00ff;
3148b032f27cSSam Leffler 		*frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
3149b032f27cSSam Leffler 		*frm++ = IEEE80211_FH_CHANSET(
3150b032f27cSSam Leffler 		    ieee80211_chan2ieee(ic, bss->ni_chan));
3151b032f27cSSam Leffler 		*frm++ = IEEE80211_FH_CHANPAT(
3152b032f27cSSam Leffler 		    ieee80211_chan2ieee(ic, bss->ni_chan));
3153b032f27cSSam Leffler 		*frm++ = bss->ni_fhindex;
3154b032f27cSSam Leffler 	} else {
3155b032f27cSSam Leffler 		*frm++ = IEEE80211_ELEMID_DSPARMS;
3156b032f27cSSam Leffler 		*frm++ = 1;
3157b032f27cSSam Leffler 		*frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
3158b032f27cSSam Leffler 	}
3159b032f27cSSam Leffler 
3160b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
3161b032f27cSSam Leffler 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
3162b032f27cSSam Leffler 		*frm++ = 2;
3163b032f27cSSam Leffler 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
3164b032f27cSSam Leffler 	}
3165b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
3166b032f27cSSam Leffler 	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
3167b032f27cSSam Leffler 		frm = ieee80211_add_countryie(frm, ic);
3168b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_DOTH) {
3169b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
3170b032f27cSSam Leffler 			frm = ieee80211_add_powerconstraint(frm, vap);
3171b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
3172b032f27cSSam Leffler 			frm = ieee80211_add_csa(frm, vap);
3173b032f27cSSam Leffler 	}
317432b0e64bSAdrian Chadd 	if (vap->iv_flags & IEEE80211_F_DOTH) {
317532b0e64bSAdrian Chadd 		if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
317632b0e64bSAdrian Chadd 		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
317732b0e64bSAdrian Chadd 			if (vap->iv_quiet)
3178ce4552cdSAdrian Chadd 				frm = ieee80211_add_quiet(frm, vap, 0);
317932b0e64bSAdrian Chadd 		}
318032b0e64bSAdrian Chadd 	}
3181b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
3182f1481c8dSAdrian Chadd 		frm = ieee80211_add_erp(frm, vap);
3183b032f27cSSam Leffler 	frm = ieee80211_add_xrates(frm, rs);
3184d8df5f3dSRui Paulo 	frm = ieee80211_add_rsn(frm, vap);
3185b032f27cSSam Leffler 	/*
3186b032f27cSSam Leffler 	 * NB: legacy 11b clients do not get certain ie's.
3187b032f27cSSam Leffler 	 *     The caller identifies such clients by passing
3188b032f27cSSam Leffler 	 *     a token in legacy to us.  Could expand this to be
3189b032f27cSSam Leffler 	 *     any legacy client for stuff like HT ie's.
3190b032f27cSSam Leffler 	 */
3191b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
3192b032f27cSSam Leffler 	    legacy != IEEE80211_SEND_LEGACY_11B) {
3193b032f27cSSam Leffler 		frm = ieee80211_add_htcap(frm, bss);
3194b032f27cSSam Leffler 		frm = ieee80211_add_htinfo(frm, bss);
3195b032f27cSSam Leffler 	}
3196ac0b7d32SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT(bss->ni_chan) &&
3197ac0b7d32SAdrian Chadd 	    legacy != IEEE80211_SEND_LEGACY_11B) {
3198ac0b7d32SAdrian Chadd 		frm = ieee80211_add_vhtcap(frm, bss);
3199ac0b7d32SAdrian Chadd 		frm = ieee80211_add_vhtinfo(frm, bss);
3200ac0b7d32SAdrian Chadd 	}
3201d8df5f3dSRui Paulo 	frm = ieee80211_add_wpa(frm, vap);
3202b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WME)
32038379e8dbSAdrian Chadd 		frm = ieee80211_add_wme_param(frm, &ic->ic_wme,
32048379e8dbSAdrian Chadd 		    !! (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD));
3205b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
32062bfc8a91SSam Leffler 	    (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
3207b032f27cSSam Leffler 	    legacy != IEEE80211_SEND_LEGACY_11B) {
3208b032f27cSSam Leffler 		frm = ieee80211_add_htcap_vendor(frm, bss);
3209b032f27cSSam Leffler 		frm = ieee80211_add_htinfo_vendor(frm, bss);
3210b032f27cSSam Leffler 	}
3211616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
32124207227cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
32134207227cSSam Leffler 	    legacy != IEEE80211_SEND_LEGACY_11B)
32144207227cSSam Leffler 		frm = ieee80211_add_athcaps(frm, bss);
3215616190d0SSam Leffler #endif
3216b032f27cSSam Leffler 	if (vap->iv_appie_proberesp != NULL)
3217b032f27cSSam Leffler 		frm = add_appie(frm, vap->iv_appie_proberesp);
321859aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
321959aa14a9SRui Paulo 	if (vap->iv_opmode == IEEE80211_M_MBSS) {
322059aa14a9SRui Paulo 		frm = ieee80211_add_meshid(frm, vap);
322159aa14a9SRui Paulo 		frm = ieee80211_add_meshconf(frm, vap);
322259aa14a9SRui Paulo 	}
322359aa14a9SRui Paulo #endif
3224b032f27cSSam Leffler 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3225b032f27cSSam Leffler 
3226b032f27cSSam Leffler 	return m;
3227b032f27cSSam Leffler }
3228b032f27cSSam Leffler 
3229b032f27cSSam Leffler /*
3230b032f27cSSam Leffler  * Send a probe response frame to the specified mac address.
3231b032f27cSSam Leffler  * This does not go through the normal mgt frame api so we
3232b032f27cSSam Leffler  * can specify the destination address and re-use the bss node
3233b032f27cSSam Leffler  * for the sta reference.
3234b032f27cSSam Leffler  */
3235b032f27cSSam Leffler int
ieee80211_send_proberesp(struct ieee80211vap * vap,const uint8_t da[IEEE80211_ADDR_LEN],int legacy)3236b032f27cSSam Leffler ieee80211_send_proberesp(struct ieee80211vap *vap,
3237b032f27cSSam Leffler 	const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
3238b032f27cSSam Leffler {
3239b032f27cSSam Leffler 	struct ieee80211_node *bss = vap->iv_bss;
3240b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
3241b032f27cSSam Leffler 	struct mbuf *m;
32425cda6006SAdrian Chadd 	int ret;
3243b032f27cSSam Leffler 
3244b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
3245b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
3246b032f27cSSam Leffler 		    "block %s frame in CAC state", "probe response");
3247b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
3248b032f27cSSam Leffler 		return EIO;		/* XXX */
3249b032f27cSSam Leffler 	}
3250b032f27cSSam Leffler 
3251b032f27cSSam Leffler 	/*
3252b032f27cSSam Leffler 	 * Hold a reference on the node so it doesn't go away until after
3253b032f27cSSam Leffler 	 * the xmit is complete all the way in the driver.  On error we
3254b032f27cSSam Leffler 	 * will remove our reference.
3255b032f27cSSam Leffler 	 */
3256b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
3257b032f27cSSam Leffler 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
3258b032f27cSSam Leffler 	    __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
3259b032f27cSSam Leffler 	    ieee80211_node_refcnt(bss)+1);
3260b032f27cSSam Leffler 	ieee80211_ref_node(bss);
3261b032f27cSSam Leffler 
3262b032f27cSSam Leffler 	m = ieee80211_alloc_proberesp(bss, legacy);
3263b032f27cSSam Leffler 	if (m == NULL) {
3264b032f27cSSam Leffler 		ieee80211_free_node(bss);
3265b032f27cSSam Leffler 		return ENOMEM;
3266b032f27cSSam Leffler 	}
3267b032f27cSSam Leffler 
3268bd29f817SBjoern A. Zeeb 	M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT);
3269b032f27cSSam Leffler 	KASSERT(m != NULL, ("no room for header"));
3270b032f27cSSam Leffler 
32715cda6006SAdrian Chadd 	IEEE80211_TX_LOCK(ic);
32729e80b1dfSSam Leffler 	ieee80211_send_setup(bss, m,
3273b032f27cSSam Leffler 	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
32748ac160cdSSam Leffler 	     IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
3275b032f27cSSam Leffler 	/* XXX power management? */
3276c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
3277b032f27cSSam Leffler 
3278b032f27cSSam Leffler 	M_WME_SETAC(m, WME_AC_BE);
3279b032f27cSSam Leffler 
3280b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
3281b032f27cSSam Leffler 	    "send probe resp on channel %u to %s%s\n",
3282b032f27cSSam Leffler 	    ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
3283b032f27cSSam Leffler 	    legacy ? " <legacy>" : "");
3284b032f27cSSam Leffler 	IEEE80211_NODE_STAT(bss, tx_mgmt);
3285b032f27cSSam Leffler 
32865cda6006SAdrian Chadd 	ret = ieee80211_raw_output(vap, bss, m, NULL);
32875cda6006SAdrian Chadd 	IEEE80211_TX_UNLOCK(ic);
32885cda6006SAdrian Chadd 	return (ret);
3289b032f27cSSam Leffler }
3290b032f27cSSam Leffler 
3291b032f27cSSam Leffler /*
3292b032f27cSSam Leffler  * Allocate and build a RTS (Request To Send) control frame.
3293b032f27cSSam Leffler  */
3294b032f27cSSam Leffler struct mbuf *
ieee80211_alloc_rts(struct ieee80211com * ic,const uint8_t ra[IEEE80211_ADDR_LEN],const uint8_t ta[IEEE80211_ADDR_LEN],uint16_t dur)3295b032f27cSSam Leffler ieee80211_alloc_rts(struct ieee80211com *ic,
3296b032f27cSSam Leffler 	const uint8_t ra[IEEE80211_ADDR_LEN],
3297b032f27cSSam Leffler 	const uint8_t ta[IEEE80211_ADDR_LEN],
3298b032f27cSSam Leffler 	uint16_t dur)
3299b032f27cSSam Leffler {
3300b032f27cSSam Leffler 	struct ieee80211_frame_rts *rts;
3301b032f27cSSam Leffler 	struct mbuf *m;
3302b032f27cSSam Leffler 
3303b032f27cSSam Leffler 	/* XXX honor ic_headroom */
3304bd29f817SBjoern A. Zeeb 	m = m_gethdr(IEEE80211_M_NOWAIT, MT_DATA);
3305b032f27cSSam Leffler 	if (m != NULL) {
3306b032f27cSSam Leffler 		rts = mtod(m, struct ieee80211_frame_rts *);
3307b032f27cSSam Leffler 		rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
3308b032f27cSSam Leffler 			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
3309b032f27cSSam Leffler 		rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3310b032f27cSSam Leffler 		*(u_int16_t *)rts->i_dur = htole16(dur);
3311b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(rts->i_ra, ra);
3312b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(rts->i_ta, ta);
3313b032f27cSSam Leffler 
3314b032f27cSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
3315b032f27cSSam Leffler 	}
3316b032f27cSSam Leffler 	return m;
3317b032f27cSSam Leffler }
3318b032f27cSSam Leffler 
3319b032f27cSSam Leffler /*
3320b032f27cSSam Leffler  * Allocate and build a CTS (Clear To Send) control frame.
3321b032f27cSSam Leffler  */
3322b032f27cSSam Leffler struct mbuf *
ieee80211_alloc_cts(struct ieee80211com * ic,const uint8_t ra[IEEE80211_ADDR_LEN],uint16_t dur)3323b032f27cSSam Leffler ieee80211_alloc_cts(struct ieee80211com *ic,
3324b032f27cSSam Leffler 	const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
3325b032f27cSSam Leffler {
3326b032f27cSSam Leffler 	struct ieee80211_frame_cts *cts;
3327b032f27cSSam Leffler 	struct mbuf *m;
3328b032f27cSSam Leffler 
3329b032f27cSSam Leffler 	/* XXX honor ic_headroom */
3330bd29f817SBjoern A. Zeeb 	m = m_gethdr(IEEE80211_M_NOWAIT, MT_DATA);
3331b032f27cSSam Leffler 	if (m != NULL) {
3332b032f27cSSam Leffler 		cts = mtod(m, struct ieee80211_frame_cts *);
3333b032f27cSSam Leffler 		cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
3334b032f27cSSam Leffler 			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
3335b032f27cSSam Leffler 		cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3336b032f27cSSam Leffler 		*(u_int16_t *)cts->i_dur = htole16(dur);
3337b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(cts->i_ra, ra);
3338b032f27cSSam Leffler 
3339b032f27cSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
3340b032f27cSSam Leffler 	}
3341b032f27cSSam Leffler 	return m;
3342b032f27cSSam Leffler }
3343b032f27cSSam Leffler 
3344d1b67106SAndriy Voskoboinyk /*
3345d1b67106SAndriy Voskoboinyk  * Wrapper for CTS/RTS frame allocation.
3346d1b67106SAndriy Voskoboinyk  */
3347d1b67106SAndriy Voskoboinyk struct mbuf *
ieee80211_alloc_prot(struct ieee80211_node * ni,const struct mbuf * m,uint8_t rate,int prot)3348d1b67106SAndriy Voskoboinyk ieee80211_alloc_prot(struct ieee80211_node *ni, const struct mbuf *m,
3349d1b67106SAndriy Voskoboinyk     uint8_t rate, int prot)
3350d1b67106SAndriy Voskoboinyk {
3351d1b67106SAndriy Voskoboinyk 	struct ieee80211com *ic = ni->ni_ic;
3352f1481c8dSAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
3353d1b67106SAndriy Voskoboinyk 	const struct ieee80211_frame *wh;
3354d1b67106SAndriy Voskoboinyk 	struct mbuf *mprot;
3355d1b67106SAndriy Voskoboinyk 	uint16_t dur;
3356d1b67106SAndriy Voskoboinyk 	int pktlen, isshort;
3357d1b67106SAndriy Voskoboinyk 
3358d1b67106SAndriy Voskoboinyk 	KASSERT(prot == IEEE80211_PROT_RTSCTS ||
3359d1b67106SAndriy Voskoboinyk 	    prot == IEEE80211_PROT_CTSONLY,
3360d1b67106SAndriy Voskoboinyk 	    ("wrong protection type %d", prot));
3361d1b67106SAndriy Voskoboinyk 
3362d1b67106SAndriy Voskoboinyk 	wh = mtod(m, const struct ieee80211_frame *);
3363d1b67106SAndriy Voskoboinyk 	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
3364f1481c8dSAdrian Chadd 	isshort = (vap->iv_flags & IEEE80211_F_SHPREAMBLE) != 0;
3365d1b67106SAndriy Voskoboinyk 	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
3366d1b67106SAndriy Voskoboinyk 	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3367d1b67106SAndriy Voskoboinyk 
3368d1b67106SAndriy Voskoboinyk 	if (prot == IEEE80211_PROT_RTSCTS) {
3369d1b67106SAndriy Voskoboinyk 		/* NB: CTS is the same size as an ACK */
3370d1b67106SAndriy Voskoboinyk 		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3371d1b67106SAndriy Voskoboinyk 		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
3372d1b67106SAndriy Voskoboinyk 	} else
3373f1481c8dSAdrian Chadd 		mprot = ieee80211_alloc_cts(ic, vap->iv_myaddr, dur);
3374d1b67106SAndriy Voskoboinyk 
3375d1b67106SAndriy Voskoboinyk 	return (mprot);
3376d1b67106SAndriy Voskoboinyk }
3377d1b67106SAndriy Voskoboinyk 
337868e8e04eSSam Leffler static void
ieee80211_tx_mgt_timeout(void * arg)337968e8e04eSSam Leffler ieee80211_tx_mgt_timeout(void *arg)
338068e8e04eSSam Leffler {
3381e94527beSAdrian Chadd 	struct ieee80211vap *vap = arg;
338268e8e04eSSam Leffler 
338304efa18fSBjoern A. Zeeb 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
338404efa18fSBjoern A. Zeeb 	    "vap %p mode %s state %s flags %#x & %#x\n", vap,
338504efa18fSBjoern A. Zeeb 	    ieee80211_opmode_name[vap->iv_opmode],
338604efa18fSBjoern A. Zeeb 	    ieee80211_state_name[vap->iv_state],
338704efa18fSBjoern A. Zeeb 	    vap->iv_ic->ic_flags, IEEE80211_F_SCAN);
338804efa18fSBjoern A. Zeeb 
3389e94527beSAdrian Chadd 	IEEE80211_LOCK(vap->iv_ic);
3390b032f27cSSam Leffler 	if (vap->iv_state != IEEE80211_S_INIT &&
3391b032f27cSSam Leffler 	    (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
339268e8e04eSSam Leffler 		/*
339368e8e04eSSam Leffler 		 * NB: it's safe to specify a timeout as the reason here;
339468e8e04eSSam Leffler 		 *     it'll only be used in the right state.
339568e8e04eSSam Leffler 		 */
3396e94527beSAdrian Chadd 		ieee80211_new_state_locked(vap, IEEE80211_S_SCAN,
339768e8e04eSSam Leffler 			IEEE80211_SCAN_FAIL_TIMEOUT);
339868e8e04eSSam Leffler 	}
3399e94527beSAdrian Chadd 	IEEE80211_UNLOCK(vap->iv_ic);
340068e8e04eSSam Leffler }
340168e8e04eSSam Leffler 
3402e94527beSAdrian Chadd /*
3403e94527beSAdrian Chadd  * This is the callback set on net80211-sourced transmitted
3404e94527beSAdrian Chadd  * authentication request frames.
3405e94527beSAdrian Chadd  *
3406e94527beSAdrian Chadd  * This does a couple of things:
3407e94527beSAdrian Chadd  *
3408e94527beSAdrian Chadd  * + If the frame transmitted was a success, it schedules a future
3409e94527beSAdrian Chadd  *   event which will transition the interface to scan.
3410e94527beSAdrian Chadd  *   If a state transition _then_ occurs before that event occurs,
3411e94527beSAdrian Chadd  *   said state transition will cancel this callout.
3412e94527beSAdrian Chadd  *
3413e94527beSAdrian Chadd  * + If the frame transmit was a failure, it immediately schedules
3414e94527beSAdrian Chadd  *   the transition back to scan.
3415e94527beSAdrian Chadd  */
341668e8e04eSSam Leffler static void
ieee80211_tx_mgt_cb(struct ieee80211_node * ni,void * arg,int status)341768e8e04eSSam Leffler ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
341868e8e04eSSam Leffler {
3419b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
34206671366aSBrooks Davis 	enum ieee80211_state ostate = (enum ieee80211_state)(uintptr_t)arg;
342168e8e04eSSam Leffler 
342268e8e04eSSam Leffler 	/*
342368e8e04eSSam Leffler 	 * Frame transmit completed; arrange timer callback.  If
3424a4641f4eSPedro F. Giffuni 	 * transmit was successfully we wait for response.  Otherwise
342568e8e04eSSam Leffler 	 * we arrange an immediate callback instead of doing the
342668e8e04eSSam Leffler 	 * callback directly since we don't know what state the driver
342768e8e04eSSam Leffler 	 * is in (e.g. what locks it is holding).  This work should
342868e8e04eSSam Leffler 	 * not be too time-critical and not happen too often so the
342968e8e04eSSam Leffler 	 * added overhead is acceptable.
343068e8e04eSSam Leffler 	 *
343168e8e04eSSam Leffler 	 * XXX what happens if !acked but response shows up before callback?
343268e8e04eSSam Leffler 	 */
3433e94527beSAdrian Chadd 	if (vap->iv_state == ostate) {
343404efa18fSBjoern A. Zeeb 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
34354f158a44SCy Schubert 		    "ni %p mode %s state %s arg %p status %d\n", ni,
343604efa18fSBjoern A. Zeeb 		    ieee80211_opmode_name[vap->iv_opmode],
34374f158a44SCy Schubert 		    ieee80211_state_name[vap->iv_state], arg, status);
343804efa18fSBjoern A. Zeeb 
3439b032f27cSSam Leffler 		callout_reset(&vap->iv_mgtsend,
344068e8e04eSSam Leffler 			status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
3441e94527beSAdrian Chadd 			ieee80211_tx_mgt_timeout, vap);
3442e94527beSAdrian Chadd 	}
344368e8e04eSSam Leffler }
344468e8e04eSSam Leffler 
3445b032f27cSSam Leffler static void
ieee80211_beacon_construct(struct mbuf * m,uint8_t * frm,struct ieee80211_node * ni)3446b032f27cSSam Leffler ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
3447210ab3c2SAdrian Chadd 	struct ieee80211_node *ni)
34488a1b9b6aSSam Leffler {
3449b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
3450210ab3c2SAdrian Chadd 	struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
3451b105a069SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
3452b032f27cSSam Leffler 	struct ieee80211_rateset *rs = &ni->ni_rates;
345368e8e04eSSam Leffler 	uint16_t capinfo;
34548a1b9b6aSSam Leffler 
34558a1b9b6aSSam Leffler 	/*
34568a1b9b6aSSam Leffler 	 * beacon frame format
345751172f62SAdrian Chadd 	 *
345851172f62SAdrian Chadd 	 * TODO: update to 802.11-2012; a lot of stuff has changed;
345951172f62SAdrian Chadd 	 * vendor extensions should be at the end, etc.
346051172f62SAdrian Chadd 	 *
34618a1b9b6aSSam Leffler 	 *	[8] time stamp
34628a1b9b6aSSam Leffler 	 *	[2] beacon interval
34638a1b9b6aSSam Leffler 	 *	[2] cabability information
34648a1b9b6aSSam Leffler 	 *	[tlv] ssid
34658a1b9b6aSSam Leffler 	 *	[tlv] supported rates
34668a1b9b6aSSam Leffler 	 *	[3] parameter set (DS)
3467b032f27cSSam Leffler 	 *	[8] CF parameter set (optional)
34688a1b9b6aSSam Leffler 	 *	[tlv] parameter set (IBSS/TIM)
3469b032f27cSSam Leffler 	 *	[tlv] country (optional)
3470b032f27cSSam Leffler 	 *	[3] power control (optional)
3471b032f27cSSam Leffler 	 *	[5] channel switch announcement (CSA) (optional)
347251172f62SAdrian Chadd 	 * XXX TODO: Quiet
347351172f62SAdrian Chadd 	 * XXX TODO: IBSS DFS
347451172f62SAdrian Chadd 	 * XXX TODO: TPC report
34758a1b9b6aSSam Leffler 	 *	[tlv] extended rate phy (ERP)
34768a1b9b6aSSam Leffler 	 *	[tlv] extended supported rates
3477688fe74dSSam Leffler 	 *	[tlv] RSN parameters
347851172f62SAdrian Chadd 	 * XXX TODO: BSSLOAD
347951172f62SAdrian Chadd 	 * (XXX EDCA parameter set, QoS capability?)
348051172f62SAdrian Chadd 	 * XXX TODO: AP channel report
348151172f62SAdrian Chadd 	 *
348268e8e04eSSam Leffler 	 *	[tlv] HT capabilities
348368e8e04eSSam Leffler 	 *	[tlv] HT information
348451172f62SAdrian Chadd 	 *	XXX TODO: 20/40 BSS coexistence
348551172f62SAdrian Chadd 	 * Mesh:
348651172f62SAdrian Chadd 	 * XXX TODO: Meshid
348751172f62SAdrian Chadd 	 * XXX TODO: mesh config
348851172f62SAdrian Chadd 	 * XXX TODO: mesh awake window
348951172f62SAdrian Chadd 	 * XXX TODO: beacon timing (mesh, etc)
349051172f62SAdrian Chadd 	 * XXX TODO: MCCAOP Advertisement Overview
349151172f62SAdrian Chadd 	 * XXX TODO: MCCAOP Advertisement
349251172f62SAdrian Chadd 	 * XXX TODO: Mesh channel switch parameters
349351172f62SAdrian Chadd 	 * VHT:
349451172f62SAdrian Chadd 	 * XXX TODO: VHT capabilities
349551172f62SAdrian Chadd 	 * XXX TODO: VHT operation
349651172f62SAdrian Chadd 	 * XXX TODO: VHT transmit power envelope
349751172f62SAdrian Chadd 	 * XXX TODO: channel switch wrapper element
349851172f62SAdrian Chadd 	 * XXX TODO: extended BSS load element
349951172f62SAdrian Chadd 	 *
3500b032f27cSSam Leffler 	 * XXX Vendor-specific OIDs (e.g. Atheros)
3501b032f27cSSam Leffler 	 *	[tlv] WPA parameters
3502b032f27cSSam Leffler 	 *	[tlv] WME parameters
350368e8e04eSSam Leffler 	 *	[tlv] Vendor OUI HT capabilities (optional)
350468e8e04eSSam Leffler 	 *	[tlv] Vendor OUI HT information (optional)
35054207227cSSam Leffler 	 *	[tlv] Atheros capabilities (optional)
350610ad9a77SSam Leffler 	 *	[tlv] TDMA parameters (optional)
350759aa14a9SRui Paulo 	 *	[tlv] Mesh ID (MBSS)
350859aa14a9SRui Paulo 	 *	[tlv] Mesh Conf (MBSS)
3509b032f27cSSam Leffler 	 *	[tlv] application data (optional)
35108a1b9b6aSSam Leffler 	 */
35118a1b9b6aSSam Leffler 
35121b6167d2SSam Leffler 	memset(bo, 0, sizeof(*bo));
35131b6167d2SSam Leffler 
35148a1b9b6aSSam Leffler 	memset(frm, 0, 8);	/* XXX timestamp is set by hardware/driver */
35158a1b9b6aSSam Leffler 	frm += 8;
351668e8e04eSSam Leffler 	*(uint16_t *)frm = htole16(ni->ni_intval);
35178a1b9b6aSSam Leffler 	frm += 2;
351859aa14a9SRui Paulo 	capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
351968e8e04eSSam Leffler 	bo->bo_caps = (uint16_t *)frm;
352068e8e04eSSam Leffler 	*(uint16_t *)frm = htole16(capinfo);
35218a1b9b6aSSam Leffler 	frm += 2;
35228a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_SSID;
3523b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
35248a1b9b6aSSam Leffler 		*frm++ = ni->ni_esslen;
35258a1b9b6aSSam Leffler 		memcpy(frm, ni->ni_essid, ni->ni_esslen);
35268a1b9b6aSSam Leffler 		frm += ni->ni_esslen;
35278a1b9b6aSSam Leffler 	} else
35288a1b9b6aSSam Leffler 		*frm++ = 0;
35298a1b9b6aSSam Leffler 	frm = ieee80211_add_rates(frm, rs);
3530b032f27cSSam Leffler 	if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
35318a1b9b6aSSam Leffler 		*frm++ = IEEE80211_ELEMID_DSPARMS;
35328a1b9b6aSSam Leffler 		*frm++ = 1;
3533b032f27cSSam Leffler 		*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
3534b032f27cSSam Leffler 	}
3535b032f27cSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PCF) {
3536b032f27cSSam Leffler 		bo->bo_cfp = frm;
3537b032f27cSSam Leffler 		frm = ieee80211_add_cfparms(frm, ic);
35388a1b9b6aSSam Leffler 	}
35398a1b9b6aSSam Leffler 	bo->bo_tim = frm;
3540b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
35418a1b9b6aSSam Leffler 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
35428a1b9b6aSSam Leffler 		*frm++ = 2;
35438a1b9b6aSSam Leffler 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
35448a1b9b6aSSam Leffler 		bo->bo_tim_len = 0;
354559aa14a9SRui Paulo 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
354659aa14a9SRui Paulo 	    vap->iv_opmode == IEEE80211_M_MBSS) {
354759aa14a9SRui Paulo 		/* TIM IE is the same for Mesh and Hostap */
35488a1b9b6aSSam Leffler 		struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
35498a1b9b6aSSam Leffler 
35508a1b9b6aSSam Leffler 		tie->tim_ie = IEEE80211_ELEMID_TIM;
35518a1b9b6aSSam Leffler 		tie->tim_len = 4;	/* length */
35528a1b9b6aSSam Leffler 		tie->tim_count = 0;	/* DTIM count */
3553b032f27cSSam Leffler 		tie->tim_period = vap->iv_dtim_period;	/* DTIM period */
35548a1b9b6aSSam Leffler 		tie->tim_bitctl = 0;	/* bitmap control */
35558a1b9b6aSSam Leffler 		tie->tim_bitmap[0] = 0;	/* Partial Virtual Bitmap */
35568a1b9b6aSSam Leffler 		frm += sizeof(struct ieee80211_tim_ie);
35578a1b9b6aSSam Leffler 		bo->bo_tim_len = 1;
35588a1b9b6aSSam Leffler 	}
3559b105a069SSam Leffler 	bo->bo_tim_trailer = frm;
3560b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
3561b032f27cSSam Leffler 	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
3562b032f27cSSam Leffler 		frm = ieee80211_add_countryie(frm, ic);
3563b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_DOTH) {
3564b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
3565b032f27cSSam Leffler 			frm = ieee80211_add_powerconstraint(frm, vap);
3566b032f27cSSam Leffler 		bo->bo_csa = frm;
3567b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
3568b032f27cSSam Leffler 			frm = ieee80211_add_csa(frm, vap);
3569b032f27cSSam Leffler 	} else
3570b032f27cSSam Leffler 		bo->bo_csa = frm;
357132b0e64bSAdrian Chadd 
35724d3dcce5SAdrian Chadd 	bo->bo_quiet = NULL;
357332b0e64bSAdrian Chadd 	if (vap->iv_flags & IEEE80211_F_DOTH) {
357432b0e64bSAdrian Chadd 		if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
35754d3dcce5SAdrian Chadd 		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
35764d3dcce5SAdrian Chadd 		    (vap->iv_quiet == 1)) {
35774d3dcce5SAdrian Chadd 			/*
35784d3dcce5SAdrian Chadd 			 * We only insert the quiet IE offset if
35794d3dcce5SAdrian Chadd 			 * the quiet IE is enabled.  Otherwise don't
35804d3dcce5SAdrian Chadd 			 * put it here or we'll just overwrite
35814d3dcce5SAdrian Chadd 			 * some other beacon contents.
35824d3dcce5SAdrian Chadd 			 */
35834d3dcce5SAdrian Chadd 			if (vap->iv_quiet) {
35844d3dcce5SAdrian Chadd 				bo->bo_quiet = frm;
3585ce4552cdSAdrian Chadd 				frm = ieee80211_add_quiet(frm,vap, 0);
358632b0e64bSAdrian Chadd 			}
35874d3dcce5SAdrian Chadd 		}
35884d3dcce5SAdrian Chadd 	}
358932b0e64bSAdrian Chadd 
3590b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
35910912bac9SSam Leffler 		bo->bo_erp = frm;
3592f1481c8dSAdrian Chadd 		frm = ieee80211_add_erp(frm, vap);
35931b6167d2SSam Leffler 	}
359468e8e04eSSam Leffler 	frm = ieee80211_add_xrates(frm, rs);
3595d8df5f3dSRui Paulo 	frm = ieee80211_add_rsn(frm, vap);
3596b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
359768e8e04eSSam Leffler 		frm = ieee80211_add_htcap(frm, ni);
359868e8e04eSSam Leffler 		bo->bo_htinfo = frm;
359968e8e04eSSam Leffler 		frm = ieee80211_add_htinfo(frm, ni);
36001b6167d2SSam Leffler 	}
360151172f62SAdrian Chadd 
360251172f62SAdrian Chadd 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
360351172f62SAdrian Chadd 		frm = ieee80211_add_vhtcap(frm, ni);
360451172f62SAdrian Chadd 		bo->bo_vhtinfo = frm;
360551172f62SAdrian Chadd 		frm = ieee80211_add_vhtinfo(frm, ni);
360651172f62SAdrian Chadd 		/* Transmit power envelope */
360751172f62SAdrian Chadd 		/* Channel switch wrapper element */
360851172f62SAdrian Chadd 		/* Extended bss load element */
360951172f62SAdrian Chadd 	}
361051172f62SAdrian Chadd 
3611d8df5f3dSRui Paulo 	frm = ieee80211_add_wpa(frm, vap);
3612b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WME) {
36131b6167d2SSam Leffler 		bo->bo_wme = frm;
36148379e8dbSAdrian Chadd 		frm = ieee80211_add_wme_param(frm, &ic->ic_wme,
36158379e8dbSAdrian Chadd 		    !! (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD));
36161b6167d2SSam Leffler 	}
3617b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
36182bfc8a91SSam Leffler 	    (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
361968e8e04eSSam Leffler 		frm = ieee80211_add_htcap_vendor(frm, ni);
362068e8e04eSSam Leffler 		frm = ieee80211_add_htinfo_vendor(frm, ni);
36210912bac9SSam Leffler 	}
362251172f62SAdrian Chadd 
36234207227cSSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
36244207227cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_ATHEROS) {
36254207227cSSam Leffler 		bo->bo_ath = frm;
36264207227cSSam Leffler 		frm = ieee80211_add_athcaps(frm, ni);
36274207227cSSam Leffler 	}
36284207227cSSam Leffler #endif
362910ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
363010ad9a77SSam Leffler 	if (vap->iv_caps & IEEE80211_C_TDMA) {
363110ad9a77SSam Leffler 		bo->bo_tdma = frm;
363210ad9a77SSam Leffler 		frm = ieee80211_add_tdma(frm, vap);
363310ad9a77SSam Leffler 	}
363410ad9a77SSam Leffler #endif
3635b032f27cSSam Leffler 	if (vap->iv_appie_beacon != NULL) {
3636b032f27cSSam Leffler 		bo->bo_appie = frm;
3637b032f27cSSam Leffler 		bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
3638b032f27cSSam Leffler 		frm = add_appie(frm, vap->iv_appie_beacon);
3639b032f27cSSam Leffler 	}
364051172f62SAdrian Chadd 
364151172f62SAdrian Chadd 	/* XXX TODO: move meshid/meshconf up to before vendor extensions? */
364259aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
364359aa14a9SRui Paulo 	if (vap->iv_opmode == IEEE80211_M_MBSS) {
364459aa14a9SRui Paulo 		frm = ieee80211_add_meshid(frm, vap);
3645d093681cSRui Paulo 		bo->bo_meshconf = frm;
364659aa14a9SRui Paulo 		frm = ieee80211_add_meshconf(frm, vap);
364759aa14a9SRui Paulo 	}
364859aa14a9SRui Paulo #endif
3649b105a069SSam Leffler 	bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
3650b032f27cSSam Leffler 	bo->bo_csa_trailer_len = frm - bo->bo_csa;
365168e8e04eSSam Leffler 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3652b032f27cSSam Leffler }
3653b032f27cSSam Leffler 
3654b032f27cSSam Leffler /*
3655b032f27cSSam Leffler  * Allocate a beacon frame and fillin the appropriate bits.
3656b032f27cSSam Leffler  */
3657b032f27cSSam Leffler struct mbuf *
ieee80211_beacon_alloc(struct ieee80211_node * ni)3658210ab3c2SAdrian Chadd ieee80211_beacon_alloc(struct ieee80211_node *ni)
3659b032f27cSSam Leffler {
3660b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
3661b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
3662b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
3663b032f27cSSam Leffler 	struct ieee80211_frame *wh;
3664b032f27cSSam Leffler 	struct mbuf *m;
3665b032f27cSSam Leffler 	int pktlen;
3666b032f27cSSam Leffler 	uint8_t *frm;
3667b032f27cSSam Leffler 
3668b032f27cSSam Leffler 	/*
36694d3dcce5SAdrian Chadd 	 * Update the "We're putting the quiet IE in the beacon" state.
36704d3dcce5SAdrian Chadd 	 */
36714d3dcce5SAdrian Chadd 	if (vap->iv_quiet == 1)
36724d3dcce5SAdrian Chadd 		vap->iv_flags_ext |= IEEE80211_FEXT_QUIET_IE;
36734d3dcce5SAdrian Chadd 	else if (vap->iv_quiet == 0)
36744d3dcce5SAdrian Chadd 		vap->iv_flags_ext &= ~IEEE80211_FEXT_QUIET_IE;
36754d3dcce5SAdrian Chadd 
36764d3dcce5SAdrian Chadd 	/*
3677b032f27cSSam Leffler 	 * beacon frame format
367851172f62SAdrian Chadd 	 *
367951172f62SAdrian Chadd 	 * Note: This needs updating for 802.11-2012.
368051172f62SAdrian Chadd 	 *
3681b032f27cSSam Leffler 	 *	[8] time stamp
3682b032f27cSSam Leffler 	 *	[2] beacon interval
3683b032f27cSSam Leffler 	 *	[2] cabability information
3684b032f27cSSam Leffler 	 *	[tlv] ssid
3685b032f27cSSam Leffler 	 *	[tlv] supported rates
3686b032f27cSSam Leffler 	 *	[3] parameter set (DS)
3687b032f27cSSam Leffler 	 *	[8] CF parameter set (optional)
3688b032f27cSSam Leffler 	 *	[tlv] parameter set (IBSS/TIM)
3689b032f27cSSam Leffler 	 *	[tlv] country (optional)
3690b032f27cSSam Leffler 	 *	[3] power control (optional)
3691b032f27cSSam Leffler 	 *	[5] channel switch announcement (CSA) (optional)
3692b032f27cSSam Leffler 	 *	[tlv] extended rate phy (ERP)
3693b032f27cSSam Leffler 	 *	[tlv] extended supported rates
3694b032f27cSSam Leffler 	 *	[tlv] RSN parameters
3695b032f27cSSam Leffler 	 *	[tlv] HT capabilities
3696b032f27cSSam Leffler 	 *	[tlv] HT information
369751172f62SAdrian Chadd 	 *	[tlv] VHT capabilities
369851172f62SAdrian Chadd 	 *	[tlv] VHT operation
3699b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT capabilities (optional)
3700b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT information (optional)
3701b032f27cSSam Leffler 	 * XXX Vendor-specific OIDs (e.g. Atheros)
3702b032f27cSSam Leffler 	 *	[tlv] WPA parameters
3703b032f27cSSam Leffler 	 *	[tlv] WME parameters
370410ad9a77SSam Leffler 	 *	[tlv] TDMA parameters (optional)
370559aa14a9SRui Paulo 	 *	[tlv] Mesh ID (MBSS)
370659aa14a9SRui Paulo 	 *	[tlv] Mesh Conf (MBSS)
3707b032f27cSSam Leffler 	 *	[tlv] application data (optional)
3708b032f27cSSam Leffler 	 * NB: we allocate the max space required for the TIM bitmap.
3709b032f27cSSam Leffler 	 * XXX how big is this?
3710b032f27cSSam Leffler 	 */
3711b032f27cSSam Leffler 	pktlen =   8					/* time stamp */
3712b032f27cSSam Leffler 		 + sizeof(uint16_t)			/* beacon interval */
3713b032f27cSSam Leffler 		 + sizeof(uint16_t)			/* capabilities */
3714b032f27cSSam Leffler 		 + 2 + ni->ni_esslen			/* ssid */
3715b032f27cSSam Leffler 	         + 2 + IEEE80211_RATE_SIZE		/* supported rates */
3716b032f27cSSam Leffler 	         + 2 + 1				/* DS parameters */
3717b032f27cSSam Leffler 		 + 2 + 6				/* CF parameters */
3718b032f27cSSam Leffler 		 + 2 + 4 + vap->iv_tim_len		/* DTIM/IBSSPARMS */
3719b032f27cSSam Leffler 		 + IEEE80211_COUNTRY_MAX_SIZE		/* country */
3720b032f27cSSam Leffler 		 + 2 + 1				/* power control */
3721b032f27cSSam Leffler 		 + sizeof(struct ieee80211_csa_ie)	/* CSA */
372232b0e64bSAdrian Chadd 		 + sizeof(struct ieee80211_quiet_ie)	/* Quiet */
3723b032f27cSSam Leffler 		 + 2 + 1				/* ERP */
3724b032f27cSSam Leffler 	         + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
3725b032f27cSSam Leffler 		 + (vap->iv_caps & IEEE80211_C_WPA ?	/* WPA 1+2 */
3726b032f27cSSam Leffler 			2*sizeof(struct ieee80211_ie_wpa) : 0)
3727b032f27cSSam Leffler 		 /* XXX conditional? */
3728b032f27cSSam Leffler 		 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
3729b032f27cSSam Leffler 		 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
3730e85eb4c8SBjoern A. Zeeb 		 + 2 + sizeof(struct ieee80211_vht_cap)/* VHT caps */
3731e85eb4c8SBjoern A. Zeeb 		 + 2 + sizeof(struct ieee80211_vht_operation)/* VHT info */
3732b032f27cSSam Leffler 		 + (vap->iv_caps & IEEE80211_C_WME ?	/* WME */
3733b032f27cSSam Leffler 			sizeof(struct ieee80211_wme_param) : 0)
37344207227cSSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
37354207227cSSam Leffler 		 + sizeof(struct ieee80211_ath_ie)	/* ATH */
37364207227cSSam Leffler #endif
373710ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
373810ad9a77SSam Leffler 		 + (vap->iv_caps & IEEE80211_C_TDMA ?	/* TDMA */
373910ad9a77SSam Leffler 			sizeof(struct ieee80211_tdma_param) : 0)
374010ad9a77SSam Leffler #endif
374159aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH
374259aa14a9SRui Paulo 		 + 2 + ni->ni_meshidlen
374359aa14a9SRui Paulo 		 + sizeof(struct ieee80211_meshconf_ie)
374459aa14a9SRui Paulo #endif
3745b032f27cSSam Leffler 		 + IEEE80211_MAX_APPIE
3746b032f27cSSam Leffler 		 ;
3747b032f27cSSam Leffler 	m = ieee80211_getmgtframe(&frm,
3748b032f27cSSam Leffler 		ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
3749b032f27cSSam Leffler 	if (m == NULL) {
3750b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
3751b032f27cSSam Leffler 			"%s: cannot get buf; size %u\n", __func__, pktlen);
3752b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
3753b032f27cSSam Leffler 		return NULL;
3754b032f27cSSam Leffler 	}
3755210ab3c2SAdrian Chadd 	ieee80211_beacon_construct(m, frm, ni);
37568a1b9b6aSSam Leffler 
3757bd29f817SBjoern A. Zeeb 	M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT);
37588a1b9b6aSSam Leffler 	KASSERT(m != NULL, ("no space for 802.11 header?"));
37598a1b9b6aSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
37608a1b9b6aSSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
37618a1b9b6aSSam Leffler 	    IEEE80211_FC0_SUBTYPE_BEACON;
37628a1b9b6aSSam Leffler 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
376368e8e04eSSam Leffler 	*(uint16_t *)wh->i_dur = 0;
37648a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
3765b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
37668a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
376768e8e04eSSam Leffler 	*(uint16_t *)wh->i_seq = 0;
37688a1b9b6aSSam Leffler 
37698a1b9b6aSSam Leffler 	return m;
37708a1b9b6aSSam Leffler }
37718a1b9b6aSSam Leffler 
37728a1b9b6aSSam Leffler /*
37738a1b9b6aSSam Leffler  * Update the dynamic parts of a beacon frame based on the current state.
37748a1b9b6aSSam Leffler  */
37758a1b9b6aSSam Leffler int
ieee80211_beacon_update(struct ieee80211_node * ni,struct mbuf * m,int mcast)3776210ab3c2SAdrian Chadd ieee80211_beacon_update(struct ieee80211_node *ni, struct mbuf *m, int mcast)
37778a1b9b6aSSam Leffler {
3778b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
3779210ab3c2SAdrian Chadd 	struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
3780b105a069SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
37818a1b9b6aSSam Leffler 	int len_changed = 0;
378268e8e04eSSam Leffler 	uint16_t capinfo;
3783fa3324c9SAdrian Chadd 	struct ieee80211_frame *wh;
3784fa3324c9SAdrian Chadd 	ieee80211_seq seqno;
37858a1b9b6aSSam Leffler 
3786b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
3787b032f27cSSam Leffler 	/*
3788b032f27cSSam Leffler 	 * Handle 11h channel change when we've reached the count.
3789b032f27cSSam Leffler 	 * We must recalculate the beacon frame contents to account
3790b032f27cSSam Leffler 	 * for the new channel.  Note we do this only for the first
3791b032f27cSSam Leffler 	 * vap that reaches this point; subsequent vaps just update
3792b032f27cSSam Leffler 	 * their beacon state to reflect the recalculated channel.
3793b032f27cSSam Leffler 	 */
3794b032f27cSSam Leffler 	if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
3795b032f27cSSam Leffler 	    vap->iv_csa_count == ic->ic_csa_count) {
3796b032f27cSSam Leffler 		vap->iv_csa_count = 0;
3797b032f27cSSam Leffler 		/*
3798b032f27cSSam Leffler 		 * Effect channel change before reconstructing the beacon
3799b032f27cSSam Leffler 		 * frame contents as many places reference ni_chan.
3800b032f27cSSam Leffler 		 */
3801b032f27cSSam Leffler 		if (ic->ic_csa_newchan != NULL)
3802b032f27cSSam Leffler 			ieee80211_csa_completeswitch(ic);
3803b032f27cSSam Leffler 		/*
3804b032f27cSSam Leffler 		 * NB: ieee80211_beacon_construct clears all pending
3805b032f27cSSam Leffler 		 * updates in bo_flags so we don't need to explicitly
3806b032f27cSSam Leffler 		 * clear IEEE80211_BEACON_CSA.
3807b032f27cSSam Leffler 		 */
3808b032f27cSSam Leffler 		ieee80211_beacon_construct(m,
3809210ab3c2SAdrian Chadd 		    mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
3810b032f27cSSam Leffler 
3811b032f27cSSam Leffler 		/* XXX do WME aggressive mode processing? */
3812b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
3813b032f27cSSam Leffler 		return 1;		/* just assume length changed */
3814b032f27cSSam Leffler 	}
3815b032f27cSSam Leffler 
38164d3dcce5SAdrian Chadd 	/*
38174d3dcce5SAdrian Chadd 	 * Handle the quiet time element being added and removed.
38184d3dcce5SAdrian Chadd 	 * Again, for now we just cheat and reconstruct the whole
38194d3dcce5SAdrian Chadd 	 * beacon - that way the gap is provided as appropriate.
38204d3dcce5SAdrian Chadd 	 *
38214d3dcce5SAdrian Chadd 	 * So, track whether we have already added the IE versus
38224d3dcce5SAdrian Chadd 	 * whether we want to be adding the IE.
38234d3dcce5SAdrian Chadd 	 */
38244d3dcce5SAdrian Chadd 	if ((vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE) &&
38254d3dcce5SAdrian Chadd 	    (vap->iv_quiet == 0)) {
38264d3dcce5SAdrian Chadd 		/*
38274d3dcce5SAdrian Chadd 		 * Quiet time beacon IE enabled, but it's disabled;
38284d3dcce5SAdrian Chadd 		 * recalc
38294d3dcce5SAdrian Chadd 		 */
38304d3dcce5SAdrian Chadd 		vap->iv_flags_ext &= ~IEEE80211_FEXT_QUIET_IE;
38314d3dcce5SAdrian Chadd 		ieee80211_beacon_construct(m,
38324d3dcce5SAdrian Chadd 		    mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
38334d3dcce5SAdrian Chadd 		/* XXX do WME aggressive mode processing? */
38344d3dcce5SAdrian Chadd 		IEEE80211_UNLOCK(ic);
38354d3dcce5SAdrian Chadd 		return 1;		/* just assume length changed */
38364d3dcce5SAdrian Chadd 	}
38374d3dcce5SAdrian Chadd 
38384d3dcce5SAdrian Chadd 	if (((vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE) == 0) &&
38394d3dcce5SAdrian Chadd 	    (vap->iv_quiet == 1)) {
38404d3dcce5SAdrian Chadd 		/*
38414d3dcce5SAdrian Chadd 		 * Quiet time beacon IE disabled, but it's now enabled;
38424d3dcce5SAdrian Chadd 		 * recalc
38434d3dcce5SAdrian Chadd 		 */
38444d3dcce5SAdrian Chadd 		vap->iv_flags_ext |= IEEE80211_FEXT_QUIET_IE;
38454d3dcce5SAdrian Chadd 		ieee80211_beacon_construct(m,
38464d3dcce5SAdrian Chadd 		    mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
38474d3dcce5SAdrian Chadd 		/* XXX do WME aggressive mode processing? */
38484d3dcce5SAdrian Chadd 		IEEE80211_UNLOCK(ic);
38494d3dcce5SAdrian Chadd 		return 1;		/* just assume length changed */
38504d3dcce5SAdrian Chadd 	}
38514d3dcce5SAdrian Chadd 
3852fa3324c9SAdrian Chadd 	wh = mtod(m, struct ieee80211_frame *);
385351172f62SAdrian Chadd 
385451172f62SAdrian Chadd 	/*
385551172f62SAdrian Chadd 	 * XXX TODO Strictly speaking this should be incremented with the TX
385651172f62SAdrian Chadd 	 * lock held so as to serialise access to the non-qos TID sequence
385751172f62SAdrian Chadd 	 * number space.
385851172f62SAdrian Chadd 	 *
385951172f62SAdrian Chadd 	 * If the driver identifies it does its own TX seqno management then
386051172f62SAdrian Chadd 	 * we can skip this (and still not do the TX seqno.)
386151172f62SAdrian Chadd 	 */
3862fa3324c9SAdrian Chadd 	seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
3863fa3324c9SAdrian Chadd 	*(uint16_t *)&wh->i_seq[0] =
3864fa3324c9SAdrian Chadd 		htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
3865fa3324c9SAdrian Chadd 	M_SEQNO_SET(m, seqno);
3866fa3324c9SAdrian Chadd 
38678a1b9b6aSSam Leffler 	/* XXX faster to recalculate entirely or just changes? */
386859aa14a9SRui Paulo 	capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
38698a1b9b6aSSam Leffler 	*bo->bo_caps = htole16(capinfo);
38708a1b9b6aSSam Leffler 
3871b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WME) {
38728a1b9b6aSSam Leffler 		struct ieee80211_wme_state *wme = &ic->ic_wme;
38738a1b9b6aSSam Leffler 
38748a1b9b6aSSam Leffler 		/*
3875a4641f4eSPedro F. Giffuni 		 * Check for aggressive mode change.  When there is
38768a1b9b6aSSam Leffler 		 * significant high priority traffic in the BSS
38778a1b9b6aSSam Leffler 		 * throttle back BE traffic by using conservative
3878a4641f4eSPedro F. Giffuni 		 * parameters.  Otherwise BE uses aggressive params
38798a1b9b6aSSam Leffler 		 * to optimize performance of legacy/non-QoS traffic.
38808a1b9b6aSSam Leffler 		 */
38818a1b9b6aSSam Leffler 		if (wme->wme_flags & WME_F_AGGRMODE) {
38828a1b9b6aSSam Leffler 			if (wme->wme_hipri_traffic >
38838a1b9b6aSSam Leffler 			    wme->wme_hipri_switch_thresh) {
3884b032f27cSSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
38858a1b9b6aSSam Leffler 				    "%s: traffic %u, disable aggressive mode\n",
38868a1b9b6aSSam Leffler 				    __func__, wme->wme_hipri_traffic);
38878a1b9b6aSSam Leffler 				wme->wme_flags &= ~WME_F_AGGRMODE;
3888b032f27cSSam Leffler 				ieee80211_wme_updateparams_locked(vap);
38898a1b9b6aSSam Leffler 				wme->wme_hipri_traffic =
38908a1b9b6aSSam Leffler 					wme->wme_hipri_switch_hysteresis;
38918a1b9b6aSSam Leffler 			} else
38928a1b9b6aSSam Leffler 				wme->wme_hipri_traffic = 0;
38938a1b9b6aSSam Leffler 		} else {
38948a1b9b6aSSam Leffler 			if (wme->wme_hipri_traffic <=
38958a1b9b6aSSam Leffler 			    wme->wme_hipri_switch_thresh) {
3896b032f27cSSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
38978a1b9b6aSSam Leffler 				    "%s: traffic %u, enable aggressive mode\n",
38988a1b9b6aSSam Leffler 				    __func__, wme->wme_hipri_traffic);
38998a1b9b6aSSam Leffler 				wme->wme_flags |= WME_F_AGGRMODE;
3900b032f27cSSam Leffler 				ieee80211_wme_updateparams_locked(vap);
39018a1b9b6aSSam Leffler 				wme->wme_hipri_traffic = 0;
39028a1b9b6aSSam Leffler 			} else
39038a1b9b6aSSam Leffler 				wme->wme_hipri_traffic =
39048a1b9b6aSSam Leffler 					wme->wme_hipri_switch_hysteresis;
39058a1b9b6aSSam Leffler 		}
3906b105a069SSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
39078379e8dbSAdrian Chadd 			(void) ieee80211_add_wme_param(bo->bo_wme, wme,
39088379e8dbSAdrian Chadd 			  vap->iv_flags_ext & IEEE80211_FEXT_UAPSD);
3909b105a069SSam Leffler 			clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
39108a1b9b6aSSam Leffler 		}
39118a1b9b6aSSam Leffler 	}
39128a1b9b6aSSam Leffler 
3913b105a069SSam Leffler 	if (isset(bo->bo_flags,  IEEE80211_BEACON_HTINFO)) {
3914b032f27cSSam Leffler 		ieee80211_ht_update_beacon(vap, bo);
3915b105a069SSam Leffler 		clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
391668e8e04eSSam Leffler 	}
391710ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
391810ad9a77SSam Leffler 	if (vap->iv_caps & IEEE80211_C_TDMA) {
391910ad9a77SSam Leffler 		/*
392010ad9a77SSam Leffler 		 * NB: the beacon is potentially updated every TBTT.
392110ad9a77SSam Leffler 		 */
392210ad9a77SSam Leffler 		ieee80211_tdma_update_beacon(vap, bo);
392310ad9a77SSam Leffler 	}
392410ad9a77SSam Leffler #endif
3925d093681cSRui Paulo #ifdef IEEE80211_SUPPORT_MESH
3926d093681cSRui Paulo 	if (vap->iv_opmode == IEEE80211_M_MBSS)
3927d093681cSRui Paulo 		ieee80211_mesh_update_beacon(vap, bo);
3928d093681cSRui Paulo #endif
3929d093681cSRui Paulo 
393059aa14a9SRui Paulo 	if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
393159aa14a9SRui Paulo 	    vap->iv_opmode == IEEE80211_M_MBSS) {	/* NB: no IBSS support*/
39328a1b9b6aSSam Leffler 		struct ieee80211_tim_ie *tie =
39338a1b9b6aSSam Leffler 			(struct ieee80211_tim_ie *) bo->bo_tim;
3934b105a069SSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
39358a1b9b6aSSam Leffler 			u_int timlen, timoff, i;
39368a1b9b6aSSam Leffler 			/*
39378a1b9b6aSSam Leffler 			 * ATIM/DTIM needs updating.  If it fits in the
39388a1b9b6aSSam Leffler 			 * current space allocated then just copy in the
39398a1b9b6aSSam Leffler 			 * new bits.  Otherwise we need to move any trailing
39408a1b9b6aSSam Leffler 			 * data to make room.  Note that we know there is
39418a1b9b6aSSam Leffler 			 * contiguous space because ieee80211_beacon_allocate
39428a1b9b6aSSam Leffler 			 * insures there is space in the mbuf to write a
3943b032f27cSSam Leffler 			 * maximal-size virtual bitmap (based on iv_max_aid).
39448a1b9b6aSSam Leffler 			 */
39458a1b9b6aSSam Leffler 			/*
39468a1b9b6aSSam Leffler 			 * Calculate the bitmap size and offset, copy any
39478a1b9b6aSSam Leffler 			 * trailer out of the way, and then copy in the
39488a1b9b6aSSam Leffler 			 * new bitmap and update the information element.
39498a1b9b6aSSam Leffler 			 * Note that the tim bitmap must contain at least
39508a1b9b6aSSam Leffler 			 * one byte and any offset must be even.
39518a1b9b6aSSam Leffler 			 */
3952b032f27cSSam Leffler 			if (vap->iv_ps_pending != 0) {
39538a1b9b6aSSam Leffler 				timoff = 128;		/* impossibly large */
3954b032f27cSSam Leffler 				for (i = 0; i < vap->iv_tim_len; i++)
3955b032f27cSSam Leffler 					if (vap->iv_tim_bitmap[i]) {
39568a1b9b6aSSam Leffler 						timoff = i &~ 1;
39578a1b9b6aSSam Leffler 						break;
39588a1b9b6aSSam Leffler 					}
39598a1b9b6aSSam Leffler 				KASSERT(timoff != 128, ("tim bitmap empty!"));
3960b032f27cSSam Leffler 				for (i = vap->iv_tim_len-1; i >= timoff; i--)
3961b032f27cSSam Leffler 					if (vap->iv_tim_bitmap[i])
39628a1b9b6aSSam Leffler 						break;
39638a1b9b6aSSam Leffler 				timlen = 1 + (i - timoff);
39648a1b9b6aSSam Leffler 			} else {
39658a1b9b6aSSam Leffler 				timoff = 0;
39668a1b9b6aSSam Leffler 				timlen = 1;
39678a1b9b6aSSam Leffler 			}
396851172f62SAdrian Chadd 
396951172f62SAdrian Chadd 			/*
397051172f62SAdrian Chadd 			 * TODO: validate this!
397151172f62SAdrian Chadd 			 */
39728a1b9b6aSSam Leffler 			if (timlen != bo->bo_tim_len) {
39738a1b9b6aSSam Leffler 				/* copy up/down trailer */
39740912bac9SSam Leffler 				int adjust = tie->tim_bitmap+timlen
3975b105a069SSam Leffler 					   - bo->bo_tim_trailer;
3976b105a069SSam Leffler 				ovbcopy(bo->bo_tim_trailer,
3977b105a069SSam Leffler 				    bo->bo_tim_trailer+adjust,
3978b105a069SSam Leffler 				    bo->bo_tim_trailer_len);
3979b105a069SSam Leffler 				bo->bo_tim_trailer += adjust;
39800912bac9SSam Leffler 				bo->bo_erp += adjust;
398168e8e04eSSam Leffler 				bo->bo_htinfo += adjust;
398251172f62SAdrian Chadd 				bo->bo_vhtinfo += adjust;
398302e69b54SBernhard Schmidt #ifdef IEEE80211_SUPPORT_SUPERG
39844207227cSSam Leffler 				bo->bo_ath += adjust;
39854207227cSSam Leffler #endif
398602e69b54SBernhard Schmidt #ifdef IEEE80211_SUPPORT_TDMA
398792e870edSSam Leffler 				bo->bo_tdma += adjust;
398892e870edSSam Leffler #endif
398902e69b54SBernhard Schmidt #ifdef IEEE80211_SUPPORT_MESH
3990d093681cSRui Paulo 				bo->bo_meshconf += adjust;
3991d093681cSRui Paulo #endif
3992b032f27cSSam Leffler 				bo->bo_appie += adjust;
3993b032f27cSSam Leffler 				bo->bo_wme += adjust;
3994b032f27cSSam Leffler 				bo->bo_csa += adjust;
399532b0e64bSAdrian Chadd 				bo->bo_quiet += adjust;
39968a1b9b6aSSam Leffler 				bo->bo_tim_len = timlen;
39978a1b9b6aSSam Leffler 
39988a1b9b6aSSam Leffler 				/* update information element */
39998a1b9b6aSSam Leffler 				tie->tim_len = 3 + timlen;
40008a1b9b6aSSam Leffler 				tie->tim_bitctl = timoff;
40018a1b9b6aSSam Leffler 				len_changed = 1;
40028a1b9b6aSSam Leffler 			}
4003b032f27cSSam Leffler 			memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
40048a1b9b6aSSam Leffler 				bo->bo_tim_len);
40058a1b9b6aSSam Leffler 
4006b105a069SSam Leffler 			clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
40078a1b9b6aSSam Leffler 
4008b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
40098a1b9b6aSSam Leffler 				"%s: TIM updated, pending %u, off %u, len %u\n",
4010b032f27cSSam Leffler 				__func__, vap->iv_ps_pending, timoff, timlen);
40118a1b9b6aSSam Leffler 		}
40128a1b9b6aSSam Leffler 		/* count down DTIM period */
40138a1b9b6aSSam Leffler 		if (tie->tim_count == 0)
40148a1b9b6aSSam Leffler 			tie->tim_count = tie->tim_period - 1;
40158a1b9b6aSSam Leffler 		else
40168a1b9b6aSSam Leffler 			tie->tim_count--;
40178a1b9b6aSSam Leffler 		/* update state for buffered multicast frames on DTIM */
4018a196b35fSSam Leffler 		if (mcast && tie->tim_count == 0)
40198a1b9b6aSSam Leffler 			tie->tim_bitctl |= 1;
40208a1b9b6aSSam Leffler 		else
40218a1b9b6aSSam Leffler 			tie->tim_bitctl &= ~1;
4022b032f27cSSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
4023b032f27cSSam Leffler 			struct ieee80211_csa_ie *csa =
4024b032f27cSSam Leffler 			    (struct ieee80211_csa_ie *) bo->bo_csa;
4025b032f27cSSam Leffler 
4026b032f27cSSam Leffler 			/*
4027b032f27cSSam Leffler 			 * Insert or update CSA ie.  If we're just starting
4028b032f27cSSam Leffler 			 * to count down to the channel switch then we need
4029b032f27cSSam Leffler 			 * to insert the CSA ie.  Otherwise we just need to
4030b032f27cSSam Leffler 			 * drop the count.  The actual change happens above
4031b032f27cSSam Leffler 			 * when the vap's count reaches the target count.
4032b032f27cSSam Leffler 			 */
4033b032f27cSSam Leffler 			if (vap->iv_csa_count == 0) {
4034b032f27cSSam Leffler 				memmove(&csa[1], csa, bo->bo_csa_trailer_len);
4035b032f27cSSam Leffler 				bo->bo_erp += sizeof(*csa);
4036d01b3c26SSam Leffler 				bo->bo_htinfo += sizeof(*csa);
403751172f62SAdrian Chadd 				bo->bo_vhtinfo += sizeof(*csa);
4038b032f27cSSam Leffler 				bo->bo_wme += sizeof(*csa);
403902e69b54SBernhard Schmidt #ifdef IEEE80211_SUPPORT_SUPERG
40404207227cSSam Leffler 				bo->bo_ath += sizeof(*csa);
40414207227cSSam Leffler #endif
404202e69b54SBernhard Schmidt #ifdef IEEE80211_SUPPORT_TDMA
404392e870edSSam Leffler 				bo->bo_tdma += sizeof(*csa);
404492e870edSSam Leffler #endif
404502e69b54SBernhard Schmidt #ifdef IEEE80211_SUPPORT_MESH
4046d093681cSRui Paulo 				bo->bo_meshconf += sizeof(*csa);
4047d093681cSRui Paulo #endif
4048b032f27cSSam Leffler 				bo->bo_appie += sizeof(*csa);
4049b032f27cSSam Leffler 				bo->bo_csa_trailer_len += sizeof(*csa);
405032b0e64bSAdrian Chadd 				bo->bo_quiet += sizeof(*csa);
4051b032f27cSSam Leffler 				bo->bo_tim_trailer_len += sizeof(*csa);
4052b032f27cSSam Leffler 				m->m_len += sizeof(*csa);
4053b032f27cSSam Leffler 				m->m_pkthdr.len += sizeof(*csa);
4054b032f27cSSam Leffler 
4055b032f27cSSam Leffler 				ieee80211_add_csa(bo->bo_csa, vap);
4056b032f27cSSam Leffler 			} else
4057b032f27cSSam Leffler 				csa->csa_count--;
4058b032f27cSSam Leffler 			vap->iv_csa_count++;
4059b032f27cSSam Leffler 			/* NB: don't clear IEEE80211_BEACON_CSA */
4060b032f27cSSam Leffler 		}
40614d3dcce5SAdrian Chadd 
40624d3dcce5SAdrian Chadd 		/*
40634d3dcce5SAdrian Chadd 		 * Only add the quiet time IE if we've enabled it
40644d3dcce5SAdrian Chadd 		 * as appropriate.
40654d3dcce5SAdrian Chadd 		 */
406632b0e64bSAdrian Chadd 		if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
406732b0e64bSAdrian Chadd 		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
40684d3dcce5SAdrian Chadd 			if (vap->iv_quiet &&
40694d3dcce5SAdrian Chadd 			    (vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE)) {
4070ce4552cdSAdrian Chadd 				ieee80211_add_quiet(bo->bo_quiet, vap, 1);
407132b0e64bSAdrian Chadd 			}
40724d3dcce5SAdrian Chadd 		}
4073b105a069SSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
40740912bac9SSam Leffler 			/*
40750912bac9SSam Leffler 			 * ERP element needs updating.
40760912bac9SSam Leffler 			 */
4077f1481c8dSAdrian Chadd 			(void) ieee80211_add_erp(bo->bo_erp, vap);
4078b105a069SSam Leffler 			clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
40790912bac9SSam Leffler 		}
40804207227cSSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
40814207227cSSam Leffler 		if (isset(bo->bo_flags,  IEEE80211_BEACON_ATH)) {
40824207227cSSam Leffler 			ieee80211_add_athcaps(bo->bo_ath, ni);
40834207227cSSam Leffler 			clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
40844207227cSSam Leffler 		}
40854207227cSSam Leffler #endif
40868a1b9b6aSSam Leffler 	}
4087b032f27cSSam Leffler 	if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
4088b032f27cSSam Leffler 		const struct ieee80211_appie *aie = vap->iv_appie_beacon;
4089b032f27cSSam Leffler 		int aielen;
4090b032f27cSSam Leffler 		uint8_t *frm;
4091b032f27cSSam Leffler 
4092b032f27cSSam Leffler 		aielen = 0;
4093b032f27cSSam Leffler 		if (aie != NULL)
4094b032f27cSSam Leffler 			aielen += aie->ie_len;
4095b032f27cSSam Leffler 		if (aielen != bo->bo_appie_len) {
4096b032f27cSSam Leffler 			/* copy up/down trailer */
4097b032f27cSSam Leffler 			int adjust = aielen - bo->bo_appie_len;
4098b032f27cSSam Leffler 			ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
4099b032f27cSSam Leffler 				bo->bo_tim_trailer_len);
4100b032f27cSSam Leffler 			bo->bo_tim_trailer += adjust;
4101b032f27cSSam Leffler 			bo->bo_appie += adjust;
4102b032f27cSSam Leffler 			bo->bo_appie_len = aielen;
4103b032f27cSSam Leffler 
4104b032f27cSSam Leffler 			len_changed = 1;
4105b032f27cSSam Leffler 		}
4106b032f27cSSam Leffler 		frm = bo->bo_appie;
4107b032f27cSSam Leffler 		if (aie != NULL)
4108b032f27cSSam Leffler 			frm  = add_appie(frm, aie);
4109b032f27cSSam Leffler 		clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
4110b032f27cSSam Leffler 	}
4111b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
41128a1b9b6aSSam Leffler 
41138a1b9b6aSSam Leffler 	return len_changed;
41148a1b9b6aSSam Leffler }
411574b4c76eSAdrian Chadd 
411674b4c76eSAdrian Chadd /*
411774b4c76eSAdrian Chadd  * Do Ethernet-LLC encapsulation for each payload in a fast frame
411874b4c76eSAdrian Chadd  * tunnel encapsulation.  The frame is assumed to have an Ethernet
411974b4c76eSAdrian Chadd  * header at the front that must be stripped before prepending the
412074b4c76eSAdrian Chadd  * LLC followed by the Ethernet header passed in (with an Ethernet
412174b4c76eSAdrian Chadd  * type that specifies the payload size).
412274b4c76eSAdrian Chadd  */
412374b4c76eSAdrian Chadd struct mbuf *
ieee80211_ff_encap1(struct ieee80211vap * vap,struct mbuf * m,const struct ether_header * eh)412474b4c76eSAdrian Chadd ieee80211_ff_encap1(struct ieee80211vap *vap, struct mbuf *m,
412574b4c76eSAdrian Chadd 	const struct ether_header *eh)
412674b4c76eSAdrian Chadd {
412774b4c76eSAdrian Chadd 	struct llc *llc;
412874b4c76eSAdrian Chadd 	uint16_t payload;
412974b4c76eSAdrian Chadd 
413074b4c76eSAdrian Chadd 	/* XXX optimize by combining m_adj+M_PREPEND */
413174b4c76eSAdrian Chadd 	m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
413274b4c76eSAdrian Chadd 	llc = mtod(m, struct llc *);
413374b4c76eSAdrian Chadd 	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
413474b4c76eSAdrian Chadd 	llc->llc_control = LLC_UI;
413574b4c76eSAdrian Chadd 	llc->llc_snap.org_code[0] = 0;
413674b4c76eSAdrian Chadd 	llc->llc_snap.org_code[1] = 0;
413774b4c76eSAdrian Chadd 	llc->llc_snap.org_code[2] = 0;
413874b4c76eSAdrian Chadd 	llc->llc_snap.ether_type = eh->ether_type;
413974b4c76eSAdrian Chadd 	payload = m->m_pkthdr.len;		/* NB: w/o Ethernet header */
414074b4c76eSAdrian Chadd 
4141bd29f817SBjoern A. Zeeb 	M_PREPEND(m, sizeof(struct ether_header), IEEE80211_M_NOWAIT);
414274b4c76eSAdrian Chadd 	if (m == NULL) {		/* XXX cannot happen */
414374b4c76eSAdrian Chadd 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
414474b4c76eSAdrian Chadd 			"%s: no space for ether_header\n", __func__);
414574b4c76eSAdrian Chadd 		vap->iv_stats.is_tx_nobuf++;
414674b4c76eSAdrian Chadd 		return NULL;
414774b4c76eSAdrian Chadd 	}
414874b4c76eSAdrian Chadd 	ETHER_HEADER_COPY(mtod(m, void *), eh);
414974b4c76eSAdrian Chadd 	mtod(m, struct ether_header *)->ether_type = htons(payload);
415074b4c76eSAdrian Chadd 	return m;
415174b4c76eSAdrian Chadd }
415236ee7775SAdrian Chadd 
415336ee7775SAdrian Chadd /*
415436ee7775SAdrian Chadd  * Complete an mbuf transmission.
415536ee7775SAdrian Chadd  *
415636ee7775SAdrian Chadd  * For now, this simply processes a completed frame after the
415736ee7775SAdrian Chadd  * driver has completed it's transmission and/or retransmission.
415836ee7775SAdrian Chadd  * It assumes the frame is an 802.11 encapsulated frame.
415936ee7775SAdrian Chadd  *
416036ee7775SAdrian Chadd  * Later on it will grow to become the exit path for a given frame
416136ee7775SAdrian Chadd  * from the driver and, depending upon how it's been encapsulated
416236ee7775SAdrian Chadd  * and already transmitted, it may end up doing A-MPDU retransmission,
416336ee7775SAdrian Chadd  * power save requeuing, etc.
416436ee7775SAdrian Chadd  *
416536ee7775SAdrian Chadd  * In order for the above to work, the driver entry point to this
416636ee7775SAdrian Chadd  * must not hold any driver locks.  Thus, the driver needs to delay
416736ee7775SAdrian Chadd  * any actual mbuf completion until it can release said locks.
416836ee7775SAdrian Chadd  *
416936ee7775SAdrian Chadd  * This frees the mbuf and if the mbuf has a node reference,
417036ee7775SAdrian Chadd  * the node reference will be freed.
417136ee7775SAdrian Chadd  */
417236ee7775SAdrian Chadd void
ieee80211_tx_complete(struct ieee80211_node * ni,struct mbuf * m,int status)417336ee7775SAdrian Chadd ieee80211_tx_complete(struct ieee80211_node *ni, struct mbuf *m, int status)
417436ee7775SAdrian Chadd {
417536ee7775SAdrian Chadd 
417636ee7775SAdrian Chadd 	if (ni != NULL) {
41777a79cebfSGleb Smirnoff 		struct ifnet *ifp = ni->ni_vap->iv_ifp;
41787a79cebfSGleb Smirnoff 
41797a79cebfSGleb Smirnoff 		if (status == 0) {
41807a79cebfSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
41817a79cebfSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
41827a79cebfSGleb Smirnoff 			if (m->m_flags & M_MCAST)
41837a79cebfSGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
41847a79cebfSGleb Smirnoff 		} else
41857a79cebfSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
418604efa18fSBjoern A. Zeeb 		if (m->m_flags & M_TXCB) {
418704efa18fSBjoern A. Zeeb 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
418804efa18fSBjoern A. Zeeb 			   "ni %p vap %p mode %s state %s m %p status %d\n", ni, ni->ni_vap,
418904efa18fSBjoern A. Zeeb 			   ieee80211_opmode_name[ni->ni_vap->iv_opmode],
419004efa18fSBjoern A. Zeeb 			   ieee80211_state_name[ni->ni_vap->iv_state], m, status);
419136ee7775SAdrian Chadd 			ieee80211_process_callback(ni, m, status);
419204efa18fSBjoern A. Zeeb 		}
419336ee7775SAdrian Chadd 		ieee80211_free_node(ni);
419436ee7775SAdrian Chadd 	}
419536ee7775SAdrian Chadd 	m_freem(m);
419636ee7775SAdrian Chadd }
4197