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