2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2005 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.19.2.7 2006/03/11 19:25:23 sam Exp $
33 * $DragonFly: src/sys/netproto/802_11/wlan/ieee80211.c,v 1.15 2007/04/26 12:59:14 sephe Exp $
37 * IEEE 802.11 generic handler
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/serialize.h>
45 #include <sys/socket.h>
48 #include <net/if_arp.h>
49 #include <net/if_media.h>
50 #include <net/ethernet.h>
52 #include <netproto/802_11/ieee80211_var.h>
56 const char *ieee80211_phymode_name[] = {
57 "auto", /* IEEE80211_MODE_AUTO */
58 "11a", /* IEEE80211_MODE_11A */
59 "11b", /* IEEE80211_MODE_11B */
60 "11g", /* IEEE80211_MODE_11G */
61 "FH", /* IEEE80211_MODE_FH */
62 "turboA", /* IEEE80211_MODE_TURBO_A */
63 "turboG", /* IEEE80211_MODE_TURBO_G */
66 /* list of all instances */
67 SLIST_HEAD(ieee80211_list, ieee80211com);
68 static struct ieee80211_list ieee80211_list =
69 SLIST_HEAD_INITIALIZER(ieee80211_list);
70 static uint8_t ieee80211_vapmap[32]; /* enough for 256 */
73 ieee80211_add_vap(struct ieee80211com *ic)
75 #define N(a) (sizeof(a)/sizeof(a[0]))
82 for (i = 0; i < N(ieee80211_vapmap) && ieee80211_vapmap[i] == 0xff; i++)
84 if (i == N(ieee80211_vapmap))
85 panic("vap table full");
86 for (b = ieee80211_vapmap[i]; b & 1; b >>= 1)
88 setbit(ieee80211_vapmap, ic->ic_vap);
89 SLIST_INSERT_HEAD(&ieee80211_list, ic, ic_next);
96 ieee80211_remove_vap(struct ieee80211com *ic)
100 SLIST_REMOVE(&ieee80211_list, ic, ieee80211com, ic_next);
101 KASSERT(ic->ic_vap < sizeof(ieee80211_vapmap)*NBBY,
102 ("invalid vap id %d", ic->ic_vap));
103 KASSERT(isset(ieee80211_vapmap, ic->ic_vap),
104 ("vap id %d not allocated", ic->ic_vap));
105 clrbit(ieee80211_vapmap, ic->ic_vap);
111 * Default reset method for use with the ioctl support. This
112 * method is invoked after any state change in the 802.11
113 * layer that should be propagated to the hardware but not
114 * require re-initialization of the 802.11 state machine (e.g
115 * rescanning for an ap). We always return ENETRESET which
116 * should cause the driver to re-initialize the device. Drivers
117 * can override this method to implement more optimized support.
120 ieee80211_default_reset(struct ifnet *ifp)
126 ieee80211_ifattach(struct ieee80211com *ic)
129 struct ieee80211_channel *c;
132 ifp = &ic->ic_ac.ac_if;
135 ether_ifattach(ifp, ic->ic_myaddr, NULL);
136 bpfattach_dlt(ifp, DLT_IEEE802_11,
137 sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf);
139 ieee80211_crypto_attach(ic);
142 * Fill in 802.11 available channel set, mark
143 * all available channels as active, and pick
144 * a default channel if not already specified.
146 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
147 ic->ic_modecaps |= 1<<IEEE80211_MODE_AUTO;
148 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
149 c = &ic->ic_channels[i];
152 * Verify driver passed us valid data.
154 if (i != ieee80211_chan2ieee(ic, c)) {
155 if_printf(ifp, "bad channel ignored; "
156 "freq %u flags %x number %u\n",
157 c->ic_freq, c->ic_flags, i);
158 c->ic_flags = 0; /* NB: remove */
161 setbit(ic->ic_chan_avail, i);
163 * Identify mode capabilities.
165 if (IEEE80211_IS_CHAN_A(c))
166 ic->ic_modecaps |= 1<<IEEE80211_MODE_11A;
167 if (IEEE80211_IS_CHAN_B(c))
168 ic->ic_modecaps |= 1<<IEEE80211_MODE_11B;
169 if (IEEE80211_IS_CHAN_PUREG(c))
170 ic->ic_modecaps |= 1<<IEEE80211_MODE_11G;
171 if (IEEE80211_IS_CHAN_FHSS(c))
172 ic->ic_modecaps |= 1<<IEEE80211_MODE_FH;
173 if (IEEE80211_IS_CHAN_T(c))
174 ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO_A;
175 if (IEEE80211_IS_CHAN_108G(c))
176 ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO_G;
177 if (ic->ic_curchan == NULL) {
178 /* arbitrarily pick the first channel */
179 ic->ic_curchan = &ic->ic_channels[i];
183 /* validate ic->ic_curmode */
184 if ((ic->ic_modecaps & (1<<ic->ic_curmode)) == 0)
185 ic->ic_curmode = IEEE80211_MODE_AUTO;
186 ic->ic_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
189 * Enable WME by default if we're capable.
191 if (ic->ic_caps & IEEE80211_C_WME)
192 ic->ic_flags |= IEEE80211_F_WME;
194 if (ic->ic_caps & IEEE80211_C_BURST)
195 ic->ic_flags |= IEEE80211_F_BURST;
196 ieee80211_setmode(ic, ic->ic_curmode);
198 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
199 ic->ic_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
200 ic->ic_dtim_period = IEEE80211_DTIM_DEFAULT;
202 ic->ic_lintval = ic->ic_bintval;
203 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
205 ieee80211_node_attach(ic);
206 ieee80211_proto_attach(ic);
208 ieee80211_add_vap(ic);
210 ieee80211_sysctl_attach(ic); /* NB: requires ic_vap */
212 ieee80211_ratectl_attach(ic);
215 * Install a default reset method for the ioctl support.
216 * The driver is expected to fill this in before calling us.
218 if (ic->ic_reset == NULL)
219 ic->ic_reset = ieee80211_default_reset;
223 ieee80211_ifdetach(struct ieee80211com *ic)
225 struct ifnet *ifp = ic->ic_ifp;
229 * Certain rate control algorithm(e.g. onoe) may iterate node
230 * tables, which will assert serializer.
231 * In order to make the assertion work, hold serializer here.
234 lwkt_serialize_enter(ifp->if_serializer);
235 ieee80211_ratectl_detach(ic);
236 lwkt_serialize_exit(ifp->if_serializer);
238 ieee80211_remove_vap(ic);
240 ieee80211_sysctl_detach(ic);
241 ieee80211_proto_detach(ic);
242 ieee80211_crypto_detach(ic);
246 * ieee80211_node_detach() -> ieee80211_node_table_cleanup()
247 * -> ieee80211_free_allnodes_locked()
248 * will assert the serializer.
249 * In order to make the assertion work, hold serializer here.
252 lwkt_serialize_enter(ifp->if_serializer);
253 ieee80211_node_detach(ic);
254 lwkt_serialize_exit(ifp->if_serializer);
256 ifmedia_removeall(&ic->ic_media);
263 * Convert MHz frequency to IEEE channel number.
266 ieee80211_mhz2ieee(u_int freq, u_int flags)
268 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
272 return (freq - 2407) / 5;
274 return 15 + ((freq - 2512) / 20);
275 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
276 return (freq - 5000) / 5;
277 } else { /* either, guess */
281 return (freq - 2407) / 5;
283 return 15 + ((freq - 2512) / 20);
284 return (freq - 5000) / 5;
289 * Convert channel to IEEE channel number.
292 ieee80211_chan2ieee(struct ieee80211com *ic, struct ieee80211_channel *c)
294 if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX])
295 return c - ic->ic_channels;
296 else if (c == IEEE80211_CHAN_ANYC)
297 return IEEE80211_CHAN_ANY;
298 else if (c != NULL) {
299 if_printf(ic->ic_ifp, "invalid channel freq %u flags %x\n",
300 c->ic_freq, c->ic_flags);
303 if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
309 * Convert IEEE channel number to MHz frequency.
312 ieee80211_ieee2mhz(u_int chan, u_int flags)
314 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
318 return 2407 + chan*5;
320 return 2512 + ((chan-15)*20);
321 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
322 return 5000 + (chan*5);
323 } else { /* either, guess */
326 if (chan < 14) /* 0-13 */
327 return 2407 + chan*5;
328 if (chan < 27) /* 15-26 */
329 return 2512 + ((chan-15)*20);
330 return 5000 + (chan*5);
335 * Setup the media data structures according to the channel and
336 * rate tables. This must be called by the driver after
337 * ieee80211_ifattach and before most anything else.
340 ieee80211_media_init(struct ieee80211com *ic,
341 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
343 #define ADD(_ic, _s, _o) \
344 ifmedia_add(&(_ic)->ic_media, \
345 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
346 struct ifnet *ifp = ic->ic_ifp;
347 struct ifmediareq imr;
348 int i, j, mode, rate, maxrate, mword, mopt, r;
349 struct ieee80211_rateset *rs;
350 struct ieee80211_rateset allrates;
354 * ieee80211_node_lateattach() -> ieee80211_rest_bss()
355 * -> ieee80211_alloc_node() -> ieee80211_setup_node()
356 * will assert the serializer
357 * In order to make the assertion work, hold serializer here
361 lwkt_serialize_enter(ifp->if_serializer);
364 * Do late attach work that must wait for any subclass
365 * (i.e. driver) work such as overriding methods.
367 ieee80211_node_lateattach(ic);
369 lwkt_serialize_exit(ifp->if_serializer);
372 * Fill in media characteristics.
374 ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
376 memset(&allrates, 0, sizeof(allrates));
377 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) {
378 static const u_int mopts[] = {
384 IFM_IEEE80211_11A | IFM_IEEE80211_TURBO,
385 IFM_IEEE80211_11G | IFM_IEEE80211_TURBO,
387 if ((ic->ic_modecaps & (1<<mode)) == 0)
390 ADD(ic, IFM_AUTO, mopt); /* e.g. 11a auto */
391 if (ic->ic_caps & IEEE80211_C_IBSS)
392 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC);
393 if (ic->ic_caps & IEEE80211_C_HOSTAP)
394 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP);
395 if (ic->ic_caps & IEEE80211_C_AHDEMO)
396 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
397 if (ic->ic_caps & IEEE80211_C_MONITOR)
398 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR);
399 if (mode == IEEE80211_MODE_AUTO)
401 rs = &ic->ic_sup_rates[mode];
402 for (i = 0; i < rs->rs_nrates; i++) {
403 rate = rs->rs_rates[i];
404 mword = ieee80211_rate2media(ic, rate, mode);
407 ADD(ic, mword, mopt);
408 if (ic->ic_caps & IEEE80211_C_IBSS)
409 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
410 if (ic->ic_caps & IEEE80211_C_HOSTAP)
411 ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
412 if (ic->ic_caps & IEEE80211_C_AHDEMO)
413 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
414 if (ic->ic_caps & IEEE80211_C_MONITOR)
415 ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
417 * Add rate to the collection of all rates.
419 r = rate & IEEE80211_RATE_VAL;
420 for (j = 0; j < allrates.rs_nrates; j++)
421 if (allrates.rs_rates[j] == r)
423 if (j == allrates.rs_nrates) {
424 /* unique, add to the set */
425 allrates.rs_rates[j] = r;
426 allrates.rs_nrates++;
428 rate = (rate & IEEE80211_RATE_VAL) / 2;
433 for (i = 0; i < allrates.rs_nrates; i++) {
434 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
435 IEEE80211_MODE_AUTO);
438 mword = IFM_SUBTYPE(mword); /* remove media options */
440 if (ic->ic_caps & IEEE80211_C_IBSS)
441 ADD(ic, mword, IFM_IEEE80211_ADHOC);
442 if (ic->ic_caps & IEEE80211_C_HOSTAP)
443 ADD(ic, mword, IFM_IEEE80211_HOSTAP);
444 if (ic->ic_caps & IEEE80211_C_AHDEMO)
445 ADD(ic, mword, IFM_IEEE80211_ADHOC | IFM_FLAG0);
446 if (ic->ic_caps & IEEE80211_C_MONITOR)
447 ADD(ic, mword, IFM_IEEE80211_MONITOR);
449 ieee80211_media_status(ifp, &imr);
450 ifmedia_set(&ic->ic_media, imr.ifm_active);
453 ifp->if_baudrate = IF_Mbps(maxrate);
458 ieee80211_announce(struct ieee80211com *ic)
460 struct ifnet *ifp = ic->ic_ifp;
461 int i, mode, rate, mword;
462 struct ieee80211_rateset *rs;
464 for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
465 if ((ic->ic_modecaps & (1<<mode)) == 0)
467 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
468 rs = &ic->ic_sup_rates[mode];
469 for (i = 0; i < rs->rs_nrates; i++) {
470 rate = rs->rs_rates[i];
471 mword = ieee80211_rate2media(ic, rate, mode);
474 kprintf("%s%d%sMbps", (i != 0 ? " " : ""),
475 (rate & IEEE80211_RATE_VAL) / 2,
476 ((rate & 0x1) != 0 ? ".5" : ""));
483 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
485 #define IEEERATE(_ic,_m,_i) \
486 ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
487 int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
488 for (i = 0; i < nrates; i++)
489 if (IEEERATE(ic, mode, i) == rate)
496 * Find an instance by it's mac address.
498 struct ieee80211com *
499 ieee80211_find_vap(const uint8_t mac[IEEE80211_ADDR_LEN])
501 struct ieee80211com *ic;
504 SLIST_FOREACH(ic, &ieee80211_list, ic_next)
505 if (IEEE80211_ADDR_EQ(mac, ic->ic_myaddr))
510 static struct ieee80211com *
511 ieee80211_find_instance(struct ifnet *ifp)
513 struct ieee80211com *ic;
516 /* XXX not right for multiple instances but works for now */
517 SLIST_FOREACH(ic, &ieee80211_list, ic_next)
518 if (ic->ic_ifp == ifp)
524 * Handle a media change request.
527 ieee80211_media_change(struct ifnet *ifp)
529 struct ieee80211com *ic;
530 struct ifmedia_entry *ime;
531 enum ieee80211_opmode newopmode;
532 enum ieee80211_phymode newphymode;
533 int i, j, newrate, error = 0;
535 ic = ieee80211_find_instance(ifp);
537 if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
540 ime = ic->ic_media.ifm_cur;
542 * First, identify the phy mode.
544 switch (IFM_MODE(ime->ifm_media)) {
545 case IFM_IEEE80211_11A:
546 newphymode = IEEE80211_MODE_11A;
548 case IFM_IEEE80211_11B:
549 newphymode = IEEE80211_MODE_11B;
551 case IFM_IEEE80211_11G:
552 newphymode = IEEE80211_MODE_11G;
554 case IFM_IEEE80211_FH:
555 newphymode = IEEE80211_MODE_FH;
558 newphymode = IEEE80211_MODE_AUTO;
564 * Turbo mode is an ``option''.
565 * XXX does not apply to AUTO
567 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
568 if (newphymode == IEEE80211_MODE_11A)
569 newphymode = IEEE80211_MODE_TURBO_A;
570 else if (newphymode == IEEE80211_MODE_11G)
571 newphymode = IEEE80211_MODE_TURBO_G;
576 * Validate requested mode is available.
578 if ((ic->ic_modecaps & (1<<newphymode)) == 0)
582 * Next, the fixed/variable rate.
585 if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
587 * Convert media subtype to rate.
589 newrate = ieee80211_media2rate(ime->ifm_media);
593 * Check the rate table for the specified/current phy.
595 if (newphymode == IEEE80211_MODE_AUTO) {
597 * In autoselect mode search for the rate.
599 for (j = IEEE80211_MODE_11A;
600 j < IEEE80211_MODE_MAX; j++) {
601 if ((ic->ic_modecaps & (1<<j)) == 0)
603 i = findrate(ic, j, newrate);
611 i = findrate(ic, newphymode, newrate);
613 if (i == -1) /* mode/rate mismatch */
616 /* NB: defer rate setting to later */
619 * Deduce new operating mode but don't install it just yet.
621 if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) ==
622 (IFM_IEEE80211_ADHOC|IFM_FLAG0))
623 newopmode = IEEE80211_M_AHDEMO;
624 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
625 newopmode = IEEE80211_M_HOSTAP;
626 else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
627 newopmode = IEEE80211_M_IBSS;
628 else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
629 newopmode = IEEE80211_M_MONITOR;
631 newopmode = IEEE80211_M_STA;
634 * Autoselect doesn't make sense when operating as an AP.
635 * If no phy mode has been selected, pick one and lock it
636 * down so rate tables can be used in forming beacon frames
639 if (newopmode == IEEE80211_M_HOSTAP &&
640 newphymode == IEEE80211_MODE_AUTO) {
641 for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++)
642 if (ic->ic_modecaps & (1<<j)) {
649 * Handle phy mode change.
651 if (ic->ic_curmode != newphymode) { /* change phy mode */
652 error = ieee80211_setmode(ic, newphymode);
659 * Committed to changes, install the rate setting.
661 if (ic->ic_fixed_rate != i) {
662 ic->ic_fixed_rate = i; /* set fixed tx rate */
667 * Handle operating mode change.
669 if (ic->ic_opmode != newopmode) {
671 * Before committing operational mode change, we reset the
672 * internal state machine here, so the resetting is performed
673 * using the current operational mode instead of the new one.
675 * NB: It is safe to reset the internal state machine here,
676 * since all the parameters have been verified.
678 ieee80211_reset_state(ic, ic->ic_state);
680 ic->ic_opmode = newopmode;
682 case IEEE80211_M_AHDEMO:
683 case IEEE80211_M_HOSTAP:
684 case IEEE80211_M_STA:
685 case IEEE80211_M_MONITOR:
686 ic->ic_flags &= ~IEEE80211_F_IBSSON;
688 case IEEE80211_M_IBSS:
689 ic->ic_flags |= IEEE80211_F_IBSSON;
693 * Yech, slot time may change depending on the
694 * operating mode so reset it to be sure everything
695 * is setup appropriately.
697 ieee80211_reset_erp(ic);
698 ieee80211_wme_initparams(ic); /* after opmode change */
702 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
707 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
709 struct ieee80211com *ic;
710 struct ieee80211_rateset *rs;
712 ic = ieee80211_find_instance(ifp);
714 if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
717 imr->ifm_status = IFM_AVALID;
718 imr->ifm_active = IFM_IEEE80211;
719 if (ic->ic_state == IEEE80211_S_RUN)
720 imr->ifm_status |= IFM_ACTIVE;
722 * Calculate a current rate if possible.
724 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
726 * A fixed rate is set, report that.
728 rs = &ic->ic_sup_rates[ic->ic_curmode];
729 imr->ifm_active |= ieee80211_rate2media(ic,
730 rs->rs_rates[ic->ic_fixed_rate], ic->ic_curmode);
731 } else if (ic->ic_opmode == IEEE80211_M_STA) {
733 * In station mode report the current transmit rate.
735 rs = &ic->ic_bss->ni_rates;
736 imr->ifm_active |= ieee80211_rate2media(ic,
737 rs->rs_rates[ic->ic_bss->ni_txrate], ic->ic_curmode);
739 imr->ifm_active |= IFM_AUTO;
740 switch (ic->ic_opmode) {
741 case IEEE80211_M_STA:
743 case IEEE80211_M_IBSS:
744 imr->ifm_active |= IFM_IEEE80211_ADHOC;
746 case IEEE80211_M_AHDEMO:
747 /* should not come here */
749 case IEEE80211_M_HOSTAP:
750 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
752 case IEEE80211_M_MONITOR:
753 imr->ifm_active |= IFM_IEEE80211_MONITOR;
756 switch (ic->ic_curmode) {
757 case IEEE80211_MODE_11A:
758 imr->ifm_active |= IFM_IEEE80211_11A;
760 case IEEE80211_MODE_11B:
761 imr->ifm_active |= IFM_IEEE80211_11B;
763 case IEEE80211_MODE_11G:
764 imr->ifm_active |= IFM_IEEE80211_11G;
766 case IEEE80211_MODE_FH:
767 imr->ifm_active |= IFM_IEEE80211_FH;
769 case IEEE80211_MODE_TURBO_A:
770 imr->ifm_active |= IFM_IEEE80211_11A
771 | IFM_IEEE80211_TURBO;
773 case IEEE80211_MODE_TURBO_G:
774 imr->ifm_active |= IFM_IEEE80211_11G
775 | IFM_IEEE80211_TURBO;
781 ieee80211_watchdog(struct ieee80211com *ic)
783 struct ieee80211_node_table *nt;
784 int need_inact_timer = 0;
786 if (ic->ic_state != IEEE80211_S_INIT) {
787 if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0)
788 ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
790 if (nt->nt_inact_timer) {
791 if (--nt->nt_inact_timer == 0)
793 need_inact_timer += nt->nt_inact_timer;
796 if (nt->nt_inact_timer) {
797 if (--nt->nt_inact_timer == 0)
799 need_inact_timer += nt->nt_inact_timer;
802 if (ic->ic_mgt_timer != 0 || need_inact_timer)
803 ic->ic_ifp->if_timer = 1;
807 * Set the current phy mode and recalculate the active channel
808 * set based on the available channels for this mode. Also
809 * select a new default/current channel if the current one is
810 * inappropriate for this mode.
813 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
815 #define N(a) (sizeof(a) / sizeof(a[0]))
816 static const u_int chanflags[] = {
817 0, /* IEEE80211_MODE_AUTO */
818 IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */
819 IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */
820 IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */
821 IEEE80211_CHAN_FHSS, /* IEEE80211_MODE_FH */
822 IEEE80211_CHAN_T, /* IEEE80211_MODE_TURBO_A */
823 IEEE80211_CHAN_108G, /* IEEE80211_MODE_TURBO_G */
825 struct ieee80211_channel *c;
829 /* validate new mode */
830 if ((ic->ic_modecaps & (1<<mode)) == 0) {
831 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
832 "%s: mode %u not supported (caps 0x%x)\n",
833 __func__, mode, ic->ic_modecaps);
838 * Verify at least one channel is present in the available
839 * channel list before committing to the new mode.
841 KASSERT(mode < N(chanflags), ("Unexpected mode %u", mode));
842 modeflags = chanflags[mode];
843 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
844 c = &ic->ic_channels[i];
845 if (c->ic_flags == 0)
847 if (mode == IEEE80211_MODE_AUTO) {
848 /* ignore static turbo channels for autoselect */
849 if (!IEEE80211_IS_CHAN_T(c))
852 if ((c->ic_flags & modeflags) == modeflags)
856 if (i > IEEE80211_CHAN_MAX) {
857 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
858 "%s: no channels found for mode %u\n", __func__, mode);
863 * Before committing any changes according to the new phymode,
864 * we reset the internal state machine first, so the resetting
865 * will be based on the current phymode instead of the new one.
867 * NB: We have verified that phymode is acceptable, so it is
868 * safe to reset the internal state machine here.
870 if (ic->ic_bss) /* NB: can be called before lateattach */
871 ieee80211_reset_state(ic, ic->ic_state);
874 * Calculate the active channel set.
876 memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
877 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
878 c = &ic->ic_channels[i];
879 if (c->ic_flags == 0)
881 if (mode == IEEE80211_MODE_AUTO) {
882 /* take anything but static turbo channels */
883 if (!IEEE80211_IS_CHAN_T(c))
884 setbit(ic->ic_chan_active, i);
886 if ((c->ic_flags & modeflags) == modeflags)
887 setbit(ic->ic_chan_active, i);
891 * If no current/default channel is setup or the current
892 * channel is wrong for the mode then pick the first
893 * available channel from the active list. This is likely
896 if (ic->ic_ibss_chan == NULL ||
897 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
898 for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
899 if (isset(ic->ic_chan_active, i)) {
900 ic->ic_ibss_chan = &ic->ic_channels[i];
903 KASSERT(ic->ic_ibss_chan != NULL &&
904 isset(ic->ic_chan_active,
905 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)),
906 ("Bad IBSS channel %u",
907 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)));
910 * If the desired channel is set but no longer valid then reset it.
912 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
913 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_des_chan)))
914 ic->ic_des_chan = IEEE80211_CHAN_ANYC;
917 * Setup an initial rate set according to the
918 * current/default channel selected above. This
919 * will be changed when scanning but must exist
920 * now so driver have a consistent state of ic_ibss_chan.
922 if (ic->ic_bss) /* NB: can be called before lateattach */
923 ic->ic_bss->ni_rates = ic->ic_sup_rates[mode];
925 ic->ic_curmode = mode;
926 ieee80211_reset_erp(ic); /* reset ERP state */
927 ieee80211_wme_initparams(ic); /* reset WME stat */
934 * Return the phy mode for with the specified channel so the
935 * caller can select a rate set. This is problematic for channels
936 * where multiple operating modes are possible (e.g. 11g+11b).
937 * In those cases we defer to the current operating mode when set.
939 enum ieee80211_phymode
940 ieee80211_chan2mode(struct ieee80211com *ic, struct ieee80211_channel *chan)
942 if (IEEE80211_IS_CHAN_T(chan)) {
943 return IEEE80211_MODE_TURBO_A;
944 } else if (IEEE80211_IS_CHAN_5GHZ(chan)) {
945 return IEEE80211_MODE_11A;
946 } else if (IEEE80211_IS_CHAN_FHSS(chan)) {
947 return IEEE80211_MODE_FH;
948 } else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN)) {
950 * This assumes all 11g channels are also usable
951 * for 11b, which is currently true.
953 if (ic->ic_curmode == IEEE80211_MODE_TURBO_G)
954 return IEEE80211_MODE_TURBO_G;
955 if (ic->ic_curmode == IEEE80211_MODE_11B)
956 return IEEE80211_MODE_11B;
957 return IEEE80211_MODE_11G;
959 return IEEE80211_MODE_11B;
964 * convert IEEE80211 rate value to ifmedia subtype.
965 * ieee80211 rate is in unit of 0.5Mbps.
968 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
970 #define N(a) (sizeof(a) / sizeof(a[0]))
971 static const struct {
972 u_int m; /* rate + mode */
973 u_int r; /* if_media rate */
975 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
976 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
977 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
978 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
979 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
980 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
981 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
982 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
983 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
984 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
985 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
986 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
987 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
988 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
989 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
990 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
991 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
992 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
993 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
994 { 44 | IFM_IEEE80211_11G, IFM_IEEE80211_DS22 },
995 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
996 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
997 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
998 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
999 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1000 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1001 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1002 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1003 /* NB: OFDM72 doesn't realy exist so we don't handle it */
1007 mask = rate & IEEE80211_RATE_VAL;
1009 case IEEE80211_MODE_11A:
1010 case IEEE80211_MODE_TURBO_A:
1011 mask |= IFM_IEEE80211_11A;
1013 case IEEE80211_MODE_11B:
1014 mask |= IFM_IEEE80211_11B;
1016 case IEEE80211_MODE_FH:
1017 mask |= IFM_IEEE80211_FH;
1019 case IEEE80211_MODE_AUTO:
1020 /* NB: ic may be NULL for some drivers */
1021 if (ic && ic->ic_phytype == IEEE80211_T_FH) {
1022 mask |= IFM_IEEE80211_FH;
1025 /* NB: hack, 11g matches both 11b+11a rates */
1027 case IEEE80211_MODE_11G:
1028 case IEEE80211_MODE_TURBO_G:
1029 mask |= IFM_IEEE80211_11G;
1032 for (i = 0; i < N(rates); i++)
1033 if (rates[i].m == mask)
1040 ieee80211_media2rate(int mword)
1042 #define N(a) (sizeof(a) / sizeof(a[0]))
1043 static const int ieeerates[] = {
1047 2, /* IFM_IEEE80211_FH1 */
1048 4, /* IFM_IEEE80211_FH2 */
1049 2, /* IFM_IEEE80211_DS1 */
1050 4, /* IFM_IEEE80211_DS2 */
1051 11, /* IFM_IEEE80211_DS5 */
1052 22, /* IFM_IEEE80211_DS11 */
1053 44, /* IFM_IEEE80211_DS22 */
1054 12, /* IFM_IEEE80211_OFDM6 */
1055 18, /* IFM_IEEE80211_OFDM9 */
1056 24, /* IFM_IEEE80211_OFDM12 */
1057 36, /* IFM_IEEE80211_OFDM18 */
1058 48, /* IFM_IEEE80211_OFDM24 */
1059 72, /* IFM_IEEE80211_OFDM36 */
1060 96, /* IFM_IEEE80211_OFDM48 */
1061 108, /* IFM_IEEE80211_OFDM54 */
1062 144, /* IFM_IEEE80211_OFDM72 */
1064 return IFM_SUBTYPE(mword) < N(ieeerates) ?
1065 ieeerates[IFM_SUBTYPE(mword)] : 0;
1070 * Covert PLCP signal/rate field to net80211 rate (.5Mbits/s)
1073 ieee80211_plcp2rate(uint8_t plcp, int ofdm)
1077 /* IEEE Std 802.11b-1999 page 15, subclause 18.2.3.3 */
1082 /* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */
1087 #define _OFDM_PLCP2RATE_MAX 16
1089 /* IEEE Std 802.11a-1999 page 14, subclause 17.3.4.1 */
1090 static const uint8_t ofdm_plcp2rate[_OFDM_PLCP2RATE_MAX] = {
1100 if (plcp < _OFDM_PLCP2RATE_MAX)
1101 return ofdm_plcp2rate[plcp];
1103 #undef _OFDM_PLCP2RATE_MAX
1108 enum ieee80211_modtype
1109 ieee80211_rate2modtype(uint8_t rate)
1111 rate &= IEEE80211_RATE_VAL;
1114 return IEEE80211_MODTYPE_PBCC;
1115 else if (rate == 22 || rate < 12)
1116 return IEEE80211_MODTYPE_DS;
1118 return IEEE80211_MODTYPE_OFDM;