1 /*- 2 * Copyright (c) 2001 Atsushi Onoe 3 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * Alternatively, this software may be distributed under the terms of the 18 * GNU General Public License ("GPL") version 2 as published by the Free 19 * Software Foundation. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/mbuf.h> 39 #include <sys/malloc.h> 40 #include <sys/endian.h> 41 #include <sys/kernel.h> 42 43 #include <sys/socket.h> 44 45 #include <net/if.h> 46 #include <net/if_media.h> 47 #include <net/ethernet.h> 48 #include <net/if_llc.h> 49 #include <net/if_vlan_var.h> 50 51 #include <net80211/ieee80211_var.h> 52 53 #include <net/bpf.h> 54 55 #ifdef IEEE80211_DEBUG 56 #include <machine/stdarg.h> 57 58 /* 59 * Decide if a received management frame should be 60 * printed when debugging is enabled. This filters some 61 * of the less interesting frames that come frequently 62 * (e.g. beacons). 63 */ 64 static __inline int 65 doprint(struct ieee80211com *ic, int subtype) 66 { 67 switch (subtype) { 68 case IEEE80211_FC0_SUBTYPE_BEACON: 69 return (ic->ic_flags & IEEE80211_F_SCAN); 70 case IEEE80211_FC0_SUBTYPE_PROBE_REQ: 71 return (ic->ic_opmode == IEEE80211_M_IBSS); 72 } 73 return 1; 74 } 75 76 /* 77 * Emit a debug message about discarding a frame or information 78 * element. One format is for extracting the mac address from 79 * the frame header; the other is for when a header is not 80 * available or otherwise appropriate. 81 */ 82 #define IEEE80211_DISCARD(_ic, _m, _wh, _type, _fmt, ...) do { \ 83 if ((_ic)->ic_debug & (_m)) \ 84 ieee80211_discard_frame(_ic, _wh, _type, _fmt, __VA_ARGS__);\ 85 } while (0) 86 #define IEEE80211_DISCARD_IE(_ic, _m, _wh, _type, _fmt, ...) do { \ 87 if ((_ic)->ic_debug & (_m)) \ 88 ieee80211_discard_ie(_ic, _wh, _type, _fmt, __VA_ARGS__);\ 89 } while (0) 90 #define IEEE80211_DISCARD_MAC(_ic, _m, _mac, _type, _fmt, ...) do { \ 91 if ((_ic)->ic_debug & (_m)) \ 92 ieee80211_discard_mac(_ic, _mac, _type, _fmt, __VA_ARGS__);\ 93 } while (0) 94 95 static const u_int8_t *ieee80211_getbssid(struct ieee80211com *, 96 const struct ieee80211_frame *); 97 static void ieee80211_discard_frame(struct ieee80211com *, 98 const struct ieee80211_frame *, const char *type, const char *fmt, ...); 99 static void ieee80211_discard_ie(struct ieee80211com *, 100 const struct ieee80211_frame *, const char *type, const char *fmt, ...); 101 static void ieee80211_discard_mac(struct ieee80211com *, 102 const u_int8_t mac[IEEE80211_ADDR_LEN], const char *type, 103 const char *fmt, ...); 104 #else 105 #define IEEE80211_DISCARD(_ic, _m, _wh, _type, _fmt, ...) 106 #define IEEE80211_DISCARD_IE(_ic, _m, _wh, _type, _fmt, ...) 107 #define IEEE80211_DISCARD_MAC(_ic, _m, _mac, _type, _fmt, ...) 108 #endif /* IEEE80211_DEBUG */ 109 110 static struct mbuf *ieee80211_defrag(struct ieee80211com *, 111 struct ieee80211_node *, struct mbuf *, int); 112 static struct mbuf *ieee80211_decap(struct ieee80211com *, struct mbuf *, int); 113 static void ieee80211_send_error(struct ieee80211com *, struct ieee80211_node *, 114 const u_int8_t *mac, int subtype, int arg); 115 static void ieee80211_node_pwrsave(struct ieee80211_node *, int enable); 116 static void ieee80211_recv_pspoll(struct ieee80211com *, 117 struct ieee80211_node *, struct mbuf *); 118 119 /* 120 * Process a received frame. The node associated with the sender 121 * should be supplied. If nothing was found in the node table then 122 * the caller is assumed to supply a reference to ic_bss instead. 123 * The RSSI and a timestamp are also supplied. The RSSI data is used 124 * during AP scanning to select a AP to associate with; it can have 125 * any units so long as values have consistent units and higher values 126 * mean ``better signal''. The receive timestamp is currently not used 127 * by the 802.11 layer. 128 */ 129 int 130 ieee80211_input(struct ieee80211com *ic, struct mbuf *m, 131 struct ieee80211_node *ni, int rssi, u_int32_t rstamp) 132 { 133 #define SEQ_LEQ(a,b) ((int)((a)-(b)) <= 0) 134 #define HAS_SEQ(type) ((type & 0x4) == 0) 135 struct ifnet *ifp = ic->ic_ifp; 136 struct ieee80211_frame *wh; 137 struct ieee80211_key *key; 138 struct ether_header *eh; 139 int len, hdrspace; 140 u_int8_t dir, type, subtype; 141 u_int8_t *bssid; 142 u_int16_t rxseq; 143 144 KASSERT(ni != NULL, ("null node")); 145 ni->ni_inact = ni->ni_inact_reload; 146 147 /* trim CRC here so WEP can find its own CRC at the end of packet. */ 148 if (m->m_flags & M_HASFCS) { 149 m_adj(m, -IEEE80211_CRC_LEN); 150 m->m_flags &= ~M_HASFCS; 151 } 152 KASSERT(m->m_pkthdr.len >= sizeof(struct ieee80211_frame_min), 153 ("frame length too short: %u", m->m_pkthdr.len)); 154 155 type = -1; /* undefined */ 156 /* 157 * In monitor mode, send everything directly to bpf. 158 * XXX may want to include the CRC 159 */ 160 if (ic->ic_opmode == IEEE80211_M_MONITOR) 161 goto out; 162 163 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) { 164 IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY, 165 ni->ni_macaddr, NULL, 166 "too short (1): len %u", m->m_pkthdr.len); 167 ic->ic_stats.is_rx_tooshort++; 168 goto out; 169 } 170 /* 171 * Bit of a cheat here, we use a pointer for a 3-address 172 * frame format but don't reference fields past outside 173 * ieee80211_frame_min w/o first validating the data is 174 * present. 175 */ 176 wh = mtod(m, struct ieee80211_frame *); 177 178 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) != 179 IEEE80211_FC0_VERSION_0) { 180 IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY, 181 ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]); 182 ic->ic_stats.is_rx_badversion++; 183 goto err; 184 } 185 186 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 187 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 188 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 189 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) { 190 switch (ic->ic_opmode) { 191 case IEEE80211_M_STA: 192 bssid = wh->i_addr2; 193 if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) { 194 /* not interested in */ 195 IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT, 196 bssid, NULL, "%s", "not to bss"); 197 ic->ic_stats.is_rx_wrongbss++; 198 goto out; 199 } 200 break; 201 case IEEE80211_M_IBSS: 202 case IEEE80211_M_AHDEMO: 203 case IEEE80211_M_HOSTAP: 204 if (dir != IEEE80211_FC1_DIR_NODS) 205 bssid = wh->i_addr1; 206 else if (type == IEEE80211_FC0_TYPE_CTL) 207 bssid = wh->i_addr1; 208 else { 209 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) { 210 IEEE80211_DISCARD_MAC(ic, 211 IEEE80211_MSG_ANY, ni->ni_macaddr, 212 NULL, "too short (2): len %u", 213 m->m_pkthdr.len); 214 ic->ic_stats.is_rx_tooshort++; 215 goto out; 216 } 217 bssid = wh->i_addr3; 218 } 219 if (type != IEEE80211_FC0_TYPE_DATA) 220 break; 221 /* 222 * Data frame, validate the bssid. 223 */ 224 if (!IEEE80211_ADDR_EQ(bssid, ic->ic_bss->ni_bssid) && 225 !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) { 226 /* not interested in */ 227 IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT, 228 bssid, NULL, "%s", "not to bss"); 229 ic->ic_stats.is_rx_wrongbss++; 230 goto out; 231 } 232 /* 233 * For adhoc mode we cons up a node when it doesn't 234 * exist. This should probably done after an ACL check. 235 */ 236 if (ni == ic->ic_bss && 237 ic->ic_opmode != IEEE80211_M_HOSTAP) { 238 /* 239 * Fake up a node for this newly 240 * discovered member of the IBSS. 241 */ 242 ni = ieee80211_fakeup_adhoc_node(&ic->ic_sta, 243 wh->i_addr2); 244 if (ni == NULL) { 245 /* NB: stat kept for alloc failure */ 246 goto err; 247 } 248 } 249 break; 250 default: 251 goto out; 252 } 253 ni->ni_rssi = rssi; 254 ni->ni_rstamp = rstamp; 255 if (HAS_SEQ(type)) { 256 u_int8_t tid; 257 if (IEEE80211_QOS_HAS_SEQ(wh)) { 258 tid = ((struct ieee80211_qosframe *)wh)-> 259 i_qos[0] & IEEE80211_QOS_TID; 260 if (tid >= WME_AC_VI) 261 ic->ic_wme.wme_hipri_traffic++; 262 tid++; 263 } else 264 tid = 0; 265 rxseq = le16toh(*(u_int16_t *)wh->i_seq); 266 if ((wh->i_fc[1] & IEEE80211_FC1_RETRY) && 267 SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) { 268 /* duplicate, discard */ 269 IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT, 270 bssid, "duplicate", 271 "seqno <%u,%u> fragno <%u,%u> tid %u", 272 rxseq >> IEEE80211_SEQ_SEQ_SHIFT, 273 ni->ni_rxseqs[tid] >> 274 IEEE80211_SEQ_SEQ_SHIFT, 275 rxseq & IEEE80211_SEQ_FRAG_MASK, 276 ni->ni_rxseqs[tid] & 277 IEEE80211_SEQ_FRAG_MASK, 278 tid); 279 ic->ic_stats.is_rx_dup++; 280 IEEE80211_NODE_STAT(ni, rx_dup); 281 goto out; 282 } 283 ni->ni_rxseqs[tid] = rxseq; 284 } 285 } 286 287 switch (type) { 288 case IEEE80211_FC0_TYPE_DATA: 289 hdrspace = ieee80211_hdrspace(ic, wh); 290 if (m->m_len < hdrspace && 291 (m = m_pullup(m, hdrspace)) == NULL) { 292 IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY, 293 ni->ni_macaddr, NULL, 294 "data too short: expecting %u", hdrspace); 295 ic->ic_stats.is_rx_tooshort++; 296 goto out; /* XXX */ 297 } 298 switch (ic->ic_opmode) { 299 case IEEE80211_M_STA: 300 if (dir != IEEE80211_FC1_DIR_FROMDS) { 301 ic->ic_stats.is_rx_wrongdir++; 302 goto out; 303 } 304 if ((ifp->if_flags & IFF_SIMPLEX) && 305 IEEE80211_IS_MULTICAST(wh->i_addr1) && 306 IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_myaddr)) { 307 /* 308 * In IEEE802.11 network, multicast packet 309 * sent from me is broadcasted from AP. 310 * It should be silently discarded for 311 * SIMPLEX interface. 312 */ 313 IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT, 314 wh, NULL, "%s", "multicast echo"); 315 ic->ic_stats.is_rx_mcastecho++; 316 goto out; 317 } 318 break; 319 case IEEE80211_M_IBSS: 320 case IEEE80211_M_AHDEMO: 321 if (dir != IEEE80211_FC1_DIR_NODS) { 322 ic->ic_stats.is_rx_wrongdir++; 323 goto out; 324 } 325 /* XXX no power-save support */ 326 break; 327 case IEEE80211_M_HOSTAP: 328 if (dir != IEEE80211_FC1_DIR_TODS) { 329 ic->ic_stats.is_rx_wrongdir++; 330 goto out; 331 } 332 /* check if source STA is associated */ 333 if (ni == ic->ic_bss) { 334 IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT, 335 wh, "data", "%s", "unknown src"); 336 ieee80211_send_error(ic, ni, wh->i_addr2, 337 IEEE80211_FC0_SUBTYPE_DEAUTH, 338 IEEE80211_REASON_NOT_AUTHED); 339 ic->ic_stats.is_rx_notassoc++; 340 goto err; 341 } 342 if (ni->ni_associd == 0) { 343 IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT, 344 wh, "data", "%s", "unassoc src"); 345 IEEE80211_SEND_MGMT(ic, ni, 346 IEEE80211_FC0_SUBTYPE_DISASSOC, 347 IEEE80211_REASON_NOT_ASSOCED); 348 ic->ic_stats.is_rx_notassoc++; 349 goto err; 350 } 351 352 /* 353 * Check for power save state change. 354 */ 355 if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^ 356 (ni->ni_flags & IEEE80211_NODE_PWR_MGT))) 357 ieee80211_node_pwrsave(ni, 358 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT); 359 break; 360 default: 361 /* XXX here to keep compiler happy */ 362 goto out; 363 } 364 365 /* 366 * Handle privacy requirements. Note that we 367 * must not be preempted from here until after 368 * we (potentially) call ieee80211_crypto_demic; 369 * otherwise we may violate assumptions in the 370 * crypto cipher modules used to do delayed update 371 * of replay sequence numbers. 372 */ 373 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 374 if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) { 375 /* 376 * Discard encrypted frames when privacy is off. 377 */ 378 IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT, 379 wh, "WEP", "%s", "PRIVACY off"); 380 ic->ic_stats.is_rx_noprivacy++; 381 IEEE80211_NODE_STAT(ni, rx_noprivacy); 382 goto out; 383 } 384 key = ieee80211_crypto_decap(ic, ni, m, hdrspace); 385 if (key == NULL) { 386 /* NB: stats+msgs handled in crypto_decap */ 387 IEEE80211_NODE_STAT(ni, rx_wepfail); 388 goto out; 389 } 390 wh = mtod(m, struct ieee80211_frame *); 391 wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 392 } else { 393 key = NULL; 394 } 395 396 /* 397 * Next up, any fragmentation. 398 */ 399 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 400 m = ieee80211_defrag(ic, ni, m, hdrspace); 401 if (m == NULL) { 402 /* Fragment dropped or frame not complete yet */ 403 goto out; 404 } 405 } 406 wh = NULL; /* no longer valid, catch any uses */ 407 408 /* 409 * Next strip any MSDU crypto bits. 410 */ 411 if (key != NULL && !ieee80211_crypto_demic(ic, key, m, 0)) { 412 IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT, 413 ni->ni_macaddr, "data", "%s", "demic error"); 414 IEEE80211_NODE_STAT(ni, rx_demicfail); 415 goto out; 416 } 417 418 /* copy to listener after decrypt */ 419 if (ic->ic_rawbpf) 420 bpf_mtap(ic->ic_rawbpf, m); 421 422 /* 423 * Finally, strip the 802.11 header. 424 */ 425 m = ieee80211_decap(ic, m, hdrspace); 426 if (m == NULL) { 427 /* don't count Null data frames as errors */ 428 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA) 429 goto out; 430 IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT, 431 ni->ni_macaddr, "data", "%s", "decap error"); 432 ic->ic_stats.is_rx_decap++; 433 IEEE80211_NODE_STAT(ni, rx_decap); 434 goto err; 435 } 436 eh = mtod(m, struct ether_header *); 437 if (!ieee80211_node_is_authorized(ni)) { 438 /* 439 * Deny any non-PAE frames received prior to 440 * authorization. For open/shared-key 441 * authentication the port is mark authorized 442 * after authentication completes. For 802.1x 443 * the port is not marked authorized by the 444 * authenticator until the handshake has completed. 445 */ 446 if (eh->ether_type != htons(ETHERTYPE_PAE)) { 447 IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT, 448 eh->ether_shost, "data", 449 "unauthorized port: ether type 0x%x len %u", 450 eh->ether_type, m->m_pkthdr.len); 451 ic->ic_stats.is_rx_unauth++; 452 IEEE80211_NODE_STAT(ni, rx_unauth); 453 goto err; 454 } 455 } else { 456 /* 457 * When denying unencrypted frames, discard 458 * any non-PAE frames received without encryption. 459 */ 460 if ((ic->ic_flags & IEEE80211_F_DROPUNENC) && 461 key == NULL && 462 eh->ether_type != htons(ETHERTYPE_PAE)) { 463 /* 464 * Drop unencrypted frames. 465 */ 466 ic->ic_stats.is_rx_unencrypted++; 467 IEEE80211_NODE_STAT(ni, rx_unencrypted); 468 goto out; 469 } 470 } 471 ifp->if_ipackets++; 472 IEEE80211_NODE_STAT(ni, rx_data); 473 IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len); 474 475 /* perform as a bridge within the AP */ 476 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 477 (ic->ic_flags & IEEE80211_F_NOBRIDGE) == 0) { 478 struct mbuf *m1 = NULL; 479 480 if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 481 m1 = m_copypacket(m, M_DONTWAIT); 482 if (m1 == NULL) 483 ifp->if_oerrors++; 484 else 485 m1->m_flags |= M_MCAST; 486 } else { 487 /* XXX this dups work done in ieee80211_encap */ 488 /* check if destination is associated */ 489 struct ieee80211_node *ni1 = 490 ieee80211_find_node(&ic->ic_sta, 491 eh->ether_dhost); 492 if (ni1 != NULL) { 493 /* XXX check if authorized */ 494 if (ni1->ni_associd != 0) { 495 m1 = m; 496 m = NULL; 497 } 498 /* XXX statistic? */ 499 ieee80211_free_node(ni1); 500 } 501 } 502 if (m1 != NULL) { 503 len = m1->m_pkthdr.len; 504 IF_ENQUEUE(&ifp->if_snd, m1); 505 if (m != NULL) 506 ifp->if_omcasts++; 507 ifp->if_obytes += len; 508 } 509 } 510 if (m != NULL) { 511 if (ni->ni_vlan != 0) { 512 /* attach vlan tag */ 513 /* XXX goto err? */ 514 VLAN_INPUT_TAG(ifp, m, ni->ni_vlan, goto out); 515 } 516 (*ifp->if_input)(ifp, m); 517 } 518 return IEEE80211_FC0_TYPE_DATA; 519 520 case IEEE80211_FC0_TYPE_MGT: 521 IEEE80211_NODE_STAT(ni, rx_mgmt); 522 if (dir != IEEE80211_FC1_DIR_NODS) { 523 ic->ic_stats.is_rx_wrongdir++; 524 goto err; 525 } 526 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) { 527 IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY, 528 ni->ni_macaddr, "mgt", "too short: len %u", 529 m->m_pkthdr.len); 530 ic->ic_stats.is_rx_tooshort++; 531 goto out; 532 } 533 #ifdef IEEE80211_DEBUG 534 if ((ieee80211_msg_debug(ic) && doprint(ic, subtype)) || 535 ieee80211_msg_dumppkts(ic)) { 536 if_printf(ic->ic_ifp, "received %s from %s rssi %d\n", 537 ieee80211_mgt_subtype_name[subtype >> 538 IEEE80211_FC0_SUBTYPE_SHIFT], 539 ether_sprintf(wh->i_addr2), rssi); 540 } 541 #endif 542 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 543 if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) { 544 /* 545 * Only shared key auth frames with a challenge 546 * should be encrypted, discard all others. 547 */ 548 IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT, 549 wh, ieee80211_mgt_subtype_name[subtype >> 550 IEEE80211_FC0_SUBTYPE_SHIFT], 551 "%s", "WEP set but not permitted"); 552 ic->ic_stats.is_rx_mgtdiscard++; /* XXX */ 553 goto out; 554 } 555 if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) { 556 /* 557 * Discard encrypted frames when privacy is off. 558 */ 559 IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT, 560 wh, "mgt", "%s", "WEP set but PRIVACY off"); 561 ic->ic_stats.is_rx_noprivacy++; 562 goto out; 563 } 564 hdrspace = ieee80211_hdrspace(ic, wh); 565 key = ieee80211_crypto_decap(ic, ni, m, hdrspace); 566 if (key == NULL) { 567 /* NB: stats+msgs handled in crypto_decap */ 568 goto out; 569 } 570 wh = mtod(m, struct ieee80211_frame *); 571 wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 572 } 573 if (ic->ic_rawbpf) 574 bpf_mtap(ic->ic_rawbpf, m); 575 (*ic->ic_recv_mgmt)(ic, m, ni, subtype, rssi, rstamp); 576 m_freem(m); 577 return type; 578 579 case IEEE80211_FC0_TYPE_CTL: 580 IEEE80211_NODE_STAT(ni, rx_ctrl); 581 ic->ic_stats.is_rx_ctl++; 582 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 583 switch (subtype) { 584 case IEEE80211_FC0_SUBTYPE_PS_POLL: 585 ieee80211_recv_pspoll(ic, ni, m); 586 break; 587 } 588 } 589 goto out; 590 default: 591 IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY, 592 wh, NULL, "bad frame type 0x%x", type); 593 /* should not come here */ 594 break; 595 } 596 err: 597 ifp->if_ierrors++; 598 out: 599 if (m != NULL) { 600 if (ic->ic_rawbpf) 601 bpf_mtap(ic->ic_rawbpf, m); 602 m_freem(m); 603 } 604 return type; 605 #undef SEQ_LEQ 606 } 607 608 /* 609 * This function reassemble fragments. 610 */ 611 static struct mbuf * 612 ieee80211_defrag(struct ieee80211com *ic, struct ieee80211_node *ni, 613 struct mbuf *m, int hdrspace) 614 { 615 struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *); 616 struct ieee80211_frame *lwh; 617 u_int16_t rxseq; 618 u_int8_t fragno; 619 u_int8_t more_frag = wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG; 620 struct mbuf *mfrag; 621 622 KASSERT(!IEEE80211_IS_MULTICAST(wh->i_addr1), ("multicast fragm?")); 623 624 rxseq = le16toh(*(u_int16_t *)wh->i_seq); 625 fragno = rxseq & IEEE80211_SEQ_FRAG_MASK; 626 627 /* Quick way out, if there's nothing to defragment */ 628 if (!more_frag && fragno == 0 && ni->ni_rxfrag[0] == NULL) 629 return m; 630 631 /* 632 * Remove frag to insure it doesn't get reaped by timer. 633 */ 634 if (ni->ni_table == NULL) { 635 /* 636 * Should never happen. If the node is orphaned (not in 637 * the table) then input packets should not reach here. 638 * Otherwise, a concurrent request that yanks the table 639 * should be blocked by other interlocking and/or by first 640 * shutting the driver down. Regardless, be defensive 641 * here and just bail 642 */ 643 /* XXX need msg+stat */ 644 m_freem(m); 645 return NULL; 646 } 647 IEEE80211_NODE_LOCK(ni->ni_table); 648 mfrag = ni->ni_rxfrag[0]; 649 ni->ni_rxfrag[0] = NULL; 650 IEEE80211_NODE_UNLOCK(ni->ni_table); 651 652 /* 653 * Validate new fragment is in order and 654 * related to the previous ones. 655 */ 656 if (mfrag != NULL) { 657 u_int16_t last_rxseq; 658 659 lwh = mtod(mfrag, struct ieee80211_frame *); 660 last_rxseq = le16toh(*(u_int16_t *)lwh->i_seq); 661 /* NB: check seq # and frag together */ 662 if (rxseq != last_rxseq+1 || 663 !IEEE80211_ADDR_EQ(wh->i_addr1, lwh->i_addr1) || 664 !IEEE80211_ADDR_EQ(wh->i_addr2, lwh->i_addr2)) { 665 /* 666 * Unrelated fragment or no space for it, 667 * clear current fragments. 668 */ 669 m_freem(mfrag); 670 mfrag = NULL; 671 } 672 } 673 674 if (mfrag == NULL) { 675 if (fragno != 0) { /* !first fragment, discard */ 676 IEEE80211_NODE_STAT(ni, rx_defrag); 677 m_freem(m); 678 return NULL; 679 } 680 mfrag = m; 681 } else { /* concatenate */ 682 m_adj(m, hdrspace); /* strip header */ 683 m_cat(mfrag, m); 684 /* NB: m_cat doesn't update the packet header */ 685 mfrag->m_pkthdr.len += m->m_pkthdr.len; 686 /* track last seqnum and fragno */ 687 lwh = mtod(mfrag, struct ieee80211_frame *); 688 *(u_int16_t *) lwh->i_seq = *(u_int16_t *) wh->i_seq; 689 } 690 if (more_frag) { /* more to come, save */ 691 ni->ni_rxfragstamp = ticks; 692 ni->ni_rxfrag[0] = mfrag; 693 mfrag = NULL; 694 } 695 return mfrag; 696 } 697 698 static struct mbuf * 699 ieee80211_decap(struct ieee80211com *ic, struct mbuf *m, int hdrlen) 700 { 701 struct ieee80211_qosframe_addr4 wh; /* Max size address frames */ 702 struct ether_header *eh; 703 struct llc *llc; 704 705 if (m->m_len < hdrlen + sizeof(*llc) && 706 (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) { 707 /* XXX stat, msg */ 708 return NULL; 709 } 710 memcpy(&wh, mtod(m, caddr_t), hdrlen); 711 llc = (struct llc *)(mtod(m, caddr_t) + hdrlen); 712 if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP && 713 llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 && 714 llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0) { 715 m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh)); 716 llc = NULL; 717 } else { 718 m_adj(m, hdrlen - sizeof(*eh)); 719 } 720 eh = mtod(m, struct ether_header *); 721 switch (wh.i_fc[1] & IEEE80211_FC1_DIR_MASK) { 722 case IEEE80211_FC1_DIR_NODS: 723 IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1); 724 IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2); 725 break; 726 case IEEE80211_FC1_DIR_TODS: 727 IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3); 728 IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2); 729 break; 730 case IEEE80211_FC1_DIR_FROMDS: 731 IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1); 732 IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr3); 733 break; 734 case IEEE80211_FC1_DIR_DSTODS: 735 IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3); 736 IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr4); 737 break; 738 } 739 #ifdef ALIGNED_POINTER 740 if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), u_int32_t)) { 741 struct mbuf *n, *n0, **np; 742 caddr_t newdata; 743 int off, pktlen; 744 745 n0 = NULL; 746 np = &n0; 747 off = 0; 748 pktlen = m->m_pkthdr.len; 749 while (pktlen > off) { 750 if (n0 == NULL) { 751 MGETHDR(n, M_DONTWAIT, MT_DATA); 752 if (n == NULL) { 753 m_freem(m); 754 return NULL; 755 } 756 M_MOVE_PKTHDR(n, m); 757 n->m_len = MHLEN; 758 } else { 759 MGET(n, M_DONTWAIT, MT_DATA); 760 if (n == NULL) { 761 m_freem(m); 762 m_freem(n0); 763 return NULL; 764 } 765 n->m_len = MLEN; 766 } 767 if (pktlen - off >= MINCLSIZE) { 768 MCLGET(n, M_DONTWAIT); 769 if (n->m_flags & M_EXT) 770 n->m_len = n->m_ext.ext_size; 771 } 772 if (n0 == NULL) { 773 newdata = 774 (caddr_t)ALIGN(n->m_data + sizeof(*eh)) - 775 sizeof(*eh); 776 n->m_len -= newdata - n->m_data; 777 n->m_data = newdata; 778 } 779 if (n->m_len > pktlen - off) 780 n->m_len = pktlen - off; 781 m_copydata(m, off, n->m_len, mtod(n, caddr_t)); 782 off += n->m_len; 783 *np = n; 784 np = &n->m_next; 785 } 786 m_freem(m); 787 m = n0; 788 } 789 #endif /* ALIGNED_POINTER */ 790 if (llc != NULL) { 791 eh = mtod(m, struct ether_header *); 792 eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh)); 793 } 794 return m; 795 } 796 797 /* 798 * Install received rate set information in the node's state block. 799 */ 800 static int 801 ieee80211_setup_rates(struct ieee80211com *ic, struct ieee80211_node *ni, 802 u_int8_t *rates, u_int8_t *xrates, int flags) 803 { 804 struct ieee80211_rateset *rs = &ni->ni_rates; 805 806 memset(rs, 0, sizeof(*rs)); 807 rs->rs_nrates = rates[1]; 808 memcpy(rs->rs_rates, rates + 2, rs->rs_nrates); 809 if (xrates != NULL) { 810 u_int8_t nxrates; 811 /* 812 * Tack on 11g extended supported rate element. 813 */ 814 nxrates = xrates[1]; 815 if (rs->rs_nrates + nxrates > IEEE80211_RATE_MAXSIZE) { 816 nxrates = IEEE80211_RATE_MAXSIZE - rs->rs_nrates; 817 IEEE80211_DPRINTF(ic, IEEE80211_MSG_XRATE, 818 "[%s] extended rate set too large;" 819 " only using %u of %u rates\n", 820 ether_sprintf(ni->ni_macaddr), nxrates, xrates[1]); 821 ic->ic_stats.is_rx_rstoobig++; 822 } 823 memcpy(rs->rs_rates + rs->rs_nrates, xrates+2, nxrates); 824 rs->rs_nrates += nxrates; 825 } 826 return ieee80211_fix_rate(ic, ni, flags); 827 } 828 829 static void 830 ieee80211_auth_open(struct ieee80211com *ic, struct ieee80211_frame *wh, 831 struct ieee80211_node *ni, int rssi, u_int32_t rstamp, u_int16_t seq, 832 u_int16_t status) 833 { 834 835 if (ni->ni_authmode == IEEE80211_AUTH_SHARED) { 836 IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH, 837 ni->ni_macaddr, "open auth", 838 "bad sta auth mode %u", ni->ni_authmode); 839 ic->ic_stats.is_rx_bad_auth++; /* XXX */ 840 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 841 /* XXX hack to workaround calling convention */ 842 ieee80211_send_error(ic, ni, wh->i_addr2, 843 IEEE80211_FC0_SUBTYPE_AUTH, 844 (seq + 1) | (IEEE80211_STATUS_ALG<<16)); 845 } 846 return; 847 } 848 switch (ic->ic_opmode) { 849 case IEEE80211_M_IBSS: 850 case IEEE80211_M_AHDEMO: 851 case IEEE80211_M_MONITOR: 852 /* should not come here */ 853 IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH, 854 ni->ni_macaddr, "open auth", 855 "bad operating mode %u", ic->ic_opmode); 856 break; 857 858 case IEEE80211_M_HOSTAP: 859 if (ic->ic_state != IEEE80211_S_RUN || 860 seq != IEEE80211_AUTH_OPEN_REQUEST) { 861 ic->ic_stats.is_rx_bad_auth++; 862 return; 863 } 864 /* always accept open authentication requests */ 865 if (ni == ic->ic_bss) { 866 ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2); 867 if (ni == NULL) 868 return; 869 } else 870 (void) ieee80211_ref_node(ni); 871 IEEE80211_SEND_MGMT(ic, ni, 872 IEEE80211_FC0_SUBTYPE_AUTH, seq + 1); 873 IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, 874 "[%s] station authenticated (open)\n", 875 ether_sprintf(ni->ni_macaddr)); 876 /* 877 * When 802.1x is not in use mark the port 878 * authorized at this point so traffic can flow. 879 */ 880 if (ni->ni_authmode != IEEE80211_AUTH_8021X) 881 ieee80211_node_authorize(ic, ni); 882 break; 883 884 case IEEE80211_M_STA: 885 if (ic->ic_state != IEEE80211_S_AUTH || 886 seq != IEEE80211_AUTH_OPEN_RESPONSE) { 887 ic->ic_stats.is_rx_bad_auth++; 888 return; 889 } 890 if (status != 0) { 891 IEEE80211_DPRINTF(ic, 892 IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, 893 "[%s] open auth failed (reason %d)\n", 894 ether_sprintf(ni->ni_macaddr), status); 895 /* XXX can this happen? */ 896 if (ni != ic->ic_bss) 897 ni->ni_fails++; 898 ic->ic_stats.is_rx_auth_fail++; 899 ieee80211_new_state(ic, IEEE80211_S_SCAN, 0); 900 } else 901 ieee80211_new_state(ic, IEEE80211_S_ASSOC, 902 wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK); 903 break; 904 } 905 } 906 907 /* 908 * Send a management frame error response to the specified 909 * station. If ni is associated with the station then use 910 * it; otherwise allocate a temporary node suitable for 911 * transmitting the frame and then free the reference so 912 * it will go away as soon as the frame has been transmitted. 913 */ 914 static void 915 ieee80211_send_error(struct ieee80211com *ic, struct ieee80211_node *ni, 916 const u_int8_t *mac, int subtype, int arg) 917 { 918 int istmp; 919 920 if (ni == ic->ic_bss) { 921 ni = ieee80211_dup_bss(&ic->ic_sta, mac); 922 if (ni == NULL) { 923 /* XXX msg */ 924 return; 925 } 926 istmp = 1; 927 } else 928 istmp = 0; 929 IEEE80211_SEND_MGMT(ic, ni, subtype, arg); 930 if (istmp) 931 ieee80211_free_node(ni); 932 } 933 934 static int 935 alloc_challenge(struct ieee80211com *ic, struct ieee80211_node *ni) 936 { 937 if (ni->ni_challenge == NULL) 938 MALLOC(ni->ni_challenge, u_int32_t*, IEEE80211_CHALLENGE_LEN, 939 M_DEVBUF, M_NOWAIT); 940 if (ni->ni_challenge == NULL) { 941 IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, 942 "[%s] shared key challenge alloc failed\n", 943 ether_sprintf(ni->ni_macaddr)); 944 /* XXX statistic */ 945 } 946 return (ni->ni_challenge != NULL); 947 } 948 949 /* XXX TODO: add statistics */ 950 static void 951 ieee80211_auth_shared(struct ieee80211com *ic, struct ieee80211_frame *wh, 952 u_int8_t *frm, u_int8_t *efrm, struct ieee80211_node *ni, int rssi, 953 u_int32_t rstamp, u_int16_t seq, u_int16_t status) 954 { 955 u_int8_t *challenge; 956 int allocbs, estatus; 957 958 /* 959 * NB: this can happen as we allow pre-shared key 960 * authentication to be enabled w/o wep being turned 961 * on so that configuration of these can be done 962 * in any order. It may be better to enforce the 963 * ordering in which case this check would just be 964 * for sanity/consistency. 965 */ 966 if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) { 967 IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH, 968 ni->ni_macaddr, "shared key auth", 969 "%s", " PRIVACY is disabled"); 970 estatus = IEEE80211_STATUS_ALG; 971 goto bad; 972 } 973 /* 974 * Pre-shared key authentication is evil; accept 975 * it only if explicitly configured (it is supported 976 * mainly for compatibility with clients like OS X). 977 */ 978 if (ni->ni_authmode != IEEE80211_AUTH_AUTO && 979 ni->ni_authmode != IEEE80211_AUTH_SHARED) { 980 IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH, 981 ni->ni_macaddr, "shared key auth", 982 "bad sta auth mode %u", ni->ni_authmode); 983 ic->ic_stats.is_rx_bad_auth++; /* XXX maybe a unique error? */ 984 estatus = IEEE80211_STATUS_ALG; 985 goto bad; 986 } 987 988 challenge = NULL; 989 if (frm + 1 < efrm) { 990 if ((frm[1] + 2) > (efrm - frm)) { 991 IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH, 992 ni->ni_macaddr, "shared key auth", 993 "ie %d/%d too long", 994 frm[0], (frm[1] + 2) - (efrm - frm)); 995 ic->ic_stats.is_rx_bad_auth++; 996 estatus = IEEE80211_STATUS_CHALLENGE; 997 goto bad; 998 } 999 if (*frm == IEEE80211_ELEMID_CHALLENGE) 1000 challenge = frm; 1001 frm += frm[1] + 2; 1002 } 1003 switch (seq) { 1004 case IEEE80211_AUTH_SHARED_CHALLENGE: 1005 case IEEE80211_AUTH_SHARED_RESPONSE: 1006 if (challenge == NULL) { 1007 IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH, 1008 ni->ni_macaddr, "shared key auth", 1009 "%s", "no challenge"); 1010 ic->ic_stats.is_rx_bad_auth++; 1011 estatus = IEEE80211_STATUS_CHALLENGE; 1012 goto bad; 1013 } 1014 if (challenge[1] != IEEE80211_CHALLENGE_LEN) { 1015 IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH, 1016 ni->ni_macaddr, "shared key auth", 1017 "bad challenge len %d", challenge[1]); 1018 ic->ic_stats.is_rx_bad_auth++; 1019 estatus = IEEE80211_STATUS_CHALLENGE; 1020 goto bad; 1021 } 1022 default: 1023 break; 1024 } 1025 switch (ic->ic_opmode) { 1026 case IEEE80211_M_MONITOR: 1027 case IEEE80211_M_AHDEMO: 1028 case IEEE80211_M_IBSS: 1029 IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH, 1030 ni->ni_macaddr, "shared key auth", 1031 "bad operating mode %u", ic->ic_opmode); 1032 return; 1033 case IEEE80211_M_HOSTAP: 1034 if (ic->ic_state != IEEE80211_S_RUN) { 1035 IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH, 1036 ni->ni_macaddr, "shared key auth", 1037 "bad state %u", ic->ic_state); 1038 estatus = IEEE80211_STATUS_ALG; /* XXX */ 1039 goto bad; 1040 } 1041 switch (seq) { 1042 case IEEE80211_AUTH_SHARED_REQUEST: 1043 if (ni == ic->ic_bss) { 1044 ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2); 1045 if (ni == NULL) { 1046 /* NB: no way to return an error */ 1047 return; 1048 } 1049 allocbs = 1; 1050 } else { 1051 (void) ieee80211_ref_node(ni); 1052 allocbs = 0; 1053 } 1054 ni->ni_rssi = rssi; 1055 ni->ni_rstamp = rstamp; 1056 if (!alloc_challenge(ic, ni)) { 1057 /* NB: don't return error so they rexmit */ 1058 return; 1059 } 1060 get_random_bytes(ni->ni_challenge, 1061 IEEE80211_CHALLENGE_LEN); 1062 IEEE80211_DPRINTF(ic, 1063 IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, 1064 "[%s] shared key %sauth request\n", 1065 ether_sprintf(ni->ni_macaddr), 1066 allocbs ? "" : "re"); 1067 break; 1068 case IEEE80211_AUTH_SHARED_RESPONSE: 1069 if (ni == ic->ic_bss) { 1070 IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH, 1071 ni->ni_macaddr, "shared key response", 1072 "%s", "unknown station"); 1073 /* NB: don't send a response */ 1074 return; 1075 } 1076 if (ni->ni_challenge == NULL) { 1077 IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH, 1078 ni->ni_macaddr, "shared key response", 1079 "%s", "no challenge recorded"); 1080 ic->ic_stats.is_rx_bad_auth++; 1081 estatus = IEEE80211_STATUS_CHALLENGE; 1082 goto bad; 1083 } 1084 if (memcmp(ni->ni_challenge, &challenge[2], 1085 challenge[1]) != 0) { 1086 IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH, 1087 ni->ni_macaddr, "shared key response", 1088 "%s", "challenge mismatch"); 1089 ic->ic_stats.is_rx_auth_fail++; 1090 estatus = IEEE80211_STATUS_CHALLENGE; 1091 goto bad; 1092 } 1093 IEEE80211_DPRINTF(ic, 1094 IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, 1095 "[%s] station authenticated (shared key)\n", 1096 ether_sprintf(ni->ni_macaddr)); 1097 ieee80211_node_authorize(ic, ni); 1098 break; 1099 default: 1100 IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH, 1101 ni->ni_macaddr, "shared key auth", 1102 "bad seq %d", seq); 1103 ic->ic_stats.is_rx_bad_auth++; 1104 estatus = IEEE80211_STATUS_SEQUENCE; 1105 goto bad; 1106 } 1107 IEEE80211_SEND_MGMT(ic, ni, 1108 IEEE80211_FC0_SUBTYPE_AUTH, seq + 1); 1109 break; 1110 1111 case IEEE80211_M_STA: 1112 if (ic->ic_state != IEEE80211_S_AUTH) 1113 return; 1114 switch (seq) { 1115 case IEEE80211_AUTH_SHARED_PASS: 1116 if (ni->ni_challenge != NULL) { 1117 FREE(ni->ni_challenge, M_DEVBUF); 1118 ni->ni_challenge = NULL; 1119 } 1120 if (status != 0) { 1121 IEEE80211_DPRINTF(ic, 1122 IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, 1123 "[%s] shared key auth failed (reason %d)\n", 1124 ether_sprintf(ieee80211_getbssid(ic, wh)), 1125 status); 1126 /* XXX can this happen? */ 1127 if (ni != ic->ic_bss) 1128 ni->ni_fails++; 1129 ic->ic_stats.is_rx_auth_fail++; 1130 return; 1131 } 1132 ieee80211_new_state(ic, IEEE80211_S_ASSOC, 1133 wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK); 1134 break; 1135 case IEEE80211_AUTH_SHARED_CHALLENGE: 1136 if (!alloc_challenge(ic, ni)) 1137 return; 1138 /* XXX could optimize by passing recvd challenge */ 1139 memcpy(ni->ni_challenge, &challenge[2], challenge[1]); 1140 IEEE80211_SEND_MGMT(ic, ni, 1141 IEEE80211_FC0_SUBTYPE_AUTH, seq + 1); 1142 break; 1143 default: 1144 IEEE80211_DISCARD(ic, IEEE80211_MSG_AUTH, 1145 wh, "shared key auth", "bad seq %d", seq); 1146 ic->ic_stats.is_rx_bad_auth++; 1147 return; 1148 } 1149 break; 1150 } 1151 return; 1152 bad: 1153 /* 1154 * Send an error response; but only when operating as an AP. 1155 */ 1156 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 1157 /* XXX hack to workaround calling convention */ 1158 ieee80211_send_error(ic, ni, wh->i_addr2, 1159 IEEE80211_FC0_SUBTYPE_AUTH, 1160 (seq + 1) | (estatus<<16)); 1161 } else if (ic->ic_opmode == IEEE80211_M_STA) { 1162 /* 1163 * Kick the state machine. This short-circuits 1164 * using the mgt frame timeout to trigger the 1165 * state transition. 1166 */ 1167 if (ic->ic_state == IEEE80211_S_AUTH) 1168 ieee80211_new_state(ic, IEEE80211_S_SCAN, 0); 1169 } 1170 } 1171 1172 /* Verify the existence and length of __elem or get out. */ 1173 #define IEEE80211_VERIFY_ELEMENT(__elem, __maxlen) do { \ 1174 if ((__elem) == NULL) { \ 1175 IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID, \ 1176 wh, ieee80211_mgt_subtype_name[subtype >> \ 1177 IEEE80211_FC0_SUBTYPE_SHIFT], \ 1178 "%s", "no " #__elem ); \ 1179 ic->ic_stats.is_rx_elem_missing++; \ 1180 return; \ 1181 } \ 1182 if ((__elem)[1] > (__maxlen)) { \ 1183 IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID, \ 1184 wh, ieee80211_mgt_subtype_name[subtype >> \ 1185 IEEE80211_FC0_SUBTYPE_SHIFT], \ 1186 "bad " #__elem " len %d", (__elem)[1]); \ 1187 ic->ic_stats.is_rx_elem_toobig++; \ 1188 return; \ 1189 } \ 1190 } while (0) 1191 1192 #define IEEE80211_VERIFY_LENGTH(_len, _minlen) do { \ 1193 if ((_len) < (_minlen)) { \ 1194 IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID, \ 1195 wh, ieee80211_mgt_subtype_name[subtype >> \ 1196 IEEE80211_FC0_SUBTYPE_SHIFT], \ 1197 "%s", "ie too short"); \ 1198 ic->ic_stats.is_rx_elem_toosmall++; \ 1199 return; \ 1200 } \ 1201 } while (0) 1202 1203 #ifdef IEEE80211_DEBUG 1204 static void 1205 ieee80211_ssid_mismatch(struct ieee80211com *ic, const char *tag, 1206 u_int8_t mac[IEEE80211_ADDR_LEN], u_int8_t *ssid) 1207 { 1208 printf("[%s] discard %s frame, ssid mismatch: ", 1209 ether_sprintf(mac), tag); 1210 ieee80211_print_essid(ssid + 2, ssid[1]); 1211 printf("\n"); 1212 } 1213 1214 #define IEEE80211_VERIFY_SSID(_ni, _ssid) do { \ 1215 if ((_ssid)[1] != 0 && \ 1216 ((_ssid)[1] != (_ni)->ni_esslen || \ 1217 memcmp((_ssid) + 2, (_ni)->ni_essid, (_ssid)[1]) != 0)) { \ 1218 if (ieee80211_msg_input(ic)) \ 1219 ieee80211_ssid_mismatch(ic, \ 1220 ieee80211_mgt_subtype_name[subtype >> \ 1221 IEEE80211_FC0_SUBTYPE_SHIFT], \ 1222 wh->i_addr2, _ssid); \ 1223 ic->ic_stats.is_rx_ssidmismatch++; \ 1224 return; \ 1225 } \ 1226 } while (0) 1227 #else /* !IEEE80211_DEBUG */ 1228 #define IEEE80211_VERIFY_SSID(_ni, _ssid) do { \ 1229 if ((_ssid)[1] != 0 && \ 1230 ((_ssid)[1] != (_ni)->ni_esslen || \ 1231 memcmp((_ssid) + 2, (_ni)->ni_essid, (_ssid)[1]) != 0)) { \ 1232 ic->ic_stats.is_rx_ssidmismatch++; \ 1233 return; \ 1234 } \ 1235 } while (0) 1236 #endif /* !IEEE80211_DEBUG */ 1237 1238 /* unalligned little endian access */ 1239 #define LE_READ_2(p) \ 1240 ((u_int16_t) \ 1241 ((((const u_int8_t *)(p))[0] ) | \ 1242 (((const u_int8_t *)(p))[1] << 8))) 1243 #define LE_READ_4(p) \ 1244 ((u_int32_t) \ 1245 ((((const u_int8_t *)(p))[0] ) | \ 1246 (((const u_int8_t *)(p))[1] << 8) | \ 1247 (((const u_int8_t *)(p))[2] << 16) | \ 1248 (((const u_int8_t *)(p))[3] << 24))) 1249 1250 static int __inline 1251 iswpaoui(const u_int8_t *frm) 1252 { 1253 return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI); 1254 } 1255 1256 static int __inline 1257 iswmeoui(const u_int8_t *frm) 1258 { 1259 return frm[1] > 3 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI); 1260 } 1261 1262 static int __inline 1263 iswmeparam(const u_int8_t *frm) 1264 { 1265 return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) && 1266 frm[6] == WME_PARAM_OUI_SUBTYPE; 1267 } 1268 1269 static int __inline 1270 iswmeinfo(const u_int8_t *frm) 1271 { 1272 return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) && 1273 frm[6] == WME_INFO_OUI_SUBTYPE; 1274 } 1275 1276 static int __inline 1277 isatherosoui(const u_int8_t *frm) 1278 { 1279 return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI); 1280 } 1281 1282 /* 1283 * Convert a WPA cipher selector OUI to an internal 1284 * cipher algorithm. Where appropriate we also 1285 * record any key length. 1286 */ 1287 static int 1288 wpa_cipher(u_int8_t *sel, u_int8_t *keylen) 1289 { 1290 #define WPA_SEL(x) (((x)<<24)|WPA_OUI) 1291 u_int32_t w = LE_READ_4(sel); 1292 1293 switch (w) { 1294 case WPA_SEL(WPA_CSE_NULL): 1295 return IEEE80211_CIPHER_NONE; 1296 case WPA_SEL(WPA_CSE_WEP40): 1297 if (keylen) 1298 *keylen = 40 / NBBY; 1299 return IEEE80211_CIPHER_WEP; 1300 case WPA_SEL(WPA_CSE_WEP104): 1301 if (keylen) 1302 *keylen = 104 / NBBY; 1303 return IEEE80211_CIPHER_WEP; 1304 case WPA_SEL(WPA_CSE_TKIP): 1305 return IEEE80211_CIPHER_TKIP; 1306 case WPA_SEL(WPA_CSE_CCMP): 1307 return IEEE80211_CIPHER_AES_CCM; 1308 } 1309 return 32; /* NB: so 1<< is discarded */ 1310 #undef WPA_SEL 1311 } 1312 1313 /* 1314 * Convert a WPA key management/authentication algorithm 1315 * to an internal code. 1316 */ 1317 static int 1318 wpa_keymgmt(u_int8_t *sel) 1319 { 1320 #define WPA_SEL(x) (((x)<<24)|WPA_OUI) 1321 u_int32_t w = LE_READ_4(sel); 1322 1323 switch (w) { 1324 case WPA_SEL(WPA_ASE_8021X_UNSPEC): 1325 return WPA_ASE_8021X_UNSPEC; 1326 case WPA_SEL(WPA_ASE_8021X_PSK): 1327 return WPA_ASE_8021X_PSK; 1328 case WPA_SEL(WPA_ASE_NONE): 1329 return WPA_ASE_NONE; 1330 } 1331 return 0; /* NB: so is discarded */ 1332 #undef WPA_SEL 1333 } 1334 1335 /* 1336 * Parse a WPA information element to collect parameters 1337 * and validate the parameters against what has been 1338 * configured for the system. 1339 */ 1340 static int 1341 ieee80211_parse_wpa(struct ieee80211com *ic, u_int8_t *frm, 1342 struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh) 1343 { 1344 u_int8_t len = frm[1]; 1345 u_int32_t w; 1346 int n; 1347 1348 /* 1349 * Check the length once for fixed parts: OUI, type, 1350 * version, mcast cipher, and 2 selector counts. 1351 * Other, variable-length data, must be checked separately. 1352 */ 1353 KASSERT(ic->ic_flags & IEEE80211_F_WPA1, 1354 ("not WPA, flags 0x%x", ic->ic_flags)); 1355 if (len < 14) { 1356 IEEE80211_DISCARD_IE(ic, 1357 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA, 1358 wh, "WPA", "too short, len %u", len); 1359 return IEEE80211_REASON_IE_INVALID; 1360 } 1361 frm += 6, len -= 4; /* NB: len is payload only */ 1362 /* NB: iswapoui already validated the OUI and type */ 1363 w = LE_READ_2(frm); 1364 if (w != WPA_VERSION) { 1365 IEEE80211_DISCARD_IE(ic, 1366 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA, 1367 wh, "WPA", "bad version %u", w); 1368 return IEEE80211_REASON_IE_INVALID; 1369 } 1370 frm += 2, len -= 2; 1371 1372 /* multicast/group cipher */ 1373 w = wpa_cipher(frm, &rsn->rsn_mcastkeylen); 1374 if (w != rsn->rsn_mcastcipher) { 1375 IEEE80211_DISCARD_IE(ic, 1376 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA, 1377 wh, "WPA", "mcast cipher mismatch; got %u, expected %u", 1378 w, rsn->rsn_mcastcipher); 1379 return IEEE80211_REASON_IE_INVALID; 1380 } 1381 frm += 4, len -= 4; 1382 1383 /* unicast ciphers */ 1384 n = LE_READ_2(frm); 1385 frm += 2, len -= 2; 1386 if (len < n*4+2) { 1387 IEEE80211_DISCARD_IE(ic, 1388 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA, 1389 wh, "WPA", "ucast cipher data too short; len %u, n %u", 1390 len, n); 1391 return IEEE80211_REASON_IE_INVALID; 1392 } 1393 w = 0; 1394 for (; n > 0; n--) { 1395 w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen); 1396 frm += 4, len -= 4; 1397 } 1398 w &= rsn->rsn_ucastcipherset; 1399 if (w == 0) { 1400 IEEE80211_DISCARD_IE(ic, 1401 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA, 1402 wh, "WPA", "%s", "ucast cipher set empty"); 1403 return IEEE80211_REASON_IE_INVALID; 1404 } 1405 if (w & (1<<IEEE80211_CIPHER_TKIP)) 1406 rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP; 1407 else 1408 rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM; 1409 1410 /* key management algorithms */ 1411 n = LE_READ_2(frm); 1412 frm += 2, len -= 2; 1413 if (len < n*4) { 1414 IEEE80211_DISCARD_IE(ic, 1415 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA, 1416 wh, "WPA", "key mgmt alg data too short; len %u, n %u", 1417 len, n); 1418 return IEEE80211_REASON_IE_INVALID; 1419 } 1420 w = 0; 1421 for (; n > 0; n--) { 1422 w |= wpa_keymgmt(frm); 1423 frm += 4, len -= 4; 1424 } 1425 w &= rsn->rsn_keymgmtset; 1426 if (w == 0) { 1427 IEEE80211_DISCARD_IE(ic, 1428 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA, 1429 wh, "WPA", "%s", "no acceptable key mgmt alg"); 1430 return IEEE80211_REASON_IE_INVALID; 1431 } 1432 if (w & WPA_ASE_8021X_UNSPEC) 1433 rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC; 1434 else 1435 rsn->rsn_keymgmt = WPA_ASE_8021X_PSK; 1436 1437 if (len > 2) /* optional capabilities */ 1438 rsn->rsn_caps = LE_READ_2(frm); 1439 1440 return 0; 1441 } 1442 1443 /* 1444 * Convert an RSN cipher selector OUI to an internal 1445 * cipher algorithm. Where appropriate we also 1446 * record any key length. 1447 */ 1448 static int 1449 rsn_cipher(u_int8_t *sel, u_int8_t *keylen) 1450 { 1451 #define RSN_SEL(x) (((x)<<24)|RSN_OUI) 1452 u_int32_t w = LE_READ_4(sel); 1453 1454 switch (w) { 1455 case RSN_SEL(RSN_CSE_NULL): 1456 return IEEE80211_CIPHER_NONE; 1457 case RSN_SEL(RSN_CSE_WEP40): 1458 if (keylen) 1459 *keylen = 40 / NBBY; 1460 return IEEE80211_CIPHER_WEP; 1461 case RSN_SEL(RSN_CSE_WEP104): 1462 if (keylen) 1463 *keylen = 104 / NBBY; 1464 return IEEE80211_CIPHER_WEP; 1465 case RSN_SEL(RSN_CSE_TKIP): 1466 return IEEE80211_CIPHER_TKIP; 1467 case RSN_SEL(RSN_CSE_CCMP): 1468 return IEEE80211_CIPHER_AES_CCM; 1469 case RSN_SEL(RSN_CSE_WRAP): 1470 return IEEE80211_CIPHER_AES_OCB; 1471 } 1472 return 32; /* NB: so 1<< is discarded */ 1473 #undef WPA_SEL 1474 } 1475 1476 /* 1477 * Convert an RSN key management/authentication algorithm 1478 * to an internal code. 1479 */ 1480 static int 1481 rsn_keymgmt(u_int8_t *sel) 1482 { 1483 #define RSN_SEL(x) (((x)<<24)|RSN_OUI) 1484 u_int32_t w = LE_READ_4(sel); 1485 1486 switch (w) { 1487 case RSN_SEL(RSN_ASE_8021X_UNSPEC): 1488 return RSN_ASE_8021X_UNSPEC; 1489 case RSN_SEL(RSN_ASE_8021X_PSK): 1490 return RSN_ASE_8021X_PSK; 1491 case RSN_SEL(RSN_ASE_NONE): 1492 return RSN_ASE_NONE; 1493 } 1494 return 0; /* NB: so is discarded */ 1495 #undef RSN_SEL 1496 } 1497 1498 /* 1499 * Parse a WPA/RSN information element to collect parameters 1500 * and validate the parameters against what has been 1501 * configured for the system. 1502 */ 1503 static int 1504 ieee80211_parse_rsn(struct ieee80211com *ic, u_int8_t *frm, 1505 struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh) 1506 { 1507 u_int8_t len = frm[1]; 1508 u_int32_t w; 1509 int n; 1510 1511 /* 1512 * Check the length once for fixed parts: 1513 * version, mcast cipher, and 2 selector counts. 1514 * Other, variable-length data, must be checked separately. 1515 */ 1516 KASSERT(ic->ic_flags & IEEE80211_F_WPA2, 1517 ("not RSN, flags 0x%x", ic->ic_flags)); 1518 if (len < 10) { 1519 IEEE80211_DISCARD_IE(ic, 1520 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA, 1521 wh, "RSN", "too short, len %u", len); 1522 return IEEE80211_REASON_IE_INVALID; 1523 } 1524 frm += 2; 1525 w = LE_READ_2(frm); 1526 if (w != RSN_VERSION) { 1527 IEEE80211_DISCARD_IE(ic, 1528 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA, 1529 wh, "RSN", "bad version %u", w); 1530 return IEEE80211_REASON_IE_INVALID; 1531 } 1532 frm += 2, len -= 2; 1533 1534 /* multicast/group cipher */ 1535 w = rsn_cipher(frm, &rsn->rsn_mcastkeylen); 1536 if (w != rsn->rsn_mcastcipher) { 1537 IEEE80211_DISCARD_IE(ic, 1538 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA, 1539 wh, "RSN", "mcast cipher mismatch; got %u, expected %u", 1540 w, rsn->rsn_mcastcipher); 1541 return IEEE80211_REASON_IE_INVALID; 1542 } 1543 frm += 4, len -= 4; 1544 1545 /* unicast ciphers */ 1546 n = LE_READ_2(frm); 1547 frm += 2, len -= 2; 1548 if (len < n*4+2) { 1549 IEEE80211_DISCARD_IE(ic, 1550 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA, 1551 wh, "RSN", "ucast cipher data too short; len %u, n %u", 1552 len, n); 1553 return IEEE80211_REASON_IE_INVALID; 1554 } 1555 w = 0; 1556 for (; n > 0; n--) { 1557 w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen); 1558 frm += 4, len -= 4; 1559 } 1560 w &= rsn->rsn_ucastcipherset; 1561 if (w == 0) { 1562 IEEE80211_DISCARD_IE(ic, 1563 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA, 1564 wh, "RSN", "%s", "ucast cipher set empty"); 1565 return IEEE80211_REASON_IE_INVALID; 1566 } 1567 if (w & (1<<IEEE80211_CIPHER_TKIP)) 1568 rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP; 1569 else 1570 rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM; 1571 1572 /* key management algorithms */ 1573 n = LE_READ_2(frm); 1574 frm += 2, len -= 2; 1575 if (len < n*4) { 1576 IEEE80211_DISCARD_IE(ic, 1577 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA, 1578 wh, "RSN", "key mgmt alg data too short; len %u, n %u", 1579 len, n); 1580 return IEEE80211_REASON_IE_INVALID; 1581 } 1582 w = 0; 1583 for (; n > 0; n--) { 1584 w |= rsn_keymgmt(frm); 1585 frm += 4, len -= 4; 1586 } 1587 w &= rsn->rsn_keymgmtset; 1588 if (w == 0) { 1589 IEEE80211_DISCARD_IE(ic, 1590 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA, 1591 wh, "RSN", "%s", "no acceptable key mgmt alg"); 1592 return IEEE80211_REASON_IE_INVALID; 1593 } 1594 if (w & RSN_ASE_8021X_UNSPEC) 1595 rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC; 1596 else 1597 rsn->rsn_keymgmt = RSN_ASE_8021X_PSK; 1598 1599 /* optional RSN capabilities */ 1600 if (len > 2) 1601 rsn->rsn_caps = LE_READ_2(frm); 1602 /* XXXPMKID */ 1603 1604 return 0; 1605 } 1606 1607 static int 1608 ieee80211_parse_wmeparams(struct ieee80211com *ic, u_int8_t *frm, 1609 const struct ieee80211_frame *wh) 1610 { 1611 #define MS(_v, _f) (((_v) & _f) >> _f##_S) 1612 struct ieee80211_wme_state *wme = &ic->ic_wme; 1613 u_int len = frm[1], qosinfo; 1614 int i; 1615 1616 if (len < sizeof(struct ieee80211_wme_param)-2) { 1617 IEEE80211_DISCARD_IE(ic, 1618 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME, 1619 wh, "WME", "too short, len %u", len); 1620 return -1; 1621 } 1622 qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)]; 1623 qosinfo &= WME_QOSINFO_COUNT; 1624 /* XXX do proper check for wraparound */ 1625 if (qosinfo == wme->wme_wmeChanParams.cap_info) 1626 return 0; 1627 frm += __offsetof(struct ieee80211_wme_param, params_acParams); 1628 for (i = 0; i < WME_NUM_AC; i++) { 1629 struct wmeParams *wmep = 1630 &wme->wme_wmeChanParams.cap_wmeParams[i]; 1631 /* NB: ACI not used */ 1632 wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM); 1633 wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN); 1634 wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN); 1635 wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX); 1636 wmep->wmep_txopLimit = LE_READ_2(frm+2); 1637 frm += 4; 1638 } 1639 wme->wme_wmeChanParams.cap_info = qosinfo; 1640 return 1; 1641 #undef MS 1642 } 1643 1644 static void 1645 ieee80211_saveie(u_int8_t **iep, const u_int8_t *ie) 1646 { 1647 u_int ielen = ie[1]+2; 1648 /* 1649 * Record information element for later use. 1650 */ 1651 if (*iep == NULL || (*iep)[1] != ie[1]) { 1652 if (*iep != NULL) 1653 FREE(*iep, M_DEVBUF); 1654 MALLOC(*iep, void*, ielen, M_DEVBUF, M_NOWAIT); 1655 } 1656 if (*iep != NULL) 1657 memcpy(*iep, ie, ielen); 1658 /* XXX note failure */ 1659 } 1660 1661 #ifdef IEEE80211_DEBUG 1662 static void 1663 dump_probe_beacon(u_int8_t subtype, int isnew, 1664 const u_int8_t mac[IEEE80211_ADDR_LEN], 1665 u_int8_t chan, u_int8_t bchan, u_int16_t capinfo, u_int16_t bintval, 1666 u_int8_t erp, u_int8_t *ssid, u_int8_t *country) 1667 { 1668 printf("[%s] %s%s on chan %u (bss chan %u) ", 1669 ether_sprintf(mac), isnew ? "new " : "", 1670 ieee80211_mgt_subtype_name[subtype >> IEEE80211_FC0_SUBTYPE_SHIFT], 1671 chan, bchan); 1672 ieee80211_print_essid(ssid + 2, ssid[1]); 1673 printf("\n"); 1674 1675 if (isnew) { 1676 printf("[%s] caps 0x%x bintval %u erp 0x%x", 1677 ether_sprintf(mac), capinfo, bintval, erp); 1678 if (country) { 1679 #ifdef __FreeBSD__ 1680 printf(" country info %*D", country[1], country+2, " "); 1681 #else 1682 int i; 1683 printf(" country info"); 1684 for (i = 0; i < country[1]; i++) 1685 printf(" %02x", country[i+2]); 1686 #endif 1687 } 1688 printf("\n"); 1689 } 1690 } 1691 #endif /* IEEE80211_DEBUG */ 1692 1693 void 1694 ieee80211_recv_mgmt(struct ieee80211com *ic, struct mbuf *m0, 1695 struct ieee80211_node *ni, 1696 int subtype, int rssi, u_int32_t rstamp) 1697 { 1698 #define ISPROBE(_st) ((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP) 1699 #define ISREASSOC(_st) ((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP) 1700 struct ieee80211_frame *wh; 1701 u_int8_t *frm, *efrm; 1702 u_int8_t *ssid, *rates, *xrates, *wpa, *wme; 1703 int reassoc, resp, allocbs; 1704 u_int8_t rate; 1705 1706 wh = mtod(m0, struct ieee80211_frame *); 1707 frm = (u_int8_t *)&wh[1]; 1708 efrm = mtod(m0, u_int8_t *) + m0->m_len; 1709 switch (subtype) { 1710 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 1711 case IEEE80211_FC0_SUBTYPE_BEACON: { 1712 u_int8_t *tstamp, *country, *tim; 1713 u_int8_t chan, bchan, fhindex, erp; 1714 u_int16_t capinfo, bintval, timoff; 1715 u_int16_t fhdwell; 1716 1717 /* 1718 * We process beacon/probe response frames: 1719 * o when scanning, or 1720 * o station mode when associated (to collect state 1721 * updates such as 802.11g slot time), or 1722 * o adhoc mode (to discover neighbors) 1723 * Frames otherwise received are discarded. 1724 */ 1725 if (!((ic->ic_flags & IEEE80211_F_SCAN) || 1726 (ic->ic_opmode == IEEE80211_M_STA && ni->ni_associd) || 1727 ic->ic_opmode == IEEE80211_M_IBSS)) { 1728 ic->ic_stats.is_rx_mgtdiscard++; 1729 return; 1730 } 1731 /* 1732 * beacon/probe response frame format 1733 * [8] time stamp 1734 * [2] beacon interval 1735 * [2] capability information 1736 * [tlv] ssid 1737 * [tlv] supported rates 1738 * [tlv] country information 1739 * [tlv] parameter set (FH/DS) 1740 * [tlv] erp information 1741 * [tlv] extended supported rates 1742 * [tlv] WME 1743 * [tlv] WPA or RSN 1744 */ 1745 IEEE80211_VERIFY_LENGTH(efrm - frm, 12); 1746 tstamp = frm; frm += 8; 1747 bintval = le16toh(*(u_int16_t *)frm); frm += 2; 1748 capinfo = le16toh(*(u_int16_t *)frm); frm += 2; 1749 ssid = rates = xrates = country = wpa = wme = tim = NULL; 1750 bchan = ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan); 1751 chan = bchan; 1752 fhdwell = 0; 1753 fhindex = 0; 1754 erp = 0; 1755 timoff = 0; 1756 while (frm < efrm) { 1757 switch (*frm) { 1758 case IEEE80211_ELEMID_SSID: 1759 ssid = frm; 1760 break; 1761 case IEEE80211_ELEMID_RATES: 1762 rates = frm; 1763 break; 1764 case IEEE80211_ELEMID_COUNTRY: 1765 country = frm; 1766 break; 1767 case IEEE80211_ELEMID_FHPARMS: 1768 if (ic->ic_phytype == IEEE80211_T_FH) { 1769 fhdwell = LE_READ_2(&frm[2]); 1770 chan = IEEE80211_FH_CHAN(frm[4], frm[5]); 1771 fhindex = frm[6]; 1772 } 1773 break; 1774 case IEEE80211_ELEMID_DSPARMS: 1775 /* 1776 * XXX hack this since depending on phytype 1777 * is problematic for multi-mode devices. 1778 */ 1779 if (ic->ic_phytype != IEEE80211_T_FH) 1780 chan = frm[2]; 1781 break; 1782 case IEEE80211_ELEMID_TIM: 1783 /* XXX ATIM? */ 1784 tim = frm; 1785 timoff = frm - mtod(m0, u_int8_t *); 1786 break; 1787 case IEEE80211_ELEMID_IBSSPARMS: 1788 break; 1789 case IEEE80211_ELEMID_XRATES: 1790 xrates = frm; 1791 break; 1792 case IEEE80211_ELEMID_ERP: 1793 if (frm[1] != 1) { 1794 IEEE80211_DISCARD_IE(ic, 1795 IEEE80211_MSG_ELEMID, wh, "ERP", 1796 "bad len %u", frm[1]); 1797 ic->ic_stats.is_rx_elem_toobig++; 1798 break; 1799 } 1800 erp = frm[2]; 1801 break; 1802 case IEEE80211_ELEMID_RSN: 1803 wpa = frm; 1804 break; 1805 case IEEE80211_ELEMID_VENDOR: 1806 if (iswpaoui(frm)) 1807 wpa = frm; 1808 else if (iswmeparam(frm) || iswmeinfo(frm)) 1809 wme = frm; 1810 /* XXX Atheros OUI support */ 1811 break; 1812 default: 1813 IEEE80211_DISCARD_IE(ic, IEEE80211_MSG_ELEMID, 1814 wh, "unhandled", 1815 "id %u, len %u", *frm, frm[1]); 1816 ic->ic_stats.is_rx_elem_unknown++; 1817 break; 1818 } 1819 frm += frm[1] + 2; 1820 } 1821 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE); 1822 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN); 1823 if ( 1824 #if IEEE80211_CHAN_MAX < 255 1825 chan > IEEE80211_CHAN_MAX || 1826 #endif 1827 isclr(ic->ic_chan_active, chan)) { 1828 IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID, 1829 wh, ieee80211_mgt_subtype_name[subtype >> 1830 IEEE80211_FC0_SUBTYPE_SHIFT], 1831 "invalid channel %u", chan); 1832 ic->ic_stats.is_rx_badchan++; 1833 return; 1834 } 1835 if (chan != bchan && ic->ic_phytype != IEEE80211_T_FH) { 1836 /* 1837 * Frame was received on a channel different from the 1838 * one indicated in the DS params element id; 1839 * silently discard it. 1840 * 1841 * NB: this can happen due to signal leakage. 1842 * But we should take it for FH phy because 1843 * the rssi value should be correct even for 1844 * different hop pattern in FH. 1845 */ 1846 IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID, 1847 wh, ieee80211_mgt_subtype_name[subtype >> 1848 IEEE80211_FC0_SUBTYPE_SHIFT], 1849 "for off-channel %u", chan); 1850 ic->ic_stats.is_rx_chanmismatch++; 1851 return; 1852 } 1853 1854 /* 1855 * Count frame now that we know it's to be processed. 1856 */ 1857 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) { 1858 ic->ic_stats.is_rx_beacon++; /* XXX remove */ 1859 IEEE80211_NODE_STAT(ni, rx_beacons); 1860 } else 1861 IEEE80211_NODE_STAT(ni, rx_proberesp); 1862 1863 /* 1864 * When operating in station mode, check for state updates. 1865 * Be careful to ignore beacons received while doing a 1866 * background scan. We consider only 11g/WMM stuff right now. 1867 */ 1868 if (ic->ic_opmode == IEEE80211_M_STA && 1869 ni->ni_associd != 0 && 1870 ((ic->ic_flags & IEEE80211_F_SCAN) == 0 || 1871 IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) { 1872 /* record tsf of last beacon */ 1873 memcpy(ni->ni_tstamp.data, tstamp, 1874 sizeof(ni->ni_tstamp)); 1875 if (ni->ni_erp != erp) { 1876 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1877 "[%s] erp change: was 0x%x, now 0x%x\n", 1878 ether_sprintf(wh->i_addr2), 1879 ni->ni_erp, erp); 1880 if (ic->ic_curmode == IEEE80211_MODE_11G && 1881 (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION)) 1882 ic->ic_flags |= IEEE80211_F_USEPROT; 1883 else 1884 ic->ic_flags &= ~IEEE80211_F_USEPROT; 1885 ni->ni_erp = erp; 1886 /* XXX statistic */ 1887 } 1888 if ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) { 1889 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 1890 "[%s] capabilities change: before 0x%x," 1891 " now 0x%x\n", 1892 ether_sprintf(wh->i_addr2), 1893 ni->ni_capinfo, capinfo); 1894 /* 1895 * NB: we assume short preamble doesn't 1896 * change dynamically 1897 */ 1898 ieee80211_set_shortslottime(ic, 1899 ic->ic_curmode == IEEE80211_MODE_11A || 1900 (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)); 1901 ni->ni_capinfo = capinfo; 1902 /* XXX statistic */ 1903 } 1904 if (wme != NULL && 1905 (ni->ni_flags & IEEE80211_NODE_QOS) && 1906 ieee80211_parse_wmeparams(ic, wme, wh) > 0) 1907 ieee80211_wme_updateparams(ic); 1908 if (tim != NULL) { 1909 struct ieee80211_tim_ie *ie = 1910 (struct ieee80211_tim_ie *) tim; 1911 1912 ni->ni_dtim_count = ie->tim_count; 1913 ni->ni_dtim_period = ie->tim_period; 1914 } 1915 /* NB: don't need the rest of this */ 1916 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) 1917 return; 1918 } 1919 1920 if (ni == ic->ic_bss) { 1921 #ifdef IEEE80211_DEBUG 1922 if (ieee80211_msg_scan(ic)) 1923 dump_probe_beacon(subtype, 1, 1924 wh->i_addr2, chan, bchan, capinfo, 1925 bintval, erp, ssid, country); 1926 #endif 1927 /* 1928 * Create a new entry. If scanning the entry goes 1929 * in the scan cache. Otherwise, be particular when 1930 * operating in adhoc mode--only take nodes marked 1931 * as ibss participants so we don't populate our 1932 * neighbor table with unintersting sta's. 1933 */ 1934 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) { 1935 if ((capinfo & IEEE80211_CAPINFO_IBSS) == 0) 1936 return; 1937 ni = ieee80211_fakeup_adhoc_node(&ic->ic_sta, 1938 wh->i_addr2); 1939 } else 1940 ni = ieee80211_dup_bss(&ic->ic_scan, wh->i_addr2); 1941 if (ni == NULL) 1942 return; 1943 ni->ni_esslen = ssid[1]; 1944 memset(ni->ni_essid, 0, sizeof(ni->ni_essid)); 1945 memcpy(ni->ni_essid, ssid + 2, ssid[1]); 1946 } else if (ssid[1] != 0 && 1947 (ISPROBE(subtype) || ni->ni_esslen == 0)) { 1948 /* 1949 * Update ESSID at probe response to adopt 1950 * hidden AP by Lucent/Cisco, which announces 1951 * null ESSID in beacon. 1952 */ 1953 #ifdef IEEE80211_DEBUG 1954 if (ieee80211_msg_scan(ic) || 1955 ieee80211_msg_debug(ic)) 1956 dump_probe_beacon(subtype, 0, 1957 wh->i_addr2, chan, bchan, capinfo, 1958 bintval, erp, ssid, country); 1959 #endif 1960 ni->ni_esslen = ssid[1]; 1961 memset(ni->ni_essid, 0, sizeof(ni->ni_essid)); 1962 memcpy(ni->ni_essid, ssid + 2, ssid[1]); 1963 } 1964 ni->ni_scangen = ic->ic_scan.nt_scangen; 1965 IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3); 1966 ni->ni_rssi = rssi; 1967 ni->ni_rstamp = rstamp; 1968 memcpy(ni->ni_tstamp.data, tstamp, sizeof(ni->ni_tstamp)); 1969 ni->ni_intval = bintval; 1970 ni->ni_capinfo = capinfo; 1971 ni->ni_chan = &ic->ic_channels[chan]; 1972 ni->ni_fhdwell = fhdwell; 1973 ni->ni_fhindex = fhindex; 1974 ni->ni_erp = erp; 1975 if (tim != NULL) { 1976 struct ieee80211_tim_ie *ie = 1977 (struct ieee80211_tim_ie *) tim; 1978 1979 ni->ni_dtim_count = ie->tim_count; 1980 ni->ni_dtim_period = ie->tim_period; 1981 } 1982 /* 1983 * Record the byte offset from the mac header to 1984 * the start of the TIM information element for 1985 * use by hardware and/or to speedup software 1986 * processing of beacon frames. 1987 */ 1988 ni->ni_timoff = timoff; 1989 /* 1990 * Record optional information elements that might be 1991 * used by applications or drivers. 1992 */ 1993 if (wme != NULL) 1994 ieee80211_saveie(&ni->ni_wme_ie, wme); 1995 if (wpa != NULL) 1996 ieee80211_saveie(&ni->ni_wpa_ie, wpa); 1997 /* NB: must be after ni_chan is setup */ 1998 ieee80211_setup_rates(ic, ni, rates, xrates, IEEE80211_F_DOSORT); 1999 break; 2000 } 2001 2002 case IEEE80211_FC0_SUBTYPE_PROBE_REQ: 2003 if (ic->ic_opmode == IEEE80211_M_STA || 2004 ic->ic_state != IEEE80211_S_RUN) { 2005 ic->ic_stats.is_rx_mgtdiscard++; 2006 return; 2007 } 2008 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) { 2009 /* frame must be directed */ 2010 ic->ic_stats.is_rx_mgtdiscard++; /* XXX stat */ 2011 return; 2012 } 2013 2014 /* 2015 * prreq frame format 2016 * [tlv] ssid 2017 * [tlv] supported rates 2018 * [tlv] extended supported rates 2019 */ 2020 ssid = rates = xrates = NULL; 2021 while (frm < efrm) { 2022 switch (*frm) { 2023 case IEEE80211_ELEMID_SSID: 2024 ssid = frm; 2025 break; 2026 case IEEE80211_ELEMID_RATES: 2027 rates = frm; 2028 break; 2029 case IEEE80211_ELEMID_XRATES: 2030 xrates = frm; 2031 break; 2032 } 2033 frm += frm[1] + 2; 2034 } 2035 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE); 2036 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN); 2037 IEEE80211_VERIFY_SSID(ic->ic_bss, ssid); 2038 if ((ic->ic_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) { 2039 IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT, 2040 wh, ieee80211_mgt_subtype_name[subtype >> 2041 IEEE80211_FC0_SUBTYPE_SHIFT], 2042 "%s", "no ssid with ssid suppression enabled"); 2043 ic->ic_stats.is_rx_ssidmismatch++; /*XXX*/ 2044 return; 2045 } 2046 2047 if (ni == ic->ic_bss) { 2048 if (ic->ic_opmode == IEEE80211_M_IBSS) { 2049 /* 2050 * XXX Cannot tell if the sender is operating 2051 * in ibss mode. But we need a new node to 2052 * send the response so blindly add them to the 2053 * neighbor table. 2054 */ 2055 ni = ieee80211_fakeup_adhoc_node(&ic->ic_sta, 2056 wh->i_addr2); 2057 } else 2058 ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2); 2059 if (ni == NULL) 2060 return; 2061 allocbs = 1; 2062 } else 2063 allocbs = 0; 2064 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2065 "[%s] recv probe req\n", ether_sprintf(wh->i_addr2)); 2066 ni->ni_rssi = rssi; 2067 ni->ni_rstamp = rstamp; 2068 rate = ieee80211_setup_rates(ic, ni, rates, xrates, 2069 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE 2070 | IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 2071 if (rate & IEEE80211_RATE_BASIC) { 2072 IEEE80211_DISCARD(ic, IEEE80211_MSG_XRATE, 2073 wh, ieee80211_mgt_subtype_name[subtype >> 2074 IEEE80211_FC0_SUBTYPE_SHIFT], 2075 "%s", "recv'd rate set invalid"); 2076 } else { 2077 IEEE80211_SEND_MGMT(ic, ni, 2078 IEEE80211_FC0_SUBTYPE_PROBE_RESP, 0); 2079 } 2080 if (allocbs && ic->ic_opmode != IEEE80211_M_IBSS) { 2081 /* reclaim immediately */ 2082 ieee80211_free_node(ni); 2083 } 2084 break; 2085 2086 case IEEE80211_FC0_SUBTYPE_AUTH: { 2087 u_int16_t algo, seq, status; 2088 /* 2089 * auth frame format 2090 * [2] algorithm 2091 * [2] sequence 2092 * [2] status 2093 * [tlv*] challenge 2094 */ 2095 IEEE80211_VERIFY_LENGTH(efrm - frm, 6); 2096 algo = le16toh(*(u_int16_t *)frm); 2097 seq = le16toh(*(u_int16_t *)(frm + 2)); 2098 status = le16toh(*(u_int16_t *)(frm + 4)); 2099 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH, 2100 "[%s] recv auth frame with algorithm %d seq %d\n", 2101 ether_sprintf(wh->i_addr2), algo, seq); 2102 /* 2103 * Consult the ACL policy module if setup. 2104 */ 2105 if (ic->ic_acl != NULL && 2106 !ic->ic_acl->iac_check(ic, wh->i_addr2)) { 2107 IEEE80211_DISCARD(ic, IEEE80211_MSG_ACL, 2108 wh, "auth", "%s", "disallowed by ACL"); 2109 ic->ic_stats.is_rx_acl++; 2110 return; 2111 } 2112 if (ic->ic_flags & IEEE80211_F_COUNTERM) { 2113 IEEE80211_DISCARD(ic, 2114 IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO, 2115 wh, "auth", "%s", "TKIP countermeasures enabled"); 2116 ic->ic_stats.is_rx_auth_countermeasures++; 2117 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 2118 IEEE80211_SEND_MGMT(ic, ni, 2119 IEEE80211_FC0_SUBTYPE_AUTH, 2120 IEEE80211_REASON_MIC_FAILURE); 2121 } 2122 return; 2123 } 2124 if (algo == IEEE80211_AUTH_ALG_SHARED) 2125 ieee80211_auth_shared(ic, wh, frm + 6, efrm, ni, rssi, 2126 rstamp, seq, status); 2127 else if (algo == IEEE80211_AUTH_ALG_OPEN) 2128 ieee80211_auth_open(ic, wh, ni, rssi, rstamp, seq, 2129 status); 2130 else { 2131 IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY, 2132 wh, "auth", "unsupported alg %d", algo); 2133 ic->ic_stats.is_rx_auth_unsupported++; 2134 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 2135 /* XXX not right */ 2136 IEEE80211_SEND_MGMT(ic, ni, 2137 IEEE80211_FC0_SUBTYPE_AUTH, 2138 (seq+1) | (IEEE80211_STATUS_ALG<<16)); 2139 } 2140 return; 2141 } 2142 break; 2143 } 2144 2145 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ: 2146 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: { 2147 u_int16_t capinfo, bintval; 2148 struct ieee80211_rsnparms rsn; 2149 u_int8_t reason; 2150 2151 if (ic->ic_opmode != IEEE80211_M_HOSTAP || 2152 ic->ic_state != IEEE80211_S_RUN) { 2153 ic->ic_stats.is_rx_mgtdiscard++; 2154 return; 2155 } 2156 2157 if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) { 2158 reassoc = 1; 2159 resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP; 2160 } else { 2161 reassoc = 0; 2162 resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP; 2163 } 2164 /* 2165 * asreq frame format 2166 * [2] capability information 2167 * [2] listen interval 2168 * [6*] current AP address (reassoc only) 2169 * [tlv] ssid 2170 * [tlv] supported rates 2171 * [tlv] extended supported rates 2172 * [tlv] WPA or RSN 2173 */ 2174 IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4)); 2175 if (!IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_bss->ni_bssid)) { 2176 IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY, 2177 wh, ieee80211_mgt_subtype_name[subtype >> 2178 IEEE80211_FC0_SUBTYPE_SHIFT], 2179 "%s", "wrong bssid"); 2180 ic->ic_stats.is_rx_assoc_bss++; 2181 return; 2182 } 2183 capinfo = le16toh(*(u_int16_t *)frm); frm += 2; 2184 bintval = le16toh(*(u_int16_t *)frm); frm += 2; 2185 if (reassoc) 2186 frm += 6; /* ignore current AP info */ 2187 ssid = rates = xrates = wpa = wme = NULL; 2188 while (frm < efrm) { 2189 switch (*frm) { 2190 case IEEE80211_ELEMID_SSID: 2191 ssid = frm; 2192 break; 2193 case IEEE80211_ELEMID_RATES: 2194 rates = frm; 2195 break; 2196 case IEEE80211_ELEMID_XRATES: 2197 xrates = frm; 2198 break; 2199 /* XXX verify only one of RSN and WPA ie's? */ 2200 case IEEE80211_ELEMID_RSN: 2201 wpa = frm; 2202 break; 2203 case IEEE80211_ELEMID_VENDOR: 2204 if (iswpaoui(frm)) { 2205 if (ic->ic_flags & IEEE80211_F_WPA1) 2206 wpa = frm; 2207 } else if (iswmeinfo(frm)) 2208 wme = frm; 2209 /* XXX Atheros OUI support */ 2210 break; 2211 } 2212 frm += frm[1] + 2; 2213 } 2214 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE); 2215 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN); 2216 IEEE80211_VERIFY_SSID(ic->ic_bss, ssid); 2217 2218 if (ni == ic->ic_bss) { 2219 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 2220 "[%s] deny %s request, sta not authenticated\n", 2221 ether_sprintf(wh->i_addr2), 2222 reassoc ? "reassoc" : "assoc"); 2223 ieee80211_send_error(ic, ni, wh->i_addr2, 2224 IEEE80211_FC0_SUBTYPE_DEAUTH, 2225 IEEE80211_REASON_ASSOC_NOT_AUTHED); 2226 ic->ic_stats.is_rx_assoc_notauth++; 2227 return; 2228 } 2229 /* assert right associstion security credentials */ 2230 if (wpa == NULL && (ic->ic_flags & IEEE80211_F_WPA)) { 2231 IEEE80211_DPRINTF(ic, 2232 IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA, 2233 "[%s] no WPA/RSN IE in association request\n", 2234 ether_sprintf(wh->i_addr2)); 2235 IEEE80211_SEND_MGMT(ic, ni, 2236 IEEE80211_FC0_SUBTYPE_DEAUTH, 2237 IEEE80211_REASON_RSN_REQUIRED); 2238 ieee80211_node_leave(ic, ni); 2239 /* XXX distinguish WPA/RSN? */ 2240 ic->ic_stats.is_rx_assoc_badwpaie++; 2241 return; 2242 } 2243 if (wpa != NULL) { 2244 /* 2245 * Parse WPA information element. Note that 2246 * we initialize the param block from the node 2247 * state so that information in the IE overrides 2248 * our defaults. The resulting parameters are 2249 * installed below after the association is assured. 2250 */ 2251 rsn = ni->ni_rsn; 2252 if (wpa[0] != IEEE80211_ELEMID_RSN) 2253 reason = ieee80211_parse_wpa(ic, wpa, &rsn, wh); 2254 else 2255 reason = ieee80211_parse_rsn(ic, wpa, &rsn, wh); 2256 if (reason != 0) { 2257 IEEE80211_SEND_MGMT(ic, ni, 2258 IEEE80211_FC0_SUBTYPE_DEAUTH, reason); 2259 ieee80211_node_leave(ic, ni); 2260 /* XXX distinguish WPA/RSN? */ 2261 ic->ic_stats.is_rx_assoc_badwpaie++; 2262 return; 2263 } 2264 IEEE80211_DPRINTF(ic, 2265 IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA, 2266 "[%s] %s ie: mc %u/%u uc %u/%u key %u caps 0x%x\n", 2267 ether_sprintf(wh->i_addr2), 2268 wpa[0] != IEEE80211_ELEMID_RSN ? "WPA" : "RSN", 2269 rsn.rsn_mcastcipher, rsn.rsn_mcastkeylen, 2270 rsn.rsn_ucastcipher, rsn.rsn_ucastkeylen, 2271 rsn.rsn_keymgmt, rsn.rsn_caps); 2272 } 2273 /* discard challenge after association */ 2274 if (ni->ni_challenge != NULL) { 2275 FREE(ni->ni_challenge, M_DEVBUF); 2276 ni->ni_challenge = NULL; 2277 } 2278 /* NB: 802.11 spec says to ignore station's privacy bit */ 2279 if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) { 2280 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 2281 "[%s] deny %s request, capability mismatch 0x%x\n", 2282 ether_sprintf(wh->i_addr2), 2283 reassoc ? "reassoc" : "assoc", capinfo); 2284 IEEE80211_SEND_MGMT(ic, ni, resp, 2285 IEEE80211_STATUS_CAPINFO); 2286 ieee80211_node_leave(ic, ni); 2287 ic->ic_stats.is_rx_assoc_capmismatch++; 2288 return; 2289 } 2290 rate = ieee80211_setup_rates(ic, ni, rates, xrates, 2291 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE | 2292 IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 2293 if (rate & IEEE80211_RATE_BASIC) { 2294 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 2295 "[%s] deny %s request, rate set mismatch\n", 2296 ether_sprintf(wh->i_addr2), 2297 reassoc ? "reassoc" : "assoc"); 2298 IEEE80211_SEND_MGMT(ic, ni, resp, 2299 IEEE80211_STATUS_BASIC_RATE); 2300 ieee80211_node_leave(ic, ni); 2301 ic->ic_stats.is_rx_assoc_norate++; 2302 return; 2303 } 2304 ni->ni_rssi = rssi; 2305 ni->ni_rstamp = rstamp; 2306 ni->ni_intval = bintval; 2307 ni->ni_capinfo = capinfo; 2308 ni->ni_chan = ic->ic_bss->ni_chan; 2309 ni->ni_fhdwell = ic->ic_bss->ni_fhdwell; 2310 ni->ni_fhindex = ic->ic_bss->ni_fhindex; 2311 if (wpa != NULL) { 2312 /* 2313 * Record WPA/RSN parameters for station, mark 2314 * node as using WPA and record information element 2315 * for applications that require it. 2316 */ 2317 ni->ni_rsn = rsn; 2318 ieee80211_saveie(&ni->ni_wpa_ie, wpa); 2319 } else if (ni->ni_wpa_ie != NULL) { 2320 /* 2321 * Flush any state from a previous association. 2322 */ 2323 FREE(ni->ni_wpa_ie, M_DEVBUF); 2324 ni->ni_wpa_ie = NULL; 2325 } 2326 if (wme != NULL) { 2327 /* 2328 * Record WME parameters for station, mark node 2329 * as capable of QoS and record information 2330 * element for applications that require it. 2331 */ 2332 ieee80211_saveie(&ni->ni_wme_ie, wme); 2333 ni->ni_flags |= IEEE80211_NODE_QOS; 2334 } else if (ni->ni_wme_ie != NULL) { 2335 /* 2336 * Flush any state from a previous association. 2337 */ 2338 FREE(ni->ni_wme_ie, M_DEVBUF); 2339 ni->ni_wme_ie = NULL; 2340 ni->ni_flags &= ~IEEE80211_NODE_QOS; 2341 } 2342 ieee80211_node_join(ic, ni, resp); 2343 break; 2344 } 2345 2346 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 2347 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: { 2348 u_int16_t capinfo, associd; 2349 u_int16_t status; 2350 2351 if (ic->ic_opmode != IEEE80211_M_STA || 2352 ic->ic_state != IEEE80211_S_ASSOC) { 2353 ic->ic_stats.is_rx_mgtdiscard++; 2354 return; 2355 } 2356 2357 /* 2358 * asresp frame format 2359 * [2] capability information 2360 * [2] status 2361 * [2] association ID 2362 * [tlv] supported rates 2363 * [tlv] extended supported rates 2364 * [tlv] WME 2365 */ 2366 IEEE80211_VERIFY_LENGTH(efrm - frm, 6); 2367 ni = ic->ic_bss; 2368 capinfo = le16toh(*(u_int16_t *)frm); 2369 frm += 2; 2370 status = le16toh(*(u_int16_t *)frm); 2371 frm += 2; 2372 if (status != 0) { 2373 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2374 "[%s] %sassoc failed (reason %d)\n", 2375 ether_sprintf(wh->i_addr2), 2376 ISREASSOC(subtype) ? "re" : "", status); 2377 if (ni != ic->ic_bss) /* XXX never true? */ 2378 ni->ni_fails++; 2379 ic->ic_stats.is_rx_auth_fail++; /* XXX */ 2380 return; 2381 } 2382 associd = le16toh(*(u_int16_t *)frm); 2383 frm += 2; 2384 2385 rates = xrates = wpa = wme = NULL; 2386 while (frm < efrm) { 2387 switch (*frm) { 2388 case IEEE80211_ELEMID_RATES: 2389 rates = frm; 2390 break; 2391 case IEEE80211_ELEMID_XRATES: 2392 xrates = frm; 2393 break; 2394 case IEEE80211_ELEMID_VENDOR: 2395 if (iswmeoui(frm)) 2396 wme = frm; 2397 /* XXX Atheros OUI support */ 2398 break; 2399 } 2400 frm += frm[1] + 2; 2401 } 2402 2403 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE); 2404 rate = ieee80211_setup_rates(ic, ni, rates, xrates, 2405 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE | 2406 IEEE80211_F_DONEGO | IEEE80211_F_DODEL); 2407 if (rate & IEEE80211_RATE_BASIC) { 2408 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2409 "[%s] %sassoc failed (rate set mismatch)\n", 2410 ether_sprintf(wh->i_addr2), 2411 ISREASSOC(subtype) ? "re" : ""); 2412 if (ni != ic->ic_bss) /* XXX never true? */ 2413 ni->ni_fails++; 2414 ic->ic_stats.is_rx_assoc_norate++; 2415 ieee80211_new_state(ic, IEEE80211_S_SCAN, 0); 2416 return; 2417 } 2418 2419 ni->ni_capinfo = capinfo; 2420 ni->ni_associd = associd; 2421 if (wme != NULL && 2422 ieee80211_parse_wmeparams(ic, wme, wh) >= 0) { 2423 ni->ni_flags |= IEEE80211_NODE_QOS; 2424 ieee80211_wme_updateparams(ic); 2425 } else 2426 ni->ni_flags &= ~IEEE80211_NODE_QOS; 2427 /* 2428 * Configure state now that we are associated. 2429 * 2430 * XXX may need different/additional driver callbacks? 2431 */ 2432 if (ic->ic_curmode == IEEE80211_MODE_11A || 2433 (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) { 2434 ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 2435 ic->ic_flags &= ~IEEE80211_F_USEBARKER; 2436 } else { 2437 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE; 2438 ic->ic_flags |= IEEE80211_F_USEBARKER; 2439 } 2440 ieee80211_set_shortslottime(ic, 2441 ic->ic_curmode == IEEE80211_MODE_11A || 2442 (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)); 2443 /* 2444 * Honor ERP protection. 2445 * 2446 * NB: ni_erp should zero for non-11g operation. 2447 * XXX check ic_curmode anyway? 2448 */ 2449 if (ic->ic_curmode == IEEE80211_MODE_11G && 2450 (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION)) 2451 ic->ic_flags |= IEEE80211_F_USEPROT; 2452 else 2453 ic->ic_flags &= ~IEEE80211_F_USEPROT; 2454 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2455 "[%s] %sassoc success: %s preamble, %s slot time%s%s\n", 2456 ether_sprintf(wh->i_addr2), 2457 ISREASSOC(subtype) ? "re" : "", 2458 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long", 2459 ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long", 2460 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "", 2461 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "" 2462 ); 2463 ieee80211_new_state(ic, IEEE80211_S_RUN, subtype); 2464 break; 2465 } 2466 2467 case IEEE80211_FC0_SUBTYPE_DEAUTH: { 2468 u_int16_t reason; 2469 2470 if (ic->ic_state == IEEE80211_S_SCAN) { 2471 ic->ic_stats.is_rx_mgtdiscard++; 2472 return; 2473 } 2474 /* 2475 * deauth frame format 2476 * [2] reason 2477 */ 2478 IEEE80211_VERIFY_LENGTH(efrm - frm, 2); 2479 reason = le16toh(*(u_int16_t *)frm); 2480 ic->ic_stats.is_rx_deauth++; 2481 IEEE80211_NODE_STAT(ni, rx_deauth); 2482 2483 IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH, 2484 "[%s] recv deauthenticate (reason %d)\n", 2485 ether_sprintf(ni->ni_macaddr), reason); 2486 switch (ic->ic_opmode) { 2487 case IEEE80211_M_STA: 2488 ieee80211_new_state(ic, IEEE80211_S_AUTH, 2489 wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK); 2490 break; 2491 case IEEE80211_M_HOSTAP: 2492 if (ni != ic->ic_bss) 2493 ieee80211_node_leave(ic, ni); 2494 break; 2495 default: 2496 ic->ic_stats.is_rx_mgtdiscard++; 2497 break; 2498 } 2499 break; 2500 } 2501 2502 case IEEE80211_FC0_SUBTYPE_DISASSOC: { 2503 u_int16_t reason; 2504 2505 if (ic->ic_state != IEEE80211_S_RUN && 2506 ic->ic_state != IEEE80211_S_ASSOC && 2507 ic->ic_state != IEEE80211_S_AUTH) { 2508 ic->ic_stats.is_rx_mgtdiscard++; 2509 return; 2510 } 2511 /* 2512 * disassoc frame format 2513 * [2] reason 2514 */ 2515 IEEE80211_VERIFY_LENGTH(efrm - frm, 2); 2516 reason = le16toh(*(u_int16_t *)frm); 2517 ic->ic_stats.is_rx_disassoc++; 2518 IEEE80211_NODE_STAT(ni, rx_disassoc); 2519 2520 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC, 2521 "[%s] recv disassociated (reason %d)\n", 2522 ether_sprintf(ni->ni_macaddr), reason); 2523 switch (ic->ic_opmode) { 2524 case IEEE80211_M_STA: 2525 ieee80211_new_state(ic, IEEE80211_S_ASSOC, 2526 wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK); 2527 break; 2528 case IEEE80211_M_HOSTAP: 2529 if (ni != ic->ic_bss) 2530 ieee80211_node_leave(ic, ni); 2531 break; 2532 default: 2533 ic->ic_stats.is_rx_mgtdiscard++; 2534 break; 2535 } 2536 break; 2537 } 2538 default: 2539 IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY, 2540 wh, "mgt", "subtype 0x%x not handled", subtype); 2541 ic->ic_stats.is_rx_badsubtype++; 2542 break; 2543 } 2544 #undef ISREASSOC 2545 #undef ISPROBE 2546 } 2547 #undef IEEE80211_VERIFY_LENGTH 2548 #undef IEEE80211_VERIFY_ELEMENT 2549 2550 /* 2551 * Handle station power-save state change. 2552 */ 2553 static void 2554 ieee80211_node_pwrsave(struct ieee80211_node *ni, int enable) 2555 { 2556 struct ieee80211com *ic = ni->ni_ic; 2557 struct mbuf *m; 2558 2559 if (enable) { 2560 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) 2561 ic->ic_ps_sta++; 2562 ni->ni_flags |= IEEE80211_NODE_PWR_MGT; 2563 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, 2564 "[%s] power save mode on, %u sta's in ps mode\n", 2565 ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta); 2566 return; 2567 } 2568 2569 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) 2570 ic->ic_ps_sta--; 2571 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT; 2572 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, 2573 "[%s] power save mode off, %u sta's in ps mode\n", 2574 ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta); 2575 /* XXX if no stations in ps mode, flush mc frames */ 2576 2577 /* 2578 * Flush queued unicast frames. 2579 */ 2580 if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0) { 2581 if (ic->ic_set_tim != NULL) 2582 ic->ic_set_tim(ic, ni, 0); /* just in case */ 2583 return; 2584 } 2585 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, 2586 "[%s] flush ps queue, %u packets queued\n", 2587 ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_SAVEQ_QLEN(ni)); 2588 for (;;) { 2589 int qlen; 2590 2591 IEEE80211_NODE_SAVEQ_DEQUEUE(ni, m, qlen); 2592 if (m == NULL) 2593 break; 2594 /* 2595 * If this is the last packet, turn off the TIM bit. 2596 * If there are more packets, set the more packets bit 2597 * in the packet dispatched to the station. 2598 */ 2599 if (qlen != 0) { 2600 struct ieee80211_frame_min *wh = 2601 mtod(m, struct ieee80211_frame_min *); 2602 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA; 2603 } 2604 /* XXX need different driver interface */ 2605 /* XXX bypasses q max */ 2606 IF_ENQUEUE(&ic->ic_ifp->if_snd, m); 2607 } 2608 if (ic->ic_set_tim != NULL) 2609 ic->ic_set_tim(ic, ni, 0); 2610 } 2611 2612 /* 2613 * Process a received ps-poll frame. 2614 */ 2615 static void 2616 ieee80211_recv_pspoll(struct ieee80211com *ic, 2617 struct ieee80211_node *ni, struct mbuf *m0) 2618 { 2619 struct ieee80211_frame_min *wh; 2620 struct mbuf *m; 2621 u_int16_t aid; 2622 int qlen; 2623 2624 wh = mtod(m0, struct ieee80211_frame_min *); 2625 if (ni->ni_associd == 0) { 2626 IEEE80211_DISCARD(ic, IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG, 2627 (struct ieee80211_frame *) wh, "ps-poll", 2628 "%s", "unassociated station"); 2629 ic->ic_stats.is_ps_unassoc++; 2630 IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH, 2631 IEEE80211_REASON_NOT_ASSOCED); 2632 return; 2633 } 2634 2635 aid = le16toh(*(u_int16_t *)wh->i_dur); 2636 if (aid != ni->ni_associd) { 2637 IEEE80211_DISCARD(ic, IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG, 2638 (struct ieee80211_frame *) wh, "ps-poll", 2639 "aid mismatch: sta aid 0x%x poll aid 0x%x", 2640 ni->ni_associd, aid); 2641 ic->ic_stats.is_ps_badaid++; 2642 IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH, 2643 IEEE80211_REASON_NOT_ASSOCED); 2644 return; 2645 } 2646 2647 /* Okay, take the first queued packet and put it out... */ 2648 IEEE80211_NODE_SAVEQ_DEQUEUE(ni, m, qlen); 2649 if (m == NULL) { 2650 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, 2651 "[%s] recv ps-poll, but queue empty\n", 2652 ether_sprintf(wh->i_addr2)); 2653 ieee80211_send_nulldata(ic, ni); 2654 ic->ic_stats.is_ps_qempty++; /* XXX node stat */ 2655 if (ic->ic_set_tim != NULL) 2656 ic->ic_set_tim(ic, ni, 0); /* just in case */ 2657 return; 2658 } 2659 /* 2660 * If there are more packets, set the more packets bit 2661 * in the packet dispatched to the station; otherwise 2662 * turn off the TIM bit. 2663 */ 2664 if (qlen != 0) { 2665 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, 2666 "[%s] recv ps-poll, send packet, %u still queued\n", 2667 ether_sprintf(ni->ni_macaddr), qlen); 2668 wh = mtod(m, struct ieee80211_frame_min *); 2669 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA; 2670 } else { 2671 IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, 2672 "[%s] recv ps-poll, send packet, queue empty\n", 2673 ether_sprintf(ni->ni_macaddr)); 2674 if (ic->ic_set_tim != NULL) 2675 ic->ic_set_tim(ic, ni, 0); 2676 } 2677 m->m_flags |= M_PWR_SAV; /* bypass PS handling */ 2678 IF_ENQUEUE(&ic->ic_ifp->if_snd, m); 2679 } 2680 2681 #ifdef IEEE80211_DEBUG 2682 /* 2683 * Debugging support. 2684 */ 2685 2686 /* 2687 * Return the bssid of a frame. 2688 */ 2689 static const u_int8_t * 2690 ieee80211_getbssid(struct ieee80211com *ic, const struct ieee80211_frame *wh) 2691 { 2692 if (ic->ic_opmode == IEEE80211_M_STA) 2693 return wh->i_addr2; 2694 if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) != IEEE80211_FC1_DIR_NODS) 2695 return wh->i_addr1; 2696 if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL) 2697 return wh->i_addr1; 2698 return wh->i_addr3; 2699 } 2700 2701 static void 2702 ieee80211_discard_frame(struct ieee80211com *ic, 2703 const struct ieee80211_frame *wh, 2704 const char *type, const char *fmt, ...) 2705 { 2706 va_list ap; 2707 2708 printf("[%s] discard ", ether_sprintf(ieee80211_getbssid(ic, wh))); 2709 if (type != NULL) 2710 printf(" %s frame, ", type); 2711 else 2712 printf(" frame, "); 2713 va_start(ap, fmt); 2714 vprintf(fmt, ap); 2715 va_end(ap); 2716 printf("\n"); 2717 } 2718 2719 static void 2720 ieee80211_discard_ie(struct ieee80211com *ic, 2721 const struct ieee80211_frame *wh, 2722 const char *type, const char *fmt, ...) 2723 { 2724 va_list ap; 2725 2726 printf("[%s] discard ", ether_sprintf(ieee80211_getbssid(ic, wh))); 2727 if (type != NULL) 2728 printf(" %s information element, ", type); 2729 else 2730 printf(" information element, "); 2731 va_start(ap, fmt); 2732 vprintf(fmt, ap); 2733 va_end(ap); 2734 printf("\n"); 2735 } 2736 2737 static void 2738 ieee80211_discard_mac(struct ieee80211com *ic, 2739 const u_int8_t mac[IEEE80211_ADDR_LEN], 2740 const char *type, const char *fmt, ...) 2741 { 2742 va_list ap; 2743 2744 printf("[%s] discard ", ether_sprintf(mac)); 2745 if (type != NULL) 2746 printf(" %s frame, ", type); 2747 else 2748 printf(" frame, "); 2749 va_start(ap, fmt); 2750 vprintf(fmt, ap); 2751 va_end(ap); 2752 printf("\n"); 2753 } 2754 #endif /* IEEE80211_DEBUG */ 2755