ieee80211_node.c revision 172211
1/*-
2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2007 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 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_node.c 172211 2007-09-17 19:07:24Z sam $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/mbuf.h>
33#include <sys/malloc.h>
34#include <sys/kernel.h>
35
36#include <sys/socket.h>
37
38#include <net/if.h>
39#include <net/if_media.h>
40#include <net/ethernet.h>
41
42#include <net80211/ieee80211_var.h>
43
44#include <net/bpf.h>
45
46/*
47 * Association id's are managed with a bit vector.
48 */
49#define	IEEE80211_AID_SET(b, w) \
50	((w)[IEEE80211_AID(b) / 32] |= (1 << (IEEE80211_AID(b) % 32)))
51#define	IEEE80211_AID_CLR(b, w) \
52	((w)[IEEE80211_AID(b) / 32] &= ~(1 << (IEEE80211_AID(b) % 32)))
53#define	IEEE80211_AID_ISSET(b, w) \
54	((w)[IEEE80211_AID(b) / 32] & (1 << (IEEE80211_AID(b) % 32)))
55
56#ifdef IEEE80211_DEBUG_REFCNT
57#define REFCNT_LOC "%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line
58#else
59#define REFCNT_LOC "%s %p<%s> refcnt %d\n", __func__
60#endif
61
62static int ieee80211_sta_join1(struct ieee80211_node *);
63
64static struct ieee80211_node *node_alloc(struct ieee80211_node_table *);
65static void node_cleanup(struct ieee80211_node *);
66static void node_free(struct ieee80211_node *);
67static int8_t node_getrssi(const struct ieee80211_node *);
68static void node_getsignal(const struct ieee80211_node *, int8_t *, int8_t *);
69
70static void ieee80211_setup_node(struct ieee80211_node_table *,
71		struct ieee80211_node *, const uint8_t *);
72static void _ieee80211_free_node(struct ieee80211_node *);
73
74static void ieee80211_node_table_init(struct ieee80211com *ic,
75	struct ieee80211_node_table *nt, const char *name,
76	int inact, int keymaxix);
77static void ieee80211_node_table_reset(struct ieee80211_node_table *);
78static void ieee80211_node_table_cleanup(struct ieee80211_node_table *nt);
79static void ieee80211_erp_timeout(struct ieee80211com *);
80
81MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
82
83void
84ieee80211_node_attach(struct ieee80211com *ic)
85{
86
87	ic->ic_node_alloc = node_alloc;
88	ic->ic_node_free = node_free;
89	ic->ic_node_cleanup = node_cleanup;
90	ic->ic_node_getrssi = node_getrssi;
91	ic->ic_node_getsignal = node_getsignal;
92
93	/* default station inactivity timer setings */
94	ic->ic_inact_init = IEEE80211_INACT_INIT;
95	ic->ic_inact_auth = IEEE80211_INACT_AUTH;
96	ic->ic_inact_run = IEEE80211_INACT_RUN;
97	ic->ic_inact_probe = IEEE80211_INACT_PROBE;
98
99	callout_init(&ic->ic_inact, CALLOUT_MPSAFE);
100
101	/* NB: driver should override */
102	ic->ic_max_aid = IEEE80211_AID_DEF;
103
104	ic->ic_flags_ext |= IEEE80211_FEXT_INACT; /* inactivity processing */
105}
106
107void
108ieee80211_node_lateattach(struct ieee80211com *ic)
109{
110	struct ieee80211_rsnparms *rsn;
111
112	if (ic->ic_max_aid > IEEE80211_AID_MAX)
113		ic->ic_max_aid = IEEE80211_AID_MAX;
114	MALLOC(ic->ic_aid_bitmap, uint32_t *,
115		howmany(ic->ic_max_aid, 32) * sizeof(uint32_t),
116		M_80211_NODE, M_NOWAIT | M_ZERO);
117	if (ic->ic_aid_bitmap == NULL) {
118		/* XXX no way to recover */
119		printf("%s: no memory for AID bitmap!\n", __func__);
120		ic->ic_max_aid = 0;
121	}
122
123	ieee80211_node_table_init(ic, &ic->ic_sta, "station",
124		IEEE80211_INACT_INIT, ic->ic_crypto.cs_max_keyix);
125
126	ieee80211_reset_bss(ic);
127	/*
128	 * Setup "global settings" in the bss node so that
129	 * each new station automatically inherits them.
130	 */
131	rsn = &ic->ic_bss->ni_rsn;
132	/* WEP, TKIP, and AES-CCM are always supported */
133	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_WEP;
134	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_TKIP;
135	rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_CCM;
136	if (ic->ic_caps & IEEE80211_C_AES)
137		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_AES_OCB;
138	if (ic->ic_caps & IEEE80211_C_CKIP)
139		rsn->rsn_ucastcipherset |= 1<<IEEE80211_CIPHER_CKIP;
140	/*
141	 * Default unicast cipher to WEP for 802.1x use.  If
142	 * WPA is enabled the management code will set these
143	 * values to reflect.
144	 */
145	rsn->rsn_ucastcipher = IEEE80211_CIPHER_WEP;
146	rsn->rsn_ucastkeylen = 104 / NBBY;
147	/*
148	 * WPA says the multicast cipher is the lowest unicast
149	 * cipher supported.  But we skip WEP which would
150	 * otherwise be used based on this criteria.
151	 */
152	rsn->rsn_mcastcipher = IEEE80211_CIPHER_TKIP;
153	rsn->rsn_mcastkeylen = 128 / NBBY;
154
155	/*
156	 * We support both WPA-PSK and 802.1x; the one used
157	 * is determined by the authentication mode and the
158	 * setting of the PSK state.
159	 */
160	rsn->rsn_keymgmtset = WPA_ASE_8021X_UNSPEC | WPA_ASE_8021X_PSK;
161	rsn->rsn_keymgmt = WPA_ASE_8021X_PSK;
162
163	ic->ic_auth = ieee80211_authenticator_get(ic->ic_bss->ni_authmode);
164}
165
166void
167ieee80211_node_detach(struct ieee80211com *ic)
168{
169
170	if (ic->ic_bss != NULL) {
171		ieee80211_free_node(ic->ic_bss);
172		ic->ic_bss = NULL;
173	}
174	ieee80211_node_table_cleanup(&ic->ic_sta);
175	if (ic->ic_aid_bitmap != NULL) {
176		FREE(ic->ic_aid_bitmap, M_80211_NODE);
177		ic->ic_aid_bitmap = NULL;
178	}
179}
180
181/*
182 * Port authorize/unauthorize interfaces for use by an authenticator.
183 */
184
185void
186ieee80211_node_authorize(struct ieee80211_node *ni)
187{
188	struct ieee80211com *ic = ni->ni_ic;
189
190	ni->ni_flags |= IEEE80211_NODE_AUTH;
191	ni->ni_inact_reload = ic->ic_inact_run;
192	ni->ni_inact = ni->ni_inact_reload;
193}
194
195void
196ieee80211_node_unauthorize(struct ieee80211_node *ni)
197{
198	struct ieee80211com *ic = ni->ni_ic;
199
200	ni->ni_flags &= ~IEEE80211_NODE_AUTH;
201	ni->ni_inact_reload = ic->ic_inact_auth;
202	if (ni->ni_inact > ni->ni_inact_reload)
203		ni->ni_inact = ni->ni_inact_reload;
204}
205
206/*
207 * Set/change the channel.  The rate set is also updated as
208 * to insure a consistent view by drivers.
209 */
210static void
211ieee80211_node_set_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
212{
213	struct ieee80211_channel *chan = ic->ic_bsschan;
214
215#if 0
216	KASSERT(chan != IEEE80211_CHAN_ANYC, ("bss channel not setup"));
217#else
218	if (chan == IEEE80211_CHAN_ANYC)	/* XXX while scanning */
219		chan = ic->ic_curchan;
220#endif
221	ni->ni_chan = chan;
222	if (IEEE80211_IS_CHAN_HT(chan)) {
223		/*
224		 * XXX Gotta be careful here; the rate set returned by
225		 * ieee80211_get_suprates is actually any HT rate
226		 * set so blindly copying it will be bad.  We must
227		 * install the legacy rate est in ni_rates and the
228		 * HT rate set in ni_htrates.
229		 */
230		ni->ni_htrates = *ieee80211_get_suphtrates(ic, chan);
231	}
232	ni->ni_rates = *ieee80211_get_suprates(ic, chan);
233}
234
235/*
236 * Probe the curent channel, if allowed, while scanning.
237 * If the channel is not marked passive-only then send
238 * a probe request immediately.  Otherwise mark state and
239 * listen for beacons on the channel; if we receive something
240 * then we'll transmit a probe request.
241 */
242void
243ieee80211_probe_curchan(struct ieee80211com *ic, int force)
244{
245	struct ifnet *ifp = ic->ic_ifp;
246
247	if ((ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0 || force) {
248		/*
249		 * XXX send both broadcast+directed probe request
250		 */
251		ieee80211_send_probereq(ic->ic_bss,
252			ic->ic_myaddr, ifp->if_broadcastaddr,
253			ifp->if_broadcastaddr,
254			ic->ic_des_ssid[0].ssid, ic->ic_des_ssid[0].len,
255			ic->ic_opt_ie, ic->ic_opt_ie_len);
256	} else
257		ic->ic_flags_ext |= IEEE80211_FEXT_PROBECHAN;
258}
259
260static __inline void
261copy_bss(struct ieee80211_node *nbss, const struct ieee80211_node *obss)
262{
263	/* propagate useful state */
264	nbss->ni_authmode = obss->ni_authmode;
265	nbss->ni_txpower = obss->ni_txpower;
266	nbss->ni_vlan = obss->ni_vlan;
267	nbss->ni_rsn = obss->ni_rsn;
268	/* XXX statistics? */
269}
270
271void
272ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
273{
274	struct ieee80211_node_table *nt;
275	struct ieee80211_node *ni;
276
277	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
278		"%s: creating ibss\n", __func__);
279
280	/*
281	 * Create the station/neighbor table.  Note that for adhoc
282	 * mode we make the initial inactivity timer longer since
283	 * we create nodes only through discovery and they typically
284	 * are long-lived associations.
285	 */
286	nt = &ic->ic_sta;
287	IEEE80211_NODE_LOCK(nt);
288	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
289		nt->nt_name = "station";
290		nt->nt_inact_init = ic->ic_inact_init;
291	} else {
292		nt->nt_name = "neighbor";
293		nt->nt_inact_init = ic->ic_inact_run;
294	}
295	IEEE80211_NODE_UNLOCK(nt);
296
297	ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr);
298	if (ni == NULL) {
299		/* XXX recovery? */
300		return;
301	}
302	IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
303	ni->ni_esslen = ic->ic_des_ssid[0].len;
304	memcpy(ni->ni_essid, ic->ic_des_ssid[0].ssid, ni->ni_esslen);
305	if (ic->ic_bss != NULL)
306		copy_bss(ni, ic->ic_bss);
307	ni->ni_intval = ic->ic_bintval;
308	if (ic->ic_flags & IEEE80211_F_PRIVACY)
309		ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
310	if (ic->ic_phytype == IEEE80211_T_FH) {
311		ni->ni_fhdwell = 200;	/* XXX */
312		ni->ni_fhindex = 1;
313	}
314	if (ic->ic_opmode == IEEE80211_M_IBSS) {
315		ic->ic_flags |= IEEE80211_F_SIBSS;
316		ni->ni_capinfo |= IEEE80211_CAPINFO_IBSS;	/* XXX */
317		if (ic->ic_flags & IEEE80211_F_DESBSSID)
318			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
319		else {
320			get_random_bytes(ni->ni_bssid, IEEE80211_ADDR_LEN);
321			/* clear group bit, add local bit */
322			ni->ni_bssid[0] = (ni->ni_bssid[0] &~ 0x01) | 0x02;
323		}
324	} else if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
325		if (ic->ic_flags & IEEE80211_F_DESBSSID)
326			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
327		else
328			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
329	}
330	/*
331	 * Fix the channel and related attributes.
332	 */
333	ic->ic_bsschan = chan;
334	ieee80211_node_set_chan(ic, ni);
335	ic->ic_curmode = ieee80211_chan2mode(chan);
336	/*
337	 * Do mode-specific rate setup.
338	 */
339	if (IEEE80211_IS_CHAN_FULL(chan)) {
340		if (IEEE80211_IS_CHAN_ANYG(chan)) {
341			/*
342			 * Use a mixed 11b/11g rate set.
343			 */
344			ieee80211_set11gbasicrates(&ni->ni_rates,
345				IEEE80211_MODE_11G);
346		} else if (IEEE80211_IS_CHAN_B(chan)) {
347			/*
348			 * Force pure 11b rate set.
349			 */
350			ieee80211_set11gbasicrates(&ni->ni_rates,
351				IEEE80211_MODE_11B);
352		}
353	}
354
355	(void) ieee80211_sta_join1(ieee80211_ref_node(ni));
356}
357
358/*
359 * Reset bss state on transition to the INIT state.
360 * Clear any stations from the table (they have been
361 * deauth'd) and reset the bss node (clears key, rate
362 * etc. state).
363 */
364void
365ieee80211_reset_bss(struct ieee80211com *ic)
366{
367	struct ieee80211_node *ni, *obss;
368
369	callout_drain(&ic->ic_inact);
370	ieee80211_node_table_reset(&ic->ic_sta);
371	ieee80211_reset_erp(ic);
372
373	ni = ieee80211_alloc_node(&ic->ic_sta, ic->ic_myaddr);
374	KASSERT(ni != NULL, ("unable to setup inital BSS node"));
375	obss = ic->ic_bss;
376	ic->ic_bss = ieee80211_ref_node(ni);
377	if (obss != NULL) {
378		copy_bss(ni, obss);
379		ni->ni_intval = ic->ic_bintval;
380		ieee80211_free_node(obss);
381	}
382}
383
384static int
385match_ssid(const struct ieee80211_node *ni,
386	int nssid, const struct ieee80211_scan_ssid ssids[])
387{
388	int i;
389
390	for (i = 0; i < nssid; i++) {
391		if (ni->ni_esslen == ssids[i].len &&
392		     memcmp(ni->ni_essid, ssids[i].ssid, ni->ni_esslen) == 0)
393			return 1;
394	}
395	return 0;
396}
397
398/*
399 * Test a node for suitability/compatibility.
400 */
401static int
402check_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
403{
404        uint8_t rate;
405
406	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
407		return 0;
408	if (ic->ic_opmode == IEEE80211_M_IBSS) {
409		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
410			return 0;
411	} else {
412		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
413			return 0;
414	}
415	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
416		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
417			return 0;
418	} else {
419		/* XXX does this mean privacy is supported or required? */
420		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
421			return 0;
422	}
423	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
424	    IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
425	if (rate & IEEE80211_RATE_BASIC)
426		return 0;
427	if (ic->ic_des_nssid != 0 &&
428	    !match_ssid(ni, ic->ic_des_nssid, ic->ic_des_ssid))
429		return 0;
430	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
431	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
432		return 0;
433	return 1;
434}
435
436#ifdef IEEE80211_DEBUG
437/*
438 * Display node suitability/compatibility.
439 */
440static void
441check_bss_debug(struct ieee80211com *ic, struct ieee80211_node *ni)
442{
443        uint8_t rate;
444        int fail;
445
446	fail = 0;
447	if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
448		fail |= 0x01;
449	if (ic->ic_opmode == IEEE80211_M_IBSS) {
450		if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
451			fail |= 0x02;
452	} else {
453		if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
454			fail |= 0x02;
455	}
456	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
457		if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
458			fail |= 0x04;
459	} else {
460		/* XXX does this mean privacy is supported or required? */
461		if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
462			fail |= 0x04;
463	}
464	rate = ieee80211_fix_rate(ni, &ni->ni_rates,
465	     IEEE80211_F_JOIN | IEEE80211_F_DONEGO | IEEE80211_F_DOFRATE);
466	if (rate & IEEE80211_RATE_BASIC)
467		fail |= 0x08;
468	if (ic->ic_des_nssid != 0 &&
469	    !match_ssid(ni, ic->ic_des_nssid, ic->ic_des_ssid))
470		fail |= 0x10;
471	if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
472	    !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
473		fail |= 0x20;
474
475	printf(" %c %s", fail ? '-' : '+', ether_sprintf(ni->ni_macaddr));
476	printf(" %s%c", ether_sprintf(ni->ni_bssid), fail & 0x20 ? '!' : ' ');
477	printf(" %3d%c",
478	    ieee80211_chan2ieee(ic, ni->ni_chan), fail & 0x01 ? '!' : ' ');
479	printf(" %+4d", ni->ni_rssi);
480	printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
481	    fail & 0x08 ? '!' : ' ');
482	printf(" %4s%c",
483	    (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
484	    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
485	    "????",
486	    fail & 0x02 ? '!' : ' ');
487	printf(" %3s%c ",
488	    (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?  "wep" : "no",
489	    fail & 0x04 ? '!' : ' ');
490	ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
491	printf("%s\n", fail & 0x10 ? "!" : "");
492}
493#endif /* IEEE80211_DEBUG */
494
495/*
496 * Handle 802.11 ad hoc network merge.  The
497 * convention, set by the Wireless Ethernet Compatibility Alliance
498 * (WECA), is that an 802.11 station will change its BSSID to match
499 * the "oldest" 802.11 ad hoc network, on the same channel, that
500 * has the station's desired SSID.  The "oldest" 802.11 network
501 * sends beacons with the greatest TSF timestamp.
502 *
503 * The caller is assumed to validate TSF's before attempting a merge.
504 *
505 * Return !0 if the BSSID changed, 0 otherwise.
506 */
507int
508ieee80211_ibss_merge(struct ieee80211_node *ni)
509{
510	struct ieee80211com *ic = ni->ni_ic;
511
512	if (ni == ic->ic_bss ||
513	    IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
514		/* unchanged, nothing to do */
515		return 0;
516	}
517	if (!check_bss(ic, ni)) {
518		/* capabilities mismatch */
519		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
520		    "%s: merge failed, capabilities mismatch\n", __func__);
521#ifdef IEEE80211_DEBUG
522		if (ieee80211_msg_assoc(ic))
523			check_bss_debug(ic, ni);
524#endif
525		ic->ic_stats.is_ibss_capmismatch++;
526		return 0;
527	}
528	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
529		"%s: new bssid %s: %s preamble, %s slot time%s\n", __func__,
530		ether_sprintf(ni->ni_bssid),
531		ic->ic_flags&IEEE80211_F_SHPREAMBLE ? "short" : "long",
532		ic->ic_flags&IEEE80211_F_SHSLOT ? "short" : "long",
533		ic->ic_flags&IEEE80211_F_USEPROT ? ", protection" : ""
534	);
535	return ieee80211_sta_join1(ieee80211_ref_node(ni));
536}
537
538/*
539 * Join the specified IBSS/BSS network.  The node is assumed to
540 * be passed in with a held reference.
541 */
542static int
543ieee80211_sta_join1(struct ieee80211_node *selbs)
544{
545	struct ieee80211com *ic = selbs->ni_ic;
546	struct ieee80211_node *obss;
547	int canreassoc;
548
549	if (ic->ic_opmode == IEEE80211_M_IBSS) {
550		struct ieee80211_node_table *nt;
551		/*
552		 * Fillin the neighbor table; it will already
553		 * exist if we are simply switching mastership.
554		 * XXX ic_sta always setup so this is unnecessary?
555		 */
556		nt = &ic->ic_sta;
557		IEEE80211_NODE_LOCK(nt);
558		nt->nt_name = "neighbor";
559		nt->nt_inact_init = ic->ic_inact_run;
560		IEEE80211_NODE_UNLOCK(nt);
561	}
562
563	/*
564	 * Committed to selbs, setup state.
565	 */
566	obss = ic->ic_bss;
567	/*
568	 * Check if old+new node have the same address in which
569	 * case we can reassociate when operating in sta mode.
570	 */
571	canreassoc = (obss != NULL &&
572		ic->ic_state == IEEE80211_S_RUN &&
573		IEEE80211_ADDR_EQ(obss->ni_macaddr, selbs->ni_macaddr));
574	ic->ic_bss = selbs;		/* NB: caller assumed to bump refcnt */
575	if (obss != NULL) {
576		copy_bss(selbs, obss);
577		ieee80211_free_node(obss);
578	}
579
580	/*
581	 * Delete unusable rates; we've already checked
582	 * that the negotiated rate set is acceptable.
583	 */
584	ieee80211_fix_rate(ic->ic_bss, &ic->ic_bss->ni_rates,
585		IEEE80211_F_DODEL | IEEE80211_F_JOIN);
586
587	ic->ic_bsschan = selbs->ni_chan;
588	ic->ic_curchan = ic->ic_bsschan;
589	ic->ic_curmode = ieee80211_chan2mode(ic->ic_curchan);
590	ic->ic_set_channel(ic);
591	/*
592	 * Set the erp state (mostly the slot time) to deal with
593	 * the auto-select case; this should be redundant if the
594	 * mode is locked.
595	 */
596	ieee80211_reset_erp(ic);
597	ieee80211_wme_initparams(ic);
598
599	if (ic->ic_opmode == IEEE80211_M_STA) {
600		if (canreassoc) {
601			/* Reassociate */
602			ieee80211_new_state(ic, IEEE80211_S_ASSOC, 1);
603		} else {
604			/*
605			 * Act as if we received a DEAUTH frame in case we
606			 * are invoked from the RUN state.  This will cause
607			 * us to try to re-authenticate if we are operating
608			 * as a station.
609			 */
610			ieee80211_new_state(ic, IEEE80211_S_AUTH,
611				IEEE80211_FC0_SUBTYPE_DEAUTH);
612		}
613	} else
614		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
615	return 1;
616}
617
618int
619ieee80211_sta_join(struct ieee80211com *ic,
620	const struct ieee80211_scan_entry *se)
621{
622	struct ieee80211_node *ni;
623
624	ni = ieee80211_alloc_node(&ic->ic_sta, se->se_macaddr);
625	if (ni == NULL) {
626		/* XXX msg */
627		return 0;
628	}
629	/*
630	 * Expand scan state into node's format.
631	 * XXX may not need all this stuff
632	 */
633	IEEE80211_ADDR_COPY(ni->ni_bssid, se->se_bssid);
634	ni->ni_esslen = se->se_ssid[1];
635	memcpy(ni->ni_essid, se->se_ssid+2, ni->ni_esslen);
636	ni->ni_rstamp = se->se_rstamp;
637	ni->ni_tstamp.tsf = se->se_tstamp.tsf;
638	ni->ni_intval = se->se_intval;
639	ni->ni_capinfo = se->se_capinfo;
640	/* XXX shift to 11n channel if htinfo present */
641	ni->ni_chan = se->se_chan;
642	ni->ni_timoff = se->se_timoff;
643	ni->ni_fhdwell = se->se_fhdwell;
644	ni->ni_fhindex = se->se_fhindex;
645	ni->ni_erp = se->se_erp;
646	ni->ni_rssi = se->se_rssi;
647	ni->ni_noise = se->se_noise;
648	if (se->se_htcap_ie != NULL)
649		ieee80211_ht_node_init(ni, se->se_htcap_ie);
650	if (se->se_htinfo_ie != NULL)
651		ieee80211_parse_htinfo(ni, se->se_htinfo_ie);
652	if (se->se_wpa_ie != NULL)
653		ieee80211_saveie(&ni->ni_wpa_ie, se->se_wpa_ie);
654	if (se->se_rsn_ie != NULL)
655		ieee80211_saveie(&ni->ni_rsn_ie, se->se_rsn_ie);
656	if (se->se_wme_ie != NULL)
657		ieee80211_saveie(&ni->ni_wme_ie, se->se_wme_ie);
658	if (se->se_ath_ie != NULL)
659		ieee80211_saveath(ni, se->se_ath_ie);
660
661	ic->ic_dtim_period = se->se_dtimperiod;
662	ic->ic_dtim_count = 0;
663
664	/* NB: must be after ni_chan is setup */
665	ieee80211_setup_rates(ni, se->se_rates, se->se_xrates,
666		IEEE80211_F_DOSORT);
667	if (se->se_htcap_ie != NULL)
668		ieee80211_setup_htrates(ni, se->se_htcap_ie, IEEE80211_F_JOIN);
669	if (se->se_htinfo_ie != NULL)
670		ieee80211_setup_basic_htrates(ni, se->se_htinfo_ie);
671
672	return ieee80211_sta_join1(ieee80211_ref_node(ni));
673}
674
675/*
676 * Leave the specified IBSS/BSS network.  The node is assumed to
677 * be passed in with a held reference.
678 */
679void
680ieee80211_sta_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
681{
682	ic->ic_node_cleanup(ni);
683	ieee80211_notify_node_leave(ic, ni);
684}
685
686static struct ieee80211_node *
687node_alloc(struct ieee80211_node_table *nt)
688{
689	struct ieee80211_node *ni;
690
691	MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
692		M_80211_NODE, M_NOWAIT | M_ZERO);
693	return ni;
694}
695
696/*
697 * Reclaim any resources in a node and reset any critical
698 * state.  Typically nodes are free'd immediately after,
699 * but in some cases the storage may be reused so we need
700 * to insure consistent state (should probably fix that).
701 */
702static void
703node_cleanup(struct ieee80211_node *ni)
704{
705#define	N(a)	(sizeof(a)/sizeof(a[0]))
706	struct ieee80211com *ic = ni->ni_ic;
707	int i;
708
709	/* NB: preserve ni_table */
710	if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
711		ic->ic_ps_sta--;
712		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
713		IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
714		    "[%s] power save mode off, %u sta's in ps mode\n",
715		    ether_sprintf(ni->ni_macaddr), ic->ic_ps_sta);
716	}
717	/*
718	 * Clear AREF flag that marks the authorization refcnt bump
719	 * has happened.  This is probably not needed as the node
720	 * should always be removed from the table so not found but
721	 * do it just in case.
722	 */
723	ni->ni_flags &= ~IEEE80211_NODE_AREF;
724
725	/*
726	 * Drain power save queue and, if needed, clear TIM.
727	 */
728	if (ieee80211_node_saveq_drain(ni) != 0 && ic->ic_set_tim != NULL)
729		ic->ic_set_tim(ni, 0);
730
731	ni->ni_associd = 0;
732	if (ni->ni_challenge != NULL) {
733		FREE(ni->ni_challenge, M_80211_NODE);
734		ni->ni_challenge = NULL;
735	}
736	/*
737	 * Preserve SSID, WPA, and WME ie's so the bss node is
738	 * reusable during a re-auth/re-assoc state transition.
739	 * If we remove these data they will not be recreated
740	 * because they come from a probe-response or beacon frame
741	 * which cannot be expected prior to the association-response.
742	 * This should not be an issue when operating in other modes
743	 * as stations leaving always go through a full state transition
744	 * which will rebuild this state.
745	 *
746	 * XXX does this leave us open to inheriting old state?
747	 */
748	for (i = 0; i < N(ni->ni_rxfrag); i++)
749		if (ni->ni_rxfrag[i] != NULL) {
750			m_freem(ni->ni_rxfrag[i]);
751			ni->ni_rxfrag[i] = NULL;
752		}
753	/*
754	 * Must be careful here to remove any key map entry w/o a LOR.
755	 */
756	ieee80211_node_delucastkey(ni);
757#undef N
758}
759
760static void
761node_free(struct ieee80211_node *ni)
762{
763	struct ieee80211com *ic = ni->ni_ic;
764
765	ic->ic_node_cleanup(ni);
766	if (ni->ni_wpa_ie != NULL)
767		FREE(ni->ni_wpa_ie, M_80211_NODE);
768	if (ni->ni_rsn_ie != NULL)
769		FREE(ni->ni_rsn_ie, M_80211_NODE);
770	if (ni->ni_wme_ie != NULL)
771		FREE(ni->ni_wme_ie, M_80211_NODE);
772	if (ni->ni_ath_ie != NULL)
773		FREE(ni->ni_ath_ie, M_80211_NODE);
774	IEEE80211_NODE_SAVEQ_DESTROY(ni);
775	FREE(ni, M_80211_NODE);
776}
777
778static int8_t
779node_getrssi(const struct ieee80211_node *ni)
780{
781	return ni->ni_rssi;
782}
783
784static void
785node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
786{
787	*rssi = ni->ni_rssi;
788	*noise = ni->ni_noise;
789}
790
791static void
792ieee80211_setup_node(struct ieee80211_node_table *nt,
793	struct ieee80211_node *ni, const uint8_t *macaddr)
794{
795	struct ieee80211com *ic = nt->nt_ic;
796	int hash;
797
798	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
799		"%s %p<%s> in %s table\n", __func__, ni,
800		ether_sprintf(macaddr), nt->nt_name);
801
802	IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
803	hash = IEEE80211_NODE_HASH(macaddr);
804	ieee80211_node_initref(ni);		/* mark referenced */
805	ni->ni_chan = IEEE80211_CHAN_ANYC;
806	ni->ni_authmode = IEEE80211_AUTH_OPEN;
807	ni->ni_txpower = ic->ic_txpowlimit;	/* max power */
808	ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey, IEEE80211_KEYIX_NONE);
809	ni->ni_inact_reload = nt->nt_inact_init;
810	ni->ni_inact = ni->ni_inact_reload;
811	ni->ni_ath_defkeyix = 0x7fff;
812	IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
813
814	IEEE80211_NODE_LOCK(nt);
815	TAILQ_INSERT_TAIL(&nt->nt_node, ni, ni_list);
816	LIST_INSERT_HEAD(&nt->nt_hash[hash], ni, ni_hash);
817	ni->ni_table = nt;
818	ni->ni_ic = ic;
819	IEEE80211_NODE_UNLOCK(nt);
820}
821
822struct ieee80211_node *
823ieee80211_alloc_node(struct ieee80211_node_table *nt, const uint8_t *macaddr)
824{
825	struct ieee80211com *ic = nt->nt_ic;
826	struct ieee80211_node *ni;
827
828	ni = ic->ic_node_alloc(nt);
829	if (ni != NULL)
830		ieee80211_setup_node(nt, ni, macaddr);
831	else
832		ic->ic_stats.is_rx_nodealloc++;
833	return ni;
834}
835
836/*
837 * Craft a temporary node suitable for sending a management frame
838 * to the specified station.  We craft only as much state as we
839 * need to do the work since the node will be immediately reclaimed
840 * once the send completes.
841 */
842struct ieee80211_node *
843ieee80211_tmp_node(struct ieee80211com *ic, const uint8_t *macaddr)
844{
845	struct ieee80211_node *ni;
846
847	ni = ic->ic_node_alloc(&ic->ic_sta);
848	if (ni != NULL) {
849		IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
850			"%s %p<%s>\n", __func__, ni, ether_sprintf(macaddr));
851
852		IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
853		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
854		ieee80211_node_initref(ni);		/* mark referenced */
855		ni->ni_txpower = ic->ic_bss->ni_txpower;
856		/* NB: required by ieee80211_fix_rate */
857		ieee80211_node_set_chan(ic, ni);
858		ieee80211_crypto_resetkey(ic, &ni->ni_ucastkey,
859			IEEE80211_KEYIX_NONE);
860		/* XXX optimize away */
861		IEEE80211_NODE_SAVEQ_INIT(ni, "unknown");
862
863		ni->ni_table = NULL;		/* NB: pedantic */
864		ni->ni_ic = ic;
865	} else {
866		/* XXX msg */
867		ic->ic_stats.is_rx_nodealloc++;
868	}
869	return ni;
870}
871
872struct ieee80211_node *
873ieee80211_dup_bss(struct ieee80211_node_table *nt, const uint8_t *macaddr)
874{
875	struct ieee80211com *ic = nt->nt_ic;
876	struct ieee80211_node *ni;
877
878	ni = ic->ic_node_alloc(nt);
879	if (ni != NULL) {
880		ieee80211_setup_node(nt, ni, macaddr);
881		/*
882		 * Inherit from ic_bss.
883		 */
884		ni->ni_authmode = ic->ic_bss->ni_authmode;
885		ni->ni_txpower = ic->ic_bss->ni_txpower;
886		ni->ni_vlan = ic->ic_bss->ni_vlan;	/* XXX?? */
887		IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
888		ieee80211_node_set_chan(ic, ni);
889		ni->ni_rsn = ic->ic_bss->ni_rsn;
890	} else
891		ic->ic_stats.is_rx_nodealloc++;
892	return ni;
893}
894
895static struct ieee80211_node *
896#ifdef IEEE80211_DEBUG_REFCNT
897_ieee80211_find_node_debug(struct ieee80211_node_table *nt,
898	const uint8_t *macaddr, const char *func, int line)
899#else
900_ieee80211_find_node(struct ieee80211_node_table *nt,
901	const uint8_t *macaddr)
902#endif
903{
904	struct ieee80211_node *ni;
905	int hash;
906
907	IEEE80211_NODE_LOCK_ASSERT(nt);
908
909	hash = IEEE80211_NODE_HASH(macaddr);
910	LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
911		if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
912			ieee80211_ref_node(ni);	/* mark referenced */
913#ifdef IEEE80211_DEBUG_REFCNT
914			IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
915			    "%s (%s:%u) %p<%s> refcnt %d\n", __func__,
916			    func, line,
917			    ni, ether_sprintf(ni->ni_macaddr),
918			    ieee80211_node_refcnt(ni));
919#endif
920			return ni;
921		}
922	}
923	return NULL;
924}
925#ifdef IEEE80211_DEBUG_REFCNT
926#define	_ieee80211_find_node(nt, mac) \
927	_ieee80211_find_node_debug(nt, mac, func, line)
928#endif
929
930struct ieee80211_node *
931#ifdef IEEE80211_DEBUG_REFCNT
932ieee80211_find_node_debug(struct ieee80211_node_table *nt,
933	const uint8_t *macaddr, const char *func, int line)
934#else
935ieee80211_find_node(struct ieee80211_node_table *nt, const uint8_t *macaddr)
936#endif
937{
938	struct ieee80211_node *ni;
939
940	IEEE80211_NODE_LOCK(nt);
941	ni = _ieee80211_find_node(nt, macaddr);
942	IEEE80211_NODE_UNLOCK(nt);
943	return ni;
944}
945
946/*
947 * Fake up a node; this handles node discovery in adhoc mode.
948 * Note that for the driver's benefit we we treat this like
949 * an association so the driver has an opportunity to setup
950 * it's private state.
951 */
952struct ieee80211_node *
953ieee80211_fakeup_adhoc_node(struct ieee80211_node_table *nt,
954	const uint8_t macaddr[IEEE80211_ADDR_LEN])
955{
956	struct ieee80211com *ic = nt->nt_ic;
957	struct ieee80211_node *ni;
958
959	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
960	    "%s: mac<%s>\n", __func__, ether_sprintf(macaddr));
961	ni = ieee80211_dup_bss(nt, macaddr);
962	if (ni != NULL) {
963		/* XXX no rate negotiation; just dup */
964		ni->ni_rates = ic->ic_bss->ni_rates;
965		if (ic->ic_newassoc != NULL)
966			ic->ic_newassoc(ni, 1);
967		if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
968			/*
969			 * In adhoc demo mode there are no management
970			 * frames to use to discover neighbor capabilities,
971			 * so blindly propagate the local configuration
972			 * so we can do interesting things (e.g. use
973			 * WME to disable ACK's).
974			 */
975			if (ic->ic_flags & IEEE80211_F_WME)
976				ni->ni_flags |= IEEE80211_NODE_QOS;
977			if (ic->ic_flags & IEEE80211_F_FF)
978				ni->ni_flags |= IEEE80211_NODE_FF;
979		}
980		/* XXX not right for 802.1x/WPA */
981		ieee80211_node_authorize(ni);
982	}
983	return ni;
984}
985
986void
987ieee80211_init_neighbor(struct ieee80211_node *ni,
988	const struct ieee80211_frame *wh,
989	const struct ieee80211_scanparams *sp)
990{
991	ni->ni_esslen = sp->ssid[1];
992	memcpy(ni->ni_essid, sp->ssid + 2, sp->ssid[1]);
993	IEEE80211_ADDR_COPY(ni->ni_bssid, wh->i_addr3);
994	memcpy(ni->ni_tstamp.data, sp->tstamp, sizeof(ni->ni_tstamp));
995	ni->ni_intval = sp->bintval;
996	ni->ni_capinfo = sp->capinfo;
997	ni->ni_chan = ni->ni_ic->ic_curchan;
998	ni->ni_fhdwell = sp->fhdwell;
999	ni->ni_fhindex = sp->fhindex;
1000	ni->ni_erp = sp->erp;
1001	ni->ni_timoff = sp->timoff;
1002	if (sp->wme != NULL)
1003		ieee80211_saveie(&ni->ni_wme_ie, sp->wme);
1004	if (sp->wpa != NULL)
1005		ieee80211_saveie(&ni->ni_wpa_ie, sp->wpa);
1006	if (sp->rsn != NULL)
1007		ieee80211_saveie(&ni->ni_rsn_ie, sp->rsn);
1008	if (sp->ath != NULL)
1009		ieee80211_saveath(ni, sp->ath);
1010
1011	/* NB: must be after ni_chan is setup */
1012	ieee80211_setup_rates(ni, sp->rates, sp->xrates,
1013		IEEE80211_F_DOSORT | IEEE80211_F_DOFRATE |
1014		IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
1015}
1016
1017/*
1018 * Do node discovery in adhoc mode on receipt of a beacon
1019 * or probe response frame.  Note that for the driver's
1020 * benefit we we treat this like an association so the
1021 * driver has an opportunity to setup it's private state.
1022 */
1023struct ieee80211_node *
1024ieee80211_add_neighbor(struct ieee80211com *ic,
1025	const struct ieee80211_frame *wh,
1026	const struct ieee80211_scanparams *sp)
1027{
1028	struct ieee80211_node *ni;
1029
1030	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1031	    "%s: mac<%s>\n", __func__, ether_sprintf(wh->i_addr2));
1032	ni = ieee80211_dup_bss(&ic->ic_sta, wh->i_addr2);/* XXX alloc_node? */
1033	if (ni != NULL) {
1034		ieee80211_init_neighbor(ni, wh, sp);
1035		if (ic->ic_newassoc != NULL)
1036			ic->ic_newassoc(ni, 1);
1037		/* XXX not right for 802.1x/WPA */
1038		ieee80211_node_authorize(ni);
1039	}
1040	return ni;
1041}
1042
1043#define	IS_CTL(wh) \
1044	((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
1045#define	IS_PSPOLL(wh) \
1046	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_PS_POLL)
1047#define	IS_BAR(wh) \
1048	((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == IEEE80211_FC0_SUBTYPE_BAR)
1049
1050/*
1051 * Locate the node for sender, track state, and then pass the
1052 * (referenced) node up to the 802.11 layer for its use.  We
1053 * are required to pass some node so we fall back to ic_bss
1054 * when this frame is from an unknown sender.  The 802.11 layer
1055 * knows this means the sender wasn't in the node table and
1056 * acts accordingly.
1057 */
1058struct ieee80211_node *
1059#ifdef IEEE80211_DEBUG_REFCNT
1060ieee80211_find_rxnode_debug(struct ieee80211com *ic,
1061	const struct ieee80211_frame_min *wh, const char *func, int line)
1062#else
1063ieee80211_find_rxnode(struct ieee80211com *ic,
1064	const struct ieee80211_frame_min *wh)
1065#endif
1066{
1067	struct ieee80211_node_table *nt;
1068	struct ieee80211_node *ni;
1069
1070	/* XXX check ic_bss first in station mode */
1071	/* XXX 4-address frames? */
1072	nt = &ic->ic_sta;
1073	IEEE80211_NODE_LOCK(nt);
1074	if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(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	if (ni == NULL)
1079		ni = ieee80211_ref_node(ic->ic_bss);
1080	IEEE80211_NODE_UNLOCK(nt);
1081
1082	return ni;
1083}
1084
1085/*
1086 * Like ieee80211_find_rxnode but use the supplied h/w
1087 * key index as a hint to locate the node in the key
1088 * mapping table.  If an entry is present at the key
1089 * index we return it; otherwise do a normal lookup and
1090 * update the mapping table if the station has a unicast
1091 * key assigned to it.
1092 */
1093struct ieee80211_node *
1094#ifdef IEEE80211_DEBUG_REFCNT
1095ieee80211_find_rxnode_withkey_debug(struct ieee80211com *ic,
1096	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix,
1097	const char *func, int line)
1098#else
1099ieee80211_find_rxnode_withkey(struct ieee80211com *ic,
1100	const struct ieee80211_frame_min *wh, ieee80211_keyix keyix)
1101#endif
1102{
1103	struct ieee80211_node_table *nt;
1104	struct ieee80211_node *ni;
1105
1106	nt = &ic->ic_sta;
1107	IEEE80211_NODE_LOCK(nt);
1108	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax)
1109		ni = nt->nt_keyixmap[keyix];
1110	else
1111		ni = NULL;
1112	if (ni == NULL) {
1113		if (IS_CTL(wh) && !IS_PSPOLL(wh) && !IS_BAR(wh) /*&& !IS_RTS(ah)*/)
1114			ni = _ieee80211_find_node(nt, wh->i_addr1);
1115		else
1116			ni = _ieee80211_find_node(nt, wh->i_addr2);
1117		if (ni == NULL)
1118			ni = ieee80211_ref_node(ic->ic_bss);
1119		if (nt->nt_keyixmap != NULL) {
1120			/*
1121			 * If the station has a unicast key cache slot
1122			 * assigned update the key->node mapping table.
1123			 */
1124			keyix = ni->ni_ucastkey.wk_rxkeyix;
1125			/* XXX can keyixmap[keyix] != NULL? */
1126			if (keyix < nt->nt_keyixmax &&
1127			    nt->nt_keyixmap[keyix] == NULL) {
1128				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1129				    "%s: add key map entry %p<%s> refcnt %d\n",
1130				    __func__, ni, ether_sprintf(ni->ni_macaddr),
1131				    ieee80211_node_refcnt(ni)+1);
1132				nt->nt_keyixmap[keyix] = ieee80211_ref_node(ni);
1133			}
1134		}
1135	} else
1136		ieee80211_ref_node(ni);
1137	IEEE80211_NODE_UNLOCK(nt);
1138
1139	return ni;
1140}
1141#undef IS_BAR
1142#undef IS_PSPOLL
1143#undef IS_CTL
1144
1145/*
1146 * Return a reference to the appropriate node for sending
1147 * a data frame.  This handles node discovery in adhoc networks.
1148 */
1149struct ieee80211_node *
1150#ifdef IEEE80211_DEBUG_REFCNT
1151ieee80211_find_txnode_debug(struct ieee80211com *ic, const uint8_t *macaddr,
1152	const char *func, int line)
1153#else
1154ieee80211_find_txnode(struct ieee80211com *ic, const uint8_t *macaddr)
1155#endif
1156{
1157	struct ieee80211_node_table *nt = &ic->ic_sta;
1158	struct ieee80211_node *ni;
1159
1160	/*
1161	 * The destination address should be in the node table
1162	 * unless this is a multicast/broadcast frame.  We can
1163	 * also optimize station mode operation, all frames go
1164	 * to the bss node.
1165	 */
1166	/* XXX can't hold lock across dup_bss 'cuz of recursive locking */
1167	IEEE80211_NODE_LOCK(nt);
1168	if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
1169		ni = ieee80211_ref_node(ic->ic_bss);
1170	else {
1171		ni = _ieee80211_find_node(nt, macaddr);
1172		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
1173		    (ni != NULL && ni->ni_associd == 0)) {
1174			/*
1175			 * Station is not associated; don't permit the
1176			 * data frame to be sent by returning NULL.  This
1177			 * is kinda a kludge but the least intrusive way
1178			 * to add this check into all drivers.
1179			 */
1180			ieee80211_unref_node(&ni);	/* NB: null's ni */
1181		}
1182	}
1183	IEEE80211_NODE_UNLOCK(nt);
1184
1185	if (ni == NULL) {
1186		if (ic->ic_opmode == IEEE80211_M_IBSS ||
1187		    ic->ic_opmode == IEEE80211_M_AHDEMO) {
1188			/*
1189			 * In adhoc mode cons up a node for the destination.
1190			 * Note that we need an additional reference for the
1191			 * caller to be consistent with _ieee80211_find_node.
1192			 */
1193			ni = ieee80211_fakeup_adhoc_node(nt, macaddr);
1194			if (ni != NULL)
1195				(void) ieee80211_ref_node(ni);
1196		} else {
1197			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
1198				"[%s] no node, discard frame (%s)\n",
1199				ether_sprintf(macaddr), __func__);
1200			ic->ic_stats.is_tx_nonode++;
1201		}
1202	}
1203	return ni;
1204}
1205
1206/*
1207 * Like find but search based on the ssid too.
1208 */
1209struct ieee80211_node *
1210#ifdef IEEE80211_DEBUG_REFCNT
1211ieee80211_find_node_with_ssid_debug(struct ieee80211_node_table *nt,
1212	const uint8_t *macaddr, u_int ssidlen, const uint8_t *ssid,
1213	const char *func, int line)
1214#else
1215ieee80211_find_node_with_ssid(struct ieee80211_node_table *nt,
1216	const uint8_t *macaddr, u_int ssidlen, const uint8_t *ssid)
1217#endif
1218{
1219#define	MATCH_SSID(ni, ssid, ssidlen) \
1220	(ni->ni_esslen == ssidlen && memcmp(ni->ni_essid, ssid, ssidlen) == 0)
1221	static const uint8_t zeromac[IEEE80211_ADDR_LEN];
1222	struct ieee80211_node *ni;
1223	int hash;
1224
1225	IEEE80211_NODE_LOCK(nt);
1226	/*
1227	 * A mac address that is all zero means match only the ssid;
1228	 * otherwise we must match both.
1229	 */
1230	if (IEEE80211_ADDR_EQ(macaddr, zeromac)) {
1231		TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1232			if (MATCH_SSID(ni, ssid, ssidlen))
1233				break;
1234		}
1235	} else {
1236		hash = IEEE80211_NODE_HASH(macaddr);
1237		LIST_FOREACH(ni, &nt->nt_hash[hash], ni_hash) {
1238			if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
1239			    MATCH_SSID(ni, ssid, ssidlen))
1240				break;
1241		}
1242	}
1243	if (ni != NULL) {
1244		ieee80211_ref_node(ni);	/* mark referenced */
1245		IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
1246		     REFCNT_LOC, ni, ether_sprintf(ni->ni_macaddr),
1247		     ieee80211_node_refcnt(ni));
1248	}
1249	IEEE80211_NODE_UNLOCK(nt);
1250	return ni;
1251#undef MATCH_SSID
1252}
1253
1254static void
1255_ieee80211_free_node(struct ieee80211_node *ni)
1256{
1257	struct ieee80211com *ic = ni->ni_ic;
1258	struct ieee80211_node_table *nt = ni->ni_table;
1259
1260	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1261		"%s %p<%s> in %s table\n", __func__, ni,
1262		ether_sprintf(ni->ni_macaddr),
1263		nt != NULL ? nt->nt_name : "<gone>");
1264
1265	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1266	if (nt != NULL) {
1267		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1268		LIST_REMOVE(ni, ni_hash);
1269	}
1270	ic->ic_node_free(ni);
1271}
1272
1273void
1274#ifdef IEEE80211_DEBUG_REFCNT
1275ieee80211_free_node_debug(struct ieee80211_node *ni, const char *func, int line)
1276#else
1277ieee80211_free_node(struct ieee80211_node *ni)
1278#endif
1279{
1280	struct ieee80211_node_table *nt = ni->ni_table;
1281
1282#ifdef IEEE80211_DEBUG_REFCNT
1283	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1284		"%s (%s:%u) %p<%s> refcnt %d\n", __func__, func, line, ni,
1285		 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)-1);
1286#endif
1287	if (nt != NULL) {
1288		IEEE80211_NODE_LOCK(nt);
1289		if (ieee80211_node_dectestref(ni)) {
1290			/*
1291			 * Last reference, reclaim state.
1292			 */
1293			_ieee80211_free_node(ni);
1294		} else if (ieee80211_node_refcnt(ni) == 1 &&
1295		    nt->nt_keyixmap != NULL) {
1296			ieee80211_keyix keyix;
1297			/*
1298			 * Check for a last reference in the key mapping table.
1299			 */
1300			keyix = ni->ni_ucastkey.wk_rxkeyix;
1301			if (keyix < nt->nt_keyixmax &&
1302			    nt->nt_keyixmap[keyix] == ni) {
1303				IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1304				    "%s: %p<%s> clear key map entry", __func__,
1305				    ni, ether_sprintf(ni->ni_macaddr));
1306				nt->nt_keyixmap[keyix] = NULL;
1307				ieee80211_node_decref(ni); /* XXX needed? */
1308				_ieee80211_free_node(ni);
1309			}
1310		}
1311		IEEE80211_NODE_UNLOCK(nt);
1312	} else {
1313		if (ieee80211_node_dectestref(ni))
1314			_ieee80211_free_node(ni);
1315	}
1316}
1317
1318/*
1319 * Reclaim a unicast key and clear any key cache state.
1320 */
1321int
1322ieee80211_node_delucastkey(struct ieee80211_node *ni)
1323{
1324	struct ieee80211com *ic = ni->ni_ic;
1325	struct ieee80211_node_table *nt = &ic->ic_sta;
1326	struct ieee80211_node *nikey;
1327	ieee80211_keyix keyix;
1328	int isowned, status;
1329
1330	/*
1331	 * NB: We must beware of LOR here; deleting the key
1332	 * can cause the crypto layer to block traffic updates
1333	 * which can generate a LOR against the node table lock;
1334	 * grab it here and stash the key index for our use below.
1335	 *
1336	 * Must also beware of recursion on the node table lock.
1337	 * When called from node_cleanup we may already have
1338	 * the node table lock held.  Unfortunately there's no
1339	 * way to separate out this path so we must do this
1340	 * conditionally.
1341	 */
1342	isowned = IEEE80211_NODE_IS_LOCKED(nt);
1343	if (!isowned)
1344		IEEE80211_NODE_LOCK(nt);
1345	keyix = ni->ni_ucastkey.wk_rxkeyix;
1346	status = ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
1347	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax) {
1348		nikey = nt->nt_keyixmap[keyix];
1349		nt->nt_keyixmap[keyix] = NULL;;
1350	} else
1351		nikey = NULL;
1352	if (!isowned)
1353		IEEE80211_NODE_UNLOCK(&ic->ic_sta);
1354
1355	if (nikey != NULL) {
1356		KASSERT(nikey == ni,
1357			("key map out of sync, ni %p nikey %p", ni, nikey));
1358		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1359			"%s: delete key map entry %p<%s> refcnt %d\n",
1360			__func__, ni, ether_sprintf(ni->ni_macaddr),
1361			ieee80211_node_refcnt(ni)-1);
1362		ieee80211_free_node(ni);
1363	}
1364	return status;
1365}
1366
1367/*
1368 * Reclaim a node.  If this is the last reference count then
1369 * do the normal free work.  Otherwise remove it from the node
1370 * table and mark it gone by clearing the back-reference.
1371 */
1372static void
1373node_reclaim(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1374{
1375	ieee80211_keyix keyix;
1376
1377	IEEE80211_NODE_LOCK_ASSERT(nt);
1378
1379	IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1380		"%s: remove %p<%s> from %s table, refcnt %d\n",
1381		__func__, ni, ether_sprintf(ni->ni_macaddr),
1382		nt->nt_name, ieee80211_node_refcnt(ni)-1);
1383	/*
1384	 * Clear any entry in the unicast key mapping table.
1385	 * We need to do it here so rx lookups don't find it
1386	 * in the mapping table even if it's not in the hash
1387	 * table.  We cannot depend on the mapping table entry
1388	 * being cleared because the node may not be free'd.
1389	 */
1390	keyix = ni->ni_ucastkey.wk_rxkeyix;
1391	if (nt->nt_keyixmap != NULL && keyix < nt->nt_keyixmax &&
1392	    nt->nt_keyixmap[keyix] == ni) {
1393		IEEE80211_DPRINTF(ni->ni_ic, IEEE80211_MSG_NODE,
1394			"%s: %p<%s> clear key map entry\n",
1395			__func__, ni, ether_sprintf(ni->ni_macaddr));
1396		nt->nt_keyixmap[keyix] = NULL;
1397		ieee80211_node_decref(ni);	/* NB: don't need free */
1398	}
1399	if (!ieee80211_node_dectestref(ni)) {
1400		/*
1401		 * Other references are present, just remove the
1402		 * node from the table so it cannot be found.  When
1403		 * the references are dropped storage will be
1404		 * reclaimed.
1405		 */
1406		TAILQ_REMOVE(&nt->nt_node, ni, ni_list);
1407		LIST_REMOVE(ni, ni_hash);
1408		ni->ni_table = NULL;		/* clear reference */
1409	} else
1410		_ieee80211_free_node(ni);
1411}
1412
1413static void
1414ieee80211_free_allnodes_locked(struct ieee80211_node_table *nt)
1415{
1416	struct ieee80211com *ic = nt->nt_ic;
1417	struct ieee80211_node *ni;
1418
1419	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1420		"%s: free all nodes in %s table\n", __func__, nt->nt_name);
1421
1422	while ((ni = TAILQ_FIRST(&nt->nt_node)) != NULL) {
1423		if (ni->ni_associd != 0) {
1424			if (ic->ic_auth->ia_node_leave != NULL)
1425				ic->ic_auth->ia_node_leave(ic, ni);
1426			IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1427		}
1428		node_reclaim(nt, ni);
1429	}
1430}
1431
1432/*
1433 * Timeout inactive stations and do related housekeeping.
1434 * Note that we cannot hold the node lock while sending a
1435 * frame as this would lead to a LOR.  Instead we use a
1436 * generation number to mark nodes that we've scanned and
1437 * drop the lock and restart a scan if we have to time out
1438 * a node.  Since we are single-threaded by virtue of
1439 * controlling the inactivity timer we can be sure this will
1440 * process each node only once.
1441 */
1442static void
1443ieee80211_timeout_stations(struct ieee80211_node_table *nt)
1444{
1445	struct ieee80211com *ic = nt->nt_ic;
1446	struct ieee80211_node *ni;
1447	u_int gen;
1448	int isadhoc;
1449
1450	isadhoc = (ic->ic_opmode == IEEE80211_M_IBSS ||
1451		   ic->ic_opmode == IEEE80211_M_AHDEMO);
1452	IEEE80211_SCAN_LOCK(nt);
1453	gen = ++nt->nt_scangen;
1454restart:
1455	IEEE80211_NODE_LOCK(nt);
1456	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1457		if (ni->ni_scangen == gen)	/* previously handled */
1458			continue;
1459		ni->ni_scangen = gen;
1460		/*
1461		 * Ignore entries for which have yet to receive an
1462		 * authentication frame.  These are transient and
1463		 * will be reclaimed when the last reference to them
1464		 * goes away (when frame xmits complete).
1465		 */
1466		if ((ic->ic_opmode == IEEE80211_M_HOSTAP ||
1467		     ic->ic_opmode == IEEE80211_M_STA) &&
1468		    (ni->ni_flags & IEEE80211_NODE_AREF) == 0)
1469			continue;
1470		/*
1471		 * Free fragment if not needed anymore
1472		 * (last fragment older than 1s).
1473		 * XXX doesn't belong here
1474		 */
1475		if (ni->ni_rxfrag[0] != NULL &&
1476		    ticks > ni->ni_rxfragstamp + hz) {
1477			m_freem(ni->ni_rxfrag[0]);
1478			ni->ni_rxfrag[0] = NULL;
1479		}
1480		if (ni->ni_inact > 0)
1481			ni->ni_inact--;
1482		/*
1483		 * Special case ourself; we may be idle for extended periods
1484		 * of time and regardless reclaiming our state is wrong.
1485		 */
1486		if (ni == ic->ic_bss)
1487			continue;
1488		if (ni->ni_associd != 0 || isadhoc) {
1489			/*
1490			 * Age frames on the power save queue.
1491			 */
1492			if (ieee80211_node_saveq_age(ni) != 0 &&
1493			    IEEE80211_NODE_SAVEQ_QLEN(ni) == 0 &&
1494			    ic->ic_set_tim != NULL)
1495				ic->ic_set_tim(ni, 0);
1496			/*
1497			 * Probe the station before time it out.  We
1498			 * send a null data frame which may not be
1499			 * universally supported by drivers (need it
1500			 * for ps-poll support so it should be...).
1501			 *
1502			 * XXX don't probe the station unless we've
1503			 *     received a frame from them (and have
1504			 *     some idea of the rates they are capable
1505			 *     of); this will get fixed more properly
1506			 *     soon with better handling of the rate set.
1507			 */
1508			if ((ic->ic_flags_ext & IEEE80211_FEXT_INACT) &&
1509			    (0 < ni->ni_inact &&
1510			     ni->ni_inact <= ic->ic_inact_probe) &&
1511			    ni->ni_rates.rs_nrates != 0) {
1512				IEEE80211_NOTE(ic,
1513				    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE,
1514				    ni, "%s",
1515				    "probe station due to inactivity");
1516				/*
1517				 * Grab a reference before unlocking the table
1518				 * so the node cannot be reclaimed before we
1519				 * send the frame. ieee80211_send_nulldata
1520				 * understands we've done this and reclaims the
1521				 * ref for us as needed.
1522				 */
1523				ieee80211_ref_node(ni);
1524				IEEE80211_NODE_UNLOCK(nt);
1525				ieee80211_send_nulldata(ni);
1526				/* XXX stat? */
1527				goto restart;
1528			}
1529		}
1530		if ((ic->ic_flags_ext & IEEE80211_FEXT_INACT) &&
1531		    ni->ni_inact <= 0) {
1532			IEEE80211_NOTE(ic,
1533			    IEEE80211_MSG_INACT | IEEE80211_MSG_NODE, ni,
1534			    "station timed out due to inactivity "
1535			    "(refcnt %u)", ieee80211_node_refcnt(ni));
1536			/*
1537			 * Send a deauthenticate frame and drop the station.
1538			 * This is somewhat complicated due to reference counts
1539			 * and locking.  At this point a station will typically
1540			 * have a reference count of 1.  ieee80211_node_leave
1541			 * will do a "free" of the node which will drop the
1542			 * reference count.  But in the meantime a reference
1543			 * wil be held by the deauth frame.  The actual reclaim
1544			 * of the node will happen either after the tx is
1545			 * completed or by ieee80211_node_leave.
1546			 *
1547			 * Separately we must drop the node lock before sending
1548			 * in case the driver takes a lock, as this can result
1549			 * in a LOR between the node lock and the driver lock.
1550			 */
1551			IEEE80211_NODE_UNLOCK(nt);
1552			if (ni->ni_associd != 0) {
1553				IEEE80211_SEND_MGMT(ic, ni,
1554				    IEEE80211_FC0_SUBTYPE_DEAUTH,
1555				    IEEE80211_REASON_AUTH_EXPIRE);
1556			}
1557			ieee80211_node_leave(ic, ni);
1558			ic->ic_stats.is_node_timeout++;
1559			goto restart;
1560		}
1561	}
1562	IEEE80211_NODE_UNLOCK(nt);
1563
1564	IEEE80211_SCAN_UNLOCK(nt);
1565}
1566
1567void
1568ieee80211_node_timeout(void *arg)
1569{
1570	struct ieee80211com *ic = arg;
1571
1572	ieee80211_scan_timeout(ic);
1573	ieee80211_timeout_stations(&ic->ic_sta);
1574
1575	IEEE80211_LOCK(ic);
1576	ieee80211_erp_timeout(ic);
1577	IEEE80211_UNLOCK(ic);
1578
1579	callout_reset(&ic->ic_inact, IEEE80211_INACT_WAIT*hz,
1580		ieee80211_node_timeout, ic);
1581}
1582
1583void
1584ieee80211_iterate_nodes(struct ieee80211_node_table *nt, ieee80211_iter_func *f, void *arg)
1585{
1586	struct ieee80211_node *ni;
1587	u_int gen;
1588
1589	IEEE80211_SCAN_LOCK(nt);
1590	gen = ++nt->nt_scangen;
1591restart:
1592	IEEE80211_NODE_LOCK(nt);
1593	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
1594		if (ni->ni_scangen != gen) {
1595			ni->ni_scangen = gen;
1596			(void) ieee80211_ref_node(ni);
1597			IEEE80211_NODE_UNLOCK(nt);
1598			(*f)(arg, ni);
1599			ieee80211_free_node(ni);
1600			goto restart;
1601		}
1602	}
1603	IEEE80211_NODE_UNLOCK(nt);
1604
1605	IEEE80211_SCAN_UNLOCK(nt);
1606}
1607
1608void
1609ieee80211_dump_node(struct ieee80211_node_table *nt, struct ieee80211_node *ni)
1610{
1611	printf("0x%p: mac %s refcnt %d\n", ni,
1612		ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni));
1613	printf("\tscangen %u authmode %u flags 0x%x\n",
1614		ni->ni_scangen, ni->ni_authmode, ni->ni_flags);
1615	printf("\tassocid 0x%x txpower %u vlan %u\n",
1616		ni->ni_associd, ni->ni_txpower, ni->ni_vlan);
1617	printf("\ttxseq %u rxseq %u fragno %u rxfragstamp %u\n",
1618		ni->ni_txseqs[IEEE80211_NONQOS_TID],
1619		ni->ni_rxseqs[IEEE80211_NONQOS_TID] >> IEEE80211_SEQ_SEQ_SHIFT,
1620		ni->ni_rxseqs[IEEE80211_NONQOS_TID] & IEEE80211_SEQ_FRAG_MASK,
1621		ni->ni_rxfragstamp);
1622	printf("\trstamp %u rssi %d noise %d intval %u capinfo 0x%x\n",
1623		ni->ni_rstamp, ni->ni_rssi, ni->ni_noise,
1624		ni->ni_intval, ni->ni_capinfo);
1625	printf("\tbssid %s essid \"%.*s\" channel %u:0x%x\n",
1626		ether_sprintf(ni->ni_bssid),
1627		ni->ni_esslen, ni->ni_essid,
1628		ni->ni_chan->ic_freq, ni->ni_chan->ic_flags);
1629	printf("\tfails %u inact %u txrate %u\n",
1630		ni->ni_fails, ni->ni_inact, ni->ni_txrate);
1631	printf("\thtcap %x htparam %x htctlchan %u ht2ndchan %u\n",
1632		ni->ni_htcap, ni->ni_htparam,
1633		ni->ni_htctlchan, ni->ni_ht2ndchan);
1634	printf("\thtopmode %x htstbc %x chw %u\n",
1635		ni->ni_htopmode, ni->ni_htstbc, ni->ni_chw);
1636}
1637
1638void
1639ieee80211_dump_nodes(struct ieee80211_node_table *nt)
1640{
1641	ieee80211_iterate_nodes(nt,
1642		(ieee80211_iter_func *) ieee80211_dump_node, nt);
1643}
1644
1645void
1646ieee80211_notify_erp(struct ieee80211com *ic)
1647{
1648	if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1649		ieee80211_beacon_notify(ic, IEEE80211_BEACON_ERP);
1650}
1651
1652/*
1653 * Handle a station joining an 11g network.
1654 */
1655static void
1656ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
1657{
1658
1659	/*
1660	 * Station isn't capable of short slot time.  Bump
1661	 * the count of long slot time stations and disable
1662	 * use of short slot time.  Note that the actual switch
1663	 * over to long slot time use may not occur until the
1664	 * next beacon transmission (per sec. 7.3.1.4 of 11g).
1665	 */
1666	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
1667		ic->ic_longslotsta++;
1668		IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1669		    "station needs long slot time, count %d",
1670		    ic->ic_longslotsta);
1671		/* XXX vap's w/ conflicting needs won't work */
1672		if (!IEEE80211_IS_CHAN_108G(ic->ic_bsschan)) {
1673			/*
1674			 * Don't force slot time when switched to turbo
1675			 * mode as non-ERP stations won't be present; this
1676			 * need only be done when on the normal G channel.
1677			 */
1678			ieee80211_set_shortslottime(ic, 0);
1679		}
1680	}
1681	/*
1682	 * If the new station is not an ERP station
1683	 * then bump the counter and enable protection
1684	 * if configured.
1685	 */
1686	if (!ieee80211_iserp_rateset(ic, &ni->ni_rates)) {
1687		ic->ic_nonerpsta++;
1688		IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1689		    "station is !ERP, %d non-ERP stations associated",
1690		    ic->ic_nonerpsta);
1691		/*
1692		 * If station does not support short preamble
1693		 * then we must enable use of Barker preamble.
1694		 */
1695		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) == 0) {
1696			IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1697			    "%s", "station needs long preamble");
1698			ic->ic_flags |= IEEE80211_F_USEBARKER;
1699			ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
1700		}
1701		/*
1702		 * If protection is configured, enable it.
1703		 */
1704		if (ic->ic_protmode != IEEE80211_PROT_NONE &&
1705		    ic->ic_nonerpsta == 1 &&
1706		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
1707			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1708			    "%s: enable use of protection\n", __func__);
1709			ic->ic_flags |= IEEE80211_F_USEPROT;
1710			ieee80211_notify_erp(ic);
1711		}
1712	} else
1713		ni->ni_flags |= IEEE80211_NODE_ERP;
1714}
1715
1716void
1717ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
1718{
1719	int newassoc;
1720
1721	if (ni->ni_associd == 0) {
1722		uint16_t aid;
1723
1724		/*
1725		 * It would be good to search the bitmap
1726		 * more efficiently, but this will do for now.
1727		 */
1728		for (aid = 1; aid < ic->ic_max_aid; aid++) {
1729			if (!IEEE80211_AID_ISSET(aid,
1730			    ic->ic_aid_bitmap))
1731				break;
1732		}
1733		if (aid >= ic->ic_max_aid) {
1734			IEEE80211_SEND_MGMT(ic, ni, resp,
1735			    IEEE80211_REASON_ASSOC_TOOMANY);
1736			ieee80211_node_leave(ic, ni);
1737			return;
1738		}
1739		ni->ni_associd = aid | 0xc000;
1740		IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
1741		ic->ic_sta_assoc++;
1742		newassoc = 1;
1743		if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
1744		    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
1745			ieee80211_node_join_11g(ic, ni);
1746	} else
1747		newassoc = 0;
1748
1749	IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
1750	    "station %sassociated at aid %d: %s preamble, %s slot time%s%s%s%s%s",
1751	    newassoc ? "" : "re",
1752	    IEEE80211_NODE_AID(ni),
1753	    ic->ic_flags & IEEE80211_F_SHPREAMBLE ? "short" : "long",
1754	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long",
1755	    ic->ic_flags & IEEE80211_F_USEPROT ? ", protection" : "",
1756	    ni->ni_flags & IEEE80211_NODE_QOS ? ", QoS" : "",
1757	    ni->ni_flags & IEEE80211_NODE_HT ?
1758		(ni->ni_chw == 20 ? ", HT20" : ", HT40") : "",
1759	    IEEE80211_ATH_CAP(ic, ni, IEEE80211_NODE_FF) ?
1760		", fast-frames" : "",
1761	    IEEE80211_ATH_CAP(ic, ni, IEEE80211_NODE_TURBOP) ?
1762		", turbo" : ""
1763	);
1764
1765	/* give driver a chance to setup state like ni_txrate */
1766	if (ic->ic_newassoc != NULL)
1767		ic->ic_newassoc(ni, newassoc);
1768	IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
1769	/* tell the authenticator about new station */
1770	if (ic->ic_auth->ia_node_join != NULL)
1771		ic->ic_auth->ia_node_join(ic, ni);
1772	ieee80211_notify_node_join(ic, ni, newassoc);
1773}
1774
1775static void
1776disable_protection(struct ieee80211com *ic)
1777{
1778	KASSERT(ic->ic_nonerpsta == 0 &&
1779	    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0,
1780	   ("%d non ERP stations, flags 0x%x", ic->ic_nonerpsta,
1781	   ic->ic_flags_ext));
1782
1783	ic->ic_flags &= ~IEEE80211_F_USEPROT;
1784	/* XXX verify mode? */
1785	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) {
1786		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
1787		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
1788	}
1789	ieee80211_notify_erp(ic);
1790}
1791
1792/*
1793 * Handle a station leaving an 11g network.
1794 */
1795static void
1796ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
1797{
1798
1799	KASSERT(IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan),
1800	     ("not in 11g, bss %u:0x%x, curmode %u", ic->ic_bsschan->ic_freq,
1801	      ic->ic_bsschan->ic_flags, ic->ic_curmode));
1802
1803	/*
1804	 * If a long slot station do the slot time bookkeeping.
1805	 */
1806	if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) == 0) {
1807		KASSERT(ic->ic_longslotsta > 0,
1808		    ("bogus long slot station count %d", ic->ic_longslotsta));
1809		ic->ic_longslotsta--;
1810		IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1811		    "long slot time station leaves, count now %d",
1812		    ic->ic_longslotsta);
1813		if (ic->ic_longslotsta == 0) {
1814			/*
1815			 * Re-enable use of short slot time if supported
1816			 * and not operating in IBSS mode (per spec).
1817			 */
1818			if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
1819			    ic->ic_opmode != IEEE80211_M_IBSS) {
1820				IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1821				    "%s: re-enable use of short slot time\n",
1822				    __func__);
1823				ieee80211_set_shortslottime(ic, 1);
1824			}
1825		}
1826	}
1827	/*
1828	 * If a non-ERP station do the protection-related bookkeeping.
1829	 */
1830	if ((ni->ni_flags & IEEE80211_NODE_ERP) == 0) {
1831		KASSERT(ic->ic_nonerpsta > 0,
1832		    ("bogus non-ERP station count %d", ic->ic_nonerpsta));
1833		ic->ic_nonerpsta--;
1834		IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC, ni,
1835		    "non-ERP station leaves, count now %d%s", ic->ic_nonerpsta,
1836		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) ?
1837			" (non-ERP sta present)" : "");
1838		if (ic->ic_nonerpsta == 0 &&
1839		    (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) == 0) {
1840			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1841				"%s: disable use of protection\n", __func__);
1842			disable_protection(ic);
1843		}
1844	}
1845}
1846
1847/*
1848 * Time out presence of an overlapping bss with non-ERP
1849 * stations.  When operating in hostap mode we listen for
1850 * beacons from other stations and if we identify a non-ERP
1851 * station is present we enable protection.  To identify
1852 * when all non-ERP stations are gone we time out this
1853 * condition.
1854 */
1855static void
1856ieee80211_erp_timeout(struct ieee80211com *ic)
1857{
1858
1859	IEEE80211_LOCK_ASSERT(ic);
1860
1861	if ((ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR) &&
1862	    time_after(ticks, ic->ic_lastnonerp + IEEE80211_NONERP_PRESENT_AGE)) {
1863		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1864		    "%s\n", "age out non-ERP sta present on channel");
1865		ic->ic_flags_ext &= ~IEEE80211_FEXT_NONERP_PR;
1866		if (ic->ic_nonerpsta == 0)
1867			disable_protection(ic);
1868	}
1869}
1870
1871/*
1872 * Handle bookkeeping for station deauthentication/disassociation
1873 * when operating as an ap.
1874 */
1875void
1876ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
1877{
1878	struct ieee80211_node_table *nt = ni->ni_table;
1879
1880	IEEE80211_NOTE(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG, ni,
1881	    "station with aid %d leaves", IEEE80211_NODE_AID(ni));
1882
1883	KASSERT(ic->ic_opmode != IEEE80211_M_STA,
1884		("unexpected operating mode %u", ic->ic_opmode));
1885	/*
1886	 * If node wasn't previously associated all
1887	 * we need to do is reclaim the reference.
1888	 */
1889	/* XXX ibss mode bypasses 11g and notification */
1890	if (ni->ni_associd == 0)
1891		goto done;
1892	/*
1893	 * Tell the authenticator the station is leaving.
1894	 * Note that we must do this before yanking the
1895	 * association id as the authenticator uses the
1896	 * associd to locate it's state block.
1897	 */
1898	if (ic->ic_auth->ia_node_leave != NULL)
1899		ic->ic_auth->ia_node_leave(ic, ni);
1900	IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
1901	ni->ni_associd = 0;
1902	ic->ic_sta_assoc--;
1903
1904	if (IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan) &&
1905	    IEEE80211_IS_CHAN_FULL(ic->ic_bsschan))
1906		ieee80211_node_leave_11g(ic, ni);
1907	/*
1908	 * Cleanup station state.  In particular clear various
1909	 * state that might otherwise be reused if the node
1910	 * is reused before the reference count goes to zero
1911	 * (and memory is reclaimed).
1912	 */
1913	ieee80211_sta_leave(ic, ni);
1914done:
1915	/*
1916	 * Remove the node from any table it's recorded in and
1917	 * drop the caller's reference.  Removal from the table
1918	 * is important to insure the node is not reprocessed
1919	 * for inactivity.
1920	 */
1921	if (nt != NULL) {
1922		IEEE80211_NODE_LOCK(nt);
1923		node_reclaim(nt, ni);
1924		IEEE80211_NODE_UNLOCK(nt);
1925	} else
1926		ieee80211_free_node(ni);
1927}
1928
1929int8_t
1930ieee80211_getrssi(struct ieee80211com *ic)
1931{
1932#define	NZ(x)	((x) == 0 ? 1 : (x))
1933	struct ieee80211_node_table *nt = &ic->ic_sta;
1934	int rssi_samples;
1935	int32_t rssi_total;
1936	struct ieee80211_node *ni;
1937
1938	rssi_total = 0;
1939	rssi_samples = 0;
1940	switch (ic->ic_opmode) {
1941	case IEEE80211_M_IBSS:		/* average of all ibss neighbors */
1942	case IEEE80211_M_AHDEMO:	/* average of all neighbors */
1943	case IEEE80211_M_HOSTAP:	/* average of all associated stations */
1944		/* XXX locking */
1945		TAILQ_FOREACH(ni, &nt->nt_node, ni_list)
1946			if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
1947			    (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS)) {
1948				int8_t rssi = ic->ic_node_getrssi(ni);
1949				if (rssi != 0) {
1950					rssi_samples++;
1951					rssi_total += rssi;
1952				}
1953			}
1954		break;
1955	case IEEE80211_M_MONITOR:	/* XXX */
1956	case IEEE80211_M_STA:		/* use stats from associated ap */
1957	default:
1958		if (ic->ic_bss != NULL)
1959			rssi_total = ic->ic_node_getrssi(ic->ic_bss);
1960		rssi_samples = 1;
1961		break;
1962	}
1963	return rssi_total / NZ(rssi_samples);
1964#undef NZ
1965}
1966
1967void
1968ieee80211_getsignal(struct ieee80211com *ic, int8_t *rssi, int8_t *noise)
1969{
1970
1971	if (ic->ic_bss == NULL)		/* NB: shouldn't happen */
1972		return;
1973	ic->ic_node_getsignal(ic->ic_bss, rssi, noise);
1974	/* for non-station mode return avg'd rssi accounting */
1975	if (ic->ic_opmode != IEEE80211_M_STA)
1976		*rssi = ieee80211_getrssi(ic);
1977}
1978
1979/*
1980 * Node table support.
1981 */
1982
1983static void
1984ieee80211_node_table_init(struct ieee80211com *ic,
1985	struct ieee80211_node_table *nt,
1986	const char *name, int inact, int keyixmax)
1987{
1988
1989	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1990		"%s %s table, inact %u\n", __func__, name, inact);
1991
1992	nt->nt_ic = ic;
1993	/* XXX need unit */
1994	IEEE80211_NODE_LOCK_INIT(nt, ic->ic_ifp->if_xname);
1995	IEEE80211_SCAN_LOCK_INIT(nt, ic->ic_ifp->if_xname);
1996	TAILQ_INIT(&nt->nt_node);
1997	nt->nt_name = name;
1998	nt->nt_scangen = 1;
1999	nt->nt_inact_init = inact;
2000	nt->nt_keyixmax = keyixmax;
2001	if (nt->nt_keyixmax > 0) {
2002		MALLOC(nt->nt_keyixmap, struct ieee80211_node **,
2003			keyixmax * sizeof(struct ieee80211_node *),
2004			M_80211_NODE, M_NOWAIT | M_ZERO);
2005		if (nt->nt_keyixmap == NULL)
2006			if_printf(ic->ic_ifp,
2007			    "Cannot allocate key index map with %u entries\n",
2008			    keyixmax);
2009	} else
2010		nt->nt_keyixmap = NULL;
2011}
2012
2013static void
2014ieee80211_node_table_reset(struct ieee80211_node_table *nt)
2015{
2016
2017	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
2018		"%s %s table\n", __func__, nt->nt_name);
2019
2020	IEEE80211_NODE_LOCK(nt);
2021	ieee80211_free_allnodes_locked(nt);
2022	IEEE80211_NODE_UNLOCK(nt);
2023}
2024
2025static void
2026ieee80211_node_table_cleanup(struct ieee80211_node_table *nt)
2027{
2028
2029	IEEE80211_DPRINTF(nt->nt_ic, IEEE80211_MSG_NODE,
2030		"%s %s table\n", __func__, nt->nt_name);
2031
2032	IEEE80211_NODE_LOCK(nt);
2033	ieee80211_free_allnodes_locked(nt);
2034	if (nt->nt_keyixmap != NULL) {
2035		/* XXX verify all entries are NULL */
2036		int i;
2037		for (i = 0; i < nt->nt_keyixmax; i++)
2038			if (nt->nt_keyixmap[i] != NULL)
2039				printf("%s: %s[%u] still active\n", __func__,
2040					nt->nt_name, i);
2041		FREE(nt->nt_keyixmap, M_80211_NODE);
2042		nt->nt_keyixmap = NULL;
2043	}
2044	IEEE80211_SCAN_LOCK_DESTROY(nt);
2045	IEEE80211_NODE_LOCK_DESTROY(nt);
2046}
2047