Home | History | Annotate | Line # | Download | only in net80211
ieee80211.c revision 1.38
      1 /*	$NetBSD: ieee80211.c,v 1.38 2005/06/26 04:31:51 dyoung Exp $	*/
      2 /*-
      3  * Copyright (c) 2001 Atsushi Onoe
      4  * Copyright (c) 2002-2005 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.19 2005/01/27 17:39:17 sam Exp $");
     37 #endif
     38 #ifdef __NetBSD__
     39 __KERNEL_RCSID(0, "$NetBSD: ieee80211.c,v 1.38 2005/06/26 04:31:51 dyoung Exp $");
     40 #endif
     41 
     42 /*
     43  * IEEE 802.11 generic handler
     44  */
     45 
     46 #include "opt_inet.h"
     47 #include "bpfilter.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/kernel.h>
     52 
     53 #include <sys/socket.h>
     54 #include <sys/sockio.h>
     55 #include <sys/endian.h>
     56 #include <sys/errno.h>
     57 #include <sys/proc.h>
     58 #include <sys/sysctl.h>
     59 
     60 #include <net/if.h>
     61 #include <net/if_media.h>
     62 #include <net/if_arp.h>
     63 #include <net/if_ether.h>
     64 #include <net/if_llc.h>
     65 
     66 #include <net80211/ieee80211_netbsd.h>
     67 #include <net80211/ieee80211_var.h>
     68 #include <net80211/ieee80211_sysctl.h>
     69 
     70 #include <net/bpf.h>
     71 
     72 #ifdef INET
     73 #include <netinet/in.h>
     74 #include <net/if_ether.h>
     75 #endif
     76 
     77 struct ieee80211com_head ieee80211com_head =
     78     LIST_HEAD_INITIALIZER(ieee80211com_head);
     79 
     80 const char *ieee80211_phymode_name[] = {
     81 	"auto",		/* IEEE80211_MODE_AUTO */
     82 	"11a",		/* IEEE80211_MODE_11A */
     83 	"11b",		/* IEEE80211_MODE_11B */
     84 	"11g",		/* IEEE80211_MODE_11G */
     85 	"FH",		/* IEEE80211_MODE_FH */
     86 	"turboA",	/* IEEE80211_MODE_TURBO_A */
     87 	"turboG",	/* IEEE80211_MODE_TURBO_G */
     88 };
     89 
     90 /* list of all instances */
     91 SLIST_HEAD(ieee80211_list, ieee80211com);
     92 static struct ieee80211_list ieee80211_list =
     93 	SLIST_HEAD_INITIALIZER(ieee80211_list);
     94 static u_int8_t ieee80211_vapmap[32];		/* enough for 256 */
     95 
     96 static void
     97 ieee80211_add_vap(struct ieee80211com *ic)
     98 {
     99 #define	N(a)	(sizeof(a)/sizeof(a[0]))
    100 	int i;
    101 	int s;
    102 	u_int8_t b;
    103 
    104 	s = splnet();
    105 	ic->ic_vap = 0;
    106 	for (i = 0; i < N(ieee80211_vapmap) && ieee80211_vapmap[i] == 0xff; i++)
    107 		ic->ic_vap += NBBY;
    108 	if (i == N(ieee80211_vapmap))
    109 		panic("vap table full");
    110 	for (b = ieee80211_vapmap[i]; b & 1; b >>= 1)
    111 		ic->ic_vap++;
    112 	setbit(ieee80211_vapmap, ic->ic_vap);
    113 	SLIST_INSERT_HEAD(&ieee80211_list, ic, ic_next);
    114 	splx(s);
    115 #undef N
    116 }
    117 
    118 static void
    119 ieee80211_remove_vap(struct ieee80211com *ic)
    120 {
    121 	int s;
    122 
    123 	s = splnet();
    124 	SLIST_REMOVE(&ieee80211_list, ic, ieee80211com, ic_next);
    125 	IASSERT(ic->ic_vap < sizeof(ieee80211_vapmap)*NBBY,
    126 		("invalid vap id %d", ic->ic_vap));
    127 	IASSERT(isset(ieee80211_vapmap, ic->ic_vap),
    128 		("vap id %d not allocated", ic->ic_vap));
    129 	clrbit(ieee80211_vapmap, ic->ic_vap);
    130 	splx(s);
    131 }
    132 
    133 /*
    134  * Default reset method for use with the ioctl support.  This
    135  * method is invoked after any state change in the 802.11
    136  * layer that should be propagated to the hardware but not
    137  * require re-initialization of the 802.11 state machine (e.g
    138  * rescanning for an ap).  We always return ENETRESET which
    139  * should cause the driver to re-initialize the device. Drivers
    140  * can override this method to implement more optimized support.
    141  */
    142 static int
    143 ieee80211_default_reset(struct ifnet *ifp)
    144 {
    145 	return ENETRESET;
    146 }
    147 
    148 void
    149 ieee80211_ifattach(struct ieee80211com *ic)
    150 {
    151 	struct ifnet *ifp = ic->ic_ifp;
    152 	struct ieee80211_channel *c;
    153 	int i;
    154 
    155 	ether_ifattach(ifp, ic->ic_myaddr);
    156 #if NBPFILTER > 0
    157 	bpfattach2(ifp, DLT_IEEE802_11,
    158 	    sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf);
    159 #endif
    160 
    161 	ieee80211_crypto_attach(ic);
    162 
    163 	/*
    164 	 * Fill in 802.11 available channel set, mark
    165 	 * all available channels as active, and pick
    166 	 * a default channel if not already specified.
    167 	 */
    168 	memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
    169 	ic->ic_modecaps |= 1<<IEEE80211_MODE_AUTO;
    170 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
    171 		c = &ic->ic_channels[i];
    172 		if (c->ic_flags) {
    173 			/*
    174 			 * Verify driver passed us valid data.
    175 			 */
    176 			if (i != ieee80211_chan2ieee(ic, c)) {
    177 				if_printf(ifp, "bad channel ignored; "
    178 					"freq %u flags %x number %u\n",
    179 					c->ic_freq, c->ic_flags, i);
    180 				c->ic_flags = 0;	/* NB: remove */
    181 				continue;
    182 			}
    183 			setbit(ic->ic_chan_avail, i);
    184 			/*
    185 			 * Identify mode capabilities.
    186 			 */
    187 			if (IEEE80211_IS_CHAN_A(c))
    188 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11A;
    189 			if (IEEE80211_IS_CHAN_B(c))
    190 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11B;
    191 			if (IEEE80211_IS_CHAN_PUREG(c))
    192 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11G;
    193 			if (IEEE80211_IS_CHAN_FHSS(c))
    194 				ic->ic_modecaps |= 1<<IEEE80211_MODE_FH;
    195 			if (IEEE80211_IS_CHAN_T(c))
    196 				ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO_A;
    197 			if (IEEE80211_IS_CHAN_108G(c))
    198 				ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO_G;
    199 		}
    200 	}
    201 	/* validate ic->ic_curmode */
    202 	if ((ic->ic_modecaps & (1<<ic->ic_curmode)) == 0)
    203 		ic->ic_curmode = IEEE80211_MODE_AUTO;
    204 	ic->ic_des_chan = IEEE80211_CHAN_ANYC;	/* any channel is ok */
    205 #if 0
    206 	/*
    207 	 * Enable WME by default if we're capable.
    208 	 */
    209 	if (ic->ic_caps & IEEE80211_C_WME)
    210 		ic->ic_flags |= IEEE80211_F_WME;
    211 #endif
    212 	(void) ieee80211_setmode(ic, ic->ic_curmode);
    213 
    214 	if (ic->ic_lintval == 0)
    215 		ic->ic_lintval = IEEE80211_BINTVAL_DEFAULT;
    216 	ic->ic_bmisstimeout = 7*ic->ic_lintval;	/* default 7 beacons */
    217 	ic->ic_dtim_period = IEEE80211_DTIM_DEFAULT;
    218 	IEEE80211_BEACON_LOCK_INIT(ic, "beacon");
    219 
    220 	ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
    221 
    222 	LIST_INSERT_HEAD(&ieee80211com_head, ic, ic_list);
    223 	ieee80211_node_attach(ic);
    224 	ieee80211_proto_attach(ic);
    225 
    226 	ieee80211_add_vap(ic);
    227 
    228 	ieee80211_sysctl_attach(ic);		/* NB: requires ic_vap */
    229 
    230 	/*
    231 	 * Install a default reset method for the ioctl support.
    232 	 * The driver is expected to fill this in before calling us.
    233 	 */
    234 	if (ic->ic_reset == NULL)
    235 		ic->ic_reset = ieee80211_default_reset;
    236 }
    237 
    238 void
    239 ieee80211_ifdetach(struct ieee80211com *ic)
    240 {
    241 	struct ifnet *ifp = ic->ic_ifp;
    242 
    243 	ieee80211_remove_vap(ic);
    244 
    245 	ieee80211_sysctl_detach(ic);
    246 	ieee80211_proto_detach(ic);
    247 	ieee80211_crypto_detach(ic);
    248 	ieee80211_node_detach(ic);
    249 	LIST_REMOVE(ic, ic_list);
    250 	ifmedia_delete_instance(&ic->ic_media, IFM_INST_ANY);
    251 
    252 	IEEE80211_BEACON_LOCK_DESTROY(ic);
    253 
    254 #if NBPFILTER > 0
    255 	bpfdetach(ifp);
    256 #endif
    257 	ether_ifdetach(ifp);
    258 }
    259 
    260 /*
    261  * Convert MHz frequency to IEEE channel number.
    262  */
    263 u_int
    264 ieee80211_mhz2ieee(u_int freq, u_int flags)
    265 {
    266 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
    267 		if (freq == 2484)
    268 			return 14;
    269 		if (freq < 2484)
    270 			return (freq - 2407) / 5;
    271 		else
    272 			return 15 + ((freq - 2512) / 20);
    273 	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
    274 		return (freq - 5000) / 5;
    275 	} else {				/* either, guess */
    276 		if (freq == 2484)
    277 			return 14;
    278 		if (freq < 2484)
    279 			return (freq - 2407) / 5;
    280 		if (freq < 5000)
    281 			return 15 + ((freq - 2512) / 20);
    282 		return (freq - 5000) / 5;
    283 	}
    284 }
    285 
    286 /*
    287  * Convert channel to IEEE channel number.
    288  */
    289 u_int
    290 ieee80211_chan2ieee(struct ieee80211com *ic, struct ieee80211_channel *c)
    291 {
    292 	if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX])
    293 		return c - ic->ic_channels;
    294 	else if (c == IEEE80211_CHAN_ANYC)
    295 		return IEEE80211_CHAN_ANY;
    296 	else if (c != NULL) {
    297 		if_printf(ic->ic_ifp, "invalid channel freq %u flags %x\n",
    298 			c->ic_freq, c->ic_flags);
    299 		return 0;		/* XXX */
    300 	} else {
    301 		if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
    302 		return 0;		/* XXX */
    303 	}
    304 }
    305 
    306 /*
    307  * Convert IEEE channel number to MHz frequency.
    308  */
    309 u_int
    310 ieee80211_ieee2mhz(u_int chan, u_int flags)
    311 {
    312 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
    313 		if (chan == 14)
    314 			return 2484;
    315 		if (chan < 14)
    316 			return 2407 + chan*5;
    317 		else
    318 			return 2512 + ((chan-15)*20);
    319 	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
    320 		return 5000 + (chan*5);
    321 	} else {				/* either, guess */
    322 		if (chan == 14)
    323 			return 2484;
    324 		if (chan < 14)			/* 0-13 */
    325 			return 2407 + chan*5;
    326 		if (chan < 27)			/* 15-26 */
    327 			return 2512 + ((chan-15)*20);
    328 		return 5000 + (chan*5);
    329 	}
    330 }
    331 
    332 /*
    333  * Setup the media data structures according to the channel and
    334  * rate tables.  This must be called by the driver after
    335  * ieee80211_attach and before most anything else.
    336  */
    337 void
    338 ieee80211_media_init(struct ieee80211com *ic,
    339 	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
    340 {
    341 #define	ADD(_ic, _s, _o) \
    342 	ifmedia_add(&(_ic)->ic_media, \
    343 		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
    344 	struct ifnet *ifp = ic->ic_ifp;
    345 	struct ifmediareq imr;
    346 	int i, j, mode, rate, maxrate, mword, mopt, r;
    347 	struct ieee80211_rateset *rs;
    348 	struct ieee80211_rateset allrates;
    349 
    350 	/*
    351 	 * Do late attach work that must wait for any subclass
    352 	 * (i.e. driver) work such as overriding methods.
    353 	 */
    354 	ieee80211_node_lateattach(ic);
    355 
    356 #ifdef IEEE80211_NO_HOSTAP
    357 	ic->ic_caps &= ~IEEE80211_C_HOSTAP;
    358 #endif /* IEEE80211_NO_HOSTAP */
    359 
    360 	/*
    361 	 * Fill in media characteristics.
    362 	 */
    363 	ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
    364 	maxrate = 0;
    365 	memset(&allrates, 0, sizeof(allrates));
    366 	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) {
    367 		static const u_int mopts[] = {
    368 			IFM_AUTO,
    369 			IFM_IEEE80211_11A,
    370 			IFM_IEEE80211_11B,
    371 			IFM_IEEE80211_11G,
    372 			IFM_IEEE80211_FH,
    373 			IFM_IEEE80211_11A | IFM_IEEE80211_TURBO,
    374 			IFM_IEEE80211_11G | IFM_IEEE80211_TURBO,
    375 		};
    376 		if ((ic->ic_modecaps & (1<<mode)) == 0)
    377 			continue;
    378 		mopt = mopts[mode];
    379 		ADD(ic, IFM_AUTO, mopt);	/* e.g. 11a auto */
    380 		if (ic->ic_caps & IEEE80211_C_IBSS)
    381 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC);
    382 		if (ic->ic_caps & IEEE80211_C_HOSTAP)
    383 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP);
    384 		if (ic->ic_caps & IEEE80211_C_AHDEMO)
    385 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
    386 		if (ic->ic_caps & IEEE80211_C_MONITOR)
    387 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR);
    388 		if (mode == IEEE80211_MODE_AUTO)
    389 			continue;
    390 		rs = &ic->ic_sup_rates[mode];
    391 		for (i = 0; i < rs->rs_nrates; i++) {
    392 			rate = rs->rs_rates[i];
    393 			mword = ieee80211_rate2media(ic, rate, mode);
    394 			if (mword == 0)
    395 				continue;
    396 			ADD(ic, mword, mopt);
    397 			if (ic->ic_caps & IEEE80211_C_IBSS)
    398 				ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
    399 			if (ic->ic_caps & IEEE80211_C_HOSTAP)
    400 				ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
    401 			if (ic->ic_caps & IEEE80211_C_AHDEMO)
    402 				ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
    403 			if (ic->ic_caps & IEEE80211_C_MONITOR)
    404 				ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
    405 			/*
    406 			 * Add rate to the collection of all rates.
    407 			 */
    408 			r = rate & IEEE80211_RATE_VAL;
    409 			for (j = 0; j < allrates.rs_nrates; j++)
    410 				if (allrates.rs_rates[j] == r)
    411 					break;
    412 			if (j == allrates.rs_nrates) {
    413 				/* unique, add to the set */
    414 				allrates.rs_rates[j] = r;
    415 				allrates.rs_nrates++;
    416 			}
    417 			rate = (rate & IEEE80211_RATE_VAL) / 2;
    418 			if (rate > maxrate)
    419 				maxrate = rate;
    420 		}
    421 	}
    422 	for (i = 0; i < allrates.rs_nrates; i++) {
    423 		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
    424 				IEEE80211_MODE_AUTO);
    425 		if (mword == 0)
    426 			continue;
    427 		mword = IFM_SUBTYPE(mword);	/* remove media options */
    428 		ADD(ic, mword, 0);
    429 		if (ic->ic_caps & IEEE80211_C_IBSS)
    430 			ADD(ic, mword, IFM_IEEE80211_ADHOC);
    431 		if (ic->ic_caps & IEEE80211_C_HOSTAP)
    432 			ADD(ic, mword, IFM_IEEE80211_HOSTAP);
    433 		if (ic->ic_caps & IEEE80211_C_AHDEMO)
    434 			ADD(ic, mword, IFM_IEEE80211_ADHOC | IFM_FLAG0);
    435 		if (ic->ic_caps & IEEE80211_C_MONITOR)
    436 			ADD(ic, mword, IFM_IEEE80211_MONITOR);
    437 	}
    438 	ieee80211_media_status(ifp, &imr);
    439 	ifmedia_set(&ic->ic_media, imr.ifm_active);
    440 
    441 	if (maxrate)
    442 		ifp->if_baudrate = IF_Mbps(maxrate);
    443 #undef ADD
    444 }
    445 
    446 void
    447 ieee80211_announce(struct ieee80211com *ic)
    448 {
    449 	struct ifnet *ifp = ic->ic_ifp;
    450 	int i, mode, rate, mword;
    451 	struct ieee80211_rateset *rs;
    452 
    453 	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
    454 		if ((ic->ic_modecaps & (1<<mode)) == 0)
    455 			continue;
    456 		if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
    457 		rs = &ic->ic_sup_rates[mode];
    458 		for (i = 0; i < rs->rs_nrates; i++) {
    459 			rate = rs->rs_rates[i];
    460 			mword = ieee80211_rate2media(ic, rate, mode);
    461 			if (mword == 0)
    462 				continue;
    463 			printf("%s%d%sMbps", (i != 0 ? " " : ""),
    464 			    (rate & IEEE80211_RATE_VAL) / 2,
    465 			    ((rate & 0x1) != 0 ? ".5" : ""));
    466 		}
    467 		printf("\n");
    468 	}
    469 }
    470 
    471 static int
    472 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
    473 {
    474 #define	IEEERATE(_ic,_m,_i) \
    475 	((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
    476 	int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
    477 	for (i = 0; i < nrates; i++)
    478 		if (IEEERATE(ic, mode, i) == rate)
    479 			return i;
    480 	return -1;
    481 #undef IEEERATE
    482 }
    483 
    484 /*
    485  * Find an instance by it's mac address.
    486  */
    487 struct ieee80211com *
    488 ieee80211_find_vap(const u_int8_t mac[IEEE80211_ADDR_LEN])
    489 {
    490 	struct ieee80211com *ic;
    491 
    492 	/* XXX lock */
    493 	SLIST_FOREACH(ic, &ieee80211_list, ic_next)
    494 		if (IEEE80211_ADDR_EQ(mac, ic->ic_myaddr))
    495 			return ic;
    496 	return NULL;
    497 }
    498 
    499 static struct ieee80211com *
    500 ieee80211_find_instance(struct ifnet *ifp)
    501 {
    502 	struct ieee80211com *ic;
    503 
    504 	/* XXX lock */
    505 	/* XXX not right for multiple instances but works for now */
    506 	SLIST_FOREACH(ic, &ieee80211_list, ic_next)
    507 		if (ic->ic_ifp == ifp)
    508 			return ic;
    509 	return NULL;
    510 }
    511 
    512 /*
    513  * Handle a media change request.
    514  */
    515 int
    516 ieee80211_media_change(struct ifnet *ifp)
    517 {
    518 	struct ieee80211com *ic;
    519 	struct ifmedia_entry *ime;
    520 	enum ieee80211_opmode newopmode;
    521 	enum ieee80211_phymode newphymode;
    522 	int i, j, newrate, error = 0;
    523 
    524 	ic = ieee80211_find_instance(ifp);
    525 	if (!ic) {
    526 		if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
    527 		return EINVAL;
    528 	}
    529 	ime = ic->ic_media.ifm_cur;
    530 	/*
    531 	 * First, identify the phy mode.
    532 	 */
    533 	switch (IFM_MODE(ime->ifm_media)) {
    534 	case IFM_IEEE80211_11A:
    535 		newphymode = IEEE80211_MODE_11A;
    536 		break;
    537 	case IFM_IEEE80211_11B:
    538 		newphymode = IEEE80211_MODE_11B;
    539 		break;
    540 	case IFM_IEEE80211_11G:
    541 		newphymode = IEEE80211_MODE_11G;
    542 		break;
    543 	case IFM_IEEE80211_FH:
    544 		newphymode = IEEE80211_MODE_FH;
    545 		break;
    546 	case IFM_AUTO:
    547 		newphymode = IEEE80211_MODE_AUTO;
    548 		break;
    549 	default:
    550 		return EINVAL;
    551 	}
    552 	/*
    553 	 * Turbo mode is an ``option''.
    554 	 * XXX does not apply to AUTO
    555 	 */
    556 	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
    557 		if (newphymode == IEEE80211_MODE_11A)
    558 			newphymode = IEEE80211_MODE_TURBO_A;
    559 		else if (newphymode == IEEE80211_MODE_11G)
    560 			newphymode = IEEE80211_MODE_TURBO_G;
    561 		else
    562 			return EINVAL;
    563 	}
    564 	/*
    565 	 * Validate requested mode is available.
    566 	 */
    567 	if ((ic->ic_modecaps & (1<<newphymode)) == 0)
    568 		return EINVAL;
    569 
    570 	/*
    571 	 * Next, the fixed/variable rate.
    572 	 */
    573 	i = -1;
    574 	if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
    575 		/*
    576 		 * Convert media subtype to rate.
    577 		 */
    578 		newrate = ieee80211_media2rate(ime->ifm_media);
    579 		if (newrate == 0)
    580 			return EINVAL;
    581 		/*
    582 		 * Check the rate table for the specified/current phy.
    583 		 */
    584 		if (newphymode == IEEE80211_MODE_AUTO) {
    585 			/*
    586 			 * In autoselect mode search for the rate.
    587 			 */
    588 			for (j = IEEE80211_MODE_11A;
    589 			     j < IEEE80211_MODE_MAX; j++) {
    590 				if ((ic->ic_modecaps & (1<<j)) == 0)
    591 					continue;
    592 				i = findrate(ic, j, newrate);
    593 				if (i != -1) {
    594 					/* lock mode too */
    595 					newphymode = j;
    596 					break;
    597 				}
    598 			}
    599 		} else {
    600 			i = findrate(ic, newphymode, newrate);
    601 		}
    602 		if (i == -1)			/* mode/rate mismatch */
    603 			return EINVAL;
    604 	}
    605 	/* NB: defer rate setting to later */
    606 
    607 	/*
    608 	 * Deduce new operating mode but don't install it just yet.
    609 	 */
    610 	if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) ==
    611 	    (IFM_IEEE80211_ADHOC|IFM_FLAG0))
    612 		newopmode = IEEE80211_M_AHDEMO;
    613 	else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
    614 		newopmode = IEEE80211_M_HOSTAP;
    615 	else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
    616 		newopmode = IEEE80211_M_IBSS;
    617 	else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
    618 		newopmode = IEEE80211_M_MONITOR;
    619 	else
    620 		newopmode = IEEE80211_M_STA;
    621 
    622 #ifndef IEEE80211_NO_HOSTAP
    623 	/*
    624 	 * Autoselect doesn't make sense when operating as an AP.
    625 	 * If no phy mode has been selected, pick one and lock it
    626 	 * down so rate tables can be used in forming beacon frames
    627 	 * and the like.
    628 	 */
    629 	if (newopmode == IEEE80211_M_HOSTAP &&
    630 	    newphymode == IEEE80211_MODE_AUTO) {
    631 		for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++)
    632 			if (ic->ic_modecaps & (1<<j)) {
    633 				newphymode = j;
    634 				break;
    635 			}
    636 	}
    637 #endif /* !IEEE80211_NO_HOSTAP */
    638 
    639 	/*
    640 	 * Handle phy mode change.
    641 	 */
    642 	if (ic->ic_curmode != newphymode) {		/* change phy mode */
    643 		error = ieee80211_setmode(ic, newphymode);
    644 		if (error != 0)
    645 			return error;
    646 		error = ENETRESET;
    647 	}
    648 
    649 	/*
    650 	 * Committed to changes, install the rate setting.
    651 	 */
    652 	if (ic->ic_fixed_rate != i) {
    653 		ic->ic_fixed_rate = i;			/* set fixed tx rate */
    654 		error = ENETRESET;
    655 	}
    656 
    657 	/*
    658 	 * Handle operating mode change.
    659 	 */
    660 	if (ic->ic_opmode != newopmode) {
    661 		ic->ic_opmode = newopmode;
    662 		switch (newopmode) {
    663 		case IEEE80211_M_AHDEMO:
    664 		case IEEE80211_M_HOSTAP:
    665 		case IEEE80211_M_STA:
    666 		case IEEE80211_M_MONITOR:
    667 			ic->ic_flags &= ~IEEE80211_F_IBSSON;
    668 			break;
    669 		case IEEE80211_M_IBSS:
    670 			ic->ic_flags |= IEEE80211_F_IBSSON;
    671 			break;
    672 		}
    673 		/*
    674 		 * Yech, slot time may change depending on the
    675 		 * operating mode so reset it to be sure everything
    676 		 * is setup appropriately.
    677 		 */
    678 		ieee80211_reset_erp(ic);
    679 		ieee80211_wme_initparams(ic);	/* after opmode change */
    680 		error = ENETRESET;
    681 	}
    682 #ifdef notdef
    683 	if (error == 0)
    684 		ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
    685 #endif
    686 	return error;
    687 }
    688 
    689 void
    690 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
    691 {
    692 	struct ieee80211com *ic;
    693 	struct ieee80211_rateset *rs;
    694 
    695 	ic = ieee80211_find_instance(ifp);
    696 	if (!ic) {
    697 		if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
    698 		return;
    699 	}
    700 	imr->ifm_status = IFM_AVALID;
    701 	imr->ifm_active = IFM_IEEE80211;
    702 	if (ic->ic_state == IEEE80211_S_RUN)
    703 		imr->ifm_status |= IFM_ACTIVE;
    704 	/*
    705 	 * Calculate a current rate if possible.
    706 	 */
    707 	if (ic->ic_fixed_rate != -1) {
    708 		/*
    709 		 * A fixed rate is set, report that.
    710 		 */
    711 		rs = &ic->ic_sup_rates[ic->ic_curmode];
    712 		imr->ifm_active |= ieee80211_rate2media(ic,
    713 			rs->rs_rates[ic->ic_fixed_rate], ic->ic_curmode);
    714 	} else if (ic->ic_opmode == IEEE80211_M_STA) {
    715 		/*
    716 		 * In station mode report the current transmit rate.
    717 		 */
    718 		rs = &ic->ic_bss->ni_rates;
    719 		imr->ifm_active |= ieee80211_rate2media(ic,
    720 			rs->rs_rates[ic->ic_bss->ni_txrate], ic->ic_curmode);
    721 	} else
    722 		imr->ifm_active |= IFM_AUTO;
    723 	switch (ic->ic_opmode) {
    724 	case IEEE80211_M_STA:
    725 		break;
    726 	case IEEE80211_M_IBSS:
    727 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
    728 		break;
    729 	case IEEE80211_M_AHDEMO:
    730 		/* should not come here */
    731 		break;
    732 	case IEEE80211_M_HOSTAP:
    733 		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
    734 		break;
    735 	case IEEE80211_M_MONITOR:
    736 		imr->ifm_active |= IFM_IEEE80211_MONITOR;
    737 		break;
    738 	}
    739 	switch (ic->ic_curmode) {
    740 	case IEEE80211_MODE_11A:
    741 		imr->ifm_active |= IFM_IEEE80211_11A;
    742 		break;
    743 	case IEEE80211_MODE_11B:
    744 		imr->ifm_active |= IFM_IEEE80211_11B;
    745 		break;
    746 	case IEEE80211_MODE_11G:
    747 		imr->ifm_active |= IFM_IEEE80211_11G;
    748 		break;
    749 	case IEEE80211_MODE_FH:
    750 		imr->ifm_active |= IFM_IEEE80211_FH;
    751 		break;
    752 	case IEEE80211_MODE_TURBO_A:
    753 		imr->ifm_active |= IFM_IEEE80211_11A
    754 				|  IFM_IEEE80211_TURBO;
    755 		break;
    756 	case IEEE80211_MODE_TURBO_G:
    757 		imr->ifm_active |= IFM_IEEE80211_11G
    758 				|  IFM_IEEE80211_TURBO;
    759 		break;
    760 	}
    761 }
    762 
    763 void
    764 ieee80211_watchdog(struct ieee80211com *ic)
    765 {
    766 	struct ieee80211_node_table *nt;
    767 	int need_inact_timer = 0;
    768 
    769 	if (ic->ic_state != IEEE80211_S_INIT) {
    770 		if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0)
    771 			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
    772 		nt = &ic->ic_scan;
    773 		if (nt->nt_inact_timer) {
    774 			if (--nt->nt_inact_timer == 0)
    775 				nt->nt_timeout(nt);
    776 			need_inact_timer += nt->nt_inact_timer;
    777 		}
    778 		nt = &ic->ic_sta;
    779 		if (nt->nt_inact_timer) {
    780 			if (--nt->nt_inact_timer == 0)
    781 				nt->nt_timeout(nt);
    782 			need_inact_timer += nt->nt_inact_timer;
    783 		}
    784 	}
    785 	if (ic->ic_mgt_timer != 0 || need_inact_timer)
    786 		ic->ic_ifp->if_timer = 1;
    787 }
    788 
    789 /*
    790  * Set the current phy mode and recalculate the active channel
    791  * set based on the available channels for this mode.  Also
    792  * select a new default/current channel if the current one is
    793  * inappropriate for this mode.
    794  */
    795 int
    796 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
    797 {
    798 #define	N(a)	(sizeof(a) / sizeof(a[0]))
    799 	static const u_int chanflags[] = {
    800 		0,			/* IEEE80211_MODE_AUTO */
    801 		IEEE80211_CHAN_A,	/* IEEE80211_MODE_11A */
    802 		IEEE80211_CHAN_B,	/* IEEE80211_MODE_11B */
    803 		IEEE80211_CHAN_PUREG,	/* IEEE80211_MODE_11G */
    804 		IEEE80211_CHAN_FHSS,	/* IEEE80211_MODE_FH */
    805 		IEEE80211_CHAN_T,	/* IEEE80211_MODE_TURBO_A */
    806 		IEEE80211_CHAN_108G,	/* IEEE80211_MODE_TURBO_G */
    807 	};
    808 	struct ieee80211_channel *c;
    809 	u_int modeflags;
    810 	int i;
    811 
    812 	/* validate new mode */
    813 	if ((ic->ic_modecaps & (1<<mode)) == 0) {
    814 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
    815 			"%s: mode %u not supported (caps 0x%x)\n",
    816 			__func__, mode, ic->ic_modecaps);
    817 		return EINVAL;
    818 	}
    819 
    820 	/*
    821 	 * Verify at least one channel is present in the available
    822 	 * channel list before committing to the new mode.
    823 	 */
    824 	IASSERT(mode < N(chanflags), ("Unexpected mode %u", mode));
    825 	modeflags = chanflags[mode];
    826 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
    827 		c = &ic->ic_channels[i];
    828 		if (mode == IEEE80211_MODE_AUTO) {
    829 			/* ignore turbo channels for autoselect */
    830 			if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
    831 				break;
    832 		} else {
    833 			if ((c->ic_flags & modeflags) == modeflags)
    834 				break;
    835 		}
    836 	}
    837 	if (i > IEEE80211_CHAN_MAX) {
    838 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
    839 			"%s: no channels found for mode %u\n", __func__, mode);
    840 		return EINVAL;
    841 	}
    842 
    843 	/*
    844 	 * Calculate the active channel set.
    845 	 */
    846 	memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
    847 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
    848 		c = &ic->ic_channels[i];
    849 		if (mode == IEEE80211_MODE_AUTO) {
    850 			/* take anything but pure turbo channels */
    851 			if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
    852 				setbit(ic->ic_chan_active, i);
    853 		} else {
    854 			if ((c->ic_flags & modeflags) == modeflags)
    855 				setbit(ic->ic_chan_active, i);
    856 		}
    857 	}
    858 	/*
    859 	 * If no current/default channel is setup or the current
    860 	 * channel is wrong for the mode then pick the first
    861 	 * available channel from the active list.  This is likely
    862 	 * not the right one.
    863 	 */
    864 	if (ic->ic_ibss_chan == NULL ||
    865 	    isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
    866 		for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
    867 			if (isset(ic->ic_chan_active, i)) {
    868 				ic->ic_ibss_chan = &ic->ic_channels[i];
    869 				break;
    870 			}
    871 		IASSERT(ic->ic_ibss_chan != NULL &&
    872 		    isset(ic->ic_chan_active,
    873 			ieee80211_chan2ieee(ic, ic->ic_ibss_chan)),
    874 		    ("Bad IBSS channel %u",
    875 		     ieee80211_chan2ieee(ic, ic->ic_ibss_chan)));
    876 	}
    877 	/*
    878 	 * If the desired channel is set but no longer valid then reset it.
    879 	 */
    880 	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
    881 	    isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_des_chan)))
    882 		ic->ic_des_chan = IEEE80211_CHAN_ANYC;
    883 
    884 	/*
    885 	 * Do mode-specific rate setup.
    886 	 */
    887 	if (mode == IEEE80211_MODE_11G) {
    888 		/*
    889 		 * Use a mixed 11b/11g rate set.
    890 		 */
    891 		ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode],
    892 			IEEE80211_MODE_11G);
    893 	} else if (mode == IEEE80211_MODE_11B) {
    894 		/*
    895 		 * Force pure 11b rate set.
    896 		 */
    897 		ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode],
    898 			IEEE80211_MODE_11B);
    899 	}
    900 	/*
    901 	 * Setup an initial rate set according to the
    902 	 * current/default channel selected above.  This
    903 	 * will be changed when scanning but must exist
    904 	 * now so driver have a consistent state of ic_ibss_chan.
    905 	 */
    906 	if (ic->ic_bss)		/* NB: can be called before lateattach */
    907 		ic->ic_bss->ni_rates = ic->ic_sup_rates[mode];
    908 
    909 	ic->ic_curmode = mode;
    910 	ieee80211_reset_erp(ic);	/* reset ERP state */
    911 	ieee80211_wme_initparams(ic);	/* reset WME stat */
    912 
    913 	return 0;
    914 #undef N
    915 }
    916 
    917 /*
    918  * Return the phy mode for with the specified channel so the
    919  * caller can select a rate set.  This is problematic for channels
    920  * where multiple operating modes are possible (e.g. 11g+11b).
    921  * In those cases we defer to the current operating mode when set.
    922  */
    923 enum ieee80211_phymode
    924 ieee80211_chan2mode(struct ieee80211com *ic, struct ieee80211_channel *chan)
    925 {
    926 	if (IEEE80211_IS_CHAN_5GHZ(chan)) {
    927 		/*
    928 		 * This assumes all 11a turbo channels are also
    929 		 * usable withut turbo, which is currently true.
    930 		 */
    931 		if (ic->ic_curmode == IEEE80211_MODE_TURBO_A)
    932 			return IEEE80211_MODE_TURBO_A;
    933 		return IEEE80211_MODE_11A;
    934 	} else if (IEEE80211_IS_CHAN_FHSS(chan))
    935 		return IEEE80211_MODE_FH;
    936 	else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN)) {
    937 		/*
    938 		 * This assumes all 11g channels are also usable
    939 		 * for 11b, which is currently true.
    940 		 */
    941 		if (ic->ic_curmode == IEEE80211_MODE_TURBO_G)
    942 			return IEEE80211_MODE_TURBO_G;
    943 		if (ic->ic_curmode == IEEE80211_MODE_11B)
    944 			return IEEE80211_MODE_11B;
    945 		return IEEE80211_MODE_11G;
    946 	} else
    947 		return IEEE80211_MODE_11B;
    948 }
    949 
    950 /*
    951  * convert IEEE80211 rate value to ifmedia subtype.
    952  * ieee80211 rate is in unit of 0.5Mbps.
    953  */
    954 int
    955 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
    956 {
    957 #define	N(a)	(sizeof(a) / sizeof(a[0]))
    958 	static const struct {
    959 		u_int	m;	/* rate + mode */
    960 		u_int	r;	/* if_media rate */
    961 	} rates[] = {
    962 		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
    963 		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
    964 		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
    965 		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
    966 		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
    967 		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
    968 		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
    969 		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
    970 		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
    971 		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
    972 		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
    973 		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
    974 		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
    975 		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
    976 		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
    977 		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
    978 		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
    979 		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
    980 		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
    981 		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
    982 		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
    983 		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
    984 		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
    985 		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
    986 		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
    987 		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
    988 		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
    989 		/* NB: OFDM72 doesn't realy exist so we don't handle it */
    990 	};
    991 	u_int mask, i;
    992 
    993 	mask = rate & IEEE80211_RATE_VAL;
    994 	switch (mode) {
    995 	case IEEE80211_MODE_11A:
    996 	case IEEE80211_MODE_TURBO_A:
    997 		mask |= IFM_IEEE80211_11A;
    998 		break;
    999 	case IEEE80211_MODE_11B:
   1000 		mask |= IFM_IEEE80211_11B;
   1001 		break;
   1002 	case IEEE80211_MODE_FH:
   1003 		mask |= IFM_IEEE80211_FH;
   1004 		break;
   1005 	case IEEE80211_MODE_AUTO:
   1006 		/* NB: ic may be NULL for some drivers */
   1007 		if (ic && ic->ic_phytype == IEEE80211_T_FH) {
   1008 			mask |= IFM_IEEE80211_FH;
   1009 			break;
   1010 		}
   1011 		/* NB: hack, 11g matches both 11b+11a rates */
   1012 		/* fall thru... */
   1013 	case IEEE80211_MODE_11G:
   1014 	case IEEE80211_MODE_TURBO_G:
   1015 		mask |= IFM_IEEE80211_11G;
   1016 		break;
   1017 	}
   1018 	for (i = 0; i < N(rates); i++)
   1019 		if (rates[i].m == mask)
   1020 			return rates[i].r;
   1021 	return IFM_AUTO;
   1022 #undef N
   1023 }
   1024 
   1025 int
   1026 ieee80211_media2rate(int mword)
   1027 {
   1028 #define	N(a)	(sizeof(a) / sizeof(a[0]))
   1029 	static const int ieeerates[] = {
   1030 		-1,		/* IFM_AUTO */
   1031 		0,		/* IFM_MANUAL */
   1032 		0,		/* IFM_NONE */
   1033 		2,		/* IFM_IEEE80211_FH1 */
   1034 		4,		/* IFM_IEEE80211_FH2 */
   1035 		2,		/* IFM_IEEE80211_DS1 */
   1036 		4,		/* IFM_IEEE80211_DS2 */
   1037 		11,		/* IFM_IEEE80211_DS5 */
   1038 		22,		/* IFM_IEEE80211_DS11 */
   1039 		44,		/* IFM_IEEE80211_DS22 */
   1040 		12,		/* IFM_IEEE80211_OFDM6 */
   1041 		18,		/* IFM_IEEE80211_OFDM9 */
   1042 		24,		/* IFM_IEEE80211_OFDM12 */
   1043 		36,		/* IFM_IEEE80211_OFDM18 */
   1044 		48,		/* IFM_IEEE80211_OFDM24 */
   1045 		72,		/* IFM_IEEE80211_OFDM36 */
   1046 		96,		/* IFM_IEEE80211_OFDM48 */
   1047 		108,		/* IFM_IEEE80211_OFDM54 */
   1048 		144,		/* IFM_IEEE80211_OFDM72 */
   1049 	};
   1050 	return IFM_SUBTYPE(mword) < N(ieeerates) ?
   1051 		ieeerates[IFM_SUBTYPE(mword)] : 0;
   1052 #undef N
   1053 }
   1054