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