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