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