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