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