ieee80211_wds.c revision 191547
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: head/sys/net80211/ieee80211_wds.c 191547 2009-04-26 21:50:21Z sam $"); 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); 66178354Ssamstatic int wds_input(struct ieee80211_node *ni, struct mbuf *m, 67178354Ssam int rssi, int noise, uint32_t rstamp); 68178354Ssamstatic void wds_recv_mgmt(struct ieee80211_node *, struct mbuf *, 69178354Ssam int subtype, int rssi, int noise, u_int32_t rstamp); 70178354Ssam 71178354Ssamvoid 72178354Ssamieee80211_wds_attach(struct ieee80211com *ic) 73178354Ssam{ 74178354Ssam ic->ic_vattach[IEEE80211_M_WDS] = wds_vattach; 75178354Ssam} 76178354Ssam 77178354Ssamvoid 78178354Ssamieee80211_wds_detach(struct ieee80211com *ic) 79178354Ssam{ 80178354Ssam} 81178354Ssam 82178354Ssamstatic void 83178354Ssamwds_vdetach(struct ieee80211vap *vap) 84178354Ssam{ 85178354Ssam if (vap->iv_bss != NULL) { 86178354Ssam /* XXX locking? */ 87178354Ssam if (vap->iv_bss->ni_wdsvap == vap) 88178354Ssam vap->iv_bss->ni_wdsvap = NULL; 89178354Ssam } 90178354Ssam} 91178354Ssam 92178354Ssamstatic void 93178354Ssamwds_vattach(struct ieee80211vap *vap) 94178354Ssam{ 95178354Ssam vap->iv_newstate = wds_newstate; 96178354Ssam vap->iv_input = wds_input; 97178354Ssam vap->iv_recv_mgmt = wds_recv_mgmt; 98178354Ssam vap->iv_opdetach = wds_vdetach; 99178354Ssam} 100178354Ssam 101178354Ssamstatic int 102178354Ssamieee80211_create_wds(struct ieee80211vap *vap, struct ieee80211_channel *chan) 103178354Ssam{ 104178354Ssam struct ieee80211com *ic = vap->iv_ic; 105178354Ssam struct ieee80211_node_table *nt = &ic->ic_sta; 106178354Ssam struct ieee80211_node *ni, *obss; 107178354Ssam 108178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_WDS, 109178354Ssam "%s: creating link to %s on channel %u\n", __func__, 110178354Ssam ether_sprintf(vap->iv_des_bssid), ieee80211_chan2ieee(ic, chan)); 111178354Ssam 112178354Ssam /* NB: vap create must specify the bssid for the link */ 113178354Ssam KASSERT(vap->iv_flags & IEEE80211_F_DESBSSID, ("no bssid")); 114178354Ssam /* NB: we should only be called on RUN transition */ 115178354Ssam KASSERT(vap->iv_state == IEEE80211_S_RUN, ("!RUN state")); 116178354Ssam 117178354Ssam if ((vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0) { 118178354Ssam /* 119178354Ssam * Dynamic/non-legacy WDS. Reference the associated 120178354Ssam * station specified by the desired bssid setup at vap 121178354Ssam * create. Point ni_wdsvap at the WDS vap so 4-address 122178354Ssam * frames received through the associated AP vap will 123178354Ssam * be dispatched upward (e.g. to a bridge) as though 124178354Ssam * they arrived on the WDS vap. 125178354Ssam */ 126178354Ssam IEEE80211_NODE_LOCK(nt); 127178354Ssam obss = NULL; 128178354Ssam ni = ieee80211_find_node_locked(&ic->ic_sta, vap->iv_des_bssid); 129178354Ssam if (ni == NULL) { 130178354Ssam /* 131178354Ssam * Node went away before we could hookup. This 132178354Ssam * should be ok; no traffic will flow and a leave 133178354Ssam * event will be dispatched that should cause 134178354Ssam * the vap to be destroyed. 135178354Ssam */ 136178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_WDS, 137178354Ssam "%s: station %s went away\n", 138178354Ssam __func__, ether_sprintf(vap->iv_des_bssid)); 139178354Ssam /* XXX stat? */ 140178354Ssam } else if (ni->ni_wdsvap != NULL) { 141178354Ssam /* 142178354Ssam * Node already setup with a WDS vap; we cannot 143178354Ssam * allow multiple references so disallow. If 144178354Ssam * ni_wdsvap points at us that's ok; we should 145178354Ssam * do nothing anyway. 146178354Ssam */ 147178354Ssam /* XXX printf instead? */ 148178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_WDS, 149178354Ssam "%s: station %s in use with %s\n", 150178354Ssam __func__, ether_sprintf(vap->iv_des_bssid), 151178354Ssam ni->ni_wdsvap->iv_ifp->if_xname); 152178354Ssam /* XXX stat? */ 153178354Ssam } else { 154178354Ssam /* 155178354Ssam * Committed to new node, setup state. 156178354Ssam */ 157178354Ssam obss = vap->iv_bss; 158178354Ssam vap->iv_bss = ni; 159178354Ssam ni->ni_wdsvap = vap; 160178354Ssam } 161178354Ssam IEEE80211_NODE_UNLOCK(nt); 162178354Ssam if (obss != NULL) { 163178354Ssam /* NB: deferred to avoid recursive lock */ 164178354Ssam ieee80211_free_node(obss); 165178354Ssam } 166178354Ssam } else { 167178354Ssam /* 168178354Ssam * Legacy WDS vap setup. 169178354Ssam */ 170178354Ssam /* 171178354Ssam * The far end does not associate so we just create 172178354Ssam * create a new node and install it as the vap's 173178354Ssam * bss node. We must simulate an association and 174178354Ssam * authorize the port for traffic to flow. 175178354Ssam * XXX check if node already in sta table? 176178354Ssam */ 177178354Ssam ni = ieee80211_node_create_wds(vap, vap->iv_des_bssid, chan); 178178354Ssam if (ni != NULL) { 179178354Ssam obss = vap->iv_bss; 180178354Ssam vap->iv_bss = ieee80211_ref_node(ni); 181178354Ssam ni->ni_flags |= IEEE80211_NODE_AREF; 182178354Ssam if (obss != NULL) 183178354Ssam ieee80211_free_node(obss); 184178354Ssam /* give driver a chance to setup state like ni_txrate */ 185178354Ssam if (ic->ic_newassoc != NULL) 186178354Ssam ic->ic_newassoc(ni, 1); 187178354Ssam /* tell the authenticator about new station */ 188178354Ssam if (vap->iv_auth->ia_node_join != NULL) 189178354Ssam vap->iv_auth->ia_node_join(ni); 190178354Ssam if (ni->ni_authmode != IEEE80211_AUTH_8021X) 191178354Ssam ieee80211_node_authorize(ni); 192178354Ssam 193178354Ssam ieee80211_notify_node_join(ni, 1 /*newassoc*/); 194178354Ssam /* XXX inject l2uf frame */ 195178354Ssam } 196178354Ssam } 197178354Ssam 198178354Ssam /* 199178354Ssam * Flush pending frames now that were setup. 200178354Ssam */ 201178354Ssam if (ni != NULL && IEEE80211_NODE_WDSQ_QLEN(ni) != 0) { 202178354Ssam int8_t rssi, noise; 203178354Ssam 204178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_WDS, ni, 205178354Ssam "flush wds queue, %u packets queued", 206178354Ssam IEEE80211_NODE_WDSQ_QLEN(ni)); 207178354Ssam ic->ic_node_getsignal(ni, &rssi, &noise); 208178354Ssam for (;;) { 209178354Ssam struct mbuf *m; 210178354Ssam 211178354Ssam IEEE80211_NODE_WDSQ_LOCK(ni); 212178354Ssam _IEEE80211_NODE_WDSQ_DEQUEUE_HEAD(ni, m); 213178354Ssam IEEE80211_NODE_WDSQ_UNLOCK(ni); 214178354Ssam if (m == NULL) 215178354Ssam break; 216178354Ssam /* XXX cheat and re-use last rstamp */ 217178354Ssam ieee80211_input(ni, m, rssi, noise, ni->ni_rstamp); 218178354Ssam } 219178354Ssam } 220178354Ssam return (ni == NULL ? ENOENT : 0); 221178354Ssam} 222178354Ssam 223178354Ssam/* 224178354Ssam * Propagate multicast frames of an ap vap to all DWDS links. 225178354Ssam * The caller is assumed to have verified this frame is multicast. 226178354Ssam */ 227178354Ssamvoid 228178354Ssamieee80211_dwds_mcast(struct ieee80211vap *vap0, struct mbuf *m) 229178354Ssam{ 230178354Ssam struct ieee80211com *ic = vap0->iv_ic; 231178354Ssam struct ifnet *parent = ic->ic_ifp; 232178354Ssam const struct ether_header *eh = mtod(m, const struct ether_header *); 233178354Ssam struct ieee80211_node *ni; 234178354Ssam struct ieee80211vap *vap; 235178354Ssam struct ifnet *ifp; 236178354Ssam struct mbuf *mcopy; 237178354Ssam int err; 238178354Ssam 239178354Ssam KASSERT(ETHER_IS_MULTICAST(eh->ether_dhost), 240178354Ssam ("%s not mcast", ether_sprintf(eh->ether_dhost))); 241178354Ssam 242178354Ssam /* XXX locking */ 243178354Ssam TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 244178354Ssam /* only DWDS vaps are interesting */ 245178354Ssam if (vap->iv_opmode != IEEE80211_M_WDS || 246178354Ssam (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)) 247178354Ssam continue; 248178354Ssam /* if it came in this interface, don't send it back out */ 249178354Ssam ifp = vap->iv_ifp; 250178354Ssam if (ifp == m->m_pkthdr.rcvif) 251178354Ssam continue; 252178354Ssam /* 253190672Ssam * Duplicate the frame and send it. 254178354Ssam */ 255178354Ssam mcopy = m_copypacket(m, M_DONTWAIT); 256178354Ssam if (mcopy == NULL) { 257178354Ssam ifp->if_oerrors++; 258178354Ssam /* XXX stat + msg */ 259178354Ssam continue; 260178354Ssam } 261178354Ssam ni = ieee80211_find_txnode(vap, eh->ether_dhost); 262178354Ssam if (ni == NULL) { 263178354Ssam /* NB: ieee80211_find_txnode does stat+msg */ 264178354Ssam ifp->if_oerrors++; 265178354Ssam m_freem(mcopy); 266178354Ssam continue; 267178354Ssam } 268190672Ssam /* calculate priority so drivers can find the tx queue */ 269178354Ssam if (ieee80211_classify(ni, mcopy)) { 270178354Ssam IEEE80211_DISCARD_MAC(vap, 271178354Ssam IEEE80211_MSG_OUTPUT | IEEE80211_MSG_WDS, 272178354Ssam eh->ether_dhost, NULL, 273178354Ssam "%s", "classification failure"); 274178354Ssam vap->iv_stats.is_tx_classify++; 275178354Ssam ifp->if_oerrors++; 276178354Ssam m_freem(mcopy); 277178354Ssam ieee80211_free_node(ni); 278178354Ssam continue; 279178354Ssam } 280191542Ssam 281191542Ssam BPF_MTAP(ifp, m); /* 802.3 tx */ 282191542Ssam 283190672Ssam /* 284190672Ssam * Encapsulate the packet in prep for transmission. 285190672Ssam */ 286190672Ssam mcopy = ieee80211_encap(vap, ni, mcopy); 287190672Ssam if (m == NULL) { 288190672Ssam /* NB: stat+msg handled in ieee80211_encap */ 289190672Ssam ieee80211_free_node(ni); 290190672Ssam continue; 291190672Ssam } 292190672Ssam mcopy->m_flags |= M_MCAST; 293178354Ssam mcopy->m_pkthdr.rcvif = (void *) ni; 294178354Ssam 295191542Ssam if (bpf_peers_present(vap->iv_rawbpf)) 296191542Ssam bpf_mtap(vap->iv_rawbpf, m); 297191542Ssam 298186658Ssam err = parent->if_transmit(parent, 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 ieee80211vap *vap = ni->ni_vap; 319178354Ssam struct ieee80211com *ic = ni->ni_ic; 320178354Ssam int qlen, age; 321178354Ssam 322178354Ssam IEEE80211_NODE_WDSQ_LOCK(ni); 323178354Ssam if (!_IF_QFULL(&ni->ni_wdsq)) { 324178354Ssam /* 325178354Ssam * Tag the frame with it's expiry time and insert 326178354Ssam * it in the queue. The aging interval is 4 times 327178354Ssam * the listen interval specified by the station. 328178354Ssam * Frames that sit around too long are reclaimed 329178354Ssam * using this information. 330178354Ssam */ 331178354Ssam /* XXX handle overflow? */ 332178354Ssam /* XXX per/vap beacon interval? */ 333178354Ssam /* NB: TU -> secs */ 334178354Ssam age = ((ni->ni_intval * ic->ic_lintval) << 2) / 1024; 335178354Ssam _IEEE80211_NODE_WDSQ_ENQUEUE(ni, m, qlen, age); 336178354Ssam IEEE80211_NODE_WDSQ_UNLOCK(ni); 337178354Ssam 338178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_WDS, ni, 339178354Ssam "save frame, %u now queued", qlen); 340178354Ssam } else { 341178354Ssam vap->iv_stats.is_dwds_qdrop++; 342178354Ssam _IF_DROP(&ni->ni_wdsq); 343178354Ssam IEEE80211_NODE_WDSQ_UNLOCK(ni); 344178354Ssam 345178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT | IEEE80211_MSG_WDS, 346178354Ssam mtod(m, struct ieee80211_frame *), "wds data", 347178354Ssam "pending q overflow, drops %d (len %d)", 348178354Ssam ni->ni_wdsq.ifq_drops, ni->ni_wdsq.ifq_len); 349178354Ssam 350178354Ssam#ifdef IEEE80211_DEBUG 351178354Ssam if (ieee80211_msg_dumppkts(vap)) 352178354Ssam ieee80211_dump_pkt(ic, mtod(m, caddr_t), 353178354Ssam m->m_len, -1, -1); 354178354Ssam#endif 355178354Ssam /* XXX tail drop? */ 356178354Ssam m_freem(m); 357178354Ssam } 358178354Ssam ieee80211_notify_wds_discover(ni); 359178354Ssam} 360178354Ssam 361178354Ssam/* 362178354Ssam * Age frames on the WDS pending queue. The aging interval is 363178354Ssam * 4 times the listen interval specified by the station. This 364178354Ssam * number is factored into the age calculations when the frame 365178354Ssam * is placed on the queue. We store ages as time differences 366178354Ssam * so we can check and/or adjust only the head of the list. 367178354Ssam * If a frame's age exceeds the threshold then discard it. 368178354Ssam * The number of frames discarded is returned to the caller. 369178354Ssam */ 370178354Ssamint 371178354Ssamieee80211_node_wdsq_age(struct ieee80211_node *ni) 372178354Ssam{ 373178354Ssam#ifdef IEEE80211_DEBUG 374178354Ssam struct ieee80211vap *vap = ni->ni_vap; 375178354Ssam#endif 376178354Ssam struct mbuf *m; 377178354Ssam int discard = 0; 378178354Ssam 379178354Ssam IEEE80211_NODE_WDSQ_LOCK(ni); 380178354Ssam while (_IF_POLL(&ni->ni_wdsq, m) != NULL && 381178354Ssam M_AGE_GET(m) < IEEE80211_INACT_WAIT) { 382178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_WDS, ni, 383178354Ssam "discard frame, age %u", M_AGE_GET(m)); 384178354Ssam 385178354Ssam /* XXX could be optimized */ 386178354Ssam _IEEE80211_NODE_WDSQ_DEQUEUE_HEAD(ni, m); 387178354Ssam m_freem(m); 388178354Ssam discard++; 389178354Ssam } 390178354Ssam if (m != NULL) 391178354Ssam M_AGE_SUB(m, IEEE80211_INACT_WAIT); 392178354Ssam IEEE80211_NODE_WDSQ_UNLOCK(ni); 393178354Ssam 394178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_WDS, ni, 395178354Ssam "discard %u frames for age", discard); 396178354Ssam#if 0 397178354Ssam IEEE80211_NODE_STAT_ADD(ni, wds_discard, discard); 398178354Ssam#endif 399178354Ssam return discard; 400178354Ssam} 401178354Ssam 402178354Ssam/* 403178354Ssam * IEEE80211_M_WDS vap state machine handler. 404178354Ssam */ 405178354Ssamstatic int 406178354Ssamwds_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 407178354Ssam{ 408178354Ssam struct ieee80211com *ic = vap->iv_ic; 409178354Ssam struct ieee80211_node *ni; 410178354Ssam enum ieee80211_state ostate; 411178354Ssam int error; 412178354Ssam 413178354Ssam IEEE80211_LOCK_ASSERT(ic); 414178354Ssam 415178354Ssam ostate = vap->iv_state; 416178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s\n", __func__, 417178354Ssam ieee80211_state_name[ostate], ieee80211_state_name[nstate]); 418178354Ssam vap->iv_state = nstate; /* state transition */ 419178354Ssam callout_stop(&vap->iv_mgtsend); /* XXX callout_drain */ 420178354Ssam if (ostate != IEEE80211_S_SCAN) 421178354Ssam ieee80211_cancel_scan(vap); /* background scan */ 422178354Ssam ni = vap->iv_bss; /* NB: no reference held */ 423178354Ssam error = 0; 424178354Ssam switch (nstate) { 425178354Ssam case IEEE80211_S_INIT: 426178354Ssam switch (ostate) { 427178354Ssam case IEEE80211_S_SCAN: 428178354Ssam ieee80211_cancel_scan(vap); 429178354Ssam break; 430178354Ssam default: 431178354Ssam break; 432178354Ssam } 433178354Ssam if (ostate != IEEE80211_S_INIT) { 434178354Ssam /* NB: optimize INIT -> INIT case */ 435178354Ssam ieee80211_reset_bss(vap); 436178354Ssam } 437178354Ssam break; 438178354Ssam case IEEE80211_S_SCAN: 439178354Ssam switch (ostate) { 440178354Ssam case IEEE80211_S_INIT: 441178354Ssam ieee80211_check_scan_current(vap); 442178354Ssam break; 443178354Ssam default: 444178354Ssam break; 445178354Ssam } 446178354Ssam break; 447178354Ssam case IEEE80211_S_RUN: 448178354Ssam if (ostate == IEEE80211_S_INIT) { 449178354Ssam /* 450178354Ssam * Already have a channel; bypass the scan 451178354Ssam * and startup immediately. 452178354Ssam */ 453178354Ssam error = ieee80211_create_wds(vap, ic->ic_curchan); 454178354Ssam } 455178354Ssam break; 456178354Ssam default: 457178354Ssam break; 458178354Ssam } 459178354Ssam return error; 460178354Ssam} 461178354Ssam 462178354Ssam/* 463178354Ssam * Process a received frame. The node associated with the sender 464178354Ssam * should be supplied. If nothing was found in the node table then 465178354Ssam * the caller is assumed to supply a reference to iv_bss instead. 466178354Ssam * The RSSI and a timestamp are also supplied. The RSSI data is used 467178354Ssam * during AP scanning to select a AP to associate with; it can have 468178354Ssam * any units so long as values have consistent units and higher values 469178354Ssam * mean ``better signal''. The receive timestamp is currently not used 470178354Ssam * by the 802.11 layer. 471178354Ssam */ 472178354Ssamstatic int 473178354Ssamwds_input(struct ieee80211_node *ni, struct mbuf *m, 474178354Ssam int rssi, int noise, uint32_t rstamp) 475178354Ssam{ 476178354Ssam#define SEQ_LEQ(a,b) ((int)((a)-(b)) <= 0) 477178354Ssam#define HAS_SEQ(type) ((type & 0x4) == 0) 478178354Ssam struct ieee80211vap *vap = ni->ni_vap; 479178354Ssam struct ieee80211com *ic = ni->ni_ic; 480178354Ssam struct ifnet *ifp = vap->iv_ifp; 481178354Ssam struct ieee80211_frame *wh; 482178354Ssam struct ieee80211_key *key; 483178354Ssam struct ether_header *eh; 484178354Ssam int hdrspace, need_tap; 485178354Ssam uint8_t dir, type, subtype, qos; 486178354Ssam uint16_t rxseq; 487178354Ssam 488183247Ssam if (m->m_flags & M_AMPDU_MPDU) { 489178354Ssam /* 490178354Ssam * Fastpath for A-MPDU reorder q resubmission. Frames 491183247Ssam * w/ M_AMPDU_MPDU marked have already passed through 492183247Ssam * here but were received out of order and been held on 493183247Ssam * the reorder queue. When resubmitted they are marked 494183247Ssam * with the M_AMPDU_MPDU flag and we can bypass most of 495183247Ssam * the normal processing. 496178354Ssam */ 497178354Ssam wh = mtod(m, struct ieee80211_frame *); 498178354Ssam type = IEEE80211_FC0_TYPE_DATA; 499178354Ssam dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 500178354Ssam subtype = IEEE80211_FC0_SUBTYPE_QOS; 501178354Ssam hdrspace = ieee80211_hdrspace(ic, wh); /* XXX optimize? */ 502178354Ssam goto resubmit_ampdu; 503178354Ssam } 504178354Ssam 505178354Ssam KASSERT(ni != NULL, ("null node")); 506178354Ssam 507178354Ssam need_tap = 1; /* mbuf need to be tapped. */ 508178354Ssam type = -1; /* undefined */ 509178354Ssam 510178354Ssam if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) { 511178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 512178354Ssam ni->ni_macaddr, NULL, 513178354Ssam "too short (1): len %u", m->m_pkthdr.len); 514178354Ssam vap->iv_stats.is_rx_tooshort++; 515178354Ssam goto out; 516178354Ssam } 517178354Ssam /* 518178354Ssam * Bit of a cheat here, we use a pointer for a 3-address 519178354Ssam * frame format but don't reference fields past outside 520178354Ssam * ieee80211_frame_min w/o first validating the data is 521178354Ssam * present. 522178354Ssam */ 523178354Ssam wh = mtod(m, struct ieee80211_frame *); 524178354Ssam 525178354Ssam if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) != 526178354Ssam IEEE80211_FC0_VERSION_0) { 527178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 528191547Ssam ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x", 529191547Ssam wh->i_fc[0], wh->i_fc[1]); 530178354Ssam vap->iv_stats.is_rx_badversion++; 531178354Ssam goto err; 532178354Ssam } 533178354Ssam 534178354Ssam dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 535178354Ssam type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 536178354Ssam subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 537178354Ssam 538178354Ssam /* NB: WDS vap's do not scan */ 539178354Ssam if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_addr4)) { 540178354Ssam IEEE80211_DISCARD_MAC(vap, 541178354Ssam IEEE80211_MSG_ANY, ni->ni_macaddr, NULL, 542178354Ssam "too short (3): len %u", m->m_pkthdr.len); 543178354Ssam vap->iv_stats.is_rx_tooshort++; 544178354Ssam goto out; 545178354Ssam } 546178354Ssam /* NB: the TA is implicitly verified by finding the wds peer node */ 547178354Ssam if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr) && 548178354Ssam !IEEE80211_ADDR_EQ(wh->i_addr1, ifp->if_broadcastaddr)) { 549178354Ssam /* not interested in */ 550178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 551178354Ssam wh->i_addr1, NULL, "%s", "not to bss"); 552178354Ssam vap->iv_stats.is_rx_wrongbss++; 553178354Ssam goto out; 554178354Ssam } 555178354Ssam IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi); 556178354Ssam ni->ni_noise = noise; 557178354Ssam ni->ni_rstamp = rstamp; 558178354Ssam if (HAS_SEQ(type)) { 559178354Ssam uint8_t tid = ieee80211_gettid(wh); 560178354Ssam if (IEEE80211_QOS_HAS_SEQ(wh) && 561178354Ssam TID_TO_WME_AC(tid) >= WME_AC_VI) 562178354Ssam ic->ic_wme.wme_hipri_traffic++; 563178354Ssam rxseq = le16toh(*(uint16_t *)wh->i_seq); 564178354Ssam if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 && 565178354Ssam (wh->i_fc[1] & IEEE80211_FC1_RETRY) && 566178354Ssam SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) { 567178354Ssam /* duplicate, discard */ 568178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 569178354Ssam wh->i_addr1, "duplicate", 570178354Ssam "seqno <%u,%u> fragno <%u,%u> tid %u", 571178354Ssam rxseq >> IEEE80211_SEQ_SEQ_SHIFT, 572178354Ssam ni->ni_rxseqs[tid] >> IEEE80211_SEQ_SEQ_SHIFT, 573178354Ssam rxseq & IEEE80211_SEQ_FRAG_MASK, 574178354Ssam ni->ni_rxseqs[tid] & IEEE80211_SEQ_FRAG_MASK, 575178354Ssam tid); 576178354Ssam vap->iv_stats.is_rx_dup++; 577178354Ssam IEEE80211_NODE_STAT(ni, rx_dup); 578178354Ssam goto out; 579178354Ssam } 580178354Ssam ni->ni_rxseqs[tid] = rxseq; 581178354Ssam } 582178354Ssam switch (type) { 583178354Ssam case IEEE80211_FC0_TYPE_DATA: 584178354Ssam hdrspace = ieee80211_hdrspace(ic, wh); 585178354Ssam if (m->m_len < hdrspace && 586178354Ssam (m = m_pullup(m, hdrspace)) == NULL) { 587178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 588178354Ssam ni->ni_macaddr, NULL, 589178354Ssam "data too short: expecting %u", hdrspace); 590178354Ssam vap->iv_stats.is_rx_tooshort++; 591178354Ssam goto out; /* XXX */ 592178354Ssam } 593178354Ssam if (dir != IEEE80211_FC1_DIR_DSTODS) { 594178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 595178354Ssam wh, "data", "incorrect dir 0x%x", dir); 596178354Ssam vap->iv_stats.is_rx_wrongdir++; 597178354Ssam goto out; 598178354Ssam } 599178354Ssam /* 600178354Ssam * Only legacy WDS traffic should take this path. 601178354Ssam */ 602178354Ssam if ((vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0) { 603178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 604178354Ssam wh, "data", "%s", "not legacy wds"); 605178354Ssam vap->iv_stats.is_rx_wrongdir++;/*XXX*/ 606178354Ssam goto out; 607178354Ssam } 608178354Ssam if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) 609178354Ssam ni->ni_inact = ni->ni_inact_reload; 610178354Ssam /* 611183247Ssam * Handle A-MPDU re-ordering. If the frame is to be 612183247Ssam * processed directly then ieee80211_ampdu_reorder 613178354Ssam * will return 0; otherwise it has consumed the mbuf 614178354Ssam * and we should do nothing more with it. 615178354Ssam */ 616183247Ssam if ((m->m_flags & M_AMPDU) && 617178354Ssam ieee80211_ampdu_reorder(ni, m) != 0) { 618178354Ssam m = NULL; 619178354Ssam goto out; 620178354Ssam } 621178354Ssam resubmit_ampdu: 622178354Ssam 623178354Ssam /* 624178354Ssam * Handle privacy requirements. Note that we 625178354Ssam * must not be preempted from here until after 626178354Ssam * we (potentially) call ieee80211_crypto_demic; 627178354Ssam * otherwise we may violate assumptions in the 628178354Ssam * crypto cipher modules used to do delayed update 629178354Ssam * of replay sequence numbers. 630178354Ssam */ 631178354Ssam if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 632178354Ssam if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) { 633178354Ssam /* 634178354Ssam * Discard encrypted frames when privacy is off. 635178354Ssam */ 636178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 637178354Ssam wh, "WEP", "%s", "PRIVACY off"); 638178354Ssam vap->iv_stats.is_rx_noprivacy++; 639178354Ssam IEEE80211_NODE_STAT(ni, rx_noprivacy); 640178354Ssam goto out; 641178354Ssam } 642178354Ssam key = ieee80211_crypto_decap(ni, m, hdrspace); 643178354Ssam if (key == NULL) { 644178354Ssam /* NB: stats+msgs handled in crypto_decap */ 645178354Ssam IEEE80211_NODE_STAT(ni, rx_wepfail); 646178354Ssam goto out; 647178354Ssam } 648178354Ssam wh = mtod(m, struct ieee80211_frame *); 649178354Ssam wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 650178354Ssam } else { 651178354Ssam /* XXX M_WEP and IEEE80211_F_PRIVACY */ 652178354Ssam key = NULL; 653178354Ssam } 654178354Ssam 655178354Ssam /* 656178354Ssam * Save QoS bits for use below--before we strip the header. 657178354Ssam */ 658178354Ssam if (subtype == IEEE80211_FC0_SUBTYPE_QOS) { 659178354Ssam qos = (dir == IEEE80211_FC1_DIR_DSTODS) ? 660178354Ssam ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] : 661178354Ssam ((struct ieee80211_qosframe *)wh)->i_qos[0]; 662178354Ssam } else 663178354Ssam qos = 0; 664178354Ssam 665178354Ssam /* 666178354Ssam * Next up, any fragmentation. 667178354Ssam */ 668178354Ssam if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 669178354Ssam m = ieee80211_defrag(ni, m, hdrspace); 670178354Ssam if (m == NULL) { 671178354Ssam /* Fragment dropped or frame not complete yet */ 672178354Ssam goto out; 673178354Ssam } 674178354Ssam } 675178354Ssam wh = NULL; /* no longer valid, catch any uses */ 676178354Ssam 677178354Ssam /* 678178354Ssam * Next strip any MSDU crypto bits. 679178354Ssam */ 680178354Ssam if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) { 681178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 682178354Ssam ni->ni_macaddr, "data", "%s", "demic error"); 683178354Ssam vap->iv_stats.is_rx_demicfail++; 684178354Ssam IEEE80211_NODE_STAT(ni, rx_demicfail); 685178354Ssam goto out; 686178354Ssam } 687178354Ssam 688178354Ssam /* copy to listener after decrypt */ 689178354Ssam if (bpf_peers_present(vap->iv_rawbpf)) 690178354Ssam bpf_mtap(vap->iv_rawbpf, m); 691178354Ssam need_tap = 0; 692178354Ssam 693178354Ssam /* 694178354Ssam * Finally, strip the 802.11 header. 695178354Ssam */ 696178354Ssam m = ieee80211_decap(vap, m, hdrspace); 697178354Ssam if (m == NULL) { 698178354Ssam /* XXX mask bit to check for both */ 699178354Ssam /* don't count Null data frames as errors */ 700178354Ssam if (subtype == IEEE80211_FC0_SUBTYPE_NODATA || 701178354Ssam subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL) 702178354Ssam goto out; 703178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 704178354Ssam ni->ni_macaddr, "data", "%s", "decap error"); 705178354Ssam vap->iv_stats.is_rx_decap++; 706178354Ssam IEEE80211_NODE_STAT(ni, rx_decap); 707178354Ssam goto err; 708178354Ssam } 709178354Ssam eh = mtod(m, struct ether_header *); 710178354Ssam if (!ieee80211_node_is_authorized(ni)) { 711178354Ssam /* 712178354Ssam * Deny any non-PAE frames received prior to 713178354Ssam * authorization. For open/shared-key 714178354Ssam * authentication the port is mark authorized 715178354Ssam * after authentication completes. For 802.1x 716178354Ssam * the port is not marked authorized by the 717178354Ssam * authenticator until the handshake has completed. 718178354Ssam */ 719178354Ssam if (eh->ether_type != htons(ETHERTYPE_PAE)) { 720178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 721178354Ssam eh->ether_shost, "data", 722178354Ssam "unauthorized port: ether type 0x%x len %u", 723178354Ssam eh->ether_type, m->m_pkthdr.len); 724178354Ssam vap->iv_stats.is_rx_unauth++; 725178354Ssam IEEE80211_NODE_STAT(ni, rx_unauth); 726178354Ssam goto err; 727178354Ssam } 728178354Ssam } else { 729178354Ssam /* 730178354Ssam * When denying unencrypted frames, discard 731178354Ssam * any non-PAE frames received without encryption. 732178354Ssam */ 733178354Ssam if ((vap->iv_flags & IEEE80211_F_DROPUNENC) && 734178354Ssam (key == NULL && (m->m_flags & M_WEP) == 0) && 735178354Ssam eh->ether_type != htons(ETHERTYPE_PAE)) { 736178354Ssam /* 737178354Ssam * Drop unencrypted frames. 738178354Ssam */ 739178354Ssam vap->iv_stats.is_rx_unencrypted++; 740178354Ssam IEEE80211_NODE_STAT(ni, rx_unencrypted); 741178354Ssam goto out; 742178354Ssam } 743178354Ssam } 744178354Ssam /* XXX require HT? */ 745178354Ssam if (qos & IEEE80211_QOS_AMSDU) { 746178354Ssam m = ieee80211_decap_amsdu(ni, m); 747178354Ssam if (m == NULL) 748178354Ssam return IEEE80211_FC0_TYPE_DATA; 749190391Ssam } else { 750190391Ssam#ifdef IEEE80211_SUPPORT_SUPERG 751190391Ssam m = ieee80211_decap_fastframe(vap, ni, m); 752190391Ssam if (m == NULL) 753178354Ssam return IEEE80211_FC0_TYPE_DATA; 754190391Ssam#endif 755178354Ssam } 756178354Ssam ieee80211_deliver_data(vap, ni, m); 757178354Ssam return IEEE80211_FC0_TYPE_DATA; 758178354Ssam 759178354Ssam case IEEE80211_FC0_TYPE_MGT: 760178354Ssam vap->iv_stats.is_rx_mgmt++; 761178354Ssam IEEE80211_NODE_STAT(ni, rx_mgmt); 762178354Ssam if (dir != IEEE80211_FC1_DIR_NODS) { 763178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 764178354Ssam wh, "data", "incorrect dir 0x%x", dir); 765178354Ssam vap->iv_stats.is_rx_wrongdir++; 766178354Ssam goto err; 767178354Ssam } 768178354Ssam if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) { 769178354Ssam IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 770178354Ssam ni->ni_macaddr, "mgt", "too short: len %u", 771178354Ssam m->m_pkthdr.len); 772178354Ssam vap->iv_stats.is_rx_tooshort++; 773178354Ssam goto out; 774178354Ssam } 775178354Ssam#ifdef IEEE80211_DEBUG 776178354Ssam if (ieee80211_msg_debug(vap) || ieee80211_msg_dumppkts(vap)) { 777178354Ssam if_printf(ifp, "received %s from %s rssi %d\n", 778178354Ssam ieee80211_mgt_subtype_name[subtype >> 779178354Ssam IEEE80211_FC0_SUBTYPE_SHIFT], 780178354Ssam ether_sprintf(wh->i_addr2), rssi); 781178354Ssam } 782178354Ssam#endif 783178354Ssam if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 784178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 785178354Ssam wh, NULL, "%s", "WEP set but not permitted"); 786178354Ssam vap->iv_stats.is_rx_mgtdiscard++; /* XXX */ 787178354Ssam goto out; 788178354Ssam } 789178354Ssam if (bpf_peers_present(vap->iv_rawbpf)) 790178354Ssam bpf_mtap(vap->iv_rawbpf, m); 791178354Ssam vap->iv_recv_mgmt(ni, m, subtype, rssi, noise, rstamp); 792178354Ssam m_freem(m); 793178354Ssam return IEEE80211_FC0_TYPE_MGT; 794178354Ssam 795178354Ssam case IEEE80211_FC0_TYPE_CTL: 796178354Ssam vap->iv_stats.is_rx_ctl++; 797178354Ssam IEEE80211_NODE_STAT(ni, rx_ctrl); 798178354Ssam goto out; 799178354Ssam default: 800178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 801178354Ssam wh, "bad", "frame type 0x%x", type); 802178354Ssam /* should not come here */ 803178354Ssam break; 804178354Ssam } 805178354Ssamerr: 806178354Ssam ifp->if_ierrors++; 807178354Ssamout: 808178354Ssam if (m != NULL) { 809178354Ssam if (bpf_peers_present(vap->iv_rawbpf) && need_tap) 810178354Ssam bpf_mtap(vap->iv_rawbpf, m); 811178354Ssam m_freem(m); 812178354Ssam } 813178354Ssam return type; 814178354Ssam#undef SEQ_LEQ 815178354Ssam} 816178354Ssam 817178354Ssamstatic void 818178354Ssamwds_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, 819178354Ssam int subtype, int rssi, int noise, u_int32_t rstamp) 820178354Ssam{ 821178354Ssam struct ieee80211vap *vap = ni->ni_vap; 822178354Ssam struct ieee80211com *ic = ni->ni_ic; 823178354Ssam struct ieee80211_frame *wh; 824178354Ssam u_int8_t *frm, *efrm; 825178354Ssam 826178354Ssam wh = mtod(m0, struct ieee80211_frame *); 827178354Ssam frm = (u_int8_t *)&wh[1]; 828178354Ssam efrm = mtod(m0, u_int8_t *) + m0->m_len; 829178354Ssam switch (subtype) { 830178354Ssam case IEEE80211_FC0_SUBTYPE_DEAUTH: 831178354Ssam case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 832178354Ssam case IEEE80211_FC0_SUBTYPE_BEACON: 833178354Ssam case IEEE80211_FC0_SUBTYPE_PROBE_REQ: 834178354Ssam case IEEE80211_FC0_SUBTYPE_AUTH: 835178354Ssam case IEEE80211_FC0_SUBTYPE_ASSOC_REQ: 836178354Ssam case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: 837178354Ssam case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 838178354Ssam case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: 839178354Ssam case IEEE80211_FC0_SUBTYPE_DISASSOC: 840178354Ssam vap->iv_stats.is_rx_mgtdiscard++; 841178354Ssam break; 842178354Ssam case IEEE80211_FC0_SUBTYPE_ACTION: 843178354Ssam if (vap->iv_state != IEEE80211_S_RUN || 844178354Ssam IEEE80211_IS_MULTICAST(wh->i_addr1)) { 845178354Ssam vap->iv_stats.is_rx_mgtdiscard++; 846178354Ssam break; 847178354Ssam } 848178354Ssam ni->ni_inact = ni->ni_inact_reload; 849178354Ssam if (ieee80211_parse_action(ni, m0) == 0) 850178354Ssam ic->ic_recv_action(ni, frm, efrm); 851178354Ssam break; 852178354Ssam default: 853178354Ssam IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 854178354Ssam wh, "mgt", "subtype 0x%x not handled", subtype); 855178354Ssam vap->iv_stats.is_rx_badsubtype++; 856178354Ssam break; 857178354Ssam } 858178354Ssam} 859