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