ieee80211_ht.c revision 172226
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 172226 2007-09-18 20:46:36Z 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#define	IEEE80211_AGGR_TIMEOUT	msecs_to_ticks(250)
83#define	IEEE80211_AGGR_MINRETRY	msecs_to_ticks(10*1000)
84#define	IEEE80211_AGGR_MAXTRIES	3
85
86static int ieee80211_addba_request(struct ieee80211_node *ni,
87	struct ieee80211_tx_ampdu *tap,
88	int dialogtoken, int baparamset, int batimeout);
89static int ieee80211_addba_response(struct ieee80211_node *ni,
90	struct ieee80211_tx_ampdu *tap,
91	int code, int baparamset, int batimeout);
92static void ieee80211_addba_stop(struct ieee80211_node *ni,
93	struct ieee80211_tx_ampdu *tap);
94static void ieee80211_aggr_recv_action(struct ieee80211_node *ni,
95	const uint8_t *frm, const uint8_t *efrm);
96
97void
98ieee80211_ht_attach(struct ieee80211com *ic)
99{
100
101	ic->ic_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_8K;
102	ic->ic_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
103	ic->ic_ampdu_limit = ic->ic_ampdu_rxmax;
104
105	ic->ic_amsdu_limit = IEEE80211_HTCAP_MAXAMSDU_3839;
106
107	/* setup default aggregation policy */
108	ic->ic_recv_action = ieee80211_aggr_recv_action;
109	ic->ic_send_action = ieee80211_send_action;
110	ic->ic_addba_request = ieee80211_addba_request;
111	ic->ic_addba_response = ieee80211_addba_response;
112	ic->ic_addba_stop = ieee80211_addba_stop;
113
114	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
115	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
116		/*
117		 * There are HT channels in the channel list; enable
118		 * all HT-related facilities by default.
119		 * XXX these choices may be too aggressive.
120		 */
121		ic->ic_flags_ext |= IEEE80211_FEXT_HT
122				 |  IEEE80211_FEXT_HTCOMPAT
123				 ;
124		if (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20)
125			ic->ic_flags_ext |= IEEE80211_FEXT_SHORTGI20;
126		/* XXX infer from channel list */
127		if (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) {
128			ic->ic_flags_ext |= IEEE80211_FEXT_USEHT40;
129			if (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)
130				ic->ic_flags_ext |= IEEE80211_FEXT_SHORTGI40;
131		}
132		/* NB: A-MPDU and A-MSDU rx are mandated, these are tx only */
133		ic->ic_flags_ext |= IEEE80211_FEXT_AMPDU_RX;
134		if (ic->ic_htcaps & IEEE80211_HTC_AMPDU)
135			ic->ic_flags_ext |= IEEE80211_FEXT_AMPDU_TX;
136		ic->ic_flags_ext |= IEEE80211_FEXT_AMSDU_RX;
137		if (ic->ic_htcaps & IEEE80211_HTC_AMSDU)
138			ic->ic_flags_ext |= IEEE80211_FEXT_AMSDU_TX;
139
140		ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE;
141	}
142}
143
144void
145ieee80211_ht_detach(struct ieee80211com *ic)
146{
147}
148
149static void
150ht_announce(struct ieee80211com *ic, int mode,
151	const struct ieee80211_htrateset *rs)
152{
153	struct ifnet *ifp = ic->ic_ifp;
154	int i, rate, mword;
155
156	if_printf(ifp, "%s MCS: ", ieee80211_phymode_name[mode]);
157	for (i = 0; i < rs->rs_nrates; i++) {
158		mword = ieee80211_rate2media(ic,
159		    rs->rs_rates[i] | IEEE80211_RATE_MCS, mode);
160		if (IFM_SUBTYPE(mword) != IFM_IEEE80211_MCS)
161			continue;
162		rate = ieee80211_htrates[rs->rs_rates[i]];
163		printf("%s%d%sMbps", (i != 0 ? " " : ""),
164		    rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
165	}
166	printf("\n");
167}
168
169void
170ieee80211_ht_announce(struct ieee80211com *ic)
171{
172	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA))
173		ht_announce(ic, IEEE80211_MODE_11NA, &ieee80211_rateset_11n);
174	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NG))
175		ht_announce(ic, IEEE80211_MODE_11NG, &ieee80211_rateset_11n);
176}
177
178const struct ieee80211_htrateset *
179ieee80211_get_suphtrates(struct ieee80211com *ic,
180	const struct ieee80211_channel *c)
181{
182	if (IEEE80211_IS_CHAN_HT(c))
183		return &ieee80211_rateset_11n;
184	/* XXX what's the right thing to do here? */
185	return (const struct ieee80211_htrateset *)
186		ieee80211_get_suprates(ic, c);
187}
188
189/*
190 * Receive processing.
191 */
192
193/*
194 * Decap the encapsulated A-MSDU frames and dispatch all but
195 * the last for delivery.  The last frame is returned for
196 * delivery via the normal path.
197 */
198struct mbuf *
199ieee80211_decap_amsdu(struct ieee80211_node *ni, struct mbuf *m)
200{
201	struct ieee80211com *ic = ni->ni_ic;
202	int totallen, framelen;
203	struct mbuf *n;
204
205	/* discard 802.3 header inserted by ieee80211_decap */
206	m_adj(m, sizeof(struct ether_header));
207
208	ic->ic_stats.is_amsdu_decap++;
209
210	totallen = m->m_pkthdr.len;
211	for (;;) {
212		/*
213		 * Decap the first frame, bust it apart from the
214		 * remainder and deliver.  We leave the last frame
215		 * delivery to the caller (for consistency with other
216		 * code paths, could also do it here).
217		 */
218		m = ieee80211_decap1(m, &framelen);
219		if (m == NULL) {
220			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
221			    ni->ni_macaddr, "a-msdu", "%s", "first decap failed");
222			ic->ic_stats.is_amsdu_tooshort++;
223			return NULL;
224		}
225		if (framelen == totallen)
226			break;
227		n = m_split(m, framelen, M_NOWAIT);
228		if (n == NULL) {
229			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
230			    ni->ni_macaddr, "a-msdu",
231			    "%s", "unable to split encapsulated frames");
232			ic->ic_stats.is_amsdu_split++;
233			m_freem(m);			/* NB: must reclaim */
234			return NULL;
235		}
236		ieee80211_deliver_data(ic, ni, m);
237
238		/*
239		 * Remove frame contents; each intermediate frame
240		 * is required to be aligned to a 4-byte boundary.
241		 */
242		m = n;
243		m_adj(m, roundup2(framelen, 4) - framelen);	/* padding */
244	}
245	return m;				/* last delivered by caller */
246}
247
248/*
249 * Start A-MPDU rx/re-order processing for the specified TID.
250 */
251static void
252ampdu_rx_start(struct ieee80211_rx_ampdu *rap, int bufsiz, int start)
253{
254	memset(rap, 0, sizeof(*rap));
255	rap->rxa_wnd = (bufsiz == 0) ?
256	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
257	rap->rxa_start = start;
258	rap->rxa_nxt = rap->rxa_start;
259	rap->rxa_flags |= IEEE80211_AGGR_XCHGPEND;
260}
261
262/*
263 * Purge all frames in the A-MPDU re-order queue.
264 */
265static void
266ampdu_rx_purge(struct ieee80211_rx_ampdu *rap)
267{
268	struct mbuf *m;
269	int i;
270
271	for (i = 0; i < rap->rxa_wnd; i++) {
272		m = rap->rxa_m[i];
273		if (m != NULL) {
274			rap->rxa_m[i] = NULL;
275			rap->rxa_qbytes -= m->m_pkthdr.len;
276			m_freem(m);
277			if (--rap->rxa_qframes == 0)
278				break;
279		}
280	}
281	KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0,
282	    ("lost %u data, %u frames on ampdu rx q",
283	    rap->rxa_qbytes, rap->rxa_qframes));
284}
285
286/*
287 * Stop A-MPDU rx processing for the specified TID.
288 */
289static void
290ampdu_rx_stop(struct ieee80211_rx_ampdu *rap)
291{
292	rap->rxa_flags &= ~IEEE80211_AGGR_XCHGPEND;
293	ampdu_rx_purge(rap);
294}
295
296/*
297 * Dispatch a frame from the A-MPDU reorder queue.  The
298 * frame is fed back into ieee80211_input marked with an
299 * M_AMPDU flag so it doesn't come back to us (it also
300 * permits ieee80211_input to optimize re-processing).
301 */
302static __inline void
303ampdu_dispatch(struct ieee80211_node *ni, struct mbuf *m)
304{
305	m->m_flags |= M_AMPDU;	/* bypass normal processing */
306	/* NB: rssi, noise, and rstamp are ignored w/ M_AMPDU set */
307	(void) ieee80211_input(ni->ni_ic, m, ni, 0, 0, 0);
308}
309
310/*
311 * Dispatch as many frames as possible from the re-order queue.
312 * Frames will always be "at the front"; we process all frames
313 * up to the first empty slot in the window.  On completion we
314 * cleanup state if there are still pending frames in the current
315 * BA window.  We assume the frame at slot 0 is already handled
316 * by the caller; we always start at slot 1.
317 */
318static void
319ampdu_rx_dispatch(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni)
320{
321	struct ieee80211com *ic = ni->ni_ic;
322	struct mbuf *m;
323	int i;
324
325	/* flush run of frames */
326	for (i = 1; i < rap->rxa_wnd; i++) {
327		m = rap->rxa_m[i];
328		if (m == NULL)
329			break;
330		rap->rxa_m[i] = NULL;
331		rap->rxa_qbytes -= m->m_pkthdr.len;
332		rap->rxa_qframes--;
333
334		ampdu_dispatch(ni, m);
335	}
336	/*
337	 * Adjust the start of the BA window to
338	 * reflect the frames just dispatched.
339	 */
340	rap->rxa_start = IEEE80211_SEQ_ADD(rap->rxa_start, i);
341	rap->rxa_nxt = rap->rxa_start;
342	ic->ic_stats.is_ampdu_rx_oor += i;
343	/*
344	 * If frames remain, copy the mbuf pointers down so
345	 * they correspond to the offsets in the new window.
346	 */
347	if (rap->rxa_qframes != 0) {
348		int n = rap->rxa_qframes, j;
349		for (j = i+1; j < rap->rxa_wnd; j++) {
350			if (rap->rxa_m[j] != NULL) {
351				rap->rxa_m[j-i] = rap->rxa_m[j];
352				rap->rxa_m[j] = NULL;
353				if (--n == 0)
354					break;
355			}
356		}
357		KASSERT(n == 0, ("lost %d frames", n));
358		ic->ic_stats.is_ampdu_rx_copy += rap->rxa_qframes;
359	}
360}
361
362/*
363 * Dispatch all frames in the A-MPDU
364 * re-order queue up to the specified slot.
365 */
366static void
367ampdu_rx_flush(struct ieee80211_node *ni,
368	struct ieee80211_rx_ampdu *rap, int limit)
369{
370	struct mbuf *m;
371	int i;
372
373	for (i = 0; i < limit; i++) {
374		m = rap->rxa_m[i];
375		if (m == NULL)
376			continue;
377		rap->rxa_m[i] = NULL;
378		rap->rxa_qbytes -= m->m_pkthdr.len;
379		ampdu_dispatch(ni, m);
380		if (--rap->rxa_qframes == 0)
381			break;
382	}
383}
384
385/*
386 * Process a received QoS data frame for an HT station.  Handle
387 * A-MPDU reordering: if this frame is received out of order
388 * and falls within the BA window hold onto it.  Otherwise if
389 * this frame completes a run flush any pending frames.  We
390 * return 1 if the frame is consumed.  A 0 is returned if
391 * the frame should be processed normally by the caller.
392 */
393int
394ieee80211_ampdu_reorder(struct ieee80211_node *ni, struct mbuf *m)
395{
396#define	IEEE80211_FC0_QOSDATA \
397	(IEEE80211_FC0_TYPE_DATA|IEEE80211_FC0_SUBTYPE_QOS|IEEE80211_FC0_VERSION_0)
398	struct ieee80211com *ic = ni->ni_ic;
399	struct ieee80211_qosframe *wh;
400	struct ieee80211_rx_ampdu *rap;
401	ieee80211_seq rxseq;
402	uint8_t tid;
403	int off;
404
405	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta"));
406
407	/* NB: m_len known to be sufficient */
408	wh = mtod(m, struct ieee80211_qosframe *);
409	KASSERT(wh->i_fc[0] == IEEE80211_FC0_QOSDATA, ("not QoS data"));
410
411	/* XXX 4-address frame */
412	tid = wh->i_qos[0] & IEEE80211_QOS_TID;
413	rap = &ni->ni_rx_ampdu[tid];
414	if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
415		/*
416		 * No ADDBA request yet, don't touch.
417		 */
418		return 0;
419	}
420	rxseq = le16toh(*(uint16_t *)wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
421	if (rxseq == rap->rxa_start) {
422		/*
423		 * First frame in window.
424		 */
425		if (rap->rxa_qframes != 0) {
426			/*
427			 * Dispatch as many packets as we can.
428			 */
429			KASSERT(rap->rxa_m[0] == NULL, ("unexpected dup"));
430			ampdu_dispatch(ni, m);
431			ampdu_rx_dispatch(rap, ni);
432			return 1;		/* NB: consumed */
433		} else {
434			/*
435			 * In order; advance window and notify
436			 * caller to dispatch directly.
437			 */
438			rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
439			rap->rxa_nxt = rap->rxa_start;
440			return 0;		/* NB: process packet */
441		}
442	}
443	/*
444	 * This packet is out of order; store it
445	 * if it's in the BA window.
446	 */
447	/* calculate offset in BA window */
448	off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
449	if (off >= rap->rxa_wnd) {
450		/*
451		 * Outside the window, clear the q and start over.
452		 *
453		 * NB: this handles the case where rxseq is before
454		 *     rxa_start because our max BA window is 64
455		 *     and the sequence number range is 4096.
456		 */
457		IEEE80211_NOTE(ic, IEEE80211_MSG_11N, ni,
458		    "flush BA win <%u:%u> (%u frames) rxseq %u tid %u",
459		    rap->rxa_start,
460		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd),
461		    rap->rxa_qframes, rxseq, tid);
462
463		if (rap->rxa_qframes != 0) {
464			ic->ic_stats.is_ampdu_rx_oor += rap->rxa_qframes;
465			ampdu_rx_flush(ni, rap, rap->rxa_wnd);
466			KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0,
467			    ("lost %u data, %u frames on ampdu rx q",
468			    rap->rxa_qbytes, rap->rxa_qframes));
469		}
470		rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
471		rap->rxa_nxt = rap->rxa_start;
472		return 0;	/* NB: process packet */
473	}
474	if (rap->rxa_qframes != 0) {
475#if 0
476		/* XXX honor batimeout? */
477		if (ticks - mn->mn_age[tid] > 50) {
478			/*
479			 * Too long since we received the first frame; flush.
480			 */
481			if (rap->rxa_qframes != 0) {
482				ic->ic_stats.is_ampdu_rx_oor +=
483				    rap->rxa_qframes;
484				ampdu_rx_flush(ni, rap, rap->rxa_wnd);
485			}
486			rap->rxa_start = IEEE80211_SEQ_INC(rxseq);
487			rap->rxa_nxt = rap->rxa_start;
488			return 0;		/* NB: process packet */
489		}
490#endif
491		rap->rxa_nxt = rxseq;
492	} else {
493		/*
494		 * First frame, start aging timer.
495		 */
496#if 0
497		mn->mn_age[tid] = ticks;
498#endif
499	}
500	/* save packet */
501	if (rap->rxa_m[off] == NULL) {
502		rap->rxa_m[off] = m;
503		rap->rxa_qframes++;
504		rap->rxa_qbytes += m->m_pkthdr.len;
505	} else {
506		IEEE80211_DISCARD_MAC(ic,
507		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
508		    ni->ni_macaddr, "a-mpdu duplicate",
509		    "seqno %u tid %u BA win <%u:%u>",
510		    rxseq, tid, rap->rxa_start, rap->rxa_wnd);
511		ic->ic_stats.is_rx_dup++;
512		IEEE80211_NODE_STAT(ni, rx_dup);
513		m_freem(m);
514	}
515	return 1;		/* NB: consumed */
516#undef IEEE80211_FC0_QOSDATA
517}
518
519/*
520 * Process a BAR ctl frame.  Dispatch all frames up to
521 * the sequence number of the frame.  If this frame is
522 * out of the window it's discarded.
523 */
524void
525ieee80211_recv_bar(struct ieee80211_node *ni, struct mbuf *m0)
526{
527	struct ieee80211com *ic = ni->ni_ic;
528	struct ieee80211_frame_bar *wh;
529	struct ieee80211_rx_ampdu *rap;
530	ieee80211_seq rxseq;
531	int tid, off;
532
533	wh = mtod(m0, struct ieee80211_frame_bar *);
534	/* XXX check basic BAR */
535	tid = MS(le16toh(wh->i_ctl), IEEE80211_BAR_TID);
536	rap = &ni->ni_rx_ampdu[tid];
537	if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) {
538		/*
539		 * No ADDBA request yet, don't touch.
540		 */
541		IEEE80211_DISCARD_MAC(ic,
542		    IEEE80211_MSG_INPUT | IEEE80211_MSG_11N,
543		    ni->ni_macaddr, "BAR", "no BA stream, tid %u", tid);
544		ic->ic_stats.is_ampdu_bar_bad++;
545		return;
546	}
547	ic->ic_stats.is_ampdu_bar_rx++;
548	rxseq = le16toh(wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
549	/* calculate offset in BA window */
550	off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start);
551	if (off >= rap->rxa_wnd) {
552		/*
553		 * Outside the window, flush the reorder q if
554		 * not pulling the sequence # backward.  The
555		 * latter is typically caused by a dropped BA.
556		 */
557		IEEE80211_NOTE(ic, IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni,
558		    "recv BAR outside BA win <%u:%u> rxseq %u tid %u",
559		    rap->rxa_start,
560		    IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd),
561		    rxseq, tid);
562		ic->ic_stats.is_ampdu_bar_oow++;
563		if (rxseq < rap->rxa_start) {
564			/* XXX stat? */
565			return;
566		}
567		if (rap->rxa_qframes != 0) {
568			ic->ic_stats.is_ampdu_rx_oor += rap->rxa_qframes;
569			ampdu_rx_flush(ni, rap, rap->rxa_wnd);
570			KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0,
571			    ("lost %u data, %u frames on ampdu rx q",
572			    rap->rxa_qbytes, rap->rxa_qframes));
573		}
574	} else if (rap->rxa_qframes != 0) {
575		/*
576		 * Dispatch packets up to rxseq.
577		 */
578		ampdu_rx_flush(ni, rap, off);
579		ic->ic_stats.is_ampdu_rx_oor += off;
580
581		/*
582		 * If frames remain, copy the mbuf pointers down so
583		 * they correspond to the offsets in the new window.
584		 */
585		if (rap->rxa_qframes != 0) {
586			int n = rap->rxa_qframes, j;
587			for (j = off+1; j < rap->rxa_wnd; j++) {
588				if (rap->rxa_m[j] != NULL) {
589					rap->rxa_m[j-off] = rap->rxa_m[j];
590					rap->rxa_m[j] = NULL;
591					if (--n == 0)
592						break;
593				}
594			}
595			KASSERT(n == 0, ("lost %d frames", n));
596			ic->ic_stats.is_ampdu_rx_copy += rap->rxa_qframes;
597		}
598	}
599	rap->rxa_start = rxseq;
600	rap->rxa_nxt = rap->rxa_start;
601}
602
603/*
604 * Setup HT-specific state in a node.  Called only
605 * when HT use is negotiated so we don't do extra
606 * work for temporary and/or legacy sta's.
607 */
608void
609ieee80211_ht_node_init(struct ieee80211_node *ni, const uint8_t *htcap)
610{
611	struct ieee80211_tx_ampdu *tap;
612	int ac;
613
614	ieee80211_parse_htcap(ni, htcap);
615	for (ac = 0; ac < WME_NUM_AC; ac++) {
616		tap = &ni->ni_tx_ampdu[ac];
617		tap->txa_ac = ac;
618	}
619	ni->ni_flags |= IEEE80211_NODE_HT;
620}
621
622/*
623 * Cleanup HT-specific state in a node.  Called only
624 * when HT use has been marked.
625 */
626void
627ieee80211_ht_node_cleanup(struct ieee80211_node *ni)
628{
629	struct ieee80211com *ic = ni->ni_ic;
630	int i;
631
632	KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node"));
633
634	/* XXX optimize this */
635	for (i = 0; i < WME_NUM_AC; i++) {
636		struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i];
637		if (IEEE80211_AMPDU_REQUESTED(tap))
638			ic->ic_addba_stop(ni, &ni->ni_tx_ampdu[i]);
639	}
640	for (i = 0; i < WME_NUM_TID; i++)
641		ampdu_rx_stop(&ni->ni_rx_ampdu[i]);
642
643	ni->ni_htcap = 0;
644	ni->ni_flags &= ~(IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT);
645}
646
647/* unalligned little endian access */
648#define LE_READ_2(p)					\
649	((uint16_t)					\
650	 ((((const uint8_t *)(p))[0]      ) |		\
651	  (((const uint8_t *)(p))[1] <<  8)))
652
653/*
654 * Process an 802.11n HT capabilities ie.
655 */
656void
657ieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie)
658{
659	struct ieee80211com *ic = ni->ni_ic;
660
661	if (ie[0] == IEEE80211_ELEMID_VENDOR) {
662		/*
663		 * Station used Vendor OUI ie to associate;
664		 * mark the node so when we respond we'll use
665		 * the Vendor OUI's and not the standard ie's.
666		 */
667		ni->ni_flags |= IEEE80211_NODE_HTCOMPAT;
668		ie += 4;
669	} else
670		ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT;
671
672	ni->ni_htcap = LE_READ_2(ie +
673		__offsetof(struct ieee80211_ie_htcap, hc_cap));
674	if ((ic->ic_flags_ext & IEEE80211_FEXT_SHORTGI40) == 0)
675		ni->ni_htcap &= ~IEEE80211_HTCAP_SHORTGI40;
676	if ((ic->ic_flags_ext & IEEE80211_FEXT_SHORTGI20) == 0)
677		ni->ni_htcap &= ~IEEE80211_HTCAP_SHORTGI20;
678	ni->ni_chw = (ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) ? 40 : 20;
679	ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)];
680#if 0
681	ni->ni_maxampdu =
682	    (8*1024) << MS(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
683	ni->ni_mpdudensity = MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
684#endif
685}
686
687/*
688 * Process an 802.11n HT info ie.
689 */
690void
691ieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie)
692{
693 	const struct ieee80211_ie_htinfo *htinfo;
694	uint16_t w;
695	int chw;
696
697	if (ie[0] == IEEE80211_ELEMID_VENDOR)
698		ie += 4;
699 	htinfo = (const struct ieee80211_ie_htinfo *) ie;
700	ni->ni_htctlchan = htinfo->hi_ctrlchannel;
701	ni->ni_ht2ndchan = SM(htinfo->hi_byte1, IEEE80211_HTINFO_2NDCHAN);
702	w = LE_READ_2(&htinfo->hi_byte2);
703	ni->ni_htopmode = SM(w, IEEE80211_HTINFO_OPMODE);
704	w = LE_READ_2(&htinfo->hi_byte45);
705	ni->ni_htstbc = SM(w, IEEE80211_HTINFO_BASIC_STBCMCS);
706	/* update node's recommended tx channel width */
707	chw = (htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) ? 40 : 20;
708	if (chw != ni->ni_chw) {
709		ni->ni_chw = chw;
710		ni->ni_flags |= IEEE80211_NODE_CHWUPDATE;
711	}
712}
713
714/*
715 * Install received HT rate set by parsing the HT cap ie.
716 */
717int
718ieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags)
719{
720	struct ieee80211com *ic = ni->ni_ic;
721	const struct ieee80211_ie_htcap *htcap;
722	struct ieee80211_htrateset *rs;
723	int i;
724
725	rs = &ni->ni_htrates;
726	memset(rs, 0, sizeof(*rs));
727	if (ie != NULL) {
728		if (ie[0] == IEEE80211_ELEMID_VENDOR)
729			ie += 4;
730		htcap = (const struct ieee80211_ie_htcap *) ie;
731		for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
732			if (isclr(htcap->hc_mcsset, i))
733				continue;
734			if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) {
735				IEEE80211_NOTE(ic,
736				    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
737				    "WARNING, HT rate set too large; only "
738				    "using %u rates", IEEE80211_HTRATE_MAXSIZE);
739				ic->ic_stats.is_rx_rstoobig++;
740				break;
741			}
742			rs->rs_rates[rs->rs_nrates++] = i;
743		}
744	}
745	return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags);
746}
747
748/*
749 * Mark rates in a node's HT rate set as basic according
750 * to the information in the supplied HT info ie.
751 */
752void
753ieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie)
754{
755	const struct ieee80211_ie_htinfo *htinfo;
756	struct ieee80211_htrateset *rs;
757	int i, j;
758
759	if (ie[0] == IEEE80211_ELEMID_VENDOR)
760		ie += 4;
761	htinfo = (const struct ieee80211_ie_htinfo *) ie;
762	rs = &ni->ni_htrates;
763	if (rs->rs_nrates == 0) {
764		IEEE80211_NOTE(ni->ni_ic,
765		    IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
766		    "%s", "WARNING, empty HT rate set");
767		return;
768	}
769	for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) {
770		if (isclr(htinfo->hi_basicmcsset, i))
771			continue;
772		for (j = 0; j < rs->rs_nrates; j++)
773			if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i)
774				rs->rs_rates[j] |= IEEE80211_RATE_BASIC;
775	}
776}
777
778static void
779addba_timeout(void *arg)
780{
781	struct ieee80211_tx_ampdu *tap = arg;
782
783	/* XXX ? */
784	tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
785	tap->txa_attempts++;
786}
787
788static void
789addba_start_timeout(struct ieee80211_tx_ampdu *tap)
790{
791	/* XXX use CALLOUT_PENDING instead? */
792	callout_reset(&tap->txa_timer, IEEE80211_AGGR_TIMEOUT,
793	    addba_timeout, tap);
794	tap->txa_flags |= IEEE80211_AGGR_XCHGPEND;
795	tap->txa_lastrequest = ticks;
796}
797
798static void
799addba_stop_timeout(struct ieee80211_tx_ampdu *tap)
800{
801	/* XXX use CALLOUT_PENDING instead? */
802	if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) {
803		callout_stop(&tap->txa_timer);
804		tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND;
805	}
806}
807
808/*
809 * Default method for requesting A-MPDU tx aggregation.
810 * We setup the specified state block and start a timer
811 * to wait for an ADDBA response frame.
812 */
813static int
814ieee80211_addba_request(struct ieee80211_node *ni,
815	struct ieee80211_tx_ampdu *tap,
816	int dialogtoken, int baparamset, int batimeout)
817{
818	int bufsiz;
819
820	/* XXX locking */
821	tap->txa_token = dialogtoken;
822	tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE;
823	tap->txa_start = tap->txa_seqstart = 0;
824	bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
825	tap->txa_wnd = (bufsiz == 0) ?
826	    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
827	addba_start_timeout(tap);
828	return 1;
829}
830
831/*
832 * Default method for processing an A-MPDU tx aggregation
833 * response.  We shutdown any pending timer and update the
834 * state block according to the reply.
835 */
836static int
837ieee80211_addba_response(struct ieee80211_node *ni,
838	struct ieee80211_tx_ampdu *tap,
839	int status, int baparamset, int batimeout)
840{
841	int bufsiz;
842
843	/* XXX locking */
844	addba_stop_timeout(tap);
845	if (status == IEEE80211_STATUS_SUCCESS) {
846		bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
847		/* XXX override our request? */
848		tap->txa_wnd = (bufsiz == 0) ?
849		    IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX);
850		tap->txa_flags |= IEEE80211_AGGR_RUNNING;
851	}
852	return 1;
853}
854
855/*
856 * Default method for stopping A-MPDU tx aggregation.
857 * Any timer is cleared and we drain any pending frames.
858 */
859static void
860ieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
861{
862	/* XXX locking */
863	addba_stop_timeout(tap);
864	if (tap->txa_flags & IEEE80211_AGGR_RUNNING) {
865		/* clear aggregation queue */
866		ieee80211_drain_ifq(&tap->txa_q);
867		tap->txa_flags &= ~IEEE80211_AGGR_RUNNING;
868	}
869	tap->txa_attempts = 0;
870}
871
872/*
873 * Process a received action frame using the default aggregation
874 * policy.  We intercept ADDBA-related frames and use them to
875 * update our aggregation state.  All other frames are passed up
876 * for processing by ieee80211_recv_action.
877 */
878static void
879ieee80211_aggr_recv_action(struct ieee80211_node *ni,
880	const uint8_t *frm, const uint8_t *efrm)
881{
882	struct ieee80211com *ic = ni->ni_ic;
883	const struct ieee80211_action *ia;
884	struct ieee80211_rx_ampdu *rap;
885	struct ieee80211_tx_ampdu *tap;
886	uint8_t dialogtoken;
887	uint16_t baparamset, batimeout, baseqctl, code;
888	uint16_t args[4];
889	int tid, ac, bufsiz;
890
891	ia = (const struct ieee80211_action *) frm;
892	switch (ia->ia_category) {
893	case IEEE80211_ACTION_CAT_BA:
894		switch (ia->ia_action) {
895		case IEEE80211_ACTION_BA_ADDBA_REQUEST:
896			dialogtoken = frm[2];
897			baparamset = LE_READ_2(frm+3);
898			batimeout = LE_READ_2(frm+5);
899			baseqctl = LE_READ_2(frm+7);
900
901			tid = MS(baparamset, IEEE80211_BAPS_TID);
902			bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
903
904			IEEE80211_NOTE(ic,
905			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
906			    "recv ADDBA request: dialogtoken %u "
907			    "baparamset 0x%x (tid %d bufsiz %d) batimeout %d "
908			    "baseqctl %d",
909			    dialogtoken, baparamset, tid, bufsiz,
910			    batimeout, baseqctl);
911
912			rap = &ni->ni_rx_ampdu[tid];
913
914			/* Send ADDBA response */
915			args[0] = dialogtoken;
916			if (ic->ic_flags_ext & IEEE80211_FEXT_AMPDU_RX) {
917				ampdu_rx_start(rap, bufsiz,
918				    MS(baseqctl, IEEE80211_BASEQ_START));
919
920				args[1] = IEEE80211_STATUS_SUCCESS;
921			} else
922				args[1] = IEEE80211_STATUS_UNSPECIFIED;
923			/* XXX honor rap flags? */
924			args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE
925				| SM(tid, IEEE80211_BAPS_TID)
926				| SM(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ)
927				;
928			args[3] = 0;
929			ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
930				IEEE80211_ACTION_BA_ADDBA_RESPONSE, args);
931			return;
932
933		case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
934			dialogtoken = frm[2];
935			code = LE_READ_2(frm+3);
936			baparamset = LE_READ_2(frm+5);
937			tid = MS(baparamset, IEEE80211_BAPS_TID);
938			bufsiz = MS(baparamset, IEEE80211_BAPS_BUFSIZ);
939			batimeout = LE_READ_2(frm+7);
940
941			IEEE80211_NOTE(ic,
942			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
943			    "recv ADDBA response: dialogtoken %u code %d "
944			    "baparamset 0x%x (tid %d bufsiz %d) batimeout %d",
945			    dialogtoken, code, baparamset, tid, bufsiz,
946			    batimeout);
947
948			ac = TID_TO_WME_AC(tid);
949			tap = &ni->ni_tx_ampdu[ac];
950
951			ic->ic_addba_response(ni, tap,
952				code, baparamset, batimeout);
953			return;
954
955		case IEEE80211_ACTION_BA_DELBA:
956			baparamset = LE_READ_2(frm+2);
957			code = LE_READ_2(frm+4);
958
959			tid = MS(baparamset, IEEE80211_DELBAPS_TID);
960
961			IEEE80211_NOTE(ic,
962			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
963			    "recv DELBA: baparamset 0x%x (tid %d initiator %d) "
964			    "code %d", baparamset, tid,
965			    MS(baparamset, IEEE80211_DELBAPS_INIT), code);
966
967			if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) {
968				ac = TID_TO_WME_AC(tid);
969				tap = &ni->ni_tx_ampdu[ac];
970				ic->ic_addba_stop(ni, tap);
971			} else {
972				rap = &ni->ni_rx_ampdu[tid];
973				ampdu_rx_stop(rap);
974			}
975			return;
976		}
977		break;
978	}
979	return ieee80211_recv_action(ni, frm, efrm);
980}
981
982/*
983 * Process a received 802.11n action frame.
984 * Aggregation-related frames are assumed to be handled
985 * already; we handle any other frames we can, otherwise
986 * complain about being unsupported (with debugging).
987 */
988void
989ieee80211_recv_action(struct ieee80211_node *ni,
990	const uint8_t *frm, const uint8_t *efrm)
991{
992	struct ieee80211com *ic = ni->ni_ic;
993	const struct ieee80211_action *ia;
994	int chw;
995
996	ia = (const struct ieee80211_action *) frm;
997	switch (ia->ia_category) {
998	case IEEE80211_ACTION_CAT_BA:
999		IEEE80211_NOTE(ic,
1000		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1001		    "%s: BA action %d not implemented", __func__,
1002		    ia->ia_action);
1003		ic->ic_stats.is_rx_mgtdiscard++;
1004		break;
1005	case IEEE80211_ACTION_CAT_HT:
1006		switch (ia->ia_action) {
1007		case IEEE80211_ACTION_HT_TXCHWIDTH:
1008			chw = frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040 ? 40 : 20;
1009			if (chw != ni->ni_chw) {
1010				ni->ni_chw = chw;
1011				ni->ni_flags |= IEEE80211_NODE_CHWUPDATE;
1012			}
1013			IEEE80211_NOTE(ic,
1014			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1015		            "%s: HT txchwidth. width %d (%s)",
1016			    __func__, chw,
1017			    ni->ni_flags & IEEE80211_NODE_CHWUPDATE ?
1018				"new" : "no change");
1019			break;
1020		default:
1021			IEEE80211_NOTE(ic,
1022			   IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1023		           "%s: HT action %d not implemented", __func__,
1024			   ia->ia_action);
1025			ic->ic_stats.is_rx_mgtdiscard++;
1026			break;
1027		}
1028		break;
1029	default:
1030		IEEE80211_NOTE(ic,
1031		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1032		    "%s: category %d not implemented", __func__,
1033		    ia->ia_category);
1034		ic->ic_stats.is_rx_mgtdiscard++;
1035		break;
1036	}
1037}
1038
1039/*
1040 * Transmit processing.
1041 */
1042
1043/*
1044 * Request A-MPDU tx aggregation.  Setup local state and
1045 * issue an ADDBA request.  BA use will only happen after
1046 * the other end replies with ADDBA response.
1047 */
1048int
1049ieee80211_ampdu_request(struct ieee80211_node *ni,
1050	struct ieee80211_tx_ampdu *tap)
1051{
1052	struct ieee80211com *ic = ni->ni_ic;
1053	uint16_t args[4];
1054	int tid, dialogtoken;
1055	static int tokens = 0;	/* XXX */
1056
1057	/* XXX locking */
1058	if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) {
1059		/* do deferred setup of state */
1060		/* XXX tap->txa_q */
1061		callout_init(&tap->txa_timer, CALLOUT_MPSAFE);
1062		tap->txa_flags |= IEEE80211_AGGR_SETUP;
1063	}
1064	if (tap->txa_attempts >= IEEE80211_AGGR_MAXTRIES &&
1065	    (ticks - tap->txa_lastrequest) < IEEE80211_AGGR_MINRETRY) {
1066		/*
1067		 * Don't retry too often; IEEE80211_AGGR_MINRETRY
1068		 * defines the minimum interval we'll retry after
1069		 * IEEE80211_AGGR_MAXTRIES failed attempts to
1070		 * negotiate use.
1071		 */
1072		return 0;
1073	}
1074	dialogtoken = (tokens+1) % 63;		/* XXX */
1075
1076	tid = WME_AC_TO_TID(tap->txa_ac);
1077	args[0] = dialogtoken;
1078	args[1]	= IEEE80211_BAPS_POLICY_IMMEDIATE
1079		| SM(tid, IEEE80211_BAPS_TID)
1080		| SM(IEEE80211_AGGR_BAWMAX, IEEE80211_BAPS_BUFSIZ)
1081		;
1082	args[2] = 0;	/* batimeout */
1083	args[3] = SM(0, IEEE80211_BASEQ_START)
1084		| SM(0, IEEE80211_BASEQ_FRAG)
1085		;
1086	/* NB: do first so there's no race against reply */
1087	if (!ic->ic_addba_request(ni, tap, dialogtoken, args[1], args[2])) {
1088		/* unable to setup state, don't make request */
1089		return 0;
1090	}
1091	tokens = dialogtoken;			/* allocate token */
1092	return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA,
1093		IEEE80211_ACTION_BA_ADDBA_REQUEST, args);
1094}
1095
1096/*
1097 * Transmit a BAR frame to the specified node.  The
1098 * BAR contents are drawn from the supplied aggregation
1099 * state associated with the node.
1100 */
1101int
1102ieee80211_send_bar(struct ieee80211_node *ni,
1103	const struct ieee80211_tx_ampdu *tap)
1104{
1105#define	senderr(_x, _v)	do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
1106#define	ADDSHORT(frm, v) do {			\
1107	frm[0] = (v) & 0xff;			\
1108	frm[1] = (v) >> 8;			\
1109	frm += 2;				\
1110} while (0)
1111	struct ieee80211com *ic = ni->ni_ic;
1112	struct ifnet *ifp = ic->ic_ifp;
1113	struct ieee80211_frame_min *wh;
1114	struct mbuf *m;
1115	uint8_t *frm;
1116	uint16_t barctl, barseqctl;
1117	int tid, ret;
1118
1119	ieee80211_ref_node(ni);
1120
1121	m = ieee80211_getmgtframe(&frm,
1122		ic->ic_headroom + sizeof(struct ieee80211_frame_min),
1123		sizeof(struct ieee80211_ba_request)
1124	);
1125	if (m == NULL)
1126		senderr(ENOMEM, is_tx_nobuf);
1127
1128	wh = mtod(m, struct ieee80211_frame_min *);
1129	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 |
1130		IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
1131	wh->i_fc[1] = 0;
1132	IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1133	IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
1134
1135	tid = WME_AC_TO_TID(tap->txa_ac);
1136	barctl 	= (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ?
1137			IEEE80211_BAPS_POLICY_IMMEDIATE :
1138			IEEE80211_BAPS_POLICY_DELAYED)
1139		| SM(tid, IEEE80211_BAPS_TID)
1140		| SM(tap->txa_wnd, IEEE80211_BAPS_BUFSIZ)
1141		;
1142	barseqctl = SM(tap->txa_start, IEEE80211_BASEQ_START)
1143		| SM(0, IEEE80211_BASEQ_FRAG)
1144		;
1145	ADDSHORT(frm, barctl);
1146	ADDSHORT(frm, barseqctl);
1147	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1148
1149	IEEE80211_NODE_STAT(ni, tx_mgmt);	/* XXX tx_ctl? */
1150
1151	IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1152	    "[%s] send bar frame (tid %u start %u) on channel %u\n",
1153	    ether_sprintf(ni->ni_macaddr), tid, tap->txa_start,
1154	    ieee80211_chan2ieee(ic, ic->ic_curchan));
1155
1156	m->m_pkthdr.rcvif = (void *)ni;
1157	IF_ENQUEUE(&ic->ic_mgtq, m);		/* cheat */
1158	(*ifp->if_start)(ifp);
1159
1160	return 0;
1161bad:
1162	ieee80211_free_node(ni);
1163	return ret;
1164#undef ADDSHORT
1165#undef senderr
1166}
1167
1168/*
1169 * Send an action management frame.  The arguments are stuff
1170 * into a frame without inspection; the caller is assumed to
1171 * prepare them carefully (e.g. based on the aggregation state).
1172 */
1173int
1174ieee80211_send_action(struct ieee80211_node *ni,
1175	int category, int action, uint16_t args[4])
1176{
1177#define	senderr(_x, _v)	do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
1178#define	ADDSHORT(frm, v) do {			\
1179	frm[0] = (v) & 0xff;			\
1180	frm[1] = (v) >> 8;			\
1181	frm += 2;				\
1182} while (0)
1183	struct ieee80211com *ic = ni->ni_ic;
1184	struct mbuf *m;
1185	uint8_t *frm;
1186	uint16_t baparamset;
1187	int ret;
1188
1189	KASSERT(ni != NULL, ("null node"));
1190
1191	/*
1192	 * Hold a reference on the node so it doesn't go away until after
1193	 * the xmit is complete all the way in the driver.  On error we
1194	 * will remove our reference.
1195	 */
1196	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1197		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1198		__func__, __LINE__,
1199		ni, ether_sprintf(ni->ni_macaddr),
1200		ieee80211_node_refcnt(ni)+1);
1201	ieee80211_ref_node(ni);
1202
1203	m = ieee80211_getmgtframe(&frm,
1204		ic->ic_headroom + sizeof(struct ieee80211_frame),
1205		  sizeof(uint16_t)	/* action+category */
1206		/* XXX may action payload */
1207		+ sizeof(struct ieee80211_action_ba_addbaresponse)
1208	);
1209	if (m == NULL)
1210		senderr(ENOMEM, is_tx_nobuf);
1211
1212	*frm++ = category;
1213	*frm++ = action;
1214	switch (category) {
1215	case IEEE80211_ACTION_CAT_BA:
1216		switch (action) {
1217		case IEEE80211_ACTION_BA_ADDBA_REQUEST:
1218			IEEE80211_NOTE(ic,
1219			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1220			    "send ADDBA request: tid %d, baparamset 0x%x",
1221			    args[0], args[1]);
1222
1223			*frm++ = args[0];	/* dialog token */
1224			ADDSHORT(frm, args[1]);	/* baparamset */
1225			ADDSHORT(frm, args[2]);	/* batimeout */
1226			ADDSHORT(frm, args[3]);	/* baseqctl */
1227			break;
1228		case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
1229			IEEE80211_NOTE(ic,
1230			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1231			    "send ADDBA response: dialogtoken %d status %d "
1232			    "baparamset 0x%x (tid %d) batimeout %d",
1233			    args[0], args[1], args[2],
1234			    MS(args[2], IEEE80211_BAPS_TID), args[3]);
1235
1236			*frm++ = args[0];	/* dialog token */
1237			ADDSHORT(frm, args[1]);	/* statuscode */
1238			ADDSHORT(frm, args[2]);	/* baparamset */
1239			ADDSHORT(frm, args[3]);	/* batimeout */
1240			break;
1241		case IEEE80211_ACTION_BA_DELBA:
1242			/* XXX */
1243			baparamset = SM(args[0], IEEE80211_DELBAPS_TID)
1244				   | SM(args[1], IEEE80211_DELBAPS_INIT)
1245				   ;
1246			ADDSHORT(frm, baparamset);
1247			ADDSHORT(frm, args[2]);	/* reason code */
1248
1249			IEEE80211_NOTE(ic,
1250			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1251			    "send DELBA action: tid %d, initiator %d reason %d",
1252			    args[0], args[1], args[2]);
1253			break;
1254		default:
1255			goto badaction;
1256		}
1257		break;
1258	case IEEE80211_ACTION_CAT_HT:
1259		switch (action) {
1260		case IEEE80211_ACTION_HT_TXCHWIDTH:
1261			IEEE80211_NOTE(ic,
1262			    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N,
1263			    ni, "send HT txchwidth: width %d",
1264			   IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) ?  40 : 20
1265			);
1266			*frm++ = IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) ?
1267				IEEE80211_A_HT_TXCHWIDTH_2040 :
1268				IEEE80211_A_HT_TXCHWIDTH_20;
1269			break;
1270		default:
1271			goto badaction;
1272		}
1273		break;
1274	default:
1275	badaction:
1276		IEEE80211_NOTE(ic,
1277		    IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni,
1278		    "%s: unsupported category %d action %d", __func__,
1279		    category, action);
1280		senderr(EINVAL, is_tx_unknownmgt);
1281		/* NOTREACHED */
1282	}
1283	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1284
1285	ret = ieee80211_mgmt_output(ic, ni, m, IEEE80211_FC0_SUBTYPE_ACTION);
1286	if (ret != 0)
1287		goto bad;
1288	return 0;
1289bad:
1290	ieee80211_free_node(ni);
1291	return ret;
1292#undef ADDSHORT
1293#undef senderr
1294}
1295
1296/*
1297 * Construct the MCS bit mask for inclusion
1298 * in an HT information element.
1299 */
1300static void
1301ieee80211_set_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
1302{
1303	int i;
1304
1305	for (i = 0; i < rs->rs_nrates; i++) {
1306		int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
1307		if (r < IEEE80211_HTRATE_MAXSIZE) {	/* XXX? */
1308			/* NB: this assumes a particular implementation */
1309			setbit(frm, r);
1310		}
1311	}
1312}
1313
1314/*
1315 * Add body of an HTCAP information element.
1316 */
1317static uint8_t *
1318ieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni)
1319{
1320#define	ADDSHORT(frm, v) do {			\
1321	frm[0] = (v) & 0xff;			\
1322	frm[1] = (v) >> 8;			\
1323	frm += 2;				\
1324} while (0)
1325	struct ieee80211com *ic = ni->ni_ic;
1326	uint16_t caps;
1327
1328	/* HT capabilities */
1329	caps = ic->ic_htcaps & 0xffff;
1330	/* override 20/40 use based on channel and config */
1331	if (IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) &&
1332	    (ic->ic_flags_ext & IEEE80211_FEXT_USEHT40))
1333		caps |= IEEE80211_HTCAP_CHWIDTH40;
1334	else
1335		caps &= ~IEEE80211_HTCAP_CHWIDTH40;
1336	/* adjust short GI based on channel and config */
1337	if ((ic->ic_flags_ext & IEEE80211_FEXT_SHORTGI20) == 0)
1338		caps &= ~IEEE80211_HTCAP_SHORTGI20;
1339	if ((ic->ic_flags_ext & IEEE80211_FEXT_SHORTGI40) == 0 ||
1340	    (caps & IEEE80211_HTCAP_CHWIDTH40) == 0)
1341		caps &= ~IEEE80211_HTCAP_SHORTGI40;
1342	ADDSHORT(frm, caps);
1343
1344	/* HT parameters */
1345	switch (ic->ic_ampdu_rxmax / 1024) {
1346	case 8:	 *frm = IEEE80211_HTCAP_MAXRXAMPDU_8K; break;
1347	case 16: *frm = IEEE80211_HTCAP_MAXRXAMPDU_16K; break;
1348	case 32: *frm = IEEE80211_HTCAP_MAXRXAMPDU_32K; break;
1349	default: *frm = IEEE80211_HTCAP_MAXRXAMPDU_64K; break;
1350	}
1351	*frm |= SM(ic->ic_ampdu_density, IEEE80211_HTCAP_MPDUDENSITY);
1352	frm++;
1353
1354	/* pre-zero remainder of ie */
1355	memset(frm, 0, sizeof(struct ieee80211_ie_htcap) -
1356		__offsetof(struct ieee80211_ie_htcap, hc_mcsset));
1357
1358	/* supported MCS set */
1359	ieee80211_set_htrates(frm, &ni->ni_htrates);
1360
1361	frm += sizeof(struct ieee80211_ie_htcap) -
1362		__offsetof(struct ieee80211_ie_htcap, hc_mcsset);
1363	return frm;
1364#undef ADDSHORT
1365}
1366
1367/*
1368 * Add 802.11n HT capabilities information element
1369 */
1370uint8_t *
1371ieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni)
1372{
1373	frm[0] = IEEE80211_ELEMID_HTCAP;
1374	frm[1] = sizeof(struct ieee80211_ie_htcap) - 2;
1375	return ieee80211_add_htcap_body(frm + 2, ni);
1376}
1377
1378/*
1379 * Add Broadcom OUI wrapped standard HTCAP ie; this is
1380 * used for compatibility w/ pre-draft implementations.
1381 */
1382uint8_t *
1383ieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni)
1384{
1385	frm[0] = IEEE80211_ELEMID_VENDOR;
1386	frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2;
1387	frm[2] = (BCM_OUI >> 0) & 0xff;
1388	frm[3] = (BCM_OUI >> 8) & 0xff;
1389	frm[4] = (BCM_OUI >> 16) & 0xff;
1390	frm[5] = BCM_OUI_HTCAP;
1391	return ieee80211_add_htcap_body(frm + 6, ni);
1392}
1393
1394/*
1395 * Construct the MCS bit mask of basic rates
1396 * for inclusion in an HT information element.
1397 */
1398static void
1399ieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs)
1400{
1401	int i;
1402
1403	for (i = 0; i < rs->rs_nrates; i++) {
1404		int r = rs->rs_rates[i] & IEEE80211_RATE_VAL;
1405		if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
1406		    r < IEEE80211_HTRATE_MAXSIZE) {
1407			/* NB: this assumes a particular implementation */
1408			setbit(frm, r);
1409		}
1410	}
1411}
1412
1413/*
1414 * Update the HTINFO ie for a beacon frame.
1415 */
1416void
1417ieee80211_ht_update_beacon(struct ieee80211com *ic,
1418	struct ieee80211_beacon_offsets *bo)
1419{
1420#define	PROTMODE	(IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT)
1421	struct ieee80211_ie_htinfo *ht =
1422	   (struct ieee80211_ie_htinfo *) bo->bo_htinfo;
1423
1424	/* XXX only update on channel change */
1425	ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, ic->ic_bsschan);
1426	ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH;
1427	if (IEEE80211_IS_CHAN_HT40U(ic->ic_bsschan))
1428		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
1429	else if (IEEE80211_IS_CHAN_HT40D(ic->ic_bsschan))
1430		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW;
1431	else
1432		ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE;
1433	if (IEEE80211_IS_CHAN_HT40(ic->ic_bsschan))
1434		ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040;
1435
1436	/* protection mode */
1437	ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode;
1438
1439	/* XXX propagate to vendor ie's */
1440#undef PROTMODE
1441}
1442
1443/*
1444 * Add body of an HTINFO information element.
1445 */
1446static uint8_t *
1447ieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni)
1448{
1449	struct ieee80211com *ic = ni->ni_ic;
1450
1451	/* pre-zero remainder of ie */
1452	memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2);
1453
1454	/* primary/control channel center */
1455	*frm++ = ieee80211_chan2ieee(ic, ic->ic_bsschan);
1456
1457	frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH;
1458	if (IEEE80211_IS_CHAN_HT40U(ic->ic_bsschan))
1459		frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE;
1460	else if (IEEE80211_IS_CHAN_HT40D(ic->ic_bsschan))
1461		frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW;
1462	else
1463		frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE;
1464	if (IEEE80211_IS_CHAN_HT40(ic->ic_bsschan))
1465		frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040;
1466
1467	frm[1] = ic->ic_curhtprotmode;
1468
1469	frm += 5;
1470
1471	/* basic MCS set */
1472	ieee80211_set_basic_htrates(frm, &ni->ni_htrates);
1473	frm += sizeof(struct ieee80211_ie_htinfo) -
1474		__offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset);
1475	return frm;
1476}
1477
1478/*
1479 * Add 802.11n HT information information element.
1480 */
1481uint8_t *
1482ieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni)
1483{
1484	frm[0] = IEEE80211_ELEMID_HTINFO;
1485	frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2;
1486	return ieee80211_add_htinfo_body(frm + 2, ni);
1487}
1488
1489/*
1490 * Add Broadcom OUI wrapped standard HTINFO ie; this is
1491 * used for compatibility w/ pre-draft implementations.
1492 */
1493uint8_t *
1494ieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni)
1495{
1496	frm[0] = IEEE80211_ELEMID_VENDOR;
1497	frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2;
1498	frm[2] = (BCM_OUI >> 0) & 0xff;
1499	frm[3] = (BCM_OUI >> 8) & 0xff;
1500	frm[4] = (BCM_OUI >> 16) & 0xff;
1501	frm[5] = BCM_OUI_HTINFO;
1502	return ieee80211_add_htinfo_body(frm + 6, ni);
1503}
1504