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