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,
71 const struct ieee80211_rx_stats *,
73 static void hostap_deliver_data(struct ieee80211vap *,
74 struct ieee80211_node *, struct mbuf *);
75 static void hostap_recv_mgmt(struct ieee80211_node *, struct mbuf *,
76 int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf);
77 static void hostap_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
80 ieee80211_hostap_attach(struct ieee80211com *ic)
82 ic->ic_vattach[IEEE80211_M_HOSTAP] = hostap_vattach;
86 ieee80211_hostap_detach(struct ieee80211com *ic)
91 hostap_vdetach(struct ieee80211vap *vap)
96 hostap_vattach(struct ieee80211vap *vap)
98 vap->iv_newstate = hostap_newstate;
99 vap->iv_input = hostap_input;
100 vap->iv_recv_mgmt = hostap_recv_mgmt;
101 vap->iv_recv_ctl = hostap_recv_ctl;
102 vap->iv_opdetach = hostap_vdetach;
103 vap->iv_deliver_data = hostap_deliver_data;
104 vap->iv_recv_pspoll = ieee80211_recv_pspoll;
108 sta_disassoc(void *arg, struct ieee80211_node *ni)
111 if (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 = ni->ni_vap;
123 if (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)
135 if (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_vap(&ic->ic_sta, 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_vap(&ic->ic_sta, vap,
200 * Clear overlapping BSS state; the beacon frame
201 * will be reconstructed on transition to the RUN
202 * state and the timeout routines check if the flag
203 * is set before doing anything so this is sufficient.
205 ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
206 ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
208 case IEEE80211_S_CAC:
210 * NB: We may get here because of a manual channel
211 * change in which case we need to stop CAC
212 * XXX no need to stop if ostate RUN but it's ok
214 ieee80211_dfs_cac_stop(vap);
216 case IEEE80211_S_INIT:
217 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
218 !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
220 * Already have a channel; bypass the
221 * scan and startup immediately.
222 * ieee80211_create_ibss will call back to
223 * move us to RUN state.
225 ieee80211_create_ibss(vap, vap->iv_des_chan);
229 * Initiate a scan. We can come here as a result
230 * of an IEEE80211_IOC_SCAN_REQ too in which case
231 * the vap will be marked with IEEE80211_FEXT_SCANREQ
232 * and the scan request parameters will be present
233 * in iv_scanreq. Otherwise we do the default.
235 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
236 ieee80211_check_scan(vap,
237 vap->iv_scanreq_flags,
238 vap->iv_scanreq_duration,
239 vap->iv_scanreq_mindwell,
240 vap->iv_scanreq_maxdwell,
241 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
242 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
244 ieee80211_check_scan_current(vap);
246 case IEEE80211_S_SCAN:
248 * A state change requires a reset; scan.
250 ieee80211_check_scan_current(vap);
256 case IEEE80211_S_CAC:
258 * Start CAC on a DFS channel. We come here when starting
259 * a bss on a DFS channel (see ieee80211_create_ibss).
261 ieee80211_dfs_cac_start(vap);
263 case IEEE80211_S_RUN:
264 if (vap->iv_flags & IEEE80211_F_WPA) {
265 /* XXX validate prerequisites */
268 case IEEE80211_S_INIT:
270 * Already have a channel; bypass the
271 * scan and startup immediately.
272 * Note that ieee80211_create_ibss will call
273 * back to do a RUN->RUN state change.
275 ieee80211_create_ibss(vap,
276 ieee80211_ht_adjust_channel(ic,
277 ic->ic_curchan, vap->iv_flags_ht));
278 /* NB: iv_bss is changed on return */
280 case IEEE80211_S_CAC:
282 * NB: This is the normal state change when CAC
283 * expires and no radar was detected; no need to
284 * clear the CAC timer as it's already expired.
287 case IEEE80211_S_CSA:
289 * Shorten inactivity timer of associated stations
290 * to weed out sta's that don't follow a CSA.
292 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap,
295 * Update bss node channel to reflect where
296 * we landed after CSA.
298 ieee80211_node_set_chan(vap->iv_bss,
299 ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
300 ieee80211_htchanflags(vap->iv_bss->ni_chan)));
301 /* XXX bypass debug msgs */
303 case IEEE80211_S_SCAN:
304 case IEEE80211_S_RUN:
305 #ifdef IEEE80211_DEBUG
306 if (ieee80211_msg_debug(vap)) {
307 struct ieee80211_node *ni = vap->iv_bss;
309 "synchronized with %s ssid ",
310 ether_sprintf(ni->ni_bssid));
311 ieee80211_print_essid(ni->ni_essid,
314 printf(" channel %d start %uMb\n",
315 ieee80211_chan2ieee(ic, ic->ic_curchan),
316 IEEE80211_RATE2MBS(ni->ni_txrate));
324 * Start/stop the authenticator. We delay until here
325 * to allow configuration to happen out of order.
327 if (vap->iv_auth->ia_attach != NULL) {
328 /* XXX check failure */
329 vap->iv_auth->ia_attach(vap);
330 } else if (vap->iv_auth->ia_detach != NULL) {
331 vap->iv_auth->ia_detach(vap);
333 ieee80211_node_authorize(vap->iv_bss);
335 case IEEE80211_S_CSA:
336 if (ostate == IEEE80211_S_RUN && isbandchange(ic)) {
338 * On a ``band change'' silently drop associated
339 * stations as they must re-associate before they
340 * can pass traffic (as otherwise protocol state
341 * such as capabilities and the negotiated rate
342 * set may/will be wrong).
344 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap,
355 hostap_deliver_data(struct ieee80211vap *vap,
356 struct ieee80211_node *ni, struct mbuf *m)
358 struct ether_header *eh = mtod(m, struct ether_header *);
359 struct ifnet *ifp = vap->iv_ifp;
361 /* clear driver/net80211 flags before passing up */
362 m->m_flags &= ~(M_MCAST | M_BCAST);
365 KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
366 ("gack, opmode %d", vap->iv_opmode));
370 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
371 IEEE80211_NODE_STAT(ni, rx_data);
372 IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
373 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
374 m->m_flags |= M_MCAST; /* XXX M_BCAST? */
375 IEEE80211_NODE_STAT(ni, rx_mcast);
377 IEEE80211_NODE_STAT(ni, rx_ucast);
379 /* perform as a bridge within the AP */
380 if ((vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0) {
381 struct mbuf *mcopy = NULL;
383 if (m->m_flags & M_MCAST) {
384 mcopy = m_dup(m, M_NOWAIT);
386 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
388 mcopy->m_flags |= M_MCAST;
391 * Check if the destination is associated with the
392 * same vap and authorized to receive traffic.
393 * Beware of traffic destined for the vap itself;
394 * sending it will not work; just let it be delivered
397 struct ieee80211_node *sta = ieee80211_find_vap_node(
398 &vap->iv_ic->ic_sta, vap, eh->ether_dhost);
400 if (ieee80211_node_is_authorized(sta)) {
402 * Beware of sending to ourself; this
403 * needs to happen via the normal
406 if (sta != vap->iv_bss) {
411 vap->iv_stats.is_rx_unauth++;
412 IEEE80211_NODE_STAT(sta, rx_unauth);
414 ieee80211_free_node(sta);
418 (void) ieee80211_vap_xmitpkt(vap, mcopy);
422 * Mark frame as coming from vap's interface.
424 m->m_pkthdr.rcvif = ifp;
425 if (m->m_flags & M_MCAST) {
427 * Spam DWDS vap's w/ multicast traffic.
429 /* XXX only if dwds in use? */
430 ieee80211_dwds_mcast(vap, m);
432 if (ni->ni_vlan != 0) {
433 /* attach vlan tag */
434 m->m_pkthdr.ether_vtag = ni->ni_vlan;
435 m->m_flags |= M_VLANTAG;
437 ifp->if_input(ifp, m);
442 * Decide if a received management frame should be
443 * printed when debugging is enabled. This filters some
444 * of the less interesting frames that come frequently
448 doprint(struct ieee80211vap *vap, int subtype)
451 case IEEE80211_FC0_SUBTYPE_BEACON:
452 return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
453 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
460 * Process a received frame. The node associated with the sender
461 * should be supplied. If nothing was found in the node table then
462 * the caller is assumed to supply a reference to iv_bss instead.
463 * The RSSI and a timestamp are also supplied. The RSSI data is used
464 * during AP scanning to select a AP to associate with; it can have
465 * any units so long as values have consistent units and higher values
466 * mean ``better signal''. The receive timestamp is currently not used
467 * by the 802.11 layer.
470 hostap_input(struct ieee80211_node *ni, struct mbuf *m,
471 const struct ieee80211_rx_stats *rxs, int rssi, int nf)
473 struct ieee80211vap *vap = ni->ni_vap;
474 struct ieee80211com *ic = ni->ni_ic;
475 struct ifnet *ifp = vap->iv_ifp;
476 struct ieee80211_frame *wh;
477 struct ieee80211_key *key;
478 struct ether_header *eh;
479 int hdrspace, need_tap = 1; /* mbuf need to be tapped. */
480 uint8_t dir, type, subtype, qos;
482 int is_hw_decrypted = 0;
483 int has_decrypted = 0;
486 * Some devices do hardware decryption all the way through
487 * to pretending the frame wasn't encrypted in the first place.
488 * So, tag it appropriately so it isn't discarded inappropriately.
490 if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED))
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);
574 if (IEEE80211_HAS_SEQ(type, subtype)) {
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 if (! ieee80211_check_rxseq(ni, wh, bssid))
585 case IEEE80211_FC0_TYPE_DATA:
586 hdrspace = ieee80211_hdrspace(ic, wh);
587 if (m->m_len < hdrspace &&
588 (m = m_pullup(m, hdrspace)) == NULL) {
589 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
590 ni->ni_macaddr, NULL,
591 "data too short: expecting %u", hdrspace);
592 vap->iv_stats.is_rx_tooshort++;
595 if (!(dir == IEEE80211_FC1_DIR_TODS ||
596 (dir == IEEE80211_FC1_DIR_DSTODS &&
597 (vap->iv_flags & IEEE80211_F_DWDS)))) {
598 if (dir != IEEE80211_FC1_DIR_DSTODS) {
599 IEEE80211_DISCARD(vap,
600 IEEE80211_MSG_INPUT, wh, "data",
601 "incorrect dir 0x%x", dir);
603 IEEE80211_DISCARD(vap,
604 IEEE80211_MSG_INPUT |
605 IEEE80211_MSG_WDS, wh,
607 "%s", "DWDS not enabled");
609 vap->iv_stats.is_rx_wrongdir++;
612 /* check if source STA is associated */
613 if (ni == vap->iv_bss) {
614 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
615 wh, "data", "%s", "unknown src");
616 ieee80211_send_error(ni, wh->i_addr2,
617 IEEE80211_FC0_SUBTYPE_DEAUTH,
618 IEEE80211_REASON_NOT_AUTHED);
619 vap->iv_stats.is_rx_notassoc++;
622 if (ni->ni_associd == 0) {
623 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
624 wh, "data", "%s", "unassoc src");
625 IEEE80211_SEND_MGMT(ni,
626 IEEE80211_FC0_SUBTYPE_DISASSOC,
627 IEEE80211_REASON_NOT_ASSOCED);
628 vap->iv_stats.is_rx_notassoc++;
633 * Check for power save state change.
634 * XXX out-of-order A-MPDU frames?
636 if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
637 (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
639 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
641 * For 4-address packets handle WDS discovery
642 * notifications. Once a WDS link is setup frames
643 * are just delivered to the WDS vap (see below).
645 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap == NULL) {
646 if (!ieee80211_node_is_authorized(ni)) {
647 IEEE80211_DISCARD(vap,
648 IEEE80211_MSG_INPUT |
649 IEEE80211_MSG_WDS, wh,
651 "%s", "unauthorized port");
652 vap->iv_stats.is_rx_unauth++;
653 IEEE80211_NODE_STAT(ni, rx_unauth);
656 ieee80211_dwds_discover(ni, m);
661 * Handle A-MPDU re-ordering. If the frame is to be
662 * processed directly then ieee80211_ampdu_reorder
663 * will return 0; otherwise it has consumed the mbuf
664 * and we should do nothing more with it.
666 if ((m->m_flags & M_AMPDU) &&
667 ieee80211_ampdu_reorder(ni, m) != 0) {
674 * Handle privacy requirements. Note that we
675 * must not be preempted from here until after
676 * we (potentially) call ieee80211_crypto_demic;
677 * otherwise we may violate assumptions in the
678 * crypto cipher modules used to do delayed update
679 * of replay sequence numbers.
681 if (is_hw_decrypted || wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
682 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
684 * Discard encrypted frames when privacy is off.
686 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
687 wh, "WEP", "%s", "PRIVACY off");
688 vap->iv_stats.is_rx_noprivacy++;
689 IEEE80211_NODE_STAT(ni, rx_noprivacy);
692 if (ieee80211_crypto_decap(ni, m, hdrspace, &key) == 0) {
693 /* NB: stats+msgs handled in crypto_decap */
694 IEEE80211_NODE_STAT(ni, rx_wepfail);
697 wh = mtod(m, struct ieee80211_frame *);
698 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
701 /* XXX M_WEP and IEEE80211_F_PRIVACY */
706 * Save QoS bits for use below--before we strip the header.
708 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
709 qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
710 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
711 ((struct ieee80211_qosframe *)wh)->i_qos[0];
716 * Next up, any fragmentation.
718 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
719 m = ieee80211_defrag(ni, m, hdrspace);
721 /* Fragment dropped or frame not complete yet */
725 wh = NULL; /* no longer valid, catch any uses */
728 * Next strip any MSDU crypto bits.
730 if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
731 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
732 ni->ni_macaddr, "data", "%s", "demic error");
733 vap->iv_stats.is_rx_demicfail++;
734 IEEE80211_NODE_STAT(ni, rx_demicfail);
737 /* copy to listener after decrypt */
738 if (ieee80211_radiotap_active_vap(vap))
739 ieee80211_radiotap_rx(vap, m);
742 * Finally, strip the 802.11 header.
744 m = ieee80211_decap(vap, m, hdrspace);
746 /* XXX mask bit to check for both */
747 /* don't count Null data frames as errors */
748 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
749 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
751 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
752 ni->ni_macaddr, "data", "%s", "decap error");
753 vap->iv_stats.is_rx_decap++;
754 IEEE80211_NODE_STAT(ni, rx_decap);
757 eh = mtod(m, struct ether_header *);
758 if (!ieee80211_node_is_authorized(ni)) {
760 * Deny any non-PAE frames received prior to
761 * authorization. For open/shared-key
762 * authentication the port is mark authorized
763 * after authentication completes. For 802.1x
764 * the port is not marked authorized by the
765 * authenticator until the handshake has completed.
767 if (eh->ether_type != htons(ETHERTYPE_PAE)) {
768 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
769 eh->ether_shost, "data",
770 "unauthorized port: ether type 0x%x len %u",
771 eh->ether_type, m->m_pkthdr.len);
772 vap->iv_stats.is_rx_unauth++;
773 IEEE80211_NODE_STAT(ni, rx_unauth);
778 * When denying unencrypted frames, discard
779 * any non-PAE frames received without encryption.
781 if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
782 ((has_decrypted == 0) && (m->m_flags & M_WEP) == 0) &&
783 (is_hw_decrypted == 0) &&
784 eh->ether_type != htons(ETHERTYPE_PAE)) {
786 * Drop unencrypted frames.
788 vap->iv_stats.is_rx_unencrypted++;
789 IEEE80211_NODE_STAT(ni, rx_unencrypted);
793 /* XXX require HT? */
794 if (qos & IEEE80211_QOS_AMSDU) {
795 m = ieee80211_decap_amsdu(ni, m);
797 return IEEE80211_FC0_TYPE_DATA;
799 #ifdef IEEE80211_SUPPORT_SUPERG
800 m = ieee80211_decap_fastframe(vap, ni, m);
802 return IEEE80211_FC0_TYPE_DATA;
805 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
806 ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
808 hostap_deliver_data(vap, ni, m);
809 return IEEE80211_FC0_TYPE_DATA;
811 case IEEE80211_FC0_TYPE_MGT:
812 vap->iv_stats.is_rx_mgmt++;
813 IEEE80211_NODE_STAT(ni, rx_mgmt);
814 if (dir != IEEE80211_FC1_DIR_NODS) {
815 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
816 wh, "mgt", "incorrect dir 0x%x", dir);
817 vap->iv_stats.is_rx_wrongdir++;
820 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
821 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
822 ni->ni_macaddr, "mgt", "too short: len %u",
824 vap->iv_stats.is_rx_tooshort++;
827 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
828 /* ensure return frames are unicast */
829 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
830 wh, NULL, "source is multicast: %s",
831 ether_sprintf(wh->i_addr2));
832 vap->iv_stats.is_rx_mgtdiscard++; /* XXX stat */
835 #ifdef IEEE80211_DEBUG
836 if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
837 ieee80211_msg_dumppkts(vap)) {
838 if_printf(ifp, "received %s from %s rssi %d\n",
839 ieee80211_mgt_subtype_name(subtype),
840 ether_sprintf(wh->i_addr2), rssi);
843 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
844 if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
846 * Only shared key auth frames with a challenge
847 * should be encrypted, discard all others.
849 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
851 "%s", "WEP set but not permitted");
852 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
855 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
857 * Discard encrypted frames when privacy is off.
859 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
860 wh, NULL, "%s", "WEP set but PRIVACY off");
861 vap->iv_stats.is_rx_noprivacy++;
864 hdrspace = ieee80211_hdrspace(ic, wh);
865 if (ieee80211_crypto_decap(ni, m, hdrspace, &key) == 0) {
866 /* NB: stats+msgs handled in crypto_decap */
869 wh = mtod(m, struct ieee80211_frame *);
870 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
874 * Pass the packet to radiotap before calling iv_recv_mgmt().
875 * Otherwise iv_recv_mgmt() might pass another packet to
876 * radiotap, resulting in out of order packet captures.
878 if (ieee80211_radiotap_active_vap(vap))
879 ieee80211_radiotap_rx(vap, m);
881 vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
884 case IEEE80211_FC0_TYPE_CTL:
885 vap->iv_stats.is_rx_ctl++;
886 IEEE80211_NODE_STAT(ni, rx_ctrl);
887 vap->iv_recv_ctl(ni, m, subtype);
890 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
891 wh, "bad", "frame type 0x%x", type);
892 /* should not come here */
896 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
899 if (need_tap && ieee80211_radiotap_active_vap(vap))
900 ieee80211_radiotap_rx(vap, m);
907 hostap_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
908 int rssi, int nf, uint16_t seq, uint16_t status)
910 struct ieee80211vap *vap = ni->ni_vap;
912 KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
914 if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
915 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
916 ni->ni_macaddr, "open auth",
917 "bad sta auth mode %u", ni->ni_authmode);
918 vap->iv_stats.is_rx_bad_auth++; /* XXX */
920 * Clear any challenge text that may be there if
921 * a previous shared key auth failed and then an
922 * open auth is attempted.
924 if (ni->ni_challenge != NULL) {
925 IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
926 ni->ni_challenge = NULL;
928 /* XXX hack to workaround calling convention */
929 ieee80211_send_error(ni, wh->i_addr2,
930 IEEE80211_FC0_SUBTYPE_AUTH,
931 (seq + 1) | (IEEE80211_STATUS_ALG<<16));
934 if (seq != IEEE80211_AUTH_OPEN_REQUEST) {
935 vap->iv_stats.is_rx_bad_auth++;
938 /* always accept open authentication requests */
939 if (ni == vap->iv_bss) {
940 ni = ieee80211_dup_bss(vap, wh->i_addr2);
943 } else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
944 (void) ieee80211_ref_node(ni);
946 * Mark the node as referenced to reflect that it's
947 * reference count has been bumped to insure it remains
948 * after the transaction completes.
950 ni->ni_flags |= IEEE80211_NODE_AREF;
952 * Mark the node as requiring a valid association id
953 * before outbound traffic is permitted.
955 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
957 if (vap->iv_acl != NULL &&
958 vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
960 * When the ACL policy is set to RADIUS we defer the
961 * authorization to a user agent. Dispatch an event,
962 * a subsequent MLME call will decide the fate of the
963 * station. If the user agent is not present then the
964 * node will be reclaimed due to inactivity.
966 IEEE80211_NOTE_MAC(vap,
967 IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL, ni->ni_macaddr,
968 "%s", "station authentication defered (radius acl)");
969 ieee80211_notify_node_auth(ni);
971 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
972 IEEE80211_NOTE_MAC(vap,
973 IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni->ni_macaddr,
974 "%s", "station authenticated (open)");
976 * When 802.1x is not in use mark the port
977 * authorized at this point so traffic can flow.
979 if (ni->ni_authmode != IEEE80211_AUTH_8021X)
980 ieee80211_node_authorize(ni);
985 hostap_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
986 uint8_t *frm, uint8_t *efrm, int rssi, int nf,
987 uint16_t seq, uint16_t status)
989 struct ieee80211vap *vap = ni->ni_vap;
991 int allocbs, estatus;
993 KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
996 * NB: this can happen as we allow pre-shared key
997 * authentication to be enabled w/o wep being turned
998 * on so that configuration of these can be done
999 * in any order. It may be better to enforce the
1000 * ordering in which case this check would just be
1001 * for sanity/consistency.
1003 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
1004 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1005 ni->ni_macaddr, "shared key auth",
1006 "%s", " PRIVACY is disabled");
1007 estatus = IEEE80211_STATUS_ALG;
1011 * Pre-shared key authentication is evil; accept
1012 * it only if explicitly configured (it is supported
1013 * mainly for compatibility with clients like Mac OS X).
1015 if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1016 ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1017 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1018 ni->ni_macaddr, "shared key auth",
1019 "bad sta auth mode %u", ni->ni_authmode);
1020 vap->iv_stats.is_rx_bad_auth++; /* XXX maybe a unique error? */
1021 estatus = IEEE80211_STATUS_ALG;
1026 if (frm + 1 < efrm) {
1027 if ((frm[1] + 2) > (efrm - frm)) {
1028 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1029 ni->ni_macaddr, "shared key auth",
1030 "ie %d/%d too long",
1031 frm[0], (frm[1] + 2) - (efrm - frm));
1032 vap->iv_stats.is_rx_bad_auth++;
1033 estatus = IEEE80211_STATUS_CHALLENGE;
1036 if (*frm == IEEE80211_ELEMID_CHALLENGE)
1041 case IEEE80211_AUTH_SHARED_CHALLENGE:
1042 case IEEE80211_AUTH_SHARED_RESPONSE:
1043 if (challenge == NULL) {
1044 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1045 ni->ni_macaddr, "shared key auth",
1046 "%s", "no challenge");
1047 vap->iv_stats.is_rx_bad_auth++;
1048 estatus = IEEE80211_STATUS_CHALLENGE;
1051 if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1052 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1053 ni->ni_macaddr, "shared key auth",
1054 "bad challenge len %d", challenge[1]);
1055 vap->iv_stats.is_rx_bad_auth++;
1056 estatus = IEEE80211_STATUS_CHALLENGE;
1063 case IEEE80211_AUTH_SHARED_REQUEST:
1064 if (ni == vap->iv_bss) {
1065 ni = ieee80211_dup_bss(vap, wh->i_addr2);
1067 /* NB: no way to return an error */
1072 if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1073 (void) ieee80211_ref_node(ni);
1077 * Mark the node as referenced to reflect that it's
1078 * reference count has been bumped to insure it remains
1079 * after the transaction completes.
1081 ni->ni_flags |= IEEE80211_NODE_AREF;
1083 * Mark the node as requiring a valid association id
1084 * before outbound traffic is permitted.
1086 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
1087 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1089 if (!ieee80211_alloc_challenge(ni)) {
1090 /* NB: don't return error so they rexmit */
1093 get_random_bytes(ni->ni_challenge,
1094 IEEE80211_CHALLENGE_LEN);
1095 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1096 ni, "shared key %sauth request", allocbs ? "" : "re");
1098 * When the ACL policy is set to RADIUS we defer the
1099 * authorization to a user agent. Dispatch an event,
1100 * a subsequent MLME call will decide the fate of the
1101 * station. If the user agent is not present then the
1102 * node will be reclaimed due to inactivity.
1104 if (vap->iv_acl != NULL &&
1105 vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
1106 IEEE80211_NOTE_MAC(vap,
1107 IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL,
1109 "%s", "station authentication defered (radius acl)");
1110 ieee80211_notify_node_auth(ni);
1114 case IEEE80211_AUTH_SHARED_RESPONSE:
1115 if (ni == vap->iv_bss) {
1116 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1117 ni->ni_macaddr, "shared key response",
1118 "%s", "unknown station");
1119 /* NB: don't send a response */
1122 if (ni->ni_challenge == NULL) {
1123 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1124 ni->ni_macaddr, "shared key response",
1125 "%s", "no challenge recorded");
1126 vap->iv_stats.is_rx_bad_auth++;
1127 estatus = IEEE80211_STATUS_CHALLENGE;
1130 if (memcmp(ni->ni_challenge, &challenge[2],
1131 challenge[1]) != 0) {
1132 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1133 ni->ni_macaddr, "shared key response",
1134 "%s", "challenge mismatch");
1135 vap->iv_stats.is_rx_auth_fail++;
1136 estatus = IEEE80211_STATUS_CHALLENGE;
1139 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1140 ni, "%s", "station authenticated (shared key)");
1141 ieee80211_node_authorize(ni);
1144 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1145 ni->ni_macaddr, "shared key auth",
1147 vap->iv_stats.is_rx_bad_auth++;
1148 estatus = IEEE80211_STATUS_SEQUENCE;
1151 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1155 * Send an error response; but only when operating as an AP.
1157 /* XXX hack to workaround calling convention */
1158 ieee80211_send_error(ni, wh->i_addr2,
1159 IEEE80211_FC0_SUBTYPE_AUTH,
1160 (seq + 1) | (estatus<<16));
1164 * Convert a WPA cipher selector OUI to an internal
1165 * cipher algorithm. Where appropriate we also
1166 * record any key length.
1169 wpa_cipher(const uint8_t *sel, uint8_t *keylen, uint8_t *cipher)
1171 #define WPA_SEL(x) (((x)<<24)|WPA_OUI)
1172 uint32_t w = le32dec(sel);
1175 case WPA_SEL(WPA_CSE_NULL):
1176 *cipher = IEEE80211_CIPHER_NONE;
1178 case WPA_SEL(WPA_CSE_WEP40):
1180 *keylen = 40 / NBBY;
1181 *cipher = IEEE80211_CIPHER_WEP;
1183 case WPA_SEL(WPA_CSE_WEP104):
1185 *keylen = 104 / NBBY;
1186 *cipher = IEEE80211_CIPHER_WEP;
1188 case WPA_SEL(WPA_CSE_TKIP):
1189 *cipher = IEEE80211_CIPHER_TKIP;
1191 case WPA_SEL(WPA_CSE_CCMP):
1192 *cipher = IEEE80211_CIPHER_AES_CCM;
1203 * Convert a WPA key management/authentication algorithm
1204 * to an internal code.
1207 wpa_keymgmt(const uint8_t *sel)
1209 #define WPA_SEL(x) (((x)<<24)|WPA_OUI)
1210 uint32_t w = le32dec(sel);
1213 case WPA_SEL(WPA_ASE_8021X_UNSPEC):
1214 return WPA_ASE_8021X_UNSPEC;
1215 case WPA_SEL(WPA_ASE_8021X_PSK):
1216 return WPA_ASE_8021X_PSK;
1217 case WPA_SEL(WPA_ASE_NONE):
1218 return WPA_ASE_NONE;
1220 return 0; /* NB: so is discarded */
1225 * Parse a WPA information element to collect parameters.
1226 * Note that we do not validate security parameters; that
1227 * is handled by the authenticator; the parsing done here
1228 * is just for internal use in making operational decisions.
1231 ieee80211_parse_wpa(struct ieee80211vap *vap, const uint8_t *frm,
1232 struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1234 uint8_t len = frm[1];
1239 * Check the length once for fixed parts: OUI, type,
1240 * version, mcast cipher, and 2 selector counts.
1241 * Other, variable-length data, must be checked separately.
1243 if ((vap->iv_flags & IEEE80211_F_WPA1) == 0) {
1244 IEEE80211_DISCARD_IE(vap,
1245 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1246 wh, "WPA", "not WPA, flags 0x%x", vap->iv_flags);
1247 return IEEE80211_REASON_IE_INVALID;
1250 IEEE80211_DISCARD_IE(vap,
1251 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1252 wh, "WPA", "too short, len %u", len);
1253 return IEEE80211_REASON_IE_INVALID;
1255 frm += 6, len -= 4; /* NB: len is payload only */
1256 /* NB: iswpaoui already validated the OUI and type */
1258 if (w != WPA_VERSION) {
1259 IEEE80211_DISCARD_IE(vap,
1260 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1261 wh, "WPA", "bad version %u", w);
1262 return IEEE80211_REASON_IE_INVALID;
1266 memset(rsn, 0, sizeof(*rsn));
1268 /* multicast/group cipher */
1269 error = wpa_cipher(frm, &rsn->rsn_mcastkeylen, &rsn->rsn_mcastcipher);
1271 IEEE80211_DISCARD_IE(vap,
1272 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1273 wh, "WPA", "unknown mcast cipher suite %08X",
1275 return IEEE80211_REASON_GROUP_CIPHER_INVALID;
1279 /* unicast ciphers */
1283 IEEE80211_DISCARD_IE(vap,
1284 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1285 wh, "WPA", "ucast cipher data too short; len %u, n %u",
1287 return IEEE80211_REASON_IE_INVALID;
1290 for (; n > 0; n--) {
1293 error = wpa_cipher(frm, &rsn->rsn_ucastkeylen, &cipher);
1300 IEEE80211_DISCARD_IE(vap,
1301 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1302 wh, "WPA", "no usable pairwise cipher suite found (w=%d)",
1304 return IEEE80211_REASON_PAIRWISE_CIPHER_INVALID;
1307 if (w & (1 << IEEE80211_CIPHER_AES_CCM))
1308 rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1310 rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1312 /* key management algorithms */
1316 IEEE80211_DISCARD_IE(vap,
1317 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1318 wh, "WPA", "key mgmt alg data too short; len %u, n %u",
1320 return IEEE80211_REASON_IE_INVALID;
1323 for (; n > 0; n--) {
1324 w |= wpa_keymgmt(frm);
1327 if (w & WPA_ASE_8021X_UNSPEC)
1328 rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
1330 rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1332 if (len > 2) /* optional capabilities */
1333 rsn->rsn_caps = le16dec(frm);
1339 * Convert an RSN cipher selector OUI to an internal
1340 * cipher algorithm. Where appropriate we also
1341 * record any key length.
1344 rsn_cipher(const uint8_t *sel, uint8_t *keylen, uint8_t *cipher)
1346 #define RSN_SEL(x) (((x)<<24)|RSN_OUI)
1347 uint32_t w = le32dec(sel);
1350 case RSN_SEL(RSN_CSE_NULL):
1351 *cipher = IEEE80211_CIPHER_NONE;
1353 case RSN_SEL(RSN_CSE_WEP40):
1355 *keylen = 40 / NBBY;
1356 *cipher = IEEE80211_CIPHER_WEP;
1358 case RSN_SEL(RSN_CSE_WEP104):
1360 *keylen = 104 / NBBY;
1361 *cipher = IEEE80211_CIPHER_WEP;
1363 case RSN_SEL(RSN_CSE_TKIP):
1364 *cipher = IEEE80211_CIPHER_TKIP;
1366 case RSN_SEL(RSN_CSE_CCMP):
1367 *cipher = IEEE80211_CIPHER_AES_CCM;
1369 case RSN_SEL(RSN_CSE_WRAP):
1370 *cipher = IEEE80211_CIPHER_AES_OCB;
1381 * Convert an RSN key management/authentication algorithm
1382 * to an internal code.
1385 rsn_keymgmt(const uint8_t *sel)
1387 #define RSN_SEL(x) (((x)<<24)|RSN_OUI)
1388 uint32_t w = le32dec(sel);
1391 case RSN_SEL(RSN_ASE_8021X_UNSPEC):
1392 return RSN_ASE_8021X_UNSPEC;
1393 case RSN_SEL(RSN_ASE_8021X_PSK):
1394 return RSN_ASE_8021X_PSK;
1395 case RSN_SEL(RSN_ASE_NONE):
1396 return RSN_ASE_NONE;
1398 return 0; /* NB: so is discarded */
1403 * Parse a WPA/RSN information element to collect parameters
1404 * and validate the parameters against what has been
1405 * configured for the system.
1408 ieee80211_parse_rsn(struct ieee80211vap *vap, const uint8_t *frm,
1409 struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1411 uint8_t len = frm[1];
1416 * Check the length once for fixed parts:
1417 * version, mcast cipher, and 2 selector counts.
1418 * Other, variable-length data, must be checked separately.
1420 if ((vap->iv_flags & IEEE80211_F_WPA2) == 0) {
1421 IEEE80211_DISCARD_IE(vap,
1422 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1423 wh, "WPA", "not RSN, flags 0x%x", vap->iv_flags);
1424 return IEEE80211_REASON_IE_INVALID;
1426 /* XXX may be shorter */
1428 IEEE80211_DISCARD_IE(vap,
1429 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1430 wh, "RSN", "too short, len %u", len);
1431 return IEEE80211_REASON_IE_INVALID;
1435 if (w != RSN_VERSION) {
1436 IEEE80211_DISCARD_IE(vap,
1437 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1438 wh, "RSN", "bad version %u", w);
1439 return IEEE80211_REASON_UNSUPP_RSN_IE_VERSION;
1443 memset(rsn, 0, sizeof(*rsn));
1445 /* multicast/group cipher */
1446 error = rsn_cipher(frm, &rsn->rsn_mcastkeylen, &rsn->rsn_mcastcipher);
1448 IEEE80211_DISCARD_IE(vap,
1449 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1450 wh, "RSN", "unknown mcast cipher suite %08X",
1452 return IEEE80211_REASON_GROUP_CIPHER_INVALID;
1454 if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_NONE) {
1455 IEEE80211_DISCARD_IE(vap,
1456 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1457 wh, "RSN", "invalid mcast cipher suite %d",
1458 rsn->rsn_mcastcipher);
1459 return IEEE80211_REASON_GROUP_CIPHER_INVALID;
1463 /* unicast ciphers */
1467 IEEE80211_DISCARD_IE(vap,
1468 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1469 wh, "RSN", "ucast cipher data too short; len %u, n %u",
1471 return IEEE80211_REASON_IE_INVALID;
1475 for (; n > 0; n--) {
1478 error = rsn_cipher(frm, &rsn->rsn_ucastkeylen, &cipher);
1484 if (w & (1 << IEEE80211_CIPHER_AES_CCM))
1485 rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1486 else if (w & (1 << IEEE80211_CIPHER_AES_OCB))
1487 rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_OCB;
1488 else if (w & (1 << IEEE80211_CIPHER_TKIP))
1489 rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1490 else if ((w & (1 << IEEE80211_CIPHER_NONE)) &&
1491 (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP ||
1492 rsn->rsn_mcastcipher == IEEE80211_CIPHER_TKIP))
1493 rsn->rsn_ucastcipher = IEEE80211_CIPHER_NONE;
1495 IEEE80211_DISCARD_IE(vap,
1496 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1497 wh, "RSN", "no usable pairwise cipher suite found (w=%d)",
1499 return IEEE80211_REASON_PAIRWISE_CIPHER_INVALID;
1502 /* key management algorithms */
1506 IEEE80211_DISCARD_IE(vap,
1507 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1508 wh, "RSN", "key mgmt alg data too short; len %u, n %u",
1510 return IEEE80211_REASON_IE_INVALID;
1513 for (; n > 0; n--) {
1514 w |= rsn_keymgmt(frm);
1517 if (w & RSN_ASE_8021X_UNSPEC)
1518 rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
1520 rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
1522 /* optional RSN capabilities */
1524 rsn->rsn_caps = le16dec(frm);
1531 * WPA/802.11i association request processing.
1534 wpa_assocreq(struct ieee80211_node *ni, struct ieee80211_rsnparms *rsnparms,
1535 const struct ieee80211_frame *wh, const uint8_t *wpa,
1536 const uint8_t *rsn, uint16_t capinfo)
1538 struct ieee80211vap *vap = ni->ni_vap;
1542 ni->ni_flags &= ~(IEEE80211_NODE_WPS|IEEE80211_NODE_TSN);
1543 if (wpa == NULL && rsn == NULL) {
1544 if (vap->iv_flags_ext & IEEE80211_FEXT_WPS) {
1546 * W-Fi Protected Setup (WPS) permits
1547 * clients to associate and pass EAPOL frames
1548 * to establish initial credentials.
1550 ni->ni_flags |= IEEE80211_NODE_WPS;
1553 if ((vap->iv_flags_ext & IEEE80211_FEXT_TSN) &&
1554 (capinfo & IEEE80211_CAPINFO_PRIVACY)) {
1556 * Transitional Security Network. Permits clients
1557 * to associate and use WEP while WPA is configured.
1559 ni->ni_flags |= IEEE80211_NODE_TSN;
1562 IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1563 wh, NULL, "%s", "no WPA/RSN IE in association request");
1564 vap->iv_stats.is_rx_assoc_badwpaie++;
1565 reason = IEEE80211_REASON_IE_INVALID;
1568 /* assert right association security credentials */
1569 badwparsn = 0; /* NB: to silence compiler */
1570 switch (vap->iv_flags & IEEE80211_F_WPA) {
1571 case IEEE80211_F_WPA1:
1572 badwparsn = (wpa == NULL);
1574 case IEEE80211_F_WPA2:
1575 badwparsn = (rsn == NULL);
1577 case IEEE80211_F_WPA1|IEEE80211_F_WPA2:
1578 badwparsn = (wpa == NULL && rsn == NULL);
1582 IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1584 "%s", "missing WPA/RSN IE in association request");
1585 vap->iv_stats.is_rx_assoc_badwpaie++;
1586 reason = IEEE80211_REASON_IE_INVALID;
1590 * Parse WPA/RSN information element.
1593 reason = ieee80211_parse_wpa(vap, wpa, rsnparms, wh);
1595 reason = ieee80211_parse_rsn(vap, rsn, rsnparms, wh);
1597 /* XXX wpa->rsn fallback? */
1598 /* XXX distinguish WPA/RSN? */
1599 vap->iv_stats.is_rx_assoc_badwpaie++;
1602 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA, ni,
1603 "%s ie: mc %u/%u uc %u/%u key %u caps 0x%x",
1604 wpa != NULL ? "WPA" : "RSN",
1605 rsnparms->rsn_mcastcipher, rsnparms->rsn_mcastkeylen,
1606 rsnparms->rsn_ucastcipher, rsnparms->rsn_ucastkeylen,
1607 rsnparms->rsn_keymgmt, rsnparms->rsn_caps);
1611 ieee80211_node_deauth(ni, reason);
1615 /* XXX find a better place for definition */
1616 struct l2_update_frame {
1617 struct ether_header eh;
1625 * Deliver a TGf L2UF frame on behalf of a station.
1626 * This primes any bridge when the station is roaming
1627 * between ap's on the same wired network.
1630 ieee80211_deliver_l2uf(struct ieee80211_node *ni)
1632 struct ieee80211vap *vap = ni->ni_vap;
1633 struct ifnet *ifp = vap->iv_ifp;
1635 struct l2_update_frame *l2uf;
1636 struct ether_header *eh;
1638 m = m_gethdr(M_NOWAIT, MT_DATA);
1640 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1641 "%s", "no mbuf for l2uf frame");
1642 vap->iv_stats.is_rx_nobuf++; /* XXX not right */
1645 l2uf = mtod(m, struct l2_update_frame *);
1647 /* dst: Broadcast address */
1648 IEEE80211_ADDR_COPY(eh->ether_dhost, ifp->if_broadcastaddr);
1649 /* src: associated STA */
1650 IEEE80211_ADDR_COPY(eh->ether_shost, ni->ni_macaddr);
1651 eh->ether_type = htons(sizeof(*l2uf) - sizeof(*eh));
1655 l2uf->control = 0xf5;
1656 l2uf->xid[0] = 0x81;
1657 l2uf->xid[1] = 0x80;
1658 l2uf->xid[2] = 0x00;
1660 m->m_pkthdr.len = m->m_len = sizeof(*l2uf);
1661 hostap_deliver_data(vap, ni, m);
1665 ratesetmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1666 int reassoc, int resp, const char *tag, int rate)
1668 IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1669 "deny %s request, %s rate set mismatch, rate/MCS %d",
1670 reassoc ? "reassoc" : "assoc", tag, rate & IEEE80211_RATE_VAL);
1671 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_BASIC_RATE);
1672 ieee80211_node_leave(ni);
1676 capinfomismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1677 int reassoc, int resp, const char *tag, int capinfo)
1679 struct ieee80211vap *vap = ni->ni_vap;
1681 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1682 "deny %s request, %s mismatch 0x%x",
1683 reassoc ? "reassoc" : "assoc", tag, capinfo);
1684 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_CAPINFO);
1685 ieee80211_node_leave(ni);
1686 vap->iv_stats.is_rx_assoc_capmismatch++;
1690 htcapmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1691 int reassoc, int resp)
1693 IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1694 "deny %s request, %s missing HT ie", reassoc ? "reassoc" : "assoc");
1695 /* XXX no better code */
1696 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_MISSING_HT_CAPS);
1697 ieee80211_node_leave(ni);
1701 authalgreject(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1702 int algo, int seq, int status)
1704 struct ieee80211vap *vap = ni->ni_vap;
1706 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1707 wh, NULL, "unsupported alg %d", algo);
1708 vap->iv_stats.is_rx_auth_unsupported++;
1709 ieee80211_send_error(ni, wh->i_addr2, IEEE80211_FC0_SUBTYPE_AUTH,
1710 seq | (status << 16));
1714 ishtmixed(const uint8_t *ie)
1716 const struct ieee80211_ie_htinfo *ht =
1717 (const struct ieee80211_ie_htinfo *) ie;
1718 return (ht->hi_byte2 & IEEE80211_HTINFO_OPMODE) ==
1719 IEEE80211_HTINFO_OPMODE_MIXED;
1723 is11bclient(const uint8_t *rates, const uint8_t *xrates)
1725 static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
1728 /* NB: the 11b clients we care about will not have xrates */
1729 if (xrates != NULL || rates == NULL)
1731 for (i = 0; i < rates[1]; i++) {
1732 int r = rates[2+i] & IEEE80211_RATE_VAL;
1733 if (r > 2*11 || ((1<<r) & brates) == 0)
1740 hostap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1741 int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
1743 struct ieee80211vap *vap = ni->ni_vap;
1744 struct ieee80211com *ic = ni->ni_ic;
1745 struct ieee80211_frame *wh;
1746 uint8_t *frm, *efrm, *sfrm;
1747 uint8_t *ssid, *rates, *xrates, *wpa, *rsn, *wme, *ath, *htcap;
1751 wh = mtod(m0, struct ieee80211_frame *);
1752 frm = (uint8_t *)&wh[1];
1753 efrm = mtod(m0, uint8_t *) + m0->m_len;
1755 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1757 * We process beacon/probe response frames when scanning;
1758 * otherwise we check beacon frames for overlapping non-ERP
1759 * BSS in 11g and/or overlapping legacy BSS when in HT.
1761 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1762 vap->iv_stats.is_rx_mgtdiscard++;
1766 case IEEE80211_FC0_SUBTYPE_BEACON: {
1767 struct ieee80211_scanparams scan;
1769 /* NB: accept off-channel frames */
1770 /* XXX TODO: use rxstatus to determine off-channel details */
1771 if (ieee80211_parse_beacon(ni, m0, ic->ic_curchan, &scan) &~ IEEE80211_BPARSE_OFFCHAN)
1774 * Count frame now that we know it's to be processed.
1776 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1777 vap->iv_stats.is_rx_beacon++; /* XXX remove */
1778 IEEE80211_NODE_STAT(ni, rx_beacons);
1780 IEEE80211_NODE_STAT(ni, rx_proberesp);
1782 * If scanning, just pass information to the scan module.
1784 if (ic->ic_flags & IEEE80211_F_SCAN) {
1785 if (scan.status == 0 && /* NB: on channel */
1786 (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN)) {
1788 * Actively scanning a channel marked passive;
1789 * send a probe request now that we know there
1790 * is 802.11 traffic present.
1792 * XXX check if the beacon we recv'd gives
1793 * us what we need and suppress the probe req
1795 ieee80211_probe_curchan(vap, 1);
1796 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1798 ieee80211_add_scan(vap, ic->ic_curchan, &scan, wh,
1803 * Check beacon for overlapping bss w/ non ERP stations.
1804 * If we detect one and protection is configured but not
1805 * enabled, enable it and start a timer that'll bring us
1806 * out if we stop seeing the bss.
1808 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1809 scan.status == 0 && /* NB: on-channel */
1810 ((scan.erp & 0x100) == 0 || /* NB: no ERP, 11b sta*/
1811 (scan.erp & IEEE80211_ERP_NON_ERP_PRESENT))) {
1812 ic->ic_lastnonerp = ticks;
1813 ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
1814 if (ic->ic_protmode != IEEE80211_PROT_NONE &&
1815 (ic->ic_flags & IEEE80211_F_USEPROT) == 0) {
1816 IEEE80211_NOTE_FRAME(vap,
1817 IEEE80211_MSG_ASSOC, wh,
1818 "non-ERP present on channel %d "
1819 "(saw erp 0x%x from channel %d), "
1820 "enable use of protection",
1821 ic->ic_curchan->ic_ieee,
1822 scan.erp, scan.chan);
1823 ic->ic_flags |= IEEE80211_F_USEPROT;
1824 ieee80211_notify_erp(ic);
1828 * Check beacon for non-HT station on HT channel
1829 * and update HT BSS occupancy as appropriate.
1831 if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) {
1832 if (scan.status & IEEE80211_BPARSE_OFFCHAN) {
1834 * Off control channel; only check frames
1835 * that come in the extension channel when
1836 * operating w/ HT40.
1838 if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
1840 if (scan.chan != ic->ic_curchan->ic_extieee)
1843 if (scan.htinfo == NULL) {
1844 ieee80211_htprot_update(ic,
1845 IEEE80211_HTINFO_OPMODE_PROTOPT |
1846 IEEE80211_HTINFO_NONHT_PRESENT);
1847 } else if (ishtmixed(scan.htinfo)) {
1848 /* XXX? take NONHT_PRESENT from beacon? */
1849 ieee80211_htprot_update(ic,
1850 IEEE80211_HTINFO_OPMODE_MIXED |
1851 IEEE80211_HTINFO_NONHT_PRESENT);
1857 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1858 if (vap->iv_state != IEEE80211_S_RUN) {
1859 vap->iv_stats.is_rx_mgtdiscard++;
1863 * Consult the ACL policy module if setup.
1865 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1866 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1867 wh, NULL, "%s", "disallowed by ACL");
1868 vap->iv_stats.is_rx_acl++;
1872 * prreq frame format
1874 * [tlv] supported rates
1875 * [tlv] extended supported rates
1877 ssid = rates = xrates = NULL;
1878 while (efrm - frm > 1) {
1879 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1881 case IEEE80211_ELEMID_SSID:
1884 case IEEE80211_ELEMID_RATES:
1887 case IEEE80211_ELEMID_XRATES:
1893 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1895 IEEE80211_VERIFY_ELEMENT(xrates,
1896 IEEE80211_RATE_MAXSIZE - rates[1], return);
1897 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1898 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
1899 if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
1900 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1902 "%s", "no ssid with ssid suppression enabled");
1903 vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
1907 /* XXX find a better class or define it's own */
1908 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1909 "%s", "recv probe req");
1911 * Some legacy 11b clients cannot hack a complete
1912 * probe response frame. When the request includes
1913 * only a bare-bones rate set, communicate this to
1914 * the transmit side.
1916 ieee80211_send_proberesp(vap, wh->i_addr2,
1917 is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
1920 case IEEE80211_FC0_SUBTYPE_AUTH: {
1921 uint16_t algo, seq, status;
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_wrongbss++; /*XXX unique stat?*/
1940 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1941 algo = le16toh(*(uint16_t *)frm);
1942 seq = le16toh(*(uint16_t *)(frm + 2));
1943 status = le16toh(*(uint16_t *)(frm + 4));
1944 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1945 "recv auth frame with algorithm %d seq %d", algo, seq);
1947 * Consult the ACL policy module if setup.
1949 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1950 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1951 wh, NULL, "%s", "disallowed by ACL");
1952 vap->iv_stats.is_rx_acl++;
1953 ieee80211_send_error(ni, wh->i_addr2,
1954 IEEE80211_FC0_SUBTYPE_AUTH,
1955 (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
1958 if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1959 IEEE80211_DISCARD(vap,
1960 IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1961 wh, NULL, "%s", "TKIP countermeasures enabled");
1962 vap->iv_stats.is_rx_auth_countermeasures++;
1963 ieee80211_send_error(ni, wh->i_addr2,
1964 IEEE80211_FC0_SUBTYPE_AUTH,
1965 IEEE80211_REASON_MIC_FAILURE);
1968 if (algo == IEEE80211_AUTH_ALG_SHARED)
1969 hostap_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1971 else if (algo == IEEE80211_AUTH_ALG_OPEN)
1972 hostap_auth_open(ni, wh, rssi, nf, seq, status);
1973 else if (algo == IEEE80211_AUTH_ALG_LEAP) {
1974 authalgreject(ni, wh, algo,
1975 seq+1, IEEE80211_STATUS_ALG);
1979 * We assume that an unknown algorithm is the result
1980 * of a decryption failure on a shared key auth frame;
1981 * return a status code appropriate for that instead
1982 * of IEEE80211_STATUS_ALG.
1984 * NB: a seq# of 4 is intentional; the decrypted
1985 * frame likely has a bogus seq value.
1987 authalgreject(ni, wh, algo,
1988 4, IEEE80211_STATUS_CHALLENGE);
1994 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1995 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
1996 uint16_t capinfo, lintval;
1997 struct ieee80211_rsnparms rsnparms;
1999 if (vap->iv_state != IEEE80211_S_RUN) {
2000 vap->iv_stats.is_rx_mgtdiscard++;
2003 if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
2004 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2005 wh, NULL, "%s", "wrong bssid");
2006 vap->iv_stats.is_rx_assoc_bss++;
2009 if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2011 resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
2014 resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
2016 if (ni == vap->iv_bss) {
2017 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
2018 "deny %s request, sta not authenticated",
2019 reassoc ? "reassoc" : "assoc");
2020 ieee80211_send_error(ni, wh->i_addr2,
2021 IEEE80211_FC0_SUBTYPE_DEAUTH,
2022 IEEE80211_REASON_ASSOC_NOT_AUTHED);
2023 vap->iv_stats.is_rx_assoc_notauth++;
2028 * asreq frame format
2029 * [2] capability information
2030 * [2] listen interval
2031 * [6*] current AP address (reassoc only)
2033 * [tlv] supported rates
2034 * [tlv] extended supported rates
2036 * [tlv] HT capabilities
2037 * [tlv] Atheros capabilities
2039 IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4), return);
2040 capinfo = le16toh(*(uint16_t *)frm); frm += 2;
2041 lintval = le16toh(*(uint16_t *)frm); frm += 2;
2043 frm += 6; /* ignore current AP info */
2044 ssid = rates = xrates = wpa = rsn = wme = ath = htcap = NULL;
2046 while (efrm - frm > 1) {
2047 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
2049 case IEEE80211_ELEMID_SSID:
2052 case IEEE80211_ELEMID_RATES:
2055 case IEEE80211_ELEMID_XRATES:
2058 case IEEE80211_ELEMID_RSN:
2061 case IEEE80211_ELEMID_HTCAP:
2064 case IEEE80211_ELEMID_VENDOR:
2067 else if (iswmeinfo(frm))
2069 #ifdef IEEE80211_SUPPORT_SUPERG
2070 else if (isatherosoui(frm))
2073 else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
2074 if (ishtcapoui(frm) && htcap == NULL)
2081 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
2083 IEEE80211_VERIFY_ELEMENT(xrates,
2084 IEEE80211_RATE_MAXSIZE - rates[1], return);
2085 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2086 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
2087 if (htcap != NULL) {
2088 IEEE80211_VERIFY_LENGTH(htcap[1],
2089 htcap[0] == IEEE80211_ELEMID_VENDOR ?
2090 4 + sizeof(struct ieee80211_ie_htcap)-2 :
2091 sizeof(struct ieee80211_ie_htcap)-2,
2092 return); /* XXX just NULL out? */
2095 if ((vap->iv_flags & IEEE80211_F_WPA) &&
2096 !wpa_assocreq(ni, &rsnparms, wh, wpa, rsn, capinfo))
2098 /* discard challenge after association */
2099 if (ni->ni_challenge != NULL) {
2100 IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
2101 ni->ni_challenge = NULL;
2103 /* NB: 802.11 spec says to ignore station's privacy bit */
2104 if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
2105 capinfomismatch(ni, wh, reassoc, resp,
2106 "capability", capinfo);
2110 * Disallow re-associate w/ invalid slot time setting.
2112 if (ni->ni_associd != 0 &&
2113 IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2114 ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
2115 capinfomismatch(ni, wh, reassoc, resp,
2116 "slot time", capinfo);
2119 rate = ieee80211_setup_rates(ni, rates, xrates,
2120 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
2121 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2122 if (rate & IEEE80211_RATE_BASIC) {
2123 ratesetmismatch(ni, wh, reassoc, resp, "legacy", rate);
2124 vap->iv_stats.is_rx_assoc_norate++;
2128 * If constrained to 11g-only stations reject an
2129 * 11b-only station. We cheat a bit here by looking
2130 * at the max negotiated xmit rate and assuming anyone
2131 * with a best rate <24Mb/s is an 11b station.
2133 if ((vap->iv_flags & IEEE80211_F_PUREG) && rate < 48) {
2134 ratesetmismatch(ni, wh, reassoc, resp, "11g", rate);
2135 vap->iv_stats.is_rx_assoc_norate++;
2139 * Do HT rate set handling and setup HT node state.
2141 ni->ni_chan = vap->iv_bss->ni_chan;
2142 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
2143 rate = ieee80211_setup_htrates(ni, htcap,
2144 IEEE80211_F_DOFMCS | IEEE80211_F_DONEGO |
2146 if (rate & IEEE80211_RATE_BASIC) {
2147 ratesetmismatch(ni, wh, reassoc, resp,
2149 vap->iv_stats.is_ht_assoc_norate++;
2152 ieee80211_ht_node_init(ni);
2153 ieee80211_ht_updatehtcap(ni, htcap);
2154 } else if (ni->ni_flags & IEEE80211_NODE_HT)
2155 ieee80211_ht_node_cleanup(ni);
2156 #ifdef IEEE80211_SUPPORT_SUPERG
2157 /* Always do ff node cleanup; for A-MSDU */
2158 ieee80211_ff_node_cleanup(ni);
2161 * Allow AMPDU operation only with unencrypted traffic
2162 * or AES-CCM; the 11n spec only specifies these ciphers
2163 * so permitting any others is undefined and can lead
2164 * to interoperability problems.
2166 if ((ni->ni_flags & IEEE80211_NODE_HT) &&
2167 (((vap->iv_flags & IEEE80211_F_WPA) &&
2168 rsnparms.rsn_ucastcipher != IEEE80211_CIPHER_AES_CCM) ||
2169 (vap->iv_flags & (IEEE80211_F_WPA|IEEE80211_F_PRIVACY)) == IEEE80211_F_PRIVACY)) {
2171 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
2172 "disallow HT use because WEP or TKIP requested, "
2173 "capinfo 0x%x ucastcipher %d", capinfo,
2174 rsnparms.rsn_ucastcipher);
2175 ieee80211_ht_node_cleanup(ni);
2176 #ifdef IEEE80211_SUPPORT_SUPERG
2177 /* Always do ff node cleanup; for A-MSDU */
2178 ieee80211_ff_node_cleanup(ni);
2180 vap->iv_stats.is_ht_assoc_downgrade++;
2183 * If constrained to 11n-only stations reject legacy stations.
2185 if ((vap->iv_flags_ht & IEEE80211_FHT_PUREN) &&
2186 (ni->ni_flags & IEEE80211_NODE_HT) == 0) {
2187 htcapmismatch(ni, wh, reassoc, resp);
2188 vap->iv_stats.is_ht_assoc_nohtcap++;
2191 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
2193 ni->ni_intval = lintval;
2194 ni->ni_capinfo = capinfo;
2195 ni->ni_fhdwell = vap->iv_bss->ni_fhdwell;
2196 ni->ni_fhindex = vap->iv_bss->ni_fhindex;
2199 * XXX maybe better to just expand
2201 if (ieee80211_ies_init(&ni->ni_ies, sfrm, efrm - sfrm)) {
2202 #define setie(_ie, _off) ieee80211_ies_setie(ni->ni_ies, _ie, _off)
2204 setie(wpa_ie, wpa - sfrm);
2206 setie(rsn_ie, rsn - sfrm);
2208 setie(htcap_ie, htcap - sfrm);
2210 setie(wme_ie, wme - sfrm);
2212 * Mark node as capable of QoS.
2214 ni->ni_flags |= IEEE80211_NODE_QOS;
2216 ni->ni_flags &= ~IEEE80211_NODE_QOS;
2217 #ifdef IEEE80211_SUPPORT_SUPERG
2219 setie(ath_ie, ath - sfrm);
2221 * Parse ATH station parameters.
2223 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
2226 ni->ni_ath_flags = 0;
2229 ni->ni_flags &= ~IEEE80211_NODE_QOS;
2230 ni->ni_ath_flags = 0;
2232 ieee80211_node_join(ni, resp);
2233 ieee80211_deliver_l2uf(ni);
2237 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2238 case IEEE80211_FC0_SUBTYPE_DISASSOC: {
2241 if (vap->iv_state != IEEE80211_S_RUN ||
2242 /* NB: can happen when in promiscuous mode */
2243 !IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
2244 vap->iv_stats.is_rx_mgtdiscard++;
2248 * deauth/disassoc frame format
2251 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
2252 reason = le16toh(*(uint16_t *)frm);
2253 if (subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
2254 vap->iv_stats.is_rx_deauth++;
2255 IEEE80211_NODE_STAT(ni, rx_deauth);
2257 vap->iv_stats.is_rx_disassoc++;
2258 IEEE80211_NODE_STAT(ni, rx_disassoc);
2260 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2261 "recv %s (reason: %d (%s))",
2262 ieee80211_mgt_subtype_name(subtype),
2263 reason, ieee80211_reason_to_string(reason));
2264 if (ni != vap->iv_bss)
2265 ieee80211_node_leave(ni);
2269 case IEEE80211_FC0_SUBTYPE_ACTION:
2270 case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2271 if (ni == vap->iv_bss) {
2272 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2273 wh, NULL, "%s", "unknown node");
2274 vap->iv_stats.is_rx_mgtdiscard++;
2275 } else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2276 !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2277 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2278 wh, NULL, "%s", "not for us");
2279 vap->iv_stats.is_rx_mgtdiscard++;
2280 } else if (vap->iv_state != IEEE80211_S_RUN) {
2281 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2282 wh, NULL, "wrong state %s",
2283 ieee80211_state_name[vap->iv_state]);
2284 vap->iv_stats.is_rx_mgtdiscard++;
2286 if (ieee80211_parse_action(ni, m0) == 0)
2287 (void)ic->ic_recv_action(ni, wh, frm, efrm);
2291 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2292 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2293 case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
2294 case IEEE80211_FC0_SUBTYPE_ATIM:
2295 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2296 wh, NULL, "%s", "not handled");
2297 vap->iv_stats.is_rx_mgtdiscard++;
2301 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2302 wh, "mgt", "subtype 0x%x not handled", subtype);
2303 vap->iv_stats.is_rx_badsubtype++;
2309 hostap_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2312 case IEEE80211_FC0_SUBTYPE_PS_POLL:
2313 ni->ni_vap->iv_recv_pspoll(ni, m);
2315 case IEEE80211_FC0_SUBTYPE_BAR:
2316 ieee80211_recv_bar(ni, m);
2322 * Process a received ps-poll frame.
2325 ieee80211_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m0)
2327 struct ieee80211vap *vap = ni->ni_vap;
2328 struct ieee80211com *ic = vap->iv_ic;
2329 struct ieee80211_frame_min *wh;
2334 wh = mtod(m0, struct ieee80211_frame_min *);
2335 if (ni->ni_associd == 0) {
2336 IEEE80211_DISCARD(vap,
2337 IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2338 (struct ieee80211_frame *) wh, NULL,
2339 "%s", "unassociated station");
2340 vap->iv_stats.is_ps_unassoc++;
2341 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
2342 IEEE80211_REASON_NOT_ASSOCED);
2346 aid = le16toh(*(uint16_t *)wh->i_dur);
2347 if (aid != ni->ni_associd) {
2348 IEEE80211_DISCARD(vap,
2349 IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2350 (struct ieee80211_frame *) wh, NULL,
2351 "aid mismatch: sta aid 0x%x poll aid 0x%x",
2352 ni->ni_associd, aid);
2353 vap->iv_stats.is_ps_badaid++;
2355 * NB: We used to deauth the station but it turns out
2356 * the Blackberry Curve 8230 (and perhaps other devices)
2357 * sometimes send the wrong AID when WME is negotiated.
2358 * Being more lenient here seems ok as we already check
2359 * the station is associated and we only return frames
2360 * queued for the station (i.e. we don't use the AID).
2365 /* Okay, take the first queued packet and put it out... */
2366 m = ieee80211_node_psq_dequeue(ni, &qlen);
2368 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_POWER, wh->i_addr2,
2369 "%s", "recv ps-poll, but queue empty");
2370 ieee80211_send_nulldata(ieee80211_ref_node(ni));
2371 vap->iv_stats.is_ps_qempty++; /* XXX node stat */
2372 if (vap->iv_set_tim != NULL)
2373 vap->iv_set_tim(ni, 0); /* just in case */
2377 * If there are more packets, set the more packets bit
2378 * in the packet dispatched to the station; otherwise
2379 * turn off the TIM bit.
2382 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2383 "recv ps-poll, send packet, %u still queued", qlen);
2384 m->m_flags |= M_MORE_DATA;
2386 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2387 "%s", "recv ps-poll, send packet, queue empty");
2388 if (vap->iv_set_tim != NULL)
2389 vap->iv_set_tim(ni, 0);
2391 m->m_flags |= M_PWR_SAV; /* bypass PS handling */
2394 * Do the right thing; if it's an encap'ed frame then
2395 * call ieee80211_parent_xmitpkt() else
2396 * call ieee80211_vap_xmitpkt().
2398 if (m->m_flags & M_ENCAP) {
2399 (void) ieee80211_parent_xmitpkt(ic, m);
2401 (void) ieee80211_vap_xmitpkt(vap, m);