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