ieee80211_wds.c revision 191542
1/*- 2 * Copyright (c) 2007-2008 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 26#include <sys/cdefs.h> 27#ifdef __FreeBSD__ 28__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_wds.c 191542 2009-04-26 21:21:48Z sam $"); 29#endif 30 31/* 32 * IEEE 802.11 WDS mode support. 33 */ 34#include "opt_inet.h" 35#include "opt_wlan.h" 36 37#include <sys/param.h> 38#include <sys/systm.h> 39#include <sys/mbuf.h> 40#include <sys/malloc.h> 41#include <sys/kernel.h> 42 43#include <sys/socket.h> 44#include <sys/sockio.h> 45#include <sys/endian.h> 46#include <sys/errno.h> 47#include <sys/proc.h> 48#include <sys/sysctl.h> 49 50#include <net/if.h> 51#include <net/if_media.h> 52#include <net/if_llc.h> 53#include <net/ethernet.h> 54 55#include <net/bpf.h> 56 57#include <net80211/ieee80211_var.h> 58#include <net80211/ieee80211_wds.h> 59#include <net80211/ieee80211_input.h> 60#ifdef IEEE80211_SUPPORT_SUPERG 61#include <net80211/ieee80211_superg.h> 62#endif 63 64static void wds_vattach(struct ieee80211vap *); 65static int wds_newstate(struct ieee80211vap *, enum ieee80211_state, int); 66static int wds_input(struct ieee80211_node *ni, struct mbuf *m, 67 int rssi, int noise, uint32_t rstamp); 68static void wds_recv_mgmt(struct ieee80211_node *, struct mbuf *, 69 int subtype, int rssi, int noise, u_int32_t rstamp); 70 71void 72ieee80211_wds_attach(struct ieee80211com *ic) 73{ 74 ic->ic_vattach[IEEE80211_M_WDS] = wds_vattach; 75} 76 77void 78ieee80211_wds_detach(struct ieee80211com *ic) 79{ 80} 81 82static void 83wds_vdetach(struct ieee80211vap *vap) 84{ 85 if (vap->iv_bss != NULL) { 86 /* XXX locking? */ 87 if (vap->iv_bss->ni_wdsvap == vap) 88 vap->iv_bss->ni_wdsvap = NULL; 89 } 90} 91 92static void 93wds_vattach(struct ieee80211vap *vap) 94{ 95 vap->iv_newstate = wds_newstate; 96 vap->iv_input = wds_input; 97 vap->iv_recv_mgmt = wds_recv_mgmt; 98 vap->iv_opdetach = wds_vdetach; 99} 100 101static int 102ieee80211_create_wds(struct ieee80211vap *vap, struct ieee80211_channel *chan) 103{ 104 struct ieee80211com *ic = vap->iv_ic; 105 struct ieee80211_node_table *nt = &ic->ic_sta; 106 struct ieee80211_node *ni, *obss; 107 108 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WDS, 109 "%s: creating link to %s on channel %u\n", __func__, 110 ether_sprintf(vap->iv_des_bssid), ieee80211_chan2ieee(ic, chan)); 111 112 /* NB: vap create must specify the bssid for the link */ 113 KASSERT(vap->iv_flags & IEEE80211_F_DESBSSID, ("no bssid")); 114 /* NB: we should only be called on RUN transition */ 115 KASSERT(vap->iv_state == IEEE80211_S_RUN, ("!RUN state")); 116 117 if ((vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0) { 118 /* 119 * Dynamic/non-legacy WDS. Reference the associated 120 * station specified by the desired bssid setup at vap 121 * create. Point ni_wdsvap at the WDS vap so 4-address 122 * frames received through the associated AP vap will 123 * be dispatched upward (e.g. to a bridge) as though 124 * they arrived on the WDS vap. 125 */ 126 IEEE80211_NODE_LOCK(nt); 127 obss = NULL; 128 ni = ieee80211_find_node_locked(&ic->ic_sta, vap->iv_des_bssid); 129 if (ni == NULL) { 130 /* 131 * Node went away before we could hookup. This 132 * should be ok; no traffic will flow and a leave 133 * event will be dispatched that should cause 134 * the vap to be destroyed. 135 */ 136 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WDS, 137 "%s: station %s went away\n", 138 __func__, ether_sprintf(vap->iv_des_bssid)); 139 /* XXX stat? */ 140 } else if (ni->ni_wdsvap != NULL) { 141 /* 142 * Node already setup with a WDS vap; we cannot 143 * allow multiple references so disallow. If 144 * ni_wdsvap points at us that's ok; we should 145 * do nothing anyway. 146 */ 147 /* XXX printf instead? */ 148 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WDS, 149 "%s: station %s in use with %s\n", 150 __func__, ether_sprintf(vap->iv_des_bssid), 151 ni->ni_wdsvap->iv_ifp->if_xname); 152 /* XXX stat? */ 153 } else { 154 /* 155 * Committed to new node, setup state. 156 */ 157 obss = vap->iv_bss; 158 vap->iv_bss = ni; 159 ni->ni_wdsvap = vap; 160 } 161 IEEE80211_NODE_UNLOCK(nt); 162 if (obss != NULL) { 163 /* NB: deferred to avoid recursive lock */ 164 ieee80211_free_node(obss); 165 } 166 } else { 167 /* 168 * Legacy WDS vap setup. 169 */ 170 /* 171 * The far end does not associate so we just create 172 * create a new node and install it as the vap's 173 * bss node. We must simulate an association and 174 * authorize the port for traffic to flow. 175 * XXX check if node already in sta table? 176 */ 177 ni = ieee80211_node_create_wds(vap, vap->iv_des_bssid, chan); 178 if (ni != NULL) { 179 obss = vap->iv_bss; 180 vap->iv_bss = ieee80211_ref_node(ni); 181 ni->ni_flags |= IEEE80211_NODE_AREF; 182 if (obss != NULL) 183 ieee80211_free_node(obss); 184 /* give driver a chance to setup state like ni_txrate */ 185 if (ic->ic_newassoc != NULL) 186 ic->ic_newassoc(ni, 1); 187 /* tell the authenticator about new station */ 188 if (vap->iv_auth->ia_node_join != NULL) 189 vap->iv_auth->ia_node_join(ni); 190 if (ni->ni_authmode != IEEE80211_AUTH_8021X) 191 ieee80211_node_authorize(ni); 192 193 ieee80211_notify_node_join(ni, 1 /*newassoc*/); 194 /* XXX inject l2uf frame */ 195 } 196 } 197 198 /* 199 * Flush pending frames now that were setup. 200 */ 201 if (ni != NULL && IEEE80211_NODE_WDSQ_QLEN(ni) != 0) { 202 int8_t rssi, noise; 203 204 IEEE80211_NOTE(vap, IEEE80211_MSG_WDS, ni, 205 "flush wds queue, %u packets queued", 206 IEEE80211_NODE_WDSQ_QLEN(ni)); 207 ic->ic_node_getsignal(ni, &rssi, &noise); 208 for (;;) { 209 struct mbuf *m; 210 211 IEEE80211_NODE_WDSQ_LOCK(ni); 212 _IEEE80211_NODE_WDSQ_DEQUEUE_HEAD(ni, m); 213 IEEE80211_NODE_WDSQ_UNLOCK(ni); 214 if (m == NULL) 215 break; 216 /* XXX cheat and re-use last rstamp */ 217 ieee80211_input(ni, m, rssi, noise, ni->ni_rstamp); 218 } 219 } 220 return (ni == NULL ? ENOENT : 0); 221} 222 223/* 224 * Propagate multicast frames of an ap vap to all DWDS links. 225 * The caller is assumed to have verified this frame is multicast. 226 */ 227void 228ieee80211_dwds_mcast(struct ieee80211vap *vap0, struct mbuf *m) 229{ 230 struct ieee80211com *ic = vap0->iv_ic; 231 struct ifnet *parent = ic->ic_ifp; 232 const struct ether_header *eh = mtod(m, const struct ether_header *); 233 struct ieee80211_node *ni; 234 struct ieee80211vap *vap; 235 struct ifnet *ifp; 236 struct mbuf *mcopy; 237 int err; 238 239 KASSERT(ETHER_IS_MULTICAST(eh->ether_dhost), 240 ("%s not mcast", ether_sprintf(eh->ether_dhost))); 241 242 /* XXX locking */ 243 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 244 /* only DWDS vaps are interesting */ 245 if (vap->iv_opmode != IEEE80211_M_WDS || 246 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)) 247 continue; 248 /* if it came in this interface, don't send it back out */ 249 ifp = vap->iv_ifp; 250 if (ifp == m->m_pkthdr.rcvif) 251 continue; 252 /* 253 * Duplicate the frame and send it. 254 */ 255 mcopy = m_copypacket(m, M_DONTWAIT); 256 if (mcopy == NULL) { 257 ifp->if_oerrors++; 258 /* XXX stat + msg */ 259 continue; 260 } 261 ni = ieee80211_find_txnode(vap, eh->ether_dhost); 262 if (ni == NULL) { 263 /* NB: ieee80211_find_txnode does stat+msg */ 264 ifp->if_oerrors++; 265 m_freem(mcopy); 266 continue; 267 } 268 /* calculate priority so drivers can find the tx queue */ 269 if (ieee80211_classify(ni, mcopy)) { 270 IEEE80211_DISCARD_MAC(vap, 271 IEEE80211_MSG_OUTPUT | IEEE80211_MSG_WDS, 272 eh->ether_dhost, NULL, 273 "%s", "classification failure"); 274 vap->iv_stats.is_tx_classify++; 275 ifp->if_oerrors++; 276 m_freem(mcopy); 277 ieee80211_free_node(ni); 278 continue; 279 } 280 281 BPF_MTAP(ifp, m); /* 802.3 tx */ 282 283 /* 284 * Encapsulate the packet in prep for transmission. 285 */ 286 mcopy = ieee80211_encap(vap, ni, mcopy); 287 if (m == NULL) { 288 /* NB: stat+msg handled in ieee80211_encap */ 289 ieee80211_free_node(ni); 290 continue; 291 } 292 mcopy->m_flags |= M_MCAST; 293 mcopy->m_pkthdr.rcvif = (void *) ni; 294 295 if (bpf_peers_present(vap->iv_rawbpf)) 296 bpf_mtap(vap->iv_rawbpf, m); 297 298 err = parent->if_transmit(parent, mcopy); 299 if (err) { 300 /* NB: IFQ_HANDOFF reclaims mbuf */ 301 ifp->if_oerrors++; 302 ieee80211_free_node(ni); 303 } else 304 ifp->if_opackets++; 305 } 306} 307 308/* 309 * Handle DWDS discovery on receipt of a 4-address frame in 310 * ap mode. Queue the frame and post an event for someone 311 * to plumb the necessary WDS vap for this station. Frames 312 * received prior to the vap set running will then be reprocessed 313 * as if they were just received. 314 */ 315void 316ieee80211_dwds_discover(struct ieee80211_node *ni, struct mbuf *m) 317{ 318 struct ieee80211vap *vap = ni->ni_vap; 319 struct ieee80211com *ic = ni->ni_ic; 320 int qlen, age; 321 322 IEEE80211_NODE_WDSQ_LOCK(ni); 323 if (!_IF_QFULL(&ni->ni_wdsq)) { 324 /* 325 * Tag the frame with it's expiry time and insert 326 * it in the queue. The aging interval is 4 times 327 * the listen interval specified by the station. 328 * Frames that sit around too long are reclaimed 329 * using this information. 330 */ 331 /* XXX handle overflow? */ 332 /* XXX per/vap beacon interval? */ 333 /* NB: TU -> secs */ 334 age = ((ni->ni_intval * ic->ic_lintval) << 2) / 1024; 335 _IEEE80211_NODE_WDSQ_ENQUEUE(ni, m, qlen, age); 336 IEEE80211_NODE_WDSQ_UNLOCK(ni); 337 338 IEEE80211_NOTE(vap, IEEE80211_MSG_WDS, ni, 339 "save frame, %u now queued", qlen); 340 } else { 341 vap->iv_stats.is_dwds_qdrop++; 342 _IF_DROP(&ni->ni_wdsq); 343 IEEE80211_NODE_WDSQ_UNLOCK(ni); 344 345 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT | IEEE80211_MSG_WDS, 346 mtod(m, struct ieee80211_frame *), "wds data", 347 "pending q overflow, drops %d (len %d)", 348 ni->ni_wdsq.ifq_drops, ni->ni_wdsq.ifq_len); 349 350#ifdef IEEE80211_DEBUG 351 if (ieee80211_msg_dumppkts(vap)) 352 ieee80211_dump_pkt(ic, mtod(m, caddr_t), 353 m->m_len, -1, -1); 354#endif 355 /* XXX tail drop? */ 356 m_freem(m); 357 } 358 ieee80211_notify_wds_discover(ni); 359} 360 361/* 362 * Age frames on the WDS pending queue. The aging interval is 363 * 4 times the listen interval specified by the station. This 364 * number is factored into the age calculations when the frame 365 * is placed on the queue. We store ages as time differences 366 * so we can check and/or adjust only the head of the list. 367 * If a frame's age exceeds the threshold then discard it. 368 * The number of frames discarded is returned to the caller. 369 */ 370int 371ieee80211_node_wdsq_age(struct ieee80211_node *ni) 372{ 373#ifdef IEEE80211_DEBUG 374 struct ieee80211vap *vap = ni->ni_vap; 375#endif 376 struct mbuf *m; 377 int discard = 0; 378 379 IEEE80211_NODE_WDSQ_LOCK(ni); 380 while (_IF_POLL(&ni->ni_wdsq, m) != NULL && 381 M_AGE_GET(m) < IEEE80211_INACT_WAIT) { 382 IEEE80211_NOTE(vap, IEEE80211_MSG_WDS, ni, 383 "discard frame, age %u", M_AGE_GET(m)); 384 385 /* XXX could be optimized */ 386 _IEEE80211_NODE_WDSQ_DEQUEUE_HEAD(ni, m); 387 m_freem(m); 388 discard++; 389 } 390 if (m != NULL) 391 M_AGE_SUB(m, IEEE80211_INACT_WAIT); 392 IEEE80211_NODE_WDSQ_UNLOCK(ni); 393 394 IEEE80211_NOTE(vap, IEEE80211_MSG_WDS, ni, 395 "discard %u frames for age", discard); 396#if 0 397 IEEE80211_NODE_STAT_ADD(ni, wds_discard, discard); 398#endif 399 return discard; 400} 401 402/* 403 * IEEE80211_M_WDS vap state machine handler. 404 */ 405static int 406wds_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 407{ 408 struct ieee80211com *ic = vap->iv_ic; 409 struct ieee80211_node *ni; 410 enum ieee80211_state ostate; 411 int error; 412 413 IEEE80211_LOCK_ASSERT(ic); 414 415 ostate = vap->iv_state; 416 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s\n", __func__, 417 ieee80211_state_name[ostate], ieee80211_state_name[nstate]); 418 vap->iv_state = nstate; /* state transition */ 419 callout_stop(&vap->iv_mgtsend); /* XXX callout_drain */ 420 if (ostate != IEEE80211_S_SCAN) 421 ieee80211_cancel_scan(vap); /* background scan */ 422 ni = vap->iv_bss; /* NB: no reference held */ 423 error = 0; 424 switch (nstate) { 425 case IEEE80211_S_INIT: 426 switch (ostate) { 427 case IEEE80211_S_SCAN: 428 ieee80211_cancel_scan(vap); 429 break; 430 default: 431 break; 432 } 433 if (ostate != IEEE80211_S_INIT) { 434 /* NB: optimize INIT -> INIT case */ 435 ieee80211_reset_bss(vap); 436 } 437 break; 438 case IEEE80211_S_SCAN: 439 switch (ostate) { 440 case IEEE80211_S_INIT: 441 ieee80211_check_scan_current(vap); 442 break; 443 default: 444 break; 445 } 446 break; 447 case IEEE80211_S_RUN: 448 if (ostate == IEEE80211_S_INIT) { 449 /* 450 * Already have a channel; bypass the scan 451 * and startup immediately. 452 */ 453 error = ieee80211_create_wds(vap, ic->ic_curchan); 454 } 455 break; 456 default: 457 break; 458 } 459 return error; 460} 461 462/* 463 * Process a received frame. The node associated with the sender 464 * should be supplied. If nothing was found in the node table then 465 * the caller is assumed to supply a reference to iv_bss instead. 466 * The RSSI and a timestamp are also supplied. The RSSI data is used 467 * during AP scanning to select a AP to associate with; it can have 468 * any units so long as values have consistent units and higher values 469 * mean ``better signal''. The receive timestamp is currently not used 470 * by the 802.11 layer. 471 */ 472static int 473wds_input(struct ieee80211_node *ni, struct mbuf *m, 474 int rssi, int noise, uint32_t rstamp) 475{ 476#define SEQ_LEQ(a,b) ((int)((a)-(b)) <= 0) 477#define HAS_SEQ(type) ((type & 0x4) == 0) 478 struct ieee80211vap *vap = ni->ni_vap; 479 struct ieee80211com *ic = ni->ni_ic; 480 struct ifnet *ifp = vap->iv_ifp; 481 struct ieee80211_frame *wh; 482 struct ieee80211_key *key; 483 struct ether_header *eh; 484 int hdrspace, need_tap; 485 uint8_t dir, type, subtype, qos; 486 uint16_t rxseq; 487 488 if (m->m_flags & M_AMPDU_MPDU) { 489 /* 490 * Fastpath for A-MPDU reorder q resubmission. Frames 491 * w/ M_AMPDU_MPDU marked have already passed through 492 * here but were received out of order and been held on 493 * the reorder queue. When resubmitted they are marked 494 * with the M_AMPDU_MPDU flag and we can bypass most of 495 * the normal processing. 496 */ 497 wh = mtod(m, struct ieee80211_frame *); 498 type = IEEE80211_FC0_TYPE_DATA; 499 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 500 subtype = IEEE80211_FC0_SUBTYPE_QOS; 501 hdrspace = ieee80211_hdrspace(ic, wh); /* XXX optimize? */ 502 goto resubmit_ampdu; 503 } 504 505 KASSERT(ni != NULL, ("null node")); 506 507 need_tap = 1; /* mbuf need to be tapped. */ 508 type = -1; /* undefined */ 509 510 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) { 511 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 512 ni->ni_macaddr, NULL, 513 "too short (1): len %u", m->m_pkthdr.len); 514 vap->iv_stats.is_rx_tooshort++; 515 goto out; 516 } 517 /* 518 * Bit of a cheat here, we use a pointer for a 3-address 519 * frame format but don't reference fields past outside 520 * ieee80211_frame_min w/o first validating the data is 521 * present. 522 */ 523 wh = mtod(m, struct ieee80211_frame *); 524 525 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) != 526 IEEE80211_FC0_VERSION_0) { 527 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 528 ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]); 529 vap->iv_stats.is_rx_badversion++; 530 goto err; 531 } 532 533 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 534 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 535 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 536 537 /* NB: WDS vap's do not scan */ 538 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_addr4)) { 539 IEEE80211_DISCARD_MAC(vap, 540 IEEE80211_MSG_ANY, ni->ni_macaddr, NULL, 541 "too short (3): len %u", m->m_pkthdr.len); 542 vap->iv_stats.is_rx_tooshort++; 543 goto out; 544 } 545 /* NB: the TA is implicitly verified by finding the wds peer node */ 546 if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr) && 547 !IEEE80211_ADDR_EQ(wh->i_addr1, ifp->if_broadcastaddr)) { 548 /* not interested in */ 549 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 550 wh->i_addr1, NULL, "%s", "not to bss"); 551 vap->iv_stats.is_rx_wrongbss++; 552 goto out; 553 } 554 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi); 555 ni->ni_noise = noise; 556 ni->ni_rstamp = rstamp; 557 if (HAS_SEQ(type)) { 558 uint8_t tid = ieee80211_gettid(wh); 559 if (IEEE80211_QOS_HAS_SEQ(wh) && 560 TID_TO_WME_AC(tid) >= WME_AC_VI) 561 ic->ic_wme.wme_hipri_traffic++; 562 rxseq = le16toh(*(uint16_t *)wh->i_seq); 563 if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 && 564 (wh->i_fc[1] & IEEE80211_FC1_RETRY) && 565 SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) { 566 /* duplicate, discard */ 567 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 568 wh->i_addr1, "duplicate", 569 "seqno <%u,%u> fragno <%u,%u> tid %u", 570 rxseq >> IEEE80211_SEQ_SEQ_SHIFT, 571 ni->ni_rxseqs[tid] >> IEEE80211_SEQ_SEQ_SHIFT, 572 rxseq & IEEE80211_SEQ_FRAG_MASK, 573 ni->ni_rxseqs[tid] & IEEE80211_SEQ_FRAG_MASK, 574 tid); 575 vap->iv_stats.is_rx_dup++; 576 IEEE80211_NODE_STAT(ni, rx_dup); 577 goto out; 578 } 579 ni->ni_rxseqs[tid] = rxseq; 580 } 581 switch (type) { 582 case IEEE80211_FC0_TYPE_DATA: 583 hdrspace = ieee80211_hdrspace(ic, wh); 584 if (m->m_len < hdrspace && 585 (m = m_pullup(m, hdrspace)) == NULL) { 586 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 587 ni->ni_macaddr, NULL, 588 "data too short: expecting %u", hdrspace); 589 vap->iv_stats.is_rx_tooshort++; 590 goto out; /* XXX */ 591 } 592 if (dir != IEEE80211_FC1_DIR_DSTODS) { 593 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 594 wh, "data", "incorrect dir 0x%x", dir); 595 vap->iv_stats.is_rx_wrongdir++; 596 goto out; 597 } 598 /* 599 * Only legacy WDS traffic should take this path. 600 */ 601 if ((vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0) { 602 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 603 wh, "data", "%s", "not legacy wds"); 604 vap->iv_stats.is_rx_wrongdir++;/*XXX*/ 605 goto out; 606 } 607 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) 608 ni->ni_inact = ni->ni_inact_reload; 609 /* 610 * Handle A-MPDU re-ordering. If the frame is to be 611 * processed directly then ieee80211_ampdu_reorder 612 * will return 0; otherwise it has consumed the mbuf 613 * and we should do nothing more with it. 614 */ 615 if ((m->m_flags & M_AMPDU) && 616 ieee80211_ampdu_reorder(ni, m) != 0) { 617 m = NULL; 618 goto out; 619 } 620 resubmit_ampdu: 621 622 /* 623 * Handle privacy requirements. Note that we 624 * must not be preempted from here until after 625 * we (potentially) call ieee80211_crypto_demic; 626 * otherwise we may violate assumptions in the 627 * crypto cipher modules used to do delayed update 628 * of replay sequence numbers. 629 */ 630 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 631 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) { 632 /* 633 * Discard encrypted frames when privacy is off. 634 */ 635 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 636 wh, "WEP", "%s", "PRIVACY off"); 637 vap->iv_stats.is_rx_noprivacy++; 638 IEEE80211_NODE_STAT(ni, rx_noprivacy); 639 goto out; 640 } 641 key = ieee80211_crypto_decap(ni, m, hdrspace); 642 if (key == NULL) { 643 /* NB: stats+msgs handled in crypto_decap */ 644 IEEE80211_NODE_STAT(ni, rx_wepfail); 645 goto out; 646 } 647 wh = mtod(m, struct ieee80211_frame *); 648 wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 649 } else { 650 /* XXX M_WEP and IEEE80211_F_PRIVACY */ 651 key = NULL; 652 } 653 654 /* 655 * Save QoS bits for use below--before we strip the header. 656 */ 657 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) { 658 qos = (dir == IEEE80211_FC1_DIR_DSTODS) ? 659 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] : 660 ((struct ieee80211_qosframe *)wh)->i_qos[0]; 661 } else 662 qos = 0; 663 664 /* 665 * Next up, any fragmentation. 666 */ 667 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 668 m = ieee80211_defrag(ni, m, hdrspace); 669 if (m == NULL) { 670 /* Fragment dropped or frame not complete yet */ 671 goto out; 672 } 673 } 674 wh = NULL; /* no longer valid, catch any uses */ 675 676 /* 677 * Next strip any MSDU crypto bits. 678 */ 679 if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) { 680 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 681 ni->ni_macaddr, "data", "%s", "demic error"); 682 vap->iv_stats.is_rx_demicfail++; 683 IEEE80211_NODE_STAT(ni, rx_demicfail); 684 goto out; 685 } 686 687 /* copy to listener after decrypt */ 688 if (bpf_peers_present(vap->iv_rawbpf)) 689 bpf_mtap(vap->iv_rawbpf, m); 690 need_tap = 0; 691 692 /* 693 * Finally, strip the 802.11 header. 694 */ 695 m = ieee80211_decap(vap, m, hdrspace); 696 if (m == NULL) { 697 /* XXX mask bit to check for both */ 698 /* don't count Null data frames as errors */ 699 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA || 700 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL) 701 goto out; 702 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 703 ni->ni_macaddr, "data", "%s", "decap error"); 704 vap->iv_stats.is_rx_decap++; 705 IEEE80211_NODE_STAT(ni, rx_decap); 706 goto err; 707 } 708 eh = mtod(m, struct ether_header *); 709 if (!ieee80211_node_is_authorized(ni)) { 710 /* 711 * Deny any non-PAE frames received prior to 712 * authorization. For open/shared-key 713 * authentication the port is mark authorized 714 * after authentication completes. For 802.1x 715 * the port is not marked authorized by the 716 * authenticator until the handshake has completed. 717 */ 718 if (eh->ether_type != htons(ETHERTYPE_PAE)) { 719 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 720 eh->ether_shost, "data", 721 "unauthorized port: ether type 0x%x len %u", 722 eh->ether_type, m->m_pkthdr.len); 723 vap->iv_stats.is_rx_unauth++; 724 IEEE80211_NODE_STAT(ni, rx_unauth); 725 goto err; 726 } 727 } else { 728 /* 729 * When denying unencrypted frames, discard 730 * any non-PAE frames received without encryption. 731 */ 732 if ((vap->iv_flags & IEEE80211_F_DROPUNENC) && 733 (key == NULL && (m->m_flags & M_WEP) == 0) && 734 eh->ether_type != htons(ETHERTYPE_PAE)) { 735 /* 736 * Drop unencrypted frames. 737 */ 738 vap->iv_stats.is_rx_unencrypted++; 739 IEEE80211_NODE_STAT(ni, rx_unencrypted); 740 goto out; 741 } 742 } 743 /* XXX require HT? */ 744 if (qos & IEEE80211_QOS_AMSDU) { 745 m = ieee80211_decap_amsdu(ni, m); 746 if (m == NULL) 747 return IEEE80211_FC0_TYPE_DATA; 748 } else { 749#ifdef IEEE80211_SUPPORT_SUPERG 750 m = ieee80211_decap_fastframe(vap, ni, m); 751 if (m == NULL) 752 return IEEE80211_FC0_TYPE_DATA; 753#endif 754 } 755 ieee80211_deliver_data(vap, ni, m); 756 return IEEE80211_FC0_TYPE_DATA; 757 758 case IEEE80211_FC0_TYPE_MGT: 759 vap->iv_stats.is_rx_mgmt++; 760 IEEE80211_NODE_STAT(ni, rx_mgmt); 761 if (dir != IEEE80211_FC1_DIR_NODS) { 762 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 763 wh, "data", "incorrect dir 0x%x", dir); 764 vap->iv_stats.is_rx_wrongdir++; 765 goto err; 766 } 767 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) { 768 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 769 ni->ni_macaddr, "mgt", "too short: len %u", 770 m->m_pkthdr.len); 771 vap->iv_stats.is_rx_tooshort++; 772 goto out; 773 } 774#ifdef IEEE80211_DEBUG 775 if (ieee80211_msg_debug(vap) || ieee80211_msg_dumppkts(vap)) { 776 if_printf(ifp, "received %s from %s rssi %d\n", 777 ieee80211_mgt_subtype_name[subtype >> 778 IEEE80211_FC0_SUBTYPE_SHIFT], 779 ether_sprintf(wh->i_addr2), rssi); 780 } 781#endif 782 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 783 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 784 wh, NULL, "%s", "WEP set but not permitted"); 785 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */ 786 goto out; 787 } 788 if (bpf_peers_present(vap->iv_rawbpf)) 789 bpf_mtap(vap->iv_rawbpf, m); 790 vap->iv_recv_mgmt(ni, m, subtype, rssi, noise, rstamp); 791 m_freem(m); 792 return IEEE80211_FC0_TYPE_MGT; 793 794 case IEEE80211_FC0_TYPE_CTL: 795 vap->iv_stats.is_rx_ctl++; 796 IEEE80211_NODE_STAT(ni, rx_ctrl); 797 goto out; 798 default: 799 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 800 wh, "bad", "frame type 0x%x", type); 801 /* should not come here */ 802 break; 803 } 804err: 805 ifp->if_ierrors++; 806out: 807 if (m != NULL) { 808 if (bpf_peers_present(vap->iv_rawbpf) && need_tap) 809 bpf_mtap(vap->iv_rawbpf, m); 810 m_freem(m); 811 } 812 return type; 813#undef SEQ_LEQ 814} 815 816static void 817wds_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, 818 int subtype, int rssi, int noise, u_int32_t rstamp) 819{ 820 struct ieee80211vap *vap = ni->ni_vap; 821 struct ieee80211com *ic = ni->ni_ic; 822 struct ieee80211_frame *wh; 823 u_int8_t *frm, *efrm; 824 825 wh = mtod(m0, struct ieee80211_frame *); 826 frm = (u_int8_t *)&wh[1]; 827 efrm = mtod(m0, u_int8_t *) + m0->m_len; 828 switch (subtype) { 829 case IEEE80211_FC0_SUBTYPE_DEAUTH: 830 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 831 case IEEE80211_FC0_SUBTYPE_BEACON: 832 case IEEE80211_FC0_SUBTYPE_PROBE_REQ: 833 case IEEE80211_FC0_SUBTYPE_AUTH: 834 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ: 835 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: 836 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 837 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: 838 case IEEE80211_FC0_SUBTYPE_DISASSOC: 839 vap->iv_stats.is_rx_mgtdiscard++; 840 break; 841 case IEEE80211_FC0_SUBTYPE_ACTION: 842 if (vap->iv_state != IEEE80211_S_RUN || 843 IEEE80211_IS_MULTICAST(wh->i_addr1)) { 844 vap->iv_stats.is_rx_mgtdiscard++; 845 break; 846 } 847 ni->ni_inact = ni->ni_inact_reload; 848 if (ieee80211_parse_action(ni, m0) == 0) 849 ic->ic_recv_action(ni, frm, efrm); 850 break; 851 default: 852 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 853 wh, "mgt", "subtype 0x%x not handled", subtype); 854 vap->iv_stats.is_rx_badsubtype++; 855 break; 856 } 857} 858