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