1 /*-
2  * Copyright (c) 2001 Atsushi Onoe
3  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: head/sys/net80211/ieee80211_output.c 198384 2009-10-23 11:13:08Z rpaulo $
27  */
28 
29 #include "opt_inet.h"
30 #include "opt_inet6.h"
31 #include "opt_wlan.h"
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/mbuf.h>
36 #include <sys/kernel.h>
37 #include <sys/endian.h>
38 
39 #include <sys/socket.h>
40 
41 #include <net/bpf.h>
42 #include <net/ethernet.h>
43 #include <net/route.h>
44 #include <net/if.h>
45 #include <net/if_llc.h>
46 #include <net/if_media.h>
47 #include <net/ifq_var.h>
48 
49 #include <netproto/802_11/ieee80211_var.h>
50 #include <netproto/802_11/ieee80211_regdomain.h>
51 #ifdef IEEE80211_SUPPORT_SUPERG
52 #include <netproto/802_11/ieee80211_superg.h>
53 #endif
54 #ifdef IEEE80211_SUPPORT_TDMA
55 #include <netproto/802_11/ieee80211_tdma.h>
56 #endif
57 #include <netproto/802_11/ieee80211_wds.h>
58 #include <netproto/802_11/ieee80211_mesh.h>
59 
60 #ifdef INET
61 #include <netinet/in.h>
62 #include <netinet/if_ether.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #endif
66 #ifdef INET6
67 #include <netinet/ip6.h>
68 #endif
69 
70 #define	ETHER_HEADER_COPY(dst, src) \
71 	memcpy(dst, src, sizeof(struct ether_header))
72 
73 /* unalligned little endian access */
74 #define LE_WRITE_2(p, v) do {				\
75 	((uint8_t *)(p))[0] = (v) & 0xff;		\
76 	((uint8_t *)(p))[1] = ((v) >> 8) & 0xff;	\
77 } while (0)
78 #define LE_WRITE_4(p, v) do {				\
79 	((uint8_t *)(p))[0] = (v) & 0xff;		\
80 	((uint8_t *)(p))[1] = ((v) >> 8) & 0xff;	\
81 	((uint8_t *)(p))[2] = ((v) >> 16) & 0xff;	\
82 	((uint8_t *)(p))[3] = ((v) >> 24) & 0xff;	\
83 } while (0)
84 
85 static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
86 	u_int hdrsize, u_int ciphdrsize, u_int mtu);
87 static	void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
88 
89 #ifdef IEEE80211_DEBUG
90 /*
91  * Decide if an outbound management frame should be
92  * printed when debugging is enabled.  This filters some
93  * of the less interesting frames that come frequently
94  * (e.g. beacons).
95  */
96 static __inline int
97 doprint(struct ieee80211vap *vap, int subtype)
98 {
99 	switch (subtype) {
100 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
101 		return (vap->iv_opmode == IEEE80211_M_IBSS);
102 	}
103 	return 1;
104 }
105 #endif
106 
107 /*
108  * Start method for vap's.  All packets from the stack come
109  * through here.  We handle common processing of the packets
110  * before dispatching them to the underlying device.
111  */
112 void
113 ieee80211_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
114 {
115 #define	IS_DWDS(vap) \
116 	(vap->iv_opmode == IEEE80211_M_WDS && \
117 	 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
118 	struct ieee80211vap *vap = ifp->if_softc;
119 	struct ieee80211com *ic = vap->iv_ic;
120 	struct ifnet *parent = ic->ic_ifp;
121 	struct ieee80211_node *ni;
122 	struct mbuf *m = NULL;
123 	struct ether_header *eh;
124 	int error;
125 
126 	wlan_assert_serialized();
127 	ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
128 
129 	/* NB: parent must be up and running */
130 	if (!IFNET_IS_UP_RUNNING(parent)) {
131 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
132 		    "%s: ignore queue, parent %s not up+running\n",
133 		    __func__, parent->if_xname);
134 		/* XXX stat */
135 		ifsq_purge(ifsq);
136 		return;
137 	}
138 	/*
139 	 * No data frames go out unless we're running.
140 	 * Note in particular this covers CAC and CSA
141 	 * states (though maybe we should check muting
142 	 * for CSA).
143 	 */
144 	if (vap->iv_state != IEEE80211_S_RUN &&
145 	    vap->iv_state != IEEE80211_S_SLEEP) {
146 		/* re-check under the com lock to avoid races */
147 		if (vap->iv_state != IEEE80211_S_RUN &&
148 		    vap->iv_state != IEEE80211_S_SLEEP) {
149 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
150 			    "%s: ignore queue, in %s state\n",
151 			    __func__, ieee80211_state_name[vap->iv_state]);
152 			vap->iv_stats.is_tx_badstate++;
153 			ifsq_set_oactive(ifsq);
154 			return;
155 		}
156 	}
157 	for (;;) {
158 		m = ifsq_dequeue(ifsq);
159 		if (m == NULL)
160 			break;
161 		/*
162 		 * Sanitize mbuf flags for net80211 use.  We cannot
163 		 * clear M_PWR_SAV or M_MORE_DATA because these may
164 		 * be set for frames that are re-submitted from the
165 		 * power save queue.
166 		 *
167 		 * NB: This must be done before ieee80211_classify as
168 		 *     it marks EAPOL in frames with M_EAPOL.
169 		 */
170 		m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
171 		/*
172 		 * Cancel any background scan.
173 		 */
174 		if (ic->ic_flags & IEEE80211_F_SCAN)
175 			ieee80211_cancel_anyscan(vap);
176 		/*
177 		 * Find the node for the destination so we can do
178 		 * things like power save and fast frames aggregation.
179 		 *
180 		 * NB: past this point various code assumes the first
181 		 *     mbuf has the 802.3 header present (and contiguous).
182 		 */
183 		ni = NULL;
184 		if (m->m_len < sizeof(struct ether_header) &&
185 		   (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
186 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
187 			    "discard frame, %s\n", "m_pullup failed");
188 			vap->iv_stats.is_tx_nobuf++;	/* XXX */
189 			IFNET_STAT_INC(ifp, oerrors, 1);
190 			continue;
191 		}
192 		eh = mtod(m, struct ether_header *);
193 		if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
194 			if (IS_DWDS(vap)) {
195 				/*
196 				 * Only unicast frames from the above go out
197 				 * DWDS vaps; multicast frames are handled by
198 				 * dispatching the frame as it comes through
199 				 * the AP vap (see below).
200 				 */
201 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
202 				    eh->ether_dhost, "mcast", "%s", "on DWDS");
203 				vap->iv_stats.is_dwds_mcast++;
204 				m_freem(m);
205 				continue;
206 			}
207 			if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
208 				/*
209 				 * Spam DWDS vap's w/ multicast traffic.
210 				 */
211 				/* XXX only if dwds in use? */
212 				ieee80211_dwds_mcast(vap, m);
213 			}
214 		}
215 #ifdef IEEE80211_SUPPORT_MESH
216 		if (vap->iv_opmode != IEEE80211_M_MBSS) {
217 #endif
218 			ni = ieee80211_find_txnode(vap, eh->ether_dhost);
219 			if (ni == NULL) {
220 				/* NB: ieee80211_find_txnode does stat+msg */
221 				IFNET_STAT_INC(ifp, oerrors, 1);
222 				m_freem(m);
223 				continue;
224 			}
225 			if (ni->ni_associd == 0 &&
226 			    (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
227 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
228 				    eh->ether_dhost, NULL,
229 				    "sta not associated (type 0x%04x)",
230 				    htons(eh->ether_type));
231 				vap->iv_stats.is_tx_notassoc++;
232 				IFNET_STAT_INC(ifp, oerrors, 1);
233 				m_freem(m);
234 				ieee80211_free_node(ni);
235 				continue;
236 			}
237 #ifdef IEEE80211_SUPPORT_MESH
238 		} else {
239 			if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) {
240 				/*
241 				 * Proxy station only if configured.
242 				 */
243 				if (!ieee80211_mesh_isproxyena(vap)) {
244 					IEEE80211_DISCARD_MAC(vap,
245 					    IEEE80211_MSG_OUTPUT |
246 						IEEE80211_MSG_MESH,
247 					    eh->ether_dhost, NULL,
248 					    "%s", "proxy not enabled");
249 					vap->iv_stats.is_mesh_notproxy++;
250 					IFNET_STAT_INC(ifp, oerrors, 1);
251 					m_freem(m);
252 					continue;
253 				}
254 				ieee80211_mesh_proxy_check(vap, eh->ether_shost);
255 			}
256 			ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m);
257 			if (ni == NULL) {
258 				/*
259 				 * NB: ieee80211_mesh_discover holds/disposes
260 				 * frame (e.g. queueing on path discovery).
261 				 */
262 				IFNET_STAT_INC(ifp, oerrors, 1);
263 				continue;
264 			}
265 		}
266 #endif
267 		/*
268 		 * We've resolved the sender, so attempt to transmit it.
269 		 */
270 		if (vap->iv_state == IEEE80211_S_SLEEP) {
271 			/*
272 			 * In power save; queue frame and then  wakeup device
273 			 * for transmit.
274 			 */
275 			ic->ic_lastdata = ticks;
276 			(void) ieee80211_pwrsave(ni, m);
277 			ieee80211_free_node(ni);
278 			ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
279 			continue;
280 		}
281 
282 		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
283 		    (m->m_flags & M_PWR_SAV) == 0) {
284 			/*
285 			 * Station in power save mode; pass the frame
286 			 * to the 802.11 layer and continue.  We'll get
287 			 * the frame back when the time is right.
288 			 * XXX lose WDS vap linkage?
289 			 */
290 			(void) ieee80211_pwrsave(ni, m);
291 			ieee80211_free_node(ni);
292 			continue;
293 		}
294 		/* calculate priority so drivers can find the tx queue */
295 		if (ieee80211_classify(ni, m)) {
296 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
297 			    eh->ether_dhost, NULL,
298 			    "%s", "classification failure");
299 			vap->iv_stats.is_tx_classify++;
300 			IFNET_STAT_INC(ifp, oerrors, 1);
301 			m_freem(m);
302 			ieee80211_free_node(ni);
303 			continue;
304 		}
305 		/*
306 		 * Stash the node pointer.  Note that we do this after
307 		 * any call to ieee80211_dwds_mcast because that code
308 		 * uses any existing value for rcvif to identify the
309 		 * interface it (might have been) received on.
310 		 */
311 		m->m_pkthdr.rcvif = (void *)ni;
312 
313 		BPF_MTAP(ifp, m);		/* 802.3 tx */
314 
315 		/*
316 		 * Check if A-MPDU tx aggregation is setup or if we
317 		 * should try to enable it.  The sta must be associated
318 		 * with HT and A-MPDU enabled for use.  When the policy
319 		 * routine decides we should enable A-MPDU we issue an
320 		 * ADDBA request and wait for a reply.  The frame being
321 		 * encapsulated will go out w/o using A-MPDU, or possibly
322 		 * it might be collected by the driver and held/retransmit.
323 		 * The default ic_ampdu_enable routine handles staggering
324 		 * ADDBA requests in case the receiver NAK's us or we are
325 		 * otherwise unable to establish a BA stream.
326 		 */
327 		if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
328 		    (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX) &&
329 		    (m->m_flags & M_EAPOL) == 0) {
330 			const int ac = M_WME_GETAC(m);
331 			struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[ac];
332 
333 			ieee80211_txampdu_count_packet(tap);
334 			if (IEEE80211_AMPDU_RUNNING(tap)) {
335 				/*
336 				 * Operational, mark frame for aggregation.
337 				 *
338 				 * XXX do tx aggregation here
339 				 */
340 				m->m_flags |= M_AMPDU_MPDU;
341 			} else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
342 			    ic->ic_ampdu_enable(ni, tap)) {
343 				/*
344 				 * Not negotiated yet, request service.
345 				 */
346 				ieee80211_ampdu_request(ni, tap);
347 				/* XXX hold frame for reply? */
348 			}
349 		}
350 #ifdef IEEE80211_SUPPORT_SUPERG
351 		else if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF)) {
352 			m = ieee80211_ff_check(ni, m);
353 			if (m == NULL) {
354 				/* NB: any ni ref held on stageq */
355 				continue;
356 			}
357 		}
358 #endif /* IEEE80211_SUPPORT_SUPERG */
359 		if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
360 			/*
361 			 * Encapsulate the packet in prep for transmission.
362 			 */
363 			m = ieee80211_encap(vap, ni, m);
364 			if (m == NULL) {
365 				/* NB: stat+msg handled in ieee80211_encap */
366 				ieee80211_free_node(ni);
367 				continue;
368 			}
369 		}
370 
371 		error = ieee80211_handoff(parent, m);
372 		if (error != 0) {
373 			/* NB: IFQ_HANDOFF reclaims mbuf */
374 			ieee80211_free_node(ni);
375 		} else {
376 			IFNET_STAT_INC(ifp, opackets, 1);
377 		}
378 		ic->ic_lastdata = ticks;
379 	}
380 #undef IS_DWDS
381 }
382 
383 
384 /*
385  * 802.11 output routine. This is (currently) used only to
386  * connect bpf write calls to the 802.11 layer for injecting
387  * raw 802.11 frames.
388  */
389 int
390 ieee80211_output(struct ifnet *ifp, struct mbuf *m,
391 	struct sockaddr *dst, struct rtentry *rt)
392 {
393 #define senderr(e) do { error = (e); goto bad;} while (0)
394 	struct ieee80211_node *ni = NULL;
395 	struct ieee80211vap *vap;
396 	struct ieee80211_frame *wh;
397 	struct ifaltq_subque *ifsq;
398 	int error;
399 
400 	ifsq = ifq_get_subq_default(&ifp->if_snd);
401 	if (ifsq_is_oactive(ifsq)) {
402 		/*
403 		 * Short-circuit requests if the vap is marked OACTIVE
404 		 * as this can happen because a packet came down through
405 		 * ieee80211_start before the vap entered RUN state in
406 		 * which case it's ok to just drop the frame.  This
407 		 * should not be necessary but callers of if_output don't
408 		 * check OACTIVE.
409 		 */
410 		senderr(ENETDOWN);
411 	}
412 	vap = ifp->if_softc;
413 	/*
414 	 * Hand to the 802.3 code if not tagged as
415 	 * a raw 802.11 frame.
416 	 */
417 	if (dst->sa_family != AF_IEEE80211)
418 		return vap->iv_output(ifp, m, dst, rt);
419 #ifdef MAC
420 	error = mac_ifnet_check_transmit(ifp, m);
421 	if (error)
422 		senderr(error);
423 #endif
424 	if (ifp->if_flags & IFF_MONITOR)
425 		senderr(ENETDOWN);
426 	if (!IFNET_IS_UP_RUNNING(ifp))
427 		senderr(ENETDOWN);
428 	if (vap->iv_state == IEEE80211_S_CAC) {
429 		IEEE80211_DPRINTF(vap,
430 		    IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
431 		    "block %s frame in CAC state\n", "raw data");
432 		vap->iv_stats.is_tx_badstate++;
433 		senderr(EIO);		/* XXX */
434 	}
435 	/* XXX bypass bridge, pfil, carp, etc. */
436 
437 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
438 		senderr(EIO);	/* XXX */
439 	wh = mtod(m, struct ieee80211_frame *);
440 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
441 	    IEEE80211_FC0_VERSION_0)
442 		senderr(EIO);	/* XXX */
443 
444 	/* locate destination node */
445 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
446 	case IEEE80211_FC1_DIR_NODS:
447 	case IEEE80211_FC1_DIR_FROMDS:
448 		ni = ieee80211_find_txnode(vap, wh->i_addr1);
449 		break;
450 	case IEEE80211_FC1_DIR_TODS:
451 	case IEEE80211_FC1_DIR_DSTODS:
452 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame))
453 			senderr(EIO);	/* XXX */
454 		ni = ieee80211_find_txnode(vap, wh->i_addr3);
455 		break;
456 	default:
457 		senderr(EIO);	/* XXX */
458 	}
459 	if (ni == NULL) {
460 		/*
461 		 * Permit packets w/ bpf params through regardless
462 		 * (see below about sa_len).
463 		 */
464 		if (dst->sa_len == 0)
465 			senderr(EHOSTUNREACH);
466 		ni = ieee80211_ref_node(vap->iv_bss);
467 	}
468 
469 	/*
470 	 * Sanitize mbuf for net80211 flags leaked from above.
471 	 *
472 	 * NB: This must be done before ieee80211_classify as
473 	 *     it marks EAPOL in frames with M_EAPOL.
474 	 */
475 	m->m_flags &= ~M_80211_TX;
476 
477 	/* calculate priority so drivers can find the tx queue */
478 	/* XXX assumes an 802.3 frame */
479 	if (ieee80211_classify(ni, m))
480 		senderr(EIO);		/* XXX */
481 
482 	IFNET_STAT_INC(ifp, opackets, 1);
483 	IEEE80211_NODE_STAT(ni, tx_data);
484 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
485 		IEEE80211_NODE_STAT(ni, tx_mcast);
486 		m->m_flags |= M_MCAST;
487 	} else
488 		IEEE80211_NODE_STAT(ni, tx_ucast);
489 	/* NB: ieee80211_encap does not include 802.11 header */
490 	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, m->m_pkthdr.len);
491 
492 	/*
493 	 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
494 	 * present by setting the sa_len field of the sockaddr (yes,
495 	 * this is a hack).
496 	 * NB: we assume sa_data is suitably aligned to cast.
497 	 */
498 	return vap->iv_ic->ic_raw_xmit(ni, m,
499 	    (const struct ieee80211_bpf_params *)(dst->sa_len ?
500 		dst->sa_data : NULL));
501 bad:
502 	if (m != NULL)
503 		m_freem(m);
504 	if (ni != NULL)
505 		ieee80211_free_node(ni);
506 	IFNET_STAT_INC(ifp, oerrors, 1);
507 	return error;
508 #undef senderr
509 }
510 
511 /*
512  * Set the direction field and address fields of an outgoing
513  * frame.  Note this should be called early on in constructing
514  * a frame as it sets i_fc[1]; other bits can then be or'd in.
515  */
516 void
517 ieee80211_send_setup(
518 	struct ieee80211_node *ni,
519 	struct mbuf *m,
520 	int type, int tid,
521 	const uint8_t sa[IEEE80211_ADDR_LEN],
522 	const uint8_t da[IEEE80211_ADDR_LEN],
523 	const uint8_t bssid[IEEE80211_ADDR_LEN])
524 {
525 #define	WH4(wh)	((struct ieee80211_frame_addr4 *)wh)
526 	struct ieee80211vap *vap = ni->ni_vap;
527 	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
528 	ieee80211_seq seqno;
529 
530 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
531 	if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
532 		switch (vap->iv_opmode) {
533 		case IEEE80211_M_STA:
534 			wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
535 			IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
536 			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
537 			IEEE80211_ADDR_COPY(wh->i_addr3, da);
538 			break;
539 		case IEEE80211_M_IBSS:
540 		case IEEE80211_M_AHDEMO:
541 			wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
542 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
543 			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
544 			IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
545 			break;
546 		case IEEE80211_M_HOSTAP:
547 			wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
548 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
549 			IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
550 			IEEE80211_ADDR_COPY(wh->i_addr3, sa);
551 			break;
552 		case IEEE80211_M_WDS:
553 			wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
554 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
555 			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
556 			IEEE80211_ADDR_COPY(wh->i_addr3, da);
557 			IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
558 			break;
559 		case IEEE80211_M_MBSS:
560 #ifdef IEEE80211_SUPPORT_MESH
561 			/* XXX add support for proxied addresses */
562 			if (IEEE80211_IS_MULTICAST(da)) {
563 				wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
564 				/* XXX next hop */
565 				IEEE80211_ADDR_COPY(wh->i_addr1, da);
566 				IEEE80211_ADDR_COPY(wh->i_addr2,
567 				    vap->iv_myaddr);
568 			} else {
569 				wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
570 				IEEE80211_ADDR_COPY(wh->i_addr1, da);
571 				IEEE80211_ADDR_COPY(wh->i_addr2,
572 				    vap->iv_myaddr);
573 				IEEE80211_ADDR_COPY(wh->i_addr3, da);
574 				IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
575 			}
576 #endif
577 			break;
578 		case IEEE80211_M_MONITOR:	/* NB: to quiet compiler */
579 			break;
580 		}
581 	} else {
582 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
583 		IEEE80211_ADDR_COPY(wh->i_addr1, da);
584 		IEEE80211_ADDR_COPY(wh->i_addr2, sa);
585 #ifdef IEEE80211_SUPPORT_MESH
586 		if (vap->iv_opmode == IEEE80211_M_MBSS)
587 			IEEE80211_ADDR_COPY(wh->i_addr3, sa);
588 		else
589 #endif
590 			IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
591 	}
592 	*(uint16_t *)&wh->i_dur[0] = 0;
593 
594 	seqno = ni->ni_txseqs[tid]++;
595 	*(uint16_t *)&wh->i_seq[0] = htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
596 	M_SEQNO_SET(m, seqno);
597 
598 	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
599 		m->m_flags |= M_MCAST;
600 #undef WH4
601 }
602 
603 /*
604  * Send a management frame to the specified node.  The node pointer
605  * must have a reference as the pointer will be passed to the driver
606  * and potentially held for a long time.  If the frame is successfully
607  * dispatched to the driver, then it is responsible for freeing the
608  * reference (and potentially free'ing up any associated storage);
609  * otherwise deal with reclaiming any reference (on error).
610  */
611 int
612 ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
613 	struct ieee80211_bpf_params *params)
614 {
615 	struct ieee80211vap *vap = ni->ni_vap;
616 	struct ieee80211com *ic = ni->ni_ic;
617 	struct ieee80211_frame *wh;
618 #ifdef IEEE80211_DEBUG
619 	char ethstr[ETHER_ADDRSTRLEN + 1];
620 #endif
621 	KASSERT(ni != NULL, ("null node"));
622 
623 	if (vap->iv_state == IEEE80211_S_CAC) {
624 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
625 		    ni, "block %s frame in CAC state",
626 			ieee80211_mgt_subtype_name[
627 			    (type & IEEE80211_FC0_SUBTYPE_MASK) >>
628 				IEEE80211_FC0_SUBTYPE_SHIFT]);
629 		vap->iv_stats.is_tx_badstate++;
630 		ieee80211_free_node(ni);
631 		m_freem(m);
632 		return EIO;		/* XXX */
633 	}
634 
635 	M_PREPEND(m, sizeof(struct ieee80211_frame), MB_DONTWAIT);
636 	if (m == NULL) {
637 		ieee80211_free_node(ni);
638 		return ENOMEM;
639 	}
640 
641 	wh = mtod(m, struct ieee80211_frame *);
642 	ieee80211_send_setup(ni, m,
643 	     IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
644 	     vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
645 	if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
646 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
647 		    "encrypting frame (%s)", __func__);
648 		wh->i_fc[1] |= IEEE80211_FC1_WEP;
649 	}
650 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
651 
652 	KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
653 	M_WME_SETAC(m, params->ibp_pri);
654 
655 #ifdef IEEE80211_DEBUG
656 	/* avoid printing too many frames */
657 	if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
658 	    ieee80211_msg_dumppkts(vap)) {
659 		kprintf("[%s] send %s on channel %u\n",
660 		    kether_ntoa(wh->i_addr1, ethstr),
661 		    ieee80211_mgt_subtype_name[
662 			(type & IEEE80211_FC0_SUBTYPE_MASK) >>
663 				IEEE80211_FC0_SUBTYPE_SHIFT],
664 		    ieee80211_chan2ieee(ic, ic->ic_curchan));
665 	}
666 #endif
667 	IEEE80211_NODE_STAT(ni, tx_mgmt);
668 
669 	return ic->ic_raw_xmit(ni, m, params);
670 }
671 
672 /*
673  * Send a null data frame to the specified node.  If the station
674  * is setup for QoS then a QoS Null Data frame is constructed.
675  * If this is a WDS station then a 4-address frame is constructed.
676  *
677  * NB: the caller is assumed to have setup a node reference
678  *     for use; this is necessary to deal with a race condition
679  *     when probing for inactive stations.  Like ieee80211_mgmt_output
680  *     we must cleanup any node reference on error;  however we
681  *     can safely just unref it as we know it will never be the
682  *     last reference to the node.
683  */
684 int
685 ieee80211_send_nulldata(struct ieee80211_node *ni)
686 {
687 	struct ieee80211vap *vap = ni->ni_vap;
688 	struct ieee80211com *ic = ni->ni_ic;
689 	struct mbuf *m;
690 	struct ieee80211_frame *wh;
691 	int hdrlen;
692 	uint8_t *frm;
693 
694 	if (vap->iv_state == IEEE80211_S_CAC) {
695 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
696 		    ni, "block %s frame in CAC state", "null data");
697 		ieee80211_unref_node(&ni);
698 		vap->iv_stats.is_tx_badstate++;
699 		return EIO;		/* XXX */
700 	}
701 
702 	if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
703 		hdrlen = sizeof(struct ieee80211_qosframe);
704 	else
705 		hdrlen = sizeof(struct ieee80211_frame);
706 	/* NB: only WDS vap's get 4-address frames */
707 	if (vap->iv_opmode == IEEE80211_M_WDS)
708 		hdrlen += IEEE80211_ADDR_LEN;
709 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
710 		hdrlen = roundup(hdrlen, sizeof(uint32_t));
711 
712 	m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
713 	if (m == NULL) {
714 		/* XXX debug msg */
715 		ieee80211_unref_node(&ni);
716 		vap->iv_stats.is_tx_nobuf++;
717 		return ENOMEM;
718 	}
719 	KASSERT(M_LEADINGSPACE(m) >= hdrlen,
720 	    ("leading space %zd", M_LEADINGSPACE(m)));
721 	M_PREPEND(m, hdrlen, MB_DONTWAIT);
722 	if (m == NULL) {
723 		/* NB: cannot happen */
724 		ieee80211_free_node(ni);
725 		return ENOMEM;
726 	}
727 
728 	wh = mtod(m, struct ieee80211_frame *);		/* NB: a little lie */
729 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
730 		const int tid = WME_AC_TO_TID(WME_AC_BE);
731 		uint8_t *qos;
732 
733 		ieee80211_send_setup(ni, m,
734 		    IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
735 		    tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
736 
737 		if (vap->iv_opmode == IEEE80211_M_WDS)
738 			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
739 		else
740 			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
741 		qos[0] = tid & IEEE80211_QOS_TID;
742 		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
743 			qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
744 		qos[1] = 0;
745 	} else {
746 		ieee80211_send_setup(ni, m,
747 		    IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
748 		    IEEE80211_NONQOS_TID,
749 		    vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
750 	}
751 	if (vap->iv_opmode != IEEE80211_M_WDS) {
752 		/* NB: power management bit is never sent by an AP */
753 		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
754 		    vap->iv_opmode != IEEE80211_M_HOSTAP)
755 			wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
756 	}
757 	m->m_len = m->m_pkthdr.len = hdrlen;
758 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
759 
760 	M_WME_SETAC(m, WME_AC_BE);
761 
762 	IEEE80211_NODE_STAT(ni, tx_data);
763 
764 	IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
765 	    "send %snull data frame on channel %u, pwr mgt %s",
766 	    ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
767 	    ieee80211_chan2ieee(ic, ic->ic_curchan),
768 	    wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
769 
770 	return ic->ic_raw_xmit(ni, m, NULL);
771 }
772 
773 /*
774  * Assign priority to a frame based on any vlan tag assigned
775  * to the station and/or any Diffserv setting in an IP header.
776  * Finally, if an ACM policy is setup (in station mode) it's
777  * applied.
778  */
779 int
780 ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
781 {
782 	const struct ether_header *eh = mtod(m, struct ether_header *);
783 	int v_wme_ac, d_wme_ac, ac;
784 
785 	/*
786 	 * Always promote PAE/EAPOL frames to high priority.
787 	 */
788 	if (eh->ether_type == htons(ETHERTYPE_PAE)) {
789 		/* NB: mark so others don't need to check header */
790 		m->m_flags |= M_EAPOL;
791 		ac = WME_AC_VO;
792 		goto done;
793 	}
794 	/*
795 	 * Non-qos traffic goes to BE.
796 	 */
797 	if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
798 		ac = WME_AC_BE;
799 		goto done;
800 	}
801 
802 	/*
803 	 * If node has a vlan tag then all traffic
804 	 * to it must have a matching tag.
805 	 */
806 	v_wme_ac = 0;
807 	if (ni->ni_vlan != 0) {
808 		 if ((m->m_flags & M_VLANTAG) == 0) {
809 			IEEE80211_NODE_STAT(ni, tx_novlantag);
810 			return 1;
811 		}
812 #ifdef __FreeBSD__
813 		if (EVL_VLANOFTAG(m->m_pkthdr.ether_vlantag) !=
814 		    EVL_VLANOFTAG(ni->ni_vlan)) {
815 			IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
816 			return 1;
817 		}
818 		/* map vlan priority to AC */
819 		v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
820 #endif
821 	}
822 
823 	/* XXX m_copydata may be too slow for fast path */
824 #ifdef INET
825 	if (eh->ether_type == htons(ETHERTYPE_IP)) {
826 		uint8_t tos;
827 		/*
828 		 * IP frame, map the DSCP bits from the TOS field.
829 		 */
830 		/* NB: ip header may not be in first mbuf */
831 		m_copydata(m, sizeof(struct ether_header) +
832 		    offsetof(struct ip, ip_tos), sizeof(tos), &tos);
833 		tos >>= 5;		/* NB: ECN + low 3 bits of DSCP */
834 		d_wme_ac = TID_TO_WME_AC(tos);
835 	} else {
836 #endif /* INET */
837 #ifdef INET6
838 	if (eh->ether_type == htons(ETHERTYPE_IPV6)) {
839 		uint32_t flow;
840 		uint8_t tos;
841 		/*
842 		 * IPv6 frame, map the DSCP bits from the TOS field.
843 		 */
844 		m_copydata(m, sizeof(struct ether_header) +
845 		    offsetof(struct ip6_hdr, ip6_flow), sizeof(flow),
846 		    (caddr_t) &flow);
847 		tos = (uint8_t)(ntohl(flow) >> 20);
848 		tos >>= 5;		/* NB: ECN + low 3 bits of DSCP */
849 		d_wme_ac = TID_TO_WME_AC(tos);
850 	} else {
851 #endif /* INET6 */
852 		d_wme_ac = WME_AC_BE;
853 #ifdef INET6
854 	}
855 #endif
856 #ifdef INET
857 	}
858 #endif
859 	/*
860 	 * Use highest priority AC.
861 	 */
862 	if (v_wme_ac > d_wme_ac)
863 		ac = v_wme_ac;
864 	else
865 		ac = d_wme_ac;
866 
867 	/*
868 	 * Apply ACM policy.
869 	 */
870 	if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
871 		static const int acmap[4] = {
872 			WME_AC_BK,	/* WME_AC_BE */
873 			WME_AC_BK,	/* WME_AC_BK */
874 			WME_AC_BE,	/* WME_AC_VI */
875 			WME_AC_VI,	/* WME_AC_VO */
876 		};
877 		struct ieee80211com *ic = ni->ni_ic;
878 
879 		while (ac != WME_AC_BK &&
880 		    ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
881 			ac = acmap[ac];
882 	}
883 done:
884 	M_WME_SETAC(m, ac);
885 	return 0;
886 }
887 
888 /*
889  * Insure there is sufficient contiguous space to encapsulate the
890  * 802.11 data frame.  If room isn't already there, arrange for it.
891  * Drivers and cipher modules assume we have done the necessary work
892  * and fail rudely if they don't find the space they need.
893  */
894 struct mbuf *
895 ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
896 	struct ieee80211_key *key, struct mbuf *m)
897 {
898 #define	TO_BE_RECLAIMED	(sizeof(struct ether_header) - sizeof(struct llc))
899 	struct mbuf *mnew = NULL;
900 	int needed_space = vap->iv_ic->ic_headroom + hdrsize;
901 
902 	if (key != NULL) {
903 		/* XXX belongs in crypto code? */
904 		needed_space += key->wk_cipher->ic_header;
905 		/* XXX frags */
906 		/*
907 		 * When crypto is being done in the host we must insure
908 		 * the data are writable for the cipher routines; clone
909 		 * a writable mbuf chain.
910 		 * XXX handle SWMIC specially
911 		 */
912 		if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
913 			mnew = m_dup(m, MB_DONTWAIT);
914 			if (m == NULL) {
915 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
916 				    "%s: cannot get writable mbuf\n", __func__);
917 				vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
918 				return NULL;
919 			}
920 			m_freem(m);
921 			m = mnew;
922 		}
923 	}
924 	/*
925 	 * We know we are called just before stripping an Ethernet
926 	 * header and prepending an LLC header.  This means we know
927 	 * there will be
928 	 *	sizeof(struct ether_header) - sizeof(struct llc)
929 	 * bytes recovered to which we need additional space for the
930 	 * 802.11 header and any crypto header.
931 	 */
932 	/* XXX check trailing space and copy instead? */
933 	if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
934 		struct mbuf *n = m_gethdr(MB_DONTWAIT, m->m_type);
935 		if (n == NULL) {
936 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
937 			    "%s: cannot expand storage\n", __func__);
938 			vap->iv_stats.is_tx_nobuf++;
939 			m_freem(m);
940 			return NULL;
941 		}
942 		KASSERT(needed_space <= MHLEN,
943 		    ("not enough room, need %u got %zu", needed_space, MHLEN));
944 		/*
945 		 * Setup new mbuf to have leading space to prepend the
946 		 * 802.11 header and any crypto header bits that are
947 		 * required (the latter are added when the driver calls
948 		 * back to ieee80211_crypto_encap to do crypto encapsulation).
949 		 */
950 		/* NB: must be first 'cuz it clobbers m_data */
951 		m_move_pkthdr(n, m);
952 		n->m_len = 0;			/* NB: m_gethdr does not set */
953 		n->m_data += needed_space;
954 		/*
955 		 * Pull up Ethernet header to create the expected layout.
956 		 * We could use m_pullup but that's overkill (i.e. we don't
957 		 * need the actual data) and it cannot fail so do it inline
958 		 * for speed.
959 		 */
960 		/* NB: struct ether_header is known to be contiguous */
961 		n->m_len += sizeof(struct ether_header);
962 		m->m_len -= sizeof(struct ether_header);
963 		m->m_data += sizeof(struct ether_header);
964 		/*
965 		 * Replace the head of the chain.
966 		 */
967 		n->m_next = m;
968 		m = n;
969 	}
970 	return m;
971 #undef TO_BE_RECLAIMED
972 }
973 
974 /*
975  * Return the transmit key to use in sending a unicast frame.
976  * If a unicast key is set we use that.  When no unicast key is set
977  * we fall back to the default transmit key.
978  */
979 static __inline struct ieee80211_key *
980 ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
981 	struct ieee80211_node *ni)
982 {
983 	if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
984 		if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
985 		    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
986 			return NULL;
987 		return &vap->iv_nw_keys[vap->iv_def_txkey];
988 	} else {
989 		return &ni->ni_ucastkey;
990 	}
991 }
992 
993 /*
994  * Return the transmit key to use in sending a multicast frame.
995  * Multicast traffic always uses the group key which is installed as
996  * the default tx key.
997  */
998 static __inline struct ieee80211_key *
999 ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
1000 	struct ieee80211_node *ni)
1001 {
1002 	if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1003 	    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1004 		return NULL;
1005 	return &vap->iv_nw_keys[vap->iv_def_txkey];
1006 }
1007 
1008 /*
1009  * Encapsulate an outbound data frame.  The mbuf chain is updated.
1010  * If an error is encountered NULL is returned.  The caller is required
1011  * to provide a node reference and pullup the ethernet header in the
1012  * first mbuf.
1013  *
1014  * NB: Packet is assumed to be processed by ieee80211_classify which
1015  *     marked EAPOL frames w/ M_EAPOL.
1016  */
1017 struct mbuf *
1018 ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
1019     struct mbuf *m)
1020 {
1021 #define	WH4(wh)	((struct ieee80211_frame_addr4 *)(wh))
1022 	struct ieee80211com *ic = ni->ni_ic;
1023 #ifdef IEEE80211_SUPPORT_MESH
1024 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1025 	struct ieee80211_meshcntl_ae10 *mc;
1026 #endif
1027 	struct ether_header eh;
1028 	struct ieee80211_frame *wh;
1029 	struct ieee80211_key *key;
1030 	struct llc *llc;
1031 	int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr;
1032 	ieee80211_seq seqno;
1033 	int meshhdrsize, meshae;
1034 	uint8_t *qos;
1035 
1036 	/*
1037 	 * Copy existing Ethernet header to a safe place.  The
1038 	 * rest of the code assumes it's ok to strip it when
1039 	 * reorganizing state for the final encapsulation.
1040 	 */
1041 	KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
1042 	ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
1043 
1044 	/*
1045 	 * Insure space for additional headers.  First identify
1046 	 * transmit key to use in calculating any buffer adjustments
1047 	 * required.  This is also used below to do privacy
1048 	 * encapsulation work.  Then calculate the 802.11 header
1049 	 * size and any padding required by the driver.
1050 	 *
1051 	 * Note key may be NULL if we fall back to the default
1052 	 * transmit key and that is not set.  In that case the
1053 	 * buffer may not be expanded as needed by the cipher
1054 	 * routines, but they will/should discard it.
1055 	 */
1056 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1057 		if (vap->iv_opmode == IEEE80211_M_STA ||
1058 		    !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
1059 		    (vap->iv_opmode == IEEE80211_M_WDS &&
1060 		     (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
1061 			key = ieee80211_crypto_getucastkey(vap, ni);
1062 		else
1063 			key = ieee80211_crypto_getmcastkey(vap, ni);
1064 		if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
1065 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
1066 			    eh.ether_dhost,
1067 			    "no default transmit key (%s) deftxkey %u",
1068 			    __func__, vap->iv_def_txkey);
1069 			vap->iv_stats.is_tx_nodefkey++;
1070 			goto bad;
1071 		}
1072 	} else
1073 		key = NULL;
1074 	/*
1075 	 * XXX Some ap's don't handle QoS-encapsulated EAPOL
1076 	 * frames so suppress use.  This may be an issue if other
1077 	 * ap's require all data frames to be QoS-encapsulated
1078 	 * once negotiated in which case we'll need to make this
1079 	 * configurable.
1080 	 */
1081 	addqos = (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) &&
1082 		 (m->m_flags & M_EAPOL) == 0;
1083 	if (addqos)
1084 		hdrsize = sizeof(struct ieee80211_qosframe);
1085 	else
1086 		hdrsize = sizeof(struct ieee80211_frame);
1087 #ifdef IEEE80211_SUPPORT_MESH
1088 	if (vap->iv_opmode == IEEE80211_M_MBSS) {
1089 		/*
1090 		 * Mesh data frames are encapsulated according to the
1091 		 * rules of Section 11B.8.5 (p.139 of D3.0 spec).
1092 		 * o Group Addressed data (aka multicast) originating
1093 		 *   at the local sta are sent w/ 3-address format and
1094 		 *   address extension mode 00
1095 		 * o Individually Addressed data (aka unicast) originating
1096 		 *   at the local sta are sent w/ 4-address format and
1097 		 *   address extension mode 00
1098 		 * o Group Addressed data forwarded from a non-mesh sta are
1099 		 *   sent w/ 3-address format and address extension mode 01
1100 		 * o Individually Address data from another sta are sent
1101 		 *   w/ 4-address format and address extension mode 10
1102 		 */
1103 		is4addr = 0;		/* NB: don't use, disable */
1104 		if (!IEEE80211_IS_MULTICAST(eh.ether_dhost))
1105 			hdrsize += IEEE80211_ADDR_LEN;	/* unicast are 4-addr */
1106 		meshhdrsize = sizeof(struct ieee80211_meshcntl);
1107 		/* XXX defines for AE modes */
1108 		if (IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) {
1109 			if (!IEEE80211_IS_MULTICAST(eh.ether_dhost))
1110 				meshae = 0;
1111 			else
1112 				meshae = 4;		/* NB: pseudo */
1113 		} else if (IEEE80211_IS_MULTICAST(eh.ether_dhost)) {
1114 			meshae = 1;
1115 			meshhdrsize += 1*IEEE80211_ADDR_LEN;
1116 		} else {
1117 			meshae = 2;
1118 			meshhdrsize += 2*IEEE80211_ADDR_LEN;
1119 		}
1120 	} else {
1121 #endif
1122 		/*
1123 		 * 4-address frames need to be generated for:
1124 		 * o packets sent through a WDS vap (IEEE80211_M_WDS)
1125 		 * o packets sent through a vap marked for relaying
1126 		 *   (e.g. a station operating with dynamic WDS)
1127 		 */
1128 		is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
1129 		    ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
1130 		     !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
1131 		if (is4addr)
1132 			hdrsize += IEEE80211_ADDR_LEN;
1133 		meshhdrsize = meshae = 0;
1134 #ifdef IEEE80211_SUPPORT_MESH
1135 	}
1136 #endif
1137 	/*
1138 	 * Honor driver DATAPAD requirement.
1139 	 */
1140 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
1141 		hdrspace = roundup(hdrsize, sizeof(uint32_t));
1142 	else
1143 		hdrspace = hdrsize;
1144 
1145 	if (__predict_true((m->m_flags & M_FF) == 0)) {
1146 		/*
1147 		 * Normal frame.
1148 		 */
1149 		m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m);
1150 		if (m == NULL) {
1151 			/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
1152 			goto bad;
1153 		}
1154 		/* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
1155 		m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
1156 		llc = mtod(m, struct llc *);
1157 		llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1158 		llc->llc_control = LLC_UI;
1159 		llc->llc_snap.org_code[0] = 0;
1160 		llc->llc_snap.org_code[1] = 0;
1161 		llc->llc_snap.org_code[2] = 0;
1162 		llc->llc_snap.ether_type = eh.ether_type;
1163 	} else {
1164 #ifdef IEEE80211_SUPPORT_SUPERG
1165 		/*
1166 		 * Aggregated frame.
1167 		 */
1168 		m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key);
1169 		if (m == NULL)
1170 #endif
1171 			goto bad;
1172 	}
1173 	datalen = m->m_pkthdr.len;		/* NB: w/o 802.11 header */
1174 
1175 	M_PREPEND(m, hdrspace + meshhdrsize, MB_DONTWAIT);
1176 	if (m == NULL) {
1177 		vap->iv_stats.is_tx_nobuf++;
1178 		goto bad;
1179 	}
1180 	wh = mtod(m, struct ieee80211_frame *);
1181 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1182 	*(uint16_t *)wh->i_dur = 0;
1183 	qos = NULL;	/* NB: quiet compiler */
1184 	if (is4addr) {
1185 		wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1186 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1187 		IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1188 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1189 		IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1190 	} else switch (vap->iv_opmode) {
1191 	case IEEE80211_M_STA:
1192 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1193 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
1194 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1195 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1196 		break;
1197 	case IEEE80211_M_IBSS:
1198 	case IEEE80211_M_AHDEMO:
1199 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1200 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1201 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1202 		/*
1203 		 * NB: always use the bssid from iv_bss as the
1204 		 *     neighbor's may be stale after an ibss merge
1205 		 */
1206 		IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
1207 		break;
1208 	case IEEE80211_M_HOSTAP:
1209 		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1210 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1211 		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
1212 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1213 		break;
1214 #ifdef IEEE80211_SUPPORT_MESH
1215 	case IEEE80211_M_MBSS:
1216 		/* NB: offset by hdrspace to deal with DATAPAD */
1217 		mc = (struct ieee80211_meshcntl_ae10 *)
1218 		     (mtod(m, uint8_t *) + hdrspace);
1219 		switch (meshae) {
1220 		case 0:			/* ucast, no proxy */
1221 			wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1222 			IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1223 			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1224 			IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1225 			IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1226 			mc->mc_flags = 0;
1227 			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1228 			break;
1229 		case 4:			/* mcast, no proxy */
1230 			wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1231 			IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1232 			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1233 			IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1234 			mc->mc_flags = 0;		/* NB: AE is really 0 */
1235 			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1236 			break;
1237 		case 1:			/* mcast, proxy */
1238 			wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1239 			IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1240 			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1241 			IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr);
1242 			mc->mc_flags = 1;
1243 			IEEE80211_ADDR_COPY(mc->mc_addr4, eh.ether_shost);
1244 			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1245 			break;
1246 		case 2:			/* ucast, proxy */
1247 			wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1248 			IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1249 			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1250 			/* XXX not right, need MeshDA */
1251 			IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1252 			/* XXX assume are MeshSA */
1253 			IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr);
1254 			mc->mc_flags = 2;
1255 			IEEE80211_ADDR_COPY(mc->mc_addr4, eh.ether_dhost);
1256 			IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_shost);
1257 			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1258 			break;
1259 		default:
1260 			KASSERT(0, ("meshae %d", meshae));
1261 			break;
1262 		}
1263 		mc->mc_ttl = ms->ms_ttl;
1264 		ms->ms_seq++;
1265 		LE_WRITE_4(mc->mc_seq, ms->ms_seq);
1266 		break;
1267 #endif
1268 	case IEEE80211_M_WDS:		/* NB: is4addr should always be true */
1269 	default:
1270 		goto bad;
1271 	}
1272 	if (m->m_flags & M_MORE_DATA)
1273 		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
1274 	if (addqos) {
1275 		int ac, tid;
1276 
1277 		if (is4addr) {
1278 			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1279 		/* NB: mesh case handled earlier */
1280 		} else if (vap->iv_opmode != IEEE80211_M_MBSS)
1281 			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1282 		ac = M_WME_GETAC(m);
1283 		/* map from access class/queue to 11e header priorty value */
1284 		tid = WME_AC_TO_TID(ac);
1285 		qos[0] = tid & IEEE80211_QOS_TID;
1286 		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
1287 			qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1288 		qos[1] = 0;
1289 		wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
1290 
1291 		if ((m->m_flags & M_AMPDU_MPDU) == 0) {
1292 			/*
1293 			 * NB: don't assign a sequence # to potential
1294 			 * aggregates; we expect this happens at the
1295 			 * point the frame comes off any aggregation q
1296 			 * as otherwise we may introduce holes in the
1297 			 * BA sequence space and/or make window accouting
1298 			 * more difficult.
1299 			 *
1300 			 * XXX may want to control this with a driver
1301 			 * capability; this may also change when we pull
1302 			 * aggregation up into net80211
1303 			 */
1304 			seqno = ni->ni_txseqs[tid]++;
1305 			*(uint16_t *)wh->i_seq =
1306 			    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1307 			M_SEQNO_SET(m, seqno);
1308 		}
1309 	} else {
1310 		seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
1311 		*(uint16_t *)wh->i_seq =
1312 		    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1313 		M_SEQNO_SET(m, seqno);
1314 	}
1315 
1316 
1317 	/* check if xmit fragmentation is required */
1318 	txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
1319 	    !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1320 	    (vap->iv_caps & IEEE80211_C_TXFRAG) &&
1321 	    (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
1322 	if (key != NULL) {
1323 		/*
1324 		 * IEEE 802.1X: send EAPOL frames always in the clear.
1325 		 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
1326 		 */
1327 		if ((m->m_flags & M_EAPOL) == 0 ||
1328 		    ((vap->iv_flags & IEEE80211_F_WPA) &&
1329 		     (vap->iv_opmode == IEEE80211_M_STA ?
1330 		      !IEEE80211_KEY_UNDEFINED(key) :
1331 		      !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
1332 			wh->i_fc[1] |= IEEE80211_FC1_WEP;
1333 			if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1334 				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1335 				    eh.ether_dhost,
1336 				    "%s", "enmic failed, discard frame");
1337 				vap->iv_stats.is_crypto_enmicfail++;
1338 				goto bad;
1339 			}
1340 		}
1341 	}
1342 	if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1343 	    key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
1344 		goto bad;
1345 
1346 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
1347 
1348 	IEEE80211_NODE_STAT(ni, tx_data);
1349 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1350 		IEEE80211_NODE_STAT(ni, tx_mcast);
1351 		m->m_flags |= M_MCAST;
1352 	} else
1353 		IEEE80211_NODE_STAT(ni, tx_ucast);
1354 	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
1355 
1356 	return m;
1357 bad:
1358 	if (m != NULL)
1359 		m_freem(m);
1360 	return NULL;
1361 #undef WH4
1362 }
1363 
1364 /*
1365  * Fragment the frame according to the specified mtu.
1366  * The size of the 802.11 header (w/o padding) is provided
1367  * so we don't need to recalculate it.  We create a new
1368  * mbuf for each fragment and chain it through m_nextpkt;
1369  * we might be able to optimize this by reusing the original
1370  * packet's mbufs but that is significantly more complicated.
1371  */
1372 static int
1373 ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
1374 	u_int hdrsize, u_int ciphdrsize, u_int mtu)
1375 {
1376 	struct ieee80211_frame *wh, *whf;
1377 	struct mbuf *m, *prev, *next;
1378 	u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
1379 
1380 	KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
1381 	KASSERT(m0->m_pkthdr.len > mtu,
1382 		("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
1383 
1384 	wh = mtod(m0, struct ieee80211_frame *);
1385 	/* NB: mark the first frag; it will be propagated below */
1386 	wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
1387 	totalhdrsize = hdrsize + ciphdrsize;
1388 	fragno = 1;
1389 	off = mtu - ciphdrsize;
1390 	remainder = m0->m_pkthdr.len - off;
1391 	prev = m0;
1392 	do {
1393 		fragsize = totalhdrsize + remainder;
1394 		if (fragsize > mtu)
1395 			fragsize = mtu;
1396 		/* XXX fragsize can be >2048! */
1397 		KASSERT(fragsize < MCLBYTES,
1398 			("fragment size %u too big!", fragsize));
1399 		if (fragsize > MHLEN)
1400 			m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1401 		else
1402 			m = m_gethdr(MB_DONTWAIT, MT_DATA);
1403 		if (m == NULL)
1404 			goto bad;
1405 		/* leave room to prepend any cipher header */
1406 		m_align(m, fragsize - ciphdrsize);
1407 
1408 		/*
1409 		 * Form the header in the fragment.  Note that since
1410 		 * we mark the first fragment with the MORE_FRAG bit
1411 		 * it automatically is propagated to each fragment; we
1412 		 * need only clear it on the last fragment (done below).
1413 		 */
1414 		whf = mtod(m, struct ieee80211_frame *);
1415 		memcpy(whf, wh, hdrsize);
1416 		*(uint16_t *)&whf->i_seq[0] |= htole16(
1417 			(fragno & IEEE80211_SEQ_FRAG_MASK) <<
1418 				IEEE80211_SEQ_FRAG_SHIFT);
1419 		fragno++;
1420 
1421 		payload = fragsize - totalhdrsize;
1422 		/* NB: destination is known to be contiguous */
1423 		m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrsize);
1424 		m->m_len = hdrsize + payload;
1425 		m->m_pkthdr.len = hdrsize + payload;
1426 		m->m_flags |= M_FRAG;
1427 		m->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
1428 
1429 		/* chain up the fragment */
1430 		prev->m_nextpkt = m;
1431 		prev = m;
1432 
1433 		/* deduct fragment just formed */
1434 		remainder -= payload;
1435 		off += payload;
1436 	} while (remainder != 0);
1437 
1438 	/* set the last fragment */
1439 	m->m_flags |= M_LASTFRAG;
1440 	whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
1441 
1442 	/* strip first mbuf now that everything has been copied */
1443 	m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
1444 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
1445 
1446 	vap->iv_stats.is_tx_fragframes++;
1447 	vap->iv_stats.is_tx_frags += fragno-1;
1448 
1449 	return 1;
1450 bad:
1451 	/* reclaim fragments but leave original frame for caller to free */
1452 	for (m = m0->m_nextpkt; m != NULL; m = next) {
1453 		next = m->m_nextpkt;
1454 		m->m_nextpkt = NULL;		/* XXX paranoid */
1455 		m_freem(m);
1456 	}
1457 	m0->m_nextpkt = NULL;
1458 	return 0;
1459 }
1460 
1461 /*
1462  * Add a supported rates element id to a frame.
1463  */
1464 uint8_t *
1465 ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
1466 {
1467 	int nrates;
1468 
1469 	*frm++ = IEEE80211_ELEMID_RATES;
1470 	nrates = rs->rs_nrates;
1471 	if (nrates > IEEE80211_RATE_SIZE)
1472 		nrates = IEEE80211_RATE_SIZE;
1473 	*frm++ = nrates;
1474 	memcpy(frm, rs->rs_rates, nrates);
1475 	return frm + nrates;
1476 }
1477 
1478 /*
1479  * Add an extended supported rates element id to a frame.
1480  */
1481 uint8_t *
1482 ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
1483 {
1484 	/*
1485 	 * Add an extended supported rates element if operating in 11g mode.
1486 	 */
1487 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
1488 		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
1489 		*frm++ = IEEE80211_ELEMID_XRATES;
1490 		*frm++ = nrates;
1491 		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
1492 		frm += nrates;
1493 	}
1494 	return frm;
1495 }
1496 
1497 /*
1498  * Add an ssid element to a frame.
1499  */
1500 static uint8_t *
1501 ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
1502 {
1503 	*frm++ = IEEE80211_ELEMID_SSID;
1504 	*frm++ = len;
1505 	memcpy(frm, ssid, len);
1506 	return frm + len;
1507 }
1508 
1509 /*
1510  * Add an erp element to a frame.
1511  */
1512 static uint8_t *
1513 ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic)
1514 {
1515 	uint8_t erp;
1516 
1517 	*frm++ = IEEE80211_ELEMID_ERP;
1518 	*frm++ = 1;
1519 	erp = 0;
1520 	if (ic->ic_nonerpsta != 0)
1521 		erp |= IEEE80211_ERP_NON_ERP_PRESENT;
1522 	if (ic->ic_flags & IEEE80211_F_USEPROT)
1523 		erp |= IEEE80211_ERP_USE_PROTECTION;
1524 	if (ic->ic_flags & IEEE80211_F_USEBARKER)
1525 		erp |= IEEE80211_ERP_LONG_PREAMBLE;
1526 	*frm++ = erp;
1527 	return frm;
1528 }
1529 
1530 /*
1531  * Add a CFParams element to a frame.
1532  */
1533 static uint8_t *
1534 ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
1535 {
1536 #define	ADDSHORT(frm, v) do {	\
1537 	LE_WRITE_2(frm, v);	\
1538 	frm += 2;		\
1539 } while (0)
1540 	*frm++ = IEEE80211_ELEMID_CFPARMS;
1541 	*frm++ = 6;
1542 	*frm++ = 0;		/* CFP count */
1543 	*frm++ = 2;		/* CFP period */
1544 	ADDSHORT(frm, 0);	/* CFP MaxDuration (TU) */
1545 	ADDSHORT(frm, 0);	/* CFP CurRemaining (TU) */
1546 	return frm;
1547 #undef ADDSHORT
1548 }
1549 
1550 static __inline uint8_t *
1551 add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
1552 {
1553 	memcpy(frm, ie->ie_data, ie->ie_len);
1554 	return frm + ie->ie_len;
1555 }
1556 
1557 static __inline uint8_t *
1558 add_ie(uint8_t *frm, const uint8_t *ie)
1559 {
1560 	memcpy(frm, ie, 2 + ie[1]);
1561 	return frm + 2 + ie[1];
1562 }
1563 
1564 #define	WME_OUI_BYTES		0x00, 0x50, 0xf2
1565 /*
1566  * Add a WME information element to a frame.
1567  */
1568 static uint8_t *
1569 ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme)
1570 {
1571 	static const struct ieee80211_wme_info info = {
1572 		.wme_id		= IEEE80211_ELEMID_VENDOR,
1573 		.wme_len	= sizeof(struct ieee80211_wme_info) - 2,
1574 		.wme_oui	= { WME_OUI_BYTES },
1575 		.wme_type	= WME_OUI_TYPE,
1576 		.wme_subtype	= WME_INFO_OUI_SUBTYPE,
1577 		.wme_version	= WME_VERSION,
1578 		.wme_info	= 0,
1579 	};
1580 	memcpy(frm, &info, sizeof(info));
1581 	return frm + sizeof(info);
1582 }
1583 
1584 /*
1585  * Add a WME parameters element to a frame.
1586  */
1587 static uint8_t *
1588 ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme)
1589 {
1590 #define	SM(_v, _f)	(((_v) << _f##_S) & _f)
1591 #define	ADDSHORT(frm, v) do {	\
1592 	LE_WRITE_2(frm, v);	\
1593 	frm += 2;		\
1594 } while (0)
1595 	/* NB: this works 'cuz a param has an info at the front */
1596 	static const struct ieee80211_wme_info param = {
1597 		.wme_id		= IEEE80211_ELEMID_VENDOR,
1598 		.wme_len	= sizeof(struct ieee80211_wme_param) - 2,
1599 		.wme_oui	= { WME_OUI_BYTES },
1600 		.wme_type	= WME_OUI_TYPE,
1601 		.wme_subtype	= WME_PARAM_OUI_SUBTYPE,
1602 		.wme_version	= WME_VERSION,
1603 	};
1604 	int i;
1605 
1606 	memcpy(frm, &param, sizeof(param));
1607 	frm += __offsetof(struct ieee80211_wme_info, wme_info);
1608 	*frm++ = wme->wme_bssChanParams.cap_info;	/* AC info */
1609 	*frm++ = 0;					/* reserved field */
1610 	for (i = 0; i < WME_NUM_AC; i++) {
1611 		const struct wmeParams *ac =
1612 		       &wme->wme_bssChanParams.cap_wmeParams[i];
1613 		*frm++ = SM(i, WME_PARAM_ACI)
1614 		       | SM(ac->wmep_acm, WME_PARAM_ACM)
1615 		       | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
1616 		       ;
1617 		*frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
1618 		       | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
1619 		       ;
1620 		ADDSHORT(frm, ac->wmep_txopLimit);
1621 	}
1622 	return frm;
1623 #undef SM
1624 #undef ADDSHORT
1625 }
1626 #undef WME_OUI_BYTES
1627 
1628 /*
1629  * Add an 11h Power Constraint element to a frame.
1630  */
1631 static uint8_t *
1632 ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
1633 {
1634 	const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
1635 	/* XXX per-vap tx power limit? */
1636 	int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
1637 
1638 	frm[0] = IEEE80211_ELEMID_PWRCNSTR;
1639 	frm[1] = 1;
1640 	frm[2] = c->ic_maxregpower > limit ?  c->ic_maxregpower - limit : 0;
1641 	return frm + 3;
1642 }
1643 
1644 /*
1645  * Add an 11h Power Capability element to a frame.
1646  */
1647 static uint8_t *
1648 ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
1649 {
1650 	frm[0] = IEEE80211_ELEMID_PWRCAP;
1651 	frm[1] = 2;
1652 	frm[2] = c->ic_minpower;
1653 	frm[3] = c->ic_maxpower;
1654 	return frm + 4;
1655 }
1656 
1657 /*
1658  * Add an 11h Supported Channels element to a frame.
1659  */
1660 static uint8_t *
1661 ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
1662 {
1663 	static const int ielen = 26;
1664 
1665 	frm[0] = IEEE80211_ELEMID_SUPPCHAN;
1666 	frm[1] = ielen;
1667 	/* XXX not correct */
1668 	memcpy(frm+2, ic->ic_chan_avail, ielen);
1669 	return frm + 2 + ielen;
1670 }
1671 
1672 /*
1673  * Add an 11h Channel Switch Announcement element to a frame.
1674  * Note that we use the per-vap CSA count to adjust the global
1675  * counter so we can use this routine to form probe response
1676  * frames and get the current count.
1677  */
1678 static uint8_t *
1679 ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
1680 {
1681 	struct ieee80211com *ic = vap->iv_ic;
1682 	struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
1683 
1684 	csa->csa_ie = IEEE80211_ELEMID_CSA;
1685 	csa->csa_len = 3;
1686 	csa->csa_mode = 1;		/* XXX force quiet on channel */
1687 	csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
1688 	csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
1689 	return frm + sizeof(*csa);
1690 }
1691 
1692 /*
1693  * Add an 11h country information element to a frame.
1694  */
1695 static uint8_t *
1696 ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
1697 {
1698 
1699 	if (ic->ic_countryie == NULL ||
1700 	    ic->ic_countryie_chan != ic->ic_bsschan) {
1701 		/*
1702 		 * Handle lazy construction of ie.  This is done on
1703 		 * first use and after a channel change that requires
1704 		 * re-calculation.
1705 		 */
1706 		if (ic->ic_countryie != NULL)
1707 			kfree(ic->ic_countryie, M_80211_NODE_IE);
1708 		ic->ic_countryie = ieee80211_alloc_countryie(ic);
1709 		if (ic->ic_countryie == NULL)
1710 			return frm;
1711 		ic->ic_countryie_chan = ic->ic_bsschan;
1712 	}
1713 	return add_appie(frm, ic->ic_countryie);
1714 }
1715 
1716 /*
1717  * Send a probe request frame with the specified ssid
1718  * and any optional information element data.
1719  */
1720 int
1721 ieee80211_send_probereq(struct ieee80211_node *ni,
1722 	const uint8_t sa[IEEE80211_ADDR_LEN],
1723 	const uint8_t da[IEEE80211_ADDR_LEN],
1724 	const uint8_t bssid[IEEE80211_ADDR_LEN],
1725 	const uint8_t *ssid, size_t ssidlen)
1726 {
1727 	struct ieee80211vap *vap = ni->ni_vap;
1728 	struct ieee80211com *ic = ni->ni_ic;
1729 	const struct ieee80211_txparam *tp;
1730 	struct ieee80211_bpf_params params;
1731 	const struct ieee80211_rateset *rs;
1732 	struct mbuf *m;
1733 	uint8_t *frm;
1734 #ifdef IEEE80211_DEBUG
1735 	char ethstr[ETHER_ADDRSTRLEN + 1];
1736 #endif
1737 
1738 	if (vap->iv_state == IEEE80211_S_CAC) {
1739 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
1740 		    "block %s frame in CAC state", "probe request");
1741 		vap->iv_stats.is_tx_badstate++;
1742 		return EIO;		/* XXX */
1743 	}
1744 
1745 	/*
1746 	 * Hold a reference on the node so it doesn't go away until after
1747 	 * the xmit is complete all the way in the driver.  On error we
1748 	 * will remove our reference.
1749 	 */
1750 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1751 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1752 		__func__, __LINE__,
1753 		ni, kether_ntoa(ni->ni_macaddr, ethstr),
1754 		ieee80211_node_refcnt(ni)+1);
1755 	ieee80211_ref_node(ni);
1756 
1757 	/*
1758 	 * prreq frame format
1759 	 *	[tlv] ssid
1760 	 *	[tlv] supported rates
1761 	 *	[tlv] RSN (optional)
1762 	 *	[tlv] extended supported rates
1763 	 *	[tlv] WPA (optional)
1764 	 *	[tlv] user-specified ie's
1765 	 */
1766 	m = ieee80211_getmgtframe(&frm,
1767 		 ic->ic_headroom + sizeof(struct ieee80211_frame),
1768 	       	 2 + IEEE80211_NWID_LEN
1769 	       + 2 + IEEE80211_RATE_SIZE
1770 	       + sizeof(struct ieee80211_ie_wpa)
1771 	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1772 	       + sizeof(struct ieee80211_ie_wpa)
1773 	       + (vap->iv_appie_probereq != NULL ?
1774 		   vap->iv_appie_probereq->ie_len : 0)
1775 	);
1776 	if (m == NULL) {
1777 		vap->iv_stats.is_tx_nobuf++;
1778 		ieee80211_free_node(ni);
1779 		return ENOMEM;
1780 	}
1781 
1782 	frm = ieee80211_add_ssid(frm, ssid, ssidlen);
1783 	rs = ieee80211_get_suprates(ic, ic->ic_curchan);
1784 	frm = ieee80211_add_rates(frm, rs);
1785 	if (vap->iv_flags & IEEE80211_F_WPA2) {
1786 		if (vap->iv_rsn_ie != NULL)
1787 			frm = add_ie(frm, vap->iv_rsn_ie);
1788 		/* XXX else complain? */
1789 	}
1790 	frm = ieee80211_add_xrates(frm, rs);
1791 	if (vap->iv_flags & IEEE80211_F_WPA1) {
1792 		if (vap->iv_wpa_ie != NULL)
1793 			frm = add_ie(frm, vap->iv_wpa_ie);
1794 		/* XXX else complain? */
1795 	}
1796 	if (vap->iv_appie_probereq != NULL)
1797 		frm = add_appie(frm, vap->iv_appie_probereq);
1798 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1799 
1800 	KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
1801 	    ("leading space %zd", M_LEADINGSPACE(m)));
1802 	M_PREPEND(m, sizeof(struct ieee80211_frame), MB_DONTWAIT);
1803 	if (m == NULL) {
1804 		/* NB: cannot happen */
1805 		ieee80211_free_node(ni);
1806 		return ENOMEM;
1807 	}
1808 
1809 	ieee80211_send_setup(ni, m,
1810 	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
1811 	     IEEE80211_NONQOS_TID, sa, da, bssid);
1812 	/* XXX power management? */
1813 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
1814 
1815 	M_WME_SETAC(m, WME_AC_BE);
1816 
1817 	IEEE80211_NODE_STAT(ni, tx_probereq);
1818 	IEEE80211_NODE_STAT(ni, tx_mgmt);
1819 
1820 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1821 	    "send probe req on channel %u bssid %s ssid \"%.*s\"\n",
1822 	    ieee80211_chan2ieee(ic, ic->ic_curchan), kether_ntoa(bssid, ethstr),
1823 	    (int)ssidlen, ssid);
1824 
1825 	memset(&params, 0, sizeof(params));
1826 	params.ibp_pri = M_WME_GETAC(m);
1827 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1828 	params.ibp_rate0 = tp->mgmtrate;
1829 	if (IEEE80211_IS_MULTICAST(da)) {
1830 		params.ibp_flags |= IEEE80211_BPF_NOACK;
1831 		params.ibp_try0 = 1;
1832 	} else
1833 		params.ibp_try0 = tp->maxretry;
1834 	params.ibp_power = ni->ni_txpower;
1835 	return ic->ic_raw_xmit(ni, m, &params);
1836 }
1837 
1838 /*
1839  * Calculate capability information for mgt frames.
1840  */
1841 uint16_t
1842 ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
1843 {
1844 	struct ieee80211com *ic = vap->iv_ic;
1845 	uint16_t capinfo;
1846 
1847 	KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
1848 
1849 	if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1850 		capinfo = IEEE80211_CAPINFO_ESS;
1851 	else if (vap->iv_opmode == IEEE80211_M_IBSS)
1852 		capinfo = IEEE80211_CAPINFO_IBSS;
1853 	else
1854 		capinfo = 0;
1855 	if (vap->iv_flags & IEEE80211_F_PRIVACY)
1856 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
1857 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1858 	    IEEE80211_IS_CHAN_2GHZ(chan))
1859 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1860 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
1861 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1862 	if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
1863 		capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
1864 	return capinfo;
1865 }
1866 
1867 /*
1868  * Send a management frame.  The node is for the destination (or ic_bss
1869  * when in station mode).  Nodes other than ic_bss have their reference
1870  * count bumped to reflect our use for an indeterminant time.
1871  */
1872 int
1873 ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
1874 {
1875 #define	HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
1876 #define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
1877 	struct ieee80211vap *vap = ni->ni_vap;
1878 	struct ieee80211com *ic = ni->ni_ic;
1879 	struct ieee80211_node *bss = vap->iv_bss;
1880 	struct ieee80211_bpf_params params;
1881 	struct mbuf *m;
1882 	uint8_t *frm;
1883 	uint16_t capinfo;
1884 	int has_challenge, is_shared_key, ret, status;
1885 #ifdef IEEE80211_DEBUG
1886 	char ethstr[ETHER_ADDRSTRLEN + 1];
1887 #endif
1888 
1889 	KASSERT(ni != NULL, ("null node"));
1890 
1891 	/*
1892 	 * Hold a reference on the node so it doesn't go away until after
1893 	 * the xmit is complete all the way in the driver.  On error we
1894 	 * will remove our reference.
1895 	 */
1896 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1897 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1898 		__func__, __LINE__,
1899 		ni, kether_ntoa(ni->ni_macaddr, ethstr),
1900 		ieee80211_node_refcnt(ni)+1);
1901 	ieee80211_ref_node(ni);
1902 
1903 	memset(&params, 0, sizeof(params));
1904 	switch (type) {
1905 
1906 	case IEEE80211_FC0_SUBTYPE_AUTH:
1907 		status = arg >> 16;
1908 		arg &= 0xffff;
1909 		has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
1910 		    arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
1911 		    ni->ni_challenge != NULL);
1912 
1913 		/*
1914 		 * Deduce whether we're doing open authentication or
1915 		 * shared key authentication.  We do the latter if
1916 		 * we're in the middle of a shared key authentication
1917 		 * handshake or if we're initiating an authentication
1918 		 * request and configured to use shared key.
1919 		 */
1920 		is_shared_key = has_challenge ||
1921 		     arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
1922 		     (arg == IEEE80211_AUTH_SHARED_REQUEST &&
1923 		      bss->ni_authmode == IEEE80211_AUTH_SHARED);
1924 
1925 		m = ieee80211_getmgtframe(&frm,
1926 			  ic->ic_headroom + sizeof(struct ieee80211_frame),
1927 			  3 * sizeof(uint16_t)
1928 			+ (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
1929 				sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)
1930 		);
1931 		if (m == NULL)
1932 			senderr(ENOMEM, is_tx_nobuf);
1933 
1934 		((uint16_t *)frm)[0] =
1935 		    (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
1936 		                    : htole16(IEEE80211_AUTH_ALG_OPEN);
1937 		((uint16_t *)frm)[1] = htole16(arg);	/* sequence number */
1938 		((uint16_t *)frm)[2] = htole16(status);/* status */
1939 
1940 		if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
1941 			((uint16_t *)frm)[3] =
1942 			    htole16((IEEE80211_CHALLENGE_LEN << 8) |
1943 			    IEEE80211_ELEMID_CHALLENGE);
1944 			memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
1945 			    IEEE80211_CHALLENGE_LEN);
1946 			m->m_pkthdr.len = m->m_len =
1947 				4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
1948 			if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
1949 				IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1950 				    "request encrypt frame (%s)", __func__);
1951 				/* mark frame for encryption */
1952 				params.ibp_flags |= IEEE80211_BPF_CRYPTO;
1953 			}
1954 		} else
1955 			m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
1956 
1957 		/* XXX not right for shared key */
1958 		if (status == IEEE80211_STATUS_SUCCESS)
1959 			IEEE80211_NODE_STAT(ni, tx_auth);
1960 		else
1961 			IEEE80211_NODE_STAT(ni, tx_auth_fail);
1962 
1963 		if (vap->iv_opmode == IEEE80211_M_STA)
1964 			ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
1965 				(void *) vap->iv_state);
1966 		break;
1967 
1968 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
1969 		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1970 		    "send station deauthenticate (reason %d)", arg);
1971 		m = ieee80211_getmgtframe(&frm,
1972 			ic->ic_headroom + sizeof(struct ieee80211_frame),
1973 			sizeof(uint16_t));
1974 		if (m == NULL)
1975 			senderr(ENOMEM, is_tx_nobuf);
1976 		*(uint16_t *)frm = htole16(arg);	/* reason */
1977 		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
1978 
1979 		IEEE80211_NODE_STAT(ni, tx_deauth);
1980 		IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
1981 
1982 		ieee80211_node_unauthorize(ni);		/* port closed */
1983 		break;
1984 
1985 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1986 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1987 		/*
1988 		 * asreq frame format
1989 		 *	[2] capability information
1990 		 *	[2] listen interval
1991 		 *	[6*] current AP address (reassoc only)
1992 		 *	[tlv] ssid
1993 		 *	[tlv] supported rates
1994 		 *	[tlv] extended supported rates
1995 		 *	[4] power capability (optional)
1996 		 *	[28] supported channels (optional)
1997 		 *	[tlv] HT capabilities
1998 		 *	[tlv] WME (optional)
1999 		 *	[tlv] Vendor OUI HT capabilities (optional)
2000 		 *	[tlv] Atheros capabilities (if negotiated)
2001 		 *	[tlv] AppIE's (optional)
2002 		 */
2003 		m = ieee80211_getmgtframe(&frm,
2004 			 ic->ic_headroom + sizeof(struct ieee80211_frame),
2005 			 sizeof(uint16_t)
2006 		       + sizeof(uint16_t)
2007 		       + IEEE80211_ADDR_LEN
2008 		       + 2 + IEEE80211_NWID_LEN
2009 		       + 2 + IEEE80211_RATE_SIZE
2010 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2011 		       + 4
2012 		       + 2 + 26
2013 		       + sizeof(struct ieee80211_wme_info)
2014 		       + sizeof(struct ieee80211_ie_htcap)
2015 		       + 4 + sizeof(struct ieee80211_ie_htcap)
2016 #ifdef IEEE80211_SUPPORT_SUPERG
2017 		       + sizeof(struct ieee80211_ath_ie)
2018 #endif
2019 		       + (vap->iv_appie_wpa != NULL ?
2020 				vap->iv_appie_wpa->ie_len : 0)
2021 		       + (vap->iv_appie_assocreq != NULL ?
2022 				vap->iv_appie_assocreq->ie_len : 0)
2023 		);
2024 		if (m == NULL)
2025 			senderr(ENOMEM, is_tx_nobuf);
2026 
2027 		KASSERT(vap->iv_opmode == IEEE80211_M_STA,
2028 		    ("wrong mode %u", vap->iv_opmode));
2029 		capinfo = IEEE80211_CAPINFO_ESS;
2030 		if (vap->iv_flags & IEEE80211_F_PRIVACY)
2031 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
2032 		/*
2033 		 * NB: Some 11a AP's reject the request when
2034 		 *     short premable is set.
2035 		 */
2036 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2037 		    IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2038 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2039 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
2040 		    (ic->ic_caps & IEEE80211_C_SHSLOT))
2041 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2042 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
2043 		    (vap->iv_flags & IEEE80211_F_DOTH))
2044 			capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2045 		*(uint16_t *)frm = htole16(capinfo);
2046 		frm += 2;
2047 
2048 		KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
2049 		*(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
2050 						    bss->ni_intval));
2051 		frm += 2;
2052 
2053 		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2054 			IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
2055 			frm += IEEE80211_ADDR_LEN;
2056 		}
2057 
2058 		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
2059 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
2060 		if (vap->iv_flags & IEEE80211_F_WPA2) {
2061 			if (vap->iv_rsn_ie != NULL)
2062 				frm = add_ie(frm, vap->iv_rsn_ie);
2063 			/* XXX else complain? */
2064 		}
2065 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2066 		if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
2067 			frm = ieee80211_add_powercapability(frm,
2068 			    ic->ic_curchan);
2069 			frm = ieee80211_add_supportedchannels(frm, ic);
2070 		}
2071 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2072 		    ni->ni_ies.htcap_ie != NULL &&
2073 		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP)
2074 			frm = ieee80211_add_htcap(frm, ni);
2075 		if (vap->iv_flags & IEEE80211_F_WPA1) {
2076 			if (vap->iv_wpa_ie != NULL)
2077 				frm = add_ie(frm, vap->iv_wpa_ie);
2078 			/* XXX else complain */
2079 		}
2080 		if ((ic->ic_flags & IEEE80211_F_WME) &&
2081 		    ni->ni_ies.wme_ie != NULL)
2082 			frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
2083 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2084 		    ni->ni_ies.htcap_ie != NULL &&
2085 		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR)
2086 			frm = ieee80211_add_htcap_vendor(frm, ni);
2087 #ifdef IEEE80211_SUPPORT_SUPERG
2088 		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
2089 			frm = ieee80211_add_ath(frm,
2090 				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2091 				((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2092 				 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2093 				vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2094 		}
2095 #endif /* IEEE80211_SUPPORT_SUPERG */
2096 		if (vap->iv_appie_assocreq != NULL)
2097 			frm = add_appie(frm, vap->iv_appie_assocreq);
2098 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2099 
2100 		ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2101 			(void *) vap->iv_state);
2102 		break;
2103 
2104 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2105 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2106 		/*
2107 		 * asresp frame format
2108 		 *	[2] capability information
2109 		 *	[2] status
2110 		 *	[2] association ID
2111 		 *	[tlv] supported rates
2112 		 *	[tlv] extended supported rates
2113 		 *	[tlv] HT capabilities (standard, if STA enabled)
2114 		 *	[tlv] HT information (standard, if STA enabled)
2115 		 *	[tlv] WME (if configured and STA enabled)
2116 		 *	[tlv] HT capabilities (vendor OUI, if STA enabled)
2117 		 *	[tlv] HT information (vendor OUI, if STA enabled)
2118 		 *	[tlv] Atheros capabilities (if STA enabled)
2119 		 *	[tlv] AppIE's (optional)
2120 		 */
2121 		m = ieee80211_getmgtframe(&frm,
2122 			 ic->ic_headroom + sizeof(struct ieee80211_frame),
2123 			 sizeof(uint16_t)
2124 		       + sizeof(uint16_t)
2125 		       + sizeof(uint16_t)
2126 		       + 2 + IEEE80211_RATE_SIZE
2127 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2128 		       + sizeof(struct ieee80211_ie_htcap) + 4
2129 		       + sizeof(struct ieee80211_ie_htinfo) + 4
2130 		       + sizeof(struct ieee80211_wme_param)
2131 #ifdef IEEE80211_SUPPORT_SUPERG
2132 		       + sizeof(struct ieee80211_ath_ie)
2133 #endif
2134 		       + (vap->iv_appie_assocresp != NULL ?
2135 				vap->iv_appie_assocresp->ie_len : 0)
2136 		);
2137 		if (m == NULL)
2138 			senderr(ENOMEM, is_tx_nobuf);
2139 
2140 		capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2141 		*(uint16_t *)frm = htole16(capinfo);
2142 		frm += 2;
2143 
2144 		*(uint16_t *)frm = htole16(arg);	/* status */
2145 		frm += 2;
2146 
2147 		if (arg == IEEE80211_STATUS_SUCCESS) {
2148 			*(uint16_t *)frm = htole16(ni->ni_associd);
2149 			IEEE80211_NODE_STAT(ni, tx_assoc);
2150 		} else
2151 			IEEE80211_NODE_STAT(ni, tx_assoc_fail);
2152 		frm += 2;
2153 
2154 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
2155 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2156 		/* NB: respond according to what we received */
2157 		if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
2158 			frm = ieee80211_add_htcap(frm, ni);
2159 			frm = ieee80211_add_htinfo(frm, ni);
2160 		}
2161 		if ((vap->iv_flags & IEEE80211_F_WME) &&
2162 		    ni->ni_ies.wme_ie != NULL)
2163 			frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2164 		if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
2165 			frm = ieee80211_add_htcap_vendor(frm, ni);
2166 			frm = ieee80211_add_htinfo_vendor(frm, ni);
2167 		}
2168 #ifdef IEEE80211_SUPPORT_SUPERG
2169 		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
2170 			frm = ieee80211_add_ath(frm,
2171 				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2172 				((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2173 				 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2174 				vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2175 #endif /* IEEE80211_SUPPORT_SUPERG */
2176 		if (vap->iv_appie_assocresp != NULL)
2177 			frm = add_appie(frm, vap->iv_appie_assocresp);
2178 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2179 		break;
2180 
2181 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
2182 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2183 		    "send station disassociate (reason %d)", arg);
2184 		m = ieee80211_getmgtframe(&frm,
2185 			ic->ic_headroom + sizeof(struct ieee80211_frame),
2186 			sizeof(uint16_t));
2187 		if (m == NULL)
2188 			senderr(ENOMEM, is_tx_nobuf);
2189 		*(uint16_t *)frm = htole16(arg);	/* reason */
2190 		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2191 
2192 		IEEE80211_NODE_STAT(ni, tx_disassoc);
2193 		IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
2194 		break;
2195 
2196 	default:
2197 		IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
2198 		    "invalid mgmt frame type %u", type);
2199 		senderr(EINVAL, is_tx_unknownmgt);
2200 		/* NOTREACHED */
2201 	}
2202 
2203 	/* NB: force non-ProbeResp frames to the highest queue */
2204 	params.ibp_pri = WME_AC_VO;
2205 	params.ibp_rate0 = bss->ni_txparms->mgmtrate;
2206 	/* NB: we know all frames are unicast */
2207 	params.ibp_try0 = bss->ni_txparms->maxretry;
2208 	params.ibp_power = bss->ni_txpower;
2209 	return ieee80211_mgmt_output(ni, m, type, &params);
2210 bad:
2211 	ieee80211_free_node(ni);
2212 	return ret;
2213 #undef senderr
2214 #undef HTFLAGS
2215 }
2216 
2217 /*
2218  * Return an mbuf with a probe response frame in it.
2219  * Space is left to prepend and 802.11 header at the
2220  * front but it's left to the caller to fill in.
2221  */
2222 struct mbuf *
2223 ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
2224 {
2225 	struct ieee80211vap *vap = bss->ni_vap;
2226 	struct ieee80211com *ic = bss->ni_ic;
2227 	const struct ieee80211_rateset *rs;
2228 	struct mbuf *m;
2229 	uint16_t capinfo;
2230 	uint8_t *frm;
2231 
2232 	/*
2233 	 * probe response frame format
2234 	 *	[8] time stamp
2235 	 *	[2] beacon interval
2236 	 *	[2] cabability information
2237 	 *	[tlv] ssid
2238 	 *	[tlv] supported rates
2239 	 *	[tlv] parameter set (FH/DS)
2240 	 *	[tlv] parameter set (IBSS)
2241 	 *	[tlv] country (optional)
2242 	 *	[3] power control (optional)
2243 	 *	[5] channel switch announcement (CSA) (optional)
2244 	 *	[tlv] extended rate phy (ERP)
2245 	 *	[tlv] extended supported rates
2246 	 *	[tlv] RSN (optional)
2247 	 *	[tlv] HT capabilities
2248 	 *	[tlv] HT information
2249 	 *	[tlv] WPA (optional)
2250 	 *	[tlv] WME (optional)
2251 	 *	[tlv] Vendor OUI HT capabilities (optional)
2252 	 *	[tlv] Vendor OUI HT information (optional)
2253 	 *	[tlv] Atheros capabilities
2254 	 *	[tlv] AppIE's (optional)
2255 	 *	[tlv] Mesh ID (MBSS)
2256 	 *	[tlv] Mesh Conf (MBSS)
2257 	 */
2258 	m = ieee80211_getmgtframe(&frm,
2259 		 ic->ic_headroom + sizeof(struct ieee80211_frame),
2260 		 8
2261 	       + sizeof(uint16_t)
2262 	       + sizeof(uint16_t)
2263 	       + 2 + IEEE80211_NWID_LEN
2264 	       + 2 + IEEE80211_RATE_SIZE
2265 	       + 7	/* max(7,3) */
2266 	       + IEEE80211_COUNTRY_MAX_SIZE
2267 	       + 3
2268 	       + sizeof(struct ieee80211_csa_ie)
2269 	       + 3
2270 	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2271 	       + sizeof(struct ieee80211_ie_wpa)
2272 	       + sizeof(struct ieee80211_ie_htcap)
2273 	       + sizeof(struct ieee80211_ie_htinfo)
2274 	       + sizeof(struct ieee80211_ie_wpa)
2275 	       + sizeof(struct ieee80211_wme_param)
2276 	       + 4 + sizeof(struct ieee80211_ie_htcap)
2277 	       + 4 + sizeof(struct ieee80211_ie_htinfo)
2278 #ifdef IEEE80211_SUPPORT_SUPERG
2279 	       + sizeof(struct ieee80211_ath_ie)
2280 #endif
2281 #ifdef IEEE80211_SUPPORT_MESH
2282 	       + 2 + IEEE80211_MESHID_LEN
2283 	       + sizeof(struct ieee80211_meshconf_ie)
2284 #endif
2285 	       + (vap->iv_appie_proberesp != NULL ?
2286 			vap->iv_appie_proberesp->ie_len : 0)
2287 	);
2288 	if (m == NULL) {
2289 		vap->iv_stats.is_tx_nobuf++;
2290 		return NULL;
2291 	}
2292 
2293 	memset(frm, 0, 8);	/* timestamp should be filled later */
2294 	frm += 8;
2295 	*(uint16_t *)frm = htole16(bss->ni_intval);
2296 	frm += 2;
2297 	capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2298 	*(uint16_t *)frm = htole16(capinfo);
2299 	frm += 2;
2300 
2301 	frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
2302 	rs = ieee80211_get_suprates(ic, bss->ni_chan);
2303 	frm = ieee80211_add_rates(frm, rs);
2304 
2305 	if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
2306 		*frm++ = IEEE80211_ELEMID_FHPARMS;
2307 		*frm++ = 5;
2308 		*frm++ = bss->ni_fhdwell & 0x00ff;
2309 		*frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
2310 		*frm++ = IEEE80211_FH_CHANSET(
2311 		    ieee80211_chan2ieee(ic, bss->ni_chan));
2312 		*frm++ = IEEE80211_FH_CHANPAT(
2313 		    ieee80211_chan2ieee(ic, bss->ni_chan));
2314 		*frm++ = bss->ni_fhindex;
2315 	} else {
2316 		*frm++ = IEEE80211_ELEMID_DSPARMS;
2317 		*frm++ = 1;
2318 		*frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
2319 	}
2320 
2321 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
2322 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
2323 		*frm++ = 2;
2324 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
2325 	}
2326 	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2327 	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2328 		frm = ieee80211_add_countryie(frm, ic);
2329 	if (vap->iv_flags & IEEE80211_F_DOTH) {
2330 		if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
2331 			frm = ieee80211_add_powerconstraint(frm, vap);
2332 		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2333 			frm = ieee80211_add_csa(frm, vap);
2334 	}
2335 	if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
2336 		frm = ieee80211_add_erp(frm, ic);
2337 	frm = ieee80211_add_xrates(frm, rs);
2338 	if (vap->iv_flags & IEEE80211_F_WPA2) {
2339 		if (vap->iv_rsn_ie != NULL)
2340 			frm = add_ie(frm, vap->iv_rsn_ie);
2341 		/* XXX else complain? */
2342 	}
2343 	/*
2344 	 * NB: legacy 11b clients do not get certain ie's.
2345 	 *     The caller identifies such clients by passing
2346 	 *     a token in legacy to us.  Could expand this to be
2347 	 *     any legacy client for stuff like HT ie's.
2348 	 */
2349 	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2350 	    legacy != IEEE80211_SEND_LEGACY_11B) {
2351 		frm = ieee80211_add_htcap(frm, bss);
2352 		frm = ieee80211_add_htinfo(frm, bss);
2353 	}
2354 	if (vap->iv_flags & IEEE80211_F_WPA1) {
2355 		if (vap->iv_wpa_ie != NULL)
2356 			frm = add_ie(frm, vap->iv_wpa_ie);
2357 		/* XXX else complain? */
2358 	}
2359 	if (vap->iv_flags & IEEE80211_F_WME)
2360 		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2361 	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2362 	    (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
2363 	    legacy != IEEE80211_SEND_LEGACY_11B) {
2364 		frm = ieee80211_add_htcap_vendor(frm, bss);
2365 		frm = ieee80211_add_htinfo_vendor(frm, bss);
2366 	}
2367 #ifdef IEEE80211_SUPPORT_SUPERG
2368 	if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
2369 	    legacy != IEEE80211_SEND_LEGACY_11B)
2370 		frm = ieee80211_add_athcaps(frm, bss);
2371 #endif
2372 	if (vap->iv_appie_proberesp != NULL)
2373 		frm = add_appie(frm, vap->iv_appie_proberesp);
2374 #ifdef IEEE80211_SUPPORT_MESH
2375 	if (vap->iv_opmode == IEEE80211_M_MBSS) {
2376 		frm = ieee80211_add_meshid(frm, vap);
2377 		frm = ieee80211_add_meshconf(frm, vap);
2378 	}
2379 #endif
2380 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2381 
2382 	return m;
2383 }
2384 
2385 /*
2386  * Send a probe response frame to the specified mac address.
2387  * This does not go through the normal mgt frame api so we
2388  * can specify the destination address and re-use the bss node
2389  * for the sta reference.
2390  */
2391 int
2392 ieee80211_send_proberesp(struct ieee80211vap *vap,
2393 	const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
2394 {
2395 	struct ieee80211_node *bss = vap->iv_bss;
2396 	struct ieee80211com *ic = vap->iv_ic;
2397 	struct mbuf *m;
2398 #ifdef IEEE80211_DEBUG
2399 	char ethstr[ETHER_ADDRSTRLEN + 1];
2400 #endif
2401 
2402 	if (vap->iv_state == IEEE80211_S_CAC) {
2403 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
2404 		    "block %s frame in CAC state", "probe response");
2405 		vap->iv_stats.is_tx_badstate++;
2406 		return EIO;		/* XXX */
2407 	}
2408 
2409 	/*
2410 	 * Hold a reference on the node so it doesn't go away until after
2411 	 * the xmit is complete all the way in the driver.  On error we
2412 	 * will remove our reference.
2413 	 */
2414 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2415 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2416 	    __func__, __LINE__, bss, kether_ntoa(bss->ni_macaddr, ethstr),
2417 	    ieee80211_node_refcnt(bss)+1);
2418 	ieee80211_ref_node(bss);
2419 
2420 	m = ieee80211_alloc_proberesp(bss, legacy);
2421 	if (m == NULL) {
2422 		ieee80211_free_node(bss);
2423 		return ENOMEM;
2424 	}
2425 
2426 	M_PREPEND(m, sizeof(struct ieee80211_frame), MB_DONTWAIT);
2427 	KASSERT(m != NULL, ("no room for header"));
2428 
2429 	ieee80211_send_setup(bss, m,
2430 	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
2431 	     IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
2432 	/* XXX power management? */
2433 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
2434 
2435 	M_WME_SETAC(m, WME_AC_BE);
2436 
2437 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2438 	    "send probe resp on channel %u to %s%s\n",
2439 	    ieee80211_chan2ieee(ic, ic->ic_curchan), kether_ntoa(da, ethstr),
2440 	    legacy ? " <legacy>" : "");
2441 	IEEE80211_NODE_STAT(bss, tx_mgmt);
2442 
2443 	return ic->ic_raw_xmit(bss, m, NULL);
2444 }
2445 
2446 /*
2447  * Allocate and build a RTS (Request To Send) control frame.
2448  */
2449 struct mbuf *
2450 ieee80211_alloc_rts(struct ieee80211com *ic,
2451 	const uint8_t ra[IEEE80211_ADDR_LEN],
2452 	const uint8_t ta[IEEE80211_ADDR_LEN],
2453 	uint16_t dur)
2454 {
2455 	struct ieee80211_frame_rts *rts;
2456 	struct mbuf *m;
2457 
2458 	/* XXX honor ic_headroom */
2459 	m = m_gethdr(MB_DONTWAIT, MT_DATA);
2460 	if (m != NULL) {
2461 		rts = mtod(m, struct ieee80211_frame_rts *);
2462 		rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2463 			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
2464 		rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2465 		*(u_int16_t *)rts->i_dur = htole16(dur);
2466 		IEEE80211_ADDR_COPY(rts->i_ra, ra);
2467 		IEEE80211_ADDR_COPY(rts->i_ta, ta);
2468 
2469 		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
2470 	}
2471 	return m;
2472 }
2473 
2474 /*
2475  * Allocate and build a CTS (Clear To Send) control frame.
2476  */
2477 struct mbuf *
2478 ieee80211_alloc_cts(struct ieee80211com *ic,
2479 	const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
2480 {
2481 	struct ieee80211_frame_cts *cts;
2482 	struct mbuf *m;
2483 
2484 	/* XXX honor ic_headroom */
2485 	m = m_gethdr(MB_DONTWAIT, MT_DATA);
2486 	if (m != NULL) {
2487 		cts = mtod(m, struct ieee80211_frame_cts *);
2488 		cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2489 			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
2490 		cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2491 		*(u_int16_t *)cts->i_dur = htole16(dur);
2492 		IEEE80211_ADDR_COPY(cts->i_ra, ra);
2493 
2494 		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
2495 	}
2496 	return m;
2497 }
2498 
2499 static void
2500 ieee80211_tx_mgt_timeout_callout(void *arg)
2501 {
2502 	struct ieee80211_node *ni = arg;
2503 	struct ieee80211vap *vap;
2504 
2505 	wlan_serialize_enter();
2506 	vap = ni->ni_vap;
2507 	if (vap->iv_state != IEEE80211_S_INIT &&
2508 	    (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
2509 		/*
2510 		 * NB: it's safe to specify a timeout as the reason here;
2511 		 *     it'll only be used in the right state.
2512 		 */
2513 		ieee80211_new_state(vap, IEEE80211_S_SCAN,
2514 			IEEE80211_SCAN_FAIL_TIMEOUT);
2515 	}
2516 	wlan_serialize_exit();
2517 }
2518 
2519 static void
2520 ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
2521 {
2522 	struct ieee80211vap *vap = ni->ni_vap;
2523 	enum ieee80211_state ostate = (enum ieee80211_state) arg;
2524 
2525 	/*
2526 	 * Frame transmit completed; arrange timer callback.  If
2527 	 * transmit was successfuly we wait for response.  Otherwise
2528 	 * we arrange an immediate callback instead of doing the
2529 	 * callback directly since we don't know what state the driver
2530 	 * is in (e.g. what locks it is holding).  This work should
2531 	 * not be too time-critical and not happen too often so the
2532 	 * added overhead is acceptable.
2533 	 *
2534 	 * XXX what happens if !acked but response shows up before callback?
2535 	 */
2536 	if (vap->iv_state == ostate)
2537 		callout_reset(&vap->iv_mgtsend,
2538 			status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
2539 			ieee80211_tx_mgt_timeout_callout, ni);
2540 }
2541 
2542 static void
2543 ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
2544 	struct ieee80211_beacon_offsets *bo, struct ieee80211_node *ni)
2545 {
2546 	struct ieee80211vap *vap = ni->ni_vap;
2547 	struct ieee80211com *ic = ni->ni_ic;
2548 	struct ieee80211_rateset *rs = &ni->ni_rates;
2549 	uint16_t capinfo;
2550 
2551 	/*
2552 	 * beacon frame format
2553 	 *	[8] time stamp
2554 	 *	[2] beacon interval
2555 	 *	[2] cabability information
2556 	 *	[tlv] ssid
2557 	 *	[tlv] supported rates
2558 	 *	[3] parameter set (DS)
2559 	 *	[8] CF parameter set (optional)
2560 	 *	[tlv] parameter set (IBSS/TIM)
2561 	 *	[tlv] country (optional)
2562 	 *	[3] power control (optional)
2563 	 *	[5] channel switch announcement (CSA) (optional)
2564 	 *	[tlv] extended rate phy (ERP)
2565 	 *	[tlv] extended supported rates
2566 	 *	[tlv] RSN parameters
2567 	 *	[tlv] HT capabilities
2568 	 *	[tlv] HT information
2569 	 * XXX Vendor-specific OIDs (e.g. Atheros)
2570 	 *	[tlv] WPA parameters
2571 	 *	[tlv] WME parameters
2572 	 *	[tlv] Vendor OUI HT capabilities (optional)
2573 	 *	[tlv] Vendor OUI HT information (optional)
2574 	 *	[tlv] Atheros capabilities (optional)
2575 	 *	[tlv] TDMA parameters (optional)
2576 	 *	[tlv] Mesh ID (MBSS)
2577 	 *	[tlv] Mesh Conf (MBSS)
2578 	 *	[tlv] application data (optional)
2579 	 */
2580 
2581 	memset(bo, 0, sizeof(*bo));
2582 
2583 	memset(frm, 0, 8);	/* XXX timestamp is set by hardware/driver */
2584 	frm += 8;
2585 	*(uint16_t *)frm = htole16(ni->ni_intval);
2586 	frm += 2;
2587 	capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
2588 	bo->bo_caps = (uint16_t *)frm;
2589 	*(uint16_t *)frm = htole16(capinfo);
2590 	frm += 2;
2591 	*frm++ = IEEE80211_ELEMID_SSID;
2592 	if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
2593 		*frm++ = ni->ni_esslen;
2594 		memcpy(frm, ni->ni_essid, ni->ni_esslen);
2595 		frm += ni->ni_esslen;
2596 	} else
2597 		*frm++ = 0;
2598 	frm = ieee80211_add_rates(frm, rs);
2599 	if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
2600 		*frm++ = IEEE80211_ELEMID_DSPARMS;
2601 		*frm++ = 1;
2602 		*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2603 	}
2604 	if (ic->ic_flags & IEEE80211_F_PCF) {
2605 		bo->bo_cfp = frm;
2606 		frm = ieee80211_add_cfparms(frm, ic);
2607 	}
2608 	bo->bo_tim = frm;
2609 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
2610 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
2611 		*frm++ = 2;
2612 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
2613 		bo->bo_tim_len = 0;
2614 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2615 	    vap->iv_opmode == IEEE80211_M_MBSS) {
2616 		/* TIM IE is the same for Mesh and Hostap */
2617 		struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
2618 
2619 		tie->tim_ie = IEEE80211_ELEMID_TIM;
2620 		tie->tim_len = 4;	/* length */
2621 		tie->tim_count = 0;	/* DTIM count */
2622 		tie->tim_period = vap->iv_dtim_period;	/* DTIM period */
2623 		tie->tim_bitctl = 0;	/* bitmap control */
2624 		tie->tim_bitmap[0] = 0;	/* Partial Virtual Bitmap */
2625 		frm += sizeof(struct ieee80211_tim_ie);
2626 		bo->bo_tim_len = 1;
2627 	}
2628 	bo->bo_tim_trailer = frm;
2629 	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2630 	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2631 		frm = ieee80211_add_countryie(frm, ic);
2632 	if (vap->iv_flags & IEEE80211_F_DOTH) {
2633 		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
2634 			frm = ieee80211_add_powerconstraint(frm, vap);
2635 		bo->bo_csa = frm;
2636 		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2637 			frm = ieee80211_add_csa(frm, vap);
2638 	} else
2639 		bo->bo_csa = frm;
2640 	if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
2641 		bo->bo_erp = frm;
2642 		frm = ieee80211_add_erp(frm, ic);
2643 	}
2644 	frm = ieee80211_add_xrates(frm, rs);
2645 	if (vap->iv_flags & IEEE80211_F_WPA2) {
2646 		if (vap->iv_rsn_ie != NULL)
2647 			frm = add_ie(frm, vap->iv_rsn_ie);
2648 		/* XXX else complain */
2649 	}
2650 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
2651 		frm = ieee80211_add_htcap(frm, ni);
2652 		bo->bo_htinfo = frm;
2653 		frm = ieee80211_add_htinfo(frm, ni);
2654 	}
2655 	if (vap->iv_flags & IEEE80211_F_WPA1) {
2656 		if (vap->iv_wpa_ie != NULL)
2657 			frm = add_ie(frm, vap->iv_wpa_ie);
2658 		/* XXX else complain */
2659 	}
2660 	if (vap->iv_flags & IEEE80211_F_WME) {
2661 		bo->bo_wme = frm;
2662 		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2663 	}
2664 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2665 	    (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
2666 		frm = ieee80211_add_htcap_vendor(frm, ni);
2667 		frm = ieee80211_add_htinfo_vendor(frm, ni);
2668 	}
2669 #ifdef IEEE80211_SUPPORT_SUPERG
2670 	if (vap->iv_flags & IEEE80211_F_ATHEROS) {
2671 		bo->bo_ath = frm;
2672 		frm = ieee80211_add_athcaps(frm, ni);
2673 	}
2674 #endif
2675 #ifdef IEEE80211_SUPPORT_TDMA
2676 	if (vap->iv_caps & IEEE80211_C_TDMA) {
2677 		bo->bo_tdma = frm;
2678 		frm = ieee80211_add_tdma(frm, vap);
2679 	}
2680 #endif
2681 	if (vap->iv_appie_beacon != NULL) {
2682 		bo->bo_appie = frm;
2683 		bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
2684 		frm = add_appie(frm, vap->iv_appie_beacon);
2685 	}
2686 #ifdef IEEE80211_SUPPORT_MESH
2687 	if (vap->iv_opmode == IEEE80211_M_MBSS) {
2688 		frm = ieee80211_add_meshid(frm, vap);
2689 		bo->bo_meshconf = frm;
2690 		frm = ieee80211_add_meshconf(frm, vap);
2691 	}
2692 #endif
2693 	bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
2694 	bo->bo_csa_trailer_len = frm - bo->bo_csa;
2695 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2696 }
2697 
2698 /*
2699  * Allocate a beacon frame and fillin the appropriate bits.
2700  */
2701 struct mbuf *
2702 ieee80211_beacon_alloc(struct ieee80211_node *ni,
2703 	struct ieee80211_beacon_offsets *bo)
2704 {
2705 	struct ieee80211vap *vap = ni->ni_vap;
2706 	struct ieee80211com *ic = ni->ni_ic;
2707 	struct ifnet *ifp = vap->iv_ifp;
2708 	struct ieee80211_frame *wh;
2709 	struct mbuf *m;
2710 	int pktlen;
2711 	uint8_t *frm;
2712 
2713 	/*
2714 	 * beacon frame format
2715 	 *	[8] time stamp
2716 	 *	[2] beacon interval
2717 	 *	[2] cabability information
2718 	 *	[tlv] ssid
2719 	 *	[tlv] supported rates
2720 	 *	[3] parameter set (DS)
2721 	 *	[8] CF parameter set (optional)
2722 	 *	[tlv] parameter set (IBSS/TIM)
2723 	 *	[tlv] country (optional)
2724 	 *	[3] power control (optional)
2725 	 *	[5] channel switch announcement (CSA) (optional)
2726 	 *	[tlv] extended rate phy (ERP)
2727 	 *	[tlv] extended supported rates
2728 	 *	[tlv] RSN parameters
2729 	 *	[tlv] HT capabilities
2730 	 *	[tlv] HT information
2731 	 *	[tlv] Vendor OUI HT capabilities (optional)
2732 	 *	[tlv] Vendor OUI HT information (optional)
2733 	 * XXX Vendor-specific OIDs (e.g. Atheros)
2734 	 *	[tlv] WPA parameters
2735 	 *	[tlv] WME parameters
2736 	 *	[tlv] TDMA parameters (optional)
2737 	 *	[tlv] Mesh ID (MBSS)
2738 	 *	[tlv] Mesh Conf (MBSS)
2739 	 *	[tlv] application data (optional)
2740 	 * NB: we allocate the max space required for the TIM bitmap.
2741 	 * XXX how big is this?
2742 	 */
2743 	pktlen =   8					/* time stamp */
2744 		 + sizeof(uint16_t)			/* beacon interval */
2745 		 + sizeof(uint16_t)			/* capabilities */
2746 		 + 2 + ni->ni_esslen			/* ssid */
2747 	         + 2 + IEEE80211_RATE_SIZE		/* supported rates */
2748 	         + 2 + 1				/* DS parameters */
2749 		 + 2 + 6				/* CF parameters */
2750 		 + 2 + 4 + vap->iv_tim_len		/* DTIM/IBSSPARMS */
2751 		 + IEEE80211_COUNTRY_MAX_SIZE		/* country */
2752 		 + 2 + 1				/* power control */
2753 	         + sizeof(struct ieee80211_csa_ie)	/* CSA */
2754 		 + 2 + 1				/* ERP */
2755 	         + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2756 		 + (vap->iv_caps & IEEE80211_C_WPA ?	/* WPA 1+2 */
2757 			2*sizeof(struct ieee80211_ie_wpa) : 0)
2758 		 /* XXX conditional? */
2759 		 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
2760 		 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
2761 		 + (vap->iv_caps & IEEE80211_C_WME ?	/* WME */
2762 			sizeof(struct ieee80211_wme_param) : 0)
2763 #ifdef IEEE80211_SUPPORT_SUPERG
2764 		 + sizeof(struct ieee80211_ath_ie)	/* ATH */
2765 #endif
2766 #ifdef IEEE80211_SUPPORT_TDMA
2767 		 + (vap->iv_caps & IEEE80211_C_TDMA ?	/* TDMA */
2768 			sizeof(struct ieee80211_tdma_param) : 0)
2769 #endif
2770 #ifdef IEEE80211_SUPPORT_MESH
2771 		 + 2 + ni->ni_meshidlen
2772 		 + sizeof(struct ieee80211_meshconf_ie)
2773 #endif
2774 		 + IEEE80211_MAX_APPIE
2775 		 ;
2776 	m = ieee80211_getmgtframe(&frm,
2777 		ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
2778 	if (m == NULL) {
2779 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
2780 			"%s: cannot get buf; size %u\n", __func__, pktlen);
2781 		vap->iv_stats.is_tx_nobuf++;
2782 		return NULL;
2783 	}
2784 	ieee80211_beacon_construct(m, frm, bo, ni);
2785 
2786 	M_PREPEND(m, sizeof(struct ieee80211_frame), MB_DONTWAIT);
2787 	KASSERT(m != NULL, ("no space for 802.11 header?"));
2788 	wh = mtod(m, struct ieee80211_frame *);
2789 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2790 	    IEEE80211_FC0_SUBTYPE_BEACON;
2791 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2792 	*(uint16_t *)wh->i_dur = 0;
2793 	IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
2794 	IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
2795 	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
2796 	*(uint16_t *)wh->i_seq = 0;
2797 
2798 	return m;
2799 }
2800 
2801 /*
2802  * Update the dynamic parts of a beacon frame based on the current state.
2803  */
2804 int
2805 ieee80211_beacon_update(struct ieee80211_node *ni,
2806 	struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
2807 {
2808 	struct ieee80211vap *vap = ni->ni_vap;
2809 	struct ieee80211com *ic = ni->ni_ic;
2810 	int len_changed = 0;
2811 	uint16_t capinfo;
2812 
2813 	/*
2814 	 * Handle 11h channel change when we've reached the count.
2815 	 * We must recalculate the beacon frame contents to account
2816 	 * for the new channel.  Note we do this only for the first
2817 	 * vap that reaches this point; subsequent vaps just update
2818 	 * their beacon state to reflect the recalculated channel.
2819 	 */
2820 	if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
2821 	    vap->iv_csa_count == ic->ic_csa_count) {
2822 		vap->iv_csa_count = 0;
2823 		/*
2824 		 * Effect channel change before reconstructing the beacon
2825 		 * frame contents as many places reference ni_chan.
2826 		 */
2827 		if (ic->ic_csa_newchan != NULL)
2828 			ieee80211_csa_completeswitch(ic);
2829 		/*
2830 		 * NB: ieee80211_beacon_construct clears all pending
2831 		 * updates in bo_flags so we don't need to explicitly
2832 		 * clear IEEE80211_BEACON_CSA.
2833 		 */
2834 		ieee80211_beacon_construct(m,
2835 		    mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), bo, ni);
2836 
2837 		/* XXX do WME aggressive mode processing? */
2838 		return 1;		/* just assume length changed */
2839 	}
2840 
2841 	/* XXX faster to recalculate entirely or just changes? */
2842 	capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
2843 	*bo->bo_caps = htole16(capinfo);
2844 
2845 	if (vap->iv_flags & IEEE80211_F_WME) {
2846 		struct ieee80211_wme_state *wme = &ic->ic_wme;
2847 
2848 		/*
2849 		 * Check for agressive mode change.  When there is
2850 		 * significant high priority traffic in the BSS
2851 		 * throttle back BE traffic by using conservative
2852 		 * parameters.  Otherwise BE uses agressive params
2853 		 * to optimize performance of legacy/non-QoS traffic.
2854 		 */
2855 		if (wme->wme_flags & WME_F_AGGRMODE) {
2856 			if (wme->wme_hipri_traffic >
2857 			    wme->wme_hipri_switch_thresh) {
2858 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
2859 				    "%s: traffic %u, disable aggressive mode\n",
2860 				    __func__, wme->wme_hipri_traffic);
2861 				wme->wme_flags &= ~WME_F_AGGRMODE;
2862 				ieee80211_wme_updateparams_locked(vap);
2863 				wme->wme_hipri_traffic =
2864 					wme->wme_hipri_switch_hysteresis;
2865 			} else
2866 				wme->wme_hipri_traffic = 0;
2867 		} else {
2868 			if (wme->wme_hipri_traffic <=
2869 			    wme->wme_hipri_switch_thresh) {
2870 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
2871 				    "%s: traffic %u, enable aggressive mode\n",
2872 				    __func__, wme->wme_hipri_traffic);
2873 				wme->wme_flags |= WME_F_AGGRMODE;
2874 				ieee80211_wme_updateparams_locked(vap);
2875 				wme->wme_hipri_traffic = 0;
2876 			} else
2877 				wme->wme_hipri_traffic =
2878 					wme->wme_hipri_switch_hysteresis;
2879 		}
2880 		if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
2881 			(void) ieee80211_add_wme_param(bo->bo_wme, wme);
2882 			clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
2883 		}
2884 	}
2885 
2886 	if (isset(bo->bo_flags,  IEEE80211_BEACON_HTINFO)) {
2887 		ieee80211_ht_update_beacon(vap, bo);
2888 		clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
2889 	}
2890 #ifdef IEEE80211_SUPPORT_TDMA
2891 	if (vap->iv_caps & IEEE80211_C_TDMA) {
2892 		/*
2893 		 * NB: the beacon is potentially updated every TBTT.
2894 		 */
2895 		ieee80211_tdma_update_beacon(vap, bo);
2896 	}
2897 #endif
2898 #ifdef IEEE80211_SUPPORT_MESH
2899 	if (vap->iv_opmode == IEEE80211_M_MBSS)
2900 		ieee80211_mesh_update_beacon(vap, bo);
2901 #endif
2902 
2903 	if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2904 	    vap->iv_opmode == IEEE80211_M_MBSS) {	/* NB: no IBSS support*/
2905 		struct ieee80211_tim_ie *tie =
2906 			(struct ieee80211_tim_ie *) bo->bo_tim;
2907 		if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
2908 			u_int timlen, timoff, i;
2909 			/*
2910 			 * ATIM/DTIM needs updating.  If it fits in the
2911 			 * current space allocated then just copy in the
2912 			 * new bits.  Otherwise we need to move any trailing
2913 			 * data to make room.  Note that we know there is
2914 			 * contiguous space because ieee80211_beacon_allocate
2915 			 * insures there is space in the mbuf to write a
2916 			 * maximal-size virtual bitmap (based on iv_max_aid).
2917 			 */
2918 			/*
2919 			 * Calculate the bitmap size and offset, copy any
2920 			 * trailer out of the way, and then copy in the
2921 			 * new bitmap and update the information element.
2922 			 * Note that the tim bitmap must contain at least
2923 			 * one byte and any offset must be even.
2924 			 */
2925 			if (vap->iv_ps_pending != 0) {
2926 				timoff = 128;		/* impossibly large */
2927 				for (i = 0; i < vap->iv_tim_len; i++)
2928 					if (vap->iv_tim_bitmap[i]) {
2929 						timoff = i &~ 1;
2930 						break;
2931 					}
2932 				KASSERT(timoff != 128, ("tim bitmap empty!"));
2933 				for (i = vap->iv_tim_len-1; i >= timoff; i--)
2934 					if (vap->iv_tim_bitmap[i])
2935 						break;
2936 				timlen = 1 + (i - timoff);
2937 			} else {
2938 				timoff = 0;
2939 				timlen = 1;
2940 			}
2941 			if (timlen != bo->bo_tim_len) {
2942 				/* copy up/down trailer */
2943 				int adjust = tie->tim_bitmap+timlen
2944 					   - bo->bo_tim_trailer;
2945 				ovbcopy(bo->bo_tim_trailer,
2946 				    bo->bo_tim_trailer+adjust,
2947 				    bo->bo_tim_trailer_len);
2948 				bo->bo_tim_trailer += adjust;
2949 				bo->bo_erp += adjust;
2950 				bo->bo_htinfo += adjust;
2951 #ifdef IEEE80211_SUPERG_SUPPORT
2952 				bo->bo_ath += adjust;
2953 #endif
2954 #ifdef IEEE80211_TDMA_SUPPORT
2955 				bo->bo_tdma += adjust;
2956 #endif
2957 #ifdef IEEE80211_MESH_SUPPORT
2958 				bo->bo_meshconf += adjust;
2959 #endif
2960 				bo->bo_appie += adjust;
2961 				bo->bo_wme += adjust;
2962 				bo->bo_csa += adjust;
2963 				bo->bo_tim_len = timlen;
2964 
2965 				/* update information element */
2966 				tie->tim_len = 3 + timlen;
2967 				tie->tim_bitctl = timoff;
2968 				len_changed = 1;
2969 			}
2970 			memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
2971 				bo->bo_tim_len);
2972 
2973 			clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
2974 
2975 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
2976 				"%s: TIM updated, pending %u, off %u, len %u\n",
2977 				__func__, vap->iv_ps_pending, timoff, timlen);
2978 		}
2979 		/* count down DTIM period */
2980 		if (tie->tim_count == 0)
2981 			tie->tim_count = tie->tim_period - 1;
2982 		else
2983 			tie->tim_count--;
2984 		/* update state for buffered multicast frames on DTIM */
2985 		if (mcast && tie->tim_count == 0)
2986 			tie->tim_bitctl |= 1;
2987 		else
2988 			tie->tim_bitctl &= ~1;
2989 		if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
2990 			struct ieee80211_csa_ie *csa =
2991 			    (struct ieee80211_csa_ie *) bo->bo_csa;
2992 
2993 			/*
2994 			 * Insert or update CSA ie.  If we're just starting
2995 			 * to count down to the channel switch then we need
2996 			 * to insert the CSA ie.  Otherwise we just need to
2997 			 * drop the count.  The actual change happens above
2998 			 * when the vap's count reaches the target count.
2999 			 */
3000 			if (vap->iv_csa_count == 0) {
3001 				memmove(&csa[1], csa, bo->bo_csa_trailer_len);
3002 				bo->bo_erp += sizeof(*csa);
3003 				bo->bo_htinfo += sizeof(*csa);
3004 				bo->bo_wme += sizeof(*csa);
3005 #ifdef IEEE80211_SUPERG_SUPPORT
3006 				bo->bo_ath += sizeof(*csa);
3007 #endif
3008 #ifdef IEEE80211_TDMA_SUPPORT
3009 				bo->bo_tdma += sizeof(*csa);
3010 #endif
3011 #ifdef IEEE80211_MESH_SUPPORT
3012 				bo->bo_meshconf += sizeof(*csa);
3013 #endif
3014 				bo->bo_appie += sizeof(*csa);
3015 				bo->bo_csa_trailer_len += sizeof(*csa);
3016 				bo->bo_tim_trailer_len += sizeof(*csa);
3017 				m->m_len += sizeof(*csa);
3018 				m->m_pkthdr.len += sizeof(*csa);
3019 
3020 				ieee80211_add_csa(bo->bo_csa, vap);
3021 			} else
3022 				csa->csa_count--;
3023 			vap->iv_csa_count++;
3024 			/* NB: don't clear IEEE80211_BEACON_CSA */
3025 		}
3026 		if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
3027 			/*
3028 			 * ERP element needs updating.
3029 			 */
3030 			(void) ieee80211_add_erp(bo->bo_erp, ic);
3031 			clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
3032 		}
3033 #ifdef IEEE80211_SUPPORT_SUPERG
3034 		if (isset(bo->bo_flags,  IEEE80211_BEACON_ATH)) {
3035 			ieee80211_add_athcaps(bo->bo_ath, ni);
3036 			clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
3037 		}
3038 #endif
3039 	}
3040 	if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
3041 		const struct ieee80211_appie *aie = vap->iv_appie_beacon;
3042 		int aielen;
3043 		uint8_t *frm;
3044 
3045 		aielen = 0;
3046 		if (aie != NULL)
3047 			aielen += aie->ie_len;
3048 		if (aielen != bo->bo_appie_len) {
3049 			/* copy up/down trailer */
3050 			int adjust = aielen - bo->bo_appie_len;
3051 			ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
3052 				bo->bo_tim_trailer_len);
3053 			bo->bo_tim_trailer += adjust;
3054 			bo->bo_appie += adjust;
3055 			bo->bo_appie_len = aielen;
3056 
3057 			len_changed = 1;
3058 		}
3059 		frm = bo->bo_appie;
3060 		if (aie != NULL)
3061 			frm  = add_appie(frm, aie);
3062 		clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
3063 	}
3064 
3065 	return len_changed;
3066 }
3067