xref: /freebsd/sys/net80211/ieee80211_output.c (revision f2a6a13c3b215ee8d0e59208fdaae028c197a4c0)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
310ad9a77SSam Leffler  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
41a1e1d21SSam Leffler  * All rights reserved.
51a1e1d21SSam Leffler  *
61a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
71a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
81a1e1d21SSam Leffler  * are met:
91a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
107535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
117535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
127535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
137535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
141a1e1d21SSam Leffler  *
157535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
167535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
177535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
187535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
197535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
207535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
217535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
227535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
237535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
247535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
251a1e1d21SSam Leffler  */
261a1e1d21SSam Leffler 
271a1e1d21SSam Leffler #include <sys/cdefs.h>
281a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
291a1e1d21SSam Leffler 
301a1e1d21SSam Leffler #include "opt_inet.h"
31b032f27cSSam Leffler #include "opt_wlan.h"
321a1e1d21SSam Leffler 
331a1e1d21SSam Leffler #include <sys/param.h>
341a1e1d21SSam Leffler #include <sys/systm.h>
351a1e1d21SSam Leffler #include <sys/mbuf.h>
361a1e1d21SSam Leffler #include <sys/kernel.h>
371a1e1d21SSam Leffler #include <sys/endian.h>
381a1e1d21SSam Leffler 
398a1b9b6aSSam Leffler #include <sys/socket.h>
401a1e1d21SSam Leffler 
411a1e1d21SSam Leffler #include <net/bpf.h>
428a1b9b6aSSam Leffler #include <net/ethernet.h>
438a1b9b6aSSam Leffler #include <net/if.h>
448a1b9b6aSSam Leffler #include <net/if_llc.h>
458a1b9b6aSSam Leffler #include <net/if_media.h>
468a1b9b6aSSam Leffler #include <net/if_vlan_var.h>
478a1b9b6aSSam Leffler 
488a1b9b6aSSam Leffler #include <net80211/ieee80211_var.h>
4968e8e04eSSam Leffler #include <net80211/ieee80211_regdomain.h>
50616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
51616190d0SSam Leffler #include <net80211/ieee80211_superg.h>
52616190d0SSam Leffler #endif
5310ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
5410ad9a77SSam Leffler #include <net80211/ieee80211_tdma.h>
5510ad9a77SSam Leffler #endif
56b032f27cSSam Leffler #include <net80211/ieee80211_wds.h>
571a1e1d21SSam Leffler 
581a1e1d21SSam Leffler #ifdef INET
591a1e1d21SSam Leffler #include <netinet/in.h>
601a1e1d21SSam Leffler #include <netinet/if_ether.h>
618a1b9b6aSSam Leffler #include <netinet/in_systm.h>
628a1b9b6aSSam Leffler #include <netinet/ip.h>
638a1b9b6aSSam Leffler #endif
648a1b9b6aSSam Leffler 
6568e8e04eSSam Leffler #define	ETHER_HEADER_COPY(dst, src) \
6668e8e04eSSam Leffler 	memcpy(dst, src, sizeof(struct ether_header))
6768e8e04eSSam Leffler 
68b032f27cSSam Leffler static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
6968e8e04eSSam Leffler 	u_int hdrsize, u_int ciphdrsize, u_int mtu);
7068e8e04eSSam Leffler static	void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
7168e8e04eSSam Leffler 
728a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
738a1b9b6aSSam Leffler /*
748a1b9b6aSSam Leffler  * Decide if an outbound management frame should be
758a1b9b6aSSam Leffler  * printed when debugging is enabled.  This filters some
768a1b9b6aSSam Leffler  * of the less interesting frames that come frequently
778a1b9b6aSSam Leffler  * (e.g. beacons).
788a1b9b6aSSam Leffler  */
798a1b9b6aSSam Leffler static __inline int
80b032f27cSSam Leffler doprint(struct ieee80211vap *vap, int subtype)
818a1b9b6aSSam Leffler {
828a1b9b6aSSam Leffler 	switch (subtype) {
838a1b9b6aSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
84b032f27cSSam Leffler 		return (vap->iv_opmode == IEEE80211_M_IBSS);
858a1b9b6aSSam Leffler 	}
868a1b9b6aSSam Leffler 	return 1;
878a1b9b6aSSam Leffler }
881a1e1d21SSam Leffler #endif
891a1e1d21SSam Leffler 
900a915fadSSam Leffler /*
91b032f27cSSam Leffler  * Start method for vap's.  All packets from the stack come
92b032f27cSSam Leffler  * through here.  We handle common processing of the packets
93b032f27cSSam Leffler  * before dispatching them to the underlying device.
94b032f27cSSam Leffler  */
95b032f27cSSam Leffler void
96b032f27cSSam Leffler ieee80211_start(struct ifnet *ifp)
97b032f27cSSam Leffler {
98b032f27cSSam Leffler #define	IS_DWDS(vap) \
99b032f27cSSam Leffler 	(vap->iv_opmode == IEEE80211_M_WDS && \
100b032f27cSSam Leffler 	 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
101b032f27cSSam Leffler 	struct ieee80211vap *vap = ifp->if_softc;
102b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
103b032f27cSSam Leffler 	struct ifnet *parent = ic->ic_ifp;
104b032f27cSSam Leffler 	struct ieee80211_node *ni;
105b032f27cSSam Leffler 	struct mbuf *m;
106b032f27cSSam Leffler 	struct ether_header *eh;
107b032f27cSSam Leffler 	int error;
108b032f27cSSam Leffler 
109b032f27cSSam Leffler 	/* NB: parent must be up and running */
110b032f27cSSam Leffler 	if (!IFNET_IS_UP_RUNNING(parent)) {
111b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
112b032f27cSSam Leffler 		    "%s: ignore queue, parent %s not up+running\n",
113b032f27cSSam Leffler 		    __func__, parent->if_xname);
114b032f27cSSam Leffler 		/* XXX stat */
115b032f27cSSam Leffler 		return;
116b032f27cSSam Leffler 	}
117b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_SLEEP) {
118b032f27cSSam Leffler 		/*
119b032f27cSSam Leffler 		 * In power save, wakeup device for transmit.
120b032f27cSSam Leffler 		 */
121b032f27cSSam Leffler 		ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
122b032f27cSSam Leffler 		return;
123b032f27cSSam Leffler 	}
124b032f27cSSam Leffler 	/*
125b032f27cSSam Leffler 	 * No data frames go out unless we're running.
126b032f27cSSam Leffler 	 * Note in particular this covers CAC and CSA
127b032f27cSSam Leffler 	 * states (though maybe we should check muting
128b032f27cSSam Leffler 	 * for CSA).
129b032f27cSSam Leffler 	 */
130b032f27cSSam Leffler 	if (vap->iv_state != IEEE80211_S_RUN) {
131b032f27cSSam Leffler 		IEEE80211_LOCK(ic);
132b032f27cSSam Leffler 		/* re-check under the com lock to avoid races */
133b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN) {
134b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
135b032f27cSSam Leffler 			    "%s: ignore queue, in %s state\n",
136b032f27cSSam Leffler 			    __func__, ieee80211_state_name[vap->iv_state]);
137b032f27cSSam Leffler 			vap->iv_stats.is_tx_badstate++;
138b032f27cSSam Leffler 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
139b032f27cSSam Leffler 			IEEE80211_UNLOCK(ic);
140b032f27cSSam Leffler 			return;
141b032f27cSSam Leffler 		}
142b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
143b032f27cSSam Leffler 	}
144b032f27cSSam Leffler 	for (;;) {
145b032f27cSSam Leffler 		IFQ_DEQUEUE(&ifp->if_snd, m);
146b032f27cSSam Leffler 		if (m == NULL)
147b032f27cSSam Leffler 			break;
148b032f27cSSam Leffler 		/*
149b032f27cSSam Leffler 		 * Sanitize mbuf flags for net80211 use.  We cannot
150b032f27cSSam Leffler 		 * clear M_PWR_SAV because this may be set for frames
151b032f27cSSam Leffler 		 * that are re-submitted from the power save queue.
152b032f27cSSam Leffler 		 *
153b032f27cSSam Leffler 		 * NB: This must be done before ieee80211_classify as
154b032f27cSSam Leffler 		 *     it marks EAPOL in frames with M_EAPOL.
155b032f27cSSam Leffler 		 */
156b032f27cSSam Leffler 		m->m_flags &= ~(M_80211_TX - M_PWR_SAV);
157b032f27cSSam Leffler 		/*
158b032f27cSSam Leffler 		 * Cancel any background scan.
159b032f27cSSam Leffler 		 */
160b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SCAN)
161b032f27cSSam Leffler 			ieee80211_cancel_anyscan(vap);
162b032f27cSSam Leffler 		/*
163b032f27cSSam Leffler 		 * Find the node for the destination so we can do
164b032f27cSSam Leffler 		 * things like power save and fast frames aggregation.
165b032f27cSSam Leffler 		 *
166b032f27cSSam Leffler 		 * NB: past this point various code assumes the first
167b032f27cSSam Leffler 		 *     mbuf has the 802.3 header present (and contiguous).
168b032f27cSSam Leffler 		 */
169b032f27cSSam Leffler 		ni = NULL;
170b032f27cSSam Leffler 		if (m->m_len < sizeof(struct ether_header) &&
171b032f27cSSam Leffler 		   (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
172b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
173b032f27cSSam Leffler 			    "discard frame, %s\n", "m_pullup failed");
174b032f27cSSam Leffler 			vap->iv_stats.is_tx_nobuf++;	/* XXX */
175b032f27cSSam Leffler 			ifp->if_oerrors++;
176b032f27cSSam Leffler 			continue;
177b032f27cSSam Leffler 		}
178b032f27cSSam Leffler 		eh = mtod(m, struct ether_header *);
179b032f27cSSam Leffler 		if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
180b032f27cSSam Leffler 			if (IS_DWDS(vap)) {
181b032f27cSSam Leffler 				/*
182b032f27cSSam Leffler 				 * Only unicast frames from the above go out
183b032f27cSSam Leffler 				 * DWDS vaps; multicast frames are handled by
184b032f27cSSam Leffler 				 * dispatching the frame as it comes through
185b032f27cSSam Leffler 				 * the AP vap (see below).
186b032f27cSSam Leffler 				 */
187b032f27cSSam Leffler 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
188b032f27cSSam Leffler 				    eh->ether_dhost, "mcast", "%s", "on DWDS");
189b032f27cSSam Leffler 				vap->iv_stats.is_dwds_mcast++;
190b032f27cSSam Leffler 				m_freem(m);
191b032f27cSSam Leffler 				continue;
192b032f27cSSam Leffler 			}
193b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
194b032f27cSSam Leffler 				/*
195b032f27cSSam Leffler 				 * Spam DWDS vap's w/ multicast traffic.
196b032f27cSSam Leffler 				 */
197b032f27cSSam Leffler 				/* XXX only if dwds in use? */
198b032f27cSSam Leffler 				ieee80211_dwds_mcast(vap, m);
199b032f27cSSam Leffler 			}
200b032f27cSSam Leffler 		}
201b032f27cSSam Leffler 		ni = ieee80211_find_txnode(vap, eh->ether_dhost);
202b032f27cSSam Leffler 		if (ni == NULL) {
203b032f27cSSam Leffler 			/* NB: ieee80211_find_txnode does stat+msg */
204b032f27cSSam Leffler 			ifp->if_oerrors++;
205b032f27cSSam Leffler 			m_freem(m);
206b032f27cSSam Leffler 			continue;
207b032f27cSSam Leffler 		}
2086ff2e10aSSam Leffler 		if (ni->ni_associd == 0 &&
2091b999d64SSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
210b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
211b032f27cSSam Leffler 			    eh->ether_dhost, NULL,
212b032f27cSSam Leffler 			    "sta not associated (type 0x%04x)",
213b032f27cSSam Leffler 			    htons(eh->ether_type));
214b032f27cSSam Leffler 			vap->iv_stats.is_tx_notassoc++;
215b032f27cSSam Leffler 			ifp->if_oerrors++;
216b032f27cSSam Leffler 			m_freem(m);
217b032f27cSSam Leffler 			ieee80211_free_node(ni);
218b032f27cSSam Leffler 			continue;
219b032f27cSSam Leffler 		}
220339ccfb3SSam Leffler 
221b032f27cSSam Leffler 		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
222b032f27cSSam Leffler 		    (m->m_flags & M_PWR_SAV) == 0) {
223b032f27cSSam Leffler 			/*
224b032f27cSSam Leffler 			 * Station in power save mode; pass the frame
225b032f27cSSam Leffler 			 * to the 802.11 layer and continue.  We'll get
226b032f27cSSam Leffler 			 * the frame back when the time is right.
227b032f27cSSam Leffler 			 * XXX lose WDS vap linkage?
228b032f27cSSam Leffler 			 */
22963092fceSSam Leffler 			(void) ieee80211_pwrsave(ni, m);
230b032f27cSSam Leffler 			ieee80211_free_node(ni);
231b032f27cSSam Leffler 			continue;
232b032f27cSSam Leffler 		}
233b032f27cSSam Leffler 		/* calculate priority so drivers can find the tx queue */
234b032f27cSSam Leffler 		if (ieee80211_classify(ni, m)) {
235b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
236b032f27cSSam Leffler 			    eh->ether_dhost, NULL,
237b032f27cSSam Leffler 			    "%s", "classification failure");
238b032f27cSSam Leffler 			vap->iv_stats.is_tx_classify++;
239b032f27cSSam Leffler 			ifp->if_oerrors++;
240b032f27cSSam Leffler 			m_freem(m);
241b032f27cSSam Leffler 			ieee80211_free_node(ni);
242b032f27cSSam Leffler 			continue;
243b032f27cSSam Leffler 		}
2447ebe9c0eSSam Leffler 		/*
2457ebe9c0eSSam Leffler 		 * Stash the node pointer.  Note that we do this after
2467ebe9c0eSSam Leffler 		 * any call to ieee80211_dwds_mcast because that code
2477ebe9c0eSSam Leffler 		 * uses any existing value for rcvif to identify the
2487ebe9c0eSSam Leffler 		 * interface it (might have been) received on.
2497ebe9c0eSSam Leffler 		 */
2507ebe9c0eSSam Leffler 		m->m_pkthdr.rcvif = (void *)ni;
251b032f27cSSam Leffler 
252339ccfb3SSam Leffler 		BPF_MTAP(ifp, m);		/* 802.3 tx */
253b032f27cSSam Leffler 
254168f582eSSam Leffler 		/*
255168f582eSSam Leffler 		 * Check if A-MPDU tx aggregation is setup or if we
256168f582eSSam Leffler 		 * should try to enable it.  The sta must be associated
257168f582eSSam Leffler 		 * with HT and A-MPDU enabled for use.  When the policy
258168f582eSSam Leffler 		 * routine decides we should enable A-MPDU we issue an
259168f582eSSam Leffler 		 * ADDBA request and wait for a reply.  The frame being
260168f582eSSam Leffler 		 * encapsulated will go out w/o using A-MPDU, or possibly
261168f582eSSam Leffler 		 * it might be collected by the driver and held/retransmit.
262168f582eSSam Leffler 		 * The default ic_ampdu_enable routine handles staggering
263168f582eSSam Leffler 		 * ADDBA requests in case the receiver NAK's us or we are
264168f582eSSam Leffler 		 * otherwise unable to establish a BA stream.
265168f582eSSam Leffler 		 */
266168f582eSSam Leffler 		if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
267168f582eSSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_AMPDU_TX) &&
268168f582eSSam Leffler 		    (m->m_flags & M_EAPOL) == 0) {
269168f582eSSam Leffler 			const int ac = M_WME_GETAC(m);
270168f582eSSam Leffler 			struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[ac];
271168f582eSSam Leffler 
272168f582eSSam Leffler 			ieee80211_txampdu_count_packet(tap);
273168f582eSSam Leffler 			if (IEEE80211_AMPDU_RUNNING(tap)) {
274168f582eSSam Leffler 				/*
275168f582eSSam Leffler 				 * Operational, mark frame for aggregation.
276168f582eSSam Leffler 				 *
277168f582eSSam Leffler 				 * XXX do tx aggregation here
278168f582eSSam Leffler 				 */
279168f582eSSam Leffler 				m->m_flags |= M_AMPDU_MPDU;
280168f582eSSam Leffler 			} else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
281168f582eSSam Leffler 			    ic->ic_ampdu_enable(ni, tap)) {
282168f582eSSam Leffler 				/*
283168f582eSSam Leffler 				 * Not negotiated yet, request service.
284168f582eSSam Leffler 				 */
285168f582eSSam Leffler 				ieee80211_ampdu_request(ni, tap);
286168f582eSSam Leffler 				/* XXX hold frame for reply? */
287168f582eSSam Leffler 			}
288168f582eSSam Leffler 		}
289339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
290168f582eSSam Leffler 		else if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF)) {
291339ccfb3SSam Leffler 			m = ieee80211_ff_check(ni, m);
292339ccfb3SSam Leffler 			if (m == NULL) {
293339ccfb3SSam Leffler 				/* NB: any ni ref held on stageq */
294339ccfb3SSam Leffler 				continue;
295339ccfb3SSam Leffler 			}
296339ccfb3SSam Leffler 		}
297339ccfb3SSam Leffler #endif /* IEEE80211_SUPPORT_SUPERG */
2989fb0fccbSSam Leffler 		if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
299b032f27cSSam Leffler 			/*
300339ccfb3SSam Leffler 			 * Encapsulate the packet in prep for transmission.
301b032f27cSSam Leffler 			 */
302339ccfb3SSam Leffler 			m = ieee80211_encap(vap, ni, m);
303339ccfb3SSam Leffler 			if (m == NULL) {
304339ccfb3SSam Leffler 				/* NB: stat+msg handled in ieee80211_encap */
305339ccfb3SSam Leffler 				ieee80211_free_node(ni);
306339ccfb3SSam Leffler 				continue;
307339ccfb3SSam Leffler 			}
3089fb0fccbSSam Leffler 		}
309b032f27cSSam Leffler 
31077825c77SSam Leffler 		/* XXX fragmented frames not handled */
31177825c77SSam Leffler 		if (bpf_peers_present(vap->iv_rawbpf))
31277825c77SSam Leffler 			bpf_mtap(vap->iv_rawbpf, m);
31377825c77SSam Leffler 
31472fecb4dSSam Leffler 		error = parent->if_transmit(parent, m);
315b032f27cSSam Leffler 		if (error != 0) {
316b032f27cSSam Leffler 			/* NB: IFQ_HANDOFF reclaims mbuf */
317b032f27cSSam Leffler 			ieee80211_free_node(ni);
318b032f27cSSam Leffler 		} else {
319b032f27cSSam Leffler 			ifp->if_opackets++;
320b032f27cSSam Leffler 		}
321b032f27cSSam Leffler 		ic->ic_lastdata = ticks;
322b032f27cSSam Leffler 	}
323b032f27cSSam Leffler #undef IS_DWDS
324b032f27cSSam Leffler }
325b032f27cSSam Leffler 
326b032f27cSSam Leffler /*
327b032f27cSSam Leffler  * 802.11 output routine. This is (currently) used only to
328b032f27cSSam Leffler  * connect bpf write calls to the 802.11 layer for injecting
329fad788b1SSam Leffler  * raw 802.11 frames.
330b032f27cSSam Leffler  */
331b032f27cSSam Leffler int
332b032f27cSSam Leffler ieee80211_output(struct ifnet *ifp, struct mbuf *m,
333279aa3d4SKip Macy 	struct sockaddr *dst, struct route *ro)
334b032f27cSSam Leffler {
335b032f27cSSam Leffler #define senderr(e) do { error = (e); goto bad;} while (0)
336b032f27cSSam Leffler 	struct ieee80211_node *ni = NULL;
337b032f27cSSam Leffler 	struct ieee80211vap *vap;
338b032f27cSSam Leffler 	struct ieee80211_frame *wh;
339b032f27cSSam Leffler 	int error;
340b032f27cSSam Leffler 
341b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
342b032f27cSSam Leffler 		/*
343b032f27cSSam Leffler 		 * Short-circuit requests if the vap is marked OACTIVE
344b032f27cSSam Leffler 		 * as this is used when tearing down state to indicate
345b032f27cSSam Leffler 		 * the vap may be gone.  This can also happen because a
346b032f27cSSam Leffler 		 * packet came down through ieee80211_start before the
347b032f27cSSam Leffler 		 * vap entered RUN state in which case it's also ok to
348b032f27cSSam Leffler 		 * just drop the frame.  This should not be necessary
349b032f27cSSam Leffler 		 * but callers of if_output don't check OACTIVE.
350b032f27cSSam Leffler 		 */
351b032f27cSSam Leffler 		senderr(ENETDOWN);
352b032f27cSSam Leffler 	}
353b032f27cSSam Leffler 	vap = ifp->if_softc;
354b032f27cSSam Leffler 	/*
355b032f27cSSam Leffler 	 * Hand to the 802.3 code if not tagged as
356b032f27cSSam Leffler 	 * a raw 802.11 frame.
357b032f27cSSam Leffler 	 */
358b032f27cSSam Leffler 	if (dst->sa_family != AF_IEEE80211)
359279aa3d4SKip Macy 		return vap->iv_output(ifp, m, dst, ro);
360b032f27cSSam Leffler #ifdef MAC
361b032f27cSSam Leffler 	error = mac_check_ifnet_transmit(ifp, m);
362b032f27cSSam Leffler 	if (error)
363b032f27cSSam Leffler 		senderr(error);
364b032f27cSSam Leffler #endif
365b032f27cSSam Leffler 	if (ifp->if_flags & IFF_MONITOR)
366b032f27cSSam Leffler 		senderr(ENETDOWN);
367b032f27cSSam Leffler 	if (!IFNET_IS_UP_RUNNING(ifp))
368b032f27cSSam Leffler 		senderr(ENETDOWN);
369b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
370b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap,
371b032f27cSSam Leffler 		    IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
372b032f27cSSam Leffler 		    "block %s frame in CAC state\n", "raw data");
373b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
374b032f27cSSam Leffler 		senderr(EIO);		/* XXX */
375b032f27cSSam Leffler 	}
376b032f27cSSam Leffler 	/* XXX bypass bridge, pfil, carp, etc. */
377b032f27cSSam Leffler 
378b032f27cSSam Leffler 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
379b032f27cSSam Leffler 		senderr(EIO);	/* XXX */
380b032f27cSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
381b032f27cSSam Leffler 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
382b032f27cSSam Leffler 	    IEEE80211_FC0_VERSION_0)
383b032f27cSSam Leffler 		senderr(EIO);	/* XXX */
384b032f27cSSam Leffler 
385b032f27cSSam Leffler 	/* locate destination node */
386b032f27cSSam Leffler 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
387b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_NODS:
388b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_FROMDS:
389b032f27cSSam Leffler 		ni = ieee80211_find_txnode(vap, wh->i_addr1);
390b032f27cSSam Leffler 		break;
391b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_TODS:
392b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_DSTODS:
393b032f27cSSam Leffler 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame))
394b032f27cSSam Leffler 			senderr(EIO);	/* XXX */
395b032f27cSSam Leffler 		ni = ieee80211_find_txnode(vap, wh->i_addr3);
396b032f27cSSam Leffler 		break;
397b032f27cSSam Leffler 	default:
398b032f27cSSam Leffler 		senderr(EIO);	/* XXX */
399b032f27cSSam Leffler 	}
400b032f27cSSam Leffler 	if (ni == NULL) {
401b032f27cSSam Leffler 		/*
402b032f27cSSam Leffler 		 * Permit packets w/ bpf params through regardless
403b032f27cSSam Leffler 		 * (see below about sa_len).
404b032f27cSSam Leffler 		 */
405b032f27cSSam Leffler 		if (dst->sa_len == 0)
406b032f27cSSam Leffler 			senderr(EHOSTUNREACH);
407b032f27cSSam Leffler 		ni = ieee80211_ref_node(vap->iv_bss);
408b032f27cSSam Leffler 	}
409b032f27cSSam Leffler 
410b032f27cSSam Leffler 	/*
411b032f27cSSam Leffler 	 * Sanitize mbuf for net80211 flags leaked from above.
412b032f27cSSam Leffler 	 *
413b032f27cSSam Leffler 	 * NB: This must be done before ieee80211_classify as
414b032f27cSSam Leffler 	 *     it marks EAPOL in frames with M_EAPOL.
415b032f27cSSam Leffler 	 */
416b032f27cSSam Leffler 	m->m_flags &= ~M_80211_TX;
417b032f27cSSam Leffler 
418b032f27cSSam Leffler 	/* calculate priority so drivers can find the tx queue */
419b032f27cSSam Leffler 	/* XXX assumes an 802.3 frame */
420b032f27cSSam Leffler 	if (ieee80211_classify(ni, m))
421b032f27cSSam Leffler 		senderr(EIO);		/* XXX */
422b032f27cSSam Leffler 
4230e910c94SSam Leffler 	if (bpf_peers_present(vap->iv_rawbpf))
4240e910c94SSam Leffler 		bpf_mtap(vap->iv_rawbpf, m);
4250e910c94SSam Leffler 
4260e910c94SSam Leffler 	IEEE80211_NODE_STAT(ni, tx_data);
4270e910c94SSam Leffler 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
4280e910c94SSam Leffler 		IEEE80211_NODE_STAT(ni, tx_mcast);
4290e910c94SSam Leffler 		m->m_flags |= M_MCAST;
4300e910c94SSam Leffler 	} else
4310e910c94SSam Leffler 		IEEE80211_NODE_STAT(ni, tx_ucast);
4320e910c94SSam Leffler 	/* NB: ieee80211_encap does not include 802.11 header */
4330e910c94SSam Leffler 	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, m->m_pkthdr.len);
434b032f27cSSam Leffler 
435b032f27cSSam Leffler 	/*
436b032f27cSSam Leffler 	 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
437b032f27cSSam Leffler 	 * present by setting the sa_len field of the sockaddr (yes,
438b032f27cSSam Leffler 	 * this is a hack).
439b032f27cSSam Leffler 	 * NB: we assume sa_data is suitably aligned to cast.
440b032f27cSSam Leffler 	 */
441b032f27cSSam Leffler 	return vap->iv_ic->ic_raw_xmit(ni, m,
442b032f27cSSam Leffler 	    (const struct ieee80211_bpf_params *)(dst->sa_len ?
443b032f27cSSam Leffler 		dst->sa_data : NULL));
444b032f27cSSam Leffler bad:
445b032f27cSSam Leffler 	if (m != NULL)
446b032f27cSSam Leffler 		m_freem(m);
447b032f27cSSam Leffler 	if (ni != NULL)
448b032f27cSSam Leffler 		ieee80211_free_node(ni);
449b032f27cSSam Leffler 	return error;
450b032f27cSSam Leffler #undef senderr
451b032f27cSSam Leffler }
452b032f27cSSam Leffler 
453b032f27cSSam Leffler /*
454add59d08SSam Leffler  * Set the direction field and address fields of an outgoing
4550e66722dSSam Leffler  * frame.  Note this should be called early on in constructing
4560e66722dSSam Leffler  * a frame as it sets i_fc[1]; other bits can then be or'd in.
457add59d08SSam Leffler  */
458add59d08SSam Leffler static void
459b032f27cSSam Leffler ieee80211_send_setup(
460add59d08SSam Leffler 	struct ieee80211_node *ni,
4619e80b1dfSSam Leffler 	struct mbuf *m,
4628ac160cdSSam Leffler 	int type, int tid,
46368e8e04eSSam Leffler 	const uint8_t sa[IEEE80211_ADDR_LEN],
46468e8e04eSSam Leffler 	const uint8_t da[IEEE80211_ADDR_LEN],
46568e8e04eSSam Leffler 	const uint8_t bssid[IEEE80211_ADDR_LEN])
466add59d08SSam Leffler {
467add59d08SSam Leffler #define	WH4(wh)	((struct ieee80211_frame_addr4 *)wh)
4689e80b1dfSSam Leffler 	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
4699e80b1dfSSam Leffler 	ieee80211_seq seqno;
470add59d08SSam Leffler 
471add59d08SSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
472add59d08SSam Leffler 	if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
473b032f27cSSam Leffler 		struct ieee80211vap *vap = ni->ni_vap;
474b032f27cSSam Leffler 
475b032f27cSSam Leffler 		switch (vap->iv_opmode) {
476add59d08SSam Leffler 		case IEEE80211_M_STA:
477add59d08SSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
478add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
479add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
480add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, da);
481add59d08SSam Leffler 			break;
482add59d08SSam Leffler 		case IEEE80211_M_IBSS:
483add59d08SSam Leffler 		case IEEE80211_M_AHDEMO:
484add59d08SSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
485add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
486add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
487add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
488add59d08SSam Leffler 			break;
489add59d08SSam Leffler 		case IEEE80211_M_HOSTAP:
490add59d08SSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
491add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
492add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
493add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, sa);
494add59d08SSam Leffler 			break;
49568e8e04eSSam Leffler 		case IEEE80211_M_WDS:
49668e8e04eSSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
497b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
498b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
49968e8e04eSSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, da);
50068e8e04eSSam Leffler 			IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
50168e8e04eSSam Leffler 			break;
502add59d08SSam Leffler 		case IEEE80211_M_MONITOR:	/* NB: to quiet compiler */
503add59d08SSam Leffler 			break;
504add59d08SSam Leffler 		}
505add59d08SSam Leffler 	} else {
506add59d08SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
507add59d08SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, da);
508add59d08SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, sa);
509add59d08SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
510add59d08SSam Leffler 	}
51168e8e04eSSam Leffler 	*(uint16_t *)&wh->i_dur[0] = 0;
5129e80b1dfSSam Leffler 
5139e80b1dfSSam Leffler 	seqno = ni->ni_txseqs[tid]++;
5149e80b1dfSSam Leffler 	*(uint16_t *)&wh->i_seq[0] = htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
5159e80b1dfSSam Leffler 
5169e80b1dfSSam Leffler 	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
5179e80b1dfSSam Leffler 		m->m_flags |= M_MCAST;
518add59d08SSam Leffler #undef WH4
519add59d08SSam Leffler }
520add59d08SSam Leffler 
521add59d08SSam Leffler /*
5220a915fadSSam Leffler  * Send a management frame to the specified node.  The node pointer
5230a915fadSSam Leffler  * must have a reference as the pointer will be passed to the driver
5240a915fadSSam Leffler  * and potentially held for a long time.  If the frame is successfully
5250a915fadSSam Leffler  * dispatched to the driver, then it is responsible for freeing the
526b032f27cSSam Leffler  * reference (and potentially free'ing up any associated storage);
527b032f27cSSam Leffler  * otherwise deal with reclaiming any reference (on error).
5280a915fadSSam Leffler  */
52968e8e04eSSam Leffler int
5308ac160cdSSam Leffler ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
5318ac160cdSSam Leffler 	struct ieee80211_bpf_params *params)
5321a1e1d21SSam Leffler {
533b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
534b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
5351a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
5361a1e1d21SSam Leffler 
5370a915fadSSam Leffler 	KASSERT(ni != NULL, ("null node"));
5381a1e1d21SSam Leffler 
539b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
540b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
541b032f27cSSam Leffler 		    ni, "block %s frame in CAC state",
542b032f27cSSam Leffler 			ieee80211_mgt_subtype_name[
543b032f27cSSam Leffler 			    (type & IEEE80211_FC0_SUBTYPE_MASK) >>
544b032f27cSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT]);
545b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
546b032f27cSSam Leffler 		ieee80211_free_node(ni);
547b032f27cSSam Leffler 		m_freem(m);
548b032f27cSSam Leffler 		return EIO;		/* XXX */
549b032f27cSSam Leffler 	}
550b032f27cSSam Leffler 
5511a1e1d21SSam Leffler 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
552b032f27cSSam Leffler 	if (m == NULL) {
553b032f27cSSam Leffler 		ieee80211_free_node(ni);
5541a1e1d21SSam Leffler 		return ENOMEM;
555b032f27cSSam Leffler 	}
5560a915fadSSam Leffler 
5571a1e1d21SSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
5589e80b1dfSSam Leffler 	ieee80211_send_setup(ni, m,
5598ac160cdSSam Leffler 	     IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
560b032f27cSSam Leffler 	     vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
5618ac160cdSSam Leffler 	if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
562b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
563b032f27cSSam Leffler 		    "encrypting frame (%s)", __func__);
5648a1b9b6aSSam Leffler 		wh->i_fc[1] |= IEEE80211_FC1_WEP;
5658a1b9b6aSSam Leffler 	}
566c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
5678ac160cdSSam Leffler 
5688ac160cdSSam Leffler 	KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
5698ac160cdSSam Leffler 	M_WME_SETAC(m, params->ibp_pri);
5708ac160cdSSam Leffler 
5718a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
5728a1b9b6aSSam Leffler 	/* avoid printing too many frames */
573b032f27cSSam Leffler 	if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
574b032f27cSSam Leffler 	    ieee80211_msg_dumppkts(vap)) {
5758a1b9b6aSSam Leffler 		printf("[%s] send %s on channel %u\n",
5768a1b9b6aSSam Leffler 		    ether_sprintf(wh->i_addr1),
5778a1b9b6aSSam Leffler 		    ieee80211_mgt_subtype_name[
5788a1b9b6aSSam Leffler 			(type & IEEE80211_FC0_SUBTYPE_MASK) >>
5798a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
580b5c99415SSam Leffler 		    ieee80211_chan2ieee(ic, ic->ic_curchan));
5818a1b9b6aSSam Leffler 	}
5828a1b9b6aSSam Leffler #endif
5838a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_mgmt);
58468e8e04eSSam Leffler 
5858ac160cdSSam Leffler 	return ic->ic_raw_xmit(ni, m, params);
586246b5467SSam Leffler }
587246b5467SSam Leffler 
588246b5467SSam Leffler /*
5896683931eSSam Leffler  * Send a null data frame to the specified node.  If the station
5906683931eSSam Leffler  * is setup for QoS then a QoS Null Data frame is constructed.
5916683931eSSam Leffler  * If this is a WDS station then a 4-address frame is constructed.
59219ad2dd7SSam Leffler  *
59319ad2dd7SSam Leffler  * NB: the caller is assumed to have setup a node reference
59419ad2dd7SSam Leffler  *     for use; this is necessary to deal with a race condition
595b032f27cSSam Leffler  *     when probing for inactive stations.  Like ieee80211_mgmt_output
596b032f27cSSam Leffler  *     we must cleanup any node reference on error;  however we
597b032f27cSSam Leffler  *     can safely just unref it as we know it will never be the
598b032f27cSSam Leffler  *     last reference to the node.
5998a1b9b6aSSam Leffler  */
6008a1b9b6aSSam Leffler int
601f62121ceSSam Leffler ieee80211_send_nulldata(struct ieee80211_node *ni)
6028a1b9b6aSSam Leffler {
603b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
604f62121ceSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
6058a1b9b6aSSam Leffler 	struct mbuf *m;
6068a1b9b6aSSam Leffler 	struct ieee80211_frame *wh;
6076683931eSSam Leffler 	int hdrlen;
6086683931eSSam Leffler 	uint8_t *frm;
6098a1b9b6aSSam Leffler 
610b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
611b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
612b032f27cSSam Leffler 		    ni, "block %s frame in CAC state", "null data");
613b032f27cSSam Leffler 		ieee80211_unref_node(&ni);
614b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
615b032f27cSSam Leffler 		return EIO;		/* XXX */
616b032f27cSSam Leffler 	}
617b032f27cSSam Leffler 
6186683931eSSam Leffler 	if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
6196683931eSSam Leffler 		hdrlen = sizeof(struct ieee80211_qosframe);
6206683931eSSam Leffler 	else
6216683931eSSam Leffler 		hdrlen = sizeof(struct ieee80211_frame);
6226683931eSSam Leffler 	/* NB: only WDS vap's get 4-address frames */
6236683931eSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_WDS)
6246683931eSSam Leffler 		hdrlen += IEEE80211_ADDR_LEN;
6256683931eSSam Leffler 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
6266683931eSSam Leffler 		hdrlen = roundup(hdrlen, sizeof(uint32_t));
6276683931eSSam Leffler 
6286683931eSSam Leffler 	m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
6298a1b9b6aSSam Leffler 	if (m == NULL) {
6308a1b9b6aSSam Leffler 		/* XXX debug msg */
63119ad2dd7SSam Leffler 		ieee80211_unref_node(&ni);
632b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
6338a1b9b6aSSam Leffler 		return ENOMEM;
6348a1b9b6aSSam Leffler 	}
6356683931eSSam Leffler 	KASSERT(M_LEADINGSPACE(m) >= hdrlen,
6366683931eSSam Leffler 	    ("leading space %zd", M_LEADINGSPACE(m)));
6376683931eSSam Leffler 	M_PREPEND(m, hdrlen, M_DONTWAIT);
6386683931eSSam Leffler 	if (m == NULL) {
6396683931eSSam Leffler 		/* NB: cannot happen */
6406683931eSSam Leffler 		ieee80211_free_node(ni);
6416683931eSSam Leffler 		return ENOMEM;
6426683931eSSam Leffler 	}
6438a1b9b6aSSam Leffler 
6446683931eSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);		/* NB: a little lie */
6456683931eSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
6466683931eSSam Leffler 		const int tid = WME_AC_TO_TID(WME_AC_BE);
6476683931eSSam Leffler 		uint8_t *qos;
6486683931eSSam Leffler 
6499e80b1dfSSam Leffler 		ieee80211_send_setup(ni, m,
6506683931eSSam Leffler 		    IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
6516683931eSSam Leffler 		    tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
6526683931eSSam Leffler 
6536683931eSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_WDS)
6546683931eSSam Leffler 			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
6556683931eSSam Leffler 		else
6566683931eSSam Leffler 			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
6576683931eSSam Leffler 		qos[0] = tid & IEEE80211_QOS_TID;
6586683931eSSam Leffler 		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
6596683931eSSam Leffler 			qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
6606683931eSSam Leffler 		qos[1] = 0;
6616683931eSSam Leffler 	} else {
6629e80b1dfSSam Leffler 		ieee80211_send_setup(ni, m,
663add59d08SSam Leffler 		    IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
6648ac160cdSSam Leffler 		    IEEE80211_NONQOS_TID,
665b032f27cSSam Leffler 		    vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
6666683931eSSam Leffler 	}
667b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_WDS) {
668add59d08SSam Leffler 		/* NB: power management bit is never sent by an AP */
669add59d08SSam Leffler 		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
670b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_HOSTAP)
671add59d08SSam Leffler 			wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
672b032f27cSSam Leffler 	}
6736683931eSSam Leffler 	m->m_len = m->m_pkthdr.len = hdrlen;
674c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
6756683931eSSam Leffler 
676bb239ce9SSam Leffler 	M_WME_SETAC(m, WME_AC_BE);
6778a1b9b6aSSam Leffler 
6788a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_data);
6798a1b9b6aSSam Leffler 
680b032f27cSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
6816683931eSSam Leffler 	    "send %snull data frame on channel %u, pwr mgt %s",
6826683931eSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
683b5c99415SSam Leffler 	    ieee80211_chan2ieee(ic, ic->ic_curchan),
684add59d08SSam Leffler 	    wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
685add59d08SSam Leffler 
686b032f27cSSam Leffler 	return ic->ic_raw_xmit(ni, m, NULL);
6878a1b9b6aSSam Leffler }
6888a1b9b6aSSam Leffler 
6898a1b9b6aSSam Leffler /*
6908a1b9b6aSSam Leffler  * Assign priority to a frame based on any vlan tag assigned
6918a1b9b6aSSam Leffler  * to the station and/or any Diffserv setting in an IP header.
6928a1b9b6aSSam Leffler  * Finally, if an ACM policy is setup (in station mode) it's
6938a1b9b6aSSam Leffler  * applied.
6948a1b9b6aSSam Leffler  */
6958a1b9b6aSSam Leffler int
696b032f27cSSam Leffler ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
6978a1b9b6aSSam Leffler {
698b032f27cSSam Leffler 	const struct ether_header *eh = mtod(m, struct ether_header *);
6998a1b9b6aSSam Leffler 	int v_wme_ac, d_wme_ac, ac;
7008a1b9b6aSSam Leffler 
701b032f27cSSam Leffler 	/*
702b032f27cSSam Leffler 	 * Always promote PAE/EAPOL frames to high priority.
703b032f27cSSam Leffler 	 */
704b032f27cSSam Leffler 	if (eh->ether_type == htons(ETHERTYPE_PAE)) {
705b032f27cSSam Leffler 		/* NB: mark so others don't need to check header */
706b032f27cSSam Leffler 		m->m_flags |= M_EAPOL;
707b032f27cSSam Leffler 		ac = WME_AC_VO;
708b032f27cSSam Leffler 		goto done;
709b032f27cSSam Leffler 	}
710b032f27cSSam Leffler 	/*
711b032f27cSSam Leffler 	 * Non-qos traffic goes to BE.
712b032f27cSSam Leffler 	 */
7138a1b9b6aSSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
7148a1b9b6aSSam Leffler 		ac = WME_AC_BE;
7158a1b9b6aSSam Leffler 		goto done;
7168a1b9b6aSSam Leffler 	}
7178a1b9b6aSSam Leffler 
7188a1b9b6aSSam Leffler 	/*
7198a1b9b6aSSam Leffler 	 * If node has a vlan tag then all traffic
7208a1b9b6aSSam Leffler 	 * to it must have a matching tag.
7218a1b9b6aSSam Leffler 	 */
7228a1b9b6aSSam Leffler 	v_wme_ac = 0;
7238a1b9b6aSSam Leffler 	if (ni->ni_vlan != 0) {
72478ba57b9SAndre Oppermann 		 if ((m->m_flags & M_VLANTAG) == 0) {
7258a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_novlantag);
7268a1b9b6aSSam Leffler 			return 1;
7278a1b9b6aSSam Leffler 		}
72878ba57b9SAndre Oppermann 		if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
7298a1b9b6aSSam Leffler 		    EVL_VLANOFTAG(ni->ni_vlan)) {
7308a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
7318a1b9b6aSSam Leffler 			return 1;
7328a1b9b6aSSam Leffler 		}
7338a1b9b6aSSam Leffler 		/* map vlan priority to AC */
734f4558c9aSSam Leffler 		v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
7358a1b9b6aSSam Leffler 	}
7368a1b9b6aSSam Leffler 
7378a1b9b6aSSam Leffler #ifdef INET
7388a1b9b6aSSam Leffler 	if (eh->ether_type == htons(ETHERTYPE_IP)) {
739f4558c9aSSam Leffler 		uint8_t tos;
7408a1b9b6aSSam Leffler 		/*
741f4558c9aSSam Leffler 		 * IP frame, map the DSCP bits from the TOS field.
7428a1b9b6aSSam Leffler 		 */
743f4558c9aSSam Leffler 		/* XXX m_copydata may be too slow for fast path */
744f4558c9aSSam Leffler 		/* NB: ip header may not be in first mbuf */
745f4558c9aSSam Leffler 		m_copydata(m, sizeof(struct ether_header) +
746f4558c9aSSam Leffler 		    offsetof(struct ip, ip_tos), sizeof(tos), &tos);
747f4558c9aSSam Leffler 		tos >>= 5;		/* NB: ECN + low 3 bits of DSCP */
748f4558c9aSSam Leffler 		d_wme_ac = TID_TO_WME_AC(tos);
7498a1b9b6aSSam Leffler 	} else {
7508a1b9b6aSSam Leffler #endif /* INET */
7518a1b9b6aSSam Leffler 		d_wme_ac = WME_AC_BE;
7528a1b9b6aSSam Leffler #ifdef INET
7538a1b9b6aSSam Leffler 	}
7548a1b9b6aSSam Leffler #endif
7558a1b9b6aSSam Leffler 	/*
7568a1b9b6aSSam Leffler 	 * Use highest priority AC.
7578a1b9b6aSSam Leffler 	 */
7588a1b9b6aSSam Leffler 	if (v_wme_ac > d_wme_ac)
7598a1b9b6aSSam Leffler 		ac = v_wme_ac;
7608a1b9b6aSSam Leffler 	else
7618a1b9b6aSSam Leffler 		ac = d_wme_ac;
7628a1b9b6aSSam Leffler 
7638a1b9b6aSSam Leffler 	/*
7648a1b9b6aSSam Leffler 	 * Apply ACM policy.
7658a1b9b6aSSam Leffler 	 */
766b032f27cSSam Leffler 	if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
7678a1b9b6aSSam Leffler 		static const int acmap[4] = {
7688a1b9b6aSSam Leffler 			WME_AC_BK,	/* WME_AC_BE */
7698a1b9b6aSSam Leffler 			WME_AC_BK,	/* WME_AC_BK */
7708a1b9b6aSSam Leffler 			WME_AC_BE,	/* WME_AC_VI */
7718a1b9b6aSSam Leffler 			WME_AC_VI,	/* WME_AC_VO */
7728a1b9b6aSSam Leffler 		};
773b032f27cSSam Leffler 		struct ieee80211com *ic = ni->ni_ic;
774b032f27cSSam Leffler 
7758a1b9b6aSSam Leffler 		while (ac != WME_AC_BK &&
7768a1b9b6aSSam Leffler 		    ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
7778a1b9b6aSSam Leffler 			ac = acmap[ac];
7788a1b9b6aSSam Leffler 	}
7798a1b9b6aSSam Leffler done:
7808a1b9b6aSSam Leffler 	M_WME_SETAC(m, ac);
7818a1b9b6aSSam Leffler 	return 0;
7828a1b9b6aSSam Leffler }
7838a1b9b6aSSam Leffler 
7848a1b9b6aSSam Leffler /*
7855e923d2eSSam Leffler  * Insure there is sufficient contiguous space to encapsulate the
7865e923d2eSSam Leffler  * 802.11 data frame.  If room isn't already there, arrange for it.
7875e923d2eSSam Leffler  * Drivers and cipher modules assume we have done the necessary work
7885e923d2eSSam Leffler  * and fail rudely if they don't find the space they need.
7895e923d2eSSam Leffler  */
790616190d0SSam Leffler struct mbuf *
791b032f27cSSam Leffler ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
7925e923d2eSSam Leffler 	struct ieee80211_key *key, struct mbuf *m)
7935e923d2eSSam Leffler {
794ab96db10SSam Leffler #define	TO_BE_RECLAIMED	(sizeof(struct ether_header) - sizeof(struct llc))
795b032f27cSSam Leffler 	int needed_space = vap->iv_ic->ic_headroom + hdrsize;
7965e923d2eSSam Leffler 
7975e923d2eSSam Leffler 	if (key != NULL) {
7985e923d2eSSam Leffler 		/* XXX belongs in crypto code? */
7995e923d2eSSam Leffler 		needed_space += key->wk_cipher->ic_header;
8005e923d2eSSam Leffler 		/* XXX frags */
80183a244dbSSam Leffler 		/*
80283a244dbSSam Leffler 		 * When crypto is being done in the host we must insure
80383a244dbSSam Leffler 		 * the data are writable for the cipher routines; clone
80483a244dbSSam Leffler 		 * a writable mbuf chain.
80583a244dbSSam Leffler 		 * XXX handle SWMIC specially
80683a244dbSSam Leffler 		 */
8075c1f7f19SSam Leffler 		if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
80883a244dbSSam Leffler 			m = m_unshare(m, M_NOWAIT);
80983a244dbSSam Leffler 			if (m == NULL) {
810b032f27cSSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
81183a244dbSSam Leffler 				    "%s: cannot get writable mbuf\n", __func__);
812b032f27cSSam Leffler 				vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
81383a244dbSSam Leffler 				return NULL;
81483a244dbSSam Leffler 			}
81583a244dbSSam Leffler 		}
8165e923d2eSSam Leffler 	}
8175e923d2eSSam Leffler 	/*
8185e923d2eSSam Leffler 	 * We know we are called just before stripping an Ethernet
8195e923d2eSSam Leffler 	 * header and prepending an LLC header.  This means we know
8205e923d2eSSam Leffler 	 * there will be
821ab96db10SSam Leffler 	 *	sizeof(struct ether_header) - sizeof(struct llc)
8225e923d2eSSam Leffler 	 * bytes recovered to which we need additional space for the
8235e923d2eSSam Leffler 	 * 802.11 header and any crypto header.
8245e923d2eSSam Leffler 	 */
8255e923d2eSSam Leffler 	/* XXX check trailing space and copy instead? */
8265e923d2eSSam Leffler 	if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
8275e923d2eSSam Leffler 		struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
8285e923d2eSSam Leffler 		if (n == NULL) {
829b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
8305e923d2eSSam Leffler 			    "%s: cannot expand storage\n", __func__);
831b032f27cSSam Leffler 			vap->iv_stats.is_tx_nobuf++;
8325e923d2eSSam Leffler 			m_freem(m);
8335e923d2eSSam Leffler 			return NULL;
8345e923d2eSSam Leffler 		}
8355e923d2eSSam Leffler 		KASSERT(needed_space <= MHLEN,
8365e923d2eSSam Leffler 		    ("not enough room, need %u got %zu\n", needed_space, MHLEN));
8375e923d2eSSam Leffler 		/*
8385e923d2eSSam Leffler 		 * Setup new mbuf to have leading space to prepend the
8395e923d2eSSam Leffler 		 * 802.11 header and any crypto header bits that are
8405e923d2eSSam Leffler 		 * required (the latter are added when the driver calls
8415e923d2eSSam Leffler 		 * back to ieee80211_crypto_encap to do crypto encapsulation).
8425e923d2eSSam Leffler 		 */
8435e923d2eSSam Leffler 		/* NB: must be first 'cuz it clobbers m_data */
8445e923d2eSSam Leffler 		m_move_pkthdr(n, m);
8455e923d2eSSam Leffler 		n->m_len = 0;			/* NB: m_gethdr does not set */
8465e923d2eSSam Leffler 		n->m_data += needed_space;
8475e923d2eSSam Leffler 		/*
8485e923d2eSSam Leffler 		 * Pull up Ethernet header to create the expected layout.
8495e923d2eSSam Leffler 		 * We could use m_pullup but that's overkill (i.e. we don't
8505e923d2eSSam Leffler 		 * need the actual data) and it cannot fail so do it inline
8515e923d2eSSam Leffler 		 * for speed.
8525e923d2eSSam Leffler 		 */
8535e923d2eSSam Leffler 		/* NB: struct ether_header is known to be contiguous */
8545e923d2eSSam Leffler 		n->m_len += sizeof(struct ether_header);
8555e923d2eSSam Leffler 		m->m_len -= sizeof(struct ether_header);
8565e923d2eSSam Leffler 		m->m_data += sizeof(struct ether_header);
8575e923d2eSSam Leffler 		/*
8585e923d2eSSam Leffler 		 * Replace the head of the chain.
8595e923d2eSSam Leffler 		 */
8605e923d2eSSam Leffler 		n->m_next = m;
8615e923d2eSSam Leffler 		m = n;
8625e923d2eSSam Leffler 	}
8635e923d2eSSam Leffler 	return m;
8645e923d2eSSam Leffler #undef TO_BE_RECLAIMED
8655e923d2eSSam Leffler }
8665e923d2eSSam Leffler 
8675e923d2eSSam Leffler /*
8685e923d2eSSam Leffler  * Return the transmit key to use in sending a unicast frame.
8695e923d2eSSam Leffler  * If a unicast key is set we use that.  When no unicast key is set
8705e923d2eSSam Leffler  * we fall back to the default transmit key.
8718a1b9b6aSSam Leffler  */
8728a1b9b6aSSam Leffler static __inline struct ieee80211_key *
873b032f27cSSam Leffler ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
874b032f27cSSam Leffler 	struct ieee80211_node *ni)
8758a1b9b6aSSam Leffler {
876cda15ce1SSam Leffler 	if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
877b032f27cSSam Leffler 		if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
878b032f27cSSam Leffler 		    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
8798a1b9b6aSSam Leffler 			return NULL;
880b032f27cSSam Leffler 		return &vap->iv_nw_keys[vap->iv_def_txkey];
8818a1b9b6aSSam Leffler 	} else {
8828a1b9b6aSSam Leffler 		return &ni->ni_ucastkey;
8838a1b9b6aSSam Leffler 	}
8845e923d2eSSam Leffler }
8855e923d2eSSam Leffler 
8865e923d2eSSam Leffler /*
8875e923d2eSSam Leffler  * Return the transmit key to use in sending a multicast frame.
8885e923d2eSSam Leffler  * Multicast traffic always uses the group key which is installed as
8895e923d2eSSam Leffler  * the default tx key.
8905e923d2eSSam Leffler  */
8915e923d2eSSam Leffler static __inline struct ieee80211_key *
892b032f27cSSam Leffler ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
893b032f27cSSam Leffler 	struct ieee80211_node *ni)
8945e923d2eSSam Leffler {
895b032f27cSSam Leffler 	if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
896b032f27cSSam Leffler 	    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
8975e923d2eSSam Leffler 		return NULL;
898b032f27cSSam Leffler 	return &vap->iv_nw_keys[vap->iv_def_txkey];
8998a1b9b6aSSam Leffler }
9008a1b9b6aSSam Leffler 
9018a1b9b6aSSam Leffler /*
9028a1b9b6aSSam Leffler  * Encapsulate an outbound data frame.  The mbuf chain is updated.
9038a1b9b6aSSam Leffler  * If an error is encountered NULL is returned.  The caller is required
9048a1b9b6aSSam Leffler  * to provide a node reference and pullup the ethernet header in the
9058a1b9b6aSSam Leffler  * first mbuf.
906b032f27cSSam Leffler  *
907b032f27cSSam Leffler  * NB: Packet is assumed to be processed by ieee80211_classify which
908b032f27cSSam Leffler  *     marked EAPOL frames w/ M_EAPOL.
9090a915fadSSam Leffler  */
9101a1e1d21SSam Leffler struct mbuf *
911339ccfb3SSam Leffler ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
912339ccfb3SSam Leffler     struct mbuf *m)
9131a1e1d21SSam Leffler {
914b032f27cSSam Leffler #define	WH4(wh)	((struct ieee80211_frame_addr4 *)(wh))
915b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
9161a1e1d21SSam Leffler 	struct ether_header eh;
9171a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
9188a1b9b6aSSam Leffler 	struct ieee80211_key *key;
9191a1e1d21SSam Leffler 	struct llc *llc;
920616190d0SSam Leffler 	int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr;
9219e80b1dfSSam Leffler 	ieee80211_seq seqno;
9221a1e1d21SSam Leffler 
92368e8e04eSSam Leffler 	/*
92468e8e04eSSam Leffler 	 * Copy existing Ethernet header to a safe place.  The
92568e8e04eSSam Leffler 	 * rest of the code assumes it's ok to strip it when
92668e8e04eSSam Leffler 	 * reorganizing state for the final encapsulation.
92768e8e04eSSam Leffler 	 */
9288a1b9b6aSSam Leffler 	KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
929b032f27cSSam Leffler 	ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
9301a1e1d21SSam Leffler 
9318a1b9b6aSSam Leffler 	/*
9328a1b9b6aSSam Leffler 	 * Insure space for additional headers.  First identify
9338a1b9b6aSSam Leffler 	 * transmit key to use in calculating any buffer adjustments
9348a1b9b6aSSam Leffler 	 * required.  This is also used below to do privacy
9358a1b9b6aSSam Leffler 	 * encapsulation work.  Then calculate the 802.11 header
9368a1b9b6aSSam Leffler 	 * size and any padding required by the driver.
9378a1b9b6aSSam Leffler 	 *
9388a1b9b6aSSam Leffler 	 * Note key may be NULL if we fall back to the default
9398a1b9b6aSSam Leffler 	 * transmit key and that is not set.  In that case the
9408a1b9b6aSSam Leffler 	 * buffer may not be expanded as needed by the cipher
9418a1b9b6aSSam Leffler 	 * routines, but they will/should discard it.
9428a1b9b6aSSam Leffler 	 */
943b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
944b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA ||
945b032f27cSSam Leffler 		    !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
946b032f27cSSam Leffler 		    (vap->iv_opmode == IEEE80211_M_WDS &&
947b032f27cSSam Leffler 		     (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
948b032f27cSSam Leffler 			key = ieee80211_crypto_getucastkey(vap, ni);
9495e923d2eSSam Leffler 		else
950b032f27cSSam Leffler 			key = ieee80211_crypto_getmcastkey(vap, ni);
951b032f27cSSam Leffler 		if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
952b032f27cSSam Leffler 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
953b032f27cSSam Leffler 			    eh.ether_dhost,
954b032f27cSSam Leffler 			    "no default transmit key (%s) deftxkey %u",
955b032f27cSSam Leffler 			    __func__, vap->iv_def_txkey);
956b032f27cSSam Leffler 			vap->iv_stats.is_tx_nodefkey++;
957d72c7253SSam Leffler 			goto bad;
9588a1b9b6aSSam Leffler 		}
9598a1b9b6aSSam Leffler 	} else
9608a1b9b6aSSam Leffler 		key = NULL;
9615e923d2eSSam Leffler 	/*
9625e923d2eSSam Leffler 	 * XXX Some ap's don't handle QoS-encapsulated EAPOL
9635e923d2eSSam Leffler 	 * frames so suppress use.  This may be an issue if other
9645e923d2eSSam Leffler 	 * ap's require all data frames to be QoS-encapsulated
9655e923d2eSSam Leffler 	 * once negotiated in which case we'll need to make this
9665e923d2eSSam Leffler 	 * configurable.
9675e923d2eSSam Leffler 	 */
96868e8e04eSSam Leffler 	addqos = (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) &&
969b032f27cSSam Leffler 		 (m->m_flags & M_EAPOL) == 0;
9705e923d2eSSam Leffler 	if (addqos)
9718a1b9b6aSSam Leffler 		hdrsize = sizeof(struct ieee80211_qosframe);
9728a1b9b6aSSam Leffler 	else
9738a1b9b6aSSam Leffler 		hdrsize = sizeof(struct ieee80211_frame);
974b032f27cSSam Leffler 	/*
975b032f27cSSam Leffler 	 * 4-address frames need to be generated for:
9766437e6daSSam Leffler 	 * o packets sent through a WDS vap (IEEE80211_M_WDS)
977f2a6a13cSSam Leffler 	 * o packets sent through a vap marked for relaying
978f2a6a13cSSam Leffler 	 *   (e.g. a station operating with dynamic WDS)
979b032f27cSSam Leffler 	 */
9806437e6daSSam Leffler 	is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
981f2a6a13cSSam Leffler 	    ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
982b032f27cSSam Leffler 	     !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
983b032f27cSSam Leffler 	if (is4addr)
984b032f27cSSam Leffler 		hdrsize += IEEE80211_ADDR_LEN;
985b032f27cSSam Leffler 	/*
986b032f27cSSam Leffler 	 * Honor driver DATAPAD requirement.
987b032f27cSSam Leffler 	 */
9888a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
989b032f27cSSam Leffler 		hdrspace = roundup(hdrsize, sizeof(uint32_t));
990b032f27cSSam Leffler 	else
991b032f27cSSam Leffler 		hdrspace = hdrsize;
99268e8e04eSSam Leffler 
993616190d0SSam Leffler 	if (__predict_true((m->m_flags & M_FF) == 0)) {
99468e8e04eSSam Leffler 		/*
99568e8e04eSSam Leffler 		 * Normal frame.
99668e8e04eSSam Leffler 		 */
997b032f27cSSam Leffler 		m = ieee80211_mbuf_adjust(vap, hdrspace, key, m);
9988a1b9b6aSSam Leffler 		if (m == NULL) {
9998a1b9b6aSSam Leffler 			/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
10000a915fadSSam Leffler 			goto bad;
10010a915fadSSam Leffler 		}
100268e8e04eSSam Leffler 		/* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
1003ab96db10SSam Leffler 		m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
10041a1e1d21SSam Leffler 		llc = mtod(m, struct llc *);
10051a1e1d21SSam Leffler 		llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
10061a1e1d21SSam Leffler 		llc->llc_control = LLC_UI;
10071a1e1d21SSam Leffler 		llc->llc_snap.org_code[0] = 0;
10081a1e1d21SSam Leffler 		llc->llc_snap.org_code[1] = 0;
10091a1e1d21SSam Leffler 		llc->llc_snap.org_code[2] = 0;
10101a1e1d21SSam Leffler 		llc->llc_snap.ether_type = eh.ether_type;
1011616190d0SSam Leffler 	} else {
1012616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1013339ccfb3SSam Leffler 		/*
1014339ccfb3SSam Leffler 		 * Aggregated frame.
1015339ccfb3SSam Leffler 		 */
1016616190d0SSam Leffler 		m = ieee80211_ff_encap(vap, m, hdrspace, key);
1017616190d0SSam Leffler 		if (m == NULL)
1018616190d0SSam Leffler #endif
1019616190d0SSam Leffler 			goto bad;
102068e8e04eSSam Leffler 	}
10218a1b9b6aSSam Leffler 	datalen = m->m_pkthdr.len;		/* NB: w/o 802.11 header */
10228a1b9b6aSSam Leffler 
1023b032f27cSSam Leffler 	M_PREPEND(m, hdrspace, M_DONTWAIT);
10241be50176SSam Leffler 	if (m == NULL) {
1025b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
10260a915fadSSam Leffler 		goto bad;
10271be50176SSam Leffler 	}
10281a1e1d21SSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
10291a1e1d21SSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
103068e8e04eSSam Leffler 	*(uint16_t *)wh->i_dur = 0;
1031b032f27cSSam Leffler 	if (is4addr) {
1032b032f27cSSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1033b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1034b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1035b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1036b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1037b032f27cSSam Leffler 	} else switch (vap->iv_opmode) {
10381a1e1d21SSam Leffler 	case IEEE80211_M_STA:
10391a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
10401a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
10411a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
10421a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
10431a1e1d21SSam Leffler 		break;
10441a1e1d21SSam Leffler 	case IEEE80211_M_IBSS:
10451a1e1d21SSam Leffler 	case IEEE80211_M_AHDEMO:
10461a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
10471a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
10481a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1049a8b16e87SSam Leffler 		/*
1050b032f27cSSam Leffler 		 * NB: always use the bssid from iv_bss as the
1051a8b16e87SSam Leffler 		 *     neighbor's may be stale after an ibss merge
1052a8b16e87SSam Leffler 		 */
1053b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
10541a1e1d21SSam Leffler 		break;
10551a1e1d21SSam Leffler 	case IEEE80211_M_HOSTAP:
10561a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
10571a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
10581a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
10591a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
10601a1e1d21SSam Leffler 		break;
10612bbe529dSSam Leffler 	case IEEE80211_M_MONITOR:
1062b032f27cSSam Leffler 	case IEEE80211_M_WDS:		/* NB: is4addr should always be true */
10630a915fadSSam Leffler 		goto bad;
10641a1e1d21SSam Leffler 	}
1065bc5627d9SSam Leffler 	if (m->m_flags & M_MORE_DATA)
1066bc5627d9SSam Leffler 		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
10675e923d2eSSam Leffler 	if (addqos) {
1068b032f27cSSam Leffler 		uint8_t *qos;
10698a1b9b6aSSam Leffler 		int ac, tid;
10708a1b9b6aSSam Leffler 
1071b032f27cSSam Leffler 		if (is4addr) {
1072b032f27cSSam Leffler 			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1073b032f27cSSam Leffler 		} else
1074b032f27cSSam Leffler 			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
10758a1b9b6aSSam Leffler 		ac = M_WME_GETAC(m);
10768a1b9b6aSSam Leffler 		/* map from access class/queue to 11e header priorty value */
10778a1b9b6aSSam Leffler 		tid = WME_AC_TO_TID(ac);
1078b032f27cSSam Leffler 		qos[0] = tid & IEEE80211_QOS_TID;
10798a1b9b6aSSam Leffler 		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
1080b032f27cSSam Leffler 			qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1081b032f27cSSam Leffler 		qos[1] = 0;
1082b032f27cSSam Leffler 		wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
10838a1b9b6aSSam Leffler 
108445f856e3SSam Leffler 		if ((m->m_flags & M_AMPDU_MPDU) == 0) {
108545f856e3SSam Leffler 			/*
108645f856e3SSam Leffler 			 * NB: don't assign a sequence # to potential
108745f856e3SSam Leffler 			 * aggregates; we expect this happens at the
108845f856e3SSam Leffler 			 * point the frame comes off any aggregation q
108945f856e3SSam Leffler 			 * as otherwise we may introduce holes in the
109045f856e3SSam Leffler 			 * BA sequence space and/or make window accouting
109145f856e3SSam Leffler 			 * more difficult.
109245f856e3SSam Leffler 			 *
109345f856e3SSam Leffler 			 * XXX may want to control this with a driver
109445f856e3SSam Leffler 			 * capability; this may also change when we pull
109545f856e3SSam Leffler 			 * aggregation up into net80211
109645f856e3SSam Leffler 			 */
10979e80b1dfSSam Leffler 			seqno = ni->ni_txseqs[tid]++;
109868e8e04eSSam Leffler 			*(uint16_t *)wh->i_seq =
10999e80b1dfSSam Leffler 			    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
110045f856e3SSam Leffler 		}
11018a1b9b6aSSam Leffler 	} else {
11029e80b1dfSSam Leffler 		seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
110368e8e04eSSam Leffler 		*(uint16_t *)wh->i_seq =
11049e80b1dfSSam Leffler 		    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
11058a1b9b6aSSam Leffler 	}
110668e8e04eSSam Leffler 	/* check if xmit fragmentation is required */
1107b032f27cSSam Leffler 	txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
110868e8e04eSSam Leffler 	    !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1109b032f27cSSam Leffler 	    (vap->iv_caps & IEEE80211_C_TXFRAG) &&
1110e9cfc1f5SSam Leffler 	    (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
11115e923d2eSSam Leffler 	if (key != NULL) {
11125e923d2eSSam Leffler 		/*
11135e923d2eSSam Leffler 		 * IEEE 802.1X: send EAPOL frames always in the clear.
11145e923d2eSSam Leffler 		 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
11155e923d2eSSam Leffler 		 */
1116b032f27cSSam Leffler 		if ((m->m_flags & M_EAPOL) == 0 ||
1117b032f27cSSam Leffler 		    ((vap->iv_flags & IEEE80211_F_WPA) &&
1118b032f27cSSam Leffler 		     (vap->iv_opmode == IEEE80211_M_STA ?
1119cda15ce1SSam Leffler 		      !IEEE80211_KEY_UNDEFINED(key) :
1120cda15ce1SSam Leffler 		      !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
11215e923d2eSSam Leffler 			wh->i_fc[1] |= IEEE80211_FC1_WEP;
1122b032f27cSSam Leffler 			if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1123b032f27cSSam Leffler 				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1124b032f27cSSam Leffler 				    eh.ether_dhost,
1125b032f27cSSam Leffler 				    "%s", "enmic failed, discard frame");
1126b032f27cSSam Leffler 				vap->iv_stats.is_crypto_enmicfail++;
11275e923d2eSSam Leffler 				goto bad;
11285e923d2eSSam Leffler 			}
11295e923d2eSSam Leffler 		}
11305e923d2eSSam Leffler 	}
1131b032f27cSSam Leffler 	if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1132b032f27cSSam Leffler 	    key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
113368e8e04eSSam Leffler 		goto bad;
11348a1b9b6aSSam Leffler 
1135c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
1136c1af44bdSSam Leffler 
11378a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_data);
11389e80b1dfSSam Leffler 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1139b6e9b119SSam Leffler 		IEEE80211_NODE_STAT(ni, tx_mcast);
11409e80b1dfSSam Leffler 		m->m_flags |= M_MCAST;
11419e80b1dfSSam Leffler 	} else
1142b6e9b119SSam Leffler 		IEEE80211_NODE_STAT(ni, tx_ucast);
11438a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
11448a1b9b6aSSam Leffler 
11451a1e1d21SSam Leffler 	return m;
11460a915fadSSam Leffler bad:
11470a915fadSSam Leffler 	if (m != NULL)
11480a915fadSSam Leffler 		m_freem(m);
11490a915fadSSam Leffler 	return NULL;
1150b032f27cSSam Leffler #undef WH4
11511a1e1d21SSam Leffler }
11521a1e1d21SSam Leffler 
11531a1e1d21SSam Leffler /*
115468e8e04eSSam Leffler  * Fragment the frame according to the specified mtu.
115568e8e04eSSam Leffler  * The size of the 802.11 header (w/o padding) is provided
115668e8e04eSSam Leffler  * so we don't need to recalculate it.  We create a new
115768e8e04eSSam Leffler  * mbuf for each fragment and chain it through m_nextpkt;
115868e8e04eSSam Leffler  * we might be able to optimize this by reusing the original
115968e8e04eSSam Leffler  * packet's mbufs but that is significantly more complicated.
116068e8e04eSSam Leffler  */
116168e8e04eSSam Leffler static int
1162b032f27cSSam Leffler ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
116368e8e04eSSam Leffler 	u_int hdrsize, u_int ciphdrsize, u_int mtu)
116468e8e04eSSam Leffler {
116568e8e04eSSam Leffler 	struct ieee80211_frame *wh, *whf;
116668e8e04eSSam Leffler 	struct mbuf *m, *prev, *next;
116768e8e04eSSam Leffler 	u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
116868e8e04eSSam Leffler 
116968e8e04eSSam Leffler 	KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
117068e8e04eSSam Leffler 	KASSERT(m0->m_pkthdr.len > mtu,
117168e8e04eSSam Leffler 		("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
117268e8e04eSSam Leffler 
117368e8e04eSSam Leffler 	wh = mtod(m0, struct ieee80211_frame *);
117468e8e04eSSam Leffler 	/* NB: mark the first frag; it will be propagated below */
117568e8e04eSSam Leffler 	wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
117668e8e04eSSam Leffler 	totalhdrsize = hdrsize + ciphdrsize;
117768e8e04eSSam Leffler 	fragno = 1;
117868e8e04eSSam Leffler 	off = mtu - ciphdrsize;
117968e8e04eSSam Leffler 	remainder = m0->m_pkthdr.len - off;
118068e8e04eSSam Leffler 	prev = m0;
118168e8e04eSSam Leffler 	do {
118268e8e04eSSam Leffler 		fragsize = totalhdrsize + remainder;
118368e8e04eSSam Leffler 		if (fragsize > mtu)
118468e8e04eSSam Leffler 			fragsize = mtu;
1185b032f27cSSam Leffler 		/* XXX fragsize can be >2048! */
118668e8e04eSSam Leffler 		KASSERT(fragsize < MCLBYTES,
118768e8e04eSSam Leffler 			("fragment size %u too big!", fragsize));
118868e8e04eSSam Leffler 		if (fragsize > MHLEN)
118968e8e04eSSam Leffler 			m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
119068e8e04eSSam Leffler 		else
119168e8e04eSSam Leffler 			m = m_gethdr(M_DONTWAIT, MT_DATA);
119268e8e04eSSam Leffler 		if (m == NULL)
119368e8e04eSSam Leffler 			goto bad;
119468e8e04eSSam Leffler 		/* leave room to prepend any cipher header */
119568e8e04eSSam Leffler 		m_align(m, fragsize - ciphdrsize);
119668e8e04eSSam Leffler 
119768e8e04eSSam Leffler 		/*
119868e8e04eSSam Leffler 		 * Form the header in the fragment.  Note that since
119968e8e04eSSam Leffler 		 * we mark the first fragment with the MORE_FRAG bit
120068e8e04eSSam Leffler 		 * it automatically is propagated to each fragment; we
120168e8e04eSSam Leffler 		 * need only clear it on the last fragment (done below).
120268e8e04eSSam Leffler 		 */
120368e8e04eSSam Leffler 		whf = mtod(m, struct ieee80211_frame *);
120468e8e04eSSam Leffler 		memcpy(whf, wh, hdrsize);
120568e8e04eSSam Leffler 		*(uint16_t *)&whf->i_seq[0] |= htole16(
120668e8e04eSSam Leffler 			(fragno & IEEE80211_SEQ_FRAG_MASK) <<
120768e8e04eSSam Leffler 				IEEE80211_SEQ_FRAG_SHIFT);
120868e8e04eSSam Leffler 		fragno++;
120968e8e04eSSam Leffler 
121068e8e04eSSam Leffler 		payload = fragsize - totalhdrsize;
121168e8e04eSSam Leffler 		/* NB: destination is known to be contiguous */
121268e8e04eSSam Leffler 		m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrsize);
121368e8e04eSSam Leffler 		m->m_len = hdrsize + payload;
121468e8e04eSSam Leffler 		m->m_pkthdr.len = hdrsize + payload;
121568e8e04eSSam Leffler 		m->m_flags |= M_FRAG;
121668e8e04eSSam Leffler 
121768e8e04eSSam Leffler 		/* chain up the fragment */
121868e8e04eSSam Leffler 		prev->m_nextpkt = m;
121968e8e04eSSam Leffler 		prev = m;
122068e8e04eSSam Leffler 
122168e8e04eSSam Leffler 		/* deduct fragment just formed */
122268e8e04eSSam Leffler 		remainder -= payload;
122368e8e04eSSam Leffler 		off += payload;
122468e8e04eSSam Leffler 	} while (remainder != 0);
122551cec121SWeongyo Jeong 
122651cec121SWeongyo Jeong 	/* set the last fragment */
122751cec121SWeongyo Jeong 	m->m_flags |= M_LASTFRAG;
122868e8e04eSSam Leffler 	whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
122968e8e04eSSam Leffler 
123068e8e04eSSam Leffler 	/* strip first mbuf now that everything has been copied */
123168e8e04eSSam Leffler 	m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
123268e8e04eSSam Leffler 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
123368e8e04eSSam Leffler 
1234b032f27cSSam Leffler 	vap->iv_stats.is_tx_fragframes++;
1235b032f27cSSam Leffler 	vap->iv_stats.is_tx_frags += fragno-1;
123668e8e04eSSam Leffler 
123768e8e04eSSam Leffler 	return 1;
123868e8e04eSSam Leffler bad:
123968e8e04eSSam Leffler 	/* reclaim fragments but leave original frame for caller to free */
124068e8e04eSSam Leffler 	for (m = m0->m_nextpkt; m != NULL; m = next) {
124168e8e04eSSam Leffler 		next = m->m_nextpkt;
124268e8e04eSSam Leffler 		m->m_nextpkt = NULL;		/* XXX paranoid */
124368e8e04eSSam Leffler 		m_freem(m);
124468e8e04eSSam Leffler 	}
124568e8e04eSSam Leffler 	m0->m_nextpkt = NULL;
124668e8e04eSSam Leffler 	return 0;
124768e8e04eSSam Leffler }
124868e8e04eSSam Leffler 
124968e8e04eSSam Leffler /*
12501a1e1d21SSam Leffler  * Add a supported rates element id to a frame.
12511a1e1d21SSam Leffler  */
125268e8e04eSSam Leffler static uint8_t *
125368e8e04eSSam Leffler ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
12541a1e1d21SSam Leffler {
12551a1e1d21SSam Leffler 	int nrates;
12561a1e1d21SSam Leffler 
12571a1e1d21SSam Leffler 	*frm++ = IEEE80211_ELEMID_RATES;
12581a1e1d21SSam Leffler 	nrates = rs->rs_nrates;
12591a1e1d21SSam Leffler 	if (nrates > IEEE80211_RATE_SIZE)
12601a1e1d21SSam Leffler 		nrates = IEEE80211_RATE_SIZE;
12611a1e1d21SSam Leffler 	*frm++ = nrates;
12621a1e1d21SSam Leffler 	memcpy(frm, rs->rs_rates, nrates);
12631a1e1d21SSam Leffler 	return frm + nrates;
12641a1e1d21SSam Leffler }
12651a1e1d21SSam Leffler 
12661a1e1d21SSam Leffler /*
12671a1e1d21SSam Leffler  * Add an extended supported rates element id to a frame.
12681a1e1d21SSam Leffler  */
126968e8e04eSSam Leffler static uint8_t *
127068e8e04eSSam Leffler ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
12711a1e1d21SSam Leffler {
12721a1e1d21SSam Leffler 	/*
12731a1e1d21SSam Leffler 	 * Add an extended supported rates element if operating in 11g mode.
12741a1e1d21SSam Leffler 	 */
12751a1e1d21SSam Leffler 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
12761a1e1d21SSam Leffler 		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
12771a1e1d21SSam Leffler 		*frm++ = IEEE80211_ELEMID_XRATES;
12781a1e1d21SSam Leffler 		*frm++ = nrates;
12791a1e1d21SSam Leffler 		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
12801a1e1d21SSam Leffler 		frm += nrates;
12811a1e1d21SSam Leffler 	}
12821a1e1d21SSam Leffler 	return frm;
12831a1e1d21SSam Leffler }
12841a1e1d21SSam Leffler 
12851a1e1d21SSam Leffler /*
1286b032f27cSSam Leffler  * Add an ssid element to a frame.
12871a1e1d21SSam Leffler  */
128868e8e04eSSam Leffler static uint8_t *
128968e8e04eSSam Leffler ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
12901a1e1d21SSam Leffler {
12911a1e1d21SSam Leffler 	*frm++ = IEEE80211_ELEMID_SSID;
12921a1e1d21SSam Leffler 	*frm++ = len;
12931a1e1d21SSam Leffler 	memcpy(frm, ssid, len);
12941a1e1d21SSam Leffler 	return frm + len;
12951a1e1d21SSam Leffler }
12961a1e1d21SSam Leffler 
12978a1b9b6aSSam Leffler /*
12988a1b9b6aSSam Leffler  * Add an erp element to a frame.
12998a1b9b6aSSam Leffler  */
130068e8e04eSSam Leffler static uint8_t *
130168e8e04eSSam Leffler ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic)
13021a1e1d21SSam Leffler {
130368e8e04eSSam Leffler 	uint8_t erp;
13041a1e1d21SSam Leffler 
13058a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_ERP;
13068a1b9b6aSSam Leffler 	*frm++ = 1;
13078a1b9b6aSSam Leffler 	erp = 0;
13088a1b9b6aSSam Leffler 	if (ic->ic_nonerpsta != 0)
13098a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_NON_ERP_PRESENT;
13108a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_USEPROT)
13118a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_USE_PROTECTION;
13128a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_USEBARKER)
13138a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_LONG_PREAMBLE;
13148a1b9b6aSSam Leffler 	*frm++ = erp;
13158a1b9b6aSSam Leffler 	return frm;
13161a1e1d21SSam Leffler }
13171a1e1d21SSam Leffler 
13188a1b9b6aSSam Leffler /*
1319b032f27cSSam Leffler  * Add a CFParams element to a frame.
13208a1b9b6aSSam Leffler  */
132168e8e04eSSam Leffler static uint8_t *
1322b032f27cSSam Leffler ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
13238a1b9b6aSSam Leffler {
1324b032f27cSSam Leffler #define	ADDSHORT(frm, v) do {			\
1325b032f27cSSam Leffler 	frm[0] = (v) & 0xff;			\
1326b032f27cSSam Leffler 	frm[1] = (v) >> 8;			\
1327b032f27cSSam Leffler 	frm += 2;				\
1328b032f27cSSam Leffler } while (0)
1329b032f27cSSam Leffler 	*frm++ = IEEE80211_ELEMID_CFPARMS;
1330b032f27cSSam Leffler 	*frm++ = 6;
1331b032f27cSSam Leffler 	*frm++ = 0;		/* CFP count */
1332b032f27cSSam Leffler 	*frm++ = 2;		/* CFP period */
1333b032f27cSSam Leffler 	ADDSHORT(frm, 0);	/* CFP MaxDuration (TU) */
1334b032f27cSSam Leffler 	ADDSHORT(frm, 0);	/* CFP CurRemaining (TU) */
13358a1b9b6aSSam Leffler 	return frm;
1336b032f27cSSam Leffler #undef ADDSHORT
1337b032f27cSSam Leffler }
1338b032f27cSSam Leffler 
1339b032f27cSSam Leffler static __inline uint8_t *
1340b032f27cSSam Leffler add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
1341b032f27cSSam Leffler {
1342b032f27cSSam Leffler 	memcpy(frm, ie->ie_data, ie->ie_len);
1343b032f27cSSam Leffler 	return frm + ie->ie_len;
1344b032f27cSSam Leffler }
1345b032f27cSSam Leffler 
1346b032f27cSSam Leffler static __inline uint8_t *
1347b032f27cSSam Leffler add_ie(uint8_t *frm, const uint8_t *ie)
1348b032f27cSSam Leffler {
1349b032f27cSSam Leffler 	memcpy(frm, ie, 2 + ie[1]);
1350b032f27cSSam Leffler 	return frm + 2 + ie[1];
13518a1b9b6aSSam Leffler }
13528a1b9b6aSSam Leffler 
13538a1b9b6aSSam Leffler #define	WME_OUI_BYTES		0x00, 0x50, 0xf2
13548a1b9b6aSSam Leffler /*
13558a1b9b6aSSam Leffler  * Add a WME information element to a frame.
13568a1b9b6aSSam Leffler  */
135768e8e04eSSam Leffler static uint8_t *
135868e8e04eSSam Leffler ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme)
13598a1b9b6aSSam Leffler {
13608a1b9b6aSSam Leffler 	static const struct ieee80211_wme_info info = {
13618a1b9b6aSSam Leffler 		.wme_id		= IEEE80211_ELEMID_VENDOR,
13628a1b9b6aSSam Leffler 		.wme_len	= sizeof(struct ieee80211_wme_info) - 2,
13638a1b9b6aSSam Leffler 		.wme_oui	= { WME_OUI_BYTES },
13648a1b9b6aSSam Leffler 		.wme_type	= WME_OUI_TYPE,
13658a1b9b6aSSam Leffler 		.wme_subtype	= WME_INFO_OUI_SUBTYPE,
13668a1b9b6aSSam Leffler 		.wme_version	= WME_VERSION,
13678a1b9b6aSSam Leffler 		.wme_info	= 0,
13688a1b9b6aSSam Leffler 	};
13698a1b9b6aSSam Leffler 	memcpy(frm, &info, sizeof(info));
13708a1b9b6aSSam Leffler 	return frm + sizeof(info);
13718a1b9b6aSSam Leffler }
13728a1b9b6aSSam Leffler 
13738a1b9b6aSSam Leffler /*
13748a1b9b6aSSam Leffler  * Add a WME parameters element to a frame.
13758a1b9b6aSSam Leffler  */
137668e8e04eSSam Leffler static uint8_t *
137768e8e04eSSam Leffler ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme)
13788a1b9b6aSSam Leffler {
13798a1b9b6aSSam Leffler #define	SM(_v, _f)	(((_v) << _f##_S) & _f)
13808a1b9b6aSSam Leffler #define	ADDSHORT(frm, v) do {			\
13818a1b9b6aSSam Leffler 	frm[0] = (v) & 0xff;			\
13828a1b9b6aSSam Leffler 	frm[1] = (v) >> 8;			\
13838a1b9b6aSSam Leffler 	frm += 2;				\
13848a1b9b6aSSam Leffler } while (0)
13858a1b9b6aSSam Leffler 	/* NB: this works 'cuz a param has an info at the front */
13868a1b9b6aSSam Leffler 	static const struct ieee80211_wme_info param = {
13878a1b9b6aSSam Leffler 		.wme_id		= IEEE80211_ELEMID_VENDOR,
13888a1b9b6aSSam Leffler 		.wme_len	= sizeof(struct ieee80211_wme_param) - 2,
13898a1b9b6aSSam Leffler 		.wme_oui	= { WME_OUI_BYTES },
13908a1b9b6aSSam Leffler 		.wme_type	= WME_OUI_TYPE,
13918a1b9b6aSSam Leffler 		.wme_subtype	= WME_PARAM_OUI_SUBTYPE,
13928a1b9b6aSSam Leffler 		.wme_version	= WME_VERSION,
13938a1b9b6aSSam Leffler 	};
13948a1b9b6aSSam Leffler 	int i;
13958a1b9b6aSSam Leffler 
13968a1b9b6aSSam Leffler 	memcpy(frm, &param, sizeof(param));
13978a1b9b6aSSam Leffler 	frm += __offsetof(struct ieee80211_wme_info, wme_info);
13988a1b9b6aSSam Leffler 	*frm++ = wme->wme_bssChanParams.cap_info;	/* AC info */
13998a1b9b6aSSam Leffler 	*frm++ = 0;					/* reserved field */
14008a1b9b6aSSam Leffler 	for (i = 0; i < WME_NUM_AC; i++) {
14018a1b9b6aSSam Leffler 		const struct wmeParams *ac =
14028a1b9b6aSSam Leffler 		       &wme->wme_bssChanParams.cap_wmeParams[i];
14038a1b9b6aSSam Leffler 		*frm++ = SM(i, WME_PARAM_ACI)
14048a1b9b6aSSam Leffler 		       | SM(ac->wmep_acm, WME_PARAM_ACM)
14058a1b9b6aSSam Leffler 		       | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
14068a1b9b6aSSam Leffler 		       ;
14078a1b9b6aSSam Leffler 		*frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
14088a1b9b6aSSam Leffler 		       | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
14098a1b9b6aSSam Leffler 		       ;
14108a1b9b6aSSam Leffler 		ADDSHORT(frm, ac->wmep_txopLimit);
14118a1b9b6aSSam Leffler 	}
14128a1b9b6aSSam Leffler 	return frm;
14138a1b9b6aSSam Leffler #undef SM
14148a1b9b6aSSam Leffler #undef ADDSHORT
14158a1b9b6aSSam Leffler }
14168a1b9b6aSSam Leffler #undef WME_OUI_BYTES
14178a1b9b6aSSam Leffler 
14180a915fadSSam Leffler /*
1419b032f27cSSam Leffler  * Add an 11h Power Constraint element to a frame.
1420b032f27cSSam Leffler  */
1421b032f27cSSam Leffler static uint8_t *
1422b032f27cSSam Leffler ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
1423b032f27cSSam Leffler {
1424b032f27cSSam Leffler 	const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
1425b032f27cSSam Leffler 	/* XXX per-vap tx power limit? */
1426b032f27cSSam Leffler 	int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
1427b032f27cSSam Leffler 
1428b032f27cSSam Leffler 	frm[0] = IEEE80211_ELEMID_PWRCNSTR;
1429b032f27cSSam Leffler 	frm[1] = 1;
1430b032f27cSSam Leffler 	frm[2] = c->ic_maxregpower > limit ?  c->ic_maxregpower - limit : 0;
1431b032f27cSSam Leffler 	return frm + 3;
1432b032f27cSSam Leffler }
1433b032f27cSSam Leffler 
1434b032f27cSSam Leffler /*
1435b032f27cSSam Leffler  * Add an 11h Power Capability element to a frame.
1436b032f27cSSam Leffler  */
1437b032f27cSSam Leffler static uint8_t *
1438b032f27cSSam Leffler ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
1439b032f27cSSam Leffler {
1440b032f27cSSam Leffler 	frm[0] = IEEE80211_ELEMID_PWRCAP;
1441b032f27cSSam Leffler 	frm[1] = 2;
1442b032f27cSSam Leffler 	frm[2] = c->ic_minpower;
1443b032f27cSSam Leffler 	frm[3] = c->ic_maxpower;
1444b032f27cSSam Leffler 	return frm + 4;
1445b032f27cSSam Leffler }
1446b032f27cSSam Leffler 
1447b032f27cSSam Leffler /*
1448b032f27cSSam Leffler  * Add an 11h Supported Channels element to a frame.
1449b032f27cSSam Leffler  */
1450b032f27cSSam Leffler static uint8_t *
1451b032f27cSSam Leffler ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
1452b032f27cSSam Leffler {
1453b032f27cSSam Leffler 	static const int ielen = 26;
1454b032f27cSSam Leffler 
1455b032f27cSSam Leffler 	frm[0] = IEEE80211_ELEMID_SUPPCHAN;
1456b032f27cSSam Leffler 	frm[1] = ielen;
1457b032f27cSSam Leffler 	/* XXX not correct */
1458b032f27cSSam Leffler 	memcpy(frm+2, ic->ic_chan_avail, ielen);
1459b032f27cSSam Leffler 	return frm + 2 + ielen;
1460b032f27cSSam Leffler }
1461b032f27cSSam Leffler 
1462b032f27cSSam Leffler /*
1463b032f27cSSam Leffler  * Add an 11h Channel Switch Announcement element to a frame.
1464b032f27cSSam Leffler  * Note that we use the per-vap CSA count to adjust the global
1465b032f27cSSam Leffler  * counter so we can use this routine to form probe response
1466b032f27cSSam Leffler  * frames and get the current count.
1467b032f27cSSam Leffler  */
1468b032f27cSSam Leffler static uint8_t *
1469b032f27cSSam Leffler ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
1470b032f27cSSam Leffler {
1471b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1472b032f27cSSam Leffler 	struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
1473b032f27cSSam Leffler 
1474b032f27cSSam Leffler 	csa->csa_ie = IEEE80211_ELEMID_CHANSWITCHANN;
1475b032f27cSSam Leffler 	csa->csa_len = 3;
1476b032f27cSSam Leffler 	csa->csa_mode = 1;		/* XXX force quiet on channel */
1477b032f27cSSam Leffler 	csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
1478b032f27cSSam Leffler 	csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
1479b032f27cSSam Leffler 	return frm + sizeof(*csa);
1480b032f27cSSam Leffler }
1481b032f27cSSam Leffler 
1482b032f27cSSam Leffler /*
1483b032f27cSSam Leffler  * Add an 11h country information element to a frame.
1484b032f27cSSam Leffler  */
1485b032f27cSSam Leffler static uint8_t *
1486b032f27cSSam Leffler ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
1487b032f27cSSam Leffler {
1488b032f27cSSam Leffler 
1489b032f27cSSam Leffler 	if (ic->ic_countryie == NULL ||
1490b032f27cSSam Leffler 	    ic->ic_countryie_chan != ic->ic_bsschan) {
1491b032f27cSSam Leffler 		/*
1492b032f27cSSam Leffler 		 * Handle lazy construction of ie.  This is done on
1493b032f27cSSam Leffler 		 * first use and after a channel change that requires
1494b032f27cSSam Leffler 		 * re-calculation.
1495b032f27cSSam Leffler 		 */
1496b032f27cSSam Leffler 		if (ic->ic_countryie != NULL)
1497b032f27cSSam Leffler 			free(ic->ic_countryie, M_80211_NODE_IE);
1498b032f27cSSam Leffler 		ic->ic_countryie = ieee80211_alloc_countryie(ic);
1499b032f27cSSam Leffler 		if (ic->ic_countryie == NULL)
1500b032f27cSSam Leffler 			return frm;
1501b032f27cSSam Leffler 		ic->ic_countryie_chan = ic->ic_bsschan;
1502b032f27cSSam Leffler 	}
1503b032f27cSSam Leffler 	return add_appie(frm, ic->ic_countryie);
1504b032f27cSSam Leffler }
1505b032f27cSSam Leffler 
1506b032f27cSSam Leffler /*
1507af8418dcSSam Leffler  * Send a probe request frame with the specified ssid
1508af8418dcSSam Leffler  * and any optional information element data.
1509af8418dcSSam Leffler  */
1510af8418dcSSam Leffler int
1511af8418dcSSam Leffler ieee80211_send_probereq(struct ieee80211_node *ni,
151268e8e04eSSam Leffler 	const uint8_t sa[IEEE80211_ADDR_LEN],
151368e8e04eSSam Leffler 	const uint8_t da[IEEE80211_ADDR_LEN],
151468e8e04eSSam Leffler 	const uint8_t bssid[IEEE80211_ADDR_LEN],
1515b032f27cSSam Leffler 	const uint8_t *ssid, size_t ssidlen)
1516af8418dcSSam Leffler {
1517b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1518af8418dcSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
151916d7cbb1SSam Leffler 	const struct ieee80211_txparam *tp;
152016d7cbb1SSam Leffler 	struct ieee80211_bpf_params params;
1521af8418dcSSam Leffler 	struct ieee80211_frame *wh;
152241b3c790SSam Leffler 	const struct ieee80211_rateset *rs;
1523af8418dcSSam Leffler 	struct mbuf *m;
152468e8e04eSSam Leffler 	uint8_t *frm;
1525af8418dcSSam Leffler 
1526b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
1527b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
1528b032f27cSSam Leffler 		    "block %s frame in CAC state", "probe request");
1529b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
1530b032f27cSSam Leffler 		return EIO;		/* XXX */
1531b032f27cSSam Leffler 	}
1532b032f27cSSam Leffler 
1533af8418dcSSam Leffler 	/*
1534af8418dcSSam Leffler 	 * Hold a reference on the node so it doesn't go away until after
1535af8418dcSSam Leffler 	 * the xmit is complete all the way in the driver.  On error we
1536af8418dcSSam Leffler 	 * will remove our reference.
1537af8418dcSSam Leffler 	 */
1538b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1539af8418dcSSam Leffler 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1540af8418dcSSam Leffler 		__func__, __LINE__,
1541af8418dcSSam Leffler 		ni, ether_sprintf(ni->ni_macaddr),
1542af8418dcSSam Leffler 		ieee80211_node_refcnt(ni)+1);
1543af8418dcSSam Leffler 	ieee80211_ref_node(ni);
1544af8418dcSSam Leffler 
1545af8418dcSSam Leffler 	/*
1546af8418dcSSam Leffler 	 * prreq frame format
1547af8418dcSSam Leffler 	 *	[tlv] ssid
1548af8418dcSSam Leffler 	 *	[tlv] supported rates
1549b032f27cSSam Leffler 	 *	[tlv] RSN (optional)
1550af8418dcSSam Leffler 	 *	[tlv] extended supported rates
1551b032f27cSSam Leffler 	 *	[tlv] WPA (optional)
1552af8418dcSSam Leffler 	 *	[tlv] user-specified ie's
1553af8418dcSSam Leffler 	 */
1554af8418dcSSam Leffler 	m = ieee80211_getmgtframe(&frm,
155568e8e04eSSam Leffler 		 ic->ic_headroom + sizeof(struct ieee80211_frame),
1556af8418dcSSam Leffler 	       	 2 + IEEE80211_NWID_LEN
1557af8418dcSSam Leffler 	       + 2 + IEEE80211_RATE_SIZE
1558b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_wpa)
1559af8418dcSSam Leffler 	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1560b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_wpa)
1561b032f27cSSam Leffler 	       + (vap->iv_appie_probereq != NULL ?
1562b032f27cSSam Leffler 		   vap->iv_appie_probereq->ie_len : 0)
1563af8418dcSSam Leffler 	);
1564af8418dcSSam Leffler 	if (m == NULL) {
1565b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
1566af8418dcSSam Leffler 		ieee80211_free_node(ni);
1567af8418dcSSam Leffler 		return ENOMEM;
1568af8418dcSSam Leffler 	}
1569af8418dcSSam Leffler 
1570af8418dcSSam Leffler 	frm = ieee80211_add_ssid(frm, ssid, ssidlen);
157141b3c790SSam Leffler 	rs = ieee80211_get_suprates(ic, ic->ic_curchan);
157241b3c790SSam Leffler 	frm = ieee80211_add_rates(frm, rs);
1573b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA2) {
1574b032f27cSSam Leffler 		if (vap->iv_rsn_ie != NULL)
1575b032f27cSSam Leffler 			frm = add_ie(frm, vap->iv_rsn_ie);
1576b032f27cSSam Leffler 		/* XXX else complain? */
1577af8418dcSSam Leffler 	}
1578b032f27cSSam Leffler 	frm = ieee80211_add_xrates(frm, rs);
1579b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA1) {
1580b032f27cSSam Leffler 		if (vap->iv_wpa_ie != NULL)
1581b032f27cSSam Leffler 			frm = add_ie(frm, vap->iv_wpa_ie);
1582b032f27cSSam Leffler 		/* XXX else complain? */
1583b032f27cSSam Leffler 	}
1584b032f27cSSam Leffler 	if (vap->iv_appie_probereq != NULL)
1585b032f27cSSam Leffler 		frm = add_appie(frm, vap->iv_appie_probereq);
158668e8e04eSSam Leffler 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1587af8418dcSSam Leffler 
158816d7cbb1SSam Leffler 	KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
158916d7cbb1SSam Leffler 	    ("leading space %zd", M_LEADINGSPACE(m)));
1590af8418dcSSam Leffler 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
159116d7cbb1SSam Leffler 	if (m == NULL) {
159216d7cbb1SSam Leffler 		/* NB: cannot happen */
159316d7cbb1SSam Leffler 		ieee80211_free_node(ni);
1594af8418dcSSam Leffler 		return ENOMEM;
159516d7cbb1SSam Leffler 	}
1596af8418dcSSam Leffler 
1597af8418dcSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
15989e80b1dfSSam Leffler 	ieee80211_send_setup(ni, m,
1599af8418dcSSam Leffler 	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
16008ac160cdSSam Leffler 	     IEEE80211_NONQOS_TID, sa, da, bssid);
1601af8418dcSSam Leffler 	/* XXX power management? */
1602c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
1603af8418dcSSam Leffler 
1604b032f27cSSam Leffler 	M_WME_SETAC(m, WME_AC_BE);
1605b032f27cSSam Leffler 
1606af8418dcSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_probereq);
1607af8418dcSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_mgmt);
1608af8418dcSSam Leffler 
1609b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1610b032f27cSSam Leffler 	    "send probe req on channel %u bssid %s ssid \"%.*s\"\n",
1611b032f27cSSam Leffler 	    ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(bssid),
1612b032f27cSSam Leffler 	    ssidlen, ssid);
1613af8418dcSSam Leffler 
161416d7cbb1SSam Leffler 	memset(&params, 0, sizeof(params));
161516d7cbb1SSam Leffler 	params.ibp_pri = M_WME_GETAC(m);
161616d7cbb1SSam Leffler 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
161716d7cbb1SSam Leffler 	params.ibp_rate0 = tp->mgmtrate;
161816d7cbb1SSam Leffler 	if (IEEE80211_IS_MULTICAST(da)) {
161916d7cbb1SSam Leffler 		params.ibp_flags |= IEEE80211_BPF_NOACK;
162016d7cbb1SSam Leffler 		params.ibp_try0 = 1;
162116d7cbb1SSam Leffler 	} else
162216d7cbb1SSam Leffler 		params.ibp_try0 = tp->maxretry;
162316d7cbb1SSam Leffler 	params.ibp_power = ni->ni_txpower;
162416d7cbb1SSam Leffler 	return ic->ic_raw_xmit(ni, m, &params);
1625af8418dcSSam Leffler }
1626af8418dcSSam Leffler 
1627af8418dcSSam Leffler /*
1628667dad55SSam Leffler  * Calculate capability information for mgt frames.
1629667dad55SSam Leffler  */
163068e8e04eSSam Leffler static uint16_t
1631b032f27cSSam Leffler getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
1632667dad55SSam Leffler {
1633b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
163468e8e04eSSam Leffler 	uint16_t capinfo;
1635667dad55SSam Leffler 
1636b032f27cSSam Leffler 	KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
1637667dad55SSam Leffler 
1638b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1639667dad55SSam Leffler 		capinfo = IEEE80211_CAPINFO_ESS;
1640b032f27cSSam Leffler 	else if (vap->iv_opmode == IEEE80211_M_IBSS)
1641667dad55SSam Leffler 		capinfo = IEEE80211_CAPINFO_IBSS;
1642667dad55SSam Leffler 	else
1643667dad55SSam Leffler 		capinfo = 0;
1644b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY)
1645667dad55SSam Leffler 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
1646667dad55SSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1647667dad55SSam Leffler 	    IEEE80211_IS_CHAN_2GHZ(chan))
1648667dad55SSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1649667dad55SSam Leffler 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
1650667dad55SSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1651b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
1652b032f27cSSam Leffler 		capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
1653667dad55SSam Leffler 	return capinfo;
1654667dad55SSam Leffler }
1655667dad55SSam Leffler 
1656667dad55SSam Leffler /*
16570a915fadSSam Leffler  * Send a management frame.  The node is for the destination (or ic_bss
16580a915fadSSam Leffler  * when in station mode).  Nodes other than ic_bss have their reference
16590a915fadSSam Leffler  * count bumped to reflect our use for an indeterminant time.
16600a915fadSSam Leffler  */
16611a1e1d21SSam Leffler int
1662b032f27cSSam Leffler ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
16631a1e1d21SSam Leffler {
16641b6167d2SSam Leffler #define	HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
1665b032f27cSSam Leffler #define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
1666b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1667b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1668b032f27cSSam Leffler 	struct ieee80211_node *bss = vap->iv_bss;
16698ac160cdSSam Leffler 	struct ieee80211_bpf_params params;
16701a1e1d21SSam Leffler 	struct mbuf *m;
167168e8e04eSSam Leffler 	uint8_t *frm;
167268e8e04eSSam Leffler 	uint16_t capinfo;
167368e8e04eSSam Leffler 	int has_challenge, is_shared_key, ret, status;
16741a1e1d21SSam Leffler 
16750a915fadSSam Leffler 	KASSERT(ni != NULL, ("null node"));
16760a915fadSSam Leffler 
16770a915fadSSam Leffler 	/*
16780a915fadSSam Leffler 	 * Hold a reference on the node so it doesn't go away until after
16790a915fadSSam Leffler 	 * the xmit is complete all the way in the driver.  On error we
16800a915fadSSam Leffler 	 * will remove our reference.
16810a915fadSSam Leffler 	 */
1682b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
168349a15236SSam Leffler 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
16848a1b9b6aSSam Leffler 		__func__, __LINE__,
168549a15236SSam Leffler 		ni, ether_sprintf(ni->ni_macaddr),
16868a1b9b6aSSam Leffler 		ieee80211_node_refcnt(ni)+1);
16870a915fadSSam Leffler 	ieee80211_ref_node(ni);
16888a1b9b6aSSam Leffler 
16898ac160cdSSam Leffler 	memset(&params, 0, sizeof(params));
16901a1e1d21SSam Leffler 	switch (type) {
16911a1e1d21SSam Leffler 
16921a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_AUTH:
16938a1b9b6aSSam Leffler 		status = arg >> 16;
16948a1b9b6aSSam Leffler 		arg &= 0xffff;
16958a1b9b6aSSam Leffler 		has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
16968a1b9b6aSSam Leffler 		    arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
16978a1b9b6aSSam Leffler 		    ni->ni_challenge != NULL);
16988a1b9b6aSSam Leffler 
16998a1b9b6aSSam Leffler 		/*
17008a1b9b6aSSam Leffler 		 * Deduce whether we're doing open authentication or
17018a1b9b6aSSam Leffler 		 * shared key authentication.  We do the latter if
17028a1b9b6aSSam Leffler 		 * we're in the middle of a shared key authentication
17038a1b9b6aSSam Leffler 		 * handshake or if we're initiating an authentication
17048a1b9b6aSSam Leffler 		 * request and configured to use shared key.
17058a1b9b6aSSam Leffler 		 */
17068a1b9b6aSSam Leffler 		is_shared_key = has_challenge ||
17078a1b9b6aSSam Leffler 		     arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
17088a1b9b6aSSam Leffler 		     (arg == IEEE80211_AUTH_SHARED_REQUEST &&
1709b032f27cSSam Leffler 		      bss->ni_authmode == IEEE80211_AUTH_SHARED);
17108a1b9b6aSSam Leffler 
17118a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
171268e8e04eSSam Leffler 			  ic->ic_headroom + sizeof(struct ieee80211_frame),
171368e8e04eSSam Leffler 			  3 * sizeof(uint16_t)
17148a1b9b6aSSam Leffler 			+ (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
171568e8e04eSSam Leffler 				sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)
17168a1b9b6aSSam Leffler 		);
17171a1e1d21SSam Leffler 		if (m == NULL)
17188a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
17198a1b9b6aSSam Leffler 
172068e8e04eSSam Leffler 		((uint16_t *)frm)[0] =
17218a1b9b6aSSam Leffler 		    (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
17228a1b9b6aSSam Leffler 		                    : htole16(IEEE80211_AUTH_ALG_OPEN);
172368e8e04eSSam Leffler 		((uint16_t *)frm)[1] = htole16(arg);	/* sequence number */
172468e8e04eSSam Leffler 		((uint16_t *)frm)[2] = htole16(status);/* status */
17258a1b9b6aSSam Leffler 
17268a1b9b6aSSam Leffler 		if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
172768e8e04eSSam Leffler 			((uint16_t *)frm)[3] =
17288a1b9b6aSSam Leffler 			    htole16((IEEE80211_CHALLENGE_LEN << 8) |
17298a1b9b6aSSam Leffler 			    IEEE80211_ELEMID_CHALLENGE);
173068e8e04eSSam Leffler 			memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
17318a1b9b6aSSam Leffler 			    IEEE80211_CHALLENGE_LEN);
17328a1b9b6aSSam Leffler 			m->m_pkthdr.len = m->m_len =
173368e8e04eSSam Leffler 				4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
17348a1b9b6aSSam Leffler 			if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
1735b032f27cSSam Leffler 				IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1736b032f27cSSam Leffler 				    "request encrypt frame (%s)", __func__);
17378ac160cdSSam Leffler 				/* mark frame for encryption */
17388ac160cdSSam Leffler 				params.ibp_flags |= IEEE80211_BPF_CRYPTO;
17398a1b9b6aSSam Leffler 			}
17408a1b9b6aSSam Leffler 		} else
174168e8e04eSSam Leffler 			m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
17428a1b9b6aSSam Leffler 
17438a1b9b6aSSam Leffler 		/* XXX not right for shared key */
17448a1b9b6aSSam Leffler 		if (status == IEEE80211_STATUS_SUCCESS)
17458a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_auth);
17468a1b9b6aSSam Leffler 		else
17478a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_auth_fail);
17488a1b9b6aSSam Leffler 
1749b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA)
175068e8e04eSSam Leffler 			ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
1751b032f27cSSam Leffler 				(void *) vap->iv_state);
17521a1e1d21SSam Leffler 		break;
17531a1e1d21SSam Leffler 
17541a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
1755b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1756b032f27cSSam Leffler 		    "send station deauthenticate (reason %d)", arg);
175768e8e04eSSam Leffler 		m = ieee80211_getmgtframe(&frm,
175868e8e04eSSam Leffler 			ic->ic_headroom + sizeof(struct ieee80211_frame),
175968e8e04eSSam Leffler 			sizeof(uint16_t));
17601a1e1d21SSam Leffler 		if (m == NULL)
17618a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
176268e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(arg);	/* reason */
176368e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
17648a1b9b6aSSam Leffler 
17658a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, tx_deauth);
17668a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
17678a1b9b6aSSam Leffler 
1768e4918ecdSSam Leffler 		ieee80211_node_unauthorize(ni);		/* port closed */
17691a1e1d21SSam Leffler 		break;
17701a1e1d21SSam Leffler 
17711a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
17721a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
17731a1e1d21SSam Leffler 		/*
17741a1e1d21SSam Leffler 		 * asreq frame format
17751a1e1d21SSam Leffler 		 *	[2] capability information
17761a1e1d21SSam Leffler 		 *	[2] listen interval
17771a1e1d21SSam Leffler 		 *	[6*] current AP address (reassoc only)
17781a1e1d21SSam Leffler 		 *	[tlv] ssid
17791a1e1d21SSam Leffler 		 *	[tlv] supported rates
17801a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
1781b032f27cSSam Leffler 		 *	[4] power capability (optional)
1782b032f27cSSam Leffler 		 *	[28] supported channels (optional)
178368e8e04eSSam Leffler 		 *	[tlv] HT capabilities
1784b032f27cSSam Leffler 		 *	[tlv] WME (optional)
178568e8e04eSSam Leffler 		 *	[tlv] Vendor OUI HT capabilities (optional)
178668e8e04eSSam Leffler 		 *	[tlv] Atheros capabilities (if negotiated)
1787b032f27cSSam Leffler 		 *	[tlv] AppIE's (optional)
17881a1e1d21SSam Leffler 		 */
17898a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
179068e8e04eSSam Leffler 			 ic->ic_headroom + sizeof(struct ieee80211_frame),
179168e8e04eSSam Leffler 			 sizeof(uint16_t)
179268e8e04eSSam Leffler 		       + sizeof(uint16_t)
17931a1e1d21SSam Leffler 		       + IEEE80211_ADDR_LEN
17948a1b9b6aSSam Leffler 		       + 2 + IEEE80211_NWID_LEN
17951a1e1d21SSam Leffler 		       + 2 + IEEE80211_RATE_SIZE
17968a1b9b6aSSam Leffler 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1797b032f27cSSam Leffler 		       + 4
1798b032f27cSSam Leffler 		       + 2 + 26
17998a1b9b6aSSam Leffler 		       + sizeof(struct ieee80211_wme_info)
1800b032f27cSSam Leffler 		       + sizeof(struct ieee80211_ie_htcap)
1801b032f27cSSam Leffler 		       + 4 + sizeof(struct ieee80211_ie_htcap)
1802616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
180368e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ath_ie)
1804616190d0SSam Leffler #endif
1805b032f27cSSam Leffler 		       + (vap->iv_appie_wpa != NULL ?
1806b032f27cSSam Leffler 				vap->iv_appie_wpa->ie_len : 0)
1807b032f27cSSam Leffler 		       + (vap->iv_appie_assocreq != NULL ?
1808b032f27cSSam Leffler 				vap->iv_appie_assocreq->ie_len : 0)
18098a1b9b6aSSam Leffler 		);
18101a1e1d21SSam Leffler 		if (m == NULL)
18118a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
18121a1e1d21SSam Leffler 
1813b032f27cSSam Leffler 		KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1814b032f27cSSam Leffler 		    ("wrong mode %u", vap->iv_opmode));
1815667dad55SSam Leffler 		capinfo = IEEE80211_CAPINFO_ESS;
1816b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_PRIVACY)
18171a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
18184f2e09c4SSam Leffler 		/*
18194f2e09c4SSam Leffler 		 * NB: Some 11a AP's reject the request when
18204f2e09c4SSam Leffler 		 *     short premable is set.
18214f2e09c4SSam Leffler 		 */
18224f2e09c4SSam Leffler 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1823b5c99415SSam Leffler 		    IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
18241a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
182568e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
18268a1b9b6aSSam Leffler 		    (ic->ic_caps & IEEE80211_C_SHSLOT))
18271a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
18281b6167d2SSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
1829b032f27cSSam Leffler 		    (vap->iv_flags & IEEE80211_F_DOTH))
18301b6167d2SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
183168e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(capinfo);
18321a1e1d21SSam Leffler 		frm += 2;
18331a1e1d21SSam Leffler 
1834b032f27cSSam Leffler 		KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
183568e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
1836b032f27cSSam Leffler 						    bss->ni_intval));
18371a1e1d21SSam Leffler 		frm += 2;
18381a1e1d21SSam Leffler 
18391a1e1d21SSam Leffler 		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1840b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
18411a1e1d21SSam Leffler 			frm += IEEE80211_ADDR_LEN;
18421a1e1d21SSam Leffler 		}
18431a1e1d21SSam Leffler 
18441a1e1d21SSam Leffler 		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
18451a1e1d21SSam Leffler 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
1846b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_WPA2) {
1847b032f27cSSam Leffler 			if (vap->iv_rsn_ie != NULL)
1848b032f27cSSam Leffler 				frm = add_ie(frm, vap->iv_rsn_ie);
1849b032f27cSSam Leffler 			/* XXX else complain? */
18508a1b9b6aSSam Leffler 		}
1851b032f27cSSam Leffler 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1852b032f27cSSam Leffler 		if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
1853b032f27cSSam Leffler 			frm = ieee80211_add_powercapability(frm,
1854b032f27cSSam Leffler 			    ic->ic_curchan);
1855b032f27cSSam Leffler 			frm = ieee80211_add_supportedchannels(frm, ic);
1856b032f27cSSam Leffler 		}
1857b032f27cSSam Leffler 		if ((vap->iv_flags_ext & IEEE80211_FEXT_HT) &&
1858b032f27cSSam Leffler 		    ni->ni_ies.htcap_ie != NULL &&
1859b032f27cSSam Leffler 		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP)
1860b032f27cSSam Leffler 			frm = ieee80211_add_htcap(frm, ni);
1861b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_WPA1) {
1862b032f27cSSam Leffler 			if (vap->iv_wpa_ie != NULL)
1863b032f27cSSam Leffler 				frm = add_ie(frm, vap->iv_wpa_ie);
1864b032f27cSSam Leffler 			/* XXX else complain */
1865b032f27cSSam Leffler 		}
1866b032f27cSSam Leffler 		if ((ic->ic_flags & IEEE80211_F_WME) &&
1867b032f27cSSam Leffler 		    ni->ni_ies.wme_ie != NULL)
1868b032f27cSSam Leffler 			frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
1869b032f27cSSam Leffler 		if ((vap->iv_flags_ext & IEEE80211_FEXT_HT) &&
1870b032f27cSSam Leffler 		    ni->ni_ies.htcap_ie != NULL &&
1871b032f27cSSam Leffler 		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR)
1872b032f27cSSam Leffler 			frm = ieee80211_add_htcap_vendor(frm, ni);
1873616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
18744207227cSSam Leffler 		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
1875b032f27cSSam Leffler 			frm = ieee80211_add_ath(frm,
1876b032f27cSSam Leffler 				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
18774207227cSSam Leffler 				((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
18784207227cSSam Leffler 				 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
18794207227cSSam Leffler 				vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
18804207227cSSam Leffler 		}
1881616190d0SSam Leffler #endif /* IEEE80211_SUPPORT_SUPERG */
1882b032f27cSSam Leffler 		if (vap->iv_appie_assocreq != NULL)
1883b032f27cSSam Leffler 			frm = add_appie(frm, vap->iv_appie_assocreq);
188468e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
18851a1e1d21SSam Leffler 
188668e8e04eSSam Leffler 		ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
1887b032f27cSSam Leffler 			(void *) vap->iv_state);
18881a1e1d21SSam Leffler 		break;
18891a1e1d21SSam Leffler 
18901a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
18911a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
18921a1e1d21SSam Leffler 		/*
189368e8e04eSSam Leffler 		 * asresp frame format
18941a1e1d21SSam Leffler 		 *	[2] capability information
18951a1e1d21SSam Leffler 		 *	[2] status
18961a1e1d21SSam Leffler 		 *	[2] association ID
18971a1e1d21SSam Leffler 		 *	[tlv] supported rates
18981a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
1899b032f27cSSam Leffler 		 *	[tlv] HT capabilities (standard, if STA enabled)
1900b032f27cSSam Leffler 		 *	[tlv] HT information (standard, if STA enabled)
1901b032f27cSSam Leffler 		 *	[tlv] WME (if configured and STA enabled)
1902b032f27cSSam Leffler 		 *	[tlv] HT capabilities (vendor OUI, if STA enabled)
1903b032f27cSSam Leffler 		 *	[tlv] HT information (vendor OUI, if STA enabled)
1904b032f27cSSam Leffler 		 *	[tlv] Atheros capabilities (if STA enabled)
1905b032f27cSSam Leffler 		 *	[tlv] AppIE's (optional)
19061a1e1d21SSam Leffler 		 */
19078a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
190868e8e04eSSam Leffler 			 ic->ic_headroom + sizeof(struct ieee80211_frame),
190968e8e04eSSam Leffler 			 sizeof(uint16_t)
191068e8e04eSSam Leffler 		       + sizeof(uint16_t)
191168e8e04eSSam Leffler 		       + sizeof(uint16_t)
19121a1e1d21SSam Leffler 		       + 2 + IEEE80211_RATE_SIZE
19138a1b9b6aSSam Leffler 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
191468e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ie_htcap) + 4
191568e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ie_htinfo) + 4
1916b032f27cSSam Leffler 		       + sizeof(struct ieee80211_wme_param)
1917616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
191868e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ath_ie)
1919616190d0SSam Leffler #endif
1920b032f27cSSam Leffler 		       + (vap->iv_appie_assocresp != NULL ?
1921b032f27cSSam Leffler 				vap->iv_appie_assocresp->ie_len : 0)
19228a1b9b6aSSam Leffler 		);
19231a1e1d21SSam Leffler 		if (m == NULL)
19248a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
19251a1e1d21SSam Leffler 
1926b032f27cSSam Leffler 		capinfo = getcapinfo(vap, bss->ni_chan);
192768e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(capinfo);
19281a1e1d21SSam Leffler 		frm += 2;
19291a1e1d21SSam Leffler 
193068e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(arg);	/* status */
19311a1e1d21SSam Leffler 		frm += 2;
19321a1e1d21SSam Leffler 
19338a1b9b6aSSam Leffler 		if (arg == IEEE80211_STATUS_SUCCESS) {
193468e8e04eSSam Leffler 			*(uint16_t *)frm = htole16(ni->ni_associd);
19358a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_assoc);
19368a1b9b6aSSam Leffler 		} else
19378a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_assoc_fail);
19381a1e1d21SSam Leffler 		frm += 2;
19391a1e1d21SSam Leffler 
19401a1e1d21SSam Leffler 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
19411a1e1d21SSam Leffler 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
194268e8e04eSSam Leffler 		/* NB: respond according to what we received */
19431b6167d2SSam Leffler 		if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
194468e8e04eSSam Leffler 			frm = ieee80211_add_htcap(frm, ni);
194568e8e04eSSam Leffler 			frm = ieee80211_add_htinfo(frm, ni);
194668e8e04eSSam Leffler 		}
1947b032f27cSSam Leffler 		if ((vap->iv_flags & IEEE80211_F_WME) &&
1948b032f27cSSam Leffler 		    ni->ni_ies.wme_ie != NULL)
19491b6167d2SSam Leffler 			frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
19501b6167d2SSam Leffler 		if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
19511b6167d2SSam Leffler 			frm = ieee80211_add_htcap_vendor(frm, ni);
19521b6167d2SSam Leffler 			frm = ieee80211_add_htinfo_vendor(frm, ni);
195368e8e04eSSam Leffler 		}
1954616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1955b032f27cSSam Leffler 		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
195668e8e04eSSam Leffler 			frm = ieee80211_add_ath(frm,
1957b032f27cSSam Leffler 				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
19584207227cSSam Leffler 				((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
19594207227cSSam Leffler 				 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
19604207227cSSam Leffler 				vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
1961616190d0SSam Leffler #endif /* IEEE80211_SUPPORT_SUPERG */
1962b032f27cSSam Leffler 		if (vap->iv_appie_assocresp != NULL)
1963b032f27cSSam Leffler 			frm = add_appie(frm, vap->iv_appie_assocresp);
196468e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
19651a1e1d21SSam Leffler 		break;
19661a1e1d21SSam Leffler 
19671a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
1968b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1969b032f27cSSam Leffler 		    "send station disassociate (reason %d)", arg);
197068e8e04eSSam Leffler 		m = ieee80211_getmgtframe(&frm,
197168e8e04eSSam Leffler 			ic->ic_headroom + sizeof(struct ieee80211_frame),
197268e8e04eSSam Leffler 			sizeof(uint16_t));
19731a1e1d21SSam Leffler 		if (m == NULL)
19748a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
197568e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(arg);	/* reason */
197668e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
19778a1b9b6aSSam Leffler 
19788a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, tx_disassoc);
19798a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
19801a1e1d21SSam Leffler 		break;
19811a1e1d21SSam Leffler 
19821a1e1d21SSam Leffler 	default:
1983b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
1984b032f27cSSam Leffler 		    "invalid mgmt frame type %u", type);
19851be50176SSam Leffler 		senderr(EINVAL, is_tx_unknownmgt);
19860a915fadSSam Leffler 		/* NOTREACHED */
19871a1e1d21SSam Leffler 	}
198868e8e04eSSam Leffler 
19898ac160cdSSam Leffler 	/* NB: force non-ProbeResp frames to the highest queue */
19908ac160cdSSam Leffler 	params.ibp_pri = WME_AC_VO;
19918ac160cdSSam Leffler 	params.ibp_rate0 = bss->ni_txparms->mgmtrate;
19928ac160cdSSam Leffler 	/* NB: we know all frames are unicast */
19938ac160cdSSam Leffler 	params.ibp_try0 = bss->ni_txparms->maxretry;
19948ac160cdSSam Leffler 	params.ibp_power = bss->ni_txpower;
19958ac160cdSSam Leffler 	return ieee80211_mgmt_output(ni, m, type, &params);
19960a915fadSSam Leffler bad:
19978a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
19981a1e1d21SSam Leffler 	return ret;
19990a915fadSSam Leffler #undef senderr
20001b6167d2SSam Leffler #undef HTFLAGS
20011a1e1d21SSam Leffler }
20028a1b9b6aSSam Leffler 
2003b032f27cSSam Leffler /*
2004b032f27cSSam Leffler  * Return an mbuf with a probe response frame in it.
2005b032f27cSSam Leffler  * Space is left to prepend and 802.11 header at the
2006b032f27cSSam Leffler  * front but it's left to the caller to fill in.
2007b032f27cSSam Leffler  */
2008b032f27cSSam Leffler struct mbuf *
2009b032f27cSSam Leffler ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
2010b032f27cSSam Leffler {
2011b032f27cSSam Leffler 	struct ieee80211vap *vap = bss->ni_vap;
2012b032f27cSSam Leffler 	struct ieee80211com *ic = bss->ni_ic;
2013b032f27cSSam Leffler 	const struct ieee80211_rateset *rs;
2014b032f27cSSam Leffler 	struct mbuf *m;
2015b032f27cSSam Leffler 	uint16_t capinfo;
2016b032f27cSSam Leffler 	uint8_t *frm;
2017b032f27cSSam Leffler 
2018b032f27cSSam Leffler 	/*
2019b032f27cSSam Leffler 	 * probe response frame format
2020b032f27cSSam Leffler 	 *	[8] time stamp
2021b032f27cSSam Leffler 	 *	[2] beacon interval
2022b032f27cSSam Leffler 	 *	[2] cabability information
2023b032f27cSSam Leffler 	 *	[tlv] ssid
2024b032f27cSSam Leffler 	 *	[tlv] supported rates
2025b032f27cSSam Leffler 	 *	[tlv] parameter set (FH/DS)
2026b032f27cSSam Leffler 	 *	[tlv] parameter set (IBSS)
2027b032f27cSSam Leffler 	 *	[tlv] country (optional)
2028b032f27cSSam Leffler 	 *	[3] power control (optional)
2029b032f27cSSam Leffler 	 *	[5] channel switch announcement (CSA) (optional)
2030b032f27cSSam Leffler 	 *	[tlv] extended rate phy (ERP)
2031b032f27cSSam Leffler 	 *	[tlv] extended supported rates
2032688fe74dSSam Leffler 	 *	[tlv] RSN (optional)
2033b032f27cSSam Leffler 	 *	[tlv] HT capabilities
2034b032f27cSSam Leffler 	 *	[tlv] HT information
2035b032f27cSSam Leffler 	 *	[tlv] WPA (optional)
2036b032f27cSSam Leffler 	 *	[tlv] WME (optional)
2037b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT capabilities (optional)
2038b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT information (optional)
2039b032f27cSSam Leffler 	 *	[tlv] Atheros capabilities
2040b032f27cSSam Leffler 	 *	[tlv] AppIE's (optional)
2041b032f27cSSam Leffler 	 */
2042b032f27cSSam Leffler 	m = ieee80211_getmgtframe(&frm,
2043b032f27cSSam Leffler 		 ic->ic_headroom + sizeof(struct ieee80211_frame),
2044b032f27cSSam Leffler 		 8
2045b032f27cSSam Leffler 	       + sizeof(uint16_t)
2046b032f27cSSam Leffler 	       + sizeof(uint16_t)
2047b032f27cSSam Leffler 	       + 2 + IEEE80211_NWID_LEN
2048b032f27cSSam Leffler 	       + 2 + IEEE80211_RATE_SIZE
2049b032f27cSSam Leffler 	       + 7	/* max(7,3) */
2050b032f27cSSam Leffler 	       + IEEE80211_COUNTRY_MAX_SIZE
2051b032f27cSSam Leffler 	       + 3
2052b032f27cSSam Leffler 	       + sizeof(struct ieee80211_csa_ie)
2053b032f27cSSam Leffler 	       + 3
2054b032f27cSSam Leffler 	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2055688fe74dSSam Leffler 	       + sizeof(struct ieee80211_ie_wpa)
2056b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_htcap)
2057b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_htinfo)
2058b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_wpa)
2059b032f27cSSam Leffler 	       + sizeof(struct ieee80211_wme_param)
2060b032f27cSSam Leffler 	       + 4 + sizeof(struct ieee80211_ie_htcap)
2061b032f27cSSam Leffler 	       + 4 + sizeof(struct ieee80211_ie_htinfo)
2062616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
2063b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ath_ie)
2064616190d0SSam Leffler #endif
2065b032f27cSSam Leffler 	       + (vap->iv_appie_proberesp != NULL ?
2066b032f27cSSam Leffler 			vap->iv_appie_proberesp->ie_len : 0)
2067b032f27cSSam Leffler 	);
2068b032f27cSSam Leffler 	if (m == NULL) {
2069b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
2070b032f27cSSam Leffler 		return NULL;
2071b032f27cSSam Leffler 	}
2072b032f27cSSam Leffler 
2073b032f27cSSam Leffler 	memset(frm, 0, 8);	/* timestamp should be filled later */
2074b032f27cSSam Leffler 	frm += 8;
2075b032f27cSSam Leffler 	*(uint16_t *)frm = htole16(bss->ni_intval);
2076b032f27cSSam Leffler 	frm += 2;
2077b032f27cSSam Leffler 	capinfo = getcapinfo(vap, bss->ni_chan);
2078b032f27cSSam Leffler 	*(uint16_t *)frm = htole16(capinfo);
2079b032f27cSSam Leffler 	frm += 2;
2080b032f27cSSam Leffler 
2081b032f27cSSam Leffler 	frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
2082b032f27cSSam Leffler 	rs = ieee80211_get_suprates(ic, bss->ni_chan);
2083b032f27cSSam Leffler 	frm = ieee80211_add_rates(frm, rs);
2084b032f27cSSam Leffler 
2085b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
2086b032f27cSSam Leffler 		*frm++ = IEEE80211_ELEMID_FHPARMS;
2087b032f27cSSam Leffler 		*frm++ = 5;
2088b032f27cSSam Leffler 		*frm++ = bss->ni_fhdwell & 0x00ff;
2089b032f27cSSam Leffler 		*frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
2090b032f27cSSam Leffler 		*frm++ = IEEE80211_FH_CHANSET(
2091b032f27cSSam Leffler 		    ieee80211_chan2ieee(ic, bss->ni_chan));
2092b032f27cSSam Leffler 		*frm++ = IEEE80211_FH_CHANPAT(
2093b032f27cSSam Leffler 		    ieee80211_chan2ieee(ic, bss->ni_chan));
2094b032f27cSSam Leffler 		*frm++ = bss->ni_fhindex;
2095b032f27cSSam Leffler 	} else {
2096b032f27cSSam Leffler 		*frm++ = IEEE80211_ELEMID_DSPARMS;
2097b032f27cSSam Leffler 		*frm++ = 1;
2098b032f27cSSam Leffler 		*frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
2099b032f27cSSam Leffler 	}
2100b032f27cSSam Leffler 
2101b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
2102b032f27cSSam Leffler 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
2103b032f27cSSam Leffler 		*frm++ = 2;
2104b032f27cSSam Leffler 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
2105b032f27cSSam Leffler 	}
2106b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2107b032f27cSSam Leffler 	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2108b032f27cSSam Leffler 		frm = ieee80211_add_countryie(frm, ic);
2109b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_DOTH) {
2110b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
2111b032f27cSSam Leffler 			frm = ieee80211_add_powerconstraint(frm, vap);
2112b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2113b032f27cSSam Leffler 			frm = ieee80211_add_csa(frm, vap);
2114b032f27cSSam Leffler 	}
2115b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
2116b032f27cSSam Leffler 		frm = ieee80211_add_erp(frm, ic);
2117b032f27cSSam Leffler 	frm = ieee80211_add_xrates(frm, rs);
2118688fe74dSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA2) {
2119688fe74dSSam Leffler 		if (vap->iv_rsn_ie != NULL)
2120688fe74dSSam Leffler 			frm = add_ie(frm, vap->iv_rsn_ie);
2121688fe74dSSam Leffler 		/* XXX else complain? */
2122688fe74dSSam Leffler 	}
2123b032f27cSSam Leffler 	/*
2124b032f27cSSam Leffler 	 * NB: legacy 11b clients do not get certain ie's.
2125b032f27cSSam Leffler 	 *     The caller identifies such clients by passing
2126b032f27cSSam Leffler 	 *     a token in legacy to us.  Could expand this to be
2127b032f27cSSam Leffler 	 *     any legacy client for stuff like HT ie's.
2128b032f27cSSam Leffler 	 */
2129b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2130b032f27cSSam Leffler 	    legacy != IEEE80211_SEND_LEGACY_11B) {
2131b032f27cSSam Leffler 		frm = ieee80211_add_htcap(frm, bss);
2132b032f27cSSam Leffler 		frm = ieee80211_add_htinfo(frm, bss);
2133b032f27cSSam Leffler 	}
2134b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA1) {
2135b032f27cSSam Leffler 		if (vap->iv_wpa_ie != NULL)
2136b032f27cSSam Leffler 			frm = add_ie(frm, vap->iv_wpa_ie);
2137b032f27cSSam Leffler 		/* XXX else complain? */
2138b032f27cSSam Leffler 	}
2139b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WME)
2140b032f27cSSam Leffler 		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2141b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2142b032f27cSSam Leffler 	    (vap->iv_flags_ext & IEEE80211_FEXT_HTCOMPAT) &&
2143b032f27cSSam Leffler 	    legacy != IEEE80211_SEND_LEGACY_11B) {
2144b032f27cSSam Leffler 		frm = ieee80211_add_htcap_vendor(frm, bss);
2145b032f27cSSam Leffler 		frm = ieee80211_add_htinfo_vendor(frm, bss);
2146b032f27cSSam Leffler 	}
2147616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
21484207227cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
21494207227cSSam Leffler 	    legacy != IEEE80211_SEND_LEGACY_11B)
21504207227cSSam Leffler 		frm = ieee80211_add_athcaps(frm, bss);
2151616190d0SSam Leffler #endif
2152b032f27cSSam Leffler 	if (vap->iv_appie_proberesp != NULL)
2153b032f27cSSam Leffler 		frm = add_appie(frm, vap->iv_appie_proberesp);
2154b032f27cSSam Leffler 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2155b032f27cSSam Leffler 
2156b032f27cSSam Leffler 	return m;
2157b032f27cSSam Leffler }
2158b032f27cSSam Leffler 
2159b032f27cSSam Leffler /*
2160b032f27cSSam Leffler  * Send a probe response frame to the specified mac address.
2161b032f27cSSam Leffler  * This does not go through the normal mgt frame api so we
2162b032f27cSSam Leffler  * can specify the destination address and re-use the bss node
2163b032f27cSSam Leffler  * for the sta reference.
2164b032f27cSSam Leffler  */
2165b032f27cSSam Leffler int
2166b032f27cSSam Leffler ieee80211_send_proberesp(struct ieee80211vap *vap,
2167b032f27cSSam Leffler 	const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
2168b032f27cSSam Leffler {
2169b032f27cSSam Leffler 	struct ieee80211_node *bss = vap->iv_bss;
2170b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2171b032f27cSSam Leffler 	struct ieee80211_frame *wh;
2172b032f27cSSam Leffler 	struct mbuf *m;
2173b032f27cSSam Leffler 
2174b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
2175b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
2176b032f27cSSam Leffler 		    "block %s frame in CAC state", "probe response");
2177b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
2178b032f27cSSam Leffler 		return EIO;		/* XXX */
2179b032f27cSSam Leffler 	}
2180b032f27cSSam Leffler 
2181b032f27cSSam Leffler 	/*
2182b032f27cSSam Leffler 	 * Hold a reference on the node so it doesn't go away until after
2183b032f27cSSam Leffler 	 * the xmit is complete all the way in the driver.  On error we
2184b032f27cSSam Leffler 	 * will remove our reference.
2185b032f27cSSam Leffler 	 */
2186b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2187b032f27cSSam Leffler 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2188b032f27cSSam Leffler 	    __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
2189b032f27cSSam Leffler 	    ieee80211_node_refcnt(bss)+1);
2190b032f27cSSam Leffler 	ieee80211_ref_node(bss);
2191b032f27cSSam Leffler 
2192b032f27cSSam Leffler 	m = ieee80211_alloc_proberesp(bss, legacy);
2193b032f27cSSam Leffler 	if (m == NULL) {
2194b032f27cSSam Leffler 		ieee80211_free_node(bss);
2195b032f27cSSam Leffler 		return ENOMEM;
2196b032f27cSSam Leffler 	}
2197b032f27cSSam Leffler 
2198b032f27cSSam Leffler 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
2199b032f27cSSam Leffler 	KASSERT(m != NULL, ("no room for header"));
2200b032f27cSSam Leffler 
2201b032f27cSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
22029e80b1dfSSam Leffler 	ieee80211_send_setup(bss, m,
2203b032f27cSSam Leffler 	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
22048ac160cdSSam Leffler 	     IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
2205b032f27cSSam Leffler 	/* XXX power management? */
2206c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
2207b032f27cSSam Leffler 
2208b032f27cSSam Leffler 	M_WME_SETAC(m, WME_AC_BE);
2209b032f27cSSam Leffler 
2210b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2211b032f27cSSam Leffler 	    "send probe resp on channel %u to %s%s\n",
2212b032f27cSSam Leffler 	    ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
2213b032f27cSSam Leffler 	    legacy ? " <legacy>" : "");
2214b032f27cSSam Leffler 	IEEE80211_NODE_STAT(bss, tx_mgmt);
2215b032f27cSSam Leffler 
2216b032f27cSSam Leffler 	return ic->ic_raw_xmit(bss, m, NULL);
2217b032f27cSSam Leffler }
2218b032f27cSSam Leffler 
2219b032f27cSSam Leffler /*
2220b032f27cSSam Leffler  * Allocate and build a RTS (Request To Send) control frame.
2221b032f27cSSam Leffler  */
2222b032f27cSSam Leffler struct mbuf *
2223b032f27cSSam Leffler ieee80211_alloc_rts(struct ieee80211com *ic,
2224b032f27cSSam Leffler 	const uint8_t ra[IEEE80211_ADDR_LEN],
2225b032f27cSSam Leffler 	const uint8_t ta[IEEE80211_ADDR_LEN],
2226b032f27cSSam Leffler 	uint16_t dur)
2227b032f27cSSam Leffler {
2228b032f27cSSam Leffler 	struct ieee80211_frame_rts *rts;
2229b032f27cSSam Leffler 	struct mbuf *m;
2230b032f27cSSam Leffler 
2231b032f27cSSam Leffler 	/* XXX honor ic_headroom */
2232b032f27cSSam Leffler 	m = m_gethdr(M_DONTWAIT, MT_DATA);
2233b032f27cSSam Leffler 	if (m != NULL) {
2234b032f27cSSam Leffler 		rts = mtod(m, struct ieee80211_frame_rts *);
2235b032f27cSSam Leffler 		rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2236b032f27cSSam Leffler 			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
2237b032f27cSSam Leffler 		rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2238b032f27cSSam Leffler 		*(u_int16_t *)rts->i_dur = htole16(dur);
2239b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(rts->i_ra, ra);
2240b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(rts->i_ta, ta);
2241b032f27cSSam Leffler 
2242b032f27cSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
2243b032f27cSSam Leffler 	}
2244b032f27cSSam Leffler 	return m;
2245b032f27cSSam Leffler }
2246b032f27cSSam Leffler 
2247b032f27cSSam Leffler /*
2248b032f27cSSam Leffler  * Allocate and build a CTS (Clear To Send) control frame.
2249b032f27cSSam Leffler  */
2250b032f27cSSam Leffler struct mbuf *
2251b032f27cSSam Leffler ieee80211_alloc_cts(struct ieee80211com *ic,
2252b032f27cSSam Leffler 	const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
2253b032f27cSSam Leffler {
2254b032f27cSSam Leffler 	struct ieee80211_frame_cts *cts;
2255b032f27cSSam Leffler 	struct mbuf *m;
2256b032f27cSSam Leffler 
2257b032f27cSSam Leffler 	/* XXX honor ic_headroom */
2258b032f27cSSam Leffler 	m = m_gethdr(M_DONTWAIT, MT_DATA);
2259b032f27cSSam Leffler 	if (m != NULL) {
2260b032f27cSSam Leffler 		cts = mtod(m, struct ieee80211_frame_cts *);
2261b032f27cSSam Leffler 		cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2262b032f27cSSam Leffler 			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
2263b032f27cSSam Leffler 		cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2264b032f27cSSam Leffler 		*(u_int16_t *)cts->i_dur = htole16(dur);
2265b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(cts->i_ra, ra);
2266b032f27cSSam Leffler 
2267b032f27cSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
2268b032f27cSSam Leffler 	}
2269b032f27cSSam Leffler 	return m;
2270b032f27cSSam Leffler }
2271b032f27cSSam Leffler 
227268e8e04eSSam Leffler static void
227368e8e04eSSam Leffler ieee80211_tx_mgt_timeout(void *arg)
227468e8e04eSSam Leffler {
227568e8e04eSSam Leffler 	struct ieee80211_node *ni = arg;
2276b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
227768e8e04eSSam Leffler 
2278b032f27cSSam Leffler 	if (vap->iv_state != IEEE80211_S_INIT &&
2279b032f27cSSam Leffler 	    (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
228068e8e04eSSam Leffler 		/*
228168e8e04eSSam Leffler 		 * NB: it's safe to specify a timeout as the reason here;
228268e8e04eSSam Leffler 		 *     it'll only be used in the right state.
228368e8e04eSSam Leffler 		 */
2284b032f27cSSam Leffler 		ieee80211_new_state(vap, IEEE80211_S_SCAN,
228568e8e04eSSam Leffler 			IEEE80211_SCAN_FAIL_TIMEOUT);
228668e8e04eSSam Leffler 	}
228768e8e04eSSam Leffler }
228868e8e04eSSam Leffler 
228968e8e04eSSam Leffler static void
229068e8e04eSSam Leffler ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
229168e8e04eSSam Leffler {
2292b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
229368e8e04eSSam Leffler 	enum ieee80211_state ostate = (enum ieee80211_state) arg;
229468e8e04eSSam Leffler 
229568e8e04eSSam Leffler 	/*
229668e8e04eSSam Leffler 	 * Frame transmit completed; arrange timer callback.  If
229768e8e04eSSam Leffler 	 * transmit was successfuly we wait for response.  Otherwise
229868e8e04eSSam Leffler 	 * we arrange an immediate callback instead of doing the
229968e8e04eSSam Leffler 	 * callback directly since we don't know what state the driver
230068e8e04eSSam Leffler 	 * is in (e.g. what locks it is holding).  This work should
230168e8e04eSSam Leffler 	 * not be too time-critical and not happen too often so the
230268e8e04eSSam Leffler 	 * added overhead is acceptable.
230368e8e04eSSam Leffler 	 *
230468e8e04eSSam Leffler 	 * XXX what happens if !acked but response shows up before callback?
230568e8e04eSSam Leffler 	 */
2306b032f27cSSam Leffler 	if (vap->iv_state == ostate)
2307b032f27cSSam Leffler 		callout_reset(&vap->iv_mgtsend,
230868e8e04eSSam Leffler 			status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
230968e8e04eSSam Leffler 			ieee80211_tx_mgt_timeout, ni);
231068e8e04eSSam Leffler }
231168e8e04eSSam Leffler 
2312b032f27cSSam Leffler static void
2313b032f27cSSam Leffler ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
2314b032f27cSSam Leffler 	struct ieee80211_beacon_offsets *bo, struct ieee80211_node *ni)
23158a1b9b6aSSam Leffler {
2316b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2317b105a069SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2318b032f27cSSam Leffler 	struct ieee80211_rateset *rs = &ni->ni_rates;
231968e8e04eSSam Leffler 	uint16_t capinfo;
23208a1b9b6aSSam Leffler 
23218a1b9b6aSSam Leffler 	/*
23228a1b9b6aSSam Leffler 	 * beacon frame format
23238a1b9b6aSSam Leffler 	 *	[8] time stamp
23248a1b9b6aSSam Leffler 	 *	[2] beacon interval
23258a1b9b6aSSam Leffler 	 *	[2] cabability information
23268a1b9b6aSSam Leffler 	 *	[tlv] ssid
23278a1b9b6aSSam Leffler 	 *	[tlv] supported rates
23288a1b9b6aSSam Leffler 	 *	[3] parameter set (DS)
2329b032f27cSSam Leffler 	 *	[8] CF parameter set (optional)
23308a1b9b6aSSam Leffler 	 *	[tlv] parameter set (IBSS/TIM)
2331b032f27cSSam Leffler 	 *	[tlv] country (optional)
2332b032f27cSSam Leffler 	 *	[3] power control (optional)
2333b032f27cSSam Leffler 	 *	[5] channel switch announcement (CSA) (optional)
23348a1b9b6aSSam Leffler 	 *	[tlv] extended rate phy (ERP)
23358a1b9b6aSSam Leffler 	 *	[tlv] extended supported rates
2336688fe74dSSam Leffler 	 *	[tlv] RSN parameters
233768e8e04eSSam Leffler 	 *	[tlv] HT capabilities
233868e8e04eSSam Leffler 	 *	[tlv] HT information
2339b032f27cSSam Leffler 	 * XXX Vendor-specific OIDs (e.g. Atheros)
2340b032f27cSSam Leffler 	 *	[tlv] WPA parameters
2341b032f27cSSam Leffler 	 *	[tlv] WME parameters
234268e8e04eSSam Leffler 	 *	[tlv] Vendor OUI HT capabilities (optional)
234368e8e04eSSam Leffler 	 *	[tlv] Vendor OUI HT information (optional)
23444207227cSSam Leffler 	 *	[tlv] Atheros capabilities (optional)
234510ad9a77SSam Leffler 	 *	[tlv] TDMA parameters (optional)
2346b032f27cSSam Leffler 	 *	[tlv] application data (optional)
23478a1b9b6aSSam Leffler 	 */
23488a1b9b6aSSam Leffler 
23491b6167d2SSam Leffler 	memset(bo, 0, sizeof(*bo));
23501b6167d2SSam Leffler 
23518a1b9b6aSSam Leffler 	memset(frm, 0, 8);	/* XXX timestamp is set by hardware/driver */
23528a1b9b6aSSam Leffler 	frm += 8;
235368e8e04eSSam Leffler 	*(uint16_t *)frm = htole16(ni->ni_intval);
23548a1b9b6aSSam Leffler 	frm += 2;
2355b032f27cSSam Leffler 	capinfo = getcapinfo(vap, ni->ni_chan);
235668e8e04eSSam Leffler 	bo->bo_caps = (uint16_t *)frm;
235768e8e04eSSam Leffler 	*(uint16_t *)frm = htole16(capinfo);
23588a1b9b6aSSam Leffler 	frm += 2;
23598a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_SSID;
2360b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
23618a1b9b6aSSam Leffler 		*frm++ = ni->ni_esslen;
23628a1b9b6aSSam Leffler 		memcpy(frm, ni->ni_essid, ni->ni_esslen);
23638a1b9b6aSSam Leffler 		frm += ni->ni_esslen;
23648a1b9b6aSSam Leffler 	} else
23658a1b9b6aSSam Leffler 		*frm++ = 0;
23668a1b9b6aSSam Leffler 	frm = ieee80211_add_rates(frm, rs);
2367b032f27cSSam Leffler 	if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
23688a1b9b6aSSam Leffler 		*frm++ = IEEE80211_ELEMID_DSPARMS;
23698a1b9b6aSSam Leffler 		*frm++ = 1;
2370b032f27cSSam Leffler 		*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2371b032f27cSSam Leffler 	}
2372b032f27cSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PCF) {
2373b032f27cSSam Leffler 		bo->bo_cfp = frm;
2374b032f27cSSam Leffler 		frm = ieee80211_add_cfparms(frm, ic);
23758a1b9b6aSSam Leffler 	}
23768a1b9b6aSSam Leffler 	bo->bo_tim = frm;
2377b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
23788a1b9b6aSSam Leffler 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
23798a1b9b6aSSam Leffler 		*frm++ = 2;
23808a1b9b6aSSam Leffler 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
23818a1b9b6aSSam Leffler 		bo->bo_tim_len = 0;
2382b032f27cSSam Leffler 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
23838a1b9b6aSSam Leffler 		struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
23848a1b9b6aSSam Leffler 
23858a1b9b6aSSam Leffler 		tie->tim_ie = IEEE80211_ELEMID_TIM;
23868a1b9b6aSSam Leffler 		tie->tim_len = 4;	/* length */
23878a1b9b6aSSam Leffler 		tie->tim_count = 0;	/* DTIM count */
2388b032f27cSSam Leffler 		tie->tim_period = vap->iv_dtim_period;	/* DTIM period */
23898a1b9b6aSSam Leffler 		tie->tim_bitctl = 0;	/* bitmap control */
23908a1b9b6aSSam Leffler 		tie->tim_bitmap[0] = 0;	/* Partial Virtual Bitmap */
23918a1b9b6aSSam Leffler 		frm += sizeof(struct ieee80211_tim_ie);
23928a1b9b6aSSam Leffler 		bo->bo_tim_len = 1;
23938a1b9b6aSSam Leffler 	}
2394b105a069SSam Leffler 	bo->bo_tim_trailer = frm;
2395b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2396b032f27cSSam Leffler 	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2397b032f27cSSam Leffler 		frm = ieee80211_add_countryie(frm, ic);
2398b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_DOTH) {
2399b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
2400b032f27cSSam Leffler 			frm = ieee80211_add_powerconstraint(frm, vap);
2401b032f27cSSam Leffler 		bo->bo_csa = frm;
2402b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2403b032f27cSSam Leffler 			frm = ieee80211_add_csa(frm, vap);
2404b032f27cSSam Leffler 	} else
2405b032f27cSSam Leffler 		bo->bo_csa = frm;
2406b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
24070912bac9SSam Leffler 		bo->bo_erp = frm;
24088a1b9b6aSSam Leffler 		frm = ieee80211_add_erp(frm, ic);
24091b6167d2SSam Leffler 	}
241068e8e04eSSam Leffler 	frm = ieee80211_add_xrates(frm, rs);
2411688fe74dSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA2) {
2412688fe74dSSam Leffler 		if (vap->iv_rsn_ie != NULL)
2413688fe74dSSam Leffler 			frm = add_ie(frm, vap->iv_rsn_ie);
2414688fe74dSSam Leffler 		/* XXX else complain */
2415688fe74dSSam Leffler 	}
2416b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
241768e8e04eSSam Leffler 		frm = ieee80211_add_htcap(frm, ni);
241868e8e04eSSam Leffler 		bo->bo_htinfo = frm;
241968e8e04eSSam Leffler 		frm = ieee80211_add_htinfo(frm, ni);
24201b6167d2SSam Leffler 	}
2421b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA1) {
2422b032f27cSSam Leffler 		if (vap->iv_wpa_ie != NULL)
2423b032f27cSSam Leffler 			frm = add_ie(frm, vap->iv_wpa_ie);
2424b032f27cSSam Leffler 		/* XXX else complain */
2425b032f27cSSam Leffler 	}
2426b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WME) {
24271b6167d2SSam Leffler 		bo->bo_wme = frm;
24281b6167d2SSam Leffler 		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
24291b6167d2SSam Leffler 	}
2430b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2431b032f27cSSam Leffler 	    (vap->iv_flags_ext & IEEE80211_FEXT_HTCOMPAT)) {
243268e8e04eSSam Leffler 		frm = ieee80211_add_htcap_vendor(frm, ni);
243368e8e04eSSam Leffler 		frm = ieee80211_add_htinfo_vendor(frm, ni);
24340912bac9SSam Leffler 	}
24354207227cSSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
24364207227cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_ATHEROS) {
24374207227cSSam Leffler 		bo->bo_ath = frm;
24384207227cSSam Leffler 		frm = ieee80211_add_athcaps(frm, ni);
24394207227cSSam Leffler 	}
24404207227cSSam Leffler #endif
244110ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
244210ad9a77SSam Leffler 	if (vap->iv_caps & IEEE80211_C_TDMA) {
244310ad9a77SSam Leffler 		bo->bo_tdma = frm;
244410ad9a77SSam Leffler 		frm = ieee80211_add_tdma(frm, vap);
244510ad9a77SSam Leffler 	}
244610ad9a77SSam Leffler #endif
2447b032f27cSSam Leffler 	if (vap->iv_appie_beacon != NULL) {
2448b032f27cSSam Leffler 		bo->bo_appie = frm;
2449b032f27cSSam Leffler 		bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
2450b032f27cSSam Leffler 		frm = add_appie(frm, vap->iv_appie_beacon);
2451b032f27cSSam Leffler 	}
2452b105a069SSam Leffler 	bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
2453b032f27cSSam Leffler 	bo->bo_csa_trailer_len = frm - bo->bo_csa;
245468e8e04eSSam Leffler 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2455b032f27cSSam Leffler }
2456b032f27cSSam Leffler 
2457b032f27cSSam Leffler /*
2458b032f27cSSam Leffler  * Allocate a beacon frame and fillin the appropriate bits.
2459b032f27cSSam Leffler  */
2460b032f27cSSam Leffler struct mbuf *
2461b032f27cSSam Leffler ieee80211_beacon_alloc(struct ieee80211_node *ni,
2462b032f27cSSam Leffler 	struct ieee80211_beacon_offsets *bo)
2463b032f27cSSam Leffler {
2464b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2465b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2466b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
2467b032f27cSSam Leffler 	struct ieee80211_frame *wh;
2468b032f27cSSam Leffler 	struct mbuf *m;
2469b032f27cSSam Leffler 	int pktlen;
2470b032f27cSSam Leffler 	uint8_t *frm;
2471b032f27cSSam Leffler 
2472b032f27cSSam Leffler 	/*
2473b032f27cSSam Leffler 	 * beacon frame format
2474b032f27cSSam Leffler 	 *	[8] time stamp
2475b032f27cSSam Leffler 	 *	[2] beacon interval
2476b032f27cSSam Leffler 	 *	[2] cabability information
2477b032f27cSSam Leffler 	 *	[tlv] ssid
2478b032f27cSSam Leffler 	 *	[tlv] supported rates
2479b032f27cSSam Leffler 	 *	[3] parameter set (DS)
2480b032f27cSSam Leffler 	 *	[8] CF parameter set (optional)
2481b032f27cSSam Leffler 	 *	[tlv] parameter set (IBSS/TIM)
2482b032f27cSSam Leffler 	 *	[tlv] country (optional)
2483b032f27cSSam Leffler 	 *	[3] power control (optional)
2484b032f27cSSam Leffler 	 *	[5] channel switch announcement (CSA) (optional)
2485b032f27cSSam Leffler 	 *	[tlv] extended rate phy (ERP)
2486b032f27cSSam Leffler 	 *	[tlv] extended supported rates
2487b032f27cSSam Leffler 	 *	[tlv] RSN parameters
2488b032f27cSSam Leffler 	 *	[tlv] HT capabilities
2489b032f27cSSam Leffler 	 *	[tlv] HT information
2490b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT capabilities (optional)
2491b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT information (optional)
2492b032f27cSSam Leffler 	 * XXX Vendor-specific OIDs (e.g. Atheros)
2493b032f27cSSam Leffler 	 *	[tlv] WPA parameters
2494b032f27cSSam Leffler 	 *	[tlv] WME parameters
249510ad9a77SSam Leffler 	 *	[tlv] TDMA parameters (optional)
2496b032f27cSSam Leffler 	 *	[tlv] application data (optional)
2497b032f27cSSam Leffler 	 * NB: we allocate the max space required for the TIM bitmap.
2498b032f27cSSam Leffler 	 * XXX how big is this?
2499b032f27cSSam Leffler 	 */
2500b032f27cSSam Leffler 	pktlen =   8					/* time stamp */
2501b032f27cSSam Leffler 		 + sizeof(uint16_t)			/* beacon interval */
2502b032f27cSSam Leffler 		 + sizeof(uint16_t)			/* capabilities */
2503b032f27cSSam Leffler 		 + 2 + ni->ni_esslen			/* ssid */
2504b032f27cSSam Leffler 	         + 2 + IEEE80211_RATE_SIZE		/* supported rates */
2505b032f27cSSam Leffler 	         + 2 + 1				/* DS parameters */
2506b032f27cSSam Leffler 		 + 2 + 6				/* CF parameters */
2507b032f27cSSam Leffler 		 + 2 + 4 + vap->iv_tim_len		/* DTIM/IBSSPARMS */
2508b032f27cSSam Leffler 		 + IEEE80211_COUNTRY_MAX_SIZE		/* country */
2509b032f27cSSam Leffler 		 + 2 + 1				/* power control */
2510b032f27cSSam Leffler 	         + sizeof(struct ieee80211_csa_ie)	/* CSA */
2511b032f27cSSam Leffler 		 + 2 + 1				/* ERP */
2512b032f27cSSam Leffler 	         + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2513b032f27cSSam Leffler 		 + (vap->iv_caps & IEEE80211_C_WPA ?	/* WPA 1+2 */
2514b032f27cSSam Leffler 			2*sizeof(struct ieee80211_ie_wpa) : 0)
2515b032f27cSSam Leffler 		 /* XXX conditional? */
2516b032f27cSSam Leffler 		 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
2517b032f27cSSam Leffler 		 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
2518b032f27cSSam Leffler 		 + (vap->iv_caps & IEEE80211_C_WME ?	/* WME */
2519b032f27cSSam Leffler 			sizeof(struct ieee80211_wme_param) : 0)
25204207227cSSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
25214207227cSSam Leffler 		 + sizeof(struct ieee80211_ath_ie)	/* ATH */
25224207227cSSam Leffler #endif
252310ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
252410ad9a77SSam Leffler 		 + (vap->iv_caps & IEEE80211_C_TDMA ?	/* TDMA */
252510ad9a77SSam Leffler 			sizeof(struct ieee80211_tdma_param) : 0)
252610ad9a77SSam Leffler #endif
2527b032f27cSSam Leffler 		 + IEEE80211_MAX_APPIE
2528b032f27cSSam Leffler 		 ;
2529b032f27cSSam Leffler 	m = ieee80211_getmgtframe(&frm,
2530b032f27cSSam Leffler 		ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
2531b032f27cSSam Leffler 	if (m == NULL) {
2532b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
2533b032f27cSSam Leffler 			"%s: cannot get buf; size %u\n", __func__, pktlen);
2534b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
2535b032f27cSSam Leffler 		return NULL;
2536b032f27cSSam Leffler 	}
2537b032f27cSSam Leffler 	ieee80211_beacon_construct(m, frm, bo, ni);
25388a1b9b6aSSam Leffler 
25398a1b9b6aSSam Leffler 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
25408a1b9b6aSSam Leffler 	KASSERT(m != NULL, ("no space for 802.11 header?"));
25418a1b9b6aSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
25428a1b9b6aSSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
25438a1b9b6aSSam Leffler 	    IEEE80211_FC0_SUBTYPE_BEACON;
25448a1b9b6aSSam Leffler 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
254568e8e04eSSam Leffler 	*(uint16_t *)wh->i_dur = 0;
25468a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
2547b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
25488a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
254968e8e04eSSam Leffler 	*(uint16_t *)wh->i_seq = 0;
25508a1b9b6aSSam Leffler 
25518a1b9b6aSSam Leffler 	return m;
25528a1b9b6aSSam Leffler }
25538a1b9b6aSSam Leffler 
25548a1b9b6aSSam Leffler /*
25558a1b9b6aSSam Leffler  * Update the dynamic parts of a beacon frame based on the current state.
25568a1b9b6aSSam Leffler  */
25578a1b9b6aSSam Leffler int
2558b105a069SSam Leffler ieee80211_beacon_update(struct ieee80211_node *ni,
25598a1b9b6aSSam Leffler 	struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
25608a1b9b6aSSam Leffler {
2561b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2562b105a069SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
25638a1b9b6aSSam Leffler 	int len_changed = 0;
256468e8e04eSSam Leffler 	uint16_t capinfo;
25658a1b9b6aSSam Leffler 
2566b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2567b032f27cSSam Leffler 	/*
2568b032f27cSSam Leffler 	 * Handle 11h channel change when we've reached the count.
2569b032f27cSSam Leffler 	 * We must recalculate the beacon frame contents to account
2570b032f27cSSam Leffler 	 * for the new channel.  Note we do this only for the first
2571b032f27cSSam Leffler 	 * vap that reaches this point; subsequent vaps just update
2572b032f27cSSam Leffler 	 * their beacon state to reflect the recalculated channel.
2573b032f27cSSam Leffler 	 */
2574b032f27cSSam Leffler 	if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
2575b032f27cSSam Leffler 	    vap->iv_csa_count == ic->ic_csa_count) {
2576b032f27cSSam Leffler 		vap->iv_csa_count = 0;
2577b032f27cSSam Leffler 		/*
2578b032f27cSSam Leffler 		 * Effect channel change before reconstructing the beacon
2579b032f27cSSam Leffler 		 * frame contents as many places reference ni_chan.
2580b032f27cSSam Leffler 		 */
2581b032f27cSSam Leffler 		if (ic->ic_csa_newchan != NULL)
2582b032f27cSSam Leffler 			ieee80211_csa_completeswitch(ic);
2583b032f27cSSam Leffler 		/*
2584b032f27cSSam Leffler 		 * NB: ieee80211_beacon_construct clears all pending
2585b032f27cSSam Leffler 		 * updates in bo_flags so we don't need to explicitly
2586b032f27cSSam Leffler 		 * clear IEEE80211_BEACON_CSA.
2587b032f27cSSam Leffler 		 */
2588b032f27cSSam Leffler 		ieee80211_beacon_construct(m,
2589b032f27cSSam Leffler 		    mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), bo, ni);
2590b032f27cSSam Leffler 
2591b032f27cSSam Leffler 		/* XXX do WME aggressive mode processing? */
2592b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
2593b032f27cSSam Leffler 		return 1;		/* just assume length changed */
2594b032f27cSSam Leffler 	}
2595b032f27cSSam Leffler 
25968a1b9b6aSSam Leffler 	/* XXX faster to recalculate entirely or just changes? */
2597b032f27cSSam Leffler 	capinfo = getcapinfo(vap, ni->ni_chan);
25988a1b9b6aSSam Leffler 	*bo->bo_caps = htole16(capinfo);
25998a1b9b6aSSam Leffler 
2600b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WME) {
26018a1b9b6aSSam Leffler 		struct ieee80211_wme_state *wme = &ic->ic_wme;
26028a1b9b6aSSam Leffler 
26038a1b9b6aSSam Leffler 		/*
26048a1b9b6aSSam Leffler 		 * Check for agressive mode change.  When there is
26058a1b9b6aSSam Leffler 		 * significant high priority traffic in the BSS
26068a1b9b6aSSam Leffler 		 * throttle back BE traffic by using conservative
26078a1b9b6aSSam Leffler 		 * parameters.  Otherwise BE uses agressive params
26088a1b9b6aSSam Leffler 		 * to optimize performance of legacy/non-QoS traffic.
26098a1b9b6aSSam Leffler 		 */
26108a1b9b6aSSam Leffler 		if (wme->wme_flags & WME_F_AGGRMODE) {
26118a1b9b6aSSam Leffler 			if (wme->wme_hipri_traffic >
26128a1b9b6aSSam Leffler 			    wme->wme_hipri_switch_thresh) {
2613b032f27cSSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
26148a1b9b6aSSam Leffler 				    "%s: traffic %u, disable aggressive mode\n",
26158a1b9b6aSSam Leffler 				    __func__, wme->wme_hipri_traffic);
26168a1b9b6aSSam Leffler 				wme->wme_flags &= ~WME_F_AGGRMODE;
2617b032f27cSSam Leffler 				ieee80211_wme_updateparams_locked(vap);
26188a1b9b6aSSam Leffler 				wme->wme_hipri_traffic =
26198a1b9b6aSSam Leffler 					wme->wme_hipri_switch_hysteresis;
26208a1b9b6aSSam Leffler 			} else
26218a1b9b6aSSam Leffler 				wme->wme_hipri_traffic = 0;
26228a1b9b6aSSam Leffler 		} else {
26238a1b9b6aSSam Leffler 			if (wme->wme_hipri_traffic <=
26248a1b9b6aSSam Leffler 			    wme->wme_hipri_switch_thresh) {
2625b032f27cSSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
26268a1b9b6aSSam Leffler 				    "%s: traffic %u, enable aggressive mode\n",
26278a1b9b6aSSam Leffler 				    __func__, wme->wme_hipri_traffic);
26288a1b9b6aSSam Leffler 				wme->wme_flags |= WME_F_AGGRMODE;
2629b032f27cSSam Leffler 				ieee80211_wme_updateparams_locked(vap);
26308a1b9b6aSSam Leffler 				wme->wme_hipri_traffic = 0;
26318a1b9b6aSSam Leffler 			} else
26328a1b9b6aSSam Leffler 				wme->wme_hipri_traffic =
26338a1b9b6aSSam Leffler 					wme->wme_hipri_switch_hysteresis;
26348a1b9b6aSSam Leffler 		}
2635b105a069SSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
26368a1b9b6aSSam Leffler 			(void) ieee80211_add_wme_param(bo->bo_wme, wme);
2637b105a069SSam Leffler 			clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
26388a1b9b6aSSam Leffler 		}
26398a1b9b6aSSam Leffler 	}
26408a1b9b6aSSam Leffler 
2641b105a069SSam Leffler 	if (isset(bo->bo_flags,  IEEE80211_BEACON_HTINFO)) {
2642b032f27cSSam Leffler 		ieee80211_ht_update_beacon(vap, bo);
2643b105a069SSam Leffler 		clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
264468e8e04eSSam Leffler 	}
264510ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
264610ad9a77SSam Leffler 	if (vap->iv_caps & IEEE80211_C_TDMA) {
264710ad9a77SSam Leffler 		/*
264810ad9a77SSam Leffler 		 * NB: the beacon is potentially updated every TBTT.
264910ad9a77SSam Leffler 		 */
265010ad9a77SSam Leffler 		ieee80211_tdma_update_beacon(vap, bo);
265110ad9a77SSam Leffler 	}
265210ad9a77SSam Leffler #endif
2653b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {	/* NB: no IBSS support*/
26548a1b9b6aSSam Leffler 		struct ieee80211_tim_ie *tie =
26558a1b9b6aSSam Leffler 			(struct ieee80211_tim_ie *) bo->bo_tim;
2656b105a069SSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
26578a1b9b6aSSam Leffler 			u_int timlen, timoff, i;
26588a1b9b6aSSam Leffler 			/*
26598a1b9b6aSSam Leffler 			 * ATIM/DTIM needs updating.  If it fits in the
26608a1b9b6aSSam Leffler 			 * current space allocated then just copy in the
26618a1b9b6aSSam Leffler 			 * new bits.  Otherwise we need to move any trailing
26628a1b9b6aSSam Leffler 			 * data to make room.  Note that we know there is
26638a1b9b6aSSam Leffler 			 * contiguous space because ieee80211_beacon_allocate
26648a1b9b6aSSam Leffler 			 * insures there is space in the mbuf to write a
2665b032f27cSSam Leffler 			 * maximal-size virtual bitmap (based on iv_max_aid).
26668a1b9b6aSSam Leffler 			 */
26678a1b9b6aSSam Leffler 			/*
26688a1b9b6aSSam Leffler 			 * Calculate the bitmap size and offset, copy any
26698a1b9b6aSSam Leffler 			 * trailer out of the way, and then copy in the
26708a1b9b6aSSam Leffler 			 * new bitmap and update the information element.
26718a1b9b6aSSam Leffler 			 * Note that the tim bitmap must contain at least
26728a1b9b6aSSam Leffler 			 * one byte and any offset must be even.
26738a1b9b6aSSam Leffler 			 */
2674b032f27cSSam Leffler 			if (vap->iv_ps_pending != 0) {
26758a1b9b6aSSam Leffler 				timoff = 128;		/* impossibly large */
2676b032f27cSSam Leffler 				for (i = 0; i < vap->iv_tim_len; i++)
2677b032f27cSSam Leffler 					if (vap->iv_tim_bitmap[i]) {
26788a1b9b6aSSam Leffler 						timoff = i &~ 1;
26798a1b9b6aSSam Leffler 						break;
26808a1b9b6aSSam Leffler 					}
26818a1b9b6aSSam Leffler 				KASSERT(timoff != 128, ("tim bitmap empty!"));
2682b032f27cSSam Leffler 				for (i = vap->iv_tim_len-1; i >= timoff; i--)
2683b032f27cSSam Leffler 					if (vap->iv_tim_bitmap[i])
26848a1b9b6aSSam Leffler 						break;
26858a1b9b6aSSam Leffler 				timlen = 1 + (i - timoff);
26868a1b9b6aSSam Leffler 			} else {
26878a1b9b6aSSam Leffler 				timoff = 0;
26888a1b9b6aSSam Leffler 				timlen = 1;
26898a1b9b6aSSam Leffler 			}
26908a1b9b6aSSam Leffler 			if (timlen != bo->bo_tim_len) {
26918a1b9b6aSSam Leffler 				/* copy up/down trailer */
26920912bac9SSam Leffler 				int adjust = tie->tim_bitmap+timlen
2693b105a069SSam Leffler 					   - bo->bo_tim_trailer;
2694b105a069SSam Leffler 				ovbcopy(bo->bo_tim_trailer,
2695b105a069SSam Leffler 				    bo->bo_tim_trailer+adjust,
2696b105a069SSam Leffler 				    bo->bo_tim_trailer_len);
2697b105a069SSam Leffler 				bo->bo_tim_trailer += adjust;
26980912bac9SSam Leffler 				bo->bo_erp += adjust;
269968e8e04eSSam Leffler 				bo->bo_htinfo += adjust;
27004207227cSSam Leffler #ifdef IEEE80211_SUPERG_SUPPORT
27014207227cSSam Leffler 				bo->bo_ath += adjust;
27024207227cSSam Leffler #endif
270392e870edSSam Leffler #ifdef IEEE80211_TDMA_SUPPORT
270492e870edSSam Leffler 				bo->bo_tdma += adjust;
270592e870edSSam Leffler #endif
2706b032f27cSSam Leffler 				bo->bo_appie += adjust;
2707b032f27cSSam Leffler 				bo->bo_wme += adjust;
2708b032f27cSSam Leffler 				bo->bo_csa += adjust;
27098a1b9b6aSSam Leffler 				bo->bo_tim_len = timlen;
27108a1b9b6aSSam Leffler 
27118a1b9b6aSSam Leffler 				/* update information element */
27128a1b9b6aSSam Leffler 				tie->tim_len = 3 + timlen;
27138a1b9b6aSSam Leffler 				tie->tim_bitctl = timoff;
27148a1b9b6aSSam Leffler 				len_changed = 1;
27158a1b9b6aSSam Leffler 			}
2716b032f27cSSam Leffler 			memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
27178a1b9b6aSSam Leffler 				bo->bo_tim_len);
27188a1b9b6aSSam Leffler 
2719b105a069SSam Leffler 			clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
27208a1b9b6aSSam Leffler 
2721b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
27228a1b9b6aSSam Leffler 				"%s: TIM updated, pending %u, off %u, len %u\n",
2723b032f27cSSam Leffler 				__func__, vap->iv_ps_pending, timoff, timlen);
27248a1b9b6aSSam Leffler 		}
27258a1b9b6aSSam Leffler 		/* count down DTIM period */
27268a1b9b6aSSam Leffler 		if (tie->tim_count == 0)
27278a1b9b6aSSam Leffler 			tie->tim_count = tie->tim_period - 1;
27288a1b9b6aSSam Leffler 		else
27298a1b9b6aSSam Leffler 			tie->tim_count--;
27308a1b9b6aSSam Leffler 		/* update state for buffered multicast frames on DTIM */
2731a196b35fSSam Leffler 		if (mcast && tie->tim_count == 0)
27328a1b9b6aSSam Leffler 			tie->tim_bitctl |= 1;
27338a1b9b6aSSam Leffler 		else
27348a1b9b6aSSam Leffler 			tie->tim_bitctl &= ~1;
2735b032f27cSSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
2736b032f27cSSam Leffler 			struct ieee80211_csa_ie *csa =
2737b032f27cSSam Leffler 			    (struct ieee80211_csa_ie *) bo->bo_csa;
2738b032f27cSSam Leffler 
2739b032f27cSSam Leffler 			/*
2740b032f27cSSam Leffler 			 * Insert or update CSA ie.  If we're just starting
2741b032f27cSSam Leffler 			 * to count down to the channel switch then we need
2742b032f27cSSam Leffler 			 * to insert the CSA ie.  Otherwise we just need to
2743b032f27cSSam Leffler 			 * drop the count.  The actual change happens above
2744b032f27cSSam Leffler 			 * when the vap's count reaches the target count.
2745b032f27cSSam Leffler 			 */
2746b032f27cSSam Leffler 			if (vap->iv_csa_count == 0) {
2747b032f27cSSam Leffler 				memmove(&csa[1], csa, bo->bo_csa_trailer_len);
2748b032f27cSSam Leffler 				bo->bo_erp += sizeof(*csa);
2749d01b3c26SSam Leffler 				bo->bo_htinfo += sizeof(*csa);
2750b032f27cSSam Leffler 				bo->bo_wme += sizeof(*csa);
27514207227cSSam Leffler #ifdef IEEE80211_SUPERG_SUPPORT
27524207227cSSam Leffler 				bo->bo_ath += sizeof(*csa);
27534207227cSSam Leffler #endif
275492e870edSSam Leffler #ifdef IEEE80211_TDMA_SUPPORT
275592e870edSSam Leffler 				bo->bo_tdma += sizeof(*csa);
275692e870edSSam Leffler #endif
2757b032f27cSSam Leffler 				bo->bo_appie += sizeof(*csa);
2758b032f27cSSam Leffler 				bo->bo_csa_trailer_len += sizeof(*csa);
2759b032f27cSSam Leffler 				bo->bo_tim_trailer_len += sizeof(*csa);
2760b032f27cSSam Leffler 				m->m_len += sizeof(*csa);
2761b032f27cSSam Leffler 				m->m_pkthdr.len += sizeof(*csa);
2762b032f27cSSam Leffler 
2763b032f27cSSam Leffler 				ieee80211_add_csa(bo->bo_csa, vap);
2764b032f27cSSam Leffler 			} else
2765b032f27cSSam Leffler 				csa->csa_count--;
2766b032f27cSSam Leffler 			vap->iv_csa_count++;
2767b032f27cSSam Leffler 			/* NB: don't clear IEEE80211_BEACON_CSA */
2768b032f27cSSam Leffler 		}
2769b105a069SSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
27700912bac9SSam Leffler 			/*
27710912bac9SSam Leffler 			 * ERP element needs updating.
27720912bac9SSam Leffler 			 */
27730912bac9SSam Leffler 			(void) ieee80211_add_erp(bo->bo_erp, ic);
2774b105a069SSam Leffler 			clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
27750912bac9SSam Leffler 		}
27764207227cSSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
27774207227cSSam Leffler 		if (isset(bo->bo_flags,  IEEE80211_BEACON_ATH)) {
27784207227cSSam Leffler 			ieee80211_add_athcaps(bo->bo_ath, ni);
27794207227cSSam Leffler 			clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
27804207227cSSam Leffler 		}
27814207227cSSam Leffler #endif
27828a1b9b6aSSam Leffler 	}
2783b032f27cSSam Leffler 	if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
2784b032f27cSSam Leffler 		const struct ieee80211_appie *aie = vap->iv_appie_beacon;
2785b032f27cSSam Leffler 		int aielen;
2786b032f27cSSam Leffler 		uint8_t *frm;
2787b032f27cSSam Leffler 
2788b032f27cSSam Leffler 		aielen = 0;
2789b032f27cSSam Leffler 		if (aie != NULL)
2790b032f27cSSam Leffler 			aielen += aie->ie_len;
2791b032f27cSSam Leffler 		if (aielen != bo->bo_appie_len) {
2792b032f27cSSam Leffler 			/* copy up/down trailer */
2793b032f27cSSam Leffler 			int adjust = aielen - bo->bo_appie_len;
2794b032f27cSSam Leffler 			ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
2795b032f27cSSam Leffler 				bo->bo_tim_trailer_len);
2796b032f27cSSam Leffler 			bo->bo_tim_trailer += adjust;
2797b032f27cSSam Leffler 			bo->bo_appie += adjust;
2798b032f27cSSam Leffler 			bo->bo_appie_len = aielen;
2799b032f27cSSam Leffler 
2800b032f27cSSam Leffler 			len_changed = 1;
2801b032f27cSSam Leffler 		}
2802b032f27cSSam Leffler 		frm = bo->bo_appie;
2803b032f27cSSam Leffler 		if (aie != NULL)
2804b032f27cSSam Leffler 			frm  = add_appie(frm, aie);
2805b032f27cSSam Leffler 		clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
2806b032f27cSSam Leffler 	}
2807b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
28088a1b9b6aSSam Leffler 
28098a1b9b6aSSam Leffler 	return len_changed;
28108a1b9b6aSSam Leffler }
2811