xref: /freebsd/sys/net80211/ieee80211_output.c (revision e755a73d1943420b549d4799280209e73569c3ee)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
310ad9a77SSam Leffler  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
41a1e1d21SSam Leffler  * All rights reserved.
51a1e1d21SSam Leffler  *
61a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
71a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
81a1e1d21SSam Leffler  * are met:
91a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
107535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
117535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
127535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
137535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
141a1e1d21SSam Leffler  *
157535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
167535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
177535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
187535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
197535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
207535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
217535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
227535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
237535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
247535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
251a1e1d21SSam Leffler  */
261a1e1d21SSam Leffler 
271a1e1d21SSam Leffler #include <sys/cdefs.h>
281a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
291a1e1d21SSam Leffler 
301a1e1d21SSam Leffler #include "opt_inet.h"
31e755a73dSSam Leffler #include "opt_inet6.h"
32b032f27cSSam Leffler #include "opt_wlan.h"
331a1e1d21SSam Leffler 
341a1e1d21SSam Leffler #include <sys/param.h>
351a1e1d21SSam Leffler #include <sys/systm.h>
361a1e1d21SSam Leffler #include <sys/mbuf.h>
371a1e1d21SSam Leffler #include <sys/kernel.h>
381a1e1d21SSam Leffler #include <sys/endian.h>
391a1e1d21SSam Leffler 
408a1b9b6aSSam Leffler #include <sys/socket.h>
411a1e1d21SSam Leffler 
421a1e1d21SSam Leffler #include <net/bpf.h>
438a1b9b6aSSam Leffler #include <net/ethernet.h>
448a1b9b6aSSam Leffler #include <net/if.h>
458a1b9b6aSSam Leffler #include <net/if_llc.h>
468a1b9b6aSSam Leffler #include <net/if_media.h>
478a1b9b6aSSam Leffler #include <net/if_vlan_var.h>
488a1b9b6aSSam Leffler 
498a1b9b6aSSam Leffler #include <net80211/ieee80211_var.h>
5068e8e04eSSam Leffler #include <net80211/ieee80211_regdomain.h>
51616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
52616190d0SSam Leffler #include <net80211/ieee80211_superg.h>
53616190d0SSam Leffler #endif
5410ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
5510ad9a77SSam Leffler #include <net80211/ieee80211_tdma.h>
5610ad9a77SSam Leffler #endif
57b032f27cSSam Leffler #include <net80211/ieee80211_wds.h>
581a1e1d21SSam Leffler 
591a1e1d21SSam Leffler #ifdef INET
601a1e1d21SSam Leffler #include <netinet/in.h>
611a1e1d21SSam Leffler #include <netinet/if_ether.h>
628a1b9b6aSSam Leffler #include <netinet/in_systm.h>
638a1b9b6aSSam Leffler #include <netinet/ip.h>
648a1b9b6aSSam Leffler #endif
65e755a73dSSam Leffler #ifdef INET6
66e755a73dSSam Leffler #include <netinet/ip6.h>
67e755a73dSSam Leffler #endif
688a1b9b6aSSam Leffler 
69c27b9cdbSRobert Watson #include <security/mac/mac_framework.h>
70c27b9cdbSRobert Watson 
7168e8e04eSSam Leffler #define	ETHER_HEADER_COPY(dst, src) \
7268e8e04eSSam Leffler 	memcpy(dst, src, sizeof(struct ether_header))
7368e8e04eSSam Leffler 
74b032f27cSSam Leffler static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
7568e8e04eSSam Leffler 	u_int hdrsize, u_int ciphdrsize, u_int mtu);
7668e8e04eSSam Leffler static	void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
7768e8e04eSSam Leffler 
788a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
798a1b9b6aSSam Leffler /*
808a1b9b6aSSam Leffler  * Decide if an outbound management frame should be
818a1b9b6aSSam Leffler  * printed when debugging is enabled.  This filters some
828a1b9b6aSSam Leffler  * of the less interesting frames that come frequently
838a1b9b6aSSam Leffler  * (e.g. beacons).
848a1b9b6aSSam Leffler  */
858a1b9b6aSSam Leffler static __inline int
86b032f27cSSam Leffler doprint(struct ieee80211vap *vap, int subtype)
878a1b9b6aSSam Leffler {
888a1b9b6aSSam Leffler 	switch (subtype) {
898a1b9b6aSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
90b032f27cSSam Leffler 		return (vap->iv_opmode == IEEE80211_M_IBSS);
918a1b9b6aSSam Leffler 	}
928a1b9b6aSSam Leffler 	return 1;
938a1b9b6aSSam Leffler }
941a1e1d21SSam Leffler #endif
951a1e1d21SSam Leffler 
960a915fadSSam Leffler /*
97b032f27cSSam Leffler  * Start method for vap's.  All packets from the stack come
98b032f27cSSam Leffler  * through here.  We handle common processing of the packets
99b032f27cSSam Leffler  * before dispatching them to the underlying device.
100b032f27cSSam Leffler  */
101b032f27cSSam Leffler void
102b032f27cSSam Leffler ieee80211_start(struct ifnet *ifp)
103b032f27cSSam Leffler {
104b032f27cSSam Leffler #define	IS_DWDS(vap) \
105b032f27cSSam Leffler 	(vap->iv_opmode == IEEE80211_M_WDS && \
106b032f27cSSam Leffler 	 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
107b032f27cSSam Leffler 	struct ieee80211vap *vap = ifp->if_softc;
108b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
109b032f27cSSam Leffler 	struct ifnet *parent = ic->ic_ifp;
110b032f27cSSam Leffler 	struct ieee80211_node *ni;
111b032f27cSSam Leffler 	struct mbuf *m;
112b032f27cSSam Leffler 	struct ether_header *eh;
113b032f27cSSam Leffler 	int error;
114b032f27cSSam Leffler 
115b032f27cSSam Leffler 	/* NB: parent must be up and running */
116b032f27cSSam Leffler 	if (!IFNET_IS_UP_RUNNING(parent)) {
117b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
118b032f27cSSam Leffler 		    "%s: ignore queue, parent %s not up+running\n",
119b032f27cSSam Leffler 		    __func__, parent->if_xname);
120b032f27cSSam Leffler 		/* XXX stat */
121b032f27cSSam Leffler 		return;
122b032f27cSSam Leffler 	}
123b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_SLEEP) {
124b032f27cSSam Leffler 		/*
125b032f27cSSam Leffler 		 * In power save, wakeup device for transmit.
126b032f27cSSam Leffler 		 */
127b032f27cSSam Leffler 		ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
128b032f27cSSam Leffler 		return;
129b032f27cSSam Leffler 	}
130b032f27cSSam Leffler 	/*
131b032f27cSSam Leffler 	 * No data frames go out unless we're running.
132b032f27cSSam Leffler 	 * Note in particular this covers CAC and CSA
133b032f27cSSam Leffler 	 * states (though maybe we should check muting
134b032f27cSSam Leffler 	 * for CSA).
135b032f27cSSam Leffler 	 */
136b032f27cSSam Leffler 	if (vap->iv_state != IEEE80211_S_RUN) {
137b032f27cSSam Leffler 		IEEE80211_LOCK(ic);
138b032f27cSSam Leffler 		/* re-check under the com lock to avoid races */
139b032f27cSSam Leffler 		if (vap->iv_state != IEEE80211_S_RUN) {
140b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
141b032f27cSSam Leffler 			    "%s: ignore queue, in %s state\n",
142b032f27cSSam Leffler 			    __func__, ieee80211_state_name[vap->iv_state]);
143b032f27cSSam Leffler 			vap->iv_stats.is_tx_badstate++;
144b032f27cSSam Leffler 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
145b032f27cSSam Leffler 			IEEE80211_UNLOCK(ic);
146b032f27cSSam Leffler 			return;
147b032f27cSSam Leffler 		}
148b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
149b032f27cSSam Leffler 	}
150b032f27cSSam Leffler 	for (;;) {
151b032f27cSSam Leffler 		IFQ_DEQUEUE(&ifp->if_snd, m);
152b032f27cSSam Leffler 		if (m == NULL)
153b032f27cSSam Leffler 			break;
154b032f27cSSam Leffler 		/*
155b032f27cSSam Leffler 		 * Sanitize mbuf flags for net80211 use.  We cannot
156b032f27cSSam Leffler 		 * clear M_PWR_SAV because this may be set for frames
157b032f27cSSam Leffler 		 * that are re-submitted from the power save queue.
158b032f27cSSam Leffler 		 *
159b032f27cSSam Leffler 		 * NB: This must be done before ieee80211_classify as
160b032f27cSSam Leffler 		 *     it marks EAPOL in frames with M_EAPOL.
161b032f27cSSam Leffler 		 */
162b032f27cSSam Leffler 		m->m_flags &= ~(M_80211_TX - M_PWR_SAV);
163b032f27cSSam Leffler 		/*
164b032f27cSSam Leffler 		 * Cancel any background scan.
165b032f27cSSam Leffler 		 */
166b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SCAN)
167b032f27cSSam Leffler 			ieee80211_cancel_anyscan(vap);
168b032f27cSSam Leffler 		/*
169b032f27cSSam Leffler 		 * Find the node for the destination so we can do
170b032f27cSSam Leffler 		 * things like power save and fast frames aggregation.
171b032f27cSSam Leffler 		 *
172b032f27cSSam Leffler 		 * NB: past this point various code assumes the first
173b032f27cSSam Leffler 		 *     mbuf has the 802.3 header present (and contiguous).
174b032f27cSSam Leffler 		 */
175b032f27cSSam Leffler 		ni = NULL;
176b032f27cSSam Leffler 		if (m->m_len < sizeof(struct ether_header) &&
177b032f27cSSam Leffler 		   (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
178b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
179b032f27cSSam Leffler 			    "discard frame, %s\n", "m_pullup failed");
180b032f27cSSam Leffler 			vap->iv_stats.is_tx_nobuf++;	/* XXX */
181b032f27cSSam Leffler 			ifp->if_oerrors++;
182b032f27cSSam Leffler 			continue;
183b032f27cSSam Leffler 		}
184b032f27cSSam Leffler 		eh = mtod(m, struct ether_header *);
185b032f27cSSam Leffler 		if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
186b032f27cSSam Leffler 			if (IS_DWDS(vap)) {
187b032f27cSSam Leffler 				/*
188b032f27cSSam Leffler 				 * Only unicast frames from the above go out
189b032f27cSSam Leffler 				 * DWDS vaps; multicast frames are handled by
190b032f27cSSam Leffler 				 * dispatching the frame as it comes through
191b032f27cSSam Leffler 				 * the AP vap (see below).
192b032f27cSSam Leffler 				 */
193b032f27cSSam Leffler 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
194b032f27cSSam Leffler 				    eh->ether_dhost, "mcast", "%s", "on DWDS");
195b032f27cSSam Leffler 				vap->iv_stats.is_dwds_mcast++;
196b032f27cSSam Leffler 				m_freem(m);
197b032f27cSSam Leffler 				continue;
198b032f27cSSam Leffler 			}
199b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
200b032f27cSSam Leffler 				/*
201b032f27cSSam Leffler 				 * Spam DWDS vap's w/ multicast traffic.
202b032f27cSSam Leffler 				 */
203b032f27cSSam Leffler 				/* XXX only if dwds in use? */
204b032f27cSSam Leffler 				ieee80211_dwds_mcast(vap, m);
205b032f27cSSam Leffler 			}
206b032f27cSSam Leffler 		}
207b032f27cSSam Leffler 		ni = ieee80211_find_txnode(vap, eh->ether_dhost);
208b032f27cSSam Leffler 		if (ni == NULL) {
209b032f27cSSam Leffler 			/* NB: ieee80211_find_txnode does stat+msg */
210b032f27cSSam Leffler 			ifp->if_oerrors++;
211b032f27cSSam Leffler 			m_freem(m);
212b032f27cSSam Leffler 			continue;
213b032f27cSSam Leffler 		}
2146ff2e10aSSam Leffler 		if (ni->ni_associd == 0 &&
2151b999d64SSam Leffler 		    (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
216b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
217b032f27cSSam Leffler 			    eh->ether_dhost, NULL,
218b032f27cSSam Leffler 			    "sta not associated (type 0x%04x)",
219b032f27cSSam Leffler 			    htons(eh->ether_type));
220b032f27cSSam Leffler 			vap->iv_stats.is_tx_notassoc++;
221b032f27cSSam Leffler 			ifp->if_oerrors++;
222b032f27cSSam Leffler 			m_freem(m);
223b032f27cSSam Leffler 			ieee80211_free_node(ni);
224b032f27cSSam Leffler 			continue;
225b032f27cSSam Leffler 		}
226339ccfb3SSam Leffler 
227b032f27cSSam Leffler 		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
228b032f27cSSam Leffler 		    (m->m_flags & M_PWR_SAV) == 0) {
229b032f27cSSam Leffler 			/*
230b032f27cSSam Leffler 			 * Station in power save mode; pass the frame
231b032f27cSSam Leffler 			 * to the 802.11 layer and continue.  We'll get
232b032f27cSSam Leffler 			 * the frame back when the time is right.
233b032f27cSSam Leffler 			 * XXX lose WDS vap linkage?
234b032f27cSSam Leffler 			 */
23563092fceSSam Leffler 			(void) ieee80211_pwrsave(ni, m);
236b032f27cSSam Leffler 			ieee80211_free_node(ni);
237b032f27cSSam Leffler 			continue;
238b032f27cSSam Leffler 		}
239b032f27cSSam Leffler 		/* calculate priority so drivers can find the tx queue */
240b032f27cSSam Leffler 		if (ieee80211_classify(ni, m)) {
241b032f27cSSam Leffler 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
242b032f27cSSam Leffler 			    eh->ether_dhost, NULL,
243b032f27cSSam Leffler 			    "%s", "classification failure");
244b032f27cSSam Leffler 			vap->iv_stats.is_tx_classify++;
245b032f27cSSam Leffler 			ifp->if_oerrors++;
246b032f27cSSam Leffler 			m_freem(m);
247b032f27cSSam Leffler 			ieee80211_free_node(ni);
248b032f27cSSam Leffler 			continue;
249b032f27cSSam Leffler 		}
2507ebe9c0eSSam Leffler 		/*
2517ebe9c0eSSam Leffler 		 * Stash the node pointer.  Note that we do this after
2527ebe9c0eSSam Leffler 		 * any call to ieee80211_dwds_mcast because that code
2537ebe9c0eSSam Leffler 		 * uses any existing value for rcvif to identify the
2547ebe9c0eSSam Leffler 		 * interface it (might have been) received on.
2557ebe9c0eSSam Leffler 		 */
2567ebe9c0eSSam Leffler 		m->m_pkthdr.rcvif = (void *)ni;
257b032f27cSSam Leffler 
258339ccfb3SSam Leffler 		BPF_MTAP(ifp, m);		/* 802.3 tx */
259b032f27cSSam Leffler 
260168f582eSSam Leffler 		/*
261168f582eSSam Leffler 		 * Check if A-MPDU tx aggregation is setup or if we
262168f582eSSam Leffler 		 * should try to enable it.  The sta must be associated
263168f582eSSam Leffler 		 * with HT and A-MPDU enabled for use.  When the policy
264168f582eSSam Leffler 		 * routine decides we should enable A-MPDU we issue an
265168f582eSSam Leffler 		 * ADDBA request and wait for a reply.  The frame being
266168f582eSSam Leffler 		 * encapsulated will go out w/o using A-MPDU, or possibly
267168f582eSSam Leffler 		 * it might be collected by the driver and held/retransmit.
268168f582eSSam Leffler 		 * The default ic_ampdu_enable routine handles staggering
269168f582eSSam Leffler 		 * ADDBA requests in case the receiver NAK's us or we are
270168f582eSSam Leffler 		 * otherwise unable to establish a BA stream.
271168f582eSSam Leffler 		 */
272168f582eSSam Leffler 		if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
2732bfc8a91SSam Leffler 		    (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX) &&
274168f582eSSam Leffler 		    (m->m_flags & M_EAPOL) == 0) {
275168f582eSSam Leffler 			const int ac = M_WME_GETAC(m);
276168f582eSSam Leffler 			struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[ac];
277168f582eSSam Leffler 
278168f582eSSam Leffler 			ieee80211_txampdu_count_packet(tap);
279168f582eSSam Leffler 			if (IEEE80211_AMPDU_RUNNING(tap)) {
280168f582eSSam Leffler 				/*
281168f582eSSam Leffler 				 * Operational, mark frame for aggregation.
282168f582eSSam Leffler 				 *
283168f582eSSam Leffler 				 * XXX do tx aggregation here
284168f582eSSam Leffler 				 */
285168f582eSSam Leffler 				m->m_flags |= M_AMPDU_MPDU;
286168f582eSSam Leffler 			} else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
287168f582eSSam Leffler 			    ic->ic_ampdu_enable(ni, tap)) {
288168f582eSSam Leffler 				/*
289168f582eSSam Leffler 				 * Not negotiated yet, request service.
290168f582eSSam Leffler 				 */
291168f582eSSam Leffler 				ieee80211_ampdu_request(ni, tap);
292168f582eSSam Leffler 				/* XXX hold frame for reply? */
293168f582eSSam Leffler 			}
294168f582eSSam Leffler 		}
295339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
296168f582eSSam Leffler 		else if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF)) {
297339ccfb3SSam Leffler 			m = ieee80211_ff_check(ni, m);
298339ccfb3SSam Leffler 			if (m == NULL) {
299339ccfb3SSam Leffler 				/* NB: any ni ref held on stageq */
300339ccfb3SSam Leffler 				continue;
301339ccfb3SSam Leffler 			}
302339ccfb3SSam Leffler 		}
303339ccfb3SSam Leffler #endif /* IEEE80211_SUPPORT_SUPERG */
3049fb0fccbSSam Leffler 		if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
305b032f27cSSam Leffler 			/*
306339ccfb3SSam Leffler 			 * Encapsulate the packet in prep for transmission.
307b032f27cSSam Leffler 			 */
308339ccfb3SSam Leffler 			m = ieee80211_encap(vap, ni, m);
309339ccfb3SSam Leffler 			if (m == NULL) {
310339ccfb3SSam Leffler 				/* NB: stat+msg handled in ieee80211_encap */
311339ccfb3SSam Leffler 				ieee80211_free_node(ni);
312339ccfb3SSam Leffler 				continue;
313339ccfb3SSam Leffler 			}
3149fb0fccbSSam Leffler 		}
315b032f27cSSam Leffler 
31672fecb4dSSam Leffler 		error = parent->if_transmit(parent, m);
317b032f27cSSam Leffler 		if (error != 0) {
318b032f27cSSam Leffler 			/* NB: IFQ_HANDOFF reclaims mbuf */
319b032f27cSSam Leffler 			ieee80211_free_node(ni);
320b032f27cSSam Leffler 		} else {
321b032f27cSSam Leffler 			ifp->if_opackets++;
322b032f27cSSam Leffler 		}
323b032f27cSSam Leffler 		ic->ic_lastdata = ticks;
324b032f27cSSam Leffler 	}
325b032f27cSSam Leffler #undef IS_DWDS
326b032f27cSSam Leffler }
327b032f27cSSam Leffler 
328b032f27cSSam Leffler /*
329b032f27cSSam Leffler  * 802.11 output routine. This is (currently) used only to
330b032f27cSSam Leffler  * connect bpf write calls to the 802.11 layer for injecting
331fad788b1SSam Leffler  * raw 802.11 frames.
332b032f27cSSam Leffler  */
333b032f27cSSam Leffler int
334b032f27cSSam Leffler ieee80211_output(struct ifnet *ifp, struct mbuf *m,
335279aa3d4SKip Macy 	struct sockaddr *dst, struct route *ro)
336b032f27cSSam Leffler {
337b032f27cSSam Leffler #define senderr(e) do { error = (e); goto bad;} while (0)
338b032f27cSSam Leffler 	struct ieee80211_node *ni = NULL;
339b032f27cSSam Leffler 	struct ieee80211vap *vap;
340b032f27cSSam Leffler 	struct ieee80211_frame *wh;
341b032f27cSSam Leffler 	int error;
342b032f27cSSam Leffler 
343b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
344b032f27cSSam Leffler 		/*
345b032f27cSSam Leffler 		 * Short-circuit requests if the vap is marked OACTIVE
346b032f27cSSam Leffler 		 * as this is used when tearing down state to indicate
347b032f27cSSam Leffler 		 * the vap may be gone.  This can also happen because a
348b032f27cSSam Leffler 		 * packet came down through ieee80211_start before the
349b032f27cSSam Leffler 		 * vap entered RUN state in which case it's also ok to
350b032f27cSSam Leffler 		 * just drop the frame.  This should not be necessary
351b032f27cSSam Leffler 		 * but callers of if_output don't check OACTIVE.
352b032f27cSSam Leffler 		 */
353b032f27cSSam Leffler 		senderr(ENETDOWN);
354b032f27cSSam Leffler 	}
355b032f27cSSam Leffler 	vap = ifp->if_softc;
356b032f27cSSam Leffler 	/*
357b032f27cSSam Leffler 	 * Hand to the 802.3 code if not tagged as
358b032f27cSSam Leffler 	 * a raw 802.11 frame.
359b032f27cSSam Leffler 	 */
360b032f27cSSam Leffler 	if (dst->sa_family != AF_IEEE80211)
361279aa3d4SKip Macy 		return vap->iv_output(ifp, m, dst, ro);
362b032f27cSSam Leffler #ifdef MAC
363c27b9cdbSRobert Watson 	error = mac_ifnet_check_transmit(ifp, m);
364b032f27cSSam Leffler 	if (error)
365b032f27cSSam Leffler 		senderr(error);
366b032f27cSSam Leffler #endif
367b032f27cSSam Leffler 	if (ifp->if_flags & IFF_MONITOR)
368b032f27cSSam Leffler 		senderr(ENETDOWN);
369b032f27cSSam Leffler 	if (!IFNET_IS_UP_RUNNING(ifp))
370b032f27cSSam Leffler 		senderr(ENETDOWN);
371b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
372b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap,
373b032f27cSSam Leffler 		    IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
374b032f27cSSam Leffler 		    "block %s frame in CAC state\n", "raw data");
375b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
376b032f27cSSam Leffler 		senderr(EIO);		/* XXX */
377b032f27cSSam Leffler 	}
378b032f27cSSam Leffler 	/* XXX bypass bridge, pfil, carp, etc. */
379b032f27cSSam Leffler 
380b032f27cSSam Leffler 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
381b032f27cSSam Leffler 		senderr(EIO);	/* XXX */
382b032f27cSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
383b032f27cSSam Leffler 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
384b032f27cSSam Leffler 	    IEEE80211_FC0_VERSION_0)
385b032f27cSSam Leffler 		senderr(EIO);	/* XXX */
386b032f27cSSam Leffler 
387b032f27cSSam Leffler 	/* locate destination node */
388b032f27cSSam Leffler 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
389b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_NODS:
390b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_FROMDS:
391b032f27cSSam Leffler 		ni = ieee80211_find_txnode(vap, wh->i_addr1);
392b032f27cSSam Leffler 		break;
393b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_TODS:
394b032f27cSSam Leffler 	case IEEE80211_FC1_DIR_DSTODS:
395b032f27cSSam Leffler 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame))
396b032f27cSSam Leffler 			senderr(EIO);	/* XXX */
397b032f27cSSam Leffler 		ni = ieee80211_find_txnode(vap, wh->i_addr3);
398b032f27cSSam Leffler 		break;
399b032f27cSSam Leffler 	default:
400b032f27cSSam Leffler 		senderr(EIO);	/* XXX */
401b032f27cSSam Leffler 	}
402b032f27cSSam Leffler 	if (ni == NULL) {
403b032f27cSSam Leffler 		/*
404b032f27cSSam Leffler 		 * Permit packets w/ bpf params through regardless
405b032f27cSSam Leffler 		 * (see below about sa_len).
406b032f27cSSam Leffler 		 */
407b032f27cSSam Leffler 		if (dst->sa_len == 0)
408b032f27cSSam Leffler 			senderr(EHOSTUNREACH);
409b032f27cSSam Leffler 		ni = ieee80211_ref_node(vap->iv_bss);
410b032f27cSSam Leffler 	}
411b032f27cSSam Leffler 
412b032f27cSSam Leffler 	/*
413b032f27cSSam Leffler 	 * Sanitize mbuf for net80211 flags leaked from above.
414b032f27cSSam Leffler 	 *
415b032f27cSSam Leffler 	 * NB: This must be done before ieee80211_classify as
416b032f27cSSam Leffler 	 *     it marks EAPOL in frames with M_EAPOL.
417b032f27cSSam Leffler 	 */
418b032f27cSSam Leffler 	m->m_flags &= ~M_80211_TX;
419b032f27cSSam Leffler 
420b032f27cSSam Leffler 	/* calculate priority so drivers can find the tx queue */
421b032f27cSSam Leffler 	/* XXX assumes an 802.3 frame */
422b032f27cSSam Leffler 	if (ieee80211_classify(ni, m))
423b032f27cSSam Leffler 		senderr(EIO);		/* XXX */
424b032f27cSSam Leffler 
4250e910c94SSam Leffler 	IEEE80211_NODE_STAT(ni, tx_data);
4260e910c94SSam Leffler 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
4270e910c94SSam Leffler 		IEEE80211_NODE_STAT(ni, tx_mcast);
4280e910c94SSam Leffler 		m->m_flags |= M_MCAST;
4290e910c94SSam Leffler 	} else
4300e910c94SSam Leffler 		IEEE80211_NODE_STAT(ni, tx_ucast);
4310e910c94SSam Leffler 	/* NB: ieee80211_encap does not include 802.11 header */
4320e910c94SSam Leffler 	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, m->m_pkthdr.len);
433b032f27cSSam Leffler 
434b032f27cSSam Leffler 	/*
435b032f27cSSam Leffler 	 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
436b032f27cSSam Leffler 	 * present by setting the sa_len field of the sockaddr (yes,
437b032f27cSSam Leffler 	 * this is a hack).
438b032f27cSSam Leffler 	 * NB: we assume sa_data is suitably aligned to cast.
439b032f27cSSam Leffler 	 */
440b032f27cSSam Leffler 	return vap->iv_ic->ic_raw_xmit(ni, m,
441b032f27cSSam Leffler 	    (const struct ieee80211_bpf_params *)(dst->sa_len ?
442b032f27cSSam Leffler 		dst->sa_data : NULL));
443b032f27cSSam Leffler bad:
444b032f27cSSam Leffler 	if (m != NULL)
445b032f27cSSam Leffler 		m_freem(m);
446b032f27cSSam Leffler 	if (ni != NULL)
447b032f27cSSam Leffler 		ieee80211_free_node(ni);
448b032f27cSSam Leffler 	return error;
449b032f27cSSam Leffler #undef senderr
450b032f27cSSam Leffler }
451b032f27cSSam Leffler 
452b032f27cSSam Leffler /*
453add59d08SSam Leffler  * Set the direction field and address fields of an outgoing
4540e66722dSSam Leffler  * frame.  Note this should be called early on in constructing
4550e66722dSSam Leffler  * a frame as it sets i_fc[1]; other bits can then be or'd in.
456add59d08SSam Leffler  */
457add59d08SSam Leffler static void
458b032f27cSSam Leffler ieee80211_send_setup(
459add59d08SSam Leffler 	struct ieee80211_node *ni,
4609e80b1dfSSam Leffler 	struct mbuf *m,
4618ac160cdSSam Leffler 	int type, int tid,
46268e8e04eSSam Leffler 	const uint8_t sa[IEEE80211_ADDR_LEN],
46368e8e04eSSam Leffler 	const uint8_t da[IEEE80211_ADDR_LEN],
46468e8e04eSSam Leffler 	const uint8_t bssid[IEEE80211_ADDR_LEN])
465add59d08SSam Leffler {
466add59d08SSam Leffler #define	WH4(wh)	((struct ieee80211_frame_addr4 *)wh)
4679e80b1dfSSam Leffler 	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
4689e80b1dfSSam Leffler 	ieee80211_seq seqno;
469add59d08SSam Leffler 
470add59d08SSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
471add59d08SSam Leffler 	if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
472b032f27cSSam Leffler 		struct ieee80211vap *vap = ni->ni_vap;
473b032f27cSSam Leffler 
474b032f27cSSam Leffler 		switch (vap->iv_opmode) {
475add59d08SSam Leffler 		case IEEE80211_M_STA:
476add59d08SSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
477add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
478add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
479add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, da);
480add59d08SSam Leffler 			break;
481add59d08SSam Leffler 		case IEEE80211_M_IBSS:
482add59d08SSam Leffler 		case IEEE80211_M_AHDEMO:
483add59d08SSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
484add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
485add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
486add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
487add59d08SSam Leffler 			break;
488add59d08SSam Leffler 		case IEEE80211_M_HOSTAP:
489add59d08SSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
490add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
491add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
492add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, sa);
493add59d08SSam Leffler 			break;
49468e8e04eSSam Leffler 		case IEEE80211_M_WDS:
49568e8e04eSSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
496b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
497b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
49868e8e04eSSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, da);
49968e8e04eSSam Leffler 			IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
50068e8e04eSSam Leffler 			break;
501add59d08SSam Leffler 		case IEEE80211_M_MONITOR:	/* NB: to quiet compiler */
502add59d08SSam Leffler 			break;
503add59d08SSam Leffler 		}
504add59d08SSam Leffler 	} else {
505add59d08SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
506add59d08SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, da);
507add59d08SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, sa);
508add59d08SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
509add59d08SSam Leffler 	}
51068e8e04eSSam Leffler 	*(uint16_t *)&wh->i_dur[0] = 0;
5119e80b1dfSSam Leffler 
5129e80b1dfSSam Leffler 	seqno = ni->ni_txseqs[tid]++;
5139e80b1dfSSam Leffler 	*(uint16_t *)&wh->i_seq[0] = htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
5149a841c7fSSam Leffler 	M_SEQNO_SET(m, seqno);
5159e80b1dfSSam Leffler 
5169e80b1dfSSam Leffler 	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
5179e80b1dfSSam Leffler 		m->m_flags |= M_MCAST;
518add59d08SSam Leffler #undef WH4
519add59d08SSam Leffler }
520add59d08SSam Leffler 
521add59d08SSam Leffler /*
5220a915fadSSam Leffler  * Send a management frame to the specified node.  The node pointer
5230a915fadSSam Leffler  * must have a reference as the pointer will be passed to the driver
5240a915fadSSam Leffler  * and potentially held for a long time.  If the frame is successfully
5250a915fadSSam Leffler  * dispatched to the driver, then it is responsible for freeing the
526b032f27cSSam Leffler  * reference (and potentially free'ing up any associated storage);
527b032f27cSSam Leffler  * otherwise deal with reclaiming any reference (on error).
5280a915fadSSam Leffler  */
52968e8e04eSSam Leffler int
5308ac160cdSSam Leffler ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
5318ac160cdSSam Leffler 	struct ieee80211_bpf_params *params)
5321a1e1d21SSam Leffler {
533b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
534b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
5351a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
5361a1e1d21SSam Leffler 
5370a915fadSSam Leffler 	KASSERT(ni != NULL, ("null node"));
5381a1e1d21SSam Leffler 
539b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
540b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
541b032f27cSSam Leffler 		    ni, "block %s frame in CAC state",
542b032f27cSSam Leffler 			ieee80211_mgt_subtype_name[
543b032f27cSSam Leffler 			    (type & IEEE80211_FC0_SUBTYPE_MASK) >>
544b032f27cSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT]);
545b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
546b032f27cSSam Leffler 		ieee80211_free_node(ni);
547b032f27cSSam Leffler 		m_freem(m);
548b032f27cSSam Leffler 		return EIO;		/* XXX */
549b032f27cSSam Leffler 	}
550b032f27cSSam Leffler 
5511a1e1d21SSam Leffler 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
552b032f27cSSam Leffler 	if (m == NULL) {
553b032f27cSSam Leffler 		ieee80211_free_node(ni);
5541a1e1d21SSam Leffler 		return ENOMEM;
555b032f27cSSam Leffler 	}
5560a915fadSSam Leffler 
5571a1e1d21SSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
5589e80b1dfSSam Leffler 	ieee80211_send_setup(ni, m,
5598ac160cdSSam Leffler 	     IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
560b032f27cSSam Leffler 	     vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
5618ac160cdSSam Leffler 	if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
562b032f27cSSam Leffler 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
563b032f27cSSam Leffler 		    "encrypting frame (%s)", __func__);
5648a1b9b6aSSam Leffler 		wh->i_fc[1] |= IEEE80211_FC1_WEP;
5658a1b9b6aSSam Leffler 	}
566c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
5678ac160cdSSam Leffler 
5688ac160cdSSam Leffler 	KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
5698ac160cdSSam Leffler 	M_WME_SETAC(m, params->ibp_pri);
5708ac160cdSSam Leffler 
5718a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
5728a1b9b6aSSam Leffler 	/* avoid printing too many frames */
573b032f27cSSam Leffler 	if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
574b032f27cSSam Leffler 	    ieee80211_msg_dumppkts(vap)) {
5758a1b9b6aSSam Leffler 		printf("[%s] send %s on channel %u\n",
5768a1b9b6aSSam Leffler 		    ether_sprintf(wh->i_addr1),
5778a1b9b6aSSam Leffler 		    ieee80211_mgt_subtype_name[
5788a1b9b6aSSam Leffler 			(type & IEEE80211_FC0_SUBTYPE_MASK) >>
5798a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
580b5c99415SSam Leffler 		    ieee80211_chan2ieee(ic, ic->ic_curchan));
5818a1b9b6aSSam Leffler 	}
5828a1b9b6aSSam Leffler #endif
5838a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_mgmt);
58468e8e04eSSam Leffler 
5858ac160cdSSam Leffler 	return ic->ic_raw_xmit(ni, m, params);
586246b5467SSam Leffler }
587246b5467SSam Leffler 
588246b5467SSam Leffler /*
5896683931eSSam Leffler  * Send a null data frame to the specified node.  If the station
5906683931eSSam Leffler  * is setup for QoS then a QoS Null Data frame is constructed.
5916683931eSSam Leffler  * If this is a WDS station then a 4-address frame is constructed.
59219ad2dd7SSam Leffler  *
59319ad2dd7SSam Leffler  * NB: the caller is assumed to have setup a node reference
59419ad2dd7SSam Leffler  *     for use; this is necessary to deal with a race condition
595b032f27cSSam Leffler  *     when probing for inactive stations.  Like ieee80211_mgmt_output
596b032f27cSSam Leffler  *     we must cleanup any node reference on error;  however we
597b032f27cSSam Leffler  *     can safely just unref it as we know it will never be the
598b032f27cSSam Leffler  *     last reference to the node.
5998a1b9b6aSSam Leffler  */
6008a1b9b6aSSam Leffler int
601f62121ceSSam Leffler ieee80211_send_nulldata(struct ieee80211_node *ni)
6028a1b9b6aSSam Leffler {
603b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
604f62121ceSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
6058a1b9b6aSSam Leffler 	struct mbuf *m;
6068a1b9b6aSSam Leffler 	struct ieee80211_frame *wh;
6076683931eSSam Leffler 	int hdrlen;
6086683931eSSam Leffler 	uint8_t *frm;
6098a1b9b6aSSam Leffler 
610b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
611b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
612b032f27cSSam Leffler 		    ni, "block %s frame in CAC state", "null data");
613b032f27cSSam Leffler 		ieee80211_unref_node(&ni);
614b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
615b032f27cSSam Leffler 		return EIO;		/* XXX */
616b032f27cSSam Leffler 	}
617b032f27cSSam Leffler 
6186683931eSSam Leffler 	if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
6196683931eSSam Leffler 		hdrlen = sizeof(struct ieee80211_qosframe);
6206683931eSSam Leffler 	else
6216683931eSSam Leffler 		hdrlen = sizeof(struct ieee80211_frame);
6226683931eSSam Leffler 	/* NB: only WDS vap's get 4-address frames */
6236683931eSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_WDS)
6246683931eSSam Leffler 		hdrlen += IEEE80211_ADDR_LEN;
6256683931eSSam Leffler 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
6266683931eSSam Leffler 		hdrlen = roundup(hdrlen, sizeof(uint32_t));
6276683931eSSam Leffler 
6286683931eSSam Leffler 	m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
6298a1b9b6aSSam Leffler 	if (m == NULL) {
6308a1b9b6aSSam Leffler 		/* XXX debug msg */
63119ad2dd7SSam Leffler 		ieee80211_unref_node(&ni);
632b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
6338a1b9b6aSSam Leffler 		return ENOMEM;
6348a1b9b6aSSam Leffler 	}
6356683931eSSam Leffler 	KASSERT(M_LEADINGSPACE(m) >= hdrlen,
6366683931eSSam Leffler 	    ("leading space %zd", M_LEADINGSPACE(m)));
6376683931eSSam Leffler 	M_PREPEND(m, hdrlen, M_DONTWAIT);
6386683931eSSam Leffler 	if (m == NULL) {
6396683931eSSam Leffler 		/* NB: cannot happen */
6406683931eSSam Leffler 		ieee80211_free_node(ni);
6416683931eSSam Leffler 		return ENOMEM;
6426683931eSSam Leffler 	}
6438a1b9b6aSSam Leffler 
6446683931eSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);		/* NB: a little lie */
6456683931eSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
6466683931eSSam Leffler 		const int tid = WME_AC_TO_TID(WME_AC_BE);
6476683931eSSam Leffler 		uint8_t *qos;
6486683931eSSam Leffler 
6499e80b1dfSSam Leffler 		ieee80211_send_setup(ni, m,
6506683931eSSam Leffler 		    IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
6516683931eSSam Leffler 		    tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
6526683931eSSam Leffler 
6536683931eSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_WDS)
6546683931eSSam Leffler 			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
6556683931eSSam Leffler 		else
6566683931eSSam Leffler 			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
6576683931eSSam Leffler 		qos[0] = tid & IEEE80211_QOS_TID;
6586683931eSSam Leffler 		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
6596683931eSSam Leffler 			qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
6606683931eSSam Leffler 		qos[1] = 0;
6616683931eSSam Leffler 	} else {
6629e80b1dfSSam Leffler 		ieee80211_send_setup(ni, m,
663add59d08SSam Leffler 		    IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
6648ac160cdSSam Leffler 		    IEEE80211_NONQOS_TID,
665b032f27cSSam Leffler 		    vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
6666683931eSSam Leffler 	}
667b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_WDS) {
668add59d08SSam Leffler 		/* NB: power management bit is never sent by an AP */
669add59d08SSam Leffler 		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
670b032f27cSSam Leffler 		    vap->iv_opmode != IEEE80211_M_HOSTAP)
671add59d08SSam Leffler 			wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
672b032f27cSSam Leffler 	}
6736683931eSSam Leffler 	m->m_len = m->m_pkthdr.len = hdrlen;
674c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
6756683931eSSam Leffler 
676bb239ce9SSam Leffler 	M_WME_SETAC(m, WME_AC_BE);
6778a1b9b6aSSam Leffler 
6788a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_data);
6798a1b9b6aSSam Leffler 
680b032f27cSSam Leffler 	IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
6816683931eSSam Leffler 	    "send %snull data frame on channel %u, pwr mgt %s",
6826683931eSSam Leffler 	    ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
683b5c99415SSam Leffler 	    ieee80211_chan2ieee(ic, ic->ic_curchan),
684add59d08SSam Leffler 	    wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
685add59d08SSam Leffler 
686b032f27cSSam Leffler 	return ic->ic_raw_xmit(ni, m, NULL);
6878a1b9b6aSSam Leffler }
6888a1b9b6aSSam Leffler 
6898a1b9b6aSSam Leffler /*
6908a1b9b6aSSam Leffler  * Assign priority to a frame based on any vlan tag assigned
6918a1b9b6aSSam Leffler  * to the station and/or any Diffserv setting in an IP header.
6928a1b9b6aSSam Leffler  * Finally, if an ACM policy is setup (in station mode) it's
6938a1b9b6aSSam Leffler  * applied.
6948a1b9b6aSSam Leffler  */
6958a1b9b6aSSam Leffler int
696b032f27cSSam Leffler ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
6978a1b9b6aSSam Leffler {
698b032f27cSSam Leffler 	const struct ether_header *eh = mtod(m, struct ether_header *);
6998a1b9b6aSSam Leffler 	int v_wme_ac, d_wme_ac, ac;
7008a1b9b6aSSam Leffler 
701b032f27cSSam Leffler 	/*
702b032f27cSSam Leffler 	 * Always promote PAE/EAPOL frames to high priority.
703b032f27cSSam Leffler 	 */
704b032f27cSSam Leffler 	if (eh->ether_type == htons(ETHERTYPE_PAE)) {
705b032f27cSSam Leffler 		/* NB: mark so others don't need to check header */
706b032f27cSSam Leffler 		m->m_flags |= M_EAPOL;
707b032f27cSSam Leffler 		ac = WME_AC_VO;
708b032f27cSSam Leffler 		goto done;
709b032f27cSSam Leffler 	}
710b032f27cSSam Leffler 	/*
711b032f27cSSam Leffler 	 * Non-qos traffic goes to BE.
712b032f27cSSam Leffler 	 */
7138a1b9b6aSSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
7148a1b9b6aSSam Leffler 		ac = WME_AC_BE;
7158a1b9b6aSSam Leffler 		goto done;
7168a1b9b6aSSam Leffler 	}
7178a1b9b6aSSam Leffler 
7188a1b9b6aSSam Leffler 	/*
7198a1b9b6aSSam Leffler 	 * If node has a vlan tag then all traffic
7208a1b9b6aSSam Leffler 	 * to it must have a matching tag.
7218a1b9b6aSSam Leffler 	 */
7228a1b9b6aSSam Leffler 	v_wme_ac = 0;
7238a1b9b6aSSam Leffler 	if (ni->ni_vlan != 0) {
72478ba57b9SAndre Oppermann 		 if ((m->m_flags & M_VLANTAG) == 0) {
7258a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_novlantag);
7268a1b9b6aSSam Leffler 			return 1;
7278a1b9b6aSSam Leffler 		}
72878ba57b9SAndre Oppermann 		if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
7298a1b9b6aSSam Leffler 		    EVL_VLANOFTAG(ni->ni_vlan)) {
7308a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
7318a1b9b6aSSam Leffler 			return 1;
7328a1b9b6aSSam Leffler 		}
7338a1b9b6aSSam Leffler 		/* map vlan priority to AC */
734f4558c9aSSam Leffler 		v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
7358a1b9b6aSSam Leffler 	}
7368a1b9b6aSSam Leffler 
737e755a73dSSam Leffler 	/* XXX m_copydata may be too slow for fast path */
7388a1b9b6aSSam Leffler #ifdef INET
7398a1b9b6aSSam Leffler 	if (eh->ether_type == htons(ETHERTYPE_IP)) {
740f4558c9aSSam Leffler 		uint8_t tos;
7418a1b9b6aSSam Leffler 		/*
742f4558c9aSSam Leffler 		 * IP frame, map the DSCP bits from the TOS field.
7438a1b9b6aSSam Leffler 		 */
744f4558c9aSSam Leffler 		/* NB: ip header may not be in first mbuf */
745f4558c9aSSam Leffler 		m_copydata(m, sizeof(struct ether_header) +
746f4558c9aSSam Leffler 		    offsetof(struct ip, ip_tos), sizeof(tos), &tos);
747f4558c9aSSam Leffler 		tos >>= 5;		/* NB: ECN + low 3 bits of DSCP */
748f4558c9aSSam Leffler 		d_wme_ac = TID_TO_WME_AC(tos);
7498a1b9b6aSSam Leffler 	} else {
7508a1b9b6aSSam Leffler #endif /* INET */
751e755a73dSSam Leffler #ifdef INET6
752e755a73dSSam Leffler 	if (eh->ether_type == htons(ETHERTYPE_IPV6)) {
753e755a73dSSam Leffler 		uint32_t flow;
754e755a73dSSam Leffler 		uint8_t tos;
755e755a73dSSam Leffler 		/*
756e755a73dSSam Leffler 		 * IPv6 frame, map the DSCP bits from the TOS field.
757e755a73dSSam Leffler 		 */
758e755a73dSSam Leffler 		m_copydata(m, sizeof(struct ether_header) +
759e755a73dSSam Leffler 		    offsetof(struct ip6_hdr, ip6_flow), sizeof(flow),
760e755a73dSSam Leffler 		    (caddr_t) &flow);
761e755a73dSSam Leffler 		tos = (uint8_t)(ntohl(flow) >> 20);
762e755a73dSSam Leffler 		tos >>= 5;		/* NB: ECN + low 3 bits of DSCP */
763e755a73dSSam Leffler 		d_wme_ac = TID_TO_WME_AC(tos);
764e755a73dSSam Leffler 	} else {
765e755a73dSSam Leffler #endif /* INET6 */
7668a1b9b6aSSam Leffler 		d_wme_ac = WME_AC_BE;
767e755a73dSSam Leffler #ifdef INET6
768e755a73dSSam Leffler 	}
769e755a73dSSam Leffler #endif
7708a1b9b6aSSam Leffler #ifdef INET
7718a1b9b6aSSam Leffler 	}
7728a1b9b6aSSam Leffler #endif
7738a1b9b6aSSam Leffler 	/*
7748a1b9b6aSSam Leffler 	 * Use highest priority AC.
7758a1b9b6aSSam Leffler 	 */
7768a1b9b6aSSam Leffler 	if (v_wme_ac > d_wme_ac)
7778a1b9b6aSSam Leffler 		ac = v_wme_ac;
7788a1b9b6aSSam Leffler 	else
7798a1b9b6aSSam Leffler 		ac = d_wme_ac;
7808a1b9b6aSSam Leffler 
7818a1b9b6aSSam Leffler 	/*
7828a1b9b6aSSam Leffler 	 * Apply ACM policy.
7838a1b9b6aSSam Leffler 	 */
784b032f27cSSam Leffler 	if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
7858a1b9b6aSSam Leffler 		static const int acmap[4] = {
7868a1b9b6aSSam Leffler 			WME_AC_BK,	/* WME_AC_BE */
7878a1b9b6aSSam Leffler 			WME_AC_BK,	/* WME_AC_BK */
7888a1b9b6aSSam Leffler 			WME_AC_BE,	/* WME_AC_VI */
7898a1b9b6aSSam Leffler 			WME_AC_VI,	/* WME_AC_VO */
7908a1b9b6aSSam Leffler 		};
791b032f27cSSam Leffler 		struct ieee80211com *ic = ni->ni_ic;
792b032f27cSSam Leffler 
7938a1b9b6aSSam Leffler 		while (ac != WME_AC_BK &&
7948a1b9b6aSSam Leffler 		    ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
7958a1b9b6aSSam Leffler 			ac = acmap[ac];
7968a1b9b6aSSam Leffler 	}
7978a1b9b6aSSam Leffler done:
7988a1b9b6aSSam Leffler 	M_WME_SETAC(m, ac);
7998a1b9b6aSSam Leffler 	return 0;
8008a1b9b6aSSam Leffler }
8018a1b9b6aSSam Leffler 
8028a1b9b6aSSam Leffler /*
8035e923d2eSSam Leffler  * Insure there is sufficient contiguous space to encapsulate the
8045e923d2eSSam Leffler  * 802.11 data frame.  If room isn't already there, arrange for it.
8055e923d2eSSam Leffler  * Drivers and cipher modules assume we have done the necessary work
8065e923d2eSSam Leffler  * and fail rudely if they don't find the space they need.
8075e923d2eSSam Leffler  */
808616190d0SSam Leffler struct mbuf *
809b032f27cSSam Leffler ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
8105e923d2eSSam Leffler 	struct ieee80211_key *key, struct mbuf *m)
8115e923d2eSSam Leffler {
812ab96db10SSam Leffler #define	TO_BE_RECLAIMED	(sizeof(struct ether_header) - sizeof(struct llc))
813b032f27cSSam Leffler 	int needed_space = vap->iv_ic->ic_headroom + hdrsize;
8145e923d2eSSam Leffler 
8155e923d2eSSam Leffler 	if (key != NULL) {
8165e923d2eSSam Leffler 		/* XXX belongs in crypto code? */
8175e923d2eSSam Leffler 		needed_space += key->wk_cipher->ic_header;
8185e923d2eSSam Leffler 		/* XXX frags */
81983a244dbSSam Leffler 		/*
82083a244dbSSam Leffler 		 * When crypto is being done in the host we must insure
82183a244dbSSam Leffler 		 * the data are writable for the cipher routines; clone
82283a244dbSSam Leffler 		 * a writable mbuf chain.
82383a244dbSSam Leffler 		 * XXX handle SWMIC specially
82483a244dbSSam Leffler 		 */
8255c1f7f19SSam Leffler 		if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
82683a244dbSSam Leffler 			m = m_unshare(m, M_NOWAIT);
82783a244dbSSam Leffler 			if (m == NULL) {
828b032f27cSSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
82983a244dbSSam Leffler 				    "%s: cannot get writable mbuf\n", __func__);
830b032f27cSSam Leffler 				vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
83183a244dbSSam Leffler 				return NULL;
83283a244dbSSam Leffler 			}
83383a244dbSSam Leffler 		}
8345e923d2eSSam Leffler 	}
8355e923d2eSSam Leffler 	/*
8365e923d2eSSam Leffler 	 * We know we are called just before stripping an Ethernet
8375e923d2eSSam Leffler 	 * header and prepending an LLC header.  This means we know
8385e923d2eSSam Leffler 	 * there will be
839ab96db10SSam Leffler 	 *	sizeof(struct ether_header) - sizeof(struct llc)
8405e923d2eSSam Leffler 	 * bytes recovered to which we need additional space for the
8415e923d2eSSam Leffler 	 * 802.11 header and any crypto header.
8425e923d2eSSam Leffler 	 */
8435e923d2eSSam Leffler 	/* XXX check trailing space and copy instead? */
8445e923d2eSSam Leffler 	if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
8455e923d2eSSam Leffler 		struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
8465e923d2eSSam Leffler 		if (n == NULL) {
847b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
8485e923d2eSSam Leffler 			    "%s: cannot expand storage\n", __func__);
849b032f27cSSam Leffler 			vap->iv_stats.is_tx_nobuf++;
8505e923d2eSSam Leffler 			m_freem(m);
8515e923d2eSSam Leffler 			return NULL;
8525e923d2eSSam Leffler 		}
8535e923d2eSSam Leffler 		KASSERT(needed_space <= MHLEN,
8545e923d2eSSam Leffler 		    ("not enough room, need %u got %zu\n", needed_space, MHLEN));
8555e923d2eSSam Leffler 		/*
8565e923d2eSSam Leffler 		 * Setup new mbuf to have leading space to prepend the
8575e923d2eSSam Leffler 		 * 802.11 header and any crypto header bits that are
8585e923d2eSSam Leffler 		 * required (the latter are added when the driver calls
8595e923d2eSSam Leffler 		 * back to ieee80211_crypto_encap to do crypto encapsulation).
8605e923d2eSSam Leffler 		 */
8615e923d2eSSam Leffler 		/* NB: must be first 'cuz it clobbers m_data */
8625e923d2eSSam Leffler 		m_move_pkthdr(n, m);
8635e923d2eSSam Leffler 		n->m_len = 0;			/* NB: m_gethdr does not set */
8645e923d2eSSam Leffler 		n->m_data += needed_space;
8655e923d2eSSam Leffler 		/*
8665e923d2eSSam Leffler 		 * Pull up Ethernet header to create the expected layout.
8675e923d2eSSam Leffler 		 * We could use m_pullup but that's overkill (i.e. we don't
8685e923d2eSSam Leffler 		 * need the actual data) and it cannot fail so do it inline
8695e923d2eSSam Leffler 		 * for speed.
8705e923d2eSSam Leffler 		 */
8715e923d2eSSam Leffler 		/* NB: struct ether_header is known to be contiguous */
8725e923d2eSSam Leffler 		n->m_len += sizeof(struct ether_header);
8735e923d2eSSam Leffler 		m->m_len -= sizeof(struct ether_header);
8745e923d2eSSam Leffler 		m->m_data += sizeof(struct ether_header);
8755e923d2eSSam Leffler 		/*
8765e923d2eSSam Leffler 		 * Replace the head of the chain.
8775e923d2eSSam Leffler 		 */
8785e923d2eSSam Leffler 		n->m_next = m;
8795e923d2eSSam Leffler 		m = n;
8805e923d2eSSam Leffler 	}
8815e923d2eSSam Leffler 	return m;
8825e923d2eSSam Leffler #undef TO_BE_RECLAIMED
8835e923d2eSSam Leffler }
8845e923d2eSSam Leffler 
8855e923d2eSSam Leffler /*
8865e923d2eSSam Leffler  * Return the transmit key to use in sending a unicast frame.
8875e923d2eSSam Leffler  * If a unicast key is set we use that.  When no unicast key is set
8885e923d2eSSam Leffler  * we fall back to the default transmit key.
8898a1b9b6aSSam Leffler  */
8908a1b9b6aSSam Leffler static __inline struct ieee80211_key *
891b032f27cSSam Leffler ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
892b032f27cSSam Leffler 	struct ieee80211_node *ni)
8938a1b9b6aSSam Leffler {
894cda15ce1SSam Leffler 	if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
895b032f27cSSam Leffler 		if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
896b032f27cSSam Leffler 		    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
8978a1b9b6aSSam Leffler 			return NULL;
898b032f27cSSam Leffler 		return &vap->iv_nw_keys[vap->iv_def_txkey];
8998a1b9b6aSSam Leffler 	} else {
9008a1b9b6aSSam Leffler 		return &ni->ni_ucastkey;
9018a1b9b6aSSam Leffler 	}
9025e923d2eSSam Leffler }
9035e923d2eSSam Leffler 
9045e923d2eSSam Leffler /*
9055e923d2eSSam Leffler  * Return the transmit key to use in sending a multicast frame.
9065e923d2eSSam Leffler  * Multicast traffic always uses the group key which is installed as
9075e923d2eSSam Leffler  * the default tx key.
9085e923d2eSSam Leffler  */
9095e923d2eSSam Leffler static __inline struct ieee80211_key *
910b032f27cSSam Leffler ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
911b032f27cSSam Leffler 	struct ieee80211_node *ni)
9125e923d2eSSam Leffler {
913b032f27cSSam Leffler 	if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
914b032f27cSSam Leffler 	    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
9155e923d2eSSam Leffler 		return NULL;
916b032f27cSSam Leffler 	return &vap->iv_nw_keys[vap->iv_def_txkey];
9178a1b9b6aSSam Leffler }
9188a1b9b6aSSam Leffler 
9198a1b9b6aSSam Leffler /*
9208a1b9b6aSSam Leffler  * Encapsulate an outbound data frame.  The mbuf chain is updated.
9218a1b9b6aSSam Leffler  * If an error is encountered NULL is returned.  The caller is required
9228a1b9b6aSSam Leffler  * to provide a node reference and pullup the ethernet header in the
9238a1b9b6aSSam Leffler  * first mbuf.
924b032f27cSSam Leffler  *
925b032f27cSSam Leffler  * NB: Packet is assumed to be processed by ieee80211_classify which
926b032f27cSSam Leffler  *     marked EAPOL frames w/ M_EAPOL.
9270a915fadSSam Leffler  */
9281a1e1d21SSam Leffler struct mbuf *
929339ccfb3SSam Leffler ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
930339ccfb3SSam Leffler     struct mbuf *m)
9311a1e1d21SSam Leffler {
932b032f27cSSam Leffler #define	WH4(wh)	((struct ieee80211_frame_addr4 *)(wh))
933b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
9341a1e1d21SSam Leffler 	struct ether_header eh;
9351a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
9368a1b9b6aSSam Leffler 	struct ieee80211_key *key;
9371a1e1d21SSam Leffler 	struct llc *llc;
938616190d0SSam Leffler 	int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr;
9399e80b1dfSSam Leffler 	ieee80211_seq seqno;
9401a1e1d21SSam Leffler 
94168e8e04eSSam Leffler 	/*
94268e8e04eSSam Leffler 	 * Copy existing Ethernet header to a safe place.  The
94368e8e04eSSam Leffler 	 * rest of the code assumes it's ok to strip it when
94468e8e04eSSam Leffler 	 * reorganizing state for the final encapsulation.
94568e8e04eSSam Leffler 	 */
9468a1b9b6aSSam Leffler 	KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
947b032f27cSSam Leffler 	ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
9481a1e1d21SSam Leffler 
9498a1b9b6aSSam Leffler 	/*
9508a1b9b6aSSam Leffler 	 * Insure space for additional headers.  First identify
9518a1b9b6aSSam Leffler 	 * transmit key to use in calculating any buffer adjustments
9528a1b9b6aSSam Leffler 	 * required.  This is also used below to do privacy
9538a1b9b6aSSam Leffler 	 * encapsulation work.  Then calculate the 802.11 header
9548a1b9b6aSSam Leffler 	 * size and any padding required by the driver.
9558a1b9b6aSSam Leffler 	 *
9568a1b9b6aSSam Leffler 	 * Note key may be NULL if we fall back to the default
9578a1b9b6aSSam Leffler 	 * transmit key and that is not set.  In that case the
9588a1b9b6aSSam Leffler 	 * buffer may not be expanded as needed by the cipher
9598a1b9b6aSSam Leffler 	 * routines, but they will/should discard it.
9608a1b9b6aSSam Leffler 	 */
961b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
962b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA ||
963b032f27cSSam Leffler 		    !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
964b032f27cSSam Leffler 		    (vap->iv_opmode == IEEE80211_M_WDS &&
965b032f27cSSam Leffler 		     (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
966b032f27cSSam Leffler 			key = ieee80211_crypto_getucastkey(vap, ni);
9675e923d2eSSam Leffler 		else
968b032f27cSSam Leffler 			key = ieee80211_crypto_getmcastkey(vap, ni);
969b032f27cSSam Leffler 		if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
970b032f27cSSam Leffler 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
971b032f27cSSam Leffler 			    eh.ether_dhost,
972b032f27cSSam Leffler 			    "no default transmit key (%s) deftxkey %u",
973b032f27cSSam Leffler 			    __func__, vap->iv_def_txkey);
974b032f27cSSam Leffler 			vap->iv_stats.is_tx_nodefkey++;
975d72c7253SSam Leffler 			goto bad;
9768a1b9b6aSSam Leffler 		}
9778a1b9b6aSSam Leffler 	} else
9788a1b9b6aSSam Leffler 		key = NULL;
9795e923d2eSSam Leffler 	/*
9805e923d2eSSam Leffler 	 * XXX Some ap's don't handle QoS-encapsulated EAPOL
9815e923d2eSSam Leffler 	 * frames so suppress use.  This may be an issue if other
9825e923d2eSSam Leffler 	 * ap's require all data frames to be QoS-encapsulated
9835e923d2eSSam Leffler 	 * once negotiated in which case we'll need to make this
9845e923d2eSSam Leffler 	 * configurable.
9855e923d2eSSam Leffler 	 */
98668e8e04eSSam Leffler 	addqos = (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) &&
987b032f27cSSam Leffler 		 (m->m_flags & M_EAPOL) == 0;
9885e923d2eSSam Leffler 	if (addqos)
9898a1b9b6aSSam Leffler 		hdrsize = sizeof(struct ieee80211_qosframe);
9908a1b9b6aSSam Leffler 	else
9918a1b9b6aSSam Leffler 		hdrsize = sizeof(struct ieee80211_frame);
992b032f27cSSam Leffler 	/*
993b032f27cSSam Leffler 	 * 4-address frames need to be generated for:
9946437e6daSSam Leffler 	 * o packets sent through a WDS vap (IEEE80211_M_WDS)
995f2a6a13cSSam Leffler 	 * o packets sent through a vap marked for relaying
996f2a6a13cSSam Leffler 	 *   (e.g. a station operating with dynamic WDS)
997b032f27cSSam Leffler 	 */
9986437e6daSSam Leffler 	is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
999f2a6a13cSSam Leffler 	    ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
1000b032f27cSSam Leffler 	     !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
1001b032f27cSSam Leffler 	if (is4addr)
1002b032f27cSSam Leffler 		hdrsize += IEEE80211_ADDR_LEN;
1003b032f27cSSam Leffler 	/*
1004b032f27cSSam Leffler 	 * Honor driver DATAPAD requirement.
1005b032f27cSSam Leffler 	 */
10068a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
1007b032f27cSSam Leffler 		hdrspace = roundup(hdrsize, sizeof(uint32_t));
1008b032f27cSSam Leffler 	else
1009b032f27cSSam Leffler 		hdrspace = hdrsize;
101068e8e04eSSam Leffler 
1011616190d0SSam Leffler 	if (__predict_true((m->m_flags & M_FF) == 0)) {
101268e8e04eSSam Leffler 		/*
101368e8e04eSSam Leffler 		 * Normal frame.
101468e8e04eSSam Leffler 		 */
1015b032f27cSSam Leffler 		m = ieee80211_mbuf_adjust(vap, hdrspace, key, m);
10168a1b9b6aSSam Leffler 		if (m == NULL) {
10178a1b9b6aSSam Leffler 			/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
10180a915fadSSam Leffler 			goto bad;
10190a915fadSSam Leffler 		}
102068e8e04eSSam Leffler 		/* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
1021ab96db10SSam Leffler 		m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
10221a1e1d21SSam Leffler 		llc = mtod(m, struct llc *);
10231a1e1d21SSam Leffler 		llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
10241a1e1d21SSam Leffler 		llc->llc_control = LLC_UI;
10251a1e1d21SSam Leffler 		llc->llc_snap.org_code[0] = 0;
10261a1e1d21SSam Leffler 		llc->llc_snap.org_code[1] = 0;
10271a1e1d21SSam Leffler 		llc->llc_snap.org_code[2] = 0;
10281a1e1d21SSam Leffler 		llc->llc_snap.ether_type = eh.ether_type;
1029616190d0SSam Leffler 	} else {
1030616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1031339ccfb3SSam Leffler 		/*
1032339ccfb3SSam Leffler 		 * Aggregated frame.
1033339ccfb3SSam Leffler 		 */
1034616190d0SSam Leffler 		m = ieee80211_ff_encap(vap, m, hdrspace, key);
1035616190d0SSam Leffler 		if (m == NULL)
1036616190d0SSam Leffler #endif
1037616190d0SSam Leffler 			goto bad;
103868e8e04eSSam Leffler 	}
10398a1b9b6aSSam Leffler 	datalen = m->m_pkthdr.len;		/* NB: w/o 802.11 header */
10408a1b9b6aSSam Leffler 
1041b032f27cSSam Leffler 	M_PREPEND(m, hdrspace, M_DONTWAIT);
10421be50176SSam Leffler 	if (m == NULL) {
1043b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
10440a915fadSSam Leffler 		goto bad;
10451be50176SSam Leffler 	}
10461a1e1d21SSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
10471a1e1d21SSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
104868e8e04eSSam Leffler 	*(uint16_t *)wh->i_dur = 0;
1049b032f27cSSam Leffler 	if (is4addr) {
1050b032f27cSSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1051b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1052b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1053b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1054b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1055b032f27cSSam Leffler 	} else switch (vap->iv_opmode) {
10561a1e1d21SSam Leffler 	case IEEE80211_M_STA:
10571a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
10581a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
10591a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
10601a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
10611a1e1d21SSam Leffler 		break;
10621a1e1d21SSam Leffler 	case IEEE80211_M_IBSS:
10631a1e1d21SSam Leffler 	case IEEE80211_M_AHDEMO:
10641a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
10651a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
10661a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1067a8b16e87SSam Leffler 		/*
1068b032f27cSSam Leffler 		 * NB: always use the bssid from iv_bss as the
1069a8b16e87SSam Leffler 		 *     neighbor's may be stale after an ibss merge
1070a8b16e87SSam Leffler 		 */
1071b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
10721a1e1d21SSam Leffler 		break;
10731a1e1d21SSam Leffler 	case IEEE80211_M_HOSTAP:
10741a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
10751a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
10761a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
10771a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
10781a1e1d21SSam Leffler 		break;
10792bbe529dSSam Leffler 	case IEEE80211_M_MONITOR:
1080b032f27cSSam Leffler 	case IEEE80211_M_WDS:		/* NB: is4addr should always be true */
10810a915fadSSam Leffler 		goto bad;
10821a1e1d21SSam Leffler 	}
1083bc5627d9SSam Leffler 	if (m->m_flags & M_MORE_DATA)
1084bc5627d9SSam Leffler 		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
10855e923d2eSSam Leffler 	if (addqos) {
1086b032f27cSSam Leffler 		uint8_t *qos;
10878a1b9b6aSSam Leffler 		int ac, tid;
10888a1b9b6aSSam Leffler 
1089b032f27cSSam Leffler 		if (is4addr) {
1090b032f27cSSam Leffler 			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1091b032f27cSSam Leffler 		} else
1092b032f27cSSam Leffler 			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
10938a1b9b6aSSam Leffler 		ac = M_WME_GETAC(m);
10948a1b9b6aSSam Leffler 		/* map from access class/queue to 11e header priorty value */
10958a1b9b6aSSam Leffler 		tid = WME_AC_TO_TID(ac);
1096b032f27cSSam Leffler 		qos[0] = tid & IEEE80211_QOS_TID;
10978a1b9b6aSSam Leffler 		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
1098b032f27cSSam Leffler 			qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1099b032f27cSSam Leffler 		qos[1] = 0;
1100b032f27cSSam Leffler 		wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
11018a1b9b6aSSam Leffler 
110245f856e3SSam Leffler 		if ((m->m_flags & M_AMPDU_MPDU) == 0) {
110345f856e3SSam Leffler 			/*
110445f856e3SSam Leffler 			 * NB: don't assign a sequence # to potential
110545f856e3SSam Leffler 			 * aggregates; we expect this happens at the
110645f856e3SSam Leffler 			 * point the frame comes off any aggregation q
110745f856e3SSam Leffler 			 * as otherwise we may introduce holes in the
110845f856e3SSam Leffler 			 * BA sequence space and/or make window accouting
110945f856e3SSam Leffler 			 * more difficult.
111045f856e3SSam Leffler 			 *
111145f856e3SSam Leffler 			 * XXX may want to control this with a driver
111245f856e3SSam Leffler 			 * capability; this may also change when we pull
111345f856e3SSam Leffler 			 * aggregation up into net80211
111445f856e3SSam Leffler 			 */
11159e80b1dfSSam Leffler 			seqno = ni->ni_txseqs[tid]++;
111668e8e04eSSam Leffler 			*(uint16_t *)wh->i_seq =
11179e80b1dfSSam Leffler 			    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
11189a841c7fSSam Leffler 			M_SEQNO_SET(m, seqno);
111945f856e3SSam Leffler 		}
11208a1b9b6aSSam Leffler 	} else {
11219e80b1dfSSam Leffler 		seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
112268e8e04eSSam Leffler 		*(uint16_t *)wh->i_seq =
11239e80b1dfSSam Leffler 		    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
11249a841c7fSSam Leffler 		M_SEQNO_SET(m, seqno);
11258a1b9b6aSSam Leffler 	}
11269a841c7fSSam Leffler 
112768e8e04eSSam Leffler 	/* check if xmit fragmentation is required */
1128b032f27cSSam Leffler 	txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
112968e8e04eSSam Leffler 	    !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1130b032f27cSSam Leffler 	    (vap->iv_caps & IEEE80211_C_TXFRAG) &&
1131e9cfc1f5SSam Leffler 	    (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
11325e923d2eSSam Leffler 	if (key != NULL) {
11335e923d2eSSam Leffler 		/*
11345e923d2eSSam Leffler 		 * IEEE 802.1X: send EAPOL frames always in the clear.
11355e923d2eSSam Leffler 		 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
11365e923d2eSSam Leffler 		 */
1137b032f27cSSam Leffler 		if ((m->m_flags & M_EAPOL) == 0 ||
1138b032f27cSSam Leffler 		    ((vap->iv_flags & IEEE80211_F_WPA) &&
1139b032f27cSSam Leffler 		     (vap->iv_opmode == IEEE80211_M_STA ?
1140cda15ce1SSam Leffler 		      !IEEE80211_KEY_UNDEFINED(key) :
1141cda15ce1SSam Leffler 		      !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
11425e923d2eSSam Leffler 			wh->i_fc[1] |= IEEE80211_FC1_WEP;
1143b032f27cSSam Leffler 			if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1144b032f27cSSam Leffler 				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1145b032f27cSSam Leffler 				    eh.ether_dhost,
1146b032f27cSSam Leffler 				    "%s", "enmic failed, discard frame");
1147b032f27cSSam Leffler 				vap->iv_stats.is_crypto_enmicfail++;
11485e923d2eSSam Leffler 				goto bad;
11495e923d2eSSam Leffler 			}
11505e923d2eSSam Leffler 		}
11515e923d2eSSam Leffler 	}
1152b032f27cSSam Leffler 	if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1153b032f27cSSam Leffler 	    key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
115468e8e04eSSam Leffler 		goto bad;
11558a1b9b6aSSam Leffler 
1156c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
1157c1af44bdSSam Leffler 
11588a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_data);
11599e80b1dfSSam Leffler 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1160b6e9b119SSam Leffler 		IEEE80211_NODE_STAT(ni, tx_mcast);
11619e80b1dfSSam Leffler 		m->m_flags |= M_MCAST;
11629e80b1dfSSam Leffler 	} else
1163b6e9b119SSam Leffler 		IEEE80211_NODE_STAT(ni, tx_ucast);
11648a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
11658a1b9b6aSSam Leffler 
11661a1e1d21SSam Leffler 	return m;
11670a915fadSSam Leffler bad:
11680a915fadSSam Leffler 	if (m != NULL)
11690a915fadSSam Leffler 		m_freem(m);
11700a915fadSSam Leffler 	return NULL;
1171b032f27cSSam Leffler #undef WH4
11721a1e1d21SSam Leffler }
11731a1e1d21SSam Leffler 
11741a1e1d21SSam Leffler /*
117568e8e04eSSam Leffler  * Fragment the frame according to the specified mtu.
117668e8e04eSSam Leffler  * The size of the 802.11 header (w/o padding) is provided
117768e8e04eSSam Leffler  * so we don't need to recalculate it.  We create a new
117868e8e04eSSam Leffler  * mbuf for each fragment and chain it through m_nextpkt;
117968e8e04eSSam Leffler  * we might be able to optimize this by reusing the original
118068e8e04eSSam Leffler  * packet's mbufs but that is significantly more complicated.
118168e8e04eSSam Leffler  */
118268e8e04eSSam Leffler static int
1183b032f27cSSam Leffler ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
118468e8e04eSSam Leffler 	u_int hdrsize, u_int ciphdrsize, u_int mtu)
118568e8e04eSSam Leffler {
118668e8e04eSSam Leffler 	struct ieee80211_frame *wh, *whf;
118768e8e04eSSam Leffler 	struct mbuf *m, *prev, *next;
118868e8e04eSSam Leffler 	u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
118968e8e04eSSam Leffler 
119068e8e04eSSam Leffler 	KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
119168e8e04eSSam Leffler 	KASSERT(m0->m_pkthdr.len > mtu,
119268e8e04eSSam Leffler 		("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
119368e8e04eSSam Leffler 
119468e8e04eSSam Leffler 	wh = mtod(m0, struct ieee80211_frame *);
119568e8e04eSSam Leffler 	/* NB: mark the first frag; it will be propagated below */
119668e8e04eSSam Leffler 	wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
119768e8e04eSSam Leffler 	totalhdrsize = hdrsize + ciphdrsize;
119868e8e04eSSam Leffler 	fragno = 1;
119968e8e04eSSam Leffler 	off = mtu - ciphdrsize;
120068e8e04eSSam Leffler 	remainder = m0->m_pkthdr.len - off;
120168e8e04eSSam Leffler 	prev = m0;
120268e8e04eSSam Leffler 	do {
120368e8e04eSSam Leffler 		fragsize = totalhdrsize + remainder;
120468e8e04eSSam Leffler 		if (fragsize > mtu)
120568e8e04eSSam Leffler 			fragsize = mtu;
1206b032f27cSSam Leffler 		/* XXX fragsize can be >2048! */
120768e8e04eSSam Leffler 		KASSERT(fragsize < MCLBYTES,
120868e8e04eSSam Leffler 			("fragment size %u too big!", fragsize));
120968e8e04eSSam Leffler 		if (fragsize > MHLEN)
121068e8e04eSSam Leffler 			m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
121168e8e04eSSam Leffler 		else
121268e8e04eSSam Leffler 			m = m_gethdr(M_DONTWAIT, MT_DATA);
121368e8e04eSSam Leffler 		if (m == NULL)
121468e8e04eSSam Leffler 			goto bad;
121568e8e04eSSam Leffler 		/* leave room to prepend any cipher header */
121668e8e04eSSam Leffler 		m_align(m, fragsize - ciphdrsize);
121768e8e04eSSam Leffler 
121868e8e04eSSam Leffler 		/*
121968e8e04eSSam Leffler 		 * Form the header in the fragment.  Note that since
122068e8e04eSSam Leffler 		 * we mark the first fragment with the MORE_FRAG bit
122168e8e04eSSam Leffler 		 * it automatically is propagated to each fragment; we
122268e8e04eSSam Leffler 		 * need only clear it on the last fragment (done below).
122368e8e04eSSam Leffler 		 */
122468e8e04eSSam Leffler 		whf = mtod(m, struct ieee80211_frame *);
122568e8e04eSSam Leffler 		memcpy(whf, wh, hdrsize);
122668e8e04eSSam Leffler 		*(uint16_t *)&whf->i_seq[0] |= htole16(
122768e8e04eSSam Leffler 			(fragno & IEEE80211_SEQ_FRAG_MASK) <<
122868e8e04eSSam Leffler 				IEEE80211_SEQ_FRAG_SHIFT);
122968e8e04eSSam Leffler 		fragno++;
123068e8e04eSSam Leffler 
123168e8e04eSSam Leffler 		payload = fragsize - totalhdrsize;
123268e8e04eSSam Leffler 		/* NB: destination is known to be contiguous */
123368e8e04eSSam Leffler 		m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrsize);
123468e8e04eSSam Leffler 		m->m_len = hdrsize + payload;
123568e8e04eSSam Leffler 		m->m_pkthdr.len = hdrsize + payload;
123668e8e04eSSam Leffler 		m->m_flags |= M_FRAG;
123768e8e04eSSam Leffler 
123868e8e04eSSam Leffler 		/* chain up the fragment */
123968e8e04eSSam Leffler 		prev->m_nextpkt = m;
124068e8e04eSSam Leffler 		prev = m;
124168e8e04eSSam Leffler 
124268e8e04eSSam Leffler 		/* deduct fragment just formed */
124368e8e04eSSam Leffler 		remainder -= payload;
124468e8e04eSSam Leffler 		off += payload;
124568e8e04eSSam Leffler 	} while (remainder != 0);
124651cec121SWeongyo Jeong 
124751cec121SWeongyo Jeong 	/* set the last fragment */
124851cec121SWeongyo Jeong 	m->m_flags |= M_LASTFRAG;
124968e8e04eSSam Leffler 	whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
125068e8e04eSSam Leffler 
125168e8e04eSSam Leffler 	/* strip first mbuf now that everything has been copied */
125268e8e04eSSam Leffler 	m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
125368e8e04eSSam Leffler 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
125468e8e04eSSam Leffler 
1255b032f27cSSam Leffler 	vap->iv_stats.is_tx_fragframes++;
1256b032f27cSSam Leffler 	vap->iv_stats.is_tx_frags += fragno-1;
125768e8e04eSSam Leffler 
125868e8e04eSSam Leffler 	return 1;
125968e8e04eSSam Leffler bad:
126068e8e04eSSam Leffler 	/* reclaim fragments but leave original frame for caller to free */
126168e8e04eSSam Leffler 	for (m = m0->m_nextpkt; m != NULL; m = next) {
126268e8e04eSSam Leffler 		next = m->m_nextpkt;
126368e8e04eSSam Leffler 		m->m_nextpkt = NULL;		/* XXX paranoid */
126468e8e04eSSam Leffler 		m_freem(m);
126568e8e04eSSam Leffler 	}
126668e8e04eSSam Leffler 	m0->m_nextpkt = NULL;
126768e8e04eSSam Leffler 	return 0;
126868e8e04eSSam Leffler }
126968e8e04eSSam Leffler 
127068e8e04eSSam Leffler /*
12711a1e1d21SSam Leffler  * Add a supported rates element id to a frame.
12721a1e1d21SSam Leffler  */
127368e8e04eSSam Leffler static uint8_t *
127468e8e04eSSam Leffler ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
12751a1e1d21SSam Leffler {
12761a1e1d21SSam Leffler 	int nrates;
12771a1e1d21SSam Leffler 
12781a1e1d21SSam Leffler 	*frm++ = IEEE80211_ELEMID_RATES;
12791a1e1d21SSam Leffler 	nrates = rs->rs_nrates;
12801a1e1d21SSam Leffler 	if (nrates > IEEE80211_RATE_SIZE)
12811a1e1d21SSam Leffler 		nrates = IEEE80211_RATE_SIZE;
12821a1e1d21SSam Leffler 	*frm++ = nrates;
12831a1e1d21SSam Leffler 	memcpy(frm, rs->rs_rates, nrates);
12841a1e1d21SSam Leffler 	return frm + nrates;
12851a1e1d21SSam Leffler }
12861a1e1d21SSam Leffler 
12871a1e1d21SSam Leffler /*
12881a1e1d21SSam Leffler  * Add an extended supported rates element id to a frame.
12891a1e1d21SSam Leffler  */
129068e8e04eSSam Leffler static uint8_t *
129168e8e04eSSam Leffler ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
12921a1e1d21SSam Leffler {
12931a1e1d21SSam Leffler 	/*
12941a1e1d21SSam Leffler 	 * Add an extended supported rates element if operating in 11g mode.
12951a1e1d21SSam Leffler 	 */
12961a1e1d21SSam Leffler 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
12971a1e1d21SSam Leffler 		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
12981a1e1d21SSam Leffler 		*frm++ = IEEE80211_ELEMID_XRATES;
12991a1e1d21SSam Leffler 		*frm++ = nrates;
13001a1e1d21SSam Leffler 		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
13011a1e1d21SSam Leffler 		frm += nrates;
13021a1e1d21SSam Leffler 	}
13031a1e1d21SSam Leffler 	return frm;
13041a1e1d21SSam Leffler }
13051a1e1d21SSam Leffler 
13061a1e1d21SSam Leffler /*
1307b032f27cSSam Leffler  * Add an ssid element to a frame.
13081a1e1d21SSam Leffler  */
130968e8e04eSSam Leffler static uint8_t *
131068e8e04eSSam Leffler ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
13111a1e1d21SSam Leffler {
13121a1e1d21SSam Leffler 	*frm++ = IEEE80211_ELEMID_SSID;
13131a1e1d21SSam Leffler 	*frm++ = len;
13141a1e1d21SSam Leffler 	memcpy(frm, ssid, len);
13151a1e1d21SSam Leffler 	return frm + len;
13161a1e1d21SSam Leffler }
13171a1e1d21SSam Leffler 
13188a1b9b6aSSam Leffler /*
13198a1b9b6aSSam Leffler  * Add an erp element to a frame.
13208a1b9b6aSSam Leffler  */
132168e8e04eSSam Leffler static uint8_t *
132268e8e04eSSam Leffler ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic)
13231a1e1d21SSam Leffler {
132468e8e04eSSam Leffler 	uint8_t erp;
13251a1e1d21SSam Leffler 
13268a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_ERP;
13278a1b9b6aSSam Leffler 	*frm++ = 1;
13288a1b9b6aSSam Leffler 	erp = 0;
13298a1b9b6aSSam Leffler 	if (ic->ic_nonerpsta != 0)
13308a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_NON_ERP_PRESENT;
13318a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_USEPROT)
13328a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_USE_PROTECTION;
13338a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_USEBARKER)
13348a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_LONG_PREAMBLE;
13358a1b9b6aSSam Leffler 	*frm++ = erp;
13368a1b9b6aSSam Leffler 	return frm;
13371a1e1d21SSam Leffler }
13381a1e1d21SSam Leffler 
13398a1b9b6aSSam Leffler /*
1340b032f27cSSam Leffler  * Add a CFParams element to a frame.
13418a1b9b6aSSam Leffler  */
134268e8e04eSSam Leffler static uint8_t *
1343b032f27cSSam Leffler ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
13448a1b9b6aSSam Leffler {
1345b032f27cSSam Leffler #define	ADDSHORT(frm, v) do {			\
1346b032f27cSSam Leffler 	frm[0] = (v) & 0xff;			\
1347b032f27cSSam Leffler 	frm[1] = (v) >> 8;			\
1348b032f27cSSam Leffler 	frm += 2;				\
1349b032f27cSSam Leffler } while (0)
1350b032f27cSSam Leffler 	*frm++ = IEEE80211_ELEMID_CFPARMS;
1351b032f27cSSam Leffler 	*frm++ = 6;
1352b032f27cSSam Leffler 	*frm++ = 0;		/* CFP count */
1353b032f27cSSam Leffler 	*frm++ = 2;		/* CFP period */
1354b032f27cSSam Leffler 	ADDSHORT(frm, 0);	/* CFP MaxDuration (TU) */
1355b032f27cSSam Leffler 	ADDSHORT(frm, 0);	/* CFP CurRemaining (TU) */
13568a1b9b6aSSam Leffler 	return frm;
1357b032f27cSSam Leffler #undef ADDSHORT
1358b032f27cSSam Leffler }
1359b032f27cSSam Leffler 
1360b032f27cSSam Leffler static __inline uint8_t *
1361b032f27cSSam Leffler add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
1362b032f27cSSam Leffler {
1363b032f27cSSam Leffler 	memcpy(frm, ie->ie_data, ie->ie_len);
1364b032f27cSSam Leffler 	return frm + ie->ie_len;
1365b032f27cSSam Leffler }
1366b032f27cSSam Leffler 
1367b032f27cSSam Leffler static __inline uint8_t *
1368b032f27cSSam Leffler add_ie(uint8_t *frm, const uint8_t *ie)
1369b032f27cSSam Leffler {
1370b032f27cSSam Leffler 	memcpy(frm, ie, 2 + ie[1]);
1371b032f27cSSam Leffler 	return frm + 2 + ie[1];
13728a1b9b6aSSam Leffler }
13738a1b9b6aSSam Leffler 
13748a1b9b6aSSam Leffler #define	WME_OUI_BYTES		0x00, 0x50, 0xf2
13758a1b9b6aSSam Leffler /*
13768a1b9b6aSSam Leffler  * Add a WME information element to a frame.
13778a1b9b6aSSam Leffler  */
137868e8e04eSSam Leffler static uint8_t *
137968e8e04eSSam Leffler ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme)
13808a1b9b6aSSam Leffler {
13818a1b9b6aSSam Leffler 	static const struct ieee80211_wme_info info = {
13828a1b9b6aSSam Leffler 		.wme_id		= IEEE80211_ELEMID_VENDOR,
13838a1b9b6aSSam Leffler 		.wme_len	= sizeof(struct ieee80211_wme_info) - 2,
13848a1b9b6aSSam Leffler 		.wme_oui	= { WME_OUI_BYTES },
13858a1b9b6aSSam Leffler 		.wme_type	= WME_OUI_TYPE,
13868a1b9b6aSSam Leffler 		.wme_subtype	= WME_INFO_OUI_SUBTYPE,
13878a1b9b6aSSam Leffler 		.wme_version	= WME_VERSION,
13888a1b9b6aSSam Leffler 		.wme_info	= 0,
13898a1b9b6aSSam Leffler 	};
13908a1b9b6aSSam Leffler 	memcpy(frm, &info, sizeof(info));
13918a1b9b6aSSam Leffler 	return frm + sizeof(info);
13928a1b9b6aSSam Leffler }
13938a1b9b6aSSam Leffler 
13948a1b9b6aSSam Leffler /*
13958a1b9b6aSSam Leffler  * Add a WME parameters element to a frame.
13968a1b9b6aSSam Leffler  */
139768e8e04eSSam Leffler static uint8_t *
139868e8e04eSSam Leffler ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme)
13998a1b9b6aSSam Leffler {
14008a1b9b6aSSam Leffler #define	SM(_v, _f)	(((_v) << _f##_S) & _f)
14018a1b9b6aSSam Leffler #define	ADDSHORT(frm, v) do {			\
14028a1b9b6aSSam Leffler 	frm[0] = (v) & 0xff;			\
14038a1b9b6aSSam Leffler 	frm[1] = (v) >> 8;			\
14048a1b9b6aSSam Leffler 	frm += 2;				\
14058a1b9b6aSSam Leffler } while (0)
14068a1b9b6aSSam Leffler 	/* NB: this works 'cuz a param has an info at the front */
14078a1b9b6aSSam Leffler 	static const struct ieee80211_wme_info param = {
14088a1b9b6aSSam Leffler 		.wme_id		= IEEE80211_ELEMID_VENDOR,
14098a1b9b6aSSam Leffler 		.wme_len	= sizeof(struct ieee80211_wme_param) - 2,
14108a1b9b6aSSam Leffler 		.wme_oui	= { WME_OUI_BYTES },
14118a1b9b6aSSam Leffler 		.wme_type	= WME_OUI_TYPE,
14128a1b9b6aSSam Leffler 		.wme_subtype	= WME_PARAM_OUI_SUBTYPE,
14138a1b9b6aSSam Leffler 		.wme_version	= WME_VERSION,
14148a1b9b6aSSam Leffler 	};
14158a1b9b6aSSam Leffler 	int i;
14168a1b9b6aSSam Leffler 
14178a1b9b6aSSam Leffler 	memcpy(frm, &param, sizeof(param));
14188a1b9b6aSSam Leffler 	frm += __offsetof(struct ieee80211_wme_info, wme_info);
14198a1b9b6aSSam Leffler 	*frm++ = wme->wme_bssChanParams.cap_info;	/* AC info */
14208a1b9b6aSSam Leffler 	*frm++ = 0;					/* reserved field */
14218a1b9b6aSSam Leffler 	for (i = 0; i < WME_NUM_AC; i++) {
14228a1b9b6aSSam Leffler 		const struct wmeParams *ac =
14238a1b9b6aSSam Leffler 		       &wme->wme_bssChanParams.cap_wmeParams[i];
14248a1b9b6aSSam Leffler 		*frm++ = SM(i, WME_PARAM_ACI)
14258a1b9b6aSSam Leffler 		       | SM(ac->wmep_acm, WME_PARAM_ACM)
14268a1b9b6aSSam Leffler 		       | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
14278a1b9b6aSSam Leffler 		       ;
14288a1b9b6aSSam Leffler 		*frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
14298a1b9b6aSSam Leffler 		       | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
14308a1b9b6aSSam Leffler 		       ;
14318a1b9b6aSSam Leffler 		ADDSHORT(frm, ac->wmep_txopLimit);
14328a1b9b6aSSam Leffler 	}
14338a1b9b6aSSam Leffler 	return frm;
14348a1b9b6aSSam Leffler #undef SM
14358a1b9b6aSSam Leffler #undef ADDSHORT
14368a1b9b6aSSam Leffler }
14378a1b9b6aSSam Leffler #undef WME_OUI_BYTES
14388a1b9b6aSSam Leffler 
14390a915fadSSam Leffler /*
1440b032f27cSSam Leffler  * Add an 11h Power Constraint element to a frame.
1441b032f27cSSam Leffler  */
1442b032f27cSSam Leffler static uint8_t *
1443b032f27cSSam Leffler ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
1444b032f27cSSam Leffler {
1445b032f27cSSam Leffler 	const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
1446b032f27cSSam Leffler 	/* XXX per-vap tx power limit? */
1447b032f27cSSam Leffler 	int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
1448b032f27cSSam Leffler 
1449b032f27cSSam Leffler 	frm[0] = IEEE80211_ELEMID_PWRCNSTR;
1450b032f27cSSam Leffler 	frm[1] = 1;
1451b032f27cSSam Leffler 	frm[2] = c->ic_maxregpower > limit ?  c->ic_maxregpower - limit : 0;
1452b032f27cSSam Leffler 	return frm + 3;
1453b032f27cSSam Leffler }
1454b032f27cSSam Leffler 
1455b032f27cSSam Leffler /*
1456b032f27cSSam Leffler  * Add an 11h Power Capability element to a frame.
1457b032f27cSSam Leffler  */
1458b032f27cSSam Leffler static uint8_t *
1459b032f27cSSam Leffler ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
1460b032f27cSSam Leffler {
1461b032f27cSSam Leffler 	frm[0] = IEEE80211_ELEMID_PWRCAP;
1462b032f27cSSam Leffler 	frm[1] = 2;
1463b032f27cSSam Leffler 	frm[2] = c->ic_minpower;
1464b032f27cSSam Leffler 	frm[3] = c->ic_maxpower;
1465b032f27cSSam Leffler 	return frm + 4;
1466b032f27cSSam Leffler }
1467b032f27cSSam Leffler 
1468b032f27cSSam Leffler /*
1469b032f27cSSam Leffler  * Add an 11h Supported Channels element to a frame.
1470b032f27cSSam Leffler  */
1471b032f27cSSam Leffler static uint8_t *
1472b032f27cSSam Leffler ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
1473b032f27cSSam Leffler {
1474b032f27cSSam Leffler 	static const int ielen = 26;
1475b032f27cSSam Leffler 
1476b032f27cSSam Leffler 	frm[0] = IEEE80211_ELEMID_SUPPCHAN;
1477b032f27cSSam Leffler 	frm[1] = ielen;
1478b032f27cSSam Leffler 	/* XXX not correct */
1479b032f27cSSam Leffler 	memcpy(frm+2, ic->ic_chan_avail, ielen);
1480b032f27cSSam Leffler 	return frm + 2 + ielen;
1481b032f27cSSam Leffler }
1482b032f27cSSam Leffler 
1483b032f27cSSam Leffler /*
1484b032f27cSSam Leffler  * Add an 11h Channel Switch Announcement element to a frame.
1485b032f27cSSam Leffler  * Note that we use the per-vap CSA count to adjust the global
1486b032f27cSSam Leffler  * counter so we can use this routine to form probe response
1487b032f27cSSam Leffler  * frames and get the current count.
1488b032f27cSSam Leffler  */
1489b032f27cSSam Leffler static uint8_t *
1490b032f27cSSam Leffler ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
1491b032f27cSSam Leffler {
1492b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1493b032f27cSSam Leffler 	struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
1494b032f27cSSam Leffler 
1495c70761e6SSam Leffler 	csa->csa_ie = IEEE80211_ELEMID_CSA;
1496b032f27cSSam Leffler 	csa->csa_len = 3;
1497b032f27cSSam Leffler 	csa->csa_mode = 1;		/* XXX force quiet on channel */
1498b032f27cSSam Leffler 	csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
1499b032f27cSSam Leffler 	csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
1500b032f27cSSam Leffler 	return frm + sizeof(*csa);
1501b032f27cSSam Leffler }
1502b032f27cSSam Leffler 
1503b032f27cSSam Leffler /*
1504b032f27cSSam Leffler  * Add an 11h country information element to a frame.
1505b032f27cSSam Leffler  */
1506b032f27cSSam Leffler static uint8_t *
1507b032f27cSSam Leffler ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
1508b032f27cSSam Leffler {
1509b032f27cSSam Leffler 
1510b032f27cSSam Leffler 	if (ic->ic_countryie == NULL ||
1511b032f27cSSam Leffler 	    ic->ic_countryie_chan != ic->ic_bsschan) {
1512b032f27cSSam Leffler 		/*
1513b032f27cSSam Leffler 		 * Handle lazy construction of ie.  This is done on
1514b032f27cSSam Leffler 		 * first use and after a channel change that requires
1515b032f27cSSam Leffler 		 * re-calculation.
1516b032f27cSSam Leffler 		 */
1517b032f27cSSam Leffler 		if (ic->ic_countryie != NULL)
1518b032f27cSSam Leffler 			free(ic->ic_countryie, M_80211_NODE_IE);
1519b032f27cSSam Leffler 		ic->ic_countryie = ieee80211_alloc_countryie(ic);
1520b032f27cSSam Leffler 		if (ic->ic_countryie == NULL)
1521b032f27cSSam Leffler 			return frm;
1522b032f27cSSam Leffler 		ic->ic_countryie_chan = ic->ic_bsschan;
1523b032f27cSSam Leffler 	}
1524b032f27cSSam Leffler 	return add_appie(frm, ic->ic_countryie);
1525b032f27cSSam Leffler }
1526b032f27cSSam Leffler 
1527b032f27cSSam Leffler /*
1528af8418dcSSam Leffler  * Send a probe request frame with the specified ssid
1529af8418dcSSam Leffler  * and any optional information element data.
1530af8418dcSSam Leffler  */
1531af8418dcSSam Leffler int
1532af8418dcSSam Leffler ieee80211_send_probereq(struct ieee80211_node *ni,
153368e8e04eSSam Leffler 	const uint8_t sa[IEEE80211_ADDR_LEN],
153468e8e04eSSam Leffler 	const uint8_t da[IEEE80211_ADDR_LEN],
153568e8e04eSSam Leffler 	const uint8_t bssid[IEEE80211_ADDR_LEN],
1536b032f27cSSam Leffler 	const uint8_t *ssid, size_t ssidlen)
1537af8418dcSSam Leffler {
1538b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1539af8418dcSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
154016d7cbb1SSam Leffler 	const struct ieee80211_txparam *tp;
154116d7cbb1SSam Leffler 	struct ieee80211_bpf_params params;
1542af8418dcSSam Leffler 	struct ieee80211_frame *wh;
154341b3c790SSam Leffler 	const struct ieee80211_rateset *rs;
1544af8418dcSSam Leffler 	struct mbuf *m;
154568e8e04eSSam Leffler 	uint8_t *frm;
1546af8418dcSSam Leffler 
1547b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
1548b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
1549b032f27cSSam Leffler 		    "block %s frame in CAC state", "probe request");
1550b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
1551b032f27cSSam Leffler 		return EIO;		/* XXX */
1552b032f27cSSam Leffler 	}
1553b032f27cSSam Leffler 
1554af8418dcSSam Leffler 	/*
1555af8418dcSSam Leffler 	 * Hold a reference on the node so it doesn't go away until after
1556af8418dcSSam Leffler 	 * the xmit is complete all the way in the driver.  On error we
1557af8418dcSSam Leffler 	 * will remove our reference.
1558af8418dcSSam Leffler 	 */
1559b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1560af8418dcSSam Leffler 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1561af8418dcSSam Leffler 		__func__, __LINE__,
1562af8418dcSSam Leffler 		ni, ether_sprintf(ni->ni_macaddr),
1563af8418dcSSam Leffler 		ieee80211_node_refcnt(ni)+1);
1564af8418dcSSam Leffler 	ieee80211_ref_node(ni);
1565af8418dcSSam Leffler 
1566af8418dcSSam Leffler 	/*
1567af8418dcSSam Leffler 	 * prreq frame format
1568af8418dcSSam Leffler 	 *	[tlv] ssid
1569af8418dcSSam Leffler 	 *	[tlv] supported rates
1570b032f27cSSam Leffler 	 *	[tlv] RSN (optional)
1571af8418dcSSam Leffler 	 *	[tlv] extended supported rates
1572b032f27cSSam Leffler 	 *	[tlv] WPA (optional)
1573af8418dcSSam Leffler 	 *	[tlv] user-specified ie's
1574af8418dcSSam Leffler 	 */
1575af8418dcSSam Leffler 	m = ieee80211_getmgtframe(&frm,
157668e8e04eSSam Leffler 		 ic->ic_headroom + sizeof(struct ieee80211_frame),
1577af8418dcSSam Leffler 	       	 2 + IEEE80211_NWID_LEN
1578af8418dcSSam Leffler 	       + 2 + IEEE80211_RATE_SIZE
1579b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_wpa)
1580af8418dcSSam Leffler 	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1581b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_wpa)
1582b032f27cSSam Leffler 	       + (vap->iv_appie_probereq != NULL ?
1583b032f27cSSam Leffler 		   vap->iv_appie_probereq->ie_len : 0)
1584af8418dcSSam Leffler 	);
1585af8418dcSSam Leffler 	if (m == NULL) {
1586b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
1587af8418dcSSam Leffler 		ieee80211_free_node(ni);
1588af8418dcSSam Leffler 		return ENOMEM;
1589af8418dcSSam Leffler 	}
1590af8418dcSSam Leffler 
1591af8418dcSSam Leffler 	frm = ieee80211_add_ssid(frm, ssid, ssidlen);
159241b3c790SSam Leffler 	rs = ieee80211_get_suprates(ic, ic->ic_curchan);
159341b3c790SSam Leffler 	frm = ieee80211_add_rates(frm, rs);
1594b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA2) {
1595b032f27cSSam Leffler 		if (vap->iv_rsn_ie != NULL)
1596b032f27cSSam Leffler 			frm = add_ie(frm, vap->iv_rsn_ie);
1597b032f27cSSam Leffler 		/* XXX else complain? */
1598af8418dcSSam Leffler 	}
1599b032f27cSSam Leffler 	frm = ieee80211_add_xrates(frm, rs);
1600b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA1) {
1601b032f27cSSam Leffler 		if (vap->iv_wpa_ie != NULL)
1602b032f27cSSam Leffler 			frm = add_ie(frm, vap->iv_wpa_ie);
1603b032f27cSSam Leffler 		/* XXX else complain? */
1604b032f27cSSam Leffler 	}
1605b032f27cSSam Leffler 	if (vap->iv_appie_probereq != NULL)
1606b032f27cSSam Leffler 		frm = add_appie(frm, vap->iv_appie_probereq);
160768e8e04eSSam Leffler 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1608af8418dcSSam Leffler 
160916d7cbb1SSam Leffler 	KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
161016d7cbb1SSam Leffler 	    ("leading space %zd", M_LEADINGSPACE(m)));
1611af8418dcSSam Leffler 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
161216d7cbb1SSam Leffler 	if (m == NULL) {
161316d7cbb1SSam Leffler 		/* NB: cannot happen */
161416d7cbb1SSam Leffler 		ieee80211_free_node(ni);
1615af8418dcSSam Leffler 		return ENOMEM;
161616d7cbb1SSam Leffler 	}
1617af8418dcSSam Leffler 
1618af8418dcSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
16199e80b1dfSSam Leffler 	ieee80211_send_setup(ni, m,
1620af8418dcSSam Leffler 	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
16218ac160cdSSam Leffler 	     IEEE80211_NONQOS_TID, sa, da, bssid);
1622af8418dcSSam Leffler 	/* XXX power management? */
1623c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
1624af8418dcSSam Leffler 
1625b032f27cSSam Leffler 	M_WME_SETAC(m, WME_AC_BE);
1626b032f27cSSam Leffler 
1627af8418dcSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_probereq);
1628af8418dcSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_mgmt);
1629af8418dcSSam Leffler 
1630b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1631b032f27cSSam Leffler 	    "send probe req on channel %u bssid %s ssid \"%.*s\"\n",
1632b032f27cSSam Leffler 	    ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(bssid),
1633b032f27cSSam Leffler 	    ssidlen, ssid);
1634af8418dcSSam Leffler 
163516d7cbb1SSam Leffler 	memset(&params, 0, sizeof(params));
163616d7cbb1SSam Leffler 	params.ibp_pri = M_WME_GETAC(m);
163716d7cbb1SSam Leffler 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
163816d7cbb1SSam Leffler 	params.ibp_rate0 = tp->mgmtrate;
163916d7cbb1SSam Leffler 	if (IEEE80211_IS_MULTICAST(da)) {
164016d7cbb1SSam Leffler 		params.ibp_flags |= IEEE80211_BPF_NOACK;
164116d7cbb1SSam Leffler 		params.ibp_try0 = 1;
164216d7cbb1SSam Leffler 	} else
164316d7cbb1SSam Leffler 		params.ibp_try0 = tp->maxretry;
164416d7cbb1SSam Leffler 	params.ibp_power = ni->ni_txpower;
164516d7cbb1SSam Leffler 	return ic->ic_raw_xmit(ni, m, &params);
1646af8418dcSSam Leffler }
1647af8418dcSSam Leffler 
1648af8418dcSSam Leffler /*
1649667dad55SSam Leffler  * Calculate capability information for mgt frames.
1650667dad55SSam Leffler  */
165168e8e04eSSam Leffler static uint16_t
1652b032f27cSSam Leffler getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
1653667dad55SSam Leffler {
1654b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
165568e8e04eSSam Leffler 	uint16_t capinfo;
1656667dad55SSam Leffler 
1657b032f27cSSam Leffler 	KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
1658667dad55SSam Leffler 
1659b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1660667dad55SSam Leffler 		capinfo = IEEE80211_CAPINFO_ESS;
1661b032f27cSSam Leffler 	else if (vap->iv_opmode == IEEE80211_M_IBSS)
1662667dad55SSam Leffler 		capinfo = IEEE80211_CAPINFO_IBSS;
1663667dad55SSam Leffler 	else
1664667dad55SSam Leffler 		capinfo = 0;
1665b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY)
1666667dad55SSam Leffler 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
1667667dad55SSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1668667dad55SSam Leffler 	    IEEE80211_IS_CHAN_2GHZ(chan))
1669667dad55SSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1670667dad55SSam Leffler 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
1671667dad55SSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1672b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
1673b032f27cSSam Leffler 		capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
1674667dad55SSam Leffler 	return capinfo;
1675667dad55SSam Leffler }
1676667dad55SSam Leffler 
1677667dad55SSam Leffler /*
16780a915fadSSam Leffler  * Send a management frame.  The node is for the destination (or ic_bss
16790a915fadSSam Leffler  * when in station mode).  Nodes other than ic_bss have their reference
16800a915fadSSam Leffler  * count bumped to reflect our use for an indeterminant time.
16810a915fadSSam Leffler  */
16821a1e1d21SSam Leffler int
1683b032f27cSSam Leffler ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
16841a1e1d21SSam Leffler {
16851b6167d2SSam Leffler #define	HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
1686b032f27cSSam Leffler #define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
1687b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
1688b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1689b032f27cSSam Leffler 	struct ieee80211_node *bss = vap->iv_bss;
16908ac160cdSSam Leffler 	struct ieee80211_bpf_params params;
16911a1e1d21SSam Leffler 	struct mbuf *m;
169268e8e04eSSam Leffler 	uint8_t *frm;
169368e8e04eSSam Leffler 	uint16_t capinfo;
169468e8e04eSSam Leffler 	int has_challenge, is_shared_key, ret, status;
16951a1e1d21SSam Leffler 
16960a915fadSSam Leffler 	KASSERT(ni != NULL, ("null node"));
16970a915fadSSam Leffler 
16980a915fadSSam Leffler 	/*
16990a915fadSSam Leffler 	 * Hold a reference on the node so it doesn't go away until after
17000a915fadSSam Leffler 	 * the xmit is complete all the way in the driver.  On error we
17010a915fadSSam Leffler 	 * will remove our reference.
17020a915fadSSam Leffler 	 */
1703b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
170449a15236SSam Leffler 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
17058a1b9b6aSSam Leffler 		__func__, __LINE__,
170649a15236SSam Leffler 		ni, ether_sprintf(ni->ni_macaddr),
17078a1b9b6aSSam Leffler 		ieee80211_node_refcnt(ni)+1);
17080a915fadSSam Leffler 	ieee80211_ref_node(ni);
17098a1b9b6aSSam Leffler 
17108ac160cdSSam Leffler 	memset(&params, 0, sizeof(params));
17111a1e1d21SSam Leffler 	switch (type) {
17121a1e1d21SSam Leffler 
17131a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_AUTH:
17148a1b9b6aSSam Leffler 		status = arg >> 16;
17158a1b9b6aSSam Leffler 		arg &= 0xffff;
17168a1b9b6aSSam Leffler 		has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
17178a1b9b6aSSam Leffler 		    arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
17188a1b9b6aSSam Leffler 		    ni->ni_challenge != NULL);
17198a1b9b6aSSam Leffler 
17208a1b9b6aSSam Leffler 		/*
17218a1b9b6aSSam Leffler 		 * Deduce whether we're doing open authentication or
17228a1b9b6aSSam Leffler 		 * shared key authentication.  We do the latter if
17238a1b9b6aSSam Leffler 		 * we're in the middle of a shared key authentication
17248a1b9b6aSSam Leffler 		 * handshake or if we're initiating an authentication
17258a1b9b6aSSam Leffler 		 * request and configured to use shared key.
17268a1b9b6aSSam Leffler 		 */
17278a1b9b6aSSam Leffler 		is_shared_key = has_challenge ||
17288a1b9b6aSSam Leffler 		     arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
17298a1b9b6aSSam Leffler 		     (arg == IEEE80211_AUTH_SHARED_REQUEST &&
1730b032f27cSSam Leffler 		      bss->ni_authmode == IEEE80211_AUTH_SHARED);
17318a1b9b6aSSam Leffler 
17328a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
173368e8e04eSSam Leffler 			  ic->ic_headroom + sizeof(struct ieee80211_frame),
173468e8e04eSSam Leffler 			  3 * sizeof(uint16_t)
17358a1b9b6aSSam Leffler 			+ (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
173668e8e04eSSam Leffler 				sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)
17378a1b9b6aSSam Leffler 		);
17381a1e1d21SSam Leffler 		if (m == NULL)
17398a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
17408a1b9b6aSSam Leffler 
174168e8e04eSSam Leffler 		((uint16_t *)frm)[0] =
17428a1b9b6aSSam Leffler 		    (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
17438a1b9b6aSSam Leffler 		                    : htole16(IEEE80211_AUTH_ALG_OPEN);
174468e8e04eSSam Leffler 		((uint16_t *)frm)[1] = htole16(arg);	/* sequence number */
174568e8e04eSSam Leffler 		((uint16_t *)frm)[2] = htole16(status);/* status */
17468a1b9b6aSSam Leffler 
17478a1b9b6aSSam Leffler 		if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
174868e8e04eSSam Leffler 			((uint16_t *)frm)[3] =
17498a1b9b6aSSam Leffler 			    htole16((IEEE80211_CHALLENGE_LEN << 8) |
17508a1b9b6aSSam Leffler 			    IEEE80211_ELEMID_CHALLENGE);
175168e8e04eSSam Leffler 			memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
17528a1b9b6aSSam Leffler 			    IEEE80211_CHALLENGE_LEN);
17538a1b9b6aSSam Leffler 			m->m_pkthdr.len = m->m_len =
175468e8e04eSSam Leffler 				4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
17558a1b9b6aSSam Leffler 			if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
1756b032f27cSSam Leffler 				IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1757b032f27cSSam Leffler 				    "request encrypt frame (%s)", __func__);
17588ac160cdSSam Leffler 				/* mark frame for encryption */
17598ac160cdSSam Leffler 				params.ibp_flags |= IEEE80211_BPF_CRYPTO;
17608a1b9b6aSSam Leffler 			}
17618a1b9b6aSSam Leffler 		} else
176268e8e04eSSam Leffler 			m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
17638a1b9b6aSSam Leffler 
17648a1b9b6aSSam Leffler 		/* XXX not right for shared key */
17658a1b9b6aSSam Leffler 		if (status == IEEE80211_STATUS_SUCCESS)
17668a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_auth);
17678a1b9b6aSSam Leffler 		else
17688a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_auth_fail);
17698a1b9b6aSSam Leffler 
1770b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_STA)
177168e8e04eSSam Leffler 			ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
1772b032f27cSSam Leffler 				(void *) vap->iv_state);
17731a1e1d21SSam Leffler 		break;
17741a1e1d21SSam Leffler 
17751a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
1776b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1777b032f27cSSam Leffler 		    "send station deauthenticate (reason %d)", arg);
177868e8e04eSSam Leffler 		m = ieee80211_getmgtframe(&frm,
177968e8e04eSSam Leffler 			ic->ic_headroom + sizeof(struct ieee80211_frame),
178068e8e04eSSam Leffler 			sizeof(uint16_t));
17811a1e1d21SSam Leffler 		if (m == NULL)
17828a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
178368e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(arg);	/* reason */
178468e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
17858a1b9b6aSSam Leffler 
17868a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, tx_deauth);
17878a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
17888a1b9b6aSSam Leffler 
1789e4918ecdSSam Leffler 		ieee80211_node_unauthorize(ni);		/* port closed */
17901a1e1d21SSam Leffler 		break;
17911a1e1d21SSam Leffler 
17921a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
17931a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
17941a1e1d21SSam Leffler 		/*
17951a1e1d21SSam Leffler 		 * asreq frame format
17961a1e1d21SSam Leffler 		 *	[2] capability information
17971a1e1d21SSam Leffler 		 *	[2] listen interval
17981a1e1d21SSam Leffler 		 *	[6*] current AP address (reassoc only)
17991a1e1d21SSam Leffler 		 *	[tlv] ssid
18001a1e1d21SSam Leffler 		 *	[tlv] supported rates
18011a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
1802b032f27cSSam Leffler 		 *	[4] power capability (optional)
1803b032f27cSSam Leffler 		 *	[28] supported channels (optional)
180468e8e04eSSam Leffler 		 *	[tlv] HT capabilities
1805b032f27cSSam Leffler 		 *	[tlv] WME (optional)
180668e8e04eSSam Leffler 		 *	[tlv] Vendor OUI HT capabilities (optional)
180768e8e04eSSam Leffler 		 *	[tlv] Atheros capabilities (if negotiated)
1808b032f27cSSam Leffler 		 *	[tlv] AppIE's (optional)
18091a1e1d21SSam Leffler 		 */
18108a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
181168e8e04eSSam Leffler 			 ic->ic_headroom + sizeof(struct ieee80211_frame),
181268e8e04eSSam Leffler 			 sizeof(uint16_t)
181368e8e04eSSam Leffler 		       + sizeof(uint16_t)
18141a1e1d21SSam Leffler 		       + IEEE80211_ADDR_LEN
18158a1b9b6aSSam Leffler 		       + 2 + IEEE80211_NWID_LEN
18161a1e1d21SSam Leffler 		       + 2 + IEEE80211_RATE_SIZE
18178a1b9b6aSSam Leffler 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1818b032f27cSSam Leffler 		       + 4
1819b032f27cSSam Leffler 		       + 2 + 26
18208a1b9b6aSSam Leffler 		       + sizeof(struct ieee80211_wme_info)
1821b032f27cSSam Leffler 		       + sizeof(struct ieee80211_ie_htcap)
1822b032f27cSSam Leffler 		       + 4 + sizeof(struct ieee80211_ie_htcap)
1823616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
182468e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ath_ie)
1825616190d0SSam Leffler #endif
1826b032f27cSSam Leffler 		       + (vap->iv_appie_wpa != NULL ?
1827b032f27cSSam Leffler 				vap->iv_appie_wpa->ie_len : 0)
1828b032f27cSSam Leffler 		       + (vap->iv_appie_assocreq != NULL ?
1829b032f27cSSam Leffler 				vap->iv_appie_assocreq->ie_len : 0)
18308a1b9b6aSSam Leffler 		);
18311a1e1d21SSam Leffler 		if (m == NULL)
18328a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
18331a1e1d21SSam Leffler 
1834b032f27cSSam Leffler 		KASSERT(vap->iv_opmode == IEEE80211_M_STA,
1835b032f27cSSam Leffler 		    ("wrong mode %u", vap->iv_opmode));
1836667dad55SSam Leffler 		capinfo = IEEE80211_CAPINFO_ESS;
1837b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_PRIVACY)
18381a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
18394f2e09c4SSam Leffler 		/*
18404f2e09c4SSam Leffler 		 * NB: Some 11a AP's reject the request when
18414f2e09c4SSam Leffler 		 *     short premable is set.
18424f2e09c4SSam Leffler 		 */
18434f2e09c4SSam Leffler 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1844b5c99415SSam Leffler 		    IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
18451a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
184668e8e04eSSam Leffler 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
18478a1b9b6aSSam Leffler 		    (ic->ic_caps & IEEE80211_C_SHSLOT))
18481a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
18491b6167d2SSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
1850b032f27cSSam Leffler 		    (vap->iv_flags & IEEE80211_F_DOTH))
18511b6167d2SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
185268e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(capinfo);
18531a1e1d21SSam Leffler 		frm += 2;
18541a1e1d21SSam Leffler 
1855b032f27cSSam Leffler 		KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
185668e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
1857b032f27cSSam Leffler 						    bss->ni_intval));
18581a1e1d21SSam Leffler 		frm += 2;
18591a1e1d21SSam Leffler 
18601a1e1d21SSam Leffler 		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1861b032f27cSSam Leffler 			IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
18621a1e1d21SSam Leffler 			frm += IEEE80211_ADDR_LEN;
18631a1e1d21SSam Leffler 		}
18641a1e1d21SSam Leffler 
18651a1e1d21SSam Leffler 		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
18661a1e1d21SSam Leffler 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
1867b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_WPA2) {
1868b032f27cSSam Leffler 			if (vap->iv_rsn_ie != NULL)
1869b032f27cSSam Leffler 				frm = add_ie(frm, vap->iv_rsn_ie);
1870b032f27cSSam Leffler 			/* XXX else complain? */
18718a1b9b6aSSam Leffler 		}
1872b032f27cSSam Leffler 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1873b032f27cSSam Leffler 		if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
1874b032f27cSSam Leffler 			frm = ieee80211_add_powercapability(frm,
1875b032f27cSSam Leffler 			    ic->ic_curchan);
1876b032f27cSSam Leffler 			frm = ieee80211_add_supportedchannels(frm, ic);
1877b032f27cSSam Leffler 		}
18782bfc8a91SSam Leffler 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
1879b032f27cSSam Leffler 		    ni->ni_ies.htcap_ie != NULL &&
1880b032f27cSSam Leffler 		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP)
1881b032f27cSSam Leffler 			frm = ieee80211_add_htcap(frm, ni);
1882b032f27cSSam Leffler 		if (vap->iv_flags & IEEE80211_F_WPA1) {
1883b032f27cSSam Leffler 			if (vap->iv_wpa_ie != NULL)
1884b032f27cSSam Leffler 				frm = add_ie(frm, vap->iv_wpa_ie);
1885b032f27cSSam Leffler 			/* XXX else complain */
1886b032f27cSSam Leffler 		}
1887b032f27cSSam Leffler 		if ((ic->ic_flags & IEEE80211_F_WME) &&
1888b032f27cSSam Leffler 		    ni->ni_ies.wme_ie != NULL)
1889b032f27cSSam Leffler 			frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
18902bfc8a91SSam Leffler 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
1891b032f27cSSam Leffler 		    ni->ni_ies.htcap_ie != NULL &&
1892b032f27cSSam Leffler 		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR)
1893b032f27cSSam Leffler 			frm = ieee80211_add_htcap_vendor(frm, ni);
1894616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
18954207227cSSam Leffler 		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
1896b032f27cSSam Leffler 			frm = ieee80211_add_ath(frm,
1897b032f27cSSam Leffler 				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
18984207227cSSam Leffler 				((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
18994207227cSSam Leffler 				 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
19004207227cSSam Leffler 				vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
19014207227cSSam Leffler 		}
1902616190d0SSam Leffler #endif /* IEEE80211_SUPPORT_SUPERG */
1903b032f27cSSam Leffler 		if (vap->iv_appie_assocreq != NULL)
1904b032f27cSSam Leffler 			frm = add_appie(frm, vap->iv_appie_assocreq);
190568e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
19061a1e1d21SSam Leffler 
190768e8e04eSSam Leffler 		ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
1908b032f27cSSam Leffler 			(void *) vap->iv_state);
19091a1e1d21SSam Leffler 		break;
19101a1e1d21SSam Leffler 
19111a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
19121a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
19131a1e1d21SSam Leffler 		/*
191468e8e04eSSam Leffler 		 * asresp frame format
19151a1e1d21SSam Leffler 		 *	[2] capability information
19161a1e1d21SSam Leffler 		 *	[2] status
19171a1e1d21SSam Leffler 		 *	[2] association ID
19181a1e1d21SSam Leffler 		 *	[tlv] supported rates
19191a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
1920b032f27cSSam Leffler 		 *	[tlv] HT capabilities (standard, if STA enabled)
1921b032f27cSSam Leffler 		 *	[tlv] HT information (standard, if STA enabled)
1922b032f27cSSam Leffler 		 *	[tlv] WME (if configured and STA enabled)
1923b032f27cSSam Leffler 		 *	[tlv] HT capabilities (vendor OUI, if STA enabled)
1924b032f27cSSam Leffler 		 *	[tlv] HT information (vendor OUI, if STA enabled)
1925b032f27cSSam Leffler 		 *	[tlv] Atheros capabilities (if STA enabled)
1926b032f27cSSam Leffler 		 *	[tlv] AppIE's (optional)
19271a1e1d21SSam Leffler 		 */
19288a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
192968e8e04eSSam Leffler 			 ic->ic_headroom + sizeof(struct ieee80211_frame),
193068e8e04eSSam Leffler 			 sizeof(uint16_t)
193168e8e04eSSam Leffler 		       + sizeof(uint16_t)
193268e8e04eSSam Leffler 		       + sizeof(uint16_t)
19331a1e1d21SSam Leffler 		       + 2 + IEEE80211_RATE_SIZE
19348a1b9b6aSSam Leffler 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
193568e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ie_htcap) + 4
193668e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ie_htinfo) + 4
1937b032f27cSSam Leffler 		       + sizeof(struct ieee80211_wme_param)
1938616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
193968e8e04eSSam Leffler 		       + sizeof(struct ieee80211_ath_ie)
1940616190d0SSam Leffler #endif
1941b032f27cSSam Leffler 		       + (vap->iv_appie_assocresp != NULL ?
1942b032f27cSSam Leffler 				vap->iv_appie_assocresp->ie_len : 0)
19438a1b9b6aSSam Leffler 		);
19441a1e1d21SSam Leffler 		if (m == NULL)
19458a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
19461a1e1d21SSam Leffler 
1947b032f27cSSam Leffler 		capinfo = getcapinfo(vap, bss->ni_chan);
194868e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(capinfo);
19491a1e1d21SSam Leffler 		frm += 2;
19501a1e1d21SSam Leffler 
195168e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(arg);	/* status */
19521a1e1d21SSam Leffler 		frm += 2;
19531a1e1d21SSam Leffler 
19548a1b9b6aSSam Leffler 		if (arg == IEEE80211_STATUS_SUCCESS) {
195568e8e04eSSam Leffler 			*(uint16_t *)frm = htole16(ni->ni_associd);
19568a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_assoc);
19578a1b9b6aSSam Leffler 		} else
19588a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_assoc_fail);
19591a1e1d21SSam Leffler 		frm += 2;
19601a1e1d21SSam Leffler 
19611a1e1d21SSam Leffler 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
19621a1e1d21SSam Leffler 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
196368e8e04eSSam Leffler 		/* NB: respond according to what we received */
19641b6167d2SSam Leffler 		if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
196568e8e04eSSam Leffler 			frm = ieee80211_add_htcap(frm, ni);
196668e8e04eSSam Leffler 			frm = ieee80211_add_htinfo(frm, ni);
196768e8e04eSSam Leffler 		}
1968b032f27cSSam Leffler 		if ((vap->iv_flags & IEEE80211_F_WME) &&
1969b032f27cSSam Leffler 		    ni->ni_ies.wme_ie != NULL)
19701b6167d2SSam Leffler 			frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
19711b6167d2SSam Leffler 		if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
19721b6167d2SSam Leffler 			frm = ieee80211_add_htcap_vendor(frm, ni);
19731b6167d2SSam Leffler 			frm = ieee80211_add_htinfo_vendor(frm, ni);
197468e8e04eSSam Leffler 		}
1975616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1976b032f27cSSam Leffler 		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
197768e8e04eSSam Leffler 			frm = ieee80211_add_ath(frm,
1978b032f27cSSam Leffler 				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
19794207227cSSam Leffler 				((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
19804207227cSSam Leffler 				 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
19814207227cSSam Leffler 				vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
1982616190d0SSam Leffler #endif /* IEEE80211_SUPPORT_SUPERG */
1983b032f27cSSam Leffler 		if (vap->iv_appie_assocresp != NULL)
1984b032f27cSSam Leffler 			frm = add_appie(frm, vap->iv_appie_assocresp);
198568e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
19861a1e1d21SSam Leffler 		break;
19871a1e1d21SSam Leffler 
19881a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
1989b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1990b032f27cSSam Leffler 		    "send station disassociate (reason %d)", arg);
199168e8e04eSSam Leffler 		m = ieee80211_getmgtframe(&frm,
199268e8e04eSSam Leffler 			ic->ic_headroom + sizeof(struct ieee80211_frame),
199368e8e04eSSam Leffler 			sizeof(uint16_t));
19941a1e1d21SSam Leffler 		if (m == NULL)
19958a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
199668e8e04eSSam Leffler 		*(uint16_t *)frm = htole16(arg);	/* reason */
199768e8e04eSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
19988a1b9b6aSSam Leffler 
19998a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, tx_disassoc);
20008a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
20011a1e1d21SSam Leffler 		break;
20021a1e1d21SSam Leffler 
20031a1e1d21SSam Leffler 	default:
2004b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
2005b032f27cSSam Leffler 		    "invalid mgmt frame type %u", type);
20061be50176SSam Leffler 		senderr(EINVAL, is_tx_unknownmgt);
20070a915fadSSam Leffler 		/* NOTREACHED */
20081a1e1d21SSam Leffler 	}
200968e8e04eSSam Leffler 
20108ac160cdSSam Leffler 	/* NB: force non-ProbeResp frames to the highest queue */
20118ac160cdSSam Leffler 	params.ibp_pri = WME_AC_VO;
20128ac160cdSSam Leffler 	params.ibp_rate0 = bss->ni_txparms->mgmtrate;
20138ac160cdSSam Leffler 	/* NB: we know all frames are unicast */
20148ac160cdSSam Leffler 	params.ibp_try0 = bss->ni_txparms->maxretry;
20158ac160cdSSam Leffler 	params.ibp_power = bss->ni_txpower;
20168ac160cdSSam Leffler 	return ieee80211_mgmt_output(ni, m, type, &params);
20170a915fadSSam Leffler bad:
20188a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
20191a1e1d21SSam Leffler 	return ret;
20200a915fadSSam Leffler #undef senderr
20211b6167d2SSam Leffler #undef HTFLAGS
20221a1e1d21SSam Leffler }
20238a1b9b6aSSam Leffler 
2024b032f27cSSam Leffler /*
2025b032f27cSSam Leffler  * Return an mbuf with a probe response frame in it.
2026b032f27cSSam Leffler  * Space is left to prepend and 802.11 header at the
2027b032f27cSSam Leffler  * front but it's left to the caller to fill in.
2028b032f27cSSam Leffler  */
2029b032f27cSSam Leffler struct mbuf *
2030b032f27cSSam Leffler ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
2031b032f27cSSam Leffler {
2032b032f27cSSam Leffler 	struct ieee80211vap *vap = bss->ni_vap;
2033b032f27cSSam Leffler 	struct ieee80211com *ic = bss->ni_ic;
2034b032f27cSSam Leffler 	const struct ieee80211_rateset *rs;
2035b032f27cSSam Leffler 	struct mbuf *m;
2036b032f27cSSam Leffler 	uint16_t capinfo;
2037b032f27cSSam Leffler 	uint8_t *frm;
2038b032f27cSSam Leffler 
2039b032f27cSSam Leffler 	/*
2040b032f27cSSam Leffler 	 * probe response frame format
2041b032f27cSSam Leffler 	 *	[8] time stamp
2042b032f27cSSam Leffler 	 *	[2] beacon interval
2043b032f27cSSam Leffler 	 *	[2] cabability information
2044b032f27cSSam Leffler 	 *	[tlv] ssid
2045b032f27cSSam Leffler 	 *	[tlv] supported rates
2046b032f27cSSam Leffler 	 *	[tlv] parameter set (FH/DS)
2047b032f27cSSam Leffler 	 *	[tlv] parameter set (IBSS)
2048b032f27cSSam Leffler 	 *	[tlv] country (optional)
2049b032f27cSSam Leffler 	 *	[3] power control (optional)
2050b032f27cSSam Leffler 	 *	[5] channel switch announcement (CSA) (optional)
2051b032f27cSSam Leffler 	 *	[tlv] extended rate phy (ERP)
2052b032f27cSSam Leffler 	 *	[tlv] extended supported rates
2053688fe74dSSam Leffler 	 *	[tlv] RSN (optional)
2054b032f27cSSam Leffler 	 *	[tlv] HT capabilities
2055b032f27cSSam Leffler 	 *	[tlv] HT information
2056b032f27cSSam Leffler 	 *	[tlv] WPA (optional)
2057b032f27cSSam Leffler 	 *	[tlv] WME (optional)
2058b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT capabilities (optional)
2059b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT information (optional)
2060b032f27cSSam Leffler 	 *	[tlv] Atheros capabilities
2061b032f27cSSam Leffler 	 *	[tlv] AppIE's (optional)
2062b032f27cSSam Leffler 	 */
2063b032f27cSSam Leffler 	m = ieee80211_getmgtframe(&frm,
2064b032f27cSSam Leffler 		 ic->ic_headroom + sizeof(struct ieee80211_frame),
2065b032f27cSSam Leffler 		 8
2066b032f27cSSam Leffler 	       + sizeof(uint16_t)
2067b032f27cSSam Leffler 	       + sizeof(uint16_t)
2068b032f27cSSam Leffler 	       + 2 + IEEE80211_NWID_LEN
2069b032f27cSSam Leffler 	       + 2 + IEEE80211_RATE_SIZE
2070b032f27cSSam Leffler 	       + 7	/* max(7,3) */
2071b032f27cSSam Leffler 	       + IEEE80211_COUNTRY_MAX_SIZE
2072b032f27cSSam Leffler 	       + 3
2073b032f27cSSam Leffler 	       + sizeof(struct ieee80211_csa_ie)
2074b032f27cSSam Leffler 	       + 3
2075b032f27cSSam Leffler 	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2076688fe74dSSam Leffler 	       + sizeof(struct ieee80211_ie_wpa)
2077b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_htcap)
2078b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_htinfo)
2079b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ie_wpa)
2080b032f27cSSam Leffler 	       + sizeof(struct ieee80211_wme_param)
2081b032f27cSSam Leffler 	       + 4 + sizeof(struct ieee80211_ie_htcap)
2082b032f27cSSam Leffler 	       + 4 + sizeof(struct ieee80211_ie_htinfo)
2083616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
2084b032f27cSSam Leffler 	       + sizeof(struct ieee80211_ath_ie)
2085616190d0SSam Leffler #endif
2086b032f27cSSam Leffler 	       + (vap->iv_appie_proberesp != NULL ?
2087b032f27cSSam Leffler 			vap->iv_appie_proberesp->ie_len : 0)
2088b032f27cSSam Leffler 	);
2089b032f27cSSam Leffler 	if (m == NULL) {
2090b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
2091b032f27cSSam Leffler 		return NULL;
2092b032f27cSSam Leffler 	}
2093b032f27cSSam Leffler 
2094b032f27cSSam Leffler 	memset(frm, 0, 8);	/* timestamp should be filled later */
2095b032f27cSSam Leffler 	frm += 8;
2096b032f27cSSam Leffler 	*(uint16_t *)frm = htole16(bss->ni_intval);
2097b032f27cSSam Leffler 	frm += 2;
2098b032f27cSSam Leffler 	capinfo = getcapinfo(vap, bss->ni_chan);
2099b032f27cSSam Leffler 	*(uint16_t *)frm = htole16(capinfo);
2100b032f27cSSam Leffler 	frm += 2;
2101b032f27cSSam Leffler 
2102b032f27cSSam Leffler 	frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
2103b032f27cSSam Leffler 	rs = ieee80211_get_suprates(ic, bss->ni_chan);
2104b032f27cSSam Leffler 	frm = ieee80211_add_rates(frm, rs);
2105b032f27cSSam Leffler 
2106b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
2107b032f27cSSam Leffler 		*frm++ = IEEE80211_ELEMID_FHPARMS;
2108b032f27cSSam Leffler 		*frm++ = 5;
2109b032f27cSSam Leffler 		*frm++ = bss->ni_fhdwell & 0x00ff;
2110b032f27cSSam Leffler 		*frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
2111b032f27cSSam Leffler 		*frm++ = IEEE80211_FH_CHANSET(
2112b032f27cSSam Leffler 		    ieee80211_chan2ieee(ic, bss->ni_chan));
2113b032f27cSSam Leffler 		*frm++ = IEEE80211_FH_CHANPAT(
2114b032f27cSSam Leffler 		    ieee80211_chan2ieee(ic, bss->ni_chan));
2115b032f27cSSam Leffler 		*frm++ = bss->ni_fhindex;
2116b032f27cSSam Leffler 	} else {
2117b032f27cSSam Leffler 		*frm++ = IEEE80211_ELEMID_DSPARMS;
2118b032f27cSSam Leffler 		*frm++ = 1;
2119b032f27cSSam Leffler 		*frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
2120b032f27cSSam Leffler 	}
2121b032f27cSSam Leffler 
2122b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
2123b032f27cSSam Leffler 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
2124b032f27cSSam Leffler 		*frm++ = 2;
2125b032f27cSSam Leffler 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
2126b032f27cSSam Leffler 	}
2127b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2128b032f27cSSam Leffler 	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2129b032f27cSSam Leffler 		frm = ieee80211_add_countryie(frm, ic);
2130b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_DOTH) {
2131b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
2132b032f27cSSam Leffler 			frm = ieee80211_add_powerconstraint(frm, vap);
2133b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2134b032f27cSSam Leffler 			frm = ieee80211_add_csa(frm, vap);
2135b032f27cSSam Leffler 	}
2136b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
2137b032f27cSSam Leffler 		frm = ieee80211_add_erp(frm, ic);
2138b032f27cSSam Leffler 	frm = ieee80211_add_xrates(frm, rs);
2139688fe74dSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA2) {
2140688fe74dSSam Leffler 		if (vap->iv_rsn_ie != NULL)
2141688fe74dSSam Leffler 			frm = add_ie(frm, vap->iv_rsn_ie);
2142688fe74dSSam Leffler 		/* XXX else complain? */
2143688fe74dSSam Leffler 	}
2144b032f27cSSam Leffler 	/*
2145b032f27cSSam Leffler 	 * NB: legacy 11b clients do not get certain ie's.
2146b032f27cSSam Leffler 	 *     The caller identifies such clients by passing
2147b032f27cSSam Leffler 	 *     a token in legacy to us.  Could expand this to be
2148b032f27cSSam Leffler 	 *     any legacy client for stuff like HT ie's.
2149b032f27cSSam Leffler 	 */
2150b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2151b032f27cSSam Leffler 	    legacy != IEEE80211_SEND_LEGACY_11B) {
2152b032f27cSSam Leffler 		frm = ieee80211_add_htcap(frm, bss);
2153b032f27cSSam Leffler 		frm = ieee80211_add_htinfo(frm, bss);
2154b032f27cSSam Leffler 	}
2155b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA1) {
2156b032f27cSSam Leffler 		if (vap->iv_wpa_ie != NULL)
2157b032f27cSSam Leffler 			frm = add_ie(frm, vap->iv_wpa_ie);
2158b032f27cSSam Leffler 		/* XXX else complain? */
2159b032f27cSSam Leffler 	}
2160b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WME)
2161b032f27cSSam Leffler 		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2162b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
21632bfc8a91SSam Leffler 	    (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
2164b032f27cSSam Leffler 	    legacy != IEEE80211_SEND_LEGACY_11B) {
2165b032f27cSSam Leffler 		frm = ieee80211_add_htcap_vendor(frm, bss);
2166b032f27cSSam Leffler 		frm = ieee80211_add_htinfo_vendor(frm, bss);
2167b032f27cSSam Leffler 	}
2168616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
21694207227cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
21704207227cSSam Leffler 	    legacy != IEEE80211_SEND_LEGACY_11B)
21714207227cSSam Leffler 		frm = ieee80211_add_athcaps(frm, bss);
2172616190d0SSam Leffler #endif
2173b032f27cSSam Leffler 	if (vap->iv_appie_proberesp != NULL)
2174b032f27cSSam Leffler 		frm = add_appie(frm, vap->iv_appie_proberesp);
2175b032f27cSSam Leffler 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2176b032f27cSSam Leffler 
2177b032f27cSSam Leffler 	return m;
2178b032f27cSSam Leffler }
2179b032f27cSSam Leffler 
2180b032f27cSSam Leffler /*
2181b032f27cSSam Leffler  * Send a probe response frame to the specified mac address.
2182b032f27cSSam Leffler  * This does not go through the normal mgt frame api so we
2183b032f27cSSam Leffler  * can specify the destination address and re-use the bss node
2184b032f27cSSam Leffler  * for the sta reference.
2185b032f27cSSam Leffler  */
2186b032f27cSSam Leffler int
2187b032f27cSSam Leffler ieee80211_send_proberesp(struct ieee80211vap *vap,
2188b032f27cSSam Leffler 	const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
2189b032f27cSSam Leffler {
2190b032f27cSSam Leffler 	struct ieee80211_node *bss = vap->iv_bss;
2191b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
2192b032f27cSSam Leffler 	struct ieee80211_frame *wh;
2193b032f27cSSam Leffler 	struct mbuf *m;
2194b032f27cSSam Leffler 
2195b032f27cSSam Leffler 	if (vap->iv_state == IEEE80211_S_CAC) {
2196b032f27cSSam Leffler 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
2197b032f27cSSam Leffler 		    "block %s frame in CAC state", "probe response");
2198b032f27cSSam Leffler 		vap->iv_stats.is_tx_badstate++;
2199b032f27cSSam Leffler 		return EIO;		/* XXX */
2200b032f27cSSam Leffler 	}
2201b032f27cSSam Leffler 
2202b032f27cSSam Leffler 	/*
2203b032f27cSSam Leffler 	 * Hold a reference on the node so it doesn't go away until after
2204b032f27cSSam Leffler 	 * the xmit is complete all the way in the driver.  On error we
2205b032f27cSSam Leffler 	 * will remove our reference.
2206b032f27cSSam Leffler 	 */
2207b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2208b032f27cSSam Leffler 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2209b032f27cSSam Leffler 	    __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
2210b032f27cSSam Leffler 	    ieee80211_node_refcnt(bss)+1);
2211b032f27cSSam Leffler 	ieee80211_ref_node(bss);
2212b032f27cSSam Leffler 
2213b032f27cSSam Leffler 	m = ieee80211_alloc_proberesp(bss, legacy);
2214b032f27cSSam Leffler 	if (m == NULL) {
2215b032f27cSSam Leffler 		ieee80211_free_node(bss);
2216b032f27cSSam Leffler 		return ENOMEM;
2217b032f27cSSam Leffler 	}
2218b032f27cSSam Leffler 
2219b032f27cSSam Leffler 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
2220b032f27cSSam Leffler 	KASSERT(m != NULL, ("no room for header"));
2221b032f27cSSam Leffler 
2222b032f27cSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
22239e80b1dfSSam Leffler 	ieee80211_send_setup(bss, m,
2224b032f27cSSam Leffler 	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
22258ac160cdSSam Leffler 	     IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
2226b032f27cSSam Leffler 	/* XXX power management? */
2227c1af44bdSSam Leffler 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
2228b032f27cSSam Leffler 
2229b032f27cSSam Leffler 	M_WME_SETAC(m, WME_AC_BE);
2230b032f27cSSam Leffler 
2231b032f27cSSam Leffler 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2232b032f27cSSam Leffler 	    "send probe resp on channel %u to %s%s\n",
2233b032f27cSSam Leffler 	    ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
2234b032f27cSSam Leffler 	    legacy ? " <legacy>" : "");
2235b032f27cSSam Leffler 	IEEE80211_NODE_STAT(bss, tx_mgmt);
2236b032f27cSSam Leffler 
2237b032f27cSSam Leffler 	return ic->ic_raw_xmit(bss, m, NULL);
2238b032f27cSSam Leffler }
2239b032f27cSSam Leffler 
2240b032f27cSSam Leffler /*
2241b032f27cSSam Leffler  * Allocate and build a RTS (Request To Send) control frame.
2242b032f27cSSam Leffler  */
2243b032f27cSSam Leffler struct mbuf *
2244b032f27cSSam Leffler ieee80211_alloc_rts(struct ieee80211com *ic,
2245b032f27cSSam Leffler 	const uint8_t ra[IEEE80211_ADDR_LEN],
2246b032f27cSSam Leffler 	const uint8_t ta[IEEE80211_ADDR_LEN],
2247b032f27cSSam Leffler 	uint16_t dur)
2248b032f27cSSam Leffler {
2249b032f27cSSam Leffler 	struct ieee80211_frame_rts *rts;
2250b032f27cSSam Leffler 	struct mbuf *m;
2251b032f27cSSam Leffler 
2252b032f27cSSam Leffler 	/* XXX honor ic_headroom */
2253b032f27cSSam Leffler 	m = m_gethdr(M_DONTWAIT, MT_DATA);
2254b032f27cSSam Leffler 	if (m != NULL) {
2255b032f27cSSam Leffler 		rts = mtod(m, struct ieee80211_frame_rts *);
2256b032f27cSSam Leffler 		rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2257b032f27cSSam Leffler 			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
2258b032f27cSSam Leffler 		rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2259b032f27cSSam Leffler 		*(u_int16_t *)rts->i_dur = htole16(dur);
2260b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(rts->i_ra, ra);
2261b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(rts->i_ta, ta);
2262b032f27cSSam Leffler 
2263b032f27cSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
2264b032f27cSSam Leffler 	}
2265b032f27cSSam Leffler 	return m;
2266b032f27cSSam Leffler }
2267b032f27cSSam Leffler 
2268b032f27cSSam Leffler /*
2269b032f27cSSam Leffler  * Allocate and build a CTS (Clear To Send) control frame.
2270b032f27cSSam Leffler  */
2271b032f27cSSam Leffler struct mbuf *
2272b032f27cSSam Leffler ieee80211_alloc_cts(struct ieee80211com *ic,
2273b032f27cSSam Leffler 	const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
2274b032f27cSSam Leffler {
2275b032f27cSSam Leffler 	struct ieee80211_frame_cts *cts;
2276b032f27cSSam Leffler 	struct mbuf *m;
2277b032f27cSSam Leffler 
2278b032f27cSSam Leffler 	/* XXX honor ic_headroom */
2279b032f27cSSam Leffler 	m = m_gethdr(M_DONTWAIT, MT_DATA);
2280b032f27cSSam Leffler 	if (m != NULL) {
2281b032f27cSSam Leffler 		cts = mtod(m, struct ieee80211_frame_cts *);
2282b032f27cSSam Leffler 		cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2283b032f27cSSam Leffler 			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
2284b032f27cSSam Leffler 		cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2285b032f27cSSam Leffler 		*(u_int16_t *)cts->i_dur = htole16(dur);
2286b032f27cSSam Leffler 		IEEE80211_ADDR_COPY(cts->i_ra, ra);
2287b032f27cSSam Leffler 
2288b032f27cSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
2289b032f27cSSam Leffler 	}
2290b032f27cSSam Leffler 	return m;
2291b032f27cSSam Leffler }
2292b032f27cSSam Leffler 
229368e8e04eSSam Leffler static void
229468e8e04eSSam Leffler ieee80211_tx_mgt_timeout(void *arg)
229568e8e04eSSam Leffler {
229668e8e04eSSam Leffler 	struct ieee80211_node *ni = arg;
2297b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
229868e8e04eSSam Leffler 
2299b032f27cSSam Leffler 	if (vap->iv_state != IEEE80211_S_INIT &&
2300b032f27cSSam Leffler 	    (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
230168e8e04eSSam Leffler 		/*
230268e8e04eSSam Leffler 		 * NB: it's safe to specify a timeout as the reason here;
230368e8e04eSSam Leffler 		 *     it'll only be used in the right state.
230468e8e04eSSam Leffler 		 */
2305b032f27cSSam Leffler 		ieee80211_new_state(vap, IEEE80211_S_SCAN,
230668e8e04eSSam Leffler 			IEEE80211_SCAN_FAIL_TIMEOUT);
230768e8e04eSSam Leffler 	}
230868e8e04eSSam Leffler }
230968e8e04eSSam Leffler 
231068e8e04eSSam Leffler static void
231168e8e04eSSam Leffler ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
231268e8e04eSSam Leffler {
2313b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
231468e8e04eSSam Leffler 	enum ieee80211_state ostate = (enum ieee80211_state) arg;
231568e8e04eSSam Leffler 
231668e8e04eSSam Leffler 	/*
231768e8e04eSSam Leffler 	 * Frame transmit completed; arrange timer callback.  If
231868e8e04eSSam Leffler 	 * transmit was successfuly we wait for response.  Otherwise
231968e8e04eSSam Leffler 	 * we arrange an immediate callback instead of doing the
232068e8e04eSSam Leffler 	 * callback directly since we don't know what state the driver
232168e8e04eSSam Leffler 	 * is in (e.g. what locks it is holding).  This work should
232268e8e04eSSam Leffler 	 * not be too time-critical and not happen too often so the
232368e8e04eSSam Leffler 	 * added overhead is acceptable.
232468e8e04eSSam Leffler 	 *
232568e8e04eSSam Leffler 	 * XXX what happens if !acked but response shows up before callback?
232668e8e04eSSam Leffler 	 */
2327b032f27cSSam Leffler 	if (vap->iv_state == ostate)
2328b032f27cSSam Leffler 		callout_reset(&vap->iv_mgtsend,
232968e8e04eSSam Leffler 			status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
233068e8e04eSSam Leffler 			ieee80211_tx_mgt_timeout, ni);
233168e8e04eSSam Leffler }
233268e8e04eSSam Leffler 
2333b032f27cSSam Leffler static void
2334b032f27cSSam Leffler ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
2335b032f27cSSam Leffler 	struct ieee80211_beacon_offsets *bo, struct ieee80211_node *ni)
23368a1b9b6aSSam Leffler {
2337b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2338b105a069SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2339b032f27cSSam Leffler 	struct ieee80211_rateset *rs = &ni->ni_rates;
234068e8e04eSSam Leffler 	uint16_t capinfo;
23418a1b9b6aSSam Leffler 
23428a1b9b6aSSam Leffler 	/*
23438a1b9b6aSSam Leffler 	 * beacon frame format
23448a1b9b6aSSam Leffler 	 *	[8] time stamp
23458a1b9b6aSSam Leffler 	 *	[2] beacon interval
23468a1b9b6aSSam Leffler 	 *	[2] cabability information
23478a1b9b6aSSam Leffler 	 *	[tlv] ssid
23488a1b9b6aSSam Leffler 	 *	[tlv] supported rates
23498a1b9b6aSSam Leffler 	 *	[3] parameter set (DS)
2350b032f27cSSam Leffler 	 *	[8] CF parameter set (optional)
23518a1b9b6aSSam Leffler 	 *	[tlv] parameter set (IBSS/TIM)
2352b032f27cSSam Leffler 	 *	[tlv] country (optional)
2353b032f27cSSam Leffler 	 *	[3] power control (optional)
2354b032f27cSSam Leffler 	 *	[5] channel switch announcement (CSA) (optional)
23558a1b9b6aSSam Leffler 	 *	[tlv] extended rate phy (ERP)
23568a1b9b6aSSam Leffler 	 *	[tlv] extended supported rates
2357688fe74dSSam Leffler 	 *	[tlv] RSN parameters
235868e8e04eSSam Leffler 	 *	[tlv] HT capabilities
235968e8e04eSSam Leffler 	 *	[tlv] HT information
2360b032f27cSSam Leffler 	 * XXX Vendor-specific OIDs (e.g. Atheros)
2361b032f27cSSam Leffler 	 *	[tlv] WPA parameters
2362b032f27cSSam Leffler 	 *	[tlv] WME parameters
236368e8e04eSSam Leffler 	 *	[tlv] Vendor OUI HT capabilities (optional)
236468e8e04eSSam Leffler 	 *	[tlv] Vendor OUI HT information (optional)
23654207227cSSam Leffler 	 *	[tlv] Atheros capabilities (optional)
236610ad9a77SSam Leffler 	 *	[tlv] TDMA parameters (optional)
2367b032f27cSSam Leffler 	 *	[tlv] application data (optional)
23688a1b9b6aSSam Leffler 	 */
23698a1b9b6aSSam Leffler 
23701b6167d2SSam Leffler 	memset(bo, 0, sizeof(*bo));
23711b6167d2SSam Leffler 
23728a1b9b6aSSam Leffler 	memset(frm, 0, 8);	/* XXX timestamp is set by hardware/driver */
23738a1b9b6aSSam Leffler 	frm += 8;
237468e8e04eSSam Leffler 	*(uint16_t *)frm = htole16(ni->ni_intval);
23758a1b9b6aSSam Leffler 	frm += 2;
2376b032f27cSSam Leffler 	capinfo = getcapinfo(vap, ni->ni_chan);
237768e8e04eSSam Leffler 	bo->bo_caps = (uint16_t *)frm;
237868e8e04eSSam Leffler 	*(uint16_t *)frm = htole16(capinfo);
23798a1b9b6aSSam Leffler 	frm += 2;
23808a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_SSID;
2381b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
23828a1b9b6aSSam Leffler 		*frm++ = ni->ni_esslen;
23838a1b9b6aSSam Leffler 		memcpy(frm, ni->ni_essid, ni->ni_esslen);
23848a1b9b6aSSam Leffler 		frm += ni->ni_esslen;
23858a1b9b6aSSam Leffler 	} else
23868a1b9b6aSSam Leffler 		*frm++ = 0;
23878a1b9b6aSSam Leffler 	frm = ieee80211_add_rates(frm, rs);
2388b032f27cSSam Leffler 	if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
23898a1b9b6aSSam Leffler 		*frm++ = IEEE80211_ELEMID_DSPARMS;
23908a1b9b6aSSam Leffler 		*frm++ = 1;
2391b032f27cSSam Leffler 		*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2392b032f27cSSam Leffler 	}
2393b032f27cSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PCF) {
2394b032f27cSSam Leffler 		bo->bo_cfp = frm;
2395b032f27cSSam Leffler 		frm = ieee80211_add_cfparms(frm, ic);
23968a1b9b6aSSam Leffler 	}
23978a1b9b6aSSam Leffler 	bo->bo_tim = frm;
2398b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
23998a1b9b6aSSam Leffler 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
24008a1b9b6aSSam Leffler 		*frm++ = 2;
24018a1b9b6aSSam Leffler 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
24028a1b9b6aSSam Leffler 		bo->bo_tim_len = 0;
2403b032f27cSSam Leffler 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
24048a1b9b6aSSam Leffler 		struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
24058a1b9b6aSSam Leffler 
24068a1b9b6aSSam Leffler 		tie->tim_ie = IEEE80211_ELEMID_TIM;
24078a1b9b6aSSam Leffler 		tie->tim_len = 4;	/* length */
24088a1b9b6aSSam Leffler 		tie->tim_count = 0;	/* DTIM count */
2409b032f27cSSam Leffler 		tie->tim_period = vap->iv_dtim_period;	/* DTIM period */
24108a1b9b6aSSam Leffler 		tie->tim_bitctl = 0;	/* bitmap control */
24118a1b9b6aSSam Leffler 		tie->tim_bitmap[0] = 0;	/* Partial Virtual Bitmap */
24128a1b9b6aSSam Leffler 		frm += sizeof(struct ieee80211_tim_ie);
24138a1b9b6aSSam Leffler 		bo->bo_tim_len = 1;
24148a1b9b6aSSam Leffler 	}
2415b105a069SSam Leffler 	bo->bo_tim_trailer = frm;
2416b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2417b032f27cSSam Leffler 	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2418b032f27cSSam Leffler 		frm = ieee80211_add_countryie(frm, ic);
2419b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_DOTH) {
2420b032f27cSSam Leffler 		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
2421b032f27cSSam Leffler 			frm = ieee80211_add_powerconstraint(frm, vap);
2422b032f27cSSam Leffler 		bo->bo_csa = frm;
2423b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2424b032f27cSSam Leffler 			frm = ieee80211_add_csa(frm, vap);
2425b032f27cSSam Leffler 	} else
2426b032f27cSSam Leffler 		bo->bo_csa = frm;
2427b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
24280912bac9SSam Leffler 		bo->bo_erp = frm;
24298a1b9b6aSSam Leffler 		frm = ieee80211_add_erp(frm, ic);
24301b6167d2SSam Leffler 	}
243168e8e04eSSam Leffler 	frm = ieee80211_add_xrates(frm, rs);
2432688fe74dSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA2) {
2433688fe74dSSam Leffler 		if (vap->iv_rsn_ie != NULL)
2434688fe74dSSam Leffler 			frm = add_ie(frm, vap->iv_rsn_ie);
2435688fe74dSSam Leffler 		/* XXX else complain */
2436688fe74dSSam Leffler 	}
2437b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
243868e8e04eSSam Leffler 		frm = ieee80211_add_htcap(frm, ni);
243968e8e04eSSam Leffler 		bo->bo_htinfo = frm;
244068e8e04eSSam Leffler 		frm = ieee80211_add_htinfo(frm, ni);
24411b6167d2SSam Leffler 	}
2442b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA1) {
2443b032f27cSSam Leffler 		if (vap->iv_wpa_ie != NULL)
2444b032f27cSSam Leffler 			frm = add_ie(frm, vap->iv_wpa_ie);
2445b032f27cSSam Leffler 		/* XXX else complain */
2446b032f27cSSam Leffler 	}
2447b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WME) {
24481b6167d2SSam Leffler 		bo->bo_wme = frm;
24491b6167d2SSam Leffler 		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
24501b6167d2SSam Leffler 	}
2451b032f27cSSam Leffler 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
24522bfc8a91SSam Leffler 	    (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
245368e8e04eSSam Leffler 		frm = ieee80211_add_htcap_vendor(frm, ni);
245468e8e04eSSam Leffler 		frm = ieee80211_add_htinfo_vendor(frm, ni);
24550912bac9SSam Leffler 	}
24564207227cSSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
24574207227cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_ATHEROS) {
24584207227cSSam Leffler 		bo->bo_ath = frm;
24594207227cSSam Leffler 		frm = ieee80211_add_athcaps(frm, ni);
24604207227cSSam Leffler 	}
24614207227cSSam Leffler #endif
246210ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
246310ad9a77SSam Leffler 	if (vap->iv_caps & IEEE80211_C_TDMA) {
246410ad9a77SSam Leffler 		bo->bo_tdma = frm;
246510ad9a77SSam Leffler 		frm = ieee80211_add_tdma(frm, vap);
246610ad9a77SSam Leffler 	}
246710ad9a77SSam Leffler #endif
2468b032f27cSSam Leffler 	if (vap->iv_appie_beacon != NULL) {
2469b032f27cSSam Leffler 		bo->bo_appie = frm;
2470b032f27cSSam Leffler 		bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
2471b032f27cSSam Leffler 		frm = add_appie(frm, vap->iv_appie_beacon);
2472b032f27cSSam Leffler 	}
2473b105a069SSam Leffler 	bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
2474b032f27cSSam Leffler 	bo->bo_csa_trailer_len = frm - bo->bo_csa;
247568e8e04eSSam Leffler 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2476b032f27cSSam Leffler }
2477b032f27cSSam Leffler 
2478b032f27cSSam Leffler /*
2479b032f27cSSam Leffler  * Allocate a beacon frame and fillin the appropriate bits.
2480b032f27cSSam Leffler  */
2481b032f27cSSam Leffler struct mbuf *
2482b032f27cSSam Leffler ieee80211_beacon_alloc(struct ieee80211_node *ni,
2483b032f27cSSam Leffler 	struct ieee80211_beacon_offsets *bo)
2484b032f27cSSam Leffler {
2485b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2486b032f27cSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2487b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ifp;
2488b032f27cSSam Leffler 	struct ieee80211_frame *wh;
2489b032f27cSSam Leffler 	struct mbuf *m;
2490b032f27cSSam Leffler 	int pktlen;
2491b032f27cSSam Leffler 	uint8_t *frm;
2492b032f27cSSam Leffler 
2493b032f27cSSam Leffler 	/*
2494b032f27cSSam Leffler 	 * beacon frame format
2495b032f27cSSam Leffler 	 *	[8] time stamp
2496b032f27cSSam Leffler 	 *	[2] beacon interval
2497b032f27cSSam Leffler 	 *	[2] cabability information
2498b032f27cSSam Leffler 	 *	[tlv] ssid
2499b032f27cSSam Leffler 	 *	[tlv] supported rates
2500b032f27cSSam Leffler 	 *	[3] parameter set (DS)
2501b032f27cSSam Leffler 	 *	[8] CF parameter set (optional)
2502b032f27cSSam Leffler 	 *	[tlv] parameter set (IBSS/TIM)
2503b032f27cSSam Leffler 	 *	[tlv] country (optional)
2504b032f27cSSam Leffler 	 *	[3] power control (optional)
2505b032f27cSSam Leffler 	 *	[5] channel switch announcement (CSA) (optional)
2506b032f27cSSam Leffler 	 *	[tlv] extended rate phy (ERP)
2507b032f27cSSam Leffler 	 *	[tlv] extended supported rates
2508b032f27cSSam Leffler 	 *	[tlv] RSN parameters
2509b032f27cSSam Leffler 	 *	[tlv] HT capabilities
2510b032f27cSSam Leffler 	 *	[tlv] HT information
2511b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT capabilities (optional)
2512b032f27cSSam Leffler 	 *	[tlv] Vendor OUI HT information (optional)
2513b032f27cSSam Leffler 	 * XXX Vendor-specific OIDs (e.g. Atheros)
2514b032f27cSSam Leffler 	 *	[tlv] WPA parameters
2515b032f27cSSam Leffler 	 *	[tlv] WME parameters
251610ad9a77SSam Leffler 	 *	[tlv] TDMA parameters (optional)
2517b032f27cSSam Leffler 	 *	[tlv] application data (optional)
2518b032f27cSSam Leffler 	 * NB: we allocate the max space required for the TIM bitmap.
2519b032f27cSSam Leffler 	 * XXX how big is this?
2520b032f27cSSam Leffler 	 */
2521b032f27cSSam Leffler 	pktlen =   8					/* time stamp */
2522b032f27cSSam Leffler 		 + sizeof(uint16_t)			/* beacon interval */
2523b032f27cSSam Leffler 		 + sizeof(uint16_t)			/* capabilities */
2524b032f27cSSam Leffler 		 + 2 + ni->ni_esslen			/* ssid */
2525b032f27cSSam Leffler 	         + 2 + IEEE80211_RATE_SIZE		/* supported rates */
2526b032f27cSSam Leffler 	         + 2 + 1				/* DS parameters */
2527b032f27cSSam Leffler 		 + 2 + 6				/* CF parameters */
2528b032f27cSSam Leffler 		 + 2 + 4 + vap->iv_tim_len		/* DTIM/IBSSPARMS */
2529b032f27cSSam Leffler 		 + IEEE80211_COUNTRY_MAX_SIZE		/* country */
2530b032f27cSSam Leffler 		 + 2 + 1				/* power control */
2531b032f27cSSam Leffler 	         + sizeof(struct ieee80211_csa_ie)	/* CSA */
2532b032f27cSSam Leffler 		 + 2 + 1				/* ERP */
2533b032f27cSSam Leffler 	         + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2534b032f27cSSam Leffler 		 + (vap->iv_caps & IEEE80211_C_WPA ?	/* WPA 1+2 */
2535b032f27cSSam Leffler 			2*sizeof(struct ieee80211_ie_wpa) : 0)
2536b032f27cSSam Leffler 		 /* XXX conditional? */
2537b032f27cSSam Leffler 		 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
2538b032f27cSSam Leffler 		 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
2539b032f27cSSam Leffler 		 + (vap->iv_caps & IEEE80211_C_WME ?	/* WME */
2540b032f27cSSam Leffler 			sizeof(struct ieee80211_wme_param) : 0)
25414207227cSSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
25424207227cSSam Leffler 		 + sizeof(struct ieee80211_ath_ie)	/* ATH */
25434207227cSSam Leffler #endif
254410ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
254510ad9a77SSam Leffler 		 + (vap->iv_caps & IEEE80211_C_TDMA ?	/* TDMA */
254610ad9a77SSam Leffler 			sizeof(struct ieee80211_tdma_param) : 0)
254710ad9a77SSam Leffler #endif
2548b032f27cSSam Leffler 		 + IEEE80211_MAX_APPIE
2549b032f27cSSam Leffler 		 ;
2550b032f27cSSam Leffler 	m = ieee80211_getmgtframe(&frm,
2551b032f27cSSam Leffler 		ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
2552b032f27cSSam Leffler 	if (m == NULL) {
2553b032f27cSSam Leffler 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
2554b032f27cSSam Leffler 			"%s: cannot get buf; size %u\n", __func__, pktlen);
2555b032f27cSSam Leffler 		vap->iv_stats.is_tx_nobuf++;
2556b032f27cSSam Leffler 		return NULL;
2557b032f27cSSam Leffler 	}
2558b032f27cSSam Leffler 	ieee80211_beacon_construct(m, frm, bo, ni);
25598a1b9b6aSSam Leffler 
25608a1b9b6aSSam Leffler 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
25618a1b9b6aSSam Leffler 	KASSERT(m != NULL, ("no space for 802.11 header?"));
25628a1b9b6aSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
25638a1b9b6aSSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
25648a1b9b6aSSam Leffler 	    IEEE80211_FC0_SUBTYPE_BEACON;
25658a1b9b6aSSam Leffler 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
256668e8e04eSSam Leffler 	*(uint16_t *)wh->i_dur = 0;
25678a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
2568b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
25698a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
257068e8e04eSSam Leffler 	*(uint16_t *)wh->i_seq = 0;
25718a1b9b6aSSam Leffler 
25728a1b9b6aSSam Leffler 	return m;
25738a1b9b6aSSam Leffler }
25748a1b9b6aSSam Leffler 
25758a1b9b6aSSam Leffler /*
25768a1b9b6aSSam Leffler  * Update the dynamic parts of a beacon frame based on the current state.
25778a1b9b6aSSam Leffler  */
25788a1b9b6aSSam Leffler int
2579b105a069SSam Leffler ieee80211_beacon_update(struct ieee80211_node *ni,
25808a1b9b6aSSam Leffler 	struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
25818a1b9b6aSSam Leffler {
2582b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2583b105a069SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
25848a1b9b6aSSam Leffler 	int len_changed = 0;
258568e8e04eSSam Leffler 	uint16_t capinfo;
25868a1b9b6aSSam Leffler 
2587b032f27cSSam Leffler 	IEEE80211_LOCK(ic);
2588b032f27cSSam Leffler 	/*
2589b032f27cSSam Leffler 	 * Handle 11h channel change when we've reached the count.
2590b032f27cSSam Leffler 	 * We must recalculate the beacon frame contents to account
2591b032f27cSSam Leffler 	 * for the new channel.  Note we do this only for the first
2592b032f27cSSam Leffler 	 * vap that reaches this point; subsequent vaps just update
2593b032f27cSSam Leffler 	 * their beacon state to reflect the recalculated channel.
2594b032f27cSSam Leffler 	 */
2595b032f27cSSam Leffler 	if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
2596b032f27cSSam Leffler 	    vap->iv_csa_count == ic->ic_csa_count) {
2597b032f27cSSam Leffler 		vap->iv_csa_count = 0;
2598b032f27cSSam Leffler 		/*
2599b032f27cSSam Leffler 		 * Effect channel change before reconstructing the beacon
2600b032f27cSSam Leffler 		 * frame contents as many places reference ni_chan.
2601b032f27cSSam Leffler 		 */
2602b032f27cSSam Leffler 		if (ic->ic_csa_newchan != NULL)
2603b032f27cSSam Leffler 			ieee80211_csa_completeswitch(ic);
2604b032f27cSSam Leffler 		/*
2605b032f27cSSam Leffler 		 * NB: ieee80211_beacon_construct clears all pending
2606b032f27cSSam Leffler 		 * updates in bo_flags so we don't need to explicitly
2607b032f27cSSam Leffler 		 * clear IEEE80211_BEACON_CSA.
2608b032f27cSSam Leffler 		 */
2609b032f27cSSam Leffler 		ieee80211_beacon_construct(m,
2610b032f27cSSam Leffler 		    mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), bo, ni);
2611b032f27cSSam Leffler 
2612b032f27cSSam Leffler 		/* XXX do WME aggressive mode processing? */
2613b032f27cSSam Leffler 		IEEE80211_UNLOCK(ic);
2614b032f27cSSam Leffler 		return 1;		/* just assume length changed */
2615b032f27cSSam Leffler 	}
2616b032f27cSSam Leffler 
26178a1b9b6aSSam Leffler 	/* XXX faster to recalculate entirely or just changes? */
2618b032f27cSSam Leffler 	capinfo = getcapinfo(vap, ni->ni_chan);
26198a1b9b6aSSam Leffler 	*bo->bo_caps = htole16(capinfo);
26208a1b9b6aSSam Leffler 
2621b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WME) {
26228a1b9b6aSSam Leffler 		struct ieee80211_wme_state *wme = &ic->ic_wme;
26238a1b9b6aSSam Leffler 
26248a1b9b6aSSam Leffler 		/*
26258a1b9b6aSSam Leffler 		 * Check for agressive mode change.  When there is
26268a1b9b6aSSam Leffler 		 * significant high priority traffic in the BSS
26278a1b9b6aSSam Leffler 		 * throttle back BE traffic by using conservative
26288a1b9b6aSSam Leffler 		 * parameters.  Otherwise BE uses agressive params
26298a1b9b6aSSam Leffler 		 * to optimize performance of legacy/non-QoS traffic.
26308a1b9b6aSSam Leffler 		 */
26318a1b9b6aSSam Leffler 		if (wme->wme_flags & WME_F_AGGRMODE) {
26328a1b9b6aSSam Leffler 			if (wme->wme_hipri_traffic >
26338a1b9b6aSSam Leffler 			    wme->wme_hipri_switch_thresh) {
2634b032f27cSSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
26358a1b9b6aSSam Leffler 				    "%s: traffic %u, disable aggressive mode\n",
26368a1b9b6aSSam Leffler 				    __func__, wme->wme_hipri_traffic);
26378a1b9b6aSSam Leffler 				wme->wme_flags &= ~WME_F_AGGRMODE;
2638b032f27cSSam Leffler 				ieee80211_wme_updateparams_locked(vap);
26398a1b9b6aSSam Leffler 				wme->wme_hipri_traffic =
26408a1b9b6aSSam Leffler 					wme->wme_hipri_switch_hysteresis;
26418a1b9b6aSSam Leffler 			} else
26428a1b9b6aSSam Leffler 				wme->wme_hipri_traffic = 0;
26438a1b9b6aSSam Leffler 		} else {
26448a1b9b6aSSam Leffler 			if (wme->wme_hipri_traffic <=
26458a1b9b6aSSam Leffler 			    wme->wme_hipri_switch_thresh) {
2646b032f27cSSam Leffler 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
26478a1b9b6aSSam Leffler 				    "%s: traffic %u, enable aggressive mode\n",
26488a1b9b6aSSam Leffler 				    __func__, wme->wme_hipri_traffic);
26498a1b9b6aSSam Leffler 				wme->wme_flags |= WME_F_AGGRMODE;
2650b032f27cSSam Leffler 				ieee80211_wme_updateparams_locked(vap);
26518a1b9b6aSSam Leffler 				wme->wme_hipri_traffic = 0;
26528a1b9b6aSSam Leffler 			} else
26538a1b9b6aSSam Leffler 				wme->wme_hipri_traffic =
26548a1b9b6aSSam Leffler 					wme->wme_hipri_switch_hysteresis;
26558a1b9b6aSSam Leffler 		}
2656b105a069SSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
26578a1b9b6aSSam Leffler 			(void) ieee80211_add_wme_param(bo->bo_wme, wme);
2658b105a069SSam Leffler 			clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
26598a1b9b6aSSam Leffler 		}
26608a1b9b6aSSam Leffler 	}
26618a1b9b6aSSam Leffler 
2662b105a069SSam Leffler 	if (isset(bo->bo_flags,  IEEE80211_BEACON_HTINFO)) {
2663b032f27cSSam Leffler 		ieee80211_ht_update_beacon(vap, bo);
2664b105a069SSam Leffler 		clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
266568e8e04eSSam Leffler 	}
266610ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
266710ad9a77SSam Leffler 	if (vap->iv_caps & IEEE80211_C_TDMA) {
266810ad9a77SSam Leffler 		/*
266910ad9a77SSam Leffler 		 * NB: the beacon is potentially updated every TBTT.
267010ad9a77SSam Leffler 		 */
267110ad9a77SSam Leffler 		ieee80211_tdma_update_beacon(vap, bo);
267210ad9a77SSam Leffler 	}
267310ad9a77SSam Leffler #endif
2674b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {	/* NB: no IBSS support*/
26758a1b9b6aSSam Leffler 		struct ieee80211_tim_ie *tie =
26768a1b9b6aSSam Leffler 			(struct ieee80211_tim_ie *) bo->bo_tim;
2677b105a069SSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
26788a1b9b6aSSam Leffler 			u_int timlen, timoff, i;
26798a1b9b6aSSam Leffler 			/*
26808a1b9b6aSSam Leffler 			 * ATIM/DTIM needs updating.  If it fits in the
26818a1b9b6aSSam Leffler 			 * current space allocated then just copy in the
26828a1b9b6aSSam Leffler 			 * new bits.  Otherwise we need to move any trailing
26838a1b9b6aSSam Leffler 			 * data to make room.  Note that we know there is
26848a1b9b6aSSam Leffler 			 * contiguous space because ieee80211_beacon_allocate
26858a1b9b6aSSam Leffler 			 * insures there is space in the mbuf to write a
2686b032f27cSSam Leffler 			 * maximal-size virtual bitmap (based on iv_max_aid).
26878a1b9b6aSSam Leffler 			 */
26888a1b9b6aSSam Leffler 			/*
26898a1b9b6aSSam Leffler 			 * Calculate the bitmap size and offset, copy any
26908a1b9b6aSSam Leffler 			 * trailer out of the way, and then copy in the
26918a1b9b6aSSam Leffler 			 * new bitmap and update the information element.
26928a1b9b6aSSam Leffler 			 * Note that the tim bitmap must contain at least
26938a1b9b6aSSam Leffler 			 * one byte and any offset must be even.
26948a1b9b6aSSam Leffler 			 */
2695b032f27cSSam Leffler 			if (vap->iv_ps_pending != 0) {
26968a1b9b6aSSam Leffler 				timoff = 128;		/* impossibly large */
2697b032f27cSSam Leffler 				for (i = 0; i < vap->iv_tim_len; i++)
2698b032f27cSSam Leffler 					if (vap->iv_tim_bitmap[i]) {
26998a1b9b6aSSam Leffler 						timoff = i &~ 1;
27008a1b9b6aSSam Leffler 						break;
27018a1b9b6aSSam Leffler 					}
27028a1b9b6aSSam Leffler 				KASSERT(timoff != 128, ("tim bitmap empty!"));
2703b032f27cSSam Leffler 				for (i = vap->iv_tim_len-1; i >= timoff; i--)
2704b032f27cSSam Leffler 					if (vap->iv_tim_bitmap[i])
27058a1b9b6aSSam Leffler 						break;
27068a1b9b6aSSam Leffler 				timlen = 1 + (i - timoff);
27078a1b9b6aSSam Leffler 			} else {
27088a1b9b6aSSam Leffler 				timoff = 0;
27098a1b9b6aSSam Leffler 				timlen = 1;
27108a1b9b6aSSam Leffler 			}
27118a1b9b6aSSam Leffler 			if (timlen != bo->bo_tim_len) {
27128a1b9b6aSSam Leffler 				/* copy up/down trailer */
27130912bac9SSam Leffler 				int adjust = tie->tim_bitmap+timlen
2714b105a069SSam Leffler 					   - bo->bo_tim_trailer;
2715b105a069SSam Leffler 				ovbcopy(bo->bo_tim_trailer,
2716b105a069SSam Leffler 				    bo->bo_tim_trailer+adjust,
2717b105a069SSam Leffler 				    bo->bo_tim_trailer_len);
2718b105a069SSam Leffler 				bo->bo_tim_trailer += adjust;
27190912bac9SSam Leffler 				bo->bo_erp += adjust;
272068e8e04eSSam Leffler 				bo->bo_htinfo += adjust;
27214207227cSSam Leffler #ifdef IEEE80211_SUPERG_SUPPORT
27224207227cSSam Leffler 				bo->bo_ath += adjust;
27234207227cSSam Leffler #endif
272492e870edSSam Leffler #ifdef IEEE80211_TDMA_SUPPORT
272592e870edSSam Leffler 				bo->bo_tdma += adjust;
272692e870edSSam Leffler #endif
2727b032f27cSSam Leffler 				bo->bo_appie += adjust;
2728b032f27cSSam Leffler 				bo->bo_wme += adjust;
2729b032f27cSSam Leffler 				bo->bo_csa += adjust;
27308a1b9b6aSSam Leffler 				bo->bo_tim_len = timlen;
27318a1b9b6aSSam Leffler 
27328a1b9b6aSSam Leffler 				/* update information element */
27338a1b9b6aSSam Leffler 				tie->tim_len = 3 + timlen;
27348a1b9b6aSSam Leffler 				tie->tim_bitctl = timoff;
27358a1b9b6aSSam Leffler 				len_changed = 1;
27368a1b9b6aSSam Leffler 			}
2737b032f27cSSam Leffler 			memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
27388a1b9b6aSSam Leffler 				bo->bo_tim_len);
27398a1b9b6aSSam Leffler 
2740b105a069SSam Leffler 			clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
27418a1b9b6aSSam Leffler 
2742b032f27cSSam Leffler 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
27438a1b9b6aSSam Leffler 				"%s: TIM updated, pending %u, off %u, len %u\n",
2744b032f27cSSam Leffler 				__func__, vap->iv_ps_pending, timoff, timlen);
27458a1b9b6aSSam Leffler 		}
27468a1b9b6aSSam Leffler 		/* count down DTIM period */
27478a1b9b6aSSam Leffler 		if (tie->tim_count == 0)
27488a1b9b6aSSam Leffler 			tie->tim_count = tie->tim_period - 1;
27498a1b9b6aSSam Leffler 		else
27508a1b9b6aSSam Leffler 			tie->tim_count--;
27518a1b9b6aSSam Leffler 		/* update state for buffered multicast frames on DTIM */
2752a196b35fSSam Leffler 		if (mcast && tie->tim_count == 0)
27538a1b9b6aSSam Leffler 			tie->tim_bitctl |= 1;
27548a1b9b6aSSam Leffler 		else
27558a1b9b6aSSam Leffler 			tie->tim_bitctl &= ~1;
2756b032f27cSSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
2757b032f27cSSam Leffler 			struct ieee80211_csa_ie *csa =
2758b032f27cSSam Leffler 			    (struct ieee80211_csa_ie *) bo->bo_csa;
2759b032f27cSSam Leffler 
2760b032f27cSSam Leffler 			/*
2761b032f27cSSam Leffler 			 * Insert or update CSA ie.  If we're just starting
2762b032f27cSSam Leffler 			 * to count down to the channel switch then we need
2763b032f27cSSam Leffler 			 * to insert the CSA ie.  Otherwise we just need to
2764b032f27cSSam Leffler 			 * drop the count.  The actual change happens above
2765b032f27cSSam Leffler 			 * when the vap's count reaches the target count.
2766b032f27cSSam Leffler 			 */
2767b032f27cSSam Leffler 			if (vap->iv_csa_count == 0) {
2768b032f27cSSam Leffler 				memmove(&csa[1], csa, bo->bo_csa_trailer_len);
2769b032f27cSSam Leffler 				bo->bo_erp += sizeof(*csa);
2770d01b3c26SSam Leffler 				bo->bo_htinfo += sizeof(*csa);
2771b032f27cSSam Leffler 				bo->bo_wme += sizeof(*csa);
27724207227cSSam Leffler #ifdef IEEE80211_SUPERG_SUPPORT
27734207227cSSam Leffler 				bo->bo_ath += sizeof(*csa);
27744207227cSSam Leffler #endif
277592e870edSSam Leffler #ifdef IEEE80211_TDMA_SUPPORT
277692e870edSSam Leffler 				bo->bo_tdma += sizeof(*csa);
277792e870edSSam Leffler #endif
2778b032f27cSSam Leffler 				bo->bo_appie += sizeof(*csa);
2779b032f27cSSam Leffler 				bo->bo_csa_trailer_len += sizeof(*csa);
2780b032f27cSSam Leffler 				bo->bo_tim_trailer_len += sizeof(*csa);
2781b032f27cSSam Leffler 				m->m_len += sizeof(*csa);
2782b032f27cSSam Leffler 				m->m_pkthdr.len += sizeof(*csa);
2783b032f27cSSam Leffler 
2784b032f27cSSam Leffler 				ieee80211_add_csa(bo->bo_csa, vap);
2785b032f27cSSam Leffler 			} else
2786b032f27cSSam Leffler 				csa->csa_count--;
2787b032f27cSSam Leffler 			vap->iv_csa_count++;
2788b032f27cSSam Leffler 			/* NB: don't clear IEEE80211_BEACON_CSA */
2789b032f27cSSam Leffler 		}
2790b105a069SSam Leffler 		if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
27910912bac9SSam Leffler 			/*
27920912bac9SSam Leffler 			 * ERP element needs updating.
27930912bac9SSam Leffler 			 */
27940912bac9SSam Leffler 			(void) ieee80211_add_erp(bo->bo_erp, ic);
2795b105a069SSam Leffler 			clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
27960912bac9SSam Leffler 		}
27974207227cSSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
27984207227cSSam Leffler 		if (isset(bo->bo_flags,  IEEE80211_BEACON_ATH)) {
27994207227cSSam Leffler 			ieee80211_add_athcaps(bo->bo_ath, ni);
28004207227cSSam Leffler 			clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
28014207227cSSam Leffler 		}
28024207227cSSam Leffler #endif
28038a1b9b6aSSam Leffler 	}
2804b032f27cSSam Leffler 	if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
2805b032f27cSSam Leffler 		const struct ieee80211_appie *aie = vap->iv_appie_beacon;
2806b032f27cSSam Leffler 		int aielen;
2807b032f27cSSam Leffler 		uint8_t *frm;
2808b032f27cSSam Leffler 
2809b032f27cSSam Leffler 		aielen = 0;
2810b032f27cSSam Leffler 		if (aie != NULL)
2811b032f27cSSam Leffler 			aielen += aie->ie_len;
2812b032f27cSSam Leffler 		if (aielen != bo->bo_appie_len) {
2813b032f27cSSam Leffler 			/* copy up/down trailer */
2814b032f27cSSam Leffler 			int adjust = aielen - bo->bo_appie_len;
2815b032f27cSSam Leffler 			ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
2816b032f27cSSam Leffler 				bo->bo_tim_trailer_len);
2817b032f27cSSam Leffler 			bo->bo_tim_trailer += adjust;
2818b032f27cSSam Leffler 			bo->bo_appie += adjust;
2819b032f27cSSam Leffler 			bo->bo_appie_len = aielen;
2820b032f27cSSam Leffler 
2821b032f27cSSam Leffler 			len_changed = 1;
2822b032f27cSSam Leffler 		}
2823b032f27cSSam Leffler 		frm = bo->bo_appie;
2824b032f27cSSam Leffler 		if (aie != NULL)
2825b032f27cSSam Leffler 			frm  = add_appie(frm, aie);
2826b032f27cSSam Leffler 		clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
2827b032f27cSSam Leffler 	}
2828b032f27cSSam Leffler 	IEEE80211_UNLOCK(ic);
28298a1b9b6aSSam Leffler 
28308a1b9b6aSSam Leffler 	return len_changed;
28318a1b9b6aSSam Leffler }
2832