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