ieee80211_proto.c revision 167442
1116742Ssam/*- 2116904Ssam * Copyright (c) 2001 Atsushi Onoe 3139530Ssam * Copyright (c) 2002-2005 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. 14116904Ssam * 3. The name of the author may not be used to endorse or promote products 15116904Ssam * derived from this software without specific prior written permission. 16116742Ssam * 17116742Ssam * Alternatively, this software may be distributed under the terms of the 18116742Ssam * GNU General Public License ("GPL") version 2 as published by the Free 19116742Ssam * Software Foundation. 20116742Ssam * 21116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30116904Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31116742Ssam */ 32116742Ssam 33116742Ssam#include <sys/cdefs.h> 34116742Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_proto.c 167442 2007-03-11 07:22:21Z sam $"); 35116742Ssam 36116742Ssam/* 37116742Ssam * IEEE 802.11 protocol support. 38116742Ssam */ 39116742Ssam 40116742Ssam#include "opt_inet.h" 41116742Ssam 42116742Ssam#include <sys/param.h> 43138568Ssam#include <sys/kernel.h> 44116742Ssam#include <sys/systm.h> 45138568Ssam 46116742Ssam#include <sys/socket.h> 47116742Ssam 48116742Ssam#include <net/if.h> 49116742Ssam#include <net/if_media.h> 50138568Ssam#include <net/ethernet.h> /* XXX for ether_sprintf */ 51116742Ssam 52116742Ssam#include <net80211/ieee80211_var.h> 53116742Ssam 54138568Ssam/* XXX tunables */ 55138568Ssam#define AGGRESSIVE_MODE_SWITCH_HYSTERESIS 3 /* pkts / 100ms */ 56138568Ssam#define HIGH_PRI_SWITCH_THRESH 10 /* pkts / 100ms */ 57116742Ssam 58116742Ssam#define IEEE80211_RATE2MBS(r) (((r) & IEEE80211_RATE_VAL) / 2) 59116742Ssam 60116742Ssamconst char *ieee80211_mgt_subtype_name[] = { 61116742Ssam "assoc_req", "assoc_resp", "reassoc_req", "reassoc_resp", 62116742Ssam "probe_req", "probe_resp", "reserved#6", "reserved#7", 63116742Ssam "beacon", "atim", "disassoc", "auth", 64116742Ssam "deauth", "reserved#13", "reserved#14", "reserved#15" 65116742Ssam}; 66138568Ssamconst char *ieee80211_ctl_subtype_name[] = { 67138568Ssam "reserved#0", "reserved#1", "reserved#2", "reserved#3", 68138568Ssam "reserved#3", "reserved#5", "reserved#6", "reserved#7", 69138568Ssam "reserved#8", "reserved#9", "ps_poll", "rts", 70138568Ssam "cts", "ack", "cf_end", "cf_end_ack" 71138568Ssam}; 72167283Ssamconst char *ieee80211_opmode_name[IEEE80211_OPMODE_MAX] = { 73167283Ssam "IBSS", /* IEEE80211_M_IBSS */ 74167283Ssam "STA", /* IEEE80211_M_STA */ 75167283Ssam "#2", 76167283Ssam "AHDEMO", /* IEEE80211_M_AHDEMO */ 77167283Ssam "#4", "#5", 78167283Ssam "HOSTAP", /* IEEE80211_M_HOSTAP */ 79167283Ssam "#7", 80167283Ssam "MONITOR" /* IEEE80211_M_MONITOR */ 81167283Ssam}; 82117811Ssamconst char *ieee80211_state_name[IEEE80211_S_MAX] = { 83117811Ssam "INIT", /* IEEE80211_S_INIT */ 84117811Ssam "SCAN", /* IEEE80211_S_SCAN */ 85117811Ssam "AUTH", /* IEEE80211_S_AUTH */ 86117811Ssam "ASSOC", /* IEEE80211_S_ASSOC */ 87117811Ssam "RUN" /* IEEE80211_S_RUN */ 88117811Ssam}; 89138568Ssamconst char *ieee80211_wme_acnames[] = { 90138568Ssam "WME_AC_BE", 91138568Ssam "WME_AC_BK", 92138568Ssam "WME_AC_VI", 93138568Ssam "WME_AC_VO", 94138568Ssam "WME_UPSD", 95138568Ssam}; 96116742Ssam 97117811Ssamstatic int ieee80211_newstate(struct ieee80211com *, enum ieee80211_state, int); 98117811Ssam 99116742Ssamvoid 100138568Ssamieee80211_proto_attach(struct ieee80211com *ic) 101116742Ssam{ 102138568Ssam struct ifnet *ifp = ic->ic_ifp; 103116742Ssam 104138568Ssam /* XXX room for crypto */ 105138568Ssam ifp->if_hdrlen = sizeof(struct ieee80211_qosframe_addr4); 106116742Ssam 107116742Ssam ic->ic_rtsthreshold = IEEE80211_RTS_DEFAULT; 108148291Ssam ic->ic_fragthreshold = IEEE80211_FRAG_DEFAULT; 109148290Ssam ic->ic_fixed_rate = IEEE80211_FIXED_RATE_NONE; 110153349Ssam ic->ic_bmiss_max = IEEE80211_BMISS_MAX; 111154736Ssam callout_init(&ic->ic_swbmiss, CALLOUT_MPSAFE); 112153346Ssam ic->ic_mcast_rate = IEEE80211_MCAST_RATE_DEFAULT; 113127648Ssam ic->ic_protmode = IEEE80211_PROT_CTSONLY; 114138568Ssam ic->ic_roaming = IEEE80211_ROAMING_AUTO; 115116742Ssam 116138568Ssam ic->ic_wme.wme_hipri_switch_hysteresis = 117138568Ssam AGGRESSIVE_MODE_SWITCH_HYSTERESIS; 118138568Ssam 119121816Sbrooks mtx_init(&ic->ic_mgtq.ifq_mtx, ifp->if_xname, "mgmt send q", MTX_DEF); 120116742Ssam 121117811Ssam /* protocol state change handler */ 122117811Ssam ic->ic_newstate = ieee80211_newstate; 123117811Ssam 124116742Ssam /* initialize management frame handlers */ 125116742Ssam ic->ic_recv_mgmt = ieee80211_recv_mgmt; 126116742Ssam ic->ic_send_mgmt = ieee80211_send_mgmt; 127160690Ssam ic->ic_raw_xmit = ieee80211_raw_xmit; 128116742Ssam} 129116742Ssam 130116742Ssamvoid 131138568Ssamieee80211_proto_detach(struct ieee80211com *ic) 132116742Ssam{ 133116742Ssam 134138568Ssam /* 135138568Ssam * This should not be needed as we detach when reseting 136138568Ssam * the state but be conservative here since the 137138568Ssam * authenticator may do things like spawn kernel threads. 138138568Ssam */ 139138568Ssam if (ic->ic_auth->ia_detach) 140138568Ssam ic->ic_auth->ia_detach(ic); 141138568Ssam 142165894Ssam ieee80211_drain_ifq(&ic->ic_mgtq); 143116742Ssam mtx_destroy(&ic->ic_mgtq.ifq_mtx); 144138568Ssam 145138568Ssam /* 146138568Ssam * Detach any ACL'ator. 147138568Ssam */ 148138568Ssam if (ic->ic_acl != NULL) 149138568Ssam ic->ic_acl->iac_detach(ic); 150116742Ssam} 151116742Ssam 152138568Ssam/* 153138568Ssam * Simple-minded authenticator module support. 154138568Ssam */ 155138568Ssam 156138568Ssam#define IEEE80211_AUTH_MAX (IEEE80211_AUTH_WPA+1) 157138568Ssam/* XXX well-known names */ 158138568Ssamstatic const char *auth_modnames[IEEE80211_AUTH_MAX] = { 159138568Ssam "wlan_internal", /* IEEE80211_AUTH_NONE */ 160138568Ssam "wlan_internal", /* IEEE80211_AUTH_OPEN */ 161138568Ssam "wlan_internal", /* IEEE80211_AUTH_SHARED */ 162138568Ssam "wlan_xauth", /* IEEE80211_AUTH_8021X */ 163138568Ssam "wlan_internal", /* IEEE80211_AUTH_AUTO */ 164138568Ssam "wlan_xauth", /* IEEE80211_AUTH_WPA */ 165138568Ssam}; 166138568Ssamstatic const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX]; 167138568Ssam 168138568Ssamstatic const struct ieee80211_authenticator auth_internal = { 169138568Ssam .ia_name = "wlan_internal", 170138568Ssam .ia_attach = NULL, 171138568Ssam .ia_detach = NULL, 172138568Ssam .ia_node_join = NULL, 173138568Ssam .ia_node_leave = NULL, 174138568Ssam}; 175138568Ssam 176138568Ssam/* 177138568Ssam * Setup internal authenticators once; they are never unregistered. 178138568Ssam */ 179138568Ssamstatic void 180138568Ssamieee80211_auth_setup(void) 181138568Ssam{ 182138568Ssam ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal); 183138568Ssam ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal); 184138568Ssam ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal); 185138568Ssam} 186138568SsamSYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL); 187138568Ssam 188138568Ssamconst struct ieee80211_authenticator * 189138568Ssamieee80211_authenticator_get(int auth) 190138568Ssam{ 191138568Ssam if (auth >= IEEE80211_AUTH_MAX) 192138568Ssam return NULL; 193138568Ssam if (authenticators[auth] == NULL) 194138568Ssam ieee80211_load_module(auth_modnames[auth]); 195138568Ssam return authenticators[auth]; 196138568Ssam} 197138568Ssam 198116742Ssamvoid 199138568Ssamieee80211_authenticator_register(int type, 200138568Ssam const struct ieee80211_authenticator *auth) 201116742Ssam{ 202138568Ssam if (type >= IEEE80211_AUTH_MAX) 203138568Ssam return; 204138568Ssam authenticators[type] = auth; 205138568Ssam} 206138568Ssam 207138568Ssamvoid 208138568Ssamieee80211_authenticator_unregister(int type) 209138568Ssam{ 210138568Ssam 211138568Ssam if (type >= IEEE80211_AUTH_MAX) 212138568Ssam return; 213138568Ssam authenticators[type] = NULL; 214138568Ssam} 215138568Ssam 216138568Ssam/* 217138568Ssam * Very simple-minded ACL module support. 218138568Ssam */ 219138568Ssam/* XXX just one for now */ 220138568Ssamstatic const struct ieee80211_aclator *acl = NULL; 221138568Ssam 222138568Ssamvoid 223138568Ssamieee80211_aclator_register(const struct ieee80211_aclator *iac) 224138568Ssam{ 225138568Ssam printf("wlan: %s acl policy registered\n", iac->iac_name); 226138568Ssam acl = iac; 227138568Ssam} 228138568Ssam 229138568Ssamvoid 230138568Ssamieee80211_aclator_unregister(const struct ieee80211_aclator *iac) 231138568Ssam{ 232138568Ssam if (acl == iac) 233138568Ssam acl = NULL; 234138568Ssam printf("wlan: %s acl policy unregistered\n", iac->iac_name); 235138568Ssam} 236138568Ssam 237138568Ssamconst struct ieee80211_aclator * 238138568Ssamieee80211_aclator_get(const char *name) 239138568Ssam{ 240138568Ssam if (acl == NULL) 241138568Ssam ieee80211_load_module("wlan_acl"); 242138568Ssam return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL; 243138568Ssam} 244138568Ssam 245138568Ssamvoid 246138568Ssamieee80211_print_essid(const u_int8_t *essid, int len) 247138568Ssam{ 248138568Ssam const u_int8_t *p; 249116742Ssam int i; 250116742Ssam 251116742Ssam if (len > IEEE80211_NWID_LEN) 252116742Ssam len = IEEE80211_NWID_LEN; 253116742Ssam /* determine printable or not */ 254116742Ssam for (i = 0, p = essid; i < len; i++, p++) { 255116742Ssam if (*p < ' ' || *p > 0x7e) 256116742Ssam break; 257116742Ssam } 258116742Ssam if (i == len) { 259116742Ssam printf("\""); 260116742Ssam for (i = 0, p = essid; i < len; i++, p++) 261116742Ssam printf("%c", *p); 262116742Ssam printf("\""); 263116742Ssam } else { 264116742Ssam printf("0x"); 265116742Ssam for (i = 0, p = essid; i < len; i++, p++) 266116742Ssam printf("%02x", *p); 267116742Ssam } 268116742Ssam} 269116742Ssam 270116742Ssamvoid 271138568Ssamieee80211_dump_pkt(const u_int8_t *buf, int len, int rate, int rssi) 272116742Ssam{ 273138568Ssam const struct ieee80211_frame *wh; 274116742Ssam int i; 275116742Ssam 276138568Ssam wh = (const struct ieee80211_frame *)buf; 277116742Ssam switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) { 278116742Ssam case IEEE80211_FC1_DIR_NODS: 279116742Ssam printf("NODS %s", ether_sprintf(wh->i_addr2)); 280116742Ssam printf("->%s", ether_sprintf(wh->i_addr1)); 281116742Ssam printf("(%s)", ether_sprintf(wh->i_addr3)); 282116742Ssam break; 283116742Ssam case IEEE80211_FC1_DIR_TODS: 284116742Ssam printf("TODS %s", ether_sprintf(wh->i_addr2)); 285116742Ssam printf("->%s", ether_sprintf(wh->i_addr3)); 286116742Ssam printf("(%s)", ether_sprintf(wh->i_addr1)); 287116742Ssam break; 288116742Ssam case IEEE80211_FC1_DIR_FROMDS: 289116742Ssam printf("FRDS %s", ether_sprintf(wh->i_addr3)); 290116742Ssam printf("->%s", ether_sprintf(wh->i_addr1)); 291116742Ssam printf("(%s)", ether_sprintf(wh->i_addr2)); 292116742Ssam break; 293116742Ssam case IEEE80211_FC1_DIR_DSTODS: 294138568Ssam printf("DSDS %s", ether_sprintf((const u_int8_t *)&wh[1])); 295116742Ssam printf("->%s", ether_sprintf(wh->i_addr3)); 296116742Ssam printf("(%s", ether_sprintf(wh->i_addr2)); 297116742Ssam printf("->%s)", ether_sprintf(wh->i_addr1)); 298116742Ssam break; 299116742Ssam } 300116742Ssam switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) { 301116742Ssam case IEEE80211_FC0_TYPE_DATA: 302116742Ssam printf(" data"); 303116742Ssam break; 304116742Ssam case IEEE80211_FC0_TYPE_MGT: 305116742Ssam printf(" %s", ieee80211_mgt_subtype_name[ 306116742Ssam (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) 307116742Ssam >> IEEE80211_FC0_SUBTYPE_SHIFT]); 308116742Ssam break; 309116742Ssam default: 310116742Ssam printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK); 311116742Ssam break; 312116742Ssam } 313138568Ssam if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 314138568Ssam int i; 315138568Ssam printf(" WEP [IV"); 316138568Ssam for (i = 0; i < IEEE80211_WEP_IVLEN; i++) 317138568Ssam printf(" %.02x", buf[sizeof(*wh)+i]); 318138568Ssam printf(" KID %u]", buf[sizeof(*wh)+i] >> 6); 319138568Ssam } 320116742Ssam if (rate >= 0) 321116742Ssam printf(" %dM", rate / 2); 322116742Ssam if (rssi >= 0) 323116742Ssam printf(" +%d", rssi); 324116742Ssam printf("\n"); 325116742Ssam if (len > 0) { 326116742Ssam for (i = 0; i < len; i++) { 327116742Ssam if ((i & 1) == 0) 328116742Ssam printf(" "); 329116742Ssam printf("%02x", buf[i]); 330116742Ssam } 331116742Ssam printf("\n"); 332116742Ssam } 333116742Ssam} 334116742Ssam 335165887Ssamstatic __inline int 336165887Ssamfindrix(const struct ieee80211_rateset *rs, int r) 337165887Ssam{ 338165887Ssam int i; 339165887Ssam 340165887Ssam for (i = 0; i < rs->rs_nrates; i++) 341165887Ssam if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r) 342165887Ssam return i; 343165887Ssam return -1; 344165887Ssam} 345165887Ssam 346116742Ssamint 347167442Ssamieee80211_fix_rate(struct ieee80211_node *ni, 348167442Ssam struct ieee80211_rateset *nrs, int flags) 349116742Ssam{ 350116742Ssam#define RV(v) ((v) & IEEE80211_RATE_VAL) 351148299Ssam struct ieee80211com *ic = ni->ni_ic; 352165887Ssam int i, j, rix, error; 353138568Ssam int okrate, badrate, fixedrate; 354165569Ssam const struct ieee80211_rateset *srs; 355116742Ssam u_int8_t r; 356116742Ssam 357138568Ssam /* 358138568Ssam * If the fixed rate check was requested but no 359138568Ssam * fixed has been defined then just remove it. 360138568Ssam */ 361148290Ssam if ((flags & IEEE80211_F_DOFRATE) && 362148290Ssam ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) 363138568Ssam flags &= ~IEEE80211_F_DOFRATE; 364116742Ssam error = 0; 365138568Ssam okrate = badrate = fixedrate = 0; 366165569Ssam srs = ieee80211_get_suprates(ic, ni->ni_chan); 367120482Ssam for (i = 0; i < nrs->rs_nrates; ) { 368116742Ssam if (flags & IEEE80211_F_DOSORT) { 369116742Ssam /* 370116742Ssam * Sort rates. 371116742Ssam */ 372116742Ssam for (j = i + 1; j < nrs->rs_nrates; j++) { 373116742Ssam if (RV(nrs->rs_rates[i]) > RV(nrs->rs_rates[j])) { 374116742Ssam r = nrs->rs_rates[i]; 375116742Ssam nrs->rs_rates[i] = nrs->rs_rates[j]; 376116742Ssam nrs->rs_rates[j] = r; 377116742Ssam } 378116742Ssam } 379116742Ssam } 380116742Ssam r = nrs->rs_rates[i] & IEEE80211_RATE_VAL; 381116742Ssam badrate = r; 382116742Ssam if (flags & IEEE80211_F_DOFRATE) { 383116742Ssam /* 384138568Ssam * Check any fixed rate is included. 385116742Ssam */ 386138568Ssam if (r == RV(srs->rs_rates[ic->ic_fixed_rate])) 387138568Ssam fixedrate = r; 388116742Ssam } 389165887Ssam /* 390165887Ssam * Check against supported rates. 391165887Ssam */ 392165887Ssam rix = findrix(srs, r); 393116742Ssam if (flags & IEEE80211_F_DONEGO) { 394165887Ssam if (rix < 0) { 395120482Ssam /* 396120482Ssam * A rate in the node's rate set is not 397120482Ssam * supported. If this is a basic rate and we 398165887Ssam * are operating as a STA then this is an error. 399120482Ssam * Otherwise we just discard/ignore the rate. 400120482Ssam */ 401165887Ssam if ((flags & IEEE80211_F_JOIN) && 402120482Ssam (nrs->rs_rates[i] & IEEE80211_RATE_BASIC)) 403116742Ssam error++; 404165887Ssam } else if ((flags & IEEE80211_F_JOIN) == 0) { 405165887Ssam /* 406165887Ssam * Overwrite with the supported rate 407165887Ssam * value so any basic rate bit is set. 408165887Ssam */ 409165887Ssam nrs->rs_rates[i] = srs->rs_rates[rix]; 410116742Ssam } 411116742Ssam } 412165887Ssam if ((flags & IEEE80211_F_DODEL) && rix < 0) { 413116742Ssam /* 414116742Ssam * Delete unacceptable rates. 415116742Ssam */ 416165887Ssam nrs->rs_nrates--; 417165887Ssam for (j = i; j < nrs->rs_nrates; j++) 418165887Ssam nrs->rs_rates[j] = nrs->rs_rates[j + 1]; 419165887Ssam nrs->rs_rates[j] = 0; 420165887Ssam continue; 421116742Ssam } 422165887Ssam if (rix >= 0) 423116742Ssam okrate = nrs->rs_rates[i]; 424116742Ssam i++; 425116742Ssam } 426138568Ssam if (okrate == 0 || error != 0 || 427138568Ssam ((flags & IEEE80211_F_DOFRATE) && fixedrate == 0)) 428116742Ssam return badrate | IEEE80211_RATE_BASIC; 429116742Ssam else 430116742Ssam return RV(okrate); 431116742Ssam#undef RV 432116742Ssam} 433116742Ssam 434138568Ssam/* 435138568Ssam * Reset 11g-related state. 436138568Ssam */ 437138568Ssamvoid 438138568Ssamieee80211_reset_erp(struct ieee80211com *ic) 439138568Ssam{ 440138568Ssam ic->ic_flags &= ~IEEE80211_F_USEPROT; 441138568Ssam ic->ic_nonerpsta = 0; 442138568Ssam ic->ic_longslotsta = 0; 443138568Ssam /* 444138568Ssam * Short slot time is enabled only when operating in 11g 445138568Ssam * and not in an IBSS. We must also honor whether or not 446138568Ssam * the driver is capable of doing it. 447138568Ssam */ 448138568Ssam ieee80211_set_shortslottime(ic, 449138568Ssam ic->ic_curmode == IEEE80211_MODE_11A || 450138568Ssam (ic->ic_curmode == IEEE80211_MODE_11G && 451138568Ssam ic->ic_opmode == IEEE80211_M_HOSTAP && 452138568Ssam (ic->ic_caps & IEEE80211_C_SHSLOT))); 453138568Ssam /* 454138568Ssam * Set short preamble and ERP barker-preamble flags. 455138568Ssam */ 456138568Ssam if (ic->ic_curmode == IEEE80211_MODE_11A || 457138568Ssam (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) { 458138568Ssam ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 459138568Ssam ic->ic_flags &= ~IEEE80211_F_USEBARKER; 460138568Ssam } else { 461138568Ssam ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE; 462138568Ssam ic->ic_flags |= IEEE80211_F_USEBARKER; 463138568Ssam } 464138568Ssam} 465138568Ssam 466138568Ssam/* 467138568Ssam * Set the short slot time state and notify the driver. 468138568Ssam */ 469138568Ssamvoid 470138568Ssamieee80211_set_shortslottime(struct ieee80211com *ic, int onoff) 471138568Ssam{ 472138568Ssam if (onoff) 473138568Ssam ic->ic_flags |= IEEE80211_F_SHSLOT; 474138568Ssam else 475138568Ssam ic->ic_flags &= ~IEEE80211_F_SHSLOT; 476138568Ssam /* notify driver */ 477138568Ssam if (ic->ic_updateslot != NULL) 478138568Ssam ic->ic_updateslot(ic->ic_ifp); 479138568Ssam} 480138568Ssam 481138568Ssam/* 482138568Ssam * Check if the specified rate set supports ERP. 483138568Ssam * NB: the rate set is assumed to be sorted. 484138568Ssam */ 485138568Ssamint 486138568Ssamieee80211_iserp_rateset(struct ieee80211com *ic, struct ieee80211_rateset *rs) 487138568Ssam{ 488138568Ssam#define N(a) (sizeof(a) / sizeof(a[0])) 489138568Ssam static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 }; 490138568Ssam int i, j; 491138568Ssam 492138568Ssam if (rs->rs_nrates < N(rates)) 493138568Ssam return 0; 494138568Ssam for (i = 0; i < N(rates); i++) { 495138568Ssam for (j = 0; j < rs->rs_nrates; j++) { 496138568Ssam int r = rs->rs_rates[j] & IEEE80211_RATE_VAL; 497138568Ssam if (rates[i] == r) 498138568Ssam goto next; 499138568Ssam if (r > rates[i]) 500138568Ssam return 0; 501138568Ssam } 502138568Ssam return 0; 503138568Ssam next: 504138568Ssam ; 505138568Ssam } 506138568Ssam return 1; 507138568Ssam#undef N 508138568Ssam} 509138568Ssam 510138568Ssam/* 511138568Ssam * Mark the basic rates for the 11g rate table based on the 512138568Ssam * operating mode. For real 11g we mark all the 11b rates 513138568Ssam * and 6, 12, and 24 OFDM. For 11b compatibility we mark only 514138568Ssam * 11b rates. There's also a pseudo 11a-mode used to mark only 515138568Ssam * the basic OFDM rates. 516138568Ssam */ 517138568Ssamvoid 518138568Ssamieee80211_set11gbasicrates(struct ieee80211_rateset *rs, enum ieee80211_phymode mode) 519138568Ssam{ 520138568Ssam static const struct ieee80211_rateset basic[] = { 521138568Ssam { 0 }, /* IEEE80211_MODE_AUTO */ 522138568Ssam { 3, { 12, 24, 48 } }, /* IEEE80211_MODE_11A */ 523138568Ssam { 2, { 2, 4 } }, /* IEEE80211_MODE_11B */ 524138568Ssam { 4, { 2, 4, 11, 22 } }, /* IEEE80211_MODE_11G (mixed b/g) */ 525138568Ssam { 0 }, /* IEEE80211_MODE_FH */ 526138568Ssam /* IEEE80211_MODE_PUREG (not yet) */ 527138568Ssam { 7, { 2, 4, 11, 22, 12, 24, 48 } }, 528138568Ssam }; 529138568Ssam int i, j; 530138568Ssam 531138568Ssam for (i = 0; i < rs->rs_nrates; i++) { 532138568Ssam rs->rs_rates[i] &= IEEE80211_RATE_VAL; 533138568Ssam for (j = 0; j < basic[mode].rs_nrates; j++) 534138568Ssam if (basic[mode].rs_rates[j] == rs->rs_rates[i]) { 535138568Ssam rs->rs_rates[i] |= IEEE80211_RATE_BASIC; 536138568Ssam break; 537138568Ssam } 538138568Ssam } 539138568Ssam} 540138568Ssam 541138568Ssam/* 542138568Ssam * WME protocol support. The following parameters come from the spec. 543138568Ssam */ 544138568Ssamtypedef struct phyParamType { 545138568Ssam u_int8_t aifsn; 546138568Ssam u_int8_t logcwmin; 547138568Ssam u_int8_t logcwmax; 548138568Ssam u_int16_t txopLimit; 549138568Ssam u_int8_t acm; 550138568Ssam} paramType; 551138568Ssam 552138568Ssamstatic const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = { 553138568Ssam { 3, 4, 6 }, /* IEEE80211_MODE_AUTO */ 554138568Ssam { 3, 4, 6 }, /* IEEE80211_MODE_11A */ 555138568Ssam { 3, 5, 7 }, /* IEEE80211_MODE_11B */ 556138568Ssam { 3, 4, 6 }, /* IEEE80211_MODE_11G */ 557138568Ssam { 3, 5, 7 }, /* IEEE80211_MODE_FH */ 558138568Ssam { 2, 3, 5 }, /* IEEE80211_MODE_TURBO_A */ 559138568Ssam { 2, 3, 5 }, /* IEEE80211_MODE_TURBO_G */ 560138568Ssam}; 561138568Ssamstatic const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = { 562138568Ssam { 7, 4, 10 }, /* IEEE80211_MODE_AUTO */ 563138568Ssam { 7, 4, 10 }, /* IEEE80211_MODE_11A */ 564138568Ssam { 7, 5, 10 }, /* IEEE80211_MODE_11B */ 565138568Ssam { 7, 4, 10 }, /* IEEE80211_MODE_11G */ 566138568Ssam { 7, 5, 10 }, /* IEEE80211_MODE_FH */ 567138568Ssam { 7, 3, 10 }, /* IEEE80211_MODE_TURBO_A */ 568138568Ssam { 7, 3, 10 }, /* IEEE80211_MODE_TURBO_G */ 569138568Ssam}; 570138568Ssamstatic const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = { 571138568Ssam { 1, 3, 4, 94 }, /* IEEE80211_MODE_AUTO */ 572138568Ssam { 1, 3, 4, 94 }, /* IEEE80211_MODE_11A */ 573138568Ssam { 1, 4, 5, 188 }, /* IEEE80211_MODE_11B */ 574138568Ssam { 1, 3, 4, 94 }, /* IEEE80211_MODE_11G */ 575138568Ssam { 1, 4, 5, 188 }, /* IEEE80211_MODE_FH */ 576138568Ssam { 1, 2, 3, 94 }, /* IEEE80211_MODE_TURBO_A */ 577138568Ssam { 1, 2, 3, 94 }, /* IEEE80211_MODE_TURBO_G */ 578138568Ssam}; 579138568Ssamstatic const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = { 580138568Ssam { 1, 2, 3, 47 }, /* IEEE80211_MODE_AUTO */ 581138568Ssam { 1, 2, 3, 47 }, /* IEEE80211_MODE_11A */ 582138568Ssam { 1, 3, 4, 102 }, /* IEEE80211_MODE_11B */ 583138568Ssam { 1, 2, 3, 47 }, /* IEEE80211_MODE_11G */ 584138568Ssam { 1, 3, 4, 102 }, /* IEEE80211_MODE_FH */ 585138568Ssam { 1, 2, 2, 47 }, /* IEEE80211_MODE_TURBO_A */ 586138568Ssam { 1, 2, 2, 47 }, /* IEEE80211_MODE_TURBO_G */ 587138568Ssam}; 588138568Ssam 589138568Ssamstatic const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = { 590138568Ssam { 3, 4, 10 }, /* IEEE80211_MODE_AUTO */ 591138568Ssam { 3, 4, 10 }, /* IEEE80211_MODE_11A */ 592138568Ssam { 3, 5, 10 }, /* IEEE80211_MODE_11B */ 593138568Ssam { 3, 4, 10 }, /* IEEE80211_MODE_11G */ 594138568Ssam { 3, 5, 10 }, /* IEEE80211_MODE_FH */ 595138568Ssam { 2, 3, 10 }, /* IEEE80211_MODE_TURBO_A */ 596138568Ssam { 2, 3, 10 }, /* IEEE80211_MODE_TURBO_G */ 597138568Ssam}; 598138568Ssamstatic const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = { 599138568Ssam { 2, 3, 4, 94 }, /* IEEE80211_MODE_AUTO */ 600138568Ssam { 2, 3, 4, 94 }, /* IEEE80211_MODE_11A */ 601138568Ssam { 2, 4, 5, 188 }, /* IEEE80211_MODE_11B */ 602138568Ssam { 2, 3, 4, 94 }, /* IEEE80211_MODE_11G */ 603138568Ssam { 2, 4, 5, 188 }, /* IEEE80211_MODE_FH */ 604138568Ssam { 2, 2, 3, 94 }, /* IEEE80211_MODE_TURBO_A */ 605138568Ssam { 2, 2, 3, 94 }, /* IEEE80211_MODE_TURBO_G */ 606138568Ssam}; 607138568Ssamstatic const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = { 608138568Ssam { 2, 2, 3, 47 }, /* IEEE80211_MODE_AUTO */ 609138568Ssam { 2, 2, 3, 47 }, /* IEEE80211_MODE_11A */ 610138568Ssam { 2, 3, 4, 102 }, /* IEEE80211_MODE_11B */ 611138568Ssam { 2, 2, 3, 47 }, /* IEEE80211_MODE_11G */ 612138568Ssam { 2, 3, 4, 102 }, /* IEEE80211_MODE_FH */ 613138568Ssam { 1, 2, 2, 47 }, /* IEEE80211_MODE_TURBO_A */ 614138568Ssam { 1, 2, 2, 47 }, /* IEEE80211_MODE_TURBO_G */ 615138568Ssam}; 616138568Ssam 617138568Ssamvoid 618138568Ssamieee80211_wme_initparams(struct ieee80211com *ic) 619138568Ssam{ 620138568Ssam struct ieee80211_wme_state *wme = &ic->ic_wme; 621138568Ssam const paramType *pPhyParam, *pBssPhyParam; 622138568Ssam struct wmeParams *wmep; 623138568Ssam int i; 624138568Ssam 625138568Ssam if ((ic->ic_caps & IEEE80211_C_WME) == 0) 626138568Ssam return; 627138568Ssam 628138568Ssam for (i = 0; i < WME_NUM_AC; i++) { 629138568Ssam switch (i) { 630138568Ssam case WME_AC_BK: 631138568Ssam pPhyParam = &phyParamForAC_BK[ic->ic_curmode]; 632138568Ssam pBssPhyParam = &phyParamForAC_BK[ic->ic_curmode]; 633138568Ssam break; 634138568Ssam case WME_AC_VI: 635138568Ssam pPhyParam = &phyParamForAC_VI[ic->ic_curmode]; 636138568Ssam pBssPhyParam = &bssPhyParamForAC_VI[ic->ic_curmode]; 637138568Ssam break; 638138568Ssam case WME_AC_VO: 639138568Ssam pPhyParam = &phyParamForAC_VO[ic->ic_curmode]; 640138568Ssam pBssPhyParam = &bssPhyParamForAC_VO[ic->ic_curmode]; 641138568Ssam break; 642138568Ssam case WME_AC_BE: 643138568Ssam default: 644138568Ssam pPhyParam = &phyParamForAC_BE[ic->ic_curmode]; 645138568Ssam pBssPhyParam = &bssPhyParamForAC_BE[ic->ic_curmode]; 646138568Ssam break; 647138568Ssam } 648138568Ssam 649138568Ssam wmep = &wme->wme_wmeChanParams.cap_wmeParams[i]; 650138568Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 651138568Ssam wmep->wmep_acm = pPhyParam->acm; 652138568Ssam wmep->wmep_aifsn = pPhyParam->aifsn; 653138568Ssam wmep->wmep_logcwmin = pPhyParam->logcwmin; 654138568Ssam wmep->wmep_logcwmax = pPhyParam->logcwmax; 655138568Ssam wmep->wmep_txopLimit = pPhyParam->txopLimit; 656138568Ssam } else { 657138568Ssam wmep->wmep_acm = pBssPhyParam->acm; 658138568Ssam wmep->wmep_aifsn = pBssPhyParam->aifsn; 659138568Ssam wmep->wmep_logcwmin = pBssPhyParam->logcwmin; 660138568Ssam wmep->wmep_logcwmax = pBssPhyParam->logcwmax; 661138568Ssam wmep->wmep_txopLimit = pBssPhyParam->txopLimit; 662138568Ssam 663138568Ssam } 664138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME, 665138568Ssam "%s: %s chan [acm %u aifsn %u log2(cwmin) %u " 666138568Ssam "log2(cwmax) %u txpoLimit %u]\n", __func__ 667138568Ssam , ieee80211_wme_acnames[i] 668138568Ssam , wmep->wmep_acm 669138568Ssam , wmep->wmep_aifsn 670138568Ssam , wmep->wmep_logcwmin 671138568Ssam , wmep->wmep_logcwmax 672138568Ssam , wmep->wmep_txopLimit 673138568Ssam ); 674138568Ssam 675138568Ssam wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i]; 676138568Ssam wmep->wmep_acm = pBssPhyParam->acm; 677138568Ssam wmep->wmep_aifsn = pBssPhyParam->aifsn; 678138568Ssam wmep->wmep_logcwmin = pBssPhyParam->logcwmin; 679138568Ssam wmep->wmep_logcwmax = pBssPhyParam->logcwmax; 680138568Ssam wmep->wmep_txopLimit = pBssPhyParam->txopLimit; 681138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME, 682138568Ssam "%s: %s bss [acm %u aifsn %u log2(cwmin) %u " 683138568Ssam "log2(cwmax) %u txpoLimit %u]\n", __func__ 684138568Ssam , ieee80211_wme_acnames[i] 685138568Ssam , wmep->wmep_acm 686138568Ssam , wmep->wmep_aifsn 687138568Ssam , wmep->wmep_logcwmin 688138568Ssam , wmep->wmep_logcwmax 689138568Ssam , wmep->wmep_txopLimit 690138568Ssam ); 691138568Ssam } 692138568Ssam /* NB: check ic_bss to avoid NULL deref on initial attach */ 693138568Ssam if (ic->ic_bss != NULL) { 694138568Ssam /* 695138568Ssam * Calculate agressive mode switching threshold based 696138568Ssam * on beacon interval. This doesn't need locking since 697138568Ssam * we're only called before entering the RUN state at 698138568Ssam * which point we start sending beacon frames. 699138568Ssam */ 700138568Ssam wme->wme_hipri_switch_thresh = 701138568Ssam (HIGH_PRI_SWITCH_THRESH * ic->ic_bss->ni_intval) / 100; 702138568Ssam ieee80211_wme_updateparams(ic); 703138568Ssam } 704138568Ssam} 705138568Ssam 706138568Ssam/* 707138568Ssam * Update WME parameters for ourself and the BSS. 708138568Ssam */ 709138568Ssamvoid 710138568Ssamieee80211_wme_updateparams_locked(struct ieee80211com *ic) 711138568Ssam{ 712138568Ssam static const paramType phyParam[IEEE80211_MODE_MAX] = { 713138568Ssam { 2, 4, 10, 64 }, /* IEEE80211_MODE_AUTO */ 714138568Ssam { 2, 4, 10, 64 }, /* IEEE80211_MODE_11A */ 715138568Ssam { 2, 5, 10, 64 }, /* IEEE80211_MODE_11B */ 716138568Ssam { 2, 4, 10, 64 }, /* IEEE80211_MODE_11G */ 717138568Ssam { 2, 5, 10, 64 }, /* IEEE80211_MODE_FH */ 718138568Ssam { 1, 3, 10, 64 }, /* IEEE80211_MODE_TURBO_A */ 719138568Ssam { 1, 3, 10, 64 }, /* IEEE80211_MODE_TURBO_G */ 720138568Ssam }; 721138568Ssam struct ieee80211_wme_state *wme = &ic->ic_wme; 722138568Ssam const struct wmeParams *wmep; 723138568Ssam struct wmeParams *chanp, *bssp; 724138568Ssam int i; 725138568Ssam 726138568Ssam /* set up the channel access parameters for the physical device */ 727138568Ssam for (i = 0; i < WME_NUM_AC; i++) { 728138568Ssam chanp = &wme->wme_chanParams.cap_wmeParams[i]; 729138568Ssam wmep = &wme->wme_wmeChanParams.cap_wmeParams[i]; 730138568Ssam chanp->wmep_aifsn = wmep->wmep_aifsn; 731138568Ssam chanp->wmep_logcwmin = wmep->wmep_logcwmin; 732138568Ssam chanp->wmep_logcwmax = wmep->wmep_logcwmax; 733138568Ssam chanp->wmep_txopLimit = wmep->wmep_txopLimit; 734138568Ssam 735138568Ssam chanp = &wme->wme_bssChanParams.cap_wmeParams[i]; 736138568Ssam wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i]; 737138568Ssam chanp->wmep_aifsn = wmep->wmep_aifsn; 738138568Ssam chanp->wmep_logcwmin = wmep->wmep_logcwmin; 739138568Ssam chanp->wmep_logcwmax = wmep->wmep_logcwmax; 740138568Ssam chanp->wmep_txopLimit = wmep->wmep_txopLimit; 741138568Ssam } 742138568Ssam 743138568Ssam /* 744138568Ssam * This implements agressive mode as found in certain 745138568Ssam * vendors' AP's. When there is significant high 746138568Ssam * priority (VI/VO) traffic in the BSS throttle back BE 747138568Ssam * traffic by using conservative parameters. Otherwise 748138568Ssam * BE uses agressive params to optimize performance of 749138568Ssam * legacy/non-QoS traffic. 750138568Ssam */ 751138568Ssam if ((ic->ic_opmode == IEEE80211_M_HOSTAP && 752156524Ssam (wme->wme_flags & WME_F_AGGRMODE) != 0) || 753153974Ssam (ic->ic_opmode == IEEE80211_M_STA && 754138568Ssam (ic->ic_bss->ni_flags & IEEE80211_NODE_QOS) == 0) || 755138568Ssam (ic->ic_flags & IEEE80211_F_WME) == 0) { 756138568Ssam chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE]; 757138568Ssam bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE]; 758138568Ssam 759138568Ssam chanp->wmep_aifsn = bssp->wmep_aifsn = 760138568Ssam phyParam[ic->ic_curmode].aifsn; 761138568Ssam chanp->wmep_logcwmin = bssp->wmep_logcwmin = 762138568Ssam phyParam[ic->ic_curmode].logcwmin; 763138568Ssam chanp->wmep_logcwmax = bssp->wmep_logcwmax = 764138568Ssam phyParam[ic->ic_curmode].logcwmax; 765138568Ssam chanp->wmep_txopLimit = bssp->wmep_txopLimit = 766153421Ssam (ic->ic_flags & IEEE80211_F_BURST) ? 767138568Ssam phyParam[ic->ic_curmode].txopLimit : 0; 768138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME, 769138568Ssam "%s: %s [acm %u aifsn %u log2(cwmin) %u " 770138568Ssam "log2(cwmax) %u txpoLimit %u]\n", __func__ 771138568Ssam , ieee80211_wme_acnames[WME_AC_BE] 772138568Ssam , chanp->wmep_acm 773138568Ssam , chanp->wmep_aifsn 774138568Ssam , chanp->wmep_logcwmin 775138568Ssam , chanp->wmep_logcwmax 776138568Ssam , chanp->wmep_txopLimit 777138568Ssam ); 778138568Ssam } 779138568Ssam 780138568Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP && 781156524Ssam ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) { 782138568Ssam static const u_int8_t logCwMin[IEEE80211_MODE_MAX] = { 783138568Ssam 3, /* IEEE80211_MODE_AUTO */ 784138568Ssam 3, /* IEEE80211_MODE_11A */ 785138568Ssam 4, /* IEEE80211_MODE_11B */ 786138568Ssam 3, /* IEEE80211_MODE_11G */ 787138568Ssam 4, /* IEEE80211_MODE_FH */ 788138568Ssam 3, /* IEEE80211_MODE_TURBO_A */ 789138568Ssam 3, /* IEEE80211_MODE_TURBO_G */ 790138568Ssam }; 791138568Ssam chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE]; 792138568Ssam bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE]; 793138568Ssam 794138568Ssam chanp->wmep_logcwmin = bssp->wmep_logcwmin = 795138568Ssam logCwMin[ic->ic_curmode]; 796138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME, 797138568Ssam "%s: %s log2(cwmin) %u\n", __func__ 798138568Ssam , ieee80211_wme_acnames[WME_AC_BE] 799138568Ssam , chanp->wmep_logcwmin 800138568Ssam ); 801138568Ssam } 802138568Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP) { /* XXX ibss? */ 803138568Ssam /* 804138568Ssam * Arrange for a beacon update and bump the parameter 805138568Ssam * set number so associated stations load the new values. 806138568Ssam */ 807138568Ssam wme->wme_bssChanParams.cap_info = 808138568Ssam (wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT; 809138568Ssam ic->ic_flags |= IEEE80211_F_WMEUPDATE; 810138568Ssam } 811138568Ssam 812138568Ssam wme->wme_update(ic); 813138568Ssam 814138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME, 815138568Ssam "%s: WME params updated, cap_info 0x%x\n", __func__, 816138568Ssam ic->ic_opmode == IEEE80211_M_STA ? 817138568Ssam wme->wme_wmeChanParams.cap_info : 818138568Ssam wme->wme_bssChanParams.cap_info); 819138568Ssam} 820138568Ssam 821138568Ssamvoid 822138568Ssamieee80211_wme_updateparams(struct ieee80211com *ic) 823138568Ssam{ 824138568Ssam 825138568Ssam if (ic->ic_caps & IEEE80211_C_WME) { 826138568Ssam IEEE80211_BEACON_LOCK(ic); 827138568Ssam ieee80211_wme_updateparams_locked(ic); 828138568Ssam IEEE80211_BEACON_UNLOCK(ic); 829138568Ssam } 830138568Ssam} 831138568Ssam 832153349Ssamvoid 833153349Ssamieee80211_beacon_miss(struct ieee80211com *ic) 834153349Ssam{ 835153349Ssam 836153349Ssam if (ic->ic_flags & IEEE80211_F_SCAN) { 837153349Ssam /* XXX check ic_curchan != ic_bsschan? */ 838153349Ssam return; 839153349Ssam } 840153349Ssam IEEE80211_DPRINTF(ic, 841153349Ssam IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG, 842153349Ssam "%s\n", "beacon miss"); 843153349Ssam 844153349Ssam /* 845153349Ssam * Our handling is only meaningful for stations that are 846153349Ssam * associated; any other conditions else will be handled 847153349Ssam * through different means (e.g. the tx timeout on mgt frames). 848153349Ssam */ 849153349Ssam if (ic->ic_opmode != IEEE80211_M_STA || ic->ic_state != IEEE80211_S_RUN) 850153349Ssam return; 851153349Ssam 852153349Ssam if (++ic->ic_bmiss_count < ic->ic_bmiss_max) { 853153349Ssam /* 854153349Ssam * Send a directed probe req before falling back to a scan; 855153349Ssam * if we receive a response ic_bmiss_count will be reset. 856153349Ssam * Some cards mistakenly report beacon miss so this avoids 857153349Ssam * the expensive scan if the ap is still there. 858153349Ssam */ 859153349Ssam ieee80211_send_probereq(ic->ic_bss, ic->ic_myaddr, 860153349Ssam ic->ic_bss->ni_bssid, ic->ic_bss->ni_bssid, 861153349Ssam ic->ic_bss->ni_essid, ic->ic_bss->ni_esslen, 862153349Ssam ic->ic_opt_ie, ic->ic_opt_ie_len); 863153349Ssam return; 864153349Ssam } 865153349Ssam ic->ic_bmiss_count = 0; 866153349Ssam ieee80211_new_state(ic, IEEE80211_S_SCAN, 0); 867153349Ssam} 868153349Ssam 869154736Ssam/* 870154736Ssam * Software beacon miss handling. Check if any beacons 871154736Ssam * were received in the last period. If not post a 872154736Ssam * beacon miss; otherwise reset the counter. 873154736Ssam */ 874147765Ssamstatic void 875154736Ssamieee80211_swbmiss(void *arg) 876154736Ssam{ 877154736Ssam struct ieee80211com *ic = arg; 878154736Ssam 879154736Ssam if (ic->ic_swbmiss_count == 0) { 880154736Ssam ieee80211_beacon_miss(ic); 881154736Ssam if (ic->ic_bmiss_count == 0) /* don't re-arm timer */ 882154736Ssam return; 883154736Ssam } else 884154736Ssam ic->ic_swbmiss_count = 0; 885154736Ssam callout_reset(&ic->ic_swbmiss, ic->ic_swbmiss_period, 886154736Ssam ieee80211_swbmiss, ic); 887154736Ssam} 888154736Ssam 889154736Ssamstatic void 890147765Ssamsta_disassoc(void *arg, struct ieee80211_node *ni) 891147765Ssam{ 892147765Ssam struct ieee80211com *ic = arg; 893147765Ssam 894147765Ssam if (ni->ni_associd != 0) { 895147765Ssam IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DISASSOC, 896147765Ssam IEEE80211_REASON_ASSOC_LEAVE); 897147765Ssam ieee80211_node_leave(ic, ni); 898147765Ssam } 899147765Ssam} 900147765Ssam 901147765Ssamstatic void 902147765Ssamsta_deauth(void *arg, struct ieee80211_node *ni) 903147765Ssam{ 904147765Ssam struct ieee80211com *ic = arg; 905147765Ssam 906147765Ssam IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH, 907147765Ssam IEEE80211_REASON_ASSOC_LEAVE); 908147765Ssam} 909147765Ssam 910117811Ssamstatic int 911138568Ssamieee80211_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 912116742Ssam{ 913138568Ssam struct ifnet *ifp = ic->ic_ifp; 914116742Ssam struct ieee80211_node *ni; 915117811Ssam enum ieee80211_state ostate; 916116742Ssam 917116742Ssam ostate = ic->ic_state; 918138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_STATE, "%s: %s -> %s\n", __func__, 919138568Ssam ieee80211_state_name[ostate], ieee80211_state_name[nstate]); 920117811Ssam ic->ic_state = nstate; /* state transition */ 921116742Ssam ni = ic->ic_bss; /* NB: no reference held */ 922154736Ssam if (ic->ic_flags_ext & IEEE80211_FEXT_SWBMISS) 923154736Ssam callout_stop(&ic->ic_swbmiss); 924116742Ssam switch (nstate) { 925116742Ssam case IEEE80211_S_INIT: 926116742Ssam switch (ostate) { 927116742Ssam case IEEE80211_S_INIT: 928116742Ssam break; 929116742Ssam case IEEE80211_S_RUN: 930116742Ssam switch (ic->ic_opmode) { 931116742Ssam case IEEE80211_M_STA: 932116742Ssam IEEE80211_SEND_MGMT(ic, ni, 933116742Ssam IEEE80211_FC0_SUBTYPE_DISASSOC, 934116742Ssam IEEE80211_REASON_ASSOC_LEAVE); 935138568Ssam ieee80211_sta_leave(ic, ni); 936116742Ssam break; 937116742Ssam case IEEE80211_M_HOSTAP: 938147765Ssam ieee80211_iterate_nodes(&ic->ic_sta, 939147765Ssam sta_disassoc, ic); 940116742Ssam break; 941116742Ssam default: 942116742Ssam break; 943116742Ssam } 944165894Ssam break; 945116742Ssam case IEEE80211_S_ASSOC: 946116742Ssam switch (ic->ic_opmode) { 947116742Ssam case IEEE80211_M_STA: 948116742Ssam IEEE80211_SEND_MGMT(ic, ni, 949116742Ssam IEEE80211_FC0_SUBTYPE_DEAUTH, 950116742Ssam IEEE80211_REASON_AUTH_LEAVE); 951116742Ssam break; 952116742Ssam case IEEE80211_M_HOSTAP: 953147765Ssam ieee80211_iterate_nodes(&ic->ic_sta, 954147765Ssam sta_deauth, ic); 955116742Ssam break; 956116742Ssam default: 957116742Ssam break; 958116742Ssam } 959165894Ssam break; 960140441Ssam case IEEE80211_S_SCAN: 961140441Ssam ieee80211_cancel_scan(ic); 962165894Ssam break; 963116742Ssam case IEEE80211_S_AUTH: 964165894Ssam break; 965165894Ssam } 966165894Ssam if (ostate != IEEE80211_S_INIT) { 967165894Ssam /* NB: optimize INIT -> INIT case */ 968116742Ssam ic->ic_mgt_timer = 0; 969165894Ssam ieee80211_drain_ifq(&ic->ic_mgtq); 970138568Ssam ieee80211_reset_bss(ic); 971116742Ssam } 972138568Ssam if (ic->ic_auth->ia_detach != NULL) 973138568Ssam ic->ic_auth->ia_detach(ic); 974116742Ssam break; 975116742Ssam case IEEE80211_S_SCAN: 976116742Ssam switch (ostate) { 977116742Ssam case IEEE80211_S_INIT: 978138568Ssam if ((ic->ic_opmode == IEEE80211_M_HOSTAP || 979138568Ssam ic->ic_opmode == IEEE80211_M_IBSS || 980138568Ssam ic->ic_opmode == IEEE80211_M_AHDEMO) && 981116742Ssam ic->ic_des_chan != IEEE80211_CHAN_ANYC) { 982116742Ssam /* 983116742Ssam * AP operation and we already have a channel; 984116742Ssam * bypass the scan and startup immediately. 985116742Ssam */ 986116742Ssam ieee80211_create_ibss(ic, ic->ic_des_chan); 987116742Ssam } else { 988138568Ssam ieee80211_begin_scan(ic, arg); 989116742Ssam } 990116742Ssam break; 991116742Ssam case IEEE80211_S_SCAN: 992138568Ssam /* 993156358Ssam * Scan next. If doing an active scan probe 994156358Ssam * for the requested ap (if any). 995138568Ssam */ 996156358Ssam if (ic->ic_flags & IEEE80211_F_ASCAN) 997156358Ssam ieee80211_probe_curchan(ic, 0); 998116742Ssam break; 999116742Ssam case IEEE80211_S_RUN: 1000116742Ssam /* beacon miss */ 1001138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_STATE, 1002138568Ssam "no recent beacons from %s; rescanning\n", 1003138568Ssam ether_sprintf(ic->ic_bss->ni_bssid)); 1004138568Ssam ieee80211_sta_leave(ic, ni); 1005138568Ssam ic->ic_flags &= ~IEEE80211_F_SIBSS; /* XXX */ 1006116742Ssam /* FALLTHRU */ 1007116742Ssam case IEEE80211_S_AUTH: 1008116742Ssam case IEEE80211_S_ASSOC: 1009116742Ssam /* timeout restart scan */ 1010138568Ssam ni = ieee80211_find_node(&ic->ic_scan, 1011138568Ssam ic->ic_bss->ni_macaddr); 1012116742Ssam if (ni != NULL) { 1013116742Ssam ni->ni_fails++; 1014116742Ssam ieee80211_unref_node(&ni); 1015116742Ssam } 1016147116Ssam if (ic->ic_roaming == IEEE80211_ROAMING_AUTO) 1017147116Ssam ieee80211_begin_scan(ic, arg); 1018116742Ssam break; 1019116742Ssam } 1020116742Ssam break; 1021116742Ssam case IEEE80211_S_AUTH: 1022116742Ssam switch (ostate) { 1023116742Ssam case IEEE80211_S_INIT: 1024116742Ssam case IEEE80211_S_SCAN: 1025116742Ssam IEEE80211_SEND_MGMT(ic, ni, 1026116742Ssam IEEE80211_FC0_SUBTYPE_AUTH, 1); 1027116742Ssam break; 1028116742Ssam case IEEE80211_S_AUTH: 1029116742Ssam case IEEE80211_S_ASSOC: 1030138568Ssam switch (arg) { 1031116742Ssam case IEEE80211_FC0_SUBTYPE_AUTH: 1032116742Ssam /* ??? */ 1033116742Ssam IEEE80211_SEND_MGMT(ic, ni, 1034116742Ssam IEEE80211_FC0_SUBTYPE_AUTH, 2); 1035116742Ssam break; 1036116742Ssam case IEEE80211_FC0_SUBTYPE_DEAUTH: 1037116742Ssam /* ignore and retry scan on timeout */ 1038116742Ssam break; 1039116742Ssam } 1040116742Ssam break; 1041116742Ssam case IEEE80211_S_RUN: 1042138568Ssam switch (arg) { 1043116742Ssam case IEEE80211_FC0_SUBTYPE_AUTH: 1044116742Ssam IEEE80211_SEND_MGMT(ic, ni, 1045116742Ssam IEEE80211_FC0_SUBTYPE_AUTH, 2); 1046116742Ssam ic->ic_state = ostate; /* stay RUN */ 1047116742Ssam break; 1048116742Ssam case IEEE80211_FC0_SUBTYPE_DEAUTH: 1049138568Ssam ieee80211_sta_leave(ic, ni); 1050147116Ssam if (ic->ic_roaming == IEEE80211_ROAMING_AUTO) { 1051147116Ssam /* try to reauth */ 1052147116Ssam IEEE80211_SEND_MGMT(ic, ni, 1053147116Ssam IEEE80211_FC0_SUBTYPE_AUTH, 1); 1054147116Ssam } 1055116742Ssam break; 1056116742Ssam } 1057116742Ssam break; 1058116742Ssam } 1059116742Ssam break; 1060116742Ssam case IEEE80211_S_ASSOC: 1061116742Ssam switch (ostate) { 1062116742Ssam case IEEE80211_S_INIT: 1063116742Ssam case IEEE80211_S_SCAN: 1064116742Ssam case IEEE80211_S_ASSOC: 1065138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 1066138568Ssam "%s: invalid transition\n", __func__); 1067116742Ssam break; 1068116742Ssam case IEEE80211_S_AUTH: 1069116742Ssam IEEE80211_SEND_MGMT(ic, ni, 1070116742Ssam IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0); 1071116742Ssam break; 1072116742Ssam case IEEE80211_S_RUN: 1073138568Ssam ieee80211_sta_leave(ic, ni); 1074147116Ssam if (ic->ic_roaming == IEEE80211_ROAMING_AUTO) { 1075147116Ssam IEEE80211_SEND_MGMT(ic, ni, 1076147116Ssam IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 1); 1077147116Ssam } 1078116742Ssam break; 1079116742Ssam } 1080116742Ssam break; 1081116742Ssam case IEEE80211_S_RUN: 1082138568Ssam if (ic->ic_flags & IEEE80211_F_WPA) { 1083138568Ssam /* XXX validate prerequisites */ 1084138568Ssam } 1085116742Ssam switch (ostate) { 1086116742Ssam case IEEE80211_S_INIT: 1087138568Ssam if (ic->ic_opmode == IEEE80211_M_MONITOR) 1088138568Ssam break; 1089138568Ssam /* fall thru... */ 1090116742Ssam case IEEE80211_S_AUTH: 1091138568Ssam IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 1092138568Ssam "%s: invalid transition\n", __func__); 1093140763Ssam /* fall thru... */ 1094140763Ssam case IEEE80211_S_RUN: 1095116742Ssam break; 1096116742Ssam case IEEE80211_S_SCAN: /* adhoc/hostap mode */ 1097116742Ssam case IEEE80211_S_ASSOC: /* infra mode */ 1098116742Ssam KASSERT(ni->ni_txrate < ni->ni_rates.rs_nrates, 1099116742Ssam ("%s: bogus xmit rate %u setup\n", __func__, 1100116742Ssam ni->ni_txrate)); 1101138568Ssam#ifdef IEEE80211_DEBUG 1102138568Ssam if (ieee80211_msg_debug(ic)) { 1103116742Ssam if (ic->ic_opmode == IEEE80211_M_STA) 1104138568Ssam if_printf(ifp, "associated "); 1105116742Ssam else 1106138568Ssam if_printf(ifp, "synchronized "); 1107116742Ssam printf("with %s ssid ", 1108116742Ssam ether_sprintf(ni->ni_bssid)); 1109116742Ssam ieee80211_print_essid(ic->ic_bss->ni_essid, 1110116742Ssam ni->ni_esslen); 1111116742Ssam printf(" channel %d start %uMb\n", 1112148936Ssam ieee80211_chan2ieee(ic, ic->ic_curchan), 1113116742Ssam IEEE80211_RATE2MBS(ni->ni_rates.rs_rates[ni->ni_txrate])); 1114116742Ssam } 1115138568Ssam#endif 1116116742Ssam ic->ic_mgt_timer = 0; 1117138568Ssam if (ic->ic_opmode == IEEE80211_M_STA) 1118138568Ssam ieee80211_notify_node_join(ic, ni, 1119138568Ssam arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP); 1120138568Ssam if_start(ifp); /* XXX not authorized yet */ 1121116742Ssam break; 1122116742Ssam } 1123154736Ssam if (ostate != IEEE80211_S_RUN && 1124154736Ssam ic->ic_opmode == IEEE80211_M_STA && 1125154736Ssam (ic->ic_flags_ext & IEEE80211_FEXT_SWBMISS)) { 1126154736Ssam /* 1127154736Ssam * Start s/w beacon miss timer for devices w/o 1128154736Ssam * hardware support. We fudge a bit here since 1129154736Ssam * we're doing this in software. 1130154736Ssam */ 1131154736Ssam ic->ic_swbmiss_period = IEEE80211_TU_TO_TICKS( 1132154736Ssam 2 * ic->ic_bmissthreshold * ni->ni_intval); 1133154736Ssam ic->ic_swbmiss_count = 0; 1134154736Ssam callout_reset(&ic->ic_swbmiss, ic->ic_swbmiss_period, 1135154736Ssam ieee80211_swbmiss, ic); 1136154736Ssam } 1137138568Ssam /* 1138138568Ssam * Start/stop the authenticator when operating as an 1139138568Ssam * AP. We delay until here to allow configuration to 1140138568Ssam * happen out of order. 1141138568Ssam */ 1142138568Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP && /* XXX IBSS/AHDEMO */ 1143138568Ssam ic->ic_auth->ia_attach != NULL) { 1144138568Ssam /* XXX check failure */ 1145138568Ssam ic->ic_auth->ia_attach(ic); 1146138568Ssam } else if (ic->ic_auth->ia_detach != NULL) { 1147138568Ssam ic->ic_auth->ia_detach(ic); 1148138568Ssam } 1149138568Ssam /* 1150138568Ssam * When 802.1x is not in use mark the port authorized 1151138568Ssam * at this point so traffic can flow. 1152138568Ssam */ 1153138568Ssam if (ni->ni_authmode != IEEE80211_AUTH_8021X) 1154148302Ssam ieee80211_node_authorize(ni); 1155138568Ssam /* 1156138568Ssam * Enable inactivity processing. 1157138568Ssam * XXX 1158138568Ssam */ 1159138568Ssam ic->ic_scan.nt_inact_timer = IEEE80211_INACT_WAIT; 1160140753Ssam ic->ic_sta.nt_inact_timer = IEEE80211_INACT_WAIT; 1161116742Ssam break; 1162116742Ssam } 1163116742Ssam return 0; 1164116742Ssam} 1165