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 731 /* XXX band-aid until ifnet handles this for us */ 732 taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 733 734 IEEE80211_LOCK(ic); 735 KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running")); 736 TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next); 737 ieee80211_syncflag_locked(ic, IEEE80211_F_WME); 738 #ifdef IEEE80211_SUPPORT_SUPERG 739 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP); 740 #endif 741 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF); 742 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST); 743 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT); 744 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40); 745 746 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT); 747 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40); 748 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80); 749 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80); 750 ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160); 751 752 /* NB: this handles the bpfdetach done below */ 753 ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF); 754 if (vap->iv_ifflags & IFF_PROMISC) 755 ieee80211_promisc(vap, false); 756 if (vap->iv_ifflags & IFF_ALLMULTI) 757 ieee80211_allmulti(vap, false); 758 IEEE80211_UNLOCK(ic); 759 760 ifmedia_removeall(&vap->iv_media); 761 762 ieee80211_radiotap_vdetach(vap); 763 ieee80211_regdomain_vdetach(vap); 764 ieee80211_scan_vdetach(vap); 765 #ifdef IEEE80211_SUPPORT_SUPERG 766 ieee80211_superg_vdetach(vap); 767 #endif 768 ieee80211_vht_vdetach(vap); 769 ieee80211_ht_vdetach(vap); 770 /* NB: must be before ieee80211_node_vdetach */ 771 ieee80211_proto_vdetach(vap); 772 ieee80211_crypto_vdetach(vap); 773 ieee80211_power_vdetach(vap); 774 ieee80211_node_vdetach(vap); 775 ieee80211_sysctl_vdetach(vap); 776 777 if_free(ifp); 778 779 CURVNET_RESTORE(); 780 } 781 782 /* 783 * Count number of vaps in promisc, and issue promisc on 784 * parent respectively. 785 */ 786 void 787 ieee80211_promisc(struct ieee80211vap *vap, bool on) 788 { 789 struct ieee80211com *ic = vap->iv_ic; 790 791 IEEE80211_LOCK_ASSERT(ic); 792 793 if (on) { 794 if (++ic->ic_promisc == 1) 795 ieee80211_runtask(ic, &ic->ic_promisc_task); 796 } else { 797 KASSERT(ic->ic_promisc > 0, ("%s: ic %p not promisc", 798 __func__, ic)); 799 if (--ic->ic_promisc == 0) 800 ieee80211_runtask(ic, &ic->ic_promisc_task); 801 } 802 } 803 804 /* 805 * Count number of vaps in allmulti, and issue allmulti on 806 * parent respectively. 807 */ 808 void 809 ieee80211_allmulti(struct ieee80211vap *vap, bool on) 810 { 811 struct ieee80211com *ic = vap->iv_ic; 812 813 IEEE80211_LOCK_ASSERT(ic); 814 815 if (on) { 816 if (++ic->ic_allmulti == 1) 817 ieee80211_runtask(ic, &ic->ic_mcast_task); 818 } else { 819 KASSERT(ic->ic_allmulti > 0, ("%s: ic %p not allmulti", 820 __func__, ic)); 821 if (--ic->ic_allmulti == 0) 822 ieee80211_runtask(ic, &ic->ic_mcast_task); 823 } 824 } 825 826 /* 827 * Synchronize flag bit state in the com structure 828 * according to the state of all vap's. This is used, 829 * for example, to handle state changes via ioctls. 830 */ 831 static void 832 ieee80211_syncflag_locked(struct ieee80211com *ic, int flag) 833 { 834 struct ieee80211vap *vap; 835 int bit; 836 837 IEEE80211_LOCK_ASSERT(ic); 838 839 bit = 0; 840 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 841 if (vap->iv_flags & flag) { 842 bit = 1; 843 break; 844 } 845 if (bit) 846 ic->ic_flags |= flag; 847 else 848 ic->ic_flags &= ~flag; 849 } 850 851 void 852 ieee80211_syncflag(struct ieee80211vap *vap, int flag) 853 { 854 struct ieee80211com *ic = vap->iv_ic; 855 856 IEEE80211_LOCK(ic); 857 if (flag < 0) { 858 flag = -flag; 859 vap->iv_flags &= ~flag; 860 } else 861 vap->iv_flags |= flag; 862 ieee80211_syncflag_locked(ic, flag); 863 IEEE80211_UNLOCK(ic); 864 } 865 866 /* 867 * Synchronize flags_ht bit state in the com structure 868 * according to the state of all vap's. This is used, 869 * for example, to handle state changes via ioctls. 870 */ 871 static void 872 ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag) 873 { 874 struct ieee80211vap *vap; 875 int bit; 876 877 IEEE80211_LOCK_ASSERT(ic); 878 879 bit = 0; 880 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 881 if (vap->iv_flags_ht & flag) { 882 bit = 1; 883 break; 884 } 885 if (bit) 886 ic->ic_flags_ht |= flag; 887 else 888 ic->ic_flags_ht &= ~flag; 889 } 890 891 void 892 ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag) 893 { 894 struct ieee80211com *ic = vap->iv_ic; 895 896 IEEE80211_LOCK(ic); 897 if (flag < 0) { 898 flag = -flag; 899 vap->iv_flags_ht &= ~flag; 900 } else 901 vap->iv_flags_ht |= flag; 902 ieee80211_syncflag_ht_locked(ic, flag); 903 IEEE80211_UNLOCK(ic); 904 } 905 906 /* 907 * Synchronize flags_vht bit state in the com structure 908 * according to the state of all vap's. This is used, 909 * for example, to handle state changes via ioctls. 910 */ 911 static void 912 ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag) 913 { 914 struct ieee80211vap *vap; 915 int bit; 916 917 IEEE80211_LOCK_ASSERT(ic); 918 919 bit = 0; 920 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 921 if (vap->iv_flags_vht & flag) { 922 bit = 1; 923 break; 924 } 925 if (bit) 926 ic->ic_flags_vht |= flag; 927 else 928 ic->ic_flags_vht &= ~flag; 929 } 930 931 void 932 ieee80211_syncflag_vht(struct ieee80211vap *vap, int flag) 933 { 934 struct ieee80211com *ic = vap->iv_ic; 935 936 IEEE80211_LOCK(ic); 937 if (flag < 0) { 938 flag = -flag; 939 vap->iv_flags_vht &= ~flag; 940 } else 941 vap->iv_flags_vht |= flag; 942 ieee80211_syncflag_vht_locked(ic, flag); 943 IEEE80211_UNLOCK(ic); 944 } 945 946 /* 947 * Synchronize flags_ext bit state in the com structure 948 * according to the state of all vap's. This is used, 949 * for example, to handle state changes via ioctls. 950 */ 951 static void 952 ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag) 953 { 954 struct ieee80211vap *vap; 955 int bit; 956 957 IEEE80211_LOCK_ASSERT(ic); 958 959 bit = 0; 960 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 961 if (vap->iv_flags_ext & flag) { 962 bit = 1; 963 break; 964 } 965 if (bit) 966 ic->ic_flags_ext |= flag; 967 else 968 ic->ic_flags_ext &= ~flag; 969 } 970 971 void 972 ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag) 973 { 974 struct ieee80211com *ic = vap->iv_ic; 975 976 IEEE80211_LOCK(ic); 977 if (flag < 0) { 978 flag = -flag; 979 vap->iv_flags_ext &= ~flag; 980 } else 981 vap->iv_flags_ext |= flag; 982 ieee80211_syncflag_ext_locked(ic, flag); 983 IEEE80211_UNLOCK(ic); 984 } 985 986 static __inline int 987 mapgsm(u_int freq, u_int flags) 988 { 989 freq *= 10; 990 if (flags & IEEE80211_CHAN_QUARTER) 991 freq += 5; 992 else if (flags & IEEE80211_CHAN_HALF) 993 freq += 10; 994 else 995 freq += 20; 996 /* NB: there is no 907/20 wide but leave room */ 997 return (freq - 906*10) / 5; 998 } 999 1000 static __inline int 1001 mappsb(u_int freq, u_int flags) 1002 { 1003 return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5; 1004 } 1005 1006 /* 1007 * Convert MHz frequency to IEEE channel number. 1008 */ 1009 int 1010 ieee80211_mhz2ieee(u_int freq, u_int flags) 1011 { 1012 #define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990) 1013 if (flags & IEEE80211_CHAN_GSM) 1014 return mapgsm(freq, flags); 1015 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 1016 if (freq == 2484) 1017 return 14; 1018 if (freq < 2484) 1019 return ((int) freq - 2407) / 5; 1020 else 1021 return 15 + ((freq - 2512) / 20); 1022 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */ 1023 if (freq <= 5000) { 1024 /* XXX check regdomain? */ 1025 if (IS_FREQ_IN_PSB(freq)) 1026 return mappsb(freq, flags); 1027 return (freq - 4000) / 5; 1028 } else 1029 return (freq - 5000) / 5; 1030 } else { /* either, guess */ 1031 if (freq == 2484) 1032 return 14; 1033 if (freq < 2484) { 1034 if (907 <= freq && freq <= 922) 1035 return mapgsm(freq, flags); 1036 return ((int) freq - 2407) / 5; 1037 } 1038 if (freq < 5000) { 1039 if (IS_FREQ_IN_PSB(freq)) 1040 return mappsb(freq, flags); 1041 else if (freq > 4900) 1042 return (freq - 4000) / 5; 1043 else 1044 return 15 + ((freq - 2512) / 20); 1045 } 1046 return (freq - 5000) / 5; 1047 } 1048 #undef IS_FREQ_IN_PSB 1049 } 1050 1051 /* 1052 * Convert channel to IEEE channel number. 1053 */ 1054 int 1055 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c) 1056 { 1057 if (c == NULL) { 1058 ic_printf(ic, "invalid channel (NULL)\n"); 1059 return 0; /* XXX */ 1060 } 1061 return (c == IEEE80211_CHAN_ANYC ? IEEE80211_CHAN_ANY : c->ic_ieee); 1062 } 1063 1064 /* 1065 * Convert IEEE channel number to MHz frequency. 1066 */ 1067 u_int 1068 ieee80211_ieee2mhz(u_int chan, u_int flags) 1069 { 1070 if (flags & IEEE80211_CHAN_GSM) 1071 return 907 + 5 * (chan / 10); 1072 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 1073 if (chan == 14) 1074 return 2484; 1075 if (chan < 14) 1076 return 2407 + chan*5; 1077 else 1078 return 2512 + ((chan-15)*20); 1079 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */ 1080 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) { 1081 chan -= 37; 1082 return 4940 + chan*5 + (chan % 5 ? 2 : 0); 1083 } 1084 return 5000 + (chan*5); 1085 } else { /* either, guess */ 1086 /* XXX can't distinguish PSB+GSM channels */ 1087 if (chan == 14) 1088 return 2484; 1089 if (chan < 14) /* 0-13 */ 1090 return 2407 + chan*5; 1091 if (chan < 27) /* 15-26 */ 1092 return 2512 + ((chan-15)*20); 1093 return 5000 + (chan*5); 1094 } 1095 } 1096 1097 static __inline void 1098 set_extchan(struct ieee80211_channel *c) 1099 { 1100 1101 /* 1102 * IEEE Std 802.11-2012, page 1738, subclause 20.3.15.4: 1103 * "the secondary channel number shall be 'N + [1,-1] * 4' 1104 */ 1105 if (c->ic_flags & IEEE80211_CHAN_HT40U) 1106 c->ic_extieee = c->ic_ieee + 4; 1107 else if (c->ic_flags & IEEE80211_CHAN_HT40D) 1108 c->ic_extieee = c->ic_ieee - 4; 1109 else 1110 c->ic_extieee = 0; 1111 } 1112 1113 /* 1114 * Populate the freq1/freq2 fields as appropriate for VHT channels. 1115 * 1116 * This for now uses a hard-coded list of 80MHz wide channels. 1117 * 1118 * For HT20/HT40, freq1 just is the centre frequency of the 40MHz 1119 * wide channel we've already decided upon. 1120 * 1121 * For VHT80 and VHT160, there are only a small number of fixed 1122 * 80/160MHz wide channels, so we just use those. 1123 * 1124 * This is all likely very very wrong - both the regulatory code 1125 * and this code needs to ensure that all four channels are 1126 * available and valid before the VHT80 (and eight for VHT160) channel 1127 * is created. 1128 */ 1129 1130 struct vht_chan_range { 1131 uint16_t freq_start; 1132 uint16_t freq_end; 1133 }; 1134 1135 struct vht_chan_range vht80_chan_ranges[] = { 1136 { 5170, 5250 }, 1137 { 5250, 5330 }, 1138 { 5490, 5570 }, 1139 { 5570, 5650 }, 1140 { 5650, 5730 }, 1141 { 5735, 5815 }, 1142 { 0, 0, } 1143 }; 1144 1145 static int 1146 set_vht_extchan(struct ieee80211_channel *c) 1147 { 1148 int i; 1149 1150 if (! IEEE80211_IS_CHAN_VHT(c)) { 1151 return (0); 1152 } 1153 1154 if (IEEE80211_IS_CHAN_VHT20(c)) { 1155 c->ic_vht_ch_freq1 = c->ic_ieee; 1156 return (1); 1157 } 1158 1159 if (IEEE80211_IS_CHAN_VHT40(c)) { 1160 if (IEEE80211_IS_CHAN_HT40U(c)) 1161 c->ic_vht_ch_freq1 = c->ic_ieee + 2; 1162 else if (IEEE80211_IS_CHAN_HT40D(c)) 1163 c->ic_vht_ch_freq1 = c->ic_ieee - 2; 1164 else 1165 return (0); 1166 return (1); 1167 } 1168 1169 if (IEEE80211_IS_CHAN_VHT80(c)) { 1170 for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) { 1171 if (c->ic_freq >= vht80_chan_ranges[i].freq_start && 1172 c->ic_freq < vht80_chan_ranges[i].freq_end) { 1173 int midpoint; 1174 1175 midpoint = vht80_chan_ranges[i].freq_start + 40; 1176 c->ic_vht_ch_freq1 = 1177 ieee80211_mhz2ieee(midpoint, c->ic_flags); 1178 c->ic_vht_ch_freq2 = 0; 1179 #if 0 1180 printf("%s: %d, freq=%d, midpoint=%d, freq1=%d, freq2=%d\n", 1181 __func__, c->ic_ieee, c->ic_freq, midpoint, 1182 c->ic_vht_ch_freq1, c->ic_vht_ch_freq2); 1183 #endif 1184 return (1); 1185 } 1186 } 1187 return (0); 1188 } 1189 1190 printf("%s: unknown VHT channel type (ieee=%d, flags=0x%08x)\n", 1191 __func__, 1192 c->ic_ieee, 1193 c->ic_flags); 1194 1195 return (0); 1196 } 1197 1198 /* 1199 * Return whether the current channel could possibly be a part of 1200 * a VHT80 channel. 1201 * 1202 * This doesn't check that the whole range is in the allowed list 1203 * according to regulatory. 1204 */ 1205 static int 1206 is_vht80_valid_freq(uint16_t freq) 1207 { 1208 int i; 1209 for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) { 1210 if (freq >= vht80_chan_ranges[i].freq_start && 1211 freq < vht80_chan_ranges[i].freq_end) 1212 return (1); 1213 } 1214 return (0); 1215 } 1216 1217 static int 1218 addchan(struct ieee80211_channel chans[], int maxchans, int *nchans, 1219 uint8_t ieee, uint16_t freq, int8_t maxregpower, uint32_t flags) 1220 { 1221 struct ieee80211_channel *c; 1222 1223 if (*nchans >= maxchans) 1224 return (ENOBUFS); 1225 1226 #if 0 1227 printf("%s: %d: ieee=%d, freq=%d, flags=0x%08x\n", 1228 __func__, 1229 *nchans, 1230 ieee, 1231 freq, 1232 flags); 1233 #endif 1234 1235 c = &chans[(*nchans)++]; 1236 c->ic_ieee = ieee; 1237 c->ic_freq = freq != 0 ? freq : ieee80211_ieee2mhz(ieee, flags); 1238 c->ic_maxregpower = maxregpower; 1239 c->ic_maxpower = 2 * maxregpower; 1240 c->ic_flags = flags; 1241 c->ic_vht_ch_freq1 = 0; 1242 c->ic_vht_ch_freq2 = 0; 1243 set_extchan(c); 1244 set_vht_extchan(c); 1245 1246 return (0); 1247 } 1248 1249 static int 1250 copychan_prev(struct ieee80211_channel chans[], int maxchans, int *nchans, 1251 uint32_t flags) 1252 { 1253 struct ieee80211_channel *c; 1254 1255 KASSERT(*nchans > 0, ("channel list is empty\n")); 1256 1257 if (*nchans >= maxchans) 1258 return (ENOBUFS); 1259 1260 #if 0 1261 printf("%s: %d: flags=0x%08x\n", 1262 __func__, 1263 *nchans, 1264 flags); 1265 #endif 1266 1267 c = &chans[(*nchans)++]; 1268 c[0] = c[-1]; 1269 c->ic_flags = flags; 1270 c->ic_vht_ch_freq1 = 0; 1271 c->ic_vht_ch_freq2 = 0; 1272 set_extchan(c); 1273 set_vht_extchan(c); 1274 1275 return (0); 1276 } 1277 1278 /* 1279 * XXX VHT-2GHz 1280 */ 1281 static void 1282 getflags_2ghz(const uint8_t bands[], uint32_t flags[], int ht40) 1283 { 1284 int nmodes; 1285 1286 nmodes = 0; 1287 if (isset(bands, IEEE80211_MODE_11B)) 1288 flags[nmodes++] = IEEE80211_CHAN_B; 1289 if (isset(bands, IEEE80211_MODE_11G)) 1290 flags[nmodes++] = IEEE80211_CHAN_G; 1291 if (isset(bands, IEEE80211_MODE_11NG)) 1292 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20; 1293 if (ht40) { 1294 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U; 1295 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D; 1296 } 1297 flags[nmodes] = 0; 1298 } 1299 1300 static void 1301 getflags_5ghz(const uint8_t bands[], uint32_t flags[], int ht40, int vht80) 1302 { 1303 int nmodes; 1304 1305 /* 1306 * the addchan_list function seems to expect the flags array to 1307 * be in channel width order, so the VHT bits are interspersed 1308 * as appropriate to maintain said order. 1309 * 1310 * It also assumes HT40U is before HT40D. 1311 */ 1312 nmodes = 0; 1313 1314 /* 20MHz */ 1315 if (isset(bands, IEEE80211_MODE_11A)) 1316 flags[nmodes++] = IEEE80211_CHAN_A; 1317 if (isset(bands, IEEE80211_MODE_11NA)) 1318 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20; 1319 if (isset(bands, IEEE80211_MODE_VHT_5GHZ)) { 1320 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20 | 1321 IEEE80211_CHAN_VHT20; 1322 } 1323 1324 /* 40MHz */ 1325 if (ht40) { 1326 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U; 1327 } 1328 if (ht40 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) { 1329 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U 1330 | IEEE80211_CHAN_VHT40U; 1331 } 1332 if (ht40) { 1333 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D; 1334 } 1335 if (ht40 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) { 1336 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D 1337 | IEEE80211_CHAN_VHT40D; 1338 } 1339 1340 /* 80MHz */ 1341 if (vht80 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) { 1342 flags[nmodes++] = IEEE80211_CHAN_A | 1343 IEEE80211_CHAN_HT40U | IEEE80211_CHAN_VHT80; 1344 flags[nmodes++] = IEEE80211_CHAN_A | 1345 IEEE80211_CHAN_HT40D | IEEE80211_CHAN_VHT80; 1346 } 1347 1348 /* XXX VHT80+80 */ 1349 /* XXX VHT160 */ 1350 flags[nmodes] = 0; 1351 } 1352 1353 static void 1354 getflags(const uint8_t bands[], uint32_t flags[], int ht40, int vht80) 1355 { 1356 1357 flags[0] = 0; 1358 if (isset(bands, IEEE80211_MODE_11A) || 1359 isset(bands, IEEE80211_MODE_11NA) || 1360 isset(bands, IEEE80211_MODE_VHT_5GHZ)) { 1361 if (isset(bands, IEEE80211_MODE_11B) || 1362 isset(bands, IEEE80211_MODE_11G) || 1363 isset(bands, IEEE80211_MODE_11NG) || 1364 isset(bands, IEEE80211_MODE_VHT_2GHZ)) 1365 return; 1366 1367 getflags_5ghz(bands, flags, ht40, vht80); 1368 } else 1369 getflags_2ghz(bands, flags, ht40); 1370 } 1371 1372 /* 1373 * Add one 20 MHz channel into specified channel list. 1374 */ 1375 /* XXX VHT */ 1376 int 1377 ieee80211_add_channel(struct ieee80211_channel chans[], int maxchans, 1378 int *nchans, uint8_t ieee, uint16_t freq, int8_t maxregpower, 1379 uint32_t chan_flags, const uint8_t bands[]) 1380 { 1381 uint32_t flags[IEEE80211_MODE_MAX]; 1382 int i, error; 1383 1384 getflags(bands, flags, 0, 0); 1385 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__)); 1386 1387 error = addchan(chans, maxchans, nchans, ieee, freq, maxregpower, 1388 flags[0] | chan_flags); 1389 for (i = 1; flags[i] != 0 && error == 0; i++) { 1390 error = copychan_prev(chans, maxchans, nchans, 1391 flags[i] | chan_flags); 1392 } 1393 1394 return (error); 1395 } 1396 1397 static struct ieee80211_channel * 1398 findchannel(struct ieee80211_channel chans[], int nchans, uint16_t freq, 1399 uint32_t flags) 1400 { 1401 struct ieee80211_channel *c; 1402 int i; 1403 1404 flags &= IEEE80211_CHAN_ALLTURBO; 1405 /* brute force search */ 1406 for (i = 0; i < nchans; i++) { 1407 c = &chans[i]; 1408 if (c->ic_freq == freq && 1409 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 1410 return c; 1411 } 1412 return NULL; 1413 } 1414 1415 /* 1416 * Add 40 MHz channel pair into specified channel list. 1417 */ 1418 /* XXX VHT */ 1419 int 1420 ieee80211_add_channel_ht40(struct ieee80211_channel chans[], int maxchans, 1421 int *nchans, uint8_t ieee, int8_t maxregpower, uint32_t flags) 1422 { 1423 struct ieee80211_channel *cent, *extc; 1424 uint16_t freq; 1425 int error; 1426 1427 freq = ieee80211_ieee2mhz(ieee, flags); 1428 1429 /* 1430 * Each entry defines an HT40 channel pair; find the 1431 * center channel, then the extension channel above. 1432 */ 1433 flags |= IEEE80211_CHAN_HT20; 1434 cent = findchannel(chans, *nchans, freq, flags); 1435 if (cent == NULL) 1436 return (EINVAL); 1437 1438 extc = findchannel(chans, *nchans, freq + 20, flags); 1439 if (extc == NULL) 1440 return (ENOENT); 1441 1442 flags &= ~IEEE80211_CHAN_HT; 1443 error = addchan(chans, maxchans, nchans, cent->ic_ieee, cent->ic_freq, 1444 maxregpower, flags | IEEE80211_CHAN_HT40U); 1445 if (error != 0) 1446 return (error); 1447 1448 error = addchan(chans, maxchans, nchans, extc->ic_ieee, extc->ic_freq, 1449 maxregpower, flags | IEEE80211_CHAN_HT40D); 1450 1451 return (error); 1452 } 1453 1454 /* 1455 * Fetch the center frequency for the primary channel. 1456 */ 1457 uint32_t 1458 ieee80211_get_channel_center_freq(const struct ieee80211_channel *c) 1459 { 1460 1461 return (c->ic_freq); 1462 } 1463 1464 /* 1465 * Fetch the center frequency for the primary BAND channel. 1466 * 1467 * For 5, 10, 20MHz channels it'll be the normally configured channel 1468 * frequency. 1469 * 1470 * For 40MHz, 80MHz, 160Mhz channels it'll the the centre of the 1471 * wide channel, not the centre of the primary channel (that's ic_freq). 1472 * 1473 * For 80+80MHz channels this will be the centre of the primary 1474 * 80MHz channel; the secondary 80MHz channel will be center_freq2(). 1475 */ 1476 uint32_t 1477 ieee80211_get_channel_center_freq1(const struct ieee80211_channel *c) 1478 { 1479 1480 /* 1481 * VHT - use the pre-calculated centre frequency 1482 * of the given channel. 1483 */ 1484 if (IEEE80211_IS_CHAN_VHT(c)) 1485 return (ieee80211_ieee2mhz(c->ic_vht_ch_freq1, c->ic_flags)); 1486 1487 if (IEEE80211_IS_CHAN_HT40U(c)) { 1488 return (c->ic_freq + 10); 1489 } 1490 if (IEEE80211_IS_CHAN_HT40D(c)) { 1491 return (c->ic_freq - 10); 1492 } 1493 1494 return (c->ic_freq); 1495 } 1496 1497 /* 1498 * For now, no 80+80 support; it will likely always return 0. 1499 */ 1500 uint32_t 1501 ieee80211_get_channel_center_freq2(const struct ieee80211_channel *c) 1502 { 1503 1504 if (IEEE80211_IS_CHAN_VHT(c) && (c->ic_vht_ch_freq2 != 0)) 1505 return (ieee80211_ieee2mhz(c->ic_vht_ch_freq2, c->ic_flags)); 1506 1507 return (0); 1508 } 1509 1510 /* 1511 * Adds channels into specified channel list (ieee[] array must be sorted). 1512 * Channels are already sorted. 1513 */ 1514 static int 1515 add_chanlist(struct ieee80211_channel chans[], int maxchans, int *nchans, 1516 const uint8_t ieee[], int nieee, uint32_t flags[]) 1517 { 1518 uint16_t freq; 1519 int i, j, error; 1520 int is_vht; 1521 1522 for (i = 0; i < nieee; i++) { 1523 freq = ieee80211_ieee2mhz(ieee[i], flags[0]); 1524 for (j = 0; flags[j] != 0; j++) { 1525 /* 1526 * Notes: 1527 * + HT40 and VHT40 channels occur together, so 1528 * we need to be careful that we actually allow that. 1529 * + VHT80, VHT160 will coexist with HT40/VHT40, so 1530 * make sure it's not skipped because of the overlap 1531 * check used for (V)HT40. 1532 */ 1533 is_vht = !! (flags[j] & IEEE80211_CHAN_VHT); 1534 1535 /* 1536 * Test for VHT80. 1537 * XXX This is all very broken right now. 1538 * What we /should/ do is: 1539 * 1540 * + check that the frequency is in the list of 1541 * allowed VHT80 ranges; and 1542 * + the other 3 channels in the list are actually 1543 * also available. 1544 */ 1545 if (is_vht && flags[j] & IEEE80211_CHAN_VHT80) 1546 if (! is_vht80_valid_freq(freq)) 1547 continue; 1548 1549 /* 1550 * Test for (V)HT40. 1551 * 1552 * This is also a fall through from VHT80; as we only 1553 * allow a VHT80 channel if the VHT40 combination is 1554 * also valid. If the VHT40 form is not valid then 1555 * we certainly can't do VHT80.. 1556 */ 1557 if (flags[j] & IEEE80211_CHAN_HT40D) 1558 /* 1559 * Can't have a "lower" channel if we are the 1560 * first channel. 1561 * 1562 * Can't have a "lower" channel if it's below/ 1563 * within 20MHz of the first channel. 1564 * 1565 * Can't have a "lower" channel if the channel 1566 * below it is not 20MHz away. 1567 */ 1568 if (i == 0 || ieee[i] < ieee[0] + 4 || 1569 freq - 20 != 1570 ieee80211_ieee2mhz(ieee[i] - 4, flags[j])) 1571 continue; 1572 if (flags[j] & IEEE80211_CHAN_HT40U) 1573 /* 1574 * Can't have an "upper" channel if we are 1575 * the last channel. 1576 * 1577 * Can't have an "upper" channel be above the 1578 * last channel in the list. 1579 * 1580 * Can't have an "upper" channel if the next 1581 * channel according to the math isn't 20MHz 1582 * away. (Likely for channel 13/14.) 1583 */ 1584 if (i == nieee - 1 || 1585 ieee[i] + 4 > ieee[nieee - 1] || 1586 freq + 20 != 1587 ieee80211_ieee2mhz(ieee[i] + 4, flags[j])) 1588 continue; 1589 1590 if (j == 0) { 1591 error = addchan(chans, maxchans, nchans, 1592 ieee[i], freq, 0, flags[j]); 1593 } else { 1594 error = copychan_prev(chans, maxchans, nchans, 1595 flags[j]); 1596 } 1597 if (error != 0) 1598 return (error); 1599 } 1600 } 1601 1602 return (0); 1603 } 1604 1605 int 1606 ieee80211_add_channel_list_2ghz(struct ieee80211_channel chans[], int maxchans, 1607 int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[], 1608 int ht40) 1609 { 1610 uint32_t flags[IEEE80211_MODE_MAX]; 1611 1612 /* XXX no VHT for now */ 1613 getflags_2ghz(bands, flags, ht40); 1614 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__)); 1615 1616 return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags)); 1617 } 1618 1619 int 1620 ieee80211_add_channel_list_5ghz(struct ieee80211_channel chans[], int maxchans, 1621 int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[], 1622 int ht40) 1623 { 1624 uint32_t flags[IEEE80211_MODE_MAX]; 1625 int vht80 = 0; 1626 1627 /* 1628 * For now, assume VHT == VHT80 support as a minimum. 1629 */ 1630 if (isset(bands, IEEE80211_MODE_VHT_5GHZ)) 1631 vht80 = 1; 1632 1633 getflags_5ghz(bands, flags, ht40, vht80); 1634 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__)); 1635 1636 return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags)); 1637 } 1638 1639 /* 1640 * Locate a channel given a frequency+flags. We cache 1641 * the previous lookup to optimize switching between two 1642 * channels--as happens with dynamic turbo. 1643 */ 1644 struct ieee80211_channel * 1645 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags) 1646 { 1647 struct ieee80211_channel *c; 1648 1649 flags &= IEEE80211_CHAN_ALLTURBO; 1650 c = ic->ic_prevchan; 1651 if (c != NULL && c->ic_freq == freq && 1652 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 1653 return c; 1654 /* brute force search */ 1655 return (findchannel(ic->ic_channels, ic->ic_nchans, freq, flags)); 1656 } 1657 1658 /* 1659 * Locate a channel given a channel number+flags. We cache 1660 * the previous lookup to optimize switching between two 1661 * channels--as happens with dynamic turbo. 1662 */ 1663 struct ieee80211_channel * 1664 ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags) 1665 { 1666 struct ieee80211_channel *c; 1667 int i; 1668 1669 flags &= IEEE80211_CHAN_ALLTURBO; 1670 c = ic->ic_prevchan; 1671 if (c != NULL && c->ic_ieee == ieee && 1672 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 1673 return c; 1674 /* brute force search */ 1675 for (i = 0; i < ic->ic_nchans; i++) { 1676 c = &ic->ic_channels[i]; 1677 if (c->ic_ieee == ieee && 1678 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 1679 return c; 1680 } 1681 return NULL; 1682 } 1683 1684 /* 1685 * Lookup a channel suitable for the given rx status. 1686 * 1687 * This is used to find a channel for a frame (eg beacon, probe 1688 * response) based purely on the received PHY information. 1689 * 1690 * For now it tries to do it based on R_FREQ / R_IEEE. 1691 * This is enough for 11bg and 11a (and thus 11ng/11na) 1692 * but it will not be enough for GSM, PSB channels and the 1693 * like. It also doesn't know about legacy-turbog and 1694 * legacy-turbo modes, which some offload NICs actually 1695 * support in weird ways. 1696 * 1697 * Takes the ic and rxstatus; returns the channel or NULL 1698 * if not found. 1699 * 1700 * XXX TODO: Add support for that when the need arises. 1701 */ 1702 struct ieee80211_channel * 1703 ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap, 1704 const struct ieee80211_rx_stats *rxs) 1705 { 1706 struct ieee80211com *ic = vap->iv_ic; 1707 uint32_t flags; 1708 struct ieee80211_channel *c; 1709 1710 if (rxs == NULL) 1711 return (NULL); 1712 1713 /* 1714 * Strictly speaking we only use freq for now, 1715 * however later on we may wish to just store 1716 * the ieee for verification. 1717 */ 1718 if ((rxs->r_flags & IEEE80211_R_FREQ) == 0) 1719 return (NULL); 1720 if ((rxs->r_flags & IEEE80211_R_IEEE) == 0) 1721 return (NULL); 1722 1723 /* 1724 * If the rx status contains a valid ieee/freq, then 1725 * ensure we populate the correct channel information 1726 * in rxchan before passing it up to the scan infrastructure. 1727 * Offload NICs will pass up beacons from all channels 1728 * during background scans. 1729 */ 1730 1731 /* Determine a band */ 1732 /* XXX should be done by the driver? */ 1733 if (rxs->c_freq < 3000) { 1734 flags = IEEE80211_CHAN_G; 1735 } else { 1736 flags = IEEE80211_CHAN_A; 1737 } 1738 1739 /* Channel lookup */ 1740 c = ieee80211_find_channel(ic, rxs->c_freq, flags); 1741 1742 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT, 1743 "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n", 1744 __func__, 1745 (int) rxs->c_freq, 1746 (int) rxs->c_ieee, 1747 flags, 1748 c); 1749 1750 return (c); 1751 } 1752 1753 static void 1754 addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword) 1755 { 1756 #define ADD(_ic, _s, _o) \ 1757 ifmedia_add(media, \ 1758 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL) 1759 static const u_int mopts[IEEE80211_MODE_MAX] = { 1760 [IEEE80211_MODE_AUTO] = IFM_AUTO, 1761 [IEEE80211_MODE_11A] = IFM_IEEE80211_11A, 1762 [IEEE80211_MODE_11B] = IFM_IEEE80211_11B, 1763 [IEEE80211_MODE_11G] = IFM_IEEE80211_11G, 1764 [IEEE80211_MODE_FH] = IFM_IEEE80211_FH, 1765 [IEEE80211_MODE_TURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO, 1766 [IEEE80211_MODE_TURBO_G] = IFM_IEEE80211_11G|IFM_IEEE80211_TURBO, 1767 [IEEE80211_MODE_STURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO, 1768 [IEEE80211_MODE_HALF] = IFM_IEEE80211_11A, /* XXX */ 1769 [IEEE80211_MODE_QUARTER] = IFM_IEEE80211_11A, /* XXX */ 1770 [IEEE80211_MODE_11NA] = IFM_IEEE80211_11NA, 1771 [IEEE80211_MODE_11NG] = IFM_IEEE80211_11NG, 1772 [IEEE80211_MODE_VHT_2GHZ] = IFM_IEEE80211_VHT2G, 1773 [IEEE80211_MODE_VHT_5GHZ] = IFM_IEEE80211_VHT5G, 1774 }; 1775 u_int mopt; 1776 1777 mopt = mopts[mode]; 1778 if (addsta) 1779 ADD(ic, mword, mopt); /* STA mode has no cap */ 1780 if (caps & IEEE80211_C_IBSS) 1781 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC); 1782 if (caps & IEEE80211_C_HOSTAP) 1783 ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP); 1784 if (caps & IEEE80211_C_AHDEMO) 1785 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0); 1786 if (caps & IEEE80211_C_MONITOR) 1787 ADD(media, mword, mopt | IFM_IEEE80211_MONITOR); 1788 if (caps & IEEE80211_C_WDS) 1789 ADD(media, mword, mopt | IFM_IEEE80211_WDS); 1790 if (caps & IEEE80211_C_MBSS) 1791 ADD(media, mword, mopt | IFM_IEEE80211_MBSS); 1792 #undef ADD 1793 } 1794 1795 /* 1796 * Setup the media data structures according to the channel and 1797 * rate tables. 1798 */ 1799 static int 1800 ieee80211_media_setup(struct ieee80211com *ic, 1801 struct ifmedia *media, int caps, int addsta, 1802 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat) 1803 { 1804 int i, j, rate, maxrate, mword, r; 1805 enum ieee80211_phymode mode; 1806 const struct ieee80211_rateset *rs; 1807 struct ieee80211_rateset allrates; 1808 1809 /* 1810 * Fill in media characteristics. 1811 */ 1812 ifmedia_init(media, 0, media_change, media_stat); 1813 maxrate = 0; 1814 /* 1815 * Add media for legacy operating modes. 1816 */ 1817 memset(&allrates, 0, sizeof(allrates)); 1818 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) { 1819 if (isclr(ic->ic_modecaps, mode)) 1820 continue; 1821 addmedia(media, caps, addsta, mode, IFM_AUTO); 1822 if (mode == IEEE80211_MODE_AUTO) 1823 continue; 1824 rs = &ic->ic_sup_rates[mode]; 1825 for (i = 0; i < rs->rs_nrates; i++) { 1826 rate = rs->rs_rates[i]; 1827 mword = ieee80211_rate2media(ic, rate, mode); 1828 if (mword == 0) 1829 continue; 1830 addmedia(media, caps, addsta, mode, mword); 1831 /* 1832 * Add legacy rate to the collection of all rates. 1833 */ 1834 r = rate & IEEE80211_RATE_VAL; 1835 for (j = 0; j < allrates.rs_nrates; j++) 1836 if (allrates.rs_rates[j] == r) 1837 break; 1838 if (j == allrates.rs_nrates) { 1839 /* unique, add to the set */ 1840 allrates.rs_rates[j] = r; 1841 allrates.rs_nrates++; 1842 } 1843 rate = (rate & IEEE80211_RATE_VAL) / 2; 1844 if (rate > maxrate) 1845 maxrate = rate; 1846 } 1847 } 1848 for (i = 0; i < allrates.rs_nrates; i++) { 1849 mword = ieee80211_rate2media(ic, allrates.rs_rates[i], 1850 IEEE80211_MODE_AUTO); 1851 if (mword == 0) 1852 continue; 1853 /* NB: remove media options from mword */ 1854 addmedia(media, caps, addsta, 1855 IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword)); 1856 } 1857 /* 1858 * Add HT/11n media. Note that we do not have enough 1859 * bits in the media subtype to express the MCS so we 1860 * use a "placeholder" media subtype and any fixed MCS 1861 * must be specified with a different mechanism. 1862 */ 1863 for (; mode <= IEEE80211_MODE_11NG; mode++) { 1864 if (isclr(ic->ic_modecaps, mode)) 1865 continue; 1866 addmedia(media, caps, addsta, mode, IFM_AUTO); 1867 addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS); 1868 } 1869 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) || 1870 isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) { 1871 addmedia(media, caps, addsta, 1872 IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS); 1873 i = ic->ic_txstream * 8 - 1; 1874 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) && 1875 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)) 1876 rate = ieee80211_htrates[i].ht40_rate_400ns; 1877 else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40)) 1878 rate = ieee80211_htrates[i].ht40_rate_800ns; 1879 else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20)) 1880 rate = ieee80211_htrates[i].ht20_rate_400ns; 1881 else 1882 rate = ieee80211_htrates[i].ht20_rate_800ns; 1883 if (rate > maxrate) 1884 maxrate = rate; 1885 } 1886 1887 /* 1888 * Add VHT media. 1889 */ 1890 for (; mode <= IEEE80211_MODE_VHT_5GHZ; mode++) { 1891 if (isclr(ic->ic_modecaps, mode)) 1892 continue; 1893 addmedia(media, caps, addsta, mode, IFM_AUTO); 1894 addmedia(media, caps, addsta, mode, IFM_IEEE80211_VHT); 1895 1896 /* XXX TODO: VHT maxrate */ 1897 } 1898 1899 return maxrate; 1900 } 1901 1902 /* XXX inline or eliminate? */ 1903 const struct ieee80211_rateset * 1904 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c) 1905 { 1906 /* XXX does this work for 11ng basic rates? */ 1907 return &ic->ic_sup_rates[ieee80211_chan2mode(c)]; 1908 } 1909 1910 /* XXX inline or eliminate? */ 1911 const struct ieee80211_htrateset * 1912 ieee80211_get_suphtrates(struct ieee80211com *ic, 1913 const struct ieee80211_channel *c) 1914 { 1915 return &ic->ic_sup_htrates; 1916 } 1917 1918 void 1919 ieee80211_announce(struct ieee80211com *ic) 1920 { 1921 int i, rate, mword; 1922 enum ieee80211_phymode mode; 1923 const struct ieee80211_rateset *rs; 1924 1925 /* NB: skip AUTO since it has no rates */ 1926 for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) { 1927 if (isclr(ic->ic_modecaps, mode)) 1928 continue; 1929 ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]); 1930 rs = &ic->ic_sup_rates[mode]; 1931 for (i = 0; i < rs->rs_nrates; i++) { 1932 mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode); 1933 if (mword == 0) 1934 continue; 1935 rate = ieee80211_media2rate(mword); 1936 printf("%s%d%sMbps", (i != 0 ? " " : ""), 1937 rate / 2, ((rate & 0x1) != 0 ? ".5" : "")); 1938 } 1939 printf("\n"); 1940 } 1941 ieee80211_ht_announce(ic); 1942 ieee80211_vht_announce(ic); 1943 } 1944 1945 void 1946 ieee80211_announce_channels(struct ieee80211com *ic) 1947 { 1948 const struct ieee80211_channel *c; 1949 char type; 1950 int i, cw; 1951 1952 printf("Chan Freq CW RegPwr MinPwr MaxPwr\n"); 1953 for (i = 0; i < ic->ic_nchans; i++) { 1954 c = &ic->ic_channels[i]; 1955 if (IEEE80211_IS_CHAN_ST(c)) 1956 type = 'S'; 1957 else if (IEEE80211_IS_CHAN_108A(c)) 1958 type = 'T'; 1959 else if (IEEE80211_IS_CHAN_108G(c)) 1960 type = 'G'; 1961 else if (IEEE80211_IS_CHAN_HT(c)) 1962 type = 'n'; 1963 else if (IEEE80211_IS_CHAN_A(c)) 1964 type = 'a'; 1965 else if (IEEE80211_IS_CHAN_ANYG(c)) 1966 type = 'g'; 1967 else if (IEEE80211_IS_CHAN_B(c)) 1968 type = 'b'; 1969 else 1970 type = 'f'; 1971 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c)) 1972 cw = 40; 1973 else if (IEEE80211_IS_CHAN_HALF(c)) 1974 cw = 10; 1975 else if (IEEE80211_IS_CHAN_QUARTER(c)) 1976 cw = 5; 1977 else 1978 cw = 20; 1979 printf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n" 1980 , c->ic_ieee, c->ic_freq, type 1981 , cw 1982 , IEEE80211_IS_CHAN_HT40U(c) ? '+' : 1983 IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' ' 1984 , c->ic_maxregpower 1985 , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0 1986 , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0 1987 ); 1988 } 1989 } 1990 1991 static int 1992 media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode) 1993 { 1994 switch (IFM_MODE(ime->ifm_media)) { 1995 case IFM_IEEE80211_11A: 1996 *mode = IEEE80211_MODE_11A; 1997 break; 1998 case IFM_IEEE80211_11B: 1999 *mode = IEEE80211_MODE_11B; 2000 break; 2001 case IFM_IEEE80211_11G: 2002 *mode = IEEE80211_MODE_11G; 2003 break; 2004 case IFM_IEEE80211_FH: 2005 *mode = IEEE80211_MODE_FH; 2006 break; 2007 case IFM_IEEE80211_11NA: 2008 *mode = IEEE80211_MODE_11NA; 2009 break; 2010 case IFM_IEEE80211_11NG: 2011 *mode = IEEE80211_MODE_11NG; 2012 break; 2013 case IFM_AUTO: 2014 *mode = IEEE80211_MODE_AUTO; 2015 break; 2016 default: 2017 return 0; 2018 } 2019 /* 2020 * Turbo mode is an ``option''. 2021 * XXX does not apply to AUTO 2022 */ 2023 if (ime->ifm_media & IFM_IEEE80211_TURBO) { 2024 if (*mode == IEEE80211_MODE_11A) { 2025 if (flags & IEEE80211_F_TURBOP) 2026 *mode = IEEE80211_MODE_TURBO_A; 2027 else 2028 *mode = IEEE80211_MODE_STURBO_A; 2029 } else if (*mode == IEEE80211_MODE_11G) 2030 *mode = IEEE80211_MODE_TURBO_G; 2031 else 2032 return 0; 2033 } 2034 /* XXX HT40 +/- */ 2035 return 1; 2036 } 2037 2038 /* 2039 * Handle a media change request on the vap interface. 2040 */ 2041 int 2042 ieee80211_media_change(struct ifnet *ifp) 2043 { 2044 struct ieee80211vap *vap = ifp->if_softc; 2045 struct ifmedia_entry *ime = vap->iv_media.ifm_cur; 2046 uint16_t newmode; 2047 2048 if (!media2mode(ime, vap->iv_flags, &newmode)) 2049 return EINVAL; 2050 if (vap->iv_des_mode != newmode) { 2051 vap->iv_des_mode = newmode; 2052 /* XXX kick state machine if up+running */ 2053 } 2054 return 0; 2055 } 2056 2057 /* 2058 * Common code to calculate the media status word 2059 * from the operating mode and channel state. 2060 */ 2061 static int 2062 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan) 2063 { 2064 int status; 2065 2066 status = IFM_IEEE80211; 2067 switch (opmode) { 2068 case IEEE80211_M_STA: 2069 break; 2070 case IEEE80211_M_IBSS: 2071 status |= IFM_IEEE80211_ADHOC; 2072 break; 2073 case IEEE80211_M_HOSTAP: 2074 status |= IFM_IEEE80211_HOSTAP; 2075 break; 2076 case IEEE80211_M_MONITOR: 2077 status |= IFM_IEEE80211_MONITOR; 2078 break; 2079 case IEEE80211_M_AHDEMO: 2080 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0; 2081 break; 2082 case IEEE80211_M_WDS: 2083 status |= IFM_IEEE80211_WDS; 2084 break; 2085 case IEEE80211_M_MBSS: 2086 status |= IFM_IEEE80211_MBSS; 2087 break; 2088 } 2089 if (IEEE80211_IS_CHAN_HTA(chan)) { 2090 status |= IFM_IEEE80211_11NA; 2091 } else if (IEEE80211_IS_CHAN_HTG(chan)) { 2092 status |= IFM_IEEE80211_11NG; 2093 } else if (IEEE80211_IS_CHAN_A(chan)) { 2094 status |= IFM_IEEE80211_11A; 2095 } else if (IEEE80211_IS_CHAN_B(chan)) { 2096 status |= IFM_IEEE80211_11B; 2097 } else if (IEEE80211_IS_CHAN_ANYG(chan)) { 2098 status |= IFM_IEEE80211_11G; 2099 } else if (IEEE80211_IS_CHAN_FHSS(chan)) { 2100 status |= IFM_IEEE80211_FH; 2101 } 2102 /* XXX else complain? */ 2103 2104 if (IEEE80211_IS_CHAN_TURBO(chan)) 2105 status |= IFM_IEEE80211_TURBO; 2106 #if 0 2107 if (IEEE80211_IS_CHAN_HT20(chan)) 2108 status |= IFM_IEEE80211_HT20; 2109 if (IEEE80211_IS_CHAN_HT40(chan)) 2110 status |= IFM_IEEE80211_HT40; 2111 #endif 2112 return status; 2113 } 2114 2115 void 2116 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr) 2117 { 2118 struct ieee80211vap *vap = ifp->if_softc; 2119 struct ieee80211com *ic = vap->iv_ic; 2120 enum ieee80211_phymode mode; 2121 2122 imr->ifm_status = IFM_AVALID; 2123 /* 2124 * NB: use the current channel's mode to lock down a xmit 2125 * rate only when running; otherwise we may have a mismatch 2126 * in which case the rate will not be convertible. 2127 */ 2128 if (vap->iv_state == IEEE80211_S_RUN || 2129 vap->iv_state == IEEE80211_S_SLEEP) { 2130 imr->ifm_status |= IFM_ACTIVE; 2131 mode = ieee80211_chan2mode(ic->ic_curchan); 2132 } else 2133 mode = IEEE80211_MODE_AUTO; 2134 imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan); 2135 /* 2136 * Calculate a current rate if possible. 2137 */ 2138 if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) { 2139 /* 2140 * A fixed rate is set, report that. 2141 */ 2142 imr->ifm_active |= ieee80211_rate2media(ic, 2143 vap->iv_txparms[mode].ucastrate, mode); 2144 } else if (vap->iv_opmode == IEEE80211_M_STA) { 2145 /* 2146 * In station mode report the current transmit rate. 2147 */ 2148 imr->ifm_active |= ieee80211_rate2media(ic, 2149 vap->iv_bss->ni_txrate, mode); 2150 } else 2151 imr->ifm_active |= IFM_AUTO; 2152 if (imr->ifm_status & IFM_ACTIVE) 2153 imr->ifm_current = imr->ifm_active; 2154 } 2155 2156 /* 2157 * Set the current phy mode and recalculate the active channel 2158 * set based on the available channels for this mode. Also 2159 * select a new default/current channel if the current one is 2160 * inappropriate for this mode. 2161 */ 2162 int 2163 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode) 2164 { 2165 /* 2166 * Adjust basic rates in 11b/11g supported rate set. 2167 * Note that if operating on a hal/quarter rate channel 2168 * this is a noop as those rates sets are different 2169 * and used instead. 2170 */ 2171 if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B) 2172 ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode); 2173 2174 ic->ic_curmode = mode; 2175 ieee80211_reset_erp(ic); /* reset ERP state */ 2176 2177 return 0; 2178 } 2179 2180 /* 2181 * Return the phy mode for with the specified channel. 2182 */ 2183 enum ieee80211_phymode 2184 ieee80211_chan2mode(const struct ieee80211_channel *chan) 2185 { 2186 2187 if (IEEE80211_IS_CHAN_VHT_2GHZ(chan)) 2188 return IEEE80211_MODE_VHT_2GHZ; 2189 else if (IEEE80211_IS_CHAN_VHT_5GHZ(chan)) 2190 return IEEE80211_MODE_VHT_5GHZ; 2191 else if (IEEE80211_IS_CHAN_HTA(chan)) 2192 return IEEE80211_MODE_11NA; 2193 else if (IEEE80211_IS_CHAN_HTG(chan)) 2194 return IEEE80211_MODE_11NG; 2195 else if (IEEE80211_IS_CHAN_108G(chan)) 2196 return IEEE80211_MODE_TURBO_G; 2197 else if (IEEE80211_IS_CHAN_ST(chan)) 2198 return IEEE80211_MODE_STURBO_A; 2199 else if (IEEE80211_IS_CHAN_TURBO(chan)) 2200 return IEEE80211_MODE_TURBO_A; 2201 else if (IEEE80211_IS_CHAN_HALF(chan)) 2202 return IEEE80211_MODE_HALF; 2203 else if (IEEE80211_IS_CHAN_QUARTER(chan)) 2204 return IEEE80211_MODE_QUARTER; 2205 else if (IEEE80211_IS_CHAN_A(chan)) 2206 return IEEE80211_MODE_11A; 2207 else if (IEEE80211_IS_CHAN_ANYG(chan)) 2208 return IEEE80211_MODE_11G; 2209 else if (IEEE80211_IS_CHAN_B(chan)) 2210 return IEEE80211_MODE_11B; 2211 else if (IEEE80211_IS_CHAN_FHSS(chan)) 2212 return IEEE80211_MODE_FH; 2213 2214 /* NB: should not get here */ 2215 printf("%s: cannot map channel to mode; freq %u flags 0x%x\n", 2216 __func__, chan->ic_freq, chan->ic_flags); 2217 return IEEE80211_MODE_11B; 2218 } 2219 2220 struct ratemedia { 2221 u_int match; /* rate + mode */ 2222 u_int media; /* if_media rate */ 2223 }; 2224 2225 static int 2226 findmedia(const struct ratemedia rates[], int n, u_int match) 2227 { 2228 int i; 2229 2230 for (i = 0; i < n; i++) 2231 if (rates[i].match == match) 2232 return rates[i].media; 2233 return IFM_AUTO; 2234 } 2235 2236 /* 2237 * Convert IEEE80211 rate value to ifmedia subtype. 2238 * Rate is either a legacy rate in units of 0.5Mbps 2239 * or an MCS index. 2240 */ 2241 int 2242 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode) 2243 { 2244 static const struct ratemedia rates[] = { 2245 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 }, 2246 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 }, 2247 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 }, 2248 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 }, 2249 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 }, 2250 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 }, 2251 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 }, 2252 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 }, 2253 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 }, 2254 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 }, 2255 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 }, 2256 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 }, 2257 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 }, 2258 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 }, 2259 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 }, 2260 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 }, 2261 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 }, 2262 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 }, 2263 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 }, 2264 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 }, 2265 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 }, 2266 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 }, 2267 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 }, 2268 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 }, 2269 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 }, 2270 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 }, 2271 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 }, 2272 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 }, 2273 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 }, 2274 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 }, 2275 /* NB: OFDM72 doesn't really exist so we don't handle it */ 2276 }; 2277 static const struct ratemedia htrates[] = { 2278 { 0, IFM_IEEE80211_MCS }, 2279 { 1, IFM_IEEE80211_MCS }, 2280 { 2, IFM_IEEE80211_MCS }, 2281 { 3, IFM_IEEE80211_MCS }, 2282 { 4, IFM_IEEE80211_MCS }, 2283 { 5, IFM_IEEE80211_MCS }, 2284 { 6, IFM_IEEE80211_MCS }, 2285 { 7, IFM_IEEE80211_MCS }, 2286 { 8, IFM_IEEE80211_MCS }, 2287 { 9, IFM_IEEE80211_MCS }, 2288 { 10, IFM_IEEE80211_MCS }, 2289 { 11, IFM_IEEE80211_MCS }, 2290 { 12, IFM_IEEE80211_MCS }, 2291 { 13, IFM_IEEE80211_MCS }, 2292 { 14, IFM_IEEE80211_MCS }, 2293 { 15, IFM_IEEE80211_MCS }, 2294 { 16, IFM_IEEE80211_MCS }, 2295 { 17, IFM_IEEE80211_MCS }, 2296 { 18, IFM_IEEE80211_MCS }, 2297 { 19, IFM_IEEE80211_MCS }, 2298 { 20, IFM_IEEE80211_MCS }, 2299 { 21, IFM_IEEE80211_MCS }, 2300 { 22, IFM_IEEE80211_MCS }, 2301 { 23, IFM_IEEE80211_MCS }, 2302 { 24, IFM_IEEE80211_MCS }, 2303 { 25, IFM_IEEE80211_MCS }, 2304 { 26, IFM_IEEE80211_MCS }, 2305 { 27, IFM_IEEE80211_MCS }, 2306 { 28, IFM_IEEE80211_MCS }, 2307 { 29, IFM_IEEE80211_MCS }, 2308 { 30, IFM_IEEE80211_MCS }, 2309 { 31, IFM_IEEE80211_MCS }, 2310 { 32, IFM_IEEE80211_MCS }, 2311 { 33, IFM_IEEE80211_MCS }, 2312 { 34, IFM_IEEE80211_MCS }, 2313 { 35, IFM_IEEE80211_MCS }, 2314 { 36, IFM_IEEE80211_MCS }, 2315 { 37, IFM_IEEE80211_MCS }, 2316 { 38, IFM_IEEE80211_MCS }, 2317 { 39, IFM_IEEE80211_MCS }, 2318 { 40, IFM_IEEE80211_MCS }, 2319 { 41, IFM_IEEE80211_MCS }, 2320 { 42, IFM_IEEE80211_MCS }, 2321 { 43, IFM_IEEE80211_MCS }, 2322 { 44, IFM_IEEE80211_MCS }, 2323 { 45, IFM_IEEE80211_MCS }, 2324 { 46, IFM_IEEE80211_MCS }, 2325 { 47, IFM_IEEE80211_MCS }, 2326 { 48, IFM_IEEE80211_MCS }, 2327 { 49, IFM_IEEE80211_MCS }, 2328 { 50, IFM_IEEE80211_MCS }, 2329 { 51, IFM_IEEE80211_MCS }, 2330 { 52, IFM_IEEE80211_MCS }, 2331 { 53, IFM_IEEE80211_MCS }, 2332 { 54, IFM_IEEE80211_MCS }, 2333 { 55, IFM_IEEE80211_MCS }, 2334 { 56, IFM_IEEE80211_MCS }, 2335 { 57, IFM_IEEE80211_MCS }, 2336 { 58, IFM_IEEE80211_MCS }, 2337 { 59, IFM_IEEE80211_MCS }, 2338 { 60, IFM_IEEE80211_MCS }, 2339 { 61, IFM_IEEE80211_MCS }, 2340 { 62, IFM_IEEE80211_MCS }, 2341 { 63, IFM_IEEE80211_MCS }, 2342 { 64, IFM_IEEE80211_MCS }, 2343 { 65, IFM_IEEE80211_MCS }, 2344 { 66, IFM_IEEE80211_MCS }, 2345 { 67, IFM_IEEE80211_MCS }, 2346 { 68, IFM_IEEE80211_MCS }, 2347 { 69, IFM_IEEE80211_MCS }, 2348 { 70, IFM_IEEE80211_MCS }, 2349 { 71, IFM_IEEE80211_MCS }, 2350 { 72, IFM_IEEE80211_MCS }, 2351 { 73, IFM_IEEE80211_MCS }, 2352 { 74, IFM_IEEE80211_MCS }, 2353 { 75, IFM_IEEE80211_MCS }, 2354 { 76, IFM_IEEE80211_MCS }, 2355 }; 2356 int m; 2357 2358 /* 2359 * Check 11n rates first for match as an MCS. 2360 */ 2361 if (mode == IEEE80211_MODE_11NA) { 2362 if (rate & IEEE80211_RATE_MCS) { 2363 rate &= ~IEEE80211_RATE_MCS; 2364 m = findmedia(htrates, nitems(htrates), rate); 2365 if (m != IFM_AUTO) 2366 return m | IFM_IEEE80211_11NA; 2367 } 2368 } else if (mode == IEEE80211_MODE_11NG) { 2369 /* NB: 12 is ambiguous, it will be treated as an MCS */ 2370 if (rate & IEEE80211_RATE_MCS) { 2371 rate &= ~IEEE80211_RATE_MCS; 2372 m = findmedia(htrates, nitems(htrates), rate); 2373 if (m != IFM_AUTO) 2374 return m | IFM_IEEE80211_11NG; 2375 } 2376 } 2377 rate &= IEEE80211_RATE_VAL; 2378 switch (mode) { 2379 case IEEE80211_MODE_11A: 2380 case IEEE80211_MODE_HALF: /* XXX good 'nuf */ 2381 case IEEE80211_MODE_QUARTER: 2382 case IEEE80211_MODE_11NA: 2383 case IEEE80211_MODE_TURBO_A: 2384 case IEEE80211_MODE_STURBO_A: 2385 return findmedia(rates, nitems(rates), 2386 rate | IFM_IEEE80211_11A); 2387 case IEEE80211_MODE_11B: 2388 return findmedia(rates, nitems(rates), 2389 rate | IFM_IEEE80211_11B); 2390 case IEEE80211_MODE_FH: 2391 return findmedia(rates, nitems(rates), 2392 rate | IFM_IEEE80211_FH); 2393 case IEEE80211_MODE_AUTO: 2394 /* NB: ic may be NULL for some drivers */ 2395 if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH) 2396 return findmedia(rates, nitems(rates), 2397 rate | IFM_IEEE80211_FH); 2398 /* NB: hack, 11g matches both 11b+11a rates */ 2399 /* fall thru... */ 2400 case IEEE80211_MODE_11G: 2401 case IEEE80211_MODE_11NG: 2402 case IEEE80211_MODE_TURBO_G: 2403 return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G); 2404 case IEEE80211_MODE_VHT_2GHZ: 2405 case IEEE80211_MODE_VHT_5GHZ: 2406 /* XXX TODO: need to figure out mapping for VHT rates */ 2407 return IFM_AUTO; 2408 } 2409 return IFM_AUTO; 2410 } 2411 2412 int 2413 ieee80211_media2rate(int mword) 2414 { 2415 static const int ieeerates[] = { 2416 -1, /* IFM_AUTO */ 2417 0, /* IFM_MANUAL */ 2418 0, /* IFM_NONE */ 2419 2, /* IFM_IEEE80211_FH1 */ 2420 4, /* IFM_IEEE80211_FH2 */ 2421 2, /* IFM_IEEE80211_DS1 */ 2422 4, /* IFM_IEEE80211_DS2 */ 2423 11, /* IFM_IEEE80211_DS5 */ 2424 22, /* IFM_IEEE80211_DS11 */ 2425 44, /* IFM_IEEE80211_DS22 */ 2426 12, /* IFM_IEEE80211_OFDM6 */ 2427 18, /* IFM_IEEE80211_OFDM9 */ 2428 24, /* IFM_IEEE80211_OFDM12 */ 2429 36, /* IFM_IEEE80211_OFDM18 */ 2430 48, /* IFM_IEEE80211_OFDM24 */ 2431 72, /* IFM_IEEE80211_OFDM36 */ 2432 96, /* IFM_IEEE80211_OFDM48 */ 2433 108, /* IFM_IEEE80211_OFDM54 */ 2434 144, /* IFM_IEEE80211_OFDM72 */ 2435 0, /* IFM_IEEE80211_DS354k */ 2436 0, /* IFM_IEEE80211_DS512k */ 2437 6, /* IFM_IEEE80211_OFDM3 */ 2438 9, /* IFM_IEEE80211_OFDM4 */ 2439 54, /* IFM_IEEE80211_OFDM27 */ 2440 -1, /* IFM_IEEE80211_MCS */ 2441 -1, /* IFM_IEEE80211_VHT */ 2442 }; 2443 return IFM_SUBTYPE(mword) < nitems(ieeerates) ? 2444 ieeerates[IFM_SUBTYPE(mword)] : 0; 2445 } 2446 2447 /* 2448 * The following hash function is adapted from "Hash Functions" by Bob Jenkins 2449 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997). 2450 */ 2451 #define mix(a, b, c) \ 2452 do { \ 2453 a -= b; a -= c; a ^= (c >> 13); \ 2454 b -= c; b -= a; b ^= (a << 8); \ 2455 c -= a; c -= b; c ^= (b >> 13); \ 2456 a -= b; a -= c; a ^= (c >> 12); \ 2457 b -= c; b -= a; b ^= (a << 16); \ 2458 c -= a; c -= b; c ^= (b >> 5); \ 2459 a -= b; a -= c; a ^= (c >> 3); \ 2460 b -= c; b -= a; b ^= (a << 10); \ 2461 c -= a; c -= b; c ^= (b >> 15); \ 2462 } while (/*CONSTCOND*/0) 2463 2464 uint32_t 2465 ieee80211_mac_hash(const struct ieee80211com *ic, 2466 const uint8_t addr[IEEE80211_ADDR_LEN]) 2467 { 2468 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key; 2469 2470 b += addr[5] << 8; 2471 b += addr[4]; 2472 a += addr[3] << 24; 2473 a += addr[2] << 16; 2474 a += addr[1] << 8; 2475 a += addr[0]; 2476 2477 mix(a, b, c); 2478 2479 return c; 2480 } 2481 #undef mix 2482 2483 char 2484 ieee80211_channel_type_char(const struct ieee80211_channel *c) 2485 { 2486 if (IEEE80211_IS_CHAN_ST(c)) 2487 return 'S'; 2488 if (IEEE80211_IS_CHAN_108A(c)) 2489 return 'T'; 2490 if (IEEE80211_IS_CHAN_108G(c)) 2491 return 'G'; 2492 if (IEEE80211_IS_CHAN_VHT(c)) 2493 return 'v'; 2494 if (IEEE80211_IS_CHAN_HT(c)) 2495 return 'n'; 2496 if (IEEE80211_IS_CHAN_A(c)) 2497 return 'a'; 2498 if (IEEE80211_IS_CHAN_ANYG(c)) 2499 return 'g'; 2500 if (IEEE80211_IS_CHAN_B(c)) 2501 return 'b'; 2502 return 'f'; 2503 } 2504