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