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