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