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