2 * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 * IEEE 802.11 Station mode support.
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/endian.h>
46 #include <sys/errno.h>
48 #include <sys/sysctl.h>
51 #include <net/if_media.h>
52 #include <net/if_llc.h>
53 #include <net/if_dl.h>
54 #include <net/if_var.h>
55 #include <net/ethernet.h>
59 #include <net80211/ieee80211_var.h>
60 #include <net80211/ieee80211_sta.h>
61 #include <net80211/ieee80211_input.h>
62 #ifdef IEEE80211_SUPPORT_SUPERG
63 #include <net80211/ieee80211_superg.h>
65 #include <net80211/ieee80211_ratectl.h>
66 #include <net80211/ieee80211_sta.h>
68 #define IEEE80211_RATE2MBS(r) (((r) & IEEE80211_RATE_VAL) / 2)
70 static void sta_vattach(struct ieee80211vap *);
71 static void sta_beacon_miss(struct ieee80211vap *);
72 static int sta_newstate(struct ieee80211vap *, enum ieee80211_state, int);
73 static int sta_input(struct ieee80211_node *, struct mbuf *, int, int);
74 static void sta_recv_mgmt(struct ieee80211_node *, struct mbuf *,
75 int subtype, int rssi, int nf);
76 static void sta_recv_ctl(struct ieee80211_node *, struct mbuf *, int subtype);
79 ieee80211_sta_attach(struct ieee80211com *ic)
81 ic->ic_vattach[IEEE80211_M_STA] = sta_vattach;
85 ieee80211_sta_detach(struct ieee80211com *ic)
90 sta_vdetach(struct ieee80211vap *vap)
95 sta_vattach(struct ieee80211vap *vap)
97 vap->iv_newstate = sta_newstate;
98 vap->iv_input = sta_input;
99 vap->iv_recv_mgmt = sta_recv_mgmt;
100 vap->iv_recv_ctl = sta_recv_ctl;
101 vap->iv_opdetach = sta_vdetach;
102 vap->iv_bmiss = sta_beacon_miss;
106 * Handle a beacon miss event. The common code filters out
107 * spurious events that can happen when scanning and/or before
108 * reaching RUN state.
111 sta_beacon_miss(struct ieee80211vap *vap)
113 struct ieee80211com *ic = vap->iv_ic;
115 IEEE80211_LOCK_ASSERT(ic);
117 KASSERT((ic->ic_flags & IEEE80211_F_SCAN) == 0, ("scanning"));
118 KASSERT(vap->iv_state >= IEEE80211_S_RUN,
119 ("wrong state %s", ieee80211_state_name[vap->iv_state]));
121 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
122 "beacon miss, mode %s state %s\n",
123 ieee80211_opmode_name[vap->iv_opmode],
124 ieee80211_state_name[vap->iv_state]);
126 if (vap->iv_state == IEEE80211_S_CSA) {
128 * A Channel Switch is pending; assume we missed the
129 * beacon that would've completed the process and just
130 * force the switch. If we made a mistake we'll not
131 * find the AP on the new channel and fall back to a
134 ieee80211_csa_completeswitch(ic);
137 if (++vap->iv_bmiss_count < vap->iv_bmiss_max) {
139 * Send a directed probe req before falling back to a
140 * scan; if we receive a response ic_bmiss_count will
141 * be reset. Some cards mistakenly report beacon miss
142 * so this avoids the expensive scan if the ap is
145 ieee80211_send_probereq(vap->iv_bss, vap->iv_myaddr,
146 vap->iv_bss->ni_bssid, vap->iv_bss->ni_bssid,
147 vap->iv_bss->ni_essid, vap->iv_bss->ni_esslen);
151 callout_stop(&vap->iv_swbmiss);
152 vap->iv_bmiss_count = 0;
153 vap->iv_stats.is_beacon_miss++;
154 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
155 #ifdef IEEE80211_SUPPORT_SUPERG
156 struct ieee80211com *ic = vap->iv_ic;
159 * If we receive a beacon miss interrupt when using
160 * dynamic turbo, attempt to switch modes before
163 if (IEEE80211_ATH_CAP(vap, vap->iv_bss, IEEE80211_NODE_TURBOP))
164 ieee80211_dturbo_switch(vap,
165 ic->ic_bsschan->ic_flags ^ IEEE80211_CHAN_TURBO);
168 * Try to reassociate before scanning for a new ap.
170 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
173 * Somebody else is controlling state changes (e.g.
174 * a user-mode app) don't do anything that would
175 * confuse them; just drop into scan mode so they'll
176 * notified of the state change and given control.
178 ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
183 * Handle deauth with reason. We retry only for
184 * the cases where we might succeed. Otherwise
185 * we downgrade the ap and scan.
188 sta_authretry(struct ieee80211vap *vap, struct ieee80211_node *ni, int reason)
191 case IEEE80211_STATUS_SUCCESS: /* NB: MLME assoc */
192 case IEEE80211_STATUS_TIMEOUT:
193 case IEEE80211_REASON_ASSOC_EXPIRE:
194 case IEEE80211_REASON_NOT_AUTHED:
195 case IEEE80211_REASON_NOT_ASSOCED:
196 case IEEE80211_REASON_ASSOC_LEAVE:
197 case IEEE80211_REASON_ASSOC_NOT_AUTHED:
198 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, 1);
201 ieee80211_scan_assoc_fail(vap, vap->iv_bss->ni_macaddr, reason);
202 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
203 ieee80211_check_scan_current(vap);
209 * IEEE80211_M_STA vap state machine handler.
210 * This routine handles the main states in the 802.11 protocol.
213 sta_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
215 struct ieee80211com *ic = vap->iv_ic;
216 struct ieee80211_node *ni;
217 enum ieee80211_state ostate;
219 IEEE80211_LOCK_ASSERT(ic);
221 ostate = vap->iv_state;
222 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
223 __func__, ieee80211_state_name[ostate],
224 ieee80211_state_name[nstate], arg);
225 vap->iv_state = nstate; /* state transition */
226 callout_stop(&vap->iv_mgtsend); /* XXX callout_drain */
227 if (ostate != IEEE80211_S_SCAN)
228 ieee80211_cancel_scan(vap); /* background scan */
229 ni = vap->iv_bss; /* NB: no reference held */
230 if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)
231 callout_stop(&vap->iv_swbmiss);
233 case IEEE80211_S_INIT:
235 case IEEE80211_S_SLEEP:
237 /* XXX driver hook to wakeup the hardware? */
238 case IEEE80211_S_RUN:
239 IEEE80211_SEND_MGMT(ni,
240 IEEE80211_FC0_SUBTYPE_DISASSOC,
241 IEEE80211_REASON_ASSOC_LEAVE);
242 ieee80211_sta_leave(ni);
244 case IEEE80211_S_ASSOC:
245 IEEE80211_SEND_MGMT(ni,
246 IEEE80211_FC0_SUBTYPE_DEAUTH,
247 IEEE80211_REASON_AUTH_LEAVE);
249 case IEEE80211_S_SCAN:
250 ieee80211_cancel_scan(vap);
255 if (ostate != IEEE80211_S_INIT) {
256 /* NB: optimize INIT -> INIT case */
257 ieee80211_reset_bss(vap);
259 if (vap->iv_auth->ia_detach != NULL)
260 vap->iv_auth->ia_detach(vap);
262 case IEEE80211_S_SCAN:
264 case IEEE80211_S_INIT:
266 * Initiate a scan. We can come here as a result
267 * of an IEEE80211_IOC_SCAN_REQ too in which case
268 * the vap will be marked with IEEE80211_FEXT_SCANREQ
269 * and the scan request parameters will be present
270 * in iv_scanreq. Otherwise we do the default.
272 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
273 ieee80211_check_scan(vap,
274 vap->iv_scanreq_flags,
275 vap->iv_scanreq_duration,
276 vap->iv_scanreq_mindwell,
277 vap->iv_scanreq_maxdwell,
278 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
279 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
281 ieee80211_check_scan_current(vap);
283 case IEEE80211_S_SCAN:
284 case IEEE80211_S_AUTH:
285 case IEEE80211_S_ASSOC:
287 * These can happen either because of a timeout
288 * on an assoc/auth response or because of a
289 * change in state that requires a reset. For
290 * the former we're called with a non-zero arg
291 * that is the cause for the failure; pass this
292 * to the scan code so it can update state.
293 * Otherwise trigger a new scan unless we're in
294 * manual roaming mode in which case an application
295 * must issue an explicit scan request.
298 ieee80211_scan_assoc_fail(vap,
299 vap->iv_bss->ni_macaddr, arg);
300 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
301 ieee80211_check_scan_current(vap);
303 case IEEE80211_S_SLEEP: /* beacon miss */
305 * XXX if in sleep we need to wakeup the hardware.
308 case IEEE80211_S_RUN: /* beacon miss */
310 * Beacon miss. Notify user space and if not
311 * under control of a user application (roaming
312 * manual) kick off a scan to re-connect.
315 ieee80211_sta_leave(ni);
316 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
317 ieee80211_check_scan_current(vap);
323 case IEEE80211_S_AUTH:
325 case IEEE80211_S_INIT:
326 case IEEE80211_S_SCAN:
327 IEEE80211_SEND_MGMT(ni,
328 IEEE80211_FC0_SUBTYPE_AUTH, 1);
330 case IEEE80211_S_AUTH:
331 case IEEE80211_S_ASSOC:
332 switch (arg & 0xff) {
333 case IEEE80211_FC0_SUBTYPE_AUTH:
335 IEEE80211_SEND_MGMT(ni,
336 IEEE80211_FC0_SUBTYPE_AUTH, 2);
338 case IEEE80211_FC0_SUBTYPE_DEAUTH:
339 sta_authretry(vap, ni, arg>>8);
343 case IEEE80211_S_RUN:
344 switch (arg & 0xff) {
345 case IEEE80211_FC0_SUBTYPE_AUTH:
346 IEEE80211_SEND_MGMT(ni,
347 IEEE80211_FC0_SUBTYPE_AUTH, 2);
348 vap->iv_state = ostate; /* stay RUN */
350 case IEEE80211_FC0_SUBTYPE_DEAUTH:
351 ieee80211_sta_leave(ni);
352 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
354 IEEE80211_SEND_MGMT(ni,
355 IEEE80211_FC0_SUBTYPE_AUTH, 1);
364 case IEEE80211_S_ASSOC:
366 case IEEE80211_S_AUTH:
367 case IEEE80211_S_ASSOC:
368 IEEE80211_SEND_MGMT(ni,
369 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
371 case IEEE80211_S_SLEEP: /* cannot happen */
372 case IEEE80211_S_RUN:
373 ieee80211_sta_leave(ni);
374 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
375 IEEE80211_SEND_MGMT(ni, arg ?
376 IEEE80211_FC0_SUBTYPE_REASSOC_REQ :
377 IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
384 case IEEE80211_S_RUN:
385 if (vap->iv_flags & IEEE80211_F_WPA) {
386 /* XXX validate prerequisites */
389 case IEEE80211_S_RUN:
390 case IEEE80211_S_CSA:
392 case IEEE80211_S_AUTH: /* when join is done in fw */
393 case IEEE80211_S_ASSOC:
394 #ifdef IEEE80211_DEBUG
395 if (ieee80211_msg_debug(vap)) {
396 ieee80211_note(vap, "%s with %s ssid ",
397 (vap->iv_opmode == IEEE80211_M_STA ?
398 "associated" : "synchronized"),
399 ether_sprintf(ni->ni_bssid));
400 ieee80211_print_essid(vap->iv_bss->ni_essid,
403 printf(" channel %d start %uMb\n",
404 ieee80211_chan2ieee(ic, ic->ic_curchan),
405 IEEE80211_RATE2MBS(ni->ni_txrate));
408 ieee80211_scan_assoc_success(vap, ni->ni_macaddr);
409 ieee80211_notify_node_join(ni,
410 arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
412 case IEEE80211_S_SLEEP:
413 /* Wake up from sleep */
414 vap->iv_sta_ps(vap, 0);
419 ieee80211_sync_curchan(ic);
420 if (ostate != IEEE80211_S_RUN &&
421 (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)) {
423 * Start s/w beacon miss timer for devices w/o
424 * hardware support. We fudge a bit here since
425 * we're doing this in software.
427 vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS(
428 2 * vap->iv_bmissthreshold * ni->ni_intval);
429 vap->iv_swbmiss_count = 0;
430 callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
431 ieee80211_swbmiss, vap);
434 * When 802.1x is not in use mark the port authorized
435 * at this point so traffic can flow.
437 if (ni->ni_authmode != IEEE80211_AUTH_8021X)
438 ieee80211_node_authorize(ni);
440 * Fake association when joining an existing bss.
442 * Don't do this if we're doing SLEEP->RUN.
444 if (ic->ic_newassoc != NULL && ostate != IEEE80211_S_SLEEP)
445 ic->ic_newassoc(vap->iv_bss, (ostate != IEEE80211_S_RUN));
447 case IEEE80211_S_CSA:
448 if (ostate != IEEE80211_S_RUN)
451 case IEEE80211_S_SLEEP:
452 vap->iv_sta_ps(vap, 1);
456 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
457 "%s: unexpected state transition %s -> %s\n", __func__,
458 ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
465 * Return non-zero if the frame is an echo of a multicast
466 * frame sent by ourself. The dir is known to be DSTODS.
469 isdstods_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
471 #define QWH4(wh) ((const struct ieee80211_qosframe_addr4 *)wh)
472 #define WH4(wh) ((const struct ieee80211_frame_addr4 *)wh)
475 KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
477 if (!IEEE80211_IS_MULTICAST(wh->i_addr3))
479 sa = IEEE80211_QOS_HAS_SEQ(wh) ? QWH4(wh)->i_addr4 : WH4(wh)->i_addr4;
480 return IEEE80211_ADDR_EQ(sa, vap->iv_myaddr);
486 * Return non-zero if the frame is an echo of a multicast
487 * frame sent by ourself. The dir is known to be FROMDS.
490 isfromds_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
492 KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
494 if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
496 return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
500 * Decide if a received management frame should be
501 * printed when debugging is enabled. This filters some
502 * of the less interesting frames that come frequently
506 doprint(struct ieee80211vap *vap, int subtype)
509 case IEEE80211_FC0_SUBTYPE_BEACON:
510 return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
511 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
518 * Process a received frame. The node associated with the sender
519 * should be supplied. If nothing was found in the node table then
520 * the caller is assumed to supply a reference to iv_bss instead.
521 * The RSSI and a timestamp are also supplied. The RSSI data is used
522 * during AP scanning to select a AP to associate with; it can have
523 * any units so long as values have consistent units and higher values
524 * mean ``better signal''. The receive timestamp is currently not used
525 * by the 802.11 layer.
528 sta_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
530 #define HAS_SEQ(type) ((type & 0x4) == 0)
531 struct ieee80211vap *vap = ni->ni_vap;
532 struct ieee80211com *ic = ni->ni_ic;
533 struct ifnet *ifp = vap->iv_ifp;
534 struct ieee80211_frame *wh;
535 struct ieee80211_key *key;
536 struct ether_header *eh;
537 int hdrspace, need_tap = 1; /* mbuf need to be tapped. */
538 uint8_t dir, type, subtype, qos;
542 if (m->m_flags & M_AMPDU_MPDU) {
544 * Fastpath for A-MPDU reorder q resubmission. Frames
545 * w/ M_AMPDU_MPDU marked have already passed through
546 * here but were received out of order and been held on
547 * the reorder queue. When resubmitted they are marked
548 * with the M_AMPDU_MPDU flag and we can bypass most of
549 * the normal processing.
551 wh = mtod(m, struct ieee80211_frame *);
552 type = IEEE80211_FC0_TYPE_DATA;
553 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
554 subtype = IEEE80211_FC0_SUBTYPE_QOS;
555 hdrspace = ieee80211_hdrspace(ic, wh); /* XXX optimize? */
559 KASSERT(ni != NULL, ("null node"));
560 ni->ni_inact = ni->ni_inact_reload;
562 type = -1; /* undefined */
564 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
565 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
566 ni->ni_macaddr, NULL,
567 "too short (1): len %u", m->m_pkthdr.len);
568 vap->iv_stats.is_rx_tooshort++;
572 * Bit of a cheat here, we use a pointer for a 3-address
573 * frame format but don't reference fields past outside
574 * ieee80211_frame_min w/o first validating the data is
577 wh = mtod(m, struct ieee80211_frame *);
579 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
580 IEEE80211_FC0_VERSION_0) {
581 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
582 ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
583 wh->i_fc[0], wh->i_fc[1]);
584 vap->iv_stats.is_rx_badversion++;
588 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
589 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
590 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
591 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
593 if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) {
594 /* not interested in */
595 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
596 bssid, NULL, "%s", "not to bss");
597 vap->iv_stats.is_rx_wrongbss++;
602 * Some devices may be in a promiscuous mode
603 * where they receive frames for multiple station
606 * If we receive a data frame that isn't
607 * destined to our VAP MAC, drop it.
609 * XXX TODO: This is only enforced when not scanning;
610 * XXX it assumes a software-driven scan will put the NIC
611 * XXX into a "no data frames" mode before setting this
612 * XXX flag. Otherwise it may be possible that we'll still
613 * XXX process data frames whilst scanning.
615 if ((! IEEE80211_IS_MULTICAST(wh->i_addr1))
616 && (! IEEE80211_ADDR_EQ(wh->i_addr1, IF_LLADDR(ifp)))) {
617 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
618 bssid, NULL, "not to cur sta: lladdr=%6D, addr1=%6D",
619 IF_LLADDR(ifp), ":", wh->i_addr1, ":");
620 vap->iv_stats.is_rx_wrongbss++;
624 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
626 if (HAS_SEQ(type) && !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
627 uint8_t tid = ieee80211_gettid(wh);
628 if (IEEE80211_QOS_HAS_SEQ(wh) &&
629 TID_TO_WME_AC(tid) >= WME_AC_VI)
630 ic->ic_wme.wme_hipri_traffic++;
631 rxseq = le16toh(*(uint16_t *)wh->i_seq);
632 if (! ieee80211_check_rxseq(ni, wh)) {
633 /* duplicate, discard */
634 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
636 "seqno <%u,%u> fragno <%u,%u> tid %u",
637 rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
638 ni->ni_rxseqs[tid] >>
639 IEEE80211_SEQ_SEQ_SHIFT,
640 rxseq & IEEE80211_SEQ_FRAG_MASK,
642 IEEE80211_SEQ_FRAG_MASK,
644 vap->iv_stats.is_rx_dup++;
645 IEEE80211_NODE_STAT(ni, rx_dup);
648 ni->ni_rxseqs[tid] = rxseq;
653 case IEEE80211_FC0_TYPE_DATA:
654 hdrspace = ieee80211_hdrspace(ic, wh);
655 if (m->m_len < hdrspace &&
656 (m = m_pullup(m, hdrspace)) == NULL) {
657 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
658 ni->ni_macaddr, NULL,
659 "data too short: expecting %u", hdrspace);
660 vap->iv_stats.is_rx_tooshort++;
664 * Handle A-MPDU re-ordering. If the frame is to be
665 * processed directly then ieee80211_ampdu_reorder
666 * will return 0; otherwise it has consumed the mbuf
667 * and we should do nothing more with it.
669 if ((m->m_flags & M_AMPDU) &&
670 (dir == IEEE80211_FC1_DIR_FROMDS ||
671 dir == IEEE80211_FC1_DIR_DSTODS) &&
672 ieee80211_ampdu_reorder(ni, m) != 0) {
677 if (dir == IEEE80211_FC1_DIR_FROMDS) {
678 if ((ifp->if_flags & IFF_SIMPLEX) &&
679 isfromds_mcastecho(vap, wh)) {
681 * In IEEE802.11 network, multicast
682 * packets sent from "me" are broadcast
683 * from the AP; silently discard for
686 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
687 wh, "data", "%s", "multicast echo");
688 vap->iv_stats.is_rx_mcastecho++;
691 if ((vap->iv_flags & IEEE80211_F_DWDS) &&
692 IEEE80211_IS_MULTICAST(wh->i_addr1)) {
694 * DWDS sta's must drop 3-address mcast frames
695 * as they will be sent separately as a 4-addr
696 * frame. Accepting the 3-addr frame will
697 * confuse the bridge into thinking the sending
698 * sta is located at the end of WDS link.
700 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
701 "3-address data", "%s", "DWDS enabled");
702 vap->iv_stats.is_rx_mcastecho++;
705 } else if (dir == IEEE80211_FC1_DIR_DSTODS) {
706 if ((vap->iv_flags & IEEE80211_F_DWDS) == 0) {
707 IEEE80211_DISCARD(vap,
708 IEEE80211_MSG_INPUT, wh, "4-address data",
709 "%s", "DWDS not enabled");
710 vap->iv_stats.is_rx_wrongdir++;
713 if ((ifp->if_flags & IFF_SIMPLEX) &&
714 isdstods_mcastecho(vap, wh)) {
716 * In IEEE802.11 network, multicast
717 * packets sent from "me" are broadcast
718 * from the AP; silently discard for
721 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
722 "4-address data", "%s", "multicast echo");
723 vap->iv_stats.is_rx_mcastecho++;
727 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
728 "data", "incorrect dir 0x%x", dir);
729 vap->iv_stats.is_rx_wrongdir++;
734 * Handle privacy requirements. Note that we
735 * must not be preempted from here until after
736 * we (potentially) call ieee80211_crypto_demic;
737 * otherwise we may violate assumptions in the
738 * crypto cipher modules used to do delayed update
739 * of replay sequence numbers.
741 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
742 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
744 * Discard encrypted frames when privacy is off.
746 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
747 wh, "WEP", "%s", "PRIVACY off");
748 vap->iv_stats.is_rx_noprivacy++;
749 IEEE80211_NODE_STAT(ni, rx_noprivacy);
752 key = ieee80211_crypto_decap(ni, m, hdrspace);
754 /* NB: stats+msgs handled in crypto_decap */
755 IEEE80211_NODE_STAT(ni, rx_wepfail);
758 wh = mtod(m, struct ieee80211_frame *);
759 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
761 /* XXX M_WEP and IEEE80211_F_PRIVACY */
766 * Save QoS bits for use below--before we strip the header.
768 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
769 qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
770 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
771 ((struct ieee80211_qosframe *)wh)->i_qos[0];
776 * Next up, any fragmentation.
778 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
779 m = ieee80211_defrag(ni, m, hdrspace);
781 /* Fragment dropped or frame not complete yet */
785 wh = NULL; /* no longer valid, catch any uses */
788 * Next strip any MSDU crypto bits.
790 if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
791 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
792 ni->ni_macaddr, "data", "%s", "demic error");
793 vap->iv_stats.is_rx_demicfail++;
794 IEEE80211_NODE_STAT(ni, rx_demicfail);
798 /* copy to listener after decrypt */
799 if (ieee80211_radiotap_active_vap(vap))
800 ieee80211_radiotap_rx(vap, m);
804 * Finally, strip the 802.11 header.
806 m = ieee80211_decap(vap, m, hdrspace);
808 /* XXX mask bit to check for both */
809 /* don't count Null data frames as errors */
810 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
811 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
813 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
814 ni->ni_macaddr, "data", "%s", "decap error");
815 vap->iv_stats.is_rx_decap++;
816 IEEE80211_NODE_STAT(ni, rx_decap);
819 eh = mtod(m, struct ether_header *);
820 if (!ieee80211_node_is_authorized(ni)) {
822 * Deny any non-PAE frames received prior to
823 * authorization. For open/shared-key
824 * authentication the port is mark authorized
825 * after authentication completes. For 802.1x
826 * the port is not marked authorized by the
827 * authenticator until the handshake has completed.
829 if (eh->ether_type != htons(ETHERTYPE_PAE)) {
830 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
831 eh->ether_shost, "data",
832 "unauthorized port: ether type 0x%x len %u",
833 eh->ether_type, m->m_pkthdr.len);
834 vap->iv_stats.is_rx_unauth++;
835 IEEE80211_NODE_STAT(ni, rx_unauth);
840 * When denying unencrypted frames, discard
841 * any non-PAE frames received without encryption.
843 if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
844 (key == NULL && (m->m_flags & M_WEP) == 0) &&
845 eh->ether_type != htons(ETHERTYPE_PAE)) {
847 * Drop unencrypted frames.
849 vap->iv_stats.is_rx_unencrypted++;
850 IEEE80211_NODE_STAT(ni, rx_unencrypted);
854 /* XXX require HT? */
855 if (qos & IEEE80211_QOS_AMSDU) {
856 m = ieee80211_decap_amsdu(ni, m);
858 return IEEE80211_FC0_TYPE_DATA;
860 #ifdef IEEE80211_SUPPORT_SUPERG
861 m = ieee80211_decap_fastframe(vap, ni, m);
863 return IEEE80211_FC0_TYPE_DATA;
866 ieee80211_deliver_data(vap, ni, m);
867 return IEEE80211_FC0_TYPE_DATA;
869 case IEEE80211_FC0_TYPE_MGT:
870 vap->iv_stats.is_rx_mgmt++;
871 IEEE80211_NODE_STAT(ni, rx_mgmt);
872 if (dir != IEEE80211_FC1_DIR_NODS) {
873 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
874 wh, "data", "incorrect dir 0x%x", dir);
875 vap->iv_stats.is_rx_wrongdir++;
878 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
879 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
880 ni->ni_macaddr, "mgt", "too short: len %u",
882 vap->iv_stats.is_rx_tooshort++;
885 #ifdef IEEE80211_DEBUG
886 if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
887 ieee80211_msg_dumppkts(vap)) {
888 if_printf(ifp, "received %s from %s rssi %d\n",
889 ieee80211_mgt_subtype_name[subtype >>
890 IEEE80211_FC0_SUBTYPE_SHIFT],
891 ether_sprintf(wh->i_addr2), rssi);
894 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
895 if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
897 * Only shared key auth frames with a challenge
898 * should be encrypted, discard all others.
900 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
901 wh, ieee80211_mgt_subtype_name[subtype >>
902 IEEE80211_FC0_SUBTYPE_SHIFT],
903 "%s", "WEP set but not permitted");
904 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
907 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
909 * Discard encrypted frames when privacy is off.
911 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
912 wh, "mgt", "%s", "WEP set but PRIVACY off");
913 vap->iv_stats.is_rx_noprivacy++;
916 hdrspace = ieee80211_hdrspace(ic, wh);
917 key = ieee80211_crypto_decap(ni, m, hdrspace);
919 /* NB: stats+msgs handled in crypto_decap */
922 wh = mtod(m, struct ieee80211_frame *);
923 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
925 vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
928 case IEEE80211_FC0_TYPE_CTL:
929 vap->iv_stats.is_rx_ctl++;
930 IEEE80211_NODE_STAT(ni, rx_ctrl);
931 vap->iv_recv_ctl(ni, m, subtype);
935 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
936 wh, NULL, "bad frame type 0x%x", type);
937 /* should not come here */
941 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
944 if (need_tap && ieee80211_radiotap_active_vap(vap))
945 ieee80211_radiotap_rx(vap, m);
952 sta_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
953 int rssi, int nf, uint16_t seq, uint16_t status)
955 struct ieee80211vap *vap = ni->ni_vap;
957 if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
958 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
959 ni->ni_macaddr, "open auth",
960 "bad sta auth mode %u", ni->ni_authmode);
961 vap->iv_stats.is_rx_bad_auth++; /* XXX */
964 if (vap->iv_state != IEEE80211_S_AUTH ||
965 seq != IEEE80211_AUTH_OPEN_RESPONSE) {
966 vap->iv_stats.is_rx_bad_auth++;
970 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
971 ni, "open auth failed (reason %d)", status);
972 vap->iv_stats.is_rx_auth_fail++;
973 vap->iv_stats.is_rx_authfail_code = status;
974 ieee80211_new_state(vap, IEEE80211_S_SCAN,
975 IEEE80211_SCAN_FAIL_STATUS);
977 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
981 sta_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
982 uint8_t *frm, uint8_t *efrm, int rssi, int nf,
983 uint16_t seq, uint16_t status)
985 struct ieee80211vap *vap = ni->ni_vap;
990 * NB: this can happen as we allow pre-shared key
991 * authentication to be enabled w/o wep being turned
992 * on so that configuration of these can be done
993 * in any order. It may be better to enforce the
994 * ordering in which case this check would just be
995 * for sanity/consistency.
997 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
998 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
999 ni->ni_macaddr, "shared key auth",
1000 "%s", " PRIVACY is disabled");
1001 estatus = IEEE80211_STATUS_ALG;
1005 * Pre-shared key authentication is evil; accept
1006 * it only if explicitly configured (it is supported
1007 * mainly for compatibility with clients like OS X).
1009 if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1010 ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1011 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1012 ni->ni_macaddr, "shared key auth",
1013 "bad sta auth mode %u", ni->ni_authmode);
1014 vap->iv_stats.is_rx_bad_auth++; /* XXX maybe a unique error? */
1015 estatus = IEEE80211_STATUS_ALG;
1020 if (frm + 1 < efrm) {
1021 if ((frm[1] + 2) > (efrm - frm)) {
1022 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1023 ni->ni_macaddr, "shared key auth",
1024 "ie %d/%d too long",
1025 frm[0], (frm[1] + 2) - (efrm - frm));
1026 vap->iv_stats.is_rx_bad_auth++;
1027 estatus = IEEE80211_STATUS_CHALLENGE;
1030 if (*frm == IEEE80211_ELEMID_CHALLENGE)
1035 case IEEE80211_AUTH_SHARED_CHALLENGE:
1036 case IEEE80211_AUTH_SHARED_RESPONSE:
1037 if (challenge == NULL) {
1038 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1039 ni->ni_macaddr, "shared key auth",
1040 "%s", "no challenge");
1041 vap->iv_stats.is_rx_bad_auth++;
1042 estatus = IEEE80211_STATUS_CHALLENGE;
1045 if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1046 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1047 ni->ni_macaddr, "shared key auth",
1048 "bad challenge len %d", challenge[1]);
1049 vap->iv_stats.is_rx_bad_auth++;
1050 estatus = IEEE80211_STATUS_CHALLENGE;
1056 if (vap->iv_state != IEEE80211_S_AUTH)
1059 case IEEE80211_AUTH_SHARED_PASS:
1060 if (ni->ni_challenge != NULL) {
1061 free(ni->ni_challenge, M_80211_NODE);
1062 ni->ni_challenge = NULL;
1065 IEEE80211_NOTE_FRAME(vap,
1066 IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, wh,
1067 "shared key auth failed (reason %d)", status);
1068 vap->iv_stats.is_rx_auth_fail++;
1069 vap->iv_stats.is_rx_authfail_code = status;
1072 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1074 case IEEE80211_AUTH_SHARED_CHALLENGE:
1075 if (!ieee80211_alloc_challenge(ni))
1077 /* XXX could optimize by passing recvd challenge */
1078 memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
1079 IEEE80211_SEND_MGMT(ni,
1080 IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1083 IEEE80211_DISCARD(vap, IEEE80211_MSG_AUTH,
1084 wh, "shared key auth", "bad seq %d", seq);
1085 vap->iv_stats.is_rx_bad_auth++;
1091 * Kick the state machine. This short-circuits
1092 * using the mgt frame timeout to trigger the
1095 if (vap->iv_state == IEEE80211_S_AUTH)
1096 ieee80211_new_state(vap, IEEE80211_S_SCAN,
1097 IEEE80211_SCAN_FAIL_STATUS);
1101 ieee80211_parse_wmeparams(struct ieee80211vap *vap, uint8_t *frm,
1102 const struct ieee80211_frame *wh)
1104 #define MS(_v, _f) (((_v) & _f) >> _f##_S)
1105 struct ieee80211_wme_state *wme = &vap->iv_ic->ic_wme;
1106 u_int len = frm[1], qosinfo;
1109 if (len < sizeof(struct ieee80211_wme_param)-2) {
1110 IEEE80211_DISCARD_IE(vap,
1111 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
1112 wh, "WME", "too short, len %u", len);
1115 qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)];
1116 qosinfo &= WME_QOSINFO_COUNT;
1117 /* XXX do proper check for wraparound */
1118 if (qosinfo == wme->wme_wmeChanParams.cap_info)
1120 frm += __offsetof(struct ieee80211_wme_param, params_acParams);
1121 for (i = 0; i < WME_NUM_AC; i++) {
1122 struct wmeParams *wmep =
1123 &wme->wme_wmeChanParams.cap_wmeParams[i];
1124 /* NB: ACI not used */
1125 wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
1126 wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
1127 wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
1128 wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
1129 wmep->wmep_txopLimit = LE_READ_2(frm+2);
1132 wme->wme_wmeChanParams.cap_info = qosinfo;
1138 * Process 11h Channel Switch Announcement (CSA) ie. If this
1139 * is the first CSA then initiate the switch. Otherwise we
1140 * track state and trigger completion and/or cancel of the switch.
1141 * XXX should be public for IBSS use
1144 ieee80211_parse_csaparams(struct ieee80211vap *vap, uint8_t *frm,
1145 const struct ieee80211_frame *wh)
1147 struct ieee80211com *ic = vap->iv_ic;
1148 const struct ieee80211_csa_ie *csa =
1149 (const struct ieee80211_csa_ie *) frm;
1151 KASSERT(vap->iv_state >= IEEE80211_S_RUN,
1152 ("state %s", ieee80211_state_name[vap->iv_state]));
1154 if (csa->csa_mode > 1) {
1155 IEEE80211_DISCARD_IE(vap,
1156 IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1157 wh, "CSA", "invalid mode %u", csa->csa_mode);
1161 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
1163 * Convert the channel number to a channel reference. We
1164 * try first to preserve turbo attribute of the current
1165 * channel then fallback. Note this will not work if the
1166 * CSA specifies a channel that requires a band switch (e.g.
1167 * 11a => 11g). This is intentional as 11h is defined only
1168 * for 5GHz/11a and because the switch does not involve a
1169 * reassociation, protocol state (capabilities, negotated
1170 * rates, etc) may/will be wrong.
1172 struct ieee80211_channel *c =
1173 ieee80211_find_channel_byieee(ic, csa->csa_newchan,
1174 (ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALLTURBO));
1176 c = ieee80211_find_channel_byieee(ic,
1178 (ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALL));
1180 IEEE80211_DISCARD_IE(vap,
1181 IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1182 wh, "CSA", "invalid channel %u",
1187 #if IEEE80211_CSA_COUNT_MIN > 0
1188 if (csa->csa_count < IEEE80211_CSA_COUNT_MIN) {
1190 * Require at least IEEE80211_CSA_COUNT_MIN count to
1191 * reduce the risk of being redirected by a fabricated
1192 * CSA. If a valid CSA is dropped we'll still get a
1193 * beacon miss when the AP leaves the channel so we'll
1194 * eventually follow to the new channel.
1196 * NOTE: this violates the 11h spec that states that
1197 * count may be any value and if 0 then a switch
1198 * should happen asap.
1200 IEEE80211_DISCARD_IE(vap,
1201 IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1202 wh, "CSA", "count %u too small, must be >= %u",
1203 csa->csa_count, IEEE80211_CSA_COUNT_MIN);
1207 ieee80211_csa_startswitch(ic, c, csa->csa_mode, csa->csa_count);
1210 * Validate this ie against the initial CSA. We require
1211 * mode and channel not change and the count must be
1212 * monotonically decreasing. This may be pointless and
1213 * canceling the switch as a result may be too paranoid but
1214 * in the worst case if we drop out of CSA because of this
1215 * and the AP does move then we'll just end up taking a
1216 * beacon miss and scan to find the AP.
1218 * XXX may want <= on count as we also process ProbeResp
1219 * frames and those may come in w/ the same count as the
1220 * previous beacon; but doing so leaves us open to a stuck
1221 * count until we add a dead-man timer
1223 if (!(csa->csa_count < ic->ic_csa_count &&
1224 csa->csa_mode == ic->ic_csa_mode &&
1225 csa->csa_newchan == ieee80211_chan2ieee(ic, ic->ic_csa_newchan))) {
1226 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_DOTH, wh,
1227 "CSA ie mismatch, initial ie <%d,%d,%d>, "
1228 "this ie <%d,%d,%d>", ic->ic_csa_mode,
1229 ic->ic_csa_newchan, ic->ic_csa_count,
1230 csa->csa_mode, csa->csa_newchan, csa->csa_count);
1231 ieee80211_csa_cancelswitch(ic);
1233 if (csa->csa_count <= 1)
1234 ieee80211_csa_completeswitch(ic);
1236 ic->ic_csa_count = csa->csa_count;
1240 IEEE80211_UNLOCK(ic);
1244 * Return non-zero if a background scan may be continued:
1245 * o bg scan is active
1246 * o no channel switch is pending
1247 * o there has not been any traffic recently
1249 * Note we do not check if there is an administrative enable;
1250 * this is only done to start the scan. We assume that any
1251 * change in state will be accompanied by a request to cancel
1252 * active scans which will otherwise cause this test to fail.
1255 contbgscan(struct ieee80211vap *vap)
1257 struct ieee80211com *ic = vap->iv_ic;
1259 return ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) &&
1260 (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1261 vap->iv_state == IEEE80211_S_RUN && /* XXX? */
1262 time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1266 * Return non-zero if a backgrond scan may be started:
1267 * o bg scanning is administratively enabled
1268 * o no channel switch is pending
1269 * o we are not boosted on a dynamic turbo channel
1270 * o there has not been a scan recently
1271 * o there has not been any traffic recently
1274 startbgscan(struct ieee80211vap *vap)
1276 struct ieee80211com *ic = vap->iv_ic;
1278 return ((vap->iv_flags & IEEE80211_F_BGSCAN) &&
1279 (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1280 #ifdef IEEE80211_SUPPORT_SUPERG
1281 !IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1283 time_after(ticks, ic->ic_lastscan + vap->iv_bgscanintvl) &&
1284 time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1288 sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1289 int subtype, int rssi, int nf)
1291 #define ISPROBE(_st) ((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1292 #define ISREASSOC(_st) ((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
1293 struct ieee80211vap *vap = ni->ni_vap;
1294 struct ieee80211com *ic = ni->ni_ic;
1295 struct ieee80211_frame *wh;
1296 uint8_t *frm, *efrm;
1297 uint8_t *rates, *xrates, *wme, *htcap, *htinfo;
1299 int ht_state_change = 0;
1301 wh = mtod(m0, struct ieee80211_frame *);
1302 frm = (uint8_t *)&wh[1];
1303 efrm = mtod(m0, uint8_t *) + m0->m_len;
1305 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1306 case IEEE80211_FC0_SUBTYPE_BEACON: {
1307 struct ieee80211_scanparams scan;
1309 * We process beacon/probe response frames:
1310 * o when scanning, or
1311 * o station mode when associated (to collect state
1312 * updates such as 802.11g slot time)
1313 * Frames otherwise received are discarded.
1315 if (!((ic->ic_flags & IEEE80211_F_SCAN) || ni->ni_associd)) {
1316 vap->iv_stats.is_rx_mgtdiscard++;
1319 /* XXX probe response in sta mode when !scanning? */
1320 if (ieee80211_parse_beacon(ni, m0, &scan) != 0) {
1321 if (! (ic->ic_flags & IEEE80211_F_SCAN))
1322 vap->iv_stats.is_beacon_bad++;
1327 * Count frame now that we know it's to be processed.
1329 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1330 vap->iv_stats.is_rx_beacon++; /* XXX remove */
1331 IEEE80211_NODE_STAT(ni, rx_beacons);
1333 IEEE80211_NODE_STAT(ni, rx_proberesp);
1335 * When operating in station mode, check for state updates.
1336 * Be careful to ignore beacons received while doing a
1337 * background scan. We consider only 11g/WMM stuff right now.
1339 if (ni->ni_associd != 0 &&
1340 ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
1341 IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
1342 /* record tsf of last beacon */
1343 memcpy(ni->ni_tstamp.data, scan.tstamp,
1344 sizeof(ni->ni_tstamp));
1345 /* count beacon frame for s/w bmiss handling */
1346 vap->iv_swbmiss_count++;
1347 vap->iv_bmiss_count = 0;
1348 if (ni->ni_erp != scan.erp) {
1349 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1351 "erp change: was 0x%x, now 0x%x",
1352 ni->ni_erp, scan.erp);
1353 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1354 (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1355 ic->ic_flags |= IEEE80211_F_USEPROT;
1357 ic->ic_flags &= ~IEEE80211_F_USEPROT;
1358 ni->ni_erp = scan.erp;
1360 /* XXX driver notification */
1362 if ((ni->ni_capinfo ^ scan.capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) {
1363 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1365 "capabilities change: was 0x%x, now 0x%x",
1366 ni->ni_capinfo, scan.capinfo);
1368 * NB: we assume short preamble doesn't
1369 * change dynamically
1371 ieee80211_set_shortslottime(ic,
1372 IEEE80211_IS_CHAN_A(ic->ic_bsschan) ||
1373 (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1374 ni->ni_capinfo = (ni->ni_capinfo &~ IEEE80211_CAPINFO_SHORT_SLOTTIME)
1375 | (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME);
1378 if (scan.wme != NULL &&
1379 (ni->ni_flags & IEEE80211_NODE_QOS) &&
1380 ieee80211_parse_wmeparams(vap, scan.wme, wh) > 0)
1381 ieee80211_wme_updateparams(vap);
1382 #ifdef IEEE80211_SUPPORT_SUPERG
1383 if (scan.ath != NULL)
1384 ieee80211_parse_athparams(ni, scan.ath, wh);
1386 if (scan.htcap != NULL && scan.htinfo != NULL &&
1387 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1388 /* XXX state changes? */
1389 if (ieee80211_ht_updateparams(ni,
1390 scan.htcap, scan.htinfo))
1391 ht_state_change = 1;
1394 ic->ic_set_quiet(ni, scan.quiet);
1396 if (scan.tim != NULL) {
1397 struct ieee80211_tim_ie *tim =
1398 (struct ieee80211_tim_ie *) scan.tim;
1400 * XXX Check/debug this code; see if it's about
1401 * the right time to force the VAP awake if we
1402 * receive a frame destined for us?
1404 int aid = IEEE80211_AID(ni->ni_associd);
1405 int ix = aid / NBBY;
1406 int min = tim->tim_bitctl &~ 1;
1407 int max = tim->tim_len + min - 4;
1410 * Only do this for unicast traffic in the TIM
1411 * The multicast traffic notification for
1412 * the scan notification stuff should occur
1415 if (min <= ix && ix <= max &&
1416 isset(tim->tim_bitmap - min, aid)) {
1417 ieee80211_sta_tim_notify(vap, 1);
1418 ic->ic_lastdata = ticks;
1422 * XXX TODO: do a separate notification
1423 * for the multicast bit being set.
1426 if (tim->tim_bitctl & 1) {
1427 ieee80211_sta_tim_notify(vap, 1);
1428 ic->ic_lastdata = ticks;
1432 ni->ni_dtim_count = tim->tim_count;
1433 ni->ni_dtim_period = tim->tim_period;
1435 if (scan.csa != NULL &&
1436 (vap->iv_flags & IEEE80211_F_DOTH))
1437 ieee80211_parse_csaparams(vap, scan.csa, wh);
1438 else if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
1440 * No CSA ie or 11h disabled, but a channel
1441 * switch is pending; drop out so we aren't
1442 * stuck in CSA state. If the AP really is
1443 * moving we'll get a beacon miss and scan.
1446 ieee80211_csa_cancelswitch(ic);
1447 IEEE80211_UNLOCK(ic);
1450 * If scanning, pass the info to the scan module.
1451 * Otherwise, check if it's the right time to do
1452 * a background scan. Background scanning must
1453 * be enabled and we must not be operating in the
1454 * turbo phase of dynamic turbo mode. Then,
1455 * it's been a while since the last background
1456 * scan and if no data frames have come through
1457 * recently, kick off a scan. Note that this
1458 * is the mechanism by which a background scan
1459 * is started _and_ continued each time we
1460 * return on-channel to receive a beacon from
1463 if (ic->ic_flags & IEEE80211_F_SCAN) {
1464 ieee80211_add_scan(vap, &scan, wh,
1466 } else if (contbgscan(vap)) {
1467 ieee80211_bg_scan(vap, 0);
1468 } else if (startbgscan(vap)) {
1469 vap->iv_stats.is_scan_bg++;
1471 /* wakeup if we are sleeing */
1472 ieee80211_set_pwrsave(vap, 0);
1474 ieee80211_bg_scan(vap, 0);
1478 * Put the station to sleep if we haven't seen
1479 * traffic in a while.
1482 ieee80211_sta_ps_timer_check(vap);
1483 IEEE80211_UNLOCK(ic);
1486 * If we've had a channel width change (eg HT20<->HT40)
1487 * then schedule a delayed driver notification.
1489 if (ht_state_change)
1490 ieee80211_update_chw(ic);
1494 * If scanning, just pass information to the scan module.
1496 if (ic->ic_flags & IEEE80211_F_SCAN) {
1497 if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1499 * Actively scanning a channel marked passive;
1500 * send a probe request now that we know there
1501 * is 802.11 traffic present.
1503 * XXX check if the beacon we recv'd gives
1504 * us what we need and suppress the probe req
1506 ieee80211_probe_curchan(vap, 1);
1507 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1509 ieee80211_add_scan(vap, &scan, wh, subtype, rssi, nf);
1515 case IEEE80211_FC0_SUBTYPE_AUTH: {
1516 uint16_t algo, seq, status;
1524 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1525 algo = le16toh(*(uint16_t *)frm);
1526 seq = le16toh(*(uint16_t *)(frm + 2));
1527 status = le16toh(*(uint16_t *)(frm + 4));
1528 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1529 "recv auth frame with algorithm %d seq %d", algo, seq);
1531 if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1532 IEEE80211_DISCARD(vap,
1533 IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1534 wh, "auth", "%s", "TKIP countermeasures enabled");
1535 vap->iv_stats.is_rx_auth_countermeasures++;
1536 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1537 ieee80211_send_error(ni, wh->i_addr2,
1538 IEEE80211_FC0_SUBTYPE_AUTH,
1539 IEEE80211_REASON_MIC_FAILURE);
1543 if (algo == IEEE80211_AUTH_ALG_SHARED)
1544 sta_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1546 else if (algo == IEEE80211_AUTH_ALG_OPEN)
1547 sta_auth_open(ni, wh, rssi, nf, seq, status);
1549 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1550 wh, "auth", "unsupported alg %d", algo);
1551 vap->iv_stats.is_rx_auth_unsupported++;
1557 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1558 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
1559 uint16_t capinfo, associd;
1562 if (vap->iv_state != IEEE80211_S_ASSOC) {
1563 vap->iv_stats.is_rx_mgtdiscard++;
1568 * asresp frame format
1569 * [2] capability information
1571 * [2] association ID
1572 * [tlv] supported rates
1573 * [tlv] extended supported rates
1575 * [tlv] HT capabilities
1578 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1580 capinfo = le16toh(*(uint16_t *)frm);
1582 status = le16toh(*(uint16_t *)frm);
1585 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1586 wh->i_addr2, "%sassoc failed (reason %d)",
1587 ISREASSOC(subtype) ? "re" : "", status);
1588 vap->iv_stats.is_rx_auth_fail++; /* XXX */
1591 associd = le16toh(*(uint16_t *)frm);
1594 rates = xrates = wme = htcap = htinfo = NULL;
1595 while (efrm - frm > 1) {
1596 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1598 case IEEE80211_ELEMID_RATES:
1601 case IEEE80211_ELEMID_XRATES:
1604 case IEEE80211_ELEMID_HTCAP:
1607 case IEEE80211_ELEMID_HTINFO:
1610 case IEEE80211_ELEMID_VENDOR:
1613 else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1615 * Accept pre-draft HT ie's if the
1616 * standard ones have not been seen.
1618 if (ishtcapoui(frm)) {
1621 } else if (ishtinfooui(frm)) {
1626 /* XXX Atheros OUI support */
1632 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1634 IEEE80211_VERIFY_ELEMENT(xrates,
1635 IEEE80211_RATE_MAXSIZE - rates[1], return);
1636 rate = ieee80211_setup_rates(ni, rates, xrates,
1638 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1639 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1640 if (rate & IEEE80211_RATE_BASIC) {
1641 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1643 "%sassoc failed (rate set mismatch)",
1644 ISREASSOC(subtype) ? "re" : "");
1645 vap->iv_stats.is_rx_assoc_norate++;
1646 ieee80211_new_state(vap, IEEE80211_S_SCAN,
1647 IEEE80211_SCAN_FAIL_STATUS);
1651 ni->ni_capinfo = capinfo;
1652 ni->ni_associd = associd;
1653 if (ni->ni_jointime == 0)
1654 ni->ni_jointime = time_uptime;
1656 ieee80211_parse_wmeparams(vap, wme, wh) >= 0) {
1657 ni->ni_flags |= IEEE80211_NODE_QOS;
1658 ieee80211_wme_updateparams(vap);
1660 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1662 * Setup HT state according to the negotiation.
1664 * NB: shouldn't need to check if HT use is enabled but some
1665 * ap's send back HT ie's even when we don't indicate we
1666 * are HT capable in our AssocReq.
1668 if (htcap != NULL && htinfo != NULL &&
1669 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1670 ieee80211_ht_node_init(ni);
1671 ieee80211_ht_updateparams(ni, htcap, htinfo);
1672 ieee80211_setup_htrates(ni, htcap,
1673 IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1674 ieee80211_setup_basic_htrates(ni, htinfo);
1675 ieee80211_node_setuptxparms(ni);
1676 ieee80211_ratectl_node_init(ni);
1678 #ifdef IEEE80211_SUPPORT_SUPERG
1679 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_ATH))
1680 ieee80211_ff_node_init(ni);
1684 * Configure state now that we are associated.
1686 * XXX may need different/additional driver callbacks?
1688 if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1689 (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1690 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1691 ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1693 ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1694 ic->ic_flags |= IEEE80211_F_USEBARKER;
1696 ieee80211_set_shortslottime(ic,
1697 IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1698 (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1700 * Honor ERP protection.
1702 * NB: ni_erp should zero for non-11g operation.
1704 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1705 (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1706 ic->ic_flags |= IEEE80211_F_USEPROT;
1708 ic->ic_flags &= ~IEEE80211_F_USEPROT;
1709 IEEE80211_NOTE_MAC(vap,
1710 IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, wh->i_addr2,
1711 "%sassoc success at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
1712 ISREASSOC(subtype) ? "re" : "",
1713 IEEE80211_NODE_AID(ni),
1714 ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
1715 ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
1716 ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
1717 ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
1718 ni->ni_flags & IEEE80211_NODE_HT ?
1719 (ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
1720 ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
1721 ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
1722 ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
1723 ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
1724 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
1725 ", fast-frames" : "",
1726 IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
1729 ieee80211_new_state(vap, IEEE80211_S_RUN, subtype);
1733 case IEEE80211_FC0_SUBTYPE_DEAUTH: {
1736 if (vap->iv_state == IEEE80211_S_SCAN) {
1737 vap->iv_stats.is_rx_mgtdiscard++;
1740 if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1741 /* NB: can happen when in promiscuous mode */
1742 vap->iv_stats.is_rx_mgtdiscard++;
1747 * deauth frame format
1750 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1751 reason = le16toh(*(uint16_t *)frm);
1753 vap->iv_stats.is_rx_deauth++;
1754 vap->iv_stats.is_rx_deauth_code = reason;
1755 IEEE80211_NODE_STAT(ni, rx_deauth);
1757 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1758 "recv deauthenticate (reason %d)", reason);
1759 ieee80211_new_state(vap, IEEE80211_S_AUTH,
1760 (reason << 8) | IEEE80211_FC0_SUBTYPE_DEAUTH);
1764 case IEEE80211_FC0_SUBTYPE_DISASSOC: {
1767 if (vap->iv_state != IEEE80211_S_RUN &&
1768 vap->iv_state != IEEE80211_S_ASSOC &&
1769 vap->iv_state != IEEE80211_S_AUTH) {
1770 vap->iv_stats.is_rx_mgtdiscard++;
1773 if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1774 /* NB: can happen when in promiscuous mode */
1775 vap->iv_stats.is_rx_mgtdiscard++;
1780 * disassoc frame format
1783 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1784 reason = le16toh(*(uint16_t *)frm);
1786 vap->iv_stats.is_rx_disassoc++;
1787 vap->iv_stats.is_rx_disassoc_code = reason;
1788 IEEE80211_NODE_STAT(ni, rx_disassoc);
1790 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1791 "recv disassociate (reason %d)", reason);
1792 ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1796 case IEEE80211_FC0_SUBTYPE_ACTION:
1797 case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
1798 if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
1799 !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1800 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1801 wh, NULL, "%s", "not for us");
1802 vap->iv_stats.is_rx_mgtdiscard++;
1803 } else if (vap->iv_state != IEEE80211_S_RUN) {
1804 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1805 wh, NULL, "wrong state %s",
1806 ieee80211_state_name[vap->iv_state]);
1807 vap->iv_stats.is_rx_mgtdiscard++;
1809 if (ieee80211_parse_action(ni, m0) == 0)
1810 (void)ic->ic_recv_action(ni, wh, frm, efrm);
1814 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1815 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1816 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1817 case IEEE80211_FC0_SUBTYPE_ATIM:
1818 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1819 wh, NULL, "%s", "not handled");
1820 vap->iv_stats.is_rx_mgtdiscard++;
1824 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1825 wh, "mgt", "subtype 0x%x not handled", subtype);
1826 vap->iv_stats.is_rx_badsubtype++;
1834 sta_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
1837 case IEEE80211_FC0_SUBTYPE_BAR:
1838 ieee80211_recv_bar(ni, m);