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