1 /*- 2 * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include <sys/cdefs.h> 27 #ifdef __FreeBSD__ 28 __FBSDID("$FreeBSD$"); 29 #endif 30 31 /* 32 * IEEE 802.11 Station mode support. 33 */ 34 #include "opt_inet.h" 35 #include "opt_wlan.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/mbuf.h> 40 #include <sys/malloc.h> 41 #include <sys/kernel.h> 42 43 #include <sys/socket.h> 44 #include <sys/sockio.h> 45 #include <sys/endian.h> 46 #include <sys/errno.h> 47 #include <sys/proc.h> 48 #include <sys/sysctl.h> 49 50 #include <net/if.h> 51 #include <net/if_media.h> 52 #include <net/if_llc.h> 53 #include <net/ethernet.h> 54 55 #include <net/bpf.h> 56 57 #include <net80211/ieee80211_var.h> 58 #include <net80211/ieee80211_sta.h> 59 #include <net80211/ieee80211_input.h> 60 61 #define IEEE80211_RATE2MBS(r) (((r) & IEEE80211_RATE_VAL) / 2) 62 63 static void sta_vattach(struct ieee80211vap *); 64 static void sta_beacon_miss(struct ieee80211vap *); 65 static int sta_newstate(struct ieee80211vap *, enum ieee80211_state, int); 66 static int sta_input(struct ieee80211_node *, struct mbuf *, 67 int rssi, int noise, uint32_t rstamp); 68 static void sta_recv_mgmt(struct ieee80211_node *, struct mbuf *, 69 int subtype, int rssi, int noise, uint32_t rstamp); 70 71 void 72 ieee80211_sta_attach(struct ieee80211com *ic) 73 { 74 ic->ic_vattach[IEEE80211_M_STA] = sta_vattach; 75 } 76 77 void 78 ieee80211_sta_detach(struct ieee80211com *ic) 79 { 80 } 81 82 static void 83 sta_vdetach(struct ieee80211vap *vap) 84 { 85 } 86 87 static void 88 sta_vattach(struct ieee80211vap *vap) 89 { 90 vap->iv_newstate = sta_newstate; 91 vap->iv_input = sta_input; 92 vap->iv_recv_mgmt = sta_recv_mgmt; 93 vap->iv_opdetach = sta_vdetach; 94 vap->iv_bmiss = sta_beacon_miss; 95 } 96 97 /* 98 * Handle a beacon miss event. The common code filters out 99 * spurious events that can happen when scanning and/or before 100 * reaching RUN state. 101 */ 102 static void 103 sta_beacon_miss(struct ieee80211vap *vap) 104 { 105 struct ieee80211com *ic = vap->iv_ic; 106 107 KASSERT((ic->ic_flags & IEEE80211_F_SCAN) == 0, ("scanning")); 108 KASSERT(vap->iv_state == IEEE80211_S_RUN, 109 ("wrong state %d", vap->iv_state)); 110 111 IEEE80211_DPRINTF(vap, 112 IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG, 113 "beacon miss, mode %u state %s\n", 114 vap->iv_opmode, ieee80211_state_name[vap->iv_state]); 115 116 if (++vap->iv_bmiss_count < vap->iv_bmiss_max) { 117 /* 118 * Send a directed probe req before falling back to a 119 * scan; if we receive a response ic_bmiss_count will 120 * be reset. Some cards mistakenly report beacon miss 121 * so this avoids the expensive scan if the ap is 122 * still there. 123 */ 124 ieee80211_send_probereq(vap->iv_bss, vap->iv_myaddr, 125 vap->iv_bss->ni_bssid, vap->iv_bss->ni_bssid, 126 vap->iv_bss->ni_essid, vap->iv_bss->ni_esslen); 127 return; 128 } 129 vap->iv_bmiss_count = 0; 130 vap->iv_stats.is_beacon_miss++; 131 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) { 132 /* 133 * If we receive a beacon miss interrupt when using 134 * dynamic turbo, attempt to switch modes before 135 * reassociating. 136 */ 137 if (IEEE80211_ATH_CAP(vap, vap->iv_bss, IEEE80211_NODE_TURBOP)) 138 ieee80211_dturbo_switch(vap, 139 ic->ic_bsschan->ic_flags ^ IEEE80211_CHAN_TURBO); 140 /* 141 * Try to reassociate before scanning for a new ap. 142 */ 143 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1); 144 } else { 145 /* 146 * Somebody else is controlling state changes (e.g. 147 * a user-mode app) don't do anything that would 148 * confuse them; just drop into scan mode so they'll 149 * notified of the state change and given control. 150 */ 151 ieee80211_new_state(vap, IEEE80211_S_SCAN, 0); 152 } 153 } 154 155 /* 156 * Handle deauth with reason. We retry only for 157 * the cases where we might succeed. Otherwise 158 * we downgrade the ap and scan. 159 */ 160 static void 161 sta_authretry(struct ieee80211vap *vap, struct ieee80211_node *ni, int reason) 162 { 163 switch (reason) { 164 case IEEE80211_STATUS_SUCCESS: /* NB: MLME assoc */ 165 case IEEE80211_STATUS_TIMEOUT: 166 case IEEE80211_REASON_ASSOC_EXPIRE: 167 case IEEE80211_REASON_NOT_AUTHED: 168 case IEEE80211_REASON_NOT_ASSOCED: 169 case IEEE80211_REASON_ASSOC_LEAVE: 170 case IEEE80211_REASON_ASSOC_NOT_AUTHED: 171 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, 1); 172 break; 173 default: 174 ieee80211_scan_assoc_fail(vap, vap->iv_bss->ni_macaddr, reason); 175 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) 176 ieee80211_check_scan_current(vap); 177 break; 178 } 179 } 180 181 /* 182 * IEEE80211_M_STA vap state machine handler. 183 * This routine handles the main states in the 802.11 protocol. 184 */ 185 static int 186 sta_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 187 { 188 struct ieee80211com *ic = vap->iv_ic; 189 struct ieee80211_node *ni; 190 enum ieee80211_state ostate; 191 192 IEEE80211_LOCK_ASSERT(ic); 193 194 ostate = vap->iv_state; 195 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n", 196 __func__, ieee80211_state_name[ostate], 197 ieee80211_state_name[nstate], arg); 198 vap->iv_state = nstate; /* state transition */ 199 callout_stop(&vap->iv_mgtsend); /* XXX callout_drain */ 200 if (ostate != IEEE80211_S_SCAN) 201 ieee80211_cancel_scan(vap); /* background scan */ 202 ni = vap->iv_bss; /* NB: no reference held */ 203 if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) 204 callout_stop(&vap->iv_swbmiss); 205 switch (nstate) { 206 case IEEE80211_S_INIT: 207 switch (ostate) { 208 case IEEE80211_S_SLEEP: 209 /* XXX wakeup */ 210 case IEEE80211_S_RUN: 211 IEEE80211_SEND_MGMT(ni, 212 IEEE80211_FC0_SUBTYPE_DISASSOC, 213 IEEE80211_REASON_ASSOC_LEAVE); 214 ieee80211_sta_leave(ni); 215 break; 216 case IEEE80211_S_ASSOC: 217 IEEE80211_SEND_MGMT(ni, 218 IEEE80211_FC0_SUBTYPE_DEAUTH, 219 IEEE80211_REASON_AUTH_LEAVE); 220 break; 221 case IEEE80211_S_SCAN: 222 ieee80211_cancel_scan(vap); 223 break; 224 default: 225 goto invalid; 226 } 227 if (ostate != IEEE80211_S_INIT) { 228 /* NB: optimize INIT -> INIT case */ 229 ieee80211_reset_bss(vap); 230 } 231 if (vap->iv_auth->ia_detach != NULL) 232 vap->iv_auth->ia_detach(vap); 233 break; 234 case IEEE80211_S_SCAN: 235 switch (ostate) { 236 case IEEE80211_S_INIT: 237 /* 238 * Initiate a scan. We can come here as a result 239 * of an IEEE80211_IOC_SCAN_REQ too in which case 240 * the vap will be marked with IEEE80211_FEXT_SCANREQ 241 * and the scan request parameters will be present 242 * in iv_scanreq. Otherwise we do the default. 243 */ 244 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) { 245 ieee80211_check_scan(vap, 246 vap->iv_scanreq_flags, 247 vap->iv_scanreq_duration, 248 vap->iv_scanreq_mindwell, 249 vap->iv_scanreq_maxdwell, 250 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid); 251 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ; 252 } else 253 ieee80211_check_scan_current(vap); 254 break; 255 case IEEE80211_S_SCAN: 256 case IEEE80211_S_AUTH: 257 case IEEE80211_S_ASSOC: 258 /* 259 * These can happen either because of a timeout 260 * on an assoc/auth response or because of a 261 * change in state that requires a reset. For 262 * the former we're called with a non-zero arg 263 * that is the cause for the failure; pass this 264 * to the scan code so it can update state. 265 * Otherwise trigger a new scan unless we're in 266 * manual roaming mode in which case an application 267 * must issue an explicit scan request. 268 */ 269 if (arg != 0) 270 ieee80211_scan_assoc_fail(vap, 271 vap->iv_bss->ni_macaddr, arg); 272 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) 273 ieee80211_check_scan_current(vap); 274 break; 275 case IEEE80211_S_RUN: /* beacon miss */ 276 /* 277 * Beacon miss. Notify user space and if not 278 * under control of a user application (roaming 279 * manual) kick off a scan to re-connect. 280 */ 281 ieee80211_sta_leave(ni); 282 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) 283 ieee80211_check_scan_current(vap); 284 break; 285 default: 286 goto invalid; 287 } 288 break; 289 case IEEE80211_S_AUTH: 290 switch (ostate) { 291 case IEEE80211_S_INIT: 292 case IEEE80211_S_SCAN: 293 IEEE80211_SEND_MGMT(ni, 294 IEEE80211_FC0_SUBTYPE_AUTH, 1); 295 break; 296 case IEEE80211_S_AUTH: 297 case IEEE80211_S_ASSOC: 298 switch (arg & 0xff) { 299 case IEEE80211_FC0_SUBTYPE_AUTH: 300 /* ??? */ 301 IEEE80211_SEND_MGMT(ni, 302 IEEE80211_FC0_SUBTYPE_AUTH, 2); 303 break; 304 case IEEE80211_FC0_SUBTYPE_DEAUTH: 305 sta_authretry(vap, ni, arg>>8); 306 break; 307 } 308 break; 309 case IEEE80211_S_RUN: 310 switch (arg & 0xff) { 311 case IEEE80211_FC0_SUBTYPE_AUTH: 312 IEEE80211_SEND_MGMT(ni, 313 IEEE80211_FC0_SUBTYPE_AUTH, 2); 314 vap->iv_state = ostate; /* stay RUN */ 315 break; 316 case IEEE80211_FC0_SUBTYPE_DEAUTH: 317 ieee80211_sta_leave(ni); 318 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) { 319 /* try to reauth */ 320 IEEE80211_SEND_MGMT(ni, 321 IEEE80211_FC0_SUBTYPE_AUTH, 1); 322 } 323 break; 324 } 325 break; 326 default: 327 goto invalid; 328 } 329 break; 330 case IEEE80211_S_ASSOC: 331 switch (ostate) { 332 case IEEE80211_S_AUTH: 333 case IEEE80211_S_ASSOC: 334 IEEE80211_SEND_MGMT(ni, 335 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0); 336 break; 337 case IEEE80211_S_SLEEP: /* cannot happen */ 338 case IEEE80211_S_RUN: 339 ieee80211_sta_leave(ni); 340 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) { 341 IEEE80211_SEND_MGMT(ni, arg ? 342 IEEE80211_FC0_SUBTYPE_REASSOC_REQ : 343 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0); 344 } 345 break; 346 default: 347 goto invalid; 348 } 349 break; 350 case IEEE80211_S_RUN: 351 if (vap->iv_flags & IEEE80211_F_WPA) { 352 /* XXX validate prerequisites */ 353 } 354 switch (ostate) { 355 case IEEE80211_S_RUN: 356 break; 357 case IEEE80211_S_AUTH: /* when join is done in fw */ 358 case IEEE80211_S_ASSOC: 359 #ifdef IEEE80211_DEBUG 360 if (ieee80211_msg_debug(vap)) { 361 ieee80211_note(vap, "%s with %s ssid ", 362 (vap->iv_opmode == IEEE80211_M_STA ? 363 "associated" : "synchronized"), 364 ether_sprintf(ni->ni_bssid)); 365 ieee80211_print_essid(vap->iv_bss->ni_essid, 366 ni->ni_esslen); 367 /* XXX MCS/HT */ 368 printf(" channel %d start %uMb\n", 369 ieee80211_chan2ieee(ic, ic->ic_curchan), 370 IEEE80211_RATE2MBS(ni->ni_txrate)); 371 } 372 #endif 373 ieee80211_scan_assoc_success(vap, ni->ni_macaddr); 374 ieee80211_notify_node_join(ni, 375 arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP); 376 break; 377 case IEEE80211_S_SLEEP: 378 ieee80211_sta_pwrsave(vap, 0); 379 break; 380 default: 381 goto invalid; 382 } 383 ieee80211_sync_curchan(ic); 384 if (ostate != IEEE80211_S_RUN && 385 (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)) { 386 /* 387 * Start s/w beacon miss timer for devices w/o 388 * hardware support. We fudge a bit here since 389 * we're doing this in software. 390 */ 391 vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS( 392 2 * vap->iv_bmissthreshold * ni->ni_intval); 393 vap->iv_swbmiss_count = 0; 394 callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period, 395 ieee80211_swbmiss, vap); 396 } 397 /* 398 * When 802.1x is not in use mark the port authorized 399 * at this point so traffic can flow. 400 */ 401 if (ni->ni_authmode != IEEE80211_AUTH_8021X) 402 ieee80211_node_authorize(ni); 403 /* 404 * Fake association when joining an existing bss. 405 */ 406 if (ic->ic_newassoc != NULL) 407 ic->ic_newassoc(vap->iv_bss, ostate != IEEE80211_S_RUN); 408 break; 409 case IEEE80211_S_SLEEP: 410 ieee80211_sta_pwrsave(vap, 0); 411 break; 412 default: 413 invalid: 414 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, 415 "%s: unexpected state transition %s -> %s\n", __func__, 416 ieee80211_state_name[ostate], ieee80211_state_name[nstate]); 417 break; 418 } 419 return 0; 420 } 421 422 /* 423 * Return non-zero if the frame is an echo of a multicast 424 * frame sent by ourself. The dir is known to be DSTODS. 425 */ 426 static __inline int 427 isdstods_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh) 428 { 429 #define QWH4(wh) ((const struct ieee80211_qosframe_addr4 *)wh) 430 #define WH4(wh) ((const struct ieee80211_frame_addr4 *)wh) 431 const uint8_t *sa; 432 433 KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode")); 434 435 if (!IEEE80211_IS_MULTICAST(wh->i_addr3)) 436 return 0; 437 sa = IEEE80211_QOS_HAS_SEQ(wh) ? QWH4(wh)->i_addr4 : WH4(wh)->i_addr4; 438 return IEEE80211_ADDR_EQ(sa, vap->iv_myaddr); 439 #undef WH4 440 #undef QWH4 441 } 442 443 /* 444 * Return non-zero if the frame is an echo of a multicast 445 * frame sent by ourself. The dir is known to be FROMDS. 446 */ 447 static __inline int 448 isfromds_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh) 449 { 450 KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode")); 451 452 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) 453 return 0; 454 return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr); 455 } 456 457 /* 458 * Decide if a received management frame should be 459 * printed when debugging is enabled. This filters some 460 * of the less interesting frames that come frequently 461 * (e.g. beacons). 462 */ 463 static __inline int 464 doprint(struct ieee80211vap *vap, int subtype) 465 { 466 switch (subtype) { 467 case IEEE80211_FC0_SUBTYPE_BEACON: 468 return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN); 469 case IEEE80211_FC0_SUBTYPE_PROBE_REQ: 470 return 0; 471 } 472 return 1; 473 } 474 475 /* 476 * Process a received frame. The node associated with the sender 477 * should be supplied. If nothing was found in the node table then 478 * the caller is assumed to supply a reference to iv_bss instead. 479 * The RSSI and a timestamp are also supplied. The RSSI data is used 480 * during AP scanning to select a AP to associate with; it can have 481 * any units so long as values have consistent units and higher values 482 * mean ``better signal''. The receive timestamp is currently not used 483 * by the 802.11 layer. 484 */ 485 static int 486 sta_input(struct ieee80211_node *ni, struct mbuf *m, 487 int rssi, int noise, uint32_t rstamp) 488 { 489 #define SEQ_LEQ(a,b) ((int)((a)-(b)) <= 0) 490 #define HAS_SEQ(type) ((type & 0x4) == 0) 491 struct ieee80211vap *vap = ni->ni_vap; 492 struct ieee80211com *ic = ni->ni_ic; 493 struct ifnet *ifp = vap->iv_ifp; 494 struct ieee80211_frame *wh; 495 struct ieee80211_key *key; 496 struct ether_header *eh; 497 int hdrspace, need_tap; 498 uint8_t dir, type, subtype, qos; 499 uint8_t *bssid; 500 uint16_t rxseq; 501 502 if (m->m_flags & M_AMPDU_MPDU) { 503 /* 504 * Fastpath for A-MPDU reorder q resubmission. Frames 505 * w/ M_AMPDU_MPDU marked have already passed through 506 * here but were received out of order and been held on 507 * the reorder queue. When resubmitted they are marked 508 * with the M_AMPDU_MPDU flag and we can bypass most of 509 * the normal processing. 510 */ 511 wh = mtod(m, struct ieee80211_frame *); 512 type = IEEE80211_FC0_TYPE_DATA; 513 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 514 subtype = IEEE80211_FC0_SUBTYPE_QOS; 515 hdrspace = ieee80211_hdrspace(ic, wh); /* XXX optimize? */ 516 goto resubmit_ampdu; 517 } 518 519 KASSERT(ni != NULL, ("null node")); 520 ni->ni_inact = ni->ni_inact_reload; 521 522 need_tap = 1; /* mbuf need to be tapped. */ 523 type = -1; /* undefined */ 524 525 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) { 526 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 527 ni->ni_macaddr, NULL, 528 "too short (1): len %u", m->m_pkthdr.len); 529 vap->iv_stats.is_rx_tooshort++; 530 goto out; 531 } 532 /* 533 * Bit of a cheat here, we use a pointer for a 3-address 534 * frame format but don't reference fields past outside 535 * ieee80211_frame_min w/o first validating the data is 536 * present. 537 */ 538 wh = mtod(m, struct ieee80211_frame *); 539 540 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) != 541 IEEE80211_FC0_VERSION_0) { 542 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 543 ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]); 544 vap->iv_stats.is_rx_badversion++; 545 goto err; 546 } 547 548 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 549 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 550 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 551 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) { 552 bssid = wh->i_addr2; 553 if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) { 554 /* not interested in */ 555 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 556 bssid, NULL, "%s", "not to bss"); 557 vap->iv_stats.is_rx_wrongbss++; 558 goto out; 559 } 560 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi); 561 ni->ni_noise = noise; 562 ni->ni_rstamp = rstamp; 563 if (HAS_SEQ(type)) { 564 uint8_t tid = ieee80211_gettid(wh); 565 if (IEEE80211_QOS_HAS_SEQ(wh) && 566 TID_TO_WME_AC(tid) >= WME_AC_VI) 567 ic->ic_wme.wme_hipri_traffic++; 568 rxseq = le16toh(*(uint16_t *)wh->i_seq); 569 if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 && 570 (wh->i_fc[1] & IEEE80211_FC1_RETRY) && 571 SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) { 572 /* duplicate, discard */ 573 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 574 bssid, "duplicate", 575 "seqno <%u,%u> fragno <%u,%u> tid %u", 576 rxseq >> IEEE80211_SEQ_SEQ_SHIFT, 577 ni->ni_rxseqs[tid] >> 578 IEEE80211_SEQ_SEQ_SHIFT, 579 rxseq & IEEE80211_SEQ_FRAG_MASK, 580 ni->ni_rxseqs[tid] & 581 IEEE80211_SEQ_FRAG_MASK, 582 tid); 583 vap->iv_stats.is_rx_dup++; 584 IEEE80211_NODE_STAT(ni, rx_dup); 585 goto out; 586 } 587 ni->ni_rxseqs[tid] = rxseq; 588 } 589 } 590 591 switch (type) { 592 case IEEE80211_FC0_TYPE_DATA: 593 hdrspace = ieee80211_hdrspace(ic, wh); 594 if (m->m_len < hdrspace && 595 (m = m_pullup(m, hdrspace)) == NULL) { 596 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 597 ni->ni_macaddr, NULL, 598 "data too short: expecting %u", hdrspace); 599 vap->iv_stats.is_rx_tooshort++; 600 goto out; /* XXX */ 601 } 602 /* 603 * Handle A-MPDU re-ordering. If the frame is to be 604 * processed directly then ieee80211_ampdu_reorder 605 * will return 0; otherwise it has consumed the mbuf 606 * and we should do nothing more with it. 607 */ 608 if ((m->m_flags & M_AMPDU) && 609 (dir == IEEE80211_FC1_DIR_FROMDS || 610 dir == IEEE80211_FC1_DIR_DSTODS) && 611 ieee80211_ampdu_reorder(ni, m) != 0) { 612 m = NULL; 613 goto out; 614 } 615 resubmit_ampdu: 616 if (dir == IEEE80211_FC1_DIR_FROMDS) { 617 if ((ifp->if_flags & IFF_SIMPLEX) && 618 isfromds_mcastecho(vap, wh)) { 619 /* 620 * In IEEE802.11 network, multicast 621 * packets sent from "me" are broadcast 622 * from the AP; silently discard for 623 * SIMPLEX interface. 624 */ 625 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 626 wh, "data", "%s", "multicast echo"); 627 vap->iv_stats.is_rx_mcastecho++; 628 goto out; 629 } 630 if ((vap->iv_flags & IEEE80211_F_DWDS) && 631 IEEE80211_IS_MULTICAST(wh->i_addr1)) { 632 /* 633 * DWDS sta's must drop 3-address mcast frames 634 * as they will be sent separately as a 4-addr 635 * frame. Accepting the 3-addr frame will 636 * confuse the bridge into thinking the sending 637 * sta is located at the end of WDS link. 638 */ 639 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh, 640 "3-address data", "%s", "DWDS enabled"); 641 vap->iv_stats.is_rx_mcastecho++; 642 goto out; 643 } 644 } else if (dir == IEEE80211_FC1_DIR_DSTODS) { 645 if ((vap->iv_flags & IEEE80211_F_DWDS) == 0) { 646 IEEE80211_DISCARD(vap, 647 IEEE80211_MSG_INPUT, wh, "4-address data", 648 "%s", "DWDS not enabled"); 649 vap->iv_stats.is_rx_wrongdir++; 650 goto out; 651 } 652 if ((ifp->if_flags & IFF_SIMPLEX) && 653 isdstods_mcastecho(vap, wh)) { 654 /* 655 * In IEEE802.11 network, multicast 656 * packets sent from "me" are broadcast 657 * from the AP; silently discard for 658 * SIMPLEX interface. 659 */ 660 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh, 661 "4-address data", "%s", "multicast echo"); 662 vap->iv_stats.is_rx_mcastecho++; 663 goto out; 664 } 665 } else { 666 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh, 667 "data", "incorrect dir 0x%x", dir); 668 vap->iv_stats.is_rx_wrongdir++; 669 goto out; 670 } 671 672 /* 673 * Handle privacy requirements. Note that we 674 * must not be preempted from here until after 675 * we (potentially) call ieee80211_crypto_demic; 676 * otherwise we may violate assumptions in the 677 * crypto cipher modules used to do delayed update 678 * of replay sequence numbers. 679 */ 680 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 681 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) { 682 /* 683 * Discard encrypted frames when privacy is off. 684 */ 685 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 686 wh, "WEP", "%s", "PRIVACY off"); 687 vap->iv_stats.is_rx_noprivacy++; 688 IEEE80211_NODE_STAT(ni, rx_noprivacy); 689 goto out; 690 } 691 key = ieee80211_crypto_decap(ni, m, hdrspace); 692 if (key == NULL) { 693 /* NB: stats+msgs handled in crypto_decap */ 694 IEEE80211_NODE_STAT(ni, rx_wepfail); 695 goto out; 696 } 697 wh = mtod(m, struct ieee80211_frame *); 698 wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 699 } else { 700 /* XXX M_WEP and IEEE80211_F_PRIVACY */ 701 key = NULL; 702 } 703 704 /* 705 * Save QoS bits for use below--before we strip the header. 706 */ 707 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) { 708 qos = (dir == IEEE80211_FC1_DIR_DSTODS) ? 709 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] : 710 ((struct ieee80211_qosframe *)wh)->i_qos[0]; 711 } else 712 qos = 0; 713 714 /* 715 * Next up, any fragmentation. 716 */ 717 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 718 m = ieee80211_defrag(ni, m, hdrspace); 719 if (m == NULL) { 720 /* Fragment dropped or frame not complete yet */ 721 goto out; 722 } 723 } 724 wh = NULL; /* no longer valid, catch any uses */ 725 726 /* 727 * Next strip any MSDU crypto bits. 728 */ 729 if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) { 730 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 731 ni->ni_macaddr, "data", "%s", "demic error"); 732 vap->iv_stats.is_rx_demicfail++; 733 IEEE80211_NODE_STAT(ni, rx_demicfail); 734 goto out; 735 } 736 737 /* copy to listener after decrypt */ 738 if (bpf_peers_present(vap->iv_rawbpf)) 739 bpf_mtap(vap->iv_rawbpf, m); 740 need_tap = 0; 741 742 /* 743 * Finally, strip the 802.11 header. 744 */ 745 m = ieee80211_decap(vap, m, hdrspace); 746 if (m == NULL) { 747 /* XXX mask bit to check for both */ 748 /* don't count Null data frames as errors */ 749 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA || 750 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL) 751 goto out; 752 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 753 ni->ni_macaddr, "data", "%s", "decap error"); 754 vap->iv_stats.is_rx_decap++; 755 IEEE80211_NODE_STAT(ni, rx_decap); 756 goto err; 757 } 758 eh = mtod(m, struct ether_header *); 759 if (!ieee80211_node_is_authorized(ni)) { 760 /* 761 * Deny any non-PAE frames received prior to 762 * authorization. For open/shared-key 763 * authentication the port is mark authorized 764 * after authentication completes. For 802.1x 765 * the port is not marked authorized by the 766 * authenticator until the handshake has completed. 767 */ 768 if (eh->ether_type != htons(ETHERTYPE_PAE)) { 769 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 770 eh->ether_shost, "data", 771 "unauthorized port: ether type 0x%x len %u", 772 eh->ether_type, m->m_pkthdr.len); 773 vap->iv_stats.is_rx_unauth++; 774 IEEE80211_NODE_STAT(ni, rx_unauth); 775 goto err; 776 } 777 } else { 778 /* 779 * When denying unencrypted frames, discard 780 * any non-PAE frames received without encryption. 781 */ 782 if ((vap->iv_flags & IEEE80211_F_DROPUNENC) && 783 (key == NULL && (m->m_flags & M_WEP) == 0) && 784 eh->ether_type != htons(ETHERTYPE_PAE)) { 785 /* 786 * Drop unencrypted frames. 787 */ 788 vap->iv_stats.is_rx_unencrypted++; 789 IEEE80211_NODE_STAT(ni, rx_unencrypted); 790 goto out; 791 } 792 } 793 /* XXX require HT? */ 794 if (qos & IEEE80211_QOS_AMSDU) { 795 m = ieee80211_decap_amsdu(ni, m); 796 if (m == NULL) 797 return IEEE80211_FC0_TYPE_DATA; 798 } else if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) && 799 #define FF_LLC_SIZE (sizeof(struct ether_header) + sizeof(struct llc)) 800 m->m_pkthdr.len >= 3*FF_LLC_SIZE) { 801 struct llc *llc; 802 803 /* 804 * Check for fast-frame tunnel encapsulation. 805 */ 806 if (m->m_len < FF_LLC_SIZE && 807 (m = m_pullup(m, FF_LLC_SIZE)) == NULL) { 808 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 809 ni->ni_macaddr, "fast-frame", 810 "%s", "m_pullup(llc) failed"); 811 vap->iv_stats.is_rx_tooshort++; 812 return IEEE80211_FC0_TYPE_DATA; 813 } 814 llc = (struct llc *)(mtod(m, uint8_t *) + 815 sizeof(struct ether_header)); 816 if (llc->llc_snap.ether_type == htons(ATH_FF_ETH_TYPE)) { 817 m_adj(m, FF_LLC_SIZE); 818 m = ieee80211_decap_fastframe(ni, m); 819 if (m == NULL) 820 return IEEE80211_FC0_TYPE_DATA; 821 } 822 } 823 #undef FF_LLC_SIZE 824 ieee80211_deliver_data(vap, ni, m); 825 return IEEE80211_FC0_TYPE_DATA; 826 827 case IEEE80211_FC0_TYPE_MGT: 828 vap->iv_stats.is_rx_mgmt++; 829 IEEE80211_NODE_STAT(ni, rx_mgmt); 830 if (dir != IEEE80211_FC1_DIR_NODS) { 831 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 832 wh, "data", "incorrect dir 0x%x", dir); 833 vap->iv_stats.is_rx_wrongdir++; 834 goto err; 835 } 836 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) { 837 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 838 ni->ni_macaddr, "mgt", "too short: len %u", 839 m->m_pkthdr.len); 840 vap->iv_stats.is_rx_tooshort++; 841 goto out; 842 } 843 #ifdef IEEE80211_DEBUG 844 if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) || 845 ieee80211_msg_dumppkts(vap)) { 846 if_printf(ifp, "received %s from %s rssi %d\n", 847 ieee80211_mgt_subtype_name[subtype >> 848 IEEE80211_FC0_SUBTYPE_SHIFT], 849 ether_sprintf(wh->i_addr2), rssi); 850 } 851 #endif 852 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 853 if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) { 854 /* 855 * Only shared key auth frames with a challenge 856 * should be encrypted, discard all others. 857 */ 858 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 859 wh, ieee80211_mgt_subtype_name[subtype >> 860 IEEE80211_FC0_SUBTYPE_SHIFT], 861 "%s", "WEP set but not permitted"); 862 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */ 863 goto out; 864 } 865 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) { 866 /* 867 * Discard encrypted frames when privacy is off. 868 */ 869 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 870 wh, "mgt", "%s", "WEP set but PRIVACY off"); 871 vap->iv_stats.is_rx_noprivacy++; 872 goto out; 873 } 874 hdrspace = ieee80211_hdrspace(ic, wh); 875 key = ieee80211_crypto_decap(ni, m, hdrspace); 876 if (key == NULL) { 877 /* NB: stats+msgs handled in crypto_decap */ 878 goto out; 879 } 880 wh = mtod(m, struct ieee80211_frame *); 881 wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 882 } 883 if (bpf_peers_present(vap->iv_rawbpf)) 884 bpf_mtap(vap->iv_rawbpf, m); 885 vap->iv_recv_mgmt(ni, m, subtype, rssi, noise, rstamp); 886 m_freem(m); 887 return IEEE80211_FC0_TYPE_MGT; 888 889 case IEEE80211_FC0_TYPE_CTL: 890 vap->iv_stats.is_rx_ctl++; 891 IEEE80211_NODE_STAT(ni, rx_ctrl); 892 goto out; 893 default: 894 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 895 wh, NULL, "bad frame type 0x%x", type); 896 /* should not come here */ 897 break; 898 } 899 err: 900 ifp->if_ierrors++; 901 out: 902 if (m != NULL) { 903 if (bpf_peers_present(vap->iv_rawbpf) && need_tap) 904 bpf_mtap(vap->iv_rawbpf, m); 905 m_freem(m); 906 } 907 return type; 908 #undef SEQ_LEQ 909 } 910 911 static void 912 sta_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh, 913 int rssi, int noise, uint32_t rstamp, uint16_t seq, uint16_t status) 914 { 915 struct ieee80211vap *vap = ni->ni_vap; 916 917 if (ni->ni_authmode == IEEE80211_AUTH_SHARED) { 918 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH, 919 ni->ni_macaddr, "open auth", 920 "bad sta auth mode %u", ni->ni_authmode); 921 vap->iv_stats.is_rx_bad_auth++; /* XXX */ 922 return; 923 } 924 if (vap->iv_state != IEEE80211_S_AUTH || 925 seq != IEEE80211_AUTH_OPEN_RESPONSE) { 926 vap->iv_stats.is_rx_bad_auth++; 927 return; 928 } 929 if (status != 0) { 930 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, 931 ni, "open auth failed (reason %d)", status); 932 vap->iv_stats.is_rx_auth_fail++; 933 vap->iv_stats.is_rx_authfail_code = status; 934 ieee80211_new_state(vap, IEEE80211_S_SCAN, 935 IEEE80211_SCAN_FAIL_STATUS); 936 } else 937 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0); 938 } 939 940 static void 941 sta_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh, 942 uint8_t *frm, uint8_t *efrm, int rssi, int noise, uint32_t rstamp, 943 uint16_t seq, uint16_t status) 944 { 945 struct ieee80211vap *vap = ni->ni_vap; 946 uint8_t *challenge; 947 int estatus; 948 949 /* 950 * NB: this can happen as we allow pre-shared key 951 * authentication to be enabled w/o wep being turned 952 * on so that configuration of these can be done 953 * in any order. It may be better to enforce the 954 * ordering in which case this check would just be 955 * for sanity/consistency. 956 */ 957 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) { 958 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH, 959 ni->ni_macaddr, "shared key auth", 960 "%s", " PRIVACY is disabled"); 961 estatus = IEEE80211_STATUS_ALG; 962 goto bad; 963 } 964 /* 965 * Pre-shared key authentication is evil; accept 966 * it only if explicitly configured (it is supported 967 * mainly for compatibility with clients like OS X). 968 */ 969 if (ni->ni_authmode != IEEE80211_AUTH_AUTO && 970 ni->ni_authmode != IEEE80211_AUTH_SHARED) { 971 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH, 972 ni->ni_macaddr, "shared key auth", 973 "bad sta auth mode %u", ni->ni_authmode); 974 vap->iv_stats.is_rx_bad_auth++; /* XXX maybe a unique error? */ 975 estatus = IEEE80211_STATUS_ALG; 976 goto bad; 977 } 978 979 challenge = NULL; 980 if (frm + 1 < efrm) { 981 if ((frm[1] + 2) > (efrm - frm)) { 982 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH, 983 ni->ni_macaddr, "shared key auth", 984 "ie %d/%d too long", 985 frm[0], (frm[1] + 2) - (efrm - frm)); 986 vap->iv_stats.is_rx_bad_auth++; 987 estatus = IEEE80211_STATUS_CHALLENGE; 988 goto bad; 989 } 990 if (*frm == IEEE80211_ELEMID_CHALLENGE) 991 challenge = frm; 992 frm += frm[1] + 2; 993 } 994 switch (seq) { 995 case IEEE80211_AUTH_SHARED_CHALLENGE: 996 case IEEE80211_AUTH_SHARED_RESPONSE: 997 if (challenge == NULL) { 998 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH, 999 ni->ni_macaddr, "shared key auth", 1000 "%s", "no challenge"); 1001 vap->iv_stats.is_rx_bad_auth++; 1002 estatus = IEEE80211_STATUS_CHALLENGE; 1003 goto bad; 1004 } 1005 if (challenge[1] != IEEE80211_CHALLENGE_LEN) { 1006 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH, 1007 ni->ni_macaddr, "shared key auth", 1008 "bad challenge len %d", challenge[1]); 1009 vap->iv_stats.is_rx_bad_auth++; 1010 estatus = IEEE80211_STATUS_CHALLENGE; 1011 goto bad; 1012 } 1013 default: 1014 break; 1015 } 1016 if (vap->iv_state != IEEE80211_S_AUTH) 1017 return; 1018 switch (seq) { 1019 case IEEE80211_AUTH_SHARED_PASS: 1020 if (ni->ni_challenge != NULL) { 1021 free(ni->ni_challenge, M_80211_NODE); 1022 ni->ni_challenge = NULL; 1023 } 1024 if (status != 0) { 1025 IEEE80211_NOTE_FRAME(vap, 1026 IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, wh, 1027 "shared key auth failed (reason %d)", status); 1028 vap->iv_stats.is_rx_auth_fail++; 1029 vap->iv_stats.is_rx_authfail_code = status; 1030 return; 1031 } 1032 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0); 1033 break; 1034 case IEEE80211_AUTH_SHARED_CHALLENGE: 1035 if (!ieee80211_alloc_challenge(ni)) 1036 return; 1037 /* XXX could optimize by passing recvd challenge */ 1038 memcpy(ni->ni_challenge, &challenge[2], challenge[1]); 1039 IEEE80211_SEND_MGMT(ni, 1040 IEEE80211_FC0_SUBTYPE_AUTH, seq + 1); 1041 break; 1042 default: 1043 IEEE80211_DISCARD(vap, IEEE80211_MSG_AUTH, 1044 wh, "shared key auth", "bad seq %d", seq); 1045 vap->iv_stats.is_rx_bad_auth++; 1046 return; 1047 } 1048 return; 1049 bad: 1050 /* 1051 * Kick the state machine. This short-circuits 1052 * using the mgt frame timeout to trigger the 1053 * state transition. 1054 */ 1055 if (vap->iv_state == IEEE80211_S_AUTH) 1056 ieee80211_new_state(vap, IEEE80211_S_SCAN, 1057 IEEE80211_SCAN_FAIL_STATUS); 1058 } 1059 1060 static int 1061 ieee80211_parse_wmeparams(struct ieee80211vap *vap, uint8_t *frm, 1062 const struct ieee80211_frame *wh) 1063 { 1064 #define MS(_v, _f) (((_v) & _f) >> _f##_S) 1065 struct ieee80211_wme_state *wme = &vap->iv_ic->ic_wme; 1066 u_int len = frm[1], qosinfo; 1067 int i; 1068 1069 if (len < sizeof(struct ieee80211_wme_param)-2) { 1070 IEEE80211_DISCARD_IE(vap, 1071 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME, 1072 wh, "WME", "too short, len %u", len); 1073 return -1; 1074 } 1075 qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)]; 1076 qosinfo &= WME_QOSINFO_COUNT; 1077 /* XXX do proper check for wraparound */ 1078 if (qosinfo == wme->wme_wmeChanParams.cap_info) 1079 return 0; 1080 frm += __offsetof(struct ieee80211_wme_param, params_acParams); 1081 for (i = 0; i < WME_NUM_AC; i++) { 1082 struct wmeParams *wmep = 1083 &wme->wme_wmeChanParams.cap_wmeParams[i]; 1084 /* NB: ACI not used */ 1085 wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM); 1086 wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN); 1087 wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN); 1088 wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX); 1089 wmep->wmep_txopLimit = LE_READ_2(frm+2); 1090 frm += 4; 1091 } 1092 wme->wme_wmeChanParams.cap_info = qosinfo; 1093 return 1; 1094 #undef MS 1095 } 1096 1097 static int 1098 ieee80211_parse_athparams(struct ieee80211_node *ni, uint8_t *frm, 1099 const struct ieee80211_frame *wh) 1100 { 1101 struct ieee80211vap *vap = ni->ni_vap; 1102 const struct ieee80211_ath_ie *ath; 1103 u_int len = frm[1]; 1104 int capschanged; 1105 uint16_t defkeyix; 1106 1107 if (len < sizeof(struct ieee80211_ath_ie)-2) { 1108 IEEE80211_DISCARD_IE(vap, 1109 IEEE80211_MSG_ELEMID | IEEE80211_MSG_SUPERG, 1110 wh, "Atheros", "too short, len %u", len); 1111 return -1; 1112 } 1113 ath = (const struct ieee80211_ath_ie *)frm; 1114 capschanged = (ni->ni_ath_flags != ath->ath_capability); 1115 defkeyix = LE_READ_2(ath->ath_defkeyix); 1116 if (capschanged || defkeyix != ni->ni_ath_defkeyix) { 1117 ni->ni_ath_flags = ath->ath_capability; 1118 ni->ni_ath_defkeyix = defkeyix; 1119 IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni, 1120 "ath ie change: new caps 0x%x defkeyix 0x%x", 1121 ni->ni_ath_flags, ni->ni_ath_defkeyix); 1122 } 1123 if (IEEE80211_ATH_CAP(vap, ni, ATHEROS_CAP_TURBO_PRIME)) { 1124 uint16_t curflags, newflags; 1125 1126 /* 1127 * Check for turbo mode switch. Calculate flags 1128 * for the new mode and effect the switch. 1129 */ 1130 newflags = curflags = vap->iv_ic->ic_bsschan->ic_flags; 1131 /* NB: BOOST is not in ic_flags, so get it from the ie */ 1132 if (ath->ath_capability & ATHEROS_CAP_BOOST) 1133 newflags |= IEEE80211_CHAN_TURBO; 1134 else 1135 newflags &= ~IEEE80211_CHAN_TURBO; 1136 if (newflags != curflags) 1137 ieee80211_dturbo_switch(vap, newflags); 1138 } 1139 return capschanged; 1140 } 1141 1142 /* 1143 * Return non-zero if a background scan may be continued: 1144 * o bg scan is active 1145 * o no channel switch is pending 1146 * o there has not been any traffic recently 1147 * 1148 * Note we do not check if there is an administrative enable; 1149 * this is only done to start the scan. We assume that any 1150 * change in state will be accompanied by a request to cancel 1151 * active scans which will otherwise cause this test to fail. 1152 */ 1153 static __inline int 1154 contbgscan(struct ieee80211vap *vap) 1155 { 1156 struct ieee80211com *ic = vap->iv_ic; 1157 1158 return ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) && 1159 (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 && 1160 vap->iv_state == IEEE80211_S_RUN && /* XXX? */ 1161 time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle)); 1162 } 1163 1164 /* 1165 * Return non-zero if a backgrond scan may be started: 1166 * o bg scanning is administratively enabled 1167 * o no channel switch is pending 1168 * o we are not boosted on a dynamic turbo channel 1169 * o there has not been a scan recently 1170 * o there has not been any traffic recently 1171 */ 1172 static __inline int 1173 startbgscan(struct ieee80211vap *vap) 1174 { 1175 struct ieee80211com *ic = vap->iv_ic; 1176 1177 return ((vap->iv_flags & IEEE80211_F_BGSCAN) && 1178 (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 && 1179 !IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) && 1180 time_after(ticks, ic->ic_lastscan + vap->iv_bgscanintvl) && 1181 time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle)); 1182 } 1183 1184 static void 1185 sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, 1186 int subtype, int rssi, int noise, uint32_t rstamp) 1187 { 1188 #define ISPROBE(_st) ((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP) 1189 #define ISREASSOC(_st) ((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP) 1190 struct ieee80211vap *vap = ni->ni_vap; 1191 struct ieee80211com *ic = ni->ni_ic; 1192 struct ieee80211_frame *wh; 1193 uint8_t *frm, *efrm; 1194 uint8_t *rates, *xrates, *wme, *htcap, *htinfo; 1195 uint8_t rate; 1196 1197 wh = mtod(m0, struct ieee80211_frame *); 1198 frm = (uint8_t *)&wh[1]; 1199 efrm = mtod(m0, uint8_t *) + m0->m_len; 1200 switch (subtype) { 1201 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 1202 case IEEE80211_FC0_SUBTYPE_BEACON: { 1203 struct ieee80211_scanparams scan; 1204 /* 1205 * We process beacon/probe response frames: 1206 * o when scanning, or 1207 * o station mode when associated (to collect state 1208 * updates such as 802.11g slot time), or 1209 * Frames otherwise received are discarded. 1210 */ 1211 if (!((ic->ic_flags & IEEE80211_F_SCAN) || ni->ni_associd)) { 1212 vap->iv_stats.is_rx_mgtdiscard++; 1213 return; 1214 } 1215 /* XXX probe response in sta mode when !scanning? */ 1216 if (ieee80211_parse_beacon(ni, m0, &scan) != 0) 1217 return; 1218 /* 1219 * Count frame now that we know it's to be processed. 1220 */ 1221 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) { 1222 vap->iv_stats.is_rx_beacon++; /* XXX remove */ 1223 IEEE80211_NODE_STAT(ni, rx_beacons); 1224 } else 1225 IEEE80211_NODE_STAT(ni, rx_proberesp); 1226 /* 1227 * When operating in station mode, check for state updates. 1228 * Be careful to ignore beacons received while doing a 1229 * background scan. We consider only 11g/WMM stuff right now. 1230 */ 1231 if (ni->ni_associd != 0 && 1232 ((ic->ic_flags & IEEE80211_F_SCAN) == 0 || 1233 IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) { 1234 /* record tsf of last beacon */ 1235 memcpy(ni->ni_tstamp.data, scan.tstamp, 1236 sizeof(ni->ni_tstamp)); 1237 /* count beacon frame for s/w bmiss handling */ 1238 vap->iv_swbmiss_count++; 1239 vap->iv_bmiss_count = 0; 1240 if (ni->ni_erp != scan.erp) { 1241 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC, 1242 wh->i_addr2, 1243 "erp change: was 0x%x, now 0x%x", 1244 ni->ni_erp, scan.erp); 1245 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) && 1246 (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION)) 1247 ic->ic_flags |= IEEE80211_F_USEPROT; 1248 else 1249 ic->ic_flags &= ~IEEE80211_F_USEPROT; 1250 ni->ni_erp = scan.erp; 1251 /* XXX statistic */ 1252 /* XXX driver notification */ 1253 } 1254 if ((ni->ni_capinfo ^ scan.capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) { 1255 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC, 1256 wh->i_addr2, 1257 "capabilities change: was 0x%x, now 0x%x", 1258 ni->ni_capinfo, scan.capinfo); 1259 /* 1260 * NB: we assume short preamble doesn't 1261 * change dynamically 1262 */ 1263 ieee80211_set_shortslottime(ic, 1264 IEEE80211_IS_CHAN_A(ic->ic_bsschan) || 1265 (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)); 1266 ni->ni_capinfo = (ni->ni_capinfo &~ IEEE80211_CAPINFO_SHORT_SLOTTIME) 1267 | (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME); 1268 /* XXX statistic */ 1269 } 1270 if (scan.wme != NULL && 1271 (ni->ni_flags & IEEE80211_NODE_QOS) && 1272 ieee80211_parse_wmeparams(vap, scan.wme, wh) > 0) 1273 ieee80211_wme_updateparams(vap); 1274 if (scan.ath != NULL) 1275 ieee80211_parse_athparams(ni, scan.ath, wh); 1276 if (scan.htcap != NULL && scan.htinfo != NULL && 1277 (vap->iv_flags_ext & IEEE80211_FEXT_HT)) { 1278 ieee80211_ht_updateparams(ni, 1279 scan.htcap, scan.htinfo); 1280 /* XXX state changes? */ 1281 } 1282 if (scan.tim != NULL) { 1283 struct ieee80211_tim_ie *tim = 1284 (struct ieee80211_tim_ie *) scan.tim; 1285 #if 0 1286 int aid = IEEE80211_AID(ni->ni_associd); 1287 int ix = aid / NBBY; 1288 int min = tim->tim_bitctl &~ 1; 1289 int max = tim->tim_len + min - 4; 1290 if ((tim->tim_bitctl&1) || 1291 (min <= ix && ix <= max && 1292 isset(tim->tim_bitmap - min, aid))) { 1293 /* 1294 * XXX Do not let bg scan kick off 1295 * we are expecting data. 1296 */ 1297 ic->ic_lastdata = ticks; 1298 ieee80211_sta_pwrsave(vap, 0); 1299 } 1300 #endif 1301 ni->ni_dtim_count = tim->tim_count; 1302 ni->ni_dtim_period = tim->tim_period; 1303 } 1304 /* 1305 * If scanning, pass the info to the scan module. 1306 * Otherwise, check if it's the right time to do 1307 * a background scan. Background scanning must 1308 * be enabled and we must not be operating in the 1309 * turbo phase of dynamic turbo mode. Then, 1310 * it's been a while since the last background 1311 * scan and if no data frames have come through 1312 * recently, kick off a scan. Note that this 1313 * is the mechanism by which a background scan 1314 * is started _and_ continued each time we 1315 * return on-channel to receive a beacon from 1316 * our ap. 1317 */ 1318 if (ic->ic_flags & IEEE80211_F_SCAN) { 1319 ieee80211_add_scan(vap, &scan, wh, 1320 subtype, rssi, noise, rstamp); 1321 } else if (contbgscan(vap)) { 1322 ieee80211_bg_scan(vap, 0); 1323 } else if (startbgscan(vap)) { 1324 vap->iv_stats.is_scan_bg++; 1325 #if 0 1326 /* wakeup if we are sleeing */ 1327 ieee80211_set_pwrsave(vap, 0); 1328 #endif 1329 ieee80211_bg_scan(vap, 0); 1330 } 1331 return; 1332 } 1333 /* 1334 * If scanning, just pass information to the scan module. 1335 */ 1336 if (ic->ic_flags & IEEE80211_F_SCAN) { 1337 if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) { 1338 /* 1339 * Actively scanning a channel marked passive; 1340 * send a probe request now that we know there 1341 * is 802.11 traffic present. 1342 * 1343 * XXX check if the beacon we recv'd gives 1344 * us what we need and suppress the probe req 1345 */ 1346 ieee80211_probe_curchan(vap, 1); 1347 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN; 1348 } 1349 ieee80211_add_scan(vap, &scan, wh, 1350 subtype, rssi, noise, rstamp); 1351 return; 1352 } 1353 break; 1354 } 1355 1356 case IEEE80211_FC0_SUBTYPE_AUTH: { 1357 uint16_t algo, seq, status; 1358 /* 1359 * auth frame format 1360 * [2] algorithm 1361 * [2] sequence 1362 * [2] status 1363 * [tlv*] challenge 1364 */ 1365 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return); 1366 algo = le16toh(*(uint16_t *)frm); 1367 seq = le16toh(*(uint16_t *)(frm + 2)); 1368 status = le16toh(*(uint16_t *)(frm + 4)); 1369 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2, 1370 "recv auth frame with algorithm %d seq %d", algo, seq); 1371 1372 if (vap->iv_flags & IEEE80211_F_COUNTERM) { 1373 IEEE80211_DISCARD(vap, 1374 IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO, 1375 wh, "auth", "%s", "TKIP countermeasures enabled"); 1376 vap->iv_stats.is_rx_auth_countermeasures++; 1377 if (vap->iv_opmode == IEEE80211_M_HOSTAP) { 1378 ieee80211_send_error(ni, wh->i_addr2, 1379 IEEE80211_FC0_SUBTYPE_AUTH, 1380 IEEE80211_REASON_MIC_FAILURE); 1381 } 1382 return; 1383 } 1384 if (algo == IEEE80211_AUTH_ALG_SHARED) 1385 sta_auth_shared(ni, wh, frm + 6, efrm, rssi, 1386 noise, rstamp, seq, status); 1387 else if (algo == IEEE80211_AUTH_ALG_OPEN) 1388 sta_auth_open(ni, wh, rssi, noise, rstamp, 1389 seq, status); 1390 else { 1391 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 1392 wh, "auth", "unsupported alg %d", algo); 1393 vap->iv_stats.is_rx_auth_unsupported++; 1394 return; 1395 } 1396 break; 1397 } 1398 1399 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 1400 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: { 1401 uint16_t capinfo, associd; 1402 uint16_t status; 1403 1404 if (vap->iv_state != IEEE80211_S_ASSOC) { 1405 vap->iv_stats.is_rx_mgtdiscard++; 1406 return; 1407 } 1408 1409 /* 1410 * asresp frame format 1411 * [2] capability information 1412 * [2] status 1413 * [2] association ID 1414 * [tlv] supported rates 1415 * [tlv] extended supported rates 1416 * [tlv] WME 1417 * [tlv] HT capabilities 1418 * [tlv] HT info 1419 */ 1420 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return); 1421 ni = vap->iv_bss; 1422 capinfo = le16toh(*(uint16_t *)frm); 1423 frm += 2; 1424 status = le16toh(*(uint16_t *)frm); 1425 frm += 2; 1426 if (status != 0) { 1427 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC, 1428 wh->i_addr2, "%sassoc failed (reason %d)", 1429 ISREASSOC(subtype) ? "re" : "", status); 1430 vap->iv_stats.is_rx_auth_fail++; /* XXX */ 1431 return; 1432 } 1433 associd = le16toh(*(uint16_t *)frm); 1434 frm += 2; 1435 1436 rates = xrates = wme = htcap = htinfo = NULL; 1437 while (efrm - frm > 1) { 1438 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return); 1439 switch (*frm) { 1440 case IEEE80211_ELEMID_RATES: 1441 rates = frm; 1442 break; 1443 case IEEE80211_ELEMID_XRATES: 1444 xrates = frm; 1445 break; 1446 case IEEE80211_ELEMID_HTCAP: 1447 htcap = frm; 1448 break; 1449 case IEEE80211_ELEMID_HTINFO: 1450 htinfo = frm; 1451 break; 1452 case IEEE80211_ELEMID_VENDOR: 1453 if (iswmeoui(frm)) 1454 wme = frm; 1455 else if (vap->iv_flags_ext & IEEE80211_FEXT_HTCOMPAT) { 1456 /* 1457 * Accept pre-draft HT ie's if the 1458 * standard ones have not been seen. 1459 */ 1460 if (ishtcapoui(frm)) { 1461 if (htcap == NULL) 1462 htcap = frm; 1463 } else if (ishtinfooui(frm)) { 1464 if (htinfo == NULL) 1465 htcap = frm; 1466 } 1467 } 1468 /* XXX Atheros OUI support */ 1469 break; 1470 } 1471 frm += frm[1] + 2; 1472 } 1473 1474 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return); 1475 if (xrates != NULL) 1476 IEEE80211_VERIFY_ELEMENT(xrates, 1477 IEEE80211_RATE_MAXSIZE - rates[1], return); 1478 rate = ieee80211_setup_rates(ni, rates, xrates, 1479 IEEE80211_F_JOIN | 1480 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE | 1481 IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 1482 if (rate & IEEE80211_RATE_BASIC) { 1483 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC, 1484 wh->i_addr2, 1485 "%sassoc failed (rate set mismatch)", 1486 ISREASSOC(subtype) ? "re" : ""); 1487 vap->iv_stats.is_rx_assoc_norate++; 1488 ieee80211_new_state(vap, IEEE80211_S_SCAN, 1489 IEEE80211_SCAN_FAIL_STATUS); 1490 return; 1491 } 1492 1493 ni->ni_capinfo = capinfo; 1494 ni->ni_associd = associd; 1495 if (ni->ni_jointime == 0) 1496 ni->ni_jointime = time_uptime; 1497 if (wme != NULL && 1498 ieee80211_parse_wmeparams(vap, wme, wh) >= 0) { 1499 ni->ni_flags |= IEEE80211_NODE_QOS; 1500 ieee80211_wme_updateparams(vap); 1501 } else 1502 ni->ni_flags &= ~IEEE80211_NODE_QOS; 1503 /* 1504 * Setup HT state according to the negotiation. 1505 * 1506 * NB: shouldn't need to check if HT use is enabled but some 1507 * ap's send back HT ie's even when we don't indicate we 1508 * are HT capable in our AssocReq. 1509 */ 1510 if (htcap != NULL && htinfo != NULL && 1511 (vap->iv_flags_ext & IEEE80211_FEXT_HT)) { 1512 ieee80211_ht_node_init(ni); 1513 ieee80211_ht_updateparams(ni, htcap, htinfo); 1514 ieee80211_setup_htrates(ni, htcap, 1515 IEEE80211_F_JOIN | IEEE80211_F_DOBRS); 1516 ieee80211_setup_basic_htrates(ni, htinfo); 1517 } 1518 /* 1519 * Configure state now that we are associated. 1520 * 1521 * XXX may need different/additional driver callbacks? 1522 */ 1523 if (IEEE80211_IS_CHAN_A(ic->ic_curchan) || 1524 (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) { 1525 ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 1526 ic->ic_flags &= ~IEEE80211_F_USEBARKER; 1527 } else { 1528 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE; 1529 ic->ic_flags |= IEEE80211_F_USEBARKER; 1530 } 1531 ieee80211_set_shortslottime(ic, 1532 IEEE80211_IS_CHAN_A(ic->ic_curchan) || 1533 (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)); 1534 /* 1535 * Honor ERP protection. 1536 * 1537 * NB: ni_erp should zero for non-11g operation. 1538 */ 1539 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) && 1540 (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION)) 1541 ic->ic_flags |= IEEE80211_F_USEPROT; 1542 else 1543 ic->ic_flags &= ~IEEE80211_F_USEPROT; 1544 IEEE80211_NOTE_MAC(vap, 1545 IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, wh->i_addr2, 1546 "%sassoc success at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s", 1547 ISREASSOC(subtype) ? "re" : "", 1548 IEEE80211_NODE_AID(ni), 1549 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long", 1550 ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long", 1551 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "", 1552 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "", 1553 ni->ni_flags & IEEE80211_NODE_HT ? 1554 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "", 1555 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "", 1556 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" : 1557 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "", 1558 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "", 1559 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ? 1560 ", fast-frames" : "", 1561 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ? 1562 ", turbo" : "" 1563 ); 1564 ieee80211_new_state(vap, IEEE80211_S_RUN, subtype); 1565 break; 1566 } 1567 1568 case IEEE80211_FC0_SUBTYPE_DEAUTH: { 1569 uint16_t reason; 1570 1571 if (vap->iv_state == IEEE80211_S_SCAN) { 1572 vap->iv_stats.is_rx_mgtdiscard++; 1573 return; 1574 } 1575 if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) { 1576 /* NB: can happen when in promiscuous mode */ 1577 vap->iv_stats.is_rx_mgtdiscard++; 1578 break; 1579 } 1580 1581 /* 1582 * deauth frame format 1583 * [2] reason 1584 */ 1585 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return); 1586 reason = le16toh(*(uint16_t *)frm); 1587 1588 vap->iv_stats.is_rx_deauth++; 1589 vap->iv_stats.is_rx_deauth_code = reason; 1590 IEEE80211_NODE_STAT(ni, rx_deauth); 1591 1592 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni, 1593 "recv deauthenticate (reason %d)", reason); 1594 ieee80211_new_state(vap, IEEE80211_S_AUTH, 1595 (reason << 8) | IEEE80211_FC0_SUBTYPE_DEAUTH); 1596 break; 1597 } 1598 1599 case IEEE80211_FC0_SUBTYPE_DISASSOC: { 1600 uint16_t reason; 1601 1602 if (vap->iv_state != IEEE80211_S_RUN && 1603 vap->iv_state != IEEE80211_S_ASSOC && 1604 vap->iv_state != IEEE80211_S_AUTH) { 1605 vap->iv_stats.is_rx_mgtdiscard++; 1606 return; 1607 } 1608 if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) { 1609 /* NB: can happen when in promiscuous mode */ 1610 vap->iv_stats.is_rx_mgtdiscard++; 1611 break; 1612 } 1613 1614 /* 1615 * disassoc frame format 1616 * [2] reason 1617 */ 1618 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return); 1619 reason = le16toh(*(uint16_t *)frm); 1620 1621 vap->iv_stats.is_rx_disassoc++; 1622 vap->iv_stats.is_rx_disassoc_code = reason; 1623 IEEE80211_NODE_STAT(ni, rx_disassoc); 1624 1625 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni, 1626 "recv disassociate (reason %d)", reason); 1627 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0); 1628 break; 1629 } 1630 1631 case IEEE80211_FC0_SUBTYPE_ACTION: 1632 if (vap->iv_state == IEEE80211_S_RUN) { 1633 if (ieee80211_parse_action(ni, m0) == 0) 1634 ic->ic_recv_action(ni, frm, efrm); 1635 } else 1636 vap->iv_stats.is_rx_mgtdiscard++; 1637 break; 1638 1639 case IEEE80211_FC0_SUBTYPE_PROBE_REQ: 1640 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ: 1641 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: 1642 vap->iv_stats.is_rx_mgtdiscard++; 1643 return; 1644 default: 1645 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 1646 wh, "mgt", "subtype 0x%x not handled", subtype); 1647 vap->iv_stats.is_rx_badsubtype++; 1648 break; 1649 } 1650 #undef ISREASSOC 1651 #undef ISPROBE 1652 } 1653