2 * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 * $FreeBSD: head/sys/net80211/ieee80211_hostap.c 203422 2010-02-03 10:07:43Z rpaulo $
30 * IEEE 802.11 HOSTAP mode support.
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/kernel.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
43 #include <sys/endian.h>
44 #include <sys/errno.h>
46 #include <sys/sysctl.h>
49 #include <net/if_media.h>
50 #include <net/if_llc.h>
51 #include <net/ethernet.h>
52 #include <net/route.h>
56 #include <netproto/802_11/ieee80211_var.h>
57 #include <netproto/802_11/ieee80211_hostap.h>
58 #include <netproto/802_11/ieee80211_input.h>
59 #ifdef IEEE80211_SUPPORT_SUPERG
60 #include <netproto/802_11/ieee80211_superg.h>
62 #include <netproto/802_11/ieee80211_wds.h>
64 #define IEEE80211_RATE2MBS(r) (((r) & IEEE80211_RATE_VAL) / 2)
66 static void hostap_vattach(struct ieee80211vap *);
67 static int hostap_newstate(struct ieee80211vap *, enum ieee80211_state, int);
68 static int hostap_input(struct ieee80211_node *ni, struct mbuf *m,
70 static void hostap_deliver_data(struct ieee80211vap *,
71 struct ieee80211_node *, struct mbuf *);
72 static void hostap_recv_mgmt(struct ieee80211_node *, struct mbuf *,
73 int subtype, int rssi, int nf);
74 static void hostap_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
75 static void hostap_recv_pspoll(struct ieee80211_node *, struct mbuf *);
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;
105 sta_disassoc(void *arg, struct ieee80211_node *ni)
107 struct ieee80211vap *vap = arg;
109 if (ni->ni_vap == vap && ni->ni_associd != 0) {
110 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
111 IEEE80211_REASON_ASSOC_LEAVE);
112 ieee80211_node_leave(ni);
117 sta_csa(void *arg, struct ieee80211_node *ni)
119 struct ieee80211vap *vap = arg;
121 if (ni->ni_vap == vap && ni->ni_associd != 0)
122 if (ni->ni_inact > vap->iv_inact_init) {
123 ni->ni_inact = vap->iv_inact_init;
124 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
125 "%s: inact %u", __func__, ni->ni_inact);
130 sta_drop(void *arg, struct ieee80211_node *ni)
132 struct ieee80211vap *vap = arg;
134 if (ni->ni_vap == vap && ni->ni_associd != 0)
135 ieee80211_node_leave(ni);
139 * Does a channel change require associated stations to re-associate
140 * so protocol state is correct. This is used when doing CSA across
141 * bands or similar (e.g. HT -> legacy).
144 isbandchange(struct ieee80211com *ic)
146 return ((ic->ic_bsschan->ic_flags ^ ic->ic_csa_newchan->ic_flags) &
147 (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_HALF |
148 IEEE80211_CHAN_QUARTER | IEEE80211_CHAN_HT)) != 0;
152 * IEEE80211_M_HOSTAP vap state machine handler.
155 hostap_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
157 struct ieee80211com *ic = vap->iv_ic;
158 enum ieee80211_state ostate;
160 IEEE80211_LOCK_ASSERT(ic);
162 ostate = vap->iv_state;
163 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
164 __func__, ieee80211_state_name[ostate],
165 ieee80211_state_name[nstate], arg);
166 vap->iv_state = nstate; /* state transition */
167 if (ostate != IEEE80211_S_SCAN)
168 ieee80211_cancel_scan(vap); /* background scan */
170 case IEEE80211_S_INIT:
172 case IEEE80211_S_SCAN:
173 ieee80211_cancel_scan(vap);
175 case IEEE80211_S_CAC:
176 ieee80211_dfs_cac_stop(vap);
178 case IEEE80211_S_RUN:
179 ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
184 if (ostate != IEEE80211_S_INIT) {
185 /* NB: optimize INIT -> INIT case */
186 ieee80211_reset_bss(vap);
188 if (vap->iv_auth->ia_detach != NULL)
189 vap->iv_auth->ia_detach(vap);
191 case IEEE80211_S_SCAN:
193 case IEEE80211_S_CSA:
194 case IEEE80211_S_RUN:
195 ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
197 * Clear overlapping BSS state; the beacon frame
198 * will be reconstructed on transition to the RUN
199 * state and the timeout routines check if the flag
200 * is set before doing anything so this is sufficient.
202 ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
203 ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
205 case IEEE80211_S_CAC:
207 * NB: We may get here because of a manual channel
208 * change in which case we need to stop CAC
209 * XXX no need to stop if ostate RUN but it's ok
211 ieee80211_dfs_cac_stop(vap);
213 case IEEE80211_S_INIT:
214 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
215 !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
217 * Already have a channel; bypass the
218 * scan and startup immediately.
219 * ieee80211_create_ibss will call back to
220 * move us to RUN state.
222 ieee80211_create_ibss(vap, vap->iv_des_chan);
226 * Initiate a scan. We can come here as a result
227 * of an IEEE80211_IOC_SCAN_REQ too in which case
228 * the vap will be marked with IEEE80211_FEXT_SCANREQ
229 * and the scan request parameters will be present
230 * in iv_scanreq. Otherwise we do the default.
232 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
233 ieee80211_check_scan(vap,
234 vap->iv_scanreq_flags,
235 vap->iv_scanreq_duration,
236 vap->iv_scanreq_mindwell,
237 vap->iv_scanreq_maxdwell,
238 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
239 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
241 ieee80211_check_scan_current(vap);
243 case IEEE80211_S_SCAN:
245 * A state change requires a reset; scan.
247 ieee80211_check_scan_current(vap);
253 case IEEE80211_S_CAC:
255 * Start CAC on a DFS channel. We come here when starting
256 * a bss on a DFS channel (see ieee80211_create_ibss).
258 ieee80211_dfs_cac_start(vap);
260 case IEEE80211_S_RUN:
261 if (vap->iv_flags & IEEE80211_F_WPA) {
262 /* XXX validate prerequisites */
265 case IEEE80211_S_INIT:
267 * Already have a channel; bypass the
268 * scan and startup immediately.
269 * Note that ieee80211_create_ibss will call
270 * back to do a RUN->RUN state change.
272 ieee80211_create_ibss(vap,
273 ieee80211_ht_adjust_channel(ic,
274 ic->ic_curchan, vap->iv_flags_ht));
275 /* NB: iv_bss is changed on return */
277 case IEEE80211_S_CAC:
279 * NB: This is the normal state change when CAC
280 * expires and no radar was detected; no need to
281 * clear the CAC timer as it's already expired.
284 case IEEE80211_S_CSA:
286 * Shorten inactivity timer of associated stations
287 * to weed out sta's that don't follow a CSA.
289 ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
291 * Update bss node channel to reflect where
292 * we landed after CSA.
294 ieee80211_node_set_chan(vap->iv_bss,
295 ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
296 ieee80211_htchanflags(vap->iv_bss->ni_chan)));
297 /* XXX bypass debug msgs */
299 case IEEE80211_S_SCAN:
300 case IEEE80211_S_RUN:
301 #ifdef IEEE80211_DEBUG
302 if (ieee80211_msg_debug(vap)) {
303 struct ieee80211_node *ni = vap->iv_bss;
305 "synchronized with %6D ssid ",
307 ieee80211_print_essid(ni->ni_essid,
310 kprintf(" channel %d start %uMb\n",
311 ieee80211_chan2ieee(ic, ic->ic_curchan),
312 IEEE80211_RATE2MBS(ni->ni_txrate));
320 * Start/stop the authenticator. We delay until here
321 * to allow configuration to happen out of order.
323 if (vap->iv_auth->ia_attach != NULL) {
324 /* XXX check failure */
325 vap->iv_auth->ia_attach(vap);
326 } else if (vap->iv_auth->ia_detach != NULL) {
327 vap->iv_auth->ia_detach(vap);
329 ieee80211_node_authorize(vap->iv_bss);
331 case IEEE80211_S_CSA:
332 if (ostate == IEEE80211_S_RUN && isbandchange(ic)) {
334 * On a ``band change'' silently drop associated
335 * stations as they must re-associate before they
336 * can pass traffic (as otherwise protocol state
337 * such as capabilities and the negotiated rate
338 * set may/will be wrong).
340 ieee80211_iterate_nodes(&ic->ic_sta, sta_drop, vap);
350 hostap_deliver_data(struct ieee80211vap *vap,
351 struct ieee80211_node *ni, struct mbuf *m)
353 struct ether_header *eh = mtod(m, struct ether_header *);
354 struct ifnet *ifp = vap->iv_ifp;
356 /* clear driver/net80211 flags before passing up */
357 m->m_flags &= ~(M_80211_RX | M_MCAST | M_BCAST);
359 KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
360 ("gack, opmode %d", vap->iv_opmode));
365 IEEE80211_NODE_STAT(ni, rx_data);
366 IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
367 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
368 m->m_flags |= M_MCAST; /* XXX M_BCAST? */
369 IEEE80211_NODE_STAT(ni, rx_mcast);
371 IEEE80211_NODE_STAT(ni, rx_ucast);
373 /* perform as a bridge within the AP */
374 if ((vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0) {
375 struct mbuf *mcopy = NULL;
377 if (m->m_flags & M_MCAST) {
378 mcopy = m_dup(m, MB_DONTWAIT);
382 mcopy->m_flags |= M_MCAST;
385 * Check if the destination is associated with the
386 * same vap and authorized to receive traffic.
387 * Beware of traffic destined for the vap itself;
388 * sending it will not work; just let it be delivered
391 struct ieee80211_node *sta = ieee80211_find_vap_node(
392 &vap->iv_ic->ic_sta, vap, eh->ether_dhost);
394 if (ieee80211_node_is_authorized(sta)) {
396 * Beware of sending to ourself; this
397 * needs to happen via the normal
400 if (sta != vap->iv_bss) {
405 vap->iv_stats.is_rx_unauth++;
406 IEEE80211_NODE_STAT(sta, rx_unauth);
408 ieee80211_free_node(sta);
413 len = mcopy->m_pkthdr.len;
414 err = ieee80211_handoff(ifp, mcopy);
416 /* NB: IFQ_HANDOFF reclaims mcopy */
424 * Mark frame as coming from vap's interface.
426 m->m_pkthdr.rcvif = ifp;
427 if (m->m_flags & M_MCAST) {
429 * Spam DWDS vap's w/ multicast traffic.
431 /* XXX only if dwds in use? */
432 ieee80211_dwds_mcast(vap, m);
434 if (ni->ni_vlan != 0) {
435 /* attach vlan tag */
436 m->m_pkthdr.ether_vlantag = ni->ni_vlan;
437 m->m_flags |= M_VLANTAG;
439 ifp->if_input(ifp, m);
444 * Decide if a received management frame should be
445 * printed when debugging is enabled. This filters some
446 * of the less interesting frames that come frequently
450 doprint(struct ieee80211vap *vap, int subtype)
453 case IEEE80211_FC0_SUBTYPE_BEACON:
454 return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
455 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
462 * Process a received frame. The node associated with the sender
463 * should be supplied. If nothing was found in the node table then
464 * the caller is assumed to supply a reference to iv_bss instead.
465 * The RSSI and a timestamp are also supplied. The RSSI data is used
466 * during AP scanning to select a AP to associate with; it can have
467 * any units so long as values have consistent units and higher values
468 * mean ``better signal''. The receive timestamp is currently not used
469 * by the 802.11 layer.
472 hostap_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
474 #define SEQ_LEQ(a,b) ((int)((a)-(b)) <= 0)
475 #define HAS_SEQ(type) ((type & 0x4) == 0)
476 struct ieee80211vap *vap = ni->ni_vap;
477 struct ieee80211com *ic = ni->ni_ic;
478 struct ifnet *ifp = vap->iv_ifp;
479 struct ieee80211_frame *wh;
480 struct ieee80211_key *key;
481 struct ether_header *eh;
482 int hdrspace, need_tap = 1; /* mbuf need to be tapped. */
483 uint8_t dir, type, subtype, qos;
487 if (m->m_flags & M_AMPDU_MPDU) {
489 * Fastpath for A-MPDU reorder q resubmission. Frames
490 * w/ M_AMPDU_MPDU marked have already passed through
491 * here but were received out of order and been held on
492 * the reorder queue. When resubmitted they are marked
493 * with the M_AMPDU_MPDU flag and we can bypass most of
494 * the normal processing.
496 wh = mtod(m, struct ieee80211_frame *);
497 type = IEEE80211_FC0_TYPE_DATA;
498 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
499 subtype = IEEE80211_FC0_SUBTYPE_QOS;
500 hdrspace = ieee80211_hdrspace(ic, wh); /* XXX optimize? */
504 KASSERT(ni != NULL, ("null node"));
505 ni->ni_inact = ni->ni_inact_reload;
507 type = -1; /* undefined */
509 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
510 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
511 ni->ni_macaddr, NULL,
512 "too short (1): len %u", m->m_pkthdr.len);
513 vap->iv_stats.is_rx_tooshort++;
517 * Bit of a cheat here, we use a pointer for a 3-address
518 * frame format but don't reference fields past outside
519 * ieee80211_frame_min w/o first validating the data is
522 wh = mtod(m, struct ieee80211_frame *);
524 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
525 IEEE80211_FC0_VERSION_0) {
526 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
527 ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
528 wh->i_fc[0], wh->i_fc[1]);
529 vap->iv_stats.is_rx_badversion++;
533 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
534 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
535 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
536 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
537 if (dir != IEEE80211_FC1_DIR_NODS)
539 else if (type == IEEE80211_FC0_TYPE_CTL)
542 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
543 IEEE80211_DISCARD_MAC(vap,
544 IEEE80211_MSG_ANY, ni->ni_macaddr,
545 NULL, "too short (2): len %u",
547 vap->iv_stats.is_rx_tooshort++;
553 * Validate the bssid.
555 if (!(type == IEEE80211_FC0_TYPE_MGT &&
556 subtype == IEEE80211_FC0_SUBTYPE_BEACON) &&
557 !IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
558 !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
559 /* not interested in */
560 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
561 bssid, NULL, "%s", "not to bss");
562 vap->iv_stats.is_rx_wrongbss++;
566 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
569 uint8_t tid = ieee80211_gettid(wh);
570 if (IEEE80211_QOS_HAS_SEQ(wh) &&
571 TID_TO_WME_AC(tid) >= WME_AC_VI)
572 ic->ic_wme.wme_hipri_traffic++;
573 rxseq = le16toh(*(uint16_t *)wh->i_seq);
574 if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
575 (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
576 SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
577 /* duplicate, discard */
578 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
580 "seqno <%u,%u> fragno <%u,%u> tid %u",
581 rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
582 ni->ni_rxseqs[tid] >>
583 IEEE80211_SEQ_SEQ_SHIFT,
584 rxseq & IEEE80211_SEQ_FRAG_MASK,
586 IEEE80211_SEQ_FRAG_MASK,
588 vap->iv_stats.is_rx_dup++;
589 IEEE80211_NODE_STAT(ni, rx_dup);
592 ni->ni_rxseqs[tid] = rxseq;
597 case IEEE80211_FC0_TYPE_DATA:
598 hdrspace = ieee80211_hdrspace(ic, wh);
599 if (m->m_len < hdrspace &&
600 (m = m_pullup(m, hdrspace)) == NULL) {
601 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
602 ni->ni_macaddr, NULL,
603 "data too short: expecting %u", hdrspace);
604 vap->iv_stats.is_rx_tooshort++;
607 if (!(dir == IEEE80211_FC1_DIR_TODS ||
608 (dir == IEEE80211_FC1_DIR_DSTODS &&
609 (vap->iv_flags & IEEE80211_F_DWDS)))) {
610 if (dir != IEEE80211_FC1_DIR_DSTODS) {
611 IEEE80211_DISCARD(vap,
612 IEEE80211_MSG_INPUT, wh, "data",
613 "incorrect dir 0x%x", dir);
615 IEEE80211_DISCARD(vap,
616 IEEE80211_MSG_INPUT |
617 IEEE80211_MSG_WDS, wh,
619 "%s", "DWDS not enabled");
621 vap->iv_stats.is_rx_wrongdir++;
624 /* check if source STA is associated */
625 if (ni == vap->iv_bss) {
626 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
627 wh, "data", "%s", "unknown src");
628 ieee80211_send_error(ni, wh->i_addr2,
629 IEEE80211_FC0_SUBTYPE_DEAUTH,
630 IEEE80211_REASON_NOT_AUTHED);
631 vap->iv_stats.is_rx_notassoc++;
634 if (ni->ni_associd == 0) {
635 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
636 wh, "data", "%s", "unassoc src");
637 IEEE80211_SEND_MGMT(ni,
638 IEEE80211_FC0_SUBTYPE_DISASSOC,
639 IEEE80211_REASON_NOT_ASSOCED);
640 vap->iv_stats.is_rx_notassoc++;
645 * Check for power save state change.
646 * XXX out-of-order A-MPDU frames?
648 if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
649 (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
650 ieee80211_node_pwrsave(ni,
651 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
653 * For 4-address packets handle WDS discovery
654 * notifications. Once a WDS link is setup frames
655 * are just delivered to the WDS vap (see below).
657 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap == NULL) {
658 if (!ieee80211_node_is_authorized(ni)) {
659 IEEE80211_DISCARD(vap,
660 IEEE80211_MSG_INPUT |
661 IEEE80211_MSG_WDS, wh,
663 "%s", "unauthorized port");
664 vap->iv_stats.is_rx_unauth++;
665 IEEE80211_NODE_STAT(ni, rx_unauth);
668 ieee80211_dwds_discover(ni, m);
673 * Handle A-MPDU re-ordering. If the frame is to be
674 * processed directly then ieee80211_ampdu_reorder
675 * will return 0; otherwise it has consumed the mbuf
676 * and we should do nothing more with it.
678 if ((m->m_flags & M_AMPDU) &&
679 ieee80211_ampdu_reorder(ni, m) != 0) {
686 * Handle privacy requirements. Note that we
687 * must not be preempted from here until after
688 * we (potentially) call ieee80211_crypto_demic;
689 * otherwise we may violate assumptions in the
690 * crypto cipher modules used to do delayed update
691 * of replay sequence numbers.
693 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
694 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
696 * Discard encrypted frames when privacy is off.
698 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
699 wh, "WEP", "%s", "PRIVACY off");
700 vap->iv_stats.is_rx_noprivacy++;
701 IEEE80211_NODE_STAT(ni, rx_noprivacy);
704 key = ieee80211_crypto_decap(ni, m, hdrspace);
706 /* NB: stats+msgs handled in crypto_decap */
707 IEEE80211_NODE_STAT(ni, rx_wepfail);
710 wh = mtod(m, struct ieee80211_frame *);
711 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
713 /* XXX M_WEP and IEEE80211_F_PRIVACY */
718 * Save QoS bits for use below--before we strip the header.
720 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
721 qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
722 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
723 ((struct ieee80211_qosframe *)wh)->i_qos[0];
728 * Next up, any fragmentation.
730 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
731 m = ieee80211_defrag(ni, m, hdrspace);
733 /* Fragment dropped or frame not complete yet */
737 wh = NULL; /* no longer valid, catch any uses */
740 * Next strip any MSDU crypto bits.
742 if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
743 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
744 ni->ni_macaddr, "data", "%s", "demic error");
745 vap->iv_stats.is_rx_demicfail++;
746 IEEE80211_NODE_STAT(ni, rx_demicfail);
749 /* copy to listener after decrypt */
750 if (ieee80211_radiotap_active_vap(vap))
751 ieee80211_radiotap_rx(vap, m);
754 * Finally, strip the 802.11 header.
756 m = ieee80211_decap(vap, m, hdrspace);
758 /* XXX mask bit to check for both */
759 /* don't count Null data frames as errors */
760 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
761 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
763 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
764 ni->ni_macaddr, "data", "%s", "decap error");
765 vap->iv_stats.is_rx_decap++;
766 IEEE80211_NODE_STAT(ni, rx_decap);
769 eh = mtod(m, struct ether_header *);
770 if (!ieee80211_node_is_authorized(ni)) {
772 * Deny any non-PAE frames received prior to
773 * authorization. For open/shared-key
774 * authentication the port is mark authorized
775 * after authentication completes. For 802.1x
776 * the port is not marked authorized by the
777 * authenticator until the handshake has completed.
779 if (eh->ether_type != htons(ETHERTYPE_PAE)) {
780 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
781 eh->ether_shost, "data",
782 "unauthorized port: ether type 0x%x len %u",
783 eh->ether_type, m->m_pkthdr.len);
784 vap->iv_stats.is_rx_unauth++;
785 IEEE80211_NODE_STAT(ni, rx_unauth);
790 * When denying unencrypted frames, discard
791 * any non-PAE frames received without encryption.
793 if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
794 (key == NULL && (m->m_flags & M_WEP) == 0) &&
795 eh->ether_type != htons(ETHERTYPE_PAE)) {
797 * Drop unencrypted frames.
799 vap->iv_stats.is_rx_unencrypted++;
800 IEEE80211_NODE_STAT(ni, rx_unencrypted);
804 /* XXX require HT? */
805 if (qos & IEEE80211_QOS_AMSDU) {
806 m = ieee80211_decap_amsdu(ni, m);
808 return IEEE80211_FC0_TYPE_DATA;
810 #ifdef IEEE80211_SUPPORT_SUPERG
811 m = ieee80211_decap_fastframe(vap, ni, m);
813 return IEEE80211_FC0_TYPE_DATA;
816 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
817 ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
819 hostap_deliver_data(vap, ni, m);
820 return IEEE80211_FC0_TYPE_DATA;
822 case IEEE80211_FC0_TYPE_MGT:
823 vap->iv_stats.is_rx_mgmt++;
824 IEEE80211_NODE_STAT(ni, rx_mgmt);
825 if (dir != IEEE80211_FC1_DIR_NODS) {
826 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
827 wh, "mgt", "incorrect dir 0x%x", dir);
828 vap->iv_stats.is_rx_wrongdir++;
831 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
832 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
833 ni->ni_macaddr, "mgt", "too short: len %u",
835 vap->iv_stats.is_rx_tooshort++;
838 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
839 /* ensure return frames are unicast */
840 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
841 wh, NULL, "source is multicast: %6D",
843 vap->iv_stats.is_rx_mgtdiscard++; /* XXX stat */
846 #ifdef IEEE80211_DEBUG
847 if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
848 ieee80211_msg_dumppkts(vap)) {
849 if_printf(ifp, "received %s from %6D rssi %d\n",
850 ieee80211_mgt_subtype_name[subtype >>
851 IEEE80211_FC0_SUBTYPE_SHIFT],
852 wh->i_addr2, ":", rssi);
855 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
856 if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
858 * Only shared key auth frames with a challenge
859 * should be encrypted, discard all others.
861 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
863 "%s", "WEP set but not permitted");
864 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
867 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
869 * Discard encrypted frames when privacy is off.
871 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
872 wh, NULL, "%s", "WEP set but PRIVACY off");
873 vap->iv_stats.is_rx_noprivacy++;
876 hdrspace = ieee80211_hdrspace(ic, wh);
877 key = ieee80211_crypto_decap(ni, m, hdrspace);
879 /* NB: stats+msgs handled in crypto_decap */
882 wh = mtod(m, struct ieee80211_frame *);
883 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
885 vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
888 case IEEE80211_FC0_TYPE_CTL:
889 vap->iv_stats.is_rx_ctl++;
890 IEEE80211_NODE_STAT(ni, rx_ctrl);
891 vap->iv_recv_ctl(ni, m, subtype);
894 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
895 wh, "bad", "frame type 0x%x", type);
896 /* should not come here */
903 if (need_tap && ieee80211_radiotap_active_vap(vap))
904 ieee80211_radiotap_rx(vap, m);
912 hostap_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
913 int rssi, int nf, uint16_t seq, uint16_t status)
915 struct ieee80211vap *vap = ni->ni_vap;
917 KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
919 if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
920 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
921 ni->ni_macaddr, "open auth",
922 "bad sta auth mode %u", ni->ni_authmode);
923 vap->iv_stats.is_rx_bad_auth++; /* XXX */
925 * Clear any challenge text that may be there if
926 * a previous shared key auth failed and then an
927 * open auth is attempted.
929 if (ni->ni_challenge != NULL) {
930 kfree(ni->ni_challenge, M_80211_NODE);
931 ni->ni_challenge = NULL;
933 /* XXX hack to workaround calling convention */
934 ieee80211_send_error(ni, wh->i_addr2,
935 IEEE80211_FC0_SUBTYPE_AUTH,
936 (seq + 1) | (IEEE80211_STATUS_ALG<<16));
939 if (seq != IEEE80211_AUTH_OPEN_REQUEST) {
940 vap->iv_stats.is_rx_bad_auth++;
943 /* always accept open authentication requests */
944 if (ni == vap->iv_bss) {
945 ni = ieee80211_dup_bss(vap, wh->i_addr2);
948 } else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
949 (void) ieee80211_ref_node(ni);
951 * Mark the node as referenced to reflect that it's
952 * reference count has been bumped to insure it remains
953 * after the transaction completes.
955 ni->ni_flags |= IEEE80211_NODE_AREF;
957 * Mark the node as requiring a valid association id
958 * before outbound traffic is permitted.
960 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
962 if (vap->iv_acl != NULL &&
963 vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
965 * When the ACL policy is set to RADIUS we defer the
966 * authorization to a user agent. Dispatch an event,
967 * a subsequent MLME call will decide the fate of the
968 * station. If the user agent is not present then the
969 * node will be reclaimed due to inactivity.
971 IEEE80211_NOTE_MAC(vap,
972 IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL, ni->ni_macaddr,
973 "%s", "station authentication defered (radius acl)");
974 ieee80211_notify_node_auth(ni);
976 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
977 IEEE80211_NOTE_MAC(vap,
978 IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni->ni_macaddr,
979 "%s", "station authenticated (open)");
981 * When 802.1x is not in use mark the port
982 * authorized at this point so traffic can flow.
984 if (ni->ni_authmode != IEEE80211_AUTH_8021X)
985 ieee80211_node_authorize(ni);
990 hostap_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
991 uint8_t *frm, uint8_t *efrm, int rssi, int nf,
992 uint16_t seq, uint16_t status)
994 struct ieee80211vap *vap = ni->ni_vap;
996 int allocbs, estatus;
998 KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
1001 * NB: this can happen as we allow pre-shared key
1002 * authentication to be enabled w/o wep being turned
1003 * on so that configuration of these can be done
1004 * in any order. It may be better to enforce the
1005 * ordering in which case this check would just be
1006 * for sanity/consistency.
1008 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
1009 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1010 ni->ni_macaddr, "shared key auth",
1011 "%s", " PRIVACY is disabled");
1012 estatus = IEEE80211_STATUS_ALG;
1016 * Pre-shared key authentication is evil; accept
1017 * it only if explicitly configured (it is supported
1018 * mainly for compatibility with clients like Mac OS X).
1020 if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1021 ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1022 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1023 ni->ni_macaddr, "shared key auth",
1024 "bad sta auth mode %u", ni->ni_authmode);
1025 vap->iv_stats.is_rx_bad_auth++; /* XXX maybe a unique error? */
1026 estatus = IEEE80211_STATUS_ALG;
1031 if (frm + 1 < efrm) {
1032 if ((frm[1] + 2) > (efrm - frm)) {
1033 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1034 ni->ni_macaddr, "shared key auth",
1035 "ie %d/%d too long",
1036 frm[0], (frm[1] + 2) - (efrm - frm));
1037 vap->iv_stats.is_rx_bad_auth++;
1038 estatus = IEEE80211_STATUS_CHALLENGE;
1041 if (*frm == IEEE80211_ELEMID_CHALLENGE)
1046 case IEEE80211_AUTH_SHARED_CHALLENGE:
1047 case IEEE80211_AUTH_SHARED_RESPONSE:
1048 if (challenge == NULL) {
1049 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1050 ni->ni_macaddr, "shared key auth",
1051 "%s", "no challenge");
1052 vap->iv_stats.is_rx_bad_auth++;
1053 estatus = IEEE80211_STATUS_CHALLENGE;
1056 if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1057 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1058 ni->ni_macaddr, "shared key auth",
1059 "bad challenge len %d", challenge[1]);
1060 vap->iv_stats.is_rx_bad_auth++;
1061 estatus = IEEE80211_STATUS_CHALLENGE;
1068 case IEEE80211_AUTH_SHARED_REQUEST:
1069 if (ni == vap->iv_bss) {
1070 ni = ieee80211_dup_bss(vap, wh->i_addr2);
1072 /* NB: no way to return an error */
1077 if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1078 (void) ieee80211_ref_node(ni);
1082 * Mark the node as referenced to reflect that it's
1083 * reference count has been bumped to insure it remains
1084 * after the transaction completes.
1086 ni->ni_flags |= IEEE80211_NODE_AREF;
1088 * Mark the node as requiring a valid associatio id
1089 * before outbound traffic is permitted.
1091 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
1092 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1094 if (!ieee80211_alloc_challenge(ni)) {
1095 /* NB: don't return error so they rexmit */
1098 get_random_bytes(ni->ni_challenge,
1099 IEEE80211_CHALLENGE_LEN);
1100 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1101 ni, "shared key %sauth request", allocbs ? "" : "re");
1103 * When the ACL policy is set to RADIUS we defer the
1104 * authorization to a user agent. Dispatch an event,
1105 * a subsequent MLME call will decide the fate of the
1106 * station. If the user agent is not present then the
1107 * node will be reclaimed due to inactivity.
1109 if (vap->iv_acl != NULL &&
1110 vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
1111 IEEE80211_NOTE_MAC(vap,
1112 IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL,
1114 "%s", "station authentication defered (radius acl)");
1115 ieee80211_notify_node_auth(ni);
1119 case IEEE80211_AUTH_SHARED_RESPONSE:
1120 if (ni == vap->iv_bss) {
1121 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1122 ni->ni_macaddr, "shared key response",
1123 "%s", "unknown station");
1124 /* NB: don't send a response */
1127 if (ni->ni_challenge == NULL) {
1128 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1129 ni->ni_macaddr, "shared key response",
1130 "%s", "no challenge recorded");
1131 vap->iv_stats.is_rx_bad_auth++;
1132 estatus = IEEE80211_STATUS_CHALLENGE;
1135 if (memcmp(ni->ni_challenge, &challenge[2],
1136 challenge[1]) != 0) {
1137 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1138 ni->ni_macaddr, "shared key response",
1139 "%s", "challenge mismatch");
1140 vap->iv_stats.is_rx_auth_fail++;
1141 estatus = IEEE80211_STATUS_CHALLENGE;
1144 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1145 ni, "%s", "station authenticated (shared key)");
1146 ieee80211_node_authorize(ni);
1149 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1150 ni->ni_macaddr, "shared key auth",
1152 vap->iv_stats.is_rx_bad_auth++;
1153 estatus = IEEE80211_STATUS_SEQUENCE;
1156 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1160 * Send an error response; but only when operating as an AP.
1162 /* XXX hack to workaround calling convention */
1163 ieee80211_send_error(ni, wh->i_addr2,
1164 IEEE80211_FC0_SUBTYPE_AUTH,
1165 (seq + 1) | (estatus<<16));
1169 * Convert a WPA cipher selector OUI to an internal
1170 * cipher algorithm. Where appropriate we also
1171 * record any key length.
1174 wpa_cipher(const uint8_t *sel, uint8_t *keylen)
1176 #define WPA_SEL(x) (((x)<<24)|WPA_OUI)
1177 uint32_t w = LE_READ_4(sel);
1180 case WPA_SEL(WPA_CSE_NULL):
1181 return IEEE80211_CIPHER_NONE;
1182 case WPA_SEL(WPA_CSE_WEP40):
1184 *keylen = 40 / NBBY;
1185 return IEEE80211_CIPHER_WEP;
1186 case WPA_SEL(WPA_CSE_WEP104):
1188 *keylen = 104 / NBBY;
1189 return IEEE80211_CIPHER_WEP;
1190 case WPA_SEL(WPA_CSE_TKIP):
1191 return IEEE80211_CIPHER_TKIP;
1192 case WPA_SEL(WPA_CSE_CCMP):
1193 return IEEE80211_CIPHER_AES_CCM;
1195 return 32; /* NB: so 1<< is discarded */
1200 * Convert a WPA key management/authentication algorithm
1201 * to an internal code.
1204 wpa_keymgmt(const uint8_t *sel)
1206 #define WPA_SEL(x) (((x)<<24)|WPA_OUI)
1207 uint32_t w = LE_READ_4(sel);
1210 case WPA_SEL(WPA_ASE_8021X_UNSPEC):
1211 return WPA_ASE_8021X_UNSPEC;
1212 case WPA_SEL(WPA_ASE_8021X_PSK):
1213 return WPA_ASE_8021X_PSK;
1214 case WPA_SEL(WPA_ASE_NONE):
1215 return WPA_ASE_NONE;
1217 return 0; /* NB: so is discarded */
1222 * Parse a WPA information element to collect parameters.
1223 * Note that we do not validate security parameters; that
1224 * is handled by the authenticator; the parsing done here
1225 * is just for internal use in making operational decisions.
1228 ieee80211_parse_wpa(struct ieee80211vap *vap, const uint8_t *frm,
1229 struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1231 uint8_t len = frm[1];
1236 * Check the length once for fixed parts: OUI, type,
1237 * version, mcast cipher, and 2 selector counts.
1238 * Other, variable-length data, must be checked separately.
1240 if ((vap->iv_flags & IEEE80211_F_WPA1) == 0) {
1241 IEEE80211_DISCARD_IE(vap,
1242 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1243 wh, "WPA", "not WPA, flags 0x%x", vap->iv_flags);
1244 return IEEE80211_REASON_IE_INVALID;
1247 IEEE80211_DISCARD_IE(vap,
1248 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1249 wh, "WPA", "too short, len %u", len);
1250 return IEEE80211_REASON_IE_INVALID;
1252 frm += 6, len -= 4; /* NB: len is payload only */
1253 /* NB: iswpaoui already validated the OUI and type */
1255 if (w != WPA_VERSION) {
1256 IEEE80211_DISCARD_IE(vap,
1257 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1258 wh, "WPA", "bad version %u", w);
1259 return IEEE80211_REASON_IE_INVALID;
1263 memset(rsn, 0, sizeof(*rsn));
1265 /* multicast/group cipher */
1266 rsn->rsn_mcastcipher = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
1269 /* unicast ciphers */
1273 IEEE80211_DISCARD_IE(vap,
1274 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1275 wh, "WPA", "ucast cipher data too short; len %u, n %u",
1277 return IEEE80211_REASON_IE_INVALID;
1280 for (; n > 0; n--) {
1281 w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
1284 if (w & (1<<IEEE80211_CIPHER_TKIP))
1285 rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1287 rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1289 /* key management algorithms */
1293 IEEE80211_DISCARD_IE(vap,
1294 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1295 wh, "WPA", "key mgmt alg data too short; len %u, n %u",
1297 return IEEE80211_REASON_IE_INVALID;
1300 for (; n > 0; n--) {
1301 w |= wpa_keymgmt(frm);
1304 if (w & WPA_ASE_8021X_UNSPEC)
1305 rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
1307 rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1309 if (len > 2) /* optional capabilities */
1310 rsn->rsn_caps = LE_READ_2(frm);
1316 * Convert an RSN cipher selector OUI to an internal
1317 * cipher algorithm. Where appropriate we also
1318 * record any key length.
1321 rsn_cipher(const uint8_t *sel, uint8_t *keylen)
1323 #define RSN_SEL(x) (((x)<<24)|RSN_OUI)
1324 uint32_t w = LE_READ_4(sel);
1327 case RSN_SEL(RSN_CSE_NULL):
1328 return IEEE80211_CIPHER_NONE;
1329 case RSN_SEL(RSN_CSE_WEP40):
1331 *keylen = 40 / NBBY;
1332 return IEEE80211_CIPHER_WEP;
1333 case RSN_SEL(RSN_CSE_WEP104):
1335 *keylen = 104 / NBBY;
1336 return IEEE80211_CIPHER_WEP;
1337 case RSN_SEL(RSN_CSE_TKIP):
1338 return IEEE80211_CIPHER_TKIP;
1339 case RSN_SEL(RSN_CSE_CCMP):
1340 return IEEE80211_CIPHER_AES_CCM;
1341 case RSN_SEL(RSN_CSE_WRAP):
1342 return IEEE80211_CIPHER_AES_OCB;
1344 return 32; /* NB: so 1<< is discarded */
1349 * Convert an RSN key management/authentication algorithm
1350 * to an internal code.
1353 rsn_keymgmt(const uint8_t *sel)
1355 #define RSN_SEL(x) (((x)<<24)|RSN_OUI)
1356 uint32_t w = LE_READ_4(sel);
1359 case RSN_SEL(RSN_ASE_8021X_UNSPEC):
1360 return RSN_ASE_8021X_UNSPEC;
1361 case RSN_SEL(RSN_ASE_8021X_PSK):
1362 return RSN_ASE_8021X_PSK;
1363 case RSN_SEL(RSN_ASE_NONE):
1364 return RSN_ASE_NONE;
1366 return 0; /* NB: so is discarded */
1371 * Parse a WPA/RSN information element to collect parameters
1372 * and validate the parameters against what has been
1373 * configured for the system.
1376 ieee80211_parse_rsn(struct ieee80211vap *vap, const uint8_t *frm,
1377 struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1379 uint8_t len = frm[1];
1384 * Check the length once for fixed parts:
1385 * version, mcast cipher, and 2 selector counts.
1386 * Other, variable-length data, must be checked separately.
1388 if ((vap->iv_flags & IEEE80211_F_WPA2) == 0) {
1389 IEEE80211_DISCARD_IE(vap,
1390 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1391 wh, "WPA", "not RSN, flags 0x%x", vap->iv_flags);
1392 return IEEE80211_REASON_IE_INVALID;
1395 IEEE80211_DISCARD_IE(vap,
1396 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1397 wh, "RSN", "too short, len %u", len);
1398 return IEEE80211_REASON_IE_INVALID;
1402 if (w != RSN_VERSION) {
1403 IEEE80211_DISCARD_IE(vap,
1404 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1405 wh, "RSN", "bad version %u", w);
1406 return IEEE80211_REASON_IE_INVALID;
1410 memset(rsn, 0, sizeof(*rsn));
1412 /* multicast/group cipher */
1413 rsn->rsn_mcastcipher = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
1416 /* unicast ciphers */
1420 IEEE80211_DISCARD_IE(vap,
1421 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1422 wh, "RSN", "ucast cipher data too short; len %u, n %u",
1424 return IEEE80211_REASON_IE_INVALID;
1427 for (; n > 0; n--) {
1428 w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
1431 if (w & (1<<IEEE80211_CIPHER_TKIP))
1432 rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1434 rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1436 /* key management algorithms */
1440 IEEE80211_DISCARD_IE(vap,
1441 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1442 wh, "RSN", "key mgmt alg data too short; len %u, n %u",
1444 return IEEE80211_REASON_IE_INVALID;
1447 for (; n > 0; n--) {
1448 w |= rsn_keymgmt(frm);
1451 if (w & RSN_ASE_8021X_UNSPEC)
1452 rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
1454 rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
1456 /* optional RSN capabilities */
1458 rsn->rsn_caps = LE_READ_2(frm);
1465 * WPA/802.11i assocation request processing.
1468 wpa_assocreq(struct ieee80211_node *ni, struct ieee80211_rsnparms *rsnparms,
1469 const struct ieee80211_frame *wh, const uint8_t *wpa,
1470 const uint8_t *rsn, uint16_t capinfo)
1472 struct ieee80211vap *vap = ni->ni_vap;
1476 ni->ni_flags &= ~(IEEE80211_NODE_WPS|IEEE80211_NODE_TSN);
1477 if (wpa == NULL && rsn == NULL) {
1478 if (vap->iv_flags_ext & IEEE80211_FEXT_WPS) {
1480 * W-Fi Protected Setup (WPS) permits
1481 * clients to associate and pass EAPOL frames
1482 * to establish initial credentials.
1484 ni->ni_flags |= IEEE80211_NODE_WPS;
1487 if ((vap->iv_flags_ext & IEEE80211_FEXT_TSN) &&
1488 (capinfo & IEEE80211_CAPINFO_PRIVACY)) {
1490 * Transitional Security Network. Permits clients
1491 * to associate and use WEP while WPA is configured.
1493 ni->ni_flags |= IEEE80211_NODE_TSN;
1496 IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1497 wh, NULL, "%s", "no WPA/RSN IE in association request");
1498 vap->iv_stats.is_rx_assoc_badwpaie++;
1499 reason = IEEE80211_REASON_IE_INVALID;
1502 /* assert right association security credentials */
1503 badwparsn = 0; /* NB: to silence compiler */
1504 switch (vap->iv_flags & IEEE80211_F_WPA) {
1505 case IEEE80211_F_WPA1:
1506 badwparsn = (wpa == NULL);
1508 case IEEE80211_F_WPA2:
1509 badwparsn = (rsn == NULL);
1511 case IEEE80211_F_WPA1|IEEE80211_F_WPA2:
1512 badwparsn = (wpa == NULL && rsn == NULL);
1516 IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1518 "%s", "missing WPA/RSN IE in association request");
1519 vap->iv_stats.is_rx_assoc_badwpaie++;
1520 reason = IEEE80211_REASON_IE_INVALID;
1524 * Parse WPA/RSN information element.
1527 reason = ieee80211_parse_wpa(vap, wpa, rsnparms, wh);
1529 reason = ieee80211_parse_rsn(vap, rsn, rsnparms, wh);
1531 /* XXX distinguish WPA/RSN? */
1532 vap->iv_stats.is_rx_assoc_badwpaie++;
1535 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA, ni,
1536 "%s ie: mc %u/%u uc %u/%u key %u caps 0x%x",
1537 wpa != NULL ? "WPA" : "RSN",
1538 rsnparms->rsn_mcastcipher, rsnparms->rsn_mcastkeylen,
1539 rsnparms->rsn_ucastcipher, rsnparms->rsn_ucastkeylen,
1540 rsnparms->rsn_keymgmt, rsnparms->rsn_caps);
1544 ieee80211_node_deauth(ni, reason);
1548 /* XXX find a better place for definition */
1549 struct l2_update_frame {
1550 struct ether_header eh;
1558 * Deliver a TGf L2UF frame on behalf of a station.
1559 * This primes any bridge when the station is roaming
1560 * between ap's on the same wired network.
1563 ieee80211_deliver_l2uf(struct ieee80211_node *ni)
1565 struct ieee80211vap *vap = ni->ni_vap;
1566 struct ifnet *ifp = vap->iv_ifp;
1568 struct l2_update_frame *l2uf;
1569 struct ether_header *eh;
1571 m = m_gethdr(MB_DONTWAIT, MT_DATA);
1573 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1574 "%s", "no mbuf for l2uf frame");
1575 vap->iv_stats.is_rx_nobuf++; /* XXX not right */
1578 l2uf = mtod(m, struct l2_update_frame *);
1580 /* dst: Broadcast address */
1581 IEEE80211_ADDR_COPY(eh->ether_dhost, ifp->if_broadcastaddr);
1582 /* src: associated STA */
1583 IEEE80211_ADDR_COPY(eh->ether_shost, ni->ni_macaddr);
1584 eh->ether_type = htons(sizeof(*l2uf) - sizeof(*eh));
1588 l2uf->control = 0xf5;
1589 l2uf->xid[0] = 0x81;
1590 l2uf->xid[1] = 0x80;
1591 l2uf->xid[2] = 0x00;
1593 m->m_pkthdr.len = m->m_len = sizeof(*l2uf);
1594 hostap_deliver_data(vap, ni, m);
1598 ratesetmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1599 int reassoc, int resp, const char *tag, int rate)
1601 IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1602 "deny %s request, %s rate set mismatch, rate/MCS %d",
1603 reassoc ? "reassoc" : "assoc", tag, rate & IEEE80211_RATE_VAL);
1604 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_BASIC_RATE);
1605 ieee80211_node_leave(ni);
1609 capinfomismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1610 int reassoc, int resp, const char *tag, int capinfo)
1612 struct ieee80211vap *vap = ni->ni_vap;
1614 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1615 "deny %s request, %s mismatch 0x%x",
1616 reassoc ? "reassoc" : "assoc", tag, capinfo);
1617 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_CAPINFO);
1618 ieee80211_node_leave(ni);
1619 vap->iv_stats.is_rx_assoc_capmismatch++;
1623 htcapmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1624 int reassoc, int resp)
1626 IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1627 "deny %s request, %s missing HT ie", reassoc ? "reassoc" : "assoc");
1628 /* XXX no better code */
1629 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_MISSING_HT_CAPS);
1630 ieee80211_node_leave(ni);
1634 authalgreject(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1635 int algo, int seq, int status)
1637 struct ieee80211vap *vap = ni->ni_vap;
1639 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1640 wh, NULL, "unsupported alg %d", algo);
1641 vap->iv_stats.is_rx_auth_unsupported++;
1642 ieee80211_send_error(ni, wh->i_addr2, IEEE80211_FC0_SUBTYPE_AUTH,
1643 seq | (status << 16));
1647 ishtmixed(const uint8_t *ie)
1649 const struct ieee80211_ie_htinfo *ht =
1650 (const struct ieee80211_ie_htinfo *) ie;
1651 return (ht->hi_byte2 & IEEE80211_HTINFO_OPMODE) ==
1652 IEEE80211_HTINFO_OPMODE_MIXED;
1656 is11bclient(const uint8_t *rates, const uint8_t *xrates)
1658 static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
1661 /* NB: the 11b clients we care about will not have xrates */
1662 if (xrates != NULL || rates == NULL)
1664 for (i = 0; i < rates[1]; i++) {
1665 int r = rates[2+i] & IEEE80211_RATE_VAL;
1666 if (r > 2*11 || ((1<<r) & brates) == 0)
1673 hostap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1674 int subtype, int rssi, int nf)
1676 struct ieee80211vap *vap = ni->ni_vap;
1677 struct ieee80211com *ic = ni->ni_ic;
1678 struct ieee80211_frame *wh;
1679 uint8_t *frm, *efrm, *sfrm;
1680 uint8_t *ssid, *rates, *xrates, *wpa, *rsn, *wme, *ath, *htcap;
1684 wh = mtod(m0, struct ieee80211_frame *);
1685 frm = (uint8_t *)&wh[1];
1686 efrm = mtod(m0, uint8_t *) + m0->m_len;
1688 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1689 case IEEE80211_FC0_SUBTYPE_BEACON: {
1690 struct ieee80211_scanparams scan;
1692 * We process beacon/probe response frames when scanning;
1693 * otherwise we check beacon frames for overlapping non-ERP
1694 * BSS in 11g and/or overlapping legacy BSS when in HT.
1696 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
1697 subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
1698 vap->iv_stats.is_rx_mgtdiscard++;
1701 /* NB: accept off-channel frames */
1702 if (ieee80211_parse_beacon(ni, m0, &scan) &~ IEEE80211_BPARSE_OFFCHAN)
1705 * Count frame now that we know it's to be processed.
1707 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1708 vap->iv_stats.is_rx_beacon++; /* XXX remove */
1709 IEEE80211_NODE_STAT(ni, rx_beacons);
1711 IEEE80211_NODE_STAT(ni, rx_proberesp);
1713 * If scanning, just pass information to the scan module.
1715 if (ic->ic_flags & IEEE80211_F_SCAN) {
1716 if (scan.status == 0 && /* NB: on channel */
1717 (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN)) {
1719 * Actively scanning a channel marked passive;
1720 * send a probe request now that we know there
1721 * is 802.11 traffic present.
1723 * XXX check if the beacon we recv'd gives
1724 * us what we need and suppress the probe req
1726 ieee80211_probe_curchan(vap, 1);
1727 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1729 ieee80211_add_scan(vap, &scan, wh, subtype, rssi, nf);
1733 * Check beacon for overlapping bss w/ non ERP stations.
1734 * If we detect one and protection is configured but not
1735 * enabled, enable it and start a timer that'll bring us
1736 * out if we stop seeing the bss.
1738 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1739 scan.status == 0 && /* NB: on-channel */
1740 ((scan.erp & 0x100) == 0 || /* NB: no ERP, 11b sta*/
1741 (scan.erp & IEEE80211_ERP_NON_ERP_PRESENT))) {
1742 ic->ic_lastnonerp = ticks;
1743 ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
1744 if (ic->ic_protmode != IEEE80211_PROT_NONE &&
1745 (ic->ic_flags & IEEE80211_F_USEPROT) == 0) {
1746 IEEE80211_NOTE_FRAME(vap,
1747 IEEE80211_MSG_ASSOC, wh,
1748 "non-ERP present on channel %d "
1749 "(saw erp 0x%x from channel %d), "
1750 "enable use of protection",
1751 ic->ic_curchan->ic_ieee,
1752 scan.erp, scan.chan);
1753 ic->ic_flags |= IEEE80211_F_USEPROT;
1754 ieee80211_notify_erp(ic);
1758 * Check beacon for non-HT station on HT channel
1759 * and update HT BSS occupancy as appropriate.
1761 if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) {
1762 if (scan.status & IEEE80211_BPARSE_OFFCHAN) {
1764 * Off control channel; only check frames
1765 * that come in the extension channel when
1766 * operating w/ HT40.
1768 if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
1770 if (scan.chan != ic->ic_curchan->ic_extieee)
1773 if (scan.htinfo == NULL) {
1774 ieee80211_htprot_update(ic,
1775 IEEE80211_HTINFO_OPMODE_PROTOPT |
1776 IEEE80211_HTINFO_NONHT_PRESENT);
1777 } else if (ishtmixed(scan.htinfo)) {
1778 /* XXX? take NONHT_PRESENT from beacon? */
1779 ieee80211_htprot_update(ic,
1780 IEEE80211_HTINFO_OPMODE_MIXED |
1781 IEEE80211_HTINFO_NONHT_PRESENT);
1787 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1788 if (vap->iv_state != IEEE80211_S_RUN) {
1789 vap->iv_stats.is_rx_mgtdiscard++;
1793 * prreq frame format
1795 * [tlv] supported rates
1796 * [tlv] extended supported rates
1798 ssid = rates = xrates = NULL;
1800 while (efrm - frm > 1) {
1801 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1803 case IEEE80211_ELEMID_SSID:
1806 case IEEE80211_ELEMID_RATES:
1809 case IEEE80211_ELEMID_XRATES:
1815 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1817 IEEE80211_VERIFY_ELEMENT(xrates,
1818 IEEE80211_RATE_MAXSIZE - rates[1], return);
1819 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1820 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
1821 if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
1822 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1824 "%s", "no ssid with ssid suppression enabled");
1825 vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
1829 /* XXX find a better class or define it's own */
1830 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1831 "%s", "recv probe req");
1833 * Some legacy 11b clients cannot hack a complete
1834 * probe response frame. When the request includes
1835 * only a bare-bones rate set, communicate this to
1836 * the transmit side.
1838 ieee80211_send_proberesp(vap, wh->i_addr2,
1839 is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
1842 case IEEE80211_FC0_SUBTYPE_AUTH: {
1843 uint16_t algo, seq, status;
1845 if (vap->iv_state != IEEE80211_S_RUN) {
1846 vap->iv_stats.is_rx_mgtdiscard++;
1849 if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1850 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1851 wh, NULL, "%s", "wrong bssid");
1852 vap->iv_stats.is_rx_wrongbss++; /*XXX unique stat?*/
1862 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1863 algo = le16toh(*(uint16_t *)frm);
1864 seq = le16toh(*(uint16_t *)(frm + 2));
1865 status = le16toh(*(uint16_t *)(frm + 4));
1866 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1867 "recv auth frame with algorithm %d seq %d", algo, seq);
1869 * Consult the ACL policy module if setup.
1871 if (vap->iv_acl != NULL &&
1872 !vap->iv_acl->iac_check(vap, wh->i_addr2)) {
1873 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1874 wh, NULL, "%s", "disallowed by ACL");
1875 vap->iv_stats.is_rx_acl++;
1876 ieee80211_send_error(ni, wh->i_addr2,
1877 IEEE80211_FC0_SUBTYPE_AUTH,
1878 (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
1881 if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1882 IEEE80211_DISCARD(vap,
1883 IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1884 wh, NULL, "%s", "TKIP countermeasures enabled");
1885 vap->iv_stats.is_rx_auth_countermeasures++;
1886 ieee80211_send_error(ni, wh->i_addr2,
1887 IEEE80211_FC0_SUBTYPE_AUTH,
1888 IEEE80211_REASON_MIC_FAILURE);
1891 if (algo == IEEE80211_AUTH_ALG_SHARED)
1892 hostap_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1894 else if (algo == IEEE80211_AUTH_ALG_OPEN)
1895 hostap_auth_open(ni, wh, rssi, nf, seq, status);
1896 else if (algo == IEEE80211_AUTH_ALG_LEAP) {
1897 authalgreject(ni, wh, algo,
1898 seq+1, IEEE80211_STATUS_ALG);
1902 * We assume that an unknown algorithm is the result
1903 * of a decryption failure on a shared key auth frame;
1904 * return a status code appropriate for that instead
1905 * of IEEE80211_STATUS_ALG.
1907 * NB: a seq# of 4 is intentional; the decrypted
1908 * frame likely has a bogus seq value.
1910 authalgreject(ni, wh, algo,
1911 4, IEEE80211_STATUS_CHALLENGE);
1917 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1918 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
1919 uint16_t capinfo, lintval;
1920 struct ieee80211_rsnparms rsnparms;
1922 if (vap->iv_state != IEEE80211_S_RUN) {
1923 vap->iv_stats.is_rx_mgtdiscard++;
1926 if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1927 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1928 wh, NULL, "%s", "wrong bssid");
1929 vap->iv_stats.is_rx_assoc_bss++;
1932 if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1934 resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
1937 resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
1939 if (ni == vap->iv_bss) {
1940 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1941 "deny %s request, sta not authenticated",
1942 reassoc ? "reassoc" : "assoc");
1943 ieee80211_send_error(ni, wh->i_addr2,
1944 IEEE80211_FC0_SUBTYPE_DEAUTH,
1945 IEEE80211_REASON_ASSOC_NOT_AUTHED);
1946 vap->iv_stats.is_rx_assoc_notauth++;
1951 * asreq frame format
1952 * [2] capability information
1953 * [2] listen interval
1954 * [6*] current AP address (reassoc only)
1956 * [tlv] supported rates
1957 * [tlv] extended supported rates
1959 * [tlv] HT capabilities
1960 * [tlv] Atheros capabilities
1962 IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4), return);
1963 capinfo = le16toh(*(uint16_t *)frm); frm += 2;
1964 lintval = le16toh(*(uint16_t *)frm); frm += 2;
1966 frm += 6; /* ignore current AP info */
1967 ssid = rates = xrates = wpa = rsn = wme = ath = htcap = NULL;
1969 while (efrm - frm > 1) {
1970 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1972 case IEEE80211_ELEMID_SSID:
1975 case IEEE80211_ELEMID_RATES:
1978 case IEEE80211_ELEMID_XRATES:
1981 case IEEE80211_ELEMID_RSN:
1984 case IEEE80211_ELEMID_HTCAP:
1987 case IEEE80211_ELEMID_VENDOR:
1990 else if (iswmeinfo(frm))
1992 #ifdef IEEE80211_SUPPORT_SUPERG
1993 else if (isatherosoui(frm))
1996 else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1997 if (ishtcapoui(frm) && htcap == NULL)
2004 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
2006 IEEE80211_VERIFY_ELEMENT(xrates,
2007 IEEE80211_RATE_MAXSIZE - rates[1], return);
2008 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2009 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
2010 if (htcap != NULL) {
2011 IEEE80211_VERIFY_LENGTH(htcap[1],
2012 htcap[0] == IEEE80211_ELEMID_VENDOR ?
2013 4 + sizeof(struct ieee80211_ie_htcap)-2 :
2014 sizeof(struct ieee80211_ie_htcap)-2,
2015 return); /* XXX just NULL out? */
2018 if ((vap->iv_flags & IEEE80211_F_WPA) &&
2019 !wpa_assocreq(ni, &rsnparms, wh, wpa, rsn, capinfo))
2021 /* discard challenge after association */
2022 if (ni->ni_challenge != NULL) {
2023 kfree(ni->ni_challenge, M_80211_NODE);
2024 ni->ni_challenge = NULL;
2026 /* NB: 802.11 spec says to ignore station's privacy bit */
2027 if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
2028 capinfomismatch(ni, wh, reassoc, resp,
2029 "capability", capinfo);
2033 * Disallow re-associate w/ invalid slot time setting.
2035 if (ni->ni_associd != 0 &&
2036 IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2037 ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
2038 capinfomismatch(ni, wh, reassoc, resp,
2039 "slot time", capinfo);
2042 rate = ieee80211_setup_rates(ni, rates, xrates,
2043 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
2044 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2045 if (rate & IEEE80211_RATE_BASIC) {
2046 ratesetmismatch(ni, wh, reassoc, resp, "legacy", rate);
2047 vap->iv_stats.is_rx_assoc_norate++;
2051 * If constrained to 11g-only stations reject an
2052 * 11b-only station. We cheat a bit here by looking
2053 * at the max negotiated xmit rate and assuming anyone
2054 * with a best rate <24Mb/s is an 11b station.
2056 if ((vap->iv_flags & IEEE80211_F_PUREG) && rate < 48) {
2057 ratesetmismatch(ni, wh, reassoc, resp, "11g", rate);
2058 vap->iv_stats.is_rx_assoc_norate++;
2062 * Do HT rate set handling and setup HT node state.
2064 ni->ni_chan = vap->iv_bss->ni_chan;
2065 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
2066 rate = ieee80211_setup_htrates(ni, htcap,
2067 IEEE80211_F_DOFMCS | IEEE80211_F_DONEGO |
2069 if (rate & IEEE80211_RATE_BASIC) {
2070 ratesetmismatch(ni, wh, reassoc, resp,
2072 vap->iv_stats.is_ht_assoc_norate++;
2075 ieee80211_ht_node_init(ni);
2076 ieee80211_ht_updatehtcap(ni, htcap);
2077 } else if (ni->ni_flags & IEEE80211_NODE_HT)
2078 ieee80211_ht_node_cleanup(ni);
2079 #ifdef IEEE80211_SUPPORT_SUPERG
2080 else if (ni->ni_ath_flags & IEEE80211_NODE_ATH)
2081 ieee80211_ff_node_cleanup(ni);
2084 * Allow AMPDU operation only with unencrypted traffic
2085 * or AES-CCM; the 11n spec only specifies these ciphers
2086 * so permitting any others is undefined and can lead
2087 * to interoperability problems.
2089 if ((ni->ni_flags & IEEE80211_NODE_HT) &&
2090 (((vap->iv_flags & IEEE80211_F_WPA) &&
2091 rsnparms.rsn_ucastcipher != IEEE80211_CIPHER_AES_CCM) ||
2092 (vap->iv_flags & (IEEE80211_F_WPA|IEEE80211_F_PRIVACY)) == IEEE80211_F_PRIVACY)) {
2094 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
2095 "disallow HT use because WEP or TKIP requested, "
2096 "capinfo 0x%x ucastcipher %d", capinfo,
2097 rsnparms.rsn_ucastcipher);
2098 ieee80211_ht_node_cleanup(ni);
2099 vap->iv_stats.is_ht_assoc_downgrade++;
2102 * If constrained to 11n-only stations reject legacy stations.
2104 if ((vap->iv_flags_ht & IEEE80211_FHT_PUREN) &&
2105 (ni->ni_flags & IEEE80211_NODE_HT) == 0) {
2106 htcapmismatch(ni, wh, reassoc, resp);
2107 vap->iv_stats.is_ht_assoc_nohtcap++;
2110 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
2112 ni->ni_intval = lintval;
2113 ni->ni_capinfo = capinfo;
2114 ni->ni_fhdwell = vap->iv_bss->ni_fhdwell;
2115 ni->ni_fhindex = vap->iv_bss->ni_fhindex;
2118 * XXX maybe better to just expand
2120 if (ieee80211_ies_init(&ni->ni_ies, sfrm, efrm - sfrm)) {
2121 #define setie(_ie, _off) ieee80211_ies_setie(ni->ni_ies, _ie, _off)
2123 setie(wpa_ie, wpa - sfrm);
2125 setie(rsn_ie, rsn - sfrm);
2127 setie(htcap_ie, htcap - sfrm);
2129 setie(wme_ie, wme - sfrm);
2131 * Mark node as capable of QoS.
2133 ni->ni_flags |= IEEE80211_NODE_QOS;
2135 ni->ni_flags &= ~IEEE80211_NODE_QOS;
2136 #ifdef IEEE80211_SUPPORT_SUPERG
2138 setie(ath_ie, ath - sfrm);
2140 * Parse ATH station parameters.
2142 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
2145 ni->ni_ath_flags = 0;
2148 ni->ni_flags &= ~IEEE80211_NODE_QOS;
2149 ni->ni_ath_flags = 0;
2151 ieee80211_node_join(ni, resp);
2152 ieee80211_deliver_l2uf(ni);
2156 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2157 case IEEE80211_FC0_SUBTYPE_DISASSOC: {
2160 if (vap->iv_state != IEEE80211_S_RUN ||
2161 /* NB: can happen when in promiscuous mode */
2162 !IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
2163 vap->iv_stats.is_rx_mgtdiscard++;
2167 * deauth/disassoc frame format
2170 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
2171 reason = le16toh(*(uint16_t *)frm);
2172 if (subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
2173 vap->iv_stats.is_rx_deauth++;
2174 IEEE80211_NODE_STAT(ni, rx_deauth);
2176 vap->iv_stats.is_rx_disassoc++;
2177 IEEE80211_NODE_STAT(ni, rx_disassoc);
2179 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2180 "recv %s (reason %d)", ieee80211_mgt_subtype_name[subtype >>
2181 IEEE80211_FC0_SUBTYPE_SHIFT], reason);
2182 if (ni != vap->iv_bss)
2183 ieee80211_node_leave(ni);
2187 case IEEE80211_FC0_SUBTYPE_ACTION:
2188 if (vap->iv_state == IEEE80211_S_RUN) {
2189 if (ieee80211_parse_action(ni, m0) == 0)
2190 ic->ic_recv_action(ni, wh, frm, efrm);
2192 vap->iv_stats.is_rx_mgtdiscard++;
2195 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2196 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2198 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2199 wh, "mgt", "subtype 0x%x not handled", subtype);
2200 vap->iv_stats.is_rx_badsubtype++;
2206 hostap_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2209 case IEEE80211_FC0_SUBTYPE_PS_POLL:
2210 hostap_recv_pspoll(ni, m);
2212 case IEEE80211_FC0_SUBTYPE_BAR:
2213 ieee80211_recv_bar(ni, m);
2219 * Process a received ps-poll frame.
2222 hostap_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m0)
2224 struct ieee80211vap *vap = ni->ni_vap;
2225 struct ieee80211_frame_min *wh;
2231 wh = mtod(m0, struct ieee80211_frame_min *);
2232 if (ni->ni_associd == 0) {
2233 IEEE80211_DISCARD(vap,
2234 IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2235 (struct ieee80211_frame *) wh, NULL,
2236 "%s", "unassociated station");
2237 vap->iv_stats.is_ps_unassoc++;
2238 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
2239 IEEE80211_REASON_NOT_ASSOCED);
2243 aid = le16toh(*(uint16_t *)wh->i_dur);
2244 if (aid != ni->ni_associd) {
2245 IEEE80211_DISCARD(vap,
2246 IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2247 (struct ieee80211_frame *) wh, NULL,
2248 "aid mismatch: sta aid 0x%x poll aid 0x%x",
2249 ni->ni_associd, aid);
2250 vap->iv_stats.is_ps_badaid++;
2252 * NB: We used to deauth the station but it turns out
2253 * the Blackberry Curve 8230 (and perhaps other devices)
2254 * sometimes send the wrong AID when WME is negotiated.
2255 * Being more lenient here seems ok as we already check
2256 * the station is associated and we only return frames
2257 * queued for the station (i.e. we don't use the AID).
2262 /* Okay, take the first queued packet and put it out... */
2263 m = ieee80211_node_psq_dequeue(ni, &qlen);
2265 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_POWER, wh->i_addr2,
2266 "%s", "recv ps-poll, but queue empty");
2267 ieee80211_send_nulldata(ieee80211_ref_node(ni));
2268 vap->iv_stats.is_ps_qempty++; /* XXX node stat */
2269 if (vap->iv_set_tim != NULL)
2270 vap->iv_set_tim(ni, 0); /* just in case */
2274 * If there are more packets, set the more packets bit
2275 * in the packet dispatched to the station; otherwise
2276 * turn off the TIM bit.
2279 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2280 "recv ps-poll, send packet, %u still queued", qlen);
2281 m->m_flags |= M_MORE_DATA;
2283 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2284 "%s", "recv ps-poll, send packet, queue empty");
2285 if (vap->iv_set_tim != NULL)
2286 vap->iv_set_tim(ni, 0);
2288 m->m_flags |= M_PWR_SAV; /* bypass PS handling */
2290 if (m->m_flags & M_ENCAP)
2291 ifp = vap->iv_ic->ic_ifp;
2294 IF_ENQUEUE(&ifp->if_snd, m);