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