Home | History | Annotate | Line # | Download | only in net80211
ieee80211.c revision 1.28
      1 /*	$NetBSD: ieee80211.c,v 1.28 2004/07/23 10:15:13 mycroft 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.28 2004/07/23 10:15:13 mycroft 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_inact_max = IEEE80211_INACT_MAX;
    106 static int ieee80211_inact_max_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 	if (ic->ic_inact_timer && --ic->ic_inact_timer == 0)
    641 		ieee80211_timeout_nodes(ic);
    642 
    643 	if (ic->ic_mgt_timer != 0 || ic->ic_inact_timer != 0)
    644 		ifp->if_timer = 1;
    645 }
    646 
    647 /*
    648  * Mark the basic rates for the 11g rate table based on the
    649  * operating mode.  For real 11g we mark all the 11b rates
    650  * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
    651  * 11b rates.  There's also a pseudo 11a-mode used to mark only
    652  * the basic OFDM rates.
    653  */
    654 static void
    655 ieee80211_setbasicrates(struct ieee80211com *ic)
    656 {
    657 	static const struct ieee80211_rateset basic[] = {
    658 	    { 0 },				/* IEEE80211_MODE_AUTO */
    659 	    { 3, { 12, 24, 48 } },		/* IEEE80211_MODE_11A */
    660 	    { 2, { 2, 4 } },			/* IEEE80211_MODE_11B */
    661 	    { 7, { 2, 4, 11, 22, 12, 24, 48 } },/* IEEE80211_MODE_11G */
    662 	    { 2, { 2, 4 } },			/* IEEE80211_MODE_FH */
    663 	    { 0 },				/* IEEE80211_MODE_TURBO	*/
    664 	};
    665 	enum ieee80211_phymode mode;
    666 	struct ieee80211_rateset *rs;
    667 	int i, j;
    668 
    669 	for (mode = 0; mode < IEEE80211_MODE_MAX; mode++) {
    670 		rs = &ic->ic_sup_rates[mode];
    671 		for (i = 0; i < rs->rs_nrates; i++) {
    672 			rs->rs_rates[i] &= IEEE80211_RATE_VAL;
    673 			for (j = 0; j < basic[mode].rs_nrates; j++)
    674 				if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
    675 					rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
    676 					break;
    677 				}
    678 		}
    679 	}
    680 }
    681 
    682 /*
    683  * Set the current phy mode and recalculate the active channel
    684  * set based on the available channels for this mode.  Also
    685  * select a new default/current channel if the current one is
    686  * inappropriate for this mode.
    687  */
    688 int
    689 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
    690 {
    691 #define	N(a)	(sizeof(a) / sizeof(a[0]))
    692 	static const u_int chanflags[] = {
    693 		0,			/* IEEE80211_MODE_AUTO */
    694 		IEEE80211_CHAN_A,	/* IEEE80211_MODE_11A */
    695 		IEEE80211_CHAN_B,	/* IEEE80211_MODE_11B */
    696 		IEEE80211_CHAN_PUREG,	/* IEEE80211_MODE_11G */
    697 		IEEE80211_CHAN_FHSS,	/* IEEE80211_MODE_FH */
    698 		IEEE80211_CHAN_T,	/* IEEE80211_MODE_TURBO	*/
    699 	};
    700 	struct ieee80211_channel *c;
    701 	u_int modeflags;
    702 	int i;
    703 
    704 	/* validate new mode */
    705 	if ((ic->ic_modecaps & (1<<mode)) == 0) {
    706 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
    707 			("%s: mode %u not supported (caps 0x%x)\n",
    708 			__func__, mode, ic->ic_modecaps));
    709 		return EINVAL;
    710 	}
    711 
    712 	/*
    713 	 * Verify at least one channel is present in the available
    714 	 * channel list before committing to the new mode.
    715 	 */
    716 	IASSERT(mode < N(chanflags), ("Unexpected mode %u", mode));
    717 	modeflags = chanflags[mode];
    718 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
    719 		c = &ic->ic_channels[i];
    720 		if (mode == IEEE80211_MODE_AUTO) {
    721 			/* ignore turbo channels for autoselect */
    722 			if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
    723 				break;
    724 		} else {
    725 			if ((c->ic_flags & modeflags) == modeflags)
    726 				break;
    727 		}
    728 	}
    729 	if (i > IEEE80211_CHAN_MAX) {
    730 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
    731 			("%s: no channels found for mode %u\n", __func__, mode));
    732 		return EINVAL;
    733 	}
    734 
    735 	/*
    736 	 * Calculate the active channel set.
    737 	 */
    738 	memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
    739 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
    740 		c = &ic->ic_channels[i];
    741 		if (mode == IEEE80211_MODE_AUTO) {
    742 			/* take anything but pure turbo channels */
    743 			if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
    744 				setbit(ic->ic_chan_active, i);
    745 		} else {
    746 			if ((c->ic_flags & modeflags) == modeflags)
    747 				setbit(ic->ic_chan_active, i);
    748 		}
    749 	}
    750 	/*
    751 	 * If no current/default channel is setup or the current
    752 	 * channel is wrong for the mode then pick the first
    753 	 * available channel from the active list.  This is likely
    754 	 * not the right one.
    755 	 */
    756 	if (ic->ic_ibss_chan == NULL ||
    757 	    isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
    758 		for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
    759 			if (isset(ic->ic_chan_active, i)) {
    760 				ic->ic_ibss_chan = &ic->ic_channels[i];
    761 				break;
    762 			}
    763 		IASSERT(ic->ic_ibss_chan != NULL &&
    764 		    isset(ic->ic_chan_active,
    765 			ieee80211_chan2ieee(ic, ic->ic_ibss_chan)),
    766 		    ("Bad IBSS channel %u",
    767 		     ieee80211_chan2ieee(ic, ic->ic_ibss_chan)));
    768 	}
    769 
    770 	/*
    771 	 * Set/reset state flags that influence beacon contents, etc.
    772 	 *
    773 	 * XXX what if we have stations already associated???
    774 	 * XXX probably not right for autoselect?
    775 	 */
    776 	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE)
    777 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
    778 	if (mode == IEEE80211_MODE_11G) {
    779 		if (ic->ic_caps & IEEE80211_C_SHSLOT)
    780 			ic->ic_flags |= IEEE80211_F_SHSLOT;
    781 	} else
    782 		ic->ic_flags &= ~IEEE80211_F_SHSLOT;
    783 
    784 	ic->ic_curmode = mode;
    785 	return 0;
    786 #undef N
    787 }
    788 
    789 /*
    790  * Return the phy mode for with the specified channel so the
    791  * caller can select a rate set.  This is problematic and the
    792  * work here assumes how things work elsewhere in this code.
    793  *
    794  * XXX never returns turbo modes -dcy
    795  */
    796 enum ieee80211_phymode
    797 ieee80211_chan2mode(struct ieee80211com *ic, struct ieee80211_channel *chan)
    798 {
    799 	/*
    800 	 * NB: this assumes the channel would not be supplied to us
    801 	 *     unless it was already compatible with the current mode.
    802 	 */
    803 	if (ic->ic_curmode != IEEE80211_MODE_AUTO ||
    804 	    chan == IEEE80211_CHAN_ANYC)
    805 		return ic->ic_curmode;
    806 	/*
    807 	 * In autoselect mode; deduce a mode based on the channel
    808 	 * characteristics.  We assume that turbo-only channels
    809 	 * are not considered when the channel set is constructed.
    810 	 */
    811 	if (IEEE80211_IS_CHAN_5GHZ(chan))
    812 		return IEEE80211_MODE_11A;
    813 	else if (IEEE80211_IS_CHAN_FHSS(chan))
    814 		return IEEE80211_MODE_FH;
    815 	else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN))
    816 		return IEEE80211_MODE_11G;
    817 	else
    818 		return IEEE80211_MODE_11B;
    819 }
    820 
    821 /*
    822  * convert IEEE80211 rate value to ifmedia subtype.
    823  * ieee80211 rate is in unit of 0.5Mbps.
    824  */
    825 int
    826 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
    827 {
    828 #define	N(a)	(sizeof(a) / sizeof(a[0]))
    829 	static const struct {
    830 		u_int	m;	/* rate + mode */
    831 		u_int	r;	/* if_media rate */
    832 	} rates[] = {
    833 		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
    834 		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
    835 		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
    836 		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
    837 		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
    838 		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
    839 		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
    840 		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
    841 		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
    842 		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
    843 		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
    844 		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
    845 		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
    846 		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
    847 		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
    848 		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
    849 		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
    850 		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
    851 		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
    852 		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
    853 		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
    854 		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
    855 		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
    856 		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
    857 		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
    858 		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
    859 		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
    860 		/* NB: OFDM72 doesn't realy exist so we don't handle it */
    861 	};
    862 	u_int mask, i;
    863 
    864 	mask = rate & IEEE80211_RATE_VAL;
    865 	switch (mode) {
    866 	case IEEE80211_MODE_11A:
    867 	case IEEE80211_MODE_TURBO:
    868 		mask |= IFM_IEEE80211_11A;
    869 		break;
    870 	case IEEE80211_MODE_11B:
    871 		mask |= IFM_IEEE80211_11B;
    872 		break;
    873 	case IEEE80211_MODE_FH:
    874 		mask |= IFM_IEEE80211_FH;
    875 		break;
    876 	case IEEE80211_MODE_AUTO:
    877 		/* NB: ic may be NULL for some drivers */
    878 		if (ic && ic->ic_phytype == IEEE80211_T_FH) {
    879 			mask |= IFM_IEEE80211_FH;
    880 			break;
    881 		}
    882 		/* NB: hack, 11g matches both 11b+11a rates */
    883 		/* fall thru... */
    884 	case IEEE80211_MODE_11G:
    885 		mask |= IFM_IEEE80211_11G;
    886 		break;
    887 	}
    888 	for (i = 0; i < N(rates); i++)
    889 		if (rates[i].m == mask)
    890 			return rates[i].r;
    891 	return IFM_AUTO;
    892 #undef N
    893 }
    894 
    895 int
    896 ieee80211_media2rate(int mword)
    897 {
    898 #define	N(a)	(sizeof(a) / sizeof(a[0]))
    899 	static const int ieeerates[] = {
    900 		-1,		/* IFM_AUTO */
    901 		0,		/* IFM_MANUAL */
    902 		0,		/* IFM_NONE */
    903 		2,		/* IFM_IEEE80211_FH1 */
    904 		4,		/* IFM_IEEE80211_FH2 */
    905 		4,		/* IFM_IEEE80211_DS2 */
    906 		11,		/* IFM_IEEE80211_DS5 */
    907 		22,		/* IFM_IEEE80211_DS11 */
    908 		2,		/* IFM_IEEE80211_DS1 */
    909 		44,		/* IFM_IEEE80211_DS22 */
    910 		12,		/* IFM_IEEE80211_OFDM6 */
    911 		18,		/* IFM_IEEE80211_OFDM9 */
    912 		24,		/* IFM_IEEE80211_OFDM12 */
    913 		36,		/* IFM_IEEE80211_OFDM18 */
    914 		48,		/* IFM_IEEE80211_OFDM24 */
    915 		72,		/* IFM_IEEE80211_OFDM36 */
    916 		96,		/* IFM_IEEE80211_OFDM48 */
    917 		108,		/* IFM_IEEE80211_OFDM54 */
    918 		144,		/* IFM_IEEE80211_OFDM72 */
    919 	};
    920 	return IFM_SUBTYPE(mword) < N(ieeerates) ?
    921 		ieeerates[IFM_SUBTYPE(mword)] : 0;
    922 #undef N
    923 }
    924 
    925 #ifdef __NetBSD__
    926 /* TBD factor with sysctl_ath_verify. */
    927 static int
    928 sysctl_ieee80211_verify(SYSCTLFN_ARGS)
    929 {
    930 	int error, t;
    931 	struct sysctlnode node;
    932 
    933 	node = *rnode;
    934 	t = *(int*)rnode->sysctl_data;
    935 	node.sysctl_data = &t;
    936 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    937 	if (error || newp == NULL)
    938 		return (error);
    939 
    940 	if (node.sysctl_num == ieee80211_inact_max_nodenum) {
    941 		if (t < 1)
    942 			return (EINVAL);
    943 		t = roundup(t, IEEE80211_INACT_WAIT) / IEEE80211_INACT_WAIT;
    944 #ifdef IEEE80211_DEBUG
    945 	} else if (node.sysctl_num == ieee80211_debug_nodenum) {
    946 		if (t < 0 || t > 2)
    947 			return (EINVAL);
    948 #endif /* IEEE80211_DEBUG */
    949 	} else
    950 		return (EINVAL);
    951 
    952 	*(int*)rnode->sysctl_data = t;
    953 
    954 	return (0);
    955 }
    956 
    957 /*
    958  * Pointers for testing:
    959  *
    960  *	If there are no interfaces, or else no 802.11 interfaces,
    961  *	ieee80211_node_walkfirst must return NULL.
    962  *
    963  *	If there is any single 802.11 interface, ieee80211_node_walkfirst
    964  *	must not return NULL.
    965  */
    966 static struct ieee80211_node *
    967 ieee80211_node_walkfirst(struct ieee80211_node_walk *nw,
    968     u_short if_index)
    969 {
    970 	struct ieee80211com *ic;
    971 	(void)memset(nw, 0, sizeof(*nw));
    972 
    973 	nw->nw_ifindex = if_index;
    974 
    975 	LIST_FOREACH(ic, &ieee80211com_head, ic_list) {
    976 		if (if_index != 0 && ic->ic_if.if_index != if_index)
    977 			continue;
    978 		nw->nw_ic = ic;
    979 		nw->nw_ni = ic->ic_bss;
    980 		break;
    981 	}
    982 
    983 	KASSERT(LOGICALLY_EQUAL(nw->nw_ni == NULL, nw->nw_ic == NULL));
    984 
    985 	return nw->nw_ni;
    986 }
    987 
    988 static struct ieee80211_node *
    989 ieee80211_node_walknext(struct ieee80211_node_walk *nw)
    990 {
    991 	KASSERT(LOGICALLY_EQUAL(nw->nw_ni == NULL, nw->nw_ic == NULL));
    992 
    993 	if (nw->nw_ic == NULL && nw->nw_ni == NULL)
    994 		return NULL;
    995 
    996 	if (nw->nw_ni == nw->nw_ic->ic_bss)
    997 		nw->nw_ni = TAILQ_FIRST(&nw->nw_ic->ic_node);
    998 	else
    999 		nw->nw_ni = TAILQ_NEXT(nw->nw_ni, ni_list);
   1000 
   1001 	if (nw->nw_ni == NULL) {
   1002 		if (nw->nw_ifindex != 0)
   1003 			return NULL;
   1004 
   1005 		nw->nw_ic = LIST_NEXT(nw->nw_ic, ic_list);
   1006 		if (nw->nw_ic == NULL)
   1007 			return NULL;
   1008 
   1009 		nw->nw_ni = nw->nw_ic->ic_bss;
   1010 	}
   1011 
   1012 	KASSERT(LOGICALLY_EQUAL(nw->nw_ni == NULL, nw->nw_ic == NULL));
   1013 
   1014 	return nw->nw_ni;
   1015 }
   1016 
   1017 static void
   1018 sysctl_ieee80211_fill_node(struct ieee80211_node *ni,
   1019     struct ieee80211_node_sysctl *ns, int ifindex,
   1020     struct ieee80211_channel *chan0, int is_bss)
   1021 {
   1022 	ns->ns_ifindex = ifindex;
   1023 	ns->ns_capinfo = ni->ni_capinfo;
   1024 	ns->ns_flags = (is_bss) ? IEEE80211_NODE_SYSCTL_F_BSS : 0;
   1025 	(void)memcpy(ns->ns_macaddr, ni->ni_macaddr, sizeof(ns->ns_macaddr));
   1026 	(void)memcpy(ns->ns_bssid, ni->ni_bssid, sizeof(ns->ns_bssid));
   1027 	if (ni->ni_chan != IEEE80211_CHAN_ANYC) {
   1028 		ns->ns_freq = ni->ni_chan->ic_freq;
   1029 		ns->ns_chanflags = ni->ni_chan->ic_flags;
   1030 		ns->ns_chanidx = ni->ni_chan - chan0;
   1031 	} else {
   1032 		ns->ns_freq = ns->ns_chanflags = 0;
   1033 		ns->ns_chanidx = 0;
   1034 	}
   1035 	ns->ns_rssi = ni->ni_rssi;
   1036 	ns->ns_esslen = ni->ni_esslen;
   1037 	(void)memcpy(ns->ns_essid, ni->ni_essid, sizeof(ns->ns_essid));
   1038 	ns->ns_pwrsave = ni->ni_pwrsave;
   1039 	ns->ns_erp = ni->ni_erp;
   1040 	ns->ns_associd = ni->ni_associd;
   1041 	ns->ns_inact = ni->ni_inact * IEEE80211_INACT_WAIT;
   1042 	ns->ns_rstamp = ni->ni_rstamp;
   1043 	ns->ns_rates = ni->ni_rates;
   1044 	ns->ns_txrate = ni->ni_txrate;
   1045 	ns->ns_intval = ni->ni_intval;
   1046 	(void)memcpy(ns->ns_tstamp, ni->ni_tstamp, sizeof(ns->ns_tstamp));
   1047 	ns->ns_txseq = ni->ni_txseq;
   1048 	ns->ns_rxseq = ni->ni_rxseq;
   1049 	ns->ns_fhdwell = ni->ni_fhdwell;
   1050 	ns->ns_fhindex = ni->ni_fhindex;
   1051 	ns->ns_fails = ni->ni_fails;
   1052 }
   1053 
   1054 /* Between two examinations of the sysctl tree, I expect each
   1055  * interface to add no more than 5 nodes.
   1056  */
   1057 #define IEEE80211_SYSCTL_NODE_GROWTH	5
   1058 
   1059 static int
   1060 sysctl_ieee80211_node(SYSCTLFN_ARGS)
   1061 {
   1062 	struct ieee80211_node_walk nw;
   1063 	struct ieee80211_node *ni;
   1064 	struct ieee80211_node_sysctl ns;
   1065 	char *dp;
   1066 	u_int cur_ifindex, ifcount, ifindex, last_ifindex, op, arg, hdr_type;
   1067 	size_t len, needed, eltsize, out_size;
   1068 	int error, s, nelt;
   1069 
   1070 	if (namelen == 1 && name[0] == CTL_QUERY)
   1071 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   1072 
   1073 	if (namelen != IEEE80211_SYSCTL_NODENAMELEN)
   1074 		return (EINVAL);
   1075 
   1076 	/* ifindex.op.arg.header-type.eltsize.nelt */
   1077 	dp = oldp;
   1078 	len = (oldp != NULL) ? *oldlenp : 0;
   1079 	ifindex = name[IEEE80211_SYSCTL_NODENAME_IF];
   1080 	op = name[IEEE80211_SYSCTL_NODENAME_OP];
   1081 	arg = name[IEEE80211_SYSCTL_NODENAME_ARG];
   1082 	hdr_type = name[IEEE80211_SYSCTL_NODENAME_TYPE];
   1083 	eltsize = name[IEEE80211_SYSCTL_NODENAME_ELTSIZE];
   1084 	nelt = name[IEEE80211_SYSCTL_NODENAME_ELTCOUNT];
   1085 	out_size = MIN(sizeof(ns), eltsize);
   1086 
   1087 	if (op != IEEE80211_SYSCTL_OP_ALL || arg != 0 ||
   1088 	    hdr_type != IEEE80211_SYSCTL_T_NODE || eltsize < 1 || nelt < 0)
   1089 		return (EINVAL);
   1090 
   1091 	error = 0;
   1092 	needed = 0;
   1093 	ifcount = 0;
   1094 	last_ifindex = 0;
   1095 
   1096 	s = splnet();
   1097 
   1098 	for (ni = ieee80211_node_walkfirst(&nw, ifindex); ni != NULL;
   1099 	     ni = ieee80211_node_walknext(&nw)) {
   1100 		struct ieee80211com *ic;
   1101 
   1102 		ic = nw.nw_ic;
   1103 		cur_ifindex = ic->ic_if.if_index;
   1104 
   1105 		if (cur_ifindex != last_ifindex) {
   1106 			ifcount++;
   1107 			last_ifindex = cur_ifindex;
   1108 		}
   1109 
   1110 		if (nelt <= 0)
   1111 			continue;
   1112 
   1113 		if (len >= eltsize) {
   1114 			sysctl_ieee80211_fill_node(ni, &ns, cur_ifindex,
   1115 			    &ic->ic_channels[0], ni == ic->ic_bss);
   1116 			error = copyout(&ns, dp, out_size);
   1117 			if (error)
   1118 				goto cleanup;
   1119 			dp += eltsize;
   1120 			len -= eltsize;
   1121 		}
   1122 		needed += eltsize;
   1123 		if (nelt != INT_MAX)
   1124 			nelt--;
   1125 	}
   1126 cleanup:
   1127 	splx(s);
   1128 
   1129 	*oldlenp = needed;
   1130 	if (oldp == NULL)
   1131 		*oldlenp += ifcount * IEEE80211_SYSCTL_NODE_GROWTH * eltsize;
   1132 
   1133 	return (error);
   1134 }
   1135 
   1136 /*
   1137  * Setup sysctl(3) MIB, net.ieee80211.*
   1138  *
   1139  * TBD condition CTLFLAG_PERMANENT on being an LKM or not
   1140  */
   1141 SYSCTL_SETUP(sysctl_ieee80211, "sysctl ieee80211 subtree setup")
   1142 {
   1143 	int rc;
   1144 	struct sysctlnode *cnode, *rnode;
   1145 
   1146 	if ((rc = sysctl_createv(clog, 0, NULL, &rnode,
   1147 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "net", NULL,
   1148 	    NULL, 0, NULL, 0, CTL_NET, CTL_EOL)) != 0)
   1149 		goto err;
   1150 
   1151 	if ((rc = sysctl_createv(clog, 0, &rnode, &rnode,
   1152 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "link",
   1153 	    "link-layer statistics and controls",
   1154 	    NULL, 0, NULL, 0, PF_LINK, CTL_EOL)) != 0)
   1155 		goto err;
   1156 
   1157 	if ((rc = sysctl_createv(clog, 0, &rnode, &rnode,
   1158 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "ieee80211",
   1159 	    "IEEE 802.11 WLAN statistics and controls",
   1160 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
   1161 		goto err;
   1162 
   1163 	if ((rc = sysctl_createv(clog, 0, &rnode, NULL,
   1164 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "nodes", "client/peer stations",
   1165 	    sysctl_ieee80211_node, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
   1166 		goto err;
   1167 
   1168 #ifdef IEEE80211_DEBUG
   1169 
   1170 	/* control debugging printfs */
   1171 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
   1172 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
   1173 	    "debug", SYSCTL_DESCR("Enable IEEE 802.11 debugging output"),
   1174 	    sysctl_ieee80211_verify, 0, &ieee80211_debug, 0,
   1175 	    CTL_CREATE, CTL_EOL)) != 0)
   1176 		goto err;
   1177 
   1178 	ieee80211_debug_nodenum = cnode->sysctl_num;
   1179 
   1180 #endif /* IEEE80211_DEBUG */
   1181 
   1182 	/* control inactivity timer */
   1183 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
   1184 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
   1185 	    "maxinact", SYSCTL_DESCR("Station inactivity timeout"),
   1186 	    sysctl_ieee80211_verify, 0, &ieee80211_inact_max,
   1187 	    0, CTL_CREATE, CTL_EOL)) != 0)
   1188 		goto err;
   1189 
   1190 	ieee80211_inact_max_nodenum = cnode->sysctl_num;
   1191 
   1192 	return;
   1193 err:
   1194 	printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
   1195 }
   1196 #endif /* __NetBSD__ */
   1197 
   1198 #ifdef __FreeBSD__
   1199 /*
   1200  * Module glue.
   1201  *
   1202  * NB: the module name is "wlan" for compatibility with NetBSD.
   1203  */
   1204 
   1205 static int
   1206 ieee80211_modevent(module_t mod, int type, void *unused)
   1207 {
   1208 	switch (type) {
   1209 	case MOD_LOAD:
   1210 		if (bootverbose)
   1211 			printf("wlan: <802.11 Link Layer>\n");
   1212 		return 0;
   1213 	case MOD_UNLOAD:
   1214 		return 0;
   1215 	}
   1216 	return EINVAL;
   1217 }
   1218 
   1219 static moduledata_t ieee80211_mod = {
   1220 	"wlan",
   1221 	ieee80211_modevent,
   1222 	0
   1223 };
   1224 DECLARE_MODULE(wlan, ieee80211_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
   1225 MODULE_VERSION(wlan, 1);
   1226 MODULE_DEPEND(wlan, rc4, 1, 1, 1);
   1227 MODULE_DEPEND(wlan, ether, 1, 1, 1);
   1228 #endif
   1229