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