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