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