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