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