xref: /netbsd-src/sys/net80211/ieee80211_input.c (revision ba65fde2d7fefa7d39838fa5fa855e62bd606b5e)
1 /*	$NetBSD: ieee80211_input.c,v 1.73 2013/01/10 17:40:10 christos Exp $	*/
2 /*-
3  * Copyright (c) 2001 Atsushi Onoe
4  * Copyright (c) 2002-2005 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  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * Alternatively, this software may be distributed under the terms of the
19  * GNU General Public License ("GPL") version 2 as published by the Free
20  * Software Foundation.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 #ifdef __FreeBSD__
36 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_input.c,v 1.81 2005/08/10 16:22:29 sam Exp $");
37 #endif
38 #ifdef __NetBSD__
39 __KERNEL_RCSID(0, "$NetBSD: ieee80211_input.c,v 1.73 2013/01/10 17:40:10 christos Exp $");
40 #endif
41 
42 #include "opt_inet.h"
43 
44 #ifdef __NetBSD__
45 #endif /* __NetBSD__ */
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/mbuf.h>
50 #include <sys/malloc.h>
51 #include <sys/endian.h>
52 #include <sys/kernel.h>
53 
54 #include <sys/socket.h>
55 #include <sys/sockio.h>
56 #include <sys/endian.h>
57 #include <sys/errno.h>
58 #include <sys/proc.h>
59 #include <sys/sysctl.h>
60 
61 #include <net/if.h>
62 #include <net/if_media.h>
63 #include <net/if_arp.h>
64 #include <net/if_ether.h>
65 #include <net/if_llc.h>
66 
67 #include <net80211/ieee80211_netbsd.h>
68 #include <net80211/ieee80211_var.h>
69 
70 #include <net/bpf.h>
71 
72 #ifdef INET
73 #include <netinet/in.h>
74 #include <net/if_ether.h>
75 #endif
76 
77 const struct timeval ieee80211_merge_print_intvl = {.tv_sec = 1, .tv_usec = 0};
78 
79 #ifdef IEEE80211_DEBUG
80 
81 /*
82  * Decide if a received management frame should be
83  * printed when debugging is enabled.  This filters some
84  * of the less interesting frames that come frequently
85  * (e.g. beacons).
86  */
87 static __inline int
88 doprint(struct ieee80211com *ic, int subtype)
89 {
90 	switch (subtype) {
91 	case IEEE80211_FC0_SUBTYPE_BEACON:
92 		return (ic->ic_flags & IEEE80211_F_SCAN);
93 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
94 		return (ic->ic_opmode == IEEE80211_M_IBSS);
95 	}
96 	return 1;
97 }
98 
99 /*
100  * Emit a debug message about discarding a frame or information
101  * element.  One format is for extracting the mac address from
102  * the frame header; the other is for when a header is not
103  * available or otherwise appropriate.
104  */
105 #define	IEEE80211_DISCARD(_ic, _m, _wh, _type, _fmt, ...) do {		\
106 	if ((_ic)->ic_debug & (_m))					\
107 		ieee80211_discard_frame(_ic, _wh, _type, _fmt, __VA_ARGS__);\
108 } while (0)
109 #define	IEEE80211_DISCARD_IE(_ic, _m, _wh, _type, _fmt, ...) do {	\
110 	if ((_ic)->ic_debug & (_m))					\
111 		ieee80211_discard_ie(_ic, _wh, _type, _fmt, __VA_ARGS__);\
112 } while (0)
113 #define	IEEE80211_DISCARD_MAC(_ic, _m, _mac, _type, _fmt, ...) do {	\
114 	if ((_ic)->ic_debug & (_m))					\
115 		ieee80211_discard_mac(_ic, _mac, _type, _fmt, __VA_ARGS__);\
116 } while (0)
117 
118 static const u_int8_t *ieee80211_getbssid(struct ieee80211com *,
119 	const struct ieee80211_frame *);
120 static void ieee80211_discard_frame(struct ieee80211com *,
121 	const struct ieee80211_frame *, const char *type, const char *fmt, ...);
122 static void ieee80211_discard_ie(struct ieee80211com *,
123 	const struct ieee80211_frame *, const char *type, const char *fmt, ...);
124 static void ieee80211_discard_mac(struct ieee80211com *,
125 	const u_int8_t mac[IEEE80211_ADDR_LEN], const char *type,
126 	const char *fmt, ...);
127 #else
128 #define	IEEE80211_DISCARD(_ic, _m, _wh, _type, _fmt, ...)
129 #define	IEEE80211_DISCARD_IE(_ic, _m, _wh, _type, _fmt, ...)
130 #define	IEEE80211_DISCARD_MAC(_ic, _m, _mac, _type, _fmt, ...)
131 #endif /* IEEE80211_DEBUG */
132 
133 static struct mbuf *ieee80211_defrag(struct ieee80211com *,
134 	struct ieee80211_node *, struct mbuf *, int);
135 static struct mbuf *ieee80211_decap(struct ieee80211com *, struct mbuf *, int);
136 static void ieee80211_send_error(struct ieee80211com *, struct ieee80211_node *,
137 		const u_int8_t *mac, int subtype, int arg);
138 static void ieee80211_deliver_data(struct ieee80211com *,
139 	struct ieee80211_node *, struct mbuf *);
140 #ifndef IEEE80211_NO_HOSTAP
141 static void ieee80211_node_pwrsave(struct ieee80211_node *, int enable);
142 static void ieee80211_recv_pspoll(struct ieee80211com *,
143 	struct ieee80211_node *, struct mbuf *);
144 #endif /* !IEEE80211_NO_HOSTAP */
145 static void ieee80211_update_adhoc_node(struct ieee80211com *,
146     struct ieee80211_node *, struct ieee80211_frame *,
147     struct ieee80211_scanparams *, int, u_int32_t);
148 
149 /*
150  * Process a received frame.  The node associated with the sender
151  * should be supplied.  If nothing was found in the node table then
152  * the caller is assumed to supply a reference to ic_bss instead.
153  * The RSSI and a timestamp are also supplied.  The RSSI data is used
154  * during AP scanning to select a AP to associate with; it can have
155  * any units so long as values have consistent units and higher values
156  * mean ``better signal''.  The receive timestamp is currently not used
157  * by the 802.11 layer.
158  */
159 int
160 ieee80211_input(struct ieee80211com *ic, struct mbuf *m,
161 	struct ieee80211_node *ni, int rssi, u_int32_t rstamp)
162 {
163 #define	SEQ_LEQ(a,b)	((int)((a)-(b)) <= 0)
164 #define	HAS_SEQ(type)	((type & 0x4) == 0)
165 	struct ifnet *ifp = ic->ic_ifp;
166 	struct ieee80211_frame *wh;
167 	struct ieee80211_key *key;
168 	struct ether_header *eh;
169 	int hdrspace;
170 	u_int8_t dir, type, subtype;
171 	u_int8_t *bssid;
172 	u_int16_t rxseq;
173 
174 	IASSERT(ni != NULL, ("null node"));
175 	ni->ni_inact = ni->ni_inact_reload;
176 
177 	/* trim CRC here so WEP can find its own CRC at the end of packet. */
178 	if (m->m_flags & M_HASFCS) {
179 		m_adj(m, -IEEE80211_CRC_LEN);
180 		m->m_flags &= ~M_HASFCS;
181 	}
182 	type = -1;			/* undefined */
183 	/*
184 	 * In monitor mode, send everything directly to bpf.
185 	 * XXX may want to include the CRC
186 	 */
187 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
188 		goto out;
189 
190 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
191 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
192 		    ni->ni_macaddr, NULL,
193 		    "too short (1): len %u", m->m_pkthdr.len);
194 		ic->ic_stats.is_rx_tooshort++;
195 		goto out;
196 	}
197 	/*
198 	 * Bit of a cheat here, we use a pointer for a 3-address
199 	 * frame format but don't reference fields past outside
200 	 * ieee80211_frame_min w/o first validating the data is
201 	 * present.
202 	 */
203 	wh = mtod(m, struct ieee80211_frame *);
204 
205 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
206 	    IEEE80211_FC0_VERSION_0) {
207 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
208 		    ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
209 		ic->ic_stats.is_rx_badversion++;
210 		goto err;
211 	}
212 
213 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
214 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
215 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
216 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
217 		switch (ic->ic_opmode) {
218 		case IEEE80211_M_STA:
219 			bssid = wh->i_addr2;
220 			if (!IEEE80211_ADDR_EQ(bssid, ni->ni_bssid)) {
221 				/* not interested in */
222 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
223 				    bssid, NULL, "%s", "not to bss");
224 				ic->ic_stats.is_rx_wrongbss++;
225 				goto out;
226 			}
227 			break;
228 		case IEEE80211_M_IBSS:
229 		case IEEE80211_M_AHDEMO:
230 		case IEEE80211_M_HOSTAP:
231 			if (dir != IEEE80211_FC1_DIR_NODS)
232 				bssid = wh->i_addr1;
233 			else if (type == IEEE80211_FC0_TYPE_CTL)
234 				bssid = wh->i_addr1;
235 			else {
236 				if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
237 					IEEE80211_DISCARD_MAC(ic,
238 					    IEEE80211_MSG_ANY, ni->ni_macaddr,
239 					    NULL, "too short (2): len %u",
240 					    m->m_pkthdr.len);
241 					ic->ic_stats.is_rx_tooshort++;
242 					goto out;
243 				}
244 				bssid = wh->i_addr3;
245 			}
246 			if (type != IEEE80211_FC0_TYPE_DATA)
247 				break;
248 			/*
249 			 * Data frame, validate the bssid.
250 			 */
251 			if (!IEEE80211_ADDR_EQ(bssid, ic->ic_bss->ni_bssid) &&
252 			    !IEEE80211_ADDR_EQ(bssid, ifp->if_broadcastaddr)) {
253 				/* not interested in */
254 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
255 				    bssid, NULL, "%s", "not to bss");
256 				ic->ic_stats.is_rx_wrongbss++;
257 				goto out;
258 			}
259 			/*
260 			 * For adhoc mode we cons up a node when it doesn't
261 			 * exist. This should probably done after an ACL check.
262 			 */
263 			if (ni == ic->ic_bss &&
264 			    ic->ic_opmode != IEEE80211_M_HOSTAP &&
265 			    !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
266 				/*
267 				 * Fake up a node for this newly
268 				 * discovered member of the IBSS.
269 				 */
270 				ni = ieee80211_fakeup_adhoc_node(&ic->ic_sta,
271 						    wh->i_addr2);
272 				if (ni == NULL) {
273 					/* NB: stat kept for alloc failure */
274 					goto err;
275 				}
276 			}
277 			break;
278 		default:
279 			goto out;
280 		}
281 		ni->ni_rssi = rssi;
282 		ni->ni_rstamp = rstamp;
283 		if (HAS_SEQ(type)) {
284 			u_int8_t tid;
285 			if (IEEE80211_QOS_HAS_SEQ(wh)) {
286 				tid = ((struct ieee80211_qosframe *)wh)->
287 					i_qos[0] & IEEE80211_QOS_TID;
288 				if (TID_TO_WME_AC(tid) >= WME_AC_VI)
289 					ic->ic_wme.wme_hipri_traffic++;
290 				tid++;
291 			} else
292 				tid = 0;
293 			rxseq = le16toh(*(u_int16_t *)wh->i_seq);
294 			if ((wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
295 			    SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
296 				/* duplicate, discard */
297 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
298 				    bssid, "duplicate",
299 				    "seqno <%u,%u> fragno <%u,%u> tid %u",
300 				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
301 				    ni->ni_rxseqs[tid] >>
302 					IEEE80211_SEQ_SEQ_SHIFT,
303 				    rxseq & IEEE80211_SEQ_FRAG_MASK,
304 				    ni->ni_rxseqs[tid] &
305 					IEEE80211_SEQ_FRAG_MASK,
306 				    tid);
307 				ic->ic_stats.is_rx_dup++;
308 				IEEE80211_NODE_STAT(ni, rx_dup);
309 				goto out;
310 			}
311 			ni->ni_rxseqs[tid] = rxseq;
312 		}
313 	}
314 
315 	switch (type) {
316 	case IEEE80211_FC0_TYPE_DATA:
317 		hdrspace = ieee80211_hdrspace(ic, wh);
318 		if (m->m_len < hdrspace &&
319 		    (m = m_pullup(m, hdrspace)) == NULL) {
320 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
321 			    ni->ni_macaddr, NULL,
322 			    "data too short: expecting %u", hdrspace);
323 			ic->ic_stats.is_rx_tooshort++;
324 			goto out;		/* XXX */
325 		}
326 		switch (ic->ic_opmode) {
327 		case IEEE80211_M_STA:
328 			if (dir != IEEE80211_FC1_DIR_FROMDS) {
329 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
330 				    wh, "data", "%s", "unknown dir 0x%x", dir);
331 				ic->ic_stats.is_rx_wrongdir++;
332 				goto out;
333 			}
334 			if ((ifp->if_flags & IFF_SIMPLEX) &&
335 			    IEEE80211_IS_MULTICAST(wh->i_addr1) &&
336 			    IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_myaddr)) {
337 				/*
338 				 * In IEEE802.11 network, multicast packet
339 				 * sent from me is broadcasted from AP.
340 				 * It should be silently discarded for
341 				 * SIMPLEX interface.
342 				 */
343 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
344 				    wh, NULL, "%s", "multicast echo");
345 				ic->ic_stats.is_rx_mcastecho++;
346 				goto out;
347 			}
348 			break;
349 		case IEEE80211_M_IBSS:
350 		case IEEE80211_M_AHDEMO:
351 			if (dir != IEEE80211_FC1_DIR_NODS) {
352 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
353 				    wh, "data", "%s", "unknown dir 0x%x", dir);
354 				ic->ic_stats.is_rx_wrongdir++;
355 				goto out;
356 			}
357 			/* XXX no power-save support */
358 			break;
359 		case IEEE80211_M_HOSTAP:
360 #ifndef IEEE80211_NO_HOSTAP
361 			if (dir != IEEE80211_FC1_DIR_TODS) {
362 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
363 				    wh, "data", "%s", "unknown dir 0x%x", dir);
364 				ic->ic_stats.is_rx_wrongdir++;
365 				goto out;
366 			}
367 			/* check if source STA is associated */
368 			if (ni == ic->ic_bss) {
369 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
370 				    wh, "data", "%s", "unknown src");
371 				ieee80211_send_error(ic, ni, wh->i_addr2,
372 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
373 				    IEEE80211_REASON_NOT_AUTHED);
374 				ic->ic_stats.is_rx_notassoc++;
375 				goto err;
376 			}
377 			if (ni->ni_associd == 0) {
378 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
379 				    wh, "data", "%s", "unassoc src");
380 				IEEE80211_SEND_MGMT(ic, ni,
381 				    IEEE80211_FC0_SUBTYPE_DISASSOC,
382 				    IEEE80211_REASON_NOT_ASSOCED);
383 				ic->ic_stats.is_rx_notassoc++;
384 				goto err;
385 			}
386 
387 			/*
388 			 * Check for power save state change.
389 			 */
390 			if (((wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) ^
391 			    (ni->ni_flags & IEEE80211_NODE_PWR_MGT)))
392 				ieee80211_node_pwrsave(ni,
393 					wh->i_fc[1] & IEEE80211_FC1_PWR_MGT);
394 #endif /* !IEEE80211_NO_HOSTAP */
395 			break;
396 		default:
397 			/* XXX here to keep compiler happy */
398 			goto out;
399 		}
400 
401 		/*
402 		 * Handle privacy requirements.  Note that we
403 		 * must not be preempted from here until after
404 		 * we (potentially) call ieee80211_crypto_demic;
405 		 * otherwise we may violate assumptions in the
406 		 * crypto cipher modules used to do delayed update
407 		 * of replay sequence numbers.
408 		 */
409 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
410 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
411 				/*
412 				 * Discard encrypted frames when privacy is off.
413 				 */
414 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
415 				    wh, "WEP", "%s", "PRIVACY off");
416 				ic->ic_stats.is_rx_noprivacy++;
417 				IEEE80211_NODE_STAT(ni, rx_noprivacy);
418 				goto out;
419 			}
420 			key = ieee80211_crypto_decap(ic, ni, m, hdrspace);
421 			if (key == NULL) {
422 				/* NB: stats+msgs handled in crypto_decap */
423 				IEEE80211_NODE_STAT(ni, rx_wepfail);
424 				goto out;
425 			}
426 			wh = mtod(m, struct ieee80211_frame *);
427 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
428 		} else {
429 			key = NULL;
430 		}
431 
432 		/*
433 		 * Next up, any fragmentation.
434 		 */
435 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
436 			m = ieee80211_defrag(ic, ni, m, hdrspace);
437 			if (m == NULL) {
438 				/* Fragment dropped or frame not complete yet */
439 				goto out;
440 			}
441 		}
442 		wh = NULL;		/* no longer valid, catch any uses */
443 
444 		/*
445 		 * Next strip any MSDU crypto bits.
446 		 */
447 		if (key != NULL && !ieee80211_crypto_demic(ic, key, m, 0)) {
448 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
449 			    ni->ni_macaddr, "data", "%s", "demic error");
450 			IEEE80211_NODE_STAT(ni, rx_demicfail);
451 			goto out;
452 		}
453 
454 		/* copy to listener after decrypt */
455 		bpf_mtap3(ic->ic_rawbpf, m);
456 
457 		/*
458 		 * Finally, strip the 802.11 header.
459 		 */
460 		m = ieee80211_decap(ic, m, hdrspace);
461 		if (m == NULL) {
462 			/* don't count Null data frames as errors */
463 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA)
464 				goto out;
465 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
466 			    ni->ni_macaddr, "data", "%s", "decap error");
467 			ic->ic_stats.is_rx_decap++;
468 			IEEE80211_NODE_STAT(ni, rx_decap);
469 			goto err;
470 		}
471 		eh = mtod(m, struct ether_header *);
472 		if (!ieee80211_node_is_authorized(ni)) {
473 			/*
474 			 * Deny any non-PAE frames received prior to
475 			 * authorization.  For open/shared-key
476 			 * authentication the port is mark authorized
477 			 * after authentication completes.  For 802.1x
478 			 * the port is not marked authorized by the
479 			 * authenticator until the handshake has completed.
480 			 */
481 			if (eh->ether_type != htons(ETHERTYPE_PAE)) {
482 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_INPUT,
483 				    eh->ether_shost, "data",
484 				    "unauthorized port: ether type 0x%x len %u",
485 				    eh->ether_type, m->m_pkthdr.len);
486 				ic->ic_stats.is_rx_unauth++;
487 				IEEE80211_NODE_STAT(ni, rx_unauth);
488 				goto err;
489 			}
490 		} else {
491 			/*
492 			 * When denying unencrypted frames, discard
493 			 * any non-PAE frames received without encryption.
494 			 */
495 			if ((ic->ic_flags & IEEE80211_F_DROPUNENC) &&
496 			    key == NULL &&
497 			    eh->ether_type != htons(ETHERTYPE_PAE)) {
498 				/*
499 				 * Drop unencrypted frames.
500 				 */
501 				ic->ic_stats.is_rx_unencrypted++;
502 				IEEE80211_NODE_STAT(ni, rx_unencrypted);
503 				goto out;
504 			}
505 		}
506 		ifp->if_ipackets++;
507 		IEEE80211_NODE_STAT(ni, rx_data);
508 		IEEE80211_NODE_STAT_ADD(ni, rx_bytes, m->m_pkthdr.len);
509 
510 		ieee80211_deliver_data(ic, ni, m);
511 		return IEEE80211_FC0_TYPE_DATA;
512 
513 	case IEEE80211_FC0_TYPE_MGT:
514 		IEEE80211_NODE_STAT(ni, rx_mgmt);
515 		if (dir != IEEE80211_FC1_DIR_NODS) {
516 			IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
517 			    wh, "data", "%s", "unknown dir 0x%x", dir);
518 			ic->ic_stats.is_rx_wrongdir++;
519 			goto err;
520 		}
521 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
522 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_ANY,
523 			    ni->ni_macaddr, "mgt", "too short: len %u",
524 			    m->m_pkthdr.len);
525 			ic->ic_stats.is_rx_tooshort++;
526 			goto out;
527 		}
528 #ifdef IEEE80211_DEBUG
529 		if ((ieee80211_msg_debug(ic) && doprint(ic, subtype)) ||
530 		    ieee80211_msg_dumppkts(ic)) {
531 			if_printf(ic->ic_ifp, "received %s from %s rssi %d\n",
532 			    ieee80211_mgt_subtype_name[subtype >>
533 				IEEE80211_FC0_SUBTYPE_SHIFT],
534 			    ether_sprintf(wh->i_addr2), rssi);
535 		}
536 #endif
537 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
538 			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
539 				/*
540 				 * Only shared key auth frames with a challenge
541 				 * should be encrypted, discard all others.
542 				 */
543 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
544 				    wh, ieee80211_mgt_subtype_name[subtype >>
545 					IEEE80211_FC0_SUBTYPE_SHIFT],
546 				    "%s", "WEP set but not permitted");
547 				ic->ic_stats.is_rx_mgtdiscard++; /* XXX */
548 				goto out;
549 			}
550 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
551 				/*
552 				 * Discard encrypted frames when privacy is off.
553 				 */
554 				IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
555 				    wh, "mgt", "%s", "WEP set but PRIVACY off");
556 				ic->ic_stats.is_rx_noprivacy++;
557 				goto out;
558 			}
559 			hdrspace = ieee80211_hdrspace(ic, wh);
560 			key = ieee80211_crypto_decap(ic, ni, m, hdrspace);
561 			if (key == NULL) {
562 				/* NB: stats+msgs handled in crypto_decap */
563 				goto out;
564 			}
565 			wh = mtod(m, struct ieee80211_frame *);
566 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
567 		}
568 		bpf_mtap3(ic->ic_rawbpf, m);
569 		(*ic->ic_recv_mgmt)(ic, m, ni, subtype, rssi, rstamp);
570 		m_freem(m);
571 		return type;
572 
573 	case IEEE80211_FC0_TYPE_CTL:
574 		IEEE80211_NODE_STAT(ni, rx_ctrl);
575 		ic->ic_stats.is_rx_ctl++;
576 #ifndef IEEE80211_NO_HOSTAP
577 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
578 			switch (subtype) {
579 			case IEEE80211_FC0_SUBTYPE_PS_POLL:
580 				ieee80211_recv_pspoll(ic, ni, m);
581 				break;
582 			}
583 		}
584 #endif /* !IEEE80211_NO_HOSTAP */
585 		goto out;
586 	default:
587 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
588 		    wh, NULL, "bad frame type 0x%x", type);
589 		/* should not come here */
590 		break;
591 	}
592 err:
593 	ifp->if_ierrors++;
594 out:
595 	if (m != NULL) {
596 		bpf_mtap3(ic->ic_rawbpf, m);
597 		m_freem(m);
598 	}
599 	return type;
600 #undef SEQ_LEQ
601 }
602 
603 /*
604  * This function reassemble fragments.
605  */
606 static struct mbuf *
607 ieee80211_defrag(struct ieee80211com *ic, struct ieee80211_node *ni,
608 	struct mbuf *m, int hdrspace)
609 {
610 	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
611 	struct ieee80211_frame *lwh;
612 	u_int16_t rxseq;
613 	u_int8_t fragno;
614 	u_int8_t more_frag = wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG;
615 	struct mbuf *mfrag;
616 
617 	IASSERT(!IEEE80211_IS_MULTICAST(wh->i_addr1), ("multicast fragm?"));
618 
619 	rxseq = le16toh(*(u_int16_t *)wh->i_seq);
620 	fragno = rxseq & IEEE80211_SEQ_FRAG_MASK;
621 
622 	/* Quick way out, if there's nothing to defragment */
623 	if (!more_frag && fragno == 0 && ni->ni_rxfrag[0] == NULL)
624 		return m;
625 
626 	/*
627 	 * Remove frag to insure it doesn't get reaped by timer.
628 	 */
629 	if (ni->ni_table == NULL) {
630 		/*
631 		 * Should never happen.  If the node is orphaned (not in
632 		 * the table) then input packets should not reach here.
633 		 * Otherwise, a concurrent request that yanks the table
634 		 * should be blocked by other interlocking and/or by first
635 		 * shutting the driver down.  Regardless, be defensive
636 		 * here and just bail
637 		 */
638 		/* XXX need msg+stat */
639 		m_freem(m);
640 		return NULL;
641 	}
642 	IEEE80211_NODE_LOCK(ni->ni_table);
643 	mfrag = ni->ni_rxfrag[0];
644 	ni->ni_rxfrag[0] = NULL;
645 	IEEE80211_NODE_UNLOCK(ni->ni_table);
646 
647 	/*
648 	 * Validate new fragment is in order and
649 	 * related to the previous ones.
650 	 */
651 	if (mfrag != NULL) {
652 		u_int16_t last_rxseq;
653 
654 		lwh = mtod(mfrag, struct ieee80211_frame *);
655 		last_rxseq = le16toh(*(u_int16_t *)lwh->i_seq);
656 		/* NB: check seq # and frag together */
657 		if (rxseq != last_rxseq+1 ||
658 		    !IEEE80211_ADDR_EQ(wh->i_addr1, lwh->i_addr1) ||
659 		    !IEEE80211_ADDR_EQ(wh->i_addr2, lwh->i_addr2)) {
660 			/*
661 			 * Unrelated fragment or no space for it,
662 			 * clear current fragments.
663 			 */
664 			m_freem(mfrag);
665 			mfrag = NULL;
666 		}
667 	}
668 
669  	if (mfrag == NULL) {
670 		if (fragno != 0) {		/* !first fragment, discard */
671 			IEEE80211_NODE_STAT(ni, rx_defrag);
672 			m_freem(m);
673 			return NULL;
674 		}
675 		mfrag = m;
676 	} else {				/* concatenate */
677 		m_adj(m, hdrspace);		/* strip header */
678 		m_cat(mfrag, m);
679 		/* NB: m_cat doesn't update the packet header */
680 		mfrag->m_pkthdr.len += m->m_pkthdr.len;
681 		/* track last seqnum and fragno */
682 		lwh = mtod(mfrag, struct ieee80211_frame *);
683 		*(u_int16_t *) lwh->i_seq = *(u_int16_t *) wh->i_seq;
684 	}
685 	if (more_frag) {			/* more to come, save */
686 		ni->ni_rxfragstamp = ticks;
687 		ni->ni_rxfrag[0] = mfrag;
688 		mfrag = NULL;
689 	}
690 	return mfrag;
691 }
692 
693 static void
694 ieee80211_deliver_data(struct ieee80211com *ic,
695 	struct ieee80211_node *ni, struct mbuf *m)
696 {
697 	struct ether_header *eh = mtod(m, struct ether_header *);
698 	struct ifnet *ifp = ic->ic_ifp;
699 	ALTQ_DECL(struct altq_pktattr pktattr;)
700 	int error;
701 
702 	/* perform as a bridge within the AP */
703 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
704 	    (ic->ic_flags & IEEE80211_F_NOBRIDGE) == 0) {
705 		struct mbuf *m1 = NULL;
706 
707 		if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
708 			m1 = m_copypacket(m, M_DONTWAIT);
709 			if (m1 == NULL)
710 				ifp->if_oerrors++;
711 			else
712 				m1->m_flags |= M_MCAST;
713 		} else {
714 			/*
715 			 * Check if the destination is known; if so
716 			 * and the port is authorized dispatch directly.
717 			 */
718 			struct ieee80211_node *sta =
719 			    ieee80211_find_node(&ic->ic_sta, eh->ether_dhost);
720 			if (sta != NULL) {
721 				if (ieee80211_node_is_authorized(sta)) {
722 					/*
723 					 * Beware of sending to ourself; this
724 					 * needs to happen via the normal
725 					 * input path.
726 					 */
727 					if (sta != ic->ic_bss) {
728 						m1 = m;
729 						m = NULL;
730 					}
731 				} else {
732 					ic->ic_stats.is_rx_unauth++;
733 					IEEE80211_NODE_STAT(sta, rx_unauth);
734 				}
735 				ieee80211_free_node(sta);
736 			}
737 		}
738 		if (m1 != NULL) {
739 			int len;
740 #ifdef ALTQ
741 			if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
742 				altq_etherclassify(&ifp->if_snd, m1,
743 				    &pktattr);
744 			}
745 #endif
746 			len = m1->m_pkthdr.len;
747 			IFQ_ENQUEUE(&ifp->if_snd, m1, &pktattr, error);
748 			if (error) {
749 				ifp->if_omcasts++;
750 				m = NULL;
751 			}
752 			ifp->if_obytes += len;
753 		}
754 	}
755 	if (m != NULL) {
756 		/*
757 		 * XXX If we forward packet into transmitter of the AP,
758 		 * we don't need to duplicate for DLT_EN10MB.
759 		 */
760 		bpf_mtap(ifp, m);
761 
762 		if (ni->ni_vlan != 0) {
763 			/* attach vlan tag */
764 			/* XXX goto err? */
765 			VLAN_INPUT_TAG(ifp, m, ni->ni_vlan, goto out);
766 		}
767 		(*ifp->if_input)(ifp, m);
768 	}
769 	return;
770   out:
771 	if (m != NULL) {
772 		bpf_mtap3(ic->ic_rawbpf, m);
773 		m_freem(m);
774 	}
775 }
776 
777 static struct mbuf *
778 ieee80211_decap(struct ieee80211com *ic, struct mbuf *m, int hdrlen)
779 {
780 	struct ieee80211_qosframe_addr4 wh;	/* Max size address frames */
781 	struct ether_header *eh;
782 	struct llc *llc;
783 
784 	if (m->m_len < hdrlen + sizeof(*llc) &&
785 	    (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
786 		/* XXX stat, msg */
787 		return NULL;
788 	}
789 	memcpy(&wh, mtod(m, void *), hdrlen);
790 	llc = (struct llc *)(mtod(m, char *) + hdrlen);
791 	if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
792 	    llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
793 	    llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0) {
794 		m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
795 		llc = NULL;
796 	} else {
797 		m_adj(m, hdrlen - sizeof(*eh));
798 	}
799 	eh = mtod(m, struct ether_header *);
800 	switch (wh.i_fc[1] & IEEE80211_FC1_DIR_MASK) {
801 	case IEEE80211_FC1_DIR_NODS:
802 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
803 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
804 		break;
805 	case IEEE80211_FC1_DIR_TODS:
806 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3);
807 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
808 		break;
809 	case IEEE80211_FC1_DIR_FROMDS:
810 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
811 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr3);
812 		break;
813 	case IEEE80211_FC1_DIR_DSTODS:
814 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3);
815 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr4);
816 		break;
817 	}
818 #ifdef ALIGNED_POINTER
819 	if (!ALIGNED_POINTER(mtod(m, char *) + sizeof(*eh), u_int32_t)) {
820 		struct mbuf *n, *n0, **np;
821 		char *newdata;
822 		int off, pktlen;
823 
824 		n0 = NULL;
825 		np = &n0;
826 		off = 0;
827 		pktlen = m->m_pkthdr.len;
828 		while (pktlen > off) {
829 			if (n0 == NULL) {
830 				MGETHDR(n, M_DONTWAIT, MT_DATA);
831 				if (n == NULL) {
832 					m_freem(m);
833 					return NULL;
834 				}
835 				M_MOVE_PKTHDR(n, m);
836 				n->m_len = MHLEN;
837 			} else {
838 				MGET(n, M_DONTWAIT, MT_DATA);
839 				if (n == NULL) {
840 					m_freem(m);
841 					m_freem(n0);
842 					return NULL;
843 				}
844 				n->m_len = MLEN;
845 			}
846 			if (pktlen - off >= MINCLSIZE) {
847 				MCLGET(n, M_DONTWAIT);
848 				if (n->m_flags & M_EXT)
849 					n->m_len = n->m_ext.ext_size;
850 			}
851 			if (n0 == NULL) {
852 				newdata =
853 				    (char *)ALIGN(n->m_data + sizeof(*eh)) -
854 				    sizeof(*eh);
855 				n->m_len -= newdata - n->m_data;
856 				n->m_data = newdata;
857 			}
858 			if (n->m_len > pktlen - off)
859 				n->m_len = pktlen - off;
860 			m_copydata(m, off, n->m_len, mtod(n, void *));
861 			off += n->m_len;
862 			*np = n;
863 			np = &n->m_next;
864 		}
865 		m_freem(m);
866 		m = n0;
867 	}
868 #endif /* ALIGNED_POINTER */
869 	if (llc != NULL) {
870 		eh = mtod(m, struct ether_header *);
871 		eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
872 	}
873 	return m;
874 }
875 
876 /*
877  * Install received rate set information in the node's state block.
878  */
879 int
880 ieee80211_setup_rates(struct ieee80211_node *ni,
881 	const u_int8_t *rates, const u_int8_t *xrates, int flags)
882 {
883 	struct ieee80211com *ic = ni->ni_ic;
884 	struct ieee80211_rateset *rs = &ni->ni_rates;
885 
886 	memset(rs, 0, sizeof(*rs));
887 	rs->rs_nrates = rates[1];
888 	memcpy(rs->rs_rates, rates + 2, rs->rs_nrates);
889 	if (xrates != NULL) {
890 		u_int8_t nxrates;
891 		/*
892 		 * Tack on 11g extended supported rate element.
893 		 */
894 		nxrates = xrates[1];
895 		if (rs->rs_nrates + nxrates > IEEE80211_RATE_MAXSIZE) {
896 			nxrates = IEEE80211_RATE_MAXSIZE - rs->rs_nrates;
897 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_XRATE,
898 			     "[%s] extended rate set too large;"
899 			     " only using %u of %u rates\n",
900 			     ether_sprintf(ni->ni_macaddr), nxrates, xrates[1]);
901 			ic->ic_stats.is_rx_rstoobig++;
902 		}
903 		memcpy(rs->rs_rates + rs->rs_nrates, xrates+2, nxrates);
904 		rs->rs_nrates += nxrates;
905 	}
906 	return ieee80211_fix_rate(ni, flags);
907 }
908 
909 static void
910 ieee80211_auth_open(struct ieee80211com *ic, struct ieee80211_frame *wh,
911     struct ieee80211_node *ni, int rssi, u_int32_t rstamp,
912     u_int16_t seq, u_int16_t status)
913 {
914 
915 	if (ni->ni_authmode == IEEE80211_AUTH_SHARED) {
916 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
917 		    ni->ni_macaddr, "open auth",
918 		    "bad sta auth mode %u", ni->ni_authmode);
919 		ic->ic_stats.is_rx_bad_auth++;	/* XXX */
920 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
921 			/* XXX hack to workaround calling convention */
922 			ieee80211_send_error(ic, ni, wh->i_addr2,
923 			    IEEE80211_FC0_SUBTYPE_AUTH,
924 			    (seq + 1) | (IEEE80211_STATUS_ALG<<16));
925 		}
926 		return;
927 	}
928 	switch (ic->ic_opmode) {
929 	case IEEE80211_M_IBSS:
930 	case IEEE80211_M_AHDEMO:
931 	case IEEE80211_M_MONITOR:
932 		/* should not come here */
933 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
934 		    ni->ni_macaddr, "open auth",
935 		    "bad operating mode %u", ic->ic_opmode);
936 		break;
937 
938 	case IEEE80211_M_HOSTAP:
939 #ifndef IEEE80211_NO_HOSTAP
940 		if (ic->ic_state != IEEE80211_S_RUN ||
941 		    seq != IEEE80211_AUTH_OPEN_REQUEST) {
942 			ic->ic_stats.is_rx_bad_auth++;
943 			return;
944 		}
945 		/* always accept open authentication requests */
946 		if (ni == ic->ic_bss) {
947 			ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);
948 			if (ni == NULL)
949 				return;
950 		} else if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
951 			(void) ieee80211_ref_node(ni);
952 		/*
953 		 * Mark the node as referenced to reflect that it's
954 		 * reference count has been bumped to insure it remains
955 		 * after the transaction completes.
956 		 */
957 		ni->ni_flags |= IEEE80211_NODE_AREF;
958 
959 		IEEE80211_SEND_MGMT(ic, ni,
960 			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
961 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
962 		    "[%s] station authenticated (open)\n",
963 		    ether_sprintf(ni->ni_macaddr));
964 		/*
965 		 * When 802.1x is not in use mark the port
966 		 * authorized at this point so traffic can flow.
967 		 */
968 		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
969 			ieee80211_node_authorize(ni);
970 #endif /* !IEEE80211_NO_HOSTAP */
971 		break;
972 
973 	case IEEE80211_M_STA:
974 		if (ic->ic_state != IEEE80211_S_AUTH ||
975 		    seq != IEEE80211_AUTH_OPEN_RESPONSE) {
976 			ic->ic_stats.is_rx_bad_auth++;
977 			return;
978 		}
979 		if (status != 0) {
980 			IEEE80211_DPRINTF(ic,
981 			    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
982 			    "[%s] open auth failed (reason %d)\n",
983 			    ether_sprintf(ni->ni_macaddr), status);
984 			/* XXX can this happen? */
985 			if (ni != ic->ic_bss)
986 				ni->ni_fails++;
987 			ic->ic_stats.is_rx_auth_fail++;
988 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
989 		} else
990 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
991 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
992 		break;
993 	}
994 }
995 
996 /*
997  * Send a management frame error response to the specified
998  * station.  If ni is associated with the station then use
999  * it; otherwise allocate a temporary node suitable for
1000  * transmitting the frame and then free the reference so
1001  * it will go away as soon as the frame has been transmitted.
1002  */
1003 static void
1004 ieee80211_send_error(struct ieee80211com *ic, struct ieee80211_node *ni,
1005 	const u_int8_t *mac, int subtype, int arg)
1006 {
1007 	int istmp;
1008 
1009 	if (ni == ic->ic_bss) {
1010 		ni = ieee80211_tmp_node(ic, mac);
1011 		if (ni == NULL) {
1012 			/* XXX msg */
1013 			return;
1014 		}
1015 		istmp = 1;
1016 	} else
1017 		istmp = 0;
1018 	IEEE80211_SEND_MGMT(ic, ni, subtype, arg);
1019 	if (istmp)
1020 		ieee80211_free_node(ni);
1021 }
1022 
1023 static int
1024 alloc_challenge(struct ieee80211com *ic, struct ieee80211_node *ni)
1025 {
1026 	if (ni->ni_challenge == NULL)
1027 		ni->ni_challenge = malloc(IEEE80211_CHALLENGE_LEN,
1028 		    M_DEVBUF, M_NOWAIT);
1029 	if (ni->ni_challenge == NULL) {
1030 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1031 		    "[%s] shared key challenge alloc failed\n",
1032 		    ether_sprintf(ni->ni_macaddr));
1033 		/* XXX statistic */
1034 	}
1035 	return (ni->ni_challenge != NULL);
1036 }
1037 
1038 /* XXX TODO: add statistics */
1039 static void
1040 ieee80211_auth_shared(struct ieee80211com *ic, struct ieee80211_frame *wh,
1041     u_int8_t *frm, u_int8_t *efrm, struct ieee80211_node *ni, int rssi,
1042     u_int32_t rstamp, u_int16_t seq, u_int16_t status)
1043 {
1044 	u_int8_t *challenge;
1045 	int estatus;
1046 
1047 	/*
1048 	 * NB: this can happen as we allow pre-shared key
1049 	 * authentication to be enabled w/o wep being turned
1050 	 * on so that configuration of these can be done
1051 	 * in any order.  It may be better to enforce the
1052 	 * ordering in which case this check would just be
1053 	 * for sanity/consistency.
1054 	 */
1055 	if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
1056 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1057 		    ni->ni_macaddr, "shared key auth",
1058 		    "%s", " PRIVACY is disabled");
1059 		estatus = IEEE80211_STATUS_ALG;
1060 		goto bad;
1061 	}
1062 	/*
1063 	 * Pre-shared key authentication is evil; accept
1064 	 * it only if explicitly configured (it is supported
1065 	 * mainly for compatibility with clients like OS X).
1066 	 */
1067 	if (ni->ni_authmode != IEEE80211_AUTH_AUTO &&
1068 	    ni->ni_authmode != IEEE80211_AUTH_SHARED) {
1069 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1070 		    ni->ni_macaddr, "shared key auth",
1071 		    "bad sta auth mode %u", ni->ni_authmode);
1072 		ic->ic_stats.is_rx_bad_auth++;	/* XXX maybe a unique error? */
1073 		estatus = IEEE80211_STATUS_ALG;
1074 		goto bad;
1075 	}
1076 
1077 	challenge = NULL;
1078 	if (frm + 1 < efrm) {
1079 		if ((frm[1] + 2) > (efrm - frm)) {
1080 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1081 			    ni->ni_macaddr, "shared key auth",
1082 			    "ie %d/%d too long",
1083 			    frm[0], (frm[1] + 2) - (efrm - frm));
1084 			ic->ic_stats.is_rx_bad_auth++;
1085 			estatus = IEEE80211_STATUS_CHALLENGE;
1086 			goto bad;
1087 		}
1088 		if (*frm == IEEE80211_ELEMID_CHALLENGE)
1089 			challenge = frm;
1090 		frm += frm[1] + 2;
1091 	}
1092 	switch (seq) {
1093 	case IEEE80211_AUTH_SHARED_CHALLENGE:
1094 	case IEEE80211_AUTH_SHARED_RESPONSE:
1095 		if (challenge == NULL) {
1096 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1097 			    ni->ni_macaddr, "shared key auth",
1098 			    "%s", "no challenge");
1099 			ic->ic_stats.is_rx_bad_auth++;
1100 			estatus = IEEE80211_STATUS_CHALLENGE;
1101 			goto bad;
1102 		}
1103 		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
1104 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1105 			    ni->ni_macaddr, "shared key auth",
1106 			    "bad challenge len %d", challenge[1]);
1107 			ic->ic_stats.is_rx_bad_auth++;
1108 			estatus = IEEE80211_STATUS_CHALLENGE;
1109 			goto bad;
1110 		}
1111 	default:
1112 		break;
1113 	}
1114 	switch (ic->ic_opmode) {
1115 	case IEEE80211_M_MONITOR:
1116 	case IEEE80211_M_AHDEMO:
1117 	case IEEE80211_M_IBSS:
1118 		IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1119 		    ni->ni_macaddr, "shared key auth",
1120 		    "bad operating mode %u", ic->ic_opmode);
1121 		return;
1122 	case IEEE80211_M_HOSTAP:
1123 #ifndef IEEE80211_NO_HOSTAP
1124 	{
1125 		int allocbs;
1126 		if (ic->ic_state != IEEE80211_S_RUN) {
1127 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1128 			    ni->ni_macaddr, "shared key auth",
1129 			    "bad state %u", ic->ic_state);
1130 			estatus = IEEE80211_STATUS_ALG;	/* XXX */
1131 			goto bad;
1132 		}
1133 		switch (seq) {
1134 		case IEEE80211_AUTH_SHARED_REQUEST:
1135 			if (ni == ic->ic_bss) {
1136 				ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);
1137 				if (ni == NULL) {
1138 					/* NB: no way to return an error */
1139 					return;
1140 				}
1141 				allocbs = 1;
1142 			} else {
1143 				if ((ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1144 					(void) ieee80211_ref_node(ni);
1145 				allocbs = 0;
1146 			}
1147 			/*
1148 			 * Mark the node as referenced to reflect that it's
1149 			 * reference count has been bumped to insure it remains
1150 			 * after the transaction completes.
1151 			 */
1152 			ni->ni_flags |= IEEE80211_NODE_AREF;
1153 			ni->ni_rssi = rssi;
1154 			ni->ni_rstamp = rstamp;
1155 			if (!alloc_challenge(ic, ni)) {
1156 				/* NB: don't return error so they rexmit */
1157 				return;
1158 			}
1159 			get_random_bytes(ni->ni_challenge,
1160 				IEEE80211_CHALLENGE_LEN);
1161 			IEEE80211_DPRINTF(ic,
1162 				IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1163 				"[%s] shared key %sauth request\n",
1164 				ether_sprintf(ni->ni_macaddr),
1165 				allocbs ? "" : "re");
1166 			break;
1167 		case IEEE80211_AUTH_SHARED_RESPONSE:
1168 			if (ni == ic->ic_bss) {
1169 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1170 				    ni->ni_macaddr, "shared key response",
1171 				    "%s", "unknown station");
1172 				/* NB: don't send a response */
1173 				return;
1174 			}
1175 			if (ni->ni_challenge == NULL) {
1176 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1177 				    ni->ni_macaddr, "shared key response",
1178 				    "%s", "no challenge recorded");
1179 				ic->ic_stats.is_rx_bad_auth++;
1180 				estatus = IEEE80211_STATUS_CHALLENGE;
1181 				goto bad;
1182 			}
1183 			if (memcmp(ni->ni_challenge, &challenge[2],
1184 			           challenge[1]) != 0) {
1185 				IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1186 				    ni->ni_macaddr, "shared key response",
1187 				    "%s", "challenge mismatch");
1188 				ic->ic_stats.is_rx_auth_fail++;
1189 				estatus = IEEE80211_STATUS_CHALLENGE;
1190 				goto bad;
1191 			}
1192 			IEEE80211_DPRINTF(ic,
1193 			    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1194 			    "[%s] station authenticated (shared key)\n",
1195 			    ether_sprintf(ni->ni_macaddr));
1196 			ieee80211_node_authorize(ni);
1197 			break;
1198 		default:
1199 			IEEE80211_DISCARD_MAC(ic, IEEE80211_MSG_AUTH,
1200 			    ni->ni_macaddr, "shared key auth",
1201 			    "bad seq %d", seq);
1202 			ic->ic_stats.is_rx_bad_auth++;
1203 			estatus = IEEE80211_STATUS_SEQUENCE;
1204 			goto bad;
1205 		}
1206 		IEEE80211_SEND_MGMT(ic, ni,
1207 			IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1208 	}
1209 #endif /* !IEEE80211_NO_HOSTAP */
1210 		break;
1211 
1212 	case IEEE80211_M_STA:
1213 		if (ic->ic_state != IEEE80211_S_AUTH)
1214 			return;
1215 		switch (seq) {
1216 		case IEEE80211_AUTH_SHARED_PASS:
1217 			if (ni->ni_challenge != NULL) {
1218 				free(ni->ni_challenge, M_DEVBUF);
1219 				ni->ni_challenge = NULL;
1220 			}
1221 			if (status != 0) {
1222 				IEEE80211_DPRINTF(ic,
1223 				    IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
1224 				    "[%s] shared key auth failed (reason %d)\n",
1225 				    ether_sprintf(ieee80211_getbssid(ic, wh)),
1226 				    status);
1227 				/* XXX can this happen? */
1228 				if (ni != ic->ic_bss)
1229 					ni->ni_fails++;
1230 				ic->ic_stats.is_rx_auth_fail++;
1231 				return;
1232 			}
1233 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
1234 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
1235 			break;
1236 		case IEEE80211_AUTH_SHARED_CHALLENGE:
1237 			if (!alloc_challenge(ic, ni))
1238 				return;
1239 			/* XXX could optimize by passing recvd challenge */
1240 			memcpy(ni->ni_challenge, &challenge[2], challenge[1]);
1241 			IEEE80211_SEND_MGMT(ic, ni,
1242 				IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
1243 			break;
1244 		default:
1245 			IEEE80211_DISCARD(ic, IEEE80211_MSG_AUTH,
1246 			    wh, "shared key auth", "bad seq %d", seq);
1247 			ic->ic_stats.is_rx_bad_auth++;
1248 			return;
1249 		}
1250 		break;
1251 	}
1252 	return;
1253 bad:
1254 #ifndef IEEE80211_NO_HOSTAP
1255 	/*
1256 	 * Send an error response; but only when operating as an AP.
1257 	 */
1258 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
1259 		/* XXX hack to workaround calling convention */
1260 		ieee80211_send_error(ic, ni, wh->i_addr2,
1261 		    IEEE80211_FC0_SUBTYPE_AUTH,
1262 		    (seq + 1) | (estatus<<16));
1263 	} else if (ic->ic_opmode == IEEE80211_M_STA) {
1264 		/*
1265 		 * Kick the state machine.  This short-circuits
1266 		 * using the mgt frame timeout to trigger the
1267 		 * state transition.
1268 		 */
1269 		if (ic->ic_state == IEEE80211_S_AUTH)
1270 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
1271 	}
1272 #else
1273 	;
1274 #endif /* !IEEE80211_NO_HOSTAP */
1275 }
1276 
1277 /* Verify the existence and length of __elem or get out. */
1278 #define IEEE80211_VERIFY_ELEMENT(__elem, __maxlen) do {			\
1279 	if ((__elem) == NULL) {						\
1280 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,		\
1281 		    wh, ieee80211_mgt_subtype_name[subtype >>		\
1282 			IEEE80211_FC0_SUBTYPE_SHIFT],			\
1283 		    "%s", "no " #__elem );				\
1284 		ic->ic_stats.is_rx_elem_missing++;			\
1285 		return;							\
1286 	}								\
1287 	if ((__elem)[1] > (__maxlen)) {					\
1288 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,		\
1289 		    wh, ieee80211_mgt_subtype_name[subtype >>		\
1290 			IEEE80211_FC0_SUBTYPE_SHIFT],			\
1291 		    "bad " #__elem " len %d", (__elem)[1]);		\
1292 		ic->ic_stats.is_rx_elem_toobig++;			\
1293 		return;							\
1294 	}								\
1295 } while (0)
1296 
1297 #define	IEEE80211_VERIFY_LENGTH(_len, _minlen) do {			\
1298 	if ((_len) < (_minlen)) {					\
1299 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ELEMID,		\
1300 		    wh, ieee80211_mgt_subtype_name[subtype >>		\
1301 			IEEE80211_FC0_SUBTYPE_SHIFT],			\
1302 		    "%s", "ie too short");				\
1303 		ic->ic_stats.is_rx_elem_toosmall++;			\
1304 		return;							\
1305 	}								\
1306 } while (0)
1307 
1308 #ifdef IEEE80211_DEBUG
1309 static void
1310 ieee80211_ssid_mismatch(struct ieee80211com *ic, const char *tag,
1311 	u_int8_t mac[IEEE80211_ADDR_LEN], u_int8_t *ssid)
1312 {
1313 	printf("[%s] discard %s frame, ssid mismatch: ",
1314 		ether_sprintf(mac), tag);
1315 	ieee80211_print_essid(ssid + 2, ssid[1]);
1316 	printf("\n");
1317 }
1318 
1319 #define	IEEE80211_VERIFY_SSID(_ni, _ssid) do {				\
1320 	if ((_ssid)[1] != 0 &&						\
1321 	    ((_ssid)[1] != (_ni)->ni_esslen ||				\
1322 	    memcmp((_ssid) + 2, (_ni)->ni_essid, (_ssid)[1]) != 0)) {	\
1323 		if (ieee80211_msg_input(ic))				\
1324 			ieee80211_ssid_mismatch(ic, 			\
1325 			    ieee80211_mgt_subtype_name[subtype >>	\
1326 				IEEE80211_FC0_SUBTYPE_SHIFT],		\
1327 				wh->i_addr2, _ssid);			\
1328 		ic->ic_stats.is_rx_ssidmismatch++;			\
1329 		return;							\
1330 	}								\
1331 } while (0)
1332 #else /* !IEEE80211_DEBUG */
1333 #define	IEEE80211_VERIFY_SSID(_ni, _ssid) do {				\
1334 	if ((_ssid)[1] != 0 &&						\
1335 	    ((_ssid)[1] != (_ni)->ni_esslen ||				\
1336 	    memcmp((_ssid) + 2, (_ni)->ni_essid, (_ssid)[1]) != 0)) {	\
1337 		ic->ic_stats.is_rx_ssidmismatch++;			\
1338 		return;							\
1339 	}								\
1340 } while (0)
1341 #endif /* !IEEE80211_DEBUG */
1342 
1343 /* unalligned little endian access */
1344 #define LE_READ_2(p)					\
1345 	((u_int16_t)					\
1346 	 ((((const u_int8_t *)(p))[0]      ) |		\
1347 	  (((const u_int8_t *)(p))[1] <<  8)))
1348 #define LE_READ_4(p)					\
1349 	((u_int32_t)					\
1350 	 ((((const u_int8_t *)(p))[0]      ) |		\
1351 	  (((const u_int8_t *)(p))[1] <<  8) |		\
1352 	  (((const u_int8_t *)(p))[2] << 16) |		\
1353 	  (((const u_int8_t *)(p))[3] << 24)))
1354 
1355 static __inline int
1356 iswpaoui(const u_int8_t *frm)
1357 {
1358 	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
1359 }
1360 
1361 static __inline int
1362 iswmeoui(const u_int8_t *frm)
1363 {
1364 	return frm[1] > 3 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI);
1365 }
1366 
1367 static __inline int
1368 iswmeparam(const u_int8_t *frm)
1369 {
1370 	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
1371 		frm[6] == WME_PARAM_OUI_SUBTYPE;
1372 }
1373 
1374 static __inline int
1375 iswmeinfo(const u_int8_t *frm)
1376 {
1377 	return frm[1] > 5 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
1378 		frm[6] == WME_INFO_OUI_SUBTYPE;
1379 }
1380 
1381 static __inline int
1382 isatherosoui(const u_int8_t *frm)
1383 {
1384 	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
1385 }
1386 
1387 /*
1388  * Convert a WPA cipher selector OUI to an internal
1389  * cipher algorithm.  Where appropriate we also
1390  * record any key length.
1391  */
1392 static int
1393 wpa_cipher(u_int8_t *sel, u_int8_t *keylen)
1394 {
1395 #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1396 	u_int32_t w = LE_READ_4(sel);
1397 
1398 	switch (w) {
1399 	case WPA_SEL(WPA_CSE_NULL):
1400 		return IEEE80211_CIPHER_NONE;
1401 	case WPA_SEL(WPA_CSE_WEP40):
1402 		if (keylen)
1403 			*keylen = 40 / NBBY;
1404 		return IEEE80211_CIPHER_WEP;
1405 	case WPA_SEL(WPA_CSE_WEP104):
1406 		if (keylen)
1407 			*keylen = 104 / NBBY;
1408 		return IEEE80211_CIPHER_WEP;
1409 	case WPA_SEL(WPA_CSE_TKIP):
1410 		return IEEE80211_CIPHER_TKIP;
1411 	case WPA_SEL(WPA_CSE_CCMP):
1412 		return IEEE80211_CIPHER_AES_CCM;
1413 	}
1414 	return 32;		/* NB: so 1<< is discarded */
1415 #undef WPA_SEL
1416 }
1417 
1418 /*
1419  * Convert a WPA key management/authentication algorithm
1420  * to an internal code.
1421  */
1422 static int
1423 wpa_keymgmt(u_int8_t *sel)
1424 {
1425 #define	WPA_SEL(x)	(((x)<<24)|WPA_OUI)
1426 	u_int32_t w = LE_READ_4(sel);
1427 
1428 	switch (w) {
1429 	case WPA_SEL(WPA_ASE_8021X_UNSPEC):
1430 		return WPA_ASE_8021X_UNSPEC;
1431 	case WPA_SEL(WPA_ASE_8021X_PSK):
1432 		return WPA_ASE_8021X_PSK;
1433 	case WPA_SEL(WPA_ASE_NONE):
1434 		return WPA_ASE_NONE;
1435 	}
1436 	return 0;		/* NB: so is discarded */
1437 #undef WPA_SEL
1438 }
1439 
1440 /*
1441  * Parse a WPA information element to collect parameters
1442  * and validate the parameters against what has been
1443  * configured for the system.
1444  */
1445 static int
1446 ieee80211_parse_wpa(struct ieee80211com *ic, u_int8_t *frm,
1447 	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1448 {
1449 	u_int8_t len = frm[1];
1450 	u_int32_t w;
1451 	int n;
1452 
1453 	/*
1454 	 * Check the length once for fixed parts: OUI, type,
1455 	 * version, mcast cipher, and 2 selector counts.
1456 	 * Other, variable-length data, must be checked separately.
1457 	 */
1458 	if ((ic->ic_flags & IEEE80211_F_WPA1) == 0) {
1459 		IEEE80211_DISCARD_IE(ic,
1460 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1461 		    wh, "WPA", "not WPA, flags 0x%x", ic->ic_flags);
1462 		return IEEE80211_REASON_IE_INVALID;
1463 	}
1464 	if (len < 14) {
1465 		IEEE80211_DISCARD_IE(ic,
1466 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1467 		    wh, "WPA", "too short, len %u", len);
1468 		return IEEE80211_REASON_IE_INVALID;
1469 	}
1470 	frm += 6, len -= 4;		/* NB: len is payload only */
1471 	/* NB: iswapoui already validated the OUI and type */
1472 	w = LE_READ_2(frm);
1473 	if (w != WPA_VERSION) {
1474 		IEEE80211_DISCARD_IE(ic,
1475 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1476 		    wh, "WPA", "bad version %u", w);
1477 		return IEEE80211_REASON_IE_INVALID;
1478 	}
1479 	frm += 2, len -= 2;
1480 
1481 	/* multicast/group cipher */
1482 	w = wpa_cipher(frm, &rsn->rsn_mcastkeylen);
1483 	if (w != rsn->rsn_mcastcipher) {
1484 		IEEE80211_DISCARD_IE(ic,
1485 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1486 		    wh, "WPA", "mcast cipher mismatch; got %u, expected %u",
1487 		    w, rsn->rsn_mcastcipher);
1488 		return IEEE80211_REASON_IE_INVALID;
1489 	}
1490 	frm += 4, len -= 4;
1491 
1492 	/* unicast ciphers */
1493 	n = LE_READ_2(frm);
1494 	frm += 2, len -= 2;
1495 	if (len < n*4+2) {
1496 		IEEE80211_DISCARD_IE(ic,
1497 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1498 		    wh, "WPA", "ucast cipher data too short; len %u, n %u",
1499 		    len, n);
1500 		return IEEE80211_REASON_IE_INVALID;
1501 	}
1502 	w = 0;
1503 	for (; n > 0; n--) {
1504 		w |= 1<<wpa_cipher(frm, &rsn->rsn_ucastkeylen);
1505 		frm += 4, len -= 4;
1506 	}
1507 	w &= rsn->rsn_ucastcipherset;
1508 	if (w == 0) {
1509 		IEEE80211_DISCARD_IE(ic,
1510 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1511 		    wh, "WPA", "%s", "ucast cipher set empty");
1512 		return IEEE80211_REASON_IE_INVALID;
1513 	}
1514 	if (w & (1<<IEEE80211_CIPHER_TKIP))
1515 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1516 	else
1517 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1518 
1519 	/* key management algorithms */
1520 	n = LE_READ_2(frm);
1521 	frm += 2, len -= 2;
1522 	if (len < n*4) {
1523 		IEEE80211_DISCARD_IE(ic,
1524 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1525 		    wh, "WPA", "key mgmt alg data too short; len %u, n %u",
1526 		    len, n);
1527 		return IEEE80211_REASON_IE_INVALID;
1528 	}
1529 	w = 0;
1530 	for (; n > 0; n--) {
1531 		w |= wpa_keymgmt(frm);
1532 		frm += 4, len -= 4;
1533 	}
1534 	w &= rsn->rsn_keymgmtset;
1535 	if (w == 0) {
1536 		IEEE80211_DISCARD_IE(ic,
1537 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1538 		    wh, "WPA", "%s", "no acceptable key mgmt alg");
1539 		return IEEE80211_REASON_IE_INVALID;
1540 	}
1541 	if (w & WPA_ASE_8021X_UNSPEC)
1542 		rsn->rsn_keymgmt = WPA_ASE_8021X_UNSPEC;
1543 	else
1544 		rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
1545 
1546 	if (len > 2)		/* optional capabilities */
1547 		rsn->rsn_caps = LE_READ_2(frm);
1548 
1549 	return 0;
1550 }
1551 
1552 /*
1553  * Convert an RSN cipher selector OUI to an internal
1554  * cipher algorithm.  Where appropriate we also
1555  * record any key length.
1556  */
1557 static int
1558 rsn_cipher(u_int8_t *sel, u_int8_t *keylen)
1559 {
1560 #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1561 	u_int32_t w = LE_READ_4(sel);
1562 
1563 	switch (w) {
1564 	case RSN_SEL(RSN_CSE_NULL):
1565 		return IEEE80211_CIPHER_NONE;
1566 	case RSN_SEL(RSN_CSE_WEP40):
1567 		if (keylen)
1568 			*keylen = 40 / NBBY;
1569 		return IEEE80211_CIPHER_WEP;
1570 	case RSN_SEL(RSN_CSE_WEP104):
1571 		if (keylen)
1572 			*keylen = 104 / NBBY;
1573 		return IEEE80211_CIPHER_WEP;
1574 	case RSN_SEL(RSN_CSE_TKIP):
1575 		return IEEE80211_CIPHER_TKIP;
1576 	case RSN_SEL(RSN_CSE_CCMP):
1577 		return IEEE80211_CIPHER_AES_CCM;
1578 	case RSN_SEL(RSN_CSE_WRAP):
1579 		return IEEE80211_CIPHER_AES_OCB;
1580 	}
1581 	return 32;		/* NB: so 1<< is discarded */
1582 #undef WPA_SEL
1583 }
1584 
1585 /*
1586  * Convert an RSN key management/authentication algorithm
1587  * to an internal code.
1588  */
1589 static int
1590 rsn_keymgmt(u_int8_t *sel)
1591 {
1592 #define	RSN_SEL(x)	(((x)<<24)|RSN_OUI)
1593 	u_int32_t w = LE_READ_4(sel);
1594 
1595 	switch (w) {
1596 	case RSN_SEL(RSN_ASE_8021X_UNSPEC):
1597 		return RSN_ASE_8021X_UNSPEC;
1598 	case RSN_SEL(RSN_ASE_8021X_PSK):
1599 		return RSN_ASE_8021X_PSK;
1600 	case RSN_SEL(RSN_ASE_NONE):
1601 		return RSN_ASE_NONE;
1602 	}
1603 	return 0;		/* NB: so is discarded */
1604 #undef RSN_SEL
1605 }
1606 
1607 /*
1608  * Parse a WPA/RSN information element to collect parameters
1609  * and validate the parameters against what has been
1610  * configured for the system.
1611  */
1612 static int
1613 ieee80211_parse_rsn(struct ieee80211com *ic, u_int8_t *frm,
1614 	struct ieee80211_rsnparms *rsn, const struct ieee80211_frame *wh)
1615 {
1616 	u_int8_t len = frm[1];
1617 	u_int32_t w;
1618 	int n;
1619 
1620 	/*
1621 	 * Check the length once for fixed parts:
1622 	 * version, mcast cipher, and 2 selector counts.
1623 	 * Other, variable-length data, must be checked separately.
1624 	 */
1625 	if ((ic->ic_flags & IEEE80211_F_WPA2) == 0) {
1626 		IEEE80211_DISCARD_IE(ic,
1627 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1628 		    wh, "WPA", "not RSN, flags 0x%x", ic->ic_flags);
1629 		return IEEE80211_REASON_IE_INVALID;
1630 	}
1631 	if (len < 10) {
1632 		IEEE80211_DISCARD_IE(ic,
1633 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1634 		    wh, "RSN", "too short, len %u", len);
1635 		return IEEE80211_REASON_IE_INVALID;
1636 	}
1637 	frm += 2;
1638 	w = LE_READ_2(frm);
1639 	if (w != RSN_VERSION) {
1640 		IEEE80211_DISCARD_IE(ic,
1641 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1642 		    wh, "RSN", "bad version %u", w);
1643 		return IEEE80211_REASON_IE_INVALID;
1644 	}
1645 	frm += 2, len -= 2;
1646 
1647 	/* multicast/group cipher */
1648 	w = rsn_cipher(frm, &rsn->rsn_mcastkeylen);
1649 	if (w != rsn->rsn_mcastcipher) {
1650 		IEEE80211_DISCARD_IE(ic,
1651 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1652 		    wh, "RSN", "mcast cipher mismatch; got %u, expected %u",
1653 		    w, rsn->rsn_mcastcipher);
1654 		return IEEE80211_REASON_IE_INVALID;
1655 	}
1656 	frm += 4, len -= 4;
1657 
1658 	/* unicast ciphers */
1659 	n = LE_READ_2(frm);
1660 	frm += 2, len -= 2;
1661 	if (len < n*4+2) {
1662 		IEEE80211_DISCARD_IE(ic,
1663 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1664 		    wh, "RSN", "ucast cipher data too short; len %u, n %u",
1665 		    len, n);
1666 		return IEEE80211_REASON_IE_INVALID;
1667 	}
1668 	w = 0;
1669 	for (; n > 0; n--) {
1670 		w |= 1<<rsn_cipher(frm, &rsn->rsn_ucastkeylen);
1671 		frm += 4, len -= 4;
1672 	}
1673 	w &= rsn->rsn_ucastcipherset;
1674 	if (w == 0) {
1675 		IEEE80211_DISCARD_IE(ic,
1676 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1677 		    wh, "RSN", "%s", "ucast cipher set empty");
1678 		return IEEE80211_REASON_IE_INVALID;
1679 	}
1680 	if (w & (1<<IEEE80211_CIPHER_TKIP))
1681 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_TKIP;
1682 	else
1683 		rsn->rsn_ucastcipher = IEEE80211_CIPHER_AES_CCM;
1684 
1685 	/* key management algorithms */
1686 	n = LE_READ_2(frm);
1687 	frm += 2, len -= 2;
1688 	if (len < n*4) {
1689 		IEEE80211_DISCARD_IE(ic,
1690 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1691 		    wh, "RSN", "key mgmt alg data too short; len %u, n %u",
1692 		    len, n);
1693 		return IEEE80211_REASON_IE_INVALID;
1694 	}
1695 	w = 0;
1696 	for (; n > 0; n--) {
1697 		w |= rsn_keymgmt(frm);
1698 		frm += 4, len -= 4;
1699 	}
1700 	w &= rsn->rsn_keymgmtset;
1701 	if (w == 0) {
1702 		IEEE80211_DISCARD_IE(ic,
1703 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WPA,
1704 		    wh, "RSN", "%s", "no acceptable key mgmt alg");
1705 		return IEEE80211_REASON_IE_INVALID;
1706 	}
1707 	if (w & RSN_ASE_8021X_UNSPEC)
1708 		rsn->rsn_keymgmt = RSN_ASE_8021X_UNSPEC;
1709 	else
1710 		rsn->rsn_keymgmt = RSN_ASE_8021X_PSK;
1711 
1712 	/* optional RSN capabilities */
1713 	if (len > 2)
1714 		rsn->rsn_caps = LE_READ_2(frm);
1715 	/* XXXPMKID */
1716 
1717 	return 0;
1718 }
1719 
1720 static int
1721 ieee80211_parse_wmeparams(struct ieee80211com *ic, u_int8_t *frm,
1722 	const struct ieee80211_frame *wh)
1723 {
1724 #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
1725 	struct ieee80211_wme_state *wme = &ic->ic_wme;
1726 	u_int len = frm[1], qosinfo;
1727 	int i;
1728 
1729 	if (len < sizeof(struct ieee80211_wme_param)-2) {
1730 		IEEE80211_DISCARD_IE(ic,
1731 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
1732 		    wh, "WME", "too short, len %u", len);
1733 		return -1;
1734 	}
1735 	qosinfo = frm[offsetof(struct ieee80211_wme_param, param_qosInfo)];
1736 	qosinfo &= WME_QOSINFO_COUNT;
1737 	/* XXX do proper check for wraparound */
1738 	if (qosinfo == wme->wme_wmeChanParams.cap_info)
1739 		return 0;
1740 	frm += offsetof(struct ieee80211_wme_param, params_acParams);
1741 	for (i = 0; i < WME_NUM_AC; i++) {
1742 		struct wmeParams *wmep =
1743 			&wme->wme_wmeChanParams.cap_wmeParams[i];
1744 		/* NB: ACI not used */
1745 		wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
1746 		wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
1747 		wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
1748 		wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
1749 		wmep->wmep_txopLimit = LE_READ_2(frm+2);
1750 		frm += 4;
1751 	}
1752 	wme->wme_wmeChanParams.cap_info = qosinfo;
1753 	return 1;
1754 #undef MS
1755 }
1756 
1757 void
1758 ieee80211_saveie(u_int8_t **iep, const u_int8_t *ie)
1759 {
1760 	u_int ielen = ie[1]+2;
1761 	/*
1762 	 * Record information element for later use.
1763 	 */
1764 	if (*iep == NULL || (*iep)[1] != ie[1]) {
1765 		if (*iep != NULL)
1766 			free(*iep, M_DEVBUF);
1767 		*iep = malloc(ielen, M_DEVBUF, M_NOWAIT);
1768 	}
1769 	if (*iep != NULL)
1770 		memcpy(*iep, ie, ielen);
1771 	/* XXX note failure */
1772 }
1773 
1774 static void
1775 ieee80211_update_adhoc_node(struct ieee80211com *ic, struct ieee80211_node *ni,
1776     struct ieee80211_frame *wh, struct ieee80211_scanparams *scan, int rssi,
1777     u_int32_t rstamp)
1778 {
1779 	if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
1780 		/*
1781 		 * Create a new entry in the neighbor table.
1782 		 * Records the TSF.
1783 		 */
1784 		if ((ni = ieee80211_add_neighbor(ic, wh, scan)) == NULL)
1785 			return;
1786 	} else if (ni->ni_capinfo == 0) {
1787 		/*
1788 		 * Initialize a node that was "faked up."  Records
1789 		 * the TSF.
1790 		 *
1791 		 * No need to check for a change of BSSID: ni could
1792 		 * not have been the IBSS (ic_bss)
1793 		 */
1794 		ieee80211_init_neighbor(ic, ni, wh, scan, 0);
1795 	} else {
1796 		/* Record TSF for potential resync. */
1797 		memcpy(ni->ni_tstamp.data, scan->tstamp, sizeof(ni->ni_tstamp));
1798 	}
1799 
1800 	ni->ni_rssi = rssi;
1801 	ni->ni_rstamp = rstamp;
1802 
1803 	/* Mark a neighbor's change of BSSID. */
1804 	if (IEEE80211_ADDR_EQ(wh->i_addr3, ni->ni_bssid))
1805 		return;
1806 
1807 	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1808 
1809 	if (ni != ic->ic_bss)
1810 		return;
1811 	else if (ic->ic_flags & IEEE80211_F_DESBSSID) {
1812 		/*
1813 		 * Now, ni does not represent a network we
1814 		 * want to belong to, so start a scan.
1815 		 */
1816 		ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
1817 		return;
1818 	} else {
1819 		/*
1820 		 * A RUN->RUN transition lets the driver
1821 		 * reprogram its BSSID filter.
1822 		 *
1823 		 * No need to SCAN, we already belong to
1824 		 * an IBSS that meets our criteria: channel,
1825 		 * SSID, etc.  It could be harmful to scan,
1826 		 * too: if a scan does not detect nodes
1827 		 * belonging to my current IBSS, then we
1828 		 * will create a new IBSS at the end of
1829 		 * the scan, needlessly splitting the
1830 		 * network.
1831 		 */
1832 		ieee80211_new_state(ic, IEEE80211_S_RUN, 0);
1833 	}
1834 }
1835 
1836 void
1837 ieee80211_recv_mgmt(struct ieee80211com *ic, struct mbuf *m0,
1838 	struct ieee80211_node *ni,
1839 	int subtype, int rssi, u_int32_t rstamp)
1840 {
1841 #define	ISPROBE(_st)	((_st) == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1842 #define	ISREASSOC(_st)	((_st) == IEEE80211_FC0_SUBTYPE_REASSOC_RESP)
1843 	struct ieee80211_frame *wh;
1844 	u_int8_t *frm, *efrm;
1845 	u_int8_t *ssid, *rates, *xrates, *wpa, *wme;
1846 	int reassoc, resp, allocbs;
1847 	u_int8_t rate;
1848 
1849 	wh = mtod(m0, struct ieee80211_frame *);
1850 	frm = (u_int8_t *)&wh[1];
1851 	efrm = mtod(m0, u_int8_t *) + m0->m_len;
1852 	switch (subtype) {
1853 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1854 	case IEEE80211_FC0_SUBTYPE_BEACON: {
1855 		struct ieee80211_scanparams scan;
1856 
1857 		/*
1858 		 * We process beacon/probe response frames:
1859 		 *    o when scanning, or
1860 		 *    o station mode when associated (to collect state
1861 		 *      updates such as 802.11g slot time), or
1862 		 *    o adhoc mode (to discover neighbors)
1863 		 * Frames otherwise received are discarded.
1864 		 */
1865 		if (!((ic->ic_flags & IEEE80211_F_SCAN) ||
1866 		      (ic->ic_opmode == IEEE80211_M_STA && ni->ni_associd) ||
1867 		       ic->ic_opmode == IEEE80211_M_IBSS)) {
1868 			ic->ic_stats.is_rx_mgtdiscard++;
1869 			return;
1870 		}
1871 		/*
1872 		 * beacon/probe response frame format
1873 		 *	[8] time stamp
1874 		 *	[2] beacon interval
1875 		 *	[2] capability information
1876 		 *	[tlv] ssid
1877 		 *	[tlv] supported rates
1878 		 *	[tlv] country information
1879 		 *	[tlv] parameter set (FH/DS)
1880 		 *	[tlv] erp information
1881 		 *	[tlv] extended supported rates
1882 		 *	[tlv] WME
1883 		 *	[tlv] WPA or RSN
1884 		 */
1885 		IEEE80211_VERIFY_LENGTH(efrm - frm, 12);
1886 		memset(&scan, 0, sizeof(scan));
1887 		scan.tstamp  = frm;				frm += 8;
1888 		scan.bintval = le16toh(*(u_int16_t *)frm);	frm += 2;
1889 		scan.capinfo = le16toh(*(u_int16_t *)frm);	frm += 2;
1890 		scan.bchan = ieee80211_chan2ieee(ic, ic->ic_curchan);
1891 		scan.chan = scan.bchan;
1892 
1893 		while (frm < efrm) {
1894 			switch (*frm) {
1895 			case IEEE80211_ELEMID_SSID:
1896 				scan.ssid = frm;
1897 				break;
1898 			case IEEE80211_ELEMID_RATES:
1899 				scan.rates = frm;
1900 				break;
1901 			case IEEE80211_ELEMID_COUNTRY:
1902 				scan.country = frm;
1903 				break;
1904 			case IEEE80211_ELEMID_FHPARMS:
1905 				if (ic->ic_phytype == IEEE80211_T_FH) {
1906 					scan.fhdwell = LE_READ_2(&frm[2]);
1907 					scan.chan = IEEE80211_FH_CHAN(frm[4], frm[5]);
1908 					scan.fhindex = frm[6];
1909 				}
1910 				break;
1911 			case IEEE80211_ELEMID_DSPARMS:
1912 				/*
1913 				 * XXX hack this since depending on phytype
1914 				 * is problematic for multi-mode devices.
1915 				 */
1916 				if (ic->ic_phytype != IEEE80211_T_FH)
1917 					scan.chan = frm[2];
1918 				break;
1919 			case IEEE80211_ELEMID_TIM:
1920 				/* XXX ATIM? */
1921 				scan.tim = frm;
1922 				scan.timoff = frm - mtod(m0, u_int8_t *);
1923 				break;
1924 			case IEEE80211_ELEMID_IBSSPARMS:
1925 				break;
1926 			case IEEE80211_ELEMID_XRATES:
1927 				scan.xrates = frm;
1928 				break;
1929 			case IEEE80211_ELEMID_ERP:
1930 				if (frm[1] != 1) {
1931 					IEEE80211_DISCARD_IE(ic,
1932 					    IEEE80211_MSG_ELEMID, wh, "ERP",
1933 					    "bad len %u", frm[1]);
1934 					ic->ic_stats.is_rx_elem_toobig++;
1935 					break;
1936 				}
1937 				scan.erp = frm[2];
1938 				break;
1939 			case IEEE80211_ELEMID_RSN:
1940 				scan.wpa = frm;
1941 				break;
1942 			case IEEE80211_ELEMID_VENDOR:
1943 				if (iswpaoui(frm))
1944 					scan.wpa = frm;
1945 				else if (iswmeparam(frm) || iswmeinfo(frm))
1946 					scan.wme = frm;
1947 				/* XXX Atheros OUI support */
1948 				break;
1949 			default:
1950 				IEEE80211_DISCARD_IE(ic, IEEE80211_MSG_ELEMID,
1951 				    wh, "unhandled",
1952 				    "id %u, len %u", *frm, frm[1]);
1953 				ic->ic_stats.is_rx_elem_unknown++;
1954 				break;
1955 			}
1956 			frm += frm[1] + 2;
1957 		}
1958 		IEEE80211_VERIFY_ELEMENT(scan.rates, IEEE80211_RATE_MAXSIZE);
1959 		IEEE80211_VERIFY_ELEMENT(scan.ssid, IEEE80211_NWID_LEN);
1960 		if (
1961 #if IEEE80211_CHAN_MAX < 255
1962 		    scan.chan > IEEE80211_CHAN_MAX ||
1963 #endif
1964 		    isclr(ic->ic_chan_active, scan.chan)) {
1965 			IEEE80211_DISCARD(ic,
1966 			    IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
1967 			    wh, ieee80211_mgt_subtype_name[subtype >>
1968 				IEEE80211_FC0_SUBTYPE_SHIFT],
1969 			    "invalid channel %u", scan.chan);
1970 			ic->ic_stats.is_rx_badchan++;
1971 			return;
1972 		}
1973 		if (scan.chan != scan.bchan &&
1974 		    ic->ic_phytype != IEEE80211_T_FH) {
1975 			/*
1976 			 * Frame was received on a channel different from the
1977 			 * one indicated in the DS params element id;
1978 			 * silently discard it.
1979 			 *
1980 			 * NB: this can happen due to signal leakage.
1981 			 *     But we should take it for FH phy because
1982 			 *     the rssi value should be correct even for
1983 			 *     different hop pattern in FH.
1984 			 */
1985 			IEEE80211_DISCARD(ic,
1986 			    IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
1987 			    wh, ieee80211_mgt_subtype_name[subtype >>
1988 				IEEE80211_FC0_SUBTYPE_SHIFT],
1989 			    "for off-channel %u", scan.chan);
1990 			ic->ic_stats.is_rx_chanmismatch++;
1991 			return;
1992 		}
1993 		if (!(IEEE80211_BINTVAL_MIN <= scan.bintval &&
1994 		      scan.bintval <= IEEE80211_BINTVAL_MAX)) {
1995 			IEEE80211_DISCARD(ic,
1996 			    IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
1997 			    wh, ieee80211_mgt_subtype_name[subtype >>
1998 				IEEE80211_FC0_SUBTYPE_SHIFT],
1999 			    "bogus beacon interval", scan.bintval);
2000 			ic->ic_stats.is_rx_badbintval++;
2001 			return;
2002 		}
2003 
2004 		if (ni != ic->ic_bss) {
2005 			ni = ieee80211_refine_node_for_beacon(ic, ni,
2006 					&ic->ic_channels[scan.chan], scan.ssid);
2007 		}
2008 		/*
2009 		 * Count frame now that we know it's to be processed.
2010 		 */
2011 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
2012 			ic->ic_stats.is_rx_beacon++;		/* XXX remove */
2013 			IEEE80211_NODE_STAT(ni, rx_beacons);
2014 		} else
2015 			IEEE80211_NODE_STAT(ni, rx_proberesp);
2016 
2017 		/*
2018 		 * When operating in station mode, check for state updates.
2019 		 * Be careful to ignore beacons received while doing a
2020 		 * background scan.  We consider only 11g/WMM stuff right now.
2021 		 */
2022 		if (ic->ic_opmode == IEEE80211_M_STA &&
2023 		    ni->ni_associd != 0 &&
2024 		    ((ic->ic_flags & IEEE80211_F_SCAN) == 0 ||
2025 		     IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))) {
2026 			/* record tsf of last beacon */
2027 			memcpy(ni->ni_tstamp.data, scan.tstamp,
2028 				sizeof(ni->ni_tstamp));
2029 			if (ni->ni_erp != scan.erp) {
2030 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2031 				    "[%s] erp change: was 0x%x, now 0x%x\n",
2032 				    ether_sprintf(wh->i_addr2),
2033 				    ni->ni_erp, scan.erp);
2034 				if (ic->ic_curmode == IEEE80211_MODE_11G &&
2035 				    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
2036 					ic->ic_flags |= IEEE80211_F_USEPROT;
2037 				else
2038 					ic->ic_flags &= ~IEEE80211_F_USEPROT;
2039 				ni->ni_erp = scan.erp;
2040 				/* XXX statistic */
2041 			}
2042 			if ((ni->ni_capinfo ^ scan.capinfo) & IEEE80211_CAPINFO_SHORT_SLOTTIME) {
2043 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2044 				    "[%s] capabilities change: before 0x%x,"
2045 				     " now 0x%x\n",
2046 				     ether_sprintf(wh->i_addr2),
2047 				     ni->ni_capinfo, scan.capinfo);
2048 				/*
2049 				 * NB: we assume short preamble doesn't
2050 				 *     change dynamically
2051 				 */
2052 				ieee80211_set_shortslottime(ic,
2053 					ic->ic_curmode == IEEE80211_MODE_11A ||
2054 					(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
2055 				ni->ni_capinfo = scan.capinfo;
2056 				/* XXX statistic */
2057 			}
2058 			if (scan.wme != NULL &&
2059 			    (ni->ni_flags & IEEE80211_NODE_QOS) &&
2060 			    ieee80211_parse_wmeparams(ic, scan.wme, wh) > 0)
2061 				ieee80211_wme_updateparams(ic);
2062 			if (scan.tim != NULL) {
2063 				struct ieee80211_tim_ie *ie =
2064 				    (struct ieee80211_tim_ie *) scan.tim;
2065 
2066 				ni->ni_dtim_count = ie->tim_count;
2067 				ni->ni_dtim_period = ie->tim_period;
2068 			}
2069 			if (ic->ic_flags & IEEE80211_F_SCAN)
2070 				ieee80211_add_scan(ic, &scan, wh,
2071 					subtype, rssi, rstamp);
2072 			ic->ic_bmiss_count = 0;
2073 			return;
2074 		}
2075 		/*
2076 		 * If scanning, just pass information to the scan module.
2077 		 */
2078 		if (ic->ic_flags & IEEE80211_F_SCAN) {
2079 			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
2080 				/*
2081 				 * Actively scanning a channel marked passive;
2082 				 * send a probe request now that we know there
2083 				 * is 802.11 traffic present.
2084 				 *
2085 				 * XXX check if the beacon we recv'd gives
2086 				 * us what we need and suppress the probe req
2087 				 */
2088 				ieee80211_probe_curchan(ic, 1);
2089 				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
2090 			}
2091 			ieee80211_add_scan(ic, &scan, wh,
2092 				subtype, rssi, rstamp);
2093 			return;
2094 		}
2095 		if (scan.capinfo & IEEE80211_CAPINFO_IBSS)
2096 			ieee80211_update_adhoc_node(ic, ni, wh, &scan, rssi,
2097 			    rstamp);
2098 		break;
2099 	}
2100 
2101 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2102 		if (ic->ic_opmode == IEEE80211_M_STA ||
2103 		    ic->ic_state != IEEE80211_S_RUN) {
2104 			ic->ic_stats.is_rx_mgtdiscard++;
2105 			return;
2106 		}
2107 		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
2108 			/* frame must be directed */
2109 			ic->ic_stats.is_rx_mgtdiscard++;	/* XXX stat */
2110 			return;
2111 		}
2112 
2113 		/*
2114 		 * prreq frame format
2115 		 *	[tlv] ssid
2116 		 *	[tlv] supported rates
2117 		 *	[tlv] extended supported rates
2118 		 */
2119 		ssid = rates = xrates = NULL;
2120 		while (frm < efrm) {
2121 			switch (*frm) {
2122 			case IEEE80211_ELEMID_SSID:
2123 				ssid = frm;
2124 				break;
2125 			case IEEE80211_ELEMID_RATES:
2126 				rates = frm;
2127 				break;
2128 			case IEEE80211_ELEMID_XRATES:
2129 				xrates = frm;
2130 				break;
2131 			}
2132 			frm += frm[1] + 2;
2133 		}
2134 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
2135 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN);
2136 		IEEE80211_VERIFY_SSID(ic->ic_bss, ssid);
2137 		if ((ic->ic_flags & IEEE80211_F_HIDESSID) && ssid[1] == 0) {
2138 			IEEE80211_DISCARD(ic, IEEE80211_MSG_INPUT,
2139 			    wh, ieee80211_mgt_subtype_name[subtype >>
2140 				IEEE80211_FC0_SUBTYPE_SHIFT],
2141 			    "%s", "no ssid with ssid suppression enabled");
2142 			ic->ic_stats.is_rx_ssidmismatch++; /*XXX*/
2143 			return;
2144 		}
2145 
2146 		if (ni == ic->ic_bss) {
2147 			if (ic->ic_opmode != IEEE80211_M_IBSS)
2148 				ni = ieee80211_tmp_node(ic, wh->i_addr2);
2149 			else if (IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr))
2150 				;
2151 			else {
2152 				/*
2153 				 * XXX Cannot tell if the sender is operating
2154 				 * in ibss mode.  But we need a new node to
2155 				 * send the response so blindly add them to the
2156 				 * neighbor table.
2157 				 */
2158 				ni = ieee80211_fakeup_adhoc_node(&ic->ic_sta,
2159 					wh->i_addr2);
2160 			}
2161 			if (ni == NULL)
2162 				return;
2163 			allocbs = 1;
2164 		} else
2165 			allocbs = 0;
2166 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2167 		    "[%s] recv probe req\n", ether_sprintf(wh->i_addr2));
2168 		ni->ni_rssi = rssi;
2169 		ni->ni_rstamp = rstamp;
2170 		rate = ieee80211_setup_rates(ni, rates, xrates,
2171 			  IEEE80211_R_DOSORT | IEEE80211_R_DOFRATE
2172 			| IEEE80211_R_DONEGO | IEEE80211_R_DODEL);
2173 		if (rate & IEEE80211_RATE_BASIC) {
2174 			IEEE80211_DISCARD(ic, IEEE80211_MSG_XRATE,
2175 			    wh, ieee80211_mgt_subtype_name[subtype >>
2176 				IEEE80211_FC0_SUBTYPE_SHIFT],
2177 			    "%s", "recv'd rate set invalid");
2178 		} else {
2179 			IEEE80211_SEND_MGMT(ic, ni,
2180 				IEEE80211_FC0_SUBTYPE_PROBE_RESP, 0);
2181 		}
2182 		if (allocbs && ic->ic_opmode != IEEE80211_M_IBSS) {
2183 			/* reclaim immediately */
2184 			ieee80211_free_node(ni);
2185 		}
2186 		break;
2187 
2188 	case IEEE80211_FC0_SUBTYPE_AUTH: {
2189 		u_int16_t algo, seq, status;
2190 		/*
2191 		 * auth frame format
2192 		 *	[2] algorithm
2193 		 *	[2] sequence
2194 		 *	[2] status
2195 		 *	[tlv*] challenge
2196 		 */
2197 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6);
2198 		algo   = le16toh(*(u_int16_t *)frm);
2199 		seq    = le16toh(*(u_int16_t *)(frm + 2));
2200 		status = le16toh(*(u_int16_t *)(frm + 4));
2201 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
2202 		    "[%s] recv auth frame with algorithm %d seq %d\n",
2203 		    ether_sprintf(wh->i_addr2), algo, seq);
2204 		/*
2205 		 * Consult the ACL policy module if setup.
2206 		 */
2207 		if (ic->ic_acl != NULL &&
2208 		    !ic->ic_acl->iac_check(ic, wh->i_addr2)) {
2209 			IEEE80211_DISCARD(ic, IEEE80211_MSG_ACL,
2210 			    wh, "auth", "%s", "disallowed by ACL");
2211 			ic->ic_stats.is_rx_acl++;
2212 			if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2213 				IEEE80211_SEND_MGMT(ic, ni,
2214 				    IEEE80211_FC0_SUBTYPE_AUTH,
2215 				    (seq+1) | (IEEE80211_STATUS_UNSPECIFIED<<16));
2216 			}
2217 			return;
2218 		}
2219 		if (ic->ic_flags & IEEE80211_F_COUNTERM) {
2220 			IEEE80211_DISCARD(ic,
2221 			    IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
2222 			    wh, "auth", "%s", "TKIP countermeasures enabled");
2223 			ic->ic_stats.is_rx_auth_countermeasures++;
2224 #ifndef IEEE80211_NO_HOSTAP
2225 			if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2226 				IEEE80211_SEND_MGMT(ic, ni,
2227 					IEEE80211_FC0_SUBTYPE_AUTH,
2228 					IEEE80211_REASON_MIC_FAILURE);
2229 			}
2230 #endif /* !IEEE80211_NO_HOSTAP */
2231 			return;
2232 		}
2233 		if (algo == IEEE80211_AUTH_ALG_SHARED)
2234 			ieee80211_auth_shared(ic, wh, frm + 6, efrm, ni, rssi,
2235 			    rstamp, seq, status);
2236 		else if (algo == IEEE80211_AUTH_ALG_OPEN)
2237 			ieee80211_auth_open(ic, wh, ni, rssi, rstamp, seq,
2238 			    status);
2239 		else {
2240 			IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
2241 			    wh, "auth", "unsupported alg %d", algo);
2242 			ic->ic_stats.is_rx_auth_unsupported++;
2243 #ifndef IEEE80211_NO_HOSTAP
2244 			if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2245 				/* XXX not right */
2246 				IEEE80211_SEND_MGMT(ic, ni,
2247 					IEEE80211_FC0_SUBTYPE_AUTH,
2248 					(seq+1) | (IEEE80211_STATUS_ALG<<16));
2249 			}
2250 #endif /* !IEEE80211_NO_HOSTAP */
2251 			return;
2252 		}
2253 		break;
2254 	}
2255 
2256 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2257 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: {
2258 		u_int16_t capinfo, lintval;
2259 		struct ieee80211_rsnparms rsn;
2260 		u_int8_t reason;
2261 
2262 		if (ic->ic_opmode != IEEE80211_M_HOSTAP ||
2263 		    ic->ic_state != IEEE80211_S_RUN) {
2264 			ic->ic_stats.is_rx_mgtdiscard++;
2265 			return;
2266 		}
2267 
2268 		if (subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2269 			reassoc = 1;
2270 			resp = IEEE80211_FC0_SUBTYPE_REASSOC_RESP;
2271 		} else {
2272 			reassoc = 0;
2273 			resp = IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
2274 		}
2275 		/*
2276 		 * asreq frame format
2277 		 *	[2] capability information
2278 		 *	[2] listen interval
2279 		 *	[6*] current AP address (reassoc only)
2280 		 *	[tlv] ssid
2281 		 *	[tlv] supported rates
2282 		 *	[tlv] extended supported rates
2283 		 *	[tlv] WPA or RSN
2284 		 */
2285 		IEEE80211_VERIFY_LENGTH(efrm - frm, (reassoc ? 10 : 4));
2286 		if (!IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_bss->ni_bssid)) {
2287 			IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
2288 			    wh, ieee80211_mgt_subtype_name[subtype >>
2289 				IEEE80211_FC0_SUBTYPE_SHIFT],
2290 			    "%s", "wrong bssid");
2291 			ic->ic_stats.is_rx_assoc_bss++;
2292 			return;
2293 		}
2294 		capinfo = le16toh(*(u_int16_t *)frm);	frm += 2;
2295 		lintval = le16toh(*(u_int16_t *)frm);	frm += 2;
2296 		if (reassoc)
2297 			frm += 6;	/* ignore current AP info */
2298 		ssid = rates = xrates = wpa = wme = NULL;
2299 		while (frm < efrm) {
2300 			switch (*frm) {
2301 			case IEEE80211_ELEMID_SSID:
2302 				ssid = frm;
2303 				break;
2304 			case IEEE80211_ELEMID_RATES:
2305 				rates = frm;
2306 				break;
2307 			case IEEE80211_ELEMID_XRATES:
2308 				xrates = frm;
2309 				break;
2310 			/* XXX verify only one of RSN and WPA ie's? */
2311 			case IEEE80211_ELEMID_RSN:
2312 				wpa = frm;
2313 				break;
2314 			case IEEE80211_ELEMID_VENDOR:
2315 				if (iswpaoui(frm))
2316 					wpa = frm;
2317 				else if (iswmeinfo(frm))
2318 					wme = frm;
2319 				/* XXX Atheros OUI support */
2320 				break;
2321 			}
2322 			frm += frm[1] + 2;
2323 		}
2324 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
2325 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN);
2326 		IEEE80211_VERIFY_SSID(ic->ic_bss, ssid);
2327 
2328 		if (ni == ic->ic_bss) {
2329 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
2330 			    "[%s] deny %s request, sta not authenticated\n",
2331 			    ether_sprintf(wh->i_addr2),
2332 			    reassoc ? "reassoc" : "assoc");
2333 			ieee80211_send_error(ic, ni, wh->i_addr2,
2334 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
2335 			    IEEE80211_REASON_ASSOC_NOT_AUTHED);
2336 			ic->ic_stats.is_rx_assoc_notauth++;
2337 			return;
2338 		}
2339 		/* assert right associstion security credentials */
2340 		if (wpa == NULL && (ic->ic_flags & IEEE80211_F_WPA)) {
2341 			IEEE80211_DPRINTF(ic,
2342 			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
2343 			    "[%s] no WPA/RSN IE in association request\n",
2344 			    ether_sprintf(wh->i_addr2));
2345 			IEEE80211_SEND_MGMT(ic, ni,
2346 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
2347 			    IEEE80211_REASON_RSN_REQUIRED);
2348 			ieee80211_node_leave(ic, ni);
2349 			/* XXX distinguish WPA/RSN? */
2350 			ic->ic_stats.is_rx_assoc_badwpaie++;
2351 			return;
2352 		}
2353 		if (wpa != NULL) {
2354 			/*
2355 			 * Parse WPA information element.  Note that
2356 			 * we initialize the param block from the node
2357 			 * state so that information in the IE overrides
2358 			 * our defaults.  The resulting parameters are
2359 			 * installed below after the association is assured.
2360 			 */
2361 			rsn = ni->ni_rsn;
2362 			if (wpa[0] != IEEE80211_ELEMID_RSN)
2363 				reason = ieee80211_parse_wpa(ic, wpa, &rsn, wh);
2364 			else
2365 				reason = ieee80211_parse_rsn(ic, wpa, &rsn, wh);
2366 			if (reason != 0) {
2367 				IEEE80211_SEND_MGMT(ic, ni,
2368 				    IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
2369 				ieee80211_node_leave(ic, ni);
2370 				/* XXX distinguish WPA/RSN? */
2371 				ic->ic_stats.is_rx_assoc_badwpaie++;
2372 				return;
2373 			}
2374 			IEEE80211_DPRINTF(ic,
2375 			    IEEE80211_MSG_ASSOC | IEEE80211_MSG_WPA,
2376 			    "[%s] %s ie: mc %u/%u uc %u/%u key %u caps 0x%x\n",
2377 			    ether_sprintf(wh->i_addr2),
2378 			    wpa[0] != IEEE80211_ELEMID_RSN ?  "WPA" : "RSN",
2379 			    rsn.rsn_mcastcipher, rsn.rsn_mcastkeylen,
2380 			    rsn.rsn_ucastcipher, rsn.rsn_ucastkeylen,
2381 			    rsn.rsn_keymgmt, rsn.rsn_caps);
2382 		}
2383 		/* discard challenge after association */
2384 		if (ni->ni_challenge != NULL) {
2385 			free(ni->ni_challenge, M_DEVBUF);
2386 			ni->ni_challenge = NULL;
2387 		}
2388 		/* NB: 802.11 spec says to ignore station's privacy bit */
2389 		if ((capinfo & IEEE80211_CAPINFO_ESS) == 0) {
2390 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
2391 			    "[%s] deny %s request, capability mismatch 0x%x\n",
2392 			    ether_sprintf(wh->i_addr2),
2393 			    reassoc ? "reassoc" : "assoc", capinfo);
2394 			IEEE80211_SEND_MGMT(ic, ni, resp,
2395 				IEEE80211_STATUS_CAPINFO);
2396 			ieee80211_node_leave(ic, ni);
2397 			ic->ic_stats.is_rx_assoc_capmismatch++;
2398 			return;
2399 		}
2400 		rate = ieee80211_setup_rates(ni, rates, xrates,
2401 				IEEE80211_R_DOSORT | IEEE80211_R_DOFRATE |
2402 				IEEE80211_R_DONEGO | IEEE80211_R_DODEL);
2403 		/*
2404 		 * If constrained to 11g-only stations reject an
2405 		 * 11b-only station.  We cheat a bit here by looking
2406 		 * at the max negotiated xmit rate and assuming anyone
2407 		 * with a best rate <24Mb/s is an 11b station.
2408 		 */
2409 		if ((rate & IEEE80211_RATE_BASIC) ||
2410 		    ((ic->ic_flags & IEEE80211_F_PUREG) && rate < 48)) {
2411 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
2412 			    "[%s] deny %s request, rate set mismatch\n",
2413 			    ether_sprintf(wh->i_addr2),
2414 			    reassoc ? "reassoc" : "assoc");
2415 			IEEE80211_SEND_MGMT(ic, ni, resp,
2416 				IEEE80211_STATUS_BASIC_RATE);
2417 			ieee80211_node_leave(ic, ni);
2418 			ic->ic_stats.is_rx_assoc_norate++;
2419 			return;
2420 		}
2421 		ni->ni_rssi = rssi;
2422 		ni->ni_rstamp = rstamp;
2423 		ni->ni_intval = lintval;
2424 		ni->ni_capinfo = capinfo;
2425 		ni->ni_chan = ic->ic_bss->ni_chan;
2426 		ni->ni_fhdwell = ic->ic_bss->ni_fhdwell;
2427 		ni->ni_fhindex = ic->ic_bss->ni_fhindex;
2428 		if (wpa != NULL) {
2429 			/*
2430 			 * Record WPA/RSN parameters for station, mark
2431 			 * node as using WPA and record information element
2432 			 * for applications that require it.
2433 			 */
2434 			ni->ni_rsn = rsn;
2435 			ieee80211_saveie(&ni->ni_wpa_ie, wpa);
2436 		} else if (ni->ni_wpa_ie != NULL) {
2437 			/*
2438 			 * Flush any state from a previous association.
2439 			 */
2440 			free(ni->ni_wpa_ie, M_DEVBUF);
2441 			ni->ni_wpa_ie = NULL;
2442 		}
2443 		if (wme != NULL) {
2444 			/*
2445 			 * Record WME parameters for station, mark node
2446 			 * as capable of QoS and record information
2447 			 * element for applications that require it.
2448 			 */
2449 			ieee80211_saveie(&ni->ni_wme_ie, wme);
2450 			ni->ni_flags |= IEEE80211_NODE_QOS;
2451 		} else if (ni->ni_wme_ie != NULL) {
2452 			/*
2453 			 * Flush any state from a previous association.
2454 			 */
2455 			free(ni->ni_wme_ie, M_DEVBUF);
2456 			ni->ni_wme_ie = NULL;
2457 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
2458 		}
2459 		ieee80211_node_join(ic, ni, resp);
2460 		break;
2461 	}
2462 
2463 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2464 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: {
2465 		u_int16_t capinfo, associd;
2466 		u_int16_t status;
2467 
2468 		if (ic->ic_opmode != IEEE80211_M_STA ||
2469 		    ic->ic_state != IEEE80211_S_ASSOC) {
2470 			ic->ic_stats.is_rx_mgtdiscard++;
2471 			return;
2472 		}
2473 
2474 		/*
2475 		 * asresp frame format
2476 		 *	[2] capability information
2477 		 *	[2] status
2478 		 *	[2] association ID
2479 		 *	[tlv] supported rates
2480 		 *	[tlv] extended supported rates
2481 		 *	[tlv] WME
2482 		 */
2483 		IEEE80211_VERIFY_LENGTH(efrm - frm, 6);
2484 		ni = ic->ic_bss;
2485 		capinfo = le16toh(*(u_int16_t *)frm);
2486 		frm += 2;
2487 		status = le16toh(*(u_int16_t *)frm);
2488 		frm += 2;
2489 		if (status != 0) {
2490 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2491 			    "[%s] %sassoc failed (reason %d)\n",
2492 			    ether_sprintf(wh->i_addr2),
2493 			    ISREASSOC(subtype) ?  "re" : "", status);
2494 			if (ni != ic->ic_bss)	/* XXX never true? */
2495 				ni->ni_fails++;
2496 			ic->ic_stats.is_rx_auth_fail++;	/* XXX */
2497 			return;
2498 		}
2499 		associd = le16toh(*(u_int16_t *)frm);
2500 		frm += 2;
2501 
2502 		rates = xrates = wpa = wme = NULL;
2503 		while (frm < efrm) {
2504 			switch (*frm) {
2505 			case IEEE80211_ELEMID_RATES:
2506 				rates = frm;
2507 				break;
2508 			case IEEE80211_ELEMID_XRATES:
2509 				xrates = frm;
2510 				break;
2511 			case IEEE80211_ELEMID_VENDOR:
2512 				if (iswmeoui(frm))
2513 					wme = frm;
2514 				/* XXX Atheros OUI support */
2515 				break;
2516 			}
2517 			frm += frm[1] + 2;
2518 		}
2519 
2520 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE);
2521 		rate = ieee80211_setup_rates(ni, rates, xrates,
2522 				IEEE80211_R_DOSORT | IEEE80211_R_DOFRATE |
2523 				IEEE80211_R_DONEGO | IEEE80211_R_DODEL);
2524 		if (rate & IEEE80211_RATE_BASIC) {
2525 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2526 			    "[%s] %sassoc failed (rate set mismatch)\n",
2527 			    ether_sprintf(wh->i_addr2),
2528 			    ISREASSOC(subtype) ?  "re" : "");
2529 			if (ni != ic->ic_bss)	/* XXX never true? */
2530 				ni->ni_fails++;
2531 			ic->ic_stats.is_rx_assoc_norate++;
2532 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
2533 			return;
2534 		}
2535 
2536 		ni->ni_capinfo = capinfo;
2537 		ni->ni_associd = associd;
2538 		if (wme != NULL &&
2539 		    ieee80211_parse_wmeparams(ic, wme, wh) >= 0) {
2540 			ni->ni_flags |= IEEE80211_NODE_QOS;
2541 			ieee80211_wme_updateparams(ic);
2542 		} else
2543 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
2544 		/*
2545 		 * Configure state now that we are associated.
2546 		 *
2547 		 * XXX may need different/additional driver callbacks?
2548 		 */
2549 		if (ic->ic_curmode == IEEE80211_MODE_11A ||
2550 		    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
2551 			ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
2552 			ic->ic_flags &= ~IEEE80211_F_USEBARKER;
2553 		} else {
2554 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
2555 			ic->ic_flags |= IEEE80211_F_USEBARKER;
2556 		}
2557 		ieee80211_set_shortslottime(ic,
2558 			ic->ic_curmode == IEEE80211_MODE_11A ||
2559 			(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
2560 		/*
2561 		 * Honor ERP protection.
2562 		 *
2563 		 * NB: ni_erp should zero for non-11g operation.
2564 		 * XXX check ic_curmode anyway?
2565 		 */
2566 		if (ic->ic_curmode == IEEE80211_MODE_11G &&
2567 		    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
2568 			ic->ic_flags |= IEEE80211_F_USEPROT;
2569 		else
2570 			ic->ic_flags &= ~IEEE80211_F_USEPROT;
2571 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2572 		    "[%s] %sassoc success: %s preamble, %s slot time%s%s\n",
2573 		    ether_sprintf(wh->i_addr2),
2574 		    ISREASSOC(subtype) ? "re" : "",
2575 		    ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
2576 		    ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
2577 		    ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
2578 		    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : ""
2579 		);
2580 		ieee80211_new_state(ic, IEEE80211_S_RUN, subtype);
2581 		break;
2582 	}
2583 
2584 	case IEEE80211_FC0_SUBTYPE_DEAUTH: {
2585 		u_int16_t reason;
2586 
2587 		if (ic->ic_state == IEEE80211_S_SCAN) {
2588 			ic->ic_stats.is_rx_mgtdiscard++;
2589 			return;
2590 		}
2591 		/*
2592 		 * deauth frame format
2593 		 *	[2] reason
2594 		 */
2595 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2);
2596 		reason = le16toh(*(u_int16_t *)frm);
2597 		ic->ic_stats.is_rx_deauth++;
2598 		IEEE80211_NODE_STAT(ni, rx_deauth);
2599 
2600 		if (!IEEE80211_ADDR_EQ(wh->i_addr1, ic->ic_myaddr)) {
2601 			/* Not intended for this station. */
2602 			ic->ic_stats.is_rx_mgtdiscard++;
2603 			break;
2604 		}
2605 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
2606 		    "[%s] recv deauthenticate (reason %d)\n",
2607 		    ether_sprintf(ni->ni_macaddr), reason);
2608 		switch (ic->ic_opmode) {
2609 		case IEEE80211_M_STA:
2610 			ieee80211_new_state(ic, IEEE80211_S_AUTH,
2611 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
2612 			break;
2613 		case IEEE80211_M_HOSTAP:
2614 #ifndef IEEE80211_NO_HOSTAP
2615 			if (ni != ic->ic_bss)
2616 				ieee80211_node_leave(ic, ni);
2617 #endif /* !IEEE80211_NO_HOSTAP */
2618 			break;
2619 		default:
2620 			ic->ic_stats.is_rx_mgtdiscard++;
2621 			break;
2622 		}
2623 		break;
2624 	}
2625 
2626 	case IEEE80211_FC0_SUBTYPE_DISASSOC: {
2627 		u_int16_t reason;
2628 
2629 		if (ic->ic_state != IEEE80211_S_RUN &&
2630 		    ic->ic_state != IEEE80211_S_ASSOC &&
2631 		    ic->ic_state != IEEE80211_S_AUTH) {
2632 			ic->ic_stats.is_rx_mgtdiscard++;
2633 			return;
2634 		}
2635 		/*
2636 		 * disassoc frame format
2637 		 *	[2] reason
2638 		 */
2639 		IEEE80211_VERIFY_LENGTH(efrm - frm, 2);
2640 		reason = le16toh(*(u_int16_t *)frm);
2641 		ic->ic_stats.is_rx_disassoc++;
2642 		IEEE80211_NODE_STAT(ni, rx_disassoc);
2643 
2644 		if (!IEEE80211_ADDR_EQ(wh->i_addr1, ic->ic_myaddr)) {
2645 			/* Not intended for this station. */
2646 			ic->ic_stats.is_rx_mgtdiscard++;
2647 			break;
2648 		}
2649 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
2650 		    "[%s] recv disassociate (reason %d)\n",
2651 		    ether_sprintf(ni->ni_macaddr), reason);
2652 		switch (ic->ic_opmode) {
2653 		case IEEE80211_M_STA:
2654 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
2655 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
2656 			break;
2657 		case IEEE80211_M_HOSTAP:
2658 #ifndef IEEE80211_NO_HOSTAP
2659 			if (ni != ic->ic_bss)
2660 				ieee80211_node_leave(ic, ni);
2661 #endif /* !IEEE80211_NO_HOSTAP */
2662 			break;
2663 		default:
2664 			ic->ic_stats.is_rx_mgtdiscard++;
2665 			break;
2666 		}
2667 		break;
2668 	}
2669 	default:
2670 		IEEE80211_DISCARD(ic, IEEE80211_MSG_ANY,
2671 		     wh, "mgt", "subtype 0x%x not handled", subtype);
2672 		ic->ic_stats.is_rx_badsubtype++;
2673 		break;
2674 	}
2675 #undef ISREASSOC
2676 #undef ISPROBE
2677 }
2678 #undef IEEE80211_VERIFY_LENGTH
2679 #undef IEEE80211_VERIFY_ELEMENT
2680 
2681 #ifndef IEEE80211_NO_HOSTAP
2682 /*
2683  * Handle station power-save state change.
2684  */
2685 static void
2686 ieee80211_node_pwrsave(struct ieee80211_node *ni, int enable)
2687 {
2688 	struct ieee80211com *ic = ni->ni_ic;
2689 	struct mbuf *m;
2690 
2691 	if (enable) {
2692 		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) == 0)
2693 			ic->ic_ps_sta++;
2694 		ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
2695 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
2696 		    "[%s] power save mode on, %u sta's in ps mode\n",
2697 		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
2698 		return;
2699 	}
2700 
2701 	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT)
2702 		ic->ic_ps_sta--;
2703 	ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
2704 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
2705 	    "[%s] power save mode off, %u sta's in ps mode\n",
2706 	    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
2707 	/* XXX if no stations in ps mode, flush mc frames */
2708 
2709 	/*
2710 	 * Flush queued unicast frames.
2711 	 */
2712 	if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0) {
2713 		if (ic->ic_set_tim != NULL)
2714 			ic->ic_set_tim(ni, 0);		/* just in case */
2715 		return;
2716 	}
2717 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
2718 	    "[%s] flush ps queue, %u packets queued\n",
2719 	    ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_SAVEQ_QLEN(ni));
2720 	for (;;) {
2721 		int qlen;
2722 
2723 		IEEE80211_NODE_SAVEQ_DEQUEUE(ni, m, qlen);
2724 		if (m == NULL)
2725 			break;
2726 		/*
2727 		 * If this is the last packet, turn off the TIM bit.
2728 		 * If there are more packets, set the more packets bit
2729 		 * in the mbuf so ieee80211_encap will mark the 802.11
2730 		 * head to indicate more data frames will follow.
2731 		 */
2732 		if (qlen != 0)
2733 			m->m_flags |= M_MORE_DATA;
2734 		/* XXX need different driver interface */
2735 		/* XXX bypasses q max */
2736 		IF_ENQUEUE(&ic->ic_ifp->if_snd, m);
2737 	}
2738 	if (ic->ic_set_tim != NULL)
2739 		ic->ic_set_tim(ni, 0);
2740 }
2741 
2742 /*
2743  * Process a received ps-poll frame.
2744  */
2745 static void
2746 ieee80211_recv_pspoll(struct ieee80211com *ic,
2747 	struct ieee80211_node *ni, struct mbuf *m0)
2748 {
2749 	struct ieee80211_frame_min *wh;
2750 	struct mbuf *m;
2751 	u_int16_t aid;
2752 	int qlen;
2753 
2754 	wh = mtod(m0, struct ieee80211_frame_min *);
2755 	if (ni->ni_associd == 0) {
2756 		IEEE80211_DISCARD(ic, IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2757 		    (struct ieee80211_frame *) wh, "ps-poll",
2758 		    "%s", "unassociated station");
2759 		ic->ic_stats.is_ps_unassoc++;
2760 		IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
2761 			IEEE80211_REASON_NOT_ASSOCED);
2762 		return;
2763 	}
2764 
2765 	aid = le16toh(*(u_int16_t *)wh->i_dur);
2766 	if (aid != ni->ni_associd) {
2767 		IEEE80211_DISCARD(ic, IEEE80211_MSG_POWER | IEEE80211_MSG_DEBUG,
2768 		    (struct ieee80211_frame *) wh, "ps-poll",
2769 		    "aid mismatch: sta aid 0x%x poll aid 0x%x",
2770 		    ni->ni_associd, aid);
2771 		ic->ic_stats.is_ps_badaid++;
2772 		IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
2773 			IEEE80211_REASON_NOT_ASSOCED);
2774 		return;
2775 	}
2776 
2777 	/* Okay, take the first queued packet and put it out... */
2778 	IEEE80211_NODE_SAVEQ_DEQUEUE(ni, m, qlen);
2779 	if (m == NULL) {
2780 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
2781 		    "[%s] recv ps-poll, but queue empty\n",
2782 		    ether_sprintf(wh->i_addr2));
2783 		ieee80211_send_nulldata(ieee80211_ref_node(ni));
2784 		ic->ic_stats.is_ps_qempty++;	/* XXX node stat */
2785 		if (ic->ic_set_tim != NULL)
2786 			ic->ic_set_tim(ni, 0);	/* just in case */
2787 		return;
2788 	}
2789 	/*
2790 	 * If there are more packets, set the more packets bit
2791 	 * in the packet dispatched to the station; otherwise
2792 	 * turn off the TIM bit.
2793 	 */
2794 	if (qlen != 0) {
2795 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
2796 		    "[%s] recv ps-poll, send packet, %u still queued\n",
2797 		    ether_sprintf(ni->ni_macaddr), qlen);
2798 		m->m_flags |= M_MORE_DATA;
2799 	} else {
2800 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
2801 		    "[%s] recv ps-poll, send packet, queue empty\n",
2802 		    ether_sprintf(ni->ni_macaddr));
2803 		if (ic->ic_set_tim != NULL)
2804 			ic->ic_set_tim(ni, 0);
2805 	}
2806 	m->m_flags |= M_PWR_SAV;		/* bypass PS handling */
2807 	IF_ENQUEUE(&ic->ic_ifp->if_snd, m);
2808 }
2809 #endif /* !IEEE80211_NO_HOSTAP */
2810 
2811 #ifdef IEEE80211_DEBUG
2812 /*
2813  * Debugging support.
2814  */
2815 
2816 /*
2817  * Return the bssid of a frame.
2818  */
2819 static const u_int8_t *
2820 ieee80211_getbssid(struct ieee80211com *ic, const struct ieee80211_frame *wh)
2821 {
2822 	if (ic->ic_opmode == IEEE80211_M_STA)
2823 		return wh->i_addr2;
2824 	if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) != IEEE80211_FC1_DIR_NODS)
2825 		return wh->i_addr1;
2826 	if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
2827 		return wh->i_addr1;
2828 	return wh->i_addr3;
2829 }
2830 
2831 void
2832 ieee80211_note(struct ieee80211com *ic, const char *fmt, ...)
2833 {
2834 	char buf[128];		/* XXX */
2835 	va_list ap;
2836 
2837 	va_start(ap, fmt);
2838 	vsnprintf(buf, sizeof(buf), fmt, ap);
2839 	va_end(ap);
2840 
2841 	if_printf(ic->ic_ifp, "%s", buf);	/* NB: no \n */
2842 }
2843 
2844 void
2845 ieee80211_note_frame(struct ieee80211com *ic,
2846 	const struct ieee80211_frame *wh,
2847 	const char *fmt, ...)
2848 {
2849 	char buf[128];		/* XXX */
2850 	va_list ap;
2851 
2852 	va_start(ap, fmt);
2853 	vsnprintf(buf, sizeof(buf), fmt, ap);
2854 	va_end(ap);
2855 	if_printf(ic->ic_ifp, "[%s] %s\n",
2856 		ether_sprintf(ieee80211_getbssid(ic, wh)), buf);
2857 }
2858 
2859 void
2860 ieee80211_note_mac(struct ieee80211com *ic,
2861 	const u_int8_t mac[IEEE80211_ADDR_LEN],
2862 	const char *fmt, ...)
2863 {
2864 	char buf[128];		/* XXX */
2865 	va_list ap;
2866 
2867 	va_start(ap, fmt);
2868 	vsnprintf(buf, sizeof(buf), fmt, ap);
2869 	va_end(ap);
2870 	if_printf(ic->ic_ifp, "[%s] %s\n", ether_sprintf(mac), buf);
2871 }
2872 
2873 static void
2874 ieee80211_discard_frame(struct ieee80211com *ic,
2875 	const struct ieee80211_frame *wh,
2876 	const char *type, const char *fmt, ...)
2877 {
2878 	va_list ap;
2879 
2880 	printf("[%s:%s] discard ", ic->ic_ifp->if_xname,
2881 		ether_sprintf(ieee80211_getbssid(ic, wh)));
2882 	if (type != NULL)
2883 		printf("%s frame, ", type);
2884 	else
2885 		printf("frame, ");
2886 	va_start(ap, fmt);
2887 	vprintf(fmt, ap);
2888 	va_end(ap);
2889 	printf("\n");
2890 }
2891 
2892 static void
2893 ieee80211_discard_ie(struct ieee80211com *ic,
2894 	const struct ieee80211_frame *wh,
2895 	const char *type, const char *fmt, ...)
2896 {
2897 	va_list ap;
2898 
2899 	printf("[%s:%s] discard ", ic->ic_ifp->if_xname,
2900 		ether_sprintf(ieee80211_getbssid(ic, wh)));
2901 	if (type != NULL)
2902 		printf("%s information element, ", type);
2903 	else
2904 		printf("information element, ");
2905 	va_start(ap, fmt);
2906 	vprintf(fmt, ap);
2907 	va_end(ap);
2908 	printf("\n");
2909 }
2910 
2911 static void
2912 ieee80211_discard_mac(struct ieee80211com *ic,
2913 	const u_int8_t mac[IEEE80211_ADDR_LEN],
2914 	const char *type, const char *fmt, ...)
2915 {
2916 	va_list ap;
2917 
2918 	printf("[%s:%s] discard ", ic->ic_ifp->if_xname, ether_sprintf(mac));
2919 	if (type != NULL)
2920 		printf("%s frame, ", type);
2921 	else
2922 		printf("frame, ");
2923 	va_start(ap, fmt);
2924 	vprintf(fmt, ap);
2925 	va_end(ap);
2926 	printf("\n");
2927 }
2928 #endif /* IEEE80211_DEBUG */
2929