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