ieee80211.c revision 158044
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 158044 2006-04-26 16:00:37Z 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	if (ic->ic_caps & IEEE80211_C_BURST)
188		ic->ic_flags |= IEEE80211_F_BURST;
189	(void) ieee80211_setmode(ic, ic->ic_curmode);
190
191	ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
192	ic->ic_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
193	ic->ic_dtim_period = IEEE80211_DTIM_DEFAULT;
194	IEEE80211_BEACON_LOCK_INIT(ic, "beacon");
195
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	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
240		if (freq == 2484)
241			return 14;
242		if (freq < 2484)
243			return ((int) freq - 2407) / 5;
244		else
245			return 15 + ((freq - 2512) / 20);
246	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
247		if (freq <= 5000)
248			return (freq - 4000) / 5;
249		else
250			return (freq - 5000) / 5;
251	} else {				/* either, guess */
252		if (freq == 2484)
253			return 14;
254		if (freq < 2484)
255			return ((int) freq - 2407) / 5;
256		if (freq < 5000) {
257			if (freq > 4900)
258				return (freq - 4000) / 5;
259			else
260				return 15 + ((freq - 2512) / 20);
261		}
262		return (freq - 5000) / 5;
263	}
264}
265
266/*
267 * Convert channel to IEEE channel number.
268 */
269int
270ieee80211_chan2ieee(struct ieee80211com *ic, struct ieee80211_channel *c)
271{
272	if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX])
273		return c - ic->ic_channels;
274	else if (c == IEEE80211_CHAN_ANYC)
275		return IEEE80211_CHAN_ANY;
276	else if (c != NULL) {
277		if_printf(ic->ic_ifp, "invalid channel freq %u flags %x\n",
278			c->ic_freq, c->ic_flags);
279		return 0;		/* XXX */
280	} else {
281		if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
282		return 0;		/* XXX */
283	}
284}
285
286/*
287 * Convert IEEE channel number to MHz frequency.
288 */
289u_int
290ieee80211_ieee2mhz(u_int chan, u_int flags)
291{
292	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
293		if (chan == 14)
294			return 2484;
295		if (chan < 14)
296			return 2407 + chan*5;
297		else
298			return 2512 + ((chan-15)*20);
299	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
300		return 5000 + (chan*5);
301	} else {				/* either, guess */
302		if (chan == 14)
303			return 2484;
304		if (chan < 14)			/* 0-13 */
305			return 2407 + chan*5;
306		if (chan < 27)			/* 15-26 */
307			return 2512 + ((chan-15)*20);
308		return 5000 + (chan*5);
309	}
310}
311
312/*
313 * Setup the media data structures according to the channel and
314 * rate tables.  This must be called by the driver after
315 * ieee80211_attach and before most anything else.
316 */
317void
318ieee80211_media_init(struct ieee80211com *ic,
319	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
320{
321#define	ADD(_ic, _s, _o) \
322	ifmedia_add(&(_ic)->ic_media, \
323		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
324	struct ifnet *ifp = ic->ic_ifp;
325	struct ifmediareq imr;
326	int i, j, mode, rate, maxrate, mword, mopt, r;
327	struct ieee80211_rateset *rs;
328	struct ieee80211_rateset allrates;
329
330	/*
331	 * Do late attach work that must wait for any subclass
332	 * (i.e. driver) work such as overriding methods.
333	 */
334	ieee80211_node_lateattach(ic);
335
336	/*
337	 * Fill in media characteristics.
338	 */
339	ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
340	maxrate = 0;
341	memset(&allrates, 0, sizeof(allrates));
342	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) {
343		static const u_int mopts[] = {
344			IFM_AUTO,
345			IFM_IEEE80211_11A,
346			IFM_IEEE80211_11B,
347			IFM_IEEE80211_11G,
348			IFM_IEEE80211_FH,
349			IFM_IEEE80211_11A | IFM_IEEE80211_TURBO,
350			IFM_IEEE80211_11G | IFM_IEEE80211_TURBO,
351		};
352		if ((ic->ic_modecaps & (1<<mode)) == 0)
353			continue;
354		mopt = mopts[mode];
355		ADD(ic, IFM_AUTO, mopt);	/* e.g. 11a auto */
356		if (ic->ic_caps & IEEE80211_C_IBSS)
357			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC);
358		if (ic->ic_caps & IEEE80211_C_HOSTAP)
359			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP);
360		if (ic->ic_caps & IEEE80211_C_AHDEMO)
361			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
362		if (ic->ic_caps & IEEE80211_C_MONITOR)
363			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR);
364		if (mode == IEEE80211_MODE_AUTO)
365			continue;
366		rs = &ic->ic_sup_rates[mode];
367		for (i = 0; i < rs->rs_nrates; i++) {
368			rate = rs->rs_rates[i];
369			mword = ieee80211_rate2media(ic, rate, mode);
370			if (mword == 0)
371				continue;
372			ADD(ic, mword, mopt);
373			if (ic->ic_caps & IEEE80211_C_IBSS)
374				ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
375			if (ic->ic_caps & IEEE80211_C_HOSTAP)
376				ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
377			if (ic->ic_caps & IEEE80211_C_AHDEMO)
378				ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
379			if (ic->ic_caps & IEEE80211_C_MONITOR)
380				ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
381			/*
382			 * Add rate to the collection of all rates.
383			 */
384			r = rate & IEEE80211_RATE_VAL;
385			for (j = 0; j < allrates.rs_nrates; j++)
386				if (allrates.rs_rates[j] == r)
387					break;
388			if (j == allrates.rs_nrates) {
389				/* unique, add to the set */
390				allrates.rs_rates[j] = r;
391				allrates.rs_nrates++;
392			}
393			rate = (rate & IEEE80211_RATE_VAL) / 2;
394			if (rate > maxrate)
395				maxrate = rate;
396		}
397	}
398	for (i = 0; i < allrates.rs_nrates; i++) {
399		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
400				IEEE80211_MODE_AUTO);
401		if (mword == 0)
402			continue;
403		mword = IFM_SUBTYPE(mword);	/* remove media options */
404		ADD(ic, mword, 0);
405		if (ic->ic_caps & IEEE80211_C_IBSS)
406			ADD(ic, mword, IFM_IEEE80211_ADHOC);
407		if (ic->ic_caps & IEEE80211_C_HOSTAP)
408			ADD(ic, mword, IFM_IEEE80211_HOSTAP);
409		if (ic->ic_caps & IEEE80211_C_AHDEMO)
410			ADD(ic, mword, IFM_IEEE80211_ADHOC | IFM_FLAG0);
411		if (ic->ic_caps & IEEE80211_C_MONITOR)
412			ADD(ic, mword, IFM_IEEE80211_MONITOR);
413	}
414	ieee80211_media_status(ifp, &imr);
415	ifmedia_set(&ic->ic_media, imr.ifm_active);
416
417	if (maxrate)
418		ifp->if_baudrate = IF_Mbps(maxrate);
419#undef ADD
420}
421
422void
423ieee80211_announce(struct ieee80211com *ic)
424{
425	struct ifnet *ifp = ic->ic_ifp;
426	int i, mode, rate, mword;
427	struct ieee80211_rateset *rs;
428
429	for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
430		if ((ic->ic_modecaps & (1<<mode)) == 0)
431			continue;
432		if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
433		rs = &ic->ic_sup_rates[mode];
434		for (i = 0; i < rs->rs_nrates; i++) {
435			rate = rs->rs_rates[i];
436			mword = ieee80211_rate2media(ic, rate, mode);
437			if (mword == 0)
438				continue;
439			printf("%s%d%sMbps", (i != 0 ? " " : ""),
440			    (rate & IEEE80211_RATE_VAL) / 2,
441			    ((rate & 0x1) != 0 ? ".5" : ""));
442		}
443		printf("\n");
444	}
445}
446
447static int
448findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
449{
450#define	IEEERATE(_ic,_m,_i) \
451	((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
452	int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
453	for (i = 0; i < nrates; i++)
454		if (IEEERATE(ic, mode, i) == rate)
455			return i;
456	return -1;
457#undef IEEERATE
458}
459
460/*
461 * Find an instance by it's mac address.
462 */
463struct ieee80211com *
464ieee80211_find_vap(const u_int8_t mac[IEEE80211_ADDR_LEN])
465{
466	struct ieee80211com *ic;
467
468	/* XXX lock */
469	SLIST_FOREACH(ic, &ieee80211_list, ic_next)
470		if (IEEE80211_ADDR_EQ(mac, ic->ic_myaddr))
471			return ic;
472	return NULL;
473}
474
475static struct ieee80211com *
476ieee80211_find_instance(struct ifnet *ifp)
477{
478	struct ieee80211com *ic;
479
480	/* XXX lock */
481	/* XXX not right for multiple instances but works for now */
482	SLIST_FOREACH(ic, &ieee80211_list, ic_next)
483		if (ic->ic_ifp == ifp)
484			return ic;
485	return NULL;
486}
487
488/*
489 * Handle a media change request.
490 */
491int
492ieee80211_media_change(struct ifnet *ifp)
493{
494	struct ieee80211com *ic;
495	struct ifmedia_entry *ime;
496	enum ieee80211_opmode newopmode;
497	enum ieee80211_phymode newphymode;
498	int i, j, newrate, error = 0;
499
500	ic = ieee80211_find_instance(ifp);
501	if (!ic) {
502		if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
503		return EINVAL;
504	}
505	ime = ic->ic_media.ifm_cur;
506	/*
507	 * First, identify the phy mode.
508	 */
509	switch (IFM_MODE(ime->ifm_media)) {
510	case IFM_IEEE80211_11A:
511		newphymode = IEEE80211_MODE_11A;
512		break;
513	case IFM_IEEE80211_11B:
514		newphymode = IEEE80211_MODE_11B;
515		break;
516	case IFM_IEEE80211_11G:
517		newphymode = IEEE80211_MODE_11G;
518		break;
519	case IFM_IEEE80211_FH:
520		newphymode = IEEE80211_MODE_FH;
521		break;
522	case IFM_AUTO:
523		newphymode = IEEE80211_MODE_AUTO;
524		break;
525	default:
526		return EINVAL;
527	}
528	/*
529	 * Turbo mode is an ``option''.
530	 * XXX does not apply to AUTO
531	 */
532	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
533		if (newphymode == IEEE80211_MODE_11A)
534			newphymode = IEEE80211_MODE_TURBO_A;
535		else if (newphymode == IEEE80211_MODE_11G)
536			newphymode = IEEE80211_MODE_TURBO_G;
537		else
538			return EINVAL;
539	}
540	/*
541	 * Validate requested mode is available.
542	 */
543	if ((ic->ic_modecaps & (1<<newphymode)) == 0)
544		return EINVAL;
545
546	/*
547	 * Next, the fixed/variable rate.
548	 */
549	i = -1;
550	if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
551		/*
552		 * Convert media subtype to rate.
553		 */
554		newrate = ieee80211_media2rate(ime->ifm_media);
555		if (newrate == 0)
556			return EINVAL;
557		/*
558		 * Check the rate table for the specified/current phy.
559		 */
560		if (newphymode == IEEE80211_MODE_AUTO) {
561			/*
562			 * In autoselect mode search for the rate.
563			 */
564			for (j = IEEE80211_MODE_11A;
565			     j < IEEE80211_MODE_MAX; j++) {
566				if ((ic->ic_modecaps & (1<<j)) == 0)
567					continue;
568				i = findrate(ic, j, newrate);
569				if (i != -1) {
570					/* lock mode too */
571					newphymode = j;
572					break;
573				}
574			}
575		} else {
576			i = findrate(ic, newphymode, newrate);
577		}
578		if (i == -1)			/* mode/rate mismatch */
579			return EINVAL;
580	}
581	/* NB: defer rate setting to later */
582
583	/*
584	 * Deduce new operating mode but don't install it just yet.
585	 */
586	if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) ==
587	    (IFM_IEEE80211_ADHOC|IFM_FLAG0))
588		newopmode = IEEE80211_M_AHDEMO;
589	else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
590		newopmode = IEEE80211_M_HOSTAP;
591	else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
592		newopmode = IEEE80211_M_IBSS;
593	else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
594		newopmode = IEEE80211_M_MONITOR;
595	else
596		newopmode = IEEE80211_M_STA;
597
598	/*
599	 * Autoselect doesn't make sense when operating as an AP.
600	 * If no phy mode has been selected, pick one and lock it
601	 * down so rate tables can be used in forming beacon frames
602	 * and the like.
603	 */
604	if (newopmode == IEEE80211_M_HOSTAP &&
605	    newphymode == IEEE80211_MODE_AUTO) {
606		for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++)
607			if (ic->ic_modecaps & (1<<j)) {
608				newphymode = j;
609				break;
610			}
611	}
612
613	/*
614	 * Handle phy mode change.
615	 */
616	if (ic->ic_curmode != newphymode) {		/* change phy mode */
617		error = ieee80211_setmode(ic, newphymode);
618		if (error != 0)
619			return error;
620		error = ENETRESET;
621	}
622
623	/*
624	 * Committed to changes, install the rate setting.
625	 */
626	if (ic->ic_fixed_rate != i) {
627		ic->ic_fixed_rate = i;			/* set fixed tx rate */
628		error = ENETRESET;
629	}
630
631	/*
632	 * Handle operating mode change.
633	 */
634	if (ic->ic_opmode != newopmode) {
635		ic->ic_opmode = newopmode;
636		switch (newopmode) {
637		case IEEE80211_M_AHDEMO:
638		case IEEE80211_M_HOSTAP:
639		case IEEE80211_M_STA:
640		case IEEE80211_M_MONITOR:
641			ic->ic_flags &= ~IEEE80211_F_IBSSON;
642			break;
643		case IEEE80211_M_IBSS:
644			ic->ic_flags |= IEEE80211_F_IBSSON;
645			break;
646		}
647		/*
648		 * Yech, slot time may change depending on the
649		 * operating mode so reset it to be sure everything
650		 * is setup appropriately.
651		 */
652		ieee80211_reset_erp(ic);
653		ieee80211_wme_initparams(ic);	/* after opmode change */
654		error = ENETRESET;
655	}
656#ifdef notdef
657	if (error == 0)
658		ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
659#endif
660	return error;
661}
662
663void
664ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
665{
666	struct ieee80211com *ic;
667	struct ieee80211_rateset *rs;
668
669	ic = ieee80211_find_instance(ifp);
670	if (!ic) {
671		if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
672		return;
673	}
674	imr->ifm_status = IFM_AVALID;
675	imr->ifm_active = IFM_IEEE80211;
676	if (ic->ic_state == IEEE80211_S_RUN)
677		imr->ifm_status |= IFM_ACTIVE;
678	/*
679	 * Calculate a current rate if possible.
680	 */
681	if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
682		/*
683		 * A fixed rate is set, report that.
684		 */
685		rs = &ic->ic_sup_rates[ic->ic_curmode];
686		imr->ifm_active |= ieee80211_rate2media(ic,
687			rs->rs_rates[ic->ic_fixed_rate], ic->ic_curmode);
688	} else if (ic->ic_opmode == IEEE80211_M_STA) {
689		/*
690		 * In station mode report the current transmit rate.
691		 */
692		rs = &ic->ic_bss->ni_rates;
693		imr->ifm_active |= ieee80211_rate2media(ic,
694			rs->rs_rates[ic->ic_bss->ni_txrate], ic->ic_curmode);
695	} else
696		imr->ifm_active |= IFM_AUTO;
697	switch (ic->ic_opmode) {
698	case IEEE80211_M_STA:
699		break;
700	case IEEE80211_M_IBSS:
701		imr->ifm_active |= IFM_IEEE80211_ADHOC;
702		break;
703	case IEEE80211_M_AHDEMO:
704		/* should not come here */
705		break;
706	case IEEE80211_M_HOSTAP:
707		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
708		break;
709	case IEEE80211_M_MONITOR:
710		imr->ifm_active |= IFM_IEEE80211_MONITOR;
711		break;
712	}
713	switch (ic->ic_curmode) {
714	case IEEE80211_MODE_11A:
715		imr->ifm_active |= IFM_IEEE80211_11A;
716		break;
717	case IEEE80211_MODE_11B:
718		imr->ifm_active |= IFM_IEEE80211_11B;
719		break;
720	case IEEE80211_MODE_11G:
721		imr->ifm_active |= IFM_IEEE80211_11G;
722		break;
723	case IEEE80211_MODE_FH:
724		imr->ifm_active |= IFM_IEEE80211_FH;
725		break;
726	case IEEE80211_MODE_TURBO_A:
727		imr->ifm_active |= IFM_IEEE80211_11A
728				|  IFM_IEEE80211_TURBO;
729		break;
730	case IEEE80211_MODE_TURBO_G:
731		imr->ifm_active |= IFM_IEEE80211_11G
732				|  IFM_IEEE80211_TURBO;
733		break;
734	}
735}
736
737void
738ieee80211_watchdog(struct ieee80211com *ic)
739{
740	struct ieee80211_node_table *nt;
741	int need_inact_timer = 0;
742
743	if (ic->ic_state != IEEE80211_S_INIT) {
744		if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0)
745			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
746		nt = &ic->ic_scan;
747		if (nt->nt_inact_timer) {
748			if (--nt->nt_inact_timer == 0)
749				nt->nt_timeout(nt);
750			need_inact_timer += nt->nt_inact_timer;
751		}
752		nt = &ic->ic_sta;
753		if (nt->nt_inact_timer) {
754			if (--nt->nt_inact_timer == 0)
755				nt->nt_timeout(nt);
756			need_inact_timer += nt->nt_inact_timer;
757		}
758	}
759	if (ic->ic_mgt_timer != 0 || need_inact_timer)
760		ic->ic_ifp->if_timer = 1;
761}
762
763/*
764 * Set the current phy mode and recalculate the active channel
765 * set based on the available channels for this mode.  Also
766 * select a new default/current channel if the current one is
767 * inappropriate for this mode.
768 */
769int
770ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
771{
772#define	N(a)	(sizeof(a) / sizeof(a[0]))
773	static const u_int chanflags[] = {
774		0,			/* IEEE80211_MODE_AUTO */
775		IEEE80211_CHAN_A,	/* IEEE80211_MODE_11A */
776		IEEE80211_CHAN_B,	/* IEEE80211_MODE_11B */
777		IEEE80211_CHAN_PUREG,	/* IEEE80211_MODE_11G */
778		IEEE80211_CHAN_FHSS,	/* IEEE80211_MODE_FH */
779		IEEE80211_CHAN_T,	/* IEEE80211_MODE_TURBO_A */
780		IEEE80211_CHAN_108G,	/* IEEE80211_MODE_TURBO_G */
781	};
782	struct ieee80211_channel *c;
783	u_int modeflags;
784	int i;
785
786	/* validate new mode */
787	if ((ic->ic_modecaps & (1<<mode)) == 0) {
788		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
789			"%s: mode %u not supported (caps 0x%x)\n",
790			__func__, mode, ic->ic_modecaps);
791		return EINVAL;
792	}
793
794	/*
795	 * Verify at least one channel is present in the available
796	 * channel list before committing to the new mode.
797	 */
798	KASSERT(mode < N(chanflags), ("Unexpected mode %u", mode));
799	modeflags = chanflags[mode];
800	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
801		c = &ic->ic_channels[i];
802		if (c->ic_flags == 0)
803			continue;
804		if (mode == IEEE80211_MODE_AUTO) {
805			/* ignore static turbo channels for autoselect */
806			if (!IEEE80211_IS_CHAN_T(c))
807				break;
808		} else {
809			if ((c->ic_flags & modeflags) == modeflags)
810				break;
811		}
812	}
813	if (i > IEEE80211_CHAN_MAX) {
814		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
815			"%s: no channels found for mode %u\n", __func__, mode);
816		return EINVAL;
817	}
818
819	/*
820	 * Calculate the active channel set.
821	 */
822	memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
823	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
824		c = &ic->ic_channels[i];
825		if (c->ic_flags == 0)
826			continue;
827		if (mode == IEEE80211_MODE_AUTO) {
828			/* take anything but static turbo channels */
829			if (!IEEE80211_IS_CHAN_T(c))
830				setbit(ic->ic_chan_active, i);
831		} else {
832			if ((c->ic_flags & modeflags) == modeflags)
833				setbit(ic->ic_chan_active, i);
834		}
835	}
836	/*
837	 * If no current/default channel is setup or the current
838	 * channel is wrong for the mode then pick the first
839	 * available channel from the active list.  This is likely
840	 * not the right one.
841	 */
842	if (ic->ic_ibss_chan == NULL ||
843	    isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
844		for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
845			if (isset(ic->ic_chan_active, i)) {
846				ic->ic_ibss_chan = &ic->ic_channels[i];
847				break;
848			}
849		KASSERT(ic->ic_ibss_chan != NULL &&
850		    isset(ic->ic_chan_active,
851			ieee80211_chan2ieee(ic, ic->ic_ibss_chan)),
852		    ("Bad IBSS channel %u",
853		     ieee80211_chan2ieee(ic, ic->ic_ibss_chan)));
854	}
855	/*
856	 * If the desired channel is set but no longer valid then reset it.
857	 */
858	if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
859	    isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_des_chan)))
860		ic->ic_des_chan = IEEE80211_CHAN_ANYC;
861
862	/*
863	 * Do mode-specific rate setup.
864	 */
865	if (mode == IEEE80211_MODE_11G) {
866		/*
867		 * Use a mixed 11b/11g rate set.
868		 */
869		ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode],
870			IEEE80211_MODE_11G);
871	} else if (mode == IEEE80211_MODE_11B) {
872		/*
873		 * Force pure 11b rate set.
874		 */
875		ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode],
876			IEEE80211_MODE_11B);
877	}
878	/*
879	 * Setup an initial rate set according to the
880	 * current/default channel selected above.  This
881	 * will be changed when scanning but must exist
882	 * now so driver have a consistent state of ic_ibss_chan.
883	 */
884	if (ic->ic_bss)		/* NB: can be called before lateattach */
885		ic->ic_bss->ni_rates = ic->ic_sup_rates[mode];
886
887	ic->ic_curmode = mode;
888	ieee80211_reset_erp(ic);	/* reset ERP state */
889	ieee80211_wme_initparams(ic);	/* reset WME stat */
890
891	return 0;
892#undef N
893}
894
895/*
896 * Return the phy mode for with the specified channel so the
897 * caller can select a rate set.  This is problematic for channels
898 * where multiple operating modes are possible (e.g. 11g+11b).
899 * In those cases we defer to the current operating mode when set.
900 */
901enum ieee80211_phymode
902ieee80211_chan2mode(struct ieee80211com *ic, struct ieee80211_channel *chan)
903{
904	if (IEEE80211_IS_CHAN_T(chan)) {
905		return IEEE80211_MODE_TURBO_A;
906	} else if (IEEE80211_IS_CHAN_5GHZ(chan)) {
907		return IEEE80211_MODE_11A;
908	} else if (IEEE80211_IS_CHAN_FHSS(chan))
909		return IEEE80211_MODE_FH;
910	else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN)) {
911		/*
912		 * This assumes all 11g channels are also usable
913		 * for 11b, which is currently true.
914		 */
915		if (ic->ic_curmode == IEEE80211_MODE_TURBO_G)
916			return IEEE80211_MODE_TURBO_G;
917		if (ic->ic_curmode == IEEE80211_MODE_11B)
918			return IEEE80211_MODE_11B;
919		return IEEE80211_MODE_11G;
920	} else
921		return IEEE80211_MODE_11B;
922}
923
924/*
925 * convert IEEE80211 rate value to ifmedia subtype.
926 * ieee80211 rate is in unit of 0.5Mbps.
927 */
928int
929ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
930{
931#define	N(a)	(sizeof(a) / sizeof(a[0]))
932	static const struct {
933		u_int	m;	/* rate + mode */
934		u_int	r;	/* if_media rate */
935	} rates[] = {
936		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
937		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
938		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
939		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
940		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
941		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
942		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
943		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
944		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
945		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
946		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
947		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
948		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
949		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
950		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
951		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
952		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
953		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
954		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
955		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
956		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
957		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
958		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
959		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
960		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
961		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
962		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
963		/* NB: OFDM72 doesn't realy exist so we don't handle it */
964	};
965	u_int mask, i;
966
967	mask = rate & IEEE80211_RATE_VAL;
968	switch (mode) {
969	case IEEE80211_MODE_11A:
970	case IEEE80211_MODE_TURBO_A:
971		mask |= IFM_IEEE80211_11A;
972		break;
973	case IEEE80211_MODE_11B:
974		mask |= IFM_IEEE80211_11B;
975		break;
976	case IEEE80211_MODE_FH:
977		mask |= IFM_IEEE80211_FH;
978		break;
979	case IEEE80211_MODE_AUTO:
980		/* NB: ic may be NULL for some drivers */
981		if (ic && ic->ic_phytype == IEEE80211_T_FH) {
982			mask |= IFM_IEEE80211_FH;
983			break;
984		}
985		/* NB: hack, 11g matches both 11b+11a rates */
986		/* fall thru... */
987	case IEEE80211_MODE_11G:
988	case IEEE80211_MODE_TURBO_G:
989		mask |= IFM_IEEE80211_11G;
990		break;
991	}
992	for (i = 0; i < N(rates); i++)
993		if (rates[i].m == mask)
994			return rates[i].r;
995	return IFM_AUTO;
996#undef N
997}
998
999int
1000ieee80211_media2rate(int mword)
1001{
1002#define	N(a)	(sizeof(a) / sizeof(a[0]))
1003	static const int ieeerates[] = {
1004		-1,		/* IFM_AUTO */
1005		0,		/* IFM_MANUAL */
1006		0,		/* IFM_NONE */
1007		2,		/* IFM_IEEE80211_FH1 */
1008		4,		/* IFM_IEEE80211_FH2 */
1009		2,		/* IFM_IEEE80211_DS1 */
1010		4,		/* IFM_IEEE80211_DS2 */
1011		11,		/* IFM_IEEE80211_DS5 */
1012		22,		/* IFM_IEEE80211_DS11 */
1013		44,		/* IFM_IEEE80211_DS22 */
1014		12,		/* IFM_IEEE80211_OFDM6 */
1015		18,		/* IFM_IEEE80211_OFDM9 */
1016		24,		/* IFM_IEEE80211_OFDM12 */
1017		36,		/* IFM_IEEE80211_OFDM18 */
1018		48,		/* IFM_IEEE80211_OFDM24 */
1019		72,		/* IFM_IEEE80211_OFDM36 */
1020		96,		/* IFM_IEEE80211_OFDM48 */
1021		108,		/* IFM_IEEE80211_OFDM54 */
1022		144,		/* IFM_IEEE80211_OFDM72 */
1023	};
1024	return IFM_SUBTYPE(mword) < N(ieeerates) ?
1025		ieeerates[IFM_SUBTYPE(mword)] : 0;
1026#undef N
1027}
1028