2 * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 * IEEE 802.11 Station mode support.
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/endian.h>
46 #include <sys/errno.h>
48 #include <sys/sysctl.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>
59 #include <net80211/ieee80211_var.h>
60 #include <net80211/ieee80211_sta.h>
61 #include <net80211/ieee80211_input.h>
62 #ifdef IEEE80211_SUPPORT_SUPERG
63 #include <net80211/ieee80211_superg.h>
65 #include <net80211/ieee80211_ratectl.h>
66 #include <net80211/ieee80211_sta.h>
68 #define IEEE80211_RATE2MBS(r) (((r) & IEEE80211_RATE_VAL) / 2)
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 *,
74 const struct ieee80211_rx_stats *, int, int);
75 static void sta_recv_mgmt(struct ieee80211_node *, struct mbuf *,
76 int subtype, const struct ieee80211_rx_stats *, int rssi, int nf);
77 static void sta_recv_ctl(struct ieee80211_node *, struct mbuf *, int subtype);
80 ieee80211_sta_attach(struct ieee80211com *ic)
82 ic->ic_vattach[IEEE80211_M_STA] = sta_vattach;
86 ieee80211_sta_detach(struct ieee80211com *ic)
91 sta_vdetach(struct ieee80211vap *vap)
96 sta_vattach(struct ieee80211vap *vap)
98 vap->iv_newstate = sta_newstate;
99 vap->iv_input = sta_input;
100 vap->iv_recv_mgmt = sta_recv_mgmt;
101 vap->iv_recv_ctl = sta_recv_ctl;
102 vap->iv_opdetach = sta_vdetach;
103 vap->iv_bmiss = sta_beacon_miss;
107 * Handle a beacon miss event. The common code filters out
108 * spurious events that can happen when scanning and/or before
109 * reaching RUN state.
112 sta_beacon_miss(struct ieee80211vap *vap)
114 struct ieee80211com *ic = vap->iv_ic;
116 IEEE80211_LOCK_ASSERT(ic);
118 KASSERT((ic->ic_flags & IEEE80211_F_SCAN) == 0, ("scanning"));
119 KASSERT(vap->iv_state >= IEEE80211_S_RUN,
120 ("wrong state %s", ieee80211_state_name[vap->iv_state]));
122 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
123 "beacon miss, mode %s state %s\n",
124 ieee80211_opmode_name[vap->iv_opmode],
125 ieee80211_state_name[vap->iv_state]);
127 if (vap->iv_state == IEEE80211_S_CSA) {
129 * A Channel Switch is pending; assume we missed the
130 * beacon that would've completed the process and just
131 * force the switch. If we made a mistake we'll not
132 * find the AP on the new channel and fall back to a
135 ieee80211_csa_completeswitch(ic);
138 if (++vap->iv_bmiss_count < vap->iv_bmiss_max) {
140 * Send a directed probe req before falling back to a
141 * scan; if we receive a response ic_bmiss_count will
142 * be reset. Some cards mistakenly report beacon miss
143 * so this avoids the expensive scan if the ap is
146 ieee80211_send_probereq(vap->iv_bss, vap->iv_myaddr,
147 vap->iv_bss->ni_bssid, vap->iv_bss->ni_bssid,
148 vap->iv_bss->ni_essid, vap->iv_bss->ni_esslen);
152 callout_stop(&vap->iv_swbmiss);
153 vap->iv_bmiss_count = 0;
154 vap->iv_stats.is_beacon_miss++;
155 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
156 #ifdef IEEE80211_SUPPORT_SUPERG
159 * If we receive a beacon miss interrupt when using
160 * dynamic turbo, attempt to switch modes before
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);
168 * Try to reassociate before scanning for a new ap.
170 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
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.
178 ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
183 * Handle deauth with reason. We retry only for
184 * the cases where we might succeed. Otherwise
185 * we downgrade the ap and scan.
188 sta_authretry(struct ieee80211vap *vap, struct ieee80211_node *ni, int 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);
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);
209 sta_swbmiss_start(struct ieee80211vap *vap)
212 if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) {
214 * Start s/w beacon miss timer for devices w/o
215 * hardware support. We fudge a bit here since
216 * we're doing this in software.
218 vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS(
219 2 * vap->iv_bmissthreshold * vap->iv_bss->ni_intval);
220 vap->iv_swbmiss_count = 0;
221 callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
222 ieee80211_swbmiss, vap);
227 * IEEE80211_M_STA vap state machine handler.
228 * This routine handles the main states in the 802.11 protocol.
231 sta_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
233 struct ieee80211com *ic = vap->iv_ic;
234 struct ieee80211_node *ni;
235 enum ieee80211_state ostate;
237 IEEE80211_LOCK_ASSERT(ic);
239 ostate = vap->iv_state;
240 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
241 __func__, ieee80211_state_name[ostate],
242 ieee80211_state_name[nstate], arg);
243 vap->iv_state = nstate; /* state transition */
244 callout_stop(&vap->iv_mgtsend); /* XXX callout_drain */
245 if (ostate != IEEE80211_S_SCAN)
246 ieee80211_cancel_scan(vap); /* background scan */
247 ni = vap->iv_bss; /* NB: no reference held */
248 if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)
249 callout_stop(&vap->iv_swbmiss);
251 case IEEE80211_S_INIT:
253 case IEEE80211_S_SLEEP:
255 /* XXX driver hook to wakeup the hardware? */
256 case IEEE80211_S_RUN:
257 IEEE80211_SEND_MGMT(ni,
258 IEEE80211_FC0_SUBTYPE_DISASSOC,
259 IEEE80211_REASON_ASSOC_LEAVE);
260 ieee80211_sta_leave(ni);
262 case IEEE80211_S_ASSOC:
263 IEEE80211_SEND_MGMT(ni,
264 IEEE80211_FC0_SUBTYPE_DEAUTH,
265 IEEE80211_REASON_AUTH_LEAVE);
267 case IEEE80211_S_SCAN:
268 ieee80211_cancel_scan(vap);
273 if (ostate != IEEE80211_S_INIT) {
274 /* NB: optimize INIT -> INIT case */
275 ieee80211_reset_bss(vap);
277 if (vap->iv_auth->ia_detach != NULL)
278 vap->iv_auth->ia_detach(vap);
280 case IEEE80211_S_SCAN:
282 case IEEE80211_S_INIT:
284 * Initiate a scan. We can come here as a result
285 * of an IEEE80211_IOC_SCAN_REQ too in which case
286 * the vap will be marked with IEEE80211_FEXT_SCANREQ
287 * and the scan request parameters will be present
288 * in iv_scanreq. Otherwise we do the default.
290 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
291 ieee80211_check_scan(vap,
292 vap->iv_scanreq_flags,
293 vap->iv_scanreq_duration,
294 vap->iv_scanreq_mindwell,
295 vap->iv_scanreq_maxdwell,
296 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
297 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
299 ieee80211_check_scan_current(vap);
301 case IEEE80211_S_SCAN:
302 case IEEE80211_S_AUTH:
303 case IEEE80211_S_ASSOC:
305 * These can happen either because of a timeout
306 * on an assoc/auth response or because of a
307 * change in state that requires a reset. For
308 * the former we're called with a non-zero arg
309 * that is the cause for the failure; pass this
310 * to the scan code so it can update state.
311 * Otherwise trigger a new scan unless we're in
312 * manual roaming mode in which case an application
313 * must issue an explicit scan request.
316 ieee80211_scan_assoc_fail(vap,
317 vap->iv_bss->ni_macaddr, arg);
318 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
319 ieee80211_check_scan_current(vap);
321 case IEEE80211_S_SLEEP: /* beacon miss */
323 * XXX if in sleep we need to wakeup the hardware.
326 case IEEE80211_S_RUN: /* beacon miss */
328 * Beacon miss. Notify user space and if not
329 * under control of a user application (roaming
330 * manual) kick off a scan to re-connect.
333 ieee80211_sta_leave(ni);
334 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
335 ieee80211_check_scan_current(vap);
341 case IEEE80211_S_AUTH:
343 case IEEE80211_S_INIT:
344 case IEEE80211_S_SCAN:
345 IEEE80211_SEND_MGMT(ni,
346 IEEE80211_FC0_SUBTYPE_AUTH, 1);
348 case IEEE80211_S_AUTH:
349 case IEEE80211_S_ASSOC:
350 switch (arg & 0xff) {
351 case IEEE80211_FC0_SUBTYPE_AUTH:
353 IEEE80211_SEND_MGMT(ni,
354 IEEE80211_FC0_SUBTYPE_AUTH, 2);
356 case IEEE80211_FC0_SUBTYPE_DEAUTH:
357 sta_authretry(vap, ni, arg>>8);
361 case IEEE80211_S_SLEEP:
362 case IEEE80211_S_RUN:
363 switch (arg & 0xff) {
364 case IEEE80211_FC0_SUBTYPE_AUTH:
365 IEEE80211_SEND_MGMT(ni,
366 IEEE80211_FC0_SUBTYPE_AUTH, 2);
367 vap->iv_state = IEEE80211_S_RUN; /* stay RUN */
369 case IEEE80211_FC0_SUBTYPE_DEAUTH:
370 ieee80211_sta_leave(ni);
371 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
373 IEEE80211_SEND_MGMT(ni,
374 IEEE80211_FC0_SUBTYPE_AUTH, 1);
383 case IEEE80211_S_ASSOC:
385 case IEEE80211_S_AUTH:
386 case IEEE80211_S_ASSOC:
387 IEEE80211_SEND_MGMT(ni,
388 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
390 case IEEE80211_S_SLEEP: /* cannot happen */
391 case IEEE80211_S_RUN:
392 ieee80211_sta_leave(ni);
393 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
394 IEEE80211_SEND_MGMT(ni, arg ?
395 IEEE80211_FC0_SUBTYPE_REASSOC_REQ :
396 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
403 case IEEE80211_S_RUN:
404 if (vap->iv_flags & IEEE80211_F_WPA) {
405 /* XXX validate prerequisites */
408 case IEEE80211_S_RUN:
409 case IEEE80211_S_CSA:
411 case IEEE80211_S_AUTH: /* when join is done in fw */
412 case IEEE80211_S_ASSOC:
413 #ifdef IEEE80211_DEBUG
414 if (ieee80211_msg_debug(vap)) {
415 ieee80211_note(vap, "%s with %s ssid ",
416 (vap->iv_opmode == IEEE80211_M_STA ?
417 "associated" : "synchronized"),
418 ether_sprintf(ni->ni_bssid));
419 ieee80211_print_essid(vap->iv_bss->ni_essid,
422 printf(" channel %d start %uMb\n",
423 ieee80211_chan2ieee(ic, ic->ic_curchan),
424 IEEE80211_RATE2MBS(ni->ni_txrate));
427 ieee80211_scan_assoc_success(vap, ni->ni_macaddr);
428 ieee80211_notify_node_join(ni,
429 arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
431 case IEEE80211_S_SLEEP:
432 /* Wake up from sleep */
433 vap->iv_sta_ps(vap, 0);
438 ieee80211_sync_curchan(ic);
439 if (ostate != IEEE80211_S_RUN)
440 sta_swbmiss_start(vap);
442 * When 802.1x is not in use mark the port authorized
443 * at this point so traffic can flow.
445 if (ni->ni_authmode != IEEE80211_AUTH_8021X)
446 ieee80211_node_authorize(ni);
448 * Fake association when joining an existing bss.
450 * Don't do this if we're doing SLEEP->RUN.
452 if (ic->ic_newassoc != NULL && ostate != IEEE80211_S_SLEEP)
453 ic->ic_newassoc(vap->iv_bss, (ostate != IEEE80211_S_RUN));
455 case IEEE80211_S_CSA:
456 if (ostate != IEEE80211_S_RUN)
459 case IEEE80211_S_SLEEP:
460 sta_swbmiss_start(vap);
461 vap->iv_sta_ps(vap, 1);
465 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
466 "%s: unexpected state transition %s -> %s\n", __func__,
467 ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
474 * Return non-zero if the frame is an echo of a multicast
475 * frame sent by ourself. The dir is known to be DSTODS.
478 isdstods_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
480 #define QWH4(wh) ((const struct ieee80211_qosframe_addr4 *)wh)
481 #define WH4(wh) ((const struct ieee80211_frame_addr4 *)wh)
484 KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
486 if (!IEEE80211_IS_MULTICAST(wh->i_addr3))
488 sa = IEEE80211_QOS_HAS_SEQ(wh) ? QWH4(wh)->i_addr4 : WH4(wh)->i_addr4;
489 return IEEE80211_ADDR_EQ(sa, vap->iv_myaddr);
495 * Return non-zero if the frame is an echo of a multicast
496 * frame sent by ourself. The dir is known to be FROMDS.
499 isfromds_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
501 KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
503 if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
505 return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
509 * Decide if a received management frame should be
510 * printed when debugging is enabled. This filters some
511 * of the less interesting frames that come frequently
515 doprint(struct ieee80211vap *vap, int subtype)
518 case IEEE80211_FC0_SUBTYPE_BEACON:
519 return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
520 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
527 * Process a received frame. The node associated with the sender
528 * should be supplied. If nothing was found in the node table then
529 * the caller is assumed to supply a reference to iv_bss instead.
530 * The RSSI and a timestamp are also supplied. The RSSI data is used
531 * during AP scanning to select a AP to associate with; it can have
532 * any units so long as values have consistent units and higher values
533 * mean ``better signal''. The receive timestamp is currently not used
534 * by the 802.11 layer.
537 sta_input(struct ieee80211_node *ni, struct mbuf *m,
538 const struct ieee80211_rx_stats *rxs, int rssi, int nf)
540 struct ieee80211vap *vap = ni->ni_vap;
541 struct ieee80211com *ic = ni->ni_ic;
542 struct ifnet *ifp = vap->iv_ifp;
543 struct ieee80211_frame *wh;
544 struct ieee80211_key *key;
545 struct ether_header *eh;
546 int hdrspace, need_tap = 1; /* mbuf need to be tapped. */
547 uint8_t dir, type, subtype, qos;
550 if (m->m_flags & M_AMPDU_MPDU) {
552 * Fastpath for A-MPDU reorder q resubmission. Frames
553 * w/ M_AMPDU_MPDU marked have already passed through
554 * here but were received out of order and been held on
555 * the reorder queue. When resubmitted they are marked
556 * with the M_AMPDU_MPDU flag and we can bypass most of
557 * the normal processing.
559 wh = mtod(m, struct ieee80211_frame *);
560 type = IEEE80211_FC0_TYPE_DATA;
561 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
562 subtype = IEEE80211_FC0_SUBTYPE_QOS;
563 hdrspace = ieee80211_hdrspace(ic, wh); /* XXX optimize? */
567 KASSERT(ni != NULL, ("null node"));
568 ni->ni_inact = ni->ni_inact_reload;
570 type = -1; /* undefined */
572 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
573 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
574 ni->ni_macaddr, NULL,
575 "too short (1): len %u", m->m_pkthdr.len);
576 vap->iv_stats.is_rx_tooshort++;
580 * Bit of a cheat here, we use a pointer for a 3-address
581 * frame format but don't reference fields past outside
582 * ieee80211_frame_min w/o first validating the data is
585 wh = mtod(m, struct ieee80211_frame *);
587 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
588 IEEE80211_FC0_VERSION_0) {
589 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
590 ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
591 wh->i_fc[0], wh->i_fc[1]);
592 vap->iv_stats.is_rx_badversion++;
596 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
597 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
598 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
599 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
601 if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) {
602 /* not interested in */
603 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
604 bssid, NULL, "%s", "not to bss");
605 vap->iv_stats.is_rx_wrongbss++;
610 * Some devices may be in a promiscuous mode
611 * where they receive frames for multiple station
614 * If we receive a data frame that isn't
615 * destined to our VAP MAC, drop it.
617 * XXX TODO: This is only enforced when not scanning;
618 * XXX it assumes a software-driven scan will put the NIC
619 * XXX into a "no data frames" mode before setting this
620 * XXX flag. Otherwise it may be possible that we'll still
621 * XXX process data frames whilst scanning.
623 if ((! IEEE80211_IS_MULTICAST(wh->i_addr1))
624 && (! IEEE80211_ADDR_EQ(wh->i_addr1, IF_LLADDR(ifp)))) {
625 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
626 bssid, NULL, "not to cur sta: lladdr=%6D, addr1=%6D",
627 IF_LLADDR(ifp), ":", wh->i_addr1, ":");
628 vap->iv_stats.is_rx_wrongbss++;
632 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
634 if ( IEEE80211_HAS_SEQ(type, subtype) &&
635 !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
636 uint8_t tid = ieee80211_gettid(wh);
637 if (IEEE80211_QOS_HAS_SEQ(wh) &&
638 TID_TO_WME_AC(tid) >= WME_AC_VI)
639 ic->ic_wme.wme_hipri_traffic++;
640 if (! ieee80211_check_rxseq(ni, wh, bssid))
646 case IEEE80211_FC0_TYPE_DATA:
647 hdrspace = ieee80211_hdrspace(ic, wh);
648 if (m->m_len < hdrspace &&
649 (m = m_pullup(m, hdrspace)) == NULL) {
650 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
651 ni->ni_macaddr, NULL,
652 "data too short: expecting %u", hdrspace);
653 vap->iv_stats.is_rx_tooshort++;
657 * Handle A-MPDU re-ordering. If the frame is to be
658 * processed directly then ieee80211_ampdu_reorder
659 * will return 0; otherwise it has consumed the mbuf
660 * and we should do nothing more with it.
662 if ((m->m_flags & M_AMPDU) &&
663 (dir == IEEE80211_FC1_DIR_FROMDS ||
664 dir == IEEE80211_FC1_DIR_DSTODS) &&
665 ieee80211_ampdu_reorder(ni, m) != 0) {
670 if (dir == IEEE80211_FC1_DIR_FROMDS) {
671 if ((ifp->if_flags & IFF_SIMPLEX) &&
672 isfromds_mcastecho(vap, wh)) {
674 * In IEEE802.11 network, multicast
675 * packets sent from "me" are broadcast
676 * from the AP; silently discard for
679 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
680 wh, "data", "%s", "multicast echo");
681 vap->iv_stats.is_rx_mcastecho++;
684 if ((vap->iv_flags & IEEE80211_F_DWDS) &&
685 IEEE80211_IS_MULTICAST(wh->i_addr1)) {
687 * DWDS sta's must drop 3-address mcast frames
688 * as they will be sent separately as a 4-addr
689 * frame. Accepting the 3-addr frame will
690 * confuse the bridge into thinking the sending
691 * sta is located at the end of WDS link.
693 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
694 "3-address data", "%s", "DWDS enabled");
695 vap->iv_stats.is_rx_mcastecho++;
698 } else if (dir == IEEE80211_FC1_DIR_DSTODS) {
699 if ((vap->iv_flags & IEEE80211_F_DWDS) == 0) {
700 IEEE80211_DISCARD(vap,
701 IEEE80211_MSG_INPUT, wh, "4-address data",
702 "%s", "DWDS not enabled");
703 vap->iv_stats.is_rx_wrongdir++;
706 if ((ifp->if_flags & IFF_SIMPLEX) &&
707 isdstods_mcastecho(vap, wh)) {
709 * In IEEE802.11 network, multicast
710 * packets sent from "me" are broadcast
711 * from the AP; silently discard for
714 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
715 "4-address data", "%s", "multicast echo");
716 vap->iv_stats.is_rx_mcastecho++;
720 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
721 "data", "incorrect dir 0x%x", dir);
722 vap->iv_stats.is_rx_wrongdir++;
727 * Handle privacy requirements. Note that we
728 * must not be preempted from here until after
729 * we (potentially) call ieee80211_crypto_demic;
730 * otherwise we may violate assumptions in the
731 * crypto cipher modules used to do delayed update
732 * of replay sequence numbers.
734 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
735 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
737 * Discard encrypted frames when privacy is off.
739 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
740 wh, "WEP", "%s", "PRIVACY off");
741 vap->iv_stats.is_rx_noprivacy++;
742 IEEE80211_NODE_STAT(ni, rx_noprivacy);
745 key = ieee80211_crypto_decap(ni, m, hdrspace);
747 /* NB: stats+msgs handled in crypto_decap */
748 IEEE80211_NODE_STAT(ni, rx_wepfail);
751 wh = mtod(m, struct ieee80211_frame *);
752 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
754 /* XXX M_WEP and IEEE80211_F_PRIVACY */
759 * Save QoS bits for use below--before we strip the header.
761 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
762 qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
763 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
764 ((struct ieee80211_qosframe *)wh)->i_qos[0];
769 * Next up, any fragmentation.
771 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
772 m = ieee80211_defrag(ni, m, hdrspace);
774 /* Fragment dropped or frame not complete yet */
778 wh = NULL; /* no longer valid, catch any uses */
781 * Next strip any MSDU crypto bits.
783 if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
784 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
785 ni->ni_macaddr, "data", "%s", "demic error");
786 vap->iv_stats.is_rx_demicfail++;
787 IEEE80211_NODE_STAT(ni, rx_demicfail);
791 /* copy to listener after decrypt */
792 if (ieee80211_radiotap_active_vap(vap))
793 ieee80211_radiotap_rx(vap, m);
797 * Finally, strip the 802.11 header.
799 m = ieee80211_decap(vap, m, hdrspace);
801 /* XXX mask bit to check for both */
802 /* don't count Null data frames as errors */
803 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
804 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
806 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
807 ni->ni_macaddr, "data", "%s", "decap error");
808 vap->iv_stats.is_rx_decap++;
809 IEEE80211_NODE_STAT(ni, rx_decap);
812 eh = mtod(m, struct ether_header *);
813 if (!ieee80211_node_is_authorized(ni)) {
815 * Deny any non-PAE frames received prior to
816 * authorization. For open/shared-key
817 * authentication the port is mark authorized
818 * after authentication completes. For 802.1x
819 * the port is not marked authorized by the
820 * authenticator until the handshake has completed.
822 if (eh->ether_type != htons(ETHERTYPE_PAE)) {
823 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
824 eh->ether_shost, "data",
825 "unauthorized port: ether type 0x%x len %u",
826 eh->ether_type, m->m_pkthdr.len);
827 vap->iv_stats.is_rx_unauth++;
828 IEEE80211_NODE_STAT(ni, rx_unauth);
833 * When denying unencrypted frames, discard
834 * any non-PAE frames received without encryption.
836 if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
837 (key == NULL && (m->m_flags & M_WEP) == 0) &&
838 eh->ether_type != htons(ETHERTYPE_PAE)) {
840 * Drop unencrypted frames.
842 vap->iv_stats.is_rx_unencrypted++;
843 IEEE80211_NODE_STAT(ni, rx_unencrypted);
847 /* XXX require HT? */
848 if (qos & IEEE80211_QOS_AMSDU) {
849 m = ieee80211_decap_amsdu(ni, m);
851 return IEEE80211_FC0_TYPE_DATA;
853 #ifdef IEEE80211_SUPPORT_SUPERG
854 m = ieee80211_decap_fastframe(vap, ni, m);
856 return IEEE80211_FC0_TYPE_DATA;
859 ieee80211_deliver_data(vap, ni, m);
860 return IEEE80211_FC0_TYPE_DATA;
862 case IEEE80211_FC0_TYPE_MGT:
863 vap->iv_stats.is_rx_mgmt++;
864 IEEE80211_NODE_STAT(ni, rx_mgmt);
865 if (dir != IEEE80211_FC1_DIR_NODS) {
866 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
867 wh, "data", "incorrect dir 0x%x", dir);
868 vap->iv_stats.is_rx_wrongdir++;
871 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
872 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
873 ni->ni_macaddr, "mgt", "too short: len %u",
875 vap->iv_stats.is_rx_tooshort++;
878 #ifdef IEEE80211_DEBUG
879 if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
880 ieee80211_msg_dumppkts(vap)) {
881 if_printf(ifp, "received %s from %s rssi %d\n",
882 ieee80211_mgt_subtype_name(subtype),
883 ether_sprintf(wh->i_addr2), rssi);
886 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
887 if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
889 * Only shared key auth frames with a challenge
890 * should be encrypted, discard all others.
892 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
893 wh, ieee80211_mgt_subtype_name(subtype),
894 "%s", "WEP set but not permitted");
895 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
898 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
900 * Discard encrypted frames when privacy is off.
902 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
903 wh, "mgt", "%s", "WEP set but PRIVACY off");
904 vap->iv_stats.is_rx_noprivacy++;
907 hdrspace = ieee80211_hdrspace(ic, wh);
908 key = ieee80211_crypto_decap(ni, m, hdrspace);
910 /* NB: stats+msgs handled in crypto_decap */
913 wh = mtod(m, struct ieee80211_frame *);
914 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
916 vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
919 case IEEE80211_FC0_TYPE_CTL:
920 vap->iv_stats.is_rx_ctl++;
921 IEEE80211_NODE_STAT(ni, rx_ctrl);
922 vap->iv_recv_ctl(ni, m, subtype);
926 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
927 wh, NULL, "bad frame type 0x%x", type);
928 /* should not come here */
932 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
935 if (need_tap && ieee80211_radiotap_active_vap(vap))
936 ieee80211_radiotap_rx(vap, m);
943 sta_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
944 int rssi, int nf, uint16_t seq, uint16_t status)
946 struct ieee80211vap *vap = ni->ni_vap;
948 if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
949 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
950 ni->ni_macaddr, "open auth",
951 "bad sta auth mode %u", ni->ni_authmode);
952 vap->iv_stats.is_rx_bad_auth++; /* XXX */
955 if (vap->iv_state != IEEE80211_S_AUTH ||
956 seq != IEEE80211_AUTH_OPEN_RESPONSE) {
957 vap->iv_stats.is_rx_bad_auth++;
961 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
962 ni, "open auth failed (reason %d)", status);
963 vap->iv_stats.is_rx_auth_fail++;
964 vap->iv_stats.is_rx_authfail_code = status;
965 ieee80211_new_state(vap, IEEE80211_S_SCAN,
966 IEEE80211_SCAN_FAIL_STATUS);
968 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
972 sta_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
973 uint8_t *frm, uint8_t *efrm, int rssi, int nf,
974 uint16_t seq, uint16_t status)
976 struct ieee80211vap *vap = ni->ni_vap;
980 * NB: this can happen as we allow pre-shared key
981 * authentication to be enabled w/o wep being turned
982 * on so that configuration of these can be done
983 * in any order. It may be better to enforce the
984 * ordering in which case this check would just be
985 * for sanity/consistency.
987 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
988 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
989 ni->ni_macaddr, "shared key auth",
990 "%s", " PRIVACY is disabled");
994 * Pre-shared key authentication is evil; accept
995 * it only if explicitly configured (it is supported
996 * mainly for compatibility with clients like OS X).
998 if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
999 ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1000 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1001 ni->ni_macaddr, "shared key auth",
1002 "bad sta auth mode %u", ni->ni_authmode);
1003 vap->iv_stats.is_rx_bad_auth++; /* XXX maybe a unique error? */
1008 if (frm + 1 < efrm) {
1009 if ((frm[1] + 2) > (efrm - frm)) {
1010 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1011 ni->ni_macaddr, "shared key auth",
1012 "ie %d/%d too long",
1013 frm[0], (frm[1] + 2) - (efrm - frm));
1014 vap->iv_stats.is_rx_bad_auth++;
1017 if (*frm == IEEE80211_ELEMID_CHALLENGE)
1022 case IEEE80211_AUTH_SHARED_CHALLENGE:
1023 case IEEE80211_AUTH_SHARED_RESPONSE:
1024 if (challenge == NULL) {
1025 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1026 ni->ni_macaddr, "shared key auth",
1027 "%s", "no challenge");
1028 vap->iv_stats.is_rx_bad_auth++;
1031 if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1032 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1033 ni->ni_macaddr, "shared key auth",
1034 "bad challenge len %d", challenge[1]);
1035 vap->iv_stats.is_rx_bad_auth++;
1041 if (vap->iv_state != IEEE80211_S_AUTH)
1044 case IEEE80211_AUTH_SHARED_PASS:
1045 if (ni->ni_challenge != NULL) {
1046 IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
1047 ni->ni_challenge = NULL;
1050 IEEE80211_NOTE_FRAME(vap,
1051 IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, wh,
1052 "shared key auth failed (reason %d)", status);
1053 vap->iv_stats.is_rx_auth_fail++;
1054 vap->iv_stats.is_rx_authfail_code = status;
1057 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1059 case IEEE80211_AUTH_SHARED_CHALLENGE:
1060 if (!ieee80211_alloc_challenge(ni))
1062 /* XXX could optimize by passing recvd challenge */
1063 memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
1064 IEEE80211_SEND_MGMT(ni,
1065 IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1068 IEEE80211_DISCARD(vap, IEEE80211_MSG_AUTH,
1069 wh, "shared key auth", "bad seq %d", seq);
1070 vap->iv_stats.is_rx_bad_auth++;
1076 * Kick the state machine. This short-circuits
1077 * using the mgt frame timeout to trigger the
1080 if (vap->iv_state == IEEE80211_S_AUTH)
1081 ieee80211_new_state(vap, IEEE80211_S_SCAN,
1082 IEEE80211_SCAN_FAIL_STATUS);
1086 ieee80211_parse_wmeparams(struct ieee80211vap *vap, uint8_t *frm,
1087 const struct ieee80211_frame *wh)
1089 #define MS(_v, _f) (((_v) & _f) >> _f##_S)
1090 struct ieee80211_wme_state *wme = &vap->iv_ic->ic_wme;
1091 u_int len = frm[1], qosinfo;
1094 if (len < sizeof(struct ieee80211_wme_param)-2) {
1095 IEEE80211_DISCARD_IE(vap,
1096 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
1097 wh, "WME", "too short, len %u", len);
1100 qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)];
1101 qosinfo &= WME_QOSINFO_COUNT;
1102 /* XXX do proper check for wraparound */
1103 if (qosinfo == wme->wme_wmeChanParams.cap_info)
1105 frm += __offsetof(struct ieee80211_wme_param, params_acParams);
1106 for (i = 0; i < WME_NUM_AC; i++) {
1107 struct wmeParams *wmep =
1108 &wme->wme_wmeChanParams.cap_wmeParams[i];
1109 /* NB: ACI not used */
1110 wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
1111 wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
1112 wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
1113 wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
1114 wmep->wmep_txopLimit = le16dec(frm+2);
1117 wme->wme_wmeChanParams.cap_info = qosinfo;
1123 * Process 11h Channel Switch Announcement (CSA) ie. If this
1124 * is the first CSA then initiate the switch. Otherwise we
1125 * track state and trigger completion and/or cancel of the switch.
1126 * XXX should be public for IBSS use
1129 ieee80211_parse_csaparams(struct ieee80211vap *vap, uint8_t *frm,
1130 const struct ieee80211_frame *wh)
1132 struct ieee80211com *ic = vap->iv_ic;
1133 const struct ieee80211_csa_ie *csa =
1134 (const struct ieee80211_csa_ie *) frm;
1136 KASSERT(vap->iv_state >= IEEE80211_S_RUN,
1137 ("state %s", ieee80211_state_name[vap->iv_state]));
1139 if (csa->csa_mode > 1) {
1140 IEEE80211_DISCARD_IE(vap,
1141 IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1142 wh, "CSA", "invalid mode %u", csa->csa_mode);
1146 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
1148 * Convert the channel number to a channel reference. We
1149 * try first to preserve turbo attribute of the current
1150 * channel then fallback. Note this will not work if the
1151 * CSA specifies a channel that requires a band switch (e.g.
1152 * 11a => 11g). This is intentional as 11h is defined only
1153 * for 5GHz/11a and because the switch does not involve a
1154 * reassociation, protocol state (capabilities, negotated
1155 * rates, etc) may/will be wrong.
1157 struct ieee80211_channel *c =
1158 ieee80211_find_channel_byieee(ic, csa->csa_newchan,
1159 (ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALLTURBO));
1161 c = ieee80211_find_channel_byieee(ic,
1163 (ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALL));
1165 IEEE80211_DISCARD_IE(vap,
1166 IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1167 wh, "CSA", "invalid channel %u",
1172 #if IEEE80211_CSA_COUNT_MIN > 0
1173 if (csa->csa_count < IEEE80211_CSA_COUNT_MIN) {
1175 * Require at least IEEE80211_CSA_COUNT_MIN count to
1176 * reduce the risk of being redirected by a fabricated
1177 * CSA. If a valid CSA is dropped we'll still get a
1178 * beacon miss when the AP leaves the channel so we'll
1179 * eventually follow to the new channel.
1181 * NOTE: this violates the 11h spec that states that
1182 * count may be any value and if 0 then a switch
1183 * should happen asap.
1185 IEEE80211_DISCARD_IE(vap,
1186 IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1187 wh, "CSA", "count %u too small, must be >= %u",
1188 csa->csa_count, IEEE80211_CSA_COUNT_MIN);
1192 ieee80211_csa_startswitch(ic, c, csa->csa_mode, csa->csa_count);
1195 * Validate this ie against the initial CSA. We require
1196 * mode and channel not change and the count must be
1197 * monotonically decreasing. This may be pointless and
1198 * canceling the switch as a result may be too paranoid but
1199 * in the worst case if we drop out of CSA because of this
1200 * and the AP does move then we'll just end up taking a
1201 * beacon miss and scan to find the AP.
1203 * XXX may want <= on count as we also process ProbeResp
1204 * frames and those may come in w/ the same count as the
1205 * previous beacon; but doing so leaves us open to a stuck
1206 * count until we add a dead-man timer
1208 if (!(csa->csa_count < ic->ic_csa_count &&
1209 csa->csa_mode == ic->ic_csa_mode &&
1210 csa->csa_newchan == ieee80211_chan2ieee(ic, ic->ic_csa_newchan))) {
1211 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_DOTH, wh,
1212 "CSA ie mismatch, initial ie <%d,%d,%d>, "
1213 "this ie <%d,%d,%d>", ic->ic_csa_mode,
1214 ic->ic_csa_newchan, ic->ic_csa_count,
1215 csa->csa_mode, csa->csa_newchan, csa->csa_count);
1216 ieee80211_csa_cancelswitch(ic);
1218 if (csa->csa_count <= 1)
1219 ieee80211_csa_completeswitch(ic);
1221 ic->ic_csa_count = csa->csa_count;
1225 IEEE80211_UNLOCK(ic);
1229 * Return non-zero if a background scan may be continued:
1230 * o bg scan is active
1231 * o no channel switch is pending
1232 * o there has not been any traffic recently
1234 * Note we do not check if there is an administrative enable;
1235 * this is only done to start the scan. We assume that any
1236 * change in state will be accompanied by a request to cancel
1237 * active scans which will otherwise cause this test to fail.
1240 contbgscan(struct ieee80211vap *vap)
1242 struct ieee80211com *ic = vap->iv_ic;
1244 return ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) &&
1245 (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1246 vap->iv_state == IEEE80211_S_RUN && /* XXX? */
1247 ieee80211_time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1251 * Return non-zero if a backgrond scan may be started:
1252 * o bg scanning is administratively enabled
1253 * o no channel switch is pending
1254 * o we are not boosted on a dynamic turbo channel
1255 * o there has not been a scan recently
1256 * o there has not been any traffic recently
1259 startbgscan(struct ieee80211vap *vap)
1261 struct ieee80211com *ic = vap->iv_ic;
1263 return ((vap->iv_flags & IEEE80211_F_BGSCAN) &&
1264 (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1265 #ifdef IEEE80211_SUPPORT_SUPERG
1266 !IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1268 ieee80211_time_after(ticks, ic->ic_lastscan + vap->iv_bgscanintvl) &&
1269 ieee80211_time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1273 sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
1274 const struct ieee80211_rx_stats *rxs,
1277 #define ISREASSOC(_st) ((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
1278 struct ieee80211vap *vap = ni->ni_vap;
1279 struct ieee80211com *ic = ni->ni_ic;
1280 struct ieee80211_channel *rxchan = ic->ic_curchan;
1281 struct ieee80211_frame *wh;
1282 uint8_t *frm, *efrm;
1283 uint8_t *rates, *xrates, *wme, *htcap, *htinfo;
1285 int ht_state_change = 0;
1287 wh = mtod(m0, struct ieee80211_frame *);
1288 frm = (uint8_t *)&wh[1];
1289 efrm = mtod(m0, uint8_t *) + m0->m_len;
1291 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1292 case IEEE80211_FC0_SUBTYPE_BEACON: {
1293 struct ieee80211_scanparams scan;
1294 struct ieee80211_channel *c;
1296 * We process beacon/probe response frames:
1297 * o when scanning, or
1298 * o station mode when associated (to collect state
1299 * updates such as 802.11g slot time)
1300 * Frames otherwise received are discarded.
1302 if (!((ic->ic_flags & IEEE80211_F_SCAN) || ni->ni_associd)) {
1303 vap->iv_stats.is_rx_mgtdiscard++;
1307 /* Override RX channel as appropriate */
1309 c = ieee80211_lookup_channel_rxstatus(vap, rxs);
1314 /* XXX probe response in sta mode when !scanning? */
1315 if (ieee80211_parse_beacon(ni, m0, rxchan, &scan) != 0) {
1316 if (! (ic->ic_flags & IEEE80211_F_SCAN))
1317 vap->iv_stats.is_beacon_bad++;
1322 * Count frame now that we know it's to be processed.
1324 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1325 vap->iv_stats.is_rx_beacon++; /* XXX remove */
1326 IEEE80211_NODE_STAT(ni, rx_beacons);
1328 IEEE80211_NODE_STAT(ni, rx_proberesp);
1330 * When operating in station mode, check for state updates.
1331 * Be careful to ignore beacons received while doing a
1332 * background scan. We consider only 11g/WMM stuff right now.
1334 if (ni->ni_associd != 0 &&
1335 ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
1336 IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
1337 /* record tsf of last beacon */
1338 memcpy(ni->ni_tstamp.data, scan.tstamp,
1339 sizeof(ni->ni_tstamp));
1340 /* count beacon frame for s/w bmiss handling */
1341 vap->iv_swbmiss_count++;
1342 vap->iv_bmiss_count = 0;
1343 if (ni->ni_erp != scan.erp) {
1344 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1346 "erp change: was 0x%x, now 0x%x",
1347 ni->ni_erp, scan.erp);
1348 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1349 (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1350 ic->ic_flags |= IEEE80211_F_USEPROT;
1352 ic->ic_flags &= ~IEEE80211_F_USEPROT;
1353 ni->ni_erp = scan.erp;
1355 /* XXX driver notification */
1357 if ((ni->ni_capinfo ^ scan.capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) {
1358 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1360 "capabilities change: was 0x%x, now 0x%x",
1361 ni->ni_capinfo, scan.capinfo);
1363 * NB: we assume short preamble doesn't
1364 * change dynamically
1366 ieee80211_set_shortslottime(ic,
1367 IEEE80211_IS_CHAN_A(ic->ic_bsschan) ||
1368 (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1369 ni->ni_capinfo = (ni->ni_capinfo &~ IEEE80211_CAPINFO_SHORT_SLOTTIME)
1370 | (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME);
1373 if (scan.wme != NULL &&
1374 (ni->ni_flags & IEEE80211_NODE_QOS) &&
1375 ieee80211_parse_wmeparams(vap, scan.wme, wh) > 0)
1376 ieee80211_wme_updateparams(vap);
1377 #ifdef IEEE80211_SUPPORT_SUPERG
1378 if (scan.ath != NULL)
1379 ieee80211_parse_athparams(ni, scan.ath, wh);
1381 if (scan.htcap != NULL && scan.htinfo != NULL &&
1382 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1383 /* XXX state changes? */
1384 if (ieee80211_ht_updateparams(ni,
1385 scan.htcap, scan.htinfo))
1386 ht_state_change = 1;
1389 ic->ic_set_quiet(ni, scan.quiet);
1391 if (scan.tim != NULL) {
1392 struct ieee80211_tim_ie *tim =
1393 (struct ieee80211_tim_ie *) scan.tim;
1395 * XXX Check/debug this code; see if it's about
1396 * the right time to force the VAP awake if we
1397 * receive a frame destined for us?
1399 int aid = IEEE80211_AID(ni->ni_associd);
1400 int ix = aid / NBBY;
1401 int min = tim->tim_bitctl &~ 1;
1402 int max = tim->tim_len + min - 4;
1403 int tim_ucast = 0, tim_mcast = 0;
1406 * Only do this for unicast traffic in the TIM
1407 * The multicast traffic notification for
1408 * the scan notification stuff should occur
1411 if (min <= ix && ix <= max &&
1412 isset(tim->tim_bitmap - min, aid)) {
1417 * Do a separate notification
1418 * for the multicast bit being set.
1420 if (tim->tim_bitctl & 1) {
1425 * If the TIM indicates there's traffic for
1426 * us then get us out of STA mode powersave.
1428 if (tim_ucast == 1) {
1431 * Wake us out of SLEEP state if we're
1432 * in it; and if we're doing bgscan
1433 * then wake us out of STA powersave.
1435 ieee80211_sta_tim_notify(vap, 1);
1438 * This is preventing us from
1439 * continuing a bgscan; because it
1440 * tricks the contbgscan()
1441 * routine to think there's always
1444 * I think we need both an RX and
1445 * TX ic_lastdata field.
1447 ic->ic_lastdata = ticks;
1450 ni->ni_dtim_count = tim->tim_count;
1451 ni->ni_dtim_period = tim->tim_period;
1453 if (scan.csa != NULL &&
1454 (vap->iv_flags & IEEE80211_F_DOTH))
1455 ieee80211_parse_csaparams(vap, scan.csa, wh);
1456 else if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
1458 * No CSA ie or 11h disabled, but a channel
1459 * switch is pending; drop out so we aren't
1460 * stuck in CSA state. If the AP really is
1461 * moving we'll get a beacon miss and scan.
1464 ieee80211_csa_cancelswitch(ic);
1465 IEEE80211_UNLOCK(ic);
1468 * If scanning, pass the info to the scan module.
1469 * Otherwise, check if it's the right time to do
1470 * a background scan. Background scanning must
1471 * be enabled and we must not be operating in the
1472 * turbo phase of dynamic turbo mode. Then,
1473 * it's been a while since the last background
1474 * scan and if no data frames have come through
1475 * recently, kick off a scan. Note that this
1476 * is the mechanism by which a background scan
1477 * is started _and_ continued each time we
1478 * return on-channel to receive a beacon from
1481 if (ic->ic_flags & IEEE80211_F_SCAN) {
1482 ieee80211_add_scan(vap, rxchan,
1483 &scan, wh, subtype, rssi, nf);
1484 } else if (contbgscan(vap)) {
1485 ieee80211_bg_scan(vap, 0);
1486 } else if (startbgscan(vap)) {
1487 vap->iv_stats.is_scan_bg++;
1489 /* wakeup if we are sleeing */
1490 ieee80211_set_pwrsave(vap, 0);
1492 ieee80211_bg_scan(vap, 0);
1496 * Put the station to sleep if we haven't seen
1497 * traffic in a while.
1500 ieee80211_sta_ps_timer_check(vap);
1501 IEEE80211_UNLOCK(ic);
1504 * If we've had a channel width change (eg HT20<->HT40)
1505 * then schedule a delayed driver notification.
1507 if (ht_state_change)
1508 ieee80211_update_chw(ic);
1512 * If scanning, just pass information to the scan module.
1514 if (ic->ic_flags & IEEE80211_F_SCAN) {
1515 if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1517 * Actively scanning a channel marked passive;
1518 * send a probe request now that we know there
1519 * is 802.11 traffic present.
1521 * XXX check if the beacon we recv'd gives
1522 * us what we need and suppress the probe req
1524 ieee80211_probe_curchan(vap, 1);
1525 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1527 ieee80211_add_scan(vap, rxchan, &scan, wh,
1534 case IEEE80211_FC0_SUBTYPE_AUTH: {
1535 uint16_t algo, seq, status;
1543 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1544 algo = le16toh(*(uint16_t *)frm);
1545 seq = le16toh(*(uint16_t *)(frm + 2));
1546 status = le16toh(*(uint16_t *)(frm + 4));
1547 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1548 "recv auth frame with algorithm %d seq %d", algo, seq);
1550 if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1551 IEEE80211_DISCARD(vap,
1552 IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1553 wh, "auth", "%s", "TKIP countermeasures enabled");
1554 vap->iv_stats.is_rx_auth_countermeasures++;
1555 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1556 ieee80211_send_error(ni, wh->i_addr2,
1557 IEEE80211_FC0_SUBTYPE_AUTH,
1558 IEEE80211_REASON_MIC_FAILURE);
1562 if (algo == IEEE80211_AUTH_ALG_SHARED)
1563 sta_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1565 else if (algo == IEEE80211_AUTH_ALG_OPEN)
1566 sta_auth_open(ni, wh, rssi, nf, seq, status);
1568 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1569 wh, "auth", "unsupported alg %d", algo);
1570 vap->iv_stats.is_rx_auth_unsupported++;
1576 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1577 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
1578 uint16_t capinfo, associd;
1581 if (vap->iv_state != IEEE80211_S_ASSOC) {
1582 vap->iv_stats.is_rx_mgtdiscard++;
1587 * asresp frame format
1588 * [2] capability information
1590 * [2] association ID
1591 * [tlv] supported rates
1592 * [tlv] extended supported rates
1594 * [tlv] HT capabilities
1597 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1599 capinfo = le16toh(*(uint16_t *)frm);
1601 status = le16toh(*(uint16_t *)frm);
1604 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1605 wh->i_addr2, "%sassoc failed (reason %d)",
1606 ISREASSOC(subtype) ? "re" : "", status);
1607 vap->iv_stats.is_rx_auth_fail++; /* XXX */
1610 associd = le16toh(*(uint16_t *)frm);
1613 rates = xrates = wme = htcap = htinfo = NULL;
1614 while (efrm - frm > 1) {
1615 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1617 case IEEE80211_ELEMID_RATES:
1620 case IEEE80211_ELEMID_XRATES:
1623 case IEEE80211_ELEMID_HTCAP:
1626 case IEEE80211_ELEMID_HTINFO:
1629 case IEEE80211_ELEMID_VENDOR:
1632 else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1634 * Accept pre-draft HT ie's if the
1635 * standard ones have not been seen.
1637 if (ishtcapoui(frm)) {
1640 } else if (ishtinfooui(frm)) {
1645 /* XXX Atheros OUI support */
1651 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1653 IEEE80211_VERIFY_ELEMENT(xrates,
1654 IEEE80211_RATE_MAXSIZE - rates[1], return);
1655 rate = ieee80211_setup_rates(ni, rates, xrates,
1657 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1658 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1659 if (rate & IEEE80211_RATE_BASIC) {
1660 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1662 "%sassoc failed (rate set mismatch)",
1663 ISREASSOC(subtype) ? "re" : "");
1664 vap->iv_stats.is_rx_assoc_norate++;
1665 ieee80211_new_state(vap, IEEE80211_S_SCAN,
1666 IEEE80211_SCAN_FAIL_STATUS);
1670 ni->ni_capinfo = capinfo;
1671 ni->ni_associd = associd;
1672 if (ni->ni_jointime == 0)
1673 ni->ni_jointime = time_uptime;
1675 ieee80211_parse_wmeparams(vap, wme, wh) >= 0) {
1676 ni->ni_flags |= IEEE80211_NODE_QOS;
1677 ieee80211_wme_updateparams(vap);
1679 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1681 * Setup HT state according to the negotiation.
1683 * NB: shouldn't need to check if HT use is enabled but some
1684 * ap's send back HT ie's even when we don't indicate we
1685 * are HT capable in our AssocReq.
1687 if (htcap != NULL && htinfo != NULL &&
1688 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1689 ieee80211_ht_node_init(ni);
1690 ieee80211_ht_updateparams(ni, htcap, htinfo);
1691 ieee80211_setup_htrates(ni, htcap,
1692 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1693 ieee80211_setup_basic_htrates(ni, htinfo);
1694 ieee80211_node_setuptxparms(ni);
1695 ieee80211_ratectl_node_init(ni);
1699 * Always initialise FF/superg state; we can use this
1700 * for doing A-MSDU encapsulation as well.
1702 #ifdef IEEE80211_SUPPORT_SUPERG
1703 ieee80211_ff_node_init(ni);
1707 * Configure state now that we are associated.
1709 * XXX may need different/additional driver callbacks?
1711 if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1712 (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1713 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1714 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1716 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1717 ic->ic_flags |= IEEE80211_F_USEBARKER;
1719 ieee80211_set_shortslottime(ic,
1720 IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1721 (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1723 * Honor ERP protection.
1725 * NB: ni_erp should zero for non-11g operation.
1727 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1728 (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1729 ic->ic_flags |= IEEE80211_F_USEPROT;
1731 ic->ic_flags &= ~IEEE80211_F_USEPROT;
1732 IEEE80211_NOTE_MAC(vap,
1733 IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, wh->i_addr2,
1734 "%sassoc success at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
1735 ISREASSOC(subtype) ? "re" : "",
1736 IEEE80211_NODE_AID(ni),
1737 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
1738 ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
1739 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
1740 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
1741 ni->ni_flags & IEEE80211_NODE_HT ?
1742 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
1743 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
1744 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
1745 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
1746 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
1747 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
1748 ", fast-frames" : "",
1749 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
1752 ieee80211_new_state(vap, IEEE80211_S_RUN, subtype);
1756 case IEEE80211_FC0_SUBTYPE_DEAUTH: {
1759 if (vap->iv_state == IEEE80211_S_SCAN) {
1760 vap->iv_stats.is_rx_mgtdiscard++;
1763 if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1764 /* NB: can happen when in promiscuous mode */
1765 vap->iv_stats.is_rx_mgtdiscard++;
1770 * deauth frame format
1773 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1774 reason = le16toh(*(uint16_t *)frm);
1776 vap->iv_stats.is_rx_deauth++;
1777 vap->iv_stats.is_rx_deauth_code = reason;
1778 IEEE80211_NODE_STAT(ni, rx_deauth);
1780 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1781 "recv deauthenticate (reason: %d (%s))", reason,
1782 ieee80211_reason_to_string(reason));
1783 ieee80211_new_state(vap, IEEE80211_S_AUTH,
1784 (reason << 8) | IEEE80211_FC0_SUBTYPE_DEAUTH);
1788 case IEEE80211_FC0_SUBTYPE_DISASSOC: {
1791 if (vap->iv_state != IEEE80211_S_RUN &&
1792 vap->iv_state != IEEE80211_S_ASSOC &&
1793 vap->iv_state != IEEE80211_S_AUTH) {
1794 vap->iv_stats.is_rx_mgtdiscard++;
1797 if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1798 /* NB: can happen when in promiscuous mode */
1799 vap->iv_stats.is_rx_mgtdiscard++;
1804 * disassoc frame format
1807 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1808 reason = le16toh(*(uint16_t *)frm);
1810 vap->iv_stats.is_rx_disassoc++;
1811 vap->iv_stats.is_rx_disassoc_code = reason;
1812 IEEE80211_NODE_STAT(ni, rx_disassoc);
1814 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1815 "recv disassociate (reason: %d (%s))", reason,
1816 ieee80211_reason_to_string(reason));
1817 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1821 case IEEE80211_FC0_SUBTYPE_ACTION:
1822 case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
1823 if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
1824 !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1825 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1826 wh, NULL, "%s", "not for us");
1827 vap->iv_stats.is_rx_mgtdiscard++;
1828 } else if (vap->iv_state != IEEE80211_S_RUN) {
1829 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1830 wh, NULL, "wrong state %s",
1831 ieee80211_state_name[vap->iv_state]);
1832 vap->iv_stats.is_rx_mgtdiscard++;
1834 if (ieee80211_parse_action(ni, m0) == 0)
1835 (void)ic->ic_recv_action(ni, wh, frm, efrm);
1839 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1840 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1841 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1842 case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
1843 case IEEE80211_FC0_SUBTYPE_ATIM:
1844 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1845 wh, NULL, "%s", "not handled");
1846 vap->iv_stats.is_rx_mgtdiscard++;
1850 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1851 wh, "mgt", "subtype 0x%x not handled", subtype);
1852 vap->iv_stats.is_rx_badsubtype++;
1859 sta_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
1862 case IEEE80211_FC0_SUBTYPE_BAR:
1863 ieee80211_recv_bar(ni, m);