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