]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net80211/ieee80211_sta.c
MFV: r313101
[FreeBSD/FreeBSD.git] / sys / net80211 / ieee80211_sta.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 Station mode support.
33  */
34 #include "opt_inet.h"
35 #include "opt_wlan.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h> 
39 #include <sys/mbuf.h>   
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
42
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/endian.h>
46 #include <sys/errno.h>
47 #include <sys/proc.h>
48 #include <sys/sysctl.h>
49
50 #include <net/if.h>
51 #include <net/if_media.h>
52 #include <net/if_llc.h>
53 #include <net/if_dl.h>
54 #include <net/if_var.h>
55 #include <net/ethernet.h>
56
57 #include <net/bpf.h>
58
59 #include <net80211/ieee80211_var.h>
60 #include <net80211/ieee80211_sta.h>
61 #include <net80211/ieee80211_input.h>
62 #ifdef IEEE80211_SUPPORT_SUPERG
63 #include <net80211/ieee80211_superg.h>
64 #endif
65 #include <net80211/ieee80211_ratectl.h>
66 #include <net80211/ieee80211_sta.h>
67 #include <net80211/ieee80211_vht.h>
68
69 #define IEEE80211_RATE2MBS(r)   (((r) & IEEE80211_RATE_VAL) / 2)
70
71 static  void sta_vattach(struct ieee80211vap *);
72 static  void sta_beacon_miss(struct ieee80211vap *);
73 static  int sta_newstate(struct ieee80211vap *, enum ieee80211_state, int);
74 static  int sta_input(struct ieee80211_node *, struct mbuf *,
75             const struct ieee80211_rx_stats *, int, int);
76 static void sta_recv_mgmt(struct ieee80211_node *, struct mbuf *,
77             int subtype, const struct ieee80211_rx_stats *, int rssi, int nf);
78 static void sta_recv_ctl(struct ieee80211_node *, struct mbuf *, int subtype);
79
80 void
81 ieee80211_sta_attach(struct ieee80211com *ic)
82 {
83         ic->ic_vattach[IEEE80211_M_STA] = sta_vattach;
84 }
85
86 void
87 ieee80211_sta_detach(struct ieee80211com *ic)
88 {
89 }
90
91 static void
92 sta_vdetach(struct ieee80211vap *vap)
93 {
94 }
95
96 static void
97 sta_vattach(struct ieee80211vap *vap)
98 {
99         vap->iv_newstate = sta_newstate;
100         vap->iv_input = sta_input;
101         vap->iv_recv_mgmt = sta_recv_mgmt;
102         vap->iv_recv_ctl = sta_recv_ctl;
103         vap->iv_opdetach = sta_vdetach;
104         vap->iv_bmiss = sta_beacon_miss;
105 }
106
107 /*
108  * Handle a beacon miss event.  The common code filters out
109  * spurious events that can happen when scanning and/or before
110  * reaching RUN state.
111  */
112 static void
113 sta_beacon_miss(struct ieee80211vap *vap)
114 {
115         struct ieee80211com *ic = vap->iv_ic;
116
117         IEEE80211_LOCK_ASSERT(ic);
118
119         KASSERT((ic->ic_flags & IEEE80211_F_SCAN) == 0, ("scanning"));
120         KASSERT(vap->iv_state >= IEEE80211_S_RUN,
121             ("wrong state %s", ieee80211_state_name[vap->iv_state]));
122
123         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
124             "beacon miss, mode %s state %s\n",
125             ieee80211_opmode_name[vap->iv_opmode],
126             ieee80211_state_name[vap->iv_state]);
127
128         if (vap->iv_state == IEEE80211_S_CSA) {
129                 /*
130                  * A Channel Switch is pending; assume we missed the
131                  * beacon that would've completed the process and just
132                  * force the switch.  If we made a mistake we'll not
133                  * find the AP on the new channel and fall back to a
134                  * normal scan.
135                  */
136                 ieee80211_csa_completeswitch(ic);
137                 return;
138         }
139         if (++vap->iv_bmiss_count < vap->iv_bmiss_max) {
140                 /*
141                  * Send a directed probe req before falling back to a
142                  * scan; if we receive a response ic_bmiss_count will
143                  * be reset.  Some cards mistakenly report beacon miss
144                  * so this avoids the expensive scan if the ap is
145                  * still there.
146                  */
147                 ieee80211_send_probereq(vap->iv_bss, vap->iv_myaddr,
148                         vap->iv_bss->ni_bssid, vap->iv_bss->ni_bssid,
149                         vap->iv_bss->ni_essid, vap->iv_bss->ni_esslen);
150                 return;
151         }
152
153         callout_stop(&vap->iv_swbmiss);
154         vap->iv_bmiss_count = 0;
155         vap->iv_stats.is_beacon_miss++;
156         if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
157 #ifdef IEEE80211_SUPPORT_SUPERG
158
159                 /*
160                  * If we receive a beacon miss interrupt when using
161                  * dynamic turbo, attempt to switch modes before
162                  * reassociating.
163                  */
164                 if (IEEE80211_ATH_CAP(vap, vap->iv_bss, IEEE80211_NODE_TURBOP))
165                         ieee80211_dturbo_switch(vap,
166                             ic->ic_bsschan->ic_flags ^ IEEE80211_CHAN_TURBO);
167 #endif
168                 /*
169                  * Try to reassociate before scanning for a new ap.
170                  */
171                 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
172         } else {
173                 /*
174                  * Somebody else is controlling state changes (e.g.
175                  * a user-mode app) don't do anything that would
176                  * confuse them; just drop into scan mode so they'll
177                  * notified of the state change and given control.
178                  */
179                 ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
180         }
181 }
182
183 /*
184  * Handle deauth with reason.  We retry only for
185  * the cases where we might succeed.  Otherwise
186  * we downgrade the ap and scan.
187  */
188 static void
189 sta_authretry(struct ieee80211vap *vap, struct ieee80211_node *ni, int reason)
190 {
191         switch (reason) {
192         case IEEE80211_STATUS_SUCCESS:          /* NB: MLME assoc */
193         case IEEE80211_STATUS_TIMEOUT:
194         case IEEE80211_REASON_ASSOC_EXPIRE:
195         case IEEE80211_REASON_NOT_AUTHED:
196         case IEEE80211_REASON_NOT_ASSOCED:
197         case IEEE80211_REASON_ASSOC_LEAVE:
198         case IEEE80211_REASON_ASSOC_NOT_AUTHED:
199                 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, 1);
200                 break;
201         default:
202                 ieee80211_scan_assoc_fail(vap, vap->iv_bss->ni_macaddr, reason);
203                 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
204                         ieee80211_check_scan_current(vap);
205                 break;
206         }
207 }
208
209 static void
210 sta_swbmiss_start(struct ieee80211vap *vap)
211 {
212
213         if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) {
214                 /*
215                  * Start s/w beacon miss timer for devices w/o
216                  * hardware support.  We fudge a bit here since
217                  * we're doing this in software.
218                  */
219                 vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS(
220                     2 * vap->iv_bmissthreshold * vap->iv_bss->ni_intval);
221                 vap->iv_swbmiss_count = 0;
222                 callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
223                     ieee80211_swbmiss, vap);
224         }
225 }
226
227 /*
228  * IEEE80211_M_STA vap state machine handler.
229  * This routine handles the main states in the 802.11 protocol.
230  */
231 static int
232 sta_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
233 {
234         struct ieee80211com *ic = vap->iv_ic;
235         struct ieee80211_node *ni;
236         enum ieee80211_state ostate;
237
238         IEEE80211_LOCK_ASSERT(ic);
239
240         ostate = vap->iv_state;
241         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
242             __func__, ieee80211_state_name[ostate],
243             ieee80211_state_name[nstate], arg);
244         vap->iv_state = nstate;                 /* state transition */
245         callout_stop(&vap->iv_mgtsend);         /* XXX callout_drain */
246         if (ostate != IEEE80211_S_SCAN)
247                 ieee80211_cancel_scan(vap);     /* background scan */
248         ni = vap->iv_bss;                       /* NB: no reference held */
249         if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)
250                 callout_stop(&vap->iv_swbmiss);
251         switch (nstate) {
252         case IEEE80211_S_INIT:
253                 switch (ostate) {
254                 case IEEE80211_S_SLEEP:
255                         /* XXX wakeup */
256                         /* XXX driver hook to wakeup the hardware? */
257                 case IEEE80211_S_RUN:
258                         IEEE80211_SEND_MGMT(ni,
259                             IEEE80211_FC0_SUBTYPE_DISASSOC,
260                             IEEE80211_REASON_ASSOC_LEAVE);
261                         ieee80211_sta_leave(ni);
262                         break;
263                 case IEEE80211_S_ASSOC:
264                         IEEE80211_SEND_MGMT(ni,
265                             IEEE80211_FC0_SUBTYPE_DEAUTH,
266                             IEEE80211_REASON_AUTH_LEAVE);
267                         break;
268                 case IEEE80211_S_SCAN:
269                         ieee80211_cancel_scan(vap);
270                         break;
271                 default:
272                         break;
273                 }
274                 if (ostate != IEEE80211_S_INIT) {
275                         /* NB: optimize INIT -> INIT case */
276                         ieee80211_reset_bss(vap);
277                 }
278                 if (vap->iv_auth->ia_detach != NULL)
279                         vap->iv_auth->ia_detach(vap);
280                 break;
281         case IEEE80211_S_SCAN:
282                 switch (ostate) {
283                 case IEEE80211_S_INIT:
284                         /*
285                          * Initiate a scan.  We can come here as a result
286                          * of an IEEE80211_IOC_SCAN_REQ too in which case
287                          * the vap will be marked with IEEE80211_FEXT_SCANREQ
288                          * and the scan request parameters will be present
289                          * in iv_scanreq.  Otherwise we do the default.
290                          */
291                         if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
292                                 ieee80211_check_scan(vap,
293                                     vap->iv_scanreq_flags,
294                                     vap->iv_scanreq_duration,
295                                     vap->iv_scanreq_mindwell,
296                                     vap->iv_scanreq_maxdwell,
297                                     vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
298                                 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
299                         } else
300                                 ieee80211_check_scan_current(vap);
301                         break;
302                 case IEEE80211_S_SCAN:
303                 case IEEE80211_S_AUTH:
304                 case IEEE80211_S_ASSOC:
305                         /*
306                          * These can happen either because of a timeout
307                          * on an assoc/auth response or because of a
308                          * change in state that requires a reset.  For
309                          * the former we're called with a non-zero arg
310                          * that is the cause for the failure; pass this
311                          * to the scan code so it can update state.
312                          * Otherwise trigger a new scan unless we're in
313                          * manual roaming mode in which case an application
314                          * must issue an explicit scan request.
315                          */
316                         if (arg != 0)
317                                 ieee80211_scan_assoc_fail(vap,
318                                         vap->iv_bss->ni_macaddr, arg);
319                         if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
320                                 ieee80211_check_scan_current(vap);
321                         break;
322                 case IEEE80211_S_SLEEP:         /* beacon miss */
323                         /*
324                          * XXX if in sleep we need to wakeup the hardware.
325                          */
326                         /* FALLTHROUGH */
327                 case IEEE80211_S_RUN:           /* beacon miss */
328                         /*
329                          * Beacon miss.  Notify user space and if not
330                          * under control of a user application (roaming
331                          * manual) kick off a scan to re-connect.
332                          */
333
334                         ieee80211_sta_leave(ni);
335                         if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
336                                 ieee80211_check_scan_current(vap);
337                         break;
338                 default:
339                         goto invalid;
340                 }
341                 break;
342         case IEEE80211_S_AUTH:
343                 switch (ostate) {
344                 case IEEE80211_S_INIT:
345                 case IEEE80211_S_SCAN:
346                         IEEE80211_SEND_MGMT(ni,
347                             IEEE80211_FC0_SUBTYPE_AUTH, 1);
348                         break;
349                 case IEEE80211_S_AUTH:
350                 case IEEE80211_S_ASSOC:
351                         switch (arg & 0xff) {
352                         case IEEE80211_FC0_SUBTYPE_AUTH:
353                                 /* ??? */
354                                 IEEE80211_SEND_MGMT(ni,
355                                     IEEE80211_FC0_SUBTYPE_AUTH, 2);
356                                 break;
357                         case IEEE80211_FC0_SUBTYPE_DEAUTH:
358                                 sta_authretry(vap, ni, arg>>8);
359                                 break;
360                         }
361                         break;
362                 case IEEE80211_S_SLEEP:
363                 case IEEE80211_S_RUN:
364                         switch (arg & 0xff) {
365                         case IEEE80211_FC0_SUBTYPE_AUTH:
366                                 IEEE80211_SEND_MGMT(ni,
367                                     IEEE80211_FC0_SUBTYPE_AUTH, 2);
368                                 vap->iv_state = IEEE80211_S_RUN; /* stay RUN */
369                                 break;
370                         case IEEE80211_FC0_SUBTYPE_DEAUTH:
371                                 ieee80211_sta_leave(ni);
372                                 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
373                                         /* try to reauth */
374                                         IEEE80211_SEND_MGMT(ni,
375                                             IEEE80211_FC0_SUBTYPE_AUTH, 1);
376                                 }
377                                 break;
378                         }
379                         break;
380                 default:
381                         goto invalid;
382                 }
383                 break;
384         case IEEE80211_S_ASSOC:
385                 switch (ostate) {
386                 case IEEE80211_S_AUTH:
387                 case IEEE80211_S_ASSOC:
388                         IEEE80211_SEND_MGMT(ni,
389                             IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
390                         break;
391                 case IEEE80211_S_SLEEP:         /* cannot happen */
392                 case IEEE80211_S_RUN:
393                         ieee80211_sta_leave(ni);
394                         if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
395                                 IEEE80211_SEND_MGMT(ni, arg ?
396                                     IEEE80211_FC0_SUBTYPE_REASSOC_REQ :
397                                     IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
398                         }
399                         break;
400                 default:
401                         goto invalid;
402                 }
403                 break;
404         case IEEE80211_S_RUN:
405                 if (vap->iv_flags & IEEE80211_F_WPA) {
406                         /* XXX validate prerequisites */
407                 }
408                 switch (ostate) {
409                 case IEEE80211_S_RUN:
410                 case IEEE80211_S_CSA:
411                         break;
412                 case IEEE80211_S_AUTH:          /* when join is done in fw */
413                 case IEEE80211_S_ASSOC:
414 #ifdef IEEE80211_DEBUG
415                         if (ieee80211_msg_debug(vap)) {
416                                 ieee80211_note(vap, "%s with %s ssid ",
417                                     (vap->iv_opmode == IEEE80211_M_STA ?
418                                     "associated" : "synchronized"),
419                                     ether_sprintf(ni->ni_bssid));
420                                 ieee80211_print_essid(vap->iv_bss->ni_essid,
421                                     ni->ni_esslen);
422                                 /* XXX MCS/HT */
423                                 printf(" channel %d start %uMb\n",
424                                     ieee80211_chan2ieee(ic, ic->ic_curchan),
425                                     IEEE80211_RATE2MBS(ni->ni_txrate));
426                         }
427 #endif
428                         ieee80211_scan_assoc_success(vap, ni->ni_macaddr);
429                         ieee80211_notify_node_join(ni, 
430                             arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
431                         break;
432                 case IEEE80211_S_SLEEP:
433                         /* Wake up from sleep */
434                         vap->iv_sta_ps(vap, 0);
435                         break;
436                 default:
437                         goto invalid;
438                 }
439                 ieee80211_sync_curchan(ic);
440                 if (ostate != IEEE80211_S_RUN)
441                         sta_swbmiss_start(vap);
442                 /*
443                  * When 802.1x is not in use mark the port authorized
444                  * at this point so traffic can flow.
445                  */
446                 if (ni->ni_authmode != IEEE80211_AUTH_8021X)
447                         ieee80211_node_authorize(ni);
448                 /*
449                  * Fake association when joining an existing bss.
450                  *
451                  * Don't do this if we're doing SLEEP->RUN.
452                  */
453                 if (ic->ic_newassoc != NULL && ostate != IEEE80211_S_SLEEP)
454                         ic->ic_newassoc(vap->iv_bss, (ostate != IEEE80211_S_RUN));
455                 break;
456         case IEEE80211_S_CSA:
457                 if (ostate != IEEE80211_S_RUN)
458                         goto invalid;
459                 break;
460         case IEEE80211_S_SLEEP:
461                 sta_swbmiss_start(vap);
462                 vap->iv_sta_ps(vap, 1);
463                 break;
464         default:
465         invalid:
466                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
467                     "%s: unexpected state transition %s -> %s\n", __func__,
468                     ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
469                 break;
470         }
471         return 0;
472 }
473
474 /*
475  * Return non-zero if the frame is an echo of a multicast
476  * frame sent by ourself.  The dir is known to be DSTODS.
477  */
478 static __inline int
479 isdstods_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
480 {
481 #define QWH4(wh)        ((const struct ieee80211_qosframe_addr4 *)wh)
482 #define WH4(wh)         ((const struct ieee80211_frame_addr4 *)wh)
483         const uint8_t *sa;
484
485         KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
486
487         if (!IEEE80211_IS_MULTICAST(wh->i_addr3))
488                 return 0;
489         sa = IEEE80211_QOS_HAS_SEQ(wh) ? QWH4(wh)->i_addr4 : WH4(wh)->i_addr4;
490         return IEEE80211_ADDR_EQ(sa, vap->iv_myaddr);
491 #undef WH4
492 #undef QWH4
493 }
494
495 /*
496  * Return non-zero if the frame is an echo of a multicast
497  * frame sent by ourself.  The dir is known to be FROMDS.
498  */
499 static __inline int
500 isfromds_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
501 {
502         KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
503
504         if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
505                 return 0;
506         return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
507 }
508
509 /*
510  * Decide if a received management frame should be
511  * printed when debugging is enabled.  This filters some
512  * of the less interesting frames that come frequently
513  * (e.g. beacons).
514  */
515 static __inline int
516 doprint(struct ieee80211vap *vap, int subtype)
517 {
518         switch (subtype) {
519         case IEEE80211_FC0_SUBTYPE_BEACON:
520                 return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
521         case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
522                 return 0;
523         }
524         return 1;
525 }
526
527 /*
528  * Process a received frame.  The node associated with the sender
529  * should be supplied.  If nothing was found in the node table then
530  * the caller is assumed to supply a reference to iv_bss instead.
531  * The RSSI and a timestamp are also supplied.  The RSSI data is used
532  * during AP scanning to select a AP to associate with; it can have
533  * any units so long as values have consistent units and higher values
534  * mean ``better signal''.  The receive timestamp is currently not used
535  * by the 802.11 layer.
536  */
537 static int
538 sta_input(struct ieee80211_node *ni, struct mbuf *m,
539     const struct ieee80211_rx_stats *rxs, int rssi, int nf)
540 {
541         struct ieee80211vap *vap = ni->ni_vap;
542         struct ieee80211com *ic = ni->ni_ic;
543         struct ifnet *ifp = vap->iv_ifp;
544         struct ieee80211_frame *wh;
545         struct ieee80211_key *key;
546         struct ether_header *eh;
547         int hdrspace, need_tap = 1;     /* mbuf need to be tapped. */
548         uint8_t dir, type, subtype, qos;
549         uint8_t *bssid;
550         int is_hw_decrypted = 0;
551         int has_decrypted = 0;
552
553         /*
554          * Some devices do hardware decryption all the way through
555          * to pretending the frame wasn't encrypted in the first place.
556          * So, tag it appropriately so it isn't discarded inappropriately.
557          */
558         if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED))
559                 is_hw_decrypted = 1;
560
561         if (m->m_flags & M_AMPDU_MPDU) {
562                 /*
563                  * Fastpath for A-MPDU reorder q resubmission.  Frames
564                  * w/ M_AMPDU_MPDU marked have already passed through
565                  * here but were received out of order and been held on
566                  * the reorder queue.  When resubmitted they are marked
567                  * with the M_AMPDU_MPDU flag and we can bypass most of
568                  * the normal processing.
569                  */
570                 wh = mtod(m, struct ieee80211_frame *);
571                 type = IEEE80211_FC0_TYPE_DATA;
572                 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
573                 subtype = IEEE80211_FC0_SUBTYPE_QOS;
574                 hdrspace = ieee80211_hdrspace(ic, wh);  /* XXX optimize? */
575                 goto resubmit_ampdu;
576         }
577
578         KASSERT(ni != NULL, ("null node"));
579         ni->ni_inact = ni->ni_inact_reload;
580
581         type = -1;                      /* undefined */
582
583         if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
584                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
585                     ni->ni_macaddr, NULL,
586                     "too short (1): len %u", m->m_pkthdr.len);
587                 vap->iv_stats.is_rx_tooshort++;
588                 goto out;
589         }
590         /*
591          * Bit of a cheat here, we use a pointer for a 3-address
592          * frame format but don't reference fields past outside
593          * ieee80211_frame_min w/o first validating the data is
594          * present.
595          */
596         wh = mtod(m, struct ieee80211_frame *);
597
598         if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
599             IEEE80211_FC0_VERSION_0) {
600                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
601                     ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
602                     wh->i_fc[0], wh->i_fc[1]);
603                 vap->iv_stats.is_rx_badversion++;
604                 goto err;
605         }
606
607         dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
608         type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
609         subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
610         if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
611                 bssid = wh->i_addr2;
612                 if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) {
613                         /* not interested in */
614                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
615                             bssid, NULL, "%s", "not to bss");
616                         vap->iv_stats.is_rx_wrongbss++;
617                         goto out;
618                 }
619
620                 /*
621                  * Some devices may be in a promiscuous mode
622                  * where they receive frames for multiple station
623                  * addresses.
624                  *
625                  * If we receive a data frame that isn't
626                  * destined to our VAP MAC, drop it.
627                  *
628                  * XXX TODO: This is only enforced when not scanning;
629                  * XXX it assumes a software-driven scan will put the NIC
630                  * XXX into a "no data frames" mode before setting this
631                  * XXX flag. Otherwise it may be possible that we'll still
632                  * XXX process data frames whilst scanning.
633                  */
634                 if ((! IEEE80211_IS_MULTICAST(wh->i_addr1))
635                     && (! IEEE80211_ADDR_EQ(wh->i_addr1, IF_LLADDR(ifp)))) {
636                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
637                             bssid, NULL, "not to cur sta: lladdr=%6D, addr1=%6D",
638                             IF_LLADDR(ifp), ":", wh->i_addr1, ":");
639                         vap->iv_stats.is_rx_wrongbss++;
640                         goto out;
641                 }
642
643                 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
644                 ni->ni_noise = nf;
645                 if ( IEEE80211_HAS_SEQ(type, subtype) &&
646                     !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
647                         uint8_t tid = ieee80211_gettid(wh);
648                         if (IEEE80211_QOS_HAS_SEQ(wh) &&
649                             TID_TO_WME_AC(tid) >= WME_AC_VI)
650                                 ic->ic_wme.wme_hipri_traffic++;
651                         if (! ieee80211_check_rxseq(ni, wh, bssid))
652                                 goto out;
653                 }
654         }
655
656         switch (type) {
657         case IEEE80211_FC0_TYPE_DATA:
658                 hdrspace = ieee80211_hdrspace(ic, wh);
659                 if (m->m_len < hdrspace &&
660                     (m = m_pullup(m, hdrspace)) == NULL) {
661                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
662                             ni->ni_macaddr, NULL,
663                             "data too short: expecting %u", hdrspace);
664                         vap->iv_stats.is_rx_tooshort++;
665                         goto out;               /* XXX */
666                 }
667                 /*
668                  * Handle A-MPDU re-ordering.  If the frame is to be
669                  * processed directly then ieee80211_ampdu_reorder
670                  * will return 0; otherwise it has consumed the mbuf
671                  * and we should do nothing more with it.
672                  */
673                 if ((m->m_flags & M_AMPDU) &&
674                     (dir == IEEE80211_FC1_DIR_FROMDS ||
675                      dir == IEEE80211_FC1_DIR_DSTODS) &&
676                     ieee80211_ampdu_reorder(ni, m) != 0) {
677                         m = NULL;
678                         goto out;
679                 }
680         resubmit_ampdu:
681                 if (dir == IEEE80211_FC1_DIR_FROMDS) {
682                         if ((ifp->if_flags & IFF_SIMPLEX) &&
683                             isfromds_mcastecho(vap, wh)) {
684                                 /*
685                                  * In IEEE802.11 network, multicast
686                                  * packets sent from "me" are broadcast
687                                  * from the AP; silently discard for
688                                  * SIMPLEX interface.
689                                  */
690                                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
691                                     wh, "data", "%s", "multicast echo");
692                                 vap->iv_stats.is_rx_mcastecho++;
693                                 goto out;
694                         }
695                         if ((vap->iv_flags & IEEE80211_F_DWDS) &&
696                             IEEE80211_IS_MULTICAST(wh->i_addr1)) {
697                                 /*
698                                  * DWDS sta's must drop 3-address mcast frames
699                                  * as they will be sent separately as a 4-addr
700                                  * frame.  Accepting the 3-addr frame will
701                                  * confuse the bridge into thinking the sending
702                                  * sta is located at the end of WDS link.
703                                  */
704                                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
705                                     "3-address data", "%s", "DWDS enabled");
706                                 vap->iv_stats.is_rx_mcastecho++;
707                                 goto out;
708                         }
709                 } else if (dir == IEEE80211_FC1_DIR_DSTODS) {
710                         if ((vap->iv_flags & IEEE80211_F_DWDS) == 0) {
711                                 IEEE80211_DISCARD(vap,
712                                     IEEE80211_MSG_INPUT, wh, "4-address data",
713                                     "%s", "DWDS not enabled");
714                                 vap->iv_stats.is_rx_wrongdir++;
715                                 goto out;
716                         }
717                         if ((ifp->if_flags & IFF_SIMPLEX) &&
718                             isdstods_mcastecho(vap, wh)) {
719                                 /*
720                                  * In IEEE802.11 network, multicast
721                                  * packets sent from "me" are broadcast
722                                  * from the AP; silently discard for
723                                  * SIMPLEX interface.
724                                  */
725                                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
726                                     "4-address data", "%s", "multicast echo");
727                                 vap->iv_stats.is_rx_mcastecho++;
728                                 goto out;
729                         }
730                 } else {
731                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
732                             "data", "incorrect dir 0x%x", dir);
733                         vap->iv_stats.is_rx_wrongdir++;
734                         goto out;
735                 }
736
737                 /*
738                  * Handle privacy requirements for hardware decryption
739                  * devices.
740                  *
741                  * For those devices, a handful of things happen.
742                  *
743                  * + If IV has been stripped, then we can't run
744                  *   ieee80211_crypto_decap() - none of the key
745                  * + If MIC has been stripped, we can't validate
746                  *   MIC here.
747                  * + If MIC fails, then we need to communicate a
748                  *   MIC failure up to the stack - but we don't know
749                  *   which key was used.
750                  */
751
752                 /*
753                  * Handle privacy requirements.  Note that we
754                  * must not be preempted from here until after
755                  * we (potentially) call ieee80211_crypto_demic;
756                  * otherwise we may violate assumptions in the
757                  * crypto cipher modules used to do delayed update
758                  * of replay sequence numbers.
759                  */
760                 if (is_hw_decrypted || wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
761                         if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
762                                 /*
763                                  * Discard encrypted frames when privacy is off.
764                                  */
765                                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
766                                     wh, "WEP", "%s", "PRIVACY off");
767                                 vap->iv_stats.is_rx_noprivacy++;
768                                 IEEE80211_NODE_STAT(ni, rx_noprivacy);
769                                 goto out;
770                         }
771                         if (ieee80211_crypto_decap(ni, m, hdrspace, &key) == 0) {
772                                 /* NB: stats+msgs handled in crypto_decap */
773                                 IEEE80211_NODE_STAT(ni, rx_wepfail);
774                                 goto out;
775                         }
776                         wh = mtod(m, struct ieee80211_frame *);
777                         wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
778                         has_decrypted = 1;
779                 } else {
780                         /* XXX M_WEP and IEEE80211_F_PRIVACY */
781                         key = NULL;
782                 }
783
784                 /*
785                  * Save QoS bits for use below--before we strip the header.
786                  */
787                 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
788                         qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
789                             ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
790                             ((struct ieee80211_qosframe *)wh)->i_qos[0];
791                 } else
792                         qos = 0;
793
794                 /*
795                  * Next up, any fragmentation.
796                  */
797                 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
798                         m = ieee80211_defrag(ni, m, hdrspace);
799                         if (m == NULL) {
800                                 /* Fragment dropped or frame not complete yet */
801                                 goto out;
802                         }
803                 }
804                 wh = NULL;              /* no longer valid, catch any uses */
805
806                 /*
807                  * Next strip any MSDU crypto bits.
808                  *
809                  * Note: we can't do MIC stripping/verification if the
810                  * upper layer has stripped it.  We have to check MIC
811                  * ourselves.  So, key may be NULL, but we have to check
812                  * the RX status.
813                  */
814                 if (!ieee80211_crypto_demic(vap, key, m, 0)) {
815                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
816                             ni->ni_macaddr, "data", "%s", "demic error");
817                         vap->iv_stats.is_rx_demicfail++;
818                         IEEE80211_NODE_STAT(ni, rx_demicfail);
819                         goto out;
820                 }
821
822                 /* copy to listener after decrypt */
823                 if (ieee80211_radiotap_active_vap(vap))
824                         ieee80211_radiotap_rx(vap, m);
825                 need_tap = 0;
826
827                 /*
828                  * Finally, strip the 802.11 header.
829                  */
830                 m = ieee80211_decap(vap, m, hdrspace);
831                 if (m == NULL) {
832                         /* XXX mask bit to check for both */
833                         /* don't count Null data frames as errors */
834                         if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
835                             subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
836                                 goto out;
837                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
838                             ni->ni_macaddr, "data", "%s", "decap error");
839                         vap->iv_stats.is_rx_decap++;
840                         IEEE80211_NODE_STAT(ni, rx_decap);
841                         goto err;
842                 }
843                 eh = mtod(m, struct ether_header *);
844                 if (!ieee80211_node_is_authorized(ni)) {
845                         /*
846                          * Deny any non-PAE frames received prior to
847                          * authorization.  For open/shared-key
848                          * authentication the port is mark authorized
849                          * after authentication completes.  For 802.1x
850                          * the port is not marked authorized by the
851                          * authenticator until the handshake has completed.
852                          */
853                         if (eh->ether_type != htons(ETHERTYPE_PAE)) {
854                                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
855                                     eh->ether_shost, "data",
856                                     "unauthorized port: ether type 0x%x len %u",
857                                     eh->ether_type, m->m_pkthdr.len);
858                                 vap->iv_stats.is_rx_unauth++;
859                                 IEEE80211_NODE_STAT(ni, rx_unauth);
860                                 goto err;
861                         }
862                 } else {
863                         /*
864                          * When denying unencrypted frames, discard
865                          * any non-PAE frames received without encryption.
866                          */
867                         if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
868                             ((has_decrypted == 0) && (m->m_flags & M_WEP) == 0) &&
869                             (is_hw_decrypted == 0) &&
870                             eh->ether_type != htons(ETHERTYPE_PAE)) {
871                                 /*
872                                  * Drop unencrypted frames.
873                                  */
874                                 vap->iv_stats.is_rx_unencrypted++;
875                                 IEEE80211_NODE_STAT(ni, rx_unencrypted);
876                                 goto out;
877                         }
878                 }
879                 /* XXX require HT? */
880                 if (qos & IEEE80211_QOS_AMSDU) {
881                         m = ieee80211_decap_amsdu(ni, m);
882                         if (m == NULL)
883                                 return IEEE80211_FC0_TYPE_DATA;
884                 } else {
885 #ifdef IEEE80211_SUPPORT_SUPERG
886                         m = ieee80211_decap_fastframe(vap, ni, m);
887                         if (m == NULL)
888                                 return IEEE80211_FC0_TYPE_DATA;
889 #endif
890                 }
891                 ieee80211_deliver_data(vap, ni, m);
892                 return IEEE80211_FC0_TYPE_DATA;
893
894         case IEEE80211_FC0_TYPE_MGT:
895                 vap->iv_stats.is_rx_mgmt++;
896                 IEEE80211_NODE_STAT(ni, rx_mgmt);
897                 if (dir != IEEE80211_FC1_DIR_NODS) {
898                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
899                             wh, "data", "incorrect dir 0x%x", dir);
900                         vap->iv_stats.is_rx_wrongdir++;
901                         goto err;
902                 }
903                 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
904                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
905                             ni->ni_macaddr, "mgt", "too short: len %u",
906                             m->m_pkthdr.len);
907                         vap->iv_stats.is_rx_tooshort++;
908                         goto out;
909                 }
910 #ifdef IEEE80211_DEBUG
911                 if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
912                     ieee80211_msg_dumppkts(vap)) {
913                         if_printf(ifp, "received %s from %s rssi %d\n",
914                             ieee80211_mgt_subtype_name(subtype),
915                             ether_sprintf(wh->i_addr2), rssi);
916                 }
917 #endif
918
919                 /*
920                  * Note: See above for hardware offload privacy requirements.
921                  *       It also applies here.
922                  */
923
924                 /*
925                  * Again, having encrypted flag set check would be good, but
926                  * then we have to also handle crypto_decap() like above.
927                  */
928                 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
929                         if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
930                                 /*
931                                  * Only shared key auth frames with a challenge
932                                  * should be encrypted, discard all others.
933                                  */
934                                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
935                                     wh, ieee80211_mgt_subtype_name(subtype),
936                                     "%s", "WEP set but not permitted");
937                                 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
938                                 goto out;
939                         }
940                         if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
941                                 /*
942                                  * Discard encrypted frames when privacy is off.
943                                  */
944                                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
945                                     wh, "mgt", "%s", "WEP set but PRIVACY off");
946                                 vap->iv_stats.is_rx_noprivacy++;
947                                 goto out;
948                         }
949                         hdrspace = ieee80211_hdrspace(ic, wh);
950
951                         /*
952                          * Again, if IV/MIC was stripped, then this whole
953                          * setup will fail.  That's going to need some poking.
954                          */
955                         if (ieee80211_crypto_decap(ni, m, hdrspace, &key) == 0) {
956                                 /* NB: stats+msgs handled in crypto_decap */
957                                 goto out;
958                         }
959                         has_decrypted = 1;
960                         wh = mtod(m, struct ieee80211_frame *);
961                         wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
962                 }
963                 vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
964                 goto out;
965
966         case IEEE80211_FC0_TYPE_CTL:
967                 vap->iv_stats.is_rx_ctl++;
968                 IEEE80211_NODE_STAT(ni, rx_ctrl);
969                 vap->iv_recv_ctl(ni, m, subtype);
970                 goto out;
971
972         default:
973                 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
974                     wh, NULL, "bad frame type 0x%x", type);
975                 /* should not come here */
976                 break;
977         }
978 err:
979         if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
980 out:
981         if (m != NULL) {
982                 if (need_tap && ieee80211_radiotap_active_vap(vap))
983                         ieee80211_radiotap_rx(vap, m);
984                 m_freem(m);
985         }
986         return type;
987 }
988
989 static void
990 sta_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
991     int rssi, int nf, uint16_t seq, uint16_t status)
992 {
993         struct ieee80211vap *vap = ni->ni_vap;
994
995         if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
996                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
997                     ni->ni_macaddr, "open auth",
998                     "bad sta auth mode %u", ni->ni_authmode);
999                 vap->iv_stats.is_rx_bad_auth++; /* XXX */
1000                 return;
1001         }
1002         if (vap->iv_state != IEEE80211_S_AUTH ||
1003             seq != IEEE80211_AUTH_OPEN_RESPONSE) {
1004                 vap->iv_stats.is_rx_bad_auth++;
1005                 return;
1006         }
1007         if (status != 0) {
1008                 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1009                     ni, "open auth failed (reason %d)", status);
1010                 vap->iv_stats.is_rx_auth_fail++;
1011                 vap->iv_stats.is_rx_authfail_code = status;
1012                 ieee80211_new_state(vap, IEEE80211_S_SCAN,
1013                     IEEE80211_SCAN_FAIL_STATUS);
1014         } else
1015                 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1016 }
1017
1018 static void
1019 sta_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
1020     uint8_t *frm, uint8_t *efrm, int rssi, int nf,
1021     uint16_t seq, uint16_t status)
1022 {
1023         struct ieee80211vap *vap = ni->ni_vap;
1024         uint8_t *challenge;
1025
1026         /*
1027          * NB: this can happen as we allow pre-shared key
1028          * authentication to be enabled w/o wep being turned
1029          * on so that configuration of these can be done
1030          * in any order.  It may be better to enforce the
1031          * ordering in which case this check would just be
1032          * for sanity/consistency.
1033          */
1034         if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
1035                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1036                     ni->ni_macaddr, "shared key auth",
1037                     "%s", " PRIVACY is disabled");
1038                 goto bad;
1039         }
1040         /*
1041          * Pre-shared key authentication is evil; accept
1042          * it only if explicitly configured (it is supported
1043          * mainly for compatibility with clients like OS X).
1044          */
1045         if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1046             ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1047                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1048                     ni->ni_macaddr, "shared key auth",
1049                     "bad sta auth mode %u", ni->ni_authmode);
1050                 vap->iv_stats.is_rx_bad_auth++; /* XXX maybe a unique error? */
1051                 goto bad;
1052         }
1053
1054         challenge = NULL;
1055         if (frm + 1 < efrm) {
1056                 if ((frm[1] + 2) > (efrm - frm)) {
1057                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1058                             ni->ni_macaddr, "shared key auth",
1059                             "ie %d/%d too long",
1060                             frm[0], (frm[1] + 2) - (efrm - frm));
1061                         vap->iv_stats.is_rx_bad_auth++;
1062                         goto bad;
1063                 }
1064                 if (*frm == IEEE80211_ELEMID_CHALLENGE)
1065                         challenge = frm;
1066                 frm += frm[1] + 2;
1067         }
1068         switch (seq) {
1069         case IEEE80211_AUTH_SHARED_CHALLENGE:
1070         case IEEE80211_AUTH_SHARED_RESPONSE:
1071                 if (challenge == NULL) {
1072                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1073                             ni->ni_macaddr, "shared key auth",
1074                             "%s", "no challenge");
1075                         vap->iv_stats.is_rx_bad_auth++;
1076                         goto bad;
1077                 }
1078                 if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1079                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1080                             ni->ni_macaddr, "shared key auth",
1081                             "bad challenge len %d", challenge[1]);
1082                         vap->iv_stats.is_rx_bad_auth++;
1083                         goto bad;
1084                 }
1085         default:
1086                 break;
1087         }
1088         if (vap->iv_state != IEEE80211_S_AUTH)
1089                 return;
1090         switch (seq) {
1091         case IEEE80211_AUTH_SHARED_PASS:
1092                 if (ni->ni_challenge != NULL) {
1093                         IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
1094                         ni->ni_challenge = NULL;
1095                 }
1096                 if (status != 0) {
1097                         IEEE80211_NOTE_FRAME(vap,
1098                             IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, wh,
1099                             "shared key auth failed (reason %d)", status);
1100                         vap->iv_stats.is_rx_auth_fail++;
1101                         vap->iv_stats.is_rx_authfail_code = status;
1102                         return;
1103                 }
1104                 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1105                 break;
1106         case IEEE80211_AUTH_SHARED_CHALLENGE:
1107                 if (!ieee80211_alloc_challenge(ni))
1108                         return;
1109                 /* XXX could optimize by passing recvd challenge */
1110                 memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
1111                 IEEE80211_SEND_MGMT(ni,
1112                         IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1113                 break;
1114         default:
1115                 IEEE80211_DISCARD(vap, IEEE80211_MSG_AUTH,
1116                     wh, "shared key auth", "bad seq %d", seq);
1117                 vap->iv_stats.is_rx_bad_auth++;
1118                 return;
1119         }
1120         return;
1121 bad:
1122         /*
1123          * Kick the state machine.  This short-circuits
1124          * using the mgt frame timeout to trigger the
1125          * state transition.
1126          */
1127         if (vap->iv_state == IEEE80211_S_AUTH)
1128                 ieee80211_new_state(vap, IEEE80211_S_SCAN,
1129                     IEEE80211_SCAN_FAIL_STATUS);
1130 }
1131
1132 int
1133 ieee80211_parse_wmeparams(struct ieee80211vap *vap, uint8_t *frm,
1134         const struct ieee80211_frame *wh)
1135 {
1136 #define MS(_v, _f)      (((_v) & _f) >> _f##_S)
1137         struct ieee80211_wme_state *wme = &vap->iv_ic->ic_wme;
1138         u_int len = frm[1], qosinfo;
1139         int i;
1140
1141         if (len < sizeof(struct ieee80211_wme_param)-2) {
1142                 IEEE80211_DISCARD_IE(vap,
1143                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
1144                     wh, "WME", "too short, len %u", len);
1145                 return -1;
1146         }
1147         qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)];
1148         qosinfo &= WME_QOSINFO_COUNT;
1149         /* XXX do proper check for wraparound */
1150         if (qosinfo == wme->wme_wmeChanParams.cap_info)
1151                 return 0;
1152         frm += __offsetof(struct ieee80211_wme_param, params_acParams);
1153         for (i = 0; i < WME_NUM_AC; i++) {
1154                 struct wmeParams *wmep =
1155                         &wme->wme_wmeChanParams.cap_wmeParams[i];
1156                 /* NB: ACI not used */
1157                 wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
1158                 wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
1159                 wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
1160                 wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
1161                 wmep->wmep_txopLimit = le16dec(frm+2);
1162                 frm += 4;
1163         }
1164         wme->wme_wmeChanParams.cap_info = qosinfo;
1165         return 1;
1166 #undef MS
1167 }
1168
1169 /*
1170  * Process 11h Channel Switch Announcement (CSA) ie.  If this
1171  * is the first CSA then initiate the switch.  Otherwise we
1172  * track state and trigger completion and/or cancel of the switch.
1173  * XXX should be public for IBSS use
1174  */
1175 static void
1176 ieee80211_parse_csaparams(struct ieee80211vap *vap, uint8_t *frm,
1177         const struct ieee80211_frame *wh)
1178 {
1179         struct ieee80211com *ic = vap->iv_ic;
1180         const struct ieee80211_csa_ie *csa =
1181             (const struct ieee80211_csa_ie *) frm;
1182
1183         KASSERT(vap->iv_state >= IEEE80211_S_RUN,
1184             ("state %s", ieee80211_state_name[vap->iv_state]));
1185
1186         if (csa->csa_mode > 1) {
1187                 IEEE80211_DISCARD_IE(vap,
1188                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1189                     wh, "CSA", "invalid mode %u", csa->csa_mode);
1190                 return;
1191         }
1192         IEEE80211_LOCK(ic);
1193         if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
1194                 /*
1195                  * Convert the channel number to a channel reference.  We
1196                  * try first to preserve turbo attribute of the current
1197                  * channel then fallback.  Note this will not work if the
1198                  * CSA specifies a channel that requires a band switch (e.g.
1199                  * 11a => 11g).  This is intentional as 11h is defined only
1200                  * for 5GHz/11a and because the switch does not involve a
1201                  * reassociation, protocol state (capabilities, negotated
1202                  * rates, etc) may/will be wrong.
1203                  */
1204                 struct ieee80211_channel *c =
1205                     ieee80211_find_channel_byieee(ic, csa->csa_newchan,
1206                         (ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALLTURBO));
1207                 if (c == NULL) {
1208                         c = ieee80211_find_channel_byieee(ic,
1209                             csa->csa_newchan,
1210                             (ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALL));
1211                         if (c == NULL) {
1212                                 IEEE80211_DISCARD_IE(vap,
1213                                     IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1214                                     wh, "CSA", "invalid channel %u",
1215                                     csa->csa_newchan);
1216                                 goto done;
1217                         }
1218                 }
1219 #if IEEE80211_CSA_COUNT_MIN > 0
1220                 if (csa->csa_count < IEEE80211_CSA_COUNT_MIN) {
1221                         /*
1222                          * Require at least IEEE80211_CSA_COUNT_MIN count to
1223                          * reduce the risk of being redirected by a fabricated
1224                          * CSA.  If a valid CSA is dropped we'll still get a
1225                          * beacon miss when the AP leaves the channel so we'll
1226                          * eventually follow to the new channel.
1227                          *
1228                          * NOTE: this violates the 11h spec that states that
1229                          * count may be any value and if 0 then a switch
1230                          * should happen asap.
1231                          */
1232                         IEEE80211_DISCARD_IE(vap,
1233                             IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1234                             wh, "CSA", "count %u too small, must be >= %u",
1235                             csa->csa_count, IEEE80211_CSA_COUNT_MIN);
1236                         goto done;
1237                 }
1238 #endif
1239                 ieee80211_csa_startswitch(ic, c, csa->csa_mode, csa->csa_count);
1240         } else {
1241                 /*
1242                  * Validate this ie against the initial CSA.  We require
1243                  * mode and channel not change and the count must be
1244                  * monotonically decreasing.  This may be pointless and
1245                  * canceling the switch as a result may be too paranoid but
1246                  * in the worst case if we drop out of CSA because of this
1247                  * and the AP does move then we'll just end up taking a
1248                  * beacon miss and scan to find the AP.
1249                  *
1250                  * XXX may want <= on count as we also process ProbeResp
1251                  * frames and those may come in w/ the same count as the
1252                  * previous beacon; but doing so leaves us open to a stuck
1253                  * count until we add a dead-man timer
1254                  */
1255                 if (!(csa->csa_count < ic->ic_csa_count &&
1256                       csa->csa_mode == ic->ic_csa_mode &&
1257                       csa->csa_newchan == ieee80211_chan2ieee(ic, ic->ic_csa_newchan))) {
1258                         IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_DOTH, wh,
1259                             "CSA ie mismatch, initial ie <%d,%d,%d>, "
1260                             "this ie <%d,%d,%d>", ic->ic_csa_mode,
1261                             ic->ic_csa_newchan, ic->ic_csa_count,
1262                             csa->csa_mode, csa->csa_newchan, csa->csa_count);
1263                         ieee80211_csa_cancelswitch(ic);
1264                 } else {
1265                         if (csa->csa_count <= 1)
1266                                 ieee80211_csa_completeswitch(ic);
1267                         else
1268                                 ic->ic_csa_count = csa->csa_count;
1269                 }
1270         }
1271 done:
1272         IEEE80211_UNLOCK(ic);
1273 }
1274
1275 /*
1276  * Return non-zero if a background scan may be continued:
1277  * o bg scan is active
1278  * o no channel switch is pending
1279  * o there has not been any traffic recently
1280  * o no full-offload scan support (no need for explicitly continuing scan then)
1281  *
1282  * Note we do not check if there is an administrative enable;
1283  * this is only done to start the scan.  We assume that any
1284  * change in state will be accompanied by a request to cancel
1285  * active scans which will otherwise cause this test to fail.
1286  */
1287 static __inline int
1288 contbgscan(struct ieee80211vap *vap)
1289 {
1290         struct ieee80211com *ic = vap->iv_ic;
1291
1292         return ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) &&
1293             (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1294             !(vap->iv_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) &&
1295             vap->iv_state == IEEE80211_S_RUN &&         /* XXX? */
1296             ieee80211_time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1297 }
1298
1299 /*
1300  * Return non-zero if a backgrond scan may be started:
1301  * o bg scanning is administratively enabled
1302  * o no channel switch is pending
1303  * o we are not boosted on a dynamic turbo channel
1304  * o there has not been a scan recently
1305  * o there has not been any traffic recently (don't check if full-offload scan)
1306  */
1307 static __inline int
1308 startbgscan(struct ieee80211vap *vap)
1309 {
1310         struct ieee80211com *ic = vap->iv_ic;
1311
1312         return ((vap->iv_flags & IEEE80211_F_BGSCAN) &&
1313             (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1314 #ifdef IEEE80211_SUPPORT_SUPERG
1315             !IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1316 #endif
1317             ieee80211_time_after(ticks, ic->ic_lastscan + vap->iv_bgscanintvl) &&
1318             ((vap->iv_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) ||
1319              ieee80211_time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle)));
1320 }
1321
1322 static void
1323 sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
1324     const struct ieee80211_rx_stats *rxs,
1325     int rssi, int nf)
1326 {
1327 #define ISREASSOC(_st)  ((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
1328         struct ieee80211vap *vap = ni->ni_vap;
1329         struct ieee80211com *ic = ni->ni_ic;
1330         struct ieee80211_channel *rxchan = ic->ic_curchan;
1331         struct ieee80211_frame *wh;
1332         uint8_t *frm, *efrm;
1333         uint8_t *rates, *xrates, *wme, *htcap, *htinfo;
1334         uint8_t *vhtcap, *vhtopmode;
1335         uint8_t rate;
1336         int ht_state_change = 0, do_ht = 0;
1337
1338         wh = mtod(m0, struct ieee80211_frame *);
1339         frm = (uint8_t *)&wh[1];
1340         efrm = mtod(m0, uint8_t *) + m0->m_len;
1341         switch (subtype) {
1342         case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1343         case IEEE80211_FC0_SUBTYPE_BEACON: {
1344                 struct ieee80211_scanparams scan;
1345                 struct ieee80211_channel *c;
1346                 /*
1347                  * We process beacon/probe response frames:
1348                  *    o when scanning, or
1349                  *    o station mode when associated (to collect state
1350                  *      updates such as 802.11g slot time)
1351                  * Frames otherwise received are discarded.
1352                  */ 
1353                 if (!((ic->ic_flags & IEEE80211_F_SCAN) || ni->ni_associd)) {
1354                         vap->iv_stats.is_rx_mgtdiscard++;
1355                         return;
1356                 }
1357
1358                 /* Override RX channel as appropriate */
1359                 if (rxs != NULL) {
1360                         c = ieee80211_lookup_channel_rxstatus(vap, rxs);
1361                         if (c != NULL)
1362                                 rxchan = c;
1363                 }
1364
1365                 /* XXX probe response in sta mode when !scanning? */
1366                 if (ieee80211_parse_beacon(ni, m0, rxchan, &scan) != 0) {
1367                         if (! (ic->ic_flags & IEEE80211_F_SCAN))
1368                                 vap->iv_stats.is_beacon_bad++;
1369                         return;
1370                 }
1371
1372                 /*
1373                  * Count frame now that we know it's to be processed.
1374                  */
1375                 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1376                         vap->iv_stats.is_rx_beacon++;           /* XXX remove */
1377                         IEEE80211_NODE_STAT(ni, rx_beacons);
1378                 } else
1379                         IEEE80211_NODE_STAT(ni, rx_proberesp);
1380                 /*
1381                  * When operating in station mode, check for state updates.
1382                  * Be careful to ignore beacons received while doing a
1383                  * background scan.  We consider only 11g/WMM stuff right now.
1384                  */
1385                 if (ni->ni_associd != 0 &&
1386                     ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
1387                      IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
1388                         /* record tsf of last beacon */
1389                         memcpy(ni->ni_tstamp.data, scan.tstamp,
1390                                 sizeof(ni->ni_tstamp));
1391                         /* count beacon frame for s/w bmiss handling */
1392                         vap->iv_swbmiss_count++;
1393                         vap->iv_bmiss_count = 0;
1394                         if (ni->ni_erp != scan.erp) {
1395                                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1396                                     wh->i_addr2,
1397                                     "erp change: was 0x%x, now 0x%x",
1398                                     ni->ni_erp, scan.erp);
1399                                 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1400                                     (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1401                                         ic->ic_flags |= IEEE80211_F_USEPROT;
1402                                 else
1403                                         ic->ic_flags &= ~IEEE80211_F_USEPROT;
1404                                 ni->ni_erp = scan.erp;
1405                                 /* XXX statistic */
1406                                 /* XXX driver notification */
1407                         }
1408                         if ((ni->ni_capinfo ^ scan.capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) {
1409                                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1410                                     wh->i_addr2,
1411                                     "capabilities change: was 0x%x, now 0x%x",
1412                                     ni->ni_capinfo, scan.capinfo);
1413                                 /*
1414                                  * NB: we assume short preamble doesn't
1415                                  *     change dynamically
1416                                  */
1417                                 ieee80211_set_shortslottime(ic,
1418                                         IEEE80211_IS_CHAN_A(ic->ic_bsschan) ||
1419                                         (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1420                                 ni->ni_capinfo = (ni->ni_capinfo &~ IEEE80211_CAPINFO_SHORT_SLOTTIME)
1421                                                | (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME);
1422                                 /* XXX statistic */
1423                         }
1424                         if (scan.wme != NULL &&
1425                             (ni->ni_flags & IEEE80211_NODE_QOS) &&
1426                             ieee80211_parse_wmeparams(vap, scan.wme, wh) > 0)
1427                                 ieee80211_wme_updateparams(vap);
1428 #ifdef IEEE80211_SUPPORT_SUPERG
1429                         if (scan.ath != NULL)
1430                                 ieee80211_parse_athparams(ni, scan.ath, wh);
1431 #endif
1432                         if (scan.htcap != NULL && scan.htinfo != NULL &&
1433                             (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1434                                 /* XXX state changes? */
1435                                 ieee80211_ht_updateparams(ni,
1436                                     scan.htcap, scan.htinfo);
1437                                 do_ht = 1;
1438                         }
1439                         if (scan.vhtcap != NULL && scan.vhtopmode != NULL &&
1440                             (vap->iv_flags_vht & IEEE80211_FVHT_VHT)) {
1441                                 /* XXX state changes? */
1442                                 ieee80211_vht_updateparams(ni,
1443                                     scan.vhtcap, scan.vhtopmode);
1444                                 do_ht = 1;
1445                         }
1446                         if (do_ht) {
1447                                 if (ieee80211_ht_updateparams_final(ni,
1448                                     scan.htcap, scan.htinfo))
1449                                         ht_state_change = 1;
1450                         }
1451
1452                         if (scan.quiet)
1453                                 ic->ic_set_quiet(ni, scan.quiet);
1454
1455                         if (scan.tim != NULL) {
1456                                 struct ieee80211_tim_ie *tim =
1457                                     (struct ieee80211_tim_ie *) scan.tim;
1458                                 /*
1459                                  * XXX Check/debug this code; see if it's about
1460                                  * the right time to force the VAP awake if we
1461                                  * receive a frame destined for us?
1462                                  */
1463                                 int aid = IEEE80211_AID(ni->ni_associd);
1464                                 int ix = aid / NBBY;
1465                                 int min = tim->tim_bitctl &~ 1;
1466                                 int max = tim->tim_len + min - 4;
1467                                 int tim_ucast = 0, tim_mcast = 0;
1468
1469                                 /*
1470                                  * Only do this for unicast traffic in the TIM
1471                                  * The multicast traffic notification for
1472                                  * the scan notification stuff should occur
1473                                  * differently.
1474                                  */
1475                                 if (min <= ix && ix <= max &&
1476                                      isset(tim->tim_bitmap - min, aid)) {
1477                                         tim_ucast = 1;
1478                                 }
1479
1480                                 /*
1481                                  * Do a separate notification
1482                                  * for the multicast bit being set.
1483                                  */
1484                                 if (tim->tim_bitctl & 1) {
1485                                         tim_mcast = 1;
1486                                 }
1487
1488                                 /*
1489                                  * If the TIM indicates there's traffic for
1490                                  * us then get us out of STA mode powersave.
1491                                  */
1492                                 if (tim_ucast == 1) {
1493
1494                                         /*
1495                                          * Wake us out of SLEEP state if we're
1496                                          * in it; and if we're doing bgscan
1497                                          * then wake us out of STA powersave.
1498                                          */
1499                                         ieee80211_sta_tim_notify(vap, 1);
1500
1501                                         /*
1502                                          * This is preventing us from
1503                                          * continuing a bgscan; because it
1504                                          * tricks the contbgscan()
1505                                          * routine to think there's always
1506                                          * traffic for us.
1507                                          *
1508                                          * I think we need both an RX and
1509                                          * TX ic_lastdata field.
1510                                          */
1511                                         ic->ic_lastdata = ticks;
1512                                 }
1513
1514                                 ni->ni_dtim_count = tim->tim_count;
1515                                 ni->ni_dtim_period = tim->tim_period;
1516                         }
1517                         if (scan.csa != NULL &&
1518                             (vap->iv_flags & IEEE80211_F_DOTH))
1519                                 ieee80211_parse_csaparams(vap, scan.csa, wh);
1520                         else if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
1521                                 /*
1522                                  * No CSA ie or 11h disabled, but a channel
1523                                  * switch is pending; drop out so we aren't
1524                                  * stuck in CSA state.  If the AP really is
1525                                  * moving we'll get a beacon miss and scan.
1526                                  */
1527                                 IEEE80211_LOCK(ic);
1528                                 ieee80211_csa_cancelswitch(ic);
1529                                 IEEE80211_UNLOCK(ic);
1530                         }
1531                         /*
1532                          * If scanning, pass the info to the scan module.
1533                          * Otherwise, check if it's the right time to do
1534                          * a background scan.  Background scanning must
1535                          * be enabled and we must not be operating in the
1536                          * turbo phase of dynamic turbo mode.  Then,
1537                          * it's been a while since the last background
1538                          * scan and if no data frames have come through
1539                          * recently, kick off a scan.  Note that this
1540                          * is the mechanism by which a background scan
1541                          * is started _and_ continued each time we
1542                          * return on-channel to receive a beacon from
1543                          * our ap.
1544                          */
1545                         if (ic->ic_flags & IEEE80211_F_SCAN) {
1546                                 ieee80211_add_scan(vap, rxchan,
1547                                     &scan, wh, subtype, rssi, nf);
1548                         } else if (contbgscan(vap)) {
1549                                 ieee80211_bg_scan(vap, 0);
1550                         } else if (startbgscan(vap)) {
1551                                 vap->iv_stats.is_scan_bg++;
1552 #if 0
1553                                 /* wakeup if we are sleeing */
1554                                 ieee80211_set_pwrsave(vap, 0);
1555 #endif
1556                                 ieee80211_bg_scan(vap, 0);
1557                         }
1558
1559                         /*
1560                          * Put the station to sleep if we haven't seen
1561                          * traffic in a while.
1562                          */
1563                         IEEE80211_LOCK(ic);
1564                         ieee80211_sta_ps_timer_check(vap);
1565                         IEEE80211_UNLOCK(ic);
1566
1567                         /*
1568                          * If we've had a channel width change (eg HT20<->HT40)
1569                          * then schedule a delayed driver notification.
1570                          */
1571                         if (ht_state_change)
1572                                 ieee80211_update_chw(ic);
1573                         return;
1574                 }
1575                 /*
1576                  * If scanning, just pass information to the scan module.
1577                  */
1578                 if (ic->ic_flags & IEEE80211_F_SCAN) {
1579                         if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1580                                 /*
1581                                  * Actively scanning a channel marked passive;
1582                                  * send a probe request now that we know there
1583                                  * is 802.11 traffic present.
1584                                  *
1585                                  * XXX check if the beacon we recv'd gives
1586                                  * us what we need and suppress the probe req
1587                                  */
1588                                 ieee80211_probe_curchan(vap, 1);
1589                                 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1590                         }
1591                         ieee80211_add_scan(vap, rxchan, &scan, wh,
1592                             subtype, rssi, nf);
1593                         return;
1594                 }
1595                 break;
1596         }
1597
1598         case IEEE80211_FC0_SUBTYPE_AUTH: {
1599                 uint16_t algo, seq, status;
1600                 /*
1601                  * auth frame format
1602                  *      [2] algorithm
1603                  *      [2] sequence
1604                  *      [2] status
1605                  *      [tlv*] challenge
1606                  */
1607                 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1608                 algo   = le16toh(*(uint16_t *)frm);
1609                 seq    = le16toh(*(uint16_t *)(frm + 2));
1610                 status = le16toh(*(uint16_t *)(frm + 4));
1611                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1612                     "recv auth frame with algorithm %d seq %d", algo, seq);
1613
1614                 if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1615                         IEEE80211_DISCARD(vap,
1616                             IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1617                             wh, "auth", "%s", "TKIP countermeasures enabled");
1618                         vap->iv_stats.is_rx_auth_countermeasures++;
1619                         if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1620                                 ieee80211_send_error(ni, wh->i_addr2,
1621                                         IEEE80211_FC0_SUBTYPE_AUTH,
1622                                         IEEE80211_REASON_MIC_FAILURE);
1623                         }
1624                         return;
1625                 }
1626                 if (algo == IEEE80211_AUTH_ALG_SHARED)
1627                         sta_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1628                             seq, status);
1629                 else if (algo == IEEE80211_AUTH_ALG_OPEN)
1630                         sta_auth_open(ni, wh, rssi, nf, seq, status);
1631                 else {
1632                         IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1633                             wh, "auth", "unsupported alg %d", algo);
1634                         vap->iv_stats.is_rx_auth_unsupported++;
1635                         return;
1636                 } 
1637                 break;
1638         }
1639
1640         case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1641         case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
1642                 uint16_t capinfo, associd;
1643                 uint16_t status;
1644
1645                 if (vap->iv_state != IEEE80211_S_ASSOC) {
1646                         vap->iv_stats.is_rx_mgtdiscard++;
1647                         return;
1648                 }
1649
1650                 /*
1651                  * asresp frame format
1652                  *      [2] capability information
1653                  *      [2] status
1654                  *      [2] association ID
1655                  *      [tlv] supported rates
1656                  *      [tlv] extended supported rates
1657                  *      [tlv] WME
1658                  *      [tlv] HT capabilities
1659                  *      [tlv] HT info
1660                  */
1661                 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1662                 ni = vap->iv_bss;
1663                 capinfo = le16toh(*(uint16_t *)frm);
1664                 frm += 2;
1665                 status = le16toh(*(uint16_t *)frm);
1666                 frm += 2;
1667                 if (status != 0) {
1668                         IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1669                             wh->i_addr2, "%sassoc failed (reason %d)",
1670                             ISREASSOC(subtype) ?  "re" : "", status);
1671                         vap->iv_stats.is_rx_auth_fail++;        /* XXX */
1672                         return;
1673                 }
1674                 associd = le16toh(*(uint16_t *)frm);
1675                 frm += 2;
1676
1677                 rates = xrates = wme = htcap = htinfo = NULL;
1678                 vhtcap = vhtopmode = NULL;
1679                 while (efrm - frm > 1) {
1680                         IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1681                         switch (*frm) {
1682                         case IEEE80211_ELEMID_RATES:
1683                                 rates = frm;
1684                                 break;
1685                         case IEEE80211_ELEMID_XRATES:
1686                                 xrates = frm;
1687                                 break;
1688                         case IEEE80211_ELEMID_HTCAP:
1689                                 htcap = frm;
1690                                 break;
1691                         case IEEE80211_ELEMID_HTINFO:
1692                                 htinfo = frm;
1693                                 break;
1694                         case IEEE80211_ELEMID_VENDOR:
1695                                 if (iswmeoui(frm))
1696                                         wme = frm;
1697                                 else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1698                                         /*
1699                                          * Accept pre-draft HT ie's if the
1700                                          * standard ones have not been seen.
1701                                          */
1702                                         if (ishtcapoui(frm)) {
1703                                                 if (htcap == NULL)
1704                                                         htcap = frm;
1705                                         } else if (ishtinfooui(frm)) {
1706                                                 if (htinfo == NULL)
1707                                                         htinfo = frm;
1708                                         }
1709                                 }
1710                                 /* XXX Atheros OUI support */
1711                                 break;
1712                         case IEEE80211_ELEMID_VHT_CAP:
1713                                 vhtcap = frm;
1714                                 break;
1715                         case IEEE80211_ELEMID_VHT_OPMODE:
1716                                 vhtopmode = frm;
1717                                 break;
1718                         }
1719                         frm += frm[1] + 2;
1720                 }
1721
1722                 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1723                 if (xrates != NULL)
1724                         IEEE80211_VERIFY_ELEMENT(xrates,
1725                                 IEEE80211_RATE_MAXSIZE - rates[1], return);
1726                 rate = ieee80211_setup_rates(ni, rates, xrates,
1727                                 IEEE80211_F_JOIN |
1728                                 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1729                                 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1730                 if (rate & IEEE80211_RATE_BASIC) {
1731                         IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1732                             wh->i_addr2,
1733                             "%sassoc failed (rate set mismatch)",
1734                             ISREASSOC(subtype) ?  "re" : "");
1735                         vap->iv_stats.is_rx_assoc_norate++;
1736                         ieee80211_new_state(vap, IEEE80211_S_SCAN,
1737                             IEEE80211_SCAN_FAIL_STATUS);
1738                         return;
1739                 }
1740
1741                 ni->ni_capinfo = capinfo;
1742                 ni->ni_associd = associd;
1743                 if (ni->ni_jointime == 0)
1744                         ni->ni_jointime = time_uptime;
1745                 if (wme != NULL &&
1746                     ieee80211_parse_wmeparams(vap, wme, wh) >= 0) {
1747                         ni->ni_flags |= IEEE80211_NODE_QOS;
1748                         ieee80211_wme_updateparams(vap);
1749                 } else
1750                         ni->ni_flags &= ~IEEE80211_NODE_QOS;
1751                 /*
1752                  * Setup HT state according to the negotiation.
1753                  *
1754                  * NB: shouldn't need to check if HT use is enabled but some
1755                  *     ap's send back HT ie's even when we don't indicate we
1756                  *     are HT capable in our AssocReq.
1757                  */
1758                 if (htcap != NULL && htinfo != NULL &&
1759                     (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1760                         ieee80211_ht_node_init(ni);
1761                         ieee80211_ht_updateparams(ni, htcap, htinfo);
1762
1763                         if ((vhtcap != NULL) && (vhtopmode != NULL) &
1764                             (vap->iv_flags_vht & IEEE80211_FVHT_VHT)) {
1765                                 /*
1766                                  * Log if we get a VHT assoc/reassoc response.
1767                                  * We aren't ready for 2GHz VHT support.
1768                                  */
1769                                 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
1770                                         printf("%s: peer %6D: VHT on 2GHz, ignoring\n",
1771                                             __func__,
1772                                             ni->ni_macaddr,
1773                                             ":");
1774                                 } else {
1775                                         ieee80211_vht_node_init(ni);
1776                                         ieee80211_vht_updateparams(ni, vhtcap, vhtopmode);
1777                                         ieee80211_setup_vht_rates(ni, vhtcap, vhtopmode);
1778                                 }
1779                         }
1780
1781                         ieee80211_ht_updateparams_final(ni, htcap, htinfo);
1782                         ieee80211_setup_htrates(ni, htcap,
1783                              IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1784                         ieee80211_setup_basic_htrates(ni, htinfo);
1785
1786                         ieee80211_node_setuptxparms(ni);
1787                         ieee80211_ratectl_node_init(ni);
1788                 }
1789
1790                 /*
1791                  * Always initialise FF/superg state; we can use this
1792                  * for doing A-MSDU encapsulation as well.
1793                  */
1794 #ifdef  IEEE80211_SUPPORT_SUPERG
1795                 ieee80211_ff_node_init(ni);
1796 #endif
1797
1798                 /*
1799                  * Configure state now that we are associated.
1800                  *
1801                  * XXX may need different/additional driver callbacks?
1802                  */
1803                 if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1804                     (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1805                         ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1806                         ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1807                 } else {
1808                         ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1809                         ic->ic_flags |= IEEE80211_F_USEBARKER;
1810                 }
1811                 ieee80211_set_shortslottime(ic,
1812                         IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1813                         (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1814                 /*
1815                  * Honor ERP protection.
1816                  *
1817                  * NB: ni_erp should zero for non-11g operation.
1818                  */
1819                 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1820                     (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1821                         ic->ic_flags |= IEEE80211_F_USEPROT;
1822                 else
1823                         ic->ic_flags &= ~IEEE80211_F_USEPROT;
1824                 IEEE80211_NOTE_MAC(vap,
1825                     IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, wh->i_addr2,
1826                     "%sassoc success at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
1827                     ISREASSOC(subtype) ? "re" : "",
1828                     IEEE80211_NODE_AID(ni),
1829                     ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
1830                     ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
1831                     ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
1832                     ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
1833                     ni->ni_flags & IEEE80211_NODE_HT ?
1834                         (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
1835                     ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
1836                     ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
1837                         ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
1838                     ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
1839                     IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
1840                         ", fast-frames" : "",
1841                     IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
1842                         ", turbo" : ""
1843                 );
1844                 ieee80211_new_state(vap, IEEE80211_S_RUN, subtype);
1845                 break;
1846         }
1847
1848         case IEEE80211_FC0_SUBTYPE_DEAUTH: {
1849                 uint16_t reason;
1850
1851                 if (vap->iv_state == IEEE80211_S_SCAN) {
1852                         vap->iv_stats.is_rx_mgtdiscard++;
1853                         return;
1854                 }
1855                 if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1856                         /* NB: can happen when in promiscuous mode */
1857                         vap->iv_stats.is_rx_mgtdiscard++;
1858                         break;
1859                 }
1860
1861                 /*
1862                  * deauth frame format
1863                  *      [2] reason
1864                  */
1865                 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1866                 reason = le16toh(*(uint16_t *)frm);
1867
1868                 vap->iv_stats.is_rx_deauth++;
1869                 vap->iv_stats.is_rx_deauth_code = reason;
1870                 IEEE80211_NODE_STAT(ni, rx_deauth);
1871
1872                 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1873                     "recv deauthenticate (reason: %d (%s))", reason,
1874                     ieee80211_reason_to_string(reason));
1875                 ieee80211_new_state(vap, IEEE80211_S_AUTH,
1876                     (reason << 8) | IEEE80211_FC0_SUBTYPE_DEAUTH);
1877                 break;
1878         }
1879
1880         case IEEE80211_FC0_SUBTYPE_DISASSOC: {
1881                 uint16_t reason;
1882
1883                 if (vap->iv_state != IEEE80211_S_RUN &&
1884                     vap->iv_state != IEEE80211_S_ASSOC &&
1885                     vap->iv_state != IEEE80211_S_AUTH) {
1886                         vap->iv_stats.is_rx_mgtdiscard++;
1887                         return;
1888                 }
1889                 if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1890                         /* NB: can happen when in promiscuous mode */
1891                         vap->iv_stats.is_rx_mgtdiscard++;
1892                         break;
1893                 }
1894
1895                 /*
1896                  * disassoc frame format
1897                  *      [2] reason
1898                  */
1899                 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1900                 reason = le16toh(*(uint16_t *)frm);
1901
1902                 vap->iv_stats.is_rx_disassoc++;
1903                 vap->iv_stats.is_rx_disassoc_code = reason;
1904                 IEEE80211_NODE_STAT(ni, rx_disassoc);
1905
1906                 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1907                     "recv disassociate (reason: %d (%s))", reason,
1908                     ieee80211_reason_to_string(reason));
1909                 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1910                 break;
1911         }
1912
1913         case IEEE80211_FC0_SUBTYPE_ACTION:
1914         case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
1915                 if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
1916                     !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1917                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1918                             wh, NULL, "%s", "not for us");
1919                         vap->iv_stats.is_rx_mgtdiscard++;
1920                 } else if (vap->iv_state != IEEE80211_S_RUN) {
1921                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1922                             wh, NULL, "wrong state %s",
1923                             ieee80211_state_name[vap->iv_state]);
1924                         vap->iv_stats.is_rx_mgtdiscard++;
1925                 } else {
1926                         if (ieee80211_parse_action(ni, m0) == 0)
1927                                 (void)ic->ic_recv_action(ni, wh, frm, efrm);
1928                 }
1929                 break;
1930
1931         case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1932         case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1933         case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1934         case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
1935         case IEEE80211_FC0_SUBTYPE_ATIM:
1936                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1937                     wh, NULL, "%s", "not handled");
1938                 vap->iv_stats.is_rx_mgtdiscard++;
1939                 break;
1940
1941         default:
1942                 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1943                     wh, "mgt", "subtype 0x%x not handled", subtype);
1944                 vap->iv_stats.is_rx_badsubtype++;
1945                 break;
1946         }
1947 #undef ISREASSOC
1948 }
1949
1950 static void
1951 sta_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
1952 {
1953         switch (subtype) {
1954         case IEEE80211_FC0_SUBTYPE_BAR:
1955                 ieee80211_recv_bar(ni, m);
1956                 break;
1957         }
1958 }