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