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