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)
110 struct ieee80211vap *vap = arg;
112 if (ni->ni_vap == vap && ni->ni_associd != 0) {
113 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
114 IEEE80211_REASON_ASSOC_LEAVE);
115 ieee80211_node_leave(ni);
120 sta_csa(void *arg, struct ieee80211_node *ni)
122 struct ieee80211vap *vap = arg;
124 if (ni->ni_vap == vap && ni->ni_associd != 0)
125 if (ni->ni_inact > vap->iv_inact_init) {
126 ni->ni_inact = vap->iv_inact_init;
127 IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
128 "%s: inact %u", __func__, ni->ni_inact);
133 sta_drop(void *arg, struct ieee80211_node *ni)
135 struct ieee80211vap *vap = arg;
137 if (ni->ni_vap == vap && ni->ni_associd != 0)
138 ieee80211_node_leave(ni);
142 * Does a channel change require associated stations to re-associate
143 * so protocol state is correct. This is used when doing CSA across
144 * bands or similar (e.g. HT -> legacy).
147 isbandchange(struct ieee80211com *ic)
149 return ((ic->ic_bsschan->ic_flags ^ ic->ic_csa_newchan->ic_flags) &
150 (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_HALF |
151 IEEE80211_CHAN_QUARTER | IEEE80211_CHAN_HT)) != 0;
155 * IEEE80211_M_HOSTAP vap state machine handler.
158 hostap_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
160 struct ieee80211com *ic = vap->iv_ic;
161 enum ieee80211_state ostate;
163 IEEE80211_LOCK_ASSERT(ic);
165 ostate = vap->iv_state;
166 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
167 __func__, ieee80211_state_name[ostate],
168 ieee80211_state_name[nstate], arg);
169 vap->iv_state = nstate; /* state transition */
170 if (ostate != IEEE80211_S_SCAN)
171 ieee80211_cancel_scan(vap); /* background scan */
173 case IEEE80211_S_INIT:
175 case IEEE80211_S_SCAN:
176 ieee80211_cancel_scan(vap);
178 case IEEE80211_S_CAC:
179 ieee80211_dfs_cac_stop(vap);
181 case IEEE80211_S_RUN:
182 ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
187 if (ostate != IEEE80211_S_INIT) {
188 /* NB: optimize INIT -> INIT case */
189 ieee80211_reset_bss(vap);
191 if (vap->iv_auth->ia_detach != NULL)
192 vap->iv_auth->ia_detach(vap);
194 case IEEE80211_S_SCAN:
196 case IEEE80211_S_CSA:
197 case IEEE80211_S_RUN:
198 ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, 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(&ic->ic_sta, sta_csa, vap);
294 * Update bss node channel to reflect where
295 * we landed after CSA.
297 ieee80211_node_set_chan(vap->iv_bss,
298 ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
299 ieee80211_htchanflags(vap->iv_bss->ni_chan)));
300 /* XXX bypass debug msgs */
302 case IEEE80211_S_SCAN:
303 case IEEE80211_S_RUN:
304 #ifdef IEEE80211_DEBUG
305 if (ieee80211_msg_debug(vap)) {
306 struct ieee80211_node *ni = vap->iv_bss;
308 "synchronized with %s ssid ",
309 ether_sprintf(ni->ni_bssid));
310 ieee80211_print_essid(ni->ni_essid,
313 printf(" channel %d start %uMb\n",
314 ieee80211_chan2ieee(ic, ic->ic_curchan),
315 IEEE80211_RATE2MBS(ni->ni_txrate));
323 * Start/stop the authenticator. We delay until here
324 * to allow configuration to happen out of order.
326 if (vap->iv_auth->ia_attach != NULL) {
327 /* XXX check failure */
328 vap->iv_auth->ia_attach(vap);
329 } else if (vap->iv_auth->ia_detach != NULL) {
330 vap->iv_auth->ia_detach(vap);
332 ieee80211_node_authorize(vap->iv_bss);
334 case IEEE80211_S_CSA:
335 if (ostate == IEEE80211_S_RUN && isbandchange(ic)) {
337 * On a ``band change'' silently drop associated
338 * stations as they must re-associate before they
339 * can pass traffic (as otherwise protocol state
340 * such as capabilities and the negotiated rate
341 * set may/will be wrong).
343 ieee80211_iterate_nodes(&ic->ic_sta, sta_drop, vap);
353 hostap_deliver_data(struct ieee80211vap *vap,
354 struct ieee80211_node *ni, struct mbuf *m)
356 struct ether_header *eh = mtod(m, struct ether_header *);
357 struct ifnet *ifp = vap->iv_ifp;
359 /* clear driver/net80211 flags before passing up */
360 m->m_flags &= ~(M_MCAST | M_BCAST);
363 KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
364 ("gack, opmode %d", vap->iv_opmode));
368 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
369 IEEE80211_NODE_STAT(ni, rx_data);
370 IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
371 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
372 m->m_flags |= M_MCAST; /* XXX M_BCAST? */
373 IEEE80211_NODE_STAT(ni, rx_mcast);
375 IEEE80211_NODE_STAT(ni, rx_ucast);
377 /* perform as a bridge within the AP */
378 if ((vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0) {
379 struct mbuf *mcopy = NULL;
381 if (m->m_flags & M_MCAST) {
382 mcopy = m_dup(m, M_NOWAIT);
384 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
386 mcopy->m_flags |= M_MCAST;
389 * Check if the destination is associated with the
390 * same vap and authorized to receive traffic.
391 * Beware of traffic destined for the vap itself;
392 * sending it will not work; just let it be delivered
395 struct ieee80211_node *sta = ieee80211_find_vap_node(
396 &vap->iv_ic->ic_sta, vap, eh->ether_dhost);
398 if (ieee80211_node_is_authorized(sta)) {
400 * Beware of sending to ourself; this
401 * needs to happen via the normal
404 if (sta != vap->iv_bss) {
409 vap->iv_stats.is_rx_unauth++;
410 IEEE80211_NODE_STAT(sta, rx_unauth);
412 ieee80211_free_node(sta);
416 (void) ieee80211_vap_xmitpkt(vap, mcopy);
420 * Mark frame as coming from vap's interface.
422 m->m_pkthdr.rcvif = ifp;
423 if (m->m_flags & M_MCAST) {
425 * Spam DWDS vap's w/ multicast traffic.
427 /* XXX only if dwds in use? */
428 ieee80211_dwds_mcast(vap, m);
430 if (ni->ni_vlan != 0) {
431 /* attach vlan tag */
432 m->m_pkthdr.ether_vtag = ni->ni_vlan;
433 m->m_flags |= M_VLANTAG;
435 ifp->if_input(ifp, m);
440 * Decide if a received management frame should be
441 * printed when debugging is enabled. This filters some
442 * of the less interesting frames that come frequently
446 doprint(struct ieee80211vap *vap, int subtype)
449 case IEEE80211_FC0_SUBTYPE_BEACON:
450 return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
451 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
458 * Process a received frame. The node associated with the sender
459 * should be supplied. If nothing was found in the node table then
460 * the caller is assumed to supply a reference to iv_bss instead.
461 * The RSSI and a timestamp are also supplied. The RSSI data is used
462 * during AP scanning to select a AP to associate with; it can have
463 * any units so long as values have consistent units and higher values
464 * mean ``better signal''. The receive timestamp is currently not used
465 * by the 802.11 layer.
468 hostap_input(struct ieee80211_node *ni, struct mbuf *m,
469 const struct ieee80211_rx_stats *rxs, int rssi, int nf)
471 struct ieee80211vap *vap = ni->ni_vap;
472 struct ieee80211com *ic = ni->ni_ic;
473 struct ifnet *ifp = vap->iv_ifp;
474 struct ieee80211_frame *wh;
475 struct ieee80211_key *key;
476 struct ether_header *eh;
477 int hdrspace, need_tap = 1; /* mbuf need to be tapped. */
478 uint8_t dir, type, subtype, qos;
481 if (m->m_flags & M_AMPDU_MPDU) {
483 * Fastpath for A-MPDU reorder q resubmission. Frames
484 * w/ M_AMPDU_MPDU marked have already passed through
485 * here but were received out of order and been held on
486 * the reorder queue. When resubmitted they are marked
487 * with the M_AMPDU_MPDU flag and we can bypass most of
488 * the normal processing.
490 wh = mtod(m, struct ieee80211_frame *);
491 type = IEEE80211_FC0_TYPE_DATA;
492 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
493 subtype = IEEE80211_FC0_SUBTYPE_QOS;
494 hdrspace = ieee80211_hdrspace(ic, wh); /* XXX optimize? */
498 KASSERT(ni != NULL, ("null node"));
499 ni->ni_inact = ni->ni_inact_reload;
501 type = -1; /* undefined */
503 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
504 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
505 ni->ni_macaddr, NULL,
506 "too short (1): len %u", m->m_pkthdr.len);
507 vap->iv_stats.is_rx_tooshort++;
511 * Bit of a cheat here, we use a pointer for a 3-address
512 * frame format but don't reference fields past outside
513 * ieee80211_frame_min w/o first validating the data is
516 wh = mtod(m, struct ieee80211_frame *);
518 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
519 IEEE80211_FC0_VERSION_0) {
520 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
521 ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
522 wh->i_fc[0], wh->i_fc[1]);
523 vap->iv_stats.is_rx_badversion++;
527 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
528 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
529 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
530 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
531 if (dir != IEEE80211_FC1_DIR_NODS)
533 else if (type == IEEE80211_FC0_TYPE_CTL)
536 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
537 IEEE80211_DISCARD_MAC(vap,
538 IEEE80211_MSG_ANY, ni->ni_macaddr,
539 NULL, "too short (2): len %u",
541 vap->iv_stats.is_rx_tooshort++;
547 * Validate the bssid.
549 if (!(type == IEEE80211_FC0_TYPE_MGT &&
550 subtype == IEEE80211_FC0_SUBTYPE_BEACON) &&
551 !IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
552 !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
553 /* not interested in */
554 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
555 bssid, NULL, "%s", "not to bss");
556 vap->iv_stats.is_rx_wrongbss++;
560 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
562 if (IEEE80211_HAS_SEQ(type, subtype)) {
563 uint8_t tid = ieee80211_gettid(wh);
564 if (IEEE80211_QOS_HAS_SEQ(wh) &&
565 TID_TO_WME_AC(tid) >= WME_AC_VI)
566 ic->ic_wme.wme_hipri_traffic++;
567 if (! ieee80211_check_rxseq(ni, wh, bssid))
573 case IEEE80211_FC0_TYPE_DATA:
574 hdrspace = ieee80211_hdrspace(ic, wh);
575 if (m->m_len < hdrspace &&
576 (m = m_pullup(m, hdrspace)) == NULL) {
577 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
578 ni->ni_macaddr, NULL,
579 "data too short: expecting %u", hdrspace);
580 vap->iv_stats.is_rx_tooshort++;
583 if (!(dir == IEEE80211_FC1_DIR_TODS ||
584 (dir == IEEE80211_FC1_DIR_DSTODS &&
585 (vap->iv_flags & IEEE80211_F_DWDS)))) {
586 if (dir != IEEE80211_FC1_DIR_DSTODS) {
587 IEEE80211_DISCARD(vap,
588 IEEE80211_MSG_INPUT, wh, "data",
589 "incorrect dir 0x%x", dir);
591 IEEE80211_DISCARD(vap,
592 IEEE80211_MSG_INPUT |
593 IEEE80211_MSG_WDS, wh,
595 "%s", "DWDS not enabled");
597 vap->iv_stats.is_rx_wrongdir++;
600 /* check if source STA is associated */
601 if (ni == vap->iv_bss) {
602 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
603 wh, "data", "%s", "unknown src");
604 ieee80211_send_error(ni, wh->i_addr2,
605 IEEE80211_FC0_SUBTYPE_DEAUTH,
606 IEEE80211_REASON_NOT_AUTHED);
607 vap->iv_stats.is_rx_notassoc++;
610 if (ni->ni_associd == 0) {
611 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
612 wh, "data", "%s", "unassoc src");
613 IEEE80211_SEND_MGMT(ni,
614 IEEE80211_FC0_SUBTYPE_DISASSOC,
615 IEEE80211_REASON_NOT_ASSOCED);
616 vap->iv_stats.is_rx_notassoc++;
621 * Check for power save state change.
622 * XXX out-of-order A-MPDU frames?
624 if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
625 (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
627 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
629 * For 4-address packets handle WDS discovery
630 * notifications. Once a WDS link is setup frames
631 * are just delivered to the WDS vap (see below).
633 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap == NULL) {
634 if (!ieee80211_node_is_authorized(ni)) {
635 IEEE80211_DISCARD(vap,
636 IEEE80211_MSG_INPUT |
637 IEEE80211_MSG_WDS, wh,
639 "%s", "unauthorized port");
640 vap->iv_stats.is_rx_unauth++;
641 IEEE80211_NODE_STAT(ni, rx_unauth);
644 ieee80211_dwds_discover(ni, m);
649 * Handle A-MPDU re-ordering. If the frame is to be
650 * processed directly then ieee80211_ampdu_reorder
651 * will return 0; otherwise it has consumed the mbuf
652 * and we should do nothing more with it.
654 if ((m->m_flags & M_AMPDU) &&
655 ieee80211_ampdu_reorder(ni, m) != 0) {
662 * Handle privacy requirements. Note that we
663 * must not be preempted from here until after
664 * we (potentially) call ieee80211_crypto_demic;
665 * otherwise we may violate assumptions in the
666 * crypto cipher modules used to do delayed update
667 * of replay sequence numbers.
669 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
670 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
672 * Discard encrypted frames when privacy is off.
674 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
675 wh, "WEP", "%s", "PRIVACY off");
676 vap->iv_stats.is_rx_noprivacy++;
677 IEEE80211_NODE_STAT(ni, rx_noprivacy);
680 key = ieee80211_crypto_decap(ni, m, hdrspace);
682 /* NB: stats+msgs handled in crypto_decap */
683 IEEE80211_NODE_STAT(ni, rx_wepfail);
686 wh = mtod(m, struct ieee80211_frame *);
687 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
689 /* XXX M_WEP and IEEE80211_F_PRIVACY */
694 * Save QoS bits for use below--before we strip the header.
696 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
697 qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
698 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
699 ((struct ieee80211_qosframe *)wh)->i_qos[0];
704 * Next up, any fragmentation.
706 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
707 m = ieee80211_defrag(ni, m, hdrspace);
709 /* Fragment dropped or frame not complete yet */
713 wh = NULL; /* no longer valid, catch any uses */
716 * Next strip any MSDU crypto bits.
718 if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
719 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
720 ni->ni_macaddr, "data", "%s", "demic error");
721 vap->iv_stats.is_rx_demicfail++;
722 IEEE80211_NODE_STAT(ni, rx_demicfail);
725 /* copy to listener after decrypt */
726 if (ieee80211_radiotap_active_vap(vap))
727 ieee80211_radiotap_rx(vap, m);
730 * Finally, strip the 802.11 header.
732 m = ieee80211_decap(vap, m, hdrspace);
734 /* XXX mask bit to check for both */
735 /* don't count Null data frames as errors */
736 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
737 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
739 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
740 ni->ni_macaddr, "data", "%s", "decap error");
741 vap->iv_stats.is_rx_decap++;
742 IEEE80211_NODE_STAT(ni, rx_decap);
745 eh = mtod(m, struct ether_header *);
746 if (!ieee80211_node_is_authorized(ni)) {
748 * Deny any non-PAE frames received prior to
749 * authorization. For open/shared-key
750 * authentication the port is mark authorized
751 * after authentication completes. For 802.1x
752 * the port is not marked authorized by the
753 * authenticator until the handshake has completed.
755 if (eh->ether_type != htons(ETHERTYPE_PAE)) {
756 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
757 eh->ether_shost, "data",
758 "unauthorized port: ether type 0x%x len %u",
759 eh->ether_type, m->m_pkthdr.len);
760 vap->iv_stats.is_rx_unauth++;
761 IEEE80211_NODE_STAT(ni, rx_unauth);
766 * When denying unencrypted frames, discard
767 * any non-PAE frames received without encryption.
769 if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
770 (key == NULL && (m->m_flags & M_WEP) == 0) &&
771 eh->ether_type != htons(ETHERTYPE_PAE)) {
773 * Drop unencrypted frames.
775 vap->iv_stats.is_rx_unencrypted++;
776 IEEE80211_NODE_STAT(ni, rx_unencrypted);
780 /* XXX require HT? */
781 if (qos & IEEE80211_QOS_AMSDU) {
782 m = ieee80211_decap_amsdu(ni, m);
784 return IEEE80211_FC0_TYPE_DATA;
786 #ifdef IEEE80211_SUPPORT_SUPERG
787 m = ieee80211_decap_fastframe(vap, ni, m);
789 return IEEE80211_FC0_TYPE_DATA;
792 if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
793 ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
795 hostap_deliver_data(vap, ni, m);
796 return IEEE80211_FC0_TYPE_DATA;
798 case IEEE80211_FC0_TYPE_MGT:
799 vap->iv_stats.is_rx_mgmt++;
800 IEEE80211_NODE_STAT(ni, rx_mgmt);
801 if (dir != IEEE80211_FC1_DIR_NODS) {
802 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
803 wh, "mgt", "incorrect dir 0x%x", dir);
804 vap->iv_stats.is_rx_wrongdir++;
807 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
808 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
809 ni->ni_macaddr, "mgt", "too short: len %u",
811 vap->iv_stats.is_rx_tooshort++;
814 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
815 /* ensure return frames are unicast */
816 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
817 wh, NULL, "source is multicast: %s",
818 ether_sprintf(wh->i_addr2));
819 vap->iv_stats.is_rx_mgtdiscard++; /* XXX stat */
822 #ifdef IEEE80211_DEBUG
823 if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
824 ieee80211_msg_dumppkts(vap)) {
825 if_printf(ifp, "received %s from %s rssi %d\n",
826 ieee80211_mgt_subtype_name(subtype),
827 ether_sprintf(wh->i_addr2), rssi);
830 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
831 if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
833 * Only shared key auth frames with a challenge
834 * should be encrypted, discard all others.
836 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
838 "%s", "WEP set but not permitted");
839 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
842 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
844 * Discard encrypted frames when privacy is off.
846 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
847 wh, NULL, "%s", "WEP set but PRIVACY off");
848 vap->iv_stats.is_rx_noprivacy++;
851 hdrspace = ieee80211_hdrspace(ic, wh);
852 key = ieee80211_crypto_decap(ni, m, hdrspace);
854 /* NB: stats+msgs handled in crypto_decap */
857 wh = mtod(m, struct ieee80211_frame *);
858 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
861 * Pass the packet to radiotap before calling iv_recv_mgmt().
862 * Otherwise iv_recv_mgmt() might pass another packet to
863 * radiotap, resulting in out of order packet captures.
865 if (ieee80211_radiotap_active_vap(vap))
866 ieee80211_radiotap_rx(vap, m);
868 vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
871 case IEEE80211_FC0_TYPE_CTL:
872 vap->iv_stats.is_rx_ctl++;
873 IEEE80211_NODE_STAT(ni, rx_ctrl);
874 vap->iv_recv_ctl(ni, m, subtype);
877 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
878 wh, "bad", "frame type 0x%x", type);
879 /* should not come here */
883 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
886 if (need_tap && ieee80211_radiotap_active_vap(vap))
887 ieee80211_radiotap_rx(vap, m);
894 hostap_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
895 int rssi, int nf, uint16_t seq, uint16_t status)
897 struct ieee80211vap *vap = ni->ni_vap;
899 KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
901 if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
902 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
903 ni->ni_macaddr, "open auth",
904 "bad sta auth mode %u", ni->ni_authmode);
905 vap->iv_stats.is_rx_bad_auth++; /* XXX */
907 * Clear any challenge text that may be there if
908 * a previous shared key auth failed and then an
909 * open auth is attempted.
911 if (ni->ni_challenge != NULL) {
912 IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
913 ni->ni_challenge = NULL;
915 /* XXX hack to workaround calling convention */
916 ieee80211_send_error(ni, wh->i_addr2,
917 IEEE80211_FC0_SUBTYPE_AUTH,
918 (seq + 1) | (IEEE80211_STATUS_ALG<<16));
921 if (seq != IEEE80211_AUTH_OPEN_REQUEST) {
922 vap->iv_stats.is_rx_bad_auth++;
925 /* always accept open authentication requests */
926 if (ni == vap->iv_bss) {
927 ni = ieee80211_dup_bss(vap, wh->i_addr2);
930 } else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
931 (void) ieee80211_ref_node(ni);
933 * Mark the node as referenced to reflect that it's
934 * reference count has been bumped to insure it remains
935 * after the transaction completes.
937 ni->ni_flags |= IEEE80211_NODE_AREF;
939 * Mark the node as requiring a valid association id
940 * before outbound traffic is permitted.
942 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
944 if (vap->iv_acl != NULL &&
945 vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
947 * When the ACL policy is set to RADIUS we defer the
948 * authorization to a user agent. Dispatch an event,
949 * a subsequent MLME call will decide the fate of the
950 * station. If the user agent is not present then the
951 * node will be reclaimed due to inactivity.
953 IEEE80211_NOTE_MAC(vap,
954 IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL, ni->ni_macaddr,
955 "%s", "station authentication defered (radius acl)");
956 ieee80211_notify_node_auth(ni);
958 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
959 IEEE80211_NOTE_MAC(vap,
960 IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni->ni_macaddr,
961 "%s", "station authenticated (open)");
963 * When 802.1x is not in use mark the port
964 * authorized at this point so traffic can flow.
966 if (ni->ni_authmode != IEEE80211_AUTH_8021X)
967 ieee80211_node_authorize(ni);
972 hostap_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
973 uint8_t *frm, uint8_t *efrm, int rssi, int nf,
974 uint16_t seq, uint16_t status)
976 struct ieee80211vap *vap = ni->ni_vap;
978 int allocbs, estatus;
980 KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
983 * NB: this can happen as we allow pre-shared key
984 * authentication to be enabled w/o wep being turned
985 * on so that configuration of these can be done
986 * in any order. It may be better to enforce the
987 * ordering in which case this check would just be
988 * for sanity/consistency.
990 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
991 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
992 ni->ni_macaddr, "shared key auth",
993 "%s", " PRIVACY is disabled");
994 estatus = IEEE80211_STATUS_ALG;
998 * Pre-shared key authentication is evil; accept
999 * it only if explicitly configured (it is supported
1000 * mainly for compatibility with clients like Mac OS X).
1002 if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1003 ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1004 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1005 ni->ni_macaddr, "shared key auth",
1006 "bad sta auth mode %u", ni->ni_authmode);
1007 vap->iv_stats.is_rx_bad_auth++; /* XXX maybe a unique error? */
1008 estatus = IEEE80211_STATUS_ALG;
1013 if (frm + 1 < efrm) {
1014 if ((frm[1] + 2) > (efrm - frm)) {
1015 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1016 ni->ni_macaddr, "shared key auth",
1017 "ie %d/%d too long",
1018 frm[0], (frm[1] + 2) - (efrm - frm));
1019 vap->iv_stats.is_rx_bad_auth++;
1020 estatus = IEEE80211_STATUS_CHALLENGE;
1023 if (*frm == IEEE80211_ELEMID_CHALLENGE)
1028 case IEEE80211_AUTH_SHARED_CHALLENGE:
1029 case IEEE80211_AUTH_SHARED_RESPONSE:
1030 if (challenge == NULL) {
1031 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1032 ni->ni_macaddr, "shared key auth",
1033 "%s", "no challenge");
1034 vap->iv_stats.is_rx_bad_auth++;
1035 estatus = IEEE80211_STATUS_CHALLENGE;
1038 if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1039 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1040 ni->ni_macaddr, "shared key auth",
1041 "bad challenge len %d", challenge[1]);
1042 vap->iv_stats.is_rx_bad_auth++;
1043 estatus = IEEE80211_STATUS_CHALLENGE;
1050 case IEEE80211_AUTH_SHARED_REQUEST:
1051 if (ni == vap->iv_bss) {
1052 ni = ieee80211_dup_bss(vap, wh->i_addr2);
1054 /* NB: no way to return an error */
1059 if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1060 (void) ieee80211_ref_node(ni);
1064 * Mark the node as referenced to reflect that it's
1065 * reference count has been bumped to insure it remains
1066 * after the transaction completes.
1068 ni->ni_flags |= IEEE80211_NODE_AREF;
1070 * Mark the node as requiring a valid association id
1071 * before outbound traffic is permitted.
1073 ni->ni_flags |= IEEE80211_NODE_ASSOCID;
1074 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1076 if (!ieee80211_alloc_challenge(ni)) {
1077 /* NB: don't return error so they rexmit */
1080 get_random_bytes(ni->ni_challenge,
1081 IEEE80211_CHALLENGE_LEN);
1082 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1083 ni, "shared key %sauth request", allocbs ? "" : "re");
1085 * When the ACL policy is set to RADIUS we defer the
1086 * authorization to a user agent. Dispatch an event,
1087 * a subsequent MLME call will decide the fate of the
1088 * station. If the user agent is not present then the
1089 * node will be reclaimed due to inactivity.
1091 if (vap->iv_acl != NULL &&
1092 vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
1093 IEEE80211_NOTE_MAC(vap,
1094 IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL,
1096 "%s", "station authentication defered (radius acl)");
1097 ieee80211_notify_node_auth(ni);
1101 case IEEE80211_AUTH_SHARED_RESPONSE:
1102 if (ni == vap->iv_bss) {
1103 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1104 ni->ni_macaddr, "shared key response",
1105 "%s", "unknown station");
1106 /* NB: don't send a response */
1109 if (ni->ni_challenge == NULL) {
1110 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1111 ni->ni_macaddr, "shared key response",
1112 "%s", "no challenge recorded");
1113 vap->iv_stats.is_rx_bad_auth++;
1114 estatus = IEEE80211_STATUS_CHALLENGE;
1117 if (memcmp(ni->ni_challenge, &challenge[2],
1118 challenge[1]) != 0) {
1119 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1120 ni->ni_macaddr, "shared key response",
1121 "%s", "challenge mismatch");
1122 vap->iv_stats.is_rx_auth_fail++;
1123 estatus = IEEE80211_STATUS_CHALLENGE;
1126 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1127 ni, "%s", "station authenticated (shared key)");
1128 ieee80211_node_authorize(ni);
1131 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1132 ni->ni_macaddr, "shared key auth",
1134 vap->iv_stats.is_rx_bad_auth++;
1135 estatus = IEEE80211_STATUS_SEQUENCE;
1138 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1142 * Send an error response; but only when operating as an AP.
1144 /* XXX hack to workaround calling convention */
1145 ieee80211_send_error(ni, wh->i_addr2,
1146 IEEE80211_FC0_SUBTYPE_AUTH,
1147 (seq + 1) | (estatus<<16));
1151 * Convert a WPA cipher selector OUI to an internal
1152 * cipher algorithm. Where appropriate we also
1153 * record any key length.
1156 wpa_cipher(const uint8_t *sel, uint8_t *keylen)
1158 #define WPA_SEL(x) (((x)<<24)|WPA_OUI)
1159 uint32_t w = le32dec(sel);
1162 case WPA_SEL(WPA_CSE_NULL):
1163 return IEEE80211_CIPHER_NONE;
1164 case WPA_SEL(WPA_CSE_WEP40):
1166 *keylen = 40 / NBBY;
1167 return IEEE80211_CIPHER_WEP;
1168 case WPA_SEL(WPA_CSE_WEP104):
1170 *keylen = 104 / NBBY;
1171 return IEEE80211_CIPHER_WEP;
1172 case WPA_SEL(WPA_CSE_TKIP):
1173 return IEEE80211_CIPHER_TKIP;
1174 case WPA_SEL(WPA_CSE_CCMP):
1175 return IEEE80211_CIPHER_AES_CCM;
1177 return 32; /* NB: so 1<< is discarded */
1182 * Convert a WPA key management/authentication algorithm
1183 * to an internal code.
1186 wpa_keymgmt(const uint8_t *sel)
1188 #define WPA_SEL(x) (((x)<<24)|WPA_OUI)
1189 uint32_t w = le32dec(sel);
1192 case WPA_SEL(WPA_ASE_8021X_UNSPEC):
1193 return WPA_ASE_8021X_UNSPEC;
1194 case WPA_SEL(WPA_ASE_8021X_PSK):
1195 return WPA_ASE_8021X_PSK;
1196 case WPA_SEL(WPA_ASE_NONE):
1197 return WPA_ASE_NONE;
1199 return 0; /* NB: so is discarded */
1204 * Parse a WPA information element to collect parameters.
1205 * Note that we do not validate security parameters; that
1206 * is handled by the authenticator; the parsing done here
1207 * is just for internal use in making operational decisions.
1210 ieee80211_parse_wpa(struct ieee80211vap *vap, const uint8_t *frm,
1211 struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1213 uint8_t len = frm[1];
1218 * Check the length once for fixed parts: OUI, type,
1219 * version, mcast cipher, and 2 selector counts.
1220 * Other, variable-length data, must be checked separately.
1222 if ((vap->iv_flags & IEEE80211_F_WPA1) == 0) {
1223 IEEE80211_DISCARD_IE(vap,
1224 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1225 wh, "WPA", "not WPA, flags 0x%x", vap->iv_flags);
1226 return IEEE80211_REASON_IE_INVALID;
1229 IEEE80211_DISCARD_IE(vap,
1230 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1231 wh, "WPA", "too short, len %u", len);
1232 return IEEE80211_REASON_IE_INVALID;
1234 frm += 6, len -= 4; /* NB: len is payload only */
1235 /* NB: iswpaoui already validated the OUI and type */
1237 if (w != WPA_VERSION) {
1238 IEEE80211_DISCARD_IE(vap,
1239 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1240 wh, "WPA", "bad version %u", w);
1241 return IEEE80211_REASON_IE_INVALID;
1245 memset(rsn, 0, sizeof(*rsn));
1247 /* multicast/group cipher */
1248 rsn->rsn_mcastcipher = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
1251 /* unicast ciphers */
1255 IEEE80211_DISCARD_IE(vap,
1256 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1257 wh, "WPA", "ucast cipher data too short; len %u, n %u",
1259 return IEEE80211_REASON_IE_INVALID;
1262 for (; n > 0; n--) {
1263 w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
1266 if (w & (1<<IEEE80211_CIPHER_TKIP))
1267 rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1269 rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1271 /* key management algorithms */
1275 IEEE80211_DISCARD_IE(vap,
1276 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1277 wh, "WPA", "key mgmt alg data too short; len %u, n %u",
1279 return IEEE80211_REASON_IE_INVALID;
1282 for (; n > 0; n--) {
1283 w |= wpa_keymgmt(frm);
1286 if (w & WPA_ASE_8021X_UNSPEC)
1287 rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
1289 rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1291 if (len > 2) /* optional capabilities */
1292 rsn->rsn_caps = le16dec(frm);
1298 * Convert an RSN cipher selector OUI to an internal
1299 * cipher algorithm. Where appropriate we also
1300 * record any key length.
1303 rsn_cipher(const uint8_t *sel, uint8_t *keylen)
1305 #define RSN_SEL(x) (((x)<<24)|RSN_OUI)
1306 uint32_t w = le32dec(sel);
1309 case RSN_SEL(RSN_CSE_NULL):
1310 return IEEE80211_CIPHER_NONE;
1311 case RSN_SEL(RSN_CSE_WEP40):
1313 *keylen = 40 / NBBY;
1314 return IEEE80211_CIPHER_WEP;
1315 case RSN_SEL(RSN_CSE_WEP104):
1317 *keylen = 104 / NBBY;
1318 return IEEE80211_CIPHER_WEP;
1319 case RSN_SEL(RSN_CSE_TKIP):
1320 return IEEE80211_CIPHER_TKIP;
1321 case RSN_SEL(RSN_CSE_CCMP):
1322 return IEEE80211_CIPHER_AES_CCM;
1323 case RSN_SEL(RSN_CSE_WRAP):
1324 return IEEE80211_CIPHER_AES_OCB;
1326 return 32; /* NB: so 1<< is discarded */
1331 * Convert an RSN key management/authentication algorithm
1332 * to an internal code.
1335 rsn_keymgmt(const uint8_t *sel)
1337 #define RSN_SEL(x) (((x)<<24)|RSN_OUI)
1338 uint32_t w = le32dec(sel);
1341 case RSN_SEL(RSN_ASE_8021X_UNSPEC):
1342 return RSN_ASE_8021X_UNSPEC;
1343 case RSN_SEL(RSN_ASE_8021X_PSK):
1344 return RSN_ASE_8021X_PSK;
1345 case RSN_SEL(RSN_ASE_NONE):
1346 return RSN_ASE_NONE;
1348 return 0; /* NB: so is discarded */
1353 * Parse a WPA/RSN information element to collect parameters
1354 * and validate the parameters against what has been
1355 * configured for the system.
1358 ieee80211_parse_rsn(struct ieee80211vap *vap, const uint8_t *frm,
1359 struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1361 uint8_t len = frm[1];
1366 * Check the length once for fixed parts:
1367 * version, mcast cipher, and 2 selector counts.
1368 * Other, variable-length data, must be checked separately.
1370 if ((vap->iv_flags & IEEE80211_F_WPA2) == 0) {
1371 IEEE80211_DISCARD_IE(vap,
1372 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1373 wh, "WPA", "not RSN, flags 0x%x", vap->iv_flags);
1374 return IEEE80211_REASON_IE_INVALID;
1377 IEEE80211_DISCARD_IE(vap,
1378 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1379 wh, "RSN", "too short, len %u", len);
1380 return IEEE80211_REASON_IE_INVALID;
1384 if (w != RSN_VERSION) {
1385 IEEE80211_DISCARD_IE(vap,
1386 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1387 wh, "RSN", "bad version %u", w);
1388 return IEEE80211_REASON_IE_INVALID;
1392 memset(rsn, 0, sizeof(*rsn));
1394 /* multicast/group cipher */
1395 rsn->rsn_mcastcipher = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
1398 /* unicast ciphers */
1402 IEEE80211_DISCARD_IE(vap,
1403 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1404 wh, "RSN", "ucast cipher data too short; len %u, n %u",
1406 return IEEE80211_REASON_IE_INVALID;
1409 for (; n > 0; n--) {
1410 w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
1413 if (w & (1<<IEEE80211_CIPHER_TKIP))
1414 rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1416 rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1418 /* key management algorithms */
1422 IEEE80211_DISCARD_IE(vap,
1423 IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1424 wh, "RSN", "key mgmt alg data too short; len %u, n %u",
1426 return IEEE80211_REASON_IE_INVALID;
1429 for (; n > 0; n--) {
1430 w |= rsn_keymgmt(frm);
1433 if (w & RSN_ASE_8021X_UNSPEC)
1434 rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
1436 rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
1438 /* optional RSN capabilities */
1440 rsn->rsn_caps = le16dec(frm);
1447 * WPA/802.11i association request processing.
1450 wpa_assocreq(struct ieee80211_node *ni, struct ieee80211_rsnparms *rsnparms,
1451 const struct ieee80211_frame *wh, const uint8_t *wpa,
1452 const uint8_t *rsn, uint16_t capinfo)
1454 struct ieee80211vap *vap = ni->ni_vap;
1458 ni->ni_flags &= ~(IEEE80211_NODE_WPS|IEEE80211_NODE_TSN);
1459 if (wpa == NULL && rsn == NULL) {
1460 if (vap->iv_flags_ext & IEEE80211_FEXT_WPS) {
1462 * W-Fi Protected Setup (WPS) permits
1463 * clients to associate and pass EAPOL frames
1464 * to establish initial credentials.
1466 ni->ni_flags |= IEEE80211_NODE_WPS;
1469 if ((vap->iv_flags_ext & IEEE80211_FEXT_TSN) &&
1470 (capinfo & IEEE80211_CAPINFO_PRIVACY)) {
1472 * Transitional Security Network. Permits clients
1473 * to associate and use WEP while WPA is configured.
1475 ni->ni_flags |= IEEE80211_NODE_TSN;
1478 IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1479 wh, NULL, "%s", "no WPA/RSN IE in association request");
1480 vap->iv_stats.is_rx_assoc_badwpaie++;
1481 reason = IEEE80211_REASON_IE_INVALID;
1484 /* assert right association security credentials */
1485 badwparsn = 0; /* NB: to silence compiler */
1486 switch (vap->iv_flags & IEEE80211_F_WPA) {
1487 case IEEE80211_F_WPA1:
1488 badwparsn = (wpa == NULL);
1490 case IEEE80211_F_WPA2:
1491 badwparsn = (rsn == NULL);
1493 case IEEE80211_F_WPA1|IEEE80211_F_WPA2:
1494 badwparsn = (wpa == NULL && rsn == NULL);
1498 IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1500 "%s", "missing WPA/RSN IE in association request");
1501 vap->iv_stats.is_rx_assoc_badwpaie++;
1502 reason = IEEE80211_REASON_IE_INVALID;
1506 * Parse WPA/RSN information element.
1509 reason = ieee80211_parse_wpa(vap, wpa, rsnparms, wh);
1511 reason = ieee80211_parse_rsn(vap, rsn, rsnparms, wh);
1513 /* XXX distinguish WPA/RSN? */
1514 vap->iv_stats.is_rx_assoc_badwpaie++;
1517 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA, ni,
1518 "%s ie: mc %u/%u uc %u/%u key %u caps 0x%x",
1519 wpa != NULL ? "WPA" : "RSN",
1520 rsnparms->rsn_mcastcipher, rsnparms->rsn_mcastkeylen,
1521 rsnparms->rsn_ucastcipher, rsnparms->rsn_ucastkeylen,
1522 rsnparms->rsn_keymgmt, rsnparms->rsn_caps);
1526 ieee80211_node_deauth(ni, reason);
1530 /* XXX find a better place for definition */
1531 struct l2_update_frame {
1532 struct ether_header eh;
1540 * Deliver a TGf L2UF frame on behalf of a station.
1541 * This primes any bridge when the station is roaming
1542 * between ap's on the same wired network.
1545 ieee80211_deliver_l2uf(struct ieee80211_node *ni)
1547 struct ieee80211vap *vap = ni->ni_vap;
1548 struct ifnet *ifp = vap->iv_ifp;
1550 struct l2_update_frame *l2uf;
1551 struct ether_header *eh;
1553 m = m_gethdr(M_NOWAIT, MT_DATA);
1555 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1556 "%s", "no mbuf for l2uf frame");
1557 vap->iv_stats.is_rx_nobuf++; /* XXX not right */
1560 l2uf = mtod(m, struct l2_update_frame *);
1562 /* dst: Broadcast address */
1563 IEEE80211_ADDR_COPY(eh->ether_dhost, ifp->if_broadcastaddr);
1564 /* src: associated STA */
1565 IEEE80211_ADDR_COPY(eh->ether_shost, ni->ni_macaddr);
1566 eh->ether_type = htons(sizeof(*l2uf) - sizeof(*eh));
1570 l2uf->control = 0xf5;
1571 l2uf->xid[0] = 0x81;
1572 l2uf->xid[1] = 0x80;
1573 l2uf->xid[2] = 0x00;
1575 m->m_pkthdr.len = m->m_len = sizeof(*l2uf);
1576 hostap_deliver_data(vap, ni, m);
1580 ratesetmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1581 int reassoc, int resp, const char *tag, int rate)
1583 IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1584 "deny %s request, %s rate set mismatch, rate/MCS %d",
1585 reassoc ? "reassoc" : "assoc", tag, rate & IEEE80211_RATE_VAL);
1586 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_BASIC_RATE);
1587 ieee80211_node_leave(ni);
1591 capinfomismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1592 int reassoc, int resp, const char *tag, int capinfo)
1594 struct ieee80211vap *vap = ni->ni_vap;
1596 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1597 "deny %s request, %s mismatch 0x%x",
1598 reassoc ? "reassoc" : "assoc", tag, capinfo);
1599 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_CAPINFO);
1600 ieee80211_node_leave(ni);
1601 vap->iv_stats.is_rx_assoc_capmismatch++;
1605 htcapmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1606 int reassoc, int resp)
1608 IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1609 "deny %s request, %s missing HT ie", reassoc ? "reassoc" : "assoc");
1610 /* XXX no better code */
1611 IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_MISSING_HT_CAPS);
1612 ieee80211_node_leave(ni);
1616 authalgreject(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1617 int algo, int seq, int status)
1619 struct ieee80211vap *vap = ni->ni_vap;
1621 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1622 wh, NULL, "unsupported alg %d", algo);
1623 vap->iv_stats.is_rx_auth_unsupported++;
1624 ieee80211_send_error(ni, wh->i_addr2, IEEE80211_FC0_SUBTYPE_AUTH,
1625 seq | (status << 16));
1629 ishtmixed(const uint8_t *ie)
1631 const struct ieee80211_ie_htinfo *ht =
1632 (const struct ieee80211_ie_htinfo *) ie;
1633 return (ht->hi_byte2 & IEEE80211_HTINFO_OPMODE) ==
1634 IEEE80211_HTINFO_OPMODE_MIXED;
1638 is11bclient(const uint8_t *rates, const uint8_t *xrates)
1640 static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
1643 /* NB: the 11b clients we care about will not have xrates */
1644 if (xrates != NULL || rates == NULL)
1646 for (i = 0; i < rates[1]; i++) {
1647 int r = rates[2+i] & IEEE80211_RATE_VAL;
1648 if (r > 2*11 || ((1<<r) & brates) == 0)
1655 hostap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1656 int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
1658 struct ieee80211vap *vap = ni->ni_vap;
1659 struct ieee80211com *ic = ni->ni_ic;
1660 struct ieee80211_frame *wh;
1661 uint8_t *frm, *efrm, *sfrm;
1662 uint8_t *ssid, *rates, *xrates, *wpa, *rsn, *wme, *ath, *htcap;
1666 wh = mtod(m0, struct ieee80211_frame *);
1667 frm = (uint8_t *)&wh[1];
1668 efrm = mtod(m0, uint8_t *) + m0->m_len;
1670 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1672 * We process beacon/probe response frames when scanning;
1673 * otherwise we check beacon frames for overlapping non-ERP
1674 * BSS in 11g and/or overlapping legacy BSS when in HT.
1676 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1677 vap->iv_stats.is_rx_mgtdiscard++;
1681 case IEEE80211_FC0_SUBTYPE_BEACON: {
1682 struct ieee80211_scanparams scan;
1684 /* NB: accept off-channel frames */
1685 /* XXX TODO: use rxstatus to determine off-channel details */
1686 if (ieee80211_parse_beacon(ni, m0, ic->ic_curchan, &scan) &~ IEEE80211_BPARSE_OFFCHAN)
1689 * Count frame now that we know it's to be processed.
1691 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1692 vap->iv_stats.is_rx_beacon++; /* XXX remove */
1693 IEEE80211_NODE_STAT(ni, rx_beacons);
1695 IEEE80211_NODE_STAT(ni, rx_proberesp);
1697 * If scanning, just pass information to the scan module.
1699 if (ic->ic_flags & IEEE80211_F_SCAN) {
1700 if (scan.status == 0 && /* NB: on channel */
1701 (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN)) {
1703 * Actively scanning a channel marked passive;
1704 * send a probe request now that we know there
1705 * is 802.11 traffic present.
1707 * XXX check if the beacon we recv'd gives
1708 * us what we need and suppress the probe req
1710 ieee80211_probe_curchan(vap, 1);
1711 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1713 ieee80211_add_scan(vap, ic->ic_curchan, &scan, wh,
1718 * Check beacon for overlapping bss w/ non ERP stations.
1719 * If we detect one and protection is configured but not
1720 * enabled, enable it and start a timer that'll bring us
1721 * out if we stop seeing the bss.
1723 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1724 scan.status == 0 && /* NB: on-channel */
1725 ((scan.erp & 0x100) == 0 || /* NB: no ERP, 11b sta*/
1726 (scan.erp & IEEE80211_ERP_NON_ERP_PRESENT))) {
1727 ic->ic_lastnonerp = ticks;
1728 ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
1729 if (ic->ic_protmode != IEEE80211_PROT_NONE &&
1730 (ic->ic_flags & IEEE80211_F_USEPROT) == 0) {
1731 IEEE80211_NOTE_FRAME(vap,
1732 IEEE80211_MSG_ASSOC, wh,
1733 "non-ERP present on channel %d "
1734 "(saw erp 0x%x from channel %d), "
1735 "enable use of protection",
1736 ic->ic_curchan->ic_ieee,
1737 scan.erp, scan.chan);
1738 ic->ic_flags |= IEEE80211_F_USEPROT;
1739 ieee80211_notify_erp(ic);
1743 * Check beacon for non-HT station on HT channel
1744 * and update HT BSS occupancy as appropriate.
1746 if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) {
1747 if (scan.status & IEEE80211_BPARSE_OFFCHAN) {
1749 * Off control channel; only check frames
1750 * that come in the extension channel when
1751 * operating w/ HT40.
1753 if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
1755 if (scan.chan != ic->ic_curchan->ic_extieee)
1758 if (scan.htinfo == NULL) {
1759 ieee80211_htprot_update(ic,
1760 IEEE80211_HTINFO_OPMODE_PROTOPT |
1761 IEEE80211_HTINFO_NONHT_PRESENT);
1762 } else if (ishtmixed(scan.htinfo)) {
1763 /* XXX? take NONHT_PRESENT from beacon? */
1764 ieee80211_htprot_update(ic,
1765 IEEE80211_HTINFO_OPMODE_MIXED |
1766 IEEE80211_HTINFO_NONHT_PRESENT);
1772 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1773 if (vap->iv_state != IEEE80211_S_RUN) {
1774 vap->iv_stats.is_rx_mgtdiscard++;
1778 * Consult the ACL policy module if setup.
1780 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1781 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1782 wh, NULL, "%s", "disallowed by ACL");
1783 vap->iv_stats.is_rx_acl++;
1787 * prreq frame format
1789 * [tlv] supported rates
1790 * [tlv] extended supported rates
1792 ssid = rates = xrates = NULL;
1793 while (efrm - frm > 1) {
1794 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1796 case IEEE80211_ELEMID_SSID:
1799 case IEEE80211_ELEMID_RATES:
1802 case IEEE80211_ELEMID_XRATES:
1808 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1810 IEEE80211_VERIFY_ELEMENT(xrates,
1811 IEEE80211_RATE_MAXSIZE - rates[1], return);
1812 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1813 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
1814 if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
1815 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1817 "%s", "no ssid with ssid suppression enabled");
1818 vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
1822 /* XXX find a better class or define it's own */
1823 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1824 "%s", "recv probe req");
1826 * Some legacy 11b clients cannot hack a complete
1827 * probe response frame. When the request includes
1828 * only a bare-bones rate set, communicate this to
1829 * the transmit side.
1831 ieee80211_send_proberesp(vap, wh->i_addr2,
1832 is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
1835 case IEEE80211_FC0_SUBTYPE_AUTH: {
1836 uint16_t algo, seq, status;
1838 if (vap->iv_state != IEEE80211_S_RUN) {
1839 vap->iv_stats.is_rx_mgtdiscard++;
1842 if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1843 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1844 wh, NULL, "%s", "wrong bssid");
1845 vap->iv_stats.is_rx_wrongbss++; /*XXX unique stat?*/
1855 IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1856 algo = le16toh(*(uint16_t *)frm);
1857 seq = le16toh(*(uint16_t *)(frm + 2));
1858 status = le16toh(*(uint16_t *)(frm + 4));
1859 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1860 "recv auth frame with algorithm %d seq %d", algo, seq);
1862 * Consult the ACL policy module if setup.
1864 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1865 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1866 wh, NULL, "%s", "disallowed by ACL");
1867 vap->iv_stats.is_rx_acl++;
1868 ieee80211_send_error(ni, wh->i_addr2,
1869 IEEE80211_FC0_SUBTYPE_AUTH,
1870 (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
1873 if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1874 IEEE80211_DISCARD(vap,
1875 IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1876 wh, NULL, "%s", "TKIP countermeasures enabled");
1877 vap->iv_stats.is_rx_auth_countermeasures++;
1878 ieee80211_send_error(ni, wh->i_addr2,
1879 IEEE80211_FC0_SUBTYPE_AUTH,
1880 IEEE80211_REASON_MIC_FAILURE);
1883 if (algo == IEEE80211_AUTH_ALG_SHARED)
1884 hostap_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1886 else if (algo == IEEE80211_AUTH_ALG_OPEN)
1887 hostap_auth_open(ni, wh, rssi, nf, seq, status);
1888 else if (algo == IEEE80211_AUTH_ALG_LEAP) {
1889 authalgreject(ni, wh, algo,
1890 seq+1, IEEE80211_STATUS_ALG);
1894 * We assume that an unknown algorithm is the result
1895 * of a decryption failure on a shared key auth frame;
1896 * return a status code appropriate for that instead
1897 * of IEEE80211_STATUS_ALG.
1899 * NB: a seq# of 4 is intentional; the decrypted
1900 * frame likely has a bogus seq value.
1902 authalgreject(ni, wh, algo,
1903 4, IEEE80211_STATUS_CHALLENGE);
1909 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1910 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
1911 uint16_t capinfo, lintval;
1912 struct ieee80211_rsnparms rsnparms;
1914 if (vap->iv_state != IEEE80211_S_RUN) {
1915 vap->iv_stats.is_rx_mgtdiscard++;
1918 if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1919 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1920 wh, NULL, "%s", "wrong bssid");
1921 vap->iv_stats.is_rx_assoc_bss++;
1924 if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1926 resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
1929 resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
1931 if (ni == vap->iv_bss) {
1932 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1933 "deny %s request, sta not authenticated",
1934 reassoc ? "reassoc" : "assoc");
1935 ieee80211_send_error(ni, wh->i_addr2,
1936 IEEE80211_FC0_SUBTYPE_DEAUTH,
1937 IEEE80211_REASON_ASSOC_NOT_AUTHED);
1938 vap->iv_stats.is_rx_assoc_notauth++;
1943 * asreq frame format
1944 * [2] capability information
1945 * [2] listen interval
1946 * [6*] current AP address (reassoc only)
1948 * [tlv] supported rates
1949 * [tlv] extended supported rates
1951 * [tlv] HT capabilities
1952 * [tlv] Atheros capabilities
1954 IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4), return);
1955 capinfo = le16toh(*(uint16_t *)frm); frm += 2;
1956 lintval = le16toh(*(uint16_t *)frm); frm += 2;
1958 frm += 6; /* ignore current AP info */
1959 ssid = rates = xrates = wpa = rsn = wme = ath = htcap = NULL;
1961 while (efrm - frm > 1) {
1962 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1964 case IEEE80211_ELEMID_SSID:
1967 case IEEE80211_ELEMID_RATES:
1970 case IEEE80211_ELEMID_XRATES:
1973 case IEEE80211_ELEMID_RSN:
1976 case IEEE80211_ELEMID_HTCAP:
1979 case IEEE80211_ELEMID_VENDOR:
1982 else if (iswmeinfo(frm))
1984 #ifdef IEEE80211_SUPPORT_SUPERG
1985 else if (isatherosoui(frm))
1988 else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1989 if (ishtcapoui(frm) && htcap == NULL)
1996 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1998 IEEE80211_VERIFY_ELEMENT(xrates,
1999 IEEE80211_RATE_MAXSIZE - rates[1], return);
2000 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2001 IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
2002 if (htcap != NULL) {
2003 IEEE80211_VERIFY_LENGTH(htcap[1],
2004 htcap[0] == IEEE80211_ELEMID_VENDOR ?
2005 4 + sizeof(struct ieee80211_ie_htcap)-2 :
2006 sizeof(struct ieee80211_ie_htcap)-2,
2007 return); /* XXX just NULL out? */
2010 if ((vap->iv_flags & IEEE80211_F_WPA) &&
2011 !wpa_assocreq(ni, &rsnparms, wh, wpa, rsn, capinfo))
2013 /* discard challenge after association */
2014 if (ni->ni_challenge != NULL) {
2015 IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
2016 ni->ni_challenge = NULL;
2018 /* NB: 802.11 spec says to ignore station's privacy bit */
2019 if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
2020 capinfomismatch(ni, wh, reassoc, resp,
2021 "capability", capinfo);
2025 * Disallow re-associate w/ invalid slot time setting.
2027 if (ni->ni_associd != 0 &&
2028 IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2029 ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
2030 capinfomismatch(ni, wh, reassoc, resp,
2031 "slot time", capinfo);
2034 rate = ieee80211_setup_rates(ni, rates, xrates,
2035 IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
2036 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2037 if (rate & IEEE80211_RATE_BASIC) {
2038 ratesetmismatch(ni, wh, reassoc, resp, "legacy", rate);
2039 vap->iv_stats.is_rx_assoc_norate++;
2043 * If constrained to 11g-only stations reject an
2044 * 11b-only station. We cheat a bit here by looking
2045 * at the max negotiated xmit rate and assuming anyone
2046 * with a best rate <24Mb/s is an 11b station.
2048 if ((vap->iv_flags & IEEE80211_F_PUREG) && rate < 48) {
2049 ratesetmismatch(ni, wh, reassoc, resp, "11g", rate);
2050 vap->iv_stats.is_rx_assoc_norate++;
2054 * Do HT rate set handling and setup HT node state.
2056 ni->ni_chan = vap->iv_bss->ni_chan;
2057 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
2058 rate = ieee80211_setup_htrates(ni, htcap,
2059 IEEE80211_F_DOFMCS | IEEE80211_F_DONEGO |
2061 if (rate & IEEE80211_RATE_BASIC) {
2062 ratesetmismatch(ni, wh, reassoc, resp,
2064 vap->iv_stats.is_ht_assoc_norate++;
2067 ieee80211_ht_node_init(ni);
2068 ieee80211_ht_updatehtcap(ni, htcap);
2069 } else if (ni->ni_flags & IEEE80211_NODE_HT)
2070 ieee80211_ht_node_cleanup(ni);
2071 #ifdef IEEE80211_SUPPORT_SUPERG
2072 /* Always do ff node cleanup; for A-MSDU */
2073 ieee80211_ff_node_cleanup(ni);
2076 * Allow AMPDU operation only with unencrypted traffic
2077 * or AES-CCM; the 11n spec only specifies these ciphers
2078 * so permitting any others is undefined and can lead
2079 * to interoperability problems.
2081 if ((ni->ni_flags & IEEE80211_NODE_HT) &&
2082 (((vap->iv_flags & IEEE80211_F_WPA) &&
2083 rsnparms.rsn_ucastcipher != IEEE80211_CIPHER_AES_CCM) ||
2084 (vap->iv_flags & (IEEE80211_F_WPA|IEEE80211_F_PRIVACY)) == IEEE80211_F_PRIVACY)) {
2086 IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
2087 "disallow HT use because WEP or TKIP requested, "
2088 "capinfo 0x%x ucastcipher %d", capinfo,
2089 rsnparms.rsn_ucastcipher);
2090 ieee80211_ht_node_cleanup(ni);
2091 #ifdef IEEE80211_SUPPORT_SUPERG
2092 /* Always do ff node cleanup; for A-MSDU */
2093 ieee80211_ff_node_cleanup(ni);
2095 vap->iv_stats.is_ht_assoc_downgrade++;
2098 * If constrained to 11n-only stations reject legacy stations.
2100 if ((vap->iv_flags_ht & IEEE80211_FHT_PUREN) &&
2101 (ni->ni_flags & IEEE80211_NODE_HT) == 0) {
2102 htcapmismatch(ni, wh, reassoc, resp);
2103 vap->iv_stats.is_ht_assoc_nohtcap++;
2106 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
2108 ni->ni_intval = lintval;
2109 ni->ni_capinfo = capinfo;
2110 ni->ni_fhdwell = vap->iv_bss->ni_fhdwell;
2111 ni->ni_fhindex = vap->iv_bss->ni_fhindex;
2114 * XXX maybe better to just expand
2116 if (ieee80211_ies_init(&ni->ni_ies, sfrm, efrm - sfrm)) {
2117 #define setie(_ie, _off) ieee80211_ies_setie(ni->ni_ies, _ie, _off)
2119 setie(wpa_ie, wpa - sfrm);
2121 setie(rsn_ie, rsn - sfrm);
2123 setie(htcap_ie, htcap - sfrm);
2125 setie(wme_ie, wme - sfrm);
2127 * Mark node as capable of QoS.
2129 ni->ni_flags |= IEEE80211_NODE_QOS;
2131 ni->ni_flags &= ~IEEE80211_NODE_QOS;
2132 #ifdef IEEE80211_SUPPORT_SUPERG
2134 setie(ath_ie, ath - sfrm);
2136 * Parse ATH station parameters.
2138 ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
2141 ni->ni_ath_flags = 0;
2144 ni->ni_flags &= ~IEEE80211_NODE_QOS;
2145 ni->ni_ath_flags = 0;
2147 ieee80211_node_join(ni, resp);
2148 ieee80211_deliver_l2uf(ni);
2152 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2153 case IEEE80211_FC0_SUBTYPE_DISASSOC: {
2156 if (vap->iv_state != IEEE80211_S_RUN ||
2157 /* NB: can happen when in promiscuous mode */
2158 !IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
2159 vap->iv_stats.is_rx_mgtdiscard++;
2163 * deauth/disassoc frame format
2166 IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
2167 reason = le16toh(*(uint16_t *)frm);
2168 if (subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
2169 vap->iv_stats.is_rx_deauth++;
2170 IEEE80211_NODE_STAT(ni, rx_deauth);
2172 vap->iv_stats.is_rx_disassoc++;
2173 IEEE80211_NODE_STAT(ni, rx_disassoc);
2175 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2176 "recv %s (reason: %d (%s))",
2177 ieee80211_mgt_subtype_name(subtype),
2178 reason, ieee80211_reason_to_string(reason));
2179 if (ni != vap->iv_bss)
2180 ieee80211_node_leave(ni);
2184 case IEEE80211_FC0_SUBTYPE_ACTION:
2185 case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2186 if (ni == vap->iv_bss) {
2187 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2188 wh, NULL, "%s", "unknown node");
2189 vap->iv_stats.is_rx_mgtdiscard++;
2190 } else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2191 !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2192 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2193 wh, NULL, "%s", "not for us");
2194 vap->iv_stats.is_rx_mgtdiscard++;
2195 } else if (vap->iv_state != IEEE80211_S_RUN) {
2196 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2197 wh, NULL, "wrong state %s",
2198 ieee80211_state_name[vap->iv_state]);
2199 vap->iv_stats.is_rx_mgtdiscard++;
2201 if (ieee80211_parse_action(ni, m0) == 0)
2202 (void)ic->ic_recv_action(ni, wh, frm, efrm);
2206 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2207 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2208 case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
2209 case IEEE80211_FC0_SUBTYPE_ATIM:
2210 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2211 wh, NULL, "%s", "not handled");
2212 vap->iv_stats.is_rx_mgtdiscard++;
2216 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2217 wh, "mgt", "subtype 0x%x not handled", subtype);
2218 vap->iv_stats.is_rx_badsubtype++;
2224 hostap_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2227 case IEEE80211_FC0_SUBTYPE_PS_POLL:
2228 ni->ni_vap->iv_recv_pspoll(ni, m);
2230 case IEEE80211_FC0_SUBTYPE_BAR:
2231 ieee80211_recv_bar(ni, m);
2237 * Process a received ps-poll frame.
2240 ieee80211_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m0)
2242 struct ieee80211vap *vap = ni->ni_vap;
2243 struct ieee80211com *ic = vap->iv_ic;
2244 struct ieee80211_frame_min *wh;
2249 wh = mtod(m0, struct ieee80211_frame_min *);
2250 if (ni->ni_associd == 0) {
2251 IEEE80211_DISCARD(vap,
2252 IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2253 (struct ieee80211_frame *) wh, NULL,
2254 "%s", "unassociated station");
2255 vap->iv_stats.is_ps_unassoc++;
2256 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
2257 IEEE80211_REASON_NOT_ASSOCED);
2261 aid = le16toh(*(uint16_t *)wh->i_dur);
2262 if (aid != ni->ni_associd) {
2263 IEEE80211_DISCARD(vap,
2264 IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2265 (struct ieee80211_frame *) wh, NULL,
2266 "aid mismatch: sta aid 0x%x poll aid 0x%x",
2267 ni->ni_associd, aid);
2268 vap->iv_stats.is_ps_badaid++;
2270 * NB: We used to deauth the station but it turns out
2271 * the Blackberry Curve 8230 (and perhaps other devices)
2272 * sometimes send the wrong AID when WME is negotiated.
2273 * Being more lenient here seems ok as we already check
2274 * the station is associated and we only return frames
2275 * queued for the station (i.e. we don't use the AID).
2280 /* Okay, take the first queued packet and put it out... */
2281 m = ieee80211_node_psq_dequeue(ni, &qlen);
2283 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_POWER, wh->i_addr2,
2284 "%s", "recv ps-poll, but queue empty");
2285 ieee80211_send_nulldata(ieee80211_ref_node(ni));
2286 vap->iv_stats.is_ps_qempty++; /* XXX node stat */
2287 if (vap->iv_set_tim != NULL)
2288 vap->iv_set_tim(ni, 0); /* just in case */
2292 * If there are more packets, set the more packets bit
2293 * in the packet dispatched to the station; otherwise
2294 * turn off the TIM bit.
2297 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2298 "recv ps-poll, send packet, %u still queued", qlen);
2299 m->m_flags |= M_MORE_DATA;
2301 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2302 "%s", "recv ps-poll, send packet, queue empty");
2303 if (vap->iv_set_tim != NULL)
2304 vap->iv_set_tim(ni, 0);
2306 m->m_flags |= M_PWR_SAV; /* bypass PS handling */
2309 * Do the right thing; if it's an encap'ed frame then
2310 * call ieee80211_parent_xmitpkt() else
2311 * call ieee80211_vap_xmitpkt().
2313 if (m->m_flags & M_ENCAP) {
2314 (void) ieee80211_parent_xmitpkt(ic, m);
2316 (void) ieee80211_vap_xmitpkt(vap, m);