ieee80211_node.c revision 1.23 1 /* $NetBSD: ieee80211_node.c,v 1.23 2004/07/23 10:15:13 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.23 2004/07/23 10:15:13 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 TAILQ_INSERT_TAIL(&ic->ic_node, ni, ni_list);
526 LIST_INSERT_HEAD(&ic->ic_hash[hash], ni, ni_hash);
527 /*
528 * Note we don't enable the inactive timer when acting
529 * as a station. Nodes created in this mode represent
530 * AP's identified while scanning. If we time them out
531 * then several things happen: we can't return the data
532 * to users to show the list of AP's we encountered, and
533 * more importantly, we'll incorrectly deauthenticate
534 * ourself because the inactivity timer will kick us off.
535 */
536 if (ic->ic_opmode != IEEE80211_M_STA)
537 ic->ic_inact_timer = IEEE80211_INACT_WAIT;
538 IEEE80211_NODE_UNLOCK_BH(ic);
539 }
540
541 struct ieee80211_node *
542 ieee80211_alloc_node(struct ieee80211com *ic, u_int8_t *macaddr)
543 {
544 struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic);
545 if (ni != NULL)
546 ieee80211_setup_node(ic, ni, macaddr);
547 else
548 ic->ic_stats.is_rx_nodealloc++;
549 return ni;
550 }
551
552 struct ieee80211_node *
553 ieee80211_dup_bss(struct ieee80211com *ic, u_int8_t *macaddr)
554 {
555 struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic);
556 if (ni != NULL) {
557 ieee80211_setup_node(ic, ni, macaddr);
558 /*
559 * Inherit from ic_bss.
560 */
561 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
562 ni->ni_chan = ic->ic_bss->ni_chan;
563 } else
564 ic->ic_stats.is_rx_nodealloc++;
565 return ni;
566 }
567
568 static struct ieee80211_node *
569 _ieee80211_find_node(struct ieee80211com *ic, u_int8_t *macaddr)
570 {
571 struct ieee80211_node *ni;
572 int hash;
573
574 IEEE80211_NODE_LOCK_ASSERT(ic);
575
576 hash = IEEE80211_NODE_HASH(macaddr);
577 LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
578 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
579 ieee80211_node_incref(ni); /* mark referenced */
580 return ni;
581 }
582 }
583 return NULL;
584 }
585
586 struct ieee80211_node *
587 ieee80211_find_node(struct ieee80211com *ic, u_int8_t *macaddr)
588 {
589 struct ieee80211_node *ni;
590
591 IEEE80211_NODE_LOCK(ic);
592 ni = _ieee80211_find_node(ic, macaddr);
593 IEEE80211_NODE_UNLOCK(ic);
594 return ni;
595 }
596
597 /*
598 * Return a reference to the appropriate node for sending
599 * a data frame. This handles node discovery in adhoc networks.
600 */
601 struct ieee80211_node *
602 ieee80211_find_txnode(struct ieee80211com *ic, u_int8_t *macaddr)
603 {
604 struct ieee80211_node *ni;
605
606 /*
607 * The destination address should be in the node table
608 * unless we are operating in station mode or this is a
609 * multicast/broadcast frame.
610 */
611 if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
612 return ic->ic_bss;
613
614 /* XXX can't hold lock across dup_bss 'cuz of recursive locking */
615 IEEE80211_NODE_LOCK(ic);
616 ni = _ieee80211_find_node(ic, macaddr);
617 IEEE80211_NODE_UNLOCK(ic);
618 if (ni == NULL &&
619 (ic->ic_opmode == IEEE80211_M_IBSS ||
620 ic->ic_opmode == IEEE80211_M_AHDEMO)) {
621 /*
622 * Fake up a node; this handles node discovery in
623 * adhoc mode. Note that for the driver's benefit
624 * we we treat this like an association so the driver
625 * has an opportunity to setup it's private state.
626 *
627 * XXX need better way to handle this; issue probe
628 * request so we can deduce rate set, etc.
629 */
630 ni = ieee80211_dup_bss(ic, macaddr);
631 if (ni != NULL) {
632 /* XXX no rate negotiation; just dup */
633 ni->ni_rates = ic->ic_bss->ni_rates;
634 if (ic->ic_newassoc)
635 (*ic->ic_newassoc)(ic, ni, 1);
636 }
637 }
638 return ni;
639 }
640
641 /*
642 * For some types of packet and for some operating modes, it is
643 * desirable to process a Rx packet using its sender's node-record
644 * instead of the BSS record, when that is possible.
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 ESSID.
649 *
650 * - IBSS mode: keep a node-record for every station *in the BSS*.
651 *
652 * - monitor mode: keep a node-record for every sender, regardless
653 * of BSS.
654 *
655 * - STA mode: the only available node-record is the BSS record,
656 * ic->ic_bss.
657 *
658 * Of all the 802.11 Control packets, only the node-records for
659 * RTS packets node-record can be looked up.
660 *
661 * Return non-zero if the packet's node-record is kept, zero
662 * otherwise.
663 */
664 static __inline int
665 ieee80211_needs_rxnode(struct ieee80211com *ic, struct ieee80211_frame *wh,
666 u_int8_t **bssid)
667 {
668 struct ieee80211_node *bss = ic->ic_bss;
669 int monitor, rc = 0;
670
671 monitor = (ic->ic_opmode == IEEE80211_M_MONITOR);
672
673 *bssid = NULL;
674
675 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
676 case IEEE80211_FC0_TYPE_CTL:
677 if (!monitor)
678 break;
679 return (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
680 IEEE80211_FC0_SUBTYPE_RTS;
681 case IEEE80211_FC0_TYPE_MGT:
682 *bssid = wh->i_addr3;
683 switch (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) {
684 case IEEE80211_FC0_SUBTYPE_BEACON:
685 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
686 rc = 1;
687 break;
688 default:
689 break;
690 }
691 break;
692 case IEEE80211_FC0_TYPE_DATA:
693 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
694 case IEEE80211_FC1_DIR_NODS:
695 *bssid = wh->i_addr3;
696 if (ic->ic_opmode == IEEE80211_M_IBSS ||
697 ic->ic_opmode == IEEE80211_M_AHDEMO)
698 rc = IEEE80211_ADDR_EQ(*bssid, bss->ni_bssid);
699 break;
700 case IEEE80211_FC1_DIR_TODS:
701 *bssid = wh->i_addr1;
702 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
703 rc = IEEE80211_ADDR_EQ(*bssid, bss->ni_bssid);
704 break;
705 case IEEE80211_FC1_DIR_FROMDS:
706 case IEEE80211_FC1_DIR_DSTODS:
707 *bssid = wh->i_addr2;
708 rc = (ic->ic_opmode == IEEE80211_M_HOSTAP);
709 break;
710 }
711 break;
712 }
713 return monitor || rc;
714 }
715
716 struct ieee80211_node *
717 ieee80211_find_rxnode(struct ieee80211com *ic, struct ieee80211_frame *wh)
718 {
719 struct ieee80211_node *ni;
720 const static u_int8_t zero[IEEE80211_ADDR_LEN];
721 u_int8_t *bssid;
722
723 if (!ieee80211_needs_rxnode(ic, wh, &bssid))
724 return ieee80211_ref_node(ic->ic_bss);
725
726 IEEE80211_NODE_LOCK(ic);
727 ni = _ieee80211_find_node(ic, wh->i_addr2);
728 IEEE80211_NODE_UNLOCK(ic);
729
730 if (ni != NULL)
731 return ni;
732
733 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
734 return ieee80211_ref_node(ic->ic_bss);
735
736 /* XXX see remarks in ieee80211_find_txnode */
737 /* XXX no rate negotiation; just dup */
738 if ((ni = ieee80211_dup_bss(ic, wh->i_addr2)) == NULL)
739 return ieee80211_ref_node(ic->ic_bss);
740
741 IEEE80211_ADDR_COPY(ni->ni_bssid, (bssid != NULL) ? bssid : zero);
742
743 ni->ni_rates = ic->ic_bss->ni_rates;
744 if (ic->ic_newassoc)
745 (*ic->ic_newassoc)(ic, ni, 1);
746
747 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
748 ("%s: faked-up node %p for %s\n", __func__, ni,
749 ether_sprintf(wh->i_addr2)));
750
751 return ieee80211_ref_node(ni);
752 }
753
754 /*
755 * Like find but search based on the channel too.
756 */
757 struct ieee80211_node *
758 ieee80211_find_node_with_channel(struct ieee80211com *ic, u_int8_t *macaddr,
759 struct ieee80211_channel *chan)
760 {
761 struct ieee80211_node *ni;
762 int hash;
763
764 hash = IEEE80211_NODE_HASH(macaddr);
765 IEEE80211_NODE_LOCK(ic);
766 LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
767 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
768 ni->ni_chan == chan) {
769 ieee80211_node_incref(ni);/* mark referenced */
770 break;
771 }
772 }
773 IEEE80211_NODE_UNLOCK(ic);
774 return ni;
775 }
776
777 static void
778 _ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
779 {
780 IASSERT(ni != ic->ic_bss, ("freeing bss node"));
781
782 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
783 ("%s %s\n", __func__, ether_sprintf(ni->ni_macaddr)));
784 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
785 TAILQ_REMOVE(&ic->ic_node, ni, ni_list);
786 LIST_REMOVE(ni, ni_hash);
787 if (!IF_IS_EMPTY(&ni->ni_savedq)) {
788 IF_PURGE(&ni->ni_savedq);
789 if (ic->ic_set_tim)
790 (*ic->ic_set_tim)(ic, ni->ni_associd, 0);
791 }
792 if (TAILQ_EMPTY(&ic->ic_node))
793 ic->ic_inact_timer = 0;
794 (*ic->ic_node_free)(ic, ni);
795 }
796
797 void
798 ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
799 {
800 IASSERT(ni != ic->ic_bss, ("freeing ic_bss"));
801
802 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
803 ("%s %s refcnt %d\n", __func__,
804 ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)));
805 if (ieee80211_node_decref(ni) == 0) {
806 IEEE80211_NODE_LOCK_BH(ic);
807 _ieee80211_free_node(ic, ni);
808 IEEE80211_NODE_UNLOCK_BH(ic);
809 }
810 }
811
812 void
813 ieee80211_free_allnodes(struct ieee80211com *ic)
814 {
815 struct ieee80211_node *ni;
816
817 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, ("free all nodes\n"));
818 IEEE80211_NODE_LOCK_BH(ic);
819 while ((ni = TAILQ_FIRST(&ic->ic_node)) != NULL)
820 _ieee80211_free_node(ic, ni);
821 IEEE80211_NODE_UNLOCK_BH(ic);
822
823 if (ic->ic_bss != NULL)
824 node_cleanup(ic, ic->ic_bss); /* for station mode */
825 }
826
827 /*
828 * Timeout inactive nodes. Note that we cannot hold the node
829 * lock while sending a frame as this would lead to a LOR.
830 * Instead we use a generation number to mark nodes that we've
831 * scanned and drop the lock and restart a scan if we have to
832 * time out a node. Since we are single-threaded by virtue of
833 * controlling the inactivity timer we can be sure this will
834 * process each node only once.
835 */
836 void
837 ieee80211_timeout_nodes(struct ieee80211com *ic)
838 {
839 struct ieee80211_node *ni;
840 u_int gen = ic->ic_scangen++; /* NB: ok 'cuz single-threaded*/
841
842 restart:
843 IEEE80211_NODE_LOCK(ic);
844 TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
845 if (ni->ni_scangen == gen) /* previously handled */
846 continue;
847 ni->ni_scangen = gen;
848 if (++ni->ni_inact > ieee80211_inact_max) {
849 IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
850 ("station %s timed out due to inactivity (%u secs)\n",
851 ether_sprintf(ni->ni_macaddr),
852 ni->ni_inact));
853 /*
854 * Send a deauthenticate frame.
855 *
856 * Drop the node lock before sending the
857 * deauthentication frame in case the driver takes
858 * a lock, as this will result in a LOR between the
859 * node lock and the driver lock.
860 */
861 IEEE80211_NODE_UNLOCK(ic);
862 IEEE80211_SEND_MGMT(ic, ni,
863 IEEE80211_FC0_SUBTYPE_DEAUTH,
864 IEEE80211_REASON_AUTH_EXPIRE);
865 ieee80211_node_leave(ic, ni);
866 ic->ic_stats.is_node_timeout++;
867 goto restart;
868 }
869 }
870 if (!TAILQ_EMPTY(&ic->ic_node))
871 ic->ic_inact_timer = IEEE80211_INACT_WAIT;
872 IEEE80211_NODE_UNLOCK(ic);
873 }
874
875 void
876 ieee80211_iterate_nodes(struct ieee80211com *ic, ieee80211_iter_func *f, void *arg)
877 {
878 struct ieee80211_node *ni;
879
880 IEEE80211_NODE_LOCK(ic);
881 TAILQ_FOREACH(ni, &ic->ic_node, ni_list)
882 (*f)(arg, ni);
883 IEEE80211_NODE_UNLOCK(ic);
884 }
885
886 void
887 ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int resp)
888 {
889 int newassoc;
890
891 if (ni->ni_associd == 0) {
892 u_int16_t aid;
893
894 /*
895 * It would be clever to search the bitmap
896 * more efficiently, but this will do for now.
897 */
898 for (aid = 1; aid < ic->ic_max_aid; aid++) {
899 if (!IEEE80211_AID_ISSET(aid,
900 ic->ic_aid_bitmap))
901 break;
902 }
903 if (aid >= ic->ic_max_aid) {
904 IEEE80211_SEND_MGMT(ic, ni, resp,
905 IEEE80211_REASON_ASSOC_TOOMANY);
906 ieee80211_node_leave(ic, ni);
907 return;
908 }
909 ni->ni_associd = aid | 0xc000;
910 IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
911 newassoc = 1;
912 /* XXX for 11g must turn off short slot time if long
913 slot time sta associates */
914 } else
915 newassoc = 0;
916
917 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC | IEEE80211_MSG_DEBUG,
918 ("station %s %s associated at aid %d\n",
919 ether_sprintf(ni->ni_macaddr),
920 (newassoc ? "newly" : "already"),
921 ni->ni_associd & ~0xc000));
922
923 /* give driver a chance to setup state like ni_txrate */
924 if (ic->ic_newassoc)
925 (*ic->ic_newassoc)(ic, ni, newassoc);
926 IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
927 }
928
929 /*
930 * Handle bookkeeping for station deauthentication/disassociation
931 * when operating as an ap.
932 */
933 void
934 ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
935 {
936
937 IASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP,
938 ("not in ap mode, mode %u", ic->ic_opmode));
939 /*
940 * If node wasn't previously associated all
941 * we need to do is reclaim the reference.
942 */
943 if (ni->ni_associd == 0)
944 goto done;
945 IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
946 ni->ni_associd = 0;
947
948 done:
949 ieee80211_free_node(ic, ni);
950 }
951