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