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