ieee80211_ht.c revision 173462
1/*-
2 * Copyright (c) 2007 Sam Leffler, Errno Consulting
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include <sys/cdefs.h>
27#ifdef __FreeBSD__
28__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_ht.c 173462 2007-11-08 17:11:40Z sam $");
29#endif
30
31/*
32 * IEEE 802.11n protocol support.
33 */
34
35#include "opt_inet.h"
36
37#include <sys/param.h>
38#include <sys/kernel.h>
39#include <sys/systm.h>
40#include <sys/endian.h>
41
42#include <sys/socket.h>
43
44#include <net/if.h>
45#include <net/if_media.h>
46#include <net/ethernet.h>
47
48#include <net80211/ieee80211_var.h>
49
50/* define here, used throughout file */
51#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
52#define	SM(_v, _f)	(((_v) << _f##_S) & _f)
53
54/* XXX need max array size */
55const int ieee80211_htrates[16] = {
56	13,		/* IFM_IEEE80211_MCS0 */
57	26,		/* IFM_IEEE80211_MCS1 */
58	39,		/* IFM_IEEE80211_MCS2 */
59	52,		/* IFM_IEEE80211_MCS3 */
60	78,		/* IFM_IEEE80211_MCS4 */
61	104,		/* IFM_IEEE80211_MCS5 */
62	117,		/* IFM_IEEE80211_MCS6 */
63	130,		/* IFM_IEEE80211_MCS7 */
64	26,		/* IFM_IEEE80211_MCS8 */
65	52,		/* IFM_IEEE80211_MCS9 */
66	78,		/* IFM_IEEE80211_MCS10 */
67	104,		/* IFM_IEEE80211_MCS11 */
68	156,		/* IFM_IEEE80211_MCS12 */
69	208,		/* IFM_IEEE80211_MCS13 */
70	234,		/* IFM_IEEE80211_MCS14 */
71	260,		/* IFM_IEEE80211_MCS15 */
72};
73
74static const struct ieee80211_htrateset ieee80211_rateset_11n =
75	{ 16, {
76	/* MCS: 6.5   13 19.5   26   39  52 58.5  65  13  26 */
77	          0,   1,   2,   3,   4,  5,   6,  7,  8,  9,
78	/*       39   52   78  104  117, 130 */
79		 10,  11,  12,  13,  14,  15 }
80	};
81
82#ifdef IEEE80211_AMPDU_AGE
83/* XXX public for sysctl hookup */
84int	ieee80211_ampdu_age = -1;	/* threshold for ampdu reorder q (ms) */
85#endif
86int	ieee80211_recv_bar_ena = 1;
87
88#define	IEEE80211_AGGR_TIMEOUT	msecs_to_ticks(250)
89#define	IEEE80211_AGGR_MINRETRY	msecs_to_ticks(10*1000)
90#define	IEEE80211_AGGR_MAXTRIES	3
91
92static int ieee80211_addba_request(struct ieee80211_node *ni,
93	struct ieee80211_tx_ampdu *tap,
94	int dialogtoken, int baparamset, int batimeout);
95static int ieee80211_addba_response(struct ieee80211_node *ni,
96	struct ieee80211_tx_ampdu *tap,
97	int code, int baparamset, int batimeout);
98static void ieee80211_addba_stop(struct ieee80211_node *ni,
99	struct ieee80211_tx_ampdu *tap);
100static void ieee80211_aggr_recv_action(struct ieee80211_node *ni,
101	const uint8_t *frm, const uint8_t *efrm);
102
103void
104ieee80211_ht_attach(struct ieee80211com *ic)
105{
106#ifdef IEEE80211_AMPDU_AGE
107	if (ieee80211_ampdu_age == -1)
108		ieee80211_ampdu_age = msecs_to_ticks(500);
109#endif
110
111	/* setup default aggregation policy */
112	ic->ic_recv_action = ieee80211_aggr_recv_action;
113	ic->ic_send_action = ieee80211_send_action;
114	ic->ic_addba_request = ieee80211_addba_request;
115	ic->ic_addba_response = ieee80211_addba_response;
116	ic->ic_addba_stop = ieee80211_addba_stop;
117
118	ic->ic_htprotmode = IEEE80211_PROT_RTSCTS;
119	ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
120
121	/* XXX get from driver */
122	ic->ic_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_8K;
123	ic->ic_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
124	ic->ic_ampdu_limit = ic->ic_ampdu_rxmax;
125	ic->ic_amsdu_limit = IEEE80211_HTCAP_MAXAMSDU_3839;
126
127	if (ic->ic_htcaps & IEEE80211_HTC_HT) {
128		/*
129		 * Device is HT capable; enable all HT-related
130		 * facilities by default.
131		 * XXX these choices may be too aggressive.
132		 */
133		ic->ic_flags_ext |= IEEE80211_FEXT_HT
134				 |  IEEE80211_FEXT_HTCOMPAT
135				 ;
136		if (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20)
137			ic->ic_flags_ext |= IEEE80211_FEXT_SHORTGI20;
138		/* XXX infer from channel list? */
139		if (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
140			ic->ic_flags_ext |= IEEE80211_FEXT_USEHT40;
141			if (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)
142				ic->ic_flags_ext |= IEEE80211_FEXT_SHORTGI40;
143		}
144		/* NB: A-MPDU and A-MSDU rx are mandated, these are tx only */
145		ic->ic_flags_ext |= IEEE80211_FEXT_AMPDU_RX;
146		if (ic->ic_htcaps & IEEE80211_HTC_AMPDU)
147			ic->ic_flags_ext |= IEEE80211_FEXT_AMPDU_TX;
148		ic->ic_flags_ext |= IEEE80211_FEXT_AMSDU_RX;
149		if (ic->ic_htcaps & IEEE80211_HTC_AMSDU)
150			ic->ic_flags_ext |= IEEE80211_FEXT_AMSDU_TX;
151	}
152}
153
154void
155ieee80211_ht_detach(struct ieee80211com *ic)
156{
157}
158
159static void
160ht_announce(struct ieee80211com *ic, int mode,
161	const struct ieee80211_htrateset *rs)
162{
163	struct ifnet *ifp = ic->ic_ifp;
164	int i, rate, mword;
165
166	if_printf(ifp, "%s MCS: ", ieee80211_phymode_name[mode]);
167	for (i = 0; i < rs->rs_nrates; i++) {
168		mword = ieee80211_rate2media(ic,
169		    rs->rs_rates[i] | IEEE80211_RATE_MCS, mode);
170		if (IFM_SUBTYPE(mword) != IFM_IEEE80211_MCS)
171			continue;
172		rate = ieee80211_htrates[rs->rs_rates[i]];
173		printf("%s%d%sMbps", (i != 0 ? " " : ""),
174		    rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
175	}
176	printf("\n");
177}
178
179void
180ieee80211_ht_announce(struct ieee80211com *ic)
181{
182	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA))
183		ht_announce(ic, IEEE80211_MODE_11NA, &ieee80211_rateset_11n);
184	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
185		ht_announce(ic, IEEE80211_MODE_11NG, &ieee80211_rateset_11n);
186}
187
188const struct ieee80211_htrateset *
189ieee80211_get_suphtrates(struct ieee80211com *ic,
190	const struct ieee80211_channel *c)
191{
192	return &ieee80211_rateset_11n;
193}
194
195/*
196 * Receive processing.
197 */
198
199/*
200 * Decap the encapsulated A-MSDU frames and dispatch all but
201 * the last for delivery.  The last frame is returned for
202 * delivery via the normal path.
203 */
204struct mbuf *
205ieee80211_decap_amsdu(struct ieee80211_node *ni, struct mbuf *m)
206{
207	struct ieee80211com *ic = ni->ni_ic;
208	int framelen;
209	struct mbuf *n;
210
211	/* discard 802.3 header inserted by ieee80211_decap */
212	m_adj(m, sizeof(struct ether_header));
213
214	ic->ic_stats.is_amsdu_decap++;
215
216	for (;;) {
217		/*
218		 * Decap the first frame, bust it apart from the
219		 * remainder and deliver.  We leave the last frame
220		 * delivery to the caller (for consistency with other
221		 * code paths, could also do it here).
222		 */
223		m = ieee80211_decap1(m, &framelen);
224		if (m == NULL) {
225			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
226			    ni->ni_macaddr, "a-msdu", "%s", "decap failed");
227			ic->ic_stats.is_amsdu_tooshort++;
228			return NULL;
229		}
230		if (m->m_pkthdr.len == framelen)
231			break;
232		n = m_split(m, framelen, M_NOWAIT);
233		if (n == NULL) {
234			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
235			    ni->ni_macaddr, "a-msdu",
236			    "%s", "unable to split encapsulated frames");
237			ic->ic_stats.is_amsdu_split++;
238			m_freem(m);			/* NB: must reclaim */
239			return NULL;
240		}
241		ieee80211_deliver_data(ic, ni, m);
242
243		/*
244		 * Remove frame contents; each intermediate frame
245		 * is required to be aligned to a 4-byte boundary.
246		 */
247		m = n;
248		m_adj(m, roundup2(framelen, 4) - framelen);	/* padding */
249	}
250	return m;				/* last delivered by caller */
251}
252
253/*
254 * Start A-MPDU rx/re-order processing for the specified TID.
255 */
256static void
257ampdu_rx_start(struct ieee80211_rx_ampdu *rap, int bufsiz, int start)
258{
259	memset(rap, 0, sizeof(*rap));
260	rap->rxa_wnd = (bufsiz == 0) ?
261	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
262	rap->rxa_start = start;
263	rap->rxa_flags |= IEEE80211_AGGR_XCHGPEND;
264}
265
266/*
267 * Purge all frames in the A-MPDU re-order queue.
268 */
269static void
270ampdu_rx_purge(struct ieee80211_rx_ampdu *rap)
271{
272	struct mbuf *m;
273	int i;
274
275	for (i = 0; i < rap->rxa_wnd; i++) {
276		m = rap->rxa_m[i];
277		if (m != NULL) {
278			rap->rxa_m[i] = NULL;
279			rap->rxa_qbytes -= m->m_pkthdr.len;
280			m_freem(m);
281			if (--rap->rxa_qframes == 0)
282				break;
283		}
284	}
285	KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0,
286	    ("lost %u data, %u frames on ampdu rx q",
287	    rap->rxa_qbytes, rap->rxa_qframes));
288}
289
290/*
291 * Stop A-MPDU rx processing for the specified TID.
292 */
293static void
294ampdu_rx_stop(struct ieee80211_rx_ampdu *rap)
295{
296	rap->rxa_flags &= ~IEEE80211_AGGR_XCHGPEND;
297	ampdu_rx_purge(rap);
298}
299
300/*
301 * Dispatch a frame from the A-MPDU reorder queue.  The
302 * frame is fed back into ieee80211_input marked with an
303 * M_AMPDU flag so it doesn't come back to us (it also
304 * permits ieee80211_input to optimize re-processing).
305 */
306static __inline void
307ampdu_dispatch(struct ieee80211_node *ni, struct mbuf *m)
308{
309	m->m_flags |= M_AMPDU;	/* bypass normal processing */
310	/* NB: rssi, noise, and rstamp are ignored w/ M_AMPDU set */
311	(void) ieee80211_input(ni->ni_ic, m, ni, 0, 0, 0);
312}
313
314/*
315 * Dispatch as many frames as possible from the re-order queue.
316 * Frames will always be "at the front"; we process all frames
317 * up to the first empty slot in the window.  On completion we
318 * cleanup state if there are still pending frames in the current
319 * BA window.  We assume the frame at slot 0 is already handled
320 * by the caller; we always start at slot 1.
321 */
322static void
323ampdu_rx_dispatch(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni)
324{
325	struct ieee80211com *ic = ni->ni_ic;
326	struct mbuf *m;
327	int i;
328
329	/* flush run of frames */
330	for (i = 1; i < rap->rxa_wnd; i++) {
331		m = rap->rxa_m[i];
332		if (m == NULL)
333			break;
334		rap->rxa_m[i] = NULL;
335		rap->rxa_qbytes -= m->m_pkthdr.len;
336		rap->rxa_qframes--;
337
338		ampdu_dispatch(ni, m);
339	}
340	/*
341	 * If frames remain, copy the mbuf pointers down so
342	 * they correspond to the offsets in the new window.
343	 */
344	if (rap->rxa_qframes != 0) {
345		int n = rap->rxa_qframes, j;
346		for (j = i+1; j < rap->rxa_wnd; j++) {
347			if (rap->rxa_m[j] != NULL) {
348				rap->rxa_m[j-i] = rap->rxa_m[j];
349				rap->rxa_m[j] = NULL;
350				if (--n == 0)
351					break;
352			}
353		}
354		KASSERT(n == 0, ("lost %d frames", n));
355		ic->ic_stats.is_ampdu_rx_copy += rap->rxa_qframes;
356	}
357	/*
358	 * Adjust the start of the BA window to
359	 * reflect the frames just dispatched.
360	 */
361	rap->rxa_start = IEEE80211_SEQ_ADD(rap->rxa_start, i);
362	ic->ic_stats.is_ampdu_rx_oor += i;
363}
364
365#ifdef IEEE80211_AMPDU_AGE
366/*
367 * Dispatch all frames in the A-MPDU re-order queue.
368 */
369static void
370ampdu_rx_flush(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
371{
372	struct ieee80211com *ic = ni->ni_ic;
373	struct mbuf *m;
374	int i;
375
376	for (i = 0; i < rap->rxa_wnd; i++) {
377		m = rap->rxa_m[i];
378		if (m == NULL)
379			continue;
380		rap->rxa_m[i] = NULL;
381		rap->rxa_qbytes -= m->m_pkthdr.len;
382		rap->rxa_qframes--;
383		ic->ic_stats.is_ampdu_rx_oor++;
384
385		ampdu_dispatch(ni, m);
386		if (rap->rxa_qframes == 0)
387			break;
388	}
389}
390#endif /* IEEE80211_AMPDU_AGE */
391
392/*
393 * Dispatch all frames in the A-MPDU re-order queue
394 * preceding the specified sequence number.  This logic
395 * handles window moves due to a received MSDU or BAR.
396 */
397static void
398ampdu_rx_flush_upto(struct ieee80211_node *ni,
399	struct ieee80211_rx_ampdu *rap, ieee80211_seq winstart)
400{
401	struct ieee80211com *ic = ni->ni_ic;
402	struct mbuf *m;
403	ieee80211_seq seqno;
404	int i;
405
406	/*
407	 * Flush any complete MSDU's with a sequence number lower
408	 * than winstart.  Gaps may exist.  Note that we may actually
409	 * dispatch frames past winstart if a run continues; this is
410	 * an optimization that avoids having to do a separate pass
411	 * to dispatch frames after moving the BA window start.
412	 */
413	seqno = rap->rxa_start;
414	for (i = 0; i < rap->rxa_wnd; i++) {
415		m = rap->rxa_m[i];
416		if (m != NULL) {
417			rap->rxa_m[i] = NULL;
418			rap->rxa_qbytes -= m->m_pkthdr.len;
419			rap->rxa_qframes--;
420			ic->ic_stats.is_ampdu_rx_oor++;
421
422			ampdu_dispatch(ni, m);
423		} else {
424			if (!IEEE80211_SEQ_BA_BEFORE(seqno, winstart))
425				break;
426		}
427		seqno = IEEE80211_SEQ_INC(seqno);
428	}
429	/*
430	 * If frames remain, copy the mbuf pointers down so
431	 * they correspond to the offsets in the new window.
432	 */
433	if (rap->rxa_qframes != 0) {
434		int n = rap->rxa_qframes, j;
435		for (j = i+1; j < rap->rxa_wnd; j++) {
436			if (rap->rxa_m[j] != NULL) {
437				rap->rxa_m[j-i] = rap->rxa_m[j];
438				rap->rxa_m[j] = NULL;
439				if (--n == 0)
440					break;
441			}
442		}
443		KASSERT(n == 0, ("%s: lost %d frames, qframes %d off %d "
444		    "BA win <%d:%d> winstart %d",
445		    __func__, n, rap->rxa_qframes, i, rap->rxa_start,
446		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
447		    winstart));
448		ic->ic_stats.is_ampdu_rx_copy += rap->rxa_qframes;
449	}
450	/*
451	 * Move the start of the BA window; we use the
452	 * sequence number of the last MSDU that was
453	 * passed up the stack+1 or winstart if stopped on
454	 * a gap in the reorder buffer.
455	 */
456	rap->rxa_start = seqno;
457}
458
459/*
460 * Process a received QoS data frame for an HT station.  Handle
461 * A-MPDU reordering: if this frame is received out of order
462 * and falls within the BA window hold onto it.  Otherwise if
463 * this frame completes a run, flush any pending frames.  We
464 * return 1 if the frame is consumed.  A 0 is returned if
465 * the frame should be processed normally by the caller.
466 */
467int
468ieee80211_ampdu_reorder(struct ieee80211_node *ni, struct mbuf *m)
469{
470#define	IEEE80211_FC0_QOSDATA \
471	(IEEE80211_FC0_TYPE_DATA|IEEE80211_FC0_SUBTYPE_QOS|IEEE80211_FC0_VERSION_0)
472#define	PROCESS		0	/* caller should process frame */
473#define	CONSUMED	1	/* frame consumed, caller does nothing */
474	struct ieee80211com *ic = ni->ni_ic;
475	struct ieee80211_qosframe *wh;
476	struct ieee80211_rx_ampdu *rap;
477	ieee80211_seq rxseq;
478	uint8_t tid;
479	int off;
480
481	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
482
483	/* NB: m_len known to be sufficient */
484	wh = mtod(m, struct ieee80211_qosframe *);
485	KASSERT(wh->i_fc[0] == IEEE80211_FC0_QOSDATA, ("not QoS data"));
486
487	if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
488		tid = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0];
489	else
490		tid = wh->i_qos[0];
491	tid &= IEEE80211_QOS_TID;
492	rap = &ni->ni_rx_ampdu[tid];
493	if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
494		/*
495		 * No ADDBA request yet, don't touch.
496		 */
497		return PROCESS;
498	}
499	rxseq = le16toh(*(uint16_t *)wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
500	rap->rxa_nframes++;
501again:
502	if (rxseq == rap->rxa_start) {
503		/*
504		 * First frame in window.
505		 */
506		if (rap->rxa_qframes != 0) {
507			/*
508			 * Dispatch as many packets as we can.
509			 */
510			KASSERT(rap->rxa_m[0] == NULL, ("unexpected dup"));
511			ampdu_dispatch(ni, m);
512			ampdu_rx_dispatch(rap, ni);
513			return CONSUMED;
514		} else {
515			/*
516			 * In order; advance window and notify
517			 * caller to dispatch directly.
518			 */
519			rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
520			return PROCESS;
521		}
522	}
523	/*
524	 * Frame is out of order; store if in the BA window.
525	 */
526	/* calculate offset in BA window */
527	off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
528	if (off < rap->rxa_wnd) {
529		/*
530		 * Common case (hopefully): in the BA window.
531		 * Sec 9.10.7.6 a) (D2.04 p.118 line 47)
532		 */
533#ifdef IEEE80211_AMPDU_AGE
534		/*
535		 * Check for frames sitting too long in the reorder queue.
536		 * This should only ever happen if frames are not delivered
537		 * without the sender otherwise notifying us (e.g. with a
538		 * BAR to move the window).  Typically this happens because
539		 * of vendor bugs that cause the sequence number to jump.
540		 * When this happens we get a gap in the reorder queue that
541		 * leaves frame sitting on the queue until they get pushed
542		 * out due to window moves.  When the vendor does not send
543		 * BAR this move only happens due to explicit packet sends
544		 *
545		 * NB: we only track the time of the oldest frame in the
546		 * reorder q; this means that if we flush we might push
547		 * frames that still "new"; if this happens then subsequent
548		 * frames will result in BA window moves which cost something
549		 * but is still better than a big throughput dip.
550		 */
551		if (rap->rxa_qframes != 0) {
552			/* XXX honor batimeout? */
553			if (ticks - rap->rxa_age > ieee80211_ampdu_age) {
554				/*
555				 * Too long since we received the first
556				 * frame; flush the reorder buffer.
557				 */
558				if (rap->rxa_qframes != 0) {
559					ic->ic_stats.is_ampdu_rx_age +=
560					    rap->rxa_qframes;
561					ampdu_rx_flush(ni, rap);
562				}
563				rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
564				return PROCESS;
565			}
566		} else {
567			/*
568			 * First frame, start aging timer.
569			 */
570			rap->rxa_age = ticks;
571		}
572#endif /* IEEE80211_AMPDU_AGE */
573		/* save packet */
574		if (rap->rxa_m[off] == NULL) {
575			rap->rxa_m[off] = m;
576			rap->rxa_qframes++;
577			rap->rxa_qbytes += m->m_pkthdr.len;
578			ic->ic_stats.is_ampdu_rx_reorder++;
579		} else {
580			IEEE80211_DISCARD_MAC(ic,
581			    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
582			    ni->ni_macaddr, "a-mpdu duplicate",
583			    "seqno %u tid %u BA win <%u:%u>",
584			    rxseq, tid, rap->rxa_start,
585			    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1));
586			ic->ic_stats.is_rx_dup++;
587			IEEE80211_NODE_STAT(ni, rx_dup);
588			m_freem(m);
589		}
590		return CONSUMED;
591	}
592	if (off < IEEE80211_SEQ_BA_RANGE) {
593		/*
594		 * Outside the BA window, but within range;
595		 * flush the reorder q and move the window.
596		 * Sec 9.10.7.6 b) (D2.04 p.118 line 60)
597		 */
598		IEEE80211_NOTE(ic, IEEE80211_MSG_11N, ni,
599		    "move BA win <%u:%u> (%u frames) rxseq %u tid %u",
600		    rap->rxa_start,
601		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
602		    rap->rxa_qframes, rxseq, tid);
603		ic->ic_stats.is_ampdu_rx_move++;
604
605		/*
606		 * The spec says to flush frames up to but not including:
607		 * 	WinStart_B = rxseq - rap->rxa_wnd + 1
608		 * Then insert the frame or notify the caller to process
609		 * it immediately.  We can safely do this by just starting
610		 * over again because we know the frame will now be within
611		 * the BA window.
612		 */
613		/* NB: rxa_wnd known to be >0 */
614		ampdu_rx_flush_upto(ni, rap,
615		    IEEE80211_SEQ_SUB(rxseq, rap->rxa_wnd-1));
616		goto again;
617	} else {
618		/*
619		 * Outside the BA window and out of range; toss.
620		 * Sec 9.10.7.6 c) (D2.04 p.119 line 16)
621		 */
622		IEEE80211_DISCARD_MAC(ic,
623		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
624		    "MSDU", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
625		    rap->rxa_start,
626		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
627		    rap->rxa_qframes, rxseq, tid,
628		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
629		ic->ic_stats.is_ampdu_rx_drop++;
630		IEEE80211_NODE_STAT(ni, rx_drop);
631		m_freem(m);
632		return CONSUMED;
633	}
634#undef CONSUMED
635#undef PROCESS
636#undef IEEE80211_FC0_QOSDATA
637}
638
639/*
640 * Process a BAR ctl frame.  Dispatch all frames up to
641 * the sequence number of the frame.  If this frame is
642 * out of range it's discarded.
643 */
644void
645ieee80211_recv_bar(struct ieee80211_node *ni, struct mbuf *m0)
646{
647	struct ieee80211com *ic = ni->ni_ic;
648	struct ieee80211_frame_bar *wh;
649	struct ieee80211_rx_ampdu *rap;
650	ieee80211_seq rxseq;
651	int tid, off;
652
653	if (!ieee80211_recv_bar_ena) {
654#if 0
655		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_11N,
656		    ni->ni_macaddr, "BAR", "%s", "processing disabled");
657#endif
658		ic->ic_stats.is_ampdu_bar_bad++;
659		return;
660	}
661	wh = mtod(m0, struct ieee80211_frame_bar *);
662	/* XXX check basic BAR */
663	tid = MS(le16toh(wh->i_ctl), IEEE80211_BAR_TID);
664	rap = &ni->ni_rx_ampdu[tid];
665	if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
666		/*
667		 * No ADDBA request yet, don't touch.
668		 */
669		IEEE80211_DISCARD_MAC(ic,
670		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
671		    ni->ni_macaddr, "BAR", "no BA stream, tid %u", tid);
672		ic->ic_stats.is_ampdu_bar_bad++;
673		return;
674	}
675	ic->ic_stats.is_ampdu_bar_rx++;
676	rxseq = le16toh(wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
677	if (rxseq == rap->rxa_start)
678		return;
679	/* calculate offset in BA window */
680	off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
681	if (off < IEEE80211_SEQ_BA_RANGE) {
682		/*
683		 * Flush the reorder q up to rxseq and move the window.
684		 * Sec 9.10.7.6 a) (D2.04 p.119 line 22)
685		 */
686		IEEE80211_NOTE(ic, IEEE80211_MSG_11N, ni,
687		    "BAR moves BA win <%u:%u> (%u frames) rxseq %u tid %u",
688		    rap->rxa_start,
689		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
690		    rap->rxa_qframes, rxseq, tid);
691		ic->ic_stats.is_ampdu_bar_move++;
692
693		ampdu_rx_flush_upto(ni, rap, rxseq);
694		if (off >= rap->rxa_wnd) {
695			/*
696			 * BAR specifies a window start to the right of BA
697			 * window; we must move it explicitly since
698			 * ampdu_rx_flush_upto will not.
699			 */
700			rap->rxa_start = rxseq;
701		}
702	} else {
703		/*
704		 * Out of range; toss.
705		 * Sec 9.10.7.6 b) (D2.04 p.119 line 41)
706		 */
707		IEEE80211_DISCARD_MAC(ic,
708		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr,
709		    "BAR", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s",
710		    rap->rxa_start,
711		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1),
712		    rap->rxa_qframes, rxseq, tid,
713		    wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : "");
714		ic->ic_stats.is_ampdu_bar_oow++;
715		IEEE80211_NODE_STAT(ni, rx_drop);
716	}
717}
718
719/*
720 * Setup HT-specific state in a node.  Called only
721 * when HT use is negotiated so we don't do extra
722 * work for temporary and/or legacy sta's.
723 */
724void
725ieee80211_ht_node_init(struct ieee80211_node *ni, const uint8_t *htcap)
726{
727	struct ieee80211_tx_ampdu *tap;
728	int ac;
729
730	if (ni->ni_flags & IEEE80211_NODE_HT) {
731		/*
732		 * Clean AMPDU state on re-associate.  This handles the case
733		 * where a station leaves w/o notifying us and then returns
734		 * before node is reaped for inactivity.
735		 */
736		ieee80211_ht_node_cleanup(ni);
737	}
738	ieee80211_parse_htcap(ni, htcap);
739	for (ac = 0; ac < WME_NUM_AC; ac++) {
740		tap = &ni->ni_tx_ampdu[ac];
741		tap->txa_ac = ac;
742		/* NB: further initialization deferred */
743	}
744	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
745}
746
747/*
748 * Cleanup HT-specific state in a node.  Called only
749 * when HT use has been marked.
750 */
751void
752ieee80211_ht_node_cleanup(struct ieee80211_node *ni)
753{
754	struct ieee80211com *ic = ni->ni_ic;
755	int i;
756
757	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
758
759	/* XXX optimize this */
760	for (i = 0; i < WME_NUM_AC; i++) {
761		struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
762		if (tap->txa_flags & IEEE80211_AGGR_SETUP) {
763			/*
764			 * Stop BA stream if setup so driver has a chance
765			 * to reclaim any resources it might have allocated.
766			 */
767			ic->ic_addba_stop(ni, &ni->ni_tx_ampdu[i]);
768			IEEE80211_TAPQ_DESTROY(tap);
769			/* NB: clearing NAK means we may re-send ADDBA */
770			tap->txa_flags &=
771			    ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK);
772		}
773	}
774	for (i = 0; i < WME_NUM_TID; i++)
775		ampdu_rx_stop(&ni->ni_rx_ampdu[i]);
776
777	ni->ni_htcap = 0;
778	ni->ni_flags &= ~(IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT |
779		IEEE80211_NODE_AMPDU);
780}
781
782static struct ieee80211_channel *
783findhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags)
784{
785	return ieee80211_find_channel(ic, c->ic_freq,
786	    (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags);
787}
788
789/*
790 * Adjust a channel to be HT/non-HT according to the vap's configuration.
791 */
792struct ieee80211_channel *
793ieee80211_ht_adjust_channel(struct ieee80211com *ic,
794	struct ieee80211_channel *chan, int flags)
795{
796	struct ieee80211_channel *c;
797
798	if (flags & IEEE80211_FEXT_HT) {
799		/* promote to HT if possible */
800		if (flags & IEEE80211_FEXT_USEHT40) {
801			if (!IEEE80211_IS_CHAN_HT40(chan)) {
802				/* NB: arbitrarily pick ht40+ over ht40- */
803				c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U);
804				if (c == NULL)
805					c = findhtchan(ic, chan,
806						IEEE80211_CHAN_HT40D);
807				if (c == NULL)
808					c = findhtchan(ic, chan,
809						IEEE80211_CHAN_HT20);
810				if (c != NULL)
811					chan = c;
812			}
813		} else if (!IEEE80211_IS_CHAN_HT20(chan)) {
814			c = findhtchan(ic, chan, IEEE80211_CHAN_HT20);
815			if (c != NULL)
816				chan = c;
817		}
818	} else if (IEEE80211_IS_CHAN_HT(chan)) {
819		/* demote to legacy, HT use is disabled */
820		c = ieee80211_find_channel(ic, chan->ic_freq,
821		    chan->ic_flags &~ IEEE80211_CHAN_HT);
822		if (c != NULL)
823			chan = c;
824	}
825	return chan;
826}
827
828/*
829 * Setup HT-specific state for a legacy WDS peer.
830 */
831void
832ieee80211_ht_wds_init(struct ieee80211_node *ni)
833{
834	struct ieee80211com *ic = ni->ni_ic;
835	struct ieee80211_tx_ampdu *tap;
836	int ac;
837
838	KASSERT(ic->ic_flags_ext & IEEE80211_FEXT_HT, ("no HT requested"));
839
840	/* XXX check scan cache in case peer has an ap and we have info */
841	/*
842	 * If setup with a legacy channel; locate an HT channel.
843	 * Otherwise if the inherited channel (from a companion
844	 * AP) is suitable use it so we use the same location
845	 * for the extension channel).
846	 */
847	ni->ni_chan = ieee80211_ht_adjust_channel(ic, ni->ni_chan,
848	    ic->ic_flags_ext);
849
850	ni->ni_htcap = 0;
851	if (ic->ic_flags_ext & IEEE80211_FEXT_SHORTGI20)
852		ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20;
853	if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
854		ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40;
855		ni->ni_chw = 40;
856		if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
857			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE;
858		else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
859			ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW;
860		if (ic->ic_flags_ext & IEEE80211_FEXT_SHORTGI40)
861			ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40;
862	} else {
863		ni->ni_chw = 20;
864		ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE;
865	}
866	ni->ni_htctlchan = ni->ni_chan->ic_ieee;
867
868	ni->ni_htopmode = 0;		/* XXX need protection state */
869	ni->ni_htstbc = 0;		/* XXX need info */
870
871	for (ac = 0; ac < WME_NUM_AC; ac++) {
872		tap = &ni->ni_tx_ampdu[ac];
873		tap->txa_ac = ac;
874	}
875	/* NB: AMPDU tx/rx governed by IEEE80211_FEXT_AMPDU_{TX,RX} */
876	ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU;
877}
878
879/*
880 * Notify hostap vaps of a change in the HTINFO ie.
881 */
882static void
883htinfo_notify(struct ieee80211com *ic)
884{
885	if (ic->ic_opmode != IEEE80211_M_HOSTAP)
886		return;
887	IEEE80211_NOTE(ic,
888	    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N,
889	    ic->ic_bss,
890	    "HT bss occupancy change: %d sta, %d ht, "
891	    "%d ht40%s, HT protmode now 0x%x"
892	    , ic->ic_sta_assoc
893	    , ic->ic_ht_sta_assoc
894	    , ic->ic_ht40_sta_assoc
895	    , (ic->ic_flags_ext & IEEE80211_FEXT_NONHT_PR) ?
896		 ", non-HT sta present" : ""
897	    , ic->ic_curhtprotmode);
898	ieee80211_beacon_notify(ic, IEEE80211_BEACON_HTINFO);
899}
900
901/*
902 * Calculate HT protection mode from current
903 * state and handle updates.
904 */
905static void
906htinfo_update(struct ieee80211com *ic)
907{
908	uint8_t protmode;
909
910	if (ic->ic_flags_ext & IEEE80211_FEXT_NONHT_PR) {
911		protmode = IEEE80211_HTINFO_OPMODE_PROTOPT
912		         | IEEE80211_HTINFO_NONHT_PRESENT;
913	} else if (ic->ic_sta_assoc != ic->ic_ht_sta_assoc) {
914		protmode = IEEE80211_HTINFO_OPMODE_MIXED
915		         | IEEE80211_HTINFO_NONHT_PRESENT;
916	} else if (IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) &&
917	    ic->ic_sta_assoc != ic->ic_ht40_sta_assoc) {
918		protmode = IEEE80211_HTINFO_OPMODE_HT20PR;
919	} else {
920		protmode = IEEE80211_HTINFO_OPMODE_PURE;
921	}
922	if (protmode != ic->ic_curhtprotmode) {
923		ic->ic_curhtprotmode = protmode;
924		htinfo_notify(ic);
925	}
926}
927
928/*
929 * Handle an HT station joining a BSS.
930 */
931void
932ieee80211_ht_node_join(struct ieee80211_node *ni)
933{
934	struct ieee80211com *ic = ni->ni_ic;
935
936	IEEE80211_LOCK_ASSERT(ic);
937
938	if (ni->ni_flags & IEEE80211_NODE_HT) {
939		ic->ic_ht_sta_assoc++;
940		if (ni->ni_chw == 40)
941			ic->ic_ht40_sta_assoc++;
942	}
943	htinfo_update(ic);
944}
945
946/*
947 * Handle an HT station leaving a BSS.
948 */
949void
950ieee80211_ht_node_leave(struct ieee80211_node *ni)
951{
952	struct ieee80211com *ic = ni->ni_ic;
953
954	IEEE80211_LOCK_ASSERT(ic);
955
956	if (ni->ni_flags & IEEE80211_NODE_HT) {
957		ic->ic_ht_sta_assoc--;
958		if (ni->ni_chw == 40)
959			ic->ic_ht40_sta_assoc--;
960	}
961	htinfo_update(ic);
962}
963
964/*
965 * Public version of htinfo_update; used for processing
966 * beacon frames from overlapping bss in hostap_recv_mgmt.
967 */
968void
969ieee80211_htinfo_update(struct ieee80211com *ic, int protmode)
970{
971	if (protmode != ic->ic_curhtprotmode) {
972		ic->ic_curhtprotmode = protmode;
973		htinfo_notify(ic);
974	}
975}
976
977/*
978 * Time out presence of an overlapping bss with non-HT
979 * stations.  When operating in hostap mode we listen for
980 * beacons from other stations and if we identify a non-HT
981 * station is present we update the opmode field of the
982 * HTINFO ie.  To identify when all non-HT stations are
983 * gone we time out this condition.
984 */
985void
986ieee80211_ht_timeout(struct ieee80211com *ic)
987{
988	IEEE80211_LOCK_ASSERT(ic);
989
990	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONHT_PR) &&
991	    time_after(ticks, ic->ic_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) {
992#if 0
993		IEEE80211_NOTE(ic, IEEE80211_MSG_11N, ni,
994		    "%s", "time out non-HT STA present on channel");
995#endif
996		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONHT_PR;
997		htinfo_update(ic);
998	}
999}
1000
1001/* unalligned little endian access */
1002#define LE_READ_2(p)					\
1003	((uint16_t)					\
1004	 ((((const uint8_t *)(p))[0]      ) |		\
1005	  (((const uint8_t *)(p))[1] <<  8)))
1006
1007/*
1008 * Process an 802.11n HT capabilities ie.
1009 */
1010void
1011ieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
1012{
1013	struct ieee80211com *ic = ni->ni_ic;
1014
1015	if (ie[0] == IEEE80211_ELEMID_VENDOR) {
1016		/*
1017		 * Station used Vendor OUI ie to associate;
1018		 * mark the node so when we respond we'll use
1019		 * the Vendor OUI's and not the standard ie's.
1020		 */
1021		ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
1022		ie += 4;
1023	} else
1024		ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
1025
1026	ni->ni_htcap = LE_READ_2(ie +
1027		__offsetof(struct ieee80211_ie_htcap, hc_cap));
1028	ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
1029	/* XXX needed or will ieee80211_parse_htinfo always be called? */
1030	ni->ni_chw = (ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) &&
1031		     (ic->ic_flags_ext & IEEE80211_FEXT_USEHT40) ? 40 : 20;
1032}
1033
1034/*
1035 * Process an 802.11n HT info ie and update the node state.
1036 * Note that we handle use this information to identify the
1037 * correct channel (HT20, HT40+, HT40-, legacy).  The caller
1038 * is responsible for insuring any required channel change is
1039 * done (e.g. in sta mode when parsing the contents of a
1040 * beacon frame).
1041 */
1042void
1043ieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
1044{
1045	struct ieee80211com *ic = ni->ni_ic;
1046 	const struct ieee80211_ie_htinfo *htinfo;
1047	struct ieee80211_channel *c;
1048	uint16_t w;
1049	int htflags, chanflags;
1050
1051	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1052		ie += 4;
1053 	htinfo = (const struct ieee80211_ie_htinfo *) ie;
1054	ni->ni_htctlchan = htinfo->hi_ctrlchannel;
1055	ni->ni_ht2ndchan = SM(htinfo->hi_byte1, IEEE80211_HTINFO_2NDCHAN);
1056	w = LE_READ_2(&htinfo->hi_byte2);
1057	ni->ni_htopmode = SM(w, IEEE80211_HTINFO_OPMODE);
1058	w = LE_READ_2(&htinfo->hi_byte45);
1059	ni->ni_htstbc = SM(w, IEEE80211_HTINFO_BASIC_STBCMCS);
1060	/*
1061	 * Handle 11n channel switch.  Use the received HT ie's to
1062	 * identify the right channel to use.  If we cannot locate it
1063	 * in the channel table then fallback to legacy operation.
1064	 */
1065	htflags = (ic->ic_flags_ext & IEEE80211_FEXT_HT) ?
1066	    IEEE80211_CHAN_HT20 : 0;
1067	/* NB: honor operating mode constraint */
1068	if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) &&
1069	    (ic->ic_flags_ext & IEEE80211_FEXT_USEHT40)) {
1070		if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE)
1071			htflags = IEEE80211_CHAN_HT40U;
1072		else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW)
1073			htflags = IEEE80211_CHAN_HT40D;
1074	}
1075	chanflags = (ni->ni_chan->ic_flags &~ IEEE80211_CHAN_HT) | htflags;
1076	if (chanflags != ni->ni_chan->ic_flags) {
1077		c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags);
1078		if (c == NULL && htflags != IEEE80211_CHAN_HT20) {
1079			/*
1080			 * No HT40 channel entry in our table; fall back
1081			 * to HT20 operation.  This should not happen.
1082			 */
1083			c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20);
1084			IEEE80211_NOTE(ni->ni_ic,
1085			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1086			    "no HT40 channel (freq %u), falling back to HT20",
1087			    ni->ni_chan->ic_freq);
1088			/* XXX stat */
1089		}
1090		if (c != NULL && c != ni->ni_chan) {
1091			IEEE80211_NOTE(ni->ni_ic,
1092			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
1093			    "switch station to HT%d channel %u/0x%x",
1094			    IEEE80211_IS_CHAN_HT40(c) ? 40 : 20,
1095			    c->ic_freq, c->ic_flags);
1096			ni->ni_chan = c;
1097		}
1098		/* NB: caller responsible for forcing any channel change */
1099	}
1100	/* update node's tx channel width */
1101	ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan)? 40 : 20;
1102}
1103
1104/*
1105 * Install received HT rate set by parsing the HT cap ie.
1106 */
1107int
1108ieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
1109{
1110	struct ieee80211com *ic = ni->ni_ic;
1111	const struct ieee80211_ie_htcap *htcap;
1112	struct ieee80211_htrateset *rs;
1113	int i;
1114
1115	rs = &ni->ni_htrates;
1116	memset(rs, 0, sizeof(*rs));
1117	if (ie != NULL) {
1118		if (ie[0] == IEEE80211_ELEMID_VENDOR)
1119			ie += 4;
1120		htcap = (const struct ieee80211_ie_htcap *) ie;
1121		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1122			if (isclr(htcap->hc_mcsset, i))
1123				continue;
1124			if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
1125				IEEE80211_NOTE(ic,
1126				    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1127				    "WARNING, HT rate set too large; only "
1128				    "using %u rates", IEEE80211_HTRATE_MAXSIZE);
1129				ic->ic_stats.is_rx_rstoobig++;
1130				break;
1131			}
1132			rs->rs_rates[rs->rs_nrates++] = i;
1133		}
1134	}
1135	return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
1136}
1137
1138/*
1139 * Mark rates in a node's HT rate set as basic according
1140 * to the information in the supplied HT info ie.
1141 */
1142void
1143ieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
1144{
1145	const struct ieee80211_ie_htinfo *htinfo;
1146	struct ieee80211_htrateset *rs;
1147	int i, j;
1148
1149	if (ie[0] == IEEE80211_ELEMID_VENDOR)
1150		ie += 4;
1151	htinfo = (const struct ieee80211_ie_htinfo *) ie;
1152	rs = &ni->ni_htrates;
1153	if (rs->rs_nrates == 0) {
1154		IEEE80211_NOTE(ni->ni_ic,
1155		    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
1156		    "%s", "WARNING, empty HT rate set");
1157		return;
1158	}
1159	for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
1160		if (isclr(htinfo->hi_basicmcsset, i))
1161			continue;
1162		for (j = 0; j < rs->rs_nrates; j++)
1163			if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
1164				rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
1165	}
1166}
1167
1168static void
1169addba_timeout(void *arg)
1170{
1171	struct ieee80211_tx_ampdu *tap = arg;
1172
1173	/* XXX ? */
1174	tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1175	tap->txa_attempts++;
1176}
1177
1178static void
1179addba_start_timeout(struct ieee80211_tx_ampdu *tap)
1180{
1181	/* XXX use CALLOUT_PENDING instead? */
1182	callout_reset(&tap->txa_timer, IEEE80211_AGGR_TIMEOUT,
1183	    addba_timeout, tap);
1184	tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
1185	tap->txa_lastrequest = ticks;
1186}
1187
1188static void
1189addba_stop_timeout(struct ieee80211_tx_ampdu *tap)
1190{
1191	/* XXX use CALLOUT_PENDING instead? */
1192	if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
1193		callout_stop(&tap->txa_timer);
1194		tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
1195	}
1196}
1197
1198/*
1199 * Default method for requesting A-MPDU tx aggregation.
1200 * We setup the specified state block and start a timer
1201 * to wait for an ADDBA response frame.
1202 */
1203static int
1204ieee80211_addba_request(struct ieee80211_node *ni,
1205	struct ieee80211_tx_ampdu *tap,
1206	int dialogtoken, int baparamset, int batimeout)
1207{
1208	int bufsiz;
1209
1210	/* XXX locking */
1211	tap->txa_token = dialogtoken;
1212	tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
1213	tap->txa_start = tap->txa_seqstart = 0;
1214	bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1215	tap->txa_wnd = (bufsiz == 0) ?
1216	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1217	addba_start_timeout(tap);
1218	return 1;
1219}
1220
1221/*
1222 * Default method for processing an A-MPDU tx aggregation
1223 * response.  We shutdown any pending timer and update the
1224 * state block according to the reply.
1225 */
1226static int
1227ieee80211_addba_response(struct ieee80211_node *ni,
1228	struct ieee80211_tx_ampdu *tap,
1229	int status, int baparamset, int batimeout)
1230{
1231	int bufsiz;
1232
1233	/* XXX locking */
1234	addba_stop_timeout(tap);
1235	if (status == IEEE80211_STATUS_SUCCESS) {
1236		bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1237		/* XXX override our request? */
1238		tap->txa_wnd = (bufsiz == 0) ?
1239		    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
1240		tap->txa_flags |= IEEE80211_AGGR_RUNNING;
1241	} else {
1242		/* mark tid so we don't try again */
1243		tap->txa_flags |= IEEE80211_AGGR_NAK;
1244	}
1245	return 1;
1246}
1247
1248/*
1249 * Default method for stopping A-MPDU tx aggregation.
1250 * Any timer is cleared and we drain any pending frames.
1251 */
1252static void
1253ieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
1254{
1255	/* XXX locking */
1256	addba_stop_timeout(tap);
1257	if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
1258		/* clear aggregation queue */
1259		ieee80211_drain_ifq(&tap->txa_q);
1260		tap->txa_flags &= ~IEEE80211_AGGR_RUNNING;
1261	}
1262	tap->txa_attempts = 0;
1263}
1264
1265/*
1266 * Process a received action frame using the default aggregation
1267 * policy.  We intercept ADDBA-related frames and use them to
1268 * update our aggregation state.  All other frames are passed up
1269 * for processing by ieee80211_recv_action.
1270 */
1271static void
1272ieee80211_aggr_recv_action(struct ieee80211_node *ni,
1273	const uint8_t *frm, const uint8_t *efrm)
1274{
1275	struct ieee80211com *ic = ni->ni_ic;
1276	const struct ieee80211_action *ia;
1277	struct ieee80211_rx_ampdu *rap;
1278	struct ieee80211_tx_ampdu *tap;
1279	uint8_t dialogtoken;
1280	uint16_t baparamset, batimeout, baseqctl, code;
1281	uint16_t args[4];
1282	int tid, ac, bufsiz;
1283
1284	ia = (const struct ieee80211_action *) frm;
1285	switch (ia->ia_category) {
1286	case IEEE80211_ACTION_CAT_BA:
1287		switch (ia->ia_action) {
1288		case IEEE80211_ACTION_BA_ADDBA_REQUEST:
1289			dialogtoken = frm[2];
1290			baparamset = LE_READ_2(frm+3);
1291			batimeout = LE_READ_2(frm+5);
1292			baseqctl = LE_READ_2(frm+7);
1293
1294			tid = MS(baparamset, IEEE80211_BAPS_TID);
1295			bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1296
1297			IEEE80211_NOTE(ic,
1298			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1299			    "recv ADDBA request: dialogtoken %u "
1300			    "baparamset 0x%x (tid %d bufsiz %d) batimeout %d "
1301			    "baseqctl %d:%d",
1302			    dialogtoken, baparamset, tid, bufsiz, batimeout,
1303			    MS(baseqctl, IEEE80211_BASEQ_START),
1304			    MS(baseqctl, IEEE80211_BASEQ_FRAG));
1305
1306			rap = &ni->ni_rx_ampdu[tid];
1307
1308			/* Send ADDBA response */
1309			args[0] = dialogtoken;
1310			/*
1311			 * NB: We ack only if the sta associated with HT and
1312			 * the ap is configured to do AMPDU rx (the latter
1313			 * violates the 11n spec and is mostly for testing).
1314			 */
1315			if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) &&
1316			    (ic->ic_flags_ext & IEEE80211_FEXT_AMPDU_RX)) {
1317				ampdu_rx_start(rap, bufsiz,
1318				    MS(baseqctl, IEEE80211_BASEQ_START));
1319
1320				args[1] = IEEE80211_STATUS_SUCCESS;
1321			} else {
1322				IEEE80211_NOTE(ic,
1323				    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1324				    ni, "reject ADDBA request: %s",
1325				    ni->ni_flags & IEEE80211_NODE_AMPDU_RX ?
1326				       "administratively disabled" :
1327				       "not negotiated for station");
1328				ic->ic_stats.is_addba_reject++;
1329				args[1] = IEEE80211_STATUS_UNSPECIFIED;
1330			}
1331			/* XXX honor rap flags? */
1332			args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
1333				| SM(tid, IEEE80211_BAPS_TID)
1334				| SM(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
1335				;
1336			args[3] = 0;
1337			ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
1338				IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
1339			return;
1340
1341		case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
1342			dialogtoken = frm[2];
1343			code = LE_READ_2(frm+3);
1344			baparamset = LE_READ_2(frm+5);
1345			tid = MS(baparamset, IEEE80211_BAPS_TID);
1346			bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
1347			batimeout = LE_READ_2(frm+7);
1348
1349			ac = TID_TO_WME_AC(tid);
1350			tap = &ni->ni_tx_ampdu[ac];
1351			if ((tap->txa_flags & ~IEEE80211_AGGR_XCHGPEND) == 0) {
1352				IEEE80211_DISCARD_MAC(ic,
1353				    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1354				    ni->ni_macaddr, "ADDBA response",
1355				    "no pending ADDBA, tid %d dialogtoken %u "
1356				    "code %d", tid, dialogtoken, code);
1357				ic->ic_stats.is_addba_norequest++;
1358				return;
1359			}
1360			if (dialogtoken != tap->txa_token) {
1361				IEEE80211_DISCARD_MAC(ic,
1362				    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1363				    ni->ni_macaddr, "ADDBA response",
1364				    "dialogtoken mismatch: waiting for %d, "
1365				    "received %d, tid %d code %d",
1366				    tap->txa_token, dialogtoken, tid, code);
1367				ic->ic_stats.is_addba_badtoken++;
1368				return;
1369			}
1370
1371			IEEE80211_NOTE(ic,
1372			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1373			    "recv ADDBA response: dialogtoken %u code %d "
1374			    "baparamset 0x%x (tid %d bufsiz %d) batimeout %d",
1375			    dialogtoken, code, baparamset, tid, bufsiz,
1376			    batimeout);
1377			ic->ic_addba_response(ni, tap,
1378				code, baparamset, batimeout);
1379			return;
1380
1381		case IEEE80211_ACTION_BA_DELBA:
1382			baparamset = LE_READ_2(frm+2);
1383			code = LE_READ_2(frm+4);
1384
1385			tid = MS(baparamset, IEEE80211_DELBAPS_TID);
1386
1387			IEEE80211_NOTE(ic,
1388			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1389			    "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
1390			    "code %d", baparamset, tid,
1391			    MS(baparamset, IEEE80211_DELBAPS_INIT), code);
1392
1393			if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
1394				ac = TID_TO_WME_AC(tid);
1395				tap = &ni->ni_tx_ampdu[ac];
1396				ic->ic_addba_stop(ni, tap);
1397			} else {
1398				rap = &ni->ni_rx_ampdu[tid];
1399				ampdu_rx_stop(rap);
1400			}
1401			return;
1402		}
1403		break;
1404	}
1405	ieee80211_recv_action(ni, frm, efrm);
1406}
1407
1408/*
1409 * Process a received 802.11n action frame.
1410 * Aggregation-related frames are assumed to be handled
1411 * already; we handle any other frames we can, otherwise
1412 * complain about being unsupported (with debugging).
1413 */
1414void
1415ieee80211_recv_action(struct ieee80211_node *ni,
1416	const uint8_t *frm, const uint8_t *efrm)
1417{
1418	struct ieee80211com *ic = ni->ni_ic;
1419	const struct ieee80211_action *ia;
1420	int chw;
1421
1422	ia = (const struct ieee80211_action *) frm;
1423	switch (ia->ia_category) {
1424	case IEEE80211_ACTION_CAT_BA:
1425		IEEE80211_NOTE(ic,
1426		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1427		    "%s: BA action %d not implemented", __func__,
1428		    ia->ia_action);
1429		ic->ic_stats.is_rx_mgtdiscard++;
1430		break;
1431	case IEEE80211_ACTION_CAT_HT:
1432		switch (ia->ia_action) {
1433		case IEEE80211_ACTION_HT_TXCHWIDTH:
1434			chw = frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040 ? 40 : 20;
1435			if (chw != ni->ni_chw) {
1436				ni->ni_chw = chw;
1437				ni->ni_flags |= IEEE80211_NODE_CHWUPDATE;
1438			}
1439			IEEE80211_NOTE(ic,
1440			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1441		            "%s: HT txchwidth, width %d (%s)",
1442			    __func__, chw,
1443			    ni->ni_flags & IEEE80211_NODE_CHWUPDATE ?
1444				"new" : "no change");
1445			break;
1446		case IEEE80211_ACTION_HT_MIMOPWRSAVE:
1447			IEEE80211_NOTE(ic,
1448			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1449		            "%s: HT MIMO PS", __func__);
1450			break;
1451		default:
1452			IEEE80211_NOTE(ic,
1453			   IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1454		           "%s: HT action %d not implemented", __func__,
1455			   ia->ia_action);
1456			ic->ic_stats.is_rx_mgtdiscard++;
1457			break;
1458		}
1459		break;
1460	default:
1461		IEEE80211_NOTE(ic,
1462		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1463		    "%s: category %d not implemented", __func__,
1464		    ia->ia_category);
1465		ic->ic_stats.is_rx_mgtdiscard++;
1466		break;
1467	}
1468}
1469
1470/*
1471 * Transmit processing.
1472 */
1473
1474/*
1475 * Request A-MPDU tx aggregation.  Setup local state and
1476 * issue an ADDBA request.  BA use will only happen after
1477 * the other end replies with ADDBA response.
1478 */
1479int
1480ieee80211_ampdu_request(struct ieee80211_node *ni,
1481	struct ieee80211_tx_ampdu *tap)
1482{
1483	struct ieee80211com *ic = ni->ni_ic;
1484	uint16_t args[4];
1485	int tid, dialogtoken;
1486	static int tokens = 0;	/* XXX */
1487
1488	/* XXX locking */
1489	if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
1490		/* do deferred setup of state */
1491		IEEE80211_TAPQ_INIT(tap);
1492		callout_init(&tap->txa_timer, CALLOUT_MPSAFE);
1493		tap->txa_flags |= IEEE80211_AGGR_SETUP;
1494	}
1495	if (tap->txa_attempts >= IEEE80211_AGGR_MAXTRIES &&
1496	    (ticks - tap->txa_lastrequest) < IEEE80211_AGGR_MINRETRY) {
1497		/*
1498		 * Don't retry too often; IEEE80211_AGGR_MINRETRY
1499		 * defines the minimum interval we'll retry after
1500		 * IEEE80211_AGGR_MAXTRIES failed attempts to
1501		 * negotiate use.
1502		 */
1503		return 0;
1504	}
1505	/* XXX hack for not doing proper locking */
1506	tap->txa_flags &= ~IEEE80211_AGGR_NAK;
1507
1508	dialogtoken = (tokens+1) % 63;		/* XXX */
1509
1510	tid = WME_AC_TO_TID(tap->txa_ac);
1511	args[0] = dialogtoken;
1512	args[1]	= IEEE80211_BAPS_POLICY_IMMEDIATE
1513		| SM(tid, IEEE80211_BAPS_TID)
1514		| SM(IEEE80211_AGGR_BAWMAX, IEEE80211_BAPS_BUFSIZ)
1515		;
1516	args[2] = 0;	/* batimeout */
1517	args[3] = SM(0, IEEE80211_BASEQ_START)
1518		| SM(0, IEEE80211_BASEQ_FRAG)
1519		;
1520	/* NB: do first so there's no race against reply */
1521	if (!ic->ic_addba_request(ni, tap, dialogtoken, args[1], args[2])) {
1522		/* unable to setup state, don't make request */
1523		IEEE80211_NOTE(ni->ni_ic, IEEE80211_MSG_11N,
1524		    ni, "%s: could not setup BA stream for AC %d",
1525		    __func__, tap->txa_ac);
1526		/* defer next try so we don't slam the driver with requests */
1527		tap->txa_attempts = IEEE80211_AGGR_MAXTRIES;
1528		tap->txa_lastrequest = ticks;
1529		return 0;
1530	}
1531	tokens = dialogtoken;			/* allocate token */
1532	return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
1533		IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
1534}
1535
1536/*
1537 * Terminate an AMPDU tx stream.  State is reclaimed
1538 * and the peer notified with a DelBA Action frame.
1539 */
1540void
1541ieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
1542{
1543	struct ieee80211com *ic = ni->ni_ic;
1544	uint16_t args[4];
1545
1546	/* XXX locking */
1547	if (IEEE80211_AMPDU_RUNNING(tap)) {
1548		IEEE80211_NOTE(ic, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1549		    ni, "%s: stop BA stream for AC %d", __func__, tap->txa_ac);
1550		ic->ic_stats.is_ampdu_stop++;
1551
1552		ic->ic_addba_stop(ni, tap);
1553		args[0] = WME_AC_TO_TID(tap->txa_ac);
1554		args[1] = IEEE80211_DELBAPS_INIT;
1555		args[2] = 1;				/* XXX reason code */
1556		ieee80211_send_action(ni, IEEE80211_ACTION_CAT_BA,
1557			IEEE80211_ACTION_BA_DELBA, args);
1558	} else {
1559		IEEE80211_NOTE(ic, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1560		    ni, "%s: BA stream for AC %d not running",
1561		    __func__, tap->txa_ac);
1562		ic->ic_stats.is_ampdu_stop_failed++;
1563	}
1564}
1565
1566/*
1567 * Transmit a BAR frame to the specified node.  The
1568 * BAR contents are drawn from the supplied aggregation
1569 * state associated with the node.
1570 */
1571int
1572ieee80211_send_bar(struct ieee80211_node *ni,
1573	const struct ieee80211_tx_ampdu *tap)
1574{
1575#define	senderr(_x, _v)	do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
1576#define	ADDSHORT(frm, v) do {			\
1577	frm[0] = (v) & 0xff;			\
1578	frm[1] = (v) >> 8;			\
1579	frm += 2;				\
1580} while (0)
1581	struct ieee80211com *ic = ni->ni_ic;
1582	struct ifnet *ifp = ic->ic_ifp;
1583	struct ieee80211_frame_min *wh;
1584	struct mbuf *m;
1585	uint8_t *frm;
1586	uint16_t barctl, barseqctl;
1587	int tid, ret;
1588
1589	ieee80211_ref_node(ni);
1590
1591	m = ieee80211_getmgtframe(&frm,
1592		ic->ic_headroom + sizeof(struct ieee80211_frame_min),
1593		sizeof(struct ieee80211_ba_request)
1594	);
1595	if (m == NULL)
1596		senderr(ENOMEM, is_tx_nobuf);
1597
1598	wh = mtod(m, struct ieee80211_frame_min *);
1599	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 |
1600		IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
1601	wh->i_fc[1] = 0;
1602	IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1603	IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
1604
1605	tid = WME_AC_TO_TID(tap->txa_ac);
1606	barctl 	= (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
1607			IEEE80211_BAPS_POLICY_IMMEDIATE :
1608			IEEE80211_BAPS_POLICY_DELAYED)
1609		| SM(tid, IEEE80211_BAPS_TID)
1610		| SM(tap->txa_wnd, IEEE80211_BAPS_BUFSIZ)
1611		;
1612	barseqctl = SM(tap->txa_start, IEEE80211_BASEQ_START)
1613		| SM(0, IEEE80211_BASEQ_FRAG)
1614		;
1615	ADDSHORT(frm, barctl);
1616	ADDSHORT(frm, barseqctl);
1617	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1618
1619	IEEE80211_NODE_STAT(ni, tx_mgmt);	/* XXX tx_ctl? */
1620
1621	IEEE80211_NOTE(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1622	    ni, "send bar frame (tid %u start %u) on channel %u",
1623	    tid, tap->txa_start, ieee80211_chan2ieee(ic, ic->ic_curchan));
1624
1625	m->m_pkthdr.rcvif = (void *)ni;
1626	IF_ENQUEUE(&ic->ic_mgtq, m);		/* cheat */
1627	if_start(ifp);
1628
1629	return 0;
1630bad:
1631	ieee80211_free_node(ni);
1632	return ret;
1633#undef ADDSHORT
1634#undef senderr
1635}
1636
1637/*
1638 * Send an action management frame.  The arguments are stuff
1639 * into a frame without inspection; the caller is assumed to
1640 * prepare them carefully (e.g. based on the aggregation state).
1641 */
1642int
1643ieee80211_send_action(struct ieee80211_node *ni,
1644	int category, int action, uint16_t args[4])
1645{
1646#define	senderr(_x, _v)	do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
1647#define	ADDSHORT(frm, v) do {			\
1648	frm[0] = (v) & 0xff;			\
1649	frm[1] = (v) >> 8;			\
1650	frm += 2;				\
1651} while (0)
1652	struct ieee80211com *ic = ni->ni_ic;
1653	struct mbuf *m;
1654	uint8_t *frm;
1655	uint16_t baparamset;
1656	int ret;
1657
1658	KASSERT(ni != NULL, ("null node"));
1659
1660	/*
1661	 * Hold a reference on the node so it doesn't go away until after
1662	 * the xmit is complete all the way in the driver.  On error we
1663	 * will remove our reference.
1664	 */
1665	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1666		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1667		__func__, __LINE__,
1668		ni, ether_sprintf(ni->ni_macaddr),
1669		ieee80211_node_refcnt(ni)+1);
1670	ieee80211_ref_node(ni);
1671
1672	m = ieee80211_getmgtframe(&frm,
1673		ic->ic_headroom + sizeof(struct ieee80211_frame),
1674		  sizeof(uint16_t)	/* action+category */
1675		/* XXX may action payload */
1676		+ sizeof(struct ieee80211_action_ba_addbaresponse)
1677	);
1678	if (m == NULL)
1679		senderr(ENOMEM, is_tx_nobuf);
1680
1681	*frm++ = category;
1682	*frm++ = action;
1683	switch (category) {
1684	case IEEE80211_ACTION_CAT_BA:
1685		switch (action) {
1686		case IEEE80211_ACTION_BA_ADDBA_REQUEST:
1687			IEEE80211_NOTE(ic,
1688			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1689			    "send ADDBA request: dialogtoken %d "
1690			    "baparamset 0x%x (tid %d) batimeout 0x%x baseqctl 0x%x",
1691			    args[0], args[1], MS(args[1], IEEE80211_BAPS_TID),
1692			    args[2], args[3]);
1693
1694			*frm++ = args[0];	/* dialog token */
1695			ADDSHORT(frm, args[1]);	/* baparamset */
1696			ADDSHORT(frm, args[2]);	/* batimeout */
1697			ADDSHORT(frm, args[3]);	/* baseqctl */
1698			break;
1699		case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
1700			IEEE80211_NOTE(ic,
1701			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1702			    "send ADDBA response: dialogtoken %d status %d "
1703			    "baparamset 0x%x (tid %d) batimeout %d",
1704			    args[0], args[1], args[2],
1705			    MS(args[2], IEEE80211_BAPS_TID), args[3]);
1706
1707			*frm++ = args[0];	/* dialog token */
1708			ADDSHORT(frm, args[1]);	/* statuscode */
1709			ADDSHORT(frm, args[2]);	/* baparamset */
1710			ADDSHORT(frm, args[3]);	/* batimeout */
1711			break;
1712		case IEEE80211_ACTION_BA_DELBA:
1713			/* XXX */
1714			baparamset = SM(args[0], IEEE80211_DELBAPS_TID)
1715				   | SM(args[1], IEEE80211_DELBAPS_INIT)
1716				   ;
1717			ADDSHORT(frm, baparamset);
1718			ADDSHORT(frm, args[2]);	/* reason code */
1719
1720			IEEE80211_NOTE(ic,
1721			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1722			    "send DELBA action: tid %d, initiator %d reason %d",
1723			    args[0], args[1], args[2]);
1724			break;
1725		default:
1726			goto badaction;
1727		}
1728		break;
1729	case IEEE80211_ACTION_CAT_HT:
1730		switch (action) {
1731		case IEEE80211_ACTION_HT_TXCHWIDTH:
1732			IEEE80211_NOTE(ic,
1733			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1734			    ni, "send HT txchwidth: width %d",
1735			    IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) ? 40 : 20
1736			);
1737			*frm++ = IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) ?
1738				IEEE80211_A_HT_TXCHWIDTH_2040 :
1739				IEEE80211_A_HT_TXCHWIDTH_20;
1740			break;
1741		default:
1742			goto badaction;
1743		}
1744		break;
1745	default:
1746	badaction:
1747		IEEE80211_NOTE(ic,
1748		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1749		    "%s: unsupported category %d action %d", __func__,
1750		    category, action);
1751		senderr(EINVAL, is_tx_unknownmgt);
1752		/* NOTREACHED */
1753	}
1754	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1755
1756	ret = ieee80211_mgmt_output(ic, ni, m, IEEE80211_FC0_SUBTYPE_ACTION);
1757	if (ret != 0)
1758		goto bad;
1759	return 0;
1760bad:
1761	ieee80211_free_node(ni);
1762	return ret;
1763#undef ADDSHORT
1764#undef senderr
1765}
1766
1767/*
1768 * Construct the MCS bit mask for inclusion
1769 * in an HT information element.
1770 */
1771static void
1772ieee80211_set_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
1773{
1774	int i;
1775
1776	for (i = 0; i < rs->rs_nrates; i++) {
1777		int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
1778		if (r < IEEE80211_HTRATE_MAXSIZE) {	/* XXX? */
1779			/* NB: this assumes a particular implementation */
1780			setbit(frm, r);
1781		}
1782	}
1783}
1784
1785/*
1786 * Add body of an HTCAP information element.
1787 */
1788static uint8_t *
1789ieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
1790{
1791#define	ADDSHORT(frm, v) do {			\
1792	frm[0] = (v) & 0xff;			\
1793	frm[1] = (v) >> 8;			\
1794	frm += 2;				\
1795} while (0)
1796	struct ieee80211com *ic = ni->ni_ic;
1797	uint16_t caps;
1798
1799	/* HT capabilities */
1800	caps = ic->ic_htcaps & 0xffff;
1801	/*
1802	 * Note channel width depends on whether we are operating as
1803	 * a sta or not.  When operating as a sta we are generating
1804	 * a request based on our desired configuration.  Otherwise
1805	 * we are operational and the channel attributes identify
1806	 * how we've been setup (which might be different if a fixed
1807	 * channel is specified).
1808	 */
1809	if (ic->ic_opmode == IEEE80211_M_STA) {
1810		/* override 20/40 use based on config */
1811		if (ic->ic_flags_ext & IEEE80211_FEXT_USEHT40)
1812			caps |= IEEE80211_HTCAP_CHWIDTH40;
1813		else
1814			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
1815	} else {
1816		/* override 20/40 use based on current channel */
1817		if (IEEE80211_IS_CHAN_HT40(ic->ic_bsschan))
1818			caps |= IEEE80211_HTCAP_CHWIDTH40;
1819		else
1820			caps &= ~IEEE80211_HTCAP_CHWIDTH40;
1821	}
1822	/* adjust short GI based on channel and config */
1823	if ((ic->ic_flags_ext & IEEE80211_FEXT_SHORTGI20) == 0)
1824		caps &= ~IEEE80211_HTCAP_SHORTGI20;
1825	if ((ic->ic_flags_ext & IEEE80211_FEXT_SHORTGI40) == 0 ||
1826	    (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
1827		caps &= ~IEEE80211_HTCAP_SHORTGI40;
1828	ADDSHORT(frm, caps);
1829
1830	/* HT parameters */
1831	*frm = SM(ic->ic_ampdu_rxmax, IEEE80211_HTCAP_MAXRXAMPDU)
1832	     | SM(ic->ic_ampdu_density, IEEE80211_HTCAP_MPDUDENSITY)
1833	     ;
1834	frm++;
1835
1836	/* pre-zero remainder of ie */
1837	memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
1838		__offsetof(struct ieee80211_ie_htcap, hc_mcsset));
1839
1840	/* supported MCS set */
1841	/*
1842	 * XXX it would better to get the rate set from ni_htrates
1843	 * so we can restrict it but for sta mode ni_htrates isn't
1844	 * setup when we're called to form an AssocReq frame so for
1845	 * now we're restricted to the default HT rate set.
1846	 */
1847	ieee80211_set_htrates(frm, &ieee80211_rateset_11n);
1848
1849	frm += sizeof(struct ieee80211_ie_htcap) -
1850		__offsetof(struct ieee80211_ie_htcap, hc_mcsset);
1851	return frm;
1852#undef ADDSHORT
1853}
1854
1855/*
1856 * Add 802.11n HT capabilities information element
1857 */
1858uint8_t *
1859ieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
1860{
1861	frm[0] = IEEE80211_ELEMID_HTCAP;
1862	frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
1863	return ieee80211_add_htcap_body(frm + 2, ni);
1864}
1865
1866/*
1867 * Add Broadcom OUI wrapped standard HTCAP ie; this is
1868 * used for compatibility w/ pre-draft implementations.
1869 */
1870uint8_t *
1871ieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
1872{
1873	frm[0] = IEEE80211_ELEMID_VENDOR;
1874	frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
1875	frm[2] = (BCM_OUI >> 0) & 0xff;
1876	frm[3] = (BCM_OUI >> 8) & 0xff;
1877	frm[4] = (BCM_OUI >> 16) & 0xff;
1878	frm[5] = BCM_OUI_HTCAP;
1879	return ieee80211_add_htcap_body(frm + 6, ni);
1880}
1881
1882/*
1883 * Construct the MCS bit mask of basic rates
1884 * for inclusion in an HT information element.
1885 */
1886static void
1887ieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
1888{
1889	int i;
1890
1891	for (i = 0; i < rs->rs_nrates; i++) {
1892		int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
1893		if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
1894		    r < IEEE80211_HTRATE_MAXSIZE) {
1895			/* NB: this assumes a particular implementation */
1896			setbit(frm, r);
1897		}
1898	}
1899}
1900
1901/*
1902 * Update the HTINFO ie for a beacon frame.
1903 */
1904void
1905ieee80211_ht_update_beacon(struct ieee80211com *ic,
1906	struct ieee80211_beacon_offsets *bo)
1907{
1908#define	PROTMODE	(IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
1909	struct ieee80211_ie_htinfo *ht =
1910	   (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
1911
1912	/* XXX only update on channel change */
1913	ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, ic->ic_bsschan);
1914	ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
1915	if (IEEE80211_IS_CHAN_HT40U(ic->ic_bsschan))
1916		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
1917	else if (IEEE80211_IS_CHAN_HT40D(ic->ic_bsschan))
1918		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
1919	else
1920		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
1921	if (IEEE80211_IS_CHAN_HT40(ic->ic_bsschan))
1922		ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
1923
1924	/* protection mode */
1925	ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
1926
1927	/* XXX propagate to vendor ie's */
1928#undef PROTMODE
1929}
1930
1931/*
1932 * Add body of an HTINFO information element.
1933 *
1934 * NB: We don't use struct ieee80211_ie_htinfo because we can
1935 * be called to fillin both a standard ie and a compat ie that
1936 * has a vendor OUI at the front.
1937 */
1938static uint8_t *
1939ieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
1940{
1941	struct ieee80211com *ic = ni->ni_ic;
1942
1943	/* pre-zero remainder of ie */
1944	memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
1945
1946	/* primary/control channel center */
1947	*frm++ = ieee80211_chan2ieee(ic, ic->ic_bsschan);
1948
1949	frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
1950	if (IEEE80211_IS_CHAN_HT40U(ic->ic_bsschan))
1951		frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
1952	else if (IEEE80211_IS_CHAN_HT40D(ic->ic_bsschan))
1953		frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
1954	else
1955		frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
1956	if (IEEE80211_IS_CHAN_HT40(ic->ic_bsschan))
1957		frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
1958
1959	frm[1] = ic->ic_curhtprotmode;
1960
1961	frm += 5;
1962
1963	/* basic MCS set */
1964	ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
1965	frm += sizeof(struct ieee80211_ie_htinfo) -
1966		__offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
1967	return frm;
1968}
1969
1970/*
1971 * Add 802.11n HT information information element.
1972 */
1973uint8_t *
1974ieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
1975{
1976	frm[0] = IEEE80211_ELEMID_HTINFO;
1977	frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
1978	return ieee80211_add_htinfo_body(frm + 2, ni);
1979}
1980
1981/*
1982 * Add Broadcom OUI wrapped standard HTINFO ie; this is
1983 * used for compatibility w/ pre-draft implementations.
1984 */
1985uint8_t *
1986ieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
1987{
1988	frm[0] = IEEE80211_ELEMID_VENDOR;
1989	frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
1990	frm[2] = (BCM_OUI >> 0) & 0xff;
1991	frm[3] = (BCM_OUI >> 8) & 0xff;
1992	frm[4] = (BCM_OUI >> 16) & 0xff;
1993	frm[5] = BCM_OUI_HTINFO;
1994	return ieee80211_add_htinfo_body(frm + 6, ni);
1995}
1996