xref: /openbsd-src/sys/net80211/ieee80211.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: ieee80211.c,v 1.40 2013/11/21 16:16:08 mpi 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 
54 #if NBPFILTER > 0
55 #include <net/bpf.h>
56 #endif
57 
58 #ifdef INET
59 #include <netinet/in.h>
60 #include <netinet/if_ether.h>
61 #endif
62 
63 #include <net80211/ieee80211_var.h>
64 #include <net80211/ieee80211_priv.h>
65 
66 #ifdef IEEE80211_DEBUG
67 int	ieee80211_debug = 0;
68 #endif
69 
70 int ieee80211_cache_size = IEEE80211_CACHE_SIZE;
71 
72 struct ieee80211com_head ieee80211com_head =
73     LIST_HEAD_INITIALIZER(ieee80211com_head);
74 
75 void ieee80211_setbasicrates(struct ieee80211com *);
76 int ieee80211_findrate(struct ieee80211com *, enum ieee80211_phymode, int);
77 
78 void
79 ieee80211_ifattach(struct ifnet *ifp)
80 {
81 	struct ieee80211com *ic = (void *)ifp;
82 	struct ieee80211_channel *c;
83 	int i;
84 
85 	memcpy(((struct arpcom *)ifp)->ac_enaddr, ic->ic_myaddr,
86 		ETHER_ADDR_LEN);
87 	ether_ifattach(ifp);
88 
89 	ifp->if_output = ieee80211_output;
90 
91 #if NBPFILTER > 0
92 	bpfattach(&ic->ic_rawbpf, ifp, DLT_IEEE802_11,
93 	    sizeof(struct ieee80211_frame_addr4));
94 #endif
95 	ieee80211_crypto_attach(ifp);
96 
97 	/*
98 	 * Fill in 802.11 available channel set, mark
99 	 * all available channels as active, and pick
100 	 * a default channel if not already specified.
101 	 */
102 	memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
103 	ic->ic_modecaps |= 1<<IEEE80211_MODE_AUTO;
104 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
105 		c = &ic->ic_channels[i];
106 		if (c->ic_flags) {
107 			/*
108 			 * Verify driver passed us valid data.
109 			 */
110 			if (i != ieee80211_chan2ieee(ic, c)) {
111 				printf("%s: bad channel ignored; "
112 					"freq %u flags %x number %u\n",
113 					ifp->if_xname, c->ic_freq, c->ic_flags,
114 					i);
115 				c->ic_flags = 0;	/* NB: remove */
116 				continue;
117 			}
118 			setbit(ic->ic_chan_avail, i);
119 			/*
120 			 * Identify mode capabilities.
121 			 */
122 			if (IEEE80211_IS_CHAN_A(c))
123 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11A;
124 			if (IEEE80211_IS_CHAN_B(c))
125 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11B;
126 			if (IEEE80211_IS_CHAN_PUREG(c))
127 				ic->ic_modecaps |= 1<<IEEE80211_MODE_11G;
128 			if (IEEE80211_IS_CHAN_T(c))
129 				ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO;
130 		}
131 	}
132 	/* validate ic->ic_curmode */
133 	if ((ic->ic_modecaps & (1<<ic->ic_curmode)) == 0)
134 		ic->ic_curmode = IEEE80211_MODE_AUTO;
135 	ic->ic_des_chan = IEEE80211_CHAN_ANYC;	/* any channel is ok */
136 	ic->ic_scan_lock = IEEE80211_SCAN_UNLOCKED;
137 
138 	/* IEEE 802.11 defines a MTU >= 2290 */
139 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
140 
141 	ieee80211_setbasicrates(ic);
142 	(void)ieee80211_setmode(ic, ic->ic_curmode);
143 
144 	if (ic->ic_lintval == 0)
145 		ic->ic_lintval = 100;		/* default sleep */
146 	ic->ic_bmisstimeout = 7*ic->ic_lintval;	/* default 7 beacons */
147 	ic->ic_dtim_period = 1;	/* all TIMs are DTIMs */
148 
149 	LIST_INSERT_HEAD(&ieee80211com_head, ic, ic_list);
150 	ieee80211_node_attach(ifp);
151 	ieee80211_proto_attach(ifp);
152 
153 	if_addgroup(ifp, "wlan");
154 	ifp->if_priority = IF_WIRELESS_DEFAULT_PRIORITY;
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 	    (ic->ic_opmode != IEEE80211_M_STA ||
551 	     !(ic->ic_flags & IEEE80211_F_RSNON) ||
552 	     ic->ic_bss->ni_port_valid))
553 		imr->ifm_status |= IFM_ACTIVE;
554 	imr->ifm_active |= IFM_AUTO;
555 	switch (ic->ic_opmode) {
556 	case IEEE80211_M_STA:
557 		ni = ic->ic_bss;
558 		/* calculate rate subtype */
559 		imr->ifm_active |= ieee80211_rate2media(ic,
560 			ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode);
561 		break;
562 #ifndef IEEE80211_STA_ONLY
563 	case IEEE80211_M_IBSS:
564 		imr->ifm_active |= IFM_IEEE80211_IBSS;
565 		break;
566 	case IEEE80211_M_AHDEMO:
567 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
568 		break;
569 	case IEEE80211_M_HOSTAP:
570 		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
571 		break;
572 #endif
573 	case IEEE80211_M_MONITOR:
574 		imr->ifm_active |= IFM_IEEE80211_MONITOR;
575 		break;
576 	default:
577 		break;
578 	}
579 	switch (ic->ic_curmode) {
580 	case IEEE80211_MODE_11A:
581 		imr->ifm_active |= IFM_IEEE80211_11A;
582 		break;
583 	case IEEE80211_MODE_11B:
584 		imr->ifm_active |= IFM_IEEE80211_11B;
585 		break;
586 	case IEEE80211_MODE_11G:
587 		imr->ifm_active |= IFM_IEEE80211_11G;
588 		break;
589 	case IEEE80211_MODE_TURBO:
590 		imr->ifm_active |= IFM_IEEE80211_11A
591 				|  IFM_IEEE80211_TURBO;
592 		break;
593 	}
594 }
595 
596 void
597 ieee80211_watchdog(struct ifnet *ifp)
598 {
599 	struct ieee80211com *ic = (void *)ifp;
600 
601 	if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0)
602 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
603 
604 	if (ic->ic_mgt_timer != 0)
605 		ifp->if_timer = 1;
606 }
607 
608 const struct ieee80211_rateset ieee80211_std_rateset_11a =
609 	{ 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
610 
611 const struct ieee80211_rateset ieee80211_std_rateset_11b =
612 	{ 4, { 2, 4, 11, 22 } };
613 
614 const struct ieee80211_rateset ieee80211_std_rateset_11g =
615 	{ 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
616 
617 /*
618  * Mark the basic rates for the 11g rate table based on the
619  * operating mode.  For real 11g we mark all the 11b rates
620  * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
621  * 11b rates.  There's also a pseudo 11a-mode used to mark only
622  * the basic OFDM rates.
623  */
624 void
625 ieee80211_setbasicrates(struct ieee80211com *ic)
626 {
627 	static const struct ieee80211_rateset basic[] = {
628 	    { 0 },				/* IEEE80211_MODE_AUTO */
629 	    { 3, { 12, 24, 48 } },		/* IEEE80211_MODE_11A */
630 	    { 2, { 2, 4 } },			/* IEEE80211_MODE_11B */
631 	    { 4, { 2, 4, 11, 22 } },		/* IEEE80211_MODE_11G */
632 	    { 0 },				/* IEEE80211_MODE_TURBO	*/
633 	};
634 	enum ieee80211_phymode mode;
635 	struct ieee80211_rateset *rs;
636 	int i, j;
637 
638 	for (mode = 0; mode < IEEE80211_MODE_MAX; mode++) {
639 		rs = &ic->ic_sup_rates[mode];
640 		for (i = 0; i < rs->rs_nrates; i++) {
641 			rs->rs_rates[i] &= IEEE80211_RATE_VAL;
642 			for (j = 0; j < basic[mode].rs_nrates; j++) {
643 				if (basic[mode].rs_rates[j] ==
644 				    rs->rs_rates[i]) {
645 					rs->rs_rates[i] |=
646 					    IEEE80211_RATE_BASIC;
647 					break;
648 				}
649 			}
650 		}
651 	}
652 }
653 
654 /*
655  * Set the current phy mode and recalculate the active channel
656  * set based on the available channels for this mode.  Also
657  * select a new default/current channel if the current one is
658  * inappropriate for this mode.
659  */
660 int
661 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
662 {
663 #define	N(a)	(sizeof(a) / sizeof(a[0]))
664 	struct ifnet *ifp = &ic->ic_if;
665 	static const u_int chanflags[] = {
666 		0,			/* IEEE80211_MODE_AUTO */
667 		IEEE80211_CHAN_A,	/* IEEE80211_MODE_11A */
668 		IEEE80211_CHAN_B,	/* IEEE80211_MODE_11B */
669 		IEEE80211_CHAN_PUREG,	/* IEEE80211_MODE_11G */
670 		IEEE80211_CHAN_T,	/* IEEE80211_MODE_TURBO	*/
671 	};
672 	const struct ieee80211_channel *c;
673 	u_int modeflags;
674 	int i;
675 
676 	/* validate new mode */
677 	if ((ic->ic_modecaps & (1<<mode)) == 0) {
678 		DPRINTF(("mode %u not supported (caps 0x%x)\n",
679 		    mode, ic->ic_modecaps));
680 		return EINVAL;
681 	}
682 
683 	/*
684 	 * Verify at least one channel is present in the available
685 	 * channel list before committing to the new mode.
686 	 */
687 	if (mode >= N(chanflags))
688 		panic("Unexpected mode %u", mode);
689 	modeflags = chanflags[mode];
690 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
691 		c = &ic->ic_channels[i];
692 		if (mode == IEEE80211_MODE_AUTO) {
693 			/* ignore turbo channels for autoselect */
694 			if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
695 				break;
696 		} else {
697 			if ((c->ic_flags & modeflags) == modeflags)
698 				break;
699 		}
700 	}
701 	if (i > IEEE80211_CHAN_MAX) {
702 		DPRINTF(("no channels found for mode %u\n", mode));
703 		return EINVAL;
704 	}
705 
706 	/*
707 	 * Calculate the active channel set.
708 	 */
709 	memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
710 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
711 		c = &ic->ic_channels[i];
712 		if (mode == IEEE80211_MODE_AUTO) {
713 			/* take anything but pure turbo channels */
714 			if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
715 				setbit(ic->ic_chan_active, i);
716 		} else {
717 			if ((c->ic_flags & modeflags) == modeflags)
718 				setbit(ic->ic_chan_active, i);
719 		}
720 	}
721 	/*
722 	 * If no current/default channel is setup or the current
723 	 * channel is wrong for the mode then pick the first
724 	 * available channel from the active list.  This is likely
725 	 * not the right one.
726 	 */
727 	if (ic->ic_ibss_chan == NULL || isclr(ic->ic_chan_active,
728 	    ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
729 		for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
730 			if (isset(ic->ic_chan_active, i)) {
731 				ic->ic_ibss_chan = &ic->ic_channels[i];
732 				break;
733 			}
734 		if ((ic->ic_ibss_chan == NULL) || isclr(ic->ic_chan_active,
735 		    ieee80211_chan2ieee(ic, ic->ic_ibss_chan)))
736 			panic("Bad IBSS channel %u",
737 			    ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
738 	}
739 
740 	/*
741 	 * Reset the scan state for the new mode. This avoids scanning
742 	 * of invalid channels, ie. 5GHz channels in 11b mode.
743 	 */
744 	ieee80211_reset_scan(ifp);
745 
746 	ic->ic_curmode = mode;
747 	ieee80211_reset_erp(ic);	/* reset ERP state */
748 
749 	return 0;
750 #undef N
751 }
752 
753 enum ieee80211_phymode
754 ieee80211_next_mode(struct ifnet *ifp)
755 {
756 	struct ieee80211com *ic = (void *)ifp;
757 
758 	if (IFM_MODE(ic->ic_media.ifm_cur->ifm_media) != IFM_AUTO) {
759 		/*
760 		 * Reset the scan state and indicate a wrap around
761 		 * if we're running in a fixed, user-specified phy mode.
762 		 */
763 		ieee80211_reset_scan(ifp);
764 		return (IEEE80211_MODE_AUTO);
765 	}
766 
767 	/*
768 	 * Get the next supported mode
769 	 */
770 	for (++ic->ic_curmode;
771 	    ic->ic_curmode <= IEEE80211_MODE_TURBO;
772 	    ic->ic_curmode++) {
773 		/* Wrap around and ignore turbo mode */
774 		if (ic->ic_curmode >= IEEE80211_MODE_TURBO) {
775 			ic->ic_curmode = IEEE80211_MODE_AUTO;
776 			break;
777 		}
778 
779 		if (ic->ic_modecaps & (1 << ic->ic_curmode))
780 			break;
781 	}
782 
783 	ieee80211_setmode(ic, ic->ic_curmode);
784 
785 	return (ic->ic_curmode);
786 }
787 
788 /*
789  * Return the phy mode for with the specified channel so the
790  * caller can select a rate set.  This is problematic and the
791  * work here assumes how things work elsewhere in this code.
792  *
793  * XXX never returns turbo modes -dcy
794  */
795 enum ieee80211_phymode
796 ieee80211_chan2mode(struct ieee80211com *ic,
797     const struct ieee80211_channel *chan)
798 {
799 	/*
800 	 * NB: this assumes the channel would not be supplied to us
801 	 *     unless it was already compatible with the current mode.
802 	 */
803 	if (ic->ic_curmode != IEEE80211_MODE_AUTO ||
804 	    chan == IEEE80211_CHAN_ANYC)
805 		return ic->ic_curmode;
806 	/*
807 	 * In autoselect mode; deduce a mode based on the channel
808 	 * characteristics.  We assume that turbo-only channels
809 	 * are not considered when the channel set is constructed.
810 	 */
811 	if (IEEE80211_IS_CHAN_T(chan))
812 		return IEEE80211_MODE_TURBO;
813 	else if (IEEE80211_IS_CHAN_5GHZ(chan))
814 		return IEEE80211_MODE_11A;
815 	else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN))
816 		return IEEE80211_MODE_11G;
817 	else
818 		return IEEE80211_MODE_11B;
819 }
820 
821 /*
822  * convert IEEE80211 rate value to ifmedia subtype.
823  * ieee80211 rate is in unit of 0.5Mbps.
824  */
825 int
826 ieee80211_rate2media(struct ieee80211com *ic, int rate,
827     enum ieee80211_phymode mode)
828 {
829 #define	N(a)	(sizeof(a) / sizeof(a[0]))
830 	static const struct {
831 		u_int	m;	/* rate + mode */
832 		u_int	r;	/* if_media rate */
833 	} rates[] = {
834 		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
835 		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
836 		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
837 		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
838 		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
839 		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
840 		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
841 		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
842 		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
843 		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
844 		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
845 		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
846 		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
847 		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
848 		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
849 		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
850 		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
851 		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
852 		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
853 		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
854 		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
855 		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
856 		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
857 		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
858 		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
859 		/* NB: OFDM72 doesn't really exist so we don't handle it */
860 	};
861 	u_int mask, i;
862 
863 	mask = rate & IEEE80211_RATE_VAL;
864 	switch (mode) {
865 	case IEEE80211_MODE_11A:
866 	case IEEE80211_MODE_TURBO:
867 		mask |= IFM_IEEE80211_11A;
868 		break;
869 	case IEEE80211_MODE_11B:
870 		mask |= IFM_IEEE80211_11B;
871 		break;
872 	case IEEE80211_MODE_AUTO:
873 		/* NB: hack, 11g matches both 11b+11a rates */
874 		/* FALLTHROUGH */
875 	case IEEE80211_MODE_11G:
876 		mask |= IFM_IEEE80211_11G;
877 		break;
878 	}
879 	for (i = 0; i < N(rates); i++)
880 		if (rates[i].m == mask)
881 			return rates[i].r;
882 	return IFM_AUTO;
883 #undef N
884 }
885 
886 int
887 ieee80211_media2rate(int mword)
888 {
889 #define	N(a)	(sizeof(a) / sizeof(a[0]))
890 	int i;
891 	static const struct {
892 		int subtype;
893 		int rate;
894 	} ieeerates[] = {
895 		{ IFM_AUTO,		-1	},
896 		{ IFM_MANUAL,		0	},
897 		{ IFM_NONE,		0	},
898 		{ IFM_IEEE80211_DS1,	2	},
899 		{ IFM_IEEE80211_DS2,	4	},
900 		{ IFM_IEEE80211_DS5,	11	},
901 		{ IFM_IEEE80211_DS11,	22	},
902 		{ IFM_IEEE80211_DS22,	44	},
903 		{ IFM_IEEE80211_OFDM6,	12	},
904 		{ IFM_IEEE80211_OFDM9,	18	},
905 		{ IFM_IEEE80211_OFDM12,	24	},
906 		{ IFM_IEEE80211_OFDM18,	36	},
907 		{ IFM_IEEE80211_OFDM24,	48	},
908 		{ IFM_IEEE80211_OFDM36,	72	},
909 		{ IFM_IEEE80211_OFDM48,	96	},
910 		{ IFM_IEEE80211_OFDM54,	108	},
911 		{ IFM_IEEE80211_OFDM72,	144	},
912 	};
913 	for (i = 0; i < N(ieeerates); i++) {
914 		if (ieeerates[i].subtype == IFM_SUBTYPE(mword))
915 			return ieeerates[i].rate;
916 	}
917 	return 0;
918 #undef N
919 }
920 
921 /*
922  * Convert bit rate (in 0.5Mbps units) to PLCP signal (R4-R1) and vice versa.
923  */
924 u_int8_t
925 ieee80211_rate2plcp(u_int8_t rate, enum ieee80211_phymode mode)
926 {
927 	rate &= IEEE80211_RATE_VAL;
928 
929 	if (mode == IEEE80211_MODE_11B) {
930 		/* IEEE Std 802.11b-1999 page 15, subclause 18.2.3.3 */
931 		switch (rate) {
932 		case 2:		return 10;
933 		case 4:		return 20;
934 		case 11:	return 55;
935 		case 22:	return 110;
936 		/* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */
937 		case 44:	return 220;
938 		}
939 	} else if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11A) {
940 		/* IEEE Std 802.11a-1999 page 14, subclause 17.3.4.1 */
941 		switch (rate) {
942 		case 12:	return 0x0b;
943 		case 18:	return 0x0f;
944 		case 24:	return 0x0a;
945 		case 36:	return 0x0e;
946 		case 48:	return 0x09;
947 		case 72:	return 0x0d;
948 		case 96:	return 0x08;
949 		case 108:	return 0x0c;
950 		}
951         } else
952 		panic("Unexpected mode %u", mode);
953 
954 	DPRINTF(("unsupported rate %u\n", rate));
955 
956 	return 0;
957 }
958 
959 u_int8_t
960 ieee80211_plcp2rate(u_int8_t plcp, enum ieee80211_phymode mode)
961 {
962 	if (mode == IEEE80211_MODE_11B) {
963 		/* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */
964 		switch (plcp) {
965 		case 10:	return 2;
966 		case 20:	return 4;
967 		case 55:	return 11;
968 		case 110:	return 22;
969 		/* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */
970 		case 220:	return 44;
971 		}
972 	} else if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11A) {
973 		/* IEEE Std 802.11a-1999 page 14, subclause 17.3.4.1 */
974 		switch (plcp) {
975 		case 0x0b:	return 12;
976 		case 0x0f:	return 18;
977 		case 0x0a:	return 24;
978 		case 0x0e:	return 36;
979 		case 0x09:	return 48;
980 		case 0x0d:	return 72;
981 		case 0x08:	return 96;
982 		case 0x0c:	return 108;
983 		}
984 	} else
985 		panic("unexpected mode %u", mode);
986 
987 	DPRINTF(("unsupported plcp %u\n", plcp));
988 
989 	return 0;
990 }
991