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