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