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