ieee80211_sta.c revision 192468
1178354Ssam/*- 2178354Ssam * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting 3178354Ssam * All rights reserved. 4178354Ssam * 5178354Ssam * Redistribution and use in source and binary forms, with or without 6178354Ssam * modification, are permitted provided that the following conditions 7178354Ssam * are met: 8178354Ssam * 1. Redistributions of source code must retain the above copyright 9178354Ssam * notice, this list of conditions and the following disclaimer. 10178354Ssam * 2. Redistributions in binary form must reproduce the above copyright 11178354Ssam * notice, this list of conditions and the following disclaimer in the 12178354Ssam * documentation and/or other materials provided with the distribution. 13178354Ssam * 14178354Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15178354Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16178354Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17178354Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18178354Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19178354Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20178354Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21178354Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22178354Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23178354Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24178354Ssam */ 25178354Ssam 26178354Ssam#include <sys/cdefs.h> 27178354Ssam#ifdef __FreeBSD__ 28178354Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_sta.c 192468 2009-05-20 20:00:40Z sam $"); 29178354Ssam#endif 30178354Ssam 31178354Ssam/* 32178354Ssam * IEEE 802.11 Station mode support. 33178354Ssam */ 34178354Ssam#include "opt_inet.h" 35178354Ssam#include "opt_wlan.h" 36178354Ssam 37178354Ssam#include <sys/param.h> 38178354Ssam#include <sys/systm.h> 39178354Ssam#include <sys/mbuf.h> 40178354Ssam#include <sys/malloc.h> 41178354Ssam#include <sys/kernel.h> 42178354Ssam 43178354Ssam#include <sys/socket.h> 44178354Ssam#include <sys/sockio.h> 45178354Ssam#include <sys/endian.h> 46178354Ssam#include <sys/errno.h> 47178354Ssam#include <sys/proc.h> 48178354Ssam#include <sys/sysctl.h> 49178354Ssam 50178354Ssam#include <net/if.h> 51178354Ssam#include <net/if_media.h> 52178354Ssam#include <net/if_llc.h> 53178354Ssam#include <net/ethernet.h> 54178354Ssam 55178354Ssam#include <net/bpf.h> 56178354Ssam 57178354Ssam#include <net80211/ieee80211_var.h> 58178354Ssam#include <net80211/ieee80211_sta.h> 59178354Ssam#include <net80211/ieee80211_input.h> 60190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG 61190391Ssam#include <net80211/ieee80211_superg.h> 62190391Ssam#endif 63178354Ssam 64178354Ssam#define IEEE80211_RATE2MBS(r) (((r) & IEEE80211_RATE_VAL) / 2) 65178354Ssam 66178354Ssamstatic void sta_vattach(struct ieee80211vap *); 67178354Ssamstatic void sta_beacon_miss(struct ieee80211vap *); 68178354Ssamstatic int sta_newstate(struct ieee80211vap *, enum ieee80211_state, int); 69192468Ssamstatic int sta_input(struct ieee80211_node *, struct mbuf *, int, int); 70178354Ssamstatic void sta_recv_mgmt(struct ieee80211_node *, struct mbuf *, 71192468Ssam int subtype, int rssi, int nf); 72191546Ssamstatic void sta_recv_ctl(struct ieee80211_node *, struct mbuf *, int subtype); 73178354Ssam 74178354Ssamvoid 75178354Ssamieee80211_sta_attach(struct ieee80211com *ic) 76178354Ssam{ 77178354Ssam ic->ic_vattach[IEEE80211_M_STA] = sta_vattach; 78178354Ssam} 79178354Ssam 80178354Ssamvoid 81178354Ssamieee80211_sta_detach(struct ieee80211com *ic) 82178354Ssam{ 83178354Ssam} 84178354Ssam 85178354Ssamstatic void 86178354Ssamsta_vdetach(struct ieee80211vap *vap) 87178354Ssam{ 88178354Ssam} 89178354Ssam 90178354Ssamstatic void 91178354Ssamsta_vattach(struct ieee80211vap *vap) 92178354Ssam{ 93178354Ssam vap->iv_newstate = sta_newstate; 94178354Ssam vap->iv_input = sta_input; 95178354Ssam vap->iv_recv_mgmt = sta_recv_mgmt; 96191546Ssam vap->iv_recv_ctl = sta_recv_ctl; 97178354Ssam vap->iv_opdetach = sta_vdetach; 98178354Ssam vap->iv_bmiss = sta_beacon_miss; 99178354Ssam} 100178354Ssam 101178354Ssam/* 102178354Ssam * Handle a beacon miss event. The common code filters out 103178354Ssam * spurious events that can happen when scanning and/or before 104178354Ssam * reaching RUN state. 105178354Ssam */ 106178354Ssamstatic void 107178354Ssamsta_beacon_miss(struct ieee80211vap *vap) 108178354Ssam{ 109190402Ssam KASSERT((vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0, ("scanning")); 110178354Ssam KASSERT(vap->iv_state == IEEE80211_S_RUN, 111178354Ssam ("wrong state %d", vap->iv_state)); 112178354Ssam 113178354Ssam IEEE80211_DPRINTF(vap, 114178354Ssam IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG, 115178354Ssam "beacon miss, mode %u state %s\n", 116178354Ssam vap->iv_opmode, ieee80211_state_name[vap->iv_state]); 117178354Ssam 118178354Ssam if (++vap->iv_bmiss_count < vap->iv_bmiss_max) { 119178354Ssam /* 120178354Ssam * Send a directed probe req before falling back to a 121178354Ssam * scan; if we receive a response ic_bmiss_count will 122178354Ssam * be reset. Some cards mistakenly report beacon miss 123178354Ssam * so this avoids the expensive scan if the ap is 124178354Ssam * still there. 125178354Ssam */ 126178354Ssam ieee80211_send_probereq(vap->iv_bss, vap->iv_myaddr, 127178354Ssam vap->iv_bss->ni_bssid, vap->iv_bss->ni_bssid, 128178354Ssam vap->iv_bss->ni_essid, vap->iv_bss->ni_esslen); 129178354Ssam return; 130178354Ssam } 131178354Ssam vap->iv_bmiss_count = 0; 132178354Ssam vap->iv_stats.is_beacon_miss++; 133178354Ssam if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) { 134190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG 135190402Ssam struct ieee80211com *ic = vap->iv_ic; 136190402Ssam 137178354Ssam /* 138178354Ssam * If we receive a beacon miss interrupt when using 139178354Ssam * dynamic turbo, attempt to switch modes before 140178354Ssam * reassociating. 141178354Ssam */ 142178354Ssam if (IEEE80211_ATH_CAP(vap, vap->iv_bss, IEEE80211_NODE_TURBOP)) 143178354Ssam ieee80211_dturbo_switch(vap, 144178354Ssam ic->ic_bsschan->ic_flags ^ IEEE80211_CHAN_TURBO); 145190391Ssam#endif 146178354Ssam /* 147178354Ssam * Try to reassociate before scanning for a new ap. 148178354Ssam */ 149178354Ssam ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1); 150178354Ssam } else { 151178354Ssam /* 152178354Ssam * Somebody else is controlling state changes (e.g. 153178354Ssam * a user-mode app) don't do anything that would 154178354Ssam * confuse them; just drop into scan mode so they'll 155178354Ssam * notified of the state change and given control. 156178354Ssam */ 157178354Ssam ieee80211_new_state(vap, IEEE80211_S_SCAN, 0); 158178354Ssam } 159178354Ssam} 160178354Ssam 161178354Ssam/* 162178354Ssam * Handle deauth with reason. We retry only for 163178354Ssam * the cases where we might succeed. Otherwise 164178354Ssam * we downgrade the ap and scan. 165178354Ssam */ 166178354Ssamstatic void 167178354Ssamsta_authretry(struct ieee80211vap *vap, struct ieee80211_node *ni, int reason) 168178354Ssam{ 169178354Ssam switch (reason) { 170178354Ssam case IEEE80211_STATUS_SUCCESS: /* NB: MLME assoc */ 171178354Ssam case IEEE80211_STATUS_TIMEOUT: 172178354Ssam case IEEE80211_REASON_ASSOC_EXPIRE: 173178354Ssam case IEEE80211_REASON_NOT_AUTHED: 174178354Ssam case IEEE80211_REASON_NOT_ASSOCED: 175178354Ssam case IEEE80211_REASON_ASSOC_LEAVE: 176178354Ssam case IEEE80211_REASON_ASSOC_NOT_AUTHED: 177178354Ssam IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, 1); 178178354Ssam break; 179178354Ssam default: 180178354Ssam ieee80211_scan_assoc_fail(vap, vap->iv_bss->ni_macaddr, reason); 181178354Ssam if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) 182178354Ssam ieee80211_check_scan_current(vap); 183178354Ssam break; 184178354Ssam } 185178354Ssam} 186178354Ssam 187178354Ssam/* 188178354Ssam * IEEE80211_M_STA vap state machine handler. 189178354Ssam * This routine handles the main states in the 802.11 protocol. 190178354Ssam */ 191178354Ssamstatic int 192178354Ssamsta_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 193178354Ssam{ 194178354Ssam struct ieee80211com *ic = vap->iv_ic; 195178354Ssam struct ieee80211_node *ni; 196178354Ssam enum ieee80211_state ostate; 197178354Ssam 198178354Ssam IEEE80211_LOCK_ASSERT(ic); 199178354Ssam 200178354Ssam ostate = vap->iv_state; 201178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n", 202178354Ssam __func__, ieee80211_state_name[ostate], 203178354Ssam ieee80211_state_name[nstate], arg); 204178354Ssam vap->iv_state = nstate; /* state transition */ 205178354Ssam callout_stop(&vap->iv_mgtsend); /* XXX callout_drain */ 206178354Ssam if (ostate != IEEE80211_S_SCAN) 207178354Ssam ieee80211_cancel_scan(vap); /* background scan */ 208178354Ssam ni = vap->iv_bss; /* NB: no reference held */ 209178354Ssam if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) 210178354Ssam callout_stop(&vap->iv_swbmiss); 211178354Ssam switch (nstate) { 212178354Ssam case IEEE80211_S_INIT: 213178354Ssam switch (ostate) { 214178354Ssam case IEEE80211_S_SLEEP: 215178354Ssam /* XXX wakeup */ 216178354Ssam case IEEE80211_S_RUN: 217178354Ssam IEEE80211_SEND_MGMT(ni, 218178354Ssam IEEE80211_FC0_SUBTYPE_DISASSOC, 219178354Ssam IEEE80211_REASON_ASSOC_LEAVE); 220178354Ssam ieee80211_sta_leave(ni); 221178354Ssam break; 222178354Ssam case IEEE80211_S_ASSOC: 223178354Ssam IEEE80211_SEND_MGMT(ni, 224178354Ssam IEEE80211_FC0_SUBTYPE_DEAUTH, 225178354Ssam IEEE80211_REASON_AUTH_LEAVE); 226178354Ssam break; 227178354Ssam case IEEE80211_S_SCAN: 228178354Ssam ieee80211_cancel_scan(vap); 229178354Ssam break; 230178354Ssam default: 231178354Ssam goto invalid; 232178354Ssam } 233178354Ssam if (ostate != IEEE80211_S_INIT) { 234178354Ssam /* NB: optimize INIT -> INIT case */ 235178354Ssam ieee80211_reset_bss(vap); 236178354Ssam } 237178354Ssam if (vap->iv_auth->ia_detach != NULL) 238178354Ssam vap->iv_auth->ia_detach(vap); 239178354Ssam break; 240178354Ssam case IEEE80211_S_SCAN: 241178354Ssam switch (ostate) { 242178354Ssam case IEEE80211_S_INIT: 243178354Ssam /* 244178354Ssam * Initiate a scan. We can come here as a result 245178354Ssam * of an IEEE80211_IOC_SCAN_REQ too in which case 246178354Ssam * the vap will be marked with IEEE80211_FEXT_SCANREQ 247178354Ssam * and the scan request parameters will be present 248178354Ssam * in iv_scanreq. Otherwise we do the default. 249178354Ssam */ 250178354Ssam if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) { 251178354Ssam ieee80211_check_scan(vap, 252178354Ssam vap->iv_scanreq_flags, 253178354Ssam vap->iv_scanreq_duration, 254178354Ssam vap->iv_scanreq_mindwell, 255178354Ssam vap->iv_scanreq_maxdwell, 256178354Ssam vap->iv_scanreq_nssid, vap->iv_scanreq_ssid); 257178354Ssam vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ; 258178354Ssam } else 259178354Ssam ieee80211_check_scan_current(vap); 260178354Ssam break; 261178354Ssam case IEEE80211_S_SCAN: 262178354Ssam case IEEE80211_S_AUTH: 263178354Ssam case IEEE80211_S_ASSOC: 264178354Ssam /* 265178354Ssam * These can happen either because of a timeout 266178354Ssam * on an assoc/auth response or because of a 267178354Ssam * change in state that requires a reset. For 268178354Ssam * the former we're called with a non-zero arg 269178354Ssam * that is the cause for the failure; pass this 270178354Ssam * to the scan code so it can update state. 271178354Ssam * Otherwise trigger a new scan unless we're in 272178354Ssam * manual roaming mode in which case an application 273178354Ssam * must issue an explicit scan request. 274178354Ssam */ 275178354Ssam if (arg != 0) 276178354Ssam ieee80211_scan_assoc_fail(vap, 277178354Ssam vap->iv_bss->ni_macaddr, arg); 278178354Ssam if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) 279178354Ssam ieee80211_check_scan_current(vap); 280178354Ssam break; 281178354Ssam case IEEE80211_S_RUN: /* beacon miss */ 282178354Ssam /* 283178354Ssam * Beacon miss. Notify user space and if not 284178354Ssam * under control of a user application (roaming 285178354Ssam * manual) kick off a scan to re-connect. 286178354Ssam */ 287178354Ssam ieee80211_sta_leave(ni); 288178354Ssam if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) 289178354Ssam ieee80211_check_scan_current(vap); 290178354Ssam break; 291178354Ssam default: 292178354Ssam goto invalid; 293178354Ssam } 294178354Ssam break; 295178354Ssam case IEEE80211_S_AUTH: 296178354Ssam switch (ostate) { 297178354Ssam case IEEE80211_S_INIT: 298178354Ssam case IEEE80211_S_SCAN: 299178354Ssam IEEE80211_SEND_MGMT(ni, 300178354Ssam IEEE80211_FC0_SUBTYPE_AUTH, 1); 301178354Ssam break; 302178354Ssam case IEEE80211_S_AUTH: 303178354Ssam case IEEE80211_S_ASSOC: 304178354Ssam switch (arg & 0xff) { 305178354Ssam case IEEE80211_FC0_SUBTYPE_AUTH: 306178354Ssam /* ??? */ 307178354Ssam IEEE80211_SEND_MGMT(ni, 308178354Ssam IEEE80211_FC0_SUBTYPE_AUTH, 2); 309178354Ssam break; 310178354Ssam case IEEE80211_FC0_SUBTYPE_DEAUTH: 311178354Ssam sta_authretry(vap, ni, arg>>8); 312178354Ssam break; 313178354Ssam } 314178354Ssam break; 315178354Ssam case IEEE80211_S_RUN: 316178354Ssam switch (arg & 0xff) { 317178354Ssam case IEEE80211_FC0_SUBTYPE_AUTH: 318178354Ssam IEEE80211_SEND_MGMT(ni, 319178354Ssam IEEE80211_FC0_SUBTYPE_AUTH, 2); 320178354Ssam vap->iv_state = ostate; /* stay RUN */ 321178354Ssam break; 322178354Ssam case IEEE80211_FC0_SUBTYPE_DEAUTH: 323178354Ssam ieee80211_sta_leave(ni); 324178354Ssam if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) { 325178354Ssam /* try to reauth */ 326178354Ssam IEEE80211_SEND_MGMT(ni, 327178354Ssam IEEE80211_FC0_SUBTYPE_AUTH, 1); 328178354Ssam } 329178354Ssam break; 330178354Ssam } 331178354Ssam break; 332178354Ssam default: 333178354Ssam goto invalid; 334178354Ssam } 335178354Ssam break; 336178354Ssam case IEEE80211_S_ASSOC: 337178354Ssam switch (ostate) { 338178354Ssam case IEEE80211_S_AUTH: 339178354Ssam case IEEE80211_S_ASSOC: 340178354Ssam IEEE80211_SEND_MGMT(ni, 341178354Ssam IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0); 342178354Ssam break; 343178354Ssam case IEEE80211_S_SLEEP: /* cannot happen */ 344178354Ssam case IEEE80211_S_RUN: 345178354Ssam ieee80211_sta_leave(ni); 346178354Ssam if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) { 347178354Ssam IEEE80211_SEND_MGMT(ni, arg ? 348178354Ssam IEEE80211_FC0_SUBTYPE_REASSOC_REQ : 349178354Ssam IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0); 350178354Ssam } 351178354Ssam break; 352178354Ssam default: 353178354Ssam goto invalid; 354178354Ssam } 355178354Ssam break; 356178354Ssam case IEEE80211_S_RUN: 357178354Ssam if (vap->iv_flags & IEEE80211_F_WPA) { 358178354Ssam /* XXX validate prerequisites */ 359178354Ssam } 360178354Ssam switch (ostate) { 361178354Ssam case IEEE80211_S_RUN: 362178354Ssam break; 363178354Ssam case IEEE80211_S_AUTH: /* when join is done in fw */ 364178354Ssam case IEEE80211_S_ASSOC: 365178354Ssam#ifdef IEEE80211_DEBUG 366178354Ssam if (ieee80211_msg_debug(vap)) { 367178354Ssam ieee80211_note(vap, "%s with %s ssid ", 368178354Ssam (vap->iv_opmode == IEEE80211_M_STA ? 369178354Ssam "associated" : "synchronized"), 370178354Ssam ether_sprintf(ni->ni_bssid)); 371178354Ssam ieee80211_print_essid(vap->iv_bss->ni_essid, 372178354Ssam ni->ni_esslen); 373178354Ssam /* XXX MCS/HT */ 374178354Ssam printf(" channel %d start %uMb\n", 375178354Ssam ieee80211_chan2ieee(ic, ic->ic_curchan), 376178354Ssam IEEE80211_RATE2MBS(ni->ni_txrate)); 377178354Ssam } 378178354Ssam#endif 379178354Ssam ieee80211_scan_assoc_success(vap, ni->ni_macaddr); 380178354Ssam ieee80211_notify_node_join(ni, 381178354Ssam arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP); 382178354Ssam break; 383178354Ssam case IEEE80211_S_SLEEP: 384178354Ssam ieee80211_sta_pwrsave(vap, 0); 385178354Ssam break; 386178354Ssam default: 387178354Ssam goto invalid; 388178354Ssam } 389178354Ssam ieee80211_sync_curchan(ic); 390178354Ssam if (ostate != IEEE80211_S_RUN && 391178354Ssam (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)) { 392178354Ssam /* 393178354Ssam * Start s/w beacon miss timer for devices w/o 394178354Ssam * hardware support. We fudge a bit here since 395178354Ssam * we're doing this in software. 396178354Ssam */ 397178354Ssam vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS( 398178354Ssam 2 * vap->iv_bmissthreshold * ni->ni_intval); 399178354Ssam vap->iv_swbmiss_count = 0; 400178354Ssam callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period, 401178354Ssam ieee80211_swbmiss, vap); 402178354Ssam } 403178354Ssam /* 404178354Ssam * When 802.1x is not in use mark the port authorized 405178354Ssam * at this point so traffic can flow. 406178354Ssam */ 407178354Ssam if (ni->ni_authmode != IEEE80211_AUTH_8021X) 408178354Ssam ieee80211_node_authorize(ni); 409184345Ssam /* 410184345Ssam * Fake association when joining an existing bss. 411184345Ssam */ 412184345Ssam if (ic->ic_newassoc != NULL) 413184345Ssam ic->ic_newassoc(vap->iv_bss, ostate != IEEE80211_S_RUN); 414178354Ssam break; 415178354Ssam case IEEE80211_S_SLEEP: 416178354Ssam ieee80211_sta_pwrsave(vap, 0); 417178354Ssam break; 418178354Ssam default: 419178354Ssam invalid: 420184268Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, 421184268Ssam "%s: unexpected state transition %s -> %s\n", __func__, 422178354Ssam ieee80211_state_name[ostate], ieee80211_state_name[nstate]); 423178354Ssam break; 424178354Ssam } 425178354Ssam return 0; 426178354Ssam} 427178354Ssam 428178354Ssam/* 429178354Ssam * Return non-zero if the frame is an echo of a multicast 430178354Ssam * frame sent by ourself. The dir is known to be DSTODS. 431178354Ssam */ 432178354Ssamstatic __inline int 433178354Ssamisdstods_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh) 434178354Ssam{ 435178354Ssam#define QWH4(wh) ((const struct ieee80211_qosframe_addr4 *)wh) 436178354Ssam#define WH4(wh) ((const struct ieee80211_frame_addr4 *)wh) 437178354Ssam const uint8_t *sa; 438178354Ssam 439178354Ssam KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode")); 440178354Ssam 441178354Ssam if (!IEEE80211_IS_MULTICAST(wh->i_addr3)) 442178354Ssam return 0; 443178354Ssam sa = IEEE80211_QOS_HAS_SEQ(wh) ? QWH4(wh)->i_addr4 : WH4(wh)->i_addr4; 444178354Ssam return IEEE80211_ADDR_EQ(sa, vap->iv_myaddr); 445178354Ssam#undef WH4 446178354Ssam#undef QWH4 447178354Ssam} 448178354Ssam 449178354Ssam/* 450178354Ssam * Return non-zero if the frame is an echo of a multicast 451178354Ssam * frame sent by ourself. The dir is known to be FROMDS. 452178354Ssam */ 453178354Ssamstatic __inline int 454178354Ssamisfromds_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh) 455178354Ssam{ 456178354Ssam KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode")); 457178354Ssam 458178354Ssam if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) 459178354Ssam return 0; 460178354Ssam return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr); 461178354Ssam} 462178354Ssam 463178354Ssam/* 464178354Ssam * Decide if a received management frame should be 465178354Ssam * printed when debugging is enabled. This filters some 466178354Ssam * of the less interesting frames that come frequently 467178354Ssam * (e.g. beacons). 468178354Ssam */ 469178354Ssamstatic __inline int 470178354Ssamdoprint(struct ieee80211vap *vap, int subtype) 471178354Ssam{ 472178354Ssam switch (subtype) { 473178354Ssam case IEEE80211_FC0_SUBTYPE_BEACON: 474178354Ssam return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN); 475178354Ssam case IEEE80211_FC0_SUBTYPE_PROBE_REQ: 476178354Ssam return 0; 477178354Ssam } 478178354Ssam return 1; 479178354Ssam} 480178354Ssam 481178354Ssam/* 482178354Ssam * Process a received frame. The node associated with the sender 483178354Ssam * should be supplied. If nothing was found in the node table then 484178354Ssam * the caller is assumed to supply a reference to iv_bss instead. 485178354Ssam * The RSSI and a timestamp are also supplied. The RSSI data is used 486178354Ssam * during AP scanning to select a AP to associate with; it can have 487178354Ssam * any units so long as values have consistent units and higher values 488178354Ssam * mean ``better signal''. The receive timestamp is currently not used 489178354Ssam * by the 802.11 layer. 490178354Ssam */ 491178354Ssamstatic int 492192468Ssamsta_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf) 493178354Ssam{ 494178354Ssam#define SEQ_LEQ(a,b) ((int)((a)-(b)) <= 0) 495178354Ssam#define HAS_SEQ(type) ((type & 0x4) == 0) 496178354Ssam struct ieee80211vap *vap = ni->ni_vap; 497178354Ssam struct ieee80211com *ic = ni->ni_ic; 498178354Ssam struct ifnet *ifp = vap->iv_ifp; 499178354Ssam struct ieee80211_frame *wh; 500178354Ssam struct ieee80211_key *key; 501178354Ssam struct ether_header *eh; 502178354Ssam int hdrspace, need_tap; 503178354Ssam uint8_t dir, type, subtype, qos; 504178354Ssam uint8_t *bssid; 505178354Ssam uint16_t rxseq; 506178354Ssam 507183247Ssam if (m->m_flags & M_AMPDU_MPDU) { 508178354Ssam /* 509178354Ssam * Fastpath for A-MPDU reorder q resubmission. Frames 510183247Ssam * w/ M_AMPDU_MPDU marked have already passed through 511183247Ssam * here but were received out of order and been held on 512183247Ssam * the reorder queue. When resubmitted they are marked 513183247Ssam * with the M_AMPDU_MPDU flag and we can bypass most of 514183247Ssam * the normal processing. 515178354Ssam */ 516178354Ssam wh = mtod(m, struct ieee80211_frame *); 517178354Ssam type = IEEE80211_FC0_TYPE_DATA; 518178354Ssam dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 519178354Ssam subtype = IEEE80211_FC0_SUBTYPE_QOS; 520178354Ssam hdrspace = ieee80211_hdrspace(ic, wh); /* XXX optimize? */ 521178354Ssam goto resubmit_ampdu; 522178354Ssam } 523178354Ssam 524178354Ssam KASSERT(ni != NULL, ("null node")); 525178354Ssam ni->ni_inact = ni->ni_inact_reload; 526178354Ssam 527178354Ssam need_tap = 1; /* mbuf need to be tapped. */ 528178354Ssam type = -1; /* undefined */ 529178354Ssam 530178354Ssam if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) { 531178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 532178354Ssam ni->ni_macaddr, NULL, 533178354Ssam "too short (1): len %u", m->m_pkthdr.len); 534178354Ssam vap->iv_stats.is_rx_tooshort++; 535178354Ssam goto out; 536178354Ssam } 537178354Ssam /* 538178354Ssam * Bit of a cheat here, we use a pointer for a 3-address 539178354Ssam * frame format but don't reference fields past outside 540178354Ssam * ieee80211_frame_min w/o first validating the data is 541178354Ssam * present. 542178354Ssam */ 543178354Ssam wh = mtod(m, struct ieee80211_frame *); 544178354Ssam 545178354Ssam if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) != 546178354Ssam IEEE80211_FC0_VERSION_0) { 547178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 548191547Ssam ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x", 549191547Ssam wh->i_fc[0], wh->i_fc[1]); 550178354Ssam vap->iv_stats.is_rx_badversion++; 551178354Ssam goto err; 552178354Ssam } 553178354Ssam 554178354Ssam dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 555178354Ssam type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 556178354Ssam subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 557178354Ssam if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) { 558178354Ssam bssid = wh->i_addr2; 559178354Ssam if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) { 560178354Ssam /* not interested in */ 561178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 562178354Ssam bssid, NULL, "%s", "not to bss"); 563178354Ssam vap->iv_stats.is_rx_wrongbss++; 564178354Ssam goto out; 565178354Ssam } 566178354Ssam IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi); 567192468Ssam ni->ni_noise = nf; 568178354Ssam if (HAS_SEQ(type)) { 569178354Ssam uint8_t tid = ieee80211_gettid(wh); 570178354Ssam if (IEEE80211_QOS_HAS_SEQ(wh) && 571178354Ssam TID_TO_WME_AC(tid) >= WME_AC_VI) 572178354Ssam ic->ic_wme.wme_hipri_traffic++; 573178354Ssam rxseq = le16toh(*(uint16_t *)wh->i_seq); 574178354Ssam if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 && 575178354Ssam (wh->i_fc[1] & IEEE80211_FC1_RETRY) && 576178354Ssam SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) { 577178354Ssam /* duplicate, discard */ 578178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 579178354Ssam bssid, "duplicate", 580178354Ssam "seqno <%u,%u> fragno <%u,%u> tid %u", 581178354Ssam rxseq >> IEEE80211_SEQ_SEQ_SHIFT, 582178354Ssam ni->ni_rxseqs[tid] >> 583178354Ssam IEEE80211_SEQ_SEQ_SHIFT, 584178354Ssam rxseq & IEEE80211_SEQ_FRAG_MASK, 585178354Ssam ni->ni_rxseqs[tid] & 586178354Ssam IEEE80211_SEQ_FRAG_MASK, 587178354Ssam tid); 588178354Ssam vap->iv_stats.is_rx_dup++; 589178354Ssam IEEE80211_NODE_STAT(ni, rx_dup); 590178354Ssam goto out; 591178354Ssam } 592178354Ssam ni->ni_rxseqs[tid] = rxseq; 593178354Ssam } 594178354Ssam } 595178354Ssam 596178354Ssam switch (type) { 597178354Ssam case IEEE80211_FC0_TYPE_DATA: 598178354Ssam hdrspace = ieee80211_hdrspace(ic, wh); 599178354Ssam if (m->m_len < hdrspace && 600178354Ssam (m = m_pullup(m, hdrspace)) == NULL) { 601178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 602178354Ssam ni->ni_macaddr, NULL, 603178354Ssam "data too short: expecting %u", hdrspace); 604178354Ssam vap->iv_stats.is_rx_tooshort++; 605178354Ssam goto out; /* XXX */ 606178354Ssam } 607178354Ssam /* 608183247Ssam * Handle A-MPDU re-ordering. If the frame is to be 609183247Ssam * processed directly then ieee80211_ampdu_reorder 610178354Ssam * will return 0; otherwise it has consumed the mbuf 611178354Ssam * and we should do nothing more with it. 612178354Ssam */ 613183247Ssam if ((m->m_flags & M_AMPDU) && 614178354Ssam (dir == IEEE80211_FC1_DIR_FROMDS || 615178354Ssam dir == IEEE80211_FC1_DIR_DSTODS) && 616178354Ssam ieee80211_ampdu_reorder(ni, m) != 0) { 617178354Ssam m = NULL; 618178354Ssam goto out; 619178354Ssam } 620178354Ssam resubmit_ampdu: 621178354Ssam if (dir == IEEE80211_FC1_DIR_FROMDS) { 622178354Ssam if ((ifp->if_flags & IFF_SIMPLEX) && 623178354Ssam isfromds_mcastecho(vap, wh)) { 624178354Ssam /* 625178354Ssam * In IEEE802.11 network, multicast 626178354Ssam * packets sent from "me" are broadcast 627178354Ssam * from the AP; silently discard for 628178354Ssam * SIMPLEX interface. 629178354Ssam */ 630178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 631178354Ssam wh, "data", "%s", "multicast echo"); 632178354Ssam vap->iv_stats.is_rx_mcastecho++; 633178354Ssam goto out; 634178354Ssam } 635178354Ssam if ((vap->iv_flags & IEEE80211_F_DWDS) && 636178354Ssam IEEE80211_IS_MULTICAST(wh->i_addr1)) { 637178354Ssam /* 638178354Ssam * DWDS sta's must drop 3-address mcast frames 639178354Ssam * as they will be sent separately as a 4-addr 640178354Ssam * frame. Accepting the 3-addr frame will 641178354Ssam * confuse the bridge into thinking the sending 642178354Ssam * sta is located at the end of WDS link. 643178354Ssam */ 644178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh, 645178354Ssam "3-address data", "%s", "DWDS enabled"); 646178354Ssam vap->iv_stats.is_rx_mcastecho++; 647178354Ssam goto out; 648178354Ssam } 649178354Ssam } else if (dir == IEEE80211_FC1_DIR_DSTODS) { 650178354Ssam if ((vap->iv_flags & IEEE80211_F_DWDS) == 0) { 651178354Ssam IEEE80211_DISCARD(vap, 652178354Ssam IEEE80211_MSG_INPUT, wh, "4-address data", 653178354Ssam "%s", "DWDS not enabled"); 654178354Ssam vap->iv_stats.is_rx_wrongdir++; 655178354Ssam goto out; 656178354Ssam } 657178354Ssam if ((ifp->if_flags & IFF_SIMPLEX) && 658178354Ssam isdstods_mcastecho(vap, wh)) { 659178354Ssam /* 660178354Ssam * In IEEE802.11 network, multicast 661178354Ssam * packets sent from "me" are broadcast 662178354Ssam * from the AP; silently discard for 663178354Ssam * SIMPLEX interface. 664178354Ssam */ 665178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh, 666178354Ssam "4-address data", "%s", "multicast echo"); 667178354Ssam vap->iv_stats.is_rx_mcastecho++; 668178354Ssam goto out; 669178354Ssam } 670178354Ssam } else { 671178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh, 672178354Ssam "data", "incorrect dir 0x%x", dir); 673178354Ssam vap->iv_stats.is_rx_wrongdir++; 674178354Ssam goto out; 675178354Ssam } 676178354Ssam 677178354Ssam /* 678178354Ssam * Handle privacy requirements. Note that we 679178354Ssam * must not be preempted from here until after 680178354Ssam * we (potentially) call ieee80211_crypto_demic; 681178354Ssam * otherwise we may violate assumptions in the 682178354Ssam * crypto cipher modules used to do delayed update 683178354Ssam * of replay sequence numbers. 684178354Ssam */ 685178354Ssam if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 686178354Ssam if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) { 687178354Ssam /* 688178354Ssam * Discard encrypted frames when privacy is off. 689178354Ssam */ 690178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 691178354Ssam wh, "WEP", "%s", "PRIVACY off"); 692178354Ssam vap->iv_stats.is_rx_noprivacy++; 693178354Ssam IEEE80211_NODE_STAT(ni, rx_noprivacy); 694178354Ssam goto out; 695178354Ssam } 696178354Ssam key = ieee80211_crypto_decap(ni, m, hdrspace); 697178354Ssam if (key == NULL) { 698178354Ssam /* NB: stats+msgs handled in crypto_decap */ 699178354Ssam IEEE80211_NODE_STAT(ni, rx_wepfail); 700178354Ssam goto out; 701178354Ssam } 702178354Ssam wh = mtod(m, struct ieee80211_frame *); 703178354Ssam wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 704178354Ssam } else { 705178354Ssam /* XXX M_WEP and IEEE80211_F_PRIVACY */ 706178354Ssam key = NULL; 707178354Ssam } 708178354Ssam 709178354Ssam /* 710178354Ssam * Save QoS bits for use below--before we strip the header. 711178354Ssam */ 712178354Ssam if (subtype == IEEE80211_FC0_SUBTYPE_QOS) { 713178354Ssam qos = (dir == IEEE80211_FC1_DIR_DSTODS) ? 714178354Ssam ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] : 715178354Ssam ((struct ieee80211_qosframe *)wh)->i_qos[0]; 716178354Ssam } else 717178354Ssam qos = 0; 718178354Ssam 719178354Ssam /* 720178354Ssam * Next up, any fragmentation. 721178354Ssam */ 722178354Ssam if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 723178354Ssam m = ieee80211_defrag(ni, m, hdrspace); 724178354Ssam if (m == NULL) { 725178354Ssam /* Fragment dropped or frame not complete yet */ 726178354Ssam goto out; 727178354Ssam } 728178354Ssam } 729178354Ssam wh = NULL; /* no longer valid, catch any uses */ 730178354Ssam 731178354Ssam /* 732178354Ssam * Next strip any MSDU crypto bits. 733178354Ssam */ 734178354Ssam if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) { 735178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 736178354Ssam ni->ni_macaddr, "data", "%s", "demic error"); 737178354Ssam vap->iv_stats.is_rx_demicfail++; 738178354Ssam IEEE80211_NODE_STAT(ni, rx_demicfail); 739178354Ssam goto out; 740178354Ssam } 741178354Ssam 742178354Ssam /* copy to listener after decrypt */ 743192468Ssam if (ieee80211_radiotap_active_vap(vap)) 744192468Ssam ieee80211_radiotap_tx(vap, m); 745178354Ssam need_tap = 0; 746178354Ssam 747178354Ssam /* 748178354Ssam * Finally, strip the 802.11 header. 749178354Ssam */ 750178354Ssam m = ieee80211_decap(vap, m, hdrspace); 751178354Ssam if (m == NULL) { 752178354Ssam /* XXX mask bit to check for both */ 753178354Ssam /* don't count Null data frames as errors */ 754178354Ssam if (subtype == IEEE80211_FC0_SUBTYPE_NODATA || 755178354Ssam subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL) 756178354Ssam goto out; 757178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 758178354Ssam ni->ni_macaddr, "data", "%s", "decap error"); 759178354Ssam vap->iv_stats.is_rx_decap++; 760178354Ssam IEEE80211_NODE_STAT(ni, rx_decap); 761178354Ssam goto err; 762178354Ssam } 763178354Ssam eh = mtod(m, struct ether_header *); 764178354Ssam if (!ieee80211_node_is_authorized(ni)) { 765178354Ssam /* 766178354Ssam * Deny any non-PAE frames received prior to 767178354Ssam * authorization. For open/shared-key 768178354Ssam * authentication the port is mark authorized 769178354Ssam * after authentication completes. For 802.1x 770178354Ssam * the port is not marked authorized by the 771178354Ssam * authenticator until the handshake has completed. 772178354Ssam */ 773178354Ssam if (eh->ether_type != htons(ETHERTYPE_PAE)) { 774178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 775178354Ssam eh->ether_shost, "data", 776178354Ssam "unauthorized port: ether type 0x%x len %u", 777178354Ssam eh->ether_type, m->m_pkthdr.len); 778178354Ssam vap->iv_stats.is_rx_unauth++; 779178354Ssam IEEE80211_NODE_STAT(ni, rx_unauth); 780178354Ssam goto err; 781178354Ssam } 782178354Ssam } else { 783178354Ssam /* 784178354Ssam * When denying unencrypted frames, discard 785178354Ssam * any non-PAE frames received without encryption. 786178354Ssam */ 787178354Ssam if ((vap->iv_flags & IEEE80211_F_DROPUNENC) && 788178354Ssam (key == NULL && (m->m_flags & M_WEP) == 0) && 789178354Ssam eh->ether_type != htons(ETHERTYPE_PAE)) { 790178354Ssam /* 791178354Ssam * Drop unencrypted frames. 792178354Ssam */ 793178354Ssam vap->iv_stats.is_rx_unencrypted++; 794178354Ssam IEEE80211_NODE_STAT(ni, rx_unencrypted); 795178354Ssam goto out; 796178354Ssam } 797178354Ssam } 798178354Ssam /* XXX require HT? */ 799178354Ssam if (qos & IEEE80211_QOS_AMSDU) { 800178354Ssam m = ieee80211_decap_amsdu(ni, m); 801178354Ssam if (m == NULL) 802178354Ssam return IEEE80211_FC0_TYPE_DATA; 803190391Ssam } else { 804190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG 805190391Ssam m = ieee80211_decap_fastframe(vap, ni, m); 806190391Ssam if (m == NULL) 807178354Ssam return IEEE80211_FC0_TYPE_DATA; 808190391Ssam#endif 809178354Ssam } 810178354Ssam ieee80211_deliver_data(vap, ni, m); 811178354Ssam return IEEE80211_FC0_TYPE_DATA; 812178354Ssam 813178354Ssam case IEEE80211_FC0_TYPE_MGT: 814178354Ssam vap->iv_stats.is_rx_mgmt++; 815178354Ssam IEEE80211_NODE_STAT(ni, rx_mgmt); 816178354Ssam if (dir != IEEE80211_FC1_DIR_NODS) { 817178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 818178354Ssam wh, "data", "incorrect dir 0x%x", dir); 819178354Ssam vap->iv_stats.is_rx_wrongdir++; 820178354Ssam goto err; 821178354Ssam } 822178354Ssam if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) { 823178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 824178354Ssam ni->ni_macaddr, "mgt", "too short: len %u", 825178354Ssam m->m_pkthdr.len); 826178354Ssam vap->iv_stats.is_rx_tooshort++; 827178354Ssam goto out; 828178354Ssam } 829178354Ssam#ifdef IEEE80211_DEBUG 830178354Ssam if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) || 831178354Ssam ieee80211_msg_dumppkts(vap)) { 832178354Ssam if_printf(ifp, "received %s from %s rssi %d\n", 833178354Ssam ieee80211_mgt_subtype_name[subtype >> 834178354Ssam IEEE80211_FC0_SUBTYPE_SHIFT], 835178354Ssam ether_sprintf(wh->i_addr2), rssi); 836178354Ssam } 837178354Ssam#endif 838178354Ssam if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 839178354Ssam if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) { 840178354Ssam /* 841178354Ssam * Only shared key auth frames with a challenge 842178354Ssam * should be encrypted, discard all others. 843178354Ssam */ 844178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 845178354Ssam wh, ieee80211_mgt_subtype_name[subtype >> 846178354Ssam IEEE80211_FC0_SUBTYPE_SHIFT], 847178354Ssam "%s", "WEP set but not permitted"); 848178354Ssam vap->iv_stats.is_rx_mgtdiscard++; /* XXX */ 849178354Ssam goto out; 850178354Ssam } 851178354Ssam if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) { 852178354Ssam /* 853178354Ssam * Discard encrypted frames when privacy is off. 854178354Ssam */ 855178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 856178354Ssam wh, "mgt", "%s", "WEP set but PRIVACY off"); 857178354Ssam vap->iv_stats.is_rx_noprivacy++; 858178354Ssam goto out; 859178354Ssam } 860178354Ssam hdrspace = ieee80211_hdrspace(ic, wh); 861178354Ssam key = ieee80211_crypto_decap(ni, m, hdrspace); 862178354Ssam if (key == NULL) { 863178354Ssam /* NB: stats+msgs handled in crypto_decap */ 864178354Ssam goto out; 865178354Ssam } 866178354Ssam wh = mtod(m, struct ieee80211_frame *); 867178354Ssam wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 868178354Ssam } 869192468Ssam vap->iv_recv_mgmt(ni, m, subtype, rssi, nf); 870191534Ssam goto out; 871178354Ssam 872178354Ssam case IEEE80211_FC0_TYPE_CTL: 873178354Ssam vap->iv_stats.is_rx_ctl++; 874178354Ssam IEEE80211_NODE_STAT(ni, rx_ctrl); 875191546Ssam vap->iv_recv_ctl(ni, m, subtype); 876178354Ssam goto out; 877191549Ssam 878178354Ssam default: 879178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 880178354Ssam wh, NULL, "bad frame type 0x%x", type); 881178354Ssam /* should not come here */ 882178354Ssam break; 883178354Ssam } 884178354Ssamerr: 885178354Ssam ifp->if_ierrors++; 886178354Ssamout: 887178354Ssam if (m != NULL) { 888192468Ssam if (need_tap) 889192468Ssam ieee80211_radiotap_rx(vap, m); 890178354Ssam m_freem(m); 891178354Ssam } 892178354Ssam return type; 893178354Ssam#undef SEQ_LEQ 894178354Ssam} 895178354Ssam 896178354Ssamstatic void 897178354Ssamsta_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh, 898192468Ssam int rssi, int nf, uint16_t seq, uint16_t status) 899178354Ssam{ 900178354Ssam struct ieee80211vap *vap = ni->ni_vap; 901178354Ssam 902178354Ssam if (ni->ni_authmode == IEEE80211_AUTH_SHARED) { 903178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH, 904178354Ssam ni->ni_macaddr, "open auth", 905178354Ssam "bad sta auth mode %u", ni->ni_authmode); 906178354Ssam vap->iv_stats.is_rx_bad_auth++; /* XXX */ 907178354Ssam return; 908178354Ssam } 909178354Ssam if (vap->iv_state != IEEE80211_S_AUTH || 910178354Ssam seq != IEEE80211_AUTH_OPEN_RESPONSE) { 911178354Ssam vap->iv_stats.is_rx_bad_auth++; 912178354Ssam return; 913178354Ssam } 914178354Ssam if (status != 0) { 915178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, 916178354Ssam ni, "open auth failed (reason %d)", status); 917178354Ssam vap->iv_stats.is_rx_auth_fail++; 918178354Ssam vap->iv_stats.is_rx_authfail_code = status; 919178354Ssam ieee80211_new_state(vap, IEEE80211_S_SCAN, 920178354Ssam IEEE80211_SCAN_FAIL_STATUS); 921178354Ssam } else 922178354Ssam ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0); 923178354Ssam} 924178354Ssam 925178354Ssamstatic void 926178354Ssamsta_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh, 927192468Ssam uint8_t *frm, uint8_t *efrm, int rssi, int nf, 928178354Ssam uint16_t seq, uint16_t status) 929178354Ssam{ 930178354Ssam struct ieee80211vap *vap = ni->ni_vap; 931178354Ssam uint8_t *challenge; 932178354Ssam int estatus; 933178354Ssam 934178354Ssam /* 935178354Ssam * NB: this can happen as we allow pre-shared key 936178354Ssam * authentication to be enabled w/o wep being turned 937178354Ssam * on so that configuration of these can be done 938178354Ssam * in any order. It may be better to enforce the 939178354Ssam * ordering in which case this check would just be 940178354Ssam * for sanity/consistency. 941178354Ssam */ 942178354Ssam if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) { 943178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH, 944178354Ssam ni->ni_macaddr, "shared key auth", 945178354Ssam "%s", " PRIVACY is disabled"); 946178354Ssam estatus = IEEE80211_STATUS_ALG; 947178354Ssam goto bad; 948178354Ssam } 949178354Ssam /* 950178354Ssam * Pre-shared key authentication is evil; accept 951178354Ssam * it only if explicitly configured (it is supported 952178354Ssam * mainly for compatibility with clients like OS X). 953178354Ssam */ 954178354Ssam if (ni->ni_authmode != IEEE80211_AUTH_AUTO && 955178354Ssam ni->ni_authmode != IEEE80211_AUTH_SHARED) { 956178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH, 957178354Ssam ni->ni_macaddr, "shared key auth", 958178354Ssam "bad sta auth mode %u", ni->ni_authmode); 959178354Ssam vap->iv_stats.is_rx_bad_auth++; /* XXX maybe a unique error? */ 960178354Ssam estatus = IEEE80211_STATUS_ALG; 961178354Ssam goto bad; 962178354Ssam } 963178354Ssam 964178354Ssam challenge = NULL; 965178354Ssam if (frm + 1 < efrm) { 966178354Ssam if ((frm[1] + 2) > (efrm - frm)) { 967178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH, 968178354Ssam ni->ni_macaddr, "shared key auth", 969178354Ssam "ie %d/%d too long", 970178354Ssam frm[0], (frm[1] + 2) - (efrm - frm)); 971178354Ssam vap->iv_stats.is_rx_bad_auth++; 972178354Ssam estatus = IEEE80211_STATUS_CHALLENGE; 973178354Ssam goto bad; 974178354Ssam } 975178354Ssam if (*frm == IEEE80211_ELEMID_CHALLENGE) 976178354Ssam challenge = frm; 977178354Ssam frm += frm[1] + 2; 978178354Ssam } 979178354Ssam switch (seq) { 980178354Ssam case IEEE80211_AUTH_SHARED_CHALLENGE: 981178354Ssam case IEEE80211_AUTH_SHARED_RESPONSE: 982178354Ssam if (challenge == NULL) { 983178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH, 984178354Ssam ni->ni_macaddr, "shared key auth", 985178354Ssam "%s", "no challenge"); 986178354Ssam vap->iv_stats.is_rx_bad_auth++; 987178354Ssam estatus = IEEE80211_STATUS_CHALLENGE; 988178354Ssam goto bad; 989178354Ssam } 990178354Ssam if (challenge[1] != IEEE80211_CHALLENGE_LEN) { 991178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH, 992178354Ssam ni->ni_macaddr, "shared key auth", 993178354Ssam "bad challenge len %d", challenge[1]); 994178354Ssam vap->iv_stats.is_rx_bad_auth++; 995178354Ssam estatus = IEEE80211_STATUS_CHALLENGE; 996178354Ssam goto bad; 997178354Ssam } 998178354Ssam default: 999178354Ssam break; 1000178354Ssam } 1001178354Ssam if (vap->iv_state != IEEE80211_S_AUTH) 1002178354Ssam return; 1003178354Ssam switch (seq) { 1004178354Ssam case IEEE80211_AUTH_SHARED_PASS: 1005178354Ssam if (ni->ni_challenge != NULL) { 1006186302Ssam free(ni->ni_challenge, M_80211_NODE); 1007178354Ssam ni->ni_challenge = NULL; 1008178354Ssam } 1009178354Ssam if (status != 0) { 1010178354Ssam IEEE80211_NOTE_FRAME(vap, 1011178354Ssam IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, wh, 1012178354Ssam "shared key auth failed (reason %d)", status); 1013178354Ssam vap->iv_stats.is_rx_auth_fail++; 1014178354Ssam vap->iv_stats.is_rx_authfail_code = status; 1015178354Ssam return; 1016178354Ssam } 1017178354Ssam ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0); 1018178354Ssam break; 1019178354Ssam case IEEE80211_AUTH_SHARED_CHALLENGE: 1020178354Ssam if (!ieee80211_alloc_challenge(ni)) 1021178354Ssam return; 1022178354Ssam /* XXX could optimize by passing recvd challenge */ 1023178354Ssam memcpy(ni->ni_challenge, &challenge[2], challenge[1]); 1024178354Ssam IEEE80211_SEND_MGMT(ni, 1025178354Ssam IEEE80211_FC0_SUBTYPE_AUTH, seq + 1); 1026178354Ssam break; 1027178354Ssam default: 1028178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_AUTH, 1029178354Ssam wh, "shared key auth", "bad seq %d", seq); 1030178354Ssam vap->iv_stats.is_rx_bad_auth++; 1031178354Ssam return; 1032178354Ssam } 1033178354Ssam return; 1034178354Ssambad: 1035178354Ssam /* 1036178354Ssam * Kick the state machine. This short-circuits 1037178354Ssam * using the mgt frame timeout to trigger the 1038178354Ssam * state transition. 1039178354Ssam */ 1040178354Ssam if (vap->iv_state == IEEE80211_S_AUTH) 1041178354Ssam ieee80211_new_state(vap, IEEE80211_S_SCAN, 1042178354Ssam IEEE80211_SCAN_FAIL_STATUS); 1043178354Ssam} 1044178354Ssam 1045178354Ssamstatic int 1046178354Ssamieee80211_parse_wmeparams(struct ieee80211vap *vap, uint8_t *frm, 1047178354Ssam const struct ieee80211_frame *wh) 1048178354Ssam{ 1049178354Ssam#define MS(_v, _f) (((_v) & _f) >> _f##_S) 1050178354Ssam struct ieee80211_wme_state *wme = &vap->iv_ic->ic_wme; 1051178354Ssam u_int len = frm[1], qosinfo; 1052178354Ssam int i; 1053178354Ssam 1054178354Ssam if (len < sizeof(struct ieee80211_wme_param)-2) { 1055178354Ssam IEEE80211_DISCARD_IE(vap, 1056178354Ssam IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME, 1057178354Ssam wh, "WME", "too short, len %u", len); 1058178354Ssam return -1; 1059178354Ssam } 1060178354Ssam qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)]; 1061178354Ssam qosinfo &= WME_QOSINFO_COUNT; 1062178354Ssam /* XXX do proper check for wraparound */ 1063178354Ssam if (qosinfo == wme->wme_wmeChanParams.cap_info) 1064178354Ssam return 0; 1065178354Ssam frm += __offsetof(struct ieee80211_wme_param, params_acParams); 1066178354Ssam for (i = 0; i < WME_NUM_AC; i++) { 1067178354Ssam struct wmeParams *wmep = 1068178354Ssam &wme->wme_wmeChanParams.cap_wmeParams[i]; 1069178354Ssam /* NB: ACI not used */ 1070178354Ssam wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM); 1071178354Ssam wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN); 1072178354Ssam wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN); 1073178354Ssam wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX); 1074178354Ssam wmep->wmep_txopLimit = LE_READ_2(frm+2); 1075178354Ssam frm += 4; 1076178354Ssam } 1077178354Ssam wme->wme_wmeChanParams.cap_info = qosinfo; 1078178354Ssam return 1; 1079178354Ssam#undef MS 1080178354Ssam} 1081178354Ssam 1082178354Ssam/* 1083178354Ssam * Return non-zero if a background scan may be continued: 1084178354Ssam * o bg scan is active 1085178354Ssam * o no channel switch is pending 1086178354Ssam * o there has not been any traffic recently 1087178354Ssam * 1088178354Ssam * Note we do not check if there is an administrative enable; 1089178354Ssam * this is only done to start the scan. We assume that any 1090178354Ssam * change in state will be accompanied by a request to cancel 1091178354Ssam * active scans which will otherwise cause this test to fail. 1092178354Ssam */ 1093178354Ssamstatic __inline int 1094178354Ssamcontbgscan(struct ieee80211vap *vap) 1095178354Ssam{ 1096178354Ssam struct ieee80211com *ic = vap->iv_ic; 1097178354Ssam 1098178354Ssam return ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) && 1099178354Ssam (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 && 1100178354Ssam vap->iv_state == IEEE80211_S_RUN && /* XXX? */ 1101178354Ssam time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle)); 1102178354Ssam} 1103178354Ssam 1104178354Ssam/* 1105178354Ssam * Return non-zero if a backgrond scan may be started: 1106178354Ssam * o bg scanning is administratively enabled 1107178354Ssam * o no channel switch is pending 1108178354Ssam * o we are not boosted on a dynamic turbo channel 1109178354Ssam * o there has not been a scan recently 1110178354Ssam * o there has not been any traffic recently 1111178354Ssam */ 1112178354Ssamstatic __inline int 1113178354Ssamstartbgscan(struct ieee80211vap *vap) 1114178354Ssam{ 1115178354Ssam struct ieee80211com *ic = vap->iv_ic; 1116178354Ssam 1117178354Ssam return ((vap->iv_flags & IEEE80211_F_BGSCAN) && 1118178354Ssam (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 && 1119190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG 1120178354Ssam !IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) && 1121190391Ssam#endif 1122178354Ssam time_after(ticks, ic->ic_lastscan + vap->iv_bgscanintvl) && 1123178354Ssam time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle)); 1124178354Ssam} 1125178354Ssam 1126178354Ssamstatic void 1127178354Ssamsta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, 1128192468Ssam int subtype, int rssi, int nf) 1129178354Ssam{ 1130178354Ssam#define ISPROBE(_st) ((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP) 1131178354Ssam#define ISREASSOC(_st) ((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP) 1132178354Ssam struct ieee80211vap *vap = ni->ni_vap; 1133178354Ssam struct ieee80211com *ic = ni->ni_ic; 1134178354Ssam struct ieee80211_frame *wh; 1135178354Ssam uint8_t *frm, *efrm; 1136178354Ssam uint8_t *rates, *xrates, *wme, *htcap, *htinfo; 1137178354Ssam uint8_t rate; 1138178354Ssam 1139178354Ssam wh = mtod(m0, struct ieee80211_frame *); 1140178354Ssam frm = (uint8_t *)&wh[1]; 1141178354Ssam efrm = mtod(m0, uint8_t *) + m0->m_len; 1142178354Ssam switch (subtype) { 1143178354Ssam case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 1144178354Ssam case IEEE80211_FC0_SUBTYPE_BEACON: { 1145178354Ssam struct ieee80211_scanparams scan; 1146178354Ssam /* 1147178354Ssam * We process beacon/probe response frames: 1148178354Ssam * o when scanning, or 1149178354Ssam * o station mode when associated (to collect state 1150191444Srpaulo * updates such as 802.11g slot time) 1151178354Ssam * Frames otherwise received are discarded. 1152178354Ssam */ 1153178354Ssam if (!((ic->ic_flags & IEEE80211_F_SCAN) || ni->ni_associd)) { 1154178354Ssam vap->iv_stats.is_rx_mgtdiscard++; 1155178354Ssam return; 1156178354Ssam } 1157178354Ssam /* XXX probe response in sta mode when !scanning? */ 1158178354Ssam if (ieee80211_parse_beacon(ni, m0, &scan) != 0) 1159178354Ssam return; 1160178354Ssam /* 1161178354Ssam * Count frame now that we know it's to be processed. 1162178354Ssam */ 1163178354Ssam if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) { 1164178354Ssam vap->iv_stats.is_rx_beacon++; /* XXX remove */ 1165178354Ssam IEEE80211_NODE_STAT(ni, rx_beacons); 1166178354Ssam } else 1167178354Ssam IEEE80211_NODE_STAT(ni, rx_proberesp); 1168178354Ssam /* 1169178354Ssam * When operating in station mode, check for state updates. 1170178354Ssam * Be careful to ignore beacons received while doing a 1171178354Ssam * background scan. We consider only 11g/WMM stuff right now. 1172178354Ssam */ 1173178354Ssam if (ni->ni_associd != 0 && 1174178354Ssam ((ic->ic_flags & IEEE80211_F_SCAN) == 0 || 1175178354Ssam IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) { 1176178354Ssam /* record tsf of last beacon */ 1177178354Ssam memcpy(ni->ni_tstamp.data, scan.tstamp, 1178178354Ssam sizeof(ni->ni_tstamp)); 1179178354Ssam /* count beacon frame for s/w bmiss handling */ 1180178354Ssam vap->iv_swbmiss_count++; 1181178354Ssam vap->iv_bmiss_count = 0; 1182178354Ssam if (ni->ni_erp != scan.erp) { 1183178354Ssam IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC, 1184178354Ssam wh->i_addr2, 1185178354Ssam "erp change: was 0x%x, now 0x%x", 1186178354Ssam ni->ni_erp, scan.erp); 1187178354Ssam if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) && 1188178354Ssam (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION)) 1189178354Ssam ic->ic_flags |= IEEE80211_F_USEPROT; 1190178354Ssam else 1191178354Ssam ic->ic_flags &= ~IEEE80211_F_USEPROT; 1192178354Ssam ni->ni_erp = scan.erp; 1193178354Ssam /* XXX statistic */ 1194178354Ssam /* XXX driver notification */ 1195178354Ssam } 1196178354Ssam if ((ni->ni_capinfo ^ scan.capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) { 1197178354Ssam IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC, 1198178354Ssam wh->i_addr2, 1199178354Ssam "capabilities change: was 0x%x, now 0x%x", 1200178354Ssam ni->ni_capinfo, scan.capinfo); 1201178354Ssam /* 1202178354Ssam * NB: we assume short preamble doesn't 1203178354Ssam * change dynamically 1204178354Ssam */ 1205178354Ssam ieee80211_set_shortslottime(ic, 1206178354Ssam IEEE80211_IS_CHAN_A(ic->ic_bsschan) || 1207178354Ssam (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)); 1208178354Ssam ni->ni_capinfo = (ni->ni_capinfo &~ IEEE80211_CAPINFO_SHORT_SLOTTIME) 1209178354Ssam | (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME); 1210178354Ssam /* XXX statistic */ 1211178354Ssam } 1212178354Ssam if (scan.wme != NULL && 1213178354Ssam (ni->ni_flags & IEEE80211_NODE_QOS) && 1214178354Ssam ieee80211_parse_wmeparams(vap, scan.wme, wh) > 0) 1215178354Ssam ieee80211_wme_updateparams(vap); 1216190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG 1217178354Ssam if (scan.ath != NULL) 1218178354Ssam ieee80211_parse_athparams(ni, scan.ath, wh); 1219190391Ssam#endif 1220183254Ssam if (scan.htcap != NULL && scan.htinfo != NULL && 1221183254Ssam (vap->iv_flags_ext & IEEE80211_FEXT_HT)) { 1222183254Ssam ieee80211_ht_updateparams(ni, 1223183254Ssam scan.htcap, scan.htinfo); 1224178354Ssam /* XXX state changes? */ 1225178354Ssam } 1226178354Ssam if (scan.tim != NULL) { 1227178354Ssam struct ieee80211_tim_ie *tim = 1228178354Ssam (struct ieee80211_tim_ie *) scan.tim; 1229178354Ssam#if 0 1230178354Ssam int aid = IEEE80211_AID(ni->ni_associd); 1231178354Ssam int ix = aid / NBBY; 1232178354Ssam int min = tim->tim_bitctl &~ 1; 1233178354Ssam int max = tim->tim_len + min - 4; 1234178354Ssam if ((tim->tim_bitctl&1) || 1235178354Ssam (min <= ix && ix <= max && 1236178354Ssam isset(tim->tim_bitmap - min, aid))) { 1237178354Ssam /* 1238178354Ssam * XXX Do not let bg scan kick off 1239178354Ssam * we are expecting data. 1240178354Ssam */ 1241178354Ssam ic->ic_lastdata = ticks; 1242178354Ssam ieee80211_sta_pwrsave(vap, 0); 1243178354Ssam } 1244178354Ssam#endif 1245178354Ssam ni->ni_dtim_count = tim->tim_count; 1246178354Ssam ni->ni_dtim_period = tim->tim_period; 1247178354Ssam } 1248178354Ssam /* 1249178354Ssam * If scanning, pass the info to the scan module. 1250178354Ssam * Otherwise, check if it's the right time to do 1251178354Ssam * a background scan. Background scanning must 1252178354Ssam * be enabled and we must not be operating in the 1253178354Ssam * turbo phase of dynamic turbo mode. Then, 1254178354Ssam * it's been a while since the last background 1255178354Ssam * scan and if no data frames have come through 1256178354Ssam * recently, kick off a scan. Note that this 1257178354Ssam * is the mechanism by which a background scan 1258178354Ssam * is started _and_ continued each time we 1259178354Ssam * return on-channel to receive a beacon from 1260178354Ssam * our ap. 1261178354Ssam */ 1262178354Ssam if (ic->ic_flags & IEEE80211_F_SCAN) { 1263178354Ssam ieee80211_add_scan(vap, &scan, wh, 1264192468Ssam subtype, rssi, nf); 1265178354Ssam } else if (contbgscan(vap)) { 1266178354Ssam ieee80211_bg_scan(vap, 0); 1267178354Ssam } else if (startbgscan(vap)) { 1268178354Ssam vap->iv_stats.is_scan_bg++; 1269178354Ssam#if 0 1270178354Ssam /* wakeup if we are sleeing */ 1271178354Ssam ieee80211_set_pwrsave(vap, 0); 1272178354Ssam#endif 1273178354Ssam ieee80211_bg_scan(vap, 0); 1274178354Ssam } 1275178354Ssam return; 1276178354Ssam } 1277178354Ssam /* 1278178354Ssam * If scanning, just pass information to the scan module. 1279178354Ssam */ 1280178354Ssam if (ic->ic_flags & IEEE80211_F_SCAN) { 1281178354Ssam if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) { 1282178354Ssam /* 1283178354Ssam * Actively scanning a channel marked passive; 1284178354Ssam * send a probe request now that we know there 1285178354Ssam * is 802.11 traffic present. 1286178354Ssam * 1287178354Ssam * XXX check if the beacon we recv'd gives 1288178354Ssam * us what we need and suppress the probe req 1289178354Ssam */ 1290178354Ssam ieee80211_probe_curchan(vap, 1); 1291178354Ssam ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN; 1292178354Ssam } 1293192468Ssam ieee80211_add_scan(vap, &scan, wh, subtype, rssi, nf); 1294178354Ssam return; 1295178354Ssam } 1296178354Ssam break; 1297178354Ssam } 1298178354Ssam 1299178354Ssam case IEEE80211_FC0_SUBTYPE_AUTH: { 1300178354Ssam uint16_t algo, seq, status; 1301178354Ssam /* 1302178354Ssam * auth frame format 1303178354Ssam * [2] algorithm 1304178354Ssam * [2] sequence 1305178354Ssam * [2] status 1306178354Ssam * [tlv*] challenge 1307178354Ssam */ 1308178354Ssam IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return); 1309178354Ssam algo = le16toh(*(uint16_t *)frm); 1310178354Ssam seq = le16toh(*(uint16_t *)(frm + 2)); 1311178354Ssam status = le16toh(*(uint16_t *)(frm + 4)); 1312178354Ssam IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2, 1313178354Ssam "recv auth frame with algorithm %d seq %d", algo, seq); 1314178354Ssam 1315178354Ssam if (vap->iv_flags & IEEE80211_F_COUNTERM) { 1316178354Ssam IEEE80211_DISCARD(vap, 1317178354Ssam IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO, 1318178354Ssam wh, "auth", "%s", "TKIP countermeasures enabled"); 1319178354Ssam vap->iv_stats.is_rx_auth_countermeasures++; 1320178354Ssam if (vap->iv_opmode == IEEE80211_M_HOSTAP) { 1321178354Ssam ieee80211_send_error(ni, wh->i_addr2, 1322178354Ssam IEEE80211_FC0_SUBTYPE_AUTH, 1323178354Ssam IEEE80211_REASON_MIC_FAILURE); 1324178354Ssam } 1325178354Ssam return; 1326178354Ssam } 1327178354Ssam if (algo == IEEE80211_AUTH_ALG_SHARED) 1328192468Ssam sta_auth_shared(ni, wh, frm + 6, efrm, rssi, nf, 1329192468Ssam seq, status); 1330178354Ssam else if (algo == IEEE80211_AUTH_ALG_OPEN) 1331192468Ssam sta_auth_open(ni, wh, rssi, nf, seq, status); 1332178354Ssam else { 1333178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 1334178354Ssam wh, "auth", "unsupported alg %d", algo); 1335178354Ssam vap->iv_stats.is_rx_auth_unsupported++; 1336178354Ssam return; 1337178354Ssam } 1338178354Ssam break; 1339178354Ssam } 1340178354Ssam 1341178354Ssam case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 1342178354Ssam case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: { 1343178354Ssam uint16_t capinfo, associd; 1344178354Ssam uint16_t status; 1345178354Ssam 1346178354Ssam if (vap->iv_state != IEEE80211_S_ASSOC) { 1347178354Ssam vap->iv_stats.is_rx_mgtdiscard++; 1348178354Ssam return; 1349178354Ssam } 1350178354Ssam 1351178354Ssam /* 1352178354Ssam * asresp frame format 1353178354Ssam * [2] capability information 1354178354Ssam * [2] status 1355178354Ssam * [2] association ID 1356178354Ssam * [tlv] supported rates 1357178354Ssam * [tlv] extended supported rates 1358178354Ssam * [tlv] WME 1359178354Ssam * [tlv] HT capabilities 1360178354Ssam * [tlv] HT info 1361178354Ssam */ 1362178354Ssam IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return); 1363178354Ssam ni = vap->iv_bss; 1364178354Ssam capinfo = le16toh(*(uint16_t *)frm); 1365178354Ssam frm += 2; 1366178354Ssam status = le16toh(*(uint16_t *)frm); 1367178354Ssam frm += 2; 1368178354Ssam if (status != 0) { 1369178354Ssam IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC, 1370178354Ssam wh->i_addr2, "%sassoc failed (reason %d)", 1371178354Ssam ISREASSOC(subtype) ? "re" : "", status); 1372178354Ssam vap->iv_stats.is_rx_auth_fail++; /* XXX */ 1373178354Ssam return; 1374178354Ssam } 1375178354Ssam associd = le16toh(*(uint16_t *)frm); 1376178354Ssam frm += 2; 1377178354Ssam 1378178354Ssam rates = xrates = wme = htcap = htinfo = NULL; 1379178354Ssam while (efrm - frm > 1) { 1380178354Ssam IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return); 1381178354Ssam switch (*frm) { 1382178354Ssam case IEEE80211_ELEMID_RATES: 1383178354Ssam rates = frm; 1384178354Ssam break; 1385178354Ssam case IEEE80211_ELEMID_XRATES: 1386178354Ssam xrates = frm; 1387178354Ssam break; 1388178354Ssam case IEEE80211_ELEMID_HTCAP: 1389178354Ssam htcap = frm; 1390178354Ssam break; 1391178354Ssam case IEEE80211_ELEMID_HTINFO: 1392178354Ssam htinfo = frm; 1393178354Ssam break; 1394178354Ssam case IEEE80211_ELEMID_VENDOR: 1395178354Ssam if (iswmeoui(frm)) 1396178354Ssam wme = frm; 1397178354Ssam else if (vap->iv_flags_ext & IEEE80211_FEXT_HTCOMPAT) { 1398178354Ssam /* 1399178354Ssam * Accept pre-draft HT ie's if the 1400178354Ssam * standard ones have not been seen. 1401178354Ssam */ 1402178354Ssam if (ishtcapoui(frm)) { 1403178354Ssam if (htcap == NULL) 1404178354Ssam htcap = frm; 1405178354Ssam } else if (ishtinfooui(frm)) { 1406178354Ssam if (htinfo == NULL) 1407178354Ssam htcap = frm; 1408178354Ssam } 1409178354Ssam } 1410178354Ssam /* XXX Atheros OUI support */ 1411178354Ssam break; 1412178354Ssam } 1413178354Ssam frm += frm[1] + 2; 1414178354Ssam } 1415178354Ssam 1416178354Ssam IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return); 1417178354Ssam if (xrates != NULL) 1418178354Ssam IEEE80211_VERIFY_ELEMENT(xrates, 1419178354Ssam IEEE80211_RATE_MAXSIZE - rates[1], return); 1420178354Ssam rate = ieee80211_setup_rates(ni, rates, xrates, 1421178354Ssam IEEE80211_F_JOIN | 1422178354Ssam IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE | 1423178354Ssam IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 1424178354Ssam if (rate & IEEE80211_RATE_BASIC) { 1425178354Ssam IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC, 1426178354Ssam wh->i_addr2, 1427178354Ssam "%sassoc failed (rate set mismatch)", 1428178354Ssam ISREASSOC(subtype) ? "re" : ""); 1429178354Ssam vap->iv_stats.is_rx_assoc_norate++; 1430178354Ssam ieee80211_new_state(vap, IEEE80211_S_SCAN, 1431178354Ssam IEEE80211_SCAN_FAIL_STATUS); 1432178354Ssam return; 1433178354Ssam } 1434178354Ssam 1435178354Ssam ni->ni_capinfo = capinfo; 1436178354Ssam ni->ni_associd = associd; 1437178354Ssam if (ni->ni_jointime == 0) 1438178354Ssam ni->ni_jointime = time_uptime; 1439178354Ssam if (wme != NULL && 1440178354Ssam ieee80211_parse_wmeparams(vap, wme, wh) >= 0) { 1441178354Ssam ni->ni_flags |= IEEE80211_NODE_QOS; 1442178354Ssam ieee80211_wme_updateparams(vap); 1443178354Ssam } else 1444178354Ssam ni->ni_flags &= ~IEEE80211_NODE_QOS; 1445178354Ssam /* 1446178354Ssam * Setup HT state according to the negotiation. 1447178354Ssam * 1448178354Ssam * NB: shouldn't need to check if HT use is enabled but some 1449178354Ssam * ap's send back HT ie's even when we don't indicate we 1450178354Ssam * are HT capable in our AssocReq. 1451178354Ssam */ 1452178354Ssam if (htcap != NULL && htinfo != NULL && 1453178354Ssam (vap->iv_flags_ext & IEEE80211_FEXT_HT)) { 1454183254Ssam ieee80211_ht_node_init(ni); 1455183254Ssam ieee80211_ht_updateparams(ni, htcap, htinfo); 1456178354Ssam ieee80211_setup_htrates(ni, htcap, 1457178354Ssam IEEE80211_F_JOIN | IEEE80211_F_DOBRS); 1458178354Ssam ieee80211_setup_basic_htrates(ni, htinfo); 1459190579Ssam } else { 1460190579Ssam#ifdef IEEE80211_SUPPORT_SUPERG 1461190579Ssam if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_ATH)) 1462190579Ssam ieee80211_ff_node_init(ni); 1463190579Ssam#endif 1464178354Ssam } 1465178354Ssam /* 1466178354Ssam * Configure state now that we are associated. 1467178354Ssam * 1468178354Ssam * XXX may need different/additional driver callbacks? 1469178354Ssam */ 1470178354Ssam if (IEEE80211_IS_CHAN_A(ic->ic_curchan) || 1471178354Ssam (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) { 1472178354Ssam ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 1473178354Ssam ic->ic_flags &= ~IEEE80211_F_USEBARKER; 1474178354Ssam } else { 1475178354Ssam ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE; 1476178354Ssam ic->ic_flags |= IEEE80211_F_USEBARKER; 1477178354Ssam } 1478178354Ssam ieee80211_set_shortslottime(ic, 1479178354Ssam IEEE80211_IS_CHAN_A(ic->ic_curchan) || 1480178354Ssam (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)); 1481178354Ssam /* 1482178354Ssam * Honor ERP protection. 1483178354Ssam * 1484178354Ssam * NB: ni_erp should zero for non-11g operation. 1485178354Ssam */ 1486178354Ssam if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) && 1487178354Ssam (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION)) 1488178354Ssam ic->ic_flags |= IEEE80211_F_USEPROT; 1489178354Ssam else 1490178354Ssam ic->ic_flags &= ~IEEE80211_F_USEPROT; 1491178354Ssam IEEE80211_NOTE_MAC(vap, 1492178354Ssam IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, wh->i_addr2, 1493183256Ssam "%sassoc success at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s", 1494178354Ssam ISREASSOC(subtype) ? "re" : "", 1495178354Ssam IEEE80211_NODE_AID(ni), 1496178354Ssam ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long", 1497178354Ssam ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long", 1498178354Ssam ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "", 1499178354Ssam ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "", 1500178354Ssam ni->ni_flags & IEEE80211_NODE_HT ? 1501182834Ssam (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "", 1502178354Ssam ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "", 1503183255Ssam ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" : 1504183255Ssam ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "", 1505183256Ssam ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "", 1506178354Ssam IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ? 1507178354Ssam ", fast-frames" : "", 1508178354Ssam IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ? 1509178354Ssam ", turbo" : "" 1510178354Ssam ); 1511178354Ssam ieee80211_new_state(vap, IEEE80211_S_RUN, subtype); 1512178354Ssam break; 1513178354Ssam } 1514178354Ssam 1515178354Ssam case IEEE80211_FC0_SUBTYPE_DEAUTH: { 1516178354Ssam uint16_t reason; 1517178354Ssam 1518178354Ssam if (vap->iv_state == IEEE80211_S_SCAN) { 1519178354Ssam vap->iv_stats.is_rx_mgtdiscard++; 1520178354Ssam return; 1521178354Ssam } 1522178354Ssam if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) { 1523178354Ssam /* NB: can happen when in promiscuous mode */ 1524178354Ssam vap->iv_stats.is_rx_mgtdiscard++; 1525178354Ssam break; 1526178354Ssam } 1527178354Ssam 1528178354Ssam /* 1529178354Ssam * deauth frame format 1530178354Ssam * [2] reason 1531178354Ssam */ 1532178354Ssam IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return); 1533178354Ssam reason = le16toh(*(uint16_t *)frm); 1534178354Ssam 1535178354Ssam vap->iv_stats.is_rx_deauth++; 1536178354Ssam vap->iv_stats.is_rx_deauth_code = reason; 1537178354Ssam IEEE80211_NODE_STAT(ni, rx_deauth); 1538178354Ssam 1539178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni, 1540178354Ssam "recv deauthenticate (reason %d)", reason); 1541178354Ssam ieee80211_new_state(vap, IEEE80211_S_AUTH, 1542178354Ssam (reason << 8) | IEEE80211_FC0_SUBTYPE_DEAUTH); 1543178354Ssam break; 1544178354Ssam } 1545178354Ssam 1546178354Ssam case IEEE80211_FC0_SUBTYPE_DISASSOC: { 1547178354Ssam uint16_t reason; 1548178354Ssam 1549178354Ssam if (vap->iv_state != IEEE80211_S_RUN && 1550178354Ssam vap->iv_state != IEEE80211_S_ASSOC && 1551178354Ssam vap->iv_state != IEEE80211_S_AUTH) { 1552178354Ssam vap->iv_stats.is_rx_mgtdiscard++; 1553178354Ssam return; 1554178354Ssam } 1555178354Ssam if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) { 1556178354Ssam /* NB: can happen when in promiscuous mode */ 1557178354Ssam vap->iv_stats.is_rx_mgtdiscard++; 1558178354Ssam break; 1559178354Ssam } 1560178354Ssam 1561178354Ssam /* 1562178354Ssam * disassoc frame format 1563178354Ssam * [2] reason 1564178354Ssam */ 1565178354Ssam IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return); 1566178354Ssam reason = le16toh(*(uint16_t *)frm); 1567178354Ssam 1568178354Ssam vap->iv_stats.is_rx_disassoc++; 1569178354Ssam vap->iv_stats.is_rx_disassoc_code = reason; 1570178354Ssam IEEE80211_NODE_STAT(ni, rx_disassoc); 1571178354Ssam 1572178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni, 1573178354Ssam "recv disassociate (reason %d)", reason); 1574178354Ssam ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0); 1575178354Ssam break; 1576178354Ssam } 1577178354Ssam 1578178354Ssam case IEEE80211_FC0_SUBTYPE_ACTION: 1579178354Ssam if (vap->iv_state == IEEE80211_S_RUN) { 1580178354Ssam if (ieee80211_parse_action(ni, m0) == 0) 1581178354Ssam ic->ic_recv_action(ni, frm, efrm); 1582178354Ssam } else 1583178354Ssam vap->iv_stats.is_rx_mgtdiscard++; 1584178354Ssam break; 1585178354Ssam 1586178354Ssam case IEEE80211_FC0_SUBTYPE_PROBE_REQ: 1587178354Ssam case IEEE80211_FC0_SUBTYPE_ASSOC_REQ: 1588178354Ssam case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: 1589178354Ssam vap->iv_stats.is_rx_mgtdiscard++; 1590178354Ssam return; 1591178354Ssam default: 1592178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 1593178354Ssam wh, "mgt", "subtype 0x%x not handled", subtype); 1594178354Ssam vap->iv_stats.is_rx_badsubtype++; 1595178354Ssam break; 1596178354Ssam } 1597178354Ssam#undef ISREASSOC 1598178354Ssam#undef ISPROBE 1599178354Ssam} 1600191546Ssam 1601191546Ssamstatic void 1602191546Ssamsta_recv_ctl(struct ieee80211_node *ni, struct mbuf *m0, int subtype) 1603191546Ssam{ 1604191546Ssam} 1605