Merge from vendor branch OPENSSH:
[dragonfly.git] / sys / netproto / 802_11 / wlan / ieee80211_node.c
1 /*
2  * Copyright (c) 2001 Atsushi Onoe
3  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * Alternatively, this software may be distributed under the terms of the
18  * GNU General Public License ("GPL") version 2 as published by the Free
19  * Software Foundation.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $FreeBSD: src/sys/net80211/ieee80211_node.c,v 1.22 2004/04/05 04:15:55 sam Exp $
33  * $DragonFly: src/sys/netproto/802_11/wlan/ieee80211_node.c,v 1.1 2004/07/26 16:30:17 joerg Exp $
34  */
35
36 #include "opt_inet.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h> 
40 #include <sys/mbuf.h>   
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/endian.h>
46 #include <sys/errno.h>
47 #include <sys/bus.h>
48 #include <sys/proc.h>
49 #include <sys/sysctl.h>
50 #include <sys/thread.h>
51 #include <sys/thread2.h>
52
53 #include <machine/atomic.h>
54  
55 #include <net/if.h>
56 #include <net/if_dl.h>
57 #include <net/if_media.h>
58 #include <net/if_arp.h>
59 #include <net/ethernet.h>
60 #include <net/if_llc.h>
61
62 #include <netproto/802_11/ieee80211_var.h>
63
64 #include <net/bpf.h>
65
66 #ifdef INET
67 #include <netinet/in.h> 
68 #include <netinet/if_ether.h>
69 #endif
70
71 static struct ieee80211_node *ieee80211_node_alloc(struct ieee80211com *);
72 static void ieee80211_node_free(struct ieee80211com *, struct ieee80211_node *);
73 static void ieee80211_node_copy(struct ieee80211com *,
74                 struct ieee80211_node *, const struct ieee80211_node *);
75 static uint8_t ieee80211_node_getrssi(struct ieee80211com *,
76                 struct ieee80211_node *);
77
78 static void ieee80211_setup_node(struct ieee80211com *ic,
79                 struct ieee80211_node *ni, uint8_t *macaddr);
80 static void _ieee80211_free_node(struct ieee80211com *,
81                 struct ieee80211_node *);
82
83 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
84
85 void
86 ieee80211_node_attach(struct ifnet *ifp)
87 {
88         struct ieee80211com *ic = (void *)ifp;
89
90         lwkt_token_init(&ic->ic_nodetoken);
91         TAILQ_INIT(&ic->ic_node);
92         ic->ic_node_alloc = ieee80211_node_alloc;
93         ic->ic_node_free = ieee80211_node_free;
94         ic->ic_node_copy = ieee80211_node_copy;
95         ic->ic_node_getrssi = ieee80211_node_getrssi;
96         ic->ic_scangen = 1;
97 }
98
99 void
100 ieee80211_node_lateattach(struct ifnet *ifp)
101 {
102         struct ieee80211com *ic = (void *)ifp;
103         struct ieee80211_node *ni;
104
105         ni = (*ic->ic_node_alloc)(ic);
106         KASSERT(ni != NULL, ("unable to setup inital BSS node"));
107         ni->ni_chan = IEEE80211_CHAN_ANYC;
108         ic->ic_bss = ni;
109         ic->ic_txpower = IEEE80211_TXPOWER_MAX;
110 }
111
112 void
113 ieee80211_node_detach(struct ifnet *ifp)
114 {
115         struct ieee80211com *ic = (void *)ifp;
116
117         if (ic->ic_bss != NULL)
118                 (*ic->ic_node_free)(ic, ic->ic_bss);
119         ieee80211_free_allnodes(ic);
120         lwkt_token_uninit(&ic->ic_nodetoken);
121 }
122
123 /*
124  * AP scanning support.
125  */
126
127 /*
128  * Initialize the active channel set based on the set
129  * of available channels and the current PHY mode.
130  */
131 static void
132 ieee80211_reset_scan(struct ifnet *ifp)
133 {
134         struct ieee80211com *ic = (void *)ifp;
135
136         memcpy(ic->ic_chan_scan, ic->ic_chan_active,
137                 sizeof(ic->ic_chan_active));
138         /* NB: hack, setup so next_scan starts with the first channel */
139         if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC)
140                 ic->ic_bss->ni_chan = &ic->ic_channels[IEEE80211_CHAN_MAX];
141 }
142
143 /*
144  * Begin an active scan.
145  */
146 void
147 ieee80211_begin_scan(struct ifnet *ifp)
148 {
149         struct ieee80211com *ic = (void *)ifp;
150
151         /*
152          * In all but hostap mode scanning starts off in
153          * an active mode before switching to passive.
154          */
155         if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
156                 ic->ic_flags |= IEEE80211_F_ASCAN;
157                 ic->ic_stats.is_scan_active++;
158         } else
159                 ic->ic_stats.is_scan_passive++;
160         if (ifp->if_flags & IFF_DEBUG)
161                 if_printf(ifp, "begin %s scan\n",
162                         (ic->ic_flags & IEEE80211_F_ASCAN) ?
163                                 "active" : "passive");
164         /*
165          * Clear scan state and flush any previously seen
166          * AP's.  Note that the latter assumes we don't act
167          * as both an AP and a station, otherwise we'll
168          * potentially flush state of stations associated
169          * with us.
170          */
171         ieee80211_reset_scan(ifp);
172         ieee80211_free_allnodes(ic);
173
174         /* Scan the next channel. */
175         ieee80211_next_scan(ifp);
176 }
177
178 /*
179  * Switch to the next channel marked for scanning.
180  */
181 void
182 ieee80211_next_scan(struct ifnet *ifp)
183 {
184         struct ieee80211com *ic = (void *)ifp;
185         struct ieee80211_channel *chan;
186
187         chan = ic->ic_bss->ni_chan;
188         for (;;) {
189                 if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
190                         chan = &ic->ic_channels[0];
191                 if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
192                         /*
193                          * Honor channels marked passive-only
194                          * during an active scan.
195                          */
196                         if ((ic->ic_flags & IEEE80211_F_ASCAN) == 0 ||
197                             (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0)
198                                 break;
199                 }
200                 if (chan == ic->ic_bss->ni_chan) {
201                         ieee80211_end_scan(ifp);
202                         return;
203                 }
204         }
205         clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
206         IEEE80211_DPRINTF(("ieee80211_next_scan: chan %d->%d\n",
207             ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
208             ieee80211_chan2ieee(ic, chan)));
209         ic->ic_bss->ni_chan = chan;
210         ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
211 }
212
213 void
214 ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
215 {
216         struct ieee80211_node *ni;
217         struct ifnet *ifp = &ic->ic_if;
218
219         ni = ic->ic_bss;
220         if (ifp->if_flags & IFF_DEBUG)
221                 if_printf(ifp, "creating ibss\n");
222         ic->ic_flags |= IEEE80211_F_SIBSS;
223         ni->ni_chan = chan;
224         ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
225         IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr);
226         IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
227         if (ic->ic_opmode == IEEE80211_M_IBSS)
228                 ni->ni_bssid[0] |= 0x02;        /* local bit for IBSS */
229         ni->ni_esslen = ic->ic_des_esslen;
230         memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
231         ni->ni_rssi = 0;
232         ni->ni_rstamp = 0;
233         memset(ni->ni_tstamp, 0, sizeof(ni->ni_tstamp));
234         ni->ni_intval = ic->ic_lintval;
235         ni->ni_capinfo = IEEE80211_CAPINFO_IBSS;
236         if (ic->ic_flags & IEEE80211_F_WEPON)
237                 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
238         if (ic->ic_phytype == IEEE80211_T_FH) {
239                 ni->ni_fhdwell = 200;   /* XXX */
240                 ni->ni_fhindex = 1;
241         }
242         ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
243 }
244
245 static int
246 ieee80211_match_bss(struct ifnet *ifp, struct ieee80211_node *ni)
247 {
248         struct ieee80211com *ic = (void *)ifp;
249         uint8_t rate;
250         int fail;
251
252         fail = 0;
253         if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
254                 fail |= 0x01;
255         if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
256             ni->ni_chan != ic->ic_des_chan)
257                 fail |= 0x01;
258         if (ic->ic_opmode == IEEE80211_M_IBSS) {
259                 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
260                         fail |= 0x02;
261         } else {
262                 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
263                         fail |= 0x02;
264         }
265         if (ic->ic_flags & IEEE80211_F_WEPON) {
266                 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
267                         fail |= 0x04;
268         } else {
269                 /* XXX does this mean privacy is supported or required? */
270                 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
271                         fail |= 0x04;
272         }
273         rate = ieee80211_fix_rate(ic, ni, IEEE80211_F_DONEGO);
274         if (rate & IEEE80211_RATE_BASIC)
275                 fail |= 0x08;
276         if (ic->ic_des_esslen != 0 &&
277             (ni->ni_esslen != ic->ic_des_esslen ||
278              memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
279                 fail |= 0x10;
280         if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
281             !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
282                 fail |= 0x20;
283 #ifdef IEEE80211_DEBUG
284         if (ifp->if_flags & IFF_DEBUG) {
285                 printf(" %c %6D", fail ? '-' : '+', ni->ni_macaddr, ":");
286                 printf(" %6D%c", ni->ni_bssid, ":", fail & 0x20 ? '!' : ' ');
287                 printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
288                         fail & 0x01 ? '!' : ' ');
289                 printf(" %+4d", ni->ni_rssi);
290                 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
291                     fail & 0x08 ? '!' : ' ');
292                 printf(" %4s%c",
293                     (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
294                     (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
295                     "????",
296                     fail & 0x02 ? '!' : ' ');
297                 printf(" %3s%c ",
298                     (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
299                     "wep" : "no",
300                     fail & 0x04 ? '!' : ' ');
301                 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
302                 printf("%s\n", fail & 0x10 ? "!" : "");
303         }
304 #endif
305         return fail;
306 }
307
308 /*
309  * Complete a scan of potential channels.
310  */
311 void
312 ieee80211_end_scan(struct ifnet *ifp)
313 {
314         struct ieee80211com *ic = (void *)ifp;
315         struct ieee80211_node *ni, *nextbs, *selbs;
316         int i, fail;
317
318         ic->ic_flags &= ~IEEE80211_F_ASCAN;
319         ni = TAILQ_FIRST(&ic->ic_node);
320
321         if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
322                 /* XXX off stack? */
323                 u_char occupied[roundup(IEEE80211_CHAN_MAX, NBBY)];
324                 /*
325                  * The passive scan to look for existing AP's completed,
326                  * select a channel to camp on.  Identify the channels
327                  * that already have one or more AP's and try to locate
328                  * an unnoccupied one.  If that fails, pick a random
329                  * channel from the active set.
330                  */
331                 for (; ni != NULL; ni = nextbs) {
332                         ieee80211_ref_node(ni);
333                         nextbs = TAILQ_NEXT(ni, ni_list);
334                         setbit(occupied, ieee80211_chan2ieee(ic, ni->ni_chan));
335                         ieee80211_free_node(ic, ni);
336                 }
337                 for (i = 0; i < IEEE80211_CHAN_MAX; i++)
338                         if (isset(ic->ic_chan_active, i) && isclr(occupied, i))
339                                 break;
340                 if (i == IEEE80211_CHAN_MAX) {
341                         fail = arc4random() & 3;        /* random 0-3 */
342                         for (i = 0; i < IEEE80211_CHAN_MAX; i++)
343                                 if (isset(ic->ic_chan_active, i) && fail-- == 0)
344                                         break;
345                 }
346                 ieee80211_create_ibss(ic, &ic->ic_channels[i]);
347                 return;
348         }
349         if (ni == NULL) {
350                 IEEE80211_DPRINTF(("%s: no scan candidate\n", __func__));
351   notfound:
352                 if (ic->ic_opmode == IEEE80211_M_IBSS &&
353                     (ic->ic_flags & IEEE80211_F_IBSSON) &&
354                     ic->ic_des_esslen != 0) {
355                         ieee80211_create_ibss(ic, ic->ic_ibss_chan);
356                         return;
357                 }
358                 /*
359                  * Reset the list of channels to scan and start again.
360                  */
361                 ieee80211_reset_scan(ifp);
362                 ieee80211_next_scan(ifp);
363                 return;
364         }
365         selbs = NULL;
366         if (ifp->if_flags & IFF_DEBUG)
367                 if_printf(ifp, "\tmacaddr          bssid         chan  rssi rate flag  wep  essid\n");
368         for (; ni != NULL; ni = nextbs) {
369                 ieee80211_ref_node(ni);
370                 nextbs = TAILQ_NEXT(ni, ni_list);
371                 if (ni->ni_fails) {
372                         /*
373                          * The configuration of the access points may change
374                          * during my scan.  So delete the entry for the AP
375                          * and retry to associate if there is another beacon.
376                          */
377                         if (ni->ni_fails++ > 2)
378                                 ieee80211_free_node(ic, ni);
379                         continue;
380                 }
381                 if (ieee80211_match_bss(ifp, ni) == 0) {
382                         if (selbs == NULL)
383                                 selbs = ni;
384                         else if (ni->ni_rssi > selbs->ni_rssi) {
385                                 ieee80211_unref_node(&selbs);
386                                 selbs = ni;
387                         } else
388                                 ieee80211_unref_node(&ni);
389                 } else {
390                         ieee80211_unref_node(&ni);
391                 }
392         }
393         if (selbs == NULL)
394                 goto notfound;
395         (*ic->ic_node_copy)(ic, ic->ic_bss, selbs);
396         if (ic->ic_opmode == IEEE80211_M_IBSS) {
397                 ieee80211_fix_rate(ic, ic->ic_bss, IEEE80211_F_DOFRATE |
398                     IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
399                 if (ic->ic_bss->ni_rates.rs_nrates == 0) {
400                         selbs->ni_fails++;
401                         ieee80211_unref_node(&selbs);
402                         goto notfound;
403                 }
404                 ieee80211_unref_node(&selbs);
405                 /*
406                  * Discard scan set; the nodes have a refcnt of zero
407                  * and have not asked the driver to setup private
408                  * node state.  Let them be repopulated on demand either
409                  * through transmission (ieee80211_find_txnode) or receipt
410                  * of a probe response (to be added).
411                  */
412                 ieee80211_free_allnodes(ic);
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 static struct ieee80211_node *
421 ieee80211_node_alloc(struct ieee80211com *ic)
422 {
423         struct ieee80211_node *ni;
424         MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
425                 M_80211_NODE, M_NOWAIT | M_ZERO);
426         return ni;
427 }
428
429 static void
430 ieee80211_node_free(struct ieee80211com *ic, struct ieee80211_node *ni)
431 {
432         FREE(ni, M_80211_NODE);
433 }
434
435 static void
436 ieee80211_node_copy(struct ieee80211com *ic,
437         struct ieee80211_node *dst, const struct ieee80211_node *src)
438 {
439         *dst = *src;
440 }
441
442 static uint8_t
443 ieee80211_node_getrssi(struct ieee80211com *ic, struct ieee80211_node *ni)
444 {
445         return ni->ni_rssi;
446 }
447
448 static void
449 ieee80211_setup_node(struct ieee80211com *ic,
450         struct ieee80211_node *ni, uint8_t *macaddr)
451 {
452         lwkt_tokref ilock;
453         int hash;
454
455         IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
456         hash = IEEE80211_NODE_HASH(macaddr);
457         ni->ni_refcnt = 1;              /* mark referenced */
458         lwkt_gettoken(&ilock, &ic->ic_nodetoken);
459         TAILQ_INSERT_TAIL(&ic->ic_node, ni, ni_list);
460         LIST_INSERT_HEAD(&ic->ic_hash[hash], ni, ni_hash);
461         /* 
462          * Note we don't enable the inactive timer when acting
463          * as a station.  Nodes created in this mode represent
464          * AP's identified while scanning.  If we time them out
465          * then several things happen: we can't return the data
466          * to users to show the list of AP's we encountered, and
467          * more importantly, we'll incorrectly deauthenticate
468          * ourself because the inactivity timer will kick us off. 
469          */
470         if (ic->ic_opmode != IEEE80211_M_STA)
471                 ic->ic_inact_timer = IEEE80211_INACT_WAIT;
472         lwkt_reltoken(&ilock);
473 }
474
475 struct ieee80211_node *
476 ieee80211_alloc_node(struct ieee80211com *ic, uint8_t *macaddr)
477 {
478         struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic);
479         if (ni != NULL)
480                 ieee80211_setup_node(ic, ni, macaddr);
481         else
482                 ic->ic_stats.is_rx_nodealloc++;
483         return ni;
484 }
485
486 struct ieee80211_node *
487 ieee80211_dup_bss(struct ieee80211com *ic, uint8_t *macaddr)
488 {
489         struct ieee80211_node *ni = (*ic->ic_node_alloc)(ic);
490         if (ni != NULL) {
491                 ieee80211_setup_node(ic, ni, macaddr);
492                 /*
493                  * Inherit from ic_bss.
494                  */
495                 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
496                 ni->ni_chan = ic->ic_bss->ni_chan;
497         } else
498                 ic->ic_stats.is_rx_nodealloc++;
499         return ni;
500 }
501
502 static struct ieee80211_node *
503 _ieee80211_find_node(struct ieee80211com *ic, uint8_t *macaddr)
504 {
505         struct ieee80211_node *ni;
506         int hash;
507
508         hash = IEEE80211_NODE_HASH(macaddr);
509         LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
510                 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
511                         atomic_add_int(&ni->ni_refcnt, 1);/* mark referenced */
512                         return ni;
513                 }
514         }
515         return NULL;
516 }
517
518 struct ieee80211_node *
519 ieee80211_find_node(struct ieee80211com *ic, uint8_t *macaddr)
520 {
521         struct ieee80211_node *ni;
522         struct lwkt_tokref ilock;
523
524         lwkt_gettoken(&ilock, &ic->ic_nodetoken);
525         ni = _ieee80211_find_node(ic, macaddr);
526         lwkt_reltoken(&ilock);
527         return ni;
528 }
529
530 /*
531  * Return a reference to the appropriate node for sending
532  * a data frame.  This handles node discovery in adhoc networks.
533  */
534 struct ieee80211_node *
535 ieee80211_find_txnode(struct ieee80211com *ic, uint8_t *macaddr)
536 {
537         struct ieee80211_node *ni;
538         struct lwkt_tokref ilock;
539
540         /*
541          * The destination address should be in the node table
542          * unless we are operating in station mode or this is a
543          * multicast/broadcast frame.
544          */
545         if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
546                 return ic->ic_bss;
547
548         /* XXX can't hold lock across dup_bss 'cuz of recursive locking */
549         lwkt_gettoken(&ilock, &ic->ic_nodetoken);
550         ni = _ieee80211_find_node(ic, macaddr);
551         lwkt_reltoken(&ilock);
552         ni = _ieee80211_find_node(ic, macaddr);
553         if (ni == NULL &&
554             (ic->ic_opmode == IEEE80211_M_IBSS ||
555              ic->ic_opmode == IEEE80211_M_AHDEMO)) {
556                 /*
557                  * Fake up a node; this handles node discovery in
558                  * adhoc mode.  Note that for the driver's benefit
559                  * we we treat this like an association so the driver
560                  * has an opportunity to setup it's private state.
561                  *
562                  * XXX need better way to handle this; issue probe
563                  *     request so we can deduce rate set, etc.
564                  */
565                 ni = ieee80211_dup_bss(ic, macaddr);
566                 if (ni != NULL) {
567                         /* XXX no rate negotiation; just dup */
568                         ni->ni_rates = ic->ic_bss->ni_rates;
569                         if (ic->ic_newassoc)
570                                 (*ic->ic_newassoc)(ic, ni, 1);
571                 }
572         }
573         return ni;
574 }
575
576 /*
577  * Like find but search based on the channel too.
578  */
579 struct ieee80211_node *
580 ieee80211_lookup_node(struct ieee80211com *ic,
581         uint8_t *macaddr, struct ieee80211_channel *chan)
582 {
583         struct ieee80211_node *ni;
584         int hash;
585         lwkt_tokref ilock;
586
587         hash = IEEE80211_NODE_HASH(macaddr);
588         lwkt_gettoken(&ilock, &ic->ic_nodetoken);
589         LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
590                 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
591                     ni->ni_chan == chan) {
592                         atomic_add_int(&ni->ni_refcnt, 1);/* mark referenced */
593                         break;
594                 }
595         }
596         lwkt_reltoken(&ilock);
597         return ni;
598 }
599
600 static void
601 _ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
602 {
603         KASSERT(ni != ic->ic_bss, ("freeing bss node"));
604
605         TAILQ_REMOVE(&ic->ic_node, ni, ni_list);
606         LIST_REMOVE(ni, ni_hash);
607         if (TAILQ_EMPTY(&ic->ic_node))
608                 ic->ic_inact_timer = 0;
609         (*ic->ic_node_free)(ic, ni);
610 }
611
612 void
613 ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
614 {
615         lwkt_tokref ilock;
616
617         KASSERT(ni != ic->ic_bss, ("freeing ic_bss"));
618
619         /* XXX DF atomic op */
620         crit_enter();
621         --ni->ni_refcnt;
622         if (ni->ni_refcnt == 0) {
623                 crit_exit();
624                 lwkt_gettoken(&ilock, &ic->ic_nodetoken);
625                 _ieee80211_free_node(ic, ni);
626                 lwkt_reltoken(&ilock);
627         } else {
628                 crit_exit();
629         }
630 }
631
632 void
633 ieee80211_free_allnodes(struct ieee80211com *ic)
634 {
635         struct ieee80211_node *ni;
636         lwkt_tokref ilock;
637
638         lwkt_gettoken(&ilock, &ic->ic_nodetoken);
639         while ((ni = TAILQ_FIRST(&ic->ic_node)) != NULL)
640                 _ieee80211_free_node(ic, ni);  
641         lwkt_reltoken(&ilock);
642 }
643
644 /*
645  * Timeout inactive nodes.  Note that we cannot hold the node
646  * lock while sending a frame as this would lead to a LOR.
647  * Instead we use a generation number to mark nodes that we've
648  * scanned and drop the lock and restart a scan if we have to
649  * time out a node.  Since we are single-threaded by virtue of
650  * controlling the inactivity timer we can be sure this will
651  * process each node only once.
652  */
653 void
654 ieee80211_timeout_nodes(struct ieee80211com *ic)
655 {
656         struct ieee80211_node *ni;
657         lwkt_tokref ilock;
658         u_int gen = ic->ic_scangen++;           /* NB: ok 'cuz single-threaded*/
659
660 restart:
661         lwkt_gettoken(&ilock, &ic->ic_nodetoken);
662         TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
663                 if (ni->ni_scangen == gen)      /* previously handled */
664                         continue;
665                 ni->ni_scangen = gen;
666                 if (++ni->ni_inact > IEEE80211_INACT_MAX) {
667                         IEEE80211_DPRINTF(("station %6D timed out "
668                             "due to inactivity (%u secs)\n",
669                             ni->ni_macaddr, ":", ni->ni_inact));
670                         /*
671                          * Send a deauthenticate frame.
672                          *
673                          * Drop the node lock before sending the
674                          * deauthentication frame in case the driver takes     
675                          * a lock, as this will result in a LOR between the     
676                          * node lock and the driver lock.
677                          */
678                         lwkt_reltoken(&ilock);
679                         IEEE80211_SEND_MGMT(ic, ni,
680                             IEEE80211_FC0_SUBTYPE_DEAUTH,
681                             IEEE80211_REASON_AUTH_EXPIRE);
682                         ieee80211_free_node(ic, ni);
683                         ic->ic_stats.is_node_timeout++;
684                         goto restart;
685                 }
686         }
687         if (!TAILQ_EMPTY(&ic->ic_node))
688                 ic->ic_inact_timer = IEEE80211_INACT_WAIT;
689         lwkt_reltoken(&ilock);
690 }
691
692 void
693 ieee80211_iterate_nodes(struct ieee80211com *ic, ieee80211_iter_func *f, void *arg)
694 {
695         struct ieee80211_node *ni;
696         struct lwkt_tokref ilock;
697
698         lwkt_gettoken(&ilock, &ic->ic_nodetoken);
699         TAILQ_FOREACH(ni, &ic->ic_node, ni_list)
700                 (*f)(arg, ni);
701         lwkt_reltoken(&ilock);
702 }