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