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