ieee80211.c revision 186107
1116742Ssam/*- 2116904Ssam * Copyright (c) 2001 Atsushi Onoe 3178354Ssam * Copyright (c) 2002-2008 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 186107 2008-12-15 01:26:33Z 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")); 129170530Ssam setbit(ic->ic_chan_avail, c->ic_ieee); 130170530Ssam /* 131170530Ssam * Identify mode capabilities. 132170530Ssam */ 133170530Ssam if (IEEE80211_IS_CHAN_A(c)) 134170530Ssam setbit(ic->ic_modecaps, IEEE80211_MODE_11A); 135170530Ssam if (IEEE80211_IS_CHAN_B(c)) 136170530Ssam setbit(ic->ic_modecaps, IEEE80211_MODE_11B); 137170530Ssam if (IEEE80211_IS_CHAN_ANYG(c)) 138170530Ssam setbit(ic->ic_modecaps, IEEE80211_MODE_11G); 139170530Ssam if (IEEE80211_IS_CHAN_FHSS(c)) 140170530Ssam setbit(ic->ic_modecaps, IEEE80211_MODE_FH); 141170530Ssam if (IEEE80211_IS_CHAN_108A(c)) 142170530Ssam setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A); 143170530Ssam if (IEEE80211_IS_CHAN_108G(c)) 144170530Ssam setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G); 145170530Ssam if (IEEE80211_IS_CHAN_ST(c)) 146170530Ssam setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A); 147170530Ssam if (IEEE80211_IS_CHAN_HTA(c)) 148170530Ssam setbit(ic->ic_modecaps, IEEE80211_MODE_11NA); 149170530Ssam if (IEEE80211_IS_CHAN_HTG(c)) 150170530Ssam setbit(ic->ic_modecaps, IEEE80211_MODE_11NG); 151116742Ssam } 152170530Ssam /* initialize candidate channels to all available */ 153170530Ssam memcpy(ic->ic_chan_active, ic->ic_chan_avail, 154170530Ssam sizeof(ic->ic_chan_avail)); 155164645Ssam 156178354Ssam /* sort channel table to allow lookup optimizations */ 157178354Ssam ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans); 158178354Ssam 159178354Ssam /* invalidate any previous state */ 160170530Ssam ic->ic_bsschan = IEEE80211_CHAN_ANYC; 161172233Ssam ic->ic_prevchan = NULL; 162178354Ssam ic->ic_csa_newchan = NULL; 163170530Ssam /* arbitrarily pick the first channel */ 164170530Ssam ic->ic_curchan = &ic->ic_channels[0]; 165170530Ssam 166164645Ssam /* fillin well-known rate sets if driver has not specified */ 167165569Ssam DEFAULTRATES(IEEE80211_MODE_11B, ieee80211_rateset_11b); 168165569Ssam DEFAULTRATES(IEEE80211_MODE_11G, ieee80211_rateset_11g); 169165569Ssam DEFAULTRATES(IEEE80211_MODE_11A, ieee80211_rateset_11a); 170165569Ssam DEFAULTRATES(IEEE80211_MODE_TURBO_A, ieee80211_rateset_11a); 171165569Ssam DEFAULTRATES(IEEE80211_MODE_TURBO_G, ieee80211_rateset_11g); 172165569Ssam 173165569Ssam /* 174165569Ssam * Set auto mode to reset active channel state and any desired channel. 175165569Ssam */ 176165569Ssam (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO); 177165569Ssam#undef DEFAULTRATES 178165569Ssam} 179165569Ssam 180178354Ssamstatic void 181178354Ssamnull_update_mcast(struct ifnet *ifp) 182178354Ssam{ 183178354Ssam if_printf(ifp, "need multicast update callback\n"); 184178354Ssam} 185178354Ssam 186178354Ssamstatic void 187178354Ssamnull_update_promisc(struct ifnet *ifp) 188178354Ssam{ 189178354Ssam if_printf(ifp, "need promiscuous mode update callback\n"); 190178354Ssam} 191178354Ssam 192178521Ssamstatic int 193178521Ssamnull_output(struct ifnet *ifp, struct mbuf *m, 194178521Ssam struct sockaddr *dst, struct rtentry *rt0) 195178521Ssam{ 196178521Ssam if_printf(ifp, "discard raw packet\n"); 197178521Ssam m_freem(m); 198178521Ssam return EIO; 199178521Ssam} 200178521Ssam 201178521Ssamstatic void 202178521Ssamnull_input(struct ifnet *ifp, struct mbuf *m) 203178521Ssam{ 204178521Ssam if_printf(ifp, "if_input should not be called\n"); 205178521Ssam m_freem(m); 206178521Ssam} 207178521Ssam 208178354Ssam/* 209178354Ssam * Attach/setup the common net80211 state. Called by 210178354Ssam * the driver on attach to prior to creating any vap's. 211178354Ssam */ 212165569Ssamvoid 213165569Ssamieee80211_ifattach(struct ieee80211com *ic) 214165569Ssam{ 215165569Ssam struct ifnet *ifp = ic->ic_ifp; 216178354Ssam struct sockaddr_dl *sdl; 217178354Ssam struct ifaddr *ifa; 218165569Ssam 219178354Ssam KASSERT(ifp->if_type == IFT_IEEE80211, ("if_type %d", ifp->if_type)); 220165569Ssam 221179388Ssam IEEE80211_LOCK_INIT(ic, ifp->if_xname); 222178354Ssam TAILQ_INIT(&ic->ic_vaps); 223165569Ssam /* 224165569Ssam * Fill in 802.11 available channel set, mark all 225165569Ssam * available channels as active, and pick a default 226165569Ssam * channel if not already specified. 227165569Ssam */ 228178354Ssam ieee80211_media_init(ic); 229170530Ssam 230178354Ssam ic->ic_update_mcast = null_update_mcast; 231178354Ssam ic->ic_update_promisc = null_update_promisc; 232116742Ssam 233155688Ssam ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT; 234155688Ssam ic->ic_lintval = ic->ic_bintval; 235138568Ssam ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX; 236138568Ssam 237170530Ssam ieee80211_crypto_attach(ic); 238138568Ssam ieee80211_node_attach(ic); 239170530Ssam ieee80211_power_attach(ic); 240138568Ssam ieee80211_proto_attach(ic); 241170530Ssam ieee80211_ht_attach(ic); 242170530Ssam ieee80211_scan_attach(ic); 243178354Ssam ieee80211_regdomain_attach(ic); 244138568Ssam 245178354Ssam ieee80211_sysctl_attach(ic); 246138568Ssam 247178354Ssam ifp->if_addrlen = IEEE80211_ADDR_LEN; 248178354Ssam ifp->if_hdrlen = 0; 249178354Ssam if_attach(ifp); 250178354Ssam ifp->if_mtu = IEEE80211_MTU_MAX; 251178354Ssam ifp->if_broadcastaddr = ieee80211broadcastaddr; 252178521Ssam ifp->if_output = null_output; 253178521Ssam ifp->if_input = null_input; /* just in case */ 254178521Ssam ifp->if_resolvemulti = NULL; /* NB: callers check */ 255140915Ssam 256178354Ssam ifa = ifaddr_byindex(ifp->if_index); 257178354Ssam KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__)); 258178354Ssam sdl = (struct sockaddr_dl *)ifa->ifa_addr; 259178354Ssam sdl->sdl_type = IFT_ETHER; /* XXX IFT_IEEE80211? */ 260178354Ssam sdl->sdl_alen = IEEE80211_ADDR_LEN; 261178354Ssam IEEE80211_ADDR_COPY(LLADDR(sdl), ic->ic_myaddr); 262116742Ssam} 263116742Ssam 264178354Ssam/* 265178354Ssam * Detach net80211 state on device detach. Tear down 266178354Ssam * all vap's and reclaim all common state prior to the 267178354Ssam * device state going away. Note we may call back into 268178354Ssam * driver; it must be prepared for this. 269178354Ssam */ 270116742Ssamvoid 271138568Ssamieee80211_ifdetach(struct ieee80211com *ic) 272116742Ssam{ 273138568Ssam struct ifnet *ifp = ic->ic_ifp; 274178354Ssam struct ieee80211vap *vap; 275116742Ssam 276178354Ssam /* XXX ieee80211_stop_all? */ 277178354Ssam while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL) 278178354Ssam ieee80211_vap_destroy(vap); 279138568Ssam 280138568Ssam ieee80211_sysctl_detach(ic); 281178354Ssam ieee80211_regdomain_detach(ic); 282170530Ssam ieee80211_scan_detach(ic); 283170530Ssam ieee80211_ht_detach(ic); 284166012Ssam /* NB: must be called before ieee80211_node_detach */ 285138568Ssam ieee80211_proto_detach(ic); 286138568Ssam ieee80211_crypto_detach(ic); 287170530Ssam ieee80211_power_detach(ic); 288138568Ssam ieee80211_node_detach(ic); 289116742Ssam ifmedia_removeall(&ic->ic_media); 290138568Ssam 291170530Ssam IEEE80211_LOCK_DESTROY(ic); 292178354Ssam if_detach(ifp); 293178354Ssam} 294138568Ssam 295178354Ssam/* 296178354Ssam * Default reset method for use with the ioctl support. This 297178354Ssam * method is invoked after any state change in the 802.11 298178354Ssam * layer that should be propagated to the hardware but not 299178354Ssam * require re-initialization of the 802.11 state machine (e.g 300178354Ssam * rescanning for an ap). We always return ENETRESET which 301178354Ssam * should cause the driver to re-initialize the device. Drivers 302178354Ssam * can override this method to implement more optimized support. 303178354Ssam */ 304178354Ssamstatic int 305178354Ssamdefault_reset(struct ieee80211vap *vap, u_long cmd) 306178354Ssam{ 307178354Ssam return ENETRESET; 308178354Ssam} 309178354Ssam 310178354Ssam/* 311178354Ssam * Prepare a vap for use. Drivers use this call to 312178354Ssam * setup net80211 state in new vap's prior attaching 313178354Ssam * them with ieee80211_vap_attach (below). 314178354Ssam */ 315178354Ssamint 316178354Ssamieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap, 317178354Ssam const char name[IFNAMSIZ], int unit, int opmode, int flags, 318178354Ssam const uint8_t bssid[IEEE80211_ADDR_LEN], 319178354Ssam const uint8_t macaddr[IEEE80211_ADDR_LEN]) 320178354Ssam{ 321178354Ssam struct ifnet *ifp; 322178354Ssam 323178354Ssam ifp = if_alloc(IFT_ETHER); 324178354Ssam if (ifp == NULL) { 325178354Ssam if_printf(ic->ic_ifp, "%s: unable to allocate ifnet\n", 326178354Ssam __func__); 327178354Ssam return ENOMEM; 328178354Ssam } 329178354Ssam if_initname(ifp, name, unit); 330178354Ssam ifp->if_softc = vap; /* back pointer */ 331178354Ssam ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST; 332178354Ssam ifp->if_start = ieee80211_start; 333178354Ssam ifp->if_ioctl = ieee80211_ioctl; 334178354Ssam ifp->if_watchdog = NULL; /* NB: no watchdog routine */ 335178354Ssam ifp->if_init = ieee80211_init; 336178354Ssam /* NB: input+output filled in by ether_ifattach */ 337178354Ssam IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 338178354Ssam ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 339178354Ssam IFQ_SET_READY(&ifp->if_snd); 340178354Ssam 341178354Ssam vap->iv_ifp = ifp; 342178354Ssam vap->iv_ic = ic; 343178354Ssam vap->iv_flags = ic->ic_flags; /* propagate common flags */ 344178354Ssam vap->iv_flags_ext = ic->ic_flags_ext; 345178354Ssam vap->iv_flags_ven = ic->ic_flags_ven; 346178354Ssam vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE; 347178354Ssam vap->iv_htcaps = ic->ic_htcaps; 348178354Ssam vap->iv_opmode = opmode; 349178957Ssam vap->iv_caps |= ieee80211_opcap[opmode]; 350178354Ssam switch (opmode) { 351178354Ssam case IEEE80211_M_WDS: 352178354Ssam /* 353178354Ssam * WDS links must specify the bssid of the far end. 354178354Ssam * For legacy operation this is a static relationship. 355178354Ssam * For non-legacy operation the station must associate 356178354Ssam * and be authorized to pass traffic. Plumbing the 357178354Ssam * vap to the proper node happens when the vap 358178354Ssam * transitions to RUN state. 359178354Ssam */ 360178354Ssam IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid); 361178354Ssam vap->iv_flags |= IEEE80211_F_DESBSSID; 362178354Ssam if (flags & IEEE80211_CLONE_WDSLEGACY) 363178354Ssam vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY; 364178354Ssam break; 365178354Ssam } 366184278Ssam /* auto-enable s/w beacon miss support */ 367184278Ssam if (flags & IEEE80211_CLONE_NOBEACONS) 368184278Ssam vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS; 369178354Ssam /* 370178354Ssam * Enable various functionality by default if we're 371178354Ssam * capable; the driver can override us if it knows better. 372178354Ssam */ 373178354Ssam if (vap->iv_caps & IEEE80211_C_WME) 374178354Ssam vap->iv_flags |= IEEE80211_F_WME; 375178354Ssam if (vap->iv_caps & IEEE80211_C_BURST) 376178354Ssam vap->iv_flags |= IEEE80211_F_BURST; 377178354Ssam if (vap->iv_caps & IEEE80211_C_FF) 378178354Ssam vap->iv_flags |= IEEE80211_F_FF; 379178354Ssam if (vap->iv_caps & IEEE80211_C_TURBOP) 380178354Ssam vap->iv_flags |= IEEE80211_F_TURBOP; 381178354Ssam /* NB: bg scanning only makes sense for station mode right now */ 382178354Ssam if (vap->iv_opmode == IEEE80211_M_STA && 383178354Ssam (vap->iv_caps & IEEE80211_C_BGSCAN)) 384178354Ssam vap->iv_flags |= IEEE80211_F_BGSCAN; 385178957Ssam vap->iv_flags |= IEEE80211_F_DOTH; /* XXX no cap, just ena */ 386178954Ssam /* NB: DFS support only makes sense for ap mode right now */ 387178954Ssam if (vap->iv_opmode == IEEE80211_M_HOSTAP && 388178954Ssam (vap->iv_caps & IEEE80211_C_DFS)) 389178354Ssam vap->iv_flags_ext |= IEEE80211_FEXT_DFS; 390178354Ssam 391178354Ssam vap->iv_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */ 392178354Ssam vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT; 393178354Ssam vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT; 394178354Ssam /* 395178354Ssam * Install a default reset method for the ioctl support; 396178354Ssam * the driver can override this. 397178354Ssam */ 398178354Ssam vap->iv_reset = default_reset; 399178354Ssam 400178354Ssam IEEE80211_ADDR_COPY(vap->iv_myaddr, macaddr); 401178354Ssam 402178354Ssam ieee80211_sysctl_vattach(vap); 403178354Ssam ieee80211_crypto_vattach(vap); 404178354Ssam ieee80211_node_vattach(vap); 405178354Ssam ieee80211_power_vattach(vap); 406178354Ssam ieee80211_proto_vattach(vap); 407178354Ssam ieee80211_ht_vattach(vap); 408178354Ssam ieee80211_scan_vattach(vap); 409178354Ssam ieee80211_regdomain_vattach(vap); 410178354Ssam 411178354Ssam return 0; 412178354Ssam} 413178354Ssam 414178354Ssam/* 415178354Ssam * Activate a vap. State should have been prepared with a 416178354Ssam * call to ieee80211_vap_setup and by the driver. On return 417178354Ssam * from this call the vap is ready for use. 418178354Ssam */ 419178354Ssamint 420178354Ssamieee80211_vap_attach(struct ieee80211vap *vap, 421178354Ssam ifm_change_cb_t media_change, ifm_stat_cb_t media_stat) 422178354Ssam{ 423178354Ssam struct ifnet *ifp = vap->iv_ifp; 424178354Ssam struct ieee80211com *ic = vap->iv_ic; 425178354Ssam struct ifmediareq imr; 426178354Ssam int maxrate; 427178354Ssam 428178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, 429178354Ssam "%s: %s parent %s flags 0x%x flags_ext 0x%x\n", 430178354Ssam __func__, ieee80211_opmode_name[vap->iv_opmode], 431178354Ssam ic->ic_ifp->if_xname, vap->iv_flags, vap->iv_flags_ext); 432178354Ssam 433178354Ssam /* 434178354Ssam * Do late attach work that cannot happen until after 435178354Ssam * the driver has had a chance to override defaults. 436178354Ssam */ 437178354Ssam ieee80211_node_latevattach(vap); 438178354Ssam ieee80211_power_latevattach(vap); 439178354Ssam 440178354Ssam maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps, 441178354Ssam vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat); 442178354Ssam ieee80211_media_status(ifp, &imr); 443178354Ssam /* NB: strip explicit mode; we're actually in autoselect */ 444178354Ssam ifmedia_set(&vap->iv_media, imr.ifm_active &~ IFM_MMASK); 445178354Ssam if (maxrate) 446178354Ssam ifp->if_baudrate = IF_Mbps(maxrate); 447178354Ssam 448178354Ssam ether_ifattach(ifp, vap->iv_myaddr); 449178354Ssam /* hook output method setup by ether_ifattach */ 450178354Ssam vap->iv_output = ifp->if_output; 451178354Ssam ifp->if_output = ieee80211_output; 452178354Ssam /* NB: if_mtu set by ether_ifattach to ETHERMTU */ 453178354Ssam bpfattach2(ifp, DLT_IEEE802_11, ifp->if_hdrlen, &vap->iv_rawbpf); 454178354Ssam 455178354Ssam IEEE80211_LOCK(ic); 456178354Ssam TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next); 457178354Ssam ieee80211_syncflag_locked(ic, IEEE80211_F_WME); 458178354Ssam ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP); 459178354Ssam ieee80211_syncflag_locked(ic, IEEE80211_F_PCF); 460178354Ssam ieee80211_syncflag_locked(ic, IEEE80211_F_BURST); 461178354Ssam ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_HT); 462178354Ssam ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_USEHT40); 463178354Ssam ieee80211_syncifflag_locked(ic, IFF_PROMISC); 464178354Ssam ieee80211_syncifflag_locked(ic, IFF_ALLMULTI); 465178354Ssam IEEE80211_UNLOCK(ic); 466178354Ssam 467178354Ssam return 1; 468178354Ssam} 469178354Ssam 470178354Ssam/* 471178354Ssam * Tear down vap state and reclaim the ifnet. 472178354Ssam * The driver is assumed to have prepared for 473178354Ssam * this; e.g. by turning off interrupts for the 474178354Ssam * underlying device. 475178354Ssam */ 476178354Ssamvoid 477178354Ssamieee80211_vap_detach(struct ieee80211vap *vap) 478178354Ssam{ 479178354Ssam struct ieee80211com *ic = vap->iv_ic; 480178354Ssam struct ifnet *ifp = vap->iv_ifp; 481178354Ssam 482178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n", 483178354Ssam __func__, ieee80211_opmode_name[vap->iv_opmode], 484178354Ssam ic->ic_ifp->if_xname); 485178354Ssam 486178354Ssam IEEE80211_LOCK(ic); 487178354Ssam /* block traffic from above */ 488178354Ssam ifp->if_drv_flags |= IFF_DRV_OACTIVE; 489178354Ssam /* 490178354Ssam * Evil hack. Clear the backpointer from the ifnet to the 491178354Ssam * vap so any requests from above will return an error or 492178354Ssam * be ignored. In particular this short-circuits requests 493178354Ssam * by the bridge to turn off promiscuous mode as a result 494178354Ssam * of calling ether_ifdetach. 495178354Ssam */ 496178354Ssam ifp->if_softc = NULL; 497178354Ssam /* 498178354Ssam * Stop the vap before detaching the ifnet. Ideally we'd 499178354Ssam * do this in the other order so the ifnet is inaccessible 500178354Ssam * while we cleanup internal state but that is hard. 501178354Ssam */ 502178354Ssam ieee80211_stop_locked(vap); 503178354Ssam 504178354Ssam /* XXX accumulate iv_stats in ic_stats? */ 505178354Ssam TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next); 506178354Ssam ieee80211_syncflag_locked(ic, IEEE80211_F_WME); 507178354Ssam ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP); 508178354Ssam ieee80211_syncflag_locked(ic, IEEE80211_F_PCF); 509178354Ssam ieee80211_syncflag_locked(ic, IEEE80211_F_BURST); 510178354Ssam ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_HT); 511178354Ssam ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_USEHT40); 512178354Ssam ieee80211_syncifflag_locked(ic, IFF_PROMISC); 513178354Ssam ieee80211_syncifflag_locked(ic, IFF_ALLMULTI); 514178354Ssam IEEE80211_UNLOCK(ic); 515178354Ssam 516178354Ssam /* XXX can't hold com lock */ 517178354Ssam /* NB: bpfattach is called by ether_ifdetach and claims all taps */ 518116742Ssam ether_ifdetach(ifp); 519178354Ssam 520178354Ssam ifmedia_removeall(&vap->iv_media); 521178354Ssam 522178354Ssam ieee80211_regdomain_vdetach(vap); 523178354Ssam ieee80211_scan_vdetach(vap); 524178354Ssam ieee80211_ht_vdetach(vap); 525178354Ssam /* NB: must be before ieee80211_node_vdetach */ 526178354Ssam ieee80211_proto_vdetach(vap); 527178354Ssam ieee80211_crypto_vdetach(vap); 528178354Ssam ieee80211_power_vdetach(vap); 529178354Ssam ieee80211_node_vdetach(vap); 530178354Ssam ieee80211_sysctl_vdetach(vap); 531182674Sweongyo 532182674Sweongyo if_free(ifp); 533116742Ssam} 534116742Ssam 535178354Ssam/* 536178354Ssam * Synchronize flag bit state in the parent ifnet structure 537178354Ssam * according to the state of all vap ifnet's. This is used, 538178354Ssam * for example, to handle IFF_PROMISC and IFF_ALLMULTI. 539178354Ssam */ 540178354Ssamvoid 541178354Ssamieee80211_syncifflag_locked(struct ieee80211com *ic, int flag) 542178354Ssam{ 543178354Ssam struct ifnet *ifp = ic->ic_ifp; 544178354Ssam struct ieee80211vap *vap; 545178354Ssam int bit, oflags; 546178354Ssam 547178354Ssam IEEE80211_LOCK_ASSERT(ic); 548178354Ssam 549178354Ssam bit = 0; 550178354Ssam TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 551178354Ssam if (vap->iv_ifp->if_flags & flag) { 552178354Ssam /* 553178354Ssam * XXX the bridge sets PROMISC but we don't want to 554178354Ssam * enable it on the device, discard here so all the 555178354Ssam * drivers don't need to special-case it 556178354Ssam */ 557178354Ssam if (flag == IFF_PROMISC && 558178354Ssam vap->iv_opmode == IEEE80211_M_HOSTAP) 559178354Ssam continue; 560178354Ssam bit = 1; 561178354Ssam break; 562178354Ssam } 563178354Ssam oflags = ifp->if_flags; 564178354Ssam if (bit) 565178354Ssam ifp->if_flags |= flag; 566178354Ssam else 567178354Ssam ifp->if_flags &= ~flag; 568178354Ssam if ((ifp->if_flags ^ oflags) & flag) { 569178354Ssam /* XXX should we return 1/0 and let caller do this? */ 570178354Ssam if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 571178354Ssam if (flag == IFF_PROMISC) 572178354Ssam ic->ic_update_promisc(ifp); 573178354Ssam else if (flag == IFF_ALLMULTI) 574178354Ssam ic->ic_update_mcast(ifp); 575178354Ssam } 576178354Ssam } 577178354Ssam} 578178354Ssam 579178354Ssam/* 580178354Ssam * Synchronize flag bit state in the com structure 581178354Ssam * according to the state of all vap's. This is used, 582178354Ssam * for example, to handle state changes via ioctls. 583178354Ssam */ 584178354Ssamstatic void 585178354Ssamieee80211_syncflag_locked(struct ieee80211com *ic, int flag) 586178354Ssam{ 587178354Ssam struct ieee80211vap *vap; 588178354Ssam int bit; 589178354Ssam 590178354Ssam IEEE80211_LOCK_ASSERT(ic); 591178354Ssam 592178354Ssam bit = 0; 593178354Ssam TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 594178354Ssam if (vap->iv_flags & flag) { 595178354Ssam bit = 1; 596178354Ssam break; 597178354Ssam } 598178354Ssam if (bit) 599178354Ssam ic->ic_flags |= flag; 600178354Ssam else 601178354Ssam ic->ic_flags &= ~flag; 602178354Ssam} 603178354Ssam 604178354Ssamvoid 605178354Ssamieee80211_syncflag(struct ieee80211vap *vap, int flag) 606178354Ssam{ 607178354Ssam struct ieee80211com *ic = vap->iv_ic; 608178354Ssam 609178354Ssam IEEE80211_LOCK(ic); 610178354Ssam if (flag < 0) { 611178354Ssam flag = -flag; 612178354Ssam vap->iv_flags &= ~flag; 613178354Ssam } else 614178354Ssam vap->iv_flags |= flag; 615178354Ssam ieee80211_syncflag_locked(ic, flag); 616178354Ssam IEEE80211_UNLOCK(ic); 617178354Ssam} 618178354Ssam 619178354Ssam/* 620178354Ssam * Synchronize flag bit state in the com structure 621178354Ssam * according to the state of all vap's. This is used, 622178354Ssam * for example, to handle state changes via ioctls. 623178354Ssam */ 624178354Ssamstatic void 625178354Ssamieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag) 626178354Ssam{ 627178354Ssam struct ieee80211vap *vap; 628178354Ssam int bit; 629178354Ssam 630178354Ssam IEEE80211_LOCK_ASSERT(ic); 631178354Ssam 632178354Ssam bit = 0; 633178354Ssam TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 634178354Ssam if (vap->iv_flags_ext & flag) { 635178354Ssam bit = 1; 636178354Ssam break; 637178354Ssam } 638178354Ssam if (bit) 639178354Ssam ic->ic_flags_ext |= flag; 640178354Ssam else 641178354Ssam ic->ic_flags_ext &= ~flag; 642178354Ssam} 643178354Ssam 644178354Ssamvoid 645178354Ssamieee80211_syncflag_ext(struct ieee80211vap *vap, int flag) 646178354Ssam{ 647178354Ssam struct ieee80211com *ic = vap->iv_ic; 648178354Ssam 649178354Ssam IEEE80211_LOCK(ic); 650178354Ssam if (flag < 0) { 651178354Ssam flag = -flag; 652178354Ssam vap->iv_flags_ext &= ~flag; 653178354Ssam } else 654178354Ssam vap->iv_flags_ext |= flag; 655178354Ssam ieee80211_syncflag_ext_locked(ic, flag); 656178354Ssam IEEE80211_UNLOCK(ic); 657178354Ssam} 658178354Ssam 659166012Ssamstatic __inline int 660166012Ssammapgsm(u_int freq, u_int flags) 661166012Ssam{ 662166012Ssam freq *= 10; 663166012Ssam if (flags & IEEE80211_CHAN_QUARTER) 664166012Ssam freq += 5; 665166012Ssam else if (flags & IEEE80211_CHAN_HALF) 666166012Ssam freq += 10; 667166012Ssam else 668166012Ssam freq += 20; 669166012Ssam /* NB: there is no 907/20 wide but leave room */ 670166012Ssam return (freq - 906*10) / 5; 671166012Ssam} 672166012Ssam 673166012Ssamstatic __inline int 674166012Ssammappsb(u_int freq, u_int flags) 675166012Ssam{ 676166012Ssam return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5; 677166012Ssam} 678166012Ssam 679116742Ssam/* 680116742Ssam * Convert MHz frequency to IEEE channel number. 681116742Ssam */ 682152450Ssamint 683116742Ssamieee80211_mhz2ieee(u_int freq, u_int flags) 684116742Ssam{ 685167430Ssam#define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990) 686166012Ssam if (flags & IEEE80211_CHAN_GSM) 687166012Ssam return mapgsm(freq, flags); 688116742Ssam if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 689116742Ssam if (freq == 2484) 690116742Ssam return 14; 691116742Ssam if (freq < 2484) 692152450Ssam return ((int) freq - 2407) / 5; 693116742Ssam else 694116742Ssam return 15 + ((freq - 2512) / 20); 695116899Ssam } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */ 696165569Ssam if (freq <= 5000) { 697170530Ssam /* XXX check regdomain? */ 698167430Ssam if (IS_FREQ_IN_PSB(freq)) 699166012Ssam return mappsb(freq, flags); 700152450Ssam return (freq - 4000) / 5; 701165569Ssam } else 702152450Ssam return (freq - 5000) / 5; 703116742Ssam } else { /* either, guess */ 704116742Ssam if (freq == 2484) 705116742Ssam return 14; 706166012Ssam if (freq < 2484) { 707166012Ssam if (907 <= freq && freq <= 922) 708166012Ssam return mapgsm(freq, flags); 709152450Ssam return ((int) freq - 2407) / 5; 710166012Ssam } 711152450Ssam if (freq < 5000) { 712167430Ssam if (IS_FREQ_IN_PSB(freq)) 713166012Ssam return mappsb(freq, flags); 714165569Ssam else if (freq > 4900) 715152450Ssam return (freq - 4000) / 5; 716152450Ssam else 717152450Ssam return 15 + ((freq - 2512) / 20); 718152450Ssam } 719116742Ssam return (freq - 5000) / 5; 720116742Ssam } 721167430Ssam#undef IS_FREQ_IN_PSB 722116742Ssam} 723116742Ssam 724116742Ssam/* 725116742Ssam * Convert channel to IEEE channel number. 726116742Ssam */ 727152450Ssamint 728165825Smjacobieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c) 729116742Ssam{ 730170530Ssam if (c == NULL) { 731138568Ssam if_printf(ic->ic_ifp, "invalid channel (NULL)\n"); 732117039Ssam return 0; /* XXX */ 733116742Ssam } 734170530Ssam return (c == IEEE80211_CHAN_ANYC ? IEEE80211_CHAN_ANY : c->ic_ieee); 735116742Ssam} 736116742Ssam 737116742Ssam/* 738116742Ssam * Convert IEEE channel number to MHz frequency. 739116742Ssam */ 740116742Ssamu_int 741116742Ssamieee80211_ieee2mhz(u_int chan, u_int flags) 742116742Ssam{ 743166012Ssam if (flags & IEEE80211_CHAN_GSM) 744166012Ssam return 907 + 5 * (chan / 10); 745116742Ssam if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 746116742Ssam if (chan == 14) 747116742Ssam return 2484; 748116742Ssam if (chan < 14) 749116742Ssam return 2407 + chan*5; 750116742Ssam else 751116742Ssam return 2512 + ((chan-15)*20); 752116742Ssam } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */ 753165569Ssam if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) { 754165569Ssam chan -= 37; 755165569Ssam return 4940 + chan*5 + (chan % 5 ? 2 : 0); 756165569Ssam } 757116742Ssam return 5000 + (chan*5); 758116742Ssam } else { /* either, guess */ 759166012Ssam /* XXX can't distinguish PSB+GSM channels */ 760116742Ssam if (chan == 14) 761116742Ssam return 2484; 762116742Ssam if (chan < 14) /* 0-13 */ 763116742Ssam return 2407 + chan*5; 764116742Ssam if (chan < 27) /* 15-26 */ 765116742Ssam return 2512 + ((chan-15)*20); 766116742Ssam return 5000 + (chan*5); 767116742Ssam } 768116742Ssam} 769116742Ssam 770116742Ssam/* 771170530Ssam * Locate a channel given a frequency+flags. We cache 772178354Ssam * the previous lookup to optimize switching between two 773170530Ssam * channels--as happens with dynamic turbo. 774170530Ssam */ 775170530Ssamstruct ieee80211_channel * 776170530Ssamieee80211_find_channel(struct ieee80211com *ic, int freq, int flags) 777170530Ssam{ 778170530Ssam struct ieee80211_channel *c; 779170530Ssam int i; 780170530Ssam 781170530Ssam flags &= IEEE80211_CHAN_ALLTURBO; 782170530Ssam c = ic->ic_prevchan; 783170530Ssam if (c != NULL && c->ic_freq == freq && 784170530Ssam (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 785170530Ssam return c; 786170530Ssam /* brute force search */ 787170530Ssam for (i = 0; i < ic->ic_nchans; i++) { 788170530Ssam c = &ic->ic_channels[i]; 789170530Ssam if (c->ic_freq == freq && 790170530Ssam (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 791170530Ssam return c; 792170530Ssam } 793170530Ssam return NULL; 794170530Ssam} 795170530Ssam 796173861Ssam/* 797173861Ssam * Locate a channel given a channel number+flags. We cache 798173861Ssam * the previous lookup to optimize switching between two 799173861Ssam * channels--as happens with dynamic turbo. 800173861Ssam */ 801173861Ssamstruct ieee80211_channel * 802173861Ssamieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags) 803173861Ssam{ 804173861Ssam struct ieee80211_channel *c; 805173861Ssam int i; 806173861Ssam 807173861Ssam flags &= IEEE80211_CHAN_ALLTURBO; 808173861Ssam c = ic->ic_prevchan; 809173861Ssam if (c != NULL && c->ic_ieee == ieee && 810173861Ssam (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 811173861Ssam return c; 812173861Ssam /* brute force search */ 813173861Ssam for (i = 0; i < ic->ic_nchans; i++) { 814173861Ssam c = &ic->ic_channels[i]; 815173861Ssam if (c->ic_ieee == ieee && 816173861Ssam (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 817173861Ssam return c; 818173861Ssam } 819173861Ssam return NULL; 820173861Ssam} 821173861Ssam 822170530Ssamstatic void 823178354Ssamaddmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword) 824170530Ssam{ 825170530Ssam#define ADD(_ic, _s, _o) \ 826178354Ssam ifmedia_add(media, \ 827170530Ssam IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL) 828170530Ssam static const u_int mopts[IEEE80211_MODE_MAX] = { 829178354Ssam IFM_AUTO, 830178354Ssam IFM_IEEE80211_11A, 831178354Ssam IFM_IEEE80211_11B, 832178354Ssam IFM_IEEE80211_11G, 833178354Ssam IFM_IEEE80211_FH, 834178354Ssam IFM_IEEE80211_11A | IFM_IEEE80211_TURBO, 835178354Ssam IFM_IEEE80211_11G | IFM_IEEE80211_TURBO, 836178354Ssam IFM_IEEE80211_11A | IFM_IEEE80211_TURBO, 837178354Ssam IFM_IEEE80211_11NA, 838178354Ssam IFM_IEEE80211_11NG, 839170530Ssam }; 840170530Ssam u_int mopt; 841170530Ssam 842170530Ssam mopt = mopts[mode]; 843178354Ssam if (addsta) 844178354Ssam ADD(ic, mword, mopt); /* STA mode has no cap */ 845178354Ssam if (caps & IEEE80211_C_IBSS) 846178354Ssam ADD(media, mword, mopt | IFM_IEEE80211_ADHOC); 847178354Ssam if (caps & IEEE80211_C_HOSTAP) 848178354Ssam ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP); 849178354Ssam if (caps & IEEE80211_C_AHDEMO) 850178354Ssam ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0); 851178354Ssam if (caps & IEEE80211_C_MONITOR) 852178354Ssam ADD(media, mword, mopt | IFM_IEEE80211_MONITOR); 853178354Ssam if (caps & IEEE80211_C_WDS) 854178354Ssam ADD(media, mword, mopt | IFM_IEEE80211_WDS); 855170530Ssam#undef ADD 856170530Ssam} 857170530Ssam 858170530Ssam/* 859116742Ssam * Setup the media data structures according to the channel and 860178354Ssam * rate tables. 861116742Ssam */ 862178354Ssamstatic int 863178354Ssamieee80211_media_setup(struct ieee80211com *ic, 864178354Ssam struct ifmedia *media, int caps, int addsta, 865116742Ssam ifm_change_cb_t media_change, ifm_stat_cb_t media_stat) 866116742Ssam{ 867170530Ssam int i, j, mode, rate, maxrate, mword, r; 868170530Ssam const struct ieee80211_rateset *rs; 869116742Ssam struct ieee80211_rateset allrates; 870116742Ssam 871118887Ssam /* 872116742Ssam * Fill in media characteristics. 873116742Ssam */ 874178354Ssam ifmedia_init(media, 0, media_change, media_stat); 875116742Ssam maxrate = 0; 876170530Ssam /* 877170530Ssam * Add media for legacy operating modes. 878170530Ssam */ 879116742Ssam memset(&allrates, 0, sizeof(allrates)); 880170530Ssam for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) { 881167468Ssam if (isclr(ic->ic_modecaps, mode)) 882116742Ssam continue; 883178354Ssam addmedia(media, caps, addsta, mode, IFM_AUTO); 884116742Ssam if (mode == IEEE80211_MODE_AUTO) 885116742Ssam continue; 886116742Ssam rs = &ic->ic_sup_rates[mode]; 887116742Ssam for (i = 0; i < rs->rs_nrates; i++) { 888116742Ssam rate = rs->rs_rates[i]; 889116742Ssam mword = ieee80211_rate2media(ic, rate, mode); 890116742Ssam if (mword == 0) 891116742Ssam continue; 892178354Ssam addmedia(media, caps, addsta, mode, mword); 893116742Ssam /* 894170530Ssam * Add legacy rate to the collection of all rates. 895116742Ssam */ 896116742Ssam r = rate & IEEE80211_RATE_VAL; 897116742Ssam for (j = 0; j < allrates.rs_nrates; j++) 898116742Ssam if (allrates.rs_rates[j] == r) 899116742Ssam break; 900116742Ssam if (j == allrates.rs_nrates) { 901116742Ssam /* unique, add to the set */ 902116742Ssam allrates.rs_rates[j] = r; 903116742Ssam allrates.rs_nrates++; 904116742Ssam } 905116742Ssam rate = (rate & IEEE80211_RATE_VAL) / 2; 906116742Ssam if (rate > maxrate) 907116742Ssam maxrate = rate; 908116742Ssam } 909116742Ssam } 910116742Ssam for (i = 0; i < allrates.rs_nrates; i++) { 911116742Ssam mword = ieee80211_rate2media(ic, allrates.rs_rates[i], 912116742Ssam IEEE80211_MODE_AUTO); 913116742Ssam if (mword == 0) 914116742Ssam continue; 915170530Ssam /* NB: remove media options from mword */ 916178354Ssam addmedia(media, caps, addsta, 917178354Ssam IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword)); 918116742Ssam } 919170530Ssam /* 920170530Ssam * Add HT/11n media. Note that we do not have enough 921170530Ssam * bits in the media subtype to express the MCS so we 922170530Ssam * use a "placeholder" media subtype and any fixed MCS 923170530Ssam * must be specified with a different mechanism. 924170530Ssam */ 925170530Ssam for (; mode < IEEE80211_MODE_MAX; mode++) { 926170530Ssam if (isclr(ic->ic_modecaps, mode)) 927170530Ssam continue; 928178354Ssam addmedia(media, caps, addsta, mode, IFM_AUTO); 929178354Ssam addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS); 930170530Ssam } 931170530Ssam if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) || 932170530Ssam isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) { 933178354Ssam addmedia(media, caps, addsta, 934178354Ssam IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS); 935170530Ssam /* XXX could walk htrates */ 936170530Ssam /* XXX known array size */ 937178354Ssam if (ieee80211_htrates[15].ht40_rate_400ns > maxrate) 938178354Ssam maxrate = ieee80211_htrates[15].ht40_rate_400ns; 939170530Ssam } 940178354Ssam return maxrate; 941178354Ssam} 942116742Ssam 943178354Ssamvoid 944178354Ssamieee80211_media_init(struct ieee80211com *ic) 945178354Ssam{ 946178354Ssam struct ifnet *ifp = ic->ic_ifp; 947178354Ssam int maxrate; 948178354Ssam 949178354Ssam /* NB: this works because the structure is initialized to zero */ 950178354Ssam if (!LIST_EMPTY(&ic->ic_media.ifm_list)) { 951178354Ssam /* 952178354Ssam * We are re-initializing the channel list; clear 953178354Ssam * the existing media state as the media routines 954178354Ssam * don't suppress duplicates. 955178354Ssam */ 956178354Ssam ifmedia_removeall(&ic->ic_media); 957178354Ssam } 958178354Ssam ieee80211_chan_init(ic); 959178354Ssam 960178354Ssam /* 961178354Ssam * Recalculate media settings in case new channel list changes 962178354Ssam * the set of available modes. 963178354Ssam */ 964178354Ssam maxrate = ieee80211_media_setup(ic, &ic->ic_media, ic->ic_caps, 1, 965178354Ssam ieee80211com_media_change, ieee80211com_media_status); 966170530Ssam /* NB: strip explicit mode; we're actually in autoselect */ 967170530Ssam ifmedia_set(&ic->ic_media, 968170530Ssam media_status(ic->ic_opmode, ic->ic_curchan) &~ IFM_MMASK); 969116742Ssam if (maxrate) 970116742Ssam ifp->if_baudrate = IF_Mbps(maxrate); 971178354Ssam 972178354Ssam /* XXX need to propagate new media settings to vap's */ 973116742Ssam} 974116742Ssam 975165569Ssamconst struct ieee80211_rateset * 976165569Ssamieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c) 977165569Ssam{ 978166012Ssam if (IEEE80211_IS_CHAN_HALF(c)) 979166012Ssam return &ieee80211_rateset_half; 980166012Ssam if (IEEE80211_IS_CHAN_QUARTER(c)) 981166012Ssam return &ieee80211_rateset_quarter; 982170530Ssam if (IEEE80211_IS_CHAN_HTA(c)) 983170530Ssam return &ic->ic_sup_rates[IEEE80211_MODE_11A]; 984170530Ssam if (IEEE80211_IS_CHAN_HTG(c)) { 985170530Ssam /* XXX does this work for basic rates? */ 986170530Ssam return &ic->ic_sup_rates[IEEE80211_MODE_11G]; 987170530Ssam } 988170530Ssam return &ic->ic_sup_rates[ieee80211_chan2mode(c)]; 989165569Ssam} 990165569Ssam 991138568Ssamvoid 992138568Ssamieee80211_announce(struct ieee80211com *ic) 993138568Ssam{ 994138568Ssam struct ifnet *ifp = ic->ic_ifp; 995138568Ssam int i, mode, rate, mword; 996170530Ssam const struct ieee80211_rateset *rs; 997138568Ssam 998172227Ssam /* NB: skip AUTO since it has no rates */ 999172227Ssam for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) { 1000167468Ssam if (isclr(ic->ic_modecaps, mode)) 1001138568Ssam continue; 1002138568Ssam if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]); 1003138568Ssam rs = &ic->ic_sup_rates[mode]; 1004138568Ssam for (i = 0; i < rs->rs_nrates; i++) { 1005170530Ssam mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode); 1006138568Ssam if (mword == 0) 1007138568Ssam continue; 1008170530Ssam rate = ieee80211_media2rate(mword); 1009138568Ssam printf("%s%d%sMbps", (i != 0 ? " " : ""), 1010170530Ssam rate / 2, ((rate & 0x1) != 0 ? ".5" : "")); 1011138568Ssam } 1012138568Ssam printf("\n"); 1013138568Ssam } 1014170530Ssam ieee80211_ht_announce(ic); 1015138568Ssam} 1016138568Ssam 1017170530Ssamvoid 1018170530Ssamieee80211_announce_channels(struct ieee80211com *ic) 1019116742Ssam{ 1020170530Ssam const struct ieee80211_channel *c; 1021170530Ssam char type; 1022170530Ssam int i, cw; 1023170530Ssam 1024170530Ssam printf("Chan Freq CW RegPwr MinPwr MaxPwr\n"); 1025170530Ssam for (i = 0; i < ic->ic_nchans; i++) { 1026170530Ssam c = &ic->ic_channels[i]; 1027170530Ssam if (IEEE80211_IS_CHAN_ST(c)) 1028170530Ssam type = 'S'; 1029170530Ssam else if (IEEE80211_IS_CHAN_108A(c)) 1030170530Ssam type = 'T'; 1031170530Ssam else if (IEEE80211_IS_CHAN_108G(c)) 1032170530Ssam type = 'G'; 1033170530Ssam else if (IEEE80211_IS_CHAN_HT(c)) 1034170530Ssam type = 'n'; 1035170530Ssam else if (IEEE80211_IS_CHAN_A(c)) 1036170530Ssam type = 'a'; 1037170530Ssam else if (IEEE80211_IS_CHAN_ANYG(c)) 1038170530Ssam type = 'g'; 1039170530Ssam else if (IEEE80211_IS_CHAN_B(c)) 1040170530Ssam type = 'b'; 1041170530Ssam else 1042170530Ssam type = 'f'; 1043170530Ssam if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c)) 1044170530Ssam cw = 40; 1045170530Ssam else if (IEEE80211_IS_CHAN_HALF(c)) 1046170530Ssam cw = 10; 1047170530Ssam else if (IEEE80211_IS_CHAN_QUARTER(c)) 1048170530Ssam cw = 5; 1049170530Ssam else 1050170530Ssam cw = 20; 1051170530Ssam printf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n" 1052170530Ssam , c->ic_ieee, c->ic_freq, type 1053170530Ssam , cw 1054170530Ssam , IEEE80211_IS_CHAN_HT40U(c) ? '+' : 1055170530Ssam IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' ' 1056170530Ssam , c->ic_maxregpower 1057170530Ssam , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0 1058170530Ssam , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0 1059170530Ssam ); 1060170530Ssam } 1061116742Ssam} 1062116742Ssam 1063170530Ssamstatic int 1064184273Ssammedia2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode) 1065170530Ssam{ 1066116742Ssam switch (IFM_MODE(ime->ifm_media)) { 1067116742Ssam case IFM_IEEE80211_11A: 1068178354Ssam *mode = IEEE80211_MODE_11A; 1069116742Ssam break; 1070116742Ssam case IFM_IEEE80211_11B: 1071178354Ssam *mode = IEEE80211_MODE_11B; 1072116742Ssam break; 1073116742Ssam case IFM_IEEE80211_11G: 1074178354Ssam *mode = IEEE80211_MODE_11G; 1075116742Ssam break; 1076124543Sonoe case IFM_IEEE80211_FH: 1077178354Ssam *mode = IEEE80211_MODE_FH; 1078124543Sonoe break; 1079170530Ssam case IFM_IEEE80211_11NA: 1080178354Ssam *mode = IEEE80211_MODE_11NA; 1081170530Ssam break; 1082170530Ssam case IFM_IEEE80211_11NG: 1083178354Ssam *mode = IEEE80211_MODE_11NG; 1084170530Ssam break; 1085116742Ssam case IFM_AUTO: 1086178354Ssam *mode = IEEE80211_MODE_AUTO; 1087116742Ssam break; 1088116742Ssam default: 1089178354Ssam return 0; 1090116742Ssam } 1091116742Ssam /* 1092138568Ssam * Turbo mode is an ``option''. 1093138568Ssam * XXX does not apply to AUTO 1094116742Ssam */ 1095116742Ssam if (ime->ifm_media & IFM_IEEE80211_TURBO) { 1096178354Ssam if (*mode == IEEE80211_MODE_11A) { 1097184273Ssam if (flags & IEEE80211_F_TURBOP) 1098178354Ssam *mode = IEEE80211_MODE_TURBO_A; 1099170530Ssam else 1100178354Ssam *mode = IEEE80211_MODE_STURBO_A; 1101178354Ssam } else if (*mode == IEEE80211_MODE_11G) 1102178354Ssam *mode = IEEE80211_MODE_TURBO_G; 1103138568Ssam else 1104178354Ssam return 0; 1105116742Ssam } 1106170530Ssam /* XXX HT40 +/- */ 1107178354Ssam return 1; 1108178354Ssam} 1109116742Ssam 1110178354Ssam/* 1111184273Ssam * Handle a media change request on the underlying interface. 1112178354Ssam */ 1113178354Ssamint 1114178354Ssamieee80211com_media_change(struct ifnet *ifp) 1115178354Ssam{ 1116184273Ssam return EINVAL; 1117178354Ssam} 1118116742Ssam 1119178354Ssam/* 1120178354Ssam * Handle a media change request on the vap interface. 1121178354Ssam */ 1122178354Ssamint 1123178354Ssamieee80211_media_change(struct ifnet *ifp) 1124178354Ssam{ 1125178354Ssam struct ieee80211vap *vap = ifp->if_softc; 1126178354Ssam struct ifmedia_entry *ime = vap->iv_media.ifm_cur; 1127184273Ssam uint16_t newmode; 1128178354Ssam 1129184273Ssam if (!media2mode(ime, vap->iv_flags, &newmode)) 1130178354Ssam return EINVAL; 1131184273Ssam if (vap->iv_des_mode != newmode) { 1132184273Ssam vap->iv_des_mode = newmode; 1133178354Ssam return ENETRESET; 1134178354Ssam } 1135178354Ssam return 0; 1136116742Ssam} 1137116742Ssam 1138170530Ssam/* 1139170530Ssam * Common code to calculate the media status word 1140170530Ssam * from the operating mode and channel state. 1141170530Ssam */ 1142170530Ssamstatic int 1143170530Ssammedia_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan) 1144170530Ssam{ 1145170530Ssam int status; 1146170530Ssam 1147170530Ssam status = IFM_IEEE80211; 1148170530Ssam switch (opmode) { 1149170530Ssam case IEEE80211_M_STA: 1150170530Ssam break; 1151170530Ssam case IEEE80211_M_IBSS: 1152170530Ssam status |= IFM_IEEE80211_ADHOC; 1153170530Ssam break; 1154170530Ssam case IEEE80211_M_HOSTAP: 1155170530Ssam status |= IFM_IEEE80211_HOSTAP; 1156170530Ssam break; 1157170530Ssam case IEEE80211_M_MONITOR: 1158170530Ssam status |= IFM_IEEE80211_MONITOR; 1159170530Ssam break; 1160170530Ssam case IEEE80211_M_AHDEMO: 1161170530Ssam status |= IFM_IEEE80211_ADHOC | IFM_FLAG0; 1162170530Ssam break; 1163170530Ssam case IEEE80211_M_WDS: 1164178354Ssam status |= IFM_IEEE80211_WDS; 1165170530Ssam break; 1166170530Ssam } 1167170530Ssam if (IEEE80211_IS_CHAN_HTA(chan)) { 1168170530Ssam status |= IFM_IEEE80211_11NA; 1169170530Ssam } else if (IEEE80211_IS_CHAN_HTG(chan)) { 1170170530Ssam status |= IFM_IEEE80211_11NG; 1171170530Ssam } else if (IEEE80211_IS_CHAN_A(chan)) { 1172170530Ssam status |= IFM_IEEE80211_11A; 1173170530Ssam } else if (IEEE80211_IS_CHAN_B(chan)) { 1174170530Ssam status |= IFM_IEEE80211_11B; 1175170530Ssam } else if (IEEE80211_IS_CHAN_ANYG(chan)) { 1176170530Ssam status |= IFM_IEEE80211_11G; 1177170530Ssam } else if (IEEE80211_IS_CHAN_FHSS(chan)) { 1178170530Ssam status |= IFM_IEEE80211_FH; 1179170530Ssam } 1180170530Ssam /* XXX else complain? */ 1181170530Ssam 1182170530Ssam if (IEEE80211_IS_CHAN_TURBO(chan)) 1183170530Ssam status |= IFM_IEEE80211_TURBO; 1184178354Ssam#if 0 1185178354Ssam if (IEEE80211_IS_CHAN_HT20(chan)) 1186178354Ssam status |= IFM_IEEE80211_HT20; 1187178354Ssam if (IEEE80211_IS_CHAN_HT40(chan)) 1188178354Ssam status |= IFM_IEEE80211_HT40; 1189178354Ssam#endif 1190170530Ssam return status; 1191170530Ssam} 1192170530Ssam 1193178354Ssamstatic void 1194178354Ssamieee80211com_media_status(struct ifnet *ifp, struct ifmediareq *imr) 1195178354Ssam{ 1196178354Ssam struct ieee80211com *ic = ifp->if_l2com; 1197178354Ssam struct ieee80211vap *vap; 1198178354Ssam 1199178354Ssam imr->ifm_status = IFM_AVALID; 1200178354Ssam TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 1201178354Ssam if (vap->iv_ifp->if_flags & IFF_UP) { 1202178354Ssam imr->ifm_status |= IFM_ACTIVE; 1203178354Ssam break; 1204178354Ssam } 1205178354Ssam imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan); 1206178354Ssam if (imr->ifm_status & IFM_ACTIVE) 1207178354Ssam imr->ifm_current = imr->ifm_active; 1208178354Ssam} 1209178354Ssam 1210116742Ssamvoid 1211116742Ssamieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr) 1212116742Ssam{ 1213178354Ssam struct ieee80211vap *vap = ifp->if_softc; 1214178354Ssam struct ieee80211com *ic = vap->iv_ic; 1215170530Ssam enum ieee80211_phymode mode; 1216116742Ssam 1217116742Ssam imr->ifm_status = IFM_AVALID; 1218170530Ssam /* 1219170530Ssam * NB: use the current channel's mode to lock down a xmit 1220170530Ssam * rate only when running; otherwise we may have a mismatch 1221170530Ssam * in which case the rate will not be convertible. 1222170530Ssam */ 1223178354Ssam if (vap->iv_state == IEEE80211_S_RUN) { 1224116742Ssam imr->ifm_status |= IFM_ACTIVE; 1225170530Ssam mode = ieee80211_chan2mode(ic->ic_curchan); 1226170530Ssam } else 1227170530Ssam mode = IEEE80211_MODE_AUTO; 1228178354Ssam imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan); 1229138568Ssam /* 1230138568Ssam * Calculate a current rate if possible. 1231138568Ssam */ 1232178354Ssam if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) { 1233138568Ssam /* 1234138568Ssam * A fixed rate is set, report that. 1235138568Ssam */ 1236138568Ssam imr->ifm_active |= ieee80211_rate2media(ic, 1237178354Ssam vap->iv_txparms[mode].ucastrate, mode); 1238178354Ssam } else if (vap->iv_opmode == IEEE80211_M_STA) { 1239138568Ssam /* 1240138568Ssam * In station mode report the current transmit rate. 1241138568Ssam */ 1242138568Ssam imr->ifm_active |= ieee80211_rate2media(ic, 1243178354Ssam vap->iv_bss->ni_txrate, mode); 1244128966Sandre } else 1245138568Ssam imr->ifm_active |= IFM_AUTO; 1246178354Ssam if (imr->ifm_status & IFM_ACTIVE) 1247178354Ssam imr->ifm_current = imr->ifm_active; 1248116742Ssam} 1249116742Ssam 1250116742Ssam/* 1251116742Ssam * Set the current phy mode and recalculate the active channel 1252116742Ssam * set based on the available channels for this mode. Also 1253116742Ssam * select a new default/current channel if the current one is 1254116742Ssam * inappropriate for this mode. 1255116742Ssam */ 1256116742Ssamint 1257116742Ssamieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode) 1258116742Ssam{ 1259116742Ssam /* 1260166012Ssam * Adjust basic rates in 11b/11g supported rate set. 1261166012Ssam * Note that if operating on a hal/quarter rate channel 1262166012Ssam * this is a noop as those rates sets are different 1263166012Ssam * and used instead. 1264116742Ssam */ 1265166012Ssam if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B) 1266178354Ssam ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode); 1267166012Ssam 1268116742Ssam ic->ic_curmode = mode; 1269138568Ssam ieee80211_reset_erp(ic); /* reset ERP state */ 1270138568Ssam 1271116742Ssam return 0; 1272116742Ssam} 1273116742Ssam 1274116742Ssam/* 1275170530Ssam * Return the phy mode for with the specified channel. 1276116742Ssam */ 1277116742Ssamenum ieee80211_phymode 1278170530Ssamieee80211_chan2mode(const struct ieee80211_channel *chan) 1279116742Ssam{ 1280170530Ssam 1281170530Ssam if (IEEE80211_IS_CHAN_HTA(chan)) 1282170530Ssam return IEEE80211_MODE_11NA; 1283170530Ssam else if (IEEE80211_IS_CHAN_HTG(chan)) 1284170530Ssam return IEEE80211_MODE_11NG; 1285170530Ssam else if (IEEE80211_IS_CHAN_108G(chan)) 1286170530Ssam return IEEE80211_MODE_TURBO_G; 1287170530Ssam else if (IEEE80211_IS_CHAN_ST(chan)) 1288170530Ssam return IEEE80211_MODE_STURBO_A; 1289170530Ssam else if (IEEE80211_IS_CHAN_TURBO(chan)) 1290153350Ssam return IEEE80211_MODE_TURBO_A; 1291170530Ssam else if (IEEE80211_IS_CHAN_A(chan)) 1292116742Ssam return IEEE80211_MODE_11A; 1293170530Ssam else if (IEEE80211_IS_CHAN_ANYG(chan)) 1294116742Ssam return IEEE80211_MODE_11G; 1295170530Ssam else if (IEEE80211_IS_CHAN_B(chan)) 1296116742Ssam return IEEE80211_MODE_11B; 1297170530Ssam else if (IEEE80211_IS_CHAN_FHSS(chan)) 1298170530Ssam return IEEE80211_MODE_FH; 1299170530Ssam 1300170530Ssam /* NB: should not get here */ 1301170530Ssam printf("%s: cannot map channel to mode; freq %u flags 0x%x\n", 1302170530Ssam __func__, chan->ic_freq, chan->ic_flags); 1303170530Ssam return IEEE80211_MODE_11B; 1304116742Ssam} 1305116742Ssam 1306170530Ssamstruct ratemedia { 1307170530Ssam u_int match; /* rate + mode */ 1308170530Ssam u_int media; /* if_media rate */ 1309170530Ssam}; 1310170530Ssam 1311170530Ssamstatic int 1312170530Ssamfindmedia(const struct ratemedia rates[], int n, u_int match) 1313170530Ssam{ 1314170530Ssam int i; 1315170530Ssam 1316170530Ssam for (i = 0; i < n; i++) 1317170530Ssam if (rates[i].match == match) 1318170530Ssam return rates[i].media; 1319170530Ssam return IFM_AUTO; 1320170530Ssam} 1321170530Ssam 1322116742Ssam/* 1323170530Ssam * Convert IEEE80211 rate value to ifmedia subtype. 1324170530Ssam * Rate is either a legacy rate in units of 0.5Mbps 1325170530Ssam * or an MCS index. 1326116742Ssam */ 1327116742Ssamint 1328116742Ssamieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode) 1329116742Ssam{ 1330116742Ssam#define N(a) (sizeof(a) / sizeof(a[0])) 1331170530Ssam static const struct ratemedia rates[] = { 1332124543Sonoe { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 }, 1333124543Sonoe { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 }, 1334124543Sonoe { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 }, 1335124543Sonoe { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 }, 1336124543Sonoe { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 }, 1337124543Sonoe { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 }, 1338124543Sonoe { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 }, 1339124543Sonoe { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 }, 1340124543Sonoe { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 }, 1341124543Sonoe { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 }, 1342124543Sonoe { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 }, 1343124543Sonoe { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 }, 1344124543Sonoe { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 }, 1345124543Sonoe { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 }, 1346124543Sonoe { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 }, 1347124543Sonoe { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 }, 1348124543Sonoe { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 }, 1349124543Sonoe { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 }, 1350124543Sonoe { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 }, 1351124543Sonoe { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 }, 1352124543Sonoe { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 }, 1353124543Sonoe { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 }, 1354124543Sonoe { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 }, 1355124543Sonoe { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 }, 1356124543Sonoe { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 }, 1357124543Sonoe { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 }, 1358124543Sonoe { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 }, 1359165569Ssam { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 }, 1360165569Ssam { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 }, 1361165569Ssam { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 }, 1362116742Ssam /* NB: OFDM72 doesn't realy exist so we don't handle it */ 1363116742Ssam }; 1364170530Ssam static const struct ratemedia htrates[] = { 1365170530Ssam { 0, IFM_IEEE80211_MCS }, 1366170530Ssam { 1, IFM_IEEE80211_MCS }, 1367170530Ssam { 2, IFM_IEEE80211_MCS }, 1368170530Ssam { 3, IFM_IEEE80211_MCS }, 1369170530Ssam { 4, IFM_IEEE80211_MCS }, 1370170530Ssam { 5, IFM_IEEE80211_MCS }, 1371170530Ssam { 6, IFM_IEEE80211_MCS }, 1372170530Ssam { 7, IFM_IEEE80211_MCS }, 1373170530Ssam { 8, IFM_IEEE80211_MCS }, 1374170530Ssam { 9, IFM_IEEE80211_MCS }, 1375170530Ssam { 10, IFM_IEEE80211_MCS }, 1376170530Ssam { 11, IFM_IEEE80211_MCS }, 1377170530Ssam { 12, IFM_IEEE80211_MCS }, 1378170530Ssam { 13, IFM_IEEE80211_MCS }, 1379170530Ssam { 14, IFM_IEEE80211_MCS }, 1380170530Ssam { 15, IFM_IEEE80211_MCS }, 1381170530Ssam }; 1382170530Ssam int m; 1383116742Ssam 1384170530Ssam /* 1385170530Ssam * Check 11n rates first for match as an MCS. 1386170530Ssam */ 1387170530Ssam if (mode == IEEE80211_MODE_11NA) { 1388172226Ssam if (rate & IEEE80211_RATE_MCS) { 1389172226Ssam rate &= ~IEEE80211_RATE_MCS; 1390170530Ssam m = findmedia(htrates, N(htrates), rate); 1391170530Ssam if (m != IFM_AUTO) 1392170530Ssam return m | IFM_IEEE80211_11NA; 1393170530Ssam } 1394170530Ssam } else if (mode == IEEE80211_MODE_11NG) { 1395170530Ssam /* NB: 12 is ambiguous, it will be treated as an MCS */ 1396172226Ssam if (rate & IEEE80211_RATE_MCS) { 1397172226Ssam rate &= ~IEEE80211_RATE_MCS; 1398170530Ssam m = findmedia(htrates, N(htrates), rate); 1399170530Ssam if (m != IFM_AUTO) 1400170530Ssam return m | IFM_IEEE80211_11NG; 1401170530Ssam } 1402170530Ssam } 1403170530Ssam rate &= IEEE80211_RATE_VAL; 1404116742Ssam switch (mode) { 1405116742Ssam case IEEE80211_MODE_11A: 1406170530Ssam case IEEE80211_MODE_11NA: 1407138568Ssam case IEEE80211_MODE_TURBO_A: 1408170530Ssam case IEEE80211_MODE_STURBO_A: 1409170530Ssam return findmedia(rates, N(rates), rate | IFM_IEEE80211_11A); 1410116742Ssam case IEEE80211_MODE_11B: 1411170530Ssam return findmedia(rates, N(rates), rate | IFM_IEEE80211_11B); 1412124543Sonoe case IEEE80211_MODE_FH: 1413170530Ssam return findmedia(rates, N(rates), rate | IFM_IEEE80211_FH); 1414116742Ssam case IEEE80211_MODE_AUTO: 1415116742Ssam /* NB: ic may be NULL for some drivers */ 1416170530Ssam if (ic && ic->ic_phytype == IEEE80211_T_FH) 1417170530Ssam return findmedia(rates, N(rates), 1418170530Ssam rate | IFM_IEEE80211_FH); 1419116742Ssam /* NB: hack, 11g matches both 11b+11a rates */ 1420116742Ssam /* fall thru... */ 1421116742Ssam case IEEE80211_MODE_11G: 1422170530Ssam case IEEE80211_MODE_11NG: 1423138568Ssam case IEEE80211_MODE_TURBO_G: 1424170530Ssam return findmedia(rates, N(rates), rate | IFM_IEEE80211_11G); 1425116742Ssam } 1426116742Ssam return IFM_AUTO; 1427116742Ssam#undef N 1428116742Ssam} 1429116742Ssam 1430116742Ssamint 1431116742Ssamieee80211_media2rate(int mword) 1432116742Ssam{ 1433116742Ssam#define N(a) (sizeof(a) / sizeof(a[0])) 1434116742Ssam static const int ieeerates[] = { 1435116742Ssam -1, /* IFM_AUTO */ 1436116742Ssam 0, /* IFM_MANUAL */ 1437116742Ssam 0, /* IFM_NONE */ 1438116742Ssam 2, /* IFM_IEEE80211_FH1 */ 1439116742Ssam 4, /* IFM_IEEE80211_FH2 */ 1440116742Ssam 2, /* IFM_IEEE80211_DS1 */ 1441116742Ssam 4, /* IFM_IEEE80211_DS2 */ 1442116742Ssam 11, /* IFM_IEEE80211_DS5 */ 1443116742Ssam 22, /* IFM_IEEE80211_DS11 */ 1444116742Ssam 44, /* IFM_IEEE80211_DS22 */ 1445116742Ssam 12, /* IFM_IEEE80211_OFDM6 */ 1446116742Ssam 18, /* IFM_IEEE80211_OFDM9 */ 1447116742Ssam 24, /* IFM_IEEE80211_OFDM12 */ 1448116742Ssam 36, /* IFM_IEEE80211_OFDM18 */ 1449116742Ssam 48, /* IFM_IEEE80211_OFDM24 */ 1450116742Ssam 72, /* IFM_IEEE80211_OFDM36 */ 1451116742Ssam 96, /* IFM_IEEE80211_OFDM48 */ 1452116742Ssam 108, /* IFM_IEEE80211_OFDM54 */ 1453116742Ssam 144, /* IFM_IEEE80211_OFDM72 */ 1454165569Ssam 0, /* IFM_IEEE80211_DS354k */ 1455165569Ssam 0, /* IFM_IEEE80211_DS512k */ 1456165569Ssam 6, /* IFM_IEEE80211_OFDM3 */ 1457165569Ssam 9, /* IFM_IEEE80211_OFDM4 */ 1458165569Ssam 54, /* IFM_IEEE80211_OFDM27 */ 1459170530Ssam -1, /* IFM_IEEE80211_MCS */ 1460116742Ssam }; 1461116742Ssam return IFM_SUBTYPE(mword) < N(ieeerates) ? 1462116742Ssam ieeerates[IFM_SUBTYPE(mword)] : 0; 1463116742Ssam#undef N 1464116742Ssam} 1465