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