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