xref: /freebsd/sys/net80211/ieee80211_output.c (revision 5956d97f4b3204318ceb6aa9c77bd0bc6ea87a41)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2001 Atsushi Onoe
5  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_inet.h"
33 #include "opt_inet6.h"
34 #include "opt_wlan.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/endian.h>
42 
43 #include <sys/socket.h>
44 
45 #include <net/bpf.h>
46 #include <net/ethernet.h>
47 #include <net/if.h>
48 #include <net/if_var.h>
49 #include <net/if_llc.h>
50 #include <net/if_media.h>
51 #include <net/if_vlan_var.h>
52 
53 #include <net80211/ieee80211_var.h>
54 #include <net80211/ieee80211_regdomain.h>
55 #ifdef IEEE80211_SUPPORT_SUPERG
56 #include <net80211/ieee80211_superg.h>
57 #endif
58 #ifdef IEEE80211_SUPPORT_TDMA
59 #include <net80211/ieee80211_tdma.h>
60 #endif
61 #include <net80211/ieee80211_wds.h>
62 #include <net80211/ieee80211_mesh.h>
63 #include <net80211/ieee80211_vht.h>
64 
65 #if defined(INET) || defined(INET6)
66 #include <netinet/in.h>
67 #endif
68 
69 #ifdef INET
70 #include <netinet/if_ether.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
73 #endif
74 #ifdef INET6
75 #include <netinet/ip6.h>
76 #endif
77 
78 #include <security/mac/mac_framework.h>
79 
80 #define	ETHER_HEADER_COPY(dst, src) \
81 	memcpy(dst, src, sizeof(struct ether_header))
82 
83 static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
84 	u_int hdrsize, u_int ciphdrsize, u_int mtu);
85 static	void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
86 
87 #ifdef IEEE80211_DEBUG
88 /*
89  * Decide if an outbound management frame should be
90  * printed when debugging is enabled.  This filters some
91  * of the less interesting frames that come frequently
92  * (e.g. beacons).
93  */
94 static __inline int
95 doprint(struct ieee80211vap *vap, int subtype)
96 {
97 	switch (subtype) {
98 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
99 		return (vap->iv_opmode == IEEE80211_M_IBSS);
100 	}
101 	return 1;
102 }
103 #endif
104 
105 /*
106  * Transmit a frame to the given destination on the given VAP.
107  *
108  * It's up to the caller to figure out the details of who this
109  * is going to and resolving the node.
110  *
111  * This routine takes care of queuing it for power save,
112  * A-MPDU state stuff, fast-frames state stuff, encapsulation
113  * if required, then passing it up to the driver layer.
114  *
115  * This routine (for now) consumes the mbuf and frees the node
116  * reference; it ideally will return a TX status which reflects
117  * whether the mbuf was consumed or not, so the caller can
118  * free the mbuf (if appropriate) and the node reference (again,
119  * if appropriate.)
120  */
121 int
122 ieee80211_vap_pkt_send_dest(struct ieee80211vap *vap, struct mbuf *m,
123     struct ieee80211_node *ni)
124 {
125 	struct ieee80211com *ic = vap->iv_ic;
126 	struct ifnet *ifp = vap->iv_ifp;
127 	int mcast;
128 	int do_ampdu = 0;
129 #ifdef IEEE80211_SUPPORT_SUPERG
130 	int do_amsdu = 0;
131 	int do_ampdu_amsdu = 0;
132 	int no_ampdu = 1; /* Will be set to 0 if ampdu is active */
133 	int do_ff = 0;
134 #endif
135 
136 	if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
137 	    (m->m_flags & M_PWR_SAV) == 0) {
138 		/*
139 		 * Station in power save mode; pass the frame
140 		 * to the 802.11 layer and continue.  We'll get
141 		 * the frame back when the time is right.
142 		 * XXX lose WDS vap linkage?
143 		 */
144 		if (ieee80211_pwrsave(ni, m) != 0)
145 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
146 		ieee80211_free_node(ni);
147 
148 		/*
149 		 * We queued it fine, so tell the upper layer
150 		 * that we consumed it.
151 		 */
152 		return (0);
153 	}
154 	/* calculate priority so drivers can find the tx queue */
155 	if (ieee80211_classify(ni, m)) {
156 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
157 		    ni->ni_macaddr, NULL,
158 		    "%s", "classification failure");
159 		vap->iv_stats.is_tx_classify++;
160 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
161 		m_freem(m);
162 		ieee80211_free_node(ni);
163 
164 		/* XXX better status? */
165 		return (0);
166 	}
167 	/*
168 	 * Stash the node pointer.  Note that we do this after
169 	 * any call to ieee80211_dwds_mcast because that code
170 	 * uses any existing value for rcvif to identify the
171 	 * interface it (might have been) received on.
172 	 */
173 	MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
174 	m->m_pkthdr.rcvif = (void *)ni;
175 	mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1: 0;
176 
177 	BPF_MTAP(ifp, m);		/* 802.3 tx */
178 
179 	/*
180 	 * Figure out if we can do A-MPDU, A-MSDU or FF.
181 	 *
182 	 * A-MPDU depends upon vap/node config.
183 	 * A-MSDU depends upon vap/node config.
184 	 * FF depends upon vap config, IE and whether
185 	 *  it's 11abg (and not 11n/11ac/etc.)
186 	 *
187 	 * Note that these flags indiciate whether we can do
188 	 * it at all, rather than the situation (eg traffic type.)
189 	 */
190 	do_ampdu = ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
191 	    (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX));
192 #ifdef IEEE80211_SUPPORT_SUPERG
193 	do_amsdu = ((ni->ni_flags & IEEE80211_NODE_AMSDU_TX) &&
194 	    (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_TX));
195 	do_ff =
196 	    ((ni->ni_flags & IEEE80211_NODE_HT) == 0) &&
197 	    ((ni->ni_flags & IEEE80211_NODE_VHT) == 0) &&
198 	    (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF));
199 #endif
200 
201 	/*
202 	 * Check if A-MPDU tx aggregation is setup or if we
203 	 * should try to enable it.  The sta must be associated
204 	 * with HT and A-MPDU enabled for use.  When the policy
205 	 * routine decides we should enable A-MPDU we issue an
206 	 * ADDBA request and wait for a reply.  The frame being
207 	 * encapsulated will go out w/o using A-MPDU, or possibly
208 	 * it might be collected by the driver and held/retransmit.
209 	 * The default ic_ampdu_enable routine handles staggering
210 	 * ADDBA requests in case the receiver NAK's us or we are
211 	 * otherwise unable to establish a BA stream.
212 	 *
213 	 * Don't treat group-addressed frames as candidates for aggregation;
214 	 * net80211 doesn't support 802.11aa-2012 and so group addressed
215 	 * frames will always have sequence numbers allocated from the NON_QOS
216 	 * TID.
217 	 */
218 	if (do_ampdu) {
219 		if ((m->m_flags & M_EAPOL) == 0 && (! mcast)) {
220 			int tid = WME_AC_TO_TID(M_WME_GETAC(m));
221 			struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[tid];
222 
223 			ieee80211_txampdu_count_packet(tap);
224 			if (IEEE80211_AMPDU_RUNNING(tap)) {
225 				/*
226 				 * Operational, mark frame for aggregation.
227 				 *
228 				 * XXX do tx aggregation here
229 				 */
230 				m->m_flags |= M_AMPDU_MPDU;
231 			} else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
232 			    ic->ic_ampdu_enable(ni, tap)) {
233 				/*
234 				 * Not negotiated yet, request service.
235 				 */
236 				ieee80211_ampdu_request(ni, tap);
237 				/* XXX hold frame for reply? */
238 			}
239 			/*
240 			 * Now update the no-ampdu flag.  A-MPDU may have been
241 			 * started or administratively disabled above; so now we
242 			 * know whether we're running yet or not.
243 			 *
244 			 * This will let us know whether we should be doing A-MSDU
245 			 * at this point.  We only do A-MSDU if we're either not
246 			 * doing A-MPDU, or A-MPDU is NACKed, or A-MPDU + A-MSDU
247 			 * is available.
248 			 *
249 			 * Whilst here, update the amsdu-ampdu flag.  The above may
250 			 * have also set or cleared the amsdu-in-ampdu txa_flags
251 			 * combination so we can correctly do A-MPDU + A-MSDU.
252 			 */
253 #ifdef IEEE80211_SUPPORT_SUPERG
254 			no_ampdu = (! IEEE80211_AMPDU_RUNNING(tap)
255 			    || (IEEE80211_AMPDU_NACKED(tap)));
256 			do_ampdu_amsdu = IEEE80211_AMPDU_RUNNING_AMSDU(tap);
257 #endif
258 		}
259 	}
260 
261 #ifdef IEEE80211_SUPPORT_SUPERG
262 	/*
263 	 * Check for AMSDU/FF; queue for aggregation
264 	 *
265 	 * Note: we don't bother trying to do fast frames or
266 	 * A-MSDU encapsulation for 802.3 drivers.  Now, we
267 	 * likely could do it for FF (because it's a magic
268 	 * atheros tunnel LLC type) but I don't think we're going
269 	 * to really need to.  For A-MSDU we'd have to set the
270 	 * A-MSDU QoS bit in the wifi header, so we just plain
271 	 * can't do it.
272 	 */
273 	if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
274 		if ((! mcast) &&
275 		    (do_ampdu_amsdu || (no_ampdu && do_amsdu)) &&
276 		    ieee80211_amsdu_tx_ok(ni)) {
277 			m = ieee80211_amsdu_check(ni, m);
278 			if (m == NULL) {
279 				/* NB: any ni ref held on stageq */
280 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
281 				    "%s: amsdu_check queued frame\n",
282 				    __func__);
283 				return (0);
284 			}
285 		} else if ((! mcast) && do_ff) {
286 			m = ieee80211_ff_check(ni, m);
287 			if (m == NULL) {
288 				/* NB: any ni ref held on stageq */
289 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
290 				    "%s: ff_check queued frame\n",
291 				    __func__);
292 				return (0);
293 			}
294 		}
295 	}
296 #endif /* IEEE80211_SUPPORT_SUPERG */
297 
298 	/*
299 	 * Grab the TX lock - serialise the TX process from this
300 	 * point (where TX state is being checked/modified)
301 	 * through to driver queue.
302 	 */
303 	IEEE80211_TX_LOCK(ic);
304 
305 	/*
306 	 * XXX make the encap and transmit code a separate function
307 	 * so things like the FF (and later A-MSDU) path can just call
308 	 * it for flushed frames.
309 	 */
310 	if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
311 		/*
312 		 * Encapsulate the packet in prep for transmission.
313 		 */
314 		m = ieee80211_encap(vap, ni, m);
315 		if (m == NULL) {
316 			/* NB: stat+msg handled in ieee80211_encap */
317 			IEEE80211_TX_UNLOCK(ic);
318 			ieee80211_free_node(ni);
319 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
320 			return (ENOBUFS);
321 		}
322 	}
323 	(void) ieee80211_parent_xmitpkt(ic, m);
324 
325 	/*
326 	 * Unlock at this point - no need to hold it across
327 	 * ieee80211_free_node() (ie, the comlock)
328 	 */
329 	IEEE80211_TX_UNLOCK(ic);
330 	ic->ic_lastdata = ticks;
331 
332 	return (0);
333 }
334 
335 /*
336  * Send the given mbuf through the given vap.
337  *
338  * This consumes the mbuf regardless of whether the transmit
339  * was successful or not.
340  *
341  * This does none of the initial checks that ieee80211_start()
342  * does (eg CAC timeout, interface wakeup) - the caller must
343  * do this first.
344  */
345 static int
346 ieee80211_start_pkt(struct ieee80211vap *vap, struct mbuf *m)
347 {
348 #define	IS_DWDS(vap) \
349 	(vap->iv_opmode == IEEE80211_M_WDS && \
350 	 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
351 	struct ieee80211com *ic = vap->iv_ic;
352 	struct ifnet *ifp = vap->iv_ifp;
353 	struct ieee80211_node *ni;
354 	struct ether_header *eh;
355 
356 	/*
357 	 * Cancel any background scan.
358 	 */
359 	if (ic->ic_flags & IEEE80211_F_SCAN)
360 		ieee80211_cancel_anyscan(vap);
361 	/*
362 	 * Find the node for the destination so we can do
363 	 * things like power save and fast frames aggregation.
364 	 *
365 	 * NB: past this point various code assumes the first
366 	 *     mbuf has the 802.3 header present (and contiguous).
367 	 */
368 	ni = NULL;
369 	if (m->m_len < sizeof(struct ether_header) &&
370 	   (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
371 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
372 		    "discard frame, %s\n", "m_pullup failed");
373 		vap->iv_stats.is_tx_nobuf++;	/* XXX */
374 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
375 		return (ENOBUFS);
376 	}
377 	eh = mtod(m, struct ether_header *);
378 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
379 		if (IS_DWDS(vap)) {
380 			/*
381 			 * Only unicast frames from the above go out
382 			 * DWDS vaps; multicast frames are handled by
383 			 * dispatching the frame as it comes through
384 			 * the AP vap (see below).
385 			 */
386 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
387 			    eh->ether_dhost, "mcast", "%s", "on DWDS");
388 			vap->iv_stats.is_dwds_mcast++;
389 			m_freem(m);
390 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
391 			/* XXX better status? */
392 			return (ENOBUFS);
393 		}
394 		if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
395 			/*
396 			 * Spam DWDS vap's w/ multicast traffic.
397 			 */
398 			/* XXX only if dwds in use? */
399 			ieee80211_dwds_mcast(vap, m);
400 		}
401 	}
402 #ifdef IEEE80211_SUPPORT_MESH
403 	if (vap->iv_opmode != IEEE80211_M_MBSS) {
404 #endif
405 		ni = ieee80211_find_txnode(vap, eh->ether_dhost);
406 		if (ni == NULL) {
407 			/* NB: ieee80211_find_txnode does stat+msg */
408 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
409 			m_freem(m);
410 			/* XXX better status? */
411 			return (ENOBUFS);
412 		}
413 		if (ni->ni_associd == 0 &&
414 		    (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
415 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
416 			    eh->ether_dhost, NULL,
417 			    "sta not associated (type 0x%04x)",
418 			    htons(eh->ether_type));
419 			vap->iv_stats.is_tx_notassoc++;
420 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
421 			m_freem(m);
422 			ieee80211_free_node(ni);
423 			/* XXX better status? */
424 			return (ENOBUFS);
425 		}
426 #ifdef IEEE80211_SUPPORT_MESH
427 	} else {
428 		if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) {
429 			/*
430 			 * Proxy station only if configured.
431 			 */
432 			if (!ieee80211_mesh_isproxyena(vap)) {
433 				IEEE80211_DISCARD_MAC(vap,
434 				    IEEE80211_MSG_OUTPUT |
435 				    IEEE80211_MSG_MESH,
436 				    eh->ether_dhost, NULL,
437 				    "%s", "proxy not enabled");
438 				vap->iv_stats.is_mesh_notproxy++;
439 				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
440 				m_freem(m);
441 				/* XXX better status? */
442 				return (ENOBUFS);
443 			}
444 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
445 			    "forward frame from DS SA(%6D), DA(%6D)\n",
446 			    eh->ether_shost, ":",
447 			    eh->ether_dhost, ":");
448 			ieee80211_mesh_proxy_check(vap, eh->ether_shost);
449 		}
450 		ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m);
451 		if (ni == NULL) {
452 			/*
453 			 * NB: ieee80211_mesh_discover holds/disposes
454 			 * frame (e.g. queueing on path discovery).
455 			 */
456 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
457 			/* XXX better status? */
458 			return (ENOBUFS);
459 		}
460 	}
461 #endif
462 
463 	/*
464 	 * We've resolved the sender, so attempt to transmit it.
465 	 */
466 
467 	if (vap->iv_state == IEEE80211_S_SLEEP) {
468 		/*
469 		 * In power save; queue frame and then  wakeup device
470 		 * for transmit.
471 		 */
472 		ic->ic_lastdata = ticks;
473 		if (ieee80211_pwrsave(ni, m) != 0)
474 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
475 		ieee80211_free_node(ni);
476 		ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
477 		return (0);
478 	}
479 
480 	if (ieee80211_vap_pkt_send_dest(vap, m, ni) != 0)
481 		return (ENOBUFS);
482 	return (0);
483 #undef	IS_DWDS
484 }
485 
486 /*
487  * Start method for vap's.  All packets from the stack come
488  * through here.  We handle common processing of the packets
489  * before dispatching them to the underlying device.
490  *
491  * if_transmit() requires that the mbuf be consumed by this call
492  * regardless of the return condition.
493  */
494 int
495 ieee80211_vap_transmit(struct ifnet *ifp, struct mbuf *m)
496 {
497 	struct ieee80211vap *vap = ifp->if_softc;
498 	struct ieee80211com *ic = vap->iv_ic;
499 
500 	/*
501 	 * No data frames go out unless we're running.
502 	 * Note in particular this covers CAC and CSA
503 	 * states (though maybe we should check muting
504 	 * for CSA).
505 	 */
506 	if (vap->iv_state != IEEE80211_S_RUN &&
507 	    vap->iv_state != IEEE80211_S_SLEEP) {
508 		IEEE80211_LOCK(ic);
509 		/* re-check under the com lock to avoid races */
510 		if (vap->iv_state != IEEE80211_S_RUN &&
511 		    vap->iv_state != IEEE80211_S_SLEEP) {
512 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
513 			    "%s: ignore queue, in %s state\n",
514 			    __func__, ieee80211_state_name[vap->iv_state]);
515 			vap->iv_stats.is_tx_badstate++;
516 			IEEE80211_UNLOCK(ic);
517 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
518 			m_freem(m);
519 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
520 			return (ENETDOWN);
521 		}
522 		IEEE80211_UNLOCK(ic);
523 	}
524 
525 	/*
526 	 * Sanitize mbuf flags for net80211 use.  We cannot
527 	 * clear M_PWR_SAV or M_MORE_DATA because these may
528 	 * be set for frames that are re-submitted from the
529 	 * power save queue.
530 	 *
531 	 * NB: This must be done before ieee80211_classify as
532 	 *     it marks EAPOL in frames with M_EAPOL.
533 	 */
534 	m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
535 
536 	/*
537 	 * Bump to the packet transmission path.
538 	 * The mbuf will be consumed here.
539 	 */
540 	return (ieee80211_start_pkt(vap, m));
541 }
542 
543 void
544 ieee80211_vap_qflush(struct ifnet *ifp)
545 {
546 
547 	/* Empty for now */
548 }
549 
550 /*
551  * 802.11 raw output routine.
552  *
553  * XXX TODO: this (and other send routines) should correctly
554  * XXX keep the pwr mgmt bit set if it decides to call into the
555  * XXX driver to send a frame whilst the state is SLEEP.
556  *
557  * Otherwise the peer may decide that we're awake and flood us
558  * with traffic we are still too asleep to receive!
559  */
560 int
561 ieee80211_raw_output(struct ieee80211vap *vap, struct ieee80211_node *ni,
562     struct mbuf *m, const struct ieee80211_bpf_params *params)
563 {
564 	struct ieee80211com *ic = vap->iv_ic;
565 	int error;
566 
567 	/*
568 	 * Set node - the caller has taken a reference, so ensure
569 	 * that the mbuf has the same node value that
570 	 * it would if it were going via the normal path.
571 	 */
572 	MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
573 	m->m_pkthdr.rcvif = (void *)ni;
574 
575 	/*
576 	 * Attempt to add bpf transmit parameters.
577 	 *
578 	 * For now it's ok to fail; the raw_xmit api still takes
579 	 * them as an option.
580 	 *
581 	 * Later on when ic_raw_xmit() has params removed,
582 	 * they'll have to be added - so fail the transmit if
583 	 * they can't be.
584 	 */
585 	if (params)
586 		(void) ieee80211_add_xmit_params(m, params);
587 
588 	error = ic->ic_raw_xmit(ni, m, params);
589 	if (error) {
590 		if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, 1);
591 		ieee80211_free_node(ni);
592 	}
593 	return (error);
594 }
595 
596 static int
597 ieee80211_validate_frame(struct mbuf *m,
598     const struct ieee80211_bpf_params *params)
599 {
600 	struct ieee80211_frame *wh;
601 	int type;
602 
603 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
604 		return (EINVAL);
605 
606 	wh = mtod(m, struct ieee80211_frame *);
607 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
608 	    IEEE80211_FC0_VERSION_0)
609 		return (EINVAL);
610 
611 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
612 	if (type != IEEE80211_FC0_TYPE_DATA) {
613 		if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
614 		    IEEE80211_FC1_DIR_NODS)
615 			return (EINVAL);
616 
617 		if (type != IEEE80211_FC0_TYPE_MGT &&
618 		    (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) != 0)
619 			return (EINVAL);
620 
621 		/* XXX skip other field checks? */
622 	}
623 
624 	if ((params && (params->ibp_flags & IEEE80211_BPF_CRYPTO) != 0) ||
625 	    (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) != 0) {
626 		int subtype;
627 
628 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
629 
630 		/*
631 		 * See IEEE Std 802.11-2012,
632 		 * 8.2.4.1.9 'Protected Frame field'
633 		 */
634 		/* XXX no support for robust management frames yet. */
635 		if (!(type == IEEE80211_FC0_TYPE_DATA ||
636 		    (type == IEEE80211_FC0_TYPE_MGT &&
637 		     subtype == IEEE80211_FC0_SUBTYPE_AUTH)))
638 			return (EINVAL);
639 
640 		wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
641 	}
642 
643 	if (m->m_pkthdr.len < ieee80211_anyhdrsize(wh))
644 		return (EINVAL);
645 
646 	return (0);
647 }
648 
649 static int
650 ieee80211_validate_rate(struct ieee80211_node *ni, uint8_t rate)
651 {
652 	struct ieee80211com *ic = ni->ni_ic;
653 
654 	if (IEEE80211_IS_HT_RATE(rate)) {
655 		if ((ic->ic_htcaps & IEEE80211_HTC_HT) == 0)
656 			return (EINVAL);
657 
658 		rate = IEEE80211_RV(rate);
659 		if (rate <= 31) {
660 			if (rate > ic->ic_txstream * 8 - 1)
661 				return (EINVAL);
662 
663 			return (0);
664 		}
665 
666 		if (rate == 32) {
667 			if ((ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
668 				return (EINVAL);
669 
670 			return (0);
671 		}
672 
673 		if ((ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) == 0)
674 			return (EINVAL);
675 
676 		switch (ic->ic_txstream) {
677 		case 0:
678 		case 1:
679 			return (EINVAL);
680 		case 2:
681 			if (rate > 38)
682 				return (EINVAL);
683 
684 			return (0);
685 		case 3:
686 			if (rate > 52)
687 				return (EINVAL);
688 
689 			return (0);
690 		case 4:
691 		default:
692 			if (rate > 76)
693 				return (EINVAL);
694 
695 			return (0);
696 		}
697 	}
698 
699 	if (!ieee80211_isratevalid(ic->ic_rt, rate))
700 		return (EINVAL);
701 
702 	return (0);
703 }
704 
705 static int
706 ieee80211_sanitize_rates(struct ieee80211_node *ni, struct mbuf *m,
707     const struct ieee80211_bpf_params *params)
708 {
709 	int error;
710 
711 	if (!params)
712 		return (0);	/* nothing to do */
713 
714 	/* NB: most drivers assume that ibp_rate0 is set (!= 0). */
715 	if (params->ibp_rate0 != 0) {
716 		error = ieee80211_validate_rate(ni, params->ibp_rate0);
717 		if (error != 0)
718 			return (error);
719 	} else {
720 		/* XXX pre-setup some default (e.g., mgmt / mcast) rate */
721 		/* XXX __DECONST? */
722 		(void) m;
723 	}
724 
725 	if (params->ibp_rate1 != 0 &&
726 	    (error = ieee80211_validate_rate(ni, params->ibp_rate1)) != 0)
727 		return (error);
728 
729 	if (params->ibp_rate2 != 0 &&
730 	    (error = ieee80211_validate_rate(ni, params->ibp_rate2)) != 0)
731 		return (error);
732 
733 	if (params->ibp_rate3 != 0 &&
734 	    (error = ieee80211_validate_rate(ni, params->ibp_rate3)) != 0)
735 		return (error);
736 
737 	return (0);
738 }
739 
740 /*
741  * 802.11 output routine. This is (currently) used only to
742  * connect bpf write calls to the 802.11 layer for injecting
743  * raw 802.11 frames.
744  */
745 int
746 ieee80211_output(struct ifnet *ifp, struct mbuf *m,
747 	const struct sockaddr *dst, struct route *ro)
748 {
749 #define senderr(e) do { error = (e); goto bad;} while (0)
750 	const struct ieee80211_bpf_params *params = NULL;
751 	struct ieee80211_node *ni = NULL;
752 	struct ieee80211vap *vap;
753 	struct ieee80211_frame *wh;
754 	struct ieee80211com *ic = NULL;
755 	int error;
756 	int ret;
757 
758 	if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
759 		/*
760 		 * Short-circuit requests if the vap is marked OACTIVE
761 		 * as this can happen because a packet came down through
762 		 * ieee80211_start before the vap entered RUN state in
763 		 * which case it's ok to just drop the frame.  This
764 		 * should not be necessary but callers of if_output don't
765 		 * check OACTIVE.
766 		 */
767 		senderr(ENETDOWN);
768 	}
769 	vap = ifp->if_softc;
770 	ic = vap->iv_ic;
771 	/*
772 	 * Hand to the 802.3 code if not tagged as
773 	 * a raw 802.11 frame.
774 	 */
775 	if (dst->sa_family != AF_IEEE80211)
776 		return vap->iv_output(ifp, m, dst, ro);
777 #ifdef MAC
778 	error = mac_ifnet_check_transmit(ifp, m);
779 	if (error)
780 		senderr(error);
781 #endif
782 	if (ifp->if_flags & IFF_MONITOR)
783 		senderr(ENETDOWN);
784 	if (!IFNET_IS_UP_RUNNING(ifp))
785 		senderr(ENETDOWN);
786 	if (vap->iv_state == IEEE80211_S_CAC) {
787 		IEEE80211_DPRINTF(vap,
788 		    IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
789 		    "block %s frame in CAC state\n", "raw data");
790 		vap->iv_stats.is_tx_badstate++;
791 		senderr(EIO);		/* XXX */
792 	} else if (vap->iv_state == IEEE80211_S_SCAN)
793 		senderr(EIO);
794 	/* XXX bypass bridge, pfil, carp, etc. */
795 
796 	/*
797 	 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
798 	 * present by setting the sa_len field of the sockaddr (yes,
799 	 * this is a hack).
800 	 * NB: we assume sa_data is suitably aligned to cast.
801 	 */
802 	if (dst->sa_len != 0)
803 		params = (const struct ieee80211_bpf_params *)dst->sa_data;
804 
805 	error = ieee80211_validate_frame(m, params);
806 	if (error != 0)
807 		senderr(error);
808 
809 	wh = mtod(m, struct ieee80211_frame *);
810 
811 	/* locate destination node */
812 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
813 	case IEEE80211_FC1_DIR_NODS:
814 	case IEEE80211_FC1_DIR_FROMDS:
815 		ni = ieee80211_find_txnode(vap, wh->i_addr1);
816 		break;
817 	case IEEE80211_FC1_DIR_TODS:
818 	case IEEE80211_FC1_DIR_DSTODS:
819 		ni = ieee80211_find_txnode(vap, wh->i_addr3);
820 		break;
821 	default:
822 		senderr(EDOOFUS);
823 	}
824 	if (ni == NULL) {
825 		/*
826 		 * Permit packets w/ bpf params through regardless
827 		 * (see below about sa_len).
828 		 */
829 		if (dst->sa_len == 0)
830 			senderr(EHOSTUNREACH);
831 		ni = ieee80211_ref_node(vap->iv_bss);
832 	}
833 
834 	/*
835 	 * Sanitize mbuf for net80211 flags leaked from above.
836 	 *
837 	 * NB: This must be done before ieee80211_classify as
838 	 *     it marks EAPOL in frames with M_EAPOL.
839 	 */
840 	m->m_flags &= ~M_80211_TX;
841 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
842 
843 	if (IEEE80211_IS_DATA(wh)) {
844 		/* calculate priority so drivers can find the tx queue */
845 		if (ieee80211_classify(ni, m))
846 			senderr(EIO);		/* XXX */
847 
848 		/* NB: ieee80211_encap does not include 802.11 header */
849 		IEEE80211_NODE_STAT_ADD(ni, tx_bytes,
850 		    m->m_pkthdr.len - ieee80211_hdrsize(wh));
851 	} else
852 		M_WME_SETAC(m, WME_AC_BE);
853 
854 	error = ieee80211_sanitize_rates(ni, m, params);
855 	if (error != 0)
856 		senderr(error);
857 
858 	IEEE80211_NODE_STAT(ni, tx_data);
859 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
860 		IEEE80211_NODE_STAT(ni, tx_mcast);
861 		m->m_flags |= M_MCAST;
862 	} else
863 		IEEE80211_NODE_STAT(ni, tx_ucast);
864 
865 	IEEE80211_TX_LOCK(ic);
866 	ret = ieee80211_raw_output(vap, ni, m, params);
867 	IEEE80211_TX_UNLOCK(ic);
868 	return (ret);
869 bad:
870 	if (m != NULL)
871 		m_freem(m);
872 	if (ni != NULL)
873 		ieee80211_free_node(ni);
874 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
875 	return error;
876 #undef senderr
877 }
878 
879 /*
880  * Set the direction field and address fields of an outgoing
881  * frame.  Note this should be called early on in constructing
882  * a frame as it sets i_fc[1]; other bits can then be or'd in.
883  */
884 void
885 ieee80211_send_setup(
886 	struct ieee80211_node *ni,
887 	struct mbuf *m,
888 	int type, int tid,
889 	const uint8_t sa[IEEE80211_ADDR_LEN],
890 	const uint8_t da[IEEE80211_ADDR_LEN],
891 	const uint8_t bssid[IEEE80211_ADDR_LEN])
892 {
893 #define	WH4(wh)	((struct ieee80211_frame_addr4 *)wh)
894 	struct ieee80211vap *vap = ni->ni_vap;
895 	struct ieee80211_tx_ampdu *tap;
896 	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
897 	ieee80211_seq seqno;
898 
899 	IEEE80211_TX_LOCK_ASSERT(ni->ni_ic);
900 
901 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
902 	if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
903 		switch (vap->iv_opmode) {
904 		case IEEE80211_M_STA:
905 			wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
906 			IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
907 			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
908 			IEEE80211_ADDR_COPY(wh->i_addr3, da);
909 			break;
910 		case IEEE80211_M_IBSS:
911 		case IEEE80211_M_AHDEMO:
912 			wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
913 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
914 			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
915 			IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
916 			break;
917 		case IEEE80211_M_HOSTAP:
918 			wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
919 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
920 			IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
921 			IEEE80211_ADDR_COPY(wh->i_addr3, sa);
922 			break;
923 		case IEEE80211_M_WDS:
924 			wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
925 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
926 			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
927 			IEEE80211_ADDR_COPY(wh->i_addr3, da);
928 			IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
929 			break;
930 		case IEEE80211_M_MBSS:
931 #ifdef IEEE80211_SUPPORT_MESH
932 			if (IEEE80211_IS_MULTICAST(da)) {
933 				wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
934 				/* XXX next hop */
935 				IEEE80211_ADDR_COPY(wh->i_addr1, da);
936 				IEEE80211_ADDR_COPY(wh->i_addr2,
937 				    vap->iv_myaddr);
938 			} else {
939 				wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
940 				IEEE80211_ADDR_COPY(wh->i_addr1, da);
941 				IEEE80211_ADDR_COPY(wh->i_addr2,
942 				    vap->iv_myaddr);
943 				IEEE80211_ADDR_COPY(wh->i_addr3, da);
944 				IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
945 			}
946 #endif
947 			break;
948 		case IEEE80211_M_MONITOR:	/* NB: to quiet compiler */
949 			break;
950 		}
951 	} else {
952 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
953 		IEEE80211_ADDR_COPY(wh->i_addr1, da);
954 		IEEE80211_ADDR_COPY(wh->i_addr2, sa);
955 #ifdef IEEE80211_SUPPORT_MESH
956 		if (vap->iv_opmode == IEEE80211_M_MBSS)
957 			IEEE80211_ADDR_COPY(wh->i_addr3, sa);
958 		else
959 #endif
960 			IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
961 	}
962 	*(uint16_t *)&wh->i_dur[0] = 0;
963 
964 	/*
965 	 * XXX TODO: this is what the TX lock is for.
966 	 * Here we're incrementing sequence numbers, and they
967 	 * need to be in lock-step with what the driver is doing
968 	 * both in TX ordering and crypto encap (IV increment.)
969 	 *
970 	 * If the driver does seqno itself, then we can skip
971 	 * assigning sequence numbers here, and we can avoid
972 	 * requiring the TX lock.
973 	 */
974 	tap = &ni->ni_tx_ampdu[tid];
975 	if (tid != IEEE80211_NONQOS_TID && IEEE80211_AMPDU_RUNNING(tap)) {
976 		m->m_flags |= M_AMPDU_MPDU;
977 
978 		/* NB: zero out i_seq field (for s/w encryption etc) */
979 		*(uint16_t *)&wh->i_seq[0] = 0;
980 	} else {
981 		if (IEEE80211_HAS_SEQ(type & IEEE80211_FC0_TYPE_MASK,
982 				      type & IEEE80211_FC0_SUBTYPE_MASK))
983 			/*
984 			 * 802.11-2012 9.3.2.10 - QoS multicast frames
985 			 * come out of a different seqno space.
986 			 */
987 			if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
988 				seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
989 			} else {
990 				seqno = ni->ni_txseqs[tid]++;
991 			}
992 		else
993 			seqno = 0;
994 
995 		*(uint16_t *)&wh->i_seq[0] =
996 		    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
997 		M_SEQNO_SET(m, seqno);
998 	}
999 
1000 	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1001 		m->m_flags |= M_MCAST;
1002 #undef WH4
1003 }
1004 
1005 /*
1006  * Send a management frame to the specified node.  The node pointer
1007  * must have a reference as the pointer will be passed to the driver
1008  * and potentially held for a long time.  If the frame is successfully
1009  * dispatched to the driver, then it is responsible for freeing the
1010  * reference (and potentially free'ing up any associated storage);
1011  * otherwise deal with reclaiming any reference (on error).
1012  */
1013 int
1014 ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
1015 	struct ieee80211_bpf_params *params)
1016 {
1017 	struct ieee80211vap *vap = ni->ni_vap;
1018 	struct ieee80211com *ic = ni->ni_ic;
1019 	struct ieee80211_frame *wh;
1020 	int ret;
1021 
1022 	KASSERT(ni != NULL, ("null node"));
1023 
1024 	if (vap->iv_state == IEEE80211_S_CAC) {
1025 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
1026 		    ni, "block %s frame in CAC state",
1027 			ieee80211_mgt_subtype_name(type));
1028 		vap->iv_stats.is_tx_badstate++;
1029 		ieee80211_free_node(ni);
1030 		m_freem(m);
1031 		return EIO;		/* XXX */
1032 	}
1033 
1034 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
1035 	if (m == NULL) {
1036 		ieee80211_free_node(ni);
1037 		return ENOMEM;
1038 	}
1039 
1040 	IEEE80211_TX_LOCK(ic);
1041 
1042 	wh = mtod(m, struct ieee80211_frame *);
1043 	ieee80211_send_setup(ni, m,
1044 	     IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
1045 	     vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
1046 	if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
1047 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
1048 		    "encrypting frame (%s)", __func__);
1049 		wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
1050 	}
1051 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
1052 
1053 	KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
1054 	M_WME_SETAC(m, params->ibp_pri);
1055 
1056 #ifdef IEEE80211_DEBUG
1057 	/* avoid printing too many frames */
1058 	if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
1059 	    ieee80211_msg_dumppkts(vap)) {
1060 		ieee80211_note(vap, "[%s] send %s on channel %u\n",
1061 		    ether_sprintf(wh->i_addr1),
1062 		    ieee80211_mgt_subtype_name(type),
1063 		    ieee80211_chan2ieee(ic, ic->ic_curchan));
1064 	}
1065 #endif
1066 	IEEE80211_NODE_STAT(ni, tx_mgmt);
1067 
1068 	ret = ieee80211_raw_output(vap, ni, m, params);
1069 	IEEE80211_TX_UNLOCK(ic);
1070 	return (ret);
1071 }
1072 
1073 static void
1074 ieee80211_nulldata_transmitted(struct ieee80211_node *ni, void *arg,
1075     int status)
1076 {
1077 	struct ieee80211vap *vap = ni->ni_vap;
1078 
1079 	wakeup(vap);
1080 }
1081 
1082 /*
1083  * Send a null data frame to the specified node.  If the station
1084  * is setup for QoS then a QoS Null Data frame is constructed.
1085  * If this is a WDS station then a 4-address frame is constructed.
1086  *
1087  * NB: the caller is assumed to have setup a node reference
1088  *     for use; this is necessary to deal with a race condition
1089  *     when probing for inactive stations.  Like ieee80211_mgmt_output
1090  *     we must cleanup any node reference on error;  however we
1091  *     can safely just unref it as we know it will never be the
1092  *     last reference to the node.
1093  */
1094 int
1095 ieee80211_send_nulldata(struct ieee80211_node *ni)
1096 {
1097 	struct ieee80211vap *vap = ni->ni_vap;
1098 	struct ieee80211com *ic = ni->ni_ic;
1099 	struct mbuf *m;
1100 	struct ieee80211_frame *wh;
1101 	int hdrlen;
1102 	uint8_t *frm;
1103 	int ret;
1104 
1105 	if (vap->iv_state == IEEE80211_S_CAC) {
1106 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
1107 		    ni, "block %s frame in CAC state", "null data");
1108 		ieee80211_unref_node(&ni);
1109 		vap->iv_stats.is_tx_badstate++;
1110 		return EIO;		/* XXX */
1111 	}
1112 
1113 	if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
1114 		hdrlen = sizeof(struct ieee80211_qosframe);
1115 	else
1116 		hdrlen = sizeof(struct ieee80211_frame);
1117 	/* NB: only WDS vap's get 4-address frames */
1118 	if (vap->iv_opmode == IEEE80211_M_WDS)
1119 		hdrlen += IEEE80211_ADDR_LEN;
1120 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
1121 		hdrlen = roundup(hdrlen, sizeof(uint32_t));
1122 
1123 	m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
1124 	if (m == NULL) {
1125 		/* XXX debug msg */
1126 		ieee80211_unref_node(&ni);
1127 		vap->iv_stats.is_tx_nobuf++;
1128 		return ENOMEM;
1129 	}
1130 	KASSERT(M_LEADINGSPACE(m) >= hdrlen,
1131 	    ("leading space %zd", M_LEADINGSPACE(m)));
1132 	M_PREPEND(m, hdrlen, M_NOWAIT);
1133 	if (m == NULL) {
1134 		/* NB: cannot happen */
1135 		ieee80211_free_node(ni);
1136 		return ENOMEM;
1137 	}
1138 
1139 	IEEE80211_TX_LOCK(ic);
1140 
1141 	wh = mtod(m, struct ieee80211_frame *);		/* NB: a little lie */
1142 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
1143 		const int tid = WME_AC_TO_TID(WME_AC_BE);
1144 		uint8_t *qos;
1145 
1146 		ieee80211_send_setup(ni, m,
1147 		    IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
1148 		    tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
1149 
1150 		if (vap->iv_opmode == IEEE80211_M_WDS)
1151 			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1152 		else
1153 			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1154 		qos[0] = tid & IEEE80211_QOS_TID;
1155 		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
1156 			qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1157 		qos[1] = 0;
1158 	} else {
1159 		ieee80211_send_setup(ni, m,
1160 		    IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
1161 		    IEEE80211_NONQOS_TID,
1162 		    vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
1163 	}
1164 	if (vap->iv_opmode != IEEE80211_M_WDS) {
1165 		/* NB: power management bit is never sent by an AP */
1166 		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
1167 		    vap->iv_opmode != IEEE80211_M_HOSTAP)
1168 			wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
1169 	}
1170 	if ((ic->ic_flags & IEEE80211_F_SCAN) &&
1171 	    (ni->ni_flags & IEEE80211_NODE_PWR_MGT)) {
1172 		ieee80211_add_callback(m, ieee80211_nulldata_transmitted,
1173 		    NULL);
1174 	}
1175 	m->m_len = m->m_pkthdr.len = hdrlen;
1176 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
1177 
1178 	M_WME_SETAC(m, WME_AC_BE);
1179 
1180 	IEEE80211_NODE_STAT(ni, tx_data);
1181 
1182 	IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
1183 	    "send %snull data frame on channel %u, pwr mgt %s",
1184 	    ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
1185 	    ieee80211_chan2ieee(ic, ic->ic_curchan),
1186 	    wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
1187 
1188 	ret = ieee80211_raw_output(vap, ni, m, NULL);
1189 	IEEE80211_TX_UNLOCK(ic);
1190 	return (ret);
1191 }
1192 
1193 /*
1194  * Assign priority to a frame based on any vlan tag assigned
1195  * to the station and/or any Diffserv setting in an IP header.
1196  * Finally, if an ACM policy is setup (in station mode) it's
1197  * applied.
1198  */
1199 int
1200 ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
1201 {
1202 	const struct ether_header *eh = NULL;
1203 	uint16_t ether_type;
1204 	int v_wme_ac, d_wme_ac, ac;
1205 
1206 	if (__predict_false(m->m_flags & M_ENCAP)) {
1207 		struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
1208 		struct llc *llc;
1209 		int hdrlen, subtype;
1210 
1211 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1212 		if (subtype & IEEE80211_FC0_SUBTYPE_NODATA) {
1213 			ac = WME_AC_BE;
1214 			goto done;
1215 		}
1216 
1217 		hdrlen = ieee80211_hdrsize(wh);
1218 		if (m->m_pkthdr.len < hdrlen + sizeof(*llc))
1219 			return 1;
1220 
1221 		llc = (struct llc *)mtodo(m, hdrlen);
1222 		if (llc->llc_dsap != LLC_SNAP_LSAP ||
1223 		    llc->llc_ssap != LLC_SNAP_LSAP ||
1224 		    llc->llc_control != LLC_UI ||
1225 		    llc->llc_snap.org_code[0] != 0 ||
1226 		    llc->llc_snap.org_code[1] != 0 ||
1227 		    llc->llc_snap.org_code[2] != 0)
1228 			return 1;
1229 
1230 		ether_type = llc->llc_snap.ether_type;
1231 	} else {
1232 		eh = mtod(m, struct ether_header *);
1233 		ether_type = eh->ether_type;
1234 	}
1235 
1236 	/*
1237 	 * Always promote PAE/EAPOL frames to high priority.
1238 	 */
1239 	if (ether_type == htons(ETHERTYPE_PAE)) {
1240 		/* NB: mark so others don't need to check header */
1241 		m->m_flags |= M_EAPOL;
1242 		ac = WME_AC_VO;
1243 		goto done;
1244 	}
1245 	/*
1246 	 * Non-qos traffic goes to BE.
1247 	 */
1248 	if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
1249 		ac = WME_AC_BE;
1250 		goto done;
1251 	}
1252 
1253 	/*
1254 	 * If node has a vlan tag then all traffic
1255 	 * to it must have a matching tag.
1256 	 */
1257 	v_wme_ac = 0;
1258 	if (ni->ni_vlan != 0) {
1259 		 if ((m->m_flags & M_VLANTAG) == 0) {
1260 			IEEE80211_NODE_STAT(ni, tx_novlantag);
1261 			return 1;
1262 		}
1263 		if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
1264 		    EVL_VLANOFTAG(ni->ni_vlan)) {
1265 			IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
1266 			return 1;
1267 		}
1268 		/* map vlan priority to AC */
1269 		v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
1270 	}
1271 
1272 	/* XXX m_copydata may be too slow for fast path */
1273 #ifdef INET
1274 	if (eh && eh->ether_type == htons(ETHERTYPE_IP)) {
1275 		uint8_t tos;
1276 		/*
1277 		 * IP frame, map the DSCP bits from the TOS field.
1278 		 */
1279 		/* NB: ip header may not be in first mbuf */
1280 		m_copydata(m, sizeof(struct ether_header) +
1281 		    offsetof(struct ip, ip_tos), sizeof(tos), &tos);
1282 		tos >>= 5;		/* NB: ECN + low 3 bits of DSCP */
1283 		d_wme_ac = TID_TO_WME_AC(tos);
1284 	} else {
1285 #endif /* INET */
1286 #ifdef INET6
1287 	if (eh && eh->ether_type == htons(ETHERTYPE_IPV6)) {
1288 		uint32_t flow;
1289 		uint8_t tos;
1290 		/*
1291 		 * IPv6 frame, map the DSCP bits from the traffic class field.
1292 		 */
1293 		m_copydata(m, sizeof(struct ether_header) +
1294 		    offsetof(struct ip6_hdr, ip6_flow), sizeof(flow),
1295 		    (caddr_t) &flow);
1296 		tos = (uint8_t)(ntohl(flow) >> 20);
1297 		tos >>= 5;		/* NB: ECN + low 3 bits of DSCP */
1298 		d_wme_ac = TID_TO_WME_AC(tos);
1299 	} else {
1300 #endif /* INET6 */
1301 		d_wme_ac = WME_AC_BE;
1302 #ifdef INET6
1303 	}
1304 #endif
1305 #ifdef INET
1306 	}
1307 #endif
1308 	/*
1309 	 * Use highest priority AC.
1310 	 */
1311 	if (v_wme_ac > d_wme_ac)
1312 		ac = v_wme_ac;
1313 	else
1314 		ac = d_wme_ac;
1315 
1316 	/*
1317 	 * Apply ACM policy.
1318 	 */
1319 	if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
1320 		static const int acmap[4] = {
1321 			WME_AC_BK,	/* WME_AC_BE */
1322 			WME_AC_BK,	/* WME_AC_BK */
1323 			WME_AC_BE,	/* WME_AC_VI */
1324 			WME_AC_VI,	/* WME_AC_VO */
1325 		};
1326 		struct ieee80211com *ic = ni->ni_ic;
1327 
1328 		while (ac != WME_AC_BK &&
1329 		    ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
1330 			ac = acmap[ac];
1331 	}
1332 done:
1333 	M_WME_SETAC(m, ac);
1334 	return 0;
1335 }
1336 
1337 /*
1338  * Insure there is sufficient contiguous space to encapsulate the
1339  * 802.11 data frame.  If room isn't already there, arrange for it.
1340  * Drivers and cipher modules assume we have done the necessary work
1341  * and fail rudely if they don't find the space they need.
1342  */
1343 struct mbuf *
1344 ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
1345 	struct ieee80211_key *key, struct mbuf *m)
1346 {
1347 #define	TO_BE_RECLAIMED	(sizeof(struct ether_header) - sizeof(struct llc))
1348 	int needed_space = vap->iv_ic->ic_headroom + hdrsize;
1349 
1350 	if (key != NULL) {
1351 		/* XXX belongs in crypto code? */
1352 		needed_space += key->wk_cipher->ic_header;
1353 		/* XXX frags */
1354 		/*
1355 		 * When crypto is being done in the host we must insure
1356 		 * the data are writable for the cipher routines; clone
1357 		 * a writable mbuf chain.
1358 		 * XXX handle SWMIC specially
1359 		 */
1360 		if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
1361 			m = m_unshare(m, M_NOWAIT);
1362 			if (m == NULL) {
1363 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
1364 				    "%s: cannot get writable mbuf\n", __func__);
1365 				vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
1366 				return NULL;
1367 			}
1368 		}
1369 	}
1370 	/*
1371 	 * We know we are called just before stripping an Ethernet
1372 	 * header and prepending an LLC header.  This means we know
1373 	 * there will be
1374 	 *	sizeof(struct ether_header) - sizeof(struct llc)
1375 	 * bytes recovered to which we need additional space for the
1376 	 * 802.11 header and any crypto header.
1377 	 */
1378 	/* XXX check trailing space and copy instead? */
1379 	if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
1380 		struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
1381 		if (n == NULL) {
1382 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
1383 			    "%s: cannot expand storage\n", __func__);
1384 			vap->iv_stats.is_tx_nobuf++;
1385 			m_freem(m);
1386 			return NULL;
1387 		}
1388 		KASSERT(needed_space <= MHLEN,
1389 		    ("not enough room, need %u got %d\n", needed_space, MHLEN));
1390 		/*
1391 		 * Setup new mbuf to have leading space to prepend the
1392 		 * 802.11 header and any crypto header bits that are
1393 		 * required (the latter are added when the driver calls
1394 		 * back to ieee80211_crypto_encap to do crypto encapsulation).
1395 		 */
1396 		/* NB: must be first 'cuz it clobbers m_data */
1397 		m_move_pkthdr(n, m);
1398 		n->m_len = 0;			/* NB: m_gethdr does not set */
1399 		n->m_data += needed_space;
1400 		/*
1401 		 * Pull up Ethernet header to create the expected layout.
1402 		 * We could use m_pullup but that's overkill (i.e. we don't
1403 		 * need the actual data) and it cannot fail so do it inline
1404 		 * for speed.
1405 		 */
1406 		/* NB: struct ether_header is known to be contiguous */
1407 		n->m_len += sizeof(struct ether_header);
1408 		m->m_len -= sizeof(struct ether_header);
1409 		m->m_data += sizeof(struct ether_header);
1410 		/*
1411 		 * Replace the head of the chain.
1412 		 */
1413 		n->m_next = m;
1414 		m = n;
1415 	}
1416 	return m;
1417 #undef TO_BE_RECLAIMED
1418 }
1419 
1420 /*
1421  * Return the transmit key to use in sending a unicast frame.
1422  * If a unicast key is set we use that.  When no unicast key is set
1423  * we fall back to the default transmit key.
1424  */
1425 static __inline struct ieee80211_key *
1426 ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
1427 	struct ieee80211_node *ni)
1428 {
1429 	if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
1430 		if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1431 		    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1432 			return NULL;
1433 		return &vap->iv_nw_keys[vap->iv_def_txkey];
1434 	} else {
1435 		return &ni->ni_ucastkey;
1436 	}
1437 }
1438 
1439 /*
1440  * Return the transmit key to use in sending a multicast frame.
1441  * Multicast traffic always uses the group key which is installed as
1442  * the default tx key.
1443  */
1444 static __inline struct ieee80211_key *
1445 ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
1446 	struct ieee80211_node *ni)
1447 {
1448 	if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1449 	    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1450 		return NULL;
1451 	return &vap->iv_nw_keys[vap->iv_def_txkey];
1452 }
1453 
1454 /*
1455  * Encapsulate an outbound data frame.  The mbuf chain is updated.
1456  * If an error is encountered NULL is returned.  The caller is required
1457  * to provide a node reference and pullup the ethernet header in the
1458  * first mbuf.
1459  *
1460  * NB: Packet is assumed to be processed by ieee80211_classify which
1461  *     marked EAPOL frames w/ M_EAPOL.
1462  */
1463 struct mbuf *
1464 ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
1465     struct mbuf *m)
1466 {
1467 #define	WH4(wh)	((struct ieee80211_frame_addr4 *)(wh))
1468 #define MC01(mc)	((struct ieee80211_meshcntl_ae01 *)mc)
1469 	struct ieee80211com *ic = ni->ni_ic;
1470 #ifdef IEEE80211_SUPPORT_MESH
1471 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1472 	struct ieee80211_meshcntl_ae10 *mc;
1473 	struct ieee80211_mesh_route *rt = NULL;
1474 	int dir = -1;
1475 #endif
1476 	struct ether_header eh;
1477 	struct ieee80211_frame *wh;
1478 	struct ieee80211_key *key;
1479 	struct llc *llc;
1480 	int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr, is_mcast;
1481 	ieee80211_seq seqno;
1482 	int meshhdrsize, meshae;
1483 	uint8_t *qos;
1484 	int is_amsdu = 0;
1485 
1486 	IEEE80211_TX_LOCK_ASSERT(ic);
1487 
1488 	is_mcast = !! (m->m_flags & (M_MCAST | M_BCAST));
1489 
1490 	/*
1491 	 * Copy existing Ethernet header to a safe place.  The
1492 	 * rest of the code assumes it's ok to strip it when
1493 	 * reorganizing state for the final encapsulation.
1494 	 */
1495 	KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
1496 	ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
1497 
1498 	/*
1499 	 * Insure space for additional headers.  First identify
1500 	 * transmit key to use in calculating any buffer adjustments
1501 	 * required.  This is also used below to do privacy
1502 	 * encapsulation work.  Then calculate the 802.11 header
1503 	 * size and any padding required by the driver.
1504 	 *
1505 	 * Note key may be NULL if we fall back to the default
1506 	 * transmit key and that is not set.  In that case the
1507 	 * buffer may not be expanded as needed by the cipher
1508 	 * routines, but they will/should discard it.
1509 	 */
1510 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1511 		if (vap->iv_opmode == IEEE80211_M_STA ||
1512 		    !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
1513 		    (vap->iv_opmode == IEEE80211_M_WDS &&
1514 		     (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY))) {
1515 			key = ieee80211_crypto_getucastkey(vap, ni);
1516 		} else if ((vap->iv_opmode == IEEE80211_M_WDS) &&
1517 		    (! (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY))) {
1518 			/*
1519 			 * Use ucastkey for DWDS transmit nodes, multicast
1520 			 * or otherwise.
1521 			 *
1522 			 * This is required to ensure that multicast frames
1523 			 * from a DWDS AP to a DWDS STA is encrypted with
1524 			 * a key that can actually work.
1525 			 *
1526 			 * There's no default key for multicast traffic
1527 			 * on a DWDS WDS VAP node (note NOT the DWDS enabled
1528 			 * AP VAP, the dynamically created per-STA WDS node)
1529 			 * so encap fails and transmit fails.
1530 			 */
1531 			key = ieee80211_crypto_getucastkey(vap, ni);
1532 		} else {
1533 			key = ieee80211_crypto_getmcastkey(vap, ni);
1534 		}
1535 		if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
1536 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
1537 			    eh.ether_dhost,
1538 			    "no default transmit key (%s) deftxkey %u",
1539 			    __func__, vap->iv_def_txkey);
1540 			vap->iv_stats.is_tx_nodefkey++;
1541 			goto bad;
1542 		}
1543 	} else
1544 		key = NULL;
1545 	/*
1546 	 * XXX Some ap's don't handle QoS-encapsulated EAPOL
1547 	 * frames so suppress use.  This may be an issue if other
1548 	 * ap's require all data frames to be QoS-encapsulated
1549 	 * once negotiated in which case we'll need to make this
1550 	 * configurable.
1551 	 *
1552 	 * Don't send multicast QoS frames.
1553 	 * Technically multicast frames can be QoS if all stations in the
1554 	 * BSS are also QoS.
1555 	 *
1556 	 * NB: mesh data frames are QoS, including multicast frames.
1557 	 */
1558 	addqos =
1559 	    (((is_mcast == 0) && (ni->ni_flags &
1560 	     (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))) ||
1561 	    (vap->iv_opmode == IEEE80211_M_MBSS)) &&
1562 	    (m->m_flags & M_EAPOL) == 0;
1563 
1564 	if (addqos)
1565 		hdrsize = sizeof(struct ieee80211_qosframe);
1566 	else
1567 		hdrsize = sizeof(struct ieee80211_frame);
1568 #ifdef IEEE80211_SUPPORT_MESH
1569 	if (vap->iv_opmode == IEEE80211_M_MBSS) {
1570 		/*
1571 		 * Mesh data frames are encapsulated according to the
1572 		 * rules of Section 11B.8.5 (p.139 of D3.0 spec).
1573 		 * o Group Addressed data (aka multicast) originating
1574 		 *   at the local sta are sent w/ 3-address format and
1575 		 *   address extension mode 00
1576 		 * o Individually Addressed data (aka unicast) originating
1577 		 *   at the local sta are sent w/ 4-address format and
1578 		 *   address extension mode 00
1579 		 * o Group Addressed data forwarded from a non-mesh sta are
1580 		 *   sent w/ 3-address format and address extension mode 01
1581 		 * o Individually Address data from another sta are sent
1582 		 *   w/ 4-address format and address extension mode 10
1583 		 */
1584 		is4addr = 0;		/* NB: don't use, disable */
1585 		if (!IEEE80211_IS_MULTICAST(eh.ether_dhost)) {
1586 			rt = ieee80211_mesh_rt_find(vap, eh.ether_dhost);
1587 			KASSERT(rt != NULL, ("route is NULL"));
1588 			dir = IEEE80211_FC1_DIR_DSTODS;
1589 			hdrsize += IEEE80211_ADDR_LEN;
1590 			if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
1591 				if (IEEE80211_ADDR_EQ(rt->rt_mesh_gate,
1592 				    vap->iv_myaddr)) {
1593 					IEEE80211_NOTE_MAC(vap,
1594 					    IEEE80211_MSG_MESH,
1595 					    eh.ether_dhost,
1596 					    "%s", "trying to send to ourself");
1597 					goto bad;
1598 				}
1599 				meshae = IEEE80211_MESH_AE_10;
1600 				meshhdrsize =
1601 				    sizeof(struct ieee80211_meshcntl_ae10);
1602 			} else {
1603 				meshae = IEEE80211_MESH_AE_00;
1604 				meshhdrsize =
1605 				    sizeof(struct ieee80211_meshcntl);
1606 			}
1607 		} else {
1608 			dir = IEEE80211_FC1_DIR_FROMDS;
1609 			if (!IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) {
1610 				/* proxy group */
1611 				meshae = IEEE80211_MESH_AE_01;
1612 				meshhdrsize =
1613 				    sizeof(struct ieee80211_meshcntl_ae01);
1614 			} else {
1615 				/* group */
1616 				meshae = IEEE80211_MESH_AE_00;
1617 				meshhdrsize = sizeof(struct ieee80211_meshcntl);
1618 			}
1619 		}
1620 	} else {
1621 #endif
1622 		/*
1623 		 * 4-address frames need to be generated for:
1624 		 * o packets sent through a WDS vap (IEEE80211_M_WDS)
1625 		 * o packets sent through a vap marked for relaying
1626 		 *   (e.g. a station operating with dynamic WDS)
1627 		 */
1628 		is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
1629 		    ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
1630 		     !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
1631 		if (is4addr)
1632 			hdrsize += IEEE80211_ADDR_LEN;
1633 		meshhdrsize = meshae = 0;
1634 #ifdef IEEE80211_SUPPORT_MESH
1635 	}
1636 #endif
1637 	/*
1638 	 * Honor driver DATAPAD requirement.
1639 	 */
1640 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
1641 		hdrspace = roundup(hdrsize, sizeof(uint32_t));
1642 	else
1643 		hdrspace = hdrsize;
1644 
1645 	if (__predict_true((m->m_flags & M_FF) == 0)) {
1646 		/*
1647 		 * Normal frame.
1648 		 */
1649 		m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m);
1650 		if (m == NULL) {
1651 			/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
1652 			goto bad;
1653 		}
1654 		/* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
1655 		m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
1656 		llc = mtod(m, struct llc *);
1657 		llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1658 		llc->llc_control = LLC_UI;
1659 		llc->llc_snap.org_code[0] = 0;
1660 		llc->llc_snap.org_code[1] = 0;
1661 		llc->llc_snap.org_code[2] = 0;
1662 		llc->llc_snap.ether_type = eh.ether_type;
1663 	} else {
1664 #ifdef IEEE80211_SUPPORT_SUPERG
1665 		/*
1666 		 * Aggregated frame.  Check if it's for AMSDU or FF.
1667 		 *
1668 		 * XXX TODO: IEEE80211_NODE_AMSDU* isn't implemented
1669 		 * anywhere for some reason.  But, since 11n requires
1670 		 * AMSDU RX, we can just assume "11n" == "AMSDU".
1671 		 */
1672 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, "%s: called; M_FF\n", __func__);
1673 		if (ieee80211_amsdu_tx_ok(ni)) {
1674 			m = ieee80211_amsdu_encap(vap, m, hdrspace + meshhdrsize, key);
1675 			is_amsdu = 1;
1676 		} else {
1677 			m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key);
1678 		}
1679 		if (m == NULL)
1680 #endif
1681 			goto bad;
1682 	}
1683 	datalen = m->m_pkthdr.len;		/* NB: w/o 802.11 header */
1684 
1685 	M_PREPEND(m, hdrspace + meshhdrsize, M_NOWAIT);
1686 	if (m == NULL) {
1687 		vap->iv_stats.is_tx_nobuf++;
1688 		goto bad;
1689 	}
1690 	wh = mtod(m, struct ieee80211_frame *);
1691 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1692 	*(uint16_t *)wh->i_dur = 0;
1693 	qos = NULL;	/* NB: quiet compiler */
1694 	if (is4addr) {
1695 		wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1696 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1697 		IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1698 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1699 		IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1700 	} else switch (vap->iv_opmode) {
1701 	case IEEE80211_M_STA:
1702 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1703 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
1704 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1705 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1706 		break;
1707 	case IEEE80211_M_IBSS:
1708 	case IEEE80211_M_AHDEMO:
1709 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1710 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1711 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1712 		/*
1713 		 * NB: always use the bssid from iv_bss as the
1714 		 *     neighbor's may be stale after an ibss merge
1715 		 */
1716 		IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
1717 		break;
1718 	case IEEE80211_M_HOSTAP:
1719 		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1720 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1721 		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
1722 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1723 		break;
1724 #ifdef IEEE80211_SUPPORT_MESH
1725 	case IEEE80211_M_MBSS:
1726 		/* NB: offset by hdrspace to deal with DATAPAD */
1727 		mc = (struct ieee80211_meshcntl_ae10 *)
1728 		     (mtod(m, uint8_t *) + hdrspace);
1729 		wh->i_fc[1] = dir;
1730 		switch (meshae) {
1731 		case IEEE80211_MESH_AE_00:	/* no proxy */
1732 			mc->mc_flags = 0;
1733 			if (dir == IEEE80211_FC1_DIR_DSTODS) { /* ucast */
1734 				IEEE80211_ADDR_COPY(wh->i_addr1,
1735 				    ni->ni_macaddr);
1736 				IEEE80211_ADDR_COPY(wh->i_addr2,
1737 				    vap->iv_myaddr);
1738 				IEEE80211_ADDR_COPY(wh->i_addr3,
1739 				    eh.ether_dhost);
1740 				IEEE80211_ADDR_COPY(WH4(wh)->i_addr4,
1741 				    eh.ether_shost);
1742 				qos =((struct ieee80211_qosframe_addr4 *)
1743 				    wh)->i_qos;
1744 			} else if (dir == IEEE80211_FC1_DIR_FROMDS) {
1745 				 /* mcast */
1746 				IEEE80211_ADDR_COPY(wh->i_addr1,
1747 				    eh.ether_dhost);
1748 				IEEE80211_ADDR_COPY(wh->i_addr2,
1749 				    vap->iv_myaddr);
1750 				IEEE80211_ADDR_COPY(wh->i_addr3,
1751 				    eh.ether_shost);
1752 				qos = ((struct ieee80211_qosframe *)
1753 				    wh)->i_qos;
1754 			}
1755 			break;
1756 		case IEEE80211_MESH_AE_01:	/* mcast, proxy */
1757 			wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1758 			IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1759 			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1760 			IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr);
1761 			mc->mc_flags = 1;
1762 			IEEE80211_ADDR_COPY(MC01(mc)->mc_addr4,
1763 			    eh.ether_shost);
1764 			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1765 			break;
1766 		case IEEE80211_MESH_AE_10:	/* ucast, proxy */
1767 			KASSERT(rt != NULL, ("route is NULL"));
1768 			IEEE80211_ADDR_COPY(wh->i_addr1, rt->rt_nexthop);
1769 			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1770 			IEEE80211_ADDR_COPY(wh->i_addr3, rt->rt_mesh_gate);
1771 			IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr);
1772 			mc->mc_flags = IEEE80211_MESH_AE_10;
1773 			IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_dhost);
1774 			IEEE80211_ADDR_COPY(mc->mc_addr6, eh.ether_shost);
1775 			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1776 			break;
1777 		default:
1778 			KASSERT(0, ("meshae %d", meshae));
1779 			break;
1780 		}
1781 		mc->mc_ttl = ms->ms_ttl;
1782 		ms->ms_seq++;
1783 		le32enc(mc->mc_seq, ms->ms_seq);
1784 		break;
1785 #endif
1786 	case IEEE80211_M_WDS:		/* NB: is4addr should always be true */
1787 	default:
1788 		goto bad;
1789 	}
1790 	if (m->m_flags & M_MORE_DATA)
1791 		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
1792 	if (addqos) {
1793 		int ac, tid;
1794 
1795 		if (is4addr) {
1796 			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1797 		/* NB: mesh case handled earlier */
1798 		} else if (vap->iv_opmode != IEEE80211_M_MBSS)
1799 			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1800 		ac = M_WME_GETAC(m);
1801 		/* map from access class/queue to 11e header priorty value */
1802 		tid = WME_AC_TO_TID(ac);
1803 		qos[0] = tid & IEEE80211_QOS_TID;
1804 		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
1805 			qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1806 #ifdef IEEE80211_SUPPORT_MESH
1807 		if (vap->iv_opmode == IEEE80211_M_MBSS)
1808 			qos[1] = IEEE80211_QOS_MC;
1809 		else
1810 #endif
1811 			qos[1] = 0;
1812 		wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
1813 
1814 		/*
1815 		 * If this is an A-MSDU then ensure we set the
1816 		 * relevant field.
1817 		 */
1818 		if (is_amsdu)
1819 			qos[0] |= IEEE80211_QOS_AMSDU;
1820 
1821 		/*
1822 		 * XXX TODO TX lock is needed for atomic updates of sequence
1823 		 * numbers.  If the driver does it, then don't do it here;
1824 		 * and we don't need the TX lock held.
1825 		 */
1826 		if ((m->m_flags & M_AMPDU_MPDU) == 0) {
1827 			/*
1828 			 * 802.11-2012 9.3.2.10 -
1829 			 *
1830 			 * If this is a multicast frame then we need
1831 			 * to ensure that the sequence number comes from
1832 			 * a separate seqno space and not the TID space.
1833 			 *
1834 			 * Otherwise multicast frames may actually cause
1835 			 * holes in the TX blockack window space and
1836 			 * upset various things.
1837 			 */
1838 			if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1839 				seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
1840 			else
1841 				seqno = ni->ni_txseqs[tid]++;
1842 
1843 			/*
1844 			 * NB: don't assign a sequence # to potential
1845 			 * aggregates; we expect this happens at the
1846 			 * point the frame comes off any aggregation q
1847 			 * as otherwise we may introduce holes in the
1848 			 * BA sequence space and/or make window accouting
1849 			 * more difficult.
1850 			 *
1851 			 * XXX may want to control this with a driver
1852 			 * capability; this may also change when we pull
1853 			 * aggregation up into net80211
1854 			 */
1855 			*(uint16_t *)wh->i_seq =
1856 			    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1857 			M_SEQNO_SET(m, seqno);
1858 		} else {
1859 			/* NB: zero out i_seq field (for s/w encryption etc) */
1860 			*(uint16_t *)wh->i_seq = 0;
1861 		}
1862 	} else {
1863 		/*
1864 		 * XXX TODO TX lock is needed for atomic updates of sequence
1865 		 * numbers.  If the driver does it, then don't do it here;
1866 		 * and we don't need the TX lock held.
1867 		 */
1868 		seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
1869 		*(uint16_t *)wh->i_seq =
1870 		    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1871 		M_SEQNO_SET(m, seqno);
1872 
1873 		/*
1874 		 * XXX TODO: we shouldn't allow EAPOL, etc that would
1875 		 * be forced to be non-QoS traffic to be A-MSDU encapsulated.
1876 		 */
1877 		if (is_amsdu)
1878 			printf("%s: XXX ERROR: is_amsdu set; not QoS!\n",
1879 			    __func__);
1880 	}
1881 
1882 	/*
1883 	 * Check if xmit fragmentation is required.
1884 	 *
1885 	 * If the hardware does fragmentation offload, then don't bother
1886 	 * doing it here.
1887 	 */
1888 	if (IEEE80211_CONF_FRAG_OFFLOAD(ic))
1889 		txfrag = 0;
1890 	else
1891 		txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
1892 		    !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1893 		    (vap->iv_caps & IEEE80211_C_TXFRAG) &&
1894 		    (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
1895 
1896 	if (key != NULL) {
1897 		/*
1898 		 * IEEE 802.1X: send EAPOL frames always in the clear.
1899 		 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
1900 		 */
1901 		if ((m->m_flags & M_EAPOL) == 0 ||
1902 		    ((vap->iv_flags & IEEE80211_F_WPA) &&
1903 		     (vap->iv_opmode == IEEE80211_M_STA ?
1904 		      !IEEE80211_KEY_UNDEFINED(key) :
1905 		      !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
1906 			wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
1907 			if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1908 				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1909 				    eh.ether_dhost,
1910 				    "%s", "enmic failed, discard frame");
1911 				vap->iv_stats.is_crypto_enmicfail++;
1912 				goto bad;
1913 			}
1914 		}
1915 	}
1916 	if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1917 	    key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
1918 		goto bad;
1919 
1920 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
1921 
1922 	IEEE80211_NODE_STAT(ni, tx_data);
1923 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1924 		IEEE80211_NODE_STAT(ni, tx_mcast);
1925 		m->m_flags |= M_MCAST;
1926 	} else
1927 		IEEE80211_NODE_STAT(ni, tx_ucast);
1928 	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
1929 
1930 	return m;
1931 bad:
1932 	if (m != NULL)
1933 		m_freem(m);
1934 	return NULL;
1935 #undef WH4
1936 #undef MC01
1937 }
1938 
1939 void
1940 ieee80211_free_mbuf(struct mbuf *m)
1941 {
1942 	struct mbuf *next;
1943 
1944 	if (m == NULL)
1945 		return;
1946 
1947 	do {
1948 		next = m->m_nextpkt;
1949 		m->m_nextpkt = NULL;
1950 		m_freem(m);
1951 	} while ((m = next) != NULL);
1952 }
1953 
1954 /*
1955  * Fragment the frame according to the specified mtu.
1956  * The size of the 802.11 header (w/o padding) is provided
1957  * so we don't need to recalculate it.  We create a new
1958  * mbuf for each fragment and chain it through m_nextpkt;
1959  * we might be able to optimize this by reusing the original
1960  * packet's mbufs but that is significantly more complicated.
1961  */
1962 static int
1963 ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
1964 	u_int hdrsize, u_int ciphdrsize, u_int mtu)
1965 {
1966 	struct ieee80211com *ic = vap->iv_ic;
1967 	struct ieee80211_frame *wh, *whf;
1968 	struct mbuf *m, *prev;
1969 	u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
1970 	u_int hdrspace;
1971 
1972 	KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
1973 	KASSERT(m0->m_pkthdr.len > mtu,
1974 		("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
1975 
1976 	/*
1977 	 * Honor driver DATAPAD requirement.
1978 	 */
1979 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
1980 		hdrspace = roundup(hdrsize, sizeof(uint32_t));
1981 	else
1982 		hdrspace = hdrsize;
1983 
1984 	wh = mtod(m0, struct ieee80211_frame *);
1985 	/* NB: mark the first frag; it will be propagated below */
1986 	wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
1987 	totalhdrsize = hdrspace + ciphdrsize;
1988 	fragno = 1;
1989 	off = mtu - ciphdrsize;
1990 	remainder = m0->m_pkthdr.len - off;
1991 	prev = m0;
1992 	do {
1993 		fragsize = MIN(totalhdrsize + remainder, mtu);
1994 		m = m_get2(fragsize, M_NOWAIT, MT_DATA, M_PKTHDR);
1995 		if (m == NULL)
1996 			goto bad;
1997 		/* leave room to prepend any cipher header */
1998 		m_align(m, fragsize - ciphdrsize);
1999 
2000 		/*
2001 		 * Form the header in the fragment.  Note that since
2002 		 * we mark the first fragment with the MORE_FRAG bit
2003 		 * it automatically is propagated to each fragment; we
2004 		 * need only clear it on the last fragment (done below).
2005 		 * NB: frag 1+ dont have Mesh Control field present.
2006 		 */
2007 		whf = mtod(m, struct ieee80211_frame *);
2008 		memcpy(whf, wh, hdrsize);
2009 #ifdef IEEE80211_SUPPORT_MESH
2010 		if (vap->iv_opmode == IEEE80211_M_MBSS)
2011 			ieee80211_getqos(wh)[1] &= ~IEEE80211_QOS_MC;
2012 #endif
2013 		*(uint16_t *)&whf->i_seq[0] |= htole16(
2014 			(fragno & IEEE80211_SEQ_FRAG_MASK) <<
2015 				IEEE80211_SEQ_FRAG_SHIFT);
2016 		fragno++;
2017 
2018 		payload = fragsize - totalhdrsize;
2019 		/* NB: destination is known to be contiguous */
2020 
2021 		m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrspace);
2022 		m->m_len = hdrspace + payload;
2023 		m->m_pkthdr.len = hdrspace + payload;
2024 		m->m_flags |= M_FRAG;
2025 
2026 		/* chain up the fragment */
2027 		prev->m_nextpkt = m;
2028 		prev = m;
2029 
2030 		/* deduct fragment just formed */
2031 		remainder -= payload;
2032 		off += payload;
2033 	} while (remainder != 0);
2034 
2035 	/* set the last fragment */
2036 	m->m_flags |= M_LASTFRAG;
2037 	whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
2038 
2039 	/* strip first mbuf now that everything has been copied */
2040 	m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
2041 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
2042 
2043 	vap->iv_stats.is_tx_fragframes++;
2044 	vap->iv_stats.is_tx_frags += fragno-1;
2045 
2046 	return 1;
2047 bad:
2048 	/* reclaim fragments but leave original frame for caller to free */
2049 	ieee80211_free_mbuf(m0->m_nextpkt);
2050 	m0->m_nextpkt = NULL;
2051 	return 0;
2052 }
2053 
2054 /*
2055  * Add a supported rates element id to a frame.
2056  */
2057 uint8_t *
2058 ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
2059 {
2060 	int nrates;
2061 
2062 	*frm++ = IEEE80211_ELEMID_RATES;
2063 	nrates = rs->rs_nrates;
2064 	if (nrates > IEEE80211_RATE_SIZE)
2065 		nrates = IEEE80211_RATE_SIZE;
2066 	*frm++ = nrates;
2067 	memcpy(frm, rs->rs_rates, nrates);
2068 	return frm + nrates;
2069 }
2070 
2071 /*
2072  * Add an extended supported rates element id to a frame.
2073  */
2074 uint8_t *
2075 ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
2076 {
2077 	/*
2078 	 * Add an extended supported rates element if operating in 11g mode.
2079 	 */
2080 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
2081 		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
2082 		*frm++ = IEEE80211_ELEMID_XRATES;
2083 		*frm++ = nrates;
2084 		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
2085 		frm += nrates;
2086 	}
2087 	return frm;
2088 }
2089 
2090 /*
2091  * Add an ssid element to a frame.
2092  */
2093 uint8_t *
2094 ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
2095 {
2096 	*frm++ = IEEE80211_ELEMID_SSID;
2097 	*frm++ = len;
2098 	memcpy(frm, ssid, len);
2099 	return frm + len;
2100 }
2101 
2102 /*
2103  * Add an erp element to a frame.
2104  */
2105 static uint8_t *
2106 ieee80211_add_erp(uint8_t *frm, struct ieee80211vap *vap)
2107 {
2108 	struct ieee80211com *ic = vap->iv_ic;
2109 	uint8_t erp;
2110 
2111 	*frm++ = IEEE80211_ELEMID_ERP;
2112 	*frm++ = 1;
2113 	erp = 0;
2114 
2115 	/*
2116 	 * TODO:  This uses the global flags for now because
2117 	 * the per-VAP flags are fine for per-VAP, but don't
2118 	 * take into account which VAPs share the same channel
2119 	 * and which are on different channels.
2120 	 *
2121 	 * ERP and HT/VHT protection mode is a function of
2122 	 * how many stations are on a channel, not specifically
2123 	 * the VAP or global.  But, until we grow that status,
2124 	 * the global flag will have to do.
2125 	 */
2126 	if (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR)
2127 		erp |= IEEE80211_ERP_NON_ERP_PRESENT;
2128 
2129 	/*
2130 	 * TODO: same as above; these should be based not
2131 	 * on the vap or ic flags, but instead on a combination
2132 	 * of per-VAP and channels.
2133 	 */
2134 	if (ic->ic_flags & IEEE80211_F_USEPROT)
2135 		erp |= IEEE80211_ERP_USE_PROTECTION;
2136 	if (ic->ic_flags & IEEE80211_F_USEBARKER)
2137 		erp |= IEEE80211_ERP_LONG_PREAMBLE;
2138 	*frm++ = erp;
2139 	return frm;
2140 }
2141 
2142 /*
2143  * Add a CFParams element to a frame.
2144  */
2145 static uint8_t *
2146 ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
2147 {
2148 #define	ADDSHORT(frm, v) do {	\
2149 	le16enc(frm, v);	\
2150 	frm += 2;		\
2151 } while (0)
2152 	*frm++ = IEEE80211_ELEMID_CFPARMS;
2153 	*frm++ = 6;
2154 	*frm++ = 0;		/* CFP count */
2155 	*frm++ = 2;		/* CFP period */
2156 	ADDSHORT(frm, 0);	/* CFP MaxDuration (TU) */
2157 	ADDSHORT(frm, 0);	/* CFP CurRemaining (TU) */
2158 	return frm;
2159 #undef ADDSHORT
2160 }
2161 
2162 static __inline uint8_t *
2163 add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
2164 {
2165 	memcpy(frm, ie->ie_data, ie->ie_len);
2166 	return frm + ie->ie_len;
2167 }
2168 
2169 static __inline uint8_t *
2170 add_ie(uint8_t *frm, const uint8_t *ie)
2171 {
2172 	memcpy(frm, ie, 2 + ie[1]);
2173 	return frm + 2 + ie[1];
2174 }
2175 
2176 #define	WME_OUI_BYTES		0x00, 0x50, 0xf2
2177 /*
2178  * Add a WME information element to a frame.
2179  */
2180 uint8_t *
2181 ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme,
2182     struct ieee80211_node *ni)
2183 {
2184 	static const uint8_t oui[4] = { WME_OUI_BYTES, WME_OUI_TYPE };
2185 	struct ieee80211vap *vap = ni->ni_vap;
2186 
2187 	*frm++ = IEEE80211_ELEMID_VENDOR;
2188 	*frm++ = sizeof(struct ieee80211_wme_info) - 2;
2189 	memcpy(frm, oui, sizeof(oui));
2190 	frm += sizeof(oui);
2191 	*frm++ = WME_INFO_OUI_SUBTYPE;
2192 	*frm++ = WME_VERSION;
2193 
2194 	/* QoS info field depends upon operating mode */
2195 	switch (vap->iv_opmode) {
2196 	case IEEE80211_M_HOSTAP:
2197 		*frm = wme->wme_bssChanParams.cap_info;
2198 		if (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD)
2199 			*frm |= WME_CAPINFO_UAPSD_EN;
2200 		frm++;
2201 		break;
2202 	case IEEE80211_M_STA:
2203 		/*
2204 		 * NB: UAPSD drivers must set this up in their
2205 		 * VAP creation method.
2206 		 */
2207 		*frm++ = vap->iv_uapsdinfo;
2208 		break;
2209 	default:
2210 		*frm++ = 0;
2211 		break;
2212 	}
2213 
2214 	return frm;
2215 }
2216 
2217 /*
2218  * Add a WME parameters element to a frame.
2219  */
2220 static uint8_t *
2221 ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme,
2222     int uapsd_enable)
2223 {
2224 #define	ADDSHORT(frm, v) do {	\
2225 	le16enc(frm, v);	\
2226 	frm += 2;		\
2227 } while (0)
2228 	/* NB: this works 'cuz a param has an info at the front */
2229 	static const struct ieee80211_wme_info param = {
2230 		.wme_id		= IEEE80211_ELEMID_VENDOR,
2231 		.wme_len	= sizeof(struct ieee80211_wme_param) - 2,
2232 		.wme_oui	= { WME_OUI_BYTES },
2233 		.wme_type	= WME_OUI_TYPE,
2234 		.wme_subtype	= WME_PARAM_OUI_SUBTYPE,
2235 		.wme_version	= WME_VERSION,
2236 	};
2237 	int i;
2238 
2239 	memcpy(frm, &param, sizeof(param));
2240 	frm += __offsetof(struct ieee80211_wme_info, wme_info);
2241 	*frm = wme->wme_bssChanParams.cap_info;	/* AC info */
2242 	if (uapsd_enable)
2243 		*frm |= WME_CAPINFO_UAPSD_EN;
2244 	frm++;
2245 	*frm++ = 0;					/* reserved field */
2246 	/* XXX TODO - U-APSD bits - SP, flags below */
2247 	for (i = 0; i < WME_NUM_AC; i++) {
2248 		const struct wmeParams *ac =
2249 		       &wme->wme_bssChanParams.cap_wmeParams[i];
2250 		*frm++ = _IEEE80211_SHIFTMASK(i, WME_PARAM_ACI)
2251 		       | _IEEE80211_SHIFTMASK(ac->wmep_acm, WME_PARAM_ACM)
2252 		       | _IEEE80211_SHIFTMASK(ac->wmep_aifsn, WME_PARAM_AIFSN)
2253 		       ;
2254 		*frm++ = _IEEE80211_SHIFTMASK(ac->wmep_logcwmax,
2255 			    WME_PARAM_LOGCWMAX)
2256 		       | _IEEE80211_SHIFTMASK(ac->wmep_logcwmin,
2257 			    WME_PARAM_LOGCWMIN)
2258 		       ;
2259 		ADDSHORT(frm, ac->wmep_txopLimit);
2260 	}
2261 	return frm;
2262 #undef ADDSHORT
2263 }
2264 #undef WME_OUI_BYTES
2265 
2266 /*
2267  * Add an 11h Power Constraint element to a frame.
2268  */
2269 static uint8_t *
2270 ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
2271 {
2272 	const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
2273 	/* XXX per-vap tx power limit? */
2274 	int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
2275 
2276 	frm[0] = IEEE80211_ELEMID_PWRCNSTR;
2277 	frm[1] = 1;
2278 	frm[2] = c->ic_maxregpower > limit ?  c->ic_maxregpower - limit : 0;
2279 	return frm + 3;
2280 }
2281 
2282 /*
2283  * Add an 11h Power Capability element to a frame.
2284  */
2285 static uint8_t *
2286 ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
2287 {
2288 	frm[0] = IEEE80211_ELEMID_PWRCAP;
2289 	frm[1] = 2;
2290 	frm[2] = c->ic_minpower;
2291 	frm[3] = c->ic_maxpower;
2292 	return frm + 4;
2293 }
2294 
2295 /*
2296  * Add an 11h Supported Channels element to a frame.
2297  */
2298 static uint8_t *
2299 ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
2300 {
2301 	static const int ielen = 26;
2302 
2303 	frm[0] = IEEE80211_ELEMID_SUPPCHAN;
2304 	frm[1] = ielen;
2305 	/* XXX not correct */
2306 	memcpy(frm+2, ic->ic_chan_avail, ielen);
2307 	return frm + 2 + ielen;
2308 }
2309 
2310 /*
2311  * Add an 11h Quiet time element to a frame.
2312  */
2313 static uint8_t *
2314 ieee80211_add_quiet(uint8_t *frm, struct ieee80211vap *vap, int update)
2315 {
2316 	struct ieee80211_quiet_ie *quiet = (struct ieee80211_quiet_ie *) frm;
2317 
2318 	quiet->quiet_ie = IEEE80211_ELEMID_QUIET;
2319 	quiet->len = 6;
2320 
2321 	/*
2322 	 * Only update every beacon interval - otherwise probe responses
2323 	 * would update the quiet count value.
2324 	 */
2325 	if (update) {
2326 		if (vap->iv_quiet_count_value == 1)
2327 			vap->iv_quiet_count_value = vap->iv_quiet_count;
2328 		else if (vap->iv_quiet_count_value > 1)
2329 			vap->iv_quiet_count_value--;
2330 	}
2331 
2332 	if (vap->iv_quiet_count_value == 0) {
2333 		/* value 0 is reserved as per 802.11h standerd */
2334 		vap->iv_quiet_count_value = 1;
2335 	}
2336 
2337 	quiet->tbttcount = vap->iv_quiet_count_value;
2338 	quiet->period = vap->iv_quiet_period;
2339 	quiet->duration = htole16(vap->iv_quiet_duration);
2340 	quiet->offset = htole16(vap->iv_quiet_offset);
2341 	return frm + sizeof(*quiet);
2342 }
2343 
2344 /*
2345  * Add an 11h Channel Switch Announcement element to a frame.
2346  * Note that we use the per-vap CSA count to adjust the global
2347  * counter so we can use this routine to form probe response
2348  * frames and get the current count.
2349  */
2350 static uint8_t *
2351 ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
2352 {
2353 	struct ieee80211com *ic = vap->iv_ic;
2354 	struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
2355 
2356 	csa->csa_ie = IEEE80211_ELEMID_CSA;
2357 	csa->csa_len = 3;
2358 	csa->csa_mode = 1;		/* XXX force quiet on channel */
2359 	csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
2360 	csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
2361 	return frm + sizeof(*csa);
2362 }
2363 
2364 /*
2365  * Add an 11h country information element to a frame.
2366  */
2367 static uint8_t *
2368 ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
2369 {
2370 
2371 	if (ic->ic_countryie == NULL ||
2372 	    ic->ic_countryie_chan != ic->ic_bsschan) {
2373 		/*
2374 		 * Handle lazy construction of ie.  This is done on
2375 		 * first use and after a channel change that requires
2376 		 * re-calculation.
2377 		 */
2378 		if (ic->ic_countryie != NULL)
2379 			IEEE80211_FREE(ic->ic_countryie, M_80211_NODE_IE);
2380 		ic->ic_countryie = ieee80211_alloc_countryie(ic);
2381 		if (ic->ic_countryie == NULL)
2382 			return frm;
2383 		ic->ic_countryie_chan = ic->ic_bsschan;
2384 	}
2385 	return add_appie(frm, ic->ic_countryie);
2386 }
2387 
2388 uint8_t *
2389 ieee80211_add_wpa(uint8_t *frm, const struct ieee80211vap *vap)
2390 {
2391 	if (vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL)
2392 		return (add_ie(frm, vap->iv_wpa_ie));
2393 	else {
2394 		/* XXX else complain? */
2395 		return (frm);
2396 	}
2397 }
2398 
2399 uint8_t *
2400 ieee80211_add_rsn(uint8_t *frm, const struct ieee80211vap *vap)
2401 {
2402 	if (vap->iv_flags & IEEE80211_F_WPA2 && vap->iv_rsn_ie != NULL)
2403 		return (add_ie(frm, vap->iv_rsn_ie));
2404 	else {
2405 		/* XXX else complain? */
2406 		return (frm);
2407 	}
2408 }
2409 
2410 uint8_t *
2411 ieee80211_add_qos(uint8_t *frm, const struct ieee80211_node *ni)
2412 {
2413 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
2414 		*frm++ = IEEE80211_ELEMID_QOS;
2415 		*frm++ = 1;
2416 		*frm++ = 0;
2417 	}
2418 
2419 	return (frm);
2420 }
2421 
2422 /*
2423  * ieee80211_send_probereq(): send a probe request frame with the specified ssid
2424  * and any optional information element data;  some helper functions as FW based
2425  * HW scans need some of that information passed too.
2426  */
2427 static uint32_t
2428 ieee80211_probereq_ie_len(struct ieee80211vap *vap, struct ieee80211com *ic)
2429 {
2430 	const struct ieee80211_rateset *rs;
2431 
2432 	rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2433 
2434 	/*
2435 	 * prreq frame format
2436 	 *	[tlv] ssid
2437 	 *	[tlv] supported rates
2438 	 *	[tlv] extended supported rates (if needed)
2439 	 *	[tlv] HT cap (optional)
2440 	 *	[tlv] VHT cap (optional)
2441 	 *	[tlv] WPA (optional)
2442 	 *	[tlv] user-specified ie's
2443 	 */
2444 	return ( 2 + IEEE80211_NWID_LEN
2445 	       + 2 + IEEE80211_RATE_SIZE
2446 	       + ((rs->rs_nrates > IEEE80211_RATE_SIZE) ?
2447 	           2 + (rs->rs_nrates - IEEE80211_RATE_SIZE) : 0)
2448 	       + (((vap->iv_opmode == IEEE80211_M_IBSS) &&
2449 		    (vap->iv_flags_ht & IEEE80211_FHT_HT)) ?
2450 	                sizeof(struct ieee80211_ie_htcap) : 0)
2451 #ifdef notyet
2452 	       + sizeof(struct ieee80211_ie_htinfo)	/* XXX not needed? */
2453 	       + sizeof(struct ieee80211_ie_vhtcap)
2454 #endif
2455 	       + ((vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL) ?
2456 	           vap->iv_wpa_ie[1] : 0)
2457 	       + (vap->iv_appie_probereq != NULL ?
2458 		   vap->iv_appie_probereq->ie_len : 0)
2459 	);
2460 }
2461 
2462 int
2463 ieee80211_probereq_ie(struct ieee80211vap *vap, struct ieee80211com *ic,
2464     uint8_t **frmp, uint32_t *frmlen, const uint8_t *ssid, size_t ssidlen,
2465     bool alloc)
2466 {
2467 	const struct ieee80211_rateset *rs;
2468 	uint8_t	*frm;
2469 	uint32_t len;
2470 
2471 	if (!alloc && (frmp == NULL || frmlen == NULL))
2472 		return (EINVAL);
2473 
2474 	len = ieee80211_probereq_ie_len(vap, ic);
2475 	if (!alloc && len > *frmlen)
2476 		return (ENOBUFS);
2477 
2478 	/* For HW scans we usually do not pass in the SSID as IE. */
2479 	if (ssidlen == -1)
2480 		len -= (2 + IEEE80211_NWID_LEN);
2481 
2482 	if (alloc) {
2483 		frm = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
2484 		*frmp = frm;
2485 		*frmlen = len;
2486 	} else
2487 		frm = *frmp;
2488 
2489 	if (ssidlen != -1)
2490 		frm = ieee80211_add_ssid(frm, ssid, ssidlen);
2491 	rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2492 	frm = ieee80211_add_rates(frm, rs);
2493 	frm = ieee80211_add_xrates(frm, rs);
2494 
2495 	/*
2496 	 * Note: we can't use bss; we don't have one yet.
2497 	 *
2498 	 * So, we should announce our capabilities
2499 	 * in this channel mode (2g/5g), not the
2500 	 * channel details itself.
2501 	 */
2502 	if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
2503 	    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
2504 		struct ieee80211_channel *c;
2505 
2506 		/*
2507 		 * Get the HT channel that we should try upgrading to.
2508 		 * If we can do 40MHz then this'll upgrade it appropriately.
2509 		 */
2510 		c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
2511 		    vap->iv_flags_ht);
2512 		frm = ieee80211_add_htcap_ch(frm, vap, c);
2513 	}
2514 
2515 	/*
2516 	 * XXX TODO: need to figure out what/how to update the
2517 	 * VHT channel.
2518 	 */
2519 #ifdef notyet
2520 	if (vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
2521 		struct ieee80211_channel *c;
2522 
2523 		c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
2524 		    vap->iv_flags_ht);
2525 		c = ieee80211_vht_adjust_channel(ic, c, vap->iv_flags_vht);
2526 		frm = ieee80211_add_vhtcap_ch(frm, vap, c);
2527 	}
2528 #endif
2529 
2530 	frm = ieee80211_add_wpa(frm, vap);
2531 	if (vap->iv_appie_probereq != NULL)
2532 		frm = add_appie(frm, vap->iv_appie_probereq);
2533 
2534 	if (!alloc) {
2535 		*frmp = frm;
2536 		*frmlen = len;
2537 	}
2538 
2539 	return (0);
2540 }
2541 
2542 int
2543 ieee80211_send_probereq(struct ieee80211_node *ni,
2544 	const uint8_t sa[IEEE80211_ADDR_LEN],
2545 	const uint8_t da[IEEE80211_ADDR_LEN],
2546 	const uint8_t bssid[IEEE80211_ADDR_LEN],
2547 	const uint8_t *ssid, size_t ssidlen)
2548 {
2549 	struct ieee80211vap *vap = ni->ni_vap;
2550 	struct ieee80211com *ic = ni->ni_ic;
2551 	struct ieee80211_node *bss;
2552 	const struct ieee80211_txparam *tp;
2553 	struct ieee80211_bpf_params params;
2554 	struct mbuf *m;
2555 	uint8_t *frm;
2556 	uint32_t frmlen;
2557 	int ret;
2558 
2559 	bss = ieee80211_ref_node(vap->iv_bss);
2560 
2561 	if (vap->iv_state == IEEE80211_S_CAC) {
2562 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
2563 		    "block %s frame in CAC state", "probe request");
2564 		vap->iv_stats.is_tx_badstate++;
2565 		ieee80211_free_node(bss);
2566 		return EIO;		/* XXX */
2567 	}
2568 
2569 	/*
2570 	 * Hold a reference on the node so it doesn't go away until after
2571 	 * the xmit is complete all the way in the driver.  On error we
2572 	 * will remove our reference.
2573 	 */
2574 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2575 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2576 		__func__, __LINE__,
2577 		ni, ether_sprintf(ni->ni_macaddr),
2578 		ieee80211_node_refcnt(ni)+1);
2579 	ieee80211_ref_node(ni);
2580 
2581 	/* See comments above for entire frame format. */
2582 	frmlen = ieee80211_probereq_ie_len(vap, ic);
2583 	m = ieee80211_getmgtframe(&frm,
2584 	    ic->ic_headroom + sizeof(struct ieee80211_frame), frmlen);
2585 	if (m == NULL) {
2586 		vap->iv_stats.is_tx_nobuf++;
2587 		ieee80211_free_node(ni);
2588 		ieee80211_free_node(bss);
2589 		return ENOMEM;
2590 	}
2591 
2592 	ret = ieee80211_probereq_ie(vap, ic, &frm, &frmlen, ssid, ssidlen,
2593 	    false);
2594 	KASSERT(ret == 0,
2595 	    ("%s: ieee80211_probereq_ie failed: %d\n", __func__, ret));
2596 
2597 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2598 	KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
2599 	    ("leading space %zd", M_LEADINGSPACE(m)));
2600 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
2601 	if (m == NULL) {
2602 		/* NB: cannot happen */
2603 		ieee80211_free_node(ni);
2604 		ieee80211_free_node(bss);
2605 		return ENOMEM;
2606 	}
2607 
2608 	IEEE80211_TX_LOCK(ic);
2609 	ieee80211_send_setup(ni, m,
2610 	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
2611 	     IEEE80211_NONQOS_TID, sa, da, bssid);
2612 	/* XXX power management? */
2613 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
2614 
2615 	M_WME_SETAC(m, WME_AC_BE);
2616 
2617 	IEEE80211_NODE_STAT(ni, tx_probereq);
2618 	IEEE80211_NODE_STAT(ni, tx_mgmt);
2619 
2620 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2621 	    "send probe req on channel %u bssid %s sa %6D da %6D ssid \"%.*s\"\n",
2622 	    ieee80211_chan2ieee(ic, ic->ic_curchan),
2623 	    ether_sprintf(bssid),
2624 	    sa, ":",
2625 	    da, ":",
2626 	    ssidlen, ssid);
2627 
2628 	memset(&params, 0, sizeof(params));
2629 	params.ibp_pri = M_WME_GETAC(m);
2630 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2631 	params.ibp_rate0 = tp->mgmtrate;
2632 	if (IEEE80211_IS_MULTICAST(da)) {
2633 		params.ibp_flags |= IEEE80211_BPF_NOACK;
2634 		params.ibp_try0 = 1;
2635 	} else
2636 		params.ibp_try0 = tp->maxretry;
2637 	params.ibp_power = ni->ni_txpower;
2638 	ret = ieee80211_raw_output(vap, ni, m, &params);
2639 	IEEE80211_TX_UNLOCK(ic);
2640 	ieee80211_free_node(bss);
2641 	return (ret);
2642 }
2643 
2644 /*
2645  * Calculate capability information for mgt frames.
2646  */
2647 uint16_t
2648 ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
2649 {
2650 	uint16_t capinfo;
2651 
2652 	KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
2653 
2654 	if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2655 		capinfo = IEEE80211_CAPINFO_ESS;
2656 	else if (vap->iv_opmode == IEEE80211_M_IBSS)
2657 		capinfo = IEEE80211_CAPINFO_IBSS;
2658 	else
2659 		capinfo = 0;
2660 	if (vap->iv_flags & IEEE80211_F_PRIVACY)
2661 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
2662 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) &&
2663 	    IEEE80211_IS_CHAN_2GHZ(chan))
2664 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2665 	if (vap->iv_flags & IEEE80211_F_SHSLOT)
2666 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2667 	if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
2668 		capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2669 	return capinfo;
2670 }
2671 
2672 /*
2673  * Send a management frame.  The node is for the destination (or ic_bss
2674  * when in station mode).  Nodes other than ic_bss have their reference
2675  * count bumped to reflect our use for an indeterminant time.
2676  */
2677 int
2678 ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
2679 {
2680 #define	HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
2681 #define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2682 	struct ieee80211vap *vap = ni->ni_vap;
2683 	struct ieee80211com *ic = ni->ni_ic;
2684 	struct ieee80211_node *bss = vap->iv_bss;
2685 	struct ieee80211_bpf_params params;
2686 	struct mbuf *m;
2687 	uint8_t *frm;
2688 	uint16_t capinfo;
2689 	int has_challenge, is_shared_key, ret, status;
2690 
2691 	KASSERT(ni != NULL, ("null node"));
2692 
2693 	/*
2694 	 * Hold a reference on the node so it doesn't go away until after
2695 	 * the xmit is complete all the way in the driver.  On error we
2696 	 * will remove our reference.
2697 	 */
2698 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2699 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2700 		__func__, __LINE__,
2701 		ni, ether_sprintf(ni->ni_macaddr),
2702 		ieee80211_node_refcnt(ni)+1);
2703 	ieee80211_ref_node(ni);
2704 
2705 	memset(&params, 0, sizeof(params));
2706 	switch (type) {
2707 	case IEEE80211_FC0_SUBTYPE_AUTH:
2708 		status = arg >> 16;
2709 		arg &= 0xffff;
2710 		has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
2711 		    arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
2712 		    ni->ni_challenge != NULL);
2713 
2714 		/*
2715 		 * Deduce whether we're doing open authentication or
2716 		 * shared key authentication.  We do the latter if
2717 		 * we're in the middle of a shared key authentication
2718 		 * handshake or if we're initiating an authentication
2719 		 * request and configured to use shared key.
2720 		 */
2721 		is_shared_key = has_challenge ||
2722 		     arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
2723 		     (arg == IEEE80211_AUTH_SHARED_REQUEST &&
2724 		      bss->ni_authmode == IEEE80211_AUTH_SHARED);
2725 
2726 		m = ieee80211_getmgtframe(&frm,
2727 			  ic->ic_headroom + sizeof(struct ieee80211_frame),
2728 			  3 * sizeof(uint16_t)
2729 			+ (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
2730 				sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0));
2731 		if (m == NULL)
2732 			senderr(ENOMEM, is_tx_nobuf);
2733 
2734 		((uint16_t *)frm)[0] =
2735 		    (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
2736 		                    : htole16(IEEE80211_AUTH_ALG_OPEN);
2737 		((uint16_t *)frm)[1] = htole16(arg);	/* sequence number */
2738 		((uint16_t *)frm)[2] = htole16(status);/* status */
2739 
2740 		if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
2741 			((uint16_t *)frm)[3] =
2742 			    htole16((IEEE80211_CHALLENGE_LEN << 8) |
2743 			    IEEE80211_ELEMID_CHALLENGE);
2744 			memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
2745 			    IEEE80211_CHALLENGE_LEN);
2746 			m->m_pkthdr.len = m->m_len =
2747 				4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
2748 			if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
2749 				IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2750 				    "request encrypt frame (%s)", __func__);
2751 				/* mark frame for encryption */
2752 				params.ibp_flags |= IEEE80211_BPF_CRYPTO;
2753 			}
2754 		} else
2755 			m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
2756 
2757 		/* XXX not right for shared key */
2758 		if (status == IEEE80211_STATUS_SUCCESS)
2759 			IEEE80211_NODE_STAT(ni, tx_auth);
2760 		else
2761 			IEEE80211_NODE_STAT(ni, tx_auth_fail);
2762 
2763 		if (vap->iv_opmode == IEEE80211_M_STA)
2764 			ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2765 				(void *) vap->iv_state);
2766 		break;
2767 
2768 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
2769 		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2770 		    "send station deauthenticate (reason: %d (%s))", arg,
2771 		    ieee80211_reason_to_string(arg));
2772 		m = ieee80211_getmgtframe(&frm,
2773 			ic->ic_headroom + sizeof(struct ieee80211_frame),
2774 			sizeof(uint16_t));
2775 		if (m == NULL)
2776 			senderr(ENOMEM, is_tx_nobuf);
2777 		*(uint16_t *)frm = htole16(arg);	/* reason */
2778 		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2779 
2780 		IEEE80211_NODE_STAT(ni, tx_deauth);
2781 		IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
2782 
2783 		ieee80211_node_unauthorize(ni);		/* port closed */
2784 		break;
2785 
2786 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2787 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2788 		/*
2789 		 * asreq frame format
2790 		 *	[2] capability information
2791 		 *	[2] listen interval
2792 		 *	[6*] current AP address (reassoc only)
2793 		 *	[tlv] ssid
2794 		 *	[tlv] supported rates
2795 		 *	[tlv] extended supported rates
2796 		 *	[4] power capability (optional)
2797 		 *	[28] supported channels (optional)
2798 		 *	[tlv] HT capabilities
2799 		 *	[tlv] VHT capabilities
2800 		 *	[tlv] WME (optional)
2801 		 *	[tlv] Vendor OUI HT capabilities (optional)
2802 		 *	[tlv] Atheros capabilities (if negotiated)
2803 		 *	[tlv] AppIE's (optional)
2804 		 */
2805 		m = ieee80211_getmgtframe(&frm,
2806 			 ic->ic_headroom + sizeof(struct ieee80211_frame),
2807 			 sizeof(uint16_t)
2808 		       + sizeof(uint16_t)
2809 		       + IEEE80211_ADDR_LEN
2810 		       + 2 + IEEE80211_NWID_LEN
2811 		       + 2 + IEEE80211_RATE_SIZE
2812 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2813 		       + 4
2814 		       + 2 + 26
2815 		       + sizeof(struct ieee80211_wme_info)
2816 		       + sizeof(struct ieee80211_ie_htcap)
2817 		       + sizeof(struct ieee80211_ie_vhtcap)
2818 		       + 4 + sizeof(struct ieee80211_ie_htcap)
2819 #ifdef IEEE80211_SUPPORT_SUPERG
2820 		       + sizeof(struct ieee80211_ath_ie)
2821 #endif
2822 		       + (vap->iv_appie_wpa != NULL ?
2823 				vap->iv_appie_wpa->ie_len : 0)
2824 		       + (vap->iv_appie_assocreq != NULL ?
2825 				vap->iv_appie_assocreq->ie_len : 0)
2826 		);
2827 		if (m == NULL)
2828 			senderr(ENOMEM, is_tx_nobuf);
2829 
2830 		KASSERT(vap->iv_opmode == IEEE80211_M_STA,
2831 		    ("wrong mode %u", vap->iv_opmode));
2832 		capinfo = IEEE80211_CAPINFO_ESS;
2833 		if (vap->iv_flags & IEEE80211_F_PRIVACY)
2834 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
2835 		/*
2836 		 * NB: Some 11a AP's reject the request when
2837 		 *     short preamble is set.
2838 		 */
2839 		if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) &&
2840 		    IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2841 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2842 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
2843 		    (ic->ic_caps & IEEE80211_C_SHSLOT))
2844 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2845 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
2846 		    (vap->iv_flags & IEEE80211_F_DOTH))
2847 			capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2848 		*(uint16_t *)frm = htole16(capinfo);
2849 		frm += 2;
2850 
2851 		KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
2852 		*(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
2853 						    bss->ni_intval));
2854 		frm += 2;
2855 
2856 		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2857 			IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
2858 			frm += IEEE80211_ADDR_LEN;
2859 		}
2860 
2861 		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
2862 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
2863 		frm = ieee80211_add_rsn(frm, vap);
2864 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2865 		if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
2866 			frm = ieee80211_add_powercapability(frm,
2867 			    ic->ic_curchan);
2868 			frm = ieee80211_add_supportedchannels(frm, ic);
2869 		}
2870 
2871 		/*
2872 		 * Check the channel - we may be using an 11n NIC with an
2873 		 * 11n capable station, but we're configured to be an 11b
2874 		 * channel.
2875 		 */
2876 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2877 		    IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2878 		    ni->ni_ies.htcap_ie != NULL &&
2879 		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP) {
2880 			frm = ieee80211_add_htcap(frm, ni);
2881 		}
2882 
2883 		if ((vap->iv_flags_vht & IEEE80211_FVHT_VHT) &&
2884 		    IEEE80211_IS_CHAN_VHT(ni->ni_chan) &&
2885 		    ni->ni_ies.vhtcap_ie != NULL &&
2886 		    ni->ni_ies.vhtcap_ie[0] == IEEE80211_ELEMID_VHT_CAP) {
2887 			frm = ieee80211_add_vhtcap(frm, ni);
2888 		}
2889 
2890 		frm = ieee80211_add_wpa(frm, vap);
2891 		if ((ic->ic_flags & IEEE80211_F_WME) &&
2892 		    ni->ni_ies.wme_ie != NULL)
2893 			frm = ieee80211_add_wme_info(frm, &ic->ic_wme, ni);
2894 
2895 		/*
2896 		 * Same deal - only send HT info if we're on an 11n
2897 		 * capable channel.
2898 		 */
2899 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2900 		    IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2901 		    ni->ni_ies.htcap_ie != NULL &&
2902 		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR) {
2903 			frm = ieee80211_add_htcap_vendor(frm, ni);
2904 		}
2905 #ifdef IEEE80211_SUPPORT_SUPERG
2906 		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
2907 			frm = ieee80211_add_ath(frm,
2908 				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2909 				((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2910 				 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2911 				vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2912 		}
2913 #endif /* IEEE80211_SUPPORT_SUPERG */
2914 		if (vap->iv_appie_assocreq != NULL)
2915 			frm = add_appie(frm, vap->iv_appie_assocreq);
2916 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2917 
2918 		ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2919 			(void *) vap->iv_state);
2920 		break;
2921 
2922 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2923 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2924 		/*
2925 		 * asresp frame format
2926 		 *	[2] capability information
2927 		 *	[2] status
2928 		 *	[2] association ID
2929 		 *	[tlv] supported rates
2930 		 *	[tlv] extended supported rates
2931 		 *	[tlv] HT capabilities (standard, if STA enabled)
2932 		 *	[tlv] HT information (standard, if STA enabled)
2933 		 *	[tlv] VHT capabilities (standard, if STA enabled)
2934 		 *	[tlv] VHT information (standard, if STA enabled)
2935 		 *	[tlv] WME (if configured and STA enabled)
2936 		 *	[tlv] HT capabilities (vendor OUI, if STA enabled)
2937 		 *	[tlv] HT information (vendor OUI, if STA enabled)
2938 		 *	[tlv] Atheros capabilities (if STA enabled)
2939 		 *	[tlv] AppIE's (optional)
2940 		 */
2941 		m = ieee80211_getmgtframe(&frm,
2942 			 ic->ic_headroom + sizeof(struct ieee80211_frame),
2943 			 sizeof(uint16_t)
2944 		       + sizeof(uint16_t)
2945 		       + sizeof(uint16_t)
2946 		       + 2 + IEEE80211_RATE_SIZE
2947 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2948 		       + sizeof(struct ieee80211_ie_htcap) + 4
2949 		       + sizeof(struct ieee80211_ie_htinfo) + 4
2950 		       + sizeof(struct ieee80211_ie_vhtcap)
2951 		       + sizeof(struct ieee80211_ie_vht_operation)
2952 		       + sizeof(struct ieee80211_wme_param)
2953 #ifdef IEEE80211_SUPPORT_SUPERG
2954 		       + sizeof(struct ieee80211_ath_ie)
2955 #endif
2956 		       + (vap->iv_appie_assocresp != NULL ?
2957 				vap->iv_appie_assocresp->ie_len : 0)
2958 		);
2959 		if (m == NULL)
2960 			senderr(ENOMEM, is_tx_nobuf);
2961 
2962 		capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2963 		*(uint16_t *)frm = htole16(capinfo);
2964 		frm += 2;
2965 
2966 		*(uint16_t *)frm = htole16(arg);	/* status */
2967 		frm += 2;
2968 
2969 		if (arg == IEEE80211_STATUS_SUCCESS) {
2970 			*(uint16_t *)frm = htole16(ni->ni_associd);
2971 			IEEE80211_NODE_STAT(ni, tx_assoc);
2972 		} else
2973 			IEEE80211_NODE_STAT(ni, tx_assoc_fail);
2974 		frm += 2;
2975 
2976 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
2977 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2978 		/* NB: respond according to what we received */
2979 		if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
2980 			frm = ieee80211_add_htcap(frm, ni);
2981 			frm = ieee80211_add_htinfo(frm, ni);
2982 		}
2983 		if ((vap->iv_flags & IEEE80211_F_WME) &&
2984 		    ni->ni_ies.wme_ie != NULL)
2985 			frm = ieee80211_add_wme_param(frm, &ic->ic_wme,
2986 			    !! (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD));
2987 		if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
2988 			frm = ieee80211_add_htcap_vendor(frm, ni);
2989 			frm = ieee80211_add_htinfo_vendor(frm, ni);
2990 		}
2991 		if (ni->ni_flags & IEEE80211_NODE_VHT) {
2992 			frm = ieee80211_add_vhtcap(frm, ni);
2993 			frm = ieee80211_add_vhtinfo(frm, ni);
2994 		}
2995 #ifdef IEEE80211_SUPPORT_SUPERG
2996 		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
2997 			frm = ieee80211_add_ath(frm,
2998 				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2999 				((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
3000 				 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
3001 				vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
3002 #endif /* IEEE80211_SUPPORT_SUPERG */
3003 		if (vap->iv_appie_assocresp != NULL)
3004 			frm = add_appie(frm, vap->iv_appie_assocresp);
3005 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3006 		break;
3007 
3008 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
3009 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
3010 		    "send station disassociate (reason: %d (%s))", arg,
3011 		    ieee80211_reason_to_string(arg));
3012 		m = ieee80211_getmgtframe(&frm,
3013 			ic->ic_headroom + sizeof(struct ieee80211_frame),
3014 			sizeof(uint16_t));
3015 		if (m == NULL)
3016 			senderr(ENOMEM, is_tx_nobuf);
3017 		*(uint16_t *)frm = htole16(arg);	/* reason */
3018 		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
3019 
3020 		IEEE80211_NODE_STAT(ni, tx_disassoc);
3021 		IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
3022 		break;
3023 
3024 	default:
3025 		IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
3026 		    "invalid mgmt frame type %u", type);
3027 		senderr(EINVAL, is_tx_unknownmgt);
3028 		/* NOTREACHED */
3029 	}
3030 
3031 	/* NB: force non-ProbeResp frames to the highest queue */
3032 	params.ibp_pri = WME_AC_VO;
3033 	params.ibp_rate0 = bss->ni_txparms->mgmtrate;
3034 	/* NB: we know all frames are unicast */
3035 	params.ibp_try0 = bss->ni_txparms->maxretry;
3036 	params.ibp_power = bss->ni_txpower;
3037 	return ieee80211_mgmt_output(ni, m, type, &params);
3038 bad:
3039 	ieee80211_free_node(ni);
3040 	return ret;
3041 #undef senderr
3042 #undef HTFLAGS
3043 }
3044 
3045 /*
3046  * Return an mbuf with a probe response frame in it.
3047  * Space is left to prepend and 802.11 header at the
3048  * front but it's left to the caller to fill in.
3049  */
3050 struct mbuf *
3051 ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
3052 {
3053 	struct ieee80211vap *vap = bss->ni_vap;
3054 	struct ieee80211com *ic = bss->ni_ic;
3055 	const struct ieee80211_rateset *rs;
3056 	struct mbuf *m;
3057 	uint16_t capinfo;
3058 	uint8_t *frm;
3059 
3060 	/*
3061 	 * probe response frame format
3062 	 *	[8] time stamp
3063 	 *	[2] beacon interval
3064 	 *	[2] cabability information
3065 	 *	[tlv] ssid
3066 	 *	[tlv] supported rates
3067 	 *	[tlv] parameter set (FH/DS)
3068 	 *	[tlv] parameter set (IBSS)
3069 	 *	[tlv] country (optional)
3070 	 *	[3] power control (optional)
3071 	 *	[5] channel switch announcement (CSA) (optional)
3072 	 *	[tlv] extended rate phy (ERP)
3073 	 *	[tlv] extended supported rates
3074 	 *	[tlv] RSN (optional)
3075 	 *	[tlv] HT capabilities
3076 	 *	[tlv] HT information
3077 	 *	[tlv] VHT capabilities
3078 	 *	[tlv] VHT information
3079 	 *	[tlv] WPA (optional)
3080 	 *	[tlv] WME (optional)
3081 	 *	[tlv] Vendor OUI HT capabilities (optional)
3082 	 *	[tlv] Vendor OUI HT information (optional)
3083 	 *	[tlv] Atheros capabilities
3084 	 *	[tlv] AppIE's (optional)
3085 	 *	[tlv] Mesh ID (MBSS)
3086 	 *	[tlv] Mesh Conf (MBSS)
3087 	 */
3088 	m = ieee80211_getmgtframe(&frm,
3089 		 ic->ic_headroom + sizeof(struct ieee80211_frame),
3090 		 8
3091 	       + sizeof(uint16_t)
3092 	       + sizeof(uint16_t)
3093 	       + 2 + IEEE80211_NWID_LEN
3094 	       + 2 + IEEE80211_RATE_SIZE
3095 	       + 7	/* max(7,3) */
3096 	       + IEEE80211_COUNTRY_MAX_SIZE
3097 	       + 3
3098 	       + sizeof(struct ieee80211_csa_ie)
3099 	       + sizeof(struct ieee80211_quiet_ie)
3100 	       + 3
3101 	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
3102 	       + sizeof(struct ieee80211_ie_wpa)
3103 	       + sizeof(struct ieee80211_ie_htcap)
3104 	       + sizeof(struct ieee80211_ie_htinfo)
3105 	       + sizeof(struct ieee80211_ie_wpa)
3106 	       + sizeof(struct ieee80211_wme_param)
3107 	       + 4 + sizeof(struct ieee80211_ie_htcap)
3108 	       + 4 + sizeof(struct ieee80211_ie_htinfo)
3109 	       +  sizeof(struct ieee80211_ie_vhtcap)
3110 	       +  sizeof(struct ieee80211_ie_vht_operation)
3111 #ifdef IEEE80211_SUPPORT_SUPERG
3112 	       + sizeof(struct ieee80211_ath_ie)
3113 #endif
3114 #ifdef IEEE80211_SUPPORT_MESH
3115 	       + 2 + IEEE80211_MESHID_LEN
3116 	       + sizeof(struct ieee80211_meshconf_ie)
3117 #endif
3118 	       + (vap->iv_appie_proberesp != NULL ?
3119 			vap->iv_appie_proberesp->ie_len : 0)
3120 	);
3121 	if (m == NULL) {
3122 		vap->iv_stats.is_tx_nobuf++;
3123 		return NULL;
3124 	}
3125 
3126 	memset(frm, 0, 8);	/* timestamp should be filled later */
3127 	frm += 8;
3128 	*(uint16_t *)frm = htole16(bss->ni_intval);
3129 	frm += 2;
3130 	capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
3131 	*(uint16_t *)frm = htole16(capinfo);
3132 	frm += 2;
3133 
3134 	frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
3135 	rs = ieee80211_get_suprates(ic, bss->ni_chan);
3136 	frm = ieee80211_add_rates(frm, rs);
3137 
3138 	if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
3139 		*frm++ = IEEE80211_ELEMID_FHPARMS;
3140 		*frm++ = 5;
3141 		*frm++ = bss->ni_fhdwell & 0x00ff;
3142 		*frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
3143 		*frm++ = IEEE80211_FH_CHANSET(
3144 		    ieee80211_chan2ieee(ic, bss->ni_chan));
3145 		*frm++ = IEEE80211_FH_CHANPAT(
3146 		    ieee80211_chan2ieee(ic, bss->ni_chan));
3147 		*frm++ = bss->ni_fhindex;
3148 	} else {
3149 		*frm++ = IEEE80211_ELEMID_DSPARMS;
3150 		*frm++ = 1;
3151 		*frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
3152 	}
3153 
3154 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
3155 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
3156 		*frm++ = 2;
3157 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
3158 	}
3159 	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
3160 	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
3161 		frm = ieee80211_add_countryie(frm, ic);
3162 	if (vap->iv_flags & IEEE80211_F_DOTH) {
3163 		if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
3164 			frm = ieee80211_add_powerconstraint(frm, vap);
3165 		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
3166 			frm = ieee80211_add_csa(frm, vap);
3167 	}
3168 	if (vap->iv_flags & IEEE80211_F_DOTH) {
3169 		if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
3170 		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
3171 			if (vap->iv_quiet)
3172 				frm = ieee80211_add_quiet(frm, vap, 0);
3173 		}
3174 	}
3175 	if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
3176 		frm = ieee80211_add_erp(frm, vap);
3177 	frm = ieee80211_add_xrates(frm, rs);
3178 	frm = ieee80211_add_rsn(frm, vap);
3179 	/*
3180 	 * NB: legacy 11b clients do not get certain ie's.
3181 	 *     The caller identifies such clients by passing
3182 	 *     a token in legacy to us.  Could expand this to be
3183 	 *     any legacy client for stuff like HT ie's.
3184 	 */
3185 	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
3186 	    legacy != IEEE80211_SEND_LEGACY_11B) {
3187 		frm = ieee80211_add_htcap(frm, bss);
3188 		frm = ieee80211_add_htinfo(frm, bss);
3189 	}
3190 	if (IEEE80211_IS_CHAN_VHT(bss->ni_chan) &&
3191 	    legacy != IEEE80211_SEND_LEGACY_11B) {
3192 		frm = ieee80211_add_vhtcap(frm, bss);
3193 		frm = ieee80211_add_vhtinfo(frm, bss);
3194 	}
3195 	frm = ieee80211_add_wpa(frm, vap);
3196 	if (vap->iv_flags & IEEE80211_F_WME)
3197 		frm = ieee80211_add_wme_param(frm, &ic->ic_wme,
3198 		    !! (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD));
3199 	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
3200 	    (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
3201 	    legacy != IEEE80211_SEND_LEGACY_11B) {
3202 		frm = ieee80211_add_htcap_vendor(frm, bss);
3203 		frm = ieee80211_add_htinfo_vendor(frm, bss);
3204 	}
3205 #ifdef IEEE80211_SUPPORT_SUPERG
3206 	if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
3207 	    legacy != IEEE80211_SEND_LEGACY_11B)
3208 		frm = ieee80211_add_athcaps(frm, bss);
3209 #endif
3210 	if (vap->iv_appie_proberesp != NULL)
3211 		frm = add_appie(frm, vap->iv_appie_proberesp);
3212 #ifdef IEEE80211_SUPPORT_MESH
3213 	if (vap->iv_opmode == IEEE80211_M_MBSS) {
3214 		frm = ieee80211_add_meshid(frm, vap);
3215 		frm = ieee80211_add_meshconf(frm, vap);
3216 	}
3217 #endif
3218 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3219 
3220 	return m;
3221 }
3222 
3223 /*
3224  * Send a probe response frame to the specified mac address.
3225  * This does not go through the normal mgt frame api so we
3226  * can specify the destination address and re-use the bss node
3227  * for the sta reference.
3228  */
3229 int
3230 ieee80211_send_proberesp(struct ieee80211vap *vap,
3231 	const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
3232 {
3233 	struct ieee80211_node *bss = vap->iv_bss;
3234 	struct ieee80211com *ic = vap->iv_ic;
3235 	struct mbuf *m;
3236 	int ret;
3237 
3238 	if (vap->iv_state == IEEE80211_S_CAC) {
3239 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
3240 		    "block %s frame in CAC state", "probe response");
3241 		vap->iv_stats.is_tx_badstate++;
3242 		return EIO;		/* XXX */
3243 	}
3244 
3245 	/*
3246 	 * Hold a reference on the node so it doesn't go away until after
3247 	 * the xmit is complete all the way in the driver.  On error we
3248 	 * will remove our reference.
3249 	 */
3250 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
3251 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
3252 	    __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
3253 	    ieee80211_node_refcnt(bss)+1);
3254 	ieee80211_ref_node(bss);
3255 
3256 	m = ieee80211_alloc_proberesp(bss, legacy);
3257 	if (m == NULL) {
3258 		ieee80211_free_node(bss);
3259 		return ENOMEM;
3260 	}
3261 
3262 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
3263 	KASSERT(m != NULL, ("no room for header"));
3264 
3265 	IEEE80211_TX_LOCK(ic);
3266 	ieee80211_send_setup(bss, m,
3267 	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
3268 	     IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
3269 	/* XXX power management? */
3270 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
3271 
3272 	M_WME_SETAC(m, WME_AC_BE);
3273 
3274 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
3275 	    "send probe resp on channel %u to %s%s\n",
3276 	    ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
3277 	    legacy ? " <legacy>" : "");
3278 	IEEE80211_NODE_STAT(bss, tx_mgmt);
3279 
3280 	ret = ieee80211_raw_output(vap, bss, m, NULL);
3281 	IEEE80211_TX_UNLOCK(ic);
3282 	return (ret);
3283 }
3284 
3285 /*
3286  * Allocate and build a RTS (Request To Send) control frame.
3287  */
3288 struct mbuf *
3289 ieee80211_alloc_rts(struct ieee80211com *ic,
3290 	const uint8_t ra[IEEE80211_ADDR_LEN],
3291 	const uint8_t ta[IEEE80211_ADDR_LEN],
3292 	uint16_t dur)
3293 {
3294 	struct ieee80211_frame_rts *rts;
3295 	struct mbuf *m;
3296 
3297 	/* XXX honor ic_headroom */
3298 	m = m_gethdr(M_NOWAIT, MT_DATA);
3299 	if (m != NULL) {
3300 		rts = mtod(m, struct ieee80211_frame_rts *);
3301 		rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
3302 			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
3303 		rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3304 		*(u_int16_t *)rts->i_dur = htole16(dur);
3305 		IEEE80211_ADDR_COPY(rts->i_ra, ra);
3306 		IEEE80211_ADDR_COPY(rts->i_ta, ta);
3307 
3308 		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
3309 	}
3310 	return m;
3311 }
3312 
3313 /*
3314  * Allocate and build a CTS (Clear To Send) control frame.
3315  */
3316 struct mbuf *
3317 ieee80211_alloc_cts(struct ieee80211com *ic,
3318 	const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
3319 {
3320 	struct ieee80211_frame_cts *cts;
3321 	struct mbuf *m;
3322 
3323 	/* XXX honor ic_headroom */
3324 	m = m_gethdr(M_NOWAIT, MT_DATA);
3325 	if (m != NULL) {
3326 		cts = mtod(m, struct ieee80211_frame_cts *);
3327 		cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
3328 			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
3329 		cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3330 		*(u_int16_t *)cts->i_dur = htole16(dur);
3331 		IEEE80211_ADDR_COPY(cts->i_ra, ra);
3332 
3333 		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
3334 	}
3335 	return m;
3336 }
3337 
3338 /*
3339  * Wrapper for CTS/RTS frame allocation.
3340  */
3341 struct mbuf *
3342 ieee80211_alloc_prot(struct ieee80211_node *ni, const struct mbuf *m,
3343     uint8_t rate, int prot)
3344 {
3345 	struct ieee80211com *ic = ni->ni_ic;
3346 	struct ieee80211vap *vap = ni->ni_vap;
3347 	const struct ieee80211_frame *wh;
3348 	struct mbuf *mprot;
3349 	uint16_t dur;
3350 	int pktlen, isshort;
3351 
3352 	KASSERT(prot == IEEE80211_PROT_RTSCTS ||
3353 	    prot == IEEE80211_PROT_CTSONLY,
3354 	    ("wrong protection type %d", prot));
3355 
3356 	wh = mtod(m, const struct ieee80211_frame *);
3357 	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
3358 	isshort = (vap->iv_flags & IEEE80211_F_SHPREAMBLE) != 0;
3359 	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
3360 	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3361 
3362 	if (prot == IEEE80211_PROT_RTSCTS) {
3363 		/* NB: CTS is the same size as an ACK */
3364 		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3365 		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
3366 	} else
3367 		mprot = ieee80211_alloc_cts(ic, vap->iv_myaddr, dur);
3368 
3369 	return (mprot);
3370 }
3371 
3372 static void
3373 ieee80211_tx_mgt_timeout(void *arg)
3374 {
3375 	struct ieee80211vap *vap = arg;
3376 
3377 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
3378 	    "vap %p mode %s state %s flags %#x & %#x\n", vap,
3379 	    ieee80211_opmode_name[vap->iv_opmode],
3380 	    ieee80211_state_name[vap->iv_state],
3381 	    vap->iv_ic->ic_flags, IEEE80211_F_SCAN);
3382 
3383 	IEEE80211_LOCK(vap->iv_ic);
3384 	if (vap->iv_state != IEEE80211_S_INIT &&
3385 	    (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
3386 		/*
3387 		 * NB: it's safe to specify a timeout as the reason here;
3388 		 *     it'll only be used in the right state.
3389 		 */
3390 		ieee80211_new_state_locked(vap, IEEE80211_S_SCAN,
3391 			IEEE80211_SCAN_FAIL_TIMEOUT);
3392 	}
3393 	IEEE80211_UNLOCK(vap->iv_ic);
3394 }
3395 
3396 /*
3397  * This is the callback set on net80211-sourced transmitted
3398  * authentication request frames.
3399  *
3400  * This does a couple of things:
3401  *
3402  * + If the frame transmitted was a success, it schedules a future
3403  *   event which will transition the interface to scan.
3404  *   If a state transition _then_ occurs before that event occurs,
3405  *   said state transition will cancel this callout.
3406  *
3407  * + If the frame transmit was a failure, it immediately schedules
3408  *   the transition back to scan.
3409  */
3410 static void
3411 ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
3412 {
3413 	struct ieee80211vap *vap = ni->ni_vap;
3414 	enum ieee80211_state ostate = (enum ieee80211_state)(uintptr_t)arg;
3415 
3416 	/*
3417 	 * Frame transmit completed; arrange timer callback.  If
3418 	 * transmit was successfully we wait for response.  Otherwise
3419 	 * we arrange an immediate callback instead of doing the
3420 	 * callback directly since we don't know what state the driver
3421 	 * is in (e.g. what locks it is holding).  This work should
3422 	 * not be too time-critical and not happen too often so the
3423 	 * added overhead is acceptable.
3424 	 *
3425 	 * XXX what happens if !acked but response shows up before callback?
3426 	 */
3427 	if (vap->iv_state == ostate) {
3428 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
3429 		    "ni %p mode %s state %s arg %p status %d\n", ni,
3430 		    ieee80211_opmode_name[vap->iv_opmode],
3431 		    ieee80211_state_name[vap->iv_state], arg, status);
3432 
3433 		callout_reset(&vap->iv_mgtsend,
3434 			status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
3435 			ieee80211_tx_mgt_timeout, vap);
3436 	}
3437 }
3438 
3439 static void
3440 ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
3441 	struct ieee80211_node *ni)
3442 {
3443 	struct ieee80211vap *vap = ni->ni_vap;
3444 	struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
3445 	struct ieee80211com *ic = ni->ni_ic;
3446 	struct ieee80211_rateset *rs = &ni->ni_rates;
3447 	uint16_t capinfo;
3448 
3449 	/*
3450 	 * beacon frame format
3451 	 *
3452 	 * TODO: update to 802.11-2012; a lot of stuff has changed;
3453 	 * vendor extensions should be at the end, etc.
3454 	 *
3455 	 *	[8] time stamp
3456 	 *	[2] beacon interval
3457 	 *	[2] cabability information
3458 	 *	[tlv] ssid
3459 	 *	[tlv] supported rates
3460 	 *	[3] parameter set (DS)
3461 	 *	[8] CF parameter set (optional)
3462 	 *	[tlv] parameter set (IBSS/TIM)
3463 	 *	[tlv] country (optional)
3464 	 *	[3] power control (optional)
3465 	 *	[5] channel switch announcement (CSA) (optional)
3466 	 * XXX TODO: Quiet
3467 	 * XXX TODO: IBSS DFS
3468 	 * XXX TODO: TPC report
3469 	 *	[tlv] extended rate phy (ERP)
3470 	 *	[tlv] extended supported rates
3471 	 *	[tlv] RSN parameters
3472 	 * XXX TODO: BSSLOAD
3473 	 * (XXX EDCA parameter set, QoS capability?)
3474 	 * XXX TODO: AP channel report
3475 	 *
3476 	 *	[tlv] HT capabilities
3477 	 *	[tlv] HT information
3478 	 *	XXX TODO: 20/40 BSS coexistence
3479 	 * Mesh:
3480 	 * XXX TODO: Meshid
3481 	 * XXX TODO: mesh config
3482 	 * XXX TODO: mesh awake window
3483 	 * XXX TODO: beacon timing (mesh, etc)
3484 	 * XXX TODO: MCCAOP Advertisement Overview
3485 	 * XXX TODO: MCCAOP Advertisement
3486 	 * XXX TODO: Mesh channel switch parameters
3487 	 * VHT:
3488 	 * XXX TODO: VHT capabilities
3489 	 * XXX TODO: VHT operation
3490 	 * XXX TODO: VHT transmit power envelope
3491 	 * XXX TODO: channel switch wrapper element
3492 	 * XXX TODO: extended BSS load element
3493 	 *
3494 	 * XXX Vendor-specific OIDs (e.g. Atheros)
3495 	 *	[tlv] WPA parameters
3496 	 *	[tlv] WME parameters
3497 	 *	[tlv] Vendor OUI HT capabilities (optional)
3498 	 *	[tlv] Vendor OUI HT information (optional)
3499 	 *	[tlv] Atheros capabilities (optional)
3500 	 *	[tlv] TDMA parameters (optional)
3501 	 *	[tlv] Mesh ID (MBSS)
3502 	 *	[tlv] Mesh Conf (MBSS)
3503 	 *	[tlv] application data (optional)
3504 	 */
3505 
3506 	memset(bo, 0, sizeof(*bo));
3507 
3508 	memset(frm, 0, 8);	/* XXX timestamp is set by hardware/driver */
3509 	frm += 8;
3510 	*(uint16_t *)frm = htole16(ni->ni_intval);
3511 	frm += 2;
3512 	capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
3513 	bo->bo_caps = (uint16_t *)frm;
3514 	*(uint16_t *)frm = htole16(capinfo);
3515 	frm += 2;
3516 	*frm++ = IEEE80211_ELEMID_SSID;
3517 	if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
3518 		*frm++ = ni->ni_esslen;
3519 		memcpy(frm, ni->ni_essid, ni->ni_esslen);
3520 		frm += ni->ni_esslen;
3521 	} else
3522 		*frm++ = 0;
3523 	frm = ieee80211_add_rates(frm, rs);
3524 	if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
3525 		*frm++ = IEEE80211_ELEMID_DSPARMS;
3526 		*frm++ = 1;
3527 		*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
3528 	}
3529 	if (ic->ic_flags & IEEE80211_F_PCF) {
3530 		bo->bo_cfp = frm;
3531 		frm = ieee80211_add_cfparms(frm, ic);
3532 	}
3533 	bo->bo_tim = frm;
3534 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
3535 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
3536 		*frm++ = 2;
3537 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
3538 		bo->bo_tim_len = 0;
3539 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
3540 	    vap->iv_opmode == IEEE80211_M_MBSS) {
3541 		/* TIM IE is the same for Mesh and Hostap */
3542 		struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
3543 
3544 		tie->tim_ie = IEEE80211_ELEMID_TIM;
3545 		tie->tim_len = 4;	/* length */
3546 		tie->tim_count = 0;	/* DTIM count */
3547 		tie->tim_period = vap->iv_dtim_period;	/* DTIM period */
3548 		tie->tim_bitctl = 0;	/* bitmap control */
3549 		tie->tim_bitmap[0] = 0;	/* Partial Virtual Bitmap */
3550 		frm += sizeof(struct ieee80211_tim_ie);
3551 		bo->bo_tim_len = 1;
3552 	}
3553 	bo->bo_tim_trailer = frm;
3554 	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
3555 	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
3556 		frm = ieee80211_add_countryie(frm, ic);
3557 	if (vap->iv_flags & IEEE80211_F_DOTH) {
3558 		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
3559 			frm = ieee80211_add_powerconstraint(frm, vap);
3560 		bo->bo_csa = frm;
3561 		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
3562 			frm = ieee80211_add_csa(frm, vap);
3563 	} else
3564 		bo->bo_csa = frm;
3565 
3566 	bo->bo_quiet = NULL;
3567 	if (vap->iv_flags & IEEE80211_F_DOTH) {
3568 		if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
3569 		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
3570 		    (vap->iv_quiet == 1)) {
3571 			/*
3572 			 * We only insert the quiet IE offset if
3573 			 * the quiet IE is enabled.  Otherwise don't
3574 			 * put it here or we'll just overwrite
3575 			 * some other beacon contents.
3576 			 */
3577 			if (vap->iv_quiet) {
3578 				bo->bo_quiet = frm;
3579 				frm = ieee80211_add_quiet(frm,vap, 0);
3580 			}
3581 		}
3582 	}
3583 
3584 	if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
3585 		bo->bo_erp = frm;
3586 		frm = ieee80211_add_erp(frm, vap);
3587 	}
3588 	frm = ieee80211_add_xrates(frm, rs);
3589 	frm = ieee80211_add_rsn(frm, vap);
3590 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
3591 		frm = ieee80211_add_htcap(frm, ni);
3592 		bo->bo_htinfo = frm;
3593 		frm = ieee80211_add_htinfo(frm, ni);
3594 	}
3595 
3596 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
3597 		frm = ieee80211_add_vhtcap(frm, ni);
3598 		bo->bo_vhtinfo = frm;
3599 		frm = ieee80211_add_vhtinfo(frm, ni);
3600 		/* Transmit power envelope */
3601 		/* Channel switch wrapper element */
3602 		/* Extended bss load element */
3603 	}
3604 
3605 	frm = ieee80211_add_wpa(frm, vap);
3606 	if (vap->iv_flags & IEEE80211_F_WME) {
3607 		bo->bo_wme = frm;
3608 		frm = ieee80211_add_wme_param(frm, &ic->ic_wme,
3609 		    !! (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD));
3610 	}
3611 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
3612 	    (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
3613 		frm = ieee80211_add_htcap_vendor(frm, ni);
3614 		frm = ieee80211_add_htinfo_vendor(frm, ni);
3615 	}
3616 
3617 #ifdef IEEE80211_SUPPORT_SUPERG
3618 	if (vap->iv_flags & IEEE80211_F_ATHEROS) {
3619 		bo->bo_ath = frm;
3620 		frm = ieee80211_add_athcaps(frm, ni);
3621 	}
3622 #endif
3623 #ifdef IEEE80211_SUPPORT_TDMA
3624 	if (vap->iv_caps & IEEE80211_C_TDMA) {
3625 		bo->bo_tdma = frm;
3626 		frm = ieee80211_add_tdma(frm, vap);
3627 	}
3628 #endif
3629 	if (vap->iv_appie_beacon != NULL) {
3630 		bo->bo_appie = frm;
3631 		bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
3632 		frm = add_appie(frm, vap->iv_appie_beacon);
3633 	}
3634 
3635 	/* XXX TODO: move meshid/meshconf up to before vendor extensions? */
3636 #ifdef IEEE80211_SUPPORT_MESH
3637 	if (vap->iv_opmode == IEEE80211_M_MBSS) {
3638 		frm = ieee80211_add_meshid(frm, vap);
3639 		bo->bo_meshconf = frm;
3640 		frm = ieee80211_add_meshconf(frm, vap);
3641 	}
3642 #endif
3643 	bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
3644 	bo->bo_csa_trailer_len = frm - bo->bo_csa;
3645 	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3646 }
3647 
3648 /*
3649  * Allocate a beacon frame and fillin the appropriate bits.
3650  */
3651 struct mbuf *
3652 ieee80211_beacon_alloc(struct ieee80211_node *ni)
3653 {
3654 	struct ieee80211vap *vap = ni->ni_vap;
3655 	struct ieee80211com *ic = ni->ni_ic;
3656 	struct ifnet *ifp = vap->iv_ifp;
3657 	struct ieee80211_frame *wh;
3658 	struct mbuf *m;
3659 	int pktlen;
3660 	uint8_t *frm;
3661 
3662 	/*
3663 	 * Update the "We're putting the quiet IE in the beacon" state.
3664 	 */
3665 	if (vap->iv_quiet == 1)
3666 		vap->iv_flags_ext |= IEEE80211_FEXT_QUIET_IE;
3667 	else if (vap->iv_quiet == 0)
3668 		vap->iv_flags_ext &= ~IEEE80211_FEXT_QUIET_IE;
3669 
3670 	/*
3671 	 * beacon frame format
3672 	 *
3673 	 * Note: This needs updating for 802.11-2012.
3674 	 *
3675 	 *	[8] time stamp
3676 	 *	[2] beacon interval
3677 	 *	[2] cabability information
3678 	 *	[tlv] ssid
3679 	 *	[tlv] supported rates
3680 	 *	[3] parameter set (DS)
3681 	 *	[8] CF parameter set (optional)
3682 	 *	[tlv] parameter set (IBSS/TIM)
3683 	 *	[tlv] country (optional)
3684 	 *	[3] power control (optional)
3685 	 *	[5] channel switch announcement (CSA) (optional)
3686 	 *	[tlv] extended rate phy (ERP)
3687 	 *	[tlv] extended supported rates
3688 	 *	[tlv] RSN parameters
3689 	 *	[tlv] HT capabilities
3690 	 *	[tlv] HT information
3691 	 *	[tlv] VHT capabilities
3692 	 *	[tlv] VHT operation
3693 	 *	[tlv] Vendor OUI HT capabilities (optional)
3694 	 *	[tlv] Vendor OUI HT information (optional)
3695 	 * XXX Vendor-specific OIDs (e.g. Atheros)
3696 	 *	[tlv] WPA parameters
3697 	 *	[tlv] WME parameters
3698 	 *	[tlv] TDMA parameters (optional)
3699 	 *	[tlv] Mesh ID (MBSS)
3700 	 *	[tlv] Mesh Conf (MBSS)
3701 	 *	[tlv] application data (optional)
3702 	 * NB: we allocate the max space required for the TIM bitmap.
3703 	 * XXX how big is this?
3704 	 */
3705 	pktlen =   8					/* time stamp */
3706 		 + sizeof(uint16_t)			/* beacon interval */
3707 		 + sizeof(uint16_t)			/* capabilities */
3708 		 + 2 + ni->ni_esslen			/* ssid */
3709 	         + 2 + IEEE80211_RATE_SIZE		/* supported rates */
3710 	         + 2 + 1				/* DS parameters */
3711 		 + 2 + 6				/* CF parameters */
3712 		 + 2 + 4 + vap->iv_tim_len		/* DTIM/IBSSPARMS */
3713 		 + IEEE80211_COUNTRY_MAX_SIZE		/* country */
3714 		 + 2 + 1				/* power control */
3715 		 + sizeof(struct ieee80211_csa_ie)	/* CSA */
3716 		 + sizeof(struct ieee80211_quiet_ie)	/* Quiet */
3717 		 + 2 + 1				/* ERP */
3718 	         + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
3719 		 + (vap->iv_caps & IEEE80211_C_WPA ?	/* WPA 1+2 */
3720 			2*sizeof(struct ieee80211_ie_wpa) : 0)
3721 		 /* XXX conditional? */
3722 		 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
3723 		 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
3724 		 + sizeof(struct ieee80211_ie_vhtcap)/* VHT caps */
3725 		 + sizeof(struct ieee80211_ie_vht_operation)/* VHT info */
3726 		 + (vap->iv_caps & IEEE80211_C_WME ?	/* WME */
3727 			sizeof(struct ieee80211_wme_param) : 0)
3728 #ifdef IEEE80211_SUPPORT_SUPERG
3729 		 + sizeof(struct ieee80211_ath_ie)	/* ATH */
3730 #endif
3731 #ifdef IEEE80211_SUPPORT_TDMA
3732 		 + (vap->iv_caps & IEEE80211_C_TDMA ?	/* TDMA */
3733 			sizeof(struct ieee80211_tdma_param) : 0)
3734 #endif
3735 #ifdef IEEE80211_SUPPORT_MESH
3736 		 + 2 + ni->ni_meshidlen
3737 		 + sizeof(struct ieee80211_meshconf_ie)
3738 #endif
3739 		 + IEEE80211_MAX_APPIE
3740 		 ;
3741 	m = ieee80211_getmgtframe(&frm,
3742 		ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
3743 	if (m == NULL) {
3744 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
3745 			"%s: cannot get buf; size %u\n", __func__, pktlen);
3746 		vap->iv_stats.is_tx_nobuf++;
3747 		return NULL;
3748 	}
3749 	ieee80211_beacon_construct(m, frm, ni);
3750 
3751 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
3752 	KASSERT(m != NULL, ("no space for 802.11 header?"));
3753 	wh = mtod(m, struct ieee80211_frame *);
3754 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
3755 	    IEEE80211_FC0_SUBTYPE_BEACON;
3756 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3757 	*(uint16_t *)wh->i_dur = 0;
3758 	IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
3759 	IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
3760 	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
3761 	*(uint16_t *)wh->i_seq = 0;
3762 
3763 	return m;
3764 }
3765 
3766 /*
3767  * Update the dynamic parts of a beacon frame based on the current state.
3768  */
3769 int
3770 ieee80211_beacon_update(struct ieee80211_node *ni, struct mbuf *m, int mcast)
3771 {
3772 	struct ieee80211vap *vap = ni->ni_vap;
3773 	struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
3774 	struct ieee80211com *ic = ni->ni_ic;
3775 	int len_changed = 0;
3776 	uint16_t capinfo;
3777 	struct ieee80211_frame *wh;
3778 	ieee80211_seq seqno;
3779 
3780 	IEEE80211_LOCK(ic);
3781 	/*
3782 	 * Handle 11h channel change when we've reached the count.
3783 	 * We must recalculate the beacon frame contents to account
3784 	 * for the new channel.  Note we do this only for the first
3785 	 * vap that reaches this point; subsequent vaps just update
3786 	 * their beacon state to reflect the recalculated channel.
3787 	 */
3788 	if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
3789 	    vap->iv_csa_count == ic->ic_csa_count) {
3790 		vap->iv_csa_count = 0;
3791 		/*
3792 		 * Effect channel change before reconstructing the beacon
3793 		 * frame contents as many places reference ni_chan.
3794 		 */
3795 		if (ic->ic_csa_newchan != NULL)
3796 			ieee80211_csa_completeswitch(ic);
3797 		/*
3798 		 * NB: ieee80211_beacon_construct clears all pending
3799 		 * updates in bo_flags so we don't need to explicitly
3800 		 * clear IEEE80211_BEACON_CSA.
3801 		 */
3802 		ieee80211_beacon_construct(m,
3803 		    mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
3804 
3805 		/* XXX do WME aggressive mode processing? */
3806 		IEEE80211_UNLOCK(ic);
3807 		return 1;		/* just assume length changed */
3808 	}
3809 
3810 	/*
3811 	 * Handle the quiet time element being added and removed.
3812 	 * Again, for now we just cheat and reconstruct the whole
3813 	 * beacon - that way the gap is provided as appropriate.
3814 	 *
3815 	 * So, track whether we have already added the IE versus
3816 	 * whether we want to be adding the IE.
3817 	 */
3818 	if ((vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE) &&
3819 	    (vap->iv_quiet == 0)) {
3820 		/*
3821 		 * Quiet time beacon IE enabled, but it's disabled;
3822 		 * recalc
3823 		 */
3824 		vap->iv_flags_ext &= ~IEEE80211_FEXT_QUIET_IE;
3825 		ieee80211_beacon_construct(m,
3826 		    mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
3827 		/* XXX do WME aggressive mode processing? */
3828 		IEEE80211_UNLOCK(ic);
3829 		return 1;		/* just assume length changed */
3830 	}
3831 
3832 	if (((vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE) == 0) &&
3833 	    (vap->iv_quiet == 1)) {
3834 		/*
3835 		 * Quiet time beacon IE disabled, but it's now enabled;
3836 		 * recalc
3837 		 */
3838 		vap->iv_flags_ext |= IEEE80211_FEXT_QUIET_IE;
3839 		ieee80211_beacon_construct(m,
3840 		    mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
3841 		/* XXX do WME aggressive mode processing? */
3842 		IEEE80211_UNLOCK(ic);
3843 		return 1;		/* just assume length changed */
3844 	}
3845 
3846 	wh = mtod(m, struct ieee80211_frame *);
3847 
3848 	/*
3849 	 * XXX TODO Strictly speaking this should be incremented with the TX
3850 	 * lock held so as to serialise access to the non-qos TID sequence
3851 	 * number space.
3852 	 *
3853 	 * If the driver identifies it does its own TX seqno management then
3854 	 * we can skip this (and still not do the TX seqno.)
3855 	 */
3856 	seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
3857 	*(uint16_t *)&wh->i_seq[0] =
3858 		htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
3859 	M_SEQNO_SET(m, seqno);
3860 
3861 	/* XXX faster to recalculate entirely or just changes? */
3862 	capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
3863 	*bo->bo_caps = htole16(capinfo);
3864 
3865 	if (vap->iv_flags & IEEE80211_F_WME) {
3866 		struct ieee80211_wme_state *wme = &ic->ic_wme;
3867 
3868 		/*
3869 		 * Check for aggressive mode change.  When there is
3870 		 * significant high priority traffic in the BSS
3871 		 * throttle back BE traffic by using conservative
3872 		 * parameters.  Otherwise BE uses aggressive params
3873 		 * to optimize performance of legacy/non-QoS traffic.
3874 		 */
3875 		if (wme->wme_flags & WME_F_AGGRMODE) {
3876 			if (wme->wme_hipri_traffic >
3877 			    wme->wme_hipri_switch_thresh) {
3878 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3879 				    "%s: traffic %u, disable aggressive mode\n",
3880 				    __func__, wme->wme_hipri_traffic);
3881 				wme->wme_flags &= ~WME_F_AGGRMODE;
3882 				ieee80211_wme_updateparams_locked(vap);
3883 				wme->wme_hipri_traffic =
3884 					wme->wme_hipri_switch_hysteresis;
3885 			} else
3886 				wme->wme_hipri_traffic = 0;
3887 		} else {
3888 			if (wme->wme_hipri_traffic <=
3889 			    wme->wme_hipri_switch_thresh) {
3890 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3891 				    "%s: traffic %u, enable aggressive mode\n",
3892 				    __func__, wme->wme_hipri_traffic);
3893 				wme->wme_flags |= WME_F_AGGRMODE;
3894 				ieee80211_wme_updateparams_locked(vap);
3895 				wme->wme_hipri_traffic = 0;
3896 			} else
3897 				wme->wme_hipri_traffic =
3898 					wme->wme_hipri_switch_hysteresis;
3899 		}
3900 		if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
3901 			(void) ieee80211_add_wme_param(bo->bo_wme, wme,
3902 			  vap->iv_flags_ext & IEEE80211_FEXT_UAPSD);
3903 			clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
3904 		}
3905 	}
3906 
3907 	if (isset(bo->bo_flags,  IEEE80211_BEACON_HTINFO)) {
3908 		ieee80211_ht_update_beacon(vap, bo);
3909 		clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
3910 	}
3911 #ifdef IEEE80211_SUPPORT_TDMA
3912 	if (vap->iv_caps & IEEE80211_C_TDMA) {
3913 		/*
3914 		 * NB: the beacon is potentially updated every TBTT.
3915 		 */
3916 		ieee80211_tdma_update_beacon(vap, bo);
3917 	}
3918 #endif
3919 #ifdef IEEE80211_SUPPORT_MESH
3920 	if (vap->iv_opmode == IEEE80211_M_MBSS)
3921 		ieee80211_mesh_update_beacon(vap, bo);
3922 #endif
3923 
3924 	if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
3925 	    vap->iv_opmode == IEEE80211_M_MBSS) {	/* NB: no IBSS support*/
3926 		struct ieee80211_tim_ie *tie =
3927 			(struct ieee80211_tim_ie *) bo->bo_tim;
3928 		if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
3929 			u_int timlen, timoff, i;
3930 			/*
3931 			 * ATIM/DTIM needs updating.  If it fits in the
3932 			 * current space allocated then just copy in the
3933 			 * new bits.  Otherwise we need to move any trailing
3934 			 * data to make room.  Note that we know there is
3935 			 * contiguous space because ieee80211_beacon_allocate
3936 			 * insures there is space in the mbuf to write a
3937 			 * maximal-size virtual bitmap (based on iv_max_aid).
3938 			 */
3939 			/*
3940 			 * Calculate the bitmap size and offset, copy any
3941 			 * trailer out of the way, and then copy in the
3942 			 * new bitmap and update the information element.
3943 			 * Note that the tim bitmap must contain at least
3944 			 * one byte and any offset must be even.
3945 			 */
3946 			if (vap->iv_ps_pending != 0) {
3947 				timoff = 128;		/* impossibly large */
3948 				for (i = 0; i < vap->iv_tim_len; i++)
3949 					if (vap->iv_tim_bitmap[i]) {
3950 						timoff = i &~ 1;
3951 						break;
3952 					}
3953 				KASSERT(timoff != 128, ("tim bitmap empty!"));
3954 				for (i = vap->iv_tim_len-1; i >= timoff; i--)
3955 					if (vap->iv_tim_bitmap[i])
3956 						break;
3957 				timlen = 1 + (i - timoff);
3958 			} else {
3959 				timoff = 0;
3960 				timlen = 1;
3961 			}
3962 
3963 			/*
3964 			 * TODO: validate this!
3965 			 */
3966 			if (timlen != bo->bo_tim_len) {
3967 				/* copy up/down trailer */
3968 				int adjust = tie->tim_bitmap+timlen
3969 					   - bo->bo_tim_trailer;
3970 				ovbcopy(bo->bo_tim_trailer,
3971 				    bo->bo_tim_trailer+adjust,
3972 				    bo->bo_tim_trailer_len);
3973 				bo->bo_tim_trailer += adjust;
3974 				bo->bo_erp += adjust;
3975 				bo->bo_htinfo += adjust;
3976 				bo->bo_vhtinfo += adjust;
3977 #ifdef IEEE80211_SUPPORT_SUPERG
3978 				bo->bo_ath += adjust;
3979 #endif
3980 #ifdef IEEE80211_SUPPORT_TDMA
3981 				bo->bo_tdma += adjust;
3982 #endif
3983 #ifdef IEEE80211_SUPPORT_MESH
3984 				bo->bo_meshconf += adjust;
3985 #endif
3986 				bo->bo_appie += adjust;
3987 				bo->bo_wme += adjust;
3988 				bo->bo_csa += adjust;
3989 				bo->bo_quiet += adjust;
3990 				bo->bo_tim_len = timlen;
3991 
3992 				/* update information element */
3993 				tie->tim_len = 3 + timlen;
3994 				tie->tim_bitctl = timoff;
3995 				len_changed = 1;
3996 			}
3997 			memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
3998 				bo->bo_tim_len);
3999 
4000 			clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
4001 
4002 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
4003 				"%s: TIM updated, pending %u, off %u, len %u\n",
4004 				__func__, vap->iv_ps_pending, timoff, timlen);
4005 		}
4006 		/* count down DTIM period */
4007 		if (tie->tim_count == 0)
4008 			tie->tim_count = tie->tim_period - 1;
4009 		else
4010 			tie->tim_count--;
4011 		/* update state for buffered multicast frames on DTIM */
4012 		if (mcast && tie->tim_count == 0)
4013 			tie->tim_bitctl |= 1;
4014 		else
4015 			tie->tim_bitctl &= ~1;
4016 		if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
4017 			struct ieee80211_csa_ie *csa =
4018 			    (struct ieee80211_csa_ie *) bo->bo_csa;
4019 
4020 			/*
4021 			 * Insert or update CSA ie.  If we're just starting
4022 			 * to count down to the channel switch then we need
4023 			 * to insert the CSA ie.  Otherwise we just need to
4024 			 * drop the count.  The actual change happens above
4025 			 * when the vap's count reaches the target count.
4026 			 */
4027 			if (vap->iv_csa_count == 0) {
4028 				memmove(&csa[1], csa, bo->bo_csa_trailer_len);
4029 				bo->bo_erp += sizeof(*csa);
4030 				bo->bo_htinfo += sizeof(*csa);
4031 				bo->bo_vhtinfo += sizeof(*csa);
4032 				bo->bo_wme += sizeof(*csa);
4033 #ifdef IEEE80211_SUPPORT_SUPERG
4034 				bo->bo_ath += sizeof(*csa);
4035 #endif
4036 #ifdef IEEE80211_SUPPORT_TDMA
4037 				bo->bo_tdma += sizeof(*csa);
4038 #endif
4039 #ifdef IEEE80211_SUPPORT_MESH
4040 				bo->bo_meshconf += sizeof(*csa);
4041 #endif
4042 				bo->bo_appie += sizeof(*csa);
4043 				bo->bo_csa_trailer_len += sizeof(*csa);
4044 				bo->bo_quiet += sizeof(*csa);
4045 				bo->bo_tim_trailer_len += sizeof(*csa);
4046 				m->m_len += sizeof(*csa);
4047 				m->m_pkthdr.len += sizeof(*csa);
4048 
4049 				ieee80211_add_csa(bo->bo_csa, vap);
4050 			} else
4051 				csa->csa_count--;
4052 			vap->iv_csa_count++;
4053 			/* NB: don't clear IEEE80211_BEACON_CSA */
4054 		}
4055 
4056 		/*
4057 		 * Only add the quiet time IE if we've enabled it
4058 		 * as appropriate.
4059 		 */
4060 		if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
4061 		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
4062 			if (vap->iv_quiet &&
4063 			    (vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE)) {
4064 				ieee80211_add_quiet(bo->bo_quiet, vap, 1);
4065 			}
4066 		}
4067 		if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
4068 			/*
4069 			 * ERP element needs updating.
4070 			 */
4071 			(void) ieee80211_add_erp(bo->bo_erp, vap);
4072 			clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
4073 		}
4074 #ifdef IEEE80211_SUPPORT_SUPERG
4075 		if (isset(bo->bo_flags,  IEEE80211_BEACON_ATH)) {
4076 			ieee80211_add_athcaps(bo->bo_ath, ni);
4077 			clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
4078 		}
4079 #endif
4080 	}
4081 	if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
4082 		const struct ieee80211_appie *aie = vap->iv_appie_beacon;
4083 		int aielen;
4084 		uint8_t *frm;
4085 
4086 		aielen = 0;
4087 		if (aie != NULL)
4088 			aielen += aie->ie_len;
4089 		if (aielen != bo->bo_appie_len) {
4090 			/* copy up/down trailer */
4091 			int adjust = aielen - bo->bo_appie_len;
4092 			ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
4093 				bo->bo_tim_trailer_len);
4094 			bo->bo_tim_trailer += adjust;
4095 			bo->bo_appie += adjust;
4096 			bo->bo_appie_len = aielen;
4097 
4098 			len_changed = 1;
4099 		}
4100 		frm = bo->bo_appie;
4101 		if (aie != NULL)
4102 			frm  = add_appie(frm, aie);
4103 		clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
4104 	}
4105 	IEEE80211_UNLOCK(ic);
4106 
4107 	return len_changed;
4108 }
4109 
4110 /*
4111  * Do Ethernet-LLC encapsulation for each payload in a fast frame
4112  * tunnel encapsulation.  The frame is assumed to have an Ethernet
4113  * header at the front that must be stripped before prepending the
4114  * LLC followed by the Ethernet header passed in (with an Ethernet
4115  * type that specifies the payload size).
4116  */
4117 struct mbuf *
4118 ieee80211_ff_encap1(struct ieee80211vap *vap, struct mbuf *m,
4119 	const struct ether_header *eh)
4120 {
4121 	struct llc *llc;
4122 	uint16_t payload;
4123 
4124 	/* XXX optimize by combining m_adj+M_PREPEND */
4125 	m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
4126 	llc = mtod(m, struct llc *);
4127 	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
4128 	llc->llc_control = LLC_UI;
4129 	llc->llc_snap.org_code[0] = 0;
4130 	llc->llc_snap.org_code[1] = 0;
4131 	llc->llc_snap.org_code[2] = 0;
4132 	llc->llc_snap.ether_type = eh->ether_type;
4133 	payload = m->m_pkthdr.len;		/* NB: w/o Ethernet header */
4134 
4135 	M_PREPEND(m, sizeof(struct ether_header), M_NOWAIT);
4136 	if (m == NULL) {		/* XXX cannot happen */
4137 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
4138 			"%s: no space for ether_header\n", __func__);
4139 		vap->iv_stats.is_tx_nobuf++;
4140 		return NULL;
4141 	}
4142 	ETHER_HEADER_COPY(mtod(m, void *), eh);
4143 	mtod(m, struct ether_header *)->ether_type = htons(payload);
4144 	return m;
4145 }
4146 
4147 /*
4148  * Complete an mbuf transmission.
4149  *
4150  * For now, this simply processes a completed frame after the
4151  * driver has completed it's transmission and/or retransmission.
4152  * It assumes the frame is an 802.11 encapsulated frame.
4153  *
4154  * Later on it will grow to become the exit path for a given frame
4155  * from the driver and, depending upon how it's been encapsulated
4156  * and already transmitted, it may end up doing A-MPDU retransmission,
4157  * power save requeuing, etc.
4158  *
4159  * In order for the above to work, the driver entry point to this
4160  * must not hold any driver locks.  Thus, the driver needs to delay
4161  * any actual mbuf completion until it can release said locks.
4162  *
4163  * This frees the mbuf and if the mbuf has a node reference,
4164  * the node reference will be freed.
4165  */
4166 void
4167 ieee80211_tx_complete(struct ieee80211_node *ni, struct mbuf *m, int status)
4168 {
4169 
4170 	if (ni != NULL) {
4171 		struct ifnet *ifp = ni->ni_vap->iv_ifp;
4172 
4173 		if (status == 0) {
4174 			if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
4175 			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
4176 			if (m->m_flags & M_MCAST)
4177 				if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4178 		} else
4179 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
4180 		if (m->m_flags & M_TXCB) {
4181 			IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
4182 			   "ni %p vap %p mode %s state %s m %p status %d\n", ni, ni->ni_vap,
4183 			   ieee80211_opmode_name[ni->ni_vap->iv_opmode],
4184 			   ieee80211_state_name[ni->ni_vap->iv_state], m, status);
4185 			ieee80211_process_callback(ni, m, status);
4186 		}
4187 		ieee80211_free_node(ni);
4188 	}
4189 	m_freem(m);
4190 }
4191