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