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