11a1e1d21SSam Leffler /*- 27535e66aSSam Leffler * Copyright (c) 2001 Atsushi Onoe 310ad9a77SSam Leffler * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting 41a1e1d21SSam Leffler * All rights reserved. 51a1e1d21SSam Leffler * 61a1e1d21SSam Leffler * Redistribution and use in source and binary forms, with or without 71a1e1d21SSam Leffler * modification, are permitted provided that the following conditions 81a1e1d21SSam Leffler * are met: 91a1e1d21SSam Leffler * 1. Redistributions of source code must retain the above copyright 107535e66aSSam Leffler * notice, this list of conditions and the following disclaimer. 117535e66aSSam Leffler * 2. Redistributions in binary form must reproduce the above copyright 127535e66aSSam Leffler * notice, this list of conditions and the following disclaimer in the 137535e66aSSam Leffler * documentation and/or other materials provided with the distribution. 141a1e1d21SSam Leffler * 157535e66aSSam Leffler * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 167535e66aSSam Leffler * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 177535e66aSSam Leffler * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 187535e66aSSam Leffler * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 197535e66aSSam Leffler * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 207535e66aSSam Leffler * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 217535e66aSSam Leffler * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 227535e66aSSam Leffler * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 237535e66aSSam Leffler * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 247535e66aSSam Leffler * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 251a1e1d21SSam Leffler */ 261a1e1d21SSam Leffler 271a1e1d21SSam Leffler #include <sys/cdefs.h> 281a1e1d21SSam Leffler __FBSDID("$FreeBSD$"); 291a1e1d21SSam Leffler 301a1e1d21SSam Leffler /* 311a1e1d21SSam Leffler * IEEE 802.11 generic handler 321a1e1d21SSam Leffler */ 33b032f27cSSam Leffler #include "opt_wlan.h" 341a1e1d21SSam Leffler 351a1e1d21SSam Leffler #include <sys/param.h> 361a1e1d21SSam Leffler #include <sys/systm.h> 371a1e1d21SSam Leffler #include <sys/kernel.h> 388ec07310SGleb Smirnoff #include <sys/malloc.h> 398a1b9b6aSSam Leffler #include <sys/socket.h> 407a79cebfSGleb Smirnoff #include <sys/sbuf.h> 411a1e1d21SSam Leffler 42c8f5794eSGleb Smirnoff #include <machine/stdarg.h> 43c8f5794eSGleb Smirnoff 441a1e1d21SSam Leffler #include <net/if.h> 4576039bc8SGleb Smirnoff #include <net/if_var.h> 46b032f27cSSam Leffler #include <net/if_dl.h> 471a1e1d21SSam Leffler #include <net/if_media.h> 48b032f27cSSam Leffler #include <net/if_types.h> 491a1e1d21SSam Leffler #include <net/ethernet.h> 501a1e1d21SSam Leffler 511a1e1d21SSam Leffler #include <net80211/ieee80211_var.h> 52b032f27cSSam Leffler #include <net80211/ieee80211_regdomain.h> 53616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG 54616190d0SSam Leffler #include <net80211/ieee80211_superg.h> 55616190d0SSam Leffler #endif 56b6108616SRui Paulo #include <net80211/ieee80211_ratectl.h> 571a1e1d21SSam Leffler 581a1e1d21SSam Leffler #include <net/bpf.h> 591a1e1d21SSam Leffler 60bb77492fSSam Leffler const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = { 61bb77492fSSam Leffler [IEEE80211_MODE_AUTO] = "auto", 62bb77492fSSam Leffler [IEEE80211_MODE_11A] = "11a", 63bb77492fSSam Leffler [IEEE80211_MODE_11B] = "11b", 64bb77492fSSam Leffler [IEEE80211_MODE_11G] = "11g", 65bb77492fSSam Leffler [IEEE80211_MODE_FH] = "FH", 66bb77492fSSam Leffler [IEEE80211_MODE_TURBO_A] = "turboA", 67bb77492fSSam Leffler [IEEE80211_MODE_TURBO_G] = "turboG", 68bb77492fSSam Leffler [IEEE80211_MODE_STURBO_A] = "sturboA", 696a76ae21SSam Leffler [IEEE80211_MODE_HALF] = "half", 706a76ae21SSam Leffler [IEEE80211_MODE_QUARTER] = "quarter", 71bb77492fSSam Leffler [IEEE80211_MODE_11NA] = "11na", 72bb77492fSSam Leffler [IEEE80211_MODE_11NG] = "11ng", 731a1e1d21SSam Leffler }; 74c43feedeSSam Leffler /* map ieee80211_opmode to the corresponding capability bit */ 75c43feedeSSam Leffler const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = { 76c43feedeSSam Leffler [IEEE80211_M_IBSS] = IEEE80211_C_IBSS, 77c43feedeSSam Leffler [IEEE80211_M_WDS] = IEEE80211_C_WDS, 78c43feedeSSam Leffler [IEEE80211_M_STA] = IEEE80211_C_STA, 79c43feedeSSam Leffler [IEEE80211_M_AHDEMO] = IEEE80211_C_AHDEMO, 80c43feedeSSam Leffler [IEEE80211_M_HOSTAP] = IEEE80211_C_HOSTAP, 81c43feedeSSam Leffler [IEEE80211_M_MONITOR] = IEEE80211_C_MONITOR, 8259aa14a9SRui Paulo #ifdef IEEE80211_SUPPORT_MESH 8359aa14a9SRui Paulo [IEEE80211_M_MBSS] = IEEE80211_C_MBSS, 8459aa14a9SRui Paulo #endif 85c43feedeSSam Leffler }; 86c43feedeSSam Leffler 8792002144SGleb Smirnoff const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] = 88b032f27cSSam Leffler { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 89b032f27cSSam Leffler 90b032f27cSSam Leffler static void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag); 912bfc8a91SSam Leffler static void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag); 92b032f27cSSam Leffler static void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag); 93b032f27cSSam Leffler static int ieee80211_media_setup(struct ieee80211com *ic, 94b032f27cSSam Leffler struct ifmedia *media, int caps, int addsta, 95b032f27cSSam Leffler ifm_change_cb_t media_change, ifm_stat_cb_t media_stat); 96b032f27cSSam Leffler static int media_status(enum ieee80211_opmode, 97b032f27cSSam Leffler const struct ieee80211_channel *); 9828da1b56SGleb Smirnoff static uint64_t ieee80211_get_counter(struct ifnet *, ift_counter); 99b032f27cSSam Leffler 100b032f27cSSam Leffler MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state"); 1011a1e1d21SSam Leffler 102aadecb1aSSam Leffler /* 103aadecb1aSSam Leffler * Default supported rates for 802.11 operation (in IEEE .5Mb units). 104aadecb1aSSam Leffler */ 105aadecb1aSSam Leffler #define B(r) ((r) | IEEE80211_RATE_BASIC) 106aadecb1aSSam Leffler static const struct ieee80211_rateset ieee80211_rateset_11a = 107aadecb1aSSam Leffler { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } }; 10841b3c790SSam Leffler static const struct ieee80211_rateset ieee80211_rateset_half = 10941b3c790SSam Leffler { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } }; 11041b3c790SSam Leffler static const struct ieee80211_rateset ieee80211_rateset_quarter = 11141b3c790SSam Leffler { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } }; 112aadecb1aSSam Leffler static const struct ieee80211_rateset ieee80211_rateset_11b = 113aadecb1aSSam Leffler { 4, { B(2), B(4), B(11), B(22) } }; 114aadecb1aSSam Leffler /* NB: OFDM rates are handled specially based on mode */ 115aadecb1aSSam Leffler static const struct ieee80211_rateset ieee80211_rateset_11g = 116aadecb1aSSam Leffler { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } }; 117aadecb1aSSam Leffler #undef B 118aadecb1aSSam Leffler 1191a1e1d21SSam Leffler /* 1201a1e1d21SSam Leffler * Fill in 802.11 available channel set, mark 1211a1e1d21SSam Leffler * all available channels as active, and pick 1221a1e1d21SSam Leffler * a default channel if not already specified. 1231a1e1d21SSam Leffler */ 1247a79cebfSGleb Smirnoff void 12541b3c790SSam Leffler ieee80211_chan_init(struct ieee80211com *ic) 12641b3c790SSam Leffler { 12741b3c790SSam Leffler #define DEFAULTRATES(m, def) do { \ 1286a76ae21SSam Leffler if (ic->ic_sup_rates[m].rs_nrates == 0) \ 12945fa8b0eSSam Leffler ic->ic_sup_rates[m] = def; \ 13041b3c790SSam Leffler } while (0) 13141b3c790SSam Leffler struct ieee80211_channel *c; 13241b3c790SSam Leffler int i; 13341b3c790SSam Leffler 13431378b1cSSam Leffler KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX, 13568e8e04eSSam Leffler ("invalid number of channels specified: %u", ic->ic_nchans)); 1361a1e1d21SSam Leffler memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail)); 137b032f27cSSam Leffler memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps)); 1386dbd16f1SSam Leffler setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO); 13968e8e04eSSam Leffler for (i = 0; i < ic->ic_nchans; i++) { 1401a1e1d21SSam Leffler c = &ic->ic_channels[i]; 14168e8e04eSSam Leffler KASSERT(c->ic_flags != 0, ("channel with no flags")); 1429c2c544dSSam Leffler /* 1439c2c544dSSam Leffler * Help drivers that work only with frequencies by filling 1449c2c544dSSam Leffler * in IEEE channel #'s if not already calculated. Note this 1459c2c544dSSam Leffler * mimics similar work done in ieee80211_setregdomain when 1469c2c544dSSam Leffler * changing regulatory state. 1479c2c544dSSam Leffler */ 1489c2c544dSSam Leffler if (c->ic_ieee == 0) 1499c2c544dSSam Leffler c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags); 1509c2c544dSSam Leffler if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0) 1519c2c544dSSam Leffler c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq + 1529c2c544dSSam Leffler (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20), 1539c2c544dSSam Leffler c->ic_flags); 1549c2c544dSSam Leffler /* default max tx power to max regulatory */ 1559c2c544dSSam Leffler if (c->ic_maxpower == 0) 1569c2c544dSSam Leffler c->ic_maxpower = 2*c->ic_maxregpower; 15768e8e04eSSam Leffler setbit(ic->ic_chan_avail, c->ic_ieee); 1581a1e1d21SSam Leffler /* 1591a1e1d21SSam Leffler * Identify mode capabilities. 1601a1e1d21SSam Leffler */ 1611a1e1d21SSam Leffler if (IEEE80211_IS_CHAN_A(c)) 1626dbd16f1SSam Leffler setbit(ic->ic_modecaps, IEEE80211_MODE_11A); 1631a1e1d21SSam Leffler if (IEEE80211_IS_CHAN_B(c)) 1646dbd16f1SSam Leffler setbit(ic->ic_modecaps, IEEE80211_MODE_11B); 16545fa8b0eSSam Leffler if (IEEE80211_IS_CHAN_ANYG(c)) 1666dbd16f1SSam Leffler setbit(ic->ic_modecaps, IEEE80211_MODE_11G); 1674844aa7dSAtsushi Onoe if (IEEE80211_IS_CHAN_FHSS(c)) 1686dbd16f1SSam Leffler setbit(ic->ic_modecaps, IEEE80211_MODE_FH); 16968e8e04eSSam Leffler if (IEEE80211_IS_CHAN_108A(c)) 1706dbd16f1SSam Leffler setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A); 1718a1b9b6aSSam Leffler if (IEEE80211_IS_CHAN_108G(c)) 1726dbd16f1SSam Leffler setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G); 17368e8e04eSSam Leffler if (IEEE80211_IS_CHAN_ST(c)) 17468e8e04eSSam Leffler setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A); 1756a76ae21SSam Leffler if (IEEE80211_IS_CHAN_HALF(c)) 1766a76ae21SSam Leffler setbit(ic->ic_modecaps, IEEE80211_MODE_HALF); 1776a76ae21SSam Leffler if (IEEE80211_IS_CHAN_QUARTER(c)) 1786a76ae21SSam Leffler setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER); 17968e8e04eSSam Leffler if (IEEE80211_IS_CHAN_HTA(c)) 18068e8e04eSSam Leffler setbit(ic->ic_modecaps, IEEE80211_MODE_11NA); 18168e8e04eSSam Leffler if (IEEE80211_IS_CHAN_HTG(c)) 18268e8e04eSSam Leffler setbit(ic->ic_modecaps, IEEE80211_MODE_11NG); 18368e8e04eSSam Leffler } 18468e8e04eSSam Leffler /* initialize candidate channels to all available */ 18568e8e04eSSam Leffler memcpy(ic->ic_chan_active, ic->ic_chan_avail, 18668e8e04eSSam Leffler sizeof(ic->ic_chan_avail)); 18768e8e04eSSam Leffler 188b032f27cSSam Leffler /* sort channel table to allow lookup optimizations */ 189b032f27cSSam Leffler ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans); 190b032f27cSSam Leffler 191b032f27cSSam Leffler /* invalidate any previous state */ 19268e8e04eSSam Leffler ic->ic_bsschan = IEEE80211_CHAN_ANYC; 193ab562eefSSam Leffler ic->ic_prevchan = NULL; 194b032f27cSSam Leffler ic->ic_csa_newchan = NULL; 195b5c99415SSam Leffler /* arbitrarily pick the first channel */ 19668e8e04eSSam Leffler ic->ic_curchan = &ic->ic_channels[0]; 19726d39e2cSSam Leffler ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan); 198aadecb1aSSam Leffler 199aadecb1aSSam Leffler /* fillin well-known rate sets if driver has not specified */ 20041b3c790SSam Leffler DEFAULTRATES(IEEE80211_MODE_11B, ieee80211_rateset_11b); 20141b3c790SSam Leffler DEFAULTRATES(IEEE80211_MODE_11G, ieee80211_rateset_11g); 20241b3c790SSam Leffler DEFAULTRATES(IEEE80211_MODE_11A, ieee80211_rateset_11a); 20341b3c790SSam Leffler DEFAULTRATES(IEEE80211_MODE_TURBO_A, ieee80211_rateset_11a); 20441b3c790SSam Leffler DEFAULTRATES(IEEE80211_MODE_TURBO_G, ieee80211_rateset_11g); 2058500d65dSSam Leffler DEFAULTRATES(IEEE80211_MODE_STURBO_A, ieee80211_rateset_11a); 2066a76ae21SSam Leffler DEFAULTRATES(IEEE80211_MODE_HALF, ieee80211_rateset_half); 2076a76ae21SSam Leffler DEFAULTRATES(IEEE80211_MODE_QUARTER, ieee80211_rateset_quarter); 20840432d36SSam Leffler DEFAULTRATES(IEEE80211_MODE_11NA, ieee80211_rateset_11a); 20940432d36SSam Leffler DEFAULTRATES(IEEE80211_MODE_11NG, ieee80211_rateset_11g); 21041b3c790SSam Leffler 21141b3c790SSam Leffler /* 212fbbe47a9SBernhard Schmidt * Setup required information to fill the mcsset field, if driver did 213fbbe47a9SBernhard Schmidt * not. Assume a 2T2R setup for historic reasons. 214fbbe47a9SBernhard Schmidt */ 215fbbe47a9SBernhard Schmidt if (ic->ic_rxstream == 0) 216fbbe47a9SBernhard Schmidt ic->ic_rxstream = 2; 217fbbe47a9SBernhard Schmidt if (ic->ic_txstream == 0) 218fbbe47a9SBernhard Schmidt ic->ic_txstream = 2; 219fbbe47a9SBernhard Schmidt 220fbbe47a9SBernhard Schmidt /* 22141b3c790SSam Leffler * Set auto mode to reset active channel state and any desired channel. 22241b3c790SSam Leffler */ 22341b3c790SSam Leffler (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO); 22441b3c790SSam Leffler #undef DEFAULTRATES 22541b3c790SSam Leffler } 22641b3c790SSam Leffler 227b032f27cSSam Leffler static void 228272f6adeSGleb Smirnoff null_update_mcast(struct ieee80211com *ic) 229b032f27cSSam Leffler { 230272f6adeSGleb Smirnoff 231272f6adeSGleb Smirnoff ic_printf(ic, "need multicast update callback\n"); 232b032f27cSSam Leffler } 233b032f27cSSam Leffler 234b032f27cSSam Leffler static void 235272f6adeSGleb Smirnoff null_update_promisc(struct ieee80211com *ic) 236b032f27cSSam Leffler { 237272f6adeSGleb Smirnoff 238272f6adeSGleb Smirnoff ic_printf(ic, "need promiscuous mode update callback\n"); 239b032f27cSSam Leffler } 240b032f27cSSam Leffler 241b94299c4SAdrian Chadd static void 242b94299c4SAdrian Chadd null_update_chw(struct ieee80211com *ic) 243b94299c4SAdrian Chadd { 244b94299c4SAdrian Chadd 245c8f5794eSGleb Smirnoff ic_printf(ic, "%s: need callback\n", __func__); 246c8f5794eSGleb Smirnoff } 247c8f5794eSGleb Smirnoff 248c8f5794eSGleb Smirnoff int 249c8f5794eSGleb Smirnoff ic_printf(struct ieee80211com *ic, const char * fmt, ...) 250c8f5794eSGleb Smirnoff { 251c8f5794eSGleb Smirnoff va_list ap; 252c8f5794eSGleb Smirnoff int retval; 253c8f5794eSGleb Smirnoff 254c8f5794eSGleb Smirnoff retval = printf("%s: ", ic->ic_name); 255c8f5794eSGleb Smirnoff va_start(ap, fmt); 256c8f5794eSGleb Smirnoff retval += vprintf(fmt, ap); 257c8f5794eSGleb Smirnoff va_end(ap); 258c8f5794eSGleb Smirnoff return (retval); 259b94299c4SAdrian Chadd } 260b94299c4SAdrian Chadd 2617a79cebfSGleb Smirnoff static LIST_HEAD(, ieee80211com) ic_head = LIST_HEAD_INITIALIZER(ic_head); 2627a79cebfSGleb Smirnoff static struct mtx ic_list_mtx; 2637a79cebfSGleb Smirnoff MTX_SYSINIT(ic_list, &ic_list_mtx, "ieee80211com list", MTX_DEF); 2647a79cebfSGleb Smirnoff 2657a79cebfSGleb Smirnoff static int 2667a79cebfSGleb Smirnoff sysctl_ieee80211coms(SYSCTL_HANDLER_ARGS) 2677a79cebfSGleb Smirnoff { 2687a79cebfSGleb Smirnoff struct ieee80211com *ic; 269f09a089eSAndriy Voskoboinyk struct sbuf sb; 2707a79cebfSGleb Smirnoff char *sp; 2717a79cebfSGleb Smirnoff int error; 2727a79cebfSGleb Smirnoff 273f09a089eSAndriy Voskoboinyk error = sysctl_wire_old_buffer(req, 0); 274f09a089eSAndriy Voskoboinyk if (error) 275f09a089eSAndriy Voskoboinyk return (error); 276f09a089eSAndriy Voskoboinyk sbuf_new_for_sysctl(&sb, NULL, 8, req); 277f09a089eSAndriy Voskoboinyk sbuf_clear_flags(&sb, SBUF_INCLUDENUL); 2787a79cebfSGleb Smirnoff sp = ""; 2797a79cebfSGleb Smirnoff mtx_lock(&ic_list_mtx); 2807a79cebfSGleb Smirnoff LIST_FOREACH(ic, &ic_head, ic_next) { 281f09a089eSAndriy Voskoboinyk sbuf_printf(&sb, "%s%s", sp, ic->ic_name); 2827a79cebfSGleb Smirnoff sp = " "; 2837a79cebfSGleb Smirnoff } 2847a79cebfSGleb Smirnoff mtx_unlock(&ic_list_mtx); 285f09a089eSAndriy Voskoboinyk error = sbuf_finish(&sb); 286f09a089eSAndriy Voskoboinyk sbuf_delete(&sb); 2877a79cebfSGleb Smirnoff return (error); 2887a79cebfSGleb Smirnoff } 2897a79cebfSGleb Smirnoff 2907a79cebfSGleb Smirnoff SYSCTL_PROC(_net_wlan, OID_AUTO, devices, 2917a79cebfSGleb Smirnoff CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 2927a79cebfSGleb Smirnoff sysctl_ieee80211coms, "A", "names of available 802.11 devices"); 2937a79cebfSGleb Smirnoff 294b032f27cSSam Leffler /* 295b032f27cSSam Leffler * Attach/setup the common net80211 state. Called by 296b032f27cSSam Leffler * the driver on attach to prior to creating any vap's. 297b032f27cSSam Leffler */ 29841b3c790SSam Leffler void 2997a79cebfSGleb Smirnoff ieee80211_ifattach(struct ieee80211com *ic) 30041b3c790SSam Leffler { 30141b3c790SSam Leffler 302c8f5794eSGleb Smirnoff IEEE80211_LOCK_INIT(ic, ic->ic_name); 303c8f5794eSGleb Smirnoff IEEE80211_TX_LOCK_INIT(ic, ic->ic_name); 304b032f27cSSam Leffler TAILQ_INIT(&ic->ic_vaps); 3055efea30fSAndrew Thompson 3065efea30fSAndrew Thompson /* Create a taskqueue for all state changes */ 3075efea30fSAndrew Thompson ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO, 3085efea30fSAndrew Thompson taskqueue_thread_enqueue, &ic->ic_tq); 3097b2b15ebSAdrian Chadd taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq", 3107fc10b6bSGleb Smirnoff ic->ic_name); 31128da1b56SGleb Smirnoff ic->ic_ierrors = counter_u64_alloc(M_WAITOK); 31228da1b56SGleb Smirnoff ic->ic_oerrors = counter_u64_alloc(M_WAITOK); 31341b3c790SSam Leffler /* 31441b3c790SSam Leffler * Fill in 802.11 available channel set, mark all 31541b3c790SSam Leffler * available channels as active, and pick a default 31641b3c790SSam Leffler * channel if not already specified. 31741b3c790SSam Leffler */ 3187a79cebfSGleb Smirnoff ieee80211_chan_init(ic); 31968e8e04eSSam Leffler 320b032f27cSSam Leffler ic->ic_update_mcast = null_update_mcast; 321b032f27cSSam Leffler ic->ic_update_promisc = null_update_promisc; 322b94299c4SAdrian Chadd ic->ic_update_chw = null_update_chw; 3231a1e1d21SSam Leffler 3245b16c28cSSam Leffler ic->ic_hash_key = arc4random(); 325d365f9c7SSam Leffler ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT; 326d365f9c7SSam Leffler ic->ic_lintval = ic->ic_bintval; 3278a1b9b6aSSam Leffler ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX; 3288a1b9b6aSSam Leffler 32968e8e04eSSam Leffler ieee80211_crypto_attach(ic); 3308a1b9b6aSSam Leffler ieee80211_node_attach(ic); 33168e8e04eSSam Leffler ieee80211_power_attach(ic); 3328a1b9b6aSSam Leffler ieee80211_proto_attach(ic); 333616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG 334616190d0SSam Leffler ieee80211_superg_attach(ic); 335616190d0SSam Leffler #endif 33668e8e04eSSam Leffler ieee80211_ht_attach(ic); 33768e8e04eSSam Leffler ieee80211_scan_attach(ic); 338b032f27cSSam Leffler ieee80211_regdomain_attach(ic); 339e95e0edbSSam Leffler ieee80211_dfs_attach(ic); 3408a1b9b6aSSam Leffler 341b032f27cSSam Leffler ieee80211_sysctl_attach(ic); 3428a1b9b6aSSam Leffler 3437a79cebfSGleb Smirnoff mtx_lock(&ic_list_mtx); 3447a79cebfSGleb Smirnoff LIST_INSERT_HEAD(&ic_head, ic, ic_next); 3457a79cebfSGleb Smirnoff mtx_unlock(&ic_list_mtx); 3461a1e1d21SSam Leffler } 3471a1e1d21SSam Leffler 348b032f27cSSam Leffler /* 349b032f27cSSam Leffler * Detach net80211 state on device detach. Tear down 350b032f27cSSam Leffler * all vap's and reclaim all common state prior to the 351b032f27cSSam Leffler * device state going away. Note we may call back into 352b032f27cSSam Leffler * driver; it must be prepared for this. 353b032f27cSSam Leffler */ 3541a1e1d21SSam Leffler void 3558a1b9b6aSSam Leffler ieee80211_ifdetach(struct ieee80211com *ic) 3561a1e1d21SSam Leffler { 357b032f27cSSam Leffler struct ieee80211vap *vap; 3581a1e1d21SSam Leffler 3597a79cebfSGleb Smirnoff mtx_lock(&ic_list_mtx); 3607a79cebfSGleb Smirnoff LIST_REMOVE(ic, ic_next); 3617a79cebfSGleb Smirnoff mtx_unlock(&ic_list_mtx); 3625c600a90SSam Leffler 3634061c639SAndriy Voskoboinyk taskqueue_drain(taskqueue_thread, &ic->ic_restart_task); 3644061c639SAndriy Voskoboinyk 36530e4856aSAdrian Chadd /* 36630e4856aSAdrian Chadd * The VAP is responsible for setting and clearing 36730e4856aSAdrian Chadd * the VIMAGE context. 36830e4856aSAdrian Chadd */ 369b032f27cSSam Leffler while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL) 370b032f27cSSam Leffler ieee80211_vap_destroy(vap); 371ae55932eSAndrew Thompson ieee80211_waitfor_parent(ic); 3728a1b9b6aSSam Leffler 3738a1b9b6aSSam Leffler ieee80211_sysctl_detach(ic); 374e95e0edbSSam Leffler ieee80211_dfs_detach(ic); 375b032f27cSSam Leffler ieee80211_regdomain_detach(ic); 37668e8e04eSSam Leffler ieee80211_scan_detach(ic); 377616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG 378616190d0SSam Leffler ieee80211_superg_detach(ic); 379616190d0SSam Leffler #endif 38068e8e04eSSam Leffler ieee80211_ht_detach(ic); 381ca4ac7aeSSam Leffler /* NB: must be called before ieee80211_node_detach */ 3828a1b9b6aSSam Leffler ieee80211_proto_detach(ic); 3838a1b9b6aSSam Leffler ieee80211_crypto_detach(ic); 38468e8e04eSSam Leffler ieee80211_power_detach(ic); 3858a1b9b6aSSam Leffler ieee80211_node_detach(ic); 3868a1b9b6aSSam Leffler 38728da1b56SGleb Smirnoff counter_u64_free(ic->ic_ierrors); 38828da1b56SGleb Smirnoff counter_u64_free(ic->ic_oerrors); 38930e4856aSAdrian Chadd 3905efea30fSAndrew Thompson taskqueue_free(ic->ic_tq); 3915cda6006SAdrian Chadd IEEE80211_TX_LOCK_DESTROY(ic); 39268e8e04eSSam Leffler IEEE80211_LOCK_DESTROY(ic); 393b032f27cSSam Leffler } 3948a1b9b6aSSam Leffler 3957a79cebfSGleb Smirnoff struct ieee80211com * 3967a79cebfSGleb Smirnoff ieee80211_find_com(const char *name) 3977a79cebfSGleb Smirnoff { 3987a79cebfSGleb Smirnoff struct ieee80211com *ic; 3997a79cebfSGleb Smirnoff 4007a79cebfSGleb Smirnoff mtx_lock(&ic_list_mtx); 4017a79cebfSGleb Smirnoff LIST_FOREACH(ic, &ic_head, ic_next) 4027a79cebfSGleb Smirnoff if (strcmp(ic->ic_name, name) == 0) 4037a79cebfSGleb Smirnoff break; 4047a79cebfSGleb Smirnoff mtx_unlock(&ic_list_mtx); 4057a79cebfSGleb Smirnoff 4067a79cebfSGleb Smirnoff return (ic); 4077a79cebfSGleb Smirnoff } 4087a79cebfSGleb Smirnoff 4097cde0202SAndriy Voskoboinyk void 4107cde0202SAndriy Voskoboinyk ieee80211_iterate_coms(ieee80211_com_iter_func *f, void *arg) 4117cde0202SAndriy Voskoboinyk { 4127cde0202SAndriy Voskoboinyk struct ieee80211com *ic; 4137cde0202SAndriy Voskoboinyk 4147cde0202SAndriy Voskoboinyk mtx_lock(&ic_list_mtx); 4157cde0202SAndriy Voskoboinyk LIST_FOREACH(ic, &ic_head, ic_next) 4167cde0202SAndriy Voskoboinyk (*f)(arg, ic); 4177cde0202SAndriy Voskoboinyk mtx_unlock(&ic_list_mtx); 4187cde0202SAndriy Voskoboinyk } 4197cde0202SAndriy Voskoboinyk 420b032f27cSSam Leffler /* 421b032f27cSSam Leffler * Default reset method for use with the ioctl support. This 422b032f27cSSam Leffler * method is invoked after any state change in the 802.11 423b032f27cSSam Leffler * layer that should be propagated to the hardware but not 424b032f27cSSam Leffler * require re-initialization of the 802.11 state machine (e.g 425b032f27cSSam Leffler * rescanning for an ap). We always return ENETRESET which 426b032f27cSSam Leffler * should cause the driver to re-initialize the device. Drivers 427b032f27cSSam Leffler * can override this method to implement more optimized support. 428b032f27cSSam Leffler */ 429b032f27cSSam Leffler static int 430b032f27cSSam Leffler default_reset(struct ieee80211vap *vap, u_long cmd) 431b032f27cSSam Leffler { 432b032f27cSSam Leffler return ENETRESET; 433b032f27cSSam Leffler } 434b032f27cSSam Leffler 435b032f27cSSam Leffler /* 43628da1b56SGleb Smirnoff * Add underlying device errors to vap errors. 43728da1b56SGleb Smirnoff */ 43828da1b56SGleb Smirnoff static uint64_t 43928da1b56SGleb Smirnoff ieee80211_get_counter(struct ifnet *ifp, ift_counter cnt) 44028da1b56SGleb Smirnoff { 44128da1b56SGleb Smirnoff struct ieee80211vap *vap = ifp->if_softc; 44228da1b56SGleb Smirnoff struct ieee80211com *ic = vap->iv_ic; 44328da1b56SGleb Smirnoff uint64_t rv; 44428da1b56SGleb Smirnoff 44528da1b56SGleb Smirnoff rv = if_get_counter_default(ifp, cnt); 44628da1b56SGleb Smirnoff switch (cnt) { 44728da1b56SGleb Smirnoff case IFCOUNTER_OERRORS: 44828da1b56SGleb Smirnoff rv += counter_u64_fetch(ic->ic_oerrors); 44928da1b56SGleb Smirnoff break; 45028da1b56SGleb Smirnoff case IFCOUNTER_IERRORS: 45128da1b56SGleb Smirnoff rv += counter_u64_fetch(ic->ic_ierrors); 45228da1b56SGleb Smirnoff break; 45328da1b56SGleb Smirnoff default: 45428da1b56SGleb Smirnoff break; 45528da1b56SGleb Smirnoff } 45628da1b56SGleb Smirnoff 45728da1b56SGleb Smirnoff return (rv); 45828da1b56SGleb Smirnoff } 45928da1b56SGleb Smirnoff 46028da1b56SGleb Smirnoff /* 461b032f27cSSam Leffler * Prepare a vap for use. Drivers use this call to 462b032f27cSSam Leffler * setup net80211 state in new vap's prior attaching 463b032f27cSSam Leffler * them with ieee80211_vap_attach (below). 464b032f27cSSam Leffler */ 465b032f27cSSam Leffler int 466b032f27cSSam Leffler ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap, 467fcd9500fSBernhard Schmidt const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode, 4687a79cebfSGleb Smirnoff int flags, const uint8_t bssid[IEEE80211_ADDR_LEN]) 469b032f27cSSam Leffler { 470b032f27cSSam Leffler struct ifnet *ifp; 471b032f27cSSam Leffler 472b032f27cSSam Leffler ifp = if_alloc(IFT_ETHER); 473b032f27cSSam Leffler if (ifp == NULL) { 474c8f5794eSGleb Smirnoff ic_printf(ic, "%s: unable to allocate ifnet\n", 475b032f27cSSam Leffler __func__); 476b032f27cSSam Leffler return ENOMEM; 477b032f27cSSam Leffler } 478b032f27cSSam Leffler if_initname(ifp, name, unit); 479b032f27cSSam Leffler ifp->if_softc = vap; /* back pointer */ 480b032f27cSSam Leffler ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST; 481e7495198SAdrian Chadd ifp->if_transmit = ieee80211_vap_transmit; 482e7495198SAdrian Chadd ifp->if_qflush = ieee80211_vap_qflush; 483b032f27cSSam Leffler ifp->if_ioctl = ieee80211_ioctl; 484b032f27cSSam Leffler ifp->if_init = ieee80211_init; 48528da1b56SGleb Smirnoff ifp->if_get_counter = ieee80211_get_counter; 486b032f27cSSam Leffler 487b032f27cSSam Leffler vap->iv_ifp = ifp; 488b032f27cSSam Leffler vap->iv_ic = ic; 489b032f27cSSam Leffler vap->iv_flags = ic->ic_flags; /* propagate common flags */ 490b032f27cSSam Leffler vap->iv_flags_ext = ic->ic_flags_ext; 491b032f27cSSam Leffler vap->iv_flags_ven = ic->ic_flags_ven; 492b032f27cSSam Leffler vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE; 493b032f27cSSam Leffler vap->iv_htcaps = ic->ic_htcaps; 494e1d36f83SRui Paulo vap->iv_htextcaps = ic->ic_htextcaps; 495b032f27cSSam Leffler vap->iv_opmode = opmode; 496c43feedeSSam Leffler vap->iv_caps |= ieee80211_opcap[opmode]; 4971d47c76cSAndriy Voskoboinyk IEEE80211_ADDR_COPY(vap->iv_myaddr, ic->ic_macaddr); 498b032f27cSSam Leffler switch (opmode) { 499b032f27cSSam Leffler case IEEE80211_M_WDS: 500b032f27cSSam Leffler /* 501b032f27cSSam Leffler * WDS links must specify the bssid of the far end. 502b032f27cSSam Leffler * For legacy operation this is a static relationship. 503b032f27cSSam Leffler * For non-legacy operation the station must associate 504b032f27cSSam Leffler * and be authorized to pass traffic. Plumbing the 505b032f27cSSam Leffler * vap to the proper node happens when the vap 506b032f27cSSam Leffler * transitions to RUN state. 507b032f27cSSam Leffler */ 508b032f27cSSam Leffler IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid); 509b032f27cSSam Leffler vap->iv_flags |= IEEE80211_F_DESBSSID; 510b032f27cSSam Leffler if (flags & IEEE80211_CLONE_WDSLEGACY) 511b032f27cSSam Leffler vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY; 512b032f27cSSam Leffler break; 51310ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA 51410ad9a77SSam Leffler case IEEE80211_M_AHDEMO: 51510ad9a77SSam Leffler if (flags & IEEE80211_CLONE_TDMA) { 51610ad9a77SSam Leffler /* NB: checked before clone operation allowed */ 51710ad9a77SSam Leffler KASSERT(ic->ic_caps & IEEE80211_C_TDMA, 51810ad9a77SSam Leffler ("not TDMA capable, ic_caps 0x%x", ic->ic_caps)); 51910ad9a77SSam Leffler /* 52010ad9a77SSam Leffler * Propagate TDMA capability to mark vap; this 52110ad9a77SSam Leffler * cannot be removed and is used to distinguish 52210ad9a77SSam Leffler * regular ahdemo operation from ahdemo+tdma. 52310ad9a77SSam Leffler */ 52410ad9a77SSam Leffler vap->iv_caps |= IEEE80211_C_TDMA; 52510ad9a77SSam Leffler } 52610ad9a77SSam Leffler break; 52710ad9a77SSam Leffler #endif 528fcd9500fSBernhard Schmidt default: 529fcd9500fSBernhard Schmidt break; 530b032f27cSSam Leffler } 531ae3f00bbSSam Leffler /* auto-enable s/w beacon miss support */ 532ae3f00bbSSam Leffler if (flags & IEEE80211_CLONE_NOBEACONS) 533ae3f00bbSSam Leffler vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS; 53483fcb812SAndrew Thompson /* auto-generated or user supplied MAC address */ 53583fcb812SAndrew Thompson if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR)) 53683fcb812SAndrew Thompson vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC; 537b032f27cSSam Leffler /* 538b032f27cSSam Leffler * Enable various functionality by default if we're 539b032f27cSSam Leffler * capable; the driver can override us if it knows better. 540b032f27cSSam Leffler */ 541b032f27cSSam Leffler if (vap->iv_caps & IEEE80211_C_WME) 542b032f27cSSam Leffler vap->iv_flags |= IEEE80211_F_WME; 543b032f27cSSam Leffler if (vap->iv_caps & IEEE80211_C_BURST) 544b032f27cSSam Leffler vap->iv_flags |= IEEE80211_F_BURST; 545b032f27cSSam Leffler /* NB: bg scanning only makes sense for station mode right now */ 546b032f27cSSam Leffler if (vap->iv_opmode == IEEE80211_M_STA && 547b032f27cSSam Leffler (vap->iv_caps & IEEE80211_C_BGSCAN)) 548b032f27cSSam Leffler vap->iv_flags |= IEEE80211_F_BGSCAN; 549c43feedeSSam Leffler vap->iv_flags |= IEEE80211_F_DOTH; /* XXX no cap, just ena */ 55082fd2577SSam Leffler /* NB: DFS support only makes sense for ap mode right now */ 55182fd2577SSam Leffler if (vap->iv_opmode == IEEE80211_M_HOSTAP && 55282fd2577SSam Leffler (vap->iv_caps & IEEE80211_C_DFS)) 553b032f27cSSam Leffler vap->iv_flags_ext |= IEEE80211_FEXT_DFS; 554b032f27cSSam Leffler 555b032f27cSSam Leffler vap->iv_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */ 556b032f27cSSam Leffler vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT; 557b032f27cSSam Leffler vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT; 558b032f27cSSam Leffler /* 559b032f27cSSam Leffler * Install a default reset method for the ioctl support; 560b032f27cSSam Leffler * the driver can override this. 561b032f27cSSam Leffler */ 562b032f27cSSam Leffler vap->iv_reset = default_reset; 563b032f27cSSam Leffler 564b032f27cSSam Leffler ieee80211_sysctl_vattach(vap); 565b032f27cSSam Leffler ieee80211_crypto_vattach(vap); 566b032f27cSSam Leffler ieee80211_node_vattach(vap); 567b032f27cSSam Leffler ieee80211_power_vattach(vap); 568b032f27cSSam Leffler ieee80211_proto_vattach(vap); 569616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG 570616190d0SSam Leffler ieee80211_superg_vattach(vap); 571616190d0SSam Leffler #endif 572b032f27cSSam Leffler ieee80211_ht_vattach(vap); 573b032f27cSSam Leffler ieee80211_scan_vattach(vap); 574b032f27cSSam Leffler ieee80211_regdomain_vattach(vap); 5755463c4a4SSam Leffler ieee80211_radiotap_vattach(vap); 576a7c6aabdSBernhard Schmidt ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE); 577b6108616SRui Paulo 578b032f27cSSam Leffler return 0; 579b032f27cSSam Leffler } 580b032f27cSSam Leffler 581b032f27cSSam Leffler /* 582b032f27cSSam Leffler * Activate a vap. State should have been prepared with a 583b032f27cSSam Leffler * call to ieee80211_vap_setup and by the driver. On return 584b032f27cSSam Leffler * from this call the vap is ready for use. 585b032f27cSSam Leffler */ 586b032f27cSSam Leffler int 5877a79cebfSGleb Smirnoff ieee80211_vap_attach(struct ieee80211vap *vap, ifm_change_cb_t media_change, 5887a79cebfSGleb Smirnoff ifm_stat_cb_t media_stat, const uint8_t macaddr[IEEE80211_ADDR_LEN]) 589b032f27cSSam Leffler { 590b032f27cSSam Leffler struct ifnet *ifp = vap->iv_ifp; 591b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic; 592b032f27cSSam Leffler struct ifmediareq imr; 593b032f27cSSam Leffler int maxrate; 594b032f27cSSam Leffler 595b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, 596b032f27cSSam Leffler "%s: %s parent %s flags 0x%x flags_ext 0x%x\n", 597b032f27cSSam Leffler __func__, ieee80211_opmode_name[vap->iv_opmode], 598c8f5794eSGleb Smirnoff ic->ic_name, vap->iv_flags, vap->iv_flags_ext); 599b032f27cSSam Leffler 600b032f27cSSam Leffler /* 601b032f27cSSam Leffler * Do late attach work that cannot happen until after 602b032f27cSSam Leffler * the driver has had a chance to override defaults. 603b032f27cSSam Leffler */ 604b032f27cSSam Leffler ieee80211_node_latevattach(vap); 605b032f27cSSam Leffler ieee80211_power_latevattach(vap); 606b032f27cSSam Leffler 607b032f27cSSam Leffler maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps, 608b032f27cSSam Leffler vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat); 609b032f27cSSam Leffler ieee80211_media_status(ifp, &imr); 610b032f27cSSam Leffler /* NB: strip explicit mode; we're actually in autoselect */ 611c3f10abdSSam Leffler ifmedia_set(&vap->iv_media, 612c3f10abdSSam Leffler imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO)); 613b032f27cSSam Leffler if (maxrate) 614b032f27cSSam Leffler ifp->if_baudrate = IF_Mbps(maxrate); 615b032f27cSSam Leffler 6167a79cebfSGleb Smirnoff ether_ifattach(ifp, macaddr); 6171d47c76cSAndriy Voskoboinyk IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp)); 618b032f27cSSam Leffler /* hook output method setup by ether_ifattach */ 619b032f27cSSam Leffler vap->iv_output = ifp->if_output; 620b032f27cSSam Leffler ifp->if_output = ieee80211_output; 621b032f27cSSam Leffler /* NB: if_mtu set by ether_ifattach to ETHERMTU */ 622b032f27cSSam Leffler 623b032f27cSSam Leffler IEEE80211_LOCK(ic); 624b032f27cSSam Leffler TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next); 625b032f27cSSam Leffler ieee80211_syncflag_locked(ic, IEEE80211_F_WME); 626616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG 627b032f27cSSam Leffler ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP); 628616190d0SSam Leffler #endif 629b032f27cSSam Leffler ieee80211_syncflag_locked(ic, IEEE80211_F_PCF); 630b032f27cSSam Leffler ieee80211_syncflag_locked(ic, IEEE80211_F_BURST); 6312bfc8a91SSam Leffler ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT); 6322bfc8a91SSam Leffler ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40); 633b032f27cSSam Leffler IEEE80211_UNLOCK(ic); 634b032f27cSSam Leffler 635b032f27cSSam Leffler return 1; 636b032f27cSSam Leffler } 637b032f27cSSam Leffler 638b032f27cSSam Leffler /* 639b032f27cSSam Leffler * Tear down vap state and reclaim the ifnet. 640b032f27cSSam Leffler * The driver is assumed to have prepared for 641b032f27cSSam Leffler * this; e.g. by turning off interrupts for the 642b032f27cSSam Leffler * underlying device. 643b032f27cSSam Leffler */ 644b032f27cSSam Leffler void 645b032f27cSSam Leffler ieee80211_vap_detach(struct ieee80211vap *vap) 646b032f27cSSam Leffler { 647b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic; 648b032f27cSSam Leffler struct ifnet *ifp = vap->iv_ifp; 649b032f27cSSam Leffler 65030e4856aSAdrian Chadd CURVNET_SET(ifp->if_vnet); 65130e4856aSAdrian Chadd 652b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n", 6537fc10b6bSGleb Smirnoff __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name); 654b032f27cSSam Leffler 6551da89db5SSam Leffler /* NB: bpfdetach is called by ether_ifdetach and claims all taps */ 6561da89db5SSam Leffler ether_ifdetach(ifp); 6571da89db5SSam Leffler 6581da89db5SSam Leffler ieee80211_stop(vap); 659b032f27cSSam Leffler 6605efea30fSAndrew Thompson /* 6615efea30fSAndrew Thompson * Flush any deferred vap tasks. 6625efea30fSAndrew Thompson */ 6635efea30fSAndrew Thompson ieee80211_draintask(ic, &vap->iv_nstate_task); 6645efea30fSAndrew Thompson ieee80211_draintask(ic, &vap->iv_swbmiss_task); 6655efea30fSAndrew Thompson 666ab501dd6SSam Leffler /* XXX band-aid until ifnet handles this for us */ 667ab501dd6SSam Leffler taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 668ab501dd6SSam Leffler 6695efea30fSAndrew Thompson IEEE80211_LOCK(ic); 6705efea30fSAndrew Thompson KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running")); 671b032f27cSSam Leffler TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next); 672b032f27cSSam Leffler ieee80211_syncflag_locked(ic, IEEE80211_F_WME); 673616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG 674b032f27cSSam Leffler ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP); 675616190d0SSam Leffler #endif 676b032f27cSSam Leffler ieee80211_syncflag_locked(ic, IEEE80211_F_PCF); 677b032f27cSSam Leffler ieee80211_syncflag_locked(ic, IEEE80211_F_BURST); 6782bfc8a91SSam Leffler ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT); 6792bfc8a91SSam Leffler ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40); 6805463c4a4SSam Leffler /* NB: this handles the bpfdetach done below */ 6815463c4a4SSam Leffler ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF); 6827a79cebfSGleb Smirnoff if (vap->iv_ifflags & IFF_PROMISC) 6837a79cebfSGleb Smirnoff ieee80211_promisc(vap, false); 6847a79cebfSGleb Smirnoff if (vap->iv_ifflags & IFF_ALLMULTI) 6857a79cebfSGleb Smirnoff ieee80211_allmulti(vap, false); 686b032f27cSSam Leffler IEEE80211_UNLOCK(ic); 687b032f27cSSam Leffler 688b032f27cSSam Leffler ifmedia_removeall(&vap->iv_media); 689b032f27cSSam Leffler 6905463c4a4SSam Leffler ieee80211_radiotap_vdetach(vap); 691b032f27cSSam Leffler ieee80211_regdomain_vdetach(vap); 692b032f27cSSam Leffler ieee80211_scan_vdetach(vap); 693616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG 694616190d0SSam Leffler ieee80211_superg_vdetach(vap); 695616190d0SSam Leffler #endif 696b032f27cSSam Leffler ieee80211_ht_vdetach(vap); 697b032f27cSSam Leffler /* NB: must be before ieee80211_node_vdetach */ 698b032f27cSSam Leffler ieee80211_proto_vdetach(vap); 699b032f27cSSam Leffler ieee80211_crypto_vdetach(vap); 700b032f27cSSam Leffler ieee80211_power_vdetach(vap); 701b032f27cSSam Leffler ieee80211_node_vdetach(vap); 702b032f27cSSam Leffler ieee80211_sysctl_vdetach(vap); 703b20f0ed1SWeongyo Jeong 704b20f0ed1SWeongyo Jeong if_free(ifp); 70530e4856aSAdrian Chadd 70630e4856aSAdrian Chadd CURVNET_RESTORE(); 707b032f27cSSam Leffler } 708b032f27cSSam Leffler 709b032f27cSSam Leffler /* 7107a79cebfSGleb Smirnoff * Count number of vaps in promisc, and issue promisc on 7117a79cebfSGleb Smirnoff * parent respectively. 712b032f27cSSam Leffler */ 713b032f27cSSam Leffler void 7147a79cebfSGleb Smirnoff ieee80211_promisc(struct ieee80211vap *vap, bool on) 715b032f27cSSam Leffler { 7167a79cebfSGleb Smirnoff struct ieee80211com *ic = vap->iv_ic; 717b032f27cSSam Leffler 718c6427be9SAndriy Voskoboinyk IEEE80211_LOCK_ASSERT(ic); 719c6427be9SAndriy Voskoboinyk 7207a79cebfSGleb Smirnoff if (on) { 7217a79cebfSGleb Smirnoff if (++ic->ic_promisc == 1) 722ba2c1fbcSAdrian Chadd ieee80211_runtask(ic, &ic->ic_promisc_task); 7237a79cebfSGleb Smirnoff } else { 7247a79cebfSGleb Smirnoff KASSERT(ic->ic_promisc > 0, ("%s: ic %p not promisc", 7257a79cebfSGleb Smirnoff __func__, ic)); 7267a79cebfSGleb Smirnoff if (--ic->ic_promisc == 0) 7277a79cebfSGleb Smirnoff ieee80211_runtask(ic, &ic->ic_promisc_task); 7287a79cebfSGleb Smirnoff } 7297a79cebfSGleb Smirnoff } 7307a79cebfSGleb Smirnoff 7317a79cebfSGleb Smirnoff /* 7327a79cebfSGleb Smirnoff * Count number of vaps in allmulti, and issue allmulti on 7337a79cebfSGleb Smirnoff * parent respectively. 7347a79cebfSGleb Smirnoff */ 7357a79cebfSGleb Smirnoff void 7367a79cebfSGleb Smirnoff ieee80211_allmulti(struct ieee80211vap *vap, bool on) 7377a79cebfSGleb Smirnoff { 7387a79cebfSGleb Smirnoff struct ieee80211com *ic = vap->iv_ic; 7397a79cebfSGleb Smirnoff 740c6427be9SAndriy Voskoboinyk IEEE80211_LOCK_ASSERT(ic); 741c6427be9SAndriy Voskoboinyk 7427a79cebfSGleb Smirnoff if (on) { 7437a79cebfSGleb Smirnoff if (++ic->ic_allmulti == 1) 7447a79cebfSGleb Smirnoff ieee80211_runtask(ic, &ic->ic_mcast_task); 7457a79cebfSGleb Smirnoff } else { 7467a79cebfSGleb Smirnoff KASSERT(ic->ic_allmulti > 0, ("%s: ic %p not allmulti", 7477a79cebfSGleb Smirnoff __func__, ic)); 7487a79cebfSGleb Smirnoff if (--ic->ic_allmulti == 0) 7495efea30fSAndrew Thompson ieee80211_runtask(ic, &ic->ic_mcast_task); 750b032f27cSSam Leffler } 751b032f27cSSam Leffler } 752b032f27cSSam Leffler 753b032f27cSSam Leffler /* 754b032f27cSSam Leffler * Synchronize flag bit state in the com structure 755b032f27cSSam Leffler * according to the state of all vap's. This is used, 756b032f27cSSam Leffler * for example, to handle state changes via ioctls. 757b032f27cSSam Leffler */ 758b032f27cSSam Leffler static void 759b032f27cSSam Leffler ieee80211_syncflag_locked(struct ieee80211com *ic, int flag) 760b032f27cSSam Leffler { 761b032f27cSSam Leffler struct ieee80211vap *vap; 762b032f27cSSam Leffler int bit; 763b032f27cSSam Leffler 764b032f27cSSam Leffler IEEE80211_LOCK_ASSERT(ic); 765b032f27cSSam Leffler 766b032f27cSSam Leffler bit = 0; 767b032f27cSSam Leffler TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 768b032f27cSSam Leffler if (vap->iv_flags & flag) { 769b032f27cSSam Leffler bit = 1; 770b032f27cSSam Leffler break; 771b032f27cSSam Leffler } 772b032f27cSSam Leffler if (bit) 773b032f27cSSam Leffler ic->ic_flags |= flag; 774b032f27cSSam Leffler else 775b032f27cSSam Leffler ic->ic_flags &= ~flag; 776b032f27cSSam Leffler } 777b032f27cSSam Leffler 778b032f27cSSam Leffler void 779b032f27cSSam Leffler ieee80211_syncflag(struct ieee80211vap *vap, int flag) 780b032f27cSSam Leffler { 781b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic; 782b032f27cSSam Leffler 783b032f27cSSam Leffler IEEE80211_LOCK(ic); 784b032f27cSSam Leffler if (flag < 0) { 785b032f27cSSam Leffler flag = -flag; 786b032f27cSSam Leffler vap->iv_flags &= ~flag; 787b032f27cSSam Leffler } else 788b032f27cSSam Leffler vap->iv_flags |= flag; 789b032f27cSSam Leffler ieee80211_syncflag_locked(ic, flag); 790b032f27cSSam Leffler IEEE80211_UNLOCK(ic); 791b032f27cSSam Leffler } 792b032f27cSSam Leffler 793b032f27cSSam Leffler /* 7942bfc8a91SSam Leffler * Synchronize flags_ht bit state in the com structure 7952bfc8a91SSam Leffler * according to the state of all vap's. This is used, 7962bfc8a91SSam Leffler * for example, to handle state changes via ioctls. 7972bfc8a91SSam Leffler */ 7982bfc8a91SSam Leffler static void 7992bfc8a91SSam Leffler ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag) 8002bfc8a91SSam Leffler { 8012bfc8a91SSam Leffler struct ieee80211vap *vap; 8022bfc8a91SSam Leffler int bit; 8032bfc8a91SSam Leffler 8042bfc8a91SSam Leffler IEEE80211_LOCK_ASSERT(ic); 8052bfc8a91SSam Leffler 8062bfc8a91SSam Leffler bit = 0; 8072bfc8a91SSam Leffler TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 8082bfc8a91SSam Leffler if (vap->iv_flags_ht & flag) { 8092bfc8a91SSam Leffler bit = 1; 8102bfc8a91SSam Leffler break; 8112bfc8a91SSam Leffler } 8122bfc8a91SSam Leffler if (bit) 8132bfc8a91SSam Leffler ic->ic_flags_ht |= flag; 8142bfc8a91SSam Leffler else 8152bfc8a91SSam Leffler ic->ic_flags_ht &= ~flag; 8162bfc8a91SSam Leffler } 8172bfc8a91SSam Leffler 8182bfc8a91SSam Leffler void 8192bfc8a91SSam Leffler ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag) 8202bfc8a91SSam Leffler { 8212bfc8a91SSam Leffler struct ieee80211com *ic = vap->iv_ic; 8222bfc8a91SSam Leffler 8232bfc8a91SSam Leffler IEEE80211_LOCK(ic); 8242bfc8a91SSam Leffler if (flag < 0) { 8252bfc8a91SSam Leffler flag = -flag; 8262bfc8a91SSam Leffler vap->iv_flags_ht &= ~flag; 8272bfc8a91SSam Leffler } else 8282bfc8a91SSam Leffler vap->iv_flags_ht |= flag; 8292bfc8a91SSam Leffler ieee80211_syncflag_ht_locked(ic, flag); 8302bfc8a91SSam Leffler IEEE80211_UNLOCK(ic); 8312bfc8a91SSam Leffler } 8322bfc8a91SSam Leffler 8332bfc8a91SSam Leffler /* 8342bfc8a91SSam Leffler * Synchronize flags_ext bit state in the com structure 835b032f27cSSam Leffler * according to the state of all vap's. This is used, 836b032f27cSSam Leffler * for example, to handle state changes via ioctls. 837b032f27cSSam Leffler */ 838b032f27cSSam Leffler static void 839b032f27cSSam Leffler ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag) 840b032f27cSSam Leffler { 841b032f27cSSam Leffler struct ieee80211vap *vap; 842b032f27cSSam Leffler int bit; 843b032f27cSSam Leffler 844b032f27cSSam Leffler IEEE80211_LOCK_ASSERT(ic); 845b032f27cSSam Leffler 846b032f27cSSam Leffler bit = 0; 847b032f27cSSam Leffler TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 848b032f27cSSam Leffler if (vap->iv_flags_ext & flag) { 849b032f27cSSam Leffler bit = 1; 850b032f27cSSam Leffler break; 851b032f27cSSam Leffler } 852b032f27cSSam Leffler if (bit) 853b032f27cSSam Leffler ic->ic_flags_ext |= flag; 854b032f27cSSam Leffler else 855b032f27cSSam Leffler ic->ic_flags_ext &= ~flag; 856b032f27cSSam Leffler } 857b032f27cSSam Leffler 858b032f27cSSam Leffler void 859b032f27cSSam Leffler ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag) 860b032f27cSSam Leffler { 861b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic; 862b032f27cSSam Leffler 863b032f27cSSam Leffler IEEE80211_LOCK(ic); 864b032f27cSSam Leffler if (flag < 0) { 865b032f27cSSam Leffler flag = -flag; 866b032f27cSSam Leffler vap->iv_flags_ext &= ~flag; 867b032f27cSSam Leffler } else 868b032f27cSSam Leffler vap->iv_flags_ext |= flag; 869b032f27cSSam Leffler ieee80211_syncflag_ext_locked(ic, flag); 870b032f27cSSam Leffler IEEE80211_UNLOCK(ic); 8711a1e1d21SSam Leffler } 8721a1e1d21SSam Leffler 873ca4ac7aeSSam Leffler static __inline int 874ca4ac7aeSSam Leffler mapgsm(u_int freq, u_int flags) 875ca4ac7aeSSam Leffler { 876ca4ac7aeSSam Leffler freq *= 10; 877ca4ac7aeSSam Leffler if (flags & IEEE80211_CHAN_QUARTER) 878ca4ac7aeSSam Leffler freq += 5; 879ca4ac7aeSSam Leffler else if (flags & IEEE80211_CHAN_HALF) 880ca4ac7aeSSam Leffler freq += 10; 881ca4ac7aeSSam Leffler else 882ca4ac7aeSSam Leffler freq += 20; 883ca4ac7aeSSam Leffler /* NB: there is no 907/20 wide but leave room */ 884ca4ac7aeSSam Leffler return (freq - 906*10) / 5; 885ca4ac7aeSSam Leffler } 886ca4ac7aeSSam Leffler 887ca4ac7aeSSam Leffler static __inline int 888ca4ac7aeSSam Leffler mappsb(u_int freq, u_int flags) 889ca4ac7aeSSam Leffler { 890ca4ac7aeSSam Leffler return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5; 891ca4ac7aeSSam Leffler } 892ca4ac7aeSSam Leffler 8931a1e1d21SSam Leffler /* 8941a1e1d21SSam Leffler * Convert MHz frequency to IEEE channel number. 8951a1e1d21SSam Leffler */ 8966f322b78SSam Leffler int 8971a1e1d21SSam Leffler ieee80211_mhz2ieee(u_int freq, u_int flags) 8981a1e1d21SSam Leffler { 89911df4239SSam Leffler #define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990) 900ca4ac7aeSSam Leffler if (flags & IEEE80211_CHAN_GSM) 901ca4ac7aeSSam Leffler return mapgsm(freq, flags); 9021a1e1d21SSam Leffler if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 9031a1e1d21SSam Leffler if (freq == 2484) 9041a1e1d21SSam Leffler return 14; 9051a1e1d21SSam Leffler if (freq < 2484) 9066f322b78SSam Leffler return ((int) freq - 2407) / 5; 9071a1e1d21SSam Leffler else 9081a1e1d21SSam Leffler return 15 + ((freq - 2512) / 20); 909c032abb5SSam Leffler } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */ 91041b3c790SSam Leffler if (freq <= 5000) { 91168e8e04eSSam Leffler /* XXX check regdomain? */ 91211df4239SSam Leffler if (IS_FREQ_IN_PSB(freq)) 913ca4ac7aeSSam Leffler return mappsb(freq, flags); 9146f322b78SSam Leffler return (freq - 4000) / 5; 91541b3c790SSam Leffler } else 9161a1e1d21SSam Leffler return (freq - 5000) / 5; 9171a1e1d21SSam Leffler } else { /* either, guess */ 9181a1e1d21SSam Leffler if (freq == 2484) 9191a1e1d21SSam Leffler return 14; 920ca4ac7aeSSam Leffler if (freq < 2484) { 921ca4ac7aeSSam Leffler if (907 <= freq && freq <= 922) 922ca4ac7aeSSam Leffler return mapgsm(freq, flags); 9236f322b78SSam Leffler return ((int) freq - 2407) / 5; 924ca4ac7aeSSam Leffler } 9256f322b78SSam Leffler if (freq < 5000) { 92611df4239SSam Leffler if (IS_FREQ_IN_PSB(freq)) 927ca4ac7aeSSam Leffler return mappsb(freq, flags); 92841b3c790SSam Leffler else if (freq > 4900) 9296f322b78SSam Leffler return (freq - 4000) / 5; 9306f322b78SSam Leffler else 9311a1e1d21SSam Leffler return 15 + ((freq - 2512) / 20); 9326f322b78SSam Leffler } 9331a1e1d21SSam Leffler return (freq - 5000) / 5; 9341a1e1d21SSam Leffler } 93511df4239SSam Leffler #undef IS_FREQ_IN_PSB 9361a1e1d21SSam Leffler } 9371a1e1d21SSam Leffler 9381a1e1d21SSam Leffler /* 9391a1e1d21SSam Leffler * Convert channel to IEEE channel number. 9401a1e1d21SSam Leffler */ 9416f322b78SSam Leffler int 94238da1496SMatt Jacob ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c) 9431a1e1d21SSam Leffler { 94468e8e04eSSam Leffler if (c == NULL) { 945c8f5794eSGleb Smirnoff ic_printf(ic, "invalid channel (NULL)\n"); 9468be0d570SSam Leffler return 0; /* XXX */ 9471a1e1d21SSam Leffler } 94868e8e04eSSam Leffler return (c == IEEE80211_CHAN_ANYC ? IEEE80211_CHAN_ANY : c->ic_ieee); 9491a1e1d21SSam Leffler } 9501a1e1d21SSam Leffler 9511a1e1d21SSam Leffler /* 9521a1e1d21SSam Leffler * Convert IEEE channel number to MHz frequency. 9531a1e1d21SSam Leffler */ 9541a1e1d21SSam Leffler u_int 9551a1e1d21SSam Leffler ieee80211_ieee2mhz(u_int chan, u_int flags) 9561a1e1d21SSam Leffler { 957ca4ac7aeSSam Leffler if (flags & IEEE80211_CHAN_GSM) 958ca4ac7aeSSam Leffler return 907 + 5 * (chan / 10); 9591a1e1d21SSam Leffler if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 9601a1e1d21SSam Leffler if (chan == 14) 9611a1e1d21SSam Leffler return 2484; 9621a1e1d21SSam Leffler if (chan < 14) 9631a1e1d21SSam Leffler return 2407 + chan*5; 9641a1e1d21SSam Leffler else 9651a1e1d21SSam Leffler return 2512 + ((chan-15)*20); 9661a1e1d21SSam Leffler } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */ 96741b3c790SSam Leffler if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) { 96841b3c790SSam Leffler chan -= 37; 96941b3c790SSam Leffler return 4940 + chan*5 + (chan % 5 ? 2 : 0); 97041b3c790SSam Leffler } 9711a1e1d21SSam Leffler return 5000 + (chan*5); 9721a1e1d21SSam Leffler } else { /* either, guess */ 973ca4ac7aeSSam Leffler /* XXX can't distinguish PSB+GSM channels */ 9741a1e1d21SSam Leffler if (chan == 14) 9751a1e1d21SSam Leffler return 2484; 9761a1e1d21SSam Leffler if (chan < 14) /* 0-13 */ 9771a1e1d21SSam Leffler return 2407 + chan*5; 9781a1e1d21SSam Leffler if (chan < 27) /* 15-26 */ 9791a1e1d21SSam Leffler return 2512 + ((chan-15)*20); 9801a1e1d21SSam Leffler return 5000 + (chan*5); 9811a1e1d21SSam Leffler } 9821a1e1d21SSam Leffler } 9831a1e1d21SSam Leffler 984355fec48SAndriy Voskoboinyk static __inline void 985355fec48SAndriy Voskoboinyk set_extchan(struct ieee80211_channel *c) 986355fec48SAndriy Voskoboinyk { 987355fec48SAndriy Voskoboinyk 988355fec48SAndriy Voskoboinyk /* 989355fec48SAndriy Voskoboinyk * IEEE Std 802.11-2012, page 1738, subclause 20.3.15.4: 990355fec48SAndriy Voskoboinyk * "the secondary channel number shall be 'N + [1,-1] * 4' 991355fec48SAndriy Voskoboinyk */ 992355fec48SAndriy Voskoboinyk if (c->ic_flags & IEEE80211_CHAN_HT40U) 993355fec48SAndriy Voskoboinyk c->ic_extieee = c->ic_ieee + 4; 994355fec48SAndriy Voskoboinyk else if (c->ic_flags & IEEE80211_CHAN_HT40D) 995355fec48SAndriy Voskoboinyk c->ic_extieee = c->ic_ieee - 4; 996355fec48SAndriy Voskoboinyk else 997355fec48SAndriy Voskoboinyk c->ic_extieee = 0; 998355fec48SAndriy Voskoboinyk } 999355fec48SAndriy Voskoboinyk 1000355fec48SAndriy Voskoboinyk static int 1001355fec48SAndriy Voskoboinyk addchan(struct ieee80211_channel chans[], int maxchans, int *nchans, 1002355fec48SAndriy Voskoboinyk uint8_t ieee, uint16_t freq, int8_t maxregpower, uint32_t flags) 1003355fec48SAndriy Voskoboinyk { 1004355fec48SAndriy Voskoboinyk struct ieee80211_channel *c; 1005355fec48SAndriy Voskoboinyk 1006355fec48SAndriy Voskoboinyk if (*nchans >= maxchans) 1007355fec48SAndriy Voskoboinyk return (ENOBUFS); 1008355fec48SAndriy Voskoboinyk 1009355fec48SAndriy Voskoboinyk c = &chans[(*nchans)++]; 1010355fec48SAndriy Voskoboinyk c->ic_ieee = ieee; 1011355fec48SAndriy Voskoboinyk c->ic_freq = freq != 0 ? freq : ieee80211_ieee2mhz(ieee, flags); 1012355fec48SAndriy Voskoboinyk c->ic_maxregpower = maxregpower; 1013355fec48SAndriy Voskoboinyk c->ic_maxpower = 2 * maxregpower; 1014355fec48SAndriy Voskoboinyk c->ic_flags = flags; 1015355fec48SAndriy Voskoboinyk set_extchan(c); 1016355fec48SAndriy Voskoboinyk 1017355fec48SAndriy Voskoboinyk return (0); 1018355fec48SAndriy Voskoboinyk } 1019355fec48SAndriy Voskoboinyk 1020355fec48SAndriy Voskoboinyk static int 1021355fec48SAndriy Voskoboinyk copychan_prev(struct ieee80211_channel chans[], int maxchans, int *nchans, 1022355fec48SAndriy Voskoboinyk uint32_t flags) 1023355fec48SAndriy Voskoboinyk { 1024355fec48SAndriy Voskoboinyk struct ieee80211_channel *c; 1025355fec48SAndriy Voskoboinyk 1026355fec48SAndriy Voskoboinyk KASSERT(*nchans > 0, ("channel list is empty\n")); 1027355fec48SAndriy Voskoboinyk 1028355fec48SAndriy Voskoboinyk if (*nchans >= maxchans) 1029355fec48SAndriy Voskoboinyk return (ENOBUFS); 1030355fec48SAndriy Voskoboinyk 1031355fec48SAndriy Voskoboinyk c = &chans[(*nchans)++]; 1032355fec48SAndriy Voskoboinyk c[0] = c[-1]; 1033355fec48SAndriy Voskoboinyk c->ic_flags = flags; 1034355fec48SAndriy Voskoboinyk set_extchan(c); 1035355fec48SAndriy Voskoboinyk 1036355fec48SAndriy Voskoboinyk return (0); 1037355fec48SAndriy Voskoboinyk } 1038355fec48SAndriy Voskoboinyk 1039355fec48SAndriy Voskoboinyk static void 1040355fec48SAndriy Voskoboinyk getflags_2ghz(const uint8_t bands[], uint32_t flags[], int ht40) 1041355fec48SAndriy Voskoboinyk { 1042355fec48SAndriy Voskoboinyk int nmodes; 1043355fec48SAndriy Voskoboinyk 1044355fec48SAndriy Voskoboinyk nmodes = 0; 1045355fec48SAndriy Voskoboinyk if (isset(bands, IEEE80211_MODE_11B)) 1046355fec48SAndriy Voskoboinyk flags[nmodes++] = IEEE80211_CHAN_B; 1047355fec48SAndriy Voskoboinyk if (isset(bands, IEEE80211_MODE_11G)) 1048355fec48SAndriy Voskoboinyk flags[nmodes++] = IEEE80211_CHAN_G; 1049355fec48SAndriy Voskoboinyk if (isset(bands, IEEE80211_MODE_11NG)) 1050355fec48SAndriy Voskoboinyk flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20; 1051355fec48SAndriy Voskoboinyk if (ht40) { 1052355fec48SAndriy Voskoboinyk flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U; 1053355fec48SAndriy Voskoboinyk flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D; 1054355fec48SAndriy Voskoboinyk } 1055355fec48SAndriy Voskoboinyk flags[nmodes] = 0; 1056355fec48SAndriy Voskoboinyk } 1057355fec48SAndriy Voskoboinyk 1058355fec48SAndriy Voskoboinyk static void 1059355fec48SAndriy Voskoboinyk getflags_5ghz(const uint8_t bands[], uint32_t flags[], int ht40) 1060355fec48SAndriy Voskoboinyk { 1061355fec48SAndriy Voskoboinyk int nmodes; 1062355fec48SAndriy Voskoboinyk 1063355fec48SAndriy Voskoboinyk nmodes = 0; 1064355fec48SAndriy Voskoboinyk if (isset(bands, IEEE80211_MODE_11A)) 1065355fec48SAndriy Voskoboinyk flags[nmodes++] = IEEE80211_CHAN_A; 1066355fec48SAndriy Voskoboinyk if (isset(bands, IEEE80211_MODE_11NA)) 1067355fec48SAndriy Voskoboinyk flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20; 1068355fec48SAndriy Voskoboinyk if (ht40) { 1069355fec48SAndriy Voskoboinyk flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U; 1070355fec48SAndriy Voskoboinyk flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D; 1071355fec48SAndriy Voskoboinyk } 1072355fec48SAndriy Voskoboinyk flags[nmodes] = 0; 1073355fec48SAndriy Voskoboinyk } 1074355fec48SAndriy Voskoboinyk 1075355fec48SAndriy Voskoboinyk static void 1076355fec48SAndriy Voskoboinyk getflags(const uint8_t bands[], uint32_t flags[], int ht40) 1077355fec48SAndriy Voskoboinyk { 1078355fec48SAndriy Voskoboinyk 1079355fec48SAndriy Voskoboinyk flags[0] = 0; 1080355fec48SAndriy Voskoboinyk if (isset(bands, IEEE80211_MODE_11A) || 1081355fec48SAndriy Voskoboinyk isset(bands, IEEE80211_MODE_11NA)) { 1082355fec48SAndriy Voskoboinyk if (isset(bands, IEEE80211_MODE_11B) || 1083355fec48SAndriy Voskoboinyk isset(bands, IEEE80211_MODE_11G) || 1084355fec48SAndriy Voskoboinyk isset(bands, IEEE80211_MODE_11NG)) 1085355fec48SAndriy Voskoboinyk return; 1086355fec48SAndriy Voskoboinyk 1087355fec48SAndriy Voskoboinyk getflags_5ghz(bands, flags, ht40); 1088355fec48SAndriy Voskoboinyk } else 1089355fec48SAndriy Voskoboinyk getflags_2ghz(bands, flags, ht40); 1090355fec48SAndriy Voskoboinyk } 1091355fec48SAndriy Voskoboinyk 1092355fec48SAndriy Voskoboinyk /* 1093355fec48SAndriy Voskoboinyk * Add one 20 MHz channel into specified channel list. 1094355fec48SAndriy Voskoboinyk */ 1095355fec48SAndriy Voskoboinyk int 1096355fec48SAndriy Voskoboinyk ieee80211_add_channel(struct ieee80211_channel chans[], int maxchans, 1097355fec48SAndriy Voskoboinyk int *nchans, uint8_t ieee, uint16_t freq, int8_t maxregpower, 1098355fec48SAndriy Voskoboinyk uint32_t chan_flags, const uint8_t bands[]) 1099355fec48SAndriy Voskoboinyk { 1100355fec48SAndriy Voskoboinyk uint32_t flags[IEEE80211_MODE_MAX]; 1101355fec48SAndriy Voskoboinyk int i, error; 1102355fec48SAndriy Voskoboinyk 1103355fec48SAndriy Voskoboinyk getflags(bands, flags, 0); 1104355fec48SAndriy Voskoboinyk KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__)); 1105355fec48SAndriy Voskoboinyk 1106355fec48SAndriy Voskoboinyk error = addchan(chans, maxchans, nchans, ieee, freq, maxregpower, 1107355fec48SAndriy Voskoboinyk flags[0] | chan_flags); 1108355fec48SAndriy Voskoboinyk for (i = 1; flags[i] != 0 && error == 0; i++) { 1109355fec48SAndriy Voskoboinyk error = copychan_prev(chans, maxchans, nchans, 1110355fec48SAndriy Voskoboinyk flags[i] | chan_flags); 1111355fec48SAndriy Voskoboinyk } 1112355fec48SAndriy Voskoboinyk 1113355fec48SAndriy Voskoboinyk return (error); 1114355fec48SAndriy Voskoboinyk } 1115355fec48SAndriy Voskoboinyk 1116355fec48SAndriy Voskoboinyk static struct ieee80211_channel * 1117355fec48SAndriy Voskoboinyk findchannel(struct ieee80211_channel chans[], int nchans, uint16_t freq, 1118355fec48SAndriy Voskoboinyk uint32_t flags) 1119355fec48SAndriy Voskoboinyk { 1120355fec48SAndriy Voskoboinyk struct ieee80211_channel *c; 1121355fec48SAndriy Voskoboinyk int i; 1122355fec48SAndriy Voskoboinyk 1123355fec48SAndriy Voskoboinyk flags &= IEEE80211_CHAN_ALLTURBO; 1124355fec48SAndriy Voskoboinyk /* brute force search */ 1125355fec48SAndriy Voskoboinyk for (i = 0; i < nchans; i++) { 1126355fec48SAndriy Voskoboinyk c = &chans[i]; 1127355fec48SAndriy Voskoboinyk if (c->ic_freq == freq && 1128355fec48SAndriy Voskoboinyk (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 1129355fec48SAndriy Voskoboinyk return c; 1130355fec48SAndriy Voskoboinyk } 1131355fec48SAndriy Voskoboinyk return NULL; 1132355fec48SAndriy Voskoboinyk } 1133355fec48SAndriy Voskoboinyk 1134355fec48SAndriy Voskoboinyk /* 1135355fec48SAndriy Voskoboinyk * Add 40 MHz channel pair into specified channel list. 1136355fec48SAndriy Voskoboinyk */ 1137355fec48SAndriy Voskoboinyk int 1138355fec48SAndriy Voskoboinyk ieee80211_add_channel_ht40(struct ieee80211_channel chans[], int maxchans, 1139355fec48SAndriy Voskoboinyk int *nchans, uint8_t ieee, int8_t maxregpower, uint32_t flags) 1140355fec48SAndriy Voskoboinyk { 1141355fec48SAndriy Voskoboinyk struct ieee80211_channel *cent, *extc; 1142355fec48SAndriy Voskoboinyk uint16_t freq; 1143355fec48SAndriy Voskoboinyk int error; 1144355fec48SAndriy Voskoboinyk 1145355fec48SAndriy Voskoboinyk freq = ieee80211_ieee2mhz(ieee, flags); 1146355fec48SAndriy Voskoboinyk 1147355fec48SAndriy Voskoboinyk /* 1148355fec48SAndriy Voskoboinyk * Each entry defines an HT40 channel pair; find the 1149355fec48SAndriy Voskoboinyk * center channel, then the extension channel above. 1150355fec48SAndriy Voskoboinyk */ 1151355fec48SAndriy Voskoboinyk flags |= IEEE80211_CHAN_HT20; 1152355fec48SAndriy Voskoboinyk cent = findchannel(chans, *nchans, freq, flags); 1153355fec48SAndriy Voskoboinyk if (cent == NULL) 1154355fec48SAndriy Voskoboinyk return (EINVAL); 1155355fec48SAndriy Voskoboinyk 1156355fec48SAndriy Voskoboinyk extc = findchannel(chans, *nchans, freq + 20, flags); 1157355fec48SAndriy Voskoboinyk if (extc == NULL) 1158355fec48SAndriy Voskoboinyk return (ENOENT); 1159355fec48SAndriy Voskoboinyk 1160355fec48SAndriy Voskoboinyk flags &= ~IEEE80211_CHAN_HT; 1161355fec48SAndriy Voskoboinyk error = addchan(chans, maxchans, nchans, cent->ic_ieee, cent->ic_freq, 1162355fec48SAndriy Voskoboinyk maxregpower, flags | IEEE80211_CHAN_HT40U); 1163355fec48SAndriy Voskoboinyk if (error != 0) 1164355fec48SAndriy Voskoboinyk return (error); 1165355fec48SAndriy Voskoboinyk 1166355fec48SAndriy Voskoboinyk error = addchan(chans, maxchans, nchans, extc->ic_ieee, extc->ic_freq, 1167355fec48SAndriy Voskoboinyk maxregpower, flags | IEEE80211_CHAN_HT40D); 1168355fec48SAndriy Voskoboinyk 1169355fec48SAndriy Voskoboinyk return (error); 1170355fec48SAndriy Voskoboinyk } 1171355fec48SAndriy Voskoboinyk 1172355fec48SAndriy Voskoboinyk /* 1173*4774b999SAdrian Chadd * Fetch the center frequency for the primary channel. 1174*4774b999SAdrian Chadd */ 1175*4774b999SAdrian Chadd uint32_t 1176*4774b999SAdrian Chadd ieee80211_get_channel_center_freq(const struct ieee80211_channel *c) 1177*4774b999SAdrian Chadd { 1178*4774b999SAdrian Chadd 1179*4774b999SAdrian Chadd return (c->ic_freq); 1180*4774b999SAdrian Chadd } 1181*4774b999SAdrian Chadd 1182*4774b999SAdrian Chadd /* 1183*4774b999SAdrian Chadd * Fetch the center frequency for the primary BAND channel. 1184*4774b999SAdrian Chadd * 1185*4774b999SAdrian Chadd * For 5, 10, 20MHz channels it'll be the normally configured channel 1186*4774b999SAdrian Chadd * frequency. 1187*4774b999SAdrian Chadd * 1188*4774b999SAdrian Chadd * For 40MHz, 80MHz, 160Mhz channels it'll the the centre of the 1189*4774b999SAdrian Chadd * wide channel, not the centre of the primary channel (that's ic_freq). 1190*4774b999SAdrian Chadd * 1191*4774b999SAdrian Chadd * For 80+80MHz channels this will be the centre of the primary 1192*4774b999SAdrian Chadd * 80MHz channel; the secondary 80MHz channel will be center_freq2(). 1193*4774b999SAdrian Chadd */ 1194*4774b999SAdrian Chadd 1195*4774b999SAdrian Chadd uint32_t 1196*4774b999SAdrian Chadd ieee80211_get_channel_center_freq1(const struct ieee80211_channel *c) 1197*4774b999SAdrian Chadd { 1198*4774b999SAdrian Chadd 1199*4774b999SAdrian Chadd if (IEEE80211_IS_CHAN_HT40U(c)) { 1200*4774b999SAdrian Chadd return (c->ic_freq + 10); 1201*4774b999SAdrian Chadd } 1202*4774b999SAdrian Chadd if (IEEE80211_IS_CHAN_HT40D(c)) { 1203*4774b999SAdrian Chadd return (c->ic_freq - 10); 1204*4774b999SAdrian Chadd } 1205*4774b999SAdrian Chadd 1206*4774b999SAdrian Chadd return (c->ic_freq); 1207*4774b999SAdrian Chadd } 1208*4774b999SAdrian Chadd 1209*4774b999SAdrian Chadd /* 1210*4774b999SAdrian Chadd * For now, no 80+80 support; this is zero. 1211*4774b999SAdrian Chadd */ 1212*4774b999SAdrian Chadd uint32_t 1213*4774b999SAdrian Chadd ieee80211_get_channel_center_freq2(const struct ieee80211_channel *c) 1214*4774b999SAdrian Chadd { 1215*4774b999SAdrian Chadd 1216*4774b999SAdrian Chadd return (0); 1217*4774b999SAdrian Chadd } 1218*4774b999SAdrian Chadd 1219*4774b999SAdrian Chadd /* 1220355fec48SAndriy Voskoboinyk * Adds channels into specified channel list (ieee[] array must be sorted). 1221355fec48SAndriy Voskoboinyk * Channels are already sorted. 1222355fec48SAndriy Voskoboinyk */ 1223355fec48SAndriy Voskoboinyk static int 1224355fec48SAndriy Voskoboinyk add_chanlist(struct ieee80211_channel chans[], int maxchans, int *nchans, 1225355fec48SAndriy Voskoboinyk const uint8_t ieee[], int nieee, uint32_t flags[]) 1226355fec48SAndriy Voskoboinyk { 1227355fec48SAndriy Voskoboinyk uint16_t freq; 1228355fec48SAndriy Voskoboinyk int i, j, error; 1229355fec48SAndriy Voskoboinyk 1230355fec48SAndriy Voskoboinyk for (i = 0; i < nieee; i++) { 1231355fec48SAndriy Voskoboinyk freq = ieee80211_ieee2mhz(ieee[i], flags[0]); 1232355fec48SAndriy Voskoboinyk for (j = 0; flags[j] != 0; j++) { 1233355fec48SAndriy Voskoboinyk if (flags[j] & IEEE80211_CHAN_HT40D) 1234355fec48SAndriy Voskoboinyk if (i == 0 || ieee[i] < ieee[0] + 4 || 1235355fec48SAndriy Voskoboinyk freq - 20 != 1236355fec48SAndriy Voskoboinyk ieee80211_ieee2mhz(ieee[i] - 4, flags[j])) 1237355fec48SAndriy Voskoboinyk continue; 1238355fec48SAndriy Voskoboinyk if (flags[j] & IEEE80211_CHAN_HT40U) 1239355fec48SAndriy Voskoboinyk if (i == nieee - 1 || 1240355fec48SAndriy Voskoboinyk ieee[i] + 4 > ieee[nieee - 1] || 1241355fec48SAndriy Voskoboinyk freq + 20 != 1242355fec48SAndriy Voskoboinyk ieee80211_ieee2mhz(ieee[i] + 4, flags[j])) 1243355fec48SAndriy Voskoboinyk continue; 1244355fec48SAndriy Voskoboinyk 1245355fec48SAndriy Voskoboinyk if (j == 0) { 1246355fec48SAndriy Voskoboinyk error = addchan(chans, maxchans, nchans, 1247355fec48SAndriy Voskoboinyk ieee[i], freq, 0, flags[j]); 1248355fec48SAndriy Voskoboinyk } else { 1249355fec48SAndriy Voskoboinyk error = copychan_prev(chans, maxchans, nchans, 1250355fec48SAndriy Voskoboinyk flags[j]); 1251355fec48SAndriy Voskoboinyk } 1252355fec48SAndriy Voskoboinyk if (error != 0) 1253355fec48SAndriy Voskoboinyk return (error); 1254355fec48SAndriy Voskoboinyk } 1255355fec48SAndriy Voskoboinyk } 1256355fec48SAndriy Voskoboinyk 12576dbbec93SAndriy Voskoboinyk return (0); 1258355fec48SAndriy Voskoboinyk } 1259355fec48SAndriy Voskoboinyk 1260355fec48SAndriy Voskoboinyk int 1261355fec48SAndriy Voskoboinyk ieee80211_add_channel_list_2ghz(struct ieee80211_channel chans[], int maxchans, 1262355fec48SAndriy Voskoboinyk int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[], 1263355fec48SAndriy Voskoboinyk int ht40) 1264355fec48SAndriy Voskoboinyk { 1265355fec48SAndriy Voskoboinyk uint32_t flags[IEEE80211_MODE_MAX]; 1266355fec48SAndriy Voskoboinyk 1267355fec48SAndriy Voskoboinyk getflags_2ghz(bands, flags, ht40); 1268355fec48SAndriy Voskoboinyk KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__)); 1269355fec48SAndriy Voskoboinyk 1270355fec48SAndriy Voskoboinyk return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags)); 1271355fec48SAndriy Voskoboinyk } 1272355fec48SAndriy Voskoboinyk 1273355fec48SAndriy Voskoboinyk int 1274355fec48SAndriy Voskoboinyk ieee80211_add_channel_list_5ghz(struct ieee80211_channel chans[], int maxchans, 1275355fec48SAndriy Voskoboinyk int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[], 1276355fec48SAndriy Voskoboinyk int ht40) 1277355fec48SAndriy Voskoboinyk { 1278355fec48SAndriy Voskoboinyk uint32_t flags[IEEE80211_MODE_MAX]; 1279355fec48SAndriy Voskoboinyk 1280355fec48SAndriy Voskoboinyk getflags_5ghz(bands, flags, ht40); 1281355fec48SAndriy Voskoboinyk KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__)); 1282355fec48SAndriy Voskoboinyk 1283355fec48SAndriy Voskoboinyk return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags)); 1284355fec48SAndriy Voskoboinyk } 1285355fec48SAndriy Voskoboinyk 12861a1e1d21SSam Leffler /* 128768e8e04eSSam Leffler * Locate a channel given a frequency+flags. We cache 1288b032f27cSSam Leffler * the previous lookup to optimize switching between two 128968e8e04eSSam Leffler * channels--as happens with dynamic turbo. 129068e8e04eSSam Leffler */ 129168e8e04eSSam Leffler struct ieee80211_channel * 129268e8e04eSSam Leffler ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags) 129368e8e04eSSam Leffler { 129468e8e04eSSam Leffler struct ieee80211_channel *c; 129568e8e04eSSam Leffler 129668e8e04eSSam Leffler flags &= IEEE80211_CHAN_ALLTURBO; 129768e8e04eSSam Leffler c = ic->ic_prevchan; 129868e8e04eSSam Leffler if (c != NULL && c->ic_freq == freq && 129968e8e04eSSam Leffler (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 130068e8e04eSSam Leffler return c; 130168e8e04eSSam Leffler /* brute force search */ 1302355fec48SAndriy Voskoboinyk return (findchannel(ic->ic_channels, ic->ic_nchans, freq, flags)); 130368e8e04eSSam Leffler } 130468e8e04eSSam Leffler 1305a557c018SSam Leffler /* 1306a557c018SSam Leffler * Locate a channel given a channel number+flags. We cache 1307a557c018SSam Leffler * the previous lookup to optimize switching between two 1308a557c018SSam Leffler * channels--as happens with dynamic turbo. 1309a557c018SSam Leffler */ 1310a557c018SSam Leffler struct ieee80211_channel * 1311a557c018SSam Leffler ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags) 1312a557c018SSam Leffler { 1313a557c018SSam Leffler struct ieee80211_channel *c; 1314a557c018SSam Leffler int i; 1315a557c018SSam Leffler 1316a557c018SSam Leffler flags &= IEEE80211_CHAN_ALLTURBO; 1317a557c018SSam Leffler c = ic->ic_prevchan; 1318a557c018SSam Leffler if (c != NULL && c->ic_ieee == ieee && 1319a557c018SSam Leffler (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 1320a557c018SSam Leffler return c; 1321a557c018SSam Leffler /* brute force search */ 1322a557c018SSam Leffler for (i = 0; i < ic->ic_nchans; i++) { 1323a557c018SSam Leffler c = &ic->ic_channels[i]; 1324a557c018SSam Leffler if (c->ic_ieee == ieee && 1325a557c018SSam Leffler (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 1326a557c018SSam Leffler return c; 1327a557c018SSam Leffler } 1328a557c018SSam Leffler return NULL; 1329a557c018SSam Leffler } 1330a557c018SSam Leffler 1331c79f192cSAdrian Chadd /* 1332c79f192cSAdrian Chadd * Lookup a channel suitable for the given rx status. 1333c79f192cSAdrian Chadd * 1334c79f192cSAdrian Chadd * This is used to find a channel for a frame (eg beacon, probe 1335c79f192cSAdrian Chadd * response) based purely on the received PHY information. 1336c79f192cSAdrian Chadd * 1337c79f192cSAdrian Chadd * For now it tries to do it based on R_FREQ / R_IEEE. 1338c79f192cSAdrian Chadd * This is enough for 11bg and 11a (and thus 11ng/11na) 1339c79f192cSAdrian Chadd * but it will not be enough for GSM, PSB channels and the 1340c79f192cSAdrian Chadd * like. It also doesn't know about legacy-turbog and 1341c79f192cSAdrian Chadd * legacy-turbo modes, which some offload NICs actually 1342c79f192cSAdrian Chadd * support in weird ways. 1343c79f192cSAdrian Chadd * 1344c79f192cSAdrian Chadd * Takes the ic and rxstatus; returns the channel or NULL 1345c79f192cSAdrian Chadd * if not found. 1346c79f192cSAdrian Chadd * 1347c79f192cSAdrian Chadd * XXX TODO: Add support for that when the need arises. 1348c79f192cSAdrian Chadd */ 1349c79f192cSAdrian Chadd struct ieee80211_channel * 1350c79f192cSAdrian Chadd ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap, 1351c79f192cSAdrian Chadd const struct ieee80211_rx_stats *rxs) 1352c79f192cSAdrian Chadd { 1353c79f192cSAdrian Chadd struct ieee80211com *ic = vap->iv_ic; 1354c79f192cSAdrian Chadd uint32_t flags; 1355c79f192cSAdrian Chadd struct ieee80211_channel *c; 1356c79f192cSAdrian Chadd 1357c79f192cSAdrian Chadd if (rxs == NULL) 1358c79f192cSAdrian Chadd return (NULL); 1359c79f192cSAdrian Chadd 1360c79f192cSAdrian Chadd /* 1361c79f192cSAdrian Chadd * Strictly speaking we only use freq for now, 1362c79f192cSAdrian Chadd * however later on we may wish to just store 1363c79f192cSAdrian Chadd * the ieee for verification. 1364c79f192cSAdrian Chadd */ 1365c79f192cSAdrian Chadd if ((rxs->r_flags & IEEE80211_R_FREQ) == 0) 1366c79f192cSAdrian Chadd return (NULL); 1367c79f192cSAdrian Chadd if ((rxs->r_flags & IEEE80211_R_IEEE) == 0) 1368c79f192cSAdrian Chadd return (NULL); 1369c79f192cSAdrian Chadd 1370c79f192cSAdrian Chadd /* 1371c79f192cSAdrian Chadd * If the rx status contains a valid ieee/freq, then 1372c79f192cSAdrian Chadd * ensure we populate the correct channel information 1373c79f192cSAdrian Chadd * in rxchan before passing it up to the scan infrastructure. 1374c79f192cSAdrian Chadd * Offload NICs will pass up beacons from all channels 1375c79f192cSAdrian Chadd * during background scans. 1376c79f192cSAdrian Chadd */ 1377c79f192cSAdrian Chadd 1378c79f192cSAdrian Chadd /* Determine a band */ 1379c79f192cSAdrian Chadd /* XXX should be done by the driver? */ 1380c79f192cSAdrian Chadd if (rxs->c_freq < 3000) { 13812108f2a8SAdrian Chadd flags = IEEE80211_CHAN_G; 1382c79f192cSAdrian Chadd } else { 1383c79f192cSAdrian Chadd flags = IEEE80211_CHAN_A; 1384c79f192cSAdrian Chadd } 1385c79f192cSAdrian Chadd 1386c79f192cSAdrian Chadd /* Channel lookup */ 1387c79f192cSAdrian Chadd c = ieee80211_find_channel(ic, rxs->c_freq, flags); 1388c79f192cSAdrian Chadd 1389c79f192cSAdrian Chadd IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT, 1390c79f192cSAdrian Chadd "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n", 1391c79f192cSAdrian Chadd __func__, 1392c79f192cSAdrian Chadd (int) rxs->c_freq, 1393c79f192cSAdrian Chadd (int) rxs->c_ieee, 1394c79f192cSAdrian Chadd flags, 1395c79f192cSAdrian Chadd c); 1396c79f192cSAdrian Chadd 1397c79f192cSAdrian Chadd return (c); 1398c79f192cSAdrian Chadd } 1399c79f192cSAdrian Chadd 140068e8e04eSSam Leffler static void 1401b032f27cSSam Leffler addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword) 140268e8e04eSSam Leffler { 140368e8e04eSSam Leffler #define ADD(_ic, _s, _o) \ 1404b032f27cSSam Leffler ifmedia_add(media, \ 140568e8e04eSSam Leffler IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL) 140668e8e04eSSam Leffler static const u_int mopts[IEEE80211_MODE_MAX] = { 1407c3f10abdSSam Leffler [IEEE80211_MODE_AUTO] = IFM_AUTO, 1408c3f10abdSSam Leffler [IEEE80211_MODE_11A] = IFM_IEEE80211_11A, 1409c3f10abdSSam Leffler [IEEE80211_MODE_11B] = IFM_IEEE80211_11B, 1410c3f10abdSSam Leffler [IEEE80211_MODE_11G] = IFM_IEEE80211_11G, 1411c3f10abdSSam Leffler [IEEE80211_MODE_FH] = IFM_IEEE80211_FH, 1412c3f10abdSSam Leffler [IEEE80211_MODE_TURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO, 1413c3f10abdSSam Leffler [IEEE80211_MODE_TURBO_G] = IFM_IEEE80211_11G|IFM_IEEE80211_TURBO, 1414c3f10abdSSam Leffler [IEEE80211_MODE_STURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO, 14156a76ae21SSam Leffler [IEEE80211_MODE_HALF] = IFM_IEEE80211_11A, /* XXX */ 14166a76ae21SSam Leffler [IEEE80211_MODE_QUARTER] = IFM_IEEE80211_11A, /* XXX */ 1417c3f10abdSSam Leffler [IEEE80211_MODE_11NA] = IFM_IEEE80211_11NA, 1418c3f10abdSSam Leffler [IEEE80211_MODE_11NG] = IFM_IEEE80211_11NG, 141968e8e04eSSam Leffler }; 142068e8e04eSSam Leffler u_int mopt; 142168e8e04eSSam Leffler 142268e8e04eSSam Leffler mopt = mopts[mode]; 1423b032f27cSSam Leffler if (addsta) 1424b032f27cSSam Leffler ADD(ic, mword, mopt); /* STA mode has no cap */ 1425b032f27cSSam Leffler if (caps & IEEE80211_C_IBSS) 1426b032f27cSSam Leffler ADD(media, mword, mopt | IFM_IEEE80211_ADHOC); 1427b032f27cSSam Leffler if (caps & IEEE80211_C_HOSTAP) 1428b032f27cSSam Leffler ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP); 1429b032f27cSSam Leffler if (caps & IEEE80211_C_AHDEMO) 1430b032f27cSSam Leffler ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0); 1431b032f27cSSam Leffler if (caps & IEEE80211_C_MONITOR) 1432b032f27cSSam Leffler ADD(media, mword, mopt | IFM_IEEE80211_MONITOR); 1433b032f27cSSam Leffler if (caps & IEEE80211_C_WDS) 1434b032f27cSSam Leffler ADD(media, mword, mopt | IFM_IEEE80211_WDS); 143559aa14a9SRui Paulo if (caps & IEEE80211_C_MBSS) 143659aa14a9SRui Paulo ADD(media, mword, mopt | IFM_IEEE80211_MBSS); 143768e8e04eSSam Leffler #undef ADD 143868e8e04eSSam Leffler } 143968e8e04eSSam Leffler 144068e8e04eSSam Leffler /* 14411a1e1d21SSam Leffler * Setup the media data structures according to the channel and 1442b032f27cSSam Leffler * rate tables. 14431a1e1d21SSam Leffler */ 1444b032f27cSSam Leffler static int 1445b032f27cSSam Leffler ieee80211_media_setup(struct ieee80211com *ic, 1446b032f27cSSam Leffler struct ifmedia *media, int caps, int addsta, 14471a1e1d21SSam Leffler ifm_change_cb_t media_change, ifm_stat_cb_t media_stat) 14481a1e1d21SSam Leffler { 1449fcd9500fSBernhard Schmidt int i, j, rate, maxrate, mword, r; 1450fcd9500fSBernhard Schmidt enum ieee80211_phymode mode; 145168e8e04eSSam Leffler const struct ieee80211_rateset *rs; 14521a1e1d21SSam Leffler struct ieee80211_rateset allrates; 14531a1e1d21SSam Leffler 14542692bb26SSam Leffler /* 14551a1e1d21SSam Leffler * Fill in media characteristics. 14561a1e1d21SSam Leffler */ 1457b032f27cSSam Leffler ifmedia_init(media, 0, media_change, media_stat); 14581a1e1d21SSam Leffler maxrate = 0; 145968e8e04eSSam Leffler /* 146068e8e04eSSam Leffler * Add media for legacy operating modes. 146168e8e04eSSam Leffler */ 14621a1e1d21SSam Leffler memset(&allrates, 0, sizeof(allrates)); 146368e8e04eSSam Leffler for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) { 14646dbd16f1SSam Leffler if (isclr(ic->ic_modecaps, mode)) 14651a1e1d21SSam Leffler continue; 1466b032f27cSSam Leffler addmedia(media, caps, addsta, mode, IFM_AUTO); 14671a1e1d21SSam Leffler if (mode == IEEE80211_MODE_AUTO) 14681a1e1d21SSam Leffler continue; 14691a1e1d21SSam Leffler rs = &ic->ic_sup_rates[mode]; 14701a1e1d21SSam Leffler for (i = 0; i < rs->rs_nrates; i++) { 14711a1e1d21SSam Leffler rate = rs->rs_rates[i]; 14721a1e1d21SSam Leffler mword = ieee80211_rate2media(ic, rate, mode); 14731a1e1d21SSam Leffler if (mword == 0) 14741a1e1d21SSam Leffler continue; 1475b032f27cSSam Leffler addmedia(media, caps, addsta, mode, mword); 14761a1e1d21SSam Leffler /* 147768e8e04eSSam Leffler * Add legacy rate to the collection of all rates. 14781a1e1d21SSam Leffler */ 14791a1e1d21SSam Leffler r = rate & IEEE80211_RATE_VAL; 14801a1e1d21SSam Leffler for (j = 0; j < allrates.rs_nrates; j++) 14811a1e1d21SSam Leffler if (allrates.rs_rates[j] == r) 14821a1e1d21SSam Leffler break; 14831a1e1d21SSam Leffler if (j == allrates.rs_nrates) { 14841a1e1d21SSam Leffler /* unique, add to the set */ 14851a1e1d21SSam Leffler allrates.rs_rates[j] = r; 14861a1e1d21SSam Leffler allrates.rs_nrates++; 14871a1e1d21SSam Leffler } 14881a1e1d21SSam Leffler rate = (rate & IEEE80211_RATE_VAL) / 2; 14891a1e1d21SSam Leffler if (rate > maxrate) 14901a1e1d21SSam Leffler maxrate = rate; 14911a1e1d21SSam Leffler } 14921a1e1d21SSam Leffler } 14931a1e1d21SSam Leffler for (i = 0; i < allrates.rs_nrates; i++) { 14941a1e1d21SSam Leffler mword = ieee80211_rate2media(ic, allrates.rs_rates[i], 14951a1e1d21SSam Leffler IEEE80211_MODE_AUTO); 14961a1e1d21SSam Leffler if (mword == 0) 14971a1e1d21SSam Leffler continue; 149868e8e04eSSam Leffler /* NB: remove media options from mword */ 1499b032f27cSSam Leffler addmedia(media, caps, addsta, 1500b032f27cSSam Leffler IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword)); 15011a1e1d21SSam Leffler } 150268e8e04eSSam Leffler /* 150368e8e04eSSam Leffler * Add HT/11n media. Note that we do not have enough 150468e8e04eSSam Leffler * bits in the media subtype to express the MCS so we 150568e8e04eSSam Leffler * use a "placeholder" media subtype and any fixed MCS 150668e8e04eSSam Leffler * must be specified with a different mechanism. 150768e8e04eSSam Leffler */ 15086a76ae21SSam Leffler for (; mode <= IEEE80211_MODE_11NG; mode++) { 150968e8e04eSSam Leffler if (isclr(ic->ic_modecaps, mode)) 151068e8e04eSSam Leffler continue; 1511b032f27cSSam Leffler addmedia(media, caps, addsta, mode, IFM_AUTO); 1512b032f27cSSam Leffler addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS); 151368e8e04eSSam Leffler } 151468e8e04eSSam Leffler if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) || 151568e8e04eSSam Leffler isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) { 1516b032f27cSSam Leffler addmedia(media, caps, addsta, 1517b032f27cSSam Leffler IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS); 15186f897ba9SBernhard Schmidt i = ic->ic_txstream * 8 - 1; 15196f897ba9SBernhard Schmidt if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) && 15206f897ba9SBernhard Schmidt (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)) 15216f897ba9SBernhard Schmidt rate = ieee80211_htrates[i].ht40_rate_400ns; 15226f897ba9SBernhard Schmidt else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40)) 15236f897ba9SBernhard Schmidt rate = ieee80211_htrates[i].ht40_rate_800ns; 15246f897ba9SBernhard Schmidt else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20)) 15256f897ba9SBernhard Schmidt rate = ieee80211_htrates[i].ht20_rate_400ns; 15266f897ba9SBernhard Schmidt else 15276f897ba9SBernhard Schmidt rate = ieee80211_htrates[i].ht20_rate_800ns; 15286f897ba9SBernhard Schmidt if (rate > maxrate) 15296f897ba9SBernhard Schmidt maxrate = rate; 1530b032f27cSSam Leffler } 1531b032f27cSSam Leffler return maxrate; 153268e8e04eSSam Leffler } 153368e8e04eSSam Leffler 15346a76ae21SSam Leffler /* XXX inline or eliminate? */ 153541b3c790SSam Leffler const struct ieee80211_rateset * 153641b3c790SSam Leffler ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c) 153741b3c790SSam Leffler { 153840432d36SSam Leffler /* XXX does this work for 11ng basic rates? */ 153968e8e04eSSam Leffler return &ic->ic_sup_rates[ieee80211_chan2mode(c)]; 154041b3c790SSam Leffler } 154141b3c790SSam Leffler 15428a1b9b6aSSam Leffler void 15438a1b9b6aSSam Leffler ieee80211_announce(struct ieee80211com *ic) 15448a1b9b6aSSam Leffler { 1545fcd9500fSBernhard Schmidt int i, rate, mword; 1546fcd9500fSBernhard Schmidt enum ieee80211_phymode mode; 154768e8e04eSSam Leffler const struct ieee80211_rateset *rs; 15488a1b9b6aSSam Leffler 15497edb9e0aSSam Leffler /* NB: skip AUTO since it has no rates */ 15507edb9e0aSSam Leffler for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) { 15516dbd16f1SSam Leffler if (isclr(ic->ic_modecaps, mode)) 15528a1b9b6aSSam Leffler continue; 1553c8f5794eSGleb Smirnoff ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]); 15548a1b9b6aSSam Leffler rs = &ic->ic_sup_rates[mode]; 15558a1b9b6aSSam Leffler for (i = 0; i < rs->rs_nrates; i++) { 155668e8e04eSSam Leffler mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode); 15578a1b9b6aSSam Leffler if (mword == 0) 15588a1b9b6aSSam Leffler continue; 155968e8e04eSSam Leffler rate = ieee80211_media2rate(mword); 15608a1b9b6aSSam Leffler printf("%s%d%sMbps", (i != 0 ? " " : ""), 156168e8e04eSSam Leffler rate / 2, ((rate & 0x1) != 0 ? ".5" : "")); 15628a1b9b6aSSam Leffler } 15638a1b9b6aSSam Leffler printf("\n"); 15648a1b9b6aSSam Leffler } 156568e8e04eSSam Leffler ieee80211_ht_announce(ic); 15668a1b9b6aSSam Leffler } 15678a1b9b6aSSam Leffler 156868e8e04eSSam Leffler void 156968e8e04eSSam Leffler ieee80211_announce_channels(struct ieee80211com *ic) 15701a1e1d21SSam Leffler { 157168e8e04eSSam Leffler const struct ieee80211_channel *c; 157268e8e04eSSam Leffler char type; 157368e8e04eSSam Leffler int i, cw; 157468e8e04eSSam Leffler 157568e8e04eSSam Leffler printf("Chan Freq CW RegPwr MinPwr MaxPwr\n"); 157668e8e04eSSam Leffler for (i = 0; i < ic->ic_nchans; i++) { 157768e8e04eSSam Leffler c = &ic->ic_channels[i]; 157868e8e04eSSam Leffler if (IEEE80211_IS_CHAN_ST(c)) 157968e8e04eSSam Leffler type = 'S'; 158068e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_108A(c)) 158168e8e04eSSam Leffler type = 'T'; 158268e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_108G(c)) 158368e8e04eSSam Leffler type = 'G'; 158468e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_HT(c)) 158568e8e04eSSam Leffler type = 'n'; 158668e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_A(c)) 158768e8e04eSSam Leffler type = 'a'; 158868e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_ANYG(c)) 158968e8e04eSSam Leffler type = 'g'; 159068e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_B(c)) 159168e8e04eSSam Leffler type = 'b'; 159268e8e04eSSam Leffler else 159368e8e04eSSam Leffler type = 'f'; 159468e8e04eSSam Leffler if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c)) 159568e8e04eSSam Leffler cw = 40; 159668e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_HALF(c)) 159768e8e04eSSam Leffler cw = 10; 159868e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_QUARTER(c)) 159968e8e04eSSam Leffler cw = 5; 160068e8e04eSSam Leffler else 160168e8e04eSSam Leffler cw = 20; 160268e8e04eSSam Leffler printf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n" 160368e8e04eSSam Leffler , c->ic_ieee, c->ic_freq, type 160468e8e04eSSam Leffler , cw 160568e8e04eSSam Leffler , IEEE80211_IS_CHAN_HT40U(c) ? '+' : 160668e8e04eSSam Leffler IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' ' 160768e8e04eSSam Leffler , c->ic_maxregpower 160868e8e04eSSam Leffler , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0 160968e8e04eSSam Leffler , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0 161068e8e04eSSam Leffler ); 161168e8e04eSSam Leffler } 16121a1e1d21SSam Leffler } 16131a1e1d21SSam Leffler 161468e8e04eSSam Leffler static int 1615f945bd7aSSam Leffler media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode) 161668e8e04eSSam Leffler { 16171a1e1d21SSam Leffler switch (IFM_MODE(ime->ifm_media)) { 16181a1e1d21SSam Leffler case IFM_IEEE80211_11A: 1619b032f27cSSam Leffler *mode = IEEE80211_MODE_11A; 16201a1e1d21SSam Leffler break; 16211a1e1d21SSam Leffler case IFM_IEEE80211_11B: 1622b032f27cSSam Leffler *mode = IEEE80211_MODE_11B; 16231a1e1d21SSam Leffler break; 16241a1e1d21SSam Leffler case IFM_IEEE80211_11G: 1625b032f27cSSam Leffler *mode = IEEE80211_MODE_11G; 16261a1e1d21SSam Leffler break; 16274844aa7dSAtsushi Onoe case IFM_IEEE80211_FH: 1628b032f27cSSam Leffler *mode = IEEE80211_MODE_FH; 16294844aa7dSAtsushi Onoe break; 163068e8e04eSSam Leffler case IFM_IEEE80211_11NA: 1631b032f27cSSam Leffler *mode = IEEE80211_MODE_11NA; 163268e8e04eSSam Leffler break; 163368e8e04eSSam Leffler case IFM_IEEE80211_11NG: 1634b032f27cSSam Leffler *mode = IEEE80211_MODE_11NG; 163568e8e04eSSam Leffler break; 16361a1e1d21SSam Leffler case IFM_AUTO: 1637b032f27cSSam Leffler *mode = IEEE80211_MODE_AUTO; 16381a1e1d21SSam Leffler break; 16391a1e1d21SSam Leffler default: 1640b032f27cSSam Leffler return 0; 16411a1e1d21SSam Leffler } 16421a1e1d21SSam Leffler /* 16438a1b9b6aSSam Leffler * Turbo mode is an ``option''. 16448a1b9b6aSSam Leffler * XXX does not apply to AUTO 16451a1e1d21SSam Leffler */ 16461a1e1d21SSam Leffler if (ime->ifm_media & IFM_IEEE80211_TURBO) { 1647b032f27cSSam Leffler if (*mode == IEEE80211_MODE_11A) { 1648f945bd7aSSam Leffler if (flags & IEEE80211_F_TURBOP) 1649b032f27cSSam Leffler *mode = IEEE80211_MODE_TURBO_A; 165068e8e04eSSam Leffler else 1651b032f27cSSam Leffler *mode = IEEE80211_MODE_STURBO_A; 1652b032f27cSSam Leffler } else if (*mode == IEEE80211_MODE_11G) 1653b032f27cSSam Leffler *mode = IEEE80211_MODE_TURBO_G; 16548a1b9b6aSSam Leffler else 1655b032f27cSSam Leffler return 0; 16561a1e1d21SSam Leffler } 165768e8e04eSSam Leffler /* XXX HT40 +/- */ 1658b032f27cSSam Leffler return 1; 1659b032f27cSSam Leffler } 16601a1e1d21SSam Leffler 16611a1e1d21SSam Leffler /* 1662b032f27cSSam Leffler * Handle a media change request on the vap interface. 1663b032f27cSSam Leffler */ 1664b032f27cSSam Leffler int 1665b032f27cSSam Leffler ieee80211_media_change(struct ifnet *ifp) 1666b032f27cSSam Leffler { 1667b032f27cSSam Leffler struct ieee80211vap *vap = ifp->if_softc; 1668b032f27cSSam Leffler struct ifmedia_entry *ime = vap->iv_media.ifm_cur; 1669f945bd7aSSam Leffler uint16_t newmode; 1670b032f27cSSam Leffler 1671f945bd7aSSam Leffler if (!media2mode(ime, vap->iv_flags, &newmode)) 1672b032f27cSSam Leffler return EINVAL; 1673f945bd7aSSam Leffler if (vap->iv_des_mode != newmode) { 1674f945bd7aSSam Leffler vap->iv_des_mode = newmode; 16750a310468SSam Leffler /* XXX kick state machine if up+running */ 1676b032f27cSSam Leffler } 1677b032f27cSSam Leffler return 0; 1678b032f27cSSam Leffler } 1679b032f27cSSam Leffler 168068e8e04eSSam Leffler /* 168168e8e04eSSam Leffler * Common code to calculate the media status word 168268e8e04eSSam Leffler * from the operating mode and channel state. 168368e8e04eSSam Leffler */ 168468e8e04eSSam Leffler static int 168568e8e04eSSam Leffler media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan) 168668e8e04eSSam Leffler { 168768e8e04eSSam Leffler int status; 168868e8e04eSSam Leffler 168968e8e04eSSam Leffler status = IFM_IEEE80211; 169068e8e04eSSam Leffler switch (opmode) { 169168e8e04eSSam Leffler case IEEE80211_M_STA: 169268e8e04eSSam Leffler break; 169368e8e04eSSam Leffler case IEEE80211_M_IBSS: 169468e8e04eSSam Leffler status |= IFM_IEEE80211_ADHOC; 169568e8e04eSSam Leffler break; 169668e8e04eSSam Leffler case IEEE80211_M_HOSTAP: 169768e8e04eSSam Leffler status |= IFM_IEEE80211_HOSTAP; 169868e8e04eSSam Leffler break; 169968e8e04eSSam Leffler case IEEE80211_M_MONITOR: 170068e8e04eSSam Leffler status |= IFM_IEEE80211_MONITOR; 170168e8e04eSSam Leffler break; 170268e8e04eSSam Leffler case IEEE80211_M_AHDEMO: 170368e8e04eSSam Leffler status |= IFM_IEEE80211_ADHOC | IFM_FLAG0; 170468e8e04eSSam Leffler break; 170568e8e04eSSam Leffler case IEEE80211_M_WDS: 1706b032f27cSSam Leffler status |= IFM_IEEE80211_WDS; 170768e8e04eSSam Leffler break; 170859aa14a9SRui Paulo case IEEE80211_M_MBSS: 170959aa14a9SRui Paulo status |= IFM_IEEE80211_MBSS; 171059aa14a9SRui Paulo break; 171168e8e04eSSam Leffler } 171268e8e04eSSam Leffler if (IEEE80211_IS_CHAN_HTA(chan)) { 171368e8e04eSSam Leffler status |= IFM_IEEE80211_11NA; 171468e8e04eSSam Leffler } else if (IEEE80211_IS_CHAN_HTG(chan)) { 171568e8e04eSSam Leffler status |= IFM_IEEE80211_11NG; 171668e8e04eSSam Leffler } else if (IEEE80211_IS_CHAN_A(chan)) { 171768e8e04eSSam Leffler status |= IFM_IEEE80211_11A; 171868e8e04eSSam Leffler } else if (IEEE80211_IS_CHAN_B(chan)) { 171968e8e04eSSam Leffler status |= IFM_IEEE80211_11B; 172068e8e04eSSam Leffler } else if (IEEE80211_IS_CHAN_ANYG(chan)) { 172168e8e04eSSam Leffler status |= IFM_IEEE80211_11G; 172268e8e04eSSam Leffler } else if (IEEE80211_IS_CHAN_FHSS(chan)) { 172368e8e04eSSam Leffler status |= IFM_IEEE80211_FH; 172468e8e04eSSam Leffler } 172568e8e04eSSam Leffler /* XXX else complain? */ 172668e8e04eSSam Leffler 172768e8e04eSSam Leffler if (IEEE80211_IS_CHAN_TURBO(chan)) 172868e8e04eSSam Leffler status |= IFM_IEEE80211_TURBO; 1729b032f27cSSam Leffler #if 0 1730b032f27cSSam Leffler if (IEEE80211_IS_CHAN_HT20(chan)) 1731b032f27cSSam Leffler status |= IFM_IEEE80211_HT20; 1732b032f27cSSam Leffler if (IEEE80211_IS_CHAN_HT40(chan)) 1733b032f27cSSam Leffler status |= IFM_IEEE80211_HT40; 1734b032f27cSSam Leffler #endif 173568e8e04eSSam Leffler return status; 173668e8e04eSSam Leffler } 173768e8e04eSSam Leffler 17381a1e1d21SSam Leffler void 17391a1e1d21SSam Leffler ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr) 17401a1e1d21SSam Leffler { 1741b032f27cSSam Leffler struct ieee80211vap *vap = ifp->if_softc; 1742b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic; 174368e8e04eSSam Leffler enum ieee80211_phymode mode; 17441a1e1d21SSam Leffler 17451a1e1d21SSam Leffler imr->ifm_status = IFM_AVALID; 174668e8e04eSSam Leffler /* 174768e8e04eSSam Leffler * NB: use the current channel's mode to lock down a xmit 174868e8e04eSSam Leffler * rate only when running; otherwise we may have a mismatch 174968e8e04eSSam Leffler * in which case the rate will not be convertible. 175068e8e04eSSam Leffler */ 17519f098ac7SAdrian Chadd if (vap->iv_state == IEEE80211_S_RUN || 17529f098ac7SAdrian Chadd vap->iv_state == IEEE80211_S_SLEEP) { 17531a1e1d21SSam Leffler imr->ifm_status |= IFM_ACTIVE; 175468e8e04eSSam Leffler mode = ieee80211_chan2mode(ic->ic_curchan); 175568e8e04eSSam Leffler } else 175668e8e04eSSam Leffler mode = IEEE80211_MODE_AUTO; 1757b032f27cSSam Leffler imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan); 17588a1b9b6aSSam Leffler /* 17598a1b9b6aSSam Leffler * Calculate a current rate if possible. 17608a1b9b6aSSam Leffler */ 1761b032f27cSSam Leffler if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) { 17628a1b9b6aSSam Leffler /* 17638a1b9b6aSSam Leffler * A fixed rate is set, report that. 17648a1b9b6aSSam Leffler */ 17658a1b9b6aSSam Leffler imr->ifm_active |= ieee80211_rate2media(ic, 1766b032f27cSSam Leffler vap->iv_txparms[mode].ucastrate, mode); 1767b032f27cSSam Leffler } else if (vap->iv_opmode == IEEE80211_M_STA) { 17688a1b9b6aSSam Leffler /* 17698a1b9b6aSSam Leffler * In station mode report the current transmit rate. 17708a1b9b6aSSam Leffler */ 17718a1b9b6aSSam Leffler imr->ifm_active |= ieee80211_rate2media(ic, 1772b032f27cSSam Leffler vap->iv_bss->ni_txrate, mode); 1773ba99a9b1SAndre Oppermann } else 17741a1e1d21SSam Leffler imr->ifm_active |= IFM_AUTO; 1775b032f27cSSam Leffler if (imr->ifm_status & IFM_ACTIVE) 1776b032f27cSSam Leffler imr->ifm_current = imr->ifm_active; 17771a1e1d21SSam Leffler } 17781a1e1d21SSam Leffler 17791a1e1d21SSam Leffler /* 17801a1e1d21SSam Leffler * Set the current phy mode and recalculate the active channel 17811a1e1d21SSam Leffler * set based on the available channels for this mode. Also 17821a1e1d21SSam Leffler * select a new default/current channel if the current one is 17831a1e1d21SSam Leffler * inappropriate for this mode. 17841a1e1d21SSam Leffler */ 17851a1e1d21SSam Leffler int 17861a1e1d21SSam Leffler ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode) 17871a1e1d21SSam Leffler { 17881a1e1d21SSam Leffler /* 1789ca4ac7aeSSam Leffler * Adjust basic rates in 11b/11g supported rate set. 1790ca4ac7aeSSam Leffler * Note that if operating on a hal/quarter rate channel 1791ca4ac7aeSSam Leffler * this is a noop as those rates sets are different 1792ca4ac7aeSSam Leffler * and used instead. 17931a1e1d21SSam Leffler */ 1794ca4ac7aeSSam Leffler if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B) 1795b032f27cSSam Leffler ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode); 1796ca4ac7aeSSam Leffler 17971a1e1d21SSam Leffler ic->ic_curmode = mode; 17988a1b9b6aSSam Leffler ieee80211_reset_erp(ic); /* reset ERP state */ 17998a1b9b6aSSam Leffler 18001a1e1d21SSam Leffler return 0; 18011a1e1d21SSam Leffler } 18021a1e1d21SSam Leffler 18031a1e1d21SSam Leffler /* 180468e8e04eSSam Leffler * Return the phy mode for with the specified channel. 18051a1e1d21SSam Leffler */ 18061a1e1d21SSam Leffler enum ieee80211_phymode 180768e8e04eSSam Leffler ieee80211_chan2mode(const struct ieee80211_channel *chan) 18081a1e1d21SSam Leffler { 180968e8e04eSSam Leffler 181068e8e04eSSam Leffler if (IEEE80211_IS_CHAN_HTA(chan)) 181168e8e04eSSam Leffler return IEEE80211_MODE_11NA; 181268e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_HTG(chan)) 181368e8e04eSSam Leffler return IEEE80211_MODE_11NG; 181468e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_108G(chan)) 18158a1b9b6aSSam Leffler return IEEE80211_MODE_TURBO_G; 181668e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_ST(chan)) 181768e8e04eSSam Leffler return IEEE80211_MODE_STURBO_A; 181868e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_TURBO(chan)) 181968e8e04eSSam Leffler return IEEE80211_MODE_TURBO_A; 18206a76ae21SSam Leffler else if (IEEE80211_IS_CHAN_HALF(chan)) 18216a76ae21SSam Leffler return IEEE80211_MODE_HALF; 18226a76ae21SSam Leffler else if (IEEE80211_IS_CHAN_QUARTER(chan)) 18236a76ae21SSam Leffler return IEEE80211_MODE_QUARTER; 182468e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_A(chan)) 182568e8e04eSSam Leffler return IEEE80211_MODE_11A; 182668e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_ANYG(chan)) 18271a1e1d21SSam Leffler return IEEE80211_MODE_11G; 182868e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_B(chan)) 182968e8e04eSSam Leffler return IEEE80211_MODE_11B; 183068e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_FHSS(chan)) 183168e8e04eSSam Leffler return IEEE80211_MODE_FH; 183268e8e04eSSam Leffler 183368e8e04eSSam Leffler /* NB: should not get here */ 183468e8e04eSSam Leffler printf("%s: cannot map channel to mode; freq %u flags 0x%x\n", 183568e8e04eSSam Leffler __func__, chan->ic_freq, chan->ic_flags); 18361a1e1d21SSam Leffler return IEEE80211_MODE_11B; 18371a1e1d21SSam Leffler } 18381a1e1d21SSam Leffler 183968e8e04eSSam Leffler struct ratemedia { 184068e8e04eSSam Leffler u_int match; /* rate + mode */ 184168e8e04eSSam Leffler u_int media; /* if_media rate */ 184268e8e04eSSam Leffler }; 184368e8e04eSSam Leffler 184468e8e04eSSam Leffler static int 184568e8e04eSSam Leffler findmedia(const struct ratemedia rates[], int n, u_int match) 184668e8e04eSSam Leffler { 184768e8e04eSSam Leffler int i; 184868e8e04eSSam Leffler 184968e8e04eSSam Leffler for (i = 0; i < n; i++) 185068e8e04eSSam Leffler if (rates[i].match == match) 185168e8e04eSSam Leffler return rates[i].media; 185268e8e04eSSam Leffler return IFM_AUTO; 185368e8e04eSSam Leffler } 185468e8e04eSSam Leffler 18551a1e1d21SSam Leffler /* 185668e8e04eSSam Leffler * Convert IEEE80211 rate value to ifmedia subtype. 185768e8e04eSSam Leffler * Rate is either a legacy rate in units of 0.5Mbps 185868e8e04eSSam Leffler * or an MCS index. 18591a1e1d21SSam Leffler */ 18601a1e1d21SSam Leffler int 18611a1e1d21SSam Leffler ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode) 18621a1e1d21SSam Leffler { 186368e8e04eSSam Leffler static const struct ratemedia rates[] = { 18644844aa7dSAtsushi Onoe { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 }, 18654844aa7dSAtsushi Onoe { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 }, 18664844aa7dSAtsushi Onoe { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 }, 18674844aa7dSAtsushi Onoe { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 }, 18684844aa7dSAtsushi Onoe { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 }, 18694844aa7dSAtsushi Onoe { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 }, 18704844aa7dSAtsushi Onoe { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 }, 18714844aa7dSAtsushi Onoe { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 }, 18724844aa7dSAtsushi Onoe { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 }, 18734844aa7dSAtsushi Onoe { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 }, 18744844aa7dSAtsushi Onoe { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 }, 18754844aa7dSAtsushi Onoe { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 }, 18764844aa7dSAtsushi Onoe { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 }, 18774844aa7dSAtsushi Onoe { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 }, 18784844aa7dSAtsushi Onoe { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 }, 18794844aa7dSAtsushi Onoe { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 }, 18804844aa7dSAtsushi Onoe { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 }, 18814844aa7dSAtsushi Onoe { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 }, 18824844aa7dSAtsushi Onoe { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 }, 18834844aa7dSAtsushi Onoe { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 }, 18844844aa7dSAtsushi Onoe { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 }, 18854844aa7dSAtsushi Onoe { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 }, 18864844aa7dSAtsushi Onoe { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 }, 18874844aa7dSAtsushi Onoe { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 }, 18884844aa7dSAtsushi Onoe { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 }, 18894844aa7dSAtsushi Onoe { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 }, 18904844aa7dSAtsushi Onoe { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 }, 189141b3c790SSam Leffler { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 }, 189241b3c790SSam Leffler { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 }, 189341b3c790SSam Leffler { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 }, 1894a4641f4eSPedro F. Giffuni /* NB: OFDM72 doesn't really exist so we don't handle it */ 18951a1e1d21SSam Leffler }; 189668e8e04eSSam Leffler static const struct ratemedia htrates[] = { 189768e8e04eSSam Leffler { 0, IFM_IEEE80211_MCS }, 189868e8e04eSSam Leffler { 1, IFM_IEEE80211_MCS }, 189968e8e04eSSam Leffler { 2, IFM_IEEE80211_MCS }, 190068e8e04eSSam Leffler { 3, IFM_IEEE80211_MCS }, 190168e8e04eSSam Leffler { 4, IFM_IEEE80211_MCS }, 190268e8e04eSSam Leffler { 5, IFM_IEEE80211_MCS }, 190368e8e04eSSam Leffler { 6, IFM_IEEE80211_MCS }, 190468e8e04eSSam Leffler { 7, IFM_IEEE80211_MCS }, 190568e8e04eSSam Leffler { 8, IFM_IEEE80211_MCS }, 190668e8e04eSSam Leffler { 9, IFM_IEEE80211_MCS }, 190768e8e04eSSam Leffler { 10, IFM_IEEE80211_MCS }, 190868e8e04eSSam Leffler { 11, IFM_IEEE80211_MCS }, 190968e8e04eSSam Leffler { 12, IFM_IEEE80211_MCS }, 191068e8e04eSSam Leffler { 13, IFM_IEEE80211_MCS }, 191168e8e04eSSam Leffler { 14, IFM_IEEE80211_MCS }, 191268e8e04eSSam Leffler { 15, IFM_IEEE80211_MCS }, 1913f136f45fSBernhard Schmidt { 16, IFM_IEEE80211_MCS }, 1914f136f45fSBernhard Schmidt { 17, IFM_IEEE80211_MCS }, 1915f136f45fSBernhard Schmidt { 18, IFM_IEEE80211_MCS }, 1916f136f45fSBernhard Schmidt { 19, IFM_IEEE80211_MCS }, 1917f136f45fSBernhard Schmidt { 20, IFM_IEEE80211_MCS }, 1918f136f45fSBernhard Schmidt { 21, IFM_IEEE80211_MCS }, 1919f136f45fSBernhard Schmidt { 22, IFM_IEEE80211_MCS }, 1920f136f45fSBernhard Schmidt { 23, IFM_IEEE80211_MCS }, 1921f136f45fSBernhard Schmidt { 24, IFM_IEEE80211_MCS }, 1922f136f45fSBernhard Schmidt { 25, IFM_IEEE80211_MCS }, 1923f136f45fSBernhard Schmidt { 26, IFM_IEEE80211_MCS }, 1924f136f45fSBernhard Schmidt { 27, IFM_IEEE80211_MCS }, 1925f136f45fSBernhard Schmidt { 28, IFM_IEEE80211_MCS }, 1926f136f45fSBernhard Schmidt { 29, IFM_IEEE80211_MCS }, 1927f136f45fSBernhard Schmidt { 30, IFM_IEEE80211_MCS }, 1928f136f45fSBernhard Schmidt { 31, IFM_IEEE80211_MCS }, 1929f136f45fSBernhard Schmidt { 32, IFM_IEEE80211_MCS }, 1930f136f45fSBernhard Schmidt { 33, IFM_IEEE80211_MCS }, 1931f136f45fSBernhard Schmidt { 34, IFM_IEEE80211_MCS }, 1932f136f45fSBernhard Schmidt { 35, IFM_IEEE80211_MCS }, 1933f136f45fSBernhard Schmidt { 36, IFM_IEEE80211_MCS }, 1934f136f45fSBernhard Schmidt { 37, IFM_IEEE80211_MCS }, 1935f136f45fSBernhard Schmidt { 38, IFM_IEEE80211_MCS }, 1936f136f45fSBernhard Schmidt { 39, IFM_IEEE80211_MCS }, 1937f136f45fSBernhard Schmidt { 40, IFM_IEEE80211_MCS }, 1938f136f45fSBernhard Schmidt { 41, IFM_IEEE80211_MCS }, 1939f136f45fSBernhard Schmidt { 42, IFM_IEEE80211_MCS }, 1940f136f45fSBernhard Schmidt { 43, IFM_IEEE80211_MCS }, 1941f136f45fSBernhard Schmidt { 44, IFM_IEEE80211_MCS }, 1942f136f45fSBernhard Schmidt { 45, IFM_IEEE80211_MCS }, 1943f136f45fSBernhard Schmidt { 46, IFM_IEEE80211_MCS }, 1944f136f45fSBernhard Schmidt { 47, IFM_IEEE80211_MCS }, 1945f136f45fSBernhard Schmidt { 48, IFM_IEEE80211_MCS }, 1946f136f45fSBernhard Schmidt { 49, IFM_IEEE80211_MCS }, 1947f136f45fSBernhard Schmidt { 50, IFM_IEEE80211_MCS }, 1948f136f45fSBernhard Schmidt { 51, IFM_IEEE80211_MCS }, 1949f136f45fSBernhard Schmidt { 52, IFM_IEEE80211_MCS }, 1950f136f45fSBernhard Schmidt { 53, IFM_IEEE80211_MCS }, 1951f136f45fSBernhard Schmidt { 54, IFM_IEEE80211_MCS }, 1952f136f45fSBernhard Schmidt { 55, IFM_IEEE80211_MCS }, 1953f136f45fSBernhard Schmidt { 56, IFM_IEEE80211_MCS }, 1954f136f45fSBernhard Schmidt { 57, IFM_IEEE80211_MCS }, 1955f136f45fSBernhard Schmidt { 58, IFM_IEEE80211_MCS }, 1956f136f45fSBernhard Schmidt { 59, IFM_IEEE80211_MCS }, 1957f136f45fSBernhard Schmidt { 60, IFM_IEEE80211_MCS }, 1958f136f45fSBernhard Schmidt { 61, IFM_IEEE80211_MCS }, 1959f136f45fSBernhard Schmidt { 62, IFM_IEEE80211_MCS }, 1960f136f45fSBernhard Schmidt { 63, IFM_IEEE80211_MCS }, 1961f136f45fSBernhard Schmidt { 64, IFM_IEEE80211_MCS }, 1962f136f45fSBernhard Schmidt { 65, IFM_IEEE80211_MCS }, 1963f136f45fSBernhard Schmidt { 66, IFM_IEEE80211_MCS }, 1964f136f45fSBernhard Schmidt { 67, IFM_IEEE80211_MCS }, 1965f136f45fSBernhard Schmidt { 68, IFM_IEEE80211_MCS }, 1966f136f45fSBernhard Schmidt { 69, IFM_IEEE80211_MCS }, 1967f136f45fSBernhard Schmidt { 70, IFM_IEEE80211_MCS }, 1968f136f45fSBernhard Schmidt { 71, IFM_IEEE80211_MCS }, 1969f136f45fSBernhard Schmidt { 72, IFM_IEEE80211_MCS }, 1970f136f45fSBernhard Schmidt { 73, IFM_IEEE80211_MCS }, 1971f136f45fSBernhard Schmidt { 74, IFM_IEEE80211_MCS }, 1972f136f45fSBernhard Schmidt { 75, IFM_IEEE80211_MCS }, 1973f136f45fSBernhard Schmidt { 76, IFM_IEEE80211_MCS }, 197468e8e04eSSam Leffler }; 197568e8e04eSSam Leffler int m; 19761a1e1d21SSam Leffler 197768e8e04eSSam Leffler /* 197868e8e04eSSam Leffler * Check 11n rates first for match as an MCS. 197968e8e04eSSam Leffler */ 198068e8e04eSSam Leffler if (mode == IEEE80211_MODE_11NA) { 1981f0ee92d5SSam Leffler if (rate & IEEE80211_RATE_MCS) { 1982f0ee92d5SSam Leffler rate &= ~IEEE80211_RATE_MCS; 1983a3e08d6fSRui Paulo m = findmedia(htrates, nitems(htrates), rate); 198468e8e04eSSam Leffler if (m != IFM_AUTO) 198568e8e04eSSam Leffler return m | IFM_IEEE80211_11NA; 198668e8e04eSSam Leffler } 198768e8e04eSSam Leffler } else if (mode == IEEE80211_MODE_11NG) { 198868e8e04eSSam Leffler /* NB: 12 is ambiguous, it will be treated as an MCS */ 1989f0ee92d5SSam Leffler if (rate & IEEE80211_RATE_MCS) { 1990f0ee92d5SSam Leffler rate &= ~IEEE80211_RATE_MCS; 1991a3e08d6fSRui Paulo m = findmedia(htrates, nitems(htrates), rate); 199268e8e04eSSam Leffler if (m != IFM_AUTO) 199368e8e04eSSam Leffler return m | IFM_IEEE80211_11NG; 199468e8e04eSSam Leffler } 199568e8e04eSSam Leffler } 199668e8e04eSSam Leffler rate &= IEEE80211_RATE_VAL; 19971a1e1d21SSam Leffler switch (mode) { 19981a1e1d21SSam Leffler case IEEE80211_MODE_11A: 19996a76ae21SSam Leffler case IEEE80211_MODE_HALF: /* XXX good 'nuf */ 20006a76ae21SSam Leffler case IEEE80211_MODE_QUARTER: 200168e8e04eSSam Leffler case IEEE80211_MODE_11NA: 20028a1b9b6aSSam Leffler case IEEE80211_MODE_TURBO_A: 200368e8e04eSSam Leffler case IEEE80211_MODE_STURBO_A: 2004a3e08d6fSRui Paulo return findmedia(rates, nitems(rates), 2005a3e08d6fSRui Paulo rate | IFM_IEEE80211_11A); 20061a1e1d21SSam Leffler case IEEE80211_MODE_11B: 2007a3e08d6fSRui Paulo return findmedia(rates, nitems(rates), 2008a3e08d6fSRui Paulo rate | IFM_IEEE80211_11B); 20094844aa7dSAtsushi Onoe case IEEE80211_MODE_FH: 2010a3e08d6fSRui Paulo return findmedia(rates, nitems(rates), 2011a3e08d6fSRui Paulo rate | IFM_IEEE80211_FH); 20121a1e1d21SSam Leffler case IEEE80211_MODE_AUTO: 20131a1e1d21SSam Leffler /* NB: ic may be NULL for some drivers */ 2014566d825bSSam Leffler if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH) 2015a3e08d6fSRui Paulo return findmedia(rates, nitems(rates), 201668e8e04eSSam Leffler rate | IFM_IEEE80211_FH); 20171a1e1d21SSam Leffler /* NB: hack, 11g matches both 11b+11a rates */ 20181a1e1d21SSam Leffler /* fall thru... */ 20191a1e1d21SSam Leffler case IEEE80211_MODE_11G: 202068e8e04eSSam Leffler case IEEE80211_MODE_11NG: 20218a1b9b6aSSam Leffler case IEEE80211_MODE_TURBO_G: 2022a3e08d6fSRui Paulo return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G); 20231a1e1d21SSam Leffler } 20241a1e1d21SSam Leffler return IFM_AUTO; 20251a1e1d21SSam Leffler } 20261a1e1d21SSam Leffler 20271a1e1d21SSam Leffler int 20281a1e1d21SSam Leffler ieee80211_media2rate(int mword) 20291a1e1d21SSam Leffler { 20301a1e1d21SSam Leffler static const int ieeerates[] = { 20311a1e1d21SSam Leffler -1, /* IFM_AUTO */ 20321a1e1d21SSam Leffler 0, /* IFM_MANUAL */ 20331a1e1d21SSam Leffler 0, /* IFM_NONE */ 20341a1e1d21SSam Leffler 2, /* IFM_IEEE80211_FH1 */ 20351a1e1d21SSam Leffler 4, /* IFM_IEEE80211_FH2 */ 20361a1e1d21SSam Leffler 2, /* IFM_IEEE80211_DS1 */ 20371a1e1d21SSam Leffler 4, /* IFM_IEEE80211_DS2 */ 20381a1e1d21SSam Leffler 11, /* IFM_IEEE80211_DS5 */ 20391a1e1d21SSam Leffler 22, /* IFM_IEEE80211_DS11 */ 20401a1e1d21SSam Leffler 44, /* IFM_IEEE80211_DS22 */ 20411a1e1d21SSam Leffler 12, /* IFM_IEEE80211_OFDM6 */ 20421a1e1d21SSam Leffler 18, /* IFM_IEEE80211_OFDM9 */ 20431a1e1d21SSam Leffler 24, /* IFM_IEEE80211_OFDM12 */ 20441a1e1d21SSam Leffler 36, /* IFM_IEEE80211_OFDM18 */ 20451a1e1d21SSam Leffler 48, /* IFM_IEEE80211_OFDM24 */ 20461a1e1d21SSam Leffler 72, /* IFM_IEEE80211_OFDM36 */ 20471a1e1d21SSam Leffler 96, /* IFM_IEEE80211_OFDM48 */ 20481a1e1d21SSam Leffler 108, /* IFM_IEEE80211_OFDM54 */ 20491a1e1d21SSam Leffler 144, /* IFM_IEEE80211_OFDM72 */ 205041b3c790SSam Leffler 0, /* IFM_IEEE80211_DS354k */ 205141b3c790SSam Leffler 0, /* IFM_IEEE80211_DS512k */ 205241b3c790SSam Leffler 6, /* IFM_IEEE80211_OFDM3 */ 205341b3c790SSam Leffler 9, /* IFM_IEEE80211_OFDM4 */ 205441b3c790SSam Leffler 54, /* IFM_IEEE80211_OFDM27 */ 205568e8e04eSSam Leffler -1, /* IFM_IEEE80211_MCS */ 20561a1e1d21SSam Leffler }; 2057a3e08d6fSRui Paulo return IFM_SUBTYPE(mword) < nitems(ieeerates) ? 20581a1e1d21SSam Leffler ieeerates[IFM_SUBTYPE(mword)] : 0; 20591a1e1d21SSam Leffler } 20605b16c28cSSam Leffler 20615b16c28cSSam Leffler /* 20625b16c28cSSam Leffler * The following hash function is adapted from "Hash Functions" by Bob Jenkins 20635b16c28cSSam Leffler * ("Algorithm Alley", Dr. Dobbs Journal, September 1997). 20645b16c28cSSam Leffler */ 20655b16c28cSSam Leffler #define mix(a, b, c) \ 20665b16c28cSSam Leffler do { \ 20675b16c28cSSam Leffler a -= b; a -= c; a ^= (c >> 13); \ 20685b16c28cSSam Leffler b -= c; b -= a; b ^= (a << 8); \ 20695b16c28cSSam Leffler c -= a; c -= b; c ^= (b >> 13); \ 20705b16c28cSSam Leffler a -= b; a -= c; a ^= (c >> 12); \ 20715b16c28cSSam Leffler b -= c; b -= a; b ^= (a << 16); \ 20725b16c28cSSam Leffler c -= a; c -= b; c ^= (b >> 5); \ 20735b16c28cSSam Leffler a -= b; a -= c; a ^= (c >> 3); \ 20745b16c28cSSam Leffler b -= c; b -= a; b ^= (a << 10); \ 20755b16c28cSSam Leffler c -= a; c -= b; c ^= (b >> 15); \ 20765b16c28cSSam Leffler } while (/*CONSTCOND*/0) 20775b16c28cSSam Leffler 20785b16c28cSSam Leffler uint32_t 20795b16c28cSSam Leffler ieee80211_mac_hash(const struct ieee80211com *ic, 20805b16c28cSSam Leffler const uint8_t addr[IEEE80211_ADDR_LEN]) 20815b16c28cSSam Leffler { 20825b16c28cSSam Leffler uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key; 20835b16c28cSSam Leffler 20845b16c28cSSam Leffler b += addr[5] << 8; 20855b16c28cSSam Leffler b += addr[4]; 20865b16c28cSSam Leffler a += addr[3] << 24; 20875b16c28cSSam Leffler a += addr[2] << 16; 20885b16c28cSSam Leffler a += addr[1] << 8; 20895b16c28cSSam Leffler a += addr[0]; 20905b16c28cSSam Leffler 20915b16c28cSSam Leffler mix(a, b, c); 20925b16c28cSSam Leffler 20935b16c28cSSam Leffler return c; 20945b16c28cSSam Leffler } 20955b16c28cSSam Leffler #undef mix 2096a1cbd043SAdrian Chadd 2097a1cbd043SAdrian Chadd char 2098a1cbd043SAdrian Chadd ieee80211_channel_type_char(const struct ieee80211_channel *c) 2099a1cbd043SAdrian Chadd { 2100a1cbd043SAdrian Chadd if (IEEE80211_IS_CHAN_ST(c)) 2101a1cbd043SAdrian Chadd return 'S'; 2102a1cbd043SAdrian Chadd if (IEEE80211_IS_CHAN_108A(c)) 2103a1cbd043SAdrian Chadd return 'T'; 2104a1cbd043SAdrian Chadd if (IEEE80211_IS_CHAN_108G(c)) 2105a1cbd043SAdrian Chadd return 'G'; 2106a1cbd043SAdrian Chadd if (IEEE80211_IS_CHAN_HT(c)) 2107a1cbd043SAdrian Chadd return 'n'; 2108a1cbd043SAdrian Chadd if (IEEE80211_IS_CHAN_A(c)) 2109a1cbd043SAdrian Chadd return 'a'; 2110a1cbd043SAdrian Chadd if (IEEE80211_IS_CHAN_ANYG(c)) 2111a1cbd043SAdrian Chadd return 'g'; 2112a1cbd043SAdrian Chadd if (IEEE80211_IS_CHAN_B(c)) 2113a1cbd043SAdrian Chadd return 'b'; 2114a1cbd043SAdrian Chadd return 'f'; 2115a1cbd043SAdrian Chadd } 2116