Home | History | Annotate | Line # | Download | only in net80211
ieee80211.c revision 1.1.1.3
      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.11 2004/04/02 20:19:20 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 #include <machine/atomic.h>
     56 
     57 #include <net/if.h>
     58 #include <net/if_dl.h>
     59 #include <net/if_media.h>
     60 #include <net/if_arp.h>
     61 #include <net/ethernet.h>
     62 #include <net/if_llc.h>
     63 
     64 #include <net80211/ieee80211_var.h>
     65 
     66 #include <net/bpf.h>
     67 
     68 #ifdef INET
     69 #include <netinet/in.h>
     70 #include <netinet/if_ether.h>
     71 #endif
     72 
     73 #ifdef IEEE80211_DEBUG
     74 int	ieee80211_debug = 0;
     75 SYSCTL_INT(_debug, OID_AUTO, ieee80211, CTLFLAG_RW, &ieee80211_debug,
     76 	    0, "IEEE 802.11 media debugging printfs");
     77 #endif
     78 
     79 static void ieee80211_set11gbasicrates(struct ieee80211_rateset *,
     80 		enum ieee80211_phymode);
     81 
     82 static const char *ieee80211_phymode_name[] = {
     83 	"auto",		/* IEEE80211_MODE_AUTO */
     84 	"11a",		/* IEEE80211_MODE_11A */
     85 	"11b",		/* IEEE80211_MODE_11B */
     86 	"11g",		/* IEEE80211_MODE_11G */
     87 	"FH",		/* IEEE80211_MODE_FH */
     88 	"turbo",	/* IEEE80211_MODE_TURBO */
     89 };
     90 
     91 void
     92 ieee80211_ifattach(struct ifnet *ifp)
     93 {
     94 	struct ieee80211com *ic = (void *)ifp;
     95 	struct ieee80211_channel *c;
     96 	int i;
     97 
     98 	ether_ifattach(ifp, ic->ic_myaddr);
     99 	bpfattach2(ifp, DLT_IEEE802_11,
    100 	    sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf);
    101 	ieee80211_crypto_attach(ifp);
    102 
    103 	/*
    104 	 * Fill in 802.11 available channel set, mark
    105 	 * all available channels as active, and pick
    106 	 * a default channel if not already specified.
    107 	 */
    108 	memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
    109 	ic->ic_modecaps |= 1<<IEEE80211_MODE_AUTO;
    110 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
    111 		c = &ic->ic_channels[i];
    112 		if (c->ic_flags) {
    113 			/*
    114 			 * Verify driver passed us valid data.
    115 			 */
    116 			if (i != ieee80211_chan2ieee(ic, c)) {
    117 				if_printf(ifp, "bad channel ignored; "
    118 					"freq %u flags %x number %u\n",
    119 					c->ic_freq, c->ic_flags, i);
    120 				c->ic_flags = 0;	/* NB: remove */
    121 				continue;
    122 			}
    123 			setbit(ic->ic_chan_avail, i);
    124 			/*
    125 			 * Identify mode capabilities.
    126 			 */
    127 			if (IEEE80211_IS_CHAN_A(c))
    128 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11A;
    129 			if (IEEE80211_IS_CHAN_B(c))
    130 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11B;
    131 			if (IEEE80211_IS_CHAN_PUREG(c))
    132 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11G;
    133 			if (IEEE80211_IS_CHAN_FHSS(c))
    134 				ic->ic_modecaps |= 1<<IEEE80211_MODE_FH;
    135 			if (IEEE80211_IS_CHAN_T(c))
    136 				ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO;
    137 		}
    138 	}
    139 	/* validate ic->ic_curmode */
    140 	if ((ic->ic_modecaps & (1<<ic->ic_curmode)) == 0)
    141 		ic->ic_curmode = IEEE80211_MODE_AUTO;
    142 	ic->ic_des_chan = IEEE80211_CHAN_ANYC;	/* any channel is ok */
    143 
    144 	(void) ieee80211_setmode(ic, ic->ic_curmode);
    145 
    146 	if (ic->ic_lintval == 0)
    147 		ic->ic_lintval = 100;		/* default sleep */
    148 	ic->ic_bmisstimeout = 7*ic->ic_lintval;	/* default 7 beacons */
    149 
    150 	ieee80211_node_attach(ifp);
    151 	ieee80211_proto_attach(ifp);
    152 }
    153 
    154 void
    155 ieee80211_ifdetach(struct ifnet *ifp)
    156 {
    157 	struct ieee80211com *ic = (void *)ifp;
    158 
    159 	ieee80211_proto_detach(ifp);
    160 	ieee80211_crypto_detach(ifp);
    161 	ieee80211_node_detach(ifp);
    162 	ifmedia_removeall(&ic->ic_media);
    163 	bpfdetach(ifp);
    164 	ether_ifdetach(ifp);
    165 }
    166 
    167 /*
    168  * Convert MHz frequency to IEEE channel number.
    169  */
    170 u_int
    171 ieee80211_mhz2ieee(u_int freq, u_int flags)
    172 {
    173 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
    174 		if (freq == 2484)
    175 			return 14;
    176 		if (freq < 2484)
    177 			return (freq - 2407) / 5;
    178 		else
    179 			return 15 + ((freq - 2512) / 20);
    180 	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
    181 		return (freq - 5000) / 5;
    182 	} else {				/* either, guess */
    183 		if (freq == 2484)
    184 			return 14;
    185 		if (freq < 2484)
    186 			return (freq - 2407) / 5;
    187 		if (freq < 5000)
    188 			return 15 + ((freq - 2512) / 20);
    189 		return (freq - 5000) / 5;
    190 	}
    191 }
    192 
    193 /*
    194  * Convert channel to IEEE channel number.
    195  */
    196 u_int
    197 ieee80211_chan2ieee(struct ieee80211com *ic, struct ieee80211_channel *c)
    198 {
    199 	if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX])
    200 		return c - ic->ic_channels;
    201 	else if (c == IEEE80211_CHAN_ANYC)
    202 		return IEEE80211_CHAN_ANY;
    203 	else if (c != NULL) {
    204 		if_printf(&ic->ic_if, "invalid channel freq %u flags %x\n",
    205 			c->ic_freq, c->ic_flags);
    206 		return 0;		/* XXX */
    207 	} else {
    208 		if_printf(&ic->ic_if, "invalid channel (NULL)\n");
    209 		return 0;		/* XXX */
    210 	}
    211 }
    212 
    213 /*
    214  * Convert IEEE channel number to MHz frequency.
    215  */
    216 u_int
    217 ieee80211_ieee2mhz(u_int chan, u_int flags)
    218 {
    219 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
    220 		if (chan == 14)
    221 			return 2484;
    222 		if (chan < 14)
    223 			return 2407 + chan*5;
    224 		else
    225 			return 2512 + ((chan-15)*20);
    226 	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
    227 		return 5000 + (chan*5);
    228 	} else {				/* either, guess */
    229 		if (chan == 14)
    230 			return 2484;
    231 		if (chan < 14)			/* 0-13 */
    232 			return 2407 + chan*5;
    233 		if (chan < 27)			/* 15-26 */
    234 			return 2512 + ((chan-15)*20);
    235 		return 5000 + (chan*5);
    236 	}
    237 }
    238 
    239 /*
    240  * Setup the media data structures according to the channel and
    241  * rate tables.  This must be called by the driver after
    242  * ieee80211_attach and before most anything else.
    243  */
    244 void
    245 ieee80211_media_init(struct ifnet *ifp,
    246 	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
    247 {
    248 #define	ADD(_ic, _s, _o) \
    249 	ifmedia_add(&(_ic)->ic_media, \
    250 		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
    251 	struct ieee80211com *ic = (void *)ifp;
    252 	struct ifmediareq imr;
    253 	int i, j, mode, rate, maxrate, mword, mopt, r;
    254 	struct ieee80211_rateset *rs;
    255 	struct ieee80211_rateset allrates;
    256 
    257 	/*
    258 	 * Do late attach work that must wait for any subclass
    259 	 * (i.e. driver) work such as overriding methods.
    260 	 */
    261 	ieee80211_node_lateattach(ifp);
    262 
    263 	/*
    264 	 * Fill in media characteristics.
    265 	 */
    266 	ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
    267 	maxrate = 0;
    268 	memset(&allrates, 0, sizeof(allrates));
    269 	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) {
    270 		static const u_int mopts[] = {
    271 			IFM_AUTO,
    272 			IFM_IEEE80211_11A,
    273 			IFM_IEEE80211_11B,
    274 			IFM_IEEE80211_11G,
    275 			IFM_IEEE80211_FH,
    276 			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_IEEE80211_FH:
    393 		newphymode = IEEE80211_MODE_FH;
    394 		break;
    395 	case IFM_AUTO:
    396 		newphymode = IEEE80211_MODE_AUTO;
    397 		break;
    398 	default:
    399 		return EINVAL;
    400 	}
    401 	/*
    402 	 * Turbo mode is an ``option''.  Eventually it
    403 	 * needs to be applied to 11g too.
    404 	 */
    405 	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
    406 		if (newphymode != IEEE80211_MODE_11A)
    407 			return EINVAL;
    408 		newphymode = IEEE80211_MODE_TURBO;
    409 	}
    410 	/*
    411 	 * Validate requested mode is available.
    412 	 */
    413 	if ((ic->ic_modecaps & (1<<newphymode)) == 0)
    414 		return EINVAL;
    415 
    416 	/*
    417 	 * Next, the fixed/variable rate.
    418 	 */
    419 	i = -1;
    420 	if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
    421 		/*
    422 		 * Convert media subtype to rate.
    423 		 */
    424 		newrate = ieee80211_media2rate(ime->ifm_media);
    425 		if (newrate == 0)
    426 			return EINVAL;
    427 		/*
    428 		 * Check the rate table for the specified/current phy.
    429 		 */
    430 		if (newphymode == IEEE80211_MODE_AUTO) {
    431 			/*
    432 			 * In autoselect mode search for the rate.
    433 			 */
    434 			for (j = IEEE80211_MODE_11A;
    435 			     j < IEEE80211_MODE_MAX; j++) {
    436 				if ((ic->ic_modecaps & (1<<j)) == 0)
    437 					continue;
    438 				i = findrate(ic, j, newrate);
    439 				if (i != -1) {
    440 					/* lock mode too */
    441 					newphymode = j;
    442 					break;
    443 				}
    444 			}
    445 		} else {
    446 			i = findrate(ic, newphymode, newrate);
    447 		}
    448 		if (i == -1)			/* mode/rate mismatch */
    449 			return EINVAL;
    450 	}
    451 	/* NB: defer rate setting to later */
    452 
    453 	/*
    454 	 * Deduce new operating mode but don't install it just yet.
    455 	 */
    456 	if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) ==
    457 	    (IFM_IEEE80211_ADHOC|IFM_FLAG0))
    458 		newopmode = IEEE80211_M_AHDEMO;
    459 	else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
    460 		newopmode = IEEE80211_M_HOSTAP;
    461 	else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
    462 		newopmode = IEEE80211_M_IBSS;
    463 	else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
    464 		newopmode = IEEE80211_M_MONITOR;
    465 	else
    466 		newopmode = IEEE80211_M_STA;
    467 
    468 	/*
    469 	 * Autoselect doesn't make sense when operating as an AP.
    470 	 * If no phy mode has been selected, pick one and lock it
    471 	 * down so rate tables can be used in forming beacon frames
    472 	 * and the like.
    473 	 */
    474 	if (newopmode == IEEE80211_M_HOSTAP &&
    475 	    newphymode == IEEE80211_MODE_AUTO) {
    476 		for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++)
    477 			if (ic->ic_modecaps & (1<<j)) {
    478 				newphymode = j;
    479 				break;
    480 			}
    481 	}
    482 
    483 	/*
    484 	 * Handle phy mode change.
    485 	 */
    486 	if (ic->ic_curmode != newphymode) {		/* change phy mode */
    487 		error = ieee80211_setmode(ic, newphymode);
    488 		if (error != 0)
    489 			return error;
    490 		error = ENETRESET;
    491 	}
    492 
    493 	/*
    494 	 * Committed to changes, install the rate setting.
    495 	 */
    496 	if (ic->ic_fixed_rate != i) {
    497 		ic->ic_fixed_rate = i;			/* set fixed tx rate */
    498 		error = ENETRESET;
    499 	}
    500 
    501 	/*
    502 	 * Handle operating mode change.
    503 	 */
    504 	if (ic->ic_opmode != newopmode) {
    505 		ic->ic_opmode = newopmode;
    506 		switch (newopmode) {
    507 		case IEEE80211_M_AHDEMO:
    508 		case IEEE80211_M_HOSTAP:
    509 		case IEEE80211_M_STA:
    510 		case IEEE80211_M_MONITOR:
    511 			ic->ic_flags &= ~IEEE80211_F_IBSSON;
    512 			break;
    513 		case IEEE80211_M_IBSS:
    514 			ic->ic_flags |= IEEE80211_F_IBSSON;
    515 #ifdef notdef
    516 			if (ic->ic_curmode == IEEE80211_MODE_11G)
    517 				ieee80211_set11gbasicrates(
    518 					&ic->ic_suprates[newphymode],
    519 					IEEE80211_MODE_11B);
    520 #endif
    521 			break;
    522 		}
    523 		error = ENETRESET;
    524 	}
    525 #ifdef notdef
    526 	if (error == 0)
    527 		ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
    528 #endif
    529 	return error;
    530 }
    531 
    532 void
    533 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
    534 {
    535 	struct ieee80211com *ic = (void *)ifp;
    536 	struct ieee80211_node *ni = NULL;
    537 
    538 	imr->ifm_status = IFM_AVALID;
    539 	imr->ifm_active = IFM_IEEE80211;
    540 	if (ic->ic_state == IEEE80211_S_RUN)
    541 		imr->ifm_status |= IFM_ACTIVE;
    542 	imr->ifm_active |= IFM_AUTO;
    543 	switch (ic->ic_opmode) {
    544 	case IEEE80211_M_STA:
    545 		ni = ic->ic_bss;
    546 		/* calculate rate subtype */
    547 		imr->ifm_active |= ieee80211_rate2media(ic,
    548 			ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode);
    549 		break;
    550 	case IEEE80211_M_IBSS:
    551 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
    552 		break;
    553 	case IEEE80211_M_AHDEMO:
    554 		/* should not come here */
    555 		break;
    556 	case IEEE80211_M_HOSTAP:
    557 		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
    558 		break;
    559 	case IEEE80211_M_MONITOR:
    560 		imr->ifm_active |= IFM_IEEE80211_MONITOR;
    561 		break;
    562 	}
    563 	switch (ic->ic_curmode) {
    564 	case IEEE80211_MODE_11A:
    565 		imr->ifm_active |= IFM_IEEE80211_11A;
    566 		break;
    567 	case IEEE80211_MODE_11B:
    568 		imr->ifm_active |= IFM_IEEE80211_11B;
    569 		break;
    570 	case IEEE80211_MODE_11G:
    571 		imr->ifm_active |= IFM_IEEE80211_11G;
    572 		break;
    573 	case IEEE80211_MODE_FH:
    574 		imr->ifm_active |= IFM_IEEE80211_FH;
    575 		break;
    576 	case IEEE80211_MODE_TURBO:
    577 		imr->ifm_active |= IFM_IEEE80211_11A
    578 				|  IFM_IEEE80211_TURBO;
    579 		break;
    580 	}
    581 }
    582 
    583 void
    584 ieee80211_watchdog(struct ifnet *ifp)
    585 {
    586 	struct ieee80211com *ic = (void *)ifp;
    587 
    588 	if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0)
    589 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
    590 	if (ic->ic_inact_timer && --ic->ic_inact_timer == 0)
    591 		ieee80211_timeout_nodes(ic);
    592 
    593 	if (ic->ic_mgt_timer != 0 || ic->ic_inact_timer != 0)
    594 		ifp->if_timer = 1;
    595 }
    596 
    597 /*
    598  * Mark the basic rates for the 11g rate table based on the
    599  * operating mode.  For real 11g we mark all the 11b rates
    600  * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
    601  * 11b rates.  There's also a pseudo 11a-mode used to mark only
    602  * the basic OFDM rates.
    603  */
    604 static void
    605 ieee80211_set11gbasicrates(struct ieee80211_rateset *rs, enum ieee80211_phymode mode)
    606 {
    607 	static const struct ieee80211_rateset basic[] = {
    608 	    { 3, { 12, 24, 48 } },		/* IEEE80211_MODE_11A */
    609 	    { 4, { 2, 4, 11, 22 } },		/* IEEE80211_MODE_11B */
    610 	    { 7, { 2, 4, 11, 22, 12, 24, 48 } },/* IEEE80211_MODE_11G */
    611 	    { 0 },				/* IEEE80211_MODE_FH */
    612 	    { 0 },				/* IEEE80211_MODE_TURBO	*/
    613 	};
    614 	int i, j;
    615 
    616 	for (i = 0; i < rs->rs_nrates; i++) {
    617 		rs->rs_rates[i] &= IEEE80211_RATE_VAL;
    618 		for (j = 0; j < basic[mode].rs_nrates; j++)
    619 			if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
    620 				rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
    621 				break;
    622 			}
    623 	}
    624 }
    625 
    626 /*
    627  * Set the current phy mode and recalculate the active channel
    628  * set based on the available channels for this mode.  Also
    629  * select a new default/current channel if the current one is
    630  * inappropriate for this mode.
    631  */
    632 int
    633 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
    634 {
    635 #define	N(a)	(sizeof(a) / sizeof(a[0]))
    636 	static const u_int chanflags[] = {
    637 		0,			/* IEEE80211_MODE_AUTO */
    638 		IEEE80211_CHAN_A,	/* IEEE80211_MODE_11A */
    639 		IEEE80211_CHAN_B,	/* IEEE80211_MODE_11B */
    640 		IEEE80211_CHAN_PUREG,	/* IEEE80211_MODE_11G */
    641 		IEEE80211_CHAN_FHSS,	/* IEEE80211_MODE_FH */
    642 		IEEE80211_CHAN_T,	/* IEEE80211_MODE_TURBO	*/
    643 	};
    644 	struct ieee80211_channel *c;
    645 	u_int modeflags;
    646 	int i;
    647 
    648 	/* validate new mode */
    649 	if ((ic->ic_modecaps & (1<<mode)) == 0) {
    650 		IEEE80211_DPRINTF(("%s: mode %u not supported (caps 0x%x)\n",
    651 			__func__, mode, ic->ic_modecaps));
    652 		return EINVAL;
    653 	}
    654 
    655 	/*
    656 	 * Verify at least one channel is present in the available
    657 	 * channel list before committing to the new mode.
    658 	 */
    659 	KASSERT(mode < N(chanflags), ("Unexpected mode %u", mode));
    660 	modeflags = chanflags[mode];
    661 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
    662 		c = &ic->ic_channels[i];
    663 		if (mode == IEEE80211_MODE_AUTO) {
    664 			/* ignore turbo channels for autoselect */
    665 			if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
    666 				break;
    667 		} else {
    668 			if ((c->ic_flags & modeflags) == modeflags)
    669 				break;
    670 		}
    671 	}
    672 	if (i > IEEE80211_CHAN_MAX) {
    673 		IEEE80211_DPRINTF(("%s: no channels found for mode %u\n",
    674 			__func__, mode));
    675 		return EINVAL;
    676 	}
    677 
    678 	/*
    679 	 * Calculate the active channel set.
    680 	 */
    681 	memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
    682 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
    683 		c = &ic->ic_channels[i];
    684 		if (mode == IEEE80211_MODE_AUTO) {
    685 			/* take anything but pure turbo channels */
    686 			if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
    687 				setbit(ic->ic_chan_active, i);
    688 		} else {
    689 			if ((c->ic_flags & modeflags) == modeflags)
    690 				setbit(ic->ic_chan_active, i);
    691 		}
    692 	}
    693 	/*
    694 	 * If no current/default channel is setup or the current
    695 	 * channel is wrong for the mode then pick the first
    696 	 * available channel from the active list.  This is likely
    697 	 * not the right one.
    698 	 */
    699 	if (ic->ic_ibss_chan == NULL ||
    700 	    isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
    701 		for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
    702 			if (isset(ic->ic_chan_active, i)) {
    703 				ic->ic_ibss_chan = &ic->ic_channels[i];
    704 				break;
    705 			}
    706 	}
    707 
    708 	/*
    709 	 * Set/reset state flags that influence beacon contents, etc.
    710 	 *
    711 	 * XXX what if we have stations already associated???
    712 	 * XXX probably not right for autoselect?
    713 	 */
    714 	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE)
    715 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
    716 	if (mode == IEEE80211_MODE_11G) {
    717 		if (ic->ic_caps & IEEE80211_C_SHSLOT)
    718 			ic->ic_flags |= IEEE80211_F_SHSLOT;
    719 		ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode],
    720 			IEEE80211_MODE_11G);
    721 	} else {
    722 		ic->ic_flags &= ~IEEE80211_F_SHSLOT;
    723 	}
    724 
    725 	ic->ic_curmode = mode;
    726 	return 0;
    727 #undef N
    728 }
    729 
    730 /*
    731  * Return the phy mode for with the specified channel so the
    732  * caller can select a rate set.  This is problematic and the
    733  * work here assumes how things work elsewhere in this code.
    734  */
    735 enum ieee80211_phymode
    736 ieee80211_chan2mode(struct ieee80211com *ic, struct ieee80211_channel *chan)
    737 {
    738 	/*
    739 	 * NB: this assumes the channel would not be supplied to us
    740 	 *     unless it was already compatible with the current mode.
    741 	 */
    742 	if (ic->ic_curmode != IEEE80211_MODE_AUTO)
    743 		return ic->ic_curmode;
    744 	/*
    745 	 * In autoselect mode; deduce a mode based on the channel
    746 	 * characteristics.  We assume that turbo-only channels
    747 	 * are not considered when the channel set is constructed.
    748 	 */
    749 	if (IEEE80211_IS_CHAN_5GHZ(chan))
    750 		return IEEE80211_MODE_11A;
    751 	else if (IEEE80211_IS_CHAN_FHSS(chan))
    752 		return IEEE80211_MODE_FH;
    753 	else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN))
    754 		return IEEE80211_MODE_11G;
    755 	else
    756 		return IEEE80211_MODE_11B;
    757 }
    758 
    759 /*
    760  * convert IEEE80211 rate value to ifmedia subtype.
    761  * ieee80211 rate is in unit of 0.5Mbps.
    762  */
    763 int
    764 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
    765 {
    766 #define	N(a)	(sizeof(a) / sizeof(a[0]))
    767 	static const struct {
    768 		u_int	m;	/* rate + mode */
    769 		u_int	r;	/* if_media rate */
    770 	} rates[] = {
    771 		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
    772 		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
    773 		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
    774 		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
    775 		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
    776 		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
    777 		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
    778 		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
    779 		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
    780 		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
    781 		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
    782 		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
    783 		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
    784 		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
    785 		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
    786 		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
    787 		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
    788 		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
    789 		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
    790 		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
    791 		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
    792 		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
    793 		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
    794 		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
    795 		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
    796 		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
    797 		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
    798 		/* NB: OFDM72 doesn't realy exist so we don't handle it */
    799 	};
    800 	u_int mask, i;
    801 
    802 	mask = rate & IEEE80211_RATE_VAL;
    803 	switch (mode) {
    804 	case IEEE80211_MODE_11A:
    805 	case IEEE80211_MODE_TURBO:
    806 		mask |= IFM_IEEE80211_11A;
    807 		break;
    808 	case IEEE80211_MODE_11B:
    809 		mask |= IFM_IEEE80211_11B;
    810 		break;
    811 	case IEEE80211_MODE_FH:
    812 		mask |= IFM_IEEE80211_FH;
    813 		break;
    814 	case IEEE80211_MODE_AUTO:
    815 		/* NB: ic may be NULL for some drivers */
    816 		if (ic && ic->ic_phytype == IEEE80211_T_FH) {
    817 			mask |= IFM_IEEE80211_FH;
    818 			break;
    819 		}
    820 		/* NB: hack, 11g matches both 11b+11a rates */
    821 		/* fall thru... */
    822 	case IEEE80211_MODE_11G:
    823 		mask |= IFM_IEEE80211_11G;
    824 		break;
    825 	}
    826 	for (i = 0; i < N(rates); i++)
    827 		if (rates[i].m == mask)
    828 			return rates[i].r;
    829 	return IFM_AUTO;
    830 #undef N
    831 }
    832 
    833 int
    834 ieee80211_media2rate(int mword)
    835 {
    836 #define	N(a)	(sizeof(a) / sizeof(a[0]))
    837 	static const int ieeerates[] = {
    838 		-1,		/* IFM_AUTO */
    839 		0,		/* IFM_MANUAL */
    840 		0,		/* IFM_NONE */
    841 		2,		/* IFM_IEEE80211_FH1 */
    842 		4,		/* IFM_IEEE80211_FH2 */
    843 		2,		/* IFM_IEEE80211_DS1 */
    844 		4,		/* IFM_IEEE80211_DS2 */
    845 		11,		/* IFM_IEEE80211_DS5 */
    846 		22,		/* IFM_IEEE80211_DS11 */
    847 		44,		/* IFM_IEEE80211_DS22 */
    848 		12,		/* IFM_IEEE80211_OFDM6 */
    849 		18,		/* IFM_IEEE80211_OFDM9 */
    850 		24,		/* IFM_IEEE80211_OFDM12 */
    851 		36,		/* IFM_IEEE80211_OFDM18 */
    852 		48,		/* IFM_IEEE80211_OFDM24 */
    853 		72,		/* IFM_IEEE80211_OFDM36 */
    854 		96,		/* IFM_IEEE80211_OFDM48 */
    855 		108,		/* IFM_IEEE80211_OFDM54 */
    856 		144,		/* IFM_IEEE80211_OFDM72 */
    857 	};
    858 	return IFM_SUBTYPE(mword) < N(ieeerates) ?
    859 		ieeerates[IFM_SUBTYPE(mword)] : 0;
    860 #undef N
    861 }
    862 
    863 /*
    864  * Module glue.
    865  *
    866  * NB: the module name is "wlan" for compatibility with NetBSD.
    867  */
    868 
    869 static int
    870 ieee80211_modevent(module_t mod, int type, void *unused)
    871 {
    872 	switch (type) {
    873 	case MOD_LOAD:
    874 		if (bootverbose)
    875 			printf("wlan: <802.11 Link Layer>\n");
    876 		return 0;
    877 	case MOD_UNLOAD:
    878 		return 0;
    879 	}
    880 	return EINVAL;
    881 }
    882 
    883 static moduledata_t ieee80211_mod = {
    884 	"wlan",
    885 	ieee80211_modevent,
    886 	0
    887 };
    888 DECLARE_MODULE(wlan, ieee80211_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
    889 MODULE_VERSION(wlan, 1);
    890 MODULE_DEPEND(wlan, rc4, 1, 1, 1);
    891 MODULE_DEPEND(wlan, ether, 1, 1, 1);
    892