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