xref: /freebsd/sys/net80211/ieee80211_output.c (revision 7ebe9c0e2e6c941541e87179f0c6fba21f614f3f)
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 
254339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
255339ccfb3SSam Leffler 		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF)) {
256339ccfb3SSam Leffler 			m = ieee80211_ff_check(ni, m);
257339ccfb3SSam Leffler 			if (m == NULL) {
258339ccfb3SSam Leffler 				/* NB: any ni ref held on stageq */
259339ccfb3SSam Leffler 				continue;
260339ccfb3SSam Leffler 			}
261339ccfb3SSam Leffler 		}
262339ccfb3SSam Leffler #endif /* IEEE80211_SUPPORT_SUPERG */
2639fb0fccbSSam Leffler 		if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
264b032f27cSSam Leffler 			/*
265339ccfb3SSam Leffler 			 * Encapsulate the packet in prep for transmission.
266b032f27cSSam Leffler 			 */
267339ccfb3SSam Leffler 			m = ieee80211_encap(vap, ni, m);
268339ccfb3SSam Leffler 			if (m == NULL) {
269339ccfb3SSam Leffler 				/* NB: stat+msg handled in ieee80211_encap */
270339ccfb3SSam Leffler 				ieee80211_free_node(ni);
271339ccfb3SSam Leffler 				continue;
272339ccfb3SSam Leffler 			}
2739fb0fccbSSam Leffler 		}
274b032f27cSSam Leffler 
27577825c77SSam Leffler 		/* XXX fragmented frames not handled */
27677825c77SSam Leffler 		if (bpf_peers_present(vap->iv_rawbpf))
27777825c77SSam Leffler 			bpf_mtap(vap->iv_rawbpf, m);
27877825c77SSam Leffler 
27972fecb4dSSam Leffler 		error = parent->if_transmit(parent, m);
280b032f27cSSam Leffler 		if (error != 0) {
281b032f27cSSam Leffler 			/* NB: IFQ_HANDOFF reclaims mbuf */
282b032f27cSSam Leffler 			ieee80211_free_node(ni);
283b032f27cSSam Leffler 		} else {
284b032f27cSSam Leffler 			ifp->if_opackets++;
285b032f27cSSam Leffler 		}
286b032f27cSSam Leffler 		ic->ic_lastdata = ticks;
287b032f27cSSam Leffler 	}
288b032f27cSSam Leffler #undef IS_DWDS
289b032f27cSSam Leffler }
290b032f27cSSam Leffler 
291b032f27cSSam Leffler /*
292b032f27cSSam Leffler  * 802.11 output routine. This is (currently) used only to
293b032f27cSSam Leffler  * connect bpf write calls to the 802.11 layer for injecting
294fad788b1SSam Leffler  * raw 802.11 frames.
295b032f27cSSam Leffler  */
296b032f27cSSam Leffler int
297b032f27cSSam Leffler ieee80211_output(struct ifnet *ifp, struct mbuf *m,
298279aa3d4SKip Macy 	struct sockaddr *dst, struct route *ro)
299b032f27cSSam Leffler {
300b032f27cSSam Leffler #define senderr(e) do { error = (e); goto bad;} while (0)
301b032f27cSSam Leffler 	struct ieee80211_node *ni = NULL;
302b032f27cSSam Leffler 	struct ieee80211vap *vap;
303b032f27cSSam Leffler 	struct ieee80211_frame *wh;
304b032f27cSSam Leffler 	int error;
305b032f27cSSam Leffler 
306b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
307b032f27cSSam Leffler 		/*
308b032f27cSSam Leffler 		 * Short-circuit requests if the vap is marked OACTIVE
309b032f27cSSam Leffler 		 * as this is used when tearing down state to indicate
310b032f27cSSam Leffler 		 * the vap may be gone.  This can also happen because a
311b032f27cSSam Leffler 		 * packet came down through ieee80211_start before the
312b032f27cSSam Leffler 		 * vap entered RUN state in which case it's also ok to
313b032f27cSSam Leffler 		 * just drop the frame.  This should not be necessary
314b032f27cSSam Leffler 		 * but callers of if_output don't check OACTIVE.
315b032f27cSSam Leffler 		 */
316b032f27cSSam Leffler 		senderr(ENETDOWN);
317b032f27cSSam Leffler 	}
318b032f27cSSam Leffler 	vap = ifp->if_softc;
319b032f27cSSam Leffler 	/*
320b032f27cSSam Leffler 	 * Hand to the 802.3 code if not tagged as
321b032f27cSSam Leffler 	 * a raw 802.11 frame.
322b032f27cSSam Leffler 	 */
323b032f27cSSam Leffler 	if (dst->sa_family != AF_IEEE80211)
324279aa3d4SKip Macy 		return vap->iv_output(ifp, m, dst, ro);
325b032f27cSSam Leffler #ifdef MAC
326b032f27cSSam Leffler 	error = mac_check_ifnet_transmit(ifp, m);
327b032f27cSSam Leffler 	if (error)
328b032f27cSSam Leffler 		senderr(error);
329b032f27cSSam Leffler #endif
330b032f27cSSam Leffler 	if (ifp->if_flags & IFF_MONITOR)
331b032f27cSSam Leffler 		senderr(ENETDOWN);
332b032f27cSSam Leffler 	if (!IFNET_IS_UP_RUNNING(ifp))
333b032f27cSSam Leffler 		senderr(ENETDOWN);
334b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
335b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap,
336b032f27cSSam Leffler 		    IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
337b032f27cSSam Leffler 		    "block %s frame in CAC state\n", "raw data");
338b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
339b032f27cSSam Leffler 		senderr(EIO);		/* XXX */
340b032f27cSSam Leffler 	}
341b032f27cSSam Leffler 	/* XXX bypass bridge, pfil, carp, etc. */
342b032f27cSSam Leffler 
343b032f27cSSam Leffler 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
344b032f27cSSam Leffler 		senderr(EIO);	/* XXX */
345b032f27cSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
346b032f27cSSam Leffler 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
347b032f27cSSam Leffler 	    IEEE80211_FC0_VERSION_0)
348b032f27cSSam Leffler 		senderr(EIO);	/* XXX */
349b032f27cSSam Leffler 
350b032f27cSSam Leffler 	/* locate destination node */
351b032f27cSSam Leffler 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
352b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_NODS:
353b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_FROMDS:
354b032f27cSSam Leffler 		ni = ieee80211_find_txnode(vap, wh->i_addr1);
355b032f27cSSam Leffler 		break;
356b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_TODS:
357b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_DSTODS:
358b032f27cSSam Leffler 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame))
359b032f27cSSam Leffler 			senderr(EIO);	/* XXX */
360b032f27cSSam Leffler 		ni = ieee80211_find_txnode(vap, wh->i_addr3);
361b032f27cSSam Leffler 		break;
362b032f27cSSam Leffler 	default:
363b032f27cSSam Leffler 		senderr(EIO);	/* XXX */
364b032f27cSSam Leffler 	}
365b032f27cSSam Leffler 	if (ni == NULL) {
366b032f27cSSam Leffler 		/*
367b032f27cSSam Leffler 		 * Permit packets w/ bpf params through regardless
368b032f27cSSam Leffler 		 * (see below about sa_len).
369b032f27cSSam Leffler 		 */
370b032f27cSSam Leffler 		if (dst->sa_len == 0)
371b032f27cSSam Leffler 			senderr(EHOSTUNREACH);
372b032f27cSSam Leffler 		ni = ieee80211_ref_node(vap->iv_bss);
373b032f27cSSam Leffler 	}
374b032f27cSSam Leffler 
375b032f27cSSam Leffler 	/*
376b032f27cSSam Leffler 	 * Sanitize mbuf for net80211 flags leaked from above.
377b032f27cSSam Leffler 	 *
378b032f27cSSam Leffler 	 * NB: This must be done before ieee80211_classify as
379b032f27cSSam Leffler 	 *     it marks EAPOL in frames with M_EAPOL.
380b032f27cSSam Leffler 	 */
381b032f27cSSam Leffler 	m->m_flags &= ~M_80211_TX;
382b032f27cSSam Leffler 
383b032f27cSSam Leffler 	/* calculate priority so drivers can find the tx queue */
384b032f27cSSam Leffler 	/* XXX assumes an 802.3 frame */
385b032f27cSSam Leffler 	if (ieee80211_classify(ni, m))
386b032f27cSSam Leffler 		senderr(EIO);		/* XXX */
387b032f27cSSam Leffler 
3880e910c94SSam Leffler 	if (bpf_peers_present(vap->iv_rawbpf))
3890e910c94SSam Leffler 		bpf_mtap(vap->iv_rawbpf, m);
3900e910c94SSam Leffler 
3910e910c94SSam Leffler 	IEEE80211_NODE_STAT(ni, tx_data);
3920e910c94SSam Leffler 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
3930e910c94SSam Leffler 		IEEE80211_NODE_STAT(ni, tx_mcast);
3940e910c94SSam Leffler 		m->m_flags |= M_MCAST;
3950e910c94SSam Leffler 	} else
3960e910c94SSam Leffler 		IEEE80211_NODE_STAT(ni, tx_ucast);
3970e910c94SSam Leffler 	/* NB: ieee80211_encap does not include 802.11 header */
3980e910c94SSam Leffler 	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, m->m_pkthdr.len);
399b032f27cSSam Leffler 
400b032f27cSSam Leffler 	/*
401b032f27cSSam Leffler 	 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
402b032f27cSSam Leffler 	 * present by setting the sa_len field of the sockaddr (yes,
403b032f27cSSam Leffler 	 * this is a hack).
404b032f27cSSam Leffler 	 * NB: we assume sa_data is suitably aligned to cast.
405b032f27cSSam Leffler 	 */
406b032f27cSSam Leffler 	return vap->iv_ic->ic_raw_xmit(ni, m,
407b032f27cSSam Leffler 	    (const struct ieee80211_bpf_params *)(dst->sa_len ?
408b032f27cSSam Leffler 		dst->sa_data : NULL));
409b032f27cSSam Leffler bad:
410b032f27cSSam Leffler 	if (m != NULL)
411b032f27cSSam Leffler 		m_freem(m);
412b032f27cSSam Leffler 	if (ni != NULL)
413b032f27cSSam Leffler 		ieee80211_free_node(ni);
414b032f27cSSam Leffler 	return error;
415b032f27cSSam Leffler #undef senderr
416b032f27cSSam Leffler }
417b032f27cSSam Leffler 
418b032f27cSSam Leffler /*
419add59d08SSam Leffler  * Set the direction field and address fields of an outgoing
4200e66722dSSam Leffler  * frame.  Note this should be called early on in constructing
4210e66722dSSam Leffler  * a frame as it sets i_fc[1]; other bits can then be or'd in.
422add59d08SSam Leffler  */
423add59d08SSam Leffler static void
424b032f27cSSam Leffler ieee80211_send_setup(
425add59d08SSam Leffler 	struct ieee80211_node *ni,
4269e80b1dfSSam Leffler 	struct mbuf *m,
4278ac160cdSSam Leffler 	int type, int tid,
42868e8e04eSSam Leffler 	const uint8_t sa[IEEE80211_ADDR_LEN],
42968e8e04eSSam Leffler 	const uint8_t da[IEEE80211_ADDR_LEN],
43068e8e04eSSam Leffler 	const uint8_t bssid[IEEE80211_ADDR_LEN])
431add59d08SSam Leffler {
432add59d08SSam Leffler #define	WH4(wh)	((struct ieee80211_frame_addr4 *)wh)
4339e80b1dfSSam Leffler 	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
4349e80b1dfSSam Leffler 	ieee80211_seq seqno;
435add59d08SSam Leffler 
436add59d08SSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
437add59d08SSam Leffler 	if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
438b032f27cSSam Leffler 		struct ieee80211vap *vap = ni->ni_vap;
439b032f27cSSam Leffler 
440b032f27cSSam Leffler 		switch (vap->iv_opmode) {
441add59d08SSam Leffler 		case IEEE80211_M_STA:
442add59d08SSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
443add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
444add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
445add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, da);
446add59d08SSam Leffler 			break;
447add59d08SSam Leffler 		case IEEE80211_M_IBSS:
448add59d08SSam Leffler 		case IEEE80211_M_AHDEMO:
449add59d08SSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
450add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
451add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
452add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
453add59d08SSam Leffler 			break;
454add59d08SSam Leffler 		case IEEE80211_M_HOSTAP:
455add59d08SSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
456add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
457add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
458add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, sa);
459add59d08SSam Leffler 			break;
46068e8e04eSSam Leffler 		case IEEE80211_M_WDS:
46168e8e04eSSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
462b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
463b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
46468e8e04eSSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, da);
46568e8e04eSSam Leffler 			IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
46668e8e04eSSam Leffler 			break;
467add59d08SSam Leffler 		case IEEE80211_M_MONITOR:	/* NB: to quiet compiler */
468add59d08SSam Leffler 			break;
469add59d08SSam Leffler 		}
470add59d08SSam Leffler 	} else {
471add59d08SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
472add59d08SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, da);
473add59d08SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, sa);
474add59d08SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
475add59d08SSam Leffler 	}
47668e8e04eSSam Leffler 	*(uint16_t *)&wh->i_dur[0] = 0;
4779e80b1dfSSam Leffler 
4789e80b1dfSSam Leffler 	seqno = ni->ni_txseqs[tid]++;
4799e80b1dfSSam Leffler 	*(uint16_t *)&wh->i_seq[0] = htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
4809e80b1dfSSam Leffler 
4819e80b1dfSSam Leffler 	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
4829e80b1dfSSam Leffler 		m->m_flags |= M_MCAST;
483add59d08SSam Leffler #undef WH4
484add59d08SSam Leffler }
485add59d08SSam Leffler 
486add59d08SSam Leffler /*
4870a915fadSSam Leffler  * Send a management frame to the specified node.  The node pointer
4880a915fadSSam Leffler  * must have a reference as the pointer will be passed to the driver
4890a915fadSSam Leffler  * and potentially held for a long time.  If the frame is successfully
4900a915fadSSam Leffler  * dispatched to the driver, then it is responsible for freeing the
491b032f27cSSam Leffler  * reference (and potentially free'ing up any associated storage);
492b032f27cSSam Leffler  * otherwise deal with reclaiming any reference (on error).
4930a915fadSSam Leffler  */
49468e8e04eSSam Leffler int
4958ac160cdSSam Leffler ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
4968ac160cdSSam Leffler 	struct ieee80211_bpf_params *params)
4971a1e1d21SSam Leffler {
498b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
499b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
5001a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
5011a1e1d21SSam Leffler 
5020a915fadSSam Leffler 	KASSERT(ni != NULL, ("null node"));
5031a1e1d21SSam Leffler 
504b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
505b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
506b032f27cSSam Leffler 		    ni, "block %s frame in CAC state",
507b032f27cSSam Leffler 			ieee80211_mgt_subtype_name[
508b032f27cSSam Leffler 			    (type & IEEE80211_FC0_SUBTYPE_MASK) >>
509b032f27cSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT]);
510b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
511b032f27cSSam Leffler 		ieee80211_free_node(ni);
512b032f27cSSam Leffler 		m_freem(m);
513b032f27cSSam Leffler 		return EIO;		/* XXX */
514b032f27cSSam Leffler 	}
515b032f27cSSam Leffler 
5161a1e1d21SSam Leffler 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
517b032f27cSSam Leffler 	if (m == NULL) {
518b032f27cSSam Leffler 		ieee80211_free_node(ni);
5191a1e1d21SSam Leffler 		return ENOMEM;
520b032f27cSSam Leffler 	}
5210a915fadSSam Leffler 
5221a1e1d21SSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
5239e80b1dfSSam Leffler 	ieee80211_send_setup(ni, m,
5248ac160cdSSam Leffler 	     IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
525b032f27cSSam Leffler 	     vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
5268ac160cdSSam Leffler 	if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
527b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
528b032f27cSSam Leffler 		    "encrypting frame (%s)", __func__);
5298a1b9b6aSSam Leffler 		wh->i_fc[1] |= IEEE80211_FC1_WEP;
5308a1b9b6aSSam Leffler 	}
531c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
5328ac160cdSSam Leffler 
5338ac160cdSSam Leffler 	KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
5348ac160cdSSam Leffler 	M_WME_SETAC(m, params->ibp_pri);
5358ac160cdSSam Leffler 
5368a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
5378a1b9b6aSSam Leffler 	/* avoid printing too many frames */
538b032f27cSSam Leffler 	if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
539b032f27cSSam Leffler 	    ieee80211_msg_dumppkts(vap)) {
5408a1b9b6aSSam Leffler 		printf("[%s] send %s on channel %u\n",
5418a1b9b6aSSam Leffler 		    ether_sprintf(wh->i_addr1),
5428a1b9b6aSSam Leffler 		    ieee80211_mgt_subtype_name[
5438a1b9b6aSSam Leffler 			(type & IEEE80211_FC0_SUBTYPE_MASK) >>
5448a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
545b5c99415SSam Leffler 		    ieee80211_chan2ieee(ic, ic->ic_curchan));
5468a1b9b6aSSam Leffler 	}
5478a1b9b6aSSam Leffler #endif
5488a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_mgmt);
54968e8e04eSSam Leffler 
5508ac160cdSSam Leffler 	return ic->ic_raw_xmit(ni, m, params);
551246b5467SSam Leffler }
552246b5467SSam Leffler 
553246b5467SSam Leffler /*
5546683931eSSam Leffler  * Send a null data frame to the specified node.  If the station
5556683931eSSam Leffler  * is setup for QoS then a QoS Null Data frame is constructed.
5566683931eSSam Leffler  * If this is a WDS station then a 4-address frame is constructed.
55719ad2dd7SSam Leffler  *
55819ad2dd7SSam Leffler  * NB: the caller is assumed to have setup a node reference
55919ad2dd7SSam Leffler  *     for use; this is necessary to deal with a race condition
560b032f27cSSam Leffler  *     when probing for inactive stations.  Like ieee80211_mgmt_output
561b032f27cSSam Leffler  *     we must cleanup any node reference on error;  however we
562b032f27cSSam Leffler  *     can safely just unref it as we know it will never be the
563b032f27cSSam Leffler  *     last reference to the node.
5648a1b9b6aSSam Leffler  */
5658a1b9b6aSSam Leffler int
566f62121ceSSam Leffler ieee80211_send_nulldata(struct ieee80211_node *ni)
5678a1b9b6aSSam Leffler {
568b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
569f62121ceSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
5708a1b9b6aSSam Leffler 	struct mbuf *m;
5718a1b9b6aSSam Leffler 	struct ieee80211_frame *wh;
5726683931eSSam Leffler 	int hdrlen;
5736683931eSSam Leffler 	uint8_t *frm;
5748a1b9b6aSSam Leffler 
575b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
576b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
577b032f27cSSam Leffler 		    ni, "block %s frame in CAC state", "null data");
578b032f27cSSam Leffler 		ieee80211_unref_node(&ni);
579b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
580b032f27cSSam Leffler 		return EIO;		/* XXX */
581b032f27cSSam Leffler 	}
582b032f27cSSam Leffler 
5836683931eSSam Leffler 	if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
5846683931eSSam Leffler 		hdrlen = sizeof(struct ieee80211_qosframe);
5856683931eSSam Leffler 	else
5866683931eSSam Leffler 		hdrlen = sizeof(struct ieee80211_frame);
5876683931eSSam Leffler 	/* NB: only WDS vap's get 4-address frames */
5886683931eSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_WDS)
5896683931eSSam Leffler 		hdrlen += IEEE80211_ADDR_LEN;
5906683931eSSam Leffler 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
5916683931eSSam Leffler 		hdrlen = roundup(hdrlen, sizeof(uint32_t));
5926683931eSSam Leffler 
5936683931eSSam Leffler 	m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
5948a1b9b6aSSam Leffler 	if (m == NULL) {
5958a1b9b6aSSam Leffler 		/* XXX debug msg */
59619ad2dd7SSam Leffler 		ieee80211_unref_node(&ni);
597b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
5988a1b9b6aSSam Leffler 		return ENOMEM;
5998a1b9b6aSSam Leffler 	}
6006683931eSSam Leffler 	KASSERT(M_LEADINGSPACE(m) >= hdrlen,
6016683931eSSam Leffler 	    ("leading space %zd", M_LEADINGSPACE(m)));
6026683931eSSam Leffler 	M_PREPEND(m, hdrlen, M_DONTWAIT);
6036683931eSSam Leffler 	if (m == NULL) {
6046683931eSSam Leffler 		/* NB: cannot happen */
6056683931eSSam Leffler 		ieee80211_free_node(ni);
6066683931eSSam Leffler 		return ENOMEM;
6076683931eSSam Leffler 	}
6088a1b9b6aSSam Leffler 
6096683931eSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);		/* NB: a little lie */
6106683931eSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
6116683931eSSam Leffler 		const int tid = WME_AC_TO_TID(WME_AC_BE);
6126683931eSSam Leffler 		uint8_t *qos;
6136683931eSSam Leffler 
6149e80b1dfSSam Leffler 		ieee80211_send_setup(ni, m,
6156683931eSSam Leffler 		    IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
6166683931eSSam Leffler 		    tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
6176683931eSSam Leffler 
6186683931eSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_WDS)
6196683931eSSam Leffler 			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
6206683931eSSam Leffler 		else
6216683931eSSam Leffler 			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
6226683931eSSam Leffler 		qos[0] = tid & IEEE80211_QOS_TID;
6236683931eSSam Leffler 		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
6246683931eSSam Leffler 			qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
6256683931eSSam Leffler 		qos[1] = 0;
6266683931eSSam Leffler 	} else {
6279e80b1dfSSam Leffler 		ieee80211_send_setup(ni, m,
628add59d08SSam Leffler 		    IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
6298ac160cdSSam Leffler 		    IEEE80211_NONQOS_TID,
630b032f27cSSam Leffler 		    vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
6316683931eSSam Leffler 	}
632b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_WDS) {
633add59d08SSam Leffler 		/* NB: power management bit is never sent by an AP */
634add59d08SSam Leffler 		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
635b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_HOSTAP)
636add59d08SSam Leffler 			wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
637b032f27cSSam Leffler 	}
6386683931eSSam Leffler 	m->m_len = m->m_pkthdr.len = hdrlen;
639c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
6406683931eSSam Leffler 
641bb239ce9SSam Leffler 	M_WME_SETAC(m, WME_AC_BE);
6428a1b9b6aSSam Leffler 
6438a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_data);
6448a1b9b6aSSam Leffler 
645b032f27cSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
6466683931eSSam Leffler 	    "send %snull data frame on channel %u, pwr mgt %s",
6476683931eSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
648b5c99415SSam Leffler 	    ieee80211_chan2ieee(ic, ic->ic_curchan),
649add59d08SSam Leffler 	    wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
650add59d08SSam Leffler 
651b032f27cSSam Leffler 	return ic->ic_raw_xmit(ni, m, NULL);
6528a1b9b6aSSam Leffler }
6538a1b9b6aSSam Leffler 
6548a1b9b6aSSam Leffler /*
6558a1b9b6aSSam Leffler  * Assign priority to a frame based on any vlan tag assigned
6568a1b9b6aSSam Leffler  * to the station and/or any Diffserv setting in an IP header.
6578a1b9b6aSSam Leffler  * Finally, if an ACM policy is setup (in station mode) it's
6588a1b9b6aSSam Leffler  * applied.
6598a1b9b6aSSam Leffler  */
6608a1b9b6aSSam Leffler int
661b032f27cSSam Leffler ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
6628a1b9b6aSSam Leffler {
663b032f27cSSam Leffler 	const struct ether_header *eh = mtod(m, struct ether_header *);
6648a1b9b6aSSam Leffler 	int v_wme_ac, d_wme_ac, ac;
6658a1b9b6aSSam Leffler 
666b032f27cSSam Leffler 	/*
667b032f27cSSam Leffler 	 * Always promote PAE/EAPOL frames to high priority.
668b032f27cSSam Leffler 	 */
669b032f27cSSam Leffler 	if (eh->ether_type == htons(ETHERTYPE_PAE)) {
670b032f27cSSam Leffler 		/* NB: mark so others don't need to check header */
671b032f27cSSam Leffler 		m->m_flags |= M_EAPOL;
672b032f27cSSam Leffler 		ac = WME_AC_VO;
673b032f27cSSam Leffler 		goto done;
674b032f27cSSam Leffler 	}
675b032f27cSSam Leffler 	/*
676b032f27cSSam Leffler 	 * Non-qos traffic goes to BE.
677b032f27cSSam Leffler 	 */
6788a1b9b6aSSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
6798a1b9b6aSSam Leffler 		ac = WME_AC_BE;
6808a1b9b6aSSam Leffler 		goto done;
6818a1b9b6aSSam Leffler 	}
6828a1b9b6aSSam Leffler 
6838a1b9b6aSSam Leffler 	/*
6848a1b9b6aSSam Leffler 	 * If node has a vlan tag then all traffic
6858a1b9b6aSSam Leffler 	 * to it must have a matching tag.
6868a1b9b6aSSam Leffler 	 */
6878a1b9b6aSSam Leffler 	v_wme_ac = 0;
6888a1b9b6aSSam Leffler 	if (ni->ni_vlan != 0) {
68978ba57b9SAndre Oppermann 		 if ((m->m_flags & M_VLANTAG) == 0) {
6908a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_novlantag);
6918a1b9b6aSSam Leffler 			return 1;
6928a1b9b6aSSam Leffler 		}
69378ba57b9SAndre Oppermann 		if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
6948a1b9b6aSSam Leffler 		    EVL_VLANOFTAG(ni->ni_vlan)) {
6958a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
6968a1b9b6aSSam Leffler 			return 1;
6978a1b9b6aSSam Leffler 		}
6988a1b9b6aSSam Leffler 		/* map vlan priority to AC */
699f4558c9aSSam Leffler 		v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
7008a1b9b6aSSam Leffler 	}
7018a1b9b6aSSam Leffler 
7028a1b9b6aSSam Leffler #ifdef INET
7038a1b9b6aSSam Leffler 	if (eh->ether_type == htons(ETHERTYPE_IP)) {
704f4558c9aSSam Leffler 		uint8_t tos;
7058a1b9b6aSSam Leffler 		/*
706f4558c9aSSam Leffler 		 * IP frame, map the DSCP bits from the TOS field.
7078a1b9b6aSSam Leffler 		 */
708f4558c9aSSam Leffler 		/* XXX m_copydata may be too slow for fast path */
709f4558c9aSSam Leffler 		/* NB: ip header may not be in first mbuf */
710f4558c9aSSam Leffler 		m_copydata(m, sizeof(struct ether_header) +
711f4558c9aSSam Leffler 		    offsetof(struct ip, ip_tos), sizeof(tos), &tos);
712f4558c9aSSam Leffler 		tos >>= 5;		/* NB: ECN + low 3 bits of DSCP */
713f4558c9aSSam Leffler 		d_wme_ac = TID_TO_WME_AC(tos);
7148a1b9b6aSSam Leffler 	} else {
7158a1b9b6aSSam Leffler #endif /* INET */
7168a1b9b6aSSam Leffler 		d_wme_ac = WME_AC_BE;
7178a1b9b6aSSam Leffler #ifdef INET
7188a1b9b6aSSam Leffler 	}
7198a1b9b6aSSam Leffler #endif
7208a1b9b6aSSam Leffler 	/*
7218a1b9b6aSSam Leffler 	 * Use highest priority AC.
7228a1b9b6aSSam Leffler 	 */
7238a1b9b6aSSam Leffler 	if (v_wme_ac > d_wme_ac)
7248a1b9b6aSSam Leffler 		ac = v_wme_ac;
7258a1b9b6aSSam Leffler 	else
7268a1b9b6aSSam Leffler 		ac = d_wme_ac;
7278a1b9b6aSSam Leffler 
7288a1b9b6aSSam Leffler 	/*
7298a1b9b6aSSam Leffler 	 * Apply ACM policy.
7308a1b9b6aSSam Leffler 	 */
731b032f27cSSam Leffler 	if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
7328a1b9b6aSSam Leffler 		static const int acmap[4] = {
7338a1b9b6aSSam Leffler 			WME_AC_BK,	/* WME_AC_BE */
7348a1b9b6aSSam Leffler 			WME_AC_BK,	/* WME_AC_BK */
7358a1b9b6aSSam Leffler 			WME_AC_BE,	/* WME_AC_VI */
7368a1b9b6aSSam Leffler 			WME_AC_VI,	/* WME_AC_VO */
7378a1b9b6aSSam Leffler 		};
738b032f27cSSam Leffler 		struct ieee80211com *ic = ni->ni_ic;
739b032f27cSSam Leffler 
7408a1b9b6aSSam Leffler 		while (ac != WME_AC_BK &&
7418a1b9b6aSSam Leffler 		    ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
7428a1b9b6aSSam Leffler 			ac = acmap[ac];
7438a1b9b6aSSam Leffler 	}
7448a1b9b6aSSam Leffler done:
7458a1b9b6aSSam Leffler 	M_WME_SETAC(m, ac);
7468a1b9b6aSSam Leffler 	return 0;
7478a1b9b6aSSam Leffler }
7488a1b9b6aSSam Leffler 
7498a1b9b6aSSam Leffler /*
7505e923d2eSSam Leffler  * Insure there is sufficient contiguous space to encapsulate the
7515e923d2eSSam Leffler  * 802.11 data frame.  If room isn't already there, arrange for it.
7525e923d2eSSam Leffler  * Drivers and cipher modules assume we have done the necessary work
7535e923d2eSSam Leffler  * and fail rudely if they don't find the space they need.
7545e923d2eSSam Leffler  */
755616190d0SSam Leffler struct mbuf *
756b032f27cSSam Leffler ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
7575e923d2eSSam Leffler 	struct ieee80211_key *key, struct mbuf *m)
7585e923d2eSSam Leffler {
759ab96db10SSam Leffler #define	TO_BE_RECLAIMED	(sizeof(struct ether_header) - sizeof(struct llc))
760b032f27cSSam Leffler 	int needed_space = vap->iv_ic->ic_headroom + hdrsize;
7615e923d2eSSam Leffler 
7625e923d2eSSam Leffler 	if (key != NULL) {
7635e923d2eSSam Leffler 		/* XXX belongs in crypto code? */
7645e923d2eSSam Leffler 		needed_space += key->wk_cipher->ic_header;
7655e923d2eSSam Leffler 		/* XXX frags */
76683a244dbSSam Leffler 		/*
76783a244dbSSam Leffler 		 * When crypto is being done in the host we must insure
76883a244dbSSam Leffler 		 * the data are writable for the cipher routines; clone
76983a244dbSSam Leffler 		 * a writable mbuf chain.
77083a244dbSSam Leffler 		 * XXX handle SWMIC specially
77183a244dbSSam Leffler 		 */
7725c1f7f19SSam Leffler 		if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
77383a244dbSSam Leffler 			m = m_unshare(m, M_NOWAIT);
77483a244dbSSam Leffler 			if (m == NULL) {
775b032f27cSSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
77683a244dbSSam Leffler 				    "%s: cannot get writable mbuf\n", __func__);
777b032f27cSSam Leffler 				vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
77883a244dbSSam Leffler 				return NULL;
77983a244dbSSam Leffler 			}
78083a244dbSSam Leffler 		}
7815e923d2eSSam Leffler 	}
7825e923d2eSSam Leffler 	/*
7835e923d2eSSam Leffler 	 * We know we are called just before stripping an Ethernet
7845e923d2eSSam Leffler 	 * header and prepending an LLC header.  This means we know
7855e923d2eSSam Leffler 	 * there will be
786ab96db10SSam Leffler 	 *	sizeof(struct ether_header) - sizeof(struct llc)
7875e923d2eSSam Leffler 	 * bytes recovered to which we need additional space for the
7885e923d2eSSam Leffler 	 * 802.11 header and any crypto header.
7895e923d2eSSam Leffler 	 */
7905e923d2eSSam Leffler 	/* XXX check trailing space and copy instead? */
7915e923d2eSSam Leffler 	if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
7925e923d2eSSam Leffler 		struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
7935e923d2eSSam Leffler 		if (n == NULL) {
794b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
7955e923d2eSSam Leffler 			    "%s: cannot expand storage\n", __func__);
796b032f27cSSam Leffler 			vap->iv_stats.is_tx_nobuf++;
7975e923d2eSSam Leffler 			m_freem(m);
7985e923d2eSSam Leffler 			return NULL;
7995e923d2eSSam Leffler 		}
8005e923d2eSSam Leffler 		KASSERT(needed_space <= MHLEN,
8015e923d2eSSam Leffler 		    ("not enough room, need %u got %zu\n", needed_space, MHLEN));
8025e923d2eSSam Leffler 		/*
8035e923d2eSSam Leffler 		 * Setup new mbuf to have leading space to prepend the
8045e923d2eSSam Leffler 		 * 802.11 header and any crypto header bits that are
8055e923d2eSSam Leffler 		 * required (the latter are added when the driver calls
8065e923d2eSSam Leffler 		 * back to ieee80211_crypto_encap to do crypto encapsulation).
8075e923d2eSSam Leffler 		 */
8085e923d2eSSam Leffler 		/* NB: must be first 'cuz it clobbers m_data */
8095e923d2eSSam Leffler 		m_move_pkthdr(n, m);
8105e923d2eSSam Leffler 		n->m_len = 0;			/* NB: m_gethdr does not set */
8115e923d2eSSam Leffler 		n->m_data += needed_space;
8125e923d2eSSam Leffler 		/*
8135e923d2eSSam Leffler 		 * Pull up Ethernet header to create the expected layout.
8145e923d2eSSam Leffler 		 * We could use m_pullup but that's overkill (i.e. we don't
8155e923d2eSSam Leffler 		 * need the actual data) and it cannot fail so do it inline
8165e923d2eSSam Leffler 		 * for speed.
8175e923d2eSSam Leffler 		 */
8185e923d2eSSam Leffler 		/* NB: struct ether_header is known to be contiguous */
8195e923d2eSSam Leffler 		n->m_len += sizeof(struct ether_header);
8205e923d2eSSam Leffler 		m->m_len -= sizeof(struct ether_header);
8215e923d2eSSam Leffler 		m->m_data += sizeof(struct ether_header);
8225e923d2eSSam Leffler 		/*
8235e923d2eSSam Leffler 		 * Replace the head of the chain.
8245e923d2eSSam Leffler 		 */
8255e923d2eSSam Leffler 		n->m_next = m;
8265e923d2eSSam Leffler 		m = n;
8275e923d2eSSam Leffler 	}
8285e923d2eSSam Leffler 	return m;
8295e923d2eSSam Leffler #undef TO_BE_RECLAIMED
8305e923d2eSSam Leffler }
8315e923d2eSSam Leffler 
8325e923d2eSSam Leffler /*
8335e923d2eSSam Leffler  * Return the transmit key to use in sending a unicast frame.
8345e923d2eSSam Leffler  * If a unicast key is set we use that.  When no unicast key is set
8355e923d2eSSam Leffler  * we fall back to the default transmit key.
8368a1b9b6aSSam Leffler  */
8378a1b9b6aSSam Leffler static __inline struct ieee80211_key *
838b032f27cSSam Leffler ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
839b032f27cSSam Leffler 	struct ieee80211_node *ni)
8408a1b9b6aSSam Leffler {
841cda15ce1SSam Leffler 	if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
842b032f27cSSam Leffler 		if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
843b032f27cSSam Leffler 		    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
8448a1b9b6aSSam Leffler 			return NULL;
845b032f27cSSam Leffler 		return &vap->iv_nw_keys[vap->iv_def_txkey];
8468a1b9b6aSSam Leffler 	} else {
8478a1b9b6aSSam Leffler 		return &ni->ni_ucastkey;
8488a1b9b6aSSam Leffler 	}
8495e923d2eSSam Leffler }
8505e923d2eSSam Leffler 
8515e923d2eSSam Leffler /*
8525e923d2eSSam Leffler  * Return the transmit key to use in sending a multicast frame.
8535e923d2eSSam Leffler  * Multicast traffic always uses the group key which is installed as
8545e923d2eSSam Leffler  * the default tx key.
8555e923d2eSSam Leffler  */
8565e923d2eSSam Leffler static __inline struct ieee80211_key *
857b032f27cSSam Leffler ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
858b032f27cSSam Leffler 	struct ieee80211_node *ni)
8595e923d2eSSam Leffler {
860b032f27cSSam Leffler 	if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
861b032f27cSSam Leffler 	    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
8625e923d2eSSam Leffler 		return NULL;
863b032f27cSSam Leffler 	return &vap->iv_nw_keys[vap->iv_def_txkey];
8648a1b9b6aSSam Leffler }
8658a1b9b6aSSam Leffler 
8668a1b9b6aSSam Leffler /*
8678a1b9b6aSSam Leffler  * Encapsulate an outbound data frame.  The mbuf chain is updated.
8688a1b9b6aSSam Leffler  * If an error is encountered NULL is returned.  The caller is required
8698a1b9b6aSSam Leffler  * to provide a node reference and pullup the ethernet header in the
8708a1b9b6aSSam Leffler  * first mbuf.
871b032f27cSSam Leffler  *
872b032f27cSSam Leffler  * NB: Packet is assumed to be processed by ieee80211_classify which
873b032f27cSSam Leffler  *     marked EAPOL frames w/ M_EAPOL.
8740a915fadSSam Leffler  */
8751a1e1d21SSam Leffler struct mbuf *
876339ccfb3SSam Leffler ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
877339ccfb3SSam Leffler     struct mbuf *m)
8781a1e1d21SSam Leffler {
879b032f27cSSam Leffler #define	WH4(wh)	((struct ieee80211_frame_addr4 *)(wh))
880b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
8811a1e1d21SSam Leffler 	struct ether_header eh;
8821a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
8838a1b9b6aSSam Leffler 	struct ieee80211_key *key;
8841a1e1d21SSam Leffler 	struct llc *llc;
885616190d0SSam Leffler 	int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr;
8869e80b1dfSSam Leffler 	ieee80211_seq seqno;
8871a1e1d21SSam Leffler 
88868e8e04eSSam Leffler 	/*
88968e8e04eSSam Leffler 	 * Copy existing Ethernet header to a safe place.  The
89068e8e04eSSam Leffler 	 * rest of the code assumes it's ok to strip it when
89168e8e04eSSam Leffler 	 * reorganizing state for the final encapsulation.
89268e8e04eSSam Leffler 	 */
8938a1b9b6aSSam Leffler 	KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
894b032f27cSSam Leffler 	ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
8951a1e1d21SSam Leffler 
8968a1b9b6aSSam Leffler 	/*
8978a1b9b6aSSam Leffler 	 * Insure space for additional headers.  First identify
8988a1b9b6aSSam Leffler 	 * transmit key to use in calculating any buffer adjustments
8998a1b9b6aSSam Leffler 	 * required.  This is also used below to do privacy
9008a1b9b6aSSam Leffler 	 * encapsulation work.  Then calculate the 802.11 header
9018a1b9b6aSSam Leffler 	 * size and any padding required by the driver.
9028a1b9b6aSSam Leffler 	 *
9038a1b9b6aSSam Leffler 	 * Note key may be NULL if we fall back to the default
9048a1b9b6aSSam Leffler 	 * transmit key and that is not set.  In that case the
9058a1b9b6aSSam Leffler 	 * buffer may not be expanded as needed by the cipher
9068a1b9b6aSSam Leffler 	 * routines, but they will/should discard it.
9078a1b9b6aSSam Leffler 	 */
908b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
909b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA ||
910b032f27cSSam Leffler 		    !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
911b032f27cSSam Leffler 		    (vap->iv_opmode == IEEE80211_M_WDS &&
912b032f27cSSam Leffler 		     (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
913b032f27cSSam Leffler 			key = ieee80211_crypto_getucastkey(vap, ni);
9145e923d2eSSam Leffler 		else
915b032f27cSSam Leffler 			key = ieee80211_crypto_getmcastkey(vap, ni);
916b032f27cSSam Leffler 		if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
917b032f27cSSam Leffler 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
918b032f27cSSam Leffler 			    eh.ether_dhost,
919b032f27cSSam Leffler 			    "no default transmit key (%s) deftxkey %u",
920b032f27cSSam Leffler 			    __func__, vap->iv_def_txkey);
921b032f27cSSam Leffler 			vap->iv_stats.is_tx_nodefkey++;
922d72c7253SSam Leffler 			goto bad;
9238a1b9b6aSSam Leffler 		}
9248a1b9b6aSSam Leffler 	} else
9258a1b9b6aSSam Leffler 		key = NULL;
9265e923d2eSSam Leffler 	/*
9275e923d2eSSam Leffler 	 * XXX Some ap's don't handle QoS-encapsulated EAPOL
9285e923d2eSSam Leffler 	 * frames so suppress use.  This may be an issue if other
9295e923d2eSSam Leffler 	 * ap's require all data frames to be QoS-encapsulated
9305e923d2eSSam Leffler 	 * once negotiated in which case we'll need to make this
9315e923d2eSSam Leffler 	 * configurable.
9325e923d2eSSam Leffler 	 */
93368e8e04eSSam Leffler 	addqos = (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) &&
934b032f27cSSam Leffler 		 (m->m_flags & M_EAPOL) == 0;
9355e923d2eSSam Leffler 	if (addqos)
9368a1b9b6aSSam Leffler 		hdrsize = sizeof(struct ieee80211_qosframe);
9378a1b9b6aSSam Leffler 	else
9388a1b9b6aSSam Leffler 		hdrsize = sizeof(struct ieee80211_frame);
939b032f27cSSam Leffler 	/*
940b032f27cSSam Leffler 	 * 4-address frames need to be generated for:
9416437e6daSSam Leffler 	 * o packets sent through a WDS vap (IEEE80211_M_WDS)
942b032f27cSSam Leffler 	 * o packets relayed by a station operating with dynamic WDS
943b032f27cSSam Leffler 	 *   (IEEE80211_M_STA+IEEE80211_F_DWDS and src address)
944b032f27cSSam Leffler 	 */
9456437e6daSSam Leffler 	is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
946b032f27cSSam Leffler 	    (vap->iv_opmode == IEEE80211_M_STA &&
947b032f27cSSam Leffler 	     (vap->iv_flags & IEEE80211_F_DWDS) &&
948b032f27cSSam Leffler 	     !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
949b032f27cSSam Leffler 	if (is4addr)
950b032f27cSSam Leffler 		hdrsize += IEEE80211_ADDR_LEN;
951b032f27cSSam Leffler 	/*
952b032f27cSSam Leffler 	 * Honor driver DATAPAD requirement.
953b032f27cSSam Leffler 	 */
9548a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
955b032f27cSSam Leffler 		hdrspace = roundup(hdrsize, sizeof(uint32_t));
956b032f27cSSam Leffler 	else
957b032f27cSSam Leffler 		hdrspace = hdrsize;
95868e8e04eSSam Leffler 
959616190d0SSam Leffler 	if (__predict_true((m->m_flags & M_FF) == 0)) {
96068e8e04eSSam Leffler 		/*
96168e8e04eSSam Leffler 		 * Normal frame.
96268e8e04eSSam Leffler 		 */
963b032f27cSSam Leffler 		m = ieee80211_mbuf_adjust(vap, hdrspace, key, m);
9648a1b9b6aSSam Leffler 		if (m == NULL) {
9658a1b9b6aSSam Leffler 			/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
9660a915fadSSam Leffler 			goto bad;
9670a915fadSSam Leffler 		}
96868e8e04eSSam Leffler 		/* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
969ab96db10SSam Leffler 		m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
9701a1e1d21SSam Leffler 		llc = mtod(m, struct llc *);
9711a1e1d21SSam Leffler 		llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
9721a1e1d21SSam Leffler 		llc->llc_control = LLC_UI;
9731a1e1d21SSam Leffler 		llc->llc_snap.org_code[0] = 0;
9741a1e1d21SSam Leffler 		llc->llc_snap.org_code[1] = 0;
9751a1e1d21SSam Leffler 		llc->llc_snap.org_code[2] = 0;
9761a1e1d21SSam Leffler 		llc->llc_snap.ether_type = eh.ether_type;
977616190d0SSam Leffler 	} else {
978616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
979339ccfb3SSam Leffler 		/*
980339ccfb3SSam Leffler 		 * Aggregated frame.
981339ccfb3SSam Leffler 		 */
982616190d0SSam Leffler 		m = ieee80211_ff_encap(vap, m, hdrspace, key);
983616190d0SSam Leffler 		if (m == NULL)
984616190d0SSam Leffler #endif
985616190d0SSam Leffler 			goto bad;
98668e8e04eSSam Leffler 	}
9878a1b9b6aSSam Leffler 	datalen = m->m_pkthdr.len;		/* NB: w/o 802.11 header */
9888a1b9b6aSSam Leffler 
989b032f27cSSam Leffler 	M_PREPEND(m, hdrspace, M_DONTWAIT);
9901be50176SSam Leffler 	if (m == NULL) {
991b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
9920a915fadSSam Leffler 		goto bad;
9931be50176SSam Leffler 	}
9941a1e1d21SSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
9951a1e1d21SSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
99668e8e04eSSam Leffler 	*(uint16_t *)wh->i_dur = 0;
997b032f27cSSam Leffler 	if (is4addr) {
998b032f27cSSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
999b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1000b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1001b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1002b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1003b032f27cSSam Leffler 	} else switch (vap->iv_opmode) {
10041a1e1d21SSam Leffler 	case IEEE80211_M_STA:
10051a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
10061a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
10071a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
10081a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
10091a1e1d21SSam Leffler 		break;
10101a1e1d21SSam Leffler 	case IEEE80211_M_IBSS:
10111a1e1d21SSam Leffler 	case IEEE80211_M_AHDEMO:
10121a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
10131a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
10141a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1015a8b16e87SSam Leffler 		/*
1016b032f27cSSam Leffler 		 * NB: always use the bssid from iv_bss as the
1017a8b16e87SSam Leffler 		 *     neighbor's may be stale after an ibss merge
1018a8b16e87SSam Leffler 		 */
1019b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
10201a1e1d21SSam Leffler 		break;
10211a1e1d21SSam Leffler 	case IEEE80211_M_HOSTAP:
10221a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
10231a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
10241a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
10251a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
10261a1e1d21SSam Leffler 		break;
10272bbe529dSSam Leffler 	case IEEE80211_M_MONITOR:
1028b032f27cSSam Leffler 	case IEEE80211_M_WDS:		/* NB: is4addr should always be true */
10290a915fadSSam Leffler 		goto bad;
10301a1e1d21SSam Leffler 	}
1031bc5627d9SSam Leffler 	if (m->m_flags & M_MORE_DATA)
1032bc5627d9SSam Leffler 		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
10335e923d2eSSam Leffler 	if (addqos) {
1034b032f27cSSam Leffler 		uint8_t *qos;
10358a1b9b6aSSam Leffler 		int ac, tid;
10368a1b9b6aSSam Leffler 
1037b032f27cSSam Leffler 		if (is4addr) {
1038b032f27cSSam Leffler 			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1039b032f27cSSam Leffler 		} else
1040b032f27cSSam Leffler 			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
10418a1b9b6aSSam Leffler 		ac = M_WME_GETAC(m);
10428a1b9b6aSSam Leffler 		/* map from access class/queue to 11e header priorty value */
10438a1b9b6aSSam Leffler 		tid = WME_AC_TO_TID(ac);
1044b032f27cSSam Leffler 		qos[0] = tid & IEEE80211_QOS_TID;
104568e8e04eSSam Leffler 		/*
104668e8e04eSSam Leffler 		 * Check if A-MPDU tx aggregation is setup or if we
104768e8e04eSSam Leffler 		 * should try to enable it.  The sta must be associated
1048b032f27cSSam Leffler 		 * with HT and A-MPDU enabled for use.  When the policy
1049b032f27cSSam Leffler 		 * routine decides we should enable A-MPDU we issue an
1050b032f27cSSam Leffler 		 * ADDBA request and wait for a reply.  The frame being
1051b032f27cSSam Leffler 		 * encapsulated will go out w/o using A-MPDU, or possibly
1052b032f27cSSam Leffler 		 * it might be collected by the driver and held/retransmit.
1053b032f27cSSam Leffler 		 * The default ic_ampdu_enable routine handles staggering
1054b032f27cSSam Leffler 		 * ADDBA requests in case the receiver NAK's us or we are
1055b032f27cSSam Leffler 		 * otherwise unable to establish a BA stream.
105668e8e04eSSam Leffler 		 */
10571b6167d2SSam Leffler 		if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
1058b032f27cSSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_AMPDU_TX)) {
105968e8e04eSSam Leffler 			struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[ac];
106068e8e04eSSam Leffler 
1061b032f27cSSam Leffler 			ieee80211_txampdu_count_packet(tap);
106268e8e04eSSam Leffler 			if (IEEE80211_AMPDU_RUNNING(tap)) {
106368e8e04eSSam Leffler 				/*
106468e8e04eSSam Leffler 				 * Operational, mark frame for aggregation.
106545f856e3SSam Leffler 				 *
106645f856e3SSam Leffler 				 * NB: We support only immediate BA's for
106745f856e3SSam Leffler 				 * AMPDU which means we set the QoS control
106845f856e3SSam Leffler 				 * field to "normal ack" (0) to get "implicit
106945f856e3SSam Leffler 				 * block ack" behaviour.
107068e8e04eSSam Leffler 				 */
107145f856e3SSam Leffler 				m->m_flags |= M_AMPDU_MPDU;
1072b032f27cSSam Leffler 			} else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
1073b032f27cSSam Leffler 			    ic->ic_ampdu_enable(ni, tap)) {
107468e8e04eSSam Leffler 				/*
107568e8e04eSSam Leffler 				 * Not negotiated yet, request service.
107668e8e04eSSam Leffler 				 */
107768e8e04eSSam Leffler 				ieee80211_ampdu_request(ni, tap);
107868e8e04eSSam Leffler 			}
107968e8e04eSSam Leffler 		}
108068e8e04eSSam Leffler 		/* XXX works even when BA marked above */
10818a1b9b6aSSam Leffler 		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
1082b032f27cSSam Leffler 			qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1083b032f27cSSam Leffler 		qos[1] = 0;
1084b032f27cSSam Leffler 		wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
10858a1b9b6aSSam Leffler 
108645f856e3SSam Leffler 		if ((m->m_flags & M_AMPDU_MPDU) == 0) {
108745f856e3SSam Leffler 			/*
108845f856e3SSam Leffler 			 * NB: don't assign a sequence # to potential
108945f856e3SSam Leffler 			 * aggregates; we expect this happens at the
109045f856e3SSam Leffler 			 * point the frame comes off any aggregation q
109145f856e3SSam Leffler 			 * as otherwise we may introduce holes in the
109245f856e3SSam Leffler 			 * BA sequence space and/or make window accouting
109345f856e3SSam Leffler 			 * more difficult.
109445f856e3SSam Leffler 			 *
109545f856e3SSam Leffler 			 * XXX may want to control this with a driver
109645f856e3SSam Leffler 			 * capability; this may also change when we pull
109745f856e3SSam Leffler 			 * aggregation up into net80211
109845f856e3SSam Leffler 			 */
10999e80b1dfSSam Leffler 			seqno = ni->ni_txseqs[tid]++;
110068e8e04eSSam Leffler 			*(uint16_t *)wh->i_seq =
11019e80b1dfSSam Leffler 			    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
110245f856e3SSam Leffler 		}
11038a1b9b6aSSam Leffler 	} else {
11049e80b1dfSSam Leffler 		seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
110568e8e04eSSam Leffler 		*(uint16_t *)wh->i_seq =
11069e80b1dfSSam Leffler 		    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
11078a1b9b6aSSam Leffler 	}
110868e8e04eSSam Leffler 	/* check if xmit fragmentation is required */
1109b032f27cSSam Leffler 	txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
111068e8e04eSSam Leffler 	    !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1111b032f27cSSam Leffler 	    (vap->iv_caps & IEEE80211_C_TXFRAG) &&
1112e9cfc1f5SSam Leffler 	    (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
11135e923d2eSSam Leffler 	if (key != NULL) {
11145e923d2eSSam Leffler 		/*
11155e923d2eSSam Leffler 		 * IEEE 802.1X: send EAPOL frames always in the clear.
11165e923d2eSSam Leffler 		 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
11175e923d2eSSam Leffler 		 */
1118b032f27cSSam Leffler 		if ((m->m_flags & M_EAPOL) == 0 ||
1119b032f27cSSam Leffler 		    ((vap->iv_flags & IEEE80211_F_WPA) &&
1120b032f27cSSam Leffler 		     (vap->iv_opmode == IEEE80211_M_STA ?
1121cda15ce1SSam Leffler 		      !IEEE80211_KEY_UNDEFINED(key) :
1122cda15ce1SSam Leffler 		      !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
11235e923d2eSSam Leffler 			wh->i_fc[1] |= IEEE80211_FC1_WEP;
1124b032f27cSSam Leffler 			if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1125b032f27cSSam Leffler 				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1126b032f27cSSam Leffler 				    eh.ether_dhost,
1127b032f27cSSam Leffler 				    "%s", "enmic failed, discard frame");
1128b032f27cSSam Leffler 				vap->iv_stats.is_crypto_enmicfail++;
11295e923d2eSSam Leffler 				goto bad;
11305e923d2eSSam Leffler 			}
11315e923d2eSSam Leffler 		}
11325e923d2eSSam Leffler 	}
1133b032f27cSSam Leffler 	if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1134b032f27cSSam Leffler 	    key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
113568e8e04eSSam Leffler 		goto bad;
11368a1b9b6aSSam Leffler 
1137c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
1138c1af44bdSSam Leffler 
11398a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_data);
11409e80b1dfSSam Leffler 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1141b6e9b119SSam Leffler 		IEEE80211_NODE_STAT(ni, tx_mcast);
11429e80b1dfSSam Leffler 		m->m_flags |= M_MCAST;
11439e80b1dfSSam Leffler 	} else
1144b6e9b119SSam Leffler 		IEEE80211_NODE_STAT(ni, tx_ucast);
11458a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
11468a1b9b6aSSam Leffler 
11471a1e1d21SSam Leffler 	return m;
11480a915fadSSam Leffler bad:
11490a915fadSSam Leffler 	if (m != NULL)
11500a915fadSSam Leffler 		m_freem(m);
11510a915fadSSam Leffler 	return NULL;
1152b032f27cSSam Leffler #undef WH4
11531a1e1d21SSam Leffler }
11541a1e1d21SSam Leffler 
11551a1e1d21SSam Leffler /*
115668e8e04eSSam Leffler  * Fragment the frame according to the specified mtu.
115768e8e04eSSam Leffler  * The size of the 802.11 header (w/o padding) is provided
115868e8e04eSSam Leffler  * so we don't need to recalculate it.  We create a new
115968e8e04eSSam Leffler  * mbuf for each fragment and chain it through m_nextpkt;
116068e8e04eSSam Leffler  * we might be able to optimize this by reusing the original
116168e8e04eSSam Leffler  * packet's mbufs but that is significantly more complicated.
116268e8e04eSSam Leffler  */
116368e8e04eSSam Leffler static int
1164b032f27cSSam Leffler ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
116568e8e04eSSam Leffler 	u_int hdrsize, u_int ciphdrsize, u_int mtu)
116668e8e04eSSam Leffler {
116768e8e04eSSam Leffler 	struct ieee80211_frame *wh, *whf;
116868e8e04eSSam Leffler 	struct mbuf *m, *prev, *next;
116968e8e04eSSam Leffler 	u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
117068e8e04eSSam Leffler 
117168e8e04eSSam Leffler 	KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
117268e8e04eSSam Leffler 	KASSERT(m0->m_pkthdr.len > mtu,
117368e8e04eSSam Leffler 		("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
117468e8e04eSSam Leffler 
117568e8e04eSSam Leffler 	wh = mtod(m0, struct ieee80211_frame *);
117668e8e04eSSam Leffler 	/* NB: mark the first frag; it will be propagated below */
117768e8e04eSSam Leffler 	wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
117868e8e04eSSam Leffler 	totalhdrsize = hdrsize + ciphdrsize;
117968e8e04eSSam Leffler 	fragno = 1;
118068e8e04eSSam Leffler 	off = mtu - ciphdrsize;
118168e8e04eSSam Leffler 	remainder = m0->m_pkthdr.len - off;
118268e8e04eSSam Leffler 	prev = m0;
118368e8e04eSSam Leffler 	do {
118468e8e04eSSam Leffler 		fragsize = totalhdrsize + remainder;
118568e8e04eSSam Leffler 		if (fragsize > mtu)
118668e8e04eSSam Leffler 			fragsize = mtu;
1187b032f27cSSam Leffler 		/* XXX fragsize can be >2048! */
118868e8e04eSSam Leffler 		KASSERT(fragsize < MCLBYTES,
118968e8e04eSSam Leffler 			("fragment size %u too big!", fragsize));
119068e8e04eSSam Leffler 		if (fragsize > MHLEN)
119168e8e04eSSam Leffler 			m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
119268e8e04eSSam Leffler 		else
119368e8e04eSSam Leffler 			m = m_gethdr(M_DONTWAIT, MT_DATA);
119468e8e04eSSam Leffler 		if (m == NULL)
119568e8e04eSSam Leffler 			goto bad;
119668e8e04eSSam Leffler 		/* leave room to prepend any cipher header */
119768e8e04eSSam Leffler 		m_align(m, fragsize - ciphdrsize);
119868e8e04eSSam Leffler 
119968e8e04eSSam Leffler 		/*
120068e8e04eSSam Leffler 		 * Form the header in the fragment.  Note that since
120168e8e04eSSam Leffler 		 * we mark the first fragment with the MORE_FRAG bit
120268e8e04eSSam Leffler 		 * it automatically is propagated to each fragment; we
120368e8e04eSSam Leffler 		 * need only clear it on the last fragment (done below).
120468e8e04eSSam Leffler 		 */
120568e8e04eSSam Leffler 		whf = mtod(m, struct ieee80211_frame *);
120668e8e04eSSam Leffler 		memcpy(whf, wh, hdrsize);
120768e8e04eSSam Leffler 		*(uint16_t *)&whf->i_seq[0] |= htole16(
120868e8e04eSSam Leffler 			(fragno & IEEE80211_SEQ_FRAG_MASK) <<
120968e8e04eSSam Leffler 				IEEE80211_SEQ_FRAG_SHIFT);
121068e8e04eSSam Leffler 		fragno++;
121168e8e04eSSam Leffler 
121268e8e04eSSam Leffler 		payload = fragsize - totalhdrsize;
121368e8e04eSSam Leffler 		/* NB: destination is known to be contiguous */
121468e8e04eSSam Leffler 		m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrsize);
121568e8e04eSSam Leffler 		m->m_len = hdrsize + payload;
121668e8e04eSSam Leffler 		m->m_pkthdr.len = hdrsize + payload;
121768e8e04eSSam Leffler 		m->m_flags |= M_FRAG;
121868e8e04eSSam Leffler 
121968e8e04eSSam Leffler 		/* chain up the fragment */
122068e8e04eSSam Leffler 		prev->m_nextpkt = m;
122168e8e04eSSam Leffler 		prev = m;
122268e8e04eSSam Leffler 
122368e8e04eSSam Leffler 		/* deduct fragment just formed */
122468e8e04eSSam Leffler 		remainder -= payload;
122568e8e04eSSam Leffler 		off += payload;
122668e8e04eSSam Leffler 	} while (remainder != 0);
122751cec121SWeongyo Jeong 
122851cec121SWeongyo Jeong 	/* set the last fragment */
122951cec121SWeongyo Jeong 	m->m_flags |= M_LASTFRAG;
123068e8e04eSSam Leffler 	whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
123168e8e04eSSam Leffler 
123268e8e04eSSam Leffler 	/* strip first mbuf now that everything has been copied */
123368e8e04eSSam Leffler 	m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
123468e8e04eSSam Leffler 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
123568e8e04eSSam Leffler 
1236b032f27cSSam Leffler 	vap->iv_stats.is_tx_fragframes++;
1237b032f27cSSam Leffler 	vap->iv_stats.is_tx_frags += fragno-1;
123868e8e04eSSam Leffler 
123968e8e04eSSam Leffler 	return 1;
124068e8e04eSSam Leffler bad:
124168e8e04eSSam Leffler 	/* reclaim fragments but leave original frame for caller to free */
124268e8e04eSSam Leffler 	for (m = m0->m_nextpkt; m != NULL; m = next) {
124368e8e04eSSam Leffler 		next = m->m_nextpkt;
124468e8e04eSSam Leffler 		m->m_nextpkt = NULL;		/* XXX paranoid */
124568e8e04eSSam Leffler 		m_freem(m);
124668e8e04eSSam Leffler 	}
124768e8e04eSSam Leffler 	m0->m_nextpkt = NULL;
124868e8e04eSSam Leffler 	return 0;
124968e8e04eSSam Leffler }
125068e8e04eSSam Leffler 
125168e8e04eSSam Leffler /*
12521a1e1d21SSam Leffler  * Add a supported rates element id to a frame.
12531a1e1d21SSam Leffler  */
125468e8e04eSSam Leffler static uint8_t *
125568e8e04eSSam Leffler ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
12561a1e1d21SSam Leffler {
12571a1e1d21SSam Leffler 	int nrates;
12581a1e1d21SSam Leffler 
12591a1e1d21SSam Leffler 	*frm++ = IEEE80211_ELEMID_RATES;
12601a1e1d21SSam Leffler 	nrates = rs->rs_nrates;
12611a1e1d21SSam Leffler 	if (nrates > IEEE80211_RATE_SIZE)
12621a1e1d21SSam Leffler 		nrates = IEEE80211_RATE_SIZE;
12631a1e1d21SSam Leffler 	*frm++ = nrates;
12641a1e1d21SSam Leffler 	memcpy(frm, rs->rs_rates, nrates);
12651a1e1d21SSam Leffler 	return frm + nrates;
12661a1e1d21SSam Leffler }
12671a1e1d21SSam Leffler 
12681a1e1d21SSam Leffler /*
12691a1e1d21SSam Leffler  * Add an extended supported rates element id to a frame.
12701a1e1d21SSam Leffler  */
127168e8e04eSSam Leffler static uint8_t *
127268e8e04eSSam Leffler ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
12731a1e1d21SSam Leffler {
12741a1e1d21SSam Leffler 	/*
12751a1e1d21SSam Leffler 	 * Add an extended supported rates element if operating in 11g mode.
12761a1e1d21SSam Leffler 	 */
12771a1e1d21SSam Leffler 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
12781a1e1d21SSam Leffler 		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
12791a1e1d21SSam Leffler 		*frm++ = IEEE80211_ELEMID_XRATES;
12801a1e1d21SSam Leffler 		*frm++ = nrates;
12811a1e1d21SSam Leffler 		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
12821a1e1d21SSam Leffler 		frm += nrates;
12831a1e1d21SSam Leffler 	}
12841a1e1d21SSam Leffler 	return frm;
12851a1e1d21SSam Leffler }
12861a1e1d21SSam Leffler 
12871a1e1d21SSam Leffler /*
1288b032f27cSSam Leffler  * Add an ssid element to a frame.
12891a1e1d21SSam Leffler  */
129068e8e04eSSam Leffler static uint8_t *
129168e8e04eSSam Leffler ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
12921a1e1d21SSam Leffler {
12931a1e1d21SSam Leffler 	*frm++ = IEEE80211_ELEMID_SSID;
12941a1e1d21SSam Leffler 	*frm++ = len;
12951a1e1d21SSam Leffler 	memcpy(frm, ssid, len);
12961a1e1d21SSam Leffler 	return frm + len;
12971a1e1d21SSam Leffler }
12981a1e1d21SSam Leffler 
12998a1b9b6aSSam Leffler /*
13008a1b9b6aSSam Leffler  * Add an erp element to a frame.
13018a1b9b6aSSam Leffler  */
130268e8e04eSSam Leffler static uint8_t *
130368e8e04eSSam Leffler ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic)
13041a1e1d21SSam Leffler {
130568e8e04eSSam Leffler 	uint8_t erp;
13061a1e1d21SSam Leffler 
13078a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_ERP;
13088a1b9b6aSSam Leffler 	*frm++ = 1;
13098a1b9b6aSSam Leffler 	erp = 0;
13108a1b9b6aSSam Leffler 	if (ic->ic_nonerpsta != 0)
13118a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_NON_ERP_PRESENT;
13128a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_USEPROT)
13138a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_USE_PROTECTION;
13148a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_USEBARKER)
13158a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_LONG_PREAMBLE;
13168a1b9b6aSSam Leffler 	*frm++ = erp;
13178a1b9b6aSSam Leffler 	return frm;
13181a1e1d21SSam Leffler }
13191a1e1d21SSam Leffler 
13208a1b9b6aSSam Leffler /*
1321b032f27cSSam Leffler  * Add a CFParams element to a frame.
13228a1b9b6aSSam Leffler  */
132368e8e04eSSam Leffler static uint8_t *
1324b032f27cSSam Leffler ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
13258a1b9b6aSSam Leffler {
1326b032f27cSSam Leffler #define	ADDSHORT(frm, v) do {			\
1327b032f27cSSam Leffler 	frm[0] = (v) & 0xff;			\
1328b032f27cSSam Leffler 	frm[1] = (v) >> 8;			\
1329b032f27cSSam Leffler 	frm += 2;				\
1330b032f27cSSam Leffler } while (0)
1331b032f27cSSam Leffler 	*frm++ = IEEE80211_ELEMID_CFPARMS;
1332b032f27cSSam Leffler 	*frm++ = 6;
1333b032f27cSSam Leffler 	*frm++ = 0;		/* CFP count */
1334b032f27cSSam Leffler 	*frm++ = 2;		/* CFP period */
1335b032f27cSSam Leffler 	ADDSHORT(frm, 0);	/* CFP MaxDuration (TU) */
1336b032f27cSSam Leffler 	ADDSHORT(frm, 0);	/* CFP CurRemaining (TU) */
13378a1b9b6aSSam Leffler 	return frm;
1338b032f27cSSam Leffler #undef ADDSHORT
1339b032f27cSSam Leffler }
1340b032f27cSSam Leffler 
1341b032f27cSSam Leffler static __inline uint8_t *
1342b032f27cSSam Leffler add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
1343b032f27cSSam Leffler {
1344b032f27cSSam Leffler 	memcpy(frm, ie->ie_data, ie->ie_len);
1345b032f27cSSam Leffler 	return frm + ie->ie_len;
1346b032f27cSSam Leffler }
1347b032f27cSSam Leffler 
1348b032f27cSSam Leffler static __inline uint8_t *
1349b032f27cSSam Leffler add_ie(uint8_t *frm, const uint8_t *ie)
1350b032f27cSSam Leffler {
1351b032f27cSSam Leffler 	memcpy(frm, ie, 2 + ie[1]);
1352b032f27cSSam Leffler 	return frm + 2 + ie[1];
13538a1b9b6aSSam Leffler }
13548a1b9b6aSSam Leffler 
13558a1b9b6aSSam Leffler #define	WME_OUI_BYTES		0x00, 0x50, 0xf2
13568a1b9b6aSSam Leffler /*
13578a1b9b6aSSam Leffler  * Add a WME information element to a frame.
13588a1b9b6aSSam Leffler  */
135968e8e04eSSam Leffler static uint8_t *
136068e8e04eSSam Leffler ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme)
13618a1b9b6aSSam Leffler {
13628a1b9b6aSSam Leffler 	static const struct ieee80211_wme_info info = {
13638a1b9b6aSSam Leffler 		.wme_id		= IEEE80211_ELEMID_VENDOR,
13648a1b9b6aSSam Leffler 		.wme_len	= sizeof(struct ieee80211_wme_info) - 2,
13658a1b9b6aSSam Leffler 		.wme_oui	= { WME_OUI_BYTES },
13668a1b9b6aSSam Leffler 		.wme_type	= WME_OUI_TYPE,
13678a1b9b6aSSam Leffler 		.wme_subtype	= WME_INFO_OUI_SUBTYPE,
13688a1b9b6aSSam Leffler 		.wme_version	= WME_VERSION,
13698a1b9b6aSSam Leffler 		.wme_info	= 0,
13708a1b9b6aSSam Leffler 	};
13718a1b9b6aSSam Leffler 	memcpy(frm, &info, sizeof(info));
13728a1b9b6aSSam Leffler 	return frm + sizeof(info);
13738a1b9b6aSSam Leffler }
13748a1b9b6aSSam Leffler 
13758a1b9b6aSSam Leffler /*
13768a1b9b6aSSam Leffler  * Add a WME parameters element to a frame.
13778a1b9b6aSSam Leffler  */
137868e8e04eSSam Leffler static uint8_t *
137968e8e04eSSam Leffler ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme)
13808a1b9b6aSSam Leffler {
13818a1b9b6aSSam Leffler #define	SM(_v, _f)	(((_v) << _f##_S) & _f)
13828a1b9b6aSSam Leffler #define	ADDSHORT(frm, v) do {			\
13838a1b9b6aSSam Leffler 	frm[0] = (v) & 0xff;			\
13848a1b9b6aSSam Leffler 	frm[1] = (v) >> 8;			\
13858a1b9b6aSSam Leffler 	frm += 2;				\
13868a1b9b6aSSam Leffler } while (0)
13878a1b9b6aSSam Leffler 	/* NB: this works 'cuz a param has an info at the front */
13888a1b9b6aSSam Leffler 	static const struct ieee80211_wme_info param = {
13898a1b9b6aSSam Leffler 		.wme_id		= IEEE80211_ELEMID_VENDOR,
13908a1b9b6aSSam Leffler 		.wme_len	= sizeof(struct ieee80211_wme_param) - 2,
13918a1b9b6aSSam Leffler 		.wme_oui	= { WME_OUI_BYTES },
13928a1b9b6aSSam Leffler 		.wme_type	= WME_OUI_TYPE,
13938a1b9b6aSSam Leffler 		.wme_subtype	= WME_PARAM_OUI_SUBTYPE,
13948a1b9b6aSSam Leffler 		.wme_version	= WME_VERSION,
13958a1b9b6aSSam Leffler 	};
13968a1b9b6aSSam Leffler 	int i;
13978a1b9b6aSSam Leffler 
13988a1b9b6aSSam Leffler 	memcpy(frm, &param, sizeof(param));
13998a1b9b6aSSam Leffler 	frm += __offsetof(struct ieee80211_wme_info, wme_info);
14008a1b9b6aSSam Leffler 	*frm++ = wme->wme_bssChanParams.cap_info;	/* AC info */
14018a1b9b6aSSam Leffler 	*frm++ = 0;					/* reserved field */
14028a1b9b6aSSam Leffler 	for (i = 0; i < WME_NUM_AC; i++) {
14038a1b9b6aSSam Leffler 		const struct wmeParams *ac =
14048a1b9b6aSSam Leffler 		       &wme->wme_bssChanParams.cap_wmeParams[i];
14058a1b9b6aSSam Leffler 		*frm++ = SM(i, WME_PARAM_ACI)
14068a1b9b6aSSam Leffler 		       | SM(ac->wmep_acm, WME_PARAM_ACM)
14078a1b9b6aSSam Leffler 		       | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
14088a1b9b6aSSam Leffler 		       ;
14098a1b9b6aSSam Leffler 		*frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
14108a1b9b6aSSam Leffler 		       | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
14118a1b9b6aSSam Leffler 		       ;
14128a1b9b6aSSam Leffler 		ADDSHORT(frm, ac->wmep_txopLimit);
14138a1b9b6aSSam Leffler 	}
14148a1b9b6aSSam Leffler 	return frm;
14158a1b9b6aSSam Leffler #undef SM
14168a1b9b6aSSam Leffler #undef ADDSHORT
14178a1b9b6aSSam Leffler }
14188a1b9b6aSSam Leffler #undef WME_OUI_BYTES
14198a1b9b6aSSam Leffler 
14200a915fadSSam Leffler /*
1421b032f27cSSam Leffler  * Add an 11h Power Constraint element to a frame.
1422b032f27cSSam Leffler  */
1423b032f27cSSam Leffler static uint8_t *
1424b032f27cSSam Leffler ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
1425b032f27cSSam Leffler {
1426b032f27cSSam Leffler 	const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
1427b032f27cSSam Leffler 	/* XXX per-vap tx power limit? */
1428b032f27cSSam Leffler 	int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
1429b032f27cSSam Leffler 
1430b032f27cSSam Leffler 	frm[0] = IEEE80211_ELEMID_PWRCNSTR;
1431b032f27cSSam Leffler 	frm[1] = 1;
1432b032f27cSSam Leffler 	frm[2] = c->ic_maxregpower > limit ?  c->ic_maxregpower - limit : 0;
1433b032f27cSSam Leffler 	return frm + 3;
1434b032f27cSSam Leffler }
1435b032f27cSSam Leffler 
1436b032f27cSSam Leffler /*
1437b032f27cSSam Leffler  * Add an 11h Power Capability element to a frame.
1438b032f27cSSam Leffler  */
1439b032f27cSSam Leffler static uint8_t *
1440b032f27cSSam Leffler ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
1441b032f27cSSam Leffler {
1442b032f27cSSam Leffler 	frm[0] = IEEE80211_ELEMID_PWRCAP;
1443b032f27cSSam Leffler 	frm[1] = 2;
1444b032f27cSSam Leffler 	frm[2] = c->ic_minpower;
1445b032f27cSSam Leffler 	frm[3] = c->ic_maxpower;
1446b032f27cSSam Leffler 	return frm + 4;
1447b032f27cSSam Leffler }
1448b032f27cSSam Leffler 
1449b032f27cSSam Leffler /*
1450b032f27cSSam Leffler  * Add an 11h Supported Channels element to a frame.
1451b032f27cSSam Leffler  */
1452b032f27cSSam Leffler static uint8_t *
1453b032f27cSSam Leffler ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
1454b032f27cSSam Leffler {
1455b032f27cSSam Leffler 	static const int ielen = 26;
1456b032f27cSSam Leffler 
1457b032f27cSSam Leffler 	frm[0] = IEEE80211_ELEMID_SUPPCHAN;
1458b032f27cSSam Leffler 	frm[1] = ielen;
1459b032f27cSSam Leffler 	/* XXX not correct */
1460b032f27cSSam Leffler 	memcpy(frm+2, ic->ic_chan_avail, ielen);
1461b032f27cSSam Leffler 	return frm + 2 + ielen;
1462b032f27cSSam Leffler }
1463b032f27cSSam Leffler 
1464b032f27cSSam Leffler /*
1465b032f27cSSam Leffler  * Add an 11h Channel Switch Announcement element to a frame.
1466b032f27cSSam Leffler  * Note that we use the per-vap CSA count to adjust the global
1467b032f27cSSam Leffler  * counter so we can use this routine to form probe response
1468b032f27cSSam Leffler  * frames and get the current count.
1469b032f27cSSam Leffler  */
1470b032f27cSSam Leffler static uint8_t *
1471b032f27cSSam Leffler ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
1472b032f27cSSam Leffler {
1473b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1474b032f27cSSam Leffler 	struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
1475b032f27cSSam Leffler 
1476b032f27cSSam Leffler 	csa->csa_ie = IEEE80211_ELEMID_CHANSWITCHANN;
1477b032f27cSSam Leffler 	csa->csa_len = 3;
1478b032f27cSSam Leffler 	csa->csa_mode = 1;		/* XXX force quiet on channel */
1479b032f27cSSam Leffler 	csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
1480b032f27cSSam Leffler 	csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
1481b032f27cSSam Leffler 	return frm + sizeof(*csa);
1482b032f27cSSam Leffler }
1483b032f27cSSam Leffler 
1484b032f27cSSam Leffler /*
1485b032f27cSSam Leffler  * Add an 11h country information element to a frame.
1486b032f27cSSam Leffler  */
1487b032f27cSSam Leffler static uint8_t *
1488b032f27cSSam Leffler ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
1489b032f27cSSam Leffler {
1490b032f27cSSam Leffler 
1491b032f27cSSam Leffler 	if (ic->ic_countryie == NULL ||
1492b032f27cSSam Leffler 	    ic->ic_countryie_chan != ic->ic_bsschan) {
1493b032f27cSSam Leffler 		/*
1494b032f27cSSam Leffler 		 * Handle lazy construction of ie.  This is done on
1495b032f27cSSam Leffler 		 * first use and after a channel change that requires
1496b032f27cSSam Leffler 		 * re-calculation.
1497b032f27cSSam Leffler 		 */
1498b032f27cSSam Leffler 		if (ic->ic_countryie != NULL)
1499b032f27cSSam Leffler 			free(ic->ic_countryie, M_80211_NODE_IE);
1500b032f27cSSam Leffler 		ic->ic_countryie = ieee80211_alloc_countryie(ic);
1501b032f27cSSam Leffler 		if (ic->ic_countryie == NULL)
1502b032f27cSSam Leffler 			return frm;
1503b032f27cSSam Leffler 		ic->ic_countryie_chan = ic->ic_bsschan;
1504b032f27cSSam Leffler 	}
1505b032f27cSSam Leffler 	return add_appie(frm, ic->ic_countryie);
1506b032f27cSSam Leffler }
1507b032f27cSSam Leffler 
1508b032f27cSSam Leffler /*
1509af8418dcSSam Leffler  * Send a probe request frame with the specified ssid
1510af8418dcSSam Leffler  * and any optional information element data.
1511af8418dcSSam Leffler  */
1512af8418dcSSam Leffler int
1513af8418dcSSam Leffler ieee80211_send_probereq(struct ieee80211_node *ni,
151468e8e04eSSam Leffler 	const uint8_t sa[IEEE80211_ADDR_LEN],
151568e8e04eSSam Leffler 	const uint8_t da[IEEE80211_ADDR_LEN],
151668e8e04eSSam Leffler 	const uint8_t bssid[IEEE80211_ADDR_LEN],
1517b032f27cSSam Leffler 	const uint8_t *ssid, size_t ssidlen)
1518af8418dcSSam Leffler {
1519b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1520af8418dcSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
152116d7cbb1SSam Leffler 	const struct ieee80211_txparam *tp;
152216d7cbb1SSam Leffler 	struct ieee80211_bpf_params params;
1523af8418dcSSam Leffler 	struct ieee80211_frame *wh;
152441b3c790SSam Leffler 	const struct ieee80211_rateset *rs;
1525af8418dcSSam Leffler 	struct mbuf *m;
152668e8e04eSSam Leffler 	uint8_t *frm;
1527af8418dcSSam Leffler 
1528b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
1529b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
1530b032f27cSSam Leffler 		    "block %s frame in CAC state", "probe request");
1531b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
1532b032f27cSSam Leffler 		return EIO;		/* XXX */
1533b032f27cSSam Leffler 	}
1534b032f27cSSam Leffler 
1535af8418dcSSam Leffler 	/*
1536af8418dcSSam Leffler 	 * Hold a reference on the node so it doesn't go away until after
1537af8418dcSSam Leffler 	 * the xmit is complete all the way in the driver.  On error we
1538af8418dcSSam Leffler 	 * will remove our reference.
1539af8418dcSSam Leffler 	 */
1540b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1541af8418dcSSam Leffler 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1542af8418dcSSam Leffler 		__func__, __LINE__,
1543af8418dcSSam Leffler 		ni, ether_sprintf(ni->ni_macaddr),
1544af8418dcSSam Leffler 		ieee80211_node_refcnt(ni)+1);
1545af8418dcSSam Leffler 	ieee80211_ref_node(ni);
1546af8418dcSSam Leffler 
1547af8418dcSSam Leffler 	/*
1548af8418dcSSam Leffler 	 * prreq frame format
1549af8418dcSSam Leffler 	 *	[tlv] ssid
1550af8418dcSSam Leffler 	 *	[tlv] supported rates
1551b032f27cSSam Leffler 	 *	[tlv] RSN (optional)
1552af8418dcSSam Leffler 	 *	[tlv] extended supported rates
1553b032f27cSSam Leffler 	 *	[tlv] WPA (optional)
1554af8418dcSSam Leffler 	 *	[tlv] user-specified ie's
1555af8418dcSSam Leffler 	 */
1556af8418dcSSam Leffler 	m = ieee80211_getmgtframe(&frm,
155768e8e04eSSam Leffler 		 ic->ic_headroom + sizeof(struct ieee80211_frame),
1558af8418dcSSam Leffler 	       	 2 + IEEE80211_NWID_LEN
1559af8418dcSSam Leffler 	       + 2 + IEEE80211_RATE_SIZE
1560b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_wpa)
1561af8418dcSSam Leffler 	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1562b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_wpa)
1563b032f27cSSam Leffler 	       + (vap->iv_appie_probereq != NULL ?
1564b032f27cSSam Leffler 		   vap->iv_appie_probereq->ie_len : 0)
1565af8418dcSSam Leffler 	);
1566af8418dcSSam Leffler 	if (m == NULL) {
1567b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
1568af8418dcSSam Leffler 		ieee80211_free_node(ni);
1569af8418dcSSam Leffler 		return ENOMEM;
1570af8418dcSSam Leffler 	}
1571af8418dcSSam Leffler 
1572af8418dcSSam Leffler 	frm = ieee80211_add_ssid(frm, ssid, ssidlen);
157341b3c790SSam Leffler 	rs = ieee80211_get_suprates(ic, ic->ic_curchan);
157441b3c790SSam Leffler 	frm = ieee80211_add_rates(frm, rs);
1575b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA2) {
1576b032f27cSSam Leffler 		if (vap->iv_rsn_ie != NULL)
1577b032f27cSSam Leffler 			frm = add_ie(frm, vap->iv_rsn_ie);
1578b032f27cSSam Leffler 		/* XXX else complain? */
1579af8418dcSSam Leffler 	}
1580b032f27cSSam Leffler 	frm = ieee80211_add_xrates(frm, rs);
1581b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA1) {
1582b032f27cSSam Leffler 		if (vap->iv_wpa_ie != NULL)
1583b032f27cSSam Leffler 			frm = add_ie(frm, vap->iv_wpa_ie);
1584b032f27cSSam Leffler 		/* XXX else complain? */
1585b032f27cSSam Leffler 	}
1586b032f27cSSam Leffler 	if (vap->iv_appie_probereq != NULL)
1587b032f27cSSam Leffler 		frm = add_appie(frm, vap->iv_appie_probereq);
158868e8e04eSSam Leffler 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1589af8418dcSSam Leffler 
159016d7cbb1SSam Leffler 	KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
159116d7cbb1SSam Leffler 	    ("leading space %zd", M_LEADINGSPACE(m)));
1592af8418dcSSam Leffler 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
159316d7cbb1SSam Leffler 	if (m == NULL) {
159416d7cbb1SSam Leffler 		/* NB: cannot happen */
159516d7cbb1SSam Leffler 		ieee80211_free_node(ni);
1596af8418dcSSam Leffler 		return ENOMEM;
159716d7cbb1SSam Leffler 	}
1598af8418dcSSam Leffler 
1599af8418dcSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
16009e80b1dfSSam Leffler 	ieee80211_send_setup(ni, m,
1601af8418dcSSam Leffler 	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
16028ac160cdSSam Leffler 	     IEEE80211_NONQOS_TID, sa, da, bssid);
1603af8418dcSSam Leffler 	/* XXX power management? */
1604c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
1605af8418dcSSam Leffler 
1606b032f27cSSam Leffler 	M_WME_SETAC(m, WME_AC_BE);
1607b032f27cSSam Leffler 
1608af8418dcSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_probereq);
1609af8418dcSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_mgmt);
1610af8418dcSSam Leffler 
1611b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1612b032f27cSSam Leffler 	    "send probe req on channel %u bssid %s ssid \"%.*s\"\n",
1613b032f27cSSam Leffler 	    ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(bssid),
1614b032f27cSSam Leffler 	    ssidlen, ssid);
1615af8418dcSSam Leffler 
161616d7cbb1SSam Leffler 	memset(&params, 0, sizeof(params));
161716d7cbb1SSam Leffler 	params.ibp_pri = M_WME_GETAC(m);
161816d7cbb1SSam Leffler 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
161916d7cbb1SSam Leffler 	params.ibp_rate0 = tp->mgmtrate;
162016d7cbb1SSam Leffler 	if (IEEE80211_IS_MULTICAST(da)) {
162116d7cbb1SSam Leffler 		params.ibp_flags |= IEEE80211_BPF_NOACK;
162216d7cbb1SSam Leffler 		params.ibp_try0 = 1;
162316d7cbb1SSam Leffler 	} else
162416d7cbb1SSam Leffler 		params.ibp_try0 = tp->maxretry;
162516d7cbb1SSam Leffler 	params.ibp_power = ni->ni_txpower;
162616d7cbb1SSam Leffler 	return ic->ic_raw_xmit(ni, m, &params);
1627af8418dcSSam Leffler }
1628af8418dcSSam Leffler 
1629af8418dcSSam Leffler /*
1630667dad55SSam Leffler  * Calculate capability information for mgt frames.
1631667dad55SSam Leffler  */
163268e8e04eSSam Leffler static uint16_t
1633b032f27cSSam Leffler getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
1634667dad55SSam Leffler {
1635b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
163668e8e04eSSam Leffler 	uint16_t capinfo;
1637667dad55SSam Leffler 
1638b032f27cSSam Leffler 	KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
1639667dad55SSam Leffler 
1640b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1641667dad55SSam Leffler 		capinfo = IEEE80211_CAPINFO_ESS;
1642b032f27cSSam Leffler 	else if (vap->iv_opmode == IEEE80211_M_IBSS)
1643667dad55SSam Leffler 		capinfo = IEEE80211_CAPINFO_IBSS;
1644667dad55SSam Leffler 	else
1645667dad55SSam Leffler 		capinfo = 0;
1646b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY)
1647667dad55SSam Leffler 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
1648667dad55SSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1649667dad55SSam Leffler 	    IEEE80211_IS_CHAN_2GHZ(chan))
1650667dad55SSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1651667dad55SSam Leffler 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
1652667dad55SSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1653b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
1654b032f27cSSam Leffler 		capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
1655667dad55SSam Leffler 	return capinfo;
1656667dad55SSam Leffler }
1657667dad55SSam Leffler 
1658667dad55SSam Leffler /*
16590a915fadSSam Leffler  * Send a management frame.  The node is for the destination (or ic_bss
16600a915fadSSam Leffler  * when in station mode).  Nodes other than ic_bss have their reference
16610a915fadSSam Leffler  * count bumped to reflect our use for an indeterminant time.
16620a915fadSSam Leffler  */
16631a1e1d21SSam Leffler int
1664b032f27cSSam Leffler ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
16651a1e1d21SSam Leffler {
16661b6167d2SSam Leffler #define	HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
1667b032f27cSSam Leffler #define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
1668b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1669b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1670b032f27cSSam Leffler 	struct ieee80211_node *bss = vap->iv_bss;
16718ac160cdSSam Leffler 	struct ieee80211_bpf_params params;
16721a1e1d21SSam Leffler 	struct mbuf *m;
167368e8e04eSSam Leffler 	uint8_t *frm;
167468e8e04eSSam Leffler 	uint16_t capinfo;
167568e8e04eSSam Leffler 	int has_challenge, is_shared_key, ret, status;
16761a1e1d21SSam Leffler 
16770a915fadSSam Leffler 	KASSERT(ni != NULL, ("null node"));
16780a915fadSSam Leffler 
16790a915fadSSam Leffler 	/*
16800a915fadSSam Leffler 	 * Hold a reference on the node so it doesn't go away until after
16810a915fadSSam Leffler 	 * the xmit is complete all the way in the driver.  On error we
16820a915fadSSam Leffler 	 * will remove our reference.
16830a915fadSSam Leffler 	 */
1684b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
168549a15236SSam Leffler 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
16868a1b9b6aSSam Leffler 		__func__, __LINE__,
168749a15236SSam Leffler 		ni, ether_sprintf(ni->ni_macaddr),
16888a1b9b6aSSam Leffler 		ieee80211_node_refcnt(ni)+1);
16890a915fadSSam Leffler 	ieee80211_ref_node(ni);
16908a1b9b6aSSam Leffler 
16918ac160cdSSam Leffler 	memset(&params, 0, sizeof(params));
16921a1e1d21SSam Leffler 	switch (type) {
16931a1e1d21SSam Leffler 
16941a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_AUTH:
16958a1b9b6aSSam Leffler 		status = arg >> 16;
16968a1b9b6aSSam Leffler 		arg &= 0xffff;
16978a1b9b6aSSam Leffler 		has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
16988a1b9b6aSSam Leffler 		    arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
16998a1b9b6aSSam Leffler 		    ni->ni_challenge != NULL);
17008a1b9b6aSSam Leffler 
17018a1b9b6aSSam Leffler 		/*
17028a1b9b6aSSam Leffler 		 * Deduce whether we're doing open authentication or
17038a1b9b6aSSam Leffler 		 * shared key authentication.  We do the latter if
17048a1b9b6aSSam Leffler 		 * we're in the middle of a shared key authentication
17058a1b9b6aSSam Leffler 		 * handshake or if we're initiating an authentication
17068a1b9b6aSSam Leffler 		 * request and configured to use shared key.
17078a1b9b6aSSam Leffler 		 */
17088a1b9b6aSSam Leffler 		is_shared_key = has_challenge ||
17098a1b9b6aSSam Leffler 		     arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
17108a1b9b6aSSam Leffler 		     (arg == IEEE80211_AUTH_SHARED_REQUEST &&
1711b032f27cSSam Leffler 		      bss->ni_authmode == IEEE80211_AUTH_SHARED);
17128a1b9b6aSSam Leffler 
17138a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
171468e8e04eSSam Leffler 			  ic->ic_headroom + sizeof(struct ieee80211_frame),
171568e8e04eSSam Leffler 			  3 * sizeof(uint16_t)
17168a1b9b6aSSam Leffler 			+ (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
171768e8e04eSSam Leffler 				sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)
17188a1b9b6aSSam Leffler 		);
17191a1e1d21SSam Leffler 		if (m == NULL)
17208a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
17218a1b9b6aSSam Leffler 
172268e8e04eSSam Leffler 		((uint16_t *)frm)[0] =
17238a1b9b6aSSam Leffler 		    (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
17248a1b9b6aSSam Leffler 		                    : htole16(IEEE80211_AUTH_ALG_OPEN);
172568e8e04eSSam Leffler 		((uint16_t *)frm)[1] = htole16(arg);	/* sequence number */
172668e8e04eSSam Leffler 		((uint16_t *)frm)[2] = htole16(status);/* status */
17278a1b9b6aSSam Leffler 
17288a1b9b6aSSam Leffler 		if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
172968e8e04eSSam Leffler 			((uint16_t *)frm)[3] =
17308a1b9b6aSSam Leffler 			    htole16((IEEE80211_CHALLENGE_LEN << 8) |
17318a1b9b6aSSam Leffler 			    IEEE80211_ELEMID_CHALLENGE);
173268e8e04eSSam Leffler 			memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
17338a1b9b6aSSam Leffler 			    IEEE80211_CHALLENGE_LEN);
17348a1b9b6aSSam Leffler 			m->m_pkthdr.len = m->m_len =
173568e8e04eSSam Leffler 				4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
17368a1b9b6aSSam Leffler 			if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
1737b032f27cSSam Leffler 				IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1738b032f27cSSam Leffler 				    "request encrypt frame (%s)", __func__);
17398ac160cdSSam Leffler 				/* mark frame for encryption */
17408ac160cdSSam Leffler 				params.ibp_flags |= IEEE80211_BPF_CRYPTO;
17418a1b9b6aSSam Leffler 			}
17428a1b9b6aSSam Leffler 		} else
174368e8e04eSSam Leffler 			m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
17448a1b9b6aSSam Leffler 
17458a1b9b6aSSam Leffler 		/* XXX not right for shared key */
17468a1b9b6aSSam Leffler 		if (status == IEEE80211_STATUS_SUCCESS)
17478a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_auth);
17488a1b9b6aSSam Leffler 		else
17498a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_auth_fail);
17508a1b9b6aSSam Leffler 
1751b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA)
175268e8e04eSSam Leffler 			ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
1753b032f27cSSam Leffler 				(void *) vap->iv_state);
17541a1e1d21SSam Leffler 		break;
17551a1e1d21SSam Leffler 
17561a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
1757b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1758b032f27cSSam Leffler 		    "send station deauthenticate (reason %d)", arg);
175968e8e04eSSam Leffler 		m = ieee80211_getmgtframe(&frm,
176068e8e04eSSam Leffler 			ic->ic_headroom + sizeof(struct ieee80211_frame),
176168e8e04eSSam Leffler 			sizeof(uint16_t));
17621a1e1d21SSam Leffler 		if (m == NULL)
17638a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
176468e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(arg);	/* reason */
176568e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
17668a1b9b6aSSam Leffler 
17678a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, tx_deauth);
17688a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
17698a1b9b6aSSam Leffler 
1770e4918ecdSSam Leffler 		ieee80211_node_unauthorize(ni);		/* port closed */
17711a1e1d21SSam Leffler 		break;
17721a1e1d21SSam Leffler 
17731a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
17741a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
17751a1e1d21SSam Leffler 		/*
17761a1e1d21SSam Leffler 		 * asreq frame format
17771a1e1d21SSam Leffler 		 *	[2] capability information
17781a1e1d21SSam Leffler 		 *	[2] listen interval
17791a1e1d21SSam Leffler 		 *	[6*] current AP address (reassoc only)
17801a1e1d21SSam Leffler 		 *	[tlv] ssid
17811a1e1d21SSam Leffler 		 *	[tlv] supported rates
17821a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
1783b032f27cSSam Leffler 		 *	[4] power capability (optional)
1784b032f27cSSam Leffler 		 *	[28] supported channels (optional)
178568e8e04eSSam Leffler 		 *	[tlv] HT capabilities
1786b032f27cSSam Leffler 		 *	[tlv] WME (optional)
178768e8e04eSSam Leffler 		 *	[tlv] Vendor OUI HT capabilities (optional)
178868e8e04eSSam Leffler 		 *	[tlv] Atheros capabilities (if negotiated)
1789b032f27cSSam Leffler 		 *	[tlv] AppIE's (optional)
17901a1e1d21SSam Leffler 		 */
17918a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
179268e8e04eSSam Leffler 			 ic->ic_headroom + sizeof(struct ieee80211_frame),
179368e8e04eSSam Leffler 			 sizeof(uint16_t)
179468e8e04eSSam Leffler 		       + sizeof(uint16_t)
17951a1e1d21SSam Leffler 		       + IEEE80211_ADDR_LEN
17968a1b9b6aSSam Leffler 		       + 2 + IEEE80211_NWID_LEN
17971a1e1d21SSam Leffler 		       + 2 + IEEE80211_RATE_SIZE
17988a1b9b6aSSam Leffler 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1799b032f27cSSam Leffler 		       + 4
1800b032f27cSSam Leffler 		       + 2 + 26
18018a1b9b6aSSam Leffler 		       + sizeof(struct ieee80211_wme_info)
1802b032f27cSSam Leffler 		       + sizeof(struct ieee80211_ie_htcap)
1803b032f27cSSam Leffler 		       + 4 + sizeof(struct ieee80211_ie_htcap)
1804616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
180568e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ath_ie)
1806616190d0SSam Leffler #endif
1807b032f27cSSam Leffler 		       + (vap->iv_appie_wpa != NULL ?
1808b032f27cSSam Leffler 				vap->iv_appie_wpa->ie_len : 0)
1809b032f27cSSam Leffler 		       + (vap->iv_appie_assocreq != NULL ?
1810b032f27cSSam Leffler 				vap->iv_appie_assocreq->ie_len : 0)
18118a1b9b6aSSam Leffler 		);
18121a1e1d21SSam Leffler 		if (m == NULL)
18138a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
18141a1e1d21SSam Leffler 
1815b032f27cSSam Leffler 		KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1816b032f27cSSam Leffler 		    ("wrong mode %u", vap->iv_opmode));
1817667dad55SSam Leffler 		capinfo = IEEE80211_CAPINFO_ESS;
1818b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_PRIVACY)
18191a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
18204f2e09c4SSam Leffler 		/*
18214f2e09c4SSam Leffler 		 * NB: Some 11a AP's reject the request when
18224f2e09c4SSam Leffler 		 *     short premable is set.
18234f2e09c4SSam Leffler 		 */
18244f2e09c4SSam Leffler 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1825b5c99415SSam Leffler 		    IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
18261a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
182768e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
18288a1b9b6aSSam Leffler 		    (ic->ic_caps & IEEE80211_C_SHSLOT))
18291a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
18301b6167d2SSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
1831b032f27cSSam Leffler 		    (vap->iv_flags & IEEE80211_F_DOTH))
18321b6167d2SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
183368e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(capinfo);
18341a1e1d21SSam Leffler 		frm += 2;
18351a1e1d21SSam Leffler 
1836b032f27cSSam Leffler 		KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
183768e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
1838b032f27cSSam Leffler 						    bss->ni_intval));
18391a1e1d21SSam Leffler 		frm += 2;
18401a1e1d21SSam Leffler 
18411a1e1d21SSam Leffler 		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1842b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
18431a1e1d21SSam Leffler 			frm += IEEE80211_ADDR_LEN;
18441a1e1d21SSam Leffler 		}
18451a1e1d21SSam Leffler 
18461a1e1d21SSam Leffler 		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
18471a1e1d21SSam Leffler 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
1848b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_WPA2) {
1849b032f27cSSam Leffler 			if (vap->iv_rsn_ie != NULL)
1850b032f27cSSam Leffler 				frm = add_ie(frm, vap->iv_rsn_ie);
1851b032f27cSSam Leffler 			/* XXX else complain? */
18528a1b9b6aSSam Leffler 		}
1853b032f27cSSam Leffler 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1854b032f27cSSam Leffler 		if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
1855b032f27cSSam Leffler 			frm = ieee80211_add_powercapability(frm,
1856b032f27cSSam Leffler 			    ic->ic_curchan);
1857b032f27cSSam Leffler 			frm = ieee80211_add_supportedchannels(frm, ic);
1858b032f27cSSam Leffler 		}
1859b032f27cSSam Leffler 		if ((vap->iv_flags_ext & IEEE80211_FEXT_HT) &&
1860b032f27cSSam Leffler 		    ni->ni_ies.htcap_ie != NULL &&
1861b032f27cSSam Leffler 		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP)
1862b032f27cSSam Leffler 			frm = ieee80211_add_htcap(frm, ni);
1863b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_WPA1) {
1864b032f27cSSam Leffler 			if (vap->iv_wpa_ie != NULL)
1865b032f27cSSam Leffler 				frm = add_ie(frm, vap->iv_wpa_ie);
1866b032f27cSSam Leffler 			/* XXX else complain */
1867b032f27cSSam Leffler 		}
1868b032f27cSSam Leffler 		if ((ic->ic_flags & IEEE80211_F_WME) &&
1869b032f27cSSam Leffler 		    ni->ni_ies.wme_ie != NULL)
1870b032f27cSSam Leffler 			frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
1871b032f27cSSam Leffler 		if ((vap->iv_flags_ext & IEEE80211_FEXT_HT) &&
1872b032f27cSSam Leffler 		    ni->ni_ies.htcap_ie != NULL &&
1873b032f27cSSam Leffler 		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR)
1874b032f27cSSam Leffler 			frm = ieee80211_add_htcap_vendor(frm, ni);
1875616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
18764207227cSSam Leffler 		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
1877b032f27cSSam Leffler 			frm = ieee80211_add_ath(frm,
1878b032f27cSSam Leffler 				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
18794207227cSSam Leffler 				((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
18804207227cSSam Leffler 				 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
18814207227cSSam Leffler 				vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
18824207227cSSam Leffler 		}
1883616190d0SSam Leffler #endif /* IEEE80211_SUPPORT_SUPERG */
1884b032f27cSSam Leffler 		if (vap->iv_appie_assocreq != NULL)
1885b032f27cSSam Leffler 			frm = add_appie(frm, vap->iv_appie_assocreq);
188668e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
18871a1e1d21SSam Leffler 
188868e8e04eSSam Leffler 		ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
1889b032f27cSSam Leffler 			(void *) vap->iv_state);
18901a1e1d21SSam Leffler 		break;
18911a1e1d21SSam Leffler 
18921a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
18931a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
18941a1e1d21SSam Leffler 		/*
189568e8e04eSSam Leffler 		 * asresp frame format
18961a1e1d21SSam Leffler 		 *	[2] capability information
18971a1e1d21SSam Leffler 		 *	[2] status
18981a1e1d21SSam Leffler 		 *	[2] association ID
18991a1e1d21SSam Leffler 		 *	[tlv] supported rates
19001a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
1901b032f27cSSam Leffler 		 *	[tlv] HT capabilities (standard, if STA enabled)
1902b032f27cSSam Leffler 		 *	[tlv] HT information (standard, if STA enabled)
1903b032f27cSSam Leffler 		 *	[tlv] WME (if configured and STA enabled)
1904b032f27cSSam Leffler 		 *	[tlv] HT capabilities (vendor OUI, if STA enabled)
1905b032f27cSSam Leffler 		 *	[tlv] HT information (vendor OUI, if STA enabled)
1906b032f27cSSam Leffler 		 *	[tlv] Atheros capabilities (if STA enabled)
1907b032f27cSSam Leffler 		 *	[tlv] AppIE's (optional)
19081a1e1d21SSam Leffler 		 */
19098a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
191068e8e04eSSam Leffler 			 ic->ic_headroom + sizeof(struct ieee80211_frame),
191168e8e04eSSam Leffler 			 sizeof(uint16_t)
191268e8e04eSSam Leffler 		       + sizeof(uint16_t)
191368e8e04eSSam Leffler 		       + sizeof(uint16_t)
19141a1e1d21SSam Leffler 		       + 2 + IEEE80211_RATE_SIZE
19158a1b9b6aSSam Leffler 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
191668e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ie_htcap) + 4
191768e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ie_htinfo) + 4
1918b032f27cSSam Leffler 		       + sizeof(struct ieee80211_wme_param)
1919616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
192068e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ath_ie)
1921616190d0SSam Leffler #endif
1922b032f27cSSam Leffler 		       + (vap->iv_appie_assocresp != NULL ?
1923b032f27cSSam Leffler 				vap->iv_appie_assocresp->ie_len : 0)
19248a1b9b6aSSam Leffler 		);
19251a1e1d21SSam Leffler 		if (m == NULL)
19268a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
19271a1e1d21SSam Leffler 
1928b032f27cSSam Leffler 		capinfo = getcapinfo(vap, bss->ni_chan);
192968e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(capinfo);
19301a1e1d21SSam Leffler 		frm += 2;
19311a1e1d21SSam Leffler 
193268e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(arg);	/* status */
19331a1e1d21SSam Leffler 		frm += 2;
19341a1e1d21SSam Leffler 
19358a1b9b6aSSam Leffler 		if (arg == IEEE80211_STATUS_SUCCESS) {
193668e8e04eSSam Leffler 			*(uint16_t *)frm = htole16(ni->ni_associd);
19378a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_assoc);
19388a1b9b6aSSam Leffler 		} else
19398a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_assoc_fail);
19401a1e1d21SSam Leffler 		frm += 2;
19411a1e1d21SSam Leffler 
19421a1e1d21SSam Leffler 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
19431a1e1d21SSam Leffler 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
194468e8e04eSSam Leffler 		/* NB: respond according to what we received */
19451b6167d2SSam Leffler 		if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
194668e8e04eSSam Leffler 			frm = ieee80211_add_htcap(frm, ni);
194768e8e04eSSam Leffler 			frm = ieee80211_add_htinfo(frm, ni);
194868e8e04eSSam Leffler 		}
1949b032f27cSSam Leffler 		if ((vap->iv_flags & IEEE80211_F_WME) &&
1950b032f27cSSam Leffler 		    ni->ni_ies.wme_ie != NULL)
19511b6167d2SSam Leffler 			frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
19521b6167d2SSam Leffler 		if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
19531b6167d2SSam Leffler 			frm = ieee80211_add_htcap_vendor(frm, ni);
19541b6167d2SSam Leffler 			frm = ieee80211_add_htinfo_vendor(frm, ni);
195568e8e04eSSam Leffler 		}
1956616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1957b032f27cSSam Leffler 		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
195868e8e04eSSam Leffler 			frm = ieee80211_add_ath(frm,
1959b032f27cSSam Leffler 				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
19604207227cSSam Leffler 				((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
19614207227cSSam Leffler 				 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
19624207227cSSam Leffler 				vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
1963616190d0SSam Leffler #endif /* IEEE80211_SUPPORT_SUPERG */
1964b032f27cSSam Leffler 		if (vap->iv_appie_assocresp != NULL)
1965b032f27cSSam Leffler 			frm = add_appie(frm, vap->iv_appie_assocresp);
196668e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
19671a1e1d21SSam Leffler 		break;
19681a1e1d21SSam Leffler 
19691a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
1970b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1971b032f27cSSam Leffler 		    "send station disassociate (reason %d)", arg);
197268e8e04eSSam Leffler 		m = ieee80211_getmgtframe(&frm,
197368e8e04eSSam Leffler 			ic->ic_headroom + sizeof(struct ieee80211_frame),
197468e8e04eSSam Leffler 			sizeof(uint16_t));
19751a1e1d21SSam Leffler 		if (m == NULL)
19768a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
197768e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(arg);	/* reason */
197868e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
19798a1b9b6aSSam Leffler 
19808a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, tx_disassoc);
19818a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
19821a1e1d21SSam Leffler 		break;
19831a1e1d21SSam Leffler 
19841a1e1d21SSam Leffler 	default:
1985b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
1986b032f27cSSam Leffler 		    "invalid mgmt frame type %u", type);
19871be50176SSam Leffler 		senderr(EINVAL, is_tx_unknownmgt);
19880a915fadSSam Leffler 		/* NOTREACHED */
19891a1e1d21SSam Leffler 	}
199068e8e04eSSam Leffler 
19918ac160cdSSam Leffler 	/* NB: force non-ProbeResp frames to the highest queue */
19928ac160cdSSam Leffler 	params.ibp_pri = WME_AC_VO;
19938ac160cdSSam Leffler 	params.ibp_rate0 = bss->ni_txparms->mgmtrate;
19948ac160cdSSam Leffler 	/* NB: we know all frames are unicast */
19958ac160cdSSam Leffler 	params.ibp_try0 = bss->ni_txparms->maxretry;
19968ac160cdSSam Leffler 	params.ibp_power = bss->ni_txpower;
19978ac160cdSSam Leffler 	return ieee80211_mgmt_output(ni, m, type, &params);
19980a915fadSSam Leffler bad:
19998a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
20001a1e1d21SSam Leffler 	return ret;
20010a915fadSSam Leffler #undef senderr
20021b6167d2SSam Leffler #undef HTFLAGS
20031a1e1d21SSam Leffler }
20048a1b9b6aSSam Leffler 
2005b032f27cSSam Leffler /*
2006b032f27cSSam Leffler  * Return an mbuf with a probe response frame in it.
2007b032f27cSSam Leffler  * Space is left to prepend and 802.11 header at the
2008b032f27cSSam Leffler  * front but it's left to the caller to fill in.
2009b032f27cSSam Leffler  */
2010b032f27cSSam Leffler struct mbuf *
2011b032f27cSSam Leffler ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
2012b032f27cSSam Leffler {
2013b032f27cSSam Leffler 	struct ieee80211vap *vap = bss->ni_vap;
2014b032f27cSSam Leffler 	struct ieee80211com *ic = bss->ni_ic;
2015b032f27cSSam Leffler 	const struct ieee80211_rateset *rs;
2016b032f27cSSam Leffler 	struct mbuf *m;
2017b032f27cSSam Leffler 	uint16_t capinfo;
2018b032f27cSSam Leffler 	uint8_t *frm;
2019b032f27cSSam Leffler 
2020b032f27cSSam Leffler 	/*
2021b032f27cSSam Leffler 	 * probe response frame format
2022b032f27cSSam Leffler 	 *	[8] time stamp
2023b032f27cSSam Leffler 	 *	[2] beacon interval
2024b032f27cSSam Leffler 	 *	[2] cabability information
2025b032f27cSSam Leffler 	 *	[tlv] ssid
2026b032f27cSSam Leffler 	 *	[tlv] supported rates
2027b032f27cSSam Leffler 	 *	[tlv] parameter set (FH/DS)
2028b032f27cSSam Leffler 	 *	[tlv] parameter set (IBSS)
2029b032f27cSSam Leffler 	 *	[tlv] country (optional)
2030b032f27cSSam Leffler 	 *	[3] power control (optional)
2031b032f27cSSam Leffler 	 *	[5] channel switch announcement (CSA) (optional)
2032b032f27cSSam Leffler 	 *	[tlv] extended rate phy (ERP)
2033b032f27cSSam Leffler 	 *	[tlv] extended supported rates
2034688fe74dSSam Leffler 	 *	[tlv] RSN (optional)
2035b032f27cSSam Leffler 	 *	[tlv] HT capabilities
2036b032f27cSSam Leffler 	 *	[tlv] HT information
2037b032f27cSSam Leffler 	 *	[tlv] WPA (optional)
2038b032f27cSSam Leffler 	 *	[tlv] WME (optional)
2039b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT capabilities (optional)
2040b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT information (optional)
2041b032f27cSSam Leffler 	 *	[tlv] Atheros capabilities
2042b032f27cSSam Leffler 	 *	[tlv] AppIE's (optional)
2043b032f27cSSam Leffler 	 */
2044b032f27cSSam Leffler 	m = ieee80211_getmgtframe(&frm,
2045b032f27cSSam Leffler 		 ic->ic_headroom + sizeof(struct ieee80211_frame),
2046b032f27cSSam Leffler 		 8
2047b032f27cSSam Leffler 	       + sizeof(uint16_t)
2048b032f27cSSam Leffler 	       + sizeof(uint16_t)
2049b032f27cSSam Leffler 	       + 2 + IEEE80211_NWID_LEN
2050b032f27cSSam Leffler 	       + 2 + IEEE80211_RATE_SIZE
2051b032f27cSSam Leffler 	       + 7	/* max(7,3) */
2052b032f27cSSam Leffler 	       + IEEE80211_COUNTRY_MAX_SIZE
2053b032f27cSSam Leffler 	       + 3
2054b032f27cSSam Leffler 	       + sizeof(struct ieee80211_csa_ie)
2055b032f27cSSam Leffler 	       + 3
2056b032f27cSSam Leffler 	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2057688fe74dSSam Leffler 	       + sizeof(struct ieee80211_ie_wpa)
2058b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_htcap)
2059b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_htinfo)
2060b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_wpa)
2061b032f27cSSam Leffler 	       + sizeof(struct ieee80211_wme_param)
2062b032f27cSSam Leffler 	       + 4 + sizeof(struct ieee80211_ie_htcap)
2063b032f27cSSam Leffler 	       + 4 + sizeof(struct ieee80211_ie_htinfo)
2064616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
2065b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ath_ie)
2066616190d0SSam Leffler #endif
2067b032f27cSSam Leffler 	       + (vap->iv_appie_proberesp != NULL ?
2068b032f27cSSam Leffler 			vap->iv_appie_proberesp->ie_len : 0)
2069b032f27cSSam Leffler 	);
2070b032f27cSSam Leffler 	if (m == NULL) {
2071b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
2072b032f27cSSam Leffler 		return NULL;
2073b032f27cSSam Leffler 	}
2074b032f27cSSam Leffler 
2075b032f27cSSam Leffler 	memset(frm, 0, 8);	/* timestamp should be filled later */
2076b032f27cSSam Leffler 	frm += 8;
2077b032f27cSSam Leffler 	*(uint16_t *)frm = htole16(bss->ni_intval);
2078b032f27cSSam Leffler 	frm += 2;
2079b032f27cSSam Leffler 	capinfo = getcapinfo(vap, bss->ni_chan);
2080b032f27cSSam Leffler 	*(uint16_t *)frm = htole16(capinfo);
2081b032f27cSSam Leffler 	frm += 2;
2082b032f27cSSam Leffler 
2083b032f27cSSam Leffler 	frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
2084b032f27cSSam Leffler 	rs = ieee80211_get_suprates(ic, bss->ni_chan);
2085b032f27cSSam Leffler 	frm = ieee80211_add_rates(frm, rs);
2086b032f27cSSam Leffler 
2087b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
2088b032f27cSSam Leffler 		*frm++ = IEEE80211_ELEMID_FHPARMS;
2089b032f27cSSam Leffler 		*frm++ = 5;
2090b032f27cSSam Leffler 		*frm++ = bss->ni_fhdwell & 0x00ff;
2091b032f27cSSam Leffler 		*frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
2092b032f27cSSam Leffler 		*frm++ = IEEE80211_FH_CHANSET(
2093b032f27cSSam Leffler 		    ieee80211_chan2ieee(ic, bss->ni_chan));
2094b032f27cSSam Leffler 		*frm++ = IEEE80211_FH_CHANPAT(
2095b032f27cSSam Leffler 		    ieee80211_chan2ieee(ic, bss->ni_chan));
2096b032f27cSSam Leffler 		*frm++ = bss->ni_fhindex;
2097b032f27cSSam Leffler 	} else {
2098b032f27cSSam Leffler 		*frm++ = IEEE80211_ELEMID_DSPARMS;
2099b032f27cSSam Leffler 		*frm++ = 1;
2100b032f27cSSam Leffler 		*frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
2101b032f27cSSam Leffler 	}
2102b032f27cSSam Leffler 
2103b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
2104b032f27cSSam Leffler 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
2105b032f27cSSam Leffler 		*frm++ = 2;
2106b032f27cSSam Leffler 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
2107b032f27cSSam Leffler 	}
2108b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2109b032f27cSSam Leffler 	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2110b032f27cSSam Leffler 		frm = ieee80211_add_countryie(frm, ic);
2111b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_DOTH) {
2112b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
2113b032f27cSSam Leffler 			frm = ieee80211_add_powerconstraint(frm, vap);
2114b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2115b032f27cSSam Leffler 			frm = ieee80211_add_csa(frm, vap);
2116b032f27cSSam Leffler 	}
2117b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
2118b032f27cSSam Leffler 		frm = ieee80211_add_erp(frm, ic);
2119b032f27cSSam Leffler 	frm = ieee80211_add_xrates(frm, rs);
2120688fe74dSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA2) {
2121688fe74dSSam Leffler 		if (vap->iv_rsn_ie != NULL)
2122688fe74dSSam Leffler 			frm = add_ie(frm, vap->iv_rsn_ie);
2123688fe74dSSam Leffler 		/* XXX else complain? */
2124688fe74dSSam Leffler 	}
2125b032f27cSSam Leffler 	/*
2126b032f27cSSam Leffler 	 * NB: legacy 11b clients do not get certain ie's.
2127b032f27cSSam Leffler 	 *     The caller identifies such clients by passing
2128b032f27cSSam Leffler 	 *     a token in legacy to us.  Could expand this to be
2129b032f27cSSam Leffler 	 *     any legacy client for stuff like HT ie's.
2130b032f27cSSam Leffler 	 */
2131b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2132b032f27cSSam Leffler 	    legacy != IEEE80211_SEND_LEGACY_11B) {
2133b032f27cSSam Leffler 		frm = ieee80211_add_htcap(frm, bss);
2134b032f27cSSam Leffler 		frm = ieee80211_add_htinfo(frm, bss);
2135b032f27cSSam Leffler 	}
2136b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA1) {
2137b032f27cSSam Leffler 		if (vap->iv_wpa_ie != NULL)
2138b032f27cSSam Leffler 			frm = add_ie(frm, vap->iv_wpa_ie);
2139b032f27cSSam Leffler 		/* XXX else complain? */
2140b032f27cSSam Leffler 	}
2141b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WME)
2142b032f27cSSam Leffler 		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2143b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2144b032f27cSSam Leffler 	    (vap->iv_flags_ext & IEEE80211_FEXT_HTCOMPAT) &&
2145b032f27cSSam Leffler 	    legacy != IEEE80211_SEND_LEGACY_11B) {
2146b032f27cSSam Leffler 		frm = ieee80211_add_htcap_vendor(frm, bss);
2147b032f27cSSam Leffler 		frm = ieee80211_add_htinfo_vendor(frm, bss);
2148b032f27cSSam Leffler 	}
2149616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
21504207227cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
21514207227cSSam Leffler 	    legacy != IEEE80211_SEND_LEGACY_11B)
21524207227cSSam Leffler 		frm = ieee80211_add_athcaps(frm, bss);
2153616190d0SSam Leffler #endif
2154b032f27cSSam Leffler 	if (vap->iv_appie_proberesp != NULL)
2155b032f27cSSam Leffler 		frm = add_appie(frm, vap->iv_appie_proberesp);
2156b032f27cSSam Leffler 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2157b032f27cSSam Leffler 
2158b032f27cSSam Leffler 	return m;
2159b032f27cSSam Leffler }
2160b032f27cSSam Leffler 
2161b032f27cSSam Leffler /*
2162b032f27cSSam Leffler  * Send a probe response frame to the specified mac address.
2163b032f27cSSam Leffler  * This does not go through the normal mgt frame api so we
2164b032f27cSSam Leffler  * can specify the destination address and re-use the bss node
2165b032f27cSSam Leffler  * for the sta reference.
2166b032f27cSSam Leffler  */
2167b032f27cSSam Leffler int
2168b032f27cSSam Leffler ieee80211_send_proberesp(struct ieee80211vap *vap,
2169b032f27cSSam Leffler 	const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
2170b032f27cSSam Leffler {
2171b032f27cSSam Leffler 	struct ieee80211_node *bss = vap->iv_bss;
2172b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2173b032f27cSSam Leffler 	struct ieee80211_frame *wh;
2174b032f27cSSam Leffler 	struct mbuf *m;
2175b032f27cSSam Leffler 
2176b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
2177b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
2178b032f27cSSam Leffler 		    "block %s frame in CAC state", "probe response");
2179b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
2180b032f27cSSam Leffler 		return EIO;		/* XXX */
2181b032f27cSSam Leffler 	}
2182b032f27cSSam Leffler 
2183b032f27cSSam Leffler 	/*
2184b032f27cSSam Leffler 	 * Hold a reference on the node so it doesn't go away until after
2185b032f27cSSam Leffler 	 * the xmit is complete all the way in the driver.  On error we
2186b032f27cSSam Leffler 	 * will remove our reference.
2187b032f27cSSam Leffler 	 */
2188b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2189b032f27cSSam Leffler 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2190b032f27cSSam Leffler 	    __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
2191b032f27cSSam Leffler 	    ieee80211_node_refcnt(bss)+1);
2192b032f27cSSam Leffler 	ieee80211_ref_node(bss);
2193b032f27cSSam Leffler 
2194b032f27cSSam Leffler 	m = ieee80211_alloc_proberesp(bss, legacy);
2195b032f27cSSam Leffler 	if (m == NULL) {
2196b032f27cSSam Leffler 		ieee80211_free_node(bss);
2197b032f27cSSam Leffler 		return ENOMEM;
2198b032f27cSSam Leffler 	}
2199b032f27cSSam Leffler 
2200b032f27cSSam Leffler 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
2201b032f27cSSam Leffler 	KASSERT(m != NULL, ("no room for header"));
2202b032f27cSSam Leffler 
2203b032f27cSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
22049e80b1dfSSam Leffler 	ieee80211_send_setup(bss, m,
2205b032f27cSSam Leffler 	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
22068ac160cdSSam Leffler 	     IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
2207b032f27cSSam Leffler 	/* XXX power management? */
2208c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
2209b032f27cSSam Leffler 
2210b032f27cSSam Leffler 	M_WME_SETAC(m, WME_AC_BE);
2211b032f27cSSam Leffler 
2212b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2213b032f27cSSam Leffler 	    "send probe resp on channel %u to %s%s\n",
2214b032f27cSSam Leffler 	    ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
2215b032f27cSSam Leffler 	    legacy ? " <legacy>" : "");
2216b032f27cSSam Leffler 	IEEE80211_NODE_STAT(bss, tx_mgmt);
2217b032f27cSSam Leffler 
2218b032f27cSSam Leffler 	return ic->ic_raw_xmit(bss, m, NULL);
2219b032f27cSSam Leffler }
2220b032f27cSSam Leffler 
2221b032f27cSSam Leffler /*
2222b032f27cSSam Leffler  * Allocate and build a RTS (Request To Send) control frame.
2223b032f27cSSam Leffler  */
2224b032f27cSSam Leffler struct mbuf *
2225b032f27cSSam Leffler ieee80211_alloc_rts(struct ieee80211com *ic,
2226b032f27cSSam Leffler 	const uint8_t ra[IEEE80211_ADDR_LEN],
2227b032f27cSSam Leffler 	const uint8_t ta[IEEE80211_ADDR_LEN],
2228b032f27cSSam Leffler 	uint16_t dur)
2229b032f27cSSam Leffler {
2230b032f27cSSam Leffler 	struct ieee80211_frame_rts *rts;
2231b032f27cSSam Leffler 	struct mbuf *m;
2232b032f27cSSam Leffler 
2233b032f27cSSam Leffler 	/* XXX honor ic_headroom */
2234b032f27cSSam Leffler 	m = m_gethdr(M_DONTWAIT, MT_DATA);
2235b032f27cSSam Leffler 	if (m != NULL) {
2236b032f27cSSam Leffler 		rts = mtod(m, struct ieee80211_frame_rts *);
2237b032f27cSSam Leffler 		rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2238b032f27cSSam Leffler 			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
2239b032f27cSSam Leffler 		rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2240b032f27cSSam Leffler 		*(u_int16_t *)rts->i_dur = htole16(dur);
2241b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(rts->i_ra, ra);
2242b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(rts->i_ta, ta);
2243b032f27cSSam Leffler 
2244b032f27cSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
2245b032f27cSSam Leffler 	}
2246b032f27cSSam Leffler 	return m;
2247b032f27cSSam Leffler }
2248b032f27cSSam Leffler 
2249b032f27cSSam Leffler /*
2250b032f27cSSam Leffler  * Allocate and build a CTS (Clear To Send) control frame.
2251b032f27cSSam Leffler  */
2252b032f27cSSam Leffler struct mbuf *
2253b032f27cSSam Leffler ieee80211_alloc_cts(struct ieee80211com *ic,
2254b032f27cSSam Leffler 	const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
2255b032f27cSSam Leffler {
2256b032f27cSSam Leffler 	struct ieee80211_frame_cts *cts;
2257b032f27cSSam Leffler 	struct mbuf *m;
2258b032f27cSSam Leffler 
2259b032f27cSSam Leffler 	/* XXX honor ic_headroom */
2260b032f27cSSam Leffler 	m = m_gethdr(M_DONTWAIT, MT_DATA);
2261b032f27cSSam Leffler 	if (m != NULL) {
2262b032f27cSSam Leffler 		cts = mtod(m, struct ieee80211_frame_cts *);
2263b032f27cSSam Leffler 		cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2264b032f27cSSam Leffler 			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
2265b032f27cSSam Leffler 		cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2266b032f27cSSam Leffler 		*(u_int16_t *)cts->i_dur = htole16(dur);
2267b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(cts->i_ra, ra);
2268b032f27cSSam Leffler 
2269b032f27cSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
2270b032f27cSSam Leffler 	}
2271b032f27cSSam Leffler 	return m;
2272b032f27cSSam Leffler }
2273b032f27cSSam Leffler 
227468e8e04eSSam Leffler static void
227568e8e04eSSam Leffler ieee80211_tx_mgt_timeout(void *arg)
227668e8e04eSSam Leffler {
227768e8e04eSSam Leffler 	struct ieee80211_node *ni = arg;
2278b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
227968e8e04eSSam Leffler 
2280b032f27cSSam Leffler 	if (vap->iv_state != IEEE80211_S_INIT &&
2281b032f27cSSam Leffler 	    (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
228268e8e04eSSam Leffler 		/*
228368e8e04eSSam Leffler 		 * NB: it's safe to specify a timeout as the reason here;
228468e8e04eSSam Leffler 		 *     it'll only be used in the right state.
228568e8e04eSSam Leffler 		 */
2286b032f27cSSam Leffler 		ieee80211_new_state(vap, IEEE80211_S_SCAN,
228768e8e04eSSam Leffler 			IEEE80211_SCAN_FAIL_TIMEOUT);
228868e8e04eSSam Leffler 	}
228968e8e04eSSam Leffler }
229068e8e04eSSam Leffler 
229168e8e04eSSam Leffler static void
229268e8e04eSSam Leffler ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
229368e8e04eSSam Leffler {
2294b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
229568e8e04eSSam Leffler 	enum ieee80211_state ostate = (enum ieee80211_state) arg;
229668e8e04eSSam Leffler 
229768e8e04eSSam Leffler 	/*
229868e8e04eSSam Leffler 	 * Frame transmit completed; arrange timer callback.  If
229968e8e04eSSam Leffler 	 * transmit was successfuly we wait for response.  Otherwise
230068e8e04eSSam Leffler 	 * we arrange an immediate callback instead of doing the
230168e8e04eSSam Leffler 	 * callback directly since we don't know what state the driver
230268e8e04eSSam Leffler 	 * is in (e.g. what locks it is holding).  This work should
230368e8e04eSSam Leffler 	 * not be too time-critical and not happen too often so the
230468e8e04eSSam Leffler 	 * added overhead is acceptable.
230568e8e04eSSam Leffler 	 *
230668e8e04eSSam Leffler 	 * XXX what happens if !acked but response shows up before callback?
230768e8e04eSSam Leffler 	 */
2308b032f27cSSam Leffler 	if (vap->iv_state == ostate)
2309b032f27cSSam Leffler 		callout_reset(&vap->iv_mgtsend,
231068e8e04eSSam Leffler 			status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
231168e8e04eSSam Leffler 			ieee80211_tx_mgt_timeout, ni);
231268e8e04eSSam Leffler }
231368e8e04eSSam Leffler 
2314b032f27cSSam Leffler static void
2315b032f27cSSam Leffler ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
2316b032f27cSSam Leffler 	struct ieee80211_beacon_offsets *bo, struct ieee80211_node *ni)
23178a1b9b6aSSam Leffler {
2318b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2319b105a069SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2320b032f27cSSam Leffler 	struct ieee80211_rateset *rs = &ni->ni_rates;
232168e8e04eSSam Leffler 	uint16_t capinfo;
23228a1b9b6aSSam Leffler 
23238a1b9b6aSSam Leffler 	/*
23248a1b9b6aSSam Leffler 	 * beacon frame format
23258a1b9b6aSSam Leffler 	 *	[8] time stamp
23268a1b9b6aSSam Leffler 	 *	[2] beacon interval
23278a1b9b6aSSam Leffler 	 *	[2] cabability information
23288a1b9b6aSSam Leffler 	 *	[tlv] ssid
23298a1b9b6aSSam Leffler 	 *	[tlv] supported rates
23308a1b9b6aSSam Leffler 	 *	[3] parameter set (DS)
2331b032f27cSSam Leffler 	 *	[8] CF parameter set (optional)
23328a1b9b6aSSam Leffler 	 *	[tlv] parameter set (IBSS/TIM)
2333b032f27cSSam Leffler 	 *	[tlv] country (optional)
2334b032f27cSSam Leffler 	 *	[3] power control (optional)
2335b032f27cSSam Leffler 	 *	[5] channel switch announcement (CSA) (optional)
23368a1b9b6aSSam Leffler 	 *	[tlv] extended rate phy (ERP)
23378a1b9b6aSSam Leffler 	 *	[tlv] extended supported rates
2338688fe74dSSam Leffler 	 *	[tlv] RSN parameters
233968e8e04eSSam Leffler 	 *	[tlv] HT capabilities
234068e8e04eSSam Leffler 	 *	[tlv] HT information
2341b032f27cSSam Leffler 	 * XXX Vendor-specific OIDs (e.g. Atheros)
2342b032f27cSSam Leffler 	 *	[tlv] WPA parameters
2343b032f27cSSam Leffler 	 *	[tlv] WME parameters
234468e8e04eSSam Leffler 	 *	[tlv] Vendor OUI HT capabilities (optional)
234568e8e04eSSam Leffler 	 *	[tlv] Vendor OUI HT information (optional)
23464207227cSSam Leffler 	 *	[tlv] Atheros capabilities (optional)
234710ad9a77SSam Leffler 	 *	[tlv] TDMA parameters (optional)
2348b032f27cSSam Leffler 	 *	[tlv] application data (optional)
23498a1b9b6aSSam Leffler 	 */
23508a1b9b6aSSam Leffler 
23511b6167d2SSam Leffler 	memset(bo, 0, sizeof(*bo));
23521b6167d2SSam Leffler 
23538a1b9b6aSSam Leffler 	memset(frm, 0, 8);	/* XXX timestamp is set by hardware/driver */
23548a1b9b6aSSam Leffler 	frm += 8;
235568e8e04eSSam Leffler 	*(uint16_t *)frm = htole16(ni->ni_intval);
23568a1b9b6aSSam Leffler 	frm += 2;
2357b032f27cSSam Leffler 	capinfo = getcapinfo(vap, ni->ni_chan);
235868e8e04eSSam Leffler 	bo->bo_caps = (uint16_t *)frm;
235968e8e04eSSam Leffler 	*(uint16_t *)frm = htole16(capinfo);
23608a1b9b6aSSam Leffler 	frm += 2;
23618a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_SSID;
2362b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
23638a1b9b6aSSam Leffler 		*frm++ = ni->ni_esslen;
23648a1b9b6aSSam Leffler 		memcpy(frm, ni->ni_essid, ni->ni_esslen);
23658a1b9b6aSSam Leffler 		frm += ni->ni_esslen;
23668a1b9b6aSSam Leffler 	} else
23678a1b9b6aSSam Leffler 		*frm++ = 0;
23688a1b9b6aSSam Leffler 	frm = ieee80211_add_rates(frm, rs);
2369b032f27cSSam Leffler 	if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
23708a1b9b6aSSam Leffler 		*frm++ = IEEE80211_ELEMID_DSPARMS;
23718a1b9b6aSSam Leffler 		*frm++ = 1;
2372b032f27cSSam Leffler 		*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2373b032f27cSSam Leffler 	}
2374b032f27cSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PCF) {
2375b032f27cSSam Leffler 		bo->bo_cfp = frm;
2376b032f27cSSam Leffler 		frm = ieee80211_add_cfparms(frm, ic);
23778a1b9b6aSSam Leffler 	}
23788a1b9b6aSSam Leffler 	bo->bo_tim = frm;
2379b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
23808a1b9b6aSSam Leffler 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
23818a1b9b6aSSam Leffler 		*frm++ = 2;
23828a1b9b6aSSam Leffler 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
23838a1b9b6aSSam Leffler 		bo->bo_tim_len = 0;
2384b032f27cSSam Leffler 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
23858a1b9b6aSSam Leffler 		struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
23868a1b9b6aSSam Leffler 
23878a1b9b6aSSam Leffler 		tie->tim_ie = IEEE80211_ELEMID_TIM;
23888a1b9b6aSSam Leffler 		tie->tim_len = 4;	/* length */
23898a1b9b6aSSam Leffler 		tie->tim_count = 0;	/* DTIM count */
2390b032f27cSSam Leffler 		tie->tim_period = vap->iv_dtim_period;	/* DTIM period */
23918a1b9b6aSSam Leffler 		tie->tim_bitctl = 0;	/* bitmap control */
23928a1b9b6aSSam Leffler 		tie->tim_bitmap[0] = 0;	/* Partial Virtual Bitmap */
23938a1b9b6aSSam Leffler 		frm += sizeof(struct ieee80211_tim_ie);
23948a1b9b6aSSam Leffler 		bo->bo_tim_len = 1;
23958a1b9b6aSSam Leffler 	}
2396b105a069SSam Leffler 	bo->bo_tim_trailer = frm;
2397b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2398b032f27cSSam Leffler 	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2399b032f27cSSam Leffler 		frm = ieee80211_add_countryie(frm, ic);
2400b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_DOTH) {
2401b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
2402b032f27cSSam Leffler 			frm = ieee80211_add_powerconstraint(frm, vap);
2403b032f27cSSam Leffler 		bo->bo_csa = frm;
2404b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2405b032f27cSSam Leffler 			frm = ieee80211_add_csa(frm, vap);
2406b032f27cSSam Leffler 	} else
2407b032f27cSSam Leffler 		bo->bo_csa = frm;
2408b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
24090912bac9SSam Leffler 		bo->bo_erp = frm;
24108a1b9b6aSSam Leffler 		frm = ieee80211_add_erp(frm, ic);
24111b6167d2SSam Leffler 	}
241268e8e04eSSam Leffler 	frm = ieee80211_add_xrates(frm, rs);
2413688fe74dSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA2) {
2414688fe74dSSam Leffler 		if (vap->iv_rsn_ie != NULL)
2415688fe74dSSam Leffler 			frm = add_ie(frm, vap->iv_rsn_ie);
2416688fe74dSSam Leffler 		/* XXX else complain */
2417688fe74dSSam Leffler 	}
2418b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
241968e8e04eSSam Leffler 		frm = ieee80211_add_htcap(frm, ni);
242068e8e04eSSam Leffler 		bo->bo_htinfo = frm;
242168e8e04eSSam Leffler 		frm = ieee80211_add_htinfo(frm, ni);
24221b6167d2SSam Leffler 	}
2423b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA1) {
2424b032f27cSSam Leffler 		if (vap->iv_wpa_ie != NULL)
2425b032f27cSSam Leffler 			frm = add_ie(frm, vap->iv_wpa_ie);
2426b032f27cSSam Leffler 		/* XXX else complain */
2427b032f27cSSam Leffler 	}
2428b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WME) {
24291b6167d2SSam Leffler 		bo->bo_wme = frm;
24301b6167d2SSam Leffler 		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
24311b6167d2SSam Leffler 	}
2432b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2433b032f27cSSam Leffler 	    (vap->iv_flags_ext & IEEE80211_FEXT_HTCOMPAT)) {
243468e8e04eSSam Leffler 		frm = ieee80211_add_htcap_vendor(frm, ni);
243568e8e04eSSam Leffler 		frm = ieee80211_add_htinfo_vendor(frm, ni);
24360912bac9SSam Leffler 	}
24374207227cSSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
24384207227cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_ATHEROS) {
24394207227cSSam Leffler 		bo->bo_ath = frm;
24404207227cSSam Leffler 		frm = ieee80211_add_athcaps(frm, ni);
24414207227cSSam Leffler 	}
24424207227cSSam Leffler #endif
244310ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
244410ad9a77SSam Leffler 	if (vap->iv_caps & IEEE80211_C_TDMA) {
244510ad9a77SSam Leffler 		bo->bo_tdma = frm;
244610ad9a77SSam Leffler 		frm = ieee80211_add_tdma(frm, vap);
244710ad9a77SSam Leffler 	}
244810ad9a77SSam Leffler #endif
2449b032f27cSSam Leffler 	if (vap->iv_appie_beacon != NULL) {
2450b032f27cSSam Leffler 		bo->bo_appie = frm;
2451b032f27cSSam Leffler 		bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
2452b032f27cSSam Leffler 		frm = add_appie(frm, vap->iv_appie_beacon);
2453b032f27cSSam Leffler 	}
2454b105a069SSam Leffler 	bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
2455b032f27cSSam Leffler 	bo->bo_csa_trailer_len = frm - bo->bo_csa;
245668e8e04eSSam Leffler 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2457b032f27cSSam Leffler }
2458b032f27cSSam Leffler 
2459b032f27cSSam Leffler /*
2460b032f27cSSam Leffler  * Allocate a beacon frame and fillin the appropriate bits.
2461b032f27cSSam Leffler  */
2462b032f27cSSam Leffler struct mbuf *
2463b032f27cSSam Leffler ieee80211_beacon_alloc(struct ieee80211_node *ni,
2464b032f27cSSam Leffler 	struct ieee80211_beacon_offsets *bo)
2465b032f27cSSam Leffler {
2466b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2467b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2468b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
2469b032f27cSSam Leffler 	struct ieee80211_frame *wh;
2470b032f27cSSam Leffler 	struct mbuf *m;
2471b032f27cSSam Leffler 	int pktlen;
2472b032f27cSSam Leffler 	uint8_t *frm;
2473b032f27cSSam Leffler 
2474b032f27cSSam Leffler 	/*
2475b032f27cSSam Leffler 	 * beacon frame format
2476b032f27cSSam Leffler 	 *	[8] time stamp
2477b032f27cSSam Leffler 	 *	[2] beacon interval
2478b032f27cSSam Leffler 	 *	[2] cabability information
2479b032f27cSSam Leffler 	 *	[tlv] ssid
2480b032f27cSSam Leffler 	 *	[tlv] supported rates
2481b032f27cSSam Leffler 	 *	[3] parameter set (DS)
2482b032f27cSSam Leffler 	 *	[8] CF parameter set (optional)
2483b032f27cSSam Leffler 	 *	[tlv] parameter set (IBSS/TIM)
2484b032f27cSSam Leffler 	 *	[tlv] country (optional)
2485b032f27cSSam Leffler 	 *	[3] power control (optional)
2486b032f27cSSam Leffler 	 *	[5] channel switch announcement (CSA) (optional)
2487b032f27cSSam Leffler 	 *	[tlv] extended rate phy (ERP)
2488b032f27cSSam Leffler 	 *	[tlv] extended supported rates
2489b032f27cSSam Leffler 	 *	[tlv] RSN parameters
2490b032f27cSSam Leffler 	 *	[tlv] HT capabilities
2491b032f27cSSam Leffler 	 *	[tlv] HT information
2492b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT capabilities (optional)
2493b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT information (optional)
2494b032f27cSSam Leffler 	 * XXX Vendor-specific OIDs (e.g. Atheros)
2495b032f27cSSam Leffler 	 *	[tlv] WPA parameters
2496b032f27cSSam Leffler 	 *	[tlv] WME parameters
249710ad9a77SSam Leffler 	 *	[tlv] TDMA parameters (optional)
2498b032f27cSSam Leffler 	 *	[tlv] application data (optional)
2499b032f27cSSam Leffler 	 * NB: we allocate the max space required for the TIM bitmap.
2500b032f27cSSam Leffler 	 * XXX how big is this?
2501b032f27cSSam Leffler 	 */
2502b032f27cSSam Leffler 	pktlen =   8					/* time stamp */
2503b032f27cSSam Leffler 		 + sizeof(uint16_t)			/* beacon interval */
2504b032f27cSSam Leffler 		 + sizeof(uint16_t)			/* capabilities */
2505b032f27cSSam Leffler 		 + 2 + ni->ni_esslen			/* ssid */
2506b032f27cSSam Leffler 	         + 2 + IEEE80211_RATE_SIZE		/* supported rates */
2507b032f27cSSam Leffler 	         + 2 + 1				/* DS parameters */
2508b032f27cSSam Leffler 		 + 2 + 6				/* CF parameters */
2509b032f27cSSam Leffler 		 + 2 + 4 + vap->iv_tim_len		/* DTIM/IBSSPARMS */
2510b032f27cSSam Leffler 		 + IEEE80211_COUNTRY_MAX_SIZE		/* country */
2511b032f27cSSam Leffler 		 + 2 + 1				/* power control */
2512b032f27cSSam Leffler 	         + sizeof(struct ieee80211_csa_ie)	/* CSA */
2513b032f27cSSam Leffler 		 + 2 + 1				/* ERP */
2514b032f27cSSam Leffler 	         + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2515b032f27cSSam Leffler 		 + (vap->iv_caps & IEEE80211_C_WPA ?	/* WPA 1+2 */
2516b032f27cSSam Leffler 			2*sizeof(struct ieee80211_ie_wpa) : 0)
2517b032f27cSSam Leffler 		 /* XXX conditional? */
2518b032f27cSSam Leffler 		 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
2519b032f27cSSam Leffler 		 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
2520b032f27cSSam Leffler 		 + (vap->iv_caps & IEEE80211_C_WME ?	/* WME */
2521b032f27cSSam Leffler 			sizeof(struct ieee80211_wme_param) : 0)
25224207227cSSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
25234207227cSSam Leffler 		 + sizeof(struct ieee80211_ath_ie)	/* ATH */
25244207227cSSam Leffler #endif
252510ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
252610ad9a77SSam Leffler 		 + (vap->iv_caps & IEEE80211_C_TDMA ?	/* TDMA */
252710ad9a77SSam Leffler 			sizeof(struct ieee80211_tdma_param) : 0)
252810ad9a77SSam Leffler #endif
2529b032f27cSSam Leffler 		 + IEEE80211_MAX_APPIE
2530b032f27cSSam Leffler 		 ;
2531b032f27cSSam Leffler 	m = ieee80211_getmgtframe(&frm,
2532b032f27cSSam Leffler 		ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
2533b032f27cSSam Leffler 	if (m == NULL) {
2534b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
2535b032f27cSSam Leffler 			"%s: cannot get buf; size %u\n", __func__, pktlen);
2536b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
2537b032f27cSSam Leffler 		return NULL;
2538b032f27cSSam Leffler 	}
2539b032f27cSSam Leffler 	ieee80211_beacon_construct(m, frm, bo, ni);
25408a1b9b6aSSam Leffler 
25418a1b9b6aSSam Leffler 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
25428a1b9b6aSSam Leffler 	KASSERT(m != NULL, ("no space for 802.11 header?"));
25438a1b9b6aSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
25448a1b9b6aSSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
25458a1b9b6aSSam Leffler 	    IEEE80211_FC0_SUBTYPE_BEACON;
25468a1b9b6aSSam Leffler 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
254768e8e04eSSam Leffler 	*(uint16_t *)wh->i_dur = 0;
25488a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
2549b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
25508a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
255168e8e04eSSam Leffler 	*(uint16_t *)wh->i_seq = 0;
25528a1b9b6aSSam Leffler 
25538a1b9b6aSSam Leffler 	return m;
25548a1b9b6aSSam Leffler }
25558a1b9b6aSSam Leffler 
25568a1b9b6aSSam Leffler /*
25578a1b9b6aSSam Leffler  * Update the dynamic parts of a beacon frame based on the current state.
25588a1b9b6aSSam Leffler  */
25598a1b9b6aSSam Leffler int
2560b105a069SSam Leffler ieee80211_beacon_update(struct ieee80211_node *ni,
25618a1b9b6aSSam Leffler 	struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
25628a1b9b6aSSam Leffler {
2563b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2564b105a069SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
25658a1b9b6aSSam Leffler 	int len_changed = 0;
256668e8e04eSSam Leffler 	uint16_t capinfo;
25678a1b9b6aSSam Leffler 
2568b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2569b032f27cSSam Leffler 	/*
2570b032f27cSSam Leffler 	 * Handle 11h channel change when we've reached the count.
2571b032f27cSSam Leffler 	 * We must recalculate the beacon frame contents to account
2572b032f27cSSam Leffler 	 * for the new channel.  Note we do this only for the first
2573b032f27cSSam Leffler 	 * vap that reaches this point; subsequent vaps just update
2574b032f27cSSam Leffler 	 * their beacon state to reflect the recalculated channel.
2575b032f27cSSam Leffler 	 */
2576b032f27cSSam Leffler 	if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
2577b032f27cSSam Leffler 	    vap->iv_csa_count == ic->ic_csa_count) {
2578b032f27cSSam Leffler 		vap->iv_csa_count = 0;
2579b032f27cSSam Leffler 		/*
2580b032f27cSSam Leffler 		 * Effect channel change before reconstructing the beacon
2581b032f27cSSam Leffler 		 * frame contents as many places reference ni_chan.
2582b032f27cSSam Leffler 		 */
2583b032f27cSSam Leffler 		if (ic->ic_csa_newchan != NULL)
2584b032f27cSSam Leffler 			ieee80211_csa_completeswitch(ic);
2585b032f27cSSam Leffler 		/*
2586b032f27cSSam Leffler 		 * NB: ieee80211_beacon_construct clears all pending
2587b032f27cSSam Leffler 		 * updates in bo_flags so we don't need to explicitly
2588b032f27cSSam Leffler 		 * clear IEEE80211_BEACON_CSA.
2589b032f27cSSam Leffler 		 */
2590b032f27cSSam Leffler 		ieee80211_beacon_construct(m,
2591b032f27cSSam Leffler 		    mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), bo, ni);
2592b032f27cSSam Leffler 
2593b032f27cSSam Leffler 		/* XXX do WME aggressive mode processing? */
2594b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
2595b032f27cSSam Leffler 		return 1;		/* just assume length changed */
2596b032f27cSSam Leffler 	}
2597b032f27cSSam Leffler 
25988a1b9b6aSSam Leffler 	/* XXX faster to recalculate entirely or just changes? */
2599b032f27cSSam Leffler 	capinfo = getcapinfo(vap, ni->ni_chan);
26008a1b9b6aSSam Leffler 	*bo->bo_caps = htole16(capinfo);
26018a1b9b6aSSam Leffler 
2602b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WME) {
26038a1b9b6aSSam Leffler 		struct ieee80211_wme_state *wme = &ic->ic_wme;
26048a1b9b6aSSam Leffler 
26058a1b9b6aSSam Leffler 		/*
26068a1b9b6aSSam Leffler 		 * Check for agressive mode change.  When there is
26078a1b9b6aSSam Leffler 		 * significant high priority traffic in the BSS
26088a1b9b6aSSam Leffler 		 * throttle back BE traffic by using conservative
26098a1b9b6aSSam Leffler 		 * parameters.  Otherwise BE uses agressive params
26108a1b9b6aSSam Leffler 		 * to optimize performance of legacy/non-QoS traffic.
26118a1b9b6aSSam Leffler 		 */
26128a1b9b6aSSam Leffler 		if (wme->wme_flags & WME_F_AGGRMODE) {
26138a1b9b6aSSam Leffler 			if (wme->wme_hipri_traffic >
26148a1b9b6aSSam Leffler 			    wme->wme_hipri_switch_thresh) {
2615b032f27cSSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
26168a1b9b6aSSam Leffler 				    "%s: traffic %u, disable aggressive mode\n",
26178a1b9b6aSSam Leffler 				    __func__, wme->wme_hipri_traffic);
26188a1b9b6aSSam Leffler 				wme->wme_flags &= ~WME_F_AGGRMODE;
2619b032f27cSSam Leffler 				ieee80211_wme_updateparams_locked(vap);
26208a1b9b6aSSam Leffler 				wme->wme_hipri_traffic =
26218a1b9b6aSSam Leffler 					wme->wme_hipri_switch_hysteresis;
26228a1b9b6aSSam Leffler 			} else
26238a1b9b6aSSam Leffler 				wme->wme_hipri_traffic = 0;
26248a1b9b6aSSam Leffler 		} else {
26258a1b9b6aSSam Leffler 			if (wme->wme_hipri_traffic <=
26268a1b9b6aSSam Leffler 			    wme->wme_hipri_switch_thresh) {
2627b032f27cSSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
26288a1b9b6aSSam Leffler 				    "%s: traffic %u, enable aggressive mode\n",
26298a1b9b6aSSam Leffler 				    __func__, wme->wme_hipri_traffic);
26308a1b9b6aSSam Leffler 				wme->wme_flags |= WME_F_AGGRMODE;
2631b032f27cSSam Leffler 				ieee80211_wme_updateparams_locked(vap);
26328a1b9b6aSSam Leffler 				wme->wme_hipri_traffic = 0;
26338a1b9b6aSSam Leffler 			} else
26348a1b9b6aSSam Leffler 				wme->wme_hipri_traffic =
26358a1b9b6aSSam Leffler 					wme->wme_hipri_switch_hysteresis;
26368a1b9b6aSSam Leffler 		}
2637b105a069SSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
26388a1b9b6aSSam Leffler 			(void) ieee80211_add_wme_param(bo->bo_wme, wme);
2639b105a069SSam Leffler 			clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
26408a1b9b6aSSam Leffler 		}
26418a1b9b6aSSam Leffler 	}
26428a1b9b6aSSam Leffler 
2643b105a069SSam Leffler 	if (isset(bo->bo_flags,  IEEE80211_BEACON_HTINFO)) {
2644b032f27cSSam Leffler 		ieee80211_ht_update_beacon(vap, bo);
2645b105a069SSam Leffler 		clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
264668e8e04eSSam Leffler 	}
264710ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
264810ad9a77SSam Leffler 	if (vap->iv_caps & IEEE80211_C_TDMA) {
264910ad9a77SSam Leffler 		/*
265010ad9a77SSam Leffler 		 * NB: the beacon is potentially updated every TBTT.
265110ad9a77SSam Leffler 		 */
265210ad9a77SSam Leffler 		ieee80211_tdma_update_beacon(vap, bo);
265310ad9a77SSam Leffler 	}
265410ad9a77SSam Leffler #endif
2655b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {	/* NB: no IBSS support*/
26568a1b9b6aSSam Leffler 		struct ieee80211_tim_ie *tie =
26578a1b9b6aSSam Leffler 			(struct ieee80211_tim_ie *) bo->bo_tim;
2658b105a069SSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
26598a1b9b6aSSam Leffler 			u_int timlen, timoff, i;
26608a1b9b6aSSam Leffler 			/*
26618a1b9b6aSSam Leffler 			 * ATIM/DTIM needs updating.  If it fits in the
26628a1b9b6aSSam Leffler 			 * current space allocated then just copy in the
26638a1b9b6aSSam Leffler 			 * new bits.  Otherwise we need to move any trailing
26648a1b9b6aSSam Leffler 			 * data to make room.  Note that we know there is
26658a1b9b6aSSam Leffler 			 * contiguous space because ieee80211_beacon_allocate
26668a1b9b6aSSam Leffler 			 * insures there is space in the mbuf to write a
2667b032f27cSSam Leffler 			 * maximal-size virtual bitmap (based on iv_max_aid).
26688a1b9b6aSSam Leffler 			 */
26698a1b9b6aSSam Leffler 			/*
26708a1b9b6aSSam Leffler 			 * Calculate the bitmap size and offset, copy any
26718a1b9b6aSSam Leffler 			 * trailer out of the way, and then copy in the
26728a1b9b6aSSam Leffler 			 * new bitmap and update the information element.
26738a1b9b6aSSam Leffler 			 * Note that the tim bitmap must contain at least
26748a1b9b6aSSam Leffler 			 * one byte and any offset must be even.
26758a1b9b6aSSam Leffler 			 */
2676b032f27cSSam Leffler 			if (vap->iv_ps_pending != 0) {
26778a1b9b6aSSam Leffler 				timoff = 128;		/* impossibly large */
2678b032f27cSSam Leffler 				for (i = 0; i < vap->iv_tim_len; i++)
2679b032f27cSSam Leffler 					if (vap->iv_tim_bitmap[i]) {
26808a1b9b6aSSam Leffler 						timoff = i &~ 1;
26818a1b9b6aSSam Leffler 						break;
26828a1b9b6aSSam Leffler 					}
26838a1b9b6aSSam Leffler 				KASSERT(timoff != 128, ("tim bitmap empty!"));
2684b032f27cSSam Leffler 				for (i = vap->iv_tim_len-1; i >= timoff; i--)
2685b032f27cSSam Leffler 					if (vap->iv_tim_bitmap[i])
26868a1b9b6aSSam Leffler 						break;
26878a1b9b6aSSam Leffler 				timlen = 1 + (i - timoff);
26888a1b9b6aSSam Leffler 			} else {
26898a1b9b6aSSam Leffler 				timoff = 0;
26908a1b9b6aSSam Leffler 				timlen = 1;
26918a1b9b6aSSam Leffler 			}
26928a1b9b6aSSam Leffler 			if (timlen != bo->bo_tim_len) {
26938a1b9b6aSSam Leffler 				/* copy up/down trailer */
26940912bac9SSam Leffler 				int adjust = tie->tim_bitmap+timlen
2695b105a069SSam Leffler 					   - bo->bo_tim_trailer;
2696b105a069SSam Leffler 				ovbcopy(bo->bo_tim_trailer,
2697b105a069SSam Leffler 				    bo->bo_tim_trailer+adjust,
2698b105a069SSam Leffler 				    bo->bo_tim_trailer_len);
2699b105a069SSam Leffler 				bo->bo_tim_trailer += adjust;
27000912bac9SSam Leffler 				bo->bo_erp += adjust;
270168e8e04eSSam Leffler 				bo->bo_htinfo += adjust;
27024207227cSSam Leffler #ifdef IEEE80211_SUPERG_SUPPORT
27034207227cSSam Leffler 				bo->bo_ath += adjust;
27044207227cSSam Leffler #endif
270592e870edSSam Leffler #ifdef IEEE80211_TDMA_SUPPORT
270692e870edSSam Leffler 				bo->bo_tdma += adjust;
270792e870edSSam Leffler #endif
2708b032f27cSSam Leffler 				bo->bo_appie += adjust;
2709b032f27cSSam Leffler 				bo->bo_wme += adjust;
2710b032f27cSSam Leffler 				bo->bo_csa += adjust;
27118a1b9b6aSSam Leffler 				bo->bo_tim_len = timlen;
27128a1b9b6aSSam Leffler 
27138a1b9b6aSSam Leffler 				/* update information element */
27148a1b9b6aSSam Leffler 				tie->tim_len = 3 + timlen;
27158a1b9b6aSSam Leffler 				tie->tim_bitctl = timoff;
27168a1b9b6aSSam Leffler 				len_changed = 1;
27178a1b9b6aSSam Leffler 			}
2718b032f27cSSam Leffler 			memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
27198a1b9b6aSSam Leffler 				bo->bo_tim_len);
27208a1b9b6aSSam Leffler 
2721b105a069SSam Leffler 			clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
27228a1b9b6aSSam Leffler 
2723b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
27248a1b9b6aSSam Leffler 				"%s: TIM updated, pending %u, off %u, len %u\n",
2725b032f27cSSam Leffler 				__func__, vap->iv_ps_pending, timoff, timlen);
27268a1b9b6aSSam Leffler 		}
27278a1b9b6aSSam Leffler 		/* count down DTIM period */
27288a1b9b6aSSam Leffler 		if (tie->tim_count == 0)
27298a1b9b6aSSam Leffler 			tie->tim_count = tie->tim_period - 1;
27308a1b9b6aSSam Leffler 		else
27318a1b9b6aSSam Leffler 			tie->tim_count--;
27328a1b9b6aSSam Leffler 		/* update state for buffered multicast frames on DTIM */
2733a196b35fSSam Leffler 		if (mcast && tie->tim_count == 0)
27348a1b9b6aSSam Leffler 			tie->tim_bitctl |= 1;
27358a1b9b6aSSam Leffler 		else
27368a1b9b6aSSam Leffler 			tie->tim_bitctl &= ~1;
2737b032f27cSSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
2738b032f27cSSam Leffler 			struct ieee80211_csa_ie *csa =
2739b032f27cSSam Leffler 			    (struct ieee80211_csa_ie *) bo->bo_csa;
2740b032f27cSSam Leffler 
2741b032f27cSSam Leffler 			/*
2742b032f27cSSam Leffler 			 * Insert or update CSA ie.  If we're just starting
2743b032f27cSSam Leffler 			 * to count down to the channel switch then we need
2744b032f27cSSam Leffler 			 * to insert the CSA ie.  Otherwise we just need to
2745b032f27cSSam Leffler 			 * drop the count.  The actual change happens above
2746b032f27cSSam Leffler 			 * when the vap's count reaches the target count.
2747b032f27cSSam Leffler 			 */
2748b032f27cSSam Leffler 			if (vap->iv_csa_count == 0) {
2749b032f27cSSam Leffler 				memmove(&csa[1], csa, bo->bo_csa_trailer_len);
2750b032f27cSSam Leffler 				bo->bo_erp += sizeof(*csa);
2751d01b3c26SSam Leffler 				bo->bo_htinfo += sizeof(*csa);
2752b032f27cSSam Leffler 				bo->bo_wme += sizeof(*csa);
27534207227cSSam Leffler #ifdef IEEE80211_SUPERG_SUPPORT
27544207227cSSam Leffler 				bo->bo_ath += sizeof(*csa);
27554207227cSSam Leffler #endif
275692e870edSSam Leffler #ifdef IEEE80211_TDMA_SUPPORT
275792e870edSSam Leffler 				bo->bo_tdma += sizeof(*csa);
275892e870edSSam Leffler #endif
2759b032f27cSSam Leffler 				bo->bo_appie += sizeof(*csa);
2760b032f27cSSam Leffler 				bo->bo_csa_trailer_len += sizeof(*csa);
2761b032f27cSSam Leffler 				bo->bo_tim_trailer_len += sizeof(*csa);
2762b032f27cSSam Leffler 				m->m_len += sizeof(*csa);
2763b032f27cSSam Leffler 				m->m_pkthdr.len += sizeof(*csa);
2764b032f27cSSam Leffler 
2765b032f27cSSam Leffler 				ieee80211_add_csa(bo->bo_csa, vap);
2766b032f27cSSam Leffler 			} else
2767b032f27cSSam Leffler 				csa->csa_count--;
2768b032f27cSSam Leffler 			vap->iv_csa_count++;
2769b032f27cSSam Leffler 			/* NB: don't clear IEEE80211_BEACON_CSA */
2770b032f27cSSam Leffler 		}
2771b105a069SSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
27720912bac9SSam Leffler 			/*
27730912bac9SSam Leffler 			 * ERP element needs updating.
27740912bac9SSam Leffler 			 */
27750912bac9SSam Leffler 			(void) ieee80211_add_erp(bo->bo_erp, ic);
2776b105a069SSam Leffler 			clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
27770912bac9SSam Leffler 		}
27784207227cSSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
27794207227cSSam Leffler 		if (isset(bo->bo_flags,  IEEE80211_BEACON_ATH)) {
27804207227cSSam Leffler 			ieee80211_add_athcaps(bo->bo_ath, ni);
27814207227cSSam Leffler 			clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
27824207227cSSam Leffler 		}
27834207227cSSam Leffler #endif
27848a1b9b6aSSam Leffler 	}
2785b032f27cSSam Leffler 	if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
2786b032f27cSSam Leffler 		const struct ieee80211_appie *aie = vap->iv_appie_beacon;
2787b032f27cSSam Leffler 		int aielen;
2788b032f27cSSam Leffler 		uint8_t *frm;
2789b032f27cSSam Leffler 
2790b032f27cSSam Leffler 		aielen = 0;
2791b032f27cSSam Leffler 		if (aie != NULL)
2792b032f27cSSam Leffler 			aielen += aie->ie_len;
2793b032f27cSSam Leffler 		if (aielen != bo->bo_appie_len) {
2794b032f27cSSam Leffler 			/* copy up/down trailer */
2795b032f27cSSam Leffler 			int adjust = aielen - bo->bo_appie_len;
2796b032f27cSSam Leffler 			ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
2797b032f27cSSam Leffler 				bo->bo_tim_trailer_len);
2798b032f27cSSam Leffler 			bo->bo_tim_trailer += adjust;
2799b032f27cSSam Leffler 			bo->bo_appie += adjust;
2800b032f27cSSam Leffler 			bo->bo_appie_len = aielen;
2801b032f27cSSam Leffler 
2802b032f27cSSam Leffler 			len_changed = 1;
2803b032f27cSSam Leffler 		}
2804b032f27cSSam Leffler 		frm = bo->bo_appie;
2805b032f27cSSam Leffler 		if (aie != NULL)
2806b032f27cSSam Leffler 			frm  = add_appie(frm, aie);
2807b032f27cSSam Leffler 		clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
2808b032f27cSSam Leffler 	}
2809b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
28108a1b9b6aSSam Leffler 
28118a1b9b6aSSam Leffler 	return len_changed;
28128a1b9b6aSSam Leffler }
2813