The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/net80211/ieee80211_node.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    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 
   33 #include <sys/cdefs.h>
   34 __FBSDID("$FreeBSD$");
   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 
   51 #include <machine/atomic.h>
   52  
   53 #include <net/if.h>
   54 #include <net/if_dl.h>
   55 #include <net/if_media.h>
   56 #include <net/if_arp.h>
   57 #include <net/ethernet.h>
   58 #include <net/if_llc.h>
   59 
   60 #include <net80211/ieee80211_var.h>
   61 
   62 #include <net/bpf.h>
   63 
   64 #ifdef INET
   65 #include <netinet/in.h> 
   66 #include <netinet/if_ether.h>
   67 #endif
   68 
   69 static struct ieee80211_node *ieee80211_node_alloc(struct ieee80211com *);
   70 static void ieee80211_node_free(struct ieee80211com *, struct ieee80211_node *);
   71 static void ieee80211_node_copy(struct ieee80211com *,
   72                 struct ieee80211_node *, const struct ieee80211_node *);
   73 static u_int8_t ieee80211_node_getrssi(struct ieee80211com *,
   74                 struct ieee80211_node *);
   75 
   76 static void ieee80211_setup_node(struct ieee80211com *ic,
   77                 struct ieee80211_node *ni, u_int8_t *macaddr);
   78 static void _ieee80211_free_node(struct ieee80211com *,
   79                 struct ieee80211_node *);
   80 
   81 MALLOC_DEFINE(M_80211_NODE, "80211node", "802.11 node state");
   82 
   83 void
   84 ieee80211_node_attach(struct ifnet *ifp)
   85 {
   86         struct ieee80211com *ic = (void *)ifp;
   87 
   88         /* XXX need unit */
   89         IEEE80211_NODE_LOCK_INIT(ic, ifp->if_xname);
   90         TAILQ_INIT(&ic->ic_node);
   91         ic->ic_node_alloc = ieee80211_node_alloc;
   92         ic->ic_node_free = ieee80211_node_free;
   93         ic->ic_node_copy = ieee80211_node_copy;
   94         ic->ic_node_getrssi = ieee80211_node_getrssi;
   95         ic->ic_scangen = 1;
   96 }
   97 
   98 void
   99 ieee80211_node_lateattach(struct ifnet *ifp)
  100 {
  101         struct ieee80211com *ic = (void *)ifp;
  102         struct ieee80211_node *ni;
  103 
  104         ni = (*ic->ic_node_alloc)(ic);
  105         KASSERT(ni != NULL, ("unable to setup inital BSS node"));
  106         ni->ni_chan = IEEE80211_CHAN_ANYC;
  107         ic->ic_bss = ni;
  108         ic->ic_txpower = IEEE80211_TXPOWER_MAX;
  109 }
  110 
  111 void
  112 ieee80211_node_detach(struct ifnet *ifp)
  113 {
  114         struct ieee80211com *ic = (void *)ifp;
  115 
  116         if (ic->ic_bss != NULL)
  117                 (*ic->ic_node_free)(ic, ic->ic_bss);
  118         ieee80211_free_allnodes(ic);
  119         IEEE80211_NODE_LOCK_DESTROY(ic);
  120 }
  121 
  122 /*
  123  * AP scanning support.
  124  */
  125 
  126 /*
  127  * Initialize the active channel set based on the set
  128  * of available channels and the current PHY mode.
  129  */
  130 static void
  131 ieee80211_reset_scan(struct ifnet *ifp)
  132 {
  133         struct ieee80211com *ic = (void *)ifp;
  134 
  135         memcpy(ic->ic_chan_scan, ic->ic_chan_active,
  136                 sizeof(ic->ic_chan_active));
  137         /* NB: hack, setup so next_scan starts with the first channel */
  138         if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC)
  139                 ic->ic_bss->ni_chan = &ic->ic_channels[IEEE80211_CHAN_MAX];
  140 }
  141 
  142 /*
  143  * Begin an active scan.
  144  */
  145 void
  146 ieee80211_begin_scan(struct ifnet *ifp)
  147 {
  148         struct ieee80211com *ic = (void *)ifp;
  149 
  150         /*
  151          * In all but hostap mode scanning starts off in
  152          * an active mode before switching to passive.
  153          */
  154         if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
  155                 ic->ic_flags |= IEEE80211_F_ASCAN;
  156                 ic->ic_stats.is_scan_active++;
  157         } else
  158                 ic->ic_stats.is_scan_passive++;
  159         if (ifp->if_flags & IFF_DEBUG)
  160                 if_printf(ifp, "begin %s scan\n",
  161                         (ic->ic_flags & IEEE80211_F_ASCAN) ?
  162                                 "active" : "passive");
  163         /*
  164          * Clear scan state and flush any previously seen
  165          * AP's.  Note that the latter assumes we don't act
  166          * as both an AP and a station, otherwise we'll
  167          * potentially flush state of stations associated
  168          * with us.
  169          */
  170         ieee80211_reset_scan(ifp);
  171         ieee80211_free_allnodes(ic);
  172 
  173         /* Scan the next channel. */
  174         ieee80211_next_scan(ifp);
  175 }
  176 
  177 /*
  178  * Switch to the next channel marked for scanning.
  179  */
  180 void
  181 ieee80211_next_scan(struct ifnet *ifp)
  182 {
  183         struct ieee80211com *ic = (void *)ifp;
  184         struct ieee80211_channel *chan;
  185 
  186         chan = ic->ic_bss->ni_chan;
  187         for (;;) {
  188                 if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
  189                         chan = &ic->ic_channels[0];
  190                 if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
  191                         /*
  192                          * Honor channels marked passive-only
  193                          * during an active scan.
  194                          */
  195                         if ((ic->ic_flags & IEEE80211_F_ASCAN) == 0 ||
  196                             (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0)
  197                                 break;
  198                 }
  199                 if (chan == ic->ic_bss->ni_chan) {
  200                         ieee80211_end_scan(ifp);
  201                         return;
  202                 }
  203         }
  204         clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
  205         IEEE80211_DPRINTF(("ieee80211_next_scan: chan %d->%d\n",
  206             ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
  207             ieee80211_chan2ieee(ic, chan)));
  208         ic->ic_bss->ni_chan = chan;
  209         ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
  210 }
  211 
  212 void
  213 ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
  214 {
  215         struct ieee80211_node *ni;
  216         struct ifnet *ifp = &ic->ic_if;
  217 
  218         ni = ic->ic_bss;
  219         if (ifp->if_flags & IFF_DEBUG)
  220                 if_printf(ifp, "creating ibss\n");
  221         ic->ic_flags |= IEEE80211_F_SIBSS;
  222         ni->ni_chan = chan;
  223         ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
  224         IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr);
  225         IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
  226         if (ic->ic_opmode == IEEE80211_M_IBSS)
  227                 ni->ni_bssid[0] |= 0x02;        /* local bit for IBSS */
  228         ni->ni_esslen = ic->ic_des_esslen;
  229         memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
  230         ni->ni_rssi = 0;
  231         ni->ni_rstamp = 0;
  232         memset(ni->ni_tstamp, 0, sizeof(ni->ni_tstamp));
  233         ni->ni_intval = ic->ic_lintval;
  234         ni->ni_capinfo = IEEE80211_CAPINFO_IBSS;
  235         if (ic->ic_flags & IEEE80211_F_WEPON)
  236                 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
  237         if (ic->ic_phytype == IEEE80211_T_FH) {
  238                 ni->ni_fhdwell = 200;   /* XXX */
  239                 ni->ni_fhindex = 1;
  240         }
  241         ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
  242 }
  243 
  244 static int
  245 ieee80211_match_bss(struct ifnet *ifp, struct ieee80211_node *ni)
  246 {
  247         struct ieee80211com *ic = (void *)ifp;
  248         u_int8_t rate;
  249         int fail;
  250 
  251         fail = 0;
  252         if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
  253                 fail |= 0x01;
  254         if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
  255             ni->ni_chan != ic->ic_des_chan)
  256                 fail |= 0x01;
  257         if (ic->ic_opmode == IEEE80211_M_IBSS) {
  258                 if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
  259                         fail |= 0x02;
  260         } else {
  261                 if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
  262                         fail |= 0x02;
  263         }
  264         if (ic->ic_flags & IEEE80211_F_WEPON) {
  265                 if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
  266                         fail |= 0x04;
  267         } else {
  268                 /* XXX does this mean privacy is supported or required? */
  269                 if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
  270                         fail |= 0x04;
  271         }
  272         rate = ieee80211_fix_rate(ic, ni, IEEE80211_F_DONEGO);
  273         if (rate & IEEE80211_RATE_BASIC)
  274                 fail |= 0x08;
  275         if (ic->ic_des_esslen != 0 &&
  276             (ni->ni_esslen != ic->ic_des_esslen ||
  277              memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
  278                 fail |= 0x10;
  279         if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
  280             !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
  281                 fail |= 0x20;
  282 #ifdef IEEE80211_DEBUG
  283         if (ifp->if_flags & IFF_DEBUG) {
  284                 printf(" %c %s", fail ? '-' : '+',
  285                     ether_sprintf(ni->ni_macaddr));
  286                 printf(" %s%c", ether_sprintf(ni->ni_bssid),
  287                     fail & 0x20 ? '!' : ' ');
  288                 printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
  289                         fail & 0x01 ? '!' : ' ');
  290                 printf(" %+4d", ni->ni_rssi);
  291                 printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
  292                     fail & 0x08 ? '!' : ' ');
  293                 printf(" %4s%c",
  294                     (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
  295                     (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
  296                     "????",
  297                     fail & 0x02 ? '!' : ' ');
  298                 printf(" %3s%c ",
  299                     (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
  300                     "wep" : "no",
  301                     fail & 0x04 ? '!' : ' ');
  302                 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
  303                 printf("%s\n", fail & 0x10 ? "!" : "");
  304         }
  305 #endif
  306         return fail;
  307 }
  308 
  309 /*
  310  * Complete a scan of potential channels.
  311  */
  312 void
  313 ieee80211_end_scan(struct ifnet *ifp)
  314 {
  315         struct ieee80211com *ic = (void *)ifp;
  316         struct ieee80211_node *ni, *nextbs, *selbs;
  317         int i, fail;
  318 
  319         ic->ic_flags &= ~IEEE80211_F_ASCAN;
  320         ni = TAILQ_FIRST(&ic->ic_node);
  321 
  322         if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
  323                 /* XXX off stack? */
  324                 u_char occupied[roundup(IEEE80211_CHAN_MAX, NBBY)];
  325                 /*
  326                  * The passive scan to look for existing AP's completed,
  327                  * select a channel to camp on.  Identify the channels
  328                  * that already have one or more AP's and try to locate
  329                  * an unnoccupied one.  If that fails, pick a random
  330                  * channel from the active set.
  331                  */
  332                 for (; ni != NULL; ni = nextbs) {
  333                         ieee80211_ref_node(ni);
  334                         nextbs = TAILQ_NEXT(ni, ni_list);
  335                         setbit(occupied, ieee80211_chan2ieee(ic, ni->ni_chan));
  336                         ieee80211_free_node(ic, ni);
  337                 }
  338                 for (i = 0; i < IEEE80211_CHAN_MAX; i++)
  339                         if (isset(ic->ic_chan_active, i) && isclr(occupied, i))
  340                                 break;
  341                 if (i == IEEE80211_CHAN_MAX) {
  342                         fail = arc4random() & 3;        /* random 0-3 */
  343                         for (i = 0; i < IEEE80211_CHAN_MAX; i++)
  344                                 if (isset(ic->ic_chan_active, i) && fail-- == 0)
  345                                         break;
  346                 }
  347                 ieee80211_create_ibss(ic, &ic->ic_channels[i]);
  348                 return;
  349         }
  350         if (ni == NULL) {
  351                 IEEE80211_DPRINTF(("%s: no scan candidate\n", __func__));
  352   notfound:
  353                 if (ic->ic_opmode == IEEE80211_M_IBSS &&
  354                     (ic->ic_flags & IEEE80211_F_IBSSON) &&
  355                     ic->ic_des_esslen != 0) {
  356                         ieee80211_create_ibss(ic, ic->ic_ibss_chan);
  357                         return;
  358                 }
  359                 /*
  360                  * Reset the list of channels to scan and start again.
  361                  */
  362                 ieee80211_reset_scan(ifp);
  363                 ieee80211_next_scan(ifp);
  364                 return;
  365         }
  366         selbs = NULL;
  367         if (ifp->if_flags & IFF_DEBUG)
  368                 if_printf(ifp, "\tmacaddr          bssid         chan  rssi rate flag  wep  essid\n");
  369         for (; ni != NULL; ni = nextbs) {
  370                 ieee80211_ref_node(ni);
  371                 nextbs = TAILQ_NEXT(ni, ni_list);
  372                 if (ni->ni_fails) {
  373                         /*
  374                          * The configuration of the access points may change
  375                          * during my scan.  So delete the entry for the AP
  376                          * and retry to associate if there is another beacon.
  377                          */
  378                         if (ni->ni_fails++ > 2)
  379                                 ieee80211_free_node(ic, ni);
  380                         continue;
  381                 }
  382                 if (ieee80211_match_bss(ifp, ni) == 0) {
  383                         if (selbs == NULL)
  384                                 selbs = ni;
  385                         else if (ni->ni_rssi > selbs->ni_rssi) {
  386                                 ieee80211_unref_node(&selbs);
  387                                 selbs = ni;
  388                         } else
  389                                 ieee80211_unref_node(&ni);
  390                 } else {
  391                         ieee80211_unref_node(&ni);
  392                 }
  393         }
  394         if (selbs == NULL)
  395                 goto notfound;
  396         (*ic->ic_node_copy)(ic, ic->ic_bss, selbs);
  397         if (ic->ic_opmode == IEEE80211_M_IBSS) {
  398                 ieee80211_fix_rate(ic, ic->ic_bss, IEEE80211_F_DOFRATE |
  399                     IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
  400                 if (ic->ic_bss->ni_rates.rs_nrates == 0) {
  401                         selbs->ni_fails++;
  402                         ieee80211_unref_node(&selbs);
  403                         goto notfound;
  404                 }
  405                 ieee80211_unref_node(&selbs);
  406                 /*
  407                  * Discard scan set; the nodes have a refcnt of zero
  408                  * and have not asked the driver to setup private
  409                  * node state.  Let them be repopulated on demand either
  410                  * through transmission (ieee80211_find_txnode) or receipt
  411                  * of a probe response (to be added).
  412                  */
  413                 ieee80211_free_allnodes(ic);
  414                 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
  415         } else {
  416                 ieee80211_unref_node(&selbs);
  417                 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
  418         }
  419 }
  420 
  421 static struct ieee80211_node *
  422 ieee80211_node_alloc(struct ieee80211com *ic)
  423 {
  424         struct ieee80211_node *ni;
  425         MALLOC(ni, struct ieee80211_node *, sizeof(struct ieee80211_node),
  426                 M_80211_NODE, M_NOWAIT | M_ZERO);
  427         return ni;
  428 }
  429 
  430 static void
  431 ieee80211_node_free(struct ieee80211com *ic, struct ieee80211_node *ni)
  432 {
  433         FREE(ni, M_80211_NODE);
  434 }
  435 
  436 static void
  437 ieee80211_node_copy(struct ieee80211com *ic,
  438         struct ieee80211_node *dst, const struct ieee80211_node *src)
  439 {
  440         *dst = *src;
  441 }
  442 
  443 static u_int8_t
  444 ieee80211_node_getrssi(struct ieee80211com *ic, struct ieee80211_node *ni)
  445 {
  446         return ni->ni_rssi;
  447 }
  448 
  449 static void
  450 ieee80211_setup_node(struct ieee80211com *ic,
  451         struct ieee80211_node *ni, u_int8_t *macaddr)
  452 {
  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         IEEE80211_NODE_LOCK(ic);
  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         IEEE80211_NODE_UNLOCK(ic);
  473 }
  474 
  475 struct ieee80211_node *
  476 ieee80211_alloc_node(struct ieee80211com *ic, u_int8_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, u_int8_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, u_int8_t *macaddr)
  504 {
  505         struct ieee80211_node *ni;
  506         int hash;
  507 
  508         IEEE80211_NODE_LOCK_ASSERT(ic);
  509 
  510         hash = IEEE80211_NODE_HASH(macaddr);
  511         LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
  512                 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr)) {
  513                         atomic_add_int(&ni->ni_refcnt, 1);/* mark referenced */
  514                         return ni;
  515                 }
  516         }
  517         return NULL;
  518 }
  519 
  520 struct ieee80211_node *
  521 ieee80211_find_node(struct ieee80211com *ic, u_int8_t *macaddr)
  522 {
  523         struct ieee80211_node *ni;
  524 
  525         IEEE80211_NODE_LOCK(ic);
  526         ni = _ieee80211_find_node(ic, macaddr);
  527         IEEE80211_NODE_UNLOCK(ic);
  528         return ni;
  529 }
  530 
  531 /*
  532  * Return a reference to the appropriate node for sending
  533  * a data frame.  This handles node discovery in adhoc networks.
  534  */
  535 struct ieee80211_node *
  536 ieee80211_find_txnode(struct ieee80211com *ic, u_int8_t *macaddr)
  537 {
  538         struct ieee80211_node *ni;
  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         IEEE80211_NODE_LOCK(ic);
  550         ni = _ieee80211_find_node(ic, macaddr);
  551         IEEE80211_NODE_UNLOCK(ic);
  552         if (ni == NULL &&
  553             (ic->ic_opmode == IEEE80211_M_IBSS ||
  554              ic->ic_opmode == IEEE80211_M_AHDEMO)) {
  555                 /*
  556                  * Fake up a node; this handles node discovery in
  557                  * adhoc mode.  Note that for the driver's benefit
  558                  * we we treat this like an association so the driver
  559                  * has an opportunity to setup it's private state.
  560                  *
  561                  * XXX need better way to handle this; issue probe
  562                  *     request so we can deduce rate set, etc.
  563                  */
  564                 ni = ieee80211_dup_bss(ic, macaddr);
  565                 if (ni != NULL) {
  566                         /* XXX no rate negotiation; just dup */
  567                         ni->ni_rates = ic->ic_bss->ni_rates;
  568                         if (ic->ic_newassoc)
  569                                 (*ic->ic_newassoc)(ic, ni, 1);
  570                 }
  571         }
  572         return ni;
  573 }
  574 
  575 /*
  576  * Like find but search based on the channel too.
  577  */
  578 struct ieee80211_node *
  579 ieee80211_lookup_node(struct ieee80211com *ic,
  580         u_int8_t *macaddr, struct ieee80211_channel *chan)
  581 {
  582         struct ieee80211_node *ni;
  583         int hash;
  584 
  585         hash = IEEE80211_NODE_HASH(macaddr);
  586         IEEE80211_NODE_LOCK(ic);
  587         LIST_FOREACH(ni, &ic->ic_hash[hash], ni_hash) {
  588                 if (IEEE80211_ADDR_EQ(ni->ni_macaddr, macaddr) &&
  589                     ni->ni_chan == chan) {
  590                         atomic_add_int(&ni->ni_refcnt, 1);/* mark referenced */
  591                         break;
  592                 }
  593         }
  594         IEEE80211_NODE_UNLOCK(ic);
  595         return ni;
  596 }
  597 
  598 static void
  599 _ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
  600 {
  601         KASSERT(ni != ic->ic_bss, ("freeing bss node"));
  602 
  603         TAILQ_REMOVE(&ic->ic_node, ni, ni_list);
  604         LIST_REMOVE(ni, ni_hash);
  605         if (TAILQ_EMPTY(&ic->ic_node))
  606                 ic->ic_inact_timer = 0;
  607         (*ic->ic_node_free)(ic, ni);
  608 }
  609 
  610 void
  611 ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
  612 {
  613         KASSERT(ni != ic->ic_bss, ("freeing ic_bss"));
  614 
  615         /* XXX need equivalent of atomic_dec_and_test */
  616         atomic_subtract_int(&ni->ni_refcnt, 1);
  617         if (atomic_cmpset_int(&ni->ni_refcnt, 0, 1)) {
  618                 IEEE80211_NODE_LOCK(ic);
  619                 _ieee80211_free_node(ic, ni);
  620                 IEEE80211_NODE_UNLOCK(ic);
  621         }
  622 }
  623 
  624 void
  625 ieee80211_free_allnodes(struct ieee80211com *ic)
  626 {
  627         struct ieee80211_node *ni;
  628 
  629         IEEE80211_NODE_LOCK(ic);
  630         while ((ni = TAILQ_FIRST(&ic->ic_node)) != NULL)
  631                 _ieee80211_free_node(ic, ni);  
  632         IEEE80211_NODE_UNLOCK(ic);
  633 }
  634 
  635 /*
  636  * Timeout inactive nodes.  Note that we cannot hold the node
  637  * lock while sending a frame as this would lead to a LOR.
  638  * Instead we use a generation number to mark nodes that we've
  639  * scanned and drop the lock and restart a scan if we have to
  640  * time out a node.  Since we are single-threaded by virtue of
  641  * controlling the inactivity timer we can be sure this will
  642  * process each node only once.
  643  */
  644 void
  645 ieee80211_timeout_nodes(struct ieee80211com *ic)
  646 {
  647         struct ieee80211_node *ni;
  648         u_int gen = ic->ic_scangen++;           /* NB: ok 'cuz single-threaded*/
  649 
  650 restart:
  651         IEEE80211_NODE_LOCK(ic);
  652         TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
  653                 if (ni->ni_scangen == gen)      /* previously handled */
  654                         continue;
  655                 ni->ni_scangen = gen;
  656                 if (++ni->ni_inact > IEEE80211_INACT_MAX) {
  657                         IEEE80211_DPRINTF(("station %s timed out "
  658                             "due to inactivity (%u secs)\n",
  659                             ether_sprintf(ni->ni_macaddr),
  660                             ni->ni_inact));
  661                         /*
  662                          * Send a deauthenticate frame.
  663                          *
  664                          * Drop the node lock before sending the
  665                          * deauthentication frame in case the driver takes     
  666                          * a lock, as this will result in a LOR between the     
  667                          * node lock and the driver lock.
  668                          */
  669                         IEEE80211_NODE_UNLOCK(ic);
  670                         IEEE80211_SEND_MGMT(ic, ni,
  671                             IEEE80211_FC0_SUBTYPE_DEAUTH,
  672                             IEEE80211_REASON_AUTH_EXPIRE);
  673                         ieee80211_free_node(ic, ni);
  674                         ic->ic_stats.is_node_timeout++;
  675                         goto restart;
  676                 }
  677         }
  678         if (!TAILQ_EMPTY(&ic->ic_node))
  679                 ic->ic_inact_timer = IEEE80211_INACT_WAIT;
  680         IEEE80211_NODE_UNLOCK(ic);
  681 }
  682 
  683 void
  684 ieee80211_iterate_nodes(struct ieee80211com *ic, ieee80211_iter_func *f, void *arg)
  685 {
  686         struct ieee80211_node *ni;
  687 
  688         IEEE80211_NODE_LOCK(ic);
  689         TAILQ_FOREACH(ni, &ic->ic_node, ni_list)
  690                 (*f)(arg, ni);
  691         IEEE80211_NODE_UNLOCK(ic);
  692 }

Cache object: efb8b6ad835b745b174fc75575c36c66


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.