xref: /openbsd-src/sys/net80211/ieee80211_input.c (revision 850e275390052b330d93020bf619a739a3c277ac)
1 /*	$OpenBSD: ieee80211_input.c,v 1.106 2008/09/27 15:16:09 damien Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 Atsushi Onoe
5  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
6  * Copyright (c) 2007, 2008 Damien Bergamini
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "bpfilter.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/mbuf.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 #include <sys/socket.h>
40 #include <sys/sockio.h>
41 #include <sys/endian.h>
42 #include <sys/errno.h>
43 #include <sys/proc.h>
44 #include <sys/sysctl.h>
45 #include <sys/endian.h>
46 
47 #include <net/if.h>
48 #include <net/if_dl.h>
49 #include <net/if_media.h>
50 #include <net/if_arp.h>
51 #include <net/if_llc.h>
52 
53 #if NBPFILTER > 0
54 #include <net/bpf.h>
55 #endif
56 
57 #ifdef INET
58 #include <netinet/in.h>
59 #include <netinet/if_ether.h>
60 #endif
61 
62 #include <net80211/ieee80211_var.h>
63 #include <net80211/ieee80211_priv.h>
64 
65 void	ieee80211_deliver_data(struct ieee80211com *, struct mbuf *,
66 	    struct ieee80211_node *);
67 int	ieee80211_parse_edca_params_body(struct ieee80211com *,
68 	    const u_int8_t *);
69 int	ieee80211_parse_edca_params(struct ieee80211com *, const u_int8_t *);
70 int	ieee80211_parse_wmm_params(struct ieee80211com *, const u_int8_t *);
71 enum	ieee80211_cipher ieee80211_parse_rsn_cipher(const u_int8_t[]);
72 enum	ieee80211_akm ieee80211_parse_rsn_akm(const u_int8_t[]);
73 int	ieee80211_parse_rsn_body(struct ieee80211com *, const u_int8_t *,
74 	    u_int, struct ieee80211_rsnparams *);
75 int	ieee80211_save_ie(const u_int8_t *, u_int8_t **);
76 #ifndef IEEE80211_STA_ONLY
77 void	ieee80211_recv_pspoll(struct ieee80211com *, struct mbuf *,
78 	    struct ieee80211_node *);
79 #endif
80 void	ieee80211_recv_probe_resp(struct ieee80211com *, struct mbuf *,
81 	    struct ieee80211_node *, struct ieee80211_rxinfo *, int);
82 #ifndef IEEE80211_STA_ONLY
83 void	ieee80211_recv_probe_req(struct ieee80211com *, struct mbuf *,
84 	    struct ieee80211_node *, struct ieee80211_rxinfo *);
85 #endif
86 void	ieee80211_recv_auth(struct ieee80211com *, struct mbuf *,
87 	    struct ieee80211_node *, struct ieee80211_rxinfo *);
88 #ifndef IEEE80211_STA_ONLY
89 void	ieee80211_recv_assoc_req(struct ieee80211com *, struct mbuf *,
90 	    struct ieee80211_node *, struct ieee80211_rxinfo *, int);
91 #endif
92 void	ieee80211_recv_assoc_resp(struct ieee80211com *, struct mbuf *,
93 	    struct ieee80211_node *, int);
94 void	ieee80211_recv_deauth(struct ieee80211com *, struct mbuf *,
95 	    struct ieee80211_node *);
96 void	ieee80211_recv_disassoc(struct ieee80211com *, struct mbuf *,
97 	    struct ieee80211_node *);
98 void	ieee80211_recv_action(struct ieee80211com *, struct mbuf *,
99 	    struct ieee80211_node *);
100 
101 /*
102  * Retrieve the length in bytes of a 802.11 header.
103  */
104 u_int
105 ieee80211_get_hdrlen(const struct ieee80211_frame *wh)
106 {
107 	u_int size = sizeof(*wh);
108 
109 	/* NB: does not work with control frames */
110 	KASSERT(ieee80211_has_seq(wh));
111 
112 	if (ieee80211_has_addr4(wh))
113 		size += IEEE80211_ADDR_LEN;	/* i_addr4 */
114 	if (ieee80211_has_qos(wh))
115 		size += sizeof(u_int16_t);	/* i_qos */
116 	if (ieee80211_has_htc(wh))
117 		size += sizeof(u_int32_t);	/* i_ht */
118 	return size;
119 }
120 
121 /*
122  * Process a received frame.  The node associated with the sender
123  * should be supplied.  If nothing was found in the node table then
124  * the caller is assumed to supply a reference to ic_bss instead.
125  * The RSSI and a timestamp are also supplied.  The RSSI data is used
126  * during AP scanning to select a AP to associate with; it can have
127  * any units so long as values have consistent units and higher values
128  * mean ``better signal''.  The receive timestamp is currently not used
129  * by the 802.11 layer.
130  */
131 void
132 ieee80211_input(struct ifnet *ifp, struct mbuf *m, struct ieee80211_node *ni,
133     struct ieee80211_rxinfo *rxi)
134 {
135 	struct ieee80211com *ic = (void *)ifp;
136 	struct ieee80211_frame *wh;
137 	u_int16_t *orxseq, nrxseq;
138 	u_int8_t dir, type, subtype, tid;
139 	int hdrlen;
140 
141 #ifdef DIAGNOSTIC
142 	if (ni == NULL)
143 		panic("null node");
144 #endif
145 	/* in monitor mode, send everything directly to bpf */
146 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
147 		goto out;
148 
149 	/*
150 	 * Do not process frames without an Address 2 field any further.
151 	 * Only CTS and ACK control frames do not have this field.
152 	 */
153 	if (m->m_len < sizeof(struct ieee80211_frame_min)) {
154 		DPRINTF(("frame too short, len %u\n", m->m_len));
155 		ic->ic_stats.is_rx_tooshort++;
156 		goto out;
157 	}
158 
159 	wh = mtod(m, struct ieee80211_frame *);
160 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
161 	    IEEE80211_FC0_VERSION_0) {
162 		DPRINTF(("frame with wrong version: %x\n", wh->i_fc[0]));
163 		ic->ic_stats.is_rx_badversion++;
164 		goto err;
165 	}
166 
167 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
168 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
169 
170 	if (type != IEEE80211_FC0_TYPE_CTL) {
171 		hdrlen = ieee80211_get_hdrlen(wh);
172 		if (m->m_len < hdrlen) {
173 			DPRINTF(("frame too short, len %u\n", m->m_len));
174 			ic->ic_stats.is_rx_tooshort++;
175 			goto err;
176 		}
177 	}
178 	/* check and save sequence control field, if present */
179 	if (ieee80211_has_seq(wh) &&
180 	    ic->ic_state != IEEE80211_S_SCAN) {
181 		nrxseq = letoh16(*(u_int16_t *)wh->i_seq) >>
182 		    IEEE80211_SEQ_SEQ_SHIFT;
183 		if (ieee80211_has_qos(wh)) {
184 			tid = ieee80211_get_qos(wh) & IEEE80211_QOS_TID;
185 			orxseq = &ni->ni_qos_rxseqs[tid];
186 		} else
187 			orxseq = &ni->ni_rxseq;
188 		if ((wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
189 		    nrxseq == *orxseq) {
190 			/* duplicate, silently discarded */
191 			ic->ic_stats.is_rx_dup++;
192 			goto out;
193 		}
194 		*orxseq = nrxseq;
195 	}
196 	if (ic->ic_state != IEEE80211_S_SCAN) {
197 		ni->ni_rssi = rxi->rxi_rssi;
198 		ni->ni_rstamp = rxi->rxi_tstamp;
199 		ni->ni_inact = 0;
200 	}
201 
202 #ifndef IEEE80211_STA_ONLY
203 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
204 	    (ic->ic_caps & IEEE80211_C_APPMGT) &&
205 	    ni->ni_state == IEEE80211_STA_ASSOC) {
206 		if (wh->i_fc[1] & IEEE80211_FC1_PWR_MGT) {
207 			if (ni->ni_pwrsave == IEEE80211_PS_AWAKE) {
208 				/* turn on PS mode */
209 				ni->ni_pwrsave = IEEE80211_PS_DOZE;
210 				ic->ic_pssta++;
211 				DPRINTF(("PS mode on for %s, count %d\n",
212 				    ether_sprintf(wh->i_addr2), ic->ic_pssta));
213 			}
214 		} else if (ni->ni_pwrsave == IEEE80211_PS_DOZE) {
215 			/* turn off PS mode */
216 			ni->ni_pwrsave = IEEE80211_PS_AWAKE;
217 			ic->ic_pssta--;
218 			DPRINTF(("PS mode off for %s, count %d\n",
219 			    ether_sprintf(wh->i_addr2), ic->ic_pssta));
220 
221 			(*ic->ic_set_tim)(ic, ni->ni_associd, 0);
222 
223 			/* dequeue buffered unicast frames */
224 			while (!IF_IS_EMPTY(&ni->ni_savedq)) {
225 				struct mbuf *m;
226 				IF_DEQUEUE(&ni->ni_savedq, m);
227 				IF_ENQUEUE(&ic->ic_pwrsaveq, m);
228 				(*ifp->if_start)(ifp);
229 			}
230 		}
231 	}
232 #endif
233 	switch (type) {
234 	case IEEE80211_FC0_TYPE_DATA:
235 		switch (ic->ic_opmode) {
236 		case IEEE80211_M_STA:
237 			if (dir != IEEE80211_FC1_DIR_FROMDS) {
238 				ic->ic_stats.is_rx_wrongdir++;
239 				goto out;
240 			}
241 			if (ic->ic_state != IEEE80211_S_SCAN &&
242 			    !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid)) {
243 				/* Source address is not our BSS. */
244 				DPRINTF(("discard frame from SA %s\n",
245 				    ether_sprintf(wh->i_addr2)));
246 				ic->ic_stats.is_rx_wrongbss++;
247 				goto out;
248 			}
249 			if ((ifp->if_flags & IFF_SIMPLEX) &&
250 			    IEEE80211_IS_MULTICAST(wh->i_addr1) &&
251 			    IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_myaddr)) {
252 				/*
253 				 * In IEEE802.11 network, multicast frame
254 				 * sent from me is broadcasted from AP.
255 				 * It should be silently discarded for
256 				 * SIMPLEX interface.
257 				 */
258 				ic->ic_stats.is_rx_mcastecho++;
259 				goto out;
260 			}
261 			break;
262 #ifndef IEEE80211_STA_ONLY
263 		case IEEE80211_M_IBSS:
264 		case IEEE80211_M_AHDEMO:
265 			if (dir != IEEE80211_FC1_DIR_NODS) {
266 				ic->ic_stats.is_rx_wrongdir++;
267 				goto out;
268 			}
269 			if (ic->ic_state != IEEE80211_S_SCAN &&
270 			    !IEEE80211_ADDR_EQ(wh->i_addr3,
271 				ic->ic_bss->ni_bssid) &&
272 			    !IEEE80211_ADDR_EQ(wh->i_addr3,
273 				etherbroadcastaddr)) {
274 				/* Destination is not our BSS or broadcast. */
275 				DPRINTF(("discard data frame to DA %s\n",
276 				    ether_sprintf(wh->i_addr3)));
277 				ic->ic_stats.is_rx_wrongbss++;
278 				goto out;
279 			}
280 			break;
281 		case IEEE80211_M_HOSTAP:
282 			if (dir != IEEE80211_FC1_DIR_TODS) {
283 				ic->ic_stats.is_rx_wrongdir++;
284 				goto out;
285 			}
286 			if (ic->ic_state != IEEE80211_S_SCAN &&
287 			    !IEEE80211_ADDR_EQ(wh->i_addr1,
288 				ic->ic_bss->ni_bssid) &&
289 			    !IEEE80211_ADDR_EQ(wh->i_addr1,
290 				etherbroadcastaddr)) {
291 				/* BSS is not us or broadcast. */
292 				DPRINTF(("discard data frame to BSS %s\n",
293 				    ether_sprintf(wh->i_addr1)));
294 				ic->ic_stats.is_rx_wrongbss++;
295 				goto out;
296 			}
297 			/* check if source STA is associated */
298 			if (ni == ic->ic_bss) {
299 				DPRINTF(("data from unknown src %s\n",
300 				    ether_sprintf(wh->i_addr2)));
301 				/* NB: caller deals with reference */
302 				ni = ieee80211_dup_bss(ic, wh->i_addr2);
303 				if (ni != NULL) {
304 					IEEE80211_SEND_MGMT(ic, ni,
305 					    IEEE80211_FC0_SUBTYPE_DEAUTH,
306 					    IEEE80211_REASON_NOT_AUTHED);
307 				}
308 				ic->ic_stats.is_rx_notassoc++;
309 				goto err;
310 			}
311 			if (ni->ni_associd == 0) {
312 				DPRINTF(("data from unassoc src %s\n",
313 				    ether_sprintf(wh->i_addr2)));
314 				IEEE80211_SEND_MGMT(ic, ni,
315 				    IEEE80211_FC0_SUBTYPE_DISASSOC,
316 				    IEEE80211_REASON_NOT_ASSOCED);
317 				ic->ic_stats.is_rx_notassoc++;
318 				goto err;
319 			}
320 			break;
321 #endif	/* IEEE80211_STA_ONLY */
322 		default:
323 			/* can't get there */
324 			goto out;
325 		}
326 
327 		if ((ic->ic_flags & IEEE80211_F_WEPON) ||
328 		    ((ic->ic_flags & IEEE80211_F_RSNON) &&
329 		     (ni->ni_flags & IEEE80211_NODE_RXPROT))) {
330 			/* protection is on for Rx */
331 			if (!(rxi->rxi_flags & IEEE80211_RXI_HWDEC)) {
332 				if (!(wh->i_fc[1] & IEEE80211_FC1_PROTECTED)) {
333 					/* drop unencrypted */
334 					ic->ic_stats.is_rx_unencrypted++;
335 					goto err;
336 				}
337 				/* do software decryption */
338 				m = ieee80211_decrypt(ic, m, ni);
339 				if (m == NULL) {
340 					ic->ic_stats.is_rx_wepfail++;
341 					goto err;
342 				}
343 				wh = mtod(m, struct ieee80211_frame *);
344 			}
345 		} else if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) ||
346 		    (rxi->rxi_flags & IEEE80211_RXI_HWDEC)) {
347 			/* frame encrypted but protection off for Rx */
348 			ic->ic_stats.is_rx_nowep++;
349 			goto out;
350 		}
351 
352 #if NBPFILTER > 0
353 		/* copy to listener after decrypt */
354 		if (ic->ic_rawbpf)
355 			bpf_mtap(ic->ic_rawbpf, m, BPF_DIRECTION_IN);
356 #endif
357 		m = ieee80211_decap(ifp, m, hdrlen);
358 		if (m == NULL) {
359 			DPRINTF(("decapsulation error for src %s\n",
360 			    ether_sprintf(wh->i_addr2)));
361 			ic->ic_stats.is_rx_decap++;
362 			goto err;
363 		}
364 		ieee80211_deliver_data(ic, m, ni);
365 		return;
366 
367 	case IEEE80211_FC0_TYPE_MGT:
368 		if (dir != IEEE80211_FC1_DIR_NODS) {
369 			ic->ic_stats.is_rx_wrongdir++;
370 			goto err;
371 		}
372 #ifndef IEEE80211_STA_ONLY
373 		if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
374 			ic->ic_stats.is_rx_ahdemo_mgt++;
375 			goto out;
376 		}
377 #endif
378 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
379 
380 		/* drop frames without interest */
381 		if (ic->ic_state == IEEE80211_S_SCAN) {
382 			if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
383 			    subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
384 				ic->ic_stats.is_rx_mgtdiscard++;
385 				goto out;
386 			}
387 		}
388 
389 		if (ni->ni_flags & IEEE80211_NODE_RXMGMTPROT) {
390 			/* MMPDU protection is on for Rx */
391 			if (subtype == IEEE80211_FC0_SUBTYPE_DISASSOC ||
392 			    subtype == IEEE80211_FC0_SUBTYPE_DEAUTH ||
393 			    subtype == IEEE80211_FC0_SUBTYPE_ACTION) {
394 				if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
395 				    !(wh->i_fc[1] & IEEE80211_FC1_PROTECTED)) {
396 					/* unicast mgmt not encrypted */
397 					goto out;
398 				}
399 				/* do software decryption */
400 				m = ieee80211_decrypt(ic, m, ni);
401 				if (m == NULL) {
402 					/* XXX stats */
403 					goto out;
404 				}
405 				wh = mtod(m, struct ieee80211_frame *);
406 			}
407 		} else if ((ic->ic_flags & IEEE80211_F_RSNON) &&
408 		    (wh->i_fc[1] & IEEE80211_FC1_PROTECTED)) {
409 			/* encrypted but MMPDU Rx protection off for TA */
410 			goto out;
411 		}
412 
413 		if (ifp->if_flags & IFF_DEBUG) {
414 			/* avoid to print too many frames */
415 			int doprint = 0;
416 
417 			switch (subtype) {
418 			case IEEE80211_FC0_SUBTYPE_BEACON:
419 				if (ic->ic_state == IEEE80211_S_SCAN)
420 					doprint = 1;
421 				break;
422 #ifndef IEEE80211_STA_ONLY
423 			case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
424 				if (ic->ic_opmode == IEEE80211_M_IBSS)
425 					doprint = 1;
426 				break;
427 #endif
428 			default:
429 				doprint = 1;
430 				break;
431 			}
432 #ifdef IEEE80211_DEBUG
433 			doprint += ieee80211_debug;
434 #endif
435 			if (doprint)
436 				printf("%s: received %s from %s rssi %d mode %s\n",
437 				    ifp->if_xname,
438 				    ieee80211_mgt_subtype_name[subtype
439 				    >> IEEE80211_FC0_SUBTYPE_SHIFT],
440 				    ether_sprintf(wh->i_addr2), rxi->rxi_rssi,
441 				    ieee80211_phymode_name[ieee80211_chan2mode(ic,
442 				    ic->ic_bss->ni_chan)]);
443 		}
444 #if NBPFILTER > 0
445 		if (ic->ic_rawbpf)
446 			bpf_mtap(ic->ic_rawbpf, m, BPF_DIRECTION_IN);
447 		/*
448 		 * Drop mbuf if it was filtered by bpf. Normally, this is
449 		 * done in ether_input() but IEEE 802.11 management frames
450 		 * are a special case.
451 		 */
452 		if (m->m_flags & M_FILDROP) {
453 			m_freem(m);
454 			return;
455 		}
456 #endif
457 		(*ic->ic_recv_mgmt)(ic, m, ni, rxi, subtype);
458 		m_freem(m);
459 		return;
460 
461 	case IEEE80211_FC0_TYPE_CTL:
462 		ic->ic_stats.is_rx_ctl++;
463 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
464 		switch (subtype) {
465 #ifndef IEEE80211_STA_ONLY
466 		case IEEE80211_FC0_SUBTYPE_PS_POLL:
467 			ieee80211_recv_pspoll(ic, m, ni);
468 			break;
469 #endif
470 		case IEEE80211_FC0_SUBTYPE_BAR:
471 			/* NYI */
472 			break;
473 		case IEEE80211_FC0_SUBTYPE_BA:
474 			/* NYI */
475 			break;
476 		}
477 		goto out;
478 
479 	default:
480 		DPRINTF(("bad frame type %x\n", type));
481 		/* should not come here */
482 		break;
483 	}
484  err:
485 	ifp->if_ierrors++;
486  out:
487 	if (m != NULL) {
488 #if NBPFILTER > 0
489 		if (ic->ic_rawbpf)
490 			bpf_mtap(ic->ic_rawbpf, m, BPF_DIRECTION_IN);
491 #endif
492 		m_freem(m);
493 	}
494 }
495 
496 void
497 ieee80211_deliver_data(struct ieee80211com *ic, struct mbuf *m,
498     struct ieee80211_node *ni)
499 {
500 	struct ifnet *ifp = &ic->ic_if;
501 	struct ether_header *eh;
502 	struct mbuf *m1;
503 
504 	eh = mtod(m, struct ether_header *);
505 
506 	if ((ic->ic_flags & IEEE80211_F_RSNON) && !ni->ni_port_valid &&
507 	    eh->ether_type != htons(ETHERTYPE_PAE)) {
508 		DPRINTF(("port not valid: %s\n",
509 		    ether_sprintf(eh->ether_dhost)));
510 		ic->ic_stats.is_rx_unauth++;
511 		m_freem(m);
512 		return;
513 	}
514 	ifp->if_ipackets++;
515 
516 	/*
517 	 * Perform as a bridge within the AP.  Notice that we do not
518 	 * bridge EAPOL frames as suggested in C.1.1 of IEEE Std 802.1X.
519 	 */
520 	m1 = NULL;
521 #ifndef IEEE80211_STA_ONLY
522 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
523 	    !(ic->ic_flags & IEEE80211_F_NOBRIDGE) &&
524 	    eh->ether_type != htons(ETHERTYPE_PAE)) {
525 		struct ieee80211_node *ni1;
526 		int error, len;
527 
528 		if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
529 			m1 = m_copym(m, 0, M_COPYALL, M_DONTWAIT);
530 			if (m1 == NULL)
531 				ifp->if_oerrors++;
532 			else
533 				m1->m_flags |= M_MCAST;
534 		} else {
535 			ni1 = ieee80211_find_node(ic, eh->ether_dhost);
536 			if (ni1 != NULL &&
537 			    ni1->ni_state == IEEE80211_STA_ASSOC) {
538 				m1 = m;
539 				m = NULL;
540 			}
541 		}
542 		if (m1 != NULL) {
543 			len = m1->m_pkthdr.len;
544 			IFQ_ENQUEUE(&ifp->if_snd, m1, NULL, error);
545 			if (error)
546 				ifp->if_oerrors++;
547 			else {
548 				if (m != NULL)
549 					ifp->if_omcasts++;
550 				ifp->if_obytes += len;
551 				if_start(ifp);
552 			}
553 		}
554 	}
555 #endif
556 	if (m != NULL) {
557 #if NBPFILTER > 0
558 		/*
559 		 * If we forward frame into transmitter of the AP,
560 		 * we don't need to duplicate for DLT_EN10MB.
561 		 */
562 		if (ifp->if_bpf && m1 == NULL)
563 			bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_IN);
564 #endif
565 		if ((ic->ic_flags & IEEE80211_F_RSNON) &&
566 		    eh->ether_type == htons(ETHERTYPE_PAE))
567 			ieee80211_eapol_key_input(ic, m, ni);
568 		else
569 			ether_input_mbuf(ifp, m);
570 	}
571 }
572 
573 struct mbuf *
574 ieee80211_decap(struct ifnet *ifp, struct mbuf *m, int hdrlen)
575 {
576 	struct ieee80211_qosframe_addr4 wh;	/* largest 802.11 header */
577 	struct ether_header *eh;
578 	struct llc *llc;
579 
580 	if (m->m_len < hdrlen + LLC_SNAPFRAMELEN) {
581 		m = m_pullup(m, hdrlen + LLC_SNAPFRAMELEN);
582 		if (m == NULL)
583 			return NULL;
584 	}
585 	memcpy(&wh, mtod(m, caddr_t), hdrlen);
586 	llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
587 	if (llc->llc_dsap == LLC_SNAP_LSAP &&
588 	    llc->llc_ssap == LLC_SNAP_LSAP &&
589 	    llc->llc_control == LLC_UI &&
590 	    llc->llc_snap.org_code[0] == 0 &&
591 	    llc->llc_snap.org_code[1] == 0 &&
592 	    llc->llc_snap.org_code[2] == 0) {
593 		m_adj(m, hdrlen + LLC_SNAPFRAMELEN - sizeof(*eh));
594 		llc = NULL;
595 	} else {
596 		m_adj(m, hdrlen - sizeof(*eh));
597 	}
598 	eh = mtod(m, struct ether_header *);
599 	switch (wh.i_fc[1] & IEEE80211_FC1_DIR_MASK) {
600 	case IEEE80211_FC1_DIR_NODS:
601 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
602 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
603 		break;
604 	case IEEE80211_FC1_DIR_TODS:
605 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3);
606 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr2);
607 		break;
608 	case IEEE80211_FC1_DIR_FROMDS:
609 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr1);
610 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr3);
611 		break;
612 	case IEEE80211_FC1_DIR_DSTODS:
613 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh.i_addr3);
614 		IEEE80211_ADDR_COPY(eh->ether_shost, wh.i_addr4);
615 		break;
616 	}
617 	if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), u_int32_t)) {
618 		struct mbuf *n, *n0, **np;
619 		caddr_t newdata;
620 		int off, pktlen;
621 
622 		n0 = NULL;
623 		np = &n0;
624 		off = 0;
625 		pktlen = m->m_pkthdr.len;
626 		while (pktlen > off) {
627 			if (n0 == NULL) {
628 				MGETHDR(n, M_DONTWAIT, MT_DATA);
629 				if (n == NULL) {
630 					m_freem(m);
631 					return NULL;
632 				}
633 				M_DUP_PKTHDR(n, m);
634 				n->m_len = MHLEN;
635 			} else {
636 				MGET(n, M_DONTWAIT, MT_DATA);
637 				if (n == NULL) {
638 					m_freem(m);
639 					m_freem(n0);
640 					return NULL;
641 				}
642 				n->m_len = MLEN;
643 			}
644 			if (pktlen - off >= MINCLSIZE) {
645 				MCLGET(n, M_DONTWAIT);
646 				if (n->m_flags & M_EXT)
647 					n->m_len = n->m_ext.ext_size;
648 			}
649 			if (n0 == NULL) {
650 				newdata =
651 				    (caddr_t)ALIGN(n->m_data + sizeof(*eh)) -
652 				    sizeof(*eh);
653 				n->m_len -= newdata - n->m_data;
654 				n->m_data = newdata;
655 			}
656 			if (n->m_len > pktlen - off)
657 				n->m_len = pktlen - off;
658 			m_copydata(m, off, n->m_len, mtod(n, caddr_t));
659 			off += n->m_len;
660 			*np = n;
661 			np = &n->m_next;
662 		}
663 		m_freem(m);
664 		m = n0;
665 	}
666 	if (llc != NULL) {
667 		eh = mtod(m, struct ether_header *);
668 		eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
669 	}
670 	return m;
671 }
672 
673 /*
674  * Parse an EDCA Parameter Set element (see 7.3.2.27).
675  */
676 int
677 ieee80211_parse_edca_params_body(struct ieee80211com *ic, const u_int8_t *frm)
678 {
679 	u_int updtcount;
680 	int aci;
681 
682 	/*
683 	 * Check if EDCA parameters have changed XXX if we miss more than
684 	 * 15 consecutive beacons, we might not detect changes to EDCA
685 	 * parameters due to wraparound of the 4-bit Update Count field.
686 	 */
687 	updtcount = frm[0] & 0xf;
688 	if (updtcount == ic->ic_edca_updtcount)
689 		return 0;	/* no changes to EDCA parameters, ignore */
690 	ic->ic_edca_updtcount = updtcount;
691 
692 	frm += 2;	/* skip QoS Info & Reserved fields */
693 
694 	/* parse AC Parameter Records */
695 	for (aci = 0; aci < EDCA_NUM_AC; aci++) {
696 		struct ieee80211_edca_ac_params *ac = &ic->ic_edca_ac[aci];
697 
698 		ac->ac_acm       = (frm[0] >> 4) & 0x1;
699 		ac->ac_aifsn     = frm[0] & 0xf;
700 		ac->ac_ecwmin    = frm[1] & 0xf;
701 		ac->ac_ecwmax    = frm[1] >> 4;
702 		ac->ac_txoplimit = LE_READ_2(frm + 2);
703 		frm += 4;
704 	}
705 	/* give drivers a chance to update their settings */
706 	if ((ic->ic_flags & IEEE80211_F_QOS) && ic->ic_updateedca != NULL)
707 		(*ic->ic_updateedca)(ic);
708 
709 	return 0;
710 }
711 
712 int
713 ieee80211_parse_edca_params(struct ieee80211com *ic, const u_int8_t *frm)
714 {
715 	if (frm[1] < 18) {
716 		ic->ic_stats.is_rx_elem_toosmall++;
717 		return IEEE80211_REASON_IE_INVALID;
718 	}
719 	return ieee80211_parse_edca_params_body(ic, frm + 2);
720 }
721 
722 int
723 ieee80211_parse_wmm_params(struct ieee80211com *ic, const u_int8_t *frm)
724 {
725 	if (frm[1] < 24) {
726 		ic->ic_stats.is_rx_elem_toosmall++;
727 		return IEEE80211_REASON_IE_INVALID;
728 	}
729 	return ieee80211_parse_edca_params_body(ic, frm + 8);
730 }
731 
732 enum ieee80211_cipher
733 ieee80211_parse_rsn_cipher(const u_int8_t selector[4])
734 {
735 	if (memcmp(selector, MICROSOFT_OUI, 3) == 0) {	/* WPA */
736 		switch (selector[3]) {
737 		case 0:	/* use group data cipher suite */
738 			return IEEE80211_CIPHER_USEGROUP;
739 		case 1:	/* WEP-40 */
740 			return IEEE80211_CIPHER_WEP40;
741 		case 2:	/* TKIP */
742 			return IEEE80211_CIPHER_TKIP;
743 		case 4:	/* CCMP (RSNA default) */
744 			return IEEE80211_CIPHER_CCMP;
745 		case 5:	/* WEP-104 */
746 			return IEEE80211_CIPHER_WEP104;
747 		}
748 	} else if (memcmp(selector, IEEE80211_OUI, 3) == 0) {	/* RSN */
749 		/* from IEEE Std 802.11 - Table 20da */
750 		switch (selector[3]) {
751 		case 0:	/* use group data cipher suite */
752 			return IEEE80211_CIPHER_USEGROUP;
753 		case 1:	/* WEP-40 */
754 			return IEEE80211_CIPHER_WEP40;
755 		case 2:	/* TKIP */
756 			return IEEE80211_CIPHER_TKIP;
757 		case 4:	/* CCMP (RSNA default) */
758 			return IEEE80211_CIPHER_CCMP;
759 		case 5:	/* WEP-104 */
760 			return IEEE80211_CIPHER_WEP104;
761 		case 6:	/* AES-128-CMAC */
762 			return IEEE80211_CIPHER_AES128_CMAC;
763 		}
764 	}
765 	return IEEE80211_CIPHER_NONE;	/* ignore unknown ciphers */
766 }
767 
768 enum ieee80211_akm
769 ieee80211_parse_rsn_akm(const u_int8_t selector[4])
770 {
771 	if (memcmp(selector, MICROSOFT_OUI, 3) == 0) {	/* WPA */
772 		switch (selector[3]) {
773 		case 1:	/* IEEE 802.1X (RSNA default) */
774 			return IEEE80211_AKM_8021X;
775 		case 2:	/* PSK */
776 			return IEEE80211_AKM_PSK;
777 		}
778 	} else if (memcmp(selector, IEEE80211_OUI, 3) == 0) {	/* RSN */
779 		/* from IEEE Std 802.11i-2004 - Table 20dc */
780 		switch (selector[3]) {
781 		case 1:	/* IEEE 802.1X (RSNA default) */
782 			return IEEE80211_AKM_8021X;
783 		case 2:	/* PSK */
784 			return IEEE80211_AKM_PSK;
785 		case 5:	/* IEEE 802.1X with SHA256 KDF */
786 			return IEEE80211_AKM_SHA256_8021X;
787 		case 6:	/* PSK with SHA256 KDF */
788 			return IEEE80211_AKM_SHA256_PSK;
789 		}
790 	}
791 	return IEEE80211_AKM_NONE;	/* ignore unknown AKMs */
792 }
793 
794 /*
795  * Parse an RSN element (see 7.3.2.25).
796  */
797 int
798 ieee80211_parse_rsn_body(struct ieee80211com *ic, const u_int8_t *frm,
799     u_int len, struct ieee80211_rsnparams *rsn)
800 {
801 	const u_int8_t *efrm;
802 	u_int16_t m, n, s;
803 
804 	efrm = frm + len;
805 
806 	/* check Version field */
807 	if (LE_READ_2(frm) != 1)
808 		return IEEE80211_STATUS_RSN_IE_VER_UNSUP;
809 	frm += 2;
810 
811 	/* all fields after the Version field are optional */
812 
813 	/* if Cipher Suite missing, default to CCMP */
814 	rsn->rsn_groupcipher = IEEE80211_CIPHER_CCMP;
815 	rsn->rsn_nciphers = 1;
816 	rsn->rsn_ciphers = IEEE80211_CIPHER_CCMP;
817 	/* if Group Management Cipher Suite missing, defaut to AES-128-CMAC */
818 	rsn->rsn_groupmgmtcipher = IEEE80211_CIPHER_AES128_CMAC;
819 	/* if AKM Suite missing, default to 802.1X */
820 	rsn->rsn_nakms = 1;
821 	rsn->rsn_akms = IEEE80211_AKM_8021X;
822 	/* if RSN capabilities missing, default to 0 */
823 	rsn->rsn_caps = 0;
824 	rsn->rsn_npmkids = 0;
825 
826 	/* read Group Data Cipher Suite field */
827 	if (frm + 4 > efrm)
828 		return 0;
829 	rsn->rsn_groupcipher = ieee80211_parse_rsn_cipher(frm);
830 	if (rsn->rsn_groupcipher == IEEE80211_CIPHER_USEGROUP)
831 		return IEEE80211_STATUS_BAD_GROUP_CIPHER;
832 	frm += 4;
833 
834 	/* read Pairwise Cipher Suite Count field */
835 	if (frm + 2 > efrm)
836 		return 0;
837 	m = rsn->rsn_nciphers = LE_READ_2(frm);
838 	frm += 2;
839 
840 	/* read Pairwise Cipher Suite List */
841 	if (frm + m * 4 > efrm)
842 		return IEEE80211_STATUS_IE_INVALID;
843 	rsn->rsn_ciphers = IEEE80211_CIPHER_NONE;
844 	while (m-- > 0) {
845 		rsn->rsn_ciphers |= ieee80211_parse_rsn_cipher(frm);
846 		frm += 4;
847 	}
848 	if (rsn->rsn_ciphers & IEEE80211_CIPHER_USEGROUP) {
849 		if (rsn->rsn_ciphers != IEEE80211_CIPHER_USEGROUP)
850 			return IEEE80211_STATUS_BAD_PAIRWISE_CIPHER;
851 		if (rsn->rsn_groupcipher == IEEE80211_CIPHER_CCMP)
852 			return IEEE80211_STATUS_BAD_PAIRWISE_CIPHER;
853 	}
854 
855 	/* read AKM Suite List Count field */
856 	if (frm + 2 > efrm)
857 		return 0;
858 	n = rsn->rsn_nakms = LE_READ_2(frm);
859 	frm += 2;
860 
861 	/* read AKM Suite List */
862 	if (frm + n * 4 > efrm)
863 		return IEEE80211_STATUS_IE_INVALID;
864 	rsn->rsn_akms = IEEE80211_AKM_NONE;
865 	while (n-- > 0) {
866 		rsn->rsn_akms |= ieee80211_parse_rsn_akm(frm);
867 		frm += 4;
868 	}
869 
870 	/* read RSN Capabilities field */
871 	if (frm + 2 > efrm)
872 		return 0;
873 	rsn->rsn_caps = LE_READ_2(frm);
874 	frm += 2;
875 
876 	/* read PMKID Count field */
877 	if (frm + 2 > efrm)
878 		return 0;
879 	s = rsn->rsn_npmkids = LE_READ_2(frm);
880 	frm += 2;
881 
882 	/* read PMKID List */
883 	if (frm + s * IEEE80211_PMKID_LEN > efrm)
884 		return IEEE80211_STATUS_IE_INVALID;
885 	if (s != 0) {
886 		rsn->rsn_pmkids = frm;
887 		frm += s * IEEE80211_PMKID_LEN;
888 	}
889 
890 	/* read Group Management Cipher Suite field */
891 	if (frm + 4 > efrm)
892 		return 0;
893 	rsn->rsn_groupmgmtcipher = ieee80211_parse_rsn_cipher(frm);
894 
895 	return IEEE80211_STATUS_SUCCESS;
896 }
897 
898 int
899 ieee80211_parse_rsn(struct ieee80211com *ic, const u_int8_t *frm,
900     struct ieee80211_rsnparams *rsn)
901 {
902 	if (frm[1] < 2) {
903 		ic->ic_stats.is_rx_elem_toosmall++;
904 		return IEEE80211_STATUS_IE_INVALID;
905 	}
906 	return ieee80211_parse_rsn_body(ic, frm + 2, frm[1], rsn);
907 }
908 
909 int
910 ieee80211_parse_wpa(struct ieee80211com *ic, const u_int8_t *frm,
911     struct ieee80211_rsnparams *rsn)
912 {
913 	if (frm[1] < 6) {
914 		ic->ic_stats.is_rx_elem_toosmall++;
915 		return IEEE80211_STATUS_IE_INVALID;
916 	}
917 	return ieee80211_parse_rsn_body(ic, frm + 6, frm[1] - 4, rsn);
918 }
919 
920 /*
921  * Create (or update) a copy of an information element.
922  */
923 int
924 ieee80211_save_ie(const u_int8_t *frm, u_int8_t **ie)
925 {
926 	if (*ie == NULL || (*ie)[1] != frm[1]) {
927 		if (*ie != NULL)
928 			free(*ie, M_DEVBUF);
929 		*ie = malloc(2 + frm[1], M_DEVBUF, M_NOWAIT);
930 		if (*ie == NULL)
931 			return ENOMEM;
932 	}
933 	memcpy(*ie, frm, 2 + frm[1]);
934 	return 0;
935 }
936 
937 /*-
938  * Beacon/Probe response frame format:
939  * [8]    Timestamp
940  * [2]    Beacon interval
941  * [2]    Capability
942  * [tlv]  Service Set Identifier (SSID)
943  * [tlv]  Supported rates
944  * [tlv*] DS Parameter Set (802.11g)
945  * [tlv]  ERP Information (802.11g)
946  * [tlv]  Extended Supported Rates (802.11g)
947  * [tlv]  RSN (802.11i)
948  * [tlv]  EDCA Parameter Set (802.11e)
949  * [tlv]  QoS Capability (Beacon only, 802.11e)
950  */
951 void
952 ieee80211_recv_probe_resp(struct ieee80211com *ic, struct mbuf *m0,
953     struct ieee80211_node *ni, struct ieee80211_rxinfo *rxi, int isprobe)
954 {
955 	const struct ieee80211_frame *wh;
956 	const u_int8_t *frm, *efrm;
957 	const u_int8_t *tstamp, *ssid, *rates, *xrates, *edcaie, *wmmie;
958 	const u_int8_t *rsnie, *wpaie;
959 	u_int16_t capinfo, bintval;
960 	u_int8_t chan, bchan, erp;
961 	int is_new;
962 
963 	/*
964 	 * We process beacon/probe response frames for:
965 	 *    o station mode: to collect state
966 	 *      updates such as 802.11g slot time and for passive
967 	 *      scanning of APs
968 	 *    o adhoc mode: to discover neighbors
969 	 *    o hostap mode: for passive scanning of neighbor APs
970 	 *    o when scanning
971 	 * In other words, in all modes other than monitor (which
972 	 * does not process incoming frames) and adhoc-demo (which
973 	 * does not use management frames at all).
974 	 */
975 #ifdef DIAGNOSTIC
976 	if (ic->ic_opmode != IEEE80211_M_STA &&
977 #ifndef IEEE80211_STA_ONLY
978 	    ic->ic_opmode != IEEE80211_M_IBSS &&
979 	    ic->ic_opmode != IEEE80211_M_HOSTAP &&
980 #endif
981 	    ic->ic_state != IEEE80211_S_SCAN) {
982 		panic("%s: impossible operating mode", __func__);
983 	}
984 #endif
985 	/* make sure all mandatory fixed fields are present */
986 	if (m0->m_len < sizeof(*wh) + 12) {
987 		DPRINTF(("frame too short\n"));
988 		return;
989 	}
990 	wh = mtod(m0, struct ieee80211_frame *);
991 	frm = (const u_int8_t *)&wh[1];
992 	efrm = mtod(m0, u_int8_t *) + m0->m_len;
993 
994 	tstamp  = frm; frm += 8;
995 	bintval = LE_READ_2(frm); frm += 2;
996 	capinfo = LE_READ_2(frm); frm += 2;
997 
998 	ssid = rates = xrates = edcaie = wmmie = rsnie = wpaie = NULL;
999 	bchan = ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan);
1000 	chan = bchan;
1001 	erp = 0;
1002 	while (frm + 2 <= efrm) {
1003 		if (frm + 2 + frm[1] > efrm) {
1004 			ic->ic_stats.is_rx_elem_toosmall++;
1005 			break;
1006 		}
1007 		switch (frm[0]) {
1008 		case IEEE80211_ELEMID_SSID:
1009 			ssid = frm;
1010 			break;
1011 		case IEEE80211_ELEMID_RATES:
1012 			rates = frm;
1013 			break;
1014 		case IEEE80211_ELEMID_DSPARMS:
1015 			if (frm[1] < 1) {
1016 				ic->ic_stats.is_rx_elem_toosmall++;
1017 				break;
1018 			}
1019 			chan = frm[2];
1020 			break;
1021 		case IEEE80211_ELEMID_TIM:
1022 			break;
1023 		case IEEE80211_ELEMID_IBSSPARMS:
1024 			break;
1025 		case IEEE80211_ELEMID_XRATES:
1026 			xrates = frm;
1027 			break;
1028 		case IEEE80211_ELEMID_ERP:
1029 			if (frm[1] < 1) {
1030 				ic->ic_stats.is_rx_elem_toosmall++;
1031 				break;
1032 			}
1033 			erp = frm[2];
1034 			break;
1035 		case IEEE80211_ELEMID_RSN:
1036 			rsnie = frm;
1037 			break;
1038 		case IEEE80211_ELEMID_EDCAPARMS:
1039 			edcaie = frm;
1040 			break;
1041 		case IEEE80211_ELEMID_QOS_CAP:
1042 			break;
1043 		case IEEE80211_ELEMID_VENDOR:
1044 			if (frm[1] < 4) {
1045 				ic->ic_stats.is_rx_elem_toosmall++;
1046 				break;
1047 			}
1048 			if (memcmp(frm + 2, MICROSOFT_OUI, 3) == 0) {
1049 				if (frm[5] == 1)
1050 					wpaie = frm;
1051 				else if (frm[1] >= 5 &&
1052 				    frm[5] == 2 && frm[6] == 1)
1053 					wmmie = frm;
1054 			}
1055 			break;
1056 		default:
1057 			DPRINTF(("element id %u/len %u ignored\n",
1058 			    frm[0], frm[1]));
1059 			ic->ic_stats.is_rx_elem_unknown++;
1060 			break;
1061 		}
1062 		frm += 2 + frm[1];
1063 	}
1064 	/* supported rates element is mandatory */
1065 	if (rates == NULL || rates[1] > IEEE80211_RATE_MAXSIZE) {
1066 		DPRINTF(("invalid supported rates element\n"));
1067 		return;
1068 	}
1069 	/* SSID element is mandatory */
1070 	if (ssid == NULL || ssid[1] > IEEE80211_NWID_LEN) {
1071 		DPRINTF(("invalid SSID element\n"));
1072 		return;
1073 	}
1074 
1075 	if (
1076 #if IEEE80211_CHAN_MAX < 255
1077 	    chan > IEEE80211_CHAN_MAX ||
1078 #endif
1079 	    isclr(ic->ic_chan_active, chan)) {
1080 		DPRINTF(("ignore %s with invalid channel %u\n",
1081 		    isprobe ? "probe response" : "beacon", chan));
1082 		ic->ic_stats.is_rx_badchan++;
1083 		return;
1084 	}
1085 	if ((ic->ic_state != IEEE80211_S_SCAN ||
1086 	     !(ic->ic_caps & IEEE80211_C_SCANALL)) &&
1087 	    chan != bchan) {
1088 		/*
1089 		 * Frame was received on a channel different from the
1090 		 * one indicated in the DS params element id;
1091 		 * silently discard it.
1092 		 *
1093 		 * NB: this can happen due to signal leakage.
1094 		 */
1095 		DPRINTF(("ignore %s on channel %u marked for channel %u\n",
1096 		    isprobe ? "probe response" : "beacon", bchan, chan));
1097 		ic->ic_stats.is_rx_chanmismatch++;
1098 		return;
1099 	}
1100 	/*
1101 	 * Use mac, channel and rssi so we collect only the
1102 	 * best potential AP with the equal bssid while scanning.
1103 	 * Collecting all potential APs may result in bloat of
1104 	 * the node tree. This call will return NULL if the node
1105 	 * for this APs does not exist or if the new node is the
1106 	 * potential better one.
1107 	 */
1108 	if ((ni = ieee80211_find_node_for_beacon(ic, wh->i_addr2,
1109 	    &ic->ic_channels[chan], ssid, rxi->rxi_rssi)) != NULL)
1110 		return;
1111 
1112 #ifdef IEEE80211_DEBUG
1113 	if (ieee80211_debug &&
1114 	    (ni == NULL || ic->ic_state == IEEE80211_S_SCAN)) {
1115 		printf("%s: %s%s on chan %u (bss chan %u) ",
1116 		    __func__, (ni == NULL ? "new " : ""),
1117 		    isprobe ? "probe response" : "beacon",
1118 		    chan, bchan);
1119 		ieee80211_print_essid(ssid + 2, ssid[1]);
1120 		printf(" from %s\n", ether_sprintf((u_int8_t *)wh->i_addr2));
1121 		printf("%s: caps 0x%x bintval %u erp 0x%x\n",
1122 			__func__, capinfo, bintval, erp);
1123 	}
1124 #endif
1125 
1126 	if ((ni = ieee80211_find_node(ic, wh->i_addr2)) == NULL) {
1127 		ni = ieee80211_alloc_node(ic, wh->i_addr2);
1128 		if (ni == NULL)
1129 			return;
1130 		is_new = 1;
1131 	} else
1132 		is_new = 0;
1133 
1134 	/*
1135 	 * When operating in station mode, check for state updates
1136 	 * while we're associated. We consider only 11g stuff right
1137 	 * now.
1138 	 */
1139 	if (ic->ic_opmode == IEEE80211_M_STA &&
1140 	    ic->ic_state == IEEE80211_S_RUN &&
1141 	    ni->ni_state == IEEE80211_STA_BSS) {
1142 		/*
1143 		 * Check if protection mode has changed since last beacon.
1144 		 */
1145 		if (ni->ni_erp != erp) {
1146 			DPRINTF(("[%s] erp change: was 0x%x, now 0x%x\n",
1147 			    ether_sprintf((u_int8_t *)wh->i_addr2),
1148 			    ni->ni_erp, erp));
1149 			if (ic->ic_curmode == IEEE80211_MODE_11G &&
1150 			    (erp & IEEE80211_ERP_USE_PROTECTION))
1151 				ic->ic_flags |= IEEE80211_F_USEPROT;
1152 			else
1153 				ic->ic_flags &= ~IEEE80211_F_USEPROT;
1154 			ic->ic_bss->ni_erp = erp;
1155 		}
1156 		/*
1157 		 * Check if AP short slot time setting has changed
1158 		 * since last beacon and give the driver a chance to
1159 		 * update the hardware.
1160 		 */
1161 		if ((ni->ni_capinfo ^ capinfo) &
1162 		    IEEE80211_CAPINFO_SHORT_SLOTTIME) {
1163 			ieee80211_set_shortslottime(ic,
1164 			    ic->ic_curmode == IEEE80211_MODE_11A ||
1165 			    (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1166 		}
1167 	}
1168 	/*
1169 	 * We do not try to update EDCA parameters if QoS was not negotiated
1170 	 * with the AP at association time.
1171 	 */
1172 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
1173 		/* always prefer EDCA IE over Wi-Fi Alliance WMM IE */
1174 		if (edcaie != NULL)
1175 			ieee80211_parse_edca_params(ic, edcaie);
1176 		else if (wmmie != NULL)
1177 			ieee80211_parse_wmm_params(ic, wmmie);
1178 	}
1179 
1180 	if (ic->ic_state == IEEE80211_S_SCAN &&
1181 #ifndef IEEE80211_STA_ONLY
1182 	    ic->ic_opmode != IEEE80211_M_HOSTAP &&
1183 #endif
1184 	    (ic->ic_flags & IEEE80211_F_RSNON)) {
1185 		struct ieee80211_rsnparams rsn;
1186 		const u_int8_t *saveie = NULL;
1187 		/*
1188 		 * If the AP advertises both RSN and WPA IEs (WPA1+WPA2),
1189 		 * we only store the parameters of the highest protocol
1190 		 * version we support.
1191 		 */
1192 		if (rsnie != NULL &&
1193 		    (ic->ic_rsnprotos & IEEE80211_PROTO_RSN)) {
1194 			if (ieee80211_parse_rsn(ic, rsnie, &rsn) == 0) {
1195 				ni->ni_rsnprotos = IEEE80211_PROTO_RSN;
1196 				saveie = rsnie;
1197 			}
1198 		} else if (wpaie != NULL &&
1199 		    (ic->ic_rsnprotos & IEEE80211_PROTO_WPA)) {
1200 			if (ieee80211_parse_wpa(ic, wpaie, &rsn) == 0) {
1201 				ni->ni_rsnprotos = IEEE80211_PROTO_WPA;
1202 				saveie = wpaie;
1203 			}
1204 		}
1205 		if (saveie != NULL &&
1206 		    ieee80211_save_ie(saveie, &ni->ni_rsnie) == 0) {
1207 			ni->ni_rsnakms = rsn.rsn_akms;
1208 			ni->ni_rsnciphers = rsn.rsn_ciphers;
1209 			ni->ni_rsngroupcipher = rsn.rsn_groupcipher;
1210 			ni->ni_rsngroupmgmtcipher = rsn.rsn_groupmgmtcipher;
1211 			ni->ni_rsncaps = rsn.rsn_caps;
1212 		} else
1213 			ni->ni_rsnprotos = IEEE80211_PROTO_NONE;
1214 	} else if (ic->ic_state == IEEE80211_S_SCAN)
1215 		ni->ni_rsnprotos = IEEE80211_PROTO_NONE;
1216 
1217 	if (ssid[1] != 0 && ni->ni_esslen == 0) {
1218 		ni->ni_esslen = ssid[1];
1219 		memset(ni->ni_essid, 0, sizeof(ni->ni_essid));
1220 		/* we know that ssid[1] <= IEEE80211_NWID_LEN */
1221 		memcpy(ni->ni_essid, &ssid[2], ssid[1]);
1222 	}
1223 	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
1224 	ni->ni_rssi = rxi->rxi_rssi;
1225 	ni->ni_rstamp = rxi->rxi_tstamp;
1226 	memcpy(ni->ni_tstamp, tstamp, sizeof(ni->ni_tstamp));
1227 	ni->ni_intval = bintval;
1228 	ni->ni_capinfo = capinfo;
1229 	/* XXX validate channel # */
1230 	ni->ni_chan = &ic->ic_channels[chan];
1231 	ni->ni_erp = erp;
1232 	/* NB: must be after ni_chan is setup */
1233 	ieee80211_setup_rates(ic, ni, rates, xrates, IEEE80211_F_DOSORT);
1234 
1235 	/*
1236 	 * When scanning we record results (nodes) with a zero
1237 	 * refcnt.  Otherwise we want to hold the reference for
1238 	 * ibss neighbors so the nodes don't get released prematurely.
1239 	 * Anything else can be discarded (XXX and should be handled
1240 	 * above so we don't do so much work).
1241 	 */
1242 	if (
1243 #ifndef IEEE80211_STA_ONLY
1244 	    ic->ic_opmode == IEEE80211_M_IBSS ||
1245 #endif
1246 	    (is_new && isprobe)) {
1247 		/*
1248 		 * Fake an association so the driver can setup it's
1249 		 * private state.  The rate set has been setup above;
1250 		 * there is no handshake as in ap/station operation.
1251 		 */
1252 		if (ic->ic_newassoc)
1253 			(*ic->ic_newassoc)(ic, ni, 1);
1254 	}
1255 }
1256 
1257 #ifndef IEEE80211_STA_ONLY
1258 /*-
1259  * Probe request frame format:
1260  * [tlv] SSID
1261  * [tlv] Supported rates
1262  * [tlv] Extended Supported Rates (802.11g)
1263  */
1264 void
1265 ieee80211_recv_probe_req(struct ieee80211com *ic, struct mbuf *m0,
1266     struct ieee80211_node *ni, struct ieee80211_rxinfo *rxi)
1267 {
1268 	const struct ieee80211_frame *wh;
1269 	const u_int8_t *frm, *efrm;
1270 	const u_int8_t *ssid, *rates, *xrates;
1271 	u_int8_t rate;
1272 
1273 	if (ic->ic_opmode == IEEE80211_M_STA ||
1274 	    ic->ic_state != IEEE80211_S_RUN)
1275 		return;
1276 
1277 	wh = mtod(m0, struct ieee80211_frame *);
1278 	frm = (const u_int8_t *)&wh[1];
1279 	efrm = mtod(m0, u_int8_t *) + m0->m_len;
1280 
1281 	ssid = rates = xrates = NULL;
1282 	while (frm + 2 <= efrm) {
1283 		if (frm + 2 + frm[1] > efrm) {
1284 			ic->ic_stats.is_rx_elem_toosmall++;
1285 			break;
1286 		}
1287 		switch (frm[0]) {
1288 		case IEEE80211_ELEMID_SSID:
1289 			ssid = frm;
1290 			break;
1291 		case IEEE80211_ELEMID_RATES:
1292 			rates = frm;
1293 			break;
1294 		case IEEE80211_ELEMID_XRATES:
1295 			xrates = frm;
1296 			break;
1297 		}
1298 		frm += 2 + frm[1];
1299 	}
1300 	/* supported rates element is mandatory */
1301 	if (rates == NULL || rates[1] > IEEE80211_RATE_MAXSIZE) {
1302 		DPRINTF(("invalid supported rates element\n"));
1303 		return;
1304 	}
1305 	/* SSID element is mandatory */
1306 	if (ssid == NULL || ssid[1] > IEEE80211_NWID_LEN) {
1307 		DPRINTF(("invalid SSID element\n"));
1308 		return;
1309 	}
1310 	/* check that the specified SSID (if not wildcard) matches ours */
1311 	if (ssid[1] != 0 && (ssid[1] != ic->ic_bss->ni_esslen ||
1312 	    memcmp(&ssid[2], ic->ic_bss->ni_essid, ic->ic_bss->ni_esslen))) {
1313 		DPRINTF(("SSID mismatch\n"));
1314 		ic->ic_stats.is_rx_ssidmismatch++;
1315 		return;
1316 	}
1317 	/* refuse wildcard SSID if we're hiding our SSID in beacons */
1318 	if (ssid[1] == 0 && (ic->ic_flags & IEEE80211_F_HIDENWID)) {
1319 		DPRINTF(("wildcard SSID rejected"));
1320 		ic->ic_stats.is_rx_ssidmismatch++;
1321 		return;
1322 	}
1323 
1324 	if (ni == ic->ic_bss) {
1325 		ni = ieee80211_dup_bss(ic, wh->i_addr2);
1326 		if (ni == NULL)
1327 			return;
1328 		DPRINTF(("new probe req from %s\n",
1329 		    ether_sprintf((u_int8_t *)wh->i_addr2)));
1330 	}
1331 	ni->ni_rssi = rxi->rxi_rssi;
1332 	ni->ni_rstamp = rxi->rxi_tstamp;
1333 	rate = ieee80211_setup_rates(ic, ni, rates, xrates,
1334 	    IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE | IEEE80211_F_DONEGO |
1335 	    IEEE80211_F_DODEL);
1336 	if (rate & IEEE80211_RATE_BASIC) {
1337 		DPRINTF(("rate mismatch for %s\n",
1338 		    ether_sprintf((u_int8_t *)wh->i_addr2)));
1339 		return;
1340 	}
1341 	IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_PROBE_RESP, 0);
1342 }
1343 #endif	/* IEEE80211_STA_ONLY */
1344 
1345 /*-
1346  * Authentication frame format:
1347  * [2] Authentication algorithm number
1348  * [2] Authentication transaction sequence number
1349  * [2] Status code
1350  */
1351 void
1352 ieee80211_recv_auth(struct ieee80211com *ic, struct mbuf *m0,
1353     struct ieee80211_node *ni, struct ieee80211_rxinfo *rxi)
1354 {
1355 	const struct ieee80211_frame *wh;
1356 	const u_int8_t *frm;
1357 	u_int16_t algo, seq, status;
1358 
1359 	/* make sure all mandatory fixed fields are present */
1360 	if (m0->m_len < sizeof(*wh) + 6) {
1361 		DPRINTF(("frame too short\n"));
1362 		return;
1363 	}
1364 	wh = mtod(m0, struct ieee80211_frame *);
1365 	frm = (const u_int8_t *)&wh[1];
1366 
1367 	algo   = LE_READ_2(frm); frm += 2;
1368 	seq    = LE_READ_2(frm); frm += 2;
1369 	status = LE_READ_2(frm); frm += 2;
1370 	DPRINTF(("auth %d seq %d from %s\n", algo, seq,
1371 	    ether_sprintf((u_int8_t *)wh->i_addr2)));
1372 
1373 	/* only "open" auth mode is supported */
1374 	if (algo != IEEE80211_AUTH_ALG_OPEN) {
1375 		DPRINTF(("unsupported auth algorithm %d from %s\n",
1376 		    algo, ether_sprintf((u_int8_t *)wh->i_addr2)));
1377 		ic->ic_stats.is_rx_auth_unsupported++;
1378 #ifndef IEEE80211_STA_ONLY
1379 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
1380 			/* XXX hack to workaround calling convention */
1381 			IEEE80211_SEND_MGMT(ic, ni,
1382 				IEEE80211_FC0_SUBTYPE_AUTH,
1383 				(seq+1) | (IEEE80211_STATUS_ALG<<16));
1384 		}
1385 #endif
1386 		return;
1387 	}
1388 	ieee80211_auth_open(ic, wh, ni, rxi, seq, status);
1389 }
1390 
1391 #ifndef IEEE80211_STA_ONLY
1392 /*-
1393  * (Re)Association request frame format:
1394  * [2]   Capability information
1395  * [2]   Listen interval
1396  * [6*]  Current AP address (Reassociation only)
1397  * [tlv] SSID
1398  * [tlv] Supported rates
1399  * [tlv] Extended Supported Rates (802.11g)
1400  * [tlv] RSN (802.11i)
1401  * [tlv] QoS Capability (802.11e)
1402  */
1403 void
1404 ieee80211_recv_assoc_req(struct ieee80211com *ic, struct mbuf *m0,
1405     struct ieee80211_node *ni, struct ieee80211_rxinfo *rxi, int reassoc)
1406 {
1407 	const struct ieee80211_frame *wh;
1408 	const u_int8_t *frm, *efrm;
1409 	const u_int8_t *ssid, *rates, *xrates, *rsnie, *wpaie;
1410 	u_int16_t capinfo, bintval;
1411 	int resp, status = 0;
1412 	struct ieee80211_rsnparams rsn;
1413 	u_int8_t rate;
1414 
1415 	if (ic->ic_opmode != IEEE80211_M_HOSTAP ||
1416 	    ic->ic_state != IEEE80211_S_RUN)
1417 		return;
1418 
1419 	/* make sure all mandatory fixed fields are present */
1420 	if (m0->m_len < sizeof(*wh) + (reassoc ? 10 : 4)) {
1421 		DPRINTF(("frame too short\n"));
1422 		return;
1423 	}
1424 	wh = mtod(m0, struct ieee80211_frame *);
1425 	frm = (const u_int8_t *)&wh[1];
1426 	efrm = mtod(m0, u_int8_t *) + m0->m_len;
1427 
1428 	if (!IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_bss->ni_bssid)) {
1429 		DPRINTF(("ignore other bss from %s\n",
1430 		    ether_sprintf((u_int8_t *)wh->i_addr2)));
1431 		ic->ic_stats.is_rx_assoc_bss++;
1432 		return;
1433 	}
1434 	capinfo = LE_READ_2(frm); frm += 2;
1435 	bintval = LE_READ_2(frm); frm += 2;
1436 	if (reassoc)
1437 		frm += IEEE80211_ADDR_LEN;	/* skip current AP address */
1438 
1439 	ssid = rates = xrates = rsnie = wpaie = NULL;
1440 	while (frm + 2 <= efrm) {
1441 		if (frm + 2 + frm[1] > efrm) {
1442 			ic->ic_stats.is_rx_elem_toosmall++;
1443 			break;
1444 		}
1445 		switch (frm[0]) {
1446 		case IEEE80211_ELEMID_SSID:
1447 			ssid = frm;
1448 			break;
1449 		case IEEE80211_ELEMID_RATES:
1450 			rates = frm;
1451 			break;
1452 		case IEEE80211_ELEMID_XRATES:
1453 			xrates = frm;
1454 			break;
1455 		case IEEE80211_ELEMID_RSN:
1456 			rsnie = frm;
1457 			break;
1458 		case IEEE80211_ELEMID_QOS_CAP:
1459 			break;
1460 		case IEEE80211_ELEMID_VENDOR:
1461 			if (frm[1] < 4) {
1462 				ic->ic_stats.is_rx_elem_toosmall++;
1463 				break;
1464 			}
1465 			if (memcmp(frm + 2, MICROSOFT_OUI, 3) == 0) {
1466 				if (frm[5] == 1)
1467 					wpaie = frm;
1468 			}
1469 			break;
1470 		}
1471 		frm += 2 + frm[1];
1472 	}
1473 	/* supported rates element is mandatory */
1474 	if (rates == NULL || rates[1] > IEEE80211_RATE_MAXSIZE) {
1475 		DPRINTF(("invalid supported rates element\n"));
1476 		return;
1477 	}
1478 	/* SSID element is mandatory */
1479 	if (ssid == NULL || ssid[1] > IEEE80211_NWID_LEN) {
1480 		DPRINTF(("invalid SSID element\n"));
1481 		return;
1482 	}
1483 	/* check that the specified SSID matches ours */
1484 	if (ssid[1] != ic->ic_bss->ni_esslen ||
1485 	    memcmp(&ssid[2], ic->ic_bss->ni_essid, ic->ic_bss->ni_esslen)) {
1486 		DPRINTF(("SSID mismatch\n"));
1487 		ic->ic_stats.is_rx_ssidmismatch++;
1488 		return;
1489 	}
1490 
1491 	if (ni->ni_state != IEEE80211_STA_AUTH &&
1492 	    ni->ni_state != IEEE80211_STA_ASSOC) {
1493 		DPRINTF(("deny %sassoc from %s, not authenticated\n",
1494 		    reassoc ? "re" : "",
1495 		    ether_sprintf((u_int8_t *)wh->i_addr2)));
1496 		ni = ieee80211_dup_bss(ic, wh->i_addr2);
1497 		if (ni != NULL) {
1498 			IEEE80211_SEND_MGMT(ic, ni,
1499 			    IEEE80211_FC0_SUBTYPE_DEAUTH,
1500 			    IEEE80211_REASON_ASSOC_NOT_AUTHED);
1501 		}
1502 		ic->ic_stats.is_rx_assoc_notauth++;
1503 		return;
1504 	}
1505 
1506 	if (!(capinfo & IEEE80211_CAPINFO_ESS)) {
1507 		ic->ic_stats.is_rx_assoc_capmismatch++;
1508 		status = IEEE80211_STATUS_CAPINFO;
1509 		goto end;
1510 	}
1511 	rate = ieee80211_setup_rates(ic, ni, rates, xrates,
1512 	    IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE | IEEE80211_F_DONEGO |
1513 	    IEEE80211_F_DODEL);
1514 	if (rate & IEEE80211_RATE_BASIC) {
1515 		ic->ic_stats.is_rx_assoc_norate++;
1516 		status = IEEE80211_STATUS_BASIC_RATE;
1517 		goto end;
1518 	}
1519 
1520 	if (ic->ic_flags & IEEE80211_F_RSNON) {
1521 		const u_int8_t *saveie;
1522 		/*
1523 		 * A station should never include both a WPA and an RSN IE
1524 		 * in its (Re)Association Requests, but if it does, we only
1525 		 * consider the IE of the highest version of the protocol
1526 		 * that is allowed (ie RSN over WPA).
1527 		 */
1528 		if (rsnie != NULL &&
1529 		    (ic->ic_rsnprotos & IEEE80211_PROTO_RSN)) {
1530 			status = ieee80211_parse_rsn(ic, rsnie, &rsn);
1531 			if (status != 0)
1532 				goto end;
1533 			ni->ni_rsnprotos = IEEE80211_PROTO_RSN;
1534 			saveie = rsnie;
1535 		} else if (wpaie != NULL &&
1536 		    (ic->ic_rsnprotos & IEEE80211_PROTO_WPA)) {
1537 			status = ieee80211_parse_wpa(ic, wpaie, &rsn);
1538 			if (status != 0)
1539 				goto end;
1540 			ni->ni_rsnprotos = IEEE80211_PROTO_WPA;
1541 			saveie = wpaie;
1542 		} else {
1543 			/*
1544 			 * In an RSN, an AP shall not associate with STAs
1545 			 * that fail to include the RSN IE in the
1546 			 * (Re)Association Request.
1547 			 */
1548 			status = IEEE80211_STATUS_IE_INVALID;
1549 			goto end;
1550 		}
1551 		/*
1552 		 * The initiating STA's RSN IE shall include one authentication
1553 		 * and pairwise cipher suite among those advertised by the
1554 		 * targeted AP.  It shall also specify the group cipher suite
1555 		 * specified by the targeted AP.
1556 		 */
1557 		if (rsn.rsn_nakms != 1 ||
1558 		    !(rsn.rsn_akms & ic->ic_bss->ni_rsnakms)) {
1559 			status = IEEE80211_STATUS_BAD_AKMP;
1560 			goto end;
1561 		}
1562 		if (rsn.rsn_nciphers != 1 ||
1563 		    !(rsn.rsn_ciphers & ic->ic_bss->ni_rsnciphers)) {
1564 			status = IEEE80211_STATUS_BAD_PAIRWISE_CIPHER;
1565 			goto end;
1566 		}
1567 		if (rsn.rsn_groupcipher != ic->ic_bss->ni_rsngroupcipher) {
1568 			status = IEEE80211_STATUS_BAD_GROUP_CIPHER;
1569 			goto end;
1570 		}
1571 
1572 		if (!(rsn.rsn_caps & IEEE80211_RSNCAP_MFPC) &&
1573 		    (ic->ic_bss->ni_rsncaps & IEEE80211_RSNCAP_MFPR)) {
1574 			status = IEEE80211_REASON_MFP_POLICY;	/* XXX */
1575 			goto end;
1576 		}
1577 		if ((ic->ic_bss->ni_rsncaps & IEEE80211_RSNCAP_MFPC) &&
1578 		    (rsn.rsn_caps & (IEEE80211_RSNCAP_MFPC |
1579 		     IEEE80211_RSNCAP_MFPR)) == IEEE80211_RSNCAP_MFPR) {
1580 			/* STA advertises an invalid setting */
1581 			status = IEEE80211_REASON_MFP_POLICY;	/* XXX */
1582 			goto end;
1583 		}
1584 		if ((rsn.rsn_caps & IEEE80211_RSNCAP_MFPC) &&
1585 		    rsn.rsn_groupmgmtcipher !=
1586 		    ic->ic_bss->ni_rsngroupmgmtcipher) {
1587 			/* XXX satus not reason?! */
1588 			status = IEEE80211_REASON_BAD_GROUP_MGMT_CIPHER;
1589 			goto end;
1590 		}
1591 
1592 		/*
1593 		 * Disallow new associations using TKIP if countermeasures
1594 		 * are active.
1595 		 */
1596 		if ((ic->ic_flags & IEEE80211_F_COUNTERM) &&
1597 		    (rsn.rsn_ciphers == IEEE80211_CIPHER_TKIP ||
1598 		     rsn.rsn_groupcipher == IEEE80211_CIPHER_TKIP)) {
1599 			status = IEEE80211_STATUS_CIPHER_REJ_POLICY;
1600 			goto end;
1601 		}
1602 
1603 		/* everything looks fine, save IE and parameters */
1604 		if (ieee80211_save_ie(saveie, &ni->ni_rsnie) != 0) {
1605 			status = IEEE80211_STATUS_TOOMANY;
1606 			goto end;
1607 		}
1608 		ni->ni_rsnakms = rsn.rsn_akms;
1609 		ni->ni_rsnciphers = rsn.rsn_ciphers;
1610 		ni->ni_rsngroupcipher = ic->ic_bss->ni_rsngroupcipher;
1611 		ni->ni_rsngroupmgmtcipher = ic->ic_bss->ni_rsngroupmgmtcipher;
1612 		ni->ni_rsncaps = rsn.rsn_caps;
1613 
1614 		if (ieee80211_is_8021x_akm(ni->ni_rsnakms)) {
1615 			struct ieee80211_pmk *pmk = NULL;
1616 			const u_int8_t *pmkid = rsn.rsn_pmkids;
1617 			/*
1618 			 * Check if we have a cached PMK entry matching one
1619 			 * of the PMKIDs specified in the RSN IE.
1620 			 */
1621 			while (rsn.rsn_npmkids-- > 0) {
1622 				pmk = ieee80211_pmksa_find(ic, ni, pmkid);
1623 				if (pmk != NULL)
1624 					break;
1625 				pmkid += IEEE80211_PMKID_LEN;
1626 			}
1627 			if (pmk != NULL) {
1628 				memcpy(ni->ni_pmk, pmk->pmk_key,
1629 				    IEEE80211_PMK_LEN);
1630 				memcpy(ni->ni_pmkid, pmk->pmk_pmkid,
1631 				    IEEE80211_PMKID_LEN);
1632 				ni->ni_flags |= IEEE80211_NODE_PMK;
1633 			}
1634 		}
1635 	} else
1636 		ni->ni_rsnprotos = IEEE80211_PROTO_NONE;
1637 
1638 	ni->ni_rssi = rxi->rxi_rssi;
1639 	ni->ni_rstamp = rxi->rxi_tstamp;
1640 	ni->ni_intval = bintval;
1641 	ni->ni_capinfo = capinfo;
1642 	ni->ni_chan = ic->ic_bss->ni_chan;
1643  end:
1644 	resp = reassoc ? IEEE80211_FC0_SUBTYPE_REASSOC_RESP :
1645 	    IEEE80211_FC0_SUBTYPE_ASSOC_RESP;
1646 	if (status != 0) {
1647 		IEEE80211_SEND_MGMT(ic, ni, resp, status);
1648 		ieee80211_node_leave(ic, ni);
1649 	} else
1650 		ieee80211_node_join(ic, ni, resp);
1651 }
1652 #endif	/* IEEE80211_STA_ONLY */
1653 
1654 /*-
1655  * (Re)Association response frame format:
1656  * [2]   Capability information
1657  * [2]   Status code
1658  * [2]   Association ID (AID)
1659  * [tlv] Supported rates
1660  * [tlv] Extended Supported Rates (802.11g)
1661  * [tlv] EDCA Parameter Set (802.11e)
1662  */
1663 void
1664 ieee80211_recv_assoc_resp(struct ieee80211com *ic, struct mbuf *m0,
1665     struct ieee80211_node *ni, int reassoc)
1666 {
1667 	struct ifnet *ifp = &ic->ic_if;
1668 	const struct ieee80211_frame *wh;
1669 	const u_int8_t *frm, *efrm;
1670 	const u_int8_t *rates, *xrates, *edcaie, *wmmie;
1671 	u_int16_t capinfo, status, associd;
1672 	u_int8_t rate;
1673 
1674 	if (ic->ic_opmode != IEEE80211_M_STA ||
1675 	    ic->ic_state != IEEE80211_S_ASSOC) {
1676 		ic->ic_stats.is_rx_mgtdiscard++;
1677 		return;
1678 	}
1679 
1680 	/* make sure all mandatory fixed fields are present */
1681 	if (m0->m_len < sizeof(*wh) + 6) {
1682 		DPRINTF(("frame too short\n"));
1683 		return;
1684 	}
1685 	wh = mtod(m0, struct ieee80211_frame *);
1686 	frm = (const u_int8_t *)&wh[1];
1687 	efrm = mtod(m0, u_int8_t *) + m0->m_len;
1688 
1689 	capinfo = LE_READ_2(frm); frm += 2;
1690 	status =  LE_READ_2(frm); frm += 2;
1691 	if (status != IEEE80211_STATUS_SUCCESS) {
1692 		if (ifp->if_flags & IFF_DEBUG)
1693 			printf("%s: %sassociation failed (reason %d)"
1694 			    " for %s\n", ifp->if_xname,
1695 			    reassoc ?  "re" : "",
1696 			    status, ether_sprintf((u_int8_t *)wh->i_addr3));
1697 		if (ni != ic->ic_bss)
1698 			ni->ni_fails++;
1699 		ic->ic_stats.is_rx_auth_fail++;
1700 		return;
1701 	}
1702 	associd = LE_READ_2(frm); frm += 2;
1703 
1704 	rates = xrates = edcaie = wmmie = NULL;
1705 	while (frm + 2 <= efrm) {
1706 		if (frm + 2 + frm[1] > efrm) {
1707 			ic->ic_stats.is_rx_elem_toosmall++;
1708 			break;
1709 		}
1710 		switch (frm[0]) {
1711 		case IEEE80211_ELEMID_RATES:
1712 			rates = frm;
1713 			break;
1714 		case IEEE80211_ELEMID_XRATES:
1715 			xrates = frm;
1716 			break;
1717 		case IEEE80211_ELEMID_EDCAPARMS:
1718 			edcaie = frm;
1719 			break;
1720 		case IEEE80211_ELEMID_VENDOR:
1721 			if (frm[1] < 4) {
1722 				ic->ic_stats.is_rx_elem_toosmall++;
1723 				break;
1724 			}
1725 			if (memcmp(frm + 2, MICROSOFT_OUI, 3) == 0) {
1726 				if (frm[1] >= 5 && frm[5] == 2 && frm[6] == 1)
1727 					wmmie = frm;
1728 			}
1729 			break;
1730 		}
1731 		frm += 2 + frm[1];
1732 	}
1733 	/* supported rates element is mandatory */
1734 	if (rates == NULL || rates[1] > IEEE80211_RATE_MAXSIZE) {
1735 		DPRINTF(("invalid supported rates element\n"));
1736 		return;
1737 	}
1738 	rate = ieee80211_setup_rates(ic, ni, rates, xrates,
1739 	    IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE | IEEE80211_F_DONEGO |
1740 	    IEEE80211_F_DODEL);
1741 	if (rate & IEEE80211_RATE_BASIC) {
1742 		DPRINTF(("rate mismatch for %s\n",
1743 		    ether_sprintf((u_int8_t *)wh->i_addr2)));
1744 		ic->ic_stats.is_rx_assoc_norate++;
1745 		return;
1746 	}
1747 	ni->ni_capinfo = capinfo;
1748 	ni->ni_associd = associd;
1749 	if (edcaie != NULL || wmmie != NULL) {
1750 		/* force update of EDCA parameters */
1751 		ic->ic_edca_updtcount = -1;
1752 
1753 		if ((edcaie != NULL &&
1754 		     ieee80211_parse_edca_params(ic, edcaie) == 0) ||
1755 		    (wmmie != NULL &&
1756 		     ieee80211_parse_wmm_params(ic, wmmie) == 0))
1757 			ni->ni_flags |= IEEE80211_NODE_QOS;
1758 		else	/* for Reassociation */
1759 			ni->ni_flags &= ~IEEE80211_NODE_QOS;
1760 	}
1761 	/*
1762 	 * Configure state now that we are associated.
1763 	 */
1764 	if (ic->ic_curmode == IEEE80211_MODE_11A ||
1765 	    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE))
1766 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1767 	else
1768 		ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1769 
1770 	ieee80211_set_shortslottime(ic,
1771 	    ic->ic_curmode == IEEE80211_MODE_11A ||
1772 	    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1773 	/*
1774 	 * Honor ERP protection.
1775 	 */
1776 	if (ic->ic_curmode == IEEE80211_MODE_11G &&
1777 	    (ni->ni_erp & IEEE80211_ERP_USE_PROTECTION))
1778 		ic->ic_flags |= IEEE80211_F_USEPROT;
1779 	else
1780 		ic->ic_flags &= ~IEEE80211_F_USEPROT;
1781 	/*
1782 	 * If not an RSNA, mark the port as valid, otherwise wait for
1783 	 * 802.1X authentication and 4-way handshake to complete..
1784 	 */
1785 	if (ic->ic_flags & IEEE80211_F_RSNON) {
1786 		/* XXX ic->ic_mgt_timer = 5; */
1787 	} else if (ic->ic_flags & IEEE80211_F_WEPON)
1788 		ni->ni_flags |= IEEE80211_NODE_TXRXPROT;
1789 
1790 	ieee80211_new_state(ic, IEEE80211_S_RUN,
1791 	    IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
1792 }
1793 
1794 /*-
1795  * Deauthentication frame format:
1796  * [2] Reason code
1797  */
1798 void
1799 ieee80211_recv_deauth(struct ieee80211com *ic, struct mbuf *m0,
1800     struct ieee80211_node *ni)
1801 {
1802 	const struct ieee80211_frame *wh;
1803 	const u_int8_t *frm;
1804 	u_int16_t reason;
1805 
1806 	/* make sure all mandatory fixed fields are present */
1807 	if (m0->m_len < sizeof(*wh) + 2) {
1808 		DPRINTF(("frame too short\n"));
1809 		return;
1810 	}
1811 	wh = mtod(m0, struct ieee80211_frame *);
1812 	frm = (const u_int8_t *)&wh[1];
1813 
1814 	reason = LE_READ_2(frm);
1815 
1816 	ic->ic_stats.is_rx_deauth++;
1817 	switch (ic->ic_opmode) {
1818 	case IEEE80211_M_STA:
1819 		ieee80211_new_state(ic, IEEE80211_S_AUTH,
1820 		    IEEE80211_FC0_SUBTYPE_DEAUTH);
1821 		break;
1822 #ifndef IEEE80211_STA_ONLY
1823 	case IEEE80211_M_HOSTAP:
1824 		if (ni != ic->ic_bss) {
1825 			if (ic->ic_if.if_flags & IFF_DEBUG)
1826 				printf("%s: station %s deauthenticated "
1827 				    "by peer (reason %d)\n",
1828 				    ic->ic_if.if_xname,
1829 				    ether_sprintf(ni->ni_macaddr),
1830 				    reason);
1831 			ieee80211_node_leave(ic, ni);
1832 		}
1833 		break;
1834 #endif
1835 	default:
1836 		break;
1837 	}
1838 }
1839 
1840 /*-
1841  * Disassociation frame format:
1842  * [2] Reason code
1843  */
1844 void
1845 ieee80211_recv_disassoc(struct ieee80211com *ic, struct mbuf *m0,
1846     struct ieee80211_node *ni)
1847 {
1848 	const struct ieee80211_frame *wh;
1849 	const u_int8_t *frm;
1850 	u_int16_t reason;
1851 
1852 	/* make sure all mandatory fixed fields are present */
1853 	if (m0->m_len < sizeof(*wh) + 2) {
1854 		DPRINTF(("frame too short\n"));
1855 		return;
1856 	}
1857 	wh = mtod(m0, struct ieee80211_frame *);
1858 	frm = (const u_int8_t *)&wh[1];
1859 
1860 	reason = LE_READ_2(frm);
1861 
1862 	ic->ic_stats.is_rx_disassoc++;
1863 	switch (ic->ic_opmode) {
1864 	case IEEE80211_M_STA:
1865 		ieee80211_new_state(ic, IEEE80211_S_ASSOC,
1866 		    IEEE80211_FC0_SUBTYPE_DISASSOC);
1867 		break;
1868 #ifndef IEEE80211_STA_ONLY
1869 	case IEEE80211_M_HOSTAP:
1870 		if (ni != ic->ic_bss) {
1871 			if (ic->ic_if.if_flags & IFF_DEBUG)
1872 				printf("%s: station %s disassociated "
1873 				    "by peer (reason %d)\n",
1874 				    ic->ic_if.if_xname,
1875 				    ether_sprintf(ni->ni_macaddr),
1876 				    reason);
1877 			ieee80211_node_leave(ic, ni);
1878 		}
1879 		break;
1880 #endif
1881 	default:
1882 		break;
1883 	}
1884 }
1885 
1886 /*-
1887  * Action frame format:
1888  * [1] Category
1889  * [1] Action
1890  */
1891 void
1892 ieee80211_recv_action(struct ieee80211com *ic, struct mbuf *m0,
1893     struct ieee80211_node *ni)
1894 {
1895 	const struct ieee80211_frame *wh;
1896 	const u_int8_t *frm;
1897 
1898 	if (m0->m_len < sizeof(*wh) + 2) {
1899 		DPRINTF(("frame too short\n"));
1900 		return;
1901 	}
1902 	wh = mtod(m0, struct ieee80211_frame *);
1903 	frm = (const u_int8_t *)&wh[1];
1904 
1905 	switch (frm[0]) {
1906 	case IEEE80211_CATEG_BA:
1907 		switch (frm[1]) {
1908 		case IEEE80211_ACTION_ADDBA_REQ:
1909 			/* NYI */
1910 			break;
1911 		case IEEE80211_ACTION_ADDBA_RESP:
1912 			/* NYI */
1913 			break;
1914 		case IEEE80211_ACTION_DELBA:
1915 			/* NYI */
1916 			break;
1917 		}
1918 		break;
1919 	case IEEE80211_CATEG_HT:
1920 		switch (frm[1]) {
1921 		case IEEE80211_ACTION_NOTIFYCW:
1922 			/* NYI */
1923 			break;
1924 		}
1925 		break;
1926 	case IEEE80211_CATEG_SALT:
1927 		switch (frm[1]) {
1928 		case IEEE80211_ACTION_SALT_REQ:
1929 			/* NYI */
1930 			break;
1931 		case IEEE80211_ACTION_SALT_RESP:
1932 			/* NYI */
1933 			break;
1934 		}
1935 		break;
1936 	default:
1937 		DPRINTF(("action frame category %d not handled\n", frm[0]));
1938 		break;
1939 	}
1940 }
1941 
1942 void
1943 ieee80211_recv_mgmt(struct ieee80211com *ic, struct mbuf *m0,
1944     struct ieee80211_node *ni, struct ieee80211_rxinfo *rxi, int subtype)
1945 {
1946 	switch (subtype) {
1947 	case IEEE80211_FC0_SUBTYPE_BEACON:
1948 		ieee80211_recv_probe_resp(ic, m0, ni, rxi, 0);
1949 		break;
1950 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1951 		ieee80211_recv_probe_resp(ic, m0, ni, rxi, 1);
1952 		break;
1953 #ifndef IEEE80211_STA_ONLY
1954 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1955 		ieee80211_recv_probe_req(ic, m0, ni, rxi);
1956 		break;
1957 #endif
1958 	case IEEE80211_FC0_SUBTYPE_AUTH:
1959 		ieee80211_recv_auth(ic, m0, ni, rxi);
1960 		break;
1961 #ifndef IEEE80211_STA_ONLY
1962 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1963 		ieee80211_recv_assoc_req(ic, m0, ni, rxi, 0);
1964 		break;
1965 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1966 		ieee80211_recv_assoc_req(ic, m0, ni, rxi, 1);
1967 		break;
1968 #endif
1969 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1970 		ieee80211_recv_assoc_resp(ic, m0, ni, 0);
1971 		break;
1972 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
1973 		ieee80211_recv_assoc_resp(ic, m0, ni, 1);
1974 		break;
1975 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
1976 		ieee80211_recv_deauth(ic, m0, ni);
1977 		break;
1978 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
1979 		ieee80211_recv_disassoc(ic, m0, ni);
1980 		break;
1981 	case IEEE80211_FC0_SUBTYPE_ACTION:
1982 		ieee80211_recv_action(ic, m0, ni);
1983 		break;
1984 	default:
1985 		DPRINTF(("mgmt frame with subtype 0x%x not handled\n",
1986 		    subtype));
1987 		ic->ic_stats.is_rx_badsubtype++;
1988 		break;
1989 	}
1990 }
1991 
1992 #ifndef IEEE80211_STA_ONLY
1993 /*
1994  * Process an incoming PS-Poll control frame (see 11.2).
1995  */
1996 void
1997 ieee80211_recv_pspoll(struct ieee80211com *ic, struct mbuf *m,
1998     struct ieee80211_node *ni)
1999 {
2000 	struct ifnet *ifp = &ic->ic_if;
2001 	struct ieee80211_frame_pspoll *psp;
2002 	struct ieee80211_frame *wh;
2003 	u_int16_t aid;
2004 
2005 	if (ic->ic_opmode != IEEE80211_M_HOSTAP ||
2006 	    !(ic->ic_caps & IEEE80211_C_APPMGT) ||
2007 	    ni->ni_state != IEEE80211_STA_ASSOC)
2008 		return;
2009 
2010 	if (m->m_len < sizeof(*psp)) {
2011 		DPRINTF(("frame too short, len %u\n", m->m_len));
2012 		ic->ic_stats.is_rx_tooshort++;
2013 		return;
2014 	}
2015 	psp = mtod(m, struct ieee80211_frame_pspoll *);
2016 	if (!IEEE80211_ADDR_EQ(psp->i_bssid, ic->ic_bss->ni_bssid)) {
2017 		DPRINTF(("discard pspoll frame to BSS %s\n",
2018 		    ether_sprintf(psp->i_bssid)));
2019 		ic->ic_stats.is_rx_wrongbss++;
2020 		return;
2021 	}
2022 	aid = letoh16(*(u_int16_t *)psp->i_aid);
2023 	if (aid != ni->ni_associd) {
2024 		DPRINTF(("invalid pspoll aid %x from %s\n", aid,
2025 		    ether_sprintf(psp->i_ta)));
2026 		return;
2027 	}
2028 
2029 	/* take the first queued frame and put it out.. */
2030 	IF_DEQUEUE(&ni->ni_savedq, m);
2031 	if (m == NULL)
2032 		return;
2033 	if (IF_IS_EMPTY(&ni->ni_savedq)) {
2034 		/* last queued frame, turn off the TIM bit */
2035 		(*ic->ic_set_tim)(ic, ni->ni_associd, 0);
2036 	} else {
2037 		/* more queued frames, set the more data bit */
2038 		wh = mtod(m, struct ieee80211_frame *);
2039 		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
2040 	}
2041 	IF_ENQUEUE(&ic->ic_pwrsaveq, m);
2042 	(*ifp->if_start)(ifp);
2043 }
2044 #endif	/* IEEE80211_STA_ONLY */
2045