ieee80211_hostap.c revision 186658
160814Sps/*-
260786Sps * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
3161478Sdelphij * All rights reserved.
460786Sps *
560786Sps * Redistribution and use in source and binary forms, with or without
660786Sps * modification, are permitted provided that the following conditions
760786Sps * are met:
860786Sps * 1. Redistributions of source code must retain the above copyright
960786Sps *    notice, this list of conditions and the following disclaimer.
1060786Sps * 2. Redistributions in binary form must reproduce the above copyright
1160786Sps *    notice, this list of conditions and the following disclaimer in the
1260786Sps *    documentation and/or other materials provided with the distribution.
1360786Sps *
1460786Sps * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1560786Sps * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1660786Sps * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1760786Sps * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1860786Sps * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1960786Sps * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2060786Sps * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2160786Sps * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2260786Sps * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2360786Sps * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2460786Sps */
2560786Sps
2660786Sps#include <sys/cdefs.h>
2760786Sps#ifdef __FreeBSD__
2860786Sps__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_hostap.c 186658 2008-12-31 21:21:46Z sam $");
2960786Sps#endif
3060786Sps
3160786Sps/*
3260786Sps * IEEE 802.11 HOSTAP mode support.
3360786Sps */
3460786Sps#include "opt_inet.h"
3560786Sps#include "opt_wlan.h"
3660786Sps
3760786Sps#include <sys/param.h>
3860786Sps#include <sys/systm.h>
3960786Sps#include <sys/mbuf.h>
4060786Sps#include <sys/malloc.h>
4160786Sps#include <sys/kernel.h>
4260786Sps
4360786Sps#include <sys/socket.h>
4460786Sps#include <sys/sockio.h>
4589022Sps#include <sys/endian.h>
4660786Sps#include <sys/errno.h>
4789022Sps#include <sys/proc.h>
4860786Sps#include <sys/sysctl.h>
4989022Sps
5060786Sps#include <net/if.h>
5189022Sps#include <net/if_media.h>
5260786Sps#include <net/if_llc.h>
5360786Sps#include <net/ethernet.h>
5489022Sps
5589022Sps#include <net/bpf.h>
5660786Sps
5760786Sps#include <net80211/ieee80211_var.h>
5860786Sps#include <net80211/ieee80211_hostap.h>
5960786Sps#include <net80211/ieee80211_input.h>
6089022Sps#include <net80211/ieee80211_wds.h>
6160786Sps
6260786Sps#define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
6360786Sps
6460786Spsstatic	void hostap_vattach(struct ieee80211vap *);
6560786Spsstatic	int hostap_newstate(struct ieee80211vap *, enum ieee80211_state, int);
6660786Spsstatic	int hostap_input(struct ieee80211_node *ni, struct mbuf *m,
6760786Sps	    int rssi, int noise, uint32_t rstamp);
6860786Spsstatic void hostap_deliver_data(struct ieee80211vap *,
6960786Sps	    struct ieee80211_node *, struct mbuf *);
7060786Spsstatic void hostap_recv_mgmt(struct ieee80211_node *, struct mbuf *,
7160786Sps	    int subtype, int rssi, int noise, uint32_t rstamp);
7260786Spsstatic void hostap_recv_pspoll(struct ieee80211_node *, struct mbuf *);
7360786Sps
7460786Spsvoid
7560786Spsieee80211_hostap_attach(struct ieee80211com *ic)
7689022Sps{
7760786Sps	ic->ic_vattach[IEEE80211_M_HOSTAP] = hostap_vattach;
7860786Sps}
7960786Sps
8060786Spsvoid
8160786Spsieee80211_hostap_detach(struct ieee80211com *ic)
8260786Sps{
8360786Sps}
8460786Sps
8560786Spsstatic void
8660786Spshostap_vdetach(struct ieee80211vap *vap)
8760786Sps{
8860786Sps}
8960786Sps
9060786Spsstatic void
9160786Spshostap_vattach(struct ieee80211vap *vap)
9260786Sps{
9360786Sps	vap->iv_newstate = hostap_newstate;
9460786Sps	vap->iv_input = hostap_input;
9560786Sps	vap->iv_recv_mgmt = hostap_recv_mgmt;
9660786Sps	vap->iv_opdetach = hostap_vdetach;
9760786Sps	vap->iv_deliver_data = hostap_deliver_data;
9860786Sps}
9960786Sps
10060786Spsstatic void
10160786Spssta_disassoc(void *arg, struct ieee80211_node *ni)
10260786Sps{
10360786Sps	struct ieee80211vap *vap = arg;
10460786Sps
10560786Sps	if (ni->ni_vap == vap && ni->ni_associd != 0) {
10660786Sps		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
10760786Sps			IEEE80211_REASON_ASSOC_LEAVE);
10860786Sps		ieee80211_node_leave(ni);
10960786Sps	}
11060786Sps}
11160786Sps
11260786Sps/*
11360786Sps * IEEE80211_M_HOSTAP vap state machine handler.
11460786Sps */
11560786Spsstatic int
11660786Spshostap_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
117128348Stjr{
118128348Stjr	struct ieee80211com *ic = vap->iv_ic;
119128348Stjr	enum ieee80211_state ostate;
120128348Stjr
12160786Sps	IEEE80211_LOCK_ASSERT(ic);
12260786Sps
12360786Sps	ostate = vap->iv_state;
124128348Stjr	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
125128348Stjr	    __func__, ieee80211_state_name[ostate],
126128348Stjr	    ieee80211_state_name[nstate], arg);
127128348Stjr	vap->iv_state = nstate;			/* state transition */
128128348Stjr	if (ostate != IEEE80211_S_SCAN)
129128348Stjr		ieee80211_cancel_scan(vap);	/* background scan */
130128348Stjr	switch (nstate) {
131128348Stjr	case IEEE80211_S_INIT:
132128348Stjr		switch (ostate) {
133128348Stjr		case IEEE80211_S_SCAN:
134128348Stjr			ieee80211_cancel_scan(vap);
135128348Stjr			break;
136128348Stjr		case IEEE80211_S_CAC:
13760786Sps			ieee80211_dfs_cac_stop(vap);
13860786Sps			break;
13960786Sps		case IEEE80211_S_RUN:
140128348Stjr			ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
141128348Stjr			break;
14260786Sps		default:
143128348Stjr			break;
14460786Sps		}
145128348Stjr		if (ostate != IEEE80211_S_INIT) {
14660786Sps			/* NB: optimize INIT -> INIT case */
14760786Sps			ieee80211_reset_bss(vap);
14860786Sps		}
14960786Sps		if (vap->iv_auth->ia_detach != NULL)
15060786Sps			vap->iv_auth->ia_detach(vap);
15160786Sps		break;
15260786Sps	case IEEE80211_S_SCAN:
15360786Sps		switch (ostate) {
15460786Sps		case IEEE80211_S_CSA:
15560786Sps		case IEEE80211_S_RUN:
15660786Sps			ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
15760786Sps			/*
15860786Sps			 * Clear overlapping BSS state; the beacon frame
15960786Sps			 * will be reconstructed on transition to the RUN
16060786Sps			 * state and the timeout routines check if the flag
16160786Sps			 * is set before doing anything so this is sufficient.
16260786Sps			 */
16360786Sps			ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
16460786Sps			ic->ic_flags_ext &= ~IEEE80211_FEXT_NONHT_PR;
16560786Sps			/* fall thru... */
16660786Sps		case IEEE80211_S_CAC:
16760786Sps			/*
168161478Sdelphij			 * NB: We may get here because of a manual channel
16960786Sps			 *     change in which case we need to stop CAC
17060786Sps			 * XXX no need to stop if ostate RUN but it's ok
17160786Sps			 */
17260786Sps			ieee80211_dfs_cac_stop(vap);
17360786Sps			/* fall thru... */
17460786Sps		case IEEE80211_S_INIT:
17560786Sps			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
17660786Sps			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
17760786Sps				/*
17860786Sps				 * Already have a channel; bypass the
17960786Sps				 * scan and startup immediately.
18060786Sps				 * ieee80211_create_ibss will call back to
18160786Sps				 * move us to RUN state.
18260786Sps				 */
18360786Sps				ieee80211_create_ibss(vap, vap->iv_des_chan);
18460786Sps				break;
18560786Sps			}
18660786Sps			/*
18760786Sps			 * Initiate a scan.  We can come here as a result
18860786Sps			 * of an IEEE80211_IOC_SCAN_REQ too in which case
18960786Sps			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
19060786Sps			 * and the scan request parameters will be present
19160786Sps			 * in iv_scanreq.  Otherwise we do the default.
19260786Sps			 */
19360786Sps			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
19460786Sps				ieee80211_check_scan(vap,
19560786Sps				    vap->iv_scanreq_flags,
19660786Sps				    vap->iv_scanreq_duration,
19760786Sps				    vap->iv_scanreq_mindwell,
19860786Sps				    vap->iv_scanreq_maxdwell,
19960786Sps				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
20060786Sps				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
20160786Sps			} else
20260786Sps				ieee80211_check_scan_current(vap);
20360786Sps			break;
204161478Sdelphij		case IEEE80211_S_SCAN:
20560786Sps			/*
20660786Sps			 * A state change requires a reset; scan.
207128348Stjr			 */
20889022Sps			ieee80211_check_scan_current(vap);
209128348Stjr			break;
210128348Stjr		default:
211128348Stjr			break;
21260786Sps		}
213128348Stjr		break;
21489022Sps	case IEEE80211_S_CAC:
215128348Stjr		/*
216128348Stjr		 * Start CAC on a DFS channel.  We come here when starting
217128348Stjr		 * a bss on a DFS channel (see ieee80211_create_ibss).
21860786Sps		 */
21960786Sps		ieee80211_dfs_cac_start(vap);
22060786Sps		break;
22160786Sps	case IEEE80211_S_RUN:
22260786Sps		if (vap->iv_flags & IEEE80211_F_WPA) {
22360786Sps			/* XXX validate prerequisites */
22460786Sps		}
22560786Sps		switch (ostate) {
22660786Sps		case IEEE80211_S_INIT:
22760786Sps			/*
22860786Sps			 * Already have a channel; bypass the
229128348Stjr			 * scan and startup immediately.
23089022Sps			 * Note that ieee80211_create_ibss will call
23189022Sps			 * back to do a RUN->RUN state change.
232128348Stjr			 */
23360786Sps			ieee80211_create_ibss(vap,
23460786Sps			    ieee80211_ht_adjust_channel(ic,
23560786Sps				ic->ic_curchan, vap->iv_flags_ext));
23660786Sps			/* NB: iv_bss is changed on return */
23760786Sps			break;
23860786Sps		case IEEE80211_S_CAC:
23960786Sps			/*
24060786Sps			 * NB: This is the normal state change when CAC
24160786Sps			 * expires and no radar was detected; no need to
24260786Sps			 * clear the CAC timer as it's already expired.
24360786Sps			 */
24460786Sps			/* fall thru... */
24560786Sps		case IEEE80211_S_CSA:
24660786Sps			/*
24760786Sps			 * Update bss node channel to reflect where
24860786Sps			 * we landed after CSA.
24960786Sps			 */
25060786Sps			ieee80211_node_set_chan(vap->iv_bss,
25160786Sps			    ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
25260786Sps				ieee80211_htchanflags(vap->iv_bss->ni_chan)));
25360786Sps			/* XXX bypass debug msgs */
254128348Stjr			break;
255128348Stjr		case IEEE80211_S_SCAN:
25660786Sps		case IEEE80211_S_RUN:
25760786Sps#ifdef IEEE80211_DEBUG
258161478Sdelphij			if (ieee80211_msg_debug(vap)) {
259161478Sdelphij				struct ieee80211_node *ni = vap->iv_bss;
260161478Sdelphij				ieee80211_note(vap,
26160786Sps				    "synchronized with %s ssid ",
26260786Sps				    ether_sprintf(ni->ni_bssid));
26360786Sps				ieee80211_print_essid(ni->ni_essid,
26460786Sps				    ni->ni_esslen);
26560786Sps				/* XXX MCS/HT */
26660786Sps				printf(" channel %d start %uMb\n",
26760786Sps				    ieee80211_chan2ieee(ic, ic->ic_curchan),
26860786Sps				    IEEE80211_RATE2MBS(ni->ni_txrate));
26960786Sps			}
27060786Sps#endif
27160786Sps			break;
27260786Sps		default:
27360786Sps			break;
274128348Stjr		}
275128348Stjr		/*
276161478Sdelphij		 * Start/stop the authenticator.  We delay until here
27760786Sps		 * to allow configuration to happen out of order.
27860786Sps		 */
27960786Sps		if (vap->iv_auth->ia_attach != NULL) {
280161478Sdelphij			/* XXX check failure */
281161478Sdelphij			vap->iv_auth->ia_attach(vap);
28260786Sps		} else if (vap->iv_auth->ia_detach != NULL) {
283161478Sdelphij			vap->iv_auth->ia_detach(vap);
28460786Sps		}
285161478Sdelphij		ieee80211_node_authorize(vap->iv_bss);
286161478Sdelphij		break;
287161478Sdelphij	default:
28860786Sps		break;
289161478Sdelphij	}
290161478Sdelphij	return 0;
291161478Sdelphij}
292161478Sdelphij
293161478Sdelphijstatic void
294161478Sdelphijhostap_deliver_data(struct ieee80211vap *vap,
295161478Sdelphij	struct ieee80211_node *ni, struct mbuf *m)
29660786Sps{
29760786Sps	struct ether_header *eh = mtod(m, struct ether_header *);
29860786Sps	struct ifnet *ifp = vap->iv_ifp;
29960786Sps
30060786Sps	KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
30160786Sps	    ("gack, opmode %d", vap->iv_opmode));
30260786Sps	/*
303128348Stjr	 * Do accounting.
30460786Sps	 */
30560786Sps	ifp->if_ipackets++;
306128348Stjr	IEEE80211_NODE_STAT(ni, rx_data);
30789022Sps	IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
30889022Sps	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
30989022Sps		m->m_flags |= M_MCAST;		/* XXX M_BCAST? */
310128348Stjr		IEEE80211_NODE_STAT(ni, rx_mcast);
31189022Sps	} else
31260786Sps		IEEE80211_NODE_STAT(ni, rx_ucast);
31360786Sps
314128348Stjr	/* clear driver/net80211 flags before passing up */
315128348Stjr	m->m_flags &= ~M_80211_RX;
316128348Stjr
31760786Sps	/* perform as a bridge within the AP */
31860786Sps	if ((vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0) {
31960786Sps		struct mbuf *mcopy = NULL;
32060786Sps
32160786Sps		if (m->m_flags & M_MCAST) {
32260786Sps			mcopy = m_dup(m, M_DONTWAIT);
323128348Stjr			if (mcopy == NULL)
32460786Sps				ifp->if_oerrors++;
32560786Sps			else
326128348Stjr				mcopy->m_flags |= M_MCAST;
32760786Sps		} else {
32860786Sps			/*
329128348Stjr			 * Check if the destination is associated with the
33089022Sps			 * same vap and authorized to receive traffic.
33189022Sps			 * Beware of traffic destined for the vap itself;
33289022Sps			 * sending it will not work; just let it be delivered
33389022Sps			 * normally.
334128348Stjr			 */
33589022Sps			struct ieee80211_node *sta = ieee80211_find_vap_node(
33660786Sps			     &vap->iv_ic->ic_sta, vap, eh->ether_dhost);
33760786Sps			if (sta != NULL) {
33860786Sps				if (ieee80211_node_is_authorized(sta)) {
33960786Sps					/*
34060786Sps					 * Beware of sending to ourself; this
34160786Sps					 * needs to happen via the normal
34260786Sps					 * input path.
34360786Sps					 */
34460786Sps					if (sta != vap->iv_bss) {
34560786Sps						mcopy = m;
346128348Stjr						m = NULL;
347128348Stjr					}
34860786Sps				} else {
349128348Stjr					vap->iv_stats.is_rx_unauth++;
35060786Sps					IEEE80211_NODE_STAT(sta, rx_unauth);
35160786Sps				}
352128348Stjr				ieee80211_free_node(sta);
35360786Sps			}
35460786Sps		}
35560786Sps		if (mcopy != NULL) {
35660786Sps			int len, err;
35760786Sps			len = mcopy->m_pkthdr.len;
35860786Sps			err = ifp->if_transmit(ifp, mcopy);
35960786Sps			if (err) {
36060786Sps				/* NB: IFQ_HANDOFF reclaims mcopy */
36160786Sps			} else {
36260786Sps				ifp->if_opackets++;
36360786Sps			}
36460786Sps		}
36560786Sps	}
36689022Sps	if (m != NULL) {
367128348Stjr		/*
36889022Sps		 * Mark frame as coming from vap's interface.
36989022Sps		 */
37089022Sps		m->m_pkthdr.rcvif = ifp;
371128348Stjr		if (m->m_flags & M_MCAST) {
37289022Sps			/*
37389022Sps			 * Spam DWDS vap's w/ multicast traffic.
37460786Sps			 */
37560786Sps			/* XXX only if dwds in use? */
37660786Sps			ieee80211_dwds_mcast(vap, m);
377128348Stjr		}
378128348Stjr		if (ni->ni_vlan != 0) {
37960786Sps			/* attach vlan tag */
38060786Sps			m->m_pkthdr.ether_vtag = ni->ni_vlan;
38160786Sps			m->m_flags |= M_VLANTAG;
38260786Sps		}
38360786Sps		ifp->if_input(ifp, m);
38460786Sps	}
38560786Sps}
38660786Sps
38760786Sps/*
38860786Sps * Decide if a received management frame should be
38960786Sps * printed when debugging is enabled.  This filters some
39060786Sps * of the less interesting frames that come frequently
39160786Sps * (e.g. beacons).
39260786Sps */
39360786Spsstatic __inline int
39460786Spsdoprint(struct ieee80211vap *vap, int subtype)
39560786Sps{
39660786Sps	switch (subtype) {
39760786Sps	case IEEE80211_FC0_SUBTYPE_BEACON:
39860786Sps		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
39960786Sps	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
40060786Sps		return 0;
40160786Sps	}
40260786Sps	return 1;
40360786Sps}
40460786Sps
40560786Sps/*
40660786Sps * Process a received frame.  The node associated with the sender
40760786Sps * should be supplied.  If nothing was found in the node table then
40860786Sps * the caller is assumed to supply a reference to iv_bss instead.
40960786Sps * The RSSI and a timestamp are also supplied.  The RSSI data is used
41060786Sps * during AP scanning to select a AP to associate with; it can have
41160786Sps * any units so long as values have consistent units and higher values
41260786Sps * mean ``better signal''.  The receive timestamp is currently not used
41360786Sps * by the 802.11 layer.
41460786Sps */
41560786Spsstatic int
41660786Spshostap_input(struct ieee80211_node *ni, struct mbuf *m,
41760786Sps	int rssi, int noise, uint32_t rstamp)
41860786Sps{
41960786Sps#define	SEQ_LEQ(a,b)	((int)((a)-(b)) <= 0)
42060786Sps#define	HAS_SEQ(type)	((type & 0x4) == 0)
42160786Sps	struct ieee80211vap *vap = ni->ni_vap;
42260786Sps	struct ieee80211com *ic = ni->ni_ic;
42360786Sps	struct ifnet *ifp = vap->iv_ifp;
42460786Sps	struct ieee80211_frame *wh;
42560786Sps	struct ieee80211_key *key;
42660786Sps	struct ether_header *eh;
42760786Sps	int hdrspace, need_tap;
42860786Sps	uint8_t dir, type, subtype, qos;
42960786Sps	uint8_t *bssid;
43060786Sps	uint16_t rxseq;
43160786Sps
43260786Sps	if (m->m_flags & M_AMPDU_MPDU) {
43360786Sps		/*
43460786Sps		 * Fastpath for A-MPDU reorder q resubmission.  Frames
43560786Sps		 * w/ M_AMPDU_MPDU marked have already passed through
43660786Sps		 * here but were received out of order and been held on
43760786Sps		 * the reorder queue.  When resubmitted they are marked
43860786Sps		 * with the M_AMPDU_MPDU flag and we can bypass most of
43960786Sps		 * the normal processing.
44060786Sps		 */
44160786Sps		wh = mtod(m, struct ieee80211_frame *);
44260786Sps		type = IEEE80211_FC0_TYPE_DATA;
44360786Sps		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
44460786Sps		subtype = IEEE80211_FC0_SUBTYPE_QOS;
44560786Sps		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
44660786Sps		goto resubmit_ampdu;
44760786Sps	}
44860786Sps
44960786Sps	KASSERT(ni != NULL, ("null node"));
45060786Sps	ni->ni_inact = ni->ni_inact_reload;
45160786Sps
45260786Sps	need_tap = 1;			/* mbuf need to be tapped. */
45360786Sps	type = -1;			/* undefined */
45460786Sps
45560786Sps	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
45660786Sps		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
45760786Sps		    ni->ni_macaddr, NULL,
45860786Sps		    "too short (1): len %u", m->m_pkthdr.len);
45960786Sps		vap->iv_stats.is_rx_tooshort++;
46060786Sps		goto out;
46160786Sps	}
46260786Sps	/*
46360786Sps	 * Bit of a cheat here, we use a pointer for a 3-address
46460786Sps	 * frame format but don't reference fields past outside
46560786Sps	 * ieee80211_frame_min w/o first validating the data is
46660786Sps	 * present.
46760786Sps	 */
46860786Sps	wh = mtod(m, struct ieee80211_frame *);
46960786Sps
47060786Sps	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
47160786Sps	    IEEE80211_FC0_VERSION_0) {
47260786Sps		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
47360786Sps		    ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
47460786Sps		vap->iv_stats.is_rx_badversion++;
47560786Sps		goto err;
47660786Sps	}
47760786Sps
47860786Sps	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
47960786Sps	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
48060786Sps	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
48160786Sps	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
48260786Sps		if (dir != IEEE80211_FC1_DIR_NODS)
48360786Sps			bssid = wh->i_addr1;
48460786Sps		else if (type == IEEE80211_FC0_TYPE_CTL)
48560786Sps			bssid = wh->i_addr1;
48660786Sps		else {
48760786Sps			if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
48860786Sps				IEEE80211_DISCARD_MAC(vap,
48960786Sps				    IEEE80211_MSG_ANY, ni->ni_macaddr,
49060786Sps				    NULL, "too short (2): len %u",
49160786Sps				    m->m_pkthdr.len);
49260786Sps				vap->iv_stats.is_rx_tooshort++;
49360786Sps				goto out;
49460786Sps			}
49560786Sps			bssid = wh->i_addr3;
49660786Sps		}
49760786Sps		/*
49860786Sps		 * Validate the bssid.
49960786Sps		 */
50060786Sps		if (!(type == IEEE80211_FC0_TYPE_MGT &&
50160786Sps		      subtype == IEEE80211_FC0_SUBTYPE_BEACON) &&
50260786Sps		    !IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
50360786Sps		    !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
50460786Sps			/* not interested in */
50560786Sps			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
50660786Sps			    bssid, NULL, "%s", "not to bss");
50760786Sps			vap->iv_stats.is_rx_wrongbss++;
50860786Sps			goto out;
50960786Sps		}
51060786Sps
51160786Sps		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
51260786Sps		ni->ni_noise = noise;
51360786Sps		ni->ni_rstamp = rstamp;
51460786Sps		if (HAS_SEQ(type)) {
51560786Sps			uint8_t tid = ieee80211_gettid(wh);
51660786Sps			if (IEEE80211_QOS_HAS_SEQ(wh) &&
51760786Sps			    TID_TO_WME_AC(tid) >= WME_AC_VI)
51860786Sps				ic->ic_wme.wme_hipri_traffic++;
51960786Sps			rxseq = le16toh(*(uint16_t *)wh->i_seq);
52060786Sps			if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
52160786Sps			    (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
52260786Sps			    SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
52360786Sps				/* duplicate, discard */
52460786Sps				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
52560786Sps				    bssid, "duplicate",
52660786Sps				    "seqno <%u,%u> fragno <%u,%u> tid %u",
52760786Sps				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
52860786Sps				    ni->ni_rxseqs[tid] >>
52960786Sps					IEEE80211_SEQ_SEQ_SHIFT,
53060786Sps				    rxseq & IEEE80211_SEQ_FRAG_MASK,
53160786Sps				    ni->ni_rxseqs[tid] &
53260786Sps					IEEE80211_SEQ_FRAG_MASK,
53360786Sps				    tid);
534161478Sdelphij				vap->iv_stats.is_rx_dup++;
53560786Sps				IEEE80211_NODE_STAT(ni, rx_dup);
53660786Sps				goto out;
53760786Sps			}
53860786Sps			ni->ni_rxseqs[tid] = rxseq;
53960786Sps		}
54060786Sps	}
54160786Sps
54260786Sps	switch (type) {
54360786Sps	case IEEE80211_FC0_TYPE_DATA:
54460786Sps		hdrspace = ieee80211_hdrspace(ic, wh);
54560786Sps		if (m->m_len < hdrspace &&
54660786Sps		    (m = m_pullup(m, hdrspace)) == NULL) {
54760786Sps			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
54860786Sps			    ni->ni_macaddr, NULL,
54960786Sps			    "data too short: expecting %u", hdrspace);
55060786Sps			vap->iv_stats.is_rx_tooshort++;
55160786Sps			goto out;		/* XXX */
55260786Sps		}
55360786Sps		if (!(dir == IEEE80211_FC1_DIR_TODS ||
55460786Sps		     (dir == IEEE80211_FC1_DIR_DSTODS &&
55560786Sps		      (vap->iv_flags & IEEE80211_F_DWDS)))) {
55660786Sps			if (dir != IEEE80211_FC1_DIR_DSTODS) {
55760786Sps				IEEE80211_DISCARD(vap,
55860786Sps				    IEEE80211_MSG_INPUT, wh, "data",
55960786Sps				    "incorrect dir 0x%x", dir);
56060786Sps			} else {
56160786Sps				IEEE80211_DISCARD(vap,
56260786Sps				    IEEE80211_MSG_INPUT |
56360786Sps				    IEEE80211_MSG_WDS, wh,
56489022Sps				    "4-address data",
56589022Sps				    "%s", "DWDS not enabled");
56689022Sps			}
56789022Sps			vap->iv_stats.is_rx_wrongdir++;
56889022Sps			goto out;
56989022Sps		}
57089022Sps		/* check if source STA is associated */
57160786Sps		if (ni == vap->iv_bss) {
57289022Sps			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
57389022Sps			    wh, "data", "%s", "unknown src");
57489022Sps			ieee80211_send_error(ni, wh->i_addr2,
57589022Sps			    IEEE80211_FC0_SUBTYPE_DEAUTH,
57689022Sps			    IEEE80211_REASON_NOT_AUTHED);
57789022Sps			vap->iv_stats.is_rx_notassoc++;
57889022Sps			goto err;
57989022Sps		}
58089022Sps		if (ni->ni_associd == 0) {
581			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
582			    wh, "data", "%s", "unassoc src");
583			IEEE80211_SEND_MGMT(ni,
584			    IEEE80211_FC0_SUBTYPE_DISASSOC,
585			    IEEE80211_REASON_NOT_ASSOCED);
586			vap->iv_stats.is_rx_notassoc++;
587			goto err;
588		}
589
590		/*
591		 * Check for power save state change.
592		 * XXX out-of-order A-MPDU frames?
593		 */
594		if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
595		    (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
596			ieee80211_node_pwrsave(ni,
597				wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
598		/*
599		 * For 4-address packets handle WDS discovery
600		 * notifications.  Once a WDS link is setup frames
601		 * are just delivered to the WDS vap (see below).
602		 */
603		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap == NULL) {
604			if (!ieee80211_node_is_authorized(ni)) {
605				IEEE80211_DISCARD(vap,
606				    IEEE80211_MSG_INPUT |
607				    IEEE80211_MSG_WDS, wh,
608				    "4-address data",
609				    "%s", "unauthorized port");
610				vap->iv_stats.is_rx_unauth++;
611				IEEE80211_NODE_STAT(ni, rx_unauth);
612				goto err;
613			}
614			ieee80211_dwds_discover(ni, m);
615			return type;
616		}
617
618		/*
619		 * Handle A-MPDU re-ordering.  If the frame is to be
620		 * processed directly then ieee80211_ampdu_reorder
621		 * will return 0; otherwise it has consumed the mbuf
622		 * and we should do nothing more with it.
623		 */
624		if ((m->m_flags & M_AMPDU) &&
625		    ieee80211_ampdu_reorder(ni, m) != 0) {
626			m = NULL;
627			goto out;
628		}
629	resubmit_ampdu:
630
631		/*
632		 * Handle privacy requirements.  Note that we
633		 * must not be preempted from here until after
634		 * we (potentially) call ieee80211_crypto_demic;
635		 * otherwise we may violate assumptions in the
636		 * crypto cipher modules used to do delayed update
637		 * of replay sequence numbers.
638		 */
639		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
640			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
641				/*
642				 * Discard encrypted frames when privacy is off.
643				 */
644				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
645				    wh, "WEP", "%s", "PRIVACY off");
646				vap->iv_stats.is_rx_noprivacy++;
647				IEEE80211_NODE_STAT(ni, rx_noprivacy);
648				goto out;
649			}
650			key = ieee80211_crypto_decap(ni, m, hdrspace);
651			if (key == NULL) {
652				/* NB: stats+msgs handled in crypto_decap */
653				IEEE80211_NODE_STAT(ni, rx_wepfail);
654				goto out;
655			}
656			wh = mtod(m, struct ieee80211_frame *);
657			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
658		} else {
659			/* XXX M_WEP and IEEE80211_F_PRIVACY */
660			key = NULL;
661		}
662
663		/*
664		 * Save QoS bits for use below--before we strip the header.
665		 */
666		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
667			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
668			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
669			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
670		} else
671			qos = 0;
672
673		/*
674		 * Next up, any fragmentation.
675		 */
676		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
677			m = ieee80211_defrag(ni, m, hdrspace);
678			if (m == NULL) {
679				/* Fragment dropped or frame not complete yet */
680				goto out;
681			}
682		}
683		wh = NULL;		/* no longer valid, catch any uses */
684
685		/*
686		 * Next strip any MSDU crypto bits.
687		 */
688		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
689			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
690			    ni->ni_macaddr, "data", "%s", "demic error");
691			vap->iv_stats.is_rx_demicfail++;
692			IEEE80211_NODE_STAT(ni, rx_demicfail);
693			goto out;
694		}
695		/* copy to listener after decrypt */
696		if (bpf_peers_present(vap->iv_rawbpf))
697			bpf_mtap(vap->iv_rawbpf, m);
698		need_tap = 0;
699		/*
700		 * Finally, strip the 802.11 header.
701		 */
702		m = ieee80211_decap(vap, m, hdrspace);
703		if (m == NULL) {
704			/* XXX mask bit to check for both */
705			/* don't count Null data frames as errors */
706			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
707			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
708				goto out;
709			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
710			    ni->ni_macaddr, "data", "%s", "decap error");
711			vap->iv_stats.is_rx_decap++;
712			IEEE80211_NODE_STAT(ni, rx_decap);
713			goto err;
714		}
715		eh = mtod(m, struct ether_header *);
716		if (!ieee80211_node_is_authorized(ni)) {
717			/*
718			 * Deny any non-PAE frames received prior to
719			 * authorization.  For open/shared-key
720			 * authentication the port is mark authorized
721			 * after authentication completes.  For 802.1x
722			 * the port is not marked authorized by the
723			 * authenticator until the handshake has completed.
724			 */
725			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
726				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
727				    eh->ether_shost, "data",
728				    "unauthorized port: ether type 0x%x len %u",
729				    eh->ether_type, m->m_pkthdr.len);
730				vap->iv_stats.is_rx_unauth++;
731				IEEE80211_NODE_STAT(ni, rx_unauth);
732				goto err;
733			}
734		} else {
735			/*
736			 * When denying unencrypted frames, discard
737			 * any non-PAE frames received without encryption.
738			 */
739			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
740			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
741			    eh->ether_type != htons(ETHERTYPE_PAE)) {
742				/*
743				 * Drop unencrypted frames.
744				 */
745				vap->iv_stats.is_rx_unencrypted++;
746				IEEE80211_NODE_STAT(ni, rx_unencrypted);
747				goto out;
748			}
749		}
750		/* XXX require HT? */
751		if (qos & IEEE80211_QOS_AMSDU) {
752			m = ieee80211_decap_amsdu(ni, m);
753			if (m == NULL)
754				return IEEE80211_FC0_TYPE_DATA;
755		} else if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) &&
756#define	FF_LLC_SIZE	(sizeof(struct ether_header) + sizeof(struct llc))
757		    m->m_pkthdr.len >= 3*FF_LLC_SIZE) {
758			struct llc *llc;
759
760			/*
761			 * Check for fast-frame tunnel encapsulation.
762			 */
763			if (m->m_len < FF_LLC_SIZE &&
764			    (m = m_pullup(m, FF_LLC_SIZE)) == NULL) {
765				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
766				    ni->ni_macaddr, "fast-frame",
767				    "%s", "m_pullup(llc) failed");
768				vap->iv_stats.is_rx_tooshort++;
769				return IEEE80211_FC0_TYPE_DATA;
770			}
771			llc = (struct llc *)(mtod(m, uint8_t *) +
772				sizeof(struct ether_header));
773			if (llc->llc_snap.ether_type == htons(ATH_FF_ETH_TYPE)) {
774				m_adj(m, FF_LLC_SIZE);
775				m = ieee80211_decap_fastframe(ni, m);
776				if (m == NULL)
777					return IEEE80211_FC0_TYPE_DATA;
778			}
779		}
780#undef FF_LLC_SIZE
781		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
782			ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
783		else
784			hostap_deliver_data(vap, ni, m);
785		return IEEE80211_FC0_TYPE_DATA;
786
787	case IEEE80211_FC0_TYPE_MGT:
788		vap->iv_stats.is_rx_mgmt++;
789		IEEE80211_NODE_STAT(ni, rx_mgmt);
790		if (dir != IEEE80211_FC1_DIR_NODS) {
791			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
792			    wh, "mgt", "incorrect dir 0x%x", dir);
793			vap->iv_stats.is_rx_wrongdir++;
794			goto err;
795		}
796		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
797			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
798			    ni->ni_macaddr, "mgt", "too short: len %u",
799			    m->m_pkthdr.len);
800			vap->iv_stats.is_rx_tooshort++;
801			goto out;
802		}
803		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
804			/* ensure return frames are unicast */
805			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
806			    wh, NULL, "source is multicast: %s",
807			    ether_sprintf(wh->i_addr2));
808			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
809			goto out;
810		}
811#ifdef IEEE80211_DEBUG
812		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
813		    ieee80211_msg_dumppkts(vap)) {
814			if_printf(ifp, "received %s from %s rssi %d\n",
815			    ieee80211_mgt_subtype_name[subtype >>
816				IEEE80211_FC0_SUBTYPE_SHIFT],
817			    ether_sprintf(wh->i_addr2), rssi);
818		}
819#endif
820		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
821			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
822				/*
823				 * Only shared key auth frames with a challenge
824				 * should be encrypted, discard all others.
825				 */
826				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
827				    wh, NULL,
828				    "%s", "WEP set but not permitted");
829				vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
830				goto out;
831			}
832			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
833				/*
834				 * Discard encrypted frames when privacy is off.
835				 */
836				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
837				    wh, NULL, "%s", "WEP set but PRIVACY off");
838				vap->iv_stats.is_rx_noprivacy++;
839				goto out;
840			}
841			hdrspace = ieee80211_hdrspace(ic, wh);
842			key = ieee80211_crypto_decap(ni, m, hdrspace);
843			if (key == NULL) {
844				/* NB: stats+msgs handled in crypto_decap */
845				goto out;
846			}
847			wh = mtod(m, struct ieee80211_frame *);
848			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
849		}
850		if (bpf_peers_present(vap->iv_rawbpf))
851			bpf_mtap(vap->iv_rawbpf, m);
852		vap->iv_recv_mgmt(ni, m, subtype, rssi, noise, rstamp);
853		m_freem(m);
854		return IEEE80211_FC0_TYPE_MGT;
855
856	case IEEE80211_FC0_TYPE_CTL:
857		vap->iv_stats.is_rx_ctl++;
858		IEEE80211_NODE_STAT(ni, rx_ctrl);
859		switch (subtype) {
860		case IEEE80211_FC0_SUBTYPE_PS_POLL:
861			hostap_recv_pspoll(ni, m);
862			break;
863		case IEEE80211_FC0_SUBTYPE_BAR:
864			ieee80211_recv_bar(ni, m);
865			break;
866		}
867		goto out;
868	default:
869		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
870		    wh, "bad", "frame type 0x%x", type);
871		/* should not come here */
872		break;
873	}
874err:
875	ifp->if_ierrors++;
876out:
877	if (m != NULL) {
878		if (bpf_peers_present(vap->iv_rawbpf) && need_tap)
879			bpf_mtap(vap->iv_rawbpf, m);
880		m_freem(m);
881	}
882	return type;
883#undef SEQ_LEQ
884}
885
886static void
887hostap_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
888    int rssi, int noise, uint32_t rstamp, uint16_t seq, uint16_t status)
889{
890	struct ieee80211vap *vap = ni->ni_vap;
891
892	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
893
894	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
895		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
896		    ni->ni_macaddr, "open auth",
897		    "bad sta auth mode %u", ni->ni_authmode);
898		vap->iv_stats.is_rx_bad_auth++;	/* XXX */
899		/*
900		 * Clear any challenge text that may be there if
901		 * a previous shared key auth failed and then an
902		 * open auth is attempted.
903		 */
904		if (ni->ni_challenge != NULL) {
905			free(ni->ni_challenge, M_80211_NODE);
906			ni->ni_challenge = NULL;
907		}
908		/* XXX hack to workaround calling convention */
909		ieee80211_send_error(ni, wh->i_addr2,
910		    IEEE80211_FC0_SUBTYPE_AUTH,
911		    (seq + 1) | (IEEE80211_STATUS_ALG<<16));
912		return;
913	}
914	if (seq != IEEE80211_AUTH_OPEN_REQUEST) {
915		vap->iv_stats.is_rx_bad_auth++;
916		return;
917	}
918	/* always accept open authentication requests */
919	if (ni == vap->iv_bss) {
920		ni = ieee80211_dup_bss(vap, wh->i_addr2);
921		if (ni == NULL)
922			return;
923	} else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
924		(void) ieee80211_ref_node(ni);
925	/*
926	 * Mark the node as referenced to reflect that it's
927	 * reference count has been bumped to insure it remains
928	 * after the transaction completes.
929	 */
930	ni->ni_flags |= IEEE80211_NODE_AREF;
931	/*
932	 * Mark the node as requiring a valid association id
933	 * before outbound traffic is permitted.
934	 */
935	ni->ni_flags |= IEEE80211_NODE_ASSOCID;
936
937	if (vap->iv_acl != NULL &&
938	    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
939		/*
940		 * When the ACL policy is set to RADIUS we defer the
941		 * authorization to a user agent.  Dispatch an event,
942		 * a subsequent MLME call will decide the fate of the
943		 * station.  If the user agent is not present then the
944		 * node will be reclaimed due to inactivity.
945		 */
946		IEEE80211_NOTE_MAC(vap,
947		    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL, ni->ni_macaddr,
948		    "%s", "station authentication defered (radius acl)");
949		ieee80211_notify_node_auth(ni);
950	} else {
951		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
952		IEEE80211_NOTE_MAC(vap,
953		    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni->ni_macaddr,
954		    "%s", "station authenticated (open)");
955		/*
956		 * When 802.1x is not in use mark the port
957		 * authorized at this point so traffic can flow.
958		 */
959		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
960			ieee80211_node_authorize(ni);
961	}
962}
963
964static void
965hostap_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
966    uint8_t *frm, uint8_t *efrm, int rssi, int noise, uint32_t rstamp,
967    uint16_t seq, uint16_t status)
968{
969	struct ieee80211vap *vap = ni->ni_vap;
970	uint8_t *challenge;
971	int allocbs, estatus;
972
973	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
974
975	/*
976	 * NB: this can happen as we allow pre-shared key
977	 * authentication to be enabled w/o wep being turned
978	 * on so that configuration of these can be done
979	 * in any order.  It may be better to enforce the
980	 * ordering in which case this check would just be
981	 * for sanity/consistency.
982	 */
983	if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
984		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
985		    ni->ni_macaddr, "shared key auth",
986		    "%s", " PRIVACY is disabled");
987		estatus = IEEE80211_STATUS_ALG;
988		goto bad;
989	}
990	/*
991	 * Pre-shared key authentication is evil; accept
992	 * it only if explicitly configured (it is supported
993	 * mainly for compatibility with clients like Mac OS X).
994	 */
995	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
996	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
997		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
998		    ni->ni_macaddr, "shared key auth",
999		    "bad sta auth mode %u", ni->ni_authmode);
1000		vap->iv_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
1001		estatus = IEEE80211_STATUS_ALG;
1002		goto bad;
1003	}
1004
1005	challenge = NULL;
1006	if (frm + 1 < efrm) {
1007		if ((frm[1] + 2) > (efrm - frm)) {
1008			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1009			    ni->ni_macaddr, "shared key auth",
1010			    "ie %d/%d too long",
1011			    frm[0], (frm[1] + 2) - (efrm - frm));
1012			vap->iv_stats.is_rx_bad_auth++;
1013			estatus = IEEE80211_STATUS_CHALLENGE;
1014			goto bad;
1015		}
1016		if (*frm == IEEE80211_ELEMID_CHALLENGE)
1017			challenge = frm;
1018		frm += frm[1] + 2;
1019	}
1020	switch (seq) {
1021	case IEEE80211_AUTH_SHARED_CHALLENGE:
1022	case IEEE80211_AUTH_SHARED_RESPONSE:
1023		if (challenge == NULL) {
1024			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1025			    ni->ni_macaddr, "shared key auth",
1026			    "%s", "no challenge");
1027			vap->iv_stats.is_rx_bad_auth++;
1028			estatus = IEEE80211_STATUS_CHALLENGE;
1029			goto bad;
1030		}
1031		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1032			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1033			    ni->ni_macaddr, "shared key auth",
1034			    "bad challenge len %d", challenge[1]);
1035			vap->iv_stats.is_rx_bad_auth++;
1036			estatus = IEEE80211_STATUS_CHALLENGE;
1037			goto bad;
1038		}
1039	default:
1040		break;
1041	}
1042	switch (seq) {
1043	case IEEE80211_AUTH_SHARED_REQUEST:
1044		if (ni == vap->iv_bss) {
1045			ni = ieee80211_dup_bss(vap, wh->i_addr2);
1046			if (ni == NULL) {
1047				/* NB: no way to return an error */
1048				return;
1049			}
1050			allocbs = 1;
1051		} else {
1052			if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1053				(void) ieee80211_ref_node(ni);
1054			allocbs = 0;
1055		}
1056		/*
1057		 * Mark the node as referenced to reflect that it's
1058		 * reference count has been bumped to insure it remains
1059		 * after the transaction completes.
1060		 */
1061		ni->ni_flags |= IEEE80211_NODE_AREF;
1062		/*
1063		 * Mark the node as requiring a valid associatio id
1064		 * before outbound traffic is permitted.
1065		 */
1066		ni->ni_flags |= IEEE80211_NODE_ASSOCID;
1067		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1068		ni->ni_noise = noise;
1069		ni->ni_rstamp = rstamp;
1070		if (!ieee80211_alloc_challenge(ni)) {
1071			/* NB: don't return error so they rexmit */
1072			return;
1073		}
1074		get_random_bytes(ni->ni_challenge,
1075			IEEE80211_CHALLENGE_LEN);
1076		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1077		    ni, "shared key %sauth request", allocbs ? "" : "re");
1078		/*
1079		 * When the ACL policy is set to RADIUS we defer the
1080		 * authorization to a user agent.  Dispatch an event,
1081		 * a subsequent MLME call will decide the fate of the
1082		 * station.  If the user agent is not present then the
1083		 * node will be reclaimed due to inactivity.
1084		 */
1085		if (vap->iv_acl != NULL &&
1086		    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
1087			IEEE80211_NOTE_MAC(vap,
1088			    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL,
1089			    ni->ni_macaddr,
1090			    "%s", "station authentication defered (radius acl)");
1091			ieee80211_notify_node_auth(ni);
1092			return;
1093		}
1094		break;
1095	case IEEE80211_AUTH_SHARED_RESPONSE:
1096		if (ni == vap->iv_bss) {
1097			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1098			    ni->ni_macaddr, "shared key response",
1099			    "%s", "unknown station");
1100			/* NB: don't send a response */
1101			return;
1102		}
1103		if (ni->ni_challenge == NULL) {
1104			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1105			    ni->ni_macaddr, "shared key response",
1106			    "%s", "no challenge recorded");
1107			vap->iv_stats.is_rx_bad_auth++;
1108			estatus = IEEE80211_STATUS_CHALLENGE;
1109			goto bad;
1110		}
1111		if (memcmp(ni->ni_challenge, &challenge[2],
1112			   challenge[1]) != 0) {
1113			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1114			    ni->ni_macaddr, "shared key response",
1115			    "%s", "challenge mismatch");
1116			vap->iv_stats.is_rx_auth_fail++;
1117			estatus = IEEE80211_STATUS_CHALLENGE;
1118			goto bad;
1119		}
1120		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1121		    ni, "%s", "station authenticated (shared key)");
1122		ieee80211_node_authorize(ni);
1123		break;
1124	default:
1125		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1126		    ni->ni_macaddr, "shared key auth",
1127		    "bad seq %d", seq);
1128		vap->iv_stats.is_rx_bad_auth++;
1129		estatus = IEEE80211_STATUS_SEQUENCE;
1130		goto bad;
1131	}
1132	IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1133	return;
1134bad:
1135	/*
1136	 * Send an error response; but only when operating as an AP.
1137	 */
1138	/* XXX hack to workaround calling convention */
1139	ieee80211_send_error(ni, wh->i_addr2,
1140	    IEEE80211_FC0_SUBTYPE_AUTH,
1141	    (seq + 1) | (estatus<<16));
1142}
1143
1144/*
1145 * Convert a WPA cipher selector OUI to an internal
1146 * cipher algorithm.  Where appropriate we also
1147 * record any key length.
1148 */
1149static int
1150wpa_cipher(const uint8_t *sel, uint8_t *keylen)
1151{
1152#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1153	uint32_t w = LE_READ_4(sel);
1154
1155	switch (w) {
1156	case WPA_SEL(WPA_CSE_NULL):
1157		return IEEE80211_CIPHER_NONE;
1158	case WPA_SEL(WPA_CSE_WEP40):
1159		if (keylen)
1160			*keylen = 40 / NBBY;
1161		return IEEE80211_CIPHER_WEP;
1162	case WPA_SEL(WPA_CSE_WEP104):
1163		if (keylen)
1164			*keylen = 104 / NBBY;
1165		return IEEE80211_CIPHER_WEP;
1166	case WPA_SEL(WPA_CSE_TKIP):
1167		return IEEE80211_CIPHER_TKIP;
1168	case WPA_SEL(WPA_CSE_CCMP):
1169		return IEEE80211_CIPHER_AES_CCM;
1170	}
1171	return 32;		/* NB: so 1<< is discarded */
1172#undef WPA_SEL
1173}
1174
1175/*
1176 * Convert a WPA key management/authentication algorithm
1177 * to an internal code.
1178 */
1179static int
1180wpa_keymgmt(const uint8_t *sel)
1181{
1182#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1183	uint32_t w = LE_READ_4(sel);
1184
1185	switch (w) {
1186	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
1187		return WPA_ASE_8021X_UNSPEC;
1188	case WPA_SEL(WPA_ASE_8021X_PSK):
1189		return WPA_ASE_8021X_PSK;
1190	case WPA_SEL(WPA_ASE_NONE):
1191		return WPA_ASE_NONE;
1192	}
1193	return 0;		/* NB: so is discarded */
1194#undef WPA_SEL
1195}
1196
1197/*
1198 * Parse a WPA information element to collect parameters.
1199 * Note that we do not validate security parameters; that
1200 * is handled by the authenticator; the parsing done here
1201 * is just for internal use in making operational decisions.
1202 */
1203static int
1204ieee80211_parse_wpa(struct ieee80211vap *vap, const uint8_t *frm,
1205	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1206{
1207	uint8_t len = frm[1];
1208	uint32_t w;
1209	int n;
1210
1211	/*
1212	 * Check the length once for fixed parts: OUI, type,
1213	 * version, mcast cipher, and 2 selector counts.
1214	 * Other, variable-length data, must be checked separately.
1215	 */
1216	if ((vap->iv_flags & IEEE80211_F_WPA1) == 0) {
1217		IEEE80211_DISCARD_IE(vap,
1218		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1219		    wh, "WPA", "not WPA, flags 0x%x", vap->iv_flags);
1220		return IEEE80211_REASON_IE_INVALID;
1221	}
1222	if (len < 14) {
1223		IEEE80211_DISCARD_IE(vap,
1224		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1225		    wh, "WPA", "too short, len %u", len);
1226		return IEEE80211_REASON_IE_INVALID;
1227	}
1228	frm += 6, len -= 4;		/* NB: len is payload only */
1229	/* NB: iswapoui already validated the OUI and type */
1230	w = LE_READ_2(frm);
1231	if (w != WPA_VERSION) {
1232		IEEE80211_DISCARD_IE(vap,
1233		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1234		    wh, "WPA", "bad version %u", w);
1235		return IEEE80211_REASON_IE_INVALID;
1236	}
1237	frm += 2, len -= 2;
1238
1239	memset(rsn, 0, sizeof(*rsn));
1240
1241	/* multicast/group cipher */
1242	rsn->rsn_mcastcipher = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
1243	frm += 4, len -= 4;
1244
1245	/* unicast ciphers */
1246	n = LE_READ_2(frm);
1247	frm += 2, len -= 2;
1248	if (len < n*4+2) {
1249		IEEE80211_DISCARD_IE(vap,
1250		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1251		    wh, "WPA", "ucast cipher data too short; len %u, n %u",
1252		    len, n);
1253		return IEEE80211_REASON_IE_INVALID;
1254	}
1255	w = 0;
1256	for (; n > 0; n--) {
1257		w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
1258		frm += 4, len -= 4;
1259	}
1260	if (w & (1<<IEEE80211_CIPHER_TKIP))
1261		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1262	else
1263		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1264
1265	/* key management algorithms */
1266	n = LE_READ_2(frm);
1267	frm += 2, len -= 2;
1268	if (len < n*4) {
1269		IEEE80211_DISCARD_IE(vap,
1270		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1271		    wh, "WPA", "key mgmt alg data too short; len %u, n %u",
1272		    len, n);
1273		return IEEE80211_REASON_IE_INVALID;
1274	}
1275	w = 0;
1276	for (; n > 0; n--) {
1277		w |= wpa_keymgmt(frm);
1278		frm += 4, len -= 4;
1279	}
1280	if (w & WPA_ASE_8021X_UNSPEC)
1281		rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
1282	else
1283		rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1284
1285	if (len > 2)		/* optional capabilities */
1286		rsn->rsn_caps = LE_READ_2(frm);
1287
1288	return 0;
1289}
1290
1291/*
1292 * Convert an RSN cipher selector OUI to an internal
1293 * cipher algorithm.  Where appropriate we also
1294 * record any key length.
1295 */
1296static int
1297rsn_cipher(const uint8_t *sel, uint8_t *keylen)
1298{
1299#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1300	uint32_t w = LE_READ_4(sel);
1301
1302	switch (w) {
1303	case RSN_SEL(RSN_CSE_NULL):
1304		return IEEE80211_CIPHER_NONE;
1305	case RSN_SEL(RSN_CSE_WEP40):
1306		if (keylen)
1307			*keylen = 40 / NBBY;
1308		return IEEE80211_CIPHER_WEP;
1309	case RSN_SEL(RSN_CSE_WEP104):
1310		if (keylen)
1311			*keylen = 104 / NBBY;
1312		return IEEE80211_CIPHER_WEP;
1313	case RSN_SEL(RSN_CSE_TKIP):
1314		return IEEE80211_CIPHER_TKIP;
1315	case RSN_SEL(RSN_CSE_CCMP):
1316		return IEEE80211_CIPHER_AES_CCM;
1317	case RSN_SEL(RSN_CSE_WRAP):
1318		return IEEE80211_CIPHER_AES_OCB;
1319	}
1320	return 32;		/* NB: so 1<< is discarded */
1321#undef WPA_SEL
1322}
1323
1324/*
1325 * Convert an RSN key management/authentication algorithm
1326 * to an internal code.
1327 */
1328static int
1329rsn_keymgmt(const uint8_t *sel)
1330{
1331#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1332	uint32_t w = LE_READ_4(sel);
1333
1334	switch (w) {
1335	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
1336		return RSN_ASE_8021X_UNSPEC;
1337	case RSN_SEL(RSN_ASE_8021X_PSK):
1338		return RSN_ASE_8021X_PSK;
1339	case RSN_SEL(RSN_ASE_NONE):
1340		return RSN_ASE_NONE;
1341	}
1342	return 0;		/* NB: so is discarded */
1343#undef RSN_SEL
1344}
1345
1346/*
1347 * Parse a WPA/RSN information element to collect parameters
1348 * and validate the parameters against what has been
1349 * configured for the system.
1350 */
1351static int
1352ieee80211_parse_rsn(struct ieee80211vap *vap, const uint8_t *frm,
1353	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1354{
1355	uint8_t len = frm[1];
1356	uint32_t w;
1357	int n;
1358
1359	/*
1360	 * Check the length once for fixed parts:
1361	 * version, mcast cipher, and 2 selector counts.
1362	 * Other, variable-length data, must be checked separately.
1363	 */
1364	if ((vap->iv_flags & IEEE80211_F_WPA2) == 0) {
1365		IEEE80211_DISCARD_IE(vap,
1366		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1367		    wh, "WPA", "not RSN, flags 0x%x", vap->iv_flags);
1368		return IEEE80211_REASON_IE_INVALID;
1369	}
1370	if (len < 10) {
1371		IEEE80211_DISCARD_IE(vap,
1372		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1373		    wh, "RSN", "too short, len %u", len);
1374		return IEEE80211_REASON_IE_INVALID;
1375	}
1376	frm += 2;
1377	w = LE_READ_2(frm);
1378	if (w != RSN_VERSION) {
1379		IEEE80211_DISCARD_IE(vap,
1380		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1381		    wh, "RSN", "bad version %u", w);
1382		return IEEE80211_REASON_IE_INVALID;
1383	}
1384	frm += 2, len -= 2;
1385
1386	memset(rsn, 0, sizeof(*rsn));
1387
1388	/* multicast/group cipher */
1389	rsn->rsn_mcastcipher = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
1390	frm += 4, len -= 4;
1391
1392	/* unicast ciphers */
1393	n = LE_READ_2(frm);
1394	frm += 2, len -= 2;
1395	if (len < n*4+2) {
1396		IEEE80211_DISCARD_IE(vap,
1397		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1398		    wh, "RSN", "ucast cipher data too short; len %u, n %u",
1399		    len, n);
1400		return IEEE80211_REASON_IE_INVALID;
1401	}
1402	w = 0;
1403	for (; n > 0; n--) {
1404		w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
1405		frm += 4, len -= 4;
1406	}
1407	if (w & (1<<IEEE80211_CIPHER_TKIP))
1408		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1409	else
1410		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1411
1412	/* key management algorithms */
1413	n = LE_READ_2(frm);
1414	frm += 2, len -= 2;
1415	if (len < n*4) {
1416		IEEE80211_DISCARD_IE(vap,
1417		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1418		    wh, "RSN", "key mgmt alg data too short; len %u, n %u",
1419		    len, n);
1420		return IEEE80211_REASON_IE_INVALID;
1421	}
1422	w = 0;
1423	for (; n > 0; n--) {
1424		w |= rsn_keymgmt(frm);
1425		frm += 4, len -= 4;
1426	}
1427	if (w & RSN_ASE_8021X_UNSPEC)
1428		rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
1429	else
1430		rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
1431
1432	/* optional RSN capabilities */
1433	if (len > 2)
1434		rsn->rsn_caps = LE_READ_2(frm);
1435	/* XXXPMKID */
1436
1437	return 0;
1438}
1439
1440/*
1441 * WPA/802.11i assocation request processing.
1442 */
1443static int
1444wpa_assocreq(struct ieee80211_node *ni, struct ieee80211_rsnparms *rsnparms,
1445	const struct ieee80211_frame *wh, const uint8_t *wpa,
1446	const uint8_t *rsn, uint16_t capinfo)
1447{
1448	struct ieee80211vap *vap = ni->ni_vap;
1449	uint8_t reason;
1450	int badwparsn;
1451
1452	ni->ni_flags &= ~(IEEE80211_NODE_WPS|IEEE80211_NODE_TSN);
1453	if (wpa == NULL && rsn == NULL) {
1454		if (vap->iv_flags_ext & IEEE80211_FEXT_WPS) {
1455			/*
1456			 * W-Fi Protected Setup (WPS) permits
1457			 * clients to associate and pass EAPOL frames
1458			 * to establish initial credentials.
1459			 */
1460			ni->ni_flags |= IEEE80211_NODE_WPS;
1461			return 1;
1462		}
1463		if ((vap->iv_flags_ext & IEEE80211_FEXT_TSN) &&
1464		    (capinfo & IEEE80211_CAPINFO_PRIVACY)) {
1465			/*
1466			 * Transitional Security Network.  Permits clients
1467			 * to associate and use WEP while WPA is configured.
1468			 */
1469			ni->ni_flags |= IEEE80211_NODE_TSN;
1470			return 1;
1471		}
1472		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1473		    wh, NULL, "%s", "no WPA/RSN IE in association request");
1474		vap->iv_stats.is_rx_assoc_badwpaie++;
1475		reason = IEEE80211_REASON_IE_INVALID;
1476		goto bad;
1477	}
1478	/* assert right association security credentials */
1479	badwparsn = 0;			/* NB: to silence compiler */
1480	switch (vap->iv_flags & IEEE80211_F_WPA) {
1481	case IEEE80211_F_WPA1:
1482		badwparsn = (wpa == NULL);
1483		break;
1484	case IEEE80211_F_WPA2:
1485		badwparsn = (rsn == NULL);
1486		break;
1487	case IEEE80211_F_WPA1|IEEE80211_F_WPA2:
1488		badwparsn = (wpa == NULL && rsn == NULL);
1489		break;
1490	}
1491	if (badwparsn) {
1492		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1493		    wh, NULL,
1494		    "%s", "missing WPA/RSN IE in association request");
1495		vap->iv_stats.is_rx_assoc_badwpaie++;
1496		reason = IEEE80211_REASON_IE_INVALID;
1497		goto bad;
1498	}
1499	/*
1500	 * Parse WPA/RSN information element.
1501	 */
1502	if (wpa != NULL)
1503		reason = ieee80211_parse_wpa(vap, wpa, rsnparms, wh);
1504	else
1505		reason = ieee80211_parse_rsn(vap, rsn, rsnparms, wh);
1506	if (reason != 0) {
1507		/* XXX distinguish WPA/RSN? */
1508		vap->iv_stats.is_rx_assoc_badwpaie++;
1509		goto bad;
1510	}
1511	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA, ni,
1512	    "%s ie: mc %u/%u uc %u/%u key %u caps 0x%x",
1513	    wpa != NULL ? "WPA" : "RSN",
1514	    rsnparms->rsn_mcastcipher, rsnparms->rsn_mcastkeylen,
1515	    rsnparms->rsn_ucastcipher, rsnparms->rsn_ucastkeylen,
1516	    rsnparms->rsn_keymgmt, rsnparms->rsn_caps);
1517
1518	return 1;
1519bad:
1520	ieee80211_node_deauth(ni, reason);
1521	return 0;
1522}
1523
1524/* XXX find a better place for definition */
1525struct l2_update_frame {
1526	struct ether_header eh;
1527	uint8_t dsap;
1528	uint8_t ssap;
1529	uint8_t control;
1530	uint8_t xid[3];
1531}  __packed;
1532
1533/*
1534 * Deliver a TGf L2UF frame on behalf of a station.
1535 * This primes any bridge when the station is roaming
1536 * between ap's on the same wired network.
1537 */
1538static void
1539ieee80211_deliver_l2uf(struct ieee80211_node *ni)
1540{
1541	struct ieee80211vap *vap = ni->ni_vap;
1542	struct ifnet *ifp = vap->iv_ifp;
1543	struct mbuf *m;
1544	struct l2_update_frame *l2uf;
1545	struct ether_header *eh;
1546
1547	m = m_gethdr(M_NOWAIT, MT_DATA);
1548	if (m == NULL) {
1549		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1550		    "%s", "no mbuf for l2uf frame");
1551		vap->iv_stats.is_rx_nobuf++;	/* XXX not right */
1552		return;
1553	}
1554	l2uf = mtod(m, struct l2_update_frame *);
1555	eh = &l2uf->eh;
1556	/* dst: Broadcast address */
1557	IEEE80211_ADDR_COPY(eh->ether_dhost, ifp->if_broadcastaddr);
1558	/* src: associated STA */
1559	IEEE80211_ADDR_COPY(eh->ether_shost, ni->ni_macaddr);
1560	eh->ether_type = htons(sizeof(*l2uf) - sizeof(*eh));
1561
1562	l2uf->dsap = 0;
1563	l2uf->ssap = 0;
1564	l2uf->control = 0xf5;
1565	l2uf->xid[0] = 0x81;
1566	l2uf->xid[1] = 0x80;
1567	l2uf->xid[2] = 0x00;
1568
1569	m->m_pkthdr.len = m->m_len = sizeof(*l2uf);
1570	hostap_deliver_data(vap, ni, m);
1571}
1572
1573static void
1574ratesetmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1575	int reassoc, int resp, const char *tag, int rate)
1576{
1577	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1578	    "deny %s request, %s rate set mismatch, rate/MCS %d",
1579	    reassoc ? "reassoc" : "assoc", tag, rate & IEEE80211_RATE_VAL);
1580	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_BASIC_RATE);
1581	ieee80211_node_leave(ni);
1582}
1583
1584static void
1585capinfomismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1586	int reassoc, int resp, const char *tag, int capinfo)
1587{
1588	struct ieee80211vap *vap = ni->ni_vap;
1589
1590	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1591	    "deny %s request, %s mismatch 0x%x",
1592	    reassoc ? "reassoc" : "assoc", tag, capinfo);
1593	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_CAPINFO);
1594	ieee80211_node_leave(ni);
1595	vap->iv_stats.is_rx_assoc_capmismatch++;
1596}
1597
1598static void
1599htcapmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1600	int reassoc, int resp)
1601{
1602	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1603	    "deny %s request, %s missing HT ie", reassoc ? "reassoc" : "assoc");
1604	/* XXX no better code */
1605	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_OTHER);
1606	ieee80211_node_leave(ni);
1607}
1608
1609static void
1610authalgreject(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1611	int algo, int seq, int status)
1612{
1613	struct ieee80211vap *vap = ni->ni_vap;
1614
1615	IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1616	    wh, NULL, "unsupported alg %d", algo);
1617	vap->iv_stats.is_rx_auth_unsupported++;
1618	ieee80211_send_error(ni, wh->i_addr2, IEEE80211_FC0_SUBTYPE_AUTH,
1619	    seq | (status << 16));
1620}
1621
1622static __inline int
1623ishtmixed(const uint8_t *ie)
1624{
1625	const struct ieee80211_ie_htinfo *ht =
1626	    (const struct ieee80211_ie_htinfo *) ie;
1627	return (ht->hi_byte2 & IEEE80211_HTINFO_OPMODE) ==
1628	    IEEE80211_HTINFO_OPMODE_MIXED;
1629}
1630
1631static int
1632is11bclient(const uint8_t *rates, const uint8_t *xrates)
1633{
1634	static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
1635	int i;
1636
1637	/* NB: the 11b clients we care about will not have xrates */
1638	if (xrates != NULL || rates == NULL)
1639		return 0;
1640	for (i = 0; i < rates[1]; i++) {
1641		int r = rates[2+i] & IEEE80211_RATE_VAL;
1642		if (r > 2*11 || ((1<<r) & brates) == 0)
1643			return 0;
1644	}
1645	return 1;
1646}
1647
1648static void
1649hostap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1650	int subtype, int rssi, int noise, uint32_t rstamp)
1651{
1652	struct ieee80211vap *vap = ni->ni_vap;
1653	struct ieee80211com *ic = ni->ni_ic;
1654	struct ieee80211_frame *wh;
1655	uint8_t *frm, *efrm, *sfrm;
1656	uint8_t *ssid, *rates, *xrates, *wpa, *rsn, *wme, *ath, *htcap;
1657	int reassoc, resp;
1658	uint8_t rate;
1659
1660	wh = mtod(m0, struct ieee80211_frame *);
1661	frm = (uint8_t *)&wh[1];
1662	efrm = mtod(m0, uint8_t *) + m0->m_len;
1663	switch (subtype) {
1664	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1665	case IEEE80211_FC0_SUBTYPE_BEACON: {
1666		struct ieee80211_scanparams scan;
1667		/*
1668		 * We process beacon/probe response frames when scanning;
1669		 * otherwise we check beacon frames for overlapping non-ERP
1670		 * BSS in 11g and/or overlapping legacy BSS when in HT.
1671		 */
1672		if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
1673		    subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
1674			vap->iv_stats.is_rx_mgtdiscard++;
1675			return;
1676		}
1677		/* NB: accept off-channel frames */
1678		if (ieee80211_parse_beacon(ni, m0, &scan) &~ IEEE80211_BPARSE_OFFCHAN)
1679			return;
1680		/*
1681		 * Count frame now that we know it's to be processed.
1682		 */
1683		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1684			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
1685			IEEE80211_NODE_STAT(ni, rx_beacons);
1686		} else
1687			IEEE80211_NODE_STAT(ni, rx_proberesp);
1688		/*
1689		 * If scanning, just pass information to the scan module.
1690		 */
1691		if (ic->ic_flags & IEEE80211_F_SCAN) {
1692			if (scan.status == 0 &&		/* NB: on channel */
1693			    (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN)) {
1694				/*
1695				 * Actively scanning a channel marked passive;
1696				 * send a probe request now that we know there
1697				 * is 802.11 traffic present.
1698				 *
1699				 * XXX check if the beacon we recv'd gives
1700				 * us what we need and suppress the probe req
1701				 */
1702				ieee80211_probe_curchan(vap, 1);
1703				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1704			}
1705			ieee80211_add_scan(vap, &scan, wh,
1706				subtype, rssi, noise, rstamp);
1707			return;
1708		}
1709		/*
1710		 * Check beacon for overlapping bss w/ non ERP stations.
1711		 * If we detect one and protection is configured but not
1712		 * enabled, enable it and start a timer that'll bring us
1713		 * out if we stop seeing the bss.
1714		 */
1715		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1716		    scan.status == 0 &&			/* NB: on-channel */
1717		    ((scan.erp & 0x100) == 0 ||		/* NB: no ERP, 11b sta*/
1718		     (scan.erp & IEEE80211_ERP_NON_ERP_PRESENT))) {
1719			ic->ic_lastnonerp = ticks;
1720			ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
1721			if (ic->ic_protmode != IEEE80211_PROT_NONE &&
1722			    (ic->ic_flags & IEEE80211_F_USEPROT) == 0) {
1723				IEEE80211_NOTE_FRAME(vap,
1724				    IEEE80211_MSG_ASSOC, wh,
1725				    "non-ERP present on channel %d "
1726				    "(saw erp 0x%x from channel %d), "
1727				    "enable use of protection",
1728				    ic->ic_curchan->ic_ieee,
1729				    scan.erp, scan.chan);
1730				ic->ic_flags |= IEEE80211_F_USEPROT;
1731				ieee80211_notify_erp(ic);
1732			}
1733		}
1734		/*
1735		 * Check beacon for non-HT station on HT channel
1736		 * and update HT BSS occupancy as appropriate.
1737		 */
1738		if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) {
1739			if (scan.status & IEEE80211_BPARSE_OFFCHAN) {
1740				/*
1741				 * Off control channel; only check frames
1742				 * that come in the extension channel when
1743				 * operating w/ HT40.
1744				 */
1745				if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
1746					break;
1747				if (scan.chan != ic->ic_curchan->ic_extieee)
1748					break;
1749			}
1750			if (scan.htinfo == NULL) {
1751				ieee80211_htprot_update(ic,
1752				    IEEE80211_HTINFO_OPMODE_PROTOPT |
1753				    IEEE80211_HTINFO_NONHT_PRESENT);
1754			} else if (ishtmixed(scan.htinfo)) {
1755				/* XXX? take NONHT_PRESENT from beacon? */
1756				ieee80211_htprot_update(ic,
1757				    IEEE80211_HTINFO_OPMODE_MIXED |
1758				    IEEE80211_HTINFO_NONHT_PRESENT);
1759			}
1760		}
1761		break;
1762	}
1763
1764	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1765		if (vap->iv_state != IEEE80211_S_RUN) {
1766			vap->iv_stats.is_rx_mgtdiscard++;
1767			return;
1768		}
1769		/*
1770		 * prreq frame format
1771		 *	[tlv] ssid
1772		 *	[tlv] supported rates
1773		 *	[tlv] extended supported rates
1774		 */
1775		ssid = rates = xrates = NULL;
1776		sfrm = frm;
1777		while (efrm - frm > 1) {
1778			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1779			switch (*frm) {
1780			case IEEE80211_ELEMID_SSID:
1781				ssid = frm;
1782				break;
1783			case IEEE80211_ELEMID_RATES:
1784				rates = frm;
1785				break;
1786			case IEEE80211_ELEMID_XRATES:
1787				xrates = frm;
1788				break;
1789			}
1790			frm += frm[1] + 2;
1791		}
1792		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1793		if (xrates != NULL)
1794			IEEE80211_VERIFY_ELEMENT(xrates,
1795				IEEE80211_RATE_MAXSIZE - rates[1], return);
1796		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1797		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
1798		if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
1799			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1800			    wh, NULL,
1801			    "%s", "no ssid with ssid suppression enabled");
1802			vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
1803			return;
1804		}
1805
1806		/* XXX find a better class or define it's own */
1807		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1808		    "%s", "recv probe req");
1809		/*
1810		 * Some legacy 11b clients cannot hack a complete
1811		 * probe response frame.  When the request includes
1812		 * only a bare-bones rate set, communicate this to
1813		 * the transmit side.
1814		 */
1815		ieee80211_send_proberesp(vap, wh->i_addr2,
1816		    is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
1817		break;
1818
1819	case IEEE80211_FC0_SUBTYPE_AUTH: {
1820		uint16_t algo, seq, status;
1821
1822		if (vap->iv_state != IEEE80211_S_RUN) {
1823			vap->iv_stats.is_rx_mgtdiscard++;
1824			return;
1825		}
1826		if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1827			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1828			    wh, NULL, "%s", "wrong bssid");
1829			vap->iv_stats.is_rx_wrongbss++;	/*XXX unique stat?*/
1830			return;
1831		}
1832		/*
1833		 * auth frame format
1834		 *	[2] algorithm
1835		 *	[2] sequence
1836		 *	[2] status
1837		 *	[tlv*] challenge
1838		 */
1839		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1840		algo   = le16toh(*(uint16_t *)frm);
1841		seq    = le16toh(*(uint16_t *)(frm + 2));
1842		status = le16toh(*(uint16_t *)(frm + 4));
1843		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1844		    "recv auth frame with algorithm %d seq %d", algo, seq);
1845		/*
1846		 * Consult the ACL policy module if setup.
1847		 */
1848		if (vap->iv_acl != NULL &&
1849		    !vap->iv_acl->iac_check(vap, wh->i_addr2)) {
1850			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1851			    wh, NULL, "%s", "disallowed by ACL");
1852			vap->iv_stats.is_rx_acl++;
1853			ieee80211_send_error(ni, wh->i_addr2,
1854			    IEEE80211_FC0_SUBTYPE_AUTH,
1855			    (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
1856			return;
1857		}
1858		if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1859			IEEE80211_DISCARD(vap,
1860			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1861			    wh, NULL, "%s", "TKIP countermeasures enabled");
1862			vap->iv_stats.is_rx_auth_countermeasures++;
1863			ieee80211_send_error(ni, wh->i_addr2,
1864				IEEE80211_FC0_SUBTYPE_AUTH,
1865				IEEE80211_REASON_MIC_FAILURE);
1866			return;
1867		}
1868		if (algo == IEEE80211_AUTH_ALG_SHARED)
1869			hostap_auth_shared(ni, wh, frm + 6, efrm, rssi,
1870			    noise, rstamp, seq, status);
1871		else if (algo == IEEE80211_AUTH_ALG_OPEN)
1872			hostap_auth_open(ni, wh, rssi, noise, rstamp,
1873			    seq, status);
1874		else if (algo == IEEE80211_AUTH_ALG_LEAP) {
1875			authalgreject(ni, wh, algo,
1876			    seq+1, IEEE80211_STATUS_ALG);
1877			return;
1878		} else {
1879			/*
1880			 * We assume that an unknown algorithm is the result
1881			 * of a decryption failure on a shared key auth frame;
1882			 * return a status code appropriate for that instead
1883			 * of IEEE80211_STATUS_ALG.
1884			 *
1885			 * NB: a seq# of 4 is intentional; the decrypted
1886			 *     frame likely has a bogus seq value.
1887			 */
1888			authalgreject(ni, wh, algo,
1889			    4, IEEE80211_STATUS_CHALLENGE);
1890			return;
1891		}
1892		break;
1893	}
1894
1895	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1896	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
1897		uint16_t capinfo, lintval;
1898		struct ieee80211_rsnparms rsnparms;
1899
1900		if (vap->iv_state != IEEE80211_S_RUN) {
1901			vap->iv_stats.is_rx_mgtdiscard++;
1902			return;
1903		}
1904		if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1905			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1906			    wh, NULL, "%s", "wrong bssid");
1907			vap->iv_stats.is_rx_assoc_bss++;
1908			return;
1909		}
1910		if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1911			reassoc = 1;
1912			resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
1913		} else {
1914			reassoc = 0;
1915			resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
1916		}
1917		if (ni == vap->iv_bss) {
1918			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1919			    "deny %s request, sta not authenticated",
1920			    reassoc ? "reassoc" : "assoc");
1921			ieee80211_send_error(ni, wh->i_addr2,
1922			    IEEE80211_FC0_SUBTYPE_DEAUTH,
1923			    IEEE80211_REASON_ASSOC_NOT_AUTHED);
1924			vap->iv_stats.is_rx_assoc_notauth++;
1925			return;
1926		}
1927
1928		/*
1929		 * asreq frame format
1930		 *	[2] capability information
1931		 *	[2] listen interval
1932		 *	[6*] current AP address (reassoc only)
1933		 *	[tlv] ssid
1934		 *	[tlv] supported rates
1935		 *	[tlv] extended supported rates
1936		 *	[tlv] WPA or RSN
1937		 *	[tlv] HT capabilities
1938		 *	[tlv] Atheros capabilities
1939		 */
1940		IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4), return);
1941		capinfo = le16toh(*(uint16_t *)frm);	frm += 2;
1942		lintval = le16toh(*(uint16_t *)frm);	frm += 2;
1943		if (reassoc)
1944			frm += 6;	/* ignore current AP info */
1945		ssid = rates = xrates = wpa = rsn = wme = ath = htcap = NULL;
1946		sfrm = frm;
1947		while (efrm - frm > 1) {
1948			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1949			switch (*frm) {
1950			case IEEE80211_ELEMID_SSID:
1951				ssid = frm;
1952				break;
1953			case IEEE80211_ELEMID_RATES:
1954				rates = frm;
1955				break;
1956			case IEEE80211_ELEMID_XRATES:
1957				xrates = frm;
1958				break;
1959			case IEEE80211_ELEMID_RSN:
1960				rsn = frm;
1961				break;
1962			case IEEE80211_ELEMID_HTCAP:
1963				htcap = frm;
1964				break;
1965			case IEEE80211_ELEMID_VENDOR:
1966				if (iswpaoui(frm))
1967					wpa = frm;
1968				else if (iswmeinfo(frm))
1969					wme = frm;
1970				else if (isatherosoui(frm))
1971					ath = frm;
1972				else if (vap->iv_flags_ext & IEEE80211_FEXT_HTCOMPAT) {
1973					if (ishtcapoui(frm) && htcap == NULL)
1974						htcap = frm;
1975				}
1976				break;
1977			}
1978			frm += frm[1] + 2;
1979		}
1980		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1981		if (xrates != NULL)
1982			IEEE80211_VERIFY_ELEMENT(xrates,
1983				IEEE80211_RATE_MAXSIZE - rates[1], return);
1984		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1985		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
1986		if (htcap != NULL) {
1987			IEEE80211_VERIFY_LENGTH(htcap[1],
1988			     htcap[0] == IEEE80211_ELEMID_VENDOR ?
1989			         4 + sizeof(struct ieee80211_ie_htcap)-2 :
1990			         sizeof(struct ieee80211_ie_htcap)-2,
1991			     return);		/* XXX just NULL out? */
1992		}
1993
1994		if ((vap->iv_flags & IEEE80211_F_WPA) &&
1995		    !wpa_assocreq(ni, &rsnparms, wh, wpa, rsn, capinfo))
1996			return;
1997		/* discard challenge after association */
1998		if (ni->ni_challenge != NULL) {
1999			free(ni->ni_challenge, M_80211_NODE);
2000			ni->ni_challenge = NULL;
2001		}
2002		/* NB: 802.11 spec says to ignore station's privacy bit */
2003		if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
2004			capinfomismatch(ni, wh, reassoc, resp,
2005			    "capability", capinfo);
2006			return;
2007		}
2008		/*
2009		 * Disallow re-associate w/ invalid slot time setting.
2010		 */
2011		if (ni->ni_associd != 0 &&
2012		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2013		    ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
2014			capinfomismatch(ni, wh, reassoc, resp,
2015			    "slot time", capinfo);
2016			return;
2017		}
2018		rate = ieee80211_setup_rates(ni, rates, xrates,
2019				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
2020				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2021		if (rate & IEEE80211_RATE_BASIC) {
2022			ratesetmismatch(ni, wh, reassoc, resp, "legacy", rate);
2023			vap->iv_stats.is_rx_assoc_norate++;
2024			return;
2025		}
2026		/*
2027		 * If constrained to 11g-only stations reject an
2028		 * 11b-only station.  We cheat a bit here by looking
2029		 * at the max negotiated xmit rate and assuming anyone
2030		 * with a best rate <24Mb/s is an 11b station.
2031		 */
2032		if ((vap->iv_flags & IEEE80211_F_PUREG) && rate < 48) {
2033			ratesetmismatch(ni, wh, reassoc, resp, "11g", rate);
2034			vap->iv_stats.is_rx_assoc_norate++;
2035			return;
2036		}
2037		/*
2038		 * Do HT rate set handling and setup HT node state.
2039		 */
2040		ni->ni_chan = vap->iv_bss->ni_chan;
2041		if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
2042			rate = ieee80211_setup_htrates(ni, htcap,
2043				IEEE80211_F_DOFMCS | IEEE80211_F_DONEGO |
2044				IEEE80211_F_DOBRS);
2045			if (rate & IEEE80211_RATE_BASIC) {
2046				ratesetmismatch(ni, wh, reassoc, resp,
2047				    "HT", rate);
2048				vap->iv_stats.is_ht_assoc_norate++;
2049				return;
2050			}
2051			ieee80211_ht_node_init(ni);
2052			ieee80211_ht_updatehtcap(ni, htcap);
2053		} else if (ni->ni_flags & IEEE80211_NODE_HT)
2054			ieee80211_ht_node_cleanup(ni);
2055		/*
2056		 * Allow AMPDU operation only with unencrypted traffic
2057		 * or AES-CCM; the 11n spec only specifies these ciphers
2058		 * so permitting any others is undefined and can lead
2059		 * to interoperability problems.
2060		 */
2061		if ((ni->ni_flags & IEEE80211_NODE_HT) &&
2062		    (((vap->iv_flags & IEEE80211_F_WPA) &&
2063		      rsnparms.rsn_ucastcipher != IEEE80211_CIPHER_AES_CCM) ||
2064		     (vap->iv_flags & (IEEE80211_F_WPA|IEEE80211_F_PRIVACY)) == IEEE80211_F_PRIVACY)) {
2065			IEEE80211_NOTE(vap,
2066			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
2067			    "disallow HT use because WEP or TKIP requested, "
2068			    "capinfo 0x%x ucastcipher %d", capinfo,
2069			    rsnparms.rsn_ucastcipher);
2070			ieee80211_ht_node_cleanup(ni);
2071			vap->iv_stats.is_ht_assoc_downgrade++;
2072		}
2073		/*
2074		 * If constrained to 11n-only stations reject legacy stations.
2075		 */
2076		if ((vap->iv_flags_ext & IEEE80211_FEXT_PUREN) &&
2077		    (ni->ni_flags & IEEE80211_NODE_HT) == 0) {
2078			htcapmismatch(ni, wh, reassoc, resp);
2079			vap->iv_stats.is_ht_assoc_nohtcap++;
2080			return;
2081		}
2082		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
2083		ni->ni_noise = noise;
2084		ni->ni_rstamp = rstamp;
2085		ni->ni_intval = lintval;
2086		ni->ni_capinfo = capinfo;
2087		ni->ni_fhdwell = vap->iv_bss->ni_fhdwell;
2088		ni->ni_fhindex = vap->iv_bss->ni_fhindex;
2089		/*
2090		 * Store the IEs.
2091		 * XXX maybe better to just expand
2092		 */
2093		if (ieee80211_ies_init(&ni->ni_ies, sfrm, efrm - sfrm)) {
2094#define	setie(_ie, _off)	ieee80211_ies_setie(ni->ni_ies, _ie, _off)
2095			if (wpa != NULL)
2096				setie(wpa_ie, wpa - sfrm);
2097			if (rsn != NULL)
2098				setie(rsn_ie, rsn - sfrm);
2099			if (htcap != NULL)
2100				setie(htcap_ie, htcap - sfrm);
2101			if (wme != NULL) {
2102				setie(wme_ie, wme - sfrm);
2103				/*
2104				 * Mark node as capable of QoS.
2105				 */
2106				ni->ni_flags |= IEEE80211_NODE_QOS;
2107			} else
2108				ni->ni_flags &= ~IEEE80211_NODE_QOS;
2109			if (ath != NULL) {
2110				setie(ath_ie, ath - sfrm);
2111				/*
2112				 * Parse ATH station parameters.
2113				 */
2114				ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
2115			} else
2116				ni->ni_ath_flags = 0;
2117#undef setie
2118		} else {
2119			ni->ni_flags &= ~IEEE80211_NODE_QOS;
2120			ni->ni_ath_flags = 0;
2121		}
2122		ieee80211_node_join(ni, resp);
2123		ieee80211_deliver_l2uf(ni);
2124		break;
2125	}
2126
2127	case IEEE80211_FC0_SUBTYPE_DEAUTH:
2128	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
2129		uint16_t reason;
2130
2131		if (vap->iv_state != IEEE80211_S_RUN ||
2132		    /* NB: can happen when in promiscuous mode */
2133		    !IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
2134			vap->iv_stats.is_rx_mgtdiscard++;
2135			break;
2136		}
2137		/*
2138		 * deauth/disassoc frame format
2139		 *	[2] reason
2140		 */
2141		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
2142		reason = le16toh(*(uint16_t *)frm);
2143		if (subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
2144			vap->iv_stats.is_rx_deauth++;
2145			IEEE80211_NODE_STAT(ni, rx_deauth);
2146		} else {
2147			vap->iv_stats.is_rx_disassoc++;
2148			IEEE80211_NODE_STAT(ni, rx_disassoc);
2149		}
2150		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2151		    "recv %s (reason %d)", ieee80211_mgt_subtype_name[subtype >>
2152			IEEE80211_FC0_SUBTYPE_SHIFT], reason);
2153		if (ni != vap->iv_bss)
2154			ieee80211_node_leave(ni);
2155		break;
2156	}
2157
2158	case IEEE80211_FC0_SUBTYPE_ACTION:
2159		if (vap->iv_state == IEEE80211_S_RUN) {
2160			if (ieee80211_parse_action(ni, m0) == 0)
2161				ic->ic_recv_action(ni, frm, efrm);
2162		} else
2163			vap->iv_stats.is_rx_mgtdiscard++;
2164		break;
2165
2166	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2167	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2168	default:
2169		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2170		     wh, "mgt", "subtype 0x%x not handled", subtype);
2171		vap->iv_stats.is_rx_badsubtype++;
2172		break;
2173	}
2174}
2175
2176/*
2177 * Process a received ps-poll frame.
2178 */
2179static void
2180hostap_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m0)
2181{
2182	struct ieee80211vap *vap = ni->ni_vap;
2183	struct ieee80211_frame_min *wh;
2184	struct ifnet *ifp;
2185	struct mbuf *m;
2186	uint16_t aid;
2187	int qlen;
2188
2189	wh = mtod(m0, struct ieee80211_frame_min *);
2190	if (ni->ni_associd == 0) {
2191		IEEE80211_DISCARD(vap,
2192		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2193		    (struct ieee80211_frame *) wh, NULL,
2194		    "%s", "unassociated station");
2195		vap->iv_stats.is_ps_unassoc++;
2196		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
2197			IEEE80211_REASON_NOT_ASSOCED);
2198		return;
2199	}
2200
2201	aid = le16toh(*(uint16_t *)wh->i_dur);
2202	if (aid != ni->ni_associd) {
2203		IEEE80211_DISCARD(vap,
2204		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2205		    (struct ieee80211_frame *) wh, NULL,
2206		    "aid mismatch: sta aid 0x%x poll aid 0x%x",
2207		    ni->ni_associd, aid);
2208		vap->iv_stats.is_ps_badaid++;
2209		/*
2210		 * NB: We used to deauth the station but it turns out
2211		 * the Blackberry Curve 8230 (and perhaps other devices)
2212		 * sometimes send the wrong AID when WME is negotiated.
2213		 * Being more lenient here seems ok as we already check
2214		 * the station is associated and we only return frames
2215		 * queued for the station (i.e. we don't use the AID).
2216		 */
2217		return;
2218	}
2219
2220	/* Okay, take the first queued packet and put it out... */
2221	m = ieee80211_node_psq_dequeue(ni, &qlen);
2222	if (m == NULL) {
2223		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_POWER, wh->i_addr2,
2224		    "%s", "recv ps-poll, but queue empty");
2225		ieee80211_send_nulldata(ieee80211_ref_node(ni));
2226		vap->iv_stats.is_ps_qempty++;	/* XXX node stat */
2227		if (vap->iv_set_tim != NULL)
2228			vap->iv_set_tim(ni, 0);	/* just in case */
2229		return;
2230	}
2231	/*
2232	 * If there are more packets, set the more packets bit
2233	 * in the packet dispatched to the station; otherwise
2234	 * turn off the TIM bit.
2235	 */
2236	if (qlen != 0) {
2237		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2238		    "recv ps-poll, send packet, %u still queued", qlen);
2239		m->m_flags |= M_MORE_DATA;
2240	} else {
2241		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2242		    "%s", "recv ps-poll, send packet, queue empty");
2243		if (vap->iv_set_tim != NULL)
2244			vap->iv_set_tim(ni, 0);
2245	}
2246	m->m_flags |= M_PWR_SAV;		/* bypass PS handling */
2247
2248	if (m->m_flags & M_ENCAP)
2249		ifp = vap->iv_ic->ic_ifp;
2250	else
2251		ifp = vap->iv_ifp;
2252	IF_ENQUEUE(&ifp->if_snd, m);
2253	if_start(ifp);
2254}
2255