ieee80211_node.c revision 1.7 1 /* $NetBSD: ieee80211_node.c,v 1.7 2003/10/29 21:50:57 dyoung Exp $ */
2 /*-
3 * Copyright (c) 2001 Atsushi Onoe
4 * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * Alternatively, this software may be distributed under the terms of the
19 * GNU General Public License ("GPL") version 2 as published by the Free
20 * Software Foundation.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 #ifdef __FreeBSD__
36 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_node.c,v 1.6 2003/08/19 22:17:03 sam Exp $");
37 #else
38 __KERNEL_RCSID(0, "$NetBSD: ieee80211_node.c,v 1.7 2003/10/29 21:50:57 dyoung Exp $");
39 #endif
40
41 #include "opt_inet.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/mbuf.h>
46 #include <sys/malloc.h>
47 #include <sys/kernel.h>
48 #include <sys/socket.h>
49 #include <sys/sockio.h>
50 #include <sys/endian.h>
51 #include <sys/errno.h>
52 #ifdef __FreeBSD__
53 #include <sys/bus.h>
54 #endif
55 #include <sys/proc.h>
56 #include <sys/sysctl.h>
57
58 #ifdef __FreeBSD__
59 #include <machine/atomic.h>
60 #endif
61
62 #include <net/if.h>
63 #include <net/if_dl.h>
64 #include <net/if_media.h>
65 #include <net/if_arp.h>
66 #ifdef __FreeBSD__
67 #include <net/ethernet.h>
68 #else
69 #include <net/if_ether.h>
70 #endif
71 #include <net/if_llc.h>
72
73 #include <net80211/ieee80211_var.h>
74 #include <net80211/ieee80211_compat.h>
75
76 #include <net/bpf.h>
77
78 #ifdef INET
79 #include <netinet/in.h>
80 #ifdef __FreeBSD__
81 #include <netinet/if_ether.h>
82 #else
83 #include <net/if_ether.h>
84 #endif
85 #endif
86
87 static struct ieee80211_node *ieee80211_node_alloc(struct ieee80211com *);
88 static void ieee80211_node_free(struct ieee80211com *, struct ieee80211_node *);
89 static void ieee80211_node_copy(struct ieee80211com *,
90 struct ieee80211_node *, const struct ieee80211_node *);
91 static void ieee80211_setup_node(struct ieee80211com *ic,
92 struct ieee80211_node *ni, u_int8_t *macaddr);
93 static void _ieee80211_free_node(struct ieee80211com *,
94 struct ieee80211_node *);
95
96 void
97 ieee80211_node_attach(struct ifnet *ifp)
98 {
99 struct ieee80211com *ic = (void *)ifp;
100
101 #ifdef __FreeBSD__
102 /* XXX need unit */
103 mtx_init(&ic->ic_nodelock, ifp->if_name, "802.11 node table", MTX_DEF);
104 #endif
105 TAILQ_INIT(&ic->ic_node);
106 ic->ic_node_alloc = ieee80211_node_alloc;
107 ic->ic_node_free = ieee80211_node_free;
108 ic->ic_node_copy = ieee80211_node_copy;
109 }
110
111 void
112 ieee80211_node_lateattach(struct ifnet *ifp)
113 {
114 struct ieee80211com *ic = (void *)ifp;
115
116 ic->ic_bss = (*ic->ic_node_alloc)(ic);
117 KASSERT(ic->ic_bss != NULL, ("unable to setup inital BSS node"));
118 ic->ic_bss->ni_chan = IEEE80211_CHAN_ANYC;
119 }
120
121 void
122 ieee80211_node_detach(struct ifnet *ifp)
123 {
124 struct ieee80211com *ic = (void *)ifp;
125
126 if (ic->ic_bss != NULL)
127 (*ic->ic_node_free)(ic, ic->ic_bss);
128 ieee80211_free_allnodes(ic);
129 #ifdef __FreeBSD__
130 mtx_destroy(&ic->ic_nodelock);
131 #endif
132 }
133
134 /*
135 * AP scanning support.
136 */
137
138 /*
139 * Initialize the active channel set based on the set
140 * of available channels and the current PHY mode.
141 */
142 static void
143 ieee80211_reset_scan(struct ifnet *ifp)
144 {
145 struct ieee80211com *ic = (void *)ifp;
146
147 memcpy(ic->ic_chan_scan, ic->ic_chan_active,
148 sizeof(ic->ic_chan_active));
149 /* NB: hack, setup so next_scan starts with the first channel */
150 if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC)
151 ic->ic_bss->ni_chan = &ic->ic_channels[IEEE80211_CHAN_MAX];
152 }
153
154 /*
155 * Begin an active scan.
156 */
157 void
158 ieee80211_begin_scan(struct ifnet *ifp)
159 {
160 struct ieee80211com *ic = (void *)ifp;
161
162 /*
163 * In all but hostap mode scanning starts off in
164 * an active mode before switching to passive.
165 */
166 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
167 ic->ic_flags |= IEEE80211_F_ASCAN;
168 if (ifp->if_flags & IFF_DEBUG)
169 if_printf(ifp, "begin %s scan\n",
170 (ic->ic_flags & IEEE80211_F_ASCAN) ?
171 "active" : "passive");
172 /*
173 * Clear scan state and flush any previously seen
174 * AP's. Note that the latter assumes we don't act
175 * as both an AP and a station, otherwise we'll
176 * potentially flush state of stations associated
177 * with us.
178 */
179 ieee80211_reset_scan(ifp);
180 ieee80211_free_allnodes(ic);
181
182 /* Scan the next channel. */
183 ieee80211_next_scan(ifp);
184 }
185
186 /*
187 * Switch to the next channel marked for scanning.
188 */
189 void
190 ieee80211_next_scan(struct ifnet *ifp)
191 {
192 struct ieee80211com *ic = (void *)ifp;
193 struct ieee80211_channel *chan;
194
195 chan = ic->ic_bss->ni_chan;
196 for (;;) {
197 if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
198 chan = &ic->ic_channels[0];
199 if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
200 /*
201 * Honor channels marked passive-only
202 * during an active scan.
203 */
204 if ((ic->ic_flags & IEEE80211_F_ASCAN) == 0 ||
205 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0)
206 break;
207 }
208 if (chan == ic->ic_bss->ni_chan) {
209 ieee80211_end_scan(ifp);
210 return;
211 }
212 }
213 clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
214 IEEE80211_DPRINTF(("ieee80211_next_scan: chan %d->%d\n",
215 ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
216 ieee80211_chan2ieee(ic, chan)));
217 ic->ic_bss->ni_chan = chan;
218 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
219 }
220
221 void
222 ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
223 {
224 struct ieee80211_node *ni;
225 struct ifnet *ifp = &ic->ic_if;
226
227 ni = ic->ic_bss;
228 if (ifp->if_flags & IFF_DEBUG)
229 if_printf(ifp, "creating ibss\n");
230 ic->ic_flags |= IEEE80211_F_SIBSS;
231 ni->ni_chan = chan;
232 ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
233 IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr);
234 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
235 if (ic->ic_opmode == IEEE80211_M_IBSS)
236 ni->ni_bssid[0] |= 0x02; /* local bit for IBSS */
237 ni->ni_esslen = ic->ic_des_esslen;
238 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
239 ni->ni_rssi = 0;
240 ni->ni_rstamp = 0;
241 memset(ni->ni_tstamp, 0, sizeof(ni->ni_tstamp));
242 ni->ni_intval = ic->ic_lintval;
243 ni->ni_capinfo = IEEE80211_CAPINFO_IBSS;
244 if (ic->ic_flags & IEEE80211_F_WEPON)
245 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
246 if (ic->ic_phytype == IEEE80211_T_FH) {
247 ni->ni_fhdwell = 200; /* XXX */
248 ni->ni_fhindex = 1;
249 }
250 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
251 }
252
253 int
254 ieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
255 {
256 struct ifnet *ifp = &ic->ic_if;
257 u_int8_t rate;
258 int fail;
259
260 fail = 0;
261 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
262 fail |= 0x01;
263 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
264 ni->ni_chan != ic->ic_des_chan)
265 fail |= 0x01;
266 if (ic->ic_opmode == IEEE80211_M_IBSS) {
267 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
268 fail |= 0x02;
269 } else {
270 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
271 fail |= 0x02;
272 }
273 if (ic->ic_flags & IEEE80211_F_WEPON) {
274 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
275 fail |= 0x04;
276 } else {
277 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
278 fail |= 0x04;
279 }
280 rate = ieee80211_fix_rate(ic, ni, IEEE80211_F_DONEGO);
281 if (rate & IEEE80211_RATE_BASIC)
282 fail |= 0x08;
283 if (ic->ic_des_esslen != 0 &&
284 (ni->ni_esslen != ic->ic_des_esslen ||
285 memcmp(ni->ni_essid, ic->ic_des_essid,
286 ic->ic_des_esslen != 0)))
287 fail |= 0x10;
288 if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
289 !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
290 fail |= 0x20;
291 if (ifp->if_flags & IFF_DEBUG) {
292 printf(" %c %s", fail ? '-' : '+',
293 ether_sprintf(ni->ni_macaddr));
294 printf(" %s%c", ether_sprintf(ni->ni_bssid),
295 fail & 0x20 ? '!' : ' ');
296 printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
297 fail & 0x01 ? '!' : ' ');
298 printf(" %+4d", ni->ni_rssi);
299 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
300 fail & 0x08 ? '!' : ' ');
301 printf(" %4s%c",
302 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
303 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
304 "????",
305 fail & 0x02 ? '!' : ' ');
306 printf(" %3s%c ",
307 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
308 "wep" : "no",
309 fail & 0x04 ? '!' : ' ');
310 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
311 printf("%s\n", fail & 0x10 ? "!" : "");
312 }
313 return fail;
314 }
315
316 /*
317 * Complete a scan of potential channels.
318 */
319 void
320 ieee80211_end_scan(struct ifnet *ifp)
321 {
322 struct ieee80211com *ic = (void *)ifp;
323 struct ieee80211_node *ni, *nextbs, *selbs;
324 int i, fail;
325
326 ic->ic_flags &= ~IEEE80211_F_ASCAN;
327 ni = TAILQ_FIRST(&ic->ic_node);
328
329 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
330 /* XXX off stack? */
331 u_char occupied[roundup(IEEE80211_CHAN_MAX, NBBY)];
332 /*
333 * The passive scan to look for existing AP's completed,
334 * select a channel to camp on. Identify the channels
335 * that already have one or more AP's and try to locate
336 * an unnoccupied one. If that fails, pick a random
337 * channel from the active set.
338 */
339 for (; ni != NULL; ni = nextbs) {
340 ieee80211_ref_node(ni);
341 nextbs = TAILQ_NEXT(ni, ni_list);
342 setbit(occupied, ieee80211_chan2ieee(ic, ni->ni_chan));
343 ieee80211_free_node(ic, ni);
344 }
345 for (i = 0; i < IEEE80211_CHAN_MAX; i++)
346 if (isset(ic->ic_chan_active, i) && isclr(occupied, i))
347 break;
348 if (i == IEEE80211_CHAN_MAX) {
349 fail = arc4random() & 3; /* random 0-3 */
350 for (i = 0; i < IEEE80211_CHAN_MAX; i++)
351 if (isset(ic->ic_chan_active, i) && fail-- == 0)
352 break;
353 }
354 ieee80211_create_ibss(ic, &ic->ic_channels[i]);
355 return;
356 }
357 if (ni == NULL) {
358 IEEE80211_DPRINTF(("%s: no scan candidate\n", __func__));
359 notfound:
360 if (ic->ic_opmode == IEEE80211_M_IBSS &&
361 (ic->ic_flags & IEEE80211_F_IBSSON) &&
362 ic->ic_des_esslen != 0) {
363 ieee80211_create_ibss(ic, ic->ic_ibss_chan);
364 return;
365 }
366 /*
367 * Reset the list of channels to scan and start again.
368 */
369 ieee80211_reset_scan(ifp);
370 ieee80211_next_scan(ifp);
371 return;
372 }
373 selbs = NULL;
374 if (ifp->if_flags & IFF_DEBUG)
375 if_printf(ifp, "\tmacaddr bssid chan rssi rate flag wep essid\n");
376 for (; ni != NULL; ni = nextbs) {
377 ieee80211_ref_node(ni);
378 nextbs = TAILQ_NEXT(ni, ni_list);
379 if (ni->ni_fails) {
380 /*
381 * The configuration of the access points may change
382 * during my scan. So delete the entry for the AP
383 * and retry to associate if there is another beacon.
384 */
385 if (ni->ni_fails++ > 2)
386 ieee80211_free_node(ic, ni);
387 continue;
388 }
389 if (ieee80211_match_bss(ic, ni) == 0) {
390 if (selbs == NULL)
391 selbs = ni;
392 else if (ni->ni_rssi > selbs->ni_rssi) {
393 ieee80211_unref_node(&selbs);
394 selbs = ni;
395 } else
396 ieee80211_unref_node(&ni);
397 } else {
398 ieee80211_unref_node(&ni);
399 }
400 }
401 if (selbs == NULL)
402 goto notfound;
403 (*ic->ic_node_copy)(ic, ic->ic_bss, selbs);
404 if (ic->ic_opmode == IEEE80211_M_IBSS) {
405 ieee80211_fix_rate(ic, ic->ic_bss, IEEE80211_F_DOFRATE |
406 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
407 if (ic->ic_bss->ni_rates.rs_nrates == 0) {
408 selbs->ni_fails++;
409 ieee80211_unref_node(&selbs);
410 goto notfound;
411 }
412 ieee80211_unref_node(&selbs);
413 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
414 } else {
415 ieee80211_unref_node(&selbs);
416 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
417 }
418 }
419
420 int
421 ieee80211_get_rate(struct ieee80211com *ic)
422 {
423 u_int8_t (*rates)[IEEE80211_RATE_MAXSIZE];
424 int rate;
425
426 rates = &ic->ic_bss->ni_rates.rs_rates;
427
428 if (ic->ic_fixed_rate != -1)
429 rate = (*rates)[ic->ic_fixed_rate];
430 else if (ic->ic_state == IEEE80211_S_RUN)
431 rate = (*rates)[ic->ic_bss->ni_txrate];
432 else
433 rate = 0;
434
435 return rate & IEEE80211_RATE_VAL;
436 }
437
438 static struct ieee80211_node *
439 ieee80211_node_alloc(struct ieee80211com *ic)
440 {
441 return malloc(sizeof(struct ieee80211_node), M_DEVBUF,
442 M_NOWAIT | M_ZERO);
443 }
444
445 static void
446 ieee80211_node_free(struct ieee80211com *ic, struct ieee80211_node *ni)
447 {
448 if (ni->ni_challenge != NULL) {
449 free(ni->ni_challenge, M_DEVBUF);
450 ni->ni_challenge = NULL;
451 }
452 free(ni, M_DEVBUF);
453 }
454
455 static void
456 ieee80211_node_copy(struct ieee80211com *ic,
457 struct ieee80211_node *dst, const struct ieee80211_node *src)
458 {
459 *dst = *src;
460 dst->ni_challenge = NULL;
461 }
462
463 static void
464 ieee80211_setup_node(struct ieee80211com *ic,
465 struct ieee80211_node *ni, u_int8_t *macaddr)
466 {
467 int hash;
468 ieee80211_node_critsec_decl(s);
469
470 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
471 hash = IEEE80211_NODE_HASH(macaddr);
472 ni->ni_refcnt = 1; /* mark referenced */
473 ieee80211_node_critsec_begin(ic, s);
474 TAILQ_INSERT_TAIL(&ic->ic_node, ni, ni_list);
475 LIST_INSERT_HEAD(&ic->ic_hash[hash], ni, ni_hash);
476 /*
477 * Note we don't enable the inactive timer when acting
478 * as a station. Nodes created in this mode represent
479 * AP's identified while scanning. If we time them out
480 * then several things happen: we can't return the data
481 * to users to show the list of AP's we encountered, and
482 * more importantly, we'll incorrectly deauthenticate
483 * ourself because the inactivity timer will kick us off.
484 */
485 if (ic->ic_opmode != IEEE80211_M_STA)
486 ic->ic_inact_timer = IEEE80211_INACT_WAIT;
487 ieee80211_node_critsec_end(ic, s);
488 }
489
490 struct ieee80211_node *
491 ieee80211_alloc_node(struct ieee80211com *ic, u_int8_t *macaddr)
492 {
493 struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic);
494 if (ni != NULL)
495 ieee80211_setup_node(ic, ni, macaddr);
496 return ni;
497 }
498
499 struct ieee80211_node *
500 ieee80211_dup_bss(struct ieee80211com *ic, u_int8_t *macaddr)
501 {
502 struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic);
503 if (ni != NULL) {
504 memcpy(ni, ic->ic_bss, sizeof(struct ieee80211_node));
505 ieee80211_setup_node(ic, ni, macaddr);
506 }
507 return ni;
508 }
509
510 struct ieee80211_node *
511 ieee80211_find_node(struct ieee80211com *ic, u_int8_t *macaddr)
512 {
513 struct ieee80211_node *ni;
514 int hash;
515 ieee80211_node_critsec_decl(s);
516
517 hash = IEEE80211_NODE_HASH(macaddr);
518 ieee80211_node_critsec_begin(ic, s);
519 LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
520 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
521 ieee80211_node_incref(ni); /* mark referenced */
522 break;
523 }
524 }
525 ieee80211_node_critsec_end(ic, s);
526 return ni;
527 }
528
529 struct ieee80211_node *
530 ieee80211_find_txnode(struct ieee80211com *ic, u_int8_t *macaddr)
531 {
532 struct ieee80211_node *ni;
533 ieee80211_node_critsec_decl(s);
534
535 /*
536 * The destination address should be in the node table
537 * unless this is a multicast/broadcast frames or we are
538 * in station mode.
539 */
540 if (IEEE80211_IS_MULTICAST(macaddr) ||
541 ic->ic_opmode == IEEE80211_M_STA)
542 ni = ic->ic_bss;
543 else {
544 ieee80211_node_critsec_begin(ic, s);
545 ni = ieee80211_find_node(ic, macaddr);
546 if (ni == NULL) {
547 if (ic->ic_opmode != IEEE80211_M_MONITOR)
548 ni = ieee80211_dup_bss(ic, macaddr);
549 IEEE80211_DPRINTF(("%s: faked-up node %p for %s\n",
550 __func__, ni, ether_sprintf(macaddr)));
551 if (ni == NULL) {
552 ieee80211_node_critsec_end(ic, s);
553 /* ic->ic_stats.st_tx_nonode++; XXX statistic */
554 return NULL;
555 }
556 (void)ieee80211_ref_node(ni);
557 }
558 ieee80211_node_critsec_end(ic, s);
559 }
560 return ni;
561 }
562
563 /*
564 * For some types of packet and for some operating modes, it is
565 * desirable to process a Rx packet using its sender's node-record
566 * instead of the BSS record, when that is possible.
567 *
568 * - AP mode: it is desirable to keep a node-record for every
569 * authenticated/associated station *in the BSS*. For future use,
570 * we also track neighboring APs, since they might belong to the
571 * same ESSID.
572 *
573 * - IBSS mode: it is desirable to keep a node-record for every
574 * station *in the BSS*.
575 *
576 * - monitor mode: it is desirable to keep a node-record for every
577 * sender, regardless of BSS.
578 *
579 * - STA mode: the only available node-record is the BSS record,
580 * ic->ic_bss.
581 *
582 * Of all the 802.11 Control packets, only the node-records for
583 * RTS packets node-record can be looked up.
584 *
585 * Return non-zero if the packet's node-record is kept, zero
586 * otherwise.
587 */
588 static __inline int
589 ieee80211_needs_rxnode(struct ieee80211com *ic, struct ieee80211_frame *wh,
590 u_int8_t **bssid)
591 {
592 struct ieee80211_node *bss = ic->ic_bss;
593 int needsnode, rc = 0;
594
595 if (ic->ic_opmode == IEEE80211_M_STA)
596 return 0;
597
598 needsnode = (ic->ic_opmode == IEEE80211_M_MONITOR);
599
600 *bssid = NULL;
601
602 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
603 case IEEE80211_FC0_TYPE_CTL:
604 return (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
605 IEEE80211_FC0_SUBTYPE_RTS;
606
607 case IEEE80211_FC0_TYPE_MGT:
608 *bssid = wh->i_addr3;
609 rc = IEEE80211_ADDR_EQ(*bssid, bss->ni_bssid);
610 break;
611 case IEEE80211_FC0_TYPE_DATA:
612 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
613 case IEEE80211_FC1_DIR_NODS:
614 *bssid = wh->i_addr3;
615 if (ic->ic_opmode == IEEE80211_M_IBSS ||
616 ic->ic_opmode == IEEE80211_M_AHDEMO)
617 rc = IEEE80211_ADDR_EQ(*bssid, bss->ni_bssid);
618 break;
619 case IEEE80211_FC1_DIR_TODS:
620 *bssid = wh->i_addr1;
621 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
622 rc = IEEE80211_ADDR_EQ(*bssid, bss->ni_bssid);
623 break;
624 case IEEE80211_FC1_DIR_FROMDS:
625 case IEEE80211_FC1_DIR_DSTODS:
626 *bssid = wh->i_addr2;
627 rc = (ic->ic_opmode == IEEE80211_M_HOSTAP);
628 break;
629 }
630 break;
631 }
632 return needsnode || rc;
633 }
634
635 struct ieee80211_node *
636 ieee80211_find_rxnode(struct ieee80211com *ic, struct ieee80211_frame *wh)
637 {
638 struct ieee80211_node *ni;
639 const static u_int8_t zero[IEEE80211_ADDR_LEN];
640 u_int8_t *bssid;
641 ieee80211_node_critsec_decl(s);
642
643 ieee80211_node_critsec_begin(ic, s);
644
645 if (!ieee80211_needs_rxnode(ic, wh, &bssid))
646 return ieee80211_ref_node(ic->ic_bss);
647
648 ni = ieee80211_find_node(ic, wh->i_addr2);
649
650 if (ni == NULL) {
651 if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
652 if ((ni = ieee80211_dup_bss(ic, wh->i_addr2)) != NULL)
653 IEEE80211_ADDR_COPY(ni->ni_bssid,
654 (bssid != NULL) ? bssid : zero);
655
656 IEEE80211_DPRINTF(("%s: faked-up node %p for %s\n",
657 __func__, ni, ether_sprintf(wh->i_addr2)));
658 }
659 ni = ieee80211_ref_node((ni == NULL) ? ic->ic_bss : ni);
660 }
661 ieee80211_node_critsec_end(ic, s);
662 KASSERT(ni != NULL, ("%s: null node", __func__));
663 return ni;
664 }
665
666 /*
667 * Like find but search based on the channel too.
668 */
669 struct ieee80211_node *
670 ieee80211_lookup_node(struct ieee80211com *ic,
671 u_int8_t *macaddr, struct ieee80211_channel *chan)
672 {
673 struct ieee80211_node *ni;
674 int hash;
675 ieee80211_node_critsec_decl(s);
676
677 hash = IEEE80211_NODE_HASH(macaddr);
678 ieee80211_node_critsec_begin(ic, s);
679 LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
680 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) && ni->ni_chan == chan) {
681 ieee80211_node_incref(ni);/* mark referenced */
682 break;
683 }
684 }
685 ieee80211_node_critsec_end(ic, s);
686 return ni;
687 }
688
689 static void
690 _ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
691 {
692 KASSERT(ni != ic->ic_bss, ("freeing bss node"));
693
694 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
695 TAILQ_REMOVE(&ic->ic_node, ni, ni_list);
696 LIST_REMOVE(ni, ni_hash);
697 if (!IF_IS_EMPTY(&ni->ni_savedq)) {
698 IF_PURGE(&ni->ni_savedq);
699 if (ic->ic_set_tim)
700 ic->ic_set_tim(ic, ni->ni_associd, 0);
701 }
702 if (TAILQ_EMPTY(&ic->ic_node))
703 ic->ic_inact_timer = 0;
704 (*ic->ic_node_free)(ic, ni);
705 }
706
707 void
708 ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
709 {
710 ieee80211_node_critsec_decl(s);
711
712 KASSERT(ni != ic->ic_bss, ("freeing ic_bss"));
713
714 if (ieee80211_node_decref(ni) == 0) {
715 ieee80211_node_critsec_begin(ic, s);
716 _ieee80211_free_node(ic, ni);
717 ieee80211_node_critsec_end(ic, s);
718 }
719 }
720
721 void
722 ieee80211_free_allnodes(struct ieee80211com *ic)
723 {
724 struct ieee80211_node *ni;
725 ieee80211_node_critsec_decl(s);
726
727 ieee80211_node_critsec_begin(ic, s);
728 while ((ni = TAILQ_FIRST(&ic->ic_node)) != NULL)
729 _ieee80211_free_node(ic, ni);
730 ieee80211_node_critsec_end(ic, s);
731 }
732
733 void
734 ieee80211_timeout_nodes(struct ieee80211com *ic)
735 {
736 struct ieee80211_node *ni, *nextbs;
737 ieee80211_node_critsec_decl(s);
738
739 ieee80211_node_critsec_begin(ic, s);
740 for (ni = TAILQ_FIRST(&ic->ic_node); ni != NULL;) {
741 if (++ni->ni_inact > IEEE80211_INACT_MAX) {
742 IEEE80211_DPRINTF(("station %s timed out "
743 "due to inactivity (%u secs)\n",
744 ether_sprintf(ni->ni_macaddr),
745 ni->ni_inact));
746 nextbs = TAILQ_NEXT(ni, ni_list);
747 /*
748 * Send a deauthenticate frame.
749 */
750 IEEE80211_SEND_MGMT(ic, ni,
751 IEEE80211_FC0_SUBTYPE_DEAUTH,
752 IEEE80211_REASON_AUTH_EXPIRE);
753 ieee80211_free_node(ic, ni);
754 ni = nextbs;
755 } else
756 ni = TAILQ_NEXT(ni, ni_list);
757 }
758 if (!TAILQ_EMPTY(&ic->ic_node))
759 ic->ic_inact_timer = IEEE80211_INACT_WAIT;
760 ieee80211_node_critsec_end(ic, s);
761 }
762
763 void
764 ieee80211_iterate_nodes(struct ieee80211com *ic, ieee80211_iter_func *f, void *arg)
765 {
766 struct ieee80211_node *ni;
767 ieee80211_node_critsec_decl(s);
768
769 ieee80211_node_critsec_begin(ic, s);
770 TAILQ_FOREACH(ni, &ic->ic_node, ni_list)
771 (*f)(arg, ni);
772 ieee80211_node_critsec_end(ic, s);
773 }
774