1 /*- 2 * Copyright (c) 2017 Adrian Chadd <adrian@FreeBSD.org> 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.11ac-2013 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 #include <net80211/ieee80211_vht.h> 55 56 /* define here, used throughout file */ 57 #define MS(_v, _f) (((_v) & _f) >> _f##_S) 58 #define SM(_v, _f) (((_v) << _f##_S) & _f) 59 60 #define ADDSHORT(frm, v) do { \ 61 frm[0] = (v) & 0xff; \ 62 frm[1] = (v) >> 8; \ 63 frm += 2; \ 64 } while (0) 65 #define ADDWORD(frm, v) do { \ 66 frm[0] = (v) & 0xff; \ 67 frm[1] = ((v) >> 8) & 0xff; \ 68 frm[2] = ((v) >> 16) & 0xff; \ 69 frm[3] = ((v) >> 24) & 0xff; \ 70 frm += 4; \ 71 } while (0) 72 73 /* 74 * Immediate TODO: 75 * 76 * + handle WLAN_ACTION_VHT_OPMODE_NOTIF and other VHT action frames 77 * + ensure vhtinfo/vhtcap parameters correctly use the negotiated 78 * capabilities and ratesets 79 * + group ID management operation 80 */ 81 82 /* 83 * XXX TODO: handle WLAN_ACTION_VHT_OPMODE_NOTIF 84 * 85 * Look at mac80211/vht.c:ieee80211_vht_handle_opmode() for further details. 86 */ 87 88 static int 89 vht_recv_action_placeholder(struct ieee80211_node *ni, 90 const struct ieee80211_frame *wh, 91 const uint8_t *frm, const uint8_t *efrm) 92 { 93 94 ieee80211_note(ni->ni_vap, "%s: called; fc=0x%.2x/0x%.2x", 95 __func__, 96 wh->i_fc[0], 97 wh->i_fc[1]); 98 99 return (0); 100 } 101 102 static int 103 vht_send_action_placeholder(struct ieee80211_node *ni, 104 int category, int action, void *arg0) 105 { 106 107 ieee80211_note(ni->ni_vap, "%s: called; category=%d, action=%d", 108 __func__, 109 category, 110 action); 111 return (EINVAL); 112 } 113 114 static void 115 ieee80211_vht_init(void) 116 { 117 118 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_VHT, 119 WLAN_ACTION_VHT_COMPRESSED_BF, vht_recv_action_placeholder); 120 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_VHT, 121 WLAN_ACTION_VHT_GROUPID_MGMT, vht_recv_action_placeholder); 122 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_VHT, 123 WLAN_ACTION_VHT_OPMODE_NOTIF, vht_recv_action_placeholder); 124 125 ieee80211_send_action_register(IEEE80211_ACTION_CAT_VHT, 126 WLAN_ACTION_VHT_COMPRESSED_BF, vht_send_action_placeholder); 127 ieee80211_send_action_register(IEEE80211_ACTION_CAT_VHT, 128 WLAN_ACTION_VHT_GROUPID_MGMT, vht_send_action_placeholder); 129 ieee80211_send_action_register(IEEE80211_ACTION_CAT_VHT, 130 WLAN_ACTION_VHT_OPMODE_NOTIF, vht_send_action_placeholder); 131 } 132 133 SYSINIT(wlan_vht, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_vht_init, NULL); 134 135 void 136 ieee80211_vht_attach(struct ieee80211com *ic) 137 { 138 } 139 140 void 141 ieee80211_vht_detach(struct ieee80211com *ic) 142 { 143 } 144 145 void 146 ieee80211_vht_vattach(struct ieee80211vap *vap) 147 { 148 struct ieee80211com *ic = vap->iv_ic; 149 150 if (! IEEE80211_CONF_VHT(ic)) 151 return; 152 153 vap->iv_vhtcaps = ic->ic_vhtcaps; 154 vap->iv_vhtextcaps = ic->ic_vhtextcaps; 155 156 /* XXX assume VHT80 support; should really check vhtcaps */ 157 vap->iv_flags_vht = 158 IEEE80211_FVHT_VHT 159 | IEEE80211_FVHT_USEVHT40 160 | IEEE80211_FVHT_USEVHT80; 161 /* XXX TODO: enable VHT80+80, VHT160 capabilities */ 162 163 memcpy(&vap->iv_vht_mcsinfo, &ic->ic_vht_mcsinfo, 164 sizeof(struct ieee80211_vht_mcs_info)); 165 } 166 167 void 168 ieee80211_vht_vdetach(struct ieee80211vap *vap) 169 { 170 } 171 172 #if 0 173 static void 174 vht_announce(struct ieee80211com *ic, enum ieee80211_phymode mode) 175 { 176 } 177 #endif 178 179 static int 180 vht_mcs_to_num(int m) 181 { 182 183 switch (m) { 184 case IEEE80211_VHT_MCS_SUPPORT_0_7: 185 return (7); 186 case IEEE80211_VHT_MCS_SUPPORT_0_8: 187 return (8); 188 case IEEE80211_VHT_MCS_SUPPORT_0_9: 189 return (9); 190 default: 191 return (0); 192 } 193 } 194 195 void 196 ieee80211_vht_announce(struct ieee80211com *ic) 197 { 198 int i, tx, rx; 199 200 if (! IEEE80211_CONF_VHT(ic)) 201 return; 202 203 /* Channel width */ 204 ic_printf(ic, "[VHT] Channel Widths: 20MHz, 40MHz, 80MHz"); 205 if (MS(ic->ic_vhtcaps, IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK) == 2) 206 printf(" 80+80MHz"); 207 if (MS(ic->ic_vhtcaps, IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK) >= 1) 208 printf(" 160MHz"); 209 printf("\n"); 210 211 /* Features */ 212 ic_printf(ic, "[VHT] Features: %b\n", ic->ic_vhtcaps, 213 IEEE80211_VHTCAP_BITS); 214 215 /* For now, just 5GHz VHT. Worry about 2GHz VHT later */ 216 for (i = 0; i < 7; i++) { 217 /* Each stream is 2 bits */ 218 tx = (ic->ic_vht_mcsinfo.tx_mcs_map >> (2*i)) & 0x3; 219 rx = (ic->ic_vht_mcsinfo.rx_mcs_map >> (2*i)) & 0x3; 220 if (tx == 3 && rx == 3) 221 continue; 222 ic_printf(ic, "[VHT] NSS %d: TX MCS 0..%d, RX MCS 0..%d\n", 223 i + 1, 224 vht_mcs_to_num(tx), 225 vht_mcs_to_num(rx)); 226 } 227 } 228 229 void 230 ieee80211_vht_node_init(struct ieee80211_node *ni) 231 { 232 233 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni, 234 "%s: called", __func__); 235 ni->ni_flags |= IEEE80211_NODE_VHT; 236 } 237 238 void 239 ieee80211_vht_node_cleanup(struct ieee80211_node *ni) 240 { 241 242 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni, 243 "%s: called", __func__); 244 ni->ni_flags &= ~IEEE80211_NODE_VHT; 245 ni->ni_vhtcap = 0; 246 bzero(&ni->ni_vht_mcsinfo, sizeof(struct ieee80211_vht_mcs_info)); 247 } 248 249 /* 250 * Parse an 802.11ac VHT operation IE. 251 */ 252 void 253 ieee80211_parse_vhtopmode(struct ieee80211_node *ni, const uint8_t *ie) 254 { 255 /* vht operation */ 256 ni->ni_vht_chanwidth = ie[2]; 257 ni->ni_vht_chan1 = ie[3]; 258 ni->ni_vht_chan2 = ie[4]; 259 ni->ni_vht_basicmcs = le16dec(ie + 5); 260 261 #if 0 262 printf("%s: chan1=%d, chan2=%d, chanwidth=%d, basicmcs=0x%04x\n", 263 __func__, 264 ni->ni_vht_chan1, 265 ni->ni_vht_chan2, 266 ni->ni_vht_chanwidth, 267 ni->ni_vht_basicmcs); 268 #endif 269 } 270 271 /* 272 * Parse an 802.11ac VHT capability IE. 273 */ 274 void 275 ieee80211_parse_vhtcap(struct ieee80211_node *ni, const uint8_t *ie) 276 { 277 278 /* vht capability */ 279 ni->ni_vhtcap = le32dec(ie + 2); 280 281 /* suppmcs */ 282 ni->ni_vht_mcsinfo.rx_mcs_map = le16dec(ie + 6); 283 ni->ni_vht_mcsinfo.rx_highest = le16dec(ie + 8); 284 ni->ni_vht_mcsinfo.tx_mcs_map = le16dec(ie + 10); 285 ni->ni_vht_mcsinfo.tx_highest = le16dec(ie + 12); 286 } 287 288 int 289 ieee80211_vht_updateparams(struct ieee80211_node *ni, 290 const uint8_t *vhtcap_ie, 291 const uint8_t *vhtop_ie) 292 { 293 294 //printf("%s: called\n", __func__); 295 296 ieee80211_parse_vhtcap(ni, vhtcap_ie); 297 ieee80211_parse_vhtopmode(ni, vhtop_ie); 298 return (0); 299 } 300 301 void 302 ieee80211_setup_vht_rates(struct ieee80211_node *ni, 303 const uint8_t *vhtcap_ie, 304 const uint8_t *vhtop_ie) 305 { 306 307 //printf("%s: called\n", __func__); 308 /* XXX TODO */ 309 } 310 311 void 312 ieee80211_vht_timeout(struct ieee80211com *ic) 313 { 314 } 315 316 void 317 ieee80211_vht_node_join(struct ieee80211_node *ni) 318 { 319 320 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni, 321 "%s: called", __func__); 322 } 323 324 void 325 ieee80211_vht_node_leave(struct ieee80211_node *ni) 326 { 327 328 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_11N, ni, 329 "%s: called", __func__); 330 } 331 332 /* 333 * Calculate the VHTCAP IE for a given node. 334 * 335 * This includes calculating the capability intersection based on the 336 * current operating mode and intersection of the TX/RX MCS maps. 337 * 338 * The standard only makes it clear about MCS rate negotiation 339 * and MCS basic rates (which must be a subset of the general 340 * negotiated rates). It doesn't make it clear that the AP should 341 * figure out the minimum functional overlap with the STA and 342 * support that. 343 * 344 * Note: this is in host order, not in 802.11 endian order. 345 * 346 * TODO: ensure I re-read 9.7.11 Rate Selection for VHT STAs. 347 * 348 * TODO: investigate what we should negotiate for MU-MIMO beamforming 349 * options. 350 * 351 * opmode is '1' for "vhtcap as if I'm a STA", 0 otherwise. 352 */ 353 void 354 ieee80211_vht_get_vhtcap_ie(struct ieee80211_node *ni, 355 struct ieee80211_ie_vhtcap *vhtcap, int opmode) 356 { 357 struct ieee80211vap *vap = ni->ni_vap; 358 // struct ieee80211com *ic = vap->iv_ic; 359 uint32_t val, val1, val2; 360 uint32_t new_vhtcap; 361 int i; 362 363 vhtcap->ie = IEEE80211_ELEMID_VHT_CAP; 364 vhtcap->len = sizeof(struct ieee80211_ie_vhtcap) - 2; 365 366 /* 367 * Capabilities - it depends on whether we are a station 368 * or not. 369 */ 370 new_vhtcap = 0; 371 372 /* 373 * Station - use our desired configuration based on 374 * local config, local device bits and the already-learnt 375 * vhtcap/vhtinfo IE in the node. 376 */ 377 378 /* Limit MPDU size to the smaller of the two */ 379 val2 = val1 = MS(vap->iv_vhtcaps, IEEE80211_VHTCAP_MAX_MPDU_MASK); 380 if (opmode == 1) { 381 val2 = MS(ni->ni_vhtcap, IEEE80211_VHTCAP_MAX_MPDU_MASK); 382 } 383 val = MIN(val1, val2); 384 new_vhtcap |= SM(val, IEEE80211_VHTCAP_MAX_MPDU_MASK); 385 386 /* Limit supp channel config */ 387 val2 = val1 = MS(vap->iv_vhtcaps, 388 IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK); 389 if (opmode == 1) { 390 val2 = MS(ni->ni_vhtcap, 391 IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK); 392 } 393 if ((val2 == 2) && 394 ((vap->iv_flags_vht & IEEE80211_FVHT_USEVHT80P80) == 0)) 395 val2 = 1; 396 if ((val2 == 1) && 397 ((vap->iv_flags_vht & IEEE80211_FVHT_USEVHT160) == 0)) 398 val2 = 0; 399 val = MIN(val1, val2); 400 new_vhtcap |= SM(val, IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_MASK); 401 402 /* RX LDPC */ 403 val2 = val1 = MS(vap->iv_vhtcaps, IEEE80211_VHTCAP_RXLDPC); 404 if (opmode == 1) { 405 val2 = MS(ni->ni_vhtcap, IEEE80211_VHTCAP_RXLDPC); 406 } 407 val = MIN(val1, val2); 408 new_vhtcap |= SM(val, IEEE80211_VHTCAP_RXLDPC); 409 410 /* Short-GI 80 */ 411 val2 = val1 = MS(vap->iv_vhtcaps, IEEE80211_VHTCAP_SHORT_GI_80); 412 if (opmode == 1) { 413 val2 = MS(ni->ni_vhtcap, IEEE80211_VHTCAP_SHORT_GI_80); 414 } 415 val = MIN(val1, val2); 416 new_vhtcap |= SM(val, IEEE80211_VHTCAP_SHORT_GI_80); 417 418 /* Short-GI 160 */ 419 val2 = val1 = MS(vap->iv_vhtcaps, IEEE80211_VHTCAP_SHORT_GI_160); 420 if (opmode == 1) { 421 val2 = MS(ni->ni_vhtcap, IEEE80211_VHTCAP_SHORT_GI_160); 422 } 423 val = MIN(val1, val2); 424 new_vhtcap |= SM(val, IEEE80211_VHTCAP_SHORT_GI_160); 425 426 /* 427 * STBC is slightly more complicated. 428 * 429 * In non-STA mode, we just announce our capabilities and that 430 * is that. 431 * 432 * In STA mode, we should calculate our capabilities based on 433 * local capabilities /and/ what the remote says. So: 434 * 435 * + Only TX STBC if we support it and the remote supports RX STBC; 436 * + Only announce RX STBC if we support it and the remote supports 437 * TX STBC; 438 * + RX STBC should be the minimum of local and remote RX STBC; 439 */ 440 441 /* TX STBC */ 442 val2 = val1 = MS(vap->iv_vhtcaps, IEEE80211_VHTCAP_TXSTBC); 443 if (opmode == 1) { 444 /* STA mode - enable it only if node RXSTBC is non-zero */ 445 val2 = !! MS(ni->ni_vhtcap, IEEE80211_VHTCAP_RXSTBC_MASK); 446 } 447 val = MIN(val1, val2); 448 /* XXX For now, use the 11n config flag */ 449 if ((vap->iv_flags_ht & IEEE80211_FHT_STBC_TX) == 0) 450 val = 0; 451 new_vhtcap |= SM(val, IEEE80211_VHTCAP_TXSTBC); 452 453 /* RX STBC1..4 */ 454 val2 = val1 = MS(vap->iv_vhtcaps, IEEE80211_VHTCAP_RXSTBC_MASK); 455 if (opmode == 1) { 456 /* STA mode - enable it only if node TXSTBC is non-zero */ 457 val2 = MS(ni->ni_vhtcap, IEEE80211_VHTCAP_TXSTBC); 458 } 459 val = MIN(val1, val2); 460 /* XXX For now, use the 11n config flag */ 461 if ((vap->iv_flags_ht & IEEE80211_FHT_STBC_RX) == 0) 462 val = 0; 463 new_vhtcap |= SM(val, IEEE80211_VHTCAP_RXSTBC_MASK); 464 465 /* 466 * Finally - if RXSTBC is 0, then don't enable TXSTBC. 467 * Strictly speaking a device can TXSTBC and not RXSTBC, but 468 * it would be silly. 469 */ 470 if (val == 0) 471 new_vhtcap &= ~IEEE80211_VHTCAP_TXSTBC; 472 473 /* 474 * Some of these fields require other fields to exist. 475 * So before using it, the parent field needs to be checked 476 * otherwise the overridden value may be wrong. 477 * 478 * For example, if SU beamformee is set to 0, then BF STS 479 * needs to be 0. 480 */ 481 482 /* SU Beamformer capable */ 483 val2 = val1 = MS(vap->iv_vhtcaps, 484 IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE); 485 if (opmode == 1) { 486 val2 = MS(ni->ni_vhtcap, 487 IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE); 488 } 489 val = MIN(val1, val2); 490 new_vhtcap |= SM(val, IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE); 491 492 /* SU Beamformee capable */ 493 val2 = val1 = MS(vap->iv_vhtcaps, 494 IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE); 495 if (opmode == 1) { 496 val2 = MS(ni->ni_vhtcap, 497 IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE); 498 } 499 val = MIN(val1, val2); 500 new_vhtcap |= SM(val, IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE); 501 502 /* Beamformee STS capability - only if SU beamformee capable */ 503 val2 = val1 = MS(vap->iv_vhtcaps, IEEE80211_VHTCAP_BEAMFORMEE_STS_MASK); 504 if (opmode == 1) { 505 val2 = MS(ni->ni_vhtcap, IEEE80211_VHTCAP_BEAMFORMEE_STS_MASK); 506 } 507 val = MIN(val1, val2); 508 if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE) == 0) 509 val = 0; 510 new_vhtcap |= SM(val, IEEE80211_VHTCAP_BEAMFORMEE_STS_MASK); 511 512 /* Sounding dimensions - only if SU beamformer capable */ 513 val2 = val1 = MS(vap->iv_vhtcaps, 514 IEEE80211_VHTCAP_SOUNDING_DIMENSIONS_MASK); 515 if (opmode == 1) 516 val2 = MS(ni->ni_vhtcap, 517 IEEE80211_VHTCAP_SOUNDING_DIMENSIONS_MASK); 518 val = MIN(val1, val2); 519 if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE) == 0) 520 val = 0; 521 new_vhtcap |= SM(val, IEEE80211_VHTCAP_SOUNDING_DIMENSIONS_MASK); 522 523 /* 524 * MU Beamformer capable - only if SU BFF capable, MU BFF capable 525 * and STA (not AP) 526 */ 527 val2 = val1 = MS(vap->iv_vhtcaps, 528 IEEE80211_VHTCAP_MU_BEAMFORMER_CAPABLE); 529 if (opmode == 1) 530 val2 = MS(ni->ni_vhtcap, 531 IEEE80211_VHTCAP_MU_BEAMFORMER_CAPABLE); 532 val = MIN(val1, val2); 533 if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE) == 0) 534 val = 0; 535 if (opmode != 1) /* Only enable for STA mode */ 536 val = 0; 537 new_vhtcap |= SM(val, IEEE80211_VHTCAP_SU_BEAMFORMER_CAPABLE); 538 539 /* 540 * MU Beamformee capable - only if SU BFE capable, MU BFE capable 541 * and AP (not STA) 542 */ 543 val2 = val1 = MS(vap->iv_vhtcaps, 544 IEEE80211_VHTCAP_MU_BEAMFORMEE_CAPABLE); 545 if (opmode == 1) 546 val2 = MS(ni->ni_vhtcap, 547 IEEE80211_VHTCAP_MU_BEAMFORMEE_CAPABLE); 548 val = MIN(val1, val2); 549 if ((new_vhtcap & IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE) == 0) 550 val = 0; 551 if (opmode != 0) /* Only enable for AP mode */ 552 val = 0; 553 new_vhtcap |= SM(val, IEEE80211_VHTCAP_SU_BEAMFORMEE_CAPABLE); 554 555 /* VHT TXOP PS */ 556 val2 = val1 = MS(vap->iv_vhtcaps, IEEE80211_VHTCAP_VHT_TXOP_PS); 557 if (opmode == 1) 558 val2 = MS(ni->ni_vhtcap, IEEE80211_VHTCAP_VHT_TXOP_PS); 559 val = MIN(val1, val2); 560 new_vhtcap |= SM(val, IEEE80211_VHTCAP_VHT_TXOP_PS); 561 562 /* HTC_VHT */ 563 val2 = val1 = MS(vap->iv_vhtcaps, IEEE80211_VHTCAP_HTC_VHT); 564 if (opmode == 1) 565 val2 = MS(ni->ni_vhtcap, IEEE80211_VHTCAP_HTC_VHT); 566 val = MIN(val1, val2); 567 new_vhtcap |= SM(val, IEEE80211_VHTCAP_HTC_VHT); 568 569 /* A-MPDU length max */ 570 /* XXX TODO: we need a userland config knob for this */ 571 val2 = val1 = MS(vap->iv_vhtcaps, 572 IEEE80211_VHTCAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK); 573 if (opmode == 1) 574 val2 = MS(ni->ni_vhtcap, 575 IEEE80211_VHTCAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK); 576 val = MIN(val1, val2); 577 new_vhtcap |= SM(val, IEEE80211_VHTCAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK); 578 579 /* 580 * Link adaptation is only valid if HTC-VHT capable is 1. 581 * Otherwise, always set it to 0. 582 */ 583 val2 = val1 = MS(vap->iv_vhtcaps, 584 IEEE80211_VHTCAP_VHT_LINK_ADAPTATION_VHT_MASK); 585 if (opmode == 1) 586 val2 = MS(ni->ni_vhtcap, 587 IEEE80211_VHTCAP_VHT_LINK_ADAPTATION_VHT_MASK); 588 val = MIN(val1, val2); 589 if ((new_vhtcap & IEEE80211_VHTCAP_HTC_VHT) == 0) 590 val = 0; 591 new_vhtcap |= SM(val, IEEE80211_VHTCAP_VHT_LINK_ADAPTATION_VHT_MASK); 592 593 /* 594 * The following two options are 0 if the pattern may change, 1 if it 595 * does not change. So, downgrade to the higher value. 596 */ 597 598 /* RX antenna pattern */ 599 val2 = val1 = MS(vap->iv_vhtcaps, IEEE80211_VHTCAP_RX_ANTENNA_PATTERN); 600 if (opmode == 1) 601 val2 = MS(ni->ni_vhtcap, IEEE80211_VHTCAP_RX_ANTENNA_PATTERN); 602 val = MAX(val1, val2); 603 new_vhtcap |= SM(val, IEEE80211_VHTCAP_RX_ANTENNA_PATTERN); 604 605 /* TX antenna pattern */ 606 val2 = val1 = MS(vap->iv_vhtcaps, IEEE80211_VHTCAP_TX_ANTENNA_PATTERN); 607 if (opmode == 1) 608 val2 = MS(ni->ni_vhtcap, IEEE80211_VHTCAP_TX_ANTENNA_PATTERN); 609 val = MAX(val1, val2); 610 new_vhtcap |= SM(val, IEEE80211_VHTCAP_TX_ANTENNA_PATTERN); 611 612 /* 613 * MCS set - again, we announce what we want to use 614 * based on configuration, device capabilities and 615 * already-learnt vhtcap/vhtinfo IE information. 616 */ 617 618 /* MCS set - start with whatever the device supports */ 619 vhtcap->supp_mcs.rx_mcs_map = vap->iv_vht_mcsinfo.rx_mcs_map; 620 vhtcap->supp_mcs.rx_highest = 0; 621 vhtcap->supp_mcs.tx_mcs_map = vap->iv_vht_mcsinfo.tx_mcs_map; 622 vhtcap->supp_mcs.tx_highest = 0; 623 624 vhtcap->vht_cap_info = new_vhtcap; 625 626 /* 627 * Now, if we're a STA, mask off whatever the AP doesn't support. 628 * Ie, we continue to state we can receive whatever we can do, 629 * but we only announce that we will transmit rates that meet 630 * the AP requirement. 631 * 632 * Note: 0 - MCS0..7; 1 - MCS0..8; 2 - MCS0..9; 3 = not supported. 633 * We can't just use MIN() because '3' means "no", so special case it. 634 */ 635 if (opmode) { 636 for (i = 0; i < 8; i++) { 637 val1 = (vhtcap->supp_mcs.tx_mcs_map >> (i*2)) & 0x3; 638 val2 = (ni->ni_vht_mcsinfo.tx_mcs_map >> (i*2)) & 0x3; 639 val = MIN(val1, val2); 640 if (val1 == 3 || val2 == 3) 641 val = 3; 642 vhtcap->supp_mcs.tx_mcs_map &= ~(0x3 << (i*2)); 643 vhtcap->supp_mcs.tx_mcs_map |= (val << (i*2)); 644 } 645 } 646 } 647 648 /* 649 * Add a VHTCAP field. 650 * 651 * If in station mode, we announce what we would like our 652 * desired configuration to be. 653 * 654 * Else, we announce our capabilities based on our current 655 * configuration. 656 */ 657 uint8_t * 658 ieee80211_add_vhtcap(uint8_t *frm, struct ieee80211_node *ni) 659 { 660 struct ieee80211_ie_vhtcap vhtcap; 661 int opmode; 662 663 opmode = 0; 664 if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) 665 opmode = 1; 666 667 ieee80211_vht_get_vhtcap_ie(ni, &vhtcap, opmode); 668 669 memset(frm, '\0', sizeof(struct ieee80211_ie_vhtcap)); 670 671 frm[0] = IEEE80211_ELEMID_VHT_CAP; 672 frm[1] = sizeof(struct ieee80211_ie_vhtcap) - 2; 673 frm += 2; 674 675 /* 32-bit VHT capability */ 676 ADDWORD(frm, vhtcap.vht_cap_info); 677 678 /* suppmcs */ 679 ADDSHORT(frm, vhtcap.supp_mcs.rx_mcs_map); 680 ADDSHORT(frm, vhtcap.supp_mcs.rx_highest); 681 ADDSHORT(frm, vhtcap.supp_mcs.tx_mcs_map); 682 ADDSHORT(frm, vhtcap.supp_mcs.tx_highest); 683 684 return (frm); 685 } 686 687 static uint8_t 688 ieee80211_vht_get_chwidth_ie(struct ieee80211_channel *c) 689 { 690 691 /* 692 * XXX TODO: look at the node configuration as 693 * well? 694 */ 695 696 if (IEEE80211_IS_CHAN_VHT160(c)) { 697 return IEEE80211_VHT_CHANWIDTH_160MHZ; 698 } 699 if (IEEE80211_IS_CHAN_VHT80_80(c)) { 700 return IEEE80211_VHT_CHANWIDTH_80P80MHZ; 701 } 702 if (IEEE80211_IS_CHAN_VHT80(c)) { 703 return IEEE80211_VHT_CHANWIDTH_80MHZ; 704 } 705 if (IEEE80211_IS_CHAN_VHT40(c)) { 706 return IEEE80211_VHT_CHANWIDTH_USE_HT; 707 } 708 if (IEEE80211_IS_CHAN_VHT20(c)) { 709 return IEEE80211_VHT_CHANWIDTH_USE_HT; 710 } 711 712 /* We shouldn't get here */ 713 printf("%s: called on a non-VHT channel (freq=%d, flags=0x%08x\n", 714 __func__, 715 (int) c->ic_freq, 716 c->ic_flags); 717 return IEEE80211_VHT_CHANWIDTH_USE_HT; 718 } 719 720 /* 721 * Note: this just uses the current channel information; 722 * it doesn't use the node info after parsing. 723 * 724 * XXX TODO: need to make the basic MCS set configurable. 725 * XXX TODO: read 802.11-2013 to determine what to set 726 * chwidth to when scanning. I have a feeling 727 * it isn't involved in scanning and we shouldn't 728 * be sending it; and I don't yet know what to set 729 * it to for IBSS or hostap where the peer may be 730 * a completely different channel width to us. 731 */ 732 uint8_t * 733 ieee80211_add_vhtinfo(uint8_t *frm, struct ieee80211_node *ni) 734 { 735 memset(frm, '\0', sizeof(struct ieee80211_ie_vht_operation)); 736 737 frm[0] = IEEE80211_ELEMID_VHT_OPMODE; 738 frm[1] = sizeof(struct ieee80211_ie_vht_operation) - 2; 739 frm += 2; 740 741 /* 8-bit chanwidth */ 742 *frm++ = ieee80211_vht_get_chwidth_ie(ni->ni_chan); 743 744 /* 8-bit freq1 */ 745 *frm++ = ni->ni_chan->ic_vht_ch_freq1; 746 747 /* 8-bit freq2 */ 748 *frm++ = ni->ni_chan->ic_vht_ch_freq2; 749 750 /* 16-bit basic MCS set - just MCS0..7 for NSS=1 for now */ 751 ADDSHORT(frm, 0xfffc); 752 753 return (frm); 754 } 755 756 void 757 ieee80211_vht_update_cap(struct ieee80211_node *ni, const uint8_t *vhtcap_ie, 758 const uint8_t *vhtop_ie) 759 { 760 761 ieee80211_parse_vhtcap(ni, vhtcap_ie); 762 ieee80211_parse_vhtopmode(ni, vhtop_ie); 763 } 764 765 static struct ieee80211_channel * 766 findvhtchan(struct ieee80211com *ic, struct ieee80211_channel *c, int vhtflags) 767 { 768 769 return (ieee80211_find_channel(ic, c->ic_freq, 770 (c->ic_flags & ~IEEE80211_CHAN_VHT) | vhtflags)); 771 } 772 773 /* 774 * Handle channel promotion to VHT, similar to ieee80211_ht_adjust_channel(). 775 */ 776 struct ieee80211_channel * 777 ieee80211_vht_adjust_channel(struct ieee80211com *ic, 778 struct ieee80211_channel *chan, int flags) 779 { 780 struct ieee80211_channel *c; 781 782 /* First case - handle channel demotion - if VHT isn't set */ 783 if ((flags & IEEE80211_FVHT_VHT) == 0) { 784 #if 0 785 printf("%s: demoting channel %d/0x%08x\n", __func__, 786 chan->ic_ieee, chan->ic_flags); 787 #endif 788 c = ieee80211_find_channel(ic, chan->ic_freq, 789 chan->ic_flags & ~IEEE80211_CHAN_VHT); 790 if (c == NULL) 791 c = chan; 792 #if 0 793 printf("%s: .. to %d/0x%08x\n", __func__, 794 c->ic_ieee, c->ic_flags); 795 #endif 796 return (c); 797 } 798 799 /* 800 * We can upgrade to VHT - attempt to do so 801 * 802 * Note: we don't clear the HT flags, these are the hints 803 * for HT40U/HT40D when selecting VHT40 or larger channels. 804 */ 805 /* Start with VHT80 */ 806 c = NULL; 807 if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT160)) 808 c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT80); 809 810 if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT80P80)) 811 c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT80_80); 812 813 if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT80)) 814 c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT80); 815 816 if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT40)) 817 c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT40U); 818 if ((c == NULL) && (flags & IEEE80211_FVHT_USEVHT40)) 819 c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT40D); 820 /* 821 * If we get here, VHT20 is always possible because we checked 822 * for IEEE80211_FVHT_VHT above. 823 */ 824 if (c == NULL) 825 c = findvhtchan(ic, chan, IEEE80211_CHAN_VHT20); 826 827 if (c != NULL) 828 chan = c; 829 830 #if 0 831 printf("%s: selected %d/0x%08x\n", __func__, c->ic_ieee, c->ic_flags); 832 #endif 833 return (chan); 834 } 835 836 /* 837 * Calculate the VHT operation IE for a given node. 838 * 839 * This includes calculating the suitable channel width/parameters 840 * and basic MCS set. 841 * 842 * TODO: ensure I read 9.7.11 Rate Selection for VHT STAs. 843 * TODO: ensure I read 10.39.7 - BSS Basic VHT-MCS and NSS set operation. 844 */ 845 void 846 ieee80211_vht_get_vhtinfo_ie(struct ieee80211_node *ni, 847 struct ieee80211_ie_vht_operation *vhtop, int opmode) 848 { 849 printf("%s: called; TODO!\n", __func__); 850 } 851