xref: /freebsd/sys/net80211/ieee80211_output.c (revision 45f856e3ac5d0535fd1a285414f23c5038b27207)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
3b032f27cSSam Leffler  * Copyright (c) 2002-2008 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>
50b032f27cSSam Leffler #include <net80211/ieee80211_wds.h>
511a1e1d21SSam Leffler 
521a1e1d21SSam Leffler #ifdef INET
531a1e1d21SSam Leffler #include <netinet/in.h>
541a1e1d21SSam Leffler #include <netinet/if_ether.h>
558a1b9b6aSSam Leffler #include <netinet/in_systm.h>
568a1b9b6aSSam Leffler #include <netinet/ip.h>
578a1b9b6aSSam Leffler #endif
588a1b9b6aSSam Leffler 
5968e8e04eSSam Leffler #define	ETHER_HEADER_COPY(dst, src) \
6068e8e04eSSam Leffler 	memcpy(dst, src, sizeof(struct ether_header))
6168e8e04eSSam Leffler 
62b032f27cSSam Leffler static struct mbuf *ieee80211_encap_fastframe(struct ieee80211vap *,
6368e8e04eSSam Leffler 	struct mbuf *m1, const struct ether_header *eh1,
6468e8e04eSSam Leffler 	struct mbuf *m2, const struct ether_header *eh2);
65b032f27cSSam Leffler static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
6668e8e04eSSam Leffler 	u_int hdrsize, u_int ciphdrsize, u_int mtu);
6768e8e04eSSam Leffler static	void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
6868e8e04eSSam Leffler 
698a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
708a1b9b6aSSam Leffler /*
718a1b9b6aSSam Leffler  * Decide if an outbound management frame should be
728a1b9b6aSSam Leffler  * printed when debugging is enabled.  This filters some
738a1b9b6aSSam Leffler  * of the less interesting frames that come frequently
748a1b9b6aSSam Leffler  * (e.g. beacons).
758a1b9b6aSSam Leffler  */
768a1b9b6aSSam Leffler static __inline int
77b032f27cSSam Leffler doprint(struct ieee80211vap *vap, int subtype)
788a1b9b6aSSam Leffler {
798a1b9b6aSSam Leffler 	switch (subtype) {
808a1b9b6aSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
81b032f27cSSam Leffler 		return (vap->iv_opmode == IEEE80211_M_IBSS);
828a1b9b6aSSam Leffler 	}
838a1b9b6aSSam Leffler 	return 1;
848a1b9b6aSSam Leffler }
851a1e1d21SSam Leffler #endif
861a1e1d21SSam Leffler 
870a915fadSSam Leffler /*
88b032f27cSSam Leffler  * Start method for vap's.  All packets from the stack come
89b032f27cSSam Leffler  * through here.  We handle common processing of the packets
90b032f27cSSam Leffler  * before dispatching them to the underlying device.
91b032f27cSSam Leffler  */
92b032f27cSSam Leffler void
93b032f27cSSam Leffler ieee80211_start(struct ifnet *ifp)
94b032f27cSSam Leffler {
95b032f27cSSam Leffler #define	IS_DWDS(vap) \
96b032f27cSSam Leffler 	(vap->iv_opmode == IEEE80211_M_WDS && \
97b032f27cSSam Leffler 	 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
98b032f27cSSam Leffler 	struct ieee80211vap *vap = ifp->if_softc;
99b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
100b032f27cSSam Leffler 	struct ifnet *parent = ic->ic_ifp;
101b032f27cSSam Leffler 	struct ieee80211_node *ni;
102b032f27cSSam Leffler 	struct mbuf *m;
103b032f27cSSam Leffler 	struct ether_header *eh;
104b032f27cSSam Leffler 	int error;
105b032f27cSSam Leffler 
106b032f27cSSam Leffler 	/* NB: parent must be up and running */
107b032f27cSSam Leffler 	if (!IFNET_IS_UP_RUNNING(parent)) {
108b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
109b032f27cSSam Leffler 		    "%s: ignore queue, parent %s not up+running\n",
110b032f27cSSam Leffler 		    __func__, parent->if_xname);
111b032f27cSSam Leffler 		/* XXX stat */
112b032f27cSSam Leffler 		return;
113b032f27cSSam Leffler 	}
114b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_SLEEP) {
115b032f27cSSam Leffler 		/*
116b032f27cSSam Leffler 		 * In power save, wakeup device for transmit.
117b032f27cSSam Leffler 		 */
118b032f27cSSam Leffler 		ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
119b032f27cSSam Leffler 		return;
120b032f27cSSam Leffler 	}
121b032f27cSSam Leffler 	/*
122b032f27cSSam Leffler 	 * No data frames go out unless we're running.
123b032f27cSSam Leffler 	 * Note in particular this covers CAC and CSA
124b032f27cSSam Leffler 	 * states (though maybe we should check muting
125b032f27cSSam Leffler 	 * for CSA).
126b032f27cSSam Leffler 	 */
127b032f27cSSam Leffler 	if (vap->iv_state != IEEE80211_S_RUN) {
128b032f27cSSam Leffler 		IEEE80211_LOCK(ic);
129b032f27cSSam Leffler 		/* re-check under the com lock to avoid races */
130b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN) {
131b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
132b032f27cSSam Leffler 			    "%s: ignore queue, in %s state\n",
133b032f27cSSam Leffler 			    __func__, ieee80211_state_name[vap->iv_state]);
134b032f27cSSam Leffler 			vap->iv_stats.is_tx_badstate++;
135b032f27cSSam Leffler 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
136b032f27cSSam Leffler 			IEEE80211_UNLOCK(ic);
137b032f27cSSam Leffler 			return;
138b032f27cSSam Leffler 		}
139b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
140b032f27cSSam Leffler 	}
141b032f27cSSam Leffler 	for (;;) {
142b032f27cSSam Leffler 		IFQ_DEQUEUE(&ifp->if_snd, m);
143b032f27cSSam Leffler 		if (m == NULL)
144b032f27cSSam Leffler 			break;
145b032f27cSSam Leffler 		/*
146b032f27cSSam Leffler 		 * Sanitize mbuf flags for net80211 use.  We cannot
147b032f27cSSam Leffler 		 * clear M_PWR_SAV because this may be set for frames
148b032f27cSSam Leffler 		 * that are re-submitted from the power save queue.
149b032f27cSSam Leffler 		 *
150b032f27cSSam Leffler 		 * NB: This must be done before ieee80211_classify as
151b032f27cSSam Leffler 		 *     it marks EAPOL in frames with M_EAPOL.
152b032f27cSSam Leffler 		 */
153b032f27cSSam Leffler 		m->m_flags &= ~(M_80211_TX - M_PWR_SAV);
154b032f27cSSam Leffler 		/*
155b032f27cSSam Leffler 		 * Cancel any background scan.
156b032f27cSSam Leffler 		 */
157b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SCAN)
158b032f27cSSam Leffler 			ieee80211_cancel_anyscan(vap);
159b032f27cSSam Leffler 		/*
160b032f27cSSam Leffler 		 * Find the node for the destination so we can do
161b032f27cSSam Leffler 		 * things like power save and fast frames aggregation.
162b032f27cSSam Leffler 		 *
163b032f27cSSam Leffler 		 * NB: past this point various code assumes the first
164b032f27cSSam Leffler 		 *     mbuf has the 802.3 header present (and contiguous).
165b032f27cSSam Leffler 		 */
166b032f27cSSam Leffler 		ni = NULL;
167b032f27cSSam Leffler 		if (m->m_len < sizeof(struct ether_header) &&
168b032f27cSSam Leffler 		   (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
169b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
170b032f27cSSam Leffler 			    "discard frame, %s\n", "m_pullup failed");
171b032f27cSSam Leffler 			vap->iv_stats.is_tx_nobuf++;	/* XXX */
172b032f27cSSam Leffler 			ifp->if_oerrors++;
173b032f27cSSam Leffler 			continue;
174b032f27cSSam Leffler 		}
175b032f27cSSam Leffler 		eh = mtod(m, struct ether_header *);
176b032f27cSSam Leffler 		if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
177b032f27cSSam Leffler 			if (IS_DWDS(vap)) {
178b032f27cSSam Leffler 				/*
179b032f27cSSam Leffler 				 * Only unicast frames from the above go out
180b032f27cSSam Leffler 				 * DWDS vaps; multicast frames are handled by
181b032f27cSSam Leffler 				 * dispatching the frame as it comes through
182b032f27cSSam Leffler 				 * the AP vap (see below).
183b032f27cSSam Leffler 				 */
184b032f27cSSam Leffler 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
185b032f27cSSam Leffler 				    eh->ether_dhost, "mcast", "%s", "on DWDS");
186b032f27cSSam Leffler 				vap->iv_stats.is_dwds_mcast++;
187b032f27cSSam Leffler 				m_freem(m);
188b032f27cSSam Leffler 				continue;
189b032f27cSSam Leffler 			}
190b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
191b032f27cSSam Leffler 				/*
192b032f27cSSam Leffler 				 * Spam DWDS vap's w/ multicast traffic.
193b032f27cSSam Leffler 				 */
194b032f27cSSam Leffler 				/* XXX only if dwds in use? */
195b032f27cSSam Leffler 				ieee80211_dwds_mcast(vap, m);
196b032f27cSSam Leffler 			}
197b032f27cSSam Leffler 		}
198b032f27cSSam Leffler 		ni = ieee80211_find_txnode(vap, eh->ether_dhost);
199b032f27cSSam Leffler 		if (ni == NULL) {
200b032f27cSSam Leffler 			/* NB: ieee80211_find_txnode does stat+msg */
201b032f27cSSam Leffler 			ifp->if_oerrors++;
202b032f27cSSam Leffler 			m_freem(m);
203b032f27cSSam Leffler 			continue;
204b032f27cSSam Leffler 		}
205b032f27cSSam Leffler 		/* XXX AUTH'd */
206b032f27cSSam Leffler 		if (ni->ni_associd == 0) {
207b032f27cSSam Leffler 			/*
208b032f27cSSam Leffler 			 * Destination is not associated; must special
209b032f27cSSam Leffler 			 * case DWDS where we point iv_bss at the node
210b032f27cSSam Leffler 			 * for the associated station.
211b032f27cSSam Leffler 			 * XXX adhoc mode?
212b032f27cSSam Leffler 			 */
213b032f27cSSam Leffler 			if (ni != vap->iv_bss || IS_DWDS(vap)) {
214b032f27cSSam Leffler 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
215b032f27cSSam Leffler 				    eh->ether_dhost, NULL,
216b032f27cSSam Leffler 				    "sta not associated (type 0x%04x)",
217b032f27cSSam Leffler 				    htons(eh->ether_type));
218b032f27cSSam Leffler 				vap->iv_stats.is_tx_notassoc++;
219b032f27cSSam Leffler 				ifp->if_oerrors++;
220b032f27cSSam Leffler 				m_freem(m);
221b032f27cSSam Leffler 				ieee80211_free_node(ni);
222b032f27cSSam Leffler 				continue;
223b032f27cSSam Leffler 			}
224b032f27cSSam Leffler 		}
225b032f27cSSam Leffler 		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
226b032f27cSSam Leffler 		    (m->m_flags & M_PWR_SAV) == 0) {
227b032f27cSSam Leffler 			/*
228b032f27cSSam Leffler 			 * Station in power save mode; pass the frame
229b032f27cSSam Leffler 			 * to the 802.11 layer and continue.  We'll get
230b032f27cSSam Leffler 			 * the frame back when the time is right.
231b032f27cSSam Leffler 			 * XXX lose WDS vap linkage?
232b032f27cSSam Leffler 			 */
233b032f27cSSam Leffler 			ieee80211_pwrsave(ni, m);
234b032f27cSSam Leffler 			ieee80211_free_node(ni);
235b032f27cSSam Leffler 			continue;
236b032f27cSSam Leffler 		}
237b032f27cSSam Leffler 		/* calculate priority so drivers can find the tx queue */
238b032f27cSSam Leffler 		if (ieee80211_classify(ni, m)) {
239b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
240b032f27cSSam Leffler 			    eh->ether_dhost, NULL,
241b032f27cSSam Leffler 			    "%s", "classification failure");
242b032f27cSSam Leffler 			vap->iv_stats.is_tx_classify++;
243b032f27cSSam Leffler 			ifp->if_oerrors++;
244b032f27cSSam Leffler 			m_freem(m);
245b032f27cSSam Leffler 			ieee80211_free_node(ni);
246b032f27cSSam Leffler 			continue;
247b032f27cSSam Leffler 		}
248b032f27cSSam Leffler 
249b032f27cSSam Leffler 		BPF_MTAP(ifp, m);		/* 802.11 tx path */
250b032f27cSSam Leffler 
251b032f27cSSam Leffler 		/*
252b032f27cSSam Leffler 		 * XXX When ni is associated with a WDS link then
253b032f27cSSam Leffler 		 * the vap will be the WDS vap but ni_vap will point
254b032f27cSSam Leffler 		 * to the ap vap the station associated to.  Once
255b032f27cSSam Leffler 		 * we handoff the packet to the driver the callback
256b032f27cSSam Leffler 		 * to ieee80211_encap won't be able to tell if the
257b032f27cSSam Leffler 		 * packet should be encapsulated for WDS or not (e.g.
258b032f27cSSam Leffler 		 * multicast frames will not be handled correctly).
259b032f27cSSam Leffler 		 * We hack this by marking the mbuf so ieee80211_encap
260b032f27cSSam Leffler 		 * can do the right thing.
261b032f27cSSam Leffler 		 */
262b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_WDS)
263b032f27cSSam Leffler 			m->m_flags |= M_WDS;
264b032f27cSSam Leffler 		else
265b032f27cSSam Leffler 			m->m_flags &= ~M_WDS;
266b032f27cSSam Leffler 
267b032f27cSSam Leffler 		/*
268b032f27cSSam Leffler 		 * Stash the node pointer and hand the frame off to
269b032f27cSSam Leffler 		 * the underlying device.  Note that we do this after
270b032f27cSSam Leffler 		 * any call to ieee80211_dwds_mcast because that code
271b032f27cSSam Leffler 		 * uses any existing value for rcvif.
272b032f27cSSam Leffler 		 */
273b032f27cSSam Leffler 		m->m_pkthdr.rcvif = (void *)ni;
274b032f27cSSam Leffler 
275b032f27cSSam Leffler 		/* XXX defer if_start calls? */
276b032f27cSSam Leffler 		IFQ_HANDOFF(parent, m, error);
277b032f27cSSam Leffler 		if (error != 0) {
278b032f27cSSam Leffler 			/* NB: IFQ_HANDOFF reclaims mbuf */
279b032f27cSSam Leffler 			ieee80211_free_node(ni);
280b032f27cSSam Leffler 		} else {
281b032f27cSSam Leffler 			ifp->if_opackets++;
282b032f27cSSam Leffler 		}
283b032f27cSSam Leffler 		ic->ic_lastdata = ticks;
284b032f27cSSam Leffler 	}
285b032f27cSSam Leffler #undef IS_DWDS
286b032f27cSSam Leffler }
287b032f27cSSam Leffler 
288b032f27cSSam Leffler /*
289b032f27cSSam Leffler  * 802.11 output routine. This is (currently) used only to
290b032f27cSSam Leffler  * connect bpf write calls to the 802.11 layer for injecting
291b032f27cSSam Leffler  * raw 802.11 frames.  Note we locate the ieee80211com from
292b032f27cSSam Leffler  * the ifnet using a spare field setup at attach time.  This
293b032f27cSSam Leffler  * will go away when the virtual ap support comes in.
294b032f27cSSam Leffler  */
295b032f27cSSam Leffler int
296b032f27cSSam Leffler ieee80211_output(struct ifnet *ifp, struct mbuf *m,
297b032f27cSSam Leffler 	struct sockaddr *dst, struct rtentry *rt0)
298b032f27cSSam Leffler {
299b032f27cSSam Leffler #define senderr(e) do { error = (e); goto bad;} while (0)
300b032f27cSSam Leffler 	struct ieee80211_node *ni = NULL;
301b032f27cSSam Leffler 	struct ieee80211vap *vap;
302b032f27cSSam Leffler 	struct ieee80211_frame *wh;
303b032f27cSSam Leffler 	int error;
304b032f27cSSam Leffler 
305b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
306b032f27cSSam Leffler 		/*
307b032f27cSSam Leffler 		 * Short-circuit requests if the vap is marked OACTIVE
308b032f27cSSam Leffler 		 * as this is used when tearing down state to indicate
309b032f27cSSam Leffler 		 * the vap may be gone.  This can also happen because a
310b032f27cSSam Leffler 		 * packet came down through ieee80211_start before the
311b032f27cSSam Leffler 		 * vap entered RUN state in which case it's also ok to
312b032f27cSSam Leffler 		 * just drop the frame.  This should not be necessary
313b032f27cSSam Leffler 		 * but callers of if_output don't check OACTIVE.
314b032f27cSSam Leffler 		 */
315b032f27cSSam Leffler 		senderr(ENETDOWN);
316b032f27cSSam Leffler 	}
317b032f27cSSam Leffler 	vap = ifp->if_softc;
318b032f27cSSam Leffler 	/*
319b032f27cSSam Leffler 	 * Hand to the 802.3 code if not tagged as
320b032f27cSSam Leffler 	 * a raw 802.11 frame.
321b032f27cSSam Leffler 	 */
322b032f27cSSam Leffler 	if (dst->sa_family != AF_IEEE80211)
323b032f27cSSam Leffler 		return vap->iv_output(ifp, m, dst, rt0);
324b032f27cSSam Leffler #ifdef MAC
325b032f27cSSam Leffler 	error = mac_check_ifnet_transmit(ifp, m);
326b032f27cSSam Leffler 	if (error)
327b032f27cSSam Leffler 		senderr(error);
328b032f27cSSam Leffler #endif
329b032f27cSSam Leffler 	if (ifp->if_flags & IFF_MONITOR)
330b032f27cSSam Leffler 		senderr(ENETDOWN);
331b032f27cSSam Leffler 	if (!IFNET_IS_UP_RUNNING(ifp))
332b032f27cSSam Leffler 		senderr(ENETDOWN);
333b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
334b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap,
335b032f27cSSam Leffler 		    IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
336b032f27cSSam Leffler 		    "block %s frame in CAC state\n", "raw data");
337b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
338b032f27cSSam Leffler 		senderr(EIO);		/* XXX */
339b032f27cSSam Leffler 	}
340b032f27cSSam Leffler 	/* XXX bypass bridge, pfil, carp, etc. */
341b032f27cSSam Leffler 
342b032f27cSSam Leffler 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
343b032f27cSSam Leffler 		senderr(EIO);	/* XXX */
344b032f27cSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
345b032f27cSSam Leffler 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
346b032f27cSSam Leffler 	    IEEE80211_FC0_VERSION_0)
347b032f27cSSam Leffler 		senderr(EIO);	/* XXX */
348b032f27cSSam Leffler 
349b032f27cSSam Leffler 	/* locate destination node */
350b032f27cSSam Leffler 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
351b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_NODS:
352b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_FROMDS:
353b032f27cSSam Leffler 		ni = ieee80211_find_txnode(vap, wh->i_addr1);
354b032f27cSSam Leffler 		break;
355b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_TODS:
356b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_DSTODS:
357b032f27cSSam Leffler 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame))
358b032f27cSSam Leffler 			senderr(EIO);	/* XXX */
359b032f27cSSam Leffler 		ni = ieee80211_find_txnode(vap, wh->i_addr3);
360b032f27cSSam Leffler 		break;
361b032f27cSSam Leffler 	default:
362b032f27cSSam Leffler 		senderr(EIO);	/* XXX */
363b032f27cSSam Leffler 	}
364b032f27cSSam Leffler 	if (ni == NULL) {
365b032f27cSSam Leffler 		/*
366b032f27cSSam Leffler 		 * Permit packets w/ bpf params through regardless
367b032f27cSSam Leffler 		 * (see below about sa_len).
368b032f27cSSam Leffler 		 */
369b032f27cSSam Leffler 		if (dst->sa_len == 0)
370b032f27cSSam Leffler 			senderr(EHOSTUNREACH);
371b032f27cSSam Leffler 		ni = ieee80211_ref_node(vap->iv_bss);
372b032f27cSSam Leffler 	}
373b032f27cSSam Leffler 
374b032f27cSSam Leffler 	/*
375b032f27cSSam Leffler 	 * Sanitize mbuf for net80211 flags leaked from above.
376b032f27cSSam Leffler 	 *
377b032f27cSSam Leffler 	 * NB: This must be done before ieee80211_classify as
378b032f27cSSam Leffler 	 *     it marks EAPOL in frames with M_EAPOL.
379b032f27cSSam Leffler 	 */
380b032f27cSSam Leffler 	m->m_flags &= ~M_80211_TX;
381b032f27cSSam Leffler 
382b032f27cSSam Leffler 	/* calculate priority so drivers can find the tx queue */
383b032f27cSSam Leffler 	/* XXX assumes an 802.3 frame */
384b032f27cSSam Leffler 	if (ieee80211_classify(ni, m))
385b032f27cSSam Leffler 		senderr(EIO);		/* XXX */
386b032f27cSSam Leffler 
387b032f27cSSam Leffler 	BPF_MTAP(ifp, m);
388b032f27cSSam Leffler 
389b032f27cSSam Leffler 	/*
390b032f27cSSam Leffler 	 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
391b032f27cSSam Leffler 	 * present by setting the sa_len field of the sockaddr (yes,
392b032f27cSSam Leffler 	 * this is a hack).
393b032f27cSSam Leffler 	 * NB: we assume sa_data is suitably aligned to cast.
394b032f27cSSam Leffler 	 */
395b032f27cSSam Leffler 	return vap->iv_ic->ic_raw_xmit(ni, m,
396b032f27cSSam Leffler 	    (const struct ieee80211_bpf_params *)(dst->sa_len ?
397b032f27cSSam Leffler 		dst->sa_data : NULL));
398b032f27cSSam Leffler bad:
399b032f27cSSam Leffler 	if (m != NULL)
400b032f27cSSam Leffler 		m_freem(m);
401b032f27cSSam Leffler 	if (ni != NULL)
402b032f27cSSam Leffler 		ieee80211_free_node(ni);
403b032f27cSSam Leffler 	return error;
404b032f27cSSam Leffler #undef senderr
405b032f27cSSam Leffler }
406b032f27cSSam Leffler 
407b032f27cSSam Leffler /*
408add59d08SSam Leffler  * Set the direction field and address fields of an outgoing
409add59d08SSam Leffler  * non-QoS frame.  Note this should be called early on in
410add59d08SSam Leffler  * constructing a frame as it sets i_fc[1]; other bits can
411add59d08SSam Leffler  * then be or'd in.
412add59d08SSam Leffler  */
413add59d08SSam Leffler static void
414b032f27cSSam Leffler ieee80211_send_setup(
415add59d08SSam Leffler 	struct ieee80211_node *ni,
416add59d08SSam Leffler 	struct ieee80211_frame *wh,
417add59d08SSam Leffler 	int type,
41868e8e04eSSam Leffler 	const uint8_t sa[IEEE80211_ADDR_LEN],
41968e8e04eSSam Leffler 	const uint8_t da[IEEE80211_ADDR_LEN],
42068e8e04eSSam Leffler 	const uint8_t bssid[IEEE80211_ADDR_LEN])
421add59d08SSam Leffler {
422add59d08SSam Leffler #define	WH4(wh)	((struct ieee80211_frame_addr4 *)wh)
423add59d08SSam Leffler 
424add59d08SSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
425add59d08SSam Leffler 	if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
426b032f27cSSam Leffler 		struct ieee80211vap *vap = ni->ni_vap;
427b032f27cSSam Leffler 
428b032f27cSSam Leffler 		switch (vap->iv_opmode) {
429add59d08SSam Leffler 		case IEEE80211_M_STA:
430add59d08SSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
431add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
432add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
433add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, da);
434add59d08SSam Leffler 			break;
435add59d08SSam Leffler 		case IEEE80211_M_IBSS:
436add59d08SSam Leffler 		case IEEE80211_M_AHDEMO:
437add59d08SSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
438add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
439add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
440add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
441add59d08SSam Leffler 			break;
442add59d08SSam Leffler 		case IEEE80211_M_HOSTAP:
443add59d08SSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
444add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
445add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
446add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, sa);
447add59d08SSam Leffler 			break;
44868e8e04eSSam Leffler 		case IEEE80211_M_WDS:
44968e8e04eSSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
450b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
451b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
45268e8e04eSSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, da);
45368e8e04eSSam Leffler 			IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
45468e8e04eSSam Leffler 			break;
455add59d08SSam Leffler 		case IEEE80211_M_MONITOR:	/* NB: to quiet compiler */
456add59d08SSam Leffler 			break;
457add59d08SSam Leffler 		}
458add59d08SSam Leffler 	} else {
459add59d08SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
460add59d08SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, da);
461add59d08SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, sa);
462add59d08SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
463add59d08SSam Leffler 	}
46468e8e04eSSam Leffler 	*(uint16_t *)&wh->i_dur[0] = 0;
465b032f27cSSam Leffler 	/* XXX probe response use per-vap seq#? */
466add59d08SSam Leffler 	/* NB: use non-QoS tid */
46768e8e04eSSam Leffler 	*(uint16_t *)&wh->i_seq[0] =
468801df4a5SSam Leffler 	    htole16(ni->ni_txseqs[IEEE80211_NONQOS_TID] << IEEE80211_SEQ_SEQ_SHIFT);
469801df4a5SSam Leffler 	ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
470add59d08SSam Leffler #undef WH4
471add59d08SSam Leffler }
472add59d08SSam Leffler 
473add59d08SSam Leffler /*
4740a915fadSSam Leffler  * Send a management frame to the specified node.  The node pointer
4750a915fadSSam Leffler  * must have a reference as the pointer will be passed to the driver
4760a915fadSSam Leffler  * and potentially held for a long time.  If the frame is successfully
4770a915fadSSam Leffler  * dispatched to the driver, then it is responsible for freeing the
478b032f27cSSam Leffler  * reference (and potentially free'ing up any associated storage);
479b032f27cSSam Leffler  * otherwise deal with reclaiming any reference (on error).
4800a915fadSSam Leffler  */
48168e8e04eSSam Leffler int
482b032f27cSSam Leffler ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type)
4831a1e1d21SSam Leffler {
484b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
485b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
4861a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
4871a1e1d21SSam Leffler 
4880a915fadSSam Leffler 	KASSERT(ni != NULL, ("null node"));
4891a1e1d21SSam Leffler 
490b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
491b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
492b032f27cSSam Leffler 		    ni, "block %s frame in CAC state",
493b032f27cSSam Leffler 			ieee80211_mgt_subtype_name[
494b032f27cSSam Leffler 			    (type & IEEE80211_FC0_SUBTYPE_MASK) >>
495b032f27cSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT]);
496b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
497b032f27cSSam Leffler 		ieee80211_free_node(ni);
498b032f27cSSam Leffler 		m_freem(m);
499b032f27cSSam Leffler 		return EIO;		/* XXX */
500b032f27cSSam Leffler 	}
501b032f27cSSam Leffler 
5021a1e1d21SSam Leffler 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
503b032f27cSSam Leffler 	if (m == NULL) {
504b032f27cSSam Leffler 		ieee80211_free_node(ni);
5051a1e1d21SSam Leffler 		return ENOMEM;
506b032f27cSSam Leffler 	}
5070a915fadSSam Leffler 
5081a1e1d21SSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
509b032f27cSSam Leffler 	ieee80211_send_setup(ni, wh,
510add59d08SSam Leffler 		IEEE80211_FC0_TYPE_MGT | type,
511b032f27cSSam Leffler 		vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
5128a1b9b6aSSam Leffler 	if ((m->m_flags & M_LINK0) != 0 && ni->ni_challenge != NULL) {
5138a1b9b6aSSam Leffler 		m->m_flags &= ~M_LINK0;
514b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
515b032f27cSSam Leffler 		    "encrypting frame (%s)", __func__);
5168a1b9b6aSSam Leffler 		wh->i_fc[1] |= IEEE80211_FC1_WEP;
5178a1b9b6aSSam Leffler 	}
518b032f27cSSam Leffler 	if (type != IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
519b032f27cSSam Leffler 		/* NB: force non-ProbeResp frames to the highest queue */
520bb239ce9SSam Leffler 		M_WME_SETAC(m, WME_AC_VO);
521bb239ce9SSam Leffler 	} else
522bb239ce9SSam Leffler 		M_WME_SETAC(m, WME_AC_BE);
5238a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
5248a1b9b6aSSam Leffler 	/* avoid printing too many frames */
525b032f27cSSam Leffler 	if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
526b032f27cSSam Leffler 	    ieee80211_msg_dumppkts(vap)) {
5278a1b9b6aSSam Leffler 		printf("[%s] send %s on channel %u\n",
5288a1b9b6aSSam Leffler 		    ether_sprintf(wh->i_addr1),
5298a1b9b6aSSam Leffler 		    ieee80211_mgt_subtype_name[
5308a1b9b6aSSam Leffler 			(type & IEEE80211_FC0_SUBTYPE_MASK) >>
5318a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
532b5c99415SSam Leffler 		    ieee80211_chan2ieee(ic, ic->ic_curchan));
5338a1b9b6aSSam Leffler 	}
5348a1b9b6aSSam Leffler #endif
5358a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_mgmt);
53668e8e04eSSam Leffler 
537b032f27cSSam Leffler 	return ic->ic_raw_xmit(ni, m, NULL);
538246b5467SSam Leffler }
539246b5467SSam Leffler 
540246b5467SSam Leffler /*
5418a1b9b6aSSam Leffler  * Send a null data frame to the specified node.
54219ad2dd7SSam Leffler  *
54319ad2dd7SSam Leffler  * NB: the caller is assumed to have setup a node reference
54419ad2dd7SSam Leffler  *     for use; this is necessary to deal with a race condition
545b032f27cSSam Leffler  *     when probing for inactive stations.  Like ieee80211_mgmt_output
546b032f27cSSam Leffler  *     we must cleanup any node reference on error;  however we
547b032f27cSSam Leffler  *     can safely just unref it as we know it will never be the
548b032f27cSSam Leffler  *     last reference to the node.
5498a1b9b6aSSam Leffler  */
5508a1b9b6aSSam Leffler int
551f62121ceSSam Leffler ieee80211_send_nulldata(struct ieee80211_node *ni)
5528a1b9b6aSSam Leffler {
553b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
554f62121ceSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
5558a1b9b6aSSam Leffler 	struct mbuf *m;
5568a1b9b6aSSam Leffler 	struct ieee80211_frame *wh;
5578a1b9b6aSSam Leffler 
558b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
559b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
560b032f27cSSam Leffler 		    ni, "block %s frame in CAC state", "null data");
561b032f27cSSam Leffler 		ieee80211_unref_node(&ni);
562b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
563b032f27cSSam Leffler 		return EIO;		/* XXX */
564b032f27cSSam Leffler 	}
565b032f27cSSam Leffler 
566b032f27cSSam Leffler 	m = m_gethdr(M_NOWAIT, MT_HEADER);
5678a1b9b6aSSam Leffler 	if (m == NULL) {
5688a1b9b6aSSam Leffler 		/* XXX debug msg */
56919ad2dd7SSam Leffler 		ieee80211_unref_node(&ni);
570b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
5718a1b9b6aSSam Leffler 		return ENOMEM;
5728a1b9b6aSSam Leffler 	}
57368e8e04eSSam Leffler 	MH_ALIGN(m, sizeof(struct ieee80211_frame));
5748a1b9b6aSSam Leffler 
5758a1b9b6aSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
576b032f27cSSam Leffler 	ieee80211_send_setup(ni, wh,
577add59d08SSam Leffler 		IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
578b032f27cSSam Leffler 		vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
579b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_WDS) {
580add59d08SSam Leffler 		/* NB: power management bit is never sent by an AP */
581add59d08SSam Leffler 		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
582b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_HOSTAP)
583add59d08SSam Leffler 			wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
5848a1b9b6aSSam Leffler 		m->m_len = m->m_pkthdr.len = sizeof(struct ieee80211_frame);
585b032f27cSSam Leffler 	} else {
586b032f27cSSam Leffler 		/* NB: 4-address frame */
587b032f27cSSam Leffler 		m->m_len = m->m_pkthdr.len =
588b032f27cSSam Leffler 		    sizeof(struct ieee80211_frame_addr4);
589b032f27cSSam Leffler 	}
590bb239ce9SSam Leffler 	M_WME_SETAC(m, WME_AC_BE);
5918a1b9b6aSSam Leffler 
5928a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_data);
5938a1b9b6aSSam Leffler 
594b032f27cSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
595b032f27cSSam Leffler 	    "send null data frame on channel %u, pwr mgt %s",
596b5c99415SSam Leffler 	    ieee80211_chan2ieee(ic, ic->ic_curchan),
597add59d08SSam Leffler 	    wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
598add59d08SSam Leffler 
599b032f27cSSam Leffler 	return ic->ic_raw_xmit(ni, m, NULL);
6008a1b9b6aSSam Leffler }
6018a1b9b6aSSam Leffler 
6028a1b9b6aSSam Leffler /*
6038a1b9b6aSSam Leffler  * Assign priority to a frame based on any vlan tag assigned
6048a1b9b6aSSam Leffler  * to the station and/or any Diffserv setting in an IP header.
6058a1b9b6aSSam Leffler  * Finally, if an ACM policy is setup (in station mode) it's
6068a1b9b6aSSam Leffler  * applied.
6078a1b9b6aSSam Leffler  */
6088a1b9b6aSSam Leffler int
609b032f27cSSam Leffler ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
6108a1b9b6aSSam Leffler {
611b032f27cSSam Leffler 	const struct ether_header *eh = mtod(m, struct ether_header *);
6128a1b9b6aSSam Leffler 	int v_wme_ac, d_wme_ac, ac;
6138a1b9b6aSSam Leffler 
614b032f27cSSam Leffler 	/*
615b032f27cSSam Leffler 	 * Always promote PAE/EAPOL frames to high priority.
616b032f27cSSam Leffler 	 */
617b032f27cSSam Leffler 	if (eh->ether_type == htons(ETHERTYPE_PAE)) {
618b032f27cSSam Leffler 		/* NB: mark so others don't need to check header */
619b032f27cSSam Leffler 		m->m_flags |= M_EAPOL;
620b032f27cSSam Leffler 		ac = WME_AC_VO;
621b032f27cSSam Leffler 		goto done;
622b032f27cSSam Leffler 	}
623b032f27cSSam Leffler 	/*
624b032f27cSSam Leffler 	 * Non-qos traffic goes to BE.
625b032f27cSSam Leffler 	 */
6268a1b9b6aSSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
6278a1b9b6aSSam Leffler 		ac = WME_AC_BE;
6288a1b9b6aSSam Leffler 		goto done;
6298a1b9b6aSSam Leffler 	}
6308a1b9b6aSSam Leffler 
6318a1b9b6aSSam Leffler 	/*
6328a1b9b6aSSam Leffler 	 * If node has a vlan tag then all traffic
6338a1b9b6aSSam Leffler 	 * to it must have a matching tag.
6348a1b9b6aSSam Leffler 	 */
6358a1b9b6aSSam Leffler 	v_wme_ac = 0;
6368a1b9b6aSSam Leffler 	if (ni->ni_vlan != 0) {
63778ba57b9SAndre Oppermann 		 if ((m->m_flags & M_VLANTAG) == 0) {
6388a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_novlantag);
6398a1b9b6aSSam Leffler 			return 1;
6408a1b9b6aSSam Leffler 		}
64178ba57b9SAndre Oppermann 		if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
6428a1b9b6aSSam Leffler 		    EVL_VLANOFTAG(ni->ni_vlan)) {
6438a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
6448a1b9b6aSSam Leffler 			return 1;
6458a1b9b6aSSam Leffler 		}
6468a1b9b6aSSam Leffler 		/* map vlan priority to AC */
647f4558c9aSSam Leffler 		v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
6488a1b9b6aSSam Leffler 	}
6498a1b9b6aSSam Leffler 
6508a1b9b6aSSam Leffler #ifdef INET
6518a1b9b6aSSam Leffler 	if (eh->ether_type == htons(ETHERTYPE_IP)) {
652f4558c9aSSam Leffler 		uint8_t tos;
6538a1b9b6aSSam Leffler 		/*
654f4558c9aSSam Leffler 		 * IP frame, map the DSCP bits from the TOS field.
6558a1b9b6aSSam Leffler 		 */
656f4558c9aSSam Leffler 		/* XXX m_copydata may be too slow for fast path */
657f4558c9aSSam Leffler 		/* NB: ip header may not be in first mbuf */
658f4558c9aSSam Leffler 		m_copydata(m, sizeof(struct ether_header) +
659f4558c9aSSam Leffler 		    offsetof(struct ip, ip_tos), sizeof(tos), &tos);
660f4558c9aSSam Leffler 		tos >>= 5;		/* NB: ECN + low 3 bits of DSCP */
661f4558c9aSSam Leffler 		d_wme_ac = TID_TO_WME_AC(tos);
6628a1b9b6aSSam Leffler 	} else {
6638a1b9b6aSSam Leffler #endif /* INET */
6648a1b9b6aSSam Leffler 		d_wme_ac = WME_AC_BE;
6658a1b9b6aSSam Leffler #ifdef INET
6668a1b9b6aSSam Leffler 	}
6678a1b9b6aSSam Leffler #endif
6688a1b9b6aSSam Leffler 	/*
6698a1b9b6aSSam Leffler 	 * Use highest priority AC.
6708a1b9b6aSSam Leffler 	 */
6718a1b9b6aSSam Leffler 	if (v_wme_ac > d_wme_ac)
6728a1b9b6aSSam Leffler 		ac = v_wme_ac;
6738a1b9b6aSSam Leffler 	else
6748a1b9b6aSSam Leffler 		ac = d_wme_ac;
6758a1b9b6aSSam Leffler 
6768a1b9b6aSSam Leffler 	/*
6778a1b9b6aSSam Leffler 	 * Apply ACM policy.
6788a1b9b6aSSam Leffler 	 */
679b032f27cSSam Leffler 	if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
6808a1b9b6aSSam Leffler 		static const int acmap[4] = {
6818a1b9b6aSSam Leffler 			WME_AC_BK,	/* WME_AC_BE */
6828a1b9b6aSSam Leffler 			WME_AC_BK,	/* WME_AC_BK */
6838a1b9b6aSSam Leffler 			WME_AC_BE,	/* WME_AC_VI */
6848a1b9b6aSSam Leffler 			WME_AC_VI,	/* WME_AC_VO */
6858a1b9b6aSSam Leffler 		};
686b032f27cSSam Leffler 		struct ieee80211com *ic = ni->ni_ic;
687b032f27cSSam Leffler 
6888a1b9b6aSSam Leffler 		while (ac != WME_AC_BK &&
6898a1b9b6aSSam Leffler 		    ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
6908a1b9b6aSSam Leffler 			ac = acmap[ac];
6918a1b9b6aSSam Leffler 	}
6928a1b9b6aSSam Leffler done:
6938a1b9b6aSSam Leffler 	M_WME_SETAC(m, ac);
6948a1b9b6aSSam Leffler 	return 0;
6958a1b9b6aSSam Leffler }
6968a1b9b6aSSam Leffler 
6978a1b9b6aSSam Leffler /*
6985e923d2eSSam Leffler  * Insure there is sufficient contiguous space to encapsulate the
6995e923d2eSSam Leffler  * 802.11 data frame.  If room isn't already there, arrange for it.
7005e923d2eSSam Leffler  * Drivers and cipher modules assume we have done the necessary work
7015e923d2eSSam Leffler  * and fail rudely if they don't find the space they need.
7025e923d2eSSam Leffler  */
7035e923d2eSSam Leffler static struct mbuf *
704b032f27cSSam Leffler ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
7055e923d2eSSam Leffler 	struct ieee80211_key *key, struct mbuf *m)
7065e923d2eSSam Leffler {
707ab96db10SSam Leffler #define	TO_BE_RECLAIMED	(sizeof(struct ether_header) - sizeof(struct llc))
708b032f27cSSam Leffler 	int needed_space = vap->iv_ic->ic_headroom + hdrsize;
7095e923d2eSSam Leffler 
7105e923d2eSSam Leffler 	if (key != NULL) {
7115e923d2eSSam Leffler 		/* XXX belongs in crypto code? */
7125e923d2eSSam Leffler 		needed_space += key->wk_cipher->ic_header;
7135e923d2eSSam Leffler 		/* XXX frags */
71483a244dbSSam Leffler 		/*
71583a244dbSSam Leffler 		 * When crypto is being done in the host we must insure
71683a244dbSSam Leffler 		 * the data are writable for the cipher routines; clone
71783a244dbSSam Leffler 		 * a writable mbuf chain.
71883a244dbSSam Leffler 		 * XXX handle SWMIC specially
71983a244dbSSam Leffler 		 */
7205c1f7f19SSam Leffler 		if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
72183a244dbSSam Leffler 			m = m_unshare(m, M_NOWAIT);
72283a244dbSSam Leffler 			if (m == NULL) {
723b032f27cSSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
72483a244dbSSam Leffler 				    "%s: cannot get writable mbuf\n", __func__);
725b032f27cSSam Leffler 				vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
72683a244dbSSam Leffler 				return NULL;
72783a244dbSSam Leffler 			}
72883a244dbSSam Leffler 		}
7295e923d2eSSam Leffler 	}
7305e923d2eSSam Leffler 	/*
7315e923d2eSSam Leffler 	 * We know we are called just before stripping an Ethernet
7325e923d2eSSam Leffler 	 * header and prepending an LLC header.  This means we know
7335e923d2eSSam Leffler 	 * there will be
734ab96db10SSam Leffler 	 *	sizeof(struct ether_header) - sizeof(struct llc)
7355e923d2eSSam Leffler 	 * bytes recovered to which we need additional space for the
7365e923d2eSSam Leffler 	 * 802.11 header and any crypto header.
7375e923d2eSSam Leffler 	 */
7385e923d2eSSam Leffler 	/* XXX check trailing space and copy instead? */
7395e923d2eSSam Leffler 	if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
7405e923d2eSSam Leffler 		struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
7415e923d2eSSam Leffler 		if (n == NULL) {
742b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
7435e923d2eSSam Leffler 			    "%s: cannot expand storage\n", __func__);
744b032f27cSSam Leffler 			vap->iv_stats.is_tx_nobuf++;
7455e923d2eSSam Leffler 			m_freem(m);
7465e923d2eSSam Leffler 			return NULL;
7475e923d2eSSam Leffler 		}
7485e923d2eSSam Leffler 		KASSERT(needed_space <= MHLEN,
7495e923d2eSSam Leffler 		    ("not enough room, need %u got %zu\n", needed_space, MHLEN));
7505e923d2eSSam Leffler 		/*
7515e923d2eSSam Leffler 		 * Setup new mbuf to have leading space to prepend the
7525e923d2eSSam Leffler 		 * 802.11 header and any crypto header bits that are
7535e923d2eSSam Leffler 		 * required (the latter are added when the driver calls
7545e923d2eSSam Leffler 		 * back to ieee80211_crypto_encap to do crypto encapsulation).
7555e923d2eSSam Leffler 		 */
7565e923d2eSSam Leffler 		/* NB: must be first 'cuz it clobbers m_data */
7575e923d2eSSam Leffler 		m_move_pkthdr(n, m);
7585e923d2eSSam Leffler 		n->m_len = 0;			/* NB: m_gethdr does not set */
7595e923d2eSSam Leffler 		n->m_data += needed_space;
7605e923d2eSSam Leffler 		/*
7615e923d2eSSam Leffler 		 * Pull up Ethernet header to create the expected layout.
7625e923d2eSSam Leffler 		 * We could use m_pullup but that's overkill (i.e. we don't
7635e923d2eSSam Leffler 		 * need the actual data) and it cannot fail so do it inline
7645e923d2eSSam Leffler 		 * for speed.
7655e923d2eSSam Leffler 		 */
7665e923d2eSSam Leffler 		/* NB: struct ether_header is known to be contiguous */
7675e923d2eSSam Leffler 		n->m_len += sizeof(struct ether_header);
7685e923d2eSSam Leffler 		m->m_len -= sizeof(struct ether_header);
7695e923d2eSSam Leffler 		m->m_data += sizeof(struct ether_header);
7705e923d2eSSam Leffler 		/*
7715e923d2eSSam Leffler 		 * Replace the head of the chain.
7725e923d2eSSam Leffler 		 */
7735e923d2eSSam Leffler 		n->m_next = m;
7745e923d2eSSam Leffler 		m = n;
7755e923d2eSSam Leffler 	}
7765e923d2eSSam Leffler 	return m;
7775e923d2eSSam Leffler #undef TO_BE_RECLAIMED
7785e923d2eSSam Leffler }
7795e923d2eSSam Leffler 
7805e923d2eSSam Leffler /*
7815e923d2eSSam Leffler  * Return the transmit key to use in sending a unicast frame.
7825e923d2eSSam Leffler  * If a unicast key is set we use that.  When no unicast key is set
7835e923d2eSSam Leffler  * we fall back to the default transmit key.
7848a1b9b6aSSam Leffler  */
7858a1b9b6aSSam Leffler static __inline struct ieee80211_key *
786b032f27cSSam Leffler ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
787b032f27cSSam Leffler 	struct ieee80211_node *ni)
7888a1b9b6aSSam Leffler {
789cda15ce1SSam Leffler 	if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
790b032f27cSSam Leffler 		if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
791b032f27cSSam Leffler 		    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
7928a1b9b6aSSam Leffler 			return NULL;
793b032f27cSSam Leffler 		return &vap->iv_nw_keys[vap->iv_def_txkey];
7948a1b9b6aSSam Leffler 	} else {
7958a1b9b6aSSam Leffler 		return &ni->ni_ucastkey;
7968a1b9b6aSSam Leffler 	}
7975e923d2eSSam Leffler }
7985e923d2eSSam Leffler 
7995e923d2eSSam Leffler /*
8005e923d2eSSam Leffler  * Return the transmit key to use in sending a multicast frame.
8015e923d2eSSam Leffler  * Multicast traffic always uses the group key which is installed as
8025e923d2eSSam Leffler  * the default tx key.
8035e923d2eSSam Leffler  */
8045e923d2eSSam Leffler static __inline struct ieee80211_key *
805b032f27cSSam Leffler ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
806b032f27cSSam Leffler 	struct ieee80211_node *ni)
8075e923d2eSSam Leffler {
808b032f27cSSam Leffler 	if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
809b032f27cSSam Leffler 	    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
8105e923d2eSSam Leffler 		return NULL;
811b032f27cSSam Leffler 	return &vap->iv_nw_keys[vap->iv_def_txkey];
8128a1b9b6aSSam Leffler }
8138a1b9b6aSSam Leffler 
8148a1b9b6aSSam Leffler /*
8158a1b9b6aSSam Leffler  * Encapsulate an outbound data frame.  The mbuf chain is updated.
8168a1b9b6aSSam Leffler  * If an error is encountered NULL is returned.  The caller is required
8178a1b9b6aSSam Leffler  * to provide a node reference and pullup the ethernet header in the
8188a1b9b6aSSam Leffler  * first mbuf.
819b032f27cSSam Leffler  *
820b032f27cSSam Leffler  * NB: Packet is assumed to be processed by ieee80211_classify which
821b032f27cSSam Leffler  *     marked EAPOL frames w/ M_EAPOL.
8220a915fadSSam Leffler  */
8231a1e1d21SSam Leffler struct mbuf *
824b032f27cSSam Leffler ieee80211_encap(struct ieee80211_node *ni, struct mbuf *m)
8251a1e1d21SSam Leffler {
826b032f27cSSam Leffler #define	WH4(wh)	((struct ieee80211_frame_addr4 *)(wh))
827b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
828b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
8291a1e1d21SSam Leffler 	struct ether_header eh;
8301a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
8318a1b9b6aSSam Leffler 	struct ieee80211_key *key;
8321a1e1d21SSam Leffler 	struct llc *llc;
833b032f27cSSam Leffler 	int hdrsize, hdrspace, datalen, addqos, txfrag, isff, is4addr;
8341a1e1d21SSam Leffler 
83568e8e04eSSam Leffler 	/*
83668e8e04eSSam Leffler 	 * Copy existing Ethernet header to a safe place.  The
83768e8e04eSSam Leffler 	 * rest of the code assumes it's ok to strip it when
83868e8e04eSSam Leffler 	 * reorganizing state for the final encapsulation.
83968e8e04eSSam Leffler 	 */
8408a1b9b6aSSam Leffler 	KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
841b032f27cSSam Leffler 	ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
8421a1e1d21SSam Leffler 
8438a1b9b6aSSam Leffler 	/*
8448a1b9b6aSSam Leffler 	 * Insure space for additional headers.  First identify
8458a1b9b6aSSam Leffler 	 * transmit key to use in calculating any buffer adjustments
8468a1b9b6aSSam Leffler 	 * required.  This is also used below to do privacy
8478a1b9b6aSSam Leffler 	 * encapsulation work.  Then calculate the 802.11 header
8488a1b9b6aSSam Leffler 	 * size and any padding required by the driver.
8498a1b9b6aSSam Leffler 	 *
8508a1b9b6aSSam Leffler 	 * Note key may be NULL if we fall back to the default
8518a1b9b6aSSam Leffler 	 * transmit key and that is not set.  In that case the
8528a1b9b6aSSam Leffler 	 * buffer may not be expanded as needed by the cipher
8538a1b9b6aSSam Leffler 	 * routines, but they will/should discard it.
8548a1b9b6aSSam Leffler 	 */
855b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
856b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA ||
857b032f27cSSam Leffler 		    !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
858b032f27cSSam Leffler 		    (vap->iv_opmode == IEEE80211_M_WDS &&
859b032f27cSSam Leffler 		     (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
860b032f27cSSam Leffler 			key = ieee80211_crypto_getucastkey(vap, ni);
8615e923d2eSSam Leffler 		else
862b032f27cSSam Leffler 			key = ieee80211_crypto_getmcastkey(vap, ni);
863b032f27cSSam Leffler 		if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
864b032f27cSSam Leffler 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
865b032f27cSSam Leffler 			    eh.ether_dhost,
866b032f27cSSam Leffler 			    "no default transmit key (%s) deftxkey %u",
867b032f27cSSam Leffler 			    __func__, vap->iv_def_txkey);
868b032f27cSSam Leffler 			vap->iv_stats.is_tx_nodefkey++;
869d72c7253SSam Leffler 			goto bad;
8708a1b9b6aSSam Leffler 		}
8718a1b9b6aSSam Leffler 	} else
8728a1b9b6aSSam Leffler 		key = NULL;
8735e923d2eSSam Leffler 	/*
8745e923d2eSSam Leffler 	 * XXX Some ap's don't handle QoS-encapsulated EAPOL
8755e923d2eSSam Leffler 	 * frames so suppress use.  This may be an issue if other
8765e923d2eSSam Leffler 	 * ap's require all data frames to be QoS-encapsulated
8775e923d2eSSam Leffler 	 * once negotiated in which case we'll need to make this
8785e923d2eSSam Leffler 	 * configurable.
8795e923d2eSSam Leffler 	 */
88068e8e04eSSam Leffler 	addqos = (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) &&
881b032f27cSSam Leffler 		 (m->m_flags & M_EAPOL) == 0;
8825e923d2eSSam Leffler 	if (addqos)
8838a1b9b6aSSam Leffler 		hdrsize = sizeof(struct ieee80211_qosframe);
8848a1b9b6aSSam Leffler 	else
8858a1b9b6aSSam Leffler 		hdrsize = sizeof(struct ieee80211_frame);
886b032f27cSSam Leffler 	/*
887b032f27cSSam Leffler 	 * 4-address frames need to be generated for:
888b032f27cSSam Leffler 	 * o packets sent through a WDS vap (M_WDS || IEEE80211_M_WDS)
889b032f27cSSam Leffler 	 * o packets relayed by a station operating with dynamic WDS
890b032f27cSSam Leffler 	 *   (IEEE80211_M_STA+IEEE80211_F_DWDS and src address)
891b032f27cSSam Leffler 	 */
892b032f27cSSam Leffler 	is4addr = (m->m_flags & M_WDS) ||
893b032f27cSSam Leffler 	    vap->iv_opmode == IEEE80211_M_WDS ||	/* XXX redundant? */
894b032f27cSSam Leffler 	    (vap->iv_opmode == IEEE80211_M_STA &&
895b032f27cSSam Leffler 	     (vap->iv_flags & IEEE80211_F_DWDS) &&
896b032f27cSSam Leffler 	     !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
897b032f27cSSam Leffler 	if (is4addr)
898b032f27cSSam Leffler 		hdrsize += IEEE80211_ADDR_LEN;
899b032f27cSSam Leffler 	/*
900b032f27cSSam Leffler 	 * Honor driver DATAPAD requirement.
901b032f27cSSam Leffler 	 */
9028a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
903b032f27cSSam Leffler 		hdrspace = roundup(hdrsize, sizeof(uint32_t));
904b032f27cSSam Leffler 	else
905b032f27cSSam Leffler 		hdrspace = hdrsize;
90668e8e04eSSam Leffler 
90768e8e04eSSam Leffler 	if ((isff = m->m_flags & M_FF) != 0) {
90868e8e04eSSam Leffler 		struct mbuf *m2;
90968e8e04eSSam Leffler 		struct ether_header eh2;
91068e8e04eSSam Leffler 
91168e8e04eSSam Leffler 		/*
91268e8e04eSSam Leffler 		 * Fast frame encapsulation.  There must be two packets
91368e8e04eSSam Leffler 		 * chained with m_nextpkt.  We do header adjustment for
91468e8e04eSSam Leffler 		 * each, add the tunnel encapsulation, and then concatenate
91568e8e04eSSam Leffler 		 * the mbuf chains to form a single frame for transmission.
91668e8e04eSSam Leffler 		 */
91768e8e04eSSam Leffler 		m2 = m->m_nextpkt;
91868e8e04eSSam Leffler 		if (m2 == NULL) {
919b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
92068e8e04eSSam Leffler 				"%s: only one frame\n", __func__);
92168e8e04eSSam Leffler 			goto bad;
92268e8e04eSSam Leffler 		}
92368e8e04eSSam Leffler 		m->m_nextpkt = NULL;
92468e8e04eSSam Leffler 		/*
92568e8e04eSSam Leffler 		 * Include fast frame headers in adjusting header
92668e8e04eSSam Leffler 		 * layout; this allocates space according to what
92768e8e04eSSam Leffler 		 * ieee80211_encap_fastframe will do.
92868e8e04eSSam Leffler 		 */
929b032f27cSSam Leffler 		m = ieee80211_mbuf_adjust(vap,
930b032f27cSSam Leffler 			hdrspace + sizeof(struct llc) + sizeof(uint32_t) + 2 +
93168e8e04eSSam Leffler 			    sizeof(struct ether_header),
93268e8e04eSSam Leffler 			key, m);
93368e8e04eSSam Leffler 		if (m == NULL) {
93468e8e04eSSam Leffler 			/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
93568e8e04eSSam Leffler 			m_freem(m2);
93668e8e04eSSam Leffler 			goto bad;
93768e8e04eSSam Leffler 		}
93868e8e04eSSam Leffler 		/*
93968e8e04eSSam Leffler 		 * Copy second frame's Ethernet header out of line
94068e8e04eSSam Leffler 		 * and adjust for encapsulation headers.  Note that
94168e8e04eSSam Leffler 		 * we make room for padding in case there isn't room
94268e8e04eSSam Leffler 		 * at the end of first frame.
94368e8e04eSSam Leffler 		 */
94468e8e04eSSam Leffler 		KASSERT(m2->m_len >= sizeof(eh2), ("no ethernet header!"));
945b032f27cSSam Leffler 		ETHER_HEADER_COPY(&eh2, mtod(m2, caddr_t));
946b032f27cSSam Leffler 		m2 = ieee80211_mbuf_adjust(vap,
94768e8e04eSSam Leffler 			ATH_FF_MAX_HDR_PAD + sizeof(struct ether_header),
94868e8e04eSSam Leffler 			NULL, m2);
94968e8e04eSSam Leffler 		if (m2 == NULL) {
95068e8e04eSSam Leffler 			/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
95168e8e04eSSam Leffler 			goto bad;
95268e8e04eSSam Leffler 		}
953b032f27cSSam Leffler 		m = ieee80211_encap_fastframe(vap, m, &eh, m2, &eh2);
95468e8e04eSSam Leffler 		if (m == NULL)
95568e8e04eSSam Leffler 			goto bad;
95668e8e04eSSam Leffler 	} else {
95768e8e04eSSam Leffler 		/*
95868e8e04eSSam Leffler 		 * Normal frame.
95968e8e04eSSam Leffler 		 */
960b032f27cSSam Leffler 		m = ieee80211_mbuf_adjust(vap, hdrspace, key, m);
9618a1b9b6aSSam Leffler 		if (m == NULL) {
9628a1b9b6aSSam Leffler 			/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
9630a915fadSSam Leffler 			goto bad;
9640a915fadSSam Leffler 		}
96568e8e04eSSam Leffler 		/* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
966ab96db10SSam Leffler 		m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
9671a1e1d21SSam Leffler 		llc = mtod(m, struct llc *);
9681a1e1d21SSam Leffler 		llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
9691a1e1d21SSam Leffler 		llc->llc_control = LLC_UI;
9701a1e1d21SSam Leffler 		llc->llc_snap.org_code[0] = 0;
9711a1e1d21SSam Leffler 		llc->llc_snap.org_code[1] = 0;
9721a1e1d21SSam Leffler 		llc->llc_snap.org_code[2] = 0;
9731a1e1d21SSam Leffler 		llc->llc_snap.ether_type = eh.ether_type;
97468e8e04eSSam Leffler 	}
9758a1b9b6aSSam Leffler 	datalen = m->m_pkthdr.len;		/* NB: w/o 802.11 header */
9768a1b9b6aSSam Leffler 
977b032f27cSSam Leffler 	M_PREPEND(m, hdrspace, M_DONTWAIT);
9781be50176SSam Leffler 	if (m == NULL) {
979b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
9800a915fadSSam Leffler 		goto bad;
9811be50176SSam Leffler 	}
9821a1e1d21SSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
9831a1e1d21SSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
98468e8e04eSSam Leffler 	*(uint16_t *)wh->i_dur = 0;
985b032f27cSSam Leffler 	if (is4addr) {
986b032f27cSSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
987b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
988b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
989b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
990b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
991b032f27cSSam Leffler 	} else switch (vap->iv_opmode) {
9921a1e1d21SSam Leffler 	case IEEE80211_M_STA:
9931a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
9941a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
9951a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
9961a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
9971a1e1d21SSam Leffler 		break;
9981a1e1d21SSam Leffler 	case IEEE80211_M_IBSS:
9991a1e1d21SSam Leffler 	case IEEE80211_M_AHDEMO:
10001a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
10011a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
10021a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1003a8b16e87SSam Leffler 		/*
1004b032f27cSSam Leffler 		 * NB: always use the bssid from iv_bss as the
1005a8b16e87SSam Leffler 		 *     neighbor's may be stale after an ibss merge
1006a8b16e87SSam Leffler 		 */
1007b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
10081a1e1d21SSam Leffler 		break;
10091a1e1d21SSam Leffler 	case IEEE80211_M_HOSTAP:
10101a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
10111a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
10121a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
10131a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
10141a1e1d21SSam Leffler 		break;
10152bbe529dSSam Leffler 	case IEEE80211_M_MONITOR:
1016b032f27cSSam Leffler 	case IEEE80211_M_WDS:		/* NB: is4addr should always be true */
10170a915fadSSam Leffler 		goto bad;
10181a1e1d21SSam Leffler 	}
1019bc5627d9SSam Leffler 	if (m->m_flags & M_MORE_DATA)
1020bc5627d9SSam Leffler 		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
10215e923d2eSSam Leffler 	if (addqos) {
1022b032f27cSSam Leffler 		uint8_t *qos;
10238a1b9b6aSSam Leffler 		int ac, tid;
10248a1b9b6aSSam Leffler 
1025b032f27cSSam Leffler 		if (is4addr) {
1026b032f27cSSam Leffler 			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1027b032f27cSSam Leffler 		} else
1028b032f27cSSam Leffler 			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
10298a1b9b6aSSam Leffler 		ac = M_WME_GETAC(m);
10308a1b9b6aSSam Leffler 		/* map from access class/queue to 11e header priorty value */
10318a1b9b6aSSam Leffler 		tid = WME_AC_TO_TID(ac);
1032b032f27cSSam Leffler 		qos[0] = tid & IEEE80211_QOS_TID;
103368e8e04eSSam Leffler 		/*
103468e8e04eSSam Leffler 		 * Check if A-MPDU tx aggregation is setup or if we
103568e8e04eSSam Leffler 		 * should try to enable it.  The sta must be associated
1036b032f27cSSam Leffler 		 * with HT and A-MPDU enabled for use.  When the policy
1037b032f27cSSam Leffler 		 * routine decides we should enable A-MPDU we issue an
1038b032f27cSSam Leffler 		 * ADDBA request and wait for a reply.  The frame being
1039b032f27cSSam Leffler 		 * encapsulated will go out w/o using A-MPDU, or possibly
1040b032f27cSSam Leffler 		 * it might be collected by the driver and held/retransmit.
1041b032f27cSSam Leffler 		 * The default ic_ampdu_enable routine handles staggering
1042b032f27cSSam Leffler 		 * ADDBA requests in case the receiver NAK's us or we are
1043b032f27cSSam Leffler 		 * otherwise unable to establish a BA stream.
104468e8e04eSSam Leffler 		 */
10451b6167d2SSam Leffler 		if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
1046b032f27cSSam Leffler 		    (vap->iv_flags_ext & IEEE80211_FEXT_AMPDU_TX)) {
104768e8e04eSSam Leffler 			struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[ac];
104868e8e04eSSam Leffler 
1049b032f27cSSam Leffler 			ieee80211_txampdu_count_packet(tap);
105068e8e04eSSam Leffler 			if (IEEE80211_AMPDU_RUNNING(tap)) {
105168e8e04eSSam Leffler 				/*
105268e8e04eSSam Leffler 				 * Operational, mark frame for aggregation.
105345f856e3SSam Leffler 				 *
105445f856e3SSam Leffler 				 * NB: We support only immediate BA's for
105545f856e3SSam Leffler 				 * AMPDU which means we set the QoS control
105645f856e3SSam Leffler 				 * field to "normal ack" (0) to get "implicit
105745f856e3SSam Leffler 				 * block ack" behaviour.
105868e8e04eSSam Leffler 				 */
105945f856e3SSam Leffler 				m->m_flags |= M_AMPDU_MPDU;
1060b032f27cSSam Leffler 			} else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
1061b032f27cSSam Leffler 			    ic->ic_ampdu_enable(ni, tap)) {
106268e8e04eSSam Leffler 				/*
106368e8e04eSSam Leffler 				 * Not negotiated yet, request service.
106468e8e04eSSam Leffler 				 */
106568e8e04eSSam Leffler 				ieee80211_ampdu_request(ni, tap);
106668e8e04eSSam Leffler 			}
106768e8e04eSSam Leffler 		}
106868e8e04eSSam Leffler 		/* XXX works even when BA marked above */
10698a1b9b6aSSam Leffler 		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
1070b032f27cSSam Leffler 			qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1071b032f27cSSam Leffler 		qos[1] = 0;
1072b032f27cSSam Leffler 		wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
10738a1b9b6aSSam Leffler 
107445f856e3SSam Leffler 		if ((m->m_flags & M_AMPDU_MPDU) == 0) {
107545f856e3SSam Leffler 			/*
107645f856e3SSam Leffler 			 * NB: don't assign a sequence # to potential
107745f856e3SSam Leffler 			 * aggregates; we expect this happens at the
107845f856e3SSam Leffler 			 * point the frame comes off any aggregation q
107945f856e3SSam Leffler 			 * as otherwise we may introduce holes in the
108045f856e3SSam Leffler 			 * BA sequence space and/or make window accouting
108145f856e3SSam Leffler 			 * more difficult.
108245f856e3SSam Leffler 			 *
108345f856e3SSam Leffler 			 * XXX may want to control this with a driver
108445f856e3SSam Leffler 			 * capability; this may also change when we pull
108545f856e3SSam Leffler 			 * aggregation up into net80211
108645f856e3SSam Leffler 			 */
108768e8e04eSSam Leffler 			*(uint16_t *)wh->i_seq =
10888a1b9b6aSSam Leffler 			    htole16(ni->ni_txseqs[tid] << IEEE80211_SEQ_SEQ_SHIFT);
10898a1b9b6aSSam Leffler 			ni->ni_txseqs[tid]++;
109045f856e3SSam Leffler 		}
109145f856e3SSam Leffler 		ni->ni_txseqs[tid]++;
10928a1b9b6aSSam Leffler 	} else {
109368e8e04eSSam Leffler 		*(uint16_t *)wh->i_seq =
1094801df4a5SSam Leffler 		    htole16(ni->ni_txseqs[IEEE80211_NONQOS_TID] << IEEE80211_SEQ_SEQ_SHIFT);
1095801df4a5SSam Leffler 		ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
10968a1b9b6aSSam Leffler 	}
109768e8e04eSSam Leffler 	/* check if xmit fragmentation is required */
1098b032f27cSSam Leffler 	txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
109968e8e04eSSam Leffler 	    !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1100b032f27cSSam Leffler 	    (vap->iv_caps & IEEE80211_C_TXFRAG) &&
110168e8e04eSSam Leffler 	    !isff);		/* NB: don't fragment ff's */
11025e923d2eSSam Leffler 	if (key != NULL) {
11035e923d2eSSam Leffler 		/*
11045e923d2eSSam Leffler 		 * IEEE 802.1X: send EAPOL frames always in the clear.
11055e923d2eSSam Leffler 		 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
11065e923d2eSSam Leffler 		 */
1107b032f27cSSam Leffler 		if ((m->m_flags & M_EAPOL) == 0 ||
1108b032f27cSSam Leffler 		    ((vap->iv_flags & IEEE80211_F_WPA) &&
1109b032f27cSSam Leffler 		     (vap->iv_opmode == IEEE80211_M_STA ?
1110cda15ce1SSam Leffler 		      !IEEE80211_KEY_UNDEFINED(key) :
1111cda15ce1SSam Leffler 		      !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
11125e923d2eSSam Leffler 			wh->i_fc[1] |= IEEE80211_FC1_WEP;
1113b032f27cSSam Leffler 			if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1114b032f27cSSam Leffler 				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1115b032f27cSSam Leffler 				    eh.ether_dhost,
1116b032f27cSSam Leffler 				    "%s", "enmic failed, discard frame");
1117b032f27cSSam Leffler 				vap->iv_stats.is_crypto_enmicfail++;
11185e923d2eSSam Leffler 				goto bad;
11195e923d2eSSam Leffler 			}
11205e923d2eSSam Leffler 		}
11215e923d2eSSam Leffler 	}
1122b032f27cSSam Leffler 	if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1123b032f27cSSam Leffler 	    key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
112468e8e04eSSam Leffler 		goto bad;
11258a1b9b6aSSam Leffler 
11268a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_data);
1127b6e9b119SSam Leffler 	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1128b6e9b119SSam Leffler 		IEEE80211_NODE_STAT(ni, tx_mcast);
1129b6e9b119SSam Leffler 	else
1130b6e9b119SSam Leffler 		IEEE80211_NODE_STAT(ni, tx_ucast);
11318a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
11328a1b9b6aSSam Leffler 
1133b032f27cSSam Leffler 	/* XXX fragmented frames not handled */
1134b032f27cSSam Leffler 	if (bpf_peers_present(vap->iv_rawbpf))
1135b032f27cSSam Leffler 		bpf_mtap(vap->iv_rawbpf, m);
1136b032f27cSSam Leffler 
11371a1e1d21SSam Leffler 	return m;
11380a915fadSSam Leffler bad:
11390a915fadSSam Leffler 	if (m != NULL)
11400a915fadSSam Leffler 		m_freem(m);
11410a915fadSSam Leffler 	return NULL;
1142b032f27cSSam Leffler #undef WH4
11431a1e1d21SSam Leffler }
11441a1e1d21SSam Leffler 
11451a1e1d21SSam Leffler /*
114668e8e04eSSam Leffler  * Do Ethernet-LLC encapsulation for each payload in a fast frame
114768e8e04eSSam Leffler  * tunnel encapsulation.  The frame is assumed to have an Ethernet
114868e8e04eSSam Leffler  * header at the front that must be stripped before prepending the
114968e8e04eSSam Leffler  * LLC followed by the Ethernet header passed in (with an Ethernet
115068e8e04eSSam Leffler  * type that specifies the payload size).
115168e8e04eSSam Leffler  */
115268e8e04eSSam Leffler static struct mbuf *
1153b032f27cSSam Leffler ieee80211_encap1(struct ieee80211vap *vap, struct mbuf *m,
115468e8e04eSSam Leffler 	const struct ether_header *eh)
115568e8e04eSSam Leffler {
115668e8e04eSSam Leffler 	struct llc *llc;
115768e8e04eSSam Leffler 	uint16_t payload;
115868e8e04eSSam Leffler 
115968e8e04eSSam Leffler 	/* XXX optimize by combining m_adj+M_PREPEND */
116068e8e04eSSam Leffler 	m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
116168e8e04eSSam Leffler 	llc = mtod(m, struct llc *);
116268e8e04eSSam Leffler 	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
116368e8e04eSSam Leffler 	llc->llc_control = LLC_UI;
116468e8e04eSSam Leffler 	llc->llc_snap.org_code[0] = 0;
116568e8e04eSSam Leffler 	llc->llc_snap.org_code[1] = 0;
116668e8e04eSSam Leffler 	llc->llc_snap.org_code[2] = 0;
116768e8e04eSSam Leffler 	llc->llc_snap.ether_type = eh->ether_type;
116868e8e04eSSam Leffler 	payload = m->m_pkthdr.len;		/* NB: w/o Ethernet header */
116968e8e04eSSam Leffler 
117068e8e04eSSam Leffler 	M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT);
117168e8e04eSSam Leffler 	if (m == NULL) {		/* XXX cannot happen */
1172b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
117368e8e04eSSam Leffler 			"%s: no space for ether_header\n", __func__);
1174b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
117568e8e04eSSam Leffler 		return NULL;
117668e8e04eSSam Leffler 	}
117768e8e04eSSam Leffler 	ETHER_HEADER_COPY(mtod(m, void *), eh);
117868e8e04eSSam Leffler 	mtod(m, struct ether_header *)->ether_type = htons(payload);
117968e8e04eSSam Leffler 	return m;
118068e8e04eSSam Leffler }
118168e8e04eSSam Leffler 
118268e8e04eSSam Leffler /*
118368e8e04eSSam Leffler  * Do fast frame tunnel encapsulation.  The two frames and
118468e8e04eSSam Leffler  * Ethernet headers are supplied.  The caller is assumed to
118568e8e04eSSam Leffler  * have arrange for space in the mbuf chains for encapsulating
118668e8e04eSSam Leffler  * headers (to avoid major mbuf fragmentation).
118768e8e04eSSam Leffler  *
118868e8e04eSSam Leffler  * The encapsulated frame is returned or NULL if there is a
118968e8e04eSSam Leffler  * problem (should not happen).
119068e8e04eSSam Leffler  */
119168e8e04eSSam Leffler static struct mbuf *
1192b032f27cSSam Leffler ieee80211_encap_fastframe(struct ieee80211vap *vap,
119368e8e04eSSam Leffler 	struct mbuf *m1, const struct ether_header *eh1,
119468e8e04eSSam Leffler 	struct mbuf *m2, const struct ether_header *eh2)
119568e8e04eSSam Leffler {
119668e8e04eSSam Leffler 	struct llc *llc;
119768e8e04eSSam Leffler 	struct mbuf *m;
119868e8e04eSSam Leffler 	int pad;
119968e8e04eSSam Leffler 
120068e8e04eSSam Leffler 	/*
120168e8e04eSSam Leffler 	 * First, each frame gets a standard encapsulation.
120268e8e04eSSam Leffler 	 */
1203b032f27cSSam Leffler 	m1 = ieee80211_encap1(vap, m1, eh1);
120468e8e04eSSam Leffler 	if (m1 == NULL) {
120568e8e04eSSam Leffler 		m_freem(m2);
120668e8e04eSSam Leffler 		return NULL;
120768e8e04eSSam Leffler 	}
1208b032f27cSSam Leffler 	m2 = ieee80211_encap1(vap, m2, eh2);
120968e8e04eSSam Leffler 	if (m2 == NULL) {
121068e8e04eSSam Leffler 		m_freem(m1);
121168e8e04eSSam Leffler 		return NULL;
121268e8e04eSSam Leffler 	}
121368e8e04eSSam Leffler 
121468e8e04eSSam Leffler 	/*
121568e8e04eSSam Leffler 	 * Pad leading frame to a 4-byte boundary.  If there
121668e8e04eSSam Leffler 	 * is space at the end of the first frame, put it
121768e8e04eSSam Leffler 	 * there; otherwise prepend to the front of the second
121868e8e04eSSam Leffler 	 * frame.  We know doing the second will always work
121968e8e04eSSam Leffler 	 * because we reserve space above.  We prefer appending
122068e8e04eSSam Leffler 	 * as this typically has better DMA alignment properties.
122168e8e04eSSam Leffler 	 */
122268e8e04eSSam Leffler 	for (m = m1; m->m_next != NULL; m = m->m_next)
122368e8e04eSSam Leffler 		;
122468e8e04eSSam Leffler 	pad = roundup2(m1->m_pkthdr.len, 4) - m1->m_pkthdr.len;
122568e8e04eSSam Leffler 	if (pad) {
122668e8e04eSSam Leffler 		if (M_TRAILINGSPACE(m) < pad) {		/* prepend to second */
122768e8e04eSSam Leffler 			m2->m_data -= pad;
122868e8e04eSSam Leffler 			m2->m_len += pad;
122968e8e04eSSam Leffler 			m2->m_pkthdr.len += pad;
123068e8e04eSSam Leffler 		} else {				/* append to first */
123168e8e04eSSam Leffler 			m->m_len += pad;
123268e8e04eSSam Leffler 			m1->m_pkthdr.len += pad;
123368e8e04eSSam Leffler 		}
123468e8e04eSSam Leffler 	}
123568e8e04eSSam Leffler 
123668e8e04eSSam Leffler 	/*
123768e8e04eSSam Leffler 	 * Now, stick 'em together and prepend the tunnel headers;
123868e8e04eSSam Leffler 	 * first the Atheros tunnel header (all zero for now) and
123968e8e04eSSam Leffler 	 * then a special fast frame LLC.
124068e8e04eSSam Leffler 	 *
124168e8e04eSSam Leffler 	 * XXX optimize by prepending together
124268e8e04eSSam Leffler 	 */
124368e8e04eSSam Leffler 	m->m_next = m2;			/* NB: last mbuf from above */
124468e8e04eSSam Leffler 	m1->m_pkthdr.len += m2->m_pkthdr.len;
124568e8e04eSSam Leffler 	M_PREPEND(m1, sizeof(uint32_t)+2, M_DONTWAIT);
124668e8e04eSSam Leffler 	if (m1 == NULL) {		/* XXX cannot happen */
1247b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
124868e8e04eSSam Leffler 			"%s: no space for tunnel header\n", __func__);
1249b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
125068e8e04eSSam Leffler 		return NULL;
125168e8e04eSSam Leffler 	}
125268e8e04eSSam Leffler 	memset(mtod(m1, void *), 0, sizeof(uint32_t)+2);
125368e8e04eSSam Leffler 
125468e8e04eSSam Leffler 	M_PREPEND(m1, sizeof(struct llc), M_DONTWAIT);
125568e8e04eSSam Leffler 	if (m1 == NULL) {		/* XXX cannot happen */
1256b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
125768e8e04eSSam Leffler 			"%s: no space for llc header\n", __func__);
1258b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
125968e8e04eSSam Leffler 		return NULL;
126068e8e04eSSam Leffler 	}
126168e8e04eSSam Leffler 	llc = mtod(m1, struct llc *);
126268e8e04eSSam Leffler 	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
126368e8e04eSSam Leffler 	llc->llc_control = LLC_UI;
126468e8e04eSSam Leffler 	llc->llc_snap.org_code[0] = ATH_FF_SNAP_ORGCODE_0;
126568e8e04eSSam Leffler 	llc->llc_snap.org_code[1] = ATH_FF_SNAP_ORGCODE_1;
126668e8e04eSSam Leffler 	llc->llc_snap.org_code[2] = ATH_FF_SNAP_ORGCODE_2;
126768e8e04eSSam Leffler 	llc->llc_snap.ether_type = htons(ATH_FF_ETH_TYPE);
126868e8e04eSSam Leffler 
1269b032f27cSSam Leffler 	vap->iv_stats.is_ff_encap++;
127068e8e04eSSam Leffler 
127168e8e04eSSam Leffler 	return m1;
127268e8e04eSSam Leffler }
127368e8e04eSSam Leffler 
127468e8e04eSSam Leffler /*
127568e8e04eSSam Leffler  * Fragment the frame according to the specified mtu.
127668e8e04eSSam Leffler  * The size of the 802.11 header (w/o padding) is provided
127768e8e04eSSam Leffler  * so we don't need to recalculate it.  We create a new
127868e8e04eSSam Leffler  * mbuf for each fragment and chain it through m_nextpkt;
127968e8e04eSSam Leffler  * we might be able to optimize this by reusing the original
128068e8e04eSSam Leffler  * packet's mbufs but that is significantly more complicated.
128168e8e04eSSam Leffler  */
128268e8e04eSSam Leffler static int
1283b032f27cSSam Leffler ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
128468e8e04eSSam Leffler 	u_int hdrsize, u_int ciphdrsize, u_int mtu)
128568e8e04eSSam Leffler {
128668e8e04eSSam Leffler 	struct ieee80211_frame *wh, *whf;
128768e8e04eSSam Leffler 	struct mbuf *m, *prev, *next;
128868e8e04eSSam Leffler 	u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
128968e8e04eSSam Leffler 
129068e8e04eSSam Leffler 	KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
129168e8e04eSSam Leffler 	KASSERT(m0->m_pkthdr.len > mtu,
129268e8e04eSSam Leffler 		("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
129368e8e04eSSam Leffler 
129468e8e04eSSam Leffler 	wh = mtod(m0, struct ieee80211_frame *);
129568e8e04eSSam Leffler 	/* NB: mark the first frag; it will be propagated below */
129668e8e04eSSam Leffler 	wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
129768e8e04eSSam Leffler 	totalhdrsize = hdrsize + ciphdrsize;
129868e8e04eSSam Leffler 	fragno = 1;
129968e8e04eSSam Leffler 	off = mtu - ciphdrsize;
130068e8e04eSSam Leffler 	remainder = m0->m_pkthdr.len - off;
130168e8e04eSSam Leffler 	prev = m0;
130268e8e04eSSam Leffler 	do {
130368e8e04eSSam Leffler 		fragsize = totalhdrsize + remainder;
130468e8e04eSSam Leffler 		if (fragsize > mtu)
130568e8e04eSSam Leffler 			fragsize = mtu;
1306b032f27cSSam Leffler 		/* XXX fragsize can be >2048! */
130768e8e04eSSam Leffler 		KASSERT(fragsize < MCLBYTES,
130868e8e04eSSam Leffler 			("fragment size %u too big!", fragsize));
130968e8e04eSSam Leffler 		if (fragsize > MHLEN)
131068e8e04eSSam Leffler 			m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
131168e8e04eSSam Leffler 		else
131268e8e04eSSam Leffler 			m = m_gethdr(M_DONTWAIT, MT_DATA);
131368e8e04eSSam Leffler 		if (m == NULL)
131468e8e04eSSam Leffler 			goto bad;
131568e8e04eSSam Leffler 		/* leave room to prepend any cipher header */
131668e8e04eSSam Leffler 		m_align(m, fragsize - ciphdrsize);
131768e8e04eSSam Leffler 
131868e8e04eSSam Leffler 		/*
131968e8e04eSSam Leffler 		 * Form the header in the fragment.  Note that since
132068e8e04eSSam Leffler 		 * we mark the first fragment with the MORE_FRAG bit
132168e8e04eSSam Leffler 		 * it automatically is propagated to each fragment; we
132268e8e04eSSam Leffler 		 * need only clear it on the last fragment (done below).
132368e8e04eSSam Leffler 		 */
132468e8e04eSSam Leffler 		whf = mtod(m, struct ieee80211_frame *);
132568e8e04eSSam Leffler 		memcpy(whf, wh, hdrsize);
132668e8e04eSSam Leffler 		*(uint16_t *)&whf->i_seq[0] |= htole16(
132768e8e04eSSam Leffler 			(fragno & IEEE80211_SEQ_FRAG_MASK) <<
132868e8e04eSSam Leffler 				IEEE80211_SEQ_FRAG_SHIFT);
132968e8e04eSSam Leffler 		fragno++;
133068e8e04eSSam Leffler 
133168e8e04eSSam Leffler 		payload = fragsize - totalhdrsize;
133268e8e04eSSam Leffler 		/* NB: destination is known to be contiguous */
133368e8e04eSSam Leffler 		m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrsize);
133468e8e04eSSam Leffler 		m->m_len = hdrsize + payload;
133568e8e04eSSam Leffler 		m->m_pkthdr.len = hdrsize + payload;
133668e8e04eSSam Leffler 		m->m_flags |= M_FRAG;
133768e8e04eSSam Leffler 
133868e8e04eSSam Leffler 		/* chain up the fragment */
133968e8e04eSSam Leffler 		prev->m_nextpkt = m;
134068e8e04eSSam Leffler 		prev = m;
134168e8e04eSSam Leffler 
134268e8e04eSSam Leffler 		/* deduct fragment just formed */
134368e8e04eSSam Leffler 		remainder -= payload;
134468e8e04eSSam Leffler 		off += payload;
134568e8e04eSSam Leffler 	} while (remainder != 0);
134668e8e04eSSam Leffler 	whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
134768e8e04eSSam Leffler 
134868e8e04eSSam Leffler 	/* strip first mbuf now that everything has been copied */
134968e8e04eSSam Leffler 	m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
135068e8e04eSSam Leffler 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
135168e8e04eSSam Leffler 
1352b032f27cSSam Leffler 	vap->iv_stats.is_tx_fragframes++;
1353b032f27cSSam Leffler 	vap->iv_stats.is_tx_frags += fragno-1;
135468e8e04eSSam Leffler 
135568e8e04eSSam Leffler 	return 1;
135668e8e04eSSam Leffler bad:
135768e8e04eSSam Leffler 	/* reclaim fragments but leave original frame for caller to free */
135868e8e04eSSam Leffler 	for (m = m0->m_nextpkt; m != NULL; m = next) {
135968e8e04eSSam Leffler 		next = m->m_nextpkt;
136068e8e04eSSam Leffler 		m->m_nextpkt = NULL;		/* XXX paranoid */
136168e8e04eSSam Leffler 		m_freem(m);
136268e8e04eSSam Leffler 	}
136368e8e04eSSam Leffler 	m0->m_nextpkt = NULL;
136468e8e04eSSam Leffler 	return 0;
136568e8e04eSSam Leffler }
136668e8e04eSSam Leffler 
136768e8e04eSSam Leffler /*
13681a1e1d21SSam Leffler  * Add a supported rates element id to a frame.
13691a1e1d21SSam Leffler  */
137068e8e04eSSam Leffler static uint8_t *
137168e8e04eSSam Leffler ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
13721a1e1d21SSam Leffler {
13731a1e1d21SSam Leffler 	int nrates;
13741a1e1d21SSam Leffler 
13751a1e1d21SSam Leffler 	*frm++ = IEEE80211_ELEMID_RATES;
13761a1e1d21SSam Leffler 	nrates = rs->rs_nrates;
13771a1e1d21SSam Leffler 	if (nrates > IEEE80211_RATE_SIZE)
13781a1e1d21SSam Leffler 		nrates = IEEE80211_RATE_SIZE;
13791a1e1d21SSam Leffler 	*frm++ = nrates;
13801a1e1d21SSam Leffler 	memcpy(frm, rs->rs_rates, nrates);
13811a1e1d21SSam Leffler 	return frm + nrates;
13821a1e1d21SSam Leffler }
13831a1e1d21SSam Leffler 
13841a1e1d21SSam Leffler /*
13851a1e1d21SSam Leffler  * Add an extended supported rates element id to a frame.
13861a1e1d21SSam Leffler  */
138768e8e04eSSam Leffler static uint8_t *
138868e8e04eSSam Leffler ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
13891a1e1d21SSam Leffler {
13901a1e1d21SSam Leffler 	/*
13911a1e1d21SSam Leffler 	 * Add an extended supported rates element if operating in 11g mode.
13921a1e1d21SSam Leffler 	 */
13931a1e1d21SSam Leffler 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
13941a1e1d21SSam Leffler 		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
13951a1e1d21SSam Leffler 		*frm++ = IEEE80211_ELEMID_XRATES;
13961a1e1d21SSam Leffler 		*frm++ = nrates;
13971a1e1d21SSam Leffler 		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
13981a1e1d21SSam Leffler 		frm += nrates;
13991a1e1d21SSam Leffler 	}
14001a1e1d21SSam Leffler 	return frm;
14011a1e1d21SSam Leffler }
14021a1e1d21SSam Leffler 
14031a1e1d21SSam Leffler /*
1404b032f27cSSam Leffler  * Add an ssid element to a frame.
14051a1e1d21SSam Leffler  */
140668e8e04eSSam Leffler static uint8_t *
140768e8e04eSSam Leffler ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
14081a1e1d21SSam Leffler {
14091a1e1d21SSam Leffler 	*frm++ = IEEE80211_ELEMID_SSID;
14101a1e1d21SSam Leffler 	*frm++ = len;
14111a1e1d21SSam Leffler 	memcpy(frm, ssid, len);
14121a1e1d21SSam Leffler 	return frm + len;
14131a1e1d21SSam Leffler }
14141a1e1d21SSam Leffler 
14158a1b9b6aSSam Leffler /*
14168a1b9b6aSSam Leffler  * Add an erp element to a frame.
14178a1b9b6aSSam Leffler  */
141868e8e04eSSam Leffler static uint8_t *
141968e8e04eSSam Leffler ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic)
14201a1e1d21SSam Leffler {
142168e8e04eSSam Leffler 	uint8_t erp;
14221a1e1d21SSam Leffler 
14238a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_ERP;
14248a1b9b6aSSam Leffler 	*frm++ = 1;
14258a1b9b6aSSam Leffler 	erp = 0;
14268a1b9b6aSSam Leffler 	if (ic->ic_nonerpsta != 0)
14278a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_NON_ERP_PRESENT;
14288a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_USEPROT)
14298a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_USE_PROTECTION;
14308a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_USEBARKER)
14318a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_LONG_PREAMBLE;
14328a1b9b6aSSam Leffler 	*frm++ = erp;
14338a1b9b6aSSam Leffler 	return frm;
14341a1e1d21SSam Leffler }
14351a1e1d21SSam Leffler 
14368a1b9b6aSSam Leffler /*
1437b032f27cSSam Leffler  * Add a CFParams element to a frame.
14388a1b9b6aSSam Leffler  */
143968e8e04eSSam Leffler static uint8_t *
1440b032f27cSSam Leffler ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
14418a1b9b6aSSam Leffler {
1442b032f27cSSam Leffler #define	ADDSHORT(frm, v) do {			\
1443b032f27cSSam Leffler 	frm[0] = (v) & 0xff;			\
1444b032f27cSSam Leffler 	frm[1] = (v) >> 8;			\
1445b032f27cSSam Leffler 	frm += 2;				\
1446b032f27cSSam Leffler } while (0)
1447b032f27cSSam Leffler 	*frm++ = IEEE80211_ELEMID_CFPARMS;
1448b032f27cSSam Leffler 	*frm++ = 6;
1449b032f27cSSam Leffler 	*frm++ = 0;		/* CFP count */
1450b032f27cSSam Leffler 	*frm++ = 2;		/* CFP period */
1451b032f27cSSam Leffler 	ADDSHORT(frm, 0);	/* CFP MaxDuration (TU) */
1452b032f27cSSam Leffler 	ADDSHORT(frm, 0);	/* CFP CurRemaining (TU) */
14538a1b9b6aSSam Leffler 	return frm;
1454b032f27cSSam Leffler #undef ADDSHORT
1455b032f27cSSam Leffler }
1456b032f27cSSam Leffler 
1457b032f27cSSam Leffler static __inline uint8_t *
1458b032f27cSSam Leffler add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
1459b032f27cSSam Leffler {
1460b032f27cSSam Leffler 	memcpy(frm, ie->ie_data, ie->ie_len);
1461b032f27cSSam Leffler 	return frm + ie->ie_len;
1462b032f27cSSam Leffler }
1463b032f27cSSam Leffler 
1464b032f27cSSam Leffler static __inline uint8_t *
1465b032f27cSSam Leffler add_ie(uint8_t *frm, const uint8_t *ie)
1466b032f27cSSam Leffler {
1467b032f27cSSam Leffler 	memcpy(frm, ie, 2 + ie[1]);
1468b032f27cSSam Leffler 	return frm + 2 + ie[1];
14698a1b9b6aSSam Leffler }
14708a1b9b6aSSam Leffler 
14718a1b9b6aSSam Leffler #define	WME_OUI_BYTES		0x00, 0x50, 0xf2
14728a1b9b6aSSam Leffler /*
14738a1b9b6aSSam Leffler  * Add a WME information element to a frame.
14748a1b9b6aSSam Leffler  */
147568e8e04eSSam Leffler static uint8_t *
147668e8e04eSSam Leffler ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme)
14778a1b9b6aSSam Leffler {
14788a1b9b6aSSam Leffler 	static const struct ieee80211_wme_info info = {
14798a1b9b6aSSam Leffler 		.wme_id		= IEEE80211_ELEMID_VENDOR,
14808a1b9b6aSSam Leffler 		.wme_len	= sizeof(struct ieee80211_wme_info) - 2,
14818a1b9b6aSSam Leffler 		.wme_oui	= { WME_OUI_BYTES },
14828a1b9b6aSSam Leffler 		.wme_type	= WME_OUI_TYPE,
14838a1b9b6aSSam Leffler 		.wme_subtype	= WME_INFO_OUI_SUBTYPE,
14848a1b9b6aSSam Leffler 		.wme_version	= WME_VERSION,
14858a1b9b6aSSam Leffler 		.wme_info	= 0,
14868a1b9b6aSSam Leffler 	};
14878a1b9b6aSSam Leffler 	memcpy(frm, &info, sizeof(info));
14888a1b9b6aSSam Leffler 	return frm + sizeof(info);
14898a1b9b6aSSam Leffler }
14908a1b9b6aSSam Leffler 
14918a1b9b6aSSam Leffler /*
14928a1b9b6aSSam Leffler  * Add a WME parameters element to a frame.
14938a1b9b6aSSam Leffler  */
149468e8e04eSSam Leffler static uint8_t *
149568e8e04eSSam Leffler ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme)
14968a1b9b6aSSam Leffler {
14978a1b9b6aSSam Leffler #define	SM(_v, _f)	(((_v) << _f##_S) & _f)
14988a1b9b6aSSam Leffler #define	ADDSHORT(frm, v) do {			\
14998a1b9b6aSSam Leffler 	frm[0] = (v) & 0xff;			\
15008a1b9b6aSSam Leffler 	frm[1] = (v) >> 8;			\
15018a1b9b6aSSam Leffler 	frm += 2;				\
15028a1b9b6aSSam Leffler } while (0)
15038a1b9b6aSSam Leffler 	/* NB: this works 'cuz a param has an info at the front */
15048a1b9b6aSSam Leffler 	static const struct ieee80211_wme_info param = {
15058a1b9b6aSSam Leffler 		.wme_id		= IEEE80211_ELEMID_VENDOR,
15068a1b9b6aSSam Leffler 		.wme_len	= sizeof(struct ieee80211_wme_param) - 2,
15078a1b9b6aSSam Leffler 		.wme_oui	= { WME_OUI_BYTES },
15088a1b9b6aSSam Leffler 		.wme_type	= WME_OUI_TYPE,
15098a1b9b6aSSam Leffler 		.wme_subtype	= WME_PARAM_OUI_SUBTYPE,
15108a1b9b6aSSam Leffler 		.wme_version	= WME_VERSION,
15118a1b9b6aSSam Leffler 	};
15128a1b9b6aSSam Leffler 	int i;
15138a1b9b6aSSam Leffler 
15148a1b9b6aSSam Leffler 	memcpy(frm, &param, sizeof(param));
15158a1b9b6aSSam Leffler 	frm += __offsetof(struct ieee80211_wme_info, wme_info);
15168a1b9b6aSSam Leffler 	*frm++ = wme->wme_bssChanParams.cap_info;	/* AC info */
15178a1b9b6aSSam Leffler 	*frm++ = 0;					/* reserved field */
15188a1b9b6aSSam Leffler 	for (i = 0; i < WME_NUM_AC; i++) {
15198a1b9b6aSSam Leffler 		const struct wmeParams *ac =
15208a1b9b6aSSam Leffler 		       &wme->wme_bssChanParams.cap_wmeParams[i];
15218a1b9b6aSSam Leffler 		*frm++ = SM(i, WME_PARAM_ACI)
15228a1b9b6aSSam Leffler 		       | SM(ac->wmep_acm, WME_PARAM_ACM)
15238a1b9b6aSSam Leffler 		       | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
15248a1b9b6aSSam Leffler 		       ;
15258a1b9b6aSSam Leffler 		*frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
15268a1b9b6aSSam Leffler 		       | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
15278a1b9b6aSSam Leffler 		       ;
15288a1b9b6aSSam Leffler 		ADDSHORT(frm, ac->wmep_txopLimit);
15298a1b9b6aSSam Leffler 	}
15308a1b9b6aSSam Leffler 	return frm;
15318a1b9b6aSSam Leffler #undef SM
15328a1b9b6aSSam Leffler #undef ADDSHORT
15338a1b9b6aSSam Leffler }
15348a1b9b6aSSam Leffler #undef WME_OUI_BYTES
15358a1b9b6aSSam Leffler 
153668e8e04eSSam Leffler #define	ATH_OUI_BYTES		0x00, 0x03, 0x7f
153768e8e04eSSam Leffler /*
153868e8e04eSSam Leffler  * Add a WME information element to a frame.
153968e8e04eSSam Leffler  */
154068e8e04eSSam Leffler static uint8_t *
154168e8e04eSSam Leffler ieee80211_add_ath(uint8_t *frm, uint8_t caps, uint16_t defkeyix)
154268e8e04eSSam Leffler {
154368e8e04eSSam Leffler 	static const struct ieee80211_ath_ie info = {
154468e8e04eSSam Leffler 		.ath_id		= IEEE80211_ELEMID_VENDOR,
154568e8e04eSSam Leffler 		.ath_len	= sizeof(struct ieee80211_ath_ie) - 2,
154668e8e04eSSam Leffler 		.ath_oui	= { ATH_OUI_BYTES },
154768e8e04eSSam Leffler 		.ath_oui_type	= ATH_OUI_TYPE,
154868e8e04eSSam Leffler 		.ath_oui_subtype= ATH_OUI_SUBTYPE,
154968e8e04eSSam Leffler 		.ath_version	= ATH_OUI_VERSION,
155068e8e04eSSam Leffler 	};
155168e8e04eSSam Leffler 	struct ieee80211_ath_ie *ath = (struct ieee80211_ath_ie *) frm;
155268e8e04eSSam Leffler 
155368e8e04eSSam Leffler 	memcpy(frm, &info, sizeof(info));
155468e8e04eSSam Leffler 	ath->ath_capability = caps;
155568e8e04eSSam Leffler 	ath->ath_defkeyix[0] = (defkeyix & 0xff);
155668e8e04eSSam Leffler 	ath->ath_defkeyix[1] = ((defkeyix >> 8) & 0xff);
155768e8e04eSSam Leffler 	return frm + sizeof(info);
155868e8e04eSSam Leffler }
155968e8e04eSSam Leffler #undef ATH_OUI_BYTES
156068e8e04eSSam Leffler 
15610a915fadSSam Leffler /*
1562b032f27cSSam Leffler  * Add an 11h Power Constraint element to a frame.
1563b032f27cSSam Leffler  */
1564b032f27cSSam Leffler static uint8_t *
1565b032f27cSSam Leffler ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
1566b032f27cSSam Leffler {
1567b032f27cSSam Leffler 	const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
1568b032f27cSSam Leffler 	/* XXX per-vap tx power limit? */
1569b032f27cSSam Leffler 	int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
1570b032f27cSSam Leffler 
1571b032f27cSSam Leffler 	frm[0] = IEEE80211_ELEMID_PWRCNSTR;
1572b032f27cSSam Leffler 	frm[1] = 1;
1573b032f27cSSam Leffler 	frm[2] = c->ic_maxregpower > limit ?  c->ic_maxregpower - limit : 0;
1574b032f27cSSam Leffler 	return frm + 3;
1575b032f27cSSam Leffler }
1576b032f27cSSam Leffler 
1577b032f27cSSam Leffler /*
1578b032f27cSSam Leffler  * Add an 11h Power Capability element to a frame.
1579b032f27cSSam Leffler  */
1580b032f27cSSam Leffler static uint8_t *
1581b032f27cSSam Leffler ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
1582b032f27cSSam Leffler {
1583b032f27cSSam Leffler 	frm[0] = IEEE80211_ELEMID_PWRCAP;
1584b032f27cSSam Leffler 	frm[1] = 2;
1585b032f27cSSam Leffler 	frm[2] = c->ic_minpower;
1586b032f27cSSam Leffler 	frm[3] = c->ic_maxpower;
1587b032f27cSSam Leffler 	return frm + 4;
1588b032f27cSSam Leffler }
1589b032f27cSSam Leffler 
1590b032f27cSSam Leffler /*
1591b032f27cSSam Leffler  * Add an 11h Supported Channels element to a frame.
1592b032f27cSSam Leffler  */
1593b032f27cSSam Leffler static uint8_t *
1594b032f27cSSam Leffler ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
1595b032f27cSSam Leffler {
1596b032f27cSSam Leffler 	static const int ielen = 26;
1597b032f27cSSam Leffler 
1598b032f27cSSam Leffler 	frm[0] = IEEE80211_ELEMID_SUPPCHAN;
1599b032f27cSSam Leffler 	frm[1] = ielen;
1600b032f27cSSam Leffler 	/* XXX not correct */
1601b032f27cSSam Leffler 	memcpy(frm+2, ic->ic_chan_avail, ielen);
1602b032f27cSSam Leffler 	return frm + 2 + ielen;
1603b032f27cSSam Leffler }
1604b032f27cSSam Leffler 
1605b032f27cSSam Leffler /*
1606b032f27cSSam Leffler  * Add an 11h Channel Switch Announcement element to a frame.
1607b032f27cSSam Leffler  * Note that we use the per-vap CSA count to adjust the global
1608b032f27cSSam Leffler  * counter so we can use this routine to form probe response
1609b032f27cSSam Leffler  * frames and get the current count.
1610b032f27cSSam Leffler  */
1611b032f27cSSam Leffler static uint8_t *
1612b032f27cSSam Leffler ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
1613b032f27cSSam Leffler {
1614b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1615b032f27cSSam Leffler 	struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
1616b032f27cSSam Leffler 
1617b032f27cSSam Leffler 	csa->csa_ie = IEEE80211_ELEMID_CHANSWITCHANN;
1618b032f27cSSam Leffler 	csa->csa_len = 3;
1619b032f27cSSam Leffler 	csa->csa_mode = 1;		/* XXX force quiet on channel */
1620b032f27cSSam Leffler 	csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
1621b032f27cSSam Leffler 	csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
1622b032f27cSSam Leffler 	return frm + sizeof(*csa);
1623b032f27cSSam Leffler }
1624b032f27cSSam Leffler 
1625b032f27cSSam Leffler /*
1626b032f27cSSam Leffler  * Add an 11h country information element to a frame.
1627b032f27cSSam Leffler  */
1628b032f27cSSam Leffler static uint8_t *
1629b032f27cSSam Leffler ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
1630b032f27cSSam Leffler {
1631b032f27cSSam Leffler 
1632b032f27cSSam Leffler 	if (ic->ic_countryie == NULL ||
1633b032f27cSSam Leffler 	    ic->ic_countryie_chan != ic->ic_bsschan) {
1634b032f27cSSam Leffler 		/*
1635b032f27cSSam Leffler 		 * Handle lazy construction of ie.  This is done on
1636b032f27cSSam Leffler 		 * first use and after a channel change that requires
1637b032f27cSSam Leffler 		 * re-calculation.
1638b032f27cSSam Leffler 		 */
1639b032f27cSSam Leffler 		if (ic->ic_countryie != NULL)
1640b032f27cSSam Leffler 			free(ic->ic_countryie, M_80211_NODE_IE);
1641b032f27cSSam Leffler 		ic->ic_countryie = ieee80211_alloc_countryie(ic);
1642b032f27cSSam Leffler 		if (ic->ic_countryie == NULL)
1643b032f27cSSam Leffler 			return frm;
1644b032f27cSSam Leffler 		ic->ic_countryie_chan = ic->ic_bsschan;
1645b032f27cSSam Leffler 	}
1646b032f27cSSam Leffler 	return add_appie(frm, ic->ic_countryie);
1647b032f27cSSam Leffler }
1648b032f27cSSam Leffler 
1649b032f27cSSam Leffler /*
1650af8418dcSSam Leffler  * Send a probe request frame with the specified ssid
1651af8418dcSSam Leffler  * and any optional information element data.
1652af8418dcSSam Leffler  */
1653af8418dcSSam Leffler int
1654af8418dcSSam Leffler ieee80211_send_probereq(struct ieee80211_node *ni,
165568e8e04eSSam Leffler 	const uint8_t sa[IEEE80211_ADDR_LEN],
165668e8e04eSSam Leffler 	const uint8_t da[IEEE80211_ADDR_LEN],
165768e8e04eSSam Leffler 	const uint8_t bssid[IEEE80211_ADDR_LEN],
1658b032f27cSSam Leffler 	const uint8_t *ssid, size_t ssidlen)
1659af8418dcSSam Leffler {
1660b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1661af8418dcSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1662af8418dcSSam Leffler 	struct ieee80211_frame *wh;
166341b3c790SSam Leffler 	const struct ieee80211_rateset *rs;
1664af8418dcSSam Leffler 	struct mbuf *m;
166568e8e04eSSam Leffler 	uint8_t *frm;
1666af8418dcSSam Leffler 
1667b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
1668b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
1669b032f27cSSam Leffler 		    "block %s frame in CAC state", "probe request");
1670b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
1671b032f27cSSam Leffler 		return EIO;		/* XXX */
1672b032f27cSSam Leffler 	}
1673b032f27cSSam Leffler 
1674af8418dcSSam Leffler 	/*
1675af8418dcSSam Leffler 	 * Hold a reference on the node so it doesn't go away until after
1676af8418dcSSam Leffler 	 * the xmit is complete all the way in the driver.  On error we
1677af8418dcSSam Leffler 	 * will remove our reference.
1678af8418dcSSam Leffler 	 */
1679b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1680af8418dcSSam Leffler 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1681af8418dcSSam Leffler 		__func__, __LINE__,
1682af8418dcSSam Leffler 		ni, ether_sprintf(ni->ni_macaddr),
1683af8418dcSSam Leffler 		ieee80211_node_refcnt(ni)+1);
1684af8418dcSSam Leffler 	ieee80211_ref_node(ni);
1685af8418dcSSam Leffler 
1686af8418dcSSam Leffler 	/*
1687af8418dcSSam Leffler 	 * prreq frame format
1688af8418dcSSam Leffler 	 *	[tlv] ssid
1689af8418dcSSam Leffler 	 *	[tlv] supported rates
1690b032f27cSSam Leffler 	 *	[tlv] RSN (optional)
1691af8418dcSSam Leffler 	 *	[tlv] extended supported rates
1692b032f27cSSam Leffler 	 *	[tlv] WPA (optional)
1693af8418dcSSam Leffler 	 *	[tlv] user-specified ie's
1694af8418dcSSam Leffler 	 */
1695af8418dcSSam Leffler 	m = ieee80211_getmgtframe(&frm,
169668e8e04eSSam Leffler 		 ic->ic_headroom + sizeof(struct ieee80211_frame),
1697af8418dcSSam Leffler 	       	 2 + IEEE80211_NWID_LEN
1698af8418dcSSam Leffler 	       + 2 + IEEE80211_RATE_SIZE
1699b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_wpa)
1700af8418dcSSam Leffler 	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1701b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_wpa)
1702b032f27cSSam Leffler 	       + (vap->iv_appie_probereq != NULL ?
1703b032f27cSSam Leffler 		   vap->iv_appie_probereq->ie_len : 0)
1704af8418dcSSam Leffler 	);
1705af8418dcSSam Leffler 	if (m == NULL) {
1706b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
1707af8418dcSSam Leffler 		ieee80211_free_node(ni);
1708af8418dcSSam Leffler 		return ENOMEM;
1709af8418dcSSam Leffler 	}
1710af8418dcSSam Leffler 
1711af8418dcSSam Leffler 	frm = ieee80211_add_ssid(frm, ssid, ssidlen);
171241b3c790SSam Leffler 	rs = ieee80211_get_suprates(ic, ic->ic_curchan);
171341b3c790SSam Leffler 	frm = ieee80211_add_rates(frm, rs);
1714b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA2) {
1715b032f27cSSam Leffler 		if (vap->iv_rsn_ie != NULL)
1716b032f27cSSam Leffler 			frm = add_ie(frm, vap->iv_rsn_ie);
1717b032f27cSSam Leffler 		/* XXX else complain? */
1718af8418dcSSam Leffler 	}
1719b032f27cSSam Leffler 	frm = ieee80211_add_xrates(frm, rs);
1720b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA1) {
1721b032f27cSSam Leffler 		if (vap->iv_wpa_ie != NULL)
1722b032f27cSSam Leffler 			frm = add_ie(frm, vap->iv_wpa_ie);
1723b032f27cSSam Leffler 		/* XXX else complain? */
1724b032f27cSSam Leffler 	}
1725b032f27cSSam Leffler 	if (vap->iv_appie_probereq != NULL)
1726b032f27cSSam Leffler 		frm = add_appie(frm, vap->iv_appie_probereq);
172768e8e04eSSam Leffler 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1728af8418dcSSam Leffler 
1729af8418dcSSam Leffler 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
1730af8418dcSSam Leffler 	if (m == NULL)
1731af8418dcSSam Leffler 		return ENOMEM;
1732af8418dcSSam Leffler 
1733af8418dcSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
1734b032f27cSSam Leffler 	ieee80211_send_setup(ni, wh,
1735af8418dcSSam Leffler 		IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
1736af8418dcSSam Leffler 		sa, da, bssid);
1737af8418dcSSam Leffler 	/* XXX power management? */
1738af8418dcSSam Leffler 
1739b032f27cSSam Leffler 	M_WME_SETAC(m, WME_AC_BE);
1740b032f27cSSam Leffler 
1741af8418dcSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_probereq);
1742af8418dcSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_mgmt);
1743af8418dcSSam Leffler 
1744b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1745b032f27cSSam Leffler 	    "send probe req on channel %u bssid %s ssid \"%.*s\"\n",
1746b032f27cSSam Leffler 	    ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(bssid),
1747b032f27cSSam Leffler 	    ssidlen, ssid);
1748af8418dcSSam Leffler 
1749b032f27cSSam Leffler 	return ic->ic_raw_xmit(ni, m, NULL);
1750af8418dcSSam Leffler }
1751af8418dcSSam Leffler 
1752af8418dcSSam Leffler /*
1753667dad55SSam Leffler  * Calculate capability information for mgt frames.
1754667dad55SSam Leffler  */
175568e8e04eSSam Leffler static uint16_t
1756b032f27cSSam Leffler getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
1757667dad55SSam Leffler {
1758b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
175968e8e04eSSam Leffler 	uint16_t capinfo;
1760667dad55SSam Leffler 
1761b032f27cSSam Leffler 	KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
1762667dad55SSam Leffler 
1763b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1764667dad55SSam Leffler 		capinfo = IEEE80211_CAPINFO_ESS;
1765b032f27cSSam Leffler 	else if (vap->iv_opmode == IEEE80211_M_IBSS)
1766667dad55SSam Leffler 		capinfo = IEEE80211_CAPINFO_IBSS;
1767667dad55SSam Leffler 	else
1768667dad55SSam Leffler 		capinfo = 0;
1769b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY)
1770667dad55SSam Leffler 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
1771667dad55SSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1772667dad55SSam Leffler 	    IEEE80211_IS_CHAN_2GHZ(chan))
1773667dad55SSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1774667dad55SSam Leffler 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
1775667dad55SSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1776b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
1777b032f27cSSam Leffler 		capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
1778667dad55SSam Leffler 	return capinfo;
1779667dad55SSam Leffler }
1780667dad55SSam Leffler 
1781667dad55SSam Leffler /*
17820a915fadSSam Leffler  * Send a management frame.  The node is for the destination (or ic_bss
17830a915fadSSam Leffler  * when in station mode).  Nodes other than ic_bss have their reference
17840a915fadSSam Leffler  * count bumped to reflect our use for an indeterminant time.
17850a915fadSSam Leffler  */
17861a1e1d21SSam Leffler int
1787b032f27cSSam Leffler ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
17881a1e1d21SSam Leffler {
17891b6167d2SSam Leffler #define	HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
1790b032f27cSSam Leffler #define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
1791b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1792b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1793b032f27cSSam Leffler 	struct ieee80211_node *bss = vap->iv_bss;
17941a1e1d21SSam Leffler 	struct mbuf *m;
179568e8e04eSSam Leffler 	uint8_t *frm;
179668e8e04eSSam Leffler 	uint16_t capinfo;
179768e8e04eSSam Leffler 	int has_challenge, is_shared_key, ret, status;
17981a1e1d21SSam Leffler 
17990a915fadSSam Leffler 	KASSERT(ni != NULL, ("null node"));
18000a915fadSSam Leffler 
18010a915fadSSam Leffler 	/*
18020a915fadSSam Leffler 	 * Hold a reference on the node so it doesn't go away until after
18030a915fadSSam Leffler 	 * the xmit is complete all the way in the driver.  On error we
18040a915fadSSam Leffler 	 * will remove our reference.
18050a915fadSSam Leffler 	 */
1806b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
180749a15236SSam Leffler 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
18088a1b9b6aSSam Leffler 		__func__, __LINE__,
180949a15236SSam Leffler 		ni, ether_sprintf(ni->ni_macaddr),
18108a1b9b6aSSam Leffler 		ieee80211_node_refcnt(ni)+1);
18110a915fadSSam Leffler 	ieee80211_ref_node(ni);
18128a1b9b6aSSam Leffler 
18131a1e1d21SSam Leffler 	switch (type) {
18141a1e1d21SSam Leffler 
18151a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_AUTH:
18168a1b9b6aSSam Leffler 		status = arg >> 16;
18178a1b9b6aSSam Leffler 		arg &= 0xffff;
18188a1b9b6aSSam Leffler 		has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
18198a1b9b6aSSam Leffler 		    arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
18208a1b9b6aSSam Leffler 		    ni->ni_challenge != NULL);
18218a1b9b6aSSam Leffler 
18228a1b9b6aSSam Leffler 		/*
18238a1b9b6aSSam Leffler 		 * Deduce whether we're doing open authentication or
18248a1b9b6aSSam Leffler 		 * shared key authentication.  We do the latter if
18258a1b9b6aSSam Leffler 		 * we're in the middle of a shared key authentication
18268a1b9b6aSSam Leffler 		 * handshake or if we're initiating an authentication
18278a1b9b6aSSam Leffler 		 * request and configured to use shared key.
18288a1b9b6aSSam Leffler 		 */
18298a1b9b6aSSam Leffler 		is_shared_key = has_challenge ||
18308a1b9b6aSSam Leffler 		     arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
18318a1b9b6aSSam Leffler 		     (arg == IEEE80211_AUTH_SHARED_REQUEST &&
1832b032f27cSSam Leffler 		      bss->ni_authmode == IEEE80211_AUTH_SHARED);
18338a1b9b6aSSam Leffler 
18348a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
183568e8e04eSSam Leffler 			 ic->ic_headroom + sizeof(struct ieee80211_frame),
183668e8e04eSSam Leffler 			  3 * sizeof(uint16_t)
18378a1b9b6aSSam Leffler 			+ (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
183868e8e04eSSam Leffler 				sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)
18398a1b9b6aSSam Leffler 		);
18401a1e1d21SSam Leffler 		if (m == NULL)
18418a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
18428a1b9b6aSSam Leffler 
184368e8e04eSSam Leffler 		((uint16_t *)frm)[0] =
18448a1b9b6aSSam Leffler 		    (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
18458a1b9b6aSSam Leffler 		                    : htole16(IEEE80211_AUTH_ALG_OPEN);
184668e8e04eSSam Leffler 		((uint16_t *)frm)[1] = htole16(arg);	/* sequence number */
184768e8e04eSSam Leffler 		((uint16_t *)frm)[2] = htole16(status);/* status */
18488a1b9b6aSSam Leffler 
18498a1b9b6aSSam Leffler 		if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
185068e8e04eSSam Leffler 			((uint16_t *)frm)[3] =
18518a1b9b6aSSam Leffler 			    htole16((IEEE80211_CHALLENGE_LEN << 8) |
18528a1b9b6aSSam Leffler 			    IEEE80211_ELEMID_CHALLENGE);
185368e8e04eSSam Leffler 			memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
18548a1b9b6aSSam Leffler 			    IEEE80211_CHALLENGE_LEN);
18558a1b9b6aSSam Leffler 			m->m_pkthdr.len = m->m_len =
185668e8e04eSSam Leffler 				4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
18578a1b9b6aSSam Leffler 			if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
1858b032f27cSSam Leffler 				IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1859b032f27cSSam Leffler 				    "request encrypt frame (%s)", __func__);
18608a1b9b6aSSam Leffler 				m->m_flags |= M_LINK0; /* WEP-encrypt, please */
18618a1b9b6aSSam Leffler 			}
18628a1b9b6aSSam Leffler 		} else
186368e8e04eSSam Leffler 			m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
18648a1b9b6aSSam Leffler 
18658a1b9b6aSSam Leffler 		/* XXX not right for shared key */
18668a1b9b6aSSam Leffler 		if (status == IEEE80211_STATUS_SUCCESS)
18678a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_auth);
18688a1b9b6aSSam Leffler 		else
18698a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_auth_fail);
18708a1b9b6aSSam Leffler 
1871b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA)
187268e8e04eSSam Leffler 			ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
1873b032f27cSSam Leffler 				(void *) vap->iv_state);
18741a1e1d21SSam Leffler 		break;
18751a1e1d21SSam Leffler 
18761a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
1877b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1878b032f27cSSam Leffler 		    "send station deauthenticate (reason %d)", arg);
187968e8e04eSSam Leffler 		m = ieee80211_getmgtframe(&frm,
188068e8e04eSSam Leffler 			ic->ic_headroom + sizeof(struct ieee80211_frame),
188168e8e04eSSam Leffler 			sizeof(uint16_t));
18821a1e1d21SSam Leffler 		if (m == NULL)
18838a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
188468e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(arg);	/* reason */
188568e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
18868a1b9b6aSSam Leffler 
18878a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, tx_deauth);
18888a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
18898a1b9b6aSSam Leffler 
1890e4918ecdSSam Leffler 		ieee80211_node_unauthorize(ni);		/* port closed */
18911a1e1d21SSam Leffler 		break;
18921a1e1d21SSam Leffler 
18931a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
18941a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
18951a1e1d21SSam Leffler 		/*
18961a1e1d21SSam Leffler 		 * asreq frame format
18971a1e1d21SSam Leffler 		 *	[2] capability information
18981a1e1d21SSam Leffler 		 *	[2] listen interval
18991a1e1d21SSam Leffler 		 *	[6*] current AP address (reassoc only)
19001a1e1d21SSam Leffler 		 *	[tlv] ssid
19011a1e1d21SSam Leffler 		 *	[tlv] supported rates
19021a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
1903b032f27cSSam Leffler 		 *	[4] power capability (optional)
1904b032f27cSSam Leffler 		 *	[28] supported channels (optional)
190568e8e04eSSam Leffler 		 *	[tlv] HT capabilities
1906b032f27cSSam Leffler 		 *	[tlv] WME (optional)
190768e8e04eSSam Leffler 		 *	[tlv] Vendor OUI HT capabilities (optional)
190868e8e04eSSam Leffler 		 *	[tlv] Atheros capabilities (if negotiated)
1909b032f27cSSam Leffler 		 *	[tlv] AppIE's (optional)
19101a1e1d21SSam Leffler 		 */
19118a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
191268e8e04eSSam Leffler 			 ic->ic_headroom + sizeof(struct ieee80211_frame),
191368e8e04eSSam Leffler 			 sizeof(uint16_t)
191468e8e04eSSam Leffler 		       + sizeof(uint16_t)
19151a1e1d21SSam Leffler 		       + IEEE80211_ADDR_LEN
19168a1b9b6aSSam Leffler 		       + 2 + IEEE80211_NWID_LEN
19171a1e1d21SSam Leffler 		       + 2 + IEEE80211_RATE_SIZE
19188a1b9b6aSSam Leffler 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1919b032f27cSSam Leffler 		       + 4
1920b032f27cSSam Leffler 		       + 2 + 26
19218a1b9b6aSSam Leffler 		       + sizeof(struct ieee80211_wme_info)
1922b032f27cSSam Leffler 		       + sizeof(struct ieee80211_ie_htcap)
1923b032f27cSSam Leffler 		       + 4 + sizeof(struct ieee80211_ie_htcap)
192468e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ath_ie)
1925b032f27cSSam Leffler 		       + (vap->iv_appie_wpa != NULL ?
1926b032f27cSSam Leffler 				vap->iv_appie_wpa->ie_len : 0)
1927b032f27cSSam Leffler 		       + (vap->iv_appie_assocreq != NULL ?
1928b032f27cSSam Leffler 				vap->iv_appie_assocreq->ie_len : 0)
19298a1b9b6aSSam Leffler 		);
19301a1e1d21SSam Leffler 		if (m == NULL)
19318a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
19321a1e1d21SSam Leffler 
1933b032f27cSSam Leffler 		KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1934b032f27cSSam Leffler 		    ("wrong mode %u", vap->iv_opmode));
1935667dad55SSam Leffler 		capinfo = IEEE80211_CAPINFO_ESS;
1936b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_PRIVACY)
19371a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
19384f2e09c4SSam Leffler 		/*
19394f2e09c4SSam Leffler 		 * NB: Some 11a AP's reject the request when
19404f2e09c4SSam Leffler 		 *     short premable is set.
19414f2e09c4SSam Leffler 		 */
19424f2e09c4SSam Leffler 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1943b5c99415SSam Leffler 		    IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
19441a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
194568e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
19468a1b9b6aSSam Leffler 		    (ic->ic_caps & IEEE80211_C_SHSLOT))
19471a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
19481b6167d2SSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
1949b032f27cSSam Leffler 		    (vap->iv_flags & IEEE80211_F_DOTH))
19501b6167d2SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
195168e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(capinfo);
19521a1e1d21SSam Leffler 		frm += 2;
19531a1e1d21SSam Leffler 
1954b032f27cSSam Leffler 		KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
195568e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
1956b032f27cSSam Leffler 						    bss->ni_intval));
19571a1e1d21SSam Leffler 		frm += 2;
19581a1e1d21SSam Leffler 
19591a1e1d21SSam Leffler 		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1960b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
19611a1e1d21SSam Leffler 			frm += IEEE80211_ADDR_LEN;
19621a1e1d21SSam Leffler 		}
19631a1e1d21SSam Leffler 
19641a1e1d21SSam Leffler 		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
19651a1e1d21SSam Leffler 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
1966b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_WPA2) {
1967b032f27cSSam Leffler 			if (vap->iv_rsn_ie != NULL)
1968b032f27cSSam Leffler 				frm = add_ie(frm, vap->iv_rsn_ie);
1969b032f27cSSam Leffler 			/* XXX else complain? */
19708a1b9b6aSSam Leffler 		}
1971b032f27cSSam Leffler 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1972b032f27cSSam Leffler 		if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
1973b032f27cSSam Leffler 			frm = ieee80211_add_powercapability(frm,
1974b032f27cSSam Leffler 			    ic->ic_curchan);
1975b032f27cSSam Leffler 			frm = ieee80211_add_supportedchannels(frm, ic);
1976b032f27cSSam Leffler 		}
1977b032f27cSSam Leffler 		if ((vap->iv_flags_ext & IEEE80211_FEXT_HT) &&
1978b032f27cSSam Leffler 		    ni->ni_ies.htcap_ie != NULL &&
1979b032f27cSSam Leffler 		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP)
1980b032f27cSSam Leffler 			frm = ieee80211_add_htcap(frm, ni);
1981b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_WPA1) {
1982b032f27cSSam Leffler 			if (vap->iv_wpa_ie != NULL)
1983b032f27cSSam Leffler 				frm = add_ie(frm, vap->iv_wpa_ie);
1984b032f27cSSam Leffler 			/* XXX else complain */
1985b032f27cSSam Leffler 		}
1986b032f27cSSam Leffler 		if ((ic->ic_flags & IEEE80211_F_WME) &&
1987b032f27cSSam Leffler 		    ni->ni_ies.wme_ie != NULL)
1988b032f27cSSam Leffler 			frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
1989b032f27cSSam Leffler 		if ((vap->iv_flags_ext & IEEE80211_FEXT_HT) &&
1990b032f27cSSam Leffler 		    ni->ni_ies.htcap_ie != NULL &&
1991b032f27cSSam Leffler 		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR)
1992b032f27cSSam Leffler 			frm = ieee80211_add_htcap_vendor(frm, ni);
1993b032f27cSSam Leffler 		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
1994b032f27cSSam Leffler 			frm = ieee80211_add_ath(frm,
1995b032f27cSSam Leffler 				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
1996b032f27cSSam Leffler 				(vap->iv_flags & IEEE80211_F_WPA) == 0 &&
1997b032f27cSSam Leffler 				ni->ni_authmode != IEEE80211_AUTH_8021X &&
1998b032f27cSSam Leffler 				vap->iv_def_txkey != IEEE80211_KEYIX_NONE ?
1999b032f27cSSam Leffler 				vap->iv_def_txkey : 0x7fff);
2000b032f27cSSam Leffler 		if (vap->iv_appie_assocreq != NULL)
2001b032f27cSSam Leffler 			frm = add_appie(frm, vap->iv_appie_assocreq);
200268e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
20031a1e1d21SSam Leffler 
200468e8e04eSSam Leffler 		ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2005b032f27cSSam Leffler 			(void *) vap->iv_state);
20061a1e1d21SSam Leffler 		break;
20071a1e1d21SSam Leffler 
20081a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
20091a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
20101a1e1d21SSam Leffler 		/*
201168e8e04eSSam Leffler 		 * asresp frame format
20121a1e1d21SSam Leffler 		 *	[2] capability information
20131a1e1d21SSam Leffler 		 *	[2] status
20141a1e1d21SSam Leffler 		 *	[2] association ID
20151a1e1d21SSam Leffler 		 *	[tlv] supported rates
20161a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
2017b032f27cSSam Leffler 		 *	[tlv] HT capabilities (standard, if STA enabled)
2018b032f27cSSam Leffler 		 *	[tlv] HT information (standard, if STA enabled)
2019b032f27cSSam Leffler 		 *	[tlv] WME (if configured and STA enabled)
2020b032f27cSSam Leffler 		 *	[tlv] HT capabilities (vendor OUI, if STA enabled)
2021b032f27cSSam Leffler 		 *	[tlv] HT information (vendor OUI, if STA enabled)
2022b032f27cSSam Leffler 		 *	[tlv] Atheros capabilities (if STA enabled)
2023b032f27cSSam Leffler 		 *	[tlv] AppIE's (optional)
20241a1e1d21SSam Leffler 		 */
20258a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
202668e8e04eSSam Leffler 			 ic->ic_headroom + sizeof(struct ieee80211_frame),
202768e8e04eSSam Leffler 			 sizeof(uint16_t)
202868e8e04eSSam Leffler 		       + sizeof(uint16_t)
202968e8e04eSSam Leffler 		       + sizeof(uint16_t)
20301a1e1d21SSam Leffler 		       + 2 + IEEE80211_RATE_SIZE
20318a1b9b6aSSam Leffler 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
203268e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ie_htcap) + 4
203368e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ie_htinfo) + 4
2034b032f27cSSam Leffler 		       + sizeof(struct ieee80211_wme_param)
203568e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ath_ie)
2036b032f27cSSam Leffler 		       + (vap->iv_appie_assocresp != NULL ?
2037b032f27cSSam Leffler 				vap->iv_appie_assocresp->ie_len : 0)
20388a1b9b6aSSam Leffler 		);
20391a1e1d21SSam Leffler 		if (m == NULL)
20408a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
20411a1e1d21SSam Leffler 
2042b032f27cSSam Leffler 		capinfo = getcapinfo(vap, bss->ni_chan);
204368e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(capinfo);
20441a1e1d21SSam Leffler 		frm += 2;
20451a1e1d21SSam Leffler 
204668e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(arg);	/* status */
20471a1e1d21SSam Leffler 		frm += 2;
20481a1e1d21SSam Leffler 
20498a1b9b6aSSam Leffler 		if (arg == IEEE80211_STATUS_SUCCESS) {
205068e8e04eSSam Leffler 			*(uint16_t *)frm = htole16(ni->ni_associd);
20518a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_assoc);
20528a1b9b6aSSam Leffler 		} else
20538a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_assoc_fail);
20541a1e1d21SSam Leffler 		frm += 2;
20551a1e1d21SSam Leffler 
20561a1e1d21SSam Leffler 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
20571a1e1d21SSam Leffler 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
205868e8e04eSSam Leffler 		/* NB: respond according to what we received */
20591b6167d2SSam Leffler 		if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
206068e8e04eSSam Leffler 			frm = ieee80211_add_htcap(frm, ni);
206168e8e04eSSam Leffler 			frm = ieee80211_add_htinfo(frm, ni);
206268e8e04eSSam Leffler 		}
2063b032f27cSSam Leffler 		if ((vap->iv_flags & IEEE80211_F_WME) &&
2064b032f27cSSam Leffler 		    ni->ni_ies.wme_ie != NULL)
20651b6167d2SSam Leffler 			frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
20661b6167d2SSam Leffler 		if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
20671b6167d2SSam Leffler 			frm = ieee80211_add_htcap_vendor(frm, ni);
20681b6167d2SSam Leffler 			frm = ieee80211_add_htinfo_vendor(frm, ni);
206968e8e04eSSam Leffler 		}
2070b032f27cSSam Leffler 		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
207168e8e04eSSam Leffler 			frm = ieee80211_add_ath(frm,
2072b032f27cSSam Leffler 				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
207368e8e04eSSam Leffler 				ni->ni_ath_defkeyix);
2074b032f27cSSam Leffler 		if (vap->iv_appie_assocresp != NULL)
2075b032f27cSSam Leffler 			frm = add_appie(frm, vap->iv_appie_assocresp);
207668e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
20771a1e1d21SSam Leffler 		break;
20781a1e1d21SSam Leffler 
20791a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
2080b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2081b032f27cSSam Leffler 		    "send station disassociate (reason %d)", arg);
208268e8e04eSSam Leffler 		m = ieee80211_getmgtframe(&frm,
208368e8e04eSSam Leffler 			ic->ic_headroom + sizeof(struct ieee80211_frame),
208468e8e04eSSam Leffler 			sizeof(uint16_t));
20851a1e1d21SSam Leffler 		if (m == NULL)
20868a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
208768e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(arg);	/* reason */
208868e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
20898a1b9b6aSSam Leffler 
20908a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, tx_disassoc);
20918a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
20921a1e1d21SSam Leffler 		break;
20931a1e1d21SSam Leffler 
20941a1e1d21SSam Leffler 	default:
2095b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
2096b032f27cSSam Leffler 		    "invalid mgmt frame type %u", type);
20971be50176SSam Leffler 		senderr(EINVAL, is_tx_unknownmgt);
20980a915fadSSam Leffler 		/* NOTREACHED */
20991a1e1d21SSam Leffler 	}
210068e8e04eSSam Leffler 
2101b032f27cSSam Leffler 	return ieee80211_mgmt_output(ni, m, type);
21020a915fadSSam Leffler bad:
21038a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
21041a1e1d21SSam Leffler 	return ret;
21050a915fadSSam Leffler #undef senderr
21061b6167d2SSam Leffler #undef HTFLAGS
21071a1e1d21SSam Leffler }
21088a1b9b6aSSam Leffler 
2109b032f27cSSam Leffler /*
2110b032f27cSSam Leffler  * Return an mbuf with a probe response frame in it.
2111b032f27cSSam Leffler  * Space is left to prepend and 802.11 header at the
2112b032f27cSSam Leffler  * front but it's left to the caller to fill in.
2113b032f27cSSam Leffler  */
2114b032f27cSSam Leffler struct mbuf *
2115b032f27cSSam Leffler ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
2116b032f27cSSam Leffler {
2117b032f27cSSam Leffler 	struct ieee80211vap *vap = bss->ni_vap;
2118b032f27cSSam Leffler 	struct ieee80211com *ic = bss->ni_ic;
2119b032f27cSSam Leffler 	const struct ieee80211_rateset *rs;
2120b032f27cSSam Leffler 	struct mbuf *m;
2121b032f27cSSam Leffler 	uint16_t capinfo;
2122b032f27cSSam Leffler 	uint8_t *frm;
2123b032f27cSSam Leffler 
2124b032f27cSSam Leffler 	/*
2125b032f27cSSam Leffler 	 * probe response frame format
2126b032f27cSSam Leffler 	 *	[8] time stamp
2127b032f27cSSam Leffler 	 *	[2] beacon interval
2128b032f27cSSam Leffler 	 *	[2] cabability information
2129b032f27cSSam Leffler 	 *	[tlv] ssid
2130b032f27cSSam Leffler 	 *	[tlv] supported rates
2131b032f27cSSam Leffler 	 *	[tlv] parameter set (FH/DS)
2132b032f27cSSam Leffler 	 *	[tlv] parameter set (IBSS)
2133b032f27cSSam Leffler 	 *	[tlv] country (optional)
2134b032f27cSSam Leffler 	 *	[3] power control (optional)
2135b032f27cSSam Leffler 	 *	[5] channel switch announcement (CSA) (optional)
2136b032f27cSSam Leffler 	 *	[tlv] extended rate phy (ERP)
2137b032f27cSSam Leffler 	 *	[tlv] extended supported rates
2138688fe74dSSam Leffler 	 *	[tlv] RSN (optional)
2139b032f27cSSam Leffler 	 *	[tlv] HT capabilities
2140b032f27cSSam Leffler 	 *	[tlv] HT information
2141b032f27cSSam Leffler 	 *	[tlv] WPA (optional)
2142b032f27cSSam Leffler 	 *	[tlv] WME (optional)
2143b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT capabilities (optional)
2144b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT information (optional)
2145b032f27cSSam Leffler 	 *	[tlv] Atheros capabilities
2146b032f27cSSam Leffler 	 *	[tlv] AppIE's (optional)
2147b032f27cSSam Leffler 	 */
2148b032f27cSSam Leffler 	m = ieee80211_getmgtframe(&frm,
2149b032f27cSSam Leffler 		 ic->ic_headroom + sizeof(struct ieee80211_frame),
2150b032f27cSSam Leffler 		 8
2151b032f27cSSam Leffler 	       + sizeof(uint16_t)
2152b032f27cSSam Leffler 	       + sizeof(uint16_t)
2153b032f27cSSam Leffler 	       + 2 + IEEE80211_NWID_LEN
2154b032f27cSSam Leffler 	       + 2 + IEEE80211_RATE_SIZE
2155b032f27cSSam Leffler 	       + 7	/* max(7,3) */
2156b032f27cSSam Leffler 	       + IEEE80211_COUNTRY_MAX_SIZE
2157b032f27cSSam Leffler 	       + 3
2158b032f27cSSam Leffler 	       + sizeof(struct ieee80211_csa_ie)
2159b032f27cSSam Leffler 	       + 3
2160b032f27cSSam Leffler 	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2161688fe74dSSam Leffler 	       + sizeof(struct ieee80211_ie_wpa)
2162b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_htcap)
2163b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_htinfo)
2164b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_wpa)
2165b032f27cSSam Leffler 	       + sizeof(struct ieee80211_wme_param)
2166b032f27cSSam Leffler 	       + 4 + sizeof(struct ieee80211_ie_htcap)
2167b032f27cSSam Leffler 	       + 4 + sizeof(struct ieee80211_ie_htinfo)
2168b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ath_ie)
2169b032f27cSSam Leffler 	       + (vap->iv_appie_proberesp != NULL ?
2170b032f27cSSam Leffler 			vap->iv_appie_proberesp->ie_len : 0)
2171b032f27cSSam Leffler 	);
2172b032f27cSSam Leffler 	if (m == NULL) {
2173b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
2174b032f27cSSam Leffler 		return NULL;
2175b032f27cSSam Leffler 	}
2176b032f27cSSam Leffler 
2177b032f27cSSam Leffler 	memset(frm, 0, 8);	/* timestamp should be filled later */
2178b032f27cSSam Leffler 	frm += 8;
2179b032f27cSSam Leffler 	*(uint16_t *)frm = htole16(bss->ni_intval);
2180b032f27cSSam Leffler 	frm += 2;
2181b032f27cSSam Leffler 	capinfo = getcapinfo(vap, bss->ni_chan);
2182b032f27cSSam Leffler 	*(uint16_t *)frm = htole16(capinfo);
2183b032f27cSSam Leffler 	frm += 2;
2184b032f27cSSam Leffler 
2185b032f27cSSam Leffler 	frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
2186b032f27cSSam Leffler 	rs = ieee80211_get_suprates(ic, bss->ni_chan);
2187b032f27cSSam Leffler 	frm = ieee80211_add_rates(frm, rs);
2188b032f27cSSam Leffler 
2189b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
2190b032f27cSSam Leffler 		*frm++ = IEEE80211_ELEMID_FHPARMS;
2191b032f27cSSam Leffler 		*frm++ = 5;
2192b032f27cSSam Leffler 		*frm++ = bss->ni_fhdwell & 0x00ff;
2193b032f27cSSam Leffler 		*frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
2194b032f27cSSam Leffler 		*frm++ = IEEE80211_FH_CHANSET(
2195b032f27cSSam Leffler 		    ieee80211_chan2ieee(ic, bss->ni_chan));
2196b032f27cSSam Leffler 		*frm++ = IEEE80211_FH_CHANPAT(
2197b032f27cSSam Leffler 		    ieee80211_chan2ieee(ic, bss->ni_chan));
2198b032f27cSSam Leffler 		*frm++ = bss->ni_fhindex;
2199b032f27cSSam Leffler 	} else {
2200b032f27cSSam Leffler 		*frm++ = IEEE80211_ELEMID_DSPARMS;
2201b032f27cSSam Leffler 		*frm++ = 1;
2202b032f27cSSam Leffler 		*frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
2203b032f27cSSam Leffler 	}
2204b032f27cSSam Leffler 
2205b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
2206b032f27cSSam Leffler 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
2207b032f27cSSam Leffler 		*frm++ = 2;
2208b032f27cSSam Leffler 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
2209b032f27cSSam Leffler 	}
2210b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2211b032f27cSSam Leffler 	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2212b032f27cSSam Leffler 		frm = ieee80211_add_countryie(frm, ic);
2213b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_DOTH) {
2214b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
2215b032f27cSSam Leffler 			frm = ieee80211_add_powerconstraint(frm, vap);
2216b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2217b032f27cSSam Leffler 			frm = ieee80211_add_csa(frm, vap);
2218b032f27cSSam Leffler 	}
2219b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
2220b032f27cSSam Leffler 		frm = ieee80211_add_erp(frm, ic);
2221b032f27cSSam Leffler 	frm = ieee80211_add_xrates(frm, rs);
2222688fe74dSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA2) {
2223688fe74dSSam Leffler 		if (vap->iv_rsn_ie != NULL)
2224688fe74dSSam Leffler 			frm = add_ie(frm, vap->iv_rsn_ie);
2225688fe74dSSam Leffler 		/* XXX else complain? */
2226688fe74dSSam Leffler 	}
2227b032f27cSSam Leffler 	/*
2228b032f27cSSam Leffler 	 * NB: legacy 11b clients do not get certain ie's.
2229b032f27cSSam Leffler 	 *     The caller identifies such clients by passing
2230b032f27cSSam Leffler 	 *     a token in legacy to us.  Could expand this to be
2231b032f27cSSam Leffler 	 *     any legacy client for stuff like HT ie's.
2232b032f27cSSam Leffler 	 */
2233b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2234b032f27cSSam Leffler 	    legacy != IEEE80211_SEND_LEGACY_11B) {
2235b032f27cSSam Leffler 		frm = ieee80211_add_htcap(frm, bss);
2236b032f27cSSam Leffler 		frm = ieee80211_add_htinfo(frm, bss);
2237b032f27cSSam Leffler 	}
2238b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA1) {
2239b032f27cSSam Leffler 		if (vap->iv_wpa_ie != NULL)
2240b032f27cSSam Leffler 			frm = add_ie(frm, vap->iv_wpa_ie);
2241b032f27cSSam Leffler 		/* XXX else complain? */
2242b032f27cSSam Leffler 	}
2243b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WME)
2244b032f27cSSam Leffler 		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2245b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2246b032f27cSSam Leffler 	    (vap->iv_flags_ext & IEEE80211_FEXT_HTCOMPAT) &&
2247b032f27cSSam Leffler 	    legacy != IEEE80211_SEND_LEGACY_11B) {
2248b032f27cSSam Leffler 		frm = ieee80211_add_htcap_vendor(frm, bss);
2249b032f27cSSam Leffler 		frm = ieee80211_add_htinfo_vendor(frm, bss);
2250b032f27cSSam Leffler 	}
2251b032f27cSSam Leffler 	if (bss->ni_ies.ath_ie != NULL && legacy != IEEE80211_SEND_LEGACY_11B)
2252b032f27cSSam Leffler 		frm = ieee80211_add_ath(frm, bss->ni_ath_flags,
2253b032f27cSSam Leffler 			bss->ni_ath_defkeyix);
2254b032f27cSSam Leffler 	if (vap->iv_appie_proberesp != NULL)
2255b032f27cSSam Leffler 		frm = add_appie(frm, vap->iv_appie_proberesp);
2256b032f27cSSam Leffler 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2257b032f27cSSam Leffler 
2258b032f27cSSam Leffler 	return m;
2259b032f27cSSam Leffler }
2260b032f27cSSam Leffler 
2261b032f27cSSam Leffler /*
2262b032f27cSSam Leffler  * Send a probe response frame to the specified mac address.
2263b032f27cSSam Leffler  * This does not go through the normal mgt frame api so we
2264b032f27cSSam Leffler  * can specify the destination address and re-use the bss node
2265b032f27cSSam Leffler  * for the sta reference.
2266b032f27cSSam Leffler  */
2267b032f27cSSam Leffler int
2268b032f27cSSam Leffler ieee80211_send_proberesp(struct ieee80211vap *vap,
2269b032f27cSSam Leffler 	const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
2270b032f27cSSam Leffler {
2271b032f27cSSam Leffler 	struct ieee80211_node *bss = vap->iv_bss;
2272b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2273b032f27cSSam Leffler 	struct ieee80211_frame *wh;
2274b032f27cSSam Leffler 	struct mbuf *m;
2275b032f27cSSam Leffler 
2276b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
2277b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
2278b032f27cSSam Leffler 		    "block %s frame in CAC state", "probe response");
2279b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
2280b032f27cSSam Leffler 		return EIO;		/* XXX */
2281b032f27cSSam Leffler 	}
2282b032f27cSSam Leffler 
2283b032f27cSSam Leffler 	/*
2284b032f27cSSam Leffler 	 * Hold a reference on the node so it doesn't go away until after
2285b032f27cSSam Leffler 	 * the xmit is complete all the way in the driver.  On error we
2286b032f27cSSam Leffler 	 * will remove our reference.
2287b032f27cSSam Leffler 	 */
2288b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2289b032f27cSSam Leffler 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2290b032f27cSSam Leffler 	    __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
2291b032f27cSSam Leffler 	    ieee80211_node_refcnt(bss)+1);
2292b032f27cSSam Leffler 	ieee80211_ref_node(bss);
2293b032f27cSSam Leffler 
2294b032f27cSSam Leffler 	m = ieee80211_alloc_proberesp(bss, legacy);
2295b032f27cSSam Leffler 	if (m == NULL) {
2296b032f27cSSam Leffler 		ieee80211_free_node(bss);
2297b032f27cSSam Leffler 		return ENOMEM;
2298b032f27cSSam Leffler 	}
2299b032f27cSSam Leffler 
2300b032f27cSSam Leffler 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
2301b032f27cSSam Leffler 	KASSERT(m != NULL, ("no room for header"));
2302b032f27cSSam Leffler 
2303b032f27cSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
2304b032f27cSSam Leffler 	ieee80211_send_setup(bss, wh,
2305b032f27cSSam Leffler 		IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
2306b032f27cSSam Leffler 		vap->iv_myaddr, da, bss->ni_bssid);
2307b032f27cSSam Leffler 	/* XXX power management? */
2308b032f27cSSam Leffler 
2309b032f27cSSam Leffler 	M_WME_SETAC(m, WME_AC_BE);
2310b032f27cSSam Leffler 
2311b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2312b032f27cSSam Leffler 	    "send probe resp on channel %u to %s%s\n",
2313b032f27cSSam Leffler 	    ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
2314b032f27cSSam Leffler 	    legacy ? " <legacy>" : "");
2315b032f27cSSam Leffler 	IEEE80211_NODE_STAT(bss, tx_mgmt);
2316b032f27cSSam Leffler 
2317b032f27cSSam Leffler 	return ic->ic_raw_xmit(bss, m, NULL);
2318b032f27cSSam Leffler }
2319b032f27cSSam Leffler 
2320b032f27cSSam Leffler /*
2321b032f27cSSam Leffler  * Allocate and build a RTS (Request To Send) control frame.
2322b032f27cSSam Leffler  */
2323b032f27cSSam Leffler struct mbuf *
2324b032f27cSSam Leffler ieee80211_alloc_rts(struct ieee80211com *ic,
2325b032f27cSSam Leffler 	const uint8_t ra[IEEE80211_ADDR_LEN],
2326b032f27cSSam Leffler 	const uint8_t ta[IEEE80211_ADDR_LEN],
2327b032f27cSSam Leffler 	uint16_t dur)
2328b032f27cSSam Leffler {
2329b032f27cSSam Leffler 	struct ieee80211_frame_rts *rts;
2330b032f27cSSam Leffler 	struct mbuf *m;
2331b032f27cSSam Leffler 
2332b032f27cSSam Leffler 	/* XXX honor ic_headroom */
2333b032f27cSSam Leffler 	m = m_gethdr(M_DONTWAIT, MT_DATA);
2334b032f27cSSam Leffler 	if (m != NULL) {
2335b032f27cSSam Leffler 		rts = mtod(m, struct ieee80211_frame_rts *);
2336b032f27cSSam Leffler 		rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2337b032f27cSSam Leffler 			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
2338b032f27cSSam Leffler 		rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2339b032f27cSSam Leffler 		*(u_int16_t *)rts->i_dur = htole16(dur);
2340b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(rts->i_ra, ra);
2341b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(rts->i_ta, ta);
2342b032f27cSSam Leffler 
2343b032f27cSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
2344b032f27cSSam Leffler 	}
2345b032f27cSSam Leffler 	return m;
2346b032f27cSSam Leffler }
2347b032f27cSSam Leffler 
2348b032f27cSSam Leffler /*
2349b032f27cSSam Leffler  * Allocate and build a CTS (Clear To Send) control frame.
2350b032f27cSSam Leffler  */
2351b032f27cSSam Leffler struct mbuf *
2352b032f27cSSam Leffler ieee80211_alloc_cts(struct ieee80211com *ic,
2353b032f27cSSam Leffler 	const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
2354b032f27cSSam Leffler {
2355b032f27cSSam Leffler 	struct ieee80211_frame_cts *cts;
2356b032f27cSSam Leffler 	struct mbuf *m;
2357b032f27cSSam Leffler 
2358b032f27cSSam Leffler 	/* XXX honor ic_headroom */
2359b032f27cSSam Leffler 	m = m_gethdr(M_DONTWAIT, MT_DATA);
2360b032f27cSSam Leffler 	if (m != NULL) {
2361b032f27cSSam Leffler 		cts = mtod(m, struct ieee80211_frame_cts *);
2362b032f27cSSam Leffler 		cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2363b032f27cSSam Leffler 			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
2364b032f27cSSam Leffler 		cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2365b032f27cSSam Leffler 		*(u_int16_t *)cts->i_dur = htole16(dur);
2366b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(cts->i_ra, ra);
2367b032f27cSSam Leffler 
2368b032f27cSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
2369b032f27cSSam Leffler 	}
2370b032f27cSSam Leffler 	return m;
2371b032f27cSSam Leffler }
2372b032f27cSSam Leffler 
237368e8e04eSSam Leffler static void
237468e8e04eSSam Leffler ieee80211_tx_mgt_timeout(void *arg)
237568e8e04eSSam Leffler {
237668e8e04eSSam Leffler 	struct ieee80211_node *ni = arg;
2377b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
237868e8e04eSSam Leffler 
2379b032f27cSSam Leffler 	if (vap->iv_state != IEEE80211_S_INIT &&
2380b032f27cSSam Leffler 	    (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
238168e8e04eSSam Leffler 		/*
238268e8e04eSSam Leffler 		 * NB: it's safe to specify a timeout as the reason here;
238368e8e04eSSam Leffler 		 *     it'll only be used in the right state.
238468e8e04eSSam Leffler 		 */
2385b032f27cSSam Leffler 		ieee80211_new_state(vap, IEEE80211_S_SCAN,
238668e8e04eSSam Leffler 			IEEE80211_SCAN_FAIL_TIMEOUT);
238768e8e04eSSam Leffler 	}
238868e8e04eSSam Leffler }
238968e8e04eSSam Leffler 
239068e8e04eSSam Leffler static void
239168e8e04eSSam Leffler ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
239268e8e04eSSam Leffler {
2393b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
239468e8e04eSSam Leffler 	enum ieee80211_state ostate = (enum ieee80211_state) arg;
239568e8e04eSSam Leffler 
239668e8e04eSSam Leffler 	/*
239768e8e04eSSam Leffler 	 * Frame transmit completed; arrange timer callback.  If
239868e8e04eSSam Leffler 	 * transmit was successfuly we wait for response.  Otherwise
239968e8e04eSSam Leffler 	 * we arrange an immediate callback instead of doing the
240068e8e04eSSam Leffler 	 * callback directly since we don't know what state the driver
240168e8e04eSSam Leffler 	 * is in (e.g. what locks it is holding).  This work should
240268e8e04eSSam Leffler 	 * not be too time-critical and not happen too often so the
240368e8e04eSSam Leffler 	 * added overhead is acceptable.
240468e8e04eSSam Leffler 	 *
240568e8e04eSSam Leffler 	 * XXX what happens if !acked but response shows up before callback?
240668e8e04eSSam Leffler 	 */
2407b032f27cSSam Leffler 	if (vap->iv_state == ostate)
2408b032f27cSSam Leffler 		callout_reset(&vap->iv_mgtsend,
240968e8e04eSSam Leffler 			status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
241068e8e04eSSam Leffler 			ieee80211_tx_mgt_timeout, ni);
241168e8e04eSSam Leffler }
241268e8e04eSSam Leffler 
2413b032f27cSSam Leffler static void
2414b032f27cSSam Leffler ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
2415b032f27cSSam Leffler 	struct ieee80211_beacon_offsets *bo, struct ieee80211_node *ni)
24168a1b9b6aSSam Leffler {
2417b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2418b105a069SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2419b032f27cSSam Leffler 	struct ieee80211_rateset *rs = &ni->ni_rates;
242068e8e04eSSam Leffler 	uint16_t capinfo;
24218a1b9b6aSSam Leffler 
24228a1b9b6aSSam Leffler 	/*
24238a1b9b6aSSam Leffler 	 * beacon frame format
24248a1b9b6aSSam Leffler 	 *	[8] time stamp
24258a1b9b6aSSam Leffler 	 *	[2] beacon interval
24268a1b9b6aSSam Leffler 	 *	[2] cabability information
24278a1b9b6aSSam Leffler 	 *	[tlv] ssid
24288a1b9b6aSSam Leffler 	 *	[tlv] supported rates
24298a1b9b6aSSam Leffler 	 *	[3] parameter set (DS)
2430b032f27cSSam Leffler 	 *	[8] CF parameter set (optional)
24318a1b9b6aSSam Leffler 	 *	[tlv] parameter set (IBSS/TIM)
2432b032f27cSSam Leffler 	 *	[tlv] country (optional)
2433b032f27cSSam Leffler 	 *	[3] power control (optional)
2434b032f27cSSam Leffler 	 *	[5] channel switch announcement (CSA) (optional)
24358a1b9b6aSSam Leffler 	 *	[tlv] extended rate phy (ERP)
24368a1b9b6aSSam Leffler 	 *	[tlv] extended supported rates
2437688fe74dSSam Leffler 	 *	[tlv] RSN parameters
243868e8e04eSSam Leffler 	 *	[tlv] HT capabilities
243968e8e04eSSam Leffler 	 *	[tlv] HT information
2440b032f27cSSam Leffler 	 * XXX Vendor-specific OIDs (e.g. Atheros)
2441b032f27cSSam Leffler 	 *	[tlv] WPA parameters
2442b032f27cSSam Leffler 	 *	[tlv] WME parameters
244368e8e04eSSam Leffler 	 *	[tlv] Vendor OUI HT capabilities (optional)
244468e8e04eSSam Leffler 	 *	[tlv] Vendor OUI HT information (optional)
2445b032f27cSSam Leffler 	 *	[tlv] application data (optional)
24468a1b9b6aSSam Leffler 	 */
24478a1b9b6aSSam Leffler 
24481b6167d2SSam Leffler 	memset(bo, 0, sizeof(*bo));
24491b6167d2SSam Leffler 
24508a1b9b6aSSam Leffler 	memset(frm, 0, 8);	/* XXX timestamp is set by hardware/driver */
24518a1b9b6aSSam Leffler 	frm += 8;
245268e8e04eSSam Leffler 	*(uint16_t *)frm = htole16(ni->ni_intval);
24538a1b9b6aSSam Leffler 	frm += 2;
2454b032f27cSSam Leffler 	capinfo = getcapinfo(vap, ni->ni_chan);
245568e8e04eSSam Leffler 	bo->bo_caps = (uint16_t *)frm;
245668e8e04eSSam Leffler 	*(uint16_t *)frm = htole16(capinfo);
24578a1b9b6aSSam Leffler 	frm += 2;
24588a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_SSID;
2459b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
24608a1b9b6aSSam Leffler 		*frm++ = ni->ni_esslen;
24618a1b9b6aSSam Leffler 		memcpy(frm, ni->ni_essid, ni->ni_esslen);
24628a1b9b6aSSam Leffler 		frm += ni->ni_esslen;
24638a1b9b6aSSam Leffler 	} else
24648a1b9b6aSSam Leffler 		*frm++ = 0;
24658a1b9b6aSSam Leffler 	frm = ieee80211_add_rates(frm, rs);
2466b032f27cSSam Leffler 	if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
24678a1b9b6aSSam Leffler 		*frm++ = IEEE80211_ELEMID_DSPARMS;
24688a1b9b6aSSam Leffler 		*frm++ = 1;
2469b032f27cSSam Leffler 		*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2470b032f27cSSam Leffler 	}
2471b032f27cSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PCF) {
2472b032f27cSSam Leffler 		bo->bo_cfp = frm;
2473b032f27cSSam Leffler 		frm = ieee80211_add_cfparms(frm, ic);
24748a1b9b6aSSam Leffler 	}
24758a1b9b6aSSam Leffler 	bo->bo_tim = frm;
2476b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
24778a1b9b6aSSam Leffler 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
24788a1b9b6aSSam Leffler 		*frm++ = 2;
24798a1b9b6aSSam Leffler 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
24808a1b9b6aSSam Leffler 		bo->bo_tim_len = 0;
2481b032f27cSSam Leffler 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
24828a1b9b6aSSam Leffler 		struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
24838a1b9b6aSSam Leffler 
24848a1b9b6aSSam Leffler 		tie->tim_ie = IEEE80211_ELEMID_TIM;
24858a1b9b6aSSam Leffler 		tie->tim_len = 4;	/* length */
24868a1b9b6aSSam Leffler 		tie->tim_count = 0;	/* DTIM count */
2487b032f27cSSam Leffler 		tie->tim_period = vap->iv_dtim_period;	/* DTIM period */
24888a1b9b6aSSam Leffler 		tie->tim_bitctl = 0;	/* bitmap control */
24898a1b9b6aSSam Leffler 		tie->tim_bitmap[0] = 0;	/* Partial Virtual Bitmap */
24908a1b9b6aSSam Leffler 		frm += sizeof(struct ieee80211_tim_ie);
24918a1b9b6aSSam Leffler 		bo->bo_tim_len = 1;
24928a1b9b6aSSam Leffler 	}
2493b105a069SSam Leffler 	bo->bo_tim_trailer = frm;
2494b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2495b032f27cSSam Leffler 	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2496b032f27cSSam Leffler 		frm = ieee80211_add_countryie(frm, ic);
2497b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_DOTH) {
2498b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
2499b032f27cSSam Leffler 			frm = ieee80211_add_powerconstraint(frm, vap);
2500b032f27cSSam Leffler 		bo->bo_csa = frm;
2501b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2502b032f27cSSam Leffler 			frm = ieee80211_add_csa(frm, vap);
2503b032f27cSSam Leffler 	} else
2504b032f27cSSam Leffler 		bo->bo_csa = frm;
2505b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
25060912bac9SSam Leffler 		bo->bo_erp = frm;
25078a1b9b6aSSam Leffler 		frm = ieee80211_add_erp(frm, ic);
25081b6167d2SSam Leffler 	}
250968e8e04eSSam Leffler 	frm = ieee80211_add_xrates(frm, rs);
2510688fe74dSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA2) {
2511688fe74dSSam Leffler 		if (vap->iv_rsn_ie != NULL)
2512688fe74dSSam Leffler 			frm = add_ie(frm, vap->iv_rsn_ie);
2513688fe74dSSam Leffler 		/* XXX else complain */
2514688fe74dSSam Leffler 	}
2515b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
251668e8e04eSSam Leffler 		frm = ieee80211_add_htcap(frm, ni);
251768e8e04eSSam Leffler 		bo->bo_htinfo = frm;
251868e8e04eSSam Leffler 		frm = ieee80211_add_htinfo(frm, ni);
25191b6167d2SSam Leffler 	}
2520b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA1) {
2521b032f27cSSam Leffler 		if (vap->iv_wpa_ie != NULL)
2522b032f27cSSam Leffler 			frm = add_ie(frm, vap->iv_wpa_ie);
2523b032f27cSSam Leffler 		/* XXX else complain */
2524b032f27cSSam Leffler 	}
2525b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WME) {
25261b6167d2SSam Leffler 		bo->bo_wme = frm;
25271b6167d2SSam Leffler 		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
25281b6167d2SSam Leffler 	}
2529b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2530b032f27cSSam Leffler 	    (vap->iv_flags_ext & IEEE80211_FEXT_HTCOMPAT)) {
253168e8e04eSSam Leffler 		frm = ieee80211_add_htcap_vendor(frm, ni);
253268e8e04eSSam Leffler 		frm = ieee80211_add_htinfo_vendor(frm, ni);
25330912bac9SSam Leffler 	}
2534b032f27cSSam Leffler 	if (vap->iv_appie_beacon != NULL) {
2535b032f27cSSam Leffler 		bo->bo_appie = frm;
2536b032f27cSSam Leffler 		bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
2537b032f27cSSam Leffler 		frm = add_appie(frm, vap->iv_appie_beacon);
2538b032f27cSSam Leffler 	}
2539b105a069SSam Leffler 	bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
2540b032f27cSSam Leffler 	bo->bo_csa_trailer_len = frm - bo->bo_csa;
254168e8e04eSSam Leffler 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2542b032f27cSSam Leffler }
2543b032f27cSSam Leffler 
2544b032f27cSSam Leffler /*
2545b032f27cSSam Leffler  * Allocate a beacon frame and fillin the appropriate bits.
2546b032f27cSSam Leffler  */
2547b032f27cSSam Leffler struct mbuf *
2548b032f27cSSam Leffler ieee80211_beacon_alloc(struct ieee80211_node *ni,
2549b032f27cSSam Leffler 	struct ieee80211_beacon_offsets *bo)
2550b032f27cSSam Leffler {
2551b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2552b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2553b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
2554b032f27cSSam Leffler 	struct ieee80211_frame *wh;
2555b032f27cSSam Leffler 	struct mbuf *m;
2556b032f27cSSam Leffler 	int pktlen;
2557b032f27cSSam Leffler 	uint8_t *frm;
2558b032f27cSSam Leffler 
2559b032f27cSSam Leffler 	/*
2560b032f27cSSam Leffler 	 * beacon frame format
2561b032f27cSSam Leffler 	 *	[8] time stamp
2562b032f27cSSam Leffler 	 *	[2] beacon interval
2563b032f27cSSam Leffler 	 *	[2] cabability information
2564b032f27cSSam Leffler 	 *	[tlv] ssid
2565b032f27cSSam Leffler 	 *	[tlv] supported rates
2566b032f27cSSam Leffler 	 *	[3] parameter set (DS)
2567b032f27cSSam Leffler 	 *	[8] CF parameter set (optional)
2568b032f27cSSam Leffler 	 *	[tlv] parameter set (IBSS/TIM)
2569b032f27cSSam Leffler 	 *	[tlv] country (optional)
2570b032f27cSSam Leffler 	 *	[3] power control (optional)
2571b032f27cSSam Leffler 	 *	[5] channel switch announcement (CSA) (optional)
2572b032f27cSSam Leffler 	 *	[tlv] extended rate phy (ERP)
2573b032f27cSSam Leffler 	 *	[tlv] extended supported rates
2574b032f27cSSam Leffler 	 *	[tlv] RSN parameters
2575b032f27cSSam Leffler 	 *	[tlv] HT capabilities
2576b032f27cSSam Leffler 	 *	[tlv] HT information
2577b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT capabilities (optional)
2578b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT information (optional)
2579b032f27cSSam Leffler 	 * XXX Vendor-specific OIDs (e.g. Atheros)
2580b032f27cSSam Leffler 	 *	[tlv] WPA parameters
2581b032f27cSSam Leffler 	 *	[tlv] WME parameters
2582b032f27cSSam Leffler 	 *	[tlv] application data (optional)
2583b032f27cSSam Leffler 	 * NB: we allocate the max space required for the TIM bitmap.
2584b032f27cSSam Leffler 	 * XXX how big is this?
2585b032f27cSSam Leffler 	 */
2586b032f27cSSam Leffler 	pktlen =   8					/* time stamp */
2587b032f27cSSam Leffler 		 + sizeof(uint16_t)			/* beacon interval */
2588b032f27cSSam Leffler 		 + sizeof(uint16_t)			/* capabilities */
2589b032f27cSSam Leffler 		 + 2 + ni->ni_esslen			/* ssid */
2590b032f27cSSam Leffler 	         + 2 + IEEE80211_RATE_SIZE		/* supported rates */
2591b032f27cSSam Leffler 	         + 2 + 1				/* DS parameters */
2592b032f27cSSam Leffler 		 + 2 + 6				/* CF parameters */
2593b032f27cSSam Leffler 		 + 2 + 4 + vap->iv_tim_len		/* DTIM/IBSSPARMS */
2594b032f27cSSam Leffler 		 + IEEE80211_COUNTRY_MAX_SIZE		/* country */
2595b032f27cSSam Leffler 		 + 2 + 1				/* power control */
2596b032f27cSSam Leffler 	         + sizeof(struct ieee80211_csa_ie)	/* CSA */
2597b032f27cSSam Leffler 		 + 2 + 1				/* ERP */
2598b032f27cSSam Leffler 	         + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2599b032f27cSSam Leffler 		 + (vap->iv_caps & IEEE80211_C_WPA ?	/* WPA 1+2 */
2600b032f27cSSam Leffler 			2*sizeof(struct ieee80211_ie_wpa) : 0)
2601b032f27cSSam Leffler 		 /* XXX conditional? */
2602b032f27cSSam Leffler 		 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
2603b032f27cSSam Leffler 		 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
2604b032f27cSSam Leffler 		 + (vap->iv_caps & IEEE80211_C_WME ?	/* WME */
2605b032f27cSSam Leffler 			sizeof(struct ieee80211_wme_param) : 0)
2606b032f27cSSam Leffler 		 + IEEE80211_MAX_APPIE
2607b032f27cSSam Leffler 		 ;
2608b032f27cSSam Leffler 	m = ieee80211_getmgtframe(&frm,
2609b032f27cSSam Leffler 		ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
2610b032f27cSSam Leffler 	if (m == NULL) {
2611b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
2612b032f27cSSam Leffler 			"%s: cannot get buf; size %u\n", __func__, pktlen);
2613b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
2614b032f27cSSam Leffler 		return NULL;
2615b032f27cSSam Leffler 	}
2616b032f27cSSam Leffler 	ieee80211_beacon_construct(m, frm, bo, ni);
26178a1b9b6aSSam Leffler 
26188a1b9b6aSSam Leffler 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
26198a1b9b6aSSam Leffler 	KASSERT(m != NULL, ("no space for 802.11 header?"));
26208a1b9b6aSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
26218a1b9b6aSSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
26228a1b9b6aSSam Leffler 	    IEEE80211_FC0_SUBTYPE_BEACON;
26238a1b9b6aSSam Leffler 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
262468e8e04eSSam Leffler 	*(uint16_t *)wh->i_dur = 0;
26258a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
2626b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
26278a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
262868e8e04eSSam Leffler 	*(uint16_t *)wh->i_seq = 0;
26298a1b9b6aSSam Leffler 
26308a1b9b6aSSam Leffler 	return m;
26318a1b9b6aSSam Leffler }
26328a1b9b6aSSam Leffler 
26338a1b9b6aSSam Leffler /*
26348a1b9b6aSSam Leffler  * Update the dynamic parts of a beacon frame based on the current state.
26358a1b9b6aSSam Leffler  */
26368a1b9b6aSSam Leffler int
2637b105a069SSam Leffler ieee80211_beacon_update(struct ieee80211_node *ni,
26388a1b9b6aSSam Leffler 	struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
26398a1b9b6aSSam Leffler {
2640b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2641b105a069SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
26428a1b9b6aSSam Leffler 	int len_changed = 0;
264368e8e04eSSam Leffler 	uint16_t capinfo;
26448a1b9b6aSSam Leffler 
2645b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2646b032f27cSSam Leffler 	/*
2647b032f27cSSam Leffler 	 * Handle 11h channel change when we've reached the count.
2648b032f27cSSam Leffler 	 * We must recalculate the beacon frame contents to account
2649b032f27cSSam Leffler 	 * for the new channel.  Note we do this only for the first
2650b032f27cSSam Leffler 	 * vap that reaches this point; subsequent vaps just update
2651b032f27cSSam Leffler 	 * their beacon state to reflect the recalculated channel.
2652b032f27cSSam Leffler 	 */
2653b032f27cSSam Leffler 	if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
2654b032f27cSSam Leffler 	    vap->iv_csa_count == ic->ic_csa_count) {
2655b032f27cSSam Leffler 		vap->iv_csa_count = 0;
2656b032f27cSSam Leffler 		/*
2657b032f27cSSam Leffler 		 * Effect channel change before reconstructing the beacon
2658b032f27cSSam Leffler 		 * frame contents as many places reference ni_chan.
2659b032f27cSSam Leffler 		 */
2660b032f27cSSam Leffler 		if (ic->ic_csa_newchan != NULL)
2661b032f27cSSam Leffler 			ieee80211_csa_completeswitch(ic);
2662b032f27cSSam Leffler 		/*
2663b032f27cSSam Leffler 		 * NB: ieee80211_beacon_construct clears all pending
2664b032f27cSSam Leffler 		 * updates in bo_flags so we don't need to explicitly
2665b032f27cSSam Leffler 		 * clear IEEE80211_BEACON_CSA.
2666b032f27cSSam Leffler 		 */
2667b032f27cSSam Leffler 		ieee80211_beacon_construct(m,
2668b032f27cSSam Leffler 		    mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), bo, ni);
2669b032f27cSSam Leffler 
2670b032f27cSSam Leffler 		/* XXX do WME aggressive mode processing? */
2671b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
2672b032f27cSSam Leffler 		return 1;		/* just assume length changed */
2673b032f27cSSam Leffler 	}
2674b032f27cSSam Leffler 
26758a1b9b6aSSam Leffler 	/* XXX faster to recalculate entirely or just changes? */
2676b032f27cSSam Leffler 	capinfo = getcapinfo(vap, ni->ni_chan);
26778a1b9b6aSSam Leffler 	*bo->bo_caps = htole16(capinfo);
26788a1b9b6aSSam Leffler 
2679b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WME) {
26808a1b9b6aSSam Leffler 		struct ieee80211_wme_state *wme = &ic->ic_wme;
26818a1b9b6aSSam Leffler 
26828a1b9b6aSSam Leffler 		/*
26838a1b9b6aSSam Leffler 		 * Check for agressive mode change.  When there is
26848a1b9b6aSSam Leffler 		 * significant high priority traffic in the BSS
26858a1b9b6aSSam Leffler 		 * throttle back BE traffic by using conservative
26868a1b9b6aSSam Leffler 		 * parameters.  Otherwise BE uses agressive params
26878a1b9b6aSSam Leffler 		 * to optimize performance of legacy/non-QoS traffic.
26888a1b9b6aSSam Leffler 		 */
26898a1b9b6aSSam Leffler 		if (wme->wme_flags & WME_F_AGGRMODE) {
26908a1b9b6aSSam Leffler 			if (wme->wme_hipri_traffic >
26918a1b9b6aSSam Leffler 			    wme->wme_hipri_switch_thresh) {
2692b032f27cSSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
26938a1b9b6aSSam Leffler 				    "%s: traffic %u, disable aggressive mode\n",
26948a1b9b6aSSam Leffler 				    __func__, wme->wme_hipri_traffic);
26958a1b9b6aSSam Leffler 				wme->wme_flags &= ~WME_F_AGGRMODE;
2696b032f27cSSam Leffler 				ieee80211_wme_updateparams_locked(vap);
26978a1b9b6aSSam Leffler 				wme->wme_hipri_traffic =
26988a1b9b6aSSam Leffler 					wme->wme_hipri_switch_hysteresis;
26998a1b9b6aSSam Leffler 			} else
27008a1b9b6aSSam Leffler 				wme->wme_hipri_traffic = 0;
27018a1b9b6aSSam Leffler 		} else {
27028a1b9b6aSSam Leffler 			if (wme->wme_hipri_traffic <=
27038a1b9b6aSSam Leffler 			    wme->wme_hipri_switch_thresh) {
2704b032f27cSSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
27058a1b9b6aSSam Leffler 				    "%s: traffic %u, enable aggressive mode\n",
27068a1b9b6aSSam Leffler 				    __func__, wme->wme_hipri_traffic);
27078a1b9b6aSSam Leffler 				wme->wme_flags |= WME_F_AGGRMODE;
2708b032f27cSSam Leffler 				ieee80211_wme_updateparams_locked(vap);
27098a1b9b6aSSam Leffler 				wme->wme_hipri_traffic = 0;
27108a1b9b6aSSam Leffler 			} else
27118a1b9b6aSSam Leffler 				wme->wme_hipri_traffic =
27128a1b9b6aSSam Leffler 					wme->wme_hipri_switch_hysteresis;
27138a1b9b6aSSam Leffler 		}
2714b105a069SSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
27158a1b9b6aSSam Leffler 			(void) ieee80211_add_wme_param(bo->bo_wme, wme);
2716b105a069SSam Leffler 			clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
27178a1b9b6aSSam Leffler 		}
27188a1b9b6aSSam Leffler 	}
27198a1b9b6aSSam Leffler 
2720b105a069SSam Leffler 	if (isset(bo->bo_flags,  IEEE80211_BEACON_HTINFO)) {
2721b032f27cSSam Leffler 		ieee80211_ht_update_beacon(vap, bo);
2722b105a069SSam Leffler 		clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
272368e8e04eSSam Leffler 	}
272468e8e04eSSam Leffler 
2725b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {	/* NB: no IBSS support*/
27268a1b9b6aSSam Leffler 		struct ieee80211_tim_ie *tie =
27278a1b9b6aSSam Leffler 			(struct ieee80211_tim_ie *) bo->bo_tim;
2728b105a069SSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
27298a1b9b6aSSam Leffler 			u_int timlen, timoff, i;
27308a1b9b6aSSam Leffler 			/*
27318a1b9b6aSSam Leffler 			 * ATIM/DTIM needs updating.  If it fits in the
27328a1b9b6aSSam Leffler 			 * current space allocated then just copy in the
27338a1b9b6aSSam Leffler 			 * new bits.  Otherwise we need to move any trailing
27348a1b9b6aSSam Leffler 			 * data to make room.  Note that we know there is
27358a1b9b6aSSam Leffler 			 * contiguous space because ieee80211_beacon_allocate
27368a1b9b6aSSam Leffler 			 * insures there is space in the mbuf to write a
2737b032f27cSSam Leffler 			 * maximal-size virtual bitmap (based on iv_max_aid).
27388a1b9b6aSSam Leffler 			 */
27398a1b9b6aSSam Leffler 			/*
27408a1b9b6aSSam Leffler 			 * Calculate the bitmap size and offset, copy any
27418a1b9b6aSSam Leffler 			 * trailer out of the way, and then copy in the
27428a1b9b6aSSam Leffler 			 * new bitmap and update the information element.
27438a1b9b6aSSam Leffler 			 * Note that the tim bitmap must contain at least
27448a1b9b6aSSam Leffler 			 * one byte and any offset must be even.
27458a1b9b6aSSam Leffler 			 */
2746b032f27cSSam Leffler 			if (vap->iv_ps_pending != 0) {
27478a1b9b6aSSam Leffler 				timoff = 128;		/* impossibly large */
2748b032f27cSSam Leffler 				for (i = 0; i < vap->iv_tim_len; i++)
2749b032f27cSSam Leffler 					if (vap->iv_tim_bitmap[i]) {
27508a1b9b6aSSam Leffler 						timoff = i &~ 1;
27518a1b9b6aSSam Leffler 						break;
27528a1b9b6aSSam Leffler 					}
27538a1b9b6aSSam Leffler 				KASSERT(timoff != 128, ("tim bitmap empty!"));
2754b032f27cSSam Leffler 				for (i = vap->iv_tim_len-1; i >= timoff; i--)
2755b032f27cSSam Leffler 					if (vap->iv_tim_bitmap[i])
27568a1b9b6aSSam Leffler 						break;
27578a1b9b6aSSam Leffler 				timlen = 1 + (i - timoff);
27588a1b9b6aSSam Leffler 			} else {
27598a1b9b6aSSam Leffler 				timoff = 0;
27608a1b9b6aSSam Leffler 				timlen = 1;
27618a1b9b6aSSam Leffler 			}
27628a1b9b6aSSam Leffler 			if (timlen != bo->bo_tim_len) {
27638a1b9b6aSSam Leffler 				/* copy up/down trailer */
27640912bac9SSam Leffler 				int adjust = tie->tim_bitmap+timlen
2765b105a069SSam Leffler 					   - bo->bo_tim_trailer;
2766b105a069SSam Leffler 				ovbcopy(bo->bo_tim_trailer,
2767b105a069SSam Leffler 				    bo->bo_tim_trailer+adjust,
2768b105a069SSam Leffler 				    bo->bo_tim_trailer_len);
2769b105a069SSam Leffler 				bo->bo_tim_trailer += adjust;
27700912bac9SSam Leffler 				bo->bo_erp += adjust;
277168e8e04eSSam Leffler 				bo->bo_htinfo += adjust;
2772b032f27cSSam Leffler 				bo->bo_appie += adjust;
2773b032f27cSSam Leffler 				bo->bo_wme += adjust;
2774b032f27cSSam Leffler 				bo->bo_csa += adjust;
27758a1b9b6aSSam Leffler 				bo->bo_tim_len = timlen;
27768a1b9b6aSSam Leffler 
27778a1b9b6aSSam Leffler 				/* update information element */
27788a1b9b6aSSam Leffler 				tie->tim_len = 3 + timlen;
27798a1b9b6aSSam Leffler 				tie->tim_bitctl = timoff;
27808a1b9b6aSSam Leffler 				len_changed = 1;
27818a1b9b6aSSam Leffler 			}
2782b032f27cSSam Leffler 			memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
27838a1b9b6aSSam Leffler 				bo->bo_tim_len);
27848a1b9b6aSSam Leffler 
2785b105a069SSam Leffler 			clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
27868a1b9b6aSSam Leffler 
2787b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
27888a1b9b6aSSam Leffler 				"%s: TIM updated, pending %u, off %u, len %u\n",
2789b032f27cSSam Leffler 				__func__, vap->iv_ps_pending, timoff, timlen);
27908a1b9b6aSSam Leffler 		}
27918a1b9b6aSSam Leffler 		/* count down DTIM period */
27928a1b9b6aSSam Leffler 		if (tie->tim_count == 0)
27938a1b9b6aSSam Leffler 			tie->tim_count = tie->tim_period - 1;
27948a1b9b6aSSam Leffler 		else
27958a1b9b6aSSam Leffler 			tie->tim_count--;
27968a1b9b6aSSam Leffler 		/* update state for buffered multicast frames on DTIM */
2797a196b35fSSam Leffler 		if (mcast && tie->tim_count == 0)
27988a1b9b6aSSam Leffler 			tie->tim_bitctl |= 1;
27998a1b9b6aSSam Leffler 		else
28008a1b9b6aSSam Leffler 			tie->tim_bitctl &= ~1;
2801b032f27cSSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
2802b032f27cSSam Leffler 			struct ieee80211_csa_ie *csa =
2803b032f27cSSam Leffler 			    (struct ieee80211_csa_ie *) bo->bo_csa;
2804b032f27cSSam Leffler 
2805b032f27cSSam Leffler 			/*
2806b032f27cSSam Leffler 			 * Insert or update CSA ie.  If we're just starting
2807b032f27cSSam Leffler 			 * to count down to the channel switch then we need
2808b032f27cSSam Leffler 			 * to insert the CSA ie.  Otherwise we just need to
2809b032f27cSSam Leffler 			 * drop the count.  The actual change happens above
2810b032f27cSSam Leffler 			 * when the vap's count reaches the target count.
2811b032f27cSSam Leffler 			 */
2812b032f27cSSam Leffler 			if (vap->iv_csa_count == 0) {
2813b032f27cSSam Leffler 				memmove(&csa[1], csa, bo->bo_csa_trailer_len);
2814b032f27cSSam Leffler 				bo->bo_erp += sizeof(*csa);
2815b032f27cSSam Leffler 				bo->bo_wme += sizeof(*csa);
2816b032f27cSSam Leffler 				bo->bo_appie += sizeof(*csa);
2817b032f27cSSam Leffler 				bo->bo_csa_trailer_len += sizeof(*csa);
2818b032f27cSSam Leffler 				bo->bo_tim_trailer_len += sizeof(*csa);
2819b032f27cSSam Leffler 				m->m_len += sizeof(*csa);
2820b032f27cSSam Leffler 				m->m_pkthdr.len += sizeof(*csa);
2821b032f27cSSam Leffler 
2822b032f27cSSam Leffler 				ieee80211_add_csa(bo->bo_csa, vap);
2823b032f27cSSam Leffler 			} else
2824b032f27cSSam Leffler 				csa->csa_count--;
2825b032f27cSSam Leffler 			vap->iv_csa_count++;
2826b032f27cSSam Leffler 			/* NB: don't clear IEEE80211_BEACON_CSA */
2827b032f27cSSam Leffler 		}
2828b105a069SSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
28290912bac9SSam Leffler 			/*
28300912bac9SSam Leffler 			 * ERP element needs updating.
28310912bac9SSam Leffler 			 */
28320912bac9SSam Leffler 			(void) ieee80211_add_erp(bo->bo_erp, ic);
2833b105a069SSam Leffler 			clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
28340912bac9SSam Leffler 		}
28358a1b9b6aSSam Leffler 	}
2836b032f27cSSam Leffler 	if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
2837b032f27cSSam Leffler 		const struct ieee80211_appie *aie = vap->iv_appie_beacon;
2838b032f27cSSam Leffler 		int aielen;
2839b032f27cSSam Leffler 		uint8_t *frm;
2840b032f27cSSam Leffler 
2841b032f27cSSam Leffler 		aielen = 0;
2842b032f27cSSam Leffler 		if (aie != NULL)
2843b032f27cSSam Leffler 			aielen += aie->ie_len;
2844b032f27cSSam Leffler 		if (aielen != bo->bo_appie_len) {
2845b032f27cSSam Leffler 			/* copy up/down trailer */
2846b032f27cSSam Leffler 			int adjust = aielen - bo->bo_appie_len;
2847b032f27cSSam Leffler 			ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
2848b032f27cSSam Leffler 				bo->bo_tim_trailer_len);
2849b032f27cSSam Leffler 			bo->bo_tim_trailer += adjust;
2850b032f27cSSam Leffler 			bo->bo_appie += adjust;
2851b032f27cSSam Leffler 			bo->bo_appie_len = aielen;
2852b032f27cSSam Leffler 
2853b032f27cSSam Leffler 			len_changed = 1;
2854b032f27cSSam Leffler 		}
2855b032f27cSSam Leffler 		frm = bo->bo_appie;
2856b032f27cSSam Leffler 		if (aie != NULL)
2857b032f27cSSam Leffler 			frm  = add_appie(frm, aie);
2858b032f27cSSam Leffler 		clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
2859b032f27cSSam Leffler 	}
2860b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
28618a1b9b6aSSam Leffler 
28628a1b9b6aSSam Leffler 	return len_changed;
28638a1b9b6aSSam Leffler }
2864