xref: /freebsd/sys/net80211/ieee80211.c (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
1 /*-
2  * Copyright (c) 2001 Atsushi Onoe
3  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * Alternatively, this software may be distributed under the terms of the
18  * GNU General Public License ("GPL") version 2 as published by the Free
19  * Software Foundation.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 /*
37  * IEEE 802.11 generic handler
38  */
39 
40 #include "opt_inet.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/mbuf.h>
45 #include <sys/malloc.h>
46 #include <sys/kernel.h>
47 #include <sys/module.h>
48 #include <sys/socket.h>
49 #include <sys/sockio.h>
50 #include <sys/endian.h>
51 #include <sys/errno.h>
52 #include <sys/bus.h>
53 #include <sys/proc.h>
54 #include <sys/sysctl.h>
55 
56 #include <machine/atomic.h>
57 
58 #include <net/if.h>
59 #include <net/if_dl.h>
60 #include <net/if_media.h>
61 #include <net/if_arp.h>
62 #include <net/ethernet.h>
63 #include <net/if_llc.h>
64 #include <net/route.h>
65 
66 #include <net80211/ieee80211_var.h>
67 
68 #include <net/bpf.h>
69 
70 #ifdef INET
71 #include <netinet/in.h>
72 #include <netinet/if_ether.h>
73 #endif
74 
75 #ifdef IEEE80211_DEBUG
76 int	ieee80211_debug = 0;
77 SYSCTL_INT(_debug, OID_AUTO, ieee80211, CTLFLAG_RW, &ieee80211_debug,
78 	    0, "IEEE 802.11 media debugging printfs");
79 #endif
80 
81 static void ieee80211_set11gbasicrates(struct ieee80211_rateset *,
82 		enum ieee80211_phymode);
83 
84 static const char *ieee80211_phymode_name[] = {
85 	"auto",		/* IEEE80211_MODE_AUTO */
86 	"11a",		/* IEEE80211_MODE_11A */
87 	"11b",		/* IEEE80211_MODE_11B */
88 	"11g",		/* IEEE80211_MODE_11G */
89 	"FH",		/* IEEE80211_MODE_FH */
90 	"turbo",	/* IEEE80211_MODE_TURBO */
91 };
92 
93 void
94 ieee80211_ifattach(struct ifnet *ifp)
95 {
96 	struct ieee80211com *ic = (void *)ifp;
97 	struct ieee80211_channel *c;
98 	int i;
99 
100 	ether_ifattach(ifp, ic->ic_myaddr);
101 	bpfattach2(ifp, DLT_IEEE802_11,
102 	    sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf);
103 	ieee80211_crypto_attach(ifp);
104 
105 	/*
106 	 * Fill in 802.11 available channel set, mark
107 	 * all available channels as active, and pick
108 	 * a default channel if not already specified.
109 	 */
110 	memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
111 	ic->ic_modecaps |= 1<<IEEE80211_MODE_AUTO;
112 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
113 		c = &ic->ic_channels[i];
114 		if (c->ic_flags) {
115 			/*
116 			 * Verify driver passed us valid data.
117 			 */
118 			if (i != ieee80211_chan2ieee(ic, c)) {
119 				if_printf(ifp, "bad channel ignored; "
120 					"freq %u flags %x number %u\n",
121 					c->ic_freq, c->ic_flags, i);
122 				c->ic_flags = 0;	/* NB: remove */
123 				continue;
124 			}
125 			setbit(ic->ic_chan_avail, i);
126 			/*
127 			 * Identify mode capabilities.
128 			 */
129 			if (IEEE80211_IS_CHAN_A(c))
130 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11A;
131 			if (IEEE80211_IS_CHAN_B(c))
132 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11B;
133 			if (IEEE80211_IS_CHAN_PUREG(c))
134 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11G;
135 			if (IEEE80211_IS_CHAN_FHSS(c))
136 				ic->ic_modecaps |= 1<<IEEE80211_MODE_FH;
137 			if (IEEE80211_IS_CHAN_T(c))
138 				ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO;
139 		}
140 	}
141 	/* validate ic->ic_curmode */
142 	if ((ic->ic_modecaps & (1<<ic->ic_curmode)) == 0)
143 		ic->ic_curmode = IEEE80211_MODE_AUTO;
144 	ic->ic_des_chan = IEEE80211_CHAN_ANYC;	/* any channel is ok */
145 
146 	(void) ieee80211_setmode(ic, ic->ic_curmode);
147 
148 	if (ic->ic_lintval == 0)
149 		ic->ic_lintval = 100;		/* default sleep */
150 	ic->ic_bmisstimeout = 7*ic->ic_lintval;	/* default 7 beacons */
151 
152 	ieee80211_node_attach(ifp);
153 	ieee80211_proto_attach(ifp);
154 }
155 
156 void
157 ieee80211_ifdetach(struct ifnet *ifp)
158 {
159 	struct ieee80211com *ic = (void *)ifp;
160 
161 	ieee80211_proto_detach(ifp);
162 	ieee80211_crypto_detach(ifp);
163 	ieee80211_node_detach(ifp);
164 	ifmedia_removeall(&ic->ic_media);
165 	bpfdetach(ifp);
166 	ether_ifdetach(ifp);
167 }
168 
169 /*
170  * Convert MHz frequency to IEEE channel number.
171  */
172 u_int
173 ieee80211_mhz2ieee(u_int freq, u_int flags)
174 {
175 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
176 		if (freq == 2484)
177 			return 14;
178 		if (freq < 2484)
179 			return (freq - 2407) / 5;
180 		else
181 			return 15 + ((freq - 2512) / 20);
182 	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
183 		return (freq - 5000) / 5;
184 	} else {				/* either, guess */
185 		if (freq == 2484)
186 			return 14;
187 		if (freq < 2484)
188 			return (freq - 2407) / 5;
189 		if (freq < 5000)
190 			return 15 + ((freq - 2512) / 20);
191 		return (freq - 5000) / 5;
192 	}
193 }
194 
195 /*
196  * Convert channel to IEEE channel number.
197  */
198 u_int
199 ieee80211_chan2ieee(struct ieee80211com *ic, struct ieee80211_channel *c)
200 {
201 	if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX])
202 		return c - ic->ic_channels;
203 	else if (c == IEEE80211_CHAN_ANYC)
204 		return IEEE80211_CHAN_ANY;
205 	else if (c != NULL) {
206 		if_printf(&ic->ic_if, "invalid channel freq %u flags %x\n",
207 			c->ic_freq, c->ic_flags);
208 		return 0;		/* XXX */
209 	} else {
210 		if_printf(&ic->ic_if, "invalid channel (NULL)\n");
211 		return 0;		/* XXX */
212 	}
213 }
214 
215 /*
216  * Convert IEEE channel number to MHz frequency.
217  */
218 u_int
219 ieee80211_ieee2mhz(u_int chan, u_int flags)
220 {
221 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
222 		if (chan == 14)
223 			return 2484;
224 		if (chan < 14)
225 			return 2407 + chan*5;
226 		else
227 			return 2512 + ((chan-15)*20);
228 	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
229 		return 5000 + (chan*5);
230 	} else {				/* either, guess */
231 		if (chan == 14)
232 			return 2484;
233 		if (chan < 14)			/* 0-13 */
234 			return 2407 + chan*5;
235 		if (chan < 27)			/* 15-26 */
236 			return 2512 + ((chan-15)*20);
237 		return 5000 + (chan*5);
238 	}
239 }
240 
241 /*
242  * Setup the media data structures according to the channel and
243  * rate tables.  This must be called by the driver after
244  * ieee80211_attach and before most anything else.
245  */
246 void
247 ieee80211_media_init(struct ifnet *ifp,
248 	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
249 {
250 #define	ADD(_ic, _s, _o) \
251 	ifmedia_add(&(_ic)->ic_media, \
252 		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
253 	struct ieee80211com *ic = (void *)ifp;
254 	struct ifmediareq imr;
255 	int i, j, mode, rate, maxrate, mword, mopt, r;
256 	struct ieee80211_rateset *rs;
257 	struct ieee80211_rateset allrates;
258 
259 	/*
260 	 * Do late attach work that must wait for any subclass
261 	 * (i.e. driver) work such as overriding methods.
262 	 */
263 	ieee80211_node_lateattach(ifp);
264 
265 	/*
266 	 * Fill in media characteristics.
267 	 */
268 	ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
269 	maxrate = 0;
270 	memset(&allrates, 0, sizeof(allrates));
271 	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) {
272 		static const u_int mopts[] = {
273 			IFM_AUTO,
274 			IFM_IEEE80211_11A,
275 			IFM_IEEE80211_11B,
276 			IFM_IEEE80211_11G,
277 			IFM_IEEE80211_FH,
278 			IFM_IEEE80211_11A | IFM_IEEE80211_TURBO,
279 		};
280 		if ((ic->ic_modecaps & (1<<mode)) == 0)
281 			continue;
282 		mopt = mopts[mode];
283 		ADD(ic, IFM_AUTO, mopt);	/* e.g. 11a auto */
284 		if (ic->ic_caps & IEEE80211_C_IBSS)
285 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC);
286 		if (ic->ic_caps & IEEE80211_C_HOSTAP)
287 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP);
288 		if (ic->ic_caps & IEEE80211_C_AHDEMO)
289 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
290 		if (ic->ic_caps & IEEE80211_C_MONITOR)
291 			ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR);
292 		if (mode == IEEE80211_MODE_AUTO)
293 			continue;
294 		if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
295 		rs = &ic->ic_sup_rates[mode];
296 		for (i = 0; i < rs->rs_nrates; i++) {
297 			rate = rs->rs_rates[i];
298 			mword = ieee80211_rate2media(ic, rate, mode);
299 			if (mword == 0)
300 				continue;
301 			printf("%s%d%sMbps", (i != 0 ? " " : ""),
302 			    (rate & IEEE80211_RATE_VAL) / 2,
303 			    ((rate & 0x1) != 0 ? ".5" : ""));
304 			ADD(ic, mword, mopt);
305 			if (ic->ic_caps & IEEE80211_C_IBSS)
306 				ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
307 			if (ic->ic_caps & IEEE80211_C_HOSTAP)
308 				ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
309 			if (ic->ic_caps & IEEE80211_C_AHDEMO)
310 				ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
311 			if (ic->ic_caps & IEEE80211_C_MONITOR)
312 				ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
313 			/*
314 			 * Add rate to the collection of all rates.
315 			 */
316 			r = rate & IEEE80211_RATE_VAL;
317 			for (j = 0; j < allrates.rs_nrates; j++)
318 				if (allrates.rs_rates[j] == r)
319 					break;
320 			if (j == allrates.rs_nrates) {
321 				/* unique, add to the set */
322 				allrates.rs_rates[j] = r;
323 				allrates.rs_nrates++;
324 			}
325 			rate = (rate & IEEE80211_RATE_VAL) / 2;
326 			if (rate > maxrate)
327 				maxrate = rate;
328 		}
329 		printf("\n");
330 	}
331 	for (i = 0; i < allrates.rs_nrates; i++) {
332 		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
333 				IEEE80211_MODE_AUTO);
334 		if (mword == 0)
335 			continue;
336 		mword = IFM_SUBTYPE(mword);	/* remove media options */
337 		ADD(ic, mword, 0);
338 		if (ic->ic_caps & IEEE80211_C_IBSS)
339 			ADD(ic, mword, IFM_IEEE80211_ADHOC);
340 		if (ic->ic_caps & IEEE80211_C_HOSTAP)
341 			ADD(ic, mword, IFM_IEEE80211_HOSTAP);
342 		if (ic->ic_caps & IEEE80211_C_AHDEMO)
343 			ADD(ic, mword, IFM_IEEE80211_ADHOC | IFM_FLAG0);
344 		if (ic->ic_caps & IEEE80211_C_MONITOR)
345 			ADD(ic, mword, IFM_IEEE80211_MONITOR);
346 	}
347 	ieee80211_media_status(ifp, &imr);
348 	ifmedia_set(&ic->ic_media, imr.ifm_active);
349 
350 	if (maxrate)
351 		ifp->if_baudrate = IF_Mbps(maxrate);
352 #undef ADD
353 }
354 
355 static int
356 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
357 {
358 #define	IEEERATE(_ic,_m,_i) \
359 	((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
360 	int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
361 	for (i = 0; i < nrates; i++)
362 		if (IEEERATE(ic, mode, i) == rate)
363 			return i;
364 	return -1;
365 #undef IEEERATE
366 }
367 
368 /*
369  * Handle a media change request.
370  */
371 int
372 ieee80211_media_change(struct ifnet *ifp)
373 {
374 	struct ieee80211com *ic = (void *)ifp;
375 	struct ifmedia_entry *ime;
376 	enum ieee80211_opmode newopmode;
377 	enum ieee80211_phymode newphymode;
378 	int i, j, newrate, error = 0;
379 
380 	ime = ic->ic_media.ifm_cur;
381 	/*
382 	 * First, identify the phy mode.
383 	 */
384 	switch (IFM_MODE(ime->ifm_media)) {
385 	case IFM_IEEE80211_11A:
386 		newphymode = IEEE80211_MODE_11A;
387 		break;
388 	case IFM_IEEE80211_11B:
389 		newphymode = IEEE80211_MODE_11B;
390 		break;
391 	case IFM_IEEE80211_11G:
392 		newphymode = IEEE80211_MODE_11G;
393 		break;
394 	case IFM_IEEE80211_FH:
395 		newphymode = IEEE80211_MODE_FH;
396 		break;
397 	case IFM_AUTO:
398 		newphymode = IEEE80211_MODE_AUTO;
399 		break;
400 	default:
401 		return EINVAL;
402 	}
403 	/*
404 	 * Turbo mode is an ``option''.  Eventually it
405 	 * needs to be applied to 11g too.
406 	 */
407 	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
408 		if (newphymode != IEEE80211_MODE_11A)
409 			return EINVAL;
410 		newphymode = IEEE80211_MODE_TURBO;
411 	}
412 	/*
413 	 * Validate requested mode is available.
414 	 */
415 	if ((ic->ic_modecaps & (1<<newphymode)) == 0)
416 		return EINVAL;
417 
418 	/*
419 	 * Next, the fixed/variable rate.
420 	 */
421 	i = -1;
422 	if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
423 		/*
424 		 * Convert media subtype to rate.
425 		 */
426 		newrate = ieee80211_media2rate(ime->ifm_media);
427 		if (newrate == 0)
428 			return EINVAL;
429 		/*
430 		 * Check the rate table for the specified/current phy.
431 		 */
432 		if (newphymode == IEEE80211_MODE_AUTO) {
433 			/*
434 			 * In autoselect mode search for the rate.
435 			 */
436 			for (j = IEEE80211_MODE_11A;
437 			     j < IEEE80211_MODE_MAX; j++) {
438 				if ((ic->ic_modecaps & (1<<j)) == 0)
439 					continue;
440 				i = findrate(ic, j, newrate);
441 				if (i != -1) {
442 					/* lock mode too */
443 					newphymode = j;
444 					break;
445 				}
446 			}
447 		} else {
448 			i = findrate(ic, newphymode, newrate);
449 		}
450 		if (i == -1)			/* mode/rate mismatch */
451 			return EINVAL;
452 	}
453 	/* NB: defer rate setting to later */
454 
455 	/*
456 	 * Deduce new operating mode but don't install it just yet.
457 	 */
458 	if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) ==
459 	    (IFM_IEEE80211_ADHOC|IFM_FLAG0))
460 		newopmode = IEEE80211_M_AHDEMO;
461 	else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
462 		newopmode = IEEE80211_M_HOSTAP;
463 	else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
464 		newopmode = IEEE80211_M_IBSS;
465 	else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
466 		newopmode = IEEE80211_M_MONITOR;
467 	else
468 		newopmode = IEEE80211_M_STA;
469 
470 	/*
471 	 * Autoselect doesn't make sense when operating as an AP.
472 	 * If no phy mode has been selected, pick one and lock it
473 	 * down so rate tables can be used in forming beacon frames
474 	 * and the like.
475 	 */
476 	if (newopmode == IEEE80211_M_HOSTAP &&
477 	    newphymode == IEEE80211_MODE_AUTO) {
478 		for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++)
479 			if (ic->ic_modecaps & (1<<j)) {
480 				newphymode = j;
481 				break;
482 			}
483 	}
484 
485 	/*
486 	 * Handle phy mode change.
487 	 */
488 	if (ic->ic_curmode != newphymode) {		/* change phy mode */
489 		error = ieee80211_setmode(ic, newphymode);
490 		if (error != 0)
491 			return error;
492 		error = ENETRESET;
493 	}
494 
495 	/*
496 	 * Committed to changes, install the rate setting.
497 	 */
498 	if (ic->ic_fixed_rate != i) {
499 		ic->ic_fixed_rate = i;			/* set fixed tx rate */
500 		error = ENETRESET;
501 	}
502 
503 	/*
504 	 * Handle operating mode change.
505 	 */
506 	if (ic->ic_opmode != newopmode) {
507 		ic->ic_opmode = newopmode;
508 		switch (newopmode) {
509 		case IEEE80211_M_AHDEMO:
510 		case IEEE80211_M_HOSTAP:
511 		case IEEE80211_M_STA:
512 		case IEEE80211_M_MONITOR:
513 			ic->ic_flags &= ~IEEE80211_F_IBSSON;
514 			break;
515 		case IEEE80211_M_IBSS:
516 			ic->ic_flags |= IEEE80211_F_IBSSON;
517 #ifdef notdef
518 			if (ic->ic_curmode == IEEE80211_MODE_11G)
519 				ieee80211_set11gbasicrates(
520 					&ic->ic_suprates[newphymode],
521 					IEEE80211_MODE_11B);
522 #endif
523 			break;
524 		}
525 		error = ENETRESET;
526 	}
527 #ifdef notdef
528 	if (error == 0)
529 		ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
530 #endif
531 	return error;
532 }
533 
534 void
535 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
536 {
537 	struct ieee80211com *ic = (void *)ifp;
538 	struct ieee80211_node *ni = NULL;
539 	int old_status = imr->ifm_status;
540 
541 	imr->ifm_status = IFM_AVALID;
542 	imr->ifm_active = IFM_IEEE80211;
543 	if (ic->ic_state == IEEE80211_S_RUN) {
544 		imr->ifm_status |= IFM_ACTIVE;
545 		ifp->if_link_state = LINK_STATE_UP;
546 	} else
547 		ifp->if_link_state = LINK_STATE_DOWN;
548 	imr->ifm_active |= IFM_AUTO;
549 	switch (ic->ic_opmode) {
550 	case IEEE80211_M_STA:
551 		ni = ic->ic_bss;
552 		/* calculate rate subtype */
553 		imr->ifm_active |= ieee80211_rate2media(ic,
554 			ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode);
555 		break;
556 	case IEEE80211_M_IBSS:
557 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
558 		break;
559 	case IEEE80211_M_AHDEMO:
560 		/* should not come here */
561 		break;
562 	case IEEE80211_M_HOSTAP:
563 		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
564 		break;
565 	case IEEE80211_M_MONITOR:
566 		imr->ifm_active |= IFM_IEEE80211_MONITOR;
567 		break;
568 	}
569 	switch (ic->ic_curmode) {
570 	case IEEE80211_MODE_11A:
571 		imr->ifm_active |= IFM_IEEE80211_11A;
572 		break;
573 	case IEEE80211_MODE_11B:
574 		imr->ifm_active |= IFM_IEEE80211_11B;
575 		break;
576 	case IEEE80211_MODE_11G:
577 		imr->ifm_active |= IFM_IEEE80211_11G;
578 		break;
579 	case IEEE80211_MODE_FH:
580 		imr->ifm_active |= IFM_IEEE80211_FH;
581 		break;
582 	case IEEE80211_MODE_TURBO:
583 		imr->ifm_active |= IFM_IEEE80211_11A
584 				|  IFM_IEEE80211_TURBO;
585 		break;
586 	}
587 
588 	/* Notify that the link state has changed. */
589 	if (imr->ifm_status != old_status)
590 		rt_ifmsg(ifp);
591 }
592 
593 void
594 ieee80211_watchdog(struct ifnet *ifp)
595 {
596 	struct ieee80211com *ic = (void *)ifp;
597 
598 	if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0)
599 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
600 	if (ic->ic_inact_timer && --ic->ic_inact_timer == 0)
601 		ieee80211_timeout_nodes(ic);
602 
603 	if (ic->ic_mgt_timer != 0 || ic->ic_inact_timer != 0)
604 		ifp->if_timer = 1;
605 }
606 
607 /*
608  * Mark the basic rates for the 11g rate table based on the
609  * operating mode.  For real 11g we mark all the 11b rates
610  * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
611  * 11b rates.  There's also a pseudo 11a-mode used to mark only
612  * the basic OFDM rates.
613  */
614 static void
615 ieee80211_set11gbasicrates(struct ieee80211_rateset *rs, enum ieee80211_phymode mode)
616 {
617 	static const struct ieee80211_rateset basic[] = {
618 	    { 3, { 12, 24, 48 } },		/* IEEE80211_MODE_11A */
619 	    { 4, { 2, 4, 11, 22 } },		/* IEEE80211_MODE_11B */
620 	    { 7, { 2, 4, 11, 22, 12, 24, 48 } },/* IEEE80211_MODE_11G */
621 	    { 0 },				/* IEEE80211_MODE_FH */
622 	    { 0 },				/* IEEE80211_MODE_TURBO	*/
623 	};
624 	int i, j;
625 
626 	for (i = 0; i < rs->rs_nrates; i++) {
627 		rs->rs_rates[i] &= IEEE80211_RATE_VAL;
628 		for (j = 0; j < basic[mode].rs_nrates; j++)
629 			if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
630 				rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
631 				break;
632 			}
633 	}
634 }
635 
636 /*
637  * Set the current phy mode and recalculate the active channel
638  * set based on the available channels for this mode.  Also
639  * select a new default/current channel if the current one is
640  * inappropriate for this mode.
641  */
642 int
643 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
644 {
645 #define	N(a)	(sizeof(a) / sizeof(a[0]))
646 	static const u_int chanflags[] = {
647 		0,			/* IEEE80211_MODE_AUTO */
648 		IEEE80211_CHAN_A,	/* IEEE80211_MODE_11A */
649 		IEEE80211_CHAN_B,	/* IEEE80211_MODE_11B */
650 		IEEE80211_CHAN_PUREG,	/* IEEE80211_MODE_11G */
651 		IEEE80211_CHAN_FHSS,	/* IEEE80211_MODE_FH */
652 		IEEE80211_CHAN_T,	/* IEEE80211_MODE_TURBO	*/
653 	};
654 	struct ieee80211_channel *c;
655 	u_int modeflags;
656 	int i;
657 
658 	/* validate new mode */
659 	if ((ic->ic_modecaps & (1<<mode)) == 0) {
660 		IEEE80211_DPRINTF(("%s: mode %u not supported (caps 0x%x)\n",
661 			__func__, mode, ic->ic_modecaps));
662 		return EINVAL;
663 	}
664 
665 	/*
666 	 * Verify at least one channel is present in the available
667 	 * channel list before committing to the new mode.
668 	 */
669 	KASSERT(mode < N(chanflags), ("Unexpected mode %u", mode));
670 	modeflags = chanflags[mode];
671 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
672 		c = &ic->ic_channels[i];
673 		if (mode == IEEE80211_MODE_AUTO) {
674 			/* ignore turbo channels for autoselect */
675 			if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
676 				break;
677 		} else {
678 			if ((c->ic_flags & modeflags) == modeflags)
679 				break;
680 		}
681 	}
682 	if (i > IEEE80211_CHAN_MAX) {
683 		IEEE80211_DPRINTF(("%s: no channels found for mode %u\n",
684 			__func__, mode));
685 		return EINVAL;
686 	}
687 
688 	/*
689 	 * Calculate the active channel set.
690 	 */
691 	memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
692 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
693 		c = &ic->ic_channels[i];
694 		if (mode == IEEE80211_MODE_AUTO) {
695 			/* take anything but pure turbo channels */
696 			if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
697 				setbit(ic->ic_chan_active, i);
698 		} else {
699 			if ((c->ic_flags & modeflags) == modeflags)
700 				setbit(ic->ic_chan_active, i);
701 		}
702 	}
703 	/*
704 	 * If no current/default channel is setup or the current
705 	 * channel is wrong for the mode then pick the first
706 	 * available channel from the active list.  This is likely
707 	 * not the right one.
708 	 */
709 	if (ic->ic_ibss_chan == NULL ||
710 	    isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
711 		for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
712 			if (isset(ic->ic_chan_active, i)) {
713 				ic->ic_ibss_chan = &ic->ic_channels[i];
714 				break;
715 			}
716 	}
717 
718 	/*
719 	 * Set/reset state flags that influence beacon contents, etc.
720 	 *
721 	 * XXX what if we have stations already associated???
722 	 * XXX probably not right for autoselect?
723 	 */
724 	if (ic->ic_caps & IEEE80211_C_SHPREAMBLE)
725 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
726 	if (mode == IEEE80211_MODE_11G) {
727 		if (ic->ic_caps & IEEE80211_C_SHSLOT)
728 			ic->ic_flags |= IEEE80211_F_SHSLOT;
729 		ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode],
730 			IEEE80211_MODE_11G);
731 	} else {
732 		ic->ic_flags &= ~IEEE80211_F_SHSLOT;
733 	}
734 
735 	ic->ic_curmode = mode;
736 	return 0;
737 #undef N
738 }
739 
740 /*
741  * Return the phy mode for with the specified channel so the
742  * caller can select a rate set.  This is problematic and the
743  * work here assumes how things work elsewhere in this code.
744  */
745 enum ieee80211_phymode
746 ieee80211_chan2mode(struct ieee80211com *ic, struct ieee80211_channel *chan)
747 {
748 	/*
749 	 * NB: this assumes the channel would not be supplied to us
750 	 *     unless it was already compatible with the current mode.
751 	 */
752 	if (ic->ic_curmode != IEEE80211_MODE_AUTO)
753 		return ic->ic_curmode;
754 	/*
755 	 * In autoselect mode; deduce a mode based on the channel
756 	 * characteristics.  We assume that turbo-only channels
757 	 * are not considered when the channel set is constructed.
758 	 */
759 	if (IEEE80211_IS_CHAN_5GHZ(chan))
760 		return IEEE80211_MODE_11A;
761 	else if (IEEE80211_IS_CHAN_FHSS(chan))
762 		return IEEE80211_MODE_FH;
763 	else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN))
764 		return IEEE80211_MODE_11G;
765 	else
766 		return IEEE80211_MODE_11B;
767 }
768 
769 /*
770  * convert IEEE80211 rate value to ifmedia subtype.
771  * ieee80211 rate is in unit of 0.5Mbps.
772  */
773 int
774 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
775 {
776 #define	N(a)	(sizeof(a) / sizeof(a[0]))
777 	static const struct {
778 		u_int	m;	/* rate + mode */
779 		u_int	r;	/* if_media rate */
780 	} rates[] = {
781 		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
782 		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
783 		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
784 		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
785 		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
786 		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
787 		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
788 		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
789 		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
790 		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
791 		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
792 		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
793 		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
794 		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
795 		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
796 		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
797 		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
798 		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
799 		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
800 		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
801 		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
802 		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
803 		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
804 		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
805 		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
806 		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
807 		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
808 		/* NB: OFDM72 doesn't realy exist so we don't handle it */
809 	};
810 	u_int mask, i;
811 
812 	mask = rate & IEEE80211_RATE_VAL;
813 	switch (mode) {
814 	case IEEE80211_MODE_11A:
815 	case IEEE80211_MODE_TURBO:
816 		mask |= IFM_IEEE80211_11A;
817 		break;
818 	case IEEE80211_MODE_11B:
819 		mask |= IFM_IEEE80211_11B;
820 		break;
821 	case IEEE80211_MODE_FH:
822 		mask |= IFM_IEEE80211_FH;
823 		break;
824 	case IEEE80211_MODE_AUTO:
825 		/* NB: ic may be NULL for some drivers */
826 		if (ic && ic->ic_phytype == IEEE80211_T_FH) {
827 			mask |= IFM_IEEE80211_FH;
828 			break;
829 		}
830 		/* NB: hack, 11g matches both 11b+11a rates */
831 		/* fall thru... */
832 	case IEEE80211_MODE_11G:
833 		mask |= IFM_IEEE80211_11G;
834 		break;
835 	}
836 	for (i = 0; i < N(rates); i++)
837 		if (rates[i].m == mask)
838 			return rates[i].r;
839 	return IFM_AUTO;
840 #undef N
841 }
842 
843 int
844 ieee80211_media2rate(int mword)
845 {
846 #define	N(a)	(sizeof(a) / sizeof(a[0]))
847 	static const int ieeerates[] = {
848 		-1,		/* IFM_AUTO */
849 		0,		/* IFM_MANUAL */
850 		0,		/* IFM_NONE */
851 		2,		/* IFM_IEEE80211_FH1 */
852 		4,		/* IFM_IEEE80211_FH2 */
853 		2,		/* IFM_IEEE80211_DS1 */
854 		4,		/* IFM_IEEE80211_DS2 */
855 		11,		/* IFM_IEEE80211_DS5 */
856 		22,		/* IFM_IEEE80211_DS11 */
857 		44,		/* IFM_IEEE80211_DS22 */
858 		12,		/* IFM_IEEE80211_OFDM6 */
859 		18,		/* IFM_IEEE80211_OFDM9 */
860 		24,		/* IFM_IEEE80211_OFDM12 */
861 		36,		/* IFM_IEEE80211_OFDM18 */
862 		48,		/* IFM_IEEE80211_OFDM24 */
863 		72,		/* IFM_IEEE80211_OFDM36 */
864 		96,		/* IFM_IEEE80211_OFDM48 */
865 		108,		/* IFM_IEEE80211_OFDM54 */
866 		144,		/* IFM_IEEE80211_OFDM72 */
867 	};
868 	return IFM_SUBTYPE(mword) < N(ieeerates) ?
869 		ieeerates[IFM_SUBTYPE(mword)] : 0;
870 #undef N
871 }
872 
873 /*
874  * Module glue.
875  *
876  * NB: the module name is "wlan" for compatibility with NetBSD.
877  */
878 
879 static int
880 ieee80211_modevent(module_t mod, int type, void *unused)
881 {
882 	switch (type) {
883 	case MOD_LOAD:
884 		if (bootverbose)
885 			printf("wlan: <802.11 Link Layer>\n");
886 		return 0;
887 	case MOD_UNLOAD:
888 		return 0;
889 	}
890 	return EINVAL;
891 }
892 
893 static moduledata_t ieee80211_mod = {
894 	"wlan",
895 	ieee80211_modevent,
896 	0
897 };
898 DECLARE_MODULE(wlan, ieee80211_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
899 MODULE_VERSION(wlan, 1);
900 MODULE_DEPEND(wlan, rc4, 1, 1, 1);
901 MODULE_DEPEND(wlan, ether, 1, 1, 1);
902