xref: /titanic_44/usr/src/uts/common/io/net80211/net80211_input.c (revision 67dbe2be0c0f1e2eb428b89088bb5667e8f0b9f6)
1 /*
2  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Copyright (c) 2001 Atsushi Onoe
8  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * Alternatively, this software may be distributed under the terms of the
23  * GNU General Public License ("GPL") version 2 as published by the Free
24  * Software Foundation.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*
39  * Process received frame
40  */
41 
42 #include <sys/mac_provider.h>
43 #include <sys/byteorder.h>
44 #include <sys/strsun.h>
45 #include "net80211_impl.h"
46 
47 static mblk_t *ieee80211_defrag(ieee80211com_t *, ieee80211_node_t *,
48     mblk_t *, int);
49 
50 /*
51  * Process a received frame.  The node associated with the sender
52  * should be supplied.  If nothing was found in the node table then
53  * the caller is assumed to supply a reference to ic_bss instead.
54  * The RSSI and a timestamp are also supplied.  The RSSI data is used
55  * during AP scanning to select a AP to associate with; it can have
56  * any units so long as values have consistent units and higher values
57  * mean ``better signal''.  The receive timestamp is currently not used
58  * by the 802.11 layer.
59  */
60 int
61 ieee80211_input(ieee80211com_t *ic, mblk_t *mp, struct ieee80211_node *in,
62     int32_t rssi, uint32_t rstamp)
63 {
64 	struct ieee80211_frame *wh;
65 	struct ieee80211_key *key;
66 	uint8_t *bssid;
67 	int hdrspace;
68 	int len;
69 	uint16_t rxseq;
70 	uint8_t dir;
71 	uint8_t type;
72 	uint8_t subtype;
73 	uint8_t tid;
74 	uint8_t qos;
75 
76 	if (mp->b_flag & M_AMPDU) {
77 		/*
78 		 * Fastpath for A-MPDU reorder q resubmission.  Frames
79 		 * w/ M_AMPDU marked have already passed through here
80 		 * but were received out of order and been held on the
81 		 * reorder queue.  When resubmitted they are marked
82 		 * with the M_AMPDU flag and we can bypass most of the
83 		 * normal processing.
84 		 */
85 		IEEE80211_LOCK(ic);
86 		wh = (struct ieee80211_frame *)mp->b_rptr;
87 		type = IEEE80211_FC0_TYPE_DATA;
88 		dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
89 		subtype = IEEE80211_FC0_SUBTYPE_QOS;
90 		hdrspace = ieee80211_hdrspace(ic, wh);	/* optimize */
91 		/* clear driver/net80211 flags before passing up */
92 		mp->b_flag &= ~M_AMPDU;
93 		goto resubmit_ampdu;
94 	}
95 
96 	ASSERT(in != NULL);
97 	in->in_inact = in->in_inact_reload;
98 	type = (uint8_t)-1;		/* undefined */
99 	len = MBLKL(mp);
100 	if (len < sizeof (struct ieee80211_frame_min)) {
101 		ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_input: "
102 		    "too short (1): len %u", len);
103 		goto out;
104 	}
105 	/*
106 	 * Bit of a cheat here, we use a pointer for a 3-address
107 	 * frame format but don't reference fields past outside
108 	 * ieee80211_frame_min w/o first validating the data is
109 	 * present.
110 	 */
111 	wh = (struct ieee80211_frame *)mp->b_rptr;
112 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
113 	    IEEE80211_FC0_VERSION_0) {
114 		ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_input: "
115 		    "discard pkt with wrong version %x", wh->i_fc[0]);
116 		goto out;
117 	}
118 
119 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
120 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
121 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
122 
123 	IEEE80211_LOCK(ic);
124 	if (!(ic->ic_flags & IEEE80211_F_SCAN)) {
125 		switch (ic->ic_opmode) {
126 		case IEEE80211_M_STA:
127 			bssid = wh->i_addr2;
128 			if (!IEEE80211_ADDR_EQ(bssid, in->in_bssid))
129 				goto out_exit_mutex;
130 			break;
131 		case IEEE80211_M_IBSS:
132 		case IEEE80211_M_AHDEMO:
133 			if (dir != IEEE80211_FC1_DIR_NODS) {
134 				bssid = wh->i_addr1;
135 			} else if (type == IEEE80211_FC0_TYPE_CTL) {
136 				bssid = wh->i_addr1;
137 			} else {
138 				if (len < sizeof (struct ieee80211_frame)) {
139 					ieee80211_dbg(IEEE80211_MSG_ANY,
140 					    "ieee80211_input: too short(2):"
141 					    "len %u\n", len);
142 					goto out_exit_mutex;
143 				}
144 				bssid = wh->i_addr3;
145 			}
146 			if (type != IEEE80211_FC0_TYPE_DATA)
147 				break;
148 			/*
149 			 * Data frame, validate the bssid.
150 			 */
151 			if (!IEEE80211_ADDR_EQ(bssid, ic->ic_bss->in_bssid) &&
152 			    !IEEE80211_ADDR_EQ(bssid, wifi_bcastaddr)) {
153 				/* not interested in */
154 				ieee80211_dbg(IEEE80211_MSG_INPUT,
155 				    "ieee80211_input: not to bss %s\n",
156 				    ieee80211_macaddr_sprintf(bssid));
157 				goto out_exit_mutex;
158 			}
159 			/*
160 			 * For adhoc mode we cons up a node when it doesn't
161 			 * exist. This should probably done after an ACL check.
162 			 */
163 			if (in == ic->ic_bss &&
164 			    ic->ic_opmode != IEEE80211_M_HOSTAP &&
165 			    !IEEE80211_ADDR_EQ(wh->i_addr2, in->in_macaddr)) {
166 				/*
167 				 * Fake up a node for this newly
168 				 * discovered member of the IBSS.
169 				 */
170 				in = ieee80211_fakeup_adhoc_node(&ic->ic_sta,
171 				    wh->i_addr2);
172 				if (in == NULL) {
173 					/* NB: stat kept for alloc failure */
174 					goto out_exit_mutex;
175 				}
176 			}
177 			break;
178 		default:
179 			goto out_exit_mutex;
180 		}
181 		in->in_rssi = (uint8_t)rssi;
182 		in->in_rstamp = rstamp;
183 		if (!(type & IEEE80211_FC0_TYPE_CTL)) {
184 			if (IEEE80211_QOS_HAS_SEQ(wh)) {
185 				tid = ((struct ieee80211_qosframe *)wh)->
186 				    i_qos[0] & IEEE80211_QOS_TID;
187 				if (TID_TO_WME_AC(tid) >= WME_AC_VI)
188 					ic->ic_wme.wme_hipri_traffic++;
189 				tid++;
190 			} else {
191 				tid = IEEE80211_NONQOS_TID;
192 			}
193 			rxseq = LE_16(*(uint16_t *)wh->i_seq);
194 			if ((in->in_flags & IEEE80211_NODE_HT) == 0 &&
195 			    (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
196 			    (rxseq - in->in_rxseqs[tid]) <= 0) {
197 				/* duplicate, discard */
198 				ieee80211_dbg(IEEE80211_MSG_INPUT,
199 				    "ieee80211_input: duplicate",
200 				    "seqno <%u,%u> fragno <%u,%u> tid %u",
201 				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
202 				    in->in_rxseqs[tid] >>
203 				    IEEE80211_SEQ_SEQ_SHIFT,
204 				    rxseq & IEEE80211_SEQ_FRAG_MASK,
205 				    in->in_rxseqs[tid] &
206 				    IEEE80211_SEQ_FRAG_MASK,
207 				    tid);
208 				ic->ic_stats.is_rx_dups++;
209 				goto out_exit_mutex;
210 			}
211 			in->in_rxseqs[tid] = rxseq;
212 		}
213 	}
214 
215 	switch (type) {
216 	case IEEE80211_FC0_TYPE_DATA:
217 		hdrspace = ieee80211_hdrspace(ic, wh);
218 		if (len < hdrspace) {
219 			ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_input: "
220 			    "data too short: expecting %u", hdrspace);
221 			goto out_exit_mutex;
222 		}
223 		switch (ic->ic_opmode) {
224 		case IEEE80211_M_STA:
225 			if (dir != IEEE80211_FC1_DIR_FROMDS) {
226 				ieee80211_dbg(IEEE80211_MSG_INPUT,
227 				    "ieee80211_input: data ",
228 				    "unknown dir 0x%x", dir);
229 				goto out_exit_mutex;
230 			}
231 			if (IEEE80211_IS_MULTICAST(wh->i_addr1) &&
232 			    IEEE80211_ADDR_EQ(wh->i_addr3, ic->ic_macaddr)) {
233 				/*
234 				 * In IEEE802.11 network, multicast packet
235 				 * sent from me is broadcasted from AP.
236 				 * It should be silently discarded for
237 				 * SIMPLEX interface.
238 				 */
239 				ieee80211_dbg(IEEE80211_MSG_INPUT,
240 				    "ieee80211_input: multicast echo\n");
241 				goto out_exit_mutex;
242 			}
243 			break;
244 		case IEEE80211_M_IBSS:
245 		case IEEE80211_M_AHDEMO:
246 			if (dir != IEEE80211_FC1_DIR_NODS) {
247 				ieee80211_dbg(IEEE80211_MSG_INPUT,
248 				    "ieee80211_input: unknown dir 0x%x",
249 				    dir);
250 				goto out_exit_mutex;
251 			}
252 			break;
253 		default:
254 			ieee80211_err("ieee80211_input: "
255 			    "receive data, unknown opmode %u, skip\n",
256 			    ic->ic_opmode);
257 			goto out_exit_mutex;
258 		}
259 
260 		/*
261 		 * Handle A-MPDU re-ordering.  The station must be
262 		 * associated and negotiated HT.  The frame must be
263 		 * a QoS frame (not QoS null data) and not previously
264 		 * processed for A-MPDU re-ordering.  If the frame is
265 		 * to be processed directly then ieee80211_ampdu_reorder
266 		 * will return 0; otherwise it has consumed the mbuf
267 		 * and we should do nothing more with it.
268 		 */
269 		if ((in->in_flags & IEEE80211_NODE_HT) &&
270 		    (subtype == IEEE80211_FC0_SUBTYPE_QOS)) {
271 			IEEE80211_UNLOCK(ic);
272 			if (ieee80211_ampdu_reorder(in, mp) != 0) {
273 				mp = NULL;	/* CONSUMED */
274 				goto out;
275 			}
276 			IEEE80211_LOCK(ic);
277 		}
278 	resubmit_ampdu:
279 
280 		/*
281 		 * Handle privacy requirements.
282 		 */
283 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
284 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
285 				/*
286 				 * Discard encrypted frames when privacy off.
287 				 */
288 				ieee80211_dbg(IEEE80211_MSG_INPUT,
289 				    "ieee80211_input: ""WEP PRIVACY off");
290 				ic->ic_stats.is_wep_errors++;
291 				goto out_exit_mutex;
292 			}
293 			key = ieee80211_crypto_decap(ic, mp, hdrspace);
294 			if (key == NULL) {
295 				/* NB: stats+msgs handled in crypto_decap */
296 				ic->ic_stats.is_wep_errors++;
297 				goto out_exit_mutex;
298 			}
299 			wh = (struct ieee80211_frame *)mp->b_rptr;
300 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
301 		} else {
302 			key = NULL;
303 		}
304 
305 		/*
306 		 * Save QoS bits for use below--before we strip the header.
307 		 */
308 		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
309 			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
310 			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
311 			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
312 		} else {
313 			qos = 0;
314 		}
315 
316 		/*
317 		 * Next up, any fragmentation
318 		 */
319 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
320 			mp = ieee80211_defrag(ic, in, mp, hdrspace);
321 			if (mp == NULL) {
322 				/* Fragment dropped or frame not complete yet */
323 				goto out_exit_mutex;
324 			}
325 		}
326 		wh = NULL;	/* no longer valid, catch any uses */
327 
328 		/*
329 		 * Next strip any MSDU crypto bits.
330 		 */
331 		if (key != NULL && !ieee80211_crypto_demic(ic, key, mp, 0)) {
332 			ieee80211_dbg(IEEE80211_MSG_INPUT, "ieee80211_input: "
333 			    "data demic error\n");
334 			goto out_exit_mutex;
335 		}
336 
337 		if (qos & IEEE80211_QOS_AMSDU) {
338 			ieee80211_dbg(IEEE80211_MSG_INPUT | IEEE80211_MSG_HT,
339 			    "ieee80211_input: QOS_AMSDU (%x)\n", qos);
340 
341 			mp = ieee80211_decap_amsdu(in, mp);
342 			if (mp == NULL)		/* MSDU processed by HT */
343 				goto out_exit_mutex;
344 		}
345 
346 		ic->ic_stats.is_rx_frags++;
347 		ic->ic_stats.is_rx_bytes += len;
348 		IEEE80211_UNLOCK(ic);
349 		mac_rx(ic->ic_mach, NULL, mp);
350 		return (IEEE80211_FC0_TYPE_DATA);
351 
352 	case IEEE80211_FC0_TYPE_MGT:
353 		if (dir != IEEE80211_FC1_DIR_NODS)
354 			goto out_exit_mutex;
355 		if (len < sizeof (struct ieee80211_frame))
356 			goto out_exit_mutex;
357 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
358 			if (subtype != IEEE80211_FC0_SUBTYPE_AUTH) {
359 				/*
360 				 * Only shared key auth frames with a challenge
361 				 * should be encrypted, discard all others.
362 				 */
363 				ieee80211_dbg(IEEE80211_MSG_INPUT,
364 				    "ieee80211_input: "
365 				    "%s WEP set but not permitted",
366 				    IEEE80211_SUBTYPE_NAME(subtype));
367 				ic->ic_stats.is_wep_errors++;
368 				goto out_exit_mutex;
369 			}
370 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
371 				/*
372 				 * Discard encrypted frames when privacy off.
373 				 */
374 				ieee80211_dbg(IEEE80211_MSG_INPUT,
375 				    "ieee80211_input: "
376 				    "mgt WEP set but PRIVACY off");
377 				ic->ic_stats.is_wep_errors++;
378 				goto out_exit_mutex;
379 			}
380 			hdrspace = ieee80211_hdrspace(ic, wh);
381 			key = ieee80211_crypto_decap(ic, mp, hdrspace);
382 			if (key == NULL) {
383 				/* NB: stats+msgs handled in crypto_decap */
384 				goto out_exit_mutex;
385 			}
386 			wh = (struct ieee80211_frame *)mp->b_rptr;
387 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
388 		}
389 		IEEE80211_UNLOCK(ic);
390 		ic->ic_recv_mgmt(ic, mp, in, subtype, rssi, rstamp);
391 		goto out;
392 
393 	case IEEE80211_FC0_TYPE_CTL:
394 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
395 			switch (subtype) {
396 			case IEEE80211_FC0_SUBTYPE_BAR:
397 				ieee80211_recv_bar(in, mp);
398 				break;
399 			}
400 		}
401 		break;
402 
403 	default:
404 		ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_input: "
405 		    "bad frame type 0x%x", type);
406 		/* should not come here */
407 		break;
408 	}
409 out_exit_mutex:
410 	IEEE80211_UNLOCK(ic);
411 out:
412 	if (mp != NULL)
413 		freemsg(mp);
414 
415 	return (type);
416 }
417 
418 /*
419  * This function reassemble fragments.
420  * More fragments bit in the frame control means the packet is fragmented.
421  * While the sequence control field consists of 4-bit fragment number
422  * field and a 12-bit sequence number field.
423  */
424 /* ARGSUSED */
425 static mblk_t *
426 ieee80211_defrag(ieee80211com_t *ic, struct ieee80211_node *in, mblk_t *mp,
427     int hdrspace)
428 {
429 	struct ieee80211_frame *wh = (struct ieee80211_frame *)mp->b_rptr;
430 	struct ieee80211_frame *lwh;
431 	mblk_t *mfrag;
432 	uint16_t rxseq;
433 	uint8_t fragno;
434 	uint8_t more_frag;
435 
436 	ASSERT(!IEEE80211_IS_MULTICAST(wh->i_addr1));
437 	more_frag = wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG;
438 	rxseq = LE_16(*(uint16_t *)wh->i_seq);
439 	fragno = rxseq & IEEE80211_SEQ_FRAG_MASK;
440 
441 	/* Quick way out, if there's nothing to defragment */
442 	if (!more_frag && fragno == 0 && in->in_rxfrag == NULL)
443 		return (mp);
444 
445 	/*
446 	 * Remove frag to insure it doesn't get reaped by timer.
447 	 */
448 	if (in->in_table == NULL) {
449 		/*
450 		 * Should never happen.  If the node is orphaned (not in
451 		 * the table) then input packets should not reach here.
452 		 * Otherwise, a concurrent request that yanks the table
453 		 * should be blocked by other interlocking and/or by first
454 		 * shutting the driver down.  Regardless, be defensive
455 		 * here and just bail
456 		 */
457 		freemsg(mp);
458 		return (NULL);
459 	}
460 	IEEE80211_NODE_LOCK(in->in_table);
461 	mfrag = in->in_rxfrag;
462 	in->in_rxfrag = NULL;
463 	IEEE80211_NODE_UNLOCK(in->in_table);
464 
465 	/*
466 	 * Validate new fragment is in order and
467 	 * related to the previous ones.
468 	 */
469 	if (mfrag != NULL) {
470 		uint16_t last_rxseq;
471 
472 		lwh = (struct ieee80211_frame *)mfrag->b_rptr;
473 		last_rxseq = LE_16(*(uint16_t *)lwh->i_seq);
474 		/*
475 		 * Sequence control field contains 12-bit sequence no
476 		 * and 4-bit fragment number. For fragemnts, the
477 		 * sequence no is not changed.
478 		 * NB: check seq # and frag together
479 		 */
480 		if (rxseq != last_rxseq + 1 ||
481 		    !IEEE80211_ADDR_EQ(wh->i_addr1, lwh->i_addr1) ||
482 		    !IEEE80211_ADDR_EQ(wh->i_addr2, lwh->i_addr2)) {
483 			/*
484 			 * Unrelated fragment or no space for it,
485 			 * clear current fragments.
486 			 */
487 			freemsg(mfrag);
488 			mfrag = NULL;
489 		}
490 	}
491 
492 	if (mfrag == NULL) {
493 		if (fragno != 0) {	/* !first fragment, discard */
494 			freemsg(mp);
495 			return (NULL);
496 		}
497 		mfrag = mp;
498 	} else {			/* concatenate */
499 		(void) adjmsg(mp, hdrspace);
500 		linkb(mfrag, mp);
501 		/* track last seqnum and fragno */
502 		lwh = (struct ieee80211_frame *)mfrag->b_rptr;
503 		*(uint16_t *)lwh->i_seq = *(uint16_t *)wh->i_seq;
504 	}
505 	if (more_frag != 0) {		/* more to come, save */
506 		in->in_rxfragstamp = ddi_get_lbolt();
507 		in->in_rxfrag = mfrag;
508 		mfrag = NULL;
509 	}
510 
511 	return (mfrag);
512 }
513 
514 /*
515  * Install received rate set information in the node's state block.
516  */
517 int
518 ieee80211_setup_rates(struct ieee80211_node *in, const uint8_t *rates,
519     const uint8_t *xrates, int flags)
520 {
521 	struct ieee80211_rateset *rs = &in->in_rates;
522 
523 	bzero(rs, sizeof (*rs));
524 	rs->ir_nrates = rates[1];
525 	/* skip 1 byte element ID and 1 byte length */
526 	bcopy(rates + 2, rs->ir_rates, rs->ir_nrates);
527 	if (xrates != NULL) {
528 		uint8_t nxrates;
529 
530 		/*
531 		 * Tack on 11g extended supported rate element.
532 		 */
533 		nxrates = xrates[1];
534 		if (rs->ir_nrates + nxrates > IEEE80211_RATE_MAXSIZE) {
535 			nxrates = IEEE80211_RATE_MAXSIZE - rs->ir_nrates;
536 			ieee80211_dbg(IEEE80211_MSG_XRATE,
537 			    "ieee80211_setup_rates: %s",
538 			    "[%s] extended rate set too large;"
539 			    " only using %u of %u rates\n",
540 			    ieee80211_macaddr_sprintf(in->in_macaddr),
541 			    nxrates, xrates[1]);
542 		}
543 		bcopy(xrates + 2, rs->ir_rates + rs->ir_nrates, nxrates);
544 		rs->ir_nrates += nxrates;
545 	}
546 	return (ieee80211_fix_rate(in, &in->in_rates, flags));
547 }
548 
549 /*
550  * Process open-system authentication response frame and start
551  * association if the authentication request is accepted.
552  */
553 static void
554 ieee80211_auth_open(ieee80211com_t *ic, struct ieee80211_frame *wh,
555     struct ieee80211_node *in, uint16_t seq, uint16_t status)
556 {
557 	IEEE80211_LOCK_ASSERT(ic);
558 	if (in->in_authmode == IEEE80211_AUTH_SHARED) {
559 		ieee80211_dbg(IEEE80211_MSG_AUTH,
560 		    "open auth: bad sta auth mode %u", in->in_authmode);
561 		return;
562 	}
563 	if (ic->ic_opmode == IEEE80211_M_STA) {
564 		if (ic->ic_state != IEEE80211_S_AUTH ||
565 		    seq != IEEE80211_AUTH_OPEN_RESPONSE) {
566 			return;
567 		}
568 		IEEE80211_UNLOCK(ic);
569 		if (status != 0) {
570 			ieee80211_dbg(IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
571 			    "open auth failed (reason %d)\n", status);
572 			if (in != ic->ic_bss)
573 				in->in_fails++;
574 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
575 		} else {
576 			/* i_fc[0] - frame control's type & subtype field */
577 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
578 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
579 		}
580 		IEEE80211_LOCK(ic);
581 	} else {
582 		ieee80211_dbg(IEEE80211_MSG_AUTH, "ieee80211_auth_open: "
583 		    "bad operating mode %u", ic->ic_opmode);
584 	}
585 }
586 
587 /*
588  * Allocate challenge text for use by shared-key authentication
589  * Return B_TRUE on success, B_FALST otherwise.
590  */
591 static boolean_t
592 ieee80211_alloc_challenge(struct ieee80211_node *in)
593 {
594 	if (in->in_challenge == NULL) {
595 		in->in_challenge = kmem_alloc(IEEE80211_CHALLENGE_LEN,
596 		    KM_NOSLEEP);
597 	}
598 	if (in->in_challenge == NULL) {
599 		ieee80211_dbg(IEEE80211_MSG_DEBUG | IEEE80211_MSG_AUTH,
600 		    "[%s] shared key challenge alloc failed\n",
601 		    ieee80211_macaddr_sprintf(in->in_macaddr));
602 	}
603 	return (in->in_challenge != NULL);
604 }
605 
606 /*
607  * Process shared-key authentication response frames. If authentication
608  * succeeds, start association; otherwise, restart scan.
609  */
610 static void
611 ieee80211_auth_shared(ieee80211com_t *ic, struct ieee80211_frame *wh,
612     uint8_t *frm, uint8_t *efrm, struct ieee80211_node *in, uint16_t seq,
613     uint16_t status)
614 {
615 	uint8_t *challenge;
616 
617 	/*
618 	 * Pre-shared key authentication is evil; accept
619 	 * it only if explicitly configured (it is supported
620 	 * mainly for compatibility with clients like OS X).
621 	 */
622 	IEEE80211_LOCK_ASSERT(ic);
623 	if (in->in_authmode != IEEE80211_AUTH_AUTO &&
624 	    in->in_authmode != IEEE80211_AUTH_SHARED) {
625 		ieee80211_dbg(IEEE80211_MSG_AUTH, "ieee80211_auth_shared: "
626 		    "bad sta auth mode %u", in->in_authmode);
627 		goto bad;
628 	}
629 
630 	challenge = NULL;
631 	if (frm + 1 < efrm) {
632 		/*
633 		 * Challenge text information element
634 		 * frm[0] - element ID
635 		 * frm[1] - length
636 		 * frm[2]... - challenge text
637 		 */
638 		if ((frm[1] + 2) > (_PTRDIFF(efrm, frm))) {
639 			ieee80211_dbg(IEEE80211_MSG_AUTH,
640 			    "ieee80211_auth_shared: ie %d%d too long\n",
641 			    frm[0], (frm[1] + 2) - (_PTRDIFF(efrm, frm)));
642 			goto bad;
643 		}
644 		if (*frm == IEEE80211_ELEMID_CHALLENGE)
645 			challenge = frm;
646 		frm += frm[1] + 2;
647 	}
648 	switch (seq) {
649 	case IEEE80211_AUTH_SHARED_CHALLENGE:
650 	case IEEE80211_AUTH_SHARED_RESPONSE:
651 		if (challenge == NULL) {
652 			ieee80211_dbg(IEEE80211_MSG_AUTH,
653 			    "ieee80211_auth_shared: no challenge\n");
654 			goto bad;
655 		}
656 		if (challenge[1] != IEEE80211_CHALLENGE_LEN) {
657 			ieee80211_dbg(IEEE80211_MSG_AUTH,
658 			    "ieee80211_auth_shared: bad challenge len %d\n",
659 			    challenge[1]);
660 			goto bad;
661 		}
662 	default:
663 		break;
664 	}
665 	switch (ic->ic_opmode) {
666 	case IEEE80211_M_STA:
667 		if (ic->ic_state != IEEE80211_S_AUTH)
668 			return;
669 		switch (seq) {
670 		case IEEE80211_AUTH_SHARED_PASS:
671 			if (in->in_challenge != NULL) {
672 				kmem_free(in->in_challenge,
673 				    IEEE80211_CHALLENGE_LEN);
674 				in->in_challenge = NULL;
675 			}
676 			if (status != 0) {
677 				ieee80211_dbg(IEEE80211_MSG_DEBUG |
678 				    IEEE80211_MSG_AUTH,
679 				    "shared key auth failed (reason %d)\n",
680 				    status);
681 				if (in != ic->ic_bss)
682 					in->in_fails++;
683 				return;
684 			}
685 			IEEE80211_UNLOCK(ic);
686 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
687 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
688 			IEEE80211_LOCK(ic);
689 			break;
690 		case IEEE80211_AUTH_SHARED_CHALLENGE:
691 			if (!ieee80211_alloc_challenge(in))
692 				return;
693 			bcopy(&challenge[2], in->in_challenge, challenge[1]);
694 			IEEE80211_UNLOCK(ic);
695 			IEEE80211_SEND_MGMT(ic, in, IEEE80211_FC0_SUBTYPE_AUTH,
696 			    seq + 1);
697 			IEEE80211_LOCK(ic);
698 			break;
699 		default:
700 			ieee80211_dbg(IEEE80211_MSG_AUTH, "80211_auth_shared: "
701 			    "shared key auth: bad seq %d", seq);
702 			return;
703 		}
704 		break;
705 
706 	default:
707 		ieee80211_dbg(IEEE80211_MSG_AUTH,
708 		    "ieee80211_auth_shared: bad opmode %u\n",
709 		    ic->ic_opmode);
710 		break;
711 	}
712 	return;
713 bad:
714 	if (ic->ic_opmode == IEEE80211_M_STA) {
715 		/*
716 		 * Kick the state machine.  This short-circuits
717 		 * using the mgt frame timeout to trigger the
718 		 * state transition.
719 		 */
720 		if (ic->ic_state == IEEE80211_S_AUTH) {
721 			IEEE80211_UNLOCK(ic);
722 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
723 			IEEE80211_LOCK(ic);
724 		}
725 	}
726 }
727 
728 static int
729 iswpaoui(const uint8_t *frm)
730 {
731 	uint32_t c;
732 	bcopy(frm + 2, &c, 4);
733 	return (frm[1] > 3 && LE_32(c) == ((WPA_OUI_TYPE << 24) | WPA_OUI));
734 }
735 
736 #define	LE_READ_4(p)							\
737 	((uint32_t)							\
738 	((((uint8_t *)(p))[0]) | (((uint8_t *)(p))[1] <<  8) |		\
739 	(((uint8_t *)(p))[2] << 16) | (((uint8_t *)(p))[3] << 24)))
740 
741 #define	LE_READ_2(p)							\
742 	((uint16_t)							\
743 	(((uint8_t *)(p))[0]) | (((uint8_t *)(p))[1] <<  8))
744 
745 static int
746 iswmeoui(const uint8_t *frm)
747 {
748 	return (frm[1] > 3 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI));
749 }
750 
751 static int
752 iswmeparam(const uint8_t *frm)
753 {
754 	return (frm[1] > 5 &&
755 	    LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
756 	    frm[6] == WME_PARAM_OUI_SUBTYPE);
757 }
758 
759 static int
760 iswmeinfo(const uint8_t *frm)
761 {
762 	return (frm[1] > 5 &&
763 	    LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI) &&
764 	    frm[6] == WME_INFO_OUI_SUBTYPE);
765 }
766 
767 static int
768 ishtcapoui(const uint8_t *frm)
769 {
770 	return (frm[1] > 3 &&
771 	    LE_READ_4(frm+2) == ((BCM_OUI_HTCAP<<24)|BCM_OUI));
772 }
773 
774 static int
775 ishtinfooui(const uint8_t *frm)
776 {
777 	return (frm[1] > 3 &&
778 	    LE_READ_4(frm+2) == ((BCM_OUI_HTINFO<<24)|BCM_OUI));
779 }
780 
781 /* ARGSUSED */
782 static int
783 ieee80211_parse_wmeparams(struct ieee80211com *ic, uint8_t *frm,
784 	const struct ieee80211_frame *wh)
785 {
786 #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
787 	struct ieee80211_wme_state *wme = &ic->ic_wme;
788 	uint_t len = frm[1];
789 	uint8_t qosinfo;
790 	int i;
791 
792 	if (len < sizeof (struct ieee80211_wme_param) - 2) {
793 		ieee80211_dbg(IEEE80211_MSG_ELEMID | IEEE80211_MSG_WME,
794 		    "WME too short, len %u", len);
795 		return (-1);
796 	}
797 	qosinfo = frm[offsetof(struct ieee80211_wme_param, wme_qosInfo)];
798 	qosinfo &= WME_QOSINFO_COUNT;
799 	/* do proper check for wraparound */
800 	if (qosinfo == wme->wme_wmeChanParams.cap_info)
801 		return (0);
802 	frm += offsetof(struct ieee80211_wme_param, wme_acParams);
803 	for (i = 0; i < WME_NUM_AC; i++) {
804 		struct wmeParams *wmep =
805 		    &wme->wme_wmeChanParams.cap_wmeParams[i];
806 		/* NB: ACI not used */
807 		wmep->wmep_acm = MS(frm[0], WME_PARAM_ACM);
808 		wmep->wmep_aifsn = MS(frm[0], WME_PARAM_AIFSN);
809 		wmep->wmep_logcwmin = MS(frm[1], WME_PARAM_LOGCWMIN);
810 		wmep->wmep_logcwmax = MS(frm[1], WME_PARAM_LOGCWMAX);
811 		wmep->wmep_txopLimit = LE_READ_2(frm+2);
812 		frm += 4;
813 	}
814 	wme->wme_wmeChanParams.cap_info = qosinfo;
815 	return (1);
816 #undef MS
817 }
818 
819 /*
820  * Process a beacon/probe response frame.
821  * When the device is in station mode, create a node and add it
822  * to the node database for a new ESS or update node info if it's
823  * already there.
824  */
825 static void
826 ieee80211_recv_beacon(ieee80211com_t *ic, mblk_t *mp, struct ieee80211_node *in,
827     int subtype, int rssi, uint32_t rstamp)
828 {
829 	ieee80211_impl_t *im = ic->ic_private;
830 	struct ieee80211_frame *wh;
831 	uint8_t *frm;
832 	uint8_t *efrm;	/* end of frame body */
833 	struct ieee80211_scanparams scan;
834 
835 	wh = (struct ieee80211_frame *)mp->b_rptr;
836 	frm = (uint8_t *)&wh[1];
837 	efrm = (uint8_t *)mp->b_wptr;
838 
839 	/*
840 	 * We process beacon/probe response frames:
841 	 *    o when scanning, or
842 	 *    o station mode when associated (to collect state
843 	 *	updates such as 802.11g slot time), or
844 	 *    o adhoc mode (to discover neighbors)
845 	 * Frames otherwise received are discarded.
846 	 */
847 	if (!((ic->ic_flags & IEEE80211_F_SCAN) ||
848 	    (ic->ic_opmode == IEEE80211_M_STA && in->in_associd != 0) ||
849 	    ic->ic_opmode == IEEE80211_M_IBSS)) {
850 		return;
851 	}
852 
853 	/*
854 	 * beacon/probe response frame format
855 	 *	[8] time stamp
856 	 *	[2] beacon interval
857 	 *	[2] capability information
858 	 *	[tlv] ssid
859 	 *	[tlv] supported rates
860 	 *	[tlv] country information
861 	 *	[tlv] parameter set (FH/DS)
862 	 *	[tlv] erp information
863 	 *	[tlv] extended supported rates
864 	 *	[tlv] WME
865 	 *	[tlv] WPA or RSN
866 	 *	[tlv] HT capabilities
867 	 *	[tlv] HT information
868 	 */
869 	IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
870 	    IEEE80211_BEACON_ELEM_MIN, return);
871 	bzero(&scan, sizeof (scan));
872 	scan.tstamp  = frm;
873 	frm += 8;
874 	scan.bintval = LE_16(*(uint16_t *)frm);
875 	frm += 2;
876 	scan.capinfo = LE_16(*(uint16_t *)frm);
877 	frm += 2;
878 	scan.bchan = ieee80211_chan2ieee(ic, ic->ic_curchan);
879 	scan.chan = scan.bchan;
880 
881 	while (frm < efrm) {
882 		/* Agere element in beacon */
883 		if ((*frm == IEEE80211_ELEMID_AGERE1) ||
884 		    (*frm == IEEE80211_ELEMID_AGERE2)) {
885 			frm = efrm;
886 			break;
887 		}
888 
889 		IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm), frm[1], return);
890 		switch (*frm) {
891 		case IEEE80211_ELEMID_SSID:
892 			scan.ssid = frm;
893 			break;
894 		case IEEE80211_ELEMID_RATES:
895 			scan.rates = frm;
896 			break;
897 		case IEEE80211_ELEMID_COUNTRY:
898 			scan.country = frm;
899 			break;
900 		case IEEE80211_ELEMID_FHPARMS:
901 			if (ic->ic_phytype == IEEE80211_T_FH) {
902 				scan.fhdwell = LE_16(*(uint16_t *)(frm + 2));
903 				scan.chan = IEEE80211_FH_CHAN(frm[4], frm[5]);
904 				scan.fhindex = frm[6];
905 				scan.phytype = IEEE80211_T_FH;
906 			}
907 			break;
908 		case IEEE80211_ELEMID_DSPARMS:
909 			if (ic->ic_phytype != IEEE80211_T_FH) {
910 				scan.chan = frm[2];
911 				scan.phytype = IEEE80211_T_DS;
912 			}
913 			break;
914 		case IEEE80211_ELEMID_TIM:
915 			scan.tim = frm;
916 			scan.timoff = _PTRDIFF(frm, mp->b_rptr);
917 			break;
918 		case IEEE80211_ELEMID_IBSSPARMS:
919 			break;
920 		case IEEE80211_ELEMID_XRATES:
921 			scan.xrates = frm;
922 			break;
923 		case IEEE80211_ELEMID_ERP:
924 			if (frm[1] != 1) {
925 				ieee80211_dbg(IEEE80211_MSG_ELEMID,
926 				    "ieee80211_recv_mgmt: ignore %s, "
927 				    "invalid ERP element; "
928 				    "length %u, expecting 1\n",
929 				    IEEE80211_SUBTYPE_NAME(subtype),
930 				    frm[1]);
931 				break;
932 			}
933 			scan.erp = frm[2];
934 			scan.phytype = IEEE80211_T_OFDM;
935 			break;
936 		case IEEE80211_ELEMID_HTCAP:
937 			scan.htcap = frm;
938 			break;
939 		case IEEE80211_ELEMID_RSN:
940 			scan.wpa = frm;
941 			break;
942 		case IEEE80211_ELEMID_HTINFO:
943 			scan.htinfo = frm;
944 			break;
945 		case IEEE80211_ELEMID_VENDOR:
946 			if (iswpaoui(frm))
947 				scan.wpa = frm;		/* IEEE802.11i D3.0 */
948 			else if (iswmeparam(frm) || iswmeinfo(frm))
949 				scan.wme = frm;
950 			else if (ic->ic_flags_ext & IEEE80211_FEXT_HTCOMPAT) {
951 				/*
952 				 * Accept pre-draft HT ie's if the
953 				 * standard ones have not been seen.
954 				 */
955 				if (ishtcapoui(frm)) {
956 					if (scan.htcap == NULL)
957 						scan.htcap = frm;
958 				} else if (ishtinfooui(frm)) {
959 					if (scan.htinfo == NULL)
960 						scan.htinfo = frm;
961 				}
962 			}
963 			break;
964 		default:
965 			ieee80211_dbg(IEEE80211_MSG_ELEMID,
966 			    "ieee80211_recv_mgmt: ignore %s,"
967 			    "unhandled id %u, len %u, totallen %u",
968 			    IEEE80211_SUBTYPE_NAME(subtype),
969 			    *frm, frm[1],
970 			    MBLKL(mp));
971 			break;
972 		}
973 		/* frm[1] - component length */
974 		frm += IEEE80211_ELEM_LEN(frm[1]);
975 	}
976 	IEEE80211_VERIFY_ELEMENT(scan.rates, IEEE80211_RATE_MAXSIZE, return);
977 	IEEE80211_VERIFY_ELEMENT(scan.ssid, IEEE80211_NWID_LEN, return);
978 	if (ieee80211_isclr(ic->ic_chan_active, scan.chan)) {
979 		ieee80211_dbg(IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
980 		    "ieee80211_recv_mgmt: ignore %s ,"
981 		    "invalid channel %u\n",
982 		    IEEE80211_SUBTYPE_NAME(subtype), scan.chan);
983 		return;
984 	}
985 	if (scan.chan != scan.bchan &&
986 	    ic->ic_phytype != IEEE80211_T_FH) {
987 		/*
988 		 * Frame was received on a channel different from the
989 		 * one indicated in the DS params element id;
990 		 * silently discard it.
991 		 *
992 		 * NB:	this can happen due to signal leakage.
993 		 *	But we should take it for FH phy because
994 		 *	the rssi value should be correct even for
995 		 *	different hop pattern in FH.
996 		 */
997 		ieee80211_dbg(IEEE80211_MSG_ELEMID,
998 		    "ieee80211_recv_mgmt: ignore %s ,"
999 		    "phytype %u channel %u marked for %u\n",
1000 		    IEEE80211_SUBTYPE_NAME(subtype),
1001 		    ic->ic_phytype, scan.bchan, scan.chan);
1002 		return;
1003 	}
1004 	if (!(IEEE80211_BINTVAL_MIN <= scan.bintval &&
1005 	    scan.bintval <= IEEE80211_BINTVAL_MAX)) {
1006 		ieee80211_dbg(IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
1007 		    "ieee80211_recv_mgmt: ignore %s ,"
1008 		    "bogus beacon interval %u\n",
1009 		    IEEE80211_SUBTYPE_NAME(subtype), scan.bintval);
1010 		return;
1011 	}
1012 	/*
1013 	 * Process HT ie's.  This is complicated by our
1014 	 * accepting both the standard ie's and the pre-draft
1015 	 * vendor OUI ie's that some vendors still use/require.
1016 	 */
1017 	if (scan.htcap != NULL) {
1018 		IEEE80211_VERIFY_LENGTH(scan.htcap[1],
1019 		    scan.htcap[0] == IEEE80211_ELEMID_VENDOR ?
1020 		    4 + sizeof (struct ieee80211_ie_htcap) - 2 :
1021 		    sizeof (struct ieee80211_ie_htcap) - 2,
1022 		    scan.htcap = NULL);
1023 	}
1024 	if (scan.htinfo != NULL) {
1025 		IEEE80211_VERIFY_LENGTH(scan.htinfo[1],
1026 		    scan.htinfo[0] == IEEE80211_ELEMID_VENDOR ?
1027 		    4 + sizeof (struct ieee80211_ie_htinfo) - 2 :
1028 		    sizeof (struct ieee80211_ie_htinfo) - 2,
1029 		    scan.htinfo = NULL);
1030 	}
1031 
1032 	/*
1033 	 * When operating in station mode, check for state updates.
1034 	 * Be careful to ignore beacons received while doing a
1035 	 * background scan.  We consider only 11g/WMM stuff right now.
1036 	 */
1037 	if (ic->ic_opmode == IEEE80211_M_STA &&
1038 	    in->in_associd != 0 &&
1039 	    (!(ic->ic_flags & IEEE80211_F_SCAN) ||
1040 	    IEEE80211_ADDR_EQ(wh->i_addr2, in->in_bssid))) {
1041 		/* record tsf of last beacon */
1042 		bcopy(scan.tstamp, in->in_tstamp.data,
1043 		    sizeof (in->in_tstamp));
1044 		/* count beacon frame for s/w bmiss handling */
1045 		im->im_swbmiss_count++;
1046 		im->im_bmiss_count = 0;
1047 
1048 		if ((in->in_capinfo ^ scan.capinfo) &
1049 		    IEEE80211_CAPINFO_SHORT_SLOTTIME) {
1050 			ieee80211_dbg(IEEE80211_MSG_ASSOC,
1051 			    "ieee80211_recv_mgmt: "
1052 			    "[%s] cap change: before 0x%x, now 0x%x\n",
1053 			    ieee80211_macaddr_sprintf(wh->i_addr2),
1054 			    in->in_capinfo, scan.capinfo);
1055 			/*
1056 			 * NB:	we assume short preamble doesn't
1057 			 *	change dynamically
1058 			 */
1059 			ieee80211_set_shortslottime(ic,
1060 			    ic->ic_curmode == IEEE80211_MODE_11A ||
1061 			    (scan.capinfo &
1062 			    IEEE80211_CAPINFO_SHORT_SLOTTIME));
1063 			in->in_capinfo = scan.capinfo;
1064 		}
1065 		if (scan.wme != NULL &&
1066 		    (in->in_flags & IEEE80211_NODE_QOS) &&
1067 		    ieee80211_parse_wmeparams(ic, scan.wme, wh) > 0) {
1068 			ieee80211_wme_updateparams(ic);
1069 		}
1070 		if (scan.htcap != NULL)
1071 			ieee80211_parse_htcap(in, scan.htcap);
1072 		if (scan.htinfo != NULL) {
1073 			ieee80211_parse_htinfo(in, scan.htinfo);
1074 			if (in->in_chan != ic->ic_curchan) {
1075 				/*
1076 				 * Channel has been adjusted based on
1077 				 * negotiated HT parameters; force the
1078 				 * channel state to follow.
1079 				 */
1080 				ieee80211_setcurchan(ic, in->in_chan);
1081 			}
1082 		}
1083 		if (scan.tim != NULL) {
1084 			struct ieee80211_tim_ie *ie;
1085 
1086 			ie = (struct ieee80211_tim_ie *)scan.tim;
1087 			in->in_dtim_count = ie->tim_count;
1088 			in->in_dtim_period = ie->tim_period;
1089 		}
1090 		if (ic->ic_flags & IEEE80211_F_SCAN) {
1091 			ieee80211_add_scan(ic, &scan, wh, subtype, rssi,
1092 			    rstamp);
1093 		}
1094 		return;
1095 	}
1096 	/*
1097 	 * If scanning, just pass information to the scan module.
1098 	 */
1099 	if (ic->ic_flags & IEEE80211_F_SCAN) {
1100 		ieee80211_add_scan(ic, &scan, wh, subtype, rssi, rstamp);
1101 		return;
1102 	}
1103 
1104 	if (ic->ic_opmode == IEEE80211_M_IBSS &&
1105 	    scan.capinfo & IEEE80211_CAPINFO_IBSS) {
1106 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, in->in_macaddr)) {
1107 			/*
1108 			 * Create a new entry in the neighbor table.
1109 			 */
1110 			in = ieee80211_add_neighbor(ic, wh, &scan);
1111 		} else {
1112 			/*
1113 			 * Copy data from beacon to neighbor table.
1114 			 * Some of this information might change after
1115 			 * ieee80211_add_neighbor(), so we just copy
1116 			 * everything over to be safe.
1117 			 */
1118 			ieee80211_init_neighbor(in, wh, &scan);
1119 		}
1120 		if (in != NULL) {
1121 			in->in_rssi = (uint8_t)rssi;
1122 			in->in_rstamp = rstamp;
1123 		}
1124 	}
1125 }
1126 
1127 /*
1128  * Perform input processing for 802.11 management frames.
1129  * It's the default ic_recv_mgmt callback function for the interface
1130  * softc, ic. Tipically ic_recv_mgmt is called within ieee80211_input()
1131  */
1132 void
1133 ieee80211_recv_mgmt(ieee80211com_t *ic, mblk_t *mp, struct ieee80211_node *in,
1134     int subtype, int rssi, uint32_t rstamp)
1135 {
1136 	struct ieee80211_frame *wh;
1137 	uint8_t *frm;		/* pointer to start of the frame */
1138 	uint8_t *efrm;		/* pointer to end of the frame */
1139 	uint8_t *ssid;
1140 	uint8_t *rates;
1141 	uint8_t *xrates;	/* extended rates */
1142 	uint8_t	*wme;
1143 	uint8_t *htcap, *htinfo;
1144 	boolean_t allocbs = B_FALSE;
1145 	uint8_t rate;
1146 	uint16_t algo;		/* authentication algorithm */
1147 	uint16_t seq;		/* sequence no */
1148 	uint16_t status;
1149 	uint16_t capinfo;
1150 	uint16_t associd;	/* association ID */
1151 	const struct ieee80211_action *ia;
1152 
1153 	IEEE80211_LOCK(ic);
1154 	wh = (struct ieee80211_frame *)mp->b_rptr;
1155 	frm = (uint8_t *)&wh[1];
1156 	efrm = (uint8_t *)mp->b_wptr;
1157 	switch (subtype) {
1158 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1159 	case IEEE80211_FC0_SUBTYPE_BEACON:
1160 		ieee80211_recv_beacon(ic, mp, in, subtype, rssi, rstamp);
1161 		break;
1162 
1163 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1164 		if (ic->ic_opmode == IEEE80211_M_STA ||
1165 		    ic->ic_state != IEEE80211_S_RUN ||
1166 		    IEEE80211_IS_MULTICAST(wh->i_addr2)) {
1167 			break;
1168 		}
1169 
1170 		/*
1171 		 * prreq frame format
1172 		 *	[tlv] ssid
1173 		 *	[tlv] supported rates
1174 		 *	[tlv] extended supported rates
1175 		 */
1176 		ssid = rates = xrates = NULL;
1177 		while (frm < efrm) {
1178 			IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
1179 			    frm[1], goto out);
1180 			switch (*frm) {
1181 			case IEEE80211_ELEMID_SSID:
1182 				ssid = frm;
1183 				break;
1184 			case IEEE80211_ELEMID_RATES:
1185 				rates = frm;
1186 				break;
1187 			case IEEE80211_ELEMID_XRATES:
1188 				xrates = frm;
1189 				break;
1190 			}
1191 			frm += frm[1] + 2;
1192 		}
1193 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, break);
1194 		if (xrates != NULL) {
1195 			IEEE80211_VERIFY_ELEMENT(xrates,
1196 			    IEEE80211_RATE_MAXSIZE - rates[1], break);
1197 		}
1198 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, break);
1199 		IEEE80211_VERIFY_SSID(ic->ic_bss, ssid, break);
1200 		if (ic->ic_flags & IEEE80211_F_HIDESSID) {
1201 			if (ssid == NULL || ssid[1] == 0) {
1202 				ieee80211_dbg(IEEE80211_MSG_INPUT,
1203 				    "ieee80211_recv_mgmt: ignore %s, "
1204 				    "no ssid with ssid suppression enabled",
1205 				    IEEE80211_SUBTYPE_NAME(subtype));
1206 				break;
1207 			}
1208 		}
1209 
1210 		if (in == ic->ic_bss) {
1211 			if (ic->ic_opmode != IEEE80211_M_IBSS) {
1212 				in = ieee80211_tmp_node(ic, wh->i_addr2);
1213 				allocbs = B_TRUE;
1214 			} else if (!IEEE80211_ADDR_EQ(wh->i_addr2,
1215 			    in->in_macaddr)) {
1216 				/*
1217 				 * Cannot tell if the sender is operating
1218 				 * in ibss mode.  But we need a new node to
1219 				 * send the response so blindly add them to the
1220 				 * neighbor table.
1221 				 */
1222 				in = ieee80211_fakeup_adhoc_node(&ic->ic_sta,
1223 				    wh->i_addr2);
1224 			}
1225 			if (in == NULL)
1226 				break;
1227 		}
1228 		ieee80211_dbg(IEEE80211_MSG_ASSOC, "ieee80211_recv_mgmt: "
1229 		    "[%s] recv probe req\n",
1230 		    ieee80211_macaddr_sprintf(wh->i_addr2));
1231 		in->in_rssi = (uint8_t)rssi;
1232 		in->in_rstamp = rstamp;
1233 		/*
1234 		 * Adjust and check station's rate list with device's
1235 		 * supported rate.  Send back response if there is at
1236 		 * least one rate or the fixed rate(if being set) is
1237 		 * supported by both station and the device
1238 		 */
1239 		rate = ieee80211_setup_rates(in, rates, xrates,
1240 		    IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1241 		    IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1242 		if (rate & IEEE80211_RATE_BASIC) {
1243 			ieee80211_dbg(IEEE80211_MSG_XRATE, "ieee80211_recv_mgmt"
1244 			    "%s recv'd rate set invalid",
1245 			    IEEE80211_SUBTYPE_NAME(subtype));
1246 		} else {
1247 			IEEE80211_UNLOCK(ic);
1248 			IEEE80211_SEND_MGMT(ic, in,
1249 			    IEEE80211_FC0_SUBTYPE_PROBE_RESP, 0);
1250 			IEEE80211_LOCK(ic);
1251 		}
1252 		if (allocbs) {
1253 			/*
1254 			 * Temporary node created just to send a
1255 			 * response, reclaim immediately.
1256 			 */
1257 			ieee80211_free_node(in);
1258 		}
1259 		break;
1260 
1261 	case IEEE80211_FC0_SUBTYPE_AUTH:
1262 		/*
1263 		 * auth frame format
1264 		 *	[2] algorithm
1265 		 *	[2] sequence
1266 		 *	[2] status
1267 		 *	[tlv*] challenge
1268 		 */
1269 		IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
1270 		    IEEE80211_AUTH_ELEM_MIN, break);
1271 		algo   = LE_16(*(uint16_t *)frm);
1272 		seq    = LE_16(*(uint16_t *)(frm + 2));
1273 		status = LE_16(*(uint16_t *)(frm + 4));
1274 		ieee80211_dbg(IEEE80211_MSG_AUTH, "ieee80211_recv_mgmt: "
1275 		    "[%s] recv auth frame with algorithm %d seq %d\n",
1276 		    ieee80211_macaddr_sprintf(wh->i_addr2), algo, seq);
1277 
1278 		if (ic->ic_flags & IEEE80211_F_COUNTERM) {
1279 			ieee80211_dbg(IEEE80211_MSG_AUTH | IEEE80211_MSG_CRYPTO,
1280 			    "ieee80211_recv_mgmt: ignore auth, %s\n",
1281 			    "TKIP countermeasures enabled");
1282 			break;
1283 		}
1284 		switch (algo) {
1285 		case IEEE80211_AUTH_ALG_SHARED:
1286 			ieee80211_auth_shared(ic, wh, frm + 6, efrm, in,
1287 			    seq, status);
1288 			break;
1289 		case IEEE80211_AUTH_ALG_OPEN:
1290 			ieee80211_auth_open(ic, wh, in, seq, status);
1291 			break;
1292 		default:
1293 			ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_recv_mgmt: "
1294 			    "ignore auth, unsupported alg %d", algo);
1295 			break;
1296 		}
1297 		break;
1298 
1299 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1300 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
1301 		if (ic->ic_opmode != IEEE80211_M_STA ||
1302 		    ic->ic_state != IEEE80211_S_ASSOC)
1303 			break;
1304 
1305 		/*
1306 		 * asresp frame format
1307 		 *	[2] capability information
1308 		 *	[2] status
1309 		 *	[2] association ID
1310 		 *	[tlv] supported rates
1311 		 *	[tlv] extended supported rates
1312 		 *	[tlv] WME
1313 		 *	[tlv] HT capabilities
1314 		 *	[tlv] HT info
1315 		 */
1316 		IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
1317 		    IEEE80211_ASSOC_RESP_ELEM_MIN, break);
1318 		in = ic->ic_bss;
1319 		capinfo = LE_16(*(uint16_t *)frm);
1320 		frm += 2;
1321 		status = LE_16(*(uint16_t *)frm);
1322 		frm += 2;
1323 		if (status != 0) {
1324 			ieee80211_dbg(IEEE80211_MSG_ASSOC,
1325 			    "assoc failed (reason %d)\n", status);
1326 			in = ieee80211_find_node(&ic->ic_scan, wh->i_addr2);
1327 			if (in != NULL) {
1328 				in->in_fails++;
1329 				ieee80211_free_node(in);
1330 			}
1331 			break;
1332 		}
1333 		associd = LE_16(*(uint16_t *)frm);
1334 		frm += 2;
1335 
1336 		rates = xrates = wme = htcap = htinfo = NULL;
1337 		while (frm < efrm) {
1338 			/*
1339 			 * Do not discard frames containing proprietary Agere
1340 			 * elements 128 and 129, as the reported element length
1341 			 * is often wrong. Skip rest of the frame, since we can
1342 			 * not rely on the given element length making it
1343 			 * impossible to know where the next element starts
1344 			 */
1345 			if ((*frm == IEEE80211_ELEMID_AGERE1) ||
1346 			    (*frm == IEEE80211_ELEMID_AGERE2)) {
1347 				frm = efrm;
1348 				break;
1349 			}
1350 
1351 			IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
1352 			    frm[1], goto out);
1353 			switch (*frm) {
1354 			case IEEE80211_ELEMID_RATES:
1355 				rates = frm;
1356 				break;
1357 			case IEEE80211_ELEMID_XRATES:
1358 				xrates = frm;
1359 				break;
1360 			case IEEE80211_ELEMID_HTCAP:
1361 				htcap = frm;
1362 				break;
1363 			case IEEE80211_ELEMID_HTINFO:
1364 				htinfo = frm;
1365 				break;
1366 			case IEEE80211_ELEMID_VENDOR:
1367 				if (iswmeoui(frm))
1368 					wme = frm;
1369 				else if (ic->ic_flags_ext &
1370 				    IEEE80211_FEXT_HTCOMPAT) {
1371 					/*
1372 					 * Accept pre-draft HT ie's if the
1373 					 * standard ones have not been seen.
1374 					 */
1375 					if (ishtcapoui(frm)) {
1376 						if (htcap == NULL)
1377 							htcap = frm;
1378 					} else if (ishtinfooui(frm)) {
1379 						if (htinfo == NULL)
1380 							htinfo = frm;
1381 					}
1382 				}
1383 				break;
1384 			}
1385 			frm += frm[1] + 2;
1386 		}
1387 
1388 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, break);
1389 		/*
1390 		 * Adjust and check AP's rate list with device's
1391 		 * supported rate. Re-start scan if no rate is or the
1392 		 * fixed rate(if being set) cannot be supported by
1393 		 * either AP or the device.
1394 		 */
1395 		rate = ieee80211_setup_rates(in, rates, xrates,
1396 		    IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1397 		    IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1398 		if (rate & IEEE80211_RATE_BASIC) {
1399 			ieee80211_dbg(IEEE80211_MSG_ASSOC,
1400 			    "assoc failed (rate set mismatch)\n");
1401 			if (in != ic->ic_bss)
1402 				in->in_fails++;
1403 			IEEE80211_UNLOCK(ic);
1404 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
1405 			return;
1406 		}
1407 
1408 		in->in_capinfo = capinfo;
1409 		in->in_associd = associd;
1410 		if (wme != NULL &&
1411 		    ieee80211_parse_wmeparams(ic, wme, wh) >= 0) {
1412 			in->in_flags |= IEEE80211_NODE_QOS;
1413 			ieee80211_wme_updateparams(ic);
1414 		} else {
1415 			in->in_flags &= ~IEEE80211_NODE_QOS;
1416 		}
1417 		/*
1418 		 * Setup HT state according to the negotiation.
1419 		 */
1420 		if ((ic->ic_htcaps & IEEE80211_HTC_HT) &&
1421 		    htcap != NULL && htinfo != NULL) {
1422 			ieee80211_ht_node_init(in, htcap);
1423 			ieee80211_parse_htinfo(in, htinfo);
1424 			(void) ieee80211_setup_htrates(in,
1425 			    htcap, IEEE80211_F_JOIN | IEEE80211_F_DOBRS);
1426 			ieee80211_setup_basic_htrates(in, htinfo);
1427 			if (in->in_chan != ic->ic_curchan) {
1428 				/*
1429 				 * Channel has been adjusted based on
1430 				 * negotiated HT parameters; force the
1431 				 * channel state to follow.
1432 				 */
1433 				ieee80211_setcurchan(ic, in->in_chan);
1434 			}
1435 		}
1436 		/*
1437 		 * Configure state now that we are associated.
1438 		 */
1439 		if (ic->ic_curmode == IEEE80211_MODE_11A ||
1440 		    (in->in_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1441 			ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1442 			ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1443 		} else {
1444 			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1445 			ic->ic_flags |= IEEE80211_F_USEBARKER;
1446 		}
1447 		ieee80211_set_shortslottime(ic,
1448 		    ic->ic_curmode == IEEE80211_MODE_11A ||
1449 		    (in->in_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME));
1450 		/*
1451 		 * Honor ERP protection.
1452 		 *
1453 		 * NB:	in_erp should zero for non-11g operation.
1454 		 *	check ic_curmode anyway
1455 		 */
1456 		if (ic->ic_curmode == IEEE80211_MODE_11G &&
1457 		    (in->in_erp & IEEE80211_ERP_USE_PROTECTION))
1458 			ic->ic_flags |= IEEE80211_F_USEPROT;
1459 		else
1460 			ic->ic_flags &= ~IEEE80211_F_USEPROT;
1461 		ieee80211_dbg(IEEE80211_MSG_ASSOC,
1462 		    "assoc success: %s preamble, %s slot time%s%s\n",
1463 		    ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
1464 		    ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
1465 		    ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : "",
1466 		    in->in_flags & IEEE80211_NODE_QOS ? ", QoS" : "");
1467 		IEEE80211_UNLOCK(ic);
1468 		ieee80211_new_state(ic, IEEE80211_S_RUN, subtype);
1469 		return;
1470 
1471 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
1472 		if (ic->ic_state == IEEE80211_S_SCAN)
1473 			break;
1474 
1475 		/*
1476 		 * deauth frame format
1477 		 *	[2] reason
1478 		 */
1479 		IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm), 2, break);
1480 		status = LE_16(*(uint16_t *)frm);
1481 
1482 		ieee80211_dbg(IEEE80211_MSG_AUTH,
1483 		    "recv deauthenticate (reason %d)\n", status);
1484 		switch (ic->ic_opmode) {
1485 		case IEEE80211_M_STA:
1486 			IEEE80211_UNLOCK(ic);
1487 			ieee80211_new_state(ic, IEEE80211_S_AUTH,
1488 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
1489 			return;
1490 		default:
1491 			break;
1492 		}
1493 		break;
1494 
1495 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
1496 		if (ic->ic_state != IEEE80211_S_RUN &&
1497 		    ic->ic_state != IEEE80211_S_ASSOC &&
1498 		    ic->ic_state != IEEE80211_S_AUTH)
1499 			break;
1500 		/*
1501 		 * disassoc frame format
1502 		 *	[2] reason
1503 		 */
1504 		IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm), 2, break);
1505 		status = LE_16(*(uint16_t *)frm);
1506 
1507 		ieee80211_dbg(IEEE80211_MSG_ASSOC,
1508 		    "recv disassociate (reason %d)\n", status);
1509 		switch (ic->ic_opmode) {
1510 		case IEEE80211_M_STA:
1511 			IEEE80211_UNLOCK(ic);
1512 			ieee80211_new_state(ic, IEEE80211_S_ASSOC,
1513 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
1514 			return;
1515 		default:
1516 			break;
1517 		}
1518 		break;
1519 
1520 	case IEEE80211_FC0_SUBTYPE_ACTION:
1521 		if (ic->ic_state != IEEE80211_S_RUN &&
1522 		    ic->ic_state != IEEE80211_S_ASSOC &&
1523 		    ic->ic_state != IEEE80211_S_AUTH)
1524 			break;
1525 
1526 		/*
1527 		 * action frame format:
1528 		 *	[1] category
1529 		 *	[1] action
1530 		 *	[tlv] parameters
1531 		 */
1532 		IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
1533 		    sizeof (struct ieee80211_action), break);
1534 		ia = (const struct ieee80211_action *) frm;
1535 
1536 		/* verify frame payloads but defer processing */
1537 		/* maybe push this to method */
1538 		switch (ia->ia_category) {
1539 		case IEEE80211_ACTION_CAT_BA:
1540 			switch (ia->ia_action) {
1541 			case IEEE80211_ACTION_BA_ADDBA_REQUEST:
1542 			IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
1543 			    sizeof (struct ieee80211_action_ba_addbarequest),
1544 			    break);
1545 			break;
1546 			case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
1547 			IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
1548 			    sizeof (struct ieee80211_action_ba_addbaresponse),
1549 			    break);
1550 			break;
1551 			case IEEE80211_ACTION_BA_DELBA:
1552 			IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
1553 			    sizeof (struct ieee80211_action_ba_delba),
1554 			    break);
1555 			break;
1556 			}
1557 			break;
1558 		case IEEE80211_ACTION_CAT_HT:
1559 			switch (ia->ia_action) {
1560 			case IEEE80211_ACTION_HT_TXCHWIDTH:
1561 			IEEE80211_VERIFY_LENGTH(_PTRDIFF(efrm, frm),
1562 			    sizeof (struct ieee80211_action_ht_txchwidth),
1563 			    break);
1564 			break;
1565 			}
1566 			break;
1567 		}
1568 		ic->ic_recv_action(in, frm, efrm);
1569 		break;
1570 
1571 	default:
1572 		ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_recv_mgmt: "
1573 		    "subtype 0x%x not handled\n", subtype);
1574 		break;
1575 	} /* switch subtype */
1576 out:
1577 	IEEE80211_UNLOCK(ic);
1578 }
1579