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