Home | History | Annotate | Line # | Download | only in net80211
ieee80211.c revision 1.2
      1 /*-
      2  * Copyright (c) 2001 Atsushi Onoe
      3  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * Alternatively, this software may be distributed under the terms of the
     18  * GNU General Public License ("GPL") version 2 as published by the Free
     19  * Software Foundation.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211.c,v 1.7 2003/08/13 22:09:44 sam Exp $");
     35 
     36 /*
     37  * IEEE 802.11 generic handler
     38  */
     39 
     40 #include "opt_inet.h"
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/mbuf.h>
     45 #include <sys/malloc.h>
     46 #include <sys/kernel.h>
     47 #include <sys/socket.h>
     48 #include <sys/sockio.h>
     49 #include <sys/endian.h>
     50 #include <sys/errno.h>
     51 #include <sys/bus.h>
     52 #include <sys/proc.h>
     53 #include <sys/sysctl.h>
     54 
     55 #ifdef __FreeBSD__
     56 #include <machine/atomic.h>
     57 #endif
     58 
     59 #include <net/if.h>
     60 #include <net/if_dl.h>
     61 #include <net/if_media.h>
     62 #include <net/if_arp.h>
     63 #ifdef __FreeBSD__
     64 #include <net/ethernet.h>
     65 #endif
     66 #include <net/if_llc.h>
     67 
     68 #include <net80211/ieee80211_var.h>
     69 
     70 #include <net/bpf.h>
     71 
     72 #ifdef INET
     73 #include <netinet/in.h>
     74 #include <netinet/if_ether.h>
     75 #endif
     76 
     77 #ifdef IEEE80211_DEBUG
     78 int	ieee80211_debug = 0;
     79 SYSCTL_INT(_debug, OID_AUTO, ieee80211, CTLFLAG_RW, &ieee80211_debug,
     80 	    0, "IEEE 802.11 media debugging printfs");
     81 #endif
     82 
     83 static void ieee80211_set11gbasicrates(struct ieee80211_rateset *,
     84 		enum ieee80211_phymode);
     85 
     86 static const char *ieee80211_phymode_name[] = {
     87 	"auto",		/* IEEE80211_MODE_AUTO */
     88 	"11a",		/* IEEE80211_MODE_11A */
     89 	"11b",		/* IEEE80211_MODE_11B */
     90 	"11g",		/* IEEE80211_MODE_11G */
     91 	"turbo",	/* IEEE80211_MODE_TURBO	*/
     92 };
     93 
     94 void
     95 ieee80211_ifattach(struct ifnet *ifp)
     96 {
     97 	struct ieee80211com *ic = (void *)ifp;
     98 	struct ieee80211_channel *c;
     99 	int i;
    100 
    101 	ether_ifattach(ifp, ic->ic_myaddr);
    102 	bpfattach2(ifp, DLT_IEEE802_11,
    103 	    sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf);
    104 	ieee80211_crypto_attach(ifp);
    105 
    106 	/*
    107 	 * Fill in 802.11 available channel set, mark
    108 	 * all available channels as active, and pick
    109 	 * a default channel if not already specified.
    110 	 */
    111 	memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
    112 	ic->ic_modecaps |= 1<<IEEE80211_MODE_AUTO;
    113 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
    114 		c = &ic->ic_channels[i];
    115 		if (c->ic_flags) {
    116 			/*
    117 			 * Verify driver passed us valid data.
    118 			 */
    119 			if (i != ieee80211_chan2ieee(ic, c)) {
    120 				if_printf(ifp, "bad channel ignored; "
    121 					"freq %u flags %x number %u\n",
    122 					c->ic_freq, c->ic_flags, i);
    123 				c->ic_flags = 0;	/* NB: remove */
    124 				continue;
    125 			}
    126 			setbit(ic->ic_chan_avail, i);
    127 			/*
    128 			 * Identify mode capabilities.
    129 			 */
    130 			if (IEEE80211_IS_CHAN_A(c))
    131 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11A;
    132 			if (IEEE80211_IS_CHAN_B(c))
    133 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11B;
    134 			if (IEEE80211_IS_CHAN_PUREG(c))
    135 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11G;
    136 			if (IEEE80211_IS_CHAN_T(c))
    137 				ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO;
    138 		}
    139 	}
    140 	/* validate ic->ic_curmode */
    141 	if ((ic->ic_modecaps & (1<<ic->ic_curmode)) == 0)
    142 		ic->ic_curmode = IEEE80211_MODE_AUTO;
    143 
    144 	(void) ieee80211_setmode(ic, ic->ic_curmode);
    145 
    146 	ic->ic_des_chan = IEEE80211_CHAN_ANYC;	/* any channel is ok */
    147 	if (ic->ic_lintval == 0)
    148 		ic->ic_lintval = 100;		/* default sleep */
    149 	ic->ic_bmisstimeout = 7*ic->ic_lintval;	/* default 7 beacons */
    150 
    151 	ieee80211_node_attach(ifp);
    152 	ieee80211_proto_attach(ifp);
    153 }
    154 
    155 void
    156 ieee80211_ifdetach(struct ifnet *ifp)
    157 {
    158 	struct ieee80211com *ic = (void *)ifp;
    159 
    160 	ieee80211_proto_detach(ifp);
    161 	ieee80211_crypto_detach(ifp);
    162 	ieee80211_node_detach(ifp);
    163 	ifmedia_removeall(&ic->ic_media);
    164 	bpfdetach(ifp);
    165 	ether_ifdetach(ifp);
    166 }
    167 
    168 /*
    169  * Convert MHz frequency to IEEE channel number.
    170  */
    171 u_int
    172 ieee80211_mhz2ieee(u_int freq, u_int flags)
    173 {
    174 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
    175 		if (freq == 2484)
    176 			return 14;
    177 		if (freq < 2484)
    178 			return (freq - 2407) / 5;
    179 		else
    180 			return 15 + ((freq - 2512) / 20);
    181 	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
    182 		return (freq - 5000) / 5;
    183 	} else {				/* either, guess */
    184 		if (freq == 2484)
    185 			return 14;
    186 		if (freq < 2484)
    187 			return (freq - 2407) / 5;
    188 		if (freq < 5000)
    189 			return 15 + ((freq - 2512) / 20);
    190 		return (freq - 5000) / 5;
    191 	}
    192 }
    193 
    194 /*
    195  * Convert channel to IEEE channel number.
    196  */
    197 u_int
    198 ieee80211_chan2ieee(struct ieee80211com *ic, struct ieee80211_channel *c)
    199 {
    200 	if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX])
    201 		return c - ic->ic_channels;
    202 	else if (c == IEEE80211_CHAN_ANYC)
    203 		return IEEE80211_CHAN_ANY;
    204 	else if (c != NULL) {
    205 		if_printf(&ic->ic_if, "invalid channel freq %u flags %x\n",
    206 			c->ic_freq, c->ic_flags);
    207 		return 0;		/* XXX */
    208 	} else {
    209 		if_printf(&ic->ic_if, "invalid channel (NULL)\n");
    210 		return 0;		/* XXX */
    211 	}
    212 }
    213 
    214 /*
    215  * Convert IEEE channel number to MHz frequency.
    216  */
    217 u_int
    218 ieee80211_ieee2mhz(u_int chan, u_int flags)
    219 {
    220 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
    221 		if (chan == 14)
    222 			return 2484;
    223 		if (chan < 14)
    224 			return 2407 + chan*5;
    225 		else
    226 			return 2512 + ((chan-15)*20);
    227 	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
    228 		return 5000 + (chan*5);
    229 	} else {				/* either, guess */
    230 		if (chan == 14)
    231 			return 2484;
    232 		if (chan < 14)			/* 0-13 */
    233 			return 2407 + chan*5;
    234 		if (chan < 27)			/* 15-26 */
    235 			return 2512 + ((chan-15)*20);
    236 		return 5000 + (chan*5);
    237 	}
    238 }
    239 
    240 /*
    241  * Setup the media data structures according to the channel and
    242  * rate tables.  This must be called by the driver after
    243  * ieee80211_attach and before most anything else.
    244  */
    245 void
    246 ieee80211_media_init(struct ifnet *ifp,
    247 	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
    248 {
    249 #define	ADD(_ic, _s, _o) \
    250 	ifmedia_add(&(_ic)->ic_media, \
    251 		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
    252 	struct ieee80211com *ic = (void *)ifp;
    253 	struct ifmediareq imr;
    254 	int i, j, mode, rate, maxrate, mword, mopt, r;
    255 	struct ieee80211_rateset *rs;
    256 	struct ieee80211_rateset allrates;
    257 
    258 	/*
    259 	 * Do late attach work that must wait for any subclass
    260 	 * (i.e. driver) work such as overriding methods.
    261 	 */
    262 	ieee80211_node_lateattach(ifp);
    263 
    264 	/*
    265 	 * Fill in media characteristics.
    266 	 */
    267 	ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
    268 	maxrate = 0;
    269 	memset(&allrates, 0, sizeof(allrates));
    270 	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) {
    271 		static const u_int mopts[] = {
    272 			IFM_AUTO,
    273 			IFM_MAKEMODE(IFM_IEEE80211_11A),
    274 			IFM_MAKEMODE(IFM_IEEE80211_11B),
    275 			IFM_MAKEMODE(IFM_IEEE80211_11G),
    276 			IFM_MAKEMODE(IFM_IEEE80211_11A) | IFM_IEEE80211_TURBO,
    277 		};
    278 		if ((ic->ic_modecaps & (1<<mode)) == 0)
    279 			continue;
    280 		mopt = mopts[mode];
    281 		ADD(ic, IFM_AUTO, mopt);	/* e.g. 11a auto */
    282 		if (ic->ic_caps & IEEE80211_C_IBSS)
    283 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC);
    284 		if (ic->ic_caps & IEEE80211_C_HOSTAP)
    285 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP);
    286 		if (ic->ic_caps & IEEE80211_C_AHDEMO)
    287 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
    288 		if (ic->ic_caps & IEEE80211_C_MONITOR)
    289 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR);
    290 		if (mode == IEEE80211_MODE_AUTO)
    291 			continue;
    292 		if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
    293 		rs = &ic->ic_sup_rates[mode];
    294 		for (i = 0; i < rs->rs_nrates; i++) {
    295 			rate = rs->rs_rates[i];
    296 			mword = ieee80211_rate2media(ic, rate, mode);
    297 			if (mword == 0)
    298 				continue;
    299 			printf("%s%d%sMbps", (i != 0 ? " " : ""),
    300 			    (rate & IEEE80211_RATE_VAL) / 2,
    301 			    ((rate & 0x1) != 0 ? ".5" : ""));
    302 			ADD(ic, mword, mopt);
    303 			if (ic->ic_caps & IEEE80211_C_IBSS)
    304 				ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
    305 			if (ic->ic_caps & IEEE80211_C_HOSTAP)
    306 				ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
    307 			if (ic->ic_caps & IEEE80211_C_AHDEMO)
    308 				ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
    309 			if (ic->ic_caps & IEEE80211_C_MONITOR)
    310 				ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
    311 			/*
    312 			 * Add rate to the collection of all rates.
    313 			 */
    314 			r = rate & IEEE80211_RATE_VAL;
    315 			for (j = 0; j < allrates.rs_nrates; j++)
    316 				if (allrates.rs_rates[j] == r)
    317 					break;
    318 			if (j == allrates.rs_nrates) {
    319 				/* unique, add to the set */
    320 				allrates.rs_rates[j] = r;
    321 				allrates.rs_nrates++;
    322 			}
    323 			rate = (rate & IEEE80211_RATE_VAL) / 2;
    324 			if (rate > maxrate)
    325 				maxrate = rate;
    326 		}
    327 		printf("\n");
    328 	}
    329 	for (i = 0; i < allrates.rs_nrates; i++) {
    330 		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
    331 				IEEE80211_MODE_AUTO);
    332 		if (mword == 0)
    333 			continue;
    334 		mword = IFM_SUBTYPE(mword);	/* remove media options */
    335 		ADD(ic, mword, 0);
    336 		if (ic->ic_caps & IEEE80211_C_IBSS)
    337 			ADD(ic, mword, IFM_IEEE80211_ADHOC);
    338 		if (ic->ic_caps & IEEE80211_C_HOSTAP)
    339 			ADD(ic, mword, IFM_IEEE80211_HOSTAP);
    340 		if (ic->ic_caps & IEEE80211_C_AHDEMO)
    341 			ADD(ic, mword, IFM_IEEE80211_ADHOC | IFM_FLAG0);
    342 		if (ic->ic_caps & IEEE80211_C_MONITOR)
    343 			ADD(ic, mword, IFM_IEEE80211_MONITOR);
    344 	}
    345 	ieee80211_media_status(ifp, &imr);
    346 	ifmedia_set(&ic->ic_media, imr.ifm_active);
    347 
    348 	if (maxrate)
    349 		ifp->if_baudrate = IF_Mbps(maxrate);
    350 #undef ADD
    351 }
    352 
    353 static int
    354 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
    355 {
    356 #define	IEEERATE(_ic,_m,_i) \
    357 	((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
    358 	int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
    359 	for (i = 0; i < nrates; i++)
    360 		if (IEEERATE(ic, mode, i) == rate)
    361 			return i;
    362 	return -1;
    363 #undef IEEERATE
    364 }
    365 
    366 /*
    367  * Handle a media change request.
    368  */
    369 int
    370 ieee80211_media_change(struct ifnet *ifp)
    371 {
    372 	struct ieee80211com *ic = (void *)ifp;
    373 	struct ifmedia_entry *ime;
    374 	enum ieee80211_opmode newopmode;
    375 	enum ieee80211_phymode newphymode;
    376 	int i, j, newrate, error = 0;
    377 
    378 	ime = ic->ic_media.ifm_cur;
    379 	/*
    380 	 * First, identify the phy mode.
    381 	 */
    382 	switch (IFM_MODE(ime->ifm_media)) {
    383 	case IFM_IEEE80211_11A:
    384 		newphymode = IEEE80211_MODE_11A;
    385 		break;
    386 	case IFM_IEEE80211_11B:
    387 		newphymode = IEEE80211_MODE_11B;
    388 		break;
    389 	case IFM_IEEE80211_11G:
    390 		newphymode = IEEE80211_MODE_11G;
    391 		break;
    392 	case IFM_AUTO:
    393 		newphymode = IEEE80211_MODE_AUTO;
    394 		break;
    395 	default:
    396 		return EINVAL;
    397 	}
    398 	/*
    399 	 * Turbo mode is an ``option''.  Eventually it
    400 	 * needs to be applied to 11g too.
    401 	 */
    402 	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
    403 		if (newphymode != IEEE80211_MODE_11A)
    404 			return EINVAL;
    405 		newphymode = IEEE80211_MODE_TURBO;
    406 	}
    407 	/*
    408 	 * Validate requested mode is available.
    409 	 */
    410 	if ((ic->ic_modecaps & (1<<newphymode)) == 0)
    411 		return EINVAL;
    412 
    413 	/*
    414 	 * Next, the fixed/variable rate.
    415 	 */
    416 	i = -1;
    417 	if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
    418 		/*
    419 		 * Convert media subtype to rate.
    420 		 */
    421 		newrate = ieee80211_media2rate(ime->ifm_media);
    422 		if (newrate == 0)
    423 			return EINVAL;
    424 		/*
    425 		 * Check the rate table for the specified/current phy.
    426 		 */
    427 		if (newphymode == IEEE80211_MODE_AUTO) {
    428 			/*
    429 			 * In autoselect mode search for the rate.
    430 			 */
    431 			for (j = IEEE80211_MODE_11A;
    432 			     j < IEEE80211_MODE_MAX; j++) {
    433 				if ((ic->ic_modecaps & (1<<j)) == 0)
    434 					continue;
    435 				i = findrate(ic, j, newrate);
    436 				if (i != -1) {
    437 					/* lock mode too */
    438 					newphymode = j;
    439 					break;
    440 				}
    441 			}
    442 		} else {
    443 			i = findrate(ic, newphymode, newrate);
    444 		}
    445 		if (i == -1)			/* mode/rate mismatch */
    446 			return EINVAL;
    447 	}
    448 	/* NB: defer rate setting to later */
    449 
    450 	/*
    451 	 * Deduce new operating mode but don't install it just yet.
    452 	 */
    453 	if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) ==
    454 	    (IFM_IEEE80211_ADHOC|IFM_FLAG0))
    455 		newopmode = IEEE80211_M_AHDEMO;
    456 	else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
    457 		newopmode = IEEE80211_M_HOSTAP;
    458 	else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
    459 		newopmode = IEEE80211_M_IBSS;
    460 	else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
    461 		newopmode = IEEE80211_M_MONITOR;
    462 	else
    463 		newopmode = IEEE80211_M_STA;
    464 
    465 	/*
    466 	 * Autoselect doesn't make sense when operating as an AP.
    467 	 * If no phy mode has been selected, pick one and lock it
    468 	 * down so rate tables can be used in forming beacon frames
    469 	 * and the like.
    470 	 */
    471 	if (newopmode == IEEE80211_M_HOSTAP &&
    472 	    newphymode == IEEE80211_MODE_AUTO) {
    473 		for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++)
    474 			if (ic->ic_modecaps & (1<<j)) {
    475 				newphymode = j;
    476 				break;
    477 			}
    478 	}
    479 
    480 	/*
    481 	 * Handle phy mode change.
    482 	 */
    483 	if (ic->ic_curmode != newphymode) {		/* change phy mode */
    484 		error = ieee80211_setmode(ic, newphymode);
    485 		if (error != 0)
    486 			return error;
    487 		error = ENETRESET;
    488 	}
    489 
    490 	/*
    491 	 * Committed to changes, install the rate setting.
    492 	 */
    493 	if (ic->ic_fixed_rate != i) {
    494 		ic->ic_fixed_rate = i;			/* set fixed tx rate */
    495 		error = ENETRESET;
    496 	}
    497 
    498 	/*
    499 	 * Handle operating mode change.
    500 	 */
    501 	if (ic->ic_opmode != newopmode) {
    502 		ic->ic_opmode = newopmode;
    503 		switch (newopmode) {
    504 		case IEEE80211_M_AHDEMO:
    505 		case IEEE80211_M_HOSTAP:
    506 		case IEEE80211_M_STA:
    507 		case IEEE80211_M_MONITOR:
    508 			ic->ic_flags &= ~IEEE80211_F_IBSSON;
    509 			break;
    510 		case IEEE80211_M_IBSS:
    511 			ic->ic_flags |= IEEE80211_F_IBSSON;
    512 #ifdef notdef
    513 			if (ic->ic_curmode == IEEE80211_MODE_11G)
    514 				ieee80211_set11gbasicrates(
    515 					&ic->ic_suprates[newphymode],
    516 					IEEE80211_MODE_11B);
    517 #endif
    518 			break;
    519 		}
    520 		error = ENETRESET;
    521 	}
    522 #ifdef notdef
    523 	if (error == 0)
    524 		ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
    525 #endif
    526 	return error;
    527 }
    528 
    529 void
    530 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
    531 {
    532 	struct ieee80211com *ic = (void *)ifp;
    533 	struct ieee80211_node *ni = NULL;
    534 
    535 	imr->ifm_status = IFM_AVALID;
    536 	imr->ifm_active = IFM_IEEE80211;
    537 	if (ic->ic_state == IEEE80211_S_RUN)
    538 		imr->ifm_status |= IFM_ACTIVE;
    539 	imr->ifm_active |= IFM_AUTO;
    540 	switch (ic->ic_opmode) {
    541 	case IEEE80211_M_STA:
    542 		ni = ic->ic_bss;
    543 		/* calculate rate subtype */
    544 		imr->ifm_active |= ieee80211_rate2media(ic,
    545 			ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode);
    546 		break;
    547 	case IEEE80211_M_IBSS:
    548 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
    549 		break;
    550 	case IEEE80211_M_AHDEMO:
    551 		/* should not come here */
    552 		break;
    553 	case IEEE80211_M_HOSTAP:
    554 		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
    555 		break;
    556 	case IEEE80211_M_MONITOR:
    557 		imr->ifm_active |= IFM_IEEE80211_MONITOR;
    558 		break;
    559 	}
    560 	switch (ic->ic_curmode) {
    561 	case IEEE80211_MODE_11A:
    562 		imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11A);
    563 		break;
    564 	case IEEE80211_MODE_11B:
    565 		imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11B);
    566 		break;
    567 	case IEEE80211_MODE_11G:
    568 		imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11G);
    569 		break;
    570 	case IEEE80211_MODE_TURBO:
    571 		imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11A)
    572 				|  IFM_IEEE80211_TURBO;
    573 		break;
    574 	}
    575 }
    576 
    577 void
    578 ieee80211_watchdog(struct ifnet *ifp)
    579 {
    580 	struct ieee80211com *ic = (void *)ifp;
    581 
    582 	if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0)
    583 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
    584 	if (ic->ic_inact_timer && --ic->ic_inact_timer == 0)
    585 		ieee80211_timeout_nodes(ic);
    586 
    587 	if (ic->ic_mgt_timer != 0 || ic->ic_inact_timer != 0)
    588 		ifp->if_timer = 1;
    589 }
    590 
    591 /*
    592  * Mark the basic rates for the 11g rate table based on the
    593  * operating mode.  For real 11g we mark all the 11b rates
    594  * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
    595  * 11b rates.  There's also a pseudo 11a-mode used to mark only
    596  * the basic OFDM rates.
    597  */
    598 static void
    599 ieee80211_set11gbasicrates(struct ieee80211_rateset *rs, enum ieee80211_phymode mode)
    600 {
    601 	static const struct ieee80211_rateset basic[] = {
    602 	    { 3, { 12, 24, 48 } },		/* IEEE80211_MODE_11A */
    603 	    { 4, { 2, 4, 11, 22 } },		/* IEEE80211_MODE_11B */
    604 	    { 7, { 2, 4, 11, 22, 12, 24, 48 } },/* IEEE80211_MODE_11G */
    605 	    { 0 },				/* IEEE80211_MODE_TURBO	*/
    606 	};
    607 	int i, j;
    608 
    609 	for (i = 0; i < rs->rs_nrates; i++) {
    610 		rs->rs_rates[i] &= IEEE80211_RATE_VAL;
    611 		for (j = 0; j < basic[mode].rs_nrates; j++)
    612 			if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
    613 				rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
    614 				break;
    615 			}
    616 	}
    617 }
    618 
    619 /*
    620  * Set the current phy mode and recalculate the active channel
    621  * set based on the available channels for this mode.  Also
    622  * select a new default/current channel if the current one is
    623  * inappropriate for this mode.
    624  */
    625 int
    626 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
    627 {
    628 #define	N(a)	(sizeof(a) / sizeof(a[0]))
    629 	static const u_int chanflags[] = {
    630 		0,			/* IEEE80211_MODE_AUTO */
    631 		IEEE80211_CHAN_A,	/* IEEE80211_MODE_11A */
    632 		IEEE80211_CHAN_B,	/* IEEE80211_MODE_11B */
    633 		IEEE80211_CHAN_PUREG,	/* IEEE80211_MODE_11G */
    634 		IEEE80211_CHAN_T,	/* IEEE80211_MODE_TURBO	*/
    635 	};
    636 	struct ieee80211_channel *c;
    637 	u_int modeflags;
    638 	int i;
    639 
    640 	/* validate new mode */
    641 	if ((ic->ic_modecaps & (1<<mode)) == 0) {
    642 		IEEE80211_DPRINTF(("%s: mode %u not supported (caps 0x%x)\n",
    643 			__func__, mode, ic->ic_modecaps));
    644 		return EINVAL;
    645 	}
    646 
    647 	/*
    648 	 * Verify at least one channel is present in the available
    649 	 * channel list before committing to the new mode.
    650 	 */
    651 	KASSERT(mode < N(chanflags), ("Unexpected mode %u\n", mode));
    652 	modeflags = chanflags[mode];
    653 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
    654 		c = &ic->ic_channels[i];
    655 		if (mode == IEEE80211_MODE_AUTO) {
    656 			/* ignore turbo channels for autoselect */
    657 			if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
    658 				break;
    659 		} else {
    660 			if ((c->ic_flags & modeflags) == modeflags)
    661 				break;
    662 		}
    663 	}
    664 	if (i > IEEE80211_CHAN_MAX) {
    665 		IEEE80211_DPRINTF(("%s: no channels found for mode %u\n",
    666 			__func__, mode));
    667 		return EINVAL;
    668 	}
    669 
    670 	/*
    671 	 * Calculate the active channel set.
    672 	 */
    673 	memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
    674 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
    675 		c = &ic->ic_channels[i];
    676 		if (mode == IEEE80211_MODE_AUTO) {
    677 			/* take anything but pure turbo channels */
    678 			if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
    679 				setbit(ic->ic_chan_active, i);
    680 		} else {
    681 			if ((c->ic_flags & modeflags) == modeflags)
    682 				setbit(ic->ic_chan_active, i);
    683 		}
    684 	}
    685 	/*
    686 	 * If no current/default channel is setup or the current
    687 	 * channel is wrong for the mode then pick the first
    688 	 * available channel from the active list.  This is likely
    689 	 * not the right one.
    690 	 */
    691 	if (ic->ic_ibss_chan == NULL ||
    692 	    isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
    693 		for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
    694 			if (isset(ic->ic_chan_active, i)) {
    695 				ic->ic_ibss_chan = &ic->ic_channels[i];
    696 				break;
    697 			}
    698 	}
    699 
    700 	/*
    701 	 * Set/reset state flags that influence beacon contents, etc.
    702 	 *
    703 	 * XXX what if we have stations already associated???
    704 	 * XXX probably not right for autoselect?
    705 	 */
    706 	if (mode == IEEE80211_MODE_11G) {
    707 		if (ic->ic_caps & IEEE80211_C_SHSLOT)
    708 			ic->ic_flags |= IEEE80211_F_SHSLOT;
    709 		if (ic->ic_caps & IEEE80211_C_SHPREAMBLE)
    710 			ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
    711 		ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode],
    712 			IEEE80211_MODE_11G);
    713 	} else {
    714 		ic->ic_flags &= ~(IEEE80211_F_SHSLOT | IEEE80211_F_SHPREAMBLE);
    715 	}
    716 
    717 	ic->ic_curmode = mode;
    718 	return 0;
    719 #undef N
    720 }
    721 
    722 /*
    723  * Return the phy mode for with the specified channel so the
    724  * caller can select a rate set.  This is problematic and the
    725  * work here assumes how things work elsewhere in this code.
    726  */
    727 enum ieee80211_phymode
    728 ieee80211_chan2mode(struct ieee80211com *ic, struct ieee80211_channel *chan)
    729 {
    730 	/*
    731 	 * NB: this assumes the channel would not be supplied to us
    732 	 *     unless it was already compatible with the current mode.
    733 	 */
    734 	if (ic->ic_curmode != IEEE80211_MODE_AUTO)
    735 		return ic->ic_curmode;
    736 	/*
    737 	 * In autoselect mode; deduce a mode based on the channel
    738 	 * characteristics.  We assume that turbo-only channels
    739 	 * are not considered when the channel set is constructed.
    740 	 */
    741 	if (IEEE80211_IS_CHAN_5GHZ(chan))
    742 		return IEEE80211_MODE_11A;
    743 	else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN))
    744 		return IEEE80211_MODE_11G;
    745 	else
    746 		return IEEE80211_MODE_11B;
    747 }
    748 
    749 /*
    750  * convert IEEE80211 rate value to ifmedia subtype.
    751  * ieee80211 rate is in unit of 0.5Mbps.
    752  */
    753 int
    754 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
    755 {
    756 #define	N(a)	(sizeof(a) / sizeof(a[0]))
    757 	static const struct {
    758 		u_int	m;	/* rate + mode */
    759 		u_int	r;	/* if_media rate */
    760 	} rates[] = {
    761 		{   2 | IFM_MAKEMODE(IFM_IEEE80211_11B), IFM_IEEE80211_DS1 },
    762 		{   4 | IFM_MAKEMODE(IFM_IEEE80211_11B), IFM_IEEE80211_DS2 },
    763 		{  11 | IFM_MAKEMODE(IFM_IEEE80211_11B), IFM_IEEE80211_DS5 },
    764 		{  22 | IFM_MAKEMODE(IFM_IEEE80211_11B), IFM_IEEE80211_DS11 },
    765 		{  44 | IFM_MAKEMODE(IFM_IEEE80211_11B), IFM_IEEE80211_DS22 },
    766 		{  12 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM6 },
    767 		{  18 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM9 },
    768 		{  24 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM12 },
    769 		{  36 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM18 },
    770 		{  48 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM24 },
    771 		{  72 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM36 },
    772 		{  96 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM48 },
    773 		{ 108 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM54 },
    774 		{   2 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_DS1 },
    775 		{   4 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_DS2 },
    776 		{  11 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_DS5 },
    777 		{  22 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_DS11 },
    778 		{  12 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM6 },
    779 		{  18 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM9 },
    780 		{  24 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM12 },
    781 		{  36 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM18 },
    782 		{  48 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM24 },
    783 		{  72 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM36 },
    784 		{  96 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM48 },
    785 		{ 108 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM54 },
    786 		/* NB: OFDM72 doesn't realy exist so we don't handle it */
    787 	};
    788 	u_int mask, i;
    789 
    790 	mask = rate & IEEE80211_RATE_VAL;
    791 	switch (mode) {
    792 	case IEEE80211_MODE_11A:
    793 	case IEEE80211_MODE_TURBO:
    794 		mask |= IFM_MAKEMODE(IFM_IEEE80211_11A);
    795 		break;
    796 	case IEEE80211_MODE_11B:
    797 		mask |= IFM_MAKEMODE(IFM_IEEE80211_11B);
    798 		break;
    799 	case IEEE80211_MODE_AUTO:
    800 		/* NB: ic may be NULL for some drivers */
    801 		if (ic && ic->ic_phytype == IEEE80211_T_FH) {
    802 			/* must handle these specially */
    803 			switch (mask) {
    804 			case 2:		return IFM_IEEE80211_FH1;
    805 			case 4:		return IFM_IEEE80211_FH2;
    806 			}
    807 			return IFM_AUTO;
    808 		}
    809 		/* NB: hack, 11g matches both 11b+11a rates */
    810 		/* fall thru... */
    811 	case IEEE80211_MODE_11G:
    812 		mask |= IFM_MAKEMODE(IFM_IEEE80211_11G);
    813 		break;
    814 	}
    815 	for (i = 0; i < N(rates); i++)
    816 		if (rates[i].m == mask)
    817 			return rates[i].r;
    818 	return IFM_AUTO;
    819 #undef N
    820 }
    821 
    822 int
    823 ieee80211_media2rate(int mword)
    824 {
    825 #define	N(a)	(sizeof(a) / sizeof(a[0]))
    826 	static const int ieeerates[] = {
    827 		-1,		/* IFM_AUTO */
    828 		0,		/* IFM_MANUAL */
    829 		0,		/* IFM_NONE */
    830 		2,		/* IFM_IEEE80211_FH1 */
    831 		4,		/* IFM_IEEE80211_FH2 */
    832 		2,		/* IFM_IEEE80211_DS1 */
    833 		4,		/* IFM_IEEE80211_DS2 */
    834 		11,		/* IFM_IEEE80211_DS5 */
    835 		22,		/* IFM_IEEE80211_DS11 */
    836 		44,		/* IFM_IEEE80211_DS22 */
    837 		12,		/* IFM_IEEE80211_OFDM6 */
    838 		18,		/* IFM_IEEE80211_OFDM9 */
    839 		24,		/* IFM_IEEE80211_OFDM12 */
    840 		36,		/* IFM_IEEE80211_OFDM18 */
    841 		48,		/* IFM_IEEE80211_OFDM24 */
    842 		72,		/* IFM_IEEE80211_OFDM36 */
    843 		96,		/* IFM_IEEE80211_OFDM48 */
    844 		108,		/* IFM_IEEE80211_OFDM54 */
    845 		144,		/* IFM_IEEE80211_OFDM72 */
    846 	};
    847 	return IFM_SUBTYPE(mword) < N(ieeerates) ?
    848 		ieeerates[IFM_SUBTYPE(mword)] : 0;
    849 #undef N
    850 }
    851 
    852 #ifdef __FreeBSD__
    853 /*
    854  * Module glue.
    855  *
    856  * NB: the module name is "wlan" for compatibility with NetBSD.
    857  */
    858 
    859 static int
    860 ieee80211_modevent(module_t mod, int type, void *unused)
    861 {
    862 	switch (type) {
    863 	case MOD_LOAD:
    864 		if (bootverbose)
    865 			printf("wlan: <802.11 Link Layer>\n");
    866 		return 0;
    867 	case MOD_UNLOAD:
    868 		return 0;
    869 	}
    870 	return EINVAL;
    871 }
    872 
    873 static moduledata_t ieee80211_mod = {
    874 	"wlan",
    875 	ieee80211_modevent,
    876 	0
    877 };
    878 DECLARE_MODULE(wlan, ieee80211_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
    879 MODULE_VERSION(wlan, 1);
    880 MODULE_DEPEND(wlan, rc4, 1, 1, 1);
    881 #endif
    882