1178354Ssam/*- 2178354Ssam * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting 3178354Ssam * All rights reserved. 4178354Ssam * 5178354Ssam * Redistribution and use in source and binary forms, with or without 6178354Ssam * modification, are permitted provided that the following conditions 7178354Ssam * are met: 8178354Ssam * 1. Redistributions of source code must retain the above copyright 9178354Ssam * notice, this list of conditions and the following disclaimer. 10178354Ssam * 2. Redistributions in binary form must reproduce the above copyright 11178354Ssam * notice, this list of conditions and the following disclaimer in the 12178354Ssam * documentation and/or other materials provided with the distribution. 13178354Ssam * 14178354Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15178354Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16178354Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17178354Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18178354Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19178354Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20178354Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21178354Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22178354Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23178354Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24178354Ssam */ 25178354Ssam 26178354Ssam#include <sys/cdefs.h> 27178354Ssam#ifdef __FreeBSD__ 28178354Ssam__FBSDID("$FreeBSD$"); 29178354Ssam#endif 30178354Ssam 31178354Ssam/* 32178354Ssam * IEEE 802.11 WDS mode support. 33178354Ssam */ 34178354Ssam#include "opt_inet.h" 35178354Ssam#include "opt_wlan.h" 36178354Ssam 37178354Ssam#include <sys/param.h> 38178354Ssam#include <sys/systm.h> 39178354Ssam#include <sys/mbuf.h> 40178354Ssam#include <sys/malloc.h> 41178354Ssam#include <sys/kernel.h> 42178354Ssam 43178354Ssam#include <sys/socket.h> 44178354Ssam#include <sys/sockio.h> 45178354Ssam#include <sys/endian.h> 46178354Ssam#include <sys/errno.h> 47178354Ssam#include <sys/proc.h> 48178354Ssam#include <sys/sysctl.h> 49178354Ssam 50178354Ssam#include <net/if.h> 51178354Ssam#include <net/if_media.h> 52178354Ssam#include <net/if_llc.h> 53178354Ssam#include <net/ethernet.h> 54178354Ssam 55178354Ssam#include <net/bpf.h> 56178354Ssam 57178354Ssam#include <net80211/ieee80211_var.h> 58178354Ssam#include <net80211/ieee80211_wds.h> 59178354Ssam#include <net80211/ieee80211_input.h> 60190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG 61190391Ssam#include <net80211/ieee80211_superg.h> 62190391Ssam#endif 63178354Ssam 64178354Ssamstatic void wds_vattach(struct ieee80211vap *); 65178354Ssamstatic int wds_newstate(struct ieee80211vap *, enum ieee80211_state, int); 66192468Ssamstatic int wds_input(struct ieee80211_node *ni, struct mbuf *m, int, int); 67178354Ssamstatic void wds_recv_mgmt(struct ieee80211_node *, struct mbuf *, 68192468Ssam int subtype, int, int); 69178354Ssam 70178354Ssamvoid 71178354Ssamieee80211_wds_attach(struct ieee80211com *ic) 72178354Ssam{ 73178354Ssam ic->ic_vattach[IEEE80211_M_WDS] = wds_vattach; 74178354Ssam} 75178354Ssam 76178354Ssamvoid 77178354Ssamieee80211_wds_detach(struct ieee80211com *ic) 78178354Ssam{ 79178354Ssam} 80178354Ssam 81178354Ssamstatic void 82178354Ssamwds_vdetach(struct ieee80211vap *vap) 83178354Ssam{ 84178354Ssam if (vap->iv_bss != NULL) { 85178354Ssam /* XXX locking? */ 86178354Ssam if (vap->iv_bss->ni_wdsvap == vap) 87178354Ssam vap->iv_bss->ni_wdsvap = NULL; 88178354Ssam } 89178354Ssam} 90178354Ssam 91178354Ssamstatic void 92178354Ssamwds_vattach(struct ieee80211vap *vap) 93178354Ssam{ 94178354Ssam vap->iv_newstate = wds_newstate; 95178354Ssam vap->iv_input = wds_input; 96178354Ssam vap->iv_recv_mgmt = wds_recv_mgmt; 97178354Ssam vap->iv_opdetach = wds_vdetach; 98178354Ssam} 99178354Ssam 100195379Ssamstatic void 101195379Ssamwds_flush(struct ieee80211_node *ni) 102195379Ssam{ 103195379Ssam struct ieee80211com *ic = ni->ni_ic; 104195379Ssam struct mbuf *m, *next; 105195379Ssam int8_t rssi, nf; 106195379Ssam 107195379Ssam m = ieee80211_ageq_remove(&ic->ic_stageq, 108195379Ssam (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr)); 109195379Ssam if (m == NULL) 110195379Ssam return; 111195379Ssam 112195379Ssam IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_WDS, ni, 113195379Ssam "%s", "flush wds queue"); 114195379Ssam ic->ic_node_getsignal(ni, &rssi, &nf); 115195379Ssam for (; m != NULL; m = next) { 116195379Ssam next = m->m_nextpkt; 117195379Ssam m->m_nextpkt = NULL; 118195379Ssam ieee80211_input(ni, m, rssi, nf); 119195379Ssam } 120195379Ssam} 121195379Ssam 122178354Ssamstatic int 123178354Ssamieee80211_create_wds(struct ieee80211vap *vap, struct ieee80211_channel *chan) 124178354Ssam{ 125178354Ssam struct ieee80211com *ic = vap->iv_ic; 126178354Ssam struct ieee80211_node_table *nt = &ic->ic_sta; 127178354Ssam struct ieee80211_node *ni, *obss; 128178354Ssam 129178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_WDS, 130178354Ssam "%s: creating link to %s on channel %u\n", __func__, 131178354Ssam ether_sprintf(vap->iv_des_bssid), ieee80211_chan2ieee(ic, chan)); 132178354Ssam 133178354Ssam /* NB: vap create must specify the bssid for the link */ 134178354Ssam KASSERT(vap->iv_flags & IEEE80211_F_DESBSSID, ("no bssid")); 135178354Ssam /* NB: we should only be called on RUN transition */ 136178354Ssam KASSERT(vap->iv_state == IEEE80211_S_RUN, ("!RUN state")); 137178354Ssam 138178354Ssam if ((vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0) { 139178354Ssam /* 140178354Ssam * Dynamic/non-legacy WDS. Reference the associated 141178354Ssam * station specified by the desired bssid setup at vap 142178354Ssam * create. Point ni_wdsvap at the WDS vap so 4-address 143178354Ssam * frames received through the associated AP vap will 144178354Ssam * be dispatched upward (e.g. to a bridge) as though 145178354Ssam * they arrived on the WDS vap. 146178354Ssam */ 147178354Ssam IEEE80211_NODE_LOCK(nt); 148178354Ssam obss = NULL; 149178354Ssam ni = ieee80211_find_node_locked(&ic->ic_sta, vap->iv_des_bssid); 150178354Ssam if (ni == NULL) { 151178354Ssam /* 152178354Ssam * Node went away before we could hookup. This 153178354Ssam * should be ok; no traffic will flow and a leave 154178354Ssam * event will be dispatched that should cause 155178354Ssam * the vap to be destroyed. 156178354Ssam */ 157178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_WDS, 158178354Ssam "%s: station %s went away\n", 159178354Ssam __func__, ether_sprintf(vap->iv_des_bssid)); 160178354Ssam /* XXX stat? */ 161178354Ssam } else if (ni->ni_wdsvap != NULL) { 162178354Ssam /* 163178354Ssam * Node already setup with a WDS vap; we cannot 164178354Ssam * allow multiple references so disallow. If 165178354Ssam * ni_wdsvap points at us that's ok; we should 166178354Ssam * do nothing anyway. 167178354Ssam */ 168178354Ssam /* XXX printf instead? */ 169178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_WDS, 170178354Ssam "%s: station %s in use with %s\n", 171178354Ssam __func__, ether_sprintf(vap->iv_des_bssid), 172178354Ssam ni->ni_wdsvap->iv_ifp->if_xname); 173178354Ssam /* XXX stat? */ 174178354Ssam } else { 175178354Ssam /* 176178354Ssam * Committed to new node, setup state. 177178354Ssam */ 178178354Ssam obss = vap->iv_bss; 179178354Ssam vap->iv_bss = ni; 180178354Ssam ni->ni_wdsvap = vap; 181178354Ssam } 182178354Ssam IEEE80211_NODE_UNLOCK(nt); 183178354Ssam if (obss != NULL) { 184178354Ssam /* NB: deferred to avoid recursive lock */ 185178354Ssam ieee80211_free_node(obss); 186178354Ssam } 187178354Ssam } else { 188178354Ssam /* 189178354Ssam * Legacy WDS vap setup. 190178354Ssam */ 191178354Ssam /* 192178354Ssam * The far end does not associate so we just create 193178354Ssam * create a new node and install it as the vap's 194178354Ssam * bss node. We must simulate an association and 195178354Ssam * authorize the port for traffic to flow. 196178354Ssam * XXX check if node already in sta table? 197178354Ssam */ 198178354Ssam ni = ieee80211_node_create_wds(vap, vap->iv_des_bssid, chan); 199178354Ssam if (ni != NULL) { 200178354Ssam obss = vap->iv_bss; 201178354Ssam vap->iv_bss = ieee80211_ref_node(ni); 202178354Ssam ni->ni_flags |= IEEE80211_NODE_AREF; 203178354Ssam if (obss != NULL) 204178354Ssam ieee80211_free_node(obss); 205178354Ssam /* give driver a chance to setup state like ni_txrate */ 206178354Ssam if (ic->ic_newassoc != NULL) 207178354Ssam ic->ic_newassoc(ni, 1); 208178354Ssam /* tell the authenticator about new station */ 209178354Ssam if (vap->iv_auth->ia_node_join != NULL) 210178354Ssam vap->iv_auth->ia_node_join(ni); 211178354Ssam if (ni->ni_authmode != IEEE80211_AUTH_8021X) 212178354Ssam ieee80211_node_authorize(ni); 213178354Ssam 214178354Ssam ieee80211_notify_node_join(ni, 1 /*newassoc*/); 215178354Ssam /* XXX inject l2uf frame */ 216178354Ssam } 217178354Ssam } 218178354Ssam 219178354Ssam /* 220195379Ssam * Flush any pending frames now that were setup. 221178354Ssam */ 222195379Ssam if (ni != NULL) 223195379Ssam wds_flush(ni); 224178354Ssam return (ni == NULL ? ENOENT : 0); 225178354Ssam} 226178354Ssam 227178354Ssam/* 228178354Ssam * Propagate multicast frames of an ap vap to all DWDS links. 229178354Ssam * The caller is assumed to have verified this frame is multicast. 230178354Ssam */ 231178354Ssamvoid 232178354Ssamieee80211_dwds_mcast(struct ieee80211vap *vap0, struct mbuf *m) 233178354Ssam{ 234178354Ssam struct ieee80211com *ic = vap0->iv_ic; 235178354Ssam const struct ether_header *eh = mtod(m, const struct ether_header *); 236178354Ssam struct ieee80211_node *ni; 237178354Ssam struct ieee80211vap *vap; 238178354Ssam struct ifnet *ifp; 239178354Ssam struct mbuf *mcopy; 240178354Ssam int err; 241178354Ssam 242178354Ssam KASSERT(ETHER_IS_MULTICAST(eh->ether_dhost), 243178354Ssam ("%s not mcast", ether_sprintf(eh->ether_dhost))); 244178354Ssam 245178354Ssam /* XXX locking */ 246178354Ssam TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 247178354Ssam /* only DWDS vaps are interesting */ 248178354Ssam if (vap->iv_opmode != IEEE80211_M_WDS || 249178354Ssam (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)) 250178354Ssam continue; 251178354Ssam /* if it came in this interface, don't send it back out */ 252178354Ssam ifp = vap->iv_ifp; 253178354Ssam if (ifp == m->m_pkthdr.rcvif) 254178354Ssam continue; 255178354Ssam /* 256190672Ssam * Duplicate the frame and send it. 257178354Ssam */ 258243882Sglebius mcopy = m_copypacket(m, M_NOWAIT); 259178354Ssam if (mcopy == NULL) { 260178354Ssam ifp->if_oerrors++; 261178354Ssam /* XXX stat + msg */ 262178354Ssam continue; 263178354Ssam } 264178354Ssam ni = ieee80211_find_txnode(vap, eh->ether_dhost); 265178354Ssam if (ni == NULL) { 266178354Ssam /* NB: ieee80211_find_txnode does stat+msg */ 267178354Ssam ifp->if_oerrors++; 268178354Ssam m_freem(mcopy); 269178354Ssam continue; 270178354Ssam } 271190672Ssam /* calculate priority so drivers can find the tx queue */ 272178354Ssam if (ieee80211_classify(ni, mcopy)) { 273178354Ssam IEEE80211_DISCARD_MAC(vap, 274178354Ssam IEEE80211_MSG_OUTPUT | IEEE80211_MSG_WDS, 275178354Ssam eh->ether_dhost, NULL, 276178354Ssam "%s", "classification failure"); 277178354Ssam vap->iv_stats.is_tx_classify++; 278178354Ssam ifp->if_oerrors++; 279178354Ssam m_freem(mcopy); 280178354Ssam ieee80211_free_node(ni); 281178354Ssam continue; 282178354Ssam } 283191542Ssam 284191542Ssam BPF_MTAP(ifp, m); /* 802.3 tx */ 285191542Ssam 286190672Ssam /* 287190672Ssam * Encapsulate the packet in prep for transmission. 288190672Ssam */ 289190672Ssam mcopy = ieee80211_encap(vap, ni, mcopy); 290194461Srpaulo if (mcopy == NULL) { 291190672Ssam /* NB: stat+msg handled in ieee80211_encap */ 292190672Ssam ieee80211_free_node(ni); 293190672Ssam continue; 294190672Ssam } 295190672Ssam mcopy->m_flags |= M_MCAST; 296178354Ssam mcopy->m_pkthdr.rcvif = (void *) ni; 297178354Ssam 298254082Sadrian err = ieee80211_parent_xmitpkt(ic, mcopy); 299178354Ssam if (err) { 300178354Ssam /* NB: IFQ_HANDOFF reclaims mbuf */ 301178354Ssam ifp->if_oerrors++; 302178354Ssam ieee80211_free_node(ni); 303283855Sae } else { 304178354Ssam ifp->if_opackets++; 305283855Sae if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1); 306283855Sae if_inc_counter(ifp, IFCOUNTER_OBYTES, 307283855Sae m->m_pkthdr.len); 308283855Sae } 309178354Ssam } 310178354Ssam} 311178354Ssam 312178354Ssam/* 313178354Ssam * Handle DWDS discovery on receipt of a 4-address frame in 314178354Ssam * ap mode. Queue the frame and post an event for someone 315178354Ssam * to plumb the necessary WDS vap for this station. Frames 316178354Ssam * received prior to the vap set running will then be reprocessed 317178354Ssam * as if they were just received. 318178354Ssam */ 319178354Ssamvoid 320178354Ssamieee80211_dwds_discover(struct ieee80211_node *ni, struct mbuf *m) 321178354Ssam{ 322178354Ssam struct ieee80211com *ic = ni->ni_ic; 323178354Ssam 324195379Ssam /* 325195379Ssam * Save the frame with an aging interval 4 times 326195379Ssam * the listen interval specified by the station. 327195379Ssam * Frames that sit around too long are reclaimed 328195379Ssam * using this information. 329195379Ssam * XXX handle overflow? 330195379Ssam * XXX per/vap beacon interval? 331195379Ssam */ 332195379Ssam m->m_pkthdr.rcvif = (void *)(uintptr_t) 333195379Ssam ieee80211_mac_hash(ic, ni->ni_macaddr); 334195379Ssam (void) ieee80211_ageq_append(&ic->ic_stageq, m, 335195379Ssam ((ni->ni_intval * ic->ic_lintval) << 2) / 1024); 336178354Ssam ieee80211_notify_wds_discover(ni); 337178354Ssam} 338178354Ssam 339178354Ssam/* 340178354Ssam * IEEE80211_M_WDS vap state machine handler. 341178354Ssam */ 342178354Ssamstatic int 343178354Ssamwds_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 344178354Ssam{ 345178354Ssam struct ieee80211com *ic = vap->iv_ic; 346178354Ssam struct ieee80211_node *ni; 347178354Ssam enum ieee80211_state ostate; 348178354Ssam int error; 349178354Ssam 350178354Ssam IEEE80211_LOCK_ASSERT(ic); 351178354Ssam 352178354Ssam ostate = vap->iv_state; 353178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s\n", __func__, 354178354Ssam ieee80211_state_name[ostate], ieee80211_state_name[nstate]); 355178354Ssam vap->iv_state = nstate; /* state transition */ 356178354Ssam callout_stop(&vap->iv_mgtsend); /* XXX callout_drain */ 357178354Ssam if (ostate != IEEE80211_S_SCAN) 358178354Ssam ieee80211_cancel_scan(vap); /* background scan */ 359178354Ssam ni = vap->iv_bss; /* NB: no reference held */ 360178354Ssam error = 0; 361178354Ssam switch (nstate) { 362178354Ssam case IEEE80211_S_INIT: 363178354Ssam switch (ostate) { 364178354Ssam case IEEE80211_S_SCAN: 365178354Ssam ieee80211_cancel_scan(vap); 366178354Ssam break; 367178354Ssam default: 368178354Ssam break; 369178354Ssam } 370178354Ssam if (ostate != IEEE80211_S_INIT) { 371178354Ssam /* NB: optimize INIT -> INIT case */ 372178354Ssam ieee80211_reset_bss(vap); 373178354Ssam } 374178354Ssam break; 375178354Ssam case IEEE80211_S_SCAN: 376178354Ssam switch (ostate) { 377178354Ssam case IEEE80211_S_INIT: 378178354Ssam ieee80211_check_scan_current(vap); 379178354Ssam break; 380178354Ssam default: 381178354Ssam break; 382178354Ssam } 383178354Ssam break; 384178354Ssam case IEEE80211_S_RUN: 385178354Ssam if (ostate == IEEE80211_S_INIT) { 386178354Ssam /* 387178354Ssam * Already have a channel; bypass the scan 388178354Ssam * and startup immediately. 389178354Ssam */ 390178354Ssam error = ieee80211_create_wds(vap, ic->ic_curchan); 391178354Ssam } 392178354Ssam break; 393178354Ssam default: 394178354Ssam break; 395178354Ssam } 396178354Ssam return error; 397178354Ssam} 398178354Ssam 399178354Ssam/* 400178354Ssam * Process a received frame. The node associated with the sender 401178354Ssam * should be supplied. If nothing was found in the node table then 402178354Ssam * the caller is assumed to supply a reference to iv_bss instead. 403178354Ssam * The RSSI and a timestamp are also supplied. The RSSI data is used 404178354Ssam * during AP scanning to select a AP to associate with; it can have 405178354Ssam * any units so long as values have consistent units and higher values 406178354Ssam * mean ``better signal''. The receive timestamp is currently not used 407178354Ssam * by the 802.11 layer. 408178354Ssam */ 409178354Ssamstatic int 410192468Ssamwds_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf) 411178354Ssam{ 412178354Ssam#define HAS_SEQ(type) ((type & 0x4) == 0) 413178354Ssam struct ieee80211vap *vap = ni->ni_vap; 414178354Ssam struct ieee80211com *ic = ni->ni_ic; 415178354Ssam struct ifnet *ifp = vap->iv_ifp; 416178354Ssam struct ieee80211_frame *wh; 417178354Ssam struct ieee80211_key *key; 418178354Ssam struct ether_header *eh; 419203422Srpaulo int hdrspace, need_tap = 1; /* mbuf need to be tapped. */ 420178354Ssam uint8_t dir, type, subtype, qos; 421178354Ssam uint16_t rxseq; 422178354Ssam 423183247Ssam if (m->m_flags & M_AMPDU_MPDU) { 424178354Ssam /* 425178354Ssam * Fastpath for A-MPDU reorder q resubmission. Frames 426183247Ssam * w/ M_AMPDU_MPDU marked have already passed through 427183247Ssam * here but were received out of order and been held on 428183247Ssam * the reorder queue. When resubmitted they are marked 429183247Ssam * with the M_AMPDU_MPDU flag and we can bypass most of 430183247Ssam * the normal processing. 431178354Ssam */ 432178354Ssam wh = mtod(m, struct ieee80211_frame *); 433178354Ssam type = IEEE80211_FC0_TYPE_DATA; 434178354Ssam dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 435178354Ssam subtype = IEEE80211_FC0_SUBTYPE_QOS; 436178354Ssam hdrspace = ieee80211_hdrspace(ic, wh); /* XXX optimize? */ 437178354Ssam goto resubmit_ampdu; 438178354Ssam } 439178354Ssam 440178354Ssam KASSERT(ni != NULL, ("null node")); 441178354Ssam 442178354Ssam type = -1; /* undefined */ 443178354Ssam 444178354Ssam if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) { 445178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 446178354Ssam ni->ni_macaddr, NULL, 447178354Ssam "too short (1): len %u", m->m_pkthdr.len); 448178354Ssam vap->iv_stats.is_rx_tooshort++; 449178354Ssam goto out; 450178354Ssam } 451178354Ssam /* 452178354Ssam * Bit of a cheat here, we use a pointer for a 3-address 453178354Ssam * frame format but don't reference fields past outside 454178354Ssam * ieee80211_frame_min w/o first validating the data is 455178354Ssam * present. 456178354Ssam */ 457178354Ssam wh = mtod(m, struct ieee80211_frame *); 458178354Ssam 459218958Sbschmidt if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) 460218958Sbschmidt ni->ni_inact = ni->ni_inact_reload; 461218958Sbschmidt 462178354Ssam if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) != 463178354Ssam IEEE80211_FC0_VERSION_0) { 464178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 465191547Ssam ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x", 466191547Ssam wh->i_fc[0], wh->i_fc[1]); 467178354Ssam vap->iv_stats.is_rx_badversion++; 468178354Ssam goto err; 469178354Ssam } 470178354Ssam 471178354Ssam dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 472178354Ssam type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 473178354Ssam subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 474178354Ssam 475178354Ssam /* NB: WDS vap's do not scan */ 476178354Ssam if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_addr4)) { 477178354Ssam IEEE80211_DISCARD_MAC(vap, 478178354Ssam IEEE80211_MSG_ANY, ni->ni_macaddr, NULL, 479178354Ssam "too short (3): len %u", m->m_pkthdr.len); 480178354Ssam vap->iv_stats.is_rx_tooshort++; 481178354Ssam goto out; 482178354Ssam } 483178354Ssam /* NB: the TA is implicitly verified by finding the wds peer node */ 484178354Ssam if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr) && 485178354Ssam !IEEE80211_ADDR_EQ(wh->i_addr1, ifp->if_broadcastaddr)) { 486178354Ssam /* not interested in */ 487178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 488178354Ssam wh->i_addr1, NULL, "%s", "not to bss"); 489178354Ssam vap->iv_stats.is_rx_wrongbss++; 490178354Ssam goto out; 491178354Ssam } 492178354Ssam IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi); 493192468Ssam ni->ni_noise = nf; 494178354Ssam if (HAS_SEQ(type)) { 495178354Ssam uint8_t tid = ieee80211_gettid(wh); 496178354Ssam if (IEEE80211_QOS_HAS_SEQ(wh) && 497178354Ssam TID_TO_WME_AC(tid) >= WME_AC_VI) 498178354Ssam ic->ic_wme.wme_hipri_traffic++; 499178354Ssam rxseq = le16toh(*(uint16_t *)wh->i_seq); 500221418Sadrian if (! ieee80211_check_rxseq(ni, wh)) { 501178354Ssam /* duplicate, discard */ 502178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 503178354Ssam wh->i_addr1, "duplicate", 504178354Ssam "seqno <%u,%u> fragno <%u,%u> tid %u", 505178354Ssam rxseq >> IEEE80211_SEQ_SEQ_SHIFT, 506178354Ssam ni->ni_rxseqs[tid] >> IEEE80211_SEQ_SEQ_SHIFT, 507178354Ssam rxseq & IEEE80211_SEQ_FRAG_MASK, 508178354Ssam ni->ni_rxseqs[tid] & IEEE80211_SEQ_FRAG_MASK, 509178354Ssam tid); 510178354Ssam vap->iv_stats.is_rx_dup++; 511178354Ssam IEEE80211_NODE_STAT(ni, rx_dup); 512178354Ssam goto out; 513178354Ssam } 514178354Ssam ni->ni_rxseqs[tid] = rxseq; 515178354Ssam } 516178354Ssam switch (type) { 517178354Ssam case IEEE80211_FC0_TYPE_DATA: 518178354Ssam hdrspace = ieee80211_hdrspace(ic, wh); 519178354Ssam if (m->m_len < hdrspace && 520178354Ssam (m = m_pullup(m, hdrspace)) == NULL) { 521178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 522178354Ssam ni->ni_macaddr, NULL, 523178354Ssam "data too short: expecting %u", hdrspace); 524178354Ssam vap->iv_stats.is_rx_tooshort++; 525178354Ssam goto out; /* XXX */ 526178354Ssam } 527178354Ssam if (dir != IEEE80211_FC1_DIR_DSTODS) { 528178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 529178354Ssam wh, "data", "incorrect dir 0x%x", dir); 530178354Ssam vap->iv_stats.is_rx_wrongdir++; 531178354Ssam goto out; 532178354Ssam } 533178354Ssam /* 534178354Ssam * Only legacy WDS traffic should take this path. 535178354Ssam */ 536178354Ssam if ((vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0) { 537178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 538178354Ssam wh, "data", "%s", "not legacy wds"); 539178354Ssam vap->iv_stats.is_rx_wrongdir++;/*XXX*/ 540178354Ssam goto out; 541178354Ssam } 542178354Ssam /* 543183247Ssam * Handle A-MPDU re-ordering. If the frame is to be 544183247Ssam * processed directly then ieee80211_ampdu_reorder 545178354Ssam * will return 0; otherwise it has consumed the mbuf 546178354Ssam * and we should do nothing more with it. 547178354Ssam */ 548183247Ssam if ((m->m_flags & M_AMPDU) && 549178354Ssam ieee80211_ampdu_reorder(ni, m) != 0) { 550178354Ssam m = NULL; 551178354Ssam goto out; 552178354Ssam } 553178354Ssam resubmit_ampdu: 554178354Ssam 555178354Ssam /* 556178354Ssam * Handle privacy requirements. Note that we 557178354Ssam * must not be preempted from here until after 558178354Ssam * we (potentially) call ieee80211_crypto_demic; 559178354Ssam * otherwise we may violate assumptions in the 560178354Ssam * crypto cipher modules used to do delayed update 561178354Ssam * of replay sequence numbers. 562178354Ssam */ 563262007Skevlo if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 564178354Ssam if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) { 565178354Ssam /* 566178354Ssam * Discard encrypted frames when privacy is off. 567178354Ssam */ 568178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 569178354Ssam wh, "WEP", "%s", "PRIVACY off"); 570178354Ssam vap->iv_stats.is_rx_noprivacy++; 571178354Ssam IEEE80211_NODE_STAT(ni, rx_noprivacy); 572178354Ssam goto out; 573178354Ssam } 574178354Ssam key = ieee80211_crypto_decap(ni, m, hdrspace); 575178354Ssam if (key == NULL) { 576178354Ssam /* NB: stats+msgs handled in crypto_decap */ 577178354Ssam IEEE80211_NODE_STAT(ni, rx_wepfail); 578178354Ssam goto out; 579178354Ssam } 580178354Ssam wh = mtod(m, struct ieee80211_frame *); 581262007Skevlo wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED; 582178354Ssam } else { 583178354Ssam /* XXX M_WEP and IEEE80211_F_PRIVACY */ 584178354Ssam key = NULL; 585178354Ssam } 586178354Ssam 587178354Ssam /* 588178354Ssam * Save QoS bits for use below--before we strip the header. 589178354Ssam */ 590178354Ssam if (subtype == IEEE80211_FC0_SUBTYPE_QOS) { 591178354Ssam qos = (dir == IEEE80211_FC1_DIR_DSTODS) ? 592178354Ssam ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] : 593178354Ssam ((struct ieee80211_qosframe *)wh)->i_qos[0]; 594178354Ssam } else 595178354Ssam qos = 0; 596178354Ssam 597178354Ssam /* 598178354Ssam * Next up, any fragmentation. 599178354Ssam */ 600178354Ssam if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 601178354Ssam m = ieee80211_defrag(ni, m, hdrspace); 602178354Ssam if (m == NULL) { 603178354Ssam /* Fragment dropped or frame not complete yet */ 604178354Ssam goto out; 605178354Ssam } 606178354Ssam } 607178354Ssam wh = NULL; /* no longer valid, catch any uses */ 608178354Ssam 609178354Ssam /* 610178354Ssam * Next strip any MSDU crypto bits. 611178354Ssam */ 612178354Ssam if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) { 613178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 614178354Ssam ni->ni_macaddr, "data", "%s", "demic error"); 615178354Ssam vap->iv_stats.is_rx_demicfail++; 616178354Ssam IEEE80211_NODE_STAT(ni, rx_demicfail); 617178354Ssam goto out; 618178354Ssam } 619178354Ssam 620178354Ssam /* copy to listener after decrypt */ 621192468Ssam if (ieee80211_radiotap_active_vap(vap)) 622192468Ssam ieee80211_radiotap_rx(vap, m); 623178354Ssam need_tap = 0; 624178354Ssam 625178354Ssam /* 626178354Ssam * Finally, strip the 802.11 header. 627178354Ssam */ 628178354Ssam m = ieee80211_decap(vap, m, hdrspace); 629178354Ssam if (m == NULL) { 630178354Ssam /* XXX mask bit to check for both */ 631178354Ssam /* don't count Null data frames as errors */ 632178354Ssam if (subtype == IEEE80211_FC0_SUBTYPE_NODATA || 633178354Ssam subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL) 634178354Ssam goto out; 635178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 636178354Ssam ni->ni_macaddr, "data", "%s", "decap error"); 637178354Ssam vap->iv_stats.is_rx_decap++; 638178354Ssam IEEE80211_NODE_STAT(ni, rx_decap); 639178354Ssam goto err; 640178354Ssam } 641178354Ssam eh = mtod(m, struct ether_header *); 642178354Ssam if (!ieee80211_node_is_authorized(ni)) { 643178354Ssam /* 644178354Ssam * Deny any non-PAE frames received prior to 645178354Ssam * authorization. For open/shared-key 646178354Ssam * authentication the port is mark authorized 647178354Ssam * after authentication completes. For 802.1x 648178354Ssam * the port is not marked authorized by the 649178354Ssam * authenticator until the handshake has completed. 650178354Ssam */ 651178354Ssam if (eh->ether_type != htons(ETHERTYPE_PAE)) { 652178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 653178354Ssam eh->ether_shost, "data", 654178354Ssam "unauthorized port: ether type 0x%x len %u", 655178354Ssam eh->ether_type, m->m_pkthdr.len); 656178354Ssam vap->iv_stats.is_rx_unauth++; 657178354Ssam IEEE80211_NODE_STAT(ni, rx_unauth); 658178354Ssam goto err; 659178354Ssam } 660178354Ssam } else { 661178354Ssam /* 662178354Ssam * When denying unencrypted frames, discard 663178354Ssam * any non-PAE frames received without encryption. 664178354Ssam */ 665178354Ssam if ((vap->iv_flags & IEEE80211_F_DROPUNENC) && 666178354Ssam (key == NULL && (m->m_flags & M_WEP) == 0) && 667178354Ssam eh->ether_type != htons(ETHERTYPE_PAE)) { 668178354Ssam /* 669178354Ssam * Drop unencrypted frames. 670178354Ssam */ 671178354Ssam vap->iv_stats.is_rx_unencrypted++; 672178354Ssam IEEE80211_NODE_STAT(ni, rx_unencrypted); 673178354Ssam goto out; 674178354Ssam } 675178354Ssam } 676178354Ssam /* XXX require HT? */ 677178354Ssam if (qos & IEEE80211_QOS_AMSDU) { 678178354Ssam m = ieee80211_decap_amsdu(ni, m); 679178354Ssam if (m == NULL) 680178354Ssam return IEEE80211_FC0_TYPE_DATA; 681190391Ssam } else { 682190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG 683190391Ssam m = ieee80211_decap_fastframe(vap, ni, m); 684190391Ssam if (m == NULL) 685178354Ssam return IEEE80211_FC0_TYPE_DATA; 686190391Ssam#endif 687178354Ssam } 688178354Ssam ieee80211_deliver_data(vap, ni, m); 689178354Ssam return IEEE80211_FC0_TYPE_DATA; 690178354Ssam 691178354Ssam case IEEE80211_FC0_TYPE_MGT: 692178354Ssam vap->iv_stats.is_rx_mgmt++; 693178354Ssam IEEE80211_NODE_STAT(ni, rx_mgmt); 694178354Ssam if (dir != IEEE80211_FC1_DIR_NODS) { 695178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 696178354Ssam wh, "data", "incorrect dir 0x%x", dir); 697178354Ssam vap->iv_stats.is_rx_wrongdir++; 698178354Ssam goto err; 699178354Ssam } 700178354Ssam if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) { 701178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 702178354Ssam ni->ni_macaddr, "mgt", "too short: len %u", 703178354Ssam m->m_pkthdr.len); 704178354Ssam vap->iv_stats.is_rx_tooshort++; 705178354Ssam goto out; 706178354Ssam } 707178354Ssam#ifdef IEEE80211_DEBUG 708178354Ssam if (ieee80211_msg_debug(vap) || ieee80211_msg_dumppkts(vap)) { 709178354Ssam if_printf(ifp, "received %s from %s rssi %d\n", 710178354Ssam ieee80211_mgt_subtype_name[subtype >> 711178354Ssam IEEE80211_FC0_SUBTYPE_SHIFT], 712178354Ssam ether_sprintf(wh->i_addr2), rssi); 713178354Ssam } 714178354Ssam#endif 715262007Skevlo if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 716178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 717178354Ssam wh, NULL, "%s", "WEP set but not permitted"); 718178354Ssam vap->iv_stats.is_rx_mgtdiscard++; /* XXX */ 719178354Ssam goto out; 720178354Ssam } 721192468Ssam vap->iv_recv_mgmt(ni, m, subtype, rssi, nf); 722192468Ssam goto out; 723178354Ssam 724178354Ssam case IEEE80211_FC0_TYPE_CTL: 725178354Ssam vap->iv_stats.is_rx_ctl++; 726178354Ssam IEEE80211_NODE_STAT(ni, rx_ctrl); 727178354Ssam goto out; 728192468Ssam 729178354Ssam default: 730178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 731178354Ssam wh, "bad", "frame type 0x%x", type); 732178354Ssam /* should not come here */ 733178354Ssam break; 734178354Ssam } 735178354Ssamerr: 736178354Ssam ifp->if_ierrors++; 737178354Ssamout: 738178354Ssam if (m != NULL) { 739192765Ssam if (need_tap && ieee80211_radiotap_active_vap(vap)) 740192468Ssam ieee80211_radiotap_rx(vap, m); 741178354Ssam m_freem(m); 742178354Ssam } 743178354Ssam return type; 744178354Ssam} 745178354Ssam 746178354Ssamstatic void 747178354Ssamwds_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, 748192468Ssam int subtype, int rssi, int nf) 749178354Ssam{ 750178354Ssam struct ieee80211vap *vap = ni->ni_vap; 751178354Ssam struct ieee80211com *ic = ni->ni_ic; 752178354Ssam struct ieee80211_frame *wh; 753178354Ssam u_int8_t *frm, *efrm; 754178354Ssam 755178354Ssam wh = mtod(m0, struct ieee80211_frame *); 756178354Ssam frm = (u_int8_t *)&wh[1]; 757178354Ssam efrm = mtod(m0, u_int8_t *) + m0->m_len; 758178354Ssam switch (subtype) { 759218927Sbschmidt case IEEE80211_FC0_SUBTYPE_ACTION: 760218927Sbschmidt case IEEE80211_FC0_SUBTYPE_ACTION_NOACK: 761218958Sbschmidt if (ni == vap->iv_bss) { 762218958Sbschmidt IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 763218958Sbschmidt wh, NULL, "%s", "unknown node"); 764218927Sbschmidt vap->iv_stats.is_rx_mgtdiscard++; 765218958Sbschmidt } else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1)) { 766218958Sbschmidt /* NB: not interested in multicast frames. */ 767218927Sbschmidt IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 768218958Sbschmidt wh, NULL, "%s", "not for us"); 769218958Sbschmidt vap->iv_stats.is_rx_mgtdiscard++; 770218958Sbschmidt } else if (vap->iv_state != IEEE80211_S_RUN) { 771218958Sbschmidt IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 772218927Sbschmidt wh, NULL, "wrong state %s", 773218927Sbschmidt ieee80211_state_name[vap->iv_state]); 774218927Sbschmidt vap->iv_stats.is_rx_mgtdiscard++; 775218958Sbschmidt } else { 776218958Sbschmidt if (ieee80211_parse_action(ni, m0) == 0) 777218958Sbschmidt (void)ic->ic_recv_action(ni, wh, frm, efrm); 778218927Sbschmidt } 779218927Sbschmidt break; 780218927Sbschmidt 781178354Ssam case IEEE80211_FC0_SUBTYPE_ASSOC_REQ: 782218927Sbschmidt case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 783178354Ssam case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: 784178354Ssam case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: 785218927Sbschmidt case IEEE80211_FC0_SUBTYPE_PROBE_REQ: 786218927Sbschmidt case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 787218927Sbschmidt case IEEE80211_FC0_SUBTYPE_BEACON: 788218927Sbschmidt case IEEE80211_FC0_SUBTYPE_ATIM: 789178354Ssam case IEEE80211_FC0_SUBTYPE_DISASSOC: 790218927Sbschmidt case IEEE80211_FC0_SUBTYPE_AUTH: 791218927Sbschmidt case IEEE80211_FC0_SUBTYPE_DEAUTH: 792218927Sbschmidt IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 793218927Sbschmidt wh, NULL, "%s", "not handled"); 794178354Ssam vap->iv_stats.is_rx_mgtdiscard++; 795178354Ssam break; 796218927Sbschmidt 797178354Ssam default: 798178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 799218927Sbschmidt wh, "mgt", "subtype 0x%x not handled", subtype); 800178354Ssam vap->iv_stats.is_rx_badsubtype++; 801178354Ssam break; 802178354Ssam } 803178354Ssam} 804