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.
25 * $FreeBSD: head/sys/net80211/ieee80211_sta.c 203422 2010-02-03 10:07:43Z rpaulo $
29 * IEEE 802.11 Station mode support.
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #include <sys/endian.h>
43 #include <sys/errno.h>
45 #include <sys/sysctl.h>
48 #include <net/if_media.h>
49 #include <net/if_llc.h>
50 #include <net/ethernet.h>
51 #include <net/route.h>
55 #include <netproto/802_11/ieee80211_var.h>
56 #include <netproto/802_11/ieee80211_sta.h>
57 #include <netproto/802_11/ieee80211_input.h>
58 #include <netproto/802_11/ieee80211_ratectl.h>
59 #ifdef IEEE80211_SUPPORT_SUPERG
60 #include <netproto/802_11/ieee80211_superg.h>
63 #define IEEE80211_RATE2MBS(r) (((r) & IEEE80211_RATE_VAL) / 2)
65 static void sta_vattach(struct ieee80211vap *);
66 static void sta_beacon_miss(struct ieee80211vap *);
67 static int sta_newstate(struct ieee80211vap *, enum ieee80211_state, int);
68 static int sta_input(struct ieee80211_node *, struct mbuf *, int, int);
69 static void sta_recv_mgmt(struct ieee80211_node *, struct mbuf *,
70 int subtype, int rssi, int nf);
71 static void sta_recv_ctl(struct ieee80211_node *, struct mbuf *, int subtype);
74 ieee80211_sta_attach(struct ieee80211com *ic)
76 ic->ic_vattach[IEEE80211_M_STA] = sta_vattach;
80 ieee80211_sta_detach(struct ieee80211com *ic)
85 sta_vdetach(struct ieee80211vap *vap)
90 sta_vattach(struct ieee80211vap *vap)
92 vap->iv_newstate = sta_newstate;
93 vap->iv_input = sta_input;
94 vap->iv_recv_mgmt = sta_recv_mgmt;
95 vap->iv_recv_ctl = sta_recv_ctl;
96 vap->iv_opdetach = sta_vdetach;
97 vap->iv_bmiss = sta_beacon_miss;
101 * Handle a beacon miss event. The common code filters out
102 * spurious events that can happen when scanning and/or before
103 * reaching RUN state.
106 sta_beacon_miss(struct ieee80211vap *vap)
108 struct ieee80211com *ic = vap->iv_ic;
110 KASSERT((ic->ic_flags & IEEE80211_F_SCAN) == 0, ("scanning"));
111 KASSERT(vap->iv_state >= IEEE80211_S_RUN,
112 ("wrong state %s", ieee80211_state_name[vap->iv_state]));
114 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
115 "beacon miss, mode %s state %s\n",
116 ieee80211_opmode_name[vap->iv_opmode],
117 ieee80211_state_name[vap->iv_state]);
119 if (vap->iv_state == IEEE80211_S_CSA) {
121 * A Channel Switch is pending; assume we missed the
122 * beacon that would've completed the process and just
123 * force the switch. If we made a mistake we'll not
124 * find the AP on the new channel and fall back to a
127 ieee80211_csa_completeswitch(ic);
130 if (++vap->iv_bmiss_count < vap->iv_bmiss_max) {
132 * Send a directed probe req before falling back to a
133 * scan; if we receive a response ic_bmiss_count will
134 * be reset. Some cards mistakenly report beacon miss
135 * so this avoids the expensive scan if the ap is
138 ieee80211_send_probereq(vap->iv_bss, vap->iv_myaddr,
139 vap->iv_bss->ni_bssid, vap->iv_bss->ni_bssid,
140 vap->iv_bss->ni_essid, vap->iv_bss->ni_esslen);
143 vap->iv_bmiss_count = 0;
144 vap->iv_stats.is_beacon_miss++;
145 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
146 #ifdef IEEE80211_SUPPORT_SUPERG
147 struct ieee80211com *ic = vap->iv_ic;
150 * If we receive a beacon miss interrupt when using
151 * dynamic turbo, attempt to switch modes before
154 if (IEEE80211_ATH_CAP(vap, vap->iv_bss, IEEE80211_NODE_TURBOP))
155 ieee80211_dturbo_switch(vap,
156 ic->ic_bsschan->ic_flags ^ IEEE80211_CHAN_TURBO);
159 * Try to reassociate before scanning for a new ap.
161 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
164 * Somebody else is controlling state changes (e.g.
165 * a user-mode app) don't do anything that would
166 * confuse them; just drop into scan mode so they'll
167 * notified of the state change and given control.
169 ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
174 * Handle deauth with reason. We retry only for
175 * the cases where we might succeed. Otherwise
176 * we downgrade the ap and scan.
179 sta_authretry(struct ieee80211vap *vap, struct ieee80211_node *ni, int reason)
182 case IEEE80211_STATUS_SUCCESS: /* NB: MLME assoc */
183 case IEEE80211_STATUS_TIMEOUT:
184 case IEEE80211_REASON_ASSOC_EXPIRE:
185 case IEEE80211_REASON_NOT_AUTHED:
186 case IEEE80211_REASON_NOT_ASSOCED:
187 case IEEE80211_REASON_ASSOC_LEAVE:
188 case IEEE80211_REASON_ASSOC_NOT_AUTHED:
189 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, 1);
192 ieee80211_scan_assoc_fail(vap, vap->iv_bss->ni_macaddr, reason);
193 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
194 ieee80211_check_scan_current(vap);
200 * IEEE80211_M_STA vap state machine handler.
201 * This routine handles the main states in the 802.11 protocol.
204 sta_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
206 struct ieee80211com *ic = vap->iv_ic;
207 struct ieee80211_node *ni;
208 enum ieee80211_state ostate;
209 #ifdef IEEE80211_DEBUG
210 char ethstr[ETHER_ADDRSTRLEN + 1];
212 ostate = vap->iv_state;
213 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
214 __func__, ieee80211_state_name[ostate],
215 ieee80211_state_name[nstate], arg);
216 vap->iv_state = nstate; /* state transition */
217 callout_stop(&vap->iv_mgtsend);
218 if (ostate != IEEE80211_S_SCAN)
219 ieee80211_cancel_scan(vap); /* background scan */
220 ni = vap->iv_bss; /* NB: no reference held */
221 if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)
222 callout_stop(&vap->iv_swbmiss);
224 case IEEE80211_S_INIT:
226 case IEEE80211_S_SLEEP:
228 case IEEE80211_S_RUN:
229 IEEE80211_SEND_MGMT(ni,
230 IEEE80211_FC0_SUBTYPE_DISASSOC,
231 IEEE80211_REASON_ASSOC_LEAVE);
232 ieee80211_sta_leave(ni);
234 case IEEE80211_S_ASSOC:
235 IEEE80211_SEND_MGMT(ni,
236 IEEE80211_FC0_SUBTYPE_DEAUTH,
237 IEEE80211_REASON_AUTH_LEAVE);
239 case IEEE80211_S_SCAN:
240 ieee80211_cancel_scan(vap);
245 if (ostate != IEEE80211_S_INIT) {
246 /* NB: optimize INIT -> INIT case */
247 ieee80211_reset_bss(vap);
249 if (vap->iv_auth->ia_detach != NULL)
250 vap->iv_auth->ia_detach(vap);
252 case IEEE80211_S_SCAN:
254 case IEEE80211_S_INIT:
256 * Initiate a scan. We can come here as a result
257 * of an IEEE80211_IOC_SCAN_REQ too in which case
258 * the vap will be marked with IEEE80211_FEXT_SCANREQ
259 * and the scan request parameters will be present
260 * in iv_scanreq. Otherwise we do the default.
262 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
263 ieee80211_check_scan(vap,
264 vap->iv_scanreq_flags,
265 vap->iv_scanreq_duration,
266 vap->iv_scanreq_mindwell,
267 vap->iv_scanreq_maxdwell,
268 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
269 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
271 ieee80211_check_scan_current(vap);
273 case IEEE80211_S_SCAN:
274 case IEEE80211_S_AUTH:
275 case IEEE80211_S_ASSOC:
277 * These can happen either because of a timeout
278 * on an assoc/auth response or because of a
279 * change in state that requires a reset. For
280 * the former we're called with a non-zero arg
281 * that is the cause for the failure; pass this
282 * to the scan code so it can update state.
283 * Otherwise trigger a new scan unless we're in
284 * manual roaming mode in which case an application
285 * must issue an explicit scan request.
288 ieee80211_scan_assoc_fail(vap,
289 vap->iv_bss->ni_macaddr, arg);
290 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
291 ieee80211_check_scan_current(vap);
293 case IEEE80211_S_RUN: /* beacon miss */
295 * Beacon miss. Notify user space and if not
296 * under control of a user application (roaming
297 * manual) kick off a scan to re-connect.
299 ieee80211_sta_leave(ni);
300 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
301 ieee80211_check_scan_current(vap);
307 case IEEE80211_S_AUTH:
309 case IEEE80211_S_INIT:
310 case IEEE80211_S_SCAN:
311 IEEE80211_SEND_MGMT(ni,
312 IEEE80211_FC0_SUBTYPE_AUTH, 1);
314 case IEEE80211_S_AUTH:
315 case IEEE80211_S_ASSOC:
316 switch (arg & 0xff) {
317 case IEEE80211_FC0_SUBTYPE_AUTH:
319 IEEE80211_SEND_MGMT(ni,
320 IEEE80211_FC0_SUBTYPE_AUTH, 2);
322 case IEEE80211_FC0_SUBTYPE_DEAUTH:
323 sta_authretry(vap, ni, arg>>8);
327 case IEEE80211_S_RUN:
328 switch (arg & 0xff) {
329 case IEEE80211_FC0_SUBTYPE_AUTH:
330 IEEE80211_SEND_MGMT(ni,
331 IEEE80211_FC0_SUBTYPE_AUTH, 2);
332 vap->iv_state = ostate; /* stay RUN */
334 case IEEE80211_FC0_SUBTYPE_DEAUTH:
335 ieee80211_sta_leave(ni);
336 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
338 IEEE80211_SEND_MGMT(ni,
339 IEEE80211_FC0_SUBTYPE_AUTH, 1);
348 case IEEE80211_S_ASSOC:
350 case IEEE80211_S_AUTH:
351 case IEEE80211_S_ASSOC:
352 IEEE80211_SEND_MGMT(ni,
353 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
355 case IEEE80211_S_SLEEP: /* cannot happen */
356 case IEEE80211_S_RUN:
357 ieee80211_sta_leave(ni);
358 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
359 IEEE80211_SEND_MGMT(ni, arg ?
360 IEEE80211_FC0_SUBTYPE_REASSOC_REQ :
361 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
368 case IEEE80211_S_RUN:
369 if (vap->iv_flags & IEEE80211_F_WPA) {
370 /* XXX validate prerequisites */
373 case IEEE80211_S_RUN:
374 case IEEE80211_S_CSA:
376 case IEEE80211_S_AUTH: /* when join is done in fw */
377 case IEEE80211_S_ASSOC:
378 #ifdef IEEE80211_DEBUG
379 if (ieee80211_msg_debug(vap)) {
380 ieee80211_note(vap, "%s with %s ssid ",
381 (vap->iv_opmode == IEEE80211_M_STA ?
382 "associated" : "synchronized"),
383 kether_ntoa(ni->ni_bssid, ethstr));
384 ieee80211_print_essid(vap->iv_bss->ni_essid,
387 kprintf(" channel %d start %uMb\n",
388 ieee80211_chan2ieee(ic, ic->ic_curchan),
389 IEEE80211_RATE2MBS(ni->ni_txrate));
392 ieee80211_scan_assoc_success(vap, ni->ni_macaddr);
393 ieee80211_notify_node_join(ni,
394 arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
396 case IEEE80211_S_SLEEP:
397 ieee80211_sta_pwrsave(vap, 0);
402 ieee80211_sync_curchan(ic);
403 if (ostate != IEEE80211_S_RUN &&
404 (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)) {
406 * Start s/w beacon miss timer for devices w/o
407 * hardware support. We fudge a bit here since
408 * we're doing this in software.
410 vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS(
411 2 * vap->iv_bmissthreshold * ni->ni_intval);
412 vap->iv_swbmiss_count = 0;
413 callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
414 ieee80211_swbmiss_callout, vap);
417 * When 802.1x is not in use mark the port authorized
418 * at this point so traffic can flow.
420 if (ni->ni_authmode != IEEE80211_AUTH_8021X)
421 ieee80211_node_authorize(ni);
423 * Fake association when joining an existing bss.
425 if (ic->ic_newassoc != NULL)
426 ic->ic_newassoc(vap->iv_bss, ostate != IEEE80211_S_RUN);
428 case IEEE80211_S_CSA:
429 if (ostate != IEEE80211_S_RUN)
432 case IEEE80211_S_SLEEP:
433 ieee80211_sta_pwrsave(vap, 1);
437 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
438 "%s: unexpected state transition %s -> %s\n", __func__,
439 ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
446 * Return non-zero if the frame is an echo of a multicast
447 * frame sent by ourself. The dir is known to be DSTODS.
450 isdstods_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
452 #define QWH4(wh) ((const struct ieee80211_qosframe_addr4 *)wh)
453 #define WH4(wh) ((const struct ieee80211_frame_addr4 *)wh)
456 KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
458 if (!IEEE80211_IS_MULTICAST(wh->i_addr3))
460 sa = IEEE80211_QOS_HAS_SEQ(wh) ? QWH4(wh)->i_addr4 : WH4(wh)->i_addr4;
461 return IEEE80211_ADDR_EQ(sa, vap->iv_myaddr);
467 * Return non-zero if the frame is an echo of a multicast
468 * frame sent by ourself. The dir is known to be FROMDS.
471 isfromds_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
473 KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
475 if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
477 return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
481 * Decide if a received management frame should be
482 * printed when debugging is enabled. This filters some
483 * of the less interesting frames that come frequently
487 doprint(struct ieee80211vap *vap, int subtype)
490 case IEEE80211_FC0_SUBTYPE_BEACON:
491 return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
492 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
499 * Process a received frame. The node associated with the sender
500 * should be supplied. If nothing was found in the node table then
501 * the caller is assumed to supply a reference to iv_bss instead.
502 * The RSSI and a timestamp are also supplied. The RSSI data is used
503 * during AP scanning to select a AP to associate with; it can have
504 * any units so long as values have consistent units and higher values
505 * mean ``better signal''. The receive timestamp is currently not used
506 * by the 802.11 layer.
509 sta_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
511 #define SEQ_LEQ(a,b) ((int)((a)-(b)) <= 0)
512 #define HAS_SEQ(type) ((type & 0x4) == 0)
513 struct ieee80211vap *vap = ni->ni_vap;
514 struct ieee80211com *ic = ni->ni_ic;
515 struct ifnet *ifp = vap->iv_ifp;
516 struct ieee80211_frame *wh;
517 struct ieee80211_key *key;
518 struct ether_header *eh;
519 int hdrspace, need_tap = 1; /* mbuf need to be tapped. */
520 uint8_t dir, type, subtype, qos;
523 #ifdef IEEE80211_DEBUG
524 char ethstr[ETHER_ADDRSTRLEN + 1];
527 if (m->m_flags & M_AMPDU_MPDU) {
529 * Fastpath for A-MPDU reorder q resubmission. Frames
530 * w/ M_AMPDU_MPDU marked have already passed through
531 * here but were received out of order and been held on
532 * the reorder queue. When resubmitted they are marked
533 * with the M_AMPDU_MPDU flag and we can bypass most of
534 * the normal processing.
536 wh = mtod(m, struct ieee80211_frame *);
537 type = IEEE80211_FC0_TYPE_DATA;
538 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
539 subtype = IEEE80211_FC0_SUBTYPE_QOS;
540 hdrspace = ieee80211_hdrspace(ic, wh); /* XXX optimize? */
544 KASSERT(ni != NULL, ("null node"));
545 ni->ni_inact = ni->ni_inact_reload;
547 type = -1; /* undefined */
549 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
550 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
551 ni->ni_macaddr, NULL,
552 "too short (1): len %u", m->m_pkthdr.len);
553 vap->iv_stats.is_rx_tooshort++;
557 * Bit of a cheat here, we use a pointer for a 3-address
558 * frame format but don't reference fields past outside
559 * ieee80211_frame_min w/o first validating the data is
562 wh = mtod(m, struct ieee80211_frame *);
564 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
565 IEEE80211_FC0_VERSION_0) {
566 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
567 ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
568 wh->i_fc[0], wh->i_fc[1]);
569 vap->iv_stats.is_rx_badversion++;
573 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
574 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
575 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
576 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
578 if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) {
579 /* not interested in */
580 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
581 bssid, NULL, "%s", "not to bss");
582 vap->iv_stats.is_rx_wrongbss++;
585 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
587 if (HAS_SEQ(type) && !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
588 uint8_t tid = ieee80211_gettid(wh);
589 if (IEEE80211_QOS_HAS_SEQ(wh) &&
590 TID_TO_WME_AC(tid) >= WME_AC_VI)
591 ic->ic_wme.wme_hipri_traffic++;
592 rxseq = le16toh(*(uint16_t *)wh->i_seq);
593 if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
594 (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
595 SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
596 /* duplicate, discard */
597 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
599 "seqno <%u,%u> fragno <%u,%u> tid %u",
600 rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
601 ni->ni_rxseqs[tid] >>
602 IEEE80211_SEQ_SEQ_SHIFT,
603 rxseq & IEEE80211_SEQ_FRAG_MASK,
605 IEEE80211_SEQ_FRAG_MASK,
607 vap->iv_stats.is_rx_dup++;
608 IEEE80211_NODE_STAT(ni, rx_dup);
611 ni->ni_rxseqs[tid] = rxseq;
616 case IEEE80211_FC0_TYPE_DATA:
617 hdrspace = ieee80211_hdrspace(ic, wh);
618 if (m->m_len < hdrspace &&
619 (m = m_pullup(m, hdrspace)) == NULL) {
620 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
621 ni->ni_macaddr, NULL,
622 "data too short: expecting %u", hdrspace);
623 vap->iv_stats.is_rx_tooshort++;
627 * Handle A-MPDU re-ordering. If the frame is to be
628 * processed directly then ieee80211_ampdu_reorder
629 * will return 0; otherwise it has consumed the mbuf
630 * and we should do nothing more with it.
632 if ((m->m_flags & M_AMPDU) &&
633 (dir == IEEE80211_FC1_DIR_FROMDS ||
634 dir == IEEE80211_FC1_DIR_DSTODS) &&
635 ieee80211_ampdu_reorder(ni, m) != 0) {
640 if (dir == IEEE80211_FC1_DIR_FROMDS) {
641 if ((ifp->if_flags & IFF_SIMPLEX) &&
642 isfromds_mcastecho(vap, wh)) {
644 * In IEEE802.11 network, multicast
645 * packets sent from "me" are broadcast
646 * from the AP; silently discard for
649 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
650 wh, "data", "%s", "multicast echo");
651 vap->iv_stats.is_rx_mcastecho++;
654 if ((vap->iv_flags & IEEE80211_F_DWDS) &&
655 IEEE80211_IS_MULTICAST(wh->i_addr1)) {
657 * DWDS sta's must drop 3-address mcast frames
658 * as they will be sent separately as a 4-addr
659 * frame. Accepting the 3-addr frame will
660 * confuse the bridge into thinking the sending
661 * sta is located at the end of WDS link.
663 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
664 "3-address data", "%s", "DWDS enabled");
665 vap->iv_stats.is_rx_mcastecho++;
668 } else if (dir == IEEE80211_FC1_DIR_DSTODS) {
669 if ((vap->iv_flags & IEEE80211_F_DWDS) == 0) {
670 IEEE80211_DISCARD(vap,
671 IEEE80211_MSG_INPUT, wh, "4-address data",
672 "%s", "DWDS not enabled");
673 vap->iv_stats.is_rx_wrongdir++;
676 if ((ifp->if_flags & IFF_SIMPLEX) &&
677 isdstods_mcastecho(vap, wh)) {
679 * In IEEE802.11 network, multicast
680 * packets sent from "me" are broadcast
681 * from the AP; silently discard for
684 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
685 "4-address data", "%s", "multicast echo");
686 vap->iv_stats.is_rx_mcastecho++;
690 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
691 "data", "incorrect dir 0x%x", dir);
692 vap->iv_stats.is_rx_wrongdir++;
697 * Handle privacy requirements. Note that we
698 * must not be preempted from here until after
699 * we (potentially) call ieee80211_crypto_demic;
700 * otherwise we may violate assumptions in the
701 * crypto cipher modules used to do delayed update
702 * of replay sequence numbers.
704 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
705 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
707 * Discard encrypted frames when privacy is off.
709 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
710 wh, "WEP", "%s", "PRIVACY off");
711 vap->iv_stats.is_rx_noprivacy++;
712 IEEE80211_NODE_STAT(ni, rx_noprivacy);
715 key = ieee80211_crypto_decap(ni, m, hdrspace);
717 /* NB: stats+msgs handled in crypto_decap */
718 IEEE80211_NODE_STAT(ni, rx_wepfail);
721 wh = mtod(m, struct ieee80211_frame *);
722 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
724 /* XXX M_WEP and IEEE80211_F_PRIVACY */
729 * Save QoS bits for use below--before we strip the header.
731 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
732 qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
733 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
734 ((struct ieee80211_qosframe *)wh)->i_qos[0];
739 * Next up, any fragmentation.
741 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
742 m = ieee80211_defrag(ni, m, hdrspace);
744 /* Fragment dropped or frame not complete yet */
748 wh = NULL; /* no longer valid, catch any uses */
751 * Next strip any MSDU crypto bits.
753 if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
754 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
755 ni->ni_macaddr, "data", "%s", "demic error");
756 vap->iv_stats.is_rx_demicfail++;
757 IEEE80211_NODE_STAT(ni, rx_demicfail);
761 /* copy to listener after decrypt */
762 if (ieee80211_radiotap_active_vap(vap))
763 ieee80211_radiotap_rx(vap, m);
767 * Finally, strip the 802.11 header.
769 m = ieee80211_decap(vap, m, hdrspace);
771 /* XXX mask bit to check for both */
772 /* don't count Null data frames as errors */
773 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
774 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
776 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
777 ni->ni_macaddr, "data", "%s", "decap error");
778 vap->iv_stats.is_rx_decap++;
779 IEEE80211_NODE_STAT(ni, rx_decap);
782 eh = mtod(m, struct ether_header *);
783 if (!ieee80211_node_is_authorized(ni)) {
785 * Deny any non-PAE frames received prior to
786 * authorization. For open/shared-key
787 * authentication the port is mark authorized
788 * after authentication completes. For 802.1x
789 * the port is not marked authorized by the
790 * authenticator until the handshake has completed.
792 if (eh->ether_type != htons(ETHERTYPE_PAE)) {
793 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
794 eh->ether_shost, "data",
795 "unauthorized port: ether type 0x%x len %u",
796 eh->ether_type, m->m_pkthdr.len);
797 vap->iv_stats.is_rx_unauth++;
798 IEEE80211_NODE_STAT(ni, rx_unauth);
803 * When denying unencrypted frames, discard
804 * any non-PAE frames received without encryption.
806 if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
807 (key == NULL && (m->m_flags & M_WEP) == 0) &&
808 eh->ether_type != htons(ETHERTYPE_PAE)) {
810 * Drop unencrypted frames.
812 vap->iv_stats.is_rx_unencrypted++;
813 IEEE80211_NODE_STAT(ni, rx_unencrypted);
817 /* XXX require HT? */
818 if (qos & IEEE80211_QOS_AMSDU) {
819 m = ieee80211_decap_amsdu(ni, m);
821 return IEEE80211_FC0_TYPE_DATA;
823 #ifdef IEEE80211_SUPPORT_SUPERG
824 m = ieee80211_decap_fastframe(vap, ni, m);
826 return IEEE80211_FC0_TYPE_DATA;
829 ieee80211_deliver_data(vap, ni, m);
830 return IEEE80211_FC0_TYPE_DATA;
832 case IEEE80211_FC0_TYPE_MGT:
833 vap->iv_stats.is_rx_mgmt++;
834 IEEE80211_NODE_STAT(ni, rx_mgmt);
835 if (dir != IEEE80211_FC1_DIR_NODS) {
836 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
837 wh, "data", "incorrect dir 0x%x", dir);
838 vap->iv_stats.is_rx_wrongdir++;
841 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
842 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
843 ni->ni_macaddr, "mgt", "too short: len %u",
845 vap->iv_stats.is_rx_tooshort++;
848 #ifdef IEEE80211_DEBUG
849 if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
850 ieee80211_msg_dumppkts(vap)) {
851 if_printf(ifp, "received %s from %s rssi %d\n",
852 ieee80211_mgt_subtype_name[subtype >>
853 IEEE80211_FC0_SUBTYPE_SHIFT],
854 kether_ntoa(wh->i_addr2, ethstr), rssi);
857 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
858 if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
860 * Only shared key auth frames with a challenge
861 * should be encrypted, discard all others.
863 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
864 wh, ieee80211_mgt_subtype_name[subtype >>
865 IEEE80211_FC0_SUBTYPE_SHIFT],
866 "%s", "WEP set but not permitted");
867 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
870 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
872 * Discard encrypted frames when privacy is off.
874 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
875 wh, "mgt", "%s", "WEP set but PRIVACY off");
876 vap->iv_stats.is_rx_noprivacy++;
879 hdrspace = ieee80211_hdrspace(ic, wh);
880 key = ieee80211_crypto_decap(ni, m, hdrspace);
882 /* NB: stats+msgs handled in crypto_decap */
885 wh = mtod(m, struct ieee80211_frame *);
886 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
888 vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
891 case IEEE80211_FC0_TYPE_CTL:
892 vap->iv_stats.is_rx_ctl++;
893 IEEE80211_NODE_STAT(ni, rx_ctrl);
894 vap->iv_recv_ctl(ni, m, subtype);
898 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
899 wh, NULL, "bad frame type 0x%x", type);
900 /* should not come here */
904 IFNET_STAT_INC(ifp, ierrors, 1);
907 if (need_tap && ieee80211_radiotap_active_vap(vap))
908 ieee80211_radiotap_rx(vap, m);
916 sta_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
917 int rssi, int nf, uint16_t seq, uint16_t status)
919 struct ieee80211vap *vap = ni->ni_vap;
921 if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
922 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
923 ni->ni_macaddr, "open auth",
924 "bad sta auth mode %u", ni->ni_authmode);
925 vap->iv_stats.is_rx_bad_auth++; /* XXX */
928 if (vap->iv_state != IEEE80211_S_AUTH ||
929 seq != IEEE80211_AUTH_OPEN_RESPONSE) {
930 vap->iv_stats.is_rx_bad_auth++;
934 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
935 ni, "open auth failed (reason %d)", status);
936 vap->iv_stats.is_rx_auth_fail++;
937 vap->iv_stats.is_rx_authfail_code = status;
938 ieee80211_new_state(vap, IEEE80211_S_SCAN,
939 IEEE80211_SCAN_FAIL_STATUS);
941 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
945 sta_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
946 uint8_t *frm, uint8_t *efrm, int rssi, int nf,
947 uint16_t seq, uint16_t status)
949 struct ieee80211vap *vap = ni->ni_vap;
953 * NB: this can happen as we allow pre-shared key
954 * authentication to be enabled w/o wep being turned
955 * on so that configuration of these can be done
956 * in any order. It may be better to enforce the
957 * ordering in which case this check would just be
958 * for sanity/consistency.
960 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
961 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
962 ni->ni_macaddr, "shared key auth",
963 "%s", " PRIVACY is disabled");
967 * Pre-shared key authentication is evil; accept
968 * it only if explicitly configured (it is supported
969 * mainly for compatibility with clients like OS X).
971 if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
972 ni->ni_authmode != IEEE80211_AUTH_SHARED) {
973 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
974 ni->ni_macaddr, "shared key auth",
975 "bad sta auth mode %u", ni->ni_authmode);
976 vap->iv_stats.is_rx_bad_auth++; /* XXX maybe a unique error? */
981 if (frm + 1 < efrm) {
982 if ((frm[1] + 2) > (efrm - frm)) {
983 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
984 ni->ni_macaddr, "shared key auth",
986 frm[0], (int)((frm[1] + 2) - (efrm - frm)));
987 vap->iv_stats.is_rx_bad_auth++;
990 if (*frm == IEEE80211_ELEMID_CHALLENGE)
995 case IEEE80211_AUTH_SHARED_CHALLENGE:
996 case IEEE80211_AUTH_SHARED_RESPONSE:
997 if (challenge == NULL) {
998 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
999 ni->ni_macaddr, "shared key auth",
1000 "%s", "no challenge");
1001 vap->iv_stats.is_rx_bad_auth++;
1004 if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1005 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1006 ni->ni_macaddr, "shared key auth",
1007 "bad challenge len %d", challenge[1]);
1008 vap->iv_stats.is_rx_bad_auth++;
1014 if (vap->iv_state != IEEE80211_S_AUTH)
1017 case IEEE80211_AUTH_SHARED_PASS:
1018 if (ni->ni_challenge != NULL) {
1019 kfree(ni->ni_challenge, M_80211_NODE);
1020 ni->ni_challenge = NULL;
1023 IEEE80211_NOTE_FRAME(vap,
1024 IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, wh,
1025 "shared key auth failed (reason %d)", status);
1026 vap->iv_stats.is_rx_auth_fail++;
1027 vap->iv_stats.is_rx_authfail_code = status;
1030 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1032 case IEEE80211_AUTH_SHARED_CHALLENGE:
1033 if (!ieee80211_alloc_challenge(ni))
1035 /* XXX could optimize by passing recvd challenge */
1036 memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
1037 IEEE80211_SEND_MGMT(ni,
1038 IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1041 IEEE80211_DISCARD(vap, IEEE80211_MSG_AUTH,
1042 wh, "shared key auth", "bad seq %d", seq);
1043 vap->iv_stats.is_rx_bad_auth++;
1049 * Kick the state machine. This short-circuits
1050 * using the mgt frame timeout to trigger the
1053 if (vap->iv_state == IEEE80211_S_AUTH)
1054 ieee80211_new_state(vap, IEEE80211_S_SCAN,
1055 IEEE80211_SCAN_FAIL_STATUS);
1059 ieee80211_parse_wmeparams(struct ieee80211vap *vap, uint8_t *frm,
1060 const struct ieee80211_frame *wh)
1062 #define MS(_v, _f) (((_v) & _f) >> _f##_S)
1063 struct ieee80211_wme_state *wme = &vap->iv_ic->ic_wme;
1064 u_int len = frm[1], qosinfo;
1067 if (len < sizeof(struct ieee80211_wme_param)-2) {
1068 IEEE80211_DISCARD_IE(vap,
1069 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
1070 wh, "WME", "too short, len %u", len);
1073 qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)];
1074 qosinfo &= WME_QOSINFO_COUNT;
1075 /* XXX do proper check for wraparound */
1076 if (qosinfo == wme->wme_wmeChanParams.cap_info)
1078 frm += __offsetof(struct ieee80211_wme_param, params_acParams);
1079 for (i = 0; i < WME_NUM_AC; i++) {
1080 struct wmeParams *wmep =
1081 &wme->wme_wmeChanParams.cap_wmeParams[i];
1082 /* NB: ACI not used */
1083 wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
1084 wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
1085 wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
1086 wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
1087 wmep->wmep_txopLimit = LE_READ_2(frm+2);
1090 wme->wme_wmeChanParams.cap_info = qosinfo;
1096 * Process 11h Channel Switch Announcement (CSA) ie. If this
1097 * is the first CSA then initiate the switch. Otherwise we
1098 * track state and trigger completion and/or cancel of the switch.
1099 * XXX should be public for IBSS use
1102 ieee80211_parse_csaparams(struct ieee80211vap *vap, uint8_t *frm,
1103 const struct ieee80211_frame *wh)
1105 struct ieee80211com *ic = vap->iv_ic;
1106 const struct ieee80211_csa_ie *csa =
1107 (const struct ieee80211_csa_ie *) frm;
1109 KASSERT(vap->iv_state >= IEEE80211_S_RUN,
1110 ("state %s", ieee80211_state_name[vap->iv_state]));
1112 if (csa->csa_mode > 1) {
1113 IEEE80211_DISCARD_IE(vap,
1114 IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1115 wh, "CSA", "invalid mode %u", csa->csa_mode);
1118 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
1120 * Convert the channel number to a channel reference. We
1121 * try first to preserve turbo attribute of the current
1122 * channel then fallback. Note this will not work if the
1123 * CSA specifies a channel that requires a band switch (e.g.
1124 * 11a => 11g). This is intentional as 11h is defined only
1125 * for 5GHz/11a and because the switch does not involve a
1126 * reassociation, protocol state (capabilities, negotated
1127 * rates, etc) may/will be wrong.
1129 struct ieee80211_channel *c =
1130 ieee80211_find_channel_byieee(ic, csa->csa_newchan,
1131 (ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALLTURBO));
1133 c = ieee80211_find_channel_byieee(ic,
1135 (ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALL));
1137 IEEE80211_DISCARD_IE(vap,
1138 IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1139 wh, "CSA", "invalid channel %u",
1144 #if IEEE80211_CSA_COUNT_MIN > 0
1145 if (csa->csa_count < IEEE80211_CSA_COUNT_MIN) {
1147 * Require at least IEEE80211_CSA_COUNT_MIN count to
1148 * reduce the risk of being redirected by a fabricated
1149 * CSA. If a valid CSA is dropped we'll still get a
1150 * beacon miss when the AP leaves the channel so we'll
1151 * eventually follow to the new channel.
1153 * NOTE: this violates the 11h spec that states that
1154 * count may be any value and if 0 then a switch
1155 * should happen asap.
1157 IEEE80211_DISCARD_IE(vap,
1158 IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1159 wh, "CSA", "count %u too small, must be >= %u",
1160 csa->csa_count, IEEE80211_CSA_COUNT_MIN);
1164 ieee80211_csa_startswitch(ic, c, csa->csa_mode, csa->csa_count);
1167 * Validate this ie against the initial CSA. We require
1168 * mode and channel not change and the count must be
1169 * monotonically decreasing. This may be pointless and
1170 * canceling the switch as a result may be too paranoid but
1171 * in the worst case if we drop out of CSA because of this
1172 * and the AP does move then we'll just end up taking a
1173 * beacon miss and scan to find the AP.
1175 * XXX may want <= on count as we also process ProbeResp
1176 * frames and those may come in w/ the same count as the
1177 * previous beacon; but doing so leaves us open to a stuck
1178 * count until we add a dead-man timer
1180 if (!(csa->csa_count < ic->ic_csa_count &&
1181 csa->csa_mode == ic->ic_csa_mode &&
1182 csa->csa_newchan == ieee80211_chan2ieee(ic, ic->ic_csa_newchan))) {
1183 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_DOTH, wh,
1184 "CSA ie mismatch, initial ie <%d,%d,%d>, "
1185 "this ie <%d,%d,%d>", ic->ic_csa_mode,
1186 ic->ic_csa_newchan->ic_ieee, ic->ic_csa_count,
1187 csa->csa_mode, csa->csa_newchan, csa->csa_count);
1188 ieee80211_csa_cancelswitch(ic);
1190 if (csa->csa_count <= 1)
1191 ieee80211_csa_completeswitch(ic);
1193 ic->ic_csa_count = csa->csa_count;
1201 * Return non-zero if a background scan may be continued:
1202 * o bg scan is active
1203 * o no channel switch is pending
1204 * o there has not been any traffic recently
1206 * Note we do not check if there is an administrative enable;
1207 * this is only done to start the scan. We assume that any
1208 * change in state will be accompanied by a request to cancel
1209 * active scans which will otherwise cause this test to fail.
1212 contbgscan(struct ieee80211vap *vap)
1214 struct ieee80211com *ic = vap->iv_ic;
1216 return ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) &&
1217 (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1218 vap->iv_state == IEEE80211_S_RUN && /* XXX? */
1219 time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1223 * Return non-zero if a backgrond scan may be started:
1224 * o bg scanning is administratively enabled
1225 * o no channel switch is pending
1226 * o we are not boosted on a dynamic turbo channel
1227 * o there has not been a scan recently
1228 * o there has not been any traffic recently
1231 startbgscan(struct ieee80211vap *vap)
1233 struct ieee80211com *ic = vap->iv_ic;
1235 return ((vap->iv_flags & IEEE80211_F_BGSCAN) &&
1236 (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1237 #ifdef IEEE80211_SUPPORT_SUPERG
1238 !IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1240 time_after(ticks, ic->ic_lastscan + vap->iv_bgscanintvl) &&
1241 time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1245 sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1246 int subtype, int rssi, int nf)
1248 #define ISPROBE(_st) ((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1249 #define ISREASSOC(_st) ((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
1250 struct ieee80211vap *vap = ni->ni_vap;
1251 struct ieee80211com *ic = ni->ni_ic;
1252 struct ieee80211_frame *wh;
1253 uint8_t *frm, *efrm;
1254 uint8_t *rates, *xrates, *wme, *htcap, *htinfo;
1257 wh = mtod(m0, struct ieee80211_frame *);
1258 frm = (uint8_t *)&wh[1];
1259 efrm = mtod(m0, uint8_t *) + m0->m_len;
1261 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1262 case IEEE80211_FC0_SUBTYPE_BEACON: {
1263 struct ieee80211_scanparams scan;
1265 * We process beacon/probe response frames:
1266 * o when scanning, or
1267 * o station mode when associated (to collect state
1268 * updates such as 802.11g slot time)
1269 * Frames otherwise received are discarded.
1271 if (!((ic->ic_flags & IEEE80211_F_SCAN) || ni->ni_associd)) {
1272 vap->iv_stats.is_rx_mgtdiscard++;
1275 /* XXX probe response in sta mode when !scanning? */
1276 if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
1279 * Count frame now that we know it's to be processed.
1281 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1282 vap->iv_stats.is_rx_beacon++; /* XXX remove */
1283 IEEE80211_NODE_STAT(ni, rx_beacons);
1285 IEEE80211_NODE_STAT(ni, rx_proberesp);
1287 * When operating in station mode, check for state updates.
1288 * Be careful to ignore beacons received while doing a
1289 * background scan. We consider only 11g/WMM stuff right now.
1291 if (ni->ni_associd != 0 &&
1292 ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
1293 IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
1294 /* record tsf of last beacon */
1295 memcpy(ni->ni_tstamp.data, scan.tstamp,
1296 sizeof(ni->ni_tstamp));
1297 /* count beacon frame for s/w bmiss handling */
1298 vap->iv_swbmiss_count++;
1299 vap->iv_bmiss_count = 0;
1300 if (ni->ni_erp != scan.erp) {
1301 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1303 "erp change: was 0x%x, now 0x%x",
1304 ni->ni_erp, scan.erp);
1305 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1306 (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1307 ic->ic_flags |= IEEE80211_F_USEPROT;
1309 ic->ic_flags &= ~IEEE80211_F_USEPROT;
1310 ni->ni_erp = scan.erp;
1312 /* XXX driver notification */
1314 if ((ni->ni_capinfo ^ scan.capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) {
1315 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1317 "capabilities change: was 0x%x, now 0x%x",
1318 ni->ni_capinfo, scan.capinfo);
1320 * NB: we assume short preamble doesn't
1321 * change dynamically
1323 ieee80211_set_shortslottime(ic,
1324 IEEE80211_IS_CHAN_A(ic->ic_bsschan) ||
1325 (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1326 ni->ni_capinfo = (ni->ni_capinfo &~ IEEE80211_CAPINFO_SHORT_SLOTTIME)
1327 | (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME);
1330 if (scan.wme != NULL &&
1331 (ni->ni_flags & IEEE80211_NODE_QOS) &&
1332 ieee80211_parse_wmeparams(vap, scan.wme, wh) > 0)
1333 ieee80211_wme_updateparams(vap);
1334 #ifdef IEEE80211_SUPPORT_SUPERG
1335 if (scan.ath != NULL)
1336 ieee80211_parse_athparams(ni, scan.ath, wh);
1338 if (scan.htcap != NULL && scan.htinfo != NULL &&
1339 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1340 ieee80211_ht_updateparams(ni,
1341 scan.htcap, scan.htinfo);
1342 /* XXX state changes? */
1344 if (scan.tim != NULL) {
1345 struct ieee80211_tim_ie *tim =
1346 (struct ieee80211_tim_ie *) scan.tim;
1348 int aid = IEEE80211_AID(ni->ni_associd);
1349 int ix = aid / NBBY;
1350 int min = tim->tim_bitctl &~ 1;
1351 int max = tim->tim_len + min - 4;
1352 if ((tim->tim_bitctl&1) ||
1353 (min <= ix && ix <= max &&
1354 isset(tim->tim_bitmap - min, aid))) {
1356 * XXX Do not let bg scan kick off
1357 * we are expecting data.
1359 ic->ic_lastdata = ticks;
1360 ieee80211_sta_pwrsave(vap, 0);
1363 ni->ni_dtim_count = tim->tim_count;
1364 ni->ni_dtim_period = tim->tim_period;
1366 if (scan.csa != NULL &&
1367 (vap->iv_flags & IEEE80211_F_DOTH))
1368 ieee80211_parse_csaparams(vap, scan.csa, wh);
1369 else if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
1371 * No CSA ie or 11h disabled, but a channel
1372 * switch is pending; drop out so we aren't
1373 * stuck in CSA state. If the AP really is
1374 * moving we'll get a beacon miss and scan.
1376 ieee80211_csa_cancelswitch(ic);
1379 * If scanning, pass the info to the scan module.
1380 * Otherwise, check if it's the right time to do
1381 * a background scan. Background scanning must
1382 * be enabled and we must not be operating in the
1383 * turbo phase of dynamic turbo mode. Then,
1384 * it's been a while since the last background
1385 * scan and if no data frames have come through
1386 * recently, kick off a scan. Note that this
1387 * is the mechanism by which a background scan
1388 * is started _and_ continued each time we
1389 * return on-channel to receive a beacon from
1392 if (ic->ic_flags & IEEE80211_F_SCAN) {
1393 ieee80211_add_scan(vap, &scan, wh,
1395 } else if (contbgscan(vap)) {
1396 ieee80211_bg_scan(vap, 0);
1397 } else if (startbgscan(vap)) {
1398 vap->iv_stats.is_scan_bg++;
1400 /* wakeup if we are sleeing */
1401 ieee80211_set_pwrsave(vap, 0);
1403 ieee80211_bg_scan(vap, 0);
1408 * If scanning, just pass information to the scan module.
1410 if (ic->ic_flags & IEEE80211_F_SCAN) {
1411 if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1413 * Actively scanning a channel marked passive;
1414 * send a probe request now that we know there
1415 * is 802.11 traffic present.
1417 * XXX check if the beacon we recv'd gives
1418 * us what we need and suppress the probe req
1420 ieee80211_probe_curchan(vap, 1);
1421 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1423 ieee80211_add_scan(vap, &scan, wh, subtype, rssi, nf);
1429 case IEEE80211_FC0_SUBTYPE_AUTH: {
1430 uint16_t algo, seq, status;
1438 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1439 algo = le16toh(*(uint16_t *)frm);
1440 seq = le16toh(*(uint16_t *)(frm + 2));
1441 status = le16toh(*(uint16_t *)(frm + 4));
1442 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1443 "recv auth frame with algorithm %d seq %d", algo, seq);
1445 if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1446 IEEE80211_DISCARD(vap,
1447 IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1448 wh, "auth", "%s", "TKIP countermeasures enabled");
1449 vap->iv_stats.is_rx_auth_countermeasures++;
1450 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1451 ieee80211_send_error(ni, wh->i_addr2,
1452 IEEE80211_FC0_SUBTYPE_AUTH,
1453 IEEE80211_REASON_MIC_FAILURE);
1457 if (algo == IEEE80211_AUTH_ALG_SHARED)
1458 sta_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1460 else if (algo == IEEE80211_AUTH_ALG_OPEN)
1461 sta_auth_open(ni, wh, rssi, nf, seq, status);
1463 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1464 wh, "auth", "unsupported alg %d", algo);
1465 vap->iv_stats.is_rx_auth_unsupported++;
1471 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1472 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
1473 uint16_t capinfo, associd;
1476 if (vap->iv_state != IEEE80211_S_ASSOC) {
1477 vap->iv_stats.is_rx_mgtdiscard++;
1482 * asresp frame format
1483 * [2] capability information
1485 * [2] association ID
1486 * [tlv] supported rates
1487 * [tlv] extended supported rates
1489 * [tlv] HT capabilities
1492 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1494 capinfo = le16toh(*(uint16_t *)frm);
1496 status = le16toh(*(uint16_t *)frm);
1499 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1500 wh->i_addr2, "%sassoc failed (reason %d)",
1501 ISREASSOC(subtype) ? "re" : "", status);
1502 vap->iv_stats.is_rx_auth_fail++; /* XXX */
1505 associd = le16toh(*(uint16_t *)frm);
1508 rates = xrates = wme = htcap = htinfo = NULL;
1509 while (efrm - frm > 1) {
1510 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1512 case IEEE80211_ELEMID_RATES:
1515 case IEEE80211_ELEMID_XRATES:
1518 case IEEE80211_ELEMID_HTCAP:
1521 case IEEE80211_ELEMID_HTINFO:
1524 case IEEE80211_ELEMID_VENDOR:
1527 else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1529 * Accept pre-draft HT ie's if the
1530 * standard ones have not been seen.
1532 if (ishtcapoui(frm)) {
1535 } else if (ishtinfooui(frm)) {
1540 /* XXX Atheros OUI support */
1546 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1548 IEEE80211_VERIFY_ELEMENT(xrates,
1549 IEEE80211_RATE_MAXSIZE - rates[1], return);
1550 rate = ieee80211_setup_rates(ni, rates, xrates,
1552 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1553 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1554 if (rate & IEEE80211_RATE_BASIC) {
1555 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1557 "%sassoc failed (rate set mismatch)",
1558 ISREASSOC(subtype) ? "re" : "");
1559 vap->iv_stats.is_rx_assoc_norate++;
1560 ieee80211_new_state(vap, IEEE80211_S_SCAN,
1561 IEEE80211_SCAN_FAIL_STATUS);
1565 ni->ni_capinfo = capinfo;
1566 ni->ni_associd = associd;
1567 if (ni->ni_jointime == 0)
1568 ni->ni_jointime = time_uptime;
1570 ieee80211_parse_wmeparams(vap, wme, wh) >= 0) {
1571 ni->ni_flags |= IEEE80211_NODE_QOS;
1572 ieee80211_wme_updateparams(vap);
1574 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1576 * Setup HT state according to the negotiation.
1578 * NB: shouldn't need to check if HT use is enabled but some
1579 * ap's send back HT ie's even when we don't indicate we
1580 * are HT capable in our AssocReq.
1582 if (htcap != NULL && htinfo != NULL &&
1583 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1584 ieee80211_ht_node_init(ni);
1585 ieee80211_ht_updateparams(ni, htcap, htinfo);
1586 ieee80211_setup_htrates(ni, htcap,
1587 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1588 ieee80211_setup_basic_htrates(ni, htinfo);
1589 ieee80211_node_setuptxparms(ni);
1590 ieee80211_ratectl_node_init(ni);
1592 #ifdef IEEE80211_SUPPORT_SUPERG
1593 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_ATH))
1594 ieee80211_ff_node_init(ni);
1598 * Configure state now that we are associated.
1600 * XXX may need different/additional driver callbacks?
1602 if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1603 (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1604 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1605 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1607 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1608 ic->ic_flags |= IEEE80211_F_USEBARKER;
1610 ieee80211_set_shortslottime(ic,
1611 IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1612 (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1614 * Honor ERP protection.
1616 * NB: ni_erp should zero for non-11g operation.
1618 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1619 (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1620 ic->ic_flags |= IEEE80211_F_USEPROT;
1622 ic->ic_flags &= ~IEEE80211_F_USEPROT;
1623 IEEE80211_NOTE_MAC(vap,
1624 IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, wh->i_addr2,
1625 "%sassoc success at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
1626 ISREASSOC(subtype) ? "re" : "",
1627 IEEE80211_NODE_AID(ni),
1628 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
1629 ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
1630 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
1631 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
1632 ni->ni_flags & IEEE80211_NODE_HT ?
1633 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
1634 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
1635 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
1636 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
1637 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
1638 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
1639 ", fast-frames" : "",
1640 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
1643 ieee80211_new_state(vap, IEEE80211_S_RUN, subtype);
1647 case IEEE80211_FC0_SUBTYPE_DEAUTH: {
1650 if (vap->iv_state == IEEE80211_S_SCAN) {
1651 vap->iv_stats.is_rx_mgtdiscard++;
1654 if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1655 /* NB: can happen when in promiscuous mode */
1656 vap->iv_stats.is_rx_mgtdiscard++;
1661 * deauth frame format
1664 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1665 reason = le16toh(*(uint16_t *)frm);
1667 vap->iv_stats.is_rx_deauth++;
1668 vap->iv_stats.is_rx_deauth_code = reason;
1669 IEEE80211_NODE_STAT(ni, rx_deauth);
1671 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1672 "recv deauthenticate (reason %d)", reason);
1673 ieee80211_new_state(vap, IEEE80211_S_AUTH,
1674 (reason << 8) | IEEE80211_FC0_SUBTYPE_DEAUTH);
1678 case IEEE80211_FC0_SUBTYPE_DISASSOC: {
1681 if (vap->iv_state != IEEE80211_S_RUN &&
1682 vap->iv_state != IEEE80211_S_ASSOC &&
1683 vap->iv_state != IEEE80211_S_AUTH) {
1684 vap->iv_stats.is_rx_mgtdiscard++;
1687 if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1688 /* NB: can happen when in promiscuous mode */
1689 vap->iv_stats.is_rx_mgtdiscard++;
1694 * disassoc frame format
1697 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1698 reason = le16toh(*(uint16_t *)frm);
1700 vap->iv_stats.is_rx_disassoc++;
1701 vap->iv_stats.is_rx_disassoc_code = reason;
1702 IEEE80211_NODE_STAT(ni, rx_disassoc);
1704 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1705 "recv disassociate (reason %d)", reason);
1706 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1710 case IEEE80211_FC0_SUBTYPE_ACTION:
1711 if (vap->iv_state == IEEE80211_S_RUN) {
1712 if (ieee80211_parse_action(ni, m0) == 0)
1713 ic->ic_recv_action(ni, wh, frm, efrm);
1715 vap->iv_stats.is_rx_mgtdiscard++;
1718 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1719 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1720 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1721 vap->iv_stats.is_rx_mgtdiscard++;
1724 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1725 wh, "mgt", "subtype 0x%x not handled", subtype);
1726 vap->iv_stats.is_rx_badsubtype++;
1734 sta_recv_ctl(struct ieee80211_node *ni, struct mbuf *m0, int subtype)