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