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