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.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: ieee80211.c,v 1.33 2005/02/26 22:45:09 perry Exp $     */
    2 /*-
    3  * Copyright (c) 2001 Atsushi Onoe
    4  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. The name of the author may not be used to endorse or promote products
   16  *    derived from this software without specific prior written permission.
   17  *
   18  * Alternatively, this software may be distributed under the terms of the
   19  * GNU General Public License ("GPL") version 2 as published by the Free
   20  * Software Foundation.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   32  */
   33 
   34 #include <sys/cdefs.h>
   35 #ifdef __FreeBSD__
   36 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211.c,v 1.11 2004/04/02 20:19:20 sam Exp $");
   37 #else
   38 __KERNEL_RCSID(0, "$NetBSD: ieee80211.c,v 1.33 2005/02/26 22:45:09 perry Exp $");
   39 #endif
   40 
   41 /*
   42  * IEEE 802.11 generic handler
   43  */
   44 
   45 #include "opt_inet.h"
   46 #include "bpfilter.h"
   47 
   48 #include <sys/param.h>
   49 #include <sys/systm.h>
   50 #include <sys/mbuf.h>
   51 #include <sys/malloc.h>
   52 #include <sys/kernel.h>
   53 #include <sys/socket.h>
   54 #include <sys/sockio.h>
   55 #include <sys/endian.h>
   56 #include <sys/errno.h>
   57 #ifdef __FreeBSD__
   58 #include <sys/bus.h>
   59 #endif
   60 #include <sys/proc.h>
   61 #include <sys/sysctl.h>
   62 
   63 #ifdef __FreeBSD__
   64 #include <machine/atomic.h>
   65 #endif
   66 
   67 #include <net/if.h>
   68 #include <net/if_dl.h>
   69 #include <net/if_media.h>
   70 #include <net/if_arp.h>
   71 #ifdef __FreeBSD__
   72 #include <net/ethernet.h>
   73 #else
   74 #include <net/if_ether.h>
   75 #endif
   76 #include <net/if_llc.h>
   77 
   78 #include <net80211/ieee80211_var.h>
   79 #include <net80211/ieee80211_compat.h>
   80 #include <net80211/ieee80211_sysctl.h>
   81 
   82 #include <net/bpf.h>
   83 
   84 #ifdef INET
   85 #include <netinet/in.h>
   86 #ifdef __FreeBSD__
   87 #include <netinet/if_ether.h>
   88 #else
   89 #include <net/if_ether.h>
   90 #endif
   91 #endif
   92 
   93 #ifdef IEEE80211_DEBUG
   94 int     ieee80211_debug = 0;
   95 #ifdef __NetBSD__
   96 static int ieee80211_debug_nodenum;
   97 #endif /* __NetBSD__ */
   98 
   99 #ifdef __FreeBSD__
  100 SYSCTL_INT(_debug, OID_AUTO, ieee80211, CTLFLAG_RW, &ieee80211_debug,
  101             0, "IEEE 802.11 media debugging printfs");
  102 #endif
  103 #endif
  104 
  105 int     ieee80211_cache_size = IEEE80211_CACHE_SIZE;
  106 static int ieee80211_cache_size_nodenum;
  107 
  108 struct ieee80211com_head ieee80211com_head =
  109     LIST_HEAD_INITIALIZER(ieee80211com_head);
  110 
  111 static void ieee80211_setbasicrates(struct ieee80211com *);
  112 
  113 #ifdef __NetBSD__
  114 static void sysctl_ieee80211_fill_node(struct ieee80211_node *,
  115     struct ieee80211_node_sysctl *, int, struct ieee80211_channel *, int);
  116 static struct ieee80211_node *ieee80211_node_walknext(
  117     struct ieee80211_node_walk *);
  118 static struct ieee80211_node *ieee80211_node_walkfirst(
  119     struct ieee80211_node_walk *, u_short);
  120 static int sysctl_ieee80211_verify(SYSCTLFN_ARGS);
  121 static int sysctl_ieee80211_node(SYSCTLFN_ARGS);
  122 #endif /* __NetBSD__ */
  123 
  124 #define LOGICALLY_EQUAL(x, y)   (!(x) == !(y))
  125 
  126 static const char *ieee80211_phymode_name[] = {
  127         "auto",         /* IEEE80211_MODE_AUTO */
  128         "11a",          /* IEEE80211_MODE_11A */
  129         "11b",          /* IEEE80211_MODE_11B */
  130         "11g",          /* IEEE80211_MODE_11G */
  131         "FH",           /* IEEE80211_MODE_FH */
  132         "turbo",        /* IEEE80211_MODE_TURBO */
  133 };
  134 
  135 void
  136 ieee80211_ifattach(struct ifnet *ifp)
  137 {
  138         struct ieee80211com *ic = (void *)ifp;
  139         struct ieee80211_channel *c;
  140         int i;
  141 
  142         ether_ifattach(ifp, ic->ic_myaddr);
  143 #if NBPFILTER > 0
  144         bpfattach2(ifp, DLT_IEEE802_11,
  145             sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf);
  146 #endif
  147         ieee80211_crypto_attach(ifp);
  148 
  149         /*
  150          * Fill in 802.11 available channel set, mark
  151          * all available channels as active, and pick
  152          * a default channel if not already specified.
  153          */
  154         memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
  155         ic->ic_modecaps |= 1<<IEEE80211_MODE_AUTO;
  156         for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
  157                 c = &ic->ic_channels[i];
  158                 if (c->ic_flags) {
  159                         /*
  160                          * Verify driver passed us valid data.
  161                          */
  162                         if (i != ieee80211_chan2ieee(ic, c)) {
  163                                 if_printf(ifp, "bad channel ignored; "
  164                                         "freq %u flags %x number %u\n",
  165                                         c->ic_freq, c->ic_flags, i);
  166                                 c->ic_flags = 0;        /* NB: remove */
  167                                 continue;
  168                         }
  169                         setbit(ic->ic_chan_avail, i);
  170                         /*
  171                          * Identify mode capabilities.
  172                          */
  173                         if (IEEE80211_IS_CHAN_A(c))
  174                                 ic->ic_modecaps |= 1<<IEEE80211_MODE_11A;
  175                         if (IEEE80211_IS_CHAN_B(c))
  176                                 ic->ic_modecaps |= 1<<IEEE80211_MODE_11B;
  177                         if (IEEE80211_IS_CHAN_PUREG(c))
  178                                 ic->ic_modecaps |= 1<<IEEE80211_MODE_11G;
  179                         if (IEEE80211_IS_CHAN_FHSS(c))
  180                                 ic->ic_modecaps |= 1<<IEEE80211_MODE_FH;
  181                         if (IEEE80211_IS_CHAN_T(c))
  182                                 ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO;
  183                 }
  184         }
  185         /* validate ic->ic_curmode */
  186         if ((ic->ic_modecaps & (1<<ic->ic_curmode)) == 0)
  187                 ic->ic_curmode = IEEE80211_MODE_AUTO;
  188         ic->ic_des_chan = IEEE80211_CHAN_ANYC;  /* any channel is ok */
  189 
  190         ieee80211_setbasicrates(ic);
  191         (void) ieee80211_setmode(ic, ic->ic_curmode);
  192 
  193         if (ic->ic_lintval == 0)
  194                 ic->ic_lintval = 100;           /* default sleep */
  195         ic->ic_bmisstimeout = 7*ic->ic_lintval; /* default 7 beacons */
  196 
  197         LIST_INSERT_HEAD(&ieee80211com_head, ic, ic_list);
  198         ieee80211_node_attach(ic);
  199         ieee80211_proto_attach(ifp);
  200 }
  201 
  202 void
  203 ieee80211_ifdetach(struct ifnet *ifp)
  204 {
  205         struct ieee80211com *ic = (void *)ifp;
  206 
  207         ieee80211_proto_detach(ifp);
  208         ieee80211_crypto_detach(ifp);
  209         ieee80211_node_detach(ic);
  210         LIST_REMOVE(ic, ic_list);
  211 #ifdef __FreeBSD__
  212         ifmedia_removeall(&ic->ic_media);
  213 #else
  214         ifmedia_delete_instance(&ic->ic_media, IFM_INST_ANY);
  215 #endif
  216 #if NBPFILTER > 0
  217         bpfdetach(ifp);
  218 #endif
  219         ether_ifdetach(ifp);
  220 }
  221 
  222 /*
  223  * Convert MHz frequency to IEEE channel number.
  224  */
  225 u_int
  226 ieee80211_mhz2ieee(u_int freq, u_int flags)
  227 {
  228         if (flags & IEEE80211_CHAN_2GHZ) {      /* 2GHz band */
  229                 if (freq == 2484)
  230                         return 14;
  231                 if (freq < 2484)
  232                         return (freq - 2407) / 5;
  233                 else
  234                         return 15 + ((freq - 2512) / 20);
  235         } else if (flags & IEEE80211_CHAN_5GHZ) {       /* 5Ghz band */
  236                 return (freq - 5000) / 5;
  237         } else {                                /* either, guess */
  238                 if (freq == 2484)
  239                         return 14;
  240                 if (freq < 2484)
  241                         return (freq - 2407) / 5;
  242                 if (freq < 5000)
  243                         return 15 + ((freq - 2512) / 20);
  244                 return (freq - 5000) / 5;
  245         }
  246 }
  247 
  248 /*
  249  * Convert channel to IEEE channel number.
  250  */
  251 u_int
  252 ieee80211_chan2ieee(struct ieee80211com *ic, struct ieee80211_channel *c)
  253 {
  254         if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX])
  255                 return c - ic->ic_channels;
  256         else if (c == IEEE80211_CHAN_ANYC)
  257                 return IEEE80211_CHAN_ANY;
  258         else if (c != NULL) {
  259                 if_printf(&ic->ic_if, "invalid channel freq %u flags %x\n",
  260                         c->ic_freq, c->ic_flags);
  261                 return 0;               /* XXX */
  262         } else {
  263                 if_printf(&ic->ic_if, "invalid channel (NULL)\n");
  264                 return 0;               /* XXX */
  265         }
  266 }
  267 
  268 /*
  269  * Convert IEEE channel number to MHz frequency.
  270  */
  271 u_int
  272 ieee80211_ieee2mhz(u_int chan, u_int flags)
  273 {
  274         if (flags & IEEE80211_CHAN_2GHZ) {      /* 2GHz band */
  275                 if (chan == 14)
  276                         return 2484;
  277                 if (chan < 14)
  278                         return 2407 + chan*5;
  279                 else
  280                         return 2512 + ((chan-15)*20);
  281         } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
  282                 return 5000 + (chan*5);
  283         } else {                                /* either, guess */
  284                 if (chan == 14)
  285                         return 2484;
  286                 if (chan < 14)                  /* 0-13 */
  287                         return 2407 + chan*5;
  288                 if (chan < 27)                  /* 15-26 */
  289                         return 2512 + ((chan-15)*20);
  290                 return 5000 + (chan*5);
  291         }
  292 }
  293 
  294 /*
  295  * Setup the media data structures according to the channel and
  296  * rate tables.  This must be called by the driver after
  297  * ieee80211_attach and before most anything else.
  298  */
  299 void
  300 ieee80211_media_init(struct ifnet *ifp,
  301         ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
  302 {
  303 #define ADD(_ic, _s, _o) \
  304         ifmedia_add(&(_ic)->ic_media, \
  305                 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
  306         struct ieee80211com *ic = (void *)ifp;
  307         struct ifmediareq imr;
  308         int i, j, mode, rate, maxrate, mword, mopt, r;
  309         struct ieee80211_rateset *rs;
  310         struct ieee80211_rateset allrates;
  311 
  312         /*
  313          * Do late attach work that must wait for any subclass
  314          * (i.e. driver) work such as overriding methods.
  315          */
  316         ieee80211_node_lateattach(ic);
  317 
  318         /*
  319          * Fill in media characteristics.
  320          */
  321         ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
  322         maxrate = 0;
  323         memset(&allrates, 0, sizeof(allrates));
  324         for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) {
  325                 static const u_int mopts[] = {
  326                         IFM_AUTO,
  327                         IFM_IEEE80211_11A,
  328                         IFM_IEEE80211_11B,
  329                         IFM_IEEE80211_11G,
  330                         IFM_IEEE80211_FH,
  331                         IFM_IEEE80211_11A | IFM_IEEE80211_TURBO,
  332                 };
  333                 if ((ic->ic_modecaps & (1<<mode)) == 0)
  334                         continue;
  335                 mopt = mopts[mode];
  336                 ADD(ic, IFM_AUTO, mopt);        /* e.g. 11a auto */
  337                 if (ic->ic_caps & IEEE80211_C_IBSS)
  338                         ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC);
  339                 if (ic->ic_caps & IEEE80211_C_HOSTAP)
  340                         ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP);
  341                 if (ic->ic_caps & IEEE80211_C_AHDEMO)
  342                         ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
  343                 if (ic->ic_caps & IEEE80211_C_MONITOR)
  344                         ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR);
  345                 if (mode == IEEE80211_MODE_AUTO)
  346                         continue;
  347                 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
  348                 rs = &ic->ic_sup_rates[mode];
  349                 for (i = 0; i < rs->rs_nrates; i++) {
  350                         rate = rs->rs_rates[i];
  351                         mword = ieee80211_rate2media(ic, rate, mode);
  352                         if (mword == 0)
  353                                 continue;
  354                         printf("%s%d%sMbps", (i != 0 ? " " : ""),
  355                             (rate & IEEE80211_RATE_VAL) / 2,
  356                             ((rate & 0x1) != 0 ? ".5" : ""));
  357                         ADD(ic, mword, mopt);
  358                         if (ic->ic_caps & IEEE80211_C_IBSS)
  359                                 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
  360                         if (ic->ic_caps & IEEE80211_C_HOSTAP)
  361                                 ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
  362                         if (ic->ic_caps & IEEE80211_C_AHDEMO)
  363                                 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
  364                         if (ic->ic_caps & IEEE80211_C_MONITOR)
  365                                 ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
  366                         /*
  367                          * Add rate to the collection of all rates.
  368                          */
  369                         r = rate & IEEE80211_RATE_VAL;
  370                         for (j = 0; j < allrates.rs_nrates; j++)
  371                                 if (allrates.rs_rates[j] == r)
  372                                         break;
  373                         if (j == allrates.rs_nrates) {
  374                                 /* unique, add to the set */
  375                                 allrates.rs_rates[j] = r;
  376                                 allrates.rs_nrates++;
  377                         }
  378                         rate = (rate & IEEE80211_RATE_VAL) / 2;
  379                         if (rate > maxrate)
  380                                 maxrate = rate;
  381                 }
  382                 printf("\n");
  383         }
  384         for (i = 0; i < allrates.rs_nrates; i++) {
  385                 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
  386                                 IEEE80211_MODE_AUTO);
  387                 if (mword == 0)
  388                         continue;
  389                 mword = IFM_SUBTYPE(mword);     /* remove media options */
  390                 ADD(ic, mword, 0);
  391                 if (ic->ic_caps & IEEE80211_C_IBSS)
  392                         ADD(ic, mword, IFM_IEEE80211_ADHOC);
  393                 if (ic->ic_caps & IEEE80211_C_HOSTAP)
  394                         ADD(ic, mword, IFM_IEEE80211_HOSTAP);
  395                 if (ic->ic_caps & IEEE80211_C_AHDEMO)
  396                         ADD(ic, mword, IFM_IEEE80211_ADHOC | IFM_FLAG0);
  397                 if (ic->ic_caps & IEEE80211_C_MONITOR)
  398                         ADD(ic, mword, IFM_IEEE80211_MONITOR);
  399         }
  400         ieee80211_media_status(ifp, &imr);
  401         ifmedia_set(&ic->ic_media, imr.ifm_active);
  402 #ifndef __linux__
  403         if (maxrate)
  404                 ifp->if_baudrate = IF_Mbps(maxrate);
  405 #endif
  406 #undef ADD
  407 }
  408 
  409 static int
  410 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
  411 {
  412 #define IEEERATE(_ic,_m,_i) \
  413         ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
  414         int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
  415         for (i = 0; i < nrates; i++)
  416                 if (IEEERATE(ic, mode, i) == rate)
  417                         return i;
  418         return -1;
  419 #undef IEEERATE
  420 }
  421 
  422 /*
  423  * Handle a media change request.
  424  */
  425 int
  426 ieee80211_media_change(struct ifnet *ifp)
  427 {
  428         struct ieee80211com *ic = (void *)ifp;
  429         struct ifmedia_entry *ime;
  430         enum ieee80211_opmode newopmode;
  431         enum ieee80211_phymode newphymode;
  432         int i, j, newrate, error = 0;
  433 
  434         ime = ic->ic_media.ifm_cur;
  435         /*
  436          * First, identify the phy mode.
  437          */
  438         switch (IFM_MODE(ime->ifm_media)) {
  439         case IFM_IEEE80211_11A:
  440                 newphymode = IEEE80211_MODE_11A;
  441                 break;
  442         case IFM_IEEE80211_11B:
  443                 newphymode = IEEE80211_MODE_11B;
  444                 break;
  445         case IFM_IEEE80211_11G:
  446                 newphymode = IEEE80211_MODE_11G;
  447                 break;
  448         case IFM_IEEE80211_FH:
  449                 newphymode = IEEE80211_MODE_FH;
  450                 break;
  451         case IFM_AUTO:
  452                 newphymode = IEEE80211_MODE_AUTO;
  453                 break;
  454         default:
  455                 return EINVAL;
  456         }
  457         /*
  458          * Turbo mode is an ``option''.  Eventually it
  459          * needs to be applied to 11g too.
  460          */
  461         if (ime->ifm_media & IFM_IEEE80211_TURBO) {
  462                 if (newphymode != IEEE80211_MODE_11A)
  463                         return EINVAL;
  464                 newphymode = IEEE80211_MODE_TURBO;
  465         }
  466         /*
  467          * Validate requested mode is available.
  468          */
  469         if ((ic->ic_modecaps & (1<<newphymode)) == 0)
  470                 return EINVAL;
  471 
  472         /*
  473          * Next, the fixed/variable rate.
  474          */
  475         i = -1;
  476         if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
  477                 /*
  478                  * Convert media subtype to rate.
  479                  */
  480                 newrate = ieee80211_media2rate(ime->ifm_media);
  481                 if (newrate == 0)
  482                         return EINVAL;
  483                 /*
  484                  * Check the rate table for the specified/current phy.
  485                  */
  486                 if (newphymode == IEEE80211_MODE_AUTO) {
  487                         /*
  488                          * In autoselect mode search for the rate.
  489                          */
  490                         for (j = IEEE80211_MODE_11A;
  491                              j < IEEE80211_MODE_MAX; j++) {
  492                                 if ((ic->ic_modecaps & (1<<j)) == 0)
  493                                         continue;
  494                                 i = findrate(ic, j, newrate);
  495                                 if (i != -1) {
  496                                         /* lock mode too */
  497                                         newphymode = j;
  498                                         break;
  499                                 }
  500                         }
  501                 } else {
  502                         i = findrate(ic, newphymode, newrate);
  503                 }
  504                 if (i == -1)                    /* mode/rate mismatch */
  505                         return EINVAL;
  506         }
  507         /* NB: defer rate setting to later */
  508 
  509         /*
  510          * Deduce new operating mode but don't install it just yet.
  511          */
  512         if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) ==
  513             (IFM_IEEE80211_ADHOC|IFM_FLAG0))
  514                 newopmode = IEEE80211_M_AHDEMO;
  515         else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
  516                 newopmode = IEEE80211_M_HOSTAP;
  517         else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
  518                 newopmode = IEEE80211_M_IBSS;
  519         else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
  520                 newopmode = IEEE80211_M_MONITOR;
  521         else
  522                 newopmode = IEEE80211_M_STA;
  523 
  524         /*
  525          * Autoselect doesn't make sense when operating as an AP.
  526          * If no phy mode has been selected, pick one and lock it
  527          * down so rate tables can be used in forming beacon frames
  528          * and the like.
  529          */
  530         if (newopmode == IEEE80211_M_HOSTAP &&
  531             newphymode == IEEE80211_MODE_AUTO) {
  532                 for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++)
  533                         if (ic->ic_modecaps & (1<<j)) {
  534                                 newphymode = j;
  535                                 break;
  536                         }
  537         }
  538 
  539         /*
  540          * Handle phy mode change.
  541          */
  542         if (ic->ic_curmode != newphymode) {             /* change phy mode */
  543                 error = ieee80211_setmode(ic, newphymode);
  544                 if (error != 0)
  545                         return error;
  546                 error = ENETRESET;
  547         }
  548 
  549         /*
  550          * Committed to changes, install the rate setting.
  551          */
  552         if (ic->ic_fixed_rate != i) {
  553                 ic->ic_fixed_rate = i;                  /* set fixed tx rate */
  554                 error = ENETRESET;
  555         }
  556 
  557         /*
  558          * Handle operating mode change.
  559          */
  560         if (ic->ic_opmode != newopmode) {
  561                 ic->ic_opmode = newopmode;
  562                 switch (newopmode) {
  563                 case IEEE80211_M_AHDEMO:
  564                 case IEEE80211_M_HOSTAP:
  565                 case IEEE80211_M_STA:
  566                 case IEEE80211_M_MONITOR:
  567                         ic->ic_flags &= ~IEEE80211_F_IBSSON;
  568                         break;
  569                 case IEEE80211_M_IBSS:
  570                         ic->ic_flags |= IEEE80211_F_IBSSON;
  571                         break;
  572                 }
  573                 error = ENETRESET;
  574         }
  575 #ifdef notdef
  576         if (error == 0)
  577                 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
  578 #endif
  579         return error;
  580 }
  581 
  582 void
  583 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
  584 {
  585         struct ieee80211com *ic = (void *)ifp;
  586         struct ieee80211_node *ni = NULL;
  587 
  588         imr->ifm_status = IFM_AVALID;
  589         imr->ifm_active = IFM_IEEE80211;
  590         if (ic->ic_state == IEEE80211_S_RUN)
  591                 imr->ifm_status |= IFM_ACTIVE;
  592         imr->ifm_active |= IFM_AUTO;
  593         switch (ic->ic_opmode) {
  594         case IEEE80211_M_STA:
  595                 ni = ic->ic_bss;
  596                 /* calculate rate subtype */
  597                 imr->ifm_active |= ieee80211_rate2media(ic,
  598                         ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode);
  599                 break;
  600         case IEEE80211_M_IBSS:
  601                 imr->ifm_active |= IFM_IEEE80211_ADHOC;
  602                 break;
  603         case IEEE80211_M_AHDEMO:
  604                 /* should not come here */
  605                 break;
  606         case IEEE80211_M_HOSTAP:
  607                 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
  608                 break;
  609         case IEEE80211_M_MONITOR:
  610                 imr->ifm_active |= IFM_IEEE80211_MONITOR;
  611                 break;
  612         }
  613         switch (ic->ic_curmode) {
  614         case IEEE80211_MODE_11A:
  615                 imr->ifm_active |= IFM_IEEE80211_11A;
  616                 break;
  617         case IEEE80211_MODE_11B:
  618                 imr->ifm_active |= IFM_IEEE80211_11B;
  619                 break;
  620         case IEEE80211_MODE_11G:
  621                 imr->ifm_active |= IFM_IEEE80211_11G;
  622                 break;
  623         case IEEE80211_MODE_FH:
  624                 imr->ifm_active |= IFM_IEEE80211_FH;
  625                 break;
  626         case IEEE80211_MODE_TURBO:
  627                 imr->ifm_active |= IFM_IEEE80211_11A
  628                                 |  IFM_IEEE80211_TURBO;
  629                 break;
  630         }
  631 }
  632 
  633 void
  634 ieee80211_watchdog(struct ifnet *ifp)
  635 {
  636         struct ieee80211com *ic = (void *)ifp;
  637 
  638         if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0)
  639                 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
  640 
  641         if (ic->ic_mgt_timer != 0)
  642                 ifp->if_timer = 1;
  643 }
  644 
  645 /*
  646  * Mark the basic rates for the 11g rate table based on the
  647  * operating mode.  For real 11g we mark all the 11b rates
  648  * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
  649  * 11b rates.  There's also a pseudo 11a-mode used to mark only
  650  * the basic OFDM rates.
  651  */
  652 static void
  653 ieee80211_setbasicrates(struct ieee80211com *ic)
  654 {
  655         static const struct ieee80211_rateset basic[] = {
  656             { 0 },                              /* IEEE80211_MODE_AUTO */
  657             { 3, { 12, 24, 48 } },              /* IEEE80211_MODE_11A */
  658             { 2, { 2, 4 } },                    /* IEEE80211_MODE_11B */
  659             { 4, { 2, 4, 11, 22 } },            /* IEEE80211_MODE_11G */
  660             { 2, { 2, 4 } },                    /* IEEE80211_MODE_FH */
  661             { 0 },                              /* IEEE80211_MODE_TURBO */
  662         };
  663         enum ieee80211_phymode mode;
  664         struct ieee80211_rateset *rs;
  665         int i, j;
  666 
  667         for (mode = 0; mode < IEEE80211_MODE_MAX; mode++) {
  668                 rs = &ic->ic_sup_rates[mode];
  669                 for (i = 0; i < rs->rs_nrates; i++) {
  670                         rs->rs_rates[i] &= IEEE80211_RATE_VAL;
  671                         for (j = 0; j < basic[mode].rs_nrates; j++)
  672                                 if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
  673                                         rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
  674                                         break;
  675                                 }
  676                 }
  677         }
  678 }
  679 
  680 /*
  681  * Set the current phy mode and recalculate the active channel
  682  * set based on the available channels for this mode.  Also
  683  * select a new default/current channel if the current one is
  684  * inappropriate for this mode.
  685  */
  686 int
  687 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
  688 {
  689 #define N(a)    (sizeof(a) / sizeof(a[0]))
  690         static const u_int chanflags[] = {
  691                 0,                      /* IEEE80211_MODE_AUTO */
  692                 IEEE80211_CHAN_A,       /* IEEE80211_MODE_11A */
  693                 IEEE80211_CHAN_B,       /* IEEE80211_MODE_11B */
  694                 IEEE80211_CHAN_PUREG,   /* IEEE80211_MODE_11G */
  695                 IEEE80211_CHAN_FHSS,    /* IEEE80211_MODE_FH */
  696                 IEEE80211_CHAN_T,       /* IEEE80211_MODE_TURBO */
  697         };
  698         struct ieee80211_channel *c;
  699         u_int modeflags;
  700         int i;
  701 
  702         /* validate new mode */
  703         if ((ic->ic_modecaps & (1<<mode)) == 0) {
  704                 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
  705                         ("%s: mode %u not supported (caps 0x%x)\n",
  706                         __func__, mode, ic->ic_modecaps));
  707                 return EINVAL;
  708         }
  709 
  710         /*
  711          * Verify at least one channel is present in the available
  712          * channel list before committing to the new mode.
  713          */
  714         IASSERT(mode < N(chanflags), ("Unexpected mode %u", mode));
  715         modeflags = chanflags[mode];
  716         for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
  717                 c = &ic->ic_channels[i];
  718                 if (mode == IEEE80211_MODE_AUTO) {
  719                         /* ignore turbo channels for autoselect */
  720                         if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
  721                                 break;
  722                 } else {
  723                         if ((c->ic_flags & modeflags) == modeflags)
  724                                 break;
  725                 }
  726         }
  727         if (i > IEEE80211_CHAN_MAX) {
  728                 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
  729                         ("%s: no channels found for mode %u\n", __func__, mode));
  730                 return EINVAL;
  731         }
  732 
  733         /*
  734          * Calculate the active channel set.
  735          */
  736         memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
  737         for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
  738                 c = &ic->ic_channels[i];
  739                 if (mode == IEEE80211_MODE_AUTO) {
  740                         /* take anything but pure turbo channels */
  741                         if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
  742                                 setbit(ic->ic_chan_active, i);
  743                 } else {
  744                         if ((c->ic_flags & modeflags) == modeflags)
  745                                 setbit(ic->ic_chan_active, i);
  746                 }
  747         }
  748         /*
  749          * If no current/default channel is setup or the current
  750          * channel is wrong for the mode then pick the first
  751          * available channel from the active list.  This is likely
  752          * not the right one.
  753          */
  754         if (ic->ic_ibss_chan == NULL ||
  755             isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
  756                 for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
  757                         if (isset(ic->ic_chan_active, i)) {
  758                                 ic->ic_ibss_chan = &ic->ic_channels[i];
  759                                 break;
  760                         }
  761                 IASSERT(ic->ic_ibss_chan != NULL &&
  762                     isset(ic->ic_chan_active,
  763                         ieee80211_chan2ieee(ic, ic->ic_ibss_chan)),
  764                     ("Bad IBSS channel %u",
  765                      ieee80211_chan2ieee(ic, ic->ic_ibss_chan)));
  766         }
  767 
  768         /*
  769          * Set/reset state flags that influence beacon contents, etc.
  770          *
  771          * XXX what if we have stations already associated???
  772          * XXX probably not right for autoselect?
  773          *
  774          * Short preamble is not interoperable with legacy .11b
  775          * equipment, so it should not be the default for b or
  776          * mixed b/g networks. -dcy
  777          */
  778 #if 0
  779         if (ic->ic_caps & IEEE80211_C_SHPREAMBLE)
  780                 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
  781 #endif
  782         if (mode == IEEE80211_MODE_11G) {
  783                 if (ic->ic_caps & IEEE80211_C_SHSLOT)
  784                         ic->ic_flags |= IEEE80211_F_SHSLOT;
  785         } else
  786                 ic->ic_flags &= ~IEEE80211_F_SHSLOT;
  787 
  788         ic->ic_curmode = mode;
  789         return 0;
  790 #undef N
  791 }
  792 
  793 /*
  794  * Return the phy mode for with the specified channel so the
  795  * caller can select a rate set.  This is problematic and the
  796  * work here assumes how things work elsewhere in this code.
  797  *
  798  * XXX never returns turbo modes -dcy
  799  */
  800 enum ieee80211_phymode
  801 ieee80211_chan2mode(struct ieee80211com *ic, struct ieee80211_channel *chan)
  802 {
  803         /*
  804          * NB: this assumes the channel would not be supplied to us
  805          *     unless it was already compatible with the current mode.
  806          */
  807         if (ic->ic_curmode != IEEE80211_MODE_AUTO ||
  808             chan == IEEE80211_CHAN_ANYC)
  809                 return ic->ic_curmode;
  810         /*
  811          * In autoselect mode; deduce a mode based on the channel
  812          * characteristics.  We assume that turbo-only channels
  813          * are not considered when the channel set is constructed.
  814          */
  815         if (IEEE80211_IS_CHAN_5GHZ(chan))
  816                 return IEEE80211_MODE_11A;
  817         else if (IEEE80211_IS_CHAN_FHSS(chan))
  818                 return IEEE80211_MODE_FH;
  819         else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN))
  820                 return IEEE80211_MODE_11G;
  821         else
  822                 return IEEE80211_MODE_11B;
  823 }
  824 
  825 /*
  826  * convert IEEE80211 rate value to ifmedia subtype.
  827  * ieee80211 rate is in unit of 0.5Mbps.
  828  */
  829 int
  830 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
  831 {
  832 #define N(a)    (sizeof(a) / sizeof(a[0]))
  833         static const struct {
  834                 u_int   m;      /* rate + mode */
  835                 u_int   r;      /* if_media rate */
  836         } rates[] = {
  837                 {   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
  838                 {   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
  839                 {   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
  840                 {   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
  841                 {  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
  842                 {  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
  843                 {  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
  844                 {  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
  845                 {  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
  846                 {  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
  847                 {  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
  848                 {  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
  849                 {  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
  850                 {  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
  851                 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
  852                 {   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
  853                 {   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
  854                 {  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
  855                 {  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
  856                 {  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
  857                 {  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
  858                 {  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
  859                 {  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
  860                 {  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
  861                 {  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
  862                 {  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
  863                 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
  864                 /* NB: OFDM72 doesn't realy exist so we don't handle it */
  865         };
  866         u_int mask, i;
  867 
  868         mask = rate & IEEE80211_RATE_VAL;
  869         switch (mode) {
  870         case IEEE80211_MODE_11A:
  871         case IEEE80211_MODE_TURBO:
  872                 mask |= IFM_IEEE80211_11A;
  873                 break;
  874         case IEEE80211_MODE_11B:
  875                 mask |= IFM_IEEE80211_11B;
  876                 break;
  877         case IEEE80211_MODE_FH:
  878                 mask |= IFM_IEEE80211_FH;
  879                 break;
  880         case IEEE80211_MODE_AUTO:
  881                 /* NB: ic may be NULL for some drivers */
  882                 if (ic && ic->ic_phytype == IEEE80211_T_FH) {
  883                         mask |= IFM_IEEE80211_FH;
  884                         break;
  885                 }
  886                 /* NB: hack, 11g matches both 11b+11a rates */
  887                 /* fall thru... */
  888         case IEEE80211_MODE_11G:
  889                 mask |= IFM_IEEE80211_11G;
  890                 break;
  891         }
  892         for (i = 0; i < N(rates); i++)
  893                 if (rates[i].m == mask)
  894                         return rates[i].r;
  895         return IFM_AUTO;
  896 #undef N
  897 }
  898 
  899 int
  900 ieee80211_media2rate(int mword)
  901 {
  902 #define N(a)    (sizeof(a) / sizeof(a[0]))
  903         static const int ieeerates[] = {
  904                 -1,             /* IFM_AUTO */
  905                 0,              /* IFM_MANUAL */
  906                 0,              /* IFM_NONE */
  907                 2,              /* IFM_IEEE80211_FH1 */
  908                 4,              /* IFM_IEEE80211_FH2 */
  909                 4,              /* IFM_IEEE80211_DS2 */
  910                 11,             /* IFM_IEEE80211_DS5 */
  911                 22,             /* IFM_IEEE80211_DS11 */
  912                 2,              /* IFM_IEEE80211_DS1 */
  913                 44,             /* IFM_IEEE80211_DS22 */
  914                 12,             /* IFM_IEEE80211_OFDM6 */
  915                 18,             /* IFM_IEEE80211_OFDM9 */
  916                 24,             /* IFM_IEEE80211_OFDM12 */
  917                 36,             /* IFM_IEEE80211_OFDM18 */
  918                 48,             /* IFM_IEEE80211_OFDM24 */
  919                 72,             /* IFM_IEEE80211_OFDM36 */
  920                 96,             /* IFM_IEEE80211_OFDM48 */
  921                 108,            /* IFM_IEEE80211_OFDM54 */
  922                 144,            /* IFM_IEEE80211_OFDM72 */
  923         };
  924         return IFM_SUBTYPE(mword) < N(ieeerates) ?
  925                 ieeerates[IFM_SUBTYPE(mword)] : 0;
  926 #undef N
  927 }
  928 
  929 #ifdef __NetBSD__
  930 static void
  931 ieee80211_clean_all_nodes(int cache_size)
  932 {
  933         struct ieee80211com *ic;
  934         LIST_FOREACH(ic, &ieee80211com_head, ic_list) {
  935                 ic->ic_max_nnodes = cache_size;
  936                 ieee80211_clean_nodes(ic);
  937         }
  938 }
  939 
  940 /* TBD factor with sysctl_ath_verify. */
  941 static int
  942 sysctl_ieee80211_verify(SYSCTLFN_ARGS)
  943 {
  944         int error, t;
  945         struct sysctlnode node;
  946 
  947         node = *rnode;
  948         t = *(int*)rnode->sysctl_data;
  949         node.sysctl_data = &t;
  950         error = sysctl_lookup(SYSCTLFN_CALL(&node));
  951         if (error || newp == NULL)
  952                 return (error);
  953 
  954         if (node.sysctl_num == ieee80211_cache_size_nodenum) {
  955                 if (t < 0)
  956                         return (EINVAL);
  957 #ifdef IEEE80211_DEBUG
  958         } else if (node.sysctl_num != ieee80211_debug_nodenum)
  959 #else /* IEEE80211_DEBUG */
  960         } else
  961 #endif /* IEEE80211_DEBUG */
  962                 return (EINVAL);
  963 
  964         *(int*)rnode->sysctl_data = t;
  965 
  966         if (node.sysctl_num == ieee80211_cache_size_nodenum)
  967                 ieee80211_clean_all_nodes(t);
  968         return (0);
  969 }
  970 
  971 /*
  972  * Pointers for testing:
  973  *
  974  *      If there are no interfaces, or else no 802.11 interfaces,
  975  *      ieee80211_node_walkfirst must return NULL.
  976  *
  977  *      If there is any single 802.11 interface, ieee80211_node_walkfirst
  978  *      must not return NULL.
  979  */
  980 static struct ieee80211_node *
  981 ieee80211_node_walkfirst(struct ieee80211_node_walk *nw,
  982     u_short if_index)
  983 {
  984         struct ieee80211com *ic;
  985         (void)memset(nw, 0, sizeof(*nw));
  986 
  987         nw->nw_ifindex = if_index;
  988 
  989         LIST_FOREACH(ic, &ieee80211com_head, ic_list) {
  990                 if (if_index != 0 && ic->ic_if.if_index != if_index)
  991                         continue;
  992                 nw->nw_ic = ic;
  993                 nw->nw_ni = ic->ic_bss;
  994                 break;
  995         }
  996 
  997         KASSERT(LOGICALLY_EQUAL(nw->nw_ni == NULL, nw->nw_ic == NULL));
  998 
  999         return nw->nw_ni;
 1000 }
 1001 
 1002 static struct ieee80211_node *
 1003 ieee80211_node_walknext(struct ieee80211_node_walk *nw)
 1004 {
 1005         KASSERT(LOGICALLY_EQUAL(nw->nw_ni == NULL, nw->nw_ic == NULL));
 1006 
 1007         if (nw->nw_ic == NULL && nw->nw_ni == NULL)
 1008                 return NULL;
 1009 
 1010         if (nw->nw_ni == nw->nw_ic->ic_bss)
 1011                 nw->nw_ni = TAILQ_FIRST(&nw->nw_ic->ic_node);
 1012         else
 1013                 nw->nw_ni = TAILQ_NEXT(nw->nw_ni, ni_list);
 1014 
 1015         if (nw->nw_ni == NULL) {
 1016                 if (nw->nw_ifindex != 0)
 1017                         return NULL;
 1018 
 1019                 nw->nw_ic = LIST_NEXT(nw->nw_ic, ic_list);
 1020                 if (nw->nw_ic == NULL)
 1021                         return NULL;
 1022 
 1023                 nw->nw_ni = nw->nw_ic->ic_bss;
 1024         }
 1025 
 1026         KASSERT(LOGICALLY_EQUAL(nw->nw_ni == NULL, nw->nw_ic == NULL));
 1027 
 1028         return nw->nw_ni;
 1029 }
 1030 
 1031 static void
 1032 sysctl_ieee80211_fill_node(struct ieee80211_node *ni,
 1033     struct ieee80211_node_sysctl *ns, int ifindex,
 1034     struct ieee80211_channel *chan0, int is_bss)
 1035 {
 1036         ns->ns_ifindex = ifindex;
 1037         ns->ns_capinfo = ni->ni_capinfo;
 1038         ns->ns_flags = (is_bss) ? IEEE80211_NODE_SYSCTL_F_BSS : 0;
 1039         (void)memcpy(ns->ns_macaddr, ni->ni_macaddr, sizeof(ns->ns_macaddr));
 1040         (void)memcpy(ns->ns_bssid, ni->ni_bssid, sizeof(ns->ns_bssid));
 1041         if (ni->ni_chan != IEEE80211_CHAN_ANYC) {
 1042                 ns->ns_freq = ni->ni_chan->ic_freq;
 1043                 ns->ns_chanflags = ni->ni_chan->ic_flags;
 1044                 ns->ns_chanidx = ni->ni_chan - chan0;
 1045         } else {
 1046                 ns->ns_freq = ns->ns_chanflags = 0;
 1047                 ns->ns_chanidx = 0;
 1048         }
 1049         ns->ns_rssi = ni->ni_rssi;
 1050         ns->ns_esslen = ni->ni_esslen;
 1051         (void)memcpy(ns->ns_essid, ni->ni_essid, sizeof(ns->ns_essid));
 1052         ns->ns_pwrsave = ni->ni_pwrsave;
 1053         ns->ns_erp = ni->ni_erp;
 1054         ns->ns_associd = ni->ni_associd;
 1055         ns->ns_inact = ni->ni_inact * IEEE80211_INACT_WAIT;
 1056         ns->ns_rstamp = ni->ni_rstamp;
 1057         ns->ns_rates = ni->ni_rates;
 1058         ns->ns_txrate = ni->ni_txrate;
 1059         ns->ns_intval = ni->ni_intval;
 1060         (void)memcpy(ns->ns_tstamp, ni->ni_tstamp, sizeof(ns->ns_tstamp));
 1061         ns->ns_txseq = ni->ni_txseq;
 1062         ns->ns_rxseq = ni->ni_rxseq;
 1063         ns->ns_fhdwell = ni->ni_fhdwell;
 1064         ns->ns_fhindex = ni->ni_fhindex;
 1065         ns->ns_fails = ni->ni_fails;
 1066 }
 1067 
 1068 /* Between two examinations of the sysctl tree, I expect each
 1069  * interface to add no more than 5 nodes.
 1070  */
 1071 #define IEEE80211_SYSCTL_NODE_GROWTH    5
 1072 
 1073 static int
 1074 sysctl_ieee80211_node(SYSCTLFN_ARGS)
 1075 {
 1076         struct ieee80211_node_walk nw;
 1077         struct ieee80211_node *ni;
 1078         struct ieee80211_node_sysctl ns;
 1079         char *dp;
 1080         u_int cur_ifindex, ifcount, ifindex, last_ifindex, op, arg, hdr_type;
 1081         size_t len, needed, eltsize, out_size;
 1082         int error, s, nelt;
 1083 
 1084         if (namelen == 1 && name[0] == CTL_QUERY)
 1085                 return (sysctl_query(SYSCTLFN_CALL(rnode)));
 1086 
 1087         if (namelen != IEEE80211_SYSCTL_NODENAMELEN)
 1088                 return (EINVAL);
 1089 
 1090         /* ifindex.op.arg.header-type.eltsize.nelt */
 1091         dp = oldp;
 1092         len = (oldp != NULL) ? *oldlenp : 0;
 1093         ifindex = name[IEEE80211_SYSCTL_NODENAME_IF];
 1094         op = name[IEEE80211_SYSCTL_NODENAME_OP];
 1095         arg = name[IEEE80211_SYSCTL_NODENAME_ARG];
 1096         hdr_type = name[IEEE80211_SYSCTL_NODENAME_TYPE];
 1097         eltsize = name[IEEE80211_SYSCTL_NODENAME_ELTSIZE];
 1098         nelt = name[IEEE80211_SYSCTL_NODENAME_ELTCOUNT];
 1099         out_size = MIN(sizeof(ns), eltsize);
 1100 
 1101         if (op != IEEE80211_SYSCTL_OP_ALL || arg != 0 ||
 1102             hdr_type != IEEE80211_SYSCTL_T_NODE || eltsize < 1 || nelt < 0)
 1103                 return (EINVAL);
 1104 
 1105         error = 0;
 1106         needed = 0;
 1107         ifcount = 0;
 1108         last_ifindex = 0;
 1109 
 1110         s = splnet();
 1111 
 1112         for (ni = ieee80211_node_walkfirst(&nw, ifindex); ni != NULL;
 1113              ni = ieee80211_node_walknext(&nw)) {
 1114                 struct ieee80211com *ic;
 1115 
 1116                 ic = nw.nw_ic;
 1117                 cur_ifindex = ic->ic_if.if_index;
 1118 
 1119                 if (cur_ifindex != last_ifindex) {
 1120                         ifcount++;
 1121                         last_ifindex = cur_ifindex;
 1122                 }
 1123 
 1124                 if (nelt <= 0)
 1125                         continue;
 1126 
 1127                 if (len >= eltsize) {
 1128                         sysctl_ieee80211_fill_node(ni, &ns, cur_ifindex,
 1129                             &ic->ic_channels[0], ni == ic->ic_bss);
 1130                         error = copyout(&ns, dp, out_size);
 1131                         if (error)
 1132                                 goto cleanup;
 1133                         dp += eltsize;
 1134                         len -= eltsize;
 1135                 }
 1136                 needed += eltsize;
 1137                 if (nelt != INT_MAX)
 1138                         nelt--;
 1139         }
 1140 cleanup:
 1141         splx(s);
 1142 
 1143         *oldlenp = needed;
 1144         if (oldp == NULL)
 1145                 *oldlenp += ifcount * IEEE80211_SYSCTL_NODE_GROWTH * eltsize;
 1146 
 1147         return (error);
 1148 }
 1149 
 1150 /*
 1151  * Setup sysctl(3) MIB, net.ieee80211.*
 1152  *
 1153  * TBD condition CTLFLAG_PERMANENT on being an LKM or not
 1154  */
 1155 SYSCTL_SETUP(sysctl_ieee80211, "sysctl ieee80211 subtree setup")
 1156 {
 1157         int rc;
 1158         struct sysctlnode *cnode, *rnode;
 1159 
 1160         if ((rc = sysctl_createv(clog, 0, NULL, &rnode,
 1161             CTLFLAG_PERMANENT, CTLTYPE_NODE, "net", NULL,
 1162             NULL, 0, NULL, 0, CTL_NET, CTL_EOL)) != 0)
 1163                 goto err;
 1164 
 1165         if ((rc = sysctl_createv(clog, 0, &rnode, &rnode,
 1166             CTLFLAG_PERMANENT, CTLTYPE_NODE, "link",
 1167             "link-layer statistics and controls",
 1168             NULL, 0, NULL, 0, PF_LINK, CTL_EOL)) != 0)
 1169                 goto err;
 1170 
 1171         if ((rc = sysctl_createv(clog, 0, &rnode, &rnode,
 1172             CTLFLAG_PERMANENT, CTLTYPE_NODE, "ieee80211",
 1173             "IEEE 802.11 WLAN statistics and controls",
 1174             NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
 1175                 goto err;
 1176 
 1177         if ((rc = sysctl_createv(clog, 0, &rnode, NULL,
 1178             CTLFLAG_PERMANENT, CTLTYPE_NODE, "nodes", "client/peer stations",
 1179             sysctl_ieee80211_node, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
 1180                 goto err;
 1181 
 1182 #ifdef IEEE80211_DEBUG
 1183 
 1184         /* control debugging printfs */
 1185         if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
 1186             CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
 1187             "debug", SYSCTL_DESCR("Enable IEEE 802.11 debugging output"),
 1188             sysctl_ieee80211_verify, 0, &ieee80211_debug, 0,
 1189             CTL_CREATE, CTL_EOL)) != 0)
 1190                 goto err;
 1191 
 1192         ieee80211_debug_nodenum = cnode->sysctl_num;
 1193 
 1194 #endif /* IEEE80211_DEBUG */
 1195 
 1196         /* control LRU cache size */
 1197         if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
 1198             CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
 1199             "maxnodecache", SYSCTL_DESCR("Maximum station cache size"),
 1200             sysctl_ieee80211_verify, 0, &ieee80211_cache_size,
 1201             0, CTL_CREATE, CTL_EOL)) != 0)
 1202                 goto err;
 1203 
 1204         ieee80211_cache_size_nodenum = cnode->sysctl_num;
 1205 
 1206         return;
 1207 err:
 1208         printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
 1209 }
 1210 #endif /* __NetBSD__ */
 1211 
 1212 #ifdef __FreeBSD__
 1213 /*
 1214  * Module glue.
 1215  *
 1216  * NB: the module name is "wlan" for compatibility with NetBSD.
 1217  */
 1218 
 1219 static int
 1220 ieee80211_modevent(module_t mod, int type, void *unused)
 1221 {
 1222         switch (type) {
 1223         case MOD_LOAD:
 1224                 if (bootverbose)
 1225                         printf("wlan: <802.11 Link Layer>\n");
 1226                 return 0;
 1227         case MOD_UNLOAD:
 1228                 return 0;
 1229         }
 1230         return EINVAL;
 1231 }
 1232 
 1233 static moduledata_t ieee80211_mod = {
 1234         "wlan",
 1235         ieee80211_modevent,
 1236         0
 1237 };
 1238 DECLARE_MODULE(wlan, ieee80211_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
 1239 MODULE_VERSION(wlan, 1);
 1240 MODULE_DEPEND(wlan, rc4, 1, 1, 1);
 1241 MODULE_DEPEND(wlan, ether, 1, 1, 1);
 1242 #endif

Cache object: 1baf25400e42bc90f8770f1edce77c48


[ 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.