1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2001 Atsushi Onoe 5 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 /* 31 * IEEE 802.11 generic handler 32 */ 33 #include "opt_wlan.h" 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/malloc.h> 39 #include <sys/socket.h> 40 #include <sys/sbuf.h> 41 42 #include <machine/stdarg.h> 43 44 #include <net/if.h> 45 #include <net/if_var.h> 46 #include <net/if_dl.h> 47 #include <net/if_media.h> 48 #include <net/if_private.h> 49 #include <net/if_types.h> 50 #include <net/ethernet.h> 51 52 #include <net80211/ieee80211_var.h> 53 #include <net80211/ieee80211_regdomain.h> 54 #ifdef IEEE80211_SUPPORT_SUPERG 55 #include <net80211/ieee80211_superg.h> 56 #endif 57 #include <net80211/ieee80211_ratectl.h> 58 #include <net80211/ieee80211_vht.h> 59 60 #include <net/bpf.h> 61 62 const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = { 63 [IEEE80211_MODE_AUTO] = "auto", 64 [IEEE80211_MODE_11A] = "11a", 65 [IEEE80211_MODE_11B] = "11b", 66 [IEEE80211_MODE_11G] = "11g", 67 [IEEE80211_MODE_FH] = "FH", 68 [IEEE80211_MODE_TURBO_A] = "turboA", 69 [IEEE80211_MODE_TURBO_G] = "turboG", 70 [IEEE80211_MODE_STURBO_A] = "sturboA", 71 [IEEE80211_MODE_HALF] = "half", 72 [IEEE80211_MODE_QUARTER] = "quarter", 73 [IEEE80211_MODE_11NA] = "11na", 74 [IEEE80211_MODE_11NG] = "11ng", 75 [IEEE80211_MODE_VHT_2GHZ] = "11acg", 76 [IEEE80211_MODE_VHT_5GHZ] = "11ac", 77 }; 78 /* map ieee80211_opmode to the corresponding capability bit */ 79 const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = { 80 [IEEE80211_M_IBSS] = IEEE80211_C_IBSS, 81 [IEEE80211_M_WDS] = IEEE80211_C_WDS, 82 [IEEE80211_M_STA] = IEEE80211_C_STA, 83 [IEEE80211_M_AHDEMO] = IEEE80211_C_AHDEMO, 84 [IEEE80211_M_HOSTAP] = IEEE80211_C_HOSTAP, 85 [IEEE80211_M_MONITOR] = IEEE80211_C_MONITOR, 86 #ifdef IEEE80211_SUPPORT_MESH 87 [IEEE80211_M_MBSS] = IEEE80211_C_MBSS, 88 #endif 89 }; 90 91 const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] = 92 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 93 94 static void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag); 95 static void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag); 96 static void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag); 97 static void ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag); 98 static int ieee80211_media_setup(struct ieee80211com *ic, 99 struct ifmedia *media, int caps, int addsta, 100 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat); 101 static int media_status(enum ieee80211_opmode, 102 const struct ieee80211_channel *); 103 static uint64_t ieee80211_get_counter(struct ifnet *, ift_counter); 104 105 MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state"); 106 107 /* 108 * Default supported rates for 802.11 operation (in IEEE .5Mb units). 109 */ 110 #define B(r) ((r) | IEEE80211_RATE_BASIC) 111 static const struct ieee80211_rateset ieee80211_rateset_11a = 112 { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } }; 113 static const struct ieee80211_rateset ieee80211_rateset_half = 114 { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } }; 115 static const struct ieee80211_rateset ieee80211_rateset_quarter = 116 { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } }; 117 static const struct ieee80211_rateset ieee80211_rateset_11b = 118 { 4, { B(2), B(4), B(11), B(22) } }; 119 /* NB: OFDM rates are handled specially based on mode */ 120 static const struct ieee80211_rateset ieee80211_rateset_11g = 121 { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } }; 122 #undef B 123 124 static int set_vht_extchan(struct ieee80211_channel *c); 125 126 /* 127 * Fill in 802.11 available channel set, mark 128 * all available channels as active, and pick 129 * a default channel if not already specified. 130 */ 131 void 132 ieee80211_chan_init(struct ieee80211com *ic) 133 { 134 #define DEFAULTRATES(m, def) do { \ 135 if (ic->ic_sup_rates[m].rs_nrates == 0) \ 136 ic->ic_sup_rates[m] = def; \ 137 } while (0) 138 struct ieee80211_channel *c; 139 int i; 140 141 KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX, 142 ("invalid number of channels specified: %u", ic->ic_nchans)); 143 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail)); 144 memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps)); 145 setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO); 146 for (i = 0; i < ic->ic_nchans; i++) { 147 c = &ic->ic_channels[i]; 148 KASSERT(c->ic_flags != 0, ("channel with no flags")); 149 /* 150 * Help drivers that work only with frequencies by filling 151 * in IEEE channel #'s if not already calculated. Note this 152 * mimics similar work done in ieee80211_setregdomain when 153 * changing regulatory state. 154 */ 155 if (c->ic_ieee == 0) 156 c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags); 157 158 /* 159 * Setup the HT40/VHT40 upper/lower bits. 160 * The VHT80/... math is done elsewhere. 161 */ 162 if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0) 163 c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq + 164 (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20), 165 c->ic_flags); 166 167 /* Update VHT math */ 168 /* 169 * XXX VHT again, note that this assumes VHT80/... channels 170 * are legit already. 171 */ 172 set_vht_extchan(c); 173 174 /* default max tx power to max regulatory */ 175 if (c->ic_maxpower == 0) 176 c->ic_maxpower = 2*c->ic_maxregpower; 177 setbit(ic->ic_chan_avail, c->ic_ieee); 178 /* 179 * Identify mode capabilities. 180 */ 181 if (IEEE80211_IS_CHAN_A(c)) 182 setbit(ic->ic_modecaps, IEEE80211_MODE_11A); 183 if (IEEE80211_IS_CHAN_B(c)) 184 setbit(ic->ic_modecaps, IEEE80211_MODE_11B); 185 if (IEEE80211_IS_CHAN_ANYG(c)) 186 setbit(ic->ic_modecaps, IEEE80211_MODE_11G); 187 if (IEEE80211_IS_CHAN_FHSS(c)) 188 setbit(ic->ic_modecaps, IEEE80211_MODE_FH); 189 if (IEEE80211_IS_CHAN_108A(c)) 190 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A); 191 if (IEEE80211_IS_CHAN_108G(c)) 192 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G); 193 if (IEEE80211_IS_CHAN_ST(c)) 194 setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A); 195 if (IEEE80211_IS_CHAN_HALF(c)) 196 setbit(ic->ic_modecaps, IEEE80211_MODE_HALF); 197 if (IEEE80211_IS_CHAN_QUARTER(c)) 198 setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER); 199 if (IEEE80211_IS_CHAN_HTA(c)) 200 setbit(ic->ic_modecaps, IEEE80211_MODE_11NA); 201 if (IEEE80211_IS_CHAN_HTG(c)) 202 setbit(ic->ic_modecaps, IEEE80211_MODE_11NG); 203 if (IEEE80211_IS_CHAN_VHTA(c)) 204 setbit(ic->ic_modecaps, IEEE80211_MODE_VHT_5GHZ); 205 if (IEEE80211_IS_CHAN_VHTG(c)) 206 setbit(ic->ic_modecaps, IEEE80211_MODE_VHT_2GHZ); 207 } 208 /* initialize candidate channels to all available */ 209 memcpy(ic->ic_chan_active, ic->ic_chan_avail, 210 sizeof(ic->ic_chan_avail)); 211 212 /* sort channel table to allow lookup optimizations */ 213 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans); 214 215 /* invalidate any previous state */ 216 ic->ic_bsschan = IEEE80211_CHAN_ANYC; 217 ic->ic_prevchan = NULL; 218 ic->ic_csa_newchan = NULL; 219 /* arbitrarily pick the first channel */ 220 ic->ic_curchan = &ic->ic_channels[0]; 221 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan); 222 223 /* fillin well-known rate sets if driver has not specified */ 224 DEFAULTRATES(IEEE80211_MODE_11B, ieee80211_rateset_11b); 225 DEFAULTRATES(IEEE80211_MODE_11G, ieee80211_rateset_11g); 226 DEFAULTRATES(IEEE80211_MODE_11A, ieee80211_rateset_11a); 227 DEFAULTRATES(IEEE80211_MODE_TURBO_A, ieee80211_rateset_11a); 228 DEFAULTRATES(IEEE80211_MODE_TURBO_G, ieee80211_rateset_11g); 229 DEFAULTRATES(IEEE80211_MODE_STURBO_A, ieee80211_rateset_11a); 230 DEFAULTRATES(IEEE80211_MODE_HALF, ieee80211_rateset_half); 231 DEFAULTRATES(IEEE80211_MODE_QUARTER, ieee80211_rateset_quarter); 232 DEFAULTRATES(IEEE80211_MODE_11NA, ieee80211_rateset_11a); 233 DEFAULTRATES(IEEE80211_MODE_11NG, ieee80211_rateset_11g); 234 DEFAULTRATES(IEEE80211_MODE_VHT_2GHZ, ieee80211_rateset_11g); 235 DEFAULTRATES(IEEE80211_MODE_VHT_5GHZ, ieee80211_rateset_11a); 236 237 /* 238 * Setup required information to fill the mcsset field, if driver did 239 * not. Assume a 2T2R setup for historic reasons. 240 */ 241 if (ic->ic_rxstream == 0) 242 ic->ic_rxstream = 2; 243 if (ic->ic_txstream == 0) 244 ic->ic_txstream = 2; 245 246 ieee80211_init_suphtrates(ic); 247 248 /* 249 * Set auto mode to reset active channel state and any desired channel. 250 */ 251 (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO); 252 #undef DEFAULTRATES 253 } 254 255 static void 256 null_update_mcast(struct ieee80211com *ic) 257 { 258 259 ic_printf(ic, "need multicast update callback\n"); 260 } 261 262 static void 263 null_update_promisc(struct ieee80211com *ic) 264 { 265 266 ic_printf(ic, "need promiscuous mode update callback\n"); 267 } 268 269 static void 270 null_update_chw(struct ieee80211com *ic) 271 { 272 273 ic_printf(ic, "%s: need callback\n", __func__); 274 } 275 276 int 277 ic_printf(struct ieee80211com *ic, const char * fmt, ...) 278 { 279 va_list ap; 280 int retval; 281 282 retval = printf("%s: ", ic->ic_name); 283 va_start(ap, fmt); 284 retval += vprintf(fmt, ap); 285 va_end(ap); 286 return (retval); 287 } 288 289 static LIST_HEAD(, ieee80211com) ic_head = LIST_HEAD_INITIALIZER(ic_head); 290 static struct mtx ic_list_mtx; 291 MTX_SYSINIT(ic_list, &ic_list_mtx, "ieee80211com list", MTX_DEF); 292 293 static int 294 sysctl_ieee80211coms(SYSCTL_HANDLER_ARGS) 295 { 296 struct ieee80211com *ic; 297 struct sbuf sb; 298 char *sp; 299 int error; 300 301 error = sysctl_wire_old_buffer(req, 0); 302 if (error) 303 return (error); 304 sbuf_new_for_sysctl(&sb, NULL, 8, req); 305 sbuf_clear_flags(&sb, SBUF_INCLUDENUL); 306 sp = ""; 307 mtx_lock(&ic_list_mtx); 308 LIST_FOREACH(ic, &ic_head, ic_next) { 309 sbuf_printf(&sb, "%s%s", sp, ic->ic_name); 310 sp = " "; 311 } 312 mtx_unlock(&ic_list_mtx); 313 error = sbuf_finish(&sb); 314 sbuf_delete(&sb); 315 return (error); 316 } 317 318 SYSCTL_PROC(_net_wlan, OID_AUTO, devices, 319 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 320 sysctl_ieee80211coms, "A", "names of available 802.11 devices"); 321 322 /* 323 * Attach/setup the common net80211 state. Called by 324 * the driver on attach to prior to creating any vap's. 325 */ 326 void 327 ieee80211_ifattach(struct ieee80211com *ic) 328 { 329 330 IEEE80211_LOCK_INIT(ic, ic->ic_name); 331 IEEE80211_TX_LOCK_INIT(ic, ic->ic_name); 332 TAILQ_INIT(&ic->ic_vaps); 333 334 /* Create a taskqueue for all state changes */ 335 ic->ic_tq = taskqueue_create("ic_taskq", 336 IEEE80211_M_WAITOK | IEEE80211_M_ZERO, 337 taskqueue_thread_enqueue, &ic->ic_tq); 338 taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq", 339 ic->ic_name); 340 ic->ic_ierrors = counter_u64_alloc(IEEE80211_M_WAITOK); 341 ic->ic_oerrors = counter_u64_alloc(IEEE80211_M_WAITOK); 342 /* 343 * Fill in 802.11 available channel set, mark all 344 * available channels as active, and pick a default 345 * channel if not already specified. 346 */ 347 ieee80211_chan_init(ic); 348 349 ic->ic_update_mcast = null_update_mcast; 350 ic->ic_update_promisc = null_update_promisc; 351 ic->ic_update_chw = null_update_chw; 352 353 ic->ic_hash_key = arc4random(); 354 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT; 355 ic->ic_lintval = ic->ic_bintval; 356 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX; 357 358 ieee80211_crypto_attach(ic); 359 ieee80211_node_attach(ic); 360 ieee80211_power_attach(ic); 361 ieee80211_proto_attach(ic); 362 #ifdef IEEE80211_SUPPORT_SUPERG 363 ieee80211_superg_attach(ic); 364 #endif 365 ieee80211_ht_attach(ic); 366 ieee80211_vht_attach(ic); 367 ieee80211_scan_attach(ic); 368 ieee80211_regdomain_attach(ic); 369 ieee80211_dfs_attach(ic); 370 371 ieee80211_sysctl_attach(ic); 372 373 mtx_lock(&ic_list_mtx); 374 LIST_INSERT_HEAD(&ic_head, ic, ic_next); 375 mtx_unlock(&ic_list_mtx); 376 } 377 378 /* 379 * Detach net80211 state on device detach. Tear down 380 * all vap's and reclaim all common state prior to the 381 * device state going away. Note we may call back into 382 * driver; it must be prepared for this. 383 */ 384 void 385 ieee80211_ifdetach(struct ieee80211com *ic) 386 { 387 struct ieee80211vap *vap; 388 389 /* 390 * We use this as an indicator that ifattach never had a chance to be 391 * called, e.g. early driver attach failed and ifdetach was called 392 * during subsequent detach. Never fear, for we have nothing to do 393 * here. 394 */ 395 if (ic->ic_tq == NULL) 396 return; 397 398 mtx_lock(&ic_list_mtx); 399 LIST_REMOVE(ic, ic_next); 400 mtx_unlock(&ic_list_mtx); 401 402 taskqueue_drain(taskqueue_thread, &ic->ic_restart_task); 403 404 /* 405 * The VAP is responsible for setting and clearing 406 * the VIMAGE context. 407 */ 408 while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL) { 409 ieee80211_com_vdetach(vap); 410 ieee80211_vap_destroy(vap); 411 } 412 ieee80211_waitfor_parent(ic); 413 414 ieee80211_sysctl_detach(ic); 415 ieee80211_dfs_detach(ic); 416 ieee80211_regdomain_detach(ic); 417 ieee80211_scan_detach(ic); 418 #ifdef IEEE80211_SUPPORT_SUPERG 419 ieee80211_superg_detach(ic); 420 #endif 421 ieee80211_vht_detach(ic); 422 ieee80211_ht_detach(ic); 423 /* NB: must be called before ieee80211_node_detach */ 424 ieee80211_proto_detach(ic); 425 ieee80211_crypto_detach(ic); 426 ieee80211_power_detach(ic); 427 ieee80211_node_detach(ic); 428 429 counter_u64_free(ic->ic_ierrors); 430 counter_u64_free(ic->ic_oerrors); 431 432 taskqueue_free(ic->ic_tq); 433 IEEE80211_TX_LOCK_DESTROY(ic); 434 IEEE80211_LOCK_DESTROY(ic); 435 } 436 437 /* 438 * Called by drivers during attach to set the supported 439 * cipher set for software encryption. 440 */ 441 void 442 ieee80211_set_software_ciphers(struct ieee80211com *ic, 443 uint32_t cipher_suite) 444 { 445 ieee80211_crypto_set_supported_software_ciphers(ic, cipher_suite); 446 } 447 448 /* 449 * Called by drivers during attach to set the supported 450 * cipher set for hardware encryption. 451 */ 452 void 453 ieee80211_set_hardware_ciphers(struct ieee80211com *ic, 454 uint32_t cipher_suite) 455 { 456 ieee80211_crypto_set_supported_hardware_ciphers(ic, cipher_suite); 457 } 458 459 /* 460 * Called by drivers during attach to set the supported 461 * key management suites by the driver/hardware. 462 */ 463 void 464 ieee80211_set_driver_keymgmt_suites(struct ieee80211com *ic, 465 uint32_t keymgmt_set) 466 { 467 ieee80211_crypto_set_supported_driver_keymgmt(ic, 468 keymgmt_set); 469 } 470 471 struct ieee80211com * 472 ieee80211_find_com(const char *name) 473 { 474 struct ieee80211com *ic; 475 476 mtx_lock(&ic_list_mtx); 477 LIST_FOREACH(ic, &ic_head, ic_next) 478 if (strcmp(ic->ic_name, name) == 0) 479 break; 480 mtx_unlock(&ic_list_mtx); 481 482 return (ic); 483 } 484 485 void 486 ieee80211_iterate_coms(ieee80211_com_iter_func *f, void *arg) 487 { 488 struct ieee80211com *ic; 489 490 mtx_lock(&ic_list_mtx); 491 LIST_FOREACH(ic, &ic_head, ic_next) 492 (*f)(arg, ic); 493 mtx_unlock(&ic_list_mtx); 494 } 495 496 /* 497 * Default reset method for use with the ioctl support. This 498 * method is invoked after any state change in the 802.11 499 * layer that should be propagated to the hardware but not 500 * require re-initialization of the 802.11 state machine (e.g 501 * rescanning for an ap). We always return ENETRESET which 502 * should cause the driver to re-initialize the device. Drivers 503 * can override this method to implement more optimized support. 504 */ 505 static int 506 default_reset(struct ieee80211vap *vap, u_long cmd) 507 { 508 return ENETRESET; 509 } 510 511 /* 512 * Default for updating the VAP default TX key index. 513 * 514 * Drivers that support TX offload as well as hardware encryption offload 515 * may need to be informed of key index changes separate from the key 516 * update. 517 */ 518 static void 519 default_update_deftxkey(struct ieee80211vap *vap, ieee80211_keyix kid) 520 { 521 522 /* XXX assert validity */ 523 /* XXX assert we're in a key update block */ 524 vap->iv_def_txkey = kid; 525 } 526 527 /* 528 * Add underlying device errors to vap errors. 529 */ 530 static uint64_t 531 ieee80211_get_counter(struct ifnet *ifp, ift_counter cnt) 532 { 533 struct ieee80211vap *vap = ifp->if_softc; 534 struct ieee80211com *ic = vap->iv_ic; 535 uint64_t rv; 536 537 rv = if_get_counter_default(ifp, cnt); 538 switch (cnt) { 539 case IFCOUNTER_OERRORS: 540 rv += counter_u64_fetch(ic->ic_oerrors); 541 break; 542 case IFCOUNTER_IERRORS: 543 rv += counter_u64_fetch(ic->ic_ierrors); 544 break; 545 default: 546 break; 547 } 548 549 return (rv); 550 } 551 552 /* 553 * Prepare a vap for use. Drivers use this call to 554 * setup net80211 state in new vap's prior attaching 555 * them with ieee80211_vap_attach (below). 556 */ 557 int 558 ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap, 559 const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode, 560 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN]) 561 { 562 struct ifnet *ifp; 563 564 ifp = if_alloc(IFT_ETHER); 565 if_initname(ifp, name, unit); 566 ifp->if_softc = vap; /* back pointer */ 567 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST; 568 ifp->if_transmit = ieee80211_vap_transmit; 569 ifp->if_qflush = ieee80211_vap_qflush; 570 ifp->if_ioctl = ieee80211_ioctl; 571 ifp->if_init = ieee80211_init; 572 ifp->if_get_counter = ieee80211_get_counter; 573 574 vap->iv_ifp = ifp; 575 vap->iv_ic = ic; 576 vap->iv_flags = ic->ic_flags; /* propagate common flags */ 577 vap->iv_flags_ext = ic->ic_flags_ext; 578 vap->iv_flags_ven = ic->ic_flags_ven; 579 vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE; 580 581 /* 11n capabilities - XXX methodize */ 582 vap->iv_htcaps = ic->ic_htcaps; 583 vap->iv_htextcaps = ic->ic_htextcaps; 584 585 /* 11ac capabilities - XXX methodize */ 586 vap->iv_vht_cap.vht_cap_info = ic->ic_vht_cap.vht_cap_info; 587 vap->iv_vhtextcaps = ic->ic_vhtextcaps; 588 589 vap->iv_opmode = opmode; 590 vap->iv_caps |= ieee80211_opcap[opmode]; 591 IEEE80211_ADDR_COPY(vap->iv_myaddr, ic->ic_macaddr); 592 switch (opmode) { 593 case IEEE80211_M_WDS: 594 /* 595 * WDS links must specify the bssid of the far end. 596 * For legacy operation this is a static relationship. 597 * For non-legacy operation the station must associate 598 * and be authorized to pass traffic. Plumbing the 599 * vap to the proper node happens when the vap 600 * transitions to RUN state. 601 */ 602 IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid); 603 vap->iv_flags |= IEEE80211_F_DESBSSID; 604 if (flags & IEEE80211_CLONE_WDSLEGACY) 605 vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY; 606 break; 607 #ifdef IEEE80211_SUPPORT_TDMA 608 case IEEE80211_M_AHDEMO: 609 if (flags & IEEE80211_CLONE_TDMA) { 610 /* NB: checked before clone operation allowed */ 611 KASSERT(ic->ic_caps & IEEE80211_C_TDMA, 612 ("not TDMA capable, ic_caps 0x%x", ic->ic_caps)); 613 /* 614 * Propagate TDMA capability to mark vap; this 615 * cannot be removed and is used to distinguish 616 * regular ahdemo operation from ahdemo+tdma. 617 */ 618 vap->iv_caps |= IEEE80211_C_TDMA; 619 } 620 break; 621 #endif 622 default: 623 break; 624 } 625 /* auto-enable s/w beacon miss support */ 626 if (flags & IEEE80211_CLONE_NOBEACONS) 627 vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS; 628 /* auto-generated or user supplied MAC address */ 629 if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR)) 630 vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC; 631 /* 632 * Enable various functionality by default if we're 633 * capable; the driver can override us if it knows better. 634 */ 635 if (vap->iv_caps & IEEE80211_C_WME) 636 vap->iv_flags |= IEEE80211_F_WME; 637 if (vap->iv_caps & IEEE80211_C_BURST) 638 vap->iv_flags |= IEEE80211_F_BURST; 639 /* NB: bg scanning only makes sense for station mode right now */ 640 if (vap->iv_opmode == IEEE80211_M_STA && 641 (vap->iv_caps & IEEE80211_C_BGSCAN)) 642 vap->iv_flags |= IEEE80211_F_BGSCAN; 643 vap->iv_flags |= IEEE80211_F_DOTH; /* XXX no cap, just ena */ 644 /* NB: DFS support only makes sense for ap mode right now */ 645 if (vap->iv_opmode == IEEE80211_M_HOSTAP && 646 (vap->iv_caps & IEEE80211_C_DFS)) 647 vap->iv_flags_ext |= IEEE80211_FEXT_DFS; 648 /* NB: only flip on U-APSD for hostap/sta for now */ 649 if ((vap->iv_opmode == IEEE80211_M_STA) 650 || (vap->iv_opmode == IEEE80211_M_HOSTAP)) { 651 if (vap->iv_caps & IEEE80211_C_UAPSD) 652 vap->iv_flags_ext |= IEEE80211_FEXT_UAPSD; 653 } 654 655 vap->iv_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */ 656 vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT; 657 vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT; 658 /* 659 * Install a default reset method for the ioctl support; 660 * the driver can override this. 661 */ 662 vap->iv_reset = default_reset; 663 664 /* 665 * Install a default crypto key update method, the driver 666 * can override this. 667 */ 668 vap->iv_update_deftxkey = default_update_deftxkey; 669 670 ieee80211_sysctl_vattach(vap); 671 ieee80211_crypto_vattach(vap); 672 ieee80211_node_vattach(vap); 673 ieee80211_power_vattach(vap); 674 ieee80211_proto_vattach(vap); 675 #ifdef IEEE80211_SUPPORT_SUPERG 676 ieee80211_superg_vattach(vap); 677 #endif 678 ieee80211_ht_vattach(vap); 679 ieee80211_vht_vattach(vap); 680 ieee80211_scan_vattach(vap); 681 ieee80211_regdomain_vattach(vap); 682 ieee80211_radiotap_vattach(vap); 683 ieee80211_vap_reset_erp(vap); 684 ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE); 685 686 return 0; 687 } 688 689 /* 690 * Activate a vap. State should have been prepared with a 691 * call to ieee80211_vap_setup and by the driver. On return 692 * from this call the vap is ready for use. 693 */ 694 int 695 ieee80211_vap_attach(struct ieee80211vap *vap, ifm_change_cb_t media_change, 696 ifm_stat_cb_t media_stat, const uint8_t macaddr[IEEE80211_ADDR_LEN]) 697 { 698 struct ifnet *ifp = vap->iv_ifp; 699 struct ieee80211com *ic = vap->iv_ic; 700 struct ifmediareq imr; 701 int maxrate; 702 703 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, 704 "%s: %s parent %s flags 0x%x flags_ext 0x%x\n", 705 __func__, ieee80211_opmode_name[vap->iv_opmode], 706 ic->ic_name, vap->iv_flags, vap->iv_flags_ext); 707 708 /* 709 * Do late attach work that cannot happen until after 710 * the driver has had a chance to override defaults. 711 */ 712 ieee80211_node_latevattach(vap); 713 ieee80211_power_latevattach(vap); 714 715 maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps, 716 vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat); 717 ieee80211_media_status(ifp, &imr); 718 /* NB: strip explicit mode; we're actually in autoselect */ 719 ifmedia_set(&vap->iv_media, 720 imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO)); 721 if (maxrate) 722 ifp->if_baudrate = IF_Mbps(maxrate); 723 724 ether_ifattach(ifp, macaddr); 725 IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp)); 726 /* hook output method setup by ether_ifattach */ 727 vap->iv_output = ifp->if_output; 728 ifp->if_output = ieee80211_output; 729 /* NB: if_mtu set by ether_ifattach to ETHERMTU */ 730 731 IEEE80211_LOCK(ic); 732 TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next); 733 ieee80211_syncflag_locked(ic, IEEE80211_F_WME); 734 #ifdef IEEE80211_SUPPORT_SUPERG 735 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP); 736 #endif 737 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF); 738 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST); 739 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT); 740 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40); 741 742 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT); 743 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40); 744 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80); 745 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160); 746 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80); 747 IEEE80211_UNLOCK(ic); 748 749 return 1; 750 } 751 752 /* 753 * Tear down vap state and reclaim the ifnet. 754 * The driver is assumed to have prepared for 755 * this; e.g. by turning off interrupts for the 756 * underlying device. 757 */ 758 void 759 ieee80211_vap_detach(struct ieee80211vap *vap) 760 { 761 struct ieee80211com *ic = vap->iv_ic; 762 struct ifnet *ifp = vap->iv_ifp; 763 int i; 764 765 CURVNET_SET(ifp->if_vnet); 766 767 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n", 768 __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name); 769 770 /* NB: bpfdetach is called by ether_ifdetach and claims all taps */ 771 ether_ifdetach(ifp); 772 773 ieee80211_stop(vap); 774 775 /* 776 * Flush any deferred vap tasks. 777 */ 778 for (i = 0; i < NET80211_IV_NSTATE_NUM; i++) 779 ieee80211_draintask(ic, &vap->iv_nstate_task[i]); 780 ieee80211_draintask(ic, &vap->iv_swbmiss_task); 781 ieee80211_draintask(ic, &vap->iv_wme_task); 782 ieee80211_draintask(ic, &ic->ic_parent_task); 783 784 /* XXX band-aid until ifnet handles this for us */ 785 taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 786 787 IEEE80211_LOCK(ic); 788 KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running")); 789 TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next); 790 ieee80211_syncflag_locked(ic, IEEE80211_F_WME); 791 #ifdef IEEE80211_SUPPORT_SUPERG 792 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP); 793 #endif 794 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF); 795 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST); 796 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT); 797 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40); 798 799 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT); 800 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40); 801 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80); 802 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160); 803 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80); 804 805 /* NB: this handles the bpfdetach done below */ 806 ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF); 807 if (vap->iv_ifflags & IFF_PROMISC) 808 ieee80211_promisc(vap, false); 809 if (vap->iv_ifflags & IFF_ALLMULTI) 810 ieee80211_allmulti(vap, false); 811 IEEE80211_UNLOCK(ic); 812 813 ifmedia_removeall(&vap->iv_media); 814 815 ieee80211_radiotap_vdetach(vap); 816 ieee80211_regdomain_vdetach(vap); 817 ieee80211_scan_vdetach(vap); 818 #ifdef IEEE80211_SUPPORT_SUPERG 819 ieee80211_superg_vdetach(vap); 820 #endif 821 ieee80211_vht_vdetach(vap); 822 ieee80211_ht_vdetach(vap); 823 /* NB: must be before ieee80211_node_vdetach */ 824 ieee80211_proto_vdetach(vap); 825 ieee80211_crypto_vdetach(vap); 826 ieee80211_power_vdetach(vap); 827 ieee80211_node_vdetach(vap); 828 ieee80211_sysctl_vdetach(vap); 829 830 if_free(ifp); 831 832 CURVNET_RESTORE(); 833 } 834 835 /* 836 * Count number of vaps in promisc, and issue promisc on 837 * parent respectively. 838 */ 839 void 840 ieee80211_promisc(struct ieee80211vap *vap, bool on) 841 { 842 struct ieee80211com *ic = vap->iv_ic; 843 844 IEEE80211_LOCK_ASSERT(ic); 845 846 if (on) { 847 if (++ic->ic_promisc == 1) 848 ieee80211_runtask(ic, &ic->ic_promisc_task); 849 } else { 850 KASSERT(ic->ic_promisc > 0, ("%s: ic %p not promisc", 851 __func__, ic)); 852 if (--ic->ic_promisc == 0) 853 ieee80211_runtask(ic, &ic->ic_promisc_task); 854 } 855 } 856 857 /* 858 * Count number of vaps in allmulti, and issue allmulti on 859 * parent respectively. 860 */ 861 void 862 ieee80211_allmulti(struct ieee80211vap *vap, bool on) 863 { 864 struct ieee80211com *ic = vap->iv_ic; 865 866 IEEE80211_LOCK_ASSERT(ic); 867 868 if (on) { 869 if (++ic->ic_allmulti == 1) 870 ieee80211_runtask(ic, &ic->ic_mcast_task); 871 } else { 872 KASSERT(ic->ic_allmulti > 0, ("%s: ic %p not allmulti", 873 __func__, ic)); 874 if (--ic->ic_allmulti == 0) 875 ieee80211_runtask(ic, &ic->ic_mcast_task); 876 } 877 } 878 879 /* 880 * Synchronize flag bit state in the com structure 881 * according to the state of all vap's. This is used, 882 * for example, to handle state changes via ioctls. 883 */ 884 static void 885 ieee80211_syncflag_locked(struct ieee80211com *ic, int flag) 886 { 887 struct ieee80211vap *vap; 888 int bit; 889 890 IEEE80211_LOCK_ASSERT(ic); 891 892 bit = 0; 893 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 894 if (vap->iv_flags & flag) { 895 bit = 1; 896 break; 897 } 898 if (bit) 899 ic->ic_flags |= flag; 900 else 901 ic->ic_flags &= ~flag; 902 } 903 904 void 905 ieee80211_syncflag(struct ieee80211vap *vap, int flag) 906 { 907 struct ieee80211com *ic = vap->iv_ic; 908 909 IEEE80211_LOCK(ic); 910 if (flag < 0) { 911 flag = -flag; 912 vap->iv_flags &= ~flag; 913 } else 914 vap->iv_flags |= flag; 915 ieee80211_syncflag_locked(ic, flag); 916 IEEE80211_UNLOCK(ic); 917 } 918 919 /* 920 * Synchronize flags_ht bit state in the com structure 921 * according to the state of all vap's. This is used, 922 * for example, to handle state changes via ioctls. 923 */ 924 static void 925 ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag) 926 { 927 struct ieee80211vap *vap; 928 int bit; 929 930 IEEE80211_LOCK_ASSERT(ic); 931 932 bit = 0; 933 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 934 if (vap->iv_flags_ht & flag) { 935 bit = 1; 936 break; 937 } 938 if (bit) 939 ic->ic_flags_ht |= flag; 940 else 941 ic->ic_flags_ht &= ~flag; 942 } 943 944 void 945 ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag) 946 { 947 struct ieee80211com *ic = vap->iv_ic; 948 949 IEEE80211_LOCK(ic); 950 if (flag < 0) { 951 flag = -flag; 952 vap->iv_flags_ht &= ~flag; 953 } else 954 vap->iv_flags_ht |= flag; 955 ieee80211_syncflag_ht_locked(ic, flag); 956 IEEE80211_UNLOCK(ic); 957 } 958 959 /* 960 * Synchronize flags_vht bit state in the com structure 961 * according to the state of all vap's. This is used, 962 * for example, to handle state changes via ioctls. 963 */ 964 static void 965 ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag) 966 { 967 struct ieee80211vap *vap; 968 int bit; 969 970 IEEE80211_LOCK_ASSERT(ic); 971 972 bit = 0; 973 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 974 if (vap->iv_vht_flags & flag) { 975 bit = 1; 976 break; 977 } 978 if (bit) 979 ic->ic_vht_flags |= flag; 980 else 981 ic->ic_vht_flags &= ~flag; 982 } 983 984 void 985 ieee80211_syncflag_vht(struct ieee80211vap *vap, int flag) 986 { 987 struct ieee80211com *ic = vap->iv_ic; 988 989 IEEE80211_LOCK(ic); 990 if (flag < 0) { 991 flag = -flag; 992 vap->iv_vht_flags &= ~flag; 993 } else 994 vap->iv_vht_flags |= flag; 995 ieee80211_syncflag_vht_locked(ic, flag); 996 IEEE80211_UNLOCK(ic); 997 } 998 999 /* 1000 * Synchronize flags_ext bit state in the com structure 1001 * according to the state of all vap's. This is used, 1002 * for example, to handle state changes via ioctls. 1003 */ 1004 static void 1005 ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag) 1006 { 1007 struct ieee80211vap *vap; 1008 int bit; 1009 1010 IEEE80211_LOCK_ASSERT(ic); 1011 1012 bit = 0; 1013 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 1014 if (vap->iv_flags_ext & flag) { 1015 bit = 1; 1016 break; 1017 } 1018 if (bit) 1019 ic->ic_flags_ext |= flag; 1020 else 1021 ic->ic_flags_ext &= ~flag; 1022 } 1023 1024 void 1025 ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag) 1026 { 1027 struct ieee80211com *ic = vap->iv_ic; 1028 1029 IEEE80211_LOCK(ic); 1030 if (flag < 0) { 1031 flag = -flag; 1032 vap->iv_flags_ext &= ~flag; 1033 } else 1034 vap->iv_flags_ext |= flag; 1035 ieee80211_syncflag_ext_locked(ic, flag); 1036 IEEE80211_UNLOCK(ic); 1037 } 1038 1039 static __inline int 1040 mapgsm(u_int freq, u_int flags) 1041 { 1042 freq *= 10; 1043 if (flags & IEEE80211_CHAN_QUARTER) 1044 freq += 5; 1045 else if (flags & IEEE80211_CHAN_HALF) 1046 freq += 10; 1047 else 1048 freq += 20; 1049 /* NB: there is no 907/20 wide but leave room */ 1050 return (freq - 906*10) / 5; 1051 } 1052 1053 static __inline int 1054 mappsb(u_int freq, u_int flags) 1055 { 1056 return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5; 1057 } 1058 1059 /* 1060 * Convert MHz frequency to IEEE channel number. 1061 */ 1062 int 1063 ieee80211_mhz2ieee(u_int freq, u_int flags) 1064 { 1065 #define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990) 1066 if (flags & IEEE80211_CHAN_GSM) 1067 return mapgsm(freq, flags); 1068 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 1069 if (freq == 2484) 1070 return 14; 1071 if (freq < 2484) 1072 return ((int) freq - 2407) / 5; 1073 else 1074 return 15 + ((freq - 2512) / 20); 1075 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */ 1076 if (freq <= 5000) { 1077 /* XXX check regdomain? */ 1078 if (IS_FREQ_IN_PSB(freq)) 1079 return mappsb(freq, flags); 1080 return (freq - 4000) / 5; 1081 } else 1082 return (freq - 5000) / 5; 1083 } else { /* either, guess */ 1084 if (freq == 2484) 1085 return 14; 1086 if (freq < 2484) { 1087 if (907 <= freq && freq <= 922) 1088 return mapgsm(freq, flags); 1089 return ((int) freq - 2407) / 5; 1090 } 1091 if (freq < 5000) { 1092 if (IS_FREQ_IN_PSB(freq)) 1093 return mappsb(freq, flags); 1094 else if (freq > 4900) 1095 return (freq - 4000) / 5; 1096 else 1097 return 15 + ((freq - 2512) / 20); 1098 } 1099 return (freq - 5000) / 5; 1100 } 1101 #undef IS_FREQ_IN_PSB 1102 } 1103 1104 /* 1105 * Convert channel to IEEE channel number. 1106 */ 1107 int 1108 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c) 1109 { 1110 if (c == NULL) { 1111 ic_printf(ic, "invalid channel (NULL)\n"); 1112 return 0; /* XXX */ 1113 } 1114 return (c == IEEE80211_CHAN_ANYC ? IEEE80211_CHAN_ANY : c->ic_ieee); 1115 } 1116 1117 /* 1118 * Convert IEEE channel number to MHz frequency. 1119 */ 1120 u_int 1121 ieee80211_ieee2mhz(u_int chan, u_int flags) 1122 { 1123 if (flags & IEEE80211_CHAN_GSM) 1124 return 907 + 5 * (chan / 10); 1125 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 1126 if (chan == 14) 1127 return 2484; 1128 if (chan < 14) 1129 return 2407 + chan*5; 1130 else 1131 return 2512 + ((chan-15)*20); 1132 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */ 1133 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) { 1134 chan -= 37; 1135 return 4940 + chan*5 + (chan % 5 ? 2 : 0); 1136 } 1137 return 5000 + (chan*5); 1138 } else { /* either, guess */ 1139 /* XXX can't distinguish PSB+GSM channels */ 1140 if (chan == 14) 1141 return 2484; 1142 if (chan < 14) /* 0-13 */ 1143 return 2407 + chan*5; 1144 if (chan < 27) /* 15-26 */ 1145 return 2512 + ((chan-15)*20); 1146 return 5000 + (chan*5); 1147 } 1148 } 1149 1150 static __inline void 1151 set_extchan(struct ieee80211_channel *c) 1152 { 1153 1154 /* 1155 * IEEE Std 802.11-2012, page 1738, subclause 20.3.15.4: 1156 * "the secondary channel number shall be 'N + [1,-1] * 4' 1157 */ 1158 if (c->ic_flags & IEEE80211_CHAN_HT40U) 1159 c->ic_extieee = c->ic_ieee + 4; 1160 else if (c->ic_flags & IEEE80211_CHAN_HT40D) 1161 c->ic_extieee = c->ic_ieee - 4; 1162 else 1163 c->ic_extieee = 0; 1164 } 1165 1166 /* 1167 * Populate the freq1/freq2 fields as appropriate for VHT channels. 1168 * 1169 * This for now uses a hard-coded list of 80MHz wide channels. 1170 * 1171 * For HT20/HT40, freq1 just is the centre frequency of the 40MHz 1172 * wide channel we've already decided upon. 1173 * 1174 * For VHT80 and VHT160, there are only a small number of fixed 1175 * 80/160MHz wide channels, so we just use those. 1176 * 1177 * This is all likely very very wrong - both the regulatory code 1178 * and this code needs to ensure that all four channels are 1179 * available and valid before the VHT80 (and eight for VHT160) channel 1180 * is created. 1181 */ 1182 1183 struct vht_chan_range { 1184 uint16_t freq_start; 1185 uint16_t freq_end; 1186 }; 1187 1188 struct vht_chan_range vht80_chan_ranges[] = { 1189 { 5170, 5250 }, 1190 { 5250, 5330 }, 1191 { 5490, 5570 }, 1192 { 5570, 5650 }, 1193 { 5650, 5730 }, 1194 { 5735, 5815 }, 1195 { 0, 0 } 1196 }; 1197 1198 struct vht_chan_range vht160_chan_ranges[] = { 1199 { 5170, 5330 }, 1200 { 5490, 5650 }, 1201 { 0, 0 } 1202 }; 1203 1204 static int 1205 set_vht_extchan(struct ieee80211_channel *c) 1206 { 1207 int i; 1208 1209 if (! IEEE80211_IS_CHAN_VHT(c)) 1210 return (0); 1211 1212 if (IEEE80211_IS_CHAN_VHT80P80(c)) { 1213 printf("%s: TODO VHT80+80 channel (ieee=%d, flags=0x%08x)\n", 1214 __func__, c->ic_ieee, c->ic_flags); 1215 } 1216 1217 if (IEEE80211_IS_CHAN_VHT160(c)) { 1218 for (i = 0; vht160_chan_ranges[i].freq_start != 0; i++) { 1219 if (c->ic_freq >= vht160_chan_ranges[i].freq_start && 1220 c->ic_freq < vht160_chan_ranges[i].freq_end) { 1221 int midpoint; 1222 1223 midpoint = vht160_chan_ranges[i].freq_start + 80; 1224 c->ic_vht_ch_freq1 = 1225 ieee80211_mhz2ieee(midpoint, c->ic_flags); 1226 c->ic_vht_ch_freq2 = 0; 1227 #if 0 1228 printf("%s: %d, freq=%d, midpoint=%d, freq1=%d, freq2=%d\n", 1229 __func__, c->ic_ieee, c->ic_freq, midpoint, 1230 c->ic_vht_ch_freq1, c->ic_vht_ch_freq2); 1231 #endif 1232 return (1); 1233 } 1234 } 1235 return (0); 1236 } 1237 1238 if (IEEE80211_IS_CHAN_VHT80(c)) { 1239 for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) { 1240 if (c->ic_freq >= vht80_chan_ranges[i].freq_start && 1241 c->ic_freq < vht80_chan_ranges[i].freq_end) { 1242 int midpoint; 1243 1244 midpoint = vht80_chan_ranges[i].freq_start + 40; 1245 c->ic_vht_ch_freq1 = 1246 ieee80211_mhz2ieee(midpoint, c->ic_flags); 1247 c->ic_vht_ch_freq2 = 0; 1248 #if 0 1249 printf("%s: %d, freq=%d, midpoint=%d, freq1=%d, freq2=%d\n", 1250 __func__, c->ic_ieee, c->ic_freq, midpoint, 1251 c->ic_vht_ch_freq1, c->ic_vht_ch_freq2); 1252 #endif 1253 return (1); 1254 } 1255 } 1256 return (0); 1257 } 1258 1259 if (IEEE80211_IS_CHAN_VHT40(c)) { 1260 if (IEEE80211_IS_CHAN_HT40U(c)) 1261 c->ic_vht_ch_freq1 = c->ic_ieee + 2; 1262 else if (IEEE80211_IS_CHAN_HT40D(c)) 1263 c->ic_vht_ch_freq1 = c->ic_ieee - 2; 1264 else 1265 return (0); 1266 return (1); 1267 } 1268 1269 if (IEEE80211_IS_CHAN_VHT20(c)) { 1270 c->ic_vht_ch_freq1 = c->ic_ieee; 1271 return (1); 1272 } 1273 1274 printf("%s: unknown VHT channel type (ieee=%d, flags=0x%08x)\n", 1275 __func__, c->ic_ieee, c->ic_flags); 1276 1277 return (0); 1278 } 1279 1280 /* 1281 * Return whether the current channel could possibly be a part of 1282 * a VHT80/VHT160 channel. 1283 * 1284 * This doesn't check that the whole range is in the allowed list 1285 * according to regulatory. 1286 */ 1287 static bool 1288 is_vht160_valid_freq(uint16_t freq) 1289 { 1290 int i; 1291 1292 for (i = 0; vht160_chan_ranges[i].freq_start != 0; i++) { 1293 if (freq >= vht160_chan_ranges[i].freq_start && 1294 freq < vht160_chan_ranges[i].freq_end) 1295 return (true); 1296 } 1297 return (false); 1298 } 1299 1300 static int 1301 is_vht80_valid_freq(uint16_t freq) 1302 { 1303 int i; 1304 for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) { 1305 if (freq >= vht80_chan_ranges[i].freq_start && 1306 freq < vht80_chan_ranges[i].freq_end) 1307 return (1); 1308 } 1309 return (0); 1310 } 1311 1312 static int 1313 addchan(struct ieee80211_channel chans[], int maxchans, int *nchans, 1314 uint8_t ieee, uint16_t freq, int8_t maxregpower, uint32_t flags) 1315 { 1316 struct ieee80211_channel *c; 1317 1318 if (*nchans >= maxchans) 1319 return (ENOBUFS); 1320 1321 #if 0 1322 printf("%s: %d of %d: ieee=%d, freq=%d, flags=0x%08x\n", 1323 __func__, *nchans, maxchans, ieee, freq, flags); 1324 #endif 1325 1326 c = &chans[(*nchans)++]; 1327 c->ic_ieee = ieee; 1328 c->ic_freq = freq != 0 ? freq : ieee80211_ieee2mhz(ieee, flags); 1329 c->ic_maxregpower = maxregpower; 1330 c->ic_maxpower = 2 * maxregpower; 1331 c->ic_flags = flags; 1332 c->ic_vht_ch_freq1 = 0; 1333 c->ic_vht_ch_freq2 = 0; 1334 set_extchan(c); 1335 set_vht_extchan(c); 1336 1337 return (0); 1338 } 1339 1340 static int 1341 copychan_prev(struct ieee80211_channel chans[], int maxchans, int *nchans, 1342 uint32_t flags) 1343 { 1344 struct ieee80211_channel *c; 1345 1346 KASSERT(*nchans > 0, ("channel list is empty\n")); 1347 1348 if (*nchans >= maxchans) 1349 return (ENOBUFS); 1350 1351 #if 0 1352 printf("%s: %d of %d: flags=0x%08x\n", 1353 __func__, *nchans, maxchans, flags); 1354 #endif 1355 1356 c = &chans[(*nchans)++]; 1357 c[0] = c[-1]; 1358 c->ic_flags = flags; 1359 c->ic_vht_ch_freq1 = 0; 1360 c->ic_vht_ch_freq2 = 0; 1361 set_extchan(c); 1362 set_vht_extchan(c); 1363 1364 return (0); 1365 } 1366 1367 /* 1368 * XXX VHT-2GHz 1369 */ 1370 static void 1371 getflags_2ghz(const uint8_t bands[], uint32_t flags[], int cbw_flags) 1372 { 1373 int nmodes; 1374 1375 nmodes = 0; 1376 if (isset(bands, IEEE80211_MODE_11B)) 1377 flags[nmodes++] = IEEE80211_CHAN_B; 1378 if (isset(bands, IEEE80211_MODE_11G)) 1379 flags[nmodes++] = IEEE80211_CHAN_G; 1380 if (isset(bands, IEEE80211_MODE_11NG)) 1381 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20; 1382 if (cbw_flags & NET80211_CBW_FLAG_HT40) { 1383 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U; 1384 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D; 1385 } 1386 flags[nmodes] = 0; 1387 } 1388 1389 static void 1390 getflags_5ghz(const uint8_t bands[], uint32_t flags[], int cbw_flags) 1391 { 1392 int nmodes; 1393 1394 /* 1395 * The addchan_list() function seems to expect the flags array to 1396 * be in channel width order, so the VHT bits are interspersed 1397 * as appropriate to maintain said order. 1398 * 1399 * It also assumes HT40U is before HT40D. 1400 */ 1401 nmodes = 0; 1402 1403 /* 20MHz */ 1404 if (isset(bands, IEEE80211_MODE_11A)) 1405 flags[nmodes++] = IEEE80211_CHAN_A; 1406 if (isset(bands, IEEE80211_MODE_11NA)) 1407 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20; 1408 if (isset(bands, IEEE80211_MODE_VHT_5GHZ)) { 1409 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20 | 1410 IEEE80211_CHAN_VHT20; 1411 } 1412 1413 /* 40MHz */ 1414 if (cbw_flags & NET80211_CBW_FLAG_HT40) 1415 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U; 1416 if ((cbw_flags & NET80211_CBW_FLAG_HT40) && 1417 isset(bands, IEEE80211_MODE_VHT_5GHZ)) 1418 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U | 1419 IEEE80211_CHAN_VHT40U; 1420 if (cbw_flags & NET80211_CBW_FLAG_HT40) 1421 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D; 1422 if ((cbw_flags & NET80211_CBW_FLAG_HT40) && 1423 isset(bands, IEEE80211_MODE_VHT_5GHZ)) 1424 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D | 1425 IEEE80211_CHAN_VHT40D; 1426 1427 /* 80MHz */ 1428 if ((cbw_flags & NET80211_CBW_FLAG_VHT80) && 1429 isset(bands, IEEE80211_MODE_VHT_5GHZ)) { 1430 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U | 1431 IEEE80211_CHAN_VHT80; 1432 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D | 1433 IEEE80211_CHAN_VHT80; 1434 } 1435 1436 /* VHT160 */ 1437 if ((cbw_flags & NET80211_CBW_FLAG_VHT160) && 1438 isset(bands, IEEE80211_MODE_VHT_5GHZ)) { 1439 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U | 1440 IEEE80211_CHAN_VHT160; 1441 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D | 1442 IEEE80211_CHAN_VHT160; 1443 } 1444 1445 /* VHT80+80 */ 1446 if ((cbw_flags & NET80211_CBW_FLAG_VHT80P80) && 1447 isset(bands, IEEE80211_MODE_VHT_5GHZ)) { 1448 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U | 1449 IEEE80211_CHAN_VHT80P80; 1450 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D | 1451 IEEE80211_CHAN_VHT80P80; 1452 } 1453 1454 flags[nmodes] = 0; 1455 } 1456 1457 static void 1458 getflags(const uint8_t bands[], uint32_t flags[], int cbw_flags) 1459 { 1460 1461 flags[0] = 0; 1462 if (isset(bands, IEEE80211_MODE_11A) || 1463 isset(bands, IEEE80211_MODE_11NA) || 1464 isset(bands, IEEE80211_MODE_VHT_5GHZ)) { 1465 if (isset(bands, IEEE80211_MODE_11B) || 1466 isset(bands, IEEE80211_MODE_11G) || 1467 isset(bands, IEEE80211_MODE_11NG) || 1468 isset(bands, IEEE80211_MODE_VHT_2GHZ)) 1469 return; 1470 1471 getflags_5ghz(bands, flags, cbw_flags); 1472 } else 1473 getflags_2ghz(bands, flags, cbw_flags); 1474 } 1475 1476 /* 1477 * Add one 20 MHz channel into specified channel list. 1478 * You MUST NOT mix bands when calling this. It will not add 5ghz 1479 * channels if you have any B/G/N band bit set. 1480 * The _cbw() variant does also support HT40/VHT80/160/80+80. 1481 */ 1482 int 1483 ieee80211_add_channel_cbw(struct ieee80211_channel chans[], int maxchans, 1484 int *nchans, uint8_t ieee, uint16_t freq, int8_t maxregpower, 1485 uint32_t chan_flags, const uint8_t bands[], int cbw_flags) 1486 { 1487 uint32_t flags[IEEE80211_MODE_MAX]; 1488 int i, error; 1489 1490 getflags(bands, flags, cbw_flags); 1491 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__)); 1492 1493 error = addchan(chans, maxchans, nchans, ieee, freq, maxregpower, 1494 flags[0] | chan_flags); 1495 for (i = 1; flags[i] != 0 && error == 0; i++) { 1496 error = copychan_prev(chans, maxchans, nchans, 1497 flags[i] | chan_flags); 1498 } 1499 1500 return (error); 1501 } 1502 1503 int 1504 ieee80211_add_channel(struct ieee80211_channel chans[], int maxchans, 1505 int *nchans, uint8_t ieee, uint16_t freq, int8_t maxregpower, 1506 uint32_t chan_flags, const uint8_t bands[]) 1507 { 1508 1509 return (ieee80211_add_channel_cbw(chans, maxchans, nchans, ieee, freq, 1510 maxregpower, chan_flags, bands, 0)); 1511 } 1512 1513 static struct ieee80211_channel * 1514 findchannel(struct ieee80211_channel chans[], int nchans, uint16_t freq, 1515 uint32_t flags) 1516 { 1517 struct ieee80211_channel *c; 1518 int i; 1519 1520 flags &= IEEE80211_CHAN_ALLTURBO; 1521 /* brute force search */ 1522 for (i = 0; i < nchans; i++) { 1523 c = &chans[i]; 1524 if (c->ic_freq == freq && 1525 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 1526 return c; 1527 } 1528 return NULL; 1529 } 1530 1531 /* 1532 * Add 40 MHz channel pair into specified channel list. 1533 */ 1534 /* XXX VHT */ 1535 int 1536 ieee80211_add_channel_ht40(struct ieee80211_channel chans[], int maxchans, 1537 int *nchans, uint8_t ieee, int8_t maxregpower, uint32_t flags) 1538 { 1539 struct ieee80211_channel *cent, *extc; 1540 uint16_t freq; 1541 int error; 1542 1543 freq = ieee80211_ieee2mhz(ieee, flags); 1544 1545 /* 1546 * Each entry defines an HT40 channel pair; find the 1547 * center channel, then the extension channel above. 1548 */ 1549 flags |= IEEE80211_CHAN_HT20; 1550 cent = findchannel(chans, *nchans, freq, flags); 1551 if (cent == NULL) 1552 return (EINVAL); 1553 1554 extc = findchannel(chans, *nchans, freq + 20, flags); 1555 if (extc == NULL) 1556 return (ENOENT); 1557 1558 flags &= ~IEEE80211_CHAN_HT; 1559 error = addchan(chans, maxchans, nchans, cent->ic_ieee, cent->ic_freq, 1560 maxregpower, flags | IEEE80211_CHAN_HT40U); 1561 if (error != 0) 1562 return (error); 1563 1564 error = addchan(chans, maxchans, nchans, extc->ic_ieee, extc->ic_freq, 1565 maxregpower, flags | IEEE80211_CHAN_HT40D); 1566 1567 return (error); 1568 } 1569 1570 /* 1571 * Fetch the center frequency for the primary channel. 1572 */ 1573 uint32_t 1574 ieee80211_get_channel_center_freq(const struct ieee80211_channel *c) 1575 { 1576 1577 return (c->ic_freq); 1578 } 1579 1580 /* 1581 * Fetch the center frequency for the primary BAND channel. 1582 * 1583 * For 5, 10, 20MHz channels it'll be the normally configured channel 1584 * frequency. 1585 * 1586 * For 40MHz, 80MHz, 160MHz channels it will be the centre of the 1587 * wide channel, not the centre of the primary channel (that's ic_freq). 1588 * 1589 * For 80+80MHz channels this will be the centre of the primary 1590 * 80MHz channel; the secondary 80MHz channel will be center_freq2(). 1591 */ 1592 uint32_t 1593 ieee80211_get_channel_center_freq1(const struct ieee80211_channel *c) 1594 { 1595 1596 /* 1597 * VHT - use the pre-calculated centre frequency 1598 * of the given channel. 1599 */ 1600 if (IEEE80211_IS_CHAN_VHT(c)) 1601 return (ieee80211_ieee2mhz(c->ic_vht_ch_freq1, c->ic_flags)); 1602 1603 if (IEEE80211_IS_CHAN_HT40U(c)) { 1604 return (c->ic_freq + 10); 1605 } 1606 if (IEEE80211_IS_CHAN_HT40D(c)) { 1607 return (c->ic_freq - 10); 1608 } 1609 1610 return (c->ic_freq); 1611 } 1612 1613 /* 1614 * For now, no 80+80 support; it will likely always return 0. 1615 */ 1616 uint32_t 1617 ieee80211_get_channel_center_freq2(const struct ieee80211_channel *c) 1618 { 1619 1620 if (IEEE80211_IS_CHAN_VHT(c) && (c->ic_vht_ch_freq2 != 0)) 1621 return (ieee80211_ieee2mhz(c->ic_vht_ch_freq2, c->ic_flags)); 1622 1623 return (0); 1624 } 1625 1626 /* 1627 * Adds channels into specified channel list (ieee[] array must be sorted). 1628 * Channels are already sorted. 1629 */ 1630 static int 1631 add_chanlist(struct ieee80211_channel chans[], int maxchans, int *nchans, 1632 const uint8_t ieee[], int nieee, uint32_t flags[]) 1633 { 1634 uint16_t freq; 1635 int i, j, error; 1636 int is_vht; 1637 1638 for (i = 0; i < nieee; i++) { 1639 freq = ieee80211_ieee2mhz(ieee[i], flags[0]); 1640 for (j = 0; flags[j] != 0; j++) { 1641 /* 1642 * Notes: 1643 * + HT40 and VHT40 channels occur together, so 1644 * we need to be careful that we actually allow that. 1645 * + VHT80, VHT160 will coexist with HT40/VHT40, so 1646 * make sure it's not skipped because of the overlap 1647 * check used for (V)HT40. 1648 */ 1649 is_vht = !! (flags[j] & IEEE80211_CHAN_VHT); 1650 1651 /* XXX TODO FIXME VHT80P80. */ 1652 1653 /* Test for VHT160 analogue to the VHT80 below. */ 1654 if (is_vht && flags[j] & IEEE80211_CHAN_VHT160) 1655 if (! is_vht160_valid_freq(freq)) 1656 continue; 1657 1658 /* 1659 * Test for VHT80. 1660 * XXX This is all very broken right now. 1661 * What we /should/ do is: 1662 * 1663 * + check that the frequency is in the list of 1664 * allowed VHT80 ranges; and 1665 * + the other 3 channels in the list are actually 1666 * also available. 1667 */ 1668 if (is_vht && flags[j] & IEEE80211_CHAN_VHT80) 1669 if (! is_vht80_valid_freq(freq)) 1670 continue; 1671 1672 /* 1673 * Test for (V)HT40. 1674 * 1675 * This is also a fall through from VHT80; as we only 1676 * allow a VHT80 channel if the VHT40 combination is 1677 * also valid. If the VHT40 form is not valid then 1678 * we certainly can't do VHT80.. 1679 */ 1680 if (flags[j] & IEEE80211_CHAN_HT40D) 1681 /* 1682 * Can't have a "lower" channel if we are the 1683 * first channel. 1684 * 1685 * Can't have a "lower" channel if it's below/ 1686 * within 20MHz of the first channel. 1687 * 1688 * Can't have a "lower" channel if the channel 1689 * below it is not 20MHz away. 1690 */ 1691 if (i == 0 || ieee[i] < ieee[0] + 4 || 1692 freq - 20 != 1693 ieee80211_ieee2mhz(ieee[i] - 4, flags[j])) 1694 continue; 1695 if (flags[j] & IEEE80211_CHAN_HT40U) 1696 /* 1697 * Can't have an "upper" channel if we are 1698 * the last channel. 1699 * 1700 * Can't have an "upper" channel be above the 1701 * last channel in the list. 1702 * 1703 * Can't have an "upper" channel if the next 1704 * channel according to the math isn't 20MHz 1705 * away. (Likely for channel 13/14.) 1706 */ 1707 if (i == nieee - 1 || 1708 ieee[i] + 4 > ieee[nieee - 1] || 1709 freq + 20 != 1710 ieee80211_ieee2mhz(ieee[i] + 4, flags[j])) 1711 continue; 1712 1713 if (j == 0) { 1714 error = addchan(chans, maxchans, nchans, 1715 ieee[i], freq, 0, flags[j]); 1716 } else { 1717 error = copychan_prev(chans, maxchans, nchans, 1718 flags[j]); 1719 } 1720 if (error != 0) 1721 return (error); 1722 } 1723 } 1724 1725 return (0); 1726 } 1727 1728 int 1729 ieee80211_add_channel_list_2ghz(struct ieee80211_channel chans[], int maxchans, 1730 int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[], 1731 int cbw_flags) 1732 { 1733 uint32_t flags[IEEE80211_MODE_MAX]; 1734 1735 /* XXX no VHT for now */ 1736 getflags_2ghz(bands, flags, cbw_flags); 1737 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__)); 1738 1739 return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags)); 1740 } 1741 1742 int 1743 ieee80211_add_channels_default_2ghz(struct ieee80211_channel chans[], 1744 int maxchans, int *nchans, const uint8_t bands[], int cbw_flags) 1745 { 1746 const uint8_t default_chan_list[] = 1747 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }; 1748 1749 return (ieee80211_add_channel_list_2ghz(chans, maxchans, nchans, 1750 default_chan_list, nitems(default_chan_list), bands, cbw_flags)); 1751 } 1752 1753 int 1754 ieee80211_add_channel_list_5ghz(struct ieee80211_channel chans[], int maxchans, 1755 int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[], 1756 int cbw_flags) 1757 { 1758 /* 1759 * XXX-BZ with HT and VHT there is no 1:1 mapping anymore. Review all 1760 * uses of IEEE80211_MODE_MAX and add a new #define name for array size. 1761 */ 1762 uint32_t flags[2 * IEEE80211_MODE_MAX]; 1763 1764 getflags_5ghz(bands, flags, cbw_flags); 1765 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__)); 1766 1767 return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags)); 1768 } 1769 1770 /* 1771 * Locate a channel given a frequency+flags. We cache 1772 * the previous lookup to optimize switching between two 1773 * channels--as happens with dynamic turbo. 1774 */ 1775 struct ieee80211_channel * 1776 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags) 1777 { 1778 struct ieee80211_channel *c; 1779 1780 flags &= IEEE80211_CHAN_ALLTURBO; 1781 c = ic->ic_prevchan; 1782 if (c != NULL && c->ic_freq == freq && 1783 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 1784 return c; 1785 /* brute force search */ 1786 return (findchannel(ic->ic_channels, ic->ic_nchans, freq, flags)); 1787 } 1788 1789 /* 1790 * Locate a channel given a channel number+flags. We cache 1791 * the previous lookup to optimize switching between two 1792 * channels--as happens with dynamic turbo. 1793 */ 1794 struct ieee80211_channel * 1795 ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags) 1796 { 1797 struct ieee80211_channel *c; 1798 int i; 1799 1800 flags &= IEEE80211_CHAN_ALLTURBO; 1801 c = ic->ic_prevchan; 1802 if (c != NULL && c->ic_ieee == ieee && 1803 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 1804 return c; 1805 /* brute force search */ 1806 for (i = 0; i < ic->ic_nchans; i++) { 1807 c = &ic->ic_channels[i]; 1808 if (c->ic_ieee == ieee && 1809 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 1810 return c; 1811 } 1812 return NULL; 1813 } 1814 1815 /* 1816 * Lookup a channel suitable for the given rx status. 1817 * 1818 * This is used to find a channel for a frame (eg beacon, probe 1819 * response) based purely on the received PHY information. 1820 * 1821 * For now it tries to do it based on R_FREQ / R_IEEE. 1822 * This is enough for 11bg and 11a (and thus 11ng/11na) 1823 * but it will not be enough for GSM, PSB channels and the 1824 * like. It also doesn't know about legacy-turbog and 1825 * legacy-turbo modes, which some offload NICs actually 1826 * support in weird ways. 1827 * 1828 * Takes the ic and rxstatus; returns the channel or NULL 1829 * if not found. 1830 * 1831 * XXX TODO: Add support for that when the need arises. 1832 */ 1833 struct ieee80211_channel * 1834 ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap, 1835 const struct ieee80211_rx_stats *rxs) 1836 { 1837 struct ieee80211com *ic = vap->iv_ic; 1838 uint32_t flags; 1839 struct ieee80211_channel *c; 1840 1841 if (rxs == NULL) 1842 return (NULL); 1843 1844 /* 1845 * Strictly speaking we only use freq for now, 1846 * however later on we may wish to just store 1847 * the ieee for verification. 1848 */ 1849 if ((rxs->r_flags & IEEE80211_R_FREQ) == 0) 1850 return (NULL); 1851 if ((rxs->r_flags & IEEE80211_R_IEEE) == 0) 1852 return (NULL); 1853 if ((rxs->r_flags & IEEE80211_R_BAND) == 0) 1854 return (NULL); 1855 1856 /* 1857 * If the rx status contains a valid ieee/freq, then 1858 * ensure we populate the correct channel information 1859 * in rxchan before passing it up to the scan infrastructure. 1860 * Offload NICs will pass up beacons from all channels 1861 * during background scans. 1862 */ 1863 1864 /* Determine a band */ 1865 switch (rxs->c_band) { 1866 case IEEE80211_CHAN_2GHZ: 1867 flags = IEEE80211_CHAN_G; 1868 break; 1869 case IEEE80211_CHAN_5GHZ: 1870 flags = IEEE80211_CHAN_A; 1871 break; 1872 default: 1873 if (rxs->c_freq < 3000) { 1874 flags = IEEE80211_CHAN_G; 1875 } else { 1876 flags = IEEE80211_CHAN_A; 1877 } 1878 break; 1879 } 1880 1881 /* Channel lookup */ 1882 c = ieee80211_find_channel(ic, rxs->c_freq, flags); 1883 1884 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT, 1885 "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n", 1886 __func__, (int) rxs->c_freq, (int) rxs->c_ieee, flags, c); 1887 1888 return (c); 1889 } 1890 1891 static void 1892 addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword) 1893 { 1894 #define ADD(_ic, _s, _o) \ 1895 ifmedia_add(media, \ 1896 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL) 1897 static const u_int mopts[IEEE80211_MODE_MAX] = { 1898 [IEEE80211_MODE_AUTO] = IFM_AUTO, 1899 [IEEE80211_MODE_11A] = IFM_IEEE80211_11A, 1900 [IEEE80211_MODE_11B] = IFM_IEEE80211_11B, 1901 [IEEE80211_MODE_11G] = IFM_IEEE80211_11G, 1902 [IEEE80211_MODE_FH] = IFM_IEEE80211_FH, 1903 [IEEE80211_MODE_TURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO, 1904 [IEEE80211_MODE_TURBO_G] = IFM_IEEE80211_11G|IFM_IEEE80211_TURBO, 1905 [IEEE80211_MODE_STURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO, 1906 [IEEE80211_MODE_HALF] = IFM_IEEE80211_11A, /* XXX */ 1907 [IEEE80211_MODE_QUARTER] = IFM_IEEE80211_11A, /* XXX */ 1908 [IEEE80211_MODE_11NA] = IFM_IEEE80211_11NA, 1909 [IEEE80211_MODE_11NG] = IFM_IEEE80211_11NG, 1910 [IEEE80211_MODE_VHT_2GHZ] = IFM_IEEE80211_VHT2G, 1911 [IEEE80211_MODE_VHT_5GHZ] = IFM_IEEE80211_VHT5G, 1912 }; 1913 u_int mopt; 1914 1915 mopt = mopts[mode]; 1916 if (addsta) 1917 ADD(ic, mword, mopt); /* STA mode has no cap */ 1918 if (caps & IEEE80211_C_IBSS) 1919 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC); 1920 if (caps & IEEE80211_C_HOSTAP) 1921 ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP); 1922 if (caps & IEEE80211_C_AHDEMO) 1923 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0); 1924 if (caps & IEEE80211_C_MONITOR) 1925 ADD(media, mword, mopt | IFM_IEEE80211_MONITOR); 1926 if (caps & IEEE80211_C_WDS) 1927 ADD(media, mword, mopt | IFM_IEEE80211_WDS); 1928 if (caps & IEEE80211_C_MBSS) 1929 ADD(media, mword, mopt | IFM_IEEE80211_MBSS); 1930 #undef ADD 1931 } 1932 1933 /* 1934 * Setup the media data structures according to the channel and 1935 * rate tables. 1936 */ 1937 static int 1938 ieee80211_media_setup(struct ieee80211com *ic, 1939 struct ifmedia *media, int caps, int addsta, 1940 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat) 1941 { 1942 int i, j, rate, maxrate, mword, r; 1943 enum ieee80211_phymode mode; 1944 const struct ieee80211_rateset *rs; 1945 struct ieee80211_rateset allrates; 1946 1947 /* 1948 * Fill in media characteristics. 1949 */ 1950 ifmedia_init(media, 0, media_change, media_stat); 1951 maxrate = 0; 1952 /* 1953 * Add media for legacy operating modes. 1954 */ 1955 memset(&allrates, 0, sizeof(allrates)); 1956 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) { 1957 if (isclr(ic->ic_modecaps, mode)) 1958 continue; 1959 addmedia(media, caps, addsta, mode, IFM_AUTO); 1960 if (mode == IEEE80211_MODE_AUTO) 1961 continue; 1962 rs = &ic->ic_sup_rates[mode]; 1963 for (i = 0; i < rs->rs_nrates; i++) { 1964 rate = rs->rs_rates[i]; 1965 mword = ieee80211_rate2media(ic, rate, mode); 1966 if (mword == 0) 1967 continue; 1968 addmedia(media, caps, addsta, mode, mword); 1969 /* 1970 * Add legacy rate to the collection of all rates. 1971 */ 1972 r = rate & IEEE80211_RATE_VAL; 1973 for (j = 0; j < allrates.rs_nrates; j++) 1974 if (allrates.rs_rates[j] == r) 1975 break; 1976 if (j == allrates.rs_nrates) { 1977 /* unique, add to the set */ 1978 allrates.rs_rates[j] = r; 1979 allrates.rs_nrates++; 1980 } 1981 rate = (rate & IEEE80211_RATE_VAL) / 2; 1982 if (rate > maxrate) 1983 maxrate = rate; 1984 } 1985 } 1986 for (i = 0; i < allrates.rs_nrates; i++) { 1987 mword = ieee80211_rate2media(ic, allrates.rs_rates[i], 1988 IEEE80211_MODE_AUTO); 1989 if (mword == 0) 1990 continue; 1991 /* NB: remove media options from mword */ 1992 addmedia(media, caps, addsta, 1993 IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword)); 1994 } 1995 /* 1996 * Add HT/11n media. Note that we do not have enough 1997 * bits in the media subtype to express the MCS so we 1998 * use a "placeholder" media subtype and any fixed MCS 1999 * must be specified with a different mechanism. 2000 */ 2001 for (; mode <= IEEE80211_MODE_11NG; mode++) { 2002 if (isclr(ic->ic_modecaps, mode)) 2003 continue; 2004 addmedia(media, caps, addsta, mode, IFM_AUTO); 2005 addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS); 2006 } 2007 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) || 2008 isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) { 2009 addmedia(media, caps, addsta, 2010 IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS); 2011 i = ic->ic_txstream * 8 - 1; 2012 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) && 2013 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)) 2014 rate = ieee80211_htrates[i].ht40_rate_400ns; 2015 else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40)) 2016 rate = ieee80211_htrates[i].ht40_rate_800ns; 2017 else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20)) 2018 rate = ieee80211_htrates[i].ht20_rate_400ns; 2019 else 2020 rate = ieee80211_htrates[i].ht20_rate_800ns; 2021 if (rate > maxrate) 2022 maxrate = rate; 2023 } 2024 2025 /* 2026 * Add VHT media. 2027 * XXX-BZ skip "VHT_2GHZ" for now. 2028 */ 2029 for (mode = IEEE80211_MODE_VHT_5GHZ; mode <= IEEE80211_MODE_VHT_5GHZ; 2030 mode++) { 2031 if (isclr(ic->ic_modecaps, mode)) 2032 continue; 2033 addmedia(media, caps, addsta, mode, IFM_AUTO); 2034 addmedia(media, caps, addsta, mode, IFM_IEEE80211_VHT); 2035 } 2036 if (isset(ic->ic_modecaps, IEEE80211_MODE_VHT_5GHZ)) { 2037 addmedia(media, caps, addsta, 2038 IEEE80211_MODE_AUTO, IFM_IEEE80211_VHT); 2039 2040 /* XXX TODO: VHT maxrate */ 2041 } 2042 2043 return maxrate; 2044 } 2045 2046 /* XXX inline or eliminate? */ 2047 const struct ieee80211_rateset * 2048 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c) 2049 { 2050 /* XXX does this work for 11ng basic rates? */ 2051 return &ic->ic_sup_rates[ieee80211_chan2mode(c)]; 2052 } 2053 2054 /* XXX inline or eliminate? */ 2055 const struct ieee80211_htrateset * 2056 ieee80211_get_suphtrates(struct ieee80211com *ic, 2057 const struct ieee80211_channel *c) 2058 { 2059 return &ic->ic_sup_htrates; 2060 } 2061 2062 void 2063 ieee80211_announce(struct ieee80211com *ic) 2064 { 2065 int i, rate, mword; 2066 enum ieee80211_phymode mode; 2067 const struct ieee80211_rateset *rs; 2068 2069 /* NB: skip AUTO since it has no rates */ 2070 for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) { 2071 if (isclr(ic->ic_modecaps, mode)) 2072 continue; 2073 ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]); 2074 rs = &ic->ic_sup_rates[mode]; 2075 for (i = 0; i < rs->rs_nrates; i++) { 2076 mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode); 2077 if (mword == 0) 2078 continue; 2079 rate = ieee80211_media2rate(mword); 2080 printf("%s%d%sMbps", (i != 0 ? " " : ""), 2081 rate / 2, ((rate & 0x1) != 0 ? ".5" : "")); 2082 } 2083 printf("\n"); 2084 } 2085 ieee80211_ht_announce(ic); 2086 ieee80211_vht_announce(ic); 2087 } 2088 2089 void 2090 ieee80211_announce_channels(struct ieee80211com *ic) 2091 { 2092 const struct ieee80211_channel *c; 2093 char type; 2094 int i, cw; 2095 2096 printf("Chan Freq CW RegPwr MinPwr MaxPwr\n"); 2097 for (i = 0; i < ic->ic_nchans; i++) { 2098 c = &ic->ic_channels[i]; 2099 if (IEEE80211_IS_CHAN_ST(c)) 2100 type = 'S'; 2101 else if (IEEE80211_IS_CHAN_108A(c)) 2102 type = 'T'; 2103 else if (IEEE80211_IS_CHAN_108G(c)) 2104 type = 'G'; 2105 else if (IEEE80211_IS_CHAN_HT(c)) 2106 type = 'n'; 2107 else if (IEEE80211_IS_CHAN_A(c)) 2108 type = 'a'; 2109 else if (IEEE80211_IS_CHAN_ANYG(c)) 2110 type = 'g'; 2111 else if (IEEE80211_IS_CHAN_B(c)) 2112 type = 'b'; 2113 else 2114 type = 'f'; 2115 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c)) 2116 cw = 40; 2117 else if (IEEE80211_IS_CHAN_HALF(c)) 2118 cw = 10; 2119 else if (IEEE80211_IS_CHAN_QUARTER(c)) 2120 cw = 5; 2121 else 2122 cw = 20; 2123 printf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n" 2124 , c->ic_ieee, c->ic_freq, type 2125 , cw 2126 , IEEE80211_IS_CHAN_HT40U(c) ? '+' : 2127 IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' ' 2128 , c->ic_maxregpower 2129 , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0 2130 , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0 2131 ); 2132 } 2133 } 2134 2135 static int 2136 media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode) 2137 { 2138 switch (IFM_MODE(ime->ifm_media)) { 2139 case IFM_IEEE80211_11A: 2140 *mode = IEEE80211_MODE_11A; 2141 break; 2142 case IFM_IEEE80211_11B: 2143 *mode = IEEE80211_MODE_11B; 2144 break; 2145 case IFM_IEEE80211_11G: 2146 *mode = IEEE80211_MODE_11G; 2147 break; 2148 case IFM_IEEE80211_FH: 2149 *mode = IEEE80211_MODE_FH; 2150 break; 2151 case IFM_IEEE80211_11NA: 2152 *mode = IEEE80211_MODE_11NA; 2153 break; 2154 case IFM_IEEE80211_11NG: 2155 *mode = IEEE80211_MODE_11NG; 2156 break; 2157 case IFM_IEEE80211_VHT2G: 2158 *mode = IEEE80211_MODE_VHT_2GHZ; 2159 break; 2160 case IFM_IEEE80211_VHT5G: 2161 *mode = IEEE80211_MODE_VHT_5GHZ; 2162 break; 2163 case IFM_AUTO: 2164 *mode = IEEE80211_MODE_AUTO; 2165 break; 2166 default: 2167 return 0; 2168 } 2169 /* 2170 * Turbo mode is an ``option''. 2171 * XXX does not apply to AUTO 2172 */ 2173 if (ime->ifm_media & IFM_IEEE80211_TURBO) { 2174 if (*mode == IEEE80211_MODE_11A) { 2175 if (flags & IEEE80211_F_TURBOP) 2176 *mode = IEEE80211_MODE_TURBO_A; 2177 else 2178 *mode = IEEE80211_MODE_STURBO_A; 2179 } else if (*mode == IEEE80211_MODE_11G) 2180 *mode = IEEE80211_MODE_TURBO_G; 2181 else 2182 return 0; 2183 } 2184 /* XXX HT40 +/- */ 2185 return 1; 2186 } 2187 2188 /* 2189 * Handle a media change request on the vap interface. 2190 */ 2191 int 2192 ieee80211_media_change(struct ifnet *ifp) 2193 { 2194 struct ieee80211vap *vap = ifp->if_softc; 2195 struct ifmedia_entry *ime = vap->iv_media.ifm_cur; 2196 uint16_t newmode; 2197 2198 if (!media2mode(ime, vap->iv_flags, &newmode)) 2199 return EINVAL; 2200 if (vap->iv_des_mode != newmode) { 2201 vap->iv_des_mode = newmode; 2202 /* XXX kick state machine if up+running */ 2203 } 2204 return 0; 2205 } 2206 2207 /* 2208 * Common code to calculate the media status word 2209 * from the operating mode and channel state. 2210 */ 2211 static int 2212 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan) 2213 { 2214 int status; 2215 2216 status = IFM_IEEE80211; 2217 switch (opmode) { 2218 case IEEE80211_M_STA: 2219 break; 2220 case IEEE80211_M_IBSS: 2221 status |= IFM_IEEE80211_ADHOC; 2222 break; 2223 case IEEE80211_M_HOSTAP: 2224 status |= IFM_IEEE80211_HOSTAP; 2225 break; 2226 case IEEE80211_M_MONITOR: 2227 status |= IFM_IEEE80211_MONITOR; 2228 break; 2229 case IEEE80211_M_AHDEMO: 2230 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0; 2231 break; 2232 case IEEE80211_M_WDS: 2233 status |= IFM_IEEE80211_WDS; 2234 break; 2235 case IEEE80211_M_MBSS: 2236 status |= IFM_IEEE80211_MBSS; 2237 break; 2238 } 2239 if (IEEE80211_IS_CHAN_VHT_5GHZ(chan)) { 2240 status |= IFM_IEEE80211_VHT5G; 2241 } else if (IEEE80211_IS_CHAN_VHT_2GHZ(chan)) { 2242 status |= IFM_IEEE80211_VHT2G; 2243 } else if (IEEE80211_IS_CHAN_HTA(chan)) { 2244 status |= IFM_IEEE80211_11NA; 2245 } else if (IEEE80211_IS_CHAN_HTG(chan)) { 2246 status |= IFM_IEEE80211_11NG; 2247 } else if (IEEE80211_IS_CHAN_A(chan)) { 2248 status |= IFM_IEEE80211_11A; 2249 } else if (IEEE80211_IS_CHAN_B(chan)) { 2250 status |= IFM_IEEE80211_11B; 2251 } else if (IEEE80211_IS_CHAN_ANYG(chan)) { 2252 status |= IFM_IEEE80211_11G; 2253 } else if (IEEE80211_IS_CHAN_FHSS(chan)) { 2254 status |= IFM_IEEE80211_FH; 2255 } 2256 /* XXX else complain? */ 2257 2258 if (IEEE80211_IS_CHAN_TURBO(chan)) 2259 status |= IFM_IEEE80211_TURBO; 2260 #if 0 2261 if (IEEE80211_IS_CHAN_HT20(chan)) 2262 status |= IFM_IEEE80211_HT20; 2263 if (IEEE80211_IS_CHAN_HT40(chan)) 2264 status |= IFM_IEEE80211_HT40; 2265 #endif 2266 return status; 2267 } 2268 2269 void 2270 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr) 2271 { 2272 struct ieee80211vap *vap = ifp->if_softc; 2273 struct ieee80211com *ic = vap->iv_ic; 2274 enum ieee80211_phymode mode; 2275 2276 imr->ifm_status = IFM_AVALID; 2277 /* 2278 * NB: use the current channel's mode to lock down a xmit 2279 * rate only when running; otherwise we may have a mismatch 2280 * in which case the rate will not be convertible. 2281 */ 2282 if (vap->iv_state == IEEE80211_S_RUN || 2283 vap->iv_state == IEEE80211_S_SLEEP) { 2284 imr->ifm_status |= IFM_ACTIVE; 2285 mode = ieee80211_chan2mode(ic->ic_curchan); 2286 } else 2287 mode = IEEE80211_MODE_AUTO; 2288 imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan); 2289 /* 2290 * Calculate a current rate if possible. 2291 */ 2292 if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) { 2293 /* 2294 * A fixed rate is set, report that. 2295 */ 2296 imr->ifm_active |= ieee80211_rate2media(ic, 2297 vap->iv_txparms[mode].ucastrate, mode); 2298 } else if (vap->iv_opmode == IEEE80211_M_STA) { 2299 /* 2300 * In station mode report the current transmit rate. 2301 */ 2302 imr->ifm_active |= ieee80211_rate2media(ic, 2303 vap->iv_bss->ni_txrate, mode); 2304 } else 2305 imr->ifm_active |= IFM_AUTO; 2306 if (imr->ifm_status & IFM_ACTIVE) 2307 imr->ifm_current = imr->ifm_active; 2308 } 2309 2310 /* 2311 * Set the current phy mode and recalculate the active channel 2312 * set based on the available channels for this mode. Also 2313 * select a new default/current channel if the current one is 2314 * inappropriate for this mode. 2315 */ 2316 int 2317 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode) 2318 { 2319 /* 2320 * Adjust basic rates in 11b/11g supported rate set. 2321 * Note that if operating on a hal/quarter rate channel 2322 * this is a noop as those rates sets are different 2323 * and used instead. 2324 */ 2325 if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B) 2326 ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode); 2327 2328 ic->ic_curmode = mode; 2329 ieee80211_reset_erp(ic); /* reset global ERP state */ 2330 2331 return 0; 2332 } 2333 2334 /* 2335 * Return the phy mode for with the specified channel. 2336 */ 2337 enum ieee80211_phymode 2338 ieee80211_chan2mode(const struct ieee80211_channel *chan) 2339 { 2340 2341 if (IEEE80211_IS_CHAN_VHT_2GHZ(chan)) 2342 return IEEE80211_MODE_VHT_2GHZ; 2343 else if (IEEE80211_IS_CHAN_VHT_5GHZ(chan)) 2344 return IEEE80211_MODE_VHT_5GHZ; 2345 else if (IEEE80211_IS_CHAN_HTA(chan)) 2346 return IEEE80211_MODE_11NA; 2347 else if (IEEE80211_IS_CHAN_HTG(chan)) 2348 return IEEE80211_MODE_11NG; 2349 else if (IEEE80211_IS_CHAN_108G(chan)) 2350 return IEEE80211_MODE_TURBO_G; 2351 else if (IEEE80211_IS_CHAN_ST(chan)) 2352 return IEEE80211_MODE_STURBO_A; 2353 else if (IEEE80211_IS_CHAN_TURBO(chan)) 2354 return IEEE80211_MODE_TURBO_A; 2355 else if (IEEE80211_IS_CHAN_HALF(chan)) 2356 return IEEE80211_MODE_HALF; 2357 else if (IEEE80211_IS_CHAN_QUARTER(chan)) 2358 return IEEE80211_MODE_QUARTER; 2359 else if (IEEE80211_IS_CHAN_A(chan)) 2360 return IEEE80211_MODE_11A; 2361 else if (IEEE80211_IS_CHAN_ANYG(chan)) 2362 return IEEE80211_MODE_11G; 2363 else if (IEEE80211_IS_CHAN_B(chan)) 2364 return IEEE80211_MODE_11B; 2365 else if (IEEE80211_IS_CHAN_FHSS(chan)) 2366 return IEEE80211_MODE_FH; 2367 2368 /* NB: should not get here */ 2369 printf("%s: cannot map channel to mode; freq %u flags 0x%x\n", 2370 __func__, chan->ic_freq, chan->ic_flags); 2371 return IEEE80211_MODE_11B; 2372 } 2373 2374 struct ratemedia { 2375 u_int match; /* rate + mode */ 2376 u_int media; /* if_media rate */ 2377 }; 2378 2379 static int 2380 findmedia(const struct ratemedia rates[], int n, u_int match) 2381 { 2382 int i; 2383 2384 for (i = 0; i < n; i++) 2385 if (rates[i].match == match) 2386 return rates[i].media; 2387 return IFM_AUTO; 2388 } 2389 2390 /* 2391 * Convert IEEE80211 rate value to ifmedia subtype. 2392 * Rate is either a legacy rate in units of 0.5Mbps 2393 * or an MCS index. 2394 */ 2395 int 2396 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode) 2397 { 2398 static const struct ratemedia rates[] = { 2399 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 }, 2400 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 }, 2401 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 }, 2402 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 }, 2403 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 }, 2404 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 }, 2405 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 }, 2406 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 }, 2407 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 }, 2408 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 }, 2409 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 }, 2410 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 }, 2411 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 }, 2412 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 }, 2413 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 }, 2414 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 }, 2415 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 }, 2416 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 }, 2417 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 }, 2418 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 }, 2419 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 }, 2420 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 }, 2421 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 }, 2422 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 }, 2423 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 }, 2424 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 }, 2425 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 }, 2426 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 }, 2427 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 }, 2428 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 }, 2429 /* NB: OFDM72 doesn't really exist so we don't handle it */ 2430 }; 2431 static const struct ratemedia htrates[] = { 2432 { 0, IFM_IEEE80211_MCS }, 2433 { 1, IFM_IEEE80211_MCS }, 2434 { 2, IFM_IEEE80211_MCS }, 2435 { 3, IFM_IEEE80211_MCS }, 2436 { 4, IFM_IEEE80211_MCS }, 2437 { 5, IFM_IEEE80211_MCS }, 2438 { 6, IFM_IEEE80211_MCS }, 2439 { 7, IFM_IEEE80211_MCS }, 2440 { 8, IFM_IEEE80211_MCS }, 2441 { 9, IFM_IEEE80211_MCS }, 2442 { 10, IFM_IEEE80211_MCS }, 2443 { 11, IFM_IEEE80211_MCS }, 2444 { 12, IFM_IEEE80211_MCS }, 2445 { 13, IFM_IEEE80211_MCS }, 2446 { 14, IFM_IEEE80211_MCS }, 2447 { 15, IFM_IEEE80211_MCS }, 2448 { 16, IFM_IEEE80211_MCS }, 2449 { 17, IFM_IEEE80211_MCS }, 2450 { 18, IFM_IEEE80211_MCS }, 2451 { 19, IFM_IEEE80211_MCS }, 2452 { 20, IFM_IEEE80211_MCS }, 2453 { 21, IFM_IEEE80211_MCS }, 2454 { 22, IFM_IEEE80211_MCS }, 2455 { 23, IFM_IEEE80211_MCS }, 2456 { 24, IFM_IEEE80211_MCS }, 2457 { 25, IFM_IEEE80211_MCS }, 2458 { 26, IFM_IEEE80211_MCS }, 2459 { 27, IFM_IEEE80211_MCS }, 2460 { 28, IFM_IEEE80211_MCS }, 2461 { 29, IFM_IEEE80211_MCS }, 2462 { 30, IFM_IEEE80211_MCS }, 2463 { 31, IFM_IEEE80211_MCS }, 2464 { 32, IFM_IEEE80211_MCS }, 2465 { 33, IFM_IEEE80211_MCS }, 2466 { 34, IFM_IEEE80211_MCS }, 2467 { 35, IFM_IEEE80211_MCS }, 2468 { 36, IFM_IEEE80211_MCS }, 2469 { 37, IFM_IEEE80211_MCS }, 2470 { 38, IFM_IEEE80211_MCS }, 2471 { 39, IFM_IEEE80211_MCS }, 2472 { 40, IFM_IEEE80211_MCS }, 2473 { 41, IFM_IEEE80211_MCS }, 2474 { 42, IFM_IEEE80211_MCS }, 2475 { 43, IFM_IEEE80211_MCS }, 2476 { 44, IFM_IEEE80211_MCS }, 2477 { 45, IFM_IEEE80211_MCS }, 2478 { 46, IFM_IEEE80211_MCS }, 2479 { 47, IFM_IEEE80211_MCS }, 2480 { 48, IFM_IEEE80211_MCS }, 2481 { 49, IFM_IEEE80211_MCS }, 2482 { 50, IFM_IEEE80211_MCS }, 2483 { 51, IFM_IEEE80211_MCS }, 2484 { 52, IFM_IEEE80211_MCS }, 2485 { 53, IFM_IEEE80211_MCS }, 2486 { 54, IFM_IEEE80211_MCS }, 2487 { 55, IFM_IEEE80211_MCS }, 2488 { 56, IFM_IEEE80211_MCS }, 2489 { 57, IFM_IEEE80211_MCS }, 2490 { 58, IFM_IEEE80211_MCS }, 2491 { 59, IFM_IEEE80211_MCS }, 2492 { 60, IFM_IEEE80211_MCS }, 2493 { 61, IFM_IEEE80211_MCS }, 2494 { 62, IFM_IEEE80211_MCS }, 2495 { 63, IFM_IEEE80211_MCS }, 2496 { 64, IFM_IEEE80211_MCS }, 2497 { 65, IFM_IEEE80211_MCS }, 2498 { 66, IFM_IEEE80211_MCS }, 2499 { 67, IFM_IEEE80211_MCS }, 2500 { 68, IFM_IEEE80211_MCS }, 2501 { 69, IFM_IEEE80211_MCS }, 2502 { 70, IFM_IEEE80211_MCS }, 2503 { 71, IFM_IEEE80211_MCS }, 2504 { 72, IFM_IEEE80211_MCS }, 2505 { 73, IFM_IEEE80211_MCS }, 2506 { 74, IFM_IEEE80211_MCS }, 2507 { 75, IFM_IEEE80211_MCS }, 2508 { 76, IFM_IEEE80211_MCS }, 2509 }; 2510 static const struct ratemedia vhtrates[] = { 2511 { 0, IFM_IEEE80211_VHT }, 2512 { 1, IFM_IEEE80211_VHT }, 2513 { 2, IFM_IEEE80211_VHT }, 2514 { 3, IFM_IEEE80211_VHT }, 2515 { 4, IFM_IEEE80211_VHT }, 2516 { 5, IFM_IEEE80211_VHT }, 2517 { 6, IFM_IEEE80211_VHT }, 2518 { 7, IFM_IEEE80211_VHT }, 2519 { 8, IFM_IEEE80211_VHT }, /* Optional. */ 2520 { 9, IFM_IEEE80211_VHT }, /* Optional. */ 2521 #if 0 2522 /* Some QCA and BRCM seem to support this; offspec. */ 2523 { 10, IFM_IEEE80211_VHT }, 2524 { 11, IFM_IEEE80211_VHT }, 2525 #endif 2526 }; 2527 int m; 2528 2529 /* 2530 * Check 11ac/11n rates first for match as an MCS. 2531 */ 2532 if (mode == IEEE80211_MODE_VHT_5GHZ) { 2533 if (rate & IFM_IEEE80211_VHT) { 2534 rate &= ~IFM_IEEE80211_VHT; 2535 m = findmedia(vhtrates, nitems(vhtrates), rate); 2536 if (m != IFM_AUTO) 2537 return (m | IFM_IEEE80211_VHT); 2538 } 2539 } else if (mode == IEEE80211_MODE_11NA) { 2540 if (rate & IEEE80211_RATE_MCS) { 2541 rate &= ~IEEE80211_RATE_MCS; 2542 m = findmedia(htrates, nitems(htrates), rate); 2543 if (m != IFM_AUTO) 2544 return m | IFM_IEEE80211_11NA; 2545 } 2546 } else if (mode == IEEE80211_MODE_11NG) { 2547 /* NB: 12 is ambiguous, it will be treated as an MCS */ 2548 if (rate & IEEE80211_RATE_MCS) { 2549 rate &= ~IEEE80211_RATE_MCS; 2550 m = findmedia(htrates, nitems(htrates), rate); 2551 if (m != IFM_AUTO) 2552 return m | IFM_IEEE80211_11NG; 2553 } 2554 } 2555 rate &= IEEE80211_RATE_VAL; 2556 switch (mode) { 2557 case IEEE80211_MODE_11A: 2558 case IEEE80211_MODE_HALF: /* XXX good 'nuf */ 2559 case IEEE80211_MODE_QUARTER: 2560 case IEEE80211_MODE_11NA: 2561 case IEEE80211_MODE_TURBO_A: 2562 case IEEE80211_MODE_STURBO_A: 2563 return findmedia(rates, nitems(rates), 2564 rate | IFM_IEEE80211_11A); 2565 case IEEE80211_MODE_11B: 2566 return findmedia(rates, nitems(rates), 2567 rate | IFM_IEEE80211_11B); 2568 case IEEE80211_MODE_FH: 2569 return findmedia(rates, nitems(rates), 2570 rate | IFM_IEEE80211_FH); 2571 case IEEE80211_MODE_AUTO: 2572 /* NB: ic may be NULL for some drivers */ 2573 if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH) 2574 return findmedia(rates, nitems(rates), 2575 rate | IFM_IEEE80211_FH); 2576 /* NB: hack, 11g matches both 11b+11a rates */ 2577 /* fall thru... */ 2578 case IEEE80211_MODE_11G: 2579 case IEEE80211_MODE_11NG: 2580 case IEEE80211_MODE_TURBO_G: 2581 return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G); 2582 case IEEE80211_MODE_VHT_2GHZ: 2583 case IEEE80211_MODE_VHT_5GHZ: 2584 /* XXX TODO: need to figure out mapping for VHT rates */ 2585 return IFM_AUTO; 2586 } 2587 return IFM_AUTO; 2588 } 2589 2590 int 2591 ieee80211_media2rate(int mword) 2592 { 2593 static const int ieeerates[] = { 2594 -1, /* IFM_AUTO */ 2595 0, /* IFM_MANUAL */ 2596 0, /* IFM_NONE */ 2597 2, /* IFM_IEEE80211_FH1 */ 2598 4, /* IFM_IEEE80211_FH2 */ 2599 2, /* IFM_IEEE80211_DS1 */ 2600 4, /* IFM_IEEE80211_DS2 */ 2601 11, /* IFM_IEEE80211_DS5 */ 2602 22, /* IFM_IEEE80211_DS11 */ 2603 44, /* IFM_IEEE80211_DS22 */ 2604 12, /* IFM_IEEE80211_OFDM6 */ 2605 18, /* IFM_IEEE80211_OFDM9 */ 2606 24, /* IFM_IEEE80211_OFDM12 */ 2607 36, /* IFM_IEEE80211_OFDM18 */ 2608 48, /* IFM_IEEE80211_OFDM24 */ 2609 72, /* IFM_IEEE80211_OFDM36 */ 2610 96, /* IFM_IEEE80211_OFDM48 */ 2611 108, /* IFM_IEEE80211_OFDM54 */ 2612 144, /* IFM_IEEE80211_OFDM72 */ 2613 0, /* IFM_IEEE80211_DS354k */ 2614 0, /* IFM_IEEE80211_DS512k */ 2615 6, /* IFM_IEEE80211_OFDM3 */ 2616 9, /* IFM_IEEE80211_OFDM4 */ 2617 54, /* IFM_IEEE80211_OFDM27 */ 2618 -1, /* IFM_IEEE80211_MCS */ 2619 -1, /* IFM_IEEE80211_VHT */ 2620 }; 2621 return IFM_SUBTYPE(mword) < nitems(ieeerates) ? 2622 ieeerates[IFM_SUBTYPE(mword)] : 0; 2623 } 2624 2625 /* 2626 * The following hash function is adapted from "Hash Functions" by Bob Jenkins 2627 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997). 2628 */ 2629 #define mix(a, b, c) \ 2630 do { \ 2631 a -= b; a -= c; a ^= (c >> 13); \ 2632 b -= c; b -= a; b ^= (a << 8); \ 2633 c -= a; c -= b; c ^= (b >> 13); \ 2634 a -= b; a -= c; a ^= (c >> 12); \ 2635 b -= c; b -= a; b ^= (a << 16); \ 2636 c -= a; c -= b; c ^= (b >> 5); \ 2637 a -= b; a -= c; a ^= (c >> 3); \ 2638 b -= c; b -= a; b ^= (a << 10); \ 2639 c -= a; c -= b; c ^= (b >> 15); \ 2640 } while (/*CONSTCOND*/0) 2641 2642 uint32_t 2643 ieee80211_mac_hash(const struct ieee80211com *ic, 2644 const uint8_t addr[IEEE80211_ADDR_LEN]) 2645 { 2646 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key; 2647 2648 b += addr[5] << 8; 2649 b += addr[4]; 2650 a += addr[3] << 24; 2651 a += addr[2] << 16; 2652 a += addr[1] << 8; 2653 a += addr[0]; 2654 2655 mix(a, b, c); 2656 2657 return c; 2658 } 2659 #undef mix 2660 2661 char 2662 ieee80211_channel_type_char(const struct ieee80211_channel *c) 2663 { 2664 if (IEEE80211_IS_CHAN_ST(c)) 2665 return 'S'; 2666 if (IEEE80211_IS_CHAN_108A(c)) 2667 return 'T'; 2668 if (IEEE80211_IS_CHAN_108G(c)) 2669 return 'G'; 2670 if (IEEE80211_IS_CHAN_VHT(c)) 2671 return 'v'; 2672 if (IEEE80211_IS_CHAN_HT(c)) 2673 return 'n'; 2674 if (IEEE80211_IS_CHAN_A(c)) 2675 return 'a'; 2676 if (IEEE80211_IS_CHAN_ANYG(c)) 2677 return 'g'; 2678 if (IEEE80211_IS_CHAN_B(c)) 2679 return 'b'; 2680 return 'f'; 2681 } 2682 2683 /* 2684 * Determine whether the given key in the given VAP is a global key. 2685 * (key index 0..3, shared between all stations on a VAP.) 2686 * 2687 * This is either a WEP key or a GROUP key. 2688 * 2689 * Note this will NOT return true if it is a IGTK key. 2690 */ 2691 bool 2692 ieee80211_is_key_global(const struct ieee80211vap *vap, 2693 const struct ieee80211_key *key) 2694 { 2695 return (&vap->iv_nw_keys[0] <= key && 2696 key < &vap->iv_nw_keys[IEEE80211_WEP_NKID]); 2697 } 2698 2699 /* 2700 * Determine whether the given key in the given VAP is a unicast key. 2701 */ 2702 bool 2703 ieee80211_is_key_unicast(const struct ieee80211vap *vap, 2704 const struct ieee80211_key *key) 2705 { 2706 /* 2707 * This is a short-cut for now; eventually we will need 2708 * to support multiple unicast keys, IGTK, etc) so we 2709 * will absolutely need to fix the key flags. 2710 */ 2711 return (!ieee80211_is_key_global(vap, key)); 2712 } 2713