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 #ifdef IEEE80211_SUPPORT_SUPERG
59 #include <netproto/802_11/ieee80211_superg.h>
62 #define IEEE80211_RATE2MBS(r) (((r) & IEEE80211_RATE_VAL) / 2)
64 static void sta_vattach(struct ieee80211vap *);
65 static void sta_beacon_miss(struct ieee80211vap *);
66 static int sta_newstate(struct ieee80211vap *, enum ieee80211_state, int);
67 static int sta_input(struct ieee80211_node *, struct mbuf *, int, int);
68 static void sta_recv_mgmt(struct ieee80211_node *, struct mbuf *,
69 int subtype, int rssi, int nf);
70 static void sta_recv_ctl(struct ieee80211_node *, struct mbuf *, int subtype);
73 ieee80211_sta_attach(struct ieee80211com *ic)
75 ic->ic_vattach[IEEE80211_M_STA] = sta_vattach;
79 ieee80211_sta_detach(struct ieee80211com *ic)
84 sta_vdetach(struct ieee80211vap *vap)
89 sta_vattach(struct ieee80211vap *vap)
91 vap->iv_newstate = sta_newstate;
92 vap->iv_input = sta_input;
93 vap->iv_recv_mgmt = sta_recv_mgmt;
94 vap->iv_recv_ctl = sta_recv_ctl;
95 vap->iv_opdetach = sta_vdetach;
96 vap->iv_bmiss = sta_beacon_miss;
100 * Handle a beacon miss event. The common code filters out
101 * spurious events that can happen when scanning and/or before
102 * reaching RUN state.
105 sta_beacon_miss(struct ieee80211vap *vap)
107 struct ieee80211com *ic = vap->iv_ic;
109 KASSERT((ic->ic_flags & IEEE80211_F_SCAN) == 0, ("scanning"));
110 KASSERT(vap->iv_state >= IEEE80211_S_RUN,
111 ("wrong state %s", ieee80211_state_name[vap->iv_state]));
113 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
114 "beacon miss, mode %s state %s\n",
115 ieee80211_opmode_name[vap->iv_opmode],
116 ieee80211_state_name[vap->iv_state]);
118 if (vap->iv_state == IEEE80211_S_CSA) {
120 * A Channel Switch is pending; assume we missed the
121 * beacon that would've completed the process and just
122 * force the switch. If we made a mistake we'll not
123 * find the AP on the new channel and fall back to a
126 ieee80211_csa_completeswitch(ic);
129 if (++vap->iv_bmiss_count < vap->iv_bmiss_max) {
131 * Send a directed probe req before falling back to a
132 * scan; if we receive a response ic_bmiss_count will
133 * be reset. Some cards mistakenly report beacon miss
134 * so this avoids the expensive scan if the ap is
137 ieee80211_send_probereq(vap->iv_bss, vap->iv_myaddr,
138 vap->iv_bss->ni_bssid, vap->iv_bss->ni_bssid,
139 vap->iv_bss->ni_essid, vap->iv_bss->ni_esslen);
142 vap->iv_bmiss_count = 0;
143 vap->iv_stats.is_beacon_miss++;
144 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
145 #ifdef IEEE80211_SUPPORT_SUPERG
146 struct ieee80211com *ic = vap->iv_ic;
149 * If we receive a beacon miss interrupt when using
150 * dynamic turbo, attempt to switch modes before
153 if (IEEE80211_ATH_CAP(vap, vap->iv_bss, IEEE80211_NODE_TURBOP))
154 ieee80211_dturbo_switch(vap,
155 ic->ic_bsschan->ic_flags ^ IEEE80211_CHAN_TURBO);
158 * Try to reassociate before scanning for a new ap.
160 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
163 * Somebody else is controlling state changes (e.g.
164 * a user-mode app) don't do anything that would
165 * confuse them; just drop into scan mode so they'll
166 * notified of the state change and given control.
168 ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
173 * Handle deauth with reason. We retry only for
174 * the cases where we might succeed. Otherwise
175 * we downgrade the ap and scan.
178 sta_authretry(struct ieee80211vap *vap, struct ieee80211_node *ni, int reason)
181 case IEEE80211_STATUS_SUCCESS: /* NB: MLME assoc */
182 case IEEE80211_STATUS_TIMEOUT:
183 case IEEE80211_REASON_ASSOC_EXPIRE:
184 case IEEE80211_REASON_NOT_AUTHED:
185 case IEEE80211_REASON_NOT_ASSOCED:
186 case IEEE80211_REASON_ASSOC_LEAVE:
187 case IEEE80211_REASON_ASSOC_NOT_AUTHED:
188 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, 1);
191 ieee80211_scan_assoc_fail(vap, vap->iv_bss->ni_macaddr, reason);
192 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
193 ieee80211_check_scan_current(vap);
199 * IEEE80211_M_STA vap state machine handler.
200 * This routine handles the main states in the 802.11 protocol.
203 sta_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
205 struct ieee80211com *ic = vap->iv_ic;
206 struct ieee80211_node *ni;
207 enum ieee80211_state ostate;
208 #ifdef IEEE80211_DEBUG
209 char ethstr[ETHER_ADDRSTRLEN + 1];
211 ostate = vap->iv_state;
212 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
213 __func__, ieee80211_state_name[ostate],
214 ieee80211_state_name[nstate], arg);
215 vap->iv_state = nstate; /* state transition */
216 callout_stop(&vap->iv_mgtsend); /* XXX callout_drain */
217 if (ostate != IEEE80211_S_SCAN)
218 ieee80211_cancel_scan(vap); /* background scan */
219 ni = vap->iv_bss; /* NB: no reference held */
220 if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)
221 callout_stop(&vap->iv_swbmiss);
223 case IEEE80211_S_INIT:
225 case IEEE80211_S_SLEEP:
227 case IEEE80211_S_RUN:
228 IEEE80211_SEND_MGMT(ni,
229 IEEE80211_FC0_SUBTYPE_DISASSOC,
230 IEEE80211_REASON_ASSOC_LEAVE);
231 ieee80211_sta_leave(ni);
233 case IEEE80211_S_ASSOC:
234 IEEE80211_SEND_MGMT(ni,
235 IEEE80211_FC0_SUBTYPE_DEAUTH,
236 IEEE80211_REASON_AUTH_LEAVE);
238 case IEEE80211_S_SCAN:
239 ieee80211_cancel_scan(vap);
244 if (ostate != IEEE80211_S_INIT) {
245 /* NB: optimize INIT -> INIT case */
246 ieee80211_reset_bss(vap);
248 if (vap->iv_auth->ia_detach != NULL)
249 vap->iv_auth->ia_detach(vap);
251 case IEEE80211_S_SCAN:
253 case IEEE80211_S_INIT:
255 * Initiate a scan. We can come here as a result
256 * of an IEEE80211_IOC_SCAN_REQ too in which case
257 * the vap will be marked with IEEE80211_FEXT_SCANREQ
258 * and the scan request parameters will be present
259 * in iv_scanreq. Otherwise we do the default.
261 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
262 ieee80211_check_scan(vap,
263 vap->iv_scanreq_flags,
264 vap->iv_scanreq_duration,
265 vap->iv_scanreq_mindwell,
266 vap->iv_scanreq_maxdwell,
267 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
268 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
270 ieee80211_check_scan_current(vap);
272 case IEEE80211_S_SCAN:
273 case IEEE80211_S_AUTH:
274 case IEEE80211_S_ASSOC:
276 * These can happen either because of a timeout
277 * on an assoc/auth response or because of a
278 * change in state that requires a reset. For
279 * the former we're called with a non-zero arg
280 * that is the cause for the failure; pass this
281 * to the scan code so it can update state.
282 * Otherwise trigger a new scan unless we're in
283 * manual roaming mode in which case an application
284 * must issue an explicit scan request.
287 ieee80211_scan_assoc_fail(vap,
288 vap->iv_bss->ni_macaddr, arg);
289 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
290 ieee80211_check_scan_current(vap);
292 case IEEE80211_S_RUN: /* beacon miss */
294 * Beacon miss. Notify user space and if not
295 * under control of a user application (roaming
296 * manual) kick off a scan to re-connect.
298 ieee80211_sta_leave(ni);
299 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
300 ieee80211_check_scan_current(vap);
306 case IEEE80211_S_AUTH:
308 case IEEE80211_S_INIT:
309 case IEEE80211_S_SCAN:
310 IEEE80211_SEND_MGMT(ni,
311 IEEE80211_FC0_SUBTYPE_AUTH, 1);
313 case IEEE80211_S_AUTH:
314 case IEEE80211_S_ASSOC:
315 switch (arg & 0xff) {
316 case IEEE80211_FC0_SUBTYPE_AUTH:
318 IEEE80211_SEND_MGMT(ni,
319 IEEE80211_FC0_SUBTYPE_AUTH, 2);
321 case IEEE80211_FC0_SUBTYPE_DEAUTH:
322 sta_authretry(vap, ni, arg>>8);
326 case IEEE80211_S_RUN:
327 switch (arg & 0xff) {
328 case IEEE80211_FC0_SUBTYPE_AUTH:
329 IEEE80211_SEND_MGMT(ni,
330 IEEE80211_FC0_SUBTYPE_AUTH, 2);
331 vap->iv_state = ostate; /* stay RUN */
333 case IEEE80211_FC0_SUBTYPE_DEAUTH:
334 ieee80211_sta_leave(ni);
335 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
337 IEEE80211_SEND_MGMT(ni,
338 IEEE80211_FC0_SUBTYPE_AUTH, 1);
347 case IEEE80211_S_ASSOC:
349 case IEEE80211_S_AUTH:
350 case IEEE80211_S_ASSOC:
351 IEEE80211_SEND_MGMT(ni,
352 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
354 case IEEE80211_S_SLEEP: /* cannot happen */
355 case IEEE80211_S_RUN:
356 ieee80211_sta_leave(ni);
357 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
358 IEEE80211_SEND_MGMT(ni, arg ?
359 IEEE80211_FC0_SUBTYPE_REASSOC_REQ :
360 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
367 case IEEE80211_S_RUN:
368 if (vap->iv_flags & IEEE80211_F_WPA) {
369 /* XXX validate prerequisites */
372 case IEEE80211_S_RUN:
373 case IEEE80211_S_CSA:
375 case IEEE80211_S_AUTH: /* when join is done in fw */
376 case IEEE80211_S_ASSOC:
377 #ifdef IEEE80211_DEBUG
378 if (ieee80211_msg_debug(vap)) {
379 ieee80211_note(vap, "%s with %s ssid ",
380 (vap->iv_opmode == IEEE80211_M_STA ?
381 "associated" : "synchronized"),
382 kether_ntoa(ni->ni_bssid, ethstr));
383 ieee80211_print_essid(vap->iv_bss->ni_essid,
386 kprintf(" channel %d start %uMb\n",
387 ieee80211_chan2ieee(ic, ic->ic_curchan),
388 IEEE80211_RATE2MBS(ni->ni_txrate));
391 ieee80211_scan_assoc_success(vap, ni->ni_macaddr);
392 ieee80211_notify_node_join(ni,
393 arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
395 case IEEE80211_S_SLEEP:
396 ieee80211_sta_pwrsave(vap, 0);
401 ieee80211_sync_curchan(ic);
402 if (ostate != IEEE80211_S_RUN &&
403 (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)) {
405 * Start s/w beacon miss timer for devices w/o
406 * hardware support. We fudge a bit here since
407 * we're doing this in software.
409 vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS(
410 2 * vap->iv_bmissthreshold * ni->ni_intval);
411 vap->iv_swbmiss_count = 0;
412 callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
413 ieee80211_swbmiss_callout, vap);
416 * When 802.1x is not in use mark the port authorized
417 * at this point so traffic can flow.
419 if (ni->ni_authmode != IEEE80211_AUTH_8021X)
420 ieee80211_node_authorize(ni);
422 * Fake association when joining an existing bss.
424 if (ic->ic_newassoc != NULL)
425 ic->ic_newassoc(vap->iv_bss, ostate != IEEE80211_S_RUN);
427 case IEEE80211_S_CSA:
428 if (ostate != IEEE80211_S_RUN)
431 case IEEE80211_S_SLEEP:
432 ieee80211_sta_pwrsave(vap, 1);
436 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
437 "%s: unexpected state transition %s -> %s\n", __func__,
438 ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
445 * Return non-zero if the frame is an echo of a multicast
446 * frame sent by ourself. The dir is known to be DSTODS.
449 isdstods_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
451 #define QWH4(wh) ((const struct ieee80211_qosframe_addr4 *)wh)
452 #define WH4(wh) ((const struct ieee80211_frame_addr4 *)wh)
455 KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
457 if (!IEEE80211_IS_MULTICAST(wh->i_addr3))
459 sa = IEEE80211_QOS_HAS_SEQ(wh) ? QWH4(wh)->i_addr4 : WH4(wh)->i_addr4;
460 return IEEE80211_ADDR_EQ(sa, vap->iv_myaddr);
466 * Return non-zero if the frame is an echo of a multicast
467 * frame sent by ourself. The dir is known to be FROMDS.
470 isfromds_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
472 KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
474 if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
476 return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
480 * Decide if a received management frame should be
481 * printed when debugging is enabled. This filters some
482 * of the less interesting frames that come frequently
486 doprint(struct ieee80211vap *vap, int subtype)
489 case IEEE80211_FC0_SUBTYPE_BEACON:
490 return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
491 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
498 * Process a received frame. The node associated with the sender
499 * should be supplied. If nothing was found in the node table then
500 * the caller is assumed to supply a reference to iv_bss instead.
501 * The RSSI and a timestamp are also supplied. The RSSI data is used
502 * during AP scanning to select a AP to associate with; it can have
503 * any units so long as values have consistent units and higher values
504 * mean ``better signal''. The receive timestamp is currently not used
505 * by the 802.11 layer.
508 sta_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
510 #define SEQ_LEQ(a,b) ((int)((a)-(b)) <= 0)
511 #define HAS_SEQ(type) ((type & 0x4) == 0)
512 struct ieee80211vap *vap = ni->ni_vap;
513 struct ieee80211com *ic = ni->ni_ic;
514 struct ifnet *ifp = vap->iv_ifp;
515 struct ieee80211_frame *wh;
516 struct ieee80211_key *key;
517 struct ether_header *eh;
518 int hdrspace, need_tap = 1; /* mbuf need to be tapped. */
519 uint8_t dir, type, subtype, qos;
522 #ifdef IEEE80211_DEBUG
523 char ethstr[ETHER_ADDRSTRLEN + 1];
526 if (m->m_flags & M_AMPDU_MPDU) {
528 * Fastpath for A-MPDU reorder q resubmission. Frames
529 * w/ M_AMPDU_MPDU marked have already passed through
530 * here but were received out of order and been held on
531 * the reorder queue. When resubmitted they are marked
532 * with the M_AMPDU_MPDU flag and we can bypass most of
533 * the normal processing.
535 wh = mtod(m, struct ieee80211_frame *);
536 type = IEEE80211_FC0_TYPE_DATA;
537 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
538 subtype = IEEE80211_FC0_SUBTYPE_QOS;
539 hdrspace = ieee80211_hdrspace(ic, wh); /* XXX optimize? */
543 KASSERT(ni != NULL, ("null node"));
544 ni->ni_inact = ni->ni_inact_reload;
546 type = -1; /* undefined */
548 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
549 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
550 ni->ni_macaddr, NULL,
551 "too short (1): len %u", m->m_pkthdr.len);
552 vap->iv_stats.is_rx_tooshort++;
556 * Bit of a cheat here, we use a pointer for a 3-address
557 * frame format but don't reference fields past outside
558 * ieee80211_frame_min w/o first validating the data is
561 wh = mtod(m, struct ieee80211_frame *);
563 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
564 IEEE80211_FC0_VERSION_0) {
565 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
566 ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
567 wh->i_fc[0], wh->i_fc[1]);
568 vap->iv_stats.is_rx_badversion++;
572 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
573 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
574 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
575 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
577 if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) {
578 /* not interested in */
579 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
580 bssid, NULL, "%s", "not to bss");
581 vap->iv_stats.is_rx_wrongbss++;
584 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
586 if (HAS_SEQ(type) && !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
587 uint8_t tid = ieee80211_gettid(wh);
588 if (IEEE80211_QOS_HAS_SEQ(wh) &&
589 TID_TO_WME_AC(tid) >= WME_AC_VI)
590 ic->ic_wme.wme_hipri_traffic++;
591 rxseq = le16toh(*(uint16_t *)wh->i_seq);
592 if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
593 (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
594 SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
595 /* duplicate, discard */
596 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
598 "seqno <%u,%u> fragno <%u,%u> tid %u",
599 rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
600 ni->ni_rxseqs[tid] >>
601 IEEE80211_SEQ_SEQ_SHIFT,
602 rxseq & IEEE80211_SEQ_FRAG_MASK,
604 IEEE80211_SEQ_FRAG_MASK,
606 vap->iv_stats.is_rx_dup++;
607 IEEE80211_NODE_STAT(ni, rx_dup);
610 ni->ni_rxseqs[tid] = rxseq;
615 case IEEE80211_FC0_TYPE_DATA:
616 hdrspace = ieee80211_hdrspace(ic, wh);
617 if (m->m_len < hdrspace &&
618 (m = m_pullup(m, hdrspace)) == NULL) {
619 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
620 ni->ni_macaddr, NULL,
621 "data too short: expecting %u", hdrspace);
622 vap->iv_stats.is_rx_tooshort++;
626 * Handle A-MPDU re-ordering. If the frame is to be
627 * processed directly then ieee80211_ampdu_reorder
628 * will return 0; otherwise it has consumed the mbuf
629 * and we should do nothing more with it.
631 if ((m->m_flags & M_AMPDU) &&
632 (dir == IEEE80211_FC1_DIR_FROMDS ||
633 dir == IEEE80211_FC1_DIR_DSTODS) &&
634 ieee80211_ampdu_reorder(ni, m) != 0) {
639 if (dir == IEEE80211_FC1_DIR_FROMDS) {
640 if ((ifp->if_flags & IFF_SIMPLEX) &&
641 isfromds_mcastecho(vap, wh)) {
643 * In IEEE802.11 network, multicast
644 * packets sent from "me" are broadcast
645 * from the AP; silently discard for
648 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
649 wh, "data", "%s", "multicast echo");
650 vap->iv_stats.is_rx_mcastecho++;
653 if ((vap->iv_flags & IEEE80211_F_DWDS) &&
654 IEEE80211_IS_MULTICAST(wh->i_addr1)) {
656 * DWDS sta's must drop 3-address mcast frames
657 * as they will be sent separately as a 4-addr
658 * frame. Accepting the 3-addr frame will
659 * confuse the bridge into thinking the sending
660 * sta is located at the end of WDS link.
662 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
663 "3-address data", "%s", "DWDS enabled");
664 vap->iv_stats.is_rx_mcastecho++;
667 } else if (dir == IEEE80211_FC1_DIR_DSTODS) {
668 if ((vap->iv_flags & IEEE80211_F_DWDS) == 0) {
669 IEEE80211_DISCARD(vap,
670 IEEE80211_MSG_INPUT, wh, "4-address data",
671 "%s", "DWDS not enabled");
672 vap->iv_stats.is_rx_wrongdir++;
675 if ((ifp->if_flags & IFF_SIMPLEX) &&
676 isdstods_mcastecho(vap, wh)) {
678 * In IEEE802.11 network, multicast
679 * packets sent from "me" are broadcast
680 * from the AP; silently discard for
683 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
684 "4-address data", "%s", "multicast echo");
685 vap->iv_stats.is_rx_mcastecho++;
689 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
690 "data", "incorrect dir 0x%x", dir);
691 vap->iv_stats.is_rx_wrongdir++;
696 * Handle privacy requirements. Note that we
697 * must not be preempted from here until after
698 * we (potentially) call ieee80211_crypto_demic;
699 * otherwise we may violate assumptions in the
700 * crypto cipher modules used to do delayed update
701 * of replay sequence numbers.
703 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
704 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
706 * Discard encrypted frames when privacy is off.
708 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
709 wh, "WEP", "%s", "PRIVACY off");
710 vap->iv_stats.is_rx_noprivacy++;
711 IEEE80211_NODE_STAT(ni, rx_noprivacy);
714 key = ieee80211_crypto_decap(ni, m, hdrspace);
716 /* NB: stats+msgs handled in crypto_decap */
717 IEEE80211_NODE_STAT(ni, rx_wepfail);
720 wh = mtod(m, struct ieee80211_frame *);
721 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
723 /* XXX M_WEP and IEEE80211_F_PRIVACY */
728 * Save QoS bits for use below--before we strip the header.
730 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
731 qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
732 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
733 ((struct ieee80211_qosframe *)wh)->i_qos[0];
738 * Next up, any fragmentation.
740 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
741 m = ieee80211_defrag(ni, m, hdrspace);
743 /* Fragment dropped or frame not complete yet */
747 wh = NULL; /* no longer valid, catch any uses */
750 * Next strip any MSDU crypto bits.
752 if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
753 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
754 ni->ni_macaddr, "data", "%s", "demic error");
755 vap->iv_stats.is_rx_demicfail++;
756 IEEE80211_NODE_STAT(ni, rx_demicfail);
760 /* copy to listener after decrypt */
761 if (ieee80211_radiotap_active_vap(vap))
762 ieee80211_radiotap_rx(vap, m);
766 * Finally, strip the 802.11 header.
768 m = ieee80211_decap(vap, m, hdrspace);
770 /* XXX mask bit to check for both */
771 /* don't count Null data frames as errors */
772 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
773 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
775 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
776 ni->ni_macaddr, "data", "%s", "decap error");
777 vap->iv_stats.is_rx_decap++;
778 IEEE80211_NODE_STAT(ni, rx_decap);
781 eh = mtod(m, struct ether_header *);
782 if (!ieee80211_node_is_authorized(ni)) {
784 * Deny any non-PAE frames received prior to
785 * authorization. For open/shared-key
786 * authentication the port is mark authorized
787 * after authentication completes. For 802.1x
788 * the port is not marked authorized by the
789 * authenticator until the handshake has completed.
791 if (eh->ether_type != htons(ETHERTYPE_PAE)) {
792 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
793 eh->ether_shost, "data",
794 "unauthorized port: ether type 0x%x len %u",
795 eh->ether_type, m->m_pkthdr.len);
796 vap->iv_stats.is_rx_unauth++;
797 IEEE80211_NODE_STAT(ni, rx_unauth);
802 * When denying unencrypted frames, discard
803 * any non-PAE frames received without encryption.
805 if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
806 (key == NULL && (m->m_flags & M_WEP) == 0) &&
807 eh->ether_type != htons(ETHERTYPE_PAE)) {
809 * Drop unencrypted frames.
811 vap->iv_stats.is_rx_unencrypted++;
812 IEEE80211_NODE_STAT(ni, rx_unencrypted);
816 /* XXX require HT? */
817 if (qos & IEEE80211_QOS_AMSDU) {
818 m = ieee80211_decap_amsdu(ni, m);
820 return IEEE80211_FC0_TYPE_DATA;
822 #ifdef IEEE80211_SUPPORT_SUPERG
823 m = ieee80211_decap_fastframe(vap, ni, m);
825 return IEEE80211_FC0_TYPE_DATA;
828 ieee80211_deliver_data(vap, ni, m);
829 return IEEE80211_FC0_TYPE_DATA;
831 case IEEE80211_FC0_TYPE_MGT:
832 vap->iv_stats.is_rx_mgmt++;
833 IEEE80211_NODE_STAT(ni, rx_mgmt);
834 if (dir != IEEE80211_FC1_DIR_NODS) {
835 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
836 wh, "data", "incorrect dir 0x%x", dir);
837 vap->iv_stats.is_rx_wrongdir++;
840 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
841 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
842 ni->ni_macaddr, "mgt", "too short: len %u",
844 vap->iv_stats.is_rx_tooshort++;
847 #ifdef IEEE80211_DEBUG
848 if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
849 ieee80211_msg_dumppkts(vap)) {
850 if_printf(ifp, "received %s from %s rssi %d\n",
851 ieee80211_mgt_subtype_name[subtype >>
852 IEEE80211_FC0_SUBTYPE_SHIFT],
853 kether_ntoa(wh->i_addr2, ethstr), rssi);
856 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
857 if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
859 * Only shared key auth frames with a challenge
860 * should be encrypted, discard all others.
862 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
863 wh, ieee80211_mgt_subtype_name[subtype >>
864 IEEE80211_FC0_SUBTYPE_SHIFT],
865 "%s", "WEP set but not permitted");
866 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
869 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
871 * Discard encrypted frames when privacy is off.
873 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
874 wh, "mgt", "%s", "WEP set but PRIVACY off");
875 vap->iv_stats.is_rx_noprivacy++;
878 hdrspace = ieee80211_hdrspace(ic, wh);
879 key = ieee80211_crypto_decap(ni, m, hdrspace);
881 /* NB: stats+msgs handled in crypto_decap */
884 wh = mtod(m, struct ieee80211_frame *);
885 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
887 vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
890 case IEEE80211_FC0_TYPE_CTL:
891 vap->iv_stats.is_rx_ctl++;
892 IEEE80211_NODE_STAT(ni, rx_ctrl);
893 vap->iv_recv_ctl(ni, m, subtype);
897 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
898 wh, NULL, "bad frame type 0x%x", type);
899 /* should not come here */
906 if (need_tap && ieee80211_radiotap_active_vap(vap))
907 ieee80211_radiotap_rx(vap, m);
915 sta_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
916 int rssi, int nf, uint16_t seq, uint16_t status)
918 struct ieee80211vap *vap = ni->ni_vap;
920 if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
921 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
922 ni->ni_macaddr, "open auth",
923 "bad sta auth mode %u", ni->ni_authmode);
924 vap->iv_stats.is_rx_bad_auth++; /* XXX */
927 if (vap->iv_state != IEEE80211_S_AUTH ||
928 seq != IEEE80211_AUTH_OPEN_RESPONSE) {
929 vap->iv_stats.is_rx_bad_auth++;
933 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
934 ni, "open auth failed (reason %d)", status);
935 vap->iv_stats.is_rx_auth_fail++;
936 vap->iv_stats.is_rx_authfail_code = status;
937 ieee80211_new_state(vap, IEEE80211_S_SCAN,
938 IEEE80211_SCAN_FAIL_STATUS);
940 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
944 sta_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
945 uint8_t *frm, uint8_t *efrm, int rssi, int nf,
946 uint16_t seq, uint16_t status)
948 struct ieee80211vap *vap = ni->ni_vap;
952 * NB: this can happen as we allow pre-shared key
953 * authentication to be enabled w/o wep being turned
954 * on so that configuration of these can be done
955 * in any order. It may be better to enforce the
956 * ordering in which case this check would just be
957 * for sanity/consistency.
959 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
960 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
961 ni->ni_macaddr, "shared key auth",
962 "%s", " PRIVACY is disabled");
966 * Pre-shared key authentication is evil; accept
967 * it only if explicitly configured (it is supported
968 * mainly for compatibility with clients like OS X).
970 if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
971 ni->ni_authmode != IEEE80211_AUTH_SHARED) {
972 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
973 ni->ni_macaddr, "shared key auth",
974 "bad sta auth mode %u", ni->ni_authmode);
975 vap->iv_stats.is_rx_bad_auth++; /* XXX maybe a unique error? */
980 if (frm + 1 < efrm) {
981 if ((frm[1] + 2) > (efrm - frm)) {
982 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
983 ni->ni_macaddr, "shared key auth",
985 frm[0], (int)((frm[1] + 2) - (efrm - frm)));
986 vap->iv_stats.is_rx_bad_auth++;
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++;
1003 if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1004 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1005 ni->ni_macaddr, "shared key auth",
1006 "bad challenge len %d", challenge[1]);
1007 vap->iv_stats.is_rx_bad_auth++;
1013 if (vap->iv_state != IEEE80211_S_AUTH)
1016 case IEEE80211_AUTH_SHARED_PASS:
1017 if (ni->ni_challenge != NULL) {
1018 kfree(ni->ni_challenge, M_80211_NODE);
1019 ni->ni_challenge = NULL;
1022 IEEE80211_NOTE_FRAME(vap,
1023 IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, wh,
1024 "shared key auth failed (reason %d)", status);
1025 vap->iv_stats.is_rx_auth_fail++;
1026 vap->iv_stats.is_rx_authfail_code = status;
1029 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1031 case IEEE80211_AUTH_SHARED_CHALLENGE:
1032 if (!ieee80211_alloc_challenge(ni))
1034 /* XXX could optimize by passing recvd challenge */
1035 memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
1036 IEEE80211_SEND_MGMT(ni,
1037 IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1040 IEEE80211_DISCARD(vap, IEEE80211_MSG_AUTH,
1041 wh, "shared key auth", "bad seq %d", seq);
1042 vap->iv_stats.is_rx_bad_auth++;
1048 * Kick the state machine. This short-circuits
1049 * using the mgt frame timeout to trigger the
1052 if (vap->iv_state == IEEE80211_S_AUTH)
1053 ieee80211_new_state(vap, IEEE80211_S_SCAN,
1054 IEEE80211_SCAN_FAIL_STATUS);
1058 ieee80211_parse_wmeparams(struct ieee80211vap *vap, uint8_t *frm,
1059 const struct ieee80211_frame *wh)
1061 #define MS(_v, _f) (((_v) & _f) >> _f##_S)
1062 struct ieee80211_wme_state *wme = &vap->iv_ic->ic_wme;
1063 u_int len = frm[1], qosinfo;
1066 if (len < sizeof(struct ieee80211_wme_param)-2) {
1067 IEEE80211_DISCARD_IE(vap,
1068 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
1069 wh, "WME", "too short, len %u", len);
1072 qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)];
1073 qosinfo &= WME_QOSINFO_COUNT;
1074 /* XXX do proper check for wraparound */
1075 if (qosinfo == wme->wme_wmeChanParams.cap_info)
1077 frm += __offsetof(struct ieee80211_wme_param, params_acParams);
1078 for (i = 0; i < WME_NUM_AC; i++) {
1079 struct wmeParams *wmep =
1080 &wme->wme_wmeChanParams.cap_wmeParams[i];
1081 /* NB: ACI not used */
1082 wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
1083 wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
1084 wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
1085 wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
1086 wmep->wmep_txopLimit = LE_READ_2(frm+2);
1089 wme->wme_wmeChanParams.cap_info = qosinfo;
1095 * Process 11h Channel Switch Announcement (CSA) ie. If this
1096 * is the first CSA then initiate the switch. Otherwise we
1097 * track state and trigger completion and/or cancel of the switch.
1098 * XXX should be public for IBSS use
1101 ieee80211_parse_csaparams(struct ieee80211vap *vap, uint8_t *frm,
1102 const struct ieee80211_frame *wh)
1104 struct ieee80211com *ic = vap->iv_ic;
1105 const struct ieee80211_csa_ie *csa =
1106 (const struct ieee80211_csa_ie *) frm;
1108 KASSERT(vap->iv_state >= IEEE80211_S_RUN,
1109 ("state %s", ieee80211_state_name[vap->iv_state]));
1111 if (csa->csa_mode > 1) {
1112 IEEE80211_DISCARD_IE(vap,
1113 IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1114 wh, "CSA", "invalid mode %u", csa->csa_mode);
1117 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
1119 * Convert the channel number to a channel reference. We
1120 * try first to preserve turbo attribute of the current
1121 * channel then fallback. Note this will not work if the
1122 * CSA specifies a channel that requires a band switch (e.g.
1123 * 11a => 11g). This is intentional as 11h is defined only
1124 * for 5GHz/11a and because the switch does not involve a
1125 * reassociation, protocol state (capabilities, negotated
1126 * rates, etc) may/will be wrong.
1128 struct ieee80211_channel *c =
1129 ieee80211_find_channel_byieee(ic, csa->csa_newchan,
1130 (ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALLTURBO));
1132 c = ieee80211_find_channel_byieee(ic,
1134 (ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALL));
1136 IEEE80211_DISCARD_IE(vap,
1137 IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1138 wh, "CSA", "invalid channel %u",
1143 #if IEEE80211_CSA_COUNT_MIN > 0
1144 if (csa->csa_count < IEEE80211_CSA_COUNT_MIN) {
1146 * Require at least IEEE80211_CSA_COUNT_MIN count to
1147 * reduce the risk of being redirected by a fabricated
1148 * CSA. If a valid CSA is dropped we'll still get a
1149 * beacon miss when the AP leaves the channel so we'll
1150 * eventually follow to the new channel.
1152 * NOTE: this violates the 11h spec that states that
1153 * count may be any value and if 0 then a switch
1154 * should happen asap.
1156 IEEE80211_DISCARD_IE(vap,
1157 IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1158 wh, "CSA", "count %u too small, must be >= %u",
1159 csa->csa_count, IEEE80211_CSA_COUNT_MIN);
1163 ieee80211_csa_startswitch(ic, c, csa->csa_mode, csa->csa_count);
1166 * Validate this ie against the initial CSA. We require
1167 * mode and channel not change and the count must be
1168 * monotonically decreasing. This may be pointless and
1169 * canceling the switch as a result may be too paranoid but
1170 * in the worst case if we drop out of CSA because of this
1171 * and the AP does move then we'll just end up taking a
1172 * beacon miss and scan to find the AP.
1174 * XXX may want <= on count as we also process ProbeResp
1175 * frames and those may come in w/ the same count as the
1176 * previous beacon; but doing so leaves us open to a stuck
1177 * count until we add a dead-man timer
1179 if (!(csa->csa_count < ic->ic_csa_count &&
1180 csa->csa_mode == ic->ic_csa_mode &&
1181 csa->csa_newchan == ieee80211_chan2ieee(ic, ic->ic_csa_newchan))) {
1182 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_DOTH, wh,
1183 "CSA ie mismatch, initial ie <%d,%d,%d>, "
1184 "this ie <%d,%d,%d>", ic->ic_csa_mode,
1185 ic->ic_csa_newchan->ic_ieee, ic->ic_csa_count,
1186 csa->csa_mode, csa->csa_newchan, csa->csa_count);
1187 ieee80211_csa_cancelswitch(ic);
1189 if (csa->csa_count <= 1)
1190 ieee80211_csa_completeswitch(ic);
1192 ic->ic_csa_count = csa->csa_count;
1200 * Return non-zero if a background scan may be continued:
1201 * o bg scan is active
1202 * o no channel switch is pending
1203 * o there has not been any traffic recently
1205 * Note we do not check if there is an administrative enable;
1206 * this is only done to start the scan. We assume that any
1207 * change in state will be accompanied by a request to cancel
1208 * active scans which will otherwise cause this test to fail.
1211 contbgscan(struct ieee80211vap *vap)
1213 struct ieee80211com *ic = vap->iv_ic;
1215 return ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) &&
1216 (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1217 vap->iv_state == IEEE80211_S_RUN && /* XXX? */
1218 time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1222 * Return non-zero if a backgrond scan may be started:
1223 * o bg scanning is administratively enabled
1224 * o no channel switch is pending
1225 * o we are not boosted on a dynamic turbo channel
1226 * o there has not been a scan recently
1227 * o there has not been any traffic recently
1230 startbgscan(struct ieee80211vap *vap)
1232 struct ieee80211com *ic = vap->iv_ic;
1234 return ((vap->iv_flags & IEEE80211_F_BGSCAN) &&
1235 (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1236 #ifdef IEEE80211_SUPPORT_SUPERG
1237 !IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1239 time_after(ticks, ic->ic_lastscan + vap->iv_bgscanintvl) &&
1240 time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1244 sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1245 int subtype, int rssi, int nf)
1247 #define ISPROBE(_st) ((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1248 #define ISREASSOC(_st) ((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
1249 struct ieee80211vap *vap = ni->ni_vap;
1250 struct ieee80211com *ic = ni->ni_ic;
1251 struct ieee80211_frame *wh;
1252 uint8_t *frm, *efrm;
1253 uint8_t *rates, *xrates, *wme, *htcap, *htinfo;
1256 wh = mtod(m0, struct ieee80211_frame *);
1257 frm = (uint8_t *)&wh[1];
1258 efrm = mtod(m0, uint8_t *) + m0->m_len;
1260 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1261 case IEEE80211_FC0_SUBTYPE_BEACON: {
1262 struct ieee80211_scanparams scan;
1264 * We process beacon/probe response frames:
1265 * o when scanning, or
1266 * o station mode when associated (to collect state
1267 * updates such as 802.11g slot time)
1268 * Frames otherwise received are discarded.
1270 if (!((ic->ic_flags & IEEE80211_F_SCAN) || ni->ni_associd)) {
1271 vap->iv_stats.is_rx_mgtdiscard++;
1274 /* XXX probe response in sta mode when !scanning? */
1275 if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
1278 * Count frame now that we know it's to be processed.
1280 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1281 vap->iv_stats.is_rx_beacon++; /* XXX remove */
1282 IEEE80211_NODE_STAT(ni, rx_beacons);
1284 IEEE80211_NODE_STAT(ni, rx_proberesp);
1286 * When operating in station mode, check for state updates.
1287 * Be careful to ignore beacons received while doing a
1288 * background scan. We consider only 11g/WMM stuff right now.
1290 if (ni->ni_associd != 0 &&
1291 ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
1292 IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
1293 /* record tsf of last beacon */
1294 memcpy(ni->ni_tstamp.data, scan.tstamp,
1295 sizeof(ni->ni_tstamp));
1296 /* count beacon frame for s/w bmiss handling */
1297 vap->iv_swbmiss_count++;
1298 vap->iv_bmiss_count = 0;
1299 if (ni->ni_erp != scan.erp) {
1300 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1302 "erp change: was 0x%x, now 0x%x",
1303 ni->ni_erp, scan.erp);
1304 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1305 (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1306 ic->ic_flags |= IEEE80211_F_USEPROT;
1308 ic->ic_flags &= ~IEEE80211_F_USEPROT;
1309 ni->ni_erp = scan.erp;
1311 /* XXX driver notification */
1313 if ((ni->ni_capinfo ^ scan.capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) {
1314 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1316 "capabilities change: was 0x%x, now 0x%x",
1317 ni->ni_capinfo, scan.capinfo);
1319 * NB: we assume short preamble doesn't
1320 * change dynamically
1322 ieee80211_set_shortslottime(ic,
1323 IEEE80211_IS_CHAN_A(ic->ic_bsschan) ||
1324 (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1325 ni->ni_capinfo = (ni->ni_capinfo &~ IEEE80211_CAPINFO_SHORT_SLOTTIME)
1326 | (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME);
1329 if (scan.wme != NULL &&
1330 (ni->ni_flags & IEEE80211_NODE_QOS) &&
1331 ieee80211_parse_wmeparams(vap, scan.wme, wh) > 0)
1332 ieee80211_wme_updateparams(vap);
1333 #ifdef IEEE80211_SUPPORT_SUPERG
1334 if (scan.ath != NULL)
1335 ieee80211_parse_athparams(ni, scan.ath, wh);
1337 if (scan.htcap != NULL && scan.htinfo != NULL &&
1338 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1339 ieee80211_ht_updateparams(ni,
1340 scan.htcap, scan.htinfo);
1341 /* XXX state changes? */
1343 if (scan.tim != NULL) {
1344 struct ieee80211_tim_ie *tim =
1345 (struct ieee80211_tim_ie *) scan.tim;
1347 int aid = IEEE80211_AID(ni->ni_associd);
1348 int ix = aid / NBBY;
1349 int min = tim->tim_bitctl &~ 1;
1350 int max = tim->tim_len + min - 4;
1351 if ((tim->tim_bitctl&1) ||
1352 (min <= ix && ix <= max &&
1353 isset(tim->tim_bitmap - min, aid))) {
1355 * XXX Do not let bg scan kick off
1356 * we are expecting data.
1358 ic->ic_lastdata = ticks;
1359 ieee80211_sta_pwrsave(vap, 0);
1362 ni->ni_dtim_count = tim->tim_count;
1363 ni->ni_dtim_period = tim->tim_period;
1365 if (scan.csa != NULL &&
1366 (vap->iv_flags & IEEE80211_F_DOTH))
1367 ieee80211_parse_csaparams(vap, scan.csa, wh);
1368 else if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
1370 * No CSA ie or 11h disabled, but a channel
1371 * switch is pending; drop out so we aren't
1372 * stuck in CSA state. If the AP really is
1373 * moving we'll get a beacon miss and scan.
1375 ieee80211_csa_cancelswitch(ic);
1378 * If scanning, pass the info to the scan module.
1379 * Otherwise, check if it's the right time to do
1380 * a background scan. Background scanning must
1381 * be enabled and we must not be operating in the
1382 * turbo phase of dynamic turbo mode. Then,
1383 * it's been a while since the last background
1384 * scan and if no data frames have come through
1385 * recently, kick off a scan. Note that this
1386 * is the mechanism by which a background scan
1387 * is started _and_ continued each time we
1388 * return on-channel to receive a beacon from
1391 if (ic->ic_flags & IEEE80211_F_SCAN) {
1392 ieee80211_add_scan(vap, &scan, wh,
1394 } else if (contbgscan(vap)) {
1395 ieee80211_bg_scan(vap, 0);
1396 } else if (startbgscan(vap)) {
1397 vap->iv_stats.is_scan_bg++;
1399 /* wakeup if we are sleeing */
1400 ieee80211_set_pwrsave(vap, 0);
1402 ieee80211_bg_scan(vap, 0);
1407 * If scanning, just pass information to the scan module.
1409 if (ic->ic_flags & IEEE80211_F_SCAN) {
1410 if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1412 * Actively scanning a channel marked passive;
1413 * send a probe request now that we know there
1414 * is 802.11 traffic present.
1416 * XXX check if the beacon we recv'd gives
1417 * us what we need and suppress the probe req
1419 ieee80211_probe_curchan(vap, 1);
1420 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1422 ieee80211_add_scan(vap, &scan, wh, subtype, rssi, nf);
1428 case IEEE80211_FC0_SUBTYPE_AUTH: {
1429 uint16_t algo, seq, status;
1437 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1438 algo = le16toh(*(uint16_t *)frm);
1439 seq = le16toh(*(uint16_t *)(frm + 2));
1440 status = le16toh(*(uint16_t *)(frm + 4));
1441 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1442 "recv auth frame with algorithm %d seq %d", algo, seq);
1444 if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1445 IEEE80211_DISCARD(vap,
1446 IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1447 wh, "auth", "%s", "TKIP countermeasures enabled");
1448 vap->iv_stats.is_rx_auth_countermeasures++;
1449 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1450 ieee80211_send_error(ni, wh->i_addr2,
1451 IEEE80211_FC0_SUBTYPE_AUTH,
1452 IEEE80211_REASON_MIC_FAILURE);
1456 if (algo == IEEE80211_AUTH_ALG_SHARED)
1457 sta_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1459 else if (algo == IEEE80211_AUTH_ALG_OPEN)
1460 sta_auth_open(ni, wh, rssi, nf, seq, status);
1462 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1463 wh, "auth", "unsupported alg %d", algo);
1464 vap->iv_stats.is_rx_auth_unsupported++;
1470 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1471 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
1472 uint16_t capinfo, associd;
1475 if (vap->iv_state != IEEE80211_S_ASSOC) {
1476 vap->iv_stats.is_rx_mgtdiscard++;
1481 * asresp frame format
1482 * [2] capability information
1484 * [2] association ID
1485 * [tlv] supported rates
1486 * [tlv] extended supported rates
1488 * [tlv] HT capabilities
1491 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1493 capinfo = le16toh(*(uint16_t *)frm);
1495 status = le16toh(*(uint16_t *)frm);
1498 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1499 wh->i_addr2, "%sassoc failed (reason %d)",
1500 ISREASSOC(subtype) ? "re" : "", status);
1501 vap->iv_stats.is_rx_auth_fail++; /* XXX */
1504 associd = le16toh(*(uint16_t *)frm);
1507 rates = xrates = wme = htcap = htinfo = NULL;
1508 while (efrm - frm > 1) {
1509 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1511 case IEEE80211_ELEMID_RATES:
1514 case IEEE80211_ELEMID_XRATES:
1517 case IEEE80211_ELEMID_HTCAP:
1520 case IEEE80211_ELEMID_HTINFO:
1523 case IEEE80211_ELEMID_VENDOR:
1526 else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1528 * Accept pre-draft HT ie's if the
1529 * standard ones have not been seen.
1531 if (ishtcapoui(frm)) {
1534 } else if (ishtinfooui(frm)) {
1539 /* XXX Atheros OUI support */
1545 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1547 IEEE80211_VERIFY_ELEMENT(xrates,
1548 IEEE80211_RATE_MAXSIZE - rates[1], return);
1549 rate = ieee80211_setup_rates(ni, rates, xrates,
1551 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1552 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1553 if (rate & IEEE80211_RATE_BASIC) {
1554 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1556 "%sassoc failed (rate set mismatch)",
1557 ISREASSOC(subtype) ? "re" : "");
1558 vap->iv_stats.is_rx_assoc_norate++;
1559 ieee80211_new_state(vap, IEEE80211_S_SCAN,
1560 IEEE80211_SCAN_FAIL_STATUS);
1564 ni->ni_capinfo = capinfo;
1565 ni->ni_associd = associd;
1566 if (ni->ni_jointime == 0)
1567 ni->ni_jointime = time_second;
1569 ieee80211_parse_wmeparams(vap, wme, wh) >= 0) {
1570 ni->ni_flags |= IEEE80211_NODE_QOS;
1571 ieee80211_wme_updateparams(vap);
1573 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1575 * Setup HT state according to the negotiation.
1577 * NB: shouldn't need to check if HT use is enabled but some
1578 * ap's send back HT ie's even when we don't indicate we
1579 * are HT capable in our AssocReq.
1581 if (htcap != NULL && htinfo != NULL &&
1582 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1583 ieee80211_ht_node_init(ni);
1584 ieee80211_ht_updateparams(ni, htcap, htinfo);
1585 ieee80211_setup_htrates(ni, htcap,
1586 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1587 ieee80211_setup_basic_htrates(ni, htinfo);
1588 ieee80211_node_setuptxparms(ni);
1590 #ifdef IEEE80211_SUPPORT_SUPERG
1591 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_ATH))
1592 ieee80211_ff_node_init(ni);
1596 * Configure state now that we are associated.
1598 * XXX may need different/additional driver callbacks?
1600 if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1601 (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1602 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1603 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1605 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1606 ic->ic_flags |= IEEE80211_F_USEBARKER;
1608 ieee80211_set_shortslottime(ic,
1609 IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1610 (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1612 * Honor ERP protection.
1614 * NB: ni_erp should zero for non-11g operation.
1616 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1617 (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1618 ic->ic_flags |= IEEE80211_F_USEPROT;
1620 ic->ic_flags &= ~IEEE80211_F_USEPROT;
1621 IEEE80211_NOTE_MAC(vap,
1622 IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, wh->i_addr2,
1623 "%sassoc success at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
1624 ISREASSOC(subtype) ? "re" : "",
1625 IEEE80211_NODE_AID(ni),
1626 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
1627 ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
1628 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
1629 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
1630 ni->ni_flags & IEEE80211_NODE_HT ?
1631 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
1632 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
1633 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
1634 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
1635 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
1636 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
1637 ", fast-frames" : "",
1638 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
1641 ieee80211_new_state(vap, IEEE80211_S_RUN, subtype);
1645 case IEEE80211_FC0_SUBTYPE_DEAUTH: {
1648 if (vap->iv_state == IEEE80211_S_SCAN) {
1649 vap->iv_stats.is_rx_mgtdiscard++;
1652 if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1653 /* NB: can happen when in promiscuous mode */
1654 vap->iv_stats.is_rx_mgtdiscard++;
1659 * deauth frame format
1662 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1663 reason = le16toh(*(uint16_t *)frm);
1665 vap->iv_stats.is_rx_deauth++;
1666 vap->iv_stats.is_rx_deauth_code = reason;
1667 IEEE80211_NODE_STAT(ni, rx_deauth);
1669 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1670 "recv deauthenticate (reason %d)", reason);
1671 ieee80211_new_state(vap, IEEE80211_S_AUTH,
1672 (reason << 8) | IEEE80211_FC0_SUBTYPE_DEAUTH);
1676 case IEEE80211_FC0_SUBTYPE_DISASSOC: {
1679 if (vap->iv_state != IEEE80211_S_RUN &&
1680 vap->iv_state != IEEE80211_S_ASSOC &&
1681 vap->iv_state != IEEE80211_S_AUTH) {
1682 vap->iv_stats.is_rx_mgtdiscard++;
1685 if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1686 /* NB: can happen when in promiscuous mode */
1687 vap->iv_stats.is_rx_mgtdiscard++;
1692 * disassoc frame format
1695 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1696 reason = le16toh(*(uint16_t *)frm);
1698 vap->iv_stats.is_rx_disassoc++;
1699 vap->iv_stats.is_rx_disassoc_code = reason;
1700 IEEE80211_NODE_STAT(ni, rx_disassoc);
1702 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1703 "recv disassociate (reason %d)", reason);
1704 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1708 case IEEE80211_FC0_SUBTYPE_ACTION:
1709 if (vap->iv_state == IEEE80211_S_RUN) {
1710 if (ieee80211_parse_action(ni, m0) == 0)
1711 ic->ic_recv_action(ni, wh, frm, efrm);
1713 vap->iv_stats.is_rx_mgtdiscard++;
1716 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1717 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1718 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1719 vap->iv_stats.is_rx_mgtdiscard++;
1722 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1723 wh, "mgt", "subtype 0x%x not handled", subtype);
1724 vap->iv_stats.is_rx_badsubtype++;
1732 sta_recv_ctl(struct ieee80211_node *ni, struct mbuf *m0, int subtype)