ieee80211.c revision 165569
1/*- 2 * Copyright (c) 2001 Atsushi Onoe 3 * Copyright (c) 2002-2005 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 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * Alternatively, this software may be distributed under the terms of the 18 * GNU General Public License ("GPL") version 2 as published by the Free 19 * Software Foundation. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33#include <sys/cdefs.h> 34__FBSDID("$FreeBSD: head/sys/net80211/ieee80211.c 165569 2006-12-27 18:46:18Z sam $"); 35 36/* 37 * IEEE 802.11 generic handler 38 */ 39 40#include <sys/param.h> 41#include <sys/systm.h> 42#include <sys/kernel.h> 43 44#include <sys/socket.h> 45 46#include <net/if.h> 47#include <net/if_media.h> 48#include <net/ethernet.h> 49 50#include <net80211/ieee80211_var.h> 51 52#include <net/bpf.h> 53 54const char *ieee80211_phymode_name[] = { 55 "auto", /* IEEE80211_MODE_AUTO */ 56 "11a", /* IEEE80211_MODE_11A */ 57 "11b", /* IEEE80211_MODE_11B */ 58 "11g", /* IEEE80211_MODE_11G */ 59 "FH", /* IEEE80211_MODE_FH */ 60 "turboA", /* IEEE80211_MODE_TURBO_A */ 61 "turboG", /* IEEE80211_MODE_TURBO_G */ 62}; 63 64/* 65 * Default supported rates for 802.11 operation (in IEEE .5Mb units). 66 */ 67#define B(r) ((r) | IEEE80211_RATE_BASIC) 68static const struct ieee80211_rateset ieee80211_rateset_11a = 69 { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } }; 70static const struct ieee80211_rateset ieee80211_rateset_half = 71 { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } }; 72static const struct ieee80211_rateset ieee80211_rateset_quarter = 73 { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } }; 74static const struct ieee80211_rateset ieee80211_rateset_11b = 75 { 4, { B(2), B(4), B(11), B(22) } }; 76/* NB: OFDM rates are handled specially based on mode */ 77static const struct ieee80211_rateset ieee80211_rateset_11g = 78 { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } }; 79#undef B 80 81/* list of all instances */ 82SLIST_HEAD(ieee80211_list, ieee80211com); 83static struct ieee80211_list ieee80211_list = 84 SLIST_HEAD_INITIALIZER(ieee80211_list); 85static u_int8_t ieee80211_vapmap[32]; /* enough for 256 */ 86static struct mtx ieee80211_vap_mtx; 87MTX_SYSINIT(ieee80211, &ieee80211_vap_mtx, "net80211 instances", MTX_DEF); 88 89static void 90ieee80211_add_vap(struct ieee80211com *ic) 91{ 92#define N(a) (sizeof(a)/sizeof(a[0])) 93 int i; 94 u_int8_t b; 95 96 mtx_lock(&ieee80211_vap_mtx); 97 ic->ic_vap = 0; 98 for (i = 0; i < N(ieee80211_vapmap) && ieee80211_vapmap[i] == 0xff; i++) 99 ic->ic_vap += NBBY; 100 if (i == N(ieee80211_vapmap)) 101 panic("vap table full"); 102 for (b = ieee80211_vapmap[i]; b & 1; b >>= 1) 103 ic->ic_vap++; 104 setbit(ieee80211_vapmap, ic->ic_vap); 105 SLIST_INSERT_HEAD(&ieee80211_list, ic, ic_next); 106 mtx_unlock(&ieee80211_vap_mtx); 107#undef N 108} 109 110static void 111ieee80211_remove_vap(struct ieee80211com *ic) 112{ 113 mtx_lock(&ieee80211_vap_mtx); 114 SLIST_REMOVE(&ieee80211_list, ic, ieee80211com, ic_next); 115 KASSERT(ic->ic_vap < sizeof(ieee80211_vapmap)*NBBY, 116 ("invalid vap id %d", ic->ic_vap)); 117 KASSERT(isset(ieee80211_vapmap, ic->ic_vap), 118 ("vap id %d not allocated", ic->ic_vap)); 119 clrbit(ieee80211_vapmap, ic->ic_vap); 120 mtx_unlock(&ieee80211_vap_mtx); 121} 122 123/* 124 * Default reset method for use with the ioctl support. This 125 * method is invoked after any state change in the 802.11 126 * layer that should be propagated to the hardware but not 127 * require re-initialization of the 802.11 state machine (e.g 128 * rescanning for an ap). We always return ENETRESET which 129 * should cause the driver to re-initialize the device. Drivers 130 * can override this method to implement more optimized support. 131 */ 132static int 133ieee80211_default_reset(struct ifnet *ifp) 134{ 135 return ENETRESET; 136} 137 138/* 139 * Fill in 802.11 available channel set, mark 140 * all available channels as active, and pick 141 * a default channel if not already specified. 142 */ 143static void 144ieee80211_chan_init(struct ieee80211com *ic) 145{ 146#define RATESDEFINED(m) \ 147 ((ic->ic_modecaps & (1<<m)) && ic->ic_sup_rates[m].rs_nrates != 0) 148#define DEFAULTRATES(m, def) do { \ 149 if (!RATESDEFINED(m)) ic->ic_sup_rates[m] = def; \ 150} while (0) 151 struct ifnet *ifp = ic->ic_ifp; 152 struct ieee80211_channel *c; 153 int i; 154 155 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail)); 156 ic->ic_modecaps = 1<<IEEE80211_MODE_AUTO; 157 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 158 c = &ic->ic_channels[i]; 159 if (c->ic_flags) { 160 /* 161 * Verify driver passed us valid data. 162 */ 163 if (i != ieee80211_chan2ieee(ic, c)) { 164 if_printf(ifp, "bad channel ignored; " 165 "freq %u flags %x number %u\n", 166 c->ic_freq, c->ic_flags, i); 167 c->ic_flags = 0; /* NB: remove */ 168 continue; 169 } 170 setbit(ic->ic_chan_avail, i); 171 /* 172 * Identify mode capabilities. 173 */ 174 if (IEEE80211_IS_CHAN_A(c)) 175 ic->ic_modecaps |= 1<<IEEE80211_MODE_11A; 176 if (IEEE80211_IS_CHAN_B(c)) 177 ic->ic_modecaps |= 1<<IEEE80211_MODE_11B; 178 if (IEEE80211_IS_CHAN_PUREG(c)) 179 ic->ic_modecaps |= 1<<IEEE80211_MODE_11G; 180 if (IEEE80211_IS_CHAN_FHSS(c)) 181 ic->ic_modecaps |= 1<<IEEE80211_MODE_FH; 182 if (IEEE80211_IS_CHAN_T(c)) 183 ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO_A; 184 if (IEEE80211_IS_CHAN_108G(c)) 185 ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO_G; 186 if (ic->ic_curchan == NULL) { 187 /* arbitrarily pick the first channel */ 188 ic->ic_curchan = &ic->ic_channels[i]; 189 } 190 } 191 } 192 193 /* fillin well-known rate sets if driver has not specified */ 194 DEFAULTRATES(IEEE80211_MODE_11B, ieee80211_rateset_11b); 195 DEFAULTRATES(IEEE80211_MODE_11G, ieee80211_rateset_11g); 196 DEFAULTRATES(IEEE80211_MODE_11A, ieee80211_rateset_11a); 197 DEFAULTRATES(IEEE80211_MODE_TURBO_A, ieee80211_rateset_11a); 198 DEFAULTRATES(IEEE80211_MODE_TURBO_G, ieee80211_rateset_11g); 199 200 /* 201 * Set auto mode to reset active channel state and any desired channel. 202 */ 203 (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO); 204#undef DEFAULTRATES 205#undef RATESDEFINED 206} 207 208void 209ieee80211_ifattach(struct ieee80211com *ic) 210{ 211 struct ifnet *ifp = ic->ic_ifp; 212 213 ether_ifattach(ifp, ic->ic_myaddr); 214 ifp->if_output = ieee80211_output; 215 216 bpfattach2(ifp, DLT_IEEE802_11, 217 sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf); 218 219 ieee80211_crypto_attach(ic); 220 221 ic->ic_des_chan = IEEE80211_CHAN_ANYC; 222 /* 223 * Fill in 802.11 available channel set, mark all 224 * available channels as active, and pick a default 225 * channel if not already specified. 226 */ 227 ieee80211_chan_init(ic); 228#if 0 229 /* 230 * Enable WME by default if we're capable. 231 */ 232 if (ic->ic_caps & IEEE80211_C_WME) 233 ic->ic_flags |= IEEE80211_F_WME; 234#endif 235 if (ic->ic_caps & IEEE80211_C_BURST) 236 ic->ic_flags |= IEEE80211_F_BURST; 237 238 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT; 239 ic->ic_bmissthreshold = IEEE80211_HWBMISS_DEFAULT; 240 ic->ic_dtim_period = IEEE80211_DTIM_DEFAULT; 241 IEEE80211_BEACON_LOCK_INIT(ic, "beacon"); 242 243 ic->ic_lintval = ic->ic_bintval; 244 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX; 245 246 ieee80211_node_attach(ic); 247 ieee80211_proto_attach(ic); 248 249 ieee80211_add_vap(ic); 250 251 ieee80211_sysctl_attach(ic); /* NB: requires ic_vap */ 252 253 /* 254 * Install a default reset method for the ioctl support. 255 * The driver is expected to fill this in before calling us. 256 */ 257 if (ic->ic_reset == NULL) 258 ic->ic_reset = ieee80211_default_reset; 259 260 KASSERT(ifp->if_spare2 == NULL, ("oops, hosed")); 261 ifp->if_spare2 = ic; /* XXX temp backpointer */ 262} 263 264void 265ieee80211_ifdetach(struct ieee80211com *ic) 266{ 267 struct ifnet *ifp = ic->ic_ifp; 268 269 ieee80211_remove_vap(ic); 270 271 ieee80211_sysctl_detach(ic); 272 ieee80211_proto_detach(ic); 273 ieee80211_crypto_detach(ic); 274 ieee80211_node_detach(ic); 275 ifmedia_removeall(&ic->ic_media); 276 277 IEEE80211_BEACON_LOCK_DESTROY(ic); 278 279 bpfdetach(ifp); 280 ether_ifdetach(ifp); 281} 282 283/* 284 * Convert MHz frequency to IEEE channel number. 285 */ 286int 287ieee80211_mhz2ieee(u_int freq, u_int flags) 288{ 289 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 290 if (freq == 2484) 291 return 14; 292 if (freq < 2484) 293 return ((int) freq - 2407) / 5; 294 else 295 return 15 + ((freq - 2512) / 20); 296 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */ 297 if (freq <= 5000) { 298 if (flags &(IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) 299 return 37 + ((freq * 10) + 300 ((freq % 5) == 2 ? 5 : 0) - 49400) / 5; 301 return (freq - 4000) / 5; 302 } else 303 return (freq - 5000) / 5; 304 } else { /* either, guess */ 305 if (freq == 2484) 306 return 14; 307 if (freq < 2484) 308 return ((int) freq - 2407) / 5; 309 if (freq < 5000) { 310 if (flags &(IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) 311 return 37 + ((freq * 10) + 312 ((freq % 5) == 2 ? 5 : 0) - 49400) / 5; 313 else if (freq > 4900) 314 return (freq - 4000) / 5; 315 else 316 return 15 + ((freq - 2512) / 20); 317 } 318 return (freq - 5000) / 5; 319 } 320} 321 322/* 323 * Convert channel to IEEE channel number. 324 */ 325int 326ieee80211_chan2ieee(struct ieee80211com *ic, struct ieee80211_channel *c) 327{ 328 if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX]) 329 return c - ic->ic_channels; 330 else if (c == IEEE80211_CHAN_ANYC) 331 return IEEE80211_CHAN_ANY; 332 else if (c != NULL) { 333 if_printf(ic->ic_ifp, "invalid channel freq %u flags %x\n", 334 c->ic_freq, c->ic_flags); 335 return 0; /* XXX */ 336 } else { 337 if_printf(ic->ic_ifp, "invalid channel (NULL)\n"); 338 return 0; /* XXX */ 339 } 340} 341 342/* 343 * Convert IEEE channel number to MHz frequency. 344 */ 345u_int 346ieee80211_ieee2mhz(u_int chan, u_int flags) 347{ 348 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 349 if (chan == 14) 350 return 2484; 351 if (chan < 14) 352 return 2407 + chan*5; 353 else 354 return 2512 + ((chan-15)*20); 355 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */ 356 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) { 357 chan -= 37; 358 return 4940 + chan*5 + (chan % 5 ? 2 : 0); 359 } 360 return 5000 + (chan*5); 361 } else { /* either, guess */ 362 if (chan == 14) 363 return 2484; 364 if (chan < 14) /* 0-13 */ 365 return 2407 + chan*5; 366 if (chan < 27) /* 15-26 */ 367 return 2512 + ((chan-15)*20); 368 /* XXX can't distinguish PSB channels */ 369 return 5000 + (chan*5); 370 } 371} 372 373/* 374 * Setup the media data structures according to the channel and 375 * rate tables. This must be called by the driver after 376 * ieee80211_attach and before most anything else. 377 */ 378void 379ieee80211_media_init(struct ieee80211com *ic, 380 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat) 381{ 382#define ADD(_ic, _s, _o) \ 383 ifmedia_add(&(_ic)->ic_media, \ 384 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL) 385 struct ifnet *ifp = ic->ic_ifp; 386 struct ifmediareq imr; 387 int i, j, mode, rate, maxrate, mword, mopt, r; 388 struct ieee80211_rateset *rs; 389 struct ieee80211_rateset allrates; 390 391 /* NB: this works because the structure is initialized to zero */ 392 if (LIST_EMPTY(&ic->ic_media.ifm_list)) { 393 /* 394 * Do late attach work that must wait for any subclass 395 * (i.e. driver) work such as overriding methods. 396 */ 397 ieee80211_node_lateattach(ic); 398 } else { 399 /* 400 * We are re-initializing the channel list; clear 401 * the existing media state as the media routines 402 * don't suppress duplicates. 403 */ 404 ifmedia_removeall(&ic->ic_media); 405 ieee80211_chan_init(ic); 406 } 407 408 /* 409 * Fill in media characteristics. 410 */ 411 ifmedia_init(&ic->ic_media, 0, media_change, media_stat); 412 maxrate = 0; 413 memset(&allrates, 0, sizeof(allrates)); 414 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) { 415 static const u_int mopts[] = { 416 IFM_AUTO, 417 IFM_IEEE80211_11A, 418 IFM_IEEE80211_11B, 419 IFM_IEEE80211_11G, 420 IFM_IEEE80211_FH, 421 IFM_IEEE80211_11A | IFM_IEEE80211_TURBO, 422 IFM_IEEE80211_11G | IFM_IEEE80211_TURBO, 423 }; 424 if ((ic->ic_modecaps & (1<<mode)) == 0) 425 continue; 426 mopt = mopts[mode]; 427 ADD(ic, IFM_AUTO, mopt); /* e.g. 11a auto */ 428 if (ic->ic_caps & IEEE80211_C_IBSS) 429 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC); 430 if (ic->ic_caps & IEEE80211_C_HOSTAP) 431 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP); 432 if (ic->ic_caps & IEEE80211_C_AHDEMO) 433 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0); 434 if (ic->ic_caps & IEEE80211_C_MONITOR) 435 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR); 436 if (mode == IEEE80211_MODE_AUTO) 437 continue; 438 rs = &ic->ic_sup_rates[mode]; 439 for (i = 0; i < rs->rs_nrates; i++) { 440 rate = rs->rs_rates[i]; 441 mword = ieee80211_rate2media(ic, rate, mode); 442 if (mword == 0) 443 continue; 444 ADD(ic, mword, mopt); 445 if (ic->ic_caps & IEEE80211_C_IBSS) 446 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC); 447 if (ic->ic_caps & IEEE80211_C_HOSTAP) 448 ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP); 449 if (ic->ic_caps & IEEE80211_C_AHDEMO) 450 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0); 451 if (ic->ic_caps & IEEE80211_C_MONITOR) 452 ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR); 453 /* 454 * Add rate to the collection of all rates. 455 */ 456 r = rate & IEEE80211_RATE_VAL; 457 for (j = 0; j < allrates.rs_nrates; j++) 458 if (allrates.rs_rates[j] == r) 459 break; 460 if (j == allrates.rs_nrates) { 461 /* unique, add to the set */ 462 allrates.rs_rates[j] = r; 463 allrates.rs_nrates++; 464 } 465 rate = (rate & IEEE80211_RATE_VAL) / 2; 466 if (rate > maxrate) 467 maxrate = rate; 468 } 469 } 470 for (i = 0; i < allrates.rs_nrates; i++) { 471 mword = ieee80211_rate2media(ic, allrates.rs_rates[i], 472 IEEE80211_MODE_AUTO); 473 if (mword == 0) 474 continue; 475 mword = IFM_SUBTYPE(mword); /* remove media options */ 476 ADD(ic, mword, 0); 477 if (ic->ic_caps & IEEE80211_C_IBSS) 478 ADD(ic, mword, IFM_IEEE80211_ADHOC); 479 if (ic->ic_caps & IEEE80211_C_HOSTAP) 480 ADD(ic, mword, IFM_IEEE80211_HOSTAP); 481 if (ic->ic_caps & IEEE80211_C_AHDEMO) 482 ADD(ic, mword, IFM_IEEE80211_ADHOC | IFM_FLAG0); 483 if (ic->ic_caps & IEEE80211_C_MONITOR) 484 ADD(ic, mword, IFM_IEEE80211_MONITOR); 485 } 486 ieee80211_media_status(ifp, &imr); 487 ifmedia_set(&ic->ic_media, imr.ifm_active); 488 489 if (maxrate) 490 ifp->if_baudrate = IF_Mbps(maxrate); 491#undef ADD 492} 493 494const struct ieee80211_rateset * 495ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c) 496{ 497 enum ieee80211_phymode mode = ieee80211_chan2mode(ic, c); 498 499 if (mode == IEEE80211_MODE_11A) { 500 if (IEEE80211_IS_CHAN_HALF(c)) 501 return &ieee80211_rateset_half; 502 if (IEEE80211_IS_CHAN_QUARTER(c)) 503 return &ieee80211_rateset_quarter; 504 } 505 return &ic->ic_sup_rates[mode]; 506} 507 508void 509ieee80211_announce(struct ieee80211com *ic) 510{ 511 struct ifnet *ifp = ic->ic_ifp; 512 int i, mode, rate, mword; 513 struct ieee80211_rateset *rs; 514 515 for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) { 516 if ((ic->ic_modecaps & (1<<mode)) == 0) 517 continue; 518 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]); 519 rs = &ic->ic_sup_rates[mode]; 520 for (i = 0; i < rs->rs_nrates; i++) { 521 rate = rs->rs_rates[i]; 522 mword = ieee80211_rate2media(ic, rate, mode); 523 if (mword == 0) 524 continue; 525 printf("%s%d%sMbps", (i != 0 ? " " : ""), 526 (rate & IEEE80211_RATE_VAL) / 2, 527 ((rate & 0x1) != 0 ? ".5" : "")); 528 } 529 printf("\n"); 530 } 531} 532 533static int 534findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate) 535{ 536#define IEEERATE(_ic,_m,_i) \ 537 ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL) 538 int i, nrates = ic->ic_sup_rates[mode].rs_nrates; 539 for (i = 0; i < nrates; i++) 540 if (IEEERATE(ic, mode, i) == rate) 541 return i; 542 return -1; 543#undef IEEERATE 544} 545 546/* 547 * Find an instance by it's mac address. 548 */ 549struct ieee80211com * 550ieee80211_find_vap(const u_int8_t mac[IEEE80211_ADDR_LEN]) 551{ 552 struct ieee80211com *ic; 553 554 /* XXX lock */ 555 SLIST_FOREACH(ic, &ieee80211_list, ic_next) 556 if (IEEE80211_ADDR_EQ(mac, ic->ic_myaddr)) 557 return ic; 558 return NULL; 559} 560 561static struct ieee80211com * 562ieee80211_find_instance(struct ifnet *ifp) 563{ 564 struct ieee80211com *ic; 565 566 /* XXX lock */ 567 /* XXX not right for multiple instances but works for now */ 568 SLIST_FOREACH(ic, &ieee80211_list, ic_next) 569 if (ic->ic_ifp == ifp) 570 return ic; 571 return NULL; 572} 573 574/* 575 * Handle a media change request. 576 */ 577int 578ieee80211_media_change(struct ifnet *ifp) 579{ 580 struct ieee80211com *ic; 581 struct ifmedia_entry *ime; 582 enum ieee80211_opmode newopmode; 583 enum ieee80211_phymode newphymode; 584 int i, j, newrate, error = 0; 585 586 ic = ieee80211_find_instance(ifp); 587 if (!ic) { 588 if_printf(ifp, "%s: no 802.11 instance!\n", __func__); 589 return EINVAL; 590 } 591 ime = ic->ic_media.ifm_cur; 592 /* 593 * First, identify the phy mode. 594 */ 595 switch (IFM_MODE(ime->ifm_media)) { 596 case IFM_IEEE80211_11A: 597 newphymode = IEEE80211_MODE_11A; 598 break; 599 case IFM_IEEE80211_11B: 600 newphymode = IEEE80211_MODE_11B; 601 break; 602 case IFM_IEEE80211_11G: 603 newphymode = IEEE80211_MODE_11G; 604 break; 605 case IFM_IEEE80211_FH: 606 newphymode = IEEE80211_MODE_FH; 607 break; 608 case IFM_AUTO: 609 newphymode = IEEE80211_MODE_AUTO; 610 break; 611 default: 612 return EINVAL; 613 } 614 /* 615 * Turbo mode is an ``option''. 616 * XXX does not apply to AUTO 617 */ 618 if (ime->ifm_media & IFM_IEEE80211_TURBO) { 619 if (newphymode == IEEE80211_MODE_11A) 620 newphymode = IEEE80211_MODE_TURBO_A; 621 else if (newphymode == IEEE80211_MODE_11G) 622 newphymode = IEEE80211_MODE_TURBO_G; 623 else 624 return EINVAL; 625 } 626 /* 627 * Validate requested mode is available. 628 */ 629 if ((ic->ic_modecaps & (1<<newphymode)) == 0) 630 return EINVAL; 631 632 /* 633 * Next, the fixed/variable rate. 634 */ 635 i = -1; 636 if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) { 637 /* 638 * Convert media subtype to rate. 639 */ 640 newrate = ieee80211_media2rate(ime->ifm_media); 641 if (newrate == 0) 642 return EINVAL; 643 /* 644 * Check the rate table for the specified/current phy. 645 */ 646 if (newphymode == IEEE80211_MODE_AUTO) { 647 /* 648 * In autoselect mode search for the rate. 649 */ 650 for (j = IEEE80211_MODE_11A; 651 j < IEEE80211_MODE_MAX; j++) { 652 if ((ic->ic_modecaps & (1<<j)) == 0) 653 continue; 654 i = findrate(ic, j, newrate); 655 if (i != -1) { 656 /* lock mode too */ 657 newphymode = j; 658 break; 659 } 660 } 661 } else { 662 i = findrate(ic, newphymode, newrate); 663 } 664 if (i == -1) /* mode/rate mismatch */ 665 return EINVAL; 666 } 667 /* NB: defer rate setting to later */ 668 669 /* 670 * Deduce new operating mode but don't install it just yet. 671 */ 672 if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) == 673 (IFM_IEEE80211_ADHOC|IFM_FLAG0)) 674 newopmode = IEEE80211_M_AHDEMO; 675 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP) 676 newopmode = IEEE80211_M_HOSTAP; 677 else if (ime->ifm_media & IFM_IEEE80211_ADHOC) 678 newopmode = IEEE80211_M_IBSS; 679 else if (ime->ifm_media & IFM_IEEE80211_MONITOR) 680 newopmode = IEEE80211_M_MONITOR; 681 else 682 newopmode = IEEE80211_M_STA; 683 684 /* 685 * Autoselect doesn't make sense when operating as an AP. 686 * If no phy mode has been selected, pick one and lock it 687 * down so rate tables can be used in forming beacon frames 688 * and the like. 689 */ 690 if (newopmode == IEEE80211_M_HOSTAP && 691 newphymode == IEEE80211_MODE_AUTO) { 692 for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++) 693 if (ic->ic_modecaps & (1<<j)) { 694 newphymode = j; 695 break; 696 } 697 } 698 699 /* 700 * Handle phy mode change. 701 */ 702 if (ic->ic_curmode != newphymode) { /* change phy mode */ 703 error = ieee80211_setmode(ic, newphymode); 704 if (error != 0) 705 return error; 706 error = ENETRESET; 707 } 708 709 /* 710 * Committed to changes, install the rate setting. 711 */ 712 if (ic->ic_fixed_rate != i) { 713 ic->ic_fixed_rate = i; /* set fixed tx rate */ 714 error = ENETRESET; 715 } 716 717 /* 718 * Handle operating mode change. 719 */ 720 if (ic->ic_opmode != newopmode) { 721 ic->ic_opmode = newopmode; 722 switch (newopmode) { 723 case IEEE80211_M_AHDEMO: 724 case IEEE80211_M_HOSTAP: 725 case IEEE80211_M_STA: 726 case IEEE80211_M_MONITOR: 727 ic->ic_flags &= ~IEEE80211_F_IBSSON; 728 break; 729 case IEEE80211_M_IBSS: 730 ic->ic_flags |= IEEE80211_F_IBSSON; 731 break; 732 } 733 /* 734 * Yech, slot time may change depending on the 735 * operating mode so reset it to be sure everything 736 * is setup appropriately. 737 */ 738 ieee80211_reset_erp(ic); 739 ieee80211_wme_initparams(ic); /* after opmode change */ 740 error = ENETRESET; 741 } 742#ifdef notdef 743 if (error == 0) 744 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media); 745#endif 746 return error; 747} 748 749void 750ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr) 751{ 752 struct ieee80211com *ic; 753 const struct ieee80211_rateset *rs; 754 755 ic = ieee80211_find_instance(ifp); 756 if (!ic) { 757 if_printf(ifp, "%s: no 802.11 instance!\n", __func__); 758 return; 759 } 760 imr->ifm_status = IFM_AVALID; 761 imr->ifm_active = IFM_IEEE80211; 762 if (ic->ic_state == IEEE80211_S_RUN) 763 imr->ifm_status |= IFM_ACTIVE; 764 /* 765 * Calculate a current rate if possible. 766 */ 767 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) { 768 /* 769 * A fixed rate is set, report that. 770 */ 771 rs = &ic->ic_sup_rates[ic->ic_curmode]; 772 imr->ifm_active |= ieee80211_rate2media(ic, 773 rs->rs_rates[ic->ic_fixed_rate], ic->ic_curmode); 774 } else if (ic->ic_opmode == IEEE80211_M_STA) { 775 /* 776 * In station mode report the current transmit rate. 777 */ 778 rs = &ic->ic_bss->ni_rates; 779 imr->ifm_active |= ieee80211_rate2media(ic, 780 rs->rs_rates[ic->ic_bss->ni_txrate], ic->ic_curmode); 781 } else 782 imr->ifm_active |= IFM_AUTO; 783 switch (ic->ic_opmode) { 784 case IEEE80211_M_STA: 785 break; 786 case IEEE80211_M_IBSS: 787 imr->ifm_active |= IFM_IEEE80211_ADHOC; 788 break; 789 case IEEE80211_M_AHDEMO: 790 /* should not come here */ 791 break; 792 case IEEE80211_M_HOSTAP: 793 imr->ifm_active |= IFM_IEEE80211_HOSTAP; 794 break; 795 case IEEE80211_M_MONITOR: 796 imr->ifm_active |= IFM_IEEE80211_MONITOR; 797 break; 798 } 799 switch (ic->ic_curmode) { 800 case IEEE80211_MODE_11A: 801 imr->ifm_active |= IFM_IEEE80211_11A; 802 break; 803 case IEEE80211_MODE_11B: 804 imr->ifm_active |= IFM_IEEE80211_11B; 805 break; 806 case IEEE80211_MODE_11G: 807 imr->ifm_active |= IFM_IEEE80211_11G; 808 break; 809 case IEEE80211_MODE_FH: 810 imr->ifm_active |= IFM_IEEE80211_FH; 811 break; 812 case IEEE80211_MODE_TURBO_A: 813 imr->ifm_active |= IFM_IEEE80211_11A 814 | IFM_IEEE80211_TURBO; 815 break; 816 case IEEE80211_MODE_TURBO_G: 817 imr->ifm_active |= IFM_IEEE80211_11G 818 | IFM_IEEE80211_TURBO; 819 break; 820 } 821} 822 823void 824ieee80211_watchdog(struct ieee80211com *ic) 825{ 826 struct ieee80211_node_table *nt; 827 int need_inact_timer = 0; 828 829 if (ic->ic_state != IEEE80211_S_INIT) { 830 if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0) 831 ieee80211_new_state(ic, IEEE80211_S_SCAN, 0); 832 nt = &ic->ic_scan; 833 if (nt->nt_inact_timer) { 834 if (--nt->nt_inact_timer == 0) 835 nt->nt_timeout(nt); 836 need_inact_timer += nt->nt_inact_timer; 837 } 838 nt = &ic->ic_sta; 839 if (nt->nt_inact_timer) { 840 if (--nt->nt_inact_timer == 0) 841 nt->nt_timeout(nt); 842 need_inact_timer += nt->nt_inact_timer; 843 } 844 } 845 if (ic->ic_mgt_timer != 0 || need_inact_timer) 846 ic->ic_ifp->if_timer = 1; 847} 848 849/* 850 * Set the current phy mode and recalculate the active channel 851 * set based on the available channels for this mode. Also 852 * select a new default/current channel if the current one is 853 * inappropriate for this mode. 854 */ 855int 856ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode) 857{ 858#define N(a) (sizeof(a) / sizeof(a[0])) 859 static const u_int chanflags[] = { 860 0, /* IEEE80211_MODE_AUTO */ 861 IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */ 862 IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */ 863 IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */ 864 IEEE80211_CHAN_FHSS, /* IEEE80211_MODE_FH */ 865 IEEE80211_CHAN_T, /* IEEE80211_MODE_TURBO_A */ 866 IEEE80211_CHAN_108G, /* IEEE80211_MODE_TURBO_G */ 867 }; 868 struct ieee80211_channel *c; 869 u_int modeflags; 870 int i; 871 872 /* validate new mode */ 873 if ((ic->ic_modecaps & (1<<mode)) == 0) { 874 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 875 "%s: mode %u not supported (caps 0x%x)\n", 876 __func__, mode, ic->ic_modecaps); 877 return EINVAL; 878 } 879 880 /* 881 * Verify at least one channel is present in the available 882 * channel list before committing to the new mode. 883 */ 884 KASSERT(mode < N(chanflags), ("Unexpected mode %u", mode)); 885 modeflags = chanflags[mode]; 886 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 887 c = &ic->ic_channels[i]; 888 if (c->ic_flags == 0) 889 continue; 890 if (mode == IEEE80211_MODE_AUTO) { 891 /* ignore static turbo channels for autoselect */ 892 if (!IEEE80211_IS_CHAN_T(c)) 893 break; 894 } else { 895 if ((c->ic_flags & modeflags) == modeflags) 896 break; 897 } 898 } 899 if (i > IEEE80211_CHAN_MAX) { 900 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 901 "%s: no channels found for mode %u\n", __func__, mode); 902 return EINVAL; 903 } 904 905 /* 906 * Calculate the active channel set. 907 */ 908 memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active)); 909 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 910 c = &ic->ic_channels[i]; 911 if (c->ic_flags == 0) 912 continue; 913 if (mode == IEEE80211_MODE_AUTO) { 914 /* take anything but static turbo channels */ 915 if (!IEEE80211_IS_CHAN_T(c)) 916 setbit(ic->ic_chan_active, i); 917 } else { 918 if ((c->ic_flags & modeflags) == modeflags) 919 setbit(ic->ic_chan_active, i); 920 } 921 } 922 /* 923 * If no current/default channel is setup or the current 924 * channel is wrong for the mode then pick the first 925 * available channel from the active list. This is likely 926 * not the right one. 927 */ 928 if (ic->ic_ibss_chan == NULL || 929 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) { 930 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) 931 if (isset(ic->ic_chan_active, i)) { 932 ic->ic_ibss_chan = &ic->ic_channels[i]; 933 break; 934 } 935 KASSERT(ic->ic_ibss_chan != NULL && 936 isset(ic->ic_chan_active, 937 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)), 938 ("Bad IBSS channel %u", 939 ieee80211_chan2ieee(ic, ic->ic_ibss_chan))); 940 } 941 /* 942 * If the desired channel is set but no longer valid then reset it. 943 */ 944 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC && 945 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_des_chan))) 946 ic->ic_des_chan = IEEE80211_CHAN_ANYC; 947 948 /* 949 * Do mode-specific rate setup. 950 */ 951 if (mode == IEEE80211_MODE_11G) { 952 /* 953 * Use a mixed 11b/11g rate set. 954 */ 955 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode], 956 IEEE80211_MODE_11G); 957 } else if (mode == IEEE80211_MODE_11B) { 958 /* 959 * Force pure 11b rate set. 960 */ 961 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode], 962 IEEE80211_MODE_11B); 963 } 964 /* 965 * Setup an initial rate set according to the 966 * current/default channel selected above. This 967 * will be changed when scanning but must exist 968 * now so driver have a consistent state of ic_ibss_chan. 969 */ 970 if (ic->ic_bss) /* NB: can be called before lateattach */ 971 ic->ic_bss->ni_rates = ic->ic_sup_rates[mode]; 972 973 ic->ic_curmode = mode; 974 ieee80211_reset_erp(ic); /* reset ERP state */ 975 ieee80211_wme_initparams(ic); /* reset WME stat */ 976 977 return 0; 978#undef N 979} 980 981/* 982 * Return the phy mode for with the specified channel so the 983 * caller can select a rate set. This is problematic for channels 984 * where multiple operating modes are possible (e.g. 11g+11b). 985 * In those cases we defer to the current operating mode when set. 986 */ 987enum ieee80211_phymode 988ieee80211_chan2mode(struct ieee80211com *ic, const struct ieee80211_channel *chan) 989{ 990 if (IEEE80211_IS_CHAN_T(chan)) { 991 return IEEE80211_MODE_TURBO_A; 992 } else if (IEEE80211_IS_CHAN_5GHZ(chan)) { 993 return IEEE80211_MODE_11A; 994 } else if (IEEE80211_IS_CHAN_FHSS(chan)) 995 return IEEE80211_MODE_FH; 996 else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN)) { 997 /* 998 * This assumes all 11g channels are also usable 999 * for 11b, which is currently true. 1000 */ 1001 if (ic->ic_curmode == IEEE80211_MODE_TURBO_G) 1002 return IEEE80211_MODE_TURBO_G; 1003 if (ic->ic_curmode == IEEE80211_MODE_11B) 1004 return IEEE80211_MODE_11B; 1005 return IEEE80211_MODE_11G; 1006 } else 1007 return IEEE80211_MODE_11B; 1008} 1009 1010/* 1011 * convert IEEE80211 rate value to ifmedia subtype. 1012 * ieee80211 rate is in unit of 0.5Mbps. 1013 */ 1014int 1015ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode) 1016{ 1017#define N(a) (sizeof(a) / sizeof(a[0])) 1018 static const struct { 1019 u_int m; /* rate + mode */ 1020 u_int r; /* if_media rate */ 1021 } rates[] = { 1022 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 }, 1023 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 }, 1024 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 }, 1025 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 }, 1026 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 }, 1027 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 }, 1028 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 }, 1029 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 }, 1030 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 }, 1031 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 }, 1032 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 }, 1033 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 }, 1034 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 }, 1035 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 }, 1036 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 }, 1037 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 }, 1038 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 }, 1039 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 }, 1040 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 }, 1041 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 }, 1042 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 }, 1043 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 }, 1044 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 }, 1045 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 }, 1046 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 }, 1047 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 }, 1048 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 }, 1049 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 }, 1050 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 }, 1051 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 }, 1052 /* NB: OFDM72 doesn't realy exist so we don't handle it */ 1053 }; 1054 u_int mask, i; 1055 1056 mask = rate & IEEE80211_RATE_VAL; 1057 switch (mode) { 1058 case IEEE80211_MODE_11A: 1059 case IEEE80211_MODE_TURBO_A: 1060 mask |= IFM_IEEE80211_11A; 1061 break; 1062 case IEEE80211_MODE_11B: 1063 mask |= IFM_IEEE80211_11B; 1064 break; 1065 case IEEE80211_MODE_FH: 1066 mask |= IFM_IEEE80211_FH; 1067 break; 1068 case IEEE80211_MODE_AUTO: 1069 /* NB: ic may be NULL for some drivers */ 1070 if (ic && ic->ic_phytype == IEEE80211_T_FH) { 1071 mask |= IFM_IEEE80211_FH; 1072 break; 1073 } 1074 /* NB: hack, 11g matches both 11b+11a rates */ 1075 /* fall thru... */ 1076 case IEEE80211_MODE_11G: 1077 case IEEE80211_MODE_TURBO_G: 1078 mask |= IFM_IEEE80211_11G; 1079 break; 1080 } 1081 for (i = 0; i < N(rates); i++) 1082 if (rates[i].m == mask) 1083 return rates[i].r; 1084 return IFM_AUTO; 1085#undef N 1086} 1087 1088int 1089ieee80211_media2rate(int mword) 1090{ 1091#define N(a) (sizeof(a) / sizeof(a[0])) 1092 static const int ieeerates[] = { 1093 -1, /* IFM_AUTO */ 1094 0, /* IFM_MANUAL */ 1095 0, /* IFM_NONE */ 1096 2, /* IFM_IEEE80211_FH1 */ 1097 4, /* IFM_IEEE80211_FH2 */ 1098 2, /* IFM_IEEE80211_DS1 */ 1099 4, /* IFM_IEEE80211_DS2 */ 1100 11, /* IFM_IEEE80211_DS5 */ 1101 22, /* IFM_IEEE80211_DS11 */ 1102 44, /* IFM_IEEE80211_DS22 */ 1103 12, /* IFM_IEEE80211_OFDM6 */ 1104 18, /* IFM_IEEE80211_OFDM9 */ 1105 24, /* IFM_IEEE80211_OFDM12 */ 1106 36, /* IFM_IEEE80211_OFDM18 */ 1107 48, /* IFM_IEEE80211_OFDM24 */ 1108 72, /* IFM_IEEE80211_OFDM36 */ 1109 96, /* IFM_IEEE80211_OFDM48 */ 1110 108, /* IFM_IEEE80211_OFDM54 */ 1111 144, /* IFM_IEEE80211_OFDM72 */ 1112 0, /* IFM_IEEE80211_DS354k */ 1113 0, /* IFM_IEEE80211_DS512k */ 1114 6, /* IFM_IEEE80211_OFDM3 */ 1115 9, /* IFM_IEEE80211_OFDM4 */ 1116 54, /* IFM_IEEE80211_OFDM27 */ 1117 }; 1118 return IFM_SUBTYPE(mword) < N(ieeerates) ? 1119 ieeerates[IFM_SUBTYPE(mword)] : 0; 1120#undef N 1121} 1122