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