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 409b032f27cSSam Leffler /* 410b032f27cSSam Leffler * Default reset method for use with the ioctl support. This 411b032f27cSSam Leffler * method is invoked after any state change in the 802.11 412b032f27cSSam Leffler * layer that should be propagated to the hardware but not 413b032f27cSSam Leffler * require re-initialization of the 802.11 state machine (e.g 414b032f27cSSam Leffler * rescanning for an ap). We always return ENETRESET which 415b032f27cSSam Leffler * should cause the driver to re-initialize the device. Drivers 416b032f27cSSam Leffler * can override this method to implement more optimized support. 417b032f27cSSam Leffler */ 418b032f27cSSam Leffler static int 419b032f27cSSam Leffler default_reset(struct ieee80211vap *vap, u_long cmd) 420b032f27cSSam Leffler { 421b032f27cSSam Leffler return ENETRESET; 422b032f27cSSam Leffler } 423b032f27cSSam Leffler 424b032f27cSSam Leffler /* 42528da1b56SGleb Smirnoff * Add underlying device errors to vap errors. 42628da1b56SGleb Smirnoff */ 42728da1b56SGleb Smirnoff static uint64_t 42828da1b56SGleb Smirnoff ieee80211_get_counter(struct ifnet *ifp, ift_counter cnt) 42928da1b56SGleb Smirnoff { 43028da1b56SGleb Smirnoff struct ieee80211vap *vap = ifp->if_softc; 43128da1b56SGleb Smirnoff struct ieee80211com *ic = vap->iv_ic; 43228da1b56SGleb Smirnoff uint64_t rv; 43328da1b56SGleb Smirnoff 43428da1b56SGleb Smirnoff rv = if_get_counter_default(ifp, cnt); 43528da1b56SGleb Smirnoff switch (cnt) { 43628da1b56SGleb Smirnoff case IFCOUNTER_OERRORS: 43728da1b56SGleb Smirnoff rv += counter_u64_fetch(ic->ic_oerrors); 43828da1b56SGleb Smirnoff break; 43928da1b56SGleb Smirnoff case IFCOUNTER_IERRORS: 44028da1b56SGleb Smirnoff rv += counter_u64_fetch(ic->ic_ierrors); 44128da1b56SGleb Smirnoff break; 44228da1b56SGleb Smirnoff default: 44328da1b56SGleb Smirnoff break; 44428da1b56SGleb Smirnoff } 44528da1b56SGleb Smirnoff 44628da1b56SGleb Smirnoff return (rv); 44728da1b56SGleb Smirnoff } 44828da1b56SGleb Smirnoff 44928da1b56SGleb Smirnoff /* 450b032f27cSSam Leffler * Prepare a vap for use. Drivers use this call to 451b032f27cSSam Leffler * setup net80211 state in new vap's prior attaching 452b032f27cSSam Leffler * them with ieee80211_vap_attach (below). 453b032f27cSSam Leffler */ 454b032f27cSSam Leffler int 455b032f27cSSam Leffler ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap, 456fcd9500fSBernhard Schmidt const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode, 4577a79cebfSGleb Smirnoff int flags, const uint8_t bssid[IEEE80211_ADDR_LEN]) 458b032f27cSSam Leffler { 459b032f27cSSam Leffler struct ifnet *ifp; 460b032f27cSSam Leffler 461b032f27cSSam Leffler ifp = if_alloc(IFT_ETHER); 462b032f27cSSam Leffler if (ifp == NULL) { 463c8f5794eSGleb Smirnoff ic_printf(ic, "%s: unable to allocate ifnet\n", 464b032f27cSSam Leffler __func__); 465b032f27cSSam Leffler return ENOMEM; 466b032f27cSSam Leffler } 467b032f27cSSam Leffler if_initname(ifp, name, unit); 468b032f27cSSam Leffler ifp->if_softc = vap; /* back pointer */ 469b032f27cSSam Leffler ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST; 470e7495198SAdrian Chadd ifp->if_transmit = ieee80211_vap_transmit; 471e7495198SAdrian Chadd ifp->if_qflush = ieee80211_vap_qflush; 472b032f27cSSam Leffler ifp->if_ioctl = ieee80211_ioctl; 473b032f27cSSam Leffler ifp->if_init = ieee80211_init; 47428da1b56SGleb Smirnoff ifp->if_get_counter = ieee80211_get_counter; 475b032f27cSSam Leffler 476b032f27cSSam Leffler vap->iv_ifp = ifp; 477b032f27cSSam Leffler vap->iv_ic = ic; 478b032f27cSSam Leffler vap->iv_flags = ic->ic_flags; /* propagate common flags */ 479b032f27cSSam Leffler vap->iv_flags_ext = ic->ic_flags_ext; 480b032f27cSSam Leffler vap->iv_flags_ven = ic->ic_flags_ven; 481b032f27cSSam Leffler vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE; 482b032f27cSSam Leffler vap->iv_htcaps = ic->ic_htcaps; 483e1d36f83SRui Paulo vap->iv_htextcaps = ic->ic_htextcaps; 484b032f27cSSam Leffler vap->iv_opmode = opmode; 485c43feedeSSam Leffler vap->iv_caps |= ieee80211_opcap[opmode]; 4861d47c76cSAndriy Voskoboinyk IEEE80211_ADDR_COPY(vap->iv_myaddr, ic->ic_macaddr); 487b032f27cSSam Leffler switch (opmode) { 488b032f27cSSam Leffler case IEEE80211_M_WDS: 489b032f27cSSam Leffler /* 490b032f27cSSam Leffler * WDS links must specify the bssid of the far end. 491b032f27cSSam Leffler * For legacy operation this is a static relationship. 492b032f27cSSam Leffler * For non-legacy operation the station must associate 493b032f27cSSam Leffler * and be authorized to pass traffic. Plumbing the 494b032f27cSSam Leffler * vap to the proper node happens when the vap 495b032f27cSSam Leffler * transitions to RUN state. 496b032f27cSSam Leffler */ 497b032f27cSSam Leffler IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid); 498b032f27cSSam Leffler vap->iv_flags |= IEEE80211_F_DESBSSID; 499b032f27cSSam Leffler if (flags & IEEE80211_CLONE_WDSLEGACY) 500b032f27cSSam Leffler vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY; 501b032f27cSSam Leffler break; 50210ad9a77SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA 50310ad9a77SSam Leffler case IEEE80211_M_AHDEMO: 50410ad9a77SSam Leffler if (flags & IEEE80211_CLONE_TDMA) { 50510ad9a77SSam Leffler /* NB: checked before clone operation allowed */ 50610ad9a77SSam Leffler KASSERT(ic->ic_caps & IEEE80211_C_TDMA, 50710ad9a77SSam Leffler ("not TDMA capable, ic_caps 0x%x", ic->ic_caps)); 50810ad9a77SSam Leffler /* 50910ad9a77SSam Leffler * Propagate TDMA capability to mark vap; this 51010ad9a77SSam Leffler * cannot be removed and is used to distinguish 51110ad9a77SSam Leffler * regular ahdemo operation from ahdemo+tdma. 51210ad9a77SSam Leffler */ 51310ad9a77SSam Leffler vap->iv_caps |= IEEE80211_C_TDMA; 51410ad9a77SSam Leffler } 51510ad9a77SSam Leffler break; 51610ad9a77SSam Leffler #endif 517fcd9500fSBernhard Schmidt default: 518fcd9500fSBernhard Schmidt break; 519b032f27cSSam Leffler } 520ae3f00bbSSam Leffler /* auto-enable s/w beacon miss support */ 521ae3f00bbSSam Leffler if (flags & IEEE80211_CLONE_NOBEACONS) 522ae3f00bbSSam Leffler vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS; 52383fcb812SAndrew Thompson /* auto-generated or user supplied MAC address */ 52483fcb812SAndrew Thompson if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR)) 52583fcb812SAndrew Thompson vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC; 526b032f27cSSam Leffler /* 527b032f27cSSam Leffler * Enable various functionality by default if we're 528b032f27cSSam Leffler * capable; the driver can override us if it knows better. 529b032f27cSSam Leffler */ 530b032f27cSSam Leffler if (vap->iv_caps & IEEE80211_C_WME) 531b032f27cSSam Leffler vap->iv_flags |= IEEE80211_F_WME; 532b032f27cSSam Leffler if (vap->iv_caps & IEEE80211_C_BURST) 533b032f27cSSam Leffler vap->iv_flags |= IEEE80211_F_BURST; 534b032f27cSSam Leffler /* NB: bg scanning only makes sense for station mode right now */ 535b032f27cSSam Leffler if (vap->iv_opmode == IEEE80211_M_STA && 536b032f27cSSam Leffler (vap->iv_caps & IEEE80211_C_BGSCAN)) 537b032f27cSSam Leffler vap->iv_flags |= IEEE80211_F_BGSCAN; 538c43feedeSSam Leffler vap->iv_flags |= IEEE80211_F_DOTH; /* XXX no cap, just ena */ 53982fd2577SSam Leffler /* NB: DFS support only makes sense for ap mode right now */ 54082fd2577SSam Leffler if (vap->iv_opmode == IEEE80211_M_HOSTAP && 54182fd2577SSam Leffler (vap->iv_caps & IEEE80211_C_DFS)) 542b032f27cSSam Leffler vap->iv_flags_ext |= IEEE80211_FEXT_DFS; 543b032f27cSSam Leffler 544b032f27cSSam Leffler vap->iv_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */ 545b032f27cSSam Leffler vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT; 546b032f27cSSam Leffler vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT; 547b032f27cSSam Leffler /* 548b032f27cSSam Leffler * Install a default reset method for the ioctl support; 549b032f27cSSam Leffler * the driver can override this. 550b032f27cSSam Leffler */ 551b032f27cSSam Leffler vap->iv_reset = default_reset; 552b032f27cSSam Leffler 553b032f27cSSam Leffler ieee80211_sysctl_vattach(vap); 554b032f27cSSam Leffler ieee80211_crypto_vattach(vap); 555b032f27cSSam Leffler ieee80211_node_vattach(vap); 556b032f27cSSam Leffler ieee80211_power_vattach(vap); 557b032f27cSSam Leffler ieee80211_proto_vattach(vap); 558616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG 559616190d0SSam Leffler ieee80211_superg_vattach(vap); 560616190d0SSam Leffler #endif 561b032f27cSSam Leffler ieee80211_ht_vattach(vap); 562b032f27cSSam Leffler ieee80211_scan_vattach(vap); 563b032f27cSSam Leffler ieee80211_regdomain_vattach(vap); 5645463c4a4SSam Leffler ieee80211_radiotap_vattach(vap); 565a7c6aabdSBernhard Schmidt ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE); 566b6108616SRui Paulo 567b032f27cSSam Leffler return 0; 568b032f27cSSam Leffler } 569b032f27cSSam Leffler 570b032f27cSSam Leffler /* 571b032f27cSSam Leffler * Activate a vap. State should have been prepared with a 572b032f27cSSam Leffler * call to ieee80211_vap_setup and by the driver. On return 573b032f27cSSam Leffler * from this call the vap is ready for use. 574b032f27cSSam Leffler */ 575b032f27cSSam Leffler int 5767a79cebfSGleb Smirnoff ieee80211_vap_attach(struct ieee80211vap *vap, ifm_change_cb_t media_change, 5777a79cebfSGleb Smirnoff ifm_stat_cb_t media_stat, const uint8_t macaddr[IEEE80211_ADDR_LEN]) 578b032f27cSSam Leffler { 579b032f27cSSam Leffler struct ifnet *ifp = vap->iv_ifp; 580b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic; 581b032f27cSSam Leffler struct ifmediareq imr; 582b032f27cSSam Leffler int maxrate; 583b032f27cSSam Leffler 584b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, 585b032f27cSSam Leffler "%s: %s parent %s flags 0x%x flags_ext 0x%x\n", 586b032f27cSSam Leffler __func__, ieee80211_opmode_name[vap->iv_opmode], 587c8f5794eSGleb Smirnoff ic->ic_name, vap->iv_flags, vap->iv_flags_ext); 588b032f27cSSam Leffler 589b032f27cSSam Leffler /* 590b032f27cSSam Leffler * Do late attach work that cannot happen until after 591b032f27cSSam Leffler * the driver has had a chance to override defaults. 592b032f27cSSam Leffler */ 593b032f27cSSam Leffler ieee80211_node_latevattach(vap); 594b032f27cSSam Leffler ieee80211_power_latevattach(vap); 595b032f27cSSam Leffler 596b032f27cSSam Leffler maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps, 597b032f27cSSam Leffler vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat); 598b032f27cSSam Leffler ieee80211_media_status(ifp, &imr); 599b032f27cSSam Leffler /* NB: strip explicit mode; we're actually in autoselect */ 600c3f10abdSSam Leffler ifmedia_set(&vap->iv_media, 601c3f10abdSSam Leffler imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO)); 602b032f27cSSam Leffler if (maxrate) 603b032f27cSSam Leffler ifp->if_baudrate = IF_Mbps(maxrate); 604b032f27cSSam Leffler 6057a79cebfSGleb Smirnoff ether_ifattach(ifp, macaddr); 6061d47c76cSAndriy Voskoboinyk IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp)); 607b032f27cSSam Leffler /* hook output method setup by ether_ifattach */ 608b032f27cSSam Leffler vap->iv_output = ifp->if_output; 609b032f27cSSam Leffler ifp->if_output = ieee80211_output; 610b032f27cSSam Leffler /* NB: if_mtu set by ether_ifattach to ETHERMTU */ 611b032f27cSSam Leffler 612b032f27cSSam Leffler IEEE80211_LOCK(ic); 613b032f27cSSam Leffler TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next); 614b032f27cSSam Leffler ieee80211_syncflag_locked(ic, IEEE80211_F_WME); 615616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG 616b032f27cSSam Leffler ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP); 617616190d0SSam Leffler #endif 618b032f27cSSam Leffler ieee80211_syncflag_locked(ic, IEEE80211_F_PCF); 619b032f27cSSam Leffler ieee80211_syncflag_locked(ic, IEEE80211_F_BURST); 6202bfc8a91SSam Leffler ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT); 6212bfc8a91SSam Leffler ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40); 622b032f27cSSam Leffler IEEE80211_UNLOCK(ic); 623b032f27cSSam Leffler 624b032f27cSSam Leffler return 1; 625b032f27cSSam Leffler } 626b032f27cSSam Leffler 627b032f27cSSam Leffler /* 628b032f27cSSam Leffler * Tear down vap state and reclaim the ifnet. 629b032f27cSSam Leffler * The driver is assumed to have prepared for 630b032f27cSSam Leffler * this; e.g. by turning off interrupts for the 631b032f27cSSam Leffler * underlying device. 632b032f27cSSam Leffler */ 633b032f27cSSam Leffler void 634b032f27cSSam Leffler ieee80211_vap_detach(struct ieee80211vap *vap) 635b032f27cSSam Leffler { 636b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic; 637b032f27cSSam Leffler struct ifnet *ifp = vap->iv_ifp; 638b032f27cSSam Leffler 63930e4856aSAdrian Chadd CURVNET_SET(ifp->if_vnet); 64030e4856aSAdrian Chadd 641b032f27cSSam Leffler IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n", 6427fc10b6bSGleb Smirnoff __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name); 643b032f27cSSam Leffler 6441da89db5SSam Leffler /* NB: bpfdetach is called by ether_ifdetach and claims all taps */ 6451da89db5SSam Leffler ether_ifdetach(ifp); 6461da89db5SSam Leffler 6471da89db5SSam Leffler ieee80211_stop(vap); 648b032f27cSSam Leffler 6495efea30fSAndrew Thompson /* 6505efea30fSAndrew Thompson * Flush any deferred vap tasks. 6515efea30fSAndrew Thompson */ 6525efea30fSAndrew Thompson ieee80211_draintask(ic, &vap->iv_nstate_task); 6535efea30fSAndrew Thompson ieee80211_draintask(ic, &vap->iv_swbmiss_task); 6545efea30fSAndrew Thompson 655ab501dd6SSam Leffler /* XXX band-aid until ifnet handles this for us */ 656ab501dd6SSam Leffler taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 657ab501dd6SSam Leffler 6585efea30fSAndrew Thompson IEEE80211_LOCK(ic); 6595efea30fSAndrew Thompson KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running")); 660b032f27cSSam Leffler TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next); 661b032f27cSSam Leffler ieee80211_syncflag_locked(ic, IEEE80211_F_WME); 662616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG 663b032f27cSSam Leffler ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP); 664616190d0SSam Leffler #endif 665b032f27cSSam Leffler ieee80211_syncflag_locked(ic, IEEE80211_F_PCF); 666b032f27cSSam Leffler ieee80211_syncflag_locked(ic, IEEE80211_F_BURST); 6672bfc8a91SSam Leffler ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT); 6682bfc8a91SSam Leffler ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40); 6695463c4a4SSam Leffler /* NB: this handles the bpfdetach done below */ 6705463c4a4SSam Leffler ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF); 6717a79cebfSGleb Smirnoff if (vap->iv_ifflags & IFF_PROMISC) 6727a79cebfSGleb Smirnoff ieee80211_promisc(vap, false); 6737a79cebfSGleb Smirnoff if (vap->iv_ifflags & IFF_ALLMULTI) 6747a79cebfSGleb Smirnoff ieee80211_allmulti(vap, false); 675b032f27cSSam Leffler IEEE80211_UNLOCK(ic); 676b032f27cSSam Leffler 677b032f27cSSam Leffler ifmedia_removeall(&vap->iv_media); 678b032f27cSSam Leffler 6795463c4a4SSam Leffler ieee80211_radiotap_vdetach(vap); 680b032f27cSSam Leffler ieee80211_regdomain_vdetach(vap); 681b032f27cSSam Leffler ieee80211_scan_vdetach(vap); 682616190d0SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG 683616190d0SSam Leffler ieee80211_superg_vdetach(vap); 684616190d0SSam Leffler #endif 685b032f27cSSam Leffler ieee80211_ht_vdetach(vap); 686b032f27cSSam Leffler /* NB: must be before ieee80211_node_vdetach */ 687b032f27cSSam Leffler ieee80211_proto_vdetach(vap); 688b032f27cSSam Leffler ieee80211_crypto_vdetach(vap); 689b032f27cSSam Leffler ieee80211_power_vdetach(vap); 690b032f27cSSam Leffler ieee80211_node_vdetach(vap); 691b032f27cSSam Leffler ieee80211_sysctl_vdetach(vap); 692b20f0ed1SWeongyo Jeong 693b20f0ed1SWeongyo Jeong if_free(ifp); 69430e4856aSAdrian Chadd 69530e4856aSAdrian Chadd CURVNET_RESTORE(); 696b032f27cSSam Leffler } 697b032f27cSSam Leffler 698b032f27cSSam Leffler /* 6997a79cebfSGleb Smirnoff * Count number of vaps in promisc, and issue promisc on 7007a79cebfSGleb Smirnoff * parent respectively. 701b032f27cSSam Leffler */ 702b032f27cSSam Leffler void 7037a79cebfSGleb Smirnoff ieee80211_promisc(struct ieee80211vap *vap, bool on) 704b032f27cSSam Leffler { 7057a79cebfSGleb Smirnoff struct ieee80211com *ic = vap->iv_ic; 706b032f27cSSam Leffler 707c6427be9SAndriy Voskoboinyk IEEE80211_LOCK_ASSERT(ic); 708c6427be9SAndriy Voskoboinyk 7097a79cebfSGleb Smirnoff if (on) { 7107a79cebfSGleb Smirnoff if (++ic->ic_promisc == 1) 711ba2c1fbcSAdrian Chadd ieee80211_runtask(ic, &ic->ic_promisc_task); 7127a79cebfSGleb Smirnoff } else { 7137a79cebfSGleb Smirnoff KASSERT(ic->ic_promisc > 0, ("%s: ic %p not promisc", 7147a79cebfSGleb Smirnoff __func__, ic)); 7157a79cebfSGleb Smirnoff if (--ic->ic_promisc == 0) 7167a79cebfSGleb Smirnoff ieee80211_runtask(ic, &ic->ic_promisc_task); 7177a79cebfSGleb Smirnoff } 7187a79cebfSGleb Smirnoff } 7197a79cebfSGleb Smirnoff 7207a79cebfSGleb Smirnoff /* 7217a79cebfSGleb Smirnoff * Count number of vaps in allmulti, and issue allmulti on 7227a79cebfSGleb Smirnoff * parent respectively. 7237a79cebfSGleb Smirnoff */ 7247a79cebfSGleb Smirnoff void 7257a79cebfSGleb Smirnoff ieee80211_allmulti(struct ieee80211vap *vap, bool on) 7267a79cebfSGleb Smirnoff { 7277a79cebfSGleb Smirnoff struct ieee80211com *ic = vap->iv_ic; 7287a79cebfSGleb Smirnoff 729c6427be9SAndriy Voskoboinyk IEEE80211_LOCK_ASSERT(ic); 730c6427be9SAndriy Voskoboinyk 7317a79cebfSGleb Smirnoff if (on) { 7327a79cebfSGleb Smirnoff if (++ic->ic_allmulti == 1) 7337a79cebfSGleb Smirnoff ieee80211_runtask(ic, &ic->ic_mcast_task); 7347a79cebfSGleb Smirnoff } else { 7357a79cebfSGleb Smirnoff KASSERT(ic->ic_allmulti > 0, ("%s: ic %p not allmulti", 7367a79cebfSGleb Smirnoff __func__, ic)); 7377a79cebfSGleb Smirnoff if (--ic->ic_allmulti == 0) 7385efea30fSAndrew Thompson ieee80211_runtask(ic, &ic->ic_mcast_task); 739b032f27cSSam Leffler } 740b032f27cSSam Leffler } 741b032f27cSSam Leffler 742b032f27cSSam Leffler /* 743b032f27cSSam Leffler * Synchronize flag bit state in the com structure 744b032f27cSSam Leffler * according to the state of all vap's. This is used, 745b032f27cSSam Leffler * for example, to handle state changes via ioctls. 746b032f27cSSam Leffler */ 747b032f27cSSam Leffler static void 748b032f27cSSam Leffler ieee80211_syncflag_locked(struct ieee80211com *ic, int flag) 749b032f27cSSam Leffler { 750b032f27cSSam Leffler struct ieee80211vap *vap; 751b032f27cSSam Leffler int bit; 752b032f27cSSam Leffler 753b032f27cSSam Leffler IEEE80211_LOCK_ASSERT(ic); 754b032f27cSSam Leffler 755b032f27cSSam Leffler bit = 0; 756b032f27cSSam Leffler TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 757b032f27cSSam Leffler if (vap->iv_flags & flag) { 758b032f27cSSam Leffler bit = 1; 759b032f27cSSam Leffler break; 760b032f27cSSam Leffler } 761b032f27cSSam Leffler if (bit) 762b032f27cSSam Leffler ic->ic_flags |= flag; 763b032f27cSSam Leffler else 764b032f27cSSam Leffler ic->ic_flags &= ~flag; 765b032f27cSSam Leffler } 766b032f27cSSam Leffler 767b032f27cSSam Leffler void 768b032f27cSSam Leffler ieee80211_syncflag(struct ieee80211vap *vap, int flag) 769b032f27cSSam Leffler { 770b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic; 771b032f27cSSam Leffler 772b032f27cSSam Leffler IEEE80211_LOCK(ic); 773b032f27cSSam Leffler if (flag < 0) { 774b032f27cSSam Leffler flag = -flag; 775b032f27cSSam Leffler vap->iv_flags &= ~flag; 776b032f27cSSam Leffler } else 777b032f27cSSam Leffler vap->iv_flags |= flag; 778b032f27cSSam Leffler ieee80211_syncflag_locked(ic, flag); 779b032f27cSSam Leffler IEEE80211_UNLOCK(ic); 780b032f27cSSam Leffler } 781b032f27cSSam Leffler 782b032f27cSSam Leffler /* 7832bfc8a91SSam Leffler * Synchronize flags_ht bit state in the com structure 7842bfc8a91SSam Leffler * according to the state of all vap's. This is used, 7852bfc8a91SSam Leffler * for example, to handle state changes via ioctls. 7862bfc8a91SSam Leffler */ 7872bfc8a91SSam Leffler static void 7882bfc8a91SSam Leffler ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag) 7892bfc8a91SSam Leffler { 7902bfc8a91SSam Leffler struct ieee80211vap *vap; 7912bfc8a91SSam Leffler int bit; 7922bfc8a91SSam Leffler 7932bfc8a91SSam Leffler IEEE80211_LOCK_ASSERT(ic); 7942bfc8a91SSam Leffler 7952bfc8a91SSam Leffler bit = 0; 7962bfc8a91SSam Leffler TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 7972bfc8a91SSam Leffler if (vap->iv_flags_ht & flag) { 7982bfc8a91SSam Leffler bit = 1; 7992bfc8a91SSam Leffler break; 8002bfc8a91SSam Leffler } 8012bfc8a91SSam Leffler if (bit) 8022bfc8a91SSam Leffler ic->ic_flags_ht |= flag; 8032bfc8a91SSam Leffler else 8042bfc8a91SSam Leffler ic->ic_flags_ht &= ~flag; 8052bfc8a91SSam Leffler } 8062bfc8a91SSam Leffler 8072bfc8a91SSam Leffler void 8082bfc8a91SSam Leffler ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag) 8092bfc8a91SSam Leffler { 8102bfc8a91SSam Leffler struct ieee80211com *ic = vap->iv_ic; 8112bfc8a91SSam Leffler 8122bfc8a91SSam Leffler IEEE80211_LOCK(ic); 8132bfc8a91SSam Leffler if (flag < 0) { 8142bfc8a91SSam Leffler flag = -flag; 8152bfc8a91SSam Leffler vap->iv_flags_ht &= ~flag; 8162bfc8a91SSam Leffler } else 8172bfc8a91SSam Leffler vap->iv_flags_ht |= flag; 8182bfc8a91SSam Leffler ieee80211_syncflag_ht_locked(ic, flag); 8192bfc8a91SSam Leffler IEEE80211_UNLOCK(ic); 8202bfc8a91SSam Leffler } 8212bfc8a91SSam Leffler 8222bfc8a91SSam Leffler /* 8232bfc8a91SSam Leffler * Synchronize flags_ext bit state in the com structure 824b032f27cSSam Leffler * according to the state of all vap's. This is used, 825b032f27cSSam Leffler * for example, to handle state changes via ioctls. 826b032f27cSSam Leffler */ 827b032f27cSSam Leffler static void 828b032f27cSSam Leffler ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag) 829b032f27cSSam Leffler { 830b032f27cSSam Leffler struct ieee80211vap *vap; 831b032f27cSSam Leffler int bit; 832b032f27cSSam Leffler 833b032f27cSSam Leffler IEEE80211_LOCK_ASSERT(ic); 834b032f27cSSam Leffler 835b032f27cSSam Leffler bit = 0; 836b032f27cSSam Leffler TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 837b032f27cSSam Leffler if (vap->iv_flags_ext & flag) { 838b032f27cSSam Leffler bit = 1; 839b032f27cSSam Leffler break; 840b032f27cSSam Leffler } 841b032f27cSSam Leffler if (bit) 842b032f27cSSam Leffler ic->ic_flags_ext |= flag; 843b032f27cSSam Leffler else 844b032f27cSSam Leffler ic->ic_flags_ext &= ~flag; 845b032f27cSSam Leffler } 846b032f27cSSam Leffler 847b032f27cSSam Leffler void 848b032f27cSSam Leffler ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag) 849b032f27cSSam Leffler { 850b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic; 851b032f27cSSam Leffler 852b032f27cSSam Leffler IEEE80211_LOCK(ic); 853b032f27cSSam Leffler if (flag < 0) { 854b032f27cSSam Leffler flag = -flag; 855b032f27cSSam Leffler vap->iv_flags_ext &= ~flag; 856b032f27cSSam Leffler } else 857b032f27cSSam Leffler vap->iv_flags_ext |= flag; 858b032f27cSSam Leffler ieee80211_syncflag_ext_locked(ic, flag); 859b032f27cSSam Leffler IEEE80211_UNLOCK(ic); 8601a1e1d21SSam Leffler } 8611a1e1d21SSam Leffler 862ca4ac7aeSSam Leffler static __inline int 863ca4ac7aeSSam Leffler mapgsm(u_int freq, u_int flags) 864ca4ac7aeSSam Leffler { 865ca4ac7aeSSam Leffler freq *= 10; 866ca4ac7aeSSam Leffler if (flags & IEEE80211_CHAN_QUARTER) 867ca4ac7aeSSam Leffler freq += 5; 868ca4ac7aeSSam Leffler else if (flags & IEEE80211_CHAN_HALF) 869ca4ac7aeSSam Leffler freq += 10; 870ca4ac7aeSSam Leffler else 871ca4ac7aeSSam Leffler freq += 20; 872ca4ac7aeSSam Leffler /* NB: there is no 907/20 wide but leave room */ 873ca4ac7aeSSam Leffler return (freq - 906*10) / 5; 874ca4ac7aeSSam Leffler } 875ca4ac7aeSSam Leffler 876ca4ac7aeSSam Leffler static __inline int 877ca4ac7aeSSam Leffler mappsb(u_int freq, u_int flags) 878ca4ac7aeSSam Leffler { 879ca4ac7aeSSam Leffler return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5; 880ca4ac7aeSSam Leffler } 881ca4ac7aeSSam Leffler 8821a1e1d21SSam Leffler /* 8831a1e1d21SSam Leffler * Convert MHz frequency to IEEE channel number. 8841a1e1d21SSam Leffler */ 8856f322b78SSam Leffler int 8861a1e1d21SSam Leffler ieee80211_mhz2ieee(u_int freq, u_int flags) 8871a1e1d21SSam Leffler { 88811df4239SSam Leffler #define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990) 889ca4ac7aeSSam Leffler if (flags & IEEE80211_CHAN_GSM) 890ca4ac7aeSSam Leffler return mapgsm(freq, flags); 8911a1e1d21SSam Leffler if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 8921a1e1d21SSam Leffler if (freq == 2484) 8931a1e1d21SSam Leffler return 14; 8941a1e1d21SSam Leffler if (freq < 2484) 8956f322b78SSam Leffler return ((int) freq - 2407) / 5; 8961a1e1d21SSam Leffler else 8971a1e1d21SSam Leffler return 15 + ((freq - 2512) / 20); 898c032abb5SSam Leffler } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */ 89941b3c790SSam Leffler if (freq <= 5000) { 90068e8e04eSSam Leffler /* XXX check regdomain? */ 90111df4239SSam Leffler if (IS_FREQ_IN_PSB(freq)) 902ca4ac7aeSSam Leffler return mappsb(freq, flags); 9036f322b78SSam Leffler return (freq - 4000) / 5; 90441b3c790SSam Leffler } else 9051a1e1d21SSam Leffler return (freq - 5000) / 5; 9061a1e1d21SSam Leffler } else { /* either, guess */ 9071a1e1d21SSam Leffler if (freq == 2484) 9081a1e1d21SSam Leffler return 14; 909ca4ac7aeSSam Leffler if (freq < 2484) { 910ca4ac7aeSSam Leffler if (907 <= freq && freq <= 922) 911ca4ac7aeSSam Leffler return mapgsm(freq, flags); 9126f322b78SSam Leffler return ((int) freq - 2407) / 5; 913ca4ac7aeSSam Leffler } 9146f322b78SSam Leffler if (freq < 5000) { 91511df4239SSam Leffler if (IS_FREQ_IN_PSB(freq)) 916ca4ac7aeSSam Leffler return mappsb(freq, flags); 91741b3c790SSam Leffler else if (freq > 4900) 9186f322b78SSam Leffler return (freq - 4000) / 5; 9196f322b78SSam Leffler else 9201a1e1d21SSam Leffler return 15 + ((freq - 2512) / 20); 9216f322b78SSam Leffler } 9221a1e1d21SSam Leffler return (freq - 5000) / 5; 9231a1e1d21SSam Leffler } 92411df4239SSam Leffler #undef IS_FREQ_IN_PSB 9251a1e1d21SSam Leffler } 9261a1e1d21SSam Leffler 9271a1e1d21SSam Leffler /* 9281a1e1d21SSam Leffler * Convert channel to IEEE channel number. 9291a1e1d21SSam Leffler */ 9306f322b78SSam Leffler int 93138da1496SMatt Jacob ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c) 9321a1e1d21SSam Leffler { 93368e8e04eSSam Leffler if (c == NULL) { 934c8f5794eSGleb Smirnoff ic_printf(ic, "invalid channel (NULL)\n"); 9358be0d570SSam Leffler return 0; /* XXX */ 9361a1e1d21SSam Leffler } 93768e8e04eSSam Leffler return (c == IEEE80211_CHAN_ANYC ? IEEE80211_CHAN_ANY : c->ic_ieee); 9381a1e1d21SSam Leffler } 9391a1e1d21SSam Leffler 9401a1e1d21SSam Leffler /* 9411a1e1d21SSam Leffler * Convert IEEE channel number to MHz frequency. 9421a1e1d21SSam Leffler */ 9431a1e1d21SSam Leffler u_int 9441a1e1d21SSam Leffler ieee80211_ieee2mhz(u_int chan, u_int flags) 9451a1e1d21SSam Leffler { 946ca4ac7aeSSam Leffler if (flags & IEEE80211_CHAN_GSM) 947ca4ac7aeSSam Leffler return 907 + 5 * (chan / 10); 9481a1e1d21SSam Leffler if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 9491a1e1d21SSam Leffler if (chan == 14) 9501a1e1d21SSam Leffler return 2484; 9511a1e1d21SSam Leffler if (chan < 14) 9521a1e1d21SSam Leffler return 2407 + chan*5; 9531a1e1d21SSam Leffler else 9541a1e1d21SSam Leffler return 2512 + ((chan-15)*20); 9551a1e1d21SSam Leffler } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */ 95641b3c790SSam Leffler if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) { 95741b3c790SSam Leffler chan -= 37; 95841b3c790SSam Leffler return 4940 + chan*5 + (chan % 5 ? 2 : 0); 95941b3c790SSam Leffler } 9601a1e1d21SSam Leffler return 5000 + (chan*5); 9611a1e1d21SSam Leffler } else { /* either, guess */ 962ca4ac7aeSSam Leffler /* XXX can't distinguish PSB+GSM channels */ 9631a1e1d21SSam Leffler if (chan == 14) 9641a1e1d21SSam Leffler return 2484; 9651a1e1d21SSam Leffler if (chan < 14) /* 0-13 */ 9661a1e1d21SSam Leffler return 2407 + chan*5; 9671a1e1d21SSam Leffler if (chan < 27) /* 15-26 */ 9681a1e1d21SSam Leffler return 2512 + ((chan-15)*20); 9691a1e1d21SSam Leffler return 5000 + (chan*5); 9701a1e1d21SSam Leffler } 9711a1e1d21SSam Leffler } 9721a1e1d21SSam Leffler 973*355fec48SAndriy Voskoboinyk static __inline void 974*355fec48SAndriy Voskoboinyk set_extchan(struct ieee80211_channel *c) 975*355fec48SAndriy Voskoboinyk { 976*355fec48SAndriy Voskoboinyk 977*355fec48SAndriy Voskoboinyk /* 978*355fec48SAndriy Voskoboinyk * IEEE Std 802.11-2012, page 1738, subclause 20.3.15.4: 979*355fec48SAndriy Voskoboinyk * "the secondary channel number shall be 'N + [1,-1] * 4' 980*355fec48SAndriy Voskoboinyk */ 981*355fec48SAndriy Voskoboinyk if (c->ic_flags & IEEE80211_CHAN_HT40U) 982*355fec48SAndriy Voskoboinyk c->ic_extieee = c->ic_ieee + 4; 983*355fec48SAndriy Voskoboinyk else if (c->ic_flags & IEEE80211_CHAN_HT40D) 984*355fec48SAndriy Voskoboinyk c->ic_extieee = c->ic_ieee - 4; 985*355fec48SAndriy Voskoboinyk else 986*355fec48SAndriy Voskoboinyk c->ic_extieee = 0; 987*355fec48SAndriy Voskoboinyk } 988*355fec48SAndriy Voskoboinyk 989*355fec48SAndriy Voskoboinyk static int 990*355fec48SAndriy Voskoboinyk addchan(struct ieee80211_channel chans[], int maxchans, int *nchans, 991*355fec48SAndriy Voskoboinyk uint8_t ieee, uint16_t freq, int8_t maxregpower, uint32_t flags) 992*355fec48SAndriy Voskoboinyk { 993*355fec48SAndriy Voskoboinyk struct ieee80211_channel *c; 994*355fec48SAndriy Voskoboinyk 995*355fec48SAndriy Voskoboinyk if (*nchans >= maxchans) 996*355fec48SAndriy Voskoboinyk return (ENOBUFS); 997*355fec48SAndriy Voskoboinyk 998*355fec48SAndriy Voskoboinyk c = &chans[(*nchans)++]; 999*355fec48SAndriy Voskoboinyk c->ic_ieee = ieee; 1000*355fec48SAndriy Voskoboinyk c->ic_freq = freq != 0 ? freq : ieee80211_ieee2mhz(ieee, flags); 1001*355fec48SAndriy Voskoboinyk c->ic_maxregpower = maxregpower; 1002*355fec48SAndriy Voskoboinyk c->ic_maxpower = 2 * maxregpower; 1003*355fec48SAndriy Voskoboinyk c->ic_flags = flags; 1004*355fec48SAndriy Voskoboinyk set_extchan(c); 1005*355fec48SAndriy Voskoboinyk 1006*355fec48SAndriy Voskoboinyk return (0); 1007*355fec48SAndriy Voskoboinyk } 1008*355fec48SAndriy Voskoboinyk 1009*355fec48SAndriy Voskoboinyk static int 1010*355fec48SAndriy Voskoboinyk copychan_prev(struct ieee80211_channel chans[], int maxchans, int *nchans, 1011*355fec48SAndriy Voskoboinyk uint32_t flags) 1012*355fec48SAndriy Voskoboinyk { 1013*355fec48SAndriy Voskoboinyk struct ieee80211_channel *c; 1014*355fec48SAndriy Voskoboinyk 1015*355fec48SAndriy Voskoboinyk KASSERT(*nchans > 0, ("channel list is empty\n")); 1016*355fec48SAndriy Voskoboinyk 1017*355fec48SAndriy Voskoboinyk if (*nchans >= maxchans) 1018*355fec48SAndriy Voskoboinyk return (ENOBUFS); 1019*355fec48SAndriy Voskoboinyk 1020*355fec48SAndriy Voskoboinyk c = &chans[(*nchans)++]; 1021*355fec48SAndriy Voskoboinyk c[0] = c[-1]; 1022*355fec48SAndriy Voskoboinyk c->ic_flags = flags; 1023*355fec48SAndriy Voskoboinyk set_extchan(c); 1024*355fec48SAndriy Voskoboinyk 1025*355fec48SAndriy Voskoboinyk return (0); 1026*355fec48SAndriy Voskoboinyk } 1027*355fec48SAndriy Voskoboinyk 1028*355fec48SAndriy Voskoboinyk static void 1029*355fec48SAndriy Voskoboinyk getflags_2ghz(const uint8_t bands[], uint32_t flags[], int ht40) 1030*355fec48SAndriy Voskoboinyk { 1031*355fec48SAndriy Voskoboinyk int nmodes; 1032*355fec48SAndriy Voskoboinyk 1033*355fec48SAndriy Voskoboinyk nmodes = 0; 1034*355fec48SAndriy Voskoboinyk if (isset(bands, IEEE80211_MODE_11B)) 1035*355fec48SAndriy Voskoboinyk flags[nmodes++] = IEEE80211_CHAN_B; 1036*355fec48SAndriy Voskoboinyk if (isset(bands, IEEE80211_MODE_11G)) 1037*355fec48SAndriy Voskoboinyk flags[nmodes++] = IEEE80211_CHAN_G; 1038*355fec48SAndriy Voskoboinyk if (isset(bands, IEEE80211_MODE_11NG)) 1039*355fec48SAndriy Voskoboinyk flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20; 1040*355fec48SAndriy Voskoboinyk if (ht40) { 1041*355fec48SAndriy Voskoboinyk flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U; 1042*355fec48SAndriy Voskoboinyk flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D; 1043*355fec48SAndriy Voskoboinyk } 1044*355fec48SAndriy Voskoboinyk flags[nmodes] = 0; 1045*355fec48SAndriy Voskoboinyk } 1046*355fec48SAndriy Voskoboinyk 1047*355fec48SAndriy Voskoboinyk static void 1048*355fec48SAndriy Voskoboinyk getflags_5ghz(const uint8_t bands[], uint32_t flags[], int ht40) 1049*355fec48SAndriy Voskoboinyk { 1050*355fec48SAndriy Voskoboinyk int nmodes; 1051*355fec48SAndriy Voskoboinyk 1052*355fec48SAndriy Voskoboinyk nmodes = 0; 1053*355fec48SAndriy Voskoboinyk if (isset(bands, IEEE80211_MODE_11A)) 1054*355fec48SAndriy Voskoboinyk flags[nmodes++] = IEEE80211_CHAN_A; 1055*355fec48SAndriy Voskoboinyk if (isset(bands, IEEE80211_MODE_11NA)) 1056*355fec48SAndriy Voskoboinyk flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20; 1057*355fec48SAndriy Voskoboinyk if (ht40) { 1058*355fec48SAndriy Voskoboinyk flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U; 1059*355fec48SAndriy Voskoboinyk flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D; 1060*355fec48SAndriy Voskoboinyk } 1061*355fec48SAndriy Voskoboinyk flags[nmodes] = 0; 1062*355fec48SAndriy Voskoboinyk } 1063*355fec48SAndriy Voskoboinyk 1064*355fec48SAndriy Voskoboinyk static void 1065*355fec48SAndriy Voskoboinyk getflags(const uint8_t bands[], uint32_t flags[], int ht40) 1066*355fec48SAndriy Voskoboinyk { 1067*355fec48SAndriy Voskoboinyk 1068*355fec48SAndriy Voskoboinyk flags[0] = 0; 1069*355fec48SAndriy Voskoboinyk if (isset(bands, IEEE80211_MODE_11A) || 1070*355fec48SAndriy Voskoboinyk isset(bands, IEEE80211_MODE_11NA)) { 1071*355fec48SAndriy Voskoboinyk if (isset(bands, IEEE80211_MODE_11B) || 1072*355fec48SAndriy Voskoboinyk isset(bands, IEEE80211_MODE_11G) || 1073*355fec48SAndriy Voskoboinyk isset(bands, IEEE80211_MODE_11NG)) 1074*355fec48SAndriy Voskoboinyk return; 1075*355fec48SAndriy Voskoboinyk 1076*355fec48SAndriy Voskoboinyk getflags_5ghz(bands, flags, ht40); 1077*355fec48SAndriy Voskoboinyk } else 1078*355fec48SAndriy Voskoboinyk getflags_2ghz(bands, flags, ht40); 1079*355fec48SAndriy Voskoboinyk } 1080*355fec48SAndriy Voskoboinyk 1081*355fec48SAndriy Voskoboinyk /* 1082*355fec48SAndriy Voskoboinyk * Add one 20 MHz channel into specified channel list. 1083*355fec48SAndriy Voskoboinyk */ 1084*355fec48SAndriy Voskoboinyk int 1085*355fec48SAndriy Voskoboinyk ieee80211_add_channel(struct ieee80211_channel chans[], int maxchans, 1086*355fec48SAndriy Voskoboinyk int *nchans, uint8_t ieee, uint16_t freq, int8_t maxregpower, 1087*355fec48SAndriy Voskoboinyk uint32_t chan_flags, const uint8_t bands[]) 1088*355fec48SAndriy Voskoboinyk { 1089*355fec48SAndriy Voskoboinyk uint32_t flags[IEEE80211_MODE_MAX]; 1090*355fec48SAndriy Voskoboinyk int i, error; 1091*355fec48SAndriy Voskoboinyk 1092*355fec48SAndriy Voskoboinyk getflags(bands, flags, 0); 1093*355fec48SAndriy Voskoboinyk KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__)); 1094*355fec48SAndriy Voskoboinyk 1095*355fec48SAndriy Voskoboinyk error = addchan(chans, maxchans, nchans, ieee, freq, maxregpower, 1096*355fec48SAndriy Voskoboinyk flags[0] | chan_flags); 1097*355fec48SAndriy Voskoboinyk for (i = 1; flags[i] != 0 && error == 0; i++) { 1098*355fec48SAndriy Voskoboinyk error = copychan_prev(chans, maxchans, nchans, 1099*355fec48SAndriy Voskoboinyk flags[i] | chan_flags); 1100*355fec48SAndriy Voskoboinyk } 1101*355fec48SAndriy Voskoboinyk 1102*355fec48SAndriy Voskoboinyk return (error); 1103*355fec48SAndriy Voskoboinyk } 1104*355fec48SAndriy Voskoboinyk 1105*355fec48SAndriy Voskoboinyk static struct ieee80211_channel * 1106*355fec48SAndriy Voskoboinyk findchannel(struct ieee80211_channel chans[], int nchans, uint16_t freq, 1107*355fec48SAndriy Voskoboinyk uint32_t flags) 1108*355fec48SAndriy Voskoboinyk { 1109*355fec48SAndriy Voskoboinyk struct ieee80211_channel *c; 1110*355fec48SAndriy Voskoboinyk int i; 1111*355fec48SAndriy Voskoboinyk 1112*355fec48SAndriy Voskoboinyk flags &= IEEE80211_CHAN_ALLTURBO; 1113*355fec48SAndriy Voskoboinyk /* brute force search */ 1114*355fec48SAndriy Voskoboinyk for (i = 0; i < nchans; i++) { 1115*355fec48SAndriy Voskoboinyk c = &chans[i]; 1116*355fec48SAndriy Voskoboinyk if (c->ic_freq == freq && 1117*355fec48SAndriy Voskoboinyk (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 1118*355fec48SAndriy Voskoboinyk return c; 1119*355fec48SAndriy Voskoboinyk } 1120*355fec48SAndriy Voskoboinyk return NULL; 1121*355fec48SAndriy Voskoboinyk } 1122*355fec48SAndriy Voskoboinyk 1123*355fec48SAndriy Voskoboinyk /* 1124*355fec48SAndriy Voskoboinyk * Add 40 MHz channel pair into specified channel list. 1125*355fec48SAndriy Voskoboinyk */ 1126*355fec48SAndriy Voskoboinyk int 1127*355fec48SAndriy Voskoboinyk ieee80211_add_channel_ht40(struct ieee80211_channel chans[], int maxchans, 1128*355fec48SAndriy Voskoboinyk int *nchans, uint8_t ieee, int8_t maxregpower, uint32_t flags) 1129*355fec48SAndriy Voskoboinyk { 1130*355fec48SAndriy Voskoboinyk struct ieee80211_channel *cent, *extc; 1131*355fec48SAndriy Voskoboinyk uint16_t freq; 1132*355fec48SAndriy Voskoboinyk int error; 1133*355fec48SAndriy Voskoboinyk 1134*355fec48SAndriy Voskoboinyk freq = ieee80211_ieee2mhz(ieee, flags); 1135*355fec48SAndriy Voskoboinyk 1136*355fec48SAndriy Voskoboinyk /* 1137*355fec48SAndriy Voskoboinyk * Each entry defines an HT40 channel pair; find the 1138*355fec48SAndriy Voskoboinyk * center channel, then the extension channel above. 1139*355fec48SAndriy Voskoboinyk */ 1140*355fec48SAndriy Voskoboinyk flags |= IEEE80211_CHAN_HT20; 1141*355fec48SAndriy Voskoboinyk cent = findchannel(chans, *nchans, freq, flags); 1142*355fec48SAndriy Voskoboinyk if (cent == NULL) 1143*355fec48SAndriy Voskoboinyk return (EINVAL); 1144*355fec48SAndriy Voskoboinyk 1145*355fec48SAndriy Voskoboinyk extc = findchannel(chans, *nchans, freq + 20, flags); 1146*355fec48SAndriy Voskoboinyk if (extc == NULL) 1147*355fec48SAndriy Voskoboinyk return (ENOENT); 1148*355fec48SAndriy Voskoboinyk 1149*355fec48SAndriy Voskoboinyk flags &= ~IEEE80211_CHAN_HT; 1150*355fec48SAndriy Voskoboinyk error = addchan(chans, maxchans, nchans, cent->ic_ieee, cent->ic_freq, 1151*355fec48SAndriy Voskoboinyk maxregpower, flags | IEEE80211_CHAN_HT40U); 1152*355fec48SAndriy Voskoboinyk if (error != 0) 1153*355fec48SAndriy Voskoboinyk return (error); 1154*355fec48SAndriy Voskoboinyk 1155*355fec48SAndriy Voskoboinyk error = addchan(chans, maxchans, nchans, extc->ic_ieee, extc->ic_freq, 1156*355fec48SAndriy Voskoboinyk maxregpower, flags | IEEE80211_CHAN_HT40D); 1157*355fec48SAndriy Voskoboinyk 1158*355fec48SAndriy Voskoboinyk return (error); 1159*355fec48SAndriy Voskoboinyk } 1160*355fec48SAndriy Voskoboinyk 1161*355fec48SAndriy Voskoboinyk /* 1162*355fec48SAndriy Voskoboinyk * Adds channels into specified channel list (ieee[] array must be sorted). 1163*355fec48SAndriy Voskoboinyk * Channels are already sorted. 1164*355fec48SAndriy Voskoboinyk */ 1165*355fec48SAndriy Voskoboinyk static int 1166*355fec48SAndriy Voskoboinyk add_chanlist(struct ieee80211_channel chans[], int maxchans, int *nchans, 1167*355fec48SAndriy Voskoboinyk const uint8_t ieee[], int nieee, uint32_t flags[]) 1168*355fec48SAndriy Voskoboinyk { 1169*355fec48SAndriy Voskoboinyk uint16_t freq; 1170*355fec48SAndriy Voskoboinyk int i, j, error; 1171*355fec48SAndriy Voskoboinyk 1172*355fec48SAndriy Voskoboinyk for (i = 0; i < nieee; i++) { 1173*355fec48SAndriy Voskoboinyk freq = ieee80211_ieee2mhz(ieee[i], flags[0]); 1174*355fec48SAndriy Voskoboinyk for (j = 0; flags[j] != 0; j++) { 1175*355fec48SAndriy Voskoboinyk if (flags[j] & IEEE80211_CHAN_HT40D) 1176*355fec48SAndriy Voskoboinyk if (i == 0 || ieee[i] < ieee[0] + 4 || 1177*355fec48SAndriy Voskoboinyk freq - 20 != 1178*355fec48SAndriy Voskoboinyk ieee80211_ieee2mhz(ieee[i] - 4, flags[j])) 1179*355fec48SAndriy Voskoboinyk continue; 1180*355fec48SAndriy Voskoboinyk if (flags[j] & IEEE80211_CHAN_HT40U) 1181*355fec48SAndriy Voskoboinyk if (i == nieee - 1 || 1182*355fec48SAndriy Voskoboinyk ieee[i] + 4 > ieee[nieee - 1] || 1183*355fec48SAndriy Voskoboinyk freq + 20 != 1184*355fec48SAndriy Voskoboinyk ieee80211_ieee2mhz(ieee[i] + 4, flags[j])) 1185*355fec48SAndriy Voskoboinyk continue; 1186*355fec48SAndriy Voskoboinyk 1187*355fec48SAndriy Voskoboinyk if (j == 0) { 1188*355fec48SAndriy Voskoboinyk error = addchan(chans, maxchans, nchans, 1189*355fec48SAndriy Voskoboinyk ieee[i], freq, 0, flags[j]); 1190*355fec48SAndriy Voskoboinyk } else { 1191*355fec48SAndriy Voskoboinyk error = copychan_prev(chans, maxchans, nchans, 1192*355fec48SAndriy Voskoboinyk flags[j]); 1193*355fec48SAndriy Voskoboinyk } 1194*355fec48SAndriy Voskoboinyk if (error != 0) 1195*355fec48SAndriy Voskoboinyk return (error); 1196*355fec48SAndriy Voskoboinyk } 1197*355fec48SAndriy Voskoboinyk } 1198*355fec48SAndriy Voskoboinyk 1199*355fec48SAndriy Voskoboinyk return (error); 1200*355fec48SAndriy Voskoboinyk } 1201*355fec48SAndriy Voskoboinyk 1202*355fec48SAndriy Voskoboinyk int 1203*355fec48SAndriy Voskoboinyk ieee80211_add_channel_list_2ghz(struct ieee80211_channel chans[], int maxchans, 1204*355fec48SAndriy Voskoboinyk int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[], 1205*355fec48SAndriy Voskoboinyk int ht40) 1206*355fec48SAndriy Voskoboinyk { 1207*355fec48SAndriy Voskoboinyk uint32_t flags[IEEE80211_MODE_MAX]; 1208*355fec48SAndriy Voskoboinyk 1209*355fec48SAndriy Voskoboinyk getflags_2ghz(bands, flags, ht40); 1210*355fec48SAndriy Voskoboinyk KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__)); 1211*355fec48SAndriy Voskoboinyk 1212*355fec48SAndriy Voskoboinyk return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags)); 1213*355fec48SAndriy Voskoboinyk } 1214*355fec48SAndriy Voskoboinyk 1215*355fec48SAndriy Voskoboinyk int 1216*355fec48SAndriy Voskoboinyk ieee80211_add_channel_list_5ghz(struct ieee80211_channel chans[], int maxchans, 1217*355fec48SAndriy Voskoboinyk int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[], 1218*355fec48SAndriy Voskoboinyk int ht40) 1219*355fec48SAndriy Voskoboinyk { 1220*355fec48SAndriy Voskoboinyk uint32_t flags[IEEE80211_MODE_MAX]; 1221*355fec48SAndriy Voskoboinyk 1222*355fec48SAndriy Voskoboinyk getflags_5ghz(bands, flags, ht40); 1223*355fec48SAndriy Voskoboinyk KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__)); 1224*355fec48SAndriy Voskoboinyk 1225*355fec48SAndriy Voskoboinyk return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags)); 1226*355fec48SAndriy Voskoboinyk } 1227*355fec48SAndriy Voskoboinyk 12281a1e1d21SSam Leffler /* 122968e8e04eSSam Leffler * Locate a channel given a frequency+flags. We cache 1230b032f27cSSam Leffler * the previous lookup to optimize switching between two 123168e8e04eSSam Leffler * channels--as happens with dynamic turbo. 123268e8e04eSSam Leffler */ 123368e8e04eSSam Leffler struct ieee80211_channel * 123468e8e04eSSam Leffler ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags) 123568e8e04eSSam Leffler { 123668e8e04eSSam Leffler struct ieee80211_channel *c; 123768e8e04eSSam Leffler 123868e8e04eSSam Leffler flags &= IEEE80211_CHAN_ALLTURBO; 123968e8e04eSSam Leffler c = ic->ic_prevchan; 124068e8e04eSSam Leffler if (c != NULL && c->ic_freq == freq && 124168e8e04eSSam Leffler (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 124268e8e04eSSam Leffler return c; 124368e8e04eSSam Leffler /* brute force search */ 1244*355fec48SAndriy Voskoboinyk return (findchannel(ic->ic_channels, ic->ic_nchans, freq, flags)); 124568e8e04eSSam Leffler } 124668e8e04eSSam Leffler 1247a557c018SSam Leffler /* 1248a557c018SSam Leffler * Locate a channel given a channel number+flags. We cache 1249a557c018SSam Leffler * the previous lookup to optimize switching between two 1250a557c018SSam Leffler * channels--as happens with dynamic turbo. 1251a557c018SSam Leffler */ 1252a557c018SSam Leffler struct ieee80211_channel * 1253a557c018SSam Leffler ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags) 1254a557c018SSam Leffler { 1255a557c018SSam Leffler struct ieee80211_channel *c; 1256a557c018SSam Leffler int i; 1257a557c018SSam Leffler 1258a557c018SSam Leffler flags &= IEEE80211_CHAN_ALLTURBO; 1259a557c018SSam Leffler c = ic->ic_prevchan; 1260a557c018SSam Leffler if (c != NULL && c->ic_ieee == ieee && 1261a557c018SSam Leffler (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 1262a557c018SSam Leffler return c; 1263a557c018SSam Leffler /* brute force search */ 1264a557c018SSam Leffler for (i = 0; i < ic->ic_nchans; i++) { 1265a557c018SSam Leffler c = &ic->ic_channels[i]; 1266a557c018SSam Leffler if (c->ic_ieee == ieee && 1267a557c018SSam Leffler (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 1268a557c018SSam Leffler return c; 1269a557c018SSam Leffler } 1270a557c018SSam Leffler return NULL; 1271a557c018SSam Leffler } 1272a557c018SSam Leffler 1273c79f192cSAdrian Chadd /* 1274c79f192cSAdrian Chadd * Lookup a channel suitable for the given rx status. 1275c79f192cSAdrian Chadd * 1276c79f192cSAdrian Chadd * This is used to find a channel for a frame (eg beacon, probe 1277c79f192cSAdrian Chadd * response) based purely on the received PHY information. 1278c79f192cSAdrian Chadd * 1279c79f192cSAdrian Chadd * For now it tries to do it based on R_FREQ / R_IEEE. 1280c79f192cSAdrian Chadd * This is enough for 11bg and 11a (and thus 11ng/11na) 1281c79f192cSAdrian Chadd * but it will not be enough for GSM, PSB channels and the 1282c79f192cSAdrian Chadd * like. It also doesn't know about legacy-turbog and 1283c79f192cSAdrian Chadd * legacy-turbo modes, which some offload NICs actually 1284c79f192cSAdrian Chadd * support in weird ways. 1285c79f192cSAdrian Chadd * 1286c79f192cSAdrian Chadd * Takes the ic and rxstatus; returns the channel or NULL 1287c79f192cSAdrian Chadd * if not found. 1288c79f192cSAdrian Chadd * 1289c79f192cSAdrian Chadd * XXX TODO: Add support for that when the need arises. 1290c79f192cSAdrian Chadd */ 1291c79f192cSAdrian Chadd struct ieee80211_channel * 1292c79f192cSAdrian Chadd ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap, 1293c79f192cSAdrian Chadd const struct ieee80211_rx_stats *rxs) 1294c79f192cSAdrian Chadd { 1295c79f192cSAdrian Chadd struct ieee80211com *ic = vap->iv_ic; 1296c79f192cSAdrian Chadd uint32_t flags; 1297c79f192cSAdrian Chadd struct ieee80211_channel *c; 1298c79f192cSAdrian Chadd 1299c79f192cSAdrian Chadd if (rxs == NULL) 1300c79f192cSAdrian Chadd return (NULL); 1301c79f192cSAdrian Chadd 1302c79f192cSAdrian Chadd /* 1303c79f192cSAdrian Chadd * Strictly speaking we only use freq for now, 1304c79f192cSAdrian Chadd * however later on we may wish to just store 1305c79f192cSAdrian Chadd * the ieee for verification. 1306c79f192cSAdrian Chadd */ 1307c79f192cSAdrian Chadd if ((rxs->r_flags & IEEE80211_R_FREQ) == 0) 1308c79f192cSAdrian Chadd return (NULL); 1309c79f192cSAdrian Chadd if ((rxs->r_flags & IEEE80211_R_IEEE) == 0) 1310c79f192cSAdrian Chadd return (NULL); 1311c79f192cSAdrian Chadd 1312c79f192cSAdrian Chadd /* 1313c79f192cSAdrian Chadd * If the rx status contains a valid ieee/freq, then 1314c79f192cSAdrian Chadd * ensure we populate the correct channel information 1315c79f192cSAdrian Chadd * in rxchan before passing it up to the scan infrastructure. 1316c79f192cSAdrian Chadd * Offload NICs will pass up beacons from all channels 1317c79f192cSAdrian Chadd * during background scans. 1318c79f192cSAdrian Chadd */ 1319c79f192cSAdrian Chadd 1320c79f192cSAdrian Chadd /* Determine a band */ 1321c79f192cSAdrian Chadd /* XXX should be done by the driver? */ 1322c79f192cSAdrian Chadd if (rxs->c_freq < 3000) { 13232108f2a8SAdrian Chadd flags = IEEE80211_CHAN_G; 1324c79f192cSAdrian Chadd } else { 1325c79f192cSAdrian Chadd flags = IEEE80211_CHAN_A; 1326c79f192cSAdrian Chadd } 1327c79f192cSAdrian Chadd 1328c79f192cSAdrian Chadd /* Channel lookup */ 1329c79f192cSAdrian Chadd c = ieee80211_find_channel(ic, rxs->c_freq, flags); 1330c79f192cSAdrian Chadd 1331c79f192cSAdrian Chadd IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT, 1332c79f192cSAdrian Chadd "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n", 1333c79f192cSAdrian Chadd __func__, 1334c79f192cSAdrian Chadd (int) rxs->c_freq, 1335c79f192cSAdrian Chadd (int) rxs->c_ieee, 1336c79f192cSAdrian Chadd flags, 1337c79f192cSAdrian Chadd c); 1338c79f192cSAdrian Chadd 1339c79f192cSAdrian Chadd return (c); 1340c79f192cSAdrian Chadd } 1341c79f192cSAdrian Chadd 134268e8e04eSSam Leffler static void 1343b032f27cSSam Leffler addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword) 134468e8e04eSSam Leffler { 134568e8e04eSSam Leffler #define ADD(_ic, _s, _o) \ 1346b032f27cSSam Leffler ifmedia_add(media, \ 134768e8e04eSSam Leffler IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL) 134868e8e04eSSam Leffler static const u_int mopts[IEEE80211_MODE_MAX] = { 1349c3f10abdSSam Leffler [IEEE80211_MODE_AUTO] = IFM_AUTO, 1350c3f10abdSSam Leffler [IEEE80211_MODE_11A] = IFM_IEEE80211_11A, 1351c3f10abdSSam Leffler [IEEE80211_MODE_11B] = IFM_IEEE80211_11B, 1352c3f10abdSSam Leffler [IEEE80211_MODE_11G] = IFM_IEEE80211_11G, 1353c3f10abdSSam Leffler [IEEE80211_MODE_FH] = IFM_IEEE80211_FH, 1354c3f10abdSSam Leffler [IEEE80211_MODE_TURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO, 1355c3f10abdSSam Leffler [IEEE80211_MODE_TURBO_G] = IFM_IEEE80211_11G|IFM_IEEE80211_TURBO, 1356c3f10abdSSam Leffler [IEEE80211_MODE_STURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO, 13576a76ae21SSam Leffler [IEEE80211_MODE_HALF] = IFM_IEEE80211_11A, /* XXX */ 13586a76ae21SSam Leffler [IEEE80211_MODE_QUARTER] = IFM_IEEE80211_11A, /* XXX */ 1359c3f10abdSSam Leffler [IEEE80211_MODE_11NA] = IFM_IEEE80211_11NA, 1360c3f10abdSSam Leffler [IEEE80211_MODE_11NG] = IFM_IEEE80211_11NG, 136168e8e04eSSam Leffler }; 136268e8e04eSSam Leffler u_int mopt; 136368e8e04eSSam Leffler 136468e8e04eSSam Leffler mopt = mopts[mode]; 1365b032f27cSSam Leffler if (addsta) 1366b032f27cSSam Leffler ADD(ic, mword, mopt); /* STA mode has no cap */ 1367b032f27cSSam Leffler if (caps & IEEE80211_C_IBSS) 1368b032f27cSSam Leffler ADD(media, mword, mopt | IFM_IEEE80211_ADHOC); 1369b032f27cSSam Leffler if (caps & IEEE80211_C_HOSTAP) 1370b032f27cSSam Leffler ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP); 1371b032f27cSSam Leffler if (caps & IEEE80211_C_AHDEMO) 1372b032f27cSSam Leffler ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0); 1373b032f27cSSam Leffler if (caps & IEEE80211_C_MONITOR) 1374b032f27cSSam Leffler ADD(media, mword, mopt | IFM_IEEE80211_MONITOR); 1375b032f27cSSam Leffler if (caps & IEEE80211_C_WDS) 1376b032f27cSSam Leffler ADD(media, mword, mopt | IFM_IEEE80211_WDS); 137759aa14a9SRui Paulo if (caps & IEEE80211_C_MBSS) 137859aa14a9SRui Paulo ADD(media, mword, mopt | IFM_IEEE80211_MBSS); 137968e8e04eSSam Leffler #undef ADD 138068e8e04eSSam Leffler } 138168e8e04eSSam Leffler 138268e8e04eSSam Leffler /* 13831a1e1d21SSam Leffler * Setup the media data structures according to the channel and 1384b032f27cSSam Leffler * rate tables. 13851a1e1d21SSam Leffler */ 1386b032f27cSSam Leffler static int 1387b032f27cSSam Leffler ieee80211_media_setup(struct ieee80211com *ic, 1388b032f27cSSam Leffler struct ifmedia *media, int caps, int addsta, 13891a1e1d21SSam Leffler ifm_change_cb_t media_change, ifm_stat_cb_t media_stat) 13901a1e1d21SSam Leffler { 1391fcd9500fSBernhard Schmidt int i, j, rate, maxrate, mword, r; 1392fcd9500fSBernhard Schmidt enum ieee80211_phymode mode; 139368e8e04eSSam Leffler const struct ieee80211_rateset *rs; 13941a1e1d21SSam Leffler struct ieee80211_rateset allrates; 13951a1e1d21SSam Leffler 13962692bb26SSam Leffler /* 13971a1e1d21SSam Leffler * Fill in media characteristics. 13981a1e1d21SSam Leffler */ 1399b032f27cSSam Leffler ifmedia_init(media, 0, media_change, media_stat); 14001a1e1d21SSam Leffler maxrate = 0; 140168e8e04eSSam Leffler /* 140268e8e04eSSam Leffler * Add media for legacy operating modes. 140368e8e04eSSam Leffler */ 14041a1e1d21SSam Leffler memset(&allrates, 0, sizeof(allrates)); 140568e8e04eSSam Leffler for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) { 14066dbd16f1SSam Leffler if (isclr(ic->ic_modecaps, mode)) 14071a1e1d21SSam Leffler continue; 1408b032f27cSSam Leffler addmedia(media, caps, addsta, mode, IFM_AUTO); 14091a1e1d21SSam Leffler if (mode == IEEE80211_MODE_AUTO) 14101a1e1d21SSam Leffler continue; 14111a1e1d21SSam Leffler rs = &ic->ic_sup_rates[mode]; 14121a1e1d21SSam Leffler for (i = 0; i < rs->rs_nrates; i++) { 14131a1e1d21SSam Leffler rate = rs->rs_rates[i]; 14141a1e1d21SSam Leffler mword = ieee80211_rate2media(ic, rate, mode); 14151a1e1d21SSam Leffler if (mword == 0) 14161a1e1d21SSam Leffler continue; 1417b032f27cSSam Leffler addmedia(media, caps, addsta, mode, mword); 14181a1e1d21SSam Leffler /* 141968e8e04eSSam Leffler * Add legacy rate to the collection of all rates. 14201a1e1d21SSam Leffler */ 14211a1e1d21SSam Leffler r = rate & IEEE80211_RATE_VAL; 14221a1e1d21SSam Leffler for (j = 0; j < allrates.rs_nrates; j++) 14231a1e1d21SSam Leffler if (allrates.rs_rates[j] == r) 14241a1e1d21SSam Leffler break; 14251a1e1d21SSam Leffler if (j == allrates.rs_nrates) { 14261a1e1d21SSam Leffler /* unique, add to the set */ 14271a1e1d21SSam Leffler allrates.rs_rates[j] = r; 14281a1e1d21SSam Leffler allrates.rs_nrates++; 14291a1e1d21SSam Leffler } 14301a1e1d21SSam Leffler rate = (rate & IEEE80211_RATE_VAL) / 2; 14311a1e1d21SSam Leffler if (rate > maxrate) 14321a1e1d21SSam Leffler maxrate = rate; 14331a1e1d21SSam Leffler } 14341a1e1d21SSam Leffler } 14351a1e1d21SSam Leffler for (i = 0; i < allrates.rs_nrates; i++) { 14361a1e1d21SSam Leffler mword = ieee80211_rate2media(ic, allrates.rs_rates[i], 14371a1e1d21SSam Leffler IEEE80211_MODE_AUTO); 14381a1e1d21SSam Leffler if (mword == 0) 14391a1e1d21SSam Leffler continue; 144068e8e04eSSam Leffler /* NB: remove media options from mword */ 1441b032f27cSSam Leffler addmedia(media, caps, addsta, 1442b032f27cSSam Leffler IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword)); 14431a1e1d21SSam Leffler } 144468e8e04eSSam Leffler /* 144568e8e04eSSam Leffler * Add HT/11n media. Note that we do not have enough 144668e8e04eSSam Leffler * bits in the media subtype to express the MCS so we 144768e8e04eSSam Leffler * use a "placeholder" media subtype and any fixed MCS 144868e8e04eSSam Leffler * must be specified with a different mechanism. 144968e8e04eSSam Leffler */ 14506a76ae21SSam Leffler for (; mode <= IEEE80211_MODE_11NG; mode++) { 145168e8e04eSSam Leffler if (isclr(ic->ic_modecaps, mode)) 145268e8e04eSSam Leffler continue; 1453b032f27cSSam Leffler addmedia(media, caps, addsta, mode, IFM_AUTO); 1454b032f27cSSam Leffler addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS); 145568e8e04eSSam Leffler } 145668e8e04eSSam Leffler if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) || 145768e8e04eSSam Leffler isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) { 1458b032f27cSSam Leffler addmedia(media, caps, addsta, 1459b032f27cSSam Leffler IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS); 14606f897ba9SBernhard Schmidt i = ic->ic_txstream * 8 - 1; 14616f897ba9SBernhard Schmidt if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) && 14626f897ba9SBernhard Schmidt (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40)) 14636f897ba9SBernhard Schmidt rate = ieee80211_htrates[i].ht40_rate_400ns; 14646f897ba9SBernhard Schmidt else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40)) 14656f897ba9SBernhard Schmidt rate = ieee80211_htrates[i].ht40_rate_800ns; 14666f897ba9SBernhard Schmidt else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20)) 14676f897ba9SBernhard Schmidt rate = ieee80211_htrates[i].ht20_rate_400ns; 14686f897ba9SBernhard Schmidt else 14696f897ba9SBernhard Schmidt rate = ieee80211_htrates[i].ht20_rate_800ns; 14706f897ba9SBernhard Schmidt if (rate > maxrate) 14716f897ba9SBernhard Schmidt maxrate = rate; 1472b032f27cSSam Leffler } 1473b032f27cSSam Leffler return maxrate; 147468e8e04eSSam Leffler } 147568e8e04eSSam Leffler 14766a76ae21SSam Leffler /* XXX inline or eliminate? */ 147741b3c790SSam Leffler const struct ieee80211_rateset * 147841b3c790SSam Leffler ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c) 147941b3c790SSam Leffler { 148040432d36SSam Leffler /* XXX does this work for 11ng basic rates? */ 148168e8e04eSSam Leffler return &ic->ic_sup_rates[ieee80211_chan2mode(c)]; 148241b3c790SSam Leffler } 148341b3c790SSam Leffler 14848a1b9b6aSSam Leffler void 14858a1b9b6aSSam Leffler ieee80211_announce(struct ieee80211com *ic) 14868a1b9b6aSSam Leffler { 1487fcd9500fSBernhard Schmidt int i, rate, mword; 1488fcd9500fSBernhard Schmidt enum ieee80211_phymode mode; 148968e8e04eSSam Leffler const struct ieee80211_rateset *rs; 14908a1b9b6aSSam Leffler 14917edb9e0aSSam Leffler /* NB: skip AUTO since it has no rates */ 14927edb9e0aSSam Leffler for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) { 14936dbd16f1SSam Leffler if (isclr(ic->ic_modecaps, mode)) 14948a1b9b6aSSam Leffler continue; 1495c8f5794eSGleb Smirnoff ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]); 14968a1b9b6aSSam Leffler rs = &ic->ic_sup_rates[mode]; 14978a1b9b6aSSam Leffler for (i = 0; i < rs->rs_nrates; i++) { 149868e8e04eSSam Leffler mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode); 14998a1b9b6aSSam Leffler if (mword == 0) 15008a1b9b6aSSam Leffler continue; 150168e8e04eSSam Leffler rate = ieee80211_media2rate(mword); 15028a1b9b6aSSam Leffler printf("%s%d%sMbps", (i != 0 ? " " : ""), 150368e8e04eSSam Leffler rate / 2, ((rate & 0x1) != 0 ? ".5" : "")); 15048a1b9b6aSSam Leffler } 15058a1b9b6aSSam Leffler printf("\n"); 15068a1b9b6aSSam Leffler } 150768e8e04eSSam Leffler ieee80211_ht_announce(ic); 15088a1b9b6aSSam Leffler } 15098a1b9b6aSSam Leffler 151068e8e04eSSam Leffler void 151168e8e04eSSam Leffler ieee80211_announce_channels(struct ieee80211com *ic) 15121a1e1d21SSam Leffler { 151368e8e04eSSam Leffler const struct ieee80211_channel *c; 151468e8e04eSSam Leffler char type; 151568e8e04eSSam Leffler int i, cw; 151668e8e04eSSam Leffler 151768e8e04eSSam Leffler printf("Chan Freq CW RegPwr MinPwr MaxPwr\n"); 151868e8e04eSSam Leffler for (i = 0; i < ic->ic_nchans; i++) { 151968e8e04eSSam Leffler c = &ic->ic_channels[i]; 152068e8e04eSSam Leffler if (IEEE80211_IS_CHAN_ST(c)) 152168e8e04eSSam Leffler type = 'S'; 152268e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_108A(c)) 152368e8e04eSSam Leffler type = 'T'; 152468e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_108G(c)) 152568e8e04eSSam Leffler type = 'G'; 152668e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_HT(c)) 152768e8e04eSSam Leffler type = 'n'; 152868e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_A(c)) 152968e8e04eSSam Leffler type = 'a'; 153068e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_ANYG(c)) 153168e8e04eSSam Leffler type = 'g'; 153268e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_B(c)) 153368e8e04eSSam Leffler type = 'b'; 153468e8e04eSSam Leffler else 153568e8e04eSSam Leffler type = 'f'; 153668e8e04eSSam Leffler if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c)) 153768e8e04eSSam Leffler cw = 40; 153868e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_HALF(c)) 153968e8e04eSSam Leffler cw = 10; 154068e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_QUARTER(c)) 154168e8e04eSSam Leffler cw = 5; 154268e8e04eSSam Leffler else 154368e8e04eSSam Leffler cw = 20; 154468e8e04eSSam Leffler printf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n" 154568e8e04eSSam Leffler , c->ic_ieee, c->ic_freq, type 154668e8e04eSSam Leffler , cw 154768e8e04eSSam Leffler , IEEE80211_IS_CHAN_HT40U(c) ? '+' : 154868e8e04eSSam Leffler IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' ' 154968e8e04eSSam Leffler , c->ic_maxregpower 155068e8e04eSSam Leffler , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0 155168e8e04eSSam Leffler , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0 155268e8e04eSSam Leffler ); 155368e8e04eSSam Leffler } 15541a1e1d21SSam Leffler } 15551a1e1d21SSam Leffler 155668e8e04eSSam Leffler static int 1557f945bd7aSSam Leffler media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode) 155868e8e04eSSam Leffler { 15591a1e1d21SSam Leffler switch (IFM_MODE(ime->ifm_media)) { 15601a1e1d21SSam Leffler case IFM_IEEE80211_11A: 1561b032f27cSSam Leffler *mode = IEEE80211_MODE_11A; 15621a1e1d21SSam Leffler break; 15631a1e1d21SSam Leffler case IFM_IEEE80211_11B: 1564b032f27cSSam Leffler *mode = IEEE80211_MODE_11B; 15651a1e1d21SSam Leffler break; 15661a1e1d21SSam Leffler case IFM_IEEE80211_11G: 1567b032f27cSSam Leffler *mode = IEEE80211_MODE_11G; 15681a1e1d21SSam Leffler break; 15694844aa7dSAtsushi Onoe case IFM_IEEE80211_FH: 1570b032f27cSSam Leffler *mode = IEEE80211_MODE_FH; 15714844aa7dSAtsushi Onoe break; 157268e8e04eSSam Leffler case IFM_IEEE80211_11NA: 1573b032f27cSSam Leffler *mode = IEEE80211_MODE_11NA; 157468e8e04eSSam Leffler break; 157568e8e04eSSam Leffler case IFM_IEEE80211_11NG: 1576b032f27cSSam Leffler *mode = IEEE80211_MODE_11NG; 157768e8e04eSSam Leffler break; 15781a1e1d21SSam Leffler case IFM_AUTO: 1579b032f27cSSam Leffler *mode = IEEE80211_MODE_AUTO; 15801a1e1d21SSam Leffler break; 15811a1e1d21SSam Leffler default: 1582b032f27cSSam Leffler return 0; 15831a1e1d21SSam Leffler } 15841a1e1d21SSam Leffler /* 15858a1b9b6aSSam Leffler * Turbo mode is an ``option''. 15868a1b9b6aSSam Leffler * XXX does not apply to AUTO 15871a1e1d21SSam Leffler */ 15881a1e1d21SSam Leffler if (ime->ifm_media & IFM_IEEE80211_TURBO) { 1589b032f27cSSam Leffler if (*mode == IEEE80211_MODE_11A) { 1590f945bd7aSSam Leffler if (flags & IEEE80211_F_TURBOP) 1591b032f27cSSam Leffler *mode = IEEE80211_MODE_TURBO_A; 159268e8e04eSSam Leffler else 1593b032f27cSSam Leffler *mode = IEEE80211_MODE_STURBO_A; 1594b032f27cSSam Leffler } else if (*mode == IEEE80211_MODE_11G) 1595b032f27cSSam Leffler *mode = IEEE80211_MODE_TURBO_G; 15968a1b9b6aSSam Leffler else 1597b032f27cSSam Leffler return 0; 15981a1e1d21SSam Leffler } 159968e8e04eSSam Leffler /* XXX HT40 +/- */ 1600b032f27cSSam Leffler return 1; 1601b032f27cSSam Leffler } 16021a1e1d21SSam Leffler 16031a1e1d21SSam Leffler /* 1604b032f27cSSam Leffler * Handle a media change request on the vap interface. 1605b032f27cSSam Leffler */ 1606b032f27cSSam Leffler int 1607b032f27cSSam Leffler ieee80211_media_change(struct ifnet *ifp) 1608b032f27cSSam Leffler { 1609b032f27cSSam Leffler struct ieee80211vap *vap = ifp->if_softc; 1610b032f27cSSam Leffler struct ifmedia_entry *ime = vap->iv_media.ifm_cur; 1611f945bd7aSSam Leffler uint16_t newmode; 1612b032f27cSSam Leffler 1613f945bd7aSSam Leffler if (!media2mode(ime, vap->iv_flags, &newmode)) 1614b032f27cSSam Leffler return EINVAL; 1615f945bd7aSSam Leffler if (vap->iv_des_mode != newmode) { 1616f945bd7aSSam Leffler vap->iv_des_mode = newmode; 16170a310468SSam Leffler /* XXX kick state machine if up+running */ 1618b032f27cSSam Leffler } 1619b032f27cSSam Leffler return 0; 1620b032f27cSSam Leffler } 1621b032f27cSSam Leffler 162268e8e04eSSam Leffler /* 162368e8e04eSSam Leffler * Common code to calculate the media status word 162468e8e04eSSam Leffler * from the operating mode and channel state. 162568e8e04eSSam Leffler */ 162668e8e04eSSam Leffler static int 162768e8e04eSSam Leffler media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan) 162868e8e04eSSam Leffler { 162968e8e04eSSam Leffler int status; 163068e8e04eSSam Leffler 163168e8e04eSSam Leffler status = IFM_IEEE80211; 163268e8e04eSSam Leffler switch (opmode) { 163368e8e04eSSam Leffler case IEEE80211_M_STA: 163468e8e04eSSam Leffler break; 163568e8e04eSSam Leffler case IEEE80211_M_IBSS: 163668e8e04eSSam Leffler status |= IFM_IEEE80211_ADHOC; 163768e8e04eSSam Leffler break; 163868e8e04eSSam Leffler case IEEE80211_M_HOSTAP: 163968e8e04eSSam Leffler status |= IFM_IEEE80211_HOSTAP; 164068e8e04eSSam Leffler break; 164168e8e04eSSam Leffler case IEEE80211_M_MONITOR: 164268e8e04eSSam Leffler status |= IFM_IEEE80211_MONITOR; 164368e8e04eSSam Leffler break; 164468e8e04eSSam Leffler case IEEE80211_M_AHDEMO: 164568e8e04eSSam Leffler status |= IFM_IEEE80211_ADHOC | IFM_FLAG0; 164668e8e04eSSam Leffler break; 164768e8e04eSSam Leffler case IEEE80211_M_WDS: 1648b032f27cSSam Leffler status |= IFM_IEEE80211_WDS; 164968e8e04eSSam Leffler break; 165059aa14a9SRui Paulo case IEEE80211_M_MBSS: 165159aa14a9SRui Paulo status |= IFM_IEEE80211_MBSS; 165259aa14a9SRui Paulo break; 165368e8e04eSSam Leffler } 165468e8e04eSSam Leffler if (IEEE80211_IS_CHAN_HTA(chan)) { 165568e8e04eSSam Leffler status |= IFM_IEEE80211_11NA; 165668e8e04eSSam Leffler } else if (IEEE80211_IS_CHAN_HTG(chan)) { 165768e8e04eSSam Leffler status |= IFM_IEEE80211_11NG; 165868e8e04eSSam Leffler } else if (IEEE80211_IS_CHAN_A(chan)) { 165968e8e04eSSam Leffler status |= IFM_IEEE80211_11A; 166068e8e04eSSam Leffler } else if (IEEE80211_IS_CHAN_B(chan)) { 166168e8e04eSSam Leffler status |= IFM_IEEE80211_11B; 166268e8e04eSSam Leffler } else if (IEEE80211_IS_CHAN_ANYG(chan)) { 166368e8e04eSSam Leffler status |= IFM_IEEE80211_11G; 166468e8e04eSSam Leffler } else if (IEEE80211_IS_CHAN_FHSS(chan)) { 166568e8e04eSSam Leffler status |= IFM_IEEE80211_FH; 166668e8e04eSSam Leffler } 166768e8e04eSSam Leffler /* XXX else complain? */ 166868e8e04eSSam Leffler 166968e8e04eSSam Leffler if (IEEE80211_IS_CHAN_TURBO(chan)) 167068e8e04eSSam Leffler status |= IFM_IEEE80211_TURBO; 1671b032f27cSSam Leffler #if 0 1672b032f27cSSam Leffler if (IEEE80211_IS_CHAN_HT20(chan)) 1673b032f27cSSam Leffler status |= IFM_IEEE80211_HT20; 1674b032f27cSSam Leffler if (IEEE80211_IS_CHAN_HT40(chan)) 1675b032f27cSSam Leffler status |= IFM_IEEE80211_HT40; 1676b032f27cSSam Leffler #endif 167768e8e04eSSam Leffler return status; 167868e8e04eSSam Leffler } 167968e8e04eSSam Leffler 16801a1e1d21SSam Leffler void 16811a1e1d21SSam Leffler ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr) 16821a1e1d21SSam Leffler { 1683b032f27cSSam Leffler struct ieee80211vap *vap = ifp->if_softc; 1684b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic; 168568e8e04eSSam Leffler enum ieee80211_phymode mode; 16861a1e1d21SSam Leffler 16871a1e1d21SSam Leffler imr->ifm_status = IFM_AVALID; 168868e8e04eSSam Leffler /* 168968e8e04eSSam Leffler * NB: use the current channel's mode to lock down a xmit 169068e8e04eSSam Leffler * rate only when running; otherwise we may have a mismatch 169168e8e04eSSam Leffler * in which case the rate will not be convertible. 169268e8e04eSSam Leffler */ 16939f098ac7SAdrian Chadd if (vap->iv_state == IEEE80211_S_RUN || 16949f098ac7SAdrian Chadd vap->iv_state == IEEE80211_S_SLEEP) { 16951a1e1d21SSam Leffler imr->ifm_status |= IFM_ACTIVE; 169668e8e04eSSam Leffler mode = ieee80211_chan2mode(ic->ic_curchan); 169768e8e04eSSam Leffler } else 169868e8e04eSSam Leffler mode = IEEE80211_MODE_AUTO; 1699b032f27cSSam Leffler imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan); 17008a1b9b6aSSam Leffler /* 17018a1b9b6aSSam Leffler * Calculate a current rate if possible. 17028a1b9b6aSSam Leffler */ 1703b032f27cSSam Leffler if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) { 17048a1b9b6aSSam Leffler /* 17058a1b9b6aSSam Leffler * A fixed rate is set, report that. 17068a1b9b6aSSam Leffler */ 17078a1b9b6aSSam Leffler imr->ifm_active |= ieee80211_rate2media(ic, 1708b032f27cSSam Leffler vap->iv_txparms[mode].ucastrate, mode); 1709b032f27cSSam Leffler } else if (vap->iv_opmode == IEEE80211_M_STA) { 17108a1b9b6aSSam Leffler /* 17118a1b9b6aSSam Leffler * In station mode report the current transmit rate. 17128a1b9b6aSSam Leffler */ 17138a1b9b6aSSam Leffler imr->ifm_active |= ieee80211_rate2media(ic, 1714b032f27cSSam Leffler vap->iv_bss->ni_txrate, mode); 1715ba99a9b1SAndre Oppermann } else 17161a1e1d21SSam Leffler imr->ifm_active |= IFM_AUTO; 1717b032f27cSSam Leffler if (imr->ifm_status & IFM_ACTIVE) 1718b032f27cSSam Leffler imr->ifm_current = imr->ifm_active; 17191a1e1d21SSam Leffler } 17201a1e1d21SSam Leffler 17211a1e1d21SSam Leffler /* 17221a1e1d21SSam Leffler * Set the current phy mode and recalculate the active channel 17231a1e1d21SSam Leffler * set based on the available channels for this mode. Also 17241a1e1d21SSam Leffler * select a new default/current channel if the current one is 17251a1e1d21SSam Leffler * inappropriate for this mode. 17261a1e1d21SSam Leffler */ 17271a1e1d21SSam Leffler int 17281a1e1d21SSam Leffler ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode) 17291a1e1d21SSam Leffler { 17301a1e1d21SSam Leffler /* 1731ca4ac7aeSSam Leffler * Adjust basic rates in 11b/11g supported rate set. 1732ca4ac7aeSSam Leffler * Note that if operating on a hal/quarter rate channel 1733ca4ac7aeSSam Leffler * this is a noop as those rates sets are different 1734ca4ac7aeSSam Leffler * and used instead. 17351a1e1d21SSam Leffler */ 1736ca4ac7aeSSam Leffler if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B) 1737b032f27cSSam Leffler ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode); 1738ca4ac7aeSSam Leffler 17391a1e1d21SSam Leffler ic->ic_curmode = mode; 17408a1b9b6aSSam Leffler ieee80211_reset_erp(ic); /* reset ERP state */ 17418a1b9b6aSSam Leffler 17421a1e1d21SSam Leffler return 0; 17431a1e1d21SSam Leffler } 17441a1e1d21SSam Leffler 17451a1e1d21SSam Leffler /* 174668e8e04eSSam Leffler * Return the phy mode for with the specified channel. 17471a1e1d21SSam Leffler */ 17481a1e1d21SSam Leffler enum ieee80211_phymode 174968e8e04eSSam Leffler ieee80211_chan2mode(const struct ieee80211_channel *chan) 17501a1e1d21SSam Leffler { 175168e8e04eSSam Leffler 175268e8e04eSSam Leffler if (IEEE80211_IS_CHAN_HTA(chan)) 175368e8e04eSSam Leffler return IEEE80211_MODE_11NA; 175468e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_HTG(chan)) 175568e8e04eSSam Leffler return IEEE80211_MODE_11NG; 175668e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_108G(chan)) 17578a1b9b6aSSam Leffler return IEEE80211_MODE_TURBO_G; 175868e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_ST(chan)) 175968e8e04eSSam Leffler return IEEE80211_MODE_STURBO_A; 176068e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_TURBO(chan)) 176168e8e04eSSam Leffler return IEEE80211_MODE_TURBO_A; 17626a76ae21SSam Leffler else if (IEEE80211_IS_CHAN_HALF(chan)) 17636a76ae21SSam Leffler return IEEE80211_MODE_HALF; 17646a76ae21SSam Leffler else if (IEEE80211_IS_CHAN_QUARTER(chan)) 17656a76ae21SSam Leffler return IEEE80211_MODE_QUARTER; 176668e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_A(chan)) 176768e8e04eSSam Leffler return IEEE80211_MODE_11A; 176868e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_ANYG(chan)) 17691a1e1d21SSam Leffler return IEEE80211_MODE_11G; 177068e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_B(chan)) 177168e8e04eSSam Leffler return IEEE80211_MODE_11B; 177268e8e04eSSam Leffler else if (IEEE80211_IS_CHAN_FHSS(chan)) 177368e8e04eSSam Leffler return IEEE80211_MODE_FH; 177468e8e04eSSam Leffler 177568e8e04eSSam Leffler /* NB: should not get here */ 177668e8e04eSSam Leffler printf("%s: cannot map channel to mode; freq %u flags 0x%x\n", 177768e8e04eSSam Leffler __func__, chan->ic_freq, chan->ic_flags); 17781a1e1d21SSam Leffler return IEEE80211_MODE_11B; 17791a1e1d21SSam Leffler } 17801a1e1d21SSam Leffler 178168e8e04eSSam Leffler struct ratemedia { 178268e8e04eSSam Leffler u_int match; /* rate + mode */ 178368e8e04eSSam Leffler u_int media; /* if_media rate */ 178468e8e04eSSam Leffler }; 178568e8e04eSSam Leffler 178668e8e04eSSam Leffler static int 178768e8e04eSSam Leffler findmedia(const struct ratemedia rates[], int n, u_int match) 178868e8e04eSSam Leffler { 178968e8e04eSSam Leffler int i; 179068e8e04eSSam Leffler 179168e8e04eSSam Leffler for (i = 0; i < n; i++) 179268e8e04eSSam Leffler if (rates[i].match == match) 179368e8e04eSSam Leffler return rates[i].media; 179468e8e04eSSam Leffler return IFM_AUTO; 179568e8e04eSSam Leffler } 179668e8e04eSSam Leffler 17971a1e1d21SSam Leffler /* 179868e8e04eSSam Leffler * Convert IEEE80211 rate value to ifmedia subtype. 179968e8e04eSSam Leffler * Rate is either a legacy rate in units of 0.5Mbps 180068e8e04eSSam Leffler * or an MCS index. 18011a1e1d21SSam Leffler */ 18021a1e1d21SSam Leffler int 18031a1e1d21SSam Leffler ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode) 18041a1e1d21SSam Leffler { 180568e8e04eSSam Leffler static const struct ratemedia rates[] = { 18064844aa7dSAtsushi Onoe { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 }, 18074844aa7dSAtsushi Onoe { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 }, 18084844aa7dSAtsushi Onoe { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 }, 18094844aa7dSAtsushi Onoe { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 }, 18104844aa7dSAtsushi Onoe { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 }, 18114844aa7dSAtsushi Onoe { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 }, 18124844aa7dSAtsushi Onoe { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 }, 18134844aa7dSAtsushi Onoe { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 }, 18144844aa7dSAtsushi Onoe { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 }, 18154844aa7dSAtsushi Onoe { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 }, 18164844aa7dSAtsushi Onoe { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 }, 18174844aa7dSAtsushi Onoe { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 }, 18184844aa7dSAtsushi Onoe { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 }, 18194844aa7dSAtsushi Onoe { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 }, 18204844aa7dSAtsushi Onoe { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 }, 18214844aa7dSAtsushi Onoe { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 }, 18224844aa7dSAtsushi Onoe { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 }, 18234844aa7dSAtsushi Onoe { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 }, 18244844aa7dSAtsushi Onoe { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 }, 18254844aa7dSAtsushi Onoe { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 }, 18264844aa7dSAtsushi Onoe { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 }, 18274844aa7dSAtsushi Onoe { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 }, 18284844aa7dSAtsushi Onoe { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 }, 18294844aa7dSAtsushi Onoe { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 }, 18304844aa7dSAtsushi Onoe { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 }, 18314844aa7dSAtsushi Onoe { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 }, 18324844aa7dSAtsushi Onoe { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 }, 183341b3c790SSam Leffler { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 }, 183441b3c790SSam Leffler { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 }, 183541b3c790SSam Leffler { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 }, 18361a1e1d21SSam Leffler /* NB: OFDM72 doesn't realy exist so we don't handle it */ 18371a1e1d21SSam Leffler }; 183868e8e04eSSam Leffler static const struct ratemedia htrates[] = { 183968e8e04eSSam Leffler { 0, IFM_IEEE80211_MCS }, 184068e8e04eSSam Leffler { 1, IFM_IEEE80211_MCS }, 184168e8e04eSSam Leffler { 2, IFM_IEEE80211_MCS }, 184268e8e04eSSam Leffler { 3, IFM_IEEE80211_MCS }, 184368e8e04eSSam Leffler { 4, IFM_IEEE80211_MCS }, 184468e8e04eSSam Leffler { 5, IFM_IEEE80211_MCS }, 184568e8e04eSSam Leffler { 6, IFM_IEEE80211_MCS }, 184668e8e04eSSam Leffler { 7, IFM_IEEE80211_MCS }, 184768e8e04eSSam Leffler { 8, IFM_IEEE80211_MCS }, 184868e8e04eSSam Leffler { 9, IFM_IEEE80211_MCS }, 184968e8e04eSSam Leffler { 10, IFM_IEEE80211_MCS }, 185068e8e04eSSam Leffler { 11, IFM_IEEE80211_MCS }, 185168e8e04eSSam Leffler { 12, IFM_IEEE80211_MCS }, 185268e8e04eSSam Leffler { 13, IFM_IEEE80211_MCS }, 185368e8e04eSSam Leffler { 14, IFM_IEEE80211_MCS }, 185468e8e04eSSam Leffler { 15, IFM_IEEE80211_MCS }, 1855f136f45fSBernhard Schmidt { 16, IFM_IEEE80211_MCS }, 1856f136f45fSBernhard Schmidt { 17, IFM_IEEE80211_MCS }, 1857f136f45fSBernhard Schmidt { 18, IFM_IEEE80211_MCS }, 1858f136f45fSBernhard Schmidt { 19, IFM_IEEE80211_MCS }, 1859f136f45fSBernhard Schmidt { 20, IFM_IEEE80211_MCS }, 1860f136f45fSBernhard Schmidt { 21, IFM_IEEE80211_MCS }, 1861f136f45fSBernhard Schmidt { 22, IFM_IEEE80211_MCS }, 1862f136f45fSBernhard Schmidt { 23, IFM_IEEE80211_MCS }, 1863f136f45fSBernhard Schmidt { 24, IFM_IEEE80211_MCS }, 1864f136f45fSBernhard Schmidt { 25, IFM_IEEE80211_MCS }, 1865f136f45fSBernhard Schmidt { 26, IFM_IEEE80211_MCS }, 1866f136f45fSBernhard Schmidt { 27, IFM_IEEE80211_MCS }, 1867f136f45fSBernhard Schmidt { 28, IFM_IEEE80211_MCS }, 1868f136f45fSBernhard Schmidt { 29, IFM_IEEE80211_MCS }, 1869f136f45fSBernhard Schmidt { 30, IFM_IEEE80211_MCS }, 1870f136f45fSBernhard Schmidt { 31, IFM_IEEE80211_MCS }, 1871f136f45fSBernhard Schmidt { 32, IFM_IEEE80211_MCS }, 1872f136f45fSBernhard Schmidt { 33, IFM_IEEE80211_MCS }, 1873f136f45fSBernhard Schmidt { 34, IFM_IEEE80211_MCS }, 1874f136f45fSBernhard Schmidt { 35, IFM_IEEE80211_MCS }, 1875f136f45fSBernhard Schmidt { 36, IFM_IEEE80211_MCS }, 1876f136f45fSBernhard Schmidt { 37, IFM_IEEE80211_MCS }, 1877f136f45fSBernhard Schmidt { 38, IFM_IEEE80211_MCS }, 1878f136f45fSBernhard Schmidt { 39, IFM_IEEE80211_MCS }, 1879f136f45fSBernhard Schmidt { 40, IFM_IEEE80211_MCS }, 1880f136f45fSBernhard Schmidt { 41, IFM_IEEE80211_MCS }, 1881f136f45fSBernhard Schmidt { 42, IFM_IEEE80211_MCS }, 1882f136f45fSBernhard Schmidt { 43, IFM_IEEE80211_MCS }, 1883f136f45fSBernhard Schmidt { 44, IFM_IEEE80211_MCS }, 1884f136f45fSBernhard Schmidt { 45, IFM_IEEE80211_MCS }, 1885f136f45fSBernhard Schmidt { 46, IFM_IEEE80211_MCS }, 1886f136f45fSBernhard Schmidt { 47, IFM_IEEE80211_MCS }, 1887f136f45fSBernhard Schmidt { 48, IFM_IEEE80211_MCS }, 1888f136f45fSBernhard Schmidt { 49, IFM_IEEE80211_MCS }, 1889f136f45fSBernhard Schmidt { 50, IFM_IEEE80211_MCS }, 1890f136f45fSBernhard Schmidt { 51, IFM_IEEE80211_MCS }, 1891f136f45fSBernhard Schmidt { 52, IFM_IEEE80211_MCS }, 1892f136f45fSBernhard Schmidt { 53, IFM_IEEE80211_MCS }, 1893f136f45fSBernhard Schmidt { 54, IFM_IEEE80211_MCS }, 1894f136f45fSBernhard Schmidt { 55, IFM_IEEE80211_MCS }, 1895f136f45fSBernhard Schmidt { 56, IFM_IEEE80211_MCS }, 1896f136f45fSBernhard Schmidt { 57, IFM_IEEE80211_MCS }, 1897f136f45fSBernhard Schmidt { 58, IFM_IEEE80211_MCS }, 1898f136f45fSBernhard Schmidt { 59, IFM_IEEE80211_MCS }, 1899f136f45fSBernhard Schmidt { 60, IFM_IEEE80211_MCS }, 1900f136f45fSBernhard Schmidt { 61, IFM_IEEE80211_MCS }, 1901f136f45fSBernhard Schmidt { 62, IFM_IEEE80211_MCS }, 1902f136f45fSBernhard Schmidt { 63, IFM_IEEE80211_MCS }, 1903f136f45fSBernhard Schmidt { 64, IFM_IEEE80211_MCS }, 1904f136f45fSBernhard Schmidt { 65, IFM_IEEE80211_MCS }, 1905f136f45fSBernhard Schmidt { 66, IFM_IEEE80211_MCS }, 1906f136f45fSBernhard Schmidt { 67, IFM_IEEE80211_MCS }, 1907f136f45fSBernhard Schmidt { 68, IFM_IEEE80211_MCS }, 1908f136f45fSBernhard Schmidt { 69, IFM_IEEE80211_MCS }, 1909f136f45fSBernhard Schmidt { 70, IFM_IEEE80211_MCS }, 1910f136f45fSBernhard Schmidt { 71, IFM_IEEE80211_MCS }, 1911f136f45fSBernhard Schmidt { 72, IFM_IEEE80211_MCS }, 1912f136f45fSBernhard Schmidt { 73, IFM_IEEE80211_MCS }, 1913f136f45fSBernhard Schmidt { 74, IFM_IEEE80211_MCS }, 1914f136f45fSBernhard Schmidt { 75, IFM_IEEE80211_MCS }, 1915f136f45fSBernhard Schmidt { 76, IFM_IEEE80211_MCS }, 191668e8e04eSSam Leffler }; 191768e8e04eSSam Leffler int m; 19181a1e1d21SSam Leffler 191968e8e04eSSam Leffler /* 192068e8e04eSSam Leffler * Check 11n rates first for match as an MCS. 192168e8e04eSSam Leffler */ 192268e8e04eSSam Leffler if (mode == IEEE80211_MODE_11NA) { 1923f0ee92d5SSam Leffler if (rate & IEEE80211_RATE_MCS) { 1924f0ee92d5SSam Leffler rate &= ~IEEE80211_RATE_MCS; 1925a3e08d6fSRui Paulo m = findmedia(htrates, nitems(htrates), rate); 192668e8e04eSSam Leffler if (m != IFM_AUTO) 192768e8e04eSSam Leffler return m | IFM_IEEE80211_11NA; 192868e8e04eSSam Leffler } 192968e8e04eSSam Leffler } else if (mode == IEEE80211_MODE_11NG) { 193068e8e04eSSam Leffler /* NB: 12 is ambiguous, it will be treated as an MCS */ 1931f0ee92d5SSam Leffler if (rate & IEEE80211_RATE_MCS) { 1932f0ee92d5SSam Leffler rate &= ~IEEE80211_RATE_MCS; 1933a3e08d6fSRui Paulo m = findmedia(htrates, nitems(htrates), rate); 193468e8e04eSSam Leffler if (m != IFM_AUTO) 193568e8e04eSSam Leffler return m | IFM_IEEE80211_11NG; 193668e8e04eSSam Leffler } 193768e8e04eSSam Leffler } 193868e8e04eSSam Leffler rate &= IEEE80211_RATE_VAL; 19391a1e1d21SSam Leffler switch (mode) { 19401a1e1d21SSam Leffler case IEEE80211_MODE_11A: 19416a76ae21SSam Leffler case IEEE80211_MODE_HALF: /* XXX good 'nuf */ 19426a76ae21SSam Leffler case IEEE80211_MODE_QUARTER: 194368e8e04eSSam Leffler case IEEE80211_MODE_11NA: 19448a1b9b6aSSam Leffler case IEEE80211_MODE_TURBO_A: 194568e8e04eSSam Leffler case IEEE80211_MODE_STURBO_A: 1946a3e08d6fSRui Paulo return findmedia(rates, nitems(rates), 1947a3e08d6fSRui Paulo rate | IFM_IEEE80211_11A); 19481a1e1d21SSam Leffler case IEEE80211_MODE_11B: 1949a3e08d6fSRui Paulo return findmedia(rates, nitems(rates), 1950a3e08d6fSRui Paulo rate | IFM_IEEE80211_11B); 19514844aa7dSAtsushi Onoe case IEEE80211_MODE_FH: 1952a3e08d6fSRui Paulo return findmedia(rates, nitems(rates), 1953a3e08d6fSRui Paulo rate | IFM_IEEE80211_FH); 19541a1e1d21SSam Leffler case IEEE80211_MODE_AUTO: 19551a1e1d21SSam Leffler /* NB: ic may be NULL for some drivers */ 1956566d825bSSam Leffler if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH) 1957a3e08d6fSRui Paulo return findmedia(rates, nitems(rates), 195868e8e04eSSam Leffler rate | IFM_IEEE80211_FH); 19591a1e1d21SSam Leffler /* NB: hack, 11g matches both 11b+11a rates */ 19601a1e1d21SSam Leffler /* fall thru... */ 19611a1e1d21SSam Leffler case IEEE80211_MODE_11G: 196268e8e04eSSam Leffler case IEEE80211_MODE_11NG: 19638a1b9b6aSSam Leffler case IEEE80211_MODE_TURBO_G: 1964a3e08d6fSRui Paulo return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G); 19651a1e1d21SSam Leffler } 19661a1e1d21SSam Leffler return IFM_AUTO; 19671a1e1d21SSam Leffler } 19681a1e1d21SSam Leffler 19691a1e1d21SSam Leffler int 19701a1e1d21SSam Leffler ieee80211_media2rate(int mword) 19711a1e1d21SSam Leffler { 19721a1e1d21SSam Leffler static const int ieeerates[] = { 19731a1e1d21SSam Leffler -1, /* IFM_AUTO */ 19741a1e1d21SSam Leffler 0, /* IFM_MANUAL */ 19751a1e1d21SSam Leffler 0, /* IFM_NONE */ 19761a1e1d21SSam Leffler 2, /* IFM_IEEE80211_FH1 */ 19771a1e1d21SSam Leffler 4, /* IFM_IEEE80211_FH2 */ 19781a1e1d21SSam Leffler 2, /* IFM_IEEE80211_DS1 */ 19791a1e1d21SSam Leffler 4, /* IFM_IEEE80211_DS2 */ 19801a1e1d21SSam Leffler 11, /* IFM_IEEE80211_DS5 */ 19811a1e1d21SSam Leffler 22, /* IFM_IEEE80211_DS11 */ 19821a1e1d21SSam Leffler 44, /* IFM_IEEE80211_DS22 */ 19831a1e1d21SSam Leffler 12, /* IFM_IEEE80211_OFDM6 */ 19841a1e1d21SSam Leffler 18, /* IFM_IEEE80211_OFDM9 */ 19851a1e1d21SSam Leffler 24, /* IFM_IEEE80211_OFDM12 */ 19861a1e1d21SSam Leffler 36, /* IFM_IEEE80211_OFDM18 */ 19871a1e1d21SSam Leffler 48, /* IFM_IEEE80211_OFDM24 */ 19881a1e1d21SSam Leffler 72, /* IFM_IEEE80211_OFDM36 */ 19891a1e1d21SSam Leffler 96, /* IFM_IEEE80211_OFDM48 */ 19901a1e1d21SSam Leffler 108, /* IFM_IEEE80211_OFDM54 */ 19911a1e1d21SSam Leffler 144, /* IFM_IEEE80211_OFDM72 */ 199241b3c790SSam Leffler 0, /* IFM_IEEE80211_DS354k */ 199341b3c790SSam Leffler 0, /* IFM_IEEE80211_DS512k */ 199441b3c790SSam Leffler 6, /* IFM_IEEE80211_OFDM3 */ 199541b3c790SSam Leffler 9, /* IFM_IEEE80211_OFDM4 */ 199641b3c790SSam Leffler 54, /* IFM_IEEE80211_OFDM27 */ 199768e8e04eSSam Leffler -1, /* IFM_IEEE80211_MCS */ 19981a1e1d21SSam Leffler }; 1999a3e08d6fSRui Paulo return IFM_SUBTYPE(mword) < nitems(ieeerates) ? 20001a1e1d21SSam Leffler ieeerates[IFM_SUBTYPE(mword)] : 0; 20011a1e1d21SSam Leffler } 20025b16c28cSSam Leffler 20035b16c28cSSam Leffler /* 20045b16c28cSSam Leffler * The following hash function is adapted from "Hash Functions" by Bob Jenkins 20055b16c28cSSam Leffler * ("Algorithm Alley", Dr. Dobbs Journal, September 1997). 20065b16c28cSSam Leffler */ 20075b16c28cSSam Leffler #define mix(a, b, c) \ 20085b16c28cSSam Leffler do { \ 20095b16c28cSSam Leffler a -= b; a -= c; a ^= (c >> 13); \ 20105b16c28cSSam Leffler b -= c; b -= a; b ^= (a << 8); \ 20115b16c28cSSam Leffler c -= a; c -= b; c ^= (b >> 13); \ 20125b16c28cSSam Leffler a -= b; a -= c; a ^= (c >> 12); \ 20135b16c28cSSam Leffler b -= c; b -= a; b ^= (a << 16); \ 20145b16c28cSSam Leffler c -= a; c -= b; c ^= (b >> 5); \ 20155b16c28cSSam Leffler a -= b; a -= c; a ^= (c >> 3); \ 20165b16c28cSSam Leffler b -= c; b -= a; b ^= (a << 10); \ 20175b16c28cSSam Leffler c -= a; c -= b; c ^= (b >> 15); \ 20185b16c28cSSam Leffler } while (/*CONSTCOND*/0) 20195b16c28cSSam Leffler 20205b16c28cSSam Leffler uint32_t 20215b16c28cSSam Leffler ieee80211_mac_hash(const struct ieee80211com *ic, 20225b16c28cSSam Leffler const uint8_t addr[IEEE80211_ADDR_LEN]) 20235b16c28cSSam Leffler { 20245b16c28cSSam Leffler uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key; 20255b16c28cSSam Leffler 20265b16c28cSSam Leffler b += addr[5] << 8; 20275b16c28cSSam Leffler b += addr[4]; 20285b16c28cSSam Leffler a += addr[3] << 24; 20295b16c28cSSam Leffler a += addr[2] << 16; 20305b16c28cSSam Leffler a += addr[1] << 8; 20315b16c28cSSam Leffler a += addr[0]; 20325b16c28cSSam Leffler 20335b16c28cSSam Leffler mix(a, b, c); 20345b16c28cSSam Leffler 20355b16c28cSSam Leffler return c; 20365b16c28cSSam Leffler } 20375b16c28cSSam Leffler #undef mix 2038a1cbd043SAdrian Chadd 2039a1cbd043SAdrian Chadd char 2040a1cbd043SAdrian Chadd ieee80211_channel_type_char(const struct ieee80211_channel *c) 2041a1cbd043SAdrian Chadd { 2042a1cbd043SAdrian Chadd if (IEEE80211_IS_CHAN_ST(c)) 2043a1cbd043SAdrian Chadd return 'S'; 2044a1cbd043SAdrian Chadd if (IEEE80211_IS_CHAN_108A(c)) 2045a1cbd043SAdrian Chadd return 'T'; 2046a1cbd043SAdrian Chadd if (IEEE80211_IS_CHAN_108G(c)) 2047a1cbd043SAdrian Chadd return 'G'; 2048a1cbd043SAdrian Chadd if (IEEE80211_IS_CHAN_HT(c)) 2049a1cbd043SAdrian Chadd return 'n'; 2050a1cbd043SAdrian Chadd if (IEEE80211_IS_CHAN_A(c)) 2051a1cbd043SAdrian Chadd return 'a'; 2052a1cbd043SAdrian Chadd if (IEEE80211_IS_CHAN_ANYG(c)) 2053a1cbd043SAdrian Chadd return 'g'; 2054a1cbd043SAdrian Chadd if (IEEE80211_IS_CHAN_B(c)) 2055a1cbd043SAdrian Chadd return 'b'; 2056a1cbd043SAdrian Chadd return 'f'; 2057a1cbd043SAdrian Chadd } 2058