404 return c; 405 } 406 return NULL; 407} 408static const u_int chanflags[IEEE80211_MODE_MAX] = { 409 IEEE80211_CHAN_B, /* IEEE80211_MODE_AUTO */ 410 IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */ 411 IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */ 412 IEEE80211_CHAN_G, /* IEEE80211_MODE_11G */ 413 IEEE80211_CHAN_FHSS, /* IEEE80211_MODE_FH */ 414 IEEE80211_CHAN_A, /* IEEE80211_MODE_TURBO_A (check base channel)*/ 415 IEEE80211_CHAN_G, /* IEEE80211_MODE_TURBO_G */ 416 IEEE80211_CHAN_ST, /* IEEE80211_MODE_STURBO_A */ 417 IEEE80211_CHAN_A, /* IEEE80211_MODE_11NA (check legacy) */ 418 IEEE80211_CHAN_G, /* IEEE80211_MODE_11NG (check legacy) */ 419}; 420 421static void 422add_channels(struct ieee80211vap *vap, 423 struct ieee80211_scan_state *ss, 424 enum ieee80211_phymode mode, const uint16_t freq[], int nfreq) 425{ 426#define N(a) (sizeof(a) / sizeof(a[0])) 427 struct ieee80211com *ic = vap->iv_ic; 428 struct ieee80211_channel *c, *cg; 429 u_int modeflags; 430 int i; 431 432 KASSERT(mode < N(chanflags), ("Unexpected mode %u", mode)); 433 modeflags = chanflags[mode]; 434 for (i = 0; i < nfreq; i++) { 435 if (ss->ss_last >= IEEE80211_SCAN_MAX) 436 break; 437 438 c = ieee80211_find_channel(ic, freq[i], modeflags); 439 if (c == NULL || isexcluded(vap, c)) 440 continue; 441 if (mode == IEEE80211_MODE_AUTO) { 442 /* 443 * XXX special-case 11b/g channels so we select 444 * the g channel if both are present. 445 */ 446 if (IEEE80211_IS_CHAN_B(c) && 447 (cg = find11gchannel(ic, i, c->ic_freq)) != NULL) 448 c = cg; 449 } 450 ss->ss_chans[ss->ss_last++] = c; 451 } 452#undef N 453} 454 455struct scanlist { 456 uint16_t mode; 457 uint16_t count; 458 const uint16_t *list; 459}; 460 461static int 462checktable(const struct scanlist *scan, const struct ieee80211_channel *c) 463{ 464 int i; 465 466 for (; scan->list != NULL; scan++) { 467 for (i = 0; i < scan->count; i++) 468 if (scan->list[i] == c->ic_freq) 469 return 1; 470 } 471 return 0; 472} 473 474static void 475sweepchannels(struct ieee80211_scan_state *ss, struct ieee80211vap *vap, 476 const struct scanlist table[]) 477{ 478 struct ieee80211com *ic = vap->iv_ic; 479 struct ieee80211_channel *c; 480 int i; 481 482 for (i = 0; i < ic->ic_nchans; i++) { 483 if (ss->ss_last >= IEEE80211_SCAN_MAX) 484 break; 485 486 c = &ic->ic_channels[i]; 487 /* 488 * Ignore dynamic turbo channels; we scan them 489 * in normal mode (i.e. not boosted). Likewise 490 * for HT channels, they get scanned using 491 * legacy rates. 492 */ 493 if (IEEE80211_IS_CHAN_DTURBO(c) || IEEE80211_IS_CHAN_HT(c)) 494 continue; 495 496 /* 497 * If a desired mode was specified, scan only 498 * channels that satisfy that constraint. 499 */ 500 if (vap->iv_des_mode != IEEE80211_MODE_AUTO && 501 vap->iv_des_mode != ieee80211_chan2mode(c)) 502 continue; 503 504 /* 505 * Skip channels excluded by user request. 506 */ 507 if (isexcluded(vap, c)) 508 continue; 509 510 /* 511 * Add the channel unless it is listed in the 512 * fixed scan order tables. This insures we 513 * don't sweep back in channels we filtered out 514 * above. 515 */ 516 if (checktable(table, c)) 517 continue; 518 519 /* Add channel to scanning list. */ 520 ss->ss_chans[ss->ss_last++] = c; 521 } 522} 523 524static void 525makescanlist(struct ieee80211_scan_state *ss, struct ieee80211vap *vap, 526 const struct scanlist table[]) 527{ 528 const struct scanlist *scan; 529 enum ieee80211_phymode mode; 530 531 ss->ss_last = 0; 532 /* 533 * Use the table of ordered channels to construct the list 534 * of channels for scanning. Any channels in the ordered 535 * list not in the master list will be discarded. 536 */ 537 for (scan = table; scan->list != NULL; scan++) { 538 mode = scan->mode; 539 if (vap->iv_des_mode != IEEE80211_MODE_AUTO) { 540 /* 541 * If a desired mode was specified, scan only 542 * channels that satisfy that constraint. 543 */ 544 if (vap->iv_des_mode != mode) { 545 /* 546 * The scan table marks 2.4Ghz channels as b 547 * so if the desired mode is 11g, then use 548 * the 11b channel list but upgrade the mode. 549 */ 550 if (vap->iv_des_mode != IEEE80211_MODE_11G || 551 mode != IEEE80211_MODE_11B) 552 continue; 553 mode = IEEE80211_MODE_11G; /* upgrade */ 554 } 555 } else { 556 /* 557 * This lets add_channels upgrade an 11b channel 558 * to 11g if available. 559 */ 560 if (mode == IEEE80211_MODE_11B) 561 mode = IEEE80211_MODE_AUTO; 562 } 563#ifdef IEEE80211_F_XR 564 /* XR does not operate on turbo channels */ 565 if ((vap->iv_flags & IEEE80211_F_XR) && 566 (mode == IEEE80211_MODE_TURBO_A || 567 mode == IEEE80211_MODE_TURBO_G || 568 mode == IEEE80211_MODE_STURBO_A)) 569 continue; 570#endif 571 /* 572 * Add the list of the channels; any that are not 573 * in the master channel list will be discarded. 574 */ 575 add_channels(vap, ss, mode, scan->list, scan->count); 576 } 577 578 /* 579 * Add the channels from the ic that are not present 580 * in the table. 581 */ 582 sweepchannels(ss, vap, table); 583} 584 585static const uint16_t rcl1[] = /* 8 FCC channel: 52, 56, 60, 64, 36, 40, 44, 48 */ 586{ 5260, 5280, 5300, 5320, 5180, 5200, 5220, 5240 }; 587static const uint16_t rcl2[] = /* 4 MKK channels: 34, 38, 42, 46 */ 588{ 5170, 5190, 5210, 5230 }; 589static const uint16_t rcl3[] = /* 2.4Ghz ch: 1,6,11,7,13 */ 590{ 2412, 2437, 2462, 2442, 2472 }; 591static const uint16_t rcl4[] = /* 5 FCC channel: 149, 153, 161, 165 */ 592{ 5745, 5765, 5785, 5805, 5825 }; 593static const uint16_t rcl7[] = /* 11 ETSI channel: 100,104,108,112,116,120,124,128,132,136,140 */ 594{ 5500, 5520, 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680, 5700 }; 595static const uint16_t rcl8[] = /* 2.4Ghz ch: 2,3,4,5,8,9,10,12 */ 596{ 2417, 2422, 2427, 2432, 2447, 2452, 2457, 2467 }; 597static const uint16_t rcl9[] = /* 2.4Ghz ch: 14 */ 598{ 2484 }; 599static const uint16_t rcl10[] = /* Added Korean channels 2312-2372 */ 600{ 2312, 2317, 2322, 2327, 2332, 2337, 2342, 2347, 2352, 2357, 2362, 2367, 2372 }; 601static const uint16_t rcl11[] = /* Added Japan channels in 4.9/5.0 spectrum */ 602{ 5040, 5060, 5080, 4920, 4940, 4960, 4980 }; 603#ifdef ATH_TURBO_SCAN 604static const uint16_t rcl5[] = /* 3 static turbo channels */ 605{ 5210, 5250, 5290 }; 606static const uint16_t rcl6[] = /* 2 static turbo channels */ 607{ 5760, 5800 }; 608static const uint16_t rcl6x[] = /* 4 FCC3 turbo channels */ 609{ 5540, 5580, 5620, 5660 }; 610static const uint16_t rcl12[] = /* 2.4Ghz Turbo channel 6 */ 611{ 2437 }; 612static const uint16_t rcl13[] = /* dynamic Turbo channels */ 613{ 5200, 5240, 5280, 5765, 5805 }; 614#endif /* ATH_TURBO_SCAN */ 615 616#define X(a) .count = sizeof(a)/sizeof(a[0]), .list = a 617 618static const struct scanlist staScanTable[] = { 619 { IEEE80211_MODE_11B, X(rcl3) }, 620 { IEEE80211_MODE_11A, X(rcl1) }, 621 { IEEE80211_MODE_11A, X(rcl2) }, 622 { IEEE80211_MODE_11B, X(rcl8) }, 623 { IEEE80211_MODE_11B, X(rcl9) }, 624 { IEEE80211_MODE_11A, X(rcl4) }, 625#ifdef ATH_TURBO_SCAN 626 { IEEE80211_MODE_STURBO_A, X(rcl5) }, 627 { IEEE80211_MODE_STURBO_A, X(rcl6) }, 628 { IEEE80211_MODE_TURBO_A, X(rcl6x) }, 629 { IEEE80211_MODE_TURBO_A, X(rcl13) }, 630#endif /* ATH_TURBO_SCAN */ 631 { IEEE80211_MODE_11A, X(rcl7) }, 632 { IEEE80211_MODE_11B, X(rcl10) }, 633 { IEEE80211_MODE_11A, X(rcl11) }, 634#ifdef ATH_TURBO_SCAN 635 { IEEE80211_MODE_TURBO_G, X(rcl12) }, 636#endif /* ATH_TURBO_SCAN */ 637 { .list = NULL } 638}; 639 640/* 641 * Start a station-mode scan by populating the channel list. 642 */ 643static int 644sta_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap) 645{ 646 struct sta_table *st = ss->ss_priv; 647 648 makescanlist(ss, vap, staScanTable); 649 650 if (ss->ss_mindwell == 0) 651 ss->ss_mindwell = msecs_to_ticks(20); /* 20ms */ 652 if (ss->ss_maxdwell == 0) 653 ss->ss_maxdwell = msecs_to_ticks(200); /* 200ms */ 654 655 st->st_scangen++; 656 st->st_newscan = 1; 657 658 return 0; 659} 660 661/* 662 * Restart a scan, typically a bg scan but can 663 * also be a fg scan that came up empty. 664 */ 665static int 666sta_restart(struct ieee80211_scan_state *ss, struct ieee80211vap *vap) 667{ 668 struct sta_table *st = ss->ss_priv; 669 670 st->st_newscan = 1; 671 return 0; 672} 673 674/* 675 * Cancel an ongoing scan. 676 */ 677static int 678sta_cancel(struct ieee80211_scan_state *ss, struct ieee80211vap *vap) 679{ 680 return 0; 681} 682 683/* unalligned little endian access */ 684#define LE_READ_2(p) \ 685 ((uint16_t) \ 686 ((((const uint8_t *)(p))[0] ) | \ 687 (((const uint8_t *)(p))[1] << 8))) 688 689/* 690 * Demote any supplied 11g channel to 11b. There should 691 * always be an 11b channel but we check anyway... 692 */ 693static struct ieee80211_channel * 694demote11b(struct ieee80211vap *vap, struct ieee80211_channel *chan) 695{ 696 struct ieee80211_channel *c; 697 698 if (IEEE80211_IS_CHAN_ANYG(chan) && 699 vap->iv_des_mode == IEEE80211_MODE_AUTO) { 700 c = ieee80211_find_channel(vap->iv_ic, chan->ic_freq, 701 (chan->ic_flags &~ (IEEE80211_CHAN_PUREG | IEEE80211_CHAN_G)) | 702 IEEE80211_CHAN_B); 703 if (c != NULL) 704 chan = c; 705 } 706 return chan; 707} 708 709static int 710maxrate(const struct ieee80211_scan_entry *se) 711{ 712 const struct ieee80211_ie_htcap *htcap = 713 (const struct ieee80211_ie_htcap *) se->se_ies.htcap_ie; 714 int rmax, r, i; 715 uint16_t caps; 716 717 rmax = 0; 718 if (htcap != NULL) { 719 /* 720 * HT station; inspect supported MCS and then adjust 721 * rate by channel width. Could also include short GI 722 * in this if we want to be extra accurate. 723 */ 724 /* XXX assumes MCS15 is max */ 725 for (i = 15; i >= 0 && isclr(htcap->hc_mcsset, i); i--) 726 ; 727 if (i >= 0) { 728 caps = LE_READ_2(&htcap->hc_cap); 729 /* XXX short/long GI */ 730 if (caps & IEEE80211_HTCAP_CHWIDTH40) 731 rmax = ieee80211_htrates[i].ht40_rate_400ns; 732 else 733 rmax = ieee80211_htrates[i].ht40_rate_800ns; 734 } 735 } 736 for (i = 0; i < se->se_rates[1]; i++) { 737 r = se->se_rates[2+i] & IEEE80211_RATE_VAL; 738 if (r > rmax) 739 rmax = r; 740 } 741 for (i = 0; i < se->se_xrates[1]; i++) { 742 r = se->se_xrates[2+i] & IEEE80211_RATE_VAL; 743 if (r > rmax) 744 rmax = r; 745 } 746 return rmax; 747} 748 749/* 750 * Compare the capabilities of two entries and decide which is 751 * more desirable (return >0 if a is considered better). Note 752 * that we assume compatibility/usability has already been checked 753 * so we don't need to (e.g. validate whether privacy is supported). 754 * Used to select the best scan candidate for association in a BSS. 755 */ 756static int 757sta_compare(const struct sta_entry *a, const struct sta_entry *b) 758{ 759#define PREFER(_a,_b,_what) do { \ 760 if (((_a) ^ (_b)) & (_what)) \ 761 return ((_a) & (_what)) ? 1 : -1; \ 762} while (0) 763 int maxa, maxb; 764 int8_t rssia, rssib; 765 int weight; 766 767 /* privacy support */ 768 PREFER(a->base.se_capinfo, b->base.se_capinfo, 769 IEEE80211_CAPINFO_PRIVACY); 770 771 /* compare count of previous failures */ 772 weight = b->se_fails - a->se_fails; 773 if (abs(weight) > 1) 774 return weight; 775 776 /* 777 * Compare rssi. If the two are considered equivalent 778 * then fallback to other criteria. We threshold the 779 * comparisons to avoid selecting an ap purely by rssi 780 * when both values may be good but one ap is otherwise 781 * more desirable (e.g. an 11b-only ap with stronger 782 * signal than an 11g ap). 783 */ 784 rssia = MIN(a->base.se_rssi, STA_RSSI_MAX); 785 rssib = MIN(b->base.se_rssi, STA_RSSI_MAX); 786 if (abs(rssib - rssia) < 5) { 787 /* best/max rate preferred if signal level close enough XXX */ 788 maxa = maxrate(&a->base); 789 maxb = maxrate(&b->base); 790 if (maxa != maxb) 791 return maxa - maxb; 792 /* XXX use freq for channel preference */ 793 /* for now just prefer 5Ghz band to all other bands */ 794 PREFER(IEEE80211_IS_CHAN_5GHZ(a->base.se_chan), 795 IEEE80211_IS_CHAN_5GHZ(b->base.se_chan), 1); 796 } 797 /* all things being equal, use signal level */ 798 return a->base.se_rssi - b->base.se_rssi; 799#undef PREFER 800} 801 802/* 803 * Check rate set suitability and return the best supported rate. 804 * XXX inspect MCS for HT 805 */ 806static int 807check_rate(struct ieee80211vap *vap, const struct ieee80211_channel *chan, 808 const struct ieee80211_scan_entry *se) 809{ 810#define RV(v) ((v) & IEEE80211_RATE_VAL) 811 const struct ieee80211_rateset *srs; 812 int i, j, nrs, r, okrate, badrate, fixedrate, ucastrate; 813 const uint8_t *rs; 814 815 okrate = badrate = 0; 816 817 srs = ieee80211_get_suprates(vap->iv_ic, chan); 818 nrs = se->se_rates[1]; 819 rs = se->se_rates+2; 820 /* XXX MCS */ 821 ucastrate = vap->iv_txparms[ieee80211_chan2mode(chan)].ucastrate; 822 fixedrate = IEEE80211_FIXED_RATE_NONE; 823again: 824 for (i = 0; i < nrs; i++) { 825 r = RV(rs[i]); 826 badrate = r; 827 /* 828 * Check any fixed rate is included. 829 */ 830 if (r == ucastrate) 831 fixedrate = r; 832 /* 833 * Check against our supported rates. 834 */ 835 for (j = 0; j < srs->rs_nrates; j++) 836 if (r == RV(srs->rs_rates[j])) { 837 if (r > okrate) /* NB: track max */ 838 okrate = r; 839 break; 840 } 841 842 if (j == srs->rs_nrates && (rs[i] & IEEE80211_RATE_BASIC)) { 843 /* 844 * Don't try joining a BSS, if we don't support 845 * one of its basic rates. 846 */ 847 okrate = 0; 848 goto back; 849 } 850 } 851 if (rs == se->se_rates+2) { 852 /* scan xrates too; sort of an algol68-style for loop */ 853 nrs = se->se_xrates[1]; 854 rs = se->se_xrates+2; 855 goto again; 856 } 857 858back: 859 if (okrate == 0 || ucastrate != fixedrate) 860 return badrate | IEEE80211_RATE_BASIC; 861 else 862 return RV(okrate); 863#undef RV 864} 865 866static int 867match_ssid(const uint8_t *ie, 868 int nssid, const struct ieee80211_scan_ssid ssids[]) 869{ 870 int i; 871 872 for (i = 0; i < nssid; i++) { 873 if (ie[1] == ssids[i].len && 874 memcmp(ie+2, ssids[i].ssid, ie[1]) == 0) 875 return 1; 876 } 877 return 0; 878} 879 880#ifdef IEEE80211_SUPPORT_TDMA 881static int 882tdma_isfull(const struct ieee80211_tdma_param *tdma) 883{ 884 int slot, slotcnt; 885 886 slotcnt = tdma->tdma_slotcnt; 887 for (slot = slotcnt-1; slot >= 0; slot--) 888 if (isclr(tdma->tdma_inuse, slot)) 889 return 0; 890 return 1; 891} 892#endif /* IEEE80211_SUPPORT_TDMA */ 893 894/* 895 * Test a scan candidate for suitability/compatibility. 896 */ 897static int 898match_bss(struct ieee80211vap *vap, 899 const struct ieee80211_scan_state *ss, struct sta_entry *se0, 900 int debug) 901{ 902 struct ieee80211com *ic = vap->iv_ic; 903 struct ieee80211_scan_entry *se = &se0->base; 904 uint8_t rate; 905 int fail; 906 907 fail = 0; 908 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, se->se_chan))) 909 fail |= MATCH_CHANNEL; 910 /* 911 * NB: normally the desired mode is used to construct 912 * the channel list, but it's possible for the scan 913 * cache to include entries for stations outside this 914 * list so we check the desired mode here to weed them 915 * out. 916 */ 917 if (vap->iv_des_mode != IEEE80211_MODE_AUTO && 918 (se->se_chan->ic_flags & IEEE80211_CHAN_ALLTURBO) != 919 chanflags[vap->iv_des_mode]) 920 fail |= MATCH_CHANNEL; 921 if (vap->iv_opmode == IEEE80211_M_IBSS) { 922 if ((se->se_capinfo & IEEE80211_CAPINFO_IBSS) == 0) 923 fail |= MATCH_CAPINFO; 924#ifdef IEEE80211_SUPPORT_TDMA 925 } else if (vap->iv_opmode == IEEE80211_M_AHDEMO) { 926 /* 927 * Adhoc demo network setup shouldn't really be scanning 928 * but just in case skip stations operating in IBSS or 929 * BSS mode. 930 */ 931 if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) 932 fail |= MATCH_CAPINFO; 933 /* 934 * TDMA operation cannot coexist with a normal 802.11 network; 935 * skip if IBSS or ESS capabilities are marked and require 936 * the beacon have a TDMA ie present. 937 */ 938 if (vap->iv_caps & IEEE80211_C_TDMA) { 939 const struct ieee80211_tdma_param *tdma = 940 (const struct ieee80211_tdma_param *)se->se_ies.tdma_ie; 941 942 if (tdma == NULL) 943 fail |= MATCH_TDMA_NOIE; 944 else if (tdma->tdma_slot != 0) 945 fail |= MATCH_TDMA_NOTMASTER; 946 else if (tdma_isfull(tdma)) 947 fail |= MATCH_TDMA_NOSLOT; 948#if 0 949 else if (ieee80211_local_address(se->se_macaddr)) 950 fail |= MATCH_TDMA_LOCAL; 951#endif 952 } 953#endif /* IEEE80211_SUPPORT_TDMA */ 954 } else { 955 if ((se->se_capinfo & IEEE80211_CAPINFO_ESS) == 0) 956 fail |= MATCH_CAPINFO; 957 /* 958 * If 11d is enabled and we're attempting to join a bss 959 * that advertises it's country code then compare our 960 * current settings to what we fetched from the country ie. 961 * If our country code is unspecified or different then do 962 * not attempt to join the bss. We should have already 963 * dispatched an event to user space that identifies the 964 * new country code so our regdomain config should match. 965 */ 966 if ((IEEE80211_IS_CHAN_11D(se->se_chan) || 967 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD)) && 968 se->se_cc[0] != 0 && 969 (ic->ic_regdomain.country == CTRY_DEFAULT || 970 !isocmp(se->se_cc, ic->ic_regdomain.isocc))) 971 fail |= MATCH_CC; 972 } 973 if (vap->iv_flags & IEEE80211_F_PRIVACY) { 974 if ((se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0) 975 fail |= MATCH_PRIVACY; 976 } else { 977 /* XXX does this mean privacy is supported or required? */ 978 if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) 979 fail |= MATCH_PRIVACY; 980 } 981 se0->se_flags &= ~STA_DEMOTE11B; 982 rate = check_rate(vap, se->se_chan, se); 983 if (rate & IEEE80211_RATE_BASIC) { 984 fail |= MATCH_RATE; 985 /* 986 * An 11b-only ap will give a rate mismatch if there is an 987 * OFDM fixed tx rate for 11g. Try downgrading the channel 988 * in the scan list to 11b and retry the rate check. 989 */ 990 if (IEEE80211_IS_CHAN_ANYG(se->se_chan)) { 991 rate = check_rate(vap, demote11b(vap, se->se_chan), se); 992 if ((rate & IEEE80211_RATE_BASIC) == 0) { 993 fail &= ~MATCH_RATE; 994 se0->se_flags |= STA_DEMOTE11B; 995 } 996 } 997 } else if (rate < 2*24) { 998 /* 999 * This is an 11b-only ap. Check the desired mode in 1000 * case that needs to be honored (mode 11g filters out 1001 * 11b-only ap's). Otherwise force any 11g channel used 1002 * in scanning to be demoted. 1003 * 1004 * NB: we cheat a bit here by looking at the max rate; 1005 * we could/should check the rates. 1006 */ 1007 if (!(vap->iv_des_mode == IEEE80211_MODE_AUTO || 1008 vap->iv_des_mode == IEEE80211_MODE_11B)) 1009 fail |= MATCH_RATE; 1010 else 1011 se0->se_flags |= STA_DEMOTE11B; 1012 } 1013 if (ss->ss_nssid != 0 && 1014 !match_ssid(se->se_ssid, ss->ss_nssid, ss->ss_ssid)) 1015 fail |= MATCH_SSID; 1016 if ((vap->iv_flags & IEEE80211_F_DESBSSID) && 1017 !IEEE80211_ADDR_EQ(vap->iv_des_bssid, se->se_bssid)) 1018 fail |= MATCH_BSSID; 1019 if (se0->se_fails >= STA_FAILS_MAX) 1020 fail |= MATCH_FAILS; 1021 if (se0->se_notseen >= STA_PURGE_SCANS) 1022 fail |= MATCH_NOTSEEN; 1023 if (se->se_rssi < STA_RSSI_MIN) 1024 fail |= MATCH_RSSI; 1025#ifdef IEEE80211_DEBUG 1026 if (ieee80211_msg(vap, debug)) { 1027 printf(" %c %s", 1028 fail & MATCH_FAILS ? '=' : 1029 fail & MATCH_NOTSEEN ? '^' : 1030 fail & MATCH_CC ? '$' : 1031#ifdef IEEE80211_SUPPORT_TDMA 1032 fail & MATCH_TDMA_NOIE ? '&' : 1033 fail & MATCH_TDMA_NOTMASTER ? ':' : 1034 fail & MATCH_TDMA_NOSLOT ? '@' : 1035 fail & MATCH_TDMA_LOCAL ? '#' : 1036#endif 1037 fail ? '-' : '+', ether_sprintf(se->se_macaddr)); 1038 printf(" %s%c", ether_sprintf(se->se_bssid), 1039 fail & MATCH_BSSID ? '!' : ' '); 1040 printf(" %3d%c", ieee80211_chan2ieee(ic, se->se_chan), 1041 fail & MATCH_CHANNEL ? '!' : ' '); 1042 printf(" %+4d%c", se->se_rssi, fail & MATCH_RSSI ? '!' : ' '); 1043 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2, 1044 fail & MATCH_RATE ? '!' : ' '); 1045 printf(" %4s%c", 1046 (se->se_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" : 1047 (se->se_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" : 1048 "????", 1049 fail & MATCH_CAPINFO ? '!' : ' '); 1050 printf(" %3s%c ", 1051 (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) ? 1052 "wep" : "no", 1053 fail & MATCH_PRIVACY ? '!' : ' '); 1054 ieee80211_print_essid(se->se_ssid+2, se->se_ssid[1]); 1055 printf("%s\n", fail & MATCH_SSID ? "!" : ""); 1056 } 1057#endif 1058 return fail; 1059} 1060 1061static void 1062sta_update_notseen(struct sta_table *st) 1063{ 1064 struct sta_entry *se; 1065 1066 mtx_lock(&st->st_lock); 1067 TAILQ_FOREACH(se, &st->st_entry, se_list) { 1068 /* 1069 * If seen the reset and don't bump the count; 1070 * otherwise bump the ``not seen'' count. Note 1071 * that this insures that stations for which we 1072 * see frames while not scanning but not during 1073 * this scan will not be penalized. 1074 */ 1075 if (se->se_seen) 1076 se->se_seen = 0; 1077 else 1078 se->se_notseen++; 1079 } 1080 mtx_unlock(&st->st_lock); 1081} 1082 1083static void 1084sta_dec_fails(struct sta_table *st) 1085{ 1086 struct sta_entry *se; 1087 1088 mtx_lock(&st->st_lock); 1089 TAILQ_FOREACH(se, &st->st_entry, se_list) 1090 if (se->se_fails) 1091 se->se_fails--; 1092 mtx_unlock(&st->st_lock); 1093} 1094 1095static struct sta_entry * 1096select_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap, int debug) 1097{ 1098 struct sta_table *st = ss->ss_priv; 1099 struct sta_entry *se, *selbs = NULL; 1100 1101 IEEE80211_DPRINTF(vap, debug, " %s\n", 1102 "macaddr bssid chan rssi rate flag wep essid"); 1103 mtx_lock(&st->st_lock); 1104 TAILQ_FOREACH(se, &st->st_entry, se_list) { 1105 ieee80211_ies_expand(&se->base.se_ies); 1106 if (match_bss(vap, ss, se, debug) == 0) { 1107 if (selbs == NULL) 1108 selbs = se; 1109 else if (sta_compare(se, selbs) > 0) 1110 selbs = se; 1111 } 1112 } 1113 mtx_unlock(&st->st_lock); 1114 1115 return selbs; 1116} 1117 1118/* 1119 * Pick an ap or ibss network to join or find a channel 1120 * to use to start an ibss network. 1121 */ 1122static int 1123sta_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap) 1124{ 1125 struct sta_table *st = ss->ss_priv; 1126 struct sta_entry *selbs; 1127 struct ieee80211_channel *chan; 1128 1129 KASSERT(vap->iv_opmode == IEEE80211_M_STA, 1130 ("wrong mode %u", vap->iv_opmode)); 1131 1132 if (st->st_newscan) { 1133 sta_update_notseen(st); 1134 st->st_newscan = 0; 1135 } 1136 if (ss->ss_flags & IEEE80211_SCAN_NOPICK) { 1137 /* 1138 * Manual/background scan, don't select+join the 1139 * bss, just return. The scanning framework will 1140 * handle notification that this has completed. 1141 */ 1142 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK; 1143 return 1; 1144 } 1145 /* 1146 * Automatic sequencing; look for a candidate and 1147 * if found join the network. 1148 */ 1149 /* NB: unlocked read should be ok */ 1150 if (TAILQ_FIRST(&st->st_entry) == NULL) { 1151 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 1152 "%s: no scan candidate\n", __func__); 1153 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN) 1154 return 0; 1155notfound: 1156 /* 1157 * If nothing suitable was found decrement 1158 * the failure counts so entries will be 1159 * reconsidered the next time around. We 1160 * really want to do this only for sta's 1161 * where we've previously had some success. 1162 */ 1163 sta_dec_fails(st); 1164 st->st_newscan = 1; 1165 return 0; /* restart scan */ 1166 } 1167 selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN); 1168 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN) 1169 return (selbs != NULL); 1170 if (selbs == NULL) 1171 goto notfound; 1172 chan = selbs->base.se_chan; 1173 if (selbs->se_flags & STA_DEMOTE11B) 1174 chan = demote11b(vap, chan); 1175 if (!ieee80211_sta_join(vap, chan, &selbs->base)) 1176 goto notfound; 1177 return 1; /* terminate scan */ 1178} 1179 1180/* 1181 * Lookup an entry in the scan cache. We assume we're 1182 * called from the bottom half or such that we don't need 1183 * to block the bottom half so that it's safe to return 1184 * a reference to an entry w/o holding the lock on the table. 1185 */ 1186static struct sta_entry * 1187sta_lookup(struct sta_table *st, const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1188{ 1189 struct sta_entry *se; 1190 int hash = STA_HASH(macaddr); 1191 1192 mtx_lock(&st->st_lock); 1193 LIST_FOREACH(se, &st->st_hash[hash], se_hash) 1194 if (IEEE80211_ADDR_EQ(se->base.se_macaddr, macaddr)) 1195 break; 1196 mtx_unlock(&st->st_lock); 1197 1198 return se; /* NB: unlocked */ 1199} 1200 1201static void 1202sta_roam_check(struct ieee80211_scan_state *ss, struct ieee80211vap *vap) 1203{ 1204 struct ieee80211com *ic = vap->iv_ic; 1205 struct ieee80211_node *ni = vap->iv_bss; 1206 struct sta_table *st = ss->ss_priv; 1207 enum ieee80211_phymode mode; 1208 struct sta_entry *se, *selbs; 1209 uint8_t roamRate, curRate, ucastRate; 1210 int8_t roamRssi, curRssi; 1211 1212 se = sta_lookup(st, ni->ni_macaddr); 1213 if (se == NULL) { 1214 /* XXX something is wrong */ 1215 return; 1216 } 1217 1218 mode = ieee80211_chan2mode(ic->ic_bsschan); 1219 roamRate = vap->iv_roamparms[mode].rate; 1220 roamRssi = vap->iv_roamparms[mode].rssi; 1221 ucastRate = vap->iv_txparms[mode].ucastrate; 1222 /* NB: the most up to date rssi is in the node, not the scan cache */ 1223 curRssi = ic->ic_node_getrssi(ni); 1224 if (ucastRate == IEEE80211_FIXED_RATE_NONE) { 1225 curRate = ni->ni_txrate; 1226 roamRate &= IEEE80211_RATE_VAL; 1227 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ROAM, 1228 "%s: currssi %d currate %u roamrssi %d roamrate %u\n", 1229 __func__, curRssi, curRate, roamRssi, roamRate); 1230 } else { 1231 curRate = roamRate; /* NB: insure compare below fails */ 1232 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ROAM, 1233 "%s: currssi %d roamrssi %d\n", __func__, curRssi, roamRssi); 1234 } 1235 /* 1236 * Check if a new ap should be used and switch. 1237 * XXX deauth current ap 1238 */ 1239 if (curRate < roamRate || curRssi < roamRssi) { 1240 if (time_after(ticks, ic->ic_lastscan + vap->iv_scanvalid)) { 1241 /* 1242 * Scan cache contents are too old; force a scan now 1243 * if possible so we have current state to make a 1244 * decision with. We don't kick off a bg scan if 1245 * we're using dynamic turbo and boosted or if the 1246 * channel is busy. 1247 * XXX force immediate switch on scan complete 1248 */ 1249 if (!IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) && 1250 time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle)) 1251 ieee80211_bg_scan(vap, 0); 1252 return; 1253 } 1254 se->base.se_rssi = curRssi; 1255 selbs = select_bss(ss, vap, IEEE80211_MSG_ROAM); 1256 if (selbs != NULL && selbs != se) { 1257 struct ieee80211_channel *chan; 1258 1259 IEEE80211_DPRINTF(vap, 1260 IEEE80211_MSG_ROAM | IEEE80211_MSG_DEBUG, 1261 "%s: ROAM: curRate %u, roamRate %u, " 1262 "curRssi %d, roamRssi %d\n", __func__, 1263 curRate, roamRate, curRssi, roamRssi); 1264 1265 chan = selbs->base.se_chan; 1266 if (selbs->se_flags & STA_DEMOTE11B) 1267 chan = demote11b(vap, chan); 1268 (void) ieee80211_sta_join(vap, chan, &selbs->base); 1269 } 1270 } 1271} 1272 1273/* 1274 * Age entries in the scan cache. 1275 * XXX also do roaming since it's convenient 1276 */ 1277static void 1278sta_age(struct ieee80211_scan_state *ss) 1279{ 1280 struct ieee80211vap *vap = ss->ss_vap; 1281 1282 adhoc_age(ss); 1283 /* 1284 * If rate control is enabled check periodically to see if 1285 * we should roam from our current connection to one that 1286 * might be better. This only applies when we're operating 1287 * in sta mode and automatic roaming is set. 1288 * XXX defer if busy 1289 * XXX repeater station 1290 * XXX do when !bgscan? 1291 */ 1292 KASSERT(vap->iv_opmode == IEEE80211_M_STA, 1293 ("wrong mode %u", vap->iv_opmode)); 1294 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO && 1295 (vap->iv_ic->ic_flags & IEEE80211_F_BGSCAN) && 1296 vap->iv_state >= IEEE80211_S_RUN) 1297 /* XXX vap is implicit */ 1298 sta_roam_check(ss, vap); 1299} 1300 1301/* 1302 * Iterate over the entries in the scan cache, invoking 1303 * the callback function on each one. 1304 */ 1305static void 1306sta_iterate(struct ieee80211_scan_state *ss, 1307 ieee80211_scan_iter_func *f, void *arg) 1308{ 1309 struct sta_table *st = ss->ss_priv; 1310 struct sta_entry *se; 1311 u_int gen; 1312 1313 mtx_lock(&st->st_scanlock); 1314 gen = st->st_scaniter++; 1315restart: 1316 mtx_lock(&st->st_lock); 1317 TAILQ_FOREACH(se, &st->st_entry, se_list) { 1318 if (se->se_scangen != gen) { 1319 se->se_scangen = gen; 1320 /* update public state */ 1321 se->base.se_age = ticks - se->se_lastupdate; 1322 mtx_unlock(&st->st_lock); 1323 (*f)(arg, &se->base); 1324 goto restart; 1325 } 1326 } 1327 mtx_unlock(&st->st_lock); 1328 1329 mtx_unlock(&st->st_scanlock); 1330} 1331 1332static void 1333sta_assoc_fail(struct ieee80211_scan_state *ss, 1334 const uint8_t macaddr[IEEE80211_ADDR_LEN], int reason) 1335{ 1336 struct sta_table *st = ss->ss_priv; 1337 struct sta_entry *se; 1338 1339 se = sta_lookup(st, macaddr); 1340 if (se != NULL) { 1341 se->se_fails++; 1342 se->se_lastfail = ticks; 1343 IEEE80211_NOTE_MAC(ss->ss_vap, IEEE80211_MSG_SCAN, 1344 macaddr, "%s: reason %u fails %u", 1345 __func__, reason, se->se_fails); 1346 } 1347} 1348 1349static void 1350sta_assoc_success(struct ieee80211_scan_state *ss, 1351 const uint8_t macaddr[IEEE80211_ADDR_LEN]) 1352{ 1353 struct sta_table *st = ss->ss_priv; 1354 struct sta_entry *se; 1355 1356 se = sta_lookup(st, macaddr); 1357 if (se != NULL) { 1358#if 0 1359 se->se_fails = 0; 1360 IEEE80211_NOTE_MAC(ss->ss_vap, IEEE80211_MSG_SCAN, 1361 macaddr, "%s: fails %u", 1362 __func__, se->se_fails); 1363#endif 1364 se->se_lastassoc = ticks; 1365 } 1366} 1367 1368static const struct ieee80211_scanner sta_default = { 1369 .scan_name = "default", 1370 .scan_attach = sta_attach, 1371 .scan_detach = sta_detach, 1372 .scan_start = sta_start, 1373 .scan_restart = sta_restart, 1374 .scan_cancel = sta_cancel, 1375 .scan_end = sta_pick_bss, 1376 .scan_flush = sta_flush, 1377 .scan_add = sta_add, 1378 .scan_age = sta_age, 1379 .scan_iterate = sta_iterate, 1380 .scan_assoc_fail = sta_assoc_fail, 1381 .scan_assoc_success = sta_assoc_success, 1382}; 1383 1384/* 1385 * Adhoc mode-specific support. 1386 */ 1387 1388static const uint16_t adhocWorld[] = /* 36, 40, 44, 48 */ 1389{ 5180, 5200, 5220, 5240 }; 1390static const uint16_t adhocFcc3[] = /* 36, 40, 44, 48 145, 149, 153, 157, 161, 165 */ 1391{ 5180, 5200, 5220, 5240, 5725, 5745, 5765, 5785, 5805, 5825 }; 1392static const uint16_t adhocMkk[] = /* 34, 38, 42, 46 */ 1393{ 5170, 5190, 5210, 5230 }; 1394static const uint16_t adhoc11b[] = /* 10, 11 */ 1395{ 2457, 2462 }; 1396 1397static const struct scanlist adhocScanTable[] = { 1398 { IEEE80211_MODE_11B, X(adhoc11b) }, 1399 { IEEE80211_MODE_11A, X(adhocWorld) }, 1400 { IEEE80211_MODE_11A, X(adhocFcc3) }, 1401 { IEEE80211_MODE_11B, X(adhocMkk) }, 1402 { .list = NULL } 1403}; 1404#undef X 1405 1406/* 1407 * Start an adhoc-mode scan by populating the channel list. 1408 */ 1409static int 1410adhoc_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap) 1411{ 1412 struct sta_table *st = ss->ss_priv; 1413 1414 makescanlist(ss, vap, adhocScanTable); 1415 1416 if (ss->ss_mindwell == 0) 1417 ss->ss_mindwell = msecs_to_ticks(200); /* 200ms */ 1418 if (ss->ss_maxdwell == 0) 1419 ss->ss_maxdwell = msecs_to_ticks(200); /* 200ms */ 1420 1421 st->st_scangen++; 1422 st->st_newscan = 1; 1423 1424 return 0; 1425} 1426 1427/* 1428 * Select a channel to start an adhoc network on. 1429 * The channel list was populated with appropriate 1430 * channels so select one that looks least occupied. 1431 */ 1432static struct ieee80211_channel * 1433adhoc_pick_channel(struct ieee80211_scan_state *ss, int flags) 1434{ 1435 struct sta_table *st = ss->ss_priv; 1436 struct sta_entry *se; 1437 struct ieee80211_channel *c, *bestchan; 1438 int i, bestrssi, maxrssi; 1439 1440 bestchan = NULL; 1441 bestrssi = -1; 1442 1443 mtx_lock(&st->st_lock); 1444 for (i = 0; i < ss->ss_last; i++) { 1445 c = ss->ss_chans[i]; 1446 /* never consider a channel with radar */ 1447 if (IEEE80211_IS_CHAN_RADAR(c)) 1448 continue; 1449 /* skip channels disallowed by regulatory settings */ 1450 if (IEEE80211_IS_CHAN_NOADHOC(c)) 1451 continue; 1452 /* check channel attributes for band compatibility */ 1453 if (flags != 0 && (c->ic_flags & flags) != flags) 1454 continue; 1455 maxrssi = 0; 1456 TAILQ_FOREACH(se, &st->st_entry, se_list) { 1457 if (se->base.se_chan != c) 1458 continue; 1459 if (se->base.se_rssi > maxrssi) 1460 maxrssi = se->base.se_rssi; 1461 } 1462 if (bestchan == NULL || maxrssi < bestrssi) 1463 bestchan = c; 1464 } 1465 mtx_unlock(&st->st_lock); 1466 1467 return bestchan; 1468} 1469 1470/* 1471 * Pick an ibss network to join or find a channel 1472 * to use to start an ibss network. 1473 */ 1474static int 1475adhoc_pick_bss(struct ieee80211_scan_state *ss, struct ieee80211vap *vap) 1476{ 1477 struct sta_table *st = ss->ss_priv; 1478 struct sta_entry *selbs; 1479 struct ieee80211_channel *chan; 1480 1481 KASSERT(vap->iv_opmode == IEEE80211_M_IBSS || 1482 vap->iv_opmode == IEEE80211_M_AHDEMO, 1483 ("wrong opmode %u", vap->iv_opmode)); 1484 1485 if (st->st_newscan) { 1486 sta_update_notseen(st); 1487 st->st_newscan = 0; 1488 } 1489 if (ss->ss_flags & IEEE80211_SCAN_NOPICK) { 1490 /* 1491 * Manual/background scan, don't select+join the 1492 * bss, just return. The scanning framework will 1493 * handle notification that this has completed. 1494 */ 1495 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK; 1496 return 1; 1497 } 1498 /* 1499 * Automatic sequencing; look for a candidate and 1500 * if found join the network. 1501 */ 1502 /* NB: unlocked read should be ok */ 1503 if (TAILQ_FIRST(&st->st_entry) == NULL) { 1504 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 1505 "%s: no scan candidate\n", __func__); 1506 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN) 1507 return 0; 1508notfound: 1509 /* NB: never auto-start a tdma network for slot !0 */ 1510#ifdef IEEE80211_SUPPORT_TDMA 1511 if (vap->iv_des_nssid && 1512 ((vap->iv_caps & IEEE80211_C_TDMA) == 0 || 1513 ieee80211_tdma_getslot(vap) == 0)) { 1514#else 1515 if (vap->iv_des_nssid) { 1516#endif 1517 /* 1518 * No existing adhoc network to join and we have 1519 * an ssid; start one up. If no channel was 1520 * specified, try to select a channel. 1521 */ 1522 if (vap->iv_des_chan == IEEE80211_CHAN_ANYC || 1523 IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) { 1524 struct ieee80211com *ic = vap->iv_ic; 1525 1526 chan = adhoc_pick_channel(ss, 0); 1527 if (chan != NULL) 1528 chan = ieee80211_ht_adjust_channel(ic, 1529 chan, vap->iv_flags_ext); 1530 } else 1531 chan = vap->iv_des_chan; 1532 if (chan != NULL) { 1533 ieee80211_create_ibss(vap, chan); 1534 return 1; 1535 } 1536 } 1537 /* 1538 * If nothing suitable was found decrement 1539 * the failure counts so entries will be 1540 * reconsidered the next time around. We 1541 * really want to do this only for sta's 1542 * where we've previously had some success. 1543 */ 1544 sta_dec_fails(st); 1545 st->st_newscan = 1; 1546 return 0; /* restart scan */ 1547 } 1548 selbs = select_bss(ss, vap, IEEE80211_MSG_SCAN); 1549 if (ss->ss_flags & IEEE80211_SCAN_NOJOIN) 1550 return (selbs != NULL); 1551 if (selbs == NULL) 1552 goto notfound; 1553 chan = selbs->base.se_chan; 1554 if (selbs->se_flags & STA_DEMOTE11B) 1555 chan = demote11b(vap, chan); 1556 if (!ieee80211_sta_join(vap, chan, &selbs->base)) 1557 goto notfound; 1558 return 1; /* terminate scan */ 1559} 1560 1561/* 1562 * Age entries in the scan cache. 1563 */ 1564static void 1565adhoc_age(struct ieee80211_scan_state *ss) 1566{ 1567 struct sta_table *st = ss->ss_priv; 1568 struct sta_entry *se, *next; 1569 1570 mtx_lock(&st->st_lock); 1571 TAILQ_FOREACH_SAFE(se, &st->st_entry, se_list, next) { 1572 if (se->se_notseen > STA_PURGE_SCANS) { 1573 TAILQ_REMOVE(&st->st_entry, se, se_list); 1574 LIST_REMOVE(se, se_hash); 1575 ieee80211_ies_cleanup(&se->base.se_ies); 1576 free(se, M_80211_SCAN); 1577 } 1578 } 1579 mtx_unlock(&st->st_lock); 1580} 1581 1582static const struct ieee80211_scanner adhoc_default = { 1583 .scan_name = "default", 1584 .scan_attach = sta_attach, 1585 .scan_detach = sta_detach, 1586 .scan_start = adhoc_start, 1587 .scan_restart = sta_restart, 1588 .scan_cancel = sta_cancel, 1589 .scan_end = adhoc_pick_bss, 1590 .scan_flush = sta_flush, 1591 .scan_pickchan = adhoc_pick_channel, 1592 .scan_add = sta_add, 1593 .scan_age = adhoc_age, 1594 .scan_iterate = sta_iterate, 1595 .scan_assoc_fail = sta_assoc_fail, 1596 .scan_assoc_success = sta_assoc_success, 1597}; 1598 1599static void 1600ap_force_promisc(struct ieee80211com *ic) 1601{ 1602 struct ifnet *ifp = ic->ic_ifp; 1603 1604 IEEE80211_LOCK(ic); 1605 /* set interface into promiscuous mode */ 1606 ifp->if_flags |= IFF_PROMISC; 1607 ic->ic_update_promisc(ifp); 1608 IEEE80211_UNLOCK(ic); 1609} 1610 1611static void 1612ap_reset_promisc(struct ieee80211com *ic) 1613{ 1614 IEEE80211_LOCK(ic); 1615 ieee80211_syncifflag_locked(ic, IFF_PROMISC); 1616 IEEE80211_UNLOCK(ic); 1617} 1618 1619static int 1620ap_start(struct ieee80211_scan_state *ss, struct ieee80211vap *vap) 1621{ 1622 struct sta_table *st = ss->ss_priv; 1623 1624 makescanlist(ss, vap, staScanTable); 1625 1626 if (ss->ss_mindwell == 0) 1627 ss->ss_mindwell = msecs_to_ticks(200); /* 200ms */ 1628 if (ss->ss_maxdwell == 0) 1629 ss->ss_maxdwell = msecs_to_ticks(200); /* 200ms */ 1630 1631 st->st_scangen++; 1632 st->st_newscan = 1; 1633 1634 ap_force_promisc(vap->iv_ic); 1635 return 0; 1636} 1637 1638/* 1639 * Cancel an ongoing scan. 1640 */ 1641static int 1642ap_cancel(struct ieee80211_scan_state *ss, struct ieee80211vap *vap) 1643{ 1644 ap_reset_promisc(vap->iv_ic); 1645 return 0; 1646} 1647 1648/* 1649 * Pick a quiet channel to use for ap operation. 1650 */ 1651static struct ieee80211_channel * 1652ap_pick_channel(struct ieee80211_scan_state *ss, int flags) 1653{ 1654 struct sta_table *st = ss->ss_priv; 1655 struct ieee80211_channel *bestchan = NULL; 1656 int i; 1657 1658 /* XXX select channel more intelligently, e.g. channel spread, power */ 1659 /* NB: use scan list order to preserve channel preference */ 1660 for (i = 0; i < ss->ss_last; i++) { 1661 struct ieee80211_channel *chan = ss->ss_chans[i]; 1662 /* 1663 * If the channel is unoccupied the max rssi 1664 * should be zero; just take it. Otherwise 1665 * track the channel with the lowest rssi and 1666 * use that when all channels appear occupied. 1667 */ 1668 if (IEEE80211_IS_CHAN_RADAR(chan)) 1669 continue; 1670 if (IEEE80211_IS_CHAN_NOHOSTAP(chan)) 1671 continue; 1672 /* check channel attributes for band compatibility */ 1673 if (flags != 0 && (chan->ic_flags & flags) != flags) 1674 continue; 1675 KASSERT(sizeof(chan->ic_ieee) == 1, ("ic_chan size")); 1676 /* XXX channel have interference */ 1677 if (st->st_maxrssi[chan->ic_ieee] == 0) { 1678 /* XXX use other considerations */ 1679 return chan; 1680 } 1681 if (bestchan == NULL || 1682 st->st_maxrssi[chan->ic_ieee] < st->st_maxrssi[bestchan->ic_ieee]) 1683 bestchan = chan; 1684 } 1685 return bestchan; 1686} 1687 1688/* 1689 * Pick a quiet channel to use for ap operation. 1690 */ 1691static int 1692ap_end(struct ieee80211_scan_state *ss, struct ieee80211vap *vap) 1693{ 1694 struct ieee80211com *ic = vap->iv_ic; 1695 struct ieee80211_channel *bestchan; 1696 1697 KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP, 1698 ("wrong opmode %u", vap->iv_opmode)); 1699 bestchan = ap_pick_channel(ss, 0); 1700 if (bestchan == NULL) { 1701 /* no suitable channel, should not happen */ 1702 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 1703 "%s: no suitable channel! (should not happen)\n", __func__); 1704 /* XXX print something? */ 1705 return 0; /* restart scan */ 1706 } 1707 /* 1708 * If this is a dynamic turbo channel, start with the unboosted one. 1709 */ 1710 if (IEEE80211_IS_CHAN_TURBO(bestchan)) { 1711 bestchan = ieee80211_find_channel(ic, bestchan->ic_freq, 1712 bestchan->ic_flags & ~IEEE80211_CHAN_TURBO); 1713 if (bestchan == NULL) { 1714 /* should never happen ?? */ 1715 return 0; 1716 } 1717 } 1718 ap_reset_promisc(ic); 1719 if (ss->ss_flags & (IEEE80211_SCAN_NOPICK | IEEE80211_SCAN_NOJOIN)) { 1720 /* 1721 * Manual/background scan, don't select+join the 1722 * bss, just return. The scanning framework will 1723 * handle notification that this has completed. 1724 */ 1725 ss->ss_flags &= ~IEEE80211_SCAN_NOPICK; 1726 return 1; 1727 } 1728 ieee80211_create_ibss(vap, 1729 ieee80211_ht_adjust_channel(ic, bestchan, vap->iv_flags_ext)); 1730 return 1; 1731} 1732 1733static const struct ieee80211_scanner ap_default = { 1734 .scan_name = "default", 1735 .scan_attach = sta_attach, 1736 .scan_detach = sta_detach, 1737 .scan_start = ap_start, 1738 .scan_restart = sta_restart, 1739 .scan_cancel = ap_cancel, 1740 .scan_end = ap_end, 1741 .scan_flush = sta_flush, 1742 .scan_pickchan = ap_pick_channel, 1743 .scan_add = sta_add, 1744 .scan_age = adhoc_age, 1745 .scan_iterate = sta_iterate, 1746 .scan_assoc_success = sta_assoc_success, 1747 .scan_assoc_fail = sta_assoc_fail, 1748}; 1749 1750/* 1751 * Module glue. 1752 */ 1753IEEE80211_SCANNER_MODULE(sta, 1); 1754IEEE80211_SCANNER_ALG(sta, IEEE80211_M_STA, sta_default); 1755IEEE80211_SCANNER_ALG(ibss, IEEE80211_M_IBSS, adhoc_default); 1756IEEE80211_SCANNER_ALG(ahdemo, IEEE80211_M_AHDEMO, adhoc_default); 1757IEEE80211_SCANNER_ALG(ap, IEEE80211_M_HOSTAP, ap_default);
|