7754262f049b6367faa36cff1437aec3bedecbe0
[dragonfly.git] / sys / netproto / 802_11 / wlan / ieee80211_hostap.c
1 /*-
2  * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * $FreeBSD: head/sys/net80211/ieee80211_hostap.c 203422 2010-02-03 10:07:43Z rpaulo $
26  * $DragonFly$
27  */
28
29 /*
30  * IEEE 802.11 HOSTAP mode support.
31  */
32 #include "opt_inet.h"
33 #include "opt_wlan.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h> 
37 #include <sys/mbuf.h>   
38 #include <sys/malloc.h>
39 #include <sys/kernel.h>
40
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
43 #include <sys/endian.h>
44 #include <sys/errno.h>
45 #include <sys/proc.h>
46 #include <sys/sysctl.h>
47
48 #include <net/if.h>
49 #include <net/if_media.h>
50 #include <net/if_llc.h>
51 #include <net/ethernet.h>
52 #include <net/route.h>
53
54 #include <net/bpf.h>
55
56 #include <netproto/802_11/ieee80211_var.h>
57 #include <netproto/802_11/ieee80211_hostap.h>
58 #include <netproto/802_11/ieee80211_input.h>
59 #ifdef IEEE80211_SUPPORT_SUPERG
60 #include <netproto/802_11/ieee80211_superg.h>
61 #endif
62 #include <netproto/802_11/ieee80211_wds.h>
63
64 #define IEEE80211_RATE2MBS(r)   (((r) & IEEE80211_RATE_VAL) / 2)
65
66 static  void hostap_vattach(struct ieee80211vap *);
67 static  int hostap_newstate(struct ieee80211vap *, enum ieee80211_state, int);
68 static  int hostap_input(struct ieee80211_node *ni, struct mbuf *m,
69             int rssi, int nf);
70 static void hostap_deliver_data(struct ieee80211vap *,
71             struct ieee80211_node *, struct mbuf *);
72 static void hostap_recv_mgmt(struct ieee80211_node *, struct mbuf *,
73             int subtype, int rssi, int nf);
74 static void hostap_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
75 static void hostap_recv_pspoll(struct ieee80211_node *, struct mbuf *);
76
77 void
78 ieee80211_hostap_attach(struct ieee80211com *ic)
79 {
80         ic->ic_vattach[IEEE80211_M_HOSTAP] = hostap_vattach;
81 }
82
83 void
84 ieee80211_hostap_detach(struct ieee80211com *ic)
85 {
86 }
87
88 static void
89 hostap_vdetach(struct ieee80211vap *vap)
90 {
91 }
92
93 static void
94 hostap_vattach(struct ieee80211vap *vap)
95 {
96         vap->iv_newstate = hostap_newstate;
97         vap->iv_input = hostap_input;
98         vap->iv_recv_mgmt = hostap_recv_mgmt;
99         vap->iv_recv_ctl = hostap_recv_ctl;
100         vap->iv_opdetach = hostap_vdetach;
101         vap->iv_deliver_data = hostap_deliver_data;
102 }
103
104 static void
105 sta_disassoc(void *arg, struct ieee80211_node *ni)
106 {
107         struct ieee80211vap *vap = arg;
108
109         if (ni->ni_vap == vap && ni->ni_associd != 0) {
110                 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
111                         IEEE80211_REASON_ASSOC_LEAVE);
112                 ieee80211_node_leave(ni);
113         }
114 }
115
116 static void
117 sta_csa(void *arg, struct ieee80211_node *ni)
118 {
119         struct ieee80211vap *vap = arg;
120
121         if (ni->ni_vap == vap && ni->ni_associd != 0)
122                 if (ni->ni_inact > vap->iv_inact_init) {
123                         ni->ni_inact = vap->iv_inact_init;
124                         IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
125                             "%s: inact %u", __func__, ni->ni_inact);
126                 }
127 }
128
129 static void
130 sta_drop(void *arg, struct ieee80211_node *ni)
131 {
132         struct ieee80211vap *vap = arg;
133
134         if (ni->ni_vap == vap && ni->ni_associd != 0)
135                 ieee80211_node_leave(ni);
136 }
137
138 /*
139  * Does a channel change require associated stations to re-associate
140  * so protocol state is correct.  This is used when doing CSA across
141  * bands or similar (e.g. HT -> legacy).
142  */
143 static int
144 isbandchange(struct ieee80211com *ic)
145 {
146         return ((ic->ic_bsschan->ic_flags ^ ic->ic_csa_newchan->ic_flags) &
147             (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_HALF |
148              IEEE80211_CHAN_QUARTER | IEEE80211_CHAN_HT)) != 0;
149 }
150
151 /*
152  * IEEE80211_M_HOSTAP vap state machine handler.
153  */
154 static int
155 hostap_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
156 {
157         struct ieee80211com *ic = vap->iv_ic;
158         enum ieee80211_state ostate;
159
160         IEEE80211_LOCK_ASSERT(ic);
161
162         ostate = vap->iv_state;
163         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
164             __func__, ieee80211_state_name[ostate],
165             ieee80211_state_name[nstate], arg);
166         vap->iv_state = nstate;                 /* state transition */
167         if (ostate != IEEE80211_S_SCAN)
168                 ieee80211_cancel_scan(vap);     /* background scan */
169         switch (nstate) {
170         case IEEE80211_S_INIT:
171                 switch (ostate) {
172                 case IEEE80211_S_SCAN:
173                         ieee80211_cancel_scan(vap);
174                         break;
175                 case IEEE80211_S_CAC:
176                         ieee80211_dfs_cac_stop(vap);
177                         break;
178                 case IEEE80211_S_RUN:
179                         ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
180                         break;
181                 default:
182                         break;
183                 }
184                 if (ostate != IEEE80211_S_INIT) {
185                         /* NB: optimize INIT -> INIT case */
186                         ieee80211_reset_bss(vap);
187                 }
188                 if (vap->iv_auth->ia_detach != NULL)
189                         vap->iv_auth->ia_detach(vap);
190                 break;
191         case IEEE80211_S_SCAN:
192                 switch (ostate) {
193                 case IEEE80211_S_CSA:
194                 case IEEE80211_S_RUN:
195                         ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
196                         /*
197                          * Clear overlapping BSS state; the beacon frame
198                          * will be reconstructed on transition to the RUN
199                          * state and the timeout routines check if the flag
200                          * is set before doing anything so this is sufficient.
201                          */
202                         ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
203                         ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
204                         /* fall thru... */
205                 case IEEE80211_S_CAC:
206                         /*
207                          * NB: We may get here because of a manual channel
208                          *     change in which case we need to stop CAC
209                          * XXX no need to stop if ostate RUN but it's ok
210                          */
211                         ieee80211_dfs_cac_stop(vap);
212                         /* fall thru... */
213                 case IEEE80211_S_INIT:
214                         if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
215                             !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
216                                 /*
217                                  * Already have a channel; bypass the
218                                  * scan and startup immediately.  
219                                  * ieee80211_create_ibss will call back to
220                                  * move us to RUN state.
221                                  */
222                                 ieee80211_create_ibss(vap, vap->iv_des_chan);
223                                 break;
224                         }
225                         /*
226                          * Initiate a scan.  We can come here as a result
227                          * of an IEEE80211_IOC_SCAN_REQ too in which case
228                          * the vap will be marked with IEEE80211_FEXT_SCANREQ
229                          * and the scan request parameters will be present
230                          * in iv_scanreq.  Otherwise we do the default.
231                          */
232                         if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
233                                 ieee80211_check_scan(vap,
234                                     vap->iv_scanreq_flags,
235                                     vap->iv_scanreq_duration,
236                                     vap->iv_scanreq_mindwell,
237                                     vap->iv_scanreq_maxdwell,
238                                     vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
239                                 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
240                         } else
241                                 ieee80211_check_scan_current(vap);
242                         break;
243                 case IEEE80211_S_SCAN:
244                         /*
245                          * A state change requires a reset; scan.
246                          */
247                         ieee80211_check_scan_current(vap);
248                         break;
249                 default:
250                         break;
251                 }
252                 break;
253         case IEEE80211_S_CAC:
254                 /*
255                  * Start CAC on a DFS channel.  We come here when starting
256                  * a bss on a DFS channel (see ieee80211_create_ibss).
257                  */
258                 ieee80211_dfs_cac_start(vap);
259                 break;
260         case IEEE80211_S_RUN:
261                 if (vap->iv_flags & IEEE80211_F_WPA) {
262                         /* XXX validate prerequisites */
263                 }
264                 switch (ostate) {
265                 case IEEE80211_S_INIT:
266                         /*
267                          * Already have a channel; bypass the
268                          * scan and startup immediately.
269                          * Note that ieee80211_create_ibss will call
270                          * back to do a RUN->RUN state change.
271                          */
272                         ieee80211_create_ibss(vap,
273                             ieee80211_ht_adjust_channel(ic,
274                                 ic->ic_curchan, vap->iv_flags_ht));
275                         /* NB: iv_bss is changed on return */
276                         break;
277                 case IEEE80211_S_CAC:
278                         /*
279                          * NB: This is the normal state change when CAC
280                          * expires and no radar was detected; no need to
281                          * clear the CAC timer as it's already expired.
282                          */
283                         /* fall thru... */
284                 case IEEE80211_S_CSA:
285                         /*
286                          * Shorten inactivity timer of associated stations
287                          * to weed out sta's that don't follow a CSA.
288                          */
289                         ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
290                         /*
291                          * Update bss node channel to reflect where
292                          * we landed after CSA.
293                          */
294                         ieee80211_node_set_chan(vap->iv_bss,
295                             ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
296                                 ieee80211_htchanflags(vap->iv_bss->ni_chan)));
297                         /* XXX bypass debug msgs */
298                         break;
299                 case IEEE80211_S_SCAN:
300                 case IEEE80211_S_RUN:
301 #ifdef IEEE80211_DEBUG
302                         if (ieee80211_msg_debug(vap)) {
303                                 struct ieee80211_node *ni = vap->iv_bss;
304                                 ieee80211_note(vap,
305                                     "synchronized with %6D ssid ",
306                                     ni->ni_bssid, ":");
307                                 ieee80211_print_essid(ni->ni_essid,
308                                     ni->ni_esslen);
309                                 /* XXX MCS/HT */
310                                 kprintf(" channel %d start %uMb\n",
311                                     ieee80211_chan2ieee(ic, ic->ic_curchan),
312                                     IEEE80211_RATE2MBS(ni->ni_txrate));
313                         }
314 #endif
315                         break;
316                 default:
317                         break;
318                 }
319                 /*
320                  * Start/stop the authenticator.  We delay until here
321                  * to allow configuration to happen out of order.
322                  */
323                 if (vap->iv_auth->ia_attach != NULL) {
324                         /* XXX check failure */
325                         vap->iv_auth->ia_attach(vap);
326                 } else if (vap->iv_auth->ia_detach != NULL) {
327                         vap->iv_auth->ia_detach(vap);
328                 }
329                 ieee80211_node_authorize(vap->iv_bss);
330                 break;
331         case IEEE80211_S_CSA:
332                 if (ostate == IEEE80211_S_RUN && isbandchange(ic)) {
333                         /*
334                          * On a ``band change'' silently drop associated
335                          * stations as they must re-associate before they
336                          * can pass traffic (as otherwise protocol state
337                          * such as capabilities and the negotiated rate
338                          * set may/will be wrong).
339                          */
340                         ieee80211_iterate_nodes(&ic->ic_sta, sta_drop, vap);
341                 }
342                 break;
343         default:
344                 break;
345         }
346         return 0;
347 }
348
349 static void
350 hostap_deliver_data(struct ieee80211vap *vap,
351         struct ieee80211_node *ni, struct mbuf *m)
352 {
353         struct ether_header *eh = mtod(m, struct ether_header *);
354         struct ifnet *ifp = vap->iv_ifp;
355
356         /* clear driver/net80211 flags before passing up */
357         m->m_flags &= ~(M_80211_RX | M_MCAST | M_BCAST);
358
359         KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
360             ("gack, opmode %d", vap->iv_opmode));
361         /*
362          * Do accounting.
363          */
364         ifp->if_ipackets++;
365         IEEE80211_NODE_STAT(ni, rx_data);
366         IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
367         if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
368                 m->m_flags |= M_MCAST;          /* XXX M_BCAST? */
369                 IEEE80211_NODE_STAT(ni, rx_mcast);
370         } else
371                 IEEE80211_NODE_STAT(ni, rx_ucast);
372
373         /* perform as a bridge within the AP */
374         if ((vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0) {
375                 struct mbuf *mcopy = NULL;
376
377                 if (m->m_flags & M_MCAST) {
378                         mcopy = m_dup(m, MB_DONTWAIT);
379                         if (mcopy == NULL)
380                                 ifp->if_oerrors++;
381                         else
382                                 mcopy->m_flags |= M_MCAST;
383                 } else {
384                         /*
385                          * Check if the destination is associated with the
386                          * same vap and authorized to receive traffic.
387                          * Beware of traffic destined for the vap itself;
388                          * sending it will not work; just let it be delivered
389                          * normally.
390                          */
391                         struct ieee80211_node *sta = ieee80211_find_vap_node(
392                              &vap->iv_ic->ic_sta, vap, eh->ether_dhost);
393                         if (sta != NULL) {
394                                 if (ieee80211_node_is_authorized(sta)) {
395                                         /*
396                                          * Beware of sending to ourself; this
397                                          * needs to happen via the normal
398                                          * input path.
399                                          */
400                                         if (sta != vap->iv_bss) {
401                                                 mcopy = m;
402                                                 m = NULL;
403                                         }
404                                 } else {
405                                         vap->iv_stats.is_rx_unauth++;
406                                         IEEE80211_NODE_STAT(sta, rx_unauth);
407                                 }
408                                 ieee80211_free_node(sta);
409                         }
410                 }
411                 if (mcopy != NULL) {
412                         int len, err;
413                         len = mcopy->m_pkthdr.len;
414                         err = ieee80211_handoff(ifp, mcopy);
415                         if (err) {
416                                 /* NB: IFQ_HANDOFF reclaims mcopy */
417                         } else {
418                                 ifp->if_opackets++;
419                         }
420                 }
421         }
422         if (m != NULL) {
423                 /*
424                  * Mark frame as coming from vap's interface.
425                  */
426                 m->m_pkthdr.rcvif = ifp;
427                 if (m->m_flags & M_MCAST) {
428                         /*
429                          * Spam DWDS vap's w/ multicast traffic.
430                          */
431                         /* XXX only if dwds in use? */
432                         ieee80211_dwds_mcast(vap, m);
433                 }
434                 if (ni->ni_vlan != 0) {
435                         /* attach vlan tag */
436                         m->m_pkthdr.ether_vlantag = ni->ni_vlan;
437                         m->m_flags |= M_VLANTAG;
438                 }
439                 ifp->if_input(ifp, m);
440         }
441 }
442
443 /*
444  * Decide if a received management frame should be
445  * printed when debugging is enabled.  This filters some
446  * of the less interesting frames that come frequently
447  * (e.g. beacons).
448  */
449 static __inline int
450 doprint(struct ieee80211vap *vap, int subtype)
451 {
452         switch (subtype) {
453         case IEEE80211_FC0_SUBTYPE_BEACON:
454                 return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
455         case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
456                 return 0;
457         }
458         return 1;
459 }
460
461 /*
462  * Process a received frame.  The node associated with the sender
463  * should be supplied.  If nothing was found in the node table then
464  * the caller is assumed to supply a reference to iv_bss instead.
465  * The RSSI and a timestamp are also supplied.  The RSSI data is used
466  * during AP scanning to select a AP to associate with; it can have
467  * any units so long as values have consistent units and higher values
468  * mean ``better signal''.  The receive timestamp is currently not used
469  * by the 802.11 layer.
470  */
471 static int
472 hostap_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
473 {
474 #define SEQ_LEQ(a,b)    ((int)((a)-(b)) <= 0)
475 #define HAS_SEQ(type)   ((type & 0x4) == 0)
476         struct ieee80211vap *vap = ni->ni_vap;
477         struct ieee80211com *ic = ni->ni_ic;
478         struct ifnet *ifp = vap->iv_ifp;
479         struct ieee80211_frame *wh;
480         struct ieee80211_key *key;
481         struct ether_header *eh;
482         int hdrspace, need_tap = 1;     /* mbuf need to be tapped. */
483         uint8_t dir, type, subtype, qos;
484         uint8_t *bssid;
485         uint16_t rxseq;
486
487         if (m->m_flags & M_AMPDU_MPDU) {
488                 /*
489                  * Fastpath for A-MPDU reorder q resubmission.  Frames
490                  * w/ M_AMPDU_MPDU marked have already passed through
491                  * here but were received out of order and been held on
492                  * the reorder queue.  When resubmitted they are marked
493                  * with the M_AMPDU_MPDU flag and we can bypass most of
494                  * the normal processing.
495                  */
496                 wh = mtod(m, struct ieee80211_frame *);
497                 type = IEEE80211_FC0_TYPE_DATA;
498                 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
499                 subtype = IEEE80211_FC0_SUBTYPE_QOS;
500                 hdrspace = ieee80211_hdrspace(ic, wh);  /* XXX optimize? */
501                 goto resubmit_ampdu;
502         }
503
504         KASSERT(ni != NULL, ("null node"));
505         ni->ni_inact = ni->ni_inact_reload;
506
507         type = -1;                      /* undefined */
508
509         if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
510                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
511                     ni->ni_macaddr, NULL,
512                     "too short (1): len %u", m->m_pkthdr.len);
513                 vap->iv_stats.is_rx_tooshort++;
514                 goto out;
515         }
516         /*
517          * Bit of a cheat here, we use a pointer for a 3-address
518          * frame format but don't reference fields past outside
519          * ieee80211_frame_min w/o first validating the data is
520          * present.
521          */
522         wh = mtod(m, struct ieee80211_frame *);
523
524         if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
525             IEEE80211_FC0_VERSION_0) {
526                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
527                     ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
528                     wh->i_fc[0], wh->i_fc[1]);
529                 vap->iv_stats.is_rx_badversion++;
530                 goto err;
531         }
532
533         dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
534         type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
535         subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
536         if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
537                 if (dir != IEEE80211_FC1_DIR_NODS)
538                         bssid = wh->i_addr1;
539                 else if (type == IEEE80211_FC0_TYPE_CTL)
540                         bssid = wh->i_addr1;
541                 else {
542                         if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
543                                 IEEE80211_DISCARD_MAC(vap,
544                                     IEEE80211_MSG_ANY, ni->ni_macaddr,
545                                     NULL, "too short (2): len %u",
546                                     m->m_pkthdr.len);
547                                 vap->iv_stats.is_rx_tooshort++;
548                                 goto out;
549                         }
550                         bssid = wh->i_addr3;
551                 }
552                 /*
553                  * Validate the bssid.
554                  */
555                 if (!(type == IEEE80211_FC0_TYPE_MGT &&
556                       subtype == IEEE80211_FC0_SUBTYPE_BEACON) &&
557                     !IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
558                     !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
559                         /* not interested in */
560                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
561                             bssid, NULL, "%s", "not to bss");
562                         vap->iv_stats.is_rx_wrongbss++;
563                         goto out;
564                 }
565
566                 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
567                 ni->ni_noise = nf;
568                 if (HAS_SEQ(type)) {
569                         uint8_t tid = ieee80211_gettid(wh);
570                         if (IEEE80211_QOS_HAS_SEQ(wh) &&
571                             TID_TO_WME_AC(tid) >= WME_AC_VI)
572                                 ic->ic_wme.wme_hipri_traffic++;
573                         rxseq = le16toh(*(uint16_t *)wh->i_seq);
574                         if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
575                             (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
576                             SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
577                                 /* duplicate, discard */
578                                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
579                                     bssid, "duplicate",
580                                     "seqno <%u,%u> fragno <%u,%u> tid %u",
581                                     rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
582                                     ni->ni_rxseqs[tid] >>
583                                         IEEE80211_SEQ_SEQ_SHIFT,
584                                     rxseq & IEEE80211_SEQ_FRAG_MASK,
585                                     ni->ni_rxseqs[tid] &
586                                         IEEE80211_SEQ_FRAG_MASK,
587                                     tid);
588                                 vap->iv_stats.is_rx_dup++;
589                                 IEEE80211_NODE_STAT(ni, rx_dup);
590                                 goto out;
591                         }
592                         ni->ni_rxseqs[tid] = rxseq;
593                 }
594         }
595
596         switch (type) {
597         case IEEE80211_FC0_TYPE_DATA:
598                 hdrspace = ieee80211_hdrspace(ic, wh);
599                 if (m->m_len < hdrspace &&
600                     (m = m_pullup(m, hdrspace)) == NULL) {
601                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
602                             ni->ni_macaddr, NULL,
603                             "data too short: expecting %u", hdrspace);
604                         vap->iv_stats.is_rx_tooshort++;
605                         goto out;               /* XXX */
606                 }
607                 if (!(dir == IEEE80211_FC1_DIR_TODS ||
608                      (dir == IEEE80211_FC1_DIR_DSTODS &&
609                       (vap->iv_flags & IEEE80211_F_DWDS)))) {
610                         if (dir != IEEE80211_FC1_DIR_DSTODS) {
611                                 IEEE80211_DISCARD(vap,
612                                     IEEE80211_MSG_INPUT, wh, "data",
613                                     "incorrect dir 0x%x", dir);
614                         } else {
615                                 IEEE80211_DISCARD(vap,
616                                     IEEE80211_MSG_INPUT |
617                                     IEEE80211_MSG_WDS, wh,
618                                     "4-address data",
619                                     "%s", "DWDS not enabled");
620                         }
621                         vap->iv_stats.is_rx_wrongdir++;
622                         goto out;
623                 }
624                 /* check if source STA is associated */
625                 if (ni == vap->iv_bss) {
626                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
627                             wh, "data", "%s", "unknown src");
628                         ieee80211_send_error(ni, wh->i_addr2,
629                             IEEE80211_FC0_SUBTYPE_DEAUTH,
630                             IEEE80211_REASON_NOT_AUTHED);
631                         vap->iv_stats.is_rx_notassoc++;
632                         goto err;
633                 }
634                 if (ni->ni_associd == 0) {
635                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
636                             wh, "data", "%s", "unassoc src");
637                         IEEE80211_SEND_MGMT(ni,
638                             IEEE80211_FC0_SUBTYPE_DISASSOC,
639                             IEEE80211_REASON_NOT_ASSOCED);
640                         vap->iv_stats.is_rx_notassoc++;
641                         goto err;
642                 }
643
644                 /*
645                  * Check for power save state change.
646                  * XXX out-of-order A-MPDU frames?
647                  */
648                 if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
649                     (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
650                         ieee80211_node_pwrsave(ni,
651                                 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
652                 /*
653                  * For 4-address packets handle WDS discovery
654                  * notifications.  Once a WDS link is setup frames
655                  * are just delivered to the WDS vap (see below).
656                  */
657                 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap == NULL) {
658                         if (!ieee80211_node_is_authorized(ni)) {
659                                 IEEE80211_DISCARD(vap,
660                                     IEEE80211_MSG_INPUT |
661                                     IEEE80211_MSG_WDS, wh,
662                                     "4-address data",
663                                     "%s", "unauthorized port");
664                                 vap->iv_stats.is_rx_unauth++;
665                                 IEEE80211_NODE_STAT(ni, rx_unauth);
666                                 goto err;
667                         }
668                         ieee80211_dwds_discover(ni, m);
669                         return type;
670                 }
671
672                 /*
673                  * Handle A-MPDU re-ordering.  If the frame is to be
674                  * processed directly then ieee80211_ampdu_reorder
675                  * will return 0; otherwise it has consumed the mbuf
676                  * and we should do nothing more with it.
677                  */
678                 if ((m->m_flags & M_AMPDU) &&
679                     ieee80211_ampdu_reorder(ni, m) != 0) {
680                         m = NULL;
681                         goto out;
682                 }
683         resubmit_ampdu:
684
685                 /*
686                  * Handle privacy requirements.  Note that we
687                  * must not be preempted from here until after
688                  * we (potentially) call ieee80211_crypto_demic;
689                  * otherwise we may violate assumptions in the
690                  * crypto cipher modules used to do delayed update
691                  * of replay sequence numbers.
692                  */
693                 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
694                         if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
695                                 /*
696                                  * Discard encrypted frames when privacy is off.
697                                  */
698                                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
699                                     wh, "WEP", "%s", "PRIVACY off");
700                                 vap->iv_stats.is_rx_noprivacy++;
701                                 IEEE80211_NODE_STAT(ni, rx_noprivacy);
702                                 goto out;
703                         }
704                         key = ieee80211_crypto_decap(ni, m, hdrspace);
705                         if (key == NULL) {
706                                 /* NB: stats+msgs handled in crypto_decap */
707                                 IEEE80211_NODE_STAT(ni, rx_wepfail);
708                                 goto out;
709                         }
710                         wh = mtod(m, struct ieee80211_frame *);
711                         wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
712                 } else {
713                         /* XXX M_WEP and IEEE80211_F_PRIVACY */
714                         key = NULL;
715                 }
716
717                 /*
718                  * Save QoS bits for use below--before we strip the header.
719                  */
720                 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
721                         qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
722                             ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
723                             ((struct ieee80211_qosframe *)wh)->i_qos[0];
724                 } else
725                         qos = 0;
726
727                 /*
728                  * Next up, any fragmentation.
729                  */
730                 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
731                         m = ieee80211_defrag(ni, m, hdrspace);
732                         if (m == NULL) {
733                                 /* Fragment dropped or frame not complete yet */
734                                 goto out;
735                         }
736                 }
737                 wh = NULL;              /* no longer valid, catch any uses */
738
739                 /*
740                  * Next strip any MSDU crypto bits.
741                  */
742                 if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
743                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
744                             ni->ni_macaddr, "data", "%s", "demic error");
745                         vap->iv_stats.is_rx_demicfail++;
746                         IEEE80211_NODE_STAT(ni, rx_demicfail);
747                         goto out;
748                 }
749                 /* copy to listener after decrypt */
750                 if (ieee80211_radiotap_active_vap(vap))
751                         ieee80211_radiotap_rx(vap, m);
752                 need_tap = 0;
753                 /*
754                  * Finally, strip the 802.11 header.
755                  */
756                 m = ieee80211_decap(vap, m, hdrspace);
757                 if (m == NULL) {
758                         /* XXX mask bit to check for both */
759                         /* don't count Null data frames as errors */
760                         if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
761                             subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
762                                 goto out;
763                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
764                             ni->ni_macaddr, "data", "%s", "decap error");
765                         vap->iv_stats.is_rx_decap++;
766                         IEEE80211_NODE_STAT(ni, rx_decap);
767                         goto err;
768                 }
769                 eh = mtod(m, struct ether_header *);
770                 if (!ieee80211_node_is_authorized(ni)) {
771                         /*
772                          * Deny any non-PAE frames received prior to
773                          * authorization.  For open/shared-key
774                          * authentication the port is mark authorized
775                          * after authentication completes.  For 802.1x
776                          * the port is not marked authorized by the
777                          * authenticator until the handshake has completed.
778                          */
779                         if (eh->ether_type != htons(ETHERTYPE_PAE)) {
780                                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
781                                     eh->ether_shost, "data",
782                                     "unauthorized port: ether type 0x%x len %u",
783                                     eh->ether_type, m->m_pkthdr.len);
784                                 vap->iv_stats.is_rx_unauth++;
785                                 IEEE80211_NODE_STAT(ni, rx_unauth);
786                                 goto err;
787                         }
788                 } else {
789                         /*
790                          * When denying unencrypted frames, discard
791                          * any non-PAE frames received without encryption.
792                          */
793                         if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
794                             (key == NULL && (m->m_flags & M_WEP) == 0) &&
795                             eh->ether_type != htons(ETHERTYPE_PAE)) {
796                                 /*
797                                  * Drop unencrypted frames.
798                                  */
799                                 vap->iv_stats.is_rx_unencrypted++;
800                                 IEEE80211_NODE_STAT(ni, rx_unencrypted);
801                                 goto out;
802                         }
803                 }
804                 /* XXX require HT? */
805                 if (qos & IEEE80211_QOS_AMSDU) {
806                         m = ieee80211_decap_amsdu(ni, m);
807                         if (m == NULL)
808                                 return IEEE80211_FC0_TYPE_DATA;
809                 } else {
810 #ifdef IEEE80211_SUPPORT_SUPERG
811                         m = ieee80211_decap_fastframe(vap, ni, m);
812                         if (m == NULL)
813                                 return IEEE80211_FC0_TYPE_DATA;
814 #endif
815                 }
816                 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
817                         ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
818                 else
819                         hostap_deliver_data(vap, ni, m);
820                 return IEEE80211_FC0_TYPE_DATA;
821
822         case IEEE80211_FC0_TYPE_MGT:
823                 vap->iv_stats.is_rx_mgmt++;
824                 IEEE80211_NODE_STAT(ni, rx_mgmt);
825                 if (dir != IEEE80211_FC1_DIR_NODS) {
826                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
827                             wh, "mgt", "incorrect dir 0x%x", dir);
828                         vap->iv_stats.is_rx_wrongdir++;
829                         goto err;
830                 }
831                 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
832                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
833                             ni->ni_macaddr, "mgt", "too short: len %u",
834                             m->m_pkthdr.len);
835                         vap->iv_stats.is_rx_tooshort++;
836                         goto out;
837                 }
838                 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
839                         /* ensure return frames are unicast */
840                         IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
841                             wh, NULL, "source is multicast: %6D",
842                             wh->i_addr2, ":");
843                         vap->iv_stats.is_rx_mgtdiscard++;       /* XXX stat */
844                         goto out;
845                 }
846 #ifdef IEEE80211_DEBUG
847                 if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
848                     ieee80211_msg_dumppkts(vap)) {
849                         if_printf(ifp, "received %s from %6D rssi %d\n",
850                             ieee80211_mgt_subtype_name[subtype >>
851                                 IEEE80211_FC0_SUBTYPE_SHIFT],
852                             wh->i_addr2, ":", rssi);
853                 }
854 #endif
855                 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
856                         if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
857                                 /*
858                                  * Only shared key auth frames with a challenge
859                                  * should be encrypted, discard all others.
860                                  */
861                                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
862                                     wh, NULL,
863                                     "%s", "WEP set but not permitted");
864                                 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
865                                 goto out;
866                         }
867                         if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
868                                 /*
869                                  * Discard encrypted frames when privacy is off.
870                                  */
871                                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
872                                     wh, NULL, "%s", "WEP set but PRIVACY off");
873                                 vap->iv_stats.is_rx_noprivacy++;
874                                 goto out;
875                         }
876                         hdrspace = ieee80211_hdrspace(ic, wh);
877                         key = ieee80211_crypto_decap(ni, m, hdrspace);
878                         if (key == NULL) {
879                                 /* NB: stats+msgs handled in crypto_decap */
880                                 goto out;
881                         }
882                         wh = mtod(m, struct ieee80211_frame *);
883                         wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
884                 }
885                 vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
886                 goto out;
887
888         case IEEE80211_FC0_TYPE_CTL:
889                 vap->iv_stats.is_rx_ctl++;
890                 IEEE80211_NODE_STAT(ni, rx_ctrl);
891                 vap->iv_recv_ctl(ni, m, subtype);
892                 goto out;
893         default:
894                 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
895                     wh, "bad", "frame type 0x%x", type);
896                 /* should not come here */
897                 break;
898         }
899 err:
900         ifp->if_ierrors++;
901 out:
902         if (m != NULL) {
903                 if (need_tap && ieee80211_radiotap_active_vap(vap))
904                         ieee80211_radiotap_rx(vap, m);
905                 m_freem(m);
906         }
907         return type;
908 #undef SEQ_LEQ
909 }
910
911 static void
912 hostap_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
913     int rssi, int nf, uint16_t seq, uint16_t status)
914 {
915         struct ieee80211vap *vap = ni->ni_vap;
916
917         KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
918
919         if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
920                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
921                     ni->ni_macaddr, "open auth",
922                     "bad sta auth mode %u", ni->ni_authmode);
923                 vap->iv_stats.is_rx_bad_auth++; /* XXX */
924                 /*
925                  * Clear any challenge text that may be there if
926                  * a previous shared key auth failed and then an
927                  * open auth is attempted.
928                  */
929                 if (ni->ni_challenge != NULL) {
930                         kfree(ni->ni_challenge, M_80211_NODE);
931                         ni->ni_challenge = NULL;
932                 }
933                 /* XXX hack to workaround calling convention */
934                 ieee80211_send_error(ni, wh->i_addr2, 
935                     IEEE80211_FC0_SUBTYPE_AUTH,
936                     (seq + 1) | (IEEE80211_STATUS_ALG<<16));
937                 return;
938         }
939         if (seq != IEEE80211_AUTH_OPEN_REQUEST) {
940                 vap->iv_stats.is_rx_bad_auth++;
941                 return;
942         }
943         /* always accept open authentication requests */
944         if (ni == vap->iv_bss) {
945                 ni = ieee80211_dup_bss(vap, wh->i_addr2);
946                 if (ni == NULL)
947                         return;
948         } else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
949                 (void) ieee80211_ref_node(ni);
950         /*
951          * Mark the node as referenced to reflect that it's
952          * reference count has been bumped to insure it remains
953          * after the transaction completes.
954          */
955         ni->ni_flags |= IEEE80211_NODE_AREF;
956         /*
957          * Mark the node as requiring a valid association id
958          * before outbound traffic is permitted.
959          */
960         ni->ni_flags |= IEEE80211_NODE_ASSOCID;
961
962         if (vap->iv_acl != NULL &&
963             vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
964                 /*
965                  * When the ACL policy is set to RADIUS we defer the
966                  * authorization to a user agent.  Dispatch an event,
967                  * a subsequent MLME call will decide the fate of the
968                  * station.  If the user agent is not present then the
969                  * node will be reclaimed due to inactivity.
970                  */
971                 IEEE80211_NOTE_MAC(vap,
972                     IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL, ni->ni_macaddr,
973                     "%s", "station authentication defered (radius acl)");
974                 ieee80211_notify_node_auth(ni);
975         } else {
976                 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
977                 IEEE80211_NOTE_MAC(vap,
978                     IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni->ni_macaddr,
979                     "%s", "station authenticated (open)");
980                 /*
981                  * When 802.1x is not in use mark the port
982                  * authorized at this point so traffic can flow.
983                  */
984                 if (ni->ni_authmode != IEEE80211_AUTH_8021X)
985                         ieee80211_node_authorize(ni);
986         }
987 }
988
989 static void
990 hostap_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
991     uint8_t *frm, uint8_t *efrm, int rssi, int nf,
992     uint16_t seq, uint16_t status)
993 {
994         struct ieee80211vap *vap = ni->ni_vap;
995         uint8_t *challenge;
996         int allocbs, estatus;
997
998         KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
999
1000         /*
1001          * NB: this can happen as we allow pre-shared key
1002          * authentication to be enabled w/o wep being turned
1003          * on so that configuration of these can be done
1004          * in any order.  It may be better to enforce the
1005          * ordering in which case this check would just be
1006          * for sanity/consistency.
1007          */
1008         if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
1009                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1010                     ni->ni_macaddr, "shared key auth",
1011                     "%s", " PRIVACY is disabled");
1012                 estatus = IEEE80211_STATUS_ALG;
1013                 goto bad;
1014         }
1015         /*
1016          * Pre-shared key authentication is evil; accept
1017          * it only if explicitly configured (it is supported
1018          * mainly for compatibility with clients like Mac OS X).
1019          */
1020         if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1021             ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1022                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1023                     ni->ni_macaddr, "shared key auth",
1024                     "bad sta auth mode %u", ni->ni_authmode);
1025                 vap->iv_stats.is_rx_bad_auth++; /* XXX maybe a unique error? */
1026                 estatus = IEEE80211_STATUS_ALG;
1027                 goto bad;
1028         }
1029
1030         challenge = NULL;
1031         if (frm + 1 < efrm) {
1032                 if ((frm[1] + 2) > (efrm - frm)) {
1033                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1034                             ni->ni_macaddr, "shared key auth",
1035                             "ie %d/%d too long",
1036                             frm[0], (frm[1] + 2) - (efrm - frm));
1037                         vap->iv_stats.is_rx_bad_auth++;
1038                         estatus = IEEE80211_STATUS_CHALLENGE;
1039                         goto bad;
1040                 }
1041                 if (*frm == IEEE80211_ELEMID_CHALLENGE)
1042                         challenge = frm;
1043                 frm += frm[1] + 2;
1044         }
1045         switch (seq) {
1046         case IEEE80211_AUTH_SHARED_CHALLENGE:
1047         case IEEE80211_AUTH_SHARED_RESPONSE:
1048                 if (challenge == NULL) {
1049                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1050                             ni->ni_macaddr, "shared key auth",
1051                             "%s", "no challenge");
1052                         vap->iv_stats.is_rx_bad_auth++;
1053                         estatus = IEEE80211_STATUS_CHALLENGE;
1054                         goto bad;
1055                 }
1056                 if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1057                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1058                             ni->ni_macaddr, "shared key auth",
1059                             "bad challenge len %d", challenge[1]);
1060                         vap->iv_stats.is_rx_bad_auth++;
1061                         estatus = IEEE80211_STATUS_CHALLENGE;
1062                         goto bad;
1063                 }
1064         default:
1065                 break;
1066         }
1067         switch (seq) {
1068         case IEEE80211_AUTH_SHARED_REQUEST:
1069                 if (ni == vap->iv_bss) {
1070                         ni = ieee80211_dup_bss(vap, wh->i_addr2);
1071                         if (ni == NULL) {
1072                                 /* NB: no way to return an error */
1073                                 return;
1074                         }
1075                         allocbs = 1;
1076                 } else {
1077                         if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1078                                 (void) ieee80211_ref_node(ni);
1079                         allocbs = 0;
1080                 }
1081                 /*
1082                  * Mark the node as referenced to reflect that it's
1083                  * reference count has been bumped to insure it remains
1084                  * after the transaction completes.
1085                  */
1086                 ni->ni_flags |= IEEE80211_NODE_AREF;
1087                 /*
1088                  * Mark the node as requiring a valid associatio id
1089                  * before outbound traffic is permitted.
1090                  */
1091                 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
1092                 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1093                 ni->ni_noise = nf;
1094                 if (!ieee80211_alloc_challenge(ni)) {
1095                         /* NB: don't return error so they rexmit */
1096                         return;
1097                 }
1098                 get_random_bytes(ni->ni_challenge,
1099                         IEEE80211_CHALLENGE_LEN);
1100                 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1101                     ni, "shared key %sauth request", allocbs ? "" : "re");
1102                 /*
1103                  * When the ACL policy is set to RADIUS we defer the
1104                  * authorization to a user agent.  Dispatch an event,
1105                  * a subsequent MLME call will decide the fate of the
1106                  * station.  If the user agent is not present then the
1107                  * node will be reclaimed due to inactivity.
1108                  */
1109                 if (vap->iv_acl != NULL &&
1110                     vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
1111                         IEEE80211_NOTE_MAC(vap,
1112                             IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL,
1113                             ni->ni_macaddr,
1114                             "%s", "station authentication defered (radius acl)");
1115                         ieee80211_notify_node_auth(ni);
1116                         return;
1117                 }
1118                 break;
1119         case IEEE80211_AUTH_SHARED_RESPONSE:
1120                 if (ni == vap->iv_bss) {
1121                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1122                             ni->ni_macaddr, "shared key response",
1123                             "%s", "unknown station");
1124                         /* NB: don't send a response */
1125                         return;
1126                 }
1127                 if (ni->ni_challenge == NULL) {
1128                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1129                             ni->ni_macaddr, "shared key response",
1130                             "%s", "no challenge recorded");
1131                         vap->iv_stats.is_rx_bad_auth++;
1132                         estatus = IEEE80211_STATUS_CHALLENGE;
1133                         goto bad;
1134                 }
1135                 if (memcmp(ni->ni_challenge, &challenge[2],
1136                            challenge[1]) != 0) {
1137                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1138                             ni->ni_macaddr, "shared key response",
1139                             "%s", "challenge mismatch");
1140                         vap->iv_stats.is_rx_auth_fail++;
1141                         estatus = IEEE80211_STATUS_CHALLENGE;
1142                         goto bad;
1143                 }
1144                 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1145                     ni, "%s", "station authenticated (shared key)");
1146                 ieee80211_node_authorize(ni);
1147                 break;
1148         default:
1149                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1150                     ni->ni_macaddr, "shared key auth",
1151                     "bad seq %d", seq);
1152                 vap->iv_stats.is_rx_bad_auth++;
1153                 estatus = IEEE80211_STATUS_SEQUENCE;
1154                 goto bad;
1155         }
1156         IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1157         return;
1158 bad:
1159         /*
1160          * Send an error response; but only when operating as an AP.
1161          */
1162         /* XXX hack to workaround calling convention */
1163         ieee80211_send_error(ni, wh->i_addr2,
1164             IEEE80211_FC0_SUBTYPE_AUTH,
1165             (seq + 1) | (estatus<<16));
1166 }
1167
1168 /*
1169  * Convert a WPA cipher selector OUI to an internal
1170  * cipher algorithm.  Where appropriate we also
1171  * record any key length.
1172  */
1173 static int
1174 wpa_cipher(const uint8_t *sel, uint8_t *keylen)
1175 {
1176 #define WPA_SEL(x)      (((x)<<24)|WPA_OUI)
1177         uint32_t w = LE_READ_4(sel);
1178
1179         switch (w) {
1180         case WPA_SEL(WPA_CSE_NULL):
1181                 return IEEE80211_CIPHER_NONE;
1182         case WPA_SEL(WPA_CSE_WEP40):
1183                 if (keylen)
1184                         *keylen = 40 / NBBY;
1185                 return IEEE80211_CIPHER_WEP;
1186         case WPA_SEL(WPA_CSE_WEP104):
1187                 if (keylen)
1188                         *keylen = 104 / NBBY;
1189                 return IEEE80211_CIPHER_WEP;
1190         case WPA_SEL(WPA_CSE_TKIP):
1191                 return IEEE80211_CIPHER_TKIP;
1192         case WPA_SEL(WPA_CSE_CCMP):
1193                 return IEEE80211_CIPHER_AES_CCM;
1194         }
1195         return 32;              /* NB: so 1<< is discarded */
1196 #undef WPA_SEL
1197 }
1198
1199 /*
1200  * Convert a WPA key management/authentication algorithm
1201  * to an internal code.
1202  */
1203 static int
1204 wpa_keymgmt(const uint8_t *sel)
1205 {
1206 #define WPA_SEL(x)      (((x)<<24)|WPA_OUI)
1207         uint32_t w = LE_READ_4(sel);
1208
1209         switch (w) {
1210         case WPA_SEL(WPA_ASE_8021X_UNSPEC):
1211                 return WPA_ASE_8021X_UNSPEC;
1212         case WPA_SEL(WPA_ASE_8021X_PSK):
1213                 return WPA_ASE_8021X_PSK;
1214         case WPA_SEL(WPA_ASE_NONE):
1215                 return WPA_ASE_NONE;
1216         }
1217         return 0;               /* NB: so is discarded */
1218 #undef WPA_SEL
1219 }
1220
1221 /*
1222  * Parse a WPA information element to collect parameters.
1223  * Note that we do not validate security parameters; that
1224  * is handled by the authenticator; the parsing done here
1225  * is just for internal use in making operational decisions.
1226  */
1227 static int
1228 ieee80211_parse_wpa(struct ieee80211vap *vap, const uint8_t *frm,
1229         struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1230 {
1231         uint8_t len = frm[1];
1232         uint32_t w;
1233         int n;
1234
1235         /*
1236          * Check the length once for fixed parts: OUI, type,
1237          * version, mcast cipher, and 2 selector counts.
1238          * Other, variable-length data, must be checked separately.
1239          */
1240         if ((vap->iv_flags & IEEE80211_F_WPA1) == 0) {
1241                 IEEE80211_DISCARD_IE(vap,
1242                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1243                     wh, "WPA", "not WPA, flags 0x%x", vap->iv_flags);
1244                 return IEEE80211_REASON_IE_INVALID;
1245         }
1246         if (len < 14) {
1247                 IEEE80211_DISCARD_IE(vap,
1248                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1249                     wh, "WPA", "too short, len %u", len);
1250                 return IEEE80211_REASON_IE_INVALID;
1251         }
1252         frm += 6, len -= 4;             /* NB: len is payload only */
1253         /* NB: iswpaoui already validated the OUI and type */
1254         w = LE_READ_2(frm);
1255         if (w != WPA_VERSION) {
1256                 IEEE80211_DISCARD_IE(vap,
1257                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1258                     wh, "WPA", "bad version %u", w);
1259                 return IEEE80211_REASON_IE_INVALID;
1260         }
1261         frm += 2, len -= 2;
1262
1263         memset(rsn, 0, sizeof(*rsn));
1264
1265         /* multicast/group cipher */
1266         rsn->rsn_mcastcipher = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
1267         frm += 4, len -= 4;
1268
1269         /* unicast ciphers */
1270         n = LE_READ_2(frm);
1271         frm += 2, len -= 2;
1272         if (len < n*4+2) {
1273                 IEEE80211_DISCARD_IE(vap,
1274                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1275                     wh, "WPA", "ucast cipher data too short; len %u, n %u",
1276                     len, n);
1277                 return IEEE80211_REASON_IE_INVALID;
1278         }
1279         w = 0;
1280         for (; n > 0; n--) {
1281                 w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
1282                 frm += 4, len -= 4;
1283         }
1284         if (w & (1<<IEEE80211_CIPHER_TKIP))
1285                 rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1286         else
1287                 rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1288
1289         /* key management algorithms */
1290         n = LE_READ_2(frm);
1291         frm += 2, len -= 2;
1292         if (len < n*4) {
1293                 IEEE80211_DISCARD_IE(vap,
1294                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1295                     wh, "WPA", "key mgmt alg data too short; len %u, n %u",
1296                     len, n);
1297                 return IEEE80211_REASON_IE_INVALID;
1298         }
1299         w = 0;
1300         for (; n > 0; n--) {
1301                 w |= wpa_keymgmt(frm);
1302                 frm += 4, len -= 4;
1303         }
1304         if (w & WPA_ASE_8021X_UNSPEC)
1305                 rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
1306         else
1307                 rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1308
1309         if (len > 2)            /* optional capabilities */
1310                 rsn->rsn_caps = LE_READ_2(frm);
1311
1312         return 0;
1313 }
1314
1315 /*
1316  * Convert an RSN cipher selector OUI to an internal
1317  * cipher algorithm.  Where appropriate we also
1318  * record any key length.
1319  */
1320 static int
1321 rsn_cipher(const uint8_t *sel, uint8_t *keylen)
1322 {
1323 #define RSN_SEL(x)      (((x)<<24)|RSN_OUI)
1324         uint32_t w = LE_READ_4(sel);
1325
1326         switch (w) {
1327         case RSN_SEL(RSN_CSE_NULL):
1328                 return IEEE80211_CIPHER_NONE;
1329         case RSN_SEL(RSN_CSE_WEP40):
1330                 if (keylen)
1331                         *keylen = 40 / NBBY;
1332                 return IEEE80211_CIPHER_WEP;
1333         case RSN_SEL(RSN_CSE_WEP104):
1334                 if (keylen)
1335                         *keylen = 104 / NBBY;
1336                 return IEEE80211_CIPHER_WEP;
1337         case RSN_SEL(RSN_CSE_TKIP):
1338                 return IEEE80211_CIPHER_TKIP;
1339         case RSN_SEL(RSN_CSE_CCMP):
1340                 return IEEE80211_CIPHER_AES_CCM;
1341         case RSN_SEL(RSN_CSE_WRAP):
1342                 return IEEE80211_CIPHER_AES_OCB;
1343         }
1344         return 32;              /* NB: so 1<< is discarded */
1345 #undef WPA_SEL
1346 }
1347
1348 /*
1349  * Convert an RSN key management/authentication algorithm
1350  * to an internal code.
1351  */
1352 static int
1353 rsn_keymgmt(const uint8_t *sel)
1354 {
1355 #define RSN_SEL(x)      (((x)<<24)|RSN_OUI)
1356         uint32_t w = LE_READ_4(sel);
1357
1358         switch (w) {
1359         case RSN_SEL(RSN_ASE_8021X_UNSPEC):
1360                 return RSN_ASE_8021X_UNSPEC;
1361         case RSN_SEL(RSN_ASE_8021X_PSK):
1362                 return RSN_ASE_8021X_PSK;
1363         case RSN_SEL(RSN_ASE_NONE):
1364                 return RSN_ASE_NONE;
1365         }
1366         return 0;               /* NB: so is discarded */
1367 #undef RSN_SEL
1368 }
1369
1370 /*
1371  * Parse a WPA/RSN information element to collect parameters
1372  * and validate the parameters against what has been
1373  * configured for the system.
1374  */
1375 static int
1376 ieee80211_parse_rsn(struct ieee80211vap *vap, const uint8_t *frm,
1377         struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1378 {
1379         uint8_t len = frm[1];
1380         uint32_t w;
1381         int n;
1382
1383         /*
1384          * Check the length once for fixed parts: 
1385          * version, mcast cipher, and 2 selector counts.
1386          * Other, variable-length data, must be checked separately.
1387          */
1388         if ((vap->iv_flags & IEEE80211_F_WPA2) == 0) {
1389                 IEEE80211_DISCARD_IE(vap,
1390                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1391                     wh, "WPA", "not RSN, flags 0x%x", vap->iv_flags);
1392                 return IEEE80211_REASON_IE_INVALID;
1393         }
1394         if (len < 10) {
1395                 IEEE80211_DISCARD_IE(vap,
1396                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1397                     wh, "RSN", "too short, len %u", len);
1398                 return IEEE80211_REASON_IE_INVALID;
1399         }
1400         frm += 2;
1401         w = LE_READ_2(frm);
1402         if (w != RSN_VERSION) {
1403                 IEEE80211_DISCARD_IE(vap,
1404                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1405                     wh, "RSN", "bad version %u", w);
1406                 return IEEE80211_REASON_IE_INVALID;
1407         }
1408         frm += 2, len -= 2;
1409
1410         memset(rsn, 0, sizeof(*rsn));
1411
1412         /* multicast/group cipher */
1413         rsn->rsn_mcastcipher = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
1414         frm += 4, len -= 4;
1415
1416         /* unicast ciphers */
1417         n = LE_READ_2(frm);
1418         frm += 2, len -= 2;
1419         if (len < n*4+2) {
1420                 IEEE80211_DISCARD_IE(vap,
1421                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1422                     wh, "RSN", "ucast cipher data too short; len %u, n %u",
1423                     len, n);
1424                 return IEEE80211_REASON_IE_INVALID;
1425         }
1426         w = 0;
1427         for (; n > 0; n--) {
1428                 w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
1429                 frm += 4, len -= 4;
1430         }
1431         if (w & (1<<IEEE80211_CIPHER_TKIP))
1432                 rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1433         else
1434                 rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1435
1436         /* key management algorithms */
1437         n = LE_READ_2(frm);
1438         frm += 2, len -= 2;
1439         if (len < n*4) {
1440                 IEEE80211_DISCARD_IE(vap,
1441                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1442                     wh, "RSN", "key mgmt alg data too short; len %u, n %u",
1443                     len, n);
1444                 return IEEE80211_REASON_IE_INVALID;
1445         }
1446         w = 0;
1447         for (; n > 0; n--) {
1448                 w |= rsn_keymgmt(frm);
1449                 frm += 4, len -= 4;
1450         }
1451         if (w & RSN_ASE_8021X_UNSPEC)
1452                 rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
1453         else
1454                 rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
1455
1456         /* optional RSN capabilities */
1457         if (len > 2)
1458                 rsn->rsn_caps = LE_READ_2(frm);
1459         /* XXXPMKID */
1460
1461         return 0;
1462 }
1463
1464 /*
1465  * WPA/802.11i assocation request processing.
1466  */
1467 static int
1468 wpa_assocreq(struct ieee80211_node *ni, struct ieee80211_rsnparms *rsnparms,
1469         const struct ieee80211_frame *wh, const uint8_t *wpa,
1470         const uint8_t *rsn, uint16_t capinfo)
1471 {
1472         struct ieee80211vap *vap = ni->ni_vap;
1473         uint8_t reason;
1474         int badwparsn;
1475
1476         ni->ni_flags &= ~(IEEE80211_NODE_WPS|IEEE80211_NODE_TSN);
1477         if (wpa == NULL && rsn == NULL) {
1478                 if (vap->iv_flags_ext & IEEE80211_FEXT_WPS) {
1479                         /*
1480                          * W-Fi Protected Setup (WPS) permits
1481                          * clients to associate and pass EAPOL frames
1482                          * to establish initial credentials.
1483                          */
1484                         ni->ni_flags |= IEEE80211_NODE_WPS;
1485                         return 1;
1486                 }
1487                 if ((vap->iv_flags_ext & IEEE80211_FEXT_TSN) &&
1488                     (capinfo & IEEE80211_CAPINFO_PRIVACY)) {
1489                         /* 
1490                          * Transitional Security Network.  Permits clients
1491                          * to associate and use WEP while WPA is configured.
1492                          */
1493                         ni->ni_flags |= IEEE80211_NODE_TSN;
1494                         return 1;
1495                 }
1496                 IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1497                     wh, NULL, "%s", "no WPA/RSN IE in association request");
1498                 vap->iv_stats.is_rx_assoc_badwpaie++;
1499                 reason = IEEE80211_REASON_IE_INVALID;
1500                 goto bad;
1501         }
1502         /* assert right association security credentials */
1503         badwparsn = 0;                  /* NB: to silence compiler */
1504         switch (vap->iv_flags & IEEE80211_F_WPA) {
1505         case IEEE80211_F_WPA1:
1506                 badwparsn = (wpa == NULL);
1507                 break;
1508         case IEEE80211_F_WPA2:
1509                 badwparsn = (rsn == NULL);
1510                 break;
1511         case IEEE80211_F_WPA1|IEEE80211_F_WPA2:
1512                 badwparsn = (wpa == NULL && rsn == NULL);
1513                 break;
1514         }
1515         if (badwparsn) {
1516                 IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1517                     wh, NULL,
1518                     "%s", "missing WPA/RSN IE in association request");
1519                 vap->iv_stats.is_rx_assoc_badwpaie++;
1520                 reason = IEEE80211_REASON_IE_INVALID;
1521                 goto bad;
1522         }
1523         /*
1524          * Parse WPA/RSN information element.
1525          */
1526         if (wpa != NULL)
1527                 reason = ieee80211_parse_wpa(vap, wpa, rsnparms, wh);
1528         else
1529                 reason = ieee80211_parse_rsn(vap, rsn, rsnparms, wh);
1530         if (reason != 0) {
1531                 /* XXX distinguish WPA/RSN? */
1532                 vap->iv_stats.is_rx_assoc_badwpaie++;
1533                 goto bad;
1534         }
1535         IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA, ni,
1536             "%s ie: mc %u/%u uc %u/%u key %u caps 0x%x",
1537             wpa != NULL ? "WPA" : "RSN",
1538             rsnparms->rsn_mcastcipher, rsnparms->rsn_mcastkeylen,
1539             rsnparms->rsn_ucastcipher, rsnparms->rsn_ucastkeylen,
1540             rsnparms->rsn_keymgmt, rsnparms->rsn_caps);
1541
1542         return 1;
1543 bad:
1544         ieee80211_node_deauth(ni, reason);
1545         return 0;
1546 }
1547
1548 /* XXX find a better place for definition */
1549 struct l2_update_frame {
1550         struct ether_header eh;
1551         uint8_t dsap;
1552         uint8_t ssap;
1553         uint8_t control;
1554         uint8_t xid[3];
1555 }  __packed;
1556
1557 /*
1558  * Deliver a TGf L2UF frame on behalf of a station.
1559  * This primes any bridge when the station is roaming
1560  * between ap's on the same wired network.
1561  */
1562 static void
1563 ieee80211_deliver_l2uf(struct ieee80211_node *ni)
1564 {
1565         struct ieee80211vap *vap = ni->ni_vap;
1566         struct ifnet *ifp = vap->iv_ifp;
1567         struct mbuf *m;
1568         struct l2_update_frame *l2uf;
1569         struct ether_header *eh;
1570         
1571         m = m_gethdr(MB_DONTWAIT, MT_DATA);
1572         if (m == NULL) {
1573                 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1574                     "%s", "no mbuf for l2uf frame");
1575                 vap->iv_stats.is_rx_nobuf++;    /* XXX not right */
1576                 return;
1577         }
1578         l2uf = mtod(m, struct l2_update_frame *);
1579         eh = &l2uf->eh;
1580         /* dst: Broadcast address */
1581         IEEE80211_ADDR_COPY(eh->ether_dhost, ifp->if_broadcastaddr);
1582         /* src: associated STA */
1583         IEEE80211_ADDR_COPY(eh->ether_shost, ni->ni_macaddr);
1584         eh->ether_type = htons(sizeof(*l2uf) - sizeof(*eh));
1585         
1586         l2uf->dsap = 0;
1587         l2uf->ssap = 0;
1588         l2uf->control = 0xf5;
1589         l2uf->xid[0] = 0x81;
1590         l2uf->xid[1] = 0x80;
1591         l2uf->xid[2] = 0x00;
1592         
1593         m->m_pkthdr.len = m->m_len = sizeof(*l2uf);
1594         hostap_deliver_data(vap, ni, m);
1595 }
1596
1597 static void
1598 ratesetmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1599         int reassoc, int resp, const char *tag, int rate)
1600 {
1601         IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1602             "deny %s request, %s rate set mismatch, rate/MCS %d",
1603             reassoc ? "reassoc" : "assoc", tag, rate & IEEE80211_RATE_VAL);
1604         IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_BASIC_RATE);
1605         ieee80211_node_leave(ni);
1606 }
1607
1608 static void
1609 capinfomismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1610         int reassoc, int resp, const char *tag, int capinfo)
1611 {
1612         struct ieee80211vap *vap = ni->ni_vap;
1613
1614         IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1615             "deny %s request, %s mismatch 0x%x",
1616             reassoc ? "reassoc" : "assoc", tag, capinfo);
1617         IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_CAPINFO);
1618         ieee80211_node_leave(ni);
1619         vap->iv_stats.is_rx_assoc_capmismatch++;
1620 }
1621
1622 static void
1623 htcapmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1624         int reassoc, int resp)
1625 {
1626         IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1627             "deny %s request, %s missing HT ie", reassoc ? "reassoc" : "assoc");
1628         /* XXX no better code */
1629         IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_MISSING_HT_CAPS);
1630         ieee80211_node_leave(ni);
1631 }
1632
1633 static void
1634 authalgreject(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1635         int algo, int seq, int status)
1636 {
1637         struct ieee80211vap *vap = ni->ni_vap;
1638
1639         IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1640             wh, NULL, "unsupported alg %d", algo);
1641         vap->iv_stats.is_rx_auth_unsupported++;
1642         ieee80211_send_error(ni, wh->i_addr2, IEEE80211_FC0_SUBTYPE_AUTH,
1643             seq | (status << 16));
1644 }
1645
1646 static __inline int
1647 ishtmixed(const uint8_t *ie)
1648 {
1649         const struct ieee80211_ie_htinfo *ht =
1650             (const struct ieee80211_ie_htinfo *) ie;
1651         return (ht->hi_byte2 & IEEE80211_HTINFO_OPMODE) ==
1652             IEEE80211_HTINFO_OPMODE_MIXED;
1653 }
1654
1655 static int
1656 is11bclient(const uint8_t *rates, const uint8_t *xrates)
1657 {
1658         static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
1659         int i;
1660
1661         /* NB: the 11b clients we care about will not have xrates */
1662         if (xrates != NULL || rates == NULL)
1663                 return 0;
1664         for (i = 0; i < rates[1]; i++) {
1665                 int r = rates[2+i] & IEEE80211_RATE_VAL;
1666                 if (r > 2*11 || ((1<<r) & brates) == 0)
1667                         return 0;
1668         }
1669         return 1;
1670 }
1671
1672 static void
1673 hostap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1674         int subtype, int rssi, int nf)
1675 {
1676         struct ieee80211vap *vap = ni->ni_vap;
1677         struct ieee80211com *ic = ni->ni_ic;
1678         struct ieee80211_frame *wh;
1679         uint8_t *frm, *efrm, *sfrm;
1680         uint8_t *ssid, *rates, *xrates, *wpa, *rsn, *wme, *ath, *htcap;
1681         int reassoc, resp;
1682         uint8_t rate;
1683
1684         wh = mtod(m0, struct ieee80211_frame *);
1685         frm = (uint8_t *)&wh[1];
1686         efrm = mtod(m0, uint8_t *) + m0->m_len;
1687         switch (subtype) {
1688         case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1689         case IEEE80211_FC0_SUBTYPE_BEACON: {
1690                 struct ieee80211_scanparams scan;
1691                 /*
1692                  * We process beacon/probe response frames when scanning;
1693                  * otherwise we check beacon frames for overlapping non-ERP
1694                  * BSS in 11g and/or overlapping legacy BSS when in HT.
1695                  */ 
1696                 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
1697                     subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
1698                         vap->iv_stats.is_rx_mgtdiscard++;
1699                         return;
1700                 }
1701                 /* NB: accept off-channel frames */
1702                 if (ieee80211_parse_beacon(ni, m0, &scan) &~ IEEE80211_BPARSE_OFFCHAN)
1703                         return;
1704                 /*
1705                  * Count frame now that we know it's to be processed.
1706                  */
1707                 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1708                         vap->iv_stats.is_rx_beacon++;           /* XXX remove */
1709                         IEEE80211_NODE_STAT(ni, rx_beacons);
1710                 } else
1711                         IEEE80211_NODE_STAT(ni, rx_proberesp);
1712                 /*
1713                  * If scanning, just pass information to the scan module.
1714                  */
1715                 if (ic->ic_flags & IEEE80211_F_SCAN) {
1716                         if (scan.status == 0 &&         /* NB: on channel */
1717                             (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN)) {
1718                                 /*
1719                                  * Actively scanning a channel marked passive;
1720                                  * send a probe request now that we know there
1721                                  * is 802.11 traffic present.
1722                                  *
1723                                  * XXX check if the beacon we recv'd gives
1724                                  * us what we need and suppress the probe req
1725                                  */
1726                                 ieee80211_probe_curchan(vap, 1);
1727                                 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1728                         }
1729                         ieee80211_add_scan(vap, &scan, wh, subtype, rssi, nf);
1730                         return;
1731                 }
1732                 /*
1733                  * Check beacon for overlapping bss w/ non ERP stations.
1734                  * If we detect one and protection is configured but not
1735                  * enabled, enable it and start a timer that'll bring us
1736                  * out if we stop seeing the bss.
1737                  */
1738                 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1739                     scan.status == 0 &&                 /* NB: on-channel */
1740                     ((scan.erp & 0x100) == 0 ||         /* NB: no ERP, 11b sta*/
1741                      (scan.erp & IEEE80211_ERP_NON_ERP_PRESENT))) {
1742                         ic->ic_lastnonerp = ticks;
1743                         ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
1744                         if (ic->ic_protmode != IEEE80211_PROT_NONE &&
1745                             (ic->ic_flags & IEEE80211_F_USEPROT) == 0) {
1746                                 IEEE80211_NOTE_FRAME(vap,
1747                                     IEEE80211_MSG_ASSOC, wh,
1748                                     "non-ERP present on channel %d "
1749                                     "(saw erp 0x%x from channel %d), "
1750                                     "enable use of protection",
1751                                     ic->ic_curchan->ic_ieee,
1752                                     scan.erp, scan.chan);
1753                                 ic->ic_flags |= IEEE80211_F_USEPROT;
1754                                 ieee80211_notify_erp(ic);
1755                         }
1756                 }
1757                 /* 
1758                  * Check beacon for non-HT station on HT channel
1759                  * and update HT BSS occupancy as appropriate.
1760                  */
1761                 if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) {
1762                         if (scan.status & IEEE80211_BPARSE_OFFCHAN) {
1763                                 /*
1764                                  * Off control channel; only check frames
1765                                  * that come in the extension channel when
1766                                  * operating w/ HT40.
1767                                  */
1768                                 if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
1769                                         break;
1770                                 if (scan.chan != ic->ic_curchan->ic_extieee)
1771                                         break;
1772                         }
1773                         if (scan.htinfo == NULL) {
1774                                 ieee80211_htprot_update(ic,
1775                                     IEEE80211_HTINFO_OPMODE_PROTOPT |
1776                                     IEEE80211_HTINFO_NONHT_PRESENT);
1777                         } else if (ishtmixed(scan.htinfo)) {
1778                                 /* XXX? take NONHT_PRESENT from beacon? */
1779                                 ieee80211_htprot_update(ic,
1780                                     IEEE80211_HTINFO_OPMODE_MIXED |
1781                                     IEEE80211_HTINFO_NONHT_PRESENT);
1782                         }
1783                 }
1784                 break;
1785         }
1786
1787         case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1788                 if (vap->iv_state != IEEE80211_S_RUN) {
1789                         vap->iv_stats.is_rx_mgtdiscard++;
1790                         return;
1791                 }
1792                 /*
1793                  * prreq frame format
1794                  *      [tlv] ssid
1795                  *      [tlv] supported rates
1796                  *      [tlv] extended supported rates
1797                  */
1798                 ssid = rates = xrates = NULL;
1799                 sfrm = frm;
1800                 while (efrm - frm > 1) {
1801                         IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1802                         switch (*frm) {
1803                         case IEEE80211_ELEMID_SSID:
1804                                 ssid = frm;
1805                                 break;
1806                         case IEEE80211_ELEMID_RATES:
1807                                 rates = frm;
1808                                 break;
1809                         case IEEE80211_ELEMID_XRATES:
1810                                 xrates = frm;
1811                                 break;
1812                         }
1813                         frm += frm[1] + 2;
1814                 }
1815                 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1816                 if (xrates != NULL)
1817                         IEEE80211_VERIFY_ELEMENT(xrates,
1818                                 IEEE80211_RATE_MAXSIZE - rates[1], return);
1819                 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1820                 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
1821                 if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
1822                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1823                             wh, NULL,
1824                             "%s", "no ssid with ssid suppression enabled");
1825                         vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
1826                         return;
1827                 }
1828
1829                 /* XXX find a better class or define it's own */
1830                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1831                     "%s", "recv probe req");
1832                 /*
1833                  * Some legacy 11b clients cannot hack a complete
1834                  * probe response frame.  When the request includes
1835                  * only a bare-bones rate set, communicate this to
1836                  * the transmit side.
1837                  */
1838                 ieee80211_send_proberesp(vap, wh->i_addr2,
1839                     is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
1840                 break;
1841
1842         case IEEE80211_FC0_SUBTYPE_AUTH: {
1843                 uint16_t algo, seq, status;
1844
1845                 if (vap->iv_state != IEEE80211_S_RUN) {
1846                         vap->iv_stats.is_rx_mgtdiscard++;
1847                         return;
1848                 }
1849                 if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1850                         IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1851                             wh, NULL, "%s", "wrong bssid");
1852                         vap->iv_stats.is_rx_wrongbss++; /*XXX unique stat?*/
1853                         return;
1854                 }
1855                 /*
1856                  * auth frame format
1857                  *      [2] algorithm
1858                  *      [2] sequence
1859                  *      [2] status
1860                  *      [tlv*] challenge
1861                  */
1862                 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1863                 algo   = le16toh(*(uint16_t *)frm);
1864                 seq    = le16toh(*(uint16_t *)(frm + 2));
1865                 status = le16toh(*(uint16_t *)(frm + 4));
1866                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1867                     "recv auth frame with algorithm %d seq %d", algo, seq);
1868                 /*
1869                  * Consult the ACL policy module if setup.
1870                  */
1871                 if (vap->iv_acl != NULL &&
1872                     !vap->iv_acl->iac_check(vap, wh->i_addr2)) {
1873                         IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1874                             wh, NULL, "%s", "disallowed by ACL");
1875                         vap->iv_stats.is_rx_acl++;
1876                         ieee80211_send_error(ni, wh->i_addr2,
1877                             IEEE80211_FC0_SUBTYPE_AUTH,
1878                             (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
1879                         return;
1880                 }
1881                 if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1882                         IEEE80211_DISCARD(vap,
1883                             IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1884                             wh, NULL, "%s", "TKIP countermeasures enabled");
1885                         vap->iv_stats.is_rx_auth_countermeasures++;
1886                         ieee80211_send_error(ni, wh->i_addr2,
1887                                 IEEE80211_FC0_SUBTYPE_AUTH,
1888                                 IEEE80211_REASON_MIC_FAILURE);
1889                         return;
1890                 }
1891                 if (algo == IEEE80211_AUTH_ALG_SHARED)
1892                         hostap_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1893                             seq, status);
1894                 else if (algo == IEEE80211_AUTH_ALG_OPEN)
1895                         hostap_auth_open(ni, wh, rssi, nf, seq, status);
1896                 else if (algo == IEEE80211_AUTH_ALG_LEAP) {
1897                         authalgreject(ni, wh, algo,
1898                             seq+1, IEEE80211_STATUS_ALG);
1899                         return;
1900                 } else {
1901                         /*
1902                          * We assume that an unknown algorithm is the result
1903                          * of a decryption failure on a shared key auth frame;
1904                          * return a status code appropriate for that instead
1905                          * of IEEE80211_STATUS_ALG.
1906                          *
1907                          * NB: a seq# of 4 is intentional; the decrypted
1908                          *     frame likely has a bogus seq value.
1909                          */
1910                         authalgreject(ni, wh, algo,
1911                             4, IEEE80211_STATUS_CHALLENGE);
1912                         return;
1913                 } 
1914                 break;
1915         }
1916
1917         case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1918         case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
1919                 uint16_t capinfo, lintval;
1920                 struct ieee80211_rsnparms rsnparms;
1921
1922                 if (vap->iv_state != IEEE80211_S_RUN) {
1923                         vap->iv_stats.is_rx_mgtdiscard++;
1924                         return;
1925                 }
1926                 if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1927                         IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1928                             wh, NULL, "%s", "wrong bssid");
1929                         vap->iv_stats.is_rx_assoc_bss++;
1930                         return;
1931                 }
1932                 if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1933                         reassoc = 1;
1934                         resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
1935                 } else {
1936                         reassoc = 0;
1937                         resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
1938                 }
1939                 if (ni == vap->iv_bss) {
1940                         IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1941                             "deny %s request, sta not authenticated",
1942                             reassoc ? "reassoc" : "assoc");
1943                         ieee80211_send_error(ni, wh->i_addr2,
1944                             IEEE80211_FC0_SUBTYPE_DEAUTH,
1945                             IEEE80211_REASON_ASSOC_NOT_AUTHED);
1946                         vap->iv_stats.is_rx_assoc_notauth++;
1947                         return;
1948                 }
1949
1950                 /*
1951                  * asreq frame format
1952                  *      [2] capability information
1953                  *      [2] listen interval
1954                  *      [6*] current AP address (reassoc only)
1955                  *      [tlv] ssid
1956                  *      [tlv] supported rates
1957                  *      [tlv] extended supported rates
1958                  *      [tlv] WPA or RSN
1959                  *      [tlv] HT capabilities
1960                  *      [tlv] Atheros capabilities
1961                  */
1962                 IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4), return);
1963                 capinfo = le16toh(*(uint16_t *)frm);    frm += 2;
1964                 lintval = le16toh(*(uint16_t *)frm);    frm += 2;
1965                 if (reassoc)
1966                         frm += 6;       /* ignore current AP info */
1967                 ssid = rates = xrates = wpa = rsn = wme = ath = htcap = NULL;
1968                 sfrm = frm;
1969                 while (efrm - frm > 1) {
1970                         IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1971                         switch (*frm) {
1972                         case IEEE80211_ELEMID_SSID:
1973                                 ssid = frm;
1974                                 break;
1975                         case IEEE80211_ELEMID_RATES:
1976                                 rates = frm;
1977                                 break;
1978                         case IEEE80211_ELEMID_XRATES:
1979                                 xrates = frm;
1980                                 break;
1981                         case IEEE80211_ELEMID_RSN:
1982                                 rsn = frm;
1983                                 break;
1984                         case IEEE80211_ELEMID_HTCAP:
1985                                 htcap = frm;
1986                                 break;
1987                         case IEEE80211_ELEMID_VENDOR:
1988                                 if (iswpaoui(frm))
1989                                         wpa = frm;
1990                                 else if (iswmeinfo(frm))
1991                                         wme = frm;
1992 #ifdef IEEE80211_SUPPORT_SUPERG
1993                                 else if (isatherosoui(frm))
1994                                         ath = frm;
1995 #endif
1996                                 else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1997                                         if (ishtcapoui(frm) && htcap == NULL)
1998                                                 htcap = frm;
1999                                 }
2000                                 break;
2001                         }
2002                         frm += frm[1] + 2;
2003                 }
2004                 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
2005                 if (xrates != NULL)
2006                         IEEE80211_VERIFY_ELEMENT(xrates,
2007                                 IEEE80211_RATE_MAXSIZE - rates[1], return);
2008                 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2009                 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
2010                 if (htcap != NULL) {
2011                         IEEE80211_VERIFY_LENGTH(htcap[1],
2012                              htcap[0] == IEEE80211_ELEMID_VENDOR ?
2013                                  4 + sizeof(struct ieee80211_ie_htcap)-2 :
2014                                  sizeof(struct ieee80211_ie_htcap)-2,
2015                              return);           /* XXX just NULL out? */
2016                 }
2017
2018                 if ((vap->iv_flags & IEEE80211_F_WPA) &&
2019                     !wpa_assocreq(ni, &rsnparms, wh, wpa, rsn, capinfo))
2020                         return;
2021                 /* discard challenge after association */
2022                 if (ni->ni_challenge != NULL) {
2023                         kfree(ni->ni_challenge, M_80211_NODE);
2024                         ni->ni_challenge = NULL;
2025                 }
2026                 /* NB: 802.11 spec says to ignore station's privacy bit */
2027                 if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
2028                         capinfomismatch(ni, wh, reassoc, resp,
2029                             "capability", capinfo);
2030                         return;
2031                 }
2032                 /*
2033                  * Disallow re-associate w/ invalid slot time setting.
2034                  */
2035                 if (ni->ni_associd != 0 &&
2036                     IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2037                     ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
2038                         capinfomismatch(ni, wh, reassoc, resp,
2039                             "slot time", capinfo);
2040                         return;
2041                 }
2042                 rate = ieee80211_setup_rates(ni, rates, xrates,
2043                                 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
2044                                 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2045                 if (rate & IEEE80211_RATE_BASIC) {
2046                         ratesetmismatch(ni, wh, reassoc, resp, "legacy", rate);
2047                         vap->iv_stats.is_rx_assoc_norate++;
2048                         return;
2049                 }
2050                 /*
2051                  * If constrained to 11g-only stations reject an
2052                  * 11b-only station.  We cheat a bit here by looking
2053                  * at the max negotiated xmit rate and assuming anyone
2054                  * with a best rate <24Mb/s is an 11b station.
2055                  */
2056                 if ((vap->iv_flags & IEEE80211_F_PUREG) && rate < 48) {
2057                         ratesetmismatch(ni, wh, reassoc, resp, "11g", rate);
2058                         vap->iv_stats.is_rx_assoc_norate++;
2059                         return;
2060                 }
2061                 /*
2062                  * Do HT rate set handling and setup HT node state.
2063                  */
2064                 ni->ni_chan = vap->iv_bss->ni_chan;
2065                 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
2066                         rate = ieee80211_setup_htrates(ni, htcap,
2067                                 IEEE80211_F_DOFMCS | IEEE80211_F_DONEGO |
2068                                 IEEE80211_F_DOBRS);
2069                         if (rate & IEEE80211_RATE_BASIC) {
2070                                 ratesetmismatch(ni, wh, reassoc, resp,
2071                                     "HT", rate);
2072                                 vap->iv_stats.is_ht_assoc_norate++;
2073                                 return;
2074                         }
2075                         ieee80211_ht_node_init(ni);
2076                         ieee80211_ht_updatehtcap(ni, htcap);
2077                 } else if (ni->ni_flags & IEEE80211_NODE_HT)
2078                         ieee80211_ht_node_cleanup(ni);
2079 #ifdef IEEE80211_SUPPORT_SUPERG
2080                 else if (ni->ni_ath_flags & IEEE80211_NODE_ATH)
2081                         ieee80211_ff_node_cleanup(ni);
2082 #endif
2083                 /*
2084                  * Allow AMPDU operation only with unencrypted traffic
2085                  * or AES-CCM; the 11n spec only specifies these ciphers
2086                  * so permitting any others is undefined and can lead
2087                  * to interoperability problems.
2088                  */
2089                 if ((ni->ni_flags & IEEE80211_NODE_HT) &&
2090                     (((vap->iv_flags & IEEE80211_F_WPA) &&
2091                       rsnparms.rsn_ucastcipher != IEEE80211_CIPHER_AES_CCM) ||
2092                      (vap->iv_flags & (IEEE80211_F_WPA|IEEE80211_F_PRIVACY)) == IEEE80211_F_PRIVACY)) {
2093                         IEEE80211_NOTE(vap,
2094                             IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
2095                             "disallow HT use because WEP or TKIP requested, "
2096                             "capinfo 0x%x ucastcipher %d", capinfo,
2097                             rsnparms.rsn_ucastcipher);
2098                         ieee80211_ht_node_cleanup(ni);
2099                         vap->iv_stats.is_ht_assoc_downgrade++;
2100                 }
2101                 /*
2102                  * If constrained to 11n-only stations reject legacy stations.
2103                  */
2104                 if ((vap->iv_flags_ht & IEEE80211_FHT_PUREN) &&
2105                     (ni->ni_flags & IEEE80211_NODE_HT) == 0) {
2106                         htcapmismatch(ni, wh, reassoc, resp);
2107                         vap->iv_stats.is_ht_assoc_nohtcap++;
2108                         return;
2109                 }
2110                 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
2111                 ni->ni_noise = nf;
2112                 ni->ni_intval = lintval;
2113                 ni->ni_capinfo = capinfo;
2114                 ni->ni_fhdwell = vap->iv_bss->ni_fhdwell;
2115                 ni->ni_fhindex = vap->iv_bss->ni_fhindex;
2116                 /*
2117                  * Store the IEs.
2118                  * XXX maybe better to just expand
2119                  */
2120                 if (ieee80211_ies_init(&ni->ni_ies, sfrm, efrm - sfrm)) {
2121 #define setie(_ie, _off)        ieee80211_ies_setie(ni->ni_ies, _ie, _off)
2122                         if (wpa != NULL)
2123                                 setie(wpa_ie, wpa - sfrm);
2124                         if (rsn != NULL)
2125                                 setie(rsn_ie, rsn - sfrm);
2126                         if (htcap != NULL)
2127                                 setie(htcap_ie, htcap - sfrm);
2128                         if (wme != NULL) {
2129                                 setie(wme_ie, wme - sfrm);
2130                                 /*
2131                                  * Mark node as capable of QoS.
2132                                  */
2133                                 ni->ni_flags |= IEEE80211_NODE_QOS;
2134                         } else
2135                                 ni->ni_flags &= ~IEEE80211_NODE_QOS;
2136 #ifdef IEEE80211_SUPPORT_SUPERG
2137                         if (ath != NULL) {
2138                                 setie(ath_ie, ath - sfrm);
2139                                 /* 
2140                                  * Parse ATH station parameters.
2141                                  */
2142                                 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
2143                         } else
2144 #endif
2145                                 ni->ni_ath_flags = 0;
2146 #undef setie
2147                 } else {
2148                         ni->ni_flags &= ~IEEE80211_NODE_QOS;
2149                         ni->ni_ath_flags = 0;
2150                 }
2151                 ieee80211_node_join(ni, resp);
2152                 ieee80211_deliver_l2uf(ni);
2153                 break;
2154         }
2155
2156         case IEEE80211_FC0_SUBTYPE_DEAUTH:
2157         case IEEE80211_FC0_SUBTYPE_DISASSOC: {
2158                 uint16_t reason;
2159
2160                 if (vap->iv_state != IEEE80211_S_RUN ||
2161                     /* NB: can happen when in promiscuous mode */
2162                     !IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
2163                         vap->iv_stats.is_rx_mgtdiscard++;
2164                         break;
2165                 }
2166                 /*
2167                  * deauth/disassoc frame format
2168                  *      [2] reason
2169                  */
2170                 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
2171                 reason = le16toh(*(uint16_t *)frm);
2172                 if (subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
2173                         vap->iv_stats.is_rx_deauth++;
2174                         IEEE80211_NODE_STAT(ni, rx_deauth);
2175                 } else {
2176                         vap->iv_stats.is_rx_disassoc++;
2177                         IEEE80211_NODE_STAT(ni, rx_disassoc);
2178                 }
2179                 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2180                     "recv %s (reason %d)", ieee80211_mgt_subtype_name[subtype >>
2181                         IEEE80211_FC0_SUBTYPE_SHIFT], reason);
2182                 if (ni != vap->iv_bss)
2183                         ieee80211_node_leave(ni);
2184                 break;
2185         }
2186
2187         case IEEE80211_FC0_SUBTYPE_ACTION:
2188                 if (vap->iv_state == IEEE80211_S_RUN) {
2189                         if (ieee80211_parse_action(ni, m0) == 0)
2190                                 ic->ic_recv_action(ni, wh, frm, efrm);
2191                 } else
2192                         vap->iv_stats.is_rx_mgtdiscard++;
2193                 break;
2194
2195         case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2196         case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2197         default:
2198                 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2199                      wh, "mgt", "subtype 0x%x not handled", subtype);
2200                 vap->iv_stats.is_rx_badsubtype++;
2201                 break;
2202         }
2203 }
2204
2205 static void
2206 hostap_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2207 {
2208         switch (subtype) {
2209         case IEEE80211_FC0_SUBTYPE_PS_POLL:
2210                 hostap_recv_pspoll(ni, m);
2211                 break;
2212         case IEEE80211_FC0_SUBTYPE_BAR:
2213                 ieee80211_recv_bar(ni, m);
2214                 break;
2215         }
2216 }
2217
2218 /*
2219  * Process a received ps-poll frame.
2220  */
2221 static void
2222 hostap_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m0)
2223 {
2224         struct ieee80211vap *vap = ni->ni_vap;
2225         struct ieee80211_frame_min *wh;
2226         struct ifnet *ifp;
2227         struct mbuf *m;
2228         uint16_t aid;
2229         int qlen;
2230
2231         wh = mtod(m0, struct ieee80211_frame_min *);
2232         if (ni->ni_associd == 0) {
2233                 IEEE80211_DISCARD(vap,
2234                     IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2235                     (struct ieee80211_frame *) wh, NULL,
2236                     "%s", "unassociated station");
2237                 vap->iv_stats.is_ps_unassoc++;
2238                 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
2239                         IEEE80211_REASON_NOT_ASSOCED);
2240                 return;
2241         }
2242
2243         aid = le16toh(*(uint16_t *)wh->i_dur);
2244         if (aid != ni->ni_associd) {
2245                 IEEE80211_DISCARD(vap,
2246                     IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2247                     (struct ieee80211_frame *) wh, NULL,
2248                     "aid mismatch: sta aid 0x%x poll aid 0x%x",
2249                     ni->ni_associd, aid);
2250                 vap->iv_stats.is_ps_badaid++;
2251                 /*
2252                  * NB: We used to deauth the station but it turns out
2253                  * the Blackberry Curve 8230 (and perhaps other devices) 
2254                  * sometimes send the wrong AID when WME is negotiated.
2255                  * Being more lenient here seems ok as we already check
2256                  * the station is associated and we only return frames
2257                  * queued for the station (i.e. we don't use the AID).
2258                  */
2259                 return;
2260         }
2261
2262         /* Okay, take the first queued packet and put it out... */
2263         m = ieee80211_node_psq_dequeue(ni, &qlen);
2264         if (m == NULL) {
2265                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_POWER, wh->i_addr2,
2266                     "%s", "recv ps-poll, but queue empty");
2267                 ieee80211_send_nulldata(ieee80211_ref_node(ni));
2268                 vap->iv_stats.is_ps_qempty++;   /* XXX node stat */
2269                 if (vap->iv_set_tim != NULL)
2270                         vap->iv_set_tim(ni, 0); /* just in case */
2271                 return;
2272         }
2273         /* 
2274          * If there are more packets, set the more packets bit
2275          * in the packet dispatched to the station; otherwise
2276          * turn off the TIM bit.
2277          */
2278         if (qlen != 0) {
2279                 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2280                     "recv ps-poll, send packet, %u still queued", qlen);
2281                 m->m_flags |= M_MORE_DATA;
2282         } else {
2283                 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2284                     "%s", "recv ps-poll, send packet, queue empty");
2285                 if (vap->iv_set_tim != NULL)
2286                         vap->iv_set_tim(ni, 0);
2287         }
2288         m->m_flags |= M_PWR_SAV;                /* bypass PS handling */
2289
2290         if (m->m_flags & M_ENCAP)
2291                 ifp = vap->iv_ic->ic_ifp;
2292         else
2293                 ifp = vap->iv_ifp;
2294         IF_ENQUEUE(&ifp->if_snd, m);
2295         ifp->if_start(ifp);
2296 }