ieee80211_node.c revision 1.11 1 /* $NetBSD: ieee80211_node.c,v 1.11 2004/04/30 23:58:14 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.22 2004/04/05 04:15:55 sam Exp $");
37 #else
38 __KERNEL_RCSID(0, "$NetBSD: ieee80211_node.c,v 1.11 2004/04/30 23:58:14 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 u_int8_t ieee80211_node_getrssi(struct ieee80211com *,
92 struct ieee80211_node *);
93
94 static void ieee80211_setup_node(struct ieee80211com *ic,
95 struct ieee80211_node *ni, u_int8_t *macaddr);
96 static void _ieee80211_free_node(struct ieee80211com *,
97 struct ieee80211_node *);
98
99 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
100
101 void
102 ieee80211_node_attach(struct ifnet *ifp)
103 {
104 struct ieee80211com *ic = (void *)ifp;
105
106 #ifdef __FreeBSD__
107 /* XXX need unit */
108 IEEE80211_NODE_LOCK_INIT(ic, ifp->if_xname);
109 #endif
110 TAILQ_INIT(&ic->ic_node);
111 ic->ic_node_alloc = ieee80211_node_alloc;
112 ic->ic_node_free = ieee80211_node_free;
113 ic->ic_node_copy = ieee80211_node_copy;
114 ic->ic_node_getrssi = ieee80211_node_getrssi;
115 ic->ic_scangen = 1;
116 }
117
118 void
119 ieee80211_node_lateattach(struct ifnet *ifp)
120 {
121 struct ieee80211com *ic = (void *)ifp;
122 struct ieee80211_node *ni;
123
124 ni = (*ic->ic_node_alloc)(ic);
125 IASSERT(ni != NULL, ("unable to setup inital BSS node"));
126 ni->ni_chan = IEEE80211_CHAN_ANYC;
127 ic->ic_bss = ni;
128 ic->ic_txpower = IEEE80211_TXPOWER_MAX;
129 }
130
131 void
132 ieee80211_node_detach(struct ifnet *ifp)
133 {
134 struct ieee80211com *ic = (void *)ifp;
135
136 if (ic->ic_bss != NULL)
137 (*ic->ic_node_free)(ic, ic->ic_bss);
138 ieee80211_free_allnodes(ic);
139 #ifdef __FreeBSD__
140 IEEE80211_NODE_LOCK_DESTROY(ic);
141 #endif
142 }
143
144 /*
145 * AP scanning support.
146 */
147
148 /*
149 * Initialize the active channel set based on the set
150 * of available channels and the current PHY mode.
151 */
152 static void
153 ieee80211_reset_scan(struct ifnet *ifp)
154 {
155 struct ieee80211com *ic = (void *)ifp;
156
157 memcpy(ic->ic_chan_scan, ic->ic_chan_active,
158 sizeof(ic->ic_chan_active));
159 /* NB: hack, setup so next_scan starts with the first channel */
160 if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC)
161 ic->ic_bss->ni_chan = &ic->ic_channels[IEEE80211_CHAN_MAX];
162 }
163
164 /*
165 * Begin an active scan.
166 */
167 void
168 ieee80211_begin_scan(struct ifnet *ifp)
169 {
170 struct ieee80211com *ic = (void *)ifp;
171
172 /*
173 * In all but hostap mode scanning starts off in
174 * an active mode before switching to passive.
175 */
176 if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
177 ic->ic_flags |= IEEE80211_F_ASCAN;
178 ic->ic_stats.is_scan_active++;
179 } else
180 ic->ic_stats.is_scan_passive++;
181 if (ifp->if_flags & IFF_DEBUG)
182 if_printf(ifp, "begin %s scan\n",
183 (ic->ic_flags & IEEE80211_F_ASCAN) ?
184 "active" : "passive");
185 /*
186 * Clear scan state and flush any previously seen
187 * AP's. Note that the latter assumes we don't act
188 * as both an AP and a station, otherwise we'll
189 * potentially flush state of stations associated
190 * with us.
191 */
192 ieee80211_reset_scan(ifp);
193 ieee80211_free_allnodes(ic);
194
195 /* Scan the next channel. */
196 ieee80211_next_scan(ifp);
197 }
198
199 /*
200 * Switch to the next channel marked for scanning.
201 */
202 void
203 ieee80211_next_scan(struct ifnet *ifp)
204 {
205 struct ieee80211com *ic = (void *)ifp;
206 struct ieee80211_channel *chan;
207
208 chan = ic->ic_bss->ni_chan;
209 for (;;) {
210 if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
211 chan = &ic->ic_channels[0];
212 if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
213 /*
214 * Honor channels marked passive-only
215 * during an active scan.
216 */
217 if ((ic->ic_flags & IEEE80211_F_ASCAN) == 0 ||
218 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0)
219 break;
220 }
221 if (chan == ic->ic_bss->ni_chan) {
222 ieee80211_end_scan(ifp);
223 return;
224 }
225 }
226 clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
227 IEEE80211_DPRINTF(("ieee80211_next_scan: chan %d->%d\n",
228 ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
229 ieee80211_chan2ieee(ic, chan)));
230 ic->ic_bss->ni_chan = chan;
231 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
232 }
233
234 void
235 ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
236 {
237 struct ieee80211_node *ni;
238 struct ifnet *ifp = &ic->ic_if;
239
240 ni = ic->ic_bss;
241 if (ifp->if_flags & IFF_DEBUG)
242 if_printf(ifp, "creating ibss\n");
243 ic->ic_flags |= IEEE80211_F_SIBSS;
244 ni->ni_chan = chan;
245 ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
246 IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr);
247 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
248 if (ic->ic_opmode == IEEE80211_M_IBSS)
249 ni->ni_bssid[0] |= 0x02; /* local bit for IBSS */
250 ni->ni_esslen = ic->ic_des_esslen;
251 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
252 ni->ni_rssi = 0;
253 ni->ni_rstamp = 0;
254 memset(ni->ni_tstamp, 0, sizeof(ni->ni_tstamp));
255 ni->ni_intval = ic->ic_lintval;
256 ni->ni_capinfo = IEEE80211_CAPINFO_IBSS;
257 if (ic->ic_flags & IEEE80211_F_WEPON)
258 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
259 if (ic->ic_phytype == IEEE80211_T_FH) {
260 ni->ni_fhdwell = 200; /* XXX */
261 ni->ni_fhindex = 1;
262 }
263 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
264 }
265
266 int
267 ieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
268 {
269 struct ifnet *ifp = &ic->ic_if;
270 u_int8_t rate;
271 int fail;
272
273 fail = 0;
274 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
275 fail |= 0x01;
276 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
277 ni->ni_chan != ic->ic_des_chan)
278 fail |= 0x01;
279 if (ic->ic_opmode == IEEE80211_M_IBSS) {
280 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
281 fail |= 0x02;
282 } else {
283 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
284 fail |= 0x02;
285 }
286 if (ic->ic_flags & IEEE80211_F_WEPON) {
287 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
288 fail |= 0x04;
289 } else {
290 /* XXX does this mean privacy is supported or required? */
291 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
292 fail |= 0x04;
293 }
294 rate = ieee80211_fix_rate(ic, ni, IEEE80211_F_DONEGO);
295 if (rate & IEEE80211_RATE_BASIC)
296 fail |= 0x08;
297 if (ic->ic_des_esslen != 0 &&
298 (ni->ni_esslen != ic->ic_des_esslen ||
299 memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
300 fail |= 0x10;
301 if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
302 !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
303 fail |= 0x20;
304 #ifdef IEEE80211_DEBUG
305 if (ifp->if_flags & IFF_DEBUG) {
306 printf(" %c %s", fail ? '-' : '+',
307 ether_sprintf(ni->ni_macaddr));
308 printf(" %s%c", ether_sprintf(ni->ni_bssid),
309 fail & 0x20 ? '!' : ' ');
310 printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
311 fail & 0x01 ? '!' : ' ');
312 printf(" %+4d", ni->ni_rssi);
313 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
314 fail & 0x08 ? '!' : ' ');
315 printf(" %4s%c",
316 (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
317 (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
318 "????",
319 fail & 0x02 ? '!' : ' ');
320 printf(" %3s%c ",
321 (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
322 "wep" : "no",
323 fail & 0x04 ? '!' : ' ');
324 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
325 printf("%s\n", fail & 0x10 ? "!" : "");
326 }
327 #endif
328 return fail;
329 }
330
331 /*
332 * Complete a scan of potential channels.
333 */
334 void
335 ieee80211_end_scan(struct ifnet *ifp)
336 {
337 struct ieee80211com *ic = (void *)ifp;
338 struct ieee80211_node *ni, *nextbs, *selbs;
339 int i, fail;
340
341 ic->ic_flags &= ~IEEE80211_F_ASCAN;
342 ni = TAILQ_FIRST(&ic->ic_node);
343
344 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
345 /* XXX off stack? */
346 u_char occupied[roundup(IEEE80211_CHAN_MAX, NBBY)];
347 /*
348 * The passive scan to look for existing AP's completed,
349 * select a channel to camp on. Identify the channels
350 * that already have one or more AP's and try to locate
351 * an unnoccupied one. If that fails, pick a random
352 * channel from the active set.
353 */
354 for (; ni != NULL; ni = nextbs) {
355 ieee80211_ref_node(ni);
356 nextbs = TAILQ_NEXT(ni, ni_list);
357 setbit(occupied, ieee80211_chan2ieee(ic, ni->ni_chan));
358 ieee80211_free_node(ic, ni);
359 }
360 for (i = 0; i < IEEE80211_CHAN_MAX; i++)
361 if (isset(ic->ic_chan_active, i) && isclr(occupied, i))
362 break;
363 if (i == IEEE80211_CHAN_MAX) {
364 fail = arc4random() & 3; /* random 0-3 */
365 for (i = 0; i < IEEE80211_CHAN_MAX; i++)
366 if (isset(ic->ic_chan_active, i) && fail-- == 0)
367 break;
368 }
369 ieee80211_create_ibss(ic, &ic->ic_channels[i]);
370 return;
371 }
372 if (ni == NULL) {
373 IEEE80211_DPRINTF(("%s: no scan candidate\n", __func__));
374 notfound:
375 if (ic->ic_opmode == IEEE80211_M_IBSS &&
376 (ic->ic_flags & IEEE80211_F_IBSSON) &&
377 ic->ic_des_esslen != 0) {
378 ieee80211_create_ibss(ic, ic->ic_ibss_chan);
379 return;
380 }
381 /*
382 * Reset the list of channels to scan and start again.
383 */
384 ieee80211_reset_scan(ifp);
385 ieee80211_next_scan(ifp);
386 return;
387 }
388 selbs = NULL;
389 if (ifp->if_flags & IFF_DEBUG)
390 if_printf(ifp, "\tmacaddr bssid chan rssi rate flag wep essid\n");
391 for (; ni != NULL; ni = nextbs) {
392 ieee80211_ref_node(ni);
393 nextbs = TAILQ_NEXT(ni, ni_list);
394 if (ni->ni_fails) {
395 /*
396 * The configuration of the access points may change
397 * during my scan. So delete the entry for the AP
398 * and retry to associate if there is another beacon.
399 */
400 if (ni->ni_fails++ > 2)
401 ieee80211_free_node(ic, ni);
402 continue;
403 }
404 if (ieee80211_match_bss(ic, ni) == 0) {
405 if (selbs == NULL)
406 selbs = ni;
407 else if (ni->ni_rssi > selbs->ni_rssi) {
408 ieee80211_unref_node(&selbs);
409 selbs = ni;
410 } else
411 ieee80211_unref_node(&ni);
412 } else {
413 ieee80211_unref_node(&ni);
414 }
415 }
416 if (selbs == NULL)
417 goto notfound;
418 (*ic->ic_node_copy)(ic, ic->ic_bss, selbs);
419 if (ic->ic_opmode == IEEE80211_M_IBSS) {
420 ieee80211_fix_rate(ic, ic->ic_bss, IEEE80211_F_DOFRATE |
421 IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
422 if (ic->ic_bss->ni_rates.rs_nrates == 0) {
423 selbs->ni_fails++;
424 ieee80211_unref_node(&selbs);
425 goto notfound;
426 }
427 ieee80211_unref_node(&selbs);
428 /*
429 * Discard scan set; the nodes have a refcnt of zero
430 * and have not asked the driver to setup private
431 * node state. Let them be repopulated on demand either
432 * through transmission (ieee80211_find_txnode) or receipt
433 * of a probe response (to be added).
434 */
435 ieee80211_free_allnodes(ic);
436 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
437 } else {
438 ieee80211_unref_node(&selbs);
439 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
440 }
441 }
442
443 int
444 ieee80211_get_rate(struct ieee80211com *ic)
445 {
446 u_int8_t (*rates)[IEEE80211_RATE_MAXSIZE];
447 int rate;
448
449 rates = &ic->ic_bss->ni_rates.rs_rates;
450
451 if (ic->ic_fixed_rate != -1)
452 rate = (*rates)[ic->ic_fixed_rate];
453 else if (ic->ic_state == IEEE80211_S_RUN)
454 rate = (*rates)[ic->ic_bss->ni_txrate];
455 else
456 rate = 0;
457
458 return rate & IEEE80211_RATE_VAL;
459 }
460
461 static struct ieee80211_node *
462 ieee80211_node_alloc(struct ieee80211com *ic)
463 {
464 struct ieee80211_node *ni;
465 MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
466 M_80211_NODE, M_NOWAIT | M_ZERO);
467 return ni;
468 }
469
470 static void
471 ieee80211_node_free(struct ieee80211com *ic, struct ieee80211_node *ni)
472 {
473 if (ni->ni_challenge != NULL) {
474 FREE(ni->ni_challenge, M_DEVBUF);
475 ni->ni_challenge = NULL;
476 }
477 FREE(ni, M_80211_NODE);
478 }
479
480 static void
481 ieee80211_node_copy(struct ieee80211com *ic,
482 struct ieee80211_node *dst, const struct ieee80211_node *src)
483 {
484 *dst = *src;
485 dst->ni_challenge = NULL;
486 }
487
488 static u_int8_t
489 ieee80211_node_getrssi(struct ieee80211com *ic, struct ieee80211_node *ni)
490 {
491 return ni->ni_rssi;
492 }
493
494 static void
495 ieee80211_setup_node(struct ieee80211com *ic,
496 struct ieee80211_node *ni, u_int8_t *macaddr)
497 {
498 int hash;
499 ieee80211_node_critsec_decl(s);
500
501 IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
502 hash = IEEE80211_NODE_HASH(macaddr);
503 ni->ni_refcnt = 1; /* mark referenced */
504 ieee80211_node_critsec_begin(ic, s);
505 TAILQ_INSERT_TAIL(&ic->ic_node, ni, ni_list);
506 LIST_INSERT_HEAD(&ic->ic_hash[hash], ni, ni_hash);
507 /*
508 * Note we don't enable the inactive timer when acting
509 * as a station. Nodes created in this mode represent
510 * AP's identified while scanning. If we time them out
511 * then several things happen: we can't return the data
512 * to users to show the list of AP's we encountered, and
513 * more importantly, we'll incorrectly deauthenticate
514 * ourself because the inactivity timer will kick us off.
515 */
516 if (ic->ic_opmode != IEEE80211_M_STA)
517 ic->ic_inact_timer = IEEE80211_INACT_WAIT;
518 ieee80211_node_critsec_end(ic, s);
519 }
520
521 struct ieee80211_node *
522 ieee80211_alloc_node(struct ieee80211com *ic, u_int8_t *macaddr)
523 {
524 struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic);
525 if (ni != NULL)
526 ieee80211_setup_node(ic, ni, macaddr);
527 else
528 ic->ic_stats.is_rx_nodealloc++;
529 return ni;
530 }
531
532 struct ieee80211_node *
533 ieee80211_dup_bss(struct ieee80211com *ic, u_int8_t *macaddr)
534 {
535 struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic);
536 if (ni != NULL) {
537 ieee80211_setup_node(ic, ni, macaddr);
538 /*
539 * Inherit from ic_bss.
540 */
541 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
542 ni->ni_chan = ic->ic_bss->ni_chan;
543 } else
544 ic->ic_stats.is_rx_nodealloc++;
545 return ni;
546 }
547
548 static struct ieee80211_node *
549 _ieee80211_find_node(struct ieee80211com *ic, u_int8_t *macaddr)
550 {
551 struct ieee80211_node *ni;
552 int hash;
553
554 #ifdef __FreeBSD__
555 IEEE80211_NODE_LOCK_ASSERT(ic);
556 #endif /* __FreeBSD__ */
557
558 hash = IEEE80211_NODE_HASH(macaddr);
559 LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
560 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
561 ieee80211_node_incref(ni); /* mark referenced */
562 return ni;
563 }
564 }
565 return NULL;
566 }
567
568 struct ieee80211_node *
569 ieee80211_find_node(struct ieee80211com *ic, u_int8_t *macaddr)
570 {
571 struct ieee80211_node *ni;
572 ieee80211_node_critsec_decl(s);
573
574 ieee80211_node_critsec_begin(ic, s);
575 ni = _ieee80211_find_node(ic, macaddr);
576 ieee80211_node_critsec_end(ic, s);
577 return ni;
578 }
579
580 /*
581 * Return a reference to the appropriate node for sending
582 * a data frame. This handles node discovery in adhoc networks.
583 */
584 struct ieee80211_node *
585 ieee80211_find_txnode(struct ieee80211com *ic, u_int8_t *macaddr)
586 {
587 struct ieee80211_node *ni;
588 ieee80211_node_critsec_decl(s);
589
590 /*
591 * The destination address should be in the node table
592 * unless we are operating in station mode or this is a
593 * multicast/broadcast frame.
594 */
595 if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
596 return ic->ic_bss;
597
598 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */
599 ieee80211_node_critsec_begin(ic, s);
600 ni = _ieee80211_find_node(ic, macaddr);
601 ieee80211_node_critsec_end(ic, s);
602 if (ni == NULL &&
603 (ic->ic_opmode == IEEE80211_M_IBSS ||
604 ic->ic_opmode == IEEE80211_M_AHDEMO)) {
605 /*
606 * Fake up a node; this handles node discovery in
607 * adhoc mode. Note that for the driver's benefit
608 * we we treat this like an association so the driver
609 * has an opportunity to setup it's private state.
610 *
611 * XXX need better way to handle this; issue probe
612 * request so we can deduce rate set, etc.
613 */
614 ni = ieee80211_dup_bss(ic, macaddr);
615 if (ni != NULL) {
616 /* XXX no rate negotiation; just dup */
617 ni->ni_rates = ic->ic_bss->ni_rates;
618 if (ic->ic_newassoc)
619 (*ic->ic_newassoc)(ic, ni, 1);
620 }
621 }
622 return ni;
623 }
624
625 /*
626 * For some types of packet and for some operating modes, it is
627 * desirable to process a Rx packet using its sender's node-record
628 * instead of the BSS record, when that is possible.
629 *
630 * - AP mode: it is desirable to keep a node-record for every
631 * authenticated/associated station *in the BSS*. For future use,
632 * we also track neighboring APs, since they might belong to the
633 * same ESSID.
634 *
635 * - IBSS mode: it is desirable to keep a node-record for every
636 * station *in the BSS*.
637 *
638 * - monitor mode: it is desirable to keep a node-record for every
639 * sender, regardless of BSS.
640 *
641 * - STA mode: the only available node-record is the BSS record,
642 * ic->ic_bss.
643 *
644 * Of all the 802.11 Control packets, only the node-records for
645 * RTS packets node-record can be looked up.
646 *
647 * Return non-zero if the packet's node-record is kept, zero
648 * otherwise.
649 */
650 static __inline int
651 ieee80211_needs_rxnode(struct ieee80211com *ic, struct ieee80211_frame *wh,
652 u_int8_t **bssid)
653 {
654 struct ieee80211_node *bss = ic->ic_bss;
655 int needsnode, rc = 0;
656
657 if (ic->ic_opmode == IEEE80211_M_STA)
658 return 0;
659
660 needsnode = (ic->ic_opmode == IEEE80211_M_MONITOR);
661
662 *bssid = NULL;
663
664 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
665 case IEEE80211_FC0_TYPE_CTL:
666 return (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
667 IEEE80211_FC0_SUBTYPE_RTS;
668
669 case IEEE80211_FC0_TYPE_MGT:
670 *bssid = wh->i_addr3;
671 rc = IEEE80211_ADDR_EQ(*bssid, bss->ni_bssid);
672 break;
673 case IEEE80211_FC0_TYPE_DATA:
674 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
675 case IEEE80211_FC1_DIR_NODS:
676 *bssid = wh->i_addr3;
677 if (ic->ic_opmode == IEEE80211_M_IBSS ||
678 ic->ic_opmode == IEEE80211_M_AHDEMO)
679 rc = IEEE80211_ADDR_EQ(*bssid, bss->ni_bssid);
680 break;
681 case IEEE80211_FC1_DIR_TODS:
682 *bssid = wh->i_addr1;
683 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
684 rc = IEEE80211_ADDR_EQ(*bssid, bss->ni_bssid);
685 break;
686 case IEEE80211_FC1_DIR_FROMDS:
687 case IEEE80211_FC1_DIR_DSTODS:
688 *bssid = wh->i_addr2;
689 rc = (ic->ic_opmode == IEEE80211_M_HOSTAP);
690 break;
691 }
692 break;
693 }
694 return needsnode || rc;
695 }
696
697 struct ieee80211_node *
698 ieee80211_find_rxnode(struct ieee80211com *ic, struct ieee80211_frame *wh)
699 {
700 struct ieee80211_node *ni;
701 const static u_int8_t zero[IEEE80211_ADDR_LEN];
702 u_int8_t *bssid;
703 ieee80211_node_critsec_decl(s);
704
705 if (!ieee80211_needs_rxnode(ic, wh, &bssid))
706 return ieee80211_ref_node(ic->ic_bss);
707
708 ieee80211_node_critsec_begin(ic, s);
709 ni = _ieee80211_find_node(ic, wh->i_addr2);
710 ieee80211_node_critsec_end(ic, s);
711
712 if (ni == NULL) {
713 if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
714 if ((ni = ieee80211_dup_bss(ic, wh->i_addr2)) != NULL)
715 IEEE80211_ADDR_COPY(ni->ni_bssid,
716 (bssid != NULL) ? bssid : zero);
717
718 IEEE80211_DPRINTF(("%s: faked-up node %p for %s\n",
719 __func__, ni, ether_sprintf(wh->i_addr2)));
720 }
721 ni = ieee80211_ref_node((ni == NULL) ? ic->ic_bss : ni);
722 }
723 IASSERT(ni != NULL, ("%s: null node", __func__));
724 return ni;
725 }
726
727 /*
728 * Like find but search based on the channel too.
729 */
730 struct ieee80211_node *
731 ieee80211_lookup_node(struct ieee80211com *ic,
732 u_int8_t *macaddr, struct ieee80211_channel *chan)
733 {
734 struct ieee80211_node *ni;
735 int hash;
736 ieee80211_node_critsec_decl(s);
737
738 hash = IEEE80211_NODE_HASH(macaddr);
739 ieee80211_node_critsec_begin(ic, s);
740 LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
741 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
742 ni->ni_chan == chan) {
743 ieee80211_node_incref(ni);/* mark referenced */
744 break;
745 }
746 }
747 ieee80211_node_critsec_end(ic, s);
748 return ni;
749 }
750
751 static void
752 _ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
753 {
754 IASSERT(ni != ic->ic_bss, ("freeing bss node"));
755
756 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
757 TAILQ_REMOVE(&ic->ic_node, ni, ni_list);
758 LIST_REMOVE(ni, ni_hash);
759 if (!IF_IS_EMPTY(&ni->ni_savedq)) {
760 IF_PURGE(&ni->ni_savedq);
761 if (ic->ic_set_tim)
762 ic->ic_set_tim(ic, ni->ni_associd, 0);
763 }
764 if (TAILQ_EMPTY(&ic->ic_node))
765 ic->ic_inact_timer = 0;
766 (*ic->ic_node_free)(ic, ni);
767 }
768
769 void
770 ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
771 {
772 ieee80211_node_critsec_decl(s);
773
774 IASSERT(ni != ic->ic_bss, ("freeing ic_bss"));
775
776 if (ieee80211_node_decref(ni) == 0) {
777 ieee80211_node_critsec_begin(ic, s);
778 _ieee80211_free_node(ic, ni);
779 ieee80211_node_critsec_end(ic, s);
780 }
781 }
782
783 void
784 ieee80211_free_allnodes(struct ieee80211com *ic)
785 {
786 struct ieee80211_node *ni;
787 ieee80211_node_critsec_decl(s);
788
789 ieee80211_node_critsec_begin(ic, s);
790 while ((ni = TAILQ_FIRST(&ic->ic_node)) != NULL)
791 _ieee80211_free_node(ic, ni);
792 ieee80211_node_critsec_end(ic, s);
793 }
794
795 /*
796 * Timeout inactive nodes. Note that we cannot hold the node
797 * lock while sending a frame as this would lead to a LOR.
798 * Instead we use a generation number to mark nodes that we've
799 * scanned and drop the lock and restart a scan if we have to
800 * time out a node. Since we are single-threaded by virtue of
801 * controlling the inactivity timer we can be sure this will
802 * process each node only once.
803 */
804 void
805 ieee80211_timeout_nodes(struct ieee80211com *ic)
806 {
807 struct ieee80211_node *ni;
808 ieee80211_node_critsec_decl(s);
809 u_int gen = ic->ic_scangen++; /* NB: ok 'cuz single-threaded*/
810
811 restart:
812 ieee80211_node_critsec_begin(ic, s);
813 TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
814 if (ni->ni_scangen == gen) /* previously handled */
815 continue;
816 ni->ni_scangen = gen;
817 if (++ni->ni_inact > IEEE80211_INACT_MAX) {
818 IEEE80211_DPRINTF(("station %s timed out "
819 "due to inactivity (%u secs)\n",
820 ether_sprintf(ni->ni_macaddr),
821 ni->ni_inact));
822 /*
823 * Send a deauthenticate frame.
824 *
825 * Drop the node lock before sending the
826 * deauthentication frame in case the driver takes
827 * a lock, as this will result in a LOR between the
828 * node lock and the driver lock.
829 */
830 ieee80211_node_critsec_end(ic, s);
831 IEEE80211_SEND_MGMT(ic, ni,
832 IEEE80211_FC0_SUBTYPE_DEAUTH,
833 IEEE80211_REASON_AUTH_EXPIRE);
834 ieee80211_free_node(ic, ni);
835 ic->ic_stats.is_node_timeout++;
836 goto restart;
837 }
838 }
839 if (!TAILQ_EMPTY(&ic->ic_node))
840 ic->ic_inact_timer = IEEE80211_INACT_WAIT;
841 ieee80211_node_critsec_end(ic, s);
842 }
843
844 void
845 ieee80211_iterate_nodes(struct ieee80211com *ic, ieee80211_iter_func *f, void *arg)
846 {
847 struct ieee80211_node *ni;
848 ieee80211_node_critsec_decl(s);
849
850 ieee80211_node_critsec_begin(ic, s);
851 TAILQ_FOREACH(ni, &ic->ic_node, ni_list)
852 (*f)(arg, ni);
853 ieee80211_node_critsec_end(ic, s);
854 }
855