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 $
30 * IEEE 802.11 Station mode support.
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/kernel.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
43 #include <sys/endian.h>
44 #include <sys/errno.h>
46 #include <sys/sysctl.h>
49 #include <net/if_media.h>
50 #include <net/if_llc.h>
51 #include <net/ethernet.h>
52 #include <net/route.h>
56 #include <netproto/802_11/ieee80211_var.h>
57 #include <netproto/802_11/ieee80211_sta.h>
58 #include <netproto/802_11/ieee80211_input.h>
59 #ifdef IEEE80211_SUPPORT_SUPERG
60 #include <netproto/802_11/ieee80211_superg.h>
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;
210 ostate = vap->iv_state;
211 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
212 __func__, ieee80211_state_name[ostate],
213 ieee80211_state_name[nstate], arg);
214 vap->iv_state = nstate; /* state transition */
215 callout_stop(&vap->iv_mgtsend); /* XXX callout_drain */
216 if (ostate != IEEE80211_S_SCAN)
217 ieee80211_cancel_scan(vap); /* background scan */
218 ni = vap->iv_bss; /* NB: no reference held */
219 if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)
220 callout_stop(&vap->iv_swbmiss);
222 case IEEE80211_S_INIT:
224 case IEEE80211_S_SLEEP:
226 case IEEE80211_S_RUN:
227 IEEE80211_SEND_MGMT(ni,
228 IEEE80211_FC0_SUBTYPE_DISASSOC,
229 IEEE80211_REASON_ASSOC_LEAVE);
230 ieee80211_sta_leave(ni);
232 case IEEE80211_S_ASSOC:
233 IEEE80211_SEND_MGMT(ni,
234 IEEE80211_FC0_SUBTYPE_DEAUTH,
235 IEEE80211_REASON_AUTH_LEAVE);
237 case IEEE80211_S_SCAN:
238 ieee80211_cancel_scan(vap);
243 if (ostate != IEEE80211_S_INIT) {
244 /* NB: optimize INIT -> INIT case */
245 ieee80211_reset_bss(vap);
247 if (vap->iv_auth->ia_detach != NULL)
248 vap->iv_auth->ia_detach(vap);
250 case IEEE80211_S_SCAN:
252 case IEEE80211_S_INIT:
254 * Initiate a scan. We can come here as a result
255 * of an IEEE80211_IOC_SCAN_REQ too in which case
256 * the vap will be marked with IEEE80211_FEXT_SCANREQ
257 * and the scan request parameters will be present
258 * in iv_scanreq. Otherwise we do the default.
260 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
261 ieee80211_check_scan(vap,
262 vap->iv_scanreq_flags,
263 vap->iv_scanreq_duration,
264 vap->iv_scanreq_mindwell,
265 vap->iv_scanreq_maxdwell,
266 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
267 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
269 ieee80211_check_scan_current(vap);
271 case IEEE80211_S_SCAN:
272 case IEEE80211_S_AUTH:
273 case IEEE80211_S_ASSOC:
275 * These can happen either because of a timeout
276 * on an assoc/auth response or because of a
277 * change in state that requires a reset. For
278 * the former we're called with a non-zero arg
279 * that is the cause for the failure; pass this
280 * to the scan code so it can update state.
281 * Otherwise trigger a new scan unless we're in
282 * manual roaming mode in which case an application
283 * must issue an explicit scan request.
286 ieee80211_scan_assoc_fail(vap,
287 vap->iv_bss->ni_macaddr, arg);
288 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
289 ieee80211_check_scan_current(vap);
291 case IEEE80211_S_RUN: /* beacon miss */
293 * Beacon miss. Notify user space and if not
294 * under control of a user application (roaming
295 * manual) kick off a scan to re-connect.
297 ieee80211_sta_leave(ni);
298 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
299 ieee80211_check_scan_current(vap);
305 case IEEE80211_S_AUTH:
307 case IEEE80211_S_INIT:
308 case IEEE80211_S_SCAN:
309 IEEE80211_SEND_MGMT(ni,
310 IEEE80211_FC0_SUBTYPE_AUTH, 1);
312 case IEEE80211_S_AUTH:
313 case IEEE80211_S_ASSOC:
314 switch (arg & 0xff) {
315 case IEEE80211_FC0_SUBTYPE_AUTH:
317 IEEE80211_SEND_MGMT(ni,
318 IEEE80211_FC0_SUBTYPE_AUTH, 2);
320 case IEEE80211_FC0_SUBTYPE_DEAUTH:
321 sta_authretry(vap, ni, arg>>8);
325 case IEEE80211_S_RUN:
326 switch (arg & 0xff) {
327 case IEEE80211_FC0_SUBTYPE_AUTH:
328 IEEE80211_SEND_MGMT(ni,
329 IEEE80211_FC0_SUBTYPE_AUTH, 2);
330 vap->iv_state = ostate; /* stay RUN */
332 case IEEE80211_FC0_SUBTYPE_DEAUTH:
333 ieee80211_sta_leave(ni);
334 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
336 IEEE80211_SEND_MGMT(ni,
337 IEEE80211_FC0_SUBTYPE_AUTH, 1);
346 case IEEE80211_S_ASSOC:
348 case IEEE80211_S_AUTH:
349 case IEEE80211_S_ASSOC:
350 IEEE80211_SEND_MGMT(ni,
351 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
353 case IEEE80211_S_SLEEP: /* cannot happen */
354 case IEEE80211_S_RUN:
355 ieee80211_sta_leave(ni);
356 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
357 IEEE80211_SEND_MGMT(ni, arg ?
358 IEEE80211_FC0_SUBTYPE_REASSOC_REQ :
359 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
366 case IEEE80211_S_RUN:
367 if (vap->iv_flags & IEEE80211_F_WPA) {
368 /* XXX validate prerequisites */
371 case IEEE80211_S_RUN:
372 case IEEE80211_S_CSA:
374 case IEEE80211_S_AUTH: /* when join is done in fw */
375 case IEEE80211_S_ASSOC:
376 #ifdef IEEE80211_DEBUG
377 if (ieee80211_msg_debug(vap)) {
378 ieee80211_note(vap, "%s with %6D ssid ",
379 (vap->iv_opmode == IEEE80211_M_STA ?
380 "associated" : "synchronized"),
382 ieee80211_print_essid(vap->iv_bss->ni_essid,
385 kprintf(" channel %d start %uMb\n",
386 ieee80211_chan2ieee(ic, ic->ic_curchan),
387 IEEE80211_RATE2MBS(ni->ni_txrate));
390 ieee80211_scan_assoc_success(vap, ni->ni_macaddr);
391 ieee80211_notify_node_join(ni,
392 arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
394 case IEEE80211_S_SLEEP:
395 ieee80211_sta_pwrsave(vap, 0);
400 ieee80211_sync_curchan(ic);
401 if (ostate != IEEE80211_S_RUN &&
402 (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)) {
404 * Start s/w beacon miss timer for devices w/o
405 * hardware support. We fudge a bit here since
406 * we're doing this in software.
408 vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS(
409 2 * vap->iv_bmissthreshold * ni->ni_intval);
410 vap->iv_swbmiss_count = 0;
411 callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
412 ieee80211_swbmiss_callout, vap);
415 * When 802.1x is not in use mark the port authorized
416 * at this point so traffic can flow.
418 if (ni->ni_authmode != IEEE80211_AUTH_8021X)
419 ieee80211_node_authorize(ni);
421 * Fake association when joining an existing bss.
423 if (ic->ic_newassoc != NULL)
424 ic->ic_newassoc(vap->iv_bss, ostate != IEEE80211_S_RUN);
426 case IEEE80211_S_CSA:
427 if (ostate != IEEE80211_S_RUN)
430 case IEEE80211_S_SLEEP:
431 ieee80211_sta_pwrsave(vap, 1);
435 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
436 "%s: unexpected state transition %s -> %s\n", __func__,
437 ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
444 * Return non-zero if the frame is an echo of a multicast
445 * frame sent by ourself. The dir is known to be DSTODS.
448 isdstods_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
450 #define QWH4(wh) ((const struct ieee80211_qosframe_addr4 *)wh)
451 #define WH4(wh) ((const struct ieee80211_frame_addr4 *)wh)
454 KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
456 if (!IEEE80211_IS_MULTICAST(wh->i_addr3))
458 sa = IEEE80211_QOS_HAS_SEQ(wh) ? QWH4(wh)->i_addr4 : WH4(wh)->i_addr4;
459 return IEEE80211_ADDR_EQ(sa, vap->iv_myaddr);
465 * Return non-zero if the frame is an echo of a multicast
466 * frame sent by ourself. The dir is known to be FROMDS.
469 isfromds_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
471 KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
473 if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
475 return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
479 * Decide if a received management frame should be
480 * printed when debugging is enabled. This filters some
481 * of the less interesting frames that come frequently
485 doprint(struct ieee80211vap *vap, int subtype)
488 case IEEE80211_FC0_SUBTYPE_BEACON:
489 return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
490 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
497 * Process a received frame. The node associated with the sender
498 * should be supplied. If nothing was found in the node table then
499 * the caller is assumed to supply a reference to iv_bss instead.
500 * The RSSI and a timestamp are also supplied. The RSSI data is used
501 * during AP scanning to select a AP to associate with; it can have
502 * any units so long as values have consistent units and higher values
503 * mean ``better signal''. The receive timestamp is currently not used
504 * by the 802.11 layer.
507 sta_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
509 #define SEQ_LEQ(a,b) ((int)((a)-(b)) <= 0)
510 #define HAS_SEQ(type) ((type & 0x4) == 0)
511 struct ieee80211vap *vap = ni->ni_vap;
512 struct ieee80211com *ic = ni->ni_ic;
513 struct ifnet *ifp = vap->iv_ifp;
514 struct ieee80211_frame *wh;
515 struct ieee80211_key *key;
516 struct ether_header *eh;
517 int hdrspace, need_tap = 1; /* mbuf need to be tapped. */
518 uint8_t dir, type, subtype, qos;
522 if (m->m_flags & M_AMPDU_MPDU) {
524 * Fastpath for A-MPDU reorder q resubmission. Frames
525 * w/ M_AMPDU_MPDU marked have already passed through
526 * here but were received out of order and been held on
527 * the reorder queue. When resubmitted they are marked
528 * with the M_AMPDU_MPDU flag and we can bypass most of
529 * the normal processing.
531 wh = mtod(m, struct ieee80211_frame *);
532 type = IEEE80211_FC0_TYPE_DATA;
533 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
534 subtype = IEEE80211_FC0_SUBTYPE_QOS;
535 hdrspace = ieee80211_hdrspace(ic, wh); /* XXX optimize? */
539 KASSERT(ni != NULL, ("null node"));
540 ni->ni_inact = ni->ni_inact_reload;
542 type = -1; /* undefined */
544 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
545 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
546 ni->ni_macaddr, NULL,
547 "too short (1): len %u", m->m_pkthdr.len);
548 vap->iv_stats.is_rx_tooshort++;
552 * Bit of a cheat here, we use a pointer for a 3-address
553 * frame format but don't reference fields past outside
554 * ieee80211_frame_min w/o first validating the data is
557 wh = mtod(m, struct ieee80211_frame *);
559 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
560 IEEE80211_FC0_VERSION_0) {
561 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
562 ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
563 wh->i_fc[0], wh->i_fc[1]);
564 vap->iv_stats.is_rx_badversion++;
568 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
569 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
570 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
571 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
573 if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) {
574 /* not interested in */
575 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
576 bssid, NULL, "%s", "not to bss");
577 vap->iv_stats.is_rx_wrongbss++;
580 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
582 if (HAS_SEQ(type) && !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
583 uint8_t tid = ieee80211_gettid(wh);
584 if (IEEE80211_QOS_HAS_SEQ(wh) &&
585 TID_TO_WME_AC(tid) >= WME_AC_VI)
586 ic->ic_wme.wme_hipri_traffic++;
587 rxseq = le16toh(*(uint16_t *)wh->i_seq);
588 if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
589 (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
590 SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
591 /* duplicate, discard */
592 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
594 "seqno <%u,%u> fragno <%u,%u> tid %u",
595 rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
596 ni->ni_rxseqs[tid] >>
597 IEEE80211_SEQ_SEQ_SHIFT,
598 rxseq & IEEE80211_SEQ_FRAG_MASK,
600 IEEE80211_SEQ_FRAG_MASK,
602 vap->iv_stats.is_rx_dup++;
603 IEEE80211_NODE_STAT(ni, rx_dup);
606 ni->ni_rxseqs[tid] = rxseq;
611 case IEEE80211_FC0_TYPE_DATA:
612 hdrspace = ieee80211_hdrspace(ic, wh);
613 if (m->m_len < hdrspace &&
614 (m = m_pullup(m, hdrspace)) == NULL) {
615 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
616 ni->ni_macaddr, NULL,
617 "data too short: expecting %u", hdrspace);
618 vap->iv_stats.is_rx_tooshort++;
622 * Handle A-MPDU re-ordering. If the frame is to be
623 * processed directly then ieee80211_ampdu_reorder
624 * will return 0; otherwise it has consumed the mbuf
625 * and we should do nothing more with it.
627 if ((m->m_flags & M_AMPDU) &&
628 (dir == IEEE80211_FC1_DIR_FROMDS ||
629 dir == IEEE80211_FC1_DIR_DSTODS) &&
630 ieee80211_ampdu_reorder(ni, m) != 0) {
635 if (dir == IEEE80211_FC1_DIR_FROMDS) {
636 if ((ifp->if_flags & IFF_SIMPLEX) &&
637 isfromds_mcastecho(vap, wh)) {
639 * In IEEE802.11 network, multicast
640 * packets sent from "me" are broadcast
641 * from the AP; silently discard for
644 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
645 wh, "data", "%s", "multicast echo");
646 vap->iv_stats.is_rx_mcastecho++;
649 if ((vap->iv_flags & IEEE80211_F_DWDS) &&
650 IEEE80211_IS_MULTICAST(wh->i_addr1)) {
652 * DWDS sta's must drop 3-address mcast frames
653 * as they will be sent separately as a 4-addr
654 * frame. Accepting the 3-addr frame will
655 * confuse the bridge into thinking the sending
656 * sta is located at the end of WDS link.
658 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
659 "3-address data", "%s", "DWDS enabled");
660 vap->iv_stats.is_rx_mcastecho++;
663 } else if (dir == IEEE80211_FC1_DIR_DSTODS) {
664 if ((vap->iv_flags & IEEE80211_F_DWDS) == 0) {
665 IEEE80211_DISCARD(vap,
666 IEEE80211_MSG_INPUT, wh, "4-address data",
667 "%s", "DWDS not enabled");
668 vap->iv_stats.is_rx_wrongdir++;
671 if ((ifp->if_flags & IFF_SIMPLEX) &&
672 isdstods_mcastecho(vap, wh)) {
674 * In IEEE802.11 network, multicast
675 * packets sent from "me" are broadcast
676 * from the AP; silently discard for
679 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
680 "4-address data", "%s", "multicast echo");
681 vap->iv_stats.is_rx_mcastecho++;
685 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
686 "data", "incorrect dir 0x%x", dir);
687 vap->iv_stats.is_rx_wrongdir++;
692 * Handle privacy requirements. Note that we
693 * must not be preempted from here until after
694 * we (potentially) call ieee80211_crypto_demic;
695 * otherwise we may violate assumptions in the
696 * crypto cipher modules used to do delayed update
697 * of replay sequence numbers.
699 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
700 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
702 * Discard encrypted frames when privacy is off.
704 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
705 wh, "WEP", "%s", "PRIVACY off");
706 vap->iv_stats.is_rx_noprivacy++;
707 IEEE80211_NODE_STAT(ni, rx_noprivacy);
710 key = ieee80211_crypto_decap(ni, m, hdrspace);
712 /* NB: stats+msgs handled in crypto_decap */
713 IEEE80211_NODE_STAT(ni, rx_wepfail);
716 wh = mtod(m, struct ieee80211_frame *);
717 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
719 /* XXX M_WEP and IEEE80211_F_PRIVACY */
724 * Save QoS bits for use below--before we strip the header.
726 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
727 qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
728 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
729 ((struct ieee80211_qosframe *)wh)->i_qos[0];
734 * Next up, any fragmentation.
736 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
737 m = ieee80211_defrag(ni, m, hdrspace);
739 /* Fragment dropped or frame not complete yet */
743 wh = NULL; /* no longer valid, catch any uses */
746 * Next strip any MSDU crypto bits.
748 if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
749 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
750 ni->ni_macaddr, "data", "%s", "demic error");
751 vap->iv_stats.is_rx_demicfail++;
752 IEEE80211_NODE_STAT(ni, rx_demicfail);
756 /* copy to listener after decrypt */
757 if (ieee80211_radiotap_active_vap(vap))
758 ieee80211_radiotap_rx(vap, m);
762 * Finally, strip the 802.11 header.
764 m = ieee80211_decap(vap, m, hdrspace);
766 /* XXX mask bit to check for both */
767 /* don't count Null data frames as errors */
768 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
769 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
771 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
772 ni->ni_macaddr, "data", "%s", "decap error");
773 vap->iv_stats.is_rx_decap++;
774 IEEE80211_NODE_STAT(ni, rx_decap);
777 eh = mtod(m, struct ether_header *);
778 if (!ieee80211_node_is_authorized(ni)) {
780 * Deny any non-PAE frames received prior to
781 * authorization. For open/shared-key
782 * authentication the port is mark authorized
783 * after authentication completes. For 802.1x
784 * the port is not marked authorized by the
785 * authenticator until the handshake has completed.
787 if (eh->ether_type != htons(ETHERTYPE_PAE)) {
788 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
789 eh->ether_shost, "data",
790 "unauthorized port: ether type 0x%x len %u",
791 eh->ether_type, m->m_pkthdr.len);
792 vap->iv_stats.is_rx_unauth++;
793 IEEE80211_NODE_STAT(ni, rx_unauth);
798 * When denying unencrypted frames, discard
799 * any non-PAE frames received without encryption.
801 if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
802 (key == NULL && (m->m_flags & M_WEP) == 0) &&
803 eh->ether_type != htons(ETHERTYPE_PAE)) {
805 * Drop unencrypted frames.
807 vap->iv_stats.is_rx_unencrypted++;
808 IEEE80211_NODE_STAT(ni, rx_unencrypted);
812 /* XXX require HT? */
813 if (qos & IEEE80211_QOS_AMSDU) {
814 m = ieee80211_decap_amsdu(ni, m);
816 return IEEE80211_FC0_TYPE_DATA;
818 #ifdef IEEE80211_SUPPORT_SUPERG
819 m = ieee80211_decap_fastframe(vap, ni, m);
821 return IEEE80211_FC0_TYPE_DATA;
824 ieee80211_deliver_data(vap, ni, m);
825 return IEEE80211_FC0_TYPE_DATA;
827 case IEEE80211_FC0_TYPE_MGT:
828 vap->iv_stats.is_rx_mgmt++;
829 IEEE80211_NODE_STAT(ni, rx_mgmt);
830 if (dir != IEEE80211_FC1_DIR_NODS) {
831 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
832 wh, "data", "incorrect dir 0x%x", dir);
833 vap->iv_stats.is_rx_wrongdir++;
836 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
837 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
838 ni->ni_macaddr, "mgt", "too short: len %u",
840 vap->iv_stats.is_rx_tooshort++;
843 #ifdef IEEE80211_DEBUG
844 if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
845 ieee80211_msg_dumppkts(vap)) {
846 if_printf(ifp, "received %s from %6D rssi %d\n",
847 ieee80211_mgt_subtype_name[subtype >>
848 IEEE80211_FC0_SUBTYPE_SHIFT],
849 wh->i_addr2, ":", rssi);
852 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
853 if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
855 * Only shared key auth frames with a challenge
856 * should be encrypted, discard all others.
858 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
859 wh, ieee80211_mgt_subtype_name[subtype >>
860 IEEE80211_FC0_SUBTYPE_SHIFT],
861 "%s", "WEP set but not permitted");
862 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
865 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
867 * Discard encrypted frames when privacy is off.
869 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
870 wh, "mgt", "%s", "WEP set but PRIVACY off");
871 vap->iv_stats.is_rx_noprivacy++;
874 hdrspace = ieee80211_hdrspace(ic, wh);
875 key = ieee80211_crypto_decap(ni, m, hdrspace);
877 /* NB: stats+msgs handled in crypto_decap */
880 wh = mtod(m, struct ieee80211_frame *);
881 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
883 vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
886 case IEEE80211_FC0_TYPE_CTL:
887 vap->iv_stats.is_rx_ctl++;
888 IEEE80211_NODE_STAT(ni, rx_ctrl);
889 vap->iv_recv_ctl(ni, m, subtype);
893 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
894 wh, NULL, "bad frame type 0x%x", type);
895 /* should not come here */
902 if (need_tap && ieee80211_radiotap_active_vap(vap))
903 ieee80211_radiotap_rx(vap, m);
911 sta_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
912 int rssi, int nf, uint16_t seq, uint16_t status)
914 struct ieee80211vap *vap = ni->ni_vap;
916 if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
917 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
918 ni->ni_macaddr, "open auth",
919 "bad sta auth mode %u", ni->ni_authmode);
920 vap->iv_stats.is_rx_bad_auth++; /* XXX */
923 if (vap->iv_state != IEEE80211_S_AUTH ||
924 seq != IEEE80211_AUTH_OPEN_RESPONSE) {
925 vap->iv_stats.is_rx_bad_auth++;
929 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
930 ni, "open auth failed (reason %d)", status);
931 vap->iv_stats.is_rx_auth_fail++;
932 vap->iv_stats.is_rx_authfail_code = status;
933 ieee80211_new_state(vap, IEEE80211_S_SCAN,
934 IEEE80211_SCAN_FAIL_STATUS);
936 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
940 sta_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
941 uint8_t *frm, uint8_t *efrm, int rssi, int nf,
942 uint16_t seq, uint16_t status)
944 struct ieee80211vap *vap = ni->ni_vap;
949 * NB: this can happen as we allow pre-shared key
950 * authentication to be enabled w/o wep being turned
951 * on so that configuration of these can be done
952 * in any order. It may be better to enforce the
953 * ordering in which case this check would just be
954 * for sanity/consistency.
956 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
957 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
958 ni->ni_macaddr, "shared key auth",
959 "%s", " PRIVACY is disabled");
960 estatus = IEEE80211_STATUS_ALG;
964 * Pre-shared key authentication is evil; accept
965 * it only if explicitly configured (it is supported
966 * mainly for compatibility with clients like OS X).
968 if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
969 ni->ni_authmode != IEEE80211_AUTH_SHARED) {
970 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
971 ni->ni_macaddr, "shared key auth",
972 "bad sta auth mode %u", ni->ni_authmode);
973 vap->iv_stats.is_rx_bad_auth++; /* XXX maybe a unique error? */
974 estatus = IEEE80211_STATUS_ALG;
979 if (frm + 1 < efrm) {
980 if ((frm[1] + 2) > (efrm - frm)) {
981 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
982 ni->ni_macaddr, "shared key auth",
984 frm[0], (int)((frm[1] + 2) - (efrm - frm)));
985 vap->iv_stats.is_rx_bad_auth++;
986 estatus = IEEE80211_STATUS_CHALLENGE;
989 if (*frm == IEEE80211_ELEMID_CHALLENGE)
994 case IEEE80211_AUTH_SHARED_CHALLENGE:
995 case IEEE80211_AUTH_SHARED_RESPONSE:
996 if (challenge == NULL) {
997 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
998 ni->ni_macaddr, "shared key auth",
999 "%s", "no challenge");
1000 vap->iv_stats.is_rx_bad_auth++;
1001 estatus = IEEE80211_STATUS_CHALLENGE;
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++;
1009 estatus = IEEE80211_STATUS_CHALLENGE;
1015 if (vap->iv_state != IEEE80211_S_AUTH)
1018 case IEEE80211_AUTH_SHARED_PASS:
1019 if (ni->ni_challenge != NULL) {
1020 kfree(ni->ni_challenge, M_80211_NODE);
1021 ni->ni_challenge = NULL;
1024 IEEE80211_NOTE_FRAME(vap,
1025 IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, wh,
1026 "shared key auth failed (reason %d)", status);
1027 vap->iv_stats.is_rx_auth_fail++;
1028 vap->iv_stats.is_rx_authfail_code = status;
1031 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1033 case IEEE80211_AUTH_SHARED_CHALLENGE:
1034 if (!ieee80211_alloc_challenge(ni))
1036 /* XXX could optimize by passing recvd challenge */
1037 memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
1038 IEEE80211_SEND_MGMT(ni,
1039 IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1042 IEEE80211_DISCARD(vap, IEEE80211_MSG_AUTH,
1043 wh, "shared key auth", "bad seq %d", seq);
1044 vap->iv_stats.is_rx_bad_auth++;
1050 * Kick the state machine. This short-circuits
1051 * using the mgt frame timeout to trigger the
1054 if (vap->iv_state == IEEE80211_S_AUTH)
1055 ieee80211_new_state(vap, IEEE80211_S_SCAN,
1056 IEEE80211_SCAN_FAIL_STATUS);
1060 ieee80211_parse_wmeparams(struct ieee80211vap *vap, uint8_t *frm,
1061 const struct ieee80211_frame *wh)
1063 #define MS(_v, _f) (((_v) & _f) >> _f##_S)
1064 struct ieee80211_wme_state *wme = &vap->iv_ic->ic_wme;
1065 u_int len = frm[1], qosinfo;
1068 if (len < sizeof(struct ieee80211_wme_param)-2) {
1069 IEEE80211_DISCARD_IE(vap,
1070 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
1071 wh, "WME", "too short, len %u", len);
1074 qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)];
1075 qosinfo &= WME_QOSINFO_COUNT;
1076 /* XXX do proper check for wraparound */
1077 if (qosinfo == wme->wme_wmeChanParams.cap_info)
1079 frm += __offsetof(struct ieee80211_wme_param, params_acParams);
1080 for (i = 0; i < WME_NUM_AC; i++) {
1081 struct wmeParams *wmep =
1082 &wme->wme_wmeChanParams.cap_wmeParams[i];
1083 /* NB: ACI not used */
1084 wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
1085 wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
1086 wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
1087 wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
1088 wmep->wmep_txopLimit = LE_READ_2(frm+2);
1091 wme->wme_wmeChanParams.cap_info = qosinfo;
1097 * Process 11h Channel Switch Announcement (CSA) ie. If this
1098 * is the first CSA then initiate the switch. Otherwise we
1099 * track state and trigger completion and/or cancel of the switch.
1100 * XXX should be public for IBSS use
1103 ieee80211_parse_csaparams(struct ieee80211vap *vap, uint8_t *frm,
1104 const struct ieee80211_frame *wh)
1106 struct ieee80211com *ic = vap->iv_ic;
1107 const struct ieee80211_csa_ie *csa =
1108 (const struct ieee80211_csa_ie *) frm;
1110 KASSERT(vap->iv_state >= IEEE80211_S_RUN,
1111 ("state %s", ieee80211_state_name[vap->iv_state]));
1113 if (csa->csa_mode > 1) {
1114 IEEE80211_DISCARD_IE(vap,
1115 IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1116 wh, "CSA", "invalid mode %u", csa->csa_mode);
1119 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
1121 * Convert the channel number to a channel reference. We
1122 * try first to preserve turbo attribute of the current
1123 * channel then fallback. Note this will not work if the
1124 * CSA specifies a channel that requires a band switch (e.g.
1125 * 11a => 11g). This is intentional as 11h is defined only
1126 * for 5GHz/11a and because the switch does not involve a
1127 * reassociation, protocol state (capabilities, negotated
1128 * rates, etc) may/will be wrong.
1130 struct ieee80211_channel *c =
1131 ieee80211_find_channel_byieee(ic, csa->csa_newchan,
1132 (ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALLTURBO));
1134 c = ieee80211_find_channel_byieee(ic,
1136 (ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALL));
1138 IEEE80211_DISCARD_IE(vap,
1139 IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1140 wh, "CSA", "invalid channel %u",
1145 #if IEEE80211_CSA_COUNT_MIN > 0
1146 if (csa->csa_count < IEEE80211_CSA_COUNT_MIN) {
1148 * Require at least IEEE80211_CSA_COUNT_MIN count to
1149 * reduce the risk of being redirected by a fabricated
1150 * CSA. If a valid CSA is dropped we'll still get a
1151 * beacon miss when the AP leaves the channel so we'll
1152 * eventually follow to the new channel.
1154 * NOTE: this violates the 11h spec that states that
1155 * count may be any value and if 0 then a switch
1156 * should happen asap.
1158 IEEE80211_DISCARD_IE(vap,
1159 IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1160 wh, "CSA", "count %u too small, must be >= %u",
1161 csa->csa_count, IEEE80211_CSA_COUNT_MIN);
1165 ieee80211_csa_startswitch(ic, c, csa->csa_mode, csa->csa_count);
1168 * Validate this ie against the initial CSA. We require
1169 * mode and channel not change and the count must be
1170 * monotonically decreasing. This may be pointless and
1171 * canceling the switch as a result may be too paranoid but
1172 * in the worst case if we drop out of CSA because of this
1173 * and the AP does move then we'll just end up taking a
1174 * beacon miss and scan to find the AP.
1176 * XXX may want <= on count as we also process ProbeResp
1177 * frames and those may come in w/ the same count as the
1178 * previous beacon; but doing so leaves us open to a stuck
1179 * count until we add a dead-man timer
1181 if (!(csa->csa_count < ic->ic_csa_count &&
1182 csa->csa_mode == ic->ic_csa_mode &&
1183 csa->csa_newchan == ieee80211_chan2ieee(ic, ic->ic_csa_newchan))) {
1184 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_DOTH, wh,
1185 "CSA ie mismatch, initial ie <%d,%d,%d>, "
1186 "this ie <%d,%d,%d>", ic->ic_csa_mode,
1187 ic->ic_csa_newchan->ic_ieee, ic->ic_csa_count,
1188 csa->csa_mode, csa->csa_newchan, csa->csa_count);
1189 ieee80211_csa_cancelswitch(ic);
1191 if (csa->csa_count <= 1)
1192 ieee80211_csa_completeswitch(ic);
1194 ic->ic_csa_count = csa->csa_count;
1202 * Return non-zero if a background scan may be continued:
1203 * o bg scan is active
1204 * o no channel switch is pending
1205 * o there has not been any traffic recently
1207 * Note we do not check if there is an administrative enable;
1208 * this is only done to start the scan. We assume that any
1209 * change in state will be accompanied by a request to cancel
1210 * active scans which will otherwise cause this test to fail.
1213 contbgscan(struct ieee80211vap *vap)
1215 struct ieee80211com *ic = vap->iv_ic;
1217 return ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) &&
1218 (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1219 vap->iv_state == IEEE80211_S_RUN && /* XXX? */
1220 time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1224 * Return non-zero if a backgrond scan may be started:
1225 * o bg scanning is administratively enabled
1226 * o no channel switch is pending
1227 * o we are not boosted on a dynamic turbo channel
1228 * o there has not been a scan recently
1229 * o there has not been any traffic recently
1232 startbgscan(struct ieee80211vap *vap)
1234 struct ieee80211com *ic = vap->iv_ic;
1236 return ((vap->iv_flags & IEEE80211_F_BGSCAN) &&
1237 (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1238 #ifdef IEEE80211_SUPPORT_SUPERG
1239 !IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1241 time_after(ticks, ic->ic_lastscan + vap->iv_bgscanintvl) &&
1242 time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1246 sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1247 int subtype, int rssi, int nf)
1249 #define ISPROBE(_st) ((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1250 #define ISREASSOC(_st) ((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
1251 struct ieee80211vap *vap = ni->ni_vap;
1252 struct ieee80211com *ic = ni->ni_ic;
1253 struct ieee80211_frame *wh;
1254 uint8_t *frm, *efrm;
1255 uint8_t *rates, *xrates, *wme, *htcap, *htinfo;
1258 wh = mtod(m0, struct ieee80211_frame *);
1259 frm = (uint8_t *)&wh[1];
1260 efrm = mtod(m0, uint8_t *) + m0->m_len;
1262 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1263 case IEEE80211_FC0_SUBTYPE_BEACON: {
1264 struct ieee80211_scanparams scan;
1266 * We process beacon/probe response frames:
1267 * o when scanning, or
1268 * o station mode when associated (to collect state
1269 * updates such as 802.11g slot time)
1270 * Frames otherwise received are discarded.
1272 if (!((ic->ic_flags & IEEE80211_F_SCAN) || ni->ni_associd)) {
1273 vap->iv_stats.is_rx_mgtdiscard++;
1276 /* XXX probe response in sta mode when !scanning? */
1277 if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
1280 * Count frame now that we know it's to be processed.
1282 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1283 vap->iv_stats.is_rx_beacon++; /* XXX remove */
1284 IEEE80211_NODE_STAT(ni, rx_beacons);
1286 IEEE80211_NODE_STAT(ni, rx_proberesp);
1288 * When operating in station mode, check for state updates.
1289 * Be careful to ignore beacons received while doing a
1290 * background scan. We consider only 11g/WMM stuff right now.
1292 if (ni->ni_associd != 0 &&
1293 ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
1294 IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
1295 /* record tsf of last beacon */
1296 memcpy(ni->ni_tstamp.data, scan.tstamp,
1297 sizeof(ni->ni_tstamp));
1298 /* count beacon frame for s/w bmiss handling */
1299 vap->iv_swbmiss_count++;
1300 vap->iv_bmiss_count = 0;
1301 if (ni->ni_erp != scan.erp) {
1302 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1304 "erp change: was 0x%x, now 0x%x",
1305 ni->ni_erp, scan.erp);
1306 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1307 (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1308 ic->ic_flags |= IEEE80211_F_USEPROT;
1310 ic->ic_flags &= ~IEEE80211_F_USEPROT;
1311 ni->ni_erp = scan.erp;
1313 /* XXX driver notification */
1315 if ((ni->ni_capinfo ^ scan.capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) {
1316 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1318 "capabilities change: was 0x%x, now 0x%x",
1319 ni->ni_capinfo, scan.capinfo);
1321 * NB: we assume short preamble doesn't
1322 * change dynamically
1324 ieee80211_set_shortslottime(ic,
1325 IEEE80211_IS_CHAN_A(ic->ic_bsschan) ||
1326 (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1327 ni->ni_capinfo = (ni->ni_capinfo &~ IEEE80211_CAPINFO_SHORT_SLOTTIME)
1328 | (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME);
1331 if (scan.wme != NULL &&
1332 (ni->ni_flags & IEEE80211_NODE_QOS) &&
1333 ieee80211_parse_wmeparams(vap, scan.wme, wh) > 0)
1334 ieee80211_wme_updateparams(vap);
1335 #ifdef IEEE80211_SUPPORT_SUPERG
1336 if (scan.ath != NULL)
1337 ieee80211_parse_athparams(ni, scan.ath, wh);
1339 if (scan.htcap != NULL && scan.htinfo != NULL &&
1340 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1341 ieee80211_ht_updateparams(ni,
1342 scan.htcap, scan.htinfo);
1343 /* XXX state changes? */
1345 if (scan.tim != NULL) {
1346 struct ieee80211_tim_ie *tim =
1347 (struct ieee80211_tim_ie *) scan.tim;
1349 int aid = IEEE80211_AID(ni->ni_associd);
1350 int ix = aid / NBBY;
1351 int min = tim->tim_bitctl &~ 1;
1352 int max = tim->tim_len + min - 4;
1353 if ((tim->tim_bitctl&1) ||
1354 (min <= ix && ix <= max &&
1355 isset(tim->tim_bitmap - min, aid))) {
1357 * XXX Do not let bg scan kick off
1358 * we are expecting data.
1360 ic->ic_lastdata = ticks;
1361 ieee80211_sta_pwrsave(vap, 0);
1364 ni->ni_dtim_count = tim->tim_count;
1365 ni->ni_dtim_period = tim->tim_period;
1367 if (scan.csa != NULL &&
1368 (vap->iv_flags & IEEE80211_F_DOTH))
1369 ieee80211_parse_csaparams(vap, scan.csa, wh);
1370 else if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
1372 * No CSA ie or 11h disabled, but a channel
1373 * switch is pending; drop out so we aren't
1374 * stuck in CSA state. If the AP really is
1375 * moving we'll get a beacon miss and scan.
1377 ieee80211_csa_cancelswitch(ic);
1380 * If scanning, pass the info to the scan module.
1381 * Otherwise, check if it's the right time to do
1382 * a background scan. Background scanning must
1383 * be enabled and we must not be operating in the
1384 * turbo phase of dynamic turbo mode. Then,
1385 * it's been a while since the last background
1386 * scan and if no data frames have come through
1387 * recently, kick off a scan. Note that this
1388 * is the mechanism by which a background scan
1389 * is started _and_ continued each time we
1390 * return on-channel to receive a beacon from
1393 if (ic->ic_flags & IEEE80211_F_SCAN) {
1394 ieee80211_add_scan(vap, &scan, wh,
1396 } else if (contbgscan(vap)) {
1397 ieee80211_bg_scan(vap, 0);
1398 } else if (startbgscan(vap)) {
1399 vap->iv_stats.is_scan_bg++;
1401 /* wakeup if we are sleeing */
1402 ieee80211_set_pwrsave(vap, 0);
1404 ieee80211_bg_scan(vap, 0);
1409 * If scanning, just pass information to the scan module.
1411 if (ic->ic_flags & IEEE80211_F_SCAN) {
1412 if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1414 * Actively scanning a channel marked passive;
1415 * send a probe request now that we know there
1416 * is 802.11 traffic present.
1418 * XXX check if the beacon we recv'd gives
1419 * us what we need and suppress the probe req
1421 ieee80211_probe_curchan(vap, 1);
1422 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1424 ieee80211_add_scan(vap, &scan, wh, subtype, rssi, nf);
1430 case IEEE80211_FC0_SUBTYPE_AUTH: {
1431 uint16_t algo, seq, status;
1439 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1440 algo = le16toh(*(uint16_t *)frm);
1441 seq = le16toh(*(uint16_t *)(frm + 2));
1442 status = le16toh(*(uint16_t *)(frm + 4));
1443 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1444 "recv auth frame with algorithm %d seq %d", algo, seq);
1446 if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1447 IEEE80211_DISCARD(vap,
1448 IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1449 wh, "auth", "%s", "TKIP countermeasures enabled");
1450 vap->iv_stats.is_rx_auth_countermeasures++;
1451 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1452 ieee80211_send_error(ni, wh->i_addr2,
1453 IEEE80211_FC0_SUBTYPE_AUTH,
1454 IEEE80211_REASON_MIC_FAILURE);
1458 if (algo == IEEE80211_AUTH_ALG_SHARED)
1459 sta_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1461 else if (algo == IEEE80211_AUTH_ALG_OPEN)
1462 sta_auth_open(ni, wh, rssi, nf, seq, status);
1464 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1465 wh, "auth", "unsupported alg %d", algo);
1466 vap->iv_stats.is_rx_auth_unsupported++;
1472 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1473 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
1474 uint16_t capinfo, associd;
1477 if (vap->iv_state != IEEE80211_S_ASSOC) {
1478 vap->iv_stats.is_rx_mgtdiscard++;
1483 * asresp frame format
1484 * [2] capability information
1486 * [2] association ID
1487 * [tlv] supported rates
1488 * [tlv] extended supported rates
1490 * [tlv] HT capabilities
1493 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1495 capinfo = le16toh(*(uint16_t *)frm);
1497 status = le16toh(*(uint16_t *)frm);
1500 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1501 wh->i_addr2, "%sassoc failed (reason %d)",
1502 ISREASSOC(subtype) ? "re" : "", status);
1503 vap->iv_stats.is_rx_auth_fail++; /* XXX */
1506 associd = le16toh(*(uint16_t *)frm);
1509 rates = xrates = wme = htcap = htinfo = NULL;
1510 while (efrm - frm > 1) {
1511 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1513 case IEEE80211_ELEMID_RATES:
1516 case IEEE80211_ELEMID_XRATES:
1519 case IEEE80211_ELEMID_HTCAP:
1522 case IEEE80211_ELEMID_HTINFO:
1525 case IEEE80211_ELEMID_VENDOR:
1528 else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1530 * Accept pre-draft HT ie's if the
1531 * standard ones have not been seen.
1533 if (ishtcapoui(frm)) {
1536 } else if (ishtinfooui(frm)) {
1541 /* XXX Atheros OUI support */
1547 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1549 IEEE80211_VERIFY_ELEMENT(xrates,
1550 IEEE80211_RATE_MAXSIZE - rates[1], return);
1551 rate = ieee80211_setup_rates(ni, rates, xrates,
1553 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1554 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1555 if (rate & IEEE80211_RATE_BASIC) {
1556 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1558 "%sassoc failed (rate set mismatch)",
1559 ISREASSOC(subtype) ? "re" : "");
1560 vap->iv_stats.is_rx_assoc_norate++;
1561 ieee80211_new_state(vap, IEEE80211_S_SCAN,
1562 IEEE80211_SCAN_FAIL_STATUS);
1566 ni->ni_capinfo = capinfo;
1567 ni->ni_associd = associd;
1568 if (ni->ni_jointime == 0)
1569 ni->ni_jointime = time_second;
1571 ieee80211_parse_wmeparams(vap, wme, wh) >= 0) {
1572 ni->ni_flags |= IEEE80211_NODE_QOS;
1573 ieee80211_wme_updateparams(vap);
1575 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1577 * Setup HT state according to the negotiation.
1579 * NB: shouldn't need to check if HT use is enabled but some
1580 * ap's send back HT ie's even when we don't indicate we
1581 * are HT capable in our AssocReq.
1583 if (htcap != NULL && htinfo != NULL &&
1584 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1585 ieee80211_ht_node_init(ni);
1586 ieee80211_ht_updateparams(ni, htcap, htinfo);
1587 ieee80211_setup_htrates(ni, htcap,
1588 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1589 ieee80211_setup_basic_htrates(ni, htinfo);
1590 ieee80211_node_setuptxparms(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)