xref: /dflybsd-src/sys/netproto/802_11/wlan/ieee80211_hostap.c (revision 4f655ef568316df0b575f05cebf37546415c0d24)
132176cfdSRui Paulo /*-
232176cfdSRui Paulo  * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
332176cfdSRui Paulo  * All rights reserved.
432176cfdSRui Paulo  *
532176cfdSRui Paulo  * Redistribution and use in source and binary forms, with or without
632176cfdSRui Paulo  * modification, are permitted provided that the following conditions
732176cfdSRui Paulo  * are met:
832176cfdSRui Paulo  * 1. Redistributions of source code must retain the above copyright
932176cfdSRui Paulo  *    notice, this list of conditions and the following disclaimer.
1032176cfdSRui Paulo  * 2. Redistributions in binary form must reproduce the above copyright
1132176cfdSRui Paulo  *    notice, this list of conditions and the following disclaimer in the
1232176cfdSRui Paulo  *    documentation and/or other materials provided with the distribution.
1332176cfdSRui Paulo  *
1432176cfdSRui Paulo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1532176cfdSRui Paulo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1632176cfdSRui Paulo  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1732176cfdSRui Paulo  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1832176cfdSRui Paulo  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1932176cfdSRui Paulo  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2032176cfdSRui Paulo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2132176cfdSRui Paulo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2232176cfdSRui Paulo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2332176cfdSRui Paulo  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2432176cfdSRui Paulo  */
2532176cfdSRui Paulo 
26085ff963SMatthew Dillon #include <sys/cdefs.h>
27085ff963SMatthew Dillon #ifdef __FreeBSD__
28085ff963SMatthew Dillon __FBSDID("$FreeBSD$");
29085ff963SMatthew Dillon #endif
30085ff963SMatthew Dillon 
3132176cfdSRui Paulo /*
3232176cfdSRui Paulo  * IEEE 802.11 HOSTAP mode support.
3332176cfdSRui Paulo  */
3432176cfdSRui Paulo #include "opt_inet.h"
3532176cfdSRui Paulo #include "opt_wlan.h"
3632176cfdSRui Paulo 
3732176cfdSRui Paulo #include <sys/param.h>
3832176cfdSRui Paulo #include <sys/systm.h>
3932176cfdSRui Paulo #include <sys/mbuf.h>
4032176cfdSRui Paulo #include <sys/malloc.h>
4132176cfdSRui Paulo #include <sys/kernel.h>
4232176cfdSRui Paulo 
4332176cfdSRui Paulo #include <sys/socket.h>
4432176cfdSRui Paulo #include <sys/sockio.h>
4532176cfdSRui Paulo #include <sys/endian.h>
4632176cfdSRui Paulo #include <sys/errno.h>
4732176cfdSRui Paulo #include <sys/proc.h>
4832176cfdSRui Paulo #include <sys/sysctl.h>
4932176cfdSRui Paulo 
5032176cfdSRui Paulo #include <net/if.h>
51085ff963SMatthew Dillon #include <net/if_var.h>
5232176cfdSRui Paulo #include <net/if_media.h>
5332176cfdSRui Paulo #include <net/if_llc.h>
5432176cfdSRui Paulo #include <net/ethernet.h>
5532176cfdSRui Paulo 
5632176cfdSRui Paulo #include <net/bpf.h>
5732176cfdSRui Paulo 
5832176cfdSRui Paulo #include <netproto/802_11/ieee80211_var.h>
5932176cfdSRui Paulo #include <netproto/802_11/ieee80211_hostap.h>
6032176cfdSRui Paulo #include <netproto/802_11/ieee80211_input.h>
6132176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG
6232176cfdSRui Paulo #include <netproto/802_11/ieee80211_superg.h>
6332176cfdSRui Paulo #endif
6432176cfdSRui Paulo #include <netproto/802_11/ieee80211_wds.h>
6532176cfdSRui Paulo 
6632176cfdSRui Paulo #define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
6732176cfdSRui Paulo 
6832176cfdSRui Paulo static	void hostap_vattach(struct ieee80211vap *);
6932176cfdSRui Paulo static	int hostap_newstate(struct ieee80211vap *, enum ieee80211_state, int);
7032176cfdSRui Paulo static	int hostap_input(struct ieee80211_node *ni, struct mbuf *m,
714f898719SImre Vadász 	    const struct ieee80211_rx_stats *,
7232176cfdSRui Paulo 	    int rssi, int nf);
7332176cfdSRui Paulo static void hostap_deliver_data(struct ieee80211vap *,
7432176cfdSRui Paulo 	    struct ieee80211_node *, struct mbuf *);
7532176cfdSRui Paulo static void hostap_recv_mgmt(struct ieee80211_node *, struct mbuf *,
764f898719SImre Vadász 	    int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf);
7732176cfdSRui Paulo static void hostap_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
7832176cfdSRui Paulo 
7932176cfdSRui Paulo void
ieee80211_hostap_attach(struct ieee80211com * ic)8032176cfdSRui Paulo ieee80211_hostap_attach(struct ieee80211com *ic)
8132176cfdSRui Paulo {
8232176cfdSRui Paulo 	ic->ic_vattach[IEEE80211_M_HOSTAP] = hostap_vattach;
8332176cfdSRui Paulo }
8432176cfdSRui Paulo 
8532176cfdSRui Paulo void
ieee80211_hostap_detach(struct ieee80211com * ic)8632176cfdSRui Paulo ieee80211_hostap_detach(struct ieee80211com *ic)
8732176cfdSRui Paulo {
8832176cfdSRui Paulo }
8932176cfdSRui Paulo 
9032176cfdSRui Paulo static void
hostap_vdetach(struct ieee80211vap * vap)9132176cfdSRui Paulo hostap_vdetach(struct ieee80211vap *vap)
9232176cfdSRui Paulo {
9332176cfdSRui Paulo }
9432176cfdSRui Paulo 
9532176cfdSRui Paulo static void
hostap_vattach(struct ieee80211vap * vap)9632176cfdSRui Paulo hostap_vattach(struct ieee80211vap *vap)
9732176cfdSRui Paulo {
9832176cfdSRui Paulo 	vap->iv_newstate = hostap_newstate;
9932176cfdSRui Paulo 	vap->iv_input = hostap_input;
10032176cfdSRui Paulo 	vap->iv_recv_mgmt = hostap_recv_mgmt;
10132176cfdSRui Paulo 	vap->iv_recv_ctl = hostap_recv_ctl;
10232176cfdSRui Paulo 	vap->iv_opdetach = hostap_vdetach;
10332176cfdSRui Paulo 	vap->iv_deliver_data = hostap_deliver_data;
104085ff963SMatthew Dillon 	vap->iv_recv_pspoll = ieee80211_recv_pspoll;
10532176cfdSRui Paulo }
10632176cfdSRui Paulo 
10732176cfdSRui Paulo static void
sta_disassoc(void * arg,struct ieee80211_node * ni)10832176cfdSRui Paulo sta_disassoc(void *arg, struct ieee80211_node *ni)
10932176cfdSRui Paulo {
11032176cfdSRui Paulo 	struct ieee80211vap *vap = arg;
11132176cfdSRui Paulo 
11232176cfdSRui Paulo 	if (ni->ni_vap == vap && ni->ni_associd != 0) {
11332176cfdSRui Paulo 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
11432176cfdSRui Paulo 			IEEE80211_REASON_ASSOC_LEAVE);
11532176cfdSRui Paulo 		ieee80211_node_leave(ni);
11632176cfdSRui Paulo 	}
11732176cfdSRui Paulo }
11832176cfdSRui Paulo 
11932176cfdSRui Paulo static void
sta_csa(void * arg,struct ieee80211_node * ni)12032176cfdSRui Paulo sta_csa(void *arg, struct ieee80211_node *ni)
12132176cfdSRui Paulo {
12232176cfdSRui Paulo 	struct ieee80211vap *vap = arg;
12332176cfdSRui Paulo 
12432176cfdSRui Paulo 	if (ni->ni_vap == vap && ni->ni_associd != 0)
12532176cfdSRui Paulo 		if (ni->ni_inact > vap->iv_inact_init) {
12632176cfdSRui Paulo 			ni->ni_inact = vap->iv_inact_init;
12732176cfdSRui Paulo 			IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
12832176cfdSRui Paulo 			    "%s: inact %u", __func__, ni->ni_inact);
12932176cfdSRui Paulo 		}
13032176cfdSRui Paulo }
13132176cfdSRui Paulo 
13232176cfdSRui Paulo static void
sta_drop(void * arg,struct ieee80211_node * ni)13332176cfdSRui Paulo sta_drop(void *arg, struct ieee80211_node *ni)
13432176cfdSRui Paulo {
13532176cfdSRui Paulo 	struct ieee80211vap *vap = arg;
13632176cfdSRui Paulo 
13732176cfdSRui Paulo 	if (ni->ni_vap == vap && ni->ni_associd != 0)
13832176cfdSRui Paulo 		ieee80211_node_leave(ni);
13932176cfdSRui Paulo }
14032176cfdSRui Paulo 
14132176cfdSRui Paulo /*
14232176cfdSRui Paulo  * Does a channel change require associated stations to re-associate
14332176cfdSRui Paulo  * so protocol state is correct.  This is used when doing CSA across
14432176cfdSRui Paulo  * bands or similar (e.g. HT -> legacy).
14532176cfdSRui Paulo  */
14632176cfdSRui Paulo static int
isbandchange(struct ieee80211com * ic)14732176cfdSRui Paulo isbandchange(struct ieee80211com *ic)
14832176cfdSRui Paulo {
14932176cfdSRui Paulo 	return ((ic->ic_bsschan->ic_flags ^ ic->ic_csa_newchan->ic_flags) &
15032176cfdSRui Paulo 	    (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_HALF |
15132176cfdSRui Paulo 	     IEEE80211_CHAN_QUARTER | IEEE80211_CHAN_HT)) != 0;
15232176cfdSRui Paulo }
15332176cfdSRui Paulo 
15432176cfdSRui Paulo /*
15532176cfdSRui Paulo  * IEEE80211_M_HOSTAP vap state machine handler.
15632176cfdSRui Paulo  */
15732176cfdSRui Paulo static int
hostap_newstate(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)15832176cfdSRui Paulo hostap_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
15932176cfdSRui Paulo {
16032176cfdSRui Paulo 	struct ieee80211com *ic = vap->iv_ic;
16132176cfdSRui Paulo 	enum ieee80211_state ostate;
162085ff963SMatthew Dillon 
163085ff963SMatthew Dillon 	IEEE80211_LOCK_ASSERT(ic);
16432176cfdSRui Paulo 
16532176cfdSRui Paulo 	ostate = vap->iv_state;
16632176cfdSRui Paulo 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
16732176cfdSRui Paulo 	    __func__, ieee80211_state_name[ostate],
16832176cfdSRui Paulo 	    ieee80211_state_name[nstate], arg);
16932176cfdSRui Paulo 	vap->iv_state = nstate;			/* state transition */
17032176cfdSRui Paulo 	if (ostate != IEEE80211_S_SCAN)
17132176cfdSRui Paulo 		ieee80211_cancel_scan(vap);	/* background scan */
17232176cfdSRui Paulo 	switch (nstate) {
17332176cfdSRui Paulo 	case IEEE80211_S_INIT:
17432176cfdSRui Paulo 		switch (ostate) {
17532176cfdSRui Paulo 		case IEEE80211_S_SCAN:
17632176cfdSRui Paulo 			ieee80211_cancel_scan(vap);
17732176cfdSRui Paulo 			break;
17832176cfdSRui Paulo 		case IEEE80211_S_CAC:
17932176cfdSRui Paulo 			ieee80211_dfs_cac_stop(vap);
18032176cfdSRui Paulo 			break;
18132176cfdSRui Paulo 		case IEEE80211_S_RUN:
18232176cfdSRui Paulo 			ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
18332176cfdSRui Paulo 			break;
18432176cfdSRui Paulo 		default:
18532176cfdSRui Paulo 			break;
18632176cfdSRui Paulo 		}
18732176cfdSRui Paulo 		if (ostate != IEEE80211_S_INIT) {
18832176cfdSRui Paulo 			/* NB: optimize INIT -> INIT case */
18932176cfdSRui Paulo 			ieee80211_reset_bss(vap);
19032176cfdSRui Paulo 		}
19132176cfdSRui Paulo 		if (vap->iv_auth->ia_detach != NULL)
19232176cfdSRui Paulo 			vap->iv_auth->ia_detach(vap);
19332176cfdSRui Paulo 		break;
19432176cfdSRui Paulo 	case IEEE80211_S_SCAN:
19532176cfdSRui Paulo 		switch (ostate) {
19632176cfdSRui Paulo 		case IEEE80211_S_CSA:
19732176cfdSRui Paulo 		case IEEE80211_S_RUN:
19832176cfdSRui Paulo 			ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
19932176cfdSRui Paulo 			/*
20032176cfdSRui Paulo 			 * Clear overlapping BSS state; the beacon frame
20132176cfdSRui Paulo 			 * will be reconstructed on transition to the RUN
20232176cfdSRui Paulo 			 * state and the timeout routines check if the flag
20332176cfdSRui Paulo 			 * is set before doing anything so this is sufficient.
20432176cfdSRui Paulo 			 */
20532176cfdSRui Paulo 			ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
20632176cfdSRui Paulo 			ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
20732176cfdSRui Paulo 			/* fall thru... */
20832176cfdSRui Paulo 		case IEEE80211_S_CAC:
20932176cfdSRui Paulo 			/*
21032176cfdSRui Paulo 			 * NB: We may get here because of a manual channel
21132176cfdSRui Paulo 			 *     change in which case we need to stop CAC
21232176cfdSRui Paulo 			 * XXX no need to stop if ostate RUN but it's ok
21332176cfdSRui Paulo 			 */
21432176cfdSRui Paulo 			ieee80211_dfs_cac_stop(vap);
21532176cfdSRui Paulo 			/* fall thru... */
21632176cfdSRui Paulo 		case IEEE80211_S_INIT:
21732176cfdSRui Paulo 			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
21832176cfdSRui Paulo 			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
21932176cfdSRui Paulo 				/*
22032176cfdSRui Paulo 				 * Already have a channel; bypass the
22132176cfdSRui Paulo 				 * scan and startup immediately.
22232176cfdSRui Paulo 				 * ieee80211_create_ibss will call back to
22332176cfdSRui Paulo 				 * move us to RUN state.
22432176cfdSRui Paulo 				 */
22532176cfdSRui Paulo 				ieee80211_create_ibss(vap, vap->iv_des_chan);
22632176cfdSRui Paulo 				break;
22732176cfdSRui Paulo 			}
22832176cfdSRui Paulo 			/*
22932176cfdSRui Paulo 			 * Initiate a scan.  We can come here as a result
23032176cfdSRui Paulo 			 * of an IEEE80211_IOC_SCAN_REQ too in which case
23132176cfdSRui Paulo 			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
23232176cfdSRui Paulo 			 * and the scan request parameters will be present
23332176cfdSRui Paulo 			 * in iv_scanreq.  Otherwise we do the default.
23432176cfdSRui Paulo 			 */
23532176cfdSRui Paulo 			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
23632176cfdSRui Paulo 				ieee80211_check_scan(vap,
23732176cfdSRui Paulo 				    vap->iv_scanreq_flags,
23832176cfdSRui Paulo 				    vap->iv_scanreq_duration,
23932176cfdSRui Paulo 				    vap->iv_scanreq_mindwell,
24032176cfdSRui Paulo 				    vap->iv_scanreq_maxdwell,
24132176cfdSRui Paulo 				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
24232176cfdSRui Paulo 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
24332176cfdSRui Paulo 			} else
24432176cfdSRui Paulo 				ieee80211_check_scan_current(vap);
24532176cfdSRui Paulo 			break;
24632176cfdSRui Paulo 		case IEEE80211_S_SCAN:
24732176cfdSRui Paulo 			/*
24832176cfdSRui Paulo 			 * A state change requires a reset; scan.
24932176cfdSRui Paulo 			 */
25032176cfdSRui Paulo 			ieee80211_check_scan_current(vap);
25132176cfdSRui Paulo 			break;
25232176cfdSRui Paulo 		default:
25332176cfdSRui Paulo 			break;
25432176cfdSRui Paulo 		}
25532176cfdSRui Paulo 		break;
25632176cfdSRui Paulo 	case IEEE80211_S_CAC:
25732176cfdSRui Paulo 		/*
25832176cfdSRui Paulo 		 * Start CAC on a DFS channel.  We come here when starting
25932176cfdSRui Paulo 		 * a bss on a DFS channel (see ieee80211_create_ibss).
26032176cfdSRui Paulo 		 */
26132176cfdSRui Paulo 		ieee80211_dfs_cac_start(vap);
26232176cfdSRui Paulo 		break;
26332176cfdSRui Paulo 	case IEEE80211_S_RUN:
26432176cfdSRui Paulo 		if (vap->iv_flags & IEEE80211_F_WPA) {
26532176cfdSRui Paulo 			/* XXX validate prerequisites */
26632176cfdSRui Paulo 		}
26732176cfdSRui Paulo 		switch (ostate) {
26832176cfdSRui Paulo 		case IEEE80211_S_INIT:
26932176cfdSRui Paulo 			/*
27032176cfdSRui Paulo 			 * Already have a channel; bypass the
27132176cfdSRui Paulo 			 * scan and startup immediately.
27232176cfdSRui Paulo 			 * Note that ieee80211_create_ibss will call
27332176cfdSRui Paulo 			 * back to do a RUN->RUN state change.
27432176cfdSRui Paulo 			 */
27532176cfdSRui Paulo 			ieee80211_create_ibss(vap,
27632176cfdSRui Paulo 			    ieee80211_ht_adjust_channel(ic,
27732176cfdSRui Paulo 				ic->ic_curchan, vap->iv_flags_ht));
27832176cfdSRui Paulo 			/* NB: iv_bss is changed on return */
27932176cfdSRui Paulo 			break;
28032176cfdSRui Paulo 		case IEEE80211_S_CAC:
28132176cfdSRui Paulo 			/*
28232176cfdSRui Paulo 			 * NB: This is the normal state change when CAC
28332176cfdSRui Paulo 			 * expires and no radar was detected; no need to
28432176cfdSRui Paulo 			 * clear the CAC timer as it's already expired.
28532176cfdSRui Paulo 			 */
28632176cfdSRui Paulo 			/* fall thru... */
28732176cfdSRui Paulo 		case IEEE80211_S_CSA:
28832176cfdSRui Paulo 			/*
28932176cfdSRui Paulo 			 * Shorten inactivity timer of associated stations
29032176cfdSRui Paulo 			 * to weed out sta's that don't follow a CSA.
29132176cfdSRui Paulo 			 */
29232176cfdSRui Paulo 			ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
29332176cfdSRui Paulo 			/*
29432176cfdSRui Paulo 			 * Update bss node channel to reflect where
29532176cfdSRui Paulo 			 * we landed after CSA.
29632176cfdSRui Paulo 			 */
29732176cfdSRui Paulo 			ieee80211_node_set_chan(vap->iv_bss,
29832176cfdSRui Paulo 			    ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
29932176cfdSRui Paulo 				ieee80211_htchanflags(vap->iv_bss->ni_chan)));
30032176cfdSRui Paulo 			/* XXX bypass debug msgs */
30132176cfdSRui Paulo 			break;
30232176cfdSRui Paulo 		case IEEE80211_S_SCAN:
30332176cfdSRui Paulo 		case IEEE80211_S_RUN:
30432176cfdSRui Paulo #ifdef IEEE80211_DEBUG
30532176cfdSRui Paulo 			if (ieee80211_msg_debug(vap)) {
30632176cfdSRui Paulo 				struct ieee80211_node *ni = vap->iv_bss;
30732176cfdSRui Paulo 				ieee80211_note(vap,
3081e290df3SAntonio Huete Jimenez 				    "synchronized with %s ssid ",
309085ff963SMatthew Dillon 				    ether_sprintf(ni->ni_bssid));
31032176cfdSRui Paulo 				ieee80211_print_essid(ni->ni_essid,
31132176cfdSRui Paulo 				    ni->ni_esslen);
31232176cfdSRui Paulo 				/* XXX MCS/HT */
3136168f72eSRui Paulo 				kprintf(" channel %d start %uMb\n",
31432176cfdSRui Paulo 				    ieee80211_chan2ieee(ic, ic->ic_curchan),
31532176cfdSRui Paulo 				    IEEE80211_RATE2MBS(ni->ni_txrate));
31632176cfdSRui Paulo 			}
31732176cfdSRui Paulo #endif
31832176cfdSRui Paulo 			break;
31932176cfdSRui Paulo 		default:
32032176cfdSRui Paulo 			break;
32132176cfdSRui Paulo 		}
32232176cfdSRui Paulo 		/*
32332176cfdSRui Paulo 		 * Start/stop the authenticator.  We delay until here
32432176cfdSRui Paulo 		 * to allow configuration to happen out of order.
32532176cfdSRui Paulo 		 */
32632176cfdSRui Paulo 		if (vap->iv_auth->ia_attach != NULL) {
32732176cfdSRui Paulo 			/* XXX check failure */
32832176cfdSRui Paulo 			vap->iv_auth->ia_attach(vap);
32932176cfdSRui Paulo 		} else if (vap->iv_auth->ia_detach != NULL) {
33032176cfdSRui Paulo 			vap->iv_auth->ia_detach(vap);
33132176cfdSRui Paulo 		}
33232176cfdSRui Paulo 		ieee80211_node_authorize(vap->iv_bss);
33332176cfdSRui Paulo 		break;
33432176cfdSRui Paulo 	case IEEE80211_S_CSA:
33532176cfdSRui Paulo 		if (ostate == IEEE80211_S_RUN && isbandchange(ic)) {
33632176cfdSRui Paulo 			/*
33732176cfdSRui Paulo 			 * On a ``band change'' silently drop associated
33832176cfdSRui Paulo 			 * stations as they must re-associate before they
33932176cfdSRui Paulo 			 * can pass traffic (as otherwise protocol state
34032176cfdSRui Paulo 			 * such as capabilities and the negotiated rate
34132176cfdSRui Paulo 			 * set may/will be wrong).
34232176cfdSRui Paulo 			 */
34332176cfdSRui Paulo 			ieee80211_iterate_nodes(&ic->ic_sta, sta_drop, vap);
34432176cfdSRui Paulo 		}
34532176cfdSRui Paulo 		break;
34632176cfdSRui Paulo 	default:
34732176cfdSRui Paulo 		break;
34832176cfdSRui Paulo 	}
34932176cfdSRui Paulo 	return 0;
35032176cfdSRui Paulo }
35132176cfdSRui Paulo 
35232176cfdSRui Paulo static void
hostap_deliver_data(struct ieee80211vap * vap,struct ieee80211_node * ni,struct mbuf * m)35332176cfdSRui Paulo hostap_deliver_data(struct ieee80211vap *vap,
35432176cfdSRui Paulo 	struct ieee80211_node *ni, struct mbuf *m)
35532176cfdSRui Paulo {
35632176cfdSRui Paulo 	struct ether_header *eh = mtod(m, struct ether_header *);
35732176cfdSRui Paulo 	struct ifnet *ifp = vap->iv_ifp;
35832176cfdSRui Paulo 
35932176cfdSRui Paulo 	/* clear driver/net80211 flags before passing up */
360294727bfSImre Vadász #if defined(__DragonFly__)
361294727bfSImre Vadász 	m->m_flags &= ~(M_80211_RX | M_MCAST | M_BCAST);
362294727bfSImre Vadász #else
363085ff963SMatthew Dillon 	m->m_flags &= ~(M_MCAST | M_BCAST);
364085ff963SMatthew Dillon 	m_clrprotoflags(m);
365085ff963SMatthew Dillon #endif
36632176cfdSRui Paulo 
36732176cfdSRui Paulo 	KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
36832176cfdSRui Paulo 	    ("gack, opmode %d", vap->iv_opmode));
36932176cfdSRui Paulo 	/*
37032176cfdSRui Paulo 	 * Do accounting.
37132176cfdSRui Paulo 	 */
372*4f655ef5SMatthew Dillon 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
37332176cfdSRui Paulo 	IEEE80211_NODE_STAT(ni, rx_data);
37432176cfdSRui Paulo 	IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
37532176cfdSRui Paulo 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
37632176cfdSRui Paulo 		m->m_flags |= M_MCAST;		/* XXX M_BCAST? */
37732176cfdSRui Paulo 		IEEE80211_NODE_STAT(ni, rx_mcast);
37832176cfdSRui Paulo 	} else
37932176cfdSRui Paulo 		IEEE80211_NODE_STAT(ni, rx_ucast);
38032176cfdSRui Paulo 
38132176cfdSRui Paulo 	/* perform as a bridge within the AP */
38232176cfdSRui Paulo 	if ((vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0) {
38332176cfdSRui Paulo 		struct mbuf *mcopy = NULL;
38432176cfdSRui Paulo 
38532176cfdSRui Paulo 		if (m->m_flags & M_MCAST) {
386b5523eacSSascha Wildner 			mcopy = m_dup(m, M_NOWAIT);
38732176cfdSRui Paulo 			if (mcopy == NULL)
388*4f655ef5SMatthew Dillon 				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
38932176cfdSRui Paulo 			else
39032176cfdSRui Paulo 				mcopy->m_flags |= M_MCAST;
39132176cfdSRui Paulo 		} else {
39232176cfdSRui Paulo 			/*
39332176cfdSRui Paulo 			 * Check if the destination is associated with the
39432176cfdSRui Paulo 			 * same vap and authorized to receive traffic.
39532176cfdSRui Paulo 			 * Beware of traffic destined for the vap itself;
39632176cfdSRui Paulo 			 * sending it will not work; just let it be delivered
39732176cfdSRui Paulo 			 * normally.
39832176cfdSRui Paulo 			 */
39932176cfdSRui Paulo 			struct ieee80211_node *sta = ieee80211_find_vap_node(
40032176cfdSRui Paulo 			     &vap->iv_ic->ic_sta, vap, eh->ether_dhost);
40132176cfdSRui Paulo 			if (sta != NULL) {
40232176cfdSRui Paulo 				if (ieee80211_node_is_authorized(sta)) {
40332176cfdSRui Paulo 					/*
40432176cfdSRui Paulo 					 * Beware of sending to ourself; this
40532176cfdSRui Paulo 					 * needs to happen via the normal
40632176cfdSRui Paulo 					 * input path.
40732176cfdSRui Paulo 					 */
40832176cfdSRui Paulo 					if (sta != vap->iv_bss) {
40932176cfdSRui Paulo 						mcopy = m;
41032176cfdSRui Paulo 						m = NULL;
41132176cfdSRui Paulo 					}
41232176cfdSRui Paulo 				} else {
41332176cfdSRui Paulo 					vap->iv_stats.is_rx_unauth++;
41432176cfdSRui Paulo 					IEEE80211_NODE_STAT(sta, rx_unauth);
41532176cfdSRui Paulo 				}
41632176cfdSRui Paulo 				ieee80211_free_node(sta);
41732176cfdSRui Paulo 			}
41832176cfdSRui Paulo 		}
41932176cfdSRui Paulo 		if (mcopy != NULL) {
420085ff963SMatthew Dillon 			int len, err;
421085ff963SMatthew Dillon 			len = mcopy->m_pkthdr.len;
422085ff963SMatthew Dillon 			err = ieee80211_vap_xmitpkt(vap, mcopy);
42332176cfdSRui Paulo 			if (err) {
42432176cfdSRui Paulo 				/* NB: IFQ_HANDOFF reclaims mcopy */
42532176cfdSRui Paulo 			} else {
426*4f655ef5SMatthew Dillon 				if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
42732176cfdSRui Paulo 			}
42832176cfdSRui Paulo 		}
42932176cfdSRui Paulo 	}
43032176cfdSRui Paulo 	if (m != NULL) {
43132176cfdSRui Paulo 		/*
43232176cfdSRui Paulo 		 * Mark frame as coming from vap's interface.
43332176cfdSRui Paulo 		 */
43432176cfdSRui Paulo 		m->m_pkthdr.rcvif = ifp;
43532176cfdSRui Paulo 		if (m->m_flags & M_MCAST) {
43632176cfdSRui Paulo 			/*
43732176cfdSRui Paulo 			 * Spam DWDS vap's w/ multicast traffic.
43832176cfdSRui Paulo 			 */
43932176cfdSRui Paulo 			/* XXX only if dwds in use? */
44032176cfdSRui Paulo 			ieee80211_dwds_mcast(vap, m);
44132176cfdSRui Paulo 		}
44232176cfdSRui Paulo 		if (ni->ni_vlan != 0) {
44332176cfdSRui Paulo 			/* attach vlan tag */
444085ff963SMatthew Dillon #if defined(__DragonFly__)
445085ff963SMatthew Dillon 			/* XXX ntohs() needed? */
44634a60cf6SRui Paulo 			m->m_pkthdr.ether_vlantag = ni->ni_vlan;
447085ff963SMatthew Dillon #else
448085ff963SMatthew Dillon 			m->m_pkthdr.ether_vtag = ni->ni_vlan;
449085ff963SMatthew Dillon #endif
45032176cfdSRui Paulo 			m->m_flags |= M_VLANTAG;
45132176cfdSRui Paulo 		}
452*4f655ef5SMatthew Dillon #if defined(__DragonFly__)
45373029d08SFranco Fichtner 		ifp->if_input(ifp, m, NULL, -1);
454*4f655ef5SMatthew Dillon #else
455*4f655ef5SMatthew Dillon 		ifp->if_input(ifp, m);
456*4f655ef5SMatthew Dillon #endif
45732176cfdSRui Paulo 	}
45832176cfdSRui Paulo }
45932176cfdSRui Paulo 
46032176cfdSRui Paulo /*
46132176cfdSRui Paulo  * Decide if a received management frame should be
46232176cfdSRui Paulo  * printed when debugging is enabled.  This filters some
46332176cfdSRui Paulo  * of the less interesting frames that come frequently
46432176cfdSRui Paulo  * (e.g. beacons).
46532176cfdSRui Paulo  */
46632176cfdSRui Paulo static __inline int
doprint(struct ieee80211vap * vap,int subtype)46732176cfdSRui Paulo doprint(struct ieee80211vap *vap, int subtype)
46832176cfdSRui Paulo {
46932176cfdSRui Paulo 	switch (subtype) {
47032176cfdSRui Paulo 	case IEEE80211_FC0_SUBTYPE_BEACON:
47132176cfdSRui Paulo 		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
47232176cfdSRui Paulo 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
47332176cfdSRui Paulo 		return 0;
47432176cfdSRui Paulo 	}
47532176cfdSRui Paulo 	return 1;
47632176cfdSRui Paulo }
47732176cfdSRui Paulo 
47832176cfdSRui Paulo /*
47932176cfdSRui Paulo  * Process a received frame.  The node associated with the sender
48032176cfdSRui Paulo  * should be supplied.  If nothing was found in the node table then
48132176cfdSRui Paulo  * the caller is assumed to supply a reference to iv_bss instead.
48232176cfdSRui Paulo  * The RSSI and a timestamp are also supplied.  The RSSI data is used
48332176cfdSRui Paulo  * during AP scanning to select a AP to associate with; it can have
48432176cfdSRui Paulo  * any units so long as values have consistent units and higher values
48532176cfdSRui Paulo  * mean ``better signal''.  The receive timestamp is currently not used
48632176cfdSRui Paulo  * by the 802.11 layer.
48732176cfdSRui Paulo  */
48832176cfdSRui Paulo static int
hostap_input(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_rx_stats * rxs,int rssi,int nf)4894f898719SImre Vadász hostap_input(struct ieee80211_node *ni, struct mbuf *m,
4904f898719SImre Vadász     const struct ieee80211_rx_stats *rxs, int rssi, int nf)
49132176cfdSRui Paulo {
49232176cfdSRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
49332176cfdSRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
49432176cfdSRui Paulo 	struct ifnet *ifp = vap->iv_ifp;
49532176cfdSRui Paulo 	struct ieee80211_frame *wh;
49632176cfdSRui Paulo 	struct ieee80211_key *key;
49732176cfdSRui Paulo 	struct ether_header *eh;
49832176cfdSRui Paulo 	int hdrspace, need_tap = 1;	/* mbuf need to be tapped. */
49932176cfdSRui Paulo 	uint8_t dir, type, subtype, qos;
50032176cfdSRui Paulo 	uint8_t *bssid;
50132176cfdSRui Paulo 
50232176cfdSRui Paulo 	if (m->m_flags & M_AMPDU_MPDU) {
50332176cfdSRui Paulo 		/*
50432176cfdSRui Paulo 		 * Fastpath for A-MPDU reorder q resubmission.  Frames
50532176cfdSRui Paulo 		 * w/ M_AMPDU_MPDU marked have already passed through
50632176cfdSRui Paulo 		 * here but were received out of order and been held on
50732176cfdSRui Paulo 		 * the reorder queue.  When resubmitted they are marked
50832176cfdSRui Paulo 		 * with the M_AMPDU_MPDU flag and we can bypass most of
50932176cfdSRui Paulo 		 * the normal processing.
51032176cfdSRui Paulo 		 */
51132176cfdSRui Paulo 		wh = mtod(m, struct ieee80211_frame *);
51232176cfdSRui Paulo 		type = IEEE80211_FC0_TYPE_DATA;
51332176cfdSRui Paulo 		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
51432176cfdSRui Paulo 		subtype = IEEE80211_FC0_SUBTYPE_QOS;
51532176cfdSRui Paulo 		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
51632176cfdSRui Paulo 		goto resubmit_ampdu;
51732176cfdSRui Paulo 	}
51832176cfdSRui Paulo 
51932176cfdSRui Paulo 	KASSERT(ni != NULL, ("null node"));
52032176cfdSRui Paulo 	ni->ni_inact = ni->ni_inact_reload;
52132176cfdSRui Paulo 
52232176cfdSRui Paulo 	type = -1;			/* undefined */
52332176cfdSRui Paulo 
52432176cfdSRui Paulo 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
52532176cfdSRui Paulo 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
52632176cfdSRui Paulo 		    ni->ni_macaddr, NULL,
52732176cfdSRui Paulo 		    "too short (1): len %u", m->m_pkthdr.len);
52832176cfdSRui Paulo 		vap->iv_stats.is_rx_tooshort++;
52932176cfdSRui Paulo 		goto out;
53032176cfdSRui Paulo 	}
53132176cfdSRui Paulo 	/*
53232176cfdSRui Paulo 	 * Bit of a cheat here, we use a pointer for a 3-address
53332176cfdSRui Paulo 	 * frame format but don't reference fields past outside
53432176cfdSRui Paulo 	 * ieee80211_frame_min w/o first validating the data is
53532176cfdSRui Paulo 	 * present.
53632176cfdSRui Paulo 	 */
53732176cfdSRui Paulo 	wh = mtod(m, struct ieee80211_frame *);
53832176cfdSRui Paulo 
53932176cfdSRui Paulo 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
54032176cfdSRui Paulo 	    IEEE80211_FC0_VERSION_0) {
54132176cfdSRui Paulo 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
54232176cfdSRui Paulo 		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
54332176cfdSRui Paulo 		    wh->i_fc[0], wh->i_fc[1]);
54432176cfdSRui Paulo 		vap->iv_stats.is_rx_badversion++;
54532176cfdSRui Paulo 		goto err;
54632176cfdSRui Paulo 	}
54732176cfdSRui Paulo 
54832176cfdSRui Paulo 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
54932176cfdSRui Paulo 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
55032176cfdSRui Paulo 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
55132176cfdSRui Paulo 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
55232176cfdSRui Paulo 		if (dir != IEEE80211_FC1_DIR_NODS)
55332176cfdSRui Paulo 			bssid = wh->i_addr1;
55432176cfdSRui Paulo 		else if (type == IEEE80211_FC0_TYPE_CTL)
55532176cfdSRui Paulo 			bssid = wh->i_addr1;
55632176cfdSRui Paulo 		else {
55732176cfdSRui Paulo 			if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
55832176cfdSRui Paulo 				IEEE80211_DISCARD_MAC(vap,
55932176cfdSRui Paulo 				    IEEE80211_MSG_ANY, ni->ni_macaddr,
56032176cfdSRui Paulo 				    NULL, "too short (2): len %u",
56132176cfdSRui Paulo 				    m->m_pkthdr.len);
56232176cfdSRui Paulo 				vap->iv_stats.is_rx_tooshort++;
56332176cfdSRui Paulo 				goto out;
56432176cfdSRui Paulo 			}
56532176cfdSRui Paulo 			bssid = wh->i_addr3;
56632176cfdSRui Paulo 		}
56732176cfdSRui Paulo 		/*
56832176cfdSRui Paulo 		 * Validate the bssid.
56932176cfdSRui Paulo 		 */
57032176cfdSRui Paulo 		if (!(type == IEEE80211_FC0_TYPE_MGT &&
57132176cfdSRui Paulo 		      subtype == IEEE80211_FC0_SUBTYPE_BEACON) &&
57232176cfdSRui Paulo 		    !IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
57332176cfdSRui Paulo 		    !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
57432176cfdSRui Paulo 			/* not interested in */
57532176cfdSRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
57632176cfdSRui Paulo 			    bssid, NULL, "%s", "not to bss");
57732176cfdSRui Paulo 			vap->iv_stats.is_rx_wrongbss++;
57832176cfdSRui Paulo 			goto out;
57932176cfdSRui Paulo 		}
58032176cfdSRui Paulo 
58132176cfdSRui Paulo 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
58232176cfdSRui Paulo 		ni->ni_noise = nf;
5834f898719SImre Vadász 		if (IEEE80211_HAS_SEQ(type, subtype)) {
58432176cfdSRui Paulo 			uint8_t tid = ieee80211_gettid(wh);
58532176cfdSRui Paulo 			if (IEEE80211_QOS_HAS_SEQ(wh) &&
58632176cfdSRui Paulo 			    TID_TO_WME_AC(tid) >= WME_AC_VI)
58732176cfdSRui Paulo 				ic->ic_wme.wme_hipri_traffic++;
588*4f655ef5SMatthew Dillon 			if (! ieee80211_check_rxseq(ni, wh, bssid))
58932176cfdSRui Paulo 				goto out;
59032176cfdSRui Paulo 		}
59132176cfdSRui Paulo 	}
59232176cfdSRui Paulo 
59332176cfdSRui Paulo 	switch (type) {
59432176cfdSRui Paulo 	case IEEE80211_FC0_TYPE_DATA:
59532176cfdSRui Paulo 		hdrspace = ieee80211_hdrspace(ic, wh);
59632176cfdSRui Paulo 		if (m->m_len < hdrspace &&
59732176cfdSRui Paulo 		    (m = m_pullup(m, hdrspace)) == NULL) {
59832176cfdSRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
59932176cfdSRui Paulo 			    ni->ni_macaddr, NULL,
60032176cfdSRui Paulo 			    "data too short: expecting %u", hdrspace);
60132176cfdSRui Paulo 			vap->iv_stats.is_rx_tooshort++;
60232176cfdSRui Paulo 			goto out;		/* XXX */
60332176cfdSRui Paulo 		}
60432176cfdSRui Paulo 		if (!(dir == IEEE80211_FC1_DIR_TODS ||
60532176cfdSRui Paulo 		     (dir == IEEE80211_FC1_DIR_DSTODS &&
60632176cfdSRui Paulo 		      (vap->iv_flags & IEEE80211_F_DWDS)))) {
60732176cfdSRui Paulo 			if (dir != IEEE80211_FC1_DIR_DSTODS) {
60832176cfdSRui Paulo 				IEEE80211_DISCARD(vap,
60932176cfdSRui Paulo 				    IEEE80211_MSG_INPUT, wh, "data",
61032176cfdSRui Paulo 				    "incorrect dir 0x%x", dir);
61132176cfdSRui Paulo 			} else {
61232176cfdSRui Paulo 				IEEE80211_DISCARD(vap,
61332176cfdSRui Paulo 				    IEEE80211_MSG_INPUT |
61432176cfdSRui Paulo 				    IEEE80211_MSG_WDS, wh,
61532176cfdSRui Paulo 				    "4-address data",
61632176cfdSRui Paulo 				    "%s", "DWDS not enabled");
61732176cfdSRui Paulo 			}
61832176cfdSRui Paulo 			vap->iv_stats.is_rx_wrongdir++;
61932176cfdSRui Paulo 			goto out;
62032176cfdSRui Paulo 		}
62132176cfdSRui Paulo 		/* check if source STA is associated */
62232176cfdSRui Paulo 		if (ni == vap->iv_bss) {
62332176cfdSRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
62432176cfdSRui Paulo 			    wh, "data", "%s", "unknown src");
62532176cfdSRui Paulo 			ieee80211_send_error(ni, wh->i_addr2,
62632176cfdSRui Paulo 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
62732176cfdSRui Paulo 			    IEEE80211_REASON_NOT_AUTHED);
62832176cfdSRui Paulo 			vap->iv_stats.is_rx_notassoc++;
62932176cfdSRui Paulo 			goto err;
63032176cfdSRui Paulo 		}
63132176cfdSRui Paulo 		if (ni->ni_associd == 0) {
63232176cfdSRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
63332176cfdSRui Paulo 			    wh, "data", "%s", "unassoc src");
63432176cfdSRui Paulo 			IEEE80211_SEND_MGMT(ni,
63532176cfdSRui Paulo 			    IEEE80211_FC0_SUBTYPE_DISASSOC,
63632176cfdSRui Paulo 			    IEEE80211_REASON_NOT_ASSOCED);
63732176cfdSRui Paulo 			vap->iv_stats.is_rx_notassoc++;
63832176cfdSRui Paulo 			goto err;
63932176cfdSRui Paulo 		}
64032176cfdSRui Paulo 
64132176cfdSRui Paulo 		/*
64232176cfdSRui Paulo 		 * Check for power save state change.
64332176cfdSRui Paulo 		 * XXX out-of-order A-MPDU frames?
64432176cfdSRui Paulo 		 */
64532176cfdSRui Paulo 		if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
64632176cfdSRui Paulo 		    (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
647085ff963SMatthew Dillon 			vap->iv_node_ps(ni,
64832176cfdSRui Paulo 				wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
64932176cfdSRui Paulo 		/*
65032176cfdSRui Paulo 		 * For 4-address packets handle WDS discovery
65132176cfdSRui Paulo 		 * notifications.  Once a WDS link is setup frames
65232176cfdSRui Paulo 		 * are just delivered to the WDS vap (see below).
65332176cfdSRui Paulo 		 */
65432176cfdSRui Paulo 		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap == NULL) {
65532176cfdSRui Paulo 			if (!ieee80211_node_is_authorized(ni)) {
65632176cfdSRui Paulo 				IEEE80211_DISCARD(vap,
65732176cfdSRui Paulo 				    IEEE80211_MSG_INPUT |
65832176cfdSRui Paulo 				    IEEE80211_MSG_WDS, wh,
65932176cfdSRui Paulo 				    "4-address data",
66032176cfdSRui Paulo 				    "%s", "unauthorized port");
66132176cfdSRui Paulo 				vap->iv_stats.is_rx_unauth++;
66232176cfdSRui Paulo 				IEEE80211_NODE_STAT(ni, rx_unauth);
66332176cfdSRui Paulo 				goto err;
66432176cfdSRui Paulo 			}
66532176cfdSRui Paulo 			ieee80211_dwds_discover(ni, m);
66632176cfdSRui Paulo 			return type;
66732176cfdSRui Paulo 		}
66832176cfdSRui Paulo 
66932176cfdSRui Paulo 		/*
67032176cfdSRui Paulo 		 * Handle A-MPDU re-ordering.  If the frame is to be
67132176cfdSRui Paulo 		 * processed directly then ieee80211_ampdu_reorder
67232176cfdSRui Paulo 		 * will return 0; otherwise it has consumed the mbuf
67332176cfdSRui Paulo 		 * and we should do nothing more with it.
67432176cfdSRui Paulo 		 */
67532176cfdSRui Paulo 		if ((m->m_flags & M_AMPDU) &&
67632176cfdSRui Paulo 		    ieee80211_ampdu_reorder(ni, m) != 0) {
67732176cfdSRui Paulo 			m = NULL;
67832176cfdSRui Paulo 			goto out;
67932176cfdSRui Paulo 		}
68032176cfdSRui Paulo 	resubmit_ampdu:
68132176cfdSRui Paulo 
68232176cfdSRui Paulo 		/*
68332176cfdSRui Paulo 		 * Handle privacy requirements.  Note that we
68432176cfdSRui Paulo 		 * must not be preempted from here until after
68532176cfdSRui Paulo 		 * we (potentially) call ieee80211_crypto_demic;
68632176cfdSRui Paulo 		 * otherwise we may violate assumptions in the
68732176cfdSRui Paulo 		 * crypto cipher modules used to do delayed update
68832176cfdSRui Paulo 		 * of replay sequence numbers.
68932176cfdSRui Paulo 		 */
690085ff963SMatthew Dillon 		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
69132176cfdSRui Paulo 			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
69232176cfdSRui Paulo 				/*
69332176cfdSRui Paulo 				 * Discard encrypted frames when privacy is off.
69432176cfdSRui Paulo 				 */
69532176cfdSRui Paulo 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
69632176cfdSRui Paulo 				    wh, "WEP", "%s", "PRIVACY off");
69732176cfdSRui Paulo 				vap->iv_stats.is_rx_noprivacy++;
69832176cfdSRui Paulo 				IEEE80211_NODE_STAT(ni, rx_noprivacy);
69932176cfdSRui Paulo 				goto out;
70032176cfdSRui Paulo 			}
70132176cfdSRui Paulo 			key = ieee80211_crypto_decap(ni, m, hdrspace);
70232176cfdSRui Paulo 			if (key == NULL) {
70332176cfdSRui Paulo 				/* NB: stats+msgs handled in crypto_decap */
70432176cfdSRui Paulo 				IEEE80211_NODE_STAT(ni, rx_wepfail);
70532176cfdSRui Paulo 				goto out;
70632176cfdSRui Paulo 			}
70732176cfdSRui Paulo 			wh = mtod(m, struct ieee80211_frame *);
708085ff963SMatthew Dillon 			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
70932176cfdSRui Paulo 		} else {
71032176cfdSRui Paulo 			/* XXX M_WEP and IEEE80211_F_PRIVACY */
71132176cfdSRui Paulo 			key = NULL;
71232176cfdSRui Paulo 		}
71332176cfdSRui Paulo 
71432176cfdSRui Paulo 		/*
71532176cfdSRui Paulo 		 * Save QoS bits for use below--before we strip the header.
71632176cfdSRui Paulo 		 */
71732176cfdSRui Paulo 		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
71832176cfdSRui Paulo 			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
71932176cfdSRui Paulo 			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
72032176cfdSRui Paulo 			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
72132176cfdSRui Paulo 		} else
72232176cfdSRui Paulo 			qos = 0;
72332176cfdSRui Paulo 
72432176cfdSRui Paulo 		/*
72532176cfdSRui Paulo 		 * Next up, any fragmentation.
72632176cfdSRui Paulo 		 */
72732176cfdSRui Paulo 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
72832176cfdSRui Paulo 			m = ieee80211_defrag(ni, m, hdrspace);
72932176cfdSRui Paulo 			if (m == NULL) {
73032176cfdSRui Paulo 				/* Fragment dropped or frame not complete yet */
73132176cfdSRui Paulo 				goto out;
73232176cfdSRui Paulo 			}
73332176cfdSRui Paulo 		}
73432176cfdSRui Paulo 		wh = NULL;		/* no longer valid, catch any uses */
73532176cfdSRui Paulo 
73632176cfdSRui Paulo 		/*
73732176cfdSRui Paulo 		 * Next strip any MSDU crypto bits.
73832176cfdSRui Paulo 		 */
73932176cfdSRui Paulo 		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
74032176cfdSRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
74132176cfdSRui Paulo 			    ni->ni_macaddr, "data", "%s", "demic error");
74232176cfdSRui Paulo 			vap->iv_stats.is_rx_demicfail++;
74332176cfdSRui Paulo 			IEEE80211_NODE_STAT(ni, rx_demicfail);
74432176cfdSRui Paulo 			goto out;
74532176cfdSRui Paulo 		}
74632176cfdSRui Paulo 		/* copy to listener after decrypt */
74732176cfdSRui Paulo 		if (ieee80211_radiotap_active_vap(vap))
74832176cfdSRui Paulo 			ieee80211_radiotap_rx(vap, m);
74932176cfdSRui Paulo 		need_tap = 0;
75032176cfdSRui Paulo 		/*
75132176cfdSRui Paulo 		 * Finally, strip the 802.11 header.
75232176cfdSRui Paulo 		 */
75332176cfdSRui Paulo 		m = ieee80211_decap(vap, m, hdrspace);
75432176cfdSRui Paulo 		if (m == NULL) {
75532176cfdSRui Paulo 			/* XXX mask bit to check for both */
75632176cfdSRui Paulo 			/* don't count Null data frames as errors */
75732176cfdSRui Paulo 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
75832176cfdSRui Paulo 			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
75932176cfdSRui Paulo 				goto out;
76032176cfdSRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
76132176cfdSRui Paulo 			    ni->ni_macaddr, "data", "%s", "decap error");
76232176cfdSRui Paulo 			vap->iv_stats.is_rx_decap++;
76332176cfdSRui Paulo 			IEEE80211_NODE_STAT(ni, rx_decap);
76432176cfdSRui Paulo 			goto err;
76532176cfdSRui Paulo 		}
76632176cfdSRui Paulo 		eh = mtod(m, struct ether_header *);
76732176cfdSRui Paulo 		if (!ieee80211_node_is_authorized(ni)) {
76832176cfdSRui Paulo 			/*
76932176cfdSRui Paulo 			 * Deny any non-PAE frames received prior to
77032176cfdSRui Paulo 			 * authorization.  For open/shared-key
77132176cfdSRui Paulo 			 * authentication the port is mark authorized
77232176cfdSRui Paulo 			 * after authentication completes.  For 802.1x
77332176cfdSRui Paulo 			 * the port is not marked authorized by the
77432176cfdSRui Paulo 			 * authenticator until the handshake has completed.
77532176cfdSRui Paulo 			 */
77632176cfdSRui Paulo 			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
77732176cfdSRui Paulo 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
77832176cfdSRui Paulo 				    eh->ether_shost, "data",
77932176cfdSRui Paulo 				    "unauthorized port: ether type 0x%x len %u",
78032176cfdSRui Paulo 				    eh->ether_type, m->m_pkthdr.len);
78132176cfdSRui Paulo 				vap->iv_stats.is_rx_unauth++;
78232176cfdSRui Paulo 				IEEE80211_NODE_STAT(ni, rx_unauth);
78332176cfdSRui Paulo 				goto err;
78432176cfdSRui Paulo 			}
78532176cfdSRui Paulo 		} else {
78632176cfdSRui Paulo 			/*
78732176cfdSRui Paulo 			 * When denying unencrypted frames, discard
78832176cfdSRui Paulo 			 * any non-PAE frames received without encryption.
78932176cfdSRui Paulo 			 */
79032176cfdSRui Paulo 			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
79132176cfdSRui Paulo 			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
79232176cfdSRui Paulo 			    eh->ether_type != htons(ETHERTYPE_PAE)) {
79332176cfdSRui Paulo 				/*
79432176cfdSRui Paulo 				 * Drop unencrypted frames.
79532176cfdSRui Paulo 				 */
79632176cfdSRui Paulo 				vap->iv_stats.is_rx_unencrypted++;
79732176cfdSRui Paulo 				IEEE80211_NODE_STAT(ni, rx_unencrypted);
79832176cfdSRui Paulo 				goto out;
79932176cfdSRui Paulo 			}
80032176cfdSRui Paulo 		}
80132176cfdSRui Paulo 		/* XXX require HT? */
80232176cfdSRui Paulo 		if (qos & IEEE80211_QOS_AMSDU) {
80332176cfdSRui Paulo 			m = ieee80211_decap_amsdu(ni, m);
80432176cfdSRui Paulo 			if (m == NULL)
80532176cfdSRui Paulo 				return IEEE80211_FC0_TYPE_DATA;
80632176cfdSRui Paulo 		} else {
80732176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG
80832176cfdSRui Paulo 			m = ieee80211_decap_fastframe(vap, ni, m);
80932176cfdSRui Paulo 			if (m == NULL)
81032176cfdSRui Paulo 				return IEEE80211_FC0_TYPE_DATA;
81132176cfdSRui Paulo #endif
81232176cfdSRui Paulo 		}
81332176cfdSRui Paulo 		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
81432176cfdSRui Paulo 			ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
81532176cfdSRui Paulo 		else
81632176cfdSRui Paulo 			hostap_deliver_data(vap, ni, m);
81732176cfdSRui Paulo 		return IEEE80211_FC0_TYPE_DATA;
81832176cfdSRui Paulo 
81932176cfdSRui Paulo 	case IEEE80211_FC0_TYPE_MGT:
82032176cfdSRui Paulo 		vap->iv_stats.is_rx_mgmt++;
82132176cfdSRui Paulo 		IEEE80211_NODE_STAT(ni, rx_mgmt);
82232176cfdSRui Paulo 		if (dir != IEEE80211_FC1_DIR_NODS) {
82332176cfdSRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
82432176cfdSRui Paulo 			    wh, "mgt", "incorrect dir 0x%x", dir);
82532176cfdSRui Paulo 			vap->iv_stats.is_rx_wrongdir++;
82632176cfdSRui Paulo 			goto err;
82732176cfdSRui Paulo 		}
82832176cfdSRui Paulo 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
82932176cfdSRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
83032176cfdSRui Paulo 			    ni->ni_macaddr, "mgt", "too short: len %u",
83132176cfdSRui Paulo 			    m->m_pkthdr.len);
83232176cfdSRui Paulo 			vap->iv_stats.is_rx_tooshort++;
83332176cfdSRui Paulo 			goto out;
83432176cfdSRui Paulo 		}
83532176cfdSRui Paulo 		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
83632176cfdSRui Paulo 			/* ensure return frames are unicast */
83732176cfdSRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
8381e290df3SAntonio Huete Jimenez 			    wh, NULL, "source is multicast: %s",
839085ff963SMatthew Dillon 			    ether_sprintf(wh->i_addr2));
84032176cfdSRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
84132176cfdSRui Paulo 			goto out;
84232176cfdSRui Paulo 		}
84332176cfdSRui Paulo #ifdef IEEE80211_DEBUG
84432176cfdSRui Paulo 		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
84532176cfdSRui Paulo 		    ieee80211_msg_dumppkts(vap)) {
8461e290df3SAntonio Huete Jimenez 			if_printf(ifp, "received %s from %s rssi %d\n",
847*4f655ef5SMatthew Dillon 			    ieee80211_mgt_subtype_name(subtype),
848085ff963SMatthew Dillon 			    ether_sprintf(wh->i_addr2), rssi);
84932176cfdSRui Paulo 		}
85032176cfdSRui Paulo #endif
851085ff963SMatthew Dillon 		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
85232176cfdSRui Paulo 			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
85332176cfdSRui Paulo 				/*
85432176cfdSRui Paulo 				 * Only shared key auth frames with a challenge
85532176cfdSRui Paulo 				 * should be encrypted, discard all others.
85632176cfdSRui Paulo 				 */
85732176cfdSRui Paulo 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
85832176cfdSRui Paulo 				    wh, NULL,
85932176cfdSRui Paulo 				    "%s", "WEP set but not permitted");
86032176cfdSRui Paulo 				vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
86132176cfdSRui Paulo 				goto out;
86232176cfdSRui Paulo 			}
86332176cfdSRui Paulo 			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
86432176cfdSRui Paulo 				/*
86532176cfdSRui Paulo 				 * Discard encrypted frames when privacy is off.
86632176cfdSRui Paulo 				 */
86732176cfdSRui Paulo 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
86832176cfdSRui Paulo 				    wh, NULL, "%s", "WEP set but PRIVACY off");
86932176cfdSRui Paulo 				vap->iv_stats.is_rx_noprivacy++;
87032176cfdSRui Paulo 				goto out;
87132176cfdSRui Paulo 			}
87232176cfdSRui Paulo 			hdrspace = ieee80211_hdrspace(ic, wh);
87332176cfdSRui Paulo 			key = ieee80211_crypto_decap(ni, m, hdrspace);
87432176cfdSRui Paulo 			if (key == NULL) {
87532176cfdSRui Paulo 				/* NB: stats+msgs handled in crypto_decap */
87632176cfdSRui Paulo 				goto out;
87732176cfdSRui Paulo 			}
87832176cfdSRui Paulo 			wh = mtod(m, struct ieee80211_frame *);
879085ff963SMatthew Dillon 			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
88032176cfdSRui Paulo 		}
881085ff963SMatthew Dillon 		/*
882085ff963SMatthew Dillon 		 * Pass the packet to radiotap before calling iv_recv_mgmt().
883085ff963SMatthew Dillon 		 * Otherwise iv_recv_mgmt() might pass another packet to
884085ff963SMatthew Dillon 		 * radiotap, resulting in out of order packet captures.
885085ff963SMatthew Dillon 		 */
886085ff963SMatthew Dillon 		if (ieee80211_radiotap_active_vap(vap))
887085ff963SMatthew Dillon 			ieee80211_radiotap_rx(vap, m);
888085ff963SMatthew Dillon 		need_tap = 0;
8894f898719SImre Vadász 		vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
89032176cfdSRui Paulo 		goto out;
89132176cfdSRui Paulo 
89232176cfdSRui Paulo 	case IEEE80211_FC0_TYPE_CTL:
89332176cfdSRui Paulo 		vap->iv_stats.is_rx_ctl++;
89432176cfdSRui Paulo 		IEEE80211_NODE_STAT(ni, rx_ctrl);
89532176cfdSRui Paulo 		vap->iv_recv_ctl(ni, m, subtype);
89632176cfdSRui Paulo 		goto out;
89732176cfdSRui Paulo 	default:
89832176cfdSRui Paulo 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
89932176cfdSRui Paulo 		    wh, "bad", "frame type 0x%x", type);
90032176cfdSRui Paulo 		/* should not come here */
90132176cfdSRui Paulo 		break;
90232176cfdSRui Paulo 	}
90332176cfdSRui Paulo err:
904*4f655ef5SMatthew Dillon 	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
90532176cfdSRui Paulo out:
90632176cfdSRui Paulo 	if (m != NULL) {
90732176cfdSRui Paulo 		if (need_tap && ieee80211_radiotap_active_vap(vap))
90832176cfdSRui Paulo 			ieee80211_radiotap_rx(vap, m);
90932176cfdSRui Paulo 		m_freem(m);
91032176cfdSRui Paulo 	}
91132176cfdSRui Paulo 	return type;
91232176cfdSRui Paulo }
91332176cfdSRui Paulo 
91432176cfdSRui Paulo static void
hostap_auth_open(struct ieee80211_node * ni,struct ieee80211_frame * wh,int rssi,int nf,uint16_t seq,uint16_t status)91532176cfdSRui Paulo hostap_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
91632176cfdSRui Paulo     int rssi, int nf, uint16_t seq, uint16_t status)
91732176cfdSRui Paulo {
91832176cfdSRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
91932176cfdSRui Paulo 
92032176cfdSRui Paulo 	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
92132176cfdSRui Paulo 
92232176cfdSRui Paulo 	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
92332176cfdSRui Paulo 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
92432176cfdSRui Paulo 		    ni->ni_macaddr, "open auth",
92532176cfdSRui Paulo 		    "bad sta auth mode %u", ni->ni_authmode);
92632176cfdSRui Paulo 		vap->iv_stats.is_rx_bad_auth++;	/* XXX */
92732176cfdSRui Paulo 		/*
92832176cfdSRui Paulo 		 * Clear any challenge text that may be there if
92932176cfdSRui Paulo 		 * a previous shared key auth failed and then an
93032176cfdSRui Paulo 		 * open auth is attempted.
93132176cfdSRui Paulo 		 */
93232176cfdSRui Paulo 		if (ni->ni_challenge != NULL) {
933*4f655ef5SMatthew Dillon 			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
93432176cfdSRui Paulo 			ni->ni_challenge = NULL;
93532176cfdSRui Paulo 		}
93632176cfdSRui Paulo 		/* XXX hack to workaround calling convention */
93732176cfdSRui Paulo 		ieee80211_send_error(ni, wh->i_addr2,
93832176cfdSRui Paulo 		    IEEE80211_FC0_SUBTYPE_AUTH,
93932176cfdSRui Paulo 		    (seq + 1) | (IEEE80211_STATUS_ALG<<16));
94032176cfdSRui Paulo 		return;
94132176cfdSRui Paulo 	}
94232176cfdSRui Paulo 	if (seq != IEEE80211_AUTH_OPEN_REQUEST) {
94332176cfdSRui Paulo 		vap->iv_stats.is_rx_bad_auth++;
94432176cfdSRui Paulo 		return;
94532176cfdSRui Paulo 	}
94632176cfdSRui Paulo 	/* always accept open authentication requests */
94732176cfdSRui Paulo 	if (ni == vap->iv_bss) {
94832176cfdSRui Paulo 		ni = ieee80211_dup_bss(vap, wh->i_addr2);
94932176cfdSRui Paulo 		if (ni == NULL)
95032176cfdSRui Paulo 			return;
95132176cfdSRui Paulo 	} else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
95232176cfdSRui Paulo 		(void) ieee80211_ref_node(ni);
95332176cfdSRui Paulo 	/*
95432176cfdSRui Paulo 	 * Mark the node as referenced to reflect that it's
95532176cfdSRui Paulo 	 * reference count has been bumped to insure it remains
95632176cfdSRui Paulo 	 * after the transaction completes.
95732176cfdSRui Paulo 	 */
95832176cfdSRui Paulo 	ni->ni_flags |= IEEE80211_NODE_AREF;
95932176cfdSRui Paulo 	/*
96032176cfdSRui Paulo 	 * Mark the node as requiring a valid association id
96132176cfdSRui Paulo 	 * before outbound traffic is permitted.
96232176cfdSRui Paulo 	 */
96332176cfdSRui Paulo 	ni->ni_flags |= IEEE80211_NODE_ASSOCID;
96432176cfdSRui Paulo 
96532176cfdSRui Paulo 	if (vap->iv_acl != NULL &&
96632176cfdSRui Paulo 	    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
96732176cfdSRui Paulo 		/*
96832176cfdSRui Paulo 		 * When the ACL policy is set to RADIUS we defer the
96932176cfdSRui Paulo 		 * authorization to a user agent.  Dispatch an event,
97032176cfdSRui Paulo 		 * a subsequent MLME call will decide the fate of the
97132176cfdSRui Paulo 		 * station.  If the user agent is not present then the
97232176cfdSRui Paulo 		 * node will be reclaimed due to inactivity.
97332176cfdSRui Paulo 		 */
97432176cfdSRui Paulo 		IEEE80211_NOTE_MAC(vap,
97532176cfdSRui Paulo 		    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL, ni->ni_macaddr,
97632176cfdSRui Paulo 		    "%s", "station authentication defered (radius acl)");
97732176cfdSRui Paulo 		ieee80211_notify_node_auth(ni);
97832176cfdSRui Paulo 	} else {
97932176cfdSRui Paulo 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
98032176cfdSRui Paulo 		IEEE80211_NOTE_MAC(vap,
98132176cfdSRui Paulo 		    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni->ni_macaddr,
98232176cfdSRui Paulo 		    "%s", "station authenticated (open)");
98332176cfdSRui Paulo 		/*
98432176cfdSRui Paulo 		 * When 802.1x is not in use mark the port
98532176cfdSRui Paulo 		 * authorized at this point so traffic can flow.
98632176cfdSRui Paulo 		 */
98732176cfdSRui Paulo 		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
98832176cfdSRui Paulo 			ieee80211_node_authorize(ni);
98932176cfdSRui Paulo 	}
99032176cfdSRui Paulo }
99132176cfdSRui Paulo 
99232176cfdSRui Paulo static void
hostap_auth_shared(struct ieee80211_node * ni,struct ieee80211_frame * wh,uint8_t * frm,uint8_t * efrm,int rssi,int nf,uint16_t seq,uint16_t status)99332176cfdSRui Paulo hostap_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
99432176cfdSRui Paulo     uint8_t *frm, uint8_t *efrm, int rssi, int nf,
99532176cfdSRui Paulo     uint16_t seq, uint16_t status)
99632176cfdSRui Paulo {
99732176cfdSRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
99832176cfdSRui Paulo 	uint8_t *challenge;
999085ff963SMatthew Dillon 	int allocbs, estatus;
100032176cfdSRui Paulo 
100132176cfdSRui Paulo 	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
100232176cfdSRui Paulo 
100332176cfdSRui Paulo 	/*
100432176cfdSRui Paulo 	 * NB: this can happen as we allow pre-shared key
100532176cfdSRui Paulo 	 * authentication to be enabled w/o wep being turned
100632176cfdSRui Paulo 	 * on so that configuration of these can be done
100732176cfdSRui Paulo 	 * in any order.  It may be better to enforce the
100832176cfdSRui Paulo 	 * ordering in which case this check would just be
100932176cfdSRui Paulo 	 * for sanity/consistency.
101032176cfdSRui Paulo 	 */
101132176cfdSRui Paulo 	if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
101232176cfdSRui Paulo 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
101332176cfdSRui Paulo 		    ni->ni_macaddr, "shared key auth",
101432176cfdSRui Paulo 		    "%s", " PRIVACY is disabled");
101532176cfdSRui Paulo 		estatus = IEEE80211_STATUS_ALG;
101632176cfdSRui Paulo 		goto bad;
101732176cfdSRui Paulo 	}
101832176cfdSRui Paulo 	/*
101932176cfdSRui Paulo 	 * Pre-shared key authentication is evil; accept
102032176cfdSRui Paulo 	 * it only if explicitly configured (it is supported
102132176cfdSRui Paulo 	 * mainly for compatibility with clients like Mac OS X).
102232176cfdSRui Paulo 	 */
102332176cfdSRui Paulo 	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
102432176cfdSRui Paulo 	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
102532176cfdSRui Paulo 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
102632176cfdSRui Paulo 		    ni->ni_macaddr, "shared key auth",
102732176cfdSRui Paulo 		    "bad sta auth mode %u", ni->ni_authmode);
102832176cfdSRui Paulo 		vap->iv_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
102932176cfdSRui Paulo 		estatus = IEEE80211_STATUS_ALG;
103032176cfdSRui Paulo 		goto bad;
103132176cfdSRui Paulo 	}
103232176cfdSRui Paulo 
103332176cfdSRui Paulo 	challenge = NULL;
103432176cfdSRui Paulo 	if (frm + 1 < efrm) {
103532176cfdSRui Paulo 		if ((frm[1] + 2) > (efrm - frm)) {
103632176cfdSRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
103732176cfdSRui Paulo 			    ni->ni_macaddr, "shared key auth",
10382c7ccc4aSSascha Wildner 			    "ie %d/%ld too long",
1039085ff963SMatthew Dillon 			    frm[0], (frm[1] + 2) - (efrm - frm));
104032176cfdSRui Paulo 			vap->iv_stats.is_rx_bad_auth++;
104132176cfdSRui Paulo 			estatus = IEEE80211_STATUS_CHALLENGE;
104232176cfdSRui Paulo 			goto bad;
104332176cfdSRui Paulo 		}
104432176cfdSRui Paulo 		if (*frm == IEEE80211_ELEMID_CHALLENGE)
104532176cfdSRui Paulo 			challenge = frm;
104632176cfdSRui Paulo 		frm += frm[1] + 2;
104732176cfdSRui Paulo 	}
104832176cfdSRui Paulo 	switch (seq) {
104932176cfdSRui Paulo 	case IEEE80211_AUTH_SHARED_CHALLENGE:
105032176cfdSRui Paulo 	case IEEE80211_AUTH_SHARED_RESPONSE:
105132176cfdSRui Paulo 		if (challenge == NULL) {
105232176cfdSRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
105332176cfdSRui Paulo 			    ni->ni_macaddr, "shared key auth",
105432176cfdSRui Paulo 			    "%s", "no challenge");
105532176cfdSRui Paulo 			vap->iv_stats.is_rx_bad_auth++;
105632176cfdSRui Paulo 			estatus = IEEE80211_STATUS_CHALLENGE;
105732176cfdSRui Paulo 			goto bad;
105832176cfdSRui Paulo 		}
105932176cfdSRui Paulo 		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
106032176cfdSRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
106132176cfdSRui Paulo 			    ni->ni_macaddr, "shared key auth",
106232176cfdSRui Paulo 			    "bad challenge len %d", challenge[1]);
106332176cfdSRui Paulo 			vap->iv_stats.is_rx_bad_auth++;
106432176cfdSRui Paulo 			estatus = IEEE80211_STATUS_CHALLENGE;
106532176cfdSRui Paulo 			goto bad;
106632176cfdSRui Paulo 		}
106732176cfdSRui Paulo 	default:
106832176cfdSRui Paulo 		break;
106932176cfdSRui Paulo 	}
107032176cfdSRui Paulo 	switch (seq) {
107132176cfdSRui Paulo 	case IEEE80211_AUTH_SHARED_REQUEST:
107232176cfdSRui Paulo 		if (ni == vap->iv_bss) {
107332176cfdSRui Paulo 			ni = ieee80211_dup_bss(vap, wh->i_addr2);
107432176cfdSRui Paulo 			if (ni == NULL) {
107532176cfdSRui Paulo 				/* NB: no way to return an error */
107632176cfdSRui Paulo 				return;
107732176cfdSRui Paulo 			}
107832176cfdSRui Paulo 			allocbs = 1;
107932176cfdSRui Paulo 		} else {
108032176cfdSRui Paulo 			if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
108132176cfdSRui Paulo 				(void) ieee80211_ref_node(ni);
108232176cfdSRui Paulo 			allocbs = 0;
108332176cfdSRui Paulo 		}
108432176cfdSRui Paulo 		/*
108532176cfdSRui Paulo 		 * Mark the node as referenced to reflect that it's
108632176cfdSRui Paulo 		 * reference count has been bumped to insure it remains
108732176cfdSRui Paulo 		 * after the transaction completes.
108832176cfdSRui Paulo 		 */
108932176cfdSRui Paulo 		ni->ni_flags |= IEEE80211_NODE_AREF;
109032176cfdSRui Paulo 		/*
109132176cfdSRui Paulo 		 * Mark the node as requiring a valid associatio id
109232176cfdSRui Paulo 		 * before outbound traffic is permitted.
109332176cfdSRui Paulo 		 */
109432176cfdSRui Paulo 		ni->ni_flags |= IEEE80211_NODE_ASSOCID;
109532176cfdSRui Paulo 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
109632176cfdSRui Paulo 		ni->ni_noise = nf;
109732176cfdSRui Paulo 		if (!ieee80211_alloc_challenge(ni)) {
109832176cfdSRui Paulo 			/* NB: don't return error so they rexmit */
109932176cfdSRui Paulo 			return;
110032176cfdSRui Paulo 		}
110132176cfdSRui Paulo 		get_random_bytes(ni->ni_challenge,
110232176cfdSRui Paulo 			IEEE80211_CHALLENGE_LEN);
110332176cfdSRui Paulo 		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
110432176cfdSRui Paulo 		    ni, "shared key %sauth request", allocbs ? "" : "re");
110532176cfdSRui Paulo 		/*
110632176cfdSRui Paulo 		 * When the ACL policy is set to RADIUS we defer the
110732176cfdSRui Paulo 		 * authorization to a user agent.  Dispatch an event,
110832176cfdSRui Paulo 		 * a subsequent MLME call will decide the fate of the
110932176cfdSRui Paulo 		 * station.  If the user agent is not present then the
111032176cfdSRui Paulo 		 * node will be reclaimed due to inactivity.
111132176cfdSRui Paulo 		 */
111232176cfdSRui Paulo 		if (vap->iv_acl != NULL &&
111332176cfdSRui Paulo 		    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
111432176cfdSRui Paulo 			IEEE80211_NOTE_MAC(vap,
111532176cfdSRui Paulo 			    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL,
111632176cfdSRui Paulo 			    ni->ni_macaddr,
111732176cfdSRui Paulo 			    "%s", "station authentication defered (radius acl)");
111832176cfdSRui Paulo 			ieee80211_notify_node_auth(ni);
111932176cfdSRui Paulo 			return;
112032176cfdSRui Paulo 		}
112132176cfdSRui Paulo 		break;
112232176cfdSRui Paulo 	case IEEE80211_AUTH_SHARED_RESPONSE:
112332176cfdSRui Paulo 		if (ni == vap->iv_bss) {
112432176cfdSRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
112532176cfdSRui Paulo 			    ni->ni_macaddr, "shared key response",
112632176cfdSRui Paulo 			    "%s", "unknown station");
112732176cfdSRui Paulo 			/* NB: don't send a response */
112832176cfdSRui Paulo 			return;
112932176cfdSRui Paulo 		}
113032176cfdSRui Paulo 		if (ni->ni_challenge == NULL) {
113132176cfdSRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
113232176cfdSRui Paulo 			    ni->ni_macaddr, "shared key response",
113332176cfdSRui Paulo 			    "%s", "no challenge recorded");
113432176cfdSRui Paulo 			vap->iv_stats.is_rx_bad_auth++;
113532176cfdSRui Paulo 			estatus = IEEE80211_STATUS_CHALLENGE;
113632176cfdSRui Paulo 			goto bad;
113732176cfdSRui Paulo 		}
113832176cfdSRui Paulo 		if (memcmp(ni->ni_challenge, &challenge[2],
113932176cfdSRui Paulo 			   challenge[1]) != 0) {
114032176cfdSRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
114132176cfdSRui Paulo 			    ni->ni_macaddr, "shared key response",
114232176cfdSRui Paulo 			    "%s", "challenge mismatch");
114332176cfdSRui Paulo 			vap->iv_stats.is_rx_auth_fail++;
114432176cfdSRui Paulo 			estatus = IEEE80211_STATUS_CHALLENGE;
114532176cfdSRui Paulo 			goto bad;
114632176cfdSRui Paulo 		}
114732176cfdSRui Paulo 		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
114832176cfdSRui Paulo 		    ni, "%s", "station authenticated (shared key)");
114932176cfdSRui Paulo 		ieee80211_node_authorize(ni);
115032176cfdSRui Paulo 		break;
115132176cfdSRui Paulo 	default:
115232176cfdSRui Paulo 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
115332176cfdSRui Paulo 		    ni->ni_macaddr, "shared key auth",
115432176cfdSRui Paulo 		    "bad seq %d", seq);
115532176cfdSRui Paulo 		vap->iv_stats.is_rx_bad_auth++;
115632176cfdSRui Paulo 		estatus = IEEE80211_STATUS_SEQUENCE;
115732176cfdSRui Paulo 		goto bad;
115832176cfdSRui Paulo 	}
115932176cfdSRui Paulo 	IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
116032176cfdSRui Paulo 	return;
116132176cfdSRui Paulo bad:
116232176cfdSRui Paulo 	/*
116332176cfdSRui Paulo 	 * Send an error response; but only when operating as an AP.
116432176cfdSRui Paulo 	 */
116532176cfdSRui Paulo 	/* XXX hack to workaround calling convention */
116632176cfdSRui Paulo 	ieee80211_send_error(ni, wh->i_addr2,
116732176cfdSRui Paulo 	    IEEE80211_FC0_SUBTYPE_AUTH,
116832176cfdSRui Paulo 	    (seq + 1) | (estatus<<16));
116932176cfdSRui Paulo }
117032176cfdSRui Paulo 
117132176cfdSRui Paulo /*
117232176cfdSRui Paulo  * Convert a WPA cipher selector OUI to an internal
117332176cfdSRui Paulo  * cipher algorithm.  Where appropriate we also
117432176cfdSRui Paulo  * record any key length.
117532176cfdSRui Paulo  */
117632176cfdSRui Paulo static int
wpa_cipher(const uint8_t * sel,uint8_t * keylen)117732176cfdSRui Paulo wpa_cipher(const uint8_t *sel, uint8_t *keylen)
117832176cfdSRui Paulo {
117932176cfdSRui Paulo #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1180*4f655ef5SMatthew Dillon 	uint32_t w = le32dec(sel);
118132176cfdSRui Paulo 
118232176cfdSRui Paulo 	switch (w) {
118332176cfdSRui Paulo 	case WPA_SEL(WPA_CSE_NULL):
118432176cfdSRui Paulo 		return IEEE80211_CIPHER_NONE;
118532176cfdSRui Paulo 	case WPA_SEL(WPA_CSE_WEP40):
118632176cfdSRui Paulo 		if (keylen)
118732176cfdSRui Paulo 			*keylen = 40 / NBBY;
118832176cfdSRui Paulo 		return IEEE80211_CIPHER_WEP;
118932176cfdSRui Paulo 	case WPA_SEL(WPA_CSE_WEP104):
119032176cfdSRui Paulo 		if (keylen)
119132176cfdSRui Paulo 			*keylen = 104 / NBBY;
119232176cfdSRui Paulo 		return IEEE80211_CIPHER_WEP;
119332176cfdSRui Paulo 	case WPA_SEL(WPA_CSE_TKIP):
119432176cfdSRui Paulo 		return IEEE80211_CIPHER_TKIP;
119532176cfdSRui Paulo 	case WPA_SEL(WPA_CSE_CCMP):
119632176cfdSRui Paulo 		return IEEE80211_CIPHER_AES_CCM;
119732176cfdSRui Paulo 	}
119832176cfdSRui Paulo 	return 32;		/* NB: so 1<< is discarded */
119932176cfdSRui Paulo #undef WPA_SEL
120032176cfdSRui Paulo }
120132176cfdSRui Paulo 
120232176cfdSRui Paulo /*
120332176cfdSRui Paulo  * Convert a WPA key management/authentication algorithm
120432176cfdSRui Paulo  * to an internal code.
120532176cfdSRui Paulo  */
120632176cfdSRui Paulo static int
wpa_keymgmt(const uint8_t * sel)120732176cfdSRui Paulo wpa_keymgmt(const uint8_t *sel)
120832176cfdSRui Paulo {
120932176cfdSRui Paulo #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1210*4f655ef5SMatthew Dillon 	uint32_t w = le32dec(sel);
121132176cfdSRui Paulo 
121232176cfdSRui Paulo 	switch (w) {
121332176cfdSRui Paulo 	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
121432176cfdSRui Paulo 		return WPA_ASE_8021X_UNSPEC;
121532176cfdSRui Paulo 	case WPA_SEL(WPA_ASE_8021X_PSK):
121632176cfdSRui Paulo 		return WPA_ASE_8021X_PSK;
121732176cfdSRui Paulo 	case WPA_SEL(WPA_ASE_NONE):
121832176cfdSRui Paulo 		return WPA_ASE_NONE;
121932176cfdSRui Paulo 	}
122032176cfdSRui Paulo 	return 0;		/* NB: so is discarded */
122132176cfdSRui Paulo #undef WPA_SEL
122232176cfdSRui Paulo }
122332176cfdSRui Paulo 
122432176cfdSRui Paulo /*
122532176cfdSRui Paulo  * Parse a WPA information element to collect parameters.
122632176cfdSRui Paulo  * Note that we do not validate security parameters; that
122732176cfdSRui Paulo  * is handled by the authenticator; the parsing done here
122832176cfdSRui Paulo  * is just for internal use in making operational decisions.
122932176cfdSRui Paulo  */
123032176cfdSRui Paulo static int
ieee80211_parse_wpa(struct ieee80211vap * vap,const uint8_t * frm,struct ieee80211_rsnparms * rsn,const struct ieee80211_frame * wh)123132176cfdSRui Paulo ieee80211_parse_wpa(struct ieee80211vap *vap, const uint8_t *frm,
123232176cfdSRui Paulo 	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
123332176cfdSRui Paulo {
123432176cfdSRui Paulo 	uint8_t len = frm[1];
123532176cfdSRui Paulo 	uint32_t w;
123632176cfdSRui Paulo 	int n;
123732176cfdSRui Paulo 
123832176cfdSRui Paulo 	/*
123932176cfdSRui Paulo 	 * Check the length once for fixed parts: OUI, type,
124032176cfdSRui Paulo 	 * version, mcast cipher, and 2 selector counts.
124132176cfdSRui Paulo 	 * Other, variable-length data, must be checked separately.
124232176cfdSRui Paulo 	 */
124332176cfdSRui Paulo 	if ((vap->iv_flags & IEEE80211_F_WPA1) == 0) {
124432176cfdSRui Paulo 		IEEE80211_DISCARD_IE(vap,
124532176cfdSRui Paulo 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
124632176cfdSRui Paulo 		    wh, "WPA", "not WPA, flags 0x%x", vap->iv_flags);
124732176cfdSRui Paulo 		return IEEE80211_REASON_IE_INVALID;
124832176cfdSRui Paulo 	}
124932176cfdSRui Paulo 	if (len < 14) {
125032176cfdSRui Paulo 		IEEE80211_DISCARD_IE(vap,
125132176cfdSRui Paulo 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
125232176cfdSRui Paulo 		    wh, "WPA", "too short, len %u", len);
125332176cfdSRui Paulo 		return IEEE80211_REASON_IE_INVALID;
125432176cfdSRui Paulo 	}
125532176cfdSRui Paulo 	frm += 6, len -= 4;		/* NB: len is payload only */
125632176cfdSRui Paulo 	/* NB: iswpaoui already validated the OUI and type */
1257*4f655ef5SMatthew Dillon 	w = le16dec(frm);
125832176cfdSRui Paulo 	if (w != WPA_VERSION) {
125932176cfdSRui Paulo 		IEEE80211_DISCARD_IE(vap,
126032176cfdSRui Paulo 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
126132176cfdSRui Paulo 		    wh, "WPA", "bad version %u", w);
126232176cfdSRui Paulo 		return IEEE80211_REASON_IE_INVALID;
126332176cfdSRui Paulo 	}
126432176cfdSRui Paulo 	frm += 2, len -= 2;
126532176cfdSRui Paulo 
126632176cfdSRui Paulo 	memset(rsn, 0, sizeof(*rsn));
126732176cfdSRui Paulo 
126832176cfdSRui Paulo 	/* multicast/group cipher */
126932176cfdSRui Paulo 	rsn->rsn_mcastcipher = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
127032176cfdSRui Paulo 	frm += 4, len -= 4;
127132176cfdSRui Paulo 
127232176cfdSRui Paulo 	/* unicast ciphers */
1273*4f655ef5SMatthew Dillon 	n = le16dec(frm);
127432176cfdSRui Paulo 	frm += 2, len -= 2;
127532176cfdSRui Paulo 	if (len < n*4+2) {
127632176cfdSRui Paulo 		IEEE80211_DISCARD_IE(vap,
127732176cfdSRui Paulo 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
127832176cfdSRui Paulo 		    wh, "WPA", "ucast cipher data too short; len %u, n %u",
127932176cfdSRui Paulo 		    len, n);
128032176cfdSRui Paulo 		return IEEE80211_REASON_IE_INVALID;
128132176cfdSRui Paulo 	}
128232176cfdSRui Paulo 	w = 0;
128332176cfdSRui Paulo 	for (; n > 0; n--) {
128432176cfdSRui Paulo 		w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
128532176cfdSRui Paulo 		frm += 4, len -= 4;
128632176cfdSRui Paulo 	}
128732176cfdSRui Paulo 	if (w & (1<<IEEE80211_CIPHER_TKIP))
128832176cfdSRui Paulo 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
128932176cfdSRui Paulo 	else
129032176cfdSRui Paulo 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
129132176cfdSRui Paulo 
129232176cfdSRui Paulo 	/* key management algorithms */
1293*4f655ef5SMatthew Dillon 	n = le16dec(frm);
129432176cfdSRui Paulo 	frm += 2, len -= 2;
129532176cfdSRui Paulo 	if (len < n*4) {
129632176cfdSRui Paulo 		IEEE80211_DISCARD_IE(vap,
129732176cfdSRui Paulo 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
129832176cfdSRui Paulo 		    wh, "WPA", "key mgmt alg data too short; len %u, n %u",
129932176cfdSRui Paulo 		    len, n);
130032176cfdSRui Paulo 		return IEEE80211_REASON_IE_INVALID;
130132176cfdSRui Paulo 	}
130232176cfdSRui Paulo 	w = 0;
130332176cfdSRui Paulo 	for (; n > 0; n--) {
130432176cfdSRui Paulo 		w |= wpa_keymgmt(frm);
130532176cfdSRui Paulo 		frm += 4, len -= 4;
130632176cfdSRui Paulo 	}
130732176cfdSRui Paulo 	if (w & WPA_ASE_8021X_UNSPEC)
130832176cfdSRui Paulo 		rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
130932176cfdSRui Paulo 	else
131032176cfdSRui Paulo 		rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
131132176cfdSRui Paulo 
131232176cfdSRui Paulo 	if (len > 2)		/* optional capabilities */
1313*4f655ef5SMatthew Dillon 		rsn->rsn_caps = le16dec(frm);
131432176cfdSRui Paulo 
131532176cfdSRui Paulo 	return 0;
131632176cfdSRui Paulo }
131732176cfdSRui Paulo 
131832176cfdSRui Paulo /*
131932176cfdSRui Paulo  * Convert an RSN cipher selector OUI to an internal
132032176cfdSRui Paulo  * cipher algorithm.  Where appropriate we also
132132176cfdSRui Paulo  * record any key length.
132232176cfdSRui Paulo  */
132332176cfdSRui Paulo static int
rsn_cipher(const uint8_t * sel,uint8_t * keylen)132432176cfdSRui Paulo rsn_cipher(const uint8_t *sel, uint8_t *keylen)
132532176cfdSRui Paulo {
132632176cfdSRui Paulo #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1327*4f655ef5SMatthew Dillon 	uint32_t w = le32dec(sel);
132832176cfdSRui Paulo 
132932176cfdSRui Paulo 	switch (w) {
133032176cfdSRui Paulo 	case RSN_SEL(RSN_CSE_NULL):
133132176cfdSRui Paulo 		return IEEE80211_CIPHER_NONE;
133232176cfdSRui Paulo 	case RSN_SEL(RSN_CSE_WEP40):
133332176cfdSRui Paulo 		if (keylen)
133432176cfdSRui Paulo 			*keylen = 40 / NBBY;
133532176cfdSRui Paulo 		return IEEE80211_CIPHER_WEP;
133632176cfdSRui Paulo 	case RSN_SEL(RSN_CSE_WEP104):
133732176cfdSRui Paulo 		if (keylen)
133832176cfdSRui Paulo 			*keylen = 104 / NBBY;
133932176cfdSRui Paulo 		return IEEE80211_CIPHER_WEP;
134032176cfdSRui Paulo 	case RSN_SEL(RSN_CSE_TKIP):
134132176cfdSRui Paulo 		return IEEE80211_CIPHER_TKIP;
134232176cfdSRui Paulo 	case RSN_SEL(RSN_CSE_CCMP):
134332176cfdSRui Paulo 		return IEEE80211_CIPHER_AES_CCM;
134432176cfdSRui Paulo 	case RSN_SEL(RSN_CSE_WRAP):
134532176cfdSRui Paulo 		return IEEE80211_CIPHER_AES_OCB;
134632176cfdSRui Paulo 	}
134732176cfdSRui Paulo 	return 32;		/* NB: so 1<< is discarded */
134832176cfdSRui Paulo #undef WPA_SEL
134932176cfdSRui Paulo }
135032176cfdSRui Paulo 
135132176cfdSRui Paulo /*
135232176cfdSRui Paulo  * Convert an RSN key management/authentication algorithm
135332176cfdSRui Paulo  * to an internal code.
135432176cfdSRui Paulo  */
135532176cfdSRui Paulo static int
rsn_keymgmt(const uint8_t * sel)135632176cfdSRui Paulo rsn_keymgmt(const uint8_t *sel)
135732176cfdSRui Paulo {
135832176cfdSRui Paulo #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1359*4f655ef5SMatthew Dillon 	uint32_t w = le32dec(sel);
136032176cfdSRui Paulo 
136132176cfdSRui Paulo 	switch (w) {
136232176cfdSRui Paulo 	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
136332176cfdSRui Paulo 		return RSN_ASE_8021X_UNSPEC;
136432176cfdSRui Paulo 	case RSN_SEL(RSN_ASE_8021X_PSK):
136532176cfdSRui Paulo 		return RSN_ASE_8021X_PSK;
136632176cfdSRui Paulo 	case RSN_SEL(RSN_ASE_NONE):
136732176cfdSRui Paulo 		return RSN_ASE_NONE;
136832176cfdSRui Paulo 	}
136932176cfdSRui Paulo 	return 0;		/* NB: so is discarded */
137032176cfdSRui Paulo #undef RSN_SEL
137132176cfdSRui Paulo }
137232176cfdSRui Paulo 
137332176cfdSRui Paulo /*
137432176cfdSRui Paulo  * Parse a WPA/RSN information element to collect parameters
137532176cfdSRui Paulo  * and validate the parameters against what has been
137632176cfdSRui Paulo  * configured for the system.
137732176cfdSRui Paulo  */
137832176cfdSRui Paulo static int
ieee80211_parse_rsn(struct ieee80211vap * vap,const uint8_t * frm,struct ieee80211_rsnparms * rsn,const struct ieee80211_frame * wh)137932176cfdSRui Paulo ieee80211_parse_rsn(struct ieee80211vap *vap, const uint8_t *frm,
138032176cfdSRui Paulo 	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
138132176cfdSRui Paulo {
138232176cfdSRui Paulo 	uint8_t len = frm[1];
138332176cfdSRui Paulo 	uint32_t w;
138432176cfdSRui Paulo 	int n;
138532176cfdSRui Paulo 
138632176cfdSRui Paulo 	/*
138732176cfdSRui Paulo 	 * Check the length once for fixed parts:
138832176cfdSRui Paulo 	 * version, mcast cipher, and 2 selector counts.
138932176cfdSRui Paulo 	 * Other, variable-length data, must be checked separately.
139032176cfdSRui Paulo 	 */
139132176cfdSRui Paulo 	if ((vap->iv_flags & IEEE80211_F_WPA2) == 0) {
139232176cfdSRui Paulo 		IEEE80211_DISCARD_IE(vap,
139332176cfdSRui Paulo 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
139432176cfdSRui Paulo 		    wh, "WPA", "not RSN, flags 0x%x", vap->iv_flags);
139532176cfdSRui Paulo 		return IEEE80211_REASON_IE_INVALID;
139632176cfdSRui Paulo 	}
139732176cfdSRui Paulo 	if (len < 10) {
139832176cfdSRui Paulo 		IEEE80211_DISCARD_IE(vap,
139932176cfdSRui Paulo 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
140032176cfdSRui Paulo 		    wh, "RSN", "too short, len %u", len);
140132176cfdSRui Paulo 		return IEEE80211_REASON_IE_INVALID;
140232176cfdSRui Paulo 	}
140332176cfdSRui Paulo 	frm += 2;
1404*4f655ef5SMatthew Dillon 	w = le16dec(frm);
140532176cfdSRui Paulo 	if (w != RSN_VERSION) {
140632176cfdSRui Paulo 		IEEE80211_DISCARD_IE(vap,
140732176cfdSRui Paulo 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
140832176cfdSRui Paulo 		    wh, "RSN", "bad version %u", w);
140932176cfdSRui Paulo 		return IEEE80211_REASON_IE_INVALID;
141032176cfdSRui Paulo 	}
141132176cfdSRui Paulo 	frm += 2, len -= 2;
141232176cfdSRui Paulo 
141332176cfdSRui Paulo 	memset(rsn, 0, sizeof(*rsn));
141432176cfdSRui Paulo 
141532176cfdSRui Paulo 	/* multicast/group cipher */
141632176cfdSRui Paulo 	rsn->rsn_mcastcipher = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
141732176cfdSRui Paulo 	frm += 4, len -= 4;
141832176cfdSRui Paulo 
141932176cfdSRui Paulo 	/* unicast ciphers */
1420*4f655ef5SMatthew Dillon 	n = le16dec(frm);
142132176cfdSRui Paulo 	frm += 2, len -= 2;
142232176cfdSRui Paulo 	if (len < n*4+2) {
142332176cfdSRui Paulo 		IEEE80211_DISCARD_IE(vap,
142432176cfdSRui Paulo 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
142532176cfdSRui Paulo 		    wh, "RSN", "ucast cipher data too short; len %u, n %u",
142632176cfdSRui Paulo 		    len, n);
142732176cfdSRui Paulo 		return IEEE80211_REASON_IE_INVALID;
142832176cfdSRui Paulo 	}
142932176cfdSRui Paulo 	w = 0;
143032176cfdSRui Paulo 	for (; n > 0; n--) {
143132176cfdSRui Paulo 		w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
143232176cfdSRui Paulo 		frm += 4, len -= 4;
143332176cfdSRui Paulo 	}
143432176cfdSRui Paulo 	if (w & (1<<IEEE80211_CIPHER_TKIP))
143532176cfdSRui Paulo 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
143632176cfdSRui Paulo 	else
143732176cfdSRui Paulo 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
143832176cfdSRui Paulo 
143932176cfdSRui Paulo 	/* key management algorithms */
1440*4f655ef5SMatthew Dillon 	n = le16dec(frm);
144132176cfdSRui Paulo 	frm += 2, len -= 2;
144232176cfdSRui Paulo 	if (len < n*4) {
144332176cfdSRui Paulo 		IEEE80211_DISCARD_IE(vap,
144432176cfdSRui Paulo 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
144532176cfdSRui Paulo 		    wh, "RSN", "key mgmt alg data too short; len %u, n %u",
144632176cfdSRui Paulo 		    len, n);
144732176cfdSRui Paulo 		return IEEE80211_REASON_IE_INVALID;
144832176cfdSRui Paulo 	}
144932176cfdSRui Paulo 	w = 0;
145032176cfdSRui Paulo 	for (; n > 0; n--) {
145132176cfdSRui Paulo 		w |= rsn_keymgmt(frm);
145232176cfdSRui Paulo 		frm += 4, len -= 4;
145332176cfdSRui Paulo 	}
145432176cfdSRui Paulo 	if (w & RSN_ASE_8021X_UNSPEC)
145532176cfdSRui Paulo 		rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
145632176cfdSRui Paulo 	else
145732176cfdSRui Paulo 		rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
145832176cfdSRui Paulo 
145932176cfdSRui Paulo 	/* optional RSN capabilities */
146032176cfdSRui Paulo 	if (len > 2)
1461*4f655ef5SMatthew Dillon 		rsn->rsn_caps = le16dec(frm);
146232176cfdSRui Paulo 	/* XXXPMKID */
146332176cfdSRui Paulo 
146432176cfdSRui Paulo 	return 0;
146532176cfdSRui Paulo }
146632176cfdSRui Paulo 
146732176cfdSRui Paulo /*
1468*4f655ef5SMatthew Dillon  * WPA/802.11i association request processing.
146932176cfdSRui Paulo  */
147032176cfdSRui Paulo static int
wpa_assocreq(struct ieee80211_node * ni,struct ieee80211_rsnparms * rsnparms,const struct ieee80211_frame * wh,const uint8_t * wpa,const uint8_t * rsn,uint16_t capinfo)147132176cfdSRui Paulo wpa_assocreq(struct ieee80211_node *ni, struct ieee80211_rsnparms *rsnparms,
147232176cfdSRui Paulo 	const struct ieee80211_frame *wh, const uint8_t *wpa,
147332176cfdSRui Paulo 	const uint8_t *rsn, uint16_t capinfo)
147432176cfdSRui Paulo {
147532176cfdSRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
147632176cfdSRui Paulo 	uint8_t reason;
147732176cfdSRui Paulo 	int badwparsn;
147832176cfdSRui Paulo 
147932176cfdSRui Paulo 	ni->ni_flags &= ~(IEEE80211_NODE_WPS|IEEE80211_NODE_TSN);
148032176cfdSRui Paulo 	if (wpa == NULL && rsn == NULL) {
148132176cfdSRui Paulo 		if (vap->iv_flags_ext & IEEE80211_FEXT_WPS) {
148232176cfdSRui Paulo 			/*
148332176cfdSRui Paulo 			 * W-Fi Protected Setup (WPS) permits
148432176cfdSRui Paulo 			 * clients to associate and pass EAPOL frames
148532176cfdSRui Paulo 			 * to establish initial credentials.
148632176cfdSRui Paulo 			 */
148732176cfdSRui Paulo 			ni->ni_flags |= IEEE80211_NODE_WPS;
148832176cfdSRui Paulo 			return 1;
148932176cfdSRui Paulo 		}
149032176cfdSRui Paulo 		if ((vap->iv_flags_ext & IEEE80211_FEXT_TSN) &&
149132176cfdSRui Paulo 		    (capinfo & IEEE80211_CAPINFO_PRIVACY)) {
149232176cfdSRui Paulo 			/*
149332176cfdSRui Paulo 			 * Transitional Security Network.  Permits clients
149432176cfdSRui Paulo 			 * to associate and use WEP while WPA is configured.
149532176cfdSRui Paulo 			 */
149632176cfdSRui Paulo 			ni->ni_flags |= IEEE80211_NODE_TSN;
149732176cfdSRui Paulo 			return 1;
149832176cfdSRui Paulo 		}
149932176cfdSRui Paulo 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
150032176cfdSRui Paulo 		    wh, NULL, "%s", "no WPA/RSN IE in association request");
150132176cfdSRui Paulo 		vap->iv_stats.is_rx_assoc_badwpaie++;
150232176cfdSRui Paulo 		reason = IEEE80211_REASON_IE_INVALID;
150332176cfdSRui Paulo 		goto bad;
150432176cfdSRui Paulo 	}
150532176cfdSRui Paulo 	/* assert right association security credentials */
150632176cfdSRui Paulo 	badwparsn = 0;			/* NB: to silence compiler */
150732176cfdSRui Paulo 	switch (vap->iv_flags & IEEE80211_F_WPA) {
150832176cfdSRui Paulo 	case IEEE80211_F_WPA1:
150932176cfdSRui Paulo 		badwparsn = (wpa == NULL);
151032176cfdSRui Paulo 		break;
151132176cfdSRui Paulo 	case IEEE80211_F_WPA2:
151232176cfdSRui Paulo 		badwparsn = (rsn == NULL);
151332176cfdSRui Paulo 		break;
151432176cfdSRui Paulo 	case IEEE80211_F_WPA1|IEEE80211_F_WPA2:
151532176cfdSRui Paulo 		badwparsn = (wpa == NULL && rsn == NULL);
151632176cfdSRui Paulo 		break;
151732176cfdSRui Paulo 	}
151832176cfdSRui Paulo 	if (badwparsn) {
151932176cfdSRui Paulo 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
152032176cfdSRui Paulo 		    wh, NULL,
152132176cfdSRui Paulo 		    "%s", "missing WPA/RSN IE in association request");
152232176cfdSRui Paulo 		vap->iv_stats.is_rx_assoc_badwpaie++;
152332176cfdSRui Paulo 		reason = IEEE80211_REASON_IE_INVALID;
152432176cfdSRui Paulo 		goto bad;
152532176cfdSRui Paulo 	}
152632176cfdSRui Paulo 	/*
152732176cfdSRui Paulo 	 * Parse WPA/RSN information element.
152832176cfdSRui Paulo 	 */
152932176cfdSRui Paulo 	if (wpa != NULL)
153032176cfdSRui Paulo 		reason = ieee80211_parse_wpa(vap, wpa, rsnparms, wh);
153132176cfdSRui Paulo 	else
153232176cfdSRui Paulo 		reason = ieee80211_parse_rsn(vap, rsn, rsnparms, wh);
153332176cfdSRui Paulo 	if (reason != 0) {
153432176cfdSRui Paulo 		/* XXX distinguish WPA/RSN? */
153532176cfdSRui Paulo 		vap->iv_stats.is_rx_assoc_badwpaie++;
153632176cfdSRui Paulo 		goto bad;
153732176cfdSRui Paulo 	}
153832176cfdSRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA, ni,
153932176cfdSRui Paulo 	    "%s ie: mc %u/%u uc %u/%u key %u caps 0x%x",
154032176cfdSRui Paulo 	    wpa != NULL ? "WPA" : "RSN",
154132176cfdSRui Paulo 	    rsnparms->rsn_mcastcipher, rsnparms->rsn_mcastkeylen,
154232176cfdSRui Paulo 	    rsnparms->rsn_ucastcipher, rsnparms->rsn_ucastkeylen,
154332176cfdSRui Paulo 	    rsnparms->rsn_keymgmt, rsnparms->rsn_caps);
154432176cfdSRui Paulo 
154532176cfdSRui Paulo 	return 1;
154632176cfdSRui Paulo bad:
154732176cfdSRui Paulo 	ieee80211_node_deauth(ni, reason);
154832176cfdSRui Paulo 	return 0;
154932176cfdSRui Paulo }
155032176cfdSRui Paulo 
155132176cfdSRui Paulo /* XXX find a better place for definition */
155232176cfdSRui Paulo struct l2_update_frame {
155332176cfdSRui Paulo 	struct ether_header eh;
155432176cfdSRui Paulo 	uint8_t dsap;
155532176cfdSRui Paulo 	uint8_t ssap;
155632176cfdSRui Paulo 	uint8_t control;
155732176cfdSRui Paulo 	uint8_t xid[3];
155832176cfdSRui Paulo }  __packed;
155932176cfdSRui Paulo 
156032176cfdSRui Paulo /*
156132176cfdSRui Paulo  * Deliver a TGf L2UF frame on behalf of a station.
156232176cfdSRui Paulo  * This primes any bridge when the station is roaming
156332176cfdSRui Paulo  * between ap's on the same wired network.
156432176cfdSRui Paulo  */
156532176cfdSRui Paulo static void
ieee80211_deliver_l2uf(struct ieee80211_node * ni)156632176cfdSRui Paulo ieee80211_deliver_l2uf(struct ieee80211_node *ni)
156732176cfdSRui Paulo {
156832176cfdSRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
156932176cfdSRui Paulo 	struct ifnet *ifp = vap->iv_ifp;
157032176cfdSRui Paulo 	struct mbuf *m;
157132176cfdSRui Paulo 	struct l2_update_frame *l2uf;
157232176cfdSRui Paulo 	struct ether_header *eh;
157332176cfdSRui Paulo 
1574b5523eacSSascha Wildner 	m = m_gethdr(M_NOWAIT, MT_DATA);
157532176cfdSRui Paulo 	if (m == NULL) {
157632176cfdSRui Paulo 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
157732176cfdSRui Paulo 		    "%s", "no mbuf for l2uf frame");
157832176cfdSRui Paulo 		vap->iv_stats.is_rx_nobuf++;	/* XXX not right */
157932176cfdSRui Paulo 		return;
158032176cfdSRui Paulo 	}
158132176cfdSRui Paulo 	l2uf = mtod(m, struct l2_update_frame *);
158232176cfdSRui Paulo 	eh = &l2uf->eh;
158332176cfdSRui Paulo 	/* dst: Broadcast address */
158432176cfdSRui Paulo 	IEEE80211_ADDR_COPY(eh->ether_dhost, ifp->if_broadcastaddr);
158532176cfdSRui Paulo 	/* src: associated STA */
158632176cfdSRui Paulo 	IEEE80211_ADDR_COPY(eh->ether_shost, ni->ni_macaddr);
158732176cfdSRui Paulo 	eh->ether_type = htons(sizeof(*l2uf) - sizeof(*eh));
158832176cfdSRui Paulo 
158932176cfdSRui Paulo 	l2uf->dsap = 0;
159032176cfdSRui Paulo 	l2uf->ssap = 0;
159132176cfdSRui Paulo 	l2uf->control = 0xf5;
159232176cfdSRui Paulo 	l2uf->xid[0] = 0x81;
159332176cfdSRui Paulo 	l2uf->xid[1] = 0x80;
159432176cfdSRui Paulo 	l2uf->xid[2] = 0x00;
159532176cfdSRui Paulo 
159632176cfdSRui Paulo 	m->m_pkthdr.len = m->m_len = sizeof(*l2uf);
159732176cfdSRui Paulo 	hostap_deliver_data(vap, ni, m);
159832176cfdSRui Paulo }
159932176cfdSRui Paulo 
160032176cfdSRui Paulo static void
ratesetmismatch(struct ieee80211_node * ni,const struct ieee80211_frame * wh,int reassoc,int resp,const char * tag,int rate)160132176cfdSRui Paulo ratesetmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
160232176cfdSRui Paulo 	int reassoc, int resp, const char *tag, int rate)
160332176cfdSRui Paulo {
160432176cfdSRui Paulo 	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
160532176cfdSRui Paulo 	    "deny %s request, %s rate set mismatch, rate/MCS %d",
160632176cfdSRui Paulo 	    reassoc ? "reassoc" : "assoc", tag, rate & IEEE80211_RATE_VAL);
160732176cfdSRui Paulo 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_BASIC_RATE);
160832176cfdSRui Paulo 	ieee80211_node_leave(ni);
160932176cfdSRui Paulo }
161032176cfdSRui Paulo 
161132176cfdSRui Paulo static void
capinfomismatch(struct ieee80211_node * ni,const struct ieee80211_frame * wh,int reassoc,int resp,const char * tag,int capinfo)161232176cfdSRui Paulo capinfomismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
161332176cfdSRui Paulo 	int reassoc, int resp, const char *tag, int capinfo)
161432176cfdSRui Paulo {
161532176cfdSRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
161632176cfdSRui Paulo 
161732176cfdSRui Paulo 	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
161832176cfdSRui Paulo 	    "deny %s request, %s mismatch 0x%x",
161932176cfdSRui Paulo 	    reassoc ? "reassoc" : "assoc", tag, capinfo);
162032176cfdSRui Paulo 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_CAPINFO);
162132176cfdSRui Paulo 	ieee80211_node_leave(ni);
162232176cfdSRui Paulo 	vap->iv_stats.is_rx_assoc_capmismatch++;
162332176cfdSRui Paulo }
162432176cfdSRui Paulo 
162532176cfdSRui Paulo static void
htcapmismatch(struct ieee80211_node * ni,const struct ieee80211_frame * wh,int reassoc,int resp)162632176cfdSRui Paulo htcapmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
162732176cfdSRui Paulo 	int reassoc, int resp)
162832176cfdSRui Paulo {
162932176cfdSRui Paulo 	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
16302c7ccc4aSSascha Wildner 	    "deny %s request, missing HT ie", reassoc ? "reassoc" : "assoc");
163132176cfdSRui Paulo 	/* XXX no better code */
163232176cfdSRui Paulo 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_MISSING_HT_CAPS);
163332176cfdSRui Paulo 	ieee80211_node_leave(ni);
163432176cfdSRui Paulo }
163532176cfdSRui Paulo 
163632176cfdSRui Paulo static void
authalgreject(struct ieee80211_node * ni,const struct ieee80211_frame * wh,int algo,int seq,int status)163732176cfdSRui Paulo authalgreject(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
163832176cfdSRui Paulo 	int algo, int seq, int status)
163932176cfdSRui Paulo {
164032176cfdSRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
164132176cfdSRui Paulo 
164232176cfdSRui Paulo 	IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
164332176cfdSRui Paulo 	    wh, NULL, "unsupported alg %d", algo);
164432176cfdSRui Paulo 	vap->iv_stats.is_rx_auth_unsupported++;
164532176cfdSRui Paulo 	ieee80211_send_error(ni, wh->i_addr2, IEEE80211_FC0_SUBTYPE_AUTH,
164632176cfdSRui Paulo 	    seq | (status << 16));
164732176cfdSRui Paulo }
164832176cfdSRui Paulo 
164932176cfdSRui Paulo static __inline int
ishtmixed(const uint8_t * ie)165032176cfdSRui Paulo ishtmixed(const uint8_t *ie)
165132176cfdSRui Paulo {
165232176cfdSRui Paulo 	const struct ieee80211_ie_htinfo *ht =
165332176cfdSRui Paulo 	    (const struct ieee80211_ie_htinfo *) ie;
165432176cfdSRui Paulo 	return (ht->hi_byte2 & IEEE80211_HTINFO_OPMODE) ==
165532176cfdSRui Paulo 	    IEEE80211_HTINFO_OPMODE_MIXED;
165632176cfdSRui Paulo }
165732176cfdSRui Paulo 
165832176cfdSRui Paulo static int
is11bclient(const uint8_t * rates,const uint8_t * xrates)165932176cfdSRui Paulo is11bclient(const uint8_t *rates, const uint8_t *xrates)
166032176cfdSRui Paulo {
166132176cfdSRui Paulo 	static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
166232176cfdSRui Paulo 	int i;
166332176cfdSRui Paulo 
166432176cfdSRui Paulo 	/* NB: the 11b clients we care about will not have xrates */
166532176cfdSRui Paulo 	if (xrates != NULL || rates == NULL)
166632176cfdSRui Paulo 		return 0;
166732176cfdSRui Paulo 	for (i = 0; i < rates[1]; i++) {
166832176cfdSRui Paulo 		int r = rates[2+i] & IEEE80211_RATE_VAL;
166932176cfdSRui Paulo 		if (r > 2*11 || ((1<<r) & brates) == 0)
167032176cfdSRui Paulo 			return 0;
167132176cfdSRui Paulo 	}
167232176cfdSRui Paulo 	return 1;
167332176cfdSRui Paulo }
167432176cfdSRui Paulo 
167532176cfdSRui Paulo static void
hostap_recv_mgmt(struct ieee80211_node * ni,struct mbuf * m0,int subtype,const struct ieee80211_rx_stats * rxs,int rssi,int nf)167632176cfdSRui Paulo hostap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
16774f898719SImre Vadász 	int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
167832176cfdSRui Paulo {
167932176cfdSRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
168032176cfdSRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
168132176cfdSRui Paulo 	struct ieee80211_frame *wh;
168232176cfdSRui Paulo 	uint8_t *frm, *efrm, *sfrm;
168332176cfdSRui Paulo 	uint8_t *ssid, *rates, *xrates, *wpa, *rsn, *wme, *ath, *htcap;
168432176cfdSRui Paulo 	int reassoc, resp;
168532176cfdSRui Paulo 	uint8_t rate;
168632176cfdSRui Paulo 
168732176cfdSRui Paulo 	wh = mtod(m0, struct ieee80211_frame *);
168832176cfdSRui Paulo 	frm = (uint8_t *)&wh[1];
168932176cfdSRui Paulo 	efrm = mtod(m0, uint8_t *) + m0->m_len;
169032176cfdSRui Paulo 	switch (subtype) {
169132176cfdSRui Paulo 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
169232176cfdSRui Paulo 		/*
169332176cfdSRui Paulo 		 * We process beacon/probe response frames when scanning;
169432176cfdSRui Paulo 		 * otherwise we check beacon frames for overlapping non-ERP
169532176cfdSRui Paulo 		 * BSS in 11g and/or overlapping legacy BSS when in HT.
169632176cfdSRui Paulo 		 */
1697*4f655ef5SMatthew Dillon 		if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
169832176cfdSRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++;
169932176cfdSRui Paulo 			return;
170032176cfdSRui Paulo 		}
1701*4f655ef5SMatthew Dillon 		/* FALLTHROUGH */
1702*4f655ef5SMatthew Dillon 	case IEEE80211_FC0_SUBTYPE_BEACON: {
1703*4f655ef5SMatthew Dillon 		struct ieee80211_scanparams scan;
1704*4f655ef5SMatthew Dillon 
170532176cfdSRui Paulo 		/* NB: accept off-channel frames */
1706*4f655ef5SMatthew Dillon 		/* XXX TODO: use rxstatus to determine off-channel details */
17074f898719SImre Vadász 		if (ieee80211_parse_beacon(ni, m0, ic->ic_curchan, &scan) &~ IEEE80211_BPARSE_OFFCHAN)
170832176cfdSRui Paulo 			return;
170932176cfdSRui Paulo 		/*
171032176cfdSRui Paulo 		 * Count frame now that we know it's to be processed.
171132176cfdSRui Paulo 		 */
171232176cfdSRui Paulo 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
171332176cfdSRui Paulo 			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
171432176cfdSRui Paulo 			IEEE80211_NODE_STAT(ni, rx_beacons);
171532176cfdSRui Paulo 		} else
171632176cfdSRui Paulo 			IEEE80211_NODE_STAT(ni, rx_proberesp);
171732176cfdSRui Paulo 		/*
171832176cfdSRui Paulo 		 * If scanning, just pass information to the scan module.
171932176cfdSRui Paulo 		 */
172032176cfdSRui Paulo 		if (ic->ic_flags & IEEE80211_F_SCAN) {
172132176cfdSRui Paulo 			if (scan.status == 0 &&		/* NB: on channel */
172232176cfdSRui Paulo 			    (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN)) {
172332176cfdSRui Paulo 				/*
172432176cfdSRui Paulo 				 * Actively scanning a channel marked passive;
172532176cfdSRui Paulo 				 * send a probe request now that we know there
172632176cfdSRui Paulo 				 * is 802.11 traffic present.
172732176cfdSRui Paulo 				 *
172832176cfdSRui Paulo 				 * XXX check if the beacon we recv'd gives
172932176cfdSRui Paulo 				 * us what we need and suppress the probe req
173032176cfdSRui Paulo 				 */
173132176cfdSRui Paulo 				ieee80211_probe_curchan(vap, 1);
173232176cfdSRui Paulo 				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
173332176cfdSRui Paulo 			}
17344f898719SImre Vadász 			ieee80211_add_scan(vap, ic->ic_curchan, &scan, wh,
17354f898719SImre Vadász 			    subtype, rssi, nf);
173632176cfdSRui Paulo 			return;
173732176cfdSRui Paulo 		}
173832176cfdSRui Paulo 		/*
173932176cfdSRui Paulo 		 * Check beacon for overlapping bss w/ non ERP stations.
174032176cfdSRui Paulo 		 * If we detect one and protection is configured but not
174132176cfdSRui Paulo 		 * enabled, enable it and start a timer that'll bring us
174232176cfdSRui Paulo 		 * out if we stop seeing the bss.
174332176cfdSRui Paulo 		 */
174432176cfdSRui Paulo 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
174532176cfdSRui Paulo 		    scan.status == 0 &&			/* NB: on-channel */
174632176cfdSRui Paulo 		    ((scan.erp & 0x100) == 0 ||		/* NB: no ERP, 11b sta*/
174732176cfdSRui Paulo 		     (scan.erp & IEEE80211_ERP_NON_ERP_PRESENT))) {
174832176cfdSRui Paulo 			ic->ic_lastnonerp = ticks;
174932176cfdSRui Paulo 			ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
175032176cfdSRui Paulo 			if (ic->ic_protmode != IEEE80211_PROT_NONE &&
175132176cfdSRui Paulo 			    (ic->ic_flags & IEEE80211_F_USEPROT) == 0) {
175232176cfdSRui Paulo 				IEEE80211_NOTE_FRAME(vap,
175332176cfdSRui Paulo 				    IEEE80211_MSG_ASSOC, wh,
175432176cfdSRui Paulo 				    "non-ERP present on channel %d "
175532176cfdSRui Paulo 				    "(saw erp 0x%x from channel %d), "
175632176cfdSRui Paulo 				    "enable use of protection",
175732176cfdSRui Paulo 				    ic->ic_curchan->ic_ieee,
175832176cfdSRui Paulo 				    scan.erp, scan.chan);
175932176cfdSRui Paulo 				ic->ic_flags |= IEEE80211_F_USEPROT;
176032176cfdSRui Paulo 				ieee80211_notify_erp(ic);
176132176cfdSRui Paulo 			}
176232176cfdSRui Paulo 		}
176332176cfdSRui Paulo 		/*
176432176cfdSRui Paulo 		 * Check beacon for non-HT station on HT channel
176532176cfdSRui Paulo 		 * and update HT BSS occupancy as appropriate.
176632176cfdSRui Paulo 		 */
176732176cfdSRui Paulo 		if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) {
176832176cfdSRui Paulo 			if (scan.status & IEEE80211_BPARSE_OFFCHAN) {
176932176cfdSRui Paulo 				/*
177032176cfdSRui Paulo 				 * Off control channel; only check frames
177132176cfdSRui Paulo 				 * that come in the extension channel when
177232176cfdSRui Paulo 				 * operating w/ HT40.
177332176cfdSRui Paulo 				 */
177432176cfdSRui Paulo 				if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
177532176cfdSRui Paulo 					break;
177632176cfdSRui Paulo 				if (scan.chan != ic->ic_curchan->ic_extieee)
177732176cfdSRui Paulo 					break;
177832176cfdSRui Paulo 			}
177932176cfdSRui Paulo 			if (scan.htinfo == NULL) {
178032176cfdSRui Paulo 				ieee80211_htprot_update(ic,
178132176cfdSRui Paulo 				    IEEE80211_HTINFO_OPMODE_PROTOPT |
178232176cfdSRui Paulo 				    IEEE80211_HTINFO_NONHT_PRESENT);
178332176cfdSRui Paulo 			} else if (ishtmixed(scan.htinfo)) {
178432176cfdSRui Paulo 				/* XXX? take NONHT_PRESENT from beacon? */
178532176cfdSRui Paulo 				ieee80211_htprot_update(ic,
178632176cfdSRui Paulo 				    IEEE80211_HTINFO_OPMODE_MIXED |
178732176cfdSRui Paulo 				    IEEE80211_HTINFO_NONHT_PRESENT);
178832176cfdSRui Paulo 			}
178932176cfdSRui Paulo 		}
179032176cfdSRui Paulo 		break;
179132176cfdSRui Paulo 	}
179232176cfdSRui Paulo 
179332176cfdSRui Paulo 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
179432176cfdSRui Paulo 		if (vap->iv_state != IEEE80211_S_RUN) {
179532176cfdSRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++;
179632176cfdSRui Paulo 			return;
179732176cfdSRui Paulo 		}
179832176cfdSRui Paulo 		/*
1799085ff963SMatthew Dillon 		 * Consult the ACL policy module if setup.
1800085ff963SMatthew Dillon 		 */
1801085ff963SMatthew Dillon 		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1802085ff963SMatthew Dillon 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1803085ff963SMatthew Dillon 			    wh, NULL, "%s", "disallowed by ACL");
1804085ff963SMatthew Dillon 			vap->iv_stats.is_rx_acl++;
1805085ff963SMatthew Dillon 			return;
1806085ff963SMatthew Dillon 		}
1807085ff963SMatthew Dillon 		/*
180832176cfdSRui Paulo 		 * prreq frame format
180932176cfdSRui Paulo 		 *	[tlv] ssid
181032176cfdSRui Paulo 		 *	[tlv] supported rates
181132176cfdSRui Paulo 		 *	[tlv] extended supported rates
181232176cfdSRui Paulo 		 */
181332176cfdSRui Paulo 		ssid = rates = xrates = NULL;
181432176cfdSRui Paulo 		sfrm = frm;
181532176cfdSRui Paulo 		while (efrm - frm > 1) {
181632176cfdSRui Paulo 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
181732176cfdSRui Paulo 			switch (*frm) {
181832176cfdSRui Paulo 			case IEEE80211_ELEMID_SSID:
181932176cfdSRui Paulo 				ssid = frm;
182032176cfdSRui Paulo 				break;
182132176cfdSRui Paulo 			case IEEE80211_ELEMID_RATES:
182232176cfdSRui Paulo 				rates = frm;
182332176cfdSRui Paulo 				break;
182432176cfdSRui Paulo 			case IEEE80211_ELEMID_XRATES:
182532176cfdSRui Paulo 				xrates = frm;
182632176cfdSRui Paulo 				break;
182732176cfdSRui Paulo 			}
182832176cfdSRui Paulo 			frm += frm[1] + 2;
182932176cfdSRui Paulo 		}
183032176cfdSRui Paulo 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
183132176cfdSRui Paulo 		if (xrates != NULL)
183232176cfdSRui Paulo 			IEEE80211_VERIFY_ELEMENT(xrates,
183332176cfdSRui Paulo 				IEEE80211_RATE_MAXSIZE - rates[1], return);
183432176cfdSRui Paulo 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
183532176cfdSRui Paulo 		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
183632176cfdSRui Paulo 		if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
183732176cfdSRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
183832176cfdSRui Paulo 			    wh, NULL,
183932176cfdSRui Paulo 			    "%s", "no ssid with ssid suppression enabled");
184032176cfdSRui Paulo 			vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
184132176cfdSRui Paulo 			return;
184232176cfdSRui Paulo 		}
184332176cfdSRui Paulo 
184432176cfdSRui Paulo 		/* XXX find a better class or define it's own */
184532176cfdSRui Paulo 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
184632176cfdSRui Paulo 		    "%s", "recv probe req");
184732176cfdSRui Paulo 		/*
184832176cfdSRui Paulo 		 * Some legacy 11b clients cannot hack a complete
184932176cfdSRui Paulo 		 * probe response frame.  When the request includes
185032176cfdSRui Paulo 		 * only a bare-bones rate set, communicate this to
185132176cfdSRui Paulo 		 * the transmit side.
185232176cfdSRui Paulo 		 */
185332176cfdSRui Paulo 		ieee80211_send_proberesp(vap, wh->i_addr2,
185432176cfdSRui Paulo 		    is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
185532176cfdSRui Paulo 		break;
185632176cfdSRui Paulo 
185732176cfdSRui Paulo 	case IEEE80211_FC0_SUBTYPE_AUTH: {
185832176cfdSRui Paulo 		uint16_t algo, seq, status;
185932176cfdSRui Paulo 
186032176cfdSRui Paulo 		if (vap->iv_state != IEEE80211_S_RUN) {
186132176cfdSRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++;
186232176cfdSRui Paulo 			return;
186332176cfdSRui Paulo 		}
186432176cfdSRui Paulo 		if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
186532176cfdSRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
186632176cfdSRui Paulo 			    wh, NULL, "%s", "wrong bssid");
186732176cfdSRui Paulo 			vap->iv_stats.is_rx_wrongbss++;	/*XXX unique stat?*/
186832176cfdSRui Paulo 			return;
186932176cfdSRui Paulo 		}
187032176cfdSRui Paulo 		/*
187132176cfdSRui Paulo 		 * auth frame format
187232176cfdSRui Paulo 		 *	[2] algorithm
187332176cfdSRui Paulo 		 *	[2] sequence
187432176cfdSRui Paulo 		 *	[2] status
187532176cfdSRui Paulo 		 *	[tlv*] challenge
187632176cfdSRui Paulo 		 */
187732176cfdSRui Paulo 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
187832176cfdSRui Paulo 		algo   = le16toh(*(uint16_t *)frm);
187932176cfdSRui Paulo 		seq    = le16toh(*(uint16_t *)(frm + 2));
188032176cfdSRui Paulo 		status = le16toh(*(uint16_t *)(frm + 4));
188132176cfdSRui Paulo 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
188232176cfdSRui Paulo 		    "recv auth frame with algorithm %d seq %d", algo, seq);
188332176cfdSRui Paulo 		/*
188432176cfdSRui Paulo 		 * Consult the ACL policy module if setup.
188532176cfdSRui Paulo 		 */
1886085ff963SMatthew Dillon 		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
188732176cfdSRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
188832176cfdSRui Paulo 			    wh, NULL, "%s", "disallowed by ACL");
188932176cfdSRui Paulo 			vap->iv_stats.is_rx_acl++;
189032176cfdSRui Paulo 			ieee80211_send_error(ni, wh->i_addr2,
189132176cfdSRui Paulo 			    IEEE80211_FC0_SUBTYPE_AUTH,
189232176cfdSRui Paulo 			    (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
189332176cfdSRui Paulo 			return;
189432176cfdSRui Paulo 		}
189532176cfdSRui Paulo 		if (vap->iv_flags & IEEE80211_F_COUNTERM) {
189632176cfdSRui Paulo 			IEEE80211_DISCARD(vap,
189732176cfdSRui Paulo 			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
189832176cfdSRui Paulo 			    wh, NULL, "%s", "TKIP countermeasures enabled");
189932176cfdSRui Paulo 			vap->iv_stats.is_rx_auth_countermeasures++;
190032176cfdSRui Paulo 			ieee80211_send_error(ni, wh->i_addr2,
190132176cfdSRui Paulo 				IEEE80211_FC0_SUBTYPE_AUTH,
190232176cfdSRui Paulo 				IEEE80211_REASON_MIC_FAILURE);
190332176cfdSRui Paulo 			return;
190432176cfdSRui Paulo 		}
190532176cfdSRui Paulo 		if (algo == IEEE80211_AUTH_ALG_SHARED)
190632176cfdSRui Paulo 			hostap_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
190732176cfdSRui Paulo 			    seq, status);
190832176cfdSRui Paulo 		else if (algo == IEEE80211_AUTH_ALG_OPEN)
190932176cfdSRui Paulo 			hostap_auth_open(ni, wh, rssi, nf, seq, status);
191032176cfdSRui Paulo 		else if (algo == IEEE80211_AUTH_ALG_LEAP) {
191132176cfdSRui Paulo 			authalgreject(ni, wh, algo,
191232176cfdSRui Paulo 			    seq+1, IEEE80211_STATUS_ALG);
191332176cfdSRui Paulo 			return;
191432176cfdSRui Paulo 		} else {
191532176cfdSRui Paulo 			/*
191632176cfdSRui Paulo 			 * We assume that an unknown algorithm is the result
191732176cfdSRui Paulo 			 * of a decryption failure on a shared key auth frame;
191832176cfdSRui Paulo 			 * return a status code appropriate for that instead
191932176cfdSRui Paulo 			 * of IEEE80211_STATUS_ALG.
192032176cfdSRui Paulo 			 *
192132176cfdSRui Paulo 			 * NB: a seq# of 4 is intentional; the decrypted
192232176cfdSRui Paulo 			 *     frame likely has a bogus seq value.
192332176cfdSRui Paulo 			 */
192432176cfdSRui Paulo 			authalgreject(ni, wh, algo,
192532176cfdSRui Paulo 			    4, IEEE80211_STATUS_CHALLENGE);
192632176cfdSRui Paulo 			return;
192732176cfdSRui Paulo 		}
192832176cfdSRui Paulo 		break;
192932176cfdSRui Paulo 	}
193032176cfdSRui Paulo 
193132176cfdSRui Paulo 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
193232176cfdSRui Paulo 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
193332176cfdSRui Paulo 		uint16_t capinfo, lintval;
193432176cfdSRui Paulo 		struct ieee80211_rsnparms rsnparms;
193532176cfdSRui Paulo 
193632176cfdSRui Paulo 		if (vap->iv_state != IEEE80211_S_RUN) {
193732176cfdSRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++;
193832176cfdSRui Paulo 			return;
193932176cfdSRui Paulo 		}
194032176cfdSRui Paulo 		if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
194132176cfdSRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
194232176cfdSRui Paulo 			    wh, NULL, "%s", "wrong bssid");
194332176cfdSRui Paulo 			vap->iv_stats.is_rx_assoc_bss++;
194432176cfdSRui Paulo 			return;
194532176cfdSRui Paulo 		}
194632176cfdSRui Paulo 		if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
194732176cfdSRui Paulo 			reassoc = 1;
194832176cfdSRui Paulo 			resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
194932176cfdSRui Paulo 		} else {
195032176cfdSRui Paulo 			reassoc = 0;
195132176cfdSRui Paulo 			resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
195232176cfdSRui Paulo 		}
195332176cfdSRui Paulo 		if (ni == vap->iv_bss) {
195432176cfdSRui Paulo 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
195532176cfdSRui Paulo 			    "deny %s request, sta not authenticated",
195632176cfdSRui Paulo 			    reassoc ? "reassoc" : "assoc");
195732176cfdSRui Paulo 			ieee80211_send_error(ni, wh->i_addr2,
195832176cfdSRui Paulo 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
195932176cfdSRui Paulo 			    IEEE80211_REASON_ASSOC_NOT_AUTHED);
196032176cfdSRui Paulo 			vap->iv_stats.is_rx_assoc_notauth++;
196132176cfdSRui Paulo 			return;
196232176cfdSRui Paulo 		}
196332176cfdSRui Paulo 
196432176cfdSRui Paulo 		/*
196532176cfdSRui Paulo 		 * asreq frame format
196632176cfdSRui Paulo 		 *	[2] capability information
196732176cfdSRui Paulo 		 *	[2] listen interval
196832176cfdSRui Paulo 		 *	[6*] current AP address (reassoc only)
196932176cfdSRui Paulo 		 *	[tlv] ssid
197032176cfdSRui Paulo 		 *	[tlv] supported rates
197132176cfdSRui Paulo 		 *	[tlv] extended supported rates
197232176cfdSRui Paulo 		 *	[tlv] WPA or RSN
197332176cfdSRui Paulo 		 *	[tlv] HT capabilities
197432176cfdSRui Paulo 		 *	[tlv] Atheros capabilities
197532176cfdSRui Paulo 		 */
197632176cfdSRui Paulo 		IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4), return);
197732176cfdSRui Paulo 		capinfo = le16toh(*(uint16_t *)frm);	frm += 2;
197832176cfdSRui Paulo 		lintval = le16toh(*(uint16_t *)frm);	frm += 2;
197932176cfdSRui Paulo 		if (reassoc)
198032176cfdSRui Paulo 			frm += 6;	/* ignore current AP info */
198132176cfdSRui Paulo 		ssid = rates = xrates = wpa = rsn = wme = ath = htcap = NULL;
198232176cfdSRui Paulo 		sfrm = frm;
198332176cfdSRui Paulo 		while (efrm - frm > 1) {
198432176cfdSRui Paulo 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
198532176cfdSRui Paulo 			switch (*frm) {
198632176cfdSRui Paulo 			case IEEE80211_ELEMID_SSID:
198732176cfdSRui Paulo 				ssid = frm;
198832176cfdSRui Paulo 				break;
198932176cfdSRui Paulo 			case IEEE80211_ELEMID_RATES:
199032176cfdSRui Paulo 				rates = frm;
199132176cfdSRui Paulo 				break;
199232176cfdSRui Paulo 			case IEEE80211_ELEMID_XRATES:
199332176cfdSRui Paulo 				xrates = frm;
199432176cfdSRui Paulo 				break;
199532176cfdSRui Paulo 			case IEEE80211_ELEMID_RSN:
199632176cfdSRui Paulo 				rsn = frm;
199732176cfdSRui Paulo 				break;
199832176cfdSRui Paulo 			case IEEE80211_ELEMID_HTCAP:
199932176cfdSRui Paulo 				htcap = frm;
200032176cfdSRui Paulo 				break;
200132176cfdSRui Paulo 			case IEEE80211_ELEMID_VENDOR:
200232176cfdSRui Paulo 				if (iswpaoui(frm))
200332176cfdSRui Paulo 					wpa = frm;
200432176cfdSRui Paulo 				else if (iswmeinfo(frm))
200532176cfdSRui Paulo 					wme = frm;
200632176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG
200732176cfdSRui Paulo 				else if (isatherosoui(frm))
200832176cfdSRui Paulo 					ath = frm;
200932176cfdSRui Paulo #endif
201032176cfdSRui Paulo 				else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
201132176cfdSRui Paulo 					if (ishtcapoui(frm) && htcap == NULL)
201232176cfdSRui Paulo 						htcap = frm;
201332176cfdSRui Paulo 				}
201432176cfdSRui Paulo 				break;
201532176cfdSRui Paulo 			}
201632176cfdSRui Paulo 			frm += frm[1] + 2;
201732176cfdSRui Paulo 		}
201832176cfdSRui Paulo 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
201932176cfdSRui Paulo 		if (xrates != NULL)
202032176cfdSRui Paulo 			IEEE80211_VERIFY_ELEMENT(xrates,
202132176cfdSRui Paulo 				IEEE80211_RATE_MAXSIZE - rates[1], return);
202232176cfdSRui Paulo 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
202332176cfdSRui Paulo 		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
202432176cfdSRui Paulo 		if (htcap != NULL) {
202532176cfdSRui Paulo 			IEEE80211_VERIFY_LENGTH(htcap[1],
202632176cfdSRui Paulo 			     htcap[0] == IEEE80211_ELEMID_VENDOR ?
202732176cfdSRui Paulo 			         4 + sizeof(struct ieee80211_ie_htcap)-2 :
202832176cfdSRui Paulo 			         sizeof(struct ieee80211_ie_htcap)-2,
202932176cfdSRui Paulo 			     return);		/* XXX just NULL out? */
203032176cfdSRui Paulo 		}
203132176cfdSRui Paulo 
203232176cfdSRui Paulo 		if ((vap->iv_flags & IEEE80211_F_WPA) &&
203332176cfdSRui Paulo 		    !wpa_assocreq(ni, &rsnparms, wh, wpa, rsn, capinfo))
203432176cfdSRui Paulo 			return;
203532176cfdSRui Paulo 		/* discard challenge after association */
203632176cfdSRui Paulo 		if (ni->ni_challenge != NULL) {
2037*4f655ef5SMatthew Dillon 			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
203832176cfdSRui Paulo 			ni->ni_challenge = NULL;
203932176cfdSRui Paulo 		}
204032176cfdSRui Paulo 		/* NB: 802.11 spec says to ignore station's privacy bit */
204132176cfdSRui Paulo 		if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
204232176cfdSRui Paulo 			capinfomismatch(ni, wh, reassoc, resp,
204332176cfdSRui Paulo 			    "capability", capinfo);
204432176cfdSRui Paulo 			return;
204532176cfdSRui Paulo 		}
204632176cfdSRui Paulo 		/*
204732176cfdSRui Paulo 		 * Disallow re-associate w/ invalid slot time setting.
204832176cfdSRui Paulo 		 */
204932176cfdSRui Paulo 		if (ni->ni_associd != 0 &&
205032176cfdSRui Paulo 		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
205132176cfdSRui Paulo 		    ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
205232176cfdSRui Paulo 			capinfomismatch(ni, wh, reassoc, resp,
205332176cfdSRui Paulo 			    "slot time", capinfo);
205432176cfdSRui Paulo 			return;
205532176cfdSRui Paulo 		}
205632176cfdSRui Paulo 		rate = ieee80211_setup_rates(ni, rates, xrates,
205732176cfdSRui Paulo 				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
205832176cfdSRui Paulo 				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
205932176cfdSRui Paulo 		if (rate & IEEE80211_RATE_BASIC) {
206032176cfdSRui Paulo 			ratesetmismatch(ni, wh, reassoc, resp, "legacy", rate);
206132176cfdSRui Paulo 			vap->iv_stats.is_rx_assoc_norate++;
206232176cfdSRui Paulo 			return;
206332176cfdSRui Paulo 		}
206432176cfdSRui Paulo 		/*
206532176cfdSRui Paulo 		 * If constrained to 11g-only stations reject an
206632176cfdSRui Paulo 		 * 11b-only station.  We cheat a bit here by looking
206732176cfdSRui Paulo 		 * at the max negotiated xmit rate and assuming anyone
206832176cfdSRui Paulo 		 * with a best rate <24Mb/s is an 11b station.
206932176cfdSRui Paulo 		 */
207032176cfdSRui Paulo 		if ((vap->iv_flags & IEEE80211_F_PUREG) && rate < 48) {
207132176cfdSRui Paulo 			ratesetmismatch(ni, wh, reassoc, resp, "11g", rate);
207232176cfdSRui Paulo 			vap->iv_stats.is_rx_assoc_norate++;
207332176cfdSRui Paulo 			return;
207432176cfdSRui Paulo 		}
207532176cfdSRui Paulo 		/*
207632176cfdSRui Paulo 		 * Do HT rate set handling and setup HT node state.
207732176cfdSRui Paulo 		 */
207832176cfdSRui Paulo 		ni->ni_chan = vap->iv_bss->ni_chan;
207932176cfdSRui Paulo 		if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
208032176cfdSRui Paulo 			rate = ieee80211_setup_htrates(ni, htcap,
208132176cfdSRui Paulo 				IEEE80211_F_DOFMCS | IEEE80211_F_DONEGO |
208232176cfdSRui Paulo 				IEEE80211_F_DOBRS);
208332176cfdSRui Paulo 			if (rate & IEEE80211_RATE_BASIC) {
208432176cfdSRui Paulo 				ratesetmismatch(ni, wh, reassoc, resp,
208532176cfdSRui Paulo 				    "HT", rate);
208632176cfdSRui Paulo 				vap->iv_stats.is_ht_assoc_norate++;
208732176cfdSRui Paulo 				return;
208832176cfdSRui Paulo 			}
208932176cfdSRui Paulo 			ieee80211_ht_node_init(ni);
209032176cfdSRui Paulo 			ieee80211_ht_updatehtcap(ni, htcap);
209132176cfdSRui Paulo 		} else if (ni->ni_flags & IEEE80211_NODE_HT)
209232176cfdSRui Paulo 			ieee80211_ht_node_cleanup(ni);
209332176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG
2094*4f655ef5SMatthew Dillon 		/* Always do ff node cleanup; for A-MSDU */
209532176cfdSRui Paulo 		ieee80211_ff_node_cleanup(ni);
209632176cfdSRui Paulo #endif
209732176cfdSRui Paulo 		/*
209832176cfdSRui Paulo 		 * Allow AMPDU operation only with unencrypted traffic
209932176cfdSRui Paulo 		 * or AES-CCM; the 11n spec only specifies these ciphers
210032176cfdSRui Paulo 		 * so permitting any others is undefined and can lead
210132176cfdSRui Paulo 		 * to interoperability problems.
210232176cfdSRui Paulo 		 */
210332176cfdSRui Paulo 		if ((ni->ni_flags & IEEE80211_NODE_HT) &&
210432176cfdSRui Paulo 		    (((vap->iv_flags & IEEE80211_F_WPA) &&
210532176cfdSRui Paulo 		      rsnparms.rsn_ucastcipher != IEEE80211_CIPHER_AES_CCM) ||
210632176cfdSRui Paulo 		     (vap->iv_flags & (IEEE80211_F_WPA|IEEE80211_F_PRIVACY)) == IEEE80211_F_PRIVACY)) {
210732176cfdSRui Paulo 			IEEE80211_NOTE(vap,
210832176cfdSRui Paulo 			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
210932176cfdSRui Paulo 			    "disallow HT use because WEP or TKIP requested, "
211032176cfdSRui Paulo 			    "capinfo 0x%x ucastcipher %d", capinfo,
211132176cfdSRui Paulo 			    rsnparms.rsn_ucastcipher);
211232176cfdSRui Paulo 			ieee80211_ht_node_cleanup(ni);
2113*4f655ef5SMatthew Dillon #ifdef IEEE80211_SUPPORT_SUPERG
2114*4f655ef5SMatthew Dillon 			/* Always do ff node cleanup; for A-MSDU */
2115*4f655ef5SMatthew Dillon 			ieee80211_ff_node_cleanup(ni);
2116*4f655ef5SMatthew Dillon #endif
211732176cfdSRui Paulo 			vap->iv_stats.is_ht_assoc_downgrade++;
211832176cfdSRui Paulo 		}
211932176cfdSRui Paulo 		/*
212032176cfdSRui Paulo 		 * If constrained to 11n-only stations reject legacy stations.
212132176cfdSRui Paulo 		 */
212232176cfdSRui Paulo 		if ((vap->iv_flags_ht & IEEE80211_FHT_PUREN) &&
212332176cfdSRui Paulo 		    (ni->ni_flags & IEEE80211_NODE_HT) == 0) {
212432176cfdSRui Paulo 			htcapmismatch(ni, wh, reassoc, resp);
212532176cfdSRui Paulo 			vap->iv_stats.is_ht_assoc_nohtcap++;
212632176cfdSRui Paulo 			return;
212732176cfdSRui Paulo 		}
212832176cfdSRui Paulo 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
212932176cfdSRui Paulo 		ni->ni_noise = nf;
213032176cfdSRui Paulo 		ni->ni_intval = lintval;
213132176cfdSRui Paulo 		ni->ni_capinfo = capinfo;
213232176cfdSRui Paulo 		ni->ni_fhdwell = vap->iv_bss->ni_fhdwell;
213332176cfdSRui Paulo 		ni->ni_fhindex = vap->iv_bss->ni_fhindex;
213432176cfdSRui Paulo 		/*
213532176cfdSRui Paulo 		 * Store the IEs.
213632176cfdSRui Paulo 		 * XXX maybe better to just expand
213732176cfdSRui Paulo 		 */
213832176cfdSRui Paulo 		if (ieee80211_ies_init(&ni->ni_ies, sfrm, efrm - sfrm)) {
213932176cfdSRui Paulo #define	setie(_ie, _off)	ieee80211_ies_setie(ni->ni_ies, _ie, _off)
214032176cfdSRui Paulo 			if (wpa != NULL)
214132176cfdSRui Paulo 				setie(wpa_ie, wpa - sfrm);
214232176cfdSRui Paulo 			if (rsn != NULL)
214332176cfdSRui Paulo 				setie(rsn_ie, rsn - sfrm);
214432176cfdSRui Paulo 			if (htcap != NULL)
214532176cfdSRui Paulo 				setie(htcap_ie, htcap - sfrm);
214632176cfdSRui Paulo 			if (wme != NULL) {
214732176cfdSRui Paulo 				setie(wme_ie, wme - sfrm);
214832176cfdSRui Paulo 				/*
214932176cfdSRui Paulo 				 * Mark node as capable of QoS.
215032176cfdSRui Paulo 				 */
215132176cfdSRui Paulo 				ni->ni_flags |= IEEE80211_NODE_QOS;
215232176cfdSRui Paulo 			} else
215332176cfdSRui Paulo 				ni->ni_flags &= ~IEEE80211_NODE_QOS;
215432176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG
215532176cfdSRui Paulo 			if (ath != NULL) {
215632176cfdSRui Paulo 				setie(ath_ie, ath - sfrm);
215732176cfdSRui Paulo 				/*
215832176cfdSRui Paulo 				 * Parse ATH station parameters.
215932176cfdSRui Paulo 				 */
216032176cfdSRui Paulo 				ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
216132176cfdSRui Paulo 			} else
216232176cfdSRui Paulo #endif
216332176cfdSRui Paulo 				ni->ni_ath_flags = 0;
216432176cfdSRui Paulo #undef setie
216532176cfdSRui Paulo 		} else {
216632176cfdSRui Paulo 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
216732176cfdSRui Paulo 			ni->ni_ath_flags = 0;
216832176cfdSRui Paulo 		}
216932176cfdSRui Paulo 		ieee80211_node_join(ni, resp);
217032176cfdSRui Paulo 		ieee80211_deliver_l2uf(ni);
217132176cfdSRui Paulo 		break;
217232176cfdSRui Paulo 	}
217332176cfdSRui Paulo 
217432176cfdSRui Paulo 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
217532176cfdSRui Paulo 	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
217632176cfdSRui Paulo 		uint16_t reason;
217732176cfdSRui Paulo 
217832176cfdSRui Paulo 		if (vap->iv_state != IEEE80211_S_RUN ||
217932176cfdSRui Paulo 		    /* NB: can happen when in promiscuous mode */
218032176cfdSRui Paulo 		    !IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
218132176cfdSRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++;
218232176cfdSRui Paulo 			break;
218332176cfdSRui Paulo 		}
218432176cfdSRui Paulo 		/*
218532176cfdSRui Paulo 		 * deauth/disassoc frame format
218632176cfdSRui Paulo 		 *	[2] reason
218732176cfdSRui Paulo 		 */
218832176cfdSRui Paulo 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
218932176cfdSRui Paulo 		reason = le16toh(*(uint16_t *)frm);
219032176cfdSRui Paulo 		if (subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
219132176cfdSRui Paulo 			vap->iv_stats.is_rx_deauth++;
219232176cfdSRui Paulo 			IEEE80211_NODE_STAT(ni, rx_deauth);
219332176cfdSRui Paulo 		} else {
219432176cfdSRui Paulo 			vap->iv_stats.is_rx_disassoc++;
219532176cfdSRui Paulo 			IEEE80211_NODE_STAT(ni, rx_disassoc);
219632176cfdSRui Paulo 		}
219732176cfdSRui Paulo 		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2198*4f655ef5SMatthew Dillon 		    "recv %s (reason: %d (%s))",
2199*4f655ef5SMatthew Dillon 		    ieee80211_mgt_subtype_name(subtype),
2200*4f655ef5SMatthew Dillon 		    reason, ieee80211_reason_to_string(reason));
220132176cfdSRui Paulo 		if (ni != vap->iv_bss)
220232176cfdSRui Paulo 			ieee80211_node_leave(ni);
220332176cfdSRui Paulo 		break;
220432176cfdSRui Paulo 	}
220532176cfdSRui Paulo 
220632176cfdSRui Paulo 	case IEEE80211_FC0_SUBTYPE_ACTION:
2207085ff963SMatthew Dillon 	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2208085ff963SMatthew Dillon 		if (ni == vap->iv_bss) {
2209085ff963SMatthew Dillon 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2210085ff963SMatthew Dillon 			    wh, NULL, "%s", "unknown node");
221132176cfdSRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++;
2212085ff963SMatthew Dillon 		} else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2213085ff963SMatthew Dillon 		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2214085ff963SMatthew Dillon 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2215085ff963SMatthew Dillon 			    wh, NULL, "%s", "not for us");
2216085ff963SMatthew Dillon 			vap->iv_stats.is_rx_mgtdiscard++;
2217085ff963SMatthew Dillon 		} else if (vap->iv_state != IEEE80211_S_RUN) {
2218085ff963SMatthew Dillon 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2219085ff963SMatthew Dillon 			    wh, NULL, "wrong state %s",
2220085ff963SMatthew Dillon 			    ieee80211_state_name[vap->iv_state]);
2221085ff963SMatthew Dillon 			vap->iv_stats.is_rx_mgtdiscard++;
2222085ff963SMatthew Dillon 		} else {
2223085ff963SMatthew Dillon 			if (ieee80211_parse_action(ni, m0) == 0)
2224085ff963SMatthew Dillon 				(void)ic->ic_recv_action(ni, wh, frm, efrm);
2225085ff963SMatthew Dillon 		}
222632176cfdSRui Paulo 		break;
222732176cfdSRui Paulo 
222832176cfdSRui Paulo 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
222932176cfdSRui Paulo 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2230*4f655ef5SMatthew Dillon 	case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
2231085ff963SMatthew Dillon 	case IEEE80211_FC0_SUBTYPE_ATIM:
2232085ff963SMatthew Dillon 		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2233085ff963SMatthew Dillon 		    wh, NULL, "%s", "not handled");
2234085ff963SMatthew Dillon 		vap->iv_stats.is_rx_mgtdiscard++;
2235085ff963SMatthew Dillon 		break;
2236085ff963SMatthew Dillon 
223732176cfdSRui Paulo 	default:
223832176cfdSRui Paulo 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
223932176cfdSRui Paulo 		    wh, "mgt", "subtype 0x%x not handled", subtype);
224032176cfdSRui Paulo 		vap->iv_stats.is_rx_badsubtype++;
224132176cfdSRui Paulo 		break;
224232176cfdSRui Paulo 	}
224332176cfdSRui Paulo }
224432176cfdSRui Paulo 
224532176cfdSRui Paulo static void
hostap_recv_ctl(struct ieee80211_node * ni,struct mbuf * m,int subtype)224632176cfdSRui Paulo hostap_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
224732176cfdSRui Paulo {
224832176cfdSRui Paulo 	switch (subtype) {
224932176cfdSRui Paulo 	case IEEE80211_FC0_SUBTYPE_PS_POLL:
2250085ff963SMatthew Dillon 		ni->ni_vap->iv_recv_pspoll(ni, m);
225132176cfdSRui Paulo 		break;
225232176cfdSRui Paulo 	case IEEE80211_FC0_SUBTYPE_BAR:
225332176cfdSRui Paulo 		ieee80211_recv_bar(ni, m);
225432176cfdSRui Paulo 		break;
225532176cfdSRui Paulo 	}
225632176cfdSRui Paulo }
225732176cfdSRui Paulo 
225832176cfdSRui Paulo /*
225932176cfdSRui Paulo  * Process a received ps-poll frame.
226032176cfdSRui Paulo  */
2261085ff963SMatthew Dillon void
ieee80211_recv_pspoll(struct ieee80211_node * ni,struct mbuf * m0)2262085ff963SMatthew Dillon ieee80211_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m0)
226332176cfdSRui Paulo {
226432176cfdSRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
2265085ff963SMatthew Dillon 	struct ieee80211com *ic = vap->iv_ic;
226632176cfdSRui Paulo 	struct ieee80211_frame_min *wh;
226732176cfdSRui Paulo 	struct mbuf *m;
226832176cfdSRui Paulo 	uint16_t aid;
226932176cfdSRui Paulo 	int qlen;
227032176cfdSRui Paulo 
227132176cfdSRui Paulo 	wh = mtod(m0, struct ieee80211_frame_min *);
227232176cfdSRui Paulo 	if (ni->ni_associd == 0) {
227332176cfdSRui Paulo 		IEEE80211_DISCARD(vap,
227432176cfdSRui Paulo 		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
227532176cfdSRui Paulo 		    (struct ieee80211_frame *) wh, NULL,
227632176cfdSRui Paulo 		    "%s", "unassociated station");
227732176cfdSRui Paulo 		vap->iv_stats.is_ps_unassoc++;
227832176cfdSRui Paulo 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
227932176cfdSRui Paulo 			IEEE80211_REASON_NOT_ASSOCED);
228032176cfdSRui Paulo 		return;
228132176cfdSRui Paulo 	}
228232176cfdSRui Paulo 
228332176cfdSRui Paulo 	aid = le16toh(*(uint16_t *)wh->i_dur);
228432176cfdSRui Paulo 	if (aid != ni->ni_associd) {
228532176cfdSRui Paulo 		IEEE80211_DISCARD(vap,
228632176cfdSRui Paulo 		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
228732176cfdSRui Paulo 		    (struct ieee80211_frame *) wh, NULL,
228832176cfdSRui Paulo 		    "aid mismatch: sta aid 0x%x poll aid 0x%x",
228932176cfdSRui Paulo 		    ni->ni_associd, aid);
229032176cfdSRui Paulo 		vap->iv_stats.is_ps_badaid++;
229132176cfdSRui Paulo 		/*
229232176cfdSRui Paulo 		 * NB: We used to deauth the station but it turns out
229332176cfdSRui Paulo 		 * the Blackberry Curve 8230 (and perhaps other devices)
229432176cfdSRui Paulo 		 * sometimes send the wrong AID when WME is negotiated.
229532176cfdSRui Paulo 		 * Being more lenient here seems ok as we already check
229632176cfdSRui Paulo 		 * the station is associated and we only return frames
229732176cfdSRui Paulo 		 * queued for the station (i.e. we don't use the AID).
229832176cfdSRui Paulo 		 */
229932176cfdSRui Paulo 		return;
230032176cfdSRui Paulo 	}
230132176cfdSRui Paulo 
230232176cfdSRui Paulo 	/* Okay, take the first queued packet and put it out... */
230332176cfdSRui Paulo 	m = ieee80211_node_psq_dequeue(ni, &qlen);
230432176cfdSRui Paulo 	if (m == NULL) {
230532176cfdSRui Paulo 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_POWER, wh->i_addr2,
230632176cfdSRui Paulo 		    "%s", "recv ps-poll, but queue empty");
230732176cfdSRui Paulo 		ieee80211_send_nulldata(ieee80211_ref_node(ni));
230832176cfdSRui Paulo 		vap->iv_stats.is_ps_qempty++;	/* XXX node stat */
230932176cfdSRui Paulo 		if (vap->iv_set_tim != NULL)
231032176cfdSRui Paulo 			vap->iv_set_tim(ni, 0);	/* just in case */
231132176cfdSRui Paulo 		return;
231232176cfdSRui Paulo 	}
231332176cfdSRui Paulo 	/*
231432176cfdSRui Paulo 	 * If there are more packets, set the more packets bit
231532176cfdSRui Paulo 	 * in the packet dispatched to the station; otherwise
231632176cfdSRui Paulo 	 * turn off the TIM bit.
231732176cfdSRui Paulo 	 */
231832176cfdSRui Paulo 	if (qlen != 0) {
231932176cfdSRui Paulo 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
232032176cfdSRui Paulo 		    "recv ps-poll, send packet, %u still queued", qlen);
232132176cfdSRui Paulo 		m->m_flags |= M_MORE_DATA;
232232176cfdSRui Paulo 	} else {
232332176cfdSRui Paulo 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
232432176cfdSRui Paulo 		    "%s", "recv ps-poll, send packet, queue empty");
232532176cfdSRui Paulo 		if (vap->iv_set_tim != NULL)
232632176cfdSRui Paulo 			vap->iv_set_tim(ni, 0);
232732176cfdSRui Paulo 	}
232832176cfdSRui Paulo 	m->m_flags |= M_PWR_SAV;		/* bypass PS handling */
232932176cfdSRui Paulo 
2330085ff963SMatthew Dillon 	/*
2331085ff963SMatthew Dillon 	 * Do the right thing; if it's an encap'ed frame then
2332*4f655ef5SMatthew Dillon 	 * call ieee80211_parent_xmitpkt() else
2333085ff963SMatthew Dillon 	 * call ieee80211_vap_xmitpkt().
2334085ff963SMatthew Dillon 	 */
2335085ff963SMatthew Dillon 	if (m->m_flags & M_ENCAP) {
2336*4f655ef5SMatthew Dillon 		(void) ieee80211_parent_xmitpkt(ic, m);
2337085ff963SMatthew Dillon 	} else {
2338085ff963SMatthew Dillon 		(void) ieee80211_vap_xmitpkt(vap, m);
2339085ff963SMatthew Dillon 	}
234032176cfdSRui Paulo }
2341