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 *, 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);
79 ieee80211_sta_attach(struct ieee80211com *ic)
81 ic->ic_vattach[IEEE80211_M_STA] = sta_vattach;
85 ieee80211_sta_detach(struct ieee80211com *ic)
90 sta_vdetach(struct ieee80211vap *vap)
95 sta_vattach(struct ieee80211vap *vap)
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;
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.
111 sta_beacon_miss(struct ieee80211vap *vap)
113 struct ieee80211com *ic = vap->iv_ic;
115 IEEE80211_LOCK_ASSERT(ic);
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]));
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]);
126 if (vap->iv_state == IEEE80211_S_CSA) {
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
134 ieee80211_csa_completeswitch(ic);
137 if (++vap->iv_bmiss_count < vap->iv_bmiss_max) {
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
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);
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;
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 * IEEE80211_M_STA vap state machine handler.
210 * This routine handles the main states in the 802.11 protocol.
213 sta_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
215 struct ieee80211com *ic = vap->iv_ic;
216 struct ieee80211_node *ni;
217 enum ieee80211_state ostate;
219 IEEE80211_LOCK_ASSERT(ic);
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);
233 case IEEE80211_S_INIT:
235 case IEEE80211_S_SLEEP:
237 case IEEE80211_S_RUN:
238 IEEE80211_SEND_MGMT(ni,
239 IEEE80211_FC0_SUBTYPE_DISASSOC,
240 IEEE80211_REASON_ASSOC_LEAVE);
241 ieee80211_sta_leave(ni);
243 case IEEE80211_S_ASSOC:
244 IEEE80211_SEND_MGMT(ni,
245 IEEE80211_FC0_SUBTYPE_DEAUTH,
246 IEEE80211_REASON_AUTH_LEAVE);
248 case IEEE80211_S_SCAN:
249 ieee80211_cancel_scan(vap);
254 if (ostate != IEEE80211_S_INIT) {
255 /* NB: optimize INIT -> INIT case */
256 ieee80211_reset_bss(vap);
258 if (vap->iv_auth->ia_detach != NULL)
259 vap->iv_auth->ia_detach(vap);
261 case IEEE80211_S_SCAN:
263 case IEEE80211_S_INIT:
265 * Initiate a scan. We can come here as a result
266 * of an IEEE80211_IOC_SCAN_REQ too in which case
267 * the vap will be marked with IEEE80211_FEXT_SCANREQ
268 * and the scan request parameters will be present
269 * in iv_scanreq. Otherwise we do the default.
271 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
272 ieee80211_check_scan(vap,
273 vap->iv_scanreq_flags,
274 vap->iv_scanreq_duration,
275 vap->iv_scanreq_mindwell,
276 vap->iv_scanreq_maxdwell,
277 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
278 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
280 ieee80211_check_scan_current(vap);
282 case IEEE80211_S_SCAN:
283 case IEEE80211_S_AUTH:
284 case IEEE80211_S_ASSOC:
286 * These can happen either because of a timeout
287 * on an assoc/auth response or because of a
288 * change in state that requires a reset. For
289 * the former we're called with a non-zero arg
290 * that is the cause for the failure; pass this
291 * to the scan code so it can update state.
292 * Otherwise trigger a new scan unless we're in
293 * manual roaming mode in which case an application
294 * must issue an explicit scan request.
297 ieee80211_scan_assoc_fail(vap,
298 vap->iv_bss->ni_macaddr, arg);
299 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
300 ieee80211_check_scan_current(vap);
302 case IEEE80211_S_RUN: /* beacon miss */
304 * Beacon miss. Notify user space and if not
305 * under control of a user application (roaming
306 * manual) kick off a scan to re-connect.
308 ieee80211_sta_leave(ni);
309 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
310 ieee80211_check_scan_current(vap);
316 case IEEE80211_S_AUTH:
318 case IEEE80211_S_INIT:
319 case IEEE80211_S_SCAN:
320 IEEE80211_SEND_MGMT(ni,
321 IEEE80211_FC0_SUBTYPE_AUTH, 1);
323 case IEEE80211_S_AUTH:
324 case IEEE80211_S_ASSOC:
325 switch (arg & 0xff) {
326 case IEEE80211_FC0_SUBTYPE_AUTH:
328 IEEE80211_SEND_MGMT(ni,
329 IEEE80211_FC0_SUBTYPE_AUTH, 2);
331 case IEEE80211_FC0_SUBTYPE_DEAUTH:
332 sta_authretry(vap, ni, arg>>8);
336 case IEEE80211_S_RUN:
337 switch (arg & 0xff) {
338 case IEEE80211_FC0_SUBTYPE_AUTH:
339 IEEE80211_SEND_MGMT(ni,
340 IEEE80211_FC0_SUBTYPE_AUTH, 2);
341 vap->iv_state = ostate; /* stay RUN */
343 case IEEE80211_FC0_SUBTYPE_DEAUTH:
344 ieee80211_sta_leave(ni);
345 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
347 IEEE80211_SEND_MGMT(ni,
348 IEEE80211_FC0_SUBTYPE_AUTH, 1);
357 case IEEE80211_S_ASSOC:
359 case IEEE80211_S_AUTH:
360 case IEEE80211_S_ASSOC:
361 IEEE80211_SEND_MGMT(ni,
362 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
364 case IEEE80211_S_SLEEP: /* cannot happen */
365 case IEEE80211_S_RUN:
366 ieee80211_sta_leave(ni);
367 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
368 IEEE80211_SEND_MGMT(ni, arg ?
369 IEEE80211_FC0_SUBTYPE_REASSOC_REQ :
370 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
377 case IEEE80211_S_RUN:
378 if (vap->iv_flags & IEEE80211_F_WPA) {
379 /* XXX validate prerequisites */
382 case IEEE80211_S_RUN:
383 case IEEE80211_S_CSA:
385 case IEEE80211_S_AUTH: /* when join is done in fw */
386 case IEEE80211_S_ASSOC:
387 #ifdef IEEE80211_DEBUG
388 if (ieee80211_msg_debug(vap)) {
389 ieee80211_note(vap, "%s with %s ssid ",
390 (vap->iv_opmode == IEEE80211_M_STA ?
391 "associated" : "synchronized"),
392 ether_sprintf(ni->ni_bssid));
393 ieee80211_print_essid(vap->iv_bss->ni_essid,
396 printf(" channel %d start %uMb\n",
397 ieee80211_chan2ieee(ic, ic->ic_curchan),
398 IEEE80211_RATE2MBS(ni->ni_txrate));
401 ieee80211_scan_assoc_success(vap, ni->ni_macaddr);
402 ieee80211_notify_node_join(ni,
403 arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
405 case IEEE80211_S_SLEEP:
406 vap->iv_sta_ps(vap, 0);
411 ieee80211_sync_curchan(ic);
412 if (ostate != IEEE80211_S_RUN &&
413 (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)) {
415 * Start s/w beacon miss timer for devices w/o
416 * hardware support. We fudge a bit here since
417 * we're doing this in software.
419 vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS(
420 2 * vap->iv_bmissthreshold * ni->ni_intval);
421 vap->iv_swbmiss_count = 0;
422 callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
423 ieee80211_swbmiss, vap);
426 * When 802.1x is not in use mark the port authorized
427 * at this point so traffic can flow.
429 if (ni->ni_authmode != IEEE80211_AUTH_8021X)
430 ieee80211_node_authorize(ni);
432 * Fake association when joining an existing bss.
434 if (ic->ic_newassoc != NULL)
435 ic->ic_newassoc(vap->iv_bss, ostate != IEEE80211_S_RUN);
437 case IEEE80211_S_CSA:
438 if (ostate != IEEE80211_S_RUN)
441 case IEEE80211_S_SLEEP:
442 vap->iv_sta_ps(vap, 1);
446 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
447 "%s: unexpected state transition %s -> %s\n", __func__,
448 ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
455 * Return non-zero if the frame is an echo of a multicast
456 * frame sent by ourself. The dir is known to be DSTODS.
459 isdstods_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
461 #define QWH4(wh) ((const struct ieee80211_qosframe_addr4 *)wh)
462 #define WH4(wh) ((const struct ieee80211_frame_addr4 *)wh)
465 KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
467 if (!IEEE80211_IS_MULTICAST(wh->i_addr3))
469 sa = IEEE80211_QOS_HAS_SEQ(wh) ? QWH4(wh)->i_addr4 : WH4(wh)->i_addr4;
470 return IEEE80211_ADDR_EQ(sa, vap->iv_myaddr);
476 * Return non-zero if the frame is an echo of a multicast
477 * frame sent by ourself. The dir is known to be FROMDS.
480 isfromds_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
482 KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
484 if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
486 return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
490 * Decide if a received management frame should be
491 * printed when debugging is enabled. This filters some
492 * of the less interesting frames that come frequently
496 doprint(struct ieee80211vap *vap, int subtype)
499 case IEEE80211_FC0_SUBTYPE_BEACON:
500 return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
501 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
508 * Process a received frame. The node associated with the sender
509 * should be supplied. If nothing was found in the node table then
510 * the caller is assumed to supply a reference to iv_bss instead.
511 * The RSSI and a timestamp are also supplied. The RSSI data is used
512 * during AP scanning to select a AP to associate with; it can have
513 * any units so long as values have consistent units and higher values
514 * mean ``better signal''. The receive timestamp is currently not used
515 * by the 802.11 layer.
518 sta_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
520 #define HAS_SEQ(type) ((type & 0x4) == 0)
521 struct ieee80211vap *vap = ni->ni_vap;
522 struct ieee80211com *ic = ni->ni_ic;
523 struct ifnet *ifp = vap->iv_ifp;
524 struct ieee80211_frame *wh;
525 struct ieee80211_key *key;
526 struct ether_header *eh;
527 int hdrspace, need_tap = 1; /* mbuf need to be tapped. */
528 uint8_t dir, type, subtype, qos;
532 if (m->m_flags & M_AMPDU_MPDU) {
534 * Fastpath for A-MPDU reorder q resubmission. Frames
535 * w/ M_AMPDU_MPDU marked have already passed through
536 * here but were received out of order and been held on
537 * the reorder queue. When resubmitted they are marked
538 * with the M_AMPDU_MPDU flag and we can bypass most of
539 * the normal processing.
541 wh = mtod(m, struct ieee80211_frame *);
542 type = IEEE80211_FC0_TYPE_DATA;
543 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
544 subtype = IEEE80211_FC0_SUBTYPE_QOS;
545 hdrspace = ieee80211_hdrspace(ic, wh); /* XXX optimize? */
549 KASSERT(ni != NULL, ("null node"));
550 ni->ni_inact = ni->ni_inact_reload;
552 type = -1; /* undefined */
554 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
555 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
556 ni->ni_macaddr, NULL,
557 "too short (1): len %u", m->m_pkthdr.len);
558 vap->iv_stats.is_rx_tooshort++;
562 * Bit of a cheat here, we use a pointer for a 3-address
563 * frame format but don't reference fields past outside
564 * ieee80211_frame_min w/o first validating the data is
567 wh = mtod(m, struct ieee80211_frame *);
569 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
570 IEEE80211_FC0_VERSION_0) {
571 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
572 ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
573 wh->i_fc[0], wh->i_fc[1]);
574 vap->iv_stats.is_rx_badversion++;
578 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
579 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
580 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
581 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
583 if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) {
584 /* not interested in */
585 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
586 bssid, NULL, "%s", "not to bss");
587 vap->iv_stats.is_rx_wrongbss++;
592 * Some devices may be in a promiscuous mode
593 * where they receive frames for multiple station
596 * If we receive a data frame that isn't
597 * destined to our VAP MAC, drop it.
599 * XXX TODO: This is only enforced when not scanning;
600 * XXX it assumes a software-driven scan will put the NIC
601 * XXX into a "no data frames" mode before setting this
602 * XXX flag. Otherwise it may be possible that we'll still
603 * XXX process data frames whilst scanning.
605 if ((! IEEE80211_IS_MULTICAST(wh->i_addr1))
606 && (! IEEE80211_ADDR_EQ(wh->i_addr1, IF_LLADDR(ifp)))) {
607 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
608 bssid, NULL, "not to cur sta: lladdr=%6D, addr1=%6D",
609 IF_LLADDR(ifp), ":", wh->i_addr1, ":");
610 vap->iv_stats.is_rx_wrongbss++;
614 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
616 if (HAS_SEQ(type) && !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
617 uint8_t tid = ieee80211_gettid(wh);
618 if (IEEE80211_QOS_HAS_SEQ(wh) &&
619 TID_TO_WME_AC(tid) >= WME_AC_VI)
620 ic->ic_wme.wme_hipri_traffic++;
621 rxseq = le16toh(*(uint16_t *)wh->i_seq);
622 if (! ieee80211_check_rxseq(ni, wh)) {
623 /* duplicate, discard */
624 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
626 "seqno <%u,%u> fragno <%u,%u> tid %u",
627 rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
628 ni->ni_rxseqs[tid] >>
629 IEEE80211_SEQ_SEQ_SHIFT,
630 rxseq & IEEE80211_SEQ_FRAG_MASK,
632 IEEE80211_SEQ_FRAG_MASK,
634 vap->iv_stats.is_rx_dup++;
635 IEEE80211_NODE_STAT(ni, rx_dup);
638 ni->ni_rxseqs[tid] = rxseq;
643 case IEEE80211_FC0_TYPE_DATA:
644 hdrspace = ieee80211_hdrspace(ic, wh);
645 if (m->m_len < hdrspace &&
646 (m = m_pullup(m, hdrspace)) == NULL) {
647 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
648 ni->ni_macaddr, NULL,
649 "data too short: expecting %u", hdrspace);
650 vap->iv_stats.is_rx_tooshort++;
654 * Handle A-MPDU re-ordering. If the frame is to be
655 * processed directly then ieee80211_ampdu_reorder
656 * will return 0; otherwise it has consumed the mbuf
657 * and we should do nothing more with it.
659 if ((m->m_flags & M_AMPDU) &&
660 (dir == IEEE80211_FC1_DIR_FROMDS ||
661 dir == IEEE80211_FC1_DIR_DSTODS) &&
662 ieee80211_ampdu_reorder(ni, m) != 0) {
667 if (dir == IEEE80211_FC1_DIR_FROMDS) {
668 if ((ifp->if_flags & IFF_SIMPLEX) &&
669 isfromds_mcastecho(vap, wh)) {
671 * In IEEE802.11 network, multicast
672 * packets sent from "me" are broadcast
673 * from the AP; silently discard for
676 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
677 wh, "data", "%s", "multicast echo");
678 vap->iv_stats.is_rx_mcastecho++;
681 if ((vap->iv_flags & IEEE80211_F_DWDS) &&
682 IEEE80211_IS_MULTICAST(wh->i_addr1)) {
684 * DWDS sta's must drop 3-address mcast frames
685 * as they will be sent separately as a 4-addr
686 * frame. Accepting the 3-addr frame will
687 * confuse the bridge into thinking the sending
688 * sta is located at the end of WDS link.
690 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
691 "3-address data", "%s", "DWDS enabled");
692 vap->iv_stats.is_rx_mcastecho++;
695 } else if (dir == IEEE80211_FC1_DIR_DSTODS) {
696 if ((vap->iv_flags & IEEE80211_F_DWDS) == 0) {
697 IEEE80211_DISCARD(vap,
698 IEEE80211_MSG_INPUT, wh, "4-address data",
699 "%s", "DWDS not enabled");
700 vap->iv_stats.is_rx_wrongdir++;
703 if ((ifp->if_flags & IFF_SIMPLEX) &&
704 isdstods_mcastecho(vap, wh)) {
706 * In IEEE802.11 network, multicast
707 * packets sent from "me" are broadcast
708 * from the AP; silently discard for
711 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
712 "4-address data", "%s", "multicast echo");
713 vap->iv_stats.is_rx_mcastecho++;
717 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
718 "data", "incorrect dir 0x%x", dir);
719 vap->iv_stats.is_rx_wrongdir++;
724 * Handle privacy requirements. Note that we
725 * must not be preempted from here until after
726 * we (potentially) call ieee80211_crypto_demic;
727 * otherwise we may violate assumptions in the
728 * crypto cipher modules used to do delayed update
729 * of replay sequence numbers.
731 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
732 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
734 * Discard encrypted frames when privacy is off.
736 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
737 wh, "WEP", "%s", "PRIVACY off");
738 vap->iv_stats.is_rx_noprivacy++;
739 IEEE80211_NODE_STAT(ni, rx_noprivacy);
742 key = ieee80211_crypto_decap(ni, m, hdrspace);
744 /* NB: stats+msgs handled in crypto_decap */
745 IEEE80211_NODE_STAT(ni, rx_wepfail);
748 wh = mtod(m, struct ieee80211_frame *);
749 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
751 /* XXX M_WEP and IEEE80211_F_PRIVACY */
756 * Save QoS bits for use below--before we strip the header.
758 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
759 qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
760 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
761 ((struct ieee80211_qosframe *)wh)->i_qos[0];
766 * Next up, any fragmentation.
768 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
769 m = ieee80211_defrag(ni, m, hdrspace);
771 /* Fragment dropped or frame not complete yet */
775 wh = NULL; /* no longer valid, catch any uses */
778 * Next strip any MSDU crypto bits.
780 if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
781 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
782 ni->ni_macaddr, "data", "%s", "demic error");
783 vap->iv_stats.is_rx_demicfail++;
784 IEEE80211_NODE_STAT(ni, rx_demicfail);
788 /* copy to listener after decrypt */
789 if (ieee80211_radiotap_active_vap(vap))
790 ieee80211_radiotap_rx(vap, m);
794 * Finally, strip the 802.11 header.
796 m = ieee80211_decap(vap, m, hdrspace);
798 /* XXX mask bit to check for both */
799 /* don't count Null data frames as errors */
800 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
801 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
803 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
804 ni->ni_macaddr, "data", "%s", "decap error");
805 vap->iv_stats.is_rx_decap++;
806 IEEE80211_NODE_STAT(ni, rx_decap);
809 eh = mtod(m, struct ether_header *);
810 if (!ieee80211_node_is_authorized(ni)) {
812 * Deny any non-PAE frames received prior to
813 * authorization. For open/shared-key
814 * authentication the port is mark authorized
815 * after authentication completes. For 802.1x
816 * the port is not marked authorized by the
817 * authenticator until the handshake has completed.
819 if (eh->ether_type != htons(ETHERTYPE_PAE)) {
820 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
821 eh->ether_shost, "data",
822 "unauthorized port: ether type 0x%x len %u",
823 eh->ether_type, m->m_pkthdr.len);
824 vap->iv_stats.is_rx_unauth++;
825 IEEE80211_NODE_STAT(ni, rx_unauth);
830 * When denying unencrypted frames, discard
831 * any non-PAE frames received without encryption.
833 if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
834 (key == NULL && (m->m_flags & M_WEP) == 0) &&
835 eh->ether_type != htons(ETHERTYPE_PAE)) {
837 * Drop unencrypted frames.
839 vap->iv_stats.is_rx_unencrypted++;
840 IEEE80211_NODE_STAT(ni, rx_unencrypted);
844 /* XXX require HT? */
845 if (qos & IEEE80211_QOS_AMSDU) {
846 m = ieee80211_decap_amsdu(ni, m);
848 return IEEE80211_FC0_TYPE_DATA;
850 #ifdef IEEE80211_SUPPORT_SUPERG
851 m = ieee80211_decap_fastframe(vap, ni, m);
853 return IEEE80211_FC0_TYPE_DATA;
856 ieee80211_deliver_data(vap, ni, m);
857 return IEEE80211_FC0_TYPE_DATA;
859 case IEEE80211_FC0_TYPE_MGT:
860 vap->iv_stats.is_rx_mgmt++;
861 IEEE80211_NODE_STAT(ni, rx_mgmt);
862 if (dir != IEEE80211_FC1_DIR_NODS) {
863 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
864 wh, "data", "incorrect dir 0x%x", dir);
865 vap->iv_stats.is_rx_wrongdir++;
868 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
869 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
870 ni->ni_macaddr, "mgt", "too short: len %u",
872 vap->iv_stats.is_rx_tooshort++;
875 #ifdef IEEE80211_DEBUG
876 if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
877 ieee80211_msg_dumppkts(vap)) {
878 if_printf(ifp, "received %s from %s rssi %d\n",
879 ieee80211_mgt_subtype_name[subtype >>
880 IEEE80211_FC0_SUBTYPE_SHIFT],
881 ether_sprintf(wh->i_addr2), rssi);
884 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
885 if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
887 * Only shared key auth frames with a challenge
888 * should be encrypted, discard all others.
890 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
891 wh, ieee80211_mgt_subtype_name[subtype >>
892 IEEE80211_FC0_SUBTYPE_SHIFT],
893 "%s", "WEP set but not permitted");
894 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
897 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
899 * Discard encrypted frames when privacy is off.
901 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
902 wh, "mgt", "%s", "WEP set but PRIVACY off");
903 vap->iv_stats.is_rx_noprivacy++;
906 hdrspace = ieee80211_hdrspace(ic, wh);
907 key = ieee80211_crypto_decap(ni, m, hdrspace);
909 /* NB: stats+msgs handled in crypto_decap */
912 wh = mtod(m, struct ieee80211_frame *);
913 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
915 vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
918 case IEEE80211_FC0_TYPE_CTL:
919 vap->iv_stats.is_rx_ctl++;
920 IEEE80211_NODE_STAT(ni, rx_ctrl);
921 vap->iv_recv_ctl(ni, m, subtype);
925 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
926 wh, NULL, "bad frame type 0x%x", type);
927 /* should not come here */
934 if (need_tap && ieee80211_radiotap_active_vap(vap))
935 ieee80211_radiotap_rx(vap, m);
942 sta_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
943 int rssi, int nf, uint16_t seq, uint16_t status)
945 struct ieee80211vap *vap = ni->ni_vap;
947 if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
948 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
949 ni->ni_macaddr, "open auth",
950 "bad sta auth mode %u", ni->ni_authmode);
951 vap->iv_stats.is_rx_bad_auth++; /* XXX */
954 if (vap->iv_state != IEEE80211_S_AUTH ||
955 seq != IEEE80211_AUTH_OPEN_RESPONSE) {
956 vap->iv_stats.is_rx_bad_auth++;
960 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
961 ni, "open auth failed (reason %d)", status);
962 vap->iv_stats.is_rx_auth_fail++;
963 vap->iv_stats.is_rx_authfail_code = status;
964 ieee80211_new_state(vap, IEEE80211_S_SCAN,
965 IEEE80211_SCAN_FAIL_STATUS);
967 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
971 sta_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
972 uint8_t *frm, uint8_t *efrm, int rssi, int nf,
973 uint16_t seq, uint16_t status)
975 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");
991 estatus = IEEE80211_STATUS_ALG;
995 * Pre-shared key authentication is evil; accept
996 * it only if explicitly configured (it is supported
997 * mainly for compatibility with clients like OS X).
999 if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1000 ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1001 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1002 ni->ni_macaddr, "shared key auth",
1003 "bad sta auth mode %u", ni->ni_authmode);
1004 vap->iv_stats.is_rx_bad_auth++; /* XXX maybe a unique error? */
1005 estatus = IEEE80211_STATUS_ALG;
1010 if (frm + 1 < efrm) {
1011 if ((frm[1] + 2) > (efrm - frm)) {
1012 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1013 ni->ni_macaddr, "shared key auth",
1014 "ie %d/%d too long",
1015 frm[0], (frm[1] + 2) - (efrm - frm));
1016 vap->iv_stats.is_rx_bad_auth++;
1017 estatus = IEEE80211_STATUS_CHALLENGE;
1020 if (*frm == IEEE80211_ELEMID_CHALLENGE)
1025 case IEEE80211_AUTH_SHARED_CHALLENGE:
1026 case IEEE80211_AUTH_SHARED_RESPONSE:
1027 if (challenge == NULL) {
1028 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1029 ni->ni_macaddr, "shared key auth",
1030 "%s", "no challenge");
1031 vap->iv_stats.is_rx_bad_auth++;
1032 estatus = IEEE80211_STATUS_CHALLENGE;
1035 if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1036 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1037 ni->ni_macaddr, "shared key auth",
1038 "bad challenge len %d", challenge[1]);
1039 vap->iv_stats.is_rx_bad_auth++;
1040 estatus = IEEE80211_STATUS_CHALLENGE;
1046 if (vap->iv_state != IEEE80211_S_AUTH)
1049 case IEEE80211_AUTH_SHARED_PASS:
1050 if (ni->ni_challenge != NULL) {
1051 free(ni->ni_challenge, M_80211_NODE);
1052 ni->ni_challenge = NULL;
1055 IEEE80211_NOTE_FRAME(vap,
1056 IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, wh,
1057 "shared key auth failed (reason %d)", status);
1058 vap->iv_stats.is_rx_auth_fail++;
1059 vap->iv_stats.is_rx_authfail_code = status;
1062 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1064 case IEEE80211_AUTH_SHARED_CHALLENGE:
1065 if (!ieee80211_alloc_challenge(ni))
1067 /* XXX could optimize by passing recvd challenge */
1068 memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
1069 IEEE80211_SEND_MGMT(ni,
1070 IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1073 IEEE80211_DISCARD(vap, IEEE80211_MSG_AUTH,
1074 wh, "shared key auth", "bad seq %d", seq);
1075 vap->iv_stats.is_rx_bad_auth++;
1081 * Kick the state machine. This short-circuits
1082 * using the mgt frame timeout to trigger the
1085 if (vap->iv_state == IEEE80211_S_AUTH)
1086 ieee80211_new_state(vap, IEEE80211_S_SCAN,
1087 IEEE80211_SCAN_FAIL_STATUS);
1091 ieee80211_parse_wmeparams(struct ieee80211vap *vap, uint8_t *frm,
1092 const struct ieee80211_frame *wh)
1094 #define MS(_v, _f) (((_v) & _f) >> _f##_S)
1095 struct ieee80211_wme_state *wme = &vap->iv_ic->ic_wme;
1096 u_int len = frm[1], qosinfo;
1099 if (len < sizeof(struct ieee80211_wme_param)-2) {
1100 IEEE80211_DISCARD_IE(vap,
1101 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
1102 wh, "WME", "too short, len %u", len);
1105 qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)];
1106 qosinfo &= WME_QOSINFO_COUNT;
1107 /* XXX do proper check for wraparound */
1108 if (qosinfo == wme->wme_wmeChanParams.cap_info)
1110 frm += __offsetof(struct ieee80211_wme_param, params_acParams);
1111 for (i = 0; i < WME_NUM_AC; i++) {
1112 struct wmeParams *wmep =
1113 &wme->wme_wmeChanParams.cap_wmeParams[i];
1114 /* NB: ACI not used */
1115 wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
1116 wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
1117 wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
1118 wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
1119 wmep->wmep_txopLimit = LE_READ_2(frm+2);
1122 wme->wme_wmeChanParams.cap_info = qosinfo;
1128 * Process 11h Channel Switch Announcement (CSA) ie. If this
1129 * is the first CSA then initiate the switch. Otherwise we
1130 * track state and trigger completion and/or cancel of the switch.
1131 * XXX should be public for IBSS use
1134 ieee80211_parse_csaparams(struct ieee80211vap *vap, uint8_t *frm,
1135 const struct ieee80211_frame *wh)
1137 struct ieee80211com *ic = vap->iv_ic;
1138 const struct ieee80211_csa_ie *csa =
1139 (const struct ieee80211_csa_ie *) frm;
1141 KASSERT(vap->iv_state >= IEEE80211_S_RUN,
1142 ("state %s", ieee80211_state_name[vap->iv_state]));
1144 if (csa->csa_mode > 1) {
1145 IEEE80211_DISCARD_IE(vap,
1146 IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1147 wh, "CSA", "invalid mode %u", csa->csa_mode);
1151 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
1153 * Convert the channel number to a channel reference. We
1154 * try first to preserve turbo attribute of the current
1155 * channel then fallback. Note this will not work if the
1156 * CSA specifies a channel that requires a band switch (e.g.
1157 * 11a => 11g). This is intentional as 11h is defined only
1158 * for 5GHz/11a and because the switch does not involve a
1159 * reassociation, protocol state (capabilities, negotated
1160 * rates, etc) may/will be wrong.
1162 struct ieee80211_channel *c =
1163 ieee80211_find_channel_byieee(ic, csa->csa_newchan,
1164 (ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALLTURBO));
1166 c = ieee80211_find_channel_byieee(ic,
1168 (ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALL));
1170 IEEE80211_DISCARD_IE(vap,
1171 IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1172 wh, "CSA", "invalid channel %u",
1177 #if IEEE80211_CSA_COUNT_MIN > 0
1178 if (csa->csa_count < IEEE80211_CSA_COUNT_MIN) {
1180 * Require at least IEEE80211_CSA_COUNT_MIN count to
1181 * reduce the risk of being redirected by a fabricated
1182 * CSA. If a valid CSA is dropped we'll still get a
1183 * beacon miss when the AP leaves the channel so we'll
1184 * eventually follow to the new channel.
1186 * NOTE: this violates the 11h spec that states that
1187 * count may be any value and if 0 then a switch
1188 * should happen asap.
1190 IEEE80211_DISCARD_IE(vap,
1191 IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1192 wh, "CSA", "count %u too small, must be >= %u",
1193 csa->csa_count, IEEE80211_CSA_COUNT_MIN);
1197 ieee80211_csa_startswitch(ic, c, csa->csa_mode, csa->csa_count);
1200 * Validate this ie against the initial CSA. We require
1201 * mode and channel not change and the count must be
1202 * monotonically decreasing. This may be pointless and
1203 * canceling the switch as a result may be too paranoid but
1204 * in the worst case if we drop out of CSA because of this
1205 * and the AP does move then we'll just end up taking a
1206 * beacon miss and scan to find the AP.
1208 * XXX may want <= on count as we also process ProbeResp
1209 * frames and those may come in w/ the same count as the
1210 * previous beacon; but doing so leaves us open to a stuck
1211 * count until we add a dead-man timer
1213 if (!(csa->csa_count < ic->ic_csa_count &&
1214 csa->csa_mode == ic->ic_csa_mode &&
1215 csa->csa_newchan == ieee80211_chan2ieee(ic, ic->ic_csa_newchan))) {
1216 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_DOTH, wh,
1217 "CSA ie mismatch, initial ie <%d,%d,%d>, "
1218 "this ie <%d,%d,%d>", ic->ic_csa_mode,
1219 ic->ic_csa_newchan, ic->ic_csa_count,
1220 csa->csa_mode, csa->csa_newchan, csa->csa_count);
1221 ieee80211_csa_cancelswitch(ic);
1223 if (csa->csa_count <= 1)
1224 ieee80211_csa_completeswitch(ic);
1226 ic->ic_csa_count = csa->csa_count;
1230 IEEE80211_UNLOCK(ic);
1234 * Return non-zero if a background scan may be continued:
1235 * o bg scan is active
1236 * o no channel switch is pending
1237 * o there has not been any traffic recently
1239 * Note we do not check if there is an administrative enable;
1240 * this is only done to start the scan. We assume that any
1241 * change in state will be accompanied by a request to cancel
1242 * active scans which will otherwise cause this test to fail.
1245 contbgscan(struct ieee80211vap *vap)
1247 struct ieee80211com *ic = vap->iv_ic;
1249 return ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) &&
1250 (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1251 vap->iv_state == IEEE80211_S_RUN && /* XXX? */
1252 time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1256 * Return non-zero if a backgrond scan may be started:
1257 * o bg scanning is administratively enabled
1258 * o no channel switch is pending
1259 * o we are not boosted on a dynamic turbo channel
1260 * o there has not been a scan recently
1261 * o there has not been any traffic recently
1264 startbgscan(struct ieee80211vap *vap)
1266 struct ieee80211com *ic = vap->iv_ic;
1268 return ((vap->iv_flags & IEEE80211_F_BGSCAN) &&
1269 (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1270 #ifdef IEEE80211_SUPPORT_SUPERG
1271 !IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1273 time_after(ticks, ic->ic_lastscan + vap->iv_bgscanintvl) &&
1274 time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1278 sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1279 int subtype, int rssi, int nf)
1281 #define ISPROBE(_st) ((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1282 #define ISREASSOC(_st) ((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
1283 struct ieee80211vap *vap = ni->ni_vap;
1284 struct ieee80211com *ic = ni->ni_ic;
1285 struct ieee80211_frame *wh;
1286 uint8_t *frm, *efrm;
1287 uint8_t *rates, *xrates, *wme, *htcap, *htinfo;
1289 int ht_state_change = 0;
1291 wh = mtod(m0, struct ieee80211_frame *);
1292 frm = (uint8_t *)&wh[1];
1293 efrm = mtod(m0, uint8_t *) + m0->m_len;
1295 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1296 case IEEE80211_FC0_SUBTYPE_BEACON: {
1297 struct ieee80211_scanparams scan;
1299 * We process beacon/probe response frames:
1300 * o when scanning, or
1301 * o station mode when associated (to collect state
1302 * updates such as 802.11g slot time)
1303 * Frames otherwise received are discarded.
1305 if (!((ic->ic_flags & IEEE80211_F_SCAN) || ni->ni_associd)) {
1306 vap->iv_stats.is_rx_mgtdiscard++;
1309 /* XXX probe response in sta mode when !scanning? */
1310 if (ieee80211_parse_beacon(ni, m0, &scan) != 0) {
1311 if (! (ic->ic_flags & IEEE80211_F_SCAN))
1312 vap->iv_stats.is_beacon_bad++;
1316 * Count frame now that we know it's to be processed.
1318 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1319 vap->iv_stats.is_rx_beacon++; /* XXX remove */
1320 IEEE80211_NODE_STAT(ni, rx_beacons);
1322 IEEE80211_NODE_STAT(ni, rx_proberesp);
1324 * When operating in station mode, check for state updates.
1325 * Be careful to ignore beacons received while doing a
1326 * background scan. We consider only 11g/WMM stuff right now.
1328 if (ni->ni_associd != 0 &&
1329 ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
1330 IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
1331 /* record tsf of last beacon */
1332 memcpy(ni->ni_tstamp.data, scan.tstamp,
1333 sizeof(ni->ni_tstamp));
1334 /* count beacon frame for s/w bmiss handling */
1335 vap->iv_swbmiss_count++;
1336 vap->iv_bmiss_count = 0;
1337 if (ni->ni_erp != scan.erp) {
1338 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1340 "erp change: was 0x%x, now 0x%x",
1341 ni->ni_erp, scan.erp);
1342 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1343 (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1344 ic->ic_flags |= IEEE80211_F_USEPROT;
1346 ic->ic_flags &= ~IEEE80211_F_USEPROT;
1347 ni->ni_erp = scan.erp;
1349 /* XXX driver notification */
1351 if ((ni->ni_capinfo ^ scan.capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) {
1352 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1354 "capabilities change: was 0x%x, now 0x%x",
1355 ni->ni_capinfo, scan.capinfo);
1357 * NB: we assume short preamble doesn't
1358 * change dynamically
1360 ieee80211_set_shortslottime(ic,
1361 IEEE80211_IS_CHAN_A(ic->ic_bsschan) ||
1362 (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1363 ni->ni_capinfo = (ni->ni_capinfo &~ IEEE80211_CAPINFO_SHORT_SLOTTIME)
1364 | (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME);
1367 if (scan.wme != NULL &&
1368 (ni->ni_flags & IEEE80211_NODE_QOS) &&
1369 ieee80211_parse_wmeparams(vap, scan.wme, wh) > 0)
1370 ieee80211_wme_updateparams(vap);
1371 #ifdef IEEE80211_SUPPORT_SUPERG
1372 if (scan.ath != NULL)
1373 ieee80211_parse_athparams(ni, scan.ath, wh);
1375 if (scan.htcap != NULL && scan.htinfo != NULL &&
1376 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1377 /* XXX state changes? */
1378 if (ieee80211_ht_updateparams(ni,
1379 scan.htcap, scan.htinfo))
1380 ht_state_change = 1;
1383 ic->ic_set_quiet(ni, scan.quiet);
1384 if (scan.tim != NULL) {
1385 struct ieee80211_tim_ie *tim =
1386 (struct ieee80211_tim_ie *) scan.tim;
1388 int aid = IEEE80211_AID(ni->ni_associd);
1389 int ix = aid / NBBY;
1390 int min = tim->tim_bitctl &~ 1;
1391 int max = tim->tim_len + min - 4;
1392 if ((tim->tim_bitctl&1) ||
1393 (min <= ix && ix <= max &&
1394 isset(tim->tim_bitmap - min, aid))) {
1396 * XXX Do not let bg scan kick off
1397 * we are expecting data.
1399 ic->ic_lastdata = ticks;
1400 vap->iv_sta_ps(vap, 0);
1403 ni->ni_dtim_count = tim->tim_count;
1404 ni->ni_dtim_period = tim->tim_period;
1406 if (scan.csa != NULL &&
1407 (vap->iv_flags & IEEE80211_F_DOTH))
1408 ieee80211_parse_csaparams(vap, scan.csa, wh);
1409 else if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
1411 * No CSA ie or 11h disabled, but a channel
1412 * switch is pending; drop out so we aren't
1413 * stuck in CSA state. If the AP really is
1414 * moving we'll get a beacon miss and scan.
1417 ieee80211_csa_cancelswitch(ic);
1418 IEEE80211_UNLOCK(ic);
1421 * If scanning, pass the info to the scan module.
1422 * Otherwise, check if it's the right time to do
1423 * a background scan. Background scanning must
1424 * be enabled and we must not be operating in the
1425 * turbo phase of dynamic turbo mode. Then,
1426 * it's been a while since the last background
1427 * scan and if no data frames have come through
1428 * recently, kick off a scan. Note that this
1429 * is the mechanism by which a background scan
1430 * is started _and_ continued each time we
1431 * return on-channel to receive a beacon from
1434 if (ic->ic_flags & IEEE80211_F_SCAN) {
1435 ieee80211_add_scan(vap, &scan, wh,
1437 } else if (contbgscan(vap)) {
1438 ieee80211_bg_scan(vap, 0);
1439 } else if (startbgscan(vap)) {
1440 vap->iv_stats.is_scan_bg++;
1442 /* wakeup if we are sleeing */
1443 ieee80211_set_pwrsave(vap, 0);
1445 ieee80211_bg_scan(vap, 0);
1449 * If we've had a channel width change (eg HT20<->HT40)
1450 * then schedule a delayed driver notification.
1452 if (ht_state_change)
1453 ieee80211_update_chw(ic);
1457 * If scanning, just pass information to the scan module.
1459 if (ic->ic_flags & IEEE80211_F_SCAN) {
1460 if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1462 * Actively scanning a channel marked passive;
1463 * send a probe request now that we know there
1464 * is 802.11 traffic present.
1466 * XXX check if the beacon we recv'd gives
1467 * us what we need and suppress the probe req
1469 ieee80211_probe_curchan(vap, 1);
1470 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1472 ieee80211_add_scan(vap, &scan, wh, subtype, rssi, nf);
1478 case IEEE80211_FC0_SUBTYPE_AUTH: {
1479 uint16_t algo, seq, status;
1487 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1488 algo = le16toh(*(uint16_t *)frm);
1489 seq = le16toh(*(uint16_t *)(frm + 2));
1490 status = le16toh(*(uint16_t *)(frm + 4));
1491 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1492 "recv auth frame with algorithm %d seq %d", algo, seq);
1494 if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1495 IEEE80211_DISCARD(vap,
1496 IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1497 wh, "auth", "%s", "TKIP countermeasures enabled");
1498 vap->iv_stats.is_rx_auth_countermeasures++;
1499 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1500 ieee80211_send_error(ni, wh->i_addr2,
1501 IEEE80211_FC0_SUBTYPE_AUTH,
1502 IEEE80211_REASON_MIC_FAILURE);
1506 if (algo == IEEE80211_AUTH_ALG_SHARED)
1507 sta_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1509 else if (algo == IEEE80211_AUTH_ALG_OPEN)
1510 sta_auth_open(ni, wh, rssi, nf, seq, status);
1512 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1513 wh, "auth", "unsupported alg %d", algo);
1514 vap->iv_stats.is_rx_auth_unsupported++;
1520 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1521 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
1522 uint16_t capinfo, associd;
1525 if (vap->iv_state != IEEE80211_S_ASSOC) {
1526 vap->iv_stats.is_rx_mgtdiscard++;
1531 * asresp frame format
1532 * [2] capability information
1534 * [2] association ID
1535 * [tlv] supported rates
1536 * [tlv] extended supported rates
1538 * [tlv] HT capabilities
1541 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1543 capinfo = le16toh(*(uint16_t *)frm);
1545 status = le16toh(*(uint16_t *)frm);
1548 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1549 wh->i_addr2, "%sassoc failed (reason %d)",
1550 ISREASSOC(subtype) ? "re" : "", status);
1551 vap->iv_stats.is_rx_auth_fail++; /* XXX */
1554 associd = le16toh(*(uint16_t *)frm);
1557 rates = xrates = wme = htcap = htinfo = NULL;
1558 while (efrm - frm > 1) {
1559 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1561 case IEEE80211_ELEMID_RATES:
1564 case IEEE80211_ELEMID_XRATES:
1567 case IEEE80211_ELEMID_HTCAP:
1570 case IEEE80211_ELEMID_HTINFO:
1573 case IEEE80211_ELEMID_VENDOR:
1576 else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1578 * Accept pre-draft HT ie's if the
1579 * standard ones have not been seen.
1581 if (ishtcapoui(frm)) {
1584 } else if (ishtinfooui(frm)) {
1589 /* XXX Atheros OUI support */
1595 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1597 IEEE80211_VERIFY_ELEMENT(xrates,
1598 IEEE80211_RATE_MAXSIZE - rates[1], return);
1599 rate = ieee80211_setup_rates(ni, rates, xrates,
1601 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1602 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1603 if (rate & IEEE80211_RATE_BASIC) {
1604 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1606 "%sassoc failed (rate set mismatch)",
1607 ISREASSOC(subtype) ? "re" : "");
1608 vap->iv_stats.is_rx_assoc_norate++;
1609 ieee80211_new_state(vap, IEEE80211_S_SCAN,
1610 IEEE80211_SCAN_FAIL_STATUS);
1614 ni->ni_capinfo = capinfo;
1615 ni->ni_associd = associd;
1616 if (ni->ni_jointime == 0)
1617 ni->ni_jointime = time_uptime;
1619 ieee80211_parse_wmeparams(vap, wme, wh) >= 0) {
1620 ni->ni_flags |= IEEE80211_NODE_QOS;
1621 ieee80211_wme_updateparams(vap);
1623 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1625 * Setup HT state according to the negotiation.
1627 * NB: shouldn't need to check if HT use is enabled but some
1628 * ap's send back HT ie's even when we don't indicate we
1629 * are HT capable in our AssocReq.
1631 if (htcap != NULL && htinfo != NULL &&
1632 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1633 ieee80211_ht_node_init(ni);
1634 ieee80211_ht_updateparams(ni, htcap, htinfo);
1635 ieee80211_setup_htrates(ni, htcap,
1636 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1637 ieee80211_setup_basic_htrates(ni, htinfo);
1638 ieee80211_node_setuptxparms(ni);
1639 ieee80211_ratectl_node_init(ni);
1641 #ifdef IEEE80211_SUPPORT_SUPERG
1642 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_ATH))
1643 ieee80211_ff_node_init(ni);
1647 * Configure state now that we are associated.
1649 * XXX may need different/additional driver callbacks?
1651 if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1652 (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1653 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1654 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1656 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1657 ic->ic_flags |= IEEE80211_F_USEBARKER;
1659 ieee80211_set_shortslottime(ic,
1660 IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1661 (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1663 * Honor ERP protection.
1665 * NB: ni_erp should zero for non-11g operation.
1667 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1668 (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1669 ic->ic_flags |= IEEE80211_F_USEPROT;
1671 ic->ic_flags &= ~IEEE80211_F_USEPROT;
1672 IEEE80211_NOTE_MAC(vap,
1673 IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, wh->i_addr2,
1674 "%sassoc success at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
1675 ISREASSOC(subtype) ? "re" : "",
1676 IEEE80211_NODE_AID(ni),
1677 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
1678 ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
1679 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
1680 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
1681 ni->ni_flags & IEEE80211_NODE_HT ?
1682 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
1683 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
1684 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
1685 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
1686 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
1687 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
1688 ", fast-frames" : "",
1689 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
1692 ieee80211_new_state(vap, IEEE80211_S_RUN, subtype);
1696 case IEEE80211_FC0_SUBTYPE_DEAUTH: {
1699 if (vap->iv_state == IEEE80211_S_SCAN) {
1700 vap->iv_stats.is_rx_mgtdiscard++;
1703 if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1704 /* NB: can happen when in promiscuous mode */
1705 vap->iv_stats.is_rx_mgtdiscard++;
1710 * deauth frame format
1713 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1714 reason = le16toh(*(uint16_t *)frm);
1716 vap->iv_stats.is_rx_deauth++;
1717 vap->iv_stats.is_rx_deauth_code = reason;
1718 IEEE80211_NODE_STAT(ni, rx_deauth);
1720 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1721 "recv deauthenticate (reason %d)", reason);
1722 ieee80211_new_state(vap, IEEE80211_S_AUTH,
1723 (reason << 8) | IEEE80211_FC0_SUBTYPE_DEAUTH);
1727 case IEEE80211_FC0_SUBTYPE_DISASSOC: {
1730 if (vap->iv_state != IEEE80211_S_RUN &&
1731 vap->iv_state != IEEE80211_S_ASSOC &&
1732 vap->iv_state != IEEE80211_S_AUTH) {
1733 vap->iv_stats.is_rx_mgtdiscard++;
1736 if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1737 /* NB: can happen when in promiscuous mode */
1738 vap->iv_stats.is_rx_mgtdiscard++;
1743 * disassoc frame format
1746 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1747 reason = le16toh(*(uint16_t *)frm);
1749 vap->iv_stats.is_rx_disassoc++;
1750 vap->iv_stats.is_rx_disassoc_code = reason;
1751 IEEE80211_NODE_STAT(ni, rx_disassoc);
1753 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1754 "recv disassociate (reason %d)", reason);
1755 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1759 case IEEE80211_FC0_SUBTYPE_ACTION:
1760 case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
1761 if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
1762 !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1763 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1764 wh, NULL, "%s", "not for us");
1765 vap->iv_stats.is_rx_mgtdiscard++;
1766 } else if (vap->iv_state != IEEE80211_S_RUN) {
1767 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1768 wh, NULL, "wrong state %s",
1769 ieee80211_state_name[vap->iv_state]);
1770 vap->iv_stats.is_rx_mgtdiscard++;
1772 if (ieee80211_parse_action(ni, m0) == 0)
1773 (void)ic->ic_recv_action(ni, wh, frm, efrm);
1777 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1778 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1779 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1780 case IEEE80211_FC0_SUBTYPE_ATIM:
1781 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1782 wh, NULL, "%s", "not handled");
1783 vap->iv_stats.is_rx_mgtdiscard++;
1787 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1788 wh, "mgt", "subtype 0x%x not handled", subtype);
1789 vap->iv_stats.is_rx_badsubtype++;
1797 sta_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
1800 case IEEE80211_FC0_SUBTYPE_BAR:
1801 ieee80211_recv_bar(ni, m);