1 /*- 2 * Copyright (c) 2007-2009 Sam Leffler, Errno Consulting 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 * $FreeBSD: head/sys/net80211/ieee80211_adhoc.c 203422 2010-02-03 10:07:43Z rpaulo $ 26 */ 27 28 /* 29 * IEEE 802.11 IBSS mode support. 30 */ 31 #include "opt_inet.h" 32 #include "opt_wlan.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/mbuf.h> 37 #include <sys/malloc.h> 38 #include <sys/kernel.h> 39 40 #include <sys/socket.h> 41 #include <sys/sockio.h> 42 #include <sys/endian.h> 43 #include <sys/errno.h> 44 #include <sys/proc.h> 45 #include <sys/sysctl.h> 46 47 #include <net/if.h> 48 #include <net/if_media.h> 49 #include <net/if_llc.h> 50 #include <net/ethernet.h> 51 #include <net/route.h> 52 53 #include <net/bpf.h> 54 55 #include <netproto/802_11/ieee80211_var.h> 56 #include <netproto/802_11/ieee80211_adhoc.h> 57 #include <netproto/802_11/ieee80211_input.h> 58 #ifdef IEEE80211_SUPPORT_SUPERG 59 #include <netproto/802_11/ieee80211_superg.h> 60 #endif 61 #ifdef IEEE80211_SUPPORT_TDMA 62 #include <netproto/802_11/ieee80211_tdma.h> 63 #endif 64 65 #define IEEE80211_RATE2MBS(r) (((r) & IEEE80211_RATE_VAL) / 2) 66 67 static void adhoc_vattach(struct ieee80211vap *); 68 static int adhoc_newstate(struct ieee80211vap *, enum ieee80211_state, int); 69 static int adhoc_input(struct ieee80211_node *, struct mbuf *, int, int); 70 static void adhoc_recv_mgmt(struct ieee80211_node *, struct mbuf *, 71 int subtype, int, int); 72 static void ahdemo_recv_mgmt(struct ieee80211_node *, struct mbuf *, 73 int subtype, int, int); 74 static void adhoc_recv_ctl(struct ieee80211_node *, struct mbuf *, int subtype); 75 76 void 77 ieee80211_adhoc_attach(struct ieee80211com *ic) 78 { 79 ic->ic_vattach[IEEE80211_M_IBSS] = adhoc_vattach; 80 ic->ic_vattach[IEEE80211_M_AHDEMO] = adhoc_vattach; 81 } 82 83 void 84 ieee80211_adhoc_detach(struct ieee80211com *ic) 85 { 86 } 87 88 static void 89 adhoc_vdetach(struct ieee80211vap *vap) 90 { 91 } 92 93 static void 94 adhoc_vattach(struct ieee80211vap *vap) 95 { 96 vap->iv_newstate = adhoc_newstate; 97 vap->iv_input = adhoc_input; 98 if (vap->iv_opmode == IEEE80211_M_IBSS) 99 vap->iv_recv_mgmt = adhoc_recv_mgmt; 100 else 101 vap->iv_recv_mgmt = ahdemo_recv_mgmt; 102 vap->iv_recv_ctl = adhoc_recv_ctl; 103 vap->iv_opdetach = adhoc_vdetach; 104 #ifdef IEEE80211_SUPPORT_TDMA 105 /* 106 * Throw control to tdma support. Note we do this 107 * after setting up our callbacks so it can piggyback 108 * on top of us. 109 */ 110 if (vap->iv_caps & IEEE80211_C_TDMA) 111 ieee80211_tdma_vattach(vap); 112 #endif 113 } 114 115 static void 116 sta_leave(void *arg, struct ieee80211_node *ni) 117 { 118 struct ieee80211vap *vap = arg; 119 120 if (ni->ni_vap == vap && ni != vap->iv_bss) 121 ieee80211_node_leave(ni); 122 } 123 124 /* 125 * IEEE80211_M_IBSS+IEEE80211_M_AHDEMO vap state machine handler. 126 */ 127 static int 128 adhoc_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 129 { 130 struct ieee80211com *ic = vap->iv_ic; 131 struct ieee80211_node *ni; 132 enum ieee80211_state ostate; 133 134 ostate = vap->iv_state; 135 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n", 136 __func__, ieee80211_state_name[ostate], 137 ieee80211_state_name[nstate], arg); 138 vap->iv_state = nstate; /* state transition */ 139 if (ostate != IEEE80211_S_SCAN) 140 ieee80211_cancel_scan(vap); /* background scan */ 141 ni = vap->iv_bss; /* NB: no reference held */ 142 switch (nstate) { 143 case IEEE80211_S_INIT: 144 switch (ostate) { 145 case IEEE80211_S_SCAN: 146 ieee80211_cancel_scan(vap); 147 break; 148 default: 149 break; 150 } 151 if (ostate != IEEE80211_S_INIT) { 152 /* NB: optimize INIT -> INIT case */ 153 ieee80211_reset_bss(vap); 154 } 155 break; 156 case IEEE80211_S_SCAN: 157 switch (ostate) { 158 case IEEE80211_S_RUN: /* beacon miss */ 159 /* purge station table; entries are stale */ 160 ieee80211_iterate_nodes(&ic->ic_sta, sta_leave, vap); 161 /* fall thru... */ 162 case IEEE80211_S_INIT: 163 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC && 164 !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) { 165 /* 166 * Already have a channel; bypass the 167 * scan and startup immediately. 168 */ 169 ieee80211_create_ibss(vap, vap->iv_des_chan); 170 break; 171 } 172 /* 173 * Initiate a scan. We can come here as a result 174 * of an IEEE80211_IOC_SCAN_REQ too in which case 175 * the vap will be marked with IEEE80211_FEXT_SCANREQ 176 * and the scan request parameters will be present 177 * in iv_scanreq. Otherwise we do the default. 178 */ 179 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) { 180 ieee80211_check_scan(vap, 181 vap->iv_scanreq_flags, 182 vap->iv_scanreq_duration, 183 vap->iv_scanreq_mindwell, 184 vap->iv_scanreq_maxdwell, 185 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid); 186 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ; 187 } else 188 ieee80211_check_scan_current(vap); 189 break; 190 case IEEE80211_S_SCAN: 191 /* 192 * This can happen because of a change in state 193 * that requires a reset. Trigger a new scan 194 * unless we're in manual roaming mode in which 195 * case an application must issue an explicit request. 196 */ 197 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) 198 ieee80211_check_scan_current(vap); 199 break; 200 default: 201 goto invalid; 202 } 203 break; 204 case IEEE80211_S_RUN: 205 if (vap->iv_flags & IEEE80211_F_WPA) { 206 /* XXX validate prerequisites */ 207 } 208 switch (ostate) { 209 case IEEE80211_S_SCAN: 210 #ifdef IEEE80211_DEBUG 211 if (ieee80211_msg_debug(vap)) { 212 ieee80211_note(vap, 213 "synchronized with %6D ssid ", 214 ni->ni_bssid, ":"); 215 ieee80211_print_essid(vap->iv_bss->ni_essid, 216 ni->ni_esslen); 217 /* XXX MCS/HT */ 218 kprintf(" channel %d start %uMb\n", 219 ieee80211_chan2ieee(ic, ic->ic_curchan), 220 IEEE80211_RATE2MBS(ni->ni_txrate)); 221 } 222 #endif 223 break; 224 default: 225 goto invalid; 226 } 227 /* 228 * When 802.1x is not in use mark the port authorized 229 * at this point so traffic can flow. 230 */ 231 if (ni->ni_authmode != IEEE80211_AUTH_8021X) 232 ieee80211_node_authorize(ni); 233 /* 234 * Fake association when joining an existing bss. 235 */ 236 if (!IEEE80211_ADDR_EQ(ni->ni_macaddr, vap->iv_myaddr) && 237 ic->ic_newassoc != NULL) 238 ic->ic_newassoc(ni, ostate != IEEE80211_S_RUN); 239 break; 240 case IEEE80211_S_SLEEP: 241 ieee80211_sta_pwrsave(vap, 0); 242 break; 243 default: 244 invalid: 245 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, 246 "%s: unexpected state transition %s -> %s\n", __func__, 247 ieee80211_state_name[ostate], ieee80211_state_name[nstate]); 248 break; 249 } 250 return 0; 251 } 252 253 /* 254 * Decide if a received management frame should be 255 * printed when debugging is enabled. This filters some 256 * of the less interesting frames that come frequently 257 * (e.g. beacons). 258 */ 259 static __inline int 260 doprint(struct ieee80211vap *vap, int subtype) 261 { 262 switch (subtype) { 263 case IEEE80211_FC0_SUBTYPE_BEACON: 264 return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN); 265 case IEEE80211_FC0_SUBTYPE_PROBE_REQ: 266 return 1; 267 } 268 return 1; 269 } 270 271 /* 272 * Process a received frame. The node associated with the sender 273 * should be supplied. If nothing was found in the node table then 274 * the caller is assumed to supply a reference to iv_bss instead. 275 * The RSSI and a timestamp are also supplied. The RSSI data is used 276 * during AP scanning to select a AP to associate with; it can have 277 * any units so long as values have consistent units and higher values 278 * mean ``better signal''. The receive timestamp is currently not used 279 * by the 802.11 layer. 280 */ 281 static int 282 adhoc_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf) 283 { 284 #define SEQ_LEQ(a,b) ((int)((a)-(b)) <= 0) 285 #define HAS_SEQ(type) ((type & 0x4) == 0) 286 struct ieee80211vap *vap = ni->ni_vap; 287 struct ieee80211com *ic = ni->ni_ic; 288 struct ifnet *ifp = vap->iv_ifp; 289 struct ieee80211_frame *wh; 290 struct ieee80211_key *key; 291 struct ether_header *eh; 292 int hdrspace, need_tap = 1; /* mbuf need to be tapped. */ 293 uint8_t dir, type, subtype, qos; 294 uint8_t *bssid; 295 uint16_t rxseq; 296 297 if (m->m_flags & M_AMPDU_MPDU) { 298 /* 299 * Fastpath for A-MPDU reorder q resubmission. Frames 300 * w/ M_AMPDU_MPDU marked have already passed through 301 * here but were received out of order and been held on 302 * the reorder queue. When resubmitted they are marked 303 * with the M_AMPDU_MPDU flag and we can bypass most of 304 * the normal processing. 305 */ 306 wh = mtod(m, struct ieee80211_frame *); 307 type = IEEE80211_FC0_TYPE_DATA; 308 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 309 subtype = IEEE80211_FC0_SUBTYPE_QOS; 310 hdrspace = ieee80211_hdrspace(ic, wh); /* XXX optimize? */ 311 goto resubmit_ampdu; 312 } 313 314 KASSERT(ni != NULL, ("null node")); 315 ni->ni_inact = ni->ni_inact_reload; 316 317 type = -1; /* undefined */ 318 319 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) { 320 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 321 ni->ni_macaddr, NULL, 322 "too short (1): len %u", m->m_pkthdr.len); 323 vap->iv_stats.is_rx_tooshort++; 324 goto out; 325 } 326 /* 327 * Bit of a cheat here, we use a pointer for a 3-address 328 * frame format but don't reference fields past outside 329 * ieee80211_frame_min w/o first validating the data is 330 * present. 331 */ 332 wh = mtod(m, struct ieee80211_frame *); 333 334 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) != 335 IEEE80211_FC0_VERSION_0) { 336 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 337 ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x", 338 wh->i_fc[0], wh->i_fc[1]); 339 vap->iv_stats.is_rx_badversion++; 340 goto err; 341 } 342 343 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 344 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 345 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 346 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) { 347 if (dir != IEEE80211_FC1_DIR_NODS) 348 bssid = wh->i_addr1; 349 else if (type == IEEE80211_FC0_TYPE_CTL) 350 bssid = wh->i_addr1; 351 else { 352 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) { 353 IEEE80211_DISCARD_MAC(vap, 354 IEEE80211_MSG_ANY, ni->ni_macaddr, 355 NULL, "too short (2): len %u", 356 m->m_pkthdr.len); 357 vap->iv_stats.is_rx_tooshort++; 358 goto out; 359 } 360 bssid = wh->i_addr3; 361 } 362 /* 363 * Validate the bssid. 364 */ 365 if (!IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) && 366 !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) { 367 /* not interested in */ 368 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 369 bssid, NULL, "%s", "not to bss"); 370 vap->iv_stats.is_rx_wrongbss++; 371 goto out; 372 } 373 /* 374 * Data frame, cons up a node when it doesn't 375 * exist. This should probably done after an ACL check. 376 */ 377 if (type == IEEE80211_FC0_TYPE_DATA && 378 ni == vap->iv_bss && 379 !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) { 380 /* 381 * Beware of frames that come in too early; we 382 * can receive broadcast frames and creating sta 383 * entries will blow up because there is no bss 384 * channel yet. 385 */ 386 if (vap->iv_state != IEEE80211_S_RUN) { 387 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 388 wh, "data", "not in RUN state (%s)", 389 ieee80211_state_name[vap->iv_state]); 390 vap->iv_stats.is_rx_badstate++; 391 goto err; 392 } 393 /* 394 * Fake up a node for this newly 395 * discovered member of the IBSS. 396 */ 397 ni = ieee80211_fakeup_adhoc_node(vap, wh->i_addr2); 398 if (ni == NULL) { 399 /* NB: stat kept for alloc failure */ 400 goto err; 401 } 402 } 403 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi); 404 ni->ni_noise = nf; 405 if (HAS_SEQ(type)) { 406 uint8_t tid = ieee80211_gettid(wh); 407 if (IEEE80211_QOS_HAS_SEQ(wh) && 408 TID_TO_WME_AC(tid) >= WME_AC_VI) 409 ic->ic_wme.wme_hipri_traffic++; 410 rxseq = le16toh(*(uint16_t *)wh->i_seq); 411 if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 && 412 (wh->i_fc[1] & IEEE80211_FC1_RETRY) && 413 SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) { 414 /* duplicate, discard */ 415 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 416 bssid, "duplicate", 417 "seqno <%u,%u> fragno <%u,%u> tid %u", 418 rxseq >> IEEE80211_SEQ_SEQ_SHIFT, 419 ni->ni_rxseqs[tid] >> 420 IEEE80211_SEQ_SEQ_SHIFT, 421 rxseq & IEEE80211_SEQ_FRAG_MASK, 422 ni->ni_rxseqs[tid] & 423 IEEE80211_SEQ_FRAG_MASK, 424 tid); 425 vap->iv_stats.is_rx_dup++; 426 IEEE80211_NODE_STAT(ni, rx_dup); 427 goto out; 428 } 429 ni->ni_rxseqs[tid] = rxseq; 430 } 431 } 432 433 switch (type) { 434 case IEEE80211_FC0_TYPE_DATA: 435 hdrspace = ieee80211_hdrspace(ic, wh); 436 if (m->m_len < hdrspace && 437 (m = m_pullup(m, hdrspace)) == NULL) { 438 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 439 ni->ni_macaddr, NULL, 440 "data too short: expecting %u", hdrspace); 441 vap->iv_stats.is_rx_tooshort++; 442 goto out; /* XXX */ 443 } 444 if (dir != IEEE80211_FC1_DIR_NODS) { 445 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 446 wh, "data", "incorrect dir 0x%x", dir); 447 vap->iv_stats.is_rx_wrongdir++; 448 goto out; 449 } 450 /* XXX no power-save support */ 451 452 /* 453 * Handle A-MPDU re-ordering. If the frame is to be 454 * processed directly then ieee80211_ampdu_reorder 455 * will return 0; otherwise it has consumed the mbuf 456 * and we should do nothing more with it. 457 */ 458 if ((m->m_flags & M_AMPDU) && 459 ieee80211_ampdu_reorder(ni, m) != 0) { 460 m = NULL; 461 goto out; 462 } 463 resubmit_ampdu: 464 465 /* 466 * Handle privacy requirements. Note that we 467 * must not be preempted from here until after 468 * we (potentially) call ieee80211_crypto_demic; 469 * otherwise we may violate assumptions in the 470 * crypto cipher modules used to do delayed update 471 * of replay sequence numbers. 472 */ 473 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 474 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) { 475 /* 476 * Discard encrypted frames when privacy is off. 477 */ 478 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 479 wh, "WEP", "%s", "PRIVACY off"); 480 vap->iv_stats.is_rx_noprivacy++; 481 IEEE80211_NODE_STAT(ni, rx_noprivacy); 482 goto out; 483 } 484 key = ieee80211_crypto_decap(ni, m, hdrspace); 485 if (key == NULL) { 486 /* NB: stats+msgs handled in crypto_decap */ 487 IEEE80211_NODE_STAT(ni, rx_wepfail); 488 goto out; 489 } 490 wh = mtod(m, struct ieee80211_frame *); 491 wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 492 } else { 493 /* XXX M_WEP and IEEE80211_F_PRIVACY */ 494 key = NULL; 495 } 496 497 /* 498 * Save QoS bits for use below--before we strip the header. 499 */ 500 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) { 501 qos = (dir == IEEE80211_FC1_DIR_DSTODS) ? 502 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] : 503 ((struct ieee80211_qosframe *)wh)->i_qos[0]; 504 } else 505 qos = 0; 506 507 /* 508 * Next up, any fragmentation. 509 */ 510 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 511 m = ieee80211_defrag(ni, m, hdrspace); 512 if (m == NULL) { 513 /* Fragment dropped or frame not complete yet */ 514 goto out; 515 } 516 } 517 wh = NULL; /* no longer valid, catch any uses */ 518 519 /* 520 * Next strip any MSDU crypto bits. 521 */ 522 if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) { 523 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 524 ni->ni_macaddr, "data", "%s", "demic error"); 525 vap->iv_stats.is_rx_demicfail++; 526 IEEE80211_NODE_STAT(ni, rx_demicfail); 527 goto out; 528 } 529 530 /* copy to listener after decrypt */ 531 if (ieee80211_radiotap_active_vap(vap)) 532 ieee80211_radiotap_rx(vap, m); 533 need_tap = 0; 534 535 /* 536 * Finally, strip the 802.11 header. 537 */ 538 m = ieee80211_decap(vap, m, hdrspace); 539 if (m == NULL) { 540 /* XXX mask bit to check for both */ 541 /* don't count Null data frames as errors */ 542 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA || 543 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL) 544 goto out; 545 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 546 ni->ni_macaddr, "data", "%s", "decap error"); 547 vap->iv_stats.is_rx_decap++; 548 IEEE80211_NODE_STAT(ni, rx_decap); 549 goto err; 550 } 551 eh = mtod(m, struct ether_header *); 552 if (!ieee80211_node_is_authorized(ni)) { 553 /* 554 * Deny any non-PAE frames received prior to 555 * authorization. For open/shared-key 556 * authentication the port is mark authorized 557 * after authentication completes. For 802.1x 558 * the port is not marked authorized by the 559 * authenticator until the handshake has completed. 560 */ 561 if (eh->ether_type != htons(ETHERTYPE_PAE)) { 562 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 563 eh->ether_shost, "data", 564 "unauthorized port: ether type 0x%x len %u", 565 eh->ether_type, m->m_pkthdr.len); 566 vap->iv_stats.is_rx_unauth++; 567 IEEE80211_NODE_STAT(ni, rx_unauth); 568 goto err; 569 } 570 } else { 571 /* 572 * When denying unencrypted frames, discard 573 * any non-PAE frames received without encryption. 574 */ 575 if ((vap->iv_flags & IEEE80211_F_DROPUNENC) && 576 (key == NULL && (m->m_flags & M_WEP) == 0) && 577 eh->ether_type != htons(ETHERTYPE_PAE)) { 578 /* 579 * Drop unencrypted frames. 580 */ 581 vap->iv_stats.is_rx_unencrypted++; 582 IEEE80211_NODE_STAT(ni, rx_unencrypted); 583 goto out; 584 } 585 } 586 /* XXX require HT? */ 587 if (qos & IEEE80211_QOS_AMSDU) { 588 m = ieee80211_decap_amsdu(ni, m); 589 if (m == NULL) 590 return IEEE80211_FC0_TYPE_DATA; 591 } else { 592 #ifdef IEEE80211_SUPPORT_SUPERG 593 m = ieee80211_decap_fastframe(vap, ni, m); 594 if (m == NULL) 595 return IEEE80211_FC0_TYPE_DATA; 596 #endif 597 } 598 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL) 599 ieee80211_deliver_data(ni->ni_wdsvap, ni, m); 600 else 601 ieee80211_deliver_data(vap, ni, m); 602 return IEEE80211_FC0_TYPE_DATA; 603 604 case IEEE80211_FC0_TYPE_MGT: 605 vap->iv_stats.is_rx_mgmt++; 606 IEEE80211_NODE_STAT(ni, rx_mgmt); 607 if (dir != IEEE80211_FC1_DIR_NODS) { 608 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 609 wh, "data", "incorrect dir 0x%x", dir); 610 vap->iv_stats.is_rx_wrongdir++; 611 goto err; 612 } 613 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) { 614 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 615 ni->ni_macaddr, "mgt", "too short: len %u", 616 m->m_pkthdr.len); 617 vap->iv_stats.is_rx_tooshort++; 618 goto out; 619 } 620 #ifdef IEEE80211_DEBUG 621 if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) || 622 ieee80211_msg_dumppkts(vap)) { 623 if_printf(ifp, "received %s from %6D rssi %d\n", 624 ieee80211_mgt_subtype_name[subtype >> 625 IEEE80211_FC0_SUBTYPE_SHIFT], 626 wh->i_addr2, ":", rssi); 627 } 628 #endif 629 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 630 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 631 wh, NULL, "%s", "WEP set but not permitted"); 632 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */ 633 goto out; 634 } 635 vap->iv_recv_mgmt(ni, m, subtype, rssi, nf); 636 goto out; 637 638 case IEEE80211_FC0_TYPE_CTL: 639 vap->iv_stats.is_rx_ctl++; 640 IEEE80211_NODE_STAT(ni, rx_ctrl); 641 vap->iv_recv_ctl(ni, m, subtype); 642 goto out; 643 644 default: 645 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 646 wh, "bad", "frame type 0x%x", type); 647 /* should not come here */ 648 break; 649 } 650 err: 651 ifp->if_ierrors++; 652 out: 653 if (m != NULL) { 654 if (need_tap && ieee80211_radiotap_active_vap(vap)) 655 ieee80211_radiotap_rx(vap, m); 656 m_freem(m); 657 } 658 return type; 659 #undef SEQ_LEQ 660 } 661 662 static int 663 is11bclient(const uint8_t *rates, const uint8_t *xrates) 664 { 665 static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11); 666 int i; 667 668 /* NB: the 11b clients we care about will not have xrates */ 669 if (xrates != NULL || rates == NULL) 670 return 0; 671 for (i = 0; i < rates[1]; i++) { 672 int r = rates[2+i] & IEEE80211_RATE_VAL; 673 if (r > 2*11 || ((1<<r) & brates) == 0) 674 return 0; 675 } 676 return 1; 677 } 678 679 static void 680 adhoc_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, 681 int subtype, int rssi, int nf) 682 { 683 struct ieee80211vap *vap = ni->ni_vap; 684 struct ieee80211com *ic = ni->ni_ic; 685 struct ieee80211_frame *wh; 686 uint8_t *frm, *efrm, *sfrm; 687 uint8_t *ssid, *rates, *xrates; 688 689 wh = mtod(m0, struct ieee80211_frame *); 690 frm = (uint8_t *)&wh[1]; 691 efrm = mtod(m0, uint8_t *) + m0->m_len; 692 switch (subtype) { 693 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 694 case IEEE80211_FC0_SUBTYPE_BEACON: { 695 struct ieee80211_scanparams scan; 696 /* 697 * We process beacon/probe response 698 * frames to discover neighbors. 699 */ 700 if (ieee80211_parse_beacon(ni, m0, &scan) != 0) 701 return; 702 /* 703 * Count frame now that we know it's to be processed. 704 */ 705 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) { 706 vap->iv_stats.is_rx_beacon++; /* XXX remove */ 707 IEEE80211_NODE_STAT(ni, rx_beacons); 708 } else 709 IEEE80211_NODE_STAT(ni, rx_proberesp); 710 /* 711 * If scanning, just pass information to the scan module. 712 */ 713 if (ic->ic_flags & IEEE80211_F_SCAN) { 714 if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) { 715 /* 716 * Actively scanning a channel marked passive; 717 * send a probe request now that we know there 718 * is 802.11 traffic present. 719 * 720 * XXX check if the beacon we recv'd gives 721 * us what we need and suppress the probe req 722 */ 723 ieee80211_probe_curchan(vap, 1); 724 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN; 725 } 726 ieee80211_add_scan(vap, &scan, wh, subtype, rssi, nf); 727 return; 728 } 729 if (scan.capinfo & IEEE80211_CAPINFO_IBSS) { 730 if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) { 731 /* 732 * Create a new entry in the neighbor table. 733 */ 734 ni = ieee80211_add_neighbor(vap, wh, &scan); 735 } else if (ni->ni_capinfo == 0) { 736 /* 737 * Update faked node created on transmit. 738 * Note this also updates the tsf. 739 */ 740 ieee80211_init_neighbor(ni, wh, &scan); 741 } else { 742 /* 743 * Record tsf for potential resync. 744 */ 745 memcpy(ni->ni_tstamp.data, scan.tstamp, 746 sizeof(ni->ni_tstamp)); 747 } 748 if (ni != NULL) { 749 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi); 750 ni->ni_noise = nf; 751 } 752 } 753 break; 754 } 755 756 case IEEE80211_FC0_SUBTYPE_PROBE_REQ: 757 if (vap->iv_state != IEEE80211_S_RUN) { 758 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 759 wh, NULL, "wrong state %s", 760 ieee80211_state_name[vap->iv_state]); 761 vap->iv_stats.is_rx_mgtdiscard++; 762 return; 763 } 764 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) { 765 /* frame must be directed */ 766 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 767 wh, NULL, "%s", "not unicast"); 768 vap->iv_stats.is_rx_mgtdiscard++; /* XXX stat */ 769 return; 770 } 771 772 /* 773 * prreq frame format 774 * [tlv] ssid 775 * [tlv] supported rates 776 * [tlv] extended supported rates 777 */ 778 ssid = rates = xrates = NULL; 779 sfrm = frm; 780 while (efrm - frm > 1) { 781 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return); 782 switch (*frm) { 783 case IEEE80211_ELEMID_SSID: 784 ssid = frm; 785 break; 786 case IEEE80211_ELEMID_RATES: 787 rates = frm; 788 break; 789 case IEEE80211_ELEMID_XRATES: 790 xrates = frm; 791 break; 792 } 793 frm += frm[1] + 2; 794 } 795 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return); 796 if (xrates != NULL) 797 IEEE80211_VERIFY_ELEMENT(xrates, 798 IEEE80211_RATE_MAXSIZE - rates[1], return); 799 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return); 800 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return); 801 if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) { 802 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 803 wh, NULL, 804 "%s", "no ssid with ssid suppression enabled"); 805 vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/ 806 return; 807 } 808 809 /* XXX find a better class or define it's own */ 810 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2, 811 "%s", "recv probe req"); 812 /* 813 * Some legacy 11b clients cannot hack a complete 814 * probe response frame. When the request includes 815 * only a bare-bones rate set, communicate this to 816 * the transmit side. 817 */ 818 ieee80211_send_proberesp(vap, wh->i_addr2, 819 is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0); 820 break; 821 822 case IEEE80211_FC0_SUBTYPE_ACTION: { 823 const struct ieee80211_action *ia; 824 825 if (vap->iv_state != IEEE80211_S_RUN) { 826 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 827 wh, NULL, "wrong state %s", 828 ieee80211_state_name[vap->iv_state]); 829 vap->iv_stats.is_rx_mgtdiscard++; 830 return; 831 } 832 /* 833 * action frame format: 834 * [1] category 835 * [1] action 836 * [tlv] parameters 837 */ 838 IEEE80211_VERIFY_LENGTH(efrm - frm, 839 sizeof(struct ieee80211_action), return); 840 ia = (const struct ieee80211_action *) frm; 841 842 vap->iv_stats.is_rx_action++; 843 IEEE80211_NODE_STAT(ni, rx_action); 844 845 /* verify frame payloads but defer processing */ 846 /* XXX maybe push this to method */ 847 switch (ia->ia_category) { 848 case IEEE80211_ACTION_CAT_BA: 849 switch (ia->ia_action) { 850 case IEEE80211_ACTION_BA_ADDBA_REQUEST: 851 IEEE80211_VERIFY_LENGTH(efrm - frm, 852 sizeof(struct ieee80211_action_ba_addbarequest), 853 return); 854 break; 855 case IEEE80211_ACTION_BA_ADDBA_RESPONSE: 856 IEEE80211_VERIFY_LENGTH(efrm - frm, 857 sizeof(struct ieee80211_action_ba_addbaresponse), 858 return); 859 break; 860 case IEEE80211_ACTION_BA_DELBA: 861 IEEE80211_VERIFY_LENGTH(efrm - frm, 862 sizeof(struct ieee80211_action_ba_delba), 863 return); 864 break; 865 } 866 break; 867 case IEEE80211_ACTION_CAT_HT: 868 switch (ia->ia_action) { 869 case IEEE80211_ACTION_HT_TXCHWIDTH: 870 IEEE80211_VERIFY_LENGTH(efrm - frm, 871 sizeof(struct ieee80211_action_ht_txchwidth), 872 return); 873 break; 874 } 875 break; 876 } 877 ic->ic_recv_action(ni, wh, frm, efrm); 878 break; 879 } 880 881 case IEEE80211_FC0_SUBTYPE_AUTH: 882 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ: 883 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: 884 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 885 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: 886 case IEEE80211_FC0_SUBTYPE_DEAUTH: 887 case IEEE80211_FC0_SUBTYPE_DISASSOC: 888 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 889 wh, NULL, "%s", "not handled"); 890 vap->iv_stats.is_rx_mgtdiscard++; 891 return; 892 893 default: 894 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 895 wh, "mgt", "subtype 0x%x not handled", subtype); 896 vap->iv_stats.is_rx_badsubtype++; 897 break; 898 } 899 } 900 #undef IEEE80211_VERIFY_LENGTH 901 #undef IEEE80211_VERIFY_ELEMENT 902 903 static void 904 ahdemo_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, 905 int subtype, int rssi, int nf) 906 { 907 struct ieee80211vap *vap = ni->ni_vap; 908 struct ieee80211com *ic = ni->ni_ic; 909 910 /* 911 * Process management frames when scanning; useful for doing 912 * a site-survey. 913 */ 914 if (ic->ic_flags & IEEE80211_F_SCAN) 915 adhoc_recv_mgmt(ni, m0, subtype, rssi, nf); 916 else 917 vap->iv_stats.is_rx_mgtdiscard++; 918 } 919 920 static void 921 adhoc_recv_ctl(struct ieee80211_node *ni, struct mbuf *m0, int subtype) 922 { 923 } 924