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