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