xref: /titanic_50/usr/src/uts/common/io/net80211/net80211_node.c (revision 54925bf60766fbb4f1f2d7c843721406a7b7a3fb)
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 	ic->ic_phytype = selbs->in_phytype;
701 	/*
702 	 * Set the erp state (mostly the slot time) to deal with
703 	 * the auto-select case; this should be redundant if the
704 	 * mode is locked.
705 	 */
706 	ieee80211_reset_erp(ic);
707 
708 	IEEE80211_UNLOCK(ic);
709 	if (ic->ic_opmode == IEEE80211_M_STA)
710 		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
711 	else
712 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
713 }
714 
715 /*
716  * Leave the specified IBSS/BSS network.  The node is assumed to
717  * be passed in with a held reference.
718  */
719 void
720 ieee80211_sta_leave(ieee80211com_t *ic, ieee80211_node_t *in)
721 {
722 	IEEE80211_LOCK(ic);
723 	ic->ic_node_cleanup(in);
724 	ieee80211_notify_node_leave(ic, in);
725 	IEEE80211_UNLOCK(ic);
726 }
727 
728 /*
729  * Allocate a node. This is the default callback function for
730  * ic_node_alloc. This function may be overridden by the driver
731  * to allocate device specific node structure.
732  */
733 /* ARGSUSED */
734 static ieee80211_node_t *
735 ieee80211_node_alloc(ieee80211com_t *ic)
736 {
737 	return (kmem_zalloc(sizeof (ieee80211_node_t), KM_SLEEP));
738 }
739 
740 /*
741  * Cleanup a node, free any memory associated with the node.
742  * This is the default callback function for ic_node_cleanup
743  * and may be overridden by the driver.
744  */
745 static void
746 ieee80211_node_cleanup(ieee80211_node_t *in)
747 {
748 	in->in_associd = 0;
749 	in->in_rssi = 0;
750 	in->in_rstamp = 0;
751 	if (in->in_challenge != NULL) {
752 		kmem_free(in->in_challenge, IEEE80211_CHALLENGE_LEN);
753 		in->in_challenge = NULL;
754 	}
755 	if (in->in_rxfrag != NULL) {
756 		freemsg(in->in_rxfrag);
757 		in->in_rxfrag = NULL;
758 	}
759 }
760 
761 /*
762  * Free a node. This is the default callback function for ic_node_free
763  * and may be overridden by the driver to free memory used by device
764  * specific node structure
765  */
766 static void
767 ieee80211_node_free(ieee80211_node_t *in)
768 {
769 	ieee80211com_t *ic = in->in_ic;
770 
771 	ic->ic_node_cleanup(in);
772 	if (in->in_wpa_ie != NULL)
773 		ieee80211_free(in->in_wpa_ie);
774 	kmem_free(in, sizeof (ieee80211_node_t));
775 }
776 
777 /*
778  * Get a node current RSSI value. This is the default callback function
779  * for ic_node_getrssi and may be overridden by the driver to provide
780  * device specific RSSI calculation algorithm.
781  */
782 static uint8_t
783 ieee80211_node_getrssi(const ieee80211_node_t *in)
784 {
785 	return (in->in_rssi);
786 }
787 
788 /* Free fragment if not needed anymore */
789 static void
790 node_cleanfrag(ieee80211_node_t *in)
791 {
792 	clock_t ticks;
793 
794 	ticks = ddi_get_lbolt();
795 	if (in->in_rxfrag != NULL && ticks > (in->in_rxfragstamp + hz)) {
796 		freemsg(in->in_rxfrag);
797 		in->in_rxfrag = NULL;
798 	}
799 }
800 
801 /*
802  * Setup a node. Initialize the node with specified macaddr. Associate
803  * with the interface softc, ic, and add it to the specified node
804  * database.
805  */
806 static void
807 ieee80211_setup_node(ieee80211com_t *ic, ieee80211_node_table_t *nt,
808     ieee80211_node_t *in, const uint8_t *macaddr)
809 {
810 	int32_t hash;
811 
812 	ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_setup_node(): "
813 	    "%p<%s> in %s table\n", in,
814 	    ieee80211_macaddr_sprintf(macaddr),
815 	    (nt != NULL) ? nt->nt_name : "NULL");
816 
817 	in->in_ic = ic;
818 	IEEE80211_ADDR_COPY(in->in_macaddr, macaddr);
819 	hash = ieee80211_node_hash(macaddr);
820 	ieee80211_node_initref(in);		/* mark referenced */
821 	in->in_authmode = IEEE80211_AUTH_OPEN;
822 	in->in_txpower = ic->ic_txpowlimit;	/* max power */
823 	in->in_chan = IEEE80211_CHAN_ANYC;
824 	in->in_inact_reload = IEEE80211_INACT_INIT;
825 	in->in_inact = in->in_inact_reload;
826 	ieee80211_crypto_resetkey(ic, &in->in_ucastkey, IEEE80211_KEYIX_NONE);
827 
828 	if (nt != NULL) {
829 		IEEE80211_NODE_LOCK(nt);
830 		list_insert_tail(&nt->nt_node, in);
831 		list_insert_tail(&nt->nt_hash[hash], in);
832 		in->in_table = nt;
833 		in->in_inact_reload = nt->nt_inact_init;
834 		IEEE80211_NODE_UNLOCK(nt);
835 	}
836 }
837 
838 /*
839  * Allocates and initialize a node with specified MAC address.
840  * Associate the node with the interface ic. If the allocation
841  * is successful, the node structure is initialized by
842  * ieee80211_setup_node(); otherwise, NULL is returned
843  */
844 ieee80211_node_t *
845 ieee80211_alloc_node(ieee80211com_t *ic, ieee80211_node_table_t *nt,
846     const uint8_t *macaddr)
847 {
848 	ieee80211_node_t *in;
849 
850 	in = ic->ic_node_alloc(ic);
851 	if (in != NULL)
852 		ieee80211_setup_node(ic, nt, in, macaddr);
853 	return (in);
854 }
855 
856 /*
857  * Craft a temporary node suitable for sending a management frame
858  * to the specified station.  We craft only as much state as we
859  * need to do the work since the node will be immediately reclaimed
860  * once the send completes.
861  */
862 ieee80211_node_t *
863 ieee80211_tmp_node(ieee80211com_t *ic, const uint8_t *macaddr)
864 {
865 	ieee80211_node_t *in;
866 
867 	in = ic->ic_node_alloc(ic);
868 	if (in != NULL) {
869 		ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_tmp_node: "
870 		    "%p<%s>\n", in, ieee80211_macaddr_sprintf(macaddr));
871 
872 		IEEE80211_ADDR_COPY(in->in_macaddr, macaddr);
873 		IEEE80211_ADDR_COPY(in->in_bssid, ic->ic_bss->in_bssid);
874 		ieee80211_node_initref(in);		/* mark referenced */
875 		in->in_txpower = ic->ic_bss->in_txpower;
876 		/* NB: required by ieee80211_fix_rate */
877 		ieee80211_node_setchan(ic, in, ic->ic_bss->in_chan);
878 		ieee80211_crypto_resetkey(ic, &in->in_ucastkey,
879 		    IEEE80211_KEYIX_NONE);
880 
881 		in->in_table = NULL;		/* NB: pedantic */
882 		in->in_ic = ic;
883 	}
884 
885 	return (in);
886 }
887 
888 /*
889  * ieee80211_dup_bss() is similar to ieee80211_alloc_node(),
890  * but is instead used to create a node database entry for
891  * the specified BSSID. If the allocation is successful, the
892  * node is initialized,  otherwise, NULL is returned.
893  */
894 ieee80211_node_t *
895 ieee80211_dup_bss(ieee80211_node_table_t *nt, const uint8_t *macaddr)
896 {
897 	ieee80211com_t *ic = nt->nt_ic;
898 	ieee80211_node_t *in;
899 
900 	in = ieee80211_alloc_node(ic, nt, macaddr);
901 	if (in != NULL) {
902 		/*
903 		 * Inherit from ic_bss.
904 		 */
905 		ieee80211_copy_bss(in, ic->ic_bss);
906 		IEEE80211_ADDR_COPY(in->in_bssid, ic->ic_bss->in_bssid);
907 		ieee80211_node_setchan(ic, in, ic->ic_bss->in_chan);
908 	}
909 
910 	return (in);
911 }
912 
913 /*
914  * Iterate through the node table, searching for a node entry which
915  * matches macaddr. If the entry is found, its reference count is
916  * incremented, and a pointer to the node is returned; otherwise,
917  * NULL will be returned.
918  * The node table lock is acquired by the caller.
919  */
920 static ieee80211_node_t *
921 ieee80211_find_node_locked(ieee80211_node_table_t *nt, const uint8_t *macaddr)
922 {
923 	ieee80211_node_t *in;
924 	int hash;
925 
926 	ASSERT(IEEE80211_NODE_IS_LOCKED(nt));
927 
928 	hash = ieee80211_node_hash(macaddr);
929 	in = list_head(&nt->nt_hash[hash]);
930 	while (in != NULL) {
931 		if (IEEE80211_ADDR_EQ(in->in_macaddr, macaddr))
932 			return (ieee80211_ref_node(in)); /* mark referenced */
933 		in = list_next(&nt->nt_hash[hash], in);
934 	}
935 	return (NULL);
936 }
937 
938 /*
939  * Iterate through the node table, searching for a node entry
940  * which match specified mac address.
941  * Return NULL if no matching node found.
942  */
943 ieee80211_node_t *
944 ieee80211_find_node(ieee80211_node_table_t *nt, const uint8_t *macaddr)
945 {
946 	ieee80211_node_t *in;
947 
948 	IEEE80211_NODE_LOCK(nt);
949 	in = ieee80211_find_node_locked(nt, macaddr);
950 	IEEE80211_NODE_UNLOCK(nt);
951 	return (in);
952 }
953 
954 /*
955  * Like find but search based on the ssid too.
956  */
957 ieee80211_node_t *
958 ieee80211_find_node_with_ssid(ieee80211_node_table_t *nt,
959 	const uint8_t *macaddr, uint32_t ssidlen, const uint8_t *ssid)
960 {
961 	ieee80211_node_t *in;
962 	int hash;
963 
964 	IEEE80211_NODE_LOCK(nt);
965 
966 	hash = ieee80211_node_hash(macaddr);
967 	in = list_head(&nt->nt_hash[hash]);
968 	while (in != NULL) {
969 		if (IEEE80211_ADDR_EQ(in->in_macaddr, macaddr) &&
970 		    in->in_esslen == ssidlen &&
971 		    memcmp(in->in_essid, ssid, ssidlen) == 0)
972 			break;
973 		in = list_next(&nt->nt_hash[hash], in);
974 	}
975 	if (in != NULL) {
976 		(void) ieee80211_ref_node(in); /* mark referenced */
977 	}
978 	IEEE80211_NODE_UNLOCK(nt);
979 
980 	return (in);
981 }
982 
983 /*
984  * Fake up a node; this handles node discovery in adhoc mode.
985  * Note that for the driver's benefit we treat this like an
986  * association so the driver has an opportunity to setup it's
987  * private state.
988  */
989 ieee80211_node_t *
990 ieee80211_fakeup_adhoc_node(ieee80211_node_table_t *nt, const uint8_t *macaddr)
991 {
992 	ieee80211com_t *ic = nt->nt_ic;
993 	ieee80211_node_t *in;
994 
995 	ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_fakeup_adhoc_node: "
996 	    "mac<%s>\n", ieee80211_macaddr_sprintf(macaddr));
997 	in = ieee80211_dup_bss(nt, macaddr);
998 	if (in != NULL) {
999 		/* no rate negotiation; just dup */
1000 		in->in_rates = ic->ic_bss->in_rates;
1001 		if (ic->ic_node_newassoc != NULL)
1002 			ic->ic_node_newassoc(in, 1);
1003 		ieee80211_node_authorize(in);
1004 	}
1005 	return (in);
1006 }
1007 
1008 static void
1009 ieee80211_saveie(uint8_t **iep, const uint8_t *ie)
1010 {
1011 	uint_t ielen = ie[1]+2;
1012 	/*
1013 	 * Record information element for later use.
1014 	 */
1015 	if (*iep == NULL || (*iep)[1] != ie[1]) {
1016 		if (*iep != NULL)
1017 			ieee80211_free(*iep);
1018 		*iep = ieee80211_malloc(ielen);
1019 	}
1020 	if (*iep != NULL)
1021 		(void) memcpy(*iep, ie, ielen);
1022 }
1023 
1024 static void
1025 saveie(uint8_t **iep, const uint8_t *ie)
1026 {
1027 	if (ie == NULL)
1028 		*iep = NULL;
1029 	else
1030 		ieee80211_saveie(iep, ie);
1031 }
1032 
1033 /*
1034  * Process a beacon or probe response frame.
1035  */
1036 void
1037 ieee80211_add_scan(ieee80211com_t *ic, const struct ieee80211_scanparams *sp,
1038     const struct ieee80211_frame *wh, int subtype, int rssi, int rstamp)
1039 {
1040 	ieee80211_node_table_t *nt = &ic->ic_scan;
1041 	ieee80211_node_t *in;
1042 	boolean_t newnode = B_FALSE;
1043 
1044 	in = ieee80211_find_node(nt, wh->i_addr3);
1045 	if (in == NULL) {
1046 		/*
1047 		 * Create a new entry.
1048 		 */
1049 		in = ieee80211_alloc_node(ic, nt, wh->i_addr3);
1050 		if (in == NULL) {
1051 			ieee80211_dbg(IEEE80211_MSG_ANY, "ieee80211_add_scan: "
1052 			    "alloc node failed\n");
1053 			return;
1054 		}
1055 		/*
1056 		 * inherit from ic_bss.
1057 		 */
1058 		ieee80211_copy_bss(in, ic->ic_bss);
1059 		ieee80211_node_setchan(ic, in, ic->ic_curchan);
1060 		newnode = B_TRUE;
1061 	}
1062 
1063 	/* ap beaconing multiple ssid w/ same bssid */
1064 
1065 	/*
1066 	 * sp->ssid[0] - element ID
1067 	 * sp->ssid[1] - length
1068 	 * sp->ssid[2]... - ssid
1069 	 */
1070 	if (sp->ssid[1] != 0 &&
1071 	    subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP ||
1072 	    in->in_esslen == 0) {
1073 		in->in_esslen = sp->ssid[1];
1074 		bzero(in->in_essid, sizeof (in->in_essid));
1075 		bcopy(sp->ssid + 2, in->in_essid, sp->ssid[1]);
1076 	}
1077 	IEEE80211_ADDR_COPY(in->in_bssid, wh->i_addr3);
1078 	in->in_rssi = (uint8_t)rssi;
1079 	in->in_rstamp = rstamp;
1080 	bcopy(sp->tstamp, in->in_tstamp.data, sizeof (in->in_tstamp));
1081 	in->in_intval = sp->bintval;
1082 	in->in_capinfo = sp->capinfo;
1083 	in->in_chan = &ic->ic_sup_channels[sp->chan];
1084 	in->in_phytype = sp->phytype;
1085 	in->in_fhdwell = sp->fhdwell;
1086 	in->in_fhindex = sp->fhindex;
1087 	in->in_erp = sp->erp;
1088 	if (sp->tim != NULL) {
1089 		struct ieee80211_tim_ie *ie;
1090 
1091 		ie = (struct ieee80211_tim_ie *)sp->tim;
1092 		in->in_dtim_count = ie->tim_count;
1093 		in->in_dtim_period = ie->tim_period;
1094 	}
1095 	/*
1096 	 * Record the byte offset from the mac header to
1097 	 * the start of the TIM information element for
1098 	 * use by hardware and/or to speedup software
1099 	 * processing of beacon frames.
1100 	 */
1101 	in->in_tim_off = sp->timoff;
1102 	/*
1103 	 * Record optional information elements that might be
1104 	 * used by applications or drivers.
1105 	 */
1106 	saveie(&in->in_wpa_ie, sp->wpa);
1107 
1108 	/* NB: must be after in_chan is setup */
1109 	(void) ieee80211_setup_rates(in, sp->rates, sp->xrates,
1110 	    IEEE80211_F_DOSORT);
1111 
1112 	if (!newnode)
1113 		ieee80211_free_node(in);
1114 }
1115 
1116 /*
1117  * Initialize/update an ad-hoc node with contents from a received
1118  * beacon frame.
1119  */
1120 void
1121 ieee80211_init_neighbor(ieee80211_node_t *in, const struct ieee80211_frame *wh,
1122     const struct ieee80211_scanparams *sp)
1123 {
1124 	in->in_esslen = sp->ssid[1];
1125 	(void) memcpy(in->in_essid, sp->ssid + 2, sp->ssid[1]);
1126 	IEEE80211_ADDR_COPY(in->in_bssid, wh->i_addr3);
1127 	(void) memcpy(in->in_tstamp.data, sp->tstamp, sizeof (in->in_tstamp));
1128 	in->in_intval = sp->bintval;
1129 	in->in_capinfo = sp->capinfo;
1130 	in->in_chan = in->in_ic->ic_curchan;
1131 	in->in_fhdwell = sp->fhdwell;
1132 	in->in_fhindex = sp->fhindex;
1133 	in->in_erp = sp->erp;
1134 	in->in_tim_off = sp->timoff;
1135 
1136 	/* NB: must be after in_chan is setup */
1137 	(void) ieee80211_setup_rates(in, sp->rates, sp->xrates,
1138 	    IEEE80211_F_DOSORT);
1139 }
1140 
1141 /*
1142  * Do node discovery in adhoc mode on receipt of a beacon
1143  * or probe response frame.  Note that for the driver's
1144  * benefit we we treat this like an association so the
1145  * driver has an opportuinty to setup it's private state.
1146  */
1147 ieee80211_node_t *
1148 ieee80211_add_neighbor(ieee80211com_t *ic, const struct ieee80211_frame *wh,
1149     const struct ieee80211_scanparams *sp)
1150 {
1151 	ieee80211_node_t *in;
1152 
1153 	in = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);
1154 	if (in != NULL) {
1155 		ieee80211_init_neighbor(in, wh, sp);
1156 		if (ic->ic_node_newassoc != NULL)
1157 			ic->ic_node_newassoc(in, 1);
1158 	}
1159 	return (in);
1160 }
1161 
1162 #define	IEEE80211_IS_CTL(wh) \
1163 	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1164 
1165 /*
1166  * Locate the node for sender, track state, and then pass the
1167  * (referenced) node up to the 802.11 layer for its use.  We
1168  * are required to pass some node so we fall back to ic_bss
1169  * when this frame is from an unknown sender.  The 802.11 layer
1170  * knows this means the sender wasn't in the node table and
1171  * acts accordingly.
1172  */
1173 ieee80211_node_t *
1174 ieee80211_find_rxnode(ieee80211com_t *ic, const struct ieee80211_frame *wh)
1175 {
1176 	ieee80211_node_table_t *nt;
1177 	ieee80211_node_t *in;
1178 
1179 	/* may want scanned nodes in the neighbor table for adhoc */
1180 	if (ic->ic_opmode == IEEE80211_M_STA ||
1181 	    (ic->ic_flags & IEEE80211_F_SCAN)) {
1182 		nt = &ic->ic_scan;
1183 	} else {
1184 		nt = &ic->ic_sta;
1185 	}
1186 
1187 	IEEE80211_NODE_LOCK(nt);
1188 	if (IEEE80211_IS_CTL(wh))
1189 		in = ieee80211_find_node_locked(nt, wh->i_addr1);
1190 	else
1191 		in = ieee80211_find_node_locked(nt, wh->i_addr2);
1192 	IEEE80211_NODE_UNLOCK(nt);
1193 
1194 	if (in == NULL)
1195 		in = ieee80211_ref_node(ic->ic_bss);
1196 
1197 	return (in);
1198 }
1199 
1200 /*
1201  * Return a reference to the appropriate node for sending
1202  * a data frame.  This handles node discovery in adhoc networks.
1203  */
1204 ieee80211_node_t *
1205 ieee80211_find_txnode(ieee80211com_t *ic, const uint8_t *daddr)
1206 {
1207 	ieee80211_node_table_t *nt = &ic->ic_sta;
1208 	ieee80211_node_t *in;
1209 
1210 	/*
1211 	 * The destination address should be in the node table
1212 	 * unless this is a multicast/broadcast frame.  We can
1213 	 * also optimize station mode operation, all frames go
1214 	 * to the bss node.
1215 	 */
1216 	IEEE80211_NODE_LOCK(nt);
1217 	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(daddr))
1218 		in = ieee80211_ref_node(ic->ic_bss);
1219 	else
1220 		in = ieee80211_find_node_locked(nt, daddr);
1221 	IEEE80211_NODE_UNLOCK(nt);
1222 
1223 	if (in == NULL) {
1224 		if (ic->ic_opmode == IEEE80211_M_IBSS) {
1225 			/*
1226 			 * In adhoc mode cons up a node for the destination.
1227 			 * Note that we need an additional reference for the
1228 			 * caller to be consistent with
1229 			 * ieee80211_find_node_locked
1230 			 * can't hold lock across ieee80211_dup_bss 'cuz of
1231 			 * recursive locking
1232 			 */
1233 			in = ieee80211_fakeup_adhoc_node(nt, daddr);
1234 			if (in != NULL)
1235 				(void) ieee80211_ref_node(in);
1236 		} else {
1237 			ieee80211_dbg(IEEE80211_MSG_OUTPUT,
1238 			    "ieee80211_find_txnode: "
1239 			    "[%s] no node, discard frame\n",
1240 			    ieee80211_macaddr_sprintf(daddr));
1241 		}
1242 	}
1243 	return (in);
1244 }
1245 
1246 /*
1247  * Remove a node from the node database entries and free memory
1248  * associated with the node. The node table lock is acquired by
1249  * the caller.
1250  */
1251 static void
1252 ieee80211_free_node_locked(ieee80211_node_t *in)
1253 {
1254 	ieee80211com_t *ic = in->in_ic;
1255 	ieee80211_node_table_t *nt = in->in_table;
1256 	int32_t hash;
1257 
1258 	if (nt != NULL) {
1259 		hash = ieee80211_node_hash(in->in_macaddr);
1260 		list_remove(&nt->nt_hash[hash], in);
1261 		list_remove(&nt->nt_node, in);
1262 	}
1263 	ic->ic_node_free(in);
1264 }
1265 
1266 /*
1267  * Remove a node from the node database entries and free any
1268  * memory associated with the node.
1269  * This method can be overridden in ieee80211_attach()
1270  */
1271 void
1272 ieee80211_free_node(ieee80211_node_t *in)
1273 {
1274 	ieee80211_node_table_t *nt = in->in_table;
1275 
1276 	if (nt != NULL)
1277 		IEEE80211_NODE_LOCK(nt);
1278 	if (ieee80211_node_decref_nv(in) == 0)
1279 		ieee80211_free_node_locked(in);
1280 	if (nt != NULL)
1281 		IEEE80211_NODE_UNLOCK(nt);
1282 }
1283 
1284 /*
1285  * Reclaim a node.  If this is the last reference count then
1286  * do the normal free work.  Otherwise remove it from the node
1287  * table and mark it gone by clearing the back-reference.
1288  */
1289 static void
1290 ieee80211_node_reclaim(ieee80211_node_table_t *nt, ieee80211_node_t *in)
1291 {
1292 	int32_t hash;
1293 
1294 	IEEE80211_NODE_LOCK_ASSERT(nt);
1295 	ieee80211_dbg(IEEE80211_MSG_NODE, "node_reclaim: "
1296 	    " remove %p<%s> from %s table, refcnt %d\n",
1297 	    in, ieee80211_macaddr_sprintf(in->in_macaddr), nt->nt_name,
1298 	    ieee80211_node_refcnt(in));
1299 
1300 	if (ieee80211_node_decref_nv(in) != 0) {
1301 		/*
1302 		 * Clear any entry in the unicast key mapping table.
1303 		 * We need to do it here so rx lookups don't find it
1304 		 * in the mapping table even if it's not in the hash
1305 		 * table.  We cannot depend on the mapping table entry
1306 		 * being cleared because the node may not be free'd.
1307 		 */
1308 		hash = ieee80211_node_hash(in->in_macaddr);
1309 		list_remove(&nt->nt_hash[hash], in);
1310 		list_remove(&nt->nt_node, in);
1311 		in->in_table = NULL;
1312 	} else {
1313 		ieee80211_free_node_locked(in);
1314 	}
1315 }
1316 
1317 /*
1318  * Iterate through the node list and reclaim all node in the node table.
1319  * The node table lock is acquired by the caller
1320  */
1321 static void
1322 ieee80211_free_allnodes_locked(ieee80211_node_table_t *nt)
1323 {
1324 	ieee80211_node_t *in;
1325 
1326 	ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_free_allnodes_locked(): "
1327 	    "free all nodes in %s table\n", nt->nt_name);
1328 
1329 	in = list_head(&nt->nt_node);
1330 	while (in != NULL) {
1331 		ieee80211_node_reclaim(nt, in);
1332 		in = list_head(&nt->nt_node);
1333 	}
1334 	ieee80211_reset_erp(nt->nt_ic);
1335 }
1336 
1337 /*
1338  * Iterate through the node list, calling ieee80211_node_reclaim() for
1339  * all nodes associated with the interface.
1340  */
1341 static void
1342 ieee80211_free_allnodes(ieee80211_node_table_t *nt)
1343 {
1344 	IEEE80211_NODE_LOCK(nt);
1345 	ieee80211_free_allnodes_locked(nt);
1346 	IEEE80211_NODE_UNLOCK(nt);
1347 }
1348 
1349 /*
1350  * Timeout entries in the scan cache. This is the timeout callback
1351  * function of node table ic_scan which is called when the inactivity
1352  * timer expires.
1353  */
1354 static void
1355 ieee80211_timeout_scan_candidates(ieee80211_node_table_t *nt)
1356 {
1357 	ieee80211com_t *ic = nt->nt_ic;
1358 	ieee80211_node_t *in;
1359 
1360 	IEEE80211_NODE_LOCK(nt);
1361 	in = ic->ic_bss;
1362 	node_cleanfrag(in);	/* Free fragment if not needed */
1363 	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1364 	IEEE80211_NODE_UNLOCK(nt);
1365 }
1366 
1367 /*
1368  * Timeout inactive stations and do related housekeeping.
1369  * Note that we cannot hold the node lock while sending a
1370  * frame as this would lead to a LOR.  Instead we use a
1371  * generation number to mark nodes that we've scanned and
1372  * drop the lock and restart a scan if we have to time out
1373  * a node.  Since we are single-threaded by virtue of
1374  * controlling the inactivity timer we can be sure this will
1375  * process each node only once.
1376  */
1377 static void
1378 ieee80211_timeout_stations(ieee80211_node_table_t *nt)
1379 {
1380 	ieee80211com_t *ic = nt->nt_ic;
1381 	ieee80211_impl_t *im = ic->ic_private;
1382 	ieee80211_node_t *in = NULL;
1383 	uint32_t gen;
1384 	boolean_t isadhoc;
1385 
1386 	IEEE80211_LOCK_ASSERT(ic);
1387 	isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS ||
1388 	    ic->ic_opmode == IEEE80211_M_AHDEMO);
1389 	IEEE80211_SCAN_LOCK(nt);
1390 	gen = ++nt->nt_scangen;
1391 restart:
1392 	IEEE80211_NODE_LOCK(nt);
1393 	for (in = list_head(&nt->nt_node); in != NULL;
1394 	    in = list_next(&nt->nt_node, in)) {
1395 		if (in->in_scangen == gen)	/* previously handled */
1396 			continue;
1397 		in->in_scangen = gen;
1398 		node_cleanfrag(in);	/* free fragment if not needed */
1399 
1400 		/*
1401 		 * Special case ourself; we may be idle for extended periods
1402 		 * of time and regardless reclaiming our state is wrong.
1403 		 */
1404 		if (in == ic->ic_bss)
1405 			continue;
1406 		in->in_inact--;
1407 		if (in->in_associd != 0 || isadhoc) {
1408 			/*
1409 			 * Probe the station before time it out.  We
1410 			 * send a null data frame which may not be
1411 			 * uinversally supported by drivers (need it
1412 			 * for ps-poll support so it should be...).
1413 			 */
1414 			if (0 < in->in_inact &&
1415 			    in->in_inact <= im->im_inact_probe) {
1416 				ieee80211_dbg(IEEE80211_MSG_NODE, "net80211: "
1417 				    "probe station due to inactivity\n");
1418 				IEEE80211_NODE_UNLOCK(nt);
1419 				IEEE80211_UNLOCK(ic);
1420 				(void) ieee80211_send_nulldata(in);
1421 				IEEE80211_LOCK(ic);
1422 				goto restart;
1423 			}
1424 		}
1425 		if (in->in_inact <= 0) {
1426 			ieee80211_dbg(IEEE80211_MSG_NODE, "net80211: "
1427 			    "station timed out due to inact (refcnt %u)\n",
1428 			    ieee80211_node_refcnt(in));
1429 			/*
1430 			 * Send a deauthenticate frame and drop the station.
1431 			 * This is somewhat complicated due to reference counts
1432 			 * and locking.  At this point a station will typically
1433 			 * have a reference count of 1.  ieee80211_node_leave
1434 			 * will do a "free" of the node which will drop the
1435 			 * reference count.  But in the meantime a reference
1436 			 * wil be held by the deauth frame.  The actual reclaim
1437 			 * of the node will happen either after the tx is
1438 			 * completed or by ieee80211_node_leave.
1439 			 *
1440 			 * Separately we must drop the node lock before sending
1441 			 * in case the driver takes a lock, as this will result
1442 			 * in  LOR between the node lock and the driver lock.
1443 			 */
1444 			IEEE80211_NODE_UNLOCK(nt);
1445 			if (in->in_associd != 0) {
1446 				IEEE80211_UNLOCK(ic);
1447 				IEEE80211_SEND_MGMT(ic, in,
1448 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
1449 				    IEEE80211_REASON_AUTH_EXPIRE);
1450 				IEEE80211_LOCK(ic);
1451 			}
1452 			ieee80211_node_leave(ic, in);
1453 			goto restart;
1454 		}
1455 	}
1456 	IEEE80211_NODE_UNLOCK(nt);
1457 
1458 	IEEE80211_SCAN_UNLOCK(nt);
1459 
1460 	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1461 }
1462 
1463 /*
1464  * Call the user-defined call back function for all nodes in
1465  * the node cache. The callback is invoked with the user-supplied
1466  * value and a pointer to the current node.
1467  */
1468 void
1469 ieee80211_iterate_nodes(ieee80211_node_table_t *nt, ieee80211_iter_func *f,
1470     void *arg)
1471 {
1472 	ieee80211_node_t *in;
1473 
1474 	IEEE80211_NODE_LOCK(nt);
1475 	in = list_head(&nt->nt_node);
1476 	while (in != NULL) {
1477 		if (in->in_chan == IEEE80211_CHAN_ANYC) {
1478 			in = list_next(&nt->nt_node, in);
1479 			continue;
1480 		}
1481 		(void) ieee80211_ref_node(in);
1482 		IEEE80211_NODE_UNLOCK(nt);
1483 		(*f)(arg, in);
1484 		ieee80211_free_node(in);
1485 		IEEE80211_NODE_LOCK(nt);
1486 		in = list_next(&nt->nt_node, in);
1487 	}
1488 	IEEE80211_NODE_UNLOCK(nt);
1489 }
1490 
1491 /*
1492  * Handle bookkeeping for station deauthentication/disassociation
1493  * when operating as an ap.
1494  */
1495 static void
1496 ieee80211_node_leave(ieee80211com_t *ic, ieee80211_node_t *in)
1497 {
1498 	ieee80211_node_table_t *nt = in->in_table;
1499 
1500 	ASSERT(ic->ic_opmode == IEEE80211_M_IBSS);
1501 
1502 	/*
1503 	 * Remove the node from any table it's recorded in and
1504 	 * drop the caller's reference.  Removal from the table
1505 	 * is important to insure the node is not reprocessed
1506 	 * for inactivity.
1507 	 */
1508 	if (nt != NULL) {
1509 		IEEE80211_NODE_LOCK(nt);
1510 		ieee80211_node_reclaim(nt, in);
1511 		IEEE80211_NODE_UNLOCK(nt);
1512 	} else {
1513 		ieee80211_free_node(in);
1514 	}
1515 }
1516 
1517 /*
1518  * Initialize a node table with specified name, inactivity timer value
1519  * and callback inactivity timeout function. Associate the node table
1520  * with interface softc, ic.
1521  */
1522 static void
1523 ieee80211_node_table_init(ieee80211com_t *ic, ieee80211_node_table_t *nt,
1524     const char *name, int inact, int keyixmax,
1525     void (*timeout)(ieee80211_node_table_t *))
1526 {
1527 	int i;
1528 
1529 	ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_node_table_init():"
1530 	    "%s table, inact %d\n", name, inact);
1531 
1532 	nt->nt_ic = ic;
1533 	nt->nt_name = name;
1534 	nt->nt_inact_timer = 0;
1535 	nt->nt_inact_init = inact;
1536 	nt->nt_timeout = timeout;
1537 	nt->nt_keyixmax = keyixmax;
1538 	nt->nt_scangen = 1;
1539 	mutex_init(&nt->nt_scanlock, NULL, MUTEX_DRIVER, NULL);
1540 	mutex_init(&nt->nt_nodelock, NULL, MUTEX_DRIVER, NULL);
1541 
1542 	list_create(&nt->nt_node, sizeof (ieee80211_node_t),
1543 	    offsetof(ieee80211_node_t, in_node));
1544 	for (i = 0; i < IEEE80211_NODE_HASHSIZE; i++) {
1545 		list_create(&nt->nt_hash[i], sizeof (ieee80211_node_t),
1546 		    offsetof(ieee80211_node_t, in_hash));
1547 	}
1548 }
1549 
1550 /*
1551  * Reset a node table. Clean its inactivity timer and call
1552  * ieee80211_free_allnodes_locked() to free all nodes in the
1553  * node table.
1554  */
1555 void
1556 ieee80211_node_table_reset(ieee80211_node_table_t *nt)
1557 {
1558 	ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_node_table_reset(): "
1559 	    "%s table\n", nt->nt_name);
1560 
1561 	IEEE80211_NODE_LOCK(nt);
1562 	nt->nt_inact_timer = 0;
1563 	ieee80211_free_allnodes_locked(nt);
1564 	IEEE80211_NODE_UNLOCK(nt);
1565 }
1566 
1567 /*
1568  * Destroy a node table. Free all nodes in the node table.
1569  * This function is usually called by node detach function.
1570  */
1571 static void
1572 ieee80211_node_table_cleanup(ieee80211_node_table_t *nt)
1573 {
1574 	ieee80211_dbg(IEEE80211_MSG_NODE, "ieee80211_node_table_cleanup(): "
1575 	    "%s table\n", nt->nt_name);
1576 
1577 	IEEE80211_NODE_LOCK(nt);
1578 	ieee80211_free_allnodes_locked(nt);
1579 	IEEE80211_NODE_UNLOCK(nt);
1580 	mutex_destroy(&nt->nt_nodelock);
1581 	mutex_destroy(&nt->nt_scanlock);
1582 }
1583