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