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