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_var.h>
52 #include <net/if_media.h>
53 #include <net/if_llc.h>
54 #include <net/ethernet.h>
58 #include <net80211/ieee80211_var.h>
59 #include <net80211/ieee80211_hostap.h>
60 #include <net80211/ieee80211_input.h>
61 #ifdef IEEE80211_SUPPORT_SUPERG
62 #include <net80211/ieee80211_superg.h>
64 #include <net80211/ieee80211_wds.h>
66 #define IEEE80211_RATE2MBS(r) (((r) & IEEE80211_RATE_VAL) / 2)
68 static void hostap_vattach(struct ieee80211vap *);
69 static int hostap_newstate(struct ieee80211vap *, enum ieee80211_state, int);
70 static int hostap_input(struct ieee80211_node *ni, struct mbuf *m,
72 static void hostap_deliver_data(struct ieee80211vap *,
73 struct ieee80211_node *, struct mbuf *);
74 static void hostap_recv_mgmt(struct ieee80211_node *, struct mbuf *,
75 int subtype, int rssi, int nf);
76 static void hostap_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
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;
103 vap->iv_recv_pspoll = ieee80211_recv_pspoll;
107 sta_disassoc(void *arg, struct ieee80211_node *ni)
109 struct ieee80211vap *vap = arg;
111 if (ni->ni_vap == vap && ni->ni_associd != 0) {
112 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
113 IEEE80211_REASON_ASSOC_LEAVE);
114 ieee80211_node_leave(ni);
119 sta_csa(void *arg, struct ieee80211_node *ni)
121 struct ieee80211vap *vap = arg;
123 if (ni->ni_vap == vap && ni->ni_associd != 0)
124 if (ni->ni_inact > vap->iv_inact_init) {
125 ni->ni_inact = vap->iv_inact_init;
126 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
127 "%s: inact %u", __func__, ni->ni_inact);
132 sta_drop(void *arg, struct ieee80211_node *ni)
134 struct ieee80211vap *vap = arg;
136 if (ni->ni_vap == vap && ni->ni_associd != 0)
137 ieee80211_node_leave(ni);
141 * Does a channel change require associated stations to re-associate
142 * so protocol state is correct. This is used when doing CSA across
143 * bands or similar (e.g. HT -> legacy).
146 isbandchange(struct ieee80211com *ic)
148 return ((ic->ic_bsschan->ic_flags ^ ic->ic_csa_newchan->ic_flags) &
149 (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_HALF |
150 IEEE80211_CHAN_QUARTER | IEEE80211_CHAN_HT)) != 0;
154 * IEEE80211_M_HOSTAP vap state machine handler.
157 hostap_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
159 struct ieee80211com *ic = vap->iv_ic;
160 enum ieee80211_state ostate;
162 IEEE80211_LOCK_ASSERT(ic);
164 ostate = vap->iv_state;
165 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
166 __func__, ieee80211_state_name[ostate],
167 ieee80211_state_name[nstate], arg);
168 vap->iv_state = nstate; /* state transition */
169 if (ostate != IEEE80211_S_SCAN)
170 ieee80211_cancel_scan(vap); /* background scan */
172 case IEEE80211_S_INIT:
174 case IEEE80211_S_SCAN:
175 ieee80211_cancel_scan(vap);
177 case IEEE80211_S_CAC:
178 ieee80211_dfs_cac_stop(vap);
180 case IEEE80211_S_RUN:
181 ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
186 if (ostate != IEEE80211_S_INIT) {
187 /* NB: optimize INIT -> INIT case */
188 ieee80211_reset_bss(vap);
190 if (vap->iv_auth->ia_detach != NULL)
191 vap->iv_auth->ia_detach(vap);
193 case IEEE80211_S_SCAN:
195 case IEEE80211_S_CSA:
196 case IEEE80211_S_RUN:
197 ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
199 * Clear overlapping BSS state; the beacon frame
200 * will be reconstructed on transition to the RUN
201 * state and the timeout routines check if the flag
202 * is set before doing anything so this is sufficient.
204 ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
205 ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
207 case IEEE80211_S_CAC:
209 * NB: We may get here because of a manual channel
210 * change in which case we need to stop CAC
211 * XXX no need to stop if ostate RUN but it's ok
213 ieee80211_dfs_cac_stop(vap);
215 case IEEE80211_S_INIT:
216 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
217 !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
219 * Already have a channel; bypass the
220 * scan and startup immediately.
221 * ieee80211_create_ibss will call back to
222 * move us to RUN state.
224 ieee80211_create_ibss(vap, vap->iv_des_chan);
228 * Initiate a scan. We can come here as a result
229 * of an IEEE80211_IOC_SCAN_REQ too in which case
230 * the vap will be marked with IEEE80211_FEXT_SCANREQ
231 * and the scan request parameters will be present
232 * in iv_scanreq. Otherwise we do the default.
234 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
235 ieee80211_check_scan(vap,
236 vap->iv_scanreq_flags,
237 vap->iv_scanreq_duration,
238 vap->iv_scanreq_mindwell,
239 vap->iv_scanreq_maxdwell,
240 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
241 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
243 ieee80211_check_scan_current(vap);
245 case IEEE80211_S_SCAN:
247 * A state change requires a reset; scan.
249 ieee80211_check_scan_current(vap);
255 case IEEE80211_S_CAC:
257 * Start CAC on a DFS channel. We come here when starting
258 * a bss on a DFS channel (see ieee80211_create_ibss).
260 ieee80211_dfs_cac_start(vap);
262 case IEEE80211_S_RUN:
263 if (vap->iv_flags & IEEE80211_F_WPA) {
264 /* XXX validate prerequisites */
267 case IEEE80211_S_INIT:
269 * Already have a channel; bypass the
270 * scan and startup immediately.
271 * Note that ieee80211_create_ibss will call
272 * back to do a RUN->RUN state change.
274 ieee80211_create_ibss(vap,
275 ieee80211_ht_adjust_channel(ic,
276 ic->ic_curchan, vap->iv_flags_ht));
277 /* NB: iv_bss is changed on return */
279 case IEEE80211_S_CAC:
281 * NB: This is the normal state change when CAC
282 * expires and no radar was detected; no need to
283 * clear the CAC timer as it's already expired.
286 case IEEE80211_S_CSA:
288 * Shorten inactivity timer of associated stations
289 * to weed out sta's that don't follow a CSA.
291 ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
293 * Update bss node channel to reflect where
294 * we landed after CSA.
296 ieee80211_node_set_chan(vap->iv_bss,
297 ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
298 ieee80211_htchanflags(vap->iv_bss->ni_chan)));
299 /* XXX bypass debug msgs */
301 case IEEE80211_S_SCAN:
302 case IEEE80211_S_RUN:
303 #ifdef IEEE80211_DEBUG
304 if (ieee80211_msg_debug(vap)) {
305 struct ieee80211_node *ni = vap->iv_bss;
307 "synchronized with %s ssid ",
308 ether_sprintf(ni->ni_bssid));
309 ieee80211_print_essid(ni->ni_essid,
312 printf(" channel %d start %uMb\n",
313 ieee80211_chan2ieee(ic, ic->ic_curchan),
314 IEEE80211_RATE2MBS(ni->ni_txrate));
322 * Start/stop the authenticator. We delay until here
323 * to allow configuration to happen out of order.
325 if (vap->iv_auth->ia_attach != NULL) {
326 /* XXX check failure */
327 vap->iv_auth->ia_attach(vap);
328 } else if (vap->iv_auth->ia_detach != NULL) {
329 vap->iv_auth->ia_detach(vap);
331 ieee80211_node_authorize(vap->iv_bss);
333 case IEEE80211_S_CSA:
334 if (ostate == IEEE80211_S_RUN && isbandchange(ic)) {
336 * On a ``band change'' silently drop associated
337 * stations as they must re-associate before they
338 * can pass traffic (as otherwise protocol state
339 * such as capabilities and the negotiated rate
340 * set may/will be wrong).
342 ieee80211_iterate_nodes(&ic->ic_sta, sta_drop, vap);
352 hostap_deliver_data(struct ieee80211vap *vap,
353 struct ieee80211_node *ni, struct mbuf *m)
355 struct ether_header *eh = mtod(m, struct ether_header *);
356 struct ifnet *ifp = vap->iv_ifp;
358 /* clear driver/net80211 flags before passing up */
359 #if __FreeBSD_version >= 1000046
360 m->m_flags &= ~(M_MCAST | M_BCAST);
363 m->m_flags &= ~(M_80211_RX | M_MCAST | M_BCAST);
366 KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
367 ("gack, opmode %d", vap->iv_opmode));
372 IEEE80211_NODE_STAT(ni, rx_data);
373 IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
374 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
375 m->m_flags |= M_MCAST; /* XXX M_BCAST? */
376 IEEE80211_NODE_STAT(ni, rx_mcast);
378 IEEE80211_NODE_STAT(ni, rx_ucast);
380 /* perform as a bridge within the AP */
381 if ((vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0) {
382 struct mbuf *mcopy = NULL;
384 if (m->m_flags & M_MCAST) {
385 mcopy = m_dup(m, M_NOWAIT);
389 mcopy->m_flags |= M_MCAST;
392 * Check if the destination is associated with the
393 * same vap and authorized to receive traffic.
394 * Beware of traffic destined for the vap itself;
395 * sending it will not work; just let it be delivered
398 struct ieee80211_node *sta = ieee80211_find_vap_node(
399 &vap->iv_ic->ic_sta, vap, eh->ether_dhost);
401 if (ieee80211_node_is_authorized(sta)) {
403 * Beware of sending to ourself; this
404 * needs to happen via the normal
407 if (sta != vap->iv_bss) {
412 vap->iv_stats.is_rx_unauth++;
413 IEEE80211_NODE_STAT(sta, rx_unauth);
415 ieee80211_free_node(sta);
420 len = mcopy->m_pkthdr.len;
421 err = ieee80211_vap_xmitpkt(vap, mcopy);
423 /* NB: IFQ_HANDOFF reclaims mcopy */
431 * Mark frame as coming from vap's interface.
433 m->m_pkthdr.rcvif = ifp;
434 if (m->m_flags & M_MCAST) {
436 * Spam DWDS vap's w/ multicast traffic.
438 /* XXX only if dwds in use? */
439 ieee80211_dwds_mcast(vap, m);
441 if (ni->ni_vlan != 0) {
442 /* attach vlan tag */
443 m->m_pkthdr.ether_vtag = ni->ni_vlan;
444 m->m_flags |= M_VLANTAG;
446 ifp->if_input(ifp, m);
451 * Decide if a received management frame should be
452 * printed when debugging is enabled. This filters some
453 * of the less interesting frames that come frequently
457 doprint(struct ieee80211vap *vap, int subtype)
460 case IEEE80211_FC0_SUBTYPE_BEACON:
461 return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
462 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
469 * Process a received frame. The node associated with the sender
470 * should be supplied. If nothing was found in the node table then
471 * the caller is assumed to supply a reference to iv_bss instead.
472 * The RSSI and a timestamp are also supplied. The RSSI data is used
473 * during AP scanning to select a AP to associate with; it can have
474 * any units so long as values have consistent units and higher values
475 * mean ``better signal''. The receive timestamp is currently not used
476 * by the 802.11 layer.
479 hostap_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
481 #define HAS_SEQ(type) ((type & 0x4) == 0)
482 struct ieee80211vap *vap = ni->ni_vap;
483 struct ieee80211com *ic = ni->ni_ic;
484 struct ifnet *ifp = vap->iv_ifp;
485 struct ieee80211_frame *wh;
486 struct ieee80211_key *key;
487 struct ether_header *eh;
488 int hdrspace, need_tap = 1; /* mbuf need to be tapped. */
489 uint8_t dir, type, subtype, qos;
493 if (m->m_flags & M_AMPDU_MPDU) {
495 * Fastpath for A-MPDU reorder q resubmission. Frames
496 * w/ M_AMPDU_MPDU marked have already passed through
497 * here but were received out of order and been held on
498 * the reorder queue. When resubmitted they are marked
499 * with the M_AMPDU_MPDU flag and we can bypass most of
500 * the normal processing.
502 wh = mtod(m, struct ieee80211_frame *);
503 type = IEEE80211_FC0_TYPE_DATA;
504 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
505 subtype = IEEE80211_FC0_SUBTYPE_QOS;
506 hdrspace = ieee80211_hdrspace(ic, wh); /* XXX optimize? */
510 KASSERT(ni != NULL, ("null node"));
511 ni->ni_inact = ni->ni_inact_reload;
513 type = -1; /* undefined */
515 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
516 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
517 ni->ni_macaddr, NULL,
518 "too short (1): len %u", m->m_pkthdr.len);
519 vap->iv_stats.is_rx_tooshort++;
523 * Bit of a cheat here, we use a pointer for a 3-address
524 * frame format but don't reference fields past outside
525 * ieee80211_frame_min w/o first validating the data is
528 wh = mtod(m, struct ieee80211_frame *);
530 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
531 IEEE80211_FC0_VERSION_0) {
532 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
533 ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
534 wh->i_fc[0], wh->i_fc[1]);
535 vap->iv_stats.is_rx_badversion++;
539 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
540 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
541 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
542 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
543 if (dir != IEEE80211_FC1_DIR_NODS)
545 else if (type == IEEE80211_FC0_TYPE_CTL)
548 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
549 IEEE80211_DISCARD_MAC(vap,
550 IEEE80211_MSG_ANY, ni->ni_macaddr,
551 NULL, "too short (2): len %u",
553 vap->iv_stats.is_rx_tooshort++;
559 * Validate the bssid.
561 if (!(type == IEEE80211_FC0_TYPE_MGT &&
562 subtype == IEEE80211_FC0_SUBTYPE_BEACON) &&
563 !IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
564 !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
565 /* not interested in */
566 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
567 bssid, NULL, "%s", "not to bss");
568 vap->iv_stats.is_rx_wrongbss++;
572 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
575 uint8_t tid = ieee80211_gettid(wh);
576 if (IEEE80211_QOS_HAS_SEQ(wh) &&
577 TID_TO_WME_AC(tid) >= WME_AC_VI)
578 ic->ic_wme.wme_hipri_traffic++;
579 rxseq = le16toh(*(uint16_t *)wh->i_seq);
580 if (! ieee80211_check_rxseq(ni, wh)) {
581 /* duplicate, discard */
582 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
584 "seqno <%u,%u> fragno <%u,%u> tid %u",
585 rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
586 ni->ni_rxseqs[tid] >>
587 IEEE80211_SEQ_SEQ_SHIFT,
588 rxseq & IEEE80211_SEQ_FRAG_MASK,
590 IEEE80211_SEQ_FRAG_MASK,
592 vap->iv_stats.is_rx_dup++;
593 IEEE80211_NODE_STAT(ni, rx_dup);
596 ni->ni_rxseqs[tid] = rxseq;
601 case IEEE80211_FC0_TYPE_DATA:
602 hdrspace = ieee80211_hdrspace(ic, wh);
603 if (m->m_len < hdrspace &&
604 (m = m_pullup(m, hdrspace)) == NULL) {
605 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
606 ni->ni_macaddr, NULL,
607 "data too short: expecting %u", hdrspace);
608 vap->iv_stats.is_rx_tooshort++;
611 if (!(dir == IEEE80211_FC1_DIR_TODS ||
612 (dir == IEEE80211_FC1_DIR_DSTODS &&
613 (vap->iv_flags & IEEE80211_F_DWDS)))) {
614 if (dir != IEEE80211_FC1_DIR_DSTODS) {
615 IEEE80211_DISCARD(vap,
616 IEEE80211_MSG_INPUT, wh, "data",
617 "incorrect dir 0x%x", dir);
619 IEEE80211_DISCARD(vap,
620 IEEE80211_MSG_INPUT |
621 IEEE80211_MSG_WDS, wh,
623 "%s", "DWDS not enabled");
625 vap->iv_stats.is_rx_wrongdir++;
628 /* check if source STA is associated */
629 if (ni == vap->iv_bss) {
630 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
631 wh, "data", "%s", "unknown src");
632 ieee80211_send_error(ni, wh->i_addr2,
633 IEEE80211_FC0_SUBTYPE_DEAUTH,
634 IEEE80211_REASON_NOT_AUTHED);
635 vap->iv_stats.is_rx_notassoc++;
638 if (ni->ni_associd == 0) {
639 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
640 wh, "data", "%s", "unassoc src");
641 IEEE80211_SEND_MGMT(ni,
642 IEEE80211_FC0_SUBTYPE_DISASSOC,
643 IEEE80211_REASON_NOT_ASSOCED);
644 vap->iv_stats.is_rx_notassoc++;
649 * Check for power save state change.
650 * XXX out-of-order A-MPDU frames?
652 if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
653 (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
655 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
657 * For 4-address packets handle WDS discovery
658 * notifications. Once a WDS link is setup frames
659 * are just delivered to the WDS vap (see below).
661 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap == NULL) {
662 if (!ieee80211_node_is_authorized(ni)) {
663 IEEE80211_DISCARD(vap,
664 IEEE80211_MSG_INPUT |
665 IEEE80211_MSG_WDS, wh,
667 "%s", "unauthorized port");
668 vap->iv_stats.is_rx_unauth++;
669 IEEE80211_NODE_STAT(ni, rx_unauth);
672 ieee80211_dwds_discover(ni, m);
677 * Handle A-MPDU re-ordering. If the frame is to be
678 * processed directly then ieee80211_ampdu_reorder
679 * will return 0; otherwise it has consumed the mbuf
680 * and we should do nothing more with it.
682 if ((m->m_flags & M_AMPDU) &&
683 ieee80211_ampdu_reorder(ni, m) != 0) {
690 * Handle privacy requirements. Note that we
691 * must not be preempted from here until after
692 * we (potentially) call ieee80211_crypto_demic;
693 * otherwise we may violate assumptions in the
694 * crypto cipher modules used to do delayed update
695 * of replay sequence numbers.
697 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
698 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
700 * Discard encrypted frames when privacy is off.
702 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
703 wh, "WEP", "%s", "PRIVACY off");
704 vap->iv_stats.is_rx_noprivacy++;
705 IEEE80211_NODE_STAT(ni, rx_noprivacy);
708 key = ieee80211_crypto_decap(ni, m, hdrspace);
710 /* NB: stats+msgs handled in crypto_decap */
711 IEEE80211_NODE_STAT(ni, rx_wepfail);
714 wh = mtod(m, struct ieee80211_frame *);
715 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
717 /* XXX M_WEP and IEEE80211_F_PRIVACY */
722 * Save QoS bits for use below--before we strip the header.
724 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
725 qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
726 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
727 ((struct ieee80211_qosframe *)wh)->i_qos[0];
732 * Next up, any fragmentation.
734 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
735 m = ieee80211_defrag(ni, m, hdrspace);
737 /* Fragment dropped or frame not complete yet */
741 wh = NULL; /* no longer valid, catch any uses */
744 * Next strip any MSDU crypto bits.
746 if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
747 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
748 ni->ni_macaddr, "data", "%s", "demic error");
749 vap->iv_stats.is_rx_demicfail++;
750 IEEE80211_NODE_STAT(ni, rx_demicfail);
753 /* copy to listener after decrypt */
754 if (ieee80211_radiotap_active_vap(vap))
755 ieee80211_radiotap_rx(vap, m);
758 * Finally, strip the 802.11 header.
760 m = ieee80211_decap(vap, m, hdrspace);
762 /* XXX mask bit to check for both */
763 /* don't count Null data frames as errors */
764 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
765 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
767 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
768 ni->ni_macaddr, "data", "%s", "decap error");
769 vap->iv_stats.is_rx_decap++;
770 IEEE80211_NODE_STAT(ni, rx_decap);
773 eh = mtod(m, struct ether_header *);
774 if (!ieee80211_node_is_authorized(ni)) {
776 * Deny any non-PAE frames received prior to
777 * authorization. For open/shared-key
778 * authentication the port is mark authorized
779 * after authentication completes. For 802.1x
780 * the port is not marked authorized by the
781 * authenticator until the handshake has completed.
783 if (eh->ether_type != htons(ETHERTYPE_PAE)) {
784 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
785 eh->ether_shost, "data",
786 "unauthorized port: ether type 0x%x len %u",
787 eh->ether_type, m->m_pkthdr.len);
788 vap->iv_stats.is_rx_unauth++;
789 IEEE80211_NODE_STAT(ni, rx_unauth);
794 * When denying unencrypted frames, discard
795 * any non-PAE frames received without encryption.
797 if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
798 (key == NULL && (m->m_flags & M_WEP) == 0) &&
799 eh->ether_type != htons(ETHERTYPE_PAE)) {
801 * Drop unencrypted frames.
803 vap->iv_stats.is_rx_unencrypted++;
804 IEEE80211_NODE_STAT(ni, rx_unencrypted);
808 /* XXX require HT? */
809 if (qos & IEEE80211_QOS_AMSDU) {
810 m = ieee80211_decap_amsdu(ni, m);
812 return IEEE80211_FC0_TYPE_DATA;
814 #ifdef IEEE80211_SUPPORT_SUPERG
815 m = ieee80211_decap_fastframe(vap, ni, m);
817 return IEEE80211_FC0_TYPE_DATA;
820 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
821 ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
823 hostap_deliver_data(vap, ni, m);
824 return IEEE80211_FC0_TYPE_DATA;
826 case IEEE80211_FC0_TYPE_MGT:
827 vap->iv_stats.is_rx_mgmt++;
828 IEEE80211_NODE_STAT(ni, rx_mgmt);
829 if (dir != IEEE80211_FC1_DIR_NODS) {
830 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
831 wh, "mgt", "incorrect dir 0x%x", dir);
832 vap->iv_stats.is_rx_wrongdir++;
835 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
836 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
837 ni->ni_macaddr, "mgt", "too short: len %u",
839 vap->iv_stats.is_rx_tooshort++;
842 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
843 /* ensure return frames are unicast */
844 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
845 wh, NULL, "source is multicast: %s",
846 ether_sprintf(wh->i_addr2));
847 vap->iv_stats.is_rx_mgtdiscard++; /* XXX stat */
850 #ifdef IEEE80211_DEBUG
851 if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
852 ieee80211_msg_dumppkts(vap)) {
853 if_printf(ifp, "received %s from %s rssi %d\n",
854 ieee80211_mgt_subtype_name[subtype >>
855 IEEE80211_FC0_SUBTYPE_SHIFT],
856 ether_sprintf(wh->i_addr2), rssi);
859 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
860 if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
862 * Only shared key auth frames with a challenge
863 * should be encrypted, discard all others.
865 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
867 "%s", "WEP set but not permitted");
868 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
871 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
873 * Discard encrypted frames when privacy is off.
875 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
876 wh, NULL, "%s", "WEP set but PRIVACY off");
877 vap->iv_stats.is_rx_noprivacy++;
880 hdrspace = ieee80211_hdrspace(ic, wh);
881 key = ieee80211_crypto_decap(ni, m, hdrspace);
883 /* NB: stats+msgs handled in crypto_decap */
886 wh = mtod(m, struct ieee80211_frame *);
887 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
890 * Pass the packet to radiotap before calling iv_recv_mgmt().
891 * Otherwise iv_recv_mgmt() might pass another packet to
892 * radiotap, resulting in out of order packet captures.
894 if (ieee80211_radiotap_active_vap(vap))
895 ieee80211_radiotap_rx(vap, m);
897 vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
900 case IEEE80211_FC0_TYPE_CTL:
901 vap->iv_stats.is_rx_ctl++;
902 IEEE80211_NODE_STAT(ni, rx_ctrl);
903 vap->iv_recv_ctl(ni, m, subtype);
906 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
907 wh, "bad", "frame type 0x%x", type);
908 /* should not come here */
915 if (need_tap && ieee80211_radiotap_active_vap(vap))
916 ieee80211_radiotap_rx(vap, m);
923 hostap_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
924 int rssi, int nf, uint16_t seq, uint16_t status)
926 struct ieee80211vap *vap = ni->ni_vap;
928 KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
930 if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
931 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
932 ni->ni_macaddr, "open auth",
933 "bad sta auth mode %u", ni->ni_authmode);
934 vap->iv_stats.is_rx_bad_auth++; /* XXX */
936 * Clear any challenge text that may be there if
937 * a previous shared key auth failed and then an
938 * open auth is attempted.
940 if (ni->ni_challenge != NULL) {
941 free(ni->ni_challenge, M_80211_NODE);
942 ni->ni_challenge = NULL;
944 /* XXX hack to workaround calling convention */
945 ieee80211_send_error(ni, wh->i_addr2,
946 IEEE80211_FC0_SUBTYPE_AUTH,
947 (seq + 1) | (IEEE80211_STATUS_ALG<<16));
950 if (seq != IEEE80211_AUTH_OPEN_REQUEST) {
951 vap->iv_stats.is_rx_bad_auth++;
954 /* always accept open authentication requests */
955 if (ni == vap->iv_bss) {
956 ni = ieee80211_dup_bss(vap, wh->i_addr2);
959 } else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
960 (void) ieee80211_ref_node(ni);
962 * Mark the node as referenced to reflect that it's
963 * reference count has been bumped to insure it remains
964 * after the transaction completes.
966 ni->ni_flags |= IEEE80211_NODE_AREF;
968 * Mark the node as requiring a valid association id
969 * before outbound traffic is permitted.
971 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
973 if (vap->iv_acl != NULL &&
974 vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
976 * When the ACL policy is set to RADIUS we defer the
977 * authorization to a user agent. Dispatch an event,
978 * a subsequent MLME call will decide the fate of the
979 * station. If the user agent is not present then the
980 * node will be reclaimed due to inactivity.
982 IEEE80211_NOTE_MAC(vap,
983 IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL, ni->ni_macaddr,
984 "%s", "station authentication defered (radius acl)");
985 ieee80211_notify_node_auth(ni);
987 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
988 IEEE80211_NOTE_MAC(vap,
989 IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni->ni_macaddr,
990 "%s", "station authenticated (open)");
992 * When 802.1x is not in use mark the port
993 * authorized at this point so traffic can flow.
995 if (ni->ni_authmode != IEEE80211_AUTH_8021X)
996 ieee80211_node_authorize(ni);
1001 hostap_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
1002 uint8_t *frm, uint8_t *efrm, int rssi, int nf,
1003 uint16_t seq, uint16_t status)
1005 struct ieee80211vap *vap = ni->ni_vap;
1007 int allocbs, estatus;
1009 KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
1012 * NB: this can happen as we allow pre-shared key
1013 * authentication to be enabled w/o wep being turned
1014 * on so that configuration of these can be done
1015 * in any order. It may be better to enforce the
1016 * ordering in which case this check would just be
1017 * for sanity/consistency.
1019 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
1020 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1021 ni->ni_macaddr, "shared key auth",
1022 "%s", " PRIVACY is disabled");
1023 estatus = IEEE80211_STATUS_ALG;
1027 * Pre-shared key authentication is evil; accept
1028 * it only if explicitly configured (it is supported
1029 * mainly for compatibility with clients like Mac OS X).
1031 if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1032 ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1033 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1034 ni->ni_macaddr, "shared key auth",
1035 "bad sta auth mode %u", ni->ni_authmode);
1036 vap->iv_stats.is_rx_bad_auth++; /* XXX maybe a unique error? */
1037 estatus = IEEE80211_STATUS_ALG;
1042 if (frm + 1 < efrm) {
1043 if ((frm[1] + 2) > (efrm - frm)) {
1044 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1045 ni->ni_macaddr, "shared key auth",
1046 "ie %d/%d too long",
1047 frm[0], (frm[1] + 2) - (efrm - frm));
1048 vap->iv_stats.is_rx_bad_auth++;
1049 estatus = IEEE80211_STATUS_CHALLENGE;
1052 if (*frm == IEEE80211_ELEMID_CHALLENGE)
1057 case IEEE80211_AUTH_SHARED_CHALLENGE:
1058 case IEEE80211_AUTH_SHARED_RESPONSE:
1059 if (challenge == NULL) {
1060 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1061 ni->ni_macaddr, "shared key auth",
1062 "%s", "no challenge");
1063 vap->iv_stats.is_rx_bad_auth++;
1064 estatus = IEEE80211_STATUS_CHALLENGE;
1067 if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1068 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1069 ni->ni_macaddr, "shared key auth",
1070 "bad challenge len %d", challenge[1]);
1071 vap->iv_stats.is_rx_bad_auth++;
1072 estatus = IEEE80211_STATUS_CHALLENGE;
1079 case IEEE80211_AUTH_SHARED_REQUEST:
1080 if (ni == vap->iv_bss) {
1081 ni = ieee80211_dup_bss(vap, wh->i_addr2);
1083 /* NB: no way to return an error */
1088 if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1089 (void) ieee80211_ref_node(ni);
1093 * Mark the node as referenced to reflect that it's
1094 * reference count has been bumped to insure it remains
1095 * after the transaction completes.
1097 ni->ni_flags |= IEEE80211_NODE_AREF;
1099 * Mark the node as requiring a valid associatio id
1100 * before outbound traffic is permitted.
1102 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
1103 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1105 if (!ieee80211_alloc_challenge(ni)) {
1106 /* NB: don't return error so they rexmit */
1109 get_random_bytes(ni->ni_challenge,
1110 IEEE80211_CHALLENGE_LEN);
1111 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1112 ni, "shared key %sauth request", allocbs ? "" : "re");
1114 * When the ACL policy is set to RADIUS we defer the
1115 * authorization to a user agent. Dispatch an event,
1116 * a subsequent MLME call will decide the fate of the
1117 * station. If the user agent is not present then the
1118 * node will be reclaimed due to inactivity.
1120 if (vap->iv_acl != NULL &&
1121 vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
1122 IEEE80211_NOTE_MAC(vap,
1123 IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL,
1125 "%s", "station authentication defered (radius acl)");
1126 ieee80211_notify_node_auth(ni);
1130 case IEEE80211_AUTH_SHARED_RESPONSE:
1131 if (ni == vap->iv_bss) {
1132 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1133 ni->ni_macaddr, "shared key response",
1134 "%s", "unknown station");
1135 /* NB: don't send a response */
1138 if (ni->ni_challenge == NULL) {
1139 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1140 ni->ni_macaddr, "shared key response",
1141 "%s", "no challenge recorded");
1142 vap->iv_stats.is_rx_bad_auth++;
1143 estatus = IEEE80211_STATUS_CHALLENGE;
1146 if (memcmp(ni->ni_challenge, &challenge[2],
1147 challenge[1]) != 0) {
1148 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1149 ni->ni_macaddr, "shared key response",
1150 "%s", "challenge mismatch");
1151 vap->iv_stats.is_rx_auth_fail++;
1152 estatus = IEEE80211_STATUS_CHALLENGE;
1155 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1156 ni, "%s", "station authenticated (shared key)");
1157 ieee80211_node_authorize(ni);
1160 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1161 ni->ni_macaddr, "shared key auth",
1163 vap->iv_stats.is_rx_bad_auth++;
1164 estatus = IEEE80211_STATUS_SEQUENCE;
1167 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1171 * Send an error response; but only when operating as an AP.
1173 /* XXX hack to workaround calling convention */
1174 ieee80211_send_error(ni, wh->i_addr2,
1175 IEEE80211_FC0_SUBTYPE_AUTH,
1176 (seq + 1) | (estatus<<16));
1180 * Convert a WPA cipher selector OUI to an internal
1181 * cipher algorithm. Where appropriate we also
1182 * record any key length.
1185 wpa_cipher(const uint8_t *sel, uint8_t *keylen)
1187 #define WPA_SEL(x) (((x)<<24)|WPA_OUI)
1188 uint32_t w = LE_READ_4(sel);
1191 case WPA_SEL(WPA_CSE_NULL):
1192 return IEEE80211_CIPHER_NONE;
1193 case WPA_SEL(WPA_CSE_WEP40):
1195 *keylen = 40 / NBBY;
1196 return IEEE80211_CIPHER_WEP;
1197 case WPA_SEL(WPA_CSE_WEP104):
1199 *keylen = 104 / NBBY;
1200 return IEEE80211_CIPHER_WEP;
1201 case WPA_SEL(WPA_CSE_TKIP):
1202 return IEEE80211_CIPHER_TKIP;
1203 case WPA_SEL(WPA_CSE_CCMP):
1204 return IEEE80211_CIPHER_AES_CCM;
1206 return 32; /* NB: so 1<< is discarded */
1211 * Convert a WPA key management/authentication algorithm
1212 * to an internal code.
1215 wpa_keymgmt(const uint8_t *sel)
1217 #define WPA_SEL(x) (((x)<<24)|WPA_OUI)
1218 uint32_t w = LE_READ_4(sel);
1221 case WPA_SEL(WPA_ASE_8021X_UNSPEC):
1222 return WPA_ASE_8021X_UNSPEC;
1223 case WPA_SEL(WPA_ASE_8021X_PSK):
1224 return WPA_ASE_8021X_PSK;
1225 case WPA_SEL(WPA_ASE_NONE):
1226 return WPA_ASE_NONE;
1228 return 0; /* NB: so is discarded */
1233 * Parse a WPA information element to collect parameters.
1234 * Note that we do not validate security parameters; that
1235 * is handled by the authenticator; the parsing done here
1236 * is just for internal use in making operational decisions.
1239 ieee80211_parse_wpa(struct ieee80211vap *vap, const uint8_t *frm,
1240 struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1242 uint8_t len = frm[1];
1247 * Check the length once for fixed parts: OUI, type,
1248 * version, mcast cipher, and 2 selector counts.
1249 * Other, variable-length data, must be checked separately.
1251 if ((vap->iv_flags & IEEE80211_F_WPA1) == 0) {
1252 IEEE80211_DISCARD_IE(vap,
1253 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1254 wh, "WPA", "not WPA, flags 0x%x", vap->iv_flags);
1255 return IEEE80211_REASON_IE_INVALID;
1258 IEEE80211_DISCARD_IE(vap,
1259 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1260 wh, "WPA", "too short, len %u", len);
1261 return IEEE80211_REASON_IE_INVALID;
1263 frm += 6, len -= 4; /* NB: len is payload only */
1264 /* NB: iswpaoui already validated the OUI and type */
1266 if (w != WPA_VERSION) {
1267 IEEE80211_DISCARD_IE(vap,
1268 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1269 wh, "WPA", "bad version %u", w);
1270 return IEEE80211_REASON_IE_INVALID;
1274 memset(rsn, 0, sizeof(*rsn));
1276 /* multicast/group cipher */
1277 rsn->rsn_mcastcipher = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
1280 /* unicast ciphers */
1284 IEEE80211_DISCARD_IE(vap,
1285 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1286 wh, "WPA", "ucast cipher data too short; len %u, n %u",
1288 return IEEE80211_REASON_IE_INVALID;
1291 for (; n > 0; n--) {
1292 w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
1295 if (w & (1<<IEEE80211_CIPHER_TKIP))
1296 rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1298 rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1300 /* key management algorithms */
1304 IEEE80211_DISCARD_IE(vap,
1305 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1306 wh, "WPA", "key mgmt alg data too short; len %u, n %u",
1308 return IEEE80211_REASON_IE_INVALID;
1311 for (; n > 0; n--) {
1312 w |= wpa_keymgmt(frm);
1315 if (w & WPA_ASE_8021X_UNSPEC)
1316 rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
1318 rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1320 if (len > 2) /* optional capabilities */
1321 rsn->rsn_caps = LE_READ_2(frm);
1327 * Convert an RSN cipher selector OUI to an internal
1328 * cipher algorithm. Where appropriate we also
1329 * record any key length.
1332 rsn_cipher(const uint8_t *sel, uint8_t *keylen)
1334 #define RSN_SEL(x) (((x)<<24)|RSN_OUI)
1335 uint32_t w = LE_READ_4(sel);
1338 case RSN_SEL(RSN_CSE_NULL):
1339 return IEEE80211_CIPHER_NONE;
1340 case RSN_SEL(RSN_CSE_WEP40):
1342 *keylen = 40 / NBBY;
1343 return IEEE80211_CIPHER_WEP;
1344 case RSN_SEL(RSN_CSE_WEP104):
1346 *keylen = 104 / NBBY;
1347 return IEEE80211_CIPHER_WEP;
1348 case RSN_SEL(RSN_CSE_TKIP):
1349 return IEEE80211_CIPHER_TKIP;
1350 case RSN_SEL(RSN_CSE_CCMP):
1351 return IEEE80211_CIPHER_AES_CCM;
1352 case RSN_SEL(RSN_CSE_WRAP):
1353 return IEEE80211_CIPHER_AES_OCB;
1355 return 32; /* NB: so 1<< is discarded */
1360 * Convert an RSN key management/authentication algorithm
1361 * to an internal code.
1364 rsn_keymgmt(const uint8_t *sel)
1366 #define RSN_SEL(x) (((x)<<24)|RSN_OUI)
1367 uint32_t w = LE_READ_4(sel);
1370 case RSN_SEL(RSN_ASE_8021X_UNSPEC):
1371 return RSN_ASE_8021X_UNSPEC;
1372 case RSN_SEL(RSN_ASE_8021X_PSK):
1373 return RSN_ASE_8021X_PSK;
1374 case RSN_SEL(RSN_ASE_NONE):
1375 return RSN_ASE_NONE;
1377 return 0; /* NB: so is discarded */
1382 * Parse a WPA/RSN information element to collect parameters
1383 * and validate the parameters against what has been
1384 * configured for the system.
1387 ieee80211_parse_rsn(struct ieee80211vap *vap, const uint8_t *frm,
1388 struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1390 uint8_t len = frm[1];
1395 * Check the length once for fixed parts:
1396 * version, mcast cipher, and 2 selector counts.
1397 * Other, variable-length data, must be checked separately.
1399 if ((vap->iv_flags & IEEE80211_F_WPA2) == 0) {
1400 IEEE80211_DISCARD_IE(vap,
1401 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1402 wh, "WPA", "not RSN, flags 0x%x", vap->iv_flags);
1403 return IEEE80211_REASON_IE_INVALID;
1406 IEEE80211_DISCARD_IE(vap,
1407 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1408 wh, "RSN", "too short, len %u", len);
1409 return IEEE80211_REASON_IE_INVALID;
1413 if (w != RSN_VERSION) {
1414 IEEE80211_DISCARD_IE(vap,
1415 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1416 wh, "RSN", "bad version %u", w);
1417 return IEEE80211_REASON_IE_INVALID;
1421 memset(rsn, 0, sizeof(*rsn));
1423 /* multicast/group cipher */
1424 rsn->rsn_mcastcipher = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
1427 /* unicast ciphers */
1431 IEEE80211_DISCARD_IE(vap,
1432 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1433 wh, "RSN", "ucast cipher data too short; len %u, n %u",
1435 return IEEE80211_REASON_IE_INVALID;
1438 for (; n > 0; n--) {
1439 w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
1442 if (w & (1<<IEEE80211_CIPHER_TKIP))
1443 rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1445 rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1447 /* key management algorithms */
1451 IEEE80211_DISCARD_IE(vap,
1452 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1453 wh, "RSN", "key mgmt alg data too short; len %u, n %u",
1455 return IEEE80211_REASON_IE_INVALID;
1458 for (; n > 0; n--) {
1459 w |= rsn_keymgmt(frm);
1462 if (w & RSN_ASE_8021X_UNSPEC)
1463 rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
1465 rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
1467 /* optional RSN capabilities */
1469 rsn->rsn_caps = LE_READ_2(frm);
1476 * WPA/802.11i assocation request processing.
1479 wpa_assocreq(struct ieee80211_node *ni, struct ieee80211_rsnparms *rsnparms,
1480 const struct ieee80211_frame *wh, const uint8_t *wpa,
1481 const uint8_t *rsn, uint16_t capinfo)
1483 struct ieee80211vap *vap = ni->ni_vap;
1487 ni->ni_flags &= ~(IEEE80211_NODE_WPS|IEEE80211_NODE_TSN);
1488 if (wpa == NULL && rsn == NULL) {
1489 if (vap->iv_flags_ext & IEEE80211_FEXT_WPS) {
1491 * W-Fi Protected Setup (WPS) permits
1492 * clients to associate and pass EAPOL frames
1493 * to establish initial credentials.
1495 ni->ni_flags |= IEEE80211_NODE_WPS;
1498 if ((vap->iv_flags_ext & IEEE80211_FEXT_TSN) &&
1499 (capinfo & IEEE80211_CAPINFO_PRIVACY)) {
1501 * Transitional Security Network. Permits clients
1502 * to associate and use WEP while WPA is configured.
1504 ni->ni_flags |= IEEE80211_NODE_TSN;
1507 IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1508 wh, NULL, "%s", "no WPA/RSN IE in association request");
1509 vap->iv_stats.is_rx_assoc_badwpaie++;
1510 reason = IEEE80211_REASON_IE_INVALID;
1513 /* assert right association security credentials */
1514 badwparsn = 0; /* NB: to silence compiler */
1515 switch (vap->iv_flags & IEEE80211_F_WPA) {
1516 case IEEE80211_F_WPA1:
1517 badwparsn = (wpa == NULL);
1519 case IEEE80211_F_WPA2:
1520 badwparsn = (rsn == NULL);
1522 case IEEE80211_F_WPA1|IEEE80211_F_WPA2:
1523 badwparsn = (wpa == NULL && rsn == NULL);
1527 IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1529 "%s", "missing WPA/RSN IE in association request");
1530 vap->iv_stats.is_rx_assoc_badwpaie++;
1531 reason = IEEE80211_REASON_IE_INVALID;
1535 * Parse WPA/RSN information element.
1538 reason = ieee80211_parse_wpa(vap, wpa, rsnparms, wh);
1540 reason = ieee80211_parse_rsn(vap, rsn, rsnparms, wh);
1542 /* XXX distinguish WPA/RSN? */
1543 vap->iv_stats.is_rx_assoc_badwpaie++;
1546 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA, ni,
1547 "%s ie: mc %u/%u uc %u/%u key %u caps 0x%x",
1548 wpa != NULL ? "WPA" : "RSN",
1549 rsnparms->rsn_mcastcipher, rsnparms->rsn_mcastkeylen,
1550 rsnparms->rsn_ucastcipher, rsnparms->rsn_ucastkeylen,
1551 rsnparms->rsn_keymgmt, rsnparms->rsn_caps);
1555 ieee80211_node_deauth(ni, reason);
1559 /* XXX find a better place for definition */
1560 struct l2_update_frame {
1561 struct ether_header eh;
1569 * Deliver a TGf L2UF frame on behalf of a station.
1570 * This primes any bridge when the station is roaming
1571 * between ap's on the same wired network.
1574 ieee80211_deliver_l2uf(struct ieee80211_node *ni)
1576 struct ieee80211vap *vap = ni->ni_vap;
1577 struct ifnet *ifp = vap->iv_ifp;
1579 struct l2_update_frame *l2uf;
1580 struct ether_header *eh;
1582 m = m_gethdr(M_NOWAIT, MT_DATA);
1584 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1585 "%s", "no mbuf for l2uf frame");
1586 vap->iv_stats.is_rx_nobuf++; /* XXX not right */
1589 l2uf = mtod(m, struct l2_update_frame *);
1591 /* dst: Broadcast address */
1592 IEEE80211_ADDR_COPY(eh->ether_dhost, ifp->if_broadcastaddr);
1593 /* src: associated STA */
1594 IEEE80211_ADDR_COPY(eh->ether_shost, ni->ni_macaddr);
1595 eh->ether_type = htons(sizeof(*l2uf) - sizeof(*eh));
1599 l2uf->control = 0xf5;
1600 l2uf->xid[0] = 0x81;
1601 l2uf->xid[1] = 0x80;
1602 l2uf->xid[2] = 0x00;
1604 m->m_pkthdr.len = m->m_len = sizeof(*l2uf);
1605 hostap_deliver_data(vap, ni, m);
1609 ratesetmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1610 int reassoc, int resp, const char *tag, int rate)
1612 IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1613 "deny %s request, %s rate set mismatch, rate/MCS %d",
1614 reassoc ? "reassoc" : "assoc", tag, rate & IEEE80211_RATE_VAL);
1615 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_BASIC_RATE);
1616 ieee80211_node_leave(ni);
1620 capinfomismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1621 int reassoc, int resp, const char *tag, int capinfo)
1623 struct ieee80211vap *vap = ni->ni_vap;
1625 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1626 "deny %s request, %s mismatch 0x%x",
1627 reassoc ? "reassoc" : "assoc", tag, capinfo);
1628 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_CAPINFO);
1629 ieee80211_node_leave(ni);
1630 vap->iv_stats.is_rx_assoc_capmismatch++;
1634 htcapmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1635 int reassoc, int resp)
1637 IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1638 "deny %s request, %s missing HT ie", reassoc ? "reassoc" : "assoc");
1639 /* XXX no better code */
1640 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_MISSING_HT_CAPS);
1641 ieee80211_node_leave(ni);
1645 authalgreject(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1646 int algo, int seq, int status)
1648 struct ieee80211vap *vap = ni->ni_vap;
1650 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1651 wh, NULL, "unsupported alg %d", algo);
1652 vap->iv_stats.is_rx_auth_unsupported++;
1653 ieee80211_send_error(ni, wh->i_addr2, IEEE80211_FC0_SUBTYPE_AUTH,
1654 seq | (status << 16));
1658 ishtmixed(const uint8_t *ie)
1660 const struct ieee80211_ie_htinfo *ht =
1661 (const struct ieee80211_ie_htinfo *) ie;
1662 return (ht->hi_byte2 & IEEE80211_HTINFO_OPMODE) ==
1663 IEEE80211_HTINFO_OPMODE_MIXED;
1667 is11bclient(const uint8_t *rates, const uint8_t *xrates)
1669 static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
1672 /* NB: the 11b clients we care about will not have xrates */
1673 if (xrates != NULL || rates == NULL)
1675 for (i = 0; i < rates[1]; i++) {
1676 int r = rates[2+i] & IEEE80211_RATE_VAL;
1677 if (r > 2*11 || ((1<<r) & brates) == 0)
1684 hostap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1685 int subtype, int rssi, int nf)
1687 struct ieee80211vap *vap = ni->ni_vap;
1688 struct ieee80211com *ic = ni->ni_ic;
1689 struct ieee80211_frame *wh;
1690 uint8_t *frm, *efrm, *sfrm;
1691 uint8_t *ssid, *rates, *xrates, *wpa, *rsn, *wme, *ath, *htcap;
1695 wh = mtod(m0, struct ieee80211_frame *);
1696 frm = (uint8_t *)&wh[1];
1697 efrm = mtod(m0, uint8_t *) + m0->m_len;
1699 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1700 case IEEE80211_FC0_SUBTYPE_BEACON: {
1701 struct ieee80211_scanparams scan;
1703 * We process beacon/probe response frames when scanning;
1704 * otherwise we check beacon frames for overlapping non-ERP
1705 * BSS in 11g and/or overlapping legacy BSS when in HT.
1707 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
1708 subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
1709 vap->iv_stats.is_rx_mgtdiscard++;
1712 /* NB: accept off-channel frames */
1713 if (ieee80211_parse_beacon(ni, m0, &scan) &~ IEEE80211_BPARSE_OFFCHAN)
1716 * Count frame now that we know it's to be processed.
1718 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1719 vap->iv_stats.is_rx_beacon++; /* XXX remove */
1720 IEEE80211_NODE_STAT(ni, rx_beacons);
1722 IEEE80211_NODE_STAT(ni, rx_proberesp);
1724 * If scanning, just pass information to the scan module.
1726 if (ic->ic_flags & IEEE80211_F_SCAN) {
1727 if (scan.status == 0 && /* NB: on channel */
1728 (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN)) {
1730 * Actively scanning a channel marked passive;
1731 * send a probe request now that we know there
1732 * is 802.11 traffic present.
1734 * XXX check if the beacon we recv'd gives
1735 * us what we need and suppress the probe req
1737 ieee80211_probe_curchan(vap, 1);
1738 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1740 ieee80211_add_scan(vap, &scan, wh, subtype, rssi, nf);
1744 * Check beacon for overlapping bss w/ non ERP stations.
1745 * If we detect one and protection is configured but not
1746 * enabled, enable it and start a timer that'll bring us
1747 * out if we stop seeing the bss.
1749 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1750 scan.status == 0 && /* NB: on-channel */
1751 ((scan.erp & 0x100) == 0 || /* NB: no ERP, 11b sta*/
1752 (scan.erp & IEEE80211_ERP_NON_ERP_PRESENT))) {
1753 ic->ic_lastnonerp = ticks;
1754 ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
1755 if (ic->ic_protmode != IEEE80211_PROT_NONE &&
1756 (ic->ic_flags & IEEE80211_F_USEPROT) == 0) {
1757 IEEE80211_NOTE_FRAME(vap,
1758 IEEE80211_MSG_ASSOC, wh,
1759 "non-ERP present on channel %d "
1760 "(saw erp 0x%x from channel %d), "
1761 "enable use of protection",
1762 ic->ic_curchan->ic_ieee,
1763 scan.erp, scan.chan);
1764 ic->ic_flags |= IEEE80211_F_USEPROT;
1765 ieee80211_notify_erp(ic);
1769 * Check beacon for non-HT station on HT channel
1770 * and update HT BSS occupancy as appropriate.
1772 if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) {
1773 if (scan.status & IEEE80211_BPARSE_OFFCHAN) {
1775 * Off control channel; only check frames
1776 * that come in the extension channel when
1777 * operating w/ HT40.
1779 if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
1781 if (scan.chan != ic->ic_curchan->ic_extieee)
1784 if (scan.htinfo == NULL) {
1785 ieee80211_htprot_update(ic,
1786 IEEE80211_HTINFO_OPMODE_PROTOPT |
1787 IEEE80211_HTINFO_NONHT_PRESENT);
1788 } else if (ishtmixed(scan.htinfo)) {
1789 /* XXX? take NONHT_PRESENT from beacon? */
1790 ieee80211_htprot_update(ic,
1791 IEEE80211_HTINFO_OPMODE_MIXED |
1792 IEEE80211_HTINFO_NONHT_PRESENT);
1798 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1799 if (vap->iv_state != IEEE80211_S_RUN) {
1800 vap->iv_stats.is_rx_mgtdiscard++;
1804 * Consult the ACL policy module if setup.
1806 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1807 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1808 wh, NULL, "%s", "disallowed by ACL");
1809 vap->iv_stats.is_rx_acl++;
1813 * prreq frame format
1815 * [tlv] supported rates
1816 * [tlv] extended supported rates
1818 ssid = rates = xrates = NULL;
1820 while (efrm - frm > 1) {
1821 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1823 case IEEE80211_ELEMID_SSID:
1826 case IEEE80211_ELEMID_RATES:
1829 case IEEE80211_ELEMID_XRATES:
1835 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1837 IEEE80211_VERIFY_ELEMENT(xrates,
1838 IEEE80211_RATE_MAXSIZE - rates[1], return);
1839 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1840 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
1841 if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
1842 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1844 "%s", "no ssid with ssid suppression enabled");
1845 vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
1849 /* XXX find a better class or define it's own */
1850 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1851 "%s", "recv probe req");
1853 * Some legacy 11b clients cannot hack a complete
1854 * probe response frame. When the request includes
1855 * only a bare-bones rate set, communicate this to
1856 * the transmit side.
1858 ieee80211_send_proberesp(vap, wh->i_addr2,
1859 is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
1862 case IEEE80211_FC0_SUBTYPE_AUTH: {
1863 uint16_t algo, seq, status;
1865 if (vap->iv_state != IEEE80211_S_RUN) {
1866 vap->iv_stats.is_rx_mgtdiscard++;
1869 if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1870 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1871 wh, NULL, "%s", "wrong bssid");
1872 vap->iv_stats.is_rx_wrongbss++; /*XXX unique stat?*/
1882 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1883 algo = le16toh(*(uint16_t *)frm);
1884 seq = le16toh(*(uint16_t *)(frm + 2));
1885 status = le16toh(*(uint16_t *)(frm + 4));
1886 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1887 "recv auth frame with algorithm %d seq %d", algo, seq);
1889 * Consult the ACL policy module if setup.
1891 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1892 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1893 wh, NULL, "%s", "disallowed by ACL");
1894 vap->iv_stats.is_rx_acl++;
1895 ieee80211_send_error(ni, wh->i_addr2,
1896 IEEE80211_FC0_SUBTYPE_AUTH,
1897 (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
1900 if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1901 IEEE80211_DISCARD(vap,
1902 IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1903 wh, NULL, "%s", "TKIP countermeasures enabled");
1904 vap->iv_stats.is_rx_auth_countermeasures++;
1905 ieee80211_send_error(ni, wh->i_addr2,
1906 IEEE80211_FC0_SUBTYPE_AUTH,
1907 IEEE80211_REASON_MIC_FAILURE);
1910 if (algo == IEEE80211_AUTH_ALG_SHARED)
1911 hostap_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1913 else if (algo == IEEE80211_AUTH_ALG_OPEN)
1914 hostap_auth_open(ni, wh, rssi, nf, seq, status);
1915 else if (algo == IEEE80211_AUTH_ALG_LEAP) {
1916 authalgreject(ni, wh, algo,
1917 seq+1, IEEE80211_STATUS_ALG);
1921 * We assume that an unknown algorithm is the result
1922 * of a decryption failure on a shared key auth frame;
1923 * return a status code appropriate for that instead
1924 * of IEEE80211_STATUS_ALG.
1926 * NB: a seq# of 4 is intentional; the decrypted
1927 * frame likely has a bogus seq value.
1929 authalgreject(ni, wh, algo,
1930 4, IEEE80211_STATUS_CHALLENGE);
1936 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1937 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
1938 uint16_t capinfo, lintval;
1939 struct ieee80211_rsnparms rsnparms;
1941 if (vap->iv_state != IEEE80211_S_RUN) {
1942 vap->iv_stats.is_rx_mgtdiscard++;
1945 if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1946 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1947 wh, NULL, "%s", "wrong bssid");
1948 vap->iv_stats.is_rx_assoc_bss++;
1951 if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1953 resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
1956 resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
1958 if (ni == vap->iv_bss) {
1959 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1960 "deny %s request, sta not authenticated",
1961 reassoc ? "reassoc" : "assoc");
1962 ieee80211_send_error(ni, wh->i_addr2,
1963 IEEE80211_FC0_SUBTYPE_DEAUTH,
1964 IEEE80211_REASON_ASSOC_NOT_AUTHED);
1965 vap->iv_stats.is_rx_assoc_notauth++;
1970 * asreq frame format
1971 * [2] capability information
1972 * [2] listen interval
1973 * [6*] current AP address (reassoc only)
1975 * [tlv] supported rates
1976 * [tlv] extended supported rates
1978 * [tlv] HT capabilities
1979 * [tlv] Atheros capabilities
1981 IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4), return);
1982 capinfo = le16toh(*(uint16_t *)frm); frm += 2;
1983 lintval = le16toh(*(uint16_t *)frm); frm += 2;
1985 frm += 6; /* ignore current AP info */
1986 ssid = rates = xrates = wpa = rsn = wme = ath = htcap = NULL;
1988 while (efrm - frm > 1) {
1989 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1991 case IEEE80211_ELEMID_SSID:
1994 case IEEE80211_ELEMID_RATES:
1997 case IEEE80211_ELEMID_XRATES:
2000 case IEEE80211_ELEMID_RSN:
2003 case IEEE80211_ELEMID_HTCAP:
2006 case IEEE80211_ELEMID_VENDOR:
2009 else if (iswmeinfo(frm))
2011 #ifdef IEEE80211_SUPPORT_SUPERG
2012 else if (isatherosoui(frm))
2015 else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
2016 if (ishtcapoui(frm) && htcap == NULL)
2023 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
2025 IEEE80211_VERIFY_ELEMENT(xrates,
2026 IEEE80211_RATE_MAXSIZE - rates[1], return);
2027 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2028 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
2029 if (htcap != NULL) {
2030 IEEE80211_VERIFY_LENGTH(htcap[1],
2031 htcap[0] == IEEE80211_ELEMID_VENDOR ?
2032 4 + sizeof(struct ieee80211_ie_htcap)-2 :
2033 sizeof(struct ieee80211_ie_htcap)-2,
2034 return); /* XXX just NULL out? */
2037 if ((vap->iv_flags & IEEE80211_F_WPA) &&
2038 !wpa_assocreq(ni, &rsnparms, wh, wpa, rsn, capinfo))
2040 /* discard challenge after association */
2041 if (ni->ni_challenge != NULL) {
2042 free(ni->ni_challenge, M_80211_NODE);
2043 ni->ni_challenge = NULL;
2045 /* NB: 802.11 spec says to ignore station's privacy bit */
2046 if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
2047 capinfomismatch(ni, wh, reassoc, resp,
2048 "capability", capinfo);
2052 * Disallow re-associate w/ invalid slot time setting.
2054 if (ni->ni_associd != 0 &&
2055 IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2056 ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
2057 capinfomismatch(ni, wh, reassoc, resp,
2058 "slot time", capinfo);
2061 rate = ieee80211_setup_rates(ni, rates, xrates,
2062 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
2063 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2064 if (rate & IEEE80211_RATE_BASIC) {
2065 ratesetmismatch(ni, wh, reassoc, resp, "legacy", rate);
2066 vap->iv_stats.is_rx_assoc_norate++;
2070 * If constrained to 11g-only stations reject an
2071 * 11b-only station. We cheat a bit here by looking
2072 * at the max negotiated xmit rate and assuming anyone
2073 * with a best rate <24Mb/s is an 11b station.
2075 if ((vap->iv_flags & IEEE80211_F_PUREG) && rate < 48) {
2076 ratesetmismatch(ni, wh, reassoc, resp, "11g", rate);
2077 vap->iv_stats.is_rx_assoc_norate++;
2081 * Do HT rate set handling and setup HT node state.
2083 ni->ni_chan = vap->iv_bss->ni_chan;
2084 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
2085 rate = ieee80211_setup_htrates(ni, htcap,
2086 IEEE80211_F_DOFMCS | IEEE80211_F_DONEGO |
2088 if (rate & IEEE80211_RATE_BASIC) {
2089 ratesetmismatch(ni, wh, reassoc, resp,
2091 vap->iv_stats.is_ht_assoc_norate++;
2094 ieee80211_ht_node_init(ni);
2095 ieee80211_ht_updatehtcap(ni, htcap);
2096 } else if (ni->ni_flags & IEEE80211_NODE_HT)
2097 ieee80211_ht_node_cleanup(ni);
2098 #ifdef IEEE80211_SUPPORT_SUPERG
2099 else if (ni->ni_ath_flags & IEEE80211_NODE_ATH)
2100 ieee80211_ff_node_cleanup(ni);
2103 * Allow AMPDU operation only with unencrypted traffic
2104 * or AES-CCM; the 11n spec only specifies these ciphers
2105 * so permitting any others is undefined and can lead
2106 * to interoperability problems.
2108 if ((ni->ni_flags & IEEE80211_NODE_HT) &&
2109 (((vap->iv_flags & IEEE80211_F_WPA) &&
2110 rsnparms.rsn_ucastcipher != IEEE80211_CIPHER_AES_CCM) ||
2111 (vap->iv_flags & (IEEE80211_F_WPA|IEEE80211_F_PRIVACY)) == IEEE80211_F_PRIVACY)) {
2113 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
2114 "disallow HT use because WEP or TKIP requested, "
2115 "capinfo 0x%x ucastcipher %d", capinfo,
2116 rsnparms.rsn_ucastcipher);
2117 ieee80211_ht_node_cleanup(ni);
2118 vap->iv_stats.is_ht_assoc_downgrade++;
2121 * If constrained to 11n-only stations reject legacy stations.
2123 if ((vap->iv_flags_ht & IEEE80211_FHT_PUREN) &&
2124 (ni->ni_flags & IEEE80211_NODE_HT) == 0) {
2125 htcapmismatch(ni, wh, reassoc, resp);
2126 vap->iv_stats.is_ht_assoc_nohtcap++;
2129 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
2131 ni->ni_intval = lintval;
2132 ni->ni_capinfo = capinfo;
2133 ni->ni_fhdwell = vap->iv_bss->ni_fhdwell;
2134 ni->ni_fhindex = vap->iv_bss->ni_fhindex;
2137 * XXX maybe better to just expand
2139 if (ieee80211_ies_init(&ni->ni_ies, sfrm, efrm - sfrm)) {
2140 #define setie(_ie, _off) ieee80211_ies_setie(ni->ni_ies, _ie, _off)
2142 setie(wpa_ie, wpa - sfrm);
2144 setie(rsn_ie, rsn - sfrm);
2146 setie(htcap_ie, htcap - sfrm);
2148 setie(wme_ie, wme - sfrm);
2150 * Mark node as capable of QoS.
2152 ni->ni_flags |= IEEE80211_NODE_QOS;
2154 ni->ni_flags &= ~IEEE80211_NODE_QOS;
2155 #ifdef IEEE80211_SUPPORT_SUPERG
2157 setie(ath_ie, ath - sfrm);
2159 * Parse ATH station parameters.
2161 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
2164 ni->ni_ath_flags = 0;
2167 ni->ni_flags &= ~IEEE80211_NODE_QOS;
2168 ni->ni_ath_flags = 0;
2170 ieee80211_node_join(ni, resp);
2171 ieee80211_deliver_l2uf(ni);
2175 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2176 case IEEE80211_FC0_SUBTYPE_DISASSOC: {
2179 if (vap->iv_state != IEEE80211_S_RUN ||
2180 /* NB: can happen when in promiscuous mode */
2181 !IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
2182 vap->iv_stats.is_rx_mgtdiscard++;
2186 * deauth/disassoc frame format
2189 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
2190 reason = le16toh(*(uint16_t *)frm);
2191 if (subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
2192 vap->iv_stats.is_rx_deauth++;
2193 IEEE80211_NODE_STAT(ni, rx_deauth);
2195 vap->iv_stats.is_rx_disassoc++;
2196 IEEE80211_NODE_STAT(ni, rx_disassoc);
2198 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2199 "recv %s (reason %d)", ieee80211_mgt_subtype_name[subtype >>
2200 IEEE80211_FC0_SUBTYPE_SHIFT], reason);
2201 if (ni != vap->iv_bss)
2202 ieee80211_node_leave(ni);
2206 case IEEE80211_FC0_SUBTYPE_ACTION:
2207 case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2208 if (ni == vap->iv_bss) {
2209 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2210 wh, NULL, "%s", "unknown node");
2211 vap->iv_stats.is_rx_mgtdiscard++;
2212 } else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2213 !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2214 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2215 wh, NULL, "%s", "not for us");
2216 vap->iv_stats.is_rx_mgtdiscard++;
2217 } else if (vap->iv_state != IEEE80211_S_RUN) {
2218 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2219 wh, NULL, "wrong state %s",
2220 ieee80211_state_name[vap->iv_state]);
2221 vap->iv_stats.is_rx_mgtdiscard++;
2223 if (ieee80211_parse_action(ni, m0) == 0)
2224 (void)ic->ic_recv_action(ni, wh, frm, efrm);
2228 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2229 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2230 case IEEE80211_FC0_SUBTYPE_ATIM:
2231 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2232 wh, NULL, "%s", "not handled");
2233 vap->iv_stats.is_rx_mgtdiscard++;
2237 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2238 wh, "mgt", "subtype 0x%x not handled", subtype);
2239 vap->iv_stats.is_rx_badsubtype++;
2245 hostap_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2248 case IEEE80211_FC0_SUBTYPE_PS_POLL:
2249 ni->ni_vap->iv_recv_pspoll(ni, m);
2251 case IEEE80211_FC0_SUBTYPE_BAR:
2252 ieee80211_recv_bar(ni, m);
2258 * Process a received ps-poll frame.
2261 ieee80211_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m0)
2263 struct ieee80211vap *vap = ni->ni_vap;
2264 struct ieee80211com *ic = vap->iv_ic;
2265 struct ieee80211_frame_min *wh;
2270 wh = mtod(m0, struct ieee80211_frame_min *);
2271 if (ni->ni_associd == 0) {
2272 IEEE80211_DISCARD(vap,
2273 IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2274 (struct ieee80211_frame *) wh, NULL,
2275 "%s", "unassociated station");
2276 vap->iv_stats.is_ps_unassoc++;
2277 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
2278 IEEE80211_REASON_NOT_ASSOCED);
2282 aid = le16toh(*(uint16_t *)wh->i_dur);
2283 if (aid != ni->ni_associd) {
2284 IEEE80211_DISCARD(vap,
2285 IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2286 (struct ieee80211_frame *) wh, NULL,
2287 "aid mismatch: sta aid 0x%x poll aid 0x%x",
2288 ni->ni_associd, aid);
2289 vap->iv_stats.is_ps_badaid++;
2291 * NB: We used to deauth the station but it turns out
2292 * the Blackberry Curve 8230 (and perhaps other devices)
2293 * sometimes send the wrong AID when WME is negotiated.
2294 * Being more lenient here seems ok as we already check
2295 * the station is associated and we only return frames
2296 * queued for the station (i.e. we don't use the AID).
2301 /* Okay, take the first queued packet and put it out... */
2302 m = ieee80211_node_psq_dequeue(ni, &qlen);
2304 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_POWER, wh->i_addr2,
2305 "%s", "recv ps-poll, but queue empty");
2306 ieee80211_send_nulldata(ieee80211_ref_node(ni));
2307 vap->iv_stats.is_ps_qempty++; /* XXX node stat */
2308 if (vap->iv_set_tim != NULL)
2309 vap->iv_set_tim(ni, 0); /* just in case */
2313 * If there are more packets, set the more packets bit
2314 * in the packet dispatched to the station; otherwise
2315 * turn off the TIM bit.
2318 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2319 "recv ps-poll, send packet, %u still queued", qlen);
2320 m->m_flags |= M_MORE_DATA;
2322 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2323 "%s", "recv ps-poll, send packet, queue empty");
2324 if (vap->iv_set_tim != NULL)
2325 vap->iv_set_tim(ni, 0);
2327 m->m_flags |= M_PWR_SAV; /* bypass PS handling */
2330 * Do the right thing; if it's an encap'ed frame then
2331 * call ieee80211_parent_xmitpkt() (and free the ref) else
2332 * call ieee80211_vap_xmitpkt().
2334 if (m->m_flags & M_ENCAP) {
2335 if (ieee80211_parent_xmitpkt(ic, m) != 0)
2336 ieee80211_free_node(ni);
2338 (void) ieee80211_vap_xmitpkt(vap, m);