1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 /* 29 * IEEE 802.11n protocol support. 30 */ 31 32 #include "opt_inet.h" 33 #include "opt_wlan.h" 34 35 #include <sys/param.h> 36 #include <sys/kernel.h> 37 #include <sys/malloc.h> 38 #include <sys/systm.h> 39 #include <sys/endian.h> 40 41 #include <sys/socket.h> 42 43 #include <net/if.h> 44 #include <net/if_var.h> 45 #include <net/if_media.h> 46 #include <net/ethernet.h> 47 48 #include <net80211/ieee80211_var.h> 49 #include <net80211/ieee80211_action.h> 50 #include <net80211/ieee80211_input.h> 51 52 const struct ieee80211_mcs_rates ieee80211_htrates[IEEE80211_HTRATE_MAXSIZE] = { 53 { 13, 14, 27, 30 }, /* MCS 0 */ 54 { 26, 29, 54, 60 }, /* MCS 1 */ 55 { 39, 43, 81, 90 }, /* MCS 2 */ 56 { 52, 58, 108, 120 }, /* MCS 3 */ 57 { 78, 87, 162, 180 }, /* MCS 4 */ 58 { 104, 116, 216, 240 }, /* MCS 5 */ 59 { 117, 130, 243, 270 }, /* MCS 6 */ 60 { 130, 144, 270, 300 }, /* MCS 7 */ 61 { 26, 29, 54, 60 }, /* MCS 8 */ 62 { 52, 58, 108, 120 }, /* MCS 9 */ 63 { 78, 87, 162, 180 }, /* MCS 10 */ 64 { 104, 116, 216, 240 }, /* MCS 11 */ 65 { 156, 173, 324, 360 }, /* MCS 12 */ 66 { 208, 231, 432, 480 }, /* MCS 13 */ 67 { 234, 260, 486, 540 }, /* MCS 14 */ 68 { 260, 289, 540, 600 }, /* MCS 15 */ 69 { 39, 43, 81, 90 }, /* MCS 16 */ 70 { 78, 87, 162, 180 }, /* MCS 17 */ 71 { 117, 130, 243, 270 }, /* MCS 18 */ 72 { 156, 173, 324, 360 }, /* MCS 19 */ 73 { 234, 260, 486, 540 }, /* MCS 20 */ 74 { 312, 347, 648, 720 }, /* MCS 21 */ 75 { 351, 390, 729, 810 }, /* MCS 22 */ 76 { 390, 433, 810, 900 }, /* MCS 23 */ 77 { 52, 58, 108, 120 }, /* MCS 24 */ 78 { 104, 116, 216, 240 }, /* MCS 25 */ 79 { 156, 173, 324, 360 }, /* MCS 26 */ 80 { 208, 231, 432, 480 }, /* MCS 27 */ 81 { 312, 347, 648, 720 }, /* MCS 28 */ 82 { 416, 462, 864, 960 }, /* MCS 29 */ 83 { 468, 520, 972, 1080 }, /* MCS 30 */ 84 { 520, 578, 1080, 1200 }, /* MCS 31 */ 85 { 0, 0, 12, 13 }, /* MCS 32 */ 86 { 78, 87, 162, 180 }, /* MCS 33 */ 87 { 104, 116, 216, 240 }, /* MCS 34 */ 88 { 130, 144, 270, 300 }, /* MCS 35 */ 89 { 117, 130, 243, 270 }, /* MCS 36 */ 90 { 156, 173, 324, 360 }, /* MCS 37 */ 91 { 195, 217, 405, 450 }, /* MCS 38 */ 92 { 104, 116, 216, 240 }, /* MCS 39 */ 93 { 130, 144, 270, 300 }, /* MCS 40 */ 94 { 130, 144, 270, 300 }, /* MCS 41 */ 95 { 156, 173, 324, 360 }, /* MCS 42 */ 96 { 182, 202, 378, 420 }, /* MCS 43 */ 97 { 182, 202, 378, 420 }, /* MCS 44 */ 98 { 208, 231, 432, 480 }, /* MCS 45 */ 99 { 156, 173, 324, 360 }, /* MCS 46 */ 100 { 195, 217, 405, 450 }, /* MCS 47 */ 101 { 195, 217, 405, 450 }, /* MCS 48 */ 102 { 234, 260, 486, 540 }, /* MCS 49 */ 103 { 273, 303, 567, 630 }, /* MCS 50 */ 104 { 273, 303, 567, 630 }, /* MCS 51 */ 105 { 312, 347, 648, 720 }, /* MCS 52 */ 106 { 130, 144, 270, 300 }, /* MCS 53 */ 107 { 156, 173, 324, 360 }, /* MCS 54 */ 108 { 182, 202, 378, 420 }, /* MCS 55 */ 109 { 156, 173, 324, 360 }, /* MCS 56 */ 110 { 182, 202, 378, 420 }, /* MCS 57 */ 111 { 208, 231, 432, 480 }, /* MCS 58 */ 112 { 234, 260, 486, 540 }, /* MCS 59 */ 113 { 208, 231, 432, 480 }, /* MCS 60 */ 114 { 234, 260, 486, 540 }, /* MCS 61 */ 115 { 260, 289, 540, 600 }, /* MCS 62 */ 116 { 260, 289, 540, 600 }, /* MCS 63 */ 117 { 286, 318, 594, 660 }, /* MCS 64 */ 118 { 195, 217, 405, 450 }, /* MCS 65 */ 119 { 234, 260, 486, 540 }, /* MCS 66 */ 120 { 273, 303, 567, 630 }, /* MCS 67 */ 121 { 234, 260, 486, 540 }, /* MCS 68 */ 122 { 273, 303, 567, 630 }, /* MCS 69 */ 123 { 312, 347, 648, 720 }, /* MCS 70 */ 124 { 351, 390, 729, 810 }, /* MCS 71 */ 125 { 312, 347, 648, 720 }, /* MCS 72 */ 126 { 351, 390, 729, 810 }, /* MCS 73 */ 127 { 390, 433, 810, 900 }, /* MCS 74 */ 128 { 390, 433, 810, 900 }, /* MCS 75 */ 129 { 429, 477, 891, 990 }, /* MCS 76 */ 130 }; 131 132 static int ieee80211_ampdu_age = -1; /* threshold for ampdu reorder q (ms) */ 133 SYSCTL_PROC(_net_wlan, OID_AUTO, ampdu_age, 134 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 135 &ieee80211_ampdu_age, 0, ieee80211_sysctl_msecs_ticks, "I", 136 "AMPDU max reorder age (ms)"); 137 138 static int ieee80211_recv_bar_ena = 1; 139 SYSCTL_INT(_net_wlan, OID_AUTO, recv_bar, CTLFLAG_RW, &ieee80211_recv_bar_ena, 140 0, "BAR frame processing (ena/dis)"); 141 142 static int ieee80211_addba_timeout = -1;/* timeout for ADDBA response */ 143 SYSCTL_PROC(_net_wlan, OID_AUTO, addba_timeout, 144 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 145 &ieee80211_addba_timeout, 0, ieee80211_sysctl_msecs_ticks, "I", 146 "ADDBA request timeout (ms)"); 147 static int ieee80211_addba_backoff = -1;/* backoff after max ADDBA requests */ 148 SYSCTL_PROC(_net_wlan, OID_AUTO, addba_backoff, 149 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 150 &ieee80211_addba_backoff, 0, ieee80211_sysctl_msecs_ticks, "I", 151 "ADDBA request backoff (ms)"); 152 static int ieee80211_addba_maxtries = 3;/* max ADDBA requests before backoff */ 153 SYSCTL_INT(_net_wlan, OID_AUTO, addba_maxtries, CTLFLAG_RW, 154 &ieee80211_addba_maxtries, 0, "max ADDBA requests sent before backoff"); 155 156 static int ieee80211_bar_timeout = -1; /* timeout waiting for BAR response */ 157 static int ieee80211_bar_maxtries = 50;/* max BAR requests before DELBA */ 158 159 static ieee80211_recv_action_func ht_recv_action_ba_addba_request; 160 static ieee80211_recv_action_func ht_recv_action_ba_addba_response; 161 static ieee80211_recv_action_func ht_recv_action_ba_delba; 162 static ieee80211_recv_action_func ht_recv_action_ht_mimopwrsave; 163 static ieee80211_recv_action_func ht_recv_action_ht_txchwidth; 164 165 static ieee80211_send_action_func ht_send_action_ba_addba; 166 static ieee80211_send_action_func ht_send_action_ba_delba; 167 static ieee80211_send_action_func ht_send_action_ht_txchwidth; 168 169 static void 170 ieee80211_ht_init(void) 171 { 172 /* 173 * Setup HT parameters that depends on the clock frequency. 174 */ 175 ieee80211_ampdu_age = msecs_to_ticks(500); 176 ieee80211_addba_timeout = msecs_to_ticks(250); 177 ieee80211_addba_backoff = msecs_to_ticks(10*1000); 178 ieee80211_bar_timeout = msecs_to_ticks(250); 179 /* 180 * Register action frame handlers. 181 */ 182 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA, 183 IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_recv_action_ba_addba_request); 184 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA, 185 IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_recv_action_ba_addba_response); 186 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_BA, 187 IEEE80211_ACTION_BA_DELBA, ht_recv_action_ba_delba); 188 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT, 189 IEEE80211_ACTION_HT_MIMOPWRSAVE, ht_recv_action_ht_mimopwrsave); 190 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_HT, 191 IEEE80211_ACTION_HT_TXCHWIDTH, ht_recv_action_ht_txchwidth); 192 193 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA, 194 IEEE80211_ACTION_BA_ADDBA_REQUEST, ht_send_action_ba_addba); 195 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA, 196 IEEE80211_ACTION_BA_ADDBA_RESPONSE, ht_send_action_ba_addba); 197 ieee80211_send_action_register(IEEE80211_ACTION_CAT_BA, 198 IEEE80211_ACTION_BA_DELBA, ht_send_action_ba_delba); 199 ieee80211_send_action_register(IEEE80211_ACTION_CAT_HT, 200 IEEE80211_ACTION_HT_TXCHWIDTH, ht_send_action_ht_txchwidth); 201 } 202 SYSINIT(wlan_ht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_ht_init, NULL); 203 204 static int ieee80211_ampdu_enable(struct ieee80211_node *ni, 205 struct ieee80211_tx_ampdu *tap); 206 static int ieee80211_addba_request(struct ieee80211_node *ni, 207 struct ieee80211_tx_ampdu *tap, 208 int dialogtoken, int baparamset, int batimeout); 209 static int ieee80211_addba_response(struct ieee80211_node *ni, 210 struct ieee80211_tx_ampdu *tap, 211 int code, int baparamset, int batimeout); 212 static void ieee80211_addba_stop(struct ieee80211_node *ni, 213 struct ieee80211_tx_ampdu *tap); 214 static void null_addba_response_timeout(struct ieee80211_node *ni, 215 struct ieee80211_tx_ampdu *tap); 216 217 static void ieee80211_bar_response(struct ieee80211_node *ni, 218 struct ieee80211_tx_ampdu *tap, int status); 219 static void ampdu_tx_stop(struct ieee80211_tx_ampdu *tap); 220 static void bar_stop_timer(struct ieee80211_tx_ampdu *tap); 221 static int ampdu_rx_start(struct ieee80211_node *, struct ieee80211_rx_ampdu *, 222 int baparamset, int batimeout, int baseqctl); 223 static void ampdu_rx_stop(struct ieee80211_node *, struct ieee80211_rx_ampdu *); 224 225 void 226 ieee80211_ht_attach(struct ieee80211com *ic) 227 { 228 /* setup default aggregation policy */ 229 ic->ic_recv_action = ieee80211_recv_action; 230 ic->ic_send_action = ieee80211_send_action; 231 ic->ic_ampdu_enable = ieee80211_ampdu_enable; 232 ic->ic_addba_request = ieee80211_addba_request; 233 ic->ic_addba_response = ieee80211_addba_response; 234 ic->ic_addba_response_timeout = null_addba_response_timeout; 235 ic->ic_addba_stop = ieee80211_addba_stop; 236 ic->ic_bar_response = ieee80211_bar_response; 237 ic->ic_ampdu_rx_start = ampdu_rx_start; 238 ic->ic_ampdu_rx_stop = ampdu_rx_stop; 239 240 ic->ic_htprotmode = IEEE80211_PROT_RTSCTS; 241 ic->ic_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE; 242 } 243 244 void 245 ieee80211_ht_detach(struct ieee80211com *ic) 246 { 247 } 248 249 void 250 ieee80211_ht_vattach(struct ieee80211vap *vap) 251 { 252 253 /* driver can override defaults */ 254 vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_8K; 255 vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA; 256 vap->iv_ampdu_limit = vap->iv_ampdu_rxmax; 257 vap->iv_amsdu_limit = vap->iv_htcaps & IEEE80211_HTCAP_MAXAMSDU; 258 /* tx aggregation traffic thresholds */ 259 vap->iv_ampdu_mintraffic[WME_AC_BK] = 128; 260 vap->iv_ampdu_mintraffic[WME_AC_BE] = 64; 261 vap->iv_ampdu_mintraffic[WME_AC_VO] = 32; 262 vap->iv_ampdu_mintraffic[WME_AC_VI] = 32; 263 264 vap->iv_htprotmode = IEEE80211_PROT_RTSCTS; 265 vap->iv_curhtprotmode = IEEE80211_HTINFO_OPMODE_PURE; 266 267 if (vap->iv_htcaps & IEEE80211_HTC_HT) { 268 /* 269 * Device is HT capable; enable all HT-related 270 * facilities by default. 271 * XXX these choices may be too aggressive. 272 */ 273 vap->iv_flags_ht |= IEEE80211_FHT_HT 274 | IEEE80211_FHT_HTCOMPAT 275 ; 276 if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI20) 277 vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20; 278 /* XXX infer from channel list? */ 279 if (vap->iv_htcaps & IEEE80211_HTCAP_CHWIDTH40) { 280 vap->iv_flags_ht |= IEEE80211_FHT_USEHT40; 281 if (vap->iv_htcaps & IEEE80211_HTCAP_SHORTGI40) 282 vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40; 283 } 284 /* enable RIFS if capable */ 285 if (vap->iv_htcaps & IEEE80211_HTC_RIFS) 286 vap->iv_flags_ht |= IEEE80211_FHT_RIFS; 287 288 /* NB: A-MPDU and A-MSDU rx are mandated, these are tx only */ 289 vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX; 290 if (vap->iv_htcaps & IEEE80211_HTC_AMPDU) 291 vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX; 292 vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX; 293 if (vap->iv_htcaps & IEEE80211_HTC_AMSDU) 294 vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX; 295 296 if (vap->iv_htcaps & IEEE80211_HTCAP_TXSTBC) 297 vap->iv_flags_ht |= IEEE80211_FHT_STBC_TX; 298 if (vap->iv_htcaps & IEEE80211_HTCAP_RXSTBC) 299 vap->iv_flags_ht |= IEEE80211_FHT_STBC_RX; 300 301 if (vap->iv_htcaps & IEEE80211_HTCAP_LDPC) 302 vap->iv_flags_ht |= IEEE80211_FHT_LDPC_RX; 303 if (vap->iv_htcaps & IEEE80211_HTC_TXLDPC) 304 vap->iv_flags_ht |= IEEE80211_FHT_LDPC_TX; 305 } 306 /* NB: disable default legacy WDS, too many issues right now */ 307 if (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) 308 vap->iv_flags_ht &= ~IEEE80211_FHT_HT; 309 } 310 311 void 312 ieee80211_ht_vdetach(struct ieee80211vap *vap) 313 { 314 } 315 316 static int 317 ht_getrate(struct ieee80211com *ic, int index, enum ieee80211_phymode mode, 318 int ratetype) 319 { 320 struct ieee80211_node_txrate tr; 321 int mword, rate; 322 323 tr = IEEE80211_NODE_TXRATE_INIT_HT(index); 324 325 mword = ieee80211_rate2media(ic, &tr, mode); 326 if (IFM_SUBTYPE(mword) != IFM_IEEE80211_MCS) 327 return (0); 328 switch (ratetype) { 329 case 0: 330 rate = ieee80211_htrates[index].ht20_rate_800ns; 331 break; 332 case 1: 333 rate = ieee80211_htrates[index].ht20_rate_400ns; 334 break; 335 case 2: 336 rate = ieee80211_htrates[index].ht40_rate_800ns; 337 break; 338 default: 339 rate = ieee80211_htrates[index].ht40_rate_400ns; 340 break; 341 } 342 return (rate); 343 } 344 345 static struct printranges { 346 int minmcs; 347 int maxmcs; 348 int txstream; 349 int ratetype; 350 int htcapflags; 351 } ranges[] = { 352 { 0, 7, 1, 0, 0 }, 353 { 8, 15, 2, 0, 0 }, 354 { 16, 23, 3, 0, 0 }, 355 { 24, 31, 4, 0, 0 }, 356 { 32, 0, 1, 2, IEEE80211_HTC_TXMCS32 }, 357 { 33, 38, 2, 0, IEEE80211_HTC_TXUNEQUAL }, 358 { 39, 52, 3, 0, IEEE80211_HTC_TXUNEQUAL }, 359 { 53, 76, 4, 0, IEEE80211_HTC_TXUNEQUAL }, 360 { 0, 0, 0, 0, 0 }, 361 }; 362 363 static void 364 ht_rateprint(struct ieee80211com *ic, enum ieee80211_phymode mode, int ratetype) 365 { 366 int minrate, maxrate; 367 struct printranges *range; 368 369 for (range = ranges; range->txstream != 0; range++) { 370 if (ic->ic_txstream < range->txstream) 371 continue; 372 if (range->htcapflags && 373 (ic->ic_htcaps & range->htcapflags) == 0) 374 continue; 375 if (ratetype < range->ratetype) 376 continue; 377 minrate = ht_getrate(ic, range->minmcs, mode, ratetype); 378 maxrate = ht_getrate(ic, range->maxmcs, mode, ratetype); 379 if (range->maxmcs) { 380 ic_printf(ic, "MCS %d-%d: %d%sMbps - %d%sMbps\n", 381 range->minmcs, range->maxmcs, 382 minrate/2, ((minrate & 0x1) != 0 ? ".5" : ""), 383 maxrate/2, ((maxrate & 0x1) != 0 ? ".5" : "")); 384 } else { 385 ic_printf(ic, "MCS %d: %d%sMbps\n", range->minmcs, 386 minrate/2, ((minrate & 0x1) != 0 ? ".5" : "")); 387 } 388 } 389 } 390 391 static void 392 ht_announce(struct ieee80211com *ic, enum ieee80211_phymode mode) 393 { 394 const char *modestr = ieee80211_phymode_name[mode]; 395 396 ic_printf(ic, "%s MCS 20MHz\n", modestr); 397 ht_rateprint(ic, mode, 0); 398 if (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20) { 399 ic_printf(ic, "%s MCS 20MHz SGI\n", modestr); 400 ht_rateprint(ic, mode, 1); 401 } 402 if (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) { 403 ic_printf(ic, "%s MCS 40MHz:\n", modestr); 404 ht_rateprint(ic, mode, 2); 405 } 406 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) && 407 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)) { 408 ic_printf(ic, "%s MCS 40MHz SGI:\n", modestr); 409 ht_rateprint(ic, mode, 3); 410 } 411 } 412 413 void 414 ieee80211_ht_announce(struct ieee80211com *ic) 415 { 416 417 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) || 418 isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) 419 ic_printf(ic, "%dT%dR\n", ic->ic_txstream, ic->ic_rxstream); 420 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA)) 421 ht_announce(ic, IEEE80211_MODE_11NA); 422 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) 423 ht_announce(ic, IEEE80211_MODE_11NG); 424 } 425 426 void 427 ieee80211_init_suphtrates(struct ieee80211com *ic) 428 { 429 #define ADDRATE(x) do { \ 430 htrateset->rs_rates[htrateset->rs_nrates] = x; \ 431 htrateset->rs_nrates++; \ 432 } while (0) 433 struct ieee80211_htrateset *htrateset = &ic->ic_sup_htrates; 434 int i; 435 436 memset(htrateset, 0, sizeof(struct ieee80211_htrateset)); 437 for (i = 0; i < ic->ic_txstream * 8; i++) 438 ADDRATE(i); 439 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) && 440 (ic->ic_htcaps & IEEE80211_HTC_TXMCS32)) 441 ADDRATE(32); 442 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) { 443 if (ic->ic_txstream >= 2) { 444 for (i = 33; i <= 38; i++) 445 ADDRATE(i); 446 } 447 if (ic->ic_txstream >= 3) { 448 for (i = 39; i <= 52; i++) 449 ADDRATE(i); 450 } 451 if (ic->ic_txstream == 4) { 452 for (i = 53; i <= 76; i++) 453 ADDRATE(i); 454 } 455 } 456 #undef ADDRATE 457 } 458 459 /* 460 * Receive processing. 461 */ 462 463 /* 464 * Decap the encapsulated A-MSDU frames and dispatch all but 465 * the last for delivery. The last frame is returned for 466 * delivery via the normal path. 467 */ 468 struct mbuf * 469 ieee80211_decap_amsdu(struct ieee80211_node *ni, struct mbuf *m) 470 { 471 struct ieee80211vap *vap = ni->ni_vap; 472 int framelen; 473 struct mbuf *n; 474 475 /* discard 802.3 header inserted by ieee80211_decap */ 476 m_adj(m, sizeof(struct ether_header)); 477 478 vap->iv_stats.is_amsdu_decap++; 479 480 for (;;) { 481 /* 482 * Decap the first frame, bust it apart from the 483 * remainder and deliver. We leave the last frame 484 * delivery to the caller (for consistency with other 485 * code paths, could also do it here). 486 */ 487 m = ieee80211_decap1(m, &framelen); 488 if (m == NULL) { 489 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 490 ni->ni_macaddr, "a-msdu", "%s", "decap failed"); 491 vap->iv_stats.is_amsdu_tooshort++; 492 return NULL; 493 } 494 if (m->m_pkthdr.len == framelen) 495 break; 496 n = m_split(m, framelen, IEEE80211_M_NOWAIT); 497 if (n == NULL) { 498 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 499 ni->ni_macaddr, "a-msdu", 500 "%s", "unable to split encapsulated frames"); 501 vap->iv_stats.is_amsdu_split++; 502 m_freem(m); /* NB: must reclaim */ 503 return NULL; 504 } 505 vap->iv_deliver_data(vap, ni, m); 506 507 /* 508 * Remove frame contents; each intermediate frame 509 * is required to be aligned to a 4-byte boundary. 510 */ 511 m = n; 512 m_adj(m, roundup2(framelen, 4) - framelen); /* padding */ 513 } 514 return m; /* last delivered by caller */ 515 } 516 517 static void 518 ampdu_rx_purge_slot(struct ieee80211_rx_ampdu *rap, int i) 519 { 520 struct mbuf *m; 521 522 /* Walk the queue, removing frames as appropriate */ 523 for (;;) { 524 m = mbufq_dequeue(&rap->rxa_mq[i]); 525 if (m == NULL) 526 break; 527 rap->rxa_qbytes -= m->m_pkthdr.len; 528 rap->rxa_qframes--; 529 m_freem(m); 530 } 531 } 532 533 /* 534 * Add the given frame to the current RX reorder slot. 535 * 536 * For future offloaded A-MSDU handling where multiple frames with 537 * the same sequence number show up here, this routine will append 538 * those frames as long as they're appropriately tagged. 539 */ 540 static int 541 ampdu_rx_add_slot(struct ieee80211_rx_ampdu *rap, int off, int tid, 542 ieee80211_seq rxseq, 543 struct ieee80211_node *ni, 544 struct mbuf *m, 545 const struct ieee80211_rx_stats *rxs) 546 { 547 const struct ieee80211_rx_stats *rxs_final = NULL; 548 struct ieee80211vap *vap = ni->ni_vap; 549 int toss_dup; 550 #define PROCESS 0 /* caller should process frame */ 551 #define CONSUMED 1 /* frame consumed, caller does nothing */ 552 553 /* 554 * Figure out if this is a duplicate frame for the given slot. 555 * 556 * We're assuming that the driver will hand us all the frames 557 * for a given AMSDU decap pass and if we get /a/ frame 558 * for an AMSDU decap then we'll get all of them. 559 * 560 * The tricksy bit is that we don't know when the /end/ of 561 * the decap pass is, because we aren't tracking state here 562 * per-slot to know that we've finished receiving the frame list. 563 * 564 * The driver sets RX_F_AMSDU and RX_F_AMSDU_MORE to tell us 565 * what's going on; so ideally we'd just check the frame at the 566 * end of the reassembly slot to see if its F_AMSDU w/ no F_AMSDU_MORE - 567 * that means we've received the whole AMSDU decap pass. 568 */ 569 570 /* 571 * Get the rxs of the final mbuf in the slot, if one exists. 572 */ 573 if (!mbufq_empty(&rap->rxa_mq[off])) { 574 rxs_final = ieee80211_get_rx_params_ptr(mbufq_last(&rap->rxa_mq[off])); 575 } 576 577 /* Default to tossing the duplicate frame */ 578 toss_dup = 1; 579 580 /* 581 * Check to see if the final frame has F_AMSDU and F_AMSDU set, AND 582 * this frame has F_AMSDU set (MORE or otherwise.) That's a sign 583 * that more can come. 584 */ 585 586 if ((rxs != NULL) && (rxs_final != NULL) && 587 ieee80211_check_rxseq_amsdu(rxs) && 588 ieee80211_check_rxseq_amsdu(rxs_final)) { 589 if (! ieee80211_check_rxseq_amsdu_more(rxs_final)) { 590 /* 591 * amsdu_more() returning 0 means "it's not the 592 * final frame" so we can append more 593 * frames here. 594 */ 595 toss_dup = 0; 596 } 597 } 598 599 /* 600 * If the list is empty OR we have determined we can put more 601 * driver decap'ed AMSDU frames in here, then insert. 602 */ 603 if (mbufq_empty(&rap->rxa_mq[off]) || (toss_dup == 0)) { 604 if (mbufq_enqueue(&rap->rxa_mq[off], m) != 0) { 605 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, 606 ni->ni_macaddr, 607 "a-mpdu queue fail", 608 "seqno %u tid %u BA win <%u:%u> off=%d, qlen=%d, maxqlen=%d", 609 rxseq, tid, rap->rxa_start, 610 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1), 611 off, 612 mbufq_len(&rap->rxa_mq[off]), 613 rap->rxa_mq[off].mq_maxlen); 614 /* XXX error count */ 615 m_freem(m); 616 return CONSUMED; 617 } 618 rap->rxa_qframes++; 619 rap->rxa_qbytes += m->m_pkthdr.len; 620 vap->iv_stats.is_ampdu_rx_reorder++; 621 /* 622 * Statistics for AMSDU decap. 623 */ 624 if (rxs != NULL && ieee80211_check_rxseq_amsdu(rxs)) { 625 if (ieee80211_check_rxseq_amsdu_more(rxs)) { 626 /* more=1, AMSDU, end of batch */ 627 IEEE80211_NODE_STAT(ni, rx_amsdu_more_end); 628 } else { 629 IEEE80211_NODE_STAT(ni, rx_amsdu_more); 630 } 631 } 632 } else { 633 IEEE80211_DISCARD_MAC(vap, 634 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, 635 ni->ni_macaddr, "a-mpdu duplicate", 636 "seqno %u tid %u BA win <%u:%u>", 637 rxseq, tid, rap->rxa_start, 638 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1)); 639 if (rxs != NULL) { 640 IEEE80211_DISCARD_MAC(vap, 641 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, 642 ni->ni_macaddr, "a-mpdu duplicate", 643 "seqno %d tid %u pktflags 0x%08x\n", 644 rxseq, tid, rxs->c_pktflags); 645 } 646 if (rxs_final != NULL) { 647 IEEE80211_DISCARD_MAC(vap, 648 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, 649 ni->ni_macaddr, "a-mpdu duplicate", 650 "final: pktflags 0x%08x\n", 651 rxs_final->c_pktflags); 652 } 653 vap->iv_stats.is_rx_dup++; 654 IEEE80211_NODE_STAT(ni, rx_dup); 655 m_freem(m); 656 } 657 return CONSUMED; 658 #undef CONSUMED 659 #undef PROCESS 660 } 661 662 /* 663 * Purge all frames in the A-MPDU re-order queue. 664 */ 665 static void 666 ampdu_rx_purge(struct ieee80211_rx_ampdu *rap) 667 { 668 int i; 669 670 for (i = 0; i < rap->rxa_wnd; i++) { 671 ampdu_rx_purge_slot(rap, i); 672 if (rap->rxa_qframes == 0) 673 break; 674 } 675 KASSERT(rap->rxa_qbytes == 0 && rap->rxa_qframes == 0, 676 ("lost %u data, %u frames on ampdu rx q", 677 rap->rxa_qbytes, rap->rxa_qframes)); 678 } 679 680 static void 681 ieee80211_ampdu_rx_init_rap(struct ieee80211_node *ni, 682 struct ieee80211_rx_ampdu *rap) 683 { 684 int i; 685 686 /* XXX TODO: ensure the queues are empty */ 687 memset(rap, 0, sizeof(*rap)); 688 for (i = 0; i < IEEE80211_AGGR_BAWMAX; i++) 689 mbufq_init(&rap->rxa_mq[i], 256); 690 } 691 692 /* 693 * Start A-MPDU rx/re-order processing for the specified TID. 694 */ 695 static int 696 ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap, 697 int baparamset, int batimeout, int baseqctl) 698 { 699 struct ieee80211vap *vap = ni->ni_vap; 700 int bufsiz = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_BUFSIZ); 701 702 if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) { 703 /* 704 * AMPDU previously setup and not terminated with a DELBA, 705 * flush the reorder q's in case anything remains. 706 */ 707 ampdu_rx_purge(rap); 708 } 709 ieee80211_ampdu_rx_init_rap(ni, rap); 710 rap->rxa_wnd = (bufsiz == 0) ? 711 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX); 712 rap->rxa_start = _IEEE80211_MASKSHIFT(baseqctl, IEEE80211_BASEQ_START); 713 rap->rxa_flags |= IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND; 714 715 /* XXX this should be a configuration flag */ 716 if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) && 717 (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU))) 718 rap->rxa_flags |= IEEE80211_AGGR_AMSDU; 719 else 720 rap->rxa_flags &= ~IEEE80211_AGGR_AMSDU; 721 722 return 0; 723 } 724 725 /* 726 * Public function; manually setup the RX ampdu state. 727 */ 728 int 729 ieee80211_ampdu_rx_start_ext(struct ieee80211_node *ni, int tid, int seq, int baw) 730 { 731 struct ieee80211_rx_ampdu *rap; 732 733 /* XXX TODO: sanity check tid, seq, baw */ 734 735 rap = &ni->ni_rx_ampdu[tid]; 736 737 if (rap->rxa_flags & IEEE80211_AGGR_RUNNING) { 738 /* 739 * AMPDU previously setup and not terminated with a DELBA, 740 * flush the reorder q's in case anything remains. 741 */ 742 ampdu_rx_purge(rap); 743 } 744 745 ieee80211_ampdu_rx_init_rap(ni, rap); 746 747 rap->rxa_wnd = (baw== 0) ? 748 IEEE80211_AGGR_BAWMAX : min(baw, IEEE80211_AGGR_BAWMAX); 749 if (seq == -1) { 750 /* Wait for the first RX frame, use that as BAW */ 751 rap->rxa_start = 0; 752 rap->rxa_flags |= IEEE80211_AGGR_WAITRX; 753 } else { 754 rap->rxa_start = seq; 755 } 756 rap->rxa_flags |= IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_XCHGPEND; 757 758 /* XXX TODO: no amsdu flag */ 759 760 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni, 761 "%s: tid=%d, start=%d, wnd=%d, flags=0x%08x", 762 __func__, 763 tid, 764 seq, 765 rap->rxa_wnd, 766 rap->rxa_flags); 767 768 return 0; 769 } 770 771 /* 772 * Public function; manually stop the RX AMPDU state. 773 */ 774 void 775 ieee80211_ampdu_rx_stop_ext(struct ieee80211_node *ni, int tid) 776 { 777 struct ieee80211_rx_ampdu *rap; 778 779 /* XXX TODO: sanity check tid, seq, baw */ 780 rap = &ni->ni_rx_ampdu[tid]; 781 ampdu_rx_stop(ni, rap); 782 } 783 784 /* 785 * Stop A-MPDU rx processing for the specified TID. 786 */ 787 static void 788 ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap) 789 { 790 791 ampdu_rx_purge(rap); 792 rap->rxa_flags &= ~(IEEE80211_AGGR_RUNNING 793 | IEEE80211_AGGR_XCHGPEND 794 | IEEE80211_AGGR_WAITRX); 795 } 796 797 /* 798 * Dispatch a frame from the A-MPDU reorder queue. The 799 * frame is fed back into ieee80211_input marked with an 800 * M_AMPDU_MPDU flag so it doesn't come back to us (it also 801 * permits ieee80211_input to optimize re-processing). 802 */ 803 static __inline void 804 ampdu_dispatch(struct ieee80211_node *ni, struct mbuf *m) 805 { 806 m->m_flags |= M_AMPDU_MPDU; /* bypass normal processing */ 807 /* NB: rssi and noise are ignored w/ M_AMPDU_MPDU set */ 808 (void) ieee80211_input(ni, m, 0, 0); 809 } 810 811 static int 812 ampdu_dispatch_slot(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni, 813 int i) 814 { 815 struct mbuf *m; 816 int n = 0; 817 818 for (;;) { 819 m = mbufq_dequeue(&rap->rxa_mq[i]); 820 if (m == NULL) 821 break; 822 n++; 823 824 rap->rxa_qbytes -= m->m_pkthdr.len; 825 rap->rxa_qframes--; 826 827 ampdu_dispatch(ni, m); 828 } 829 return (n); 830 } 831 832 static void 833 ampdu_rx_moveup(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni, 834 int i, int winstart) 835 { 836 struct ieee80211vap *vap = ni->ni_vap; 837 838 /* 839 * If frames remain, copy the mbuf pointers down so 840 * they correspond to the offsets in the new window. 841 */ 842 if (rap->rxa_qframes != 0) { 843 int n = rap->rxa_qframes, j; 844 for (j = i+1; j < rap->rxa_wnd; j++) { 845 /* 846 * Concat the list contents over, which will 847 * blank the source list for us. 848 */ 849 if (mbufq_len(&rap->rxa_mq[j]) != 0) { 850 n = n - mbufq_len(&rap->rxa_mq[j]); 851 mbufq_concat(&rap->rxa_mq[j-i], &rap->rxa_mq[j]); 852 KASSERT(n >= 0, ("%s: n < 0 (%d)", __func__, n)); 853 if (n == 0) 854 break; 855 } 856 } 857 KASSERT(n == 0, ("%s: lost %d frames, qframes %d off %d " 858 "BA win <%d:%d> winstart %d", 859 __func__, n, rap->rxa_qframes, i, rap->rxa_start, 860 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1), 861 winstart)); 862 vap->iv_stats.is_ampdu_rx_copy += rap->rxa_qframes; 863 } 864 } 865 866 /* 867 * Dispatch as many frames as possible from the re-order queue. 868 * Frames will always be "at the front"; we process all frames 869 * up to the first empty slot in the window. On completion we 870 * cleanup state if there are still pending frames in the current 871 * BA window. We assume the frame at slot 0 is already handled 872 * by the caller; we always start at slot 1. 873 */ 874 static void 875 ampdu_rx_dispatch(struct ieee80211_rx_ampdu *rap, struct ieee80211_node *ni) 876 { 877 struct ieee80211vap *vap = ni->ni_vap; 878 int i, r, r2; 879 880 /* flush run of frames */ 881 r2 = 0; 882 for (i = 1; i < rap->rxa_wnd; i++) { 883 r = ampdu_dispatch_slot(rap, ni, i); 884 if (r == 0) 885 break; 886 r2 += r; 887 } 888 889 /* move up frames */ 890 ampdu_rx_moveup(rap, ni, i, -1); 891 892 /* 893 * Adjust the start of the BA window to 894 * reflect the frames just dispatched. 895 */ 896 rap->rxa_start = IEEE80211_SEQ_ADD(rap->rxa_start, i); 897 vap->iv_stats.is_ampdu_rx_oor += r2; 898 899 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni, 900 "%s: moved slot up %d slots to start at %d (%d frames)", 901 __func__, 902 i, 903 rap->rxa_start, 904 r2); 905 } 906 907 /* 908 * Dispatch all frames in the A-MPDU re-order queue. 909 */ 910 static void 911 ampdu_rx_flush(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap) 912 { 913 int i, r; 914 915 for (i = 0; i < rap->rxa_wnd; i++) { 916 r = ampdu_dispatch_slot(rap, ni, i); 917 if (r == 0) 918 continue; 919 ni->ni_vap->iv_stats.is_ampdu_rx_oor += r; 920 921 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni, 922 "%s: moved slot up %d slots to start at %d (%d frames)", 923 __func__, 924 1, 925 rap->rxa_start, 926 r); 927 928 if (rap->rxa_qframes == 0) 929 break; 930 } 931 } 932 933 /* 934 * Dispatch all frames in the A-MPDU re-order queue 935 * preceding the specified sequence number. This logic 936 * handles window moves due to a received MSDU or BAR. 937 */ 938 static void 939 ampdu_rx_flush_upto(struct ieee80211_node *ni, 940 struct ieee80211_rx_ampdu *rap, ieee80211_seq winstart) 941 { 942 struct ieee80211vap *vap = ni->ni_vap; 943 ieee80211_seq seqno; 944 int i, r; 945 946 /* 947 * Flush any complete MSDU's with a sequence number lower 948 * than winstart. Gaps may exist. Note that we may actually 949 * dispatch frames past winstart if a run continues; this is 950 * an optimization that avoids having to do a separate pass 951 * to dispatch frames after moving the BA window start. 952 */ 953 seqno = rap->rxa_start; 954 for (i = 0; i < rap->rxa_wnd; i++) { 955 if ((r = mbufq_len(&rap->rxa_mq[i])) != 0) { 956 (void) ampdu_dispatch_slot(rap, ni, i); 957 } else { 958 if (!IEEE80211_SEQ_BA_BEFORE(seqno, winstart)) 959 break; 960 } 961 vap->iv_stats.is_ampdu_rx_oor += r; 962 seqno = IEEE80211_SEQ_INC(seqno); 963 964 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni, 965 "%s: moved slot up %d slots to start at %d (%d frames)", 966 __func__, 967 1, 968 seqno, 969 r); 970 } 971 972 /* 973 * If frames remain, copy the mbuf pointers down so 974 * they correspond to the offsets in the new window. 975 */ 976 ampdu_rx_moveup(rap, ni, i, winstart); 977 978 /* 979 * Move the start of the BA window; we use the 980 * sequence number of the last MSDU that was 981 * passed up the stack+1 or winstart if stopped on 982 * a gap in the reorder buffer. 983 */ 984 rap->rxa_start = seqno; 985 } 986 987 /* 988 * Process a received QoS data frame for an HT station. Handle 989 * A-MPDU reordering: if this frame is received out of order 990 * and falls within the BA window hold onto it. Otherwise if 991 * this frame completes a run, flush any pending frames. We 992 * return 1 if the frame is consumed. A 0 is returned if 993 * the frame should be processed normally by the caller. 994 * 995 * A-MSDU: handle hardware decap'ed A-MSDU frames that are 996 * pretending to be MPDU's. They're dispatched directly if 997 * able; or attempted to put into the receive reordering slot. 998 */ 999 int 1000 ieee80211_ampdu_reorder(struct ieee80211_node *ni, struct mbuf *m, 1001 const struct ieee80211_rx_stats *rxs) 1002 { 1003 #define PROCESS 0 /* caller should process frame */ 1004 #define CONSUMED 1 /* frame consumed, caller does nothing */ 1005 struct ieee80211vap *vap = ni->ni_vap; 1006 struct ieee80211_qosframe *wh; 1007 struct ieee80211_rx_ampdu *rap; 1008 ieee80211_seq rxseq; 1009 uint8_t tid; 1010 int off; 1011 int amsdu = ieee80211_check_rxseq_amsdu(rxs); 1012 int amsdu_end = ieee80211_check_rxseq_amsdu_more(rxs); 1013 1014 KASSERT((m->m_flags & (M_AMPDU | M_AMPDU_MPDU)) == M_AMPDU, 1015 ("!a-mpdu or already re-ordered, flags 0x%x", m->m_flags)); 1016 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta")); 1017 1018 /* NB: m_len known to be sufficient */ 1019 wh = mtod(m, struct ieee80211_qosframe *); 1020 if (!IEEE80211_IS_QOSDATA(wh)) { 1021 /* 1022 * Not QoS data, shouldn't get here but just 1023 * return it to the caller for processing. 1024 */ 1025 return PROCESS; 1026 } 1027 1028 /* 1029 * 802.11-2012 9.3.2.10 - Duplicate detection and recovery. 1030 * 1031 * Multicast QoS data frames are checked against a different 1032 * counter, not the per-TID counter. 1033 */ 1034 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 1035 return PROCESS; 1036 1037 tid = ieee80211_getqos(wh)[0]; 1038 tid &= IEEE80211_QOS_TID; 1039 rap = &ni->ni_rx_ampdu[tid]; 1040 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) { 1041 /* 1042 * No ADDBA request yet, don't touch. 1043 */ 1044 return PROCESS; 1045 } 1046 rxseq = le16toh(*(uint16_t *)wh->i_seq); 1047 if ((rxseq & IEEE80211_SEQ_FRAG_MASK) != 0) { 1048 /* 1049 * Fragments are not allowed; toss. 1050 */ 1051 IEEE80211_DISCARD_MAC(vap, 1052 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr, 1053 "A-MPDU", "fragment, rxseq 0x%x tid %u%s", rxseq, tid, 1054 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : ""); 1055 vap->iv_stats.is_ampdu_rx_drop++; 1056 IEEE80211_NODE_STAT(ni, rx_drop); 1057 m_freem(m); 1058 return CONSUMED; 1059 } 1060 rxseq >>= IEEE80211_SEQ_SEQ_SHIFT; 1061 rap->rxa_nframes++; 1062 1063 /* 1064 * Handle waiting for the first frame to define the BAW. 1065 * Some firmware doesn't provide the RX of the starting point 1066 * of the BAW and we have to cope. 1067 */ 1068 if (rap->rxa_flags & IEEE80211_AGGR_WAITRX) { 1069 rap->rxa_flags &= ~IEEE80211_AGGR_WAITRX; 1070 rap->rxa_start = rxseq; 1071 } 1072 again: 1073 if (rxseq == rap->rxa_start) { 1074 /* 1075 * First frame in window. 1076 */ 1077 if (rap->rxa_qframes != 0) { 1078 /* 1079 * Dispatch as many packets as we can. 1080 */ 1081 KASSERT(mbufq_empty(&rap->rxa_mq[0]), ("unexpected dup")); 1082 ampdu_dispatch(ni, m); 1083 ampdu_rx_dispatch(rap, ni); 1084 return CONSUMED; 1085 } else { 1086 /* 1087 * In order; advance window if needed and notify 1088 * caller to dispatch directly. 1089 */ 1090 if (amsdu) { 1091 if (amsdu_end) { 1092 rap->rxa_start = IEEE80211_SEQ_INC(rxseq); 1093 IEEE80211_NODE_STAT(ni, rx_amsdu_more_end); 1094 } else { 1095 IEEE80211_NODE_STAT(ni, rx_amsdu_more); 1096 } 1097 } else { 1098 rap->rxa_start = IEEE80211_SEQ_INC(rxseq); 1099 } 1100 return PROCESS; 1101 } 1102 } 1103 /* 1104 * Frame is out of order; store if in the BA window. 1105 */ 1106 /* calculate offset in BA window */ 1107 off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start); 1108 if (off < rap->rxa_wnd) { 1109 /* 1110 * Common case (hopefully): in the BA window. 1111 * Sec 9.10.7.6.2 a) (p.137) 1112 */ 1113 1114 /* 1115 * Check for frames sitting too long in the reorder queue. 1116 * This should only ever happen if frames are not delivered 1117 * without the sender otherwise notifying us (e.g. with a 1118 * BAR to move the window). Typically this happens because 1119 * of vendor bugs that cause the sequence number to jump. 1120 * When this happens we get a gap in the reorder queue that 1121 * leaves frame sitting on the queue until they get pushed 1122 * out due to window moves. When the vendor does not send 1123 * BAR this move only happens due to explicit packet sends 1124 * 1125 * NB: we only track the time of the oldest frame in the 1126 * reorder q; this means that if we flush we might push 1127 * frames that still "new"; if this happens then subsequent 1128 * frames will result in BA window moves which cost something 1129 * but is still better than a big throughput dip. 1130 */ 1131 if (rap->rxa_qframes != 0) { 1132 /* XXX honor batimeout? */ 1133 if (ticks - rap->rxa_age > ieee80211_ampdu_age) { 1134 /* 1135 * Too long since we received the first 1136 * frame; flush the reorder buffer. 1137 */ 1138 if (rap->rxa_qframes != 0) { 1139 vap->iv_stats.is_ampdu_rx_age += 1140 rap->rxa_qframes; 1141 ampdu_rx_flush(ni, rap); 1142 } 1143 /* 1144 * Advance the window if needed and notify 1145 * the caller to dispatch directly. 1146 */ 1147 if (amsdu) { 1148 if (amsdu_end) { 1149 rap->rxa_start = 1150 IEEE80211_SEQ_INC(rxseq); 1151 IEEE80211_NODE_STAT(ni, 1152 rx_amsdu_more_end); 1153 } else { 1154 IEEE80211_NODE_STAT(ni, 1155 rx_amsdu_more); 1156 } 1157 } else { 1158 rap->rxa_start = 1159 IEEE80211_SEQ_INC(rxseq); 1160 } 1161 return PROCESS; 1162 } 1163 } else { 1164 /* 1165 * First frame, start aging timer. 1166 */ 1167 rap->rxa_age = ticks; 1168 } 1169 1170 /* save packet - this consumes, no matter what */ 1171 ampdu_rx_add_slot(rap, off, tid, rxseq, ni, m, rxs); 1172 return CONSUMED; 1173 } 1174 if (off < IEEE80211_SEQ_BA_RANGE) { 1175 /* 1176 * Outside the BA window, but within range; 1177 * flush the reorder q and move the window. 1178 * Sec 9.10.7.6.2 b) (p.138) 1179 */ 1180 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni, 1181 "move BA win <%u:%u> (%u frames) rxseq %u tid %u", 1182 rap->rxa_start, 1183 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1), 1184 rap->rxa_qframes, rxseq, tid); 1185 vap->iv_stats.is_ampdu_rx_move++; 1186 1187 /* 1188 * The spec says to flush frames up to but not including: 1189 * WinStart_B = rxseq - rap->rxa_wnd + 1 1190 * Then insert the frame or notify the caller to process 1191 * it immediately. We can safely do this by just starting 1192 * over again because we know the frame will now be within 1193 * the BA window. 1194 */ 1195 /* NB: rxa_wnd known to be >0 */ 1196 ampdu_rx_flush_upto(ni, rap, 1197 IEEE80211_SEQ_SUB(rxseq, rap->rxa_wnd-1)); 1198 goto again; 1199 } else { 1200 /* 1201 * Outside the BA window and out of range; toss. 1202 * Sec 9.10.7.6.2 c) (p.138) 1203 */ 1204 IEEE80211_DISCARD_MAC(vap, 1205 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr, 1206 "MPDU", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s", 1207 rap->rxa_start, 1208 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1), 1209 rap->rxa_qframes, rxseq, tid, 1210 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : ""); 1211 vap->iv_stats.is_ampdu_rx_drop++; 1212 IEEE80211_NODE_STAT(ni, rx_drop); 1213 m_freem(m); 1214 return CONSUMED; 1215 } 1216 #undef CONSUMED 1217 #undef PROCESS 1218 } 1219 1220 /* 1221 * Process a BAR ctl frame. Dispatch all frames up to 1222 * the sequence number of the frame. If this frame is 1223 * out of range it's discarded. 1224 */ 1225 void 1226 ieee80211_recv_bar(struct ieee80211_node *ni, struct mbuf *m0) 1227 { 1228 struct ieee80211vap *vap = ni->ni_vap; 1229 struct ieee80211_frame_bar *wh; 1230 struct ieee80211_rx_ampdu *rap; 1231 ieee80211_seq rxseq; 1232 int tid, off; 1233 1234 if (!ieee80211_recv_bar_ena) { 1235 #if 0 1236 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_11N, 1237 ni->ni_macaddr, "BAR", "%s", "processing disabled"); 1238 #endif 1239 vap->iv_stats.is_ampdu_bar_bad++; 1240 return; 1241 } 1242 wh = mtod(m0, struct ieee80211_frame_bar *); 1243 /* XXX check basic BAR */ 1244 tid = _IEEE80211_MASKSHIFT(le16toh(wh->i_ctl), IEEE80211_BAR_TID); 1245 rap = &ni->ni_rx_ampdu[tid]; 1246 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) { 1247 /* 1248 * No ADDBA request yet, don't touch. 1249 */ 1250 IEEE80211_DISCARD_MAC(vap, 1251 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, 1252 ni->ni_macaddr, "BAR", "no BA stream, tid %u", tid); 1253 vap->iv_stats.is_ampdu_bar_bad++; 1254 return; 1255 } 1256 vap->iv_stats.is_ampdu_bar_rx++; 1257 rxseq = le16toh(wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT; 1258 if (rxseq == rap->rxa_start) 1259 return; 1260 /* calculate offset in BA window */ 1261 off = IEEE80211_SEQ_SUB(rxseq, rap->rxa_start); 1262 if (off < IEEE80211_SEQ_BA_RANGE) { 1263 /* 1264 * Flush the reorder q up to rxseq and move the window. 1265 * Sec 9.10.7.6.3 a) (p.138) 1266 */ 1267 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni, 1268 "BAR moves BA win <%u:%u> (%u frames) rxseq %u tid %u", 1269 rap->rxa_start, 1270 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1), 1271 rap->rxa_qframes, rxseq, tid); 1272 vap->iv_stats.is_ampdu_bar_move++; 1273 1274 ampdu_rx_flush_upto(ni, rap, rxseq); 1275 if (off >= rap->rxa_wnd) { 1276 /* 1277 * BAR specifies a window start to the right of BA 1278 * window; we must move it explicitly since 1279 * ampdu_rx_flush_upto will not. 1280 */ 1281 rap->rxa_start = rxseq; 1282 } 1283 } else { 1284 /* 1285 * Out of range; toss. 1286 * Sec 9.10.7.6.3 b) (p.138) 1287 */ 1288 IEEE80211_DISCARD_MAC(vap, 1289 IEEE80211_MSG_INPUT | IEEE80211_MSG_11N, ni->ni_macaddr, 1290 "BAR", "BA win <%u:%u> (%u frames) rxseq %u tid %u%s", 1291 rap->rxa_start, 1292 IEEE80211_SEQ_ADD(rap->rxa_start, rap->rxa_wnd-1), 1293 rap->rxa_qframes, rxseq, tid, 1294 wh->i_fc[1] & IEEE80211_FC1_RETRY ? " (retransmit)" : ""); 1295 vap->iv_stats.is_ampdu_bar_oow++; 1296 IEEE80211_NODE_STAT(ni, rx_drop); 1297 } 1298 } 1299 1300 /* 1301 * Setup HT-specific state in a node. Called only 1302 * when HT use is negotiated so we don't do extra 1303 * work for temporary and/or legacy sta's. 1304 */ 1305 void 1306 ieee80211_ht_node_init(struct ieee80211_node *ni) 1307 { 1308 struct ieee80211_tx_ampdu *tap; 1309 int tid; 1310 1311 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, 1312 ni, 1313 "%s: called (%p)", 1314 __func__, 1315 ni); 1316 1317 if (ni->ni_flags & IEEE80211_NODE_HT) { 1318 /* 1319 * Clean AMPDU state on re-associate. This handles the case 1320 * where a station leaves w/o notifying us and then returns 1321 * before node is reaped for inactivity. 1322 */ 1323 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, 1324 ni, 1325 "%s: calling cleanup (%p)", 1326 __func__, ni); 1327 ieee80211_ht_node_cleanup(ni); 1328 } 1329 for (tid = 0; tid < WME_NUM_TID; tid++) { 1330 tap = &ni->ni_tx_ampdu[tid]; 1331 tap->txa_tid = tid; 1332 tap->txa_ni = ni; 1333 ieee80211_txampdu_init_pps(tap); 1334 /* NB: further initialization deferred */ 1335 ieee80211_ampdu_rx_init_rap(ni, &ni->ni_rx_ampdu[tid]); 1336 } 1337 ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU | 1338 IEEE80211_NODE_AMSDU; 1339 } 1340 1341 /* 1342 * Cleanup HT-specific state in a node. Called only 1343 * when HT use has been marked. 1344 */ 1345 void 1346 ieee80211_ht_node_cleanup(struct ieee80211_node *ni) 1347 { 1348 struct ieee80211com *ic = ni->ni_ic; 1349 int i; 1350 1351 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, 1352 ni, 1353 "%s: called (%p)", 1354 __func__, ni); 1355 1356 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT node")); 1357 1358 /* XXX optimize this */ 1359 for (i = 0; i < WME_NUM_TID; i++) { 1360 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[i]; 1361 if (tap->txa_flags & IEEE80211_AGGR_SETUP) 1362 ampdu_tx_stop(tap); 1363 } 1364 for (i = 0; i < WME_NUM_TID; i++) 1365 ic->ic_ampdu_rx_stop(ni, &ni->ni_rx_ampdu[i]); 1366 1367 ni->ni_htcap = 0; 1368 ni->ni_flags &= ~IEEE80211_NODE_HT_ALL; 1369 } 1370 1371 /* 1372 * Age out HT resources for a station. 1373 */ 1374 void 1375 ieee80211_ht_node_age(struct ieee80211_node *ni) 1376 { 1377 struct ieee80211vap *vap = ni->ni_vap; 1378 uint8_t tid; 1379 1380 KASSERT(ni->ni_flags & IEEE80211_NODE_HT, ("not an HT sta")); 1381 1382 for (tid = 0; tid < WME_NUM_TID; tid++) { 1383 struct ieee80211_rx_ampdu *rap; 1384 1385 rap = &ni->ni_rx_ampdu[tid]; 1386 if ((rap->rxa_flags & IEEE80211_AGGR_XCHGPEND) == 0) 1387 continue; 1388 if (rap->rxa_qframes == 0) 1389 continue; 1390 /* 1391 * Check for frames sitting too long in the reorder queue. 1392 * See above for more details on what's happening here. 1393 */ 1394 /* XXX honor batimeout? */ 1395 if (ticks - rap->rxa_age > ieee80211_ampdu_age) { 1396 /* 1397 * Too long since we received the first 1398 * frame; flush the reorder buffer. 1399 */ 1400 vap->iv_stats.is_ampdu_rx_age += rap->rxa_qframes; 1401 ampdu_rx_flush(ni, rap); 1402 } 1403 } 1404 } 1405 1406 static struct ieee80211_channel * 1407 findhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int htflags) 1408 { 1409 return ieee80211_find_channel(ic, c->ic_freq, 1410 (c->ic_flags &~ IEEE80211_CHAN_HT) | htflags); 1411 } 1412 1413 /* 1414 * Adjust a channel to be HT/non-HT according to the vap's configuration. 1415 */ 1416 struct ieee80211_channel * 1417 ieee80211_ht_adjust_channel(struct ieee80211com *ic, 1418 struct ieee80211_channel *chan, int flags) 1419 { 1420 struct ieee80211_channel *c; 1421 1422 if (flags & IEEE80211_FHT_HT) { 1423 /* promote to HT if possible */ 1424 if (flags & IEEE80211_FHT_USEHT40) { 1425 if (!IEEE80211_IS_CHAN_HT40(chan)) { 1426 /* NB: arbitrarily pick ht40+ over ht40- */ 1427 c = findhtchan(ic, chan, IEEE80211_CHAN_HT40U); 1428 if (c == NULL) 1429 c = findhtchan(ic, chan, 1430 IEEE80211_CHAN_HT40D); 1431 if (c == NULL) 1432 c = findhtchan(ic, chan, 1433 IEEE80211_CHAN_HT20); 1434 if (c != NULL) 1435 chan = c; 1436 } 1437 } else if (!IEEE80211_IS_CHAN_HT20(chan)) { 1438 c = findhtchan(ic, chan, IEEE80211_CHAN_HT20); 1439 if (c != NULL) 1440 chan = c; 1441 } 1442 } else if (IEEE80211_IS_CHAN_HT(chan)) { 1443 /* demote to legacy, HT use is disabled */ 1444 c = ieee80211_find_channel(ic, chan->ic_freq, 1445 chan->ic_flags &~ IEEE80211_CHAN_HT); 1446 if (c != NULL) 1447 chan = c; 1448 } 1449 return chan; 1450 } 1451 1452 /* 1453 * Setup HT-specific state for a legacy WDS peer. 1454 */ 1455 void 1456 ieee80211_ht_wds_init(struct ieee80211_node *ni) 1457 { 1458 struct ieee80211vap *vap = ni->ni_vap; 1459 struct ieee80211_tx_ampdu *tap; 1460 int tid; 1461 1462 KASSERT(vap->iv_flags_ht & IEEE80211_FHT_HT, ("no HT requested")); 1463 1464 /* XXX check scan cache in case peer has an ap and we have info */ 1465 /* 1466 * If setup with a legacy channel; locate an HT channel. 1467 * Otherwise if the inherited channel (from a companion 1468 * AP) is suitable use it so we use the same location 1469 * for the extension channel). 1470 */ 1471 ni->ni_chan = ieee80211_ht_adjust_channel(ni->ni_ic, 1472 ni->ni_chan, ieee80211_htchanflags(ni->ni_chan)); 1473 1474 ni->ni_htcap = 0; 1475 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) 1476 ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI20; 1477 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) { 1478 ni->ni_htcap |= IEEE80211_HTCAP_CHWIDTH40; 1479 ni->ni_chw = IEEE80211_STA_RX_BW_40; 1480 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan)) 1481 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_ABOVE; 1482 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan)) 1483 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_BELOW; 1484 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) 1485 ni->ni_htcap |= IEEE80211_HTCAP_SHORTGI40; 1486 } else { 1487 ni->ni_chw = IEEE80211_STA_RX_BW_20; 1488 ni->ni_ht2ndchan = IEEE80211_HTINFO_2NDCHAN_NONE; 1489 } 1490 ni->ni_htctlchan = ni->ni_chan->ic_ieee; 1491 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS) 1492 ni->ni_flags |= IEEE80211_NODE_RIFS; 1493 /* XXX does it make sense to enable SMPS? */ 1494 1495 ni->ni_htopmode = 0; /* XXX need protection state */ 1496 ni->ni_htstbc = 0; /* XXX need info */ 1497 1498 for (tid = 0; tid < WME_NUM_TID; tid++) { 1499 tap = &ni->ni_tx_ampdu[tid]; 1500 tap->txa_tid = tid; 1501 ieee80211_txampdu_init_pps(tap); 1502 } 1503 /* NB: AMPDU tx/rx governed by IEEE80211_FHT_AMPDU_{TX,RX} */ 1504 ni->ni_flags |= IEEE80211_NODE_HT | IEEE80211_NODE_AMPDU | 1505 IEEE80211_NODE_AMSDU; 1506 } 1507 1508 /* 1509 * Notify a VAP of a change in the HTINFO ie if it's a hostap VAP. 1510 * 1511 * This is to be called from the deferred HT protection update 1512 * task once the flags are updated. 1513 */ 1514 void 1515 ieee80211_htinfo_notify(struct ieee80211vap *vap) 1516 { 1517 1518 IEEE80211_LOCK_ASSERT(vap->iv_ic); 1519 1520 if (vap->iv_opmode != IEEE80211_M_HOSTAP) 1521 return; 1522 if (vap->iv_state != IEEE80211_S_RUN || 1523 !IEEE80211_IS_CHAN_HT(vap->iv_bss->ni_chan)) 1524 return; 1525 1526 IEEE80211_NOTE(vap, 1527 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, 1528 vap->iv_bss, 1529 "HT bss occupancy change: %d sta, %d ht, " 1530 "%d ht40%s, HT protmode now 0x%x" 1531 , vap->iv_sta_assoc 1532 , vap->iv_ht_sta_assoc 1533 , vap->iv_ht40_sta_assoc 1534 , (vap->iv_flags_ht & IEEE80211_FHT_NONHT_PR) ? 1535 ", non-HT sta present" : "" 1536 , vap->iv_curhtprotmode); 1537 1538 ieee80211_beacon_notify(vap, IEEE80211_BEACON_HTINFO); 1539 } 1540 1541 /* 1542 * Calculate HT protection mode from current 1543 * state and handle updates. 1544 */ 1545 static void 1546 htinfo_update(struct ieee80211vap *vap) 1547 { 1548 struct ieee80211com *ic = vap->iv_ic; 1549 uint8_t protmode; 1550 1551 if (vap->iv_sta_assoc != vap->iv_ht_sta_assoc) { 1552 protmode = IEEE80211_HTINFO_OPMODE_MIXED 1553 | IEEE80211_HTINFO_NONHT_PRESENT; 1554 } else if (vap->iv_flags_ht & IEEE80211_FHT_NONHT_PR) { 1555 protmode = IEEE80211_HTINFO_OPMODE_PROTOPT 1556 | IEEE80211_HTINFO_NONHT_PRESENT; 1557 } else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC && 1558 IEEE80211_IS_CHAN_HT40(ic->ic_bsschan) && 1559 vap->iv_sta_assoc != vap->iv_ht40_sta_assoc) { 1560 protmode = IEEE80211_HTINFO_OPMODE_HT20PR; 1561 } else { 1562 protmode = IEEE80211_HTINFO_OPMODE_PURE; 1563 } 1564 if (protmode != vap->iv_curhtprotmode) { 1565 vap->iv_curhtprotmode = protmode; 1566 /* Update VAP with new protection mode */ 1567 ieee80211_vap_update_ht_protmode(vap); 1568 } 1569 } 1570 1571 /* 1572 * Handle an HT station joining a BSS. 1573 */ 1574 void 1575 ieee80211_ht_node_join(struct ieee80211_node *ni) 1576 { 1577 struct ieee80211vap *vap = ni->ni_vap; 1578 1579 IEEE80211_LOCK_ASSERT(vap->iv_ic); 1580 1581 if (ni->ni_flags & IEEE80211_NODE_HT) { 1582 vap->iv_ht_sta_assoc++; 1583 if (ni->ni_chw == IEEE80211_STA_RX_BW_40) 1584 vap->iv_ht40_sta_assoc++; 1585 } 1586 htinfo_update(vap); 1587 } 1588 1589 /* 1590 * Handle an HT station leaving a BSS. 1591 */ 1592 void 1593 ieee80211_ht_node_leave(struct ieee80211_node *ni) 1594 { 1595 struct ieee80211vap *vap = ni->ni_vap; 1596 1597 IEEE80211_LOCK_ASSERT(vap->iv_ic); 1598 1599 if (ni->ni_flags & IEEE80211_NODE_HT) { 1600 vap->iv_ht_sta_assoc--; 1601 if (ni->ni_chw == IEEE80211_STA_RX_BW_40) 1602 vap->iv_ht40_sta_assoc--; 1603 } 1604 htinfo_update(vap); 1605 } 1606 1607 /* 1608 * Public version of htinfo_update; used for processing 1609 * beacon frames from overlapping bss. 1610 * 1611 * Caller can specify either IEEE80211_HTINFO_OPMODE_MIXED 1612 * (on receipt of a beacon that advertises MIXED) or 1613 * IEEE80211_HTINFO_OPMODE_PROTOPT (on receipt of a beacon 1614 * from an overlapping legacy bss). We treat MIXED with 1615 * a higher precedence than PROTOPT (i.e. we will not change 1616 * change PROTOPT -> MIXED; only MIXED -> PROTOPT). This 1617 * corresponds to how we handle things in htinfo_update. 1618 * 1619 */ 1620 void 1621 ieee80211_htprot_update(struct ieee80211vap *vap, int protmode) 1622 { 1623 struct ieee80211com *ic = vap->iv_ic; 1624 #define OPMODE(x) _IEEE80211_SHIFTMASK(x, IEEE80211_HTINFO_OPMODE) 1625 IEEE80211_LOCK(ic); 1626 1627 /* track non-HT station presence */ 1628 KASSERT(protmode & IEEE80211_HTINFO_NONHT_PRESENT, 1629 ("protmode 0x%x", protmode)); 1630 vap->iv_flags_ht |= IEEE80211_FHT_NONHT_PR; 1631 vap->iv_lastnonht = ticks; 1632 1633 if (protmode != vap->iv_curhtprotmode && 1634 (OPMODE(vap->iv_curhtprotmode) != IEEE80211_HTINFO_OPMODE_MIXED || 1635 OPMODE(protmode) == IEEE80211_HTINFO_OPMODE_PROTOPT)) { 1636 vap->iv_curhtprotmode = protmode; 1637 /* Update VAP with new protection mode */ 1638 ieee80211_vap_update_ht_protmode(vap); 1639 } 1640 IEEE80211_UNLOCK(ic); 1641 #undef OPMODE 1642 } 1643 1644 /* 1645 * Time out presence of an overlapping bss with non-HT 1646 * stations. When operating in hostap mode we listen for 1647 * beacons from other stations and if we identify a non-HT 1648 * station is present we update the opmode field of the 1649 * HTINFO ie. To identify when all non-HT stations are 1650 * gone we time out this condition. 1651 */ 1652 void 1653 ieee80211_ht_timeout(struct ieee80211vap *vap) 1654 { 1655 1656 IEEE80211_LOCK_ASSERT(vap->iv_ic); 1657 1658 if ((vap->iv_flags_ht & IEEE80211_FHT_NONHT_PR) && 1659 ieee80211_time_after(ticks, vap->iv_lastnonht + IEEE80211_NONHT_PRESENT_AGE)) { 1660 IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N, 1661 "%s", "time out non-HT STA present on channel"); 1662 vap->iv_flags_ht &= ~IEEE80211_FHT_NONHT_PR; 1663 htinfo_update(vap); 1664 } 1665 } 1666 1667 /* 1668 * Process an 802.11n HT capabilities ie. 1669 */ 1670 void 1671 ieee80211_parse_htcap(struct ieee80211_node *ni, const uint8_t *ie) 1672 { 1673 if (ie[0] == IEEE80211_ELEMID_VENDOR) { 1674 /* 1675 * Station used Vendor OUI ie to associate; 1676 * mark the node so when we respond we'll use 1677 * the Vendor OUI's and not the standard ie's. 1678 */ 1679 ni->ni_flags |= IEEE80211_NODE_HTCOMPAT; 1680 ie += 4; 1681 } else 1682 ni->ni_flags &= ~IEEE80211_NODE_HTCOMPAT; 1683 1684 ni->ni_htcap = le16dec(ie + 1685 __offsetof(struct ieee80211_ie_htcap, hc_cap)); 1686 ni->ni_htparam = ie[__offsetof(struct ieee80211_ie_htcap, hc_param)]; 1687 } 1688 1689 static void 1690 htinfo_parse(struct ieee80211_node *ni, 1691 const struct ieee80211_ie_htinfo *htinfo) 1692 { 1693 uint16_t w; 1694 1695 ni->ni_htctlchan = htinfo->hi_ctrlchannel; 1696 ni->ni_ht2ndchan = _IEEE80211_SHIFTMASK(htinfo->hi_byte1, 1697 IEEE80211_HTINFO_2NDCHAN); 1698 w = le16dec(&htinfo->hi_byte2); 1699 ni->ni_htopmode = _IEEE80211_SHIFTMASK(w, IEEE80211_HTINFO_OPMODE); 1700 w = le16dec(&htinfo->hi_byte45); 1701 ni->ni_htstbc = _IEEE80211_SHIFTMASK(w, IEEE80211_HTINFO_BASIC_STBCMCS); 1702 } 1703 1704 /* 1705 * Parse an 802.11n HT info ie and save useful information 1706 * to the node state. Note this does not effect any state 1707 * changes such as for channel width change. 1708 */ 1709 void 1710 ieee80211_parse_htinfo(struct ieee80211_node *ni, const uint8_t *ie) 1711 { 1712 if (ie[0] == IEEE80211_ELEMID_VENDOR) 1713 ie += 4; 1714 htinfo_parse(ni, (const struct ieee80211_ie_htinfo *) ie); 1715 } 1716 1717 /* 1718 * Handle 11n/11ac channel switch. 1719 * 1720 * Use the received HT/VHT ie's to identify the right channel to use. 1721 * If we cannot locate it in the channel table then fallback to 1722 * legacy operation. 1723 * 1724 * Note that we use this information to identify the node's 1725 * channel only; the caller is responsible for insuring any 1726 * required channel change is done (e.g. in sta mode when 1727 * parsing the contents of a beacon frame). 1728 */ 1729 static int 1730 htinfo_update_chw(struct ieee80211_node *ni, int htflags, int vhtflags) 1731 { 1732 struct ieee80211com *ic = ni->ni_ic; 1733 struct ieee80211_channel *c; 1734 int chanflags; 1735 int ret = 0; 1736 1737 /* 1738 * First step - do HT/VHT only channel lookup based on operating mode 1739 * flags. This involves masking out the VHT flags as well. 1740 * Otherwise we end up doing the full channel walk each time 1741 * we trigger this, which is expensive. 1742 */ 1743 chanflags = (ni->ni_chan->ic_flags &~ 1744 (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT)) | htflags | vhtflags; 1745 1746 if (chanflags == ni->ni_chan->ic_flags) 1747 goto done; 1748 1749 /* 1750 * If HT /or/ VHT flags have changed then check both. 1751 * We need to start by picking a HT channel anyway. 1752 */ 1753 1754 c = NULL; 1755 chanflags = (ni->ni_chan->ic_flags &~ 1756 (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT)) | htflags; 1757 /* XXX not right for ht40- */ 1758 c = ieee80211_find_channel(ic, ni->ni_chan->ic_freq, chanflags); 1759 if (c == NULL && (htflags & IEEE80211_CHAN_HT40)) { 1760 /* 1761 * No HT40 channel entry in our table; fall back 1762 * to HT20 operation. This should not happen. 1763 */ 1764 c = findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT20); 1765 #if 0 1766 IEEE80211_NOTE(ni->ni_vap, 1767 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni, 1768 "no HT40 channel (freq %u), falling back to HT20", 1769 ni->ni_chan->ic_freq); 1770 #endif 1771 /* XXX stat */ 1772 } 1773 1774 /* Nothing found - leave it alone; move onto VHT */ 1775 if (c == NULL) 1776 c = ni->ni_chan; 1777 1778 /* 1779 * If it's non-HT, then bail out now. 1780 */ 1781 if (! IEEE80211_IS_CHAN_HT(c)) { 1782 IEEE80211_NOTE(ni->ni_vap, 1783 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni, 1784 "not HT; skipping VHT check (%u/0x%x)", 1785 c->ic_freq, c->ic_flags); 1786 goto done; 1787 } 1788 1789 /* 1790 * Next step - look at the current VHT flags and determine 1791 * if we need to upgrade. Mask out the VHT and HT flags since 1792 * the vhtflags field will already have the correct HT 1793 * flags to use. 1794 */ 1795 if (IEEE80211_CONF_VHT(ic) && ni->ni_vhtcap != 0 && vhtflags != 0) { 1796 chanflags = (c->ic_flags 1797 &~ (IEEE80211_CHAN_HT | IEEE80211_CHAN_VHT)) 1798 | vhtflags; 1799 IEEE80211_NOTE(ni->ni_vap, 1800 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, 1801 ni, 1802 "%s: VHT; chanwidth=0x%02x; vhtflags=0x%08x", 1803 __func__, ni->ni_vht_chanwidth, vhtflags); 1804 1805 IEEE80211_NOTE(ni->ni_vap, 1806 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, 1807 ni, 1808 "%s: VHT; trying lookup for %d/0x%08x", 1809 __func__, c->ic_freq, chanflags); 1810 c = ieee80211_find_channel(ic, c->ic_freq, chanflags); 1811 } 1812 1813 /* Finally, if it's changed */ 1814 if (c != NULL && c != ni->ni_chan) { 1815 IEEE80211_NOTE(ni->ni_vap, 1816 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni, 1817 "switch station to %s%d channel %u/0x%x", 1818 IEEE80211_IS_CHAN_VHT(c) ? "VHT" : "HT", 1819 IEEE80211_IS_CHAN_VHT80(c) ? 80 : 1820 (IEEE80211_IS_CHAN_HT40(c) ? 40 : 20), 1821 c->ic_freq, c->ic_flags); 1822 ni->ni_chan = c; 1823 ret = 1; 1824 } 1825 /* NB: caller responsible for forcing any channel change */ 1826 1827 done: 1828 /* update node's (11n) tx channel width */ 1829 ni->ni_chw = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 1830 IEEE80211_STA_RX_BW_40 : IEEE80211_STA_RX_BW_20; 1831 return (ret); 1832 } 1833 1834 /* 1835 * Update 11n MIMO PS state according to received htcap. 1836 */ 1837 static __inline int 1838 htcap_update_mimo_ps(struct ieee80211_node *ni) 1839 { 1840 uint16_t oflags = ni->ni_flags; 1841 1842 switch (ni->ni_htcap & IEEE80211_HTCAP_SMPS) { 1843 case IEEE80211_HTCAP_SMPS_DYNAMIC: 1844 ni->ni_flags |= IEEE80211_NODE_MIMO_PS; 1845 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS; 1846 break; 1847 case IEEE80211_HTCAP_SMPS_ENA: 1848 ni->ni_flags |= IEEE80211_NODE_MIMO_PS; 1849 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS; 1850 break; 1851 case IEEE80211_HTCAP_SMPS_OFF: 1852 default: /* disable on rx of reserved value */ 1853 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS; 1854 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS; 1855 break; 1856 } 1857 return (oflags ^ ni->ni_flags); 1858 } 1859 1860 /* 1861 * Update short GI state according to received htcap 1862 * and local settings. 1863 */ 1864 static __inline void 1865 htcap_update_shortgi(struct ieee80211_node *ni) 1866 { 1867 struct ieee80211vap *vap = ni->ni_vap; 1868 1869 ni->ni_flags &= ~(IEEE80211_NODE_SGI20|IEEE80211_NODE_SGI40); 1870 if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20) && 1871 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)) 1872 ni->ni_flags |= IEEE80211_NODE_SGI20; 1873 if ((ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) && 1874 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)) 1875 ni->ni_flags |= IEEE80211_NODE_SGI40; 1876 } 1877 1878 /* 1879 * Update LDPC state according to received htcap 1880 * and local settings. 1881 */ 1882 static __inline void 1883 htcap_update_ldpc(struct ieee80211_node *ni) 1884 { 1885 struct ieee80211vap *vap = ni->ni_vap; 1886 1887 if ((ni->ni_htcap & IEEE80211_HTCAP_LDPC) && 1888 (vap->iv_flags_ht & IEEE80211_FHT_LDPC_TX)) 1889 ni->ni_flags |= IEEE80211_NODE_LDPC; 1890 } 1891 1892 /* 1893 * Parse and update HT-related state extracted from 1894 * the HT cap and info ie's. 1895 * 1896 * This is called from the STA management path and 1897 * the ieee80211_node_join() path. It will take into 1898 * account the IEs discovered during scanning and 1899 * adjust things accordingly. 1900 */ 1901 void 1902 ieee80211_ht_updateparams(struct ieee80211_node *ni, 1903 const uint8_t *htcapie, const uint8_t *htinfoie) 1904 { 1905 struct ieee80211vap *vap = ni->ni_vap; 1906 const struct ieee80211_ie_htinfo *htinfo; 1907 1908 ieee80211_parse_htcap(ni, htcapie); 1909 if (vap->iv_htcaps & IEEE80211_HTC_SMPS) 1910 htcap_update_mimo_ps(ni); 1911 htcap_update_shortgi(ni); 1912 htcap_update_ldpc(ni); 1913 1914 if (htinfoie[0] == IEEE80211_ELEMID_VENDOR) 1915 htinfoie += 4; 1916 htinfo = (const struct ieee80211_ie_htinfo *) htinfoie; 1917 htinfo_parse(ni, htinfo); 1918 1919 /* 1920 * Defer the node channel change; we need to now 1921 * update VHT parameters before we do it. 1922 */ 1923 1924 if ((htinfo->hi_byte1 & IEEE80211_HTINFO_RIFSMODE_PERM) && 1925 (vap->iv_flags_ht & IEEE80211_FHT_RIFS)) 1926 ni->ni_flags |= IEEE80211_NODE_RIFS; 1927 else 1928 ni->ni_flags &= ~IEEE80211_NODE_RIFS; 1929 } 1930 1931 static uint32_t 1932 ieee80211_vht_get_vhtflags(struct ieee80211_node *ni, uint32_t htflags) 1933 { 1934 struct ieee80211vap *vap = ni->ni_vap; 1935 uint32_t vhtflags = 0; 1936 1937 vhtflags = 0; 1938 if (ni->ni_flags & IEEE80211_NODE_VHT && vap->iv_vht_flags & IEEE80211_FVHT_VHT) { 1939 if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_160MHZ) && 1940 IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(vap->iv_vht_cap.vht_cap_info) && 1941 (vap->iv_vht_flags & IEEE80211_FVHT_USEVHT160)) { 1942 vhtflags = IEEE80211_CHAN_VHT160; 1943 /* Mirror the HT40 flags */ 1944 if (htflags == IEEE80211_CHAN_HT40U) { 1945 vhtflags |= IEEE80211_CHAN_HT40U; 1946 } else if (htflags == IEEE80211_CHAN_HT40D) { 1947 vhtflags |= IEEE80211_CHAN_HT40D; 1948 } 1949 } else if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_80P80MHZ) && 1950 IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(vap->iv_vht_cap.vht_cap_info) && 1951 (vap->iv_vht_flags & IEEE80211_FVHT_USEVHT80P80)) { 1952 vhtflags = IEEE80211_CHAN_VHT80P80; 1953 /* Mirror the HT40 flags */ 1954 if (htflags == IEEE80211_CHAN_HT40U) { 1955 vhtflags |= IEEE80211_CHAN_HT40U; 1956 } else if (htflags == IEEE80211_CHAN_HT40D) { 1957 vhtflags |= IEEE80211_CHAN_HT40D; 1958 } 1959 } else if ((ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_80MHZ) && 1960 (vap->iv_vht_flags & IEEE80211_FVHT_USEVHT80)) { 1961 vhtflags = IEEE80211_CHAN_VHT80; 1962 /* Mirror the HT40 flags */ 1963 if (htflags == IEEE80211_CHAN_HT40U) { 1964 vhtflags |= IEEE80211_CHAN_HT40U; 1965 } else if (htflags == IEEE80211_CHAN_HT40D) { 1966 vhtflags |= IEEE80211_CHAN_HT40D; 1967 } 1968 } else if (ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_USE_HT) { 1969 /* Mirror the HT40 flags */ 1970 /* 1971 * XXX TODO: if ht40 is disabled, but vht40 isn't 1972 * disabled then this logic will get very, very sad. 1973 * It's quite possible the only sane thing to do is 1974 * to not have vht40 as an option, and just obey 1975 * 'ht40' as that flag. 1976 */ 1977 if ((htflags == IEEE80211_CHAN_HT40U) && 1978 (vap->iv_vht_flags & IEEE80211_FVHT_USEVHT40)) { 1979 vhtflags = IEEE80211_CHAN_VHT40U 1980 | IEEE80211_CHAN_HT40U; 1981 } else if (htflags == IEEE80211_CHAN_HT40D && 1982 (vap->iv_vht_flags & IEEE80211_FVHT_USEVHT40)) { 1983 vhtflags = IEEE80211_CHAN_VHT40D 1984 | IEEE80211_CHAN_HT40D; 1985 } else if (htflags == IEEE80211_CHAN_HT20) { 1986 vhtflags = IEEE80211_CHAN_VHT20 1987 | IEEE80211_CHAN_HT20; 1988 } 1989 } else { 1990 vhtflags = IEEE80211_CHAN_VHT20; 1991 } 1992 } 1993 return (vhtflags); 1994 } 1995 1996 /* 1997 * Final part of updating the HT parameters. 1998 * 1999 * This is called from the STA management path and 2000 * the ieee80211_node_join() path. It will take into 2001 * account the IEs discovered during scanning and 2002 * adjust things accordingly. 2003 * 2004 * This is done after a call to ieee80211_ht_updateparams() 2005 * because it (and the upcoming VHT version of updateparams) 2006 * needs to ensure everything is parsed before htinfo_update_chw() 2007 * is called - which will change the channel config for the 2008 * node for us. 2009 */ 2010 int 2011 ieee80211_ht_updateparams_final(struct ieee80211_node *ni, 2012 const uint8_t *htcapie, const uint8_t *htinfoie) 2013 { 2014 struct ieee80211vap *vap = ni->ni_vap; 2015 const struct ieee80211_ie_htinfo *htinfo; 2016 int htflags, vhtflags; 2017 int ret = 0; 2018 2019 htinfo = (const struct ieee80211_ie_htinfo *) htinfoie; 2020 2021 htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ? 2022 IEEE80211_CHAN_HT20 : 0; 2023 2024 /* NB: honor operating mode constraint */ 2025 if ((htinfo->hi_byte1 & IEEE80211_HTINFO_TXWIDTH_2040) && 2026 (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) { 2027 if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_ABOVE) 2028 htflags = IEEE80211_CHAN_HT40U; 2029 else if (ni->ni_ht2ndchan == IEEE80211_HTINFO_2NDCHAN_BELOW) 2030 htflags = IEEE80211_CHAN_HT40D; 2031 } 2032 2033 /* 2034 * VHT flags - do much the same; check whether VHT is available 2035 * and if so, what our ideal channel use would be based on our 2036 * capabilities and the (pre-parsed) VHT info IE. 2037 */ 2038 vhtflags = ieee80211_vht_get_vhtflags(ni, htflags); 2039 2040 if (htinfo_update_chw(ni, htflags, vhtflags)) 2041 ret = 1; 2042 2043 return (ret); 2044 } 2045 2046 /* 2047 * Parse and update HT-related state extracted from the HT cap ie 2048 * for a station joining an HT BSS. 2049 * 2050 * This is called from the hostap path for each station. 2051 */ 2052 void 2053 ieee80211_ht_updatehtcap(struct ieee80211_node *ni, const uint8_t *htcapie) 2054 { 2055 struct ieee80211vap *vap = ni->ni_vap; 2056 2057 ieee80211_parse_htcap(ni, htcapie); 2058 if (vap->iv_htcaps & IEEE80211_HTC_SMPS) 2059 htcap_update_mimo_ps(ni); 2060 htcap_update_shortgi(ni); 2061 htcap_update_ldpc(ni); 2062 } 2063 2064 /* 2065 * Called once HT and VHT capabilities are parsed in hostap mode - 2066 * this will adjust the channel configuration of the given node 2067 * based on the configuration and capabilities. 2068 */ 2069 void 2070 ieee80211_ht_updatehtcap_final(struct ieee80211_node *ni) 2071 { 2072 struct ieee80211vap *vap = ni->ni_vap; 2073 int htflags; 2074 int vhtflags; 2075 2076 /* NB: honor operating mode constraint */ 2077 /* XXX 40 MHz intolerant */ 2078 htflags = (vap->iv_flags_ht & IEEE80211_FHT_HT) ? 2079 IEEE80211_CHAN_HT20 : 0; 2080 if ((ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) && 2081 (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)) { 2082 if (IEEE80211_IS_CHAN_HT40U(vap->iv_bss->ni_chan)) 2083 htflags = IEEE80211_CHAN_HT40U; 2084 else if (IEEE80211_IS_CHAN_HT40D(vap->iv_bss->ni_chan)) 2085 htflags = IEEE80211_CHAN_HT40D; 2086 } 2087 /* 2088 * VHT flags - do much the same; check whether VHT is available 2089 * and if so, what our ideal channel use would be based on our 2090 * capabilities and the (pre-parsed) VHT info IE. 2091 */ 2092 vhtflags = ieee80211_vht_get_vhtflags(ni, htflags); 2093 2094 (void) htinfo_update_chw(ni, htflags, vhtflags); 2095 } 2096 2097 /* 2098 * Install received HT rate set by parsing the HT cap ie. 2099 */ 2100 int 2101 ieee80211_setup_htrates(struct ieee80211_node *ni, const uint8_t *ie, int flags) 2102 { 2103 struct ieee80211com *ic = ni->ni_ic; 2104 struct ieee80211vap *vap = ni->ni_vap; 2105 const struct ieee80211_ie_htcap *htcap; 2106 struct ieee80211_htrateset *rs; 2107 int i, maxequalmcs, maxunequalmcs; 2108 2109 maxequalmcs = ic->ic_txstream * 8 - 1; 2110 maxunequalmcs = 0; 2111 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) { 2112 if (ic->ic_txstream >= 2) 2113 maxunequalmcs = 38; 2114 if (ic->ic_txstream >= 3) 2115 maxunequalmcs = 52; 2116 if (ic->ic_txstream >= 4) 2117 maxunequalmcs = 76; 2118 } 2119 2120 rs = &ni->ni_htrates; 2121 memset(rs, 0, sizeof(*rs)); 2122 if (ie != NULL) { 2123 if (ie[0] == IEEE80211_ELEMID_VENDOR) 2124 ie += 4; 2125 htcap = (const struct ieee80211_ie_htcap *) ie; 2126 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) { 2127 if (isclr(htcap->hc_mcsset, i)) 2128 continue; 2129 if (rs->rs_nrates == IEEE80211_HTRATE_MAXSIZE) { 2130 IEEE80211_NOTE(vap, 2131 IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni, 2132 "WARNING, HT rate set too large; only " 2133 "using %u rates", IEEE80211_HTRATE_MAXSIZE); 2134 vap->iv_stats.is_rx_rstoobig++; 2135 break; 2136 } 2137 if (i <= 31 && i > maxequalmcs) 2138 continue; 2139 if (i == 32 && 2140 (ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0) 2141 continue; 2142 if (i > 32 && i > maxunequalmcs) 2143 continue; 2144 rs->rs_rates[rs->rs_nrates++] = i; 2145 } 2146 } 2147 return ieee80211_fix_rate(ni, (struct ieee80211_rateset *) rs, flags); 2148 } 2149 2150 /* 2151 * Mark rates in a node's HT rate set as basic according 2152 * to the information in the supplied HT info ie. 2153 */ 2154 void 2155 ieee80211_setup_basic_htrates(struct ieee80211_node *ni, const uint8_t *ie) 2156 { 2157 const struct ieee80211_ie_htinfo *htinfo; 2158 struct ieee80211_htrateset *rs; 2159 int i, j; 2160 2161 if (ie[0] == IEEE80211_ELEMID_VENDOR) 2162 ie += 4; 2163 htinfo = (const struct ieee80211_ie_htinfo *) ie; 2164 rs = &ni->ni_htrates; 2165 if (rs->rs_nrates == 0) { 2166 IEEE80211_NOTE(ni->ni_vap, 2167 IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni, 2168 "%s", "WARNING, empty HT rate set"); 2169 return; 2170 } 2171 for (i = 0; i < IEEE80211_HTRATE_MAXSIZE; i++) { 2172 if (isclr(htinfo->hi_basicmcsset, i)) 2173 continue; 2174 for (j = 0; j < rs->rs_nrates; j++) 2175 if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == i) 2176 rs->rs_rates[j] |= IEEE80211_RATE_BASIC; 2177 } 2178 } 2179 2180 static void 2181 ampdu_tx_setup(struct ieee80211_tx_ampdu *tap) 2182 { 2183 callout_init(&tap->txa_timer, 1); 2184 tap->txa_flags |= IEEE80211_AGGR_SETUP; 2185 tap->txa_lastsample = ticks; 2186 } 2187 2188 static void 2189 ampdu_tx_stop(struct ieee80211_tx_ampdu *tap) 2190 { 2191 struct ieee80211_node *ni = tap->txa_ni; 2192 struct ieee80211com *ic = ni->ni_ic; 2193 2194 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N, 2195 tap->txa_ni, 2196 "%s: called", 2197 __func__); 2198 2199 KASSERT(tap->txa_flags & IEEE80211_AGGR_SETUP, 2200 ("txa_flags 0x%x tid %d ac %d", tap->txa_flags, tap->txa_tid, 2201 TID_TO_WME_AC(tap->txa_tid))); 2202 2203 /* 2204 * Stop BA stream if setup so driver has a chance 2205 * to reclaim any resources it might have allocated. 2206 */ 2207 ic->ic_addba_stop(ni, tap); 2208 /* 2209 * Stop any pending BAR transmit. 2210 */ 2211 bar_stop_timer(tap); 2212 2213 /* 2214 * Reset packet estimate. 2215 */ 2216 ieee80211_txampdu_init_pps(tap); 2217 2218 /* NB: clearing NAK means we may re-send ADDBA */ 2219 tap->txa_flags &= ~(IEEE80211_AGGR_SETUP | IEEE80211_AGGR_NAK); 2220 } 2221 2222 /* 2223 * ADDBA response timeout. 2224 * 2225 * If software aggregation and per-TID queue management was done here, 2226 * that queue would be unpaused after the ADDBA timeout occurs. 2227 */ 2228 static void 2229 addba_timeout(void *arg) 2230 { 2231 struct ieee80211_tx_ampdu *tap = arg; 2232 struct ieee80211_node *ni = tap->txa_ni; 2233 struct ieee80211com *ic = ni->ni_ic; 2234 2235 /* XXX ? */ 2236 tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND; 2237 tap->txa_attempts++; 2238 ic->ic_addba_response_timeout(ni, tap); 2239 } 2240 2241 static void 2242 addba_start_timeout(struct ieee80211_tx_ampdu *tap) 2243 { 2244 /* XXX use CALLOUT_PENDING instead? */ 2245 callout_reset(&tap->txa_timer, ieee80211_addba_timeout, 2246 addba_timeout, tap); 2247 tap->txa_flags |= IEEE80211_AGGR_XCHGPEND; 2248 tap->txa_nextrequest = ticks + ieee80211_addba_timeout; 2249 } 2250 2251 static void 2252 addba_stop_timeout(struct ieee80211_tx_ampdu *tap) 2253 { 2254 /* XXX use CALLOUT_PENDING instead? */ 2255 if (tap->txa_flags & IEEE80211_AGGR_XCHGPEND) { 2256 callout_stop(&tap->txa_timer); 2257 tap->txa_flags &= ~IEEE80211_AGGR_XCHGPEND; 2258 } 2259 } 2260 2261 static void 2262 null_addba_response_timeout(struct ieee80211_node *ni, 2263 struct ieee80211_tx_ampdu *tap) 2264 { 2265 } 2266 2267 /* 2268 * Default method for requesting A-MPDU tx aggregation. 2269 * We setup the specified state block and start a timer 2270 * to wait for an ADDBA response frame. 2271 */ 2272 static int 2273 ieee80211_addba_request(struct ieee80211_node *ni, 2274 struct ieee80211_tx_ampdu *tap, 2275 int dialogtoken, int baparamset, int batimeout) 2276 { 2277 int bufsiz; 2278 2279 /* XXX locking */ 2280 tap->txa_token = dialogtoken; 2281 tap->txa_flags |= IEEE80211_AGGR_IMMEDIATE; 2282 bufsiz = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_BUFSIZ); 2283 tap->txa_wnd = (bufsiz == 0) ? 2284 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX); 2285 addba_start_timeout(tap); 2286 return 1; 2287 } 2288 2289 /* 2290 * Called by drivers that wish to request an ADDBA session be 2291 * setup. This brings it up and starts the request timer. 2292 */ 2293 int 2294 ieee80211_ampdu_tx_request_ext(struct ieee80211_node *ni, int tid) 2295 { 2296 struct ieee80211_tx_ampdu *tap; 2297 2298 if (tid < 0 || tid > 15) 2299 return (0); 2300 tap = &ni->ni_tx_ampdu[tid]; 2301 2302 /* XXX locking */ 2303 if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) { 2304 /* do deferred setup of state */ 2305 ampdu_tx_setup(tap); 2306 } 2307 /* XXX hack for not doing proper locking */ 2308 tap->txa_flags &= ~IEEE80211_AGGR_NAK; 2309 addba_start_timeout(tap); 2310 return (1); 2311 } 2312 2313 /* 2314 * Called by drivers that have marked a session as active. 2315 */ 2316 int 2317 ieee80211_ampdu_tx_request_active_ext(struct ieee80211_node *ni, int tid, 2318 int status) 2319 { 2320 struct ieee80211_tx_ampdu *tap; 2321 2322 if (tid < 0 || tid > 15) 2323 return (0); 2324 tap = &ni->ni_tx_ampdu[tid]; 2325 2326 /* XXX locking */ 2327 addba_stop_timeout(tap); 2328 if (status == 1) { 2329 tap->txa_flags |= IEEE80211_AGGR_RUNNING; 2330 tap->txa_attempts = 0; 2331 } else { 2332 /* mark tid so we don't try again */ 2333 tap->txa_flags |= IEEE80211_AGGR_NAK; 2334 } 2335 return (1); 2336 } 2337 2338 /* 2339 * Default method for processing an A-MPDU tx aggregation 2340 * response. We shutdown any pending timer and update the 2341 * state block according to the reply. 2342 */ 2343 static int 2344 ieee80211_addba_response(struct ieee80211_node *ni, 2345 struct ieee80211_tx_ampdu *tap, 2346 int status, int baparamset, int batimeout) 2347 { 2348 struct ieee80211vap *vap = ni->ni_vap; 2349 int bufsiz; 2350 2351 /* XXX locking */ 2352 addba_stop_timeout(tap); 2353 if (status == IEEE80211_STATUS_SUCCESS) { 2354 bufsiz = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_BUFSIZ); 2355 /* XXX override our request? */ 2356 tap->txa_wnd = (bufsiz == 0) ? 2357 IEEE80211_AGGR_BAWMAX : min(bufsiz, IEEE80211_AGGR_BAWMAX); 2358 #ifdef __notyet__ 2359 tid = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_TID); 2360 #endif 2361 tap->txa_flags |= IEEE80211_AGGR_RUNNING; 2362 tap->txa_attempts = 0; 2363 /* TODO: this should be a vap flag */ 2364 if ((vap->iv_htcaps & IEEE80211_HTC_TX_AMSDU_AMPDU) && 2365 (ni->ni_flags & IEEE80211_NODE_AMSDU_TX) && 2366 (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU))) 2367 tap->txa_flags |= IEEE80211_AGGR_AMSDU; 2368 else 2369 tap->txa_flags &= ~IEEE80211_AGGR_AMSDU; 2370 } else { 2371 /* mark tid so we don't try again */ 2372 tap->txa_flags |= IEEE80211_AGGR_NAK; 2373 } 2374 return 1; 2375 } 2376 2377 /* 2378 * Default method for stopping A-MPDU tx aggregation. 2379 * Any timer is cleared and we drain any pending frames. 2380 */ 2381 static void 2382 ieee80211_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap) 2383 { 2384 /* XXX locking */ 2385 addba_stop_timeout(tap); 2386 if (tap->txa_flags & IEEE80211_AGGR_RUNNING) { 2387 /* XXX clear aggregation queue */ 2388 tap->txa_flags &= ~(IEEE80211_AGGR_RUNNING | IEEE80211_AGGR_AMSDU); 2389 } 2390 tap->txa_attempts = 0; 2391 } 2392 2393 /* 2394 * Process a received action frame using the default aggregation 2395 * policy. We intercept ADDBA-related frames and use them to 2396 * update our aggregation state. All other frames are passed up 2397 * for processing by ieee80211_recv_action. 2398 */ 2399 static int 2400 ht_recv_action_ba_addba_request(struct ieee80211_node *ni, 2401 const struct ieee80211_frame *wh, 2402 const uint8_t *frm, const uint8_t *efrm) 2403 { 2404 struct ieee80211com *ic = ni->ni_ic; 2405 struct ieee80211vap *vap = ni->ni_vap; 2406 struct ieee80211_rx_ampdu *rap; 2407 uint8_t dialogtoken; 2408 uint16_t baparamset, batimeout, baseqctl; 2409 uint16_t args[5]; 2410 int tid; 2411 2412 dialogtoken = frm[2]; 2413 baparamset = le16dec(frm+3); 2414 batimeout = le16dec(frm+5); 2415 baseqctl = le16dec(frm+7); 2416 2417 tid = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_TID); 2418 2419 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni, 2420 "recv ADDBA request: dialogtoken %u baparamset 0x%x " 2421 "(tid %d bufsiz %d) batimeout %d baseqctl %d:%d amsdu %d", 2422 dialogtoken, baparamset, 2423 tid, _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_BUFSIZ), 2424 batimeout, 2425 _IEEE80211_MASKSHIFT(baseqctl, IEEE80211_BASEQ_START), 2426 _IEEE80211_MASKSHIFT(baseqctl, IEEE80211_BASEQ_FRAG), 2427 _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)); 2428 2429 rap = &ni->ni_rx_ampdu[tid]; 2430 2431 /* Send ADDBA response */ 2432 args[0] = dialogtoken; 2433 /* 2434 * NB: We ack only if the sta associated with HT and 2435 * the ap is configured to do AMPDU rx (the latter 2436 * violates the 11n spec and is mostly for testing). 2437 */ 2438 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_RX) && 2439 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)) { 2440 /* XXX TODO: handle ampdu_rx_start failure */ 2441 ic->ic_ampdu_rx_start(ni, rap, 2442 baparamset, batimeout, baseqctl); 2443 2444 args[1] = IEEE80211_STATUS_SUCCESS; 2445 } else { 2446 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, 2447 ni, "reject ADDBA request: %s", 2448 ni->ni_flags & IEEE80211_NODE_AMPDU_RX ? 2449 "administratively disabled" : 2450 "not negotiated for station"); 2451 vap->iv_stats.is_addba_reject++; 2452 args[1] = IEEE80211_STATUS_UNSPECIFIED; 2453 } 2454 /* XXX honor rap flags? */ 2455 args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE 2456 | _IEEE80211_SHIFTMASK(tid, IEEE80211_BAPS_TID) 2457 | _IEEE80211_SHIFTMASK(rap->rxa_wnd, IEEE80211_BAPS_BUFSIZ) 2458 ; 2459 2460 /* 2461 * TODO: we're out of iv_flags_ht fields; once 2462 * this is extended we should make this configurable. 2463 */ 2464 if ((baparamset & IEEE80211_BAPS_AMSDU) && 2465 (ni->ni_flags & IEEE80211_NODE_AMSDU_RX) && 2466 (vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU)) 2467 args[2] |= IEEE80211_BAPS_AMSDU; 2468 2469 args[3] = 0; 2470 args[4] = 0; 2471 ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA, 2472 IEEE80211_ACTION_BA_ADDBA_RESPONSE, args); 2473 return 0; 2474 } 2475 2476 static int 2477 ht_recv_action_ba_addba_response(struct ieee80211_node *ni, 2478 const struct ieee80211_frame *wh, 2479 const uint8_t *frm, const uint8_t *efrm) 2480 { 2481 struct ieee80211com *ic = ni->ni_ic; 2482 struct ieee80211vap *vap = ni->ni_vap; 2483 struct ieee80211_tx_ampdu *tap; 2484 uint8_t dialogtoken, policy; 2485 uint16_t baparamset, batimeout, code; 2486 int tid; 2487 #ifdef IEEE80211_DEBUG 2488 int amsdu, bufsiz; 2489 #endif 2490 2491 dialogtoken = frm[2]; 2492 code = le16dec(frm+3); 2493 baparamset = le16dec(frm+5); 2494 tid = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_TID); 2495 #ifdef IEEE80211_DEBUG 2496 bufsiz = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_BUFSIZ); 2497 amsdu = !! _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU); 2498 #endif 2499 policy = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_POLICY); 2500 batimeout = le16dec(frm+7); 2501 2502 tap = &ni->ni_tx_ampdu[tid]; 2503 if ((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0) { 2504 IEEE80211_DISCARD_MAC(vap, 2505 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, 2506 ni->ni_macaddr, "ADDBA response", 2507 "no pending ADDBA, tid %d dialogtoken %u " 2508 "code %d", tid, dialogtoken, code); 2509 vap->iv_stats.is_addba_norequest++; 2510 return 0; 2511 } 2512 if (dialogtoken != tap->txa_token) { 2513 IEEE80211_DISCARD_MAC(vap, 2514 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, 2515 ni->ni_macaddr, "ADDBA response", 2516 "dialogtoken mismatch: waiting for %d, " 2517 "received %d, tid %d code %d", 2518 tap->txa_token, dialogtoken, tid, code); 2519 vap->iv_stats.is_addba_badtoken++; 2520 return 0; 2521 } 2522 /* NB: assumes IEEE80211_AGGR_IMMEDIATE is 1 */ 2523 if (policy != (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE)) { 2524 IEEE80211_DISCARD_MAC(vap, 2525 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, 2526 ni->ni_macaddr, "ADDBA response", 2527 "policy mismatch: expecting %s, " 2528 "received %s, tid %d code %d", 2529 tap->txa_flags & IEEE80211_AGGR_IMMEDIATE, 2530 policy, tid, code); 2531 vap->iv_stats.is_addba_badpolicy++; 2532 return 0; 2533 } 2534 #if 0 2535 /* XXX we take MIN in ieee80211_addba_response */ 2536 if (bufsiz > IEEE80211_AGGR_BAWMAX) { 2537 IEEE80211_DISCARD_MAC(vap, 2538 IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, 2539 ni->ni_macaddr, "ADDBA response", 2540 "BA window too large: max %d, " 2541 "received %d, tid %d code %d", 2542 bufsiz, IEEE80211_AGGR_BAWMAX, tid, code); 2543 vap->iv_stats.is_addba_badbawinsize++; 2544 return 0; 2545 } 2546 #endif 2547 2548 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni, 2549 "recv ADDBA response: dialogtoken %u code %d " 2550 "baparamset 0x%x (tid %d bufsiz %d amsdu %d) batimeout %d", 2551 dialogtoken, code, baparamset, tid, 2552 bufsiz, 2553 amsdu, 2554 batimeout); 2555 ic->ic_addba_response(ni, tap, code, baparamset, batimeout); 2556 return 0; 2557 } 2558 2559 static int 2560 ht_recv_action_ba_delba(struct ieee80211_node *ni, 2561 const struct ieee80211_frame *wh, 2562 const uint8_t *frm, const uint8_t *efrm) 2563 { 2564 struct ieee80211com *ic = ni->ni_ic; 2565 struct ieee80211_rx_ampdu *rap; 2566 struct ieee80211_tx_ampdu *tap; 2567 uint16_t baparamset; 2568 #ifdef IEEE80211_DEBUG 2569 uint16_t code; 2570 #endif 2571 int tid; 2572 2573 baparamset = le16dec(frm+2); 2574 #ifdef IEEE80211_DEBUG 2575 code = le16dec(frm+4); 2576 #endif 2577 2578 tid = _IEEE80211_MASKSHIFT(baparamset, IEEE80211_DELBAPS_TID); 2579 2580 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni, 2581 "recv DELBA: baparamset 0x%x (tid %d initiator %d) " 2582 "code %d", baparamset, tid, 2583 _IEEE80211_MASKSHIFT(baparamset, IEEE80211_DELBAPS_INIT), code); 2584 2585 if ((baparamset & IEEE80211_DELBAPS_INIT) == 0) { 2586 tap = &ni->ni_tx_ampdu[tid]; 2587 ic->ic_addba_stop(ni, tap); 2588 } else { 2589 rap = &ni->ni_rx_ampdu[tid]; 2590 ic->ic_ampdu_rx_stop(ni, rap); 2591 } 2592 return 0; 2593 } 2594 2595 /* 2596 * Handle the HT channel width action frame. 2597 * 2598 * 802.11-2020 9.6.11.2 (Notify Channel Width frame format). 2599 */ 2600 static int 2601 ht_recv_action_ht_txchwidth(struct ieee80211_node *ni, 2602 const struct ieee80211_frame *wh __unused, 2603 const uint8_t *frm, const uint8_t *efrm __unused) 2604 { 2605 int chw; 2606 2607 /* If 20/40 is not supported the chw cannot change. */ 2608 if ((ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40) == 0) 2609 return (0); 2610 2611 /* 2612 * The supported values are either 0 (any supported width) 2613 * or 1 (HT20). 80, 160, etc MHz widths are not represented 2614 * here. 2615 */ 2616 chw = (frm[2] == IEEE80211_A_HT_TXCHWIDTH_2040) ? 2617 IEEE80211_STA_RX_BW_40 : IEEE80211_STA_RX_BW_20; 2618 2619 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni, 2620 "%s: HT txchwidth, width %d%s (%s)", __func__, 2621 chw, ni->ni_chw != chw ? "*" : "", ieee80211_ni_chw_to_str(chw)); 2622 if (chw != ni->ni_chw) { 2623 /* XXX does this need to change the ht40 station count? */ 2624 ni->ni_chw = chw; 2625 /* XXX notify on change */ 2626 } 2627 return 0; 2628 } 2629 2630 static int 2631 ht_recv_action_ht_mimopwrsave(struct ieee80211_node *ni, 2632 const struct ieee80211_frame *wh, 2633 const uint8_t *frm, const uint8_t *efrm) 2634 { 2635 const struct ieee80211_action_ht_mimopowersave *mps = 2636 (const struct ieee80211_action_ht_mimopowersave *) frm; 2637 2638 /* XXX check iv_htcaps */ 2639 if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_ENA) 2640 ni->ni_flags |= IEEE80211_NODE_MIMO_PS; 2641 else 2642 ni->ni_flags &= ~IEEE80211_NODE_MIMO_PS; 2643 if (mps->am_control & IEEE80211_A_HT_MIMOPWRSAVE_MODE) 2644 ni->ni_flags |= IEEE80211_NODE_MIMO_RTS; 2645 else 2646 ni->ni_flags &= ~IEEE80211_NODE_MIMO_RTS; 2647 /* XXX notify on change */ 2648 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni, 2649 "%s: HT MIMO PS (%s%s)", __func__, 2650 (ni->ni_flags & IEEE80211_NODE_MIMO_PS) ? "on" : "off", 2651 (ni->ni_flags & IEEE80211_NODE_MIMO_RTS) ? "+rts" : "" 2652 ); 2653 return 0; 2654 } 2655 2656 /* 2657 * Transmit processing. 2658 */ 2659 2660 /* 2661 * Check if A-MPDU should be requested/enabled for a stream. 2662 * We require a traffic rate above a per-AC threshold and we 2663 * also handle backoff from previous failed attempts. 2664 * 2665 * Drivers may override this method to bring in information 2666 * such as link state conditions in making the decision. 2667 */ 2668 static int 2669 ieee80211_ampdu_enable(struct ieee80211_node *ni, 2670 struct ieee80211_tx_ampdu *tap) 2671 { 2672 struct ieee80211vap *vap = ni->ni_vap; 2673 2674 if (tap->txa_avgpps < 2675 vap->iv_ampdu_mintraffic[TID_TO_WME_AC(tap->txa_tid)]) 2676 return 0; 2677 /* XXX check rssi? */ 2678 if (tap->txa_attempts >= ieee80211_addba_maxtries && 2679 ieee80211_time_after(ticks, tap->txa_nextrequest)) { 2680 /* 2681 * Don't retry too often; txa_nextrequest is set 2682 * to the minimum interval we'll retry after 2683 * ieee80211_addba_maxtries failed attempts are made. 2684 */ 2685 return 0; 2686 } 2687 IEEE80211_NOTE(vap, IEEE80211_MSG_11N, ni, 2688 "enable AMPDU on tid %d (%s), avgpps %d pkts %d attempt %d", 2689 tap->txa_tid, ieee80211_wme_acnames[TID_TO_WME_AC(tap->txa_tid)], 2690 tap->txa_avgpps, tap->txa_pkts, tap->txa_attempts); 2691 return 1; 2692 } 2693 2694 /* 2695 * Request A-MPDU tx aggregation. Setup local state and 2696 * issue an ADDBA request. BA use will only happen after 2697 * the other end replies with ADDBA response. 2698 */ 2699 int 2700 ieee80211_ampdu_request(struct ieee80211_node *ni, 2701 struct ieee80211_tx_ampdu *tap) 2702 { 2703 struct ieee80211com *ic = ni->ni_ic; 2704 uint16_t args[5]; 2705 int tid, dialogtoken; 2706 static int tokens = 0; /* XXX */ 2707 2708 /* XXX locking */ 2709 if ((tap->txa_flags & IEEE80211_AGGR_SETUP) == 0) { 2710 /* do deferred setup of state */ 2711 ampdu_tx_setup(tap); 2712 } 2713 /* XXX hack for not doing proper locking */ 2714 tap->txa_flags &= ~IEEE80211_AGGR_NAK; 2715 2716 dialogtoken = (tokens+1) % 63; /* XXX */ 2717 tid = tap->txa_tid; 2718 2719 /* 2720 * XXX TODO: This is racy with any other parallel TX going on. :( 2721 */ 2722 tap->txa_start = ni->ni_txseqs[tid]; 2723 2724 args[0] = dialogtoken; 2725 args[1] = 0; /* NB: status code not used */ 2726 args[2] = IEEE80211_BAPS_POLICY_IMMEDIATE 2727 | _IEEE80211_SHIFTMASK(tid, IEEE80211_BAPS_TID) 2728 | _IEEE80211_SHIFTMASK(IEEE80211_AGGR_BAWMAX, 2729 IEEE80211_BAPS_BUFSIZ) 2730 ; 2731 2732 /* XXX TODO: this should be a flag, not iv_htcaps */ 2733 if ((ni->ni_flags & IEEE80211_NODE_AMSDU_TX) && 2734 (ni->ni_vap->iv_htcaps & IEEE80211_HTC_TX_AMSDU_AMPDU)) 2735 args[2] |= IEEE80211_BAPS_AMSDU; 2736 2737 args[3] = 0; /* batimeout */ 2738 /* NB: do first so there's no race against reply */ 2739 if (!ic->ic_addba_request(ni, tap, dialogtoken, args[2], args[3])) { 2740 /* unable to setup state, don't make request */ 2741 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, 2742 ni, "%s: could not setup BA stream for TID %d AC %d", 2743 __func__, tap->txa_tid, TID_TO_WME_AC(tap->txa_tid)); 2744 /* defer next try so we don't slam the driver with requests */ 2745 tap->txa_attempts = ieee80211_addba_maxtries; 2746 /* NB: check in case driver wants to override */ 2747 if (tap->txa_nextrequest <= ticks) 2748 tap->txa_nextrequest = ticks + ieee80211_addba_backoff; 2749 return 0; 2750 } 2751 tokens = dialogtoken; /* allocate token */ 2752 /* NB: after calling ic_addba_request so driver can set txa_start */ 2753 args[4] = _IEEE80211_SHIFTMASK(tap->txa_start, IEEE80211_BASEQ_START) 2754 | _IEEE80211_SHIFTMASK(0, IEEE80211_BASEQ_FRAG) 2755 ; 2756 return ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA, 2757 IEEE80211_ACTION_BA_ADDBA_REQUEST, args); 2758 } 2759 2760 /* 2761 * Terminate an AMPDU tx stream. State is reclaimed 2762 * and the peer notified with a DelBA Action frame. 2763 */ 2764 void 2765 ieee80211_ampdu_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap, 2766 int reason) 2767 { 2768 struct ieee80211com *ic = ni->ni_ic; 2769 struct ieee80211vap *vap = ni->ni_vap; 2770 uint16_t args[4]; 2771 2772 /* XXX locking */ 2773 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND; 2774 if (IEEE80211_AMPDU_RUNNING(tap)) { 2775 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, 2776 ni, "%s: stop BA stream for TID %d (reason: %d (%s))", 2777 __func__, tap->txa_tid, reason, 2778 ieee80211_reason_to_string(reason)); 2779 vap->iv_stats.is_ampdu_stop++; 2780 2781 ic->ic_addba_stop(ni, tap); 2782 args[0] = tap->txa_tid; 2783 args[1] = IEEE80211_DELBAPS_INIT; 2784 args[2] = reason; /* XXX reason code */ 2785 ic->ic_send_action(ni, IEEE80211_ACTION_CAT_BA, 2786 IEEE80211_ACTION_BA_DELBA, args); 2787 } else { 2788 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, 2789 ni, "%s: BA stream for TID %d not running " 2790 "(reason: %d (%s))", __func__, tap->txa_tid, reason, 2791 ieee80211_reason_to_string(reason)); 2792 vap->iv_stats.is_ampdu_stop_failed++; 2793 } 2794 } 2795 2796 /* XXX */ 2797 static void bar_start_timer(struct ieee80211_tx_ampdu *tap); 2798 2799 static void 2800 bar_timeout(void *arg) 2801 { 2802 struct ieee80211_tx_ampdu *tap = arg; 2803 struct ieee80211_node *ni = tap->txa_ni; 2804 2805 KASSERT((tap->txa_flags & IEEE80211_AGGR_XCHGPEND) == 0, 2806 ("bar/addba collision, flags 0x%x", tap->txa_flags)); 2807 2808 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, 2809 ni, "%s: tid %u flags 0x%x attempts %d", __func__, 2810 tap->txa_tid, tap->txa_flags, tap->txa_attempts); 2811 2812 /* guard against race with bar_tx_complete */ 2813 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0) 2814 return; 2815 /* XXX ? */ 2816 if (tap->txa_attempts >= ieee80211_bar_maxtries) { 2817 struct ieee80211com *ic = ni->ni_ic; 2818 2819 ni->ni_vap->iv_stats.is_ampdu_bar_tx_fail++; 2820 /* 2821 * If (at least) the last BAR TX timeout was due to 2822 * an ieee80211_send_bar() failures, then we need 2823 * to make sure we notify the driver that a BAR 2824 * TX did occur and fail. This gives the driver 2825 * a chance to undo any queue pause that may 2826 * have occurred. 2827 */ 2828 ic->ic_bar_response(ni, tap, 1); 2829 ieee80211_ampdu_stop(ni, tap, IEEE80211_REASON_TIMEOUT); 2830 } else { 2831 ni->ni_vap->iv_stats.is_ampdu_bar_tx_retry++; 2832 if (ieee80211_send_bar(ni, tap, tap->txa_seqpending) != 0) { 2833 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, 2834 ni, "%s: failed to TX, starting timer\n", 2835 __func__); 2836 /* 2837 * If ieee80211_send_bar() fails here, the 2838 * timer may have stopped and/or the pending 2839 * flag may be clear. Because of this, 2840 * fake the BARPEND and reset the timer. 2841 * A retransmission attempt will then occur 2842 * during the next timeout. 2843 */ 2844 /* XXX locking */ 2845 tap->txa_flags |= IEEE80211_AGGR_BARPEND; 2846 bar_start_timer(tap); 2847 } 2848 } 2849 } 2850 2851 static void 2852 bar_start_timer(struct ieee80211_tx_ampdu *tap) 2853 { 2854 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N, 2855 tap->txa_ni, 2856 "%s: called", 2857 __func__); 2858 callout_reset(&tap->txa_timer, ieee80211_bar_timeout, bar_timeout, tap); 2859 } 2860 2861 static void 2862 bar_stop_timer(struct ieee80211_tx_ampdu *tap) 2863 { 2864 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N, 2865 tap->txa_ni, 2866 "%s: called", 2867 __func__); 2868 callout_stop(&tap->txa_timer); 2869 } 2870 2871 static void 2872 bar_tx_complete(struct ieee80211_node *ni, void *arg, int status) 2873 { 2874 struct ieee80211_tx_ampdu *tap = arg; 2875 2876 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, 2877 ni, "%s: tid %u flags 0x%x pending %d status %d", 2878 __func__, tap->txa_tid, tap->txa_flags, 2879 callout_pending(&tap->txa_timer), status); 2880 2881 ni->ni_vap->iv_stats.is_ampdu_bar_tx++; 2882 /* XXX locking */ 2883 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) && 2884 callout_pending(&tap->txa_timer)) { 2885 struct ieee80211com *ic = ni->ni_ic; 2886 2887 if (status == 0) /* ACK'd */ 2888 bar_stop_timer(tap); 2889 ic->ic_bar_response(ni, tap, status); 2890 /* NB: just let timer expire so we pace requests */ 2891 } 2892 } 2893 2894 static void 2895 ieee80211_bar_response(struct ieee80211_node *ni, 2896 struct ieee80211_tx_ampdu *tap, int status) 2897 { 2898 2899 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N, 2900 tap->txa_ni, 2901 "%s: called", 2902 __func__); 2903 if (status == 0) { /* got ACK */ 2904 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, 2905 ni, "BAR moves BA win <%u:%u> (%u frames) txseq %u tid %u", 2906 tap->txa_start, 2907 IEEE80211_SEQ_ADD(tap->txa_start, tap->txa_wnd-1), 2908 tap->txa_qframes, tap->txa_seqpending, 2909 tap->txa_tid); 2910 2911 /* NB: timer already stopped in bar_tx_complete */ 2912 tap->txa_start = tap->txa_seqpending; 2913 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND; 2914 } 2915 } 2916 2917 /* 2918 * Transmit a BAR frame to the specified node. The 2919 * BAR contents are drawn from the supplied aggregation 2920 * state associated with the node. 2921 * 2922 * NB: we only handle immediate ACK w/ compressed bitmap. 2923 */ 2924 int 2925 ieee80211_send_bar(struct ieee80211_node *ni, 2926 struct ieee80211_tx_ampdu *tap, ieee80211_seq seq) 2927 { 2928 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0) 2929 struct ieee80211vap *vap = ni->ni_vap; 2930 struct ieee80211com *ic = ni->ni_ic; 2931 struct ieee80211_frame_bar *bar; 2932 struct mbuf *m; 2933 uint16_t barctl, barseqctl; 2934 uint8_t *frm; 2935 int tid, ret; 2936 2937 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N, 2938 tap->txa_ni, 2939 "%s: called", 2940 __func__); 2941 2942 if ((tap->txa_flags & IEEE80211_AGGR_RUNNING) == 0) { 2943 /* no ADDBA response, should not happen */ 2944 /* XXX stat+msg */ 2945 return EINVAL; 2946 } 2947 /* XXX locking */ 2948 bar_stop_timer(tap); 2949 2950 ieee80211_ref_node(ni); 2951 2952 m = ieee80211_getmgtframe(&frm, ic->ic_headroom, sizeof(*bar)); 2953 if (m == NULL) 2954 senderr(ENOMEM, is_tx_nobuf); 2955 2956 if (!ieee80211_add_callback(m, bar_tx_complete, tap)) { 2957 m_freem(m); 2958 senderr(ENOMEM, is_tx_nobuf); /* XXX */ 2959 /* NOTREACHED */ 2960 } 2961 2962 bar = mtod(m, struct ieee80211_frame_bar *); 2963 bar->i_fc[0] = IEEE80211_FC0_VERSION_0 | 2964 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR; 2965 bar->i_fc[1] = 0; 2966 IEEE80211_ADDR_COPY(bar->i_ra, ni->ni_macaddr); 2967 IEEE80211_ADDR_COPY(bar->i_ta, vap->iv_myaddr); 2968 2969 tid = tap->txa_tid; 2970 barctl = (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ? 2971 0 : IEEE80211_BAR_NOACK) 2972 | IEEE80211_BAR_COMP 2973 | _IEEE80211_SHIFTMASK(tid, IEEE80211_BAR_TID) 2974 ; 2975 barseqctl = _IEEE80211_SHIFTMASK(seq, IEEE80211_BAR_SEQ_START); 2976 /* NB: known to have proper alignment */ 2977 bar->i_ctl = htole16(barctl); 2978 bar->i_seq = htole16(barseqctl); 2979 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_bar); 2980 2981 M_WME_SETAC(m, WME_AC_VO); 2982 2983 IEEE80211_NODE_STAT(ni, tx_mgmt); /* XXX tx_ctl? */ 2984 2985 /* XXX locking */ 2986 /* init/bump attempts counter */ 2987 if ((tap->txa_flags & IEEE80211_AGGR_BARPEND) == 0) 2988 tap->txa_attempts = 1; 2989 else 2990 tap->txa_attempts++; 2991 tap->txa_seqpending = seq; 2992 tap->txa_flags |= IEEE80211_AGGR_BARPEND; 2993 2994 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N, 2995 ni, "send BAR: tid %u ctl 0x%x start %u (attempt %d)", 2996 tid, barctl, seq, tap->txa_attempts); 2997 2998 /* 2999 * ic_raw_xmit will free the node reference 3000 * regardless of queue/TX success or failure. 3001 */ 3002 IEEE80211_TX_LOCK(ic); 3003 ret = ieee80211_raw_output(vap, ni, m, NULL); 3004 IEEE80211_TX_UNLOCK(ic); 3005 if (ret != 0) { 3006 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_11N, 3007 ni, "send BAR: failed: (ret = %d)\n", 3008 ret); 3009 /* xmit failed, clear state flag */ 3010 tap->txa_flags &= ~IEEE80211_AGGR_BARPEND; 3011 vap->iv_stats.is_ampdu_bar_tx_fail++; 3012 return ret; 3013 } 3014 /* XXX hack against tx complete happening before timer is started */ 3015 if (tap->txa_flags & IEEE80211_AGGR_BARPEND) 3016 bar_start_timer(tap); 3017 return 0; 3018 bad: 3019 IEEE80211_NOTE(tap->txa_ni->ni_vap, IEEE80211_MSG_11N, 3020 tap->txa_ni, 3021 "%s: bad! ret=%d", 3022 __func__, ret); 3023 vap->iv_stats.is_ampdu_bar_tx_fail++; 3024 ieee80211_free_node(ni); 3025 return ret; 3026 #undef senderr 3027 } 3028 3029 static int 3030 ht_action_output(struct ieee80211_node *ni, struct mbuf *m) 3031 { 3032 struct ieee80211_bpf_params params; 3033 3034 memset(¶ms, 0, sizeof(params)); 3035 params.ibp_pri = WME_AC_VO; 3036 params.ibp_rate0 = ni->ni_txparms->mgmtrate; 3037 /* NB: we know all frames are unicast */ 3038 params.ibp_try0 = ni->ni_txparms->maxretry; 3039 params.ibp_power = ni->ni_txpower; 3040 return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION, 3041 ¶ms); 3042 } 3043 3044 #define ADDSHORT(frm, v) do { \ 3045 frm[0] = (v) & 0xff; \ 3046 frm[1] = (v) >> 8; \ 3047 frm += 2; \ 3048 } while (0) 3049 3050 /* 3051 * Send an action management frame. The arguments are stuff 3052 * into a frame without inspection; the caller is assumed to 3053 * prepare them carefully (e.g. based on the aggregation state). 3054 */ 3055 static int 3056 ht_send_action_ba_addba(struct ieee80211_node *ni, 3057 int category, int action, void *arg0) 3058 { 3059 struct ieee80211vap *vap = ni->ni_vap; 3060 struct ieee80211com *ic = ni->ni_ic; 3061 uint16_t *args = arg0; 3062 struct mbuf *m; 3063 uint8_t *frm; 3064 3065 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni, 3066 "send ADDBA %s: dialogtoken %d status %d " 3067 "baparamset 0x%x (tid %d amsdu %d) batimeout 0x%x baseqctl 0x%x", 3068 (action == IEEE80211_ACTION_BA_ADDBA_REQUEST) ? 3069 "request" : "response", args[0], args[1], args[2], 3070 _IEEE80211_MASKSHIFT(args[2], IEEE80211_BAPS_TID), 3071 _IEEE80211_MASKSHIFT(args[2], IEEE80211_BAPS_AMSDU), 3072 args[3], args[4]); 3073 3074 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 3075 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__, 3076 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1); 3077 ieee80211_ref_node(ni); 3078 3079 m = ieee80211_getmgtframe(&frm, 3080 ic->ic_headroom + sizeof(struct ieee80211_frame), 3081 sizeof(uint16_t) /* action+category */ 3082 /* XXX may action payload */ 3083 + sizeof(struct ieee80211_action_ba_addbaresponse) 3084 ); 3085 if (m != NULL) { 3086 *frm++ = category; 3087 *frm++ = action; 3088 *frm++ = args[0]; /* dialog token */ 3089 if (action == IEEE80211_ACTION_BA_ADDBA_RESPONSE) 3090 ADDSHORT(frm, args[1]); /* status code */ 3091 ADDSHORT(frm, args[2]); /* baparamset */ 3092 ADDSHORT(frm, args[3]); /* batimeout */ 3093 if (action == IEEE80211_ACTION_BA_ADDBA_REQUEST) 3094 ADDSHORT(frm, args[4]); /* baseqctl */ 3095 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 3096 return ht_action_output(ni, m); 3097 } else { 3098 vap->iv_stats.is_tx_nobuf++; 3099 ieee80211_free_node(ni); 3100 return ENOMEM; 3101 } 3102 } 3103 3104 static int 3105 ht_send_action_ba_delba(struct ieee80211_node *ni, 3106 int category, int action, void *arg0) 3107 { 3108 struct ieee80211vap *vap = ni->ni_vap; 3109 struct ieee80211com *ic = ni->ni_ic; 3110 uint16_t *args = arg0; 3111 struct mbuf *m; 3112 uint16_t baparamset; 3113 uint8_t *frm; 3114 3115 baparamset = _IEEE80211_SHIFTMASK(args[0], IEEE80211_DELBAPS_TID) 3116 | args[1] 3117 ; 3118 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni, 3119 "send DELBA action: tid %d, initiator %d reason %d (%s)", 3120 args[0], args[1], args[2], ieee80211_reason_to_string(args[2])); 3121 3122 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 3123 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__, 3124 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1); 3125 ieee80211_ref_node(ni); 3126 3127 m = ieee80211_getmgtframe(&frm, 3128 ic->ic_headroom + sizeof(struct ieee80211_frame), 3129 sizeof(uint16_t) /* action+category */ 3130 /* XXX may action payload */ 3131 + sizeof(struct ieee80211_action_ba_addbaresponse) 3132 ); 3133 if (m != NULL) { 3134 *frm++ = category; 3135 *frm++ = action; 3136 ADDSHORT(frm, baparamset); 3137 ADDSHORT(frm, args[2]); /* reason code */ 3138 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 3139 return ht_action_output(ni, m); 3140 } else { 3141 vap->iv_stats.is_tx_nobuf++; 3142 ieee80211_free_node(ni); 3143 return ENOMEM; 3144 } 3145 } 3146 3147 static int 3148 ht_send_action_ht_txchwidth(struct ieee80211_node *ni, 3149 int category, int action, void *arg0) 3150 { 3151 struct ieee80211vap *vap = ni->ni_vap; 3152 struct ieee80211com *ic = ni->ni_ic; 3153 struct mbuf *m; 3154 uint8_t *frm; 3155 3156 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_11N, ni, 3157 "send HT txchwidth: width %d", 3158 IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 40 : 20); 3159 3160 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 3161 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__, 3162 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1); 3163 ieee80211_ref_node(ni); 3164 3165 m = ieee80211_getmgtframe(&frm, 3166 ic->ic_headroom + sizeof(struct ieee80211_frame), 3167 sizeof(uint16_t) /* action+category */ 3168 /* XXX may action payload */ 3169 + sizeof(struct ieee80211_action_ba_addbaresponse) 3170 ); 3171 if (m != NULL) { 3172 *frm++ = category; 3173 *frm++ = action; 3174 *frm++ = IEEE80211_IS_CHAN_HT40(ni->ni_chan) ? 3175 IEEE80211_A_HT_TXCHWIDTH_2040 : 3176 IEEE80211_A_HT_TXCHWIDTH_20; 3177 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 3178 return ht_action_output(ni, m); 3179 } else { 3180 vap->iv_stats.is_tx_nobuf++; 3181 ieee80211_free_node(ni); 3182 return ENOMEM; 3183 } 3184 } 3185 #undef ADDSHORT 3186 3187 /* 3188 * Construct the MCS bit mask for inclusion in an HT capabilities 3189 * information element. 3190 */ 3191 static void 3192 ieee80211_set_mcsset(struct ieee80211com *ic, uint8_t *frm) 3193 { 3194 int i; 3195 uint8_t txparams; 3196 3197 KASSERT((ic->ic_rxstream > 0 && ic->ic_rxstream <= 4), 3198 ("ic_rxstream %d out of range", ic->ic_rxstream)); 3199 KASSERT((ic->ic_txstream > 0 && ic->ic_txstream <= 4), 3200 ("ic_txstream %d out of range", ic->ic_txstream)); 3201 3202 for (i = 0; i < ic->ic_rxstream * 8; i++) 3203 setbit(frm, i); 3204 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) && 3205 (ic->ic_htcaps & IEEE80211_HTC_RXMCS32)) 3206 setbit(frm, 32); 3207 if (ic->ic_htcaps & IEEE80211_HTC_RXUNEQUAL) { 3208 if (ic->ic_rxstream >= 2) { 3209 for (i = 33; i <= 38; i++) 3210 setbit(frm, i); 3211 } 3212 if (ic->ic_rxstream >= 3) { 3213 for (i = 39; i <= 52; i++) 3214 setbit(frm, i); 3215 } 3216 if (ic->ic_rxstream >= 4) { 3217 for (i = 53; i <= 76; i++) 3218 setbit(frm, i); 3219 } 3220 } 3221 3222 txparams = 0x1; /* TX MCS set defined */ 3223 if (ic->ic_rxstream != ic->ic_txstream) { 3224 txparams |= 0x2; /* TX RX MCS not equal */ 3225 txparams |= (ic->ic_txstream - 1) << 2; /* num TX streams */ 3226 if (ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) 3227 txparams |= 0x16; /* TX unequal modulation sup */ 3228 } 3229 3230 frm[12] = txparams; 3231 } 3232 3233 /* 3234 * Add body of an HTCAP information element. 3235 */ 3236 static uint8_t * 3237 ieee80211_add_htcap_body(uint8_t *frm, struct ieee80211_node *ni) 3238 { 3239 #define ADDSHORT(frm, v) do { \ 3240 frm[0] = (v) & 0xff; \ 3241 frm[1] = (v) >> 8; \ 3242 frm += 2; \ 3243 } while (0) 3244 struct ieee80211com *ic = ni->ni_ic; 3245 struct ieee80211vap *vap = ni->ni_vap; 3246 uint16_t caps, extcaps; 3247 int rxmax, density; 3248 3249 /* HT capabilities */ 3250 caps = vap->iv_htcaps & 0xffff; 3251 /* 3252 * Note channel width depends on whether we are operating as 3253 * a sta or not. When operating as a sta we are generating 3254 * a request based on our desired configuration. Otherwise 3255 * we are operational and the channel attributes identify 3256 * how we've been setup (which might be different if a fixed 3257 * channel is specified). 3258 */ 3259 if (vap->iv_opmode == IEEE80211_M_STA) { 3260 /* override 20/40 use based on config */ 3261 if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40) 3262 caps |= IEEE80211_HTCAP_CHWIDTH40; 3263 else 3264 caps &= ~IEEE80211_HTCAP_CHWIDTH40; 3265 3266 /* Start by using the advertised settings */ 3267 rxmax = _IEEE80211_MASKSHIFT(ni->ni_htparam, 3268 IEEE80211_HTCAP_MAXRXAMPDU); 3269 density = _IEEE80211_MASKSHIFT(ni->ni_htparam, 3270 IEEE80211_HTCAP_MPDUDENSITY); 3271 3272 IEEE80211_DPRINTF(vap, IEEE80211_MSG_11N, 3273 "%s: advertised rxmax=%d, density=%d, vap rxmax=%d, density=%d\n", 3274 __func__, 3275 rxmax, 3276 density, 3277 vap->iv_ampdu_rxmax, 3278 vap->iv_ampdu_density); 3279 3280 /* Cap at VAP rxmax */ 3281 if (rxmax > vap->iv_ampdu_rxmax) 3282 rxmax = vap->iv_ampdu_rxmax; 3283 3284 /* 3285 * If the VAP ampdu density value greater, use that. 3286 * 3287 * (Larger density value == larger minimum gap between A-MPDU 3288 * subframes.) 3289 */ 3290 if (vap->iv_ampdu_density > density) 3291 density = vap->iv_ampdu_density; 3292 3293 /* 3294 * NB: Hardware might support HT40 on some but not all 3295 * channels. We can't determine this earlier because only 3296 * after association the channel is upgraded to HT based 3297 * on the negotiated capabilities. 3298 */ 3299 if (ni->ni_chan != IEEE80211_CHAN_ANYC && 3300 findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40U) == NULL && 3301 findhtchan(ic, ni->ni_chan, IEEE80211_CHAN_HT40D) == NULL) 3302 caps &= ~IEEE80211_HTCAP_CHWIDTH40; 3303 } else { 3304 /* override 20/40 use based on current channel */ 3305 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) 3306 caps |= IEEE80211_HTCAP_CHWIDTH40; 3307 else 3308 caps &= ~IEEE80211_HTCAP_CHWIDTH40; 3309 3310 /* XXX TODO should it start by using advertised settings? */ 3311 rxmax = vap->iv_ampdu_rxmax; 3312 density = vap->iv_ampdu_density; 3313 } 3314 3315 /* adjust short GI based on channel and config */ 3316 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0) 3317 caps &= ~IEEE80211_HTCAP_SHORTGI20; 3318 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 || 3319 (caps & IEEE80211_HTCAP_CHWIDTH40) == 0) 3320 caps &= ~IEEE80211_HTCAP_SHORTGI40; 3321 3322 /* adjust STBC based on receive capabilities */ 3323 if ((vap->iv_flags_ht & IEEE80211_FHT_STBC_RX) == 0) 3324 caps &= ~IEEE80211_HTCAP_RXSTBC; 3325 3326 /* adjust LDPC based on receive capabilites */ 3327 if ((vap->iv_flags_ht & IEEE80211_FHT_LDPC_RX) == 0) 3328 caps &= ~IEEE80211_HTCAP_LDPC; 3329 3330 ADDSHORT(frm, caps); 3331 3332 /* HT parameters */ 3333 *frm = _IEEE80211_SHIFTMASK(rxmax, IEEE80211_HTCAP_MAXRXAMPDU) 3334 | _IEEE80211_SHIFTMASK(density, IEEE80211_HTCAP_MPDUDENSITY) 3335 ; 3336 frm++; 3337 3338 /* pre-zero remainder of ie */ 3339 memset(frm, 0, sizeof(struct ieee80211_ie_htcap) - 3340 __offsetof(struct ieee80211_ie_htcap, hc_mcsset)); 3341 3342 /* supported MCS set */ 3343 /* 3344 * XXX: For sta mode the rate set should be restricted based 3345 * on the AP's capabilities, but ni_htrates isn't setup when 3346 * we're called to form an AssocReq frame so for now we're 3347 * restricted to the device capabilities. 3348 */ 3349 ieee80211_set_mcsset(ni->ni_ic, frm); 3350 3351 frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) - 3352 __offsetof(struct ieee80211_ie_htcap, hc_mcsset); 3353 3354 /* HT extended capabilities */ 3355 extcaps = vap->iv_htextcaps & 0xffff; 3356 3357 ADDSHORT(frm, extcaps); 3358 3359 frm += sizeof(struct ieee80211_ie_htcap) - 3360 __offsetof(struct ieee80211_ie_htcap, hc_txbf); 3361 3362 return frm; 3363 #undef ADDSHORT 3364 } 3365 3366 /* 3367 * Add 802.11n HT capabilities information element 3368 */ 3369 uint8_t * 3370 ieee80211_add_htcap(uint8_t *frm, struct ieee80211_node *ni) 3371 { 3372 frm[0] = IEEE80211_ELEMID_HTCAP; 3373 frm[1] = sizeof(struct ieee80211_ie_htcap) - 2; 3374 return ieee80211_add_htcap_body(frm + 2, ni); 3375 } 3376 3377 /* 3378 * Non-associated probe request - add HT capabilities based on 3379 * the current channel configuration. 3380 */ 3381 static uint8_t * 3382 ieee80211_add_htcap_body_ch(uint8_t *frm, struct ieee80211vap *vap, 3383 struct ieee80211_channel *c) 3384 { 3385 #define ADDSHORT(frm, v) do { \ 3386 frm[0] = (v) & 0xff; \ 3387 frm[1] = (v) >> 8; \ 3388 frm += 2; \ 3389 } while (0) 3390 struct ieee80211com *ic = vap->iv_ic; 3391 uint16_t caps, extcaps; 3392 int rxmax, density; 3393 3394 /* HT capabilities */ 3395 caps = vap->iv_htcaps & 0xffff; 3396 3397 /* 3398 * We don't use this in STA mode; only in IBSS mode. 3399 * So in IBSS mode we base our HTCAP flags on the 3400 * given channel. 3401 */ 3402 3403 /* override 20/40 use based on current channel */ 3404 if (IEEE80211_IS_CHAN_HT40(c)) 3405 caps |= IEEE80211_HTCAP_CHWIDTH40; 3406 else 3407 caps &= ~IEEE80211_HTCAP_CHWIDTH40; 3408 3409 /* Use the currently configured values */ 3410 rxmax = vap->iv_ampdu_rxmax; 3411 density = vap->iv_ampdu_density; 3412 3413 /* adjust short GI based on channel and config */ 3414 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) == 0) 3415 caps &= ~IEEE80211_HTCAP_SHORTGI20; 3416 if ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) == 0 || 3417 (caps & IEEE80211_HTCAP_CHWIDTH40) == 0) 3418 caps &= ~IEEE80211_HTCAP_SHORTGI40; 3419 ADDSHORT(frm, caps); 3420 3421 /* HT parameters */ 3422 *frm = _IEEE80211_SHIFTMASK(rxmax, IEEE80211_HTCAP_MAXRXAMPDU) 3423 | _IEEE80211_SHIFTMASK(density, IEEE80211_HTCAP_MPDUDENSITY) 3424 ; 3425 frm++; 3426 3427 /* pre-zero remainder of ie */ 3428 memset(frm, 0, sizeof(struct ieee80211_ie_htcap) - 3429 __offsetof(struct ieee80211_ie_htcap, hc_mcsset)); 3430 3431 /* supported MCS set */ 3432 /* 3433 * XXX: For sta mode the rate set should be restricted based 3434 * on the AP's capabilities, but ni_htrates isn't setup when 3435 * we're called to form an AssocReq frame so for now we're 3436 * restricted to the device capabilities. 3437 */ 3438 ieee80211_set_mcsset(ic, frm); 3439 3440 frm += __offsetof(struct ieee80211_ie_htcap, hc_extcap) - 3441 __offsetof(struct ieee80211_ie_htcap, hc_mcsset); 3442 3443 /* HT extended capabilities */ 3444 extcaps = vap->iv_htextcaps & 0xffff; 3445 3446 ADDSHORT(frm, extcaps); 3447 3448 frm += sizeof(struct ieee80211_ie_htcap) - 3449 __offsetof(struct ieee80211_ie_htcap, hc_txbf); 3450 3451 return frm; 3452 #undef ADDSHORT 3453 } 3454 3455 /* 3456 * Add 802.11n HT capabilities information element 3457 */ 3458 uint8_t * 3459 ieee80211_add_htcap_ch(uint8_t *frm, struct ieee80211vap *vap, 3460 struct ieee80211_channel *c) 3461 { 3462 frm[0] = IEEE80211_ELEMID_HTCAP; 3463 frm[1] = sizeof(struct ieee80211_ie_htcap) - 2; 3464 return ieee80211_add_htcap_body_ch(frm + 2, vap, c); 3465 } 3466 3467 /* 3468 * Add Broadcom OUI wrapped standard HTCAP ie; this is 3469 * used for compatibility w/ pre-draft implementations. 3470 */ 3471 uint8_t * 3472 ieee80211_add_htcap_vendor(uint8_t *frm, struct ieee80211_node *ni) 3473 { 3474 frm[0] = IEEE80211_ELEMID_VENDOR; 3475 frm[1] = 4 + sizeof(struct ieee80211_ie_htcap) - 2; 3476 frm[2] = (BCM_OUI >> 0) & 0xff; 3477 frm[3] = (BCM_OUI >> 8) & 0xff; 3478 frm[4] = (BCM_OUI >> 16) & 0xff; 3479 frm[5] = BCM_OUI_HTCAP; 3480 return ieee80211_add_htcap_body(frm + 6, ni); 3481 } 3482 3483 /* 3484 * Construct the MCS bit mask of basic rates 3485 * for inclusion in an HT information element. 3486 */ 3487 static void 3488 ieee80211_set_basic_htrates(uint8_t *frm, const struct ieee80211_htrateset *rs) 3489 { 3490 int i; 3491 3492 for (i = 0; i < rs->rs_nrates; i++) { 3493 int r = rs->rs_rates[i] & IEEE80211_RATE_VAL; 3494 if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) && 3495 r < IEEE80211_HTRATE_MAXSIZE) { 3496 /* NB: this assumes a particular implementation */ 3497 setbit(frm, r); 3498 } 3499 } 3500 } 3501 3502 /* 3503 * Update the HTINFO ie for a beacon frame. 3504 */ 3505 void 3506 ieee80211_ht_update_beacon(struct ieee80211vap *vap, 3507 struct ieee80211_beacon_offsets *bo) 3508 { 3509 #define PROTMODE (IEEE80211_HTINFO_OPMODE|IEEE80211_HTINFO_NONHT_PRESENT) 3510 struct ieee80211_node *ni; 3511 const struct ieee80211_channel *bsschan; 3512 struct ieee80211com *ic = vap->iv_ic; 3513 struct ieee80211_ie_htinfo *ht = 3514 (struct ieee80211_ie_htinfo *) bo->bo_htinfo; 3515 3516 ni = ieee80211_ref_node(vap->iv_bss); 3517 bsschan = ni->ni_chan; 3518 3519 /* XXX only update on channel change */ 3520 ht->hi_ctrlchannel = ieee80211_chan2ieee(ic, bsschan); 3521 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS) 3522 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PERM; 3523 else 3524 ht->hi_byte1 = IEEE80211_HTINFO_RIFSMODE_PROH; 3525 if (IEEE80211_IS_CHAN_HT40U(bsschan)) 3526 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_ABOVE; 3527 else if (IEEE80211_IS_CHAN_HT40D(bsschan)) 3528 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_BELOW; 3529 else 3530 ht->hi_byte1 |= IEEE80211_HTINFO_2NDCHAN_NONE; 3531 if (IEEE80211_IS_CHAN_HT40(bsschan)) 3532 ht->hi_byte1 |= IEEE80211_HTINFO_TXWIDTH_2040; 3533 3534 /* protection mode */ 3535 /* 3536 * XXX TODO: this uses the global flag, not the per-VAP flag. 3537 * Eventually (once the protection modes are done per-channel 3538 * rather than per-VAP) we can flip this over to be per-VAP but 3539 * using the channel protection mode. 3540 */ 3541 ht->hi_byte2 = (ht->hi_byte2 &~ PROTMODE) | ic->ic_curhtprotmode; 3542 3543 ieee80211_free_node(ni); 3544 3545 /* XXX propagate to vendor ie's */ 3546 #undef PROTMODE 3547 } 3548 3549 /* 3550 * Add body of an HTINFO information element. 3551 * 3552 * NB: We don't use struct ieee80211_ie_htinfo because we can 3553 * be called to fillin both a standard ie and a compat ie that 3554 * has a vendor OUI at the front. 3555 */ 3556 static uint8_t * 3557 ieee80211_add_htinfo_body(uint8_t *frm, struct ieee80211_node *ni) 3558 { 3559 struct ieee80211vap *vap = ni->ni_vap; 3560 struct ieee80211com *ic = ni->ni_ic; 3561 3562 /* pre-zero remainder of ie */ 3563 memset(frm, 0, sizeof(struct ieee80211_ie_htinfo) - 2); 3564 3565 /* primary/control channel center */ 3566 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan); 3567 3568 if (vap->iv_flags_ht & IEEE80211_FHT_RIFS) 3569 frm[0] = IEEE80211_HTINFO_RIFSMODE_PERM; 3570 else 3571 frm[0] = IEEE80211_HTINFO_RIFSMODE_PROH; 3572 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan)) 3573 frm[0] |= IEEE80211_HTINFO_2NDCHAN_ABOVE; 3574 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan)) 3575 frm[0] |= IEEE80211_HTINFO_2NDCHAN_BELOW; 3576 else 3577 frm[0] |= IEEE80211_HTINFO_2NDCHAN_NONE; 3578 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) 3579 frm[0] |= IEEE80211_HTINFO_TXWIDTH_2040; 3580 3581 /* 3582 * Add current protection mode. Unlike for beacons, 3583 * this will respect the per-VAP flags. 3584 */ 3585 frm[1] = vap->iv_curhtprotmode; 3586 3587 frm += 5; 3588 3589 /* basic MCS set */ 3590 ieee80211_set_basic_htrates(frm, &ni->ni_htrates); 3591 frm += sizeof(struct ieee80211_ie_htinfo) - 3592 __offsetof(struct ieee80211_ie_htinfo, hi_basicmcsset); 3593 return frm; 3594 } 3595 3596 /* 3597 * Add 802.11n HT information element. 3598 */ 3599 uint8_t * 3600 ieee80211_add_htinfo(uint8_t *frm, struct ieee80211_node *ni) 3601 { 3602 frm[0] = IEEE80211_ELEMID_HTINFO; 3603 frm[1] = sizeof(struct ieee80211_ie_htinfo) - 2; 3604 return ieee80211_add_htinfo_body(frm + 2, ni); 3605 } 3606 3607 /* 3608 * Add Broadcom OUI wrapped standard HTINFO ie; this is 3609 * used for compatibility w/ pre-draft implementations. 3610 */ 3611 uint8_t * 3612 ieee80211_add_htinfo_vendor(uint8_t *frm, struct ieee80211_node *ni) 3613 { 3614 frm[0] = IEEE80211_ELEMID_VENDOR; 3615 frm[1] = 4 + sizeof(struct ieee80211_ie_htinfo) - 2; 3616 frm[2] = (BCM_OUI >> 0) & 0xff; 3617 frm[3] = (BCM_OUI >> 8) & 0xff; 3618 frm[4] = (BCM_OUI >> 16) & 0xff; 3619 frm[5] = BCM_OUI_HTINFO; 3620 return ieee80211_add_htinfo_body(frm + 6, ni); 3621 } 3622 3623 /* 3624 * Get the HT density for the given 802.11n node. 3625 * 3626 * Take into account the density advertised from the peer. 3627 * Larger values are longer A-MPDU density spacing values, and 3628 * we want to obey them per station if we get them. 3629 */ 3630 int 3631 ieee80211_ht_get_node_ampdu_density(const struct ieee80211_node *ni) 3632 { 3633 struct ieee80211vap *vap; 3634 int peer_mpdudensity; 3635 3636 vap = ni->ni_vap; 3637 peer_mpdudensity = 3638 _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY); 3639 if (vap->iv_ampdu_density > peer_mpdudensity) 3640 peer_mpdudensity = vap->iv_ampdu_density; 3641 return (peer_mpdudensity); 3642 } 3643 3644 /* 3645 * Get the transmit A-MPDU limit for the given 802.11n node. 3646 * 3647 * Take into account the limit advertised from the peer. 3648 * Smaller values indicate smaller maximum A-MPDU sizes, and 3649 * should be used when forming an A-MPDU to the given peer. 3650 */ 3651 int 3652 ieee80211_ht_get_node_ampdu_limit(const struct ieee80211_node *ni) 3653 { 3654 struct ieee80211vap *vap; 3655 int peer_mpdulimit; 3656 3657 vap = ni->ni_vap; 3658 peer_mpdulimit = 3659 _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU); 3660 3661 return (MIN(vap->iv_ampdu_limit, peer_mpdulimit)); 3662 } 3663 3664 /* 3665 * Return true if short-GI is available when transmitting to 3666 * the given node at 20MHz. 3667 * 3668 * Ensure it's configured and available in the VAP / driver as 3669 * well as the node. 3670 */ 3671 bool 3672 ieee80211_ht_check_tx_shortgi_20(const struct ieee80211_node *ni) 3673 { 3674 const struct ieee80211vap *vap; 3675 const struct ieee80211com *ic; 3676 3677 if (! ieee80211_ht_check_tx_ht(ni)) 3678 return (false); 3679 3680 vap = ni->ni_vap; 3681 ic = ni->ni_ic; 3682 3683 return ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20) && 3684 (ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20) && 3685 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)); 3686 } 3687 3688 /* 3689 * Return true if short-GI is available when transmitting to 3690 * the given node at 40MHz. 3691 * 3692 * Ensure it's configured and available in the VAP / driver as 3693 * well as the node and BSS. 3694 */ 3695 bool 3696 ieee80211_ht_check_tx_shortgi_40(const struct ieee80211_node *ni) 3697 { 3698 const struct ieee80211vap *vap; 3699 const struct ieee80211com *ic; 3700 3701 if (! ieee80211_ht_check_tx_ht40(ni)) 3702 return (false); 3703 3704 vap = ni->ni_vap; 3705 ic = ni->ni_ic; 3706 3707 return ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40) && 3708 (ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) && 3709 (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)); 3710 } 3711 3712 /* 3713 * Return true if HT rates can be used for the given node. 3714 * 3715 * There are some situations seen in the wild, wild past where 3716 * HT APs would announce HT but no HT rates. 3717 */ 3718 bool 3719 ieee80211_ht_check_tx_ht(const struct ieee80211_node *ni) 3720 { 3721 const struct ieee80211vap *vap; 3722 const struct ieee80211_channel *bss_chan; 3723 3724 if (ni == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC || 3725 ni->ni_vap == NULL || ni->ni_vap->iv_bss == NULL) 3726 return (false); 3727 3728 vap = ni->ni_vap; 3729 bss_chan = vap->iv_bss->ni_chan; 3730 3731 if (bss_chan == IEEE80211_CHAN_ANYC) 3732 return (false); 3733 3734 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && 3735 ni->ni_htrates.rs_nrates == 0) 3736 return (false); 3737 return (IEEE80211_IS_CHAN_HT(ni->ni_chan)); 3738 } 3739 3740 /* 3741 * Return true if HT40 rates can be transmitted to the given node. 3742 * 3743 * This verifies that the BSS is HT40 capable and the current 3744 * node channel width is 40MHz. 3745 */ 3746 bool 3747 ieee80211_ht_check_tx_ht40(const struct ieee80211_node *ni) 3748 { 3749 struct ieee80211vap *vap; 3750 struct ieee80211_channel *bss_chan; 3751 3752 if (! ieee80211_ht_check_tx_ht(ni)) 3753 return (false); 3754 3755 vap = ni->ni_vap; 3756 bss_chan = vap->iv_bss->ni_chan; 3757 3758 return (IEEE80211_IS_CHAN_HT40(bss_chan) && 3759 IEEE80211_IS_CHAN_HT40(ni->ni_chan) && 3760 (ni->ni_chw == IEEE80211_STA_RX_BW_40)); 3761 } 3762