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