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