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