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