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