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