xref: /freebsd/sys/net80211/ieee80211_sta.c (revision 5dae51da3da0cc94d17bd67b308fad304ebec7e0)
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 Station 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/if_dl.h>
54 #include <net/if_var.h>
55 #include <net/ethernet.h>
56 
57 #include <net/bpf.h>
58 
59 #include <net80211/ieee80211_var.h>
60 #include <net80211/ieee80211_sta.h>
61 #include <net80211/ieee80211_input.h>
62 #ifdef IEEE80211_SUPPORT_SUPERG
63 #include <net80211/ieee80211_superg.h>
64 #endif
65 #include <net80211/ieee80211_ratectl.h>
66 #include <net80211/ieee80211_sta.h>
67 
68 #define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
69 
70 static	void sta_vattach(struct ieee80211vap *);
71 static	void sta_beacon_miss(struct ieee80211vap *);
72 static	int sta_newstate(struct ieee80211vap *, enum ieee80211_state, int);
73 static	int sta_input(struct ieee80211_node *, struct mbuf *,
74 	    const struct ieee80211_rx_stats *, int, int);
75 static void sta_recv_mgmt(struct ieee80211_node *, struct mbuf *,
76 	    int subtype, const struct ieee80211_rx_stats *, int rssi, int nf);
77 static void sta_recv_ctl(struct ieee80211_node *, struct mbuf *, int subtype);
78 
79 void
80 ieee80211_sta_attach(struct ieee80211com *ic)
81 {
82 	ic->ic_vattach[IEEE80211_M_STA] = sta_vattach;
83 }
84 
85 void
86 ieee80211_sta_detach(struct ieee80211com *ic)
87 {
88 }
89 
90 static void
91 sta_vdetach(struct ieee80211vap *vap)
92 {
93 }
94 
95 static void
96 sta_vattach(struct ieee80211vap *vap)
97 {
98 	vap->iv_newstate = sta_newstate;
99 	vap->iv_input = sta_input;
100 	vap->iv_recv_mgmt = sta_recv_mgmt;
101 	vap->iv_recv_ctl = sta_recv_ctl;
102 	vap->iv_opdetach = sta_vdetach;
103 	vap->iv_bmiss = sta_beacon_miss;
104 }
105 
106 /*
107  * Handle a beacon miss event.  The common code filters out
108  * spurious events that can happen when scanning and/or before
109  * reaching RUN state.
110  */
111 static void
112 sta_beacon_miss(struct ieee80211vap *vap)
113 {
114 	struct ieee80211com *ic = vap->iv_ic;
115 
116 	IEEE80211_LOCK_ASSERT(ic);
117 
118 	KASSERT((ic->ic_flags & IEEE80211_F_SCAN) == 0, ("scanning"));
119 	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
120 	    ("wrong state %s", ieee80211_state_name[vap->iv_state]));
121 
122 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
123 	    "beacon miss, mode %s state %s\n",
124 	    ieee80211_opmode_name[vap->iv_opmode],
125 	    ieee80211_state_name[vap->iv_state]);
126 
127 	if (vap->iv_state == IEEE80211_S_CSA) {
128 		/*
129 		 * A Channel Switch is pending; assume we missed the
130 		 * beacon that would've completed the process and just
131 		 * force the switch.  If we made a mistake we'll not
132 		 * find the AP on the new channel and fall back to a
133 		 * normal scan.
134 		 */
135 		ieee80211_csa_completeswitch(ic);
136 		return;
137 	}
138 	if (++vap->iv_bmiss_count < vap->iv_bmiss_max) {
139 		/*
140 		 * Send a directed probe req before falling back to a
141 		 * scan; if we receive a response ic_bmiss_count will
142 		 * be reset.  Some cards mistakenly report beacon miss
143 		 * so this avoids the expensive scan if the ap is
144 		 * still there.
145 		 */
146 		ieee80211_send_probereq(vap->iv_bss, vap->iv_myaddr,
147 			vap->iv_bss->ni_bssid, vap->iv_bss->ni_bssid,
148 			vap->iv_bss->ni_essid, vap->iv_bss->ni_esslen);
149 		return;
150 	}
151 
152 	callout_stop(&vap->iv_swbmiss);
153 	vap->iv_bmiss_count = 0;
154 	vap->iv_stats.is_beacon_miss++;
155 	if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
156 #ifdef IEEE80211_SUPPORT_SUPERG
157 
158 		/*
159 		 * If we receive a beacon miss interrupt when using
160 		 * dynamic turbo, attempt to switch modes before
161 		 * reassociating.
162 		 */
163 		if (IEEE80211_ATH_CAP(vap, vap->iv_bss, IEEE80211_NODE_TURBOP))
164 			ieee80211_dturbo_switch(vap,
165 			    ic->ic_bsschan->ic_flags ^ IEEE80211_CHAN_TURBO);
166 #endif
167 		/*
168 		 * Try to reassociate before scanning for a new ap.
169 		 */
170 		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 1);
171 	} else {
172 		/*
173 		 * Somebody else is controlling state changes (e.g.
174 		 * a user-mode app) don't do anything that would
175 		 * confuse them; just drop into scan mode so they'll
176 		 * notified of the state change and given control.
177 		 */
178 		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
179 	}
180 }
181 
182 /*
183  * Handle deauth with reason.  We retry only for
184  * the cases where we might succeed.  Otherwise
185  * we downgrade the ap and scan.
186  */
187 static void
188 sta_authretry(struct ieee80211vap *vap, struct ieee80211_node *ni, int reason)
189 {
190 	switch (reason) {
191 	case IEEE80211_STATUS_SUCCESS:		/* NB: MLME assoc */
192 	case IEEE80211_STATUS_TIMEOUT:
193 	case IEEE80211_REASON_ASSOC_EXPIRE:
194 	case IEEE80211_REASON_NOT_AUTHED:
195 	case IEEE80211_REASON_NOT_ASSOCED:
196 	case IEEE80211_REASON_ASSOC_LEAVE:
197 	case IEEE80211_REASON_ASSOC_NOT_AUTHED:
198 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_AUTH, 1);
199 		break;
200 	default:
201 		ieee80211_scan_assoc_fail(vap, vap->iv_bss->ni_macaddr, reason);
202 		if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
203 			ieee80211_check_scan_current(vap);
204 		break;
205 	}
206 }
207 
208 static void
209 sta_swbmiss_start(struct ieee80211vap *vap)
210 {
211 
212 	if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) {
213 		/*
214 		 * Start s/w beacon miss timer for devices w/o
215 		 * hardware support.  We fudge a bit here since
216 		 * we're doing this in software.
217 		 */
218 		vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS(
219 		    2 * vap->iv_bmissthreshold * vap->iv_bss->ni_intval);
220 		vap->iv_swbmiss_count = 0;
221 		callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
222 		    ieee80211_swbmiss, vap);
223 	}
224 }
225 
226 /*
227  * IEEE80211_M_STA vap state machine handler.
228  * This routine handles the main states in the 802.11 protocol.
229  */
230 static int
231 sta_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
232 {
233 	struct ieee80211com *ic = vap->iv_ic;
234 	struct ieee80211_node *ni;
235 	enum ieee80211_state ostate;
236 
237 	IEEE80211_LOCK_ASSERT(ic);
238 
239 	ostate = vap->iv_state;
240 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
241 	    __func__, ieee80211_state_name[ostate],
242 	    ieee80211_state_name[nstate], arg);
243 	vap->iv_state = nstate;			/* state transition */
244 	callout_stop(&vap->iv_mgtsend);		/* XXX callout_drain */
245 	if (ostate != IEEE80211_S_SCAN)
246 		ieee80211_cancel_scan(vap);	/* background scan */
247 	ni = vap->iv_bss;			/* NB: no reference held */
248 	if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)
249 		callout_stop(&vap->iv_swbmiss);
250 	switch (nstate) {
251 	case IEEE80211_S_INIT:
252 		switch (ostate) {
253 		case IEEE80211_S_SLEEP:
254 			/* XXX wakeup */
255 			/* XXX driver hook to wakeup the hardware? */
256 		case IEEE80211_S_RUN:
257 			IEEE80211_SEND_MGMT(ni,
258 			    IEEE80211_FC0_SUBTYPE_DISASSOC,
259 			    IEEE80211_REASON_ASSOC_LEAVE);
260 			ieee80211_sta_leave(ni);
261 			break;
262 		case IEEE80211_S_ASSOC:
263 			IEEE80211_SEND_MGMT(ni,
264 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
265 			    IEEE80211_REASON_AUTH_LEAVE);
266 			break;
267 		case IEEE80211_S_SCAN:
268 			ieee80211_cancel_scan(vap);
269 			break;
270 		default:
271 			break;
272 		}
273 		if (ostate != IEEE80211_S_INIT) {
274 			/* NB: optimize INIT -> INIT case */
275 			ieee80211_reset_bss(vap);
276 		}
277 		if (vap->iv_auth->ia_detach != NULL)
278 			vap->iv_auth->ia_detach(vap);
279 		break;
280 	case IEEE80211_S_SCAN:
281 		switch (ostate) {
282 		case IEEE80211_S_INIT:
283 			/*
284 			 * Initiate a scan.  We can come here as a result
285 			 * of an IEEE80211_IOC_SCAN_REQ too in which case
286 			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
287 			 * and the scan request parameters will be present
288 			 * in iv_scanreq.  Otherwise we do the default.
289 			 */
290 			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
291 				ieee80211_check_scan(vap,
292 				    vap->iv_scanreq_flags,
293 				    vap->iv_scanreq_duration,
294 				    vap->iv_scanreq_mindwell,
295 				    vap->iv_scanreq_maxdwell,
296 				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
297 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
298 			} else
299 				ieee80211_check_scan_current(vap);
300 			break;
301 		case IEEE80211_S_SCAN:
302 		case IEEE80211_S_AUTH:
303 		case IEEE80211_S_ASSOC:
304 			/*
305 			 * These can happen either because of a timeout
306 			 * on an assoc/auth response or because of a
307 			 * change in state that requires a reset.  For
308 			 * the former we're called with a non-zero arg
309 			 * that is the cause for the failure; pass this
310 			 * to the scan code so it can update state.
311 			 * Otherwise trigger a new scan unless we're in
312 			 * manual roaming mode in which case an application
313 			 * must issue an explicit scan request.
314 			 */
315 			if (arg != 0)
316 				ieee80211_scan_assoc_fail(vap,
317 					vap->iv_bss->ni_macaddr, arg);
318 			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
319 				ieee80211_check_scan_current(vap);
320 			break;
321 		case IEEE80211_S_SLEEP:		/* beacon miss */
322 			/*
323 			 * XXX if in sleep we need to wakeup the hardware.
324 			 */
325 			/* FALLTHROUGH */
326 		case IEEE80211_S_RUN:		/* beacon miss */
327 			/*
328 			 * Beacon miss.  Notify user space and if not
329 			 * under control of a user application (roaming
330 			 * manual) kick off a scan to re-connect.
331 			 */
332 
333 			ieee80211_sta_leave(ni);
334 			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO)
335 				ieee80211_check_scan_current(vap);
336 			break;
337 		default:
338 			goto invalid;
339 		}
340 		break;
341 	case IEEE80211_S_AUTH:
342 		switch (ostate) {
343 		case IEEE80211_S_INIT:
344 		case IEEE80211_S_SCAN:
345 			IEEE80211_SEND_MGMT(ni,
346 			    IEEE80211_FC0_SUBTYPE_AUTH, 1);
347 			break;
348 		case IEEE80211_S_AUTH:
349 		case IEEE80211_S_ASSOC:
350 			switch (arg & 0xff) {
351 			case IEEE80211_FC0_SUBTYPE_AUTH:
352 				/* ??? */
353 				IEEE80211_SEND_MGMT(ni,
354 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
355 				break;
356 			case IEEE80211_FC0_SUBTYPE_DEAUTH:
357 				sta_authretry(vap, ni, arg>>8);
358 				break;
359 			}
360 			break;
361 		case IEEE80211_S_SLEEP:
362 		case IEEE80211_S_RUN:
363 			switch (arg & 0xff) {
364 			case IEEE80211_FC0_SUBTYPE_AUTH:
365 				IEEE80211_SEND_MGMT(ni,
366 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
367 				vap->iv_state = IEEE80211_S_RUN; /* stay RUN */
368 				break;
369 			case IEEE80211_FC0_SUBTYPE_DEAUTH:
370 				ieee80211_sta_leave(ni);
371 				if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
372 					/* try to reauth */
373 					IEEE80211_SEND_MGMT(ni,
374 					    IEEE80211_FC0_SUBTYPE_AUTH, 1);
375 				}
376 				break;
377 			}
378 			break;
379 		default:
380 			goto invalid;
381 		}
382 		break;
383 	case IEEE80211_S_ASSOC:
384 		switch (ostate) {
385 		case IEEE80211_S_AUTH:
386 		case IEEE80211_S_ASSOC:
387 			IEEE80211_SEND_MGMT(ni,
388 			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
389 			break;
390 		case IEEE80211_S_SLEEP:		/* cannot happen */
391 		case IEEE80211_S_RUN:
392 			ieee80211_sta_leave(ni);
393 			if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
394 				IEEE80211_SEND_MGMT(ni, arg ?
395 				    IEEE80211_FC0_SUBTYPE_REASSOC_REQ :
396 				    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
397 			}
398 			break;
399 		default:
400 			goto invalid;
401 		}
402 		break;
403 	case IEEE80211_S_RUN:
404 		if (vap->iv_flags & IEEE80211_F_WPA) {
405 			/* XXX validate prerequisites */
406 		}
407 		switch (ostate) {
408 		case IEEE80211_S_RUN:
409 		case IEEE80211_S_CSA:
410 			break;
411 		case IEEE80211_S_AUTH:		/* when join is done in fw */
412 		case IEEE80211_S_ASSOC:
413 #ifdef IEEE80211_DEBUG
414 			if (ieee80211_msg_debug(vap)) {
415 				ieee80211_note(vap, "%s with %s ssid ",
416 				    (vap->iv_opmode == IEEE80211_M_STA ?
417 				    "associated" : "synchronized"),
418 				    ether_sprintf(ni->ni_bssid));
419 				ieee80211_print_essid(vap->iv_bss->ni_essid,
420 				    ni->ni_esslen);
421 				/* XXX MCS/HT */
422 				printf(" channel %d start %uMb\n",
423 				    ieee80211_chan2ieee(ic, ic->ic_curchan),
424 				    IEEE80211_RATE2MBS(ni->ni_txrate));
425 			}
426 #endif
427 			ieee80211_scan_assoc_success(vap, ni->ni_macaddr);
428 			ieee80211_notify_node_join(ni,
429 			    arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
430 			break;
431 		case IEEE80211_S_SLEEP:
432 			/* Wake up from sleep */
433 			vap->iv_sta_ps(vap, 0);
434 			break;
435 		default:
436 			goto invalid;
437 		}
438 		ieee80211_sync_curchan(ic);
439 		if (ostate != IEEE80211_S_RUN)
440 			sta_swbmiss_start(vap);
441 		/*
442 		 * When 802.1x is not in use mark the port authorized
443 		 * at this point so traffic can flow.
444 		 */
445 		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
446 			ieee80211_node_authorize(ni);
447 		/*
448 		 * Fake association when joining an existing bss.
449 		 *
450 		 * Don't do this if we're doing SLEEP->RUN.
451 		 */
452 		if (ic->ic_newassoc != NULL && ostate != IEEE80211_S_SLEEP)
453 			ic->ic_newassoc(vap->iv_bss, (ostate != IEEE80211_S_RUN));
454 		break;
455 	case IEEE80211_S_CSA:
456 		if (ostate != IEEE80211_S_RUN)
457 			goto invalid;
458 		break;
459 	case IEEE80211_S_SLEEP:
460 		sta_swbmiss_start(vap);
461 		vap->iv_sta_ps(vap, 1);
462 		break;
463 	default:
464 	invalid:
465 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
466 		    "%s: unexpected state transition %s -> %s\n", __func__,
467 		    ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
468 		break;
469 	}
470 	return 0;
471 }
472 
473 /*
474  * Return non-zero if the frame is an echo of a multicast
475  * frame sent by ourself.  The dir is known to be DSTODS.
476  */
477 static __inline int
478 isdstods_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
479 {
480 #define	QWH4(wh)	((const struct ieee80211_qosframe_addr4 *)wh)
481 #define	WH4(wh)		((const struct ieee80211_frame_addr4 *)wh)
482 	const uint8_t *sa;
483 
484 	KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
485 
486 	if (!IEEE80211_IS_MULTICAST(wh->i_addr3))
487 		return 0;
488 	sa = IEEE80211_QOS_HAS_SEQ(wh) ? QWH4(wh)->i_addr4 : WH4(wh)->i_addr4;
489 	return IEEE80211_ADDR_EQ(sa, vap->iv_myaddr);
490 #undef WH4
491 #undef QWH4
492 }
493 
494 /*
495  * Return non-zero if the frame is an echo of a multicast
496  * frame sent by ourself.  The dir is known to be FROMDS.
497  */
498 static __inline int
499 isfromds_mcastecho(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
500 {
501 	KASSERT(vap->iv_opmode == IEEE80211_M_STA, ("wrong mode"));
502 
503 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
504 		return 0;
505 	return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
506 }
507 
508 /*
509  * Decide if a received management frame should be
510  * printed when debugging is enabled.  This filters some
511  * of the less interesting frames that come frequently
512  * (e.g. beacons).
513  */
514 static __inline int
515 doprint(struct ieee80211vap *vap, int subtype)
516 {
517 	switch (subtype) {
518 	case IEEE80211_FC0_SUBTYPE_BEACON:
519 		return (vap->iv_ic->ic_flags & IEEE80211_F_SCAN);
520 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
521 		return 0;
522 	}
523 	return 1;
524 }
525 
526 /*
527  * Process a received frame.  The node associated with the sender
528  * should be supplied.  If nothing was found in the node table then
529  * the caller is assumed to supply a reference to iv_bss instead.
530  * The RSSI and a timestamp are also supplied.  The RSSI data is used
531  * during AP scanning to select a AP to associate with; it can have
532  * any units so long as values have consistent units and higher values
533  * mean ``better signal''.  The receive timestamp is currently not used
534  * by the 802.11 layer.
535  */
536 static int
537 sta_input(struct ieee80211_node *ni, struct mbuf *m,
538     const struct ieee80211_rx_stats *rxs, int rssi, int nf)
539 {
540 	struct ieee80211vap *vap = ni->ni_vap;
541 	struct ieee80211com *ic = ni->ni_ic;
542 	struct ifnet *ifp = vap->iv_ifp;
543 	struct ieee80211_frame *wh;
544 	struct ieee80211_key *key;
545 	struct ether_header *eh;
546 	int hdrspace, need_tap = 1;	/* mbuf need to be tapped. */
547 	uint8_t dir, type, subtype, qos;
548 	uint8_t *bssid;
549 
550 	if (m->m_flags & M_AMPDU_MPDU) {
551 		/*
552 		 * Fastpath for A-MPDU reorder q resubmission.  Frames
553 		 * w/ M_AMPDU_MPDU marked have already passed through
554 		 * here but were received out of order and been held on
555 		 * the reorder queue.  When resubmitted they are marked
556 		 * with the M_AMPDU_MPDU flag and we can bypass most of
557 		 * the normal processing.
558 		 */
559 		wh = mtod(m, struct ieee80211_frame *);
560 		type = IEEE80211_FC0_TYPE_DATA;
561 		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
562 		subtype = IEEE80211_FC0_SUBTYPE_QOS;
563 		hdrspace = ieee80211_hdrspace(ic, wh);	/* XXX optimize? */
564 		goto resubmit_ampdu;
565 	}
566 
567 	KASSERT(ni != NULL, ("null node"));
568 	ni->ni_inact = ni->ni_inact_reload;
569 
570 	type = -1;			/* undefined */
571 
572 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
573 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
574 		    ni->ni_macaddr, NULL,
575 		    "too short (1): len %u", m->m_pkthdr.len);
576 		vap->iv_stats.is_rx_tooshort++;
577 		goto out;
578 	}
579 	/*
580 	 * Bit of a cheat here, we use a pointer for a 3-address
581 	 * frame format but don't reference fields past outside
582 	 * ieee80211_frame_min w/o first validating the data is
583 	 * present.
584 	 */
585 	wh = mtod(m, struct ieee80211_frame *);
586 
587 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
588 	    IEEE80211_FC0_VERSION_0) {
589 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
590 		    ni->ni_macaddr, NULL, "wrong version, fc %02x:%02x",
591 		    wh->i_fc[0], wh->i_fc[1]);
592 		vap->iv_stats.is_rx_badversion++;
593 		goto err;
594 	}
595 
596 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
597 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
598 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
599 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
600 		bssid = wh->i_addr2;
601 		if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) {
602 			/* not interested in */
603 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
604 			    bssid, NULL, "%s", "not to bss");
605 			vap->iv_stats.is_rx_wrongbss++;
606 			goto out;
607 		}
608 
609 		/*
610 		 * Some devices may be in a promiscuous mode
611 		 * where they receive frames for multiple station
612 		 * addresses.
613 		 *
614 		 * If we receive a data frame that isn't
615 		 * destined to our VAP MAC, drop it.
616 		 *
617 		 * XXX TODO: This is only enforced when not scanning;
618 		 * XXX it assumes a software-driven scan will put the NIC
619 		 * XXX into a "no data frames" mode before setting this
620 		 * XXX flag. Otherwise it may be possible that we'll still
621 		 * XXX process data frames whilst scanning.
622 		 */
623 		if ((! IEEE80211_IS_MULTICAST(wh->i_addr1))
624 		    && (! IEEE80211_ADDR_EQ(wh->i_addr1, IF_LLADDR(ifp)))) {
625 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
626 			    bssid, NULL, "not to cur sta: lladdr=%6D, addr1=%6D",
627 			    IF_LLADDR(ifp), ":", wh->i_addr1, ":");
628 			vap->iv_stats.is_rx_wrongbss++;
629 			goto out;
630 		}
631 
632 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
633 		ni->ni_noise = nf;
634 		if ( IEEE80211_HAS_SEQ(type, subtype) &&
635 		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
636 			uint8_t tid = ieee80211_gettid(wh);
637 			if (IEEE80211_QOS_HAS_SEQ(wh) &&
638 			    TID_TO_WME_AC(tid) >= WME_AC_VI)
639 				ic->ic_wme.wme_hipri_traffic++;
640 			if (! ieee80211_check_rxseq(ni, wh, bssid))
641 				goto out;
642 		}
643 	}
644 
645 	switch (type) {
646 	case IEEE80211_FC0_TYPE_DATA:
647 		hdrspace = ieee80211_hdrspace(ic, wh);
648 		if (m->m_len < hdrspace &&
649 		    (m = m_pullup(m, hdrspace)) == NULL) {
650 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
651 			    ni->ni_macaddr, NULL,
652 			    "data too short: expecting %u", hdrspace);
653 			vap->iv_stats.is_rx_tooshort++;
654 			goto out;		/* XXX */
655 		}
656 		/*
657 		 * Handle A-MPDU re-ordering.  If the frame is to be
658 		 * processed directly then ieee80211_ampdu_reorder
659 		 * will return 0; otherwise it has consumed the mbuf
660 		 * and we should do nothing more with it.
661 		 */
662 		if ((m->m_flags & M_AMPDU) &&
663 		    (dir == IEEE80211_FC1_DIR_FROMDS ||
664 		     dir == IEEE80211_FC1_DIR_DSTODS) &&
665 		    ieee80211_ampdu_reorder(ni, m) != 0) {
666 			m = NULL;
667 			goto out;
668 		}
669 	resubmit_ampdu:
670 		if (dir == IEEE80211_FC1_DIR_FROMDS) {
671 			if ((ifp->if_flags & IFF_SIMPLEX) &&
672 			    isfromds_mcastecho(vap, wh)) {
673 				/*
674 				 * In IEEE802.11 network, multicast
675 				 * packets sent from "me" are broadcast
676 				 * from the AP; silently discard for
677 				 * SIMPLEX interface.
678 				 */
679 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
680 				    wh, "data", "%s", "multicast echo");
681 				vap->iv_stats.is_rx_mcastecho++;
682 				goto out;
683 			}
684 			if ((vap->iv_flags & IEEE80211_F_DWDS) &&
685 			    IEEE80211_IS_MULTICAST(wh->i_addr1)) {
686 				/*
687 				 * DWDS sta's must drop 3-address mcast frames
688 				 * as they will be sent separately as a 4-addr
689 				 * frame.  Accepting the 3-addr frame will
690 				 * confuse the bridge into thinking the sending
691 				 * sta is located at the end of WDS link.
692 				 */
693 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
694 				    "3-address data", "%s", "DWDS enabled");
695 				vap->iv_stats.is_rx_mcastecho++;
696 				goto out;
697 			}
698 		} else if (dir == IEEE80211_FC1_DIR_DSTODS) {
699 			if ((vap->iv_flags & IEEE80211_F_DWDS) == 0) {
700 				IEEE80211_DISCARD(vap,
701 				    IEEE80211_MSG_INPUT, wh, "4-address data",
702 				    "%s", "DWDS not enabled");
703 				vap->iv_stats.is_rx_wrongdir++;
704 				goto out;
705 			}
706 			if ((ifp->if_flags & IFF_SIMPLEX) &&
707 			    isdstods_mcastecho(vap, wh)) {
708 				/*
709 				 * In IEEE802.11 network, multicast
710 				 * packets sent from "me" are broadcast
711 				 * from the AP; silently discard for
712 				 * SIMPLEX interface.
713 				 */
714 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
715 				    "4-address data", "%s", "multicast echo");
716 				vap->iv_stats.is_rx_mcastecho++;
717 				goto out;
718 			}
719 		} else {
720 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, wh,
721 			    "data", "incorrect dir 0x%x", dir);
722 			vap->iv_stats.is_rx_wrongdir++;
723 			goto out;
724 		}
725 
726 		/*
727 		 * Handle privacy requirements.  Note that we
728 		 * must not be preempted from here until after
729 		 * we (potentially) call ieee80211_crypto_demic;
730 		 * otherwise we may violate assumptions in the
731 		 * crypto cipher modules used to do delayed update
732 		 * of replay sequence numbers.
733 		 */
734 		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
735 			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
736 				/*
737 				 * Discard encrypted frames when privacy is off.
738 				 */
739 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
740 				    wh, "WEP", "%s", "PRIVACY off");
741 				vap->iv_stats.is_rx_noprivacy++;
742 				IEEE80211_NODE_STAT(ni, rx_noprivacy);
743 				goto out;
744 			}
745 			key = ieee80211_crypto_decap(ni, m, hdrspace);
746 			if (key == NULL) {
747 				/* NB: stats+msgs handled in crypto_decap */
748 				IEEE80211_NODE_STAT(ni, rx_wepfail);
749 				goto out;
750 			}
751 			wh = mtod(m, struct ieee80211_frame *);
752 			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
753 		} else {
754 			/* XXX M_WEP and IEEE80211_F_PRIVACY */
755 			key = NULL;
756 		}
757 
758 		/*
759 		 * Save QoS bits for use below--before we strip the header.
760 		 */
761 		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
762 			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
763 			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
764 			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
765 		} else
766 			qos = 0;
767 
768 		/*
769 		 * Next up, any fragmentation.
770 		 */
771 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
772 			m = ieee80211_defrag(ni, m, hdrspace);
773 			if (m == NULL) {
774 				/* Fragment dropped or frame not complete yet */
775 				goto out;
776 			}
777 		}
778 		wh = NULL;		/* no longer valid, catch any uses */
779 
780 		/*
781 		 * Next strip any MSDU crypto bits.
782 		 */
783 		if (key != NULL && !ieee80211_crypto_demic(vap, key, m, 0)) {
784 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
785 			    ni->ni_macaddr, "data", "%s", "demic error");
786 			vap->iv_stats.is_rx_demicfail++;
787 			IEEE80211_NODE_STAT(ni, rx_demicfail);
788 			goto out;
789 		}
790 
791 		/* copy to listener after decrypt */
792 		if (ieee80211_radiotap_active_vap(vap))
793 			ieee80211_radiotap_rx(vap, m);
794 		need_tap = 0;
795 
796 		/*
797 		 * Finally, strip the 802.11 header.
798 		 */
799 		m = ieee80211_decap(vap, m, hdrspace);
800 		if (m == NULL) {
801 			/* XXX mask bit to check for both */
802 			/* don't count Null data frames as errors */
803 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
804 			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
805 				goto out;
806 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
807 			    ni->ni_macaddr, "data", "%s", "decap error");
808 			vap->iv_stats.is_rx_decap++;
809 			IEEE80211_NODE_STAT(ni, rx_decap);
810 			goto err;
811 		}
812 		eh = mtod(m, struct ether_header *);
813 		if (!ieee80211_node_is_authorized(ni)) {
814 			/*
815 			 * Deny any non-PAE frames received prior to
816 			 * authorization.  For open/shared-key
817 			 * authentication the port is mark authorized
818 			 * after authentication completes.  For 802.1x
819 			 * the port is not marked authorized by the
820 			 * authenticator until the handshake has completed.
821 			 */
822 			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
823 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
824 				    eh->ether_shost, "data",
825 				    "unauthorized port: ether type 0x%x len %u",
826 				    eh->ether_type, m->m_pkthdr.len);
827 				vap->iv_stats.is_rx_unauth++;
828 				IEEE80211_NODE_STAT(ni, rx_unauth);
829 				goto err;
830 			}
831 		} else {
832 			/*
833 			 * When denying unencrypted frames, discard
834 			 * any non-PAE frames received without encryption.
835 			 */
836 			if ((vap->iv_flags & IEEE80211_F_DROPUNENC) &&
837 			    (key == NULL && (m->m_flags & M_WEP) == 0) &&
838 			    eh->ether_type != htons(ETHERTYPE_PAE)) {
839 				/*
840 				 * Drop unencrypted frames.
841 				 */
842 				vap->iv_stats.is_rx_unencrypted++;
843 				IEEE80211_NODE_STAT(ni, rx_unencrypted);
844 				goto out;
845 			}
846 		}
847 		/* XXX require HT? */
848 		if (qos & IEEE80211_QOS_AMSDU) {
849 			m = ieee80211_decap_amsdu(ni, m);
850 			if (m == NULL)
851 				return IEEE80211_FC0_TYPE_DATA;
852 		} else {
853 #ifdef IEEE80211_SUPPORT_SUPERG
854 			m = ieee80211_decap_fastframe(vap, ni, m);
855 			if (m == NULL)
856 				return IEEE80211_FC0_TYPE_DATA;
857 #endif
858 		}
859 		ieee80211_deliver_data(vap, ni, m);
860 		return IEEE80211_FC0_TYPE_DATA;
861 
862 	case IEEE80211_FC0_TYPE_MGT:
863 		vap->iv_stats.is_rx_mgmt++;
864 		IEEE80211_NODE_STAT(ni, rx_mgmt);
865 		if (dir != IEEE80211_FC1_DIR_NODS) {
866 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
867 			    wh, "data", "incorrect dir 0x%x", dir);
868 			vap->iv_stats.is_rx_wrongdir++;
869 			goto err;
870 		}
871 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
872 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
873 			    ni->ni_macaddr, "mgt", "too short: len %u",
874 			    m->m_pkthdr.len);
875 			vap->iv_stats.is_rx_tooshort++;
876 			goto out;
877 		}
878 #ifdef IEEE80211_DEBUG
879 		if ((ieee80211_msg_debug(vap) && doprint(vap, subtype)) ||
880 		    ieee80211_msg_dumppkts(vap)) {
881 			if_printf(ifp, "received %s from %s rssi %d\n",
882 			    ieee80211_mgt_subtype_name(subtype),
883 			    ether_sprintf(wh->i_addr2), rssi);
884 		}
885 #endif
886 		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
887 			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
888 				/*
889 				 * Only shared key auth frames with a challenge
890 				 * should be encrypted, discard all others.
891 				 */
892 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
893 				    wh, ieee80211_mgt_subtype_name(subtype),
894 				    "%s", "WEP set but not permitted");
895 				vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
896 				goto out;
897 			}
898 			if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
899 				/*
900 				 * Discard encrypted frames when privacy is off.
901 				 */
902 				IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
903 				    wh, "mgt", "%s", "WEP set but PRIVACY off");
904 				vap->iv_stats.is_rx_noprivacy++;
905 				goto out;
906 			}
907 			hdrspace = ieee80211_hdrspace(ic, wh);
908 			key = ieee80211_crypto_decap(ni, m, hdrspace);
909 			if (key == NULL) {
910 				/* NB: stats+msgs handled in crypto_decap */
911 				goto out;
912 			}
913 			wh = mtod(m, struct ieee80211_frame *);
914 			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
915 		}
916 		vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
917 		goto out;
918 
919 	case IEEE80211_FC0_TYPE_CTL:
920 		vap->iv_stats.is_rx_ctl++;
921 		IEEE80211_NODE_STAT(ni, rx_ctrl);
922 		vap->iv_recv_ctl(ni, m, subtype);
923 		goto out;
924 
925 	default:
926 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
927 		    wh, NULL, "bad frame type 0x%x", type);
928 		/* should not come here */
929 		break;
930 	}
931 err:
932 	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
933 out:
934 	if (m != NULL) {
935 		if (need_tap && ieee80211_radiotap_active_vap(vap))
936 			ieee80211_radiotap_rx(vap, m);
937 		m_freem(m);
938 	}
939 	return type;
940 }
941 
942 static void
943 sta_auth_open(struct ieee80211_node *ni, struct ieee80211_frame *wh,
944     int rssi, int nf, uint16_t seq, uint16_t status)
945 {
946 	struct ieee80211vap *vap = ni->ni_vap;
947 
948 	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
949 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
950 		    ni->ni_macaddr, "open auth",
951 		    "bad sta auth mode %u", ni->ni_authmode);
952 		vap->iv_stats.is_rx_bad_auth++;	/* XXX */
953 		return;
954 	}
955 	if (vap->iv_state != IEEE80211_S_AUTH ||
956 	    seq != IEEE80211_AUTH_OPEN_RESPONSE) {
957 		vap->iv_stats.is_rx_bad_auth++;
958 		return;
959 	}
960 	if (status != 0) {
961 		IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
962 		    ni, "open auth failed (reason %d)", status);
963 		vap->iv_stats.is_rx_auth_fail++;
964 		vap->iv_stats.is_rx_authfail_code = status;
965 		ieee80211_new_state(vap, IEEE80211_S_SCAN,
966 		    IEEE80211_SCAN_FAIL_STATUS);
967 	} else
968 		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
969 }
970 
971 static void
972 sta_auth_shared(struct ieee80211_node *ni, struct ieee80211_frame *wh,
973     uint8_t *frm, uint8_t *efrm, int rssi, int nf,
974     uint16_t seq, uint16_t status)
975 {
976 	struct ieee80211vap *vap = ni->ni_vap;
977 	uint8_t *challenge;
978 
979 	/*
980 	 * NB: this can happen as we allow pre-shared key
981 	 * authentication to be enabled w/o wep being turned
982 	 * on so that configuration of these can be done
983 	 * in any order.  It may be better to enforce the
984 	 * ordering in which case this check would just be
985 	 * for sanity/consistency.
986 	 */
987 	if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) {
988 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
989 		    ni->ni_macaddr, "shared key auth",
990 		    "%s", " PRIVACY is disabled");
991 		goto bad;
992 	}
993 	/*
994 	 * Pre-shared key authentication is evil; accept
995 	 * it only if explicitly configured (it is supported
996 	 * mainly for compatibility with clients like OS X).
997 	 */
998 	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
999 	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1000 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1001 		    ni->ni_macaddr, "shared key auth",
1002 		    "bad sta auth mode %u", ni->ni_authmode);
1003 		vap->iv_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
1004 		goto bad;
1005 	}
1006 
1007 	challenge = NULL;
1008 	if (frm + 1 < efrm) {
1009 		if ((frm[1] + 2) > (efrm - frm)) {
1010 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1011 			    ni->ni_macaddr, "shared key auth",
1012 			    "ie %d/%d too long",
1013 			    frm[0], (frm[1] + 2) - (efrm - frm));
1014 			vap->iv_stats.is_rx_bad_auth++;
1015 			goto bad;
1016 		}
1017 		if (*frm == IEEE80211_ELEMID_CHALLENGE)
1018 			challenge = frm;
1019 		frm += frm[1] + 2;
1020 	}
1021 	switch (seq) {
1022 	case IEEE80211_AUTH_SHARED_CHALLENGE:
1023 	case IEEE80211_AUTH_SHARED_RESPONSE:
1024 		if (challenge == NULL) {
1025 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1026 			    ni->ni_macaddr, "shared key auth",
1027 			    "%s", "no challenge");
1028 			vap->iv_stats.is_rx_bad_auth++;
1029 			goto bad;
1030 		}
1031 		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1032 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_AUTH,
1033 			    ni->ni_macaddr, "shared key auth",
1034 			    "bad challenge len %d", challenge[1]);
1035 			vap->iv_stats.is_rx_bad_auth++;
1036 			goto bad;
1037 		}
1038 	default:
1039 		break;
1040 	}
1041 	if (vap->iv_state != IEEE80211_S_AUTH)
1042 		return;
1043 	switch (seq) {
1044 	case IEEE80211_AUTH_SHARED_PASS:
1045 		if (ni->ni_challenge != NULL) {
1046 			IEEE80211_FREE(ni->ni_challenge, M_80211_NODE);
1047 			ni->ni_challenge = NULL;
1048 		}
1049 		if (status != 0) {
1050 			IEEE80211_NOTE_FRAME(vap,
1051 			    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH, wh,
1052 			    "shared key auth failed (reason %d)", status);
1053 			vap->iv_stats.is_rx_auth_fail++;
1054 			vap->iv_stats.is_rx_authfail_code = status;
1055 			return;
1056 		}
1057 		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1058 		break;
1059 	case IEEE80211_AUTH_SHARED_CHALLENGE:
1060 		if (!ieee80211_alloc_challenge(ni))
1061 			return;
1062 		/* XXX could optimize by passing recvd challenge */
1063 		memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
1064 		IEEE80211_SEND_MGMT(ni,
1065 			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1066 		break;
1067 	default:
1068 		IEEE80211_DISCARD(vap, IEEE80211_MSG_AUTH,
1069 		    wh, "shared key auth", "bad seq %d", seq);
1070 		vap->iv_stats.is_rx_bad_auth++;
1071 		return;
1072 	}
1073 	return;
1074 bad:
1075 	/*
1076 	 * Kick the state machine.  This short-circuits
1077 	 * using the mgt frame timeout to trigger the
1078 	 * state transition.
1079 	 */
1080 	if (vap->iv_state == IEEE80211_S_AUTH)
1081 		ieee80211_new_state(vap, IEEE80211_S_SCAN,
1082 		    IEEE80211_SCAN_FAIL_STATUS);
1083 }
1084 
1085 int
1086 ieee80211_parse_wmeparams(struct ieee80211vap *vap, uint8_t *frm,
1087 	const struct ieee80211_frame *wh)
1088 {
1089 #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
1090 	struct ieee80211_wme_state *wme = &vap->iv_ic->ic_wme;
1091 	u_int len = frm[1], qosinfo;
1092 	int i;
1093 
1094 	if (len < sizeof(struct ieee80211_wme_param)-2) {
1095 		IEEE80211_DISCARD_IE(vap,
1096 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
1097 		    wh, "WME", "too short, len %u", len);
1098 		return -1;
1099 	}
1100 	qosinfo = frm[__offsetof(struct ieee80211_wme_param, param_qosInfo)];
1101 	qosinfo &= WME_QOSINFO_COUNT;
1102 	/* XXX do proper check for wraparound */
1103 	if (qosinfo == wme->wme_wmeChanParams.cap_info)
1104 		return 0;
1105 	frm += __offsetof(struct ieee80211_wme_param, params_acParams);
1106 	for (i = 0; i < WME_NUM_AC; i++) {
1107 		struct wmeParams *wmep =
1108 			&wme->wme_wmeChanParams.cap_wmeParams[i];
1109 		/* NB: ACI not used */
1110 		wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
1111 		wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
1112 		wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
1113 		wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
1114 		wmep->wmep_txopLimit = le16dec(frm+2);
1115 		frm += 4;
1116 	}
1117 	wme->wme_wmeChanParams.cap_info = qosinfo;
1118 	return 1;
1119 #undef MS
1120 }
1121 
1122 /*
1123  * Process 11h Channel Switch Announcement (CSA) ie.  If this
1124  * is the first CSA then initiate the switch.  Otherwise we
1125  * track state and trigger completion and/or cancel of the switch.
1126  * XXX should be public for IBSS use
1127  */
1128 static void
1129 ieee80211_parse_csaparams(struct ieee80211vap *vap, uint8_t *frm,
1130 	const struct ieee80211_frame *wh)
1131 {
1132 	struct ieee80211com *ic = vap->iv_ic;
1133 	const struct ieee80211_csa_ie *csa =
1134 	    (const struct ieee80211_csa_ie *) frm;
1135 
1136 	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
1137 	    ("state %s", ieee80211_state_name[vap->iv_state]));
1138 
1139 	if (csa->csa_mode > 1) {
1140 		IEEE80211_DISCARD_IE(vap,
1141 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1142 		    wh, "CSA", "invalid mode %u", csa->csa_mode);
1143 		return;
1144 	}
1145 	IEEE80211_LOCK(ic);
1146 	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) {
1147 		/*
1148 		 * Convert the channel number to a channel reference.  We
1149 		 * try first to preserve turbo attribute of the current
1150 		 * channel then fallback.  Note this will not work if the
1151 		 * CSA specifies a channel that requires a band switch (e.g.
1152 		 * 11a => 11g).  This is intentional as 11h is defined only
1153 		 * for 5GHz/11a and because the switch does not involve a
1154 		 * reassociation, protocol state (capabilities, negotated
1155 		 * rates, etc) may/will be wrong.
1156 		 */
1157 		struct ieee80211_channel *c =
1158 		    ieee80211_find_channel_byieee(ic, csa->csa_newchan,
1159 			(ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALLTURBO));
1160 		if (c == NULL) {
1161 			c = ieee80211_find_channel_byieee(ic,
1162 			    csa->csa_newchan,
1163 			    (ic->ic_bsschan->ic_flags & IEEE80211_CHAN_ALL));
1164 			if (c == NULL) {
1165 				IEEE80211_DISCARD_IE(vap,
1166 				    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1167 				    wh, "CSA", "invalid channel %u",
1168 				    csa->csa_newchan);
1169 				goto done;
1170 			}
1171 		}
1172 #if IEEE80211_CSA_COUNT_MIN > 0
1173 		if (csa->csa_count < IEEE80211_CSA_COUNT_MIN) {
1174 			/*
1175 			 * Require at least IEEE80211_CSA_COUNT_MIN count to
1176 			 * reduce the risk of being redirected by a fabricated
1177 			 * CSA.  If a valid CSA is dropped we'll still get a
1178 			 * beacon miss when the AP leaves the channel so we'll
1179 			 * eventually follow to the new channel.
1180 			 *
1181 			 * NOTE: this violates the 11h spec that states that
1182 			 * count may be any value and if 0 then a switch
1183 			 * should happen asap.
1184 			 */
1185 			IEEE80211_DISCARD_IE(vap,
1186 			    IEEE80211_MSG_ELEMID | IEEE80211_MSG_DOTH,
1187 			    wh, "CSA", "count %u too small, must be >= %u",
1188 			    csa->csa_count, IEEE80211_CSA_COUNT_MIN);
1189 			goto done;
1190 		}
1191 #endif
1192 		ieee80211_csa_startswitch(ic, c, csa->csa_mode, csa->csa_count);
1193 	} else {
1194 		/*
1195 		 * Validate this ie against the initial CSA.  We require
1196 		 * mode and channel not change and the count must be
1197 		 * monotonically decreasing.  This may be pointless and
1198 		 * canceling the switch as a result may be too paranoid but
1199 		 * in the worst case if we drop out of CSA because of this
1200 		 * and the AP does move then we'll just end up taking a
1201 		 * beacon miss and scan to find the AP.
1202 		 *
1203 		 * XXX may want <= on count as we also process ProbeResp
1204 		 * frames and those may come in w/ the same count as the
1205 		 * previous beacon; but doing so leaves us open to a stuck
1206 		 * count until we add a dead-man timer
1207 		 */
1208 		if (!(csa->csa_count < ic->ic_csa_count &&
1209 		      csa->csa_mode == ic->ic_csa_mode &&
1210 		      csa->csa_newchan == ieee80211_chan2ieee(ic, ic->ic_csa_newchan))) {
1211 			IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_DOTH, wh,
1212 			    "CSA ie mismatch, initial ie <%d,%d,%d>, "
1213 			    "this ie <%d,%d,%d>", ic->ic_csa_mode,
1214 			    ic->ic_csa_newchan, ic->ic_csa_count,
1215 			    csa->csa_mode, csa->csa_newchan, csa->csa_count);
1216 			ieee80211_csa_cancelswitch(ic);
1217 		} else {
1218 			if (csa->csa_count <= 1)
1219 				ieee80211_csa_completeswitch(ic);
1220 			else
1221 				ic->ic_csa_count = csa->csa_count;
1222 		}
1223 	}
1224 done:
1225 	IEEE80211_UNLOCK(ic);
1226 }
1227 
1228 /*
1229  * Return non-zero if a background scan may be continued:
1230  * o bg scan is active
1231  * o no channel switch is pending
1232  * o there has not been any traffic recently
1233  *
1234  * Note we do not check if there is an administrative enable;
1235  * this is only done to start the scan.  We assume that any
1236  * change in state will be accompanied by a request to cancel
1237  * active scans which will otherwise cause this test to fail.
1238  */
1239 static __inline int
1240 contbgscan(struct ieee80211vap *vap)
1241 {
1242 	struct ieee80211com *ic = vap->iv_ic;
1243 
1244 	return ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) &&
1245 	    (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1246 	    vap->iv_state == IEEE80211_S_RUN &&		/* XXX? */
1247 	    ieee80211_time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1248 }
1249 
1250 /*
1251  * Return non-zero if a backgrond scan may be started:
1252  * o bg scanning is administratively enabled
1253  * o no channel switch is pending
1254  * o we are not boosted on a dynamic turbo channel
1255  * o there has not been a scan recently
1256  * o there has not been any traffic recently
1257  */
1258 static __inline int
1259 startbgscan(struct ieee80211vap *vap)
1260 {
1261 	struct ieee80211com *ic = vap->iv_ic;
1262 
1263 	return ((vap->iv_flags & IEEE80211_F_BGSCAN) &&
1264 	    (ic->ic_flags & IEEE80211_F_CSAPENDING) == 0 &&
1265 #ifdef IEEE80211_SUPPORT_SUPERG
1266 	    !IEEE80211_IS_CHAN_DTURBO(ic->ic_curchan) &&
1267 #endif
1268 	    ieee80211_time_after(ticks, ic->ic_lastscan + vap->iv_bgscanintvl) &&
1269 	    ieee80211_time_after(ticks, ic->ic_lastdata + vap->iv_bgscanidle));
1270 }
1271 
1272 static void
1273 sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
1274     const struct ieee80211_rx_stats *rxs,
1275     int rssi, int nf)
1276 {
1277 #define	ISREASSOC(_st)	((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
1278 	struct ieee80211vap *vap = ni->ni_vap;
1279 	struct ieee80211com *ic = ni->ni_ic;
1280 	struct ieee80211_channel *rxchan = ic->ic_curchan;
1281 	struct ieee80211_frame *wh;
1282 	uint8_t *frm, *efrm;
1283 	uint8_t *rates, *xrates, *wme, *htcap, *htinfo;
1284 	uint8_t rate;
1285 	int ht_state_change = 0;
1286 
1287 	wh = mtod(m0, struct ieee80211_frame *);
1288 	frm = (uint8_t *)&wh[1];
1289 	efrm = mtod(m0, uint8_t *) + m0->m_len;
1290 	switch (subtype) {
1291 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1292 	case IEEE80211_FC0_SUBTYPE_BEACON: {
1293 		struct ieee80211_scanparams scan;
1294 		struct ieee80211_channel *c;
1295 		/*
1296 		 * We process beacon/probe response frames:
1297 		 *    o when scanning, or
1298 		 *    o station mode when associated (to collect state
1299 		 *      updates such as 802.11g slot time)
1300 		 * Frames otherwise received are discarded.
1301 		 */
1302 		if (!((ic->ic_flags & IEEE80211_F_SCAN) || ni->ni_associd)) {
1303 			vap->iv_stats.is_rx_mgtdiscard++;
1304 			return;
1305 		}
1306 
1307 		/* Override RX channel as appropriate */
1308 		if (rxs != NULL) {
1309 			c = ieee80211_lookup_channel_rxstatus(vap, rxs);
1310 			if (c != NULL)
1311 				rxchan = c;
1312 		}
1313 
1314 		/* XXX probe response in sta mode when !scanning? */
1315 		if (ieee80211_parse_beacon(ni, m0, rxchan, &scan) != 0) {
1316 			if (! (ic->ic_flags & IEEE80211_F_SCAN))
1317 				vap->iv_stats.is_beacon_bad++;
1318 			return;
1319 		}
1320 
1321 		/*
1322 		 * Count frame now that we know it's to be processed.
1323 		 */
1324 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1325 			vap->iv_stats.is_rx_beacon++;		/* XXX remove */
1326 			IEEE80211_NODE_STAT(ni, rx_beacons);
1327 		} else
1328 			IEEE80211_NODE_STAT(ni, rx_proberesp);
1329 		/*
1330 		 * When operating in station mode, check for state updates.
1331 		 * Be careful to ignore beacons received while doing a
1332 		 * background scan.  We consider only 11g/WMM stuff right now.
1333 		 */
1334 		if (ni->ni_associd != 0 &&
1335 		    ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
1336 		     IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
1337 			/* record tsf of last beacon */
1338 			memcpy(ni->ni_tstamp.data, scan.tstamp,
1339 				sizeof(ni->ni_tstamp));
1340 			/* count beacon frame for s/w bmiss handling */
1341 			vap->iv_swbmiss_count++;
1342 			vap->iv_bmiss_count = 0;
1343 			if (ni->ni_erp != scan.erp) {
1344 				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1345 				    wh->i_addr2,
1346 				    "erp change: was 0x%x, now 0x%x",
1347 				    ni->ni_erp, scan.erp);
1348 				if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1349 				    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1350 					ic->ic_flags |= IEEE80211_F_USEPROT;
1351 				else
1352 					ic->ic_flags &= ~IEEE80211_F_USEPROT;
1353 				ni->ni_erp = scan.erp;
1354 				/* XXX statistic */
1355 				/* XXX driver notification */
1356 			}
1357 			if ((ni->ni_capinfo ^ scan.capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) {
1358 				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1359 				    wh->i_addr2,
1360 				    "capabilities change: was 0x%x, now 0x%x",
1361 				    ni->ni_capinfo, scan.capinfo);
1362 				/*
1363 				 * NB: we assume short preamble doesn't
1364 				 *     change dynamically
1365 				 */
1366 				ieee80211_set_shortslottime(ic,
1367 					IEEE80211_IS_CHAN_A(ic->ic_bsschan) ||
1368 					(scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1369 				ni->ni_capinfo = (ni->ni_capinfo &~ IEEE80211_CAPINFO_SHORT_SLOTTIME)
1370 					       | (scan.capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME);
1371 				/* XXX statistic */
1372 			}
1373 			if (scan.wme != NULL &&
1374 			    (ni->ni_flags & IEEE80211_NODE_QOS) &&
1375 			    ieee80211_parse_wmeparams(vap, scan.wme, wh) > 0)
1376 				ieee80211_wme_updateparams(vap);
1377 #ifdef IEEE80211_SUPPORT_SUPERG
1378 			if (scan.ath != NULL)
1379 				ieee80211_parse_athparams(ni, scan.ath, wh);
1380 #endif
1381 			if (scan.htcap != NULL && scan.htinfo != NULL &&
1382 			    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1383 				/* XXX state changes? */
1384 				if (ieee80211_ht_updateparams(ni,
1385 				    scan.htcap, scan.htinfo))
1386 					ht_state_change = 1;
1387 			}
1388 			if (scan.quiet)
1389 				ic->ic_set_quiet(ni, scan.quiet);
1390 
1391 			if (scan.tim != NULL) {
1392 				struct ieee80211_tim_ie *tim =
1393 				    (struct ieee80211_tim_ie *) scan.tim;
1394 				/*
1395 				 * XXX Check/debug this code; see if it's about
1396 				 * the right time to force the VAP awake if we
1397 				 * receive a frame destined for us?
1398 				 */
1399 				int aid = IEEE80211_AID(ni->ni_associd);
1400 				int ix = aid / NBBY;
1401 				int min = tim->tim_bitctl &~ 1;
1402 				int max = tim->tim_len + min - 4;
1403 				int tim_ucast = 0, tim_mcast = 0;
1404 
1405 				/*
1406 				 * Only do this for unicast traffic in the TIM
1407 				 * The multicast traffic notification for
1408 				 * the scan notification stuff should occur
1409 				 * differently.
1410 				 */
1411 				if (min <= ix && ix <= max &&
1412 				     isset(tim->tim_bitmap - min, aid)) {
1413 					tim_ucast = 1;
1414 				}
1415 
1416 				/*
1417 				 * Do a separate notification
1418 				 * for the multicast bit being set.
1419 				 */
1420 				if (tim->tim_bitctl & 1) {
1421 					tim_mcast = 1;
1422 				}
1423 
1424 				/*
1425 				 * If the TIM indicates there's traffic for
1426 				 * us then get us out of STA mode powersave.
1427 				 */
1428 				if (tim_ucast == 1) {
1429 
1430 					/*
1431 					 * Wake us out of SLEEP state if we're
1432 					 * in it; and if we're doing bgscan
1433 					 * then wake us out of STA powersave.
1434 					 */
1435 					ieee80211_sta_tim_notify(vap, 1);
1436 
1437 					/*
1438 					 * This is preventing us from
1439 					 * continuing a bgscan; because it
1440 					 * tricks the contbgscan()
1441 					 * routine to think there's always
1442 					 * traffic for us.
1443 					 *
1444 					 * I think we need both an RX and
1445 					 * TX ic_lastdata field.
1446 					 */
1447 					ic->ic_lastdata = ticks;
1448 				}
1449 
1450 				ni->ni_dtim_count = tim->tim_count;
1451 				ni->ni_dtim_period = tim->tim_period;
1452 			}
1453 			if (scan.csa != NULL &&
1454 			    (vap->iv_flags & IEEE80211_F_DOTH))
1455 				ieee80211_parse_csaparams(vap, scan.csa, wh);
1456 			else if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
1457 				/*
1458 				 * No CSA ie or 11h disabled, but a channel
1459 				 * switch is pending; drop out so we aren't
1460 				 * stuck in CSA state.  If the AP really is
1461 				 * moving we'll get a beacon miss and scan.
1462 				 */
1463 				IEEE80211_LOCK(ic);
1464 				ieee80211_csa_cancelswitch(ic);
1465 				IEEE80211_UNLOCK(ic);
1466 			}
1467 			/*
1468 			 * If scanning, pass the info to the scan module.
1469 			 * Otherwise, check if it's the right time to do
1470 			 * a background scan.  Background scanning must
1471 			 * be enabled and we must not be operating in the
1472 			 * turbo phase of dynamic turbo mode.  Then,
1473 			 * it's been a while since the last background
1474 			 * scan and if no data frames have come through
1475 			 * recently, kick off a scan.  Note that this
1476 			 * is the mechanism by which a background scan
1477 			 * is started _and_ continued each time we
1478 			 * return on-channel to receive a beacon from
1479 			 * our ap.
1480 			 */
1481 			if (ic->ic_flags & IEEE80211_F_SCAN) {
1482 				ieee80211_add_scan(vap, rxchan,
1483 				    &scan, wh, subtype, rssi, nf);
1484 			} else if (contbgscan(vap)) {
1485 				ieee80211_bg_scan(vap, 0);
1486 			} else if (startbgscan(vap)) {
1487 				vap->iv_stats.is_scan_bg++;
1488 #if 0
1489 				/* wakeup if we are sleeing */
1490 				ieee80211_set_pwrsave(vap, 0);
1491 #endif
1492 				ieee80211_bg_scan(vap, 0);
1493 			}
1494 
1495 			/*
1496 			 * Put the station to sleep if we haven't seen
1497 			 * traffic in a while.
1498 			 */
1499 			IEEE80211_LOCK(ic);
1500 			ieee80211_sta_ps_timer_check(vap);
1501 			IEEE80211_UNLOCK(ic);
1502 
1503 			/*
1504 			 * If we've had a channel width change (eg HT20<->HT40)
1505 			 * then schedule a delayed driver notification.
1506 			 */
1507 			if (ht_state_change)
1508 				ieee80211_update_chw(ic);
1509 			return;
1510 		}
1511 		/*
1512 		 * If scanning, just pass information to the scan module.
1513 		 */
1514 		if (ic->ic_flags & IEEE80211_F_SCAN) {
1515 			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1516 				/*
1517 				 * Actively scanning a channel marked passive;
1518 				 * send a probe request now that we know there
1519 				 * is 802.11 traffic present.
1520 				 *
1521 				 * XXX check if the beacon we recv'd gives
1522 				 * us what we need and suppress the probe req
1523 				 */
1524 				ieee80211_probe_curchan(vap, 1);
1525 				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1526 			}
1527 			ieee80211_add_scan(vap, rxchan, &scan, wh,
1528 			    subtype, rssi, nf);
1529 			return;
1530 		}
1531 		break;
1532 	}
1533 
1534 	case IEEE80211_FC0_SUBTYPE_AUTH: {
1535 		uint16_t algo, seq, status;
1536 		/*
1537 		 * auth frame format
1538 		 *	[2] algorithm
1539 		 *	[2] sequence
1540 		 *	[2] status
1541 		 *	[tlv*] challenge
1542 		 */
1543 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1544 		algo   = le16toh(*(uint16_t *)frm);
1545 		seq    = le16toh(*(uint16_t *)(frm + 2));
1546 		status = le16toh(*(uint16_t *)(frm + 4));
1547 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr2,
1548 		    "recv auth frame with algorithm %d seq %d", algo, seq);
1549 
1550 		if (vap->iv_flags & IEEE80211_F_COUNTERM) {
1551 			IEEE80211_DISCARD(vap,
1552 			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1553 			    wh, "auth", "%s", "TKIP countermeasures enabled");
1554 			vap->iv_stats.is_rx_auth_countermeasures++;
1555 			if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
1556 				ieee80211_send_error(ni, wh->i_addr2,
1557 					IEEE80211_FC0_SUBTYPE_AUTH,
1558 					IEEE80211_REASON_MIC_FAILURE);
1559 			}
1560 			return;
1561 		}
1562 		if (algo == IEEE80211_AUTH_ALG_SHARED)
1563 			sta_auth_shared(ni, wh, frm + 6, efrm, rssi, nf,
1564 			    seq, status);
1565 		else if (algo == IEEE80211_AUTH_ALG_OPEN)
1566 			sta_auth_open(ni, wh, rssi, nf, seq, status);
1567 		else {
1568 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1569 			    wh, "auth", "unsupported alg %d", algo);
1570 			vap->iv_stats.is_rx_auth_unsupported++;
1571 			return;
1572 		}
1573 		break;
1574 	}
1575 
1576 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1577 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
1578 		uint16_t capinfo, associd;
1579 		uint16_t status;
1580 
1581 		if (vap->iv_state != IEEE80211_S_ASSOC) {
1582 			vap->iv_stats.is_rx_mgtdiscard++;
1583 			return;
1584 		}
1585 
1586 		/*
1587 		 * asresp frame format
1588 		 *	[2] capability information
1589 		 *	[2] status
1590 		 *	[2] association ID
1591 		 *	[tlv] supported rates
1592 		 *	[tlv] extended supported rates
1593 		 *	[tlv] WME
1594 		 *	[tlv] HT capabilities
1595 		 *	[tlv] HT info
1596 		 */
1597 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6, return);
1598 		ni = vap->iv_bss;
1599 		capinfo = le16toh(*(uint16_t *)frm);
1600 		frm += 2;
1601 		status = le16toh(*(uint16_t *)frm);
1602 		frm += 2;
1603 		if (status != 0) {
1604 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1605 			    wh->i_addr2, "%sassoc failed (reason %d)",
1606 			    ISREASSOC(subtype) ?  "re" : "", status);
1607 			vap->iv_stats.is_rx_auth_fail++;	/* XXX */
1608 			return;
1609 		}
1610 		associd = le16toh(*(uint16_t *)frm);
1611 		frm += 2;
1612 
1613 		rates = xrates = wme = htcap = htinfo = NULL;
1614 		while (efrm - frm > 1) {
1615 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1616 			switch (*frm) {
1617 			case IEEE80211_ELEMID_RATES:
1618 				rates = frm;
1619 				break;
1620 			case IEEE80211_ELEMID_XRATES:
1621 				xrates = frm;
1622 				break;
1623 			case IEEE80211_ELEMID_HTCAP:
1624 				htcap = frm;
1625 				break;
1626 			case IEEE80211_ELEMID_HTINFO:
1627 				htinfo = frm;
1628 				break;
1629 			case IEEE80211_ELEMID_VENDOR:
1630 				if (iswmeoui(frm))
1631 					wme = frm;
1632 				else if (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) {
1633 					/*
1634 					 * Accept pre-draft HT ie's if the
1635 					 * standard ones have not been seen.
1636 					 */
1637 					if (ishtcapoui(frm)) {
1638 						if (htcap == NULL)
1639 							htcap = frm;
1640 					} else if (ishtinfooui(frm)) {
1641 						if (htinfo == NULL)
1642 							htinfo = frm;
1643 					}
1644 				}
1645 				/* XXX Atheros OUI support */
1646 				break;
1647 			}
1648 			frm += frm[1] + 2;
1649 		}
1650 
1651 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1652 		if (xrates != NULL)
1653 			IEEE80211_VERIFY_ELEMENT(xrates,
1654 				IEEE80211_RATE_MAXSIZE - rates[1], return);
1655 		rate = ieee80211_setup_rates(ni, rates, xrates,
1656 				IEEE80211_F_JOIN |
1657 				IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1658 				IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1659 		if (rate & IEEE80211_RATE_BASIC) {
1660 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ASSOC,
1661 			    wh->i_addr2,
1662 			    "%sassoc failed (rate set mismatch)",
1663 			    ISREASSOC(subtype) ?  "re" : "");
1664 			vap->iv_stats.is_rx_assoc_norate++;
1665 			ieee80211_new_state(vap, IEEE80211_S_SCAN,
1666 			    IEEE80211_SCAN_FAIL_STATUS);
1667 			return;
1668 		}
1669 
1670 		ni->ni_capinfo = capinfo;
1671 		ni->ni_associd = associd;
1672 		if (ni->ni_jointime == 0)
1673 			ni->ni_jointime = time_uptime;
1674 		if (wme != NULL &&
1675 		    ieee80211_parse_wmeparams(vap, wme, wh) >= 0) {
1676 			ni->ni_flags |= IEEE80211_NODE_QOS;
1677 			ieee80211_wme_updateparams(vap);
1678 		} else
1679 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
1680 		/*
1681 		 * Setup HT state according to the negotiation.
1682 		 *
1683 		 * NB: shouldn't need to check if HT use is enabled but some
1684 		 *     ap's send back HT ie's even when we don't indicate we
1685 		 *     are HT capable in our AssocReq.
1686 		 */
1687 		if (htcap != NULL && htinfo != NULL &&
1688 		    (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
1689 			ieee80211_ht_node_init(ni);
1690 			ieee80211_ht_updateparams(ni, htcap, htinfo);
1691 			ieee80211_setup_htrates(ni, htcap,
1692 			     IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1693 			ieee80211_setup_basic_htrates(ni, htinfo);
1694 			ieee80211_node_setuptxparms(ni);
1695 			ieee80211_ratectl_node_init(ni);
1696 		}
1697 
1698 		/*
1699 		 * Always initialise FF/superg state; we can use this
1700 		 * for doing A-MSDU encapsulation as well.
1701 		 */
1702 #ifdef	IEEE80211_SUPPORT_SUPERG
1703 		ieee80211_ff_node_init(ni);
1704 #endif
1705 
1706 		/*
1707 		 * Configure state now that we are associated.
1708 		 *
1709 		 * XXX may need different/additional driver callbacks?
1710 		 */
1711 		if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1712 		    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1713 			ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1714 			ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1715 		} else {
1716 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1717 			ic->ic_flags |= IEEE80211_F_USEBARKER;
1718 		}
1719 		ieee80211_set_shortslottime(ic,
1720 			IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
1721 			(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1722 		/*
1723 		 * Honor ERP protection.
1724 		 *
1725 		 * NB: ni_erp should zero for non-11g operation.
1726 		 */
1727 		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
1728 		    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1729 			ic->ic_flags |= IEEE80211_F_USEPROT;
1730 		else
1731 			ic->ic_flags &= ~IEEE80211_F_USEPROT;
1732 		IEEE80211_NOTE_MAC(vap,
1733 		    IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, wh->i_addr2,
1734 		    "%sassoc success at aid %d: %s preamble, %s slot time%s%s%s%s%s%s%s%s",
1735 		    ISREASSOC(subtype) ? "re" : "",
1736 		    IEEE80211_NODE_AID(ni),
1737 		    ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
1738 		    ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
1739 		    ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
1740 		    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
1741 		    ni->ni_flags & IEEE80211_NODE_HT ?
1742 			(ni->ni_chw == 40 ? ", HT40" : ", HT20") : "",
1743 		    ni->ni_flags & IEEE80211_NODE_AMPDU ? " (+AMPDU)" : "",
1744 		    ni->ni_flags & IEEE80211_NODE_MIMO_RTS ? " (+SMPS-DYN)" :
1745 			ni->ni_flags & IEEE80211_NODE_MIMO_PS ? " (+SMPS)" : "",
1746 		    ni->ni_flags & IEEE80211_NODE_RIFS ? " (+RIFS)" : "",
1747 		    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF) ?
1748 			", fast-frames" : "",
1749 		    IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_TURBOP) ?
1750 			", turbo" : ""
1751 		);
1752 		ieee80211_new_state(vap, IEEE80211_S_RUN, subtype);
1753 		break;
1754 	}
1755 
1756 	case IEEE80211_FC0_SUBTYPE_DEAUTH: {
1757 		uint16_t reason;
1758 
1759 		if (vap->iv_state == IEEE80211_S_SCAN) {
1760 			vap->iv_stats.is_rx_mgtdiscard++;
1761 			return;
1762 		}
1763 		if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1764 			/* NB: can happen when in promiscuous mode */
1765 			vap->iv_stats.is_rx_mgtdiscard++;
1766 			break;
1767 		}
1768 
1769 		/*
1770 		 * deauth frame format
1771 		 *	[2] reason
1772 		 */
1773 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1774 		reason = le16toh(*(uint16_t *)frm);
1775 
1776 		vap->iv_stats.is_rx_deauth++;
1777 		vap->iv_stats.is_rx_deauth_code = reason;
1778 		IEEE80211_NODE_STAT(ni, rx_deauth);
1779 
1780 		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1781 		    "recv deauthenticate (reason: %d (%s))", reason,
1782 		    ieee80211_reason_to_string(reason));
1783 		ieee80211_new_state(vap, IEEE80211_S_AUTH,
1784 		    (reason << 8) | IEEE80211_FC0_SUBTYPE_DEAUTH);
1785 		break;
1786 	}
1787 
1788 	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
1789 		uint16_t reason;
1790 
1791 		if (vap->iv_state != IEEE80211_S_RUN &&
1792 		    vap->iv_state != IEEE80211_S_ASSOC &&
1793 		    vap->iv_state != IEEE80211_S_AUTH) {
1794 			vap->iv_stats.is_rx_mgtdiscard++;
1795 			return;
1796 		}
1797 		if (!IEEE80211_ADDR_EQ(wh->i_addr1, vap->iv_myaddr)) {
1798 			/* NB: can happen when in promiscuous mode */
1799 			vap->iv_stats.is_rx_mgtdiscard++;
1800 			break;
1801 		}
1802 
1803 		/*
1804 		 * disassoc frame format
1805 		 *	[2] reason
1806 		 */
1807 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2, return);
1808 		reason = le16toh(*(uint16_t *)frm);
1809 
1810 		vap->iv_stats.is_rx_disassoc++;
1811 		vap->iv_stats.is_rx_disassoc_code = reason;
1812 		IEEE80211_NODE_STAT(ni, rx_disassoc);
1813 
1814 		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
1815 		    "recv disassociate (reason: %d (%s))", reason,
1816 		    ieee80211_reason_to_string(reason));
1817 		ieee80211_new_state(vap, IEEE80211_S_ASSOC, 0);
1818 		break;
1819 	}
1820 
1821 	case IEEE80211_FC0_SUBTYPE_ACTION:
1822 	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
1823 		if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
1824 		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1825 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1826 			    wh, NULL, "%s", "not for us");
1827 			vap->iv_stats.is_rx_mgtdiscard++;
1828 		} else if (vap->iv_state != IEEE80211_S_RUN) {
1829 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1830 			    wh, NULL, "wrong state %s",
1831 			    ieee80211_state_name[vap->iv_state]);
1832 			vap->iv_stats.is_rx_mgtdiscard++;
1833 		} else {
1834 			if (ieee80211_parse_action(ni, m0) == 0)
1835 				(void)ic->ic_recv_action(ni, wh, frm, efrm);
1836 		}
1837 		break;
1838 
1839 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1840 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1841 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1842 	case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
1843 	case IEEE80211_FC0_SUBTYPE_ATIM:
1844 		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1845 		    wh, NULL, "%s", "not handled");
1846 		vap->iv_stats.is_rx_mgtdiscard++;
1847 		break;
1848 
1849 	default:
1850 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1851 		    wh, "mgt", "subtype 0x%x not handled", subtype);
1852 		vap->iv_stats.is_rx_badsubtype++;
1853 		break;
1854 	}
1855 #undef ISREASSOC
1856 }
1857 
1858 static void
1859 sta_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
1860 {
1861 	switch (subtype) {
1862 	case IEEE80211_FC0_SUBTYPE_BAR:
1863 		ieee80211_recv_bar(ni, m);
1864 		break;
1865 	}
1866 }
1867