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 HOSTAP 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/ethernet.h>
57 #include <net80211/ieee80211_var.h>
58 #include <net80211/ieee80211_hostap.h>
59 #include <net80211/ieee80211_input.h>
60 #ifdef IEEE80211_SUPPORT_SUPERG
61 #include <net80211/ieee80211_superg.h>
63 #include <net80211/ieee80211_wds.h>
65 #define IEEE80211_RATE2MBS(r) (((r) & IEEE80211_RATE_VAL) / 2)
67 static void hostap_vattach(struct ieee80211vap *);
68 static int hostap_newstate(struct ieee80211vap *, enum ieee80211_state, int);
69 static int hostap_input(struct ieee80211_node *ni, struct mbuf *m,
71 static void hostap_deliver_data(struct ieee80211vap *,
72 struct ieee80211_node *, struct mbuf *);
73 static void hostap_recv_mgmt(struct ieee80211_node *, struct mbuf *,
74 int subtype, int rssi, int nf);
75 static void hostap_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
78 ieee80211_hostap_attach(struct ieee80211com *ic)
80 ic->ic_vattach[IEEE80211_M_HOSTAP] = hostap_vattach;
84 ieee80211_hostap_detach(struct ieee80211com *ic)
89 hostap_vdetach(struct ieee80211vap *vap)
94 hostap_vattach(struct ieee80211vap *vap)
96 vap->iv_newstate = hostap_newstate;
97 vap->iv_input = hostap_input;
98 vap->iv_recv_mgmt = hostap_recv_mgmt;
99 vap->iv_recv_ctl = hostap_recv_ctl;
100 vap->iv_opdetach = hostap_vdetach;
101 vap->iv_deliver_data = hostap_deliver_data;
102 vap->iv_recv_pspoll = ieee80211_recv_pspoll;
106 sta_disassoc(void *arg, struct ieee80211_node *ni)
108 struct ieee80211vap *vap = arg;
110 if (ni->ni_vap == vap && ni->ni_associd != 0) {
111 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
112 IEEE80211_REASON_ASSOC_LEAVE);
113 ieee80211_node_leave(ni);
118 sta_csa(void *arg, struct ieee80211_node *ni)
120 struct ieee80211vap *vap = arg;
122 if (ni->ni_vap == vap && ni->ni_associd != 0)
123 if (ni->ni_inact > vap->iv_inact_init) {
124 ni->ni_inact = vap->iv_inact_init;
125 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
126 "%s: inact %u", __func__, ni->ni_inact);
131 sta_drop(void *arg, struct ieee80211_node *ni)
133 struct ieee80211vap *vap = arg;
135 if (ni->ni_vap == vap && ni->ni_associd != 0)
136 ieee80211_node_leave(ni);
140 * Does a channel change require associated stations to re-associate
141 * so protocol state is correct. This is used when doing CSA across
142 * bands or similar (e.g. HT -> legacy).
145 isbandchange(struct ieee80211com *ic)
147 return ((ic->ic_bsschan->ic_flags ^ ic->ic_csa_newchan->ic_flags) &
148 (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_HALF |
149 IEEE80211_CHAN_QUARTER | IEEE80211_CHAN_HT)) != 0;
153 * IEEE80211_M_HOSTAP vap state machine handler.
156 hostap_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
158 struct ieee80211com *ic = vap->iv_ic;
159 enum ieee80211_state ostate;
161 IEEE80211_LOCK_ASSERT(ic);
163 ostate = vap->iv_state;
164 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
165 __func__, ieee80211_state_name[ostate],
166 ieee80211_state_name[nstate], arg);
167 vap->iv_state = nstate; /* state transition */
168 if (ostate != IEEE80211_S_SCAN)
169 ieee80211_cancel_scan(vap); /* background scan */
171 case IEEE80211_S_INIT:
173 case IEEE80211_S_SCAN:
174 ieee80211_cancel_scan(vap);
176 case IEEE80211_S_CAC:
177 ieee80211_dfs_cac_stop(vap);
179 case IEEE80211_S_RUN:
180 ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
185 if (ostate != IEEE80211_S_INIT) {
186 /* NB: optimize INIT -> INIT case */
187 ieee80211_reset_bss(vap);
189 if (vap->iv_auth->ia_detach != NULL)
190 vap->iv_auth->ia_detach(vap);
192 case IEEE80211_S_SCAN:
194 case IEEE80211_S_CSA:
195 case IEEE80211_S_RUN:
196 ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
198 * Clear overlapping BSS state; the beacon frame
199 * will be reconstructed on transition to the RUN
200 * state and the timeout routines check if the flag
201 * is set before doing anything so this is sufficient.
203 ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
204 ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
206 case IEEE80211_S_CAC:
208 * NB: We may get here because of a manual channel
209 * change in which case we need to stop CAC
210 * XXX no need to stop if ostate RUN but it's ok
212 ieee80211_dfs_cac_stop(vap);
214 case IEEE80211_S_INIT:
215 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
216 !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
218 * Already have a channel; bypass the
219 * scan and startup immediately.
220 * ieee80211_create_ibss will call back to
221 * move us to RUN state.
223 ieee80211_create_ibss(vap, vap->iv_des_chan);
227 * Initiate a scan. We can come here as a result
228 * of an IEEE80211_IOC_SCAN_REQ too in which case
229 * the vap will be marked with IEEE80211_FEXT_SCANREQ
230 * and the scan request parameters will be present
231 * in iv_scanreq. Otherwise we do the default.
233 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
234 ieee80211_check_scan(vap,
235 vap->iv_scanreq_flags,
236 vap->iv_scanreq_duration,
237 vap->iv_scanreq_mindwell,
238 vap->iv_scanreq_maxdwell,
239 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
240 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
242 ieee80211_check_scan_current(vap);
244 case IEEE80211_S_SCAN:
246 * A state change requires a reset; scan.
248 ieee80211_check_scan_current(vap);
254 case IEEE80211_S_CAC:
256 * Start CAC on a DFS channel. We come here when starting
257 * a bss on a DFS channel (see ieee80211_create_ibss).
259 ieee80211_dfs_cac_start(vap);
261 case IEEE80211_S_RUN:
262 if (vap->iv_flags & IEEE80211_F_WPA) {
263 /* XXX validate prerequisites */
266 case IEEE80211_S_INIT:
268 * Already have a channel; bypass the
269 * scan and startup immediately.
270 * Note that ieee80211_create_ibss will call
271 * back to do a RUN->RUN state change.
273 ieee80211_create_ibss(vap,
274 ieee80211_ht_adjust_channel(ic,
275 ic->ic_curchan, vap->iv_flags_ht));
276 /* NB: iv_bss is changed on return */
278 case IEEE80211_S_CAC:
280 * NB: This is the normal state change when CAC
281 * expires and no radar was detected; no need to
282 * clear the CAC timer as it's already expired.
285 case IEEE80211_S_CSA:
287 * Shorten inactivity timer of associated stations
288 * to weed out sta's that don't follow a CSA.
290 ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
292 * Update bss node channel to reflect where
293 * we landed after CSA.
295 ieee80211_node_set_chan(vap->iv_bss,
296 ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
297 ieee80211_htchanflags(vap->iv_bss->ni_chan)));
298 /* XXX bypass debug msgs */
300 case IEEE80211_S_SCAN:
301 case IEEE80211_S_RUN:
302 #ifdef IEEE80211_DEBUG
303 if (ieee80211_msg_debug(vap)) {
304 struct ieee80211_node *ni = vap->iv_bss;
306 "synchronized with %s ssid ",
307 ether_sprintf(ni->ni_bssid));
308 ieee80211_print_essid(ni->ni_essid,
311 printf(" channel %d start %uMb\n",
312 ieee80211_chan2ieee(ic, ic->ic_curchan),
313 IEEE80211_RATE2MBS(ni->ni_txrate));
321 * Start/stop the authenticator. We delay until here
322 * to allow configuration to happen out of order.
324 if (vap->iv_auth->ia_attach != NULL) {
325 /* XXX check failure */
326 vap->iv_auth->ia_attach(vap);
327 } else if (vap->iv_auth->ia_detach != NULL) {
328 vap->iv_auth->ia_detach(vap);
330 ieee80211_node_authorize(vap->iv_bss);
332 case IEEE80211_S_CSA:
333 if (ostate == IEEE80211_S_RUN && isbandchange(ic)) {
335 * On a ``band change'' silently drop associated
336 * stations as they must re-associate before they
337 * can pass traffic (as otherwise protocol state
338 * such as capabilities and the negotiated rate
339 * set may/will be wrong).
341 ieee80211_iterate_nodes(&ic->ic_sta, sta_drop, vap);
351 hostap_deliver_data(struct ieee80211vap *vap,
352 struct ieee80211_node *ni, struct mbuf *m)
354 struct ether_header *eh = mtod(m, struct ether_header *);
355 struct ifnet *ifp = vap->iv_ifp;
357 /* clear driver/net80211 flags before passing up */
358 m->m_flags &= ~(M_MCAST | M_BCAST);
361 KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
362 ("gack, opmode %d", vap->iv_opmode));
367 IEEE80211_NODE_STAT(ni, rx_data);
368 IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
369 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
370 m->m_flags |= M_MCAST; /* XXX M_BCAST? */
371 IEEE80211_NODE_STAT(ni, rx_mcast);
373 IEEE80211_NODE_STAT(ni, rx_ucast);
375 /* perform as a bridge within the AP */
376 if ((vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0) {
377 struct mbuf *mcopy = NULL;
379 if (m->m_flags & M_MCAST) {
380 mcopy = m_dup(m, M_NOWAIT);
384 mcopy->m_flags |= M_MCAST;
387 * Check if the destination is associated with the
388 * same vap and authorized to receive traffic.
389 * Beware of traffic destined for the vap itself;
390 * sending it will not work; just let it be delivered
393 struct ieee80211_node *sta = ieee80211_find_vap_node(
394 &vap->iv_ic->ic_sta, vap, eh->ether_dhost);
396 if (ieee80211_node_is_authorized(sta)) {
398 * Beware of sending to ourself; this
399 * needs to happen via the normal
402 if (sta != vap->iv_bss) {
407 vap->iv_stats.is_rx_unauth++;
408 IEEE80211_NODE_STAT(sta, rx_unauth);
410 ieee80211_free_node(sta);
415 len = mcopy->m_pkthdr.len;
416 err = ieee80211_vap_xmitpkt(vap, mcopy);
418 /* NB: IFQ_HANDOFF reclaims mcopy */
426 * Mark frame as coming from vap's interface.
428 m->m_pkthdr.rcvif = ifp;
429 if (m->m_flags & M_MCAST) {
431 * Spam DWDS vap's w/ multicast traffic.
433 /* XXX only if dwds in use? */
434 ieee80211_dwds_mcast(vap, m);
436 if (ni->ni_vlan != 0) {
437 /* attach vlan tag */
438 m->m_pkthdr.ether_vtag = ni->ni_vlan;
439 m->m_flags |= M_VLANTAG;
441 ifp->if_input(ifp, m);
446 * Decide if a received management frame should be
447 * printed when debugging is enabled. This filters some
448 * of the less interesting frames that come frequently
452 doprint(struct ieee80211vap *vap, int subtype)
455 case IEEE80211_FC0_SUBTYPE_BEACON:
456 return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
457 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
464 * Process a received frame. The node associated with the sender
465 * should be supplied. If nothing was found in the node table then
466 * the caller is assumed to supply a reference to iv_bss instead.
467 * The RSSI and a timestamp are also supplied. The RSSI data is used
468 * during AP scanning to select a AP to associate with; it can have
469 * any units so long as values have consistent units and higher values
470 * mean ``better signal''. The receive timestamp is currently not used
471 * by the 802.11 layer.
474 hostap_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
476 #define HAS_SEQ(type) ((type & 0x4) == 0)
477 struct ieee80211vap *vap = ni->ni_vap;
478 struct ieee80211com *ic = ni->ni_ic;
479 struct ifnet *ifp = vap->iv_ifp;
480 struct ieee80211_frame *wh;
481 struct ieee80211_key *key;
482 struct ether_header *eh;
483 int hdrspace, need_tap = 1; /* mbuf need to be tapped. */
484 uint8_t dir, type, subtype, qos;
488 if (m->m_flags & M_AMPDU_MPDU) {
490 * Fastpath for A-MPDU reorder q resubmission. Frames
491 * w/ M_AMPDU_MPDU marked have already passed through
492 * here but were received out of order and been held on
493 * the reorder queue. When resubmitted they are marked
494 * with the M_AMPDU_MPDU flag and we can bypass most of
495 * the normal processing.
497 wh = mtod(m, struct ieee80211_frame *);
498 type = IEEE80211_FC0_TYPE_DATA;
499 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
500 subtype = IEEE80211_FC0_SUBTYPE_QOS;
501 hdrspace = ieee80211_hdrspace(ic, wh); /* XXX optimize? */
505 KASSERT(ni != NULL, ("null node"));
506 ni->ni_inact = ni->ni_inact_reload;
508 type = -1; /* undefined */
510 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
511 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
512 ni->ni_macaddr, NULL,
513 "too short (1): len %u", m->m_pkthdr.len);
514 vap->iv_stats.is_rx_tooshort++;
518 * Bit of a cheat here, we use a pointer for a 3-address
519 * frame format but don't reference fields past outside
520 * ieee80211_frame_min w/o first validating the data is
523 wh = mtod(m, struct ieee80211_frame *);
525 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
526 IEEE80211_FC0_VERSION_0) {
527 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
528 ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
529 wh->i_fc[0], wh->i_fc[1]);
530 vap->iv_stats.is_rx_badversion++;
534 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
535 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
536 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
537 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
538 if (dir != IEEE80211_FC1_DIR_NODS)
540 else if (type == IEEE80211_FC0_TYPE_CTL)
543 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
544 IEEE80211_DISCARD_MAC(vap,
545 IEEE80211_MSG_ANY, ni->ni_macaddr,
546 NULL, "too short (2): len %u",
548 vap->iv_stats.is_rx_tooshort++;
554 * Validate the bssid.
556 if (!(type == IEEE80211_FC0_TYPE_MGT &&
557 subtype == IEEE80211_FC0_SUBTYPE_BEACON) &&
558 !IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
559 !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
560 /* not interested in */
561 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
562 bssid, NULL, "%s", "not to bss");
563 vap->iv_stats.is_rx_wrongbss++;
567 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
570 uint8_t tid = ieee80211_gettid(wh);
571 if (IEEE80211_QOS_HAS_SEQ(wh) &&
572 TID_TO_WME_AC(tid) >= WME_AC_VI)
573 ic->ic_wme.wme_hipri_traffic++;
574 rxseq = le16toh(*(uint16_t *)wh->i_seq);
575 if (! ieee80211_check_rxseq(ni, wh)) {
576 /* duplicate, discard */
577 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
579 "seqno <%u,%u> fragno <%u,%u> tid %u",
580 rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
581 ni->ni_rxseqs[tid] >>
582 IEEE80211_SEQ_SEQ_SHIFT,
583 rxseq & IEEE80211_SEQ_FRAG_MASK,
585 IEEE80211_SEQ_FRAG_MASK,
587 vap->iv_stats.is_rx_dup++;
588 IEEE80211_NODE_STAT(ni, rx_dup);
591 ni->ni_rxseqs[tid] = rxseq;
596 case IEEE80211_FC0_TYPE_DATA:
597 hdrspace = ieee80211_hdrspace(ic, wh);
598 if (m->m_len < hdrspace &&
599 (m = m_pullup(m, hdrspace)) == NULL) {
600 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
601 ni->ni_macaddr, NULL,
602 "data too short: expecting %u", hdrspace);
603 vap->iv_stats.is_rx_tooshort++;
606 if (!(dir == IEEE80211_FC1_DIR_TODS ||
607 (dir == IEEE80211_FC1_DIR_DSTODS &&
608 (vap->iv_flags & IEEE80211_F_DWDS)))) {
609 if (dir != IEEE80211_FC1_DIR_DSTODS) {
610 IEEE80211_DISCARD(vap,
611 IEEE80211_MSG_INPUT, wh, "data",
612 "incorrect dir 0x%x", dir);
614 IEEE80211_DISCARD(vap,
615 IEEE80211_MSG_INPUT |
616 IEEE80211_MSG_WDS, wh,
618 "%s", "DWDS not enabled");
620 vap->iv_stats.is_rx_wrongdir++;
623 /* check if source STA is associated */
624 if (ni == vap->iv_bss) {
625 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
626 wh, "data", "%s", "unknown src");
627 ieee80211_send_error(ni, wh->i_addr2,
628 IEEE80211_FC0_SUBTYPE_DEAUTH,
629 IEEE80211_REASON_NOT_AUTHED);
630 vap->iv_stats.is_rx_notassoc++;
633 if (ni->ni_associd == 0) {
634 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
635 wh, "data", "%s", "unassoc src");
636 IEEE80211_SEND_MGMT(ni,
637 IEEE80211_FC0_SUBTYPE_DISASSOC,
638 IEEE80211_REASON_NOT_ASSOCED);
639 vap->iv_stats.is_rx_notassoc++;
644 * Check for power save state change.
645 * XXX out-of-order A-MPDU frames?
647 if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
648 (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
650 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
652 * For 4-address packets handle WDS discovery
653 * notifications. Once a WDS link is setup frames
654 * are just delivered to the WDS vap (see below).
656 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap == NULL) {
657 if (!ieee80211_node_is_authorized(ni)) {
658 IEEE80211_DISCARD(vap,
659 IEEE80211_MSG_INPUT |
660 IEEE80211_MSG_WDS, wh,
662 "%s", "unauthorized port");
663 vap->iv_stats.is_rx_unauth++;
664 IEEE80211_NODE_STAT(ni, rx_unauth);
667 ieee80211_dwds_discover(ni, m);
672 * Handle A-MPDU re-ordering. If the frame is to be
673 * processed directly then ieee80211_ampdu_reorder
674 * will return 0; otherwise it has consumed the mbuf
675 * and we should do nothing more with it.
677 if ((m->m_flags & M_AMPDU) &&
678 ieee80211_ampdu_reorder(ni, m) != 0) {
685 * Handle privacy requirements. Note that we
686 * must not be preempted from here until after
687 * we (potentially) call ieee80211_crypto_demic;
688 * otherwise we may violate assumptions in the
689 * crypto cipher modules used to do delayed update
690 * of replay sequence numbers.
692 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
693 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
695 * Discard encrypted frames when privacy is off.
697 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
698 wh, "WEP", "%s", "PRIVACY off");
699 vap->iv_stats.is_rx_noprivacy++;
700 IEEE80211_NODE_STAT(ni, rx_noprivacy);
703 key = ieee80211_crypto_decap(ni, m, hdrspace);
705 /* NB: stats+msgs handled in crypto_decap */
706 IEEE80211_NODE_STAT(ni, rx_wepfail);
709 wh = mtod(m, struct ieee80211_frame *);
710 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
712 /* XXX M_WEP and IEEE80211_F_PRIVACY */
717 * Save QoS bits for use below--before we strip the header.
719 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
720 qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
721 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
722 ((struct ieee80211_qosframe *)wh)->i_qos[0];
727 * Next up, any fragmentation.
729 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
730 m = ieee80211_defrag(ni, m, hdrspace);
732 /* Fragment dropped or frame not complete yet */
736 wh = NULL; /* no longer valid, catch any uses */
739 * Next strip any MSDU crypto bits.
741 if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
742 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
743 ni->ni_macaddr, "data", "%s", "demic error");
744 vap->iv_stats.is_rx_demicfail++;
745 IEEE80211_NODE_STAT(ni, rx_demicfail);
748 /* copy to listener after decrypt */
749 if (ieee80211_radiotap_active_vap(vap))
750 ieee80211_radiotap_rx(vap, m);
753 * Finally, strip the 802.11 header.
755 m = ieee80211_decap(vap, m, hdrspace);
757 /* XXX mask bit to check for both */
758 /* don't count Null data frames as errors */
759 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
760 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
762 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
763 ni->ni_macaddr, "data", "%s", "decap error");
764 vap->iv_stats.is_rx_decap++;
765 IEEE80211_NODE_STAT(ni, rx_decap);
768 eh = mtod(m, struct ether_header *);
769 if (!ieee80211_node_is_authorized(ni)) {
771 * Deny any non-PAE frames received prior to
772 * authorization. For open/shared-key
773 * authentication the port is mark authorized
774 * after authentication completes. For 802.1x
775 * the port is not marked authorized by the
776 * authenticator until the handshake has completed.
778 if (eh->ether_type != htons(ETHERTYPE_PAE)) {
779 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
780 eh->ether_shost, "data",
781 "unauthorized port: ether type 0x%x len %u",
782 eh->ether_type, m->m_pkthdr.len);
783 vap->iv_stats.is_rx_unauth++;
784 IEEE80211_NODE_STAT(ni, rx_unauth);
789 * When denying unencrypted frames, discard
790 * any non-PAE frames received without encryption.
792 if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
793 (key == NULL && (m->m_flags & M_WEP) == 0) &&
794 eh->ether_type != htons(ETHERTYPE_PAE)) {
796 * Drop unencrypted frames.
798 vap->iv_stats.is_rx_unencrypted++;
799 IEEE80211_NODE_STAT(ni, rx_unencrypted);
803 /* XXX require HT? */
804 if (qos & IEEE80211_QOS_AMSDU) {
805 m = ieee80211_decap_amsdu(ni, m);
807 return IEEE80211_FC0_TYPE_DATA;
809 #ifdef IEEE80211_SUPPORT_SUPERG
810 m = ieee80211_decap_fastframe(vap, ni, m);
812 return IEEE80211_FC0_TYPE_DATA;
815 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
816 ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
818 hostap_deliver_data(vap, ni, m);
819 return IEEE80211_FC0_TYPE_DATA;
821 case IEEE80211_FC0_TYPE_MGT:
822 vap->iv_stats.is_rx_mgmt++;
823 IEEE80211_NODE_STAT(ni, rx_mgmt);
824 if (dir != IEEE80211_FC1_DIR_NODS) {
825 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
826 wh, "mgt", "incorrect dir 0x%x", dir);
827 vap->iv_stats.is_rx_wrongdir++;
830 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
831 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
832 ni->ni_macaddr, "mgt", "too short: len %u",
834 vap->iv_stats.is_rx_tooshort++;
837 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
838 /* ensure return frames are unicast */
839 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
840 wh, NULL, "source is multicast: %s",
841 ether_sprintf(wh->i_addr2));
842 vap->iv_stats.is_rx_mgtdiscard++; /* XXX stat */
845 #ifdef IEEE80211_DEBUG
846 if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
847 ieee80211_msg_dumppkts(vap)) {
848 if_printf(ifp, "received %s from %s rssi %d\n",
849 ieee80211_mgt_subtype_name[subtype >>
850 IEEE80211_FC0_SUBTYPE_SHIFT],
851 ether_sprintf(wh->i_addr2), rssi);
854 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
855 if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
857 * Only shared key auth frames with a challenge
858 * should be encrypted, discard all others.
860 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
862 "%s", "WEP set but not permitted");
863 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
866 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
868 * Discard encrypted frames when privacy is off.
870 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
871 wh, NULL, "%s", "WEP set but PRIVACY off");
872 vap->iv_stats.is_rx_noprivacy++;
875 hdrspace = ieee80211_hdrspace(ic, wh);
876 key = ieee80211_crypto_decap(ni, m, hdrspace);
878 /* NB: stats+msgs handled in crypto_decap */
881 wh = mtod(m, struct ieee80211_frame *);
882 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
885 * Pass the packet to radiotap before calling iv_recv_mgmt().
886 * Otherwise iv_recv_mgmt() might pass another packet to
887 * radiotap, resulting in out of order packet captures.
889 if (ieee80211_radiotap_active_vap(vap))
890 ieee80211_radiotap_rx(vap, m);
892 vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
895 case IEEE80211_FC0_TYPE_CTL:
896 vap->iv_stats.is_rx_ctl++;
897 IEEE80211_NODE_STAT(ni, rx_ctrl);
898 vap->iv_recv_ctl(ni, m, subtype);
901 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
902 wh, "bad", "frame type 0x%x", type);
903 /* should not come here */
910 if (need_tap && ieee80211_radiotap_active_vap(vap))
911 ieee80211_radiotap_rx(vap, m);
918 hostap_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
919 int rssi, int nf, uint16_t seq, uint16_t status)
921 struct ieee80211vap *vap = ni->ni_vap;
923 KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
925 if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
926 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
927 ni->ni_macaddr, "open auth",
928 "bad sta auth mode %u", ni->ni_authmode);
929 vap->iv_stats.is_rx_bad_auth++; /* XXX */
931 * Clear any challenge text that may be there if
932 * a previous shared key auth failed and then an
933 * open auth is attempted.
935 if (ni->ni_challenge != NULL) {
936 free(ni->ni_challenge, M_80211_NODE);
937 ni->ni_challenge = NULL;
939 /* XXX hack to workaround calling convention */
940 ieee80211_send_error(ni, wh->i_addr2,
941 IEEE80211_FC0_SUBTYPE_AUTH,
942 (seq + 1) | (IEEE80211_STATUS_ALG<<16));
945 if (seq != IEEE80211_AUTH_OPEN_REQUEST) {
946 vap->iv_stats.is_rx_bad_auth++;
949 /* always accept open authentication requests */
950 if (ni == vap->iv_bss) {
951 ni = ieee80211_dup_bss(vap, wh->i_addr2);
954 } else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
955 (void) ieee80211_ref_node(ni);
957 * Mark the node as referenced to reflect that it's
958 * reference count has been bumped to insure it remains
959 * after the transaction completes.
961 ni->ni_flags |= IEEE80211_NODE_AREF;
963 * Mark the node as requiring a valid association id
964 * before outbound traffic is permitted.
966 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
968 if (vap->iv_acl != NULL &&
969 vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
971 * When the ACL policy is set to RADIUS we defer the
972 * authorization to a user agent. Dispatch an event,
973 * a subsequent MLME call will decide the fate of the
974 * station. If the user agent is not present then the
975 * node will be reclaimed due to inactivity.
977 IEEE80211_NOTE_MAC(vap,
978 IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL, ni->ni_macaddr,
979 "%s", "station authentication defered (radius acl)");
980 ieee80211_notify_node_auth(ni);
982 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
983 IEEE80211_NOTE_MAC(vap,
984 IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni->ni_macaddr,
985 "%s", "station authenticated (open)");
987 * When 802.1x is not in use mark the port
988 * authorized at this point so traffic can flow.
990 if (ni->ni_authmode != IEEE80211_AUTH_8021X)
991 ieee80211_node_authorize(ni);
996 hostap_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
997 uint8_t *frm, uint8_t *efrm, int rssi, int nf,
998 uint16_t seq, uint16_t status)
1000 struct ieee80211vap *vap = ni->ni_vap;
1002 int allocbs, estatus;
1004 KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
1007 * NB: this can happen as we allow pre-shared key
1008 * authentication to be enabled w/o wep being turned
1009 * on so that configuration of these can be done
1010 * in any order. It may be better to enforce the
1011 * ordering in which case this check would just be
1012 * for sanity/consistency.
1014 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
1015 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1016 ni->ni_macaddr, "shared key auth",
1017 "%s", " PRIVACY is disabled");
1018 estatus = IEEE80211_STATUS_ALG;
1022 * Pre-shared key authentication is evil; accept
1023 * it only if explicitly configured (it is supported
1024 * mainly for compatibility with clients like Mac OS X).
1026 if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1027 ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1028 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1029 ni->ni_macaddr, "shared key auth",
1030 "bad sta auth mode %u", ni->ni_authmode);
1031 vap->iv_stats.is_rx_bad_auth++; /* XXX maybe a unique error? */
1032 estatus = IEEE80211_STATUS_ALG;
1037 if (frm + 1 < efrm) {
1038 if ((frm[1] + 2) > (efrm - frm)) {
1039 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1040 ni->ni_macaddr, "shared key auth",
1041 "ie %d/%d too long",
1042 frm[0], (frm[1] + 2) - (efrm - frm));
1043 vap->iv_stats.is_rx_bad_auth++;
1044 estatus = IEEE80211_STATUS_CHALLENGE;
1047 if (*frm == IEEE80211_ELEMID_CHALLENGE)
1052 case IEEE80211_AUTH_SHARED_CHALLENGE:
1053 case IEEE80211_AUTH_SHARED_RESPONSE:
1054 if (challenge == NULL) {
1055 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1056 ni->ni_macaddr, "shared key auth",
1057 "%s", "no challenge");
1058 vap->iv_stats.is_rx_bad_auth++;
1059 estatus = IEEE80211_STATUS_CHALLENGE;
1062 if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1063 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1064 ni->ni_macaddr, "shared key auth",
1065 "bad challenge len %d", challenge[1]);
1066 vap->iv_stats.is_rx_bad_auth++;
1067 estatus = IEEE80211_STATUS_CHALLENGE;
1074 case IEEE80211_AUTH_SHARED_REQUEST:
1075 if (ni == vap->iv_bss) {
1076 ni = ieee80211_dup_bss(vap, wh->i_addr2);
1078 /* NB: no way to return an error */
1083 if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1084 (void) ieee80211_ref_node(ni);
1088 * Mark the node as referenced to reflect that it's
1089 * reference count has been bumped to insure it remains
1090 * after the transaction completes.
1092 ni->ni_flags |= IEEE80211_NODE_AREF;
1094 * Mark the node as requiring a valid associatio id
1095 * before outbound traffic is permitted.
1097 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
1098 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1100 if (!ieee80211_alloc_challenge(ni)) {
1101 /* NB: don't return error so they rexmit */
1104 get_random_bytes(ni->ni_challenge,
1105 IEEE80211_CHALLENGE_LEN);
1106 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1107 ni, "shared key %sauth request", allocbs ? "" : "re");
1109 * When the ACL policy is set to RADIUS we defer the
1110 * authorization to a user agent. Dispatch an event,
1111 * a subsequent MLME call will decide the fate of the
1112 * station. If the user agent is not present then the
1113 * node will be reclaimed due to inactivity.
1115 if (vap->iv_acl != NULL &&
1116 vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
1117 IEEE80211_NOTE_MAC(vap,
1118 IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL,
1120 "%s", "station authentication defered (radius acl)");
1121 ieee80211_notify_node_auth(ni);
1125 case IEEE80211_AUTH_SHARED_RESPONSE:
1126 if (ni == vap->iv_bss) {
1127 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1128 ni->ni_macaddr, "shared key response",
1129 "%s", "unknown station");
1130 /* NB: don't send a response */
1133 if (ni->ni_challenge == NULL) {
1134 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1135 ni->ni_macaddr, "shared key response",
1136 "%s", "no challenge recorded");
1137 vap->iv_stats.is_rx_bad_auth++;
1138 estatus = IEEE80211_STATUS_CHALLENGE;
1141 if (memcmp(ni->ni_challenge, &challenge[2],
1142 challenge[1]) != 0) {
1143 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1144 ni->ni_macaddr, "shared key response",
1145 "%s", "challenge mismatch");
1146 vap->iv_stats.is_rx_auth_fail++;
1147 estatus = IEEE80211_STATUS_CHALLENGE;
1150 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1151 ni, "%s", "station authenticated (shared key)");
1152 ieee80211_node_authorize(ni);
1155 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1156 ni->ni_macaddr, "shared key auth",
1158 vap->iv_stats.is_rx_bad_auth++;
1159 estatus = IEEE80211_STATUS_SEQUENCE;
1162 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1166 * Send an error response; but only when operating as an AP.
1168 /* XXX hack to workaround calling convention */
1169 ieee80211_send_error(ni, wh->i_addr2,
1170 IEEE80211_FC0_SUBTYPE_AUTH,
1171 (seq + 1) | (estatus<<16));
1175 * Convert a WPA cipher selector OUI to an internal
1176 * cipher algorithm. Where appropriate we also
1177 * record any key length.
1180 wpa_cipher(const uint8_t *sel, uint8_t *keylen)
1182 #define WPA_SEL(x) (((x)<<24)|WPA_OUI)
1183 uint32_t w = LE_READ_4(sel);
1186 case WPA_SEL(WPA_CSE_NULL):
1187 return IEEE80211_CIPHER_NONE;
1188 case WPA_SEL(WPA_CSE_WEP40):
1190 *keylen = 40 / NBBY;
1191 return IEEE80211_CIPHER_WEP;
1192 case WPA_SEL(WPA_CSE_WEP104):
1194 *keylen = 104 / NBBY;
1195 return IEEE80211_CIPHER_WEP;
1196 case WPA_SEL(WPA_CSE_TKIP):
1197 return IEEE80211_CIPHER_TKIP;
1198 case WPA_SEL(WPA_CSE_CCMP):
1199 return IEEE80211_CIPHER_AES_CCM;
1201 return 32; /* NB: so 1<< is discarded */
1206 * Convert a WPA key management/authentication algorithm
1207 * to an internal code.
1210 wpa_keymgmt(const uint8_t *sel)
1212 #define WPA_SEL(x) (((x)<<24)|WPA_OUI)
1213 uint32_t w = LE_READ_4(sel);
1216 case WPA_SEL(WPA_ASE_8021X_UNSPEC):
1217 return WPA_ASE_8021X_UNSPEC;
1218 case WPA_SEL(WPA_ASE_8021X_PSK):
1219 return WPA_ASE_8021X_PSK;
1220 case WPA_SEL(WPA_ASE_NONE):
1221 return WPA_ASE_NONE;
1223 return 0; /* NB: so is discarded */
1228 * Parse a WPA information element to collect parameters.
1229 * Note that we do not validate security parameters; that
1230 * is handled by the authenticator; the parsing done here
1231 * is just for internal use in making operational decisions.
1234 ieee80211_parse_wpa(struct ieee80211vap *vap, const uint8_t *frm,
1235 struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1237 uint8_t len = frm[1];
1242 * Check the length once for fixed parts: OUI, type,
1243 * version, mcast cipher, and 2 selector counts.
1244 * Other, variable-length data, must be checked separately.
1246 if ((vap->iv_flags & IEEE80211_F_WPA1) == 0) {
1247 IEEE80211_DISCARD_IE(vap,
1248 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1249 wh, "WPA", "not WPA, flags 0x%x", vap->iv_flags);
1250 return IEEE80211_REASON_IE_INVALID;
1253 IEEE80211_DISCARD_IE(vap,
1254 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1255 wh, "WPA", "too short, len %u", len);
1256 return IEEE80211_REASON_IE_INVALID;
1258 frm += 6, len -= 4; /* NB: len is payload only */
1259 /* NB: iswpaoui already validated the OUI and type */
1261 if (w != WPA_VERSION) {
1262 IEEE80211_DISCARD_IE(vap,
1263 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1264 wh, "WPA", "bad version %u", w);
1265 return IEEE80211_REASON_IE_INVALID;
1269 memset(rsn, 0, sizeof(*rsn));
1271 /* multicast/group cipher */
1272 rsn->rsn_mcastcipher = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
1275 /* unicast ciphers */
1279 IEEE80211_DISCARD_IE(vap,
1280 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1281 wh, "WPA", "ucast cipher data too short; len %u, n %u",
1283 return IEEE80211_REASON_IE_INVALID;
1286 for (; n > 0; n--) {
1287 w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
1290 if (w & (1<<IEEE80211_CIPHER_TKIP))
1291 rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1293 rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1295 /* key management algorithms */
1299 IEEE80211_DISCARD_IE(vap,
1300 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1301 wh, "WPA", "key mgmt alg data too short; len %u, n %u",
1303 return IEEE80211_REASON_IE_INVALID;
1306 for (; n > 0; n--) {
1307 w |= wpa_keymgmt(frm);
1310 if (w & WPA_ASE_8021X_UNSPEC)
1311 rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
1313 rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1315 if (len > 2) /* optional capabilities */
1316 rsn->rsn_caps = LE_READ_2(frm);
1322 * Convert an RSN cipher selector OUI to an internal
1323 * cipher algorithm. Where appropriate we also
1324 * record any key length.
1327 rsn_cipher(const uint8_t *sel, uint8_t *keylen)
1329 #define RSN_SEL(x) (((x)<<24)|RSN_OUI)
1330 uint32_t w = LE_READ_4(sel);
1333 case RSN_SEL(RSN_CSE_NULL):
1334 return IEEE80211_CIPHER_NONE;
1335 case RSN_SEL(RSN_CSE_WEP40):
1337 *keylen = 40 / NBBY;
1338 return IEEE80211_CIPHER_WEP;
1339 case RSN_SEL(RSN_CSE_WEP104):
1341 *keylen = 104 / NBBY;
1342 return IEEE80211_CIPHER_WEP;
1343 case RSN_SEL(RSN_CSE_TKIP):
1344 return IEEE80211_CIPHER_TKIP;
1345 case RSN_SEL(RSN_CSE_CCMP):
1346 return IEEE80211_CIPHER_AES_CCM;
1347 case RSN_SEL(RSN_CSE_WRAP):
1348 return IEEE80211_CIPHER_AES_OCB;
1350 return 32; /* NB: so 1<< is discarded */
1355 * Convert an RSN key management/authentication algorithm
1356 * to an internal code.
1359 rsn_keymgmt(const uint8_t *sel)
1361 #define RSN_SEL(x) (((x)<<24)|RSN_OUI)
1362 uint32_t w = LE_READ_4(sel);
1365 case RSN_SEL(RSN_ASE_8021X_UNSPEC):
1366 return RSN_ASE_8021X_UNSPEC;
1367 case RSN_SEL(RSN_ASE_8021X_PSK):
1368 return RSN_ASE_8021X_PSK;
1369 case RSN_SEL(RSN_ASE_NONE):
1370 return RSN_ASE_NONE;
1372 return 0; /* NB: so is discarded */
1377 * Parse a WPA/RSN information element to collect parameters
1378 * and validate the parameters against what has been
1379 * configured for the system.
1382 ieee80211_parse_rsn(struct ieee80211vap *vap, const uint8_t *frm,
1383 struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1385 uint8_t len = frm[1];
1390 * Check the length once for fixed parts:
1391 * version, mcast cipher, and 2 selector counts.
1392 * Other, variable-length data, must be checked separately.
1394 if ((vap->iv_flags & IEEE80211_F_WPA2) == 0) {
1395 IEEE80211_DISCARD_IE(vap,
1396 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1397 wh, "WPA", "not RSN, flags 0x%x", vap->iv_flags);
1398 return IEEE80211_REASON_IE_INVALID;
1401 IEEE80211_DISCARD_IE(vap,
1402 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1403 wh, "RSN", "too short, len %u", len);
1404 return IEEE80211_REASON_IE_INVALID;
1408 if (w != RSN_VERSION) {
1409 IEEE80211_DISCARD_IE(vap,
1410 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1411 wh, "RSN", "bad version %u", w);
1412 return IEEE80211_REASON_IE_INVALID;
1416 memset(rsn, 0, sizeof(*rsn));
1418 /* multicast/group cipher */
1419 rsn->rsn_mcastcipher = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
1422 /* unicast ciphers */
1426 IEEE80211_DISCARD_IE(vap,
1427 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1428 wh, "RSN", "ucast cipher data too short; len %u, n %u",
1430 return IEEE80211_REASON_IE_INVALID;
1433 for (; n > 0; n--) {
1434 w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
1437 if (w & (1<<IEEE80211_CIPHER_TKIP))
1438 rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1440 rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1442 /* key management algorithms */
1446 IEEE80211_DISCARD_IE(vap,
1447 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1448 wh, "RSN", "key mgmt alg data too short; len %u, n %u",
1450 return IEEE80211_REASON_IE_INVALID;
1453 for (; n > 0; n--) {
1454 w |= rsn_keymgmt(frm);
1457 if (w & RSN_ASE_8021X_UNSPEC)
1458 rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
1460 rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
1462 /* optional RSN capabilities */
1464 rsn->rsn_caps = LE_READ_2(frm);
1471 * WPA/802.11i assocation request processing.
1474 wpa_assocreq(struct ieee80211_node *ni, struct ieee80211_rsnparms *rsnparms,
1475 const struct ieee80211_frame *wh, const uint8_t *wpa,
1476 const uint8_t *rsn, uint16_t capinfo)
1478 struct ieee80211vap *vap = ni->ni_vap;
1482 ni->ni_flags &= ~(IEEE80211_NODE_WPS|IEEE80211_NODE_TSN);
1483 if (wpa == NULL && rsn == NULL) {
1484 if (vap->iv_flags_ext & IEEE80211_FEXT_WPS) {
1486 * W-Fi Protected Setup (WPS) permits
1487 * clients to associate and pass EAPOL frames
1488 * to establish initial credentials.
1490 ni->ni_flags |= IEEE80211_NODE_WPS;
1493 if ((vap->iv_flags_ext & IEEE80211_FEXT_TSN) &&
1494 (capinfo & IEEE80211_CAPINFO_PRIVACY)) {
1496 * Transitional Security Network. Permits clients
1497 * to associate and use WEP while WPA is configured.
1499 ni->ni_flags |= IEEE80211_NODE_TSN;
1502 IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1503 wh, NULL, "%s", "no WPA/RSN IE in association request");
1504 vap->iv_stats.is_rx_assoc_badwpaie++;
1505 reason = IEEE80211_REASON_IE_INVALID;
1508 /* assert right association security credentials */
1509 badwparsn = 0; /* NB: to silence compiler */
1510 switch (vap->iv_flags & IEEE80211_F_WPA) {
1511 case IEEE80211_F_WPA1:
1512 badwparsn = (wpa == NULL);
1514 case IEEE80211_F_WPA2:
1515 badwparsn = (rsn == NULL);
1517 case IEEE80211_F_WPA1|IEEE80211_F_WPA2:
1518 badwparsn = (wpa == NULL && rsn == NULL);
1522 IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1524 "%s", "missing WPA/RSN IE in association request");
1525 vap->iv_stats.is_rx_assoc_badwpaie++;
1526 reason = IEEE80211_REASON_IE_INVALID;
1530 * Parse WPA/RSN information element.
1533 reason = ieee80211_parse_wpa(vap, wpa, rsnparms, wh);
1535 reason = ieee80211_parse_rsn(vap, rsn, rsnparms, wh);
1537 /* XXX distinguish WPA/RSN? */
1538 vap->iv_stats.is_rx_assoc_badwpaie++;
1541 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA, ni,
1542 "%s ie: mc %u/%u uc %u/%u key %u caps 0x%x",
1543 wpa != NULL ? "WPA" : "RSN",
1544 rsnparms->rsn_mcastcipher, rsnparms->rsn_mcastkeylen,
1545 rsnparms->rsn_ucastcipher, rsnparms->rsn_ucastkeylen,
1546 rsnparms->rsn_keymgmt, rsnparms->rsn_caps);
1550 ieee80211_node_deauth(ni, reason);
1554 /* XXX find a better place for definition */
1555 struct l2_update_frame {
1556 struct ether_header eh;
1564 * Deliver a TGf L2UF frame on behalf of a station.
1565 * This primes any bridge when the station is roaming
1566 * between ap's on the same wired network.
1569 ieee80211_deliver_l2uf(struct ieee80211_node *ni)
1571 struct ieee80211vap *vap = ni->ni_vap;
1572 struct ifnet *ifp = vap->iv_ifp;
1574 struct l2_update_frame *l2uf;
1575 struct ether_header *eh;
1577 m = m_gethdr(M_NOWAIT, MT_DATA);
1579 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1580 "%s", "no mbuf for l2uf frame");
1581 vap->iv_stats.is_rx_nobuf++; /* XXX not right */
1584 l2uf = mtod(m, struct l2_update_frame *);
1586 /* dst: Broadcast address */
1587 IEEE80211_ADDR_COPY(eh->ether_dhost, ifp->if_broadcastaddr);
1588 /* src: associated STA */
1589 IEEE80211_ADDR_COPY(eh->ether_shost, ni->ni_macaddr);
1590 eh->ether_type = htons(sizeof(*l2uf) - sizeof(*eh));
1594 l2uf->control = 0xf5;
1595 l2uf->xid[0] = 0x81;
1596 l2uf->xid[1] = 0x80;
1597 l2uf->xid[2] = 0x00;
1599 m->m_pkthdr.len = m->m_len = sizeof(*l2uf);
1600 hostap_deliver_data(vap, ni, m);
1604 ratesetmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1605 int reassoc, int resp, const char *tag, int rate)
1607 IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1608 "deny %s request, %s rate set mismatch, rate/MCS %d",
1609 reassoc ? "reassoc" : "assoc", tag, rate & IEEE80211_RATE_VAL);
1610 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_BASIC_RATE);
1611 ieee80211_node_leave(ni);
1615 capinfomismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1616 int reassoc, int resp, const char *tag, int capinfo)
1618 struct ieee80211vap *vap = ni->ni_vap;
1620 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1621 "deny %s request, %s mismatch 0x%x",
1622 reassoc ? "reassoc" : "assoc", tag, capinfo);
1623 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_CAPINFO);
1624 ieee80211_node_leave(ni);
1625 vap->iv_stats.is_rx_assoc_capmismatch++;
1629 htcapmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1630 int reassoc, int resp)
1632 IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1633 "deny %s request, %s missing HT ie", reassoc ? "reassoc" : "assoc");
1634 /* XXX no better code */
1635 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_MISSING_HT_CAPS);
1636 ieee80211_node_leave(ni);
1640 authalgreject(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1641 int algo, int seq, int status)
1643 struct ieee80211vap *vap = ni->ni_vap;
1645 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1646 wh, NULL, "unsupported alg %d", algo);
1647 vap->iv_stats.is_rx_auth_unsupported++;
1648 ieee80211_send_error(ni, wh->i_addr2, IEEE80211_FC0_SUBTYPE_AUTH,
1649 seq | (status << 16));
1653 ishtmixed(const uint8_t *ie)
1655 const struct ieee80211_ie_htinfo *ht =
1656 (const struct ieee80211_ie_htinfo *) ie;
1657 return (ht->hi_byte2 & IEEE80211_HTINFO_OPMODE) ==
1658 IEEE80211_HTINFO_OPMODE_MIXED;
1662 is11bclient(const uint8_t *rates, const uint8_t *xrates)
1664 static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
1667 /* NB: the 11b clients we care about will not have xrates */
1668 if (xrates != NULL || rates == NULL)
1670 for (i = 0; i < rates[1]; i++) {
1671 int r = rates[2+i] & IEEE80211_RATE_VAL;
1672 if (r > 2*11 || ((1<<r) & brates) == 0)
1679 hostap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1680 int subtype, int rssi, int nf)
1682 struct ieee80211vap *vap = ni->ni_vap;
1683 struct ieee80211com *ic = ni->ni_ic;
1684 struct ieee80211_frame *wh;
1685 uint8_t *frm, *efrm, *sfrm;
1686 uint8_t *ssid, *rates, *xrates, *wpa, *rsn, *wme, *ath, *htcap;
1690 wh = mtod(m0, struct ieee80211_frame *);
1691 frm = (uint8_t *)&wh[1];
1692 efrm = mtod(m0, uint8_t *) + m0->m_len;
1694 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1695 case IEEE80211_FC0_SUBTYPE_BEACON: {
1696 struct ieee80211_scanparams scan;
1698 * We process beacon/probe response frames when scanning;
1699 * otherwise we check beacon frames for overlapping non-ERP
1700 * BSS in 11g and/or overlapping legacy BSS when in HT.
1702 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
1703 subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
1704 vap->iv_stats.is_rx_mgtdiscard++;
1707 /* NB: accept off-channel frames */
1708 if (ieee80211_parse_beacon(ni, m0, &scan) &~ IEEE80211_BPARSE_OFFCHAN)
1711 * Count frame now that we know it's to be processed.
1713 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1714 vap->iv_stats.is_rx_beacon++; /* XXX remove */
1715 IEEE80211_NODE_STAT(ni, rx_beacons);
1717 IEEE80211_NODE_STAT(ni, rx_proberesp);
1719 * If scanning, just pass information to the scan module.
1721 if (ic->ic_flags & IEEE80211_F_SCAN) {
1722 if (scan.status == 0 && /* NB: on channel */
1723 (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN)) {
1725 * Actively scanning a channel marked passive;
1726 * send a probe request now that we know there
1727 * is 802.11 traffic present.
1729 * XXX check if the beacon we recv'd gives
1730 * us what we need and suppress the probe req
1732 ieee80211_probe_curchan(vap, 1);
1733 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1735 ieee80211_add_scan(vap, &scan, wh, subtype, rssi, nf);
1739 * Check beacon for overlapping bss w/ non ERP stations.
1740 * If we detect one and protection is configured but not
1741 * enabled, enable it and start a timer that'll bring us
1742 * out if we stop seeing the bss.
1744 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1745 scan.status == 0 && /* NB: on-channel */
1746 ((scan.erp & 0x100) == 0 || /* NB: no ERP, 11b sta*/
1747 (scan.erp & IEEE80211_ERP_NON_ERP_PRESENT))) {
1748 ic->ic_lastnonerp = ticks;
1749 ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
1750 if (ic->ic_protmode != IEEE80211_PROT_NONE &&
1751 (ic->ic_flags & IEEE80211_F_USEPROT) == 0) {
1752 IEEE80211_NOTE_FRAME(vap,
1753 IEEE80211_MSG_ASSOC, wh,
1754 "non-ERP present on channel %d "
1755 "(saw erp 0x%x from channel %d), "
1756 "enable use of protection",
1757 ic->ic_curchan->ic_ieee,
1758 scan.erp, scan.chan);
1759 ic->ic_flags |= IEEE80211_F_USEPROT;
1760 ieee80211_notify_erp(ic);
1764 * Check beacon for non-HT station on HT channel
1765 * and update HT BSS occupancy as appropriate.
1767 if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) {
1768 if (scan.status & IEEE80211_BPARSE_OFFCHAN) {
1770 * Off control channel; only check frames
1771 * that come in the extension channel when
1772 * operating w/ HT40.
1774 if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
1776 if (scan.chan != ic->ic_curchan->ic_extieee)
1779 if (scan.htinfo == NULL) {
1780 ieee80211_htprot_update(ic,
1781 IEEE80211_HTINFO_OPMODE_PROTOPT |
1782 IEEE80211_HTINFO_NONHT_PRESENT);
1783 } else if (ishtmixed(scan.htinfo)) {
1784 /* XXX? take NONHT_PRESENT from beacon? */
1785 ieee80211_htprot_update(ic,
1786 IEEE80211_HTINFO_OPMODE_MIXED |
1787 IEEE80211_HTINFO_NONHT_PRESENT);
1793 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1794 if (vap->iv_state != IEEE80211_S_RUN) {
1795 vap->iv_stats.is_rx_mgtdiscard++;
1799 * Consult the ACL policy module if setup.
1801 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1802 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1803 wh, NULL, "%s", "disallowed by ACL");
1804 vap->iv_stats.is_rx_acl++;
1808 * prreq frame format
1810 * [tlv] supported rates
1811 * [tlv] extended supported rates
1813 ssid = rates = xrates = NULL;
1815 while (efrm - frm > 1) {
1816 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1818 case IEEE80211_ELEMID_SSID:
1821 case IEEE80211_ELEMID_RATES:
1824 case IEEE80211_ELEMID_XRATES:
1830 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1832 IEEE80211_VERIFY_ELEMENT(xrates,
1833 IEEE80211_RATE_MAXSIZE - rates[1], return);
1834 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1835 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
1836 if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
1837 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1839 "%s", "no ssid with ssid suppression enabled");
1840 vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
1844 /* XXX find a better class or define it's own */
1845 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1846 "%s", "recv probe req");
1848 * Some legacy 11b clients cannot hack a complete
1849 * probe response frame. When the request includes
1850 * only a bare-bones rate set, communicate this to
1851 * the transmit side.
1853 ieee80211_send_proberesp(vap, wh->i_addr2,
1854 is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
1857 case IEEE80211_FC0_SUBTYPE_AUTH: {
1858 uint16_t algo, seq, status;
1860 if (vap->iv_state != IEEE80211_S_RUN) {
1861 vap->iv_stats.is_rx_mgtdiscard++;
1864 if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1865 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1866 wh, NULL, "%s", "wrong bssid");
1867 vap->iv_stats.is_rx_wrongbss++; /*XXX unique stat?*/
1877 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1878 algo = le16toh(*(uint16_t *)frm);
1879 seq = le16toh(*(uint16_t *)(frm + 2));
1880 status = le16toh(*(uint16_t *)(frm + 4));
1881 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1882 "recv auth frame with algorithm %d seq %d", algo, seq);
1884 * Consult the ACL policy module if setup.
1886 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1887 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1888 wh, NULL, "%s", "disallowed by ACL");
1889 vap->iv_stats.is_rx_acl++;
1890 ieee80211_send_error(ni, wh->i_addr2,
1891 IEEE80211_FC0_SUBTYPE_AUTH,
1892 (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
1895 if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1896 IEEE80211_DISCARD(vap,
1897 IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1898 wh, NULL, "%s", "TKIP countermeasures enabled");
1899 vap->iv_stats.is_rx_auth_countermeasures++;
1900 ieee80211_send_error(ni, wh->i_addr2,
1901 IEEE80211_FC0_SUBTYPE_AUTH,
1902 IEEE80211_REASON_MIC_FAILURE);
1905 if (algo == IEEE80211_AUTH_ALG_SHARED)
1906 hostap_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1908 else if (algo == IEEE80211_AUTH_ALG_OPEN)
1909 hostap_auth_open(ni, wh, rssi, nf, seq, status);
1910 else if (algo == IEEE80211_AUTH_ALG_LEAP) {
1911 authalgreject(ni, wh, algo,
1912 seq+1, IEEE80211_STATUS_ALG);
1916 * We assume that an unknown algorithm is the result
1917 * of a decryption failure on a shared key auth frame;
1918 * return a status code appropriate for that instead
1919 * of IEEE80211_STATUS_ALG.
1921 * NB: a seq# of 4 is intentional; the decrypted
1922 * frame likely has a bogus seq value.
1924 authalgreject(ni, wh, algo,
1925 4, IEEE80211_STATUS_CHALLENGE);
1931 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1932 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
1933 uint16_t capinfo, lintval;
1934 struct ieee80211_rsnparms rsnparms;
1936 if (vap->iv_state != IEEE80211_S_RUN) {
1937 vap->iv_stats.is_rx_mgtdiscard++;
1940 if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1941 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1942 wh, NULL, "%s", "wrong bssid");
1943 vap->iv_stats.is_rx_assoc_bss++;
1946 if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1948 resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
1951 resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
1953 if (ni == vap->iv_bss) {
1954 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1955 "deny %s request, sta not authenticated",
1956 reassoc ? "reassoc" : "assoc");
1957 ieee80211_send_error(ni, wh->i_addr2,
1958 IEEE80211_FC0_SUBTYPE_DEAUTH,
1959 IEEE80211_REASON_ASSOC_NOT_AUTHED);
1960 vap->iv_stats.is_rx_assoc_notauth++;
1965 * asreq frame format
1966 * [2] capability information
1967 * [2] listen interval
1968 * [6*] current AP address (reassoc only)
1970 * [tlv] supported rates
1971 * [tlv] extended supported rates
1973 * [tlv] HT capabilities
1974 * [tlv] Atheros capabilities
1976 IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4), return);
1977 capinfo = le16toh(*(uint16_t *)frm); frm += 2;
1978 lintval = le16toh(*(uint16_t *)frm); frm += 2;
1980 frm += 6; /* ignore current AP info */
1981 ssid = rates = xrates = wpa = rsn = wme = ath = htcap = NULL;
1983 while (efrm - frm > 1) {
1984 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1986 case IEEE80211_ELEMID_SSID:
1989 case IEEE80211_ELEMID_RATES:
1992 case IEEE80211_ELEMID_XRATES:
1995 case IEEE80211_ELEMID_RSN:
1998 case IEEE80211_ELEMID_HTCAP:
2001 case IEEE80211_ELEMID_VENDOR:
2004 else if (iswmeinfo(frm))
2006 #ifdef IEEE80211_SUPPORT_SUPERG
2007 else if (isatherosoui(frm))
2010 else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
2011 if (ishtcapoui(frm) && htcap == NULL)
2018 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
2020 IEEE80211_VERIFY_ELEMENT(xrates,
2021 IEEE80211_RATE_MAXSIZE - rates[1], return);
2022 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2023 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
2024 if (htcap != NULL) {
2025 IEEE80211_VERIFY_LENGTH(htcap[1],
2026 htcap[0] == IEEE80211_ELEMID_VENDOR ?
2027 4 + sizeof(struct ieee80211_ie_htcap)-2 :
2028 sizeof(struct ieee80211_ie_htcap)-2,
2029 return); /* XXX just NULL out? */
2032 if ((vap->iv_flags & IEEE80211_F_WPA) &&
2033 !wpa_assocreq(ni, &rsnparms, wh, wpa, rsn, capinfo))
2035 /* discard challenge after association */
2036 if (ni->ni_challenge != NULL) {
2037 free(ni->ni_challenge, M_80211_NODE);
2038 ni->ni_challenge = NULL;
2040 /* NB: 802.11 spec says to ignore station's privacy bit */
2041 if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
2042 capinfomismatch(ni, wh, reassoc, resp,
2043 "capability", capinfo);
2047 * Disallow re-associate w/ invalid slot time setting.
2049 if (ni->ni_associd != 0 &&
2050 IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2051 ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
2052 capinfomismatch(ni, wh, reassoc, resp,
2053 "slot time", capinfo);
2056 rate = ieee80211_setup_rates(ni, rates, xrates,
2057 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
2058 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2059 if (rate & IEEE80211_RATE_BASIC) {
2060 ratesetmismatch(ni, wh, reassoc, resp, "legacy", rate);
2061 vap->iv_stats.is_rx_assoc_norate++;
2065 * If constrained to 11g-only stations reject an
2066 * 11b-only station. We cheat a bit here by looking
2067 * at the max negotiated xmit rate and assuming anyone
2068 * with a best rate <24Mb/s is an 11b station.
2070 if ((vap->iv_flags & IEEE80211_F_PUREG) && rate < 48) {
2071 ratesetmismatch(ni, wh, reassoc, resp, "11g", rate);
2072 vap->iv_stats.is_rx_assoc_norate++;
2076 * Do HT rate set handling and setup HT node state.
2078 ni->ni_chan = vap->iv_bss->ni_chan;
2079 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
2080 rate = ieee80211_setup_htrates(ni, htcap,
2081 IEEE80211_F_DOFMCS | IEEE80211_F_DONEGO |
2083 if (rate & IEEE80211_RATE_BASIC) {
2084 ratesetmismatch(ni, wh, reassoc, resp,
2086 vap->iv_stats.is_ht_assoc_norate++;
2089 ieee80211_ht_node_init(ni);
2090 ieee80211_ht_updatehtcap(ni, htcap);
2091 } else if (ni->ni_flags & IEEE80211_NODE_HT)
2092 ieee80211_ht_node_cleanup(ni);
2093 #ifdef IEEE80211_SUPPORT_SUPERG
2094 else if (ni->ni_ath_flags & IEEE80211_NODE_ATH)
2095 ieee80211_ff_node_cleanup(ni);
2098 * Allow AMPDU operation only with unencrypted traffic
2099 * or AES-CCM; the 11n spec only specifies these ciphers
2100 * so permitting any others is undefined and can lead
2101 * to interoperability problems.
2103 if ((ni->ni_flags & IEEE80211_NODE_HT) &&
2104 (((vap->iv_flags & IEEE80211_F_WPA) &&
2105 rsnparms.rsn_ucastcipher != IEEE80211_CIPHER_AES_CCM) ||
2106 (vap->iv_flags & (IEEE80211_F_WPA|IEEE80211_F_PRIVACY)) == IEEE80211_F_PRIVACY)) {
2108 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
2109 "disallow HT use because WEP or TKIP requested, "
2110 "capinfo 0x%x ucastcipher %d", capinfo,
2111 rsnparms.rsn_ucastcipher);
2112 ieee80211_ht_node_cleanup(ni);
2113 vap->iv_stats.is_ht_assoc_downgrade++;
2116 * If constrained to 11n-only stations reject legacy stations.
2118 if ((vap->iv_flags_ht & IEEE80211_FHT_PUREN) &&
2119 (ni->ni_flags & IEEE80211_NODE_HT) == 0) {
2120 htcapmismatch(ni, wh, reassoc, resp);
2121 vap->iv_stats.is_ht_assoc_nohtcap++;
2124 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
2126 ni->ni_intval = lintval;
2127 ni->ni_capinfo = capinfo;
2128 ni->ni_fhdwell = vap->iv_bss->ni_fhdwell;
2129 ni->ni_fhindex = vap->iv_bss->ni_fhindex;
2132 * XXX maybe better to just expand
2134 if (ieee80211_ies_init(&ni->ni_ies, sfrm, efrm - sfrm)) {
2135 #define setie(_ie, _off) ieee80211_ies_setie(ni->ni_ies, _ie, _off)
2137 setie(wpa_ie, wpa - sfrm);
2139 setie(rsn_ie, rsn - sfrm);
2141 setie(htcap_ie, htcap - sfrm);
2143 setie(wme_ie, wme - sfrm);
2145 * Mark node as capable of QoS.
2147 ni->ni_flags |= IEEE80211_NODE_QOS;
2149 ni->ni_flags &= ~IEEE80211_NODE_QOS;
2150 #ifdef IEEE80211_SUPPORT_SUPERG
2152 setie(ath_ie, ath - sfrm);
2154 * Parse ATH station parameters.
2156 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
2159 ni->ni_ath_flags = 0;
2162 ni->ni_flags &= ~IEEE80211_NODE_QOS;
2163 ni->ni_ath_flags = 0;
2165 ieee80211_node_join(ni, resp);
2166 ieee80211_deliver_l2uf(ni);
2170 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2171 case IEEE80211_FC0_SUBTYPE_DISASSOC: {
2174 if (vap->iv_state != IEEE80211_S_RUN ||
2175 /* NB: can happen when in promiscuous mode */
2176 !IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
2177 vap->iv_stats.is_rx_mgtdiscard++;
2181 * deauth/disassoc frame format
2184 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
2185 reason = le16toh(*(uint16_t *)frm);
2186 if (subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
2187 vap->iv_stats.is_rx_deauth++;
2188 IEEE80211_NODE_STAT(ni, rx_deauth);
2190 vap->iv_stats.is_rx_disassoc++;
2191 IEEE80211_NODE_STAT(ni, rx_disassoc);
2193 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2194 "recv %s (reason %d)", ieee80211_mgt_subtype_name[subtype >>
2195 IEEE80211_FC0_SUBTYPE_SHIFT], reason);
2196 if (ni != vap->iv_bss)
2197 ieee80211_node_leave(ni);
2201 case IEEE80211_FC0_SUBTYPE_ACTION:
2202 case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2203 if (ni == vap->iv_bss) {
2204 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2205 wh, NULL, "%s", "unknown node");
2206 vap->iv_stats.is_rx_mgtdiscard++;
2207 } else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2208 !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2209 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2210 wh, NULL, "%s", "not for us");
2211 vap->iv_stats.is_rx_mgtdiscard++;
2212 } else if (vap->iv_state != IEEE80211_S_RUN) {
2213 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2214 wh, NULL, "wrong state %s",
2215 ieee80211_state_name[vap->iv_state]);
2216 vap->iv_stats.is_rx_mgtdiscard++;
2218 if (ieee80211_parse_action(ni, m0) == 0)
2219 (void)ic->ic_recv_action(ni, wh, frm, efrm);
2223 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2224 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2225 case IEEE80211_FC0_SUBTYPE_ATIM:
2226 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2227 wh, NULL, "%s", "not handled");
2228 vap->iv_stats.is_rx_mgtdiscard++;
2232 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2233 wh, "mgt", "subtype 0x%x not handled", subtype);
2234 vap->iv_stats.is_rx_badsubtype++;
2240 hostap_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2243 case IEEE80211_FC0_SUBTYPE_PS_POLL:
2244 ni->ni_vap->iv_recv_pspoll(ni, m);
2246 case IEEE80211_FC0_SUBTYPE_BAR:
2247 ieee80211_recv_bar(ni, m);
2253 * Process a received ps-poll frame.
2256 ieee80211_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m0)
2258 struct ieee80211vap *vap = ni->ni_vap;
2259 struct ieee80211com *ic = vap->iv_ic;
2260 struct ieee80211_frame_min *wh;
2265 wh = mtod(m0, struct ieee80211_frame_min *);
2266 if (ni->ni_associd == 0) {
2267 IEEE80211_DISCARD(vap,
2268 IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2269 (struct ieee80211_frame *) wh, NULL,
2270 "%s", "unassociated station");
2271 vap->iv_stats.is_ps_unassoc++;
2272 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
2273 IEEE80211_REASON_NOT_ASSOCED);
2277 aid = le16toh(*(uint16_t *)wh->i_dur);
2278 if (aid != ni->ni_associd) {
2279 IEEE80211_DISCARD(vap,
2280 IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2281 (struct ieee80211_frame *) wh, NULL,
2282 "aid mismatch: sta aid 0x%x poll aid 0x%x",
2283 ni->ni_associd, aid);
2284 vap->iv_stats.is_ps_badaid++;
2286 * NB: We used to deauth the station but it turns out
2287 * the Blackberry Curve 8230 (and perhaps other devices)
2288 * sometimes send the wrong AID when WME is negotiated.
2289 * Being more lenient here seems ok as we already check
2290 * the station is associated and we only return frames
2291 * queued for the station (i.e. we don't use the AID).
2296 /* Okay, take the first queued packet and put it out... */
2297 m = ieee80211_node_psq_dequeue(ni, &qlen);
2299 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_POWER, wh->i_addr2,
2300 "%s", "recv ps-poll, but queue empty");
2301 ieee80211_send_nulldata(ieee80211_ref_node(ni));
2302 vap->iv_stats.is_ps_qempty++; /* XXX node stat */
2303 if (vap->iv_set_tim != NULL)
2304 vap->iv_set_tim(ni, 0); /* just in case */
2308 * If there are more packets, set the more packets bit
2309 * in the packet dispatched to the station; otherwise
2310 * turn off the TIM bit.
2313 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2314 "recv ps-poll, send packet, %u still queued", qlen);
2315 m->m_flags |= M_MORE_DATA;
2317 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2318 "%s", "recv ps-poll, send packet, queue empty");
2319 if (vap->iv_set_tim != NULL)
2320 vap->iv_set_tim(ni, 0);
2322 m->m_flags |= M_PWR_SAV; /* bypass PS handling */
2325 * Do the right thing; if it's an encap'ed frame then
2326 * call ieee80211_parent_xmitpkt() (and free the ref) else
2327 * call ieee80211_vap_xmitpkt().
2329 if (m->m_flags & M_ENCAP) {
2330 if (ieee80211_parent_xmitpkt(ic, m) != 0)
2331 ieee80211_free_node(ni);
2333 (void) ieee80211_vap_xmitpkt(vap, m);