]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net80211/ieee80211_wds.c
fixup ieee80211_output handling:
[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_media.h>
52 #include <net/if_llc.h>
53 #include <net/ethernet.h>
54
55 #include <net/bpf.h>
56
57 #include <net80211/ieee80211_var.h>
58 #include <net80211/ieee80211_wds.h>
59 #include <net80211/ieee80211_input.h>
60 #ifdef IEEE80211_SUPPORT_SUPERG
61 #include <net80211/ieee80211_superg.h>
62 #endif
63
64 static void wds_vattach(struct ieee80211vap *);
65 static int wds_newstate(struct ieee80211vap *, enum ieee80211_state, int);
66 static  int wds_input(struct ieee80211_node *ni, struct mbuf *m,
67             int rssi, int noise, uint32_t rstamp);
68 static void wds_recv_mgmt(struct ieee80211_node *, struct mbuf *,
69             int subtype, int rssi, int noise, u_int32_t rstamp);
70
71 void
72 ieee80211_wds_attach(struct ieee80211com *ic)
73 {
74         ic->ic_vattach[IEEE80211_M_WDS] = wds_vattach;
75 }
76
77 void
78 ieee80211_wds_detach(struct ieee80211com *ic)
79 {
80 }
81
82 static void
83 wds_vdetach(struct ieee80211vap *vap)
84 {
85         if (vap->iv_bss != NULL) {
86                 /* XXX locking? */
87                 if (vap->iv_bss->ni_wdsvap == vap)
88                         vap->iv_bss->ni_wdsvap = NULL;
89         }
90 }
91
92 static void
93 wds_vattach(struct ieee80211vap *vap)
94 {
95         vap->iv_newstate = wds_newstate;
96         vap->iv_input = wds_input;
97         vap->iv_recv_mgmt = wds_recv_mgmt;
98         vap->iv_opdetach = wds_vdetach;
99 }
100
101 static int
102 ieee80211_create_wds(struct ieee80211vap *vap, struct ieee80211_channel *chan)
103 {
104         struct ieee80211com *ic = vap->iv_ic;
105         struct ieee80211_node_table *nt = &ic->ic_sta;
106         struct ieee80211_node *ni, *obss;
107
108         IEEE80211_DPRINTF(vap, IEEE80211_MSG_WDS,
109              "%s: creating link to %s on channel %u\n", __func__,
110              ether_sprintf(vap->iv_des_bssid), ieee80211_chan2ieee(ic, chan));
111
112         /* NB: vap create must specify the bssid for the link */
113         KASSERT(vap->iv_flags & IEEE80211_F_DESBSSID, ("no bssid"));
114         /* NB: we should only be called on RUN transition */
115         KASSERT(vap->iv_state == IEEE80211_S_RUN, ("!RUN state"));
116
117         if ((vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0) {
118                 /*
119                  * Dynamic/non-legacy WDS.  Reference the associated
120                  * station specified by the desired bssid setup at vap
121                  * create.  Point ni_wdsvap at the WDS vap so 4-address
122                  * frames received through the associated AP vap will
123                  * be dispatched upward (e.g. to a bridge) as though
124                  * they arrived on the WDS vap.
125                  */
126                 IEEE80211_NODE_LOCK(nt);
127                 obss = NULL;
128                 ni = ieee80211_find_node_locked(&ic->ic_sta, vap->iv_des_bssid);
129                 if (ni == NULL) {
130                         /*
131                          * Node went away before we could hookup.  This
132                          * should be ok; no traffic will flow and a leave
133                          * event will be dispatched that should cause
134                          * the vap to be destroyed.
135                          */
136                         IEEE80211_DPRINTF(vap, IEEE80211_MSG_WDS,
137                             "%s: station %s went away\n",
138                             __func__, ether_sprintf(vap->iv_des_bssid));
139                         /* XXX stat? */
140                 } else if (ni->ni_wdsvap != NULL) {
141                         /*
142                          * Node already setup with a WDS vap; we cannot
143                          * allow multiple references so disallow.  If
144                          * ni_wdsvap points at us that's ok; we should
145                          * do nothing anyway.
146                          */
147                         /* XXX printf instead? */
148                         IEEE80211_DPRINTF(vap, IEEE80211_MSG_WDS,
149                             "%s: station %s in use with %s\n",
150                             __func__, ether_sprintf(vap->iv_des_bssid),
151                             ni->ni_wdsvap->iv_ifp->if_xname);
152                         /* XXX stat? */
153                 } else {
154                         /*
155                          * Committed to new node, setup state.
156                          */
157                         obss = vap->iv_bss;
158                         vap->iv_bss = ni;
159                         ni->ni_wdsvap = vap;
160                 }
161                 IEEE80211_NODE_UNLOCK(nt);
162                 if (obss != NULL) {
163                         /* NB: deferred to avoid recursive lock */
164                         ieee80211_free_node(obss);
165                 }
166         } else {
167                 /*
168                  * Legacy WDS vap setup.
169                  */
170                 /*
171                  * The far end does not associate so we just create
172                  * create a new node and install it as the vap's
173                  * bss node.  We must simulate an association and
174                  * authorize the port for traffic to flow.
175                  * XXX check if node already in sta table?
176                  */
177                 ni = ieee80211_node_create_wds(vap, vap->iv_des_bssid, chan);
178                 if (ni != NULL) {
179                         obss = vap->iv_bss;
180                         vap->iv_bss = ieee80211_ref_node(ni);
181                         ni->ni_flags |= IEEE80211_NODE_AREF;
182                         if (obss != NULL)
183                                 ieee80211_free_node(obss);
184                         /* give driver a chance to setup state like ni_txrate */
185                         if (ic->ic_newassoc != NULL)
186                                 ic->ic_newassoc(ni, 1);
187                         /* tell the authenticator about new station */
188                         if (vap->iv_auth->ia_node_join != NULL)
189                                 vap->iv_auth->ia_node_join(ni);
190                         if (ni->ni_authmode != IEEE80211_AUTH_8021X)
191                                 ieee80211_node_authorize(ni);
192
193                         ieee80211_notify_node_join(ni, 1 /*newassoc*/);
194                         /* XXX inject l2uf frame */
195                 }
196         }
197
198         /*
199          * Flush pending frames now that were setup.
200          */
201         if (ni != NULL && IEEE80211_NODE_WDSQ_QLEN(ni) != 0) {
202                 int8_t rssi, noise;
203
204                 IEEE80211_NOTE(vap, IEEE80211_MSG_WDS, ni,
205                     "flush wds queue, %u packets queued",
206                     IEEE80211_NODE_WDSQ_QLEN(ni));
207                 ic->ic_node_getsignal(ni, &rssi, &noise);
208                 for (;;) {
209                         struct mbuf *m;
210
211                         IEEE80211_NODE_WDSQ_LOCK(ni);
212                         _IEEE80211_NODE_WDSQ_DEQUEUE_HEAD(ni, m);
213                         IEEE80211_NODE_WDSQ_UNLOCK(ni);
214                         if (m == NULL)
215                                 break;
216                         /* XXX cheat and re-use last rstamp */
217                         ieee80211_input(ni, m, rssi, noise, ni->ni_rstamp);
218                 }
219         }
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             ("%s not mcast", ether_sprintf(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, M_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                  * Encapsulate the packet in prep for transmission.
282                  */
283                 mcopy = ieee80211_encap(vap, ni, mcopy);
284                 if (m == NULL) {
285                         /* NB: stat+msg handled in ieee80211_encap */
286                         ieee80211_free_node(ni);
287                         continue;
288                 }
289                 mcopy->m_flags |= M_MCAST;
290                 mcopy->m_pkthdr.rcvif = (void *) ni;
291
292                 err = parent->if_transmit(parent, mcopy);
293                 if (err) {
294                         /* NB: IFQ_HANDOFF reclaims mbuf */
295                         ifp->if_oerrors++;
296                         ieee80211_free_node(ni);
297                 } else
298                         ifp->if_opackets++;
299         }
300 }
301
302 /*
303  * Handle DWDS discovery on receipt of a 4-address frame in
304  * ap mode.  Queue the frame and post an event for someone
305  * to plumb the necessary WDS vap for this station.  Frames
306  * received prior to the vap set running will then be reprocessed
307  * as if they were just received.
308  */
309 void
310 ieee80211_dwds_discover(struct ieee80211_node *ni, struct mbuf *m)
311 {
312         struct ieee80211vap *vap = ni->ni_vap;
313         struct ieee80211com *ic = ni->ni_ic;
314         int qlen, age;
315
316         IEEE80211_NODE_WDSQ_LOCK(ni);
317         if (!_IF_QFULL(&ni->ni_wdsq)) {
318                 /*
319                  * Tag the frame with it's expiry time and insert
320                  * it in the queue.  The aging interval is 4 times
321                  * the listen interval specified by the station. 
322                  * Frames that sit around too long are reclaimed
323                  * using this information.
324                  */
325                 /* XXX handle overflow? */
326                 /* XXX per/vap beacon interval? */
327                 /* NB: TU -> secs */
328                 age = ((ni->ni_intval * ic->ic_lintval) << 2) / 1024;
329                 _IEEE80211_NODE_WDSQ_ENQUEUE(ni, m, qlen, age);
330                 IEEE80211_NODE_WDSQ_UNLOCK(ni);
331
332                 IEEE80211_NOTE(vap, IEEE80211_MSG_WDS, ni,
333                     "save frame, %u now queued", qlen);
334         } else {
335                 vap->iv_stats.is_dwds_qdrop++;
336                 _IF_DROP(&ni->ni_wdsq);
337                 IEEE80211_NODE_WDSQ_UNLOCK(ni);
338
339                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT | IEEE80211_MSG_WDS,
340                     mtod(m, struct ieee80211_frame *), "wds data",
341                     "pending q overflow, drops %d (len %d)",
342                     ni->ni_wdsq.ifq_drops, ni->ni_wdsq.ifq_len);
343
344 #ifdef IEEE80211_DEBUG
345                 if (ieee80211_msg_dumppkts(vap))
346                         ieee80211_dump_pkt(ic, mtod(m, caddr_t),
347                             m->m_len, -1, -1);
348 #endif
349                 /* XXX tail drop? */
350                 m_freem(m);
351         }
352         ieee80211_notify_wds_discover(ni);
353 }
354
355 /*
356  * Age frames on the WDS pending queue. The aging interval is
357  * 4 times the listen interval specified by the station.  This
358  * number is factored into the age calculations when the frame
359  * is placed on the queue.  We store ages as time differences
360  * so we can check and/or adjust only the head of the list.
361  * If a frame's age exceeds the threshold then discard it.
362  * The number of frames discarded is returned to the caller.
363  */
364 int
365 ieee80211_node_wdsq_age(struct ieee80211_node *ni)
366 {
367 #ifdef IEEE80211_DEBUG
368         struct ieee80211vap *vap = ni->ni_vap;
369 #endif
370         struct mbuf *m;
371         int discard = 0;
372
373         IEEE80211_NODE_WDSQ_LOCK(ni);
374         while (_IF_POLL(&ni->ni_wdsq, m) != NULL &&
375              M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
376                 IEEE80211_NOTE(vap, IEEE80211_MSG_WDS, ni,
377                         "discard frame, age %u", M_AGE_GET(m));
378
379                 /* XXX could be optimized */
380                 _IEEE80211_NODE_WDSQ_DEQUEUE_HEAD(ni, m);
381                 m_freem(m);
382                 discard++;
383         }
384         if (m != NULL)
385                 M_AGE_SUB(m, IEEE80211_INACT_WAIT);
386         IEEE80211_NODE_WDSQ_UNLOCK(ni);
387
388         IEEE80211_NOTE(vap, IEEE80211_MSG_WDS, ni,
389             "discard %u frames for age", discard);
390 #if 0
391         IEEE80211_NODE_STAT_ADD(ni, wds_discard, discard);
392 #endif
393         return discard;
394 }
395
396 /*
397  * IEEE80211_M_WDS vap state machine handler.
398  */
399 static int
400 wds_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
401 {
402         struct ieee80211com *ic = vap->iv_ic;
403         struct ieee80211_node *ni;
404         enum ieee80211_state ostate;
405         int error;
406
407         IEEE80211_LOCK_ASSERT(ic);
408
409         ostate = vap->iv_state;
410         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s\n", __func__,
411                 ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
412         vap->iv_state = nstate;                 /* state transition */
413         callout_stop(&vap->iv_mgtsend);         /* XXX callout_drain */
414         if (ostate != IEEE80211_S_SCAN)
415                 ieee80211_cancel_scan(vap);     /* background scan */
416         ni = vap->iv_bss;                       /* NB: no reference held */
417         error = 0;
418         switch (nstate) {
419         case IEEE80211_S_INIT:
420                 switch (ostate) {
421                 case IEEE80211_S_SCAN:
422                         ieee80211_cancel_scan(vap);
423                         break;
424                 default:
425                         break;
426                 }
427                 if (ostate != IEEE80211_S_INIT) {
428                         /* NB: optimize INIT -> INIT case */
429                         ieee80211_reset_bss(vap);
430                 }
431                 break;
432         case IEEE80211_S_SCAN:
433                 switch (ostate) {
434                 case IEEE80211_S_INIT:
435                         ieee80211_check_scan_current(vap);
436                         break;
437                 default:
438                         break;
439                 }
440                 break;
441         case IEEE80211_S_RUN:
442                 if (ostate == IEEE80211_S_INIT) {
443                         /*
444                          * Already have a channel; bypass the scan
445                          * and startup immediately.
446                          */
447                         error = ieee80211_create_wds(vap, ic->ic_curchan);
448                 }
449                 break;
450         default:
451                 break;
452         }
453         return error;
454 }
455
456 /*
457  * Process a received frame.  The node associated with the sender
458  * should be supplied.  If nothing was found in the node table then
459  * the caller is assumed to supply a reference to iv_bss instead.
460  * The RSSI and a timestamp are also supplied.  The RSSI data is used
461  * during AP scanning to select a AP to associate with; it can have
462  * any units so long as values have consistent units and higher values
463  * mean ``better signal''.  The receive timestamp is currently not used
464  * by the 802.11 layer.
465  */
466 static int
467 wds_input(struct ieee80211_node *ni, struct mbuf *m,
468         int rssi, int noise, uint32_t rstamp)
469 {
470 #define SEQ_LEQ(a,b)    ((int)((a)-(b)) <= 0)
471 #define HAS_SEQ(type)   ((type & 0x4) == 0)
472         struct ieee80211vap *vap = ni->ni_vap;
473         struct ieee80211com *ic = ni->ni_ic;
474         struct ifnet *ifp = vap->iv_ifp;
475         struct ieee80211_frame *wh;
476         struct ieee80211_key *key;
477         struct ether_header *eh;
478         int hdrspace, need_tap;
479         uint8_t dir, type, subtype, qos;
480         uint16_t rxseq;
481
482         if (m->m_flags & M_AMPDU_MPDU) {
483                 /*
484                  * Fastpath for A-MPDU reorder q resubmission.  Frames
485                  * w/ M_AMPDU_MPDU marked have already passed through
486                  * here but were received out of order and been held on
487                  * the reorder queue.  When resubmitted they are marked
488                  * with the M_AMPDU_MPDU flag and we can bypass most of
489                  * the normal processing.
490                  */
491                 wh = mtod(m, struct ieee80211_frame *);
492                 type = IEEE80211_FC0_TYPE_DATA;
493                 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
494                 subtype = IEEE80211_FC0_SUBTYPE_QOS;
495                 hdrspace = ieee80211_hdrspace(ic, wh);  /* XXX optimize? */
496                 goto resubmit_ampdu;
497         }
498
499         KASSERT(ni != NULL, ("null node"));
500
501         need_tap = 1;                   /* mbuf need to be tapped. */
502         type = -1;                      /* undefined */
503
504         if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
505                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
506                     ni->ni_macaddr, NULL,
507                     "too short (1): len %u", m->m_pkthdr.len);
508                 vap->iv_stats.is_rx_tooshort++;
509                 goto out;
510         }
511         /*
512          * Bit of a cheat here, we use a pointer for a 3-address
513          * frame format but don't reference fields past outside
514          * ieee80211_frame_min w/o first validating the data is
515          * present.
516          */
517         wh = mtod(m, struct ieee80211_frame *);
518
519         if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
520             IEEE80211_FC0_VERSION_0) {
521                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
522                     ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
523                 vap->iv_stats.is_rx_badversion++;
524                 goto err;
525         }
526
527         dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
528         type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
529         subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
530
531         /* NB: WDS vap's do not scan */
532         if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_addr4)) {
533                 IEEE80211_DISCARD_MAC(vap,
534                     IEEE80211_MSG_ANY, ni->ni_macaddr, NULL,
535                     "too short (3): len %u", m->m_pkthdr.len);
536                 vap->iv_stats.is_rx_tooshort++;
537                 goto out;
538         }
539         /* NB: the TA is implicitly verified by finding the wds peer node */
540         if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr) &&
541             !IEEE80211_ADDR_EQ(wh->i_addr1, ifp->if_broadcastaddr)) {
542                 /* not interested in */
543                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
544                     wh->i_addr1, NULL, "%s", "not to bss");
545                 vap->iv_stats.is_rx_wrongbss++;
546                 goto out;
547         }
548         IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
549         ni->ni_noise = noise;
550         ni->ni_rstamp = rstamp;
551         if (HAS_SEQ(type)) {
552                 uint8_t tid = ieee80211_gettid(wh);
553                 if (IEEE80211_QOS_HAS_SEQ(wh) &&
554                     TID_TO_WME_AC(tid) >= WME_AC_VI)
555                         ic->ic_wme.wme_hipri_traffic++;
556                 rxseq = le16toh(*(uint16_t *)wh->i_seq);
557                 if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
558                     (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
559                     SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
560                         /* duplicate, discard */
561                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
562                             wh->i_addr1, "duplicate",
563                             "seqno <%u,%u> fragno <%u,%u> tid %u",
564                             rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
565                             ni->ni_rxseqs[tid] >> IEEE80211_SEQ_SEQ_SHIFT,
566                             rxseq & IEEE80211_SEQ_FRAG_MASK,
567                             ni->ni_rxseqs[tid] & IEEE80211_SEQ_FRAG_MASK,
568                             tid);
569                         vap->iv_stats.is_rx_dup++;
570                         IEEE80211_NODE_STAT(ni, rx_dup);
571                         goto out;
572                 }
573                 ni->ni_rxseqs[tid] = rxseq;
574         }
575         switch (type) {
576         case IEEE80211_FC0_TYPE_DATA:
577                 hdrspace = ieee80211_hdrspace(ic, wh);
578                 if (m->m_len < hdrspace &&
579                     (m = m_pullup(m, hdrspace)) == NULL) {
580                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
581                             ni->ni_macaddr, NULL,
582                             "data too short: expecting %u", hdrspace);
583                         vap->iv_stats.is_rx_tooshort++;
584                         goto out;               /* XXX */
585                 }
586                 if (dir != IEEE80211_FC1_DIR_DSTODS) {
587                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
588                             wh, "data", "incorrect dir 0x%x", dir);
589                         vap->iv_stats.is_rx_wrongdir++;
590                         goto out;
591                 }
592                 /*
593                  * Only legacy WDS traffic should take this path.
594                  */
595                 if ((vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0) {
596                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
597                             wh, "data", "%s", "not legacy wds");
598                         vap->iv_stats.is_rx_wrongdir++;/*XXX*/
599                         goto out;
600                 }
601                 if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
602                         ni->ni_inact = ni->ni_inact_reload;
603                 /*
604                  * Handle A-MPDU re-ordering.  If the frame is to be
605                  * processed directly then ieee80211_ampdu_reorder
606                  * will return 0; otherwise it has consumed the mbuf
607                  * and we should do nothing more with it.
608                  */
609                 if ((m->m_flags & M_AMPDU) &&
610                     ieee80211_ampdu_reorder(ni, m) != 0) {
611                         m = NULL;
612                         goto out;
613                 }
614         resubmit_ampdu:
615
616                 /*
617                  * Handle privacy requirements.  Note that we
618                  * must not be preempted from here until after
619                  * we (potentially) call ieee80211_crypto_demic;
620                  * otherwise we may violate assumptions in the
621                  * crypto cipher modules used to do delayed update
622                  * of replay sequence numbers.
623                  */
624                 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
625                         if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
626                                 /*
627                                  * Discard encrypted frames when privacy is off.
628                                  */
629                                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
630                                     wh, "WEP", "%s", "PRIVACY off");
631                                 vap->iv_stats.is_rx_noprivacy++;
632                                 IEEE80211_NODE_STAT(ni, rx_noprivacy);
633                                 goto out;
634                         }
635                         key = ieee80211_crypto_decap(ni, m, hdrspace);
636                         if (key == NULL) {
637                                 /* NB: stats+msgs handled in crypto_decap */
638                                 IEEE80211_NODE_STAT(ni, rx_wepfail);
639                                 goto out;
640                         }
641                         wh = mtod(m, struct ieee80211_frame *);
642                         wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
643                 } else {
644                         /* XXX M_WEP and IEEE80211_F_PRIVACY */
645                         key = NULL;
646                 }
647
648                 /*
649                  * Save QoS bits for use below--before we strip the header.
650                  */
651                 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
652                         qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
653                             ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
654                             ((struct ieee80211_qosframe *)wh)->i_qos[0];
655                 } else
656                         qos = 0;
657
658                 /*
659                  * Next up, any fragmentation.
660                  */
661                 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
662                         m = ieee80211_defrag(ni, m, hdrspace);
663                         if (m == NULL) {
664                                 /* Fragment dropped or frame not complete yet */
665                                 goto out;
666                         }
667                 }
668                 wh = NULL;              /* no longer valid, catch any uses */
669
670                 /*
671                  * Next strip any MSDU crypto bits.
672                  */
673                 if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
674                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
675                             ni->ni_macaddr, "data", "%s", "demic error");
676                         vap->iv_stats.is_rx_demicfail++;
677                         IEEE80211_NODE_STAT(ni, rx_demicfail);
678                         goto out;
679                 }
680
681                 /* copy to listener after decrypt */
682                 if (bpf_peers_present(vap->iv_rawbpf))
683                         bpf_mtap(vap->iv_rawbpf, m);
684                 need_tap = 0;
685
686                 /*
687                  * Finally, strip the 802.11 header.
688                  */
689                 m = ieee80211_decap(vap, m, hdrspace);
690                 if (m == NULL) {
691                         /* XXX mask bit to check for both */
692                         /* don't count Null data frames as errors */
693                         if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
694                             subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
695                                 goto out;
696                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
697                             ni->ni_macaddr, "data", "%s", "decap error");
698                         vap->iv_stats.is_rx_decap++;
699                         IEEE80211_NODE_STAT(ni, rx_decap);
700                         goto err;
701                 }
702                 eh = mtod(m, struct ether_header *);
703                 if (!ieee80211_node_is_authorized(ni)) {
704                         /*
705                          * Deny any non-PAE frames received prior to
706                          * authorization.  For open/shared-key
707                          * authentication the port is mark authorized
708                          * after authentication completes.  For 802.1x
709                          * the port is not marked authorized by the
710                          * authenticator until the handshake has completed.
711                          */
712                         if (eh->ether_type != htons(ETHERTYPE_PAE)) {
713                                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
714                                     eh->ether_shost, "data",
715                                     "unauthorized port: ether type 0x%x len %u",
716                                     eh->ether_type, m->m_pkthdr.len);
717                                 vap->iv_stats.is_rx_unauth++;
718                                 IEEE80211_NODE_STAT(ni, rx_unauth);
719                                 goto err;
720                         }
721                 } else {
722                         /*
723                          * When denying unencrypted frames, discard
724                          * any non-PAE frames received without encryption.
725                          */
726                         if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
727                             (key == NULL && (m->m_flags & M_WEP) == 0) &&
728                             eh->ether_type != htons(ETHERTYPE_PAE)) {
729                                 /*
730                                  * Drop unencrypted frames.
731                                  */
732                                 vap->iv_stats.is_rx_unencrypted++;
733                                 IEEE80211_NODE_STAT(ni, rx_unencrypted);
734                                 goto out;
735                         }
736                 }
737                 /* XXX require HT? */
738                 if (qos & IEEE80211_QOS_AMSDU) {
739                         m = ieee80211_decap_amsdu(ni, m);
740                         if (m == NULL)
741                                 return IEEE80211_FC0_TYPE_DATA;
742                 } else {
743 #ifdef IEEE80211_SUPPORT_SUPERG
744                         m = ieee80211_decap_fastframe(vap, ni, m);
745                         if (m == NULL)
746                                 return IEEE80211_FC0_TYPE_DATA;
747 #endif
748                 }
749                 ieee80211_deliver_data(vap, ni, m);
750                 return IEEE80211_FC0_TYPE_DATA;
751
752         case IEEE80211_FC0_TYPE_MGT:
753                 vap->iv_stats.is_rx_mgmt++;
754                 IEEE80211_NODE_STAT(ni, rx_mgmt);
755                 if (dir != IEEE80211_FC1_DIR_NODS) {
756                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
757                             wh, "data", "incorrect dir 0x%x", dir);
758                         vap->iv_stats.is_rx_wrongdir++;
759                         goto err;
760                 }
761                 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
762                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
763                             ni->ni_macaddr, "mgt", "too short: len %u",
764                             m->m_pkthdr.len);
765                         vap->iv_stats.is_rx_tooshort++;
766                         goto out;
767                 }
768 #ifdef IEEE80211_DEBUG
769                 if (ieee80211_msg_debug(vap) || ieee80211_msg_dumppkts(vap)) {
770                         if_printf(ifp, "received %s from %s rssi %d\n",
771                             ieee80211_mgt_subtype_name[subtype >>
772                                 IEEE80211_FC0_SUBTYPE_SHIFT],
773                             ether_sprintf(wh->i_addr2), rssi);
774                 }
775 #endif
776                 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
777                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
778                             wh, NULL, "%s", "WEP set but not permitted");
779                         vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
780                         goto out;
781                 }
782                 if (bpf_peers_present(vap->iv_rawbpf))
783                         bpf_mtap(vap->iv_rawbpf, m);
784                 vap->iv_recv_mgmt(ni, m, subtype, rssi, noise, rstamp);
785                 m_freem(m);
786                 return IEEE80211_FC0_TYPE_MGT;
787
788         case IEEE80211_FC0_TYPE_CTL:
789                 vap->iv_stats.is_rx_ctl++;
790                 IEEE80211_NODE_STAT(ni, rx_ctrl);
791                 goto out;
792         default:
793                 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
794                     wh, "bad", "frame type 0x%x", type);
795                 /* should not come here */
796                 break;
797         }
798 err:
799         ifp->if_ierrors++;
800 out:
801         if (m != NULL) {
802                 if (bpf_peers_present(vap->iv_rawbpf) && need_tap)
803                         bpf_mtap(vap->iv_rawbpf, m);
804                 m_freem(m);
805         }
806         return type;
807 #undef SEQ_LEQ
808 }
809
810 static void
811 wds_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
812         int subtype, int rssi, int noise, u_int32_t rstamp)
813 {
814         struct ieee80211vap *vap = ni->ni_vap;
815         struct ieee80211com *ic = ni->ni_ic;
816         struct ieee80211_frame *wh;
817         u_int8_t *frm, *efrm;
818
819         wh = mtod(m0, struct ieee80211_frame *);
820         frm = (u_int8_t *)&wh[1];
821         efrm = mtod(m0, u_int8_t *) + m0->m_len;
822         switch (subtype) {
823         case IEEE80211_FC0_SUBTYPE_DEAUTH:
824         case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
825         case IEEE80211_FC0_SUBTYPE_BEACON:
826         case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
827         case IEEE80211_FC0_SUBTYPE_AUTH:
828         case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
829         case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
830         case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
831         case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
832         case IEEE80211_FC0_SUBTYPE_DISASSOC:
833                 vap->iv_stats.is_rx_mgtdiscard++;
834                 break;
835         case IEEE80211_FC0_SUBTYPE_ACTION:
836                 if (vap->iv_state != IEEE80211_S_RUN ||
837                     IEEE80211_IS_MULTICAST(wh->i_addr1)) {
838                         vap->iv_stats.is_rx_mgtdiscard++;
839                         break;
840                 }
841                 ni->ni_inact = ni->ni_inact_reload;
842                 if (ieee80211_parse_action(ni, m0) == 0)
843                         ic->ic_recv_action(ni, frm, efrm);
844                 break;
845         default:
846                 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
847                      wh, "mgt", "subtype 0x%x not handled", subtype);
848                 vap->iv_stats.is_rx_badsubtype++;
849                 break;
850         }
851 }