ieee80211.c revision 187897
1116742Ssam/*- 2116904Ssam * Copyright (c) 2001 Atsushi Onoe 3186904Ssam * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting 4116742Ssam * All rights reserved. 5116742Ssam * 6116742Ssam * Redistribution and use in source and binary forms, with or without 7116742Ssam * modification, are permitted provided that the following conditions 8116742Ssam * are met: 9116742Ssam * 1. Redistributions of source code must retain the above copyright 10116904Ssam * notice, this list of conditions and the following disclaimer. 11116904Ssam * 2. Redistributions in binary form must reproduce the above copyright 12116904Ssam * notice, this list of conditions and the following disclaimer in the 13116904Ssam * documentation and/or other materials provided with the distribution. 14116742Ssam * 15116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24116904Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25116742Ssam */ 26116742Ssam 27116742Ssam#include <sys/cdefs.h> 28116742Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211.c 187897 2009-01-29 23:09:12Z sam $"); 29116742Ssam 30116742Ssam/* 31116742Ssam * IEEE 802.11 generic handler 32116742Ssam */ 33178354Ssam#include "opt_wlan.h" 34116742Ssam 35116742Ssam#include <sys/param.h> 36116742Ssam#include <sys/systm.h> 37116742Ssam#include <sys/kernel.h> 38182742Sbrooks 39116742Ssam#include <sys/socket.h> 40116742Ssam 41116742Ssam#include <net/if.h> 42178354Ssam#include <net/if_dl.h> 43116742Ssam#include <net/if_media.h> 44178354Ssam#include <net/if_types.h> 45116742Ssam#include <net/ethernet.h> 46116742Ssam 47116742Ssam#include <net80211/ieee80211_var.h> 48178354Ssam#include <net80211/ieee80211_regdomain.h> 49116742Ssam 50116742Ssam#include <net/bpf.h> 51116742Ssam 52178955Ssamconst char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = { 53178955Ssam [IEEE80211_MODE_AUTO] = "auto", 54178955Ssam [IEEE80211_MODE_11A] = "11a", 55178955Ssam [IEEE80211_MODE_11B] = "11b", 56178955Ssam [IEEE80211_MODE_11G] = "11g", 57178955Ssam [IEEE80211_MODE_FH] = "FH", 58178955Ssam [IEEE80211_MODE_TURBO_A] = "turboA", 59178955Ssam [IEEE80211_MODE_TURBO_G] = "turboG", 60178955Ssam [IEEE80211_MODE_STURBO_A] = "sturboA", 61178955Ssam [IEEE80211_MODE_11NA] = "11na", 62178955Ssam [IEEE80211_MODE_11NG] = "11ng", 63116742Ssam}; 64178957Ssam/* map ieee80211_opmode to the corresponding capability bit */ 65178957Ssamconst int ieee80211_opcap[IEEE80211_OPMODE_MAX] = { 66178957Ssam [IEEE80211_M_IBSS] = IEEE80211_C_IBSS, 67178957Ssam [IEEE80211_M_WDS] = IEEE80211_C_WDS, 68178957Ssam [IEEE80211_M_STA] = IEEE80211_C_STA, 69178957Ssam [IEEE80211_M_AHDEMO] = IEEE80211_C_AHDEMO, 70178957Ssam [IEEE80211_M_HOSTAP] = IEEE80211_C_HOSTAP, 71178957Ssam [IEEE80211_M_MONITOR] = IEEE80211_C_MONITOR, 72178957Ssam}; 73178957Ssam 74178354Ssamstatic const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] = 75178354Ssam { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 76116742Ssam 77178354Ssamstatic void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag); 78178354Ssamstatic void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag); 79178354Ssamstatic int ieee80211_media_setup(struct ieee80211com *ic, 80178354Ssam struct ifmedia *media, int caps, int addsta, 81178354Ssam ifm_change_cb_t media_change, ifm_stat_cb_t media_stat); 82178354Ssamstatic void ieee80211com_media_status(struct ifnet *, struct ifmediareq *); 83178354Ssamstatic int ieee80211com_media_change(struct ifnet *); 84178354Ssamstatic int media_status(enum ieee80211_opmode, 85178354Ssam const struct ieee80211_channel *); 86178354Ssam 87178354SsamMALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state"); 88178354Ssam 89164645Ssam/* 90164645Ssam * Default supported rates for 802.11 operation (in IEEE .5Mb units). 91164645Ssam */ 92164645Ssam#define B(r) ((r) | IEEE80211_RATE_BASIC) 93164645Ssamstatic const struct ieee80211_rateset ieee80211_rateset_11a = 94164645Ssam { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } }; 95165569Ssamstatic const struct ieee80211_rateset ieee80211_rateset_half = 96165569Ssam { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } }; 97165569Ssamstatic const struct ieee80211_rateset ieee80211_rateset_quarter = 98165569Ssam { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } }; 99164645Ssamstatic const struct ieee80211_rateset ieee80211_rateset_11b = 100164645Ssam { 4, { B(2), B(4), B(11), B(22) } }; 101164645Ssam/* NB: OFDM rates are handled specially based on mode */ 102164645Ssamstatic const struct ieee80211_rateset ieee80211_rateset_11g = 103164645Ssam { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } }; 104164645Ssam#undef B 105164645Ssam 106140915Ssam/* 107165569Ssam * Fill in 802.11 available channel set, mark 108165569Ssam * all available channels as active, and pick 109165569Ssam * a default channel if not already specified. 110165569Ssam */ 111165569Ssamstatic void 112165569Ssamieee80211_chan_init(struct ieee80211com *ic) 113116742Ssam{ 114165569Ssam#define DEFAULTRATES(m, def) do { \ 115167468Ssam if (isset(ic->ic_modecaps, m) && ic->ic_sup_rates[m].rs_nrates == 0) \ 116165574Ssam ic->ic_sup_rates[m] = def; \ 117165569Ssam} while (0) 118116742Ssam struct ieee80211_channel *c; 119116742Ssam int i; 120116742Ssam 121186107Ssam KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX, 122170530Ssam ("invalid number of channels specified: %u", ic->ic_nchans)); 123116742Ssam memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail)); 124178354Ssam memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps)); 125167468Ssam setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO); 126170530Ssam for (i = 0; i < ic->ic_nchans; i++) { 127116742Ssam c = &ic->ic_channels[i]; 128170530Ssam KASSERT(c->ic_flags != 0, ("channel with no flags")); 129187796Ssam /* 130187796Ssam * Help drivers that work only with frequencies by filling 131187796Ssam * in IEEE channel #'s if not already calculated. Note this 132187796Ssam * mimics similar work done in ieee80211_setregdomain when 133187796Ssam * changing regulatory state. 134187796Ssam */ 135187796Ssam if (c->ic_ieee == 0) 136187796Ssam c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags); 137187796Ssam if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0) 138187796Ssam c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq + 139187796Ssam (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20), 140187796Ssam c->ic_flags); 141187796Ssam /* default max tx power to max regulatory */ 142187796Ssam if (c->ic_maxpower == 0) 143187796Ssam c->ic_maxpower = 2*c->ic_maxregpower; 144170530Ssam setbit(ic->ic_chan_avail, c->ic_ieee); 145170530Ssam /* 146170530Ssam * Identify mode capabilities. 147170530Ssam */ 148170530Ssam if (IEEE80211_IS_CHAN_A(c)) 149170530Ssam setbit(ic->ic_modecaps, IEEE80211_MODE_11A); 150170530Ssam if (IEEE80211_IS_CHAN_B(c)) 151170530Ssam setbit(ic->ic_modecaps, IEEE80211_MODE_11B); 152170530Ssam if (IEEE80211_IS_CHAN_ANYG(c)) 153170530Ssam setbit(ic->ic_modecaps, IEEE80211_MODE_11G); 154170530Ssam if (IEEE80211_IS_CHAN_FHSS(c)) 155170530Ssam setbit(ic->ic_modecaps, IEEE80211_MODE_FH); 156170530Ssam if (IEEE80211_IS_CHAN_108A(c)) 157170530Ssam setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A); 158170530Ssam if (IEEE80211_IS_CHAN_108G(c)) 159170530Ssam setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G); 160170530Ssam if (IEEE80211_IS_CHAN_ST(c)) 161170530Ssam setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A); 162170530Ssam if (IEEE80211_IS_CHAN_HTA(c)) 163170530Ssam setbit(ic->ic_modecaps, IEEE80211_MODE_11NA); 164170530Ssam if (IEEE80211_IS_CHAN_HTG(c)) 165170530Ssam setbit(ic->ic_modecaps, IEEE80211_MODE_11NG); 166116742Ssam } 167170530Ssam /* initialize candidate channels to all available */ 168170530Ssam memcpy(ic->ic_chan_active, ic->ic_chan_avail, 169170530Ssam sizeof(ic->ic_chan_avail)); 170164645Ssam 171178354Ssam /* sort channel table to allow lookup optimizations */ 172178354Ssam ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans); 173178354Ssam 174178354Ssam /* invalidate any previous state */ 175170530Ssam ic->ic_bsschan = IEEE80211_CHAN_ANYC; 176172233Ssam ic->ic_prevchan = NULL; 177178354Ssam ic->ic_csa_newchan = NULL; 178170530Ssam /* arbitrarily pick the first channel */ 179170530Ssam ic->ic_curchan = &ic->ic_channels[0]; 180170530Ssam 181164645Ssam /* fillin well-known rate sets if driver has not specified */ 182165569Ssam DEFAULTRATES(IEEE80211_MODE_11B, ieee80211_rateset_11b); 183165569Ssam DEFAULTRATES(IEEE80211_MODE_11G, ieee80211_rateset_11g); 184165569Ssam DEFAULTRATES(IEEE80211_MODE_11A, ieee80211_rateset_11a); 185165569Ssam DEFAULTRATES(IEEE80211_MODE_TURBO_A, ieee80211_rateset_11a); 186165569Ssam DEFAULTRATES(IEEE80211_MODE_TURBO_G, ieee80211_rateset_11g); 187187897Ssam DEFAULTRATES(IEEE80211_MODE_STURBO_A, ieee80211_rateset_11a); 188165569Ssam 189165569Ssam /* 190165569Ssam * Set auto mode to reset active channel state and any desired channel. 191165569Ssam */ 192165569Ssam (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO); 193165569Ssam#undef DEFAULTRATES 194165569Ssam} 195165569Ssam 196178354Ssamstatic void 197178354Ssamnull_update_mcast(struct ifnet *ifp) 198178354Ssam{ 199178354Ssam if_printf(ifp, "need multicast update callback\n"); 200178354Ssam} 201178354Ssam 202178354Ssamstatic void 203178354Ssamnull_update_promisc(struct ifnet *ifp) 204178354Ssam{ 205178354Ssam if_printf(ifp, "need promiscuous mode update callback\n"); 206178354Ssam} 207178354Ssam 208178521Ssamstatic int 209178521Ssamnull_output(struct ifnet *ifp, struct mbuf *m, 210178521Ssam struct sockaddr *dst, struct rtentry *rt0) 211178521Ssam{ 212178521Ssam if_printf(ifp, "discard raw packet\n"); 213178521Ssam m_freem(m); 214178521Ssam return EIO; 215178521Ssam} 216178521Ssam 217178521Ssamstatic void 218178521Ssamnull_input(struct ifnet *ifp, struct mbuf *m) 219178521Ssam{ 220178521Ssam if_printf(ifp, "if_input should not be called\n"); 221178521Ssam m_freem(m); 222178521Ssam} 223178521Ssam 224178354Ssam/* 225178354Ssam * Attach/setup the common net80211 state. Called by 226178354Ssam * the driver on attach to prior to creating any vap's. 227178354Ssam */ 228165569Ssamvoid 229165569Ssamieee80211_ifattach(struct ieee80211com *ic) 230165569Ssam{ 231165569Ssam struct ifnet *ifp = ic->ic_ifp; 232178354Ssam struct sockaddr_dl *sdl; 233178354Ssam struct ifaddr *ifa; 234165569Ssam 235178354Ssam KASSERT(ifp->if_type == IFT_IEEE80211, ("if_type %d", ifp->if_type)); 236165569Ssam 237179388Ssam IEEE80211_LOCK_INIT(ic, ifp->if_xname); 238178354Ssam TAILQ_INIT(&ic->ic_vaps); 239165569Ssam /* 240165569Ssam * Fill in 802.11 available channel set, mark all 241165569Ssam * available channels as active, and pick a default 242165569Ssam * channel if not already specified. 243165569Ssam */ 244178354Ssam ieee80211_media_init(ic); 245170530Ssam 246178354Ssam ic->ic_update_mcast = null_update_mcast; 247178354Ssam ic->ic_update_promisc = null_update_promisc; 248116742Ssam 249155688Ssam ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT; 250155688Ssam ic->ic_lintval = ic->ic_bintval; 251138568Ssam ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX; 252138568Ssam 253170530Ssam ieee80211_crypto_attach(ic); 254138568Ssam ieee80211_node_attach(ic); 255170530Ssam ieee80211_power_attach(ic); 256138568Ssam ieee80211_proto_attach(ic); 257170530Ssam ieee80211_ht_attach(ic); 258170530Ssam ieee80211_scan_attach(ic); 259178354Ssam ieee80211_regdomain_attach(ic); 260138568Ssam 261178354Ssam ieee80211_sysctl_attach(ic); 262138568Ssam 263178354Ssam ifp->if_addrlen = IEEE80211_ADDR_LEN; 264178354Ssam ifp->if_hdrlen = 0; 265178354Ssam if_attach(ifp); 266178354Ssam ifp->if_mtu = IEEE80211_MTU_MAX; 267178354Ssam ifp->if_broadcastaddr = ieee80211broadcastaddr; 268178521Ssam ifp->if_output = null_output; 269178521Ssam ifp->if_input = null_input; /* just in case */ 270178521Ssam ifp->if_resolvemulti = NULL; /* NB: callers check */ 271140915Ssam 272178354Ssam ifa = ifaddr_byindex(ifp->if_index); 273178354Ssam KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__)); 274178354Ssam sdl = (struct sockaddr_dl *)ifa->ifa_addr; 275178354Ssam sdl->sdl_type = IFT_ETHER; /* XXX IFT_IEEE80211? */ 276178354Ssam sdl->sdl_alen = IEEE80211_ADDR_LEN; 277178354Ssam IEEE80211_ADDR_COPY(LLADDR(sdl), ic->ic_myaddr); 278116742Ssam} 279116742Ssam 280178354Ssam/* 281178354Ssam * Detach net80211 state on device detach. Tear down 282178354Ssam * all vap's and reclaim all common state prior to the 283178354Ssam * device state going away. Note we may call back into 284178354Ssam * driver; it must be prepared for this. 285178354Ssam */ 286116742Ssamvoid 287138568Ssamieee80211_ifdetach(struct ieee80211com *ic) 288116742Ssam{ 289138568Ssam struct ifnet *ifp = ic->ic_ifp; 290178354Ssam struct ieee80211vap *vap; 291116742Ssam 292178354Ssam /* XXX ieee80211_stop_all? */ 293178354Ssam while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL) 294178354Ssam ieee80211_vap_destroy(vap); 295138568Ssam 296138568Ssam ieee80211_sysctl_detach(ic); 297178354Ssam ieee80211_regdomain_detach(ic); 298170530Ssam ieee80211_scan_detach(ic); 299170530Ssam ieee80211_ht_detach(ic); 300166012Ssam /* NB: must be called before ieee80211_node_detach */ 301138568Ssam ieee80211_proto_detach(ic); 302138568Ssam ieee80211_crypto_detach(ic); 303170530Ssam ieee80211_power_detach(ic); 304138568Ssam ieee80211_node_detach(ic); 305116742Ssam ifmedia_removeall(&ic->ic_media); 306138568Ssam 307170530Ssam IEEE80211_LOCK_DESTROY(ic); 308178354Ssam if_detach(ifp); 309178354Ssam} 310138568Ssam 311178354Ssam/* 312178354Ssam * Default reset method for use with the ioctl support. This 313178354Ssam * method is invoked after any state change in the 802.11 314178354Ssam * layer that should be propagated to the hardware but not 315178354Ssam * require re-initialization of the 802.11 state machine (e.g 316178354Ssam * rescanning for an ap). We always return ENETRESET which 317178354Ssam * should cause the driver to re-initialize the device. Drivers 318178354Ssam * can override this method to implement more optimized support. 319178354Ssam */ 320178354Ssamstatic int 321178354Ssamdefault_reset(struct ieee80211vap *vap, u_long cmd) 322178354Ssam{ 323178354Ssam return ENETRESET; 324178354Ssam} 325178354Ssam 326178354Ssam/* 327178354Ssam * Prepare a vap for use. Drivers use this call to 328178354Ssam * setup net80211 state in new vap's prior attaching 329178354Ssam * them with ieee80211_vap_attach (below). 330178354Ssam */ 331178354Ssamint 332178354Ssamieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap, 333178354Ssam const char name[IFNAMSIZ], int unit, int opmode, int flags, 334178354Ssam const uint8_t bssid[IEEE80211_ADDR_LEN], 335178354Ssam const uint8_t macaddr[IEEE80211_ADDR_LEN]) 336178354Ssam{ 337178354Ssam struct ifnet *ifp; 338178354Ssam 339178354Ssam ifp = if_alloc(IFT_ETHER); 340178354Ssam if (ifp == NULL) { 341178354Ssam if_printf(ic->ic_ifp, "%s: unable to allocate ifnet\n", 342178354Ssam __func__); 343178354Ssam return ENOMEM; 344178354Ssam } 345178354Ssam if_initname(ifp, name, unit); 346178354Ssam ifp->if_softc = vap; /* back pointer */ 347178354Ssam ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST; 348178354Ssam ifp->if_start = ieee80211_start; 349178354Ssam ifp->if_ioctl = ieee80211_ioctl; 350178354Ssam ifp->if_watchdog = NULL; /* NB: no watchdog routine */ 351178354Ssam ifp->if_init = ieee80211_init; 352178354Ssam /* NB: input+output filled in by ether_ifattach */ 353178354Ssam IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 354178354Ssam ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 355178354Ssam IFQ_SET_READY(&ifp->if_snd); 356178354Ssam 357178354Ssam vap->iv_ifp = ifp; 358178354Ssam vap->iv_ic = ic; 359178354Ssam vap->iv_flags = ic->ic_flags; /* propagate common flags */ 360178354Ssam vap->iv_flags_ext = ic->ic_flags_ext; 361178354Ssam vap->iv_flags_ven = ic->ic_flags_ven; 362178354Ssam vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE; 363178354Ssam vap->iv_htcaps = ic->ic_htcaps; 364178354Ssam vap->iv_opmode = opmode; 365178957Ssam vap->iv_caps |= ieee80211_opcap[opmode]; 366178354Ssam switch (opmode) { 367178354Ssam case IEEE80211_M_WDS: 368178354Ssam /* 369178354Ssam * WDS links must specify the bssid of the far end. 370178354Ssam * For legacy operation this is a static relationship. 371178354Ssam * For non-legacy operation the station must associate 372178354Ssam * and be authorized to pass traffic. Plumbing the 373178354Ssam * vap to the proper node happens when the vap 374178354Ssam * transitions to RUN state. 375178354Ssam */ 376178354Ssam IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid); 377178354Ssam vap->iv_flags |= IEEE80211_F_DESBSSID; 378178354Ssam if (flags & IEEE80211_CLONE_WDSLEGACY) 379178354Ssam vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY; 380178354Ssam break; 381186904Ssam#ifdef IEEE80211_SUPPORT_TDMA 382186904Ssam case IEEE80211_M_AHDEMO: 383186904Ssam if (flags & IEEE80211_CLONE_TDMA) { 384186904Ssam /* NB: checked before clone operation allowed */ 385186904Ssam KASSERT(ic->ic_caps & IEEE80211_C_TDMA, 386186904Ssam ("not TDMA capable, ic_caps 0x%x", ic->ic_caps)); 387186904Ssam /* 388186904Ssam * Propagate TDMA capability to mark vap; this 389186904Ssam * cannot be removed and is used to distinguish 390186904Ssam * regular ahdemo operation from ahdemo+tdma. 391186904Ssam */ 392186904Ssam vap->iv_caps |= IEEE80211_C_TDMA; 393186904Ssam } 394186904Ssam break; 395186904Ssam#endif 396178354Ssam } 397184278Ssam /* auto-enable s/w beacon miss support */ 398184278Ssam if (flags & IEEE80211_CLONE_NOBEACONS) 399184278Ssam vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS; 400178354Ssam /* 401178354Ssam * Enable various functionality by default if we're 402178354Ssam * capable; the driver can override us if it knows better. 403178354Ssam */ 404178354Ssam if (vap->iv_caps & IEEE80211_C_WME) 405178354Ssam vap->iv_flags |= IEEE80211_F_WME; 406178354Ssam if (vap->iv_caps & IEEE80211_C_BURST) 407178354Ssam vap->iv_flags |= IEEE80211_F_BURST; 408178354Ssam if (vap->iv_caps & IEEE80211_C_FF) 409178354Ssam vap->iv_flags |= IEEE80211_F_FF; 410178354Ssam if (vap->iv_caps & IEEE80211_C_TURBOP) 411178354Ssam vap->iv_flags |= IEEE80211_F_TURBOP; 412178354Ssam /* NB: bg scanning only makes sense for station mode right now */ 413178354Ssam if (vap->iv_opmode == IEEE80211_M_STA && 414178354Ssam (vap->iv_caps & IEEE80211_C_BGSCAN)) 415178354Ssam vap->iv_flags |= IEEE80211_F_BGSCAN; 416178957Ssam vap->iv_flags |= IEEE80211_F_DOTH; /* XXX no cap, just ena */ 417178954Ssam /* NB: DFS support only makes sense for ap mode right now */ 418178954Ssam if (vap->iv_opmode == IEEE80211_M_HOSTAP && 419178954Ssam (vap->iv_caps & IEEE80211_C_DFS)) 420178354Ssam vap->iv_flags_ext |= IEEE80211_FEXT_DFS; 421178354Ssam 422178354Ssam vap->iv_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */ 423178354Ssam vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT; 424178354Ssam vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT; 425178354Ssam /* 426178354Ssam * Install a default reset method for the ioctl support; 427178354Ssam * the driver can override this. 428178354Ssam */ 429178354Ssam vap->iv_reset = default_reset; 430178354Ssam 431178354Ssam IEEE80211_ADDR_COPY(vap->iv_myaddr, macaddr); 432178354Ssam 433178354Ssam ieee80211_sysctl_vattach(vap); 434178354Ssam ieee80211_crypto_vattach(vap); 435178354Ssam ieee80211_node_vattach(vap); 436178354Ssam ieee80211_power_vattach(vap); 437178354Ssam ieee80211_proto_vattach(vap); 438178354Ssam ieee80211_ht_vattach(vap); 439178354Ssam ieee80211_scan_vattach(vap); 440178354Ssam ieee80211_regdomain_vattach(vap); 441178354Ssam 442178354Ssam return 0; 443178354Ssam} 444178354Ssam 445178354Ssam/* 446178354Ssam * Activate a vap. State should have been prepared with a 447178354Ssam * call to ieee80211_vap_setup and by the driver. On return 448178354Ssam * from this call the vap is ready for use. 449178354Ssam */ 450178354Ssamint 451178354Ssamieee80211_vap_attach(struct ieee80211vap *vap, 452178354Ssam ifm_change_cb_t media_change, ifm_stat_cb_t media_stat) 453178354Ssam{ 454178354Ssam struct ifnet *ifp = vap->iv_ifp; 455178354Ssam struct ieee80211com *ic = vap->iv_ic; 456178354Ssam struct ifmediareq imr; 457178354Ssam int maxrate; 458178354Ssam 459178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, 460178354Ssam "%s: %s parent %s flags 0x%x flags_ext 0x%x\n", 461178354Ssam __func__, ieee80211_opmode_name[vap->iv_opmode], 462178354Ssam ic->ic_ifp->if_xname, vap->iv_flags, vap->iv_flags_ext); 463178354Ssam 464178354Ssam /* 465178354Ssam * Do late attach work that cannot happen until after 466178354Ssam * the driver has had a chance to override defaults. 467178354Ssam */ 468178354Ssam ieee80211_node_latevattach(vap); 469178354Ssam ieee80211_power_latevattach(vap); 470178354Ssam 471178354Ssam maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps, 472178354Ssam vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat); 473178354Ssam ieee80211_media_status(ifp, &imr); 474178354Ssam /* NB: strip explicit mode; we're actually in autoselect */ 475178354Ssam ifmedia_set(&vap->iv_media, imr.ifm_active &~ IFM_MMASK); 476178354Ssam if (maxrate) 477178354Ssam ifp->if_baudrate = IF_Mbps(maxrate); 478178354Ssam 479178354Ssam ether_ifattach(ifp, vap->iv_myaddr); 480178354Ssam /* hook output method setup by ether_ifattach */ 481178354Ssam vap->iv_output = ifp->if_output; 482178354Ssam ifp->if_output = ieee80211_output; 483178354Ssam /* NB: if_mtu set by ether_ifattach to ETHERMTU */ 484178354Ssam bpfattach2(ifp, DLT_IEEE802_11, ifp->if_hdrlen, &vap->iv_rawbpf); 485178354Ssam 486178354Ssam IEEE80211_LOCK(ic); 487178354Ssam TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next); 488178354Ssam ieee80211_syncflag_locked(ic, IEEE80211_F_WME); 489178354Ssam ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP); 490178354Ssam ieee80211_syncflag_locked(ic, IEEE80211_F_PCF); 491178354Ssam ieee80211_syncflag_locked(ic, IEEE80211_F_BURST); 492178354Ssam ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_HT); 493178354Ssam ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_USEHT40); 494178354Ssam ieee80211_syncifflag_locked(ic, IFF_PROMISC); 495178354Ssam ieee80211_syncifflag_locked(ic, IFF_ALLMULTI); 496178354Ssam IEEE80211_UNLOCK(ic); 497178354Ssam 498178354Ssam return 1; 499178354Ssam} 500178354Ssam 501178354Ssam/* 502178354Ssam * Tear down vap state and reclaim the ifnet. 503178354Ssam * The driver is assumed to have prepared for 504178354Ssam * this; e.g. by turning off interrupts for the 505178354Ssam * underlying device. 506178354Ssam */ 507178354Ssamvoid 508178354Ssamieee80211_vap_detach(struct ieee80211vap *vap) 509178354Ssam{ 510178354Ssam struct ieee80211com *ic = vap->iv_ic; 511178354Ssam struct ifnet *ifp = vap->iv_ifp; 512178354Ssam 513178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n", 514178354Ssam __func__, ieee80211_opmode_name[vap->iv_opmode], 515178354Ssam ic->ic_ifp->if_xname); 516178354Ssam 517178354Ssam IEEE80211_LOCK(ic); 518178354Ssam /* block traffic from above */ 519178354Ssam ifp->if_drv_flags |= IFF_DRV_OACTIVE; 520178354Ssam /* 521178354Ssam * Evil hack. Clear the backpointer from the ifnet to the 522178354Ssam * vap so any requests from above will return an error or 523178354Ssam * be ignored. In particular this short-circuits requests 524178354Ssam * by the bridge to turn off promiscuous mode as a result 525178354Ssam * of calling ether_ifdetach. 526178354Ssam */ 527178354Ssam ifp->if_softc = NULL; 528178354Ssam /* 529178354Ssam * Stop the vap before detaching the ifnet. Ideally we'd 530178354Ssam * do this in the other order so the ifnet is inaccessible 531178354Ssam * while we cleanup internal state but that is hard. 532178354Ssam */ 533178354Ssam ieee80211_stop_locked(vap); 534178354Ssam 535178354Ssam /* XXX accumulate iv_stats in ic_stats? */ 536178354Ssam TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next); 537178354Ssam ieee80211_syncflag_locked(ic, IEEE80211_F_WME); 538178354Ssam ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP); 539178354Ssam ieee80211_syncflag_locked(ic, IEEE80211_F_PCF); 540178354Ssam ieee80211_syncflag_locked(ic, IEEE80211_F_BURST); 541178354Ssam ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_HT); 542178354Ssam ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_USEHT40); 543178354Ssam ieee80211_syncifflag_locked(ic, IFF_PROMISC); 544178354Ssam ieee80211_syncifflag_locked(ic, IFF_ALLMULTI); 545178354Ssam IEEE80211_UNLOCK(ic); 546178354Ssam 547178354Ssam /* XXX can't hold com lock */ 548178354Ssam /* NB: bpfattach is called by ether_ifdetach and claims all taps */ 549116742Ssam ether_ifdetach(ifp); 550178354Ssam 551178354Ssam ifmedia_removeall(&vap->iv_media); 552178354Ssam 553178354Ssam ieee80211_regdomain_vdetach(vap); 554178354Ssam ieee80211_scan_vdetach(vap); 555178354Ssam ieee80211_ht_vdetach(vap); 556178354Ssam /* NB: must be before ieee80211_node_vdetach */ 557178354Ssam ieee80211_proto_vdetach(vap); 558178354Ssam ieee80211_crypto_vdetach(vap); 559178354Ssam ieee80211_power_vdetach(vap); 560178354Ssam ieee80211_node_vdetach(vap); 561178354Ssam ieee80211_sysctl_vdetach(vap); 562182674Sweongyo 563182674Sweongyo if_free(ifp); 564116742Ssam} 565116742Ssam 566178354Ssam/* 567178354Ssam * Synchronize flag bit state in the parent ifnet structure 568178354Ssam * according to the state of all vap ifnet's. This is used, 569178354Ssam * for example, to handle IFF_PROMISC and IFF_ALLMULTI. 570178354Ssam */ 571178354Ssamvoid 572178354Ssamieee80211_syncifflag_locked(struct ieee80211com *ic, int flag) 573178354Ssam{ 574178354Ssam struct ifnet *ifp = ic->ic_ifp; 575178354Ssam struct ieee80211vap *vap; 576178354Ssam int bit, oflags; 577178354Ssam 578178354Ssam IEEE80211_LOCK_ASSERT(ic); 579178354Ssam 580178354Ssam bit = 0; 581178354Ssam TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 582178354Ssam if (vap->iv_ifp->if_flags & flag) { 583178354Ssam /* 584178354Ssam * XXX the bridge sets PROMISC but we don't want to 585178354Ssam * enable it on the device, discard here so all the 586178354Ssam * drivers don't need to special-case it 587178354Ssam */ 588178354Ssam if (flag == IFF_PROMISC && 589178354Ssam vap->iv_opmode == IEEE80211_M_HOSTAP) 590178354Ssam continue; 591178354Ssam bit = 1; 592178354Ssam break; 593178354Ssam } 594178354Ssam oflags = ifp->if_flags; 595178354Ssam if (bit) 596178354Ssam ifp->if_flags |= flag; 597178354Ssam else 598178354Ssam ifp->if_flags &= ~flag; 599178354Ssam if ((ifp->if_flags ^ oflags) & flag) { 600178354Ssam /* XXX should we return 1/0 and let caller do this? */ 601178354Ssam if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 602178354Ssam if (flag == IFF_PROMISC) 603178354Ssam ic->ic_update_promisc(ifp); 604178354Ssam else if (flag == IFF_ALLMULTI) 605178354Ssam ic->ic_update_mcast(ifp); 606178354Ssam } 607178354Ssam } 608178354Ssam} 609178354Ssam 610178354Ssam/* 611178354Ssam * Synchronize flag bit state in the com structure 612178354Ssam * according to the state of all vap's. This is used, 613178354Ssam * for example, to handle state changes via ioctls. 614178354Ssam */ 615178354Ssamstatic void 616178354Ssamieee80211_syncflag_locked(struct ieee80211com *ic, int flag) 617178354Ssam{ 618178354Ssam struct ieee80211vap *vap; 619178354Ssam int bit; 620178354Ssam 621178354Ssam IEEE80211_LOCK_ASSERT(ic); 622178354Ssam 623178354Ssam bit = 0; 624178354Ssam TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 625178354Ssam if (vap->iv_flags & flag) { 626178354Ssam bit = 1; 627178354Ssam break; 628178354Ssam } 629178354Ssam if (bit) 630178354Ssam ic->ic_flags |= flag; 631178354Ssam else 632178354Ssam ic->ic_flags &= ~flag; 633178354Ssam} 634178354Ssam 635178354Ssamvoid 636178354Ssamieee80211_syncflag(struct ieee80211vap *vap, int flag) 637178354Ssam{ 638178354Ssam struct ieee80211com *ic = vap->iv_ic; 639178354Ssam 640178354Ssam IEEE80211_LOCK(ic); 641178354Ssam if (flag < 0) { 642178354Ssam flag = -flag; 643178354Ssam vap->iv_flags &= ~flag; 644178354Ssam } else 645178354Ssam vap->iv_flags |= flag; 646178354Ssam ieee80211_syncflag_locked(ic, flag); 647178354Ssam IEEE80211_UNLOCK(ic); 648178354Ssam} 649178354Ssam 650178354Ssam/* 651178354Ssam * Synchronize flag bit state in the com structure 652178354Ssam * according to the state of all vap's. This is used, 653178354Ssam * for example, to handle state changes via ioctls. 654178354Ssam */ 655178354Ssamstatic void 656178354Ssamieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag) 657178354Ssam{ 658178354Ssam struct ieee80211vap *vap; 659178354Ssam int bit; 660178354Ssam 661178354Ssam IEEE80211_LOCK_ASSERT(ic); 662178354Ssam 663178354Ssam bit = 0; 664178354Ssam TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 665178354Ssam if (vap->iv_flags_ext & flag) { 666178354Ssam bit = 1; 667178354Ssam break; 668178354Ssam } 669178354Ssam if (bit) 670178354Ssam ic->ic_flags_ext |= flag; 671178354Ssam else 672178354Ssam ic->ic_flags_ext &= ~flag; 673178354Ssam} 674178354Ssam 675178354Ssamvoid 676178354Ssamieee80211_syncflag_ext(struct ieee80211vap *vap, int flag) 677178354Ssam{ 678178354Ssam struct ieee80211com *ic = vap->iv_ic; 679178354Ssam 680178354Ssam IEEE80211_LOCK(ic); 681178354Ssam if (flag < 0) { 682178354Ssam flag = -flag; 683178354Ssam vap->iv_flags_ext &= ~flag; 684178354Ssam } else 685178354Ssam vap->iv_flags_ext |= flag; 686178354Ssam ieee80211_syncflag_ext_locked(ic, flag); 687178354Ssam IEEE80211_UNLOCK(ic); 688178354Ssam} 689178354Ssam 690166012Ssamstatic __inline int 691166012Ssammapgsm(u_int freq, u_int flags) 692166012Ssam{ 693166012Ssam freq *= 10; 694166012Ssam if (flags & IEEE80211_CHAN_QUARTER) 695166012Ssam freq += 5; 696166012Ssam else if (flags & IEEE80211_CHAN_HALF) 697166012Ssam freq += 10; 698166012Ssam else 699166012Ssam freq += 20; 700166012Ssam /* NB: there is no 907/20 wide but leave room */ 701166012Ssam return (freq - 906*10) / 5; 702166012Ssam} 703166012Ssam 704166012Ssamstatic __inline int 705166012Ssammappsb(u_int freq, u_int flags) 706166012Ssam{ 707166012Ssam return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5; 708166012Ssam} 709166012Ssam 710116742Ssam/* 711116742Ssam * Convert MHz frequency to IEEE channel number. 712116742Ssam */ 713152450Ssamint 714116742Ssamieee80211_mhz2ieee(u_int freq, u_int flags) 715116742Ssam{ 716167430Ssam#define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990) 717166012Ssam if (flags & IEEE80211_CHAN_GSM) 718166012Ssam return mapgsm(freq, flags); 719116742Ssam if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 720116742Ssam if (freq == 2484) 721116742Ssam return 14; 722116742Ssam if (freq < 2484) 723152450Ssam return ((int) freq - 2407) / 5; 724116742Ssam else 725116742Ssam return 15 + ((freq - 2512) / 20); 726116899Ssam } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */ 727165569Ssam if (freq <= 5000) { 728170530Ssam /* XXX check regdomain? */ 729167430Ssam if (IS_FREQ_IN_PSB(freq)) 730166012Ssam return mappsb(freq, flags); 731152450Ssam return (freq - 4000) / 5; 732165569Ssam } else 733152450Ssam return (freq - 5000) / 5; 734116742Ssam } else { /* either, guess */ 735116742Ssam if (freq == 2484) 736116742Ssam return 14; 737166012Ssam if (freq < 2484) { 738166012Ssam if (907 <= freq && freq <= 922) 739166012Ssam return mapgsm(freq, flags); 740152450Ssam return ((int) freq - 2407) / 5; 741166012Ssam } 742152450Ssam if (freq < 5000) { 743167430Ssam if (IS_FREQ_IN_PSB(freq)) 744166012Ssam return mappsb(freq, flags); 745165569Ssam else if (freq > 4900) 746152450Ssam return (freq - 4000) / 5; 747152450Ssam else 748152450Ssam return 15 + ((freq - 2512) / 20); 749152450Ssam } 750116742Ssam return (freq - 5000) / 5; 751116742Ssam } 752167430Ssam#undef IS_FREQ_IN_PSB 753116742Ssam} 754116742Ssam 755116742Ssam/* 756116742Ssam * Convert channel to IEEE channel number. 757116742Ssam */ 758152450Ssamint 759165825Smjacobieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c) 760116742Ssam{ 761170530Ssam if (c == NULL) { 762138568Ssam if_printf(ic->ic_ifp, "invalid channel (NULL)\n"); 763117039Ssam return 0; /* XXX */ 764116742Ssam } 765170530Ssam return (c == IEEE80211_CHAN_ANYC ? IEEE80211_CHAN_ANY : c->ic_ieee); 766116742Ssam} 767116742Ssam 768116742Ssam/* 769116742Ssam * Convert IEEE channel number to MHz frequency. 770116742Ssam */ 771116742Ssamu_int 772116742Ssamieee80211_ieee2mhz(u_int chan, u_int flags) 773116742Ssam{ 774166012Ssam if (flags & IEEE80211_CHAN_GSM) 775166012Ssam return 907 + 5 * (chan / 10); 776116742Ssam if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 777116742Ssam if (chan == 14) 778116742Ssam return 2484; 779116742Ssam if (chan < 14) 780116742Ssam return 2407 + chan*5; 781116742Ssam else 782116742Ssam return 2512 + ((chan-15)*20); 783116742Ssam } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */ 784165569Ssam if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) { 785165569Ssam chan -= 37; 786165569Ssam return 4940 + chan*5 + (chan % 5 ? 2 : 0); 787165569Ssam } 788116742Ssam return 5000 + (chan*5); 789116742Ssam } else { /* either, guess */ 790166012Ssam /* XXX can't distinguish PSB+GSM channels */ 791116742Ssam if (chan == 14) 792116742Ssam return 2484; 793116742Ssam if (chan < 14) /* 0-13 */ 794116742Ssam return 2407 + chan*5; 795116742Ssam if (chan < 27) /* 15-26 */ 796116742Ssam return 2512 + ((chan-15)*20); 797116742Ssam return 5000 + (chan*5); 798116742Ssam } 799116742Ssam} 800116742Ssam 801116742Ssam/* 802170530Ssam * Locate a channel given a frequency+flags. We cache 803178354Ssam * the previous lookup to optimize switching between two 804170530Ssam * channels--as happens with dynamic turbo. 805170530Ssam */ 806170530Ssamstruct ieee80211_channel * 807170530Ssamieee80211_find_channel(struct ieee80211com *ic, int freq, int flags) 808170530Ssam{ 809170530Ssam struct ieee80211_channel *c; 810170530Ssam int i; 811170530Ssam 812170530Ssam flags &= IEEE80211_CHAN_ALLTURBO; 813170530Ssam c = ic->ic_prevchan; 814170530Ssam if (c != NULL && c->ic_freq == freq && 815170530Ssam (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 816170530Ssam return c; 817170530Ssam /* brute force search */ 818170530Ssam for (i = 0; i < ic->ic_nchans; i++) { 819170530Ssam c = &ic->ic_channels[i]; 820170530Ssam if (c->ic_freq == freq && 821170530Ssam (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 822170530Ssam return c; 823170530Ssam } 824170530Ssam return NULL; 825170530Ssam} 826170530Ssam 827173861Ssam/* 828173861Ssam * Locate a channel given a channel number+flags. We cache 829173861Ssam * the previous lookup to optimize switching between two 830173861Ssam * channels--as happens with dynamic turbo. 831173861Ssam */ 832173861Ssamstruct ieee80211_channel * 833173861Ssamieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags) 834173861Ssam{ 835173861Ssam struct ieee80211_channel *c; 836173861Ssam int i; 837173861Ssam 838173861Ssam flags &= IEEE80211_CHAN_ALLTURBO; 839173861Ssam c = ic->ic_prevchan; 840173861Ssam if (c != NULL && c->ic_ieee == ieee && 841173861Ssam (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 842173861Ssam return c; 843173861Ssam /* brute force search */ 844173861Ssam for (i = 0; i < ic->ic_nchans; i++) { 845173861Ssam c = &ic->ic_channels[i]; 846173861Ssam if (c->ic_ieee == ieee && 847173861Ssam (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 848173861Ssam return c; 849173861Ssam } 850173861Ssam return NULL; 851173861Ssam} 852173861Ssam 853170530Ssamstatic void 854178354Ssamaddmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword) 855170530Ssam{ 856170530Ssam#define ADD(_ic, _s, _o) \ 857178354Ssam ifmedia_add(media, \ 858170530Ssam IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL) 859170530Ssam static const u_int mopts[IEEE80211_MODE_MAX] = { 860178354Ssam IFM_AUTO, 861178354Ssam IFM_IEEE80211_11A, 862178354Ssam IFM_IEEE80211_11B, 863178354Ssam IFM_IEEE80211_11G, 864178354Ssam IFM_IEEE80211_FH, 865178354Ssam IFM_IEEE80211_11A | IFM_IEEE80211_TURBO, 866178354Ssam IFM_IEEE80211_11G | IFM_IEEE80211_TURBO, 867178354Ssam IFM_IEEE80211_11A | IFM_IEEE80211_TURBO, 868178354Ssam IFM_IEEE80211_11NA, 869178354Ssam IFM_IEEE80211_11NG, 870170530Ssam }; 871170530Ssam u_int mopt; 872170530Ssam 873170530Ssam mopt = mopts[mode]; 874178354Ssam if (addsta) 875178354Ssam ADD(ic, mword, mopt); /* STA mode has no cap */ 876178354Ssam if (caps & IEEE80211_C_IBSS) 877178354Ssam ADD(media, mword, mopt | IFM_IEEE80211_ADHOC); 878178354Ssam if (caps & IEEE80211_C_HOSTAP) 879178354Ssam ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP); 880178354Ssam if (caps & IEEE80211_C_AHDEMO) 881178354Ssam ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0); 882178354Ssam if (caps & IEEE80211_C_MONITOR) 883178354Ssam ADD(media, mword, mopt | IFM_IEEE80211_MONITOR); 884178354Ssam if (caps & IEEE80211_C_WDS) 885178354Ssam ADD(media, mword, mopt | IFM_IEEE80211_WDS); 886170530Ssam#undef ADD 887170530Ssam} 888170530Ssam 889170530Ssam/* 890116742Ssam * Setup the media data structures according to the channel and 891178354Ssam * rate tables. 892116742Ssam */ 893178354Ssamstatic int 894178354Ssamieee80211_media_setup(struct ieee80211com *ic, 895178354Ssam struct ifmedia *media, int caps, int addsta, 896116742Ssam ifm_change_cb_t media_change, ifm_stat_cb_t media_stat) 897116742Ssam{ 898170530Ssam int i, j, mode, rate, maxrate, mword, r; 899170530Ssam const struct ieee80211_rateset *rs; 900116742Ssam struct ieee80211_rateset allrates; 901116742Ssam 902118887Ssam /* 903116742Ssam * Fill in media characteristics. 904116742Ssam */ 905178354Ssam ifmedia_init(media, 0, media_change, media_stat); 906116742Ssam maxrate = 0; 907170530Ssam /* 908170530Ssam * Add media for legacy operating modes. 909170530Ssam */ 910116742Ssam memset(&allrates, 0, sizeof(allrates)); 911170530Ssam for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) { 912167468Ssam if (isclr(ic->ic_modecaps, mode)) 913116742Ssam continue; 914178354Ssam addmedia(media, caps, addsta, mode, IFM_AUTO); 915116742Ssam if (mode == IEEE80211_MODE_AUTO) 916116742Ssam continue; 917116742Ssam rs = &ic->ic_sup_rates[mode]; 918116742Ssam for (i = 0; i < rs->rs_nrates; i++) { 919116742Ssam rate = rs->rs_rates[i]; 920116742Ssam mword = ieee80211_rate2media(ic, rate, mode); 921116742Ssam if (mword == 0) 922116742Ssam continue; 923178354Ssam addmedia(media, caps, addsta, mode, mword); 924116742Ssam /* 925170530Ssam * Add legacy rate to the collection of all rates. 926116742Ssam */ 927116742Ssam r = rate & IEEE80211_RATE_VAL; 928116742Ssam for (j = 0; j < allrates.rs_nrates; j++) 929116742Ssam if (allrates.rs_rates[j] == r) 930116742Ssam break; 931116742Ssam if (j == allrates.rs_nrates) { 932116742Ssam /* unique, add to the set */ 933116742Ssam allrates.rs_rates[j] = r; 934116742Ssam allrates.rs_nrates++; 935116742Ssam } 936116742Ssam rate = (rate & IEEE80211_RATE_VAL) / 2; 937116742Ssam if (rate > maxrate) 938116742Ssam maxrate = rate; 939116742Ssam } 940116742Ssam } 941116742Ssam for (i = 0; i < allrates.rs_nrates; i++) { 942116742Ssam mword = ieee80211_rate2media(ic, allrates.rs_rates[i], 943116742Ssam IEEE80211_MODE_AUTO); 944116742Ssam if (mword == 0) 945116742Ssam continue; 946170530Ssam /* NB: remove media options from mword */ 947178354Ssam addmedia(media, caps, addsta, 948178354Ssam IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword)); 949116742Ssam } 950170530Ssam /* 951170530Ssam * Add HT/11n media. Note that we do not have enough 952170530Ssam * bits in the media subtype to express the MCS so we 953170530Ssam * use a "placeholder" media subtype and any fixed MCS 954170530Ssam * must be specified with a different mechanism. 955170530Ssam */ 956170530Ssam for (; mode < IEEE80211_MODE_MAX; mode++) { 957170530Ssam if (isclr(ic->ic_modecaps, mode)) 958170530Ssam continue; 959178354Ssam addmedia(media, caps, addsta, mode, IFM_AUTO); 960178354Ssam addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS); 961170530Ssam } 962170530Ssam if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) || 963170530Ssam isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) { 964178354Ssam addmedia(media, caps, addsta, 965178354Ssam IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS); 966170530Ssam /* XXX could walk htrates */ 967170530Ssam /* XXX known array size */ 968178354Ssam if (ieee80211_htrates[15].ht40_rate_400ns > maxrate) 969178354Ssam maxrate = ieee80211_htrates[15].ht40_rate_400ns; 970170530Ssam } 971178354Ssam return maxrate; 972178354Ssam} 973116742Ssam 974178354Ssamvoid 975178354Ssamieee80211_media_init(struct ieee80211com *ic) 976178354Ssam{ 977178354Ssam struct ifnet *ifp = ic->ic_ifp; 978178354Ssam int maxrate; 979178354Ssam 980178354Ssam /* NB: this works because the structure is initialized to zero */ 981178354Ssam if (!LIST_EMPTY(&ic->ic_media.ifm_list)) { 982178354Ssam /* 983178354Ssam * We are re-initializing the channel list; clear 984178354Ssam * the existing media state as the media routines 985178354Ssam * don't suppress duplicates. 986178354Ssam */ 987178354Ssam ifmedia_removeall(&ic->ic_media); 988178354Ssam } 989178354Ssam ieee80211_chan_init(ic); 990178354Ssam 991178354Ssam /* 992178354Ssam * Recalculate media settings in case new channel list changes 993178354Ssam * the set of available modes. 994178354Ssam */ 995178354Ssam maxrate = ieee80211_media_setup(ic, &ic->ic_media, ic->ic_caps, 1, 996178354Ssam ieee80211com_media_change, ieee80211com_media_status); 997170530Ssam /* NB: strip explicit mode; we're actually in autoselect */ 998170530Ssam ifmedia_set(&ic->ic_media, 999170530Ssam media_status(ic->ic_opmode, ic->ic_curchan) &~ IFM_MMASK); 1000116742Ssam if (maxrate) 1001116742Ssam ifp->if_baudrate = IF_Mbps(maxrate); 1002178354Ssam 1003178354Ssam /* XXX need to propagate new media settings to vap's */ 1004116742Ssam} 1005116742Ssam 1006165569Ssamconst struct ieee80211_rateset * 1007165569Ssamieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c) 1008165569Ssam{ 1009166012Ssam if (IEEE80211_IS_CHAN_HALF(c)) 1010166012Ssam return &ieee80211_rateset_half; 1011166012Ssam if (IEEE80211_IS_CHAN_QUARTER(c)) 1012166012Ssam return &ieee80211_rateset_quarter; 1013170530Ssam if (IEEE80211_IS_CHAN_HTA(c)) 1014170530Ssam return &ic->ic_sup_rates[IEEE80211_MODE_11A]; 1015170530Ssam if (IEEE80211_IS_CHAN_HTG(c)) { 1016170530Ssam /* XXX does this work for basic rates? */ 1017170530Ssam return &ic->ic_sup_rates[IEEE80211_MODE_11G]; 1018170530Ssam } 1019170530Ssam return &ic->ic_sup_rates[ieee80211_chan2mode(c)]; 1020165569Ssam} 1021165569Ssam 1022138568Ssamvoid 1023138568Ssamieee80211_announce(struct ieee80211com *ic) 1024138568Ssam{ 1025138568Ssam struct ifnet *ifp = ic->ic_ifp; 1026138568Ssam int i, mode, rate, mword; 1027170530Ssam const struct ieee80211_rateset *rs; 1028138568Ssam 1029172227Ssam /* NB: skip AUTO since it has no rates */ 1030172227Ssam for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) { 1031167468Ssam if (isclr(ic->ic_modecaps, mode)) 1032138568Ssam continue; 1033138568Ssam if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]); 1034138568Ssam rs = &ic->ic_sup_rates[mode]; 1035138568Ssam for (i = 0; i < rs->rs_nrates; i++) { 1036170530Ssam mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode); 1037138568Ssam if (mword == 0) 1038138568Ssam continue; 1039170530Ssam rate = ieee80211_media2rate(mword); 1040138568Ssam printf("%s%d%sMbps", (i != 0 ? " " : ""), 1041170530Ssam rate / 2, ((rate & 0x1) != 0 ? ".5" : "")); 1042138568Ssam } 1043138568Ssam printf("\n"); 1044138568Ssam } 1045170530Ssam ieee80211_ht_announce(ic); 1046138568Ssam} 1047138568Ssam 1048170530Ssamvoid 1049170530Ssamieee80211_announce_channels(struct ieee80211com *ic) 1050116742Ssam{ 1051170530Ssam const struct ieee80211_channel *c; 1052170530Ssam char type; 1053170530Ssam int i, cw; 1054170530Ssam 1055170530Ssam printf("Chan Freq CW RegPwr MinPwr MaxPwr\n"); 1056170530Ssam for (i = 0; i < ic->ic_nchans; i++) { 1057170530Ssam c = &ic->ic_channels[i]; 1058170530Ssam if (IEEE80211_IS_CHAN_ST(c)) 1059170530Ssam type = 'S'; 1060170530Ssam else if (IEEE80211_IS_CHAN_108A(c)) 1061170530Ssam type = 'T'; 1062170530Ssam else if (IEEE80211_IS_CHAN_108G(c)) 1063170530Ssam type = 'G'; 1064170530Ssam else if (IEEE80211_IS_CHAN_HT(c)) 1065170530Ssam type = 'n'; 1066170530Ssam else if (IEEE80211_IS_CHAN_A(c)) 1067170530Ssam type = 'a'; 1068170530Ssam else if (IEEE80211_IS_CHAN_ANYG(c)) 1069170530Ssam type = 'g'; 1070170530Ssam else if (IEEE80211_IS_CHAN_B(c)) 1071170530Ssam type = 'b'; 1072170530Ssam else 1073170530Ssam type = 'f'; 1074170530Ssam if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c)) 1075170530Ssam cw = 40; 1076170530Ssam else if (IEEE80211_IS_CHAN_HALF(c)) 1077170530Ssam cw = 10; 1078170530Ssam else if (IEEE80211_IS_CHAN_QUARTER(c)) 1079170530Ssam cw = 5; 1080170530Ssam else 1081170530Ssam cw = 20; 1082170530Ssam printf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n" 1083170530Ssam , c->ic_ieee, c->ic_freq, type 1084170530Ssam , cw 1085170530Ssam , IEEE80211_IS_CHAN_HT40U(c) ? '+' : 1086170530Ssam IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' ' 1087170530Ssam , c->ic_maxregpower 1088170530Ssam , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0 1089170530Ssam , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0 1090170530Ssam ); 1091170530Ssam } 1092116742Ssam} 1093116742Ssam 1094170530Ssamstatic int 1095184273Ssammedia2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode) 1096170530Ssam{ 1097116742Ssam switch (IFM_MODE(ime->ifm_media)) { 1098116742Ssam case IFM_IEEE80211_11A: 1099178354Ssam *mode = IEEE80211_MODE_11A; 1100116742Ssam break; 1101116742Ssam case IFM_IEEE80211_11B: 1102178354Ssam *mode = IEEE80211_MODE_11B; 1103116742Ssam break; 1104116742Ssam case IFM_IEEE80211_11G: 1105178354Ssam *mode = IEEE80211_MODE_11G; 1106116742Ssam break; 1107124543Sonoe case IFM_IEEE80211_FH: 1108178354Ssam *mode = IEEE80211_MODE_FH; 1109124543Sonoe break; 1110170530Ssam case IFM_IEEE80211_11NA: 1111178354Ssam *mode = IEEE80211_MODE_11NA; 1112170530Ssam break; 1113170530Ssam case IFM_IEEE80211_11NG: 1114178354Ssam *mode = IEEE80211_MODE_11NG; 1115170530Ssam break; 1116116742Ssam case IFM_AUTO: 1117178354Ssam *mode = IEEE80211_MODE_AUTO; 1118116742Ssam break; 1119116742Ssam default: 1120178354Ssam return 0; 1121116742Ssam } 1122116742Ssam /* 1123138568Ssam * Turbo mode is an ``option''. 1124138568Ssam * XXX does not apply to AUTO 1125116742Ssam */ 1126116742Ssam if (ime->ifm_media & IFM_IEEE80211_TURBO) { 1127178354Ssam if (*mode == IEEE80211_MODE_11A) { 1128184273Ssam if (flags & IEEE80211_F_TURBOP) 1129178354Ssam *mode = IEEE80211_MODE_TURBO_A; 1130170530Ssam else 1131178354Ssam *mode = IEEE80211_MODE_STURBO_A; 1132178354Ssam } else if (*mode == IEEE80211_MODE_11G) 1133178354Ssam *mode = IEEE80211_MODE_TURBO_G; 1134138568Ssam else 1135178354Ssam return 0; 1136116742Ssam } 1137170530Ssam /* XXX HT40 +/- */ 1138178354Ssam return 1; 1139178354Ssam} 1140116742Ssam 1141178354Ssam/* 1142184273Ssam * Handle a media change request on the underlying interface. 1143178354Ssam */ 1144178354Ssamint 1145178354Ssamieee80211com_media_change(struct ifnet *ifp) 1146178354Ssam{ 1147184273Ssam return EINVAL; 1148178354Ssam} 1149116742Ssam 1150178354Ssam/* 1151178354Ssam * Handle a media change request on the vap interface. 1152178354Ssam */ 1153178354Ssamint 1154178354Ssamieee80211_media_change(struct ifnet *ifp) 1155178354Ssam{ 1156178354Ssam struct ieee80211vap *vap = ifp->if_softc; 1157178354Ssam struct ifmedia_entry *ime = vap->iv_media.ifm_cur; 1158184273Ssam uint16_t newmode; 1159178354Ssam 1160184273Ssam if (!media2mode(ime, vap->iv_flags, &newmode)) 1161178354Ssam return EINVAL; 1162184273Ssam if (vap->iv_des_mode != newmode) { 1163184273Ssam vap->iv_des_mode = newmode; 1164178354Ssam return ENETRESET; 1165178354Ssam } 1166178354Ssam return 0; 1167116742Ssam} 1168116742Ssam 1169170530Ssam/* 1170170530Ssam * Common code to calculate the media status word 1171170530Ssam * from the operating mode and channel state. 1172170530Ssam */ 1173170530Ssamstatic int 1174170530Ssammedia_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan) 1175170530Ssam{ 1176170530Ssam int status; 1177170530Ssam 1178170530Ssam status = IFM_IEEE80211; 1179170530Ssam switch (opmode) { 1180170530Ssam case IEEE80211_M_STA: 1181170530Ssam break; 1182170530Ssam case IEEE80211_M_IBSS: 1183170530Ssam status |= IFM_IEEE80211_ADHOC; 1184170530Ssam break; 1185170530Ssam case IEEE80211_M_HOSTAP: 1186170530Ssam status |= IFM_IEEE80211_HOSTAP; 1187170530Ssam break; 1188170530Ssam case IEEE80211_M_MONITOR: 1189170530Ssam status |= IFM_IEEE80211_MONITOR; 1190170530Ssam break; 1191170530Ssam case IEEE80211_M_AHDEMO: 1192170530Ssam status |= IFM_IEEE80211_ADHOC | IFM_FLAG0; 1193170530Ssam break; 1194170530Ssam case IEEE80211_M_WDS: 1195178354Ssam status |= IFM_IEEE80211_WDS; 1196170530Ssam break; 1197170530Ssam } 1198170530Ssam if (IEEE80211_IS_CHAN_HTA(chan)) { 1199170530Ssam status |= IFM_IEEE80211_11NA; 1200170530Ssam } else if (IEEE80211_IS_CHAN_HTG(chan)) { 1201170530Ssam status |= IFM_IEEE80211_11NG; 1202170530Ssam } else if (IEEE80211_IS_CHAN_A(chan)) { 1203170530Ssam status |= IFM_IEEE80211_11A; 1204170530Ssam } else if (IEEE80211_IS_CHAN_B(chan)) { 1205170530Ssam status |= IFM_IEEE80211_11B; 1206170530Ssam } else if (IEEE80211_IS_CHAN_ANYG(chan)) { 1207170530Ssam status |= IFM_IEEE80211_11G; 1208170530Ssam } else if (IEEE80211_IS_CHAN_FHSS(chan)) { 1209170530Ssam status |= IFM_IEEE80211_FH; 1210170530Ssam } 1211170530Ssam /* XXX else complain? */ 1212170530Ssam 1213170530Ssam if (IEEE80211_IS_CHAN_TURBO(chan)) 1214170530Ssam status |= IFM_IEEE80211_TURBO; 1215178354Ssam#if 0 1216178354Ssam if (IEEE80211_IS_CHAN_HT20(chan)) 1217178354Ssam status |= IFM_IEEE80211_HT20; 1218178354Ssam if (IEEE80211_IS_CHAN_HT40(chan)) 1219178354Ssam status |= IFM_IEEE80211_HT40; 1220178354Ssam#endif 1221170530Ssam return status; 1222170530Ssam} 1223170530Ssam 1224178354Ssamstatic void 1225178354Ssamieee80211com_media_status(struct ifnet *ifp, struct ifmediareq *imr) 1226178354Ssam{ 1227178354Ssam struct ieee80211com *ic = ifp->if_l2com; 1228178354Ssam struct ieee80211vap *vap; 1229178354Ssam 1230178354Ssam imr->ifm_status = IFM_AVALID; 1231178354Ssam TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 1232178354Ssam if (vap->iv_ifp->if_flags & IFF_UP) { 1233178354Ssam imr->ifm_status |= IFM_ACTIVE; 1234178354Ssam break; 1235178354Ssam } 1236178354Ssam imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan); 1237178354Ssam if (imr->ifm_status & IFM_ACTIVE) 1238178354Ssam imr->ifm_current = imr->ifm_active; 1239178354Ssam} 1240178354Ssam 1241116742Ssamvoid 1242116742Ssamieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr) 1243116742Ssam{ 1244178354Ssam struct ieee80211vap *vap = ifp->if_softc; 1245178354Ssam struct ieee80211com *ic = vap->iv_ic; 1246170530Ssam enum ieee80211_phymode mode; 1247116742Ssam 1248116742Ssam imr->ifm_status = IFM_AVALID; 1249170530Ssam /* 1250170530Ssam * NB: use the current channel's mode to lock down a xmit 1251170530Ssam * rate only when running; otherwise we may have a mismatch 1252170530Ssam * in which case the rate will not be convertible. 1253170530Ssam */ 1254178354Ssam if (vap->iv_state == IEEE80211_S_RUN) { 1255116742Ssam imr->ifm_status |= IFM_ACTIVE; 1256170530Ssam mode = ieee80211_chan2mode(ic->ic_curchan); 1257170530Ssam } else 1258170530Ssam mode = IEEE80211_MODE_AUTO; 1259178354Ssam imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan); 1260138568Ssam /* 1261138568Ssam * Calculate a current rate if possible. 1262138568Ssam */ 1263178354Ssam if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) { 1264138568Ssam /* 1265138568Ssam * A fixed rate is set, report that. 1266138568Ssam */ 1267138568Ssam imr->ifm_active |= ieee80211_rate2media(ic, 1268178354Ssam vap->iv_txparms[mode].ucastrate, mode); 1269178354Ssam } else if (vap->iv_opmode == IEEE80211_M_STA) { 1270138568Ssam /* 1271138568Ssam * In station mode report the current transmit rate. 1272138568Ssam */ 1273138568Ssam imr->ifm_active |= ieee80211_rate2media(ic, 1274178354Ssam vap->iv_bss->ni_txrate, mode); 1275128966Sandre } else 1276138568Ssam imr->ifm_active |= IFM_AUTO; 1277178354Ssam if (imr->ifm_status & IFM_ACTIVE) 1278178354Ssam imr->ifm_current = imr->ifm_active; 1279116742Ssam} 1280116742Ssam 1281116742Ssam/* 1282116742Ssam * Set the current phy mode and recalculate the active channel 1283116742Ssam * set based on the available channels for this mode. Also 1284116742Ssam * select a new default/current channel if the current one is 1285116742Ssam * inappropriate for this mode. 1286116742Ssam */ 1287116742Ssamint 1288116742Ssamieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode) 1289116742Ssam{ 1290116742Ssam /* 1291166012Ssam * Adjust basic rates in 11b/11g supported rate set. 1292166012Ssam * Note that if operating on a hal/quarter rate channel 1293166012Ssam * this is a noop as those rates sets are different 1294166012Ssam * and used instead. 1295116742Ssam */ 1296166012Ssam if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B) 1297178354Ssam ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode); 1298166012Ssam 1299116742Ssam ic->ic_curmode = mode; 1300138568Ssam ieee80211_reset_erp(ic); /* reset ERP state */ 1301138568Ssam 1302116742Ssam return 0; 1303116742Ssam} 1304116742Ssam 1305116742Ssam/* 1306170530Ssam * Return the phy mode for with the specified channel. 1307116742Ssam */ 1308116742Ssamenum ieee80211_phymode 1309170530Ssamieee80211_chan2mode(const struct ieee80211_channel *chan) 1310116742Ssam{ 1311170530Ssam 1312170530Ssam if (IEEE80211_IS_CHAN_HTA(chan)) 1313170530Ssam return IEEE80211_MODE_11NA; 1314170530Ssam else if (IEEE80211_IS_CHAN_HTG(chan)) 1315170530Ssam return IEEE80211_MODE_11NG; 1316170530Ssam else if (IEEE80211_IS_CHAN_108G(chan)) 1317170530Ssam return IEEE80211_MODE_TURBO_G; 1318170530Ssam else if (IEEE80211_IS_CHAN_ST(chan)) 1319170530Ssam return IEEE80211_MODE_STURBO_A; 1320170530Ssam else if (IEEE80211_IS_CHAN_TURBO(chan)) 1321153350Ssam return IEEE80211_MODE_TURBO_A; 1322170530Ssam else if (IEEE80211_IS_CHAN_A(chan)) 1323116742Ssam return IEEE80211_MODE_11A; 1324170530Ssam else if (IEEE80211_IS_CHAN_ANYG(chan)) 1325116742Ssam return IEEE80211_MODE_11G; 1326170530Ssam else if (IEEE80211_IS_CHAN_B(chan)) 1327116742Ssam return IEEE80211_MODE_11B; 1328170530Ssam else if (IEEE80211_IS_CHAN_FHSS(chan)) 1329170530Ssam return IEEE80211_MODE_FH; 1330170530Ssam 1331170530Ssam /* NB: should not get here */ 1332170530Ssam printf("%s: cannot map channel to mode; freq %u flags 0x%x\n", 1333170530Ssam __func__, chan->ic_freq, chan->ic_flags); 1334170530Ssam return IEEE80211_MODE_11B; 1335116742Ssam} 1336116742Ssam 1337170530Ssamstruct ratemedia { 1338170530Ssam u_int match; /* rate + mode */ 1339170530Ssam u_int media; /* if_media rate */ 1340170530Ssam}; 1341170530Ssam 1342170530Ssamstatic int 1343170530Ssamfindmedia(const struct ratemedia rates[], int n, u_int match) 1344170530Ssam{ 1345170530Ssam int i; 1346170530Ssam 1347170530Ssam for (i = 0; i < n; i++) 1348170530Ssam if (rates[i].match == match) 1349170530Ssam return rates[i].media; 1350170530Ssam return IFM_AUTO; 1351170530Ssam} 1352170530Ssam 1353116742Ssam/* 1354170530Ssam * Convert IEEE80211 rate value to ifmedia subtype. 1355170530Ssam * Rate is either a legacy rate in units of 0.5Mbps 1356170530Ssam * or an MCS index. 1357116742Ssam */ 1358116742Ssamint 1359116742Ssamieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode) 1360116742Ssam{ 1361116742Ssam#define N(a) (sizeof(a) / sizeof(a[0])) 1362170530Ssam static const struct ratemedia rates[] = { 1363124543Sonoe { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 }, 1364124543Sonoe { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 }, 1365124543Sonoe { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 }, 1366124543Sonoe { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 }, 1367124543Sonoe { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 }, 1368124543Sonoe { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 }, 1369124543Sonoe { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 }, 1370124543Sonoe { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 }, 1371124543Sonoe { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 }, 1372124543Sonoe { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 }, 1373124543Sonoe { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 }, 1374124543Sonoe { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 }, 1375124543Sonoe { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 }, 1376124543Sonoe { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 }, 1377124543Sonoe { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 }, 1378124543Sonoe { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 }, 1379124543Sonoe { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 }, 1380124543Sonoe { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 }, 1381124543Sonoe { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 }, 1382124543Sonoe { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 }, 1383124543Sonoe { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 }, 1384124543Sonoe { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 }, 1385124543Sonoe { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 }, 1386124543Sonoe { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 }, 1387124543Sonoe { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 }, 1388124543Sonoe { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 }, 1389124543Sonoe { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 }, 1390165569Ssam { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 }, 1391165569Ssam { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 }, 1392165569Ssam { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 }, 1393116742Ssam /* NB: OFDM72 doesn't realy exist so we don't handle it */ 1394116742Ssam }; 1395170530Ssam static const struct ratemedia htrates[] = { 1396170530Ssam { 0, IFM_IEEE80211_MCS }, 1397170530Ssam { 1, IFM_IEEE80211_MCS }, 1398170530Ssam { 2, IFM_IEEE80211_MCS }, 1399170530Ssam { 3, IFM_IEEE80211_MCS }, 1400170530Ssam { 4, IFM_IEEE80211_MCS }, 1401170530Ssam { 5, IFM_IEEE80211_MCS }, 1402170530Ssam { 6, IFM_IEEE80211_MCS }, 1403170530Ssam { 7, IFM_IEEE80211_MCS }, 1404170530Ssam { 8, IFM_IEEE80211_MCS }, 1405170530Ssam { 9, IFM_IEEE80211_MCS }, 1406170530Ssam { 10, IFM_IEEE80211_MCS }, 1407170530Ssam { 11, IFM_IEEE80211_MCS }, 1408170530Ssam { 12, IFM_IEEE80211_MCS }, 1409170530Ssam { 13, IFM_IEEE80211_MCS }, 1410170530Ssam { 14, IFM_IEEE80211_MCS }, 1411170530Ssam { 15, IFM_IEEE80211_MCS }, 1412170530Ssam }; 1413170530Ssam int m; 1414116742Ssam 1415170530Ssam /* 1416170530Ssam * Check 11n rates first for match as an MCS. 1417170530Ssam */ 1418170530Ssam if (mode == IEEE80211_MODE_11NA) { 1419172226Ssam if (rate & IEEE80211_RATE_MCS) { 1420172226Ssam rate &= ~IEEE80211_RATE_MCS; 1421170530Ssam m = findmedia(htrates, N(htrates), rate); 1422170530Ssam if (m != IFM_AUTO) 1423170530Ssam return m | IFM_IEEE80211_11NA; 1424170530Ssam } 1425170530Ssam } else if (mode == IEEE80211_MODE_11NG) { 1426170530Ssam /* NB: 12 is ambiguous, it will be treated as an MCS */ 1427172226Ssam if (rate & IEEE80211_RATE_MCS) { 1428172226Ssam rate &= ~IEEE80211_RATE_MCS; 1429170530Ssam m = findmedia(htrates, N(htrates), rate); 1430170530Ssam if (m != IFM_AUTO) 1431170530Ssam return m | IFM_IEEE80211_11NG; 1432170530Ssam } 1433170530Ssam } 1434170530Ssam rate &= IEEE80211_RATE_VAL; 1435116742Ssam switch (mode) { 1436116742Ssam case IEEE80211_MODE_11A: 1437170530Ssam case IEEE80211_MODE_11NA: 1438138568Ssam case IEEE80211_MODE_TURBO_A: 1439170530Ssam case IEEE80211_MODE_STURBO_A: 1440170530Ssam return findmedia(rates, N(rates), rate | IFM_IEEE80211_11A); 1441116742Ssam case IEEE80211_MODE_11B: 1442170530Ssam return findmedia(rates, N(rates), rate | IFM_IEEE80211_11B); 1443124543Sonoe case IEEE80211_MODE_FH: 1444170530Ssam return findmedia(rates, N(rates), rate | IFM_IEEE80211_FH); 1445116742Ssam case IEEE80211_MODE_AUTO: 1446116742Ssam /* NB: ic may be NULL for some drivers */ 1447170530Ssam if (ic && ic->ic_phytype == IEEE80211_T_FH) 1448170530Ssam return findmedia(rates, N(rates), 1449170530Ssam rate | IFM_IEEE80211_FH); 1450116742Ssam /* NB: hack, 11g matches both 11b+11a rates */ 1451116742Ssam /* fall thru... */ 1452116742Ssam case IEEE80211_MODE_11G: 1453170530Ssam case IEEE80211_MODE_11NG: 1454138568Ssam case IEEE80211_MODE_TURBO_G: 1455170530Ssam return findmedia(rates, N(rates), rate | IFM_IEEE80211_11G); 1456116742Ssam } 1457116742Ssam return IFM_AUTO; 1458116742Ssam#undef N 1459116742Ssam} 1460116742Ssam 1461116742Ssamint 1462116742Ssamieee80211_media2rate(int mword) 1463116742Ssam{ 1464116742Ssam#define N(a) (sizeof(a) / sizeof(a[0])) 1465116742Ssam static const int ieeerates[] = { 1466116742Ssam -1, /* IFM_AUTO */ 1467116742Ssam 0, /* IFM_MANUAL */ 1468116742Ssam 0, /* IFM_NONE */ 1469116742Ssam 2, /* IFM_IEEE80211_FH1 */ 1470116742Ssam 4, /* IFM_IEEE80211_FH2 */ 1471116742Ssam 2, /* IFM_IEEE80211_DS1 */ 1472116742Ssam 4, /* IFM_IEEE80211_DS2 */ 1473116742Ssam 11, /* IFM_IEEE80211_DS5 */ 1474116742Ssam 22, /* IFM_IEEE80211_DS11 */ 1475116742Ssam 44, /* IFM_IEEE80211_DS22 */ 1476116742Ssam 12, /* IFM_IEEE80211_OFDM6 */ 1477116742Ssam 18, /* IFM_IEEE80211_OFDM9 */ 1478116742Ssam 24, /* IFM_IEEE80211_OFDM12 */ 1479116742Ssam 36, /* IFM_IEEE80211_OFDM18 */ 1480116742Ssam 48, /* IFM_IEEE80211_OFDM24 */ 1481116742Ssam 72, /* IFM_IEEE80211_OFDM36 */ 1482116742Ssam 96, /* IFM_IEEE80211_OFDM48 */ 1483116742Ssam 108, /* IFM_IEEE80211_OFDM54 */ 1484116742Ssam 144, /* IFM_IEEE80211_OFDM72 */ 1485165569Ssam 0, /* IFM_IEEE80211_DS354k */ 1486165569Ssam 0, /* IFM_IEEE80211_DS512k */ 1487165569Ssam 6, /* IFM_IEEE80211_OFDM3 */ 1488165569Ssam 9, /* IFM_IEEE80211_OFDM4 */ 1489165569Ssam 54, /* IFM_IEEE80211_OFDM27 */ 1490170530Ssam -1, /* IFM_IEEE80211_MCS */ 1491116742Ssam }; 1492116742Ssam return IFM_SUBTYPE(mword) < N(ieeerates) ? 1493116742Ssam ieeerates[IFM_SUBTYPE(mword)] : 0; 1494116742Ssam#undef N 1495116742Ssam} 1496