ieee80211_input.c revision 170530
1/*-
2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_input.c 170530 2007-06-11 03:36:55Z sam $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/mbuf.h>
33#include <sys/malloc.h>
34#include <sys/endian.h>
35#include <sys/kernel.h>
36
37#include <sys/socket.h>
38
39#include <net/if.h>
40#include <net/if_media.h>
41#include <net/ethernet.h>
42#include <net/if_llc.h>
43#include <net/if_vlan_var.h>
44
45#include <net80211/ieee80211_var.h>
46
47#include <net/bpf.h>
48
49#ifdef IEEE80211_DEBUG
50#include <machine/stdarg.h>
51
52/*
53 * Decide if a received management frame should be
54 * printed when debugging is enabled.  This filters some
55 * of the less interesting frames that come frequently
56 * (e.g. beacons).
57 */
58static __inline int
59doprint(struct ieee80211com *ic, int subtype)
60{
61	switch (subtype) {
62	case IEEE80211_FC0_SUBTYPE_BEACON:
63		return (ic->ic_flags & IEEE80211_F_SCAN);
64	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
65		return (ic->ic_opmode == IEEE80211_M_IBSS);
66	}
67	return 1;
68}
69
70static const uint8_t *ieee80211_getbssid(struct ieee80211com *,
71	const struct ieee80211_frame *);
72#endif /* IEEE80211_DEBUG */
73
74static struct mbuf *ieee80211_defrag(struct ieee80211com *,
75	struct ieee80211_node *, struct mbuf *, int);
76static struct mbuf *ieee80211_decap(struct ieee80211com *, struct mbuf *, int);
77static void ieee80211_send_error(struct ieee80211com *, struct ieee80211_node *,
78		const uint8_t *mac, int subtype, int arg);
79static struct mbuf *ieee80211_decap_fastframe(struct ieee80211com *,
80	struct ieee80211_node *, struct mbuf *);
81static void ieee80211_recv_pspoll(struct ieee80211com *,
82	struct ieee80211_node *, struct mbuf *);
83
84/*
85 * Process a received frame.  The node associated with the sender
86 * should be supplied.  If nothing was found in the node table then
87 * the caller is assumed to supply a reference to ic_bss instead.
88 * The RSSI and a timestamp are also supplied.  The RSSI data is used
89 * during AP scanning to select a AP to associate with; it can have
90 * any units so long as values have consistent units and higher values
91 * mean ``better signal''.  The receive timestamp is currently not used
92 * by the 802.11 layer.
93 */
94int
95ieee80211_input(struct ieee80211com *ic, struct mbuf *m,
96	struct ieee80211_node *ni, int rssi, int noise, uint32_t rstamp)
97{
98#define	SEQ_LEQ(a,b)	((int)((a)-(b)) <= 0)
99#define	HAS_SEQ(type)	((type & 0x4) == 0)
100	struct ifnet *ifp = ic->ic_ifp;
101	struct ieee80211_frame *wh;
102	struct ieee80211_key *key;
103	struct ether_header *eh;
104	int hdrspace, need_tap;
105	uint8_t dir, type, subtype, qos;
106	uint8_t *bssid;
107	uint16_t rxseq;
108
109	if (m->m_flags & M_AMPDU) {
110		/*
111		 * Fastpath for A-MPDU reorder q resubmission.  Frames
112		 * w/ M_AMPDU marked have already passed through here
113		 * but were received out of order and been held on the
114		 * reorder queue.  When resubmitted they are marked
115		 * with the M_AMPDU flag and we can bypass most of the
116		 * normal processing.
117		 */
118		wh = mtod(m, struct ieee80211_frame *);
119		type = IEEE80211_FC0_TYPE_DATA;
120		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
121		subtype = IEEE80211_FC0_SUBTYPE_QOS;
122		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
123		need_tap = 0;
124		goto resubmit_ampdu;
125	}
126
127	KASSERT(ni != NULL, ("null node"));
128	ni->ni_inact = ni->ni_inact_reload;
129
130	need_tap = 1;			/* mbuf need to be tapped. */
131	type = -1;			/* undefined */
132	/*
133	 * In monitor mode, send everything directly to bpf.
134	 * XXX may want to include the CRC
135	 */
136	if (ic->ic_opmode == IEEE80211_M_MONITOR)
137		goto out;
138
139	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
140		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
141		    ni->ni_macaddr, NULL,
142		    "too short (1): len %u", m->m_pkthdr.len);
143		ic->ic_stats.is_rx_tooshort++;
144		goto out;
145	}
146	/*
147	 * Bit of a cheat here, we use a pointer for a 3-address
148	 * frame format but don't reference fields past outside
149	 * ieee80211_frame_min w/o first validating the data is
150	 * present.
151	 */
152	wh = mtod(m, struct ieee80211_frame *);
153
154	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
155	    IEEE80211_FC0_VERSION_0) {
156		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
157		    ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
158		ic->ic_stats.is_rx_badversion++;
159		goto err;
160	}
161
162	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
163	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
164	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
165	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
166		switch (ic->ic_opmode) {
167		case IEEE80211_M_STA:
168			bssid = wh->i_addr2;
169			if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) {
170				/* not interested in */
171				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
172				    bssid, NULL, "%s", "not to bss");
173				ic->ic_stats.is_rx_wrongbss++;
174				goto out;
175			}
176			break;
177		case IEEE80211_M_IBSS:
178		case IEEE80211_M_AHDEMO:
179		case IEEE80211_M_HOSTAP:
180			if (dir != IEEE80211_FC1_DIR_NODS)
181				bssid = wh->i_addr1;
182			else if (type == IEEE80211_FC0_TYPE_CTL)
183				bssid = wh->i_addr1;
184			else {
185				if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
186					IEEE80211_DISCARD_MAC(ic,
187					    IEEE80211_MSG_ANY, ni->ni_macaddr,
188					    NULL, "too short (2): len %u",
189					    m->m_pkthdr.len);
190					ic->ic_stats.is_rx_tooshort++;
191					goto out;
192				}
193				bssid = wh->i_addr3;
194			}
195			if (type != IEEE80211_FC0_TYPE_DATA)
196				break;
197			/*
198			 * Data frame, validate the bssid.
199			 */
200			if (!IEEE80211_ADDR_EQ(bssid, ic->ic_bss->ni_bssid) &&
201			    !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
202				/* not interested in */
203				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
204				    bssid, NULL, "%s", "not to bss");
205				ic->ic_stats.is_rx_wrongbss++;
206				goto out;
207			}
208			/*
209			 * For adhoc mode we cons up a node when it doesn't
210			 * exist. This should probably done after an ACL check.
211			 */
212			if (ni == ic->ic_bss &&
213			    ic->ic_opmode != IEEE80211_M_HOSTAP &&
214			    !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
215				/*
216				 * Fake up a node for this newly
217				 * discovered member of the IBSS.
218				 */
219				ni = ieee80211_fakeup_adhoc_node(&ic->ic_sta,
220						    wh->i_addr2);
221				if (ni == NULL) {
222					/* NB: stat kept for alloc failure */
223					goto err;
224				}
225			}
226			break;
227		default:
228			goto out;
229		}
230		ni->ni_rssi = rssi;
231		ni->ni_noise = noise;
232		ni->ni_rstamp = rstamp;
233		if (HAS_SEQ(type)) {
234			uint8_t tid;
235			if (IEEE80211_QOS_HAS_SEQ(wh)) {
236				tid = ((struct ieee80211_qosframe *)wh)->
237					i_qos[0] & IEEE80211_QOS_TID;
238				if (TID_TO_WME_AC(tid) >= WME_AC_VI)
239					ic->ic_wme.wme_hipri_traffic++;
240				tid++;
241			} else
242				tid = IEEE80211_NONQOS_TID;
243			rxseq = le16toh(*(uint16_t *)wh->i_seq);
244			if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
245			    (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
246			    SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
247				/* duplicate, discard */
248				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
249				    bssid, "duplicate",
250				    "seqno <%u,%u> fragno <%u,%u> tid %u",
251				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
252				    ni->ni_rxseqs[tid] >>
253					IEEE80211_SEQ_SEQ_SHIFT,
254				    rxseq & IEEE80211_SEQ_FRAG_MASK,
255				    ni->ni_rxseqs[tid] &
256					IEEE80211_SEQ_FRAG_MASK,
257				    tid);
258				ic->ic_stats.is_rx_dup++;
259				IEEE80211_NODE_STAT(ni, rx_dup);
260				goto out;
261			}
262			ni->ni_rxseqs[tid] = rxseq;
263		}
264	}
265
266	switch (type) {
267	case IEEE80211_FC0_TYPE_DATA:
268		hdrspace = ieee80211_hdrspace(ic, wh);
269		if (m->m_len < hdrspace &&
270		    (m = m_pullup(m, hdrspace)) == NULL) {
271			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
272			    ni->ni_macaddr, NULL,
273			    "data too short: expecting %u", hdrspace);
274			ic->ic_stats.is_rx_tooshort++;
275			goto out;		/* XXX */
276		}
277		switch (ic->ic_opmode) {
278		case IEEE80211_M_STA:
279			if (dir != IEEE80211_FC1_DIR_FROMDS) {
280				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
281				    wh, "data", "unknown dir 0x%x", dir);
282				ic->ic_stats.is_rx_wrongdir++;
283				goto out;
284			}
285			if ((ifp->if_flags & IFF_SIMPLEX) &&
286			    IEEE80211_IS_MULTICAST(wh->i_addr1) &&
287			    IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_myaddr)) {
288				/*
289				 * In IEEE802.11 network, multicast packet
290				 * sent from me is broadcasted from AP.
291				 * It should be silently discarded for
292				 * SIMPLEX interface.
293				 */
294				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
295				    wh, NULL, "%s", "multicast echo");
296				ic->ic_stats.is_rx_mcastecho++;
297				goto out;
298			}
299			break;
300		case IEEE80211_M_IBSS:
301		case IEEE80211_M_AHDEMO:
302			if (dir != IEEE80211_FC1_DIR_NODS) {
303				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
304				    wh, "data", "unknown dir 0x%x", dir);
305				ic->ic_stats.is_rx_wrongdir++;
306				goto out;
307			}
308			/* XXX no power-save support */
309			break;
310		case IEEE80211_M_HOSTAP:
311			if (dir != IEEE80211_FC1_DIR_TODS) {
312				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
313				    wh, "data", "unknown dir 0x%x", dir);
314				ic->ic_stats.is_rx_wrongdir++;
315				goto out;
316			}
317			/* check if source STA is associated */
318			if (ni == ic->ic_bss) {
319				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
320				    wh, "data", "%s", "unknown src");
321				ieee80211_send_error(ic, ni, wh->i_addr2,
322				    IEEE80211_FC0_SUBTYPE_DEAUTH,
323				    IEEE80211_REASON_NOT_AUTHED);
324				ic->ic_stats.is_rx_notassoc++;
325				goto err;
326			}
327			if (ni->ni_associd == 0) {
328				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
329				    wh, "data", "%s", "unassoc src");
330				IEEE80211_SEND_MGMT(ic, ni,
331				    IEEE80211_FC0_SUBTYPE_DISASSOC,
332				    IEEE80211_REASON_NOT_ASSOCED);
333				ic->ic_stats.is_rx_notassoc++;
334				goto err;
335			}
336
337			/*
338			 * Check for power save state change.
339			 * XXX out-of-order A-MPDU frames?
340			 */
341			if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
342			    (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
343				ieee80211_node_pwrsave(ni,
344					wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
345			break;
346		default:
347			/* XXX here to keep compiler happy */
348			goto out;
349		}
350
351		/*
352		 * Handle A-MPDU re-ordering.  The station must be
353		 * associated and negotiated HT.  The frame must be
354		 * a QoS frame (not QoS null data) and not previously
355		 * processed for A-MPDU re-ordering.  If the frame is
356		 * to be processed directly then ieee80211_ampdu_reorder
357		 * will return 0; otherwise it has consumed the mbuf
358		 * and we should do nothing more with it.
359		 */
360		if ((ni->ni_flags & IEEE80211_NODE_HT) &&
361		    subtype == IEEE80211_FC0_SUBTYPE_QOS &&
362		    ieee80211_ampdu_reorder(ni, m) != 0) {
363			m = NULL;
364			goto out;
365		}
366	resubmit_ampdu:
367
368		/*
369		 * Handle privacy requirements.  Note that we
370		 * must not be preempted from here until after
371		 * we (potentially) call ieee80211_crypto_demic;
372		 * otherwise we may violate assumptions in the
373		 * crypto cipher modules used to do delayed update
374		 * of replay sequence numbers.
375		 */
376		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
377			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
378				/*
379				 * Discard encrypted frames when privacy is off.
380				 */
381				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
382				    wh, "WEP", "%s", "PRIVACY off");
383				ic->ic_stats.is_rx_noprivacy++;
384				IEEE80211_NODE_STAT(ni, rx_noprivacy);
385				goto out;
386			}
387			key = ieee80211_crypto_decap(ic, ni, m, hdrspace);
388			if (key == NULL) {
389				/* NB: stats+msgs handled in crypto_decap */
390				IEEE80211_NODE_STAT(ni, rx_wepfail);
391				goto out;
392			}
393			wh = mtod(m, struct ieee80211_frame *);
394			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
395		} else {
396			key = NULL;
397		}
398
399		/*
400		 * Save QoS bits for use below--before we strip the header.
401		 */
402		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
403			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
404			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
405			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
406		} else
407			qos = 0;
408
409		/*
410		 * Next up, any fragmentation.
411		 */
412		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
413			m = ieee80211_defrag(ic, ni, m, hdrspace);
414			if (m == NULL) {
415				/* Fragment dropped or frame not complete yet */
416				goto out;
417			}
418		}
419		wh = NULL;		/* no longer valid, catch any uses */
420
421		/*
422		 * Next strip any MSDU crypto bits.
423		 */
424		if (key != NULL && !ieee80211_crypto_demic(ic, key, m, 0)) {
425			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
426			    ni->ni_macaddr, "data", "%s", "demic error");
427			ic->ic_stats.is_rx_demicfail++;
428			IEEE80211_NODE_STAT(ni, rx_demicfail);
429			goto out;
430		}
431
432		/* copy to listener after decrypt */
433		if (bpf_peers_present(ic->ic_rawbpf))
434			bpf_mtap(ic->ic_rawbpf, m);
435		need_tap = 0;
436
437		/*
438		 * Finally, strip the 802.11 header.
439		 */
440		m = ieee80211_decap(ic, m, hdrspace);
441		if (m == NULL) {
442			/* don't count Null data frames as errors */
443			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
444			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
445				goto out;
446			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
447			    ni->ni_macaddr, "data", "%s", "decap error");
448			ic->ic_stats.is_rx_decap++;
449			IEEE80211_NODE_STAT(ni, rx_decap);
450			goto err;
451		}
452		eh = mtod(m, struct ether_header *);
453		if (!ieee80211_node_is_authorized(ni)) {
454			/*
455			 * Deny any non-PAE frames received prior to
456			 * authorization.  For open/shared-key
457			 * authentication the port is mark authorized
458			 * after authentication completes.  For 802.1x
459			 * the port is not marked authorized by the
460			 * authenticator until the handshake has completed.
461			 */
462			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
463				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
464				    eh->ether_shost, "data",
465				    "unauthorized port: ether type 0x%x len %u",
466				    eh->ether_type, m->m_pkthdr.len);
467				ic->ic_stats.is_rx_unauth++;
468				IEEE80211_NODE_STAT(ni, rx_unauth);
469				goto err;
470			}
471		} else {
472			/*
473			 * When denying unencrypted frames, discard
474			 * any non-PAE frames received without encryption.
475			 */
476			if ((ic->ic_flags & IEEE80211_F_DROPUNENC) &&
477			    key == NULL &&
478			    eh->ether_type != htons(ETHERTYPE_PAE)) {
479				/*
480				 * Drop unencrypted frames.
481				 */
482				ic->ic_stats.is_rx_unencrypted++;
483				IEEE80211_NODE_STAT(ni, rx_unencrypted);
484				goto out;
485			}
486		}
487		/* XXX require HT? */
488		if (qos & IEEE80211_QOS_AMSDU) {
489			m = ieee80211_decap_amsdu(ni, m);
490			if (m == NULL)
491				return IEEE80211_FC0_TYPE_DATA;
492		} else if ((ni->ni_ath_flags & IEEE80211_NODE_FF) &&
493#define	FF_LLC_SIZE	(sizeof(struct ether_header) + sizeof(struct llc))
494		    m->m_pkthdr.len >= 3*FF_LLC_SIZE) {
495			struct llc *llc;
496
497			/*
498			 * Check for fast-frame tunnel encapsulation.
499			 */
500			if (m->m_len < FF_LLC_SIZE &&
501			    (m = m_pullup(m, FF_LLC_SIZE)) == NULL) {
502				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
503				    ni->ni_macaddr, "fast-frame",
504				    "%s", "m_pullup(llc) failed");
505				ic->ic_stats.is_rx_tooshort++;
506				return IEEE80211_FC0_TYPE_DATA;
507			}
508			llc = (struct llc *)(mtod(m, uint8_t *) +
509				sizeof(struct ether_header));
510			if (llc->llc_snap.ether_type == htons(ATH_FF_ETH_TYPE)) {
511				m_adj(m, FF_LLC_SIZE);
512				m = ieee80211_decap_fastframe(ic, ni, m);
513				if (m == NULL)
514					return IEEE80211_FC0_TYPE_DATA;
515			}
516		}
517#undef FF_LLC_SIZE
518		ieee80211_deliver_data(ic, ni, m);
519		return IEEE80211_FC0_TYPE_DATA;
520
521	case IEEE80211_FC0_TYPE_MGT:
522		ic->ic_stats.is_rx_mgmt++;
523		IEEE80211_NODE_STAT(ni, rx_mgmt);
524		if (dir != IEEE80211_FC1_DIR_NODS) {
525			IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
526			    wh, "data", "unknown dir 0x%x", dir);
527			ic->ic_stats.is_rx_wrongdir++;
528			goto err;
529		}
530		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
531			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
532			    ni->ni_macaddr, "mgt", "too short: len %u",
533			    m->m_pkthdr.len);
534			ic->ic_stats.is_rx_tooshort++;
535			goto out;
536		}
537#ifdef IEEE80211_DEBUG
538		if ((ieee80211_msg_debug(ic) && doprint(ic, subtype)) ||
539		    ieee80211_msg_dumppkts(ic)) {
540			if_printf(ic->ic_ifp, "received %s from %s rssi %d\n",
541			    ieee80211_mgt_subtype_name[subtype >>
542				IEEE80211_FC0_SUBTYPE_SHIFT],
543			    ether_sprintf(wh->i_addr2), rssi);
544		}
545#endif
546		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
547			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
548				/*
549				 * Only shared key auth frames with a challenge
550				 * should be encrypted, discard all others.
551				 */
552				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
553				    wh, ieee80211_mgt_subtype_name[subtype >>
554					IEEE80211_FC0_SUBTYPE_SHIFT],
555				    "%s", "WEP set but not permitted");
556				ic->ic_stats.is_rx_mgtdiscard++; /* XXX */
557				goto out;
558			}
559			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
560				/*
561				 * Discard encrypted frames when privacy is off.
562				 */
563				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
564				    wh, "mgt", "%s", "WEP set but PRIVACY off");
565				ic->ic_stats.is_rx_noprivacy++;
566				goto out;
567			}
568			hdrspace = ieee80211_hdrspace(ic, wh);
569			key = ieee80211_crypto_decap(ic, ni, m, hdrspace);
570			if (key == NULL) {
571				/* NB: stats+msgs handled in crypto_decap */
572				goto out;
573			}
574			wh = mtod(m, struct ieee80211_frame *);
575			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
576		}
577		if (bpf_peers_present(ic->ic_rawbpf))
578			bpf_mtap(ic->ic_rawbpf, m);
579		(*ic->ic_recv_mgmt)(ic, m, ni, subtype, rssi, noise, rstamp);
580		m_freem(m);
581		return IEEE80211_FC0_TYPE_MGT;
582
583	case IEEE80211_FC0_TYPE_CTL:
584		ic->ic_stats.is_rx_ctl++;
585		IEEE80211_NODE_STAT(ni, rx_ctrl);
586		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
587			switch (subtype) {
588			case IEEE80211_FC0_SUBTYPE_PS_POLL:
589				ieee80211_recv_pspoll(ic, ni, m);
590				break;
591			case IEEE80211_FC0_SUBTYPE_BAR:
592				ieee80211_recv_bar(ni, m);
593				break;
594			}
595		}
596		goto out;
597	default:
598		IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
599		    wh, NULL, "bad frame type 0x%x", type);
600		/* should not come here */
601		break;
602	}
603err:
604	ifp->if_ierrors++;
605out:
606	if (m != NULL) {
607		if (bpf_peers_present(ic->ic_rawbpf) && need_tap)
608			bpf_mtap(ic->ic_rawbpf, m);
609		m_freem(m);
610	}
611	return type;
612#undef SEQ_LEQ
613}
614
615/*
616 * This function reassemble fragments.
617 */
618static struct mbuf *
619ieee80211_defrag(struct ieee80211com *ic, struct ieee80211_node *ni,
620	struct mbuf *m, int hdrspace)
621{
622	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
623	struct ieee80211_frame *lwh;
624	uint16_t rxseq;
625	uint8_t fragno;
626	uint8_t more_frag = wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG;
627	struct mbuf *mfrag;
628
629	KASSERT(!IEEE80211_IS_MULTICAST(wh->i_addr1), ("multicast fragm?"));
630
631	rxseq = le16toh(*(uint16_t *)wh->i_seq);
632	fragno = rxseq & IEEE80211_SEQ_FRAG_MASK;
633
634	/* Quick way out, if there's nothing to defragment */
635	if (!more_frag && fragno == 0 && ni->ni_rxfrag[0] == NULL)
636		return m;
637
638	/*
639	 * Remove frag to insure it doesn't get reaped by timer.
640	 */
641	if (ni->ni_table == NULL) {
642		/*
643		 * Should never happen.  If the node is orphaned (not in
644		 * the table) then input packets should not reach here.
645		 * Otherwise, a concurrent request that yanks the table
646		 * should be blocked by other interlocking and/or by first
647		 * shutting the driver down.  Regardless, be defensive
648		 * here and just bail
649		 */
650		/* XXX need msg+stat */
651		m_freem(m);
652		return NULL;
653	}
654	IEEE80211_NODE_LOCK(ni->ni_table);
655	mfrag = ni->ni_rxfrag[0];
656	ni->ni_rxfrag[0] = NULL;
657	IEEE80211_NODE_UNLOCK(ni->ni_table);
658
659	/*
660	 * Validate new fragment is in order and
661	 * related to the previous ones.
662	 */
663	if (mfrag != NULL) {
664		uint16_t last_rxseq;
665
666		lwh = mtod(mfrag, struct ieee80211_frame *);
667		last_rxseq = le16toh(*(uint16_t *)lwh->i_seq);
668		/* NB: check seq # and frag together */
669		if (rxseq != last_rxseq+1 ||
670		    !IEEE80211_ADDR_EQ(wh->i_addr1, lwh->i_addr1) ||
671		    !IEEE80211_ADDR_EQ(wh->i_addr2, lwh->i_addr2)) {
672			/*
673			 * Unrelated fragment or no space for it,
674			 * clear current fragments.
675			 */
676			m_freem(mfrag);
677			mfrag = NULL;
678		}
679	}
680
681 	if (mfrag == NULL) {
682		if (fragno != 0) {		/* !first fragment, discard */
683			ic->ic_stats.is_rx_defrag++;
684			IEEE80211_NODE_STAT(ni, rx_defrag);
685			m_freem(m);
686			return NULL;
687		}
688		mfrag = m;
689	} else {				/* concatenate */
690		m_adj(m, hdrspace);		/* strip header */
691		m_cat(mfrag, m);
692		/* NB: m_cat doesn't update the packet header */
693		mfrag->m_pkthdr.len += m->m_pkthdr.len;
694		/* track last seqnum and fragno */
695		lwh = mtod(mfrag, struct ieee80211_frame *);
696		*(uint16_t *) lwh->i_seq = *(uint16_t *) wh->i_seq;
697	}
698	if (more_frag) {			/* more to come, save */
699		ni->ni_rxfragstamp = ticks;
700		ni->ni_rxfrag[0] = mfrag;
701		mfrag = NULL;
702	}
703	return mfrag;
704}
705
706void
707ieee80211_deliver_data(struct ieee80211com *ic,
708	struct ieee80211_node *ni, struct mbuf *m)
709{
710	struct ether_header *eh = mtod(m, struct ether_header *);
711	struct ifnet *ifp = ic->ic_ifp;
712
713	/*
714	 * Do accounting.
715	 */
716	ifp->if_ipackets++;
717	IEEE80211_NODE_STAT(ni, rx_data);
718	IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
719	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
720		m->m_flags |= M_MCAST;		/* XXX M_BCAST? */
721		IEEE80211_NODE_STAT(ni, rx_mcast);
722	} else
723		IEEE80211_NODE_STAT(ni, rx_ucast);
724
725	/* perform as a bridge within the AP */
726	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
727	    (ic->ic_flags & IEEE80211_F_NOBRIDGE) == 0) {
728		struct mbuf *m1 = NULL;
729
730		if (m->m_flags & M_MCAST) {
731			m1 = m_dup(m, M_DONTWAIT);
732			if (m1 == NULL)
733				ifp->if_oerrors++;
734			else
735				m1->m_flags |= M_MCAST;
736		} else {
737			/*
738			 * Check if the destination is known; if so
739			 * and the port is authorized dispatch directly.
740			 */
741			struct ieee80211_node *sta =
742			    ieee80211_find_node(&ic->ic_sta, eh->ether_dhost);
743			if (sta != NULL) {
744				if (ieee80211_node_is_authorized(sta)) {
745					/*
746					 * Beware of sending to ourself; this
747					 * needs to happen via the normal
748					 * input path.
749					 */
750					if (sta != ic->ic_bss) {
751						m1 = m;
752						m = NULL;
753					}
754				} else {
755					ic->ic_stats.is_rx_unauth++;
756					IEEE80211_NODE_STAT(sta, rx_unauth);
757				}
758				ieee80211_free_node(sta);
759			}
760		}
761		if (m1 != NULL)
762			(void) IF_HANDOFF(&ifp->if_snd, m1, ifp);
763	}
764	if (m != NULL) {
765		m->m_pkthdr.rcvif = ifp;
766		if (ni->ni_vlan != 0) {
767			/* attach vlan tag */
768			m->m_pkthdr.ether_vtag = ni->ni_vlan;
769			m->m_flags |= M_VLANTAG;
770		}
771		(*ifp->if_input)(ifp, m);
772	}
773}
774
775static struct mbuf *
776ieee80211_decap(struct ieee80211com *ic, struct mbuf *m, int hdrlen)
777{
778	struct ieee80211_qosframe_addr4 wh;	/* Max size address frames */
779	struct ether_header *eh;
780	struct llc *llc;
781
782	if (m->m_len < hdrlen + sizeof(*llc) &&
783	    (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
784		/* XXX stat, msg */
785		return NULL;
786	}
787	memcpy(&wh, mtod(m, caddr_t), hdrlen);
788	llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
789	if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
790	    llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
791	    llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0) {
792		m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
793		llc = NULL;
794	} else {
795		m_adj(m, hdrlen - sizeof(*eh));
796	}
797	eh = mtod(m, struct ether_header *);
798	switch (wh.i_fc[1] & IEEE80211_FC1_DIR_MASK) {
799	case IEEE80211_FC1_DIR_NODS:
800		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
801		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
802		break;
803	case IEEE80211_FC1_DIR_TODS:
804		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3);
805		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
806		break;
807	case IEEE80211_FC1_DIR_FROMDS:
808		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
809		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr3);
810		break;
811	case IEEE80211_FC1_DIR_DSTODS:
812		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3);
813		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr4);
814		break;
815	}
816#ifdef ALIGNED_POINTER
817	if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), uint32_t)) {
818		struct mbuf *n, *n0, **np;
819		caddr_t newdata;
820		int off, pktlen;
821
822		n0 = NULL;
823		np = &n0;
824		off = 0;
825		pktlen = m->m_pkthdr.len;
826		while (pktlen > off) {
827			if (n0 == NULL) {
828				MGETHDR(n, M_DONTWAIT, MT_DATA);
829				if (n == NULL) {
830					m_freem(m);
831					return NULL;
832				}
833				M_MOVE_PKTHDR(n, m);
834				n->m_len = MHLEN;
835			} else {
836				MGET(n, M_DONTWAIT, MT_DATA);
837				if (n == NULL) {
838					m_freem(m);
839					m_freem(n0);
840					return NULL;
841				}
842				n->m_len = MLEN;
843			}
844			if (pktlen - off >= MINCLSIZE) {
845				MCLGET(n, M_DONTWAIT);
846				if (n->m_flags & M_EXT)
847					n->m_len = n->m_ext.ext_size;
848			}
849			if (n0 == NULL) {
850				newdata =
851				    (caddr_t)ALIGN(n->m_data + sizeof(*eh)) -
852				    sizeof(*eh);
853				n->m_len -= newdata - n->m_data;
854				n->m_data = newdata;
855			}
856			if (n->m_len > pktlen - off)
857				n->m_len = pktlen - off;
858			m_copydata(m, off, n->m_len, mtod(n, caddr_t));
859			off += n->m_len;
860			*np = n;
861			np = &n->m_next;
862		}
863		m_freem(m);
864		m = n0;
865	}
866#endif /* ALIGNED_POINTER */
867	if (llc != NULL) {
868		eh = mtod(m, struct ether_header *);
869		eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
870	}
871	return m;
872}
873
874/*
875 * Decap a frame encapsulated in a fast-frame/A-MSDU.
876 */
877struct mbuf *
878ieee80211_decap1(struct mbuf *m, int *framelen)
879{
880#define	FF_LLC_SIZE	(sizeof(struct ether_header) + sizeof(struct llc))
881	struct ether_header *eh;
882	struct llc *llc;
883
884	/*
885	 * The frame has an 802.3 header followed by an 802.2
886	 * LLC header.  The encapsulated frame length is in the
887	 * first header type field; save that and overwrite it
888	 * with the true type field found in the second.  Then
889	 * copy the 802.3 header up to where it belongs and
890	 * adjust the mbuf contents to remove the void.
891	 */
892	if (m->m_len < FF_LLC_SIZE && (m = m_pullup(m, FF_LLC_SIZE)) == NULL)
893		return NULL;
894	eh = mtod(m, struct ether_header *);	/* 802.3 header is first */
895	llc = (struct llc *)&eh[1];		/* 802.2 header follows */
896	*framelen = ntohs(eh->ether_type)	/* encap'd frame size */
897		  + sizeof(struct ether_header) - sizeof(struct llc);
898	eh->ether_type = llc->llc_un.type_snap.ether_type;
899	ovbcopy(eh, mtod(m, uint8_t *) + sizeof(struct llc),
900		sizeof(struct ether_header));
901	m_adj(m, sizeof(struct llc));
902	return m;
903#undef FF_LLC_SIZE
904}
905
906/*
907 * Decap the encapsulated frame pair and dispatch the first
908 * for delivery.  The second frame is returned for delivery
909 * via the normal path.
910 */
911static struct mbuf *
912ieee80211_decap_fastframe(struct ieee80211com *ic,
913	struct ieee80211_node *ni, struct mbuf *m)
914{
915#define	MS(x,f)	(((x) & f) >> f##_S)
916	uint32_t ath;
917	struct mbuf *n;
918	int framelen;
919
920	m_copydata(m, 0, sizeof(uint32_t), (caddr_t) &ath);
921	if (MS(ath, ATH_FF_PROTO) != ATH_FF_PROTO_L2TUNNEL) {
922		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
923		    ni->ni_macaddr, "fast-frame",
924		    "unsupport tunnel protocol, header 0x%x", ath);
925		ic->ic_stats.is_ff_badhdr++;
926		m_freem(m);
927		return NULL;
928	}
929	/* NB: skip header and alignment padding */
930	m_adj(m, roundup(sizeof(uint32_t) - 2, 4) + 2);
931
932	ic->ic_stats.is_ff_decap++;
933
934	/*
935	 * Decap the first frame, bust it apart from the
936	 * second and deliver; then decap the second frame
937	 * and return it to the caller for normal delivery.
938	 */
939	m = ieee80211_decap1(m, &framelen);
940	if (m == NULL) {
941		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
942		    ni->ni_macaddr, "fast-frame", "%s", "first decap failed");
943		ic->ic_stats.is_ff_tooshort++;
944		return NULL;
945	}
946	n = m_split(m, framelen, M_NOWAIT);
947	if (n == NULL) {
948		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
949		    ni->ni_macaddr, "fast-frame",
950		    "%s", "unable to split encapsulated frames");
951		ic->ic_stats.is_ff_split++;
952		m_freem(m);			/* NB: must reclaim */
953		return NULL;
954	}
955	ieee80211_deliver_data(ic, ni, m);	/* 1st of pair */
956
957	/*
958	 * Decap second frame.
959	 */
960	m_adj(n, roundup2(framelen, 4) - framelen);	/* padding */
961	n = ieee80211_decap1(n, &framelen);
962	if (n == NULL) {
963		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
964		    ni->ni_macaddr, "fast-frame", "%s", "second decap failed");
965		ic->ic_stats.is_ff_tooshort++;
966	}
967	/* XXX verify framelen against mbuf contents */
968	return n;				/* 2nd delivered by caller */
969#undef MS
970}
971
972/*
973 * Install received rate set information in the node's state block.
974 */
975int
976ieee80211_setup_rates(struct ieee80211_node *ni,
977	const uint8_t *rates, const uint8_t *xrates, int flags)
978{
979	struct ieee80211com *ic = ni->ni_ic;
980	struct ieee80211_rateset *rs = &ni->ni_rates;
981
982	memset(rs, 0, sizeof(*rs));
983	rs->rs_nrates = rates[1];
984	memcpy(rs->rs_rates, rates + 2, rs->rs_nrates);
985	if (xrates != NULL) {
986		uint8_t nxrates;
987		/*
988		 * Tack on 11g extended supported rate element.
989		 */
990		nxrates = xrates[1];
991		if (rs->rs_nrates + nxrates > IEEE80211_RATE_MAXSIZE) {
992			nxrates = IEEE80211_RATE_MAXSIZE - rs->rs_nrates;
993			IEEE80211_DPRINTF(ic, IEEE80211_MSG_XRATE,
994			     "[%s] extended rate set too large;"
995			     " only using %u of %u rates\n",
996			     ether_sprintf(ni->ni_macaddr), nxrates, xrates[1]);
997			ic->ic_stats.is_rx_rstoobig++;
998		}
999		memcpy(rs->rs_rates + rs->rs_nrates, xrates+2, nxrates);
1000		rs->rs_nrates += nxrates;
1001	}
1002	return ieee80211_fix_rate(ni, rs, flags);
1003}
1004
1005static void
1006ieee80211_auth_open(struct ieee80211com *ic, struct ieee80211_frame *wh,
1007    struct ieee80211_node *ni, int rssi, int noise, uint32_t rstamp,
1008    uint16_t seq, uint16_t status)
1009{
1010
1011	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
1012		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1013		    ni->ni_macaddr, "open auth",
1014		    "bad sta auth mode %u", ni->ni_authmode);
1015		ic->ic_stats.is_rx_bad_auth++;	/* XXX */
1016		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
1017			/*
1018			 * Clear any challenge text that may be there if
1019			 * a previous shared key auth failed and then an
1020			 * open auth is attempted.
1021			 */
1022			if (ni->ni_challenge != NULL) {
1023				FREE(ni->ni_challenge, M_80211_NODE);
1024				ni->ni_challenge = NULL;
1025			}
1026			/* XXX hack to workaround calling convention */
1027			ieee80211_send_error(ic, ni, wh->i_addr2,
1028			    IEEE80211_FC0_SUBTYPE_AUTH,
1029			    (seq + 1) | (IEEE80211_STATUS_ALG<<16));
1030		}
1031		return;
1032	}
1033	switch (ic->ic_opmode) {
1034	case IEEE80211_M_IBSS:
1035	case IEEE80211_M_AHDEMO:
1036	case IEEE80211_M_MONITOR:
1037	case IEEE80211_M_WDS:
1038		/* should not come here */
1039		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1040		    ni->ni_macaddr, "open auth",
1041		    "bad operating mode %u", ic->ic_opmode);
1042		break;
1043
1044	case IEEE80211_M_HOSTAP:
1045		if (ic->ic_state != IEEE80211_S_RUN ||
1046		    seq != IEEE80211_AUTH_OPEN_REQUEST) {
1047			ic->ic_stats.is_rx_bad_auth++;
1048			return;
1049		}
1050		/* always accept open authentication requests */
1051		if (ni == ic->ic_bss) {
1052			ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);
1053			if (ni == NULL)
1054				return;
1055		} else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1056			(void) ieee80211_ref_node(ni);
1057		/*
1058		 * Mark the node as referenced to reflect that it's
1059		 * reference count has been bumped to insure it remains
1060		 * after the transaction completes.
1061		 */
1062		ni->ni_flags |= IEEE80211_NODE_AREF;
1063
1064		IEEE80211_SEND_MGMT(ic, ni,
1065			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1066		IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1067		    "[%s] station authenticated (open)\n",
1068		    ether_sprintf(ni->ni_macaddr));
1069		/*
1070		 * When 802.1x is not in use mark the port
1071		 * authorized at this point so traffic can flow.
1072		 */
1073		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
1074			ieee80211_node_authorize(ni);
1075		break;
1076
1077	case IEEE80211_M_STA:
1078		if (ic->ic_state != IEEE80211_S_AUTH ||
1079		    seq != IEEE80211_AUTH_OPEN_RESPONSE) {
1080			ic->ic_stats.is_rx_bad_auth++;
1081			return;
1082		}
1083		if (status != 0) {
1084			IEEE80211_DPRINTF(ic,
1085			    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1086			    "[%s] open auth failed (reason %d)\n",
1087			    ether_sprintf(ni->ni_macaddr), status);
1088			/* XXX can this happen? */
1089			if (ni != ic->ic_bss)
1090				ni->ni_fails++;
1091			ic->ic_stats.is_rx_auth_fail++;
1092			ieee80211_new_state(ic, IEEE80211_S_SCAN,
1093			    IEEE80211_SCAN_FAIL_STATUS);
1094		} else
1095			ieee80211_new_state(ic, IEEE80211_S_ASSOC, 0);
1096		break;
1097	}
1098}
1099
1100/*
1101 * Send a management frame error response to the specified
1102 * station.  If ni is associated with the station then use
1103 * it; otherwise allocate a temporary node suitable for
1104 * transmitting the frame and then free the reference so
1105 * it will go away as soon as the frame has been transmitted.
1106 */
1107static void
1108ieee80211_send_error(struct ieee80211com *ic, struct ieee80211_node *ni,
1109	const uint8_t *mac, int subtype, int arg)
1110{
1111	int istmp;
1112
1113	if (ni == ic->ic_bss) {
1114		ni = ieee80211_tmp_node(ic, mac);
1115		if (ni == NULL) {
1116			/* XXX msg */
1117			return;
1118		}
1119		istmp = 1;
1120	} else
1121		istmp = 0;
1122	IEEE80211_SEND_MGMT(ic, ni, subtype, arg);
1123	if (istmp)
1124		ieee80211_free_node(ni);
1125}
1126
1127static int
1128alloc_challenge(struct ieee80211com *ic, struct ieee80211_node *ni)
1129{
1130	if (ni->ni_challenge == NULL)
1131		MALLOC(ni->ni_challenge, uint32_t*, IEEE80211_CHALLENGE_LEN,
1132		    M_80211_NODE, M_NOWAIT);
1133	if (ni->ni_challenge == NULL) {
1134		IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1135		    "[%s] shared key challenge alloc failed\n",
1136		    ether_sprintf(ni->ni_macaddr));
1137		/* XXX statistic */
1138	}
1139	return (ni->ni_challenge != NULL);
1140}
1141
1142/* XXX TODO: add statistics */
1143static void
1144ieee80211_auth_shared(struct ieee80211com *ic, struct ieee80211_frame *wh,
1145    uint8_t *frm, uint8_t *efrm, struct ieee80211_node *ni,
1146    int rssi, int noise, uint32_t rstamp, uint16_t seq, uint16_t status)
1147{
1148	uint8_t *challenge;
1149	int allocbs, estatus;
1150
1151	/*
1152	 * NB: this can happen as we allow pre-shared key
1153	 * authentication to be enabled w/o wep being turned
1154	 * on so that configuration of these can be done
1155	 * in any order.  It may be better to enforce the
1156	 * ordering in which case this check would just be
1157	 * for sanity/consistency.
1158	 */
1159	if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
1160		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1161		    ni->ni_macaddr, "shared key auth",
1162		    "%s", " PRIVACY is disabled");
1163		estatus = IEEE80211_STATUS_ALG;
1164		goto bad;
1165	}
1166	/*
1167	 * Pre-shared key authentication is evil; accept
1168	 * it only if explicitly configured (it is supported
1169	 * mainly for compatibility with clients like OS X).
1170	 */
1171	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1172	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1173		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1174		    ni->ni_macaddr, "shared key auth",
1175		    "bad sta auth mode %u", ni->ni_authmode);
1176		ic->ic_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
1177		estatus = IEEE80211_STATUS_ALG;
1178		goto bad;
1179	}
1180
1181	challenge = NULL;
1182	if (frm + 1 < efrm) {
1183		if ((frm[1] + 2) > (efrm - frm)) {
1184			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1185			    ni->ni_macaddr, "shared key auth",
1186			    "ie %d/%d too long",
1187			    frm[0], (frm[1] + 2) - (efrm - frm));
1188			ic->ic_stats.is_rx_bad_auth++;
1189			estatus = IEEE80211_STATUS_CHALLENGE;
1190			goto bad;
1191		}
1192		if (*frm == IEEE80211_ELEMID_CHALLENGE)
1193			challenge = frm;
1194		frm += frm[1] + 2;
1195	}
1196	switch (seq) {
1197	case IEEE80211_AUTH_SHARED_CHALLENGE:
1198	case IEEE80211_AUTH_SHARED_RESPONSE:
1199		if (challenge == NULL) {
1200			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1201			    ni->ni_macaddr, "shared key auth",
1202			    "%s", "no challenge");
1203			ic->ic_stats.is_rx_bad_auth++;
1204			estatus = IEEE80211_STATUS_CHALLENGE;
1205			goto bad;
1206		}
1207		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1208			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1209			    ni->ni_macaddr, "shared key auth",
1210			    "bad challenge len %d", challenge[1]);
1211			ic->ic_stats.is_rx_bad_auth++;
1212			estatus = IEEE80211_STATUS_CHALLENGE;
1213			goto bad;
1214		}
1215	default:
1216		break;
1217	}
1218	switch (ic->ic_opmode) {
1219	case IEEE80211_M_MONITOR:
1220	case IEEE80211_M_AHDEMO:
1221	case IEEE80211_M_IBSS:
1222	case IEEE80211_M_WDS:
1223		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1224		    ni->ni_macaddr, "shared key auth",
1225		    "bad operating mode %u", ic->ic_opmode);
1226		return;
1227	case IEEE80211_M_HOSTAP:
1228		if (ic->ic_state != IEEE80211_S_RUN) {
1229			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1230			    ni->ni_macaddr, "shared key auth",
1231			    "bad state %u", ic->ic_state);
1232			estatus = IEEE80211_STATUS_ALG;	/* XXX */
1233			goto bad;
1234		}
1235		switch (seq) {
1236		case IEEE80211_AUTH_SHARED_REQUEST:
1237			if (ni == ic->ic_bss) {
1238				ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);
1239				if (ni == NULL) {
1240					/* NB: no way to return an error */
1241					return;
1242				}
1243				allocbs = 1;
1244			} else {
1245				if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1246					(void) ieee80211_ref_node(ni);
1247				allocbs = 0;
1248			}
1249			/*
1250			 * Mark the node as referenced to reflect that it's
1251			 * reference count has been bumped to insure it remains
1252			 * after the transaction completes.
1253			 */
1254			ni->ni_flags |= IEEE80211_NODE_AREF;
1255			ni->ni_rssi = rssi;
1256			ni->ni_noise = noise;
1257			ni->ni_rstamp = rstamp;
1258			if (!alloc_challenge(ic, ni)) {
1259				/* NB: don't return error so they rexmit */
1260				return;
1261			}
1262			get_random_bytes(ni->ni_challenge,
1263				IEEE80211_CHALLENGE_LEN);
1264			IEEE80211_DPRINTF(ic,
1265				IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1266				"[%s] shared key %sauth request\n",
1267				ether_sprintf(ni->ni_macaddr),
1268				allocbs ? "" : "re");
1269			break;
1270		case IEEE80211_AUTH_SHARED_RESPONSE:
1271			if (ni == ic->ic_bss) {
1272				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1273				    ni->ni_macaddr, "shared key response",
1274				    "%s", "unknown station");
1275				/* NB: don't send a response */
1276				return;
1277			}
1278			if (ni->ni_challenge == NULL) {
1279				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1280				    ni->ni_macaddr, "shared key response",
1281				    "%s", "no challenge recorded");
1282				ic->ic_stats.is_rx_bad_auth++;
1283				estatus = IEEE80211_STATUS_CHALLENGE;
1284				goto bad;
1285			}
1286			if (memcmp(ni->ni_challenge, &challenge[2],
1287			           challenge[1]) != 0) {
1288				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1289				    ni->ni_macaddr, "shared key response",
1290				    "%s", "challenge mismatch");
1291				ic->ic_stats.is_rx_auth_fail++;
1292				estatus = IEEE80211_STATUS_CHALLENGE;
1293				goto bad;
1294			}
1295			IEEE80211_DPRINTF(ic,
1296			    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1297			    "[%s] station authenticated (shared key)\n",
1298			    ether_sprintf(ni->ni_macaddr));
1299			ieee80211_node_authorize(ni);
1300			break;
1301		default:
1302			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1303			    ni->ni_macaddr, "shared key auth",
1304			    "bad seq %d", seq);
1305			ic->ic_stats.is_rx_bad_auth++;
1306			estatus = IEEE80211_STATUS_SEQUENCE;
1307			goto bad;
1308		}
1309		IEEE80211_SEND_MGMT(ic, ni,
1310			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1311		break;
1312
1313	case IEEE80211_M_STA:
1314		if (ic->ic_state != IEEE80211_S_AUTH)
1315			return;
1316		switch (seq) {
1317		case IEEE80211_AUTH_SHARED_PASS:
1318			if (ni->ni_challenge != NULL) {
1319				FREE(ni->ni_challenge, M_80211_NODE);
1320				ni->ni_challenge = NULL;
1321			}
1322			if (status != 0) {
1323				IEEE80211_DPRINTF(ic,
1324				    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1325				    "[%s] shared key auth failed (reason %d)\n",
1326				    ether_sprintf(ieee80211_getbssid(ic, wh)),
1327				    status);
1328				/* XXX can this happen? */
1329				if (ni != ic->ic_bss)
1330					ni->ni_fails++;
1331				ic->ic_stats.is_rx_auth_fail++;
1332				return;
1333			}
1334			ieee80211_new_state(ic, IEEE80211_S_ASSOC, 0);
1335			break;
1336		case IEEE80211_AUTH_SHARED_CHALLENGE:
1337			if (!alloc_challenge(ic, ni))
1338				return;
1339			/* XXX could optimize by passing recvd challenge */
1340			memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
1341			IEEE80211_SEND_MGMT(ic, ni,
1342				IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1343			break;
1344		default:
1345			IEEE80211_DISCARD(ic, IEEE80211_MSG_AUTH,
1346			    wh, "shared key auth", "bad seq %d", seq);
1347			ic->ic_stats.is_rx_bad_auth++;
1348			return;
1349		}
1350		break;
1351	}
1352	return;
1353bad:
1354	/*
1355	 * Send an error response; but only when operating as an AP.
1356	 */
1357	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
1358		/* XXX hack to workaround calling convention */
1359		ieee80211_send_error(ic, ni, wh->i_addr2,
1360		    IEEE80211_FC0_SUBTYPE_AUTH,
1361		    (seq + 1) | (estatus<<16));
1362	} else if (ic->ic_opmode == IEEE80211_M_STA) {
1363		/*
1364		 * Kick the state machine.  This short-circuits
1365		 * using the mgt frame timeout to trigger the
1366		 * state transition.
1367		 */
1368		if (ic->ic_state == IEEE80211_S_AUTH)
1369			ieee80211_new_state(ic, IEEE80211_S_SCAN,
1370			    IEEE80211_SCAN_FAIL_STATUS);
1371	}
1372}
1373
1374/* Verify the existence and length of __elem or get out. */
1375#define IEEE80211_VERIFY_ELEMENT(__elem, __maxlen) do {			\
1376	if ((__elem) == NULL) {						\
1377		IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,		\
1378		    wh, ieee80211_mgt_subtype_name[subtype >>		\
1379			IEEE80211_FC0_SUBTYPE_SHIFT],			\
1380		    "%s", "no " #__elem );				\
1381		ic->ic_stats.is_rx_elem_missing++;			\
1382		return;							\
1383	}								\
1384	if ((__elem)[1] > (__maxlen)) {					\
1385		IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,		\
1386		    wh, ieee80211_mgt_subtype_name[subtype >>		\
1387			IEEE80211_FC0_SUBTYPE_SHIFT],			\
1388		    "bad " #__elem " len %d", (__elem)[1]);		\
1389		ic->ic_stats.is_rx_elem_toobig++;			\
1390		return;							\
1391	}								\
1392} while (0)
1393
1394#define	IEEE80211_VERIFY_LENGTH(_len, _minlen, _action) do {		\
1395	if ((_len) < (_minlen)) {					\
1396		IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,		\
1397		    wh, ieee80211_mgt_subtype_name[subtype >>		\
1398			IEEE80211_FC0_SUBTYPE_SHIFT],			\
1399		    "ie too short, got %d, expected %d",		\
1400		    (_len), (_minlen));					\
1401		ic->ic_stats.is_rx_elem_toosmall++;			\
1402		_action;						\
1403	}								\
1404} while (0)
1405
1406#ifdef IEEE80211_DEBUG
1407static void
1408ieee80211_ssid_mismatch(struct ieee80211com *ic, const char *tag,
1409	uint8_t mac[IEEE80211_ADDR_LEN], uint8_t *ssid)
1410{
1411	printf("[%s] discard %s frame, ssid mismatch: ",
1412		ether_sprintf(mac), tag);
1413	ieee80211_print_essid(ssid + 2, ssid[1]);
1414	printf("\n");
1415}
1416
1417#define	IEEE80211_VERIFY_SSID(_ni, _ssid) do {				\
1418	if ((_ssid)[1] != 0 &&						\
1419	    ((_ssid)[1] != (_ni)->ni_esslen ||				\
1420	    memcmp((_ssid) + 2, (_ni)->ni_essid, (_ssid)[1]) != 0)) {	\
1421		if (ieee80211_msg_input(ic))				\
1422			ieee80211_ssid_mismatch(ic, 			\
1423			    ieee80211_mgt_subtype_name[subtype >>	\
1424				IEEE80211_FC0_SUBTYPE_SHIFT],		\
1425				wh->i_addr2, _ssid);			\
1426		ic->ic_stats.is_rx_ssidmismatch++;			\
1427		return;							\
1428	}								\
1429} while (0)
1430#else /* !IEEE80211_DEBUG */
1431#define	IEEE80211_VERIFY_SSID(_ni, _ssid) do {				\
1432	if ((_ssid)[1] != 0 &&						\
1433	    ((_ssid)[1] != (_ni)->ni_esslen ||				\
1434	    memcmp((_ssid) + 2, (_ni)->ni_essid, (_ssid)[1]) != 0)) {	\
1435		ic->ic_stats.is_rx_ssidmismatch++;			\
1436		return;							\
1437	}								\
1438} while (0)
1439#endif /* !IEEE80211_DEBUG */
1440
1441/* unalligned little endian access */
1442#define LE_READ_2(p)					\
1443	((uint16_t)					\
1444	 ((((const uint8_t *)(p))[0]      ) |		\
1445	  (((const uint8_t *)(p))[1] <<  8)))
1446#define LE_READ_4(p)					\
1447	((uint32_t)					\
1448	 ((((const uint8_t *)(p))[0]      ) |		\
1449	  (((const uint8_t *)(p))[1] <<  8) |		\
1450	  (((const uint8_t *)(p))[2] << 16) |		\
1451	  (((const uint8_t *)(p))[3] << 24)))
1452
1453static __inline int
1454iswpaoui(const uint8_t *frm)
1455{
1456	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
1457}
1458
1459static __inline int
1460iswmeoui(const uint8_t *frm)
1461{
1462	return frm[1] > 3 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI);
1463}
1464
1465static __inline int
1466iswmeparam(const uint8_t *frm)
1467{
1468	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
1469		frm[6] == WME_PARAM_OUI_SUBTYPE;
1470}
1471
1472static __inline int
1473iswmeinfo(const uint8_t *frm)
1474{
1475	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
1476		frm[6] == WME_INFO_OUI_SUBTYPE;
1477}
1478
1479static __inline int
1480isatherosoui(const uint8_t *frm)
1481{
1482	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
1483}
1484
1485static __inline int
1486ishtcapoui(const uint8_t *frm)
1487{
1488	return frm[1] > 3 && LE_READ_4(frm+2) == ((BCM_OUI_HTCAP<<24)|BCM_OUI);
1489}
1490
1491static __inline int
1492ishtinfooui(const uint8_t *frm)
1493{
1494	return frm[1] > 3 && LE_READ_4(frm+2) == ((BCM_OUI_HTINFO<<24)|BCM_OUI);
1495}
1496
1497/*
1498 * Convert a WPA cipher selector OUI to an internal
1499 * cipher algorithm.  Where appropriate we also
1500 * record any key length.
1501 */
1502static int
1503wpa_cipher(uint8_t *sel, uint8_t *keylen)
1504{
1505#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1506	uint32_t w = LE_READ_4(sel);
1507
1508	switch (w) {
1509	case WPA_SEL(WPA_CSE_NULL):
1510		return IEEE80211_CIPHER_NONE;
1511	case WPA_SEL(WPA_CSE_WEP40):
1512		if (keylen)
1513			*keylen = 40 / NBBY;
1514		return IEEE80211_CIPHER_WEP;
1515	case WPA_SEL(WPA_CSE_WEP104):
1516		if (keylen)
1517			*keylen = 104 / NBBY;
1518		return IEEE80211_CIPHER_WEP;
1519	case WPA_SEL(WPA_CSE_TKIP):
1520		return IEEE80211_CIPHER_TKIP;
1521	case WPA_SEL(WPA_CSE_CCMP):
1522		return IEEE80211_CIPHER_AES_CCM;
1523	}
1524	return 32;		/* NB: so 1<< is discarded */
1525#undef WPA_SEL
1526}
1527
1528/*
1529 * Convert a WPA key management/authentication algorithm
1530 * to an internal code.
1531 */
1532static int
1533wpa_keymgmt(uint8_t *sel)
1534{
1535#define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1536	uint32_t w = LE_READ_4(sel);
1537
1538	switch (w) {
1539	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
1540		return WPA_ASE_8021X_UNSPEC;
1541	case WPA_SEL(WPA_ASE_8021X_PSK):
1542		return WPA_ASE_8021X_PSK;
1543	case WPA_SEL(WPA_ASE_NONE):
1544		return WPA_ASE_NONE;
1545	}
1546	return 0;		/* NB: so is discarded */
1547#undef WPA_SEL
1548}
1549
1550/*
1551 * Parse a WPA information element to collect parameters
1552 * and validate the parameters against what has been
1553 * configured for the system.
1554 */
1555static int
1556ieee80211_parse_wpa(struct ieee80211com *ic, uint8_t *frm,
1557	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1558{
1559	uint8_t len = frm[1];
1560	uint32_t w;
1561	int n;
1562
1563	/*
1564	 * Check the length once for fixed parts: OUI, type,
1565	 * version, mcast cipher, and 2 selector counts.
1566	 * Other, variable-length data, must be checked separately.
1567	 */
1568	if ((ic->ic_flags & IEEE80211_F_WPA1) == 0) {
1569		IEEE80211_DISCARD_IE(ic,
1570		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1571		    wh, "WPA", "not WPA, flags 0x%x", ic->ic_flags);
1572		return IEEE80211_REASON_IE_INVALID;
1573	}
1574	if (len < 14) {
1575		IEEE80211_DISCARD_IE(ic,
1576		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1577		    wh, "WPA", "too short, len %u", len);
1578		return IEEE80211_REASON_IE_INVALID;
1579	}
1580	frm += 6, len -= 4;		/* NB: len is payload only */
1581	/* NB: iswapoui already validated the OUI and type */
1582	w = LE_READ_2(frm);
1583	if (w != WPA_VERSION) {
1584		IEEE80211_DISCARD_IE(ic,
1585		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1586		    wh, "WPA", "bad version %u", w);
1587		return IEEE80211_REASON_IE_INVALID;
1588	}
1589	frm += 2, len -= 2;
1590
1591	/* multicast/group cipher */
1592	w = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
1593	if (w != rsn->rsn_mcastcipher) {
1594		IEEE80211_DISCARD_IE(ic,
1595		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1596		    wh, "WPA", "mcast cipher mismatch; got %u, expected %u",
1597		    w, rsn->rsn_mcastcipher);
1598		return IEEE80211_REASON_IE_INVALID;
1599	}
1600	frm += 4, len -= 4;
1601
1602	/* unicast ciphers */
1603	n = LE_READ_2(frm);
1604	frm += 2, len -= 2;
1605	if (len < n*4+2) {
1606		IEEE80211_DISCARD_IE(ic,
1607		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1608		    wh, "WPA", "ucast cipher data too short; len %u, n %u",
1609		    len, n);
1610		return IEEE80211_REASON_IE_INVALID;
1611	}
1612	w = 0;
1613	for (; n > 0; n--) {
1614		w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
1615		frm += 4, len -= 4;
1616	}
1617	w &= rsn->rsn_ucastcipherset;
1618	if (w == 0) {
1619		IEEE80211_DISCARD_IE(ic,
1620		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1621		    wh, "WPA", "%s", "ucast cipher set empty");
1622		return IEEE80211_REASON_IE_INVALID;
1623	}
1624	if (w & (1<<IEEE80211_CIPHER_TKIP))
1625		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1626	else
1627		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1628
1629	/* key management algorithms */
1630	n = LE_READ_2(frm);
1631	frm += 2, len -= 2;
1632	if (len < n*4) {
1633		IEEE80211_DISCARD_IE(ic,
1634		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1635		    wh, "WPA", "key mgmt alg data too short; len %u, n %u",
1636		    len, n);
1637		return IEEE80211_REASON_IE_INVALID;
1638	}
1639	w = 0;
1640	for (; n > 0; n--) {
1641		w |= wpa_keymgmt(frm);
1642		frm += 4, len -= 4;
1643	}
1644	w &= rsn->rsn_keymgmtset;
1645	if (w == 0) {
1646		IEEE80211_DISCARD_IE(ic,
1647		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1648		    wh, "WPA", "%s", "no acceptable key mgmt alg");
1649		return IEEE80211_REASON_IE_INVALID;
1650	}
1651	if (w & WPA_ASE_8021X_UNSPEC)
1652		rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
1653	else
1654		rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1655
1656	if (len > 2)		/* optional capabilities */
1657		rsn->rsn_caps = LE_READ_2(frm);
1658
1659	return 0;
1660}
1661
1662/*
1663 * Convert an RSN cipher selector OUI to an internal
1664 * cipher algorithm.  Where appropriate we also
1665 * record any key length.
1666 */
1667static int
1668rsn_cipher(uint8_t *sel, uint8_t *keylen)
1669{
1670#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1671	uint32_t w = LE_READ_4(sel);
1672
1673	switch (w) {
1674	case RSN_SEL(RSN_CSE_NULL):
1675		return IEEE80211_CIPHER_NONE;
1676	case RSN_SEL(RSN_CSE_WEP40):
1677		if (keylen)
1678			*keylen = 40 / NBBY;
1679		return IEEE80211_CIPHER_WEP;
1680	case RSN_SEL(RSN_CSE_WEP104):
1681		if (keylen)
1682			*keylen = 104 / NBBY;
1683		return IEEE80211_CIPHER_WEP;
1684	case RSN_SEL(RSN_CSE_TKIP):
1685		return IEEE80211_CIPHER_TKIP;
1686	case RSN_SEL(RSN_CSE_CCMP):
1687		return IEEE80211_CIPHER_AES_CCM;
1688	case RSN_SEL(RSN_CSE_WRAP):
1689		return IEEE80211_CIPHER_AES_OCB;
1690	}
1691	return 32;		/* NB: so 1<< is discarded */
1692#undef WPA_SEL
1693}
1694
1695/*
1696 * Convert an RSN key management/authentication algorithm
1697 * to an internal code.
1698 */
1699static int
1700rsn_keymgmt(uint8_t *sel)
1701{
1702#define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1703	uint32_t w = LE_READ_4(sel);
1704
1705	switch (w) {
1706	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
1707		return RSN_ASE_8021X_UNSPEC;
1708	case RSN_SEL(RSN_ASE_8021X_PSK):
1709		return RSN_ASE_8021X_PSK;
1710	case RSN_SEL(RSN_ASE_NONE):
1711		return RSN_ASE_NONE;
1712	}
1713	return 0;		/* NB: so is discarded */
1714#undef RSN_SEL
1715}
1716
1717/*
1718 * Parse a WPA/RSN information element to collect parameters
1719 * and validate the parameters against what has been
1720 * configured for the system.
1721 */
1722static int
1723ieee80211_parse_rsn(struct ieee80211com *ic, uint8_t *frm,
1724	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1725{
1726	uint8_t len = frm[1];
1727	uint32_t w;
1728	int n;
1729
1730	/*
1731	 * Check the length once for fixed parts:
1732	 * version, mcast cipher, and 2 selector counts.
1733	 * Other, variable-length data, must be checked separately.
1734	 */
1735	if ((ic->ic_flags & IEEE80211_F_WPA2) == 0) {
1736		IEEE80211_DISCARD_IE(ic,
1737		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1738		    wh, "WPA", "not RSN, flags 0x%x", ic->ic_flags);
1739		return IEEE80211_REASON_IE_INVALID;
1740	}
1741	if (len < 10) {
1742		IEEE80211_DISCARD_IE(ic,
1743		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1744		    wh, "RSN", "too short, len %u", len);
1745		return IEEE80211_REASON_IE_INVALID;
1746	}
1747	frm += 2;
1748	w = LE_READ_2(frm);
1749	if (w != RSN_VERSION) {
1750		IEEE80211_DISCARD_IE(ic,
1751		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1752		    wh, "RSN", "bad version %u", w);
1753		return IEEE80211_REASON_IE_INVALID;
1754	}
1755	frm += 2, len -= 2;
1756
1757	/* multicast/group cipher */
1758	w = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
1759	if (w != rsn->rsn_mcastcipher) {
1760		IEEE80211_DISCARD_IE(ic,
1761		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1762		    wh, "RSN", "mcast cipher mismatch; got %u, expected %u",
1763		    w, rsn->rsn_mcastcipher);
1764		return IEEE80211_REASON_IE_INVALID;
1765	}
1766	frm += 4, len -= 4;
1767
1768	/* unicast ciphers */
1769	n = LE_READ_2(frm);
1770	frm += 2, len -= 2;
1771	if (len < n*4+2) {
1772		IEEE80211_DISCARD_IE(ic,
1773		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1774		    wh, "RSN", "ucast cipher data too short; len %u, n %u",
1775		    len, n);
1776		return IEEE80211_REASON_IE_INVALID;
1777	}
1778	w = 0;
1779	for (; n > 0; n--) {
1780		w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
1781		frm += 4, len -= 4;
1782	}
1783	w &= rsn->rsn_ucastcipherset;
1784	if (w == 0) {
1785		IEEE80211_DISCARD_IE(ic,
1786		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1787		    wh, "RSN", "%s", "ucast cipher set empty");
1788		return IEEE80211_REASON_IE_INVALID;
1789	}
1790	if (w & (1<<IEEE80211_CIPHER_TKIP))
1791		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1792	else
1793		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1794
1795	/* key management algorithms */
1796	n = LE_READ_2(frm);
1797	frm += 2, len -= 2;
1798	if (len < n*4) {
1799		IEEE80211_DISCARD_IE(ic,
1800		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1801		    wh, "RSN", "key mgmt alg data too short; len %u, n %u",
1802		    len, n);
1803		return IEEE80211_REASON_IE_INVALID;
1804	}
1805	w = 0;
1806	for (; n > 0; n--) {
1807		w |= rsn_keymgmt(frm);
1808		frm += 4, len -= 4;
1809	}
1810	w &= rsn->rsn_keymgmtset;
1811	if (w == 0) {
1812		IEEE80211_DISCARD_IE(ic,
1813		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1814		    wh, "RSN", "%s", "no acceptable key mgmt alg");
1815		return IEEE80211_REASON_IE_INVALID;
1816	}
1817	if (w & RSN_ASE_8021X_UNSPEC)
1818		rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
1819	else
1820		rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
1821
1822	/* optional RSN capabilities */
1823	if (len > 2)
1824		rsn->rsn_caps = LE_READ_2(frm);
1825	/* XXXPMKID */
1826
1827	return 0;
1828}
1829
1830static int
1831ieee80211_parse_wmeparams(struct ieee80211com *ic, uint8_t *frm,
1832	const struct ieee80211_frame *wh)
1833{
1834#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
1835	struct ieee80211_wme_state *wme = &ic->ic_wme;
1836	u_int len = frm[1], qosinfo;
1837	int i;
1838
1839	if (len < sizeof(struct ieee80211_wme_param)-2) {
1840		IEEE80211_DISCARD_IE(ic,
1841		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
1842		    wh, "WME", "too short, len %u", len);
1843		return -1;
1844	}
1845	qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)];
1846	qosinfo &= WME_QOSINFO_COUNT;
1847	/* XXX do proper check for wraparound */
1848	if (qosinfo == wme->wme_wmeChanParams.cap_info)
1849		return 0;
1850	frm += __offsetof(struct ieee80211_wme_param, params_acParams);
1851	for (i = 0; i < WME_NUM_AC; i++) {
1852		struct wmeParams *wmep =
1853			&wme->wme_wmeChanParams.cap_wmeParams[i];
1854		/* NB: ACI not used */
1855		wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
1856		wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
1857		wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
1858		wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
1859		wmep->wmep_txopLimit = LE_READ_2(frm+2);
1860		frm += 4;
1861	}
1862	wme->wme_wmeChanParams.cap_info = qosinfo;
1863	return 1;
1864#undef MS
1865}
1866
1867static int
1868ieee80211_parse_athparams(struct ieee80211_node *ni, uint8_t *frm,
1869	const struct ieee80211_frame *wh)
1870{
1871	struct ieee80211com *ic = ni->ni_ic;
1872	const struct ieee80211_ath_ie *ath;
1873	u_int len = frm[1];
1874	int capschanged;
1875	uint16_t defkeyix;
1876
1877	if (len < sizeof(struct ieee80211_ath_ie)-2) {
1878		IEEE80211_DISCARD_IE(ic,
1879		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_SUPERG,
1880		    wh, "Atheros", "too short, len %u", len);
1881		return -1;
1882	}
1883	ath = (const struct ieee80211_ath_ie *)frm;
1884	capschanged = (ni->ni_ath_flags != ath->ath_capability);
1885	defkeyix = LE_READ_2(ath->ath_defkeyix);
1886	if (capschanged || defkeyix != ni->ni_ath_defkeyix) {
1887		ni->ni_ath_flags = ath->ath_capability;
1888		ni->ni_ath_defkeyix = defkeyix;
1889		IEEE80211_DPRINTF(ic, IEEE80211_MSG_SUPERG,
1890		    "[%s] ath ie change: new caps 0x%x defkeyix 0x%x\n",
1891		    ether_sprintf(ni->ni_macaddr),
1892		    ni->ni_ath_flags, ni->ni_ath_defkeyix);
1893	}
1894	if (IEEE80211_ATH_CAP(ic, ni, ATHEROS_CAP_TURBO_PRIME)) {
1895		uint16_t curflags, newflags;
1896
1897		/*
1898		 * Check for turbo mode switch.  Calculate flags
1899		 * for the new mode and effect the switch.
1900		 */
1901		newflags = curflags = ic->ic_bsschan->ic_flags;
1902		/* NB: BOOST is not in ic_flags, so get it from the ie */
1903		if (ath->ath_capability & ATHEROS_CAP_BOOST)
1904			newflags |= IEEE80211_CHAN_TURBO;
1905		else
1906			newflags &= ~IEEE80211_CHAN_TURBO;
1907		if (newflags != curflags)
1908			ieee80211_dturbo_switch(ic, newflags);
1909	}
1910	return capschanged;
1911}
1912
1913void
1914ieee80211_saveath(struct ieee80211_node *ni, uint8_t *ie)
1915{
1916	const struct ieee80211_ath_ie *ath =
1917		(const struct ieee80211_ath_ie *) ie;
1918
1919	ni->ni_ath_flags = ath->ath_capability;
1920	ni->ni_ath_defkeyix = LE_READ_2(&ath->ath_defkeyix);
1921	ieee80211_saveie(&ni->ni_ath_ie, ie);
1922}
1923
1924void
1925ieee80211_saveie(uint8_t **iep, const uint8_t *ie)
1926{
1927	u_int ielen = ie[1]+2;
1928	/*
1929	 * Record information element for later use.
1930	 */
1931	if (*iep == NULL || (*iep)[1] != ie[1]) {
1932		if (*iep != NULL)
1933			FREE(*iep, M_80211_NODE);
1934		MALLOC(*iep, void*, ielen, M_80211_NODE, M_NOWAIT);
1935	}
1936	if (*iep != NULL)
1937		memcpy(*iep, ie, ielen);
1938	/* XXX note failure */
1939}
1940
1941/* XXX find a better place for definition */
1942struct l2_update_frame {
1943	struct ether_header eh;
1944	uint8_t dsap;
1945	uint8_t ssap;
1946	uint8_t control;
1947	uint8_t xid[3];
1948}  __packed;
1949
1950/*
1951 * Deliver a TGf L2UF frame on behalf of a station.
1952 * This primes any bridge when the station is roaming
1953 * between ap's on the same wired network.
1954 */
1955static void
1956ieee80211_deliver_l2uf(struct ieee80211_node *ni)
1957{
1958	struct ieee80211com *ic = ni->ni_ic;
1959	struct ifnet *ifp = ic->ic_ifp;
1960	struct mbuf *m;
1961	struct l2_update_frame *l2uf;
1962	struct ether_header *eh;
1963
1964	m = m_gethdr(M_NOWAIT, MT_DATA);
1965	if (m == NULL) {
1966		IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1967		    "%s", "no mbuf for l2uf frame");
1968		ic->ic_stats.is_rx_nobuf++;	/* XXX not right */
1969		return;
1970	}
1971	l2uf = mtod(m, struct l2_update_frame *);
1972	eh = &l2uf->eh;
1973	/* dst: Broadcast address */
1974	IEEE80211_ADDR_COPY(eh->ether_dhost, ifp->if_broadcastaddr);
1975	/* src: associated STA */
1976	IEEE80211_ADDR_COPY(eh->ether_shost, ni->ni_macaddr);
1977	eh->ether_type = htons(sizeof(*l2uf) - sizeof(*eh));
1978
1979	l2uf->dsap = 0;
1980	l2uf->ssap = 0;
1981	l2uf->control = 0xf5;
1982	l2uf->xid[0] = 0x81;
1983	l2uf->xid[1] = 0x80;
1984	l2uf->xid[2] = 0x00;
1985
1986	m->m_pkthdr.len = m->m_len = sizeof(*l2uf);
1987	ieee80211_deliver_data(ic, ni, m);
1988}
1989
1990static __inline int
1991contbgscan(struct ieee80211com *ic)
1992{
1993	return ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) &&
1994	    time_after(ticks, ic->ic_lastdata + ic->ic_bgscanidle));
1995}
1996
1997static __inline int
1998startbgscan(struct ieee80211com *ic)
1999{
2000	return ((ic->ic_flags & IEEE80211_F_BGSCAN) &&
2001	    !IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
2002	    time_after(ticks, ic->ic_lastscan + ic->ic_bgscanintvl) &&
2003	    time_after(ticks, ic->ic_lastdata + ic->ic_bgscanidle));
2004}
2005
2006static void
2007ratesetmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
2008	int reassoc, int resp, const char *tag, int rate)
2009{
2010	struct ieee80211com *ic = ni->ni_ic;
2011
2012	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
2013	    "[%s] deny %s request, %s rate set mismatch, rate 0x%x\n",
2014	    ether_sprintf(wh->i_addr2),
2015	    reassoc ? "reassoc" : "assoc", tag, rate);
2016	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_BASIC_RATE);
2017	ieee80211_node_leave(ic, ni);
2018	ic->ic_stats.is_rx_assoc_norate++;
2019}
2020
2021static void
2022capinfomismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
2023	int reassoc, int resp, const char *tag, int capinfo)
2024{
2025	struct ieee80211com *ic = ni->ni_ic;
2026
2027	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
2028	    "[%s] deny %s request, %s mismatch 0x%x\n",
2029	    ether_sprintf(wh->i_addr2),
2030	    reassoc ? "reassoc" : "assoc", tag, capinfo);
2031	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_CAPINFO);
2032	ieee80211_node_leave(ic, ni);
2033	ic->ic_stats.is_rx_assoc_capmismatch++;
2034}
2035
2036void
2037ieee80211_recv_mgmt(struct ieee80211com *ic, struct mbuf *m0,
2038	struct ieee80211_node *ni,
2039	int subtype, int rssi, int noise, uint32_t rstamp)
2040{
2041#define	ISPROBE(_st)	((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
2042#define	ISREASSOC(_st)	((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
2043	struct ieee80211_frame *wh;
2044	uint8_t *frm, *efrm;
2045	uint8_t *ssid, *rates, *xrates, *wpa, *rsn, *wme, *ath, *htcap;
2046	int reassoc, resp, allocbs;
2047	uint8_t rate;
2048
2049	wh = mtod(m0, struct ieee80211_frame *);
2050	frm = (uint8_t *)&wh[1];
2051	efrm = mtod(m0, uint8_t *) + m0->m_len;
2052	switch (subtype) {
2053	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2054	case IEEE80211_FC0_SUBTYPE_BEACON: {
2055		struct ieee80211_scanparams scan;
2056
2057		/*
2058		 * We process beacon/probe response frames:
2059		 *    o when scanning, or
2060		 *    o station mode when associated (to collect state
2061		 *      updates such as 802.11g slot time), or
2062		 *    o adhoc mode (to discover neighbors)
2063		 * Frames otherwise received are discarded.
2064		 */
2065		if (!((ic->ic_flags & IEEE80211_F_SCAN) ||
2066		      (ic->ic_opmode == IEEE80211_M_STA && ni->ni_associd) ||
2067		       ic->ic_opmode == IEEE80211_M_IBSS)) {
2068			ic->ic_stats.is_rx_mgtdiscard++;
2069			return;
2070		}
2071		/*
2072		 * beacon/probe response frame format
2073		 *	[8] time stamp
2074		 *	[2] beacon interval
2075		 *	[2] capability information
2076		 *	[tlv] ssid
2077		 *	[tlv] supported rates
2078		 *	[tlv] country information
2079		 *	[tlv] parameter set (FH/DS)
2080		 *	[tlv] erp information
2081		 *	[tlv] extended supported rates
2082		 *	[tlv] WME
2083		 *	[tlv] WPA or RSN
2084		 *	[tlv] HT capabilities
2085		 *	[tlv] HT information
2086		 *	[tlv] Atheros capabilities
2087		 */
2088		IEEE80211_VERIFY_LENGTH(efrm - frm, 12, return);
2089		memset(&scan, 0, sizeof(scan));
2090		scan.tstamp  = frm;				frm += 8;
2091		scan.bintval = le16toh(*(uint16_t *)frm);	frm += 2;
2092		scan.capinfo = le16toh(*(uint16_t *)frm);	frm += 2;
2093		scan.bchan = ieee80211_chan2ieee(ic, ic->ic_curchan);
2094		scan.chan = scan.bchan;
2095
2096		while (efrm - frm > 1) {
2097			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
2098			switch (*frm) {
2099			case IEEE80211_ELEMID_SSID:
2100				scan.ssid = frm;
2101				break;
2102			case IEEE80211_ELEMID_RATES:
2103				scan.rates = frm;
2104				break;
2105			case IEEE80211_ELEMID_COUNTRY:
2106				scan.country = frm;
2107				break;
2108			case IEEE80211_ELEMID_FHPARMS:
2109				if (ic->ic_phytype == IEEE80211_T_FH) {
2110					scan.fhdwell = LE_READ_2(&frm[2]);
2111					scan.chan = IEEE80211_FH_CHAN(frm[4], frm[5]);
2112					scan.fhindex = frm[6];
2113				}
2114				break;
2115			case IEEE80211_ELEMID_DSPARMS:
2116				/*
2117				 * XXX hack this since depending on phytype
2118				 * is problematic for multi-mode devices.
2119				 */
2120				if (ic->ic_phytype != IEEE80211_T_FH)
2121					scan.chan = frm[2];
2122				break;
2123			case IEEE80211_ELEMID_TIM:
2124				/* XXX ATIM? */
2125				scan.tim = frm;
2126				scan.timoff = frm - mtod(m0, uint8_t *);
2127				break;
2128			case IEEE80211_ELEMID_IBSSPARMS:
2129				break;
2130			case IEEE80211_ELEMID_XRATES:
2131				scan.xrates = frm;
2132				break;
2133			case IEEE80211_ELEMID_ERP:
2134				if (frm[1] != 1) {
2135					IEEE80211_DISCARD_IE(ic,
2136					    IEEE80211_MSG_ELEMID, wh, "ERP",
2137					    "bad len %u", frm[1]);
2138					ic->ic_stats.is_rx_elem_toobig++;
2139					break;
2140				}
2141				scan.erp = frm[2];
2142				break;
2143			case IEEE80211_ELEMID_HTCAP:
2144				scan.htcap = frm;
2145				break;
2146			case IEEE80211_ELEMID_RSN:
2147				scan.rsn = frm;
2148				break;
2149			case IEEE80211_ELEMID_HTINFO:
2150				scan.htinfo = frm;
2151				break;
2152			case IEEE80211_ELEMID_VENDOR:
2153				if (iswpaoui(frm))
2154					scan.wpa = frm;
2155				else if (iswmeparam(frm) || iswmeinfo(frm))
2156					scan.wme = frm;
2157				else if (isatherosoui(frm))
2158					scan.ath = frm;
2159				else if (ic->ic_flags_ext & IEEE80211_FEXT_HTCOMPAT) {
2160					/*
2161					 * Accept pre-draft HT ie's if the
2162					 * standard ones have not been seen.
2163					 */
2164					if (ishtcapoui(frm)) {
2165						if (scan.htcap == NULL)
2166							scan.htcap = frm;
2167					} else if (ishtinfooui(frm)) {
2168						if (scan.htinfo == NULL)
2169							scan.htcap = frm;
2170					}
2171				}
2172				break;
2173			default:
2174				IEEE80211_DISCARD_IE(ic, IEEE80211_MSG_ELEMID,
2175				    wh, "unhandled",
2176				    "id %u, len %u", *frm, frm[1]);
2177				ic->ic_stats.is_rx_elem_unknown++;
2178				break;
2179			}
2180			frm += frm[1] + 2;
2181		}
2182		IEEE80211_VERIFY_ELEMENT(scan.rates, IEEE80211_RATE_MAXSIZE);
2183		if (scan.xrates != NULL)
2184			IEEE80211_VERIFY_ELEMENT(scan.xrates,
2185				IEEE80211_RATE_MAXSIZE - scan.rates[1]);
2186		IEEE80211_VERIFY_ELEMENT(scan.ssid, IEEE80211_NWID_LEN);
2187#if IEEE80211_CHAN_MAX < 255
2188		if (scan.chan > IEEE80211_CHAN_MAX) {
2189			IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,
2190			    wh, ieee80211_mgt_subtype_name[subtype >>
2191				IEEE80211_FC0_SUBTYPE_SHIFT],
2192			    "invalid channel %u", scan.chan);
2193			ic->ic_stats.is_rx_badchan++;
2194			return;
2195		}
2196#endif
2197		if (scan.chan != scan.bchan &&
2198		    ic->ic_phytype != IEEE80211_T_FH) {
2199			/*
2200			 * Frame was received on a channel different from the
2201			 * one indicated in the DS params element id;
2202			 * silently discard it.
2203			 *
2204			 * NB: this can happen due to signal leakage.
2205			 *     But we should take it for FH phy because
2206			 *     the rssi value should be correct even for
2207			 *     different hop pattern in FH.
2208			 */
2209			IEEE80211_DISCARD(ic,
2210			    IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
2211			    wh, ieee80211_mgt_subtype_name[subtype >>
2212				IEEE80211_FC0_SUBTYPE_SHIFT],
2213			    "for off-channel %u", scan.chan);
2214			ic->ic_stats.is_rx_chanmismatch++;
2215			return;
2216		}
2217		if (!(IEEE80211_BINTVAL_MIN <= scan.bintval &&
2218		      scan.bintval <= IEEE80211_BINTVAL_MAX)) {
2219			IEEE80211_DISCARD(ic,
2220			    IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
2221			    wh, ieee80211_mgt_subtype_name[subtype >>
2222				IEEE80211_FC0_SUBTYPE_SHIFT],
2223			    "bogus beacon interval", scan.bintval);
2224			ic->ic_stats.is_rx_badbintval++;
2225			return;
2226		}
2227		/*
2228		 * Process HT ie's.  This is complicated by our
2229		 * accepting both the standard ie's and the pre-draft
2230		 * vendor OUI ie's that some vendors still use/require.
2231		 */
2232		if (scan.htcap != NULL) {
2233			IEEE80211_VERIFY_LENGTH(scan.htcap[1],
2234			     scan.htcap[0] == IEEE80211_ELEMID_VENDOR ?
2235			         4 + sizeof(struct ieee80211_ie_htcap)-2 :
2236			         sizeof(struct ieee80211_ie_htcap)-2,
2237			     scan.htcap = NULL);
2238		}
2239		if (scan.htinfo != NULL) {
2240			IEEE80211_VERIFY_LENGTH(scan.htinfo[1],
2241			     scan.htinfo[0] == IEEE80211_ELEMID_VENDOR ?
2242			         4 + sizeof(struct ieee80211_ie_htinfo)-2 :
2243			         sizeof(struct ieee80211_ie_htinfo)-2,
2244			     scan.htinfo = NULL);
2245		}
2246
2247		/*
2248		 * Count frame now that we know it's to be processed.
2249		 */
2250		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
2251			ic->ic_stats.is_rx_beacon++;		/* XXX remove */
2252			IEEE80211_NODE_STAT(ni, rx_beacons);
2253		} else
2254			IEEE80211_NODE_STAT(ni, rx_proberesp);
2255
2256		/*
2257		 * When operating in station mode, check for state updates.
2258		 * Be careful to ignore beacons received while doing a
2259		 * background scan.  We consider only 11g/WMM stuff right now.
2260		 */
2261		if (ic->ic_opmode == IEEE80211_M_STA &&
2262		    ni->ni_associd != 0 &&
2263		    ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
2264		     IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
2265			/* record tsf of last beacon */
2266			memcpy(ni->ni_tstamp.data, scan.tstamp,
2267				sizeof(ni->ni_tstamp));
2268			/* count beacon frame for s/w bmiss handling */
2269			ic->ic_swbmiss_count++;
2270			ic->ic_bmiss_count = 0;
2271			if (ni->ni_erp != scan.erp) {
2272				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2273				    "[%s] erp change: was 0x%x, now 0x%x\n",
2274				    ether_sprintf(wh->i_addr2),
2275				    ni->ni_erp, scan.erp);
2276				if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
2277				    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
2278					ic->ic_flags |= IEEE80211_F_USEPROT;
2279				else
2280					ic->ic_flags &= ~IEEE80211_F_USEPROT;
2281				ni->ni_erp = scan.erp;
2282				/* XXX statistic */
2283			}
2284			if ((ni->ni_capinfo ^ scan.capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) {
2285				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2286				    "[%s] capabilities change: before 0x%x,"
2287				     " now 0x%x\n",
2288				     ether_sprintf(wh->i_addr2),
2289				     ni->ni_capinfo, scan.capinfo);
2290				/*
2291				 * NB: we assume short preamble doesn't
2292				 *     change dynamically
2293				 */
2294				ieee80211_set_shortslottime(ic,
2295					IEEE80211_IS_CHAN_A(ic->ic_bsschan) ||
2296					(scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
2297				ni->ni_capinfo = (ni->ni_capinfo &~ IEEE80211_CAPINFO_SHORT_SLOTTIME)
2298					       | (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME);
2299				/* XXX statistic */
2300			}
2301			if (scan.wme != NULL &&
2302			    (ni->ni_flags & IEEE80211_NODE_QOS) &&
2303			    ieee80211_parse_wmeparams(ic, scan.wme, wh) > 0)
2304				ieee80211_wme_updateparams(ic);
2305			if (scan.ath != NULL)
2306				ieee80211_parse_athparams(ni, scan.ath, wh);
2307			if (scan.htcap != NULL)
2308				ieee80211_parse_htcap(ni, scan.htcap);
2309			if (scan.htinfo != NULL)
2310				ieee80211_parse_htinfo(ni, scan.htinfo);
2311			if (scan.tim != NULL) {
2312				struct ieee80211_tim_ie *tim =
2313				    (struct ieee80211_tim_ie *) scan.tim;
2314#if 0
2315				int aid = IEEE80211_AID(ni->ni_associd);
2316				int ix = aid / NBBY;
2317				int min = tim->tim_bitctl &~ 1;
2318				int max = tim->tim_len + min - 4;
2319				if ((tim->tim_bitctl&1) ||
2320				    (min <= ix && ix <= max &&
2321				     isset(tim->tim_bitmap - min, aid))) {
2322					/*
2323					 * XXX Do not let bg scan kick off
2324					 * we are expecting data.
2325					 */
2326					ic->ic_lastdata = ticks;
2327					ieee80211_sta_pwrsave(ic, 0);
2328				}
2329#endif
2330				ni->ni_dtim_count = tim->tim_count;
2331				ni->ni_dtim_period = tim->tim_period;
2332			}
2333			/*
2334			 * If scanning, pass the info to the scan module.
2335			 * Otherwise, check if it's the right time to do
2336			 * a background scan.  Background scanning must
2337			 * be enabled and we must not be operating in the
2338			 * turbo phase of dynamic turbo mode.  Then,
2339			 * it's been a while since the last background
2340			 * scan and if no data frames have come through
2341			 * recently, kick off a scan.  Note that this
2342			 * is the mechanism by which a background scan
2343			 * is started _and_ continued each time we
2344			 * return on-channel to receive a beacon from
2345			 * our ap.
2346			 */
2347			if (ic->ic_flags & IEEE80211_F_SCAN) {
2348				ieee80211_add_scan(ic, &scan, wh,
2349					subtype, rssi, noise, rstamp);
2350			} else if (contbgscan(ic)) {
2351				ieee80211_bg_scan(ic);
2352			} else if (startbgscan(ic)) {
2353#if 0
2354				/* wakeup if we are sleeing */
2355				ieee80211_set_pwrsave(ic, 0);
2356#endif
2357				ieee80211_bg_scan(ic);
2358			}
2359			return;
2360		}
2361		/*
2362		 * If scanning, just pass information to the scan module.
2363		 */
2364		if (ic->ic_flags & IEEE80211_F_SCAN) {
2365			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
2366				/*
2367				 * Actively scanning a channel marked passive;
2368				 * send a probe request now that we know there
2369				 * is 802.11 traffic present.
2370				 *
2371				 * XXX check if the beacon we recv'd gives
2372				 * us what we need and suppress the probe req
2373				 */
2374				ieee80211_probe_curchan(ic, 1);
2375				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
2376			}
2377			ieee80211_add_scan(ic, &scan, wh,
2378				subtype, rssi, noise, rstamp);
2379			return;
2380		}
2381		if (scan.capinfo & IEEE80211_CAPINFO_IBSS) {
2382			if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
2383				/*
2384				 * Create a new entry in the neighbor table.
2385				 */
2386				ni = ieee80211_add_neighbor(ic, wh, &scan);
2387			} else if (ni->ni_capinfo == 0) {
2388				/*
2389				 * Update faked node created on transmit.
2390				 * Note this also updates the tsf.
2391				 */
2392				ieee80211_init_neighbor(ni, wh, &scan);
2393			} else {
2394				/*
2395				 * Record tsf for potential resync.
2396				 */
2397				memcpy(ni->ni_tstamp.data, scan.tstamp,
2398					sizeof(ni->ni_tstamp));
2399			}
2400			if (ni != NULL) {
2401				ni->ni_rssi = rssi;
2402				ni->ni_noise = noise;
2403				ni->ni_rstamp = rstamp;
2404			}
2405		}
2406		break;
2407	}
2408
2409	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2410		if (ic->ic_opmode == IEEE80211_M_STA ||
2411		    ic->ic_state != IEEE80211_S_RUN) {
2412			ic->ic_stats.is_rx_mgtdiscard++;
2413			return;
2414		}
2415		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
2416			/* frame must be directed */
2417			ic->ic_stats.is_rx_mgtdiscard++;	/* XXX stat */
2418			return;
2419		}
2420
2421		/*
2422		 * prreq frame format
2423		 *	[tlv] ssid
2424		 *	[tlv] supported rates
2425		 *	[tlv] extended supported rates
2426		 *	[tlv] Atheros capabilities
2427		 */
2428		ssid = rates = xrates = ath = NULL;
2429		while (efrm - frm > 1) {
2430			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
2431			switch (*frm) {
2432			case IEEE80211_ELEMID_SSID:
2433				ssid = frm;
2434				break;
2435			case IEEE80211_ELEMID_RATES:
2436				rates = frm;
2437				break;
2438			case IEEE80211_ELEMID_XRATES:
2439				xrates = frm;
2440				break;
2441			case IEEE80211_ELEMID_VENDOR:
2442				if (isatherosoui(frm))
2443					ath = frm;
2444				break;
2445			}
2446			frm += frm[1] + 2;
2447		}
2448		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
2449		if (xrates != NULL)
2450			IEEE80211_VERIFY_ELEMENT(xrates,
2451				IEEE80211_RATE_MAXSIZE - rates[1]);
2452		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN);
2453		IEEE80211_VERIFY_SSID(ic->ic_bss, ssid);
2454		if ((ic->ic_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
2455			IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
2456			    wh, ieee80211_mgt_subtype_name[subtype >>
2457				IEEE80211_FC0_SUBTYPE_SHIFT],
2458			    "%s", "no ssid with ssid suppression enabled");
2459			ic->ic_stats.is_rx_ssidmismatch++; /*XXX*/
2460			return;
2461		}
2462
2463		allocbs = 0;
2464		if (ni == ic->ic_bss) {
2465			if (ic->ic_opmode != IEEE80211_M_IBSS) {
2466				ni = ieee80211_tmp_node(ic, wh->i_addr2);
2467				allocbs = 1;
2468			} else if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
2469				/*
2470				 * XXX Cannot tell if the sender is operating
2471				 * in ibss mode.  But we need a new node to
2472				 * send the response so blindly add them to the
2473				 * neighbor table.
2474				 */
2475				ni = ieee80211_fakeup_adhoc_node(&ic->ic_sta,
2476					wh->i_addr2);
2477			}
2478			if (ni == NULL)
2479				return;
2480		}
2481		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2482		    "[%s] recv probe req\n", ether_sprintf(wh->i_addr2));
2483		ni->ni_rssi = rssi;
2484		ni->ni_rstamp = rstamp;
2485		rate = ieee80211_setup_rates(ni, rates, xrates,
2486			  IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE
2487			| IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2488		if (rate & IEEE80211_RATE_BASIC) {
2489			IEEE80211_DISCARD(ic, IEEE80211_MSG_XRATE,
2490			    wh, ieee80211_mgt_subtype_name[subtype >>
2491				IEEE80211_FC0_SUBTYPE_SHIFT],
2492			    "%s", "recv'd rate set invalid");
2493		} else {
2494			IEEE80211_SEND_MGMT(ic, ni,
2495				IEEE80211_FC0_SUBTYPE_PROBE_RESP, 0);
2496		}
2497		if (allocbs) {
2498			/*
2499			 * Temporary node created just to send a
2500			 * response, reclaim immediately.
2501			 */
2502			ieee80211_free_node(ni);
2503		} else if (ath != NULL)
2504			ieee80211_saveath(ni, ath);
2505		break;
2506
2507	case IEEE80211_FC0_SUBTYPE_AUTH: {
2508		uint16_t algo, seq, status;
2509		/*
2510		 * auth frame format
2511		 *	[2] algorithm
2512		 *	[2] sequence
2513		 *	[2] status
2514		 *	[tlv*] challenge
2515		 */
2516		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
2517		algo   = le16toh(*(uint16_t *)frm);
2518		seq    = le16toh(*(uint16_t *)(frm + 2));
2519		status = le16toh(*(uint16_t *)(frm + 4));
2520		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
2521		    "[%s] recv auth frame with algorithm %d seq %d\n",
2522		    ether_sprintf(wh->i_addr2), algo, seq);
2523		/*
2524		 * Consult the ACL policy module if setup.
2525		 */
2526		if (ic->ic_acl != NULL &&
2527		    !ic->ic_acl->iac_check(ic, wh->i_addr2)) {
2528			IEEE80211_DISCARD(ic, IEEE80211_MSG_ACL,
2529			    wh, "auth", "%s", "disallowed by ACL");
2530			ic->ic_stats.is_rx_acl++;
2531			if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2532				IEEE80211_SEND_MGMT(ic, ni,
2533				    IEEE80211_FC0_SUBTYPE_AUTH,
2534				    (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
2535			}
2536			return;
2537		}
2538		if (ic->ic_flags & IEEE80211_F_COUNTERM) {
2539			IEEE80211_DISCARD(ic,
2540			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
2541			    wh, "auth", "%s", "TKIP countermeasures enabled");
2542			ic->ic_stats.is_rx_auth_countermeasures++;
2543			if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2544				IEEE80211_SEND_MGMT(ic, ni,
2545					IEEE80211_FC0_SUBTYPE_AUTH,
2546					IEEE80211_REASON_MIC_FAILURE);
2547			}
2548			return;
2549		}
2550		if (algo == IEEE80211_AUTH_ALG_SHARED)
2551			ieee80211_auth_shared(ic, wh, frm + 6, efrm, ni, rssi,
2552			    noise, rstamp, seq, status);
2553		else if (algo == IEEE80211_AUTH_ALG_OPEN)
2554			ieee80211_auth_open(ic, wh, ni, rssi, noise, rstamp,
2555			    seq, status);
2556		else {
2557			IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
2558			    wh, "auth", "unsupported alg %d", algo);
2559			ic->ic_stats.is_rx_auth_unsupported++;
2560			if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2561				/* XXX not right */
2562				IEEE80211_SEND_MGMT(ic, ni,
2563					IEEE80211_FC0_SUBTYPE_AUTH,
2564					(seq+1) | (IEEE80211_STATUS_ALG<<16));
2565			}
2566			return;
2567		}
2568		break;
2569	}
2570
2571	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2572	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
2573		uint16_t capinfo, lintval;
2574		struct ieee80211_rsnparms rsnparms;
2575		uint8_t reason;
2576		int badwparsn;
2577
2578		if (ic->ic_opmode != IEEE80211_M_HOSTAP ||
2579		    ic->ic_state != IEEE80211_S_RUN) {
2580			ic->ic_stats.is_rx_mgtdiscard++;
2581			return;
2582		}
2583
2584		if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2585			reassoc = 1;
2586			resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
2587		} else {
2588			reassoc = 0;
2589			resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
2590		}
2591		/*
2592		 * asreq frame format
2593		 *	[2] capability information
2594		 *	[2] listen interval
2595		 *	[6*] current AP address (reassoc only)
2596		 *	[tlv] ssid
2597		 *	[tlv] supported rates
2598		 *	[tlv] extended supported rates
2599		 *	[tlv] WPA or RSN
2600		 *	[tlv] HT capabilities
2601		 *	[tlv] Atheros capabilities
2602		 */
2603		IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4), return);
2604		if (!IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_bss->ni_bssid)) {
2605			IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
2606			    wh, ieee80211_mgt_subtype_name[subtype >>
2607				IEEE80211_FC0_SUBTYPE_SHIFT],
2608			    "%s", "wrong bssid");
2609			ic->ic_stats.is_rx_assoc_bss++;
2610			return;
2611		}
2612		capinfo = le16toh(*(uint16_t *)frm);	frm += 2;
2613		lintval = le16toh(*(uint16_t *)frm);	frm += 2;
2614		if (reassoc)
2615			frm += 6;	/* ignore current AP info */
2616		ssid = rates = xrates = wpa = rsn = wme = ath = htcap = NULL;
2617		while (efrm - frm > 1) {
2618			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
2619			switch (*frm) {
2620			case IEEE80211_ELEMID_SSID:
2621				ssid = frm;
2622				break;
2623			case IEEE80211_ELEMID_RATES:
2624				rates = frm;
2625				break;
2626			case IEEE80211_ELEMID_XRATES:
2627				xrates = frm;
2628				break;
2629			/* XXX verify only one of RSN and WPA ie's? */
2630			case IEEE80211_ELEMID_RSN:
2631				rsn = frm;
2632				break;
2633			case IEEE80211_ELEMID_HTCAP:
2634				htcap = frm;
2635				break;
2636			case IEEE80211_ELEMID_VENDOR:
2637				if (iswpaoui(frm))
2638					wpa = frm;
2639				else if (iswmeinfo(frm))
2640					wme = frm;
2641				else if (isatherosoui(frm))
2642					ath = frm;
2643				else if (ic->ic_flags_ext & IEEE80211_FEXT_HTCOMPAT) {
2644					if (ishtcapoui(frm) && htcap == NULL)
2645						htcap = frm;
2646				}
2647				break;
2648			}
2649			frm += frm[1] + 2;
2650		}
2651		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
2652		if (xrates != NULL)
2653			IEEE80211_VERIFY_ELEMENT(xrates,
2654				IEEE80211_RATE_MAXSIZE - rates[1]);
2655		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN);
2656		IEEE80211_VERIFY_SSID(ic->ic_bss, ssid);
2657		if (htcap != NULL) {
2658			IEEE80211_VERIFY_LENGTH(htcap[1],
2659			     htcap[0] == IEEE80211_ELEMID_VENDOR ?
2660			         4 + sizeof(struct ieee80211_ie_htcap)-2 :
2661			         sizeof(struct ieee80211_ie_htcap)-2,
2662			     return);		/* XXX just NULL out? */
2663		}
2664
2665		if (ni == ic->ic_bss) {
2666			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
2667			    "[%s] deny %s request, sta not authenticated\n",
2668			    ether_sprintf(wh->i_addr2),
2669			    reassoc ? "reassoc" : "assoc");
2670			ieee80211_send_error(ic, ni, wh->i_addr2,
2671			    IEEE80211_FC0_SUBTYPE_DEAUTH,
2672			    IEEE80211_REASON_ASSOC_NOT_AUTHED);
2673			ic->ic_stats.is_rx_assoc_notauth++;
2674			return;
2675		}
2676		/* assert right association security credentials */
2677		badwparsn = 0;
2678		switch (ic->ic_flags & IEEE80211_F_WPA) {
2679		case IEEE80211_F_WPA1:
2680			if (wpa == NULL)
2681				badwparsn = 1;
2682			break;
2683		case IEEE80211_F_WPA2:
2684			if (rsn == NULL)
2685				badwparsn = 1;
2686			break;
2687		case IEEE80211_F_WPA1|IEEE80211_F_WPA2:
2688			if (wpa == NULL && rsn == NULL)
2689				badwparsn = 1;
2690			break;
2691		}
2692		if (badwparsn) {
2693			IEEE80211_DPRINTF(ic,
2694			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
2695			    "[%s] no WPA/RSN IE in association request\n",
2696			    ether_sprintf(wh->i_addr2));
2697			IEEE80211_SEND_MGMT(ic, ni,
2698			    IEEE80211_FC0_SUBTYPE_DEAUTH,
2699			    IEEE80211_REASON_RSN_REQUIRED);
2700			ieee80211_node_leave(ic, ni);
2701			ic->ic_stats.is_rx_assoc_badwpaie++;
2702			return;
2703		}
2704		if (wpa != NULL || rsn != NULL) {
2705			/*
2706			 * Parse WPA/RSN information element.  Note that
2707			 * we initialize the param block from the node
2708			 * state so that information in the IE overrides
2709			 * our defaults.  The resulting parameters are
2710			 * installed below after the association is assured.
2711			 */
2712			rsnparms = ni->ni_rsn;
2713			if (wpa != NULL)
2714				reason = ieee80211_parse_wpa(ic, wpa, &rsnparms, wh);
2715			else
2716				reason = ieee80211_parse_rsn(ic, rsn, &rsnparms, wh);
2717			if (reason != 0) {
2718				IEEE80211_SEND_MGMT(ic, ni,
2719				    IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
2720				ieee80211_node_leave(ic, ni);
2721				/* XXX distinguish WPA/RSN? */
2722				ic->ic_stats.is_rx_assoc_badwpaie++;
2723				return;
2724			}
2725			IEEE80211_DPRINTF(ic,
2726			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
2727			    "[%s] %s ie: mc %u/%u uc %u/%u key %u caps 0x%x\n",
2728			    ether_sprintf(wh->i_addr2),
2729			    wpa != NULL ? "WPA" : "RSN",
2730			    rsnparms.rsn_mcastcipher, rsnparms.rsn_mcastkeylen,
2731			    rsnparms.rsn_ucastcipher, rsnparms.rsn_ucastkeylen,
2732			    rsnparms.rsn_keymgmt, rsnparms.rsn_caps);
2733		}
2734		/* discard challenge after association */
2735		if (ni->ni_challenge != NULL) {
2736			FREE(ni->ni_challenge, M_80211_NODE);
2737			ni->ni_challenge = NULL;
2738		}
2739		/* NB: 802.11 spec says to ignore station's privacy bit */
2740		if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
2741			capinfomismatch(ni, wh, reassoc, resp,
2742			    "capability", capinfo);
2743			return;
2744		}
2745		/*
2746		 * Disallow re-associate w/ invalid slot time setting.
2747		 */
2748		if (ni->ni_associd != 0 &&
2749		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2750		    ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
2751			capinfomismatch(ni, wh, reassoc, resp,
2752			    "slot time", capinfo);
2753			return;
2754		}
2755		rate = ieee80211_setup_rates(ni, rates, xrates,
2756				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
2757				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2758		if (rate & IEEE80211_RATE_BASIC) {
2759			ratesetmismatch(ni, wh, reassoc, resp, "basic", rate);
2760			return;
2761		}
2762		/*
2763		 * If constrained to 11g-only stations reject an
2764		 * 11b-only station.  We cheat a bit here by looking
2765		 * at the max negotiated xmit rate and assuming anyone
2766		 * with a best rate <24Mb/s is an 11b station.
2767		 */
2768		if ((ic->ic_flags & IEEE80211_F_PUREG) && rate < 48) {
2769			ratesetmismatch(ni, wh, reassoc, resp, "11g", rate);
2770			return;
2771		}
2772		/* XXX enforce PUREN */
2773		/* 802.11n-specific rateset handling */
2774		if (IEEE80211_IS_CHAN_HT(ic->ic_curchan) && htcap != NULL) {
2775			rate = ieee80211_setup_htrates(ni, htcap,
2776				IEEE80211_F_DOFRATE | IEEE80211_F_DONEGO |
2777				IEEE80211_F_DOBRS);
2778			if (rate & IEEE80211_RATE_BASIC) {
2779				/* XXX 11n-specific stat */
2780				ratesetmismatch(ni, wh, reassoc, resp,
2781				    "HT", rate);
2782				return;
2783			}
2784			ieee80211_ht_node_init(ni, htcap);
2785		} else if (ni->ni_flags & IEEE80211_NODE_HT)
2786			ieee80211_ht_node_cleanup(ni);
2787		ni->ni_rssi = rssi;
2788		ni->ni_noise = noise;
2789		ni->ni_rstamp = rstamp;
2790		ni->ni_intval = lintval;
2791		ni->ni_capinfo = capinfo;
2792		ni->ni_chan = ic->ic_bsschan;
2793		ni->ni_fhdwell = ic->ic_bss->ni_fhdwell;
2794		ni->ni_fhindex = ic->ic_bss->ni_fhindex;
2795		if (wpa != NULL) {
2796			/*
2797			 * Record WPA parameters for station, mark
2798			 * node as using WPA and record information element
2799			 * for applications that require it.
2800			 */
2801			ni->ni_rsn = rsnparms;
2802			ieee80211_saveie(&ni->ni_wpa_ie, wpa);
2803		} else if (ni->ni_wpa_ie != NULL) {
2804			/*
2805			 * Flush any state from a previous association.
2806			 */
2807			FREE(ni->ni_wpa_ie, M_80211_NODE);
2808			ni->ni_wpa_ie = NULL;
2809		}
2810		if (rsn != NULL) {
2811			/*
2812			 * Record RSN parameters for station, mark
2813			 * node as using WPA and record information element
2814			 * for applications that require it.
2815			 */
2816			ni->ni_rsn = rsnparms;
2817			ieee80211_saveie(&ni->ni_rsn_ie, rsn);
2818		} else if (ni->ni_rsn_ie != NULL) {
2819			/*
2820			 * Flush any state from a previous association.
2821			 */
2822			FREE(ni->ni_rsn_ie, M_80211_NODE);
2823			ni->ni_rsn_ie = NULL;
2824		}
2825		if (wme != NULL) {
2826			/*
2827			 * Record WME parameters for station, mark node
2828			 * as capable of QoS and record information
2829			 * element for applications that require it.
2830			 */
2831			ieee80211_saveie(&ni->ni_wme_ie, wme);
2832			ni->ni_flags |= IEEE80211_NODE_QOS;
2833		} else if (ni->ni_wme_ie != NULL) {
2834			/*
2835			 * Flush any state from a previous association.
2836			 */
2837			FREE(ni->ni_wme_ie, M_80211_NODE);
2838			ni->ni_wme_ie = NULL;
2839			ni->ni_flags &= ~IEEE80211_NODE_QOS;
2840		}
2841		if (ath != NULL) {
2842			/*
2843			 * Record ATH parameters for station, mark
2844			 * node with appropriate capabilities, and
2845			 * record the information element for
2846			 * applications that require it.
2847			 */
2848			ieee80211_saveath(ni, ath);
2849		} else if (ni->ni_ath_ie != NULL) {
2850			/*
2851			 * Flush any state from a previous association.
2852			 */
2853			FREE(ni->ni_ath_ie, M_80211_NODE);
2854			ni->ni_ath_ie = NULL;
2855			ni->ni_ath_flags = 0;
2856		}
2857		ieee80211_node_join(ic, ni, resp);
2858		ieee80211_deliver_l2uf(ni);
2859		break;
2860	}
2861
2862	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2863	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
2864		uint16_t capinfo, associd;
2865		uint16_t status;
2866
2867		if (ic->ic_opmode != IEEE80211_M_STA ||
2868		    ic->ic_state != IEEE80211_S_ASSOC) {
2869			ic->ic_stats.is_rx_mgtdiscard++;
2870			return;
2871		}
2872
2873		/*
2874		 * asresp frame format
2875		 *	[2] capability information
2876		 *	[2] status
2877		 *	[2] association ID
2878		 *	[tlv] supported rates
2879		 *	[tlv] extended supported rates
2880		 *	[tlv] WME
2881		 *	[tlv] HT capabilities
2882		 */
2883		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
2884		ni = ic->ic_bss;
2885		capinfo = le16toh(*(uint16_t *)frm);
2886		frm += 2;
2887		status = le16toh(*(uint16_t *)frm);
2888		frm += 2;
2889		if (status != 0) {
2890			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2891			    "[%s] %sassoc failed (reason %d)\n",
2892			    ether_sprintf(wh->i_addr2),
2893			    ISREASSOC(subtype) ?  "re" : "", status);
2894			if (ni != ic->ic_bss)	/* XXX never true? */
2895				ni->ni_fails++;
2896			ic->ic_stats.is_rx_auth_fail++;	/* XXX */
2897			return;
2898		}
2899		associd = le16toh(*(uint16_t *)frm);
2900		frm += 2;
2901
2902		rates = xrates = wme = htcap = NULL;
2903		while (efrm - frm > 1) {
2904			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
2905			switch (*frm) {
2906			case IEEE80211_ELEMID_RATES:
2907				rates = frm;
2908				break;
2909			case IEEE80211_ELEMID_XRATES:
2910				xrates = frm;
2911				break;
2912			case IEEE80211_ELEMID_HTCAP:
2913				htcap = frm;
2914				break;
2915			case IEEE80211_ELEMID_VENDOR:
2916				if (iswmeoui(frm))
2917					wme = frm;
2918				/* XXX Atheros OUI support */
2919				break;
2920			}
2921			frm += frm[1] + 2;
2922		}
2923
2924		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
2925		if (xrates != NULL)
2926			IEEE80211_VERIFY_ELEMENT(xrates,
2927				IEEE80211_RATE_MAXSIZE - rates[1]);
2928		rate = ieee80211_setup_rates(ni, rates, xrates,
2929				IEEE80211_F_JOIN |
2930				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
2931				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2932		if (rate & IEEE80211_RATE_BASIC) {
2933			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2934			    "[%s] %sassoc failed (rate set mismatch)\n",
2935			    ether_sprintf(wh->i_addr2),
2936			    ISREASSOC(subtype) ?  "re" : "");
2937			if (ni != ic->ic_bss)	/* XXX never true? */
2938				ni->ni_fails++;
2939			ic->ic_stats.is_rx_assoc_norate++;
2940			ieee80211_new_state(ic, IEEE80211_S_SCAN,
2941			    IEEE80211_SCAN_FAIL_STATUS);
2942			return;
2943		}
2944
2945		ni->ni_capinfo = capinfo;
2946		ni->ni_associd = associd;
2947		if (wme != NULL &&
2948		    ieee80211_parse_wmeparams(ic, wme, wh) >= 0) {
2949			ni->ni_flags |= IEEE80211_NODE_QOS;
2950			ieee80211_wme_updateparams(ic);
2951		} else
2952			ni->ni_flags &= ~IEEE80211_NODE_QOS;
2953		/*
2954		 * Configure state now that we are associated.
2955		 *
2956		 * XXX may need different/additional driver callbacks?
2957		 */
2958		if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
2959		    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
2960			ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2961			ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2962		} else {
2963			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2964			ic->ic_flags |= IEEE80211_F_USEBARKER;
2965		}
2966		ieee80211_set_shortslottime(ic,
2967			IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
2968			(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
2969		/*
2970		 * Honor ERP protection.
2971		 *
2972		 * NB: ni_erp should zero for non-11g operation.
2973		 */
2974		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
2975		    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
2976			ic->ic_flags |= IEEE80211_F_USEPROT;
2977		else
2978			ic->ic_flags &= ~IEEE80211_F_USEPROT;
2979		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2980		    "[%s] %sassoc success: %s preamble, %s slot time%s%s%s%s\n",
2981		    ether_sprintf(wh->i_addr2),
2982		    ISREASSOC(subtype) ? "re" : "",
2983		    ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
2984		    ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
2985		    ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
2986		    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
2987		    IEEE80211_ATH_CAP(ic, ni, IEEE80211_NODE_FF) ?
2988			", fast-frames" : "",
2989		    IEEE80211_ATH_CAP(ic, ni, IEEE80211_NODE_TURBOP) ?
2990			", turbo" : ""
2991		);
2992		ieee80211_new_state(ic, IEEE80211_S_RUN, subtype);
2993		break;
2994	}
2995
2996	case IEEE80211_FC0_SUBTYPE_DEAUTH: {
2997		uint16_t reason;
2998
2999		if (ic->ic_state == IEEE80211_S_SCAN) {
3000			ic->ic_stats.is_rx_mgtdiscard++;
3001			return;
3002		}
3003		/*
3004		 * deauth frame format
3005		 *	[2] reason
3006		 */
3007		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
3008		reason = le16toh(*(uint16_t *)frm);
3009		ic->ic_stats.is_rx_deauth++;
3010		IEEE80211_NODE_STAT(ni, rx_deauth);
3011
3012		if (!IEEE80211_ADDR_EQ(wh->i_addr1, ic->ic_myaddr)) {
3013			/* NB: can happen when in promiscuous mode */
3014			ic->ic_stats.is_rx_mgtdiscard++;
3015			break;
3016		}
3017		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
3018		    "[%s] recv deauthenticate (reason %d)\n",
3019		    ether_sprintf(ni->ni_macaddr), reason);
3020		switch (ic->ic_opmode) {
3021		case IEEE80211_M_STA:
3022			ieee80211_new_state(ic, IEEE80211_S_AUTH,
3023			    (reason << 8) | IEEE80211_FC0_SUBTYPE_DEAUTH);
3024			break;
3025		case IEEE80211_M_HOSTAP:
3026			if (ni != ic->ic_bss)
3027				ieee80211_node_leave(ic, ni);
3028			break;
3029		default:
3030			ic->ic_stats.is_rx_mgtdiscard++;
3031			break;
3032		}
3033		break;
3034	}
3035
3036	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
3037		uint16_t reason;
3038
3039		if (ic->ic_state != IEEE80211_S_RUN &&
3040		    ic->ic_state != IEEE80211_S_ASSOC &&
3041		    ic->ic_state != IEEE80211_S_AUTH) {
3042			ic->ic_stats.is_rx_mgtdiscard++;
3043			return;
3044		}
3045		/*
3046		 * disassoc frame format
3047		 *	[2] reason
3048		 */
3049		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
3050		reason = le16toh(*(uint16_t *)frm);
3051		ic->ic_stats.is_rx_disassoc++;
3052		IEEE80211_NODE_STAT(ni, rx_disassoc);
3053
3054		if (!IEEE80211_ADDR_EQ(wh->i_addr1, ic->ic_myaddr)) {
3055			/* NB: can happen when in promiscuous mode */
3056			ic->ic_stats.is_rx_mgtdiscard++;
3057			break;
3058		}
3059		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
3060		    "[%s] recv disassociate (reason %d)\n",
3061		    ether_sprintf(ni->ni_macaddr), reason);
3062		switch (ic->ic_opmode) {
3063		case IEEE80211_M_STA:
3064			ieee80211_new_state(ic, IEEE80211_S_ASSOC, 0);
3065			break;
3066		case IEEE80211_M_HOSTAP:
3067			if (ni != ic->ic_bss)
3068				ieee80211_node_leave(ic, ni);
3069			break;
3070		default:
3071			ic->ic_stats.is_rx_mgtdiscard++;
3072			break;
3073		}
3074		break;
3075	}
3076
3077	case IEEE80211_FC0_SUBTYPE_ACTION: {
3078		const struct ieee80211_action *ia;
3079
3080		if (ic->ic_state != IEEE80211_S_RUN &&
3081		    ic->ic_state != IEEE80211_S_ASSOC &&
3082		    ic->ic_state != IEEE80211_S_AUTH) {
3083			ic->ic_stats.is_rx_mgtdiscard++;
3084			return;
3085		}
3086		/*
3087		 * action frame format:
3088		 *	[1] category
3089		 *	[1] action
3090		 *	[tlv] parameters
3091		 */
3092		IEEE80211_VERIFY_LENGTH(efrm - frm,
3093			sizeof(struct ieee80211_action), return);
3094		ia = (const struct ieee80211_action *) frm;
3095
3096		ic->ic_stats.is_rx_action++;
3097		IEEE80211_NODE_STAT(ni, rx_action);
3098
3099		/* verify frame payloads but defer processing */
3100		/* XXX maybe push this to method */
3101		switch (ia->ia_category) {
3102		case IEEE80211_ACTION_CAT_BA:
3103			switch (ia->ia_action) {
3104			case IEEE80211_ACTION_BA_ADDBA_REQUEST:
3105				IEEE80211_VERIFY_LENGTH(efrm - frm,
3106				    sizeof(struct ieee80211_action_ba_addbarequest),
3107				    return);
3108				break;
3109			case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
3110				IEEE80211_VERIFY_LENGTH(efrm - frm,
3111				    sizeof(struct ieee80211_action_ba_addbaresponse),
3112				    return);
3113				break;
3114			case IEEE80211_ACTION_BA_DELBA:
3115				IEEE80211_VERIFY_LENGTH(efrm - frm,
3116				    sizeof(struct ieee80211_action_ba_delba),
3117				    return);
3118				break;
3119			}
3120			break;
3121		case IEEE80211_ACTION_CAT_HT:
3122			switch (ia->ia_action) {
3123			case IEEE80211_ACTION_HT_TXCHWIDTH:
3124				IEEE80211_VERIFY_LENGTH(efrm - frm,
3125				    sizeof(struct ieee80211_action_ht_txchwidth),
3126				    return);
3127				break;
3128			}
3129			break;
3130		}
3131		ic->ic_recv_action(ni, frm, efrm);
3132		break;
3133	}
3134
3135	default:
3136		IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
3137		     wh, "mgt", "subtype 0x%x not handled", subtype);
3138		ic->ic_stats.is_rx_badsubtype++;
3139		break;
3140	}
3141#undef ISREASSOC
3142#undef ISPROBE
3143}
3144#undef IEEE80211_VERIFY_LENGTH
3145#undef IEEE80211_VERIFY_ELEMENT
3146
3147/*
3148 * Process a received ps-poll frame.
3149 */
3150static void
3151ieee80211_recv_pspoll(struct ieee80211com *ic,
3152	struct ieee80211_node *ni, struct mbuf *m0)
3153{
3154	struct ieee80211_frame_min *wh;
3155	struct mbuf *m;
3156	uint16_t aid;
3157	int qlen;
3158
3159	wh = mtod(m0, struct ieee80211_frame_min *);
3160	if (ni->ni_associd == 0) {
3161		IEEE80211_DISCARD(ic, IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
3162		    (struct ieee80211_frame *) wh, "ps-poll",
3163		    "%s", "unassociated station");
3164		ic->ic_stats.is_ps_unassoc++;
3165		IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
3166			IEEE80211_REASON_NOT_ASSOCED);
3167		return;
3168	}
3169
3170	aid = le16toh(*(uint16_t *)wh->i_dur);
3171	if (aid != ni->ni_associd) {
3172		IEEE80211_DISCARD(ic, IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
3173		    (struct ieee80211_frame *) wh, "ps-poll",
3174		    "aid mismatch: sta aid 0x%x poll aid 0x%x",
3175		    ni->ni_associd, aid);
3176		ic->ic_stats.is_ps_badaid++;
3177		IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
3178			IEEE80211_REASON_NOT_ASSOCED);
3179		return;
3180	}
3181
3182	/* Okay, take the first queued packet and put it out... */
3183	IEEE80211_NODE_SAVEQ_DEQUEUE(ni, m, qlen);
3184	if (m == NULL) {
3185		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
3186		    "[%s] recv ps-poll, but queue empty\n",
3187		    ether_sprintf(wh->i_addr2));
3188		ieee80211_send_nulldata(ieee80211_ref_node(ni));
3189		ic->ic_stats.is_ps_qempty++;	/* XXX node stat */
3190		if (ic->ic_set_tim != NULL)
3191			ic->ic_set_tim(ni, 0);	/* just in case */
3192		return;
3193	}
3194	/*
3195	 * If there are more packets, set the more packets bit
3196	 * in the packet dispatched to the station; otherwise
3197	 * turn off the TIM bit.
3198	 */
3199	if (qlen != 0) {
3200		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
3201		    "[%s] recv ps-poll, send packet, %u still queued\n",
3202		    ether_sprintf(ni->ni_macaddr), qlen);
3203		m->m_flags |= M_MORE_DATA;
3204	} else {
3205		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
3206		    "[%s] recv ps-poll, send packet, queue empty\n",
3207		    ether_sprintf(ni->ni_macaddr));
3208		if (ic->ic_set_tim != NULL)
3209			ic->ic_set_tim(ni, 0);
3210	}
3211	m->m_flags |= M_PWR_SAV;		/* bypass PS handling */
3212	IF_ENQUEUE(&ic->ic_ifp->if_snd, m);
3213}
3214
3215#ifdef IEEE80211_DEBUG
3216/*
3217 * Debugging support.
3218 */
3219
3220/*
3221 * Return the bssid of a frame.
3222 */
3223static const uint8_t *
3224ieee80211_getbssid(struct ieee80211com *ic, const struct ieee80211_frame *wh)
3225{
3226	if (ic->ic_opmode == IEEE80211_M_STA)
3227		return wh->i_addr2;
3228	if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) != IEEE80211_FC1_DIR_NODS)
3229		return wh->i_addr1;
3230	if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
3231		return wh->i_addr1;
3232	return wh->i_addr3;
3233}
3234
3235void
3236ieee80211_note(struct ieee80211com *ic, const char *fmt, ...)
3237{
3238	char buf[128];		/* XXX */
3239	va_list ap;
3240
3241	va_start(ap, fmt);
3242	vsnprintf(buf, sizeof(buf), fmt, ap);
3243	va_end(ap);
3244
3245	if_printf(ic->ic_ifp, "%s", buf);	/* NB: no \n */
3246}
3247
3248void
3249ieee80211_note_frame(struct ieee80211com *ic,
3250	const struct ieee80211_frame *wh,
3251	const char *fmt, ...)
3252{
3253	char buf[128];		/* XXX */
3254	va_list ap;
3255
3256	va_start(ap, fmt);
3257	vsnprintf(buf, sizeof(buf), fmt, ap);
3258	va_end(ap);
3259	if_printf(ic->ic_ifp, "[%s] %s\n",
3260		ether_sprintf(ieee80211_getbssid(ic, wh)), buf);
3261}
3262
3263void
3264ieee80211_note_mac(struct ieee80211com *ic,
3265	const uint8_t mac[IEEE80211_ADDR_LEN],
3266	const char *fmt, ...)
3267{
3268	char buf[128];		/* XXX */
3269	va_list ap;
3270
3271	va_start(ap, fmt);
3272	vsnprintf(buf, sizeof(buf), fmt, ap);
3273	va_end(ap);
3274	if_printf(ic->ic_ifp, "[%s] %s\n", ether_sprintf(mac), buf);
3275}
3276
3277void
3278ieee80211_discard_frame(struct ieee80211com *ic,
3279	const struct ieee80211_frame *wh,
3280	const char *type, const char *fmt, ...)
3281{
3282	va_list ap;
3283
3284	printf("[%s:%s] discard ", ic->ic_ifp->if_xname,
3285		ether_sprintf(ieee80211_getbssid(ic, wh)));
3286	if (type != NULL)
3287		printf("%s frame, ", type);
3288	else
3289		printf("frame, ");
3290	va_start(ap, fmt);
3291	vprintf(fmt, ap);
3292	va_end(ap);
3293	printf("\n");
3294}
3295
3296void
3297ieee80211_discard_ie(struct ieee80211com *ic,
3298	const struct ieee80211_frame *wh,
3299	const char *type, const char *fmt, ...)
3300{
3301	va_list ap;
3302
3303	printf("[%s:%s] discard ", ic->ic_ifp->if_xname,
3304		ether_sprintf(ieee80211_getbssid(ic, wh)));
3305	if (type != NULL)
3306		printf("%s information element, ", type);
3307	else
3308		printf("information element, ");
3309	va_start(ap, fmt);
3310	vprintf(fmt, ap);
3311	va_end(ap);
3312	printf("\n");
3313}
3314
3315void
3316ieee80211_discard_mac(struct ieee80211com *ic,
3317	const uint8_t mac[IEEE80211_ADDR_LEN],
3318	const char *type, const char *fmt, ...)
3319{
3320	va_list ap;
3321
3322	printf("[%s:%s] discard ", ic->ic_ifp->if_xname, ether_sprintf(mac));
3323	if (type != NULL)
3324		printf("%s frame, ", type);
3325	else
3326		printf("frame, ");
3327	va_start(ap, fmt);
3328	vprintf(fmt, ap);
3329	va_end(ap);
3330	printf("\n");
3331}
3332#endif /* IEEE80211_DEBUG */
3333