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); 303178354Ssam } else 304178354Ssam ifp->if_opackets++; 305178354Ssam } 306178354Ssam} 307178354Ssam 308178354Ssam/* 309178354Ssam * Handle DWDS discovery on receipt of a 4-address frame in 310178354Ssam * ap mode. Queue the frame and post an event for someone 311178354Ssam * to plumb the necessary WDS vap for this station. Frames 312178354Ssam * received prior to the vap set running will then be reprocessed 313178354Ssam * as if they were just received. 314178354Ssam */ 315178354Ssamvoid 316178354Ssamieee80211_dwds_discover(struct ieee80211_node *ni, struct mbuf *m) 317178354Ssam{ 318178354Ssam struct ieee80211com *ic = ni->ni_ic; 319178354Ssam 320195379Ssam /* 321195379Ssam * Save the frame with an aging interval 4 times 322195379Ssam * the listen interval specified by the station. 323195379Ssam * Frames that sit around too long are reclaimed 324195379Ssam * using this information. 325195379Ssam * XXX handle overflow? 326195379Ssam * XXX per/vap beacon interval? 327195379Ssam */ 328195379Ssam m->m_pkthdr.rcvif = (void *)(uintptr_t) 329195379Ssam ieee80211_mac_hash(ic, ni->ni_macaddr); 330195379Ssam (void) ieee80211_ageq_append(&ic->ic_stageq, m, 331195379Ssam ((ni->ni_intval * ic->ic_lintval) << 2) / 1024); 332178354Ssam ieee80211_notify_wds_discover(ni); 333178354Ssam} 334178354Ssam 335178354Ssam/* 336178354Ssam * IEEE80211_M_WDS vap state machine handler. 337178354Ssam */ 338178354Ssamstatic int 339178354Ssamwds_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 340178354Ssam{ 341178354Ssam struct ieee80211com *ic = vap->iv_ic; 342178354Ssam struct ieee80211_node *ni; 343178354Ssam enum ieee80211_state ostate; 344178354Ssam int error; 345178354Ssam 346178354Ssam IEEE80211_LOCK_ASSERT(ic); 347178354Ssam 348178354Ssam ostate = vap->iv_state; 349178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s\n", __func__, 350178354Ssam ieee80211_state_name[ostate], ieee80211_state_name[nstate]); 351178354Ssam vap->iv_state = nstate; /* state transition */ 352178354Ssam callout_stop(&vap->iv_mgtsend); /* XXX callout_drain */ 353178354Ssam if (ostate != IEEE80211_S_SCAN) 354178354Ssam ieee80211_cancel_scan(vap); /* background scan */ 355178354Ssam ni = vap->iv_bss; /* NB: no reference held */ 356178354Ssam error = 0; 357178354Ssam switch (nstate) { 358178354Ssam case IEEE80211_S_INIT: 359178354Ssam switch (ostate) { 360178354Ssam case IEEE80211_S_SCAN: 361178354Ssam ieee80211_cancel_scan(vap); 362178354Ssam break; 363178354Ssam default: 364178354Ssam break; 365178354Ssam } 366178354Ssam if (ostate != IEEE80211_S_INIT) { 367178354Ssam /* NB: optimize INIT -> INIT case */ 368178354Ssam ieee80211_reset_bss(vap); 369178354Ssam } 370178354Ssam break; 371178354Ssam case IEEE80211_S_SCAN: 372178354Ssam switch (ostate) { 373178354Ssam case IEEE80211_S_INIT: 374178354Ssam ieee80211_check_scan_current(vap); 375178354Ssam break; 376178354Ssam default: 377178354Ssam break; 378178354Ssam } 379178354Ssam break; 380178354Ssam case IEEE80211_S_RUN: 381178354Ssam if (ostate == IEEE80211_S_INIT) { 382178354Ssam /* 383178354Ssam * Already have a channel; bypass the scan 384178354Ssam * and startup immediately. 385178354Ssam */ 386178354Ssam error = ieee80211_create_wds(vap, ic->ic_curchan); 387178354Ssam } 388178354Ssam break; 389178354Ssam default: 390178354Ssam break; 391178354Ssam } 392178354Ssam return error; 393178354Ssam} 394178354Ssam 395178354Ssam/* 396178354Ssam * Process a received frame. The node associated with the sender 397178354Ssam * should be supplied. If nothing was found in the node table then 398178354Ssam * the caller is assumed to supply a reference to iv_bss instead. 399178354Ssam * The RSSI and a timestamp are also supplied. The RSSI data is used 400178354Ssam * during AP scanning to select a AP to associate with; it can have 401178354Ssam * any units so long as values have consistent units and higher values 402178354Ssam * mean ``better signal''. The receive timestamp is currently not used 403178354Ssam * by the 802.11 layer. 404178354Ssam */ 405178354Ssamstatic int 406192468Ssamwds_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf) 407178354Ssam{ 408178354Ssam#define HAS_SEQ(type) ((type & 0x4) == 0) 409178354Ssam struct ieee80211vap *vap = ni->ni_vap; 410178354Ssam struct ieee80211com *ic = ni->ni_ic; 411178354Ssam struct ifnet *ifp = vap->iv_ifp; 412178354Ssam struct ieee80211_frame *wh; 413178354Ssam struct ieee80211_key *key; 414178354Ssam struct ether_header *eh; 415203422Srpaulo int hdrspace, need_tap = 1; /* mbuf need to be tapped. */ 416178354Ssam uint8_t dir, type, subtype, qos; 417178354Ssam uint16_t rxseq; 418178354Ssam 419183247Ssam if (m->m_flags & M_AMPDU_MPDU) { 420178354Ssam /* 421178354Ssam * Fastpath for A-MPDU reorder q resubmission. Frames 422183247Ssam * w/ M_AMPDU_MPDU marked have already passed through 423183247Ssam * here but were received out of order and been held on 424183247Ssam * the reorder queue. When resubmitted they are marked 425183247Ssam * with the M_AMPDU_MPDU flag and we can bypass most of 426183247Ssam * the normal processing. 427178354Ssam */ 428178354Ssam wh = mtod(m, struct ieee80211_frame *); 429178354Ssam type = IEEE80211_FC0_TYPE_DATA; 430178354Ssam dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 431178354Ssam subtype = IEEE80211_FC0_SUBTYPE_QOS; 432178354Ssam hdrspace = ieee80211_hdrspace(ic, wh); /* XXX optimize? */ 433178354Ssam goto resubmit_ampdu; 434178354Ssam } 435178354Ssam 436178354Ssam KASSERT(ni != NULL, ("null node")); 437178354Ssam 438178354Ssam type = -1; /* undefined */ 439178354Ssam 440178354Ssam if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) { 441178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 442178354Ssam ni->ni_macaddr, NULL, 443178354Ssam "too short (1): len %u", m->m_pkthdr.len); 444178354Ssam vap->iv_stats.is_rx_tooshort++; 445178354Ssam goto out; 446178354Ssam } 447178354Ssam /* 448178354Ssam * Bit of a cheat here, we use a pointer for a 3-address 449178354Ssam * frame format but don't reference fields past outside 450178354Ssam * ieee80211_frame_min w/o first validating the data is 451178354Ssam * present. 452178354Ssam */ 453178354Ssam wh = mtod(m, struct ieee80211_frame *); 454178354Ssam 455218958Sbschmidt if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) 456218958Sbschmidt ni->ni_inact = ni->ni_inact_reload; 457218958Sbschmidt 458178354Ssam if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) != 459178354Ssam IEEE80211_FC0_VERSION_0) { 460178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 461191547Ssam ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x", 462191547Ssam wh->i_fc[0], wh->i_fc[1]); 463178354Ssam vap->iv_stats.is_rx_badversion++; 464178354Ssam goto err; 465178354Ssam } 466178354Ssam 467178354Ssam dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 468178354Ssam type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 469178354Ssam subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 470178354Ssam 471178354Ssam /* NB: WDS vap's do not scan */ 472178354Ssam if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_addr4)) { 473178354Ssam IEEE80211_DISCARD_MAC(vap, 474178354Ssam IEEE80211_MSG_ANY, ni->ni_macaddr, NULL, 475178354Ssam "too short (3): len %u", m->m_pkthdr.len); 476178354Ssam vap->iv_stats.is_rx_tooshort++; 477178354Ssam goto out; 478178354Ssam } 479178354Ssam /* NB: the TA is implicitly verified by finding the wds peer node */ 480178354Ssam if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr) && 481178354Ssam !IEEE80211_ADDR_EQ(wh->i_addr1, ifp->if_broadcastaddr)) { 482178354Ssam /* not interested in */ 483178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 484178354Ssam wh->i_addr1, NULL, "%s", "not to bss"); 485178354Ssam vap->iv_stats.is_rx_wrongbss++; 486178354Ssam goto out; 487178354Ssam } 488178354Ssam IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi); 489192468Ssam ni->ni_noise = nf; 490178354Ssam if (HAS_SEQ(type)) { 491178354Ssam uint8_t tid = ieee80211_gettid(wh); 492178354Ssam if (IEEE80211_QOS_HAS_SEQ(wh) && 493178354Ssam TID_TO_WME_AC(tid) >= WME_AC_VI) 494178354Ssam ic->ic_wme.wme_hipri_traffic++; 495178354Ssam rxseq = le16toh(*(uint16_t *)wh->i_seq); 496221418Sadrian if (! ieee80211_check_rxseq(ni, wh)) { 497178354Ssam /* duplicate, discard */ 498178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 499178354Ssam wh->i_addr1, "duplicate", 500178354Ssam "seqno <%u,%u> fragno <%u,%u> tid %u", 501178354Ssam rxseq >> IEEE80211_SEQ_SEQ_SHIFT, 502178354Ssam ni->ni_rxseqs[tid] >> IEEE80211_SEQ_SEQ_SHIFT, 503178354Ssam rxseq & IEEE80211_SEQ_FRAG_MASK, 504178354Ssam ni->ni_rxseqs[tid] & IEEE80211_SEQ_FRAG_MASK, 505178354Ssam tid); 506178354Ssam vap->iv_stats.is_rx_dup++; 507178354Ssam IEEE80211_NODE_STAT(ni, rx_dup); 508178354Ssam goto out; 509178354Ssam } 510178354Ssam ni->ni_rxseqs[tid] = rxseq; 511178354Ssam } 512178354Ssam switch (type) { 513178354Ssam case IEEE80211_FC0_TYPE_DATA: 514178354Ssam hdrspace = ieee80211_hdrspace(ic, wh); 515178354Ssam if (m->m_len < hdrspace && 516178354Ssam (m = m_pullup(m, hdrspace)) == NULL) { 517178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 518178354Ssam ni->ni_macaddr, NULL, 519178354Ssam "data too short: expecting %u", hdrspace); 520178354Ssam vap->iv_stats.is_rx_tooshort++; 521178354Ssam goto out; /* XXX */ 522178354Ssam } 523178354Ssam if (dir != IEEE80211_FC1_DIR_DSTODS) { 524178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 525178354Ssam wh, "data", "incorrect dir 0x%x", dir); 526178354Ssam vap->iv_stats.is_rx_wrongdir++; 527178354Ssam goto out; 528178354Ssam } 529178354Ssam /* 530178354Ssam * Only legacy WDS traffic should take this path. 531178354Ssam */ 532178354Ssam if ((vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0) { 533178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 534178354Ssam wh, "data", "%s", "not legacy wds"); 535178354Ssam vap->iv_stats.is_rx_wrongdir++;/*XXX*/ 536178354Ssam goto out; 537178354Ssam } 538178354Ssam /* 539183247Ssam * Handle A-MPDU re-ordering. If the frame is to be 540183247Ssam * processed directly then ieee80211_ampdu_reorder 541178354Ssam * will return 0; otherwise it has consumed the mbuf 542178354Ssam * and we should do nothing more with it. 543178354Ssam */ 544183247Ssam if ((m->m_flags & M_AMPDU) && 545178354Ssam ieee80211_ampdu_reorder(ni, m) != 0) { 546178354Ssam m = NULL; 547178354Ssam goto out; 548178354Ssam } 549178354Ssam resubmit_ampdu: 550178354Ssam 551178354Ssam /* 552178354Ssam * Handle privacy requirements. Note that we 553178354Ssam * must not be preempted from here until after 554178354Ssam * we (potentially) call ieee80211_crypto_demic; 555178354Ssam * otherwise we may violate assumptions in the 556178354Ssam * crypto cipher modules used to do delayed update 557178354Ssam * of replay sequence numbers. 558178354Ssam */ 559262007Skevlo if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 560178354Ssam if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) { 561178354Ssam /* 562178354Ssam * Discard encrypted frames when privacy is off. 563178354Ssam */ 564178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 565178354Ssam wh, "WEP", "%s", "PRIVACY off"); 566178354Ssam vap->iv_stats.is_rx_noprivacy++; 567178354Ssam IEEE80211_NODE_STAT(ni, rx_noprivacy); 568178354Ssam goto out; 569178354Ssam } 570178354Ssam key = ieee80211_crypto_decap(ni, m, hdrspace); 571178354Ssam if (key == NULL) { 572178354Ssam /* NB: stats+msgs handled in crypto_decap */ 573178354Ssam IEEE80211_NODE_STAT(ni, rx_wepfail); 574178354Ssam goto out; 575178354Ssam } 576178354Ssam wh = mtod(m, struct ieee80211_frame *); 577262007Skevlo wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED; 578178354Ssam } else { 579178354Ssam /* XXX M_WEP and IEEE80211_F_PRIVACY */ 580178354Ssam key = NULL; 581178354Ssam } 582178354Ssam 583178354Ssam /* 584178354Ssam * Save QoS bits for use below--before we strip the header. 585178354Ssam */ 586178354Ssam if (subtype == IEEE80211_FC0_SUBTYPE_QOS) { 587178354Ssam qos = (dir == IEEE80211_FC1_DIR_DSTODS) ? 588178354Ssam ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] : 589178354Ssam ((struct ieee80211_qosframe *)wh)->i_qos[0]; 590178354Ssam } else 591178354Ssam qos = 0; 592178354Ssam 593178354Ssam /* 594178354Ssam * Next up, any fragmentation. 595178354Ssam */ 596178354Ssam if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 597178354Ssam m = ieee80211_defrag(ni, m, hdrspace); 598178354Ssam if (m == NULL) { 599178354Ssam /* Fragment dropped or frame not complete yet */ 600178354Ssam goto out; 601178354Ssam } 602178354Ssam } 603178354Ssam wh = NULL; /* no longer valid, catch any uses */ 604178354Ssam 605178354Ssam /* 606178354Ssam * Next strip any MSDU crypto bits. 607178354Ssam */ 608178354Ssam if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) { 609178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 610178354Ssam ni->ni_macaddr, "data", "%s", "demic error"); 611178354Ssam vap->iv_stats.is_rx_demicfail++; 612178354Ssam IEEE80211_NODE_STAT(ni, rx_demicfail); 613178354Ssam goto out; 614178354Ssam } 615178354Ssam 616178354Ssam /* copy to listener after decrypt */ 617192468Ssam if (ieee80211_radiotap_active_vap(vap)) 618192468Ssam ieee80211_radiotap_rx(vap, m); 619178354Ssam need_tap = 0; 620178354Ssam 621178354Ssam /* 622178354Ssam * Finally, strip the 802.11 header. 623178354Ssam */ 624178354Ssam m = ieee80211_decap(vap, m, hdrspace); 625178354Ssam if (m == NULL) { 626178354Ssam /* XXX mask bit to check for both */ 627178354Ssam /* don't count Null data frames as errors */ 628178354Ssam if (subtype == IEEE80211_FC0_SUBTYPE_NODATA || 629178354Ssam subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL) 630178354Ssam goto out; 631178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 632178354Ssam ni->ni_macaddr, "data", "%s", "decap error"); 633178354Ssam vap->iv_stats.is_rx_decap++; 634178354Ssam IEEE80211_NODE_STAT(ni, rx_decap); 635178354Ssam goto err; 636178354Ssam } 637178354Ssam eh = mtod(m, struct ether_header *); 638178354Ssam if (!ieee80211_node_is_authorized(ni)) { 639178354Ssam /* 640178354Ssam * Deny any non-PAE frames received prior to 641178354Ssam * authorization. For open/shared-key 642178354Ssam * authentication the port is mark authorized 643178354Ssam * after authentication completes. For 802.1x 644178354Ssam * the port is not marked authorized by the 645178354Ssam * authenticator until the handshake has completed. 646178354Ssam */ 647178354Ssam if (eh->ether_type != htons(ETHERTYPE_PAE)) { 648178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 649178354Ssam eh->ether_shost, "data", 650178354Ssam "unauthorized port: ether type 0x%x len %u", 651178354Ssam eh->ether_type, m->m_pkthdr.len); 652178354Ssam vap->iv_stats.is_rx_unauth++; 653178354Ssam IEEE80211_NODE_STAT(ni, rx_unauth); 654178354Ssam goto err; 655178354Ssam } 656178354Ssam } else { 657178354Ssam /* 658178354Ssam * When denying unencrypted frames, discard 659178354Ssam * any non-PAE frames received without encryption. 660178354Ssam */ 661178354Ssam if ((vap->iv_flags & IEEE80211_F_DROPUNENC) && 662178354Ssam (key == NULL && (m->m_flags & M_WEP) == 0) && 663178354Ssam eh->ether_type != htons(ETHERTYPE_PAE)) { 664178354Ssam /* 665178354Ssam * Drop unencrypted frames. 666178354Ssam */ 667178354Ssam vap->iv_stats.is_rx_unencrypted++; 668178354Ssam IEEE80211_NODE_STAT(ni, rx_unencrypted); 669178354Ssam goto out; 670178354Ssam } 671178354Ssam } 672178354Ssam /* XXX require HT? */ 673178354Ssam if (qos & IEEE80211_QOS_AMSDU) { 674178354Ssam m = ieee80211_decap_amsdu(ni, m); 675178354Ssam if (m == NULL) 676178354Ssam return IEEE80211_FC0_TYPE_DATA; 677190391Ssam } else { 678190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG 679190391Ssam m = ieee80211_decap_fastframe(vap, ni, m); 680190391Ssam if (m == NULL) 681178354Ssam return IEEE80211_FC0_TYPE_DATA; 682190391Ssam#endif 683178354Ssam } 684178354Ssam ieee80211_deliver_data(vap, ni, m); 685178354Ssam return IEEE80211_FC0_TYPE_DATA; 686178354Ssam 687178354Ssam case IEEE80211_FC0_TYPE_MGT: 688178354Ssam vap->iv_stats.is_rx_mgmt++; 689178354Ssam IEEE80211_NODE_STAT(ni, rx_mgmt); 690178354Ssam if (dir != IEEE80211_FC1_DIR_NODS) { 691178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 692178354Ssam wh, "data", "incorrect dir 0x%x", dir); 693178354Ssam vap->iv_stats.is_rx_wrongdir++; 694178354Ssam goto err; 695178354Ssam } 696178354Ssam if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) { 697178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 698178354Ssam ni->ni_macaddr, "mgt", "too short: len %u", 699178354Ssam m->m_pkthdr.len); 700178354Ssam vap->iv_stats.is_rx_tooshort++; 701178354Ssam goto out; 702178354Ssam } 703178354Ssam#ifdef IEEE80211_DEBUG 704178354Ssam if (ieee80211_msg_debug(vap) || ieee80211_msg_dumppkts(vap)) { 705178354Ssam if_printf(ifp, "received %s from %s rssi %d\n", 706178354Ssam ieee80211_mgt_subtype_name[subtype >> 707178354Ssam IEEE80211_FC0_SUBTYPE_SHIFT], 708178354Ssam ether_sprintf(wh->i_addr2), rssi); 709178354Ssam } 710178354Ssam#endif 711262007Skevlo if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 712178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 713178354Ssam wh, NULL, "%s", "WEP set but not permitted"); 714178354Ssam vap->iv_stats.is_rx_mgtdiscard++; /* XXX */ 715178354Ssam goto out; 716178354Ssam } 717192468Ssam vap->iv_recv_mgmt(ni, m, subtype, rssi, nf); 718192468Ssam goto out; 719178354Ssam 720178354Ssam case IEEE80211_FC0_TYPE_CTL: 721178354Ssam vap->iv_stats.is_rx_ctl++; 722178354Ssam IEEE80211_NODE_STAT(ni, rx_ctrl); 723178354Ssam goto out; 724192468Ssam 725178354Ssam default: 726178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 727178354Ssam wh, "bad", "frame type 0x%x", type); 728178354Ssam /* should not come here */ 729178354Ssam break; 730178354Ssam } 731178354Ssamerr: 732178354Ssam ifp->if_ierrors++; 733178354Ssamout: 734178354Ssam if (m != NULL) { 735192765Ssam if (need_tap && ieee80211_radiotap_active_vap(vap)) 736192468Ssam ieee80211_radiotap_rx(vap, m); 737178354Ssam m_freem(m); 738178354Ssam } 739178354Ssam return type; 740178354Ssam} 741178354Ssam 742178354Ssamstatic void 743178354Ssamwds_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, 744192468Ssam int subtype, int rssi, int nf) 745178354Ssam{ 746178354Ssam struct ieee80211vap *vap = ni->ni_vap; 747178354Ssam struct ieee80211com *ic = ni->ni_ic; 748178354Ssam struct ieee80211_frame *wh; 749178354Ssam u_int8_t *frm, *efrm; 750178354Ssam 751178354Ssam wh = mtod(m0, struct ieee80211_frame *); 752178354Ssam frm = (u_int8_t *)&wh[1]; 753178354Ssam efrm = mtod(m0, u_int8_t *) + m0->m_len; 754178354Ssam switch (subtype) { 755218927Sbschmidt case IEEE80211_FC0_SUBTYPE_ACTION: 756218927Sbschmidt case IEEE80211_FC0_SUBTYPE_ACTION_NOACK: 757218958Sbschmidt if (ni == vap->iv_bss) { 758218958Sbschmidt IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 759218958Sbschmidt wh, NULL, "%s", "unknown node"); 760218927Sbschmidt vap->iv_stats.is_rx_mgtdiscard++; 761218958Sbschmidt } else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1)) { 762218958Sbschmidt /* NB: not interested in multicast frames. */ 763218927Sbschmidt IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 764218958Sbschmidt wh, NULL, "%s", "not for us"); 765218958Sbschmidt vap->iv_stats.is_rx_mgtdiscard++; 766218958Sbschmidt } else if (vap->iv_state != IEEE80211_S_RUN) { 767218958Sbschmidt IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 768218927Sbschmidt wh, NULL, "wrong state %s", 769218927Sbschmidt ieee80211_state_name[vap->iv_state]); 770218927Sbschmidt vap->iv_stats.is_rx_mgtdiscard++; 771218958Sbschmidt } else { 772218958Sbschmidt if (ieee80211_parse_action(ni, m0) == 0) 773218958Sbschmidt (void)ic->ic_recv_action(ni, wh, frm, efrm); 774218927Sbschmidt } 775218927Sbschmidt break; 776218927Sbschmidt 777178354Ssam case IEEE80211_FC0_SUBTYPE_ASSOC_REQ: 778218927Sbschmidt case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 779178354Ssam case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: 780178354Ssam case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: 781218927Sbschmidt case IEEE80211_FC0_SUBTYPE_PROBE_REQ: 782218927Sbschmidt case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 783218927Sbschmidt case IEEE80211_FC0_SUBTYPE_BEACON: 784218927Sbschmidt case IEEE80211_FC0_SUBTYPE_ATIM: 785178354Ssam case IEEE80211_FC0_SUBTYPE_DISASSOC: 786218927Sbschmidt case IEEE80211_FC0_SUBTYPE_AUTH: 787218927Sbschmidt case IEEE80211_FC0_SUBTYPE_DEAUTH: 788218927Sbschmidt IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 789218927Sbschmidt wh, NULL, "%s", "not handled"); 790178354Ssam vap->iv_stats.is_rx_mgtdiscard++; 791178354Ssam break; 792218927Sbschmidt 793178354Ssam default: 794178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 795218927Sbschmidt wh, "mgt", "subtype 0x%x not handled", subtype); 796178354Ssam vap->iv_stats.is_rx_badsubtype++; 797178354Ssam break; 798178354Ssam } 799178354Ssam} 800