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.2 2004/07/27 12:21:54 hmp 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);
101 #ifdef IEEE80211_RAWBPF
102 bpfattach2(ifp, DLT_IEEE802_11,
103 sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf);
105 ieee80211_crypto_attach(ifp);
108 * Fill in 802.11 available channel set, mark
109 * all available channels as active, and pick
110 * a default channel if not already specified.
112 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
113 ic->ic_modecaps |= 1<<IEEE80211_MODE_AUTO;
114 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
115 c = &ic->ic_channels[i];
118 * Verify driver passed us valid data.
120 if (i != ieee80211_chan2ieee(ic, c)) {
121 if_printf(ifp, "bad channel ignored; "
122 "freq %u flags %x number %u\n",
123 c->ic_freq, c->ic_flags, i);
124 c->ic_flags = 0; /* NB: remove */
127 setbit(ic->ic_chan_avail, i);
129 * Identify mode capabilities.
131 if (IEEE80211_IS_CHAN_A(c))
132 ic->ic_modecaps |= 1<<IEEE80211_MODE_11A;
133 if (IEEE80211_IS_CHAN_B(c))
134 ic->ic_modecaps |= 1<<IEEE80211_MODE_11B;
135 if (IEEE80211_IS_CHAN_PUREG(c))
136 ic->ic_modecaps |= 1<<IEEE80211_MODE_11G;
137 if (IEEE80211_IS_CHAN_FHSS(c))
138 ic->ic_modecaps |= 1<<IEEE80211_MODE_FH;
139 if (IEEE80211_IS_CHAN_T(c))
140 ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO;
143 /* validate ic->ic_curmode */
144 if ((ic->ic_modecaps & (1<<ic->ic_curmode)) == 0)
145 ic->ic_curmode = IEEE80211_MODE_AUTO;
146 ic->ic_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
148 (void) ieee80211_setmode(ic, ic->ic_curmode);
150 if (ic->ic_lintval == 0)
151 ic->ic_lintval = 100; /* default sleep */
152 ic->ic_bmisstimeout = 7*ic->ic_lintval; /* default 7 beacons */
154 ieee80211_node_attach(ifp);
155 ieee80211_proto_attach(ifp);
159 ieee80211_ifdetach(struct ifnet *ifp)
161 struct ieee80211com *ic = (void *)ifp;
163 ieee80211_proto_detach(ifp);
164 ieee80211_crypto_detach(ifp);
165 ieee80211_node_detach(ifp);
166 ifmedia_removeall(&ic->ic_media);
171 * Convert MHz frequency to IEEE channel number.
174 ieee80211_mhz2ieee(u_int freq, u_int flags)
176 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
180 return (freq - 2407) / 5;
182 return 15 + ((freq - 2512) / 20);
183 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
184 return (freq - 5000) / 5;
185 } else { /* either, guess */
189 return (freq - 2407) / 5;
191 return 15 + ((freq - 2512) / 20);
192 return (freq - 5000) / 5;
197 * Convert channel to IEEE channel number.
200 ieee80211_chan2ieee(struct ieee80211com *ic, struct ieee80211_channel *c)
202 if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX])
203 return c - ic->ic_channels;
204 else if (c == IEEE80211_CHAN_ANYC)
205 return IEEE80211_CHAN_ANY;
206 else if (c != NULL) {
207 if_printf(&ic->ic_if, "invalid channel freq %u flags %x\n",
208 c->ic_freq, c->ic_flags);
211 if_printf(&ic->ic_if, "invalid channel (NULL)\n");
217 * Convert IEEE channel number to MHz frequency.
220 ieee80211_ieee2mhz(u_int chan, u_int flags)
222 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
226 return 2407 + chan*5;
228 return 2512 + ((chan-15)*20);
229 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
230 return 5000 + (chan*5);
231 } else { /* either, guess */
234 if (chan < 14) /* 0-13 */
235 return 2407 + chan*5;
236 if (chan < 27) /* 15-26 */
237 return 2512 + ((chan-15)*20);
238 return 5000 + (chan*5);
243 * Setup the media data structures according to the channel and
244 * rate tables. This must be called by the driver after
245 * ieee80211_attach and before most anything else.
248 ieee80211_media_init(struct ifnet *ifp,
249 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
251 #define ADD(_ic, _s, _o) \
252 ifmedia_add(&(_ic)->ic_media, \
253 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
254 struct ieee80211com *ic = (void *)ifp;
255 struct ifmediareq imr;
256 int i, j, mode, rate, maxrate, mword, mopt, r;
257 struct ieee80211_rateset *rs;
258 struct ieee80211_rateset allrates;
261 * Do late attach work that must wait for any subclass
262 * (i.e. driver) work such as overriding methods.
264 ieee80211_node_lateattach(ifp);
267 * Fill in media characteristics.
269 ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
271 memset(&allrates, 0, sizeof(allrates));
272 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) {
273 static const u_int mopts[] = {
279 IFM_IEEE80211_11A | IFM_IEEE80211_TURBO,
281 if ((ic->ic_modecaps & (1<<mode)) == 0)
284 ADD(ic, IFM_AUTO, mopt); /* e.g. 11a auto */
285 if (ic->ic_caps & IEEE80211_C_IBSS)
286 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC);
287 if (ic->ic_caps & IEEE80211_C_HOSTAP)
288 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP);
289 if (ic->ic_caps & IEEE80211_C_AHDEMO)
290 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
291 if (ic->ic_caps & IEEE80211_C_MONITOR)
292 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR);
293 if (mode == IEEE80211_MODE_AUTO)
295 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
296 rs = &ic->ic_sup_rates[mode];
297 for (i = 0; i < rs->rs_nrates; i++) {
298 rate = rs->rs_rates[i];
299 mword = ieee80211_rate2media(ic, rate, mode);
302 printf("%s%d%sMbps", (i != 0 ? " " : ""),
303 (rate & IEEE80211_RATE_VAL) / 2,
304 ((rate & 0x1) != 0 ? ".5" : ""));
305 ADD(ic, mword, mopt);
306 if (ic->ic_caps & IEEE80211_C_IBSS)
307 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
308 if (ic->ic_caps & IEEE80211_C_HOSTAP)
309 ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
310 if (ic->ic_caps & IEEE80211_C_AHDEMO)
311 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
312 if (ic->ic_caps & IEEE80211_C_MONITOR)
313 ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
315 * Add rate to the collection of all rates.
317 r = rate & IEEE80211_RATE_VAL;
318 for (j = 0; j < allrates.rs_nrates; j++)
319 if (allrates.rs_rates[j] == r)
321 if (j == allrates.rs_nrates) {
322 /* unique, add to the set */
323 allrates.rs_rates[j] = r;
324 allrates.rs_nrates++;
326 rate = (rate & IEEE80211_RATE_VAL) / 2;
332 for (i = 0; i < allrates.rs_nrates; i++) {
333 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
334 IEEE80211_MODE_AUTO);
337 mword = IFM_SUBTYPE(mword); /* remove media options */
339 if (ic->ic_caps & IEEE80211_C_IBSS)
340 ADD(ic, mword, IFM_IEEE80211_ADHOC);
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 | IFM_FLAG0);
345 if (ic->ic_caps & IEEE80211_C_MONITOR)
346 ADD(ic, mword, IFM_IEEE80211_MONITOR);
348 ieee80211_media_status(ifp, &imr);
349 ifmedia_set(&ic->ic_media, imr.ifm_active);
352 ifp->if_baudrate = IF_Mbps(maxrate);
357 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
359 #define IEEERATE(_ic,_m,_i) \
360 ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
361 int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
362 for (i = 0; i < nrates; i++)
363 if (IEEERATE(ic, mode, i) == rate)
370 * Handle a media change request.
373 ieee80211_media_change(struct ifnet *ifp)
375 struct ieee80211com *ic = (void *)ifp;
376 struct ifmedia_entry *ime;
377 enum ieee80211_opmode newopmode;
378 enum ieee80211_phymode newphymode;
379 int i, j, newrate, error = 0;
381 ime = ic->ic_media.ifm_cur;
383 * First, identify the phy mode.
385 switch (IFM_MODE(ime->ifm_media)) {
386 case IFM_IEEE80211_11A:
387 newphymode = IEEE80211_MODE_11A;
389 case IFM_IEEE80211_11B:
390 newphymode = IEEE80211_MODE_11B;
392 case IFM_IEEE80211_11G:
393 newphymode = IEEE80211_MODE_11G;
395 case IFM_IEEE80211_FH:
396 newphymode = IEEE80211_MODE_FH;
399 newphymode = IEEE80211_MODE_AUTO;
405 * Turbo mode is an ``option''. Eventually it
406 * needs to be applied to 11g too.
408 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
409 if (newphymode != IEEE80211_MODE_11A)
411 newphymode = IEEE80211_MODE_TURBO;
414 * Validate requested mode is available.
416 if ((ic->ic_modecaps & (1<<newphymode)) == 0)
420 * Next, the fixed/variable rate.
423 if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
425 * Convert media subtype to rate.
427 newrate = ieee80211_media2rate(ime->ifm_media);
431 * Check the rate table for the specified/current phy.
433 if (newphymode == IEEE80211_MODE_AUTO) {
435 * In autoselect mode search for the rate.
437 for (j = IEEE80211_MODE_11A;
438 j < IEEE80211_MODE_MAX; j++) {
439 if ((ic->ic_modecaps & (1<<j)) == 0)
441 i = findrate(ic, j, newrate);
449 i = findrate(ic, newphymode, newrate);
451 if (i == -1) /* mode/rate mismatch */
454 /* NB: defer rate setting to later */
457 * Deduce new operating mode but don't install it just yet.
459 if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) ==
460 (IFM_IEEE80211_ADHOC|IFM_FLAG0))
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_ADHOC)
465 newopmode = IEEE80211_M_IBSS;
466 else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
467 newopmode = IEEE80211_M_MONITOR;
469 newopmode = IEEE80211_M_STA;
472 * Autoselect doesn't make sense when operating as an AP.
473 * If no phy mode has been selected, pick one and lock it
474 * down so rate tables can be used in forming beacon frames
477 if (newopmode == IEEE80211_M_HOSTAP &&
478 newphymode == IEEE80211_MODE_AUTO) {
479 for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++)
480 if (ic->ic_modecaps & (1<<j)) {
487 * Handle phy mode change.
489 if (ic->ic_curmode != newphymode) { /* change phy mode */
490 error = ieee80211_setmode(ic, newphymode);
497 * Committed to changes, install the rate setting.
499 if (ic->ic_fixed_rate != i) {
500 ic->ic_fixed_rate = i; /* set fixed tx rate */
505 * Handle operating mode change.
507 if (ic->ic_opmode != newopmode) {
508 ic->ic_opmode = newopmode;
510 case IEEE80211_M_AHDEMO:
511 case IEEE80211_M_HOSTAP:
512 case IEEE80211_M_STA:
513 case IEEE80211_M_MONITOR:
514 ic->ic_flags &= ~IEEE80211_F_IBSSON;
516 case IEEE80211_M_IBSS:
517 ic->ic_flags |= IEEE80211_F_IBSSON;
519 if (ic->ic_curmode == IEEE80211_MODE_11G)
520 ieee80211_set11gbasicrates(
521 &ic->ic_suprates[newphymode],
530 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
536 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
538 struct ieee80211com *ic = (void *)ifp;
539 struct ieee80211_node *ni = NULL;
540 int old_status = imr->ifm_status;
542 imr->ifm_status = IFM_AVALID;
543 imr->ifm_active = IFM_IEEE80211;
545 if (ic->ic_state == IEEE80211_S_RUN) {
546 imr->ifm_status |= IFM_ACTIVE;
547 ifp->if_link_state = LINK_STATE_UP;
549 ifp->if_link_state = LINK_STATE_DOWN;
551 imr->ifm_active |= IFM_AUTO;
552 switch (ic->ic_opmode) {
553 case IEEE80211_M_STA:
555 /* calculate rate subtype */
556 imr->ifm_active |= ieee80211_rate2media(ic,
557 ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode);
559 case IEEE80211_M_IBSS:
560 imr->ifm_active |= IFM_IEEE80211_ADHOC;
562 case IEEE80211_M_AHDEMO:
563 /* should not come here */
565 case IEEE80211_M_HOSTAP:
566 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
568 case IEEE80211_M_MONITOR:
569 imr->ifm_active |= IFM_IEEE80211_MONITOR;
572 switch (ic->ic_curmode) {
573 case IEEE80211_MODE_11A:
574 imr->ifm_active |= IFM_IEEE80211_11A;
576 case IEEE80211_MODE_11B:
577 imr->ifm_active |= IFM_IEEE80211_11B;
579 case IEEE80211_MODE_11G:
580 imr->ifm_active |= IFM_IEEE80211_11G;
582 case IEEE80211_MODE_FH:
583 imr->ifm_active |= IFM_IEEE80211_FH;
585 case IEEE80211_MODE_TURBO:
586 imr->ifm_active |= IFM_IEEE80211_11A
587 | IFM_IEEE80211_TURBO;
591 /* Notify that the link state has changed. */
592 if (imr->ifm_status != old_status)
597 ieee80211_watchdog(struct ifnet *ifp)
599 struct ieee80211com *ic = (void *)ifp;
601 if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0)
602 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
603 if (ic->ic_inact_timer && --ic->ic_inact_timer == 0)
604 ieee80211_timeout_nodes(ic);
606 if (ic->ic_mgt_timer != 0 || ic->ic_inact_timer != 0)
611 * Mark the basic rates for the 11g rate table based on the
612 * operating mode. For real 11g we mark all the 11b rates
613 * and 6, 12, and 24 OFDM. For 11b compatibility we mark only
614 * 11b rates. There's also a pseudo 11a-mode used to mark only
615 * the basic OFDM rates.
618 ieee80211_set11gbasicrates(struct ieee80211_rateset *rs, enum ieee80211_phymode mode)
620 static const struct ieee80211_rateset basic[] = {
621 { 3, { 12, 24, 48 } }, /* IEEE80211_MODE_11A */
622 { 4, { 2, 4, 11, 22 } }, /* IEEE80211_MODE_11B */
623 { 7, { 2, 4, 11, 22, 12, 24, 48 } },/* IEEE80211_MODE_11G */
624 { 0 }, /* IEEE80211_MODE_FH */
625 { 0 }, /* IEEE80211_MODE_TURBO */
629 for (i = 0; i < rs->rs_nrates; i++) {
630 rs->rs_rates[i] &= IEEE80211_RATE_VAL;
631 for (j = 0; j < basic[mode].rs_nrates; j++)
632 if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
633 rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
640 * Set the current phy mode and recalculate the active channel
641 * set based on the available channels for this mode. Also
642 * select a new default/current channel if the current one is
643 * inappropriate for this mode.
646 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
648 #define N(a) (sizeof(a) / sizeof(a[0]))
649 static const u_int chanflags[] = {
650 0, /* IEEE80211_MODE_AUTO */
651 IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */
652 IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */
653 IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */
654 IEEE80211_CHAN_FHSS, /* IEEE80211_MODE_FH */
655 IEEE80211_CHAN_T, /* IEEE80211_MODE_TURBO */
657 struct ieee80211_channel *c;
661 /* validate new mode */
662 if ((ic->ic_modecaps & (1<<mode)) == 0) {
663 IEEE80211_DPRINTF(("%s: mode %u not supported (caps 0x%x)\n",
664 __func__, mode, ic->ic_modecaps));
669 * Verify at least one channel is present in the available
670 * channel list before committing to the new mode.
672 KASSERT(mode < N(chanflags), ("Unexpected mode %u", mode));
673 modeflags = chanflags[mode];
674 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
675 c = &ic->ic_channels[i];
676 if (mode == IEEE80211_MODE_AUTO) {
677 /* ignore turbo channels for autoselect */
678 if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
681 if ((c->ic_flags & modeflags) == modeflags)
685 if (i > IEEE80211_CHAN_MAX) {
686 IEEE80211_DPRINTF(("%s: no channels found for mode %u\n",
692 * Calculate the active channel set.
694 memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
695 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
696 c = &ic->ic_channels[i];
697 if (mode == IEEE80211_MODE_AUTO) {
698 /* take anything but pure turbo channels */
699 if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
700 setbit(ic->ic_chan_active, i);
702 if ((c->ic_flags & modeflags) == modeflags)
703 setbit(ic->ic_chan_active, i);
707 * If no current/default channel is setup or the current
708 * channel is wrong for the mode then pick the first
709 * available channel from the active list. This is likely
712 if (ic->ic_ibss_chan == NULL ||
713 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
714 for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
715 if (isset(ic->ic_chan_active, i)) {
716 ic->ic_ibss_chan = &ic->ic_channels[i];
722 * Set/reset state flags that influence beacon contents, etc.
724 * XXX what if we have stations already associated???
725 * XXX probably not right for autoselect?
727 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE)
728 ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
729 if (mode == IEEE80211_MODE_11G) {
730 if (ic->ic_caps & IEEE80211_C_SHSLOT)
731 ic->ic_flags |= IEEE80211_F_SHSLOT;
732 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode],
735 ic->ic_flags &= ~IEEE80211_F_SHSLOT;
738 ic->ic_curmode = mode;
744 * Return the phy mode for with the specified channel so the
745 * caller can select a rate set. This is problematic and the
746 * work here assumes how things work elsewhere in this code.
748 enum ieee80211_phymode
749 ieee80211_chan2mode(struct ieee80211com *ic, struct ieee80211_channel *chan)
752 * NB: this assumes the channel would not be supplied to us
753 * unless it was already compatible with the current mode.
755 if (ic->ic_curmode != IEEE80211_MODE_AUTO)
756 return ic->ic_curmode;
758 * In autoselect mode; deduce a mode based on the channel
759 * characteristics. We assume that turbo-only channels
760 * are not considered when the channel set is constructed.
762 if (IEEE80211_IS_CHAN_5GHZ(chan))
763 return IEEE80211_MODE_11A;
764 else if (IEEE80211_IS_CHAN_FHSS(chan))
765 return IEEE80211_MODE_FH;
766 else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN))
767 return IEEE80211_MODE_11G;
769 return IEEE80211_MODE_11B;
773 * convert IEEE80211 rate value to ifmedia subtype.
774 * ieee80211 rate is in unit of 0.5Mbps.
777 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
779 #define N(a) (sizeof(a) / sizeof(a[0]))
780 static const struct {
781 u_int m; /* rate + mode */
782 u_int r; /* if_media rate */
784 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
785 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
786 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
787 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
788 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
789 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
790 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
791 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
792 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
793 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
794 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
795 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
796 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
797 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
798 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
799 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
800 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
801 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
802 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
803 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
804 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
805 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
806 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
807 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
808 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
809 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
810 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
811 /* NB: OFDM72 doesn't realy exist so we don't handle it */
815 mask = rate & IEEE80211_RATE_VAL;
817 case IEEE80211_MODE_11A:
818 case IEEE80211_MODE_TURBO:
819 mask |= IFM_IEEE80211_11A;
821 case IEEE80211_MODE_11B:
822 mask |= IFM_IEEE80211_11B;
824 case IEEE80211_MODE_FH:
825 mask |= IFM_IEEE80211_FH;
827 case IEEE80211_MODE_AUTO:
828 /* NB: ic may be NULL for some drivers */
829 if (ic && ic->ic_phytype == IEEE80211_T_FH) {
830 mask |= IFM_IEEE80211_FH;
833 /* NB: hack, 11g matches both 11b+11a rates */
835 case IEEE80211_MODE_11G:
836 mask |= IFM_IEEE80211_11G;
839 for (i = 0; i < N(rates); i++)
840 if (rates[i].m == mask)
847 ieee80211_media2rate(int mword)
849 #define N(a) (sizeof(a) / sizeof(a[0]))
850 static const int ieeerates[] = {
854 2, /* IFM_IEEE80211_FH1 */
855 4, /* IFM_IEEE80211_FH2 */
856 2, /* IFM_IEEE80211_DS1 */
857 4, /* IFM_IEEE80211_DS2 */
858 11, /* IFM_IEEE80211_DS5 */
859 22, /* IFM_IEEE80211_DS11 */
860 44, /* IFM_IEEE80211_DS22 */
861 12, /* IFM_IEEE80211_OFDM6 */
862 18, /* IFM_IEEE80211_OFDM9 */
863 24, /* IFM_IEEE80211_OFDM12 */
864 36, /* IFM_IEEE80211_OFDM18 */
865 48, /* IFM_IEEE80211_OFDM24 */
866 72, /* IFM_IEEE80211_OFDM36 */
867 96, /* IFM_IEEE80211_OFDM48 */
868 108, /* IFM_IEEE80211_OFDM54 */
869 144, /* IFM_IEEE80211_OFDM72 */
871 return IFM_SUBTYPE(mword) < N(ieeerates) ?
872 ieeerates[IFM_SUBTYPE(mword)] : 0;
879 * NB: the module name is "wlan" for compatibility with NetBSD.
883 ieee80211_modevent(module_t mod, int type, void *unused)
888 printf("wlan: <802.11 Link Layer>\n");
897 static moduledata_t ieee80211_mod = {
902 DECLARE_MODULE(wlan, ieee80211_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
903 MODULE_VERSION(wlan, 1);
904 MODULE_DEPEND(wlan, crypto, 1, 1, 1);