2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
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.
32 * $FreeBSD: src/sys/net80211/ieee80211.c,v 1.13 2004/05/30 17:57:45 phk Exp $
33 * $DragonFly: src/sys/netproto/802_11/Attic/ieee80211.c,v 1.5 2006/01/14 13:36:39 swildner Exp $
37 * IEEE 802.11 generic handler
42 #include <sys/param.h>
43 #include <sys/systm.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>
54 #include <sys/sysctl.h>
56 #include <machine/atomic.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>
66 #include <netproto/802_11/ieee80211_var.h>
71 #include <netinet/in.h>
72 #include <netinet/if_ether.h>
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");
81 static void ieee80211_set11gbasicrates(struct ieee80211_rateset *,
82 enum ieee80211_phymode);
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 */
94 ieee80211_ifattach(struct ifnet *ifp)
96 struct ieee80211com *ic = (void *)ifp;
97 struct ieee80211_channel *c;
100 ether_ifattach(ifp, ic->ic_myaddr, NULL);
101 bpfattach_dlt(ifp, DLT_IEEE802_11,
102 sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf);
103 ieee80211_crypto_attach(ifp);
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.
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];
116 * Verify driver passed us valid data.
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 */
125 setbit(ic->ic_chan_avail, i);
127 * Identify mode capabilities.
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;
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 */
146 ieee80211_setmode(ic, ic->ic_curmode);
148 if (ic->ic_lintval == 0)
149 ic->ic_lintval = 100; /* default sleep */
150 ic->ic_bmisstimeout = 7*ic->ic_lintval; /* default 7 beacons */
152 ieee80211_node_attach(ifp);
153 ieee80211_proto_attach(ifp);
157 ieee80211_ifdetach(struct ifnet *ifp)
159 struct ieee80211com *ic = (void *)ifp;
161 ieee80211_proto_detach(ifp);
162 ieee80211_crypto_detach(ifp);
163 ieee80211_node_detach(ifp);
164 ifmedia_removeall(&ic->ic_media);
169 * Convert MHz frequency to IEEE channel number.
172 ieee80211_mhz2ieee(u_int freq, u_int flags)
174 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
178 return (freq - 2407) / 5;
180 return 15 + ((freq - 2512) / 20);
181 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
182 return (freq - 5000) / 5;
183 } else { /* either, guess */
187 return (freq - 2407) / 5;
189 return 15 + ((freq - 2512) / 20);
190 return (freq - 5000) / 5;
195 * Convert channel to IEEE channel number.
198 ieee80211_chan2ieee(struct ieee80211com *ic, struct ieee80211_channel *c)
200 if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX])
201 return c - ic->ic_channels;
202 else if (c == IEEE80211_CHAN_ANYC)
203 return IEEE80211_CHAN_ANY;
204 else if (c != NULL) {
205 if_printf(&ic->ic_if, "invalid channel freq %u flags %x\n",
206 c->ic_freq, c->ic_flags);
209 if_printf(&ic->ic_if, "invalid channel (NULL)\n");
215 * Convert IEEE channel number to MHz frequency.
218 ieee80211_ieee2mhz(u_int chan, u_int flags)
220 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
224 return 2407 + chan*5;
226 return 2512 + ((chan-15)*20);
227 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
228 return 5000 + (chan*5);
229 } else { /* either, guess */
232 if (chan < 14) /* 0-13 */
233 return 2407 + chan*5;
234 if (chan < 27) /* 15-26 */
235 return 2512 + ((chan-15)*20);
236 return 5000 + (chan*5);
241 * Setup the media data structures according to the channel and
242 * rate tables. This must be called by the driver after
243 * ieee80211_attach and before most anything else.
246 ieee80211_media_init(struct ifnet *ifp,
247 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
249 #define ADD(_ic, _s, _o) \
250 ifmedia_add(&(_ic)->ic_media, \
251 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
252 struct ieee80211com *ic = (void *)ifp;
253 struct ifmediareq imr;
254 int i, j, mode, rate, maxrate, mword, mopt, r;
255 struct ieee80211_rateset *rs;
256 struct ieee80211_rateset allrates;
259 * Do late attach work that must wait for any subclass
260 * (i.e. driver) work such as overriding methods.
262 ieee80211_node_lateattach(ifp);
265 * Fill in media characteristics.
267 ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
269 memset(&allrates, 0, sizeof(allrates));
270 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) {
271 static const u_int mopts[] = {
277 IFM_IEEE80211_11A | IFM_IEEE80211_TURBO,
279 if ((ic->ic_modecaps & (1<<mode)) == 0)
282 ADD(ic, IFM_AUTO, mopt); /* e.g. 11a auto */
283 if (ic->ic_caps & IEEE80211_C_IBSS)
284 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC);
285 if (ic->ic_caps & IEEE80211_C_HOSTAP)
286 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP);
287 if (ic->ic_caps & IEEE80211_C_AHDEMO)
288 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
289 if (ic->ic_caps & IEEE80211_C_MONITOR)
290 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR);
291 if (mode == IEEE80211_MODE_AUTO)
293 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
294 rs = &ic->ic_sup_rates[mode];
295 for (i = 0; i < rs->rs_nrates; i++) {
296 rate = rs->rs_rates[i];
297 mword = ieee80211_rate2media(ic, rate, mode);
300 printf("%s%d%sMbps", (i != 0 ? " " : ""),
301 (rate & IEEE80211_RATE_VAL) / 2,
302 ((rate & 0x1) != 0 ? ".5" : ""));
303 ADD(ic, mword, mopt);
304 if (ic->ic_caps & IEEE80211_C_IBSS)
305 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
306 if (ic->ic_caps & IEEE80211_C_HOSTAP)
307 ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
308 if (ic->ic_caps & IEEE80211_C_AHDEMO)
309 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
310 if (ic->ic_caps & IEEE80211_C_MONITOR)
311 ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
313 * Add rate to the collection of all rates.
315 r = rate & IEEE80211_RATE_VAL;
316 for (j = 0; j < allrates.rs_nrates; j++)
317 if (allrates.rs_rates[j] == r)
319 if (j == allrates.rs_nrates) {
320 /* unique, add to the set */
321 allrates.rs_rates[j] = r;
322 allrates.rs_nrates++;
324 rate = (rate & IEEE80211_RATE_VAL) / 2;
330 for (i = 0; i < allrates.rs_nrates; i++) {
331 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
332 IEEE80211_MODE_AUTO);
335 mword = IFM_SUBTYPE(mword); /* remove media options */
337 if (ic->ic_caps & IEEE80211_C_IBSS)
338 ADD(ic, mword, IFM_IEEE80211_ADHOC);
339 if (ic->ic_caps & IEEE80211_C_HOSTAP)
340 ADD(ic, mword, IFM_IEEE80211_HOSTAP);
341 if (ic->ic_caps & IEEE80211_C_AHDEMO)
342 ADD(ic, mword, IFM_IEEE80211_ADHOC | IFM_FLAG0);
343 if (ic->ic_caps & IEEE80211_C_MONITOR)
344 ADD(ic, mword, IFM_IEEE80211_MONITOR);
346 ieee80211_media_status(ifp, &imr);
347 ifmedia_set(&ic->ic_media, imr.ifm_active);
350 ifp->if_baudrate = IF_Mbps(maxrate);
355 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
357 #define IEEERATE(_ic,_m,_i) \
358 ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
359 int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
360 for (i = 0; i < nrates; i++)
361 if (IEEERATE(ic, mode, i) == rate)
368 * Handle a media change request.
371 ieee80211_media_change(struct ifnet *ifp)
373 struct ieee80211com *ic = (void *)ifp;
374 struct ifmedia_entry *ime;
375 enum ieee80211_opmode newopmode;
376 enum ieee80211_phymode newphymode;
377 int i, j, newrate, error = 0;
379 ime = ic->ic_media.ifm_cur;
381 * First, identify the phy mode.
383 switch (IFM_MODE(ime->ifm_media)) {
384 case IFM_IEEE80211_11A:
385 newphymode = IEEE80211_MODE_11A;
387 case IFM_IEEE80211_11B:
388 newphymode = IEEE80211_MODE_11B;
390 case IFM_IEEE80211_11G:
391 newphymode = IEEE80211_MODE_11G;
393 case IFM_IEEE80211_FH:
394 newphymode = IEEE80211_MODE_FH;
397 newphymode = IEEE80211_MODE_AUTO;
403 * Turbo mode is an ``option''. Eventually it
404 * needs to be applied to 11g too.
406 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
407 if (newphymode != IEEE80211_MODE_11A)
409 newphymode = IEEE80211_MODE_TURBO;
412 * Validate requested mode is available.
414 if ((ic->ic_modecaps & (1<<newphymode)) == 0)
418 * Next, the fixed/variable rate.
421 if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
423 * Convert media subtype to rate.
425 newrate = ieee80211_media2rate(ime->ifm_media);
429 * Check the rate table for the specified/current phy.
431 if (newphymode == IEEE80211_MODE_AUTO) {
433 * In autoselect mode search for the rate.
435 for (j = IEEE80211_MODE_11A;
436 j < IEEE80211_MODE_MAX; j++) {
437 if ((ic->ic_modecaps & (1<<j)) == 0)
439 i = findrate(ic, j, newrate);
447 i = findrate(ic, newphymode, newrate);
449 if (i == -1) /* mode/rate mismatch */
452 /* NB: defer rate setting to later */
455 * Deduce new operating mode but don't install it just yet.
457 if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) ==
458 (IFM_IEEE80211_ADHOC|IFM_FLAG0))
459 newopmode = IEEE80211_M_AHDEMO;
460 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
461 newopmode = IEEE80211_M_HOSTAP;
462 else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
463 newopmode = IEEE80211_M_IBSS;
464 else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
465 newopmode = IEEE80211_M_MONITOR;
467 newopmode = IEEE80211_M_STA;
470 * Autoselect doesn't make sense when operating as an AP.
471 * If no phy mode has been selected, pick one and lock it
472 * down so rate tables can be used in forming beacon frames
475 if (newopmode == IEEE80211_M_HOSTAP &&
476 newphymode == IEEE80211_MODE_AUTO) {
477 for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++)
478 if (ic->ic_modecaps & (1<<j)) {
485 * Handle phy mode change.
487 if (ic->ic_curmode != newphymode) { /* change phy mode */
488 error = ieee80211_setmode(ic, newphymode);
495 * Committed to changes, install the rate setting.
497 if (ic->ic_fixed_rate != i) {
498 ic->ic_fixed_rate = i; /* set fixed tx rate */
503 * Handle operating mode change.
505 if (ic->ic_opmode != newopmode) {
506 ic->ic_opmode = newopmode;
508 case IEEE80211_M_AHDEMO:
509 case IEEE80211_M_HOSTAP:
510 case IEEE80211_M_STA:
511 case IEEE80211_M_MONITOR:
512 ic->ic_flags &= ~IEEE80211_F_IBSSON;
514 case IEEE80211_M_IBSS:
515 ic->ic_flags |= IEEE80211_F_IBSSON;
517 if (ic->ic_curmode == IEEE80211_MODE_11G)
518 ieee80211_set11gbasicrates(
519 &ic->ic_suprates[newphymode],
528 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
534 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
536 struct ieee80211com *ic = (void *)ifp;
537 struct ieee80211_node *ni = NULL;
538 int old_status = imr->ifm_status;
540 imr->ifm_status = IFM_AVALID;
541 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;
547 ifp->if_link_state = LINK_STATE_DOWN;
549 imr->ifm_active |= IFM_AUTO;
550 switch (ic->ic_opmode) {
551 case IEEE80211_M_STA:
553 /* calculate rate subtype */
554 imr->ifm_active |= ieee80211_rate2media(ic,
555 ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode);
557 case IEEE80211_M_IBSS:
558 imr->ifm_active |= IFM_IEEE80211_ADHOC;
560 case IEEE80211_M_AHDEMO:
561 /* should not come here */
563 case IEEE80211_M_HOSTAP:
564 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
566 case IEEE80211_M_MONITOR:
567 imr->ifm_active |= IFM_IEEE80211_MONITOR;
570 switch (ic->ic_curmode) {
571 case IEEE80211_MODE_11A:
572 imr->ifm_active |= IFM_IEEE80211_11A;
574 case IEEE80211_MODE_11B:
575 imr->ifm_active |= IFM_IEEE80211_11B;
577 case IEEE80211_MODE_11G:
578 imr->ifm_active |= IFM_IEEE80211_11G;
580 case IEEE80211_MODE_FH:
581 imr->ifm_active |= IFM_IEEE80211_FH;
583 case IEEE80211_MODE_TURBO:
584 imr->ifm_active |= IFM_IEEE80211_11A
585 | IFM_IEEE80211_TURBO;
589 /* Notify that the link state has changed. */
590 if (imr->ifm_status != old_status)
595 ieee80211_watchdog(struct ifnet *ifp)
597 struct ieee80211com *ic = (void *)ifp;
599 if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0)
600 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
601 if (ic->ic_inact_timer && --ic->ic_inact_timer == 0)
602 ieee80211_timeout_nodes(ic);
604 if (ic->ic_mgt_timer != 0 || ic->ic_inact_timer != 0)
609 * Mark the basic rates for the 11g rate table based on the
610 * operating mode. For real 11g we mark all the 11b rates
611 * and 6, 12, and 24 OFDM. For 11b compatibility we mark only
612 * 11b rates. There's also a pseudo 11a-mode used to mark only
613 * the basic OFDM rates.
616 ieee80211_set11gbasicrates(struct ieee80211_rateset *rs, enum ieee80211_phymode mode)
618 static const struct ieee80211_rateset basic[] = {
619 { 3, { 12, 24, 48 } }, /* IEEE80211_MODE_11A */
620 { 4, { 2, 4, 11, 22 } }, /* IEEE80211_MODE_11B */
621 { 7, { 2, 4, 11, 22, 12, 24, 48 } },/* IEEE80211_MODE_11G */
622 { 0 }, /* IEEE80211_MODE_FH */
623 { 0 }, /* IEEE80211_MODE_TURBO */
627 for (i = 0; i < rs->rs_nrates; i++) {
628 rs->rs_rates[i] &= IEEE80211_RATE_VAL;
629 for (j = 0; j < basic[mode].rs_nrates; j++)
630 if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
631 rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
638 * Set the current phy mode and recalculate the active channel
639 * set based on the available channels for this mode. Also
640 * select a new default/current channel if the current one is
641 * inappropriate for this mode.
644 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
646 #define N(a) (sizeof(a) / sizeof(a[0]))
647 static const u_int chanflags[] = {
648 0, /* IEEE80211_MODE_AUTO */
649 IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */
650 IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */
651 IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */
652 IEEE80211_CHAN_FHSS, /* IEEE80211_MODE_FH */
653 IEEE80211_CHAN_T, /* IEEE80211_MODE_TURBO */
655 struct ieee80211_channel *c;
659 /* validate new mode */
660 if ((ic->ic_modecaps & (1<<mode)) == 0) {
661 IEEE80211_DPRINTF(("%s: mode %u not supported (caps 0x%x)\n",
662 __func__, mode, ic->ic_modecaps));
667 * Verify at least one channel is present in the available
668 * channel list before committing to the new mode.
670 KASSERT(mode < N(chanflags), ("Unexpected mode %u", mode));
671 modeflags = chanflags[mode];
672 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
673 c = &ic->ic_channels[i];
674 if (mode == IEEE80211_MODE_AUTO) {
675 /* ignore turbo channels for autoselect */
676 if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
679 if ((c->ic_flags & modeflags) == modeflags)
683 if (i > IEEE80211_CHAN_MAX) {
684 IEEE80211_DPRINTF(("%s: no channels found for mode %u\n",
690 * Calculate the active channel set.
692 memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
693 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
694 c = &ic->ic_channels[i];
695 if (mode == IEEE80211_MODE_AUTO) {
696 /* take anything but pure turbo channels */
697 if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
698 setbit(ic->ic_chan_active, i);
700 if ((c->ic_flags & modeflags) == modeflags)
701 setbit(ic->ic_chan_active, i);
705 * If no current/default channel is setup or the current
706 * channel is wrong for the mode then pick the first
707 * available channel from the active list. This is likely
710 if (ic->ic_ibss_chan == NULL ||
711 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
712 for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
713 if (isset(ic->ic_chan_active, i)) {
714 ic->ic_ibss_chan = &ic->ic_channels[i];
720 * Set/reset state flags that influence beacon contents, etc.
722 * XXX what if we have stations already associated???
723 * XXX probably not right for autoselect?
725 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE)
726 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
727 if (mode == IEEE80211_MODE_11G) {
728 if (ic->ic_caps & IEEE80211_C_SHSLOT)
729 ic->ic_flags |= IEEE80211_F_SHSLOT;
730 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode],
733 ic->ic_flags &= ~IEEE80211_F_SHSLOT;
736 ic->ic_curmode = mode;
742 * Return the phy mode for with the specified channel so the
743 * caller can select a rate set. This is problematic and the
744 * work here assumes how things work elsewhere in this code.
746 enum ieee80211_phymode
747 ieee80211_chan2mode(struct ieee80211com *ic, struct ieee80211_channel *chan)
750 * NB: this assumes the channel would not be supplied to us
751 * unless it was already compatible with the current mode.
753 if (ic->ic_curmode != IEEE80211_MODE_AUTO)
754 return ic->ic_curmode;
756 * In autoselect mode; deduce a mode based on the channel
757 * characteristics. We assume that turbo-only channels
758 * are not considered when the channel set is constructed.
760 if (IEEE80211_IS_CHAN_5GHZ(chan))
761 return IEEE80211_MODE_11A;
762 else if (IEEE80211_IS_CHAN_FHSS(chan))
763 return IEEE80211_MODE_FH;
764 else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN))
765 return IEEE80211_MODE_11G;
767 return IEEE80211_MODE_11B;
771 * convert IEEE80211 rate value to ifmedia subtype.
772 * ieee80211 rate is in unit of 0.5Mbps.
775 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
777 #define N(a) (sizeof(a) / sizeof(a[0]))
778 static const struct {
779 u_int m; /* rate + mode */
780 u_int r; /* if_media rate */
782 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
783 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
784 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
785 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
786 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
787 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
788 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
789 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
790 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
791 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
792 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
793 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
794 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
795 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
796 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
797 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
798 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
799 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
800 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
801 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
802 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
803 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
804 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
805 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
806 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
807 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
808 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
809 /* NB: OFDM72 doesn't realy exist so we don't handle it */
813 mask = rate & IEEE80211_RATE_VAL;
815 case IEEE80211_MODE_11A:
816 case IEEE80211_MODE_TURBO:
817 mask |= IFM_IEEE80211_11A;
819 case IEEE80211_MODE_11B:
820 mask |= IFM_IEEE80211_11B;
822 case IEEE80211_MODE_FH:
823 mask |= IFM_IEEE80211_FH;
825 case IEEE80211_MODE_AUTO:
826 /* NB: ic may be NULL for some drivers */
827 if (ic && ic->ic_phytype == IEEE80211_T_FH) {
828 mask |= IFM_IEEE80211_FH;
831 /* NB: hack, 11g matches both 11b+11a rates */
833 case IEEE80211_MODE_11G:
834 mask |= IFM_IEEE80211_11G;
837 for (i = 0; i < N(rates); i++)
838 if (rates[i].m == mask)
845 ieee80211_media2rate(int mword)
847 #define N(a) (sizeof(a) / sizeof(a[0]))
848 static const int ieeerates[] = {
852 2, /* IFM_IEEE80211_FH1 */
853 4, /* IFM_IEEE80211_FH2 */
854 2, /* IFM_IEEE80211_DS1 */
855 4, /* IFM_IEEE80211_DS2 */
856 11, /* IFM_IEEE80211_DS5 */
857 22, /* IFM_IEEE80211_DS11 */
858 44, /* IFM_IEEE80211_DS22 */
859 12, /* IFM_IEEE80211_OFDM6 */
860 18, /* IFM_IEEE80211_OFDM9 */
861 24, /* IFM_IEEE80211_OFDM12 */
862 36, /* IFM_IEEE80211_OFDM18 */
863 48, /* IFM_IEEE80211_OFDM24 */
864 72, /* IFM_IEEE80211_OFDM36 */
865 96, /* IFM_IEEE80211_OFDM48 */
866 108, /* IFM_IEEE80211_OFDM54 */
867 144, /* IFM_IEEE80211_OFDM72 */
869 return IFM_SUBTYPE(mword) < N(ieeerates) ?
870 ieeerates[IFM_SUBTYPE(mword)] : 0;
877 * NB: the module name is "wlan" for compatibility with NetBSD.
881 ieee80211_modevent(module_t mod, int type, void *unused)
886 printf("wlan: <802.11 Link Layer>\n");
895 static moduledata_t ieee80211_mod = {
900 DECLARE_MODULE(wlan, ieee80211_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
901 MODULE_VERSION(wlan, 1);
902 MODULE_DEPEND(wlan, crypto, 1, 1, 1);