Home | History | Annotate | Line # | Download | only in net80211
ieee80211.c revision 1.22
      1 /*	$NetBSD: ieee80211.c,v 1.22 2004/07/16 03:02:41 dyoung 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.22 2004/07/16 03:02:41 dyoung 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_set11gbasicrates(struct ieee80211_rateset *,
    112 		enum ieee80211_phymode);
    113 
    114 #ifdef __NetBSD__
    115 static void sysctl_ieee80211_fill_node(struct ieee80211_node *,
    116     struct ieee80211_node_sysctl *, int, struct ieee80211_channel *, int);
    117 static struct ieee80211_node *ieee80211_node_walknext(
    118     struct ieee80211_node_walk *);
    119 static struct ieee80211_node *ieee80211_node_walkfirst(
    120     struct ieee80211_node_walk *, u_short);
    121 static int sysctl_ieee80211_verify(SYSCTLFN_ARGS);
    122 static int sysctl_ieee80211_node(SYSCTLFN_ARGS);
    123 #endif /* __NetBSD__ */
    124 
    125 #define	LOGICALLY_EQUAL(x, y)	(!(x) == !(y))
    126 
    127 static const char *ieee80211_phymode_name[] = {
    128 	"auto",		/* IEEE80211_MODE_AUTO */
    129 	"11a",		/* IEEE80211_MODE_11A */
    130 	"11b",		/* IEEE80211_MODE_11B */
    131 	"11g",		/* IEEE80211_MODE_11G */
    132 	"FH",		/* IEEE80211_MODE_FH */
    133 	"turbo",	/* IEEE80211_MODE_TURBO */
    134 };
    135 
    136 void
    137 ieee80211_ifattach(struct ifnet *ifp)
    138 {
    139 	struct ieee80211com *ic = (void *)ifp;
    140 	struct ieee80211_channel *c;
    141 	int i;
    142 
    143 	ether_ifattach(ifp, ic->ic_myaddr);
    144 #if NBPFILTER > 0
    145 	bpfattach2(ifp, DLT_IEEE802_11,
    146 	    sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf);
    147 #endif
    148 	ieee80211_crypto_attach(ifp);
    149 
    150 	/*
    151 	 * Fill in 802.11 available channel set, mark
    152 	 * all available channels as active, and pick
    153 	 * a default channel if not already specified.
    154 	 */
    155 	memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
    156 	ic->ic_modecaps |= 1<<IEEE80211_MODE_AUTO;
    157 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
    158 		c = &ic->ic_channels[i];
    159 		if (c->ic_flags) {
    160 			/*
    161 			 * Verify driver passed us valid data.
    162 			 */
    163 			if (i != ieee80211_chan2ieee(ic, c)) {
    164 				if_printf(ifp, "bad channel ignored; "
    165 					"freq %u flags %x number %u\n",
    166 					c->ic_freq, c->ic_flags, i);
    167 				c->ic_flags = 0;	/* NB: remove */
    168 				continue;
    169 			}
    170 			setbit(ic->ic_chan_avail, i);
    171 			/*
    172 			 * Identify mode capabilities.
    173 			 */
    174 			if (IEEE80211_IS_CHAN_A(c))
    175 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11A;
    176 			if (IEEE80211_IS_CHAN_B(c))
    177 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11B;
    178 			if (IEEE80211_IS_CHAN_PUREG(c))
    179 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11G;
    180 			if (IEEE80211_IS_CHAN_FHSS(c))
    181 				ic->ic_modecaps |= 1<<IEEE80211_MODE_FH;
    182 			if (IEEE80211_IS_CHAN_T(c))
    183 				ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO;
    184 		}
    185 	}
    186 	/* validate ic->ic_curmode */
    187 	if ((ic->ic_modecaps & (1<<ic->ic_curmode)) == 0)
    188 		ic->ic_curmode = IEEE80211_MODE_AUTO;
    189 	ic->ic_des_chan = IEEE80211_CHAN_ANYC;	/* any channel is ok */
    190 
    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(ifp);
    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(ifp);
    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(ifp);
    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 
    403 	if (maxrate)
    404 		ifp->if_baudrate = IF_Mbps(maxrate);
    405 
    406 	if (ic->ic_max_aid == 0)
    407 		ic->ic_max_aid = IEEE80211_MAX_AID;
    408 
    409 #undef ADD
    410 }
    411 
    412 static int
    413 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
    414 {
    415 #define	IEEERATE(_ic,_m,_i) \
    416 	((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
    417 	int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
    418 	for (i = 0; i < nrates; i++)
    419 		if (IEEERATE(ic, mode, i) == rate)
    420 			return i;
    421 	return -1;
    422 #undef IEEERATE
    423 }
    424 
    425 /*
    426  * Handle a media change request.
    427  */
    428 int
    429 ieee80211_media_change(struct ifnet *ifp)
    430 {
    431 	struct ieee80211com *ic = (void *)ifp;
    432 	struct ifmedia_entry *ime;
    433 	enum ieee80211_opmode newopmode;
    434 	enum ieee80211_phymode newphymode;
    435 	int i, j, newrate, error = 0;
    436 
    437 	ime = ic->ic_media.ifm_cur;
    438 	/*
    439 	 * First, identify the phy mode.
    440 	 */
    441 	switch (IFM_MODE(ime->ifm_media)) {
    442 	case IFM_IEEE80211_11A:
    443 		newphymode = IEEE80211_MODE_11A;
    444 		break;
    445 	case IFM_IEEE80211_11B:
    446 		newphymode = IEEE80211_MODE_11B;
    447 		break;
    448 	case IFM_IEEE80211_11G:
    449 		newphymode = IEEE80211_MODE_11G;
    450 		break;
    451 	case IFM_IEEE80211_FH:
    452 		newphymode = IEEE80211_MODE_FH;
    453 		break;
    454 	case IFM_AUTO:
    455 		newphymode = IEEE80211_MODE_AUTO;
    456 		break;
    457 	default:
    458 		return EINVAL;
    459 	}
    460 	/*
    461 	 * Turbo mode is an ``option''.  Eventually it
    462 	 * needs to be applied to 11g too.
    463 	 */
    464 	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
    465 		if (newphymode != IEEE80211_MODE_11A)
    466 			return EINVAL;
    467 		newphymode = IEEE80211_MODE_TURBO;
    468 	}
    469 	/*
    470 	 * Validate requested mode is available.
    471 	 */
    472 	if ((ic->ic_modecaps & (1<<newphymode)) == 0)
    473 		return EINVAL;
    474 
    475 	/*
    476 	 * Next, the fixed/variable rate.
    477 	 */
    478 	i = -1;
    479 	if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
    480 		/*
    481 		 * Convert media subtype to rate.
    482 		 */
    483 		newrate = ieee80211_media2rate(ime->ifm_media);
    484 		if (newrate == 0)
    485 			return EINVAL;
    486 		/*
    487 		 * Check the rate table for the specified/current phy.
    488 		 */
    489 		if (newphymode == IEEE80211_MODE_AUTO) {
    490 			/*
    491 			 * In autoselect mode search for the rate.
    492 			 */
    493 			for (j = IEEE80211_MODE_11A;
    494 			     j < IEEE80211_MODE_MAX; j++) {
    495 				if ((ic->ic_modecaps & (1<<j)) == 0)
    496 					continue;
    497 				i = findrate(ic, j, newrate);
    498 				if (i != -1) {
    499 					/* lock mode too */
    500 					newphymode = j;
    501 					break;
    502 				}
    503 			}
    504 		} else {
    505 			i = findrate(ic, newphymode, newrate);
    506 		}
    507 		if (i == -1)			/* mode/rate mismatch */
    508 			return EINVAL;
    509 	}
    510 	/* NB: defer rate setting to later */
    511 
    512 	/*
    513 	 * Deduce new operating mode but don't install it just yet.
    514 	 */
    515 	if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) ==
    516 	    (IFM_IEEE80211_ADHOC|IFM_FLAG0))
    517 		newopmode = IEEE80211_M_AHDEMO;
    518 	else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
    519 		newopmode = IEEE80211_M_HOSTAP;
    520 	else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
    521 		newopmode = IEEE80211_M_IBSS;
    522 	else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
    523 		newopmode = IEEE80211_M_MONITOR;
    524 	else
    525 		newopmode = IEEE80211_M_STA;
    526 
    527 	/*
    528 	 * Autoselect doesn't make sense when operating as an AP.
    529 	 * If no phy mode has been selected, pick one and lock it
    530 	 * down so rate tables can be used in forming beacon frames
    531 	 * and the like.
    532 	 */
    533 	if (newopmode == IEEE80211_M_HOSTAP &&
    534 	    newphymode == IEEE80211_MODE_AUTO) {
    535 		for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++)
    536 			if (ic->ic_modecaps & (1<<j)) {
    537 				newphymode = j;
    538 				break;
    539 			}
    540 	}
    541 
    542 	/*
    543 	 * Handle phy mode change.
    544 	 */
    545 	if (ic->ic_curmode != newphymode) {		/* change phy mode */
    546 		error = ieee80211_setmode(ic, newphymode);
    547 		if (error != 0)
    548 			return error;
    549 		error = ENETRESET;
    550 	}
    551 
    552 	/*
    553 	 * Committed to changes, install the rate setting.
    554 	 */
    555 	if (ic->ic_fixed_rate != i) {
    556 		ic->ic_fixed_rate = i;			/* set fixed tx rate */
    557 		error = ENETRESET;
    558 	}
    559 
    560 	/*
    561 	 * Handle operating mode change.
    562 	 */
    563 	if (ic->ic_opmode != newopmode) {
    564 		ic->ic_opmode = newopmode;
    565 		switch (newopmode) {
    566 		case IEEE80211_M_AHDEMO:
    567 		case IEEE80211_M_HOSTAP:
    568 		case IEEE80211_M_STA:
    569 		case IEEE80211_M_MONITOR:
    570 			ic->ic_flags &= ~IEEE80211_F_IBSSON;
    571 			break;
    572 		case IEEE80211_M_IBSS:
    573 			ic->ic_flags |= IEEE80211_F_IBSSON;
    574 #ifdef notdef
    575 			if (ic->ic_curmode == IEEE80211_MODE_11G)
    576 				ieee80211_set11gbasicrates(
    577 					&ic->ic_sup_rates[newphymode],
    578 					IEEE80211_MODE_11B);
    579 #endif
    580 			break;
    581 		}
    582 		error = ENETRESET;
    583 	}
    584 #ifdef notdef
    585 	if (error == 0)
    586 		ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
    587 #endif
    588 	return error;
    589 }
    590 
    591 void
    592 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
    593 {
    594 	struct ieee80211com *ic = (void *)ifp;
    595 	struct ieee80211_node *ni = NULL;
    596 
    597 	imr->ifm_status = IFM_AVALID;
    598 	imr->ifm_active = IFM_IEEE80211;
    599 	if (ic->ic_state == IEEE80211_S_RUN)
    600 		imr->ifm_status |= IFM_ACTIVE;
    601 	imr->ifm_active |= IFM_AUTO;
    602 	switch (ic->ic_opmode) {
    603 	case IEEE80211_M_STA:
    604 		ni = ic->ic_bss;
    605 		/* calculate rate subtype */
    606 		imr->ifm_active |= ieee80211_rate2media(ic,
    607 			ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode);
    608 		break;
    609 	case IEEE80211_M_IBSS:
    610 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
    611 		break;
    612 	case IEEE80211_M_AHDEMO:
    613 		/* should not come here */
    614 		break;
    615 	case IEEE80211_M_HOSTAP:
    616 		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
    617 		break;
    618 	case IEEE80211_M_MONITOR:
    619 		imr->ifm_active |= IFM_IEEE80211_MONITOR;
    620 		break;
    621 	}
    622 	switch (ic->ic_curmode) {
    623 	case IEEE80211_MODE_11A:
    624 		imr->ifm_active |= IFM_IEEE80211_11A;
    625 		break;
    626 	case IEEE80211_MODE_11B:
    627 		imr->ifm_active |= IFM_IEEE80211_11B;
    628 		break;
    629 	case IEEE80211_MODE_11G:
    630 		imr->ifm_active |= IFM_IEEE80211_11G;
    631 		break;
    632 	case IEEE80211_MODE_FH:
    633 		imr->ifm_active |= IFM_IEEE80211_FH;
    634 		break;
    635 	case IEEE80211_MODE_TURBO:
    636 		imr->ifm_active |= IFM_IEEE80211_11A
    637 				|  IFM_IEEE80211_TURBO;
    638 		break;
    639 	}
    640 }
    641 
    642 void
    643 ieee80211_watchdog(struct ifnet *ifp)
    644 {
    645 	struct ieee80211com *ic = (void *)ifp;
    646 
    647 	if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0)
    648 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
    649 	if (ic->ic_inact_timer && --ic->ic_inact_timer == 0)
    650 		ieee80211_timeout_nodes(ic);
    651 
    652 	if (ic->ic_mgt_timer != 0 || ic->ic_inact_timer != 0)
    653 		ifp->if_timer = 1;
    654 }
    655 
    656 /*
    657  * Mark the basic rates for the 11g rate table based on the
    658  * operating mode.  For real 11g we mark all the 11b rates
    659  * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
    660  * 11b rates.  There's also a pseudo 11a-mode used to mark only
    661  * the basic OFDM rates.
    662  */
    663 static void
    664 ieee80211_set11gbasicrates(struct ieee80211_rateset *rs, enum ieee80211_phymode mode)
    665 {
    666 	static const struct ieee80211_rateset basic[] = {
    667 	    { 3, { 12, 24, 48 } },		/* IEEE80211_MODE_11A */
    668 	    { 4, { 2, 4, 11, 22 } },		/* IEEE80211_MODE_11B */
    669 	    { 7, { 2, 4, 11, 22, 12, 24, 48 } },/* IEEE80211_MODE_11G */
    670 	    { 0 },				/* IEEE80211_MODE_FH */
    671 	    { 0 },				/* IEEE80211_MODE_TURBO	*/
    672 	};
    673 	int i, j;
    674 
    675 	for (i = 0; i < rs->rs_nrates; i++) {
    676 		rs->rs_rates[i] &= IEEE80211_RATE_VAL;
    677 		for (j = 0; j < basic[mode].rs_nrates; j++)
    678 			if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
    679 				rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
    680 				break;
    681 			}
    682 	}
    683 }
    684 
    685 /*
    686  * Set the current phy mode and recalculate the active channel
    687  * set based on the available channels for this mode.  Also
    688  * select a new default/current channel if the current one is
    689  * inappropriate for this mode.
    690  */
    691 int
    692 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
    693 {
    694 #define	N(a)	(sizeof(a) / sizeof(a[0]))
    695 	static const u_int chanflags[] = {
    696 		0,			/* IEEE80211_MODE_AUTO */
    697 		IEEE80211_CHAN_A,	/* IEEE80211_MODE_11A */
    698 		IEEE80211_CHAN_B,	/* IEEE80211_MODE_11B */
    699 		IEEE80211_CHAN_PUREG,	/* IEEE80211_MODE_11G */
    700 		IEEE80211_CHAN_FHSS,	/* IEEE80211_MODE_FH */
    701 		IEEE80211_CHAN_T,	/* IEEE80211_MODE_TURBO	*/
    702 	};
    703 	struct ieee80211_channel *c;
    704 	u_int modeflags;
    705 	int i;
    706 
    707 	/* validate new mode */
    708 	if ((ic->ic_modecaps & (1<<mode)) == 0) {
    709 		IEEE80211_DPRINTF(("%s: mode %u not supported (caps 0x%x)\n",
    710 			__func__, mode, ic->ic_modecaps));
    711 		return EINVAL;
    712 	}
    713 
    714 	/*
    715 	 * Verify at least one channel is present in the available
    716 	 * channel list before committing to the new mode.
    717 	 */
    718 	IASSERT(mode < N(chanflags), ("Unexpected mode %u", mode));
    719 	modeflags = chanflags[mode];
    720 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
    721 		c = &ic->ic_channels[i];
    722 		if (mode == IEEE80211_MODE_AUTO) {
    723 			/* ignore turbo channels for autoselect */
    724 			if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
    725 				break;
    726 		} else {
    727 			if ((c->ic_flags & modeflags) == modeflags)
    728 				break;
    729 		}
    730 	}
    731 	if (i > IEEE80211_CHAN_MAX) {
    732 		IEEE80211_DPRINTF(("%s: no channels found for mode %u\n",
    733 			__func__, mode));
    734 		return EINVAL;
    735 	}
    736 
    737 	/*
    738 	 * Calculate the active channel set.
    739 	 */
    740 	memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
    741 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
    742 		c = &ic->ic_channels[i];
    743 		if (mode == IEEE80211_MODE_AUTO) {
    744 			/* take anything but pure turbo channels */
    745 			if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
    746 				setbit(ic->ic_chan_active, i);
    747 		} else {
    748 			if ((c->ic_flags & modeflags) == modeflags)
    749 				setbit(ic->ic_chan_active, i);
    750 		}
    751 	}
    752 	/*
    753 	 * If no current/default channel is setup or the current
    754 	 * channel is wrong for the mode then pick the first
    755 	 * available channel from the active list.  This is likely
    756 	 * not the right one.
    757 	 */
    758 	if (ic->ic_ibss_chan == NULL ||
    759 	    isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
    760 		for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
    761 			if (isset(ic->ic_chan_active, i)) {
    762 				ic->ic_ibss_chan = &ic->ic_channels[i];
    763 				break;
    764 			}
    765 		IASSERT(ic->ic_ibss_chan != NULL &&
    766 		    isset(ic->ic_chan_active,
    767 			ieee80211_chan2ieee(ic, ic->ic_ibss_chan)),
    768 		    ("Bad IBSS channel %u\n",
    769 		     ieee80211_chan2ieee(ic, ic->ic_ibss_chan)));
    770 	}
    771 
    772 	/*
    773 	 * Set/reset state flags that influence beacon contents, etc.
    774 	 *
    775 	 * XXX what if we have stations already associated???
    776 	 * XXX probably not right for autoselect?
    777 	 */
    778 	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE)
    779 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
    780 	if (mode == IEEE80211_MODE_11G) {
    781 		if (ic->ic_caps & IEEE80211_C_SHSLOT)
    782 			ic->ic_flags |= IEEE80211_F_SHSLOT;
    783 		ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode],
    784 			IEEE80211_MODE_11G);
    785 	} else {
    786 		ic->ic_flags &= ~IEEE80211_F_SHSLOT;
    787 	}
    788 
    789 	ic->ic_curmode = mode;
    790 	return 0;
    791 #undef N
    792 }
    793 
    794 /*
    795  * Return the phy mode for with the specified channel so the
    796  * caller can select a rate set.  This is problematic and the
    797  * work here assumes how things work elsewhere in this code.
    798  *
    799  * XXX never returns turbo modes -dcy
    800  */
    801 enum ieee80211_phymode
    802 ieee80211_chan2mode(struct ieee80211com *ic, struct ieee80211_channel *chan)
    803 {
    804 	/*
    805 	 * NB: this assumes the channel would not be supplied to us
    806 	 *     unless it was already compatible with the current mode.
    807 	 */
    808 	if (ic->ic_curmode != IEEE80211_MODE_AUTO ||
    809 	    chan == IEEE80211_CHAN_ANYC)
    810 		return ic->ic_curmode;
    811 	/*
    812 	 * In autoselect mode; deduce a mode based on the channel
    813 	 * characteristics.  We assume that turbo-only channels
    814 	 * are not considered when the channel set is constructed.
    815 	 */
    816 	if (IEEE80211_IS_CHAN_5GHZ(chan))
    817 		return IEEE80211_MODE_11A;
    818 	else if (IEEE80211_IS_CHAN_FHSS(chan))
    819 		return IEEE80211_MODE_FH;
    820 	else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN))
    821 		return IEEE80211_MODE_11G;
    822 	else
    823 		return IEEE80211_MODE_11B;
    824 }
    825 
    826 /*
    827  * convert IEEE80211 rate value to ifmedia subtype.
    828  * ieee80211 rate is in unit of 0.5Mbps.
    829  */
    830 int
    831 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
    832 {
    833 #define	N(a)	(sizeof(a) / sizeof(a[0]))
    834 	static const struct {
    835 		u_int	m;	/* rate + mode */
    836 		u_int	r;	/* if_media rate */
    837 	} rates[] = {
    838 		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
    839 		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
    840 		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
    841 		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
    842 		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
    843 		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
    844 		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
    845 		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
    846 		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
    847 		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
    848 		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
    849 		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
    850 		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
    851 		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
    852 		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
    853 		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
    854 		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
    855 		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
    856 		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
    857 		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
    858 		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
    859 		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
    860 		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
    861 		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
    862 		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
    863 		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
    864 		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
    865 		/* NB: OFDM72 doesn't realy exist so we don't handle it */
    866 	};
    867 	u_int mask, i;
    868 
    869 	mask = rate & IEEE80211_RATE_VAL;
    870 	switch (mode) {
    871 	case IEEE80211_MODE_11A:
    872 	case IEEE80211_MODE_TURBO:
    873 		mask |= IFM_IEEE80211_11A;
    874 		break;
    875 	case IEEE80211_MODE_11B:
    876 		mask |= IFM_IEEE80211_11B;
    877 		break;
    878 	case IEEE80211_MODE_FH:
    879 		mask |= IFM_IEEE80211_FH;
    880 		break;
    881 	case IEEE80211_MODE_AUTO:
    882 		/* NB: ic may be NULL for some drivers */
    883 		if (ic && ic->ic_phytype == IEEE80211_T_FH) {
    884 			mask |= IFM_IEEE80211_FH;
    885 			break;
    886 		}
    887 		/* NB: hack, 11g matches both 11b+11a rates */
    888 		/* fall thru... */
    889 	case IEEE80211_MODE_11G:
    890 		mask |= IFM_IEEE80211_11G;
    891 		break;
    892 	}
    893 	for (i = 0; i < N(rates); i++)
    894 		if (rates[i].m == mask)
    895 			return rates[i].r;
    896 	return IFM_AUTO;
    897 #undef N
    898 }
    899 
    900 int
    901 ieee80211_media2rate(int mword)
    902 {
    903 #define	N(a)	(sizeof(a) / sizeof(a[0]))
    904 	int i;
    905 	static const struct {
    906 		int subtype;
    907 		int rate;
    908 	} ieeerates[] = {
    909 		{ IFM_AUTO,		-1	},
    910 		{ IFM_MANUAL,		0	},
    911 		{ IFM_NONE,		0	},
    912 		{ IFM_IEEE80211_FH1,	2	},
    913 		{ IFM_IEEE80211_FH2,	4	},
    914 		{ IFM_IEEE80211_DS1,	2	},
    915 		{ IFM_IEEE80211_DS2,	4	},
    916 		{ IFM_IEEE80211_DS5,	11	},
    917 		{ IFM_IEEE80211_DS11,	22	},
    918 		{ IFM_IEEE80211_DS22,	44	},
    919 		{ IFM_IEEE80211_OFDM6,	12	},
    920 		{ IFM_IEEE80211_OFDM9,	18	},
    921 		{ IFM_IEEE80211_OFDM12,	24	},
    922 		{ IFM_IEEE80211_OFDM18,	36	},
    923 		{ IFM_IEEE80211_OFDM24,	48	},
    924 		{ IFM_IEEE80211_OFDM36,	72	},
    925 		{ IFM_IEEE80211_OFDM48,	96	},
    926 		{ IFM_IEEE80211_OFDM54,	108	},
    927 		{ IFM_IEEE80211_OFDM72,	144	},
    928 	};
    929 	for (i = 0; i < N(ieeerates); i++) {
    930 		if (ieeerates[i].subtype == IFM_SUBTYPE(mword))
    931 			return ieeerates[i].rate;
    932 	}
    933 	return 0;
    934 #undef N
    935 }
    936 
    937 #ifdef __NetBSD__
    938 /* TBD factor with sysctl_ath_verify. */
    939 static int
    940 sysctl_ieee80211_verify(SYSCTLFN_ARGS)
    941 {
    942 	int error, t;
    943 	struct sysctlnode node;
    944 
    945 	node = *rnode;
    946 	t = *(int*)rnode->sysctl_data;
    947 	node.sysctl_data = &t;
    948 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    949 	if (error || newp == NULL)
    950 		return (error);
    951 
    952 	IEEE80211_DPRINTF(("%s: t = %d, nodenum = %d, rnodenum = %d\n",
    953 	    __func__, t, node.sysctl_num, rnode->sysctl_num));
    954 
    955 	if (node.sysctl_num == ieee80211_inact_max_nodenum) {
    956 		if (t < 1)
    957 			return (EINVAL);
    958 		t = roundup(t, IEEE80211_INACT_WAIT) / IEEE80211_INACT_WAIT;
    959 #ifdef IEEE80211_DEBUG
    960 	} else if (node.sysctl_num == ieee80211_debug_nodenum) {
    961 		if (t < 0 || t > 2)
    962 			return (EINVAL);
    963 #endif /* IEEE80211_DEBUG */
    964 	} else
    965 		return (EINVAL);
    966 
    967 	*(int*)rnode->sysctl_data = t;
    968 
    969 	return (0);
    970 }
    971 
    972 /*
    973  * Pointers for testing:
    974  *
    975  *	If there are no interfaces, or else no 802.11 interfaces,
    976  *	ieee80211_node_walkfirst must return NULL.
    977  *
    978  *	If there is any single 802.11 interface, ieee80211_node_walkfirst
    979  *	must not return NULL.
    980  */
    981 static struct ieee80211_node *
    982 ieee80211_node_walkfirst(struct ieee80211_node_walk *nw,
    983     u_short if_index)
    984 {
    985 	struct ieee80211com *ic;
    986 	(void)memset(nw, 0, sizeof(*nw));
    987 
    988 	nw->nw_ifindex = if_index;
    989 
    990 	LIST_FOREACH(ic, &ieee80211com_head, ic_list) {
    991 		IEEE80211_DPRINTF(("%s: visiting ic %p\n", __func__, ic));
    992 		if (if_index != 0 && ic->ic_if.if_index != if_index) {
    993 			IEEE80211_DPRINTF(("%s: wrong ifindex, "
    994 			    "skipping ic %p\n", __func__, ic));
    995 			continue;
    996 		}
    997 		nw->nw_ic = ic;
    998 		nw->nw_ni = ic->ic_bss;
    999 		break;
   1000 	}
   1001 	IEEE80211_DPRINTF(("%s: ic %p ni %p\n", __func__, nw->nw_ic,
   1002 	    nw->nw_ni));
   1003 
   1004 	KASSERT(LOGICALLY_EQUAL(nw->nw_ni == NULL, nw->nw_ic == NULL));
   1005 
   1006 	return nw->nw_ni;
   1007 }
   1008 
   1009 static struct ieee80211_node *
   1010 ieee80211_node_walknext(struct ieee80211_node_walk *nw)
   1011 {
   1012 	KASSERT(LOGICALLY_EQUAL(nw->nw_ni == NULL, nw->nw_ic == NULL));
   1013 
   1014 	if (nw->nw_ic == NULL && nw->nw_ni == NULL)
   1015 		return NULL;
   1016 
   1017 	if (nw->nw_ni == nw->nw_ic->ic_bss)
   1018 		nw->nw_ni = TAILQ_FIRST(&nw->nw_ic->ic_node);
   1019 	else
   1020 		nw->nw_ni = TAILQ_NEXT(nw->nw_ni, ni_list);
   1021 
   1022 	if (nw->nw_ni == NULL) {
   1023 		if (nw->nw_ifindex != 0)
   1024 			return NULL;
   1025 
   1026 		nw->nw_ic = LIST_NEXT(nw->nw_ic, ic_list);
   1027 		if (nw->nw_ic == NULL)
   1028 			return NULL;
   1029 
   1030 		nw->nw_ni = nw->nw_ic->ic_bss;
   1031 	}
   1032 
   1033 	IEEE80211_DPRINTF(("%s: ic %p bss %p ni %p\n", __func__, nw->nw_ic,
   1034 	    nw->nw_ic->ic_bss, nw->nw_ni));
   1035 
   1036 	KASSERT(LOGICALLY_EQUAL(nw->nw_ni == NULL, nw->nw_ic == NULL));
   1037 
   1038 	return nw->nw_ni;
   1039 }
   1040 
   1041 static void
   1042 sysctl_ieee80211_fill_node(struct ieee80211_node *ni,
   1043     struct ieee80211_node_sysctl *ns, int ifindex,
   1044     struct ieee80211_channel *chan0, int is_bss)
   1045 {
   1046 	ns->ns_ifindex = ifindex;
   1047 	ns->ns_capinfo = ni->ni_capinfo;
   1048 	ns->ns_flags = (is_bss) ? IEEE80211_NODE_SYSCTL_F_BSS : 0;
   1049 	(void)memcpy(ns->ns_macaddr, ni->ni_macaddr, sizeof(ns->ns_macaddr));
   1050 	(void)memcpy(ns->ns_bssid, ni->ni_bssid, sizeof(ns->ns_bssid));
   1051 	if (ni->ni_chan != IEEE80211_CHAN_ANYC) {
   1052 		ns->ns_freq = ni->ni_chan->ic_freq;
   1053 		ns->ns_chanflags = ni->ni_chan->ic_flags;
   1054 		ns->ns_chanidx = ni->ni_chan - chan0;
   1055 	} else {
   1056 		ns->ns_freq = ns->ns_chanflags = 0;
   1057 		ns->ns_chanidx = 0;
   1058 	}
   1059 	ns->ns_rssi = ni->ni_rssi;
   1060 	ns->ns_esslen = ni->ni_esslen;
   1061 	(void)memcpy(ns->ns_essid, ni->ni_essid, sizeof(ns->ns_essid));
   1062 	ns->ns_pwrsave = ni->ni_pwrsave;
   1063 	ns->ns_erp = ni->ni_erp;
   1064 	ns->ns_associd = ni->ni_associd;
   1065 	ns->ns_inact = ni->ni_inact * IEEE80211_INACT_WAIT;
   1066 	ns->ns_rstamp = ni->ni_rstamp;
   1067 	ns->ns_rates = ni->ni_rates;
   1068 	ns->ns_txrate = ni->ni_txrate;
   1069 	ns->ns_intval = ni->ni_intval;
   1070 	(void)memcpy(ns->ns_tstamp, ni->ni_tstamp, sizeof(ns->ns_tstamp));
   1071 	ns->ns_txseq = ni->ni_txseq;
   1072 	ns->ns_rxseq = ni->ni_rxseq;
   1073 	ns->ns_fhdwell = ni->ni_fhdwell;
   1074 	ns->ns_fhindex = ni->ni_fhindex;
   1075 	ns->ns_fails = ni->ni_fails;
   1076 }
   1077 
   1078 /* Between two examinations of the sysctl tree, I expect each
   1079  * interface to add no more than 5 nodes.
   1080  */
   1081 #define IEEE80211_SYSCTL_NODE_GROWTH	5
   1082 
   1083 static int
   1084 sysctl_ieee80211_node(SYSCTLFN_ARGS)
   1085 {
   1086 	struct ieee80211_node_walk nw;
   1087 	struct ieee80211_node *ni;
   1088 	struct ieee80211_node_sysctl ns;
   1089 	char *dp;
   1090 	u_int cur_ifindex, ifcount, ifindex, last_ifindex, op, arg, hdr_type;
   1091 	size_t len, needed, eltsize, out_size;
   1092 	int error, s, nelt;
   1093 
   1094 	if (namelen == 1 && name[0] == CTL_QUERY)
   1095 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   1096 
   1097 	if (namelen != IEEE80211_SYSCTL_NODENAMELEN)
   1098 		return (EINVAL);
   1099 
   1100 	/* ifindex.op.arg.header-type.eltsize.nelt */
   1101 	dp = oldp;
   1102 	len = (oldp != NULL) ? *oldlenp : 0;
   1103 	ifindex = name[IEEE80211_SYSCTL_NODENAME_IF];
   1104 	op = name[IEEE80211_SYSCTL_NODENAME_OP];
   1105 	arg = name[IEEE80211_SYSCTL_NODENAME_ARG];
   1106 	hdr_type = name[IEEE80211_SYSCTL_NODENAME_TYPE];
   1107 	eltsize = name[IEEE80211_SYSCTL_NODENAME_ELTSIZE];
   1108 	nelt = name[IEEE80211_SYSCTL_NODENAME_ELTCOUNT];
   1109 	out_size = MIN(sizeof(ns), eltsize);
   1110 
   1111 	IEEE80211_DPRINTF(("%s: ifindex %u op %u arg %u hdr_type %u eltsize %u "
   1112 	    "nelt %d out_size %u\n", __func__, ifindex, op, arg, hdr_type,
   1113 	    eltsize, nelt, out_size));
   1114 
   1115 	if (op != IEEE80211_SYSCTL_OP_ALL || arg != 0 ||
   1116 	    hdr_type != IEEE80211_SYSCTL_T_NODE || eltsize < 1 || nelt < 0)
   1117 		return (EINVAL);
   1118 
   1119 	error = 0;
   1120 	needed = 0;
   1121 	ifcount = 0;
   1122 	last_ifindex = 0;
   1123 
   1124 	s = splnet();
   1125 
   1126 	for (ni = ieee80211_node_walkfirst(&nw, ifindex); ni != NULL;
   1127 	     ni = ieee80211_node_walknext(&nw)) {
   1128 		struct ieee80211com *ic;
   1129 
   1130 		ic = nw.nw_ic;
   1131 		cur_ifindex = ic->ic_if.if_index;
   1132 
   1133 		IEEE80211_DPRINTF(("%s: visit ic %p idx %u\n", __func__, ic,
   1134 		    cur_ifindex));
   1135 
   1136 		if (cur_ifindex != last_ifindex) {
   1137 			ifcount++;
   1138 			last_ifindex = cur_ifindex;
   1139 		}
   1140 
   1141 		IEEE80211_DPRINTF(("%s: ni %p dp %p len %u nelt %d\n",
   1142 		    __func__, ni, dp, len, nelt));
   1143 
   1144 		if (nelt <= 0)
   1145 			continue;
   1146 
   1147 		if (len >= eltsize) {
   1148 			sysctl_ieee80211_fill_node(ni, &ns, cur_ifindex,
   1149 			    &ic->ic_channels[0], ni == ic->ic_bss);
   1150 			error = copyout(&ns, dp, out_size);
   1151 			if (error)
   1152 				goto cleanup;
   1153 			dp += eltsize;
   1154 			len -= eltsize;
   1155 		}
   1156 		needed += eltsize;
   1157 		if (nelt != INT_MAX)
   1158 			nelt--;
   1159 		IEEE80211_DPRINTF(("%s: needed %u\n", __func__, needed));
   1160 	}
   1161 	IEEE80211_DPRINTF(("%s: %u interfaces\n", __func__, ifcount));
   1162 cleanup:
   1163 	splx(s);
   1164 
   1165 	*oldlenp = needed;
   1166 	if (oldp == NULL)
   1167 		*oldlenp += ifcount * IEEE80211_SYSCTL_NODE_GROWTH * eltsize;
   1168 
   1169 	return (error);
   1170 }
   1171 
   1172 /*
   1173  * Setup sysctl(3) MIB, net.ieee80211.*
   1174  *
   1175  * TBD condition CTLFLAG_PERMANENT on being an LKM or not
   1176  */
   1177 SYSCTL_SETUP(sysctl_ieee80211, "sysctl ieee80211 subtree setup")
   1178 {
   1179 	int rc;
   1180 	struct sysctlnode *cnode, *rnode;
   1181 
   1182 	if ((rc = sysctl_createv(clog, 0, NULL, &rnode,
   1183 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "net", NULL,
   1184 	    NULL, 0, NULL, 0, CTL_NET, CTL_EOL)) != 0)
   1185 		goto err;
   1186 
   1187 	if ((rc = sysctl_createv(clog, 0, &rnode, &rnode,
   1188 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "link",
   1189 	    "link-layer statistics and controls",
   1190 	    NULL, 0, NULL, 0, PF_LINK, CTL_EOL)) != 0)
   1191 		goto err;
   1192 
   1193 	if ((rc = sysctl_createv(clog, 0, &rnode, &rnode,
   1194 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "ieee80211",
   1195 	    "IEEE 802.11 WLAN statistics and controls",
   1196 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
   1197 		goto err;
   1198 
   1199 	if ((rc = sysctl_createv(clog, 0, &rnode, NULL,
   1200 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "nodes", "client/peer stations",
   1201 	    sysctl_ieee80211_node, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
   1202 		goto err;
   1203 
   1204 #ifdef IEEE80211_DEBUG
   1205 
   1206 	/* control debugging printfs */
   1207 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
   1208 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
   1209 	    "debug", SYSCTL_DESCR("Enable IEEE 802.11 debugging output"),
   1210 	    sysctl_ieee80211_verify, 0, &ieee80211_debug, 0,
   1211 	    CTL_CREATE, CTL_EOL)) != 0)
   1212 		goto err;
   1213 
   1214 	ieee80211_debug_nodenum = cnode->sysctl_num;
   1215 
   1216 #endif /* IEEE80211_DEBUG */
   1217 
   1218 	/* control inactivity timer */
   1219 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
   1220 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
   1221 	    "maxinact", SYSCTL_DESCR("Station inactivity timeout"),
   1222 	    sysctl_ieee80211_verify, 0, &ieee80211_inact_max,
   1223 	    0, CTL_CREATE, CTL_EOL)) != 0)
   1224 		goto err;
   1225 
   1226 	ieee80211_inact_max_nodenum = cnode->sysctl_num;
   1227 
   1228 	return;
   1229 err:
   1230 	printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
   1231 }
   1232 #endif /* __NetBSD__ */
   1233 
   1234 #ifdef __FreeBSD__
   1235 /*
   1236  * Module glue.
   1237  *
   1238  * NB: the module name is "wlan" for compatibility with NetBSD.
   1239  */
   1240 
   1241 static int
   1242 ieee80211_modevent(module_t mod, int type, void *unused)
   1243 {
   1244 	switch (type) {
   1245 	case MOD_LOAD:
   1246 		if (bootverbose)
   1247 			printf("wlan: <802.11 Link Layer>\n");
   1248 		return 0;
   1249 	case MOD_UNLOAD:
   1250 		return 0;
   1251 	}
   1252 	return EINVAL;
   1253 }
   1254 
   1255 static moduledata_t ieee80211_mod = {
   1256 	"wlan",
   1257 	ieee80211_modevent,
   1258 	0
   1259 };
   1260 DECLARE_MODULE(wlan, ieee80211_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
   1261 MODULE_VERSION(wlan, 1);
   1262 MODULE_DEPEND(wlan, rc4, 1, 1, 1);
   1263 MODULE_DEPEND(wlan, ether, 1, 1, 1);
   1264 #endif
   1265