xref: /openbsd-src/sys/net80211/ieee80211_output.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*	$OpenBSD: ieee80211_output.c,v 1.128 2020/03/03 18:54:50 stsp Exp $	*/
2 /*	$NetBSD: ieee80211_output.c,v 1.13 2004/05/31 11:02:55 dyoung Exp $	*/
3 
4 /*-
5  * Copyright (c) 2001 Atsushi Onoe
6  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
7  * Copyright (c) 2007-2009 Damien Bergamini
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "bpfilter.h"
34 #include "vlan.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/mbuf.h>
39 #include <sys/kernel.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #include <sys/endian.h>
43 #include <sys/errno.h>
44 #include <sys/sysctl.h>
45 
46 #include <net/if.h>
47 #include <net/if_dl.h>
48 #include <net/if_media.h>
49 #include <net/if_llc.h>
50 #include <net/bpf.h>
51 
52 #include <netinet/in.h>
53 #include <netinet/if_ether.h>
54 #include <netinet/ip.h>
55 #ifdef INET6
56 #include <netinet/ip6.h>
57 #endif
58 
59 #if NVLAN > 0
60 #include <net/if_vlan_var.h>
61 #endif
62 
63 #include <net80211/ieee80211_var.h>
64 #include <net80211/ieee80211_priv.h>
65 
66 int	ieee80211_mgmt_output(struct ifnet *, struct ieee80211_node *,
67 	    struct mbuf *, int);
68 int	ieee80211_can_use_ampdu(struct ieee80211com *,
69 	    struct ieee80211_node *);
70 u_int8_t *ieee80211_add_rsn_body(u_int8_t *, struct ieee80211com *,
71 	    const struct ieee80211_node *, int);
72 struct	mbuf *ieee80211_getmgmt(int, int, u_int);
73 struct	mbuf *ieee80211_get_probe_req(struct ieee80211com *,
74 	    struct ieee80211_node *);
75 #ifndef IEEE80211_STA_ONLY
76 struct	mbuf *ieee80211_get_probe_resp(struct ieee80211com *);
77 #endif
78 struct	mbuf *ieee80211_get_auth(struct ieee80211com *,
79 	    struct ieee80211_node *, u_int16_t, u_int16_t);
80 struct	mbuf *ieee80211_get_deauth(struct ieee80211com *,
81 	    struct ieee80211_node *, u_int16_t);
82 struct	mbuf *ieee80211_get_assoc_req(struct ieee80211com *,
83 	    struct ieee80211_node *, int);
84 #ifndef IEEE80211_STA_ONLY
85 struct	mbuf *ieee80211_get_assoc_resp(struct ieee80211com *,
86 	    struct ieee80211_node *, u_int16_t);
87 #endif
88 struct	mbuf *ieee80211_get_disassoc(struct ieee80211com *,
89 	    struct ieee80211_node *, u_int16_t);
90 struct	mbuf *ieee80211_get_addba_req(struct ieee80211com *,
91 	    struct ieee80211_node *, u_int8_t);
92 struct	mbuf *ieee80211_get_addba_resp(struct ieee80211com *,
93 	    struct ieee80211_node *, u_int8_t, u_int8_t, u_int16_t);
94 struct	mbuf *ieee80211_get_delba(struct ieee80211com *,
95 	    struct ieee80211_node *, u_int8_t, u_int8_t, u_int16_t);
96 uint8_t *ieee80211_add_wme_info(uint8_t *, struct ieee80211com *);
97 #ifndef IEEE80211_STA_ONLY
98 uint8_t *ieee80211_add_wme_param(uint8_t *, struct ieee80211com *);
99 #endif
100 struct	mbuf *ieee80211_get_sa_query(struct ieee80211com *,
101 	    struct ieee80211_node *, u_int8_t);
102 struct	mbuf *ieee80211_get_action(struct ieee80211com *,
103 	    struct ieee80211_node *, u_int8_t, u_int8_t, int);
104 
105 /*
106  * IEEE 802.11 output routine. Normally this will directly call the
107  * Ethernet output routine because 802.11 encapsulation is called
108  * later by the driver. This function can be used to send raw frames
109  * if the mbuf has been tagged with a 802.11 data link type.
110  */
111 int
112 ieee80211_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
113     struct rtentry *rt)
114 {
115 	struct ieee80211_frame *wh;
116 	struct m_tag *mtag;
117 	int error = 0;
118 
119 	/* Interface has to be up and running */
120 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
121 	    (IFF_UP | IFF_RUNNING)) {
122 		error = ENETDOWN;
123 		goto bad;
124 	}
125 
126 	/* Try to get the DLT from a mbuf tag */
127 	if ((mtag = m_tag_find(m, PACKET_TAG_DLT, NULL)) != NULL) {
128 		struct ieee80211com *ic = (void *)ifp;
129 		u_int dlt = *(u_int *)(mtag + 1);
130 
131 		/* Fallback to ethernet for non-802.11 linktypes */
132 		if (!(dlt == DLT_IEEE802_11 || dlt == DLT_IEEE802_11_RADIO))
133 			goto fallback;
134 
135 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min))
136 			return (EINVAL);
137 		wh = mtod(m, struct ieee80211_frame *);
138 		if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
139 		    IEEE80211_FC0_VERSION_0)
140 			return (EINVAL);
141 		if (!(ic->ic_caps & IEEE80211_C_RAWCTL) &&
142 		    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
143 		    IEEE80211_FC0_TYPE_CTL)
144 			return (EINVAL);
145 
146 		return (if_enqueue(ifp, m));
147 	}
148 
149  fallback:
150 	return (ether_output(ifp, m, dst, rt));
151 
152  bad:
153 	m_freem(m);
154 	return (error);
155 }
156 
157 /*
158  * Send a management frame to the specified node.  The node pointer
159  * must have a reference as the pointer will be passed to the driver
160  * and potentially held for a long time.  If the frame is successfully
161  * dispatched to the driver, then it is responsible for freeing the
162  * reference (and potentially free'ing up any associated storage).
163  */
164 int
165 ieee80211_mgmt_output(struct ifnet *ifp, struct ieee80211_node *ni,
166     struct mbuf *m, int type)
167 {
168 	struct ieee80211com *ic = (void *)ifp;
169 	struct ieee80211_frame *wh;
170 
171 	if (ni == NULL)
172 		panic("null node");
173 	ni->ni_inact = 0;
174 
175 	/*
176 	 * We want to pass the node down to the driver's start
177 	 * routine.  We could stick this in an m_tag and tack that
178 	 * on to the mbuf.  However that's rather expensive to do
179 	 * for every frame so instead we stuff it in a special pkthdr
180 	 * field.
181 	 */
182 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
183 	if (m == NULL)
184 		return ENOMEM;
185 	m->m_pkthdr.ph_cookie = ni;
186 
187 	wh = mtod(m, struct ieee80211_frame *);
188 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | type;
189 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
190 	*(u_int16_t *)&wh->i_dur[0] = 0;
191 	*(u_int16_t *)&wh->i_seq[0] =
192 	    htole16(ni->ni_txseq << IEEE80211_SEQ_SEQ_SHIFT);
193 	ni->ni_txseq++;
194 	IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
195 	IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
196 	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
197 
198 	/* check if protection is required for this mgmt frame */
199 	if ((ic->ic_caps & IEEE80211_C_MFP) &&
200 	    (type == IEEE80211_FC0_SUBTYPE_DISASSOC ||
201 	     type == IEEE80211_FC0_SUBTYPE_DEAUTH ||
202 	     type == IEEE80211_FC0_SUBTYPE_ACTION)) {
203 		/*
204 		 * Hack: we should not set the Protected bit in outgoing
205 		 * group management frames, however it is used as an
206 		 * indication to the drivers that they must encrypt the
207 		 * frame.  Drivers should clear this bit from group
208 		 * management frames (software crypto code will do it).
209 		 * XXX could use an mbuf flag..
210 		 */
211 		if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
212 		    (ni->ni_flags & IEEE80211_NODE_TXMGMTPROT))
213 			wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
214 	}
215 
216 	if (ifp->if_flags & IFF_DEBUG) {
217 		/* avoid to print too many frames */
218 		if (
219 #ifndef IEEE80211_STA_ONLY
220 		    ic->ic_opmode == IEEE80211_M_IBSS ||
221 #endif
222 #ifdef IEEE80211_DEBUG
223 		    ieee80211_debug > 1 ||
224 #endif
225 		    (type & IEEE80211_FC0_SUBTYPE_MASK) !=
226 		    IEEE80211_FC0_SUBTYPE_PROBE_RESP)
227 			printf("%s: sending %s to %s on channel %u mode %s\n",
228 			    ifp->if_xname,
229 			    ieee80211_mgt_subtype_name[
230 			    (type & IEEE80211_FC0_SUBTYPE_MASK)
231 			    >> IEEE80211_FC0_SUBTYPE_SHIFT],
232 			    ether_sprintf(ni->ni_macaddr),
233 			    ieee80211_chan2ieee(ic, ni->ni_chan),
234 			    ieee80211_phymode_name[ic->ic_curmode]);
235 	}
236 
237 #ifndef IEEE80211_STA_ONLY
238 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
239 	    ieee80211_pwrsave(ic, m, ni) != 0)
240 		return 0;
241 #endif
242 	mq_enqueue(&ic->ic_mgtq, m);
243 	ifp->if_timer = 1;
244 	if_start(ifp);
245 	return 0;
246 }
247 
248 /*-
249  * EDCA tables are computed using the following formulas:
250  *
251  * 1) EDCATable (non-AP QSTA)
252  *
253  * AC     CWmin 	   CWmax	   AIFSN  TXOP limit(ms)
254  * -------------------------------------------------------------
255  * AC_BK  aCWmin	   aCWmax	   7	  0
256  * AC_BE  aCWmin	   aCWmax	   3	  0
257  * AC_VI  (aCWmin+1)/2-1   aCWmin	   2	  agn=3.008 b=6.016 others=0
258  * AC_VO  (aCWmin+1)/4-1   (aCWmin+1)/2-1  2	  agn=1.504 b=3.264 others=0
259  *
260  * 2) QAPEDCATable (QAP)
261  *
262  * AC     CWmin 	   CWmax	   AIFSN  TXOP limit(ms)
263  * -------------------------------------------------------------
264  * AC_BK  aCWmin	   aCWmax	   7	  0
265  * AC_BE  aCWmin	   4*(aCWmin+1)-1  3	  0
266  * AC_VI  (aCWmin+1)/2-1   aCWmin	   1	  agn=3.008 b=6.016 others=0
267  * AC_VO  (aCWmin+1)/4-1   (aCWmin+1)/2-1  1	  agn=1.504 b=3.264 others=0
268  *
269  * and the following aCWmin/aCWmax values:
270  *
271  * PHY		aCWmin	aCWmax
272  * ---------------------------
273  * 11A		15	1023
274  * 11B  	31	1023
275  * 11G		15*	1023	(*) aCWmin(1)
276  * 11N		15	1023
277  */
278 const struct ieee80211_edca_ac_params
279     ieee80211_edca_table[IEEE80211_MODE_MAX][EDCA_NUM_AC] = {
280 	[IEEE80211_MODE_11B] = {
281 		[EDCA_AC_BK] = { 5, 10, 7,   0 },
282 		[EDCA_AC_BE] = { 5, 10, 3,   0 },
283 		[EDCA_AC_VI] = { 4,  5, 2, 188 },
284 		[EDCA_AC_VO] = { 3,  4, 2, 102 }
285 	},
286 	[IEEE80211_MODE_11A] = {
287 		[EDCA_AC_BK] = { 4, 10, 7,   0 },
288 		[EDCA_AC_BE] = { 4, 10, 3,   0 },
289 		[EDCA_AC_VI] = { 3,  4, 2,  94 },
290 		[EDCA_AC_VO] = { 2,  3, 2,  47 }
291 	},
292 	[IEEE80211_MODE_11G] = {
293 		[EDCA_AC_BK] = { 4, 10, 7,   0 },
294 		[EDCA_AC_BE] = { 4, 10, 3,   0 },
295 		[EDCA_AC_VI] = { 3,  4, 2,  94 },
296 		[EDCA_AC_VO] = { 2,  3, 2,  47 }
297 	},
298 	[IEEE80211_MODE_11N] = {
299 		[EDCA_AC_BK] = { 4, 10, 7,   0 },
300 		[EDCA_AC_BE] = { 4, 10, 3,   0 },
301 		[EDCA_AC_VI] = { 3,  4, 2,  94 },
302 		[EDCA_AC_VO] = { 2,  3, 2,  47 }
303 	},
304 };
305 
306 #ifndef IEEE80211_STA_ONLY
307 const struct ieee80211_edca_ac_params
308     ieee80211_qap_edca_table[IEEE80211_MODE_MAX][EDCA_NUM_AC] = {
309 	[IEEE80211_MODE_11B] = {
310 		[EDCA_AC_BK] = { 5, 10, 7,   0 },
311 		[EDCA_AC_BE] = { 5,  7, 3,   0 },
312 		[EDCA_AC_VI] = { 4,  5, 1, 188 },
313 		[EDCA_AC_VO] = { 3,  4, 1, 102 }
314 	},
315 	[IEEE80211_MODE_11A] = {
316 		[EDCA_AC_BK] = { 4, 10, 7,   0 },
317 		[EDCA_AC_BE] = { 4,  6, 3,   0 },
318 		[EDCA_AC_VI] = { 3,  4, 1,  94 },
319 		[EDCA_AC_VO] = { 2,  3, 1,  47 }
320 	},
321 	[IEEE80211_MODE_11G] = {
322 		[EDCA_AC_BK] = { 4, 10, 7,   0 },
323 		[EDCA_AC_BE] = { 4,  6, 3,   0 },
324 		[EDCA_AC_VI] = { 3,  4, 1,  94 },
325 		[EDCA_AC_VO] = { 2,  3, 1,  47 }
326 	},
327 	[IEEE80211_MODE_11N] = {
328 		[EDCA_AC_BK] = { 4, 10, 7,   0 },
329 		[EDCA_AC_BE] = { 4,  6, 3,   0 },
330 		[EDCA_AC_VI] = { 3,  4, 1,  94 },
331 		[EDCA_AC_VO] = { 2,  3, 1,  47 }
332 	},
333 };
334 #endif	/* IEEE80211_STA_ONLY */
335 
336 /*
337  * Return the EDCA Access Category to be used for transmitting a frame with
338  * user-priority `up'.
339  */
340 enum ieee80211_edca_ac
341 ieee80211_up_to_ac(struct ieee80211com *ic, int up)
342 {
343 	/* see Table 9-1 */
344 	static const enum ieee80211_edca_ac up_to_ac[] = {
345 		EDCA_AC_BE,	/* BE */
346 		EDCA_AC_BK,	/* BK */
347 		EDCA_AC_BK,	/* -- */
348 		EDCA_AC_BE,	/* EE */
349 		EDCA_AC_VI,	/* CL */
350 		EDCA_AC_VI,	/* VI */
351 		EDCA_AC_VO,	/* VO */
352 		EDCA_AC_VO	/* NC */
353 	};
354 	enum ieee80211_edca_ac ac;
355 
356 	ac = (up <= 7) ? up_to_ac[up] : EDCA_AC_BE;
357 
358 #ifndef IEEE80211_STA_ONLY
359 	if (ic->ic_opmode == IEEE80211_M_HOSTAP)
360 		return ac;
361 #endif
362 	/*
363 	 * We do not support the admission control procedure defined in
364 	 * IEEE Std 802.11-2012 section 9.19.4.2.3. The spec says that
365 	 * non-AP QSTAs that don't support this procedure shall use EDCA
366 	 * parameters of a lower priority AC that does not require
367 	 * admission control.
368 	 */
369 	while (ac != EDCA_AC_BK && ic->ic_edca_ac[ac].ac_acm) {
370 		switch (ac) {
371 		case EDCA_AC_BK:
372 			/* can't get there */
373 			break;
374 		case EDCA_AC_BE:
375 			/* BE shouldn't require admission control */
376 			ac = EDCA_AC_BK;
377 			break;
378 		case EDCA_AC_VI:
379 			ac = EDCA_AC_BE;
380 			break;
381 		case EDCA_AC_VO:
382 			ac = EDCA_AC_VI;
383 			break;
384 		}
385 	}
386 	return ac;
387 }
388 
389 /*
390  * Get mbuf's user-priority: if mbuf is not VLAN tagged, select user-priority
391  * based on the DSCP (Differentiated Services Codepoint) field.
392  */
393 int
394 ieee80211_classify(struct ieee80211com *ic, struct mbuf *m)
395 {
396 	struct ether_header eh;
397 	u_int8_t ds_field;
398 #if NVLAN > 0
399 	if (m->m_flags & M_VLANTAG)	/* use VLAN 802.1D user-priority */
400 		return EVL_PRIOFTAG(m->m_pkthdr.ether_vtag);
401 #endif
402 	m_copydata(m, 0, sizeof(eh), (caddr_t)&eh);
403 	if (eh.ether_type == htons(ETHERTYPE_IP)) {
404 		struct ip ip;
405 		m_copydata(m, sizeof(eh), sizeof(ip), (caddr_t)&ip);
406 		if (ip.ip_v != 4)
407 			return 0;
408 		ds_field = ip.ip_tos;
409 	}
410 #ifdef INET6
411 	else if (eh.ether_type == htons(ETHERTYPE_IPV6)) {
412 		struct ip6_hdr ip6;
413 		u_int32_t flowlabel;
414 		m_copydata(m, sizeof(eh), sizeof(ip6), (caddr_t)&ip6);
415 		flowlabel = ntohl(ip6.ip6_flow);
416 		if ((flowlabel >> 28) != 6)
417 			return 0;
418 		ds_field = (flowlabel >> 20) & 0xff;
419 	}
420 #endif	/* INET6 */
421 	else	/* neither IPv4 nor IPv6 */
422 		return 0;
423 
424 	/*
425 	 * Map Differentiated Services Codepoint field (see RFC2474).
426 	 * Preserves backward compatibility with IP Precedence field.
427 	 */
428 	switch (ds_field & 0xfc) {
429 	case IPTOS_PREC_PRIORITY:
430 		return EDCA_AC_VI;
431 	case IPTOS_PREC_IMMEDIATE:
432 		return EDCA_AC_BK;
433 	case IPTOS_PREC_FLASH:
434 	case IPTOS_PREC_FLASHOVERRIDE:
435 	case IPTOS_PREC_CRITIC_ECP:
436 	case IPTOS_PREC_INTERNETCONTROL:
437 	case IPTOS_PREC_NETCONTROL:
438 		return EDCA_AC_VO;
439 	default:
440 		return EDCA_AC_BE;
441 	}
442 }
443 
444 int
445 ieee80211_can_use_ampdu(struct ieee80211com *ic, struct ieee80211_node *ni)
446 {
447 	return (ni->ni_flags & IEEE80211_NODE_HT) &&
448 	    (ic->ic_caps & IEEE80211_C_TX_AMPDU) &&
449 	    !(ic->ic_opmode == IEEE80211_M_STA && ni != ic->ic_bss) &&
450 	    /*
451 	     * Don't use A-MPDU on non-encrypted networks. There are devices
452 	     * with buggy firmware which allow an attacker to inject 802.11
453 	     * frames into a wifi network by embedding rouge A-MPDU subframes
454 	     * in an arbitrary data payload (e.g. PNG images) which may end
455 	     * up appearing as actual frames after de-aggregation by a buggy
456 	     * device; see https://github.com/rpp0/aggr-inject for details.
457 	     * WPA2 prevents this injection attack since the attacker would
458 	     * need to inject frames which get decrypted correctly.
459 	     */
460 	    ((ic->ic_flags & IEEE80211_F_RSNON) &&
461 	      (ni->ni_rsnprotos & IEEE80211_PROTO_RSN));
462 }
463 
464 void
465 ieee80211_tx_compressed_bar(struct ieee80211com *ic, struct ieee80211_node *ni,
466     int tid, uint16_t ssn)
467 {
468 	struct ifnet *ifp = &ic->ic_if;
469 	struct mbuf *m;
470 
471 	m = ieee80211_get_compressed_bar(ic, ni, tid, ssn);
472 	if (m == NULL)
473 		return;
474 
475 	ieee80211_ref_node(ni);
476 	if (mq_enqueue(&ic->ic_mgtq, m) == 0)
477 		if_start(ifp);
478 	else
479 		ieee80211_release_node(ic, ni);
480 }
481 
482 /*
483  * Encapsulate an outbound data frame.  The mbuf chain is updated and
484  * a reference to the destination node is returned.  If an error is
485  * encountered NULL is returned and the node reference will also be NULL.
486  *
487  * NB: The caller is responsible for free'ing a returned node reference.
488  *     The convention is ic_bss is not reference counted; the caller must
489  *     maintain that.
490  */
491 struct mbuf *
492 ieee80211_encap(struct ifnet *ifp, struct mbuf *m, struct ieee80211_node **pni)
493 {
494 	struct ieee80211com *ic = (void *)ifp;
495 	struct ether_header eh;
496 	struct ieee80211_frame *wh;
497 	struct ieee80211_node *ni = NULL;
498 	struct llc *llc;
499 	struct m_tag *mtag;
500 	u_int8_t *addr;
501 	u_int dlt, hdrlen;
502 	int addqos, tid;
503 
504 	/* Handle raw frames if mbuf is tagged as 802.11 */
505 	if ((mtag = m_tag_find(m, PACKET_TAG_DLT, NULL)) != NULL) {
506 		dlt = *(u_int *)(mtag + 1);
507 
508 		if (!(dlt == DLT_IEEE802_11 || dlt == DLT_IEEE802_11_RADIO))
509 			goto fallback;
510 
511 		wh = mtod(m, struct ieee80211_frame *);
512 		switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
513 		case IEEE80211_FC1_DIR_NODS:
514 		case IEEE80211_FC1_DIR_FROMDS:
515 			addr = wh->i_addr1;
516 			break;
517 		case IEEE80211_FC1_DIR_DSTODS:
518 		case IEEE80211_FC1_DIR_TODS:
519 			addr = wh->i_addr3;
520 			break;
521 		default:
522 			goto bad;
523 		}
524 
525 		ni = ieee80211_find_txnode(ic, addr);
526 		if (ni == NULL)
527 			ni = ieee80211_ref_node(ic->ic_bss);
528 		if (ni == NULL) {
529 			printf("%s: no node for dst %s, "
530 			    "discard raw tx frame\n", ifp->if_xname,
531 			    ether_sprintf(addr));
532 			ic->ic_stats.is_tx_nonode++;
533 			goto bad;
534 		}
535 		ni->ni_inact = 0;
536 
537 		*pni = ni;
538 		return (m);
539 	}
540 
541  fallback:
542 	if (m->m_len < sizeof(struct ether_header)) {
543 		m = m_pullup(m, sizeof(struct ether_header));
544 		if (m == NULL) {
545 			ic->ic_stats.is_tx_nombuf++;
546 			goto bad;
547 		}
548 	}
549 	memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header));
550 
551 	ni = ieee80211_find_txnode(ic, eh.ether_dhost);
552 	if (ni == NULL) {
553 		DPRINTF(("no node for dst %s, discard frame\n",
554 		    ether_sprintf(eh.ether_dhost)));
555 		ic->ic_stats.is_tx_nonode++;
556 		goto bad;
557 	}
558 
559 	if ((ic->ic_flags & IEEE80211_F_RSNON) &&
560 	    !ni->ni_port_valid &&
561 	    eh.ether_type != htons(ETHERTYPE_PAE)) {
562 		DPRINTF(("port not valid: %s\n",
563 		    ether_sprintf(eh.ether_dhost)));
564 		ic->ic_stats.is_tx_noauth++;
565 		goto bad;
566 	}
567 
568 	if ((ic->ic_flags & IEEE80211_F_COUNTERM) &&
569 	    ni->ni_rsncipher == IEEE80211_CIPHER_TKIP)
570 		/* XXX TKIP countermeasures! */;
571 
572 	ni->ni_inact = 0;
573 
574 	if ((ic->ic_flags & IEEE80211_F_QOS) &&
575 	    (ni->ni_flags & IEEE80211_NODE_QOS) &&
576 	    /* do not QoS-encapsulate EAPOL frames */
577 	    eh.ether_type != htons(ETHERTYPE_PAE)) {
578 		struct ieee80211_tx_ba *ba;
579 		tid = ieee80211_classify(ic, m);
580 		ba = &ni->ni_tx_ba[tid];
581 		/* We use QoS data frames for aggregation only. */
582 		if (ba->ba_state != IEEE80211_BA_AGREED) {
583 			hdrlen = sizeof(struct ieee80211_frame);
584 			addqos = 0;
585 			if (ieee80211_can_use_ampdu(ic, ni))
586 				ieee80211_node_trigger_addba_req(ni, tid);
587 		} else {
588 			hdrlen = sizeof(struct ieee80211_qosframe);
589 			addqos = 1;
590 		}
591 	} else {
592 		hdrlen = sizeof(struct ieee80211_frame);
593 		addqos = 0;
594 	}
595 	m_adj(m, sizeof(struct ether_header) - LLC_SNAPFRAMELEN);
596 	llc = mtod(m, struct llc *);
597 	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
598 	llc->llc_control = LLC_UI;
599 	llc->llc_snap.org_code[0] = 0;
600 	llc->llc_snap.org_code[1] = 0;
601 	llc->llc_snap.org_code[2] = 0;
602 	llc->llc_snap.ether_type = eh.ether_type;
603 	M_PREPEND(m, hdrlen, M_DONTWAIT);
604 	if (m == NULL) {
605 		ic->ic_stats.is_tx_nombuf++;
606 		goto bad;
607 	}
608 	wh = mtod(m, struct ieee80211_frame *);
609 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
610 	*(u_int16_t *)&wh->i_dur[0] = 0;
611 	if (addqos) {
612 		struct ieee80211_qosframe *qwh =
613 		    (struct ieee80211_qosframe *)wh;
614 		u_int16_t qos = tid;
615 
616 		if (ic->ic_tid_noack & (1 << tid))
617 			qos |= IEEE80211_QOS_ACK_POLICY_NOACK;
618 		else {
619 			/* Use HT immediate block-ack. */
620 			qos |= IEEE80211_QOS_ACK_POLICY_NORMAL;
621 		}
622 		qwh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
623 		*(u_int16_t *)qwh->i_qos = htole16(qos);
624 		*(u_int16_t *)qwh->i_seq =
625 		    htole16(ni->ni_qos_txseqs[tid] << IEEE80211_SEQ_SEQ_SHIFT);
626 		ni->ni_qos_txseqs[tid]++;
627 	} else {
628 		*(u_int16_t *)&wh->i_seq[0] =
629 		    htole16(ni->ni_txseq << IEEE80211_SEQ_SEQ_SHIFT);
630 		ni->ni_txseq++;
631 	}
632 	switch (ic->ic_opmode) {
633 	case IEEE80211_M_STA:
634 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
635 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
636 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
637 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
638 		break;
639 #ifndef IEEE80211_STA_ONLY
640 	case IEEE80211_M_IBSS:
641 	case IEEE80211_M_AHDEMO:
642 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
643 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
644 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
645 		IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_bss->ni_bssid);
646 		break;
647 	case IEEE80211_M_HOSTAP:
648 		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
649 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
650 		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
651 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
652 		break;
653 #endif
654 	default:
655 		/* should not get there */
656 		goto bad;
657 	}
658 
659 	if ((ic->ic_flags & IEEE80211_F_WEPON) ||
660 	    ((ic->ic_flags & IEEE80211_F_RSNON) &&
661 	     (ni->ni_flags & IEEE80211_NODE_TXPROT)))
662 		wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
663 
664 #ifndef IEEE80211_STA_ONLY
665 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
666 	    ieee80211_pwrsave(ic, m, ni) != 0) {
667 		*pni = NULL;
668 		return NULL;
669 	}
670 #endif
671 	*pni = ni;
672 	return m;
673 bad:
674 	m_freem(m);
675 	if (ni != NULL)
676 		ieee80211_release_node(ic, ni);
677 	*pni = NULL;
678 	return NULL;
679 }
680 
681 /*
682  * Add a Capability Information field to a frame (see 7.3.1.4).
683  */
684 u_int8_t *
685 ieee80211_add_capinfo(u_int8_t *frm, struct ieee80211com *ic,
686     const struct ieee80211_node *ni)
687 {
688 	u_int16_t capinfo;
689 
690 #ifndef IEEE80211_STA_ONLY
691 	if (ic->ic_opmode == IEEE80211_M_IBSS)
692 		capinfo = IEEE80211_CAPINFO_IBSS;
693 	else if (ic->ic_opmode == IEEE80211_M_HOSTAP)
694 		capinfo = IEEE80211_CAPINFO_ESS;
695 	else
696 #endif
697 		capinfo = 0;
698 #ifndef IEEE80211_STA_ONLY
699 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
700 	    (ic->ic_flags & (IEEE80211_F_WEPON | IEEE80211_F_RSNON)))
701 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
702 #endif
703 	/* NB: some 11a AP's reject the request when short preamble is set */
704 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
705 	    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
706 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
707 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
708 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
709 	LE_WRITE_2(frm, capinfo);
710 	return frm + 2;
711 }
712 
713 /*
714  * Add an SSID element to a frame (see 7.3.2.1).
715  */
716 u_int8_t *
717 ieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len)
718 {
719 	*frm++ = IEEE80211_ELEMID_SSID;
720 	*frm++ = len;
721 	memcpy(frm, ssid, len);
722 	return frm + len;
723 }
724 
725 /*
726  * Add a supported rates element to a frame (see 7.3.2.2).
727  */
728 u_int8_t *
729 ieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs)
730 {
731 	int nrates;
732 
733 	*frm++ = IEEE80211_ELEMID_RATES;
734 	nrates = min(rs->rs_nrates, IEEE80211_RATE_SIZE);
735 	*frm++ = nrates;
736 	memcpy(frm, rs->rs_rates, nrates);
737 	return frm + nrates;
738 }
739 
740 #ifndef IEEE80211_STA_ONLY
741 /*
742  * Add a DS Parameter Set element to a frame (see 7.3.2.4).
743  */
744 u_int8_t *
745 ieee80211_add_ds_params(u_int8_t *frm, struct ieee80211com *ic,
746     const struct ieee80211_node *ni)
747 {
748 	*frm++ = IEEE80211_ELEMID_DSPARMS;
749 	*frm++ = 1;
750 	*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
751 	return frm;
752 }
753 
754 /*
755  * Add a TIM element to a frame (see 7.3.2.6 and Annex L).
756  */
757 u_int8_t *
758 ieee80211_add_tim(u_int8_t *frm, struct ieee80211com *ic)
759 {
760 	u_int i, offset = 0, len;
761 
762 	/* find first non-zero octet in the virtual bit map */
763 	for (i = 0; i < ic->ic_tim_len && ic->ic_tim_bitmap[i] == 0; i++);
764 
765 	/* clear the lsb as it is reserved for the broadcast indication bit */
766 	if (i < ic->ic_tim_len)
767 		offset = i & ~1;
768 
769 	/* find last non-zero octet in the virtual bit map */
770 	for (i = ic->ic_tim_len - 1; i > 0 && ic->ic_tim_bitmap[i] == 0; i--);
771 
772 	len = i - offset + 1;
773 
774 	*frm++ = IEEE80211_ELEMID_TIM;
775 	*frm++ = len + 3;		/* length */
776 	*frm++ = ic->ic_dtim_count;	/* DTIM count */
777 	*frm++ = ic->ic_dtim_period;	/* DTIM period */
778 
779 	/* Bitmap Control */
780 	*frm = offset;
781 	/* set broadcast/multicast indication bit if necessary */
782 	if (ic->ic_dtim_count == 0 && ic->ic_tim_mcast_pending)
783 		*frm |= 0x01;
784 	frm++;
785 
786 	/* Partial Virtual Bitmap */
787 	memcpy(frm, &ic->ic_tim_bitmap[offset], len);
788 	return frm + len;
789 }
790 
791 /*
792  * Add an IBSS Parameter Set element to a frame (see 7.3.2.7).
793  */
794 u_int8_t *
795 ieee80211_add_ibss_params(u_int8_t *frm, const struct ieee80211_node *ni)
796 {
797 	*frm++ = IEEE80211_ELEMID_IBSSPARMS;
798 	*frm++ = 2;
799 	LE_WRITE_2(frm, 0);	/* TODO: ATIM window */
800 	return frm + 2;
801 }
802 
803 /*
804  * Add an EDCA Parameter Set element to a frame (see 7.3.2.29).
805  */
806 u_int8_t *
807 ieee80211_add_edca_params(u_int8_t *frm, struct ieee80211com *ic)
808 {
809 	const struct ieee80211_edca_ac_params *edca;
810 	int aci;
811 
812 	*frm++ = IEEE80211_ELEMID_EDCAPARMS;
813 	*frm++ = 18;	/* length */
814 	*frm++ = 0;	/* QoS Info */
815 	*frm++ = 0;	/* reserved */
816 
817 	/* setup AC Parameter Records */
818 	edca = ieee80211_edca_table[ic->ic_curmode];
819 	for (aci = 0; aci < EDCA_NUM_AC; aci++) {
820 		const struct ieee80211_edca_ac_params *ac = &edca[aci];
821 
822 		*frm++ = (aci << 5) | ((ac->ac_acm & 0x1) << 4) |
823 			 (ac->ac_aifsn & 0xf);
824 		*frm++ = (ac->ac_ecwmax << 4) |
825 			 (ac->ac_ecwmin & 0xf);
826 		LE_WRITE_2(frm, ac->ac_txoplimit); frm += 2;
827 	}
828 	return frm;
829 }
830 
831 /*
832  * Add an ERP element to a frame (see 7.3.2.13).
833  */
834 u_int8_t *
835 ieee80211_add_erp(u_int8_t *frm, struct ieee80211com *ic)
836 {
837 	u_int8_t erp;
838 	int nonerpsta = 0;
839 
840 	*frm++ = IEEE80211_ELEMID_ERP;
841 	*frm++ = 1;
842 	erp = 0;
843 	/*
844 	 * The NonERP_Present bit shall be set to 1 when a NonERP STA
845 	 * is associated with the BSS.
846 	 */
847 	ieee80211_iterate_nodes(ic, ieee80211_count_nonerpsta, &nonerpsta);
848 	if (nonerpsta != 0)
849 		erp |= IEEE80211_ERP_NON_ERP_PRESENT;
850 	/*
851 	 * If one or more NonERP STAs are associated in the BSS, the
852 	 * Use_Protection bit shall be set to 1 in transmitted ERP
853 	 * Information Elements.
854 	 */
855 	if (ic->ic_flags & IEEE80211_F_USEPROT)
856 		erp |= IEEE80211_ERP_USE_PROTECTION;
857 	/*
858 	 * The Barker_Preamble_Mode bit shall be set to 1 by the ERP
859 	 * Information Element sender if one or more associated NonERP
860 	 * STAs are not short preamble capable.
861 	 */
862 	if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE))
863 		erp |= IEEE80211_ERP_BARKER_MODE;
864 	*frm++ = erp;
865 	return frm;
866 }
867 #endif	/* IEEE80211_STA_ONLY */
868 
869 /*
870  * Add a QoS Capability element to a frame (see 7.3.2.35).
871  */
872 u_int8_t *
873 ieee80211_add_qos_capability(u_int8_t *frm, struct ieee80211com *ic)
874 {
875 	*frm++ = IEEE80211_ELEMID_QOS_CAP;
876 	*frm++ = 1;
877 	*frm++ = 0;	/* QoS Info */
878 	return frm;
879 }
880 
881 /*
882  * Add a Wifi-Alliance WME (aka WMM) info element to a frame.
883  * WME is a requirement for Wifi-Alliance compliance and some
884  * 11n APs will not negotiate HT if this element is missing.
885  */
886 uint8_t *
887 ieee80211_add_wme_info(uint8_t *frm, struct ieee80211com *ic)
888 {
889 	*frm++ = IEEE80211_ELEMID_VENDOR;
890 	*frm++ = 7;
891 	memcpy(frm, MICROSOFT_OUI, 3); frm += 3;
892 	*frm++ = 2; /* OUI type */
893 	*frm++ = 0; /* OUI subtype */
894 	*frm++ = 1; /* version */
895 	*frm++ = 0; /* info */
896 
897 	return frm;
898 }
899 
900 #ifndef IEEE80211_STA_ONLY
901 /*
902  * Add a Wifi-Alliance WMM (aka WME) parameter element to a frame.
903  */
904 uint8_t *
905 ieee80211_add_wme_param(uint8_t *frm, struct ieee80211com *ic)
906 {
907 	const struct ieee80211_edca_ac_params *edca;
908 	int aci;
909 
910 	*frm++ = IEEE80211_ELEMID_VENDOR;
911 	*frm++ = 24;
912 	memcpy(frm, MICROSOFT_OUI, 3); frm += 3;
913 	*frm++ = 2; /* OUI type */
914 	*frm++ = 1; /* OUI subtype */
915 	*frm++ = 1; /* version */
916 	*frm++ = 0; /* info */
917 	*frm++ = 0; /* reserved */
918 
919 	/* setup AC Parameter Records */
920 	edca = ieee80211_edca_table[ic->ic_curmode];
921 	for (aci = 0; aci < EDCA_NUM_AC; aci++) {
922 		const struct ieee80211_edca_ac_params *ac = &edca[aci];
923 
924 		*frm++ = (aci << 5) | ((ac->ac_acm & 0x1) << 4) |
925 			 (ac->ac_aifsn & 0xf);
926 		*frm++ = (ac->ac_ecwmax << 4) |
927 			 (ac->ac_ecwmin & 0xf);
928 		LE_WRITE_2(frm, ac->ac_txoplimit); frm += 2;
929 	}
930 
931 	return frm;
932 }
933 #endif
934 
935 /*
936  * Add an RSN element to a frame (see 802.11-2012 8.4.2.27)
937  */
938 u_int8_t *
939 ieee80211_add_rsn_body(u_int8_t *frm, struct ieee80211com *ic,
940     const struct ieee80211_node *ni, int wpa)
941 {
942 	const u_int8_t *oui = wpa ? MICROSOFT_OUI : IEEE80211_OUI;
943 	u_int8_t *pcount;
944 	u_int16_t count;
945 
946 	/* write Version field */
947 	LE_WRITE_2(frm, 1); frm += 2;
948 
949 	/* write Group Data Cipher Suite field (see 802.11-2012 Table 8-99) */
950 	memcpy(frm, oui, 3); frm += 3;
951 	switch (ni->ni_rsngroupcipher) {
952 	case IEEE80211_CIPHER_WEP40:
953 		*frm++ = 1;
954 		break;
955 	case IEEE80211_CIPHER_TKIP:
956 		*frm++ = 2;
957 		break;
958 	case IEEE80211_CIPHER_CCMP:
959 		*frm++ = 4;
960 		break;
961 	case IEEE80211_CIPHER_WEP104:
962 		*frm++ = 5;
963 		break;
964 	default:
965 		/* can't get there */
966 		panic("invalid group data cipher!");
967 	}
968 
969 	pcount = frm; frm += 2;
970 	count = 0;
971 	/* write Pairwise Cipher Suite List */
972 	if (ni->ni_rsnciphers & IEEE80211_CIPHER_USEGROUP) {
973 		memcpy(frm, oui, 3); frm += 3;
974 		*frm++ = 0;
975 		count++;
976 	}
977 	if (ni->ni_rsnciphers & IEEE80211_CIPHER_TKIP) {
978 		memcpy(frm, oui, 3); frm += 3;
979 		*frm++ = 2;
980 		count++;
981 	}
982 	if (ni->ni_rsnciphers & IEEE80211_CIPHER_CCMP) {
983 		memcpy(frm, oui, 3); frm += 3;
984 		*frm++ = 4;
985 		count++;
986 	}
987 	/* write Pairwise Cipher Suite Count field */
988 	LE_WRITE_2(pcount, count);
989 
990 	pcount = frm; frm += 2;
991 	count = 0;
992 	/* write AKM Suite List (see Table 20dc) */
993 	if (ni->ni_rsnakms & IEEE80211_AKM_8021X) {
994 		memcpy(frm, oui, 3); frm += 3;
995 		*frm++ = 1;
996 		count++;
997 	}
998 	if (ni->ni_rsnakms & IEEE80211_AKM_PSK) {
999 		memcpy(frm, oui, 3); frm += 3;
1000 		*frm++ = 2;
1001 		count++;
1002 	}
1003 	if (!wpa && (ni->ni_rsnakms & IEEE80211_AKM_SHA256_8021X)) {
1004 		memcpy(frm, oui, 3); frm += 3;
1005 		*frm++ = 5;
1006 		count++;
1007 	}
1008 	if (!wpa && (ni->ni_rsnakms & IEEE80211_AKM_SHA256_PSK)) {
1009 		memcpy(frm, oui, 3); frm += 3;
1010 		*frm++ = 6;
1011 		count++;
1012 	}
1013 	/* write AKM Suite List Count field */
1014 	LE_WRITE_2(pcount, count);
1015 
1016 	if (wpa)
1017 		return frm;
1018 
1019 	/* write RSN Capabilities field */
1020 	LE_WRITE_2(frm, ni->ni_rsncaps); frm += 2;
1021 
1022 	if (ni->ni_flags & IEEE80211_NODE_PMKID) {
1023 		/* write PMKID Count field */
1024 		LE_WRITE_2(frm, 1); frm += 2;
1025 		/* write PMKID List (only 1) */
1026 		memcpy(frm, ni->ni_pmkid, IEEE80211_PMKID_LEN);
1027 		frm += IEEE80211_PMKID_LEN;
1028 	}
1029 
1030 	if (!(ic->ic_caps & IEEE80211_C_MFP))
1031 		return frm;
1032 
1033 	if ((ni->ni_flags & IEEE80211_NODE_PMKID) == 0) {
1034 		/* no PMKID (PMKID Count=0) */
1035 		LE_WRITE_2(frm, 0); frm += 2;
1036 	}
1037 
1038 	/* write Group Integrity Cipher Suite field */
1039 	memcpy(frm, oui, 3); frm += 3;
1040 	switch (ic->ic_rsngroupmgmtcipher) {
1041 	case IEEE80211_CIPHER_BIP:
1042 		*frm++ = 6;
1043 		break;
1044 	default:
1045 		/* can't get there */
1046 		panic("invalid integrity group cipher!");
1047 	}
1048 	return frm;
1049 }
1050 
1051 u_int8_t *
1052 ieee80211_add_rsn(u_int8_t *frm, struct ieee80211com *ic,
1053     const struct ieee80211_node *ni)
1054 {
1055 	u_int8_t *plen;
1056 
1057 	*frm++ = IEEE80211_ELEMID_RSN;
1058 	plen = frm++;	/* length filled in later */
1059 	frm = ieee80211_add_rsn_body(frm, ic, ni, 0);
1060 
1061 	/* write length field */
1062 	*plen = frm - plen - 1;
1063 	return frm;
1064 }
1065 
1066 /*
1067  * Add a vendor-specific WPA element to a frame.
1068  * This is required for compatibility with Wi-Fi Alliance WPA.
1069  */
1070 u_int8_t *
1071 ieee80211_add_wpa(u_int8_t *frm, struct ieee80211com *ic,
1072     const struct ieee80211_node *ni)
1073 {
1074 	u_int8_t *plen;
1075 
1076 	*frm++ = IEEE80211_ELEMID_VENDOR;
1077 	plen = frm++;	/* length filled in later */
1078 	memcpy(frm, MICROSOFT_OUI, 3); frm += 3;
1079 	*frm++ = 1;	/* WPA */
1080 	frm = ieee80211_add_rsn_body(frm, ic, ni, 1);
1081 
1082 	/* write length field */
1083 	*plen = frm - plen - 1;
1084 	return frm;
1085 }
1086 
1087 /*
1088  * Add an extended supported rates element to a frame (see 7.3.2.14).
1089  */
1090 u_int8_t *
1091 ieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs)
1092 {
1093 	int nrates;
1094 
1095 	KASSERT(rs->rs_nrates > IEEE80211_RATE_SIZE);
1096 
1097 	*frm++ = IEEE80211_ELEMID_XRATES;
1098 	nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
1099 	*frm++ = nrates;
1100 	memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
1101 	return frm + nrates;
1102 }
1103 
1104 /*
1105  * Add an HT Capabilities element to a frame (see 7.3.2.57).
1106  */
1107 u_int8_t *
1108 ieee80211_add_htcaps(u_int8_t *frm, struct ieee80211com *ic)
1109 {
1110 	*frm++ = IEEE80211_ELEMID_HTCAPS;
1111 	*frm++ = 26;
1112 	LE_WRITE_2(frm, ic->ic_htcaps); frm += 2;
1113 	*frm++ = ic->ic_ampdu_params;
1114 	memcpy(frm, ic->ic_sup_mcs, 10); frm += 10;
1115 	LE_WRITE_2(frm, (ic->ic_max_rxrate & IEEE80211_MCS_RX_RATE_HIGH));
1116 	frm += 2;
1117 	*frm++ = ic->ic_tx_mcs_set;
1118 	*frm++ = 0; /* reserved */
1119 	*frm++ = 0; /* reserved */
1120 	*frm++ = 0; /* reserved */
1121 	LE_WRITE_2(frm, ic->ic_htxcaps); frm += 2;
1122 	LE_WRITE_4(frm, ic->ic_txbfcaps); frm += 4;
1123 	*frm++ = ic->ic_aselcaps;
1124 	return frm;
1125 }
1126 
1127 #ifndef IEEE80211_STA_ONLY
1128 /*
1129  * Add an HT Operation element to a frame (see 7.3.2.58).
1130  */
1131 u_int8_t *
1132 ieee80211_add_htop(u_int8_t *frm, struct ieee80211com *ic)
1133 {
1134 	*frm++ = IEEE80211_ELEMID_HTOP;
1135 	*frm++ = 22;
1136 	*frm++ = ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan);
1137 	*frm++ = ic->ic_bss->ni_htop0;
1138 	LE_WRITE_2(frm, ic->ic_bss->ni_htop1); frm += 2;
1139 	LE_WRITE_2(frm, ic->ic_bss->ni_htop2); frm += 2;
1140 	memset(frm, 0, 16); frm += 16;
1141 	return frm;
1142 }
1143 #endif	/* !IEEE80211_STA_ONLY */
1144 
1145 #ifndef IEEE80211_STA_ONLY
1146 /*
1147  * Add a Timeout Interval element to a frame (see 7.3.2.49).
1148  */
1149 u_int8_t *
1150 ieee80211_add_tie(u_int8_t *frm, u_int8_t type, u_int32_t value)
1151 {
1152 	*frm++ = IEEE80211_ELEMID_TIE;
1153 	*frm++ = 5;	/* length */
1154 	*frm++ = type;	/* Timeout Interval type */
1155 	LE_WRITE_4(frm, value);
1156 	return frm + 4;
1157 }
1158 #endif
1159 
1160 struct mbuf *
1161 ieee80211_getmgmt(int flags, int type, u_int pktlen)
1162 {
1163 	struct mbuf *m;
1164 
1165 	/* reserve space for 802.11 header */
1166 	pktlen += sizeof(struct ieee80211_frame);
1167 
1168 	if (pktlen > MCLBYTES)
1169 		panic("management frame too large: %u", pktlen);
1170 	MGETHDR(m, flags, type);
1171 	if (m == NULL)
1172 		return NULL;
1173 	if (pktlen > MHLEN) {
1174 		MCLGET(m, flags);
1175 		if (!(m->m_flags & M_EXT))
1176 			return m_free(m);
1177 	}
1178 	m->m_data += sizeof(struct ieee80211_frame);
1179 	return m;
1180 }
1181 
1182 /*-
1183  * Probe request frame format:
1184  * [tlv] SSID
1185  * [tlv] Supported rates
1186  * [tlv] Extended Supported Rates (802.11g)
1187  * [tlv] HT Capabilities (802.11n)
1188  */
1189 struct mbuf *
1190 ieee80211_get_probe_req(struct ieee80211com *ic, struct ieee80211_node *ni)
1191 {
1192 	const struct ieee80211_rateset *rs =
1193 	    &ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
1194 	struct mbuf *m;
1195 	u_int8_t *frm;
1196 
1197 	m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA,
1198 	    2 + ic->ic_des_esslen +
1199 	    2 + min(rs->rs_nrates, IEEE80211_RATE_SIZE) +
1200 	    ((rs->rs_nrates > IEEE80211_RATE_SIZE) ?
1201 		2 + rs->rs_nrates - IEEE80211_RATE_SIZE : 0) +
1202 	    ((ic->ic_flags & IEEE80211_F_HTON) ? 28 + 9 : 0));
1203 	if (m == NULL)
1204 		return NULL;
1205 
1206 	frm = mtod(m, u_int8_t *);
1207 	frm = ieee80211_add_ssid(frm, ic->ic_des_essid, ic->ic_des_esslen);
1208 	frm = ieee80211_add_rates(frm, rs);
1209 	if (rs->rs_nrates > IEEE80211_RATE_SIZE)
1210 		frm = ieee80211_add_xrates(frm, rs);
1211 	if (ic->ic_flags & IEEE80211_F_HTON) {
1212 		frm = ieee80211_add_htcaps(frm, ic);
1213 		frm = ieee80211_add_wme_info(frm, ic);
1214 	}
1215 
1216 	m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1217 
1218 	return m;
1219 }
1220 
1221 #ifndef IEEE80211_STA_ONLY
1222 /*-
1223  * Probe response frame format:
1224  * [8]   Timestamp
1225  * [2]   Beacon interval
1226  * [2]   Capability
1227  * [tlv] Service Set Identifier (SSID)
1228  * [tlv] Supported rates
1229  * [tlv] DS Parameter Set (802.11g)
1230  * [tlv] ERP Information (802.11g)
1231  * [tlv] Extended Supported Rates (802.11g)
1232  * [tlv] RSN (802.11i)
1233  * [tlv] EDCA Parameter Set (802.11e)
1234  * [tlv] HT Capabilities (802.11n)
1235  * [tlv] HT Operation (802.11n)
1236  */
1237 struct mbuf *
1238 ieee80211_get_probe_resp(struct ieee80211com *ic)
1239 {
1240 	const struct ieee80211_rateset *rs = &ic->ic_bss->ni_rates;
1241 	struct mbuf *m;
1242 	u_int8_t *frm;
1243 
1244 	m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA,
1245 	    8 + 2 + 2 +
1246 	    2 + ic->ic_bss->ni_esslen +
1247 	    2 + min(rs->rs_nrates, IEEE80211_RATE_SIZE) +
1248 	    2 + 1 +
1249 	    ((ic->ic_opmode == IEEE80211_M_IBSS) ? 2 + 2 : 0) +
1250 	    ((ic->ic_curmode == IEEE80211_MODE_11G) ? 2 + 1 : 0) +
1251 	    ((rs->rs_nrates > IEEE80211_RATE_SIZE) ?
1252 		2 + rs->rs_nrates - IEEE80211_RATE_SIZE : 0) +
1253 	    (((ic->ic_flags & IEEE80211_F_RSNON) &&
1254 	      (ic->ic_bss->ni_rsnprotos & IEEE80211_PROTO_RSN)) ?
1255 		2 + IEEE80211_RSNIE_MAXLEN : 0) +
1256 	    ((ic->ic_flags & IEEE80211_F_QOS) ? 2 + 18 : 0) +
1257 	    (((ic->ic_flags & IEEE80211_F_RSNON) &&
1258 	      (ic->ic_bss->ni_rsnprotos & IEEE80211_PROTO_WPA)) ?
1259 		2 + IEEE80211_WPAIE_MAXLEN : 0) +
1260 	    ((ic->ic_flags & IEEE80211_F_HTON) ? 28 + 24 + 26 : 0));
1261 	if (m == NULL)
1262 		return NULL;
1263 
1264 	frm = mtod(m, u_int8_t *);
1265 	memset(frm, 0, 8); frm += 8;	/* timestamp is set by hardware */
1266 	LE_WRITE_2(frm, ic->ic_bss->ni_intval); frm += 2;
1267 	frm = ieee80211_add_capinfo(frm, ic, ic->ic_bss);
1268 	frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid,
1269 	    ic->ic_bss->ni_esslen);
1270 	frm = ieee80211_add_rates(frm, rs);
1271 	frm = ieee80211_add_ds_params(frm, ic, ic->ic_bss);
1272 	if (ic->ic_opmode == IEEE80211_M_IBSS)
1273 		frm = ieee80211_add_ibss_params(frm, ic->ic_bss);
1274 	if (ic->ic_curmode == IEEE80211_MODE_11G)
1275 		frm = ieee80211_add_erp(frm, ic);
1276 	if (rs->rs_nrates > IEEE80211_RATE_SIZE)
1277 		frm = ieee80211_add_xrates(frm, rs);
1278 	if ((ic->ic_flags & IEEE80211_F_RSNON) &&
1279 	    (ic->ic_bss->ni_rsnprotos & IEEE80211_PROTO_RSN))
1280 		frm = ieee80211_add_rsn(frm, ic, ic->ic_bss);
1281 	if (ic->ic_flags & IEEE80211_F_QOS)
1282 		frm = ieee80211_add_edca_params(frm, ic);
1283 	if ((ic->ic_flags & IEEE80211_F_RSNON) &&
1284 	    (ic->ic_bss->ni_rsnprotos & IEEE80211_PROTO_WPA))
1285 		frm = ieee80211_add_wpa(frm, ic, ic->ic_bss);
1286 	if (ic->ic_flags & IEEE80211_F_HTON) {
1287 		frm = ieee80211_add_htcaps(frm, ic);
1288 		frm = ieee80211_add_htop(frm, ic);
1289 		frm = ieee80211_add_wme_param(frm, ic);
1290 	}
1291 
1292 	m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1293 
1294 	return m;
1295 }
1296 #endif	/* IEEE80211_STA_ONLY */
1297 
1298 /*-
1299  * Authentication frame format:
1300  * [2] Authentication algorithm number
1301  * [2] Authentication transaction sequence number
1302  * [2] Status code
1303  */
1304 struct mbuf *
1305 ieee80211_get_auth(struct ieee80211com *ic, struct ieee80211_node *ni,
1306     u_int16_t status, u_int16_t seq)
1307 {
1308 	struct mbuf *m;
1309 	u_int8_t *frm;
1310 
1311 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1312 	if (m == NULL)
1313 		return NULL;
1314 	m_align(m, 2 * 3);
1315 	m->m_pkthdr.len = m->m_len = 2 * 3;
1316 
1317 	frm = mtod(m, u_int8_t *);
1318 	LE_WRITE_2(frm, IEEE80211_AUTH_ALG_OPEN); frm += 2;
1319 	LE_WRITE_2(frm, seq); frm += 2;
1320 	LE_WRITE_2(frm, status);
1321 
1322 	return m;
1323 }
1324 
1325 /*-
1326  * Deauthentication frame format:
1327  * [2] Reason code
1328  */
1329 struct mbuf *
1330 ieee80211_get_deauth(struct ieee80211com *ic, struct ieee80211_node *ni,
1331     u_int16_t reason)
1332 {
1333 	struct mbuf *m;
1334 
1335 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1336 	if (m == NULL)
1337 		return NULL;
1338 	m_align(m, 2);
1339 	m->m_pkthdr.len = m->m_len = 2;
1340 
1341 	*mtod(m, u_int16_t *) = htole16(reason);
1342 
1343 	return m;
1344 }
1345 
1346 /*-
1347  * (Re)Association request frame format:
1348  * [2]   Capability information
1349  * [2]   Listen interval
1350  * [6*]  Current AP address (Reassociation only)
1351  * [tlv] SSID
1352  * [tlv] Supported rates
1353  * [tlv] Extended Supported Rates (802.11g)
1354  * [tlv] RSN (802.11i)
1355  * [tlv] QoS Capability (802.11e)
1356  * [tlv] HT Capabilities (802.11n)
1357  */
1358 struct mbuf *
1359 ieee80211_get_assoc_req(struct ieee80211com *ic, struct ieee80211_node *ni,
1360     int type)
1361 {
1362 	const struct ieee80211_rateset *rs = &ni->ni_rates;
1363 	struct mbuf *m;
1364 	u_int8_t *frm;
1365 	u_int16_t capinfo;
1366 
1367 	m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA,
1368 	    2 + 2 +
1369 	    ((type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) ?
1370 		IEEE80211_ADDR_LEN : 0) +
1371 	    2 + ni->ni_esslen +
1372 	    2 + min(rs->rs_nrates, IEEE80211_RATE_SIZE) +
1373 	    ((rs->rs_nrates > IEEE80211_RATE_SIZE) ?
1374 		2 + rs->rs_nrates - IEEE80211_RATE_SIZE : 0) +
1375 	    (((ic->ic_flags & IEEE80211_F_RSNON) &&
1376 	      (ni->ni_rsnprotos & IEEE80211_PROTO_RSN)) ?
1377 		2 + IEEE80211_RSNIE_MAXLEN : 0) +
1378 	    ((ni->ni_flags & IEEE80211_NODE_QOS) ? 2 + 1 : 0) +
1379 	    (((ic->ic_flags & IEEE80211_F_RSNON) &&
1380 	      (ni->ni_rsnprotos & IEEE80211_PROTO_WPA)) ?
1381 		2 + IEEE80211_WPAIE_MAXLEN : 0) +
1382 	    ((ic->ic_flags & IEEE80211_F_HTON) ? 28 + 9 : 0));
1383 	if (m == NULL)
1384 		return NULL;
1385 
1386 	frm = mtod(m, u_int8_t *);
1387 	capinfo = IEEE80211_CAPINFO_ESS;
1388 	if (ic->ic_flags & IEEE80211_F_WEPON)
1389 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
1390 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1391 	    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1392 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1393 	if (ic->ic_caps & IEEE80211_C_SHSLOT)
1394 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1395 	LE_WRITE_2(frm, capinfo); frm += 2;
1396 	LE_WRITE_2(frm, ic->ic_lintval); frm += 2;
1397 	if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1398 		IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid);
1399 		frm += IEEE80211_ADDR_LEN;
1400 	}
1401 	frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
1402 	frm = ieee80211_add_rates(frm, rs);
1403 	if (rs->rs_nrates > IEEE80211_RATE_SIZE)
1404 		frm = ieee80211_add_xrates(frm, rs);
1405 	if ((ic->ic_flags & IEEE80211_F_RSNON) &&
1406 	    (ni->ni_rsnprotos & IEEE80211_PROTO_RSN))
1407 		frm = ieee80211_add_rsn(frm, ic, ni);
1408 	if (ni->ni_flags & IEEE80211_NODE_QOS)
1409 		frm = ieee80211_add_qos_capability(frm, ic);
1410 	if ((ic->ic_flags & IEEE80211_F_RSNON) &&
1411 	    (ni->ni_rsnprotos & IEEE80211_PROTO_WPA))
1412 		frm = ieee80211_add_wpa(frm, ic, ni);
1413 	if (ic->ic_flags & IEEE80211_F_HTON) {
1414 		frm = ieee80211_add_htcaps(frm, ic);
1415 		frm = ieee80211_add_wme_info(frm, ic);
1416 	}
1417 
1418 	m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1419 
1420 	return m;
1421 }
1422 
1423 #ifndef IEEE80211_STA_ONLY
1424 /*-
1425  * (Re)Association response frame format:
1426  * [2]   Capability information
1427  * [2]   Status code
1428  * [2]   Association ID (AID)
1429  * [tlv] Supported rates
1430  * [tlv] Extended Supported Rates (802.11g)
1431  * [tlv] EDCA Parameter Set (802.11e)
1432  * [tlv] Timeout Interval (802.11w)
1433  * [tlv] HT Capabilities (802.11n)
1434  * [tlv] HT Operation (802.11n)
1435  */
1436 struct mbuf *
1437 ieee80211_get_assoc_resp(struct ieee80211com *ic, struct ieee80211_node *ni,
1438     u_int16_t status)
1439 {
1440 	const struct ieee80211_rateset *rs = &ni->ni_rates;
1441 	struct mbuf *m;
1442 	u_int8_t *frm;
1443 
1444 	m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA,
1445 	    2 + 2 + 2 +
1446 	    2 + min(rs->rs_nrates, IEEE80211_RATE_SIZE) +
1447 	    ((rs->rs_nrates > IEEE80211_RATE_SIZE) ?
1448 		2 + rs->rs_nrates - IEEE80211_RATE_SIZE : 0) +
1449 	    ((ni->ni_flags & IEEE80211_NODE_QOS) ? 2 + 18 : 0) +
1450 	    ((status == IEEE80211_STATUS_TRY_AGAIN_LATER) ? 2 + 7 : 0) +
1451 	    ((ic->ic_flags & IEEE80211_F_HTON) ? 28 + 24 + 26 : 0));
1452 	if (m == NULL)
1453 		return NULL;
1454 
1455 	frm = mtod(m, u_int8_t *);
1456 	frm = ieee80211_add_capinfo(frm, ic, ni);
1457 	LE_WRITE_2(frm, status); frm += 2;
1458 	if (status == IEEE80211_STATUS_SUCCESS)
1459 		LE_WRITE_2(frm, ni->ni_associd);
1460 	else
1461 		LE_WRITE_2(frm, 0);
1462 	frm += 2;
1463 	frm = ieee80211_add_rates(frm, rs);
1464 	if (rs->rs_nrates > IEEE80211_RATE_SIZE)
1465 		frm = ieee80211_add_xrates(frm, rs);
1466 	if (ni->ni_flags & IEEE80211_NODE_QOS)
1467 		frm = ieee80211_add_edca_params(frm, ic);
1468 	if ((ni->ni_flags & IEEE80211_NODE_MFP) &&
1469 	    status == IEEE80211_STATUS_TRY_AGAIN_LATER) {
1470 		/* Association Comeback Time */
1471 		frm = ieee80211_add_tie(frm, 3, 1000 /* XXX */);
1472 	}
1473 	if (ic->ic_flags & IEEE80211_F_HTON) {
1474 		frm = ieee80211_add_htcaps(frm, ic);
1475 		frm = ieee80211_add_htop(frm, ic);
1476 		frm = ieee80211_add_wme_param(frm, ic);
1477 	}
1478 
1479 	m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1480 
1481 	return m;
1482 }
1483 #endif	/* IEEE80211_STA_ONLY */
1484 
1485 /*-
1486  * Disassociation frame format:
1487  * [2] Reason code
1488  */
1489 struct mbuf *
1490 ieee80211_get_disassoc(struct ieee80211com *ic, struct ieee80211_node *ni,
1491     u_int16_t reason)
1492 {
1493 	struct mbuf *m;
1494 
1495 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1496 	if (m == NULL)
1497 		return NULL;
1498 	m_align(m, 2);
1499 	m->m_pkthdr.len = m->m_len = 2;
1500 
1501 	*mtod(m, u_int16_t *) = htole16(reason);
1502 
1503 	return m;
1504 }
1505 
1506 /*-
1507  * ADDBA Request frame format:
1508  * [1] Category
1509  * [1] Action
1510  * [1] Dialog Token
1511  * [2] Block Ack Parameter Set
1512  * [2] Block Ack Timeout Value
1513  * [2] Block Ack Starting Sequence Control
1514  */
1515 struct mbuf *
1516 ieee80211_get_addba_req(struct ieee80211com *ic, struct ieee80211_node *ni,
1517     u_int8_t tid)
1518 {
1519 	struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid];
1520 	struct mbuf *m;
1521 	u_int8_t *frm;
1522 
1523 	m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA, 9);
1524 	if (m == NULL)
1525 		return m;
1526 
1527 	frm = mtod(m, u_int8_t *);
1528 	*frm++ = IEEE80211_CATEG_BA;
1529 	*frm++ = IEEE80211_ACTION_ADDBA_REQ;
1530 	*frm++ = ba->ba_token;
1531 	LE_WRITE_2(frm, ba->ba_params); frm += 2;
1532 	LE_WRITE_2(frm, ba->ba_timeout_val / IEEE80211_DUR_TU); frm += 2;
1533 	LE_WRITE_2(frm, ba->ba_winstart << IEEE80211_SEQ_SEQ_SHIFT); frm += 2;
1534 
1535 	m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1536 
1537 	return m;
1538 }
1539 
1540 /* Move Tx BA window forward to the specified SSN. */
1541 void
1542 ieee80211_output_ba_move_window(struct ieee80211com *ic,
1543     struct ieee80211_node *ni, uint8_t tid, uint16_t ssn)
1544 {
1545 	struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid];
1546 	uint16_t s = ba->ba_winstart;
1547 
1548 	while (SEQ_LT(s, ssn) && ba->ba_bitmap) {
1549 		s = (s + 1) % 0xfff;
1550 		ba->ba_bitmap >>= 1;
1551 	}
1552 
1553 	ba->ba_winstart = (ssn & 0xfff);
1554 	ba->ba_winend = (ba->ba_winstart + ba->ba_winsize - 1) & 0xfff;
1555 }
1556 
1557 /*
1558  * Move Tx BA window forward up to the first hole in the bitmap
1559  * or up to the specified SSN, whichever comes first.
1560  * After calling this function, frames before the start of the
1561  * potentially changed BA window should be discarded.
1562  */
1563 void
1564 ieee80211_output_ba_move_window_to_first_unacked(struct ieee80211com *ic,
1565     struct ieee80211_node *ni, uint8_t tid, uint16_t ssn)
1566 {
1567 	struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid];
1568 	uint16_t s = ba->ba_winstart;
1569 	uint64_t bitmap = ba->ba_bitmap;
1570 	int can_move_window = 0;
1571 
1572 	while (bitmap && SEQ_LT(s, ssn)) {
1573 		if ((bitmap & 1) == 0)
1574 			break;
1575 		s = (s + 1) % 0xfff;
1576 		bitmap >>= 1;
1577 		can_move_window = 1;
1578 	}
1579 
1580 	if (can_move_window)
1581 		ieee80211_output_ba_move_window(ic, ni, tid, s);
1582 }
1583 
1584 /* Record an ACK for a frame with a given SSN within the Tx BA window. */
1585 void
1586 ieee80211_output_ba_record_ack(struct ieee80211com *ic,
1587     struct ieee80211_node *ni, uint8_t tid, uint16_t ssn)
1588 {
1589 	struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid];
1590 	int i = 0;
1591 	uint16_t s = ba->ba_winstart;
1592 
1593 	KASSERT(!SEQ_LT(ssn, ba->ba_winstart));
1594 	KASSERT(!SEQ_LT(ba->ba_winend, ssn));
1595 
1596 	while (SEQ_LT(s, ssn)) {
1597 		s = (s + 1) % 0xfff;
1598 		i++;
1599 	}
1600 	if (i < ba->ba_winsize)
1601 		ba->ba_bitmap |= (1 << i);
1602 }
1603 
1604 /*-
1605  * ADDBA Response frame format:
1606  * [1] Category
1607  * [1] Action
1608  * [1] Dialog Token
1609  * [2] Status Code
1610  * [2] Block Ack Parameter Set
1611  * [2] Block Ack Timeout Value
1612  */
1613 struct mbuf *
1614 ieee80211_get_addba_resp(struct ieee80211com *ic, struct ieee80211_node *ni,
1615     u_int8_t tid, u_int8_t token, u_int16_t status)
1616 {
1617 	struct ieee80211_rx_ba *ba = &ni->ni_rx_ba[tid];
1618 	struct mbuf *m;
1619 	u_int8_t *frm;
1620 	u_int16_t params;
1621 
1622 	m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA, 9);
1623 	if (m == NULL)
1624 		return m;
1625 
1626 	frm = mtod(m, u_int8_t *);
1627 	*frm++ = IEEE80211_CATEG_BA;
1628 	*frm++ = IEEE80211_ACTION_ADDBA_RESP;
1629 	*frm++ = token;
1630 	LE_WRITE_2(frm, status); frm += 2;
1631 	if (status == 0)
1632 		params = ba->ba_params;
1633 	else
1634 		params = tid << IEEE80211_ADDBA_TID_SHIFT;
1635 	LE_WRITE_2(frm, params); frm += 2;
1636 	if (status == 0)
1637 		LE_WRITE_2(frm, ba->ba_timeout_val / IEEE80211_DUR_TU);
1638 	else
1639 		LE_WRITE_2(frm, 0);
1640 	frm += 2;
1641 
1642 	m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1643 
1644 	return m;
1645 }
1646 
1647 /*-
1648  * DELBA frame format:
1649  * [1] Category
1650  * [1] Action
1651  * [2] DELBA Parameter Set
1652  * [2] Reason Code
1653  */
1654 struct mbuf *
1655 ieee80211_get_delba(struct ieee80211com *ic, struct ieee80211_node *ni,
1656     u_int8_t tid, u_int8_t dir, u_int16_t reason)
1657 {
1658 	struct mbuf *m;
1659 	u_int8_t *frm;
1660 	u_int16_t params;
1661 
1662 	m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA, 6);
1663 	if (m == NULL)
1664 		return m;
1665 
1666 	frm = mtod(m, u_int8_t *);
1667 	*frm++ = IEEE80211_CATEG_BA;
1668 	*frm++ = IEEE80211_ACTION_DELBA;
1669 	params = tid << 12;
1670 	if (dir)
1671 		params |= IEEE80211_DELBA_INITIATOR;
1672 	LE_WRITE_2(frm, params); frm += 2;
1673 	LE_WRITE_2(frm, reason); frm += 2;
1674 
1675 	m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1676 
1677 	return m;
1678 }
1679 
1680 /*-
1681  * SA Query Request/Reponse frame format:
1682  * [1]  Category
1683  * [1]  Action
1684  * [16] Transaction Identifier
1685  */
1686 struct mbuf *
1687 ieee80211_get_sa_query(struct ieee80211com *ic, struct ieee80211_node *ni,
1688     u_int8_t action)
1689 {
1690 	struct mbuf *m;
1691 	u_int8_t *frm;
1692 
1693 	m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA, 4);
1694 	if (m == NULL)
1695 		return NULL;
1696 
1697 	frm = mtod(m, u_int8_t *);
1698 	*frm++ = IEEE80211_CATEG_SA_QUERY;
1699 	*frm++ = action;	/* ACTION_SA_QUERY_REQ/RESP */
1700 	LE_WRITE_2(frm, ni->ni_sa_query_trid); frm += 2;
1701 
1702 	m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1703 
1704 	return m;
1705 }
1706 
1707 struct mbuf *
1708 ieee80211_get_action(struct ieee80211com *ic, struct ieee80211_node *ni,
1709     u_int8_t categ, u_int8_t action, int arg)
1710 {
1711 	struct mbuf *m = NULL;
1712 
1713 	switch (categ) {
1714 	case IEEE80211_CATEG_BA:
1715 		switch (action) {
1716 		case IEEE80211_ACTION_ADDBA_REQ:
1717 			m = ieee80211_get_addba_req(ic, ni, arg & 0xffff);
1718 			break;
1719 		case IEEE80211_ACTION_ADDBA_RESP:
1720 			m = ieee80211_get_addba_resp(ic, ni, arg & 0xff,
1721 			    arg >> 8, arg >> 16);
1722 			break;
1723 		case IEEE80211_ACTION_DELBA:
1724 			m = ieee80211_get_delba(ic, ni, arg & 0xff, arg >> 8,
1725 			    arg >> 16);
1726 			break;
1727 		}
1728 		break;
1729 	case IEEE80211_CATEG_SA_QUERY:
1730 		switch (action) {
1731 #ifndef IEEE80211_STA_ONLY
1732 		case IEEE80211_ACTION_SA_QUERY_REQ:
1733 #endif
1734 		case IEEE80211_ACTION_SA_QUERY_RESP:
1735 			m = ieee80211_get_sa_query(ic, ni, action);
1736 			break;
1737 		}
1738 		break;
1739 	}
1740 	return m;
1741 }
1742 
1743 /*
1744  * Send a management frame.  The node is for the destination (or ic_bss
1745  * when in station mode).  Nodes other than ic_bss have their reference
1746  * count bumped to reflect our use for an indeterminant time.
1747  */
1748 int
1749 ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
1750     int type, int arg1, int arg2)
1751 {
1752 #define	senderr(_x, _v)	do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
1753 	struct ifnet *ifp = &ic->ic_if;
1754 	struct mbuf *m;
1755 	int ret, timer;
1756 
1757 	if (ni == NULL)
1758 		panic("null node");
1759 
1760 	/*
1761 	 * Hold a reference on the node so it doesn't go away until after
1762 	 * the xmit is complete all the way in the driver.  On error we
1763 	 * will remove our reference.
1764 	 */
1765 	ieee80211_ref_node(ni);
1766 	timer = 0;
1767 	switch (type) {
1768 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1769 		if ((m = ieee80211_get_probe_req(ic, ni)) == NULL)
1770 			senderr(ENOMEM, is_tx_nombuf);
1771 
1772 		timer = IEEE80211_TRANS_WAIT;
1773 		break;
1774 #ifndef IEEE80211_STA_ONLY
1775 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1776 		if ((m = ieee80211_get_probe_resp(ic)) == NULL)
1777 			senderr(ENOMEM, is_tx_nombuf);
1778 		break;
1779 #endif
1780 	case IEEE80211_FC0_SUBTYPE_AUTH:
1781 		m = ieee80211_get_auth(ic, ni, arg1 >> 16, arg1 & 0xffff);
1782 		if (m == NULL)
1783 			senderr(ENOMEM, is_tx_nombuf);
1784 
1785 		if (ic->ic_opmode == IEEE80211_M_STA)
1786 			timer = IEEE80211_TRANS_WAIT;
1787 		break;
1788 
1789 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
1790 		if ((m = ieee80211_get_deauth(ic, ni, arg1)) == NULL)
1791 			senderr(ENOMEM, is_tx_nombuf);
1792 #ifndef IEEE80211_STA_ONLY
1793 		if ((ifp->if_flags & IFF_DEBUG) &&
1794 		    (ic->ic_opmode == IEEE80211_M_HOSTAP ||
1795 		    ic->ic_opmode == IEEE80211_M_IBSS))
1796 			printf("%s: station %s deauthenticate (reason %d)\n",
1797 			    ifp->if_xname, ether_sprintf(ni->ni_macaddr),
1798 			    arg1);
1799 #endif
1800 		break;
1801 
1802 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1803 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1804 		if ((m = ieee80211_get_assoc_req(ic, ni, type)) == NULL)
1805 			senderr(ENOMEM, is_tx_nombuf);
1806 
1807 		timer = IEEE80211_TRANS_WAIT;
1808 		break;
1809 #ifndef IEEE80211_STA_ONLY
1810 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1811 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
1812 		if ((m = ieee80211_get_assoc_resp(ic, ni, arg1)) == NULL)
1813 			senderr(ENOMEM, is_tx_nombuf);
1814 		break;
1815 #endif
1816 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
1817 		if ((m = ieee80211_get_disassoc(ic, ni, arg1)) == NULL)
1818 			senderr(ENOMEM, is_tx_nombuf);
1819 #ifndef IEEE80211_STA_ONLY
1820 		if ((ifp->if_flags & IFF_DEBUG) &&
1821 		    (ic->ic_opmode == IEEE80211_M_HOSTAP ||
1822 		    ic->ic_opmode == IEEE80211_M_IBSS))
1823 			printf("%s: station %s disassociate (reason %d)\n",
1824 			    ifp->if_xname, ether_sprintf(ni->ni_macaddr),
1825 			    arg1);
1826 #endif
1827 		break;
1828 
1829 	case IEEE80211_FC0_SUBTYPE_ACTION:
1830 		m = ieee80211_get_action(ic, ni, arg1 >> 16, arg1 & 0xffff,
1831 		    arg2);
1832 		if (m == NULL)
1833 			senderr(ENOMEM, is_tx_nombuf);
1834 		break;
1835 
1836 	default:
1837 		DPRINTF(("invalid mgmt frame type %u\n", type));
1838 		senderr(EINVAL, is_tx_unknownmgt);
1839 		/* NOTREACHED */
1840 	}
1841 
1842 	ret = ieee80211_mgmt_output(ifp, ni, m, type);
1843 	if (ret == 0) {
1844 		if (timer)
1845 			ic->ic_mgt_timer = timer;
1846 	} else {
1847 bad:
1848 		ieee80211_release_node(ic, ni);
1849 	}
1850 	return ret;
1851 #undef senderr
1852 }
1853 
1854 /*
1855  * Build a RTS (Request To Send) control frame (see 7.2.1.1).
1856  */
1857 struct mbuf *
1858 ieee80211_get_rts(struct ieee80211com *ic, const struct ieee80211_frame *wh,
1859     u_int16_t dur)
1860 {
1861 	struct ieee80211_frame_rts *rts;
1862 	struct mbuf *m;
1863 
1864 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1865 	if (m == NULL)
1866 		return NULL;
1867 
1868 	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
1869 
1870 	rts = mtod(m, struct ieee80211_frame_rts *);
1871 	rts->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_CTL |
1872 	    IEEE80211_FC0_SUBTYPE_RTS;
1873 	rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1874 	*(u_int16_t *)rts->i_dur = htole16(dur);
1875 	IEEE80211_ADDR_COPY(rts->i_ra, wh->i_addr1);
1876 	IEEE80211_ADDR_COPY(rts->i_ta, wh->i_addr2);
1877 
1878 	return m;
1879 }
1880 
1881 /*
1882  * Build a CTS-to-self (Clear To Send) control frame (see 7.2.1.2).
1883  */
1884 struct mbuf *
1885 ieee80211_get_cts_to_self(struct ieee80211com *ic, u_int16_t dur)
1886 {
1887 	struct ieee80211_frame_cts *cts;
1888 	struct mbuf *m;
1889 
1890 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1891 	if (m == NULL)
1892 		return NULL;
1893 
1894 	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
1895 
1896 	cts = mtod(m, struct ieee80211_frame_cts *);
1897 	cts->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_CTL |
1898 	    IEEE80211_FC0_SUBTYPE_CTS;
1899 	cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1900 	*(u_int16_t *)cts->i_dur = htole16(dur);
1901 	IEEE80211_ADDR_COPY(cts->i_ra, ic->ic_myaddr);
1902 
1903 	return m;
1904 }
1905 
1906 /*
1907  * Build a compressed Block Ack Request control frame.
1908  */
1909 struct mbuf *
1910 ieee80211_get_compressed_bar(struct ieee80211com *ic,
1911     struct ieee80211_node *ni, int tid, uint16_t ssn)
1912 {
1913 	struct ieee80211_frame_min *wh;
1914 	uint8_t *frm;
1915 	uint16_t ctl;
1916 	struct mbuf *m;
1917 
1918 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1919 	if (m == NULL)
1920 		return NULL;
1921 
1922 	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_min) +
1923 	    sizeof(ctl) + sizeof(ssn);
1924 
1925 	wh = mtod(m, struct ieee80211_frame_min *);
1926 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_CTL |
1927 	    IEEE80211_FC0_SUBTYPE_BAR;
1928 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1929 	*(u_int16_t *)wh->i_dur = 0;
1930 	IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1931 	IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
1932 	frm = (uint8_t *)&wh[1];
1933 
1934 	ctl = IEEE80211_BA_COMPRESSED | (tid << IEEE80211_BA_TID_INFO_SHIFT);
1935 	LE_WRITE_2(frm, ctl);
1936 	frm += 2;
1937 
1938 	LE_WRITE_2(frm, ssn << IEEE80211_SEQ_SEQ_SHIFT);
1939 	frm += 2;
1940 
1941 	m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1942 	m->m_pkthdr.ph_cookie = ni;
1943 
1944 	return m;
1945 }
1946 
1947 #ifndef IEEE80211_STA_ONLY
1948 /*-
1949  * Beacon frame format:
1950  * [8]   Timestamp
1951  * [2]   Beacon interval
1952  * [2]   Capability
1953  * [tlv] Service Set Identifier (SSID)
1954  * [tlv] Supported rates
1955  * [tlv] DS Parameter Set (802.11g)
1956  * [tlv] IBSS Parameter Set
1957  * [tlv] Traffic Indication Map (TIM)
1958  * [tlv] ERP Information (802.11g)
1959  * [tlv] Extended Supported Rates (802.11g)
1960  * [tlv] RSN (802.11i)
1961  * [tlv] EDCA Parameter Set (802.11e)
1962  * [tlv] HT Capabilities (802.11n)
1963  * [tlv] HT Operation (802.11n)
1964  */
1965 struct mbuf *
1966 ieee80211_beacon_alloc(struct ieee80211com *ic, struct ieee80211_node *ni)
1967 {
1968 	const struct ieee80211_rateset *rs = &ni->ni_rates;
1969 	struct ieee80211_frame *wh;
1970 	struct mbuf *m;
1971 	u_int8_t *frm;
1972 
1973 	m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA,
1974 	    8 + 2 + 2 +
1975 	    2 + ((ic->ic_userflags & IEEE80211_F_HIDENWID) ?
1976 	    0 : ni->ni_esslen) +
1977 	    2 + min(rs->rs_nrates, IEEE80211_RATE_SIZE) +
1978 	    2 + 1 +
1979 	    2 + ((ic->ic_opmode == IEEE80211_M_IBSS) ? 2 : 254) +
1980 	    ((ic->ic_curmode == IEEE80211_MODE_11G) ? 2 + 1 : 0) +
1981 	    ((rs->rs_nrates > IEEE80211_RATE_SIZE) ?
1982 		2 + rs->rs_nrates - IEEE80211_RATE_SIZE : 0) +
1983 	    (((ic->ic_flags & IEEE80211_F_RSNON) &&
1984 	      (ni->ni_rsnprotos & IEEE80211_PROTO_RSN)) ?
1985 		2 + IEEE80211_RSNIE_MAXLEN : 0) +
1986 	    ((ic->ic_flags & IEEE80211_F_QOS) ? 2 + 18 : 0) +
1987 	    (((ic->ic_flags & IEEE80211_F_RSNON) &&
1988 	      (ni->ni_rsnprotos & IEEE80211_PROTO_WPA)) ?
1989 		2 + IEEE80211_WPAIE_MAXLEN : 0) +
1990 	    ((ic->ic_flags & IEEE80211_F_HTON) ? 28 + 24 + 26 : 0));
1991 	if (m == NULL)
1992 		return NULL;
1993 
1994 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
1995 	if (m == NULL)
1996 		return NULL;
1997 	wh = mtod(m, struct ieee80211_frame *);
1998 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
1999 	    IEEE80211_FC0_SUBTYPE_BEACON;
2000 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2001 	*(u_int16_t *)wh->i_dur = 0;
2002 	IEEE80211_ADDR_COPY(wh->i_addr1, etherbroadcastaddr);
2003 	IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
2004 	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
2005 	*(u_int16_t *)wh->i_seq = 0;
2006 
2007 	frm = (u_int8_t *)&wh[1];
2008 	memset(frm, 0, 8); frm += 8;	/* timestamp is set by hardware */
2009 	LE_WRITE_2(frm, ni->ni_intval); frm += 2;
2010 	frm = ieee80211_add_capinfo(frm, ic, ni);
2011 	if (ic->ic_userflags & IEEE80211_F_HIDENWID)
2012 		frm = ieee80211_add_ssid(frm, NULL, 0);
2013 	else
2014 		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
2015 	frm = ieee80211_add_rates(frm, rs);
2016 	frm = ieee80211_add_ds_params(frm, ic, ni);
2017 	if (ic->ic_opmode == IEEE80211_M_IBSS)
2018 		frm = ieee80211_add_ibss_params(frm, ni);
2019 	else
2020 		frm = ieee80211_add_tim(frm, ic);
2021 	if (ic->ic_curmode == IEEE80211_MODE_11G)
2022 		frm = ieee80211_add_erp(frm, ic);
2023 	if (rs->rs_nrates > IEEE80211_RATE_SIZE)
2024 		frm = ieee80211_add_xrates(frm, rs);
2025 	if ((ic->ic_flags & IEEE80211_F_RSNON) &&
2026 	    (ni->ni_rsnprotos & IEEE80211_PROTO_RSN))
2027 		frm = ieee80211_add_rsn(frm, ic, ni);
2028 	if (ic->ic_flags & IEEE80211_F_QOS)
2029 		frm = ieee80211_add_edca_params(frm, ic);
2030 	if ((ic->ic_flags & IEEE80211_F_RSNON) &&
2031 	    (ni->ni_rsnprotos & IEEE80211_PROTO_WPA))
2032 		frm = ieee80211_add_wpa(frm, ic, ni);
2033 	if (ic->ic_flags & IEEE80211_F_HTON) {
2034 		frm = ieee80211_add_htcaps(frm, ic);
2035 		frm = ieee80211_add_htop(frm, ic);
2036 		frm = ieee80211_add_wme_param(frm, ic);
2037 	}
2038 
2039 	m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
2040 	m->m_pkthdr.ph_cookie = ni;
2041 
2042 	return m;
2043 }
2044 
2045 /*
2046  * Check if an outgoing MSDU or management frame should be buffered into
2047  * the AP for power management.  Return 1 if the frame was buffered into
2048  * the AP, or 0 if the frame shall be transmitted immediately.
2049  */
2050 int
2051 ieee80211_pwrsave(struct ieee80211com *ic, struct mbuf *m,
2052     struct ieee80211_node *ni)
2053 {
2054 	const struct ieee80211_frame *wh;
2055 	int pssta = 0;
2056 
2057 	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP);
2058 	if (!(ic->ic_caps & IEEE80211_C_APPMGT))
2059 		return 0;
2060 
2061 	wh = mtod(m, struct ieee80211_frame *);
2062 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2063 		/*
2064 		 * Buffer group addressed MSDUs with the Order bit clear
2065 		 * if any associated STAs are in PS mode.
2066 		 */
2067 		ieee80211_iterate_nodes(ic, ieee80211_count_pssta, &pssta);
2068 		if ((wh->i_fc[1] & IEEE80211_FC1_ORDER) || pssta == 0)
2069 			return 0;
2070 		ic->ic_tim_mcast_pending = 1;
2071 	} else {
2072 		/*
2073 		 * Buffer MSDUs, A-MSDUs or management frames destined for
2074 		 * PS STAs.
2075 		 */
2076 		if (ni->ni_pwrsave == IEEE80211_PS_AWAKE ||
2077 		    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
2078 		    IEEE80211_FC0_TYPE_CTL)
2079 			return 0;
2080 		if (mq_empty(&ni->ni_savedq))
2081 			(*ic->ic_set_tim)(ic, ni->ni_associd, 1);
2082 	}
2083 	/* NB: ni == ic->ic_bss for broadcast/multicast */
2084 	/*
2085 	 * Similar to ieee80211_mgmt_output, store the node in a
2086 	 * special pkthdr field.
2087 	 */
2088 	m->m_pkthdr.ph_cookie = ni;
2089 	mq_enqueue(&ni->ni_savedq, m);
2090 	return 1;
2091 }
2092 #endif	/* IEEE80211_STA_ONLY */
2093