ieee80211_node.c revision 139521
1/*-
2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2004 Sam Leffler, Errno Consulting
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * Alternatively, this software may be distributed under the terms of the
18 * GNU General Public License ("GPL") version 2 as published by the Free
19 * Software Foundation.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_node.c 139521 2004-12-31 21:22:12Z sam $");
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/mbuf.h>
39#include <sys/malloc.h>
40#include <sys/kernel.h>
41
42#include <sys/socket.h>
43
44#include <net/if.h>
45#include <net/if_media.h>
46#include <net/ethernet.h>
47
48#include <net80211/ieee80211_var.h>
49
50#include <net/bpf.h>
51
52static struct ieee80211_node *node_alloc(struct ieee80211_node_table *);
53static void node_cleanup(struct ieee80211_node *);
54static void node_free(struct ieee80211_node *);
55static u_int8_t node_getrssi(const struct ieee80211_node *);
56
57static void ieee80211_setup_node(struct ieee80211_node_table *,
58		struct ieee80211_node *, const u_int8_t *);
59static void _ieee80211_free_node(struct ieee80211_node *);
60static void ieee80211_free_allnodes(struct ieee80211_node_table *);
61
62static void ieee80211_timeout_scan_candidates(struct ieee80211_node_table *);
63static void ieee80211_timeout_stations(struct ieee80211_node_table *);
64
65static void ieee80211_set_tim(struct ieee80211com *,
66		struct ieee80211_node *, int set);
67
68static void ieee80211_node_table_init(struct ieee80211com *ic,
69	struct ieee80211_node_table *nt, const char *name, int inact,
70	void (*timeout)(struct ieee80211_node_table *));
71static struct ieee80211_node_table *ieee80211_node_table_alloc(
72	struct ieee80211com *ic, const char *name, int inact,
73	void (*timeout)(struct ieee80211_node_table *));
74static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
75
76MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
77
78void
79ieee80211_node_attach(struct ieee80211com *ic)
80{
81
82	ic->ic_sta = NULL;		/* defer to when we need it */
83	ieee80211_node_table_init(ic, &ic->ic_scan, "scan",
84		IEEE80211_INACT_SCAN, ieee80211_timeout_scan_candidates);
85
86	ic->ic_node_alloc = node_alloc;
87	ic->ic_node_free = node_free;
88	ic->ic_node_cleanup = node_cleanup;
89	ic->ic_node_getrssi = node_getrssi;
90
91	/* default station inactivity timer setings */
92	ic->ic_inact_init = IEEE80211_INACT_INIT;
93	ic->ic_inact_auth = IEEE80211_INACT_AUTH;
94	ic->ic_inact_run = IEEE80211_INACT_RUN;
95	ic->ic_inact_probe = IEEE80211_INACT_PROBE;
96
97	/* XXX defer */
98	if (ic->ic_max_aid == 0)
99		ic->ic_max_aid = IEEE80211_AID_DEF;
100	else if (ic->ic_max_aid > IEEE80211_AID_MAX)
101		ic->ic_max_aid = IEEE80211_AID_MAX;
102	MALLOC(ic->ic_aid_bitmap, u_int32_t *,
103		howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t),
104		M_DEVBUF, M_NOWAIT | M_ZERO);
105	if (ic->ic_aid_bitmap == NULL) {
106		/* XXX no way to recover */
107		printf("%s: no memory for AID bitmap!\n", __func__);
108		ic->ic_max_aid = 0;
109	}
110
111	/* XXX defer until using hostap/ibss mode */
112	ic->ic_tim_len = howmany(ic->ic_max_aid, 8) * sizeof(u_int8_t);
113	MALLOC(ic->ic_tim_bitmap, u_int8_t *, ic->ic_tim_len,
114		M_DEVBUF, M_NOWAIT | M_ZERO);
115	if (ic->ic_tim_bitmap == NULL) {
116		/* XXX no way to recover */
117		printf("%s: no memory for TIM bitmap!\n", __func__);
118	}
119	ic->ic_set_tim = ieee80211_set_tim;	/* NB: driver should override */
120}
121
122void
123ieee80211_node_lateattach(struct ieee80211com *ic)
124{
125	struct ieee80211_node *ni;
126	struct ieee80211_rsnparms *rsn;
127
128	ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
129	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
130	/*
131	 * Setup "global settings" in the bss node so that
132	 * each new station automatically inherits them.
133	 */
134	rsn = &ni->ni_rsn;
135	/* WEP, TKIP, and AES-CCM are always supported */
136	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP;
137	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP;
138	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM;
139	if (ic->ic_caps & IEEE80211_C_AES)
140		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB;
141	if (ic->ic_caps & IEEE80211_C_CKIP)
142		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP;
143	/*
144	 * Default unicast cipher to WEP for 802.1x use.  If
145	 * WPA is enabled the management code will set these
146	 * values to reflect.
147	 */
148	rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP;
149	rsn->rsn_ucastkeylen = 104 / NBBY;
150	/*
151	 * WPA says the multicast cipher is the lowest unicast
152	 * cipher supported.  But we skip WEP which would
153	 * otherwise be used based on this criteria.
154	 */
155	rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP;
156	rsn->rsn_mcastkeylen = 128 / NBBY;
157
158	/*
159	 * We support both WPA-PSK and 802.1x; the one used
160	 * is determined by the authentication mode and the
161	 * setting of the PSK state.
162	 */
163	rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK;
164	rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
165
166	ic->ic_bss = ieee80211_ref_node(ni);		/* hold reference */
167	ic->ic_auth = ieee80211_authenticator_get(ni->ni_authmode);
168}
169
170void
171ieee80211_node_detach(struct ieee80211com *ic)
172{
173
174	if (ic->ic_bss != NULL) {
175		ieee80211_free_node(ic->ic_bss);
176		ic->ic_bss = NULL;
177	}
178	ieee80211_node_table_cleanup(&ic->ic_scan);
179	if (ic->ic_sta != NULL) {
180		ieee80211_node_table_free(ic->ic_sta);
181		ic->ic_sta = NULL;
182	}
183	if (ic->ic_aid_bitmap != NULL) {
184		FREE(ic->ic_aid_bitmap, M_DEVBUF);
185		ic->ic_aid_bitmap = NULL;
186	}
187	if (ic->ic_tim_bitmap != NULL) {
188		FREE(ic->ic_tim_bitmap, M_DEVBUF);
189		ic->ic_tim_bitmap = NULL;
190	}
191}
192
193/*
194 * Port authorize/unauthorize interfaces for use by an authenticator.
195 */
196
197void
198ieee80211_node_authorize(struct ieee80211com *ic, struct ieee80211_node *ni)
199{
200	ni->ni_flags |= IEEE80211_NODE_AUTH;
201}
202
203void
204ieee80211_node_unauthorize(struct ieee80211com *ic, struct ieee80211_node *ni)
205{
206	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
207}
208
209/*
210 * Set/change the channel.  The rate set is also updated as
211 * to insure a consistent view by drivers.
212 */
213static __inline void
214ieee80211_set_chan(struct ieee80211com *ic,
215	struct ieee80211_node *ni, struct ieee80211_channel *chan)
216{
217	ni->ni_chan = chan;
218	ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, chan)];
219}
220
221/*
222 * AP scanning support.
223 */
224
225#ifdef IEEE80211_DEBUG
226static void
227dump_chanlist(const u_char chans[])
228{
229	const char *sep;
230	int i;
231
232	sep = " ";
233	for (i = 0; i < IEEE80211_CHAN_MAX; i++)
234		if (isset(chans, i)) {
235			printf("%s%u", sep, i);
236			sep = ", ";
237		}
238}
239#endif /* IEEE80211_DEBUG */
240
241/*
242 * Initialize the channel set to scan based on the
243 * of available channels and the current PHY mode.
244 */
245static void
246ieee80211_reset_scan(struct ieee80211com *ic)
247{
248
249	/* XXX ic_des_chan should be handled with ic_chan_active */
250	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
251		memset(ic->ic_chan_scan, 0, sizeof(ic->ic_chan_scan));
252		setbit(ic->ic_chan_scan,
253			ieee80211_chan2ieee(ic, ic->ic_des_chan));
254	} else
255		memcpy(ic->ic_chan_scan, ic->ic_chan_active,
256			sizeof(ic->ic_chan_active));
257	/* NB: hack, setup so next_scan starts with the first channel */
258	if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC)
259		ieee80211_set_chan(ic, ic->ic_bss,
260			&ic->ic_channels[IEEE80211_CHAN_MAX]);
261#ifdef IEEE80211_DEBUG
262	if (ieee80211_msg_scan(ic)) {
263		printf("%s: scan set:", __func__);
264		dump_chanlist(ic->ic_chan_scan);
265		printf(" start chan %u\n",
266			ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan));
267	}
268#endif /* IEEE80211_DEBUG */
269}
270
271/*
272 * Begin an active scan.
273 */
274void
275ieee80211_begin_scan(struct ieee80211com *ic, int reset)
276{
277
278	ic->ic_scan.nt_scangen++;
279	/*
280	 * In all but hostap mode scanning starts off in
281	 * an active mode before switching to passive.
282	 */
283	if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
284		ic->ic_flags |= IEEE80211_F_ASCAN;
285		ic->ic_stats.is_scan_active++;
286	} else
287		ic->ic_stats.is_scan_passive++;
288	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
289		"begin %s scan in %s mode, scangen %u\n",
290		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive",
291		ieee80211_phymode_name[ic->ic_curmode], ic->ic_scan.nt_scangen);
292	/*
293	 * Clear scan state and flush any previously seen AP's.
294	 */
295	ieee80211_reset_scan(ic);
296	if (reset)
297		ieee80211_free_allnodes(&ic->ic_scan);
298
299	ic->ic_flags |= IEEE80211_F_SCAN;
300
301	/* Scan the next channel. */
302	ieee80211_next_scan(ic);
303}
304
305/*
306 * Switch to the next channel marked for scanning.
307 */
308int
309ieee80211_next_scan(struct ieee80211com *ic)
310{
311	struct ieee80211_channel *chan;
312
313	/*
314	 * Insure any previous mgt frame timeouts don't fire.
315	 * This assumes the driver does the right thing in
316	 * flushing anything queued in the driver and below.
317	 */
318	ic->ic_mgt_timer = 0;
319
320	chan = ic->ic_bss->ni_chan;
321	do {
322		if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
323			chan = &ic->ic_channels[0];
324		if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
325			clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
326			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
327			    "%s: chan %d->%d\n", __func__,
328			    ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
329			    ieee80211_chan2ieee(ic, chan));
330			ieee80211_set_chan(ic, ic->ic_bss, chan);
331#ifdef notyet
332			/* XXX driver state change */
333			/*
334			 * Scan next channel. If doing an active scan
335			 * and the channel is not marked passive-only
336			 * then send a probe request.  Otherwise just
337			 * listen for beacons on the channel.
338			 */
339			if ((ic->ic_flags & IEEE80211_F_ASCAN) &&
340			    (ni->ni_chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) {
341				IEEE80211_SEND_MGMT(ic, ni,
342				    IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0);
343			}
344#else
345			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
346#endif
347			return 1;
348		}
349	} while (chan != ic->ic_bss->ni_chan);
350	ieee80211_end_scan(ic);
351	return 0;
352}
353
354void
355ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
356{
357	struct ieee80211_node *ni;
358
359	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
360		"%s: creating ibss\n", __func__);
361
362	/*
363	 * Create the station/neighbor table.  Note that for adhoc
364	 * mode we make the initial inactivity timer longer since
365	 * we create nodes only through discovery and they typically
366	 * are long-lived associations.
367	 */
368	if (ic->ic_opmode == IEEE80211_M_HOSTAP)
369		ic->ic_sta = ieee80211_node_table_alloc(ic,
370					"station", ic->ic_inact_init,
371					ieee80211_timeout_stations);
372	else
373		ic->ic_sta = ieee80211_node_table_alloc(ic,
374					"neighbor", ic->ic_inact_run,
375					ieee80211_timeout_stations);
376	if (ic->ic_sta == NULL) {
377		/*
378		 * Should remain in SCAN state and retry.
379		 */
380		/* XXX stat+msg */
381		return;
382	}
383
384	ni = ic->ic_bss;
385	IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr);
386	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
387	ni->ni_esslen = ic->ic_des_esslen;
388	memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
389	ni->ni_rssi = 0;
390	ni->ni_rstamp = 0;
391	ni->ni_tstamp.tsf = 0;
392	ni->ni_intval = ic->ic_lintval;
393	ni->ni_capinfo = 0;
394	ni->ni_erp = 0;
395	if (ic->ic_flags & IEEE80211_F_PRIVACY)
396		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
397	if (ic->ic_phytype == IEEE80211_T_FH) {
398		ni->ni_fhdwell = 200;	/* XXX */
399		ni->ni_fhindex = 1;
400	}
401	if (ic->ic_opmode == IEEE80211_M_IBSS) {
402		ic->ic_flags |= IEEE80211_F_SIBSS;
403		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
404		ni->ni_bssid[0] |= 0x02;	/* local bit for IBSS */
405	}
406	/*
407	 * Fix the channel and related attributes.
408	 */
409	ieee80211_set_chan(ic, ni, chan);
410	ic->ic_curmode = ieee80211_chan2mode(ic, chan);
411	/*
412	 * Do mode-specific rate setup.
413	 */
414	if (ic->ic_curmode == IEEE80211_MODE_11G) {
415		/*
416		 * Use a mixed 11b/11g rate set.
417		 */
418		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11G);
419	} else if (ic->ic_curmode == IEEE80211_MODE_11B) {
420		/*
421		 * Force pure 11b rate set.
422		 */
423		ieee80211_set11gbasicrates(&ni->ni_rates, IEEE80211_MODE_11B);
424	}
425	/*
426	 * Set the erp state (mostly the slot time) to deal with
427	 * the auto-select case; this should be redundant if the
428	 * mode is locked.
429	 */
430	ieee80211_reset_erp(ic);
431	ieee80211_wme_initparams(ic);
432
433	ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
434}
435
436void
437ieee80211_reset_bss(struct ieee80211com *ic)
438{
439	struct ieee80211_node *ni, *obss;
440
441	ieee80211_node_table_reset(&ic->ic_scan);
442	ni = ieee80211_alloc_node(&ic->ic_scan, ic->ic_myaddr);
443	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
444	obss = ic->ic_bss;
445	ic->ic_bss = ieee80211_ref_node(ni);
446	if (obss != NULL)
447		ieee80211_free_node(obss);
448	if (ic->ic_sta != NULL) {
449		ieee80211_node_table_free(ic->ic_sta);
450		ic->ic_sta = NULL;
451	}
452}
453
454static int
455ieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
456{
457        u_int8_t rate;
458        int fail;
459
460	fail = 0;
461	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
462		fail |= 0x01;
463	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
464	    ni->ni_chan != ic->ic_des_chan)
465		fail |= 0x01;
466	if (ic->ic_opmode == IEEE80211_M_IBSS) {
467		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
468			fail |= 0x02;
469	} else {
470		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
471			fail |= 0x02;
472	}
473	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
474		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
475			fail |= 0x04;
476	} else {
477		/* XXX does this mean privacy is supported or required? */
478		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
479			fail |= 0x04;
480	}
481	rate = ieee80211_fix_rate(ic, ni, IEEE80211_F_DONEGO);
482	if (rate & IEEE80211_RATE_BASIC)
483		fail |= 0x08;
484	if (ic->ic_des_esslen != 0 &&
485	    (ni->ni_esslen != ic->ic_des_esslen ||
486	     memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
487		fail |= 0x10;
488	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
489	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
490		fail |= 0x20;
491#ifdef IEEE80211_DEBUG
492	if (ieee80211_msg_scan(ic)) {
493		printf(" %c %s", fail ? '-' : '+',
494		    ether_sprintf(ni->ni_macaddr));
495		printf(" %s%c", ether_sprintf(ni->ni_bssid),
496		    fail & 0x20 ? '!' : ' ');
497		printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
498			fail & 0x01 ? '!' : ' ');
499		printf(" %+4d", ni->ni_rssi);
500		printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
501		    fail & 0x08 ? '!' : ' ');
502		printf(" %4s%c",
503		    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
504		    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
505		    "????",
506		    fail & 0x02 ? '!' : ' ');
507		printf(" %3s%c ",
508		    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
509		    "wep" : "no",
510		    fail & 0x04 ? '!' : ' ');
511		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
512		printf("%s\n", fail & 0x10 ? "!" : "");
513	}
514#endif
515	return fail;
516}
517
518static __inline u_int8_t
519maxrate(const struct ieee80211_node *ni)
520{
521	const struct ieee80211_rateset *rs = &ni->ni_rates;
522	/* NB: assumes rate set is sorted (happens on frame receive) */
523	return rs->rs_rates[rs->rs_nrates-1] & IEEE80211_RATE_VAL;
524}
525
526/*
527 * Compare the capabilities of two nodes and decide which is
528 * more desirable (return >0 if a is considered better).  Note
529 * that we assume compatibility/usability has already been checked
530 * so we don't need to (e.g. validate whether privacy is supported).
531 * Used to select the best scan candidate for association in a BSS.
532 */
533static int
534ieee80211_node_compare(struct ieee80211com *ic,
535		       const struct ieee80211_node *a,
536		       const struct ieee80211_node *b)
537{
538	u_int8_t maxa, maxb;
539	u_int8_t rssia, rssib;
540
541	/* privacy support preferred */
542	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) &&
543	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
544		return 1;
545	if ((a->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0 &&
546	    (b->ni_capinfo & IEEE80211_CAPINFO_PRIVACY))
547		return -1;
548
549	/* best/max rate preferred if signal level close enough XXX */
550	maxa = maxrate(a);
551	maxb = maxrate(b);
552	rssia = ic->ic_node_getrssi(a);
553	rssib = ic->ic_node_getrssi(b);
554	if (maxa != maxb && abs(rssib - rssia) < 5)
555		return maxa - maxb;
556
557	/* XXX use freq for channel preference */
558	/* for now just prefer 5Ghz band to all other bands */
559	if (IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
560	   !IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
561		return 1;
562	if (!IEEE80211_IS_CHAN_5GHZ(a->ni_chan) &&
563	     IEEE80211_IS_CHAN_5GHZ(b->ni_chan))
564		return -1;
565
566	/* all things being equal, use signal level */
567	return rssia - rssib;
568}
569
570/*
571 * Complete a scan of potential channels.
572 */
573void
574ieee80211_end_scan(struct ieee80211com *ic)
575{
576	struct ieee80211_node *ni, *nextbs, *selbs;
577	struct ieee80211_node_table *nt;
578
579	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "end %s scan\n",
580		(ic->ic_flags & IEEE80211_F_ASCAN) ?  "active" : "passive");
581
582	ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
583	nt = &ic->ic_scan;
584	ni = TAILQ_FIRST(&nt->nt_node);
585
586	ieee80211_notify_scan_done(ic);
587
588	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
589		u_int8_t maxrssi[IEEE80211_CHAN_MAX];	/* XXX off stack? */
590		int i, bestchan;
591		u_int8_t rssi;
592
593		/*
594		 * The passive scan to look for existing AP's completed,
595		 * select a channel to camp on.  Identify the channels
596		 * that already have one or more AP's and try to locate
597		 * an unnoccupied one.  If that fails, pick a channel that
598		 * looks to be quietest.
599		 */
600		memset(maxrssi, 0, sizeof(maxrssi));
601		for (; ni != NULL; ni = nextbs) {
602			ieee80211_ref_node(ni);
603			nextbs = TAILQ_NEXT(ni, ni_list);
604			rssi = ic->ic_node_getrssi(ni);
605			i = ieee80211_chan2ieee(ic, ni->ni_chan);
606			if (rssi > maxrssi[i])
607				maxrssi[i] = rssi;
608			ieee80211_unref_node(&ni);
609		}
610		/* XXX select channel more intelligently */
611		bestchan = -1;
612		for (i = 0; i < IEEE80211_CHAN_MAX; i++)
613			if (isset(ic->ic_chan_active, i)) {
614				/*
615				 * If the channel is unoccupied the max rssi
616				 * should be zero; just take it.  Otherwise
617				 * track the channel with the lowest rssi and
618				 * use that when all channels appear occupied.
619				 */
620				if (maxrssi[i] == 0) {
621					bestchan = i;
622					break;
623				}
624				if (maxrssi[i] < maxrssi[bestchan])
625					bestchan = i;
626			}
627		if (bestchan != -1) {
628			ieee80211_create_ibss(ic, &ic->ic_channels[bestchan]);
629			return;
630		}
631		/* no suitable channel, should not happen */
632	}
633
634	/*
635	 * When manually sequencing the state machine; scan just once
636	 * regardless of whether we have a candidate or not.  The
637	 * controlling application is expected to setup state and
638	 * initiate an association.
639	 */
640	if (ic->ic_roaming == IEEE80211_ROAMING_MANUAL)
641		return;
642	/*
643	 * Automatic sequencing; look for a candidate and
644	 * if found join the network.
645	 */
646	if (ni == NULL) {
647		IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
648			"%s: no scan candidate\n", __func__);
649  notfound:
650		if (ic->ic_opmode == IEEE80211_M_IBSS &&
651		    (ic->ic_flags & IEEE80211_F_IBSSON) &&
652		    ic->ic_des_esslen != 0) {
653			ieee80211_create_ibss(ic, ic->ic_ibss_chan);
654			return;
655		}
656		/*
657		 * Reset the list of channels to scan and start again.
658		 */
659		ieee80211_reset_scan(ic);
660		ic->ic_flags |= IEEE80211_F_SCAN;
661		ieee80211_next_scan(ic);
662		return;
663	}
664	selbs = NULL;
665	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN, "\t%s\n",
666	    "macaddr          bssid         chan  rssi rate flag  wep  essid");
667	for (; ni != NULL; ni = nextbs) {
668		ieee80211_ref_node(ni);
669		nextbs = TAILQ_NEXT(ni, ni_list);
670		if (ni->ni_fails) {
671			/*
672			 * The configuration of the access points may change
673			 * during my scan.  So delete the entry for the AP
674			 * and retry to associate if there is another beacon.
675			 */
676			IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
677				"%s: skip scan candidate %s, fails %u\n",
678				__func__, ether_sprintf(ni->ni_macaddr),
679				ni->ni_fails);
680			if (ni->ni_fails++ > 2)
681				ieee80211_free_node(ni);
682			continue;
683		}
684		if (ieee80211_match_bss(ic, ni) == 0) {
685			if (selbs == NULL)
686				selbs = ni;
687			else if (ieee80211_node_compare(ic, ni, selbs) > 0) {
688				ieee80211_unref_node(&selbs);
689				selbs = ni;
690			} else
691				ieee80211_unref_node(&ni);
692		} else {
693			ieee80211_unref_node(&ni);
694		}
695	}
696	if (selbs == NULL)
697		goto notfound;
698	if (!ieee80211_sta_join(ic, selbs)) {
699		ieee80211_unref_node(&selbs);
700		goto notfound;
701	}
702}
703
704/*
705 * Handle 802.11 ad hoc network merge.  The
706 * convention, set by the Wireless Ethernet Compatibility Alliance
707 * (WECA), is that an 802.11 station will change its BSSID to match
708 * the "oldest" 802.11 ad hoc network, on the same channel, that
709 * has the station's desired SSID.  The "oldest" 802.11 network
710 * sends beacons with the greatest TSF timestamp.
711 *
712 * The caller is assumed to validate TSF's before attempting a merge.
713 *
714 * Return !0 if the BSSID changed, 0 otherwise.
715 */
716int
717ieee80211_ibss_merge(struct ieee80211com *ic, struct ieee80211_node *ni)
718{
719
720	if (IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
721		/* unchanged, nothing to do */
722		return 0;
723	}
724	if (ieee80211_match_bss(ic, ni) != 0) {	/* capabilities mismatch */
725		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
726		    "%s: merge failed, capabilities mismatch\n", __func__);
727		ic->ic_stats.is_ibss_capmismatch++;
728		return 0;
729	}
730	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
731		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
732		ether_sprintf(ni->ni_bssid),
733		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
734		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
735		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
736	);
737	return ieee80211_sta_join(ic, ni);
738}
739
740/*
741 * Join the specified IBSS/BSS network.  The node is assumed to
742 * be passed in with a held reference.
743 */
744int
745ieee80211_sta_join(struct ieee80211com *ic, struct ieee80211_node *selbs)
746{
747	struct ieee80211_node *obss;
748
749	if (ic->ic_opmode == IEEE80211_M_IBSS) {
750		/*
751		 * Check rate set before committing to this node.
752		 */
753		ieee80211_fix_rate(ic, selbs, IEEE80211_F_DOFRATE |
754		    IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
755		if (selbs->ni_rates.rs_nrates == 0) {
756			selbs->ni_fails++;
757			ic->ic_stats.is_ibss_norate++;
758			return 0;
759		}
760		/*
761		 * Create the neighbor table; it will already
762		 * exist if we are simply switching mastership.
763		 */
764		if (ic->ic_sta == NULL) {
765			ic->ic_sta = ieee80211_node_table_alloc(ic,
766					"neighbor", ic->ic_inact_run,
767					ieee80211_timeout_stations);
768			if (ic->ic_sta == NULL) {
769				/*
770				 * Should remain in SCAN state and retry.
771				 */
772				/* XXX stat+msg */
773				return 0;
774			}
775		}
776	}
777
778	/*
779	 * Committed to selbs, setup state.
780	 */
781	obss = ic->ic_bss;
782	ic->ic_bss = selbs;
783	if (obss != NULL)
784		ieee80211_free_node(obss);
785	/*
786	 * Set the erp state (mostly the slot time) to deal with
787	 * the auto-select case; this should be redundant if the
788	 * mode is locked.
789	 */
790	ic->ic_curmode = ieee80211_chan2mode(ic, selbs->ni_chan);
791	ieee80211_reset_erp(ic);
792	ieee80211_wme_initparams(ic);
793	if (ic->ic_opmode == IEEE80211_M_IBSS)
794		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
795	else
796		ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
797	return 1;
798}
799
800/*
801 * Leave the specified IBSS/BSS network.  The node is assumed to
802 * be passed in with a held reference.
803 */
804void
805ieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
806{
807	ic->ic_node_cleanup(ni);
808	ieee80211_notify_node_leave(ic, ni);
809}
810
811static struct ieee80211_node *
812node_alloc(struct ieee80211_node_table *nt)
813{
814	struct ieee80211_node *ni;
815
816	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
817		M_80211_NODE, M_NOWAIT | M_ZERO);
818	return ni;
819}
820
821/*
822 * Reclaim any resources in a node and reset any critical
823 * state.  Typically nodes are free'd immediately after,
824 * but in some cases the storage may be reused so we need
825 * to insure consistent state (should probably fix that).
826 */
827static void
828node_cleanup(struct ieee80211_node *ni)
829{
830#define	N(a)	(sizeof(a)/sizeof(a[0]))
831	struct ieee80211com *ic = ni->ni_ic;
832	int i, qlen;
833
834	/* NB: preserve ni_table */
835	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
836		ic->ic_ps_sta--;
837		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
838		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
839		    "[%s] power save mode off, %u sta's in ps mode\n",
840		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
841	}
842
843	/*
844	 * Drain power save queue and, if needed, clear TIM.
845	 */
846	IEEE80211_NODE_SAVEQ_DRAIN(ni, qlen);
847	if (qlen != 0 && ic->ic_set_tim != NULL)
848		ic->ic_set_tim(ic, ni, 0);
849
850	ni->ni_associd = 0;
851	if (ni->ni_challenge != NULL) {
852		FREE(ni->ni_challenge, M_DEVBUF);
853		ni->ni_challenge = NULL;
854	}
855	/*
856	 * Preserve SSID, WPA, and WME ie's so the bss node is
857	 * reusable during a re-auth/re-assoc state transition.
858	 * If we remove these data they will not be recreated
859	 * because they come from a probe-response or beacon frame
860	 * which cannot be expected prior to the association-response.
861	 * This should not be an issue when operating in other modes
862	 * as stations leaving always go through a full state transition
863	 * which will rebuild this state.
864	 *
865	 * XXX does this leave us open to inheriting old state?
866	 */
867	for (i = 0; i < N(ni->ni_rxfrag); i++)
868		if (ni->ni_rxfrag[i] != NULL) {
869			m_freem(ni->ni_rxfrag[i]);
870			ni->ni_rxfrag[i] = NULL;
871		}
872	ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
873#undef N
874}
875
876static void
877node_free(struct ieee80211_node *ni)
878{
879	struct ieee80211com *ic = ni->ni_ic;
880
881	ic->ic_node_cleanup(ni);
882	if (ni->ni_wpa_ie != NULL)
883		FREE(ni->ni_wpa_ie, M_DEVBUF);
884	if (ni->ni_wme_ie != NULL)
885		FREE(ni->ni_wme_ie, M_DEVBUF);
886	IEEE80211_NODE_SAVEQ_DESTROY(ni);
887	FREE(ni, M_80211_NODE);
888}
889
890static u_int8_t
891node_getrssi(const struct ieee80211_node *ni)
892{
893	return ni->ni_rssi;
894}
895
896static void
897ieee80211_setup_node(struct ieee80211_node_table *nt,
898	struct ieee80211_node *ni, const u_int8_t *macaddr)
899{
900	struct ieee80211com *ic = nt->nt_ic;
901	int hash;
902
903	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
904		"%s %s in %s table\n", __func__,
905		ether_sprintf(macaddr), nt->nt_name);
906
907	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
908	hash = IEEE80211_NODE_HASH(macaddr);
909	ieee80211_node_initref(ni);		/* mark referenced */
910	ni->ni_chan = IEEE80211_CHAN_ANYC;
911	ni->ni_authmode = IEEE80211_AUTH_OPEN;
912	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
913	ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
914	ni->ni_inact = ni->ni_inact_reload = nt->nt_inact_init;
915	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
916
917	IEEE80211_NODE_LOCK(nt);
918	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
919	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
920	ni->ni_table = nt;
921	ni->ni_ic = ic;
922	IEEE80211_NODE_UNLOCK(nt);
923}
924
925struct ieee80211_node *
926ieee80211_alloc_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
927{
928	struct ieee80211com *ic = nt->nt_ic;
929	struct ieee80211_node *ni;
930
931	ni = ic->ic_node_alloc(nt);
932	if (ni != NULL)
933		ieee80211_setup_node(nt, ni, macaddr);
934	else
935		ic->ic_stats.is_rx_nodealloc++;
936	return ni;
937}
938
939struct ieee80211_node *
940ieee80211_dup_bss(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
941{
942	struct ieee80211com *ic = nt->nt_ic;
943	struct ieee80211_node *ni;
944
945	ni = ic->ic_node_alloc(nt);
946	if (ni != NULL) {
947		ieee80211_setup_node(nt, ni, macaddr);
948		/*
949		 * Inherit from ic_bss.
950		 */
951		ni->ni_authmode = ic->ic_bss->ni_authmode;
952		ni->ni_txpower = ic->ic_bss->ni_txpower;
953		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
954		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
955		ieee80211_set_chan(ic, ni, ic->ic_bss->ni_chan);
956		ni->ni_rsn = ic->ic_bss->ni_rsn;
957	} else
958		ic->ic_stats.is_rx_nodealloc++;
959	return ni;
960}
961
962static struct ieee80211_node *
963#ifdef IEEE80211_DEBUG_REFCNT
964_ieee80211_find_node_debug(struct ieee80211_node_table *nt,
965	const u_int8_t *macaddr, const char *func, int line)
966#else
967_ieee80211_find_node(struct ieee80211_node_table *nt,
968	const u_int8_t *macaddr)
969#endif
970{
971	struct ieee80211_node *ni;
972	int hash;
973
974	IEEE80211_NODE_LOCK_ASSERT(nt);
975
976	hash = IEEE80211_NODE_HASH(macaddr);
977	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
978		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
979			ieee80211_ref_node(ni);	/* mark referenced */
980#ifdef IEEE80211_DEBUG_REFCNT
981			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
982			    "%s (%s:%u) %s refcnt %d\n", __func__, func, line,
983			     ether_sprintf(ni->ni_macaddr),
984			     ieee80211_node_refcnt(ni));
985#endif
986			return ni;
987		}
988	}
989	return NULL;
990}
991#ifdef IEEE80211_DEBUG_REFCNT
992#define	_ieee80211_find_node(nt, mac) \
993	_ieee80211_find_node_debug(nt, mac, func, line)
994#endif
995
996struct ieee80211_node *
997#ifdef IEEE80211_DEBUG_REFCNT
998ieee80211_find_node_debug(struct ieee80211_node_table *nt,
999	const u_int8_t *macaddr, const char *func, int line)
1000#else
1001ieee80211_find_node(struct ieee80211_node_table *nt, const u_int8_t *macaddr)
1002#endif
1003{
1004	struct ieee80211_node *ni;
1005
1006	IEEE80211_NODE_LOCK(nt);
1007	ni = _ieee80211_find_node(nt, macaddr);
1008	IEEE80211_NODE_UNLOCK(nt);
1009	return ni;
1010}
1011
1012/*
1013 * Fake up a node; this handles node discovery in adhoc mode.
1014 * Note that for the driver's benefit we we treat this like
1015 * an association so the driver has an opportunity to setup
1016 * it's private state.
1017 */
1018struct ieee80211_node *
1019ieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
1020	const u_int8_t macaddr[IEEE80211_ADDR_LEN])
1021{
1022	struct ieee80211com *ic = nt->nt_ic;
1023	struct ieee80211_node *ni;
1024
1025	ni = ieee80211_dup_bss(nt, macaddr);
1026	if (ni != NULL) {
1027		/* XXX no rate negotiation; just dup */
1028		ni->ni_rates = ic->ic_bss->ni_rates;
1029		if (ic->ic_newassoc)
1030			ic->ic_newassoc(ic, ni, 1);
1031		/* XXX not right for 802.1x/WPA */
1032		ieee80211_node_authorize(ic, ni);
1033		ieee80211_ref_node(ni);		/* hold reference */
1034	}
1035	return ni;
1036}
1037
1038/*
1039 * Locate the node for sender, track state, and then pass the
1040 * (referenced) node up to the 802.11 layer for its use.  We
1041 * are required to pass some node so we fall back to ic_bss
1042 * when this frame is from an unknown sender.  The 802.11 layer
1043 * knows this means the sender wasn't in the node table and
1044 * acts accordingly.
1045 */
1046struct ieee80211_node *
1047#ifdef IEEE80211_DEBUG_REFCNT
1048ieee80211_find_rxnode_debug(struct ieee80211com *ic,
1049	const struct ieee80211_frame_min *wh, const char *func, int line)
1050#else
1051ieee80211_find_rxnode(struct ieee80211com *ic,
1052	const struct ieee80211_frame_min *wh)
1053#endif
1054{
1055#define	IS_CTL(wh) \
1056	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1057#define	IS_PSPOLL(wh) \
1058	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
1059	struct ieee80211_node_table *nt;
1060	struct ieee80211_node *ni;
1061
1062	/* XXX may want scanned nodes in the neighbor table for adhoc */
1063	if (ic->ic_opmode == IEEE80211_M_STA ||
1064	    ic->ic_opmode == IEEE80211_M_MONITOR ||
1065	    (ic->ic_flags & IEEE80211_F_SCAN))
1066		nt = &ic->ic_scan;
1067	else
1068		nt = ic->ic_sta;
1069	/* XXX check ic_bss first in station mode */
1070	/* XXX 4-address frames? */
1071	IEEE80211_NODE_LOCK(nt);
1072	if (IS_CTL(wh) && !IS_PSPOLL(wh) /*&& !IS_RTS(ah)*/)
1073		ni = _ieee80211_find_node(nt, wh->i_addr1);
1074	else
1075		ni = _ieee80211_find_node(nt, wh->i_addr2);
1076	IEEE80211_NODE_UNLOCK(nt);
1077
1078	return (ni != NULL ? ni : ieee80211_ref_node(ic->ic_bss));
1079#undef IS_PSPOLL
1080#undef IS_CTL
1081}
1082
1083/*
1084 * Return a reference to the appropriate node for sending
1085 * a data frame.  This handles node discovery in adhoc networks.
1086 */
1087struct ieee80211_node *
1088#ifdef IEEE80211_DEBUG_REFCNT
1089ieee80211_find_txnode_debug(struct ieee80211com *ic, const u_int8_t *macaddr,
1090	const char *func, int line)
1091#else
1092ieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr)
1093#endif
1094{
1095	struct ieee80211_node_table *nt = ic->ic_sta;
1096	struct ieee80211_node *ni;
1097
1098	/*
1099	 * The destination address should be in the node table
1100	 * unless we are operating in station mode or this is a
1101	 * multicast/broadcast frame.
1102	 */
1103	if (nt == NULL || IEEE80211_IS_MULTICAST(macaddr))
1104		return ieee80211_ref_node(ic->ic_bss);
1105
1106	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1107	IEEE80211_NODE_LOCK(nt);
1108	ni = _ieee80211_find_node(nt, macaddr);
1109	IEEE80211_NODE_UNLOCK(nt);
1110
1111	if (ni == NULL) {
1112		if (ic->ic_opmode == IEEE80211_M_IBSS ||
1113		    ic->ic_opmode == IEEE80211_M_AHDEMO)
1114			ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
1115		else {
1116			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
1117				"[%s] no node, discard frame (%s)\n",
1118				ether_sprintf(macaddr), __func__);
1119			ic->ic_stats.is_tx_nonode++;
1120		}
1121	}
1122	return ni;
1123}
1124
1125/*
1126 * Like find but search based on the channel too.
1127 */
1128struct ieee80211_node *
1129#ifdef IEEE80211_DEBUG_REFCNT
1130ieee80211_find_node_with_channel_debug(struct ieee80211_node_table *nt,
1131	const u_int8_t *macaddr, struct ieee80211_channel *chan,
1132	const char *func, int line)
1133#else
1134ieee80211_find_node_with_channel(struct ieee80211_node_table *nt,
1135	const u_int8_t *macaddr, struct ieee80211_channel *chan)
1136#endif
1137{
1138	struct ieee80211_node *ni;
1139	int hash;
1140
1141	hash = IEEE80211_NODE_HASH(macaddr);
1142	IEEE80211_NODE_LOCK(nt);
1143	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1144		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1145		    ni->ni_chan == chan) {
1146			ieee80211_ref_node(ni);		/* mark referenced */
1147			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1148#ifdef IEEE80211_DEBUG_REFCNT
1149			    "%s (%s:%u) %s refcnt %d\n", __func__, func, line,
1150#else
1151			    "%s %s refcnt %d\n", __func__,
1152#endif
1153			     ether_sprintf(ni->ni_macaddr),
1154			     ieee80211_node_refcnt(ni));
1155			break;
1156		}
1157	}
1158	IEEE80211_NODE_UNLOCK(nt);
1159	return ni;
1160}
1161
1162/*
1163 * Like find but search based on the ssid too.
1164 */
1165struct ieee80211_node *
1166#ifdef IEEE80211_DEBUG_REFCNT
1167ieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
1168	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid,
1169	const char *func, int line)
1170#else
1171ieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
1172	const u_int8_t *macaddr, u_int ssidlen, const u_int8_t *ssid)
1173#endif
1174{
1175	struct ieee80211com *ic = nt->nt_ic;
1176	struct ieee80211_node *ni;
1177	int hash;
1178
1179	hash = IEEE80211_NODE_HASH(macaddr);
1180	IEEE80211_NODE_LOCK(nt);
1181	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1182		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1183		    ni->ni_esslen == ic->ic_des_esslen &&
1184		    memcmp(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen) == 0) {
1185			ieee80211_ref_node(ni);		/* mark referenced */
1186			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1187#ifdef IEEE80211_DEBUG_REFCNT
1188			    "%s (%s:%u) %s refcnt %d\n", __func__, func, line,
1189#else
1190			    "%s %s refcnt %d\n", __func__,
1191#endif
1192			     ether_sprintf(ni->ni_macaddr),
1193			     ieee80211_node_refcnt(ni));
1194			break;
1195		}
1196	}
1197	IEEE80211_NODE_UNLOCK(nt);
1198	return ni;
1199}
1200
1201
1202static void
1203_ieee80211_free_node(struct ieee80211_node *ni)
1204{
1205	struct ieee80211com *ic = ni->ni_ic;
1206	struct ieee80211_node_table *nt = ni->ni_table;
1207
1208	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1209		"%s %s in %s table\n", __func__, ether_sprintf(ni->ni_macaddr),
1210		nt != NULL ? nt->nt_name : "<gone>");
1211
1212	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1213	if (nt != NULL) {
1214		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1215		LIST_REMOVE(ni, ni_hash);
1216	}
1217	ic->ic_node_free(ni);
1218}
1219
1220void
1221#ifdef IEEE80211_DEBUG_REFCNT
1222ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1223#else
1224ieee80211_free_node(struct ieee80211_node *ni)
1225#endif
1226{
1227	struct ieee80211_node_table *nt = ni->ni_table;
1228
1229#ifdef IEEE80211_DEBUG_REFCNT
1230	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1231		"%s (%s:%u) %s refcnt %d\n", __func__, func, line,
1232		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1233#endif
1234	if (ieee80211_node_dectestref(ni)) {
1235		/*
1236		 * Beware; if the node is marked gone then it's already
1237		 * been removed from the table and we cannot assume the
1238		 * table still exists.  Regardless, there's no need to lock
1239		 * the table.
1240		 */
1241		if (ni->ni_table != NULL) {
1242			IEEE80211_NODE_LOCK(nt);
1243			_ieee80211_free_node(ni);
1244			IEEE80211_NODE_UNLOCK(nt);
1245		} else
1246			_ieee80211_free_node(ni);
1247	}
1248}
1249
1250/*
1251 * Reclaim a node.  If this is the last reference count then
1252 * do the normal free work.  Otherwise remove it from the node
1253 * table and mark it gone by clearing the back-reference.
1254 */
1255static void
1256node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1257{
1258
1259	if (!ieee80211_node_dectestref(ni)) {
1260		/*
1261		 * Other references are present, just remove the
1262		 * node from the table so it cannot be found.  When
1263		 * the references are dropped storage will be
1264		 * reclaimed.  This normally only happens for ic_bss.
1265		 */
1266		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1267		LIST_REMOVE(ni, ni_hash);
1268		ni->ni_table = NULL;		/* clear reference */
1269	} else
1270		_ieee80211_free_node(ni);
1271}
1272
1273static void
1274ieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
1275{
1276	struct ieee80211com *ic = nt->nt_ic;
1277	struct ieee80211_node *ni;
1278
1279	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1280		"%s: free all nodes in %s table\n", __func__, nt->nt_name);
1281
1282	while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
1283		if (ni->ni_associd != 0) {
1284			if (ic->ic_auth->ia_node_leave != NULL)
1285				ic->ic_auth->ia_node_leave(ic, ni);
1286			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1287		}
1288		node_reclaim(nt, ni);
1289	}
1290	ieee80211_reset_erp(ic);
1291}
1292
1293static void
1294ieee80211_free_allnodes(struct ieee80211_node_table *nt)
1295{
1296
1297	IEEE80211_NODE_LOCK(nt);
1298	ieee80211_free_allnodes_locked(nt);
1299	IEEE80211_NODE_UNLOCK(nt);
1300}
1301
1302/*
1303 * Timeout entries in the scan cache.
1304 */
1305static void
1306ieee80211_timeout_scan_candidates(struct ieee80211_node_table *nt)
1307{
1308	struct ieee80211com *ic = nt->nt_ic;
1309	struct ieee80211_node *ni, *tni;
1310
1311	IEEE80211_NODE_LOCK(nt);
1312	ni = ic->ic_bss;
1313	/* XXX belongs elsewhere */
1314	if (ni->ni_rxfrag[0] != NULL && ticks > ni->ni_rxfragstamp + hz) {
1315		m_freem(ni->ni_rxfrag[0]);
1316		ni->ni_rxfrag[0] = NULL;
1317	}
1318	TAILQ_FOREACH_SAFE(ni, &nt->nt_node, ni_list, tni) {
1319		if (ni->ni_inact && --ni->ni_inact == 0) {
1320			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1321			    "[%s] scan candidate purged from cache "
1322			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
1323			    ieee80211_node_refcnt(ni)-1);
1324			node_reclaim(nt, ni);
1325		}
1326	}
1327	IEEE80211_NODE_UNLOCK(nt);
1328
1329	nt->nt_inact_timer = IEEE80211_INACT_WAIT;
1330}
1331
1332/*
1333 * Timeout inactive stations and do related housekeeping.
1334 * Note that we cannot hold the node lock while sending a
1335 * frame as this would lead to a LOR.  Instead we use a
1336 * generation number to mark nodes that we've scanned and
1337 * drop the lock and restart a scan if we have to time out
1338 * a node.  Since we are single-threaded by virtue of
1339 * controlling the inactivity timer we can be sure this will
1340 * process each node only once.
1341 */
1342static void
1343ieee80211_timeout_stations(struct ieee80211_node_table *nt)
1344{
1345	struct ieee80211com *ic = nt->nt_ic;
1346	struct ieee80211_node *ni;
1347	u_int gen;
1348
1349	IEEE80211_SCAN_LOCK(nt);
1350	gen = nt->nt_scangen++;
1351	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1352		"%s: sta scangen %u\n", __func__, gen);
1353restart:
1354	IEEE80211_NODE_LOCK(nt);
1355	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1356		if (ni->ni_scangen == gen)	/* previously handled */
1357			continue;
1358		ni->ni_scangen = gen;
1359		/*
1360		 * Free fragment if not needed anymore
1361		 * (last fragment older than 1s).
1362		 * XXX doesn't belong here
1363		 */
1364		if (ni->ni_rxfrag[0] != NULL &&
1365		    ticks > ni->ni_rxfragstamp + hz) {
1366			m_freem(ni->ni_rxfrag[0]);
1367			ni->ni_rxfrag[0] = NULL;
1368		}
1369		ni->ni_inact--;
1370		if (ni->ni_associd != 0) {
1371			/*
1372			 * Age frames on the power save queue. The
1373			 * aging interval is 4 times the listen
1374			 * interval specified by the station.  This
1375			 * number is factored into the age calculations
1376			 * when the frame is placed on the queue.  We
1377			 * store ages as time differences we can check
1378			 * and/or adjust only the head of the list.
1379			 */
1380			if (IEEE80211_NODE_SAVEQ_QLEN(ni) != 0) {
1381				struct mbuf *m;
1382				int discard = 0;
1383
1384				IEEE80211_NODE_SAVEQ_LOCK(ni);
1385				while (IF_POLL(&ni->ni_savedq, m) != NULL &&
1386				     M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
1387IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER, "[%s] discard frame, age %u\n", ether_sprintf(ni->ni_macaddr), M_AGE_GET(m));/*XXX*/
1388					_IEEE80211_NODE_SAVEQ_DEQUEUE_HEAD(ni, m);
1389					m_freem(m);
1390					discard++;
1391				}
1392				if (m != NULL)
1393					M_AGE_SUB(m, IEEE80211_INACT_WAIT);
1394				IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1395
1396				if (discard != 0) {
1397					IEEE80211_DPRINTF(ic,
1398					    IEEE80211_MSG_POWER,
1399					    "[%s] discard %u frames for age\n",
1400					    ether_sprintf(ni->ni_macaddr),
1401					    discard);
1402					IEEE80211_NODE_STAT_ADD(ni,
1403						ps_discard, discard);
1404					if (IEEE80211_NODE_SAVEQ_QLEN(ni) == 0)
1405						ic->ic_set_tim(ic, ni, 0);
1406				}
1407			}
1408			/*
1409			 * Probe the station before time it out.  We
1410			 * send a null data frame which may not be
1411			 * universally supported by drivers (need it
1412			 * for ps-poll support so it should be...).
1413			 */
1414			if (ni->ni_inact == ic->ic_inact_probe) {
1415				IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1416				    "[%s] probe station due to inactivity\n",
1417				    ether_sprintf(ni->ni_macaddr));
1418				IEEE80211_NODE_UNLOCK(nt);
1419				ieee80211_send_nulldata(ic, ni);
1420				/* XXX stat? */
1421				goto restart;
1422			}
1423		}
1424		if (ni->ni_inact <= 0) {
1425			IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1426			    "[%s] station timed out due to inactivity "
1427			    "(refcnt %u)\n", ether_sprintf(ni->ni_macaddr),
1428			    ieee80211_node_refcnt(ni));
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 (ni->ni_associd != 0) {
1446				IEEE80211_SEND_MGMT(ic, ni,
1447				    IEEE80211_FC0_SUBTYPE_DEAUTH,
1448				    IEEE80211_REASON_AUTH_EXPIRE);
1449			}
1450			ieee80211_node_leave(ic, ni);
1451			ic->ic_stats.is_node_timeout++;
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
1462void
1463ieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
1464{
1465	struct ieee80211_node *ni;
1466	u_int gen;
1467
1468	IEEE80211_SCAN_LOCK(nt);
1469	gen = nt->nt_scangen++;
1470
1471	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1472		"%s: sta scangen %u\n", __func__, gen);
1473restart:
1474	IEEE80211_NODE_LOCK(nt);
1475	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1476		if (ni->ni_scangen != gen) {
1477			ni->ni_scangen = gen;
1478			(void) ieee80211_ref_node(ni);
1479			IEEE80211_NODE_UNLOCK(nt);
1480			(*f)(arg, ni);
1481			ieee80211_free_node(ni);
1482			goto restart;
1483		}
1484	}
1485	IEEE80211_NODE_UNLOCK(nt);
1486
1487	IEEE80211_SCAN_UNLOCK(nt);
1488}
1489
1490void
1491ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1492{
1493	printf("0x%p: mac %s refcnt %d\n", ni,
1494		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
1495	printf("\tscangen %u authmode %u flags 0x%x\n",
1496		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
1497	printf("\tassocid 0x%x txpower %u vlan %u\n",
1498		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
1499	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
1500		ni->ni_txseqs[0],
1501		ni->ni_rxseqs[0] >> IEEE80211_SEQ_SEQ_SHIFT,
1502		ni->ni_rxseqs[0] & IEEE80211_SEQ_FRAG_MASK,
1503		ni->ni_rxfragstamp);
1504	printf("\trstamp %u rssi %u intval %u capinfo 0x%x\n",
1505		ni->ni_rstamp, ni->ni_rssi, ni->ni_intval, ni->ni_capinfo);
1506	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
1507		ether_sprintf(ni->ni_bssid),
1508		ni->ni_esslen, ni->ni_essid,
1509		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
1510	printf("\tfails %u inact %u txrate %u\n",
1511		ni->ni_fails, ni->ni_inact, ni->ni_txrate);
1512}
1513
1514void
1515ieee80211_dump_nodes(struct ieee80211_node_table *nt)
1516{
1517	ieee80211_iterate_nodes(nt,
1518		(ieee80211_iter_func *) ieee80211_dump_node, nt);
1519}
1520
1521/*
1522 * Handle a station joining an 11g network.
1523 */
1524static void
1525ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
1526{
1527
1528	/*
1529	 * Station isn't capable of short slot time.  Bump
1530	 * the count of long slot time stations and disable
1531	 * use of short slot time.  Note that the actual switch
1532	 * over to long slot time use may not occur until the
1533	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
1534	 */
1535	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
1536		ic->ic_longslotsta++;
1537		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1538		    "[%s] station needs long slot time, count %d\n",
1539		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
1540		/* XXX vap's w/ conflicting needs won't work */
1541		ieee80211_set_shortslottime(ic, 0);
1542	}
1543	/*
1544	 * If the new station is not an ERP station
1545	 * then bump the counter and enable protection
1546	 * if configured.
1547	 */
1548	if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
1549		ic->ic_nonerpsta++;
1550		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1551		    "[%s] station is !ERP, %d non-ERP stations associated\n",
1552		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
1553		/*
1554		 * If protection is configured, enable it.
1555		 */
1556		if (ic->ic_protmode != IEEE80211_PROT_NONE) {
1557			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1558			    "%s: enable use of protection\n", __func__);
1559			ic->ic_flags |= IEEE80211_F_USEPROT;
1560		}
1561		/*
1562		 * If station does not support short preamble
1563		 * then we must enable use of Barker preamble.
1564		 */
1565		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
1566			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1567			    "[%s] station needs long preamble\n",
1568			    ether_sprintf(ni->ni_macaddr));
1569			ic->ic_flags |= IEEE80211_F_USEBARKER;
1570			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1571		}
1572	} else
1573		ni->ni_flags |= IEEE80211_NODE_ERP;
1574}
1575
1576void
1577ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
1578{
1579	int newassoc;
1580
1581	if (ni->ni_associd == 0) {
1582		u_int16_t aid;
1583
1584		/*
1585		 * It would be good to search the bitmap
1586		 * more efficiently, but this will do for now.
1587		 */
1588		for (aid = 1; aid < ic->ic_max_aid; aid++) {
1589			if (!IEEE80211_AID_ISSET(aid,
1590			    ic->ic_aid_bitmap))
1591				break;
1592		}
1593		if (aid >= ic->ic_max_aid) {
1594			IEEE80211_SEND_MGMT(ic, ni, resp,
1595			    IEEE80211_REASON_ASSOC_TOOMANY);
1596			ieee80211_node_leave(ic, ni);
1597			return;
1598		}
1599		ni->ni_associd = aid | 0xc000;
1600		IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
1601		ic->ic_sta_assoc++;
1602		newassoc = 1;
1603		if (ic->ic_curmode == IEEE80211_MODE_11G)
1604			ieee80211_node_join_11g(ic, ni);
1605	} else
1606		newassoc = 0;
1607
1608	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
1609	    "[%s] station %s associated at aid %d\n",
1610	    ether_sprintf(ni->ni_macaddr), newassoc ? "newly" : "already",
1611	    IEEE80211_NODE_AID(ni));
1612
1613	/* give driver a chance to setup state like ni_txrate */
1614	if (ic->ic_newassoc)
1615		ic->ic_newassoc(ic, ni, newassoc);
1616	ni->ni_inact_reload = ic->ic_inact_run;
1617	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
1618	/* tell the authenticator about new station */
1619	if (ic->ic_auth->ia_node_join != NULL)
1620		ic->ic_auth->ia_node_join(ic, ni);
1621	ieee80211_notify_node_join(ic, ni, newassoc);
1622}
1623
1624/*
1625 * Handle a station leaving an 11g network.
1626 */
1627static void
1628ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
1629{
1630
1631	KASSERT(ic->ic_curmode == IEEE80211_MODE_11G,
1632		("not in 11g, curmode %x", ic->ic_curmode));
1633
1634	/*
1635	 * If a long slot station do the slot time bookkeeping.
1636	 */
1637	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
1638		KASSERT(ic->ic_longslotsta > 0,
1639		    ("bogus long slot station count %d", ic->ic_longslotsta));
1640		ic->ic_longslotsta--;
1641		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1642		    "[%s] long slot time station leaves, count now %d\n",
1643		    ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta);
1644		if (ic->ic_longslotsta == 0) {
1645			/*
1646			 * Re-enable use of short slot time if supported
1647			 * and not operating in IBSS mode (per spec).
1648			 */
1649			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
1650			    ic->ic_opmode != IEEE80211_M_IBSS) {
1651				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1652				    "%s: re-enable use of short slot time\n",
1653				    __func__);
1654				ieee80211_set_shortslottime(ic, 1);
1655			}
1656		}
1657	}
1658	/*
1659	 * If a non-ERP station do the protection-related bookkeeping.
1660	 */
1661	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
1662		KASSERT(ic->ic_nonerpsta > 0,
1663		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
1664		ic->ic_nonerpsta--;
1665		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1666		    "[%s] non-ERP station leaves, count now %d\n",
1667		    ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta);
1668		if (ic->ic_nonerpsta == 0) {
1669			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1670				"%s: disable use of protection\n", __func__);
1671			ic->ic_flags &= ~IEEE80211_F_USEPROT;
1672			/* XXX verify mode? */
1673			if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
1674				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1675				    "%s: re-enable use of short preamble\n",
1676				    __func__);
1677				ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1678				ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1679			}
1680		}
1681	}
1682}
1683
1684/*
1685 * Handle bookkeeping for station deauthentication/disassociation
1686 * when operating as an ap.
1687 */
1688void
1689ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
1690{
1691
1692	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
1693	    "[%s] station with aid %d leaves\n",
1694	    ether_sprintf(ni->ni_macaddr), IEEE80211_NODE_AID(ni));
1695
1696	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
1697		ic->ic_opmode == IEEE80211_M_IBSS ||
1698		ic->ic_opmode == IEEE80211_M_AHDEMO,
1699		("unexpected operating mode %u", ic->ic_opmode));
1700	/*
1701	 * If node wasn't previously associated all
1702	 * we need to do is reclaim the reference.
1703	 */
1704	/* XXX ibss mode bypasses 11g and notification */
1705	if (ni->ni_associd == 0)
1706		goto done;
1707	/*
1708	 * Tell the authenticator the station is leaving.
1709	 * Note that we must do this before yanking the
1710	 * association id as the authenticator uses the
1711	 * associd to locate it's state block.
1712	 */
1713	if (ic->ic_auth->ia_node_leave != NULL)
1714		ic->ic_auth->ia_node_leave(ic, ni);
1715	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1716	ni->ni_associd = 0;
1717	ic->ic_sta_assoc--;
1718
1719	if (ic->ic_curmode == IEEE80211_MODE_11G)
1720		ieee80211_node_leave_11g(ic, ni);
1721	/*
1722	 * Cleanup station state.  In particular clear various
1723	 * state that might otherwise be reused if the node
1724	 * is reused before the reference count goes to zero
1725	 * (and memory is reclaimed).
1726	 */
1727	ieee80211_sta_leave(ic, ni);
1728done:
1729	ni->ni_inact_reload = ic->ic_inact_init;	/* just in case */
1730	ieee80211_free_node(ni);
1731}
1732
1733u_int8_t
1734ieee80211_getrssi(struct ieee80211com *ic)
1735{
1736#define	NZ(x)	((x) == 0 ? 1 : (x))
1737	struct ieee80211_node_table *nt = ic->ic_sta;
1738	u_int32_t rssi_samples, rssi_total;
1739	struct ieee80211_node *ni;
1740
1741	rssi_total = 0;
1742	rssi_samples = 0;
1743	switch (ic->ic_opmode) {
1744	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
1745		nt = ic->ic_sta;
1746		if (nt == NULL)
1747			break;
1748		/* XXX locking */
1749		TAILQ_FOREACH(ni, &ic->ic_sta->nt_node, ni_list)
1750			if (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) {
1751				rssi_samples++;
1752				rssi_total += ic->ic_node_getrssi(ni);
1753			}
1754		break;
1755	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
1756		nt = ic->ic_sta;
1757		if (nt == NULL)
1758			break;
1759		/* XXX locking */
1760		TAILQ_FOREACH(ni, &ic->ic_sta->nt_node, ni_list) {
1761			rssi_samples++;
1762			rssi_total += ic->ic_node_getrssi(ni);
1763		}
1764		break;
1765	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
1766		nt = ic->ic_sta;
1767		if (nt == NULL)
1768			break;
1769		/* XXX locking */
1770		TAILQ_FOREACH(ni, &ic->ic_sta->nt_node, ni_list)
1771			if (IEEE80211_AID(ni->ni_associd) != 0) {
1772				rssi_samples++;
1773				rssi_total += ic->ic_node_getrssi(ni);
1774			}
1775		break;
1776	case IEEE80211_M_MONITOR:	/* XXX */
1777	case IEEE80211_M_STA:		/* use stats from associated ap */
1778	default:
1779		if (ic->ic_bss != NULL)
1780			rssi_total = ic->ic_node_getrssi(ic->ic_bss);
1781		rssi_samples = 1;
1782		break;
1783	}
1784	return rssi_total / NZ(rssi_samples);
1785#undef NZ
1786}
1787
1788/*
1789 * Indicate whether there are frames queued for a station in power-save mode.
1790 */
1791static void
1792ieee80211_set_tim(struct ieee80211com *ic, struct ieee80211_node *ni, int set)
1793{
1794	u_int16_t aid;
1795
1796	KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP ||
1797		ic->ic_opmode == IEEE80211_M_IBSS,
1798		("operating mode %u", ic->ic_opmode));
1799
1800	aid = IEEE80211_AID(ni->ni_associd);
1801	KASSERT(aid < ic->ic_max_aid,
1802		("bogus aid %u, max %u", aid, ic->ic_max_aid));
1803
1804	IEEE80211_BEACON_LOCK(ic);
1805	if (set != (isset(ic->ic_tim_bitmap, aid) != 0)) {
1806		if (set) {
1807			setbit(ic->ic_tim_bitmap, aid);
1808			ic->ic_ps_pending++;
1809		} else {
1810			clrbit(ic->ic_tim_bitmap, aid);
1811			ic->ic_ps_pending--;
1812		}
1813		ic->ic_flags |= IEEE80211_F_TIMUPDATE;
1814	}
1815	IEEE80211_BEACON_UNLOCK(ic);
1816}
1817
1818/*
1819 * Node table support.
1820 */
1821
1822static void
1823ieee80211_node_table_init(struct ieee80211com *ic,
1824	struct ieee80211_node_table *nt,
1825	const char *name, int inact,
1826	void (*timeout)(struct ieee80211_node_table *))
1827{
1828
1829	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1830		"%s %s table, inact %u\n", __func__, name, inact);
1831
1832	nt->nt_ic = ic;
1833	/* XXX need unit */
1834	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
1835	IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
1836	TAILQ_INIT(&nt->nt_node);
1837	nt->nt_name = name;
1838	nt->nt_scangen = 1;
1839	nt->nt_inact_init = inact;
1840	nt->nt_timeout = timeout;
1841}
1842
1843static struct ieee80211_node_table *
1844ieee80211_node_table_alloc(struct ieee80211com *ic,
1845	const char *name, int inact,
1846	void (*timeout)(struct ieee80211_node_table *))
1847{
1848	struct ieee80211_node_table *nt;
1849
1850	MALLOC(nt, struct ieee80211_node_table *,
1851		sizeof(struct ieee80211_node_table),
1852		M_DEVBUF, M_NOWAIT | M_ZERO);
1853	if (nt == NULL) {
1854		printf("%s: no memory node table!\n", __func__);
1855		return NULL;
1856	}
1857	ieee80211_node_table_init(ic, nt, name, inact, timeout);
1858	return nt;
1859}
1860
1861void
1862ieee80211_node_table_reset(struct ieee80211_node_table *nt)
1863{
1864
1865	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1866		"%s %s table\n", __func__, nt->nt_name);
1867
1868	IEEE80211_NODE_LOCK(nt);
1869	nt->nt_inact_timer = 0;
1870	ieee80211_free_allnodes_locked(nt);
1871	IEEE80211_NODE_UNLOCK(nt);
1872}
1873
1874static void
1875ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
1876{
1877
1878	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1879		"%s %s table\n", __func__, nt->nt_name);
1880
1881	ieee80211_free_allnodes_locked(nt);
1882	IEEE80211_SCAN_LOCK_DESTROY(nt);
1883	IEEE80211_NODE_LOCK_DESTROY(nt);
1884}
1885
1886/*
1887 * NB: public for use in ieee80211_proto.c
1888 */
1889void
1890ieee80211_node_table_free(struct ieee80211_node_table *nt)
1891{
1892
1893	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1894		"%s %s table\n", __func__, nt->nt_name);
1895
1896	IEEE80211_NODE_LOCK(nt);
1897	nt->nt_inact_timer = 0;
1898	ieee80211_node_table_cleanup(nt);
1899	FREE(nt, M_DEVBUF);
1900}
1901