xref: /freebsd/sys/net80211/ieee80211_hostap.c (revision 80c7cc1c8f027fcf5d5f0a2df4b9aef6904ed079)
1 /*-
2  * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 #ifdef __FreeBSD__
28 __FBSDID("$FreeBSD$");
29 #endif
30 
31 /*
32  * IEEE 802.11 HOSTAP mode support.
33  */
34 #include "opt_inet.h"
35 #include "opt_wlan.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mbuf.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
42 
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/endian.h>
46 #include <sys/errno.h>
47 #include <sys/proc.h>
48 #include <sys/sysctl.h>
49 
50 #include <net/if.h>
51 #include <net/if_var.h>
52 #include <net/if_media.h>
53 #include <net/if_llc.h>
54 #include <net/ethernet.h>
55 
56 #include <net/bpf.h>
57 
58 #include <net80211/ieee80211_var.h>
59 #include <net80211/ieee80211_hostap.h>
60 #include <net80211/ieee80211_input.h>
61 #ifdef IEEE80211_SUPPORT_SUPERG
62 #include <net80211/ieee80211_superg.h>
63 #endif
64 #include <net80211/ieee80211_wds.h>
65 
66 #define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
67 
68 static	void hostap_vattach(struct ieee80211vap *);
69 static	int hostap_newstate(struct ieee80211vap *, enum ieee80211_state, int);
70 static	int hostap_input(struct ieee80211_node *ni, struct mbuf *m,
71 	    const struct ieee80211_rx_stats *,
72 	    int rssi, int nf);
73 static void hostap_deliver_data(struct ieee80211vap *,
74 	    struct ieee80211_node *, struct mbuf *);
75 static void hostap_recv_mgmt(struct ieee80211_node *, struct mbuf *,
76 	    int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf);
77 static void hostap_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
78 
79 void
80 ieee80211_hostap_attach(struct ieee80211com *ic)
81 {
82 	ic->ic_vattach[IEEE80211_M_HOSTAP] = hostap_vattach;
83 }
84 
85 void
86 ieee80211_hostap_detach(struct ieee80211com *ic)
87 {
88 }
89 
90 static void
91 hostap_vdetach(struct ieee80211vap *vap)
92 {
93 }
94 
95 static void
96 hostap_vattach(struct ieee80211vap *vap)
97 {
98 	vap->iv_newstate = hostap_newstate;
99 	vap->iv_input = hostap_input;
100 	vap->iv_recv_mgmt = hostap_recv_mgmt;
101 	vap->iv_recv_ctl = hostap_recv_ctl;
102 	vap->iv_opdetach = hostap_vdetach;
103 	vap->iv_deliver_data = hostap_deliver_data;
104 	vap->iv_recv_pspoll = ieee80211_recv_pspoll;
105 }
106 
107 static void
108 sta_disassoc(void *arg, struct ieee80211_node *ni)
109 {
110 	struct ieee80211vap *vap = arg;
111 
112 	if (ni->ni_vap == vap && ni->ni_associd != 0) {
113 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
114 			IEEE80211_REASON_ASSOC_LEAVE);
115 		ieee80211_node_leave(ni);
116 	}
117 }
118 
119 static void
120 sta_csa(void *arg, struct ieee80211_node *ni)
121 {
122 	struct ieee80211vap *vap = arg;
123 
124 	if (ni->ni_vap == vap && ni->ni_associd != 0)
125 		if (ni->ni_inact > vap->iv_inact_init) {
126 			ni->ni_inact = vap->iv_inact_init;
127 			IEEE80211_NOTE(vap, IEEE80211_MSG_INACT, ni,
128 			    "%s: inact %u", __func__, ni->ni_inact);
129 		}
130 }
131 
132 static void
133 sta_drop(void *arg, struct ieee80211_node *ni)
134 {
135 	struct ieee80211vap *vap = arg;
136 
137 	if (ni->ni_vap == vap && ni->ni_associd != 0)
138 		ieee80211_node_leave(ni);
139 }
140 
141 /*
142  * Does a channel change require associated stations to re-associate
143  * so protocol state is correct.  This is used when doing CSA across
144  * bands or similar (e.g. HT -> legacy).
145  */
146 static int
147 isbandchange(struct ieee80211com *ic)
148 {
149 	return ((ic->ic_bsschan->ic_flags ^ ic->ic_csa_newchan->ic_flags) &
150 	    (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_HALF |
151 	     IEEE80211_CHAN_QUARTER | IEEE80211_CHAN_HT)) != 0;
152 }
153 
154 /*
155  * IEEE80211_M_HOSTAP vap state machine handler.
156  */
157 static int
158 hostap_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
159 {
160 	struct ieee80211com *ic = vap->iv_ic;
161 	enum ieee80211_state ostate;
162 
163 	IEEE80211_LOCK_ASSERT(ic);
164 
165 	ostate = vap->iv_state;
166 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
167 	    __func__, ieee80211_state_name[ostate],
168 	    ieee80211_state_name[nstate], arg);
169 	vap->iv_state = nstate;			/* state transition */
170 	if (ostate != IEEE80211_S_SCAN)
171 		ieee80211_cancel_scan(vap);	/* background scan */
172 	switch (nstate) {
173 	case IEEE80211_S_INIT:
174 		switch (ostate) {
175 		case IEEE80211_S_SCAN:
176 			ieee80211_cancel_scan(vap);
177 			break;
178 		case IEEE80211_S_CAC:
179 			ieee80211_dfs_cac_stop(vap);
180 			break;
181 		case IEEE80211_S_RUN:
182 			ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
183 			break;
184 		default:
185 			break;
186 		}
187 		if (ostate != IEEE80211_S_INIT) {
188 			/* NB: optimize INIT -> INIT case */
189 			ieee80211_reset_bss(vap);
190 		}
191 		if (vap->iv_auth->ia_detach != NULL)
192 			vap->iv_auth->ia_detach(vap);
193 		break;
194 	case IEEE80211_S_SCAN:
195 		switch (ostate) {
196 		case IEEE80211_S_CSA:
197 		case IEEE80211_S_RUN:
198 			ieee80211_iterate_nodes(&ic->ic_sta, sta_disassoc, vap);
199 			/*
200 			 * Clear overlapping BSS state; the beacon frame
201 			 * will be reconstructed on transition to the RUN
202 			 * state and the timeout routines check if the flag
203 			 * is set before doing anything so this is sufficient.
204 			 */
205 			ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
206 			ic->ic_flags_ht &= ~IEEE80211_FHT_NONHT_PR;
207 			/* fall thru... */
208 		case IEEE80211_S_CAC:
209 			/*
210 			 * NB: We may get here because of a manual channel
211 			 *     change in which case we need to stop CAC
212 			 * XXX no need to stop if ostate RUN but it's ok
213 			 */
214 			ieee80211_dfs_cac_stop(vap);
215 			/* fall thru... */
216 		case IEEE80211_S_INIT:
217 			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
218 			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan)) {
219 				/*
220 				 * Already have a channel; bypass the
221 				 * scan and startup immediately.
222 				 * ieee80211_create_ibss will call back to
223 				 * move us to RUN state.
224 				 */
225 				ieee80211_create_ibss(vap, vap->iv_des_chan);
226 				break;
227 			}
228 			/*
229 			 * Initiate a scan.  We can come here as a result
230 			 * of an IEEE80211_IOC_SCAN_REQ too in which case
231 			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
232 			 * and the scan request parameters will be present
233 			 * in iv_scanreq.  Otherwise we do the default.
234 			 */
235 			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
236 				ieee80211_check_scan(vap,
237 				    vap->iv_scanreq_flags,
238 				    vap->iv_scanreq_duration,
239 				    vap->iv_scanreq_mindwell,
240 				    vap->iv_scanreq_maxdwell,
241 				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
242 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
243 			} else
244 				ieee80211_check_scan_current(vap);
245 			break;
246 		case IEEE80211_S_SCAN:
247 			/*
248 			 * A state change requires a reset; scan.
249 			 */
250 			ieee80211_check_scan_current(vap);
251 			break;
252 		default:
253 			break;
254 		}
255 		break;
256 	case IEEE80211_S_CAC:
257 		/*
258 		 * Start CAC on a DFS channel.  We come here when starting
259 		 * a bss on a DFS channel (see ieee80211_create_ibss).
260 		 */
261 		ieee80211_dfs_cac_start(vap);
262 		break;
263 	case IEEE80211_S_RUN:
264 		if (vap->iv_flags & IEEE80211_F_WPA) {
265 			/* XXX validate prerequisites */
266 		}
267 		switch (ostate) {
268 		case IEEE80211_S_INIT:
269 			/*
270 			 * Already have a channel; bypass the
271 			 * scan and startup immediately.
272 			 * Note that ieee80211_create_ibss will call
273 			 * back to do a RUN->RUN state change.
274 			 */
275 			ieee80211_create_ibss(vap,
276 			    ieee80211_ht_adjust_channel(ic,
277 				ic->ic_curchan, vap->iv_flags_ht));
278 			/* NB: iv_bss is changed on return */
279 			break;
280 		case IEEE80211_S_CAC:
281 			/*
282 			 * NB: This is the normal state change when CAC
283 			 * expires and no radar was detected; no need to
284 			 * clear the CAC timer as it's already expired.
285 			 */
286 			/* fall thru... */
287 		case IEEE80211_S_CSA:
288 			/*
289 			 * Shorten inactivity timer of associated stations
290 			 * to weed out sta's that don't follow a CSA.
291 			 */
292 			ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
293 			/*
294 			 * Update bss node channel to reflect where
295 			 * we landed after CSA.
296 			 */
297 			ieee80211_node_set_chan(vap->iv_bss,
298 			    ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
299 				ieee80211_htchanflags(vap->iv_bss->ni_chan)));
300 			/* XXX bypass debug msgs */
301 			break;
302 		case IEEE80211_S_SCAN:
303 		case IEEE80211_S_RUN:
304 #ifdef IEEE80211_DEBUG
305 			if (ieee80211_msg_debug(vap)) {
306 				struct ieee80211_node *ni = vap->iv_bss;
307 				ieee80211_note(vap,
308 				    "synchronized with %s ssid ",
309 				    ether_sprintf(ni->ni_bssid));
310 				ieee80211_print_essid(ni->ni_essid,
311 				    ni->ni_esslen);
312 				/* XXX MCS/HT */
313 				printf(" channel %d start %uMb\n",
314 				    ieee80211_chan2ieee(ic, ic->ic_curchan),
315 				    IEEE80211_RATE2MBS(ni->ni_txrate));
316 			}
317 #endif
318 			break;
319 		default:
320 			break;
321 		}
322 		/*
323 		 * Start/stop the authenticator.  We delay until here
324 		 * to allow configuration to happen out of order.
325 		 */
326 		if (vap->iv_auth->ia_attach != NULL) {
327 			/* XXX check failure */
328 			vap->iv_auth->ia_attach(vap);
329 		} else if (vap->iv_auth->ia_detach != NULL) {
330 			vap->iv_auth->ia_detach(vap);
331 		}
332 		ieee80211_node_authorize(vap->iv_bss);
333 		break;
334 	case IEEE80211_S_CSA:
335 		if (ostate == IEEE80211_S_RUN && isbandchange(ic)) {
336 			/*
337 			 * On a ``band change'' silently drop associated
338 			 * stations as they must re-associate before they
339 			 * can pass traffic (as otherwise protocol state
340 			 * such as capabilities and the negotiated rate
341 			 * set may/will be wrong).
342 			 */
343 			ieee80211_iterate_nodes(&ic->ic_sta, sta_drop, vap);
344 		}
345 		break;
346 	default:
347 		break;
348 	}
349 	return 0;
350 }
351 
352 static void
353 hostap_deliver_data(struct ieee80211vap *vap,
354 	struct ieee80211_node *ni, struct mbuf *m)
355 {
356 	struct ether_header *eh = mtod(m, struct ether_header *);
357 	struct ifnet *ifp = vap->iv_ifp;
358 
359 	/* clear driver/net80211 flags before passing up */
360 	m->m_flags &= ~(M_MCAST | M_BCAST);
361 	m_clrprotoflags(m);
362 
363 	KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP,
364 	    ("gack, opmode %d", vap->iv_opmode));
365 	/*
366 	 * Do accounting.
367 	 */
368 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
369 	IEEE80211_NODE_STAT(ni, rx_data);
370 	IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
371 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
372 		m->m_flags |= M_MCAST;		/* XXX M_BCAST? */
373 		IEEE80211_NODE_STAT(ni, rx_mcast);
374 	} else
375 		IEEE80211_NODE_STAT(ni, rx_ucast);
376 
377 	/* perform as a bridge within the AP */
378 	if ((vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0) {
379 		struct mbuf *mcopy = NULL;
380 
381 		if (m->m_flags & M_MCAST) {
382 			mcopy = m_dup(m, M_NOWAIT);
383 			if (mcopy == NULL)
384 				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
385 			else
386 				mcopy->m_flags |= M_MCAST;
387 		} else {
388 			/*
389 			 * Check if the destination is associated with the
390 			 * same vap and authorized to receive traffic.
391 			 * Beware of traffic destined for the vap itself;
392 			 * sending it will not work; just let it be delivered
393 			 * normally.
394 			 */
395 			struct ieee80211_node *sta = ieee80211_find_vap_node(
396 			     &vap->iv_ic->ic_sta, vap, eh->ether_dhost);
397 			if (sta != NULL) {
398 				if (ieee80211_node_is_authorized(sta)) {
399 					/*
400 					 * Beware of sending to ourself; this
401 					 * needs to happen via the normal
402 					 * input path.
403 					 */
404 					if (sta != vap->iv_bss) {
405 						mcopy = m;
406 						m = NULL;
407 					}
408 				} else {
409 					vap->iv_stats.is_rx_unauth++;
410 					IEEE80211_NODE_STAT(sta, rx_unauth);
411 				}
412 				ieee80211_free_node(sta);
413 			}
414 		}
415 		if (mcopy != NULL) {
416 			int len, err;
417 			len = mcopy->m_pkthdr.len;
418 			err = ieee80211_vap_xmitpkt(vap, mcopy);
419 			if (err) {
420 				/* NB: IFQ_HANDOFF reclaims mcopy */
421 			} else {
422 				if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
423 			}
424 		}
425 	}
426 	if (m != NULL) {
427 		/*
428 		 * Mark frame as coming from vap's interface.
429 		 */
430 		m->m_pkthdr.rcvif = ifp;
431 		if (m->m_flags & M_MCAST) {
432 			/*
433 			 * Spam DWDS vap's w/ multicast traffic.
434 			 */
435 			/* XXX only if dwds in use? */
436 			ieee80211_dwds_mcast(vap, m);
437 		}
438 		if (ni->ni_vlan != 0) {
439 			/* attach vlan tag */
440 			m->m_pkthdr.ether_vtag = ni->ni_vlan;
441 			m->m_flags |= M_VLANTAG;
442 		}
443 		ifp->if_input(ifp, m);
444 	}
445 }
446 
447 /*
448  * Decide if a received management frame should be
449  * printed when debugging is enabled.  This filters some
450  * of the less interesting frames that come frequently
451  * (e.g. beacons).
452  */
453 static __inline int
454 doprint(struct ieee80211vap *vap, int subtype)
455 {
456 	switch (subtype) {
457 	case IEEE80211_FC0_SUBTYPE_BEACON:
458 		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
459 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
460 		return 0;
461 	}
462 	return 1;
463 }
464 
465 /*
466  * Process a received frame.  The node associated with the sender
467  * should be supplied.  If nothing was found in the node table then
468  * the caller is assumed to supply a reference to iv_bss instead.
469  * The RSSI and a timestamp are also supplied.  The RSSI data is used
470  * during AP scanning to select a AP to associate with; it can have
471  * any units so long as values have consistent units and higher values
472  * mean ``better signal''.  The receive timestamp is currently not used
473  * by the 802.11 layer.
474  */
475 static int
476 hostap_input(struct ieee80211_node *ni, struct mbuf *m,
477     const struct ieee80211_rx_stats *rxs, int rssi, int nf)
478 {
479 	struct ieee80211vap *vap = ni->ni_vap;
480 	struct ieee80211com *ic = ni->ni_ic;
481 	struct ifnet *ifp = vap->iv_ifp;
482 	struct ieee80211_frame *wh;
483 	struct ieee80211_key *key;
484 	struct ether_header *eh;
485 	int hdrspace, need_tap = 1;	/* mbuf need to be tapped. */
486 	uint8_t dir, type, subtype, qos;
487 	uint8_t *bssid;
488 
489 	if (m->m_flags & M_AMPDU_MPDU) {
490 		/*
491 		 * Fastpath for A-MPDU reorder q resubmission.  Frames
492 		 * w/ M_AMPDU_MPDU marked have already passed through
493 		 * here but were received out of order and been held on
494 		 * the reorder queue.  When resubmitted they are marked
495 		 * with the M_AMPDU_MPDU flag and we can bypass most of
496 		 * the normal processing.
497 		 */
498 		wh = mtod(m, struct ieee80211_frame *);
499 		type = IEEE80211_FC0_TYPE_DATA;
500 		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
501 		subtype = IEEE80211_FC0_SUBTYPE_QOS;
502 		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
503 		goto resubmit_ampdu;
504 	}
505 
506 	KASSERT(ni != NULL, ("null node"));
507 	ni->ni_inact = ni->ni_inact_reload;
508 
509 	type = -1;			/* undefined */
510 
511 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
512 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
513 		    ni->ni_macaddr, NULL,
514 		    "too short (1): len %u", m->m_pkthdr.len);
515 		vap->iv_stats.is_rx_tooshort++;
516 		goto out;
517 	}
518 	/*
519 	 * Bit of a cheat here, we use a pointer for a 3-address
520 	 * frame format but don't reference fields past outside
521 	 * ieee80211_frame_min w/o first validating the data is
522 	 * present.
523 	 */
524 	wh = mtod(m, struct ieee80211_frame *);
525 
526 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
527 	    IEEE80211_FC0_VERSION_0) {
528 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
529 		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
530 		    wh->i_fc[0], wh->i_fc[1]);
531 		vap->iv_stats.is_rx_badversion++;
532 		goto err;
533 	}
534 
535 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
536 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
537 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
538 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
539 		if (dir != IEEE80211_FC1_DIR_NODS)
540 			bssid = wh->i_addr1;
541 		else if (type == IEEE80211_FC0_TYPE_CTL)
542 			bssid = wh->i_addr1;
543 		else {
544 			if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
545 				IEEE80211_DISCARD_MAC(vap,
546 				    IEEE80211_MSG_ANY, ni->ni_macaddr,
547 				    NULL, "too short (2): len %u",
548 				    m->m_pkthdr.len);
549 				vap->iv_stats.is_rx_tooshort++;
550 				goto out;
551 			}
552 			bssid = wh->i_addr3;
553 		}
554 		/*
555 		 * Validate the bssid.
556 		 */
557 		if (!(type == IEEE80211_FC0_TYPE_MGT &&
558 		      subtype == IEEE80211_FC0_SUBTYPE_BEACON) &&
559 		    !IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
560 		    !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
561 			/* not interested in */
562 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
563 			    bssid, NULL, "%s", "not to bss");
564 			vap->iv_stats.is_rx_wrongbss++;
565 			goto out;
566 		}
567 
568 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
569 		ni->ni_noise = nf;
570 		if (IEEE80211_HAS_SEQ(type, subtype)) {
571 			uint8_t tid = ieee80211_gettid(wh);
572 			if (IEEE80211_QOS_HAS_SEQ(wh) &&
573 			    TID_TO_WME_AC(tid) >= WME_AC_VI)
574 				ic->ic_wme.wme_hipri_traffic++;
575 			if (! ieee80211_check_rxseq(ni, wh, bssid))
576 				goto out;
577 		}
578 	}
579 
580 	switch (type) {
581 	case IEEE80211_FC0_TYPE_DATA:
582 		hdrspace = ieee80211_hdrspace(ic, wh);
583 		if (m->m_len < hdrspace &&
584 		    (m = m_pullup(m, hdrspace)) == NULL) {
585 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
586 			    ni->ni_macaddr, NULL,
587 			    "data too short: expecting %u", hdrspace);
588 			vap->iv_stats.is_rx_tooshort++;
589 			goto out;		/* XXX */
590 		}
591 		if (!(dir == IEEE80211_FC1_DIR_TODS ||
592 		     (dir == IEEE80211_FC1_DIR_DSTODS &&
593 		      (vap->iv_flags & IEEE80211_F_DWDS)))) {
594 			if (dir != IEEE80211_FC1_DIR_DSTODS) {
595 				IEEE80211_DISCARD(vap,
596 				    IEEE80211_MSG_INPUT, wh, "data",
597 				    "incorrect dir 0x%x", dir);
598 			} else {
599 				IEEE80211_DISCARD(vap,
600 				    IEEE80211_MSG_INPUT |
601 				    IEEE80211_MSG_WDS, wh,
602 				    "4-address data",
603 				    "%s", "DWDS not enabled");
604 			}
605 			vap->iv_stats.is_rx_wrongdir++;
606 			goto out;
607 		}
608 		/* check if source STA is associated */
609 		if (ni == vap->iv_bss) {
610 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
611 			    wh, "data", "%s", "unknown src");
612 			ieee80211_send_error(ni, wh->i_addr2,
613 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
614 			    IEEE80211_REASON_NOT_AUTHED);
615 			vap->iv_stats.is_rx_notassoc++;
616 			goto err;
617 		}
618 		if (ni->ni_associd == 0) {
619 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
620 			    wh, "data", "%s", "unassoc src");
621 			IEEE80211_SEND_MGMT(ni,
622 			    IEEE80211_FC0_SUBTYPE_DISASSOC,
623 			    IEEE80211_REASON_NOT_ASSOCED);
624 			vap->iv_stats.is_rx_notassoc++;
625 			goto err;
626 		}
627 
628 		/*
629 		 * Check for power save state change.
630 		 * XXX out-of-order A-MPDU frames?
631 		 */
632 		if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
633 		    (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
634 			vap->iv_node_ps(ni,
635 				wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
636 		/*
637 		 * For 4-address packets handle WDS discovery
638 		 * notifications.  Once a WDS link is setup frames
639 		 * are just delivered to the WDS vap (see below).
640 		 */
641 		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap == NULL) {
642 			if (!ieee80211_node_is_authorized(ni)) {
643 				IEEE80211_DISCARD(vap,
644 				    IEEE80211_MSG_INPUT |
645 				    IEEE80211_MSG_WDS, wh,
646 				    "4-address data",
647 				    "%s", "unauthorized port");
648 				vap->iv_stats.is_rx_unauth++;
649 				IEEE80211_NODE_STAT(ni, rx_unauth);
650 				goto err;
651 			}
652 			ieee80211_dwds_discover(ni, m);
653 			return type;
654 		}
655 
656 		/*
657 		 * Handle A-MPDU re-ordering.  If the frame is to be
658 		 * processed directly then ieee80211_ampdu_reorder
659 		 * will return 0; otherwise it has consumed the mbuf
660 		 * and we should do nothing more with it.
661 		 */
662 		if ((m->m_flags & M_AMPDU) &&
663 		    ieee80211_ampdu_reorder(ni, m) != 0) {
664 			m = NULL;
665 			goto out;
666 		}
667 	resubmit_ampdu:
668 
669 		/*
670 		 * Handle privacy requirements.  Note that we
671 		 * must not be preempted from here until after
672 		 * we (potentially) call ieee80211_crypto_demic;
673 		 * otherwise we may violate assumptions in the
674 		 * crypto cipher modules used to do delayed update
675 		 * of replay sequence numbers.
676 		 */
677 		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
678 			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
679 				/*
680 				 * Discard encrypted frames when privacy is off.
681 				 */
682 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
683 				    wh, "WEP", "%s", "PRIVACY off");
684 				vap->iv_stats.is_rx_noprivacy++;
685 				IEEE80211_NODE_STAT(ni, rx_noprivacy);
686 				goto out;
687 			}
688 			key = ieee80211_crypto_decap(ni, m, hdrspace);
689 			if (key == NULL) {
690 				/* NB: stats+msgs handled in crypto_decap */
691 				IEEE80211_NODE_STAT(ni, rx_wepfail);
692 				goto out;
693 			}
694 			wh = mtod(m, struct ieee80211_frame *);
695 			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
696 		} else {
697 			/* XXX M_WEP and IEEE80211_F_PRIVACY */
698 			key = NULL;
699 		}
700 
701 		/*
702 		 * Save QoS bits for use below--before we strip the header.
703 		 */
704 		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
705 			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
706 			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
707 			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
708 		} else
709 			qos = 0;
710 
711 		/*
712 		 * Next up, any fragmentation.
713 		 */
714 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
715 			m = ieee80211_defrag(ni, m, hdrspace);
716 			if (m == NULL) {
717 				/* Fragment dropped or frame not complete yet */
718 				goto out;
719 			}
720 		}
721 		wh = NULL;		/* no longer valid, catch any uses */
722 
723 		/*
724 		 * Next strip any MSDU crypto bits.
725 		 */
726 		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
727 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
728 			    ni->ni_macaddr, "data", "%s", "demic error");
729 			vap->iv_stats.is_rx_demicfail++;
730 			IEEE80211_NODE_STAT(ni, rx_demicfail);
731 			goto out;
732 		}
733 		/* copy to listener after decrypt */
734 		if (ieee80211_radiotap_active_vap(vap))
735 			ieee80211_radiotap_rx(vap, m);
736 		need_tap = 0;
737 		/*
738 		 * Finally, strip the 802.11 header.
739 		 */
740 		m = ieee80211_decap(vap, m, hdrspace);
741 		if (m == NULL) {
742 			/* XXX mask bit to check for both */
743 			/* don't count Null data frames as errors */
744 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
745 			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
746 				goto out;
747 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
748 			    ni->ni_macaddr, "data", "%s", "decap error");
749 			vap->iv_stats.is_rx_decap++;
750 			IEEE80211_NODE_STAT(ni, rx_decap);
751 			goto err;
752 		}
753 		eh = mtod(m, struct ether_header *);
754 		if (!ieee80211_node_is_authorized(ni)) {
755 			/*
756 			 * Deny any non-PAE frames received prior to
757 			 * authorization.  For open/shared-key
758 			 * authentication the port is mark authorized
759 			 * after authentication completes.  For 802.1x
760 			 * the port is not marked authorized by the
761 			 * authenticator until the handshake has completed.
762 			 */
763 			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
764 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
765 				    eh->ether_shost, "data",
766 				    "unauthorized port: ether type 0x%x len %u",
767 				    eh->ether_type, m->m_pkthdr.len);
768 				vap->iv_stats.is_rx_unauth++;
769 				IEEE80211_NODE_STAT(ni, rx_unauth);
770 				goto err;
771 			}
772 		} else {
773 			/*
774 			 * When denying unencrypted frames, discard
775 			 * any non-PAE frames received without encryption.
776 			 */
777 			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
778 			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
779 			    eh->ether_type != htons(ETHERTYPE_PAE)) {
780 				/*
781 				 * Drop unencrypted frames.
782 				 */
783 				vap->iv_stats.is_rx_unencrypted++;
784 				IEEE80211_NODE_STAT(ni, rx_unencrypted);
785 				goto out;
786 			}
787 		}
788 		/* XXX require HT? */
789 		if (qos & IEEE80211_QOS_AMSDU) {
790 			m = ieee80211_decap_amsdu(ni, m);
791 			if (m == NULL)
792 				return IEEE80211_FC0_TYPE_DATA;
793 		} else {
794 #ifdef IEEE80211_SUPPORT_SUPERG
795 			m = ieee80211_decap_fastframe(vap, ni, m);
796 			if (m == NULL)
797 				return IEEE80211_FC0_TYPE_DATA;
798 #endif
799 		}
800 		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
801 			ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
802 		else
803 			hostap_deliver_data(vap, ni, m);
804 		return IEEE80211_FC0_TYPE_DATA;
805 
806 	case IEEE80211_FC0_TYPE_MGT:
807 		vap->iv_stats.is_rx_mgmt++;
808 		IEEE80211_NODE_STAT(ni, rx_mgmt);
809 		if (dir != IEEE80211_FC1_DIR_NODS) {
810 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
811 			    wh, "mgt", "incorrect dir 0x%x", dir);
812 			vap->iv_stats.is_rx_wrongdir++;
813 			goto err;
814 		}
815 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
816 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
817 			    ni->ni_macaddr, "mgt", "too short: len %u",
818 			    m->m_pkthdr.len);
819 			vap->iv_stats.is_rx_tooshort++;
820 			goto out;
821 		}
822 		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
823 			/* ensure return frames are unicast */
824 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
825 			    wh, NULL, "source is multicast: %s",
826 			    ether_sprintf(wh->i_addr2));
827 			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
828 			goto out;
829 		}
830 #ifdef IEEE80211_DEBUG
831 		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
832 		    ieee80211_msg_dumppkts(vap)) {
833 			if_printf(ifp, "received %s from %s rssi %d\n",
834 			    ieee80211_mgt_subtype_name[subtype >>
835 				IEEE80211_FC0_SUBTYPE_SHIFT],
836 			    ether_sprintf(wh->i_addr2), rssi);
837 		}
838 #endif
839 		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
840 			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
841 				/*
842 				 * Only shared key auth frames with a challenge
843 				 * should be encrypted, discard all others.
844 				 */
845 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
846 				    wh, NULL,
847 				    "%s", "WEP set but not permitted");
848 				vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
849 				goto out;
850 			}
851 			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
852 				/*
853 				 * Discard encrypted frames when privacy is off.
854 				 */
855 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
856 				    wh, NULL, "%s", "WEP set but PRIVACY off");
857 				vap->iv_stats.is_rx_noprivacy++;
858 				goto out;
859 			}
860 			hdrspace = ieee80211_hdrspace(ic, wh);
861 			key = ieee80211_crypto_decap(ni, m, hdrspace);
862 			if (key == NULL) {
863 				/* NB: stats+msgs handled in crypto_decap */
864 				goto out;
865 			}
866 			wh = mtod(m, struct ieee80211_frame *);
867 			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
868 		}
869 		/*
870 		 * Pass the packet to radiotap before calling iv_recv_mgmt().
871 		 * Otherwise iv_recv_mgmt() might pass another packet to
872 		 * radiotap, resulting in out of order packet captures.
873 		 */
874 		if (ieee80211_radiotap_active_vap(vap))
875 			ieee80211_radiotap_rx(vap, m);
876 		need_tap = 0;
877 		vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
878 		goto out;
879 
880 	case IEEE80211_FC0_TYPE_CTL:
881 		vap->iv_stats.is_rx_ctl++;
882 		IEEE80211_NODE_STAT(ni, rx_ctrl);
883 		vap->iv_recv_ctl(ni, m, subtype);
884 		goto out;
885 	default:
886 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
887 		    wh, "bad", "frame type 0x%x", type);
888 		/* should not come here */
889 		break;
890 	}
891 err:
892 	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
893 out:
894 	if (m != NULL) {
895 		if (need_tap && ieee80211_radiotap_active_vap(vap))
896 			ieee80211_radiotap_rx(vap, m);
897 		m_freem(m);
898 	}
899 	return type;
900 }
901 
902 static void
903 hostap_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
904     int rssi, int nf, uint16_t seq, uint16_t status)
905 {
906 	struct ieee80211vap *vap = ni->ni_vap;
907 
908 	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
909 
910 	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
911 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
912 		    ni->ni_macaddr, "open auth",
913 		    "bad sta auth mode %u", ni->ni_authmode);
914 		vap->iv_stats.is_rx_bad_auth++;	/* XXX */
915 		/*
916 		 * Clear any challenge text that may be there if
917 		 * a previous shared key auth failed and then an
918 		 * open auth is attempted.
919 		 */
920 		if (ni->ni_challenge != NULL) {
921 			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
922 			ni->ni_challenge = NULL;
923 		}
924 		/* XXX hack to workaround calling convention */
925 		ieee80211_send_error(ni, wh->i_addr2,
926 		    IEEE80211_FC0_SUBTYPE_AUTH,
927 		    (seq + 1) | (IEEE80211_STATUS_ALG<<16));
928 		return;
929 	}
930 	if (seq != IEEE80211_AUTH_OPEN_REQUEST) {
931 		vap->iv_stats.is_rx_bad_auth++;
932 		return;
933 	}
934 	/* always accept open authentication requests */
935 	if (ni == vap->iv_bss) {
936 		ni = ieee80211_dup_bss(vap, wh->i_addr2);
937 		if (ni == NULL)
938 			return;
939 	} else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
940 		(void) ieee80211_ref_node(ni);
941 	/*
942 	 * Mark the node as referenced to reflect that it's
943 	 * reference count has been bumped to insure it remains
944 	 * after the transaction completes.
945 	 */
946 	ni->ni_flags |= IEEE80211_NODE_AREF;
947 	/*
948 	 * Mark the node as requiring a valid association id
949 	 * before outbound traffic is permitted.
950 	 */
951 	ni->ni_flags |= IEEE80211_NODE_ASSOCID;
952 
953 	if (vap->iv_acl != NULL &&
954 	    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
955 		/*
956 		 * When the ACL policy is set to RADIUS we defer the
957 		 * authorization to a user agent.  Dispatch an event,
958 		 * a subsequent MLME call will decide the fate of the
959 		 * station.  If the user agent is not present then the
960 		 * node will be reclaimed due to inactivity.
961 		 */
962 		IEEE80211_NOTE_MAC(vap,
963 		    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL, ni->ni_macaddr,
964 		    "%s", "station authentication defered (radius acl)");
965 		ieee80211_notify_node_auth(ni);
966 	} else {
967 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
968 		IEEE80211_NOTE_MAC(vap,
969 		    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni->ni_macaddr,
970 		    "%s", "station authenticated (open)");
971 		/*
972 		 * When 802.1x is not in use mark the port
973 		 * authorized at this point so traffic can flow.
974 		 */
975 		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
976 			ieee80211_node_authorize(ni);
977 	}
978 }
979 
980 static void
981 hostap_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
982     uint8_t *frm, uint8_t *efrm, int rssi, int nf,
983     uint16_t seq, uint16_t status)
984 {
985 	struct ieee80211vap *vap = ni->ni_vap;
986 	uint8_t *challenge;
987 	int allocbs, estatus;
988 
989 	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
990 
991 	/*
992 	 * NB: this can happen as we allow pre-shared key
993 	 * authentication to be enabled w/o wep being turned
994 	 * on so that configuration of these can be done
995 	 * in any order.  It may be better to enforce the
996 	 * ordering in which case this check would just be
997 	 * for sanity/consistency.
998 	 */
999 	if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
1000 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1001 		    ni->ni_macaddr, "shared key auth",
1002 		    "%s", " PRIVACY is disabled");
1003 		estatus = IEEE80211_STATUS_ALG;
1004 		goto bad;
1005 	}
1006 	/*
1007 	 * Pre-shared key authentication is evil; accept
1008 	 * it only if explicitly configured (it is supported
1009 	 * mainly for compatibility with clients like Mac OS X).
1010 	 */
1011 	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1012 	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1013 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1014 		    ni->ni_macaddr, "shared key auth",
1015 		    "bad sta auth mode %u", ni->ni_authmode);
1016 		vap->iv_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
1017 		estatus = IEEE80211_STATUS_ALG;
1018 		goto bad;
1019 	}
1020 
1021 	challenge = NULL;
1022 	if (frm + 1 < efrm) {
1023 		if ((frm[1] + 2) > (efrm - frm)) {
1024 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1025 			    ni->ni_macaddr, "shared key auth",
1026 			    "ie %d/%d too long",
1027 			    frm[0], (frm[1] + 2) - (efrm - frm));
1028 			vap->iv_stats.is_rx_bad_auth++;
1029 			estatus = IEEE80211_STATUS_CHALLENGE;
1030 			goto bad;
1031 		}
1032 		if (*frm == IEEE80211_ELEMID_CHALLENGE)
1033 			challenge = frm;
1034 		frm += frm[1] + 2;
1035 	}
1036 	switch (seq) {
1037 	case IEEE80211_AUTH_SHARED_CHALLENGE:
1038 	case IEEE80211_AUTH_SHARED_RESPONSE:
1039 		if (challenge == NULL) {
1040 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1041 			    ni->ni_macaddr, "shared key auth",
1042 			    "%s", "no challenge");
1043 			vap->iv_stats.is_rx_bad_auth++;
1044 			estatus = IEEE80211_STATUS_CHALLENGE;
1045 			goto bad;
1046 		}
1047 		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1048 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1049 			    ni->ni_macaddr, "shared key auth",
1050 			    "bad challenge len %d", challenge[1]);
1051 			vap->iv_stats.is_rx_bad_auth++;
1052 			estatus = IEEE80211_STATUS_CHALLENGE;
1053 			goto bad;
1054 		}
1055 	default:
1056 		break;
1057 	}
1058 	switch (seq) {
1059 	case IEEE80211_AUTH_SHARED_REQUEST:
1060 		if (ni == vap->iv_bss) {
1061 			ni = ieee80211_dup_bss(vap, wh->i_addr2);
1062 			if (ni == NULL) {
1063 				/* NB: no way to return an error */
1064 				return;
1065 			}
1066 			allocbs = 1;
1067 		} else {
1068 			if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1069 				(void) ieee80211_ref_node(ni);
1070 			allocbs = 0;
1071 		}
1072 		/*
1073 		 * Mark the node as referenced to reflect that it's
1074 		 * reference count has been bumped to insure it remains
1075 		 * after the transaction completes.
1076 		 */
1077 		ni->ni_flags |= IEEE80211_NODE_AREF;
1078 		/*
1079 		 * Mark the node as requiring a valid associatio id
1080 		 * before outbound traffic is permitted.
1081 		 */
1082 		ni->ni_flags |= IEEE80211_NODE_ASSOCID;
1083 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1084 		ni->ni_noise = nf;
1085 		if (!ieee80211_alloc_challenge(ni)) {
1086 			/* NB: don't return error so they rexmit */
1087 			return;
1088 		}
1089 		get_random_bytes(ni->ni_challenge,
1090 			IEEE80211_CHALLENGE_LEN);
1091 		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1092 		    ni, "shared key %sauth request", allocbs ? "" : "re");
1093 		/*
1094 		 * When the ACL policy is set to RADIUS we defer the
1095 		 * authorization to a user agent.  Dispatch an event,
1096 		 * a subsequent MLME call will decide the fate of the
1097 		 * station.  If the user agent is not present then the
1098 		 * node will be reclaimed due to inactivity.
1099 		 */
1100 		if (vap->iv_acl != NULL &&
1101 		    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
1102 			IEEE80211_NOTE_MAC(vap,
1103 			    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL,
1104 			    ni->ni_macaddr,
1105 			    "%s", "station authentication defered (radius acl)");
1106 			ieee80211_notify_node_auth(ni);
1107 			return;
1108 		}
1109 		break;
1110 	case IEEE80211_AUTH_SHARED_RESPONSE:
1111 		if (ni == vap->iv_bss) {
1112 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1113 			    ni->ni_macaddr, "shared key response",
1114 			    "%s", "unknown station");
1115 			/* NB: don't send a response */
1116 			return;
1117 		}
1118 		if (ni->ni_challenge == NULL) {
1119 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1120 			    ni->ni_macaddr, "shared key response",
1121 			    "%s", "no challenge recorded");
1122 			vap->iv_stats.is_rx_bad_auth++;
1123 			estatus = IEEE80211_STATUS_CHALLENGE;
1124 			goto bad;
1125 		}
1126 		if (memcmp(ni->ni_challenge, &challenge[2],
1127 			   challenge[1]) != 0) {
1128 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1129 			    ni->ni_macaddr, "shared key response",
1130 			    "%s", "challenge mismatch");
1131 			vap->iv_stats.is_rx_auth_fail++;
1132 			estatus = IEEE80211_STATUS_CHALLENGE;
1133 			goto bad;
1134 		}
1135 		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1136 		    ni, "%s", "station authenticated (shared key)");
1137 		ieee80211_node_authorize(ni);
1138 		break;
1139 	default:
1140 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1141 		    ni->ni_macaddr, "shared key auth",
1142 		    "bad seq %d", seq);
1143 		vap->iv_stats.is_rx_bad_auth++;
1144 		estatus = IEEE80211_STATUS_SEQUENCE;
1145 		goto bad;
1146 	}
1147 	IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1148 	return;
1149 bad:
1150 	/*
1151 	 * Send an error response; but only when operating as an AP.
1152 	 */
1153 	/* XXX hack to workaround calling convention */
1154 	ieee80211_send_error(ni, wh->i_addr2,
1155 	    IEEE80211_FC0_SUBTYPE_AUTH,
1156 	    (seq + 1) | (estatus<<16));
1157 }
1158 
1159 /*
1160  * Convert a WPA cipher selector OUI to an internal
1161  * cipher algorithm.  Where appropriate we also
1162  * record any key length.
1163  */
1164 static int
1165 wpa_cipher(const uint8_t *sel, uint8_t *keylen)
1166 {
1167 #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1168 	uint32_t w = LE_READ_4(sel);
1169 
1170 	switch (w) {
1171 	case WPA_SEL(WPA_CSE_NULL):
1172 		return IEEE80211_CIPHER_NONE;
1173 	case WPA_SEL(WPA_CSE_WEP40):
1174 		if (keylen)
1175 			*keylen = 40 / NBBY;
1176 		return IEEE80211_CIPHER_WEP;
1177 	case WPA_SEL(WPA_CSE_WEP104):
1178 		if (keylen)
1179 			*keylen = 104 / NBBY;
1180 		return IEEE80211_CIPHER_WEP;
1181 	case WPA_SEL(WPA_CSE_TKIP):
1182 		return IEEE80211_CIPHER_TKIP;
1183 	case WPA_SEL(WPA_CSE_CCMP):
1184 		return IEEE80211_CIPHER_AES_CCM;
1185 	}
1186 	return 32;		/* NB: so 1<< is discarded */
1187 #undef WPA_SEL
1188 }
1189 
1190 /*
1191  * Convert a WPA key management/authentication algorithm
1192  * to an internal code.
1193  */
1194 static int
1195 wpa_keymgmt(const uint8_t *sel)
1196 {
1197 #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1198 	uint32_t w = LE_READ_4(sel);
1199 
1200 	switch (w) {
1201 	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
1202 		return WPA_ASE_8021X_UNSPEC;
1203 	case WPA_SEL(WPA_ASE_8021X_PSK):
1204 		return WPA_ASE_8021X_PSK;
1205 	case WPA_SEL(WPA_ASE_NONE):
1206 		return WPA_ASE_NONE;
1207 	}
1208 	return 0;		/* NB: so is discarded */
1209 #undef WPA_SEL
1210 }
1211 
1212 /*
1213  * Parse a WPA information element to collect parameters.
1214  * Note that we do not validate security parameters; that
1215  * is handled by the authenticator; the parsing done here
1216  * is just for internal use in making operational decisions.
1217  */
1218 static int
1219 ieee80211_parse_wpa(struct ieee80211vap *vap, const uint8_t *frm,
1220 	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1221 {
1222 	uint8_t len = frm[1];
1223 	uint32_t w;
1224 	int n;
1225 
1226 	/*
1227 	 * Check the length once for fixed parts: OUI, type,
1228 	 * version, mcast cipher, and 2 selector counts.
1229 	 * Other, variable-length data, must be checked separately.
1230 	 */
1231 	if ((vap->iv_flags & IEEE80211_F_WPA1) == 0) {
1232 		IEEE80211_DISCARD_IE(vap,
1233 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1234 		    wh, "WPA", "not WPA, flags 0x%x", vap->iv_flags);
1235 		return IEEE80211_REASON_IE_INVALID;
1236 	}
1237 	if (len < 14) {
1238 		IEEE80211_DISCARD_IE(vap,
1239 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1240 		    wh, "WPA", "too short, len %u", len);
1241 		return IEEE80211_REASON_IE_INVALID;
1242 	}
1243 	frm += 6, len -= 4;		/* NB: len is payload only */
1244 	/* NB: iswpaoui already validated the OUI and type */
1245 	w = LE_READ_2(frm);
1246 	if (w != WPA_VERSION) {
1247 		IEEE80211_DISCARD_IE(vap,
1248 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1249 		    wh, "WPA", "bad version %u", w);
1250 		return IEEE80211_REASON_IE_INVALID;
1251 	}
1252 	frm += 2, len -= 2;
1253 
1254 	memset(rsn, 0, sizeof(*rsn));
1255 
1256 	/* multicast/group cipher */
1257 	rsn->rsn_mcastcipher = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
1258 	frm += 4, len -= 4;
1259 
1260 	/* unicast ciphers */
1261 	n = LE_READ_2(frm);
1262 	frm += 2, len -= 2;
1263 	if (len < n*4+2) {
1264 		IEEE80211_DISCARD_IE(vap,
1265 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1266 		    wh, "WPA", "ucast cipher data too short; len %u, n %u",
1267 		    len, n);
1268 		return IEEE80211_REASON_IE_INVALID;
1269 	}
1270 	w = 0;
1271 	for (; n > 0; n--) {
1272 		w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
1273 		frm += 4, len -= 4;
1274 	}
1275 	if (w & (1<<IEEE80211_CIPHER_TKIP))
1276 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1277 	else
1278 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1279 
1280 	/* key management algorithms */
1281 	n = LE_READ_2(frm);
1282 	frm += 2, len -= 2;
1283 	if (len < n*4) {
1284 		IEEE80211_DISCARD_IE(vap,
1285 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1286 		    wh, "WPA", "key mgmt alg data too short; len %u, n %u",
1287 		    len, n);
1288 		return IEEE80211_REASON_IE_INVALID;
1289 	}
1290 	w = 0;
1291 	for (; n > 0; n--) {
1292 		w |= wpa_keymgmt(frm);
1293 		frm += 4, len -= 4;
1294 	}
1295 	if (w & WPA_ASE_8021X_UNSPEC)
1296 		rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
1297 	else
1298 		rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1299 
1300 	if (len > 2)		/* optional capabilities */
1301 		rsn->rsn_caps = LE_READ_2(frm);
1302 
1303 	return 0;
1304 }
1305 
1306 /*
1307  * Convert an RSN cipher selector OUI to an internal
1308  * cipher algorithm.  Where appropriate we also
1309  * record any key length.
1310  */
1311 static int
1312 rsn_cipher(const uint8_t *sel, uint8_t *keylen)
1313 {
1314 #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1315 	uint32_t w = LE_READ_4(sel);
1316 
1317 	switch (w) {
1318 	case RSN_SEL(RSN_CSE_NULL):
1319 		return IEEE80211_CIPHER_NONE;
1320 	case RSN_SEL(RSN_CSE_WEP40):
1321 		if (keylen)
1322 			*keylen = 40 / NBBY;
1323 		return IEEE80211_CIPHER_WEP;
1324 	case RSN_SEL(RSN_CSE_WEP104):
1325 		if (keylen)
1326 			*keylen = 104 / NBBY;
1327 		return IEEE80211_CIPHER_WEP;
1328 	case RSN_SEL(RSN_CSE_TKIP):
1329 		return IEEE80211_CIPHER_TKIP;
1330 	case RSN_SEL(RSN_CSE_CCMP):
1331 		return IEEE80211_CIPHER_AES_CCM;
1332 	case RSN_SEL(RSN_CSE_WRAP):
1333 		return IEEE80211_CIPHER_AES_OCB;
1334 	}
1335 	return 32;		/* NB: so 1<< is discarded */
1336 #undef WPA_SEL
1337 }
1338 
1339 /*
1340  * Convert an RSN key management/authentication algorithm
1341  * to an internal code.
1342  */
1343 static int
1344 rsn_keymgmt(const uint8_t *sel)
1345 {
1346 #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1347 	uint32_t w = LE_READ_4(sel);
1348 
1349 	switch (w) {
1350 	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
1351 		return RSN_ASE_8021X_UNSPEC;
1352 	case RSN_SEL(RSN_ASE_8021X_PSK):
1353 		return RSN_ASE_8021X_PSK;
1354 	case RSN_SEL(RSN_ASE_NONE):
1355 		return RSN_ASE_NONE;
1356 	}
1357 	return 0;		/* NB: so is discarded */
1358 #undef RSN_SEL
1359 }
1360 
1361 /*
1362  * Parse a WPA/RSN information element to collect parameters
1363  * and validate the parameters against what has been
1364  * configured for the system.
1365  */
1366 static int
1367 ieee80211_parse_rsn(struct ieee80211vap *vap, const uint8_t *frm,
1368 	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1369 {
1370 	uint8_t len = frm[1];
1371 	uint32_t w;
1372 	int n;
1373 
1374 	/*
1375 	 * Check the length once for fixed parts:
1376 	 * version, mcast cipher, and 2 selector counts.
1377 	 * Other, variable-length data, must be checked separately.
1378 	 */
1379 	if ((vap->iv_flags & IEEE80211_F_WPA2) == 0) {
1380 		IEEE80211_DISCARD_IE(vap,
1381 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1382 		    wh, "WPA", "not RSN, flags 0x%x", vap->iv_flags);
1383 		return IEEE80211_REASON_IE_INVALID;
1384 	}
1385 	if (len < 10) {
1386 		IEEE80211_DISCARD_IE(vap,
1387 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1388 		    wh, "RSN", "too short, len %u", len);
1389 		return IEEE80211_REASON_IE_INVALID;
1390 	}
1391 	frm += 2;
1392 	w = LE_READ_2(frm);
1393 	if (w != RSN_VERSION) {
1394 		IEEE80211_DISCARD_IE(vap,
1395 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1396 		    wh, "RSN", "bad version %u", w);
1397 		return IEEE80211_REASON_IE_INVALID;
1398 	}
1399 	frm += 2, len -= 2;
1400 
1401 	memset(rsn, 0, sizeof(*rsn));
1402 
1403 	/* multicast/group cipher */
1404 	rsn->rsn_mcastcipher = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
1405 	frm += 4, len -= 4;
1406 
1407 	/* unicast ciphers */
1408 	n = LE_READ_2(frm);
1409 	frm += 2, len -= 2;
1410 	if (len < n*4+2) {
1411 		IEEE80211_DISCARD_IE(vap,
1412 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1413 		    wh, "RSN", "ucast cipher data too short; len %u, n %u",
1414 		    len, n);
1415 		return IEEE80211_REASON_IE_INVALID;
1416 	}
1417 	w = 0;
1418 	for (; n > 0; n--) {
1419 		w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
1420 		frm += 4, len -= 4;
1421 	}
1422 	if (w & (1<<IEEE80211_CIPHER_TKIP))
1423 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1424 	else
1425 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1426 
1427 	/* key management algorithms */
1428 	n = LE_READ_2(frm);
1429 	frm += 2, len -= 2;
1430 	if (len < n*4) {
1431 		IEEE80211_DISCARD_IE(vap,
1432 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1433 		    wh, "RSN", "key mgmt alg data too short; len %u, n %u",
1434 		    len, n);
1435 		return IEEE80211_REASON_IE_INVALID;
1436 	}
1437 	w = 0;
1438 	for (; n > 0; n--) {
1439 		w |= rsn_keymgmt(frm);
1440 		frm += 4, len -= 4;
1441 	}
1442 	if (w & RSN_ASE_8021X_UNSPEC)
1443 		rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
1444 	else
1445 		rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
1446 
1447 	/* optional RSN capabilities */
1448 	if (len > 2)
1449 		rsn->rsn_caps = LE_READ_2(frm);
1450 	/* XXXPMKID */
1451 
1452 	return 0;
1453 }
1454 
1455 /*
1456  * WPA/802.11i assocation request processing.
1457  */
1458 static int
1459 wpa_assocreq(struct ieee80211_node *ni, struct ieee80211_rsnparms *rsnparms,
1460 	const struct ieee80211_frame *wh, const uint8_t *wpa,
1461 	const uint8_t *rsn, uint16_t capinfo)
1462 {
1463 	struct ieee80211vap *vap = ni->ni_vap;
1464 	uint8_t reason;
1465 	int badwparsn;
1466 
1467 	ni->ni_flags &= ~(IEEE80211_NODE_WPS|IEEE80211_NODE_TSN);
1468 	if (wpa == NULL && rsn == NULL) {
1469 		if (vap->iv_flags_ext & IEEE80211_FEXT_WPS) {
1470 			/*
1471 			 * W-Fi Protected Setup (WPS) permits
1472 			 * clients to associate and pass EAPOL frames
1473 			 * to establish initial credentials.
1474 			 */
1475 			ni->ni_flags |= IEEE80211_NODE_WPS;
1476 			return 1;
1477 		}
1478 		if ((vap->iv_flags_ext & IEEE80211_FEXT_TSN) &&
1479 		    (capinfo & IEEE80211_CAPINFO_PRIVACY)) {
1480 			/*
1481 			 * Transitional Security Network.  Permits clients
1482 			 * to associate and use WEP while WPA is configured.
1483 			 */
1484 			ni->ni_flags |= IEEE80211_NODE_TSN;
1485 			return 1;
1486 		}
1487 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1488 		    wh, NULL, "%s", "no WPA/RSN IE in association request");
1489 		vap->iv_stats.is_rx_assoc_badwpaie++;
1490 		reason = IEEE80211_REASON_IE_INVALID;
1491 		goto bad;
1492 	}
1493 	/* assert right association security credentials */
1494 	badwparsn = 0;			/* NB: to silence compiler */
1495 	switch (vap->iv_flags & IEEE80211_F_WPA) {
1496 	case IEEE80211_F_WPA1:
1497 		badwparsn = (wpa == NULL);
1498 		break;
1499 	case IEEE80211_F_WPA2:
1500 		badwparsn = (rsn == NULL);
1501 		break;
1502 	case IEEE80211_F_WPA1|IEEE80211_F_WPA2:
1503 		badwparsn = (wpa == NULL && rsn == NULL);
1504 		break;
1505 	}
1506 	if (badwparsn) {
1507 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1508 		    wh, NULL,
1509 		    "%s", "missing WPA/RSN IE in association request");
1510 		vap->iv_stats.is_rx_assoc_badwpaie++;
1511 		reason = IEEE80211_REASON_IE_INVALID;
1512 		goto bad;
1513 	}
1514 	/*
1515 	 * Parse WPA/RSN information element.
1516 	 */
1517 	if (wpa != NULL)
1518 		reason = ieee80211_parse_wpa(vap, wpa, rsnparms, wh);
1519 	else
1520 		reason = ieee80211_parse_rsn(vap, rsn, rsnparms, wh);
1521 	if (reason != 0) {
1522 		/* XXX distinguish WPA/RSN? */
1523 		vap->iv_stats.is_rx_assoc_badwpaie++;
1524 		goto bad;
1525 	}
1526 	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA, ni,
1527 	    "%s ie: mc %u/%u uc %u/%u key %u caps 0x%x",
1528 	    wpa != NULL ? "WPA" : "RSN",
1529 	    rsnparms->rsn_mcastcipher, rsnparms->rsn_mcastkeylen,
1530 	    rsnparms->rsn_ucastcipher, rsnparms->rsn_ucastkeylen,
1531 	    rsnparms->rsn_keymgmt, rsnparms->rsn_caps);
1532 
1533 	return 1;
1534 bad:
1535 	ieee80211_node_deauth(ni, reason);
1536 	return 0;
1537 }
1538 
1539 /* XXX find a better place for definition */
1540 struct l2_update_frame {
1541 	struct ether_header eh;
1542 	uint8_t dsap;
1543 	uint8_t ssap;
1544 	uint8_t control;
1545 	uint8_t xid[3];
1546 }  __packed;
1547 
1548 /*
1549  * Deliver a TGf L2UF frame on behalf of a station.
1550  * This primes any bridge when the station is roaming
1551  * between ap's on the same wired network.
1552  */
1553 static void
1554 ieee80211_deliver_l2uf(struct ieee80211_node *ni)
1555 {
1556 	struct ieee80211vap *vap = ni->ni_vap;
1557 	struct ifnet *ifp = vap->iv_ifp;
1558 	struct mbuf *m;
1559 	struct l2_update_frame *l2uf;
1560 	struct ether_header *eh;
1561 
1562 	m = m_gethdr(M_NOWAIT, MT_DATA);
1563 	if (m == NULL) {
1564 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1565 		    "%s", "no mbuf for l2uf frame");
1566 		vap->iv_stats.is_rx_nobuf++;	/* XXX not right */
1567 		return;
1568 	}
1569 	l2uf = mtod(m, struct l2_update_frame *);
1570 	eh = &l2uf->eh;
1571 	/* dst: Broadcast address */
1572 	IEEE80211_ADDR_COPY(eh->ether_dhost, ifp->if_broadcastaddr);
1573 	/* src: associated STA */
1574 	IEEE80211_ADDR_COPY(eh->ether_shost, ni->ni_macaddr);
1575 	eh->ether_type = htons(sizeof(*l2uf) - sizeof(*eh));
1576 
1577 	l2uf->dsap = 0;
1578 	l2uf->ssap = 0;
1579 	l2uf->control = 0xf5;
1580 	l2uf->xid[0] = 0x81;
1581 	l2uf->xid[1] = 0x80;
1582 	l2uf->xid[2] = 0x00;
1583 
1584 	m->m_pkthdr.len = m->m_len = sizeof(*l2uf);
1585 	hostap_deliver_data(vap, ni, m);
1586 }
1587 
1588 static void
1589 ratesetmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1590 	int reassoc, int resp, const char *tag, int rate)
1591 {
1592 	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1593 	    "deny %s request, %s rate set mismatch, rate/MCS %d",
1594 	    reassoc ? "reassoc" : "assoc", tag, rate & IEEE80211_RATE_VAL);
1595 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_BASIC_RATE);
1596 	ieee80211_node_leave(ni);
1597 }
1598 
1599 static void
1600 capinfomismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1601 	int reassoc, int resp, const char *tag, int capinfo)
1602 {
1603 	struct ieee80211vap *vap = ni->ni_vap;
1604 
1605 	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1606 	    "deny %s request, %s mismatch 0x%x",
1607 	    reassoc ? "reassoc" : "assoc", tag, capinfo);
1608 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_CAPINFO);
1609 	ieee80211_node_leave(ni);
1610 	vap->iv_stats.is_rx_assoc_capmismatch++;
1611 }
1612 
1613 static void
1614 htcapmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1615 	int reassoc, int resp)
1616 {
1617 	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1618 	    "deny %s request, %s missing HT ie", reassoc ? "reassoc" : "assoc");
1619 	/* XXX no better code */
1620 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_MISSING_HT_CAPS);
1621 	ieee80211_node_leave(ni);
1622 }
1623 
1624 static void
1625 authalgreject(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1626 	int algo, int seq, int status)
1627 {
1628 	struct ieee80211vap *vap = ni->ni_vap;
1629 
1630 	IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1631 	    wh, NULL, "unsupported alg %d", algo);
1632 	vap->iv_stats.is_rx_auth_unsupported++;
1633 	ieee80211_send_error(ni, wh->i_addr2, IEEE80211_FC0_SUBTYPE_AUTH,
1634 	    seq | (status << 16));
1635 }
1636 
1637 static __inline int
1638 ishtmixed(const uint8_t *ie)
1639 {
1640 	const struct ieee80211_ie_htinfo *ht =
1641 	    (const struct ieee80211_ie_htinfo *) ie;
1642 	return (ht->hi_byte2 & IEEE80211_HTINFO_OPMODE) ==
1643 	    IEEE80211_HTINFO_OPMODE_MIXED;
1644 }
1645 
1646 static int
1647 is11bclient(const uint8_t *rates, const uint8_t *xrates)
1648 {
1649 	static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
1650 	int i;
1651 
1652 	/* NB: the 11b clients we care about will not have xrates */
1653 	if (xrates != NULL || rates == NULL)
1654 		return 0;
1655 	for (i = 0; i < rates[1]; i++) {
1656 		int r = rates[2+i] & IEEE80211_RATE_VAL;
1657 		if (r > 2*11 || ((1<<r) & brates) == 0)
1658 			return 0;
1659 	}
1660 	return 1;
1661 }
1662 
1663 static void
1664 hostap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1665 	int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
1666 {
1667 	struct ieee80211vap *vap = ni->ni_vap;
1668 	struct ieee80211com *ic = ni->ni_ic;
1669 	struct ieee80211_frame *wh;
1670 	uint8_t *frm, *efrm, *sfrm;
1671 	uint8_t *ssid, *rates, *xrates, *wpa, *rsn, *wme, *ath, *htcap;
1672 	int reassoc, resp;
1673 	uint8_t rate;
1674 
1675 	wh = mtod(m0, struct ieee80211_frame *);
1676 	frm = (uint8_t *)&wh[1];
1677 	efrm = mtod(m0, uint8_t *) + m0->m_len;
1678 	switch (subtype) {
1679 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1680 	case IEEE80211_FC0_SUBTYPE_BEACON: {
1681 		struct ieee80211_scanparams scan;
1682 		/*
1683 		 * We process beacon/probe response frames when scanning;
1684 		 * otherwise we check beacon frames for overlapping non-ERP
1685 		 * BSS in 11g and/or overlapping legacy BSS when in HT.
1686 		 */
1687 		if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
1688 		    subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
1689 			vap->iv_stats.is_rx_mgtdiscard++;
1690 			return;
1691 		}
1692 		/* NB: accept off-channel frames */
1693 		/* XXX TODO: use rxstatus to determine off-channel details */
1694 		if (ieee80211_parse_beacon(ni, m0, ic->ic_curchan, &scan) &~ IEEE80211_BPARSE_OFFCHAN)
1695 			return;
1696 		/*
1697 		 * Count frame now that we know it's to be processed.
1698 		 */
1699 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1700 			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
1701 			IEEE80211_NODE_STAT(ni, rx_beacons);
1702 		} else
1703 			IEEE80211_NODE_STAT(ni, rx_proberesp);
1704 		/*
1705 		 * If scanning, just pass information to the scan module.
1706 		 */
1707 		if (ic->ic_flags & IEEE80211_F_SCAN) {
1708 			if (scan.status == 0 &&		/* NB: on channel */
1709 			    (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN)) {
1710 				/*
1711 				 * Actively scanning a channel marked passive;
1712 				 * send a probe request now that we know there
1713 				 * is 802.11 traffic present.
1714 				 *
1715 				 * XXX check if the beacon we recv'd gives
1716 				 * us what we need and suppress the probe req
1717 				 */
1718 				ieee80211_probe_curchan(vap, 1);
1719 				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1720 			}
1721 			ieee80211_add_scan(vap, ic->ic_curchan, &scan, wh,
1722 			    subtype, rssi, nf);
1723 			return;
1724 		}
1725 		/*
1726 		 * Check beacon for overlapping bss w/ non ERP stations.
1727 		 * If we detect one and protection is configured but not
1728 		 * enabled, enable it and start a timer that'll bring us
1729 		 * out if we stop seeing the bss.
1730 		 */
1731 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1732 		    scan.status == 0 &&			/* NB: on-channel */
1733 		    ((scan.erp & 0x100) == 0 ||		/* NB: no ERP, 11b sta*/
1734 		     (scan.erp & IEEE80211_ERP_NON_ERP_PRESENT))) {
1735 			ic->ic_lastnonerp = ticks;
1736 			ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
1737 			if (ic->ic_protmode != IEEE80211_PROT_NONE &&
1738 			    (ic->ic_flags & IEEE80211_F_USEPROT) == 0) {
1739 				IEEE80211_NOTE_FRAME(vap,
1740 				    IEEE80211_MSG_ASSOC, wh,
1741 				    "non-ERP present on channel %d "
1742 				    "(saw erp 0x%x from channel %d), "
1743 				    "enable use of protection",
1744 				    ic->ic_curchan->ic_ieee,
1745 				    scan.erp, scan.chan);
1746 				ic->ic_flags |= IEEE80211_F_USEPROT;
1747 				ieee80211_notify_erp(ic);
1748 			}
1749 		}
1750 		/*
1751 		 * Check beacon for non-HT station on HT channel
1752 		 * and update HT BSS occupancy as appropriate.
1753 		 */
1754 		if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) {
1755 			if (scan.status & IEEE80211_BPARSE_OFFCHAN) {
1756 				/*
1757 				 * Off control channel; only check frames
1758 				 * that come in the extension channel when
1759 				 * operating w/ HT40.
1760 				 */
1761 				if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
1762 					break;
1763 				if (scan.chan != ic->ic_curchan->ic_extieee)
1764 					break;
1765 			}
1766 			if (scan.htinfo == NULL) {
1767 				ieee80211_htprot_update(ic,
1768 				    IEEE80211_HTINFO_OPMODE_PROTOPT |
1769 				    IEEE80211_HTINFO_NONHT_PRESENT);
1770 			} else if (ishtmixed(scan.htinfo)) {
1771 				/* XXX? take NONHT_PRESENT from beacon? */
1772 				ieee80211_htprot_update(ic,
1773 				    IEEE80211_HTINFO_OPMODE_MIXED |
1774 				    IEEE80211_HTINFO_NONHT_PRESENT);
1775 			}
1776 		}
1777 		break;
1778 	}
1779 
1780 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1781 		if (vap->iv_state != IEEE80211_S_RUN) {
1782 			vap->iv_stats.is_rx_mgtdiscard++;
1783 			return;
1784 		}
1785 		/*
1786 		 * Consult the ACL policy module if setup.
1787 		 */
1788 		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1789 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1790 			    wh, NULL, "%s", "disallowed by ACL");
1791 			vap->iv_stats.is_rx_acl++;
1792 			return;
1793 		}
1794 		/*
1795 		 * prreq frame format
1796 		 *	[tlv] ssid
1797 		 *	[tlv] supported rates
1798 		 *	[tlv] extended supported rates
1799 		 */
1800 		ssid = rates = xrates = NULL;
1801 		sfrm = frm;
1802 		while (efrm - frm > 1) {
1803 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1804 			switch (*frm) {
1805 			case IEEE80211_ELEMID_SSID:
1806 				ssid = frm;
1807 				break;
1808 			case IEEE80211_ELEMID_RATES:
1809 				rates = frm;
1810 				break;
1811 			case IEEE80211_ELEMID_XRATES:
1812 				xrates = frm;
1813 				break;
1814 			}
1815 			frm += frm[1] + 2;
1816 		}
1817 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1818 		if (xrates != NULL)
1819 			IEEE80211_VERIFY_ELEMENT(xrates,
1820 				IEEE80211_RATE_MAXSIZE - rates[1], return);
1821 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1822 		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
1823 		if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
1824 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1825 			    wh, NULL,
1826 			    "%s", "no ssid with ssid suppression enabled");
1827 			vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
1828 			return;
1829 		}
1830 
1831 		/* XXX find a better class or define it's own */
1832 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1833 		    "%s", "recv probe req");
1834 		/*
1835 		 * Some legacy 11b clients cannot hack a complete
1836 		 * probe response frame.  When the request includes
1837 		 * only a bare-bones rate set, communicate this to
1838 		 * the transmit side.
1839 		 */
1840 		ieee80211_send_proberesp(vap, wh->i_addr2,
1841 		    is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
1842 		break;
1843 
1844 	case IEEE80211_FC0_SUBTYPE_AUTH: {
1845 		uint16_t algo, seq, status;
1846 
1847 		if (vap->iv_state != IEEE80211_S_RUN) {
1848 			vap->iv_stats.is_rx_mgtdiscard++;
1849 			return;
1850 		}
1851 		if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1852 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1853 			    wh, NULL, "%s", "wrong bssid");
1854 			vap->iv_stats.is_rx_wrongbss++;	/*XXX unique stat?*/
1855 			return;
1856 		}
1857 		/*
1858 		 * auth frame format
1859 		 *	[2] algorithm
1860 		 *	[2] sequence
1861 		 *	[2] status
1862 		 *	[tlv*] challenge
1863 		 */
1864 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1865 		algo   = le16toh(*(uint16_t *)frm);
1866 		seq    = le16toh(*(uint16_t *)(frm + 2));
1867 		status = le16toh(*(uint16_t *)(frm + 4));
1868 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1869 		    "recv auth frame with algorithm %d seq %d", algo, seq);
1870 		/*
1871 		 * Consult the ACL policy module if setup.
1872 		 */
1873 		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1874 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1875 			    wh, NULL, "%s", "disallowed by ACL");
1876 			vap->iv_stats.is_rx_acl++;
1877 			ieee80211_send_error(ni, wh->i_addr2,
1878 			    IEEE80211_FC0_SUBTYPE_AUTH,
1879 			    (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
1880 			return;
1881 		}
1882 		if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1883 			IEEE80211_DISCARD(vap,
1884 			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1885 			    wh, NULL, "%s", "TKIP countermeasures enabled");
1886 			vap->iv_stats.is_rx_auth_countermeasures++;
1887 			ieee80211_send_error(ni, wh->i_addr2,
1888 				IEEE80211_FC0_SUBTYPE_AUTH,
1889 				IEEE80211_REASON_MIC_FAILURE);
1890 			return;
1891 		}
1892 		if (algo == IEEE80211_AUTH_ALG_SHARED)
1893 			hostap_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1894 			    seq, status);
1895 		else if (algo == IEEE80211_AUTH_ALG_OPEN)
1896 			hostap_auth_open(ni, wh, rssi, nf, seq, status);
1897 		else if (algo == IEEE80211_AUTH_ALG_LEAP) {
1898 			authalgreject(ni, wh, algo,
1899 			    seq+1, IEEE80211_STATUS_ALG);
1900 			return;
1901 		} else {
1902 			/*
1903 			 * We assume that an unknown algorithm is the result
1904 			 * of a decryption failure on a shared key auth frame;
1905 			 * return a status code appropriate for that instead
1906 			 * of IEEE80211_STATUS_ALG.
1907 			 *
1908 			 * NB: a seq# of 4 is intentional; the decrypted
1909 			 *     frame likely has a bogus seq value.
1910 			 */
1911 			authalgreject(ni, wh, algo,
1912 			    4, IEEE80211_STATUS_CHALLENGE);
1913 			return;
1914 		}
1915 		break;
1916 	}
1917 
1918 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1919 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
1920 		uint16_t capinfo, lintval;
1921 		struct ieee80211_rsnparms rsnparms;
1922 
1923 		if (vap->iv_state != IEEE80211_S_RUN) {
1924 			vap->iv_stats.is_rx_mgtdiscard++;
1925 			return;
1926 		}
1927 		if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1928 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1929 			    wh, NULL, "%s", "wrong bssid");
1930 			vap->iv_stats.is_rx_assoc_bss++;
1931 			return;
1932 		}
1933 		if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1934 			reassoc = 1;
1935 			resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
1936 		} else {
1937 			reassoc = 0;
1938 			resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
1939 		}
1940 		if (ni == vap->iv_bss) {
1941 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1942 			    "deny %s request, sta not authenticated",
1943 			    reassoc ? "reassoc" : "assoc");
1944 			ieee80211_send_error(ni, wh->i_addr2,
1945 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
1946 			    IEEE80211_REASON_ASSOC_NOT_AUTHED);
1947 			vap->iv_stats.is_rx_assoc_notauth++;
1948 			return;
1949 		}
1950 
1951 		/*
1952 		 * asreq frame format
1953 		 *	[2] capability information
1954 		 *	[2] listen interval
1955 		 *	[6*] current AP address (reassoc only)
1956 		 *	[tlv] ssid
1957 		 *	[tlv] supported rates
1958 		 *	[tlv] extended supported rates
1959 		 *	[tlv] WPA or RSN
1960 		 *	[tlv] HT capabilities
1961 		 *	[tlv] Atheros capabilities
1962 		 */
1963 		IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4), return);
1964 		capinfo = le16toh(*(uint16_t *)frm);	frm += 2;
1965 		lintval = le16toh(*(uint16_t *)frm);	frm += 2;
1966 		if (reassoc)
1967 			frm += 6;	/* ignore current AP info */
1968 		ssid = rates = xrates = wpa = rsn = wme = ath = htcap = NULL;
1969 		sfrm = frm;
1970 		while (efrm - frm > 1) {
1971 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1972 			switch (*frm) {
1973 			case IEEE80211_ELEMID_SSID:
1974 				ssid = frm;
1975 				break;
1976 			case IEEE80211_ELEMID_RATES:
1977 				rates = frm;
1978 				break;
1979 			case IEEE80211_ELEMID_XRATES:
1980 				xrates = frm;
1981 				break;
1982 			case IEEE80211_ELEMID_RSN:
1983 				rsn = frm;
1984 				break;
1985 			case IEEE80211_ELEMID_HTCAP:
1986 				htcap = frm;
1987 				break;
1988 			case IEEE80211_ELEMID_VENDOR:
1989 				if (iswpaoui(frm))
1990 					wpa = frm;
1991 				else if (iswmeinfo(frm))
1992 					wme = frm;
1993 #ifdef IEEE80211_SUPPORT_SUPERG
1994 				else if (isatherosoui(frm))
1995 					ath = frm;
1996 #endif
1997 				else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1998 					if (ishtcapoui(frm) && htcap == NULL)
1999 						htcap = frm;
2000 				}
2001 				break;
2002 			}
2003 			frm += frm[1] + 2;
2004 		}
2005 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
2006 		if (xrates != NULL)
2007 			IEEE80211_VERIFY_ELEMENT(xrates,
2008 				IEEE80211_RATE_MAXSIZE - rates[1], return);
2009 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2010 		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
2011 		if (htcap != NULL) {
2012 			IEEE80211_VERIFY_LENGTH(htcap[1],
2013 			     htcap[0] == IEEE80211_ELEMID_VENDOR ?
2014 			         4 + sizeof(struct ieee80211_ie_htcap)-2 :
2015 			         sizeof(struct ieee80211_ie_htcap)-2,
2016 			     return);		/* XXX just NULL out? */
2017 		}
2018 
2019 		if ((vap->iv_flags & IEEE80211_F_WPA) &&
2020 		    !wpa_assocreq(ni, &rsnparms, wh, wpa, rsn, capinfo))
2021 			return;
2022 		/* discard challenge after association */
2023 		if (ni->ni_challenge != NULL) {
2024 			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
2025 			ni->ni_challenge = NULL;
2026 		}
2027 		/* NB: 802.11 spec says to ignore station's privacy bit */
2028 		if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
2029 			capinfomismatch(ni, wh, reassoc, resp,
2030 			    "capability", capinfo);
2031 			return;
2032 		}
2033 		/*
2034 		 * Disallow re-associate w/ invalid slot time setting.
2035 		 */
2036 		if (ni->ni_associd != 0 &&
2037 		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2038 		    ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
2039 			capinfomismatch(ni, wh, reassoc, resp,
2040 			    "slot time", capinfo);
2041 			return;
2042 		}
2043 		rate = ieee80211_setup_rates(ni, rates, xrates,
2044 				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
2045 				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2046 		if (rate & IEEE80211_RATE_BASIC) {
2047 			ratesetmismatch(ni, wh, reassoc, resp, "legacy", rate);
2048 			vap->iv_stats.is_rx_assoc_norate++;
2049 			return;
2050 		}
2051 		/*
2052 		 * If constrained to 11g-only stations reject an
2053 		 * 11b-only station.  We cheat a bit here by looking
2054 		 * at the max negotiated xmit rate and assuming anyone
2055 		 * with a best rate <24Mb/s is an 11b station.
2056 		 */
2057 		if ((vap->iv_flags & IEEE80211_F_PUREG) && rate < 48) {
2058 			ratesetmismatch(ni, wh, reassoc, resp, "11g", rate);
2059 			vap->iv_stats.is_rx_assoc_norate++;
2060 			return;
2061 		}
2062 		/*
2063 		 * Do HT rate set handling and setup HT node state.
2064 		 */
2065 		ni->ni_chan = vap->iv_bss->ni_chan;
2066 		if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
2067 			rate = ieee80211_setup_htrates(ni, htcap,
2068 				IEEE80211_F_DOFMCS | IEEE80211_F_DONEGO |
2069 				IEEE80211_F_DOBRS);
2070 			if (rate & IEEE80211_RATE_BASIC) {
2071 				ratesetmismatch(ni, wh, reassoc, resp,
2072 				    "HT", rate);
2073 				vap->iv_stats.is_ht_assoc_norate++;
2074 				return;
2075 			}
2076 			ieee80211_ht_node_init(ni);
2077 			ieee80211_ht_updatehtcap(ni, htcap);
2078 		} else if (ni->ni_flags & IEEE80211_NODE_HT)
2079 			ieee80211_ht_node_cleanup(ni);
2080 #ifdef IEEE80211_SUPPORT_SUPERG
2081 		/* Always do ff node cleanup; for A-MSDU */
2082 		ieee80211_ff_node_cleanup(ni);
2083 #endif
2084 		/*
2085 		 * Allow AMPDU operation only with unencrypted traffic
2086 		 * or AES-CCM; the 11n spec only specifies these ciphers
2087 		 * so permitting any others is undefined and can lead
2088 		 * to interoperability problems.
2089 		 */
2090 		if ((ni->ni_flags & IEEE80211_NODE_HT) &&
2091 		    (((vap->iv_flags & IEEE80211_F_WPA) &&
2092 		      rsnparms.rsn_ucastcipher != IEEE80211_CIPHER_AES_CCM) ||
2093 		     (vap->iv_flags & (IEEE80211_F_WPA|IEEE80211_F_PRIVACY)) == IEEE80211_F_PRIVACY)) {
2094 			IEEE80211_NOTE(vap,
2095 			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
2096 			    "disallow HT use because WEP or TKIP requested, "
2097 			    "capinfo 0x%x ucastcipher %d", capinfo,
2098 			    rsnparms.rsn_ucastcipher);
2099 			ieee80211_ht_node_cleanup(ni);
2100 #ifdef IEEE80211_SUPPORT_SUPERG
2101 			/* Always do ff node cleanup; for A-MSDU */
2102 			ieee80211_ff_node_cleanup(ni);
2103 #endif
2104 			vap->iv_stats.is_ht_assoc_downgrade++;
2105 		}
2106 		/*
2107 		 * If constrained to 11n-only stations reject legacy stations.
2108 		 */
2109 		if ((vap->iv_flags_ht & IEEE80211_FHT_PUREN) &&
2110 		    (ni->ni_flags & IEEE80211_NODE_HT) == 0) {
2111 			htcapmismatch(ni, wh, reassoc, resp);
2112 			vap->iv_stats.is_ht_assoc_nohtcap++;
2113 			return;
2114 		}
2115 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
2116 		ni->ni_noise = nf;
2117 		ni->ni_intval = lintval;
2118 		ni->ni_capinfo = capinfo;
2119 		ni->ni_fhdwell = vap->iv_bss->ni_fhdwell;
2120 		ni->ni_fhindex = vap->iv_bss->ni_fhindex;
2121 		/*
2122 		 * Store the IEs.
2123 		 * XXX maybe better to just expand
2124 		 */
2125 		if (ieee80211_ies_init(&ni->ni_ies, sfrm, efrm - sfrm)) {
2126 #define	setie(_ie, _off)	ieee80211_ies_setie(ni->ni_ies, _ie, _off)
2127 			if (wpa != NULL)
2128 				setie(wpa_ie, wpa - sfrm);
2129 			if (rsn != NULL)
2130 				setie(rsn_ie, rsn - sfrm);
2131 			if (htcap != NULL)
2132 				setie(htcap_ie, htcap - sfrm);
2133 			if (wme != NULL) {
2134 				setie(wme_ie, wme - sfrm);
2135 				/*
2136 				 * Mark node as capable of QoS.
2137 				 */
2138 				ni->ni_flags |= IEEE80211_NODE_QOS;
2139 			} else
2140 				ni->ni_flags &= ~IEEE80211_NODE_QOS;
2141 #ifdef IEEE80211_SUPPORT_SUPERG
2142 			if (ath != NULL) {
2143 				setie(ath_ie, ath - sfrm);
2144 				/*
2145 				 * Parse ATH station parameters.
2146 				 */
2147 				ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
2148 			} else
2149 #endif
2150 				ni->ni_ath_flags = 0;
2151 #undef setie
2152 		} else {
2153 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
2154 			ni->ni_ath_flags = 0;
2155 		}
2156 		ieee80211_node_join(ni, resp);
2157 		ieee80211_deliver_l2uf(ni);
2158 		break;
2159 	}
2160 
2161 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
2162 	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
2163 		uint16_t reason;
2164 
2165 		if (vap->iv_state != IEEE80211_S_RUN ||
2166 		    /* NB: can happen when in promiscuous mode */
2167 		    !IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
2168 			vap->iv_stats.is_rx_mgtdiscard++;
2169 			break;
2170 		}
2171 		/*
2172 		 * deauth/disassoc frame format
2173 		 *	[2] reason
2174 		 */
2175 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
2176 		reason = le16toh(*(uint16_t *)frm);
2177 		if (subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
2178 			vap->iv_stats.is_rx_deauth++;
2179 			IEEE80211_NODE_STAT(ni, rx_deauth);
2180 		} else {
2181 			vap->iv_stats.is_rx_disassoc++;
2182 			IEEE80211_NODE_STAT(ni, rx_disassoc);
2183 		}
2184 		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2185 		    "recv %s (reason %d)", ieee80211_mgt_subtype_name[subtype >>
2186 			IEEE80211_FC0_SUBTYPE_SHIFT], reason);
2187 		if (ni != vap->iv_bss)
2188 			ieee80211_node_leave(ni);
2189 		break;
2190 	}
2191 
2192 	case IEEE80211_FC0_SUBTYPE_ACTION:
2193 	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2194 		if (ni == vap->iv_bss) {
2195 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2196 			    wh, NULL, "%s", "unknown node");
2197 			vap->iv_stats.is_rx_mgtdiscard++;
2198 		} else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2199 		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2200 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2201 			    wh, NULL, "%s", "not for us");
2202 			vap->iv_stats.is_rx_mgtdiscard++;
2203 		} else if (vap->iv_state != IEEE80211_S_RUN) {
2204 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2205 			    wh, NULL, "wrong state %s",
2206 			    ieee80211_state_name[vap->iv_state]);
2207 			vap->iv_stats.is_rx_mgtdiscard++;
2208 		} else {
2209 			if (ieee80211_parse_action(ni, m0) == 0)
2210 				(void)ic->ic_recv_action(ni, wh, frm, efrm);
2211 		}
2212 		break;
2213 
2214 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2215 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2216 	case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
2217 	case IEEE80211_FC0_SUBTYPE_ATIM:
2218 		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2219 		    wh, NULL, "%s", "not handled");
2220 		vap->iv_stats.is_rx_mgtdiscard++;
2221 		break;
2222 
2223 	default:
2224 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2225 		    wh, "mgt", "subtype 0x%x not handled", subtype);
2226 		vap->iv_stats.is_rx_badsubtype++;
2227 		break;
2228 	}
2229 }
2230 
2231 static void
2232 hostap_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2233 {
2234 	switch (subtype) {
2235 	case IEEE80211_FC0_SUBTYPE_PS_POLL:
2236 		ni->ni_vap->iv_recv_pspoll(ni, m);
2237 		break;
2238 	case IEEE80211_FC0_SUBTYPE_BAR:
2239 		ieee80211_recv_bar(ni, m);
2240 		break;
2241 	}
2242 }
2243 
2244 /*
2245  * Process a received ps-poll frame.
2246  */
2247 void
2248 ieee80211_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m0)
2249 {
2250 	struct ieee80211vap *vap = ni->ni_vap;
2251 	struct ieee80211com *ic = vap->iv_ic;
2252 	struct ieee80211_frame_min *wh;
2253 	struct mbuf *m;
2254 	uint16_t aid;
2255 	int qlen;
2256 
2257 	wh = mtod(m0, struct ieee80211_frame_min *);
2258 	if (ni->ni_associd == 0) {
2259 		IEEE80211_DISCARD(vap,
2260 		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2261 		    (struct ieee80211_frame *) wh, NULL,
2262 		    "%s", "unassociated station");
2263 		vap->iv_stats.is_ps_unassoc++;
2264 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
2265 			IEEE80211_REASON_NOT_ASSOCED);
2266 		return;
2267 	}
2268 
2269 	aid = le16toh(*(uint16_t *)wh->i_dur);
2270 	if (aid != ni->ni_associd) {
2271 		IEEE80211_DISCARD(vap,
2272 		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2273 		    (struct ieee80211_frame *) wh, NULL,
2274 		    "aid mismatch: sta aid 0x%x poll aid 0x%x",
2275 		    ni->ni_associd, aid);
2276 		vap->iv_stats.is_ps_badaid++;
2277 		/*
2278 		 * NB: We used to deauth the station but it turns out
2279 		 * the Blackberry Curve 8230 (and perhaps other devices)
2280 		 * sometimes send the wrong AID when WME is negotiated.
2281 		 * Being more lenient here seems ok as we already check
2282 		 * the station is associated and we only return frames
2283 		 * queued for the station (i.e. we don't use the AID).
2284 		 */
2285 		return;
2286 	}
2287 
2288 	/* Okay, take the first queued packet and put it out... */
2289 	m = ieee80211_node_psq_dequeue(ni, &qlen);
2290 	if (m == NULL) {
2291 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_POWER, wh->i_addr2,
2292 		    "%s", "recv ps-poll, but queue empty");
2293 		ieee80211_send_nulldata(ieee80211_ref_node(ni));
2294 		vap->iv_stats.is_ps_qempty++;	/* XXX node stat */
2295 		if (vap->iv_set_tim != NULL)
2296 			vap->iv_set_tim(ni, 0);	/* just in case */
2297 		return;
2298 	}
2299 	/*
2300 	 * If there are more packets, set the more packets bit
2301 	 * in the packet dispatched to the station; otherwise
2302 	 * turn off the TIM bit.
2303 	 */
2304 	if (qlen != 0) {
2305 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2306 		    "recv ps-poll, send packet, %u still queued", qlen);
2307 		m->m_flags |= M_MORE_DATA;
2308 	} else {
2309 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2310 		    "%s", "recv ps-poll, send packet, queue empty");
2311 		if (vap->iv_set_tim != NULL)
2312 			vap->iv_set_tim(ni, 0);
2313 	}
2314 	m->m_flags |= M_PWR_SAV;		/* bypass PS handling */
2315 
2316 	/*
2317 	 * Do the right thing; if it's an encap'ed frame then
2318 	 * call ieee80211_parent_xmitpkt() else
2319 	 * call ieee80211_vap_xmitpkt().
2320 	 */
2321 	if (m->m_flags & M_ENCAP) {
2322 		(void) ieee80211_parent_xmitpkt(ic, m);
2323 	} else {
2324 		(void) ieee80211_vap_xmitpkt(vap, m);
2325 	}
2326 }
2327