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