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