ieee80211_node.c revision 1.4 1 1.4 dyoung /* $NetBSD: ieee80211_node.c,v 1.4 2003/09/23 15:59:09 dyoung Exp $ */
2 1.1 dyoung /*-
3 1.1 dyoung * Copyright (c) 2001 Atsushi Onoe
4 1.1 dyoung * Copyright (c) 2002, 2003 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.1 dyoung __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_node.c,v 1.6 2003/08/19 22:17:03 sam Exp $");
37 1.3 dyoung #else
38 1.4 dyoung __KERNEL_RCSID(0, "$NetBSD: ieee80211_node.c,v 1.4 2003/09/23 15:59:09 dyoung Exp $");
39 1.3 dyoung #endif
40 1.1 dyoung
41 1.1 dyoung #include "opt_inet.h"
42 1.1 dyoung
43 1.1 dyoung #include <sys/param.h>
44 1.1 dyoung #include <sys/systm.h>
45 1.1 dyoung #include <sys/mbuf.h>
46 1.1 dyoung #include <sys/malloc.h>
47 1.1 dyoung #include <sys/kernel.h>
48 1.1 dyoung #include <sys/socket.h>
49 1.1 dyoung #include <sys/sockio.h>
50 1.1 dyoung #include <sys/endian.h>
51 1.1 dyoung #include <sys/errno.h>
52 1.4 dyoung #ifdef __FreeBSD__
53 1.1 dyoung #include <sys/bus.h>
54 1.4 dyoung #endif
55 1.1 dyoung #include <sys/proc.h>
56 1.1 dyoung #include <sys/sysctl.h>
57 1.1 dyoung
58 1.2 dyoung #ifdef __FreeBSD__
59 1.1 dyoung #include <machine/atomic.h>
60 1.2 dyoung #endif
61 1.1 dyoung
62 1.1 dyoung #include <net/if.h>
63 1.1 dyoung #include <net/if_dl.h>
64 1.1 dyoung #include <net/if_media.h>
65 1.1 dyoung #include <net/if_arp.h>
66 1.2 dyoung #ifdef __FreeBSD__
67 1.1 dyoung #include <net/ethernet.h>
68 1.4 dyoung #else
69 1.4 dyoung #include <net/if_ether.h>
70 1.2 dyoung #endif
71 1.1 dyoung #include <net/if_llc.h>
72 1.1 dyoung
73 1.1 dyoung #include <net80211/ieee80211_var.h>
74 1.4 dyoung #include <net80211/ieee80211_compat.h>
75 1.1 dyoung
76 1.1 dyoung #include <net/bpf.h>
77 1.1 dyoung
78 1.1 dyoung #ifdef INET
79 1.1 dyoung #include <netinet/in.h>
80 1.4 dyoung #ifdef __FreeBSD__
81 1.1 dyoung #include <netinet/if_ether.h>
82 1.4 dyoung #else
83 1.4 dyoung #include <net/if_ether.h>
84 1.4 dyoung #endif
85 1.1 dyoung #endif
86 1.1 dyoung
87 1.1 dyoung static struct ieee80211_node *ieee80211_node_alloc(struct ieee80211com *);
88 1.1 dyoung static void ieee80211_node_free(struct ieee80211com *, struct ieee80211_node *);
89 1.1 dyoung static void ieee80211_node_copy(struct ieee80211com *,
90 1.1 dyoung struct ieee80211_node *, const struct ieee80211_node *);
91 1.1 dyoung static void ieee80211_setup_node(struct ieee80211com *ic,
92 1.1 dyoung struct ieee80211_node *ni, u_int8_t *macaddr);
93 1.1 dyoung static void _ieee80211_free_node(struct ieee80211com *,
94 1.1 dyoung struct ieee80211_node *);
95 1.1 dyoung
96 1.1 dyoung void
97 1.1 dyoung ieee80211_node_attach(struct ifnet *ifp)
98 1.1 dyoung {
99 1.1 dyoung struct ieee80211com *ic = (void *)ifp;
100 1.1 dyoung
101 1.2 dyoung #ifdef __FreeBSD__
102 1.1 dyoung /* XXX need unit */
103 1.1 dyoung mtx_init(&ic->ic_nodelock, ifp->if_name, "802.11 node table", MTX_DEF);
104 1.2 dyoung #endif
105 1.1 dyoung TAILQ_INIT(&ic->ic_node);
106 1.1 dyoung ic->ic_node_alloc = ieee80211_node_alloc;
107 1.1 dyoung ic->ic_node_free = ieee80211_node_free;
108 1.1 dyoung ic->ic_node_copy = ieee80211_node_copy;
109 1.1 dyoung }
110 1.1 dyoung
111 1.1 dyoung void
112 1.1 dyoung ieee80211_node_lateattach(struct ifnet *ifp)
113 1.1 dyoung {
114 1.1 dyoung struct ieee80211com *ic = (void *)ifp;
115 1.1 dyoung
116 1.1 dyoung ic->ic_bss = (*ic->ic_node_alloc)(ic);
117 1.1 dyoung KASSERT(ic->ic_bss != NULL, ("unable to setup inital BSS node"));
118 1.1 dyoung ic->ic_bss->ni_chan = IEEE80211_CHAN_ANYC;
119 1.1 dyoung }
120 1.1 dyoung
121 1.1 dyoung void
122 1.1 dyoung ieee80211_node_detach(struct ifnet *ifp)
123 1.1 dyoung {
124 1.1 dyoung struct ieee80211com *ic = (void *)ifp;
125 1.1 dyoung
126 1.1 dyoung if (ic->ic_bss != NULL)
127 1.1 dyoung (*ic->ic_node_free)(ic, ic->ic_bss);
128 1.1 dyoung ieee80211_free_allnodes(ic);
129 1.2 dyoung #ifdef __FreeBSD__
130 1.1 dyoung mtx_destroy(&ic->ic_nodelock);
131 1.2 dyoung #endif
132 1.1 dyoung }
133 1.1 dyoung
134 1.1 dyoung /*
135 1.1 dyoung * AP scanning support.
136 1.1 dyoung */
137 1.1 dyoung
138 1.1 dyoung /*
139 1.1 dyoung * Initialize the active channel set based on the set
140 1.1 dyoung * of available channels and the current PHY mode.
141 1.1 dyoung */
142 1.1 dyoung static void
143 1.1 dyoung ieee80211_reset_scan(struct ifnet *ifp)
144 1.1 dyoung {
145 1.1 dyoung struct ieee80211com *ic = (void *)ifp;
146 1.1 dyoung
147 1.1 dyoung memcpy(ic->ic_chan_scan, ic->ic_chan_active,
148 1.1 dyoung sizeof(ic->ic_chan_active));
149 1.1 dyoung /* NB: hack, setup so next_scan starts with the first channel */
150 1.1 dyoung if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC)
151 1.1 dyoung ic->ic_bss->ni_chan = &ic->ic_channels[IEEE80211_CHAN_MAX];
152 1.1 dyoung }
153 1.1 dyoung
154 1.1 dyoung /*
155 1.1 dyoung * Begin an active scan.
156 1.1 dyoung */
157 1.1 dyoung void
158 1.1 dyoung ieee80211_begin_scan(struct ifnet *ifp)
159 1.1 dyoung {
160 1.1 dyoung struct ieee80211com *ic = (void *)ifp;
161 1.1 dyoung
162 1.1 dyoung /*
163 1.1 dyoung * In all but hostap mode scanning starts off in
164 1.1 dyoung * an active mode before switching to passive.
165 1.1 dyoung */
166 1.1 dyoung if (ic->ic_opmode != IEEE80211_M_HOSTAP)
167 1.1 dyoung ic->ic_flags |= IEEE80211_F_ASCAN;
168 1.1 dyoung if (ifp->if_flags & IFF_DEBUG)
169 1.1 dyoung if_printf(ifp, "begin %s scan\n",
170 1.1 dyoung (ic->ic_flags & IEEE80211_F_ASCAN) ?
171 1.1 dyoung "active" : "passive");
172 1.1 dyoung /*
173 1.1 dyoung * Clear scan state and flush any previously seen
174 1.1 dyoung * AP's. Note that the latter assumes we don't act
175 1.1 dyoung * as both an AP and a station, otherwise we'll
176 1.1 dyoung * potentially flush state of stations associated
177 1.1 dyoung * with us.
178 1.1 dyoung */
179 1.1 dyoung ieee80211_reset_scan(ifp);
180 1.1 dyoung ieee80211_free_allnodes(ic);
181 1.1 dyoung
182 1.1 dyoung /* Scan the next channel. */
183 1.1 dyoung ieee80211_next_scan(ifp);
184 1.1 dyoung }
185 1.1 dyoung
186 1.1 dyoung /*
187 1.1 dyoung * Switch to the next channel marked for scanning.
188 1.1 dyoung */
189 1.1 dyoung void
190 1.1 dyoung ieee80211_next_scan(struct ifnet *ifp)
191 1.1 dyoung {
192 1.1 dyoung struct ieee80211com *ic = (void *)ifp;
193 1.1 dyoung struct ieee80211_channel *chan;
194 1.1 dyoung
195 1.1 dyoung chan = ic->ic_bss->ni_chan;
196 1.1 dyoung for (;;) {
197 1.1 dyoung if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
198 1.1 dyoung chan = &ic->ic_channels[0];
199 1.1 dyoung if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
200 1.1 dyoung /*
201 1.1 dyoung * Honor channels marked passive-only
202 1.1 dyoung * during an active scan.
203 1.1 dyoung */
204 1.1 dyoung if ((ic->ic_flags & IEEE80211_F_ASCAN) == 0 ||
205 1.1 dyoung (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0)
206 1.1 dyoung break;
207 1.1 dyoung }
208 1.1 dyoung if (chan == ic->ic_bss->ni_chan) {
209 1.1 dyoung ieee80211_end_scan(ifp);
210 1.1 dyoung return;
211 1.1 dyoung }
212 1.1 dyoung }
213 1.1 dyoung clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
214 1.1 dyoung IEEE80211_DPRINTF(("ieee80211_next_scan: chan %d->%d\n",
215 1.1 dyoung ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
216 1.1 dyoung ieee80211_chan2ieee(ic, chan)));
217 1.1 dyoung ic->ic_bss->ni_chan = chan;
218 1.1 dyoung ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
219 1.1 dyoung }
220 1.1 dyoung
221 1.1 dyoung void
222 1.1 dyoung ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
223 1.1 dyoung {
224 1.1 dyoung struct ieee80211_node *ni;
225 1.1 dyoung struct ifnet *ifp = &ic->ic_if;
226 1.1 dyoung
227 1.1 dyoung ni = ic->ic_bss;
228 1.1 dyoung if (ifp->if_flags & IFF_DEBUG)
229 1.1 dyoung if_printf(ifp, "creating ibss\n");
230 1.1 dyoung ic->ic_flags |= IEEE80211_F_SIBSS;
231 1.1 dyoung ni->ni_chan = chan;
232 1.1 dyoung ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
233 1.1 dyoung IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr);
234 1.1 dyoung IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
235 1.1 dyoung if (ic->ic_opmode == IEEE80211_M_IBSS)
236 1.1 dyoung ni->ni_bssid[0] |= 0x02; /* local bit for IBSS */
237 1.1 dyoung ni->ni_esslen = ic->ic_des_esslen;
238 1.1 dyoung memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
239 1.1 dyoung ni->ni_rssi = 0;
240 1.1 dyoung ni->ni_rstamp = 0;
241 1.1 dyoung memset(ni->ni_tstamp, 0, sizeof(ni->ni_tstamp));
242 1.1 dyoung ni->ni_intval = ic->ic_lintval;
243 1.1 dyoung ni->ni_capinfo = IEEE80211_CAPINFO_IBSS;
244 1.1 dyoung if (ic->ic_flags & IEEE80211_F_WEPON)
245 1.1 dyoung ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
246 1.1 dyoung if (ic->ic_phytype == IEEE80211_T_FH) {
247 1.1 dyoung ni->ni_fhdwell = 200; /* XXX */
248 1.1 dyoung ni->ni_fhindex = 1;
249 1.1 dyoung }
250 1.1 dyoung ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
251 1.1 dyoung }
252 1.1 dyoung
253 1.1 dyoung /*
254 1.1 dyoung * Complete a scan of potential channels.
255 1.1 dyoung */
256 1.1 dyoung void
257 1.1 dyoung ieee80211_end_scan(struct ifnet *ifp)
258 1.1 dyoung {
259 1.1 dyoung struct ieee80211com *ic = (void *)ifp;
260 1.1 dyoung struct ieee80211_node *ni, *nextbs, *selbs;
261 1.1 dyoung u_int8_t rate;
262 1.1 dyoung int i, fail;
263 1.1 dyoung
264 1.1 dyoung ic->ic_flags &= ~IEEE80211_F_ASCAN;
265 1.1 dyoung ni = TAILQ_FIRST(&ic->ic_node);
266 1.1 dyoung
267 1.1 dyoung if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
268 1.1 dyoung /* XXX off stack? */
269 1.1 dyoung u_char occupied[roundup(IEEE80211_CHAN_MAX, NBBY)];
270 1.1 dyoung /*
271 1.1 dyoung * The passive scan to look for existing AP's completed,
272 1.1 dyoung * select a channel to camp on. Identify the channels
273 1.1 dyoung * that already have one or more AP's and try to locate
274 1.1 dyoung * an unnoccupied one. If that fails, pick a random
275 1.1 dyoung * channel from the active set.
276 1.1 dyoung */
277 1.1 dyoung for (; ni != NULL; ni = nextbs) {
278 1.1 dyoung ieee80211_ref_node(ni);
279 1.1 dyoung nextbs = TAILQ_NEXT(ni, ni_list);
280 1.1 dyoung setbit(occupied, ieee80211_chan2ieee(ic, ni->ni_chan));
281 1.1 dyoung ieee80211_free_node(ic, ni);
282 1.1 dyoung }
283 1.1 dyoung for (i = 0; i < IEEE80211_CHAN_MAX; i++)
284 1.1 dyoung if (isset(ic->ic_chan_active, i) && isclr(occupied, i))
285 1.1 dyoung break;
286 1.1 dyoung if (i == IEEE80211_CHAN_MAX) {
287 1.1 dyoung fail = arc4random() & 3; /* random 0-3 */
288 1.1 dyoung for (i = 0; i < IEEE80211_CHAN_MAX; i++)
289 1.1 dyoung if (isset(ic->ic_chan_active, i) && fail-- == 0)
290 1.1 dyoung break;
291 1.1 dyoung }
292 1.1 dyoung ieee80211_create_ibss(ic, &ic->ic_channels[i]);
293 1.1 dyoung return;
294 1.1 dyoung }
295 1.1 dyoung if (ni == NULL) {
296 1.1 dyoung IEEE80211_DPRINTF(("%s: no scan candidate\n", __func__));
297 1.1 dyoung notfound:
298 1.1 dyoung if (ic->ic_opmode == IEEE80211_M_IBSS &&
299 1.1 dyoung (ic->ic_flags & IEEE80211_F_IBSSON) &&
300 1.1 dyoung ic->ic_des_esslen != 0) {
301 1.1 dyoung ieee80211_create_ibss(ic, ic->ic_ibss_chan);
302 1.1 dyoung return;
303 1.1 dyoung }
304 1.1 dyoung /*
305 1.1 dyoung * Reset the list of channels to scan and start again.
306 1.1 dyoung */
307 1.1 dyoung ieee80211_reset_scan(ifp);
308 1.1 dyoung ieee80211_next_scan(ifp);
309 1.1 dyoung return;
310 1.1 dyoung }
311 1.1 dyoung selbs = NULL;
312 1.1 dyoung if (ifp->if_flags & IFF_DEBUG)
313 1.1 dyoung if_printf(ifp, "\tmacaddr bssid chan rssi rate flag wep essid\n");
314 1.1 dyoung for (; ni != NULL; ni = nextbs) {
315 1.1 dyoung ieee80211_ref_node(ni);
316 1.1 dyoung nextbs = TAILQ_NEXT(ni, ni_list);
317 1.1 dyoung if (ni->ni_fails) {
318 1.1 dyoung /*
319 1.1 dyoung * The configuration of the access points may change
320 1.1 dyoung * during my scan. So delete the entry for the AP
321 1.1 dyoung * and retry to associate if there is another beacon.
322 1.1 dyoung */
323 1.1 dyoung if (ni->ni_fails++ > 2)
324 1.1 dyoung ieee80211_free_node(ic, ni);
325 1.1 dyoung continue;
326 1.1 dyoung }
327 1.1 dyoung fail = 0;
328 1.1 dyoung if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
329 1.1 dyoung fail |= 0x01;
330 1.1 dyoung if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
331 1.1 dyoung ni->ni_chan != ic->ic_des_chan)
332 1.1 dyoung fail |= 0x01;
333 1.1 dyoung if (ic->ic_opmode == IEEE80211_M_IBSS) {
334 1.1 dyoung if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
335 1.1 dyoung fail |= 0x02;
336 1.1 dyoung } else {
337 1.1 dyoung if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
338 1.1 dyoung fail |= 0x02;
339 1.1 dyoung }
340 1.1 dyoung if (ic->ic_flags & IEEE80211_F_WEPON) {
341 1.1 dyoung if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
342 1.1 dyoung fail |= 0x04;
343 1.1 dyoung } else {
344 1.1 dyoung if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
345 1.1 dyoung fail |= 0x04;
346 1.1 dyoung }
347 1.1 dyoung rate = ieee80211_fix_rate(ic, ni, IEEE80211_F_DONEGO);
348 1.1 dyoung if (rate & IEEE80211_RATE_BASIC)
349 1.1 dyoung fail |= 0x08;
350 1.1 dyoung if (ic->ic_des_esslen != 0 &&
351 1.1 dyoung (ni->ni_esslen != ic->ic_des_esslen ||
352 1.1 dyoung memcmp(ni->ni_essid, ic->ic_des_essid,
353 1.1 dyoung ic->ic_des_esslen != 0)))
354 1.1 dyoung fail |= 0x10;
355 1.1 dyoung if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
356 1.1 dyoung !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
357 1.1 dyoung fail |= 0x20;
358 1.1 dyoung if (ifp->if_flags & IFF_DEBUG) {
359 1.1 dyoung printf(" %c %s", fail ? '-' : '+',
360 1.1 dyoung ether_sprintf(ni->ni_macaddr));
361 1.1 dyoung printf(" %s%c", ether_sprintf(ni->ni_bssid),
362 1.1 dyoung fail & 0x20 ? '!' : ' ');
363 1.1 dyoung printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
364 1.1 dyoung fail & 0x01 ? '!' : ' ');
365 1.1 dyoung printf(" %+4d", ni->ni_rssi);
366 1.1 dyoung printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
367 1.1 dyoung fail & 0x08 ? '!' : ' ');
368 1.1 dyoung printf(" %4s%c",
369 1.1 dyoung (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
370 1.1 dyoung (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
371 1.1 dyoung "????",
372 1.1 dyoung fail & 0x02 ? '!' : ' ');
373 1.1 dyoung printf(" %3s%c ",
374 1.1 dyoung (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
375 1.1 dyoung "wep" : "no",
376 1.1 dyoung fail & 0x04 ? '!' : ' ');
377 1.1 dyoung ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
378 1.1 dyoung printf("%s\n", fail & 0x10 ? "!" : "");
379 1.1 dyoung }
380 1.1 dyoung if (!fail) {
381 1.1 dyoung if (selbs == NULL)
382 1.1 dyoung selbs = ni;
383 1.1 dyoung else if (ni->ni_rssi > selbs->ni_rssi) {
384 1.1 dyoung ieee80211_unref_node(&selbs);
385 1.1 dyoung selbs = ni;
386 1.1 dyoung } else
387 1.1 dyoung ieee80211_unref_node(&ni);
388 1.1 dyoung } else {
389 1.1 dyoung ieee80211_unref_node(&ni);
390 1.1 dyoung }
391 1.1 dyoung }
392 1.1 dyoung if (selbs == NULL)
393 1.1 dyoung goto notfound;
394 1.1 dyoung (*ic->ic_node_copy)(ic, ic->ic_bss, selbs);
395 1.1 dyoung if (ic->ic_opmode == IEEE80211_M_IBSS) {
396 1.1 dyoung ieee80211_fix_rate(ic, ic->ic_bss, IEEE80211_F_DOFRATE |
397 1.1 dyoung IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
398 1.1 dyoung if (ic->ic_bss->ni_rates.rs_nrates == 0) {
399 1.1 dyoung selbs->ni_fails++;
400 1.1 dyoung ieee80211_unref_node(&selbs);
401 1.1 dyoung goto notfound;
402 1.1 dyoung }
403 1.1 dyoung ieee80211_unref_node(&selbs);
404 1.1 dyoung ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
405 1.1 dyoung } else {
406 1.1 dyoung ieee80211_unref_node(&selbs);
407 1.1 dyoung ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
408 1.1 dyoung }
409 1.1 dyoung }
410 1.1 dyoung
411 1.1 dyoung static struct ieee80211_node *
412 1.1 dyoung ieee80211_node_alloc(struct ieee80211com *ic)
413 1.1 dyoung {
414 1.1 dyoung return malloc(sizeof(struct ieee80211_node), M_DEVBUF,
415 1.1 dyoung M_NOWAIT | M_ZERO);
416 1.1 dyoung }
417 1.1 dyoung
418 1.1 dyoung static void
419 1.1 dyoung ieee80211_node_free(struct ieee80211com *ic, struct ieee80211_node *ni)
420 1.1 dyoung {
421 1.1 dyoung free(ni, M_DEVBUF);
422 1.1 dyoung }
423 1.1 dyoung
424 1.1 dyoung static void
425 1.1 dyoung ieee80211_node_copy(struct ieee80211com *ic,
426 1.1 dyoung struct ieee80211_node *dst, const struct ieee80211_node *src)
427 1.1 dyoung {
428 1.1 dyoung *dst = *src;
429 1.1 dyoung }
430 1.1 dyoung
431 1.1 dyoung static void
432 1.1 dyoung ieee80211_setup_node(struct ieee80211com *ic,
433 1.1 dyoung struct ieee80211_node *ni, u_int8_t *macaddr)
434 1.1 dyoung {
435 1.1 dyoung int hash;
436 1.2 dyoung ieee80211_node_critsec_decl(s);
437 1.1 dyoung
438 1.1 dyoung IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
439 1.1 dyoung hash = IEEE80211_NODE_HASH(macaddr);
440 1.1 dyoung ni->ni_refcnt = 1; /* mark referenced */
441 1.2 dyoung ieee80211_node_critsec_begin(ic, s);
442 1.1 dyoung TAILQ_INSERT_TAIL(&ic->ic_node, ni, ni_list);
443 1.1 dyoung LIST_INSERT_HEAD(&ic->ic_hash[hash], ni, ni_hash);
444 1.1 dyoung /*
445 1.1 dyoung * Note we don't enable the inactive timer when acting
446 1.1 dyoung * as a station. Nodes created in this mode represent
447 1.1 dyoung * AP's identified while scanning. If we time them out
448 1.1 dyoung * then several things happen: we can't return the data
449 1.1 dyoung * to users to show the list of AP's we encountered, and
450 1.1 dyoung * more importantly, we'll incorrectly deauthenticate
451 1.1 dyoung * ourself because the inactivity timer will kick us off.
452 1.1 dyoung */
453 1.1 dyoung if (ic->ic_opmode != IEEE80211_M_STA)
454 1.1 dyoung ic->ic_inact_timer = IEEE80211_INACT_WAIT;
455 1.2 dyoung ieee80211_node_critsec_end(ic, s);
456 1.1 dyoung }
457 1.1 dyoung
458 1.1 dyoung struct ieee80211_node *
459 1.1 dyoung ieee80211_alloc_node(struct ieee80211com *ic, u_int8_t *macaddr)
460 1.1 dyoung {
461 1.1 dyoung struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic);
462 1.1 dyoung if (ni != NULL)
463 1.1 dyoung ieee80211_setup_node(ic, ni, macaddr);
464 1.1 dyoung return ni;
465 1.1 dyoung }
466 1.1 dyoung
467 1.1 dyoung struct ieee80211_node *
468 1.1 dyoung ieee80211_dup_bss(struct ieee80211com *ic, u_int8_t *macaddr)
469 1.1 dyoung {
470 1.1 dyoung struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic);
471 1.1 dyoung if (ni != NULL) {
472 1.1 dyoung memcpy(ni, ic->ic_bss, sizeof(struct ieee80211_node));
473 1.1 dyoung ieee80211_setup_node(ic, ni, macaddr);
474 1.1 dyoung }
475 1.1 dyoung return ni;
476 1.1 dyoung }
477 1.1 dyoung
478 1.1 dyoung struct ieee80211_node *
479 1.1 dyoung ieee80211_find_node(struct ieee80211com *ic, u_int8_t *macaddr)
480 1.1 dyoung {
481 1.1 dyoung struct ieee80211_node *ni;
482 1.1 dyoung int hash;
483 1.2 dyoung ieee80211_node_critsec_decl(s);
484 1.1 dyoung
485 1.1 dyoung hash = IEEE80211_NODE_HASH(macaddr);
486 1.2 dyoung ieee80211_node_critsec_begin(ic, s);
487 1.1 dyoung LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
488 1.1 dyoung if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
489 1.2 dyoung ieee80211_node_incref(ni); /* mark referenced */
490 1.1 dyoung break;
491 1.1 dyoung }
492 1.1 dyoung }
493 1.2 dyoung ieee80211_node_critsec_end(ic, s);
494 1.1 dyoung return ni;
495 1.1 dyoung }
496 1.1 dyoung
497 1.1 dyoung /*
498 1.1 dyoung * Like find but search based on the channel too.
499 1.1 dyoung */
500 1.1 dyoung struct ieee80211_node *
501 1.1 dyoung ieee80211_lookup_node(struct ieee80211com *ic,
502 1.1 dyoung u_int8_t *macaddr, struct ieee80211_channel *chan)
503 1.1 dyoung {
504 1.1 dyoung struct ieee80211_node *ni;
505 1.1 dyoung int hash;
506 1.2 dyoung ieee80211_node_critsec_decl(s);
507 1.1 dyoung
508 1.1 dyoung hash = IEEE80211_NODE_HASH(macaddr);
509 1.2 dyoung ieee80211_node_critsec_begin(ic, s);
510 1.1 dyoung LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
511 1.1 dyoung if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && ni->ni_chan == chan) {
512 1.2 dyoung ieee80211_node_incref(ni);/* mark referenced */
513 1.1 dyoung break;
514 1.1 dyoung }
515 1.1 dyoung }
516 1.2 dyoung ieee80211_node_critsec_end(ic, s);
517 1.1 dyoung return ni;
518 1.1 dyoung }
519 1.1 dyoung
520 1.1 dyoung static void
521 1.1 dyoung _ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
522 1.1 dyoung {
523 1.1 dyoung KASSERT(ni != ic->ic_bss, ("freeing bss node"));
524 1.1 dyoung
525 1.1 dyoung TAILQ_REMOVE(&ic->ic_node, ni, ni_list);
526 1.1 dyoung LIST_REMOVE(ni, ni_hash);
527 1.1 dyoung if (TAILQ_EMPTY(&ic->ic_node))
528 1.1 dyoung ic->ic_inact_timer = 0;
529 1.1 dyoung (*ic->ic_node_free)(ic, ni);
530 1.1 dyoung }
531 1.1 dyoung
532 1.1 dyoung void
533 1.1 dyoung ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
534 1.1 dyoung {
535 1.2 dyoung ieee80211_node_critsec_decl(s);
536 1.2 dyoung
537 1.1 dyoung KASSERT(ni != ic->ic_bss, ("freeing ic_bss"));
538 1.1 dyoung
539 1.2 dyoung if (ieee80211_node_decref(ni) == 0) {
540 1.2 dyoung ieee80211_node_critsec_begin(ic, s);
541 1.1 dyoung _ieee80211_free_node(ic, ni);
542 1.2 dyoung ieee80211_node_critsec_end(ic, s);
543 1.1 dyoung }
544 1.1 dyoung }
545 1.1 dyoung
546 1.1 dyoung void
547 1.1 dyoung ieee80211_free_allnodes(struct ieee80211com *ic)
548 1.1 dyoung {
549 1.1 dyoung struct ieee80211_node *ni;
550 1.2 dyoung ieee80211_node_critsec_decl(s);
551 1.1 dyoung
552 1.2 dyoung ieee80211_node_critsec_begin(ic, s);
553 1.1 dyoung while ((ni = TAILQ_FIRST(&ic->ic_node)) != NULL)
554 1.1 dyoung _ieee80211_free_node(ic, ni);
555 1.2 dyoung ieee80211_node_critsec_end(ic, s);
556 1.1 dyoung }
557 1.1 dyoung
558 1.1 dyoung void
559 1.1 dyoung ieee80211_timeout_nodes(struct ieee80211com *ic)
560 1.1 dyoung {
561 1.1 dyoung struct ieee80211_node *ni, *nextbs;
562 1.2 dyoung ieee80211_node_critsec_decl(s);
563 1.1 dyoung
564 1.2 dyoung ieee80211_node_critsec_begin(ic, s);
565 1.1 dyoung for (ni = TAILQ_FIRST(&ic->ic_node); ni != NULL;) {
566 1.1 dyoung if (++ni->ni_inact > IEEE80211_INACT_MAX) {
567 1.1 dyoung IEEE80211_DPRINTF(("station %s timed out "
568 1.1 dyoung "due to inactivity (%u secs)\n",
569 1.1 dyoung ether_sprintf(ni->ni_macaddr),
570 1.1 dyoung ni->ni_inact));
571 1.1 dyoung nextbs = TAILQ_NEXT(ni, ni_list);
572 1.1 dyoung /*
573 1.1 dyoung * Send a deauthenticate frame.
574 1.1 dyoung */
575 1.1 dyoung IEEE80211_SEND_MGMT(ic, ni,
576 1.1 dyoung IEEE80211_FC0_SUBTYPE_DEAUTH,
577 1.1 dyoung IEEE80211_REASON_AUTH_EXPIRE);
578 1.1 dyoung ieee80211_free_node(ic, ni);
579 1.1 dyoung ni = nextbs;
580 1.1 dyoung } else
581 1.1 dyoung ni = TAILQ_NEXT(ni, ni_list);
582 1.1 dyoung }
583 1.1 dyoung if (!TAILQ_EMPTY(&ic->ic_node))
584 1.1 dyoung ic->ic_inact_timer = IEEE80211_INACT_WAIT;
585 1.2 dyoung ieee80211_node_critsec_end(ic, s);
586 1.1 dyoung }
587 1.1 dyoung
588 1.1 dyoung void
589 1.1 dyoung ieee80211_iterate_nodes(struct ieee80211com *ic, ieee80211_iter_func *f, void *arg)
590 1.1 dyoung {
591 1.1 dyoung struct ieee80211_node *ni;
592 1.2 dyoung ieee80211_node_critsec_decl(s);
593 1.1 dyoung
594 1.2 dyoung ieee80211_node_critsec_begin(ic, s);
595 1.1 dyoung TAILQ_FOREACH(ni, &ic->ic_node, ni_list)
596 1.1 dyoung (*f)(arg, ni);
597 1.2 dyoung ieee80211_node_critsec_end(ic, s);
598 1.1 dyoung }
599