if: Per-cpu ifnet/ifaddr statistics, step 1/3
[dragonfly.git] / sys / netproto / 802_11 / wlan / ieee80211_adhoc.c
1 /*-
2  * Copyright (c) 2007-2009 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_adhoc.c 203422 2010-02-03 10:07:43Z rpaulo $
26  */
27
28 /*
29  * IEEE 802.11 IBSS 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_adhoc.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 #ifdef IEEE80211_SUPPORT_TDMA
62 #include <netproto/802_11/ieee80211_tdma.h>
63 #endif
64
65 #define IEEE80211_RATE2MBS(r)   (((r) & IEEE80211_RATE_VAL) / 2)
66
67 static  void adhoc_vattach(struct ieee80211vap *);
68 static  int adhoc_newstate(struct ieee80211vap *, enum ieee80211_state, int);
69 static int adhoc_input(struct ieee80211_node *, struct mbuf *, int, int);
70 static void adhoc_recv_mgmt(struct ieee80211_node *, struct mbuf *,
71         int subtype, int, int);
72 static void ahdemo_recv_mgmt(struct ieee80211_node *, struct mbuf *,
73         int subtype, int, int);
74 static void adhoc_recv_ctl(struct ieee80211_node *, struct mbuf *, int subtype);
75
76 void
77 ieee80211_adhoc_attach(struct ieee80211com *ic)
78 {
79         ic->ic_vattach[IEEE80211_M_IBSS] = adhoc_vattach;
80         ic->ic_vattach[IEEE80211_M_AHDEMO] = adhoc_vattach;
81 }
82
83 void
84 ieee80211_adhoc_detach(struct ieee80211com *ic)
85 {
86 }
87
88 static void
89 adhoc_vdetach(struct ieee80211vap *vap)
90 {
91 }
92
93 static void
94 adhoc_vattach(struct ieee80211vap *vap)
95 {
96         vap->iv_newstate = adhoc_newstate;
97         vap->iv_input = adhoc_input;
98         if (vap->iv_opmode == IEEE80211_M_IBSS)
99                 vap->iv_recv_mgmt = adhoc_recv_mgmt;
100         else
101                 vap->iv_recv_mgmt = ahdemo_recv_mgmt;
102         vap->iv_recv_ctl = adhoc_recv_ctl;
103         vap->iv_opdetach = adhoc_vdetach;
104 #ifdef IEEE80211_SUPPORT_TDMA
105         /*
106          * Throw control to tdma support.  Note we do this
107          * after setting up our callbacks so it can piggyback
108          * on top of us.
109          */
110         if (vap->iv_caps & IEEE80211_C_TDMA)
111                 ieee80211_tdma_vattach(vap);
112 #endif
113 }
114
115 static void
116 sta_leave(void *arg, struct ieee80211_node *ni)
117 {
118         struct ieee80211vap *vap = arg;
119
120         if (ni->ni_vap == vap && ni != vap->iv_bss)
121                 ieee80211_node_leave(ni);
122 }
123
124 /*
125  * IEEE80211_M_IBSS+IEEE80211_M_AHDEMO vap state machine handler.
126  */
127 static int
128 adhoc_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
129 {
130         struct ieee80211com *ic = vap->iv_ic;
131         struct ieee80211_node *ni;
132         enum ieee80211_state ostate;
133 #ifdef IEEE80211_DEBUG
134         char ethstr[ETHER_ADDRSTRLEN + 1];
135 #endif
136
137         ostate = vap->iv_state;
138         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
139             __func__, ieee80211_state_name[ostate],
140             ieee80211_state_name[nstate], arg);
141         vap->iv_state = nstate;                 /* state transition */
142         if (ostate != IEEE80211_S_SCAN)
143                 ieee80211_cancel_scan(vap);     /* background scan */
144         ni = vap->iv_bss;                       /* NB: no reference held */
145         switch (nstate) {
146         case IEEE80211_S_INIT:
147                 switch (ostate) {
148                 case IEEE80211_S_SCAN:
149                         ieee80211_cancel_scan(vap);
150                         break;
151                 default:
152                         break;
153                 }
154                 if (ostate != IEEE80211_S_INIT) {
155                         /* NB: optimize INIT -> INIT case */
156                         ieee80211_reset_bss(vap);
157                 }
158                 break;
159         case IEEE80211_S_SCAN:
160                 switch (ostate) {
161                 case IEEE80211_S_RUN:           /* beacon miss */
162                         /* purge station table; entries are stale */
163                         ieee80211_iterate_nodes(&ic->ic_sta, sta_leave, vap);
164                         /* fall thru... */
165                 case IEEE80211_S_INIT:
166                         if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
167                             !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
168                                 /*
169                                  * Already have a channel; bypass the
170                                  * scan and startup immediately.
171                                  */
172                                 ieee80211_create_ibss(vap, vap->iv_des_chan);
173                                 break;
174                         }
175                         /*
176                          * Initiate a scan.  We can come here as a result
177                          * of an IEEE80211_IOC_SCAN_REQ too in which case
178                          * the vap will be marked with IEEE80211_FEXT_SCANREQ
179                          * and the scan request parameters will be present
180                          * in iv_scanreq.  Otherwise we do the default.
181                          */
182                         if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
183                                 ieee80211_check_scan(vap,
184                                     vap->iv_scanreq_flags,
185                                     vap->iv_scanreq_duration,
186                                     vap->iv_scanreq_mindwell,
187                                     vap->iv_scanreq_maxdwell,
188                                     vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
189                                 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
190                         } else
191                                 ieee80211_check_scan_current(vap);
192                         break;
193                 case IEEE80211_S_SCAN:
194                         /*
195                          * This can happen because of a change in state
196                          * that requires a reset.  Trigger a new scan
197                          * unless we're in manual roaming mode in which
198                          * case an application must issue an explicit request.
199                          */
200                         if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
201                                 ieee80211_check_scan_current(vap);
202                         break;
203                 default:
204                         goto invalid;
205                 }
206                 break;
207         case IEEE80211_S_RUN:
208                 if (vap->iv_flags & IEEE80211_F_WPA) {
209                         /* XXX validate prerequisites */
210                 }
211                 switch (ostate) {
212                 case IEEE80211_S_SCAN:
213 #ifdef IEEE80211_DEBUG
214                         if (ieee80211_msg_debug(vap)) {
215                                 ieee80211_note(vap,
216                                     "synchronized with %s ssid ",
217                                     kether_ntoa(ni->ni_bssid, ethstr));
218                                 ieee80211_print_essid(vap->iv_bss->ni_essid,
219                                     ni->ni_esslen);
220                                 /* XXX MCS/HT */
221                                 kprintf(" channel %d start %uMb\n",
222                                     ieee80211_chan2ieee(ic, ic->ic_curchan),
223                                     IEEE80211_RATE2MBS(ni->ni_txrate));
224                         }
225 #endif
226                         break;
227                 default:
228                         goto invalid;
229                 }
230                 /*
231                  * When 802.1x is not in use mark the port authorized
232                  * at this point so traffic can flow.
233                  */
234                 if (ni->ni_authmode != IEEE80211_AUTH_8021X)
235                         ieee80211_node_authorize(ni);
236                 /*
237                  * Fake association when joining an existing bss.
238                  */
239                 if (!IEEE80211_ADDR_EQ(ni->ni_macaddr, vap->iv_myaddr) &&
240                     ic->ic_newassoc != NULL)
241                         ic->ic_newassoc(ni, ostate != IEEE80211_S_RUN);
242                 break;
243         case IEEE80211_S_SLEEP:
244                 ieee80211_sta_pwrsave(vap, 0);
245                 break;
246         default:
247         invalid:
248                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
249                     "%s: unexpected state transition %s -> %s\n", __func__,
250                     ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
251                 break;
252         }
253         return 0;
254 }
255
256 /*
257  * Decide if a received management frame should be
258  * printed when debugging is enabled.  This filters some
259  * of the less interesting frames that come frequently
260  * (e.g. beacons).
261  */
262 static __inline int
263 doprint(struct ieee80211vap *vap, int subtype)
264 {
265         switch (subtype) {
266         case IEEE80211_FC0_SUBTYPE_BEACON:
267                 return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
268         case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
269                 return 1;
270         }
271         return 1;
272 }
273
274 /*
275  * Process a received frame.  The node associated with the sender
276  * should be supplied.  If nothing was found in the node table then
277  * the caller is assumed to supply a reference to iv_bss instead.
278  * The RSSI and a timestamp are also supplied.  The RSSI data is used
279  * during AP scanning to select a AP to associate with; it can have
280  * any units so long as values have consistent units and higher values
281  * mean ``better signal''.  The receive timestamp is currently not used
282  * by the 802.11 layer.
283  */
284 static int
285 adhoc_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
286 {
287 #define SEQ_LEQ(a,b)    ((int)((a)-(b)) <= 0)
288 #define HAS_SEQ(type)   ((type & 0x4) == 0)
289         struct ieee80211vap *vap = ni->ni_vap;
290         struct ieee80211com *ic = ni->ni_ic;
291         struct ifnet *ifp = vap->iv_ifp;
292         struct ieee80211_frame *wh;
293         struct ieee80211_key *key;
294         struct ether_header *eh;
295         int hdrspace, need_tap = 1;     /* mbuf need to be tapped. */   
296         uint8_t dir, type, subtype, qos;
297         uint8_t *bssid;
298         uint16_t rxseq;
299 #ifdef IEEE80211_DEBUG
300         char ethstr[ETHER_ADDRSTRLEN + 1];
301 #endif
302
303         if (m->m_flags & M_AMPDU_MPDU) {
304                 /*
305                  * Fastpath for A-MPDU reorder q resubmission.  Frames
306                  * w/ M_AMPDU_MPDU marked have already passed through
307                  * here but were received out of order and been held on
308                  * the reorder queue.  When resubmitted they are marked
309                  * with the M_AMPDU_MPDU flag and we can bypass most of
310                  * the normal processing.
311                  */
312                 wh = mtod(m, struct ieee80211_frame *);
313                 type = IEEE80211_FC0_TYPE_DATA;
314                 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
315                 subtype = IEEE80211_FC0_SUBTYPE_QOS;
316                 hdrspace = ieee80211_hdrspace(ic, wh);  /* XXX optimize? */
317                 goto resubmit_ampdu;
318         }
319
320         KASSERT(ni != NULL, ("null node"));
321         ni->ni_inact = ni->ni_inact_reload;
322
323         type = -1;                      /* undefined */
324
325         if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
326                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
327                     ni->ni_macaddr, NULL,
328                     "too short (1): len %u", m->m_pkthdr.len);
329                 vap->iv_stats.is_rx_tooshort++;
330                 goto out;
331         }
332         /*
333          * Bit of a cheat here, we use a pointer for a 3-address
334          * frame format but don't reference fields past outside
335          * ieee80211_frame_min w/o first validating the data is
336          * present.
337          */
338         wh = mtod(m, struct ieee80211_frame *);
339
340         if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
341             IEEE80211_FC0_VERSION_0) {
342                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
343                     ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
344                     wh->i_fc[0], wh->i_fc[1]);
345                 vap->iv_stats.is_rx_badversion++;
346                 goto err;
347         }
348
349         dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
350         type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
351         subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
352         if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
353                 if (dir != IEEE80211_FC1_DIR_NODS)
354                         bssid = wh->i_addr1;
355                 else if (type == IEEE80211_FC0_TYPE_CTL)
356                         bssid = wh->i_addr1;
357                 else {
358                         if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
359                                 IEEE80211_DISCARD_MAC(vap,
360                                     IEEE80211_MSG_ANY, ni->ni_macaddr,
361                                     NULL, "too short (2): len %u",
362                                     m->m_pkthdr.len);
363                                 vap->iv_stats.is_rx_tooshort++;
364                                 goto out;
365                         }
366                         bssid = wh->i_addr3;
367                 }
368                 /*
369                  * Validate the bssid.
370                  */
371                 if (!IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
372                     !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
373                         /* not interested in */
374                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
375                             bssid, NULL, "%s", "not to bss");
376                         vap->iv_stats.is_rx_wrongbss++;
377                         goto out;
378                 }
379                 /*
380                  * Data frame, cons up a node when it doesn't
381                  * exist. This should probably done after an ACL check.
382                  */
383                 if (type == IEEE80211_FC0_TYPE_DATA &&
384                     ni == vap->iv_bss &&
385                     !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
386                         /*
387                          * Beware of frames that come in too early; we
388                          * can receive broadcast frames and creating sta
389                          * entries will blow up because there is no bss
390                          * channel yet.
391                          */
392                         if (vap->iv_state != IEEE80211_S_RUN) {
393                                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
394                                     wh, "data", "not in RUN state (%s)",
395                                     ieee80211_state_name[vap->iv_state]);
396                                 vap->iv_stats.is_rx_badstate++;
397                                 goto err;
398                         }
399                         /*
400                          * Fake up a node for this newly
401                          * discovered member of the IBSS.
402                          */
403                         ni = ieee80211_fakeup_adhoc_node(vap, wh->i_addr2);
404                         if (ni == NULL) {
405                                 /* NB: stat kept for alloc failure */
406                                 goto err;
407                         }
408                 }
409                 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
410                 ni->ni_noise = nf;
411                 if (HAS_SEQ(type)) {
412                         uint8_t tid = ieee80211_gettid(wh);
413                         if (IEEE80211_QOS_HAS_SEQ(wh) &&
414                             TID_TO_WME_AC(tid) >= WME_AC_VI)
415                                 ic->ic_wme.wme_hipri_traffic++;
416                         rxseq = le16toh(*(uint16_t *)wh->i_seq);
417                         if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
418                             (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
419                             SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
420                                 /* duplicate, discard */
421                                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
422                                     bssid, "duplicate",
423                                     "seqno <%u,%u> fragno <%u,%u> tid %u",
424                                     rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
425                                     ni->ni_rxseqs[tid] >>
426                                         IEEE80211_SEQ_SEQ_SHIFT,
427                                     rxseq & IEEE80211_SEQ_FRAG_MASK,
428                                     ni->ni_rxseqs[tid] &
429                                         IEEE80211_SEQ_FRAG_MASK,
430                                     tid);
431                                 vap->iv_stats.is_rx_dup++;
432                                 IEEE80211_NODE_STAT(ni, rx_dup);
433                                 goto out;
434                         }
435                         ni->ni_rxseqs[tid] = rxseq;
436                 }
437         }
438
439         switch (type) {
440         case IEEE80211_FC0_TYPE_DATA:
441                 hdrspace = ieee80211_hdrspace(ic, wh);
442                 if (m->m_len < hdrspace &&
443                     (m = m_pullup(m, hdrspace)) == NULL) {
444                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
445                             ni->ni_macaddr, NULL,
446                             "data too short: expecting %u", hdrspace);
447                         vap->iv_stats.is_rx_tooshort++;
448                         goto out;               /* XXX */
449                 }
450                 if (dir != IEEE80211_FC1_DIR_NODS) {
451                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
452                             wh, "data", "incorrect dir 0x%x", dir);
453                         vap->iv_stats.is_rx_wrongdir++;
454                         goto out;
455                 }
456                 /* XXX no power-save support */
457
458                 /*
459                  * Handle A-MPDU re-ordering.  If the frame is to be
460                  * processed directly then ieee80211_ampdu_reorder
461                  * will return 0; otherwise it has consumed the mbuf
462                  * and we should do nothing more with it.
463                  */
464                 if ((m->m_flags & M_AMPDU) &&
465                     ieee80211_ampdu_reorder(ni, m) != 0) {
466                         m = NULL;
467                         goto out;
468                 }
469         resubmit_ampdu:
470
471                 /*
472                  * Handle privacy requirements.  Note that we
473                  * must not be preempted from here until after
474                  * we (potentially) call ieee80211_crypto_demic;
475                  * otherwise we may violate assumptions in the
476                  * crypto cipher modules used to do delayed update
477                  * of replay sequence numbers.
478                  */
479                 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
480                         if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
481                                 /*
482                                  * Discard encrypted frames when privacy is off.
483                                  */
484                                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
485                                     wh, "WEP", "%s", "PRIVACY off");
486                                 vap->iv_stats.is_rx_noprivacy++;
487                                 IEEE80211_NODE_STAT(ni, rx_noprivacy);
488                                 goto out;
489                         }
490                         key = ieee80211_crypto_decap(ni, m, hdrspace);
491                         if (key == NULL) {
492                                 /* NB: stats+msgs handled in crypto_decap */
493                                 IEEE80211_NODE_STAT(ni, rx_wepfail);
494                                 goto out;
495                         }
496                         wh = mtod(m, struct ieee80211_frame *);
497                         wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
498                 } else {
499                         /* XXX M_WEP and IEEE80211_F_PRIVACY */
500                         key = NULL;
501                 }
502
503                 /*
504                  * Save QoS bits for use below--before we strip the header.
505                  */
506                 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
507                         qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
508                             ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
509                             ((struct ieee80211_qosframe *)wh)->i_qos[0];
510                 } else
511                         qos = 0;
512
513                 /*
514                  * Next up, any fragmentation.
515                  */
516                 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
517                         m = ieee80211_defrag(ni, m, hdrspace);
518                         if (m == NULL) {
519                                 /* Fragment dropped or frame not complete yet */
520                                 goto out;
521                         }
522                 }
523                 wh = NULL;              /* no longer valid, catch any uses */
524
525                 /*
526                  * Next strip any MSDU crypto bits.
527                  */
528                 if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
529                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
530                             ni->ni_macaddr, "data", "%s", "demic error");
531                         vap->iv_stats.is_rx_demicfail++;
532                         IEEE80211_NODE_STAT(ni, rx_demicfail);
533                         goto out;
534                 }
535
536                 /* copy to listener after decrypt */
537                 if (ieee80211_radiotap_active_vap(vap))
538                         ieee80211_radiotap_rx(vap, m);
539                 need_tap = 0;
540
541                 /*
542                  * Finally, strip the 802.11 header.
543                  */
544                 m = ieee80211_decap(vap, m, hdrspace);
545                 if (m == NULL) {
546                         /* XXX mask bit to check for both */
547                         /* don't count Null data frames as errors */
548                         if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
549                             subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
550                                 goto out;
551                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
552                             ni->ni_macaddr, "data", "%s", "decap error");
553                         vap->iv_stats.is_rx_decap++;
554                         IEEE80211_NODE_STAT(ni, rx_decap);
555                         goto err;
556                 }
557                 eh = mtod(m, struct ether_header *);
558                 if (!ieee80211_node_is_authorized(ni)) {
559                         /*
560                          * Deny any non-PAE frames received prior to
561                          * authorization.  For open/shared-key
562                          * authentication the port is mark authorized
563                          * after authentication completes.  For 802.1x
564                          * the port is not marked authorized by the
565                          * authenticator until the handshake has completed.
566                          */
567                         if (eh->ether_type != htons(ETHERTYPE_PAE)) {
568                                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
569                                     eh->ether_shost, "data",
570                                     "unauthorized port: ether type 0x%x len %u",
571                                     eh->ether_type, m->m_pkthdr.len);
572                                 vap->iv_stats.is_rx_unauth++;
573                                 IEEE80211_NODE_STAT(ni, rx_unauth);
574                                 goto err;
575                         }
576                 } else {
577                         /*
578                          * When denying unencrypted frames, discard
579                          * any non-PAE frames received without encryption.
580                          */
581                         if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
582                             (key == NULL && (m->m_flags & M_WEP) == 0) &&
583                             eh->ether_type != htons(ETHERTYPE_PAE)) {
584                                 /*
585                                  * Drop unencrypted frames.
586                                  */
587                                 vap->iv_stats.is_rx_unencrypted++;
588                                 IEEE80211_NODE_STAT(ni, rx_unencrypted);
589                                 goto out;
590                         }
591                 }
592                 /* XXX require HT? */
593                 if (qos & IEEE80211_QOS_AMSDU) {
594                         m = ieee80211_decap_amsdu(ni, m);
595                         if (m == NULL)
596                                 return IEEE80211_FC0_TYPE_DATA;
597                 } else {
598 #ifdef IEEE80211_SUPPORT_SUPERG
599                         m = ieee80211_decap_fastframe(vap, ni, m);
600                         if (m == NULL)
601                                 return IEEE80211_FC0_TYPE_DATA;
602 #endif
603                 }
604                 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
605                         ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
606                 else
607                         ieee80211_deliver_data(vap, ni, m);
608                 return IEEE80211_FC0_TYPE_DATA;
609
610         case IEEE80211_FC0_TYPE_MGT:
611                 vap->iv_stats.is_rx_mgmt++;
612                 IEEE80211_NODE_STAT(ni, rx_mgmt);
613                 if (dir != IEEE80211_FC1_DIR_NODS) {
614                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
615                             wh, "data", "incorrect dir 0x%x", dir);
616                         vap->iv_stats.is_rx_wrongdir++;
617                         goto err;
618                 }
619                 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
620                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
621                             ni->ni_macaddr, "mgt", "too short: len %u",
622                             m->m_pkthdr.len);
623                         vap->iv_stats.is_rx_tooshort++;
624                         goto out;
625                 }
626 #ifdef IEEE80211_DEBUG
627                 if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
628                     ieee80211_msg_dumppkts(vap)) {
629                         if_printf(ifp, "received %s from %s rssi %d\n",
630                             ieee80211_mgt_subtype_name[subtype >>
631                                 IEEE80211_FC0_SUBTYPE_SHIFT],
632                             kether_ntoa(wh->i_addr2, ethstr), rssi);
633                 }
634 #endif
635                 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
636                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
637                             wh, NULL, "%s", "WEP set but not permitted");
638                         vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
639                         goto out;
640                 }
641                 vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
642                 goto out;
643
644         case IEEE80211_FC0_TYPE_CTL:
645                 vap->iv_stats.is_rx_ctl++;
646                 IEEE80211_NODE_STAT(ni, rx_ctrl);
647                 vap->iv_recv_ctl(ni, m, subtype);
648                 goto out;
649
650         default:
651                 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
652                     wh, "bad", "frame type 0x%x", type);
653                 /* should not come here */
654                 break;
655         }
656 err:
657         IFNET_STAT_INC(ifp, ierrors, 1);
658 out:
659         if (m != NULL) {
660                 if (need_tap && ieee80211_radiotap_active_vap(vap))
661                         ieee80211_radiotap_rx(vap, m);
662                 m_freem(m);
663         }
664         return type;
665 #undef SEQ_LEQ
666 }
667
668 static int
669 is11bclient(const uint8_t *rates, const uint8_t *xrates)
670 {
671         static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
672         int i;
673
674         /* NB: the 11b clients we care about will not have xrates */
675         if (xrates != NULL || rates == NULL)
676                 return 0;
677         for (i = 0; i < rates[1]; i++) {
678                 int r = rates[2+i] & IEEE80211_RATE_VAL;
679                 if (r > 2*11 || ((1<<r) & brates) == 0)
680                         return 0;
681         }
682         return 1;
683 }
684
685 static void
686 adhoc_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
687         int subtype, int rssi, int nf)
688 {
689         struct ieee80211vap *vap = ni->ni_vap;
690         struct ieee80211com *ic = ni->ni_ic;
691         struct ieee80211_frame *wh;
692         uint8_t *frm, *efrm;
693         uint8_t *ssid, *rates, *xrates;
694
695         wh = mtod(m0, struct ieee80211_frame *);
696         frm = (uint8_t *)&wh[1];
697         efrm = mtod(m0, uint8_t *) + m0->m_len;
698         switch (subtype) {
699         case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
700         case IEEE80211_FC0_SUBTYPE_BEACON: {
701                 struct ieee80211_scanparams scan;
702                 /*
703                  * We process beacon/probe response
704                  * frames to discover neighbors.
705                  */ 
706                 if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
707                         return;
708                 /*
709                  * Count frame now that we know it's to be processed.
710                  */
711                 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
712                         vap->iv_stats.is_rx_beacon++;           /* XXX remove */
713                         IEEE80211_NODE_STAT(ni, rx_beacons);
714                 } else
715                         IEEE80211_NODE_STAT(ni, rx_proberesp);
716                 /*
717                  * If scanning, just pass information to the scan module.
718                  */
719                 if (ic->ic_flags & IEEE80211_F_SCAN) {
720                         if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
721                                 /*
722                                  * Actively scanning a channel marked passive;
723                                  * send a probe request now that we know there
724                                  * is 802.11 traffic present.
725                                  *
726                                  * XXX check if the beacon we recv'd gives
727                                  * us what we need and suppress the probe req
728                                  */
729                                 ieee80211_probe_curchan(vap, 1);
730                                 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
731                         }
732                         ieee80211_add_scan(vap, &scan, wh, subtype, rssi, nf);
733                         return;
734                 }
735                 if (scan.capinfo & IEEE80211_CAPINFO_IBSS) {
736                         if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
737                                 /*
738                                  * Create a new entry in the neighbor table.
739                                  */
740                                 ni = ieee80211_add_neighbor(vap, wh, &scan);
741                         } else if (ni->ni_capinfo == 0) {
742                                 /*
743                                  * Update faked node created on transmit.
744                                  * Note this also updates the tsf.
745                                  */
746                                 ieee80211_init_neighbor(ni, wh, &scan);
747                         } else {
748                                 /*
749                                  * Record tsf for potential resync.
750                                  */
751                                 memcpy(ni->ni_tstamp.data, scan.tstamp,
752                                         sizeof(ni->ni_tstamp));
753                         }
754                         if (ni != NULL) {
755                                 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
756                                 ni->ni_noise = nf;
757                         }
758                 }
759                 break;
760         }
761
762         case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
763                 if (vap->iv_state != IEEE80211_S_RUN) {
764                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
765                             wh, NULL, "wrong state %s",
766                             ieee80211_state_name[vap->iv_state]);
767                         vap->iv_stats.is_rx_mgtdiscard++;
768                         return;
769                 }
770                 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
771                         /* frame must be directed */
772                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
773                             wh, NULL, "%s", "not unicast");
774                         vap->iv_stats.is_rx_mgtdiscard++;       /* XXX stat */
775                         return;
776                 }
777
778                 /*
779                  * prreq frame format
780                  *      [tlv] ssid
781                  *      [tlv] supported rates
782                  *      [tlv] extended supported rates
783                  */
784                 ssid = rates = xrates = NULL;
785                 while (efrm - frm > 1) {
786                         IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
787                         switch (*frm) {
788                         case IEEE80211_ELEMID_SSID:
789                                 ssid = frm;
790                                 break;
791                         case IEEE80211_ELEMID_RATES:
792                                 rates = frm;
793                                 break;
794                         case IEEE80211_ELEMID_XRATES:
795                                 xrates = frm;
796                                 break;
797                         }
798                         frm += frm[1] + 2;
799                 }
800                 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
801                 if (xrates != NULL)
802                         IEEE80211_VERIFY_ELEMENT(xrates,
803                                 IEEE80211_RATE_MAXSIZE - rates[1], return);
804                 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
805                 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
806                 if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
807                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
808                             wh, NULL,
809                             "%s", "no ssid with ssid suppression enabled");
810                         vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
811                         return;
812                 }
813
814                 /* XXX find a better class or define it's own */
815                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
816                     "%s", "recv probe req");
817                 /*
818                  * Some legacy 11b clients cannot hack a complete
819                  * probe response frame.  When the request includes
820                  * only a bare-bones rate set, communicate this to
821                  * the transmit side.
822                  */
823                 ieee80211_send_proberesp(vap, wh->i_addr2,
824                     is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
825                 break;
826
827         case IEEE80211_FC0_SUBTYPE_ACTION: {
828                 const struct ieee80211_action *ia;
829
830                 if (vap->iv_state != IEEE80211_S_RUN) {
831                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
832                             wh, NULL, "wrong state %s",
833                             ieee80211_state_name[vap->iv_state]);
834                         vap->iv_stats.is_rx_mgtdiscard++;
835                         return;
836                 }
837                 /*
838                  * action frame format:
839                  *      [1] category
840                  *      [1] action
841                  *      [tlv] parameters
842                  */
843                 IEEE80211_VERIFY_LENGTH(efrm - frm,
844                         sizeof(struct ieee80211_action), return);
845                 ia = (const struct ieee80211_action *) frm;
846
847                 vap->iv_stats.is_rx_action++;
848                 IEEE80211_NODE_STAT(ni, rx_action);
849
850                 /* verify frame payloads but defer processing */
851                 /* XXX maybe push this to method */
852                 switch (ia->ia_category) {
853                 case IEEE80211_ACTION_CAT_BA:
854                         switch (ia->ia_action) {
855                         case IEEE80211_ACTION_BA_ADDBA_REQUEST:
856                                 IEEE80211_VERIFY_LENGTH(efrm - frm,
857                                     sizeof(struct ieee80211_action_ba_addbarequest),
858                                     return);
859                                 break;
860                         case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
861                                 IEEE80211_VERIFY_LENGTH(efrm - frm,
862                                     sizeof(struct ieee80211_action_ba_addbaresponse),
863                                     return);
864                                 break;
865                         case IEEE80211_ACTION_BA_DELBA:
866                                 IEEE80211_VERIFY_LENGTH(efrm - frm,
867                                     sizeof(struct ieee80211_action_ba_delba),
868                                     return);
869                                 break;
870                         }
871                         break;
872                 case IEEE80211_ACTION_CAT_HT:
873                         switch (ia->ia_action) {
874                         case IEEE80211_ACTION_HT_TXCHWIDTH:
875                                 IEEE80211_VERIFY_LENGTH(efrm - frm,
876                                     sizeof(struct ieee80211_action_ht_txchwidth),
877                                     return);
878                                 break;
879                         }
880                         break;
881                 }
882                 ic->ic_recv_action(ni, wh, frm, efrm);
883                 break;
884         }
885
886         case IEEE80211_FC0_SUBTYPE_AUTH:
887         case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
888         case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
889         case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
890         case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
891         case IEEE80211_FC0_SUBTYPE_DEAUTH:
892         case IEEE80211_FC0_SUBTYPE_DISASSOC:
893                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
894                      wh, NULL, "%s", "not handled");
895                 vap->iv_stats.is_rx_mgtdiscard++;
896                 return;
897
898         default:
899                 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
900                      wh, "mgt", "subtype 0x%x not handled", subtype);
901                 vap->iv_stats.is_rx_badsubtype++;
902                 break;
903         }
904 }
905 #undef IEEE80211_VERIFY_LENGTH
906 #undef IEEE80211_VERIFY_ELEMENT
907
908 static void
909 ahdemo_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
910         int subtype, int rssi, int nf)
911 {
912         struct ieee80211vap *vap = ni->ni_vap;
913         struct ieee80211com *ic = ni->ni_ic;
914
915         /*
916          * Process management frames when scanning; useful for doing
917          * a site-survey.
918          */
919         if (ic->ic_flags & IEEE80211_F_SCAN)
920                 adhoc_recv_mgmt(ni, m0, subtype, rssi, nf);
921         else
922                 vap->iv_stats.is_rx_mgtdiscard++;
923 }
924
925 static void
926 adhoc_recv_ctl(struct ieee80211_node *ni, struct mbuf *m0, int subtype)
927 {
928 }