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