xref: /freebsd/sys/net80211/ieee80211_hostap.c (revision ef0cb5db0af0d5d5b75b74f8e534fe601b7176d7)
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 	uint16_t rxseq;
489 
490 	if (m->m_flags & M_AMPDU_MPDU) {
491 		/*
492 		 * Fastpath for A-MPDU reorder q resubmission.  Frames
493 		 * w/ M_AMPDU_MPDU marked have already passed through
494 		 * here but were received out of order and been held on
495 		 * the reorder queue.  When resubmitted they are marked
496 		 * with the M_AMPDU_MPDU flag and we can bypass most of
497 		 * the normal processing.
498 		 */
499 		wh = mtod(m, struct ieee80211_frame *);
500 		type = IEEE80211_FC0_TYPE_DATA;
501 		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
502 		subtype = IEEE80211_FC0_SUBTYPE_QOS;
503 		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
504 		goto resubmit_ampdu;
505 	}
506 
507 	KASSERT(ni != NULL, ("null node"));
508 	ni->ni_inact = ni->ni_inact_reload;
509 
510 	type = -1;			/* undefined */
511 
512 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
513 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
514 		    ni->ni_macaddr, NULL,
515 		    "too short (1): len %u", m->m_pkthdr.len);
516 		vap->iv_stats.is_rx_tooshort++;
517 		goto out;
518 	}
519 	/*
520 	 * Bit of a cheat here, we use a pointer for a 3-address
521 	 * frame format but don't reference fields past outside
522 	 * ieee80211_frame_min w/o first validating the data is
523 	 * present.
524 	 */
525 	wh = mtod(m, struct ieee80211_frame *);
526 
527 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
528 	    IEEE80211_FC0_VERSION_0) {
529 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
530 		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
531 		    wh->i_fc[0], wh->i_fc[1]);
532 		vap->iv_stats.is_rx_badversion++;
533 		goto err;
534 	}
535 
536 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
537 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
538 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
539 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
540 		if (dir != IEEE80211_FC1_DIR_NODS)
541 			bssid = wh->i_addr1;
542 		else if (type == IEEE80211_FC0_TYPE_CTL)
543 			bssid = wh->i_addr1;
544 		else {
545 			if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
546 				IEEE80211_DISCARD_MAC(vap,
547 				    IEEE80211_MSG_ANY, ni->ni_macaddr,
548 				    NULL, "too short (2): len %u",
549 				    m->m_pkthdr.len);
550 				vap->iv_stats.is_rx_tooshort++;
551 				goto out;
552 			}
553 			bssid = wh->i_addr3;
554 		}
555 		/*
556 		 * Validate the bssid.
557 		 */
558 		if (!(type == IEEE80211_FC0_TYPE_MGT &&
559 		      subtype == IEEE80211_FC0_SUBTYPE_BEACON) &&
560 		    !IEEE80211_ADDR_EQ(bssid, vap->iv_bss->ni_bssid) &&
561 		    !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
562 			/* not interested in */
563 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
564 			    bssid, NULL, "%s", "not to bss");
565 			vap->iv_stats.is_rx_wrongbss++;
566 			goto out;
567 		}
568 
569 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
570 		ni->ni_noise = nf;
571 		if (IEEE80211_HAS_SEQ(type, subtype)) {
572 			uint8_t tid = ieee80211_gettid(wh);
573 			if (IEEE80211_QOS_HAS_SEQ(wh) &&
574 			    TID_TO_WME_AC(tid) >= WME_AC_VI)
575 				ic->ic_wme.wme_hipri_traffic++;
576 			rxseq = le16toh(*(uint16_t *)wh->i_seq);
577 			if (! ieee80211_check_rxseq(ni, wh)) {
578 				/* duplicate, discard */
579 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
580 				    bssid, "duplicate",
581 				    "seqno <%u,%u> fragno <%u,%u> tid %u",
582 				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
583 				    ni->ni_rxseqs[tid] >>
584 					IEEE80211_SEQ_SEQ_SHIFT,
585 				    rxseq & IEEE80211_SEQ_FRAG_MASK,
586 				    ni->ni_rxseqs[tid] &
587 					IEEE80211_SEQ_FRAG_MASK,
588 				    tid);
589 				vap->iv_stats.is_rx_dup++;
590 				IEEE80211_NODE_STAT(ni, rx_dup);
591 				goto out;
592 			}
593 			ni->ni_rxseqs[tid] = rxseq;
594 		}
595 	}
596 
597 	switch (type) {
598 	case IEEE80211_FC0_TYPE_DATA:
599 		hdrspace = ieee80211_hdrspace(ic, wh);
600 		if (m->m_len < hdrspace &&
601 		    (m = m_pullup(m, hdrspace)) == NULL) {
602 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
603 			    ni->ni_macaddr, NULL,
604 			    "data too short: expecting %u", hdrspace);
605 			vap->iv_stats.is_rx_tooshort++;
606 			goto out;		/* XXX */
607 		}
608 		if (!(dir == IEEE80211_FC1_DIR_TODS ||
609 		     (dir == IEEE80211_FC1_DIR_DSTODS &&
610 		      (vap->iv_flags & IEEE80211_F_DWDS)))) {
611 			if (dir != IEEE80211_FC1_DIR_DSTODS) {
612 				IEEE80211_DISCARD(vap,
613 				    IEEE80211_MSG_INPUT, wh, "data",
614 				    "incorrect dir 0x%x", dir);
615 			} else {
616 				IEEE80211_DISCARD(vap,
617 				    IEEE80211_MSG_INPUT |
618 				    IEEE80211_MSG_WDS, wh,
619 				    "4-address data",
620 				    "%s", "DWDS not enabled");
621 			}
622 			vap->iv_stats.is_rx_wrongdir++;
623 			goto out;
624 		}
625 		/* check if source STA is associated */
626 		if (ni == vap->iv_bss) {
627 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
628 			    wh, "data", "%s", "unknown src");
629 			ieee80211_send_error(ni, wh->i_addr2,
630 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
631 			    IEEE80211_REASON_NOT_AUTHED);
632 			vap->iv_stats.is_rx_notassoc++;
633 			goto err;
634 		}
635 		if (ni->ni_associd == 0) {
636 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
637 			    wh, "data", "%s", "unassoc src");
638 			IEEE80211_SEND_MGMT(ni,
639 			    IEEE80211_FC0_SUBTYPE_DISASSOC,
640 			    IEEE80211_REASON_NOT_ASSOCED);
641 			vap->iv_stats.is_rx_notassoc++;
642 			goto err;
643 		}
644 
645 		/*
646 		 * Check for power save state change.
647 		 * XXX out-of-order A-MPDU frames?
648 		 */
649 		if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
650 		    (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
651 			vap->iv_node_ps(ni,
652 				wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
653 		/*
654 		 * For 4-address packets handle WDS discovery
655 		 * notifications.  Once a WDS link is setup frames
656 		 * are just delivered to the WDS vap (see below).
657 		 */
658 		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap == NULL) {
659 			if (!ieee80211_node_is_authorized(ni)) {
660 				IEEE80211_DISCARD(vap,
661 				    IEEE80211_MSG_INPUT |
662 				    IEEE80211_MSG_WDS, wh,
663 				    "4-address data",
664 				    "%s", "unauthorized port");
665 				vap->iv_stats.is_rx_unauth++;
666 				IEEE80211_NODE_STAT(ni, rx_unauth);
667 				goto err;
668 			}
669 			ieee80211_dwds_discover(ni, m);
670 			return type;
671 		}
672 
673 		/*
674 		 * Handle A-MPDU re-ordering.  If the frame is to be
675 		 * processed directly then ieee80211_ampdu_reorder
676 		 * will return 0; otherwise it has consumed the mbuf
677 		 * and we should do nothing more with it.
678 		 */
679 		if ((m->m_flags & M_AMPDU) &&
680 		    ieee80211_ampdu_reorder(ni, m) != 0) {
681 			m = NULL;
682 			goto out;
683 		}
684 	resubmit_ampdu:
685 
686 		/*
687 		 * Handle privacy requirements.  Note that we
688 		 * must not be preempted from here until after
689 		 * we (potentially) call ieee80211_crypto_demic;
690 		 * otherwise we may violate assumptions in the
691 		 * crypto cipher modules used to do delayed update
692 		 * of replay sequence numbers.
693 		 */
694 		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
695 			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
696 				/*
697 				 * Discard encrypted frames when privacy is off.
698 				 */
699 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
700 				    wh, "WEP", "%s", "PRIVACY off");
701 				vap->iv_stats.is_rx_noprivacy++;
702 				IEEE80211_NODE_STAT(ni, rx_noprivacy);
703 				goto out;
704 			}
705 			key = ieee80211_crypto_decap(ni, m, hdrspace);
706 			if (key == NULL) {
707 				/* NB: stats+msgs handled in crypto_decap */
708 				IEEE80211_NODE_STAT(ni, rx_wepfail);
709 				goto out;
710 			}
711 			wh = mtod(m, struct ieee80211_frame *);
712 			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
713 		} else {
714 			/* XXX M_WEP and IEEE80211_F_PRIVACY */
715 			key = NULL;
716 		}
717 
718 		/*
719 		 * Save QoS bits for use below--before we strip the header.
720 		 */
721 		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
722 			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
723 			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
724 			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
725 		} else
726 			qos = 0;
727 
728 		/*
729 		 * Next up, any fragmentation.
730 		 */
731 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
732 			m = ieee80211_defrag(ni, m, hdrspace);
733 			if (m == NULL) {
734 				/* Fragment dropped or frame not complete yet */
735 				goto out;
736 			}
737 		}
738 		wh = NULL;		/* no longer valid, catch any uses */
739 
740 		/*
741 		 * Next strip any MSDU crypto bits.
742 		 */
743 		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
744 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
745 			    ni->ni_macaddr, "data", "%s", "demic error");
746 			vap->iv_stats.is_rx_demicfail++;
747 			IEEE80211_NODE_STAT(ni, rx_demicfail);
748 			goto out;
749 		}
750 		/* copy to listener after decrypt */
751 		if (ieee80211_radiotap_active_vap(vap))
752 			ieee80211_radiotap_rx(vap, m);
753 		need_tap = 0;
754 		/*
755 		 * Finally, strip the 802.11 header.
756 		 */
757 		m = ieee80211_decap(vap, m, hdrspace);
758 		if (m == NULL) {
759 			/* XXX mask bit to check for both */
760 			/* don't count Null data frames as errors */
761 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
762 			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
763 				goto out;
764 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
765 			    ni->ni_macaddr, "data", "%s", "decap error");
766 			vap->iv_stats.is_rx_decap++;
767 			IEEE80211_NODE_STAT(ni, rx_decap);
768 			goto err;
769 		}
770 		eh = mtod(m, struct ether_header *);
771 		if (!ieee80211_node_is_authorized(ni)) {
772 			/*
773 			 * Deny any non-PAE frames received prior to
774 			 * authorization.  For open/shared-key
775 			 * authentication the port is mark authorized
776 			 * after authentication completes.  For 802.1x
777 			 * the port is not marked authorized by the
778 			 * authenticator until the handshake has completed.
779 			 */
780 			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
781 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
782 				    eh->ether_shost, "data",
783 				    "unauthorized port: ether type 0x%x len %u",
784 				    eh->ether_type, m->m_pkthdr.len);
785 				vap->iv_stats.is_rx_unauth++;
786 				IEEE80211_NODE_STAT(ni, rx_unauth);
787 				goto err;
788 			}
789 		} else {
790 			/*
791 			 * When denying unencrypted frames, discard
792 			 * any non-PAE frames received without encryption.
793 			 */
794 			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
795 			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
796 			    eh->ether_type != htons(ETHERTYPE_PAE)) {
797 				/*
798 				 * Drop unencrypted frames.
799 				 */
800 				vap->iv_stats.is_rx_unencrypted++;
801 				IEEE80211_NODE_STAT(ni, rx_unencrypted);
802 				goto out;
803 			}
804 		}
805 		/* XXX require HT? */
806 		if (qos & IEEE80211_QOS_AMSDU) {
807 			m = ieee80211_decap_amsdu(ni, m);
808 			if (m == NULL)
809 				return IEEE80211_FC0_TYPE_DATA;
810 		} else {
811 #ifdef IEEE80211_SUPPORT_SUPERG
812 			m = ieee80211_decap_fastframe(vap, ni, m);
813 			if (m == NULL)
814 				return IEEE80211_FC0_TYPE_DATA;
815 #endif
816 		}
817 		if (dir == IEEE80211_FC1_DIR_DSTODS && ni->ni_wdsvap != NULL)
818 			ieee80211_deliver_data(ni->ni_wdsvap, ni, m);
819 		else
820 			hostap_deliver_data(vap, ni, m);
821 		return IEEE80211_FC0_TYPE_DATA;
822 
823 	case IEEE80211_FC0_TYPE_MGT:
824 		vap->iv_stats.is_rx_mgmt++;
825 		IEEE80211_NODE_STAT(ni, rx_mgmt);
826 		if (dir != IEEE80211_FC1_DIR_NODS) {
827 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
828 			    wh, "mgt", "incorrect dir 0x%x", dir);
829 			vap->iv_stats.is_rx_wrongdir++;
830 			goto err;
831 		}
832 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
833 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
834 			    ni->ni_macaddr, "mgt", "too short: len %u",
835 			    m->m_pkthdr.len);
836 			vap->iv_stats.is_rx_tooshort++;
837 			goto out;
838 		}
839 		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
840 			/* ensure return frames are unicast */
841 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
842 			    wh, NULL, "source is multicast: %s",
843 			    ether_sprintf(wh->i_addr2));
844 			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
845 			goto out;
846 		}
847 #ifdef IEEE80211_DEBUG
848 		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
849 		    ieee80211_msg_dumppkts(vap)) {
850 			if_printf(ifp, "received %s from %s rssi %d\n",
851 			    ieee80211_mgt_subtype_name[subtype >>
852 				IEEE80211_FC0_SUBTYPE_SHIFT],
853 			    ether_sprintf(wh->i_addr2), rssi);
854 		}
855 #endif
856 		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
857 			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
858 				/*
859 				 * Only shared key auth frames with a challenge
860 				 * should be encrypted, discard all others.
861 				 */
862 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
863 				    wh, NULL,
864 				    "%s", "WEP set but not permitted");
865 				vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
866 				goto out;
867 			}
868 			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
869 				/*
870 				 * Discard encrypted frames when privacy is off.
871 				 */
872 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
873 				    wh, NULL, "%s", "WEP set but PRIVACY off");
874 				vap->iv_stats.is_rx_noprivacy++;
875 				goto out;
876 			}
877 			hdrspace = ieee80211_hdrspace(ic, wh);
878 			key = ieee80211_crypto_decap(ni, m, hdrspace);
879 			if (key == NULL) {
880 				/* NB: stats+msgs handled in crypto_decap */
881 				goto out;
882 			}
883 			wh = mtod(m, struct ieee80211_frame *);
884 			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
885 		}
886 		/*
887 		 * Pass the packet to radiotap before calling iv_recv_mgmt().
888 		 * Otherwise iv_recv_mgmt() might pass another packet to
889 		 * radiotap, resulting in out of order packet captures.
890 		 */
891 		if (ieee80211_radiotap_active_vap(vap))
892 			ieee80211_radiotap_rx(vap, m);
893 		need_tap = 0;
894 		vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
895 		goto out;
896 
897 	case IEEE80211_FC0_TYPE_CTL:
898 		vap->iv_stats.is_rx_ctl++;
899 		IEEE80211_NODE_STAT(ni, rx_ctrl);
900 		vap->iv_recv_ctl(ni, m, subtype);
901 		goto out;
902 	default:
903 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
904 		    wh, "bad", "frame type 0x%x", type);
905 		/* should not come here */
906 		break;
907 	}
908 err:
909 	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
910 out:
911 	if (m != NULL) {
912 		if (need_tap && ieee80211_radiotap_active_vap(vap))
913 			ieee80211_radiotap_rx(vap, m);
914 		m_freem(m);
915 	}
916 	return type;
917 }
918 
919 static void
920 hostap_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
921     int rssi, int nf, uint16_t seq, uint16_t status)
922 {
923 	struct ieee80211vap *vap = ni->ni_vap;
924 
925 	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
926 
927 	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
928 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
929 		    ni->ni_macaddr, "open auth",
930 		    "bad sta auth mode %u", ni->ni_authmode);
931 		vap->iv_stats.is_rx_bad_auth++;	/* XXX */
932 		/*
933 		 * Clear any challenge text that may be there if
934 		 * a previous shared key auth failed and then an
935 		 * open auth is attempted.
936 		 */
937 		if (ni->ni_challenge != NULL) {
938 			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
939 			ni->ni_challenge = NULL;
940 		}
941 		/* XXX hack to workaround calling convention */
942 		ieee80211_send_error(ni, wh->i_addr2,
943 		    IEEE80211_FC0_SUBTYPE_AUTH,
944 		    (seq + 1) | (IEEE80211_STATUS_ALG<<16));
945 		return;
946 	}
947 	if (seq != IEEE80211_AUTH_OPEN_REQUEST) {
948 		vap->iv_stats.is_rx_bad_auth++;
949 		return;
950 	}
951 	/* always accept open authentication requests */
952 	if (ni == vap->iv_bss) {
953 		ni = ieee80211_dup_bss(vap, wh->i_addr2);
954 		if (ni == NULL)
955 			return;
956 	} else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
957 		(void) ieee80211_ref_node(ni);
958 	/*
959 	 * Mark the node as referenced to reflect that it's
960 	 * reference count has been bumped to insure it remains
961 	 * after the transaction completes.
962 	 */
963 	ni->ni_flags |= IEEE80211_NODE_AREF;
964 	/*
965 	 * Mark the node as requiring a valid association id
966 	 * before outbound traffic is permitted.
967 	 */
968 	ni->ni_flags |= IEEE80211_NODE_ASSOCID;
969 
970 	if (vap->iv_acl != NULL &&
971 	    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
972 		/*
973 		 * When the ACL policy is set to RADIUS we defer the
974 		 * authorization to a user agent.  Dispatch an event,
975 		 * a subsequent MLME call will decide the fate of the
976 		 * station.  If the user agent is not present then the
977 		 * node will be reclaimed due to inactivity.
978 		 */
979 		IEEE80211_NOTE_MAC(vap,
980 		    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL, ni->ni_macaddr,
981 		    "%s", "station authentication defered (radius acl)");
982 		ieee80211_notify_node_auth(ni);
983 	} else {
984 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
985 		IEEE80211_NOTE_MAC(vap,
986 		    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, ni->ni_macaddr,
987 		    "%s", "station authenticated (open)");
988 		/*
989 		 * When 802.1x is not in use mark the port
990 		 * authorized at this point so traffic can flow.
991 		 */
992 		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
993 			ieee80211_node_authorize(ni);
994 	}
995 }
996 
997 static void
998 hostap_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
999     uint8_t *frm, uint8_t *efrm, int rssi, int nf,
1000     uint16_t seq, uint16_t status)
1001 {
1002 	struct ieee80211vap *vap = ni->ni_vap;
1003 	uint8_t *challenge;
1004 	int allocbs, estatus;
1005 
1006 	KASSERT(vap->iv_state == IEEE80211_S_RUN, ("state %d", vap->iv_state));
1007 
1008 	/*
1009 	 * NB: this can happen as we allow pre-shared key
1010 	 * authentication to be enabled w/o wep being turned
1011 	 * on so that configuration of these can be done
1012 	 * in any order.  It may be better to enforce the
1013 	 * ordering in which case this check would just be
1014 	 * for sanity/consistency.
1015 	 */
1016 	if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
1017 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1018 		    ni->ni_macaddr, "shared key auth",
1019 		    "%s", " PRIVACY is disabled");
1020 		estatus = IEEE80211_STATUS_ALG;
1021 		goto bad;
1022 	}
1023 	/*
1024 	 * Pre-shared key authentication is evil; accept
1025 	 * it only if explicitly configured (it is supported
1026 	 * mainly for compatibility with clients like Mac OS X).
1027 	 */
1028 	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1029 	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1030 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1031 		    ni->ni_macaddr, "shared key auth",
1032 		    "bad sta auth mode %u", ni->ni_authmode);
1033 		vap->iv_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
1034 		estatus = IEEE80211_STATUS_ALG;
1035 		goto bad;
1036 	}
1037 
1038 	challenge = NULL;
1039 	if (frm + 1 < efrm) {
1040 		if ((frm[1] + 2) > (efrm - frm)) {
1041 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1042 			    ni->ni_macaddr, "shared key auth",
1043 			    "ie %d/%d too long",
1044 			    frm[0], (frm[1] + 2) - (efrm - frm));
1045 			vap->iv_stats.is_rx_bad_auth++;
1046 			estatus = IEEE80211_STATUS_CHALLENGE;
1047 			goto bad;
1048 		}
1049 		if (*frm == IEEE80211_ELEMID_CHALLENGE)
1050 			challenge = frm;
1051 		frm += frm[1] + 2;
1052 	}
1053 	switch (seq) {
1054 	case IEEE80211_AUTH_SHARED_CHALLENGE:
1055 	case IEEE80211_AUTH_SHARED_RESPONSE:
1056 		if (challenge == NULL) {
1057 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1058 			    ni->ni_macaddr, "shared key auth",
1059 			    "%s", "no challenge");
1060 			vap->iv_stats.is_rx_bad_auth++;
1061 			estatus = IEEE80211_STATUS_CHALLENGE;
1062 			goto bad;
1063 		}
1064 		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1065 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1066 			    ni->ni_macaddr, "shared key auth",
1067 			    "bad challenge len %d", challenge[1]);
1068 			vap->iv_stats.is_rx_bad_auth++;
1069 			estatus = IEEE80211_STATUS_CHALLENGE;
1070 			goto bad;
1071 		}
1072 	default:
1073 		break;
1074 	}
1075 	switch (seq) {
1076 	case IEEE80211_AUTH_SHARED_REQUEST:
1077 		if (ni == vap->iv_bss) {
1078 			ni = ieee80211_dup_bss(vap, wh->i_addr2);
1079 			if (ni == NULL) {
1080 				/* NB: no way to return an error */
1081 				return;
1082 			}
1083 			allocbs = 1;
1084 		} else {
1085 			if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1086 				(void) ieee80211_ref_node(ni);
1087 			allocbs = 0;
1088 		}
1089 		/*
1090 		 * Mark the node as referenced to reflect that it's
1091 		 * reference count has been bumped to insure it remains
1092 		 * after the transaction completes.
1093 		 */
1094 		ni->ni_flags |= IEEE80211_NODE_AREF;
1095 		/*
1096 		 * Mark the node as requiring a valid associatio id
1097 		 * before outbound traffic is permitted.
1098 		 */
1099 		ni->ni_flags |= IEEE80211_NODE_ASSOCID;
1100 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1101 		ni->ni_noise = nf;
1102 		if (!ieee80211_alloc_challenge(ni)) {
1103 			/* NB: don't return error so they rexmit */
1104 			return;
1105 		}
1106 		get_random_bytes(ni->ni_challenge,
1107 			IEEE80211_CHALLENGE_LEN);
1108 		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1109 		    ni, "shared key %sauth request", allocbs ? "" : "re");
1110 		/*
1111 		 * When the ACL policy is set to RADIUS we defer the
1112 		 * authorization to a user agent.  Dispatch an event,
1113 		 * a subsequent MLME call will decide the fate of the
1114 		 * station.  If the user agent is not present then the
1115 		 * node will be reclaimed due to inactivity.
1116 		 */
1117 		if (vap->iv_acl != NULL &&
1118 		    vap->iv_acl->iac_getpolicy(vap) == IEEE80211_MACCMD_POLICY_RADIUS) {
1119 			IEEE80211_NOTE_MAC(vap,
1120 			    IEEE80211_MSG_AUTH | IEEE80211_MSG_ACL,
1121 			    ni->ni_macaddr,
1122 			    "%s", "station authentication defered (radius acl)");
1123 			ieee80211_notify_node_auth(ni);
1124 			return;
1125 		}
1126 		break;
1127 	case IEEE80211_AUTH_SHARED_RESPONSE:
1128 		if (ni == vap->iv_bss) {
1129 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1130 			    ni->ni_macaddr, "shared key response",
1131 			    "%s", "unknown station");
1132 			/* NB: don't send a response */
1133 			return;
1134 		}
1135 		if (ni->ni_challenge == NULL) {
1136 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1137 			    ni->ni_macaddr, "shared key response",
1138 			    "%s", "no challenge recorded");
1139 			vap->iv_stats.is_rx_bad_auth++;
1140 			estatus = IEEE80211_STATUS_CHALLENGE;
1141 			goto bad;
1142 		}
1143 		if (memcmp(ni->ni_challenge, &challenge[2],
1144 			   challenge[1]) != 0) {
1145 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1146 			    ni->ni_macaddr, "shared key response",
1147 			    "%s", "challenge mismatch");
1148 			vap->iv_stats.is_rx_auth_fail++;
1149 			estatus = IEEE80211_STATUS_CHALLENGE;
1150 			goto bad;
1151 		}
1152 		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1153 		    ni, "%s", "station authenticated (shared key)");
1154 		ieee80211_node_authorize(ni);
1155 		break;
1156 	default:
1157 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1158 		    ni->ni_macaddr, "shared key auth",
1159 		    "bad seq %d", seq);
1160 		vap->iv_stats.is_rx_bad_auth++;
1161 		estatus = IEEE80211_STATUS_SEQUENCE;
1162 		goto bad;
1163 	}
1164 	IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1165 	return;
1166 bad:
1167 	/*
1168 	 * Send an error response; but only when operating as an AP.
1169 	 */
1170 	/* XXX hack to workaround calling convention */
1171 	ieee80211_send_error(ni, wh->i_addr2,
1172 	    IEEE80211_FC0_SUBTYPE_AUTH,
1173 	    (seq + 1) | (estatus<<16));
1174 }
1175 
1176 /*
1177  * Convert a WPA cipher selector OUI to an internal
1178  * cipher algorithm.  Where appropriate we also
1179  * record any key length.
1180  */
1181 static int
1182 wpa_cipher(const uint8_t *sel, uint8_t *keylen)
1183 {
1184 #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1185 	uint32_t w = LE_READ_4(sel);
1186 
1187 	switch (w) {
1188 	case WPA_SEL(WPA_CSE_NULL):
1189 		return IEEE80211_CIPHER_NONE;
1190 	case WPA_SEL(WPA_CSE_WEP40):
1191 		if (keylen)
1192 			*keylen = 40 / NBBY;
1193 		return IEEE80211_CIPHER_WEP;
1194 	case WPA_SEL(WPA_CSE_WEP104):
1195 		if (keylen)
1196 			*keylen = 104 / NBBY;
1197 		return IEEE80211_CIPHER_WEP;
1198 	case WPA_SEL(WPA_CSE_TKIP):
1199 		return IEEE80211_CIPHER_TKIP;
1200 	case WPA_SEL(WPA_CSE_CCMP):
1201 		return IEEE80211_CIPHER_AES_CCM;
1202 	}
1203 	return 32;		/* NB: so 1<< is discarded */
1204 #undef WPA_SEL
1205 }
1206 
1207 /*
1208  * Convert a WPA key management/authentication algorithm
1209  * to an internal code.
1210  */
1211 static int
1212 wpa_keymgmt(const uint8_t *sel)
1213 {
1214 #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1215 	uint32_t w = LE_READ_4(sel);
1216 
1217 	switch (w) {
1218 	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
1219 		return WPA_ASE_8021X_UNSPEC;
1220 	case WPA_SEL(WPA_ASE_8021X_PSK):
1221 		return WPA_ASE_8021X_PSK;
1222 	case WPA_SEL(WPA_ASE_NONE):
1223 		return WPA_ASE_NONE;
1224 	}
1225 	return 0;		/* NB: so is discarded */
1226 #undef WPA_SEL
1227 }
1228 
1229 /*
1230  * Parse a WPA information element to collect parameters.
1231  * Note that we do not validate security parameters; that
1232  * is handled by the authenticator; the parsing done here
1233  * is just for internal use in making operational decisions.
1234  */
1235 static int
1236 ieee80211_parse_wpa(struct ieee80211vap *vap, const uint8_t *frm,
1237 	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1238 {
1239 	uint8_t len = frm[1];
1240 	uint32_t w;
1241 	int n;
1242 
1243 	/*
1244 	 * Check the length once for fixed parts: OUI, type,
1245 	 * version, mcast cipher, and 2 selector counts.
1246 	 * Other, variable-length data, must be checked separately.
1247 	 */
1248 	if ((vap->iv_flags & IEEE80211_F_WPA1) == 0) {
1249 		IEEE80211_DISCARD_IE(vap,
1250 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1251 		    wh, "WPA", "not WPA, flags 0x%x", vap->iv_flags);
1252 		return IEEE80211_REASON_IE_INVALID;
1253 	}
1254 	if (len < 14) {
1255 		IEEE80211_DISCARD_IE(vap,
1256 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1257 		    wh, "WPA", "too short, len %u", len);
1258 		return IEEE80211_REASON_IE_INVALID;
1259 	}
1260 	frm += 6, len -= 4;		/* NB: len is payload only */
1261 	/* NB: iswpaoui already validated the OUI and type */
1262 	w = LE_READ_2(frm);
1263 	if (w != WPA_VERSION) {
1264 		IEEE80211_DISCARD_IE(vap,
1265 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1266 		    wh, "WPA", "bad version %u", w);
1267 		return IEEE80211_REASON_IE_INVALID;
1268 	}
1269 	frm += 2, len -= 2;
1270 
1271 	memset(rsn, 0, sizeof(*rsn));
1272 
1273 	/* multicast/group cipher */
1274 	rsn->rsn_mcastcipher = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
1275 	frm += 4, len -= 4;
1276 
1277 	/* unicast ciphers */
1278 	n = LE_READ_2(frm);
1279 	frm += 2, len -= 2;
1280 	if (len < n*4+2) {
1281 		IEEE80211_DISCARD_IE(vap,
1282 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1283 		    wh, "WPA", "ucast cipher data too short; len %u, n %u",
1284 		    len, n);
1285 		return IEEE80211_REASON_IE_INVALID;
1286 	}
1287 	w = 0;
1288 	for (; n > 0; n--) {
1289 		w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
1290 		frm += 4, len -= 4;
1291 	}
1292 	if (w & (1<<IEEE80211_CIPHER_TKIP))
1293 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1294 	else
1295 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1296 
1297 	/* key management algorithms */
1298 	n = LE_READ_2(frm);
1299 	frm += 2, len -= 2;
1300 	if (len < n*4) {
1301 		IEEE80211_DISCARD_IE(vap,
1302 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1303 		    wh, "WPA", "key mgmt alg data too short; len %u, n %u",
1304 		    len, n);
1305 		return IEEE80211_REASON_IE_INVALID;
1306 	}
1307 	w = 0;
1308 	for (; n > 0; n--) {
1309 		w |= wpa_keymgmt(frm);
1310 		frm += 4, len -= 4;
1311 	}
1312 	if (w & WPA_ASE_8021X_UNSPEC)
1313 		rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
1314 	else
1315 		rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1316 
1317 	if (len > 2)		/* optional capabilities */
1318 		rsn->rsn_caps = LE_READ_2(frm);
1319 
1320 	return 0;
1321 }
1322 
1323 /*
1324  * Convert an RSN cipher selector OUI to an internal
1325  * cipher algorithm.  Where appropriate we also
1326  * record any key length.
1327  */
1328 static int
1329 rsn_cipher(const uint8_t *sel, uint8_t *keylen)
1330 {
1331 #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1332 	uint32_t w = LE_READ_4(sel);
1333 
1334 	switch (w) {
1335 	case RSN_SEL(RSN_CSE_NULL):
1336 		return IEEE80211_CIPHER_NONE;
1337 	case RSN_SEL(RSN_CSE_WEP40):
1338 		if (keylen)
1339 			*keylen = 40 / NBBY;
1340 		return IEEE80211_CIPHER_WEP;
1341 	case RSN_SEL(RSN_CSE_WEP104):
1342 		if (keylen)
1343 			*keylen = 104 / NBBY;
1344 		return IEEE80211_CIPHER_WEP;
1345 	case RSN_SEL(RSN_CSE_TKIP):
1346 		return IEEE80211_CIPHER_TKIP;
1347 	case RSN_SEL(RSN_CSE_CCMP):
1348 		return IEEE80211_CIPHER_AES_CCM;
1349 	case RSN_SEL(RSN_CSE_WRAP):
1350 		return IEEE80211_CIPHER_AES_OCB;
1351 	}
1352 	return 32;		/* NB: so 1<< is discarded */
1353 #undef WPA_SEL
1354 }
1355 
1356 /*
1357  * Convert an RSN key management/authentication algorithm
1358  * to an internal code.
1359  */
1360 static int
1361 rsn_keymgmt(const uint8_t *sel)
1362 {
1363 #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1364 	uint32_t w = LE_READ_4(sel);
1365 
1366 	switch (w) {
1367 	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
1368 		return RSN_ASE_8021X_UNSPEC;
1369 	case RSN_SEL(RSN_ASE_8021X_PSK):
1370 		return RSN_ASE_8021X_PSK;
1371 	case RSN_SEL(RSN_ASE_NONE):
1372 		return RSN_ASE_NONE;
1373 	}
1374 	return 0;		/* NB: so is discarded */
1375 #undef RSN_SEL
1376 }
1377 
1378 /*
1379  * Parse a WPA/RSN information element to collect parameters
1380  * and validate the parameters against what has been
1381  * configured for the system.
1382  */
1383 static int
1384 ieee80211_parse_rsn(struct ieee80211vap *vap, const uint8_t *frm,
1385 	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1386 {
1387 	uint8_t len = frm[1];
1388 	uint32_t w;
1389 	int n;
1390 
1391 	/*
1392 	 * Check the length once for fixed parts:
1393 	 * version, mcast cipher, and 2 selector counts.
1394 	 * Other, variable-length data, must be checked separately.
1395 	 */
1396 	if ((vap->iv_flags & IEEE80211_F_WPA2) == 0) {
1397 		IEEE80211_DISCARD_IE(vap,
1398 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1399 		    wh, "WPA", "not RSN, flags 0x%x", vap->iv_flags);
1400 		return IEEE80211_REASON_IE_INVALID;
1401 	}
1402 	if (len < 10) {
1403 		IEEE80211_DISCARD_IE(vap,
1404 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1405 		    wh, "RSN", "too short, len %u", len);
1406 		return IEEE80211_REASON_IE_INVALID;
1407 	}
1408 	frm += 2;
1409 	w = LE_READ_2(frm);
1410 	if (w != RSN_VERSION) {
1411 		IEEE80211_DISCARD_IE(vap,
1412 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1413 		    wh, "RSN", "bad version %u", w);
1414 		return IEEE80211_REASON_IE_INVALID;
1415 	}
1416 	frm += 2, len -= 2;
1417 
1418 	memset(rsn, 0, sizeof(*rsn));
1419 
1420 	/* multicast/group cipher */
1421 	rsn->rsn_mcastcipher = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
1422 	frm += 4, len -= 4;
1423 
1424 	/* unicast ciphers */
1425 	n = LE_READ_2(frm);
1426 	frm += 2, len -= 2;
1427 	if (len < n*4+2) {
1428 		IEEE80211_DISCARD_IE(vap,
1429 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1430 		    wh, "RSN", "ucast cipher data too short; len %u, n %u",
1431 		    len, n);
1432 		return IEEE80211_REASON_IE_INVALID;
1433 	}
1434 	w = 0;
1435 	for (; n > 0; n--) {
1436 		w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
1437 		frm += 4, len -= 4;
1438 	}
1439 	if (w & (1<<IEEE80211_CIPHER_TKIP))
1440 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1441 	else
1442 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1443 
1444 	/* key management algorithms */
1445 	n = LE_READ_2(frm);
1446 	frm += 2, len -= 2;
1447 	if (len < n*4) {
1448 		IEEE80211_DISCARD_IE(vap,
1449 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1450 		    wh, "RSN", "key mgmt alg data too short; len %u, n %u",
1451 		    len, n);
1452 		return IEEE80211_REASON_IE_INVALID;
1453 	}
1454 	w = 0;
1455 	for (; n > 0; n--) {
1456 		w |= rsn_keymgmt(frm);
1457 		frm += 4, len -= 4;
1458 	}
1459 	if (w & RSN_ASE_8021X_UNSPEC)
1460 		rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
1461 	else
1462 		rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
1463 
1464 	/* optional RSN capabilities */
1465 	if (len > 2)
1466 		rsn->rsn_caps = LE_READ_2(frm);
1467 	/* XXXPMKID */
1468 
1469 	return 0;
1470 }
1471 
1472 /*
1473  * WPA/802.11i assocation request processing.
1474  */
1475 static int
1476 wpa_assocreq(struct ieee80211_node *ni, struct ieee80211_rsnparms *rsnparms,
1477 	const struct ieee80211_frame *wh, const uint8_t *wpa,
1478 	const uint8_t *rsn, uint16_t capinfo)
1479 {
1480 	struct ieee80211vap *vap = ni->ni_vap;
1481 	uint8_t reason;
1482 	int badwparsn;
1483 
1484 	ni->ni_flags &= ~(IEEE80211_NODE_WPS|IEEE80211_NODE_TSN);
1485 	if (wpa == NULL && rsn == NULL) {
1486 		if (vap->iv_flags_ext & IEEE80211_FEXT_WPS) {
1487 			/*
1488 			 * W-Fi Protected Setup (WPS) permits
1489 			 * clients to associate and pass EAPOL frames
1490 			 * to establish initial credentials.
1491 			 */
1492 			ni->ni_flags |= IEEE80211_NODE_WPS;
1493 			return 1;
1494 		}
1495 		if ((vap->iv_flags_ext & IEEE80211_FEXT_TSN) &&
1496 		    (capinfo & IEEE80211_CAPINFO_PRIVACY)) {
1497 			/*
1498 			 * Transitional Security Network.  Permits clients
1499 			 * to associate and use WEP while WPA is configured.
1500 			 */
1501 			ni->ni_flags |= IEEE80211_NODE_TSN;
1502 			return 1;
1503 		}
1504 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1505 		    wh, NULL, "%s", "no WPA/RSN IE in association request");
1506 		vap->iv_stats.is_rx_assoc_badwpaie++;
1507 		reason = IEEE80211_REASON_IE_INVALID;
1508 		goto bad;
1509 	}
1510 	/* assert right association security credentials */
1511 	badwparsn = 0;			/* NB: to silence compiler */
1512 	switch (vap->iv_flags & IEEE80211_F_WPA) {
1513 	case IEEE80211_F_WPA1:
1514 		badwparsn = (wpa == NULL);
1515 		break;
1516 	case IEEE80211_F_WPA2:
1517 		badwparsn = (rsn == NULL);
1518 		break;
1519 	case IEEE80211_F_WPA1|IEEE80211_F_WPA2:
1520 		badwparsn = (wpa == NULL && rsn == NULL);
1521 		break;
1522 	}
1523 	if (badwparsn) {
1524 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
1525 		    wh, NULL,
1526 		    "%s", "missing WPA/RSN IE in association request");
1527 		vap->iv_stats.is_rx_assoc_badwpaie++;
1528 		reason = IEEE80211_REASON_IE_INVALID;
1529 		goto bad;
1530 	}
1531 	/*
1532 	 * Parse WPA/RSN information element.
1533 	 */
1534 	if (wpa != NULL)
1535 		reason = ieee80211_parse_wpa(vap, wpa, rsnparms, wh);
1536 	else
1537 		reason = ieee80211_parse_rsn(vap, rsn, rsnparms, wh);
1538 	if (reason != 0) {
1539 		/* XXX distinguish WPA/RSN? */
1540 		vap->iv_stats.is_rx_assoc_badwpaie++;
1541 		goto bad;
1542 	}
1543 	IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA, ni,
1544 	    "%s ie: mc %u/%u uc %u/%u key %u caps 0x%x",
1545 	    wpa != NULL ? "WPA" : "RSN",
1546 	    rsnparms->rsn_mcastcipher, rsnparms->rsn_mcastkeylen,
1547 	    rsnparms->rsn_ucastcipher, rsnparms->rsn_ucastkeylen,
1548 	    rsnparms->rsn_keymgmt, rsnparms->rsn_caps);
1549 
1550 	return 1;
1551 bad:
1552 	ieee80211_node_deauth(ni, reason);
1553 	return 0;
1554 }
1555 
1556 /* XXX find a better place for definition */
1557 struct l2_update_frame {
1558 	struct ether_header eh;
1559 	uint8_t dsap;
1560 	uint8_t ssap;
1561 	uint8_t control;
1562 	uint8_t xid[3];
1563 }  __packed;
1564 
1565 /*
1566  * Deliver a TGf L2UF frame on behalf of a station.
1567  * This primes any bridge when the station is roaming
1568  * between ap's on the same wired network.
1569  */
1570 static void
1571 ieee80211_deliver_l2uf(struct ieee80211_node *ni)
1572 {
1573 	struct ieee80211vap *vap = ni->ni_vap;
1574 	struct ifnet *ifp = vap->iv_ifp;
1575 	struct mbuf *m;
1576 	struct l2_update_frame *l2uf;
1577 	struct ether_header *eh;
1578 
1579 	m = m_gethdr(M_NOWAIT, MT_DATA);
1580 	if (m == NULL) {
1581 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1582 		    "%s", "no mbuf for l2uf frame");
1583 		vap->iv_stats.is_rx_nobuf++;	/* XXX not right */
1584 		return;
1585 	}
1586 	l2uf = mtod(m, struct l2_update_frame *);
1587 	eh = &l2uf->eh;
1588 	/* dst: Broadcast address */
1589 	IEEE80211_ADDR_COPY(eh->ether_dhost, ifp->if_broadcastaddr);
1590 	/* src: associated STA */
1591 	IEEE80211_ADDR_COPY(eh->ether_shost, ni->ni_macaddr);
1592 	eh->ether_type = htons(sizeof(*l2uf) - sizeof(*eh));
1593 
1594 	l2uf->dsap = 0;
1595 	l2uf->ssap = 0;
1596 	l2uf->control = 0xf5;
1597 	l2uf->xid[0] = 0x81;
1598 	l2uf->xid[1] = 0x80;
1599 	l2uf->xid[2] = 0x00;
1600 
1601 	m->m_pkthdr.len = m->m_len = sizeof(*l2uf);
1602 	hostap_deliver_data(vap, ni, m);
1603 }
1604 
1605 static void
1606 ratesetmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1607 	int reassoc, int resp, const char *tag, int rate)
1608 {
1609 	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1610 	    "deny %s request, %s rate set mismatch, rate/MCS %d",
1611 	    reassoc ? "reassoc" : "assoc", tag, rate & IEEE80211_RATE_VAL);
1612 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_BASIC_RATE);
1613 	ieee80211_node_leave(ni);
1614 }
1615 
1616 static void
1617 capinfomismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1618 	int reassoc, int resp, const char *tag, int capinfo)
1619 {
1620 	struct ieee80211vap *vap = ni->ni_vap;
1621 
1622 	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1623 	    "deny %s request, %s mismatch 0x%x",
1624 	    reassoc ? "reassoc" : "assoc", tag, capinfo);
1625 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_CAPINFO);
1626 	ieee80211_node_leave(ni);
1627 	vap->iv_stats.is_rx_assoc_capmismatch++;
1628 }
1629 
1630 static void
1631 htcapmismatch(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1632 	int reassoc, int resp)
1633 {
1634 	IEEE80211_NOTE_MAC(ni->ni_vap, IEEE80211_MSG_ANY, wh->i_addr2,
1635 	    "deny %s request, %s missing HT ie", reassoc ? "reassoc" : "assoc");
1636 	/* XXX no better code */
1637 	IEEE80211_SEND_MGMT(ni, resp, IEEE80211_STATUS_MISSING_HT_CAPS);
1638 	ieee80211_node_leave(ni);
1639 }
1640 
1641 static void
1642 authalgreject(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
1643 	int algo, int seq, int status)
1644 {
1645 	struct ieee80211vap *vap = ni->ni_vap;
1646 
1647 	IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1648 	    wh, NULL, "unsupported alg %d", algo);
1649 	vap->iv_stats.is_rx_auth_unsupported++;
1650 	ieee80211_send_error(ni, wh->i_addr2, IEEE80211_FC0_SUBTYPE_AUTH,
1651 	    seq | (status << 16));
1652 }
1653 
1654 static __inline int
1655 ishtmixed(const uint8_t *ie)
1656 {
1657 	const struct ieee80211_ie_htinfo *ht =
1658 	    (const struct ieee80211_ie_htinfo *) ie;
1659 	return (ht->hi_byte2 & IEEE80211_HTINFO_OPMODE) ==
1660 	    IEEE80211_HTINFO_OPMODE_MIXED;
1661 }
1662 
1663 static int
1664 is11bclient(const uint8_t *rates, const uint8_t *xrates)
1665 {
1666 	static const uint32_t brates = (1<<2*1)|(1<<2*2)|(1<<11)|(1<<2*11);
1667 	int i;
1668 
1669 	/* NB: the 11b clients we care about will not have xrates */
1670 	if (xrates != NULL || rates == NULL)
1671 		return 0;
1672 	for (i = 0; i < rates[1]; i++) {
1673 		int r = rates[2+i] & IEEE80211_RATE_VAL;
1674 		if (r > 2*11 || ((1<<r) & brates) == 0)
1675 			return 0;
1676 	}
1677 	return 1;
1678 }
1679 
1680 static void
1681 hostap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
1682 	int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
1683 {
1684 	struct ieee80211vap *vap = ni->ni_vap;
1685 	struct ieee80211com *ic = ni->ni_ic;
1686 	struct ieee80211_frame *wh;
1687 	uint8_t *frm, *efrm, *sfrm;
1688 	uint8_t *ssid, *rates, *xrates, *wpa, *rsn, *wme, *ath, *htcap;
1689 	int reassoc, resp;
1690 	uint8_t rate;
1691 
1692 	wh = mtod(m0, struct ieee80211_frame *);
1693 	frm = (uint8_t *)&wh[1];
1694 	efrm = mtod(m0, uint8_t *) + m0->m_len;
1695 	switch (subtype) {
1696 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1697 	case IEEE80211_FC0_SUBTYPE_BEACON: {
1698 		struct ieee80211_scanparams scan;
1699 		/*
1700 		 * We process beacon/probe response frames when scanning;
1701 		 * otherwise we check beacon frames for overlapping non-ERP
1702 		 * BSS in 11g and/or overlapping legacy BSS when in HT.
1703 		 */
1704 		if ((ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
1705 		    subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
1706 			vap->iv_stats.is_rx_mgtdiscard++;
1707 			return;
1708 		}
1709 		/* NB: accept off-channel frames */
1710 		/* XXX TODO: use rxstatus to determine off-channel details */
1711 		if (ieee80211_parse_beacon(ni, m0, ic->ic_curchan, &scan) &~ IEEE80211_BPARSE_OFFCHAN)
1712 			return;
1713 		/*
1714 		 * Count frame now that we know it's to be processed.
1715 		 */
1716 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1717 			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
1718 			IEEE80211_NODE_STAT(ni, rx_beacons);
1719 		} else
1720 			IEEE80211_NODE_STAT(ni, rx_proberesp);
1721 		/*
1722 		 * If scanning, just pass information to the scan module.
1723 		 */
1724 		if (ic->ic_flags & IEEE80211_F_SCAN) {
1725 			if (scan.status == 0 &&		/* NB: on channel */
1726 			    (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN)) {
1727 				/*
1728 				 * Actively scanning a channel marked passive;
1729 				 * send a probe request now that we know there
1730 				 * is 802.11 traffic present.
1731 				 *
1732 				 * XXX check if the beacon we recv'd gives
1733 				 * us what we need and suppress the probe req
1734 				 */
1735 				ieee80211_probe_curchan(vap, 1);
1736 				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1737 			}
1738 			ieee80211_add_scan(vap, ic->ic_curchan, &scan, wh,
1739 			    subtype, rssi, nf);
1740 			return;
1741 		}
1742 		/*
1743 		 * Check beacon for overlapping bss w/ non ERP stations.
1744 		 * If we detect one and protection is configured but not
1745 		 * enabled, enable it and start a timer that'll bring us
1746 		 * out if we stop seeing the bss.
1747 		 */
1748 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1749 		    scan.status == 0 &&			/* NB: on-channel */
1750 		    ((scan.erp & 0x100) == 0 ||		/* NB: no ERP, 11b sta*/
1751 		     (scan.erp & IEEE80211_ERP_NON_ERP_PRESENT))) {
1752 			ic->ic_lastnonerp = ticks;
1753 			ic->ic_flags_ext |= IEEE80211_FEXT_NONERP_PR;
1754 			if (ic->ic_protmode != IEEE80211_PROT_NONE &&
1755 			    (ic->ic_flags & IEEE80211_F_USEPROT) == 0) {
1756 				IEEE80211_NOTE_FRAME(vap,
1757 				    IEEE80211_MSG_ASSOC, wh,
1758 				    "non-ERP present on channel %d "
1759 				    "(saw erp 0x%x from channel %d), "
1760 				    "enable use of protection",
1761 				    ic->ic_curchan->ic_ieee,
1762 				    scan.erp, scan.chan);
1763 				ic->ic_flags |= IEEE80211_F_USEPROT;
1764 				ieee80211_notify_erp(ic);
1765 			}
1766 		}
1767 		/*
1768 		 * Check beacon for non-HT station on HT channel
1769 		 * and update HT BSS occupancy as appropriate.
1770 		 */
1771 		if (IEEE80211_IS_CHAN_HT(ic->ic_curchan)) {
1772 			if (scan.status & IEEE80211_BPARSE_OFFCHAN) {
1773 				/*
1774 				 * Off control channel; only check frames
1775 				 * that come in the extension channel when
1776 				 * operating w/ HT40.
1777 				 */
1778 				if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
1779 					break;
1780 				if (scan.chan != ic->ic_curchan->ic_extieee)
1781 					break;
1782 			}
1783 			if (scan.htinfo == NULL) {
1784 				ieee80211_htprot_update(ic,
1785 				    IEEE80211_HTINFO_OPMODE_PROTOPT |
1786 				    IEEE80211_HTINFO_NONHT_PRESENT);
1787 			} else if (ishtmixed(scan.htinfo)) {
1788 				/* XXX? take NONHT_PRESENT from beacon? */
1789 				ieee80211_htprot_update(ic,
1790 				    IEEE80211_HTINFO_OPMODE_MIXED |
1791 				    IEEE80211_HTINFO_NONHT_PRESENT);
1792 			}
1793 		}
1794 		break;
1795 	}
1796 
1797 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1798 		if (vap->iv_state != IEEE80211_S_RUN) {
1799 			vap->iv_stats.is_rx_mgtdiscard++;
1800 			return;
1801 		}
1802 		/*
1803 		 * Consult the ACL policy module if setup.
1804 		 */
1805 		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1806 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1807 			    wh, NULL, "%s", "disallowed by ACL");
1808 			vap->iv_stats.is_rx_acl++;
1809 			return;
1810 		}
1811 		/*
1812 		 * prreq frame format
1813 		 *	[tlv] ssid
1814 		 *	[tlv] supported rates
1815 		 *	[tlv] extended supported rates
1816 		 */
1817 		ssid = rates = xrates = NULL;
1818 		sfrm = frm;
1819 		while (efrm - frm > 1) {
1820 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1821 			switch (*frm) {
1822 			case IEEE80211_ELEMID_SSID:
1823 				ssid = frm;
1824 				break;
1825 			case IEEE80211_ELEMID_RATES:
1826 				rates = frm;
1827 				break;
1828 			case IEEE80211_ELEMID_XRATES:
1829 				xrates = frm;
1830 				break;
1831 			}
1832 			frm += frm[1] + 2;
1833 		}
1834 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1835 		if (xrates != NULL)
1836 			IEEE80211_VERIFY_ELEMENT(xrates,
1837 				IEEE80211_RATE_MAXSIZE - rates[1], return);
1838 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1839 		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
1840 		if ((vap->iv_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
1841 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1842 			    wh, NULL,
1843 			    "%s", "no ssid with ssid suppression enabled");
1844 			vap->iv_stats.is_rx_ssidmismatch++; /*XXX*/
1845 			return;
1846 		}
1847 
1848 		/* XXX find a better class or define it's own */
1849 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1850 		    "%s", "recv probe req");
1851 		/*
1852 		 * Some legacy 11b clients cannot hack a complete
1853 		 * probe response frame.  When the request includes
1854 		 * only a bare-bones rate set, communicate this to
1855 		 * the transmit side.
1856 		 */
1857 		ieee80211_send_proberesp(vap, wh->i_addr2,
1858 		    is11bclient(rates, xrates) ? IEEE80211_SEND_LEGACY_11B : 0);
1859 		break;
1860 
1861 	case IEEE80211_FC0_SUBTYPE_AUTH: {
1862 		uint16_t algo, seq, status;
1863 
1864 		if (vap->iv_state != IEEE80211_S_RUN) {
1865 			vap->iv_stats.is_rx_mgtdiscard++;
1866 			return;
1867 		}
1868 		if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1869 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1870 			    wh, NULL, "%s", "wrong bssid");
1871 			vap->iv_stats.is_rx_wrongbss++;	/*XXX unique stat?*/
1872 			return;
1873 		}
1874 		/*
1875 		 * auth frame format
1876 		 *	[2] algorithm
1877 		 *	[2] sequence
1878 		 *	[2] status
1879 		 *	[tlv*] challenge
1880 		 */
1881 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1882 		algo   = le16toh(*(uint16_t *)frm);
1883 		seq    = le16toh(*(uint16_t *)(frm + 2));
1884 		status = le16toh(*(uint16_t *)(frm + 4));
1885 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1886 		    "recv auth frame with algorithm %d seq %d", algo, seq);
1887 		/*
1888 		 * Consult the ACL policy module if setup.
1889 		 */
1890 		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1891 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1892 			    wh, NULL, "%s", "disallowed by ACL");
1893 			vap->iv_stats.is_rx_acl++;
1894 			ieee80211_send_error(ni, wh->i_addr2,
1895 			    IEEE80211_FC0_SUBTYPE_AUTH,
1896 			    (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
1897 			return;
1898 		}
1899 		if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1900 			IEEE80211_DISCARD(vap,
1901 			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1902 			    wh, NULL, "%s", "TKIP countermeasures enabled");
1903 			vap->iv_stats.is_rx_auth_countermeasures++;
1904 			ieee80211_send_error(ni, wh->i_addr2,
1905 				IEEE80211_FC0_SUBTYPE_AUTH,
1906 				IEEE80211_REASON_MIC_FAILURE);
1907 			return;
1908 		}
1909 		if (algo == IEEE80211_AUTH_ALG_SHARED)
1910 			hostap_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1911 			    seq, status);
1912 		else if (algo == IEEE80211_AUTH_ALG_OPEN)
1913 			hostap_auth_open(ni, wh, rssi, nf, seq, status);
1914 		else if (algo == IEEE80211_AUTH_ALG_LEAP) {
1915 			authalgreject(ni, wh, algo,
1916 			    seq+1, IEEE80211_STATUS_ALG);
1917 			return;
1918 		} else {
1919 			/*
1920 			 * We assume that an unknown algorithm is the result
1921 			 * of a decryption failure on a shared key auth frame;
1922 			 * return a status code appropriate for that instead
1923 			 * of IEEE80211_STATUS_ALG.
1924 			 *
1925 			 * NB: a seq# of 4 is intentional; the decrypted
1926 			 *     frame likely has a bogus seq value.
1927 			 */
1928 			authalgreject(ni, wh, algo,
1929 			    4, IEEE80211_STATUS_CHALLENGE);
1930 			return;
1931 		}
1932 		break;
1933 	}
1934 
1935 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1936 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
1937 		uint16_t capinfo, lintval;
1938 		struct ieee80211_rsnparms rsnparms;
1939 
1940 		if (vap->iv_state != IEEE80211_S_RUN) {
1941 			vap->iv_stats.is_rx_mgtdiscard++;
1942 			return;
1943 		}
1944 		if (!IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_bss->ni_bssid)) {
1945 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1946 			    wh, NULL, "%s", "wrong bssid");
1947 			vap->iv_stats.is_rx_assoc_bss++;
1948 			return;
1949 		}
1950 		if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1951 			reassoc = 1;
1952 			resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
1953 		} else {
1954 			reassoc = 0;
1955 			resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
1956 		}
1957 		if (ni == vap->iv_bss) {
1958 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, wh->i_addr2,
1959 			    "deny %s request, sta not authenticated",
1960 			    reassoc ? "reassoc" : "assoc");
1961 			ieee80211_send_error(ni, wh->i_addr2,
1962 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
1963 			    IEEE80211_REASON_ASSOC_NOT_AUTHED);
1964 			vap->iv_stats.is_rx_assoc_notauth++;
1965 			return;
1966 		}
1967 
1968 		/*
1969 		 * asreq frame format
1970 		 *	[2] capability information
1971 		 *	[2] listen interval
1972 		 *	[6*] current AP address (reassoc only)
1973 		 *	[tlv] ssid
1974 		 *	[tlv] supported rates
1975 		 *	[tlv] extended supported rates
1976 		 *	[tlv] WPA or RSN
1977 		 *	[tlv] HT capabilities
1978 		 *	[tlv] Atheros capabilities
1979 		 */
1980 		IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4), return);
1981 		capinfo = le16toh(*(uint16_t *)frm);	frm += 2;
1982 		lintval = le16toh(*(uint16_t *)frm);	frm += 2;
1983 		if (reassoc)
1984 			frm += 6;	/* ignore current AP info */
1985 		ssid = rates = xrates = wpa = rsn = wme = ath = htcap = NULL;
1986 		sfrm = frm;
1987 		while (efrm - frm > 1) {
1988 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1989 			switch (*frm) {
1990 			case IEEE80211_ELEMID_SSID:
1991 				ssid = frm;
1992 				break;
1993 			case IEEE80211_ELEMID_RATES:
1994 				rates = frm;
1995 				break;
1996 			case IEEE80211_ELEMID_XRATES:
1997 				xrates = frm;
1998 				break;
1999 			case IEEE80211_ELEMID_RSN:
2000 				rsn = frm;
2001 				break;
2002 			case IEEE80211_ELEMID_HTCAP:
2003 				htcap = frm;
2004 				break;
2005 			case IEEE80211_ELEMID_VENDOR:
2006 				if (iswpaoui(frm))
2007 					wpa = frm;
2008 				else if (iswmeinfo(frm))
2009 					wme = frm;
2010 #ifdef IEEE80211_SUPPORT_SUPERG
2011 				else if (isatherosoui(frm))
2012 					ath = frm;
2013 #endif
2014 				else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
2015 					if (ishtcapoui(frm) && htcap == NULL)
2016 						htcap = frm;
2017 				}
2018 				break;
2019 			}
2020 			frm += frm[1] + 2;
2021 		}
2022 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
2023 		if (xrates != NULL)
2024 			IEEE80211_VERIFY_ELEMENT(xrates,
2025 				IEEE80211_RATE_MAXSIZE - rates[1], return);
2026 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2027 		IEEE80211_VERIFY_SSID(vap->iv_bss, ssid, return);
2028 		if (htcap != NULL) {
2029 			IEEE80211_VERIFY_LENGTH(htcap[1],
2030 			     htcap[0] == IEEE80211_ELEMID_VENDOR ?
2031 			         4 + sizeof(struct ieee80211_ie_htcap)-2 :
2032 			         sizeof(struct ieee80211_ie_htcap)-2,
2033 			     return);		/* XXX just NULL out? */
2034 		}
2035 
2036 		if ((vap->iv_flags & IEEE80211_F_WPA) &&
2037 		    !wpa_assocreq(ni, &rsnparms, wh, wpa, rsn, capinfo))
2038 			return;
2039 		/* discard challenge after association */
2040 		if (ni->ni_challenge != NULL) {
2041 			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
2042 			ni->ni_challenge = NULL;
2043 		}
2044 		/* NB: 802.11 spec says to ignore station's privacy bit */
2045 		if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
2046 			capinfomismatch(ni, wh, reassoc, resp,
2047 			    "capability", capinfo);
2048 			return;
2049 		}
2050 		/*
2051 		 * Disallow re-associate w/ invalid slot time setting.
2052 		 */
2053 		if (ni->ni_associd != 0 &&
2054 		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
2055 		    ((ni->ni_capinfo ^ capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
2056 			capinfomismatch(ni, wh, reassoc, resp,
2057 			    "slot time", capinfo);
2058 			return;
2059 		}
2060 		rate = ieee80211_setup_rates(ni, rates, xrates,
2061 				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
2062 				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
2063 		if (rate & IEEE80211_RATE_BASIC) {
2064 			ratesetmismatch(ni, wh, reassoc, resp, "legacy", rate);
2065 			vap->iv_stats.is_rx_assoc_norate++;
2066 			return;
2067 		}
2068 		/*
2069 		 * If constrained to 11g-only stations reject an
2070 		 * 11b-only station.  We cheat a bit here by looking
2071 		 * at the max negotiated xmit rate and assuming anyone
2072 		 * with a best rate <24Mb/s is an 11b station.
2073 		 */
2074 		if ((vap->iv_flags & IEEE80211_F_PUREG) && rate < 48) {
2075 			ratesetmismatch(ni, wh, reassoc, resp, "11g", rate);
2076 			vap->iv_stats.is_rx_assoc_norate++;
2077 			return;
2078 		}
2079 		/*
2080 		 * Do HT rate set handling and setup HT node state.
2081 		 */
2082 		ni->ni_chan = vap->iv_bss->ni_chan;
2083 		if (IEEE80211_IS_CHAN_HT(ni->ni_chan) && htcap != NULL) {
2084 			rate = ieee80211_setup_htrates(ni, htcap,
2085 				IEEE80211_F_DOFMCS | IEEE80211_F_DONEGO |
2086 				IEEE80211_F_DOBRS);
2087 			if (rate & IEEE80211_RATE_BASIC) {
2088 				ratesetmismatch(ni, wh, reassoc, resp,
2089 				    "HT", rate);
2090 				vap->iv_stats.is_ht_assoc_norate++;
2091 				return;
2092 			}
2093 			ieee80211_ht_node_init(ni);
2094 			ieee80211_ht_updatehtcap(ni, htcap);
2095 		} else if (ni->ni_flags & IEEE80211_NODE_HT)
2096 			ieee80211_ht_node_cleanup(ni);
2097 #ifdef IEEE80211_SUPPORT_SUPERG
2098 		else if (ni->ni_ath_flags & IEEE80211_NODE_ATH)
2099 			ieee80211_ff_node_cleanup(ni);
2100 #endif
2101 		/*
2102 		 * Allow AMPDU operation only with unencrypted traffic
2103 		 * or AES-CCM; the 11n spec only specifies these ciphers
2104 		 * so permitting any others is undefined and can lead
2105 		 * to interoperability problems.
2106 		 */
2107 		if ((ni->ni_flags & IEEE80211_NODE_HT) &&
2108 		    (((vap->iv_flags & IEEE80211_F_WPA) &&
2109 		      rsnparms.rsn_ucastcipher != IEEE80211_CIPHER_AES_CCM) ||
2110 		     (vap->iv_flags & (IEEE80211_F_WPA|IEEE80211_F_PRIVACY)) == IEEE80211_F_PRIVACY)) {
2111 			IEEE80211_NOTE(vap,
2112 			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_11N, ni,
2113 			    "disallow HT use because WEP or TKIP requested, "
2114 			    "capinfo 0x%x ucastcipher %d", capinfo,
2115 			    rsnparms.rsn_ucastcipher);
2116 			ieee80211_ht_node_cleanup(ni);
2117 			vap->iv_stats.is_ht_assoc_downgrade++;
2118 		}
2119 		/*
2120 		 * If constrained to 11n-only stations reject legacy stations.
2121 		 */
2122 		if ((vap->iv_flags_ht & IEEE80211_FHT_PUREN) &&
2123 		    (ni->ni_flags & IEEE80211_NODE_HT) == 0) {
2124 			htcapmismatch(ni, wh, reassoc, resp);
2125 			vap->iv_stats.is_ht_assoc_nohtcap++;
2126 			return;
2127 		}
2128 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
2129 		ni->ni_noise = nf;
2130 		ni->ni_intval = lintval;
2131 		ni->ni_capinfo = capinfo;
2132 		ni->ni_fhdwell = vap->iv_bss->ni_fhdwell;
2133 		ni->ni_fhindex = vap->iv_bss->ni_fhindex;
2134 		/*
2135 		 * Store the IEs.
2136 		 * XXX maybe better to just expand
2137 		 */
2138 		if (ieee80211_ies_init(&ni->ni_ies, sfrm, efrm - sfrm)) {
2139 #define	setie(_ie, _off)	ieee80211_ies_setie(ni->ni_ies, _ie, _off)
2140 			if (wpa != NULL)
2141 				setie(wpa_ie, wpa - sfrm);
2142 			if (rsn != NULL)
2143 				setie(rsn_ie, rsn - sfrm);
2144 			if (htcap != NULL)
2145 				setie(htcap_ie, htcap - sfrm);
2146 			if (wme != NULL) {
2147 				setie(wme_ie, wme - sfrm);
2148 				/*
2149 				 * Mark node as capable of QoS.
2150 				 */
2151 				ni->ni_flags |= IEEE80211_NODE_QOS;
2152 			} else
2153 				ni->ni_flags &= ~IEEE80211_NODE_QOS;
2154 #ifdef IEEE80211_SUPPORT_SUPERG
2155 			if (ath != NULL) {
2156 				setie(ath_ie, ath - sfrm);
2157 				/*
2158 				 * Parse ATH station parameters.
2159 				 */
2160 				ieee80211_parse_ath(ni, ni->ni_ies.ath_ie);
2161 			} else
2162 #endif
2163 				ni->ni_ath_flags = 0;
2164 #undef setie
2165 		} else {
2166 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
2167 			ni->ni_ath_flags = 0;
2168 		}
2169 		ieee80211_node_join(ni, resp);
2170 		ieee80211_deliver_l2uf(ni);
2171 		break;
2172 	}
2173 
2174 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
2175 	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
2176 		uint16_t reason;
2177 
2178 		if (vap->iv_state != IEEE80211_S_RUN ||
2179 		    /* NB: can happen when in promiscuous mode */
2180 		    !IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
2181 			vap->iv_stats.is_rx_mgtdiscard++;
2182 			break;
2183 		}
2184 		/*
2185 		 * deauth/disassoc frame format
2186 		 *	[2] reason
2187 		 */
2188 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
2189 		reason = le16toh(*(uint16_t *)frm);
2190 		if (subtype == IEEE80211_FC0_SUBTYPE_DEAUTH) {
2191 			vap->iv_stats.is_rx_deauth++;
2192 			IEEE80211_NODE_STAT(ni, rx_deauth);
2193 		} else {
2194 			vap->iv_stats.is_rx_disassoc++;
2195 			IEEE80211_NODE_STAT(ni, rx_disassoc);
2196 		}
2197 		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2198 		    "recv %s (reason %d)", ieee80211_mgt_subtype_name[subtype >>
2199 			IEEE80211_FC0_SUBTYPE_SHIFT], reason);
2200 		if (ni != vap->iv_bss)
2201 			ieee80211_node_leave(ni);
2202 		break;
2203 	}
2204 
2205 	case IEEE80211_FC0_SUBTYPE_ACTION:
2206 	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2207 		if (ni == vap->iv_bss) {
2208 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2209 			    wh, NULL, "%s", "unknown node");
2210 			vap->iv_stats.is_rx_mgtdiscard++;
2211 		} else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2212 		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2213 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2214 			    wh, NULL, "%s", "not for us");
2215 			vap->iv_stats.is_rx_mgtdiscard++;
2216 		} else if (vap->iv_state != IEEE80211_S_RUN) {
2217 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2218 			    wh, NULL, "wrong state %s",
2219 			    ieee80211_state_name[vap->iv_state]);
2220 			vap->iv_stats.is_rx_mgtdiscard++;
2221 		} else {
2222 			if (ieee80211_parse_action(ni, m0) == 0)
2223 				(void)ic->ic_recv_action(ni, wh, frm, efrm);
2224 		}
2225 		break;
2226 
2227 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2228 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2229 	case IEEE80211_FC0_SUBTYPE_ATIM:
2230 		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2231 		    wh, NULL, "%s", "not handled");
2232 		vap->iv_stats.is_rx_mgtdiscard++;
2233 		break;
2234 
2235 	default:
2236 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2237 		    wh, "mgt", "subtype 0x%x not handled", subtype);
2238 		vap->iv_stats.is_rx_badsubtype++;
2239 		break;
2240 	}
2241 }
2242 
2243 static void
2244 hostap_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2245 {
2246 	switch (subtype) {
2247 	case IEEE80211_FC0_SUBTYPE_PS_POLL:
2248 		ni->ni_vap->iv_recv_pspoll(ni, m);
2249 		break;
2250 	case IEEE80211_FC0_SUBTYPE_BAR:
2251 		ieee80211_recv_bar(ni, m);
2252 		break;
2253 	}
2254 }
2255 
2256 /*
2257  * Process a received ps-poll frame.
2258  */
2259 void
2260 ieee80211_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m0)
2261 {
2262 	struct ieee80211vap *vap = ni->ni_vap;
2263 	struct ieee80211com *ic = vap->iv_ic;
2264 	struct ieee80211_frame_min *wh;
2265 	struct mbuf *m;
2266 	uint16_t aid;
2267 	int qlen;
2268 
2269 	wh = mtod(m0, struct ieee80211_frame_min *);
2270 	if (ni->ni_associd == 0) {
2271 		IEEE80211_DISCARD(vap,
2272 		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2273 		    (struct ieee80211_frame *) wh, NULL,
2274 		    "%s", "unassociated station");
2275 		vap->iv_stats.is_ps_unassoc++;
2276 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
2277 			IEEE80211_REASON_NOT_ASSOCED);
2278 		return;
2279 	}
2280 
2281 	aid = le16toh(*(uint16_t *)wh->i_dur);
2282 	if (aid != ni->ni_associd) {
2283 		IEEE80211_DISCARD(vap,
2284 		    IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2285 		    (struct ieee80211_frame *) wh, NULL,
2286 		    "aid mismatch: sta aid 0x%x poll aid 0x%x",
2287 		    ni->ni_associd, aid);
2288 		vap->iv_stats.is_ps_badaid++;
2289 		/*
2290 		 * NB: We used to deauth the station but it turns out
2291 		 * the Blackberry Curve 8230 (and perhaps other devices)
2292 		 * sometimes send the wrong AID when WME is negotiated.
2293 		 * Being more lenient here seems ok as we already check
2294 		 * the station is associated and we only return frames
2295 		 * queued for the station (i.e. we don't use the AID).
2296 		 */
2297 		return;
2298 	}
2299 
2300 	/* Okay, take the first queued packet and put it out... */
2301 	m = ieee80211_node_psq_dequeue(ni, &qlen);
2302 	if (m == NULL) {
2303 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_POWER, wh->i_addr2,
2304 		    "%s", "recv ps-poll, but queue empty");
2305 		ieee80211_send_nulldata(ieee80211_ref_node(ni));
2306 		vap->iv_stats.is_ps_qempty++;	/* XXX node stat */
2307 		if (vap->iv_set_tim != NULL)
2308 			vap->iv_set_tim(ni, 0);	/* just in case */
2309 		return;
2310 	}
2311 	/*
2312 	 * If there are more packets, set the more packets bit
2313 	 * in the packet dispatched to the station; otherwise
2314 	 * turn off the TIM bit.
2315 	 */
2316 	if (qlen != 0) {
2317 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2318 		    "recv ps-poll, send packet, %u still queued", qlen);
2319 		m->m_flags |= M_MORE_DATA;
2320 	} else {
2321 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
2322 		    "%s", "recv ps-poll, send packet, queue empty");
2323 		if (vap->iv_set_tim != NULL)
2324 			vap->iv_set_tim(ni, 0);
2325 	}
2326 	m->m_flags |= M_PWR_SAV;		/* bypass PS handling */
2327 
2328 	/*
2329 	 * Do the right thing; if it's an encap'ed frame then
2330 	 * call ieee80211_parent_xmitpkt() (and free the ref) else
2331 	 * call ieee80211_vap_xmitpkt().
2332 	 */
2333 	if (m->m_flags & M_ENCAP) {
2334 		if (ieee80211_parent_xmitpkt(ic, m) != 0)
2335 			ieee80211_free_node(ni);
2336 	} else {
2337 		(void) ieee80211_vap_xmitpkt(vap, m);
2338 	}
2339 }
2340