if: Per-cpu ifnet/ifaddr statistics, step 1/3
[dragonfly.git] / sys / netproto / 802_11 / wlan / 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  * $FreeBSD: head/sys/net80211/ieee80211_wds.c 203422 2010-02-03 10:07:43Z rpaulo $
26  */
27
28 /*
29  * IEEE 802.11 WDS mode support.
30  */
31 #include "opt_inet.h"
32 #include "opt_wlan.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h> 
36 #include <sys/mbuf.h>   
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #include <sys/endian.h>
43 #include <sys/errno.h>
44 #include <sys/proc.h>
45 #include <sys/sysctl.h>
46
47 #include <net/if.h>
48 #include <net/if_media.h>
49 #include <net/if_llc.h>
50 #include <net/ethernet.h>
51 #include <net/route.h>
52
53 #include <net/bpf.h>
54
55 #include <netproto/802_11/ieee80211_var.h>
56 #include <netproto/802_11/ieee80211_wds.h>
57 #include <netproto/802_11/ieee80211_input.h>
58 #ifdef IEEE80211_SUPPORT_SUPERG
59 #include <netproto/802_11/ieee80211_superg.h>
60 #endif
61
62 static void wds_vattach(struct ieee80211vap *);
63 static int wds_newstate(struct ieee80211vap *, enum ieee80211_state, int);
64 static  int wds_input(struct ieee80211_node *ni, struct mbuf *m, int, int);
65 static void wds_recv_mgmt(struct ieee80211_node *, struct mbuf *,
66             int subtype, int, int);
67
68 void
69 ieee80211_wds_attach(struct ieee80211com *ic)
70 {
71         ic->ic_vattach[IEEE80211_M_WDS] = wds_vattach;
72 }
73
74 void
75 ieee80211_wds_detach(struct ieee80211com *ic)
76 {
77 }
78
79 static void
80 wds_vdetach(struct ieee80211vap *vap)
81 {
82         if (vap->iv_bss != NULL) {
83                 /* XXX locking? */
84                 if (vap->iv_bss->ni_wdsvap == vap)
85                         vap->iv_bss->ni_wdsvap = NULL;
86         }
87 }
88
89 static void
90 wds_vattach(struct ieee80211vap *vap)
91 {
92         vap->iv_newstate = wds_newstate;
93         vap->iv_input = wds_input;
94         vap->iv_recv_mgmt = wds_recv_mgmt;
95         vap->iv_opdetach = wds_vdetach;
96 }
97
98 static void
99 wds_flush(struct ieee80211_node *ni)
100 {
101         struct ieee80211com *ic = ni->ni_ic;
102         struct mbuf *m, *next;
103         int8_t rssi, nf;
104
105         m = ieee80211_ageq_remove(&ic->ic_stageq,
106             (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr));
107         if (m == NULL)
108                 return;
109
110         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_WDS, ni,
111             "%s", "flush wds queue");
112         ic->ic_node_getsignal(ni, &rssi, &nf);
113         for (; m != NULL; m = next) {
114                 next = m->m_nextpkt;
115                 m->m_nextpkt = NULL;
116                 ieee80211_input(ni, m, rssi, nf);
117         }
118 }
119
120 static int
121 ieee80211_create_wds(struct ieee80211vap *vap, struct ieee80211_channel *chan)
122 {
123         struct ieee80211com *ic = vap->iv_ic;
124 /*      struct ieee80211_node_table *nt = &ic->ic_sta;*/
125         struct ieee80211_node *ni, *obss;
126 #ifdef IEEE80211_DEBUG
127         char ethstr[ETHER_ADDRSTRLEN + 1];
128 #endif
129
130         IEEE80211_DPRINTF(vap, IEEE80211_MSG_WDS,
131              "%s: creating link to %s on channel %u\n", __func__,
132              kether_ntoa(vap->iv_des_bssid, ethstr), ieee80211_chan2ieee(ic, chan));
133
134         /* NB: vap create must specify the bssid for the link */
135         KASSERT(vap->iv_flags & IEEE80211_F_DESBSSID, ("no bssid"));
136         /* NB: we should only be called on RUN transition */
137         KASSERT(vap->iv_state == IEEE80211_S_RUN, ("!RUN state"));
138
139         if ((vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0) {
140                 /*
141                  * Dynamic/non-legacy WDS.  Reference the associated
142                  * station specified by the desired bssid setup at vap
143                  * create.  Point ni_wdsvap at the WDS vap so 4-address
144                  * frames received through the associated AP vap will
145                  * be dispatched upward (e.g. to a bridge) as though
146                  * they arrived on the WDS vap.
147                  */
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__, kether_ntoa(vap->iv_des_bssid, ethstr));
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__, kether_ntoa(vap->iv_des_bssid, ethstr),
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                 if (obss != NULL) {
183                         /* NB: deferred to avoid recursive lock */
184                         ieee80211_free_node(obss);
185                 }
186         } else {
187                 /*
188                  * Legacy WDS vap setup.
189                  */
190                 /*
191                  * The far end does not associate so we just create
192                  * create a new node and install it as the vap's
193                  * bss node.  We must simulate an association and
194                  * authorize the port for traffic to flow.
195                  * XXX check if node already in sta table?
196                  */
197                 ni = ieee80211_node_create_wds(vap, vap->iv_des_bssid, chan);
198                 if (ni != NULL) {
199                         obss = vap->iv_bss;
200                         vap->iv_bss = ieee80211_ref_node(ni);
201                         ni->ni_flags |= IEEE80211_NODE_AREF;
202                         if (obss != NULL)
203                                 ieee80211_free_node(obss);
204                         /* give driver a chance to setup state like ni_txrate */
205                         if (ic->ic_newassoc != NULL)
206                                 ic->ic_newassoc(ni, 1);
207                         /* tell the authenticator about new station */
208                         if (vap->iv_auth->ia_node_join != NULL)
209                                 vap->iv_auth->ia_node_join(ni);
210                         if (ni->ni_authmode != IEEE80211_AUTH_8021X)
211                                 ieee80211_node_authorize(ni);
212
213                         ieee80211_notify_node_join(ni, 1 /*newassoc*/);
214                         /* XXX inject l2uf frame */
215                 }
216         }
217
218         /*
219          * Flush any pending frames now that were setup.
220          */
221         if (ni != NULL)
222                 wds_flush(ni);
223         return (ni == NULL ? ENOENT : 0);
224 }
225
226 /*
227  * Propagate multicast frames of an ap vap to all DWDS links.
228  * The caller is assumed to have verified this frame is multicast.
229  */
230 void
231 ieee80211_dwds_mcast(struct ieee80211vap *vap0, struct mbuf *m)
232 {
233         struct ieee80211com *ic = vap0->iv_ic;
234         struct ifnet *parent = ic->ic_ifp;
235         const struct ether_header *eh = mtod(m, const struct ether_header *);
236         struct ieee80211_node *ni;
237         struct ieee80211vap *vap;
238         struct ifnet *ifp;
239         struct mbuf *mcopy;
240         int err;
241         char ethstr[ETHER_ADDRSTRLEN + 1];
242
243         KASSERT(ETHER_IS_MULTICAST(eh->ether_dhost),
244             ("%s not mcast", kether_ntoa(eh->ether_dhost, ethstr)));
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, MB_DONTWAIT);
260                 if (mcopy == NULL) {
261                         IFNET_STAT_INC(ifp, oerrors, 1);
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                         IFNET_STAT_INC(ifp, oerrors, 1);
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                         IFNET_STAT_INC(ifp, oerrors, 1);
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 = ieee80211_handoff(parent, mcopy);
300                 if (err) {
301                         /* NB: IFQ_HANDOFF reclaims mbuf */
302                         IFNET_STAT_INC(ifp, oerrors, 1);
303                         ieee80211_free_node(ni);
304                 } else
305                         IFNET_STAT_INC(ifp, opackets, 1);
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         enum ieee80211_state ostate;
344         int error;
345
346         ostate = vap->iv_state;
347         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s\n", __func__,
348                 ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
349         vap->iv_state = nstate;                 /* state transition */
350         callout_stop(&vap->iv_mgtsend);         /* XXX callout_drain */
351         if (ostate != IEEE80211_S_SCAN)
352                 ieee80211_cancel_scan(vap);     /* background scan */
353         error = 0;
354         switch (nstate) {
355         case IEEE80211_S_INIT:
356                 switch (ostate) {
357                 case IEEE80211_S_SCAN:
358                         ieee80211_cancel_scan(vap);
359                         break;
360                 default:
361                         break;
362                 }
363                 if (ostate != IEEE80211_S_INIT) {
364                         /* NB: optimize INIT -> INIT case */
365                         ieee80211_reset_bss(vap);
366                 }
367                 break;
368         case IEEE80211_S_SCAN:
369                 switch (ostate) {
370                 case IEEE80211_S_INIT:
371                         ieee80211_check_scan_current(vap);
372                         break;
373                 default:
374                         break;
375                 }
376                 break;
377         case IEEE80211_S_RUN:
378                 if (ostate == IEEE80211_S_INIT) {
379                         /*
380                          * Already have a channel; bypass the scan
381                          * and startup immediately.
382                          */
383                         error = ieee80211_create_wds(vap, ic->ic_curchan);
384                 }
385                 break;
386         default:
387                 break;
388         }
389         return error;
390 }
391
392 /*
393  * Process a received frame.  The node associated with the sender
394  * should be supplied.  If nothing was found in the node table then
395  * the caller is assumed to supply a reference to iv_bss instead.
396  * The RSSI and a timestamp are also supplied.  The RSSI data is used
397  * during AP scanning to select a AP to associate with; it can have
398  * any units so long as values have consistent units and higher values
399  * mean ``better signal''.  The receive timestamp is currently not used
400  * by the 802.11 layer.
401  */
402 static int
403 wds_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
404 {
405 #define SEQ_LEQ(a,b)    ((int)((a)-(b)) <= 0)
406 #define HAS_SEQ(type)   ((type & 0x4) == 0)
407         struct ieee80211vap *vap = ni->ni_vap;
408         struct ieee80211com *ic = ni->ni_ic;
409         struct ifnet *ifp = vap->iv_ifp;
410         struct ieee80211_frame *wh;
411         struct ieee80211_key *key;
412         struct ether_header *eh;
413         int hdrspace, need_tap = 1;     /* mbuf need to be tapped. */
414         uint8_t dir, type, subtype, qos;
415         uint16_t rxseq;
416 #ifdef IEEE80211_DEBUG
417         char ethstr[ETHER_ADDRSTRLEN + 1];
418 #endif
419         if (m->m_flags & M_AMPDU_MPDU) {
420                 /*
421                  * Fastpath for A-MPDU reorder q resubmission.  Frames
422                  * w/ M_AMPDU_MPDU marked have already passed through
423                  * here but were received out of order and been held on
424                  * the reorder queue.  When resubmitted they are marked
425                  * with the M_AMPDU_MPDU flag and we can bypass most of
426                  * the normal processing.
427                  */
428                 wh = mtod(m, struct ieee80211_frame *);
429                 type = IEEE80211_FC0_TYPE_DATA;
430                 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
431                 subtype = IEEE80211_FC0_SUBTYPE_QOS;
432                 hdrspace = ieee80211_hdrspace(ic, wh);  /* XXX optimize? */
433                 goto resubmit_ampdu;
434         }
435
436         KASSERT(ni != NULL, ("null node"));
437
438         type = -1;                      /* undefined */
439
440         if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
441                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
442                     ni->ni_macaddr, NULL,
443                     "too short (1): len %u", m->m_pkthdr.len);
444                 vap->iv_stats.is_rx_tooshort++;
445                 goto out;
446         }
447         /*
448          * Bit of a cheat here, we use a pointer for a 3-address
449          * frame format but don't reference fields past outside
450          * ieee80211_frame_min w/o first validating the data is
451          * present.
452          */
453         wh = mtod(m, struct ieee80211_frame *);
454
455         if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
456             IEEE80211_FC0_VERSION_0) {
457                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
458                     ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
459                     wh->i_fc[0], wh->i_fc[1]);
460                 vap->iv_stats.is_rx_badversion++;
461                 goto err;
462         }
463
464         dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
465         type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
466         subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
467
468         /* NB: WDS vap's do not scan */
469         if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_addr4)) {
470                 IEEE80211_DISCARD_MAC(vap,
471                     IEEE80211_MSG_ANY, ni->ni_macaddr, NULL,
472                     "too short (3): len %u", m->m_pkthdr.len);
473                 vap->iv_stats.is_rx_tooshort++;
474                 goto out;
475         }
476         /* NB: the TA is implicitly verified by finding the wds peer node */
477         if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr) &&
478             !IEEE80211_ADDR_EQ(wh->i_addr1, ifp->if_broadcastaddr)) {
479                 /* not interested in */
480                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
481                     wh->i_addr1, NULL, "%s", "not to bss");
482                 vap->iv_stats.is_rx_wrongbss++;
483                 goto out;
484         }
485         IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
486         ni->ni_noise = nf;
487         if (HAS_SEQ(type)) {
488                 uint8_t tid = ieee80211_gettid(wh);
489                 if (IEEE80211_QOS_HAS_SEQ(wh) &&
490                     TID_TO_WME_AC(tid) >= WME_AC_VI)
491                         ic->ic_wme.wme_hipri_traffic++;
492                 rxseq = le16toh(*(uint16_t *)wh->i_seq);
493                 if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
494                     (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
495                     SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
496                         /* duplicate, discard */
497                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
498                             wh->i_addr1, "duplicate",
499                             "seqno <%u,%u> fragno <%u,%u> tid %u",
500                             rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
501                             ni->ni_rxseqs[tid] >> IEEE80211_SEQ_SEQ_SHIFT,
502                             rxseq & IEEE80211_SEQ_FRAG_MASK,
503                             ni->ni_rxseqs[tid] & IEEE80211_SEQ_FRAG_MASK,
504                             tid);
505                         vap->iv_stats.is_rx_dup++;
506                         IEEE80211_NODE_STAT(ni, rx_dup);
507                         goto out;
508                 }
509                 ni->ni_rxseqs[tid] = rxseq;
510         }
511         switch (type) {
512         case IEEE80211_FC0_TYPE_DATA:
513                 hdrspace = ieee80211_hdrspace(ic, wh);
514                 if (m->m_len < hdrspace &&
515                     (m = m_pullup(m, hdrspace)) == NULL) {
516                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
517                             ni->ni_macaddr, NULL,
518                             "data too short: expecting %u", hdrspace);
519                         vap->iv_stats.is_rx_tooshort++;
520                         goto out;               /* XXX */
521                 }
522                 if (dir != IEEE80211_FC1_DIR_DSTODS) {
523                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
524                             wh, "data", "incorrect dir 0x%x", dir);
525                         vap->iv_stats.is_rx_wrongdir++;
526                         goto out;
527                 }
528                 /*
529                  * Only legacy WDS traffic should take this path.
530                  */
531                 if ((vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0) {
532                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
533                             wh, "data", "%s", "not legacy wds");
534                         vap->iv_stats.is_rx_wrongdir++;/*XXX*/
535                         goto out;
536                 }
537                 if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
538                         ni->ni_inact = ni->ni_inact_reload;
539                 /*
540                  * Handle A-MPDU re-ordering.  If the frame is to be
541                  * processed directly then ieee80211_ampdu_reorder
542                  * will return 0; otherwise it has consumed the mbuf
543                  * and we should do nothing more with it.
544                  */
545                 if ((m->m_flags & M_AMPDU) &&
546                     ieee80211_ampdu_reorder(ni, m) != 0) {
547                         m = NULL;
548                         goto out;
549                 }
550         resubmit_ampdu:
551
552                 /*
553                  * Handle privacy requirements.  Note that we
554                  * must not be preempted from here until after
555                  * we (potentially) call ieee80211_crypto_demic;
556                  * otherwise we may violate assumptions in the
557                  * crypto cipher modules used to do delayed update
558                  * of replay sequence numbers.
559                  */
560                 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
561                         if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
562                                 /*
563                                  * Discard encrypted frames when privacy is off.
564                                  */
565                                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
566                                     wh, "WEP", "%s", "PRIVACY off");
567                                 vap->iv_stats.is_rx_noprivacy++;
568                                 IEEE80211_NODE_STAT(ni, rx_noprivacy);
569                                 goto out;
570                         }
571                         key = ieee80211_crypto_decap(ni, m, hdrspace);
572                         if (key == NULL) {
573                                 /* NB: stats+msgs handled in crypto_decap */
574                                 IEEE80211_NODE_STAT(ni, rx_wepfail);
575                                 goto out;
576                         }
577                         wh = mtod(m, struct ieee80211_frame *);
578                         wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
579                 } else {
580                         /* XXX M_WEP and IEEE80211_F_PRIVACY */
581                         key = NULL;
582                 }
583
584                 /*
585                  * Save QoS bits for use below--before we strip the header.
586                  */
587                 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
588                         qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
589                             ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
590                             ((struct ieee80211_qosframe *)wh)->i_qos[0];
591                 } else
592                         qos = 0;
593
594                 /*
595                  * Next up, any fragmentation.
596                  */
597                 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
598                         m = ieee80211_defrag(ni, m, hdrspace);
599                         if (m == NULL) {
600                                 /* Fragment dropped or frame not complete yet */
601                                 goto out;
602                         }
603                 }
604                 wh = NULL;              /* no longer valid, catch any uses */
605
606                 /*
607                  * Next strip any MSDU crypto bits.
608                  */
609                 if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
610                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
611                             ni->ni_macaddr, "data", "%s", "demic error");
612                         vap->iv_stats.is_rx_demicfail++;
613                         IEEE80211_NODE_STAT(ni, rx_demicfail);
614                         goto out;
615                 }
616
617                 /* copy to listener after decrypt */
618                 if (ieee80211_radiotap_active_vap(vap))
619                         ieee80211_radiotap_rx(vap, m);
620                 need_tap = 0;
621
622                 /*
623                  * Finally, strip the 802.11 header.
624                  */
625                 m = ieee80211_decap(vap, m, hdrspace);
626                 if (m == NULL) {
627                         /* XXX mask bit to check for both */
628                         /* don't count Null data frames as errors */
629                         if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
630                             subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
631                                 goto out;
632                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
633                             ni->ni_macaddr, "data", "%s", "decap error");
634                         vap->iv_stats.is_rx_decap++;
635                         IEEE80211_NODE_STAT(ni, rx_decap);
636                         goto err;
637                 }
638                 eh = mtod(m, struct ether_header *);
639                 if (!ieee80211_node_is_authorized(ni)) {
640                         /*
641                          * Deny any non-PAE frames received prior to
642                          * authorization.  For open/shared-key
643                          * authentication the port is mark authorized
644                          * after authentication completes.  For 802.1x
645                          * the port is not marked authorized by the
646                          * authenticator until the handshake has completed.
647                          */
648                         if (eh->ether_type != htons(ETHERTYPE_PAE)) {
649                                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
650                                     eh->ether_shost, "data",
651                                     "unauthorized port: ether type 0x%x len %u",
652                                     eh->ether_type, m->m_pkthdr.len);
653                                 vap->iv_stats.is_rx_unauth++;
654                                 IEEE80211_NODE_STAT(ni, rx_unauth);
655                                 goto err;
656                         }
657                 } else {
658                         /*
659                          * When denying unencrypted frames, discard
660                          * any non-PAE frames received without encryption.
661                          */
662                         if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
663                             (key == NULL && (m->m_flags & M_WEP) == 0) &&
664                             eh->ether_type != htons(ETHERTYPE_PAE)) {
665                                 /*
666                                  * Drop unencrypted frames.
667                                  */
668                                 vap->iv_stats.is_rx_unencrypted++;
669                                 IEEE80211_NODE_STAT(ni, rx_unencrypted);
670                                 goto out;
671                         }
672                 }
673                 /* XXX require HT? */
674                 if (qos & IEEE80211_QOS_AMSDU) {
675                         m = ieee80211_decap_amsdu(ni, m);
676                         if (m == NULL)
677                                 return IEEE80211_FC0_TYPE_DATA;
678                 } else {
679 #ifdef IEEE80211_SUPPORT_SUPERG
680                         m = ieee80211_decap_fastframe(vap, ni, m);
681                         if (m == NULL)
682                                 return IEEE80211_FC0_TYPE_DATA;
683 #endif
684                 }
685                 ieee80211_deliver_data(vap, ni, m);
686                 return IEEE80211_FC0_TYPE_DATA;
687
688         case IEEE80211_FC0_TYPE_MGT:
689                 vap->iv_stats.is_rx_mgmt++;
690                 IEEE80211_NODE_STAT(ni, rx_mgmt);
691                 if (dir != IEEE80211_FC1_DIR_NODS) {
692                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
693                             wh, "data", "incorrect dir 0x%x", dir);
694                         vap->iv_stats.is_rx_wrongdir++;
695                         goto err;
696                 }
697                 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
698                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
699                             ni->ni_macaddr, "mgt", "too short: len %u",
700                             m->m_pkthdr.len);
701                         vap->iv_stats.is_rx_tooshort++;
702                         goto out;
703                 }
704 #ifdef IEEE80211_DEBUG
705                 if (ieee80211_msg_debug(vap) || ieee80211_msg_dumppkts(vap)) {
706                         if_printf(ifp, "received %s from %s rssi %d\n",
707                             ieee80211_mgt_subtype_name[subtype >>
708                                 IEEE80211_FC0_SUBTYPE_SHIFT],
709                             kether_ntoa(wh->i_addr2, ethstr), rssi);
710                 }
711 #endif
712                 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
713                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
714                             wh, NULL, "%s", "WEP set but not permitted");
715                         vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
716                         goto out;
717                 }
718                 vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
719                 goto out;
720
721         case IEEE80211_FC0_TYPE_CTL:
722                 vap->iv_stats.is_rx_ctl++;
723                 IEEE80211_NODE_STAT(ni, rx_ctrl);
724                 goto out;
725
726         default:
727                 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
728                     wh, "bad", "frame type 0x%x", type);
729                 /* should not come here */
730                 break;
731         }
732 err:
733         IFNET_STAT_INC(ifp, ierrors, 1);
734 out:
735         if (m != NULL) {
736                 if (need_tap && ieee80211_radiotap_active_vap(vap))
737                         ieee80211_radiotap_rx(vap, m);
738                 m_freem(m);
739         }
740         return type;
741 #undef SEQ_LEQ
742 }
743
744 static void
745 wds_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
746         int subtype, int rssi, int nf)
747 {
748         struct ieee80211vap *vap = ni->ni_vap;
749         struct ieee80211com *ic = ni->ni_ic;
750         struct ieee80211_frame *wh;
751         u_int8_t *frm, *efrm;
752
753         wh = mtod(m0, struct ieee80211_frame *);
754         frm = (u_int8_t *)&wh[1];
755         efrm = mtod(m0, u_int8_t *) + m0->m_len;
756         switch (subtype) {
757         case IEEE80211_FC0_SUBTYPE_DEAUTH:
758         case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
759         case IEEE80211_FC0_SUBTYPE_BEACON:
760         case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
761         case IEEE80211_FC0_SUBTYPE_AUTH:
762         case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
763         case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
764         case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
765         case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
766         case IEEE80211_FC0_SUBTYPE_DISASSOC:
767                 vap->iv_stats.is_rx_mgtdiscard++;
768                 break;
769         case IEEE80211_FC0_SUBTYPE_ACTION:
770                 if (vap->iv_state != IEEE80211_S_RUN ||
771                     IEEE80211_IS_MULTICAST(wh->i_addr1)) {
772                         vap->iv_stats.is_rx_mgtdiscard++;
773                         break;
774                 }
775                 ni->ni_inact = ni->ni_inact_reload;
776                 if (ieee80211_parse_action(ni, m0) == 0)
777                         ic->ic_recv_action(ni, wh, frm, efrm);
778                 break;
779         default:
780                 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
781                      wh, "mgt", "subtype 0x%x not handled", subtype);
782                 vap->iv_stats.is_rx_badsubtype++;
783                 break;
784         }
785 }