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);
76 static void hostap_recv_pspoll(struct ieee80211_node *, struct mbuf *);
79 ieee80211_hostap_attach(struct ieee80211com *ic)
81 ic->ic_vattach[IEEE80211_M_HOSTAP] = hostap_vattach;
85 ieee80211_hostap_detach(struct ieee80211com *ic)
90 hostap_vdetach(struct ieee80211vap *vap)
95 hostap_vattach(struct ieee80211vap *vap)
97 vap->iv_newstate = hostap_newstate;
98 vap->iv_input = hostap_input;
99 vap->iv_recv_mgmt = hostap_recv_mgmt;
100 vap->iv_recv_ctl = hostap_recv_ctl;
101 vap->iv_opdetach = hostap_vdetach;
102 vap->iv_deliver_data = hostap_deliver_data;
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_80211_RX | M_MCAST | M_BCAST);
360 KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
361 ("gack, opmode %d", vap->iv_opmode));
366 IEEE80211_NODE_STAT(ni, rx_data);
367 IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
368 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
369 m->m_flags |= M_MCAST; /* XXX M_BCAST? */
370 IEEE80211_NODE_STAT(ni, rx_mcast);
372 IEEE80211_NODE_STAT(ni, rx_ucast);
374 /* perform as a bridge within the AP */
375 if ((vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0) {
376 struct mbuf *mcopy = NULL;
378 if (m->m_flags & M_MCAST) {
379 mcopy = m_dup(m, M_DONTWAIT);
383 mcopy->m_flags |= M_MCAST;
386 * Check if the destination is associated with the
387 * same vap and authorized to receive traffic.
388 * Beware of traffic destined for the vap itself;
389 * sending it will not work; just let it be delivered
392 struct ieee80211_node *sta = ieee80211_find_vap_node(
393 &vap->iv_ic->ic_sta, vap, eh->ether_dhost);
395 if (ieee80211_node_is_authorized(sta)) {
397 * Beware of sending to ourself; this
398 * needs to happen via the normal
401 if (sta != vap->iv_bss) {
406 vap->iv_stats.is_rx_unauth++;
407 IEEE80211_NODE_STAT(sta, rx_unauth);
409 ieee80211_free_node(sta);
414 len = mcopy->m_pkthdr.len;
415 err = ifp->if_transmit(ifp, mcopy);
417 /* NB: IFQ_HANDOFF reclaims mcopy */
425 * Mark frame as coming from vap's interface.
427 m->m_pkthdr.rcvif = ifp;
428 if (m->m_flags & M_MCAST) {
430 * Spam DWDS vap's w/ multicast traffic.
432 /* XXX only if dwds in use? */
433 ieee80211_dwds_mcast(vap, m);
435 if (ni->ni_vlan != 0) {
436 /* attach vlan tag */
437 m->m_pkthdr.ether_vtag = ni->ni_vlan;
438 m->m_flags |= M_VLANTAG;
440 ifp->if_input(ifp, m);
445 * Decide if a received management frame should be
446 * printed when debugging is enabled. This filters some
447 * of the less interesting frames that come frequently
451 doprint(struct ieee80211vap *vap, int subtype)
454 case IEEE80211_FC0_SUBTYPE_BEACON:
455 return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
456 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
463 * Process a received frame. The node associated with the sender
464 * should be supplied. If nothing was found in the node table then
465 * the caller is assumed to supply a reference to iv_bss instead.
466 * The RSSI and a timestamp are also supplied. The RSSI data is used
467 * during AP scanning to select a AP to associate with; it can have
468 * any units so long as values have consistent units and higher values
469 * mean ``better signal''. The receive timestamp is currently not used
470 * by the 802.11 layer.
473 hostap_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
475 #define SEQ_LEQ(a,b) ((int)((a)-(b)) <= 0)
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 ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
576 (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
577 SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
578 /* duplicate, discard */
579 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
581 "seqno <%u,%u> fragno <%u,%u> tid %u",
582 rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
583 ni->ni_rxseqs[tid] >>
584 IEEE80211_SEQ_SEQ_SHIFT,
585 rxseq & IEEE80211_SEQ_FRAG_MASK,
587 IEEE80211_SEQ_FRAG_MASK,
589 vap->iv_stats.is_rx_dup++;
590 IEEE80211_NODE_STAT(ni, rx_dup);
593 ni->ni_rxseqs[tid] = rxseq;
598 case IEEE80211_FC0_TYPE_DATA:
599 hdrspace = ieee80211_hdrspace(ic, wh);
600 if (m->m_len < hdrspace &&
601 (m = m_pullup(m, hdrspace)) == NULL) {
602 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
603 ni->ni_macaddr, NULL,
604 "data too short: expecting %u", hdrspace);
605 vap->iv_stats.is_rx_tooshort++;
608 if (!(dir == IEEE80211_FC1_DIR_TODS ||
609 (dir == IEEE80211_FC1_DIR_DSTODS &&
610 (vap->iv_flags & IEEE80211_F_DWDS)))) {
611 if (dir != IEEE80211_FC1_DIR_DSTODS) {
612 IEEE80211_DISCARD(vap,
613 IEEE80211_MSG_INPUT, wh, "data",
614 "incorrect dir 0x%x", dir);
616 IEEE80211_DISCARD(vap,
617 IEEE80211_MSG_INPUT |
618 IEEE80211_MSG_WDS, wh,
620 "%s", "DWDS not enabled");
622 vap->iv_stats.is_rx_wrongdir++;
625 /* check if source STA is associated */
626 if (ni == vap->iv_bss) {
627 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
628 wh, "data", "%s", "unknown src");
629 ieee80211_send_error(ni, wh->i_addr2,
630 IEEE80211_FC0_SUBTYPE_DEAUTH,
631 IEEE80211_REASON_NOT_AUTHED);
632 vap->iv_stats.is_rx_notassoc++;
635 if (ni->ni_associd == 0) {
636 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
637 wh, "data", "%s", "unassoc src");
638 IEEE80211_SEND_MGMT(ni,
639 IEEE80211_FC0_SUBTYPE_DISASSOC,
640 IEEE80211_REASON_NOT_ASSOCED);
641 vap->iv_stats.is_rx_notassoc++;
646 * Check for power save state change.
647 * XXX out-of-order A-MPDU frames?
649 if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
650 (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
651 ieee80211_node_pwrsave(ni,
652 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
654 * For 4-address packets handle WDS discovery
655 * notifications. Once a WDS link is setup frames
656 * are just delivered to the WDS vap (see below).
658 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap == NULL) {
659 if (!ieee80211_node_is_authorized(ni)) {
660 IEEE80211_DISCARD(vap,
661 IEEE80211_MSG_INPUT |
662 IEEE80211_MSG_WDS, wh,
664 "%s", "unauthorized port");
665 vap->iv_stats.is_rx_unauth++;
666 IEEE80211_NODE_STAT(ni, rx_unauth);
669 ieee80211_dwds_discover(ni, m);
674 * Handle A-MPDU re-ordering. If the frame is to be
675 * processed directly then ieee80211_ampdu_reorder
676 * will return 0; otherwise it has consumed the mbuf
677 * and we should do nothing more with it.
679 if ((m->m_flags & M_AMPDU) &&
680 ieee80211_ampdu_reorder(ni, m) != 0) {
687 * Handle privacy requirements. Note that we
688 * must not be preempted from here until after
689 * we (potentially) call ieee80211_crypto_demic;
690 * otherwise we may violate assumptions in the
691 * crypto cipher modules used to do delayed update
692 * of replay sequence numbers.
694 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
695 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
697 * Discard encrypted frames when privacy is off.
699 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
700 wh, "WEP", "%s", "PRIVACY off");
701 vap->iv_stats.is_rx_noprivacy++;
702 IEEE80211_NODE_STAT(ni, rx_noprivacy);
705 key = ieee80211_crypto_decap(ni, m, hdrspace);
707 /* NB: stats+msgs handled in crypto_decap */
708 IEEE80211_NODE_STAT(ni, rx_wepfail);
711 wh = mtod(m, struct ieee80211_frame *);
712 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
714 /* XXX M_WEP and IEEE80211_F_PRIVACY */
719 * Save QoS bits for use below--before we strip the header.
721 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
722 qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
723 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
724 ((struct ieee80211_qosframe *)wh)->i_qos[0];
729 * Next up, any fragmentation.
731 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
732 m = ieee80211_defrag(ni, m, hdrspace);
734 /* Fragment dropped or frame not complete yet */
738 wh = NULL; /* no longer valid, catch any uses */
741 * Next strip any MSDU crypto bits.
743 if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
744 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
745 ni->ni_macaddr, "data", "%s", "demic error");
746 vap->iv_stats.is_rx_demicfail++;
747 IEEE80211_NODE_STAT(ni, rx_demicfail);
750 /* copy to listener after decrypt */
751 if (ieee80211_radiotap_active_vap(vap))
752 ieee80211_radiotap_rx(vap, m);
755 * Finally, strip the 802.11 header.
757 m = ieee80211_decap(vap, m, hdrspace);
759 /* XXX mask bit to check for both */
760 /* don't count Null data frames as errors */
761 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
762 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
764 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
765 ni->ni_macaddr, "data", "%s", "decap error");
766 vap->iv_stats.is_rx_decap++;
767 IEEE80211_NODE_STAT(ni, rx_decap);
770 eh = mtod(m, struct ether_header *);
771 if (!ieee80211_node_is_authorized(ni)) {
773 * Deny any non-PAE frames received prior to
774 * authorization. For open/shared-key
775 * authentication the port is mark authorized
776 * after authentication completes. For 802.1x
777 * the port is not marked authorized by the
778 * authenticator until the handshake has completed.
780 if (eh->ether_type != htons(ETHERTYPE_PAE)) {
781 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
782 eh->ether_shost, "data",
783 "unauthorized port: ether type 0x%x len %u",
784 eh->ether_type, m->m_pkthdr.len);
785 vap->iv_stats.is_rx_unauth++;
786 IEEE80211_NODE_STAT(ni, rx_unauth);
791 * When denying unencrypted frames, discard
792 * any non-PAE frames received without encryption.
794 if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
795 (key == NULL && (m->m_flags & M_WEP) == 0) &&
796 eh->ether_type != htons(ETHERTYPE_PAE)) {
798 * Drop unencrypted frames.
800 vap->iv_stats.is_rx_unencrypted++;
801 IEEE80211_NODE_STAT(ni, rx_unencrypted);
805 /* XXX require HT? */
806 if (qos & IEEE80211_QOS_AMSDU) {
807 m = ieee80211_decap_amsdu(ni, m);
809 return IEEE80211_FC0_TYPE_DATA;
811 #ifdef IEEE80211_SUPPORT_SUPERG
812 m = ieee80211_decap_fastframe(vap, ni, m);
814 return IEEE80211_FC0_TYPE_DATA;
817 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
818 ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
820 hostap_deliver_data(vap, ni, m);
821 return IEEE80211_FC0_TYPE_DATA;
823 case IEEE80211_FC0_TYPE_MGT:
824 vap->iv_stats.is_rx_mgmt++;
825 IEEE80211_NODE_STAT(ni, rx_mgmt);
826 if (dir != IEEE80211_FC1_DIR_NODS) {
827 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
828 wh, "mgt", "incorrect dir 0x%x", dir);
829 vap->iv_stats.is_rx_wrongdir++;
832 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
833 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
834 ni->ni_macaddr, "mgt", "too short: len %u",
836 vap->iv_stats.is_rx_tooshort++;
839 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
840 /* ensure return frames are unicast */
841 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
842 wh, NULL, "source is multicast: %s",
843 ether_sprintf(wh->i_addr2));
844 vap->iv_stats.is_rx_mgtdiscard++; /* XXX stat */
847 #ifdef IEEE80211_DEBUG
848 if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
849 ieee80211_msg_dumppkts(vap)) {
850 if_printf(ifp, "received %s from %s rssi %d\n",
851 ieee80211_mgt_subtype_name[subtype >>
852 IEEE80211_FC0_SUBTYPE_SHIFT],
853 ether_sprintf(wh->i_addr2), rssi);
856 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
857 if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
859 * Only shared key auth frames with a challenge
860 * should be encrypted, discard all others.
862 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
864 "%s", "WEP set but not permitted");
865 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
868 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
870 * Discard encrypted frames when privacy is off.
872 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
873 wh, NULL, "%s", "WEP set but PRIVACY off");
874 vap->iv_stats.is_rx_noprivacy++;
877 hdrspace = ieee80211_hdrspace(ic, wh);
878 key = ieee80211_crypto_decap(ni, m, hdrspace);
880 /* NB: stats+msgs handled in crypto_decap */
883 wh = mtod(m, struct ieee80211_frame *);
884 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
886 vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
889 case IEEE80211_FC0_TYPE_CTL:
890 vap->iv_stats.is_rx_ctl++;
891 IEEE80211_NODE_STAT(ni, rx_ctrl);
892 vap->iv_recv_ctl(ni, m, subtype);
895 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
896 wh, "bad", "frame type 0x%x", type);
897 /* should not come here */
904 if (need_tap && ieee80211_radiotap_active_vap(vap))
905 ieee80211_radiotap_rx(vap, m);
913 hostap_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
914 int rssi, int nf, uint16_t seq, uint16_t status)
916 struct ieee80211vap *vap = ni->ni_vap;
918 KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
920 if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
921 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
922 ni->ni_macaddr, "open auth",
923 "bad sta auth mode %u", ni->ni_authmode);
924 vap->iv_stats.is_rx_bad_auth++; /* XXX */
926 * Clear any challenge text that may be there if
927 * a previous shared key auth failed and then an
928 * open auth is attempted.
930 if (ni->ni_challenge != NULL) {
931 free(ni->ni_challenge, M_80211_NODE);
932 ni->ni_challenge = NULL;
934 /* XXX hack to workaround calling convention */
935 ieee80211_send_error(ni, wh->i_addr2,
936 IEEE80211_FC0_SUBTYPE_AUTH,
937 (seq + 1) | (IEEE80211_STATUS_ALG<<16));
940 if (seq != IEEE80211_AUTH_OPEN_REQUEST) {
941 vap->iv_stats.is_rx_bad_auth++;
944 /* always accept open authentication requests */
945 if (ni == vap->iv_bss) {
946 ni = ieee80211_dup_bss(vap, wh->i_addr2);
949 } else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
950 (void) ieee80211_ref_node(ni);
952 * Mark the node as referenced to reflect that it's
953 * reference count has been bumped to insure it remains
954 * after the transaction completes.
956 ni->ni_flags |= IEEE80211_NODE_AREF;
958 * Mark the node as requiring a valid association id
959 * before outbound traffic is permitted.
961 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
963 if (vap->iv_acl != NULL &&
964 vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
966 * When the ACL policy is set to RADIUS we defer the
967 * authorization to a user agent. Dispatch an event,
968 * a subsequent MLME call will decide the fate of the
969 * station. If the user agent is not present then the
970 * node will be reclaimed due to inactivity.
972 IEEE80211_NOTE_MAC(vap,
973 IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL, ni->ni_macaddr,
974 "%s", "station authentication defered (radius acl)");
975 ieee80211_notify_node_auth(ni);
977 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
978 IEEE80211_NOTE_MAC(vap,
979 IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni->ni_macaddr,
980 "%s", "station authenticated (open)");
982 * When 802.1x is not in use mark the port
983 * authorized at this point so traffic can flow.
985 if (ni->ni_authmode != IEEE80211_AUTH_8021X)
986 ieee80211_node_authorize(ni);
991 hostap_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
992 uint8_t *frm, uint8_t *efrm, int rssi, int nf,
993 uint16_t seq, uint16_t status)
995 struct ieee80211vap *vap = ni->ni_vap;
997 int allocbs, estatus;
999 KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
1002 * NB: this can happen as we allow pre-shared key
1003 * authentication to be enabled w/o wep being turned
1004 * on so that configuration of these can be done
1005 * in any order. It may be better to enforce the
1006 * ordering in which case this check would just be
1007 * for sanity/consistency.
1009 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
1010 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1011 ni->ni_macaddr, "shared key auth",
1012 "%s", " PRIVACY is disabled");
1013 estatus = IEEE80211_STATUS_ALG;
1017 * Pre-shared key authentication is evil; accept
1018 * it only if explicitly configured (it is supported
1019 * mainly for compatibility with clients like Mac OS X).
1021 if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1022 ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1023 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1024 ni->ni_macaddr, "shared key auth",
1025 "bad sta auth mode %u", ni->ni_authmode);
1026 vap->iv_stats.is_rx_bad_auth++; /* XXX maybe a unique error? */
1027 estatus = IEEE80211_STATUS_ALG;
1032 if (frm + 1 < efrm) {
1033 if ((frm[1] + 2) > (efrm - frm)) {
1034 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1035 ni->ni_macaddr, "shared key auth",
1036 "ie %d/%d too long",
1037 frm[0], (frm[1] + 2) - (efrm - frm));
1038 vap->iv_stats.is_rx_bad_auth++;
1039 estatus = IEEE80211_STATUS_CHALLENGE;
1042 if (*frm == IEEE80211_ELEMID_CHALLENGE)
1047 case IEEE80211_AUTH_SHARED_CHALLENGE:
1048 case IEEE80211_AUTH_SHARED_RESPONSE:
1049 if (challenge == NULL) {
1050 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1051 ni->ni_macaddr, "shared key auth",
1052 "%s", "no challenge");
1053 vap->iv_stats.is_rx_bad_auth++;
1054 estatus = IEEE80211_STATUS_CHALLENGE;
1057 if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1058 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1059 ni->ni_macaddr, "shared key auth",
1060 "bad challenge len %d", challenge[1]);
1061 vap->iv_stats.is_rx_bad_auth++;
1062 estatus = IEEE80211_STATUS_CHALLENGE;
1069 case IEEE80211_AUTH_SHARED_REQUEST:
1070 if (ni == vap->iv_bss) {
1071 ni = ieee80211_dup_bss(vap, wh->i_addr2);
1073 /* NB: no way to return an error */
1078 if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1079 (void) ieee80211_ref_node(ni);
1083 * Mark the node as referenced to reflect that it's
1084 * reference count has been bumped to insure it remains
1085 * after the transaction completes.
1087 ni->ni_flags |= IEEE80211_NODE_AREF;
1089 * Mark the node as requiring a valid associatio id
1090 * before outbound traffic is permitted.
1092 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
1093 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1095 if (!ieee80211_alloc_challenge(ni)) {
1096 /* NB: don't return error so they rexmit */
1099 get_random_bytes(ni->ni_challenge,
1100 IEEE80211_CHALLENGE_LEN);
1101 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1102 ni, "shared key %sauth request", allocbs ? "" : "re");
1104 * When the ACL policy is set to RADIUS we defer the
1105 * authorization to a user agent. Dispatch an event,
1106 * a subsequent MLME call will decide the fate of the
1107 * station. If the user agent is not present then the
1108 * node will be reclaimed due to inactivity.
1110 if (vap->iv_acl != NULL &&
1111 vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
1112 IEEE80211_NOTE_MAC(vap,
1113 IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL,
1115 "%s", "station authentication defered (radius acl)");
1116 ieee80211_notify_node_auth(ni);
1120 case IEEE80211_AUTH_SHARED_RESPONSE:
1121 if (ni == vap->iv_bss) {
1122 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1123 ni->ni_macaddr, "shared key response",
1124 "%s", "unknown station");
1125 /* NB: don't send a response */
1128 if (ni->ni_challenge == NULL) {
1129 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1130 ni->ni_macaddr, "shared key response",
1131 "%s", "no challenge recorded");
1132 vap->iv_stats.is_rx_bad_auth++;
1133 estatus = IEEE80211_STATUS_CHALLENGE;
1136 if (memcmp(ni->ni_challenge, &challenge[2],
1137 challenge[1]) != 0) {
1138 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1139 ni->ni_macaddr, "shared key response",
1140 "%s", "challenge mismatch");
1141 vap->iv_stats.is_rx_auth_fail++;
1142 estatus = IEEE80211_STATUS_CHALLENGE;
1145 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1146 ni, "%s", "station authenticated (shared key)");
1147 ieee80211_node_authorize(ni);
1150 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1151 ni->ni_macaddr, "shared key auth",
1153 vap->iv_stats.is_rx_bad_auth++;
1154 estatus = IEEE80211_STATUS_SEQUENCE;
1157 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1161 * Send an error response; but only when operating as an AP.
1163 /* XXX hack to workaround calling convention */
1164 ieee80211_send_error(ni, wh->i_addr2,
1165 IEEE80211_FC0_SUBTYPE_AUTH,
1166 (seq + 1) | (estatus<<16));
1170 * Convert a WPA cipher selector OUI to an internal
1171 * cipher algorithm. Where appropriate we also
1172 * record any key length.
1175 wpa_cipher(const uint8_t *sel, uint8_t *keylen)
1177 #define WPA_SEL(x) (((x)<<24)|WPA_OUI)
1178 uint32_t w = LE_READ_4(sel);
1181 case WPA_SEL(WPA_CSE_NULL):
1182 return IEEE80211_CIPHER_NONE;
1183 case WPA_SEL(WPA_CSE_WEP40):
1185 *keylen = 40 / NBBY;
1186 return IEEE80211_CIPHER_WEP;
1187 case WPA_SEL(WPA_CSE_WEP104):
1189 *keylen = 104 / NBBY;
1190 return IEEE80211_CIPHER_WEP;
1191 case WPA_SEL(WPA_CSE_TKIP):
1192 return IEEE80211_CIPHER_TKIP;
1193 case WPA_SEL(WPA_CSE_CCMP):
1194 return IEEE80211_CIPHER_AES_CCM;
1196 return 32; /* NB: so 1<< is discarded */
1201 * Convert a WPA key management/authentication algorithm
1202 * to an internal code.
1205 wpa_keymgmt(const uint8_t *sel)
1207 #define WPA_SEL(x) (((x)<<24)|WPA_OUI)
1208 uint32_t w = LE_READ_4(sel);
1211 case WPA_SEL(WPA_ASE_8021X_UNSPEC):
1212 return WPA_ASE_8021X_UNSPEC;
1213 case WPA_SEL(WPA_ASE_8021X_PSK):
1214 return WPA_ASE_8021X_PSK;
1215 case WPA_SEL(WPA_ASE_NONE):
1216 return WPA_ASE_NONE;
1218 return 0; /* NB: so is discarded */
1223 * Parse a WPA information element to collect parameters.
1224 * Note that we do not validate security parameters; that
1225 * is handled by the authenticator; the parsing done here
1226 * is just for internal use in making operational decisions.
1229 ieee80211_parse_wpa(struct ieee80211vap *vap, const uint8_t *frm,
1230 struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1232 uint8_t len = frm[1];
1237 * Check the length once for fixed parts: OUI, type,
1238 * version, mcast cipher, and 2 selector counts.
1239 * Other, variable-length data, must be checked separately.
1241 if ((vap->iv_flags & IEEE80211_F_WPA1) == 0) {
1242 IEEE80211_DISCARD_IE(vap,
1243 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1244 wh, "WPA", "not WPA, flags 0x%x", vap->iv_flags);
1245 return IEEE80211_REASON_IE_INVALID;
1248 IEEE80211_DISCARD_IE(vap,
1249 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1250 wh, "WPA", "too short, len %u", len);
1251 return IEEE80211_REASON_IE_INVALID;
1253 frm += 6, len -= 4; /* NB: len is payload only */
1254 /* NB: iswpaoui already validated the OUI and type */
1256 if (w != WPA_VERSION) {
1257 IEEE80211_DISCARD_IE(vap,
1258 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1259 wh, "WPA", "bad version %u", w);
1260 return IEEE80211_REASON_IE_INVALID;
1264 memset(rsn, 0, sizeof(*rsn));
1266 /* multicast/group cipher */
1267 rsn->rsn_mcastcipher = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
1270 /* unicast ciphers */
1274 IEEE80211_DISCARD_IE(vap,
1275 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1276 wh, "WPA", "ucast cipher data too short; len %u, n %u",
1278 return IEEE80211_REASON_IE_INVALID;
1281 for (; n > 0; n--) {
1282 w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
1285 if (w & (1<<IEEE80211_CIPHER_TKIP))
1286 rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1288 rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1290 /* key management algorithms */
1294 IEEE80211_DISCARD_IE(vap,
1295 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1296 wh, "WPA", "key mgmt alg data too short; len %u, n %u",
1298 return IEEE80211_REASON_IE_INVALID;
1301 for (; n > 0; n--) {
1302 w |= wpa_keymgmt(frm);
1305 if (w & WPA_ASE_8021X_UNSPEC)
1306 rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
1308 rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1310 if (len > 2) /* optional capabilities */
1311 rsn->rsn_caps = LE_READ_2(frm);
1317 * Convert an RSN cipher selector OUI to an internal
1318 * cipher algorithm. Where appropriate we also
1319 * record any key length.
1322 rsn_cipher(const uint8_t *sel, uint8_t *keylen)
1324 #define RSN_SEL(x) (((x)<<24)|RSN_OUI)
1325 uint32_t w = LE_READ_4(sel);
1328 case RSN_SEL(RSN_CSE_NULL):
1329 return IEEE80211_CIPHER_NONE;
1330 case RSN_SEL(RSN_CSE_WEP40):
1332 *keylen = 40 / NBBY;
1333 return IEEE80211_CIPHER_WEP;
1334 case RSN_SEL(RSN_CSE_WEP104):
1336 *keylen = 104 / NBBY;
1337 return IEEE80211_CIPHER_WEP;
1338 case RSN_SEL(RSN_CSE_TKIP):
1339 return IEEE80211_CIPHER_TKIP;
1340 case RSN_SEL(RSN_CSE_CCMP):
1341 return IEEE80211_CIPHER_AES_CCM;
1342 case RSN_SEL(RSN_CSE_WRAP):
1343 return IEEE80211_CIPHER_AES_OCB;
1345 return 32; /* NB: so 1<< is discarded */
1350 * Convert an RSN key management/authentication algorithm
1351 * to an internal code.
1354 rsn_keymgmt(const uint8_t *sel)
1356 #define RSN_SEL(x) (((x)<<24)|RSN_OUI)
1357 uint32_t w = LE_READ_4(sel);
1360 case RSN_SEL(RSN_ASE_8021X_UNSPEC):
1361 return RSN_ASE_8021X_UNSPEC;
1362 case RSN_SEL(RSN_ASE_8021X_PSK):
1363 return RSN_ASE_8021X_PSK;
1364 case RSN_SEL(RSN_ASE_NONE):
1365 return RSN_ASE_NONE;
1367 return 0; /* NB: so is discarded */
1372 * Parse a WPA/RSN information element to collect parameters
1373 * and validate the parameters against what has been
1374 * configured for the system.
1377 ieee80211_parse_rsn(struct ieee80211vap *vap, const uint8_t *frm,
1378 struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1380 uint8_t len = frm[1];
1385 * Check the length once for fixed parts:
1386 * version, mcast cipher, and 2 selector counts.
1387 * Other, variable-length data, must be checked separately.
1389 if ((vap->iv_flags & IEEE80211_F_WPA2) == 0) {
1390 IEEE80211_DISCARD_IE(vap,
1391 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1392 wh, "WPA", "not RSN, flags 0x%x", vap->iv_flags);
1393 return IEEE80211_REASON_IE_INVALID;
1396 IEEE80211_DISCARD_IE(vap,
1397 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1398 wh, "RSN", "too short, len %u", len);
1399 return IEEE80211_REASON_IE_INVALID;
1403 if (w != RSN_VERSION) {
1404 IEEE80211_DISCARD_IE(vap,
1405 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1406 wh, "RSN", "bad version %u", w);
1407 return IEEE80211_REASON_IE_INVALID;
1411 memset(rsn, 0, sizeof(*rsn));
1413 /* multicast/group cipher */
1414 rsn->rsn_mcastcipher = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
1417 /* unicast ciphers */
1421 IEEE80211_DISCARD_IE(vap,
1422 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1423 wh, "RSN", "ucast cipher data too short; len %u, n %u",
1425 return IEEE80211_REASON_IE_INVALID;
1428 for (; n > 0; n--) {
1429 w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
1432 if (w & (1<<IEEE80211_CIPHER_TKIP))
1433 rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1435 rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1437 /* key management algorithms */
1441 IEEE80211_DISCARD_IE(vap,
1442 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1443 wh, "RSN", "key mgmt alg data too short; len %u, n %u",
1445 return IEEE80211_REASON_IE_INVALID;
1448 for (; n > 0; n--) {
1449 w |= rsn_keymgmt(frm);
1452 if (w & RSN_ASE_8021X_UNSPEC)
1453 rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
1455 rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
1457 /* optional RSN capabilities */
1459 rsn->rsn_caps = LE_READ_2(frm);
1466 * WPA/802.11i assocation request processing.
1469 wpa_assocreq(struct ieee80211_node *ni, struct ieee80211_rsnparms *rsnparms,
1470 const struct ieee80211_frame *wh, const uint8_t *wpa,
1471 const uint8_t *rsn, uint16_t capinfo)
1473 struct ieee80211vap *vap = ni->ni_vap;
1477 ni->ni_flags &= ~(IEEE80211_NODE_WPS|IEEE80211_NODE_TSN);
1478 if (wpa == NULL && rsn == NULL) {
1479 if (vap->iv_flags_ext & IEEE80211_FEXT_WPS) {
1481 * W-Fi Protected Setup (WPS) permits
1482 * clients to associate and pass EAPOL frames
1483 * to establish initial credentials.
1485 ni->ni_flags |= IEEE80211_NODE_WPS;
1488 if ((vap->iv_flags_ext & IEEE80211_FEXT_TSN) &&
1489 (capinfo & IEEE80211_CAPINFO_PRIVACY)) {
1491 * Transitional Security Network. Permits clients
1492 * to associate and use WEP while WPA is configured.
1494 ni->ni_flags |= IEEE80211_NODE_TSN;
1497 IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1498 wh, NULL, "%s", "no WPA/RSN IE in association request");
1499 vap->iv_stats.is_rx_assoc_badwpaie++;
1500 reason = IEEE80211_REASON_IE_INVALID;
1503 /* assert right association security credentials */
1504 badwparsn = 0; /* NB: to silence compiler */
1505 switch (vap->iv_flags & IEEE80211_F_WPA) {
1506 case IEEE80211_F_WPA1:
1507 badwparsn = (wpa == NULL);
1509 case IEEE80211_F_WPA2:
1510 badwparsn = (rsn == NULL);
1512 case IEEE80211_F_WPA1|IEEE80211_F_WPA2:
1513 badwparsn = (wpa == NULL && rsn == NULL);
1517 IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1519 "%s", "missing WPA/RSN IE in association request");
1520 vap->iv_stats.is_rx_assoc_badwpaie++;
1521 reason = IEEE80211_REASON_IE_INVALID;
1525 * Parse WPA/RSN information element.
1528 reason = ieee80211_parse_wpa(vap, wpa, rsnparms, wh);
1530 reason = ieee80211_parse_rsn(vap, rsn, rsnparms, wh);
1532 /* XXX distinguish WPA/RSN? */
1533 vap->iv_stats.is_rx_assoc_badwpaie++;
1536 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA, ni,
1537 "%s ie: mc %u/%u uc %u/%u key %u caps 0x%x",
1538 wpa != NULL ? "WPA" : "RSN",
1539 rsnparms->rsn_mcastcipher, rsnparms->rsn_mcastkeylen,
1540 rsnparms->rsn_ucastcipher, rsnparms->rsn_ucastkeylen,
1541 rsnparms->rsn_keymgmt, rsnparms->rsn_caps);
1545 ieee80211_node_deauth(ni, reason);
1549 /* XXX find a better place for definition */
1550 struct l2_update_frame {
1551 struct ether_header eh;
1559 * Deliver a TGf L2UF frame on behalf of a station.
1560 * This primes any bridge when the station is roaming
1561 * between ap's on the same wired network.
1564 ieee80211_deliver_l2uf(struct ieee80211_node *ni)
1566 struct ieee80211vap *vap = ni->ni_vap;
1567 struct ifnet *ifp = vap->iv_ifp;
1569 struct l2_update_frame *l2uf;
1570 struct ether_header *eh;
1572 m = m_gethdr(M_NOWAIT, MT_DATA);
1574 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1575 "%s", "no mbuf for l2uf frame");
1576 vap->iv_stats.is_rx_nobuf++; /* XXX not right */
1579 l2uf = mtod(m, struct l2_update_frame *);
1581 /* dst: Broadcast address */
1582 IEEE80211_ADDR_COPY(eh->ether_dhost, ifp->if_broadcastaddr);
1583 /* src: associated STA */
1584 IEEE80211_ADDR_COPY(eh->ether_shost, ni->ni_macaddr);
1585 eh->ether_type = htons(sizeof(*l2uf) - sizeof(*eh));
1589 l2uf->control = 0xf5;
1590 l2uf->xid[0] = 0x81;
1591 l2uf->xid[1] = 0x80;
1592 l2uf->xid[2] = 0x00;
1594 m->m_pkthdr.len = m->m_len = sizeof(*l2uf);
1595 hostap_deliver_data(vap, ni, m);
1599 ratesetmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1600 int reassoc, int resp, const char *tag, int rate)
1602 IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1603 "deny %s request, %s rate set mismatch, rate/MCS %d",
1604 reassoc ? "reassoc" : "assoc", tag, rate & IEEE80211_RATE_VAL);
1605 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_BASIC_RATE);
1606 ieee80211_node_leave(ni);
1610 capinfomismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1611 int reassoc, int resp, const char *tag, int capinfo)
1613 struct ieee80211vap *vap = ni->ni_vap;
1615 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1616 "deny %s request, %s mismatch 0x%x",
1617 reassoc ? "reassoc" : "assoc", tag, capinfo);
1618 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_CAPINFO);
1619 ieee80211_node_leave(ni);
1620 vap->iv_stats.is_rx_assoc_capmismatch++;
1624 htcapmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1625 int reassoc, int resp)
1627 IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1628 "deny %s request, %s missing HT ie", reassoc ? "reassoc" : "assoc");
1629 /* XXX no better code */
1630 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_MISSING_HT_CAPS);
1631 ieee80211_node_leave(ni);
1635 authalgreject(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1636 int algo, int seq, int status)
1638 struct ieee80211vap *vap = ni->ni_vap;
1640 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1641 wh, NULL, "unsupported alg %d", algo);
1642 vap->iv_stats.is_rx_auth_unsupported++;
1643 ieee80211_send_error(ni, wh->i_addr2, IEEE80211_FC0_SUBTYPE_AUTH,
1644 seq | (status << 16));
1648 ishtmixed(const uint8_t *ie)
1650 const struct ieee80211_ie_htinfo *ht =
1651 (const struct ieee80211_ie_htinfo *) ie;
1652 return (ht->hi_byte2 & IEEE80211_HTINFO_OPMODE) ==
1653 IEEE80211_HTINFO_OPMODE_MIXED;
1657 is11bclient(const uint8_t *rates, const uint8_t *xrates)
1659 static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
1662 /* NB: the 11b clients we care about will not have xrates */
1663 if (xrates != NULL || rates == NULL)
1665 for (i = 0; i < rates[1]; i++) {
1666 int r = rates[2+i] & IEEE80211_RATE_VAL;
1667 if (r > 2*11 || ((1<<r) & brates) == 0)
1674 hostap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1675 int subtype, int rssi, int nf)
1677 struct ieee80211vap *vap = ni->ni_vap;
1678 struct ieee80211com *ic = ni->ni_ic;
1679 struct ieee80211_frame *wh;
1680 uint8_t *frm, *efrm, *sfrm;
1681 uint8_t *ssid, *rates, *xrates, *wpa, *rsn, *wme, *ath, *htcap;
1685 wh = mtod(m0, struct ieee80211_frame *);
1686 frm = (uint8_t *)&wh[1];
1687 efrm = mtod(m0, uint8_t *) + m0->m_len;
1689 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1690 case IEEE80211_FC0_SUBTYPE_BEACON: {
1691 struct ieee80211_scanparams scan;
1693 * We process beacon/probe response frames when scanning;
1694 * otherwise we check beacon frames for overlapping non-ERP
1695 * BSS in 11g and/or overlapping legacy BSS when in HT.
1697 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
1698 subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
1699 vap->iv_stats.is_rx_mgtdiscard++;
1702 /* NB: accept off-channel frames */
1703 if (ieee80211_parse_beacon(ni, m0, &scan) &~ IEEE80211_BPARSE_OFFCHAN)
1706 * Count frame now that we know it's to be processed.
1708 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1709 vap->iv_stats.is_rx_beacon++; /* XXX remove */
1710 IEEE80211_NODE_STAT(ni, rx_beacons);
1712 IEEE80211_NODE_STAT(ni, rx_proberesp);
1714 * If scanning, just pass information to the scan module.
1716 if (ic->ic_flags & IEEE80211_F_SCAN) {
1717 if (scan.status == 0 && /* NB: on channel */
1718 (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN)) {
1720 * Actively scanning a channel marked passive;
1721 * send a probe request now that we know there
1722 * is 802.11 traffic present.
1724 * XXX check if the beacon we recv'd gives
1725 * us what we need and suppress the probe req
1727 ieee80211_probe_curchan(vap, 1);
1728 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1730 ieee80211_add_scan(vap, &scan, wh, subtype, rssi, nf);
1734 * Check beacon for overlapping bss w/ non ERP stations.
1735 * If we detect one and protection is configured but not
1736 * enabled, enable it and start a timer that'll bring us
1737 * out if we stop seeing the bss.
1739 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1740 scan.status == 0 && /* NB: on-channel */
1741 ((scan.erp & 0x100) == 0 || /* NB: no ERP, 11b sta*/
1742 (scan.erp & IEEE80211_ERP_NON_ERP_PRESENT))) {
1743 ic->ic_lastnonerp = ticks;
1744 ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
1745 if (ic->ic_protmode != IEEE80211_PROT_NONE &&
1746 (ic->ic_flags & IEEE80211_F_USEPROT) == 0) {
1747 IEEE80211_NOTE_FRAME(vap,
1748 IEEE80211_MSG_ASSOC, wh,
1749 "non-ERP present on channel %d "
1750 "(saw erp 0x%x from channel %d), "
1751 "enable use of protection",
1752 ic->ic_curchan->ic_ieee,
1753 scan.erp, scan.chan);
1754 ic->ic_flags |= IEEE80211_F_USEPROT;
1755 ieee80211_notify_erp(ic);
1759 * Check beacon for non-HT station on HT channel
1760 * and update HT BSS occupancy as appropriate.
1762 if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) {
1763 if (scan.status & IEEE80211_BPARSE_OFFCHAN) {
1765 * Off control channel; only check frames
1766 * that come in the extension channel when
1767 * operating w/ HT40.
1769 if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
1771 if (scan.chan != ic->ic_curchan->ic_extieee)
1774 if (scan.htinfo == NULL) {
1775 ieee80211_htprot_update(ic,
1776 IEEE80211_HTINFO_OPMODE_PROTOPT |
1777 IEEE80211_HTINFO_NONHT_PRESENT);
1778 } else if (ishtmixed(scan.htinfo)) {
1779 /* XXX? take NONHT_PRESENT from beacon? */
1780 ieee80211_htprot_update(ic,
1781 IEEE80211_HTINFO_OPMODE_MIXED |
1782 IEEE80211_HTINFO_NONHT_PRESENT);
1788 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1789 if (vap->iv_state != IEEE80211_S_RUN) {
1790 vap->iv_stats.is_rx_mgtdiscard++;
1794 * prreq frame format
1796 * [tlv] supported rates
1797 * [tlv] extended supported rates
1799 ssid = rates = xrates = NULL;
1801 while (efrm - frm > 1) {
1802 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1804 case IEEE80211_ELEMID_SSID:
1807 case IEEE80211_ELEMID_RATES:
1810 case IEEE80211_ELEMID_XRATES:
1816 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1818 IEEE80211_VERIFY_ELEMENT(xrates,
1819 IEEE80211_RATE_MAXSIZE - rates[1], return);
1820 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1821 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
1822 if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
1823 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1825 "%s", "no ssid with ssid suppression enabled");
1826 vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
1830 /* XXX find a better class or define it's own */
1831 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1832 "%s", "recv probe req");
1834 * Some legacy 11b clients cannot hack a complete
1835 * probe response frame. When the request includes
1836 * only a bare-bones rate set, communicate this to
1837 * the transmit side.
1839 ieee80211_send_proberesp(vap, wh->i_addr2,
1840 is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
1843 case IEEE80211_FC0_SUBTYPE_AUTH: {
1844 uint16_t algo, seq, status;
1846 if (vap->iv_state != IEEE80211_S_RUN) {
1847 vap->iv_stats.is_rx_mgtdiscard++;
1850 if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1851 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1852 wh, NULL, "%s", "wrong bssid");
1853 vap->iv_stats.is_rx_wrongbss++; /*XXX unique stat?*/
1863 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1864 algo = le16toh(*(uint16_t *)frm);
1865 seq = le16toh(*(uint16_t *)(frm + 2));
1866 status = le16toh(*(uint16_t *)(frm + 4));
1867 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1868 "recv auth frame with algorithm %d seq %d", algo, seq);
1870 * Consult the ACL policy module if setup.
1872 if (vap->iv_acl != NULL &&
1873 !vap->iv_acl->iac_check(vap, wh->i_addr2)) {
1874 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1875 wh, NULL, "%s", "disallowed by ACL");
1876 vap->iv_stats.is_rx_acl++;
1877 ieee80211_send_error(ni, wh->i_addr2,
1878 IEEE80211_FC0_SUBTYPE_AUTH,
1879 (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
1882 if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1883 IEEE80211_DISCARD(vap,
1884 IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1885 wh, NULL, "%s", "TKIP countermeasures enabled");
1886 vap->iv_stats.is_rx_auth_countermeasures++;
1887 ieee80211_send_error(ni, wh->i_addr2,
1888 IEEE80211_FC0_SUBTYPE_AUTH,
1889 IEEE80211_REASON_MIC_FAILURE);
1892 if (algo == IEEE80211_AUTH_ALG_SHARED)
1893 hostap_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1895 else if (algo == IEEE80211_AUTH_ALG_OPEN)
1896 hostap_auth_open(ni, wh, rssi, nf, seq, status);
1897 else if (algo == IEEE80211_AUTH_ALG_LEAP) {
1898 authalgreject(ni, wh, algo,
1899 seq+1, IEEE80211_STATUS_ALG);
1903 * We assume that an unknown algorithm is the result
1904 * of a decryption failure on a shared key auth frame;
1905 * return a status code appropriate for that instead
1906 * of IEEE80211_STATUS_ALG.
1908 * NB: a seq# of 4 is intentional; the decrypted
1909 * frame likely has a bogus seq value.
1911 authalgreject(ni, wh, algo,
1912 4, IEEE80211_STATUS_CHALLENGE);
1918 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1919 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
1920 uint16_t capinfo, lintval;
1921 struct ieee80211_rsnparms rsnparms;
1923 if (vap->iv_state != IEEE80211_S_RUN) {
1924 vap->iv_stats.is_rx_mgtdiscard++;
1927 if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1928 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1929 wh, NULL, "%s", "wrong bssid");
1930 vap->iv_stats.is_rx_assoc_bss++;
1933 if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1935 resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
1938 resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
1940 if (ni == vap->iv_bss) {
1941 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1942 "deny %s request, sta not authenticated",
1943 reassoc ? "reassoc" : "assoc");
1944 ieee80211_send_error(ni, wh->i_addr2,
1945 IEEE80211_FC0_SUBTYPE_DEAUTH,
1946 IEEE80211_REASON_ASSOC_NOT_AUTHED);
1947 vap->iv_stats.is_rx_assoc_notauth++;
1952 * asreq frame format
1953 * [2] capability information
1954 * [2] listen interval
1955 * [6*] current AP address (reassoc only)
1957 * [tlv] supported rates
1958 * [tlv] extended supported rates
1960 * [tlv] HT capabilities
1961 * [tlv] Atheros capabilities
1963 IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4), return);
1964 capinfo = le16toh(*(uint16_t *)frm); frm += 2;
1965 lintval = le16toh(*(uint16_t *)frm); frm += 2;
1967 frm += 6; /* ignore current AP info */
1968 ssid = rates = xrates = wpa = rsn = wme = ath = htcap = NULL;
1970 while (efrm - frm > 1) {
1971 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1973 case IEEE80211_ELEMID_SSID:
1976 case IEEE80211_ELEMID_RATES:
1979 case IEEE80211_ELEMID_XRATES:
1982 case IEEE80211_ELEMID_RSN:
1985 case IEEE80211_ELEMID_HTCAP:
1988 case IEEE80211_ELEMID_VENDOR:
1991 else if (iswmeinfo(frm))
1993 #ifdef IEEE80211_SUPPORT_SUPERG
1994 else if (isatherosoui(frm))
1997 else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1998 if (ishtcapoui(frm) && htcap == NULL)
2005 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
2007 IEEE80211_VERIFY_ELEMENT(xrates,
2008 IEEE80211_RATE_MAXSIZE - rates[1], return);
2009 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2010 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
2011 if (htcap != NULL) {
2012 IEEE80211_VERIFY_LENGTH(htcap[1],
2013 htcap[0] == IEEE80211_ELEMID_VENDOR ?
2014 4 + sizeof(struct ieee80211_ie_htcap)-2 :
2015 sizeof(struct ieee80211_ie_htcap)-2,
2016 return); /* XXX just NULL out? */
2019 if ((vap->iv_flags & IEEE80211_F_WPA) &&
2020 !wpa_assocreq(ni, &rsnparms, wh, wpa, rsn, capinfo))
2022 /* discard challenge after association */
2023 if (ni->ni_challenge != NULL) {
2024 free(ni->ni_challenge, M_80211_NODE);
2025 ni->ni_challenge = NULL;
2027 /* NB: 802.11 spec says to ignore station's privacy bit */
2028 if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
2029 capinfomismatch(ni, wh, reassoc, resp,
2030 "capability", capinfo);
2034 * Disallow re-associate w/ invalid slot time setting.
2036 if (ni->ni_associd != 0 &&
2037 IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2038 ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
2039 capinfomismatch(ni, wh, reassoc, resp,
2040 "slot time", capinfo);
2043 rate = ieee80211_setup_rates(ni, rates, xrates,
2044 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
2045 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2046 if (rate & IEEE80211_RATE_BASIC) {
2047 ratesetmismatch(ni, wh, reassoc, resp, "legacy", rate);
2048 vap->iv_stats.is_rx_assoc_norate++;
2052 * If constrained to 11g-only stations reject an
2053 * 11b-only station. We cheat a bit here by looking
2054 * at the max negotiated xmit rate and assuming anyone
2055 * with a best rate <24Mb/s is an 11b station.
2057 if ((vap->iv_flags & IEEE80211_F_PUREG) && rate < 48) {
2058 ratesetmismatch(ni, wh, reassoc, resp, "11g", rate);
2059 vap->iv_stats.is_rx_assoc_norate++;
2063 * Do HT rate set handling and setup HT node state.
2065 ni->ni_chan = vap->iv_bss->ni_chan;
2066 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
2067 rate = ieee80211_setup_htrates(ni, htcap,
2068 IEEE80211_F_DOFMCS | IEEE80211_F_DONEGO |
2070 if (rate & IEEE80211_RATE_BASIC) {
2071 ratesetmismatch(ni, wh, reassoc, resp,
2073 vap->iv_stats.is_ht_assoc_norate++;
2076 ieee80211_ht_node_init(ni);
2077 ieee80211_ht_updatehtcap(ni, htcap);
2078 } else if (ni->ni_flags & IEEE80211_NODE_HT)
2079 ieee80211_ht_node_cleanup(ni);
2080 #ifdef IEEE80211_SUPPORT_SUPERG
2081 else if (ni->ni_ath_flags & IEEE80211_NODE_ATH)
2082 ieee80211_ff_node_cleanup(ni);
2085 * Allow AMPDU operation only with unencrypted traffic
2086 * or AES-CCM; the 11n spec only specifies these ciphers
2087 * so permitting any others is undefined and can lead
2088 * to interoperability problems.
2090 if ((ni->ni_flags & IEEE80211_NODE_HT) &&
2091 (((vap->iv_flags & IEEE80211_F_WPA) &&
2092 rsnparms.rsn_ucastcipher != IEEE80211_CIPHER_AES_CCM) ||
2093 (vap->iv_flags & (IEEE80211_F_WPA|IEEE80211_F_PRIVACY)) == IEEE80211_F_PRIVACY)) {
2095 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
2096 "disallow HT use because WEP or TKIP requested, "
2097 "capinfo 0x%x ucastcipher %d", capinfo,
2098 rsnparms.rsn_ucastcipher);
2099 ieee80211_ht_node_cleanup(ni);
2100 vap->iv_stats.is_ht_assoc_downgrade++;
2103 * If constrained to 11n-only stations reject legacy stations.
2105 if ((vap->iv_flags_ht & IEEE80211_FHT_PUREN) &&
2106 (ni->ni_flags & IEEE80211_NODE_HT) == 0) {
2107 htcapmismatch(ni, wh, reassoc, resp);
2108 vap->iv_stats.is_ht_assoc_nohtcap++;
2111 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
2113 ni->ni_intval = lintval;
2114 ni->ni_capinfo = capinfo;
2115 ni->ni_fhdwell = vap->iv_bss->ni_fhdwell;
2116 ni->ni_fhindex = vap->iv_bss->ni_fhindex;
2119 * XXX maybe better to just expand
2121 if (ieee80211_ies_init(&ni->ni_ies, sfrm, efrm - sfrm)) {
2122 #define setie(_ie, _off) ieee80211_ies_setie(ni->ni_ies, _ie, _off)
2124 setie(wpa_ie, wpa - sfrm);
2126 setie(rsn_ie, rsn - sfrm);
2128 setie(htcap_ie, htcap - sfrm);
2130 setie(wme_ie, wme - sfrm);
2132 * Mark node as capable of QoS.
2134 ni->ni_flags |= IEEE80211_NODE_QOS;
2136 ni->ni_flags &= ~IEEE80211_NODE_QOS;
2137 #ifdef IEEE80211_SUPPORT_SUPERG
2139 setie(ath_ie, ath - sfrm);
2141 * Parse ATH station parameters.
2143 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
2146 ni->ni_ath_flags = 0;
2149 ni->ni_flags &= ~IEEE80211_NODE_QOS;
2150 ni->ni_ath_flags = 0;
2152 ieee80211_node_join(ni, resp);
2153 ieee80211_deliver_l2uf(ni);
2157 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2158 case IEEE80211_FC0_SUBTYPE_DISASSOC: {
2161 if (vap->iv_state != IEEE80211_S_RUN ||
2162 /* NB: can happen when in promiscuous mode */
2163 !IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
2164 vap->iv_stats.is_rx_mgtdiscard++;
2168 * deauth/disassoc frame format
2171 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
2172 reason = le16toh(*(uint16_t *)frm);
2173 if (subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
2174 vap->iv_stats.is_rx_deauth++;
2175 IEEE80211_NODE_STAT(ni, rx_deauth);
2177 vap->iv_stats.is_rx_disassoc++;
2178 IEEE80211_NODE_STAT(ni, rx_disassoc);
2180 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2181 "recv %s (reason %d)", ieee80211_mgt_subtype_name[subtype >>
2182 IEEE80211_FC0_SUBTYPE_SHIFT], reason);
2183 if (ni != vap->iv_bss)
2184 ieee80211_node_leave(ni);
2188 case IEEE80211_FC0_SUBTYPE_ACTION:
2189 if (vap->iv_state == IEEE80211_S_RUN) {
2190 if (ieee80211_parse_action(ni, m0) == 0)
2191 ic->ic_recv_action(ni, wh, frm, efrm);
2193 vap->iv_stats.is_rx_mgtdiscard++;
2196 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2197 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2199 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2200 wh, "mgt", "subtype 0x%x not handled", subtype);
2201 vap->iv_stats.is_rx_badsubtype++;
2207 hostap_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2210 case IEEE80211_FC0_SUBTYPE_PS_POLL:
2211 hostap_recv_pspoll(ni, m);
2213 case IEEE80211_FC0_SUBTYPE_BAR:
2214 ieee80211_recv_bar(ni, m);
2220 * Process a received ps-poll frame.
2223 hostap_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m0)
2225 struct ieee80211vap *vap = ni->ni_vap;
2226 struct ieee80211_frame_min *wh;
2232 wh = mtod(m0, struct ieee80211_frame_min *);
2233 if (ni->ni_associd == 0) {
2234 IEEE80211_DISCARD(vap,
2235 IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2236 (struct ieee80211_frame *) wh, NULL,
2237 "%s", "unassociated station");
2238 vap->iv_stats.is_ps_unassoc++;
2239 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
2240 IEEE80211_REASON_NOT_ASSOCED);
2244 aid = le16toh(*(uint16_t *)wh->i_dur);
2245 if (aid != ni->ni_associd) {
2246 IEEE80211_DISCARD(vap,
2247 IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2248 (struct ieee80211_frame *) wh, NULL,
2249 "aid mismatch: sta aid 0x%x poll aid 0x%x",
2250 ni->ni_associd, aid);
2251 vap->iv_stats.is_ps_badaid++;
2253 * NB: We used to deauth the station but it turns out
2254 * the Blackberry Curve 8230 (and perhaps other devices)
2255 * sometimes send the wrong AID when WME is negotiated.
2256 * Being more lenient here seems ok as we already check
2257 * the station is associated and we only return frames
2258 * queued for the station (i.e. we don't use the AID).
2263 /* Okay, take the first queued packet and put it out... */
2264 m = ieee80211_node_psq_dequeue(ni, &qlen);
2266 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_POWER, wh->i_addr2,
2267 "%s", "recv ps-poll, but queue empty");
2268 ieee80211_send_nulldata(ieee80211_ref_node(ni));
2269 vap->iv_stats.is_ps_qempty++; /* XXX node stat */
2270 if (vap->iv_set_tim != NULL)
2271 vap->iv_set_tim(ni, 0); /* just in case */
2275 * If there are more packets, set the more packets bit
2276 * in the packet dispatched to the station; otherwise
2277 * turn off the TIM bit.
2280 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2281 "recv ps-poll, send packet, %u still queued", qlen);
2282 m->m_flags |= M_MORE_DATA;
2284 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2285 "%s", "recv ps-poll, send packet, queue empty");
2286 if (vap->iv_set_tim != NULL)
2287 vap->iv_set_tim(ni, 0);
2289 m->m_flags |= M_PWR_SAV; /* bypass PS handling */
2291 if (m->m_flags & M_ENCAP)
2292 ifp = vap->iv_ic->ic_ifp;
2295 IF_ENQUEUE(&ifp->if_snd, m);