xref: /titanic_44/usr/src/uts/common/io/net80211/net80211_node.c (revision 24da5b34f49324ed742a340010ed5bd3d4e06625)
1 /*
2  * Copyright 2007 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-2005 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
39 
40 /*
41  * Node management routines
42  */
43 
44 #include "net80211_impl.h"
45 
46 static ieee80211_node_t *ieee80211_node_alloc(ieee80211com_t *);
47 static void ieee80211_node_cleanup(ieee80211_node_t *);
48 static void ieee80211_node_free(ieee80211_node_t *);
49 static uint8_t ieee80211_node_getrssi(const ieee80211_node_t *);
50 static void ieee80211_setup_node(ieee80211com_t *, ieee80211_node_table_t *,
51     ieee80211_node_t *, const uint8_t *);
52 static void ieee80211_node_reclaim(ieee80211_node_table_t *,
53     ieee80211_node_t *);
54 static void ieee80211_free_node_locked(ieee80211_node_t *);
55 static void ieee80211_free_allnodes(ieee80211_node_table_t *);
56 static void ieee80211_node_leave(ieee80211com_t *, ieee80211_node_t *);
57 static void ieee80211_timeout_scan_candidates(ieee80211_node_table_t *);
58 static void ieee80211_timeout_stations(ieee80211_node_table_t *);
59 static void ieee80211_node_table_init(ieee80211com_t *,
60     ieee80211_node_table_t *, const char *, int, int,
61     void (*timeout)(ieee80211_node_table_t *));
62 static void ieee80211_node_table_cleanup(ieee80211_node_table_t *);
63 
64 /*
65  * association failures before ignored
66  * The failure may be caused by the response frame is lost for
67  * environmental reason. So Try associate more than once before
68  * ignore the node
69  */
70 #define	IEEE80211_STA_FAILS_MAX	2
71 
72 /*
73  * Initialize node database management callbacks for the interface.
74  * This function is called by ieee80211_attach(). These callback
75  * functions may be overridden in special circumstances, as long as
76  * as this is done after calling ieee80211_attach() and prior to any
77  * other call which may allocate a node
78  */
79 void
80 ieee80211_node_attach(ieee80211com_t *ic)
81 {
82 	struct ieee80211_impl *im = ic->ic_private;
83 
84 	ic->ic_node_alloc = ieee80211_node_alloc;
85 	ic->ic_node_free = ieee80211_node_free;
86 	ic->ic_node_cleanup = ieee80211_node_cleanup;
87 	ic->ic_node_getrssi = ieee80211_node_getrssi;
88 
89 	/* default station inactivity timer setings */
90 	im->im_inact_init = IEEE80211_INACT_INIT;
91 	im->im_inact_assoc = IEEE80211_INACT_ASSOC;
92 	im->im_inact_run = IEEE80211_INACT_RUN;
93 	im->im_inact_probe = IEEE80211_INACT_PROBE;
94 }
95 
96 /*
97  * Initialize node databases and the ic_bss node element.
98  */
99 void
100 ieee80211_node_lateattach(ieee80211com_t *ic)
101 {
102 	/*
103 	 * Calculate ic_tim_bitmap size in bytes
104 	 * IEEE80211_AID_MAX defines maximum bits in ic_tim_bitmap
105 	 */
106 	ic->ic_tim_len = howmany(IEEE80211_AID_MAX, 8) * sizeof (uint8_t);
107 
108 	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
109 		IEEE80211_INACT_INIT, IEEE80211_WEP_NKID,
110 		ieee80211_timeout_stations);
111 	ieee80211_node_table_init(ic, &ic->ic_scan, "scan",
112 		IEEE80211_INACT_SCAN, 0, ieee80211_timeout_scan_candidates);
113 
114 	ieee80211_reset_bss(ic);
115 }
116 
117 /*
118  * Destroy all node databases and is usually called during device detach
119  */
120 void
121 ieee80211_node_detach(ieee80211com_t *ic)
122 {
123 	/* Node Detach */
124 	if (ic->ic_bss != NULL) {
125 		ieee80211_free_node(ic->ic_bss);
126 		ic->ic_bss = NULL;
127 	}
128 	ieee80211_node_table_cleanup(&ic->ic_scan);
129 	ieee80211_node_table_cleanup(&ic->ic_sta);
130 }
131 
132 /*
133  * Increase a node's reference count
134  *
135  * Return pointer to the node
136  */
137 ieee80211_node_t *
138 ieee80211_ref_node(ieee80211_node_t *in)
139 {
140 	ieee80211_node_incref(in);
141 	return (in);
142 }
143 
144 /*
145  * Dexrease a node's reference count
146  */
147 void
148 ieee80211_unref_node(ieee80211_node_t **in)
149 {
150 	ieee80211_node_decref(*in);
151 	*in = NULL;			/* guard against use */
152 }
153 
154 /*
155  * Mark ports authorized for data traffic. This function is usually
156  * used by 802.1x authenticator.
157  */
158 void
159 ieee80211_node_authorize(ieee80211_node_t *in)
160 {
161 	ieee80211_impl_t *im = in->in_ic->ic_private;
162 
163 	in->in_flags |= IEEE80211_NODE_AUTH;
164 	in->in_inact_reload = im->im_inact_run;
165 }
166 
167 /*
168  * Mark ports unauthorized for data traffic. This function is usually
169  * used by 802.1x authenticator.
170  */
171 void
172 ieee80211_node_unauthorize(ieee80211_node_t *in)
173 {
174 	in->in_flags &= ~IEEE80211_NODE_AUTH;
175 }
176 
177 /*
178  * Set/change the channel.  The rate set is also updated as
179  * to insure a consistent view by drivers.
180  */
181 static void
182 ieee80211_node_setchan(ieee80211com_t *ic, ieee80211_node_t *in,
183     struct ieee80211_channel *chan)
184 {
185 	if (chan == IEEE80211_CHAN_ANYC)
186 		chan = ic->ic_curchan;
187 	in->in_chan = chan;
188 	in->in_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)];
189 }
190 
191 /*
192  * Initialize the channel set to scan based on the available channels
193  * and the current PHY mode.
194  */
195 static void
196 ieee80211_reset_scan(ieee80211com_t *ic)
197 {
198 	ieee80211_impl_t	*im = ic->ic_private;
199 
200 	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
201 		(void) memset(im->im_chan_scan, 0, sizeof (im->im_chan_scan));
202 		ieee80211_setbit(im->im_chan_scan,
203 			ieee80211_chan2ieee(ic, ic->ic_des_chan));
204 	} else {
205 		bcopy(ic->ic_chan_active, im->im_chan_scan,
206 			sizeof (ic->ic_chan_active));
207 	}
208 	ieee80211_dbg(IEEE80211_MSG_SCAN, "ieee80211_reset_scan(): "
209 		"start chan %u\n", ieee80211_chan2ieee(ic, ic->ic_curchan));
210 }
211 
212 /*
213  * Begin an active scan. Initialize the node cache. The scan
214  * begins on the next radio channel by calling ieee80211_next_scan().
215  * The actual scanning is not automated. The driver itself
216  * only handles setting the radio frequency and stepping through
217  * the channels.
218  */
219 void
220 ieee80211_begin_scan(ieee80211com_t *ic, boolean_t reset)
221 {
222 	IEEE80211_LOCK(ic);
223 
224 	if (ic->ic_opmode != IEEE80211_M_HOSTAP)
225 		ic->ic_flags |= IEEE80211_F_ASCAN;
226 	ieee80211_dbg(IEEE80211_MSG_SCAN,
227 		"begin %s scan in %s mode on channel %u\n",
228 		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive",
229 		ieee80211_phymode_name[ic->ic_curmode],
230 		ieee80211_chan2ieee(ic, ic->ic_curchan));
231 
232 	/*
233 	 * Clear scan state and flush any previously seen AP's.
234 	 */
235 	ieee80211_reset_scan(ic);
236 	if (reset)
237 		ieee80211_free_allnodes(&ic->ic_scan);
238 
239 	ic->ic_flags |= IEEE80211_F_SCAN;
240 	IEEE80211_UNLOCK(ic);
241 
242 	/* Scan the next channel. */
243 	ieee80211_next_scan(ic);
244 }
245 
246 /*
247  * Switch to the next channel marked for scanning.
248  * A driver is expected to first call ieee80211_begin_scan(),
249  * to initialize the node cache, then set the radio channel
250  * on the device. And then after a certain time has elapsed,
251  * call ieee80211_next_scan() to move to the next channel.
252  * Typically, a timeout routine is used to automate this process.
253  */
254 void
255 ieee80211_next_scan(ieee80211com_t *ic)
256 {
257 	ieee80211_impl_t *im = ic->ic_private;
258 	struct ieee80211_channel *chan;
259 
260 	IEEE80211_LOCK(ic);
261 	/*
262 	 * Insure any previous mgt frame timeouts don't fire.
263 	 * This assumes the driver does the right thing in
264 	 * flushing anything queued in the driver and below.
265 	 */
266 	im->im_mgt_timer = 0;
267 
268 	chan = ic->ic_curchan;
269 	do {
270 		if (++chan > &ic->ic_sup_channels[IEEE80211_CHAN_MAX])
271 			chan = &ic->ic_sup_channels[0];
272 		if (ieee80211_isset(im->im_chan_scan,
273 		    ieee80211_chan2ieee(ic, chan))) {
274 			ieee80211_clrbit(im->im_chan_scan,
275 				ieee80211_chan2ieee(ic, chan));
276 			ieee80211_dbg(IEEE80211_MSG_SCAN,
277 				"ieee80211_next_scan: chan %d->%d\n",
278 				ieee80211_chan2ieee(ic, ic->ic_curchan),
279 				ieee80211_chan2ieee(ic, chan));
280 			ic->ic_curchan = chan;
281 			/*
282 			 * drivers should do this as needed,
283 			 * for now maintain compatibility
284 			 */
285 			ic->ic_bss->in_rates =
286 				ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)];
287 			IEEE80211_UNLOCK(ic);
288 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
289 			return;
290 		}
291 	} while (chan != ic->ic_curchan);
292 	IEEE80211_UNLOCK(ic);
293 	ieee80211_end_scan(ic);
294 }
295 
296 /*
297  * Copy useful state from node obss into nbss.
298  */
299 static void
300 ieee80211_copy_bss(ieee80211_node_t *nbss, const ieee80211_node_t *obss)
301 {
302 	/* propagate useful state */
303 	nbss->in_authmode = obss->in_authmode;
304 	nbss->in_txpower = obss->in_txpower;
305 	nbss->in_vlan = obss->in_vlan;
306 }
307 
308 /*
309  * Setup the net80211 specific portion of an interface's softc, ic,
310  * for use in IBSS mode
311  */
312 void
313 ieee80211_create_ibss(ieee80211com_t *ic, struct ieee80211_channel *chan)
314 {
315 	ieee80211_impl_t *im = ic->ic_private;
316 	ieee80211_node_table_t *nt;
317 	ieee80211_node_t *in;
318 
319 	IEEE80211_LOCK_ASSERT(ic);
320 	ieee80211_dbg(IEEE80211_MSG_SCAN, "ieee80211_create_ibss: "
321 		"creating ibss\n");
322 
323 	/*
324 	 * Create the station/neighbor table.  Note that for adhoc
325 	 * mode we make the initial inactivity timer longer since
326 	 * we create nodes only through discovery and they typically
327 	 * are long-lived associations.
328 	 */
329 	nt = &ic->ic_sta;
330 	IEEE80211_NODE_LOCK(nt);
331 	nt->nt_name = "neighbor";
332 	nt->nt_inact_init = im->im_inact_run;
333 	IEEE80211_NODE_UNLOCK(nt);
334 
335 	in = ieee80211_alloc_node(ic, &ic->ic_sta, ic->ic_macaddr);
336 	if (in == NULL) {
337 		ieee80211_err("ieee80211_create_ibss(): alloc node failed\n");
338 		return;
339 	}
340 	IEEE80211_ADDR_COPY(in->in_bssid, ic->ic_macaddr);
341 	in->in_esslen = ic->ic_des_esslen;
342 	(void) memcpy(in->in_essid, ic->ic_des_essid, in->in_esslen);
343 	ieee80211_copy_bss(in, ic->ic_bss);
344 	in->in_intval = ic->ic_bintval;
345 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
346 		in->in_capinfo |= IEEE80211_CAPINFO_PRIVACY;
347 	if (ic->ic_phytype == IEEE80211_T_FH) {
348 		in->in_fhdwell = 200;
349 		in->in_fhindex = 1;
350 	}
351 	switch (ic->ic_opmode) {
352 	case IEEE80211_M_IBSS:
353 		ic->ic_flags |= IEEE80211_F_SIBSS;
354 		in->in_capinfo |= IEEE80211_CAPINFO_IBSS;
355 		if (ic->ic_flags & IEEE80211_F_DESBSSID)
356 			IEEE80211_ADDR_COPY(in->in_bssid, ic->ic_des_bssid);
357 		else
358 			in->in_bssid[0] |= 0x02;	/* local bit for IBSS */
359 		break;
360 	case IEEE80211_M_AHDEMO:
361 		if (ic->ic_flags & IEEE80211_F_DESBSSID)
362 			IEEE80211_ADDR_COPY(in->in_bssid, ic->ic_des_bssid);
363 		else
364 			(void) memset(in->in_bssid, 0, IEEE80211_ADDR_LEN);
365 		break;
366 	default:
367 		ieee80211_err("ieee80211_create_ibss(): "
368 			"wrong opmode %u to creat IBSS, abort\n",
369 			ic->ic_opmode);
370 		ieee80211_free_node(in);
371 		return;
372 	}
373 
374 	/*
375 	 * Fix the channel and related attributes.
376 	 */
377 	ieee80211_node_setchan(ic, in, chan);
378 	ic->ic_curchan = chan;
379 	ic->ic_curmode = ieee80211_chan2mode(ic, chan);
380 	/*
381 	 * Do mode-specific rate setup.
382 	 */
383 	ieee80211_setbasicrates(&in->in_rates, ic->ic_curmode);
384 	IEEE80211_UNLOCK(ic);
385 	ieee80211_sta_join(ic, ieee80211_ref_node(in));
386 	IEEE80211_LOCK(ic);
387 }
388 
389 void
390 ieee80211_reset_bss(ieee80211com_t *ic)
391 {
392 	ieee80211_node_t *in;
393 	ieee80211_node_t *obss;
394 
395 	in = ieee80211_alloc_node(ic, &ic->ic_scan, ic->ic_macaddr);
396 	ASSERT(in != NULL);
397 	obss = ic->ic_bss;
398 	ic->ic_bss = ieee80211_ref_node(in);
399 	if (obss != NULL) {
400 		ieee80211_copy_bss(in, obss);
401 		in->in_intval = ic->ic_bintval;
402 		ieee80211_free_node(obss);
403 	}
404 }
405 
406 static int
407 ieee80211_match_bss(ieee80211com_t *ic, ieee80211_node_t *in)
408 {
409 	uint8_t rate;
410 	int fail;
411 
412 	fail = 0;
413 	if (ieee80211_isclr(ic->ic_chan_active,
414 	    ieee80211_chan2ieee(ic, in->in_chan))) {
415 		fail |= IEEE80211_BADCHAN;
416 	}
417 	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
418 	    in->in_chan != ic->ic_des_chan) {
419 		fail |= IEEE80211_BADCHAN;
420 	}
421 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
422 		if (!(in->in_capinfo & IEEE80211_CAPINFO_IBSS))
423 			fail |= IEEE80211_BADOPMODE;
424 	} else {
425 		if (!(in->in_capinfo & IEEE80211_CAPINFO_ESS))
426 			fail |= IEEE80211_BADOPMODE;
427 	}
428 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
429 		if (!(in->in_capinfo & IEEE80211_CAPINFO_PRIVACY))
430 			fail |= IEEE80211_BADPRIVACY;
431 	} else {
432 		if (in->in_capinfo & IEEE80211_CAPINFO_PRIVACY)
433 			fail |= IEEE80211_BADPRIVACY;
434 	}
435 	rate = ieee80211_fix_rate(in, IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
436 	if (rate & IEEE80211_RATE_BASIC)
437 		fail |= IEEE80211_BADRATE;
438 	if (ic->ic_des_esslen != 0 &&
439 	    (in->in_esslen != ic->ic_des_esslen ||
440 	    memcmp(in->in_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0)) {
441 		fail |= IEEE80211_BADESSID;
442 	}
443 	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
444 	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, in->in_bssid)) {
445 		fail |= IEEE80211_BADBSSID;
446 	}
447 	if (in->in_fails >= IEEE80211_STA_FAILS_MAX)
448 		fail |= IEEE80211_NODEFAIL;
449 
450 	return (fail);
451 }
452 
453 #define	IEEE80211_MAXRATE(_rs) \
454 	((_rs).ir_rates[(_rs).ir_nrates - 1] & IEEE80211_RATE_VAL)
455 
456 /*
457  * Compare the capabilities of node a with node b and decide which is
458  * more desirable (return b if b is considered better than a).  Note
459  * that we assume compatibility/usability has already been checked
460  * so we don't need to (e.g. validate whether privacy is supported).
461  * Used to select the best scan candidate for association in a BSS.
462  *
463  * Return desired node
464  */
465 static ieee80211_node_t *
466 ieee80211_node_compare(ieee80211com_t *ic, ieee80211_node_t *a,
467     ieee80211_node_t *b)
468 {
469 	uint8_t maxa;
470 	uint8_t maxb;
471 	uint8_t rssia;
472 	uint8_t rssib;
473 
474 	/* privacy support preferred */
475 	if ((a->in_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
476 	    !(b->in_capinfo & IEEE80211_CAPINFO_PRIVACY)) {
477 		return (a);
478 	}
479 	if (!(a->in_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
480 	    (b->in_capinfo & IEEE80211_CAPINFO_PRIVACY)) {
481 		return (b);
482 	}
483 
484 	/* compare count of previous failures */
485 	if (b->in_fails != a->in_fails)
486 		return ((a->in_fails > b->in_fails) ? b : a);
487 
488 	rssia = ic->ic_node_getrssi(a);
489 	rssib = ic->ic_node_getrssi(b);
490 	if (ABS(rssib - rssia) < IEEE80211_RSSI_CMP_THRESHOLD) {
491 		/* best/max rate preferred if signal level close enough */
492 		maxa = IEEE80211_MAXRATE(a->in_rates);
493 		maxb = IEEE80211_MAXRATE(b->in_rates);
494 		if (maxa != maxb)
495 			return ((maxb > maxa) ? b : a);
496 		/* for now just prefer 5Ghz band to all other bands */
497 		if (IEEE80211_IS_CHAN_5GHZ(a->in_chan) &&
498 		    !IEEE80211_IS_CHAN_5GHZ(b->in_chan)) {
499 			return (a);
500 		}
501 		if (!IEEE80211_IS_CHAN_5GHZ(a->in_chan) &&
502 		    IEEE80211_IS_CHAN_5GHZ(b->in_chan)) {
503 			return (b);
504 		}
505 	}
506 	/* all things being equal, compare signal level */
507 	return ((rssib > rssia) ? b : a);
508 }
509 
510 /*
511  * Mark an ongoing scan stopped.
512  */
513 void
514 ieee80211_cancel_scan(ieee80211com_t *ic)
515 {
516 	IEEE80211_LOCK(ic);
517 	ieee80211_dbg(IEEE80211_MSG_SCAN, "ieee80211_cancel_scan()"
518 		"end %s scan\n",
519 		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive");
520 	ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
521 	cv_broadcast(&((ieee80211_impl_t *)ic->ic_private)->im_scan_cv);
522 	IEEE80211_UNLOCK(ic);
523 }
524 
525 /*
526  * Complete a scan of potential channels. It is called by
527  * ieee80211_next_scan() when the state machine has performed
528  * a full cycle of scaning on all available radio channels.
529  * ieee80211_end_scan() will inspect the node cache for suitable
530  * APs found during scaning, and associate with one, should
531  * the parameters of the node match those of the configuration
532  * requested from userland.
533  */
534 void
535 ieee80211_end_scan(ieee80211com_t *ic)
536 {
537 	ieee80211_node_table_t *nt = &ic->ic_scan;
538 	ieee80211_node_t *in;
539 	ieee80211_node_t *selbs;
540 
541 	ieee80211_cancel_scan(ic);
542 	/* notify SCAN done */
543 	ieee80211_notify(ic, EVENT_SCAN_RESULTS);
544 	IEEE80211_LOCK(ic);
545 
546 	/*
547 	 * Automatic sequencing; look for a candidate and
548 	 * if found join the network.
549 	 */
550 	/* NB: unlocked read should be ok */
551 	in = list_head(&nt->nt_node);
552 	if (in == NULL && (ic->ic_flags & IEEE80211_F_WPA) == 0) {
553 		ieee80211_dbg(IEEE80211_MSG_SCAN, "ieee80211_end_scan: "
554 			"no scan candidate\n");
555 	notfound:
556 		if (ic->ic_opmode == IEEE80211_M_IBSS &&
557 		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
558 		    ic->ic_des_esslen != 0) {
559 			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
560 			IEEE80211_UNLOCK(ic);
561 			return;
562 		}
563 
564 		/*
565 		 * Reset the list of channels to scan and start again.
566 		 */
567 		ieee80211_reset_scan(ic);
568 		ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN;
569 		IEEE80211_UNLOCK(ic);
570 
571 		ieee80211_next_scan(ic);
572 		return;
573 	}
574 
575 	if (ic->ic_flags & IEEE80211_F_SCANONLY ||
576 	    ic->ic_flags & IEEE80211_F_WPA) {	/* scan only */
577 		ic->ic_flags &= ~IEEE80211_F_SCANONLY;
578 		IEEE80211_UNLOCK(ic);
579 		ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
580 		return;
581 	}
582 
583 	selbs = NULL;
584 	IEEE80211_NODE_LOCK(nt);
585 	while (in != NULL) {
586 		if (in->in_fails >= IEEE80211_STA_FAILS_MAX) {
587 			ieee80211_node_t *tmpin = in;
588 
589 			/*
590 			 * The configuration of the access points may change
591 			 * during my scan.  So delete the entry for the AP
592 			 * and retry to associate if there is another beacon.
593 			 */
594 			in = list_next(&nt->nt_node, tmpin);
595 			ieee80211_node_reclaim(nt, tmpin);
596 			continue;
597 		}
598 		/*
599 		 * It's possible at some special moments, the in_chan will
600 		 * be none. Need to skip the null node.
601 		 */
602 		if (in->in_chan == IEEE80211_CHAN_ANYC) {
603 			in = list_next(&nt->nt_node, in);
604 			continue;
605 		}
606 		if (ieee80211_match_bss(ic, in) == 0) {
607 			if (selbs == NULL)
608 				selbs = in;
609 			else
610 				selbs = ieee80211_node_compare(ic, selbs, in);
611 		}
612 		in = list_next(&nt->nt_node, in);
613 	}
614 	if (selbs != NULL)	/* grab ref while dropping lock */
615 		(void) ieee80211_ref_node(selbs);
616 	IEEE80211_NODE_UNLOCK(nt);
617 	if (selbs == NULL)
618 		goto notfound;
619 	IEEE80211_UNLOCK(ic);
620 	ieee80211_sta_join(ic, selbs);
621 }
622 
623 
624 /*
625  * Handle 802.11 ad hoc network merge.  The convention, set by the
626  * Wireless Ethernet Compatibility Alliance (WECA), is that an 802.11
627  * station will change its BSSID to match the "oldest" 802.11 ad hoc
628  * network, on the same channel, that has the station's desired SSID.
629  * The "oldest" 802.11 network sends beacons with the greatest TSF
630  * timestamp.
631  * The caller is assumed to validate TSF's before attempting a merge.
632  *
633  * Return B_TRUE if the BSSID changed, B_FALSE otherwise.
634  */
635 boolean_t
636 ieee80211_ibss_merge(ieee80211_node_t *in)
637 {
638 	ieee80211com_t *ic = in->in_ic;
639 
640 	if (in == ic->ic_bss ||
641 	    IEEE80211_ADDR_EQ(in->in_bssid, ic->ic_bss->in_bssid)) {
642 		/* unchanged, nothing to do */
643 		return (B_FALSE);
644 	}
645 	if (ieee80211_match_bss(ic, in) != 0) {	/* capabilities mismatch */
646 		ieee80211_dbg(IEEE80211_MSG_ASSOC, "ieee80211_ibss_merge: "
647 			" merge failed, capabilities mismatch\n");
648 		return (B_FALSE);
649 	}
650 	ieee80211_dbg(IEEE80211_MSG_ASSOC, "ieee80211_ibss_merge: "
651 		"new bssid %s: %s preamble, %s slot time%s\n",
652 		ieee80211_macaddr_sprintf(in->in_bssid),
653 		(ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long",
654 		(ic->ic_flags & IEEE80211_F_SHSLOT) ? "short" : "long",
655 		(ic->ic_flags&IEEE80211_F_USEPROT) ? ", protection" : "");
656 	ieee80211_sta_join(ic, ieee80211_ref_node(in));
657 	return (B_TRUE);
658 }
659 
660 /*
661  * Join the specified IBSS/BSS network.  The node is assumed to
662  * be passed in with a held reference.
663  */
664 void
665 ieee80211_sta_join(ieee80211com_t *ic, ieee80211_node_t *selbs)
666 {
667 	ieee80211_impl_t *im = ic->ic_private;
668 	ieee80211_node_t *obss;
669 
670 	IEEE80211_LOCK(ic);
671 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
672 		ieee80211_node_table_t *nt;
673 
674 		/*
675 		 * Delete unusable rates; we've already checked
676 		 * that the negotiated rate set is acceptable.
677 		 */
678 		(void) ieee80211_fix_rate(selbs, IEEE80211_F_DODEL);
679 		/*
680 		 * Fillin the neighbor table
681 		 */
682 		nt = &ic->ic_sta;
683 		IEEE80211_NODE_LOCK(nt);
684 		nt->nt_name = "neighbor";
685 		nt->nt_inact_init = im->im_inact_run;
686 		IEEE80211_NODE_UNLOCK(nt);
687 	}
688 
689 	/*
690 	 * Committed to selbs, setup state.
691 	 */
692 	obss = ic->ic_bss;
693 	ic->ic_bss = selbs;	/* caller assumed to bump refcnt */
694 	if (obss != NULL) {
695 		ieee80211_copy_bss(selbs, obss);
696 		ieee80211_free_node(obss);
697 	}
698 	ic->ic_curmode = ieee80211_chan2mode(ic, selbs->in_chan);
699 	ic->ic_curchan = selbs->in_chan;
700 	/*
701 	 * Set the erp state (mostly the slot time) to deal with
702 	 * the auto-select case; this should be redundant if the
703 	 * mode is locked.
704 	 */
705 	ieee80211_reset_erp(ic);
706 
707 	IEEE80211_UNLOCK(ic);
708 	if (ic->ic_opmode == IEEE80211_M_STA)
709 		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
710 	else
711 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
712 }
713 
714 /*
715  * Leave the specified IBSS/BSS network.  The node is assumed to
716  * be passed in with a held reference.
717  */
718 void
719 ieee80211_sta_leave(ieee80211com_t *ic, ieee80211_node_t *in)
720 {
721 	IEEE80211_LOCK(ic);
722 	ic->ic_node_cleanup(in);
723 	ieee80211_notify_node_leave(ic, in);
724 	IEEE80211_UNLOCK(ic);
725 }
726 
727 /*
728  * Allocate a node. This is the default callback function for
729  * ic_node_alloc. This function may be overridden by the driver
730  * to allocate device specific node structure.
731  */
732 /* ARGSUSED */
733 static ieee80211_node_t *
734 ieee80211_node_alloc(ieee80211com_t *ic)
735 {
736 	return (kmem_zalloc(sizeof (ieee80211_node_t), KM_SLEEP));
737 }
738 
739 /*
740  * Cleanup a node, free any memory associated with the node.
741  * This is the default callback function for ic_node_cleanup
742  * and may be overridden by the driver.
743  */
744 static void
745 ieee80211_node_cleanup(ieee80211_node_t *in)
746 {
747 	in->in_associd = 0;
748 	in->in_rssi = 0;
749 	in->in_rstamp = 0;
750 	if (in->in_challenge != NULL) {
751 		kmem_free(in->in_challenge, IEEE80211_CHALLENGE_LEN);
752 		in->in_challenge = NULL;
753 	}
754 	if (in->in_rxfrag != NULL) {
755 		freemsg(in->in_rxfrag);
756 		in->in_rxfrag = NULL;
757 	}
758 }
759 
760 /*
761  * Free a node. This is the default callback function for ic_node_free
762  * and may be overridden by the driver to free memory used by device
763  * specific node structure
764  */
765 static void
766 ieee80211_node_free(ieee80211_node_t *in)
767 {
768 	ieee80211com_t *ic = in->in_ic;
769 
770 	ic->ic_node_cleanup(in);
771 	if (in->in_wpa_ie != NULL)
772 		ieee80211_free(in->in_wpa_ie);
773 	kmem_free(in, sizeof (ieee80211_node_t));
774 }
775 
776 /*
777  * Get a node current RSSI value. This is the default callback function
778  * for ic_node_getrssi and may be overridden by the driver to provide
779  * device specific RSSI calculation algorithm.
780  */
781 static uint8_t
782 ieee80211_node_getrssi(const ieee80211_node_t *in)
783 {
784 	return (in->in_rssi);
785 }
786 
787 /* Free fragment if not needed anymore */
788 static void
789 node_cleanfrag(ieee80211_node_t *in)
790 {
791 	clock_t ticks;
792 
793 	ticks = ddi_get_lbolt();
794 	if (in->in_rxfrag != NULL && ticks > (in->in_rxfragstamp + hz)) {
795 		freemsg(in->in_rxfrag);
796 		in->in_rxfrag = NULL;
797 	}
798 }
799 
800 /*
801  * Setup a node. Initialize the node with specified macaddr. Associate
802  * with the interface softc, ic, and add it to the specified node
803  * database.
804  */
805 static void
806 ieee80211_setup_node(ieee80211com_t *ic, ieee80211_node_table_t *nt,
807     ieee80211_node_t *in, const uint8_t *macaddr)
808 {
809 	int32_t hash;
810 
811 	ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_setup_node(): "
812 		"%p<%s> in %s table\n", in,
813 		ieee80211_macaddr_sprintf(macaddr),
814 		(nt != NULL) ? nt->nt_name : "NULL");
815 
816 	in->in_ic = ic;
817 	IEEE80211_ADDR_COPY(in->in_macaddr, macaddr);
818 	hash = ieee80211_node_hash(macaddr);
819 	ieee80211_node_initref(in);		/* mark referenced */
820 	in->in_authmode = IEEE80211_AUTH_OPEN;
821 	in->in_txpower = ic->ic_txpowlimit;	/* max power */
822 	in->in_chan = IEEE80211_CHAN_ANYC;
823 	in->in_inact_reload = IEEE80211_INACT_INIT;
824 	in->in_inact = in->in_inact_reload;
825 	ieee80211_crypto_resetkey(ic, &in->in_ucastkey, IEEE80211_KEYIX_NONE);
826 
827 	if (nt != NULL) {
828 		IEEE80211_NODE_LOCK(nt);
829 		list_insert_tail(&nt->nt_node, in);
830 		list_insert_tail(&nt->nt_hash[hash], in);
831 		in->in_table = nt;
832 		in->in_inact_reload = nt->nt_inact_init;
833 		IEEE80211_NODE_UNLOCK(nt);
834 	}
835 }
836 
837 /*
838  * Allocates and initialize a node with specified MAC address.
839  * Associate the node with the interface ic. If the allocation
840  * is successful, the node structure is initialized by
841  * ieee80211_setup_node(); otherwise, NULL is returned
842  */
843 ieee80211_node_t *
844 ieee80211_alloc_node(ieee80211com_t *ic, ieee80211_node_table_t *nt,
845     const uint8_t *macaddr)
846 {
847 	ieee80211_node_t *in;
848 
849 	in = ic->ic_node_alloc(ic);
850 	if (in != NULL)
851 		ieee80211_setup_node(ic, nt, in, macaddr);
852 	return (in);
853 }
854 
855 /*
856  * Craft a temporary node suitable for sending a management frame
857  * to the specified station.  We craft only as much state as we
858  * need to do the work since the node will be immediately reclaimed
859  * once the send completes.
860  */
861 ieee80211_node_t *
862 ieee80211_tmp_node(ieee80211com_t *ic, const uint8_t *macaddr)
863 {
864 	ieee80211_node_t *in;
865 
866 	in = ic->ic_node_alloc(ic);
867 	if (in != NULL) {
868 		ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_tmp_node: "
869 			"%p<%s>\n", in, ieee80211_macaddr_sprintf(macaddr));
870 
871 		IEEE80211_ADDR_COPY(in->in_macaddr, macaddr);
872 		IEEE80211_ADDR_COPY(in->in_bssid, ic->ic_bss->in_bssid);
873 		ieee80211_node_initref(in);		/* mark referenced */
874 		in->in_txpower = ic->ic_bss->in_txpower;
875 		/* NB: required by ieee80211_fix_rate */
876 		ieee80211_node_setchan(ic, in, ic->ic_bss->in_chan);
877 		ieee80211_crypto_resetkey(ic, &in->in_ucastkey,
878 			IEEE80211_KEYIX_NONE);
879 
880 		in->in_table = NULL;		/* NB: pedantic */
881 		in->in_ic = ic;
882 	}
883 
884 	return (in);
885 }
886 
887 /*
888  * ieee80211_dup_bss() is similar to ieee80211_alloc_node(),
889  * but is instead used to create a node database entry for
890  * the specified BSSID. If the allocation is successful, the
891  * node is initialized,  otherwise, NULL is returned.
892  */
893 ieee80211_node_t *
894 ieee80211_dup_bss(ieee80211_node_table_t *nt, const uint8_t *macaddr)
895 {
896 	ieee80211com_t *ic = nt->nt_ic;
897 	ieee80211_node_t *in;
898 
899 	in = ieee80211_alloc_node(ic, nt, macaddr);
900 	if (in != NULL) {
901 		/*
902 		 * Inherit from ic_bss.
903 		 */
904 		ieee80211_copy_bss(in, ic->ic_bss);
905 		IEEE80211_ADDR_COPY(in->in_bssid, ic->ic_bss->in_bssid);
906 		ieee80211_node_setchan(ic, in, ic->ic_bss->in_chan);
907 	}
908 
909 	return (in);
910 }
911 
912 /*
913  * Iterate through the node table, searching for a node entry which
914  * matches macaddr. If the entry is found, its reference count is
915  * incremented, and a pointer to the node is returned; otherwise,
916  * NULL will be returned.
917  * The node table lock is acquired by the caller.
918  */
919 static ieee80211_node_t *
920 ieee80211_find_node_locked(ieee80211_node_table_t *nt, const uint8_t *macaddr)
921 {
922 	ieee80211_node_t *in;
923 	int hash;
924 
925 	ASSERT(IEEE80211_NODE_IS_LOCKED(nt));
926 
927 	hash = ieee80211_node_hash(macaddr);
928 	in = list_head(&nt->nt_hash[hash]);
929 	while (in != NULL) {
930 		if (IEEE80211_ADDR_EQ(in->in_macaddr, macaddr))
931 			return (ieee80211_ref_node(in)); /* mark referenced */
932 		in = list_next(&nt->nt_hash[hash], in);
933 	}
934 	return (NULL);
935 }
936 
937 /*
938  * Iterate through the node table, searching for a node entry
939  * which match specified mac address.
940  * Return NULL if no matching node found.
941  */
942 ieee80211_node_t *
943 ieee80211_find_node(ieee80211_node_table_t *nt, const uint8_t *macaddr)
944 {
945 	ieee80211_node_t *in;
946 
947 	IEEE80211_NODE_LOCK(nt);
948 	in = ieee80211_find_node_locked(nt, macaddr);
949 	IEEE80211_NODE_UNLOCK(nt);
950 	return (in);
951 }
952 
953 /*
954  * Like find but search based on the ssid too.
955  */
956 ieee80211_node_t *
957 ieee80211_find_node_with_ssid(ieee80211_node_table_t *nt,
958 	const uint8_t *macaddr, uint32_t ssidlen, const uint8_t *ssid)
959 {
960 	ieee80211_node_t *in;
961 	int hash;
962 
963 	IEEE80211_NODE_LOCK(nt);
964 
965 	hash = ieee80211_node_hash(macaddr);
966 	in = list_head(&nt->nt_hash[hash]);
967 	while (in != NULL) {
968 		if (IEEE80211_ADDR_EQ(in->in_macaddr, macaddr) &&
969 		    in->in_esslen == ssidlen &&
970 		    memcmp(in->in_essid, ssid, ssidlen) == 0)
971 			break;
972 		in = list_next(&nt->nt_hash[hash], in);
973 	}
974 	if (in != NULL) {
975 		(void) ieee80211_ref_node(in); /* mark referenced */
976 	}
977 	IEEE80211_NODE_UNLOCK(nt);
978 
979 	return (in);
980 }
981 
982 /*
983  * Fake up a node; this handles node discovery in adhoc mode.
984  * Note that for the driver's benefit we treat this like an
985  * association so the driver has an opportunity to setup it's
986  * private state.
987  */
988 ieee80211_node_t *
989 ieee80211_fakeup_adhoc_node(ieee80211_node_table_t *nt, const uint8_t *macaddr)
990 {
991 	ieee80211com_t *ic = nt->nt_ic;
992 	ieee80211_node_t *in;
993 
994 	ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_fakeup_adhoc_node: "
995 		"mac<%s>\n", ieee80211_macaddr_sprintf(macaddr));
996 	in = ieee80211_dup_bss(nt, macaddr);
997 	if (in != NULL) {
998 		/* no rate negotiation; just dup */
999 		in->in_rates = ic->ic_bss->in_rates;
1000 		if (ic->ic_node_newassoc != NULL)
1001 			ic->ic_node_newassoc(in, 1);
1002 		ieee80211_node_authorize(in);
1003 	}
1004 	return (in);
1005 }
1006 
1007 static void
1008 ieee80211_saveie(uint8_t **iep, const uint8_t *ie)
1009 {
1010 	uint_t ielen = ie[1]+2;
1011 	/*
1012 	 * Record information element for later use.
1013 	 */
1014 	if (*iep == NULL || (*iep)[1] != ie[1]) {
1015 		if (*iep != NULL)
1016 			ieee80211_free(*iep);
1017 		*iep = ieee80211_malloc(ielen);
1018 	}
1019 	if (*iep != NULL)
1020 		(void) memcpy(*iep, ie, ielen);
1021 }
1022 
1023 static void
1024 saveie(uint8_t **iep, const uint8_t *ie)
1025 {
1026 	if (ie == NULL)
1027 		*iep = NULL;
1028 	else
1029 		ieee80211_saveie(iep, ie);
1030 }
1031 
1032 /*
1033  * Process a beacon or probe response frame.
1034  */
1035 void
1036 ieee80211_add_scan(ieee80211com_t *ic, const struct ieee80211_scanparams *sp,
1037     const struct ieee80211_frame *wh, int subtype, int rssi, int rstamp)
1038 {
1039 	ieee80211_node_table_t *nt = &ic->ic_scan;
1040 	ieee80211_node_t *in;
1041 	boolean_t newnode = B_FALSE;
1042 
1043 	in = ieee80211_find_node(nt, wh->i_addr2);
1044 	if (in == NULL) {
1045 		/*
1046 		 * Create a new entry.
1047 		 */
1048 		in = ieee80211_alloc_node(ic, nt, wh->i_addr2);
1049 		if (in == NULL) {
1050 			ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_add_scan: "
1051 				"alloc node failed\n");
1052 			return;
1053 		}
1054 		/*
1055 		 * inherit from ic_bss.
1056 		 */
1057 		ieee80211_copy_bss(in, ic->ic_bss);
1058 		ieee80211_node_setchan(ic, in, ic->ic_curchan);
1059 		newnode = B_TRUE;
1060 	}
1061 
1062 	/* ap beaconing multiple ssid w/ same bssid */
1063 
1064 	/*
1065 	 * sp->ssid[0] - element ID
1066 	 * sp->ssid[1] - length
1067 	 * sp->ssid[2]... - ssid
1068 	 */
1069 	if (sp->ssid[1] != 0 &&
1070 	    subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP ||
1071 	    in->in_esslen == 0) {
1072 		in->in_esslen = sp->ssid[1];
1073 		bzero(in->in_essid, sizeof (in->in_essid));
1074 		bcopy(sp->ssid + 2, in->in_essid, sp->ssid[1]);
1075 	}
1076 	IEEE80211_ADDR_COPY(in->in_bssid, wh->i_addr3);
1077 	in->in_rssi = (uint8_t)rssi;
1078 	in->in_rstamp = rstamp;
1079 	bcopy(sp->tstamp, in->in_tstamp.data, sizeof (in->in_tstamp));
1080 	in->in_intval = sp->bintval;
1081 	in->in_capinfo = sp->capinfo;
1082 	in->in_chan = &ic->ic_sup_channels[sp->chan];
1083 	in->in_phytype = sp->phytype;
1084 	in->in_fhdwell = sp->fhdwell;
1085 	in->in_fhindex = sp->fhindex;
1086 	in->in_erp = sp->erp;
1087 	if (sp->tim != NULL) {
1088 		struct ieee80211_tim_ie *ie;
1089 
1090 		ie = (struct ieee80211_tim_ie *)sp->tim;
1091 		in->in_dtim_count = ie->tim_count;
1092 		in->in_dtim_period = ie->tim_period;
1093 	}
1094 	/*
1095 	 * Record the byte offset from the mac header to
1096 	 * the start of the TIM information element for
1097 	 * use by hardware and/or to speedup software
1098 	 * processing of beacon frames.
1099 	 */
1100 	in->in_tim_off = sp->timoff;
1101 	/*
1102 	 * Record optional information elements that might be
1103 	 * used by applications or drivers.
1104 	 */
1105 	saveie(&in->in_wpa_ie, sp->wpa);
1106 
1107 	/* NB: must be after in_chan is setup */
1108 	(void) ieee80211_setup_rates(in, sp->rates, sp->xrates,
1109 		IEEE80211_F_DOSORT);
1110 
1111 	if (!newnode)
1112 		ieee80211_free_node(in);
1113 }
1114 
1115 /*
1116  * Initialize/update an ad-hoc node with contents from a received
1117  * beacon frame.
1118  */
1119 void
1120 ieee80211_init_neighbor(ieee80211_node_t *in, const struct ieee80211_frame *wh,
1121     const struct ieee80211_scanparams *sp)
1122 {
1123 	in->in_esslen = sp->ssid[1];
1124 	(void) memcpy(in->in_essid, sp->ssid + 2, sp->ssid[1]);
1125 	IEEE80211_ADDR_COPY(in->in_bssid, wh->i_addr3);
1126 	(void) memcpy(in->in_tstamp.data, sp->tstamp, sizeof (in->in_tstamp));
1127 	in->in_intval = sp->bintval;
1128 	in->in_capinfo = sp->capinfo;
1129 	in->in_chan = in->in_ic->ic_curchan;
1130 	in->in_fhdwell = sp->fhdwell;
1131 	in->in_fhindex = sp->fhindex;
1132 	in->in_erp = sp->erp;
1133 	in->in_tim_off = sp->timoff;
1134 
1135 	/* NB: must be after in_chan is setup */
1136 	(void) ieee80211_setup_rates(in, sp->rates, sp->xrates,
1137 		IEEE80211_F_DOSORT);
1138 }
1139 
1140 /*
1141  * Do node discovery in adhoc mode on receipt of a beacon
1142  * or probe response frame.  Note that for the driver's
1143  * benefit we we treat this like an association so the
1144  * driver has an opportuinty to setup it's private state.
1145  */
1146 ieee80211_node_t *
1147 ieee80211_add_neighbor(ieee80211com_t *ic, const struct ieee80211_frame *wh,
1148     const struct ieee80211_scanparams *sp)
1149 {
1150 	ieee80211_node_t *in;
1151 
1152 	in = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);
1153 	if (in != NULL) {
1154 		ieee80211_init_neighbor(in, wh, sp);
1155 		if (ic->ic_node_newassoc != NULL)
1156 			ic->ic_node_newassoc(in, 1);
1157 	}
1158 	return (in);
1159 }
1160 
1161 #define	IEEE80211_IS_CTL(wh) \
1162 	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1163 
1164 /*
1165  * Locate the node for sender, track state, and then pass the
1166  * (referenced) node up to the 802.11 layer for its use.  We
1167  * are required to pass some node so we fall back to ic_bss
1168  * when this frame is from an unknown sender.  The 802.11 layer
1169  * knows this means the sender wasn't in the node table and
1170  * acts accordingly.
1171  */
1172 ieee80211_node_t *
1173 ieee80211_find_rxnode(ieee80211com_t *ic, const struct ieee80211_frame *wh)
1174 {
1175 	ieee80211_node_table_t *nt;
1176 	ieee80211_node_t *in;
1177 
1178 	/* may want scanned nodes in the neighbor table for adhoc */
1179 	if (ic->ic_opmode == IEEE80211_M_STA ||
1180 	    (ic->ic_flags & IEEE80211_F_SCAN)) {
1181 		nt = &ic->ic_scan;
1182 	} else {
1183 		nt = &ic->ic_sta;
1184 	}
1185 
1186 	IEEE80211_NODE_LOCK(nt);
1187 	if (IEEE80211_IS_CTL(wh))
1188 		in = ieee80211_find_node_locked(nt, wh->i_addr1);
1189 	else
1190 		in = ieee80211_find_node_locked(nt, wh->i_addr2);
1191 	IEEE80211_NODE_UNLOCK(nt);
1192 
1193 	if (in == NULL)
1194 		in = ieee80211_ref_node(ic->ic_bss);
1195 
1196 	return (in);
1197 }
1198 
1199 /*
1200  * Return a reference to the appropriate node for sending
1201  * a data frame.  This handles node discovery in adhoc networks.
1202  */
1203 ieee80211_node_t *
1204 ieee80211_find_txnode(ieee80211com_t *ic, const uint8_t *daddr)
1205 {
1206 	ieee80211_node_table_t *nt = &ic->ic_sta;
1207 	ieee80211_node_t *in;
1208 
1209 	/*
1210 	 * The destination address should be in the node table
1211 	 * unless this is a multicast/broadcast frame.  We can
1212 	 * also optimize station mode operation, all frames go
1213 	 * to the bss node.
1214 	 */
1215 	IEEE80211_NODE_LOCK(nt);
1216 	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(daddr))
1217 		in = ieee80211_ref_node(ic->ic_bss);
1218 	else
1219 		in = ieee80211_find_node_locked(nt, daddr);
1220 	IEEE80211_NODE_UNLOCK(nt);
1221 
1222 	if (in == NULL) {
1223 		if (ic->ic_opmode == IEEE80211_M_IBSS) {
1224 			/*
1225 			 * In adhoc mode cons up a node for the destination.
1226 			 * Note that we need an additional reference for the
1227 			 * caller to be consistent with
1228 			 * ieee80211_find_node_locked
1229 			 * can't hold lock across ieee80211_dup_bss 'cuz of
1230 			 * recursive locking
1231 			 */
1232 			in = ieee80211_fakeup_adhoc_node(nt, daddr);
1233 			if (in != NULL)
1234 				(void) ieee80211_ref_node(in);
1235 		} else {
1236 			ieee80211_dbg(IEEE80211_MSG_OUTPUT,
1237 				"ieee80211_find_txnode: "
1238 				"[%s] no node, discard frame\n",
1239 				ieee80211_macaddr_sprintf(daddr));
1240 		}
1241 	}
1242 	return (in);
1243 }
1244 
1245 /*
1246  * Remove a node from the node database entries and free memory
1247  * associated with the node. The node table lock is acquired by
1248  * the caller.
1249  */
1250 static void
1251 ieee80211_free_node_locked(ieee80211_node_t *in)
1252 {
1253 	ieee80211com_t *ic = in->in_ic;
1254 	ieee80211_node_table_t *nt = in->in_table;
1255 	int32_t hash;
1256 
1257 	if (nt != NULL) {
1258 		hash = ieee80211_node_hash(in->in_macaddr);
1259 		list_remove(&nt->nt_hash[hash], in);
1260 		list_remove(&nt->nt_node, in);
1261 	}
1262 	ic->ic_node_free(in);
1263 }
1264 
1265 /*
1266  * Remove a node from the node database entries and free any
1267  * memory associated with the node.
1268  * This method can be overridden in ieee80211_attach()
1269  */
1270 void
1271 ieee80211_free_node(ieee80211_node_t *in)
1272 {
1273 	ieee80211_node_table_t *nt = in->in_table;
1274 
1275 	if (nt != NULL)
1276 		IEEE80211_NODE_LOCK(nt);
1277 	if (ieee80211_node_decref_nv(in) == 0)
1278 		ieee80211_free_node_locked(in);
1279 	if (nt != NULL)
1280 		IEEE80211_NODE_UNLOCK(nt);
1281 }
1282 
1283 /*
1284  * Reclaim a node.  If this is the last reference count then
1285  * do the normal free work.  Otherwise remove it from the node
1286  * table and mark it gone by clearing the back-reference.
1287  */
1288 static void
1289 ieee80211_node_reclaim(ieee80211_node_table_t *nt, ieee80211_node_t *in)
1290 {
1291 	int32_t hash;
1292 
1293 	IEEE80211_NODE_LOCK_ASSERT(nt);
1294 	ieee80211_dbg(IEEE80211_MSG_NODE, "node_reclaim: "
1295 		" remove %p<%s> from %s table, refcnt %d\n",
1296 		in, ieee80211_macaddr_sprintf(in->in_macaddr), nt->nt_name,
1297 		ieee80211_node_refcnt(in));
1298 
1299 	if (ieee80211_node_decref_nv(in) != 0) {
1300 		/*
1301 		 * Clear any entry in the unicast key mapping table.
1302 		 * We need to do it here so rx lookups don't find it
1303 		 * in the mapping table even if it's not in the hash
1304 		 * table.  We cannot depend on the mapping table entry
1305 		 * being cleared because the node may not be free'd.
1306 		 */
1307 		hash = ieee80211_node_hash(in->in_macaddr);
1308 		list_remove(&nt->nt_hash[hash], in);
1309 		list_remove(&nt->nt_node, in);
1310 		in->in_table = NULL;
1311 	} else {
1312 		ieee80211_free_node_locked(in);
1313 	}
1314 }
1315 
1316 /*
1317  * Iterate through the node list and reclaim all node in the node table.
1318  * The node table lock is acquired by the caller
1319  */
1320 static void
1321 ieee80211_free_allnodes_locked(ieee80211_node_table_t *nt)
1322 {
1323 	ieee80211_node_t *in;
1324 
1325 	ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_free_allnodes_locked(): "
1326 		"free all nodes in %s table\n", nt->nt_name);
1327 
1328 	in = list_head(&nt->nt_node);
1329 	while (in != NULL) {
1330 		ieee80211_node_reclaim(nt, in);
1331 		in = list_head(&nt->nt_node);
1332 	}
1333 	ieee80211_reset_erp(nt->nt_ic);
1334 }
1335 
1336 /*
1337  * Iterate through the node list, calling ieee80211_node_reclaim() for
1338  * all nodes associated with the interface.
1339  */
1340 static void
1341 ieee80211_free_allnodes(ieee80211_node_table_t *nt)
1342 {
1343 	IEEE80211_NODE_LOCK(nt);
1344 	ieee80211_free_allnodes_locked(nt);
1345 	IEEE80211_NODE_UNLOCK(nt);
1346 }
1347 
1348 /*
1349  * Timeout entries in the scan cache. This is the timeout callback
1350  * function of node table ic_scan which is called when the inactivity
1351  * timer expires.
1352  */
1353 static void
1354 ieee80211_timeout_scan_candidates(ieee80211_node_table_t *nt)
1355 {
1356 	ieee80211com_t *ic = nt->nt_ic;
1357 	ieee80211_node_t *in;
1358 
1359 	IEEE80211_NODE_LOCK(nt);
1360 	in = ic->ic_bss;
1361 	node_cleanfrag(in);	/* Free fragment if not needed */
1362 	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1363 	IEEE80211_NODE_UNLOCK(nt);
1364 }
1365 
1366 /*
1367  * Timeout inactive stations and do related housekeeping.
1368  * Note that we cannot hold the node lock while sending a
1369  * frame as this would lead to a LOR.  Instead we use a
1370  * generation number to mark nodes that we've scanned and
1371  * drop the lock and restart a scan if we have to time out
1372  * a node.  Since we are single-threaded by virtue of
1373  * controlling the inactivity timer we can be sure this will
1374  * process each node only once.
1375  */
1376 static void
1377 ieee80211_timeout_stations(ieee80211_node_table_t *nt)
1378 {
1379 	ieee80211com_t *ic = nt->nt_ic;
1380 	ieee80211_impl_t *im = ic->ic_private;
1381 	ieee80211_node_t *in = NULL;
1382 	uint32_t gen;
1383 	boolean_t isadhoc;
1384 
1385 	IEEE80211_LOCK_ASSERT(ic);
1386 	isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS ||
1387 		ic->ic_opmode == IEEE80211_M_AHDEMO);
1388 	IEEE80211_SCAN_LOCK(nt);
1389 	gen = ++nt->nt_scangen;
1390 restart:
1391 	IEEE80211_NODE_LOCK(nt);
1392 	for (in = list_head(&nt->nt_node); in != NULL;
1393 		in = list_next(&nt->nt_node, in)) {
1394 		if (in->in_scangen == gen)	/* previously handled */
1395 			continue;
1396 		in->in_scangen = gen;
1397 		node_cleanfrag(in);	/* free fragment if not needed */
1398 
1399 		/*
1400 		 * Special case ourself; we may be idle for extended periods
1401 		 * of time and regardless reclaiming our state is wrong.
1402 		 */
1403 		if (in == ic->ic_bss)
1404 			continue;
1405 		in->in_inact--;
1406 		if (in->in_associd != 0 || isadhoc) {
1407 			/*
1408 			 * Probe the station before time it out.  We
1409 			 * send a null data frame which may not be
1410 			 * uinversally supported by drivers (need it
1411 			 * for ps-poll support so it should be...).
1412 			 */
1413 			if (0 < in->in_inact &&
1414 			    in->in_inact <= im->im_inact_probe) {
1415 				ieee80211_dbg(IEEE80211_MSG_NODE, "net80211: "
1416 					"probe station due to inactivity\n");
1417 				IEEE80211_NODE_UNLOCK(nt);
1418 				IEEE80211_UNLOCK(ic);
1419 				(void) ieee80211_send_nulldata(in);
1420 				IEEE80211_LOCK(ic);
1421 				goto restart;
1422 			}
1423 		}
1424 		if (in->in_inact <= 0) {
1425 			ieee80211_dbg(IEEE80211_MSG_NODE, "net80211: "
1426 				"station timed out due to inact (refcnt %u)\n",
1427 				ieee80211_node_refcnt(in));
1428 			/*
1429 			 * Send a deauthenticate frame and drop the station.
1430 			 * This is somewhat complicated due to reference counts
1431 			 * and locking.  At this point a station will typically
1432 			 * have a reference count of 1.  ieee80211_node_leave
1433 			 * will do a "free" of the node which will drop the
1434 			 * reference count.  But in the meantime a reference
1435 			 * wil be held by the deauth frame.  The actual reclaim
1436 			 * of the node will happen either after the tx is
1437 			 * completed or by ieee80211_node_leave.
1438 			 *
1439 			 * Separately we must drop the node lock before sending
1440 			 * in case the driver takes a lock, as this will result
1441 			 * in  LOR between the node lock and the driver lock.
1442 			 */
1443 			IEEE80211_NODE_UNLOCK(nt);
1444 			if (in->in_associd != 0) {
1445 				IEEE80211_UNLOCK(ic);
1446 				IEEE80211_SEND_MGMT(ic, in,
1447 					IEEE80211_FC0_SUBTYPE_DEAUTH,
1448 					IEEE80211_REASON_AUTH_EXPIRE);
1449 				IEEE80211_LOCK(ic);
1450 			}
1451 			ieee80211_node_leave(ic, in);
1452 			goto restart;
1453 		}
1454 	}
1455 	IEEE80211_NODE_UNLOCK(nt);
1456 
1457 	IEEE80211_SCAN_UNLOCK(nt);
1458 
1459 	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1460 }
1461 
1462 /*
1463  * Call the user-defined call back function for all nodes in
1464  * the node cache. The callback is invoked with the user-supplied
1465  * value and a pointer to the current node.
1466  */
1467 void
1468 ieee80211_iterate_nodes(ieee80211_node_table_t *nt, ieee80211_iter_func *f,
1469     void *arg)
1470 {
1471 	ieee80211_node_t *in;
1472 
1473 	IEEE80211_NODE_LOCK(nt);
1474 	in = list_head(&nt->nt_node);
1475 	while (in != NULL) {
1476 		(void) ieee80211_ref_node(in);
1477 		IEEE80211_NODE_UNLOCK(nt);
1478 		(*f)(arg, in);
1479 		ieee80211_free_node(in);
1480 		IEEE80211_NODE_LOCK(nt);
1481 		in = list_next(&nt->nt_node, in);
1482 	}
1483 	IEEE80211_NODE_UNLOCK(nt);
1484 }
1485 
1486 /*
1487  * Handle bookkeeping for station deauthentication/disassociation
1488  * when operating as an ap.
1489  */
1490 static void
1491 ieee80211_node_leave(ieee80211com_t *ic, ieee80211_node_t *in)
1492 {
1493 	ieee80211_node_table_t *nt = in->in_table;
1494 
1495 	ASSERT(ic->ic_opmode == IEEE80211_M_IBSS);
1496 
1497 	/*
1498 	 * Remove the node from any table it's recorded in and
1499 	 * drop the caller's reference.  Removal from the table
1500 	 * is important to insure the node is not reprocessed
1501 	 * for inactivity.
1502 	 */
1503 	if (nt != NULL) {
1504 		IEEE80211_NODE_LOCK(nt);
1505 		ieee80211_node_reclaim(nt, in);
1506 		IEEE80211_NODE_UNLOCK(nt);
1507 	} else {
1508 		ieee80211_free_node(in);
1509 	}
1510 }
1511 
1512 /*
1513  * Initialize a node table with specified name, inactivity timer value
1514  * and callback inactivity timeout function. Associate the node table
1515  * with interface softc, ic.
1516  */
1517 static void
1518 ieee80211_node_table_init(ieee80211com_t *ic, ieee80211_node_table_t *nt,
1519     const char *name, int inact, int keyixmax,
1520     void (*timeout)(ieee80211_node_table_t *))
1521 {
1522 	int i;
1523 
1524 	ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_node_table_init():"
1525 		"%s table, inact %d\n", name, inact);
1526 
1527 	nt->nt_ic = ic;
1528 	nt->nt_name = name;
1529 	nt->nt_inact_timer = 0;
1530 	nt->nt_inact_init = inact;
1531 	nt->nt_timeout = timeout;
1532 	nt->nt_keyixmax = keyixmax;
1533 	nt->nt_scangen = 1;
1534 	mutex_init(&nt->nt_scanlock, NULL, MUTEX_DRIVER, NULL);
1535 	mutex_init(&nt->nt_nodelock, NULL, MUTEX_DRIVER, NULL);
1536 
1537 	list_create(&nt->nt_node, sizeof (ieee80211_node_t),
1538 		offsetof(ieee80211_node_t, in_node));
1539 	for (i = 0; i < IEEE80211_NODE_HASHSIZE; i++) {
1540 		list_create(&nt->nt_hash[i], sizeof (ieee80211_node_t),
1541 			offsetof(ieee80211_node_t, in_hash));
1542 	}
1543 }
1544 
1545 /*
1546  * Reset a node table. Clean its inactivity timer and call
1547  * ieee80211_free_allnodes_locked() to free all nodes in the
1548  * node table.
1549  */
1550 void
1551 ieee80211_node_table_reset(ieee80211_node_table_t *nt)
1552 {
1553 	ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_node_table_reset(): "
1554 		"%s table\n", nt->nt_name);
1555 
1556 	IEEE80211_NODE_LOCK(nt);
1557 	nt->nt_inact_timer = 0;
1558 	ieee80211_free_allnodes_locked(nt);
1559 	IEEE80211_NODE_UNLOCK(nt);
1560 }
1561 
1562 /*
1563  * Destroy a node table. Free all nodes in the node table.
1564  * This function is usually called by node detach function.
1565  */
1566 static void
1567 ieee80211_node_table_cleanup(ieee80211_node_table_t *nt)
1568 {
1569 	ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_node_table_cleanup(): "
1570 	    "%s table\n", nt->nt_name);
1571 
1572 	IEEE80211_NODE_LOCK(nt);
1573 	ieee80211_free_allnodes_locked(nt);
1574 	IEEE80211_NODE_UNLOCK(nt);
1575 	mutex_destroy(&nt->nt_nodelock);
1576 	mutex_destroy(&nt->nt_scanlock);
1577 }
1578