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.6 2006/05/18 13:51:46 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 */
213 * Install a default reset method for the ioctl support.
214 * The driver is expected to fill this in before calling us.
216 if (ic->ic_reset == NULL)
217 ic->ic_reset = ieee80211_default_reset;
221 ieee80211_ifdetach(struct ieee80211com *ic)
223 struct ifnet *ifp = ic->ic_ifp;
225 ieee80211_remove_vap(ic);
227 ieee80211_sysctl_detach(ic);
228 ieee80211_proto_detach(ic);
229 ieee80211_crypto_detach(ic);
233 * ieee80211_node_detach() -> ieee80211_node_table_cleanup()
234 * -> ieee80211_free_allnodes_locked()
235 * will assert the serializer
236 * In order to make the assertion work, hold serializer here
240 lwkt_serialize_enter(ifp->if_serializer);
241 ieee80211_node_detach(ic);
242 lwkt_serialize_exit(ifp->if_serializer);
244 ifmedia_removeall(&ic->ic_media);
251 * Convert MHz frequency to IEEE channel number.
254 ieee80211_mhz2ieee(u_int freq, u_int flags)
256 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
260 return (freq - 2407) / 5;
262 return 15 + ((freq - 2512) / 20);
263 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
264 return (freq - 5000) / 5;
265 } else { /* either, guess */
269 return (freq - 2407) / 5;
271 return 15 + ((freq - 2512) / 20);
272 return (freq - 5000) / 5;
277 * Convert channel to IEEE channel number.
280 ieee80211_chan2ieee(struct ieee80211com *ic, struct ieee80211_channel *c)
282 if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX])
283 return c - ic->ic_channels;
284 else if (c == IEEE80211_CHAN_ANYC)
285 return IEEE80211_CHAN_ANY;
286 else if (c != NULL) {
287 if_printf(ic->ic_ifp, "invalid channel freq %u flags %x\n",
288 c->ic_freq, c->ic_flags);
291 if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
297 * Convert IEEE channel number to MHz frequency.
300 ieee80211_ieee2mhz(u_int chan, u_int flags)
302 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
306 return 2407 + chan*5;
308 return 2512 + ((chan-15)*20);
309 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
310 return 5000 + (chan*5);
311 } else { /* either, guess */
314 if (chan < 14) /* 0-13 */
315 return 2407 + chan*5;
316 if (chan < 27) /* 15-26 */
317 return 2512 + ((chan-15)*20);
318 return 5000 + (chan*5);
323 * Setup the media data structures according to the channel and
324 * rate tables. This must be called by the driver after
325 * ieee80211_attach and before most anything else.
328 ieee80211_media_init(struct ieee80211com *ic,
329 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
331 #define ADD(_ic, _s, _o) \
332 ifmedia_add(&(_ic)->ic_media, \
333 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
334 struct ifnet *ifp = ic->ic_ifp;
335 struct ifmediareq imr;
336 int i, j, mode, rate, maxrate, mword, mopt, r;
337 struct ieee80211_rateset *rs;
338 struct ieee80211_rateset allrates;
342 * ieee80211_node_lateattach() -> ieee80211_rest_bss()
343 * -> ieee80211_alloc_node() -> ieee80211_setup_node()
344 * will assert the serializer
345 * In order to make the assertion work, hold serializer here
349 lwkt_serialize_enter(ifp->if_serializer);
352 * Do late attach work that must wait for any subclass
353 * (i.e. driver) work such as overriding methods.
355 ieee80211_node_lateattach(ic);
357 lwkt_serialize_exit(ifp->if_serializer);
360 * Fill in media characteristics.
362 ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
364 memset(&allrates, 0, sizeof(allrates));
365 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) {
366 static const u_int mopts[] = {
372 IFM_IEEE80211_11A | IFM_IEEE80211_TURBO,
373 IFM_IEEE80211_11G | IFM_IEEE80211_TURBO,
375 if ((ic->ic_modecaps & (1<<mode)) == 0)
378 ADD(ic, IFM_AUTO, mopt); /* e.g. 11a auto */
379 if (ic->ic_caps & IEEE80211_C_IBSS)
380 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC);
381 if (ic->ic_caps & IEEE80211_C_HOSTAP)
382 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP);
383 if (ic->ic_caps & IEEE80211_C_AHDEMO)
384 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
385 if (ic->ic_caps & IEEE80211_C_MONITOR)
386 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR);
387 if (mode == IEEE80211_MODE_AUTO)
389 rs = &ic->ic_sup_rates[mode];
390 for (i = 0; i < rs->rs_nrates; i++) {
391 rate = rs->rs_rates[i];
392 mword = ieee80211_rate2media(ic, rate, mode);
395 ADD(ic, mword, mopt);
396 if (ic->ic_caps & IEEE80211_C_IBSS)
397 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
398 if (ic->ic_caps & IEEE80211_C_HOSTAP)
399 ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
400 if (ic->ic_caps & IEEE80211_C_AHDEMO)
401 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
402 if (ic->ic_caps & IEEE80211_C_MONITOR)
403 ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
405 * Add rate to the collection of all rates.
407 r = rate & IEEE80211_RATE_VAL;
408 for (j = 0; j < allrates.rs_nrates; j++)
409 if (allrates.rs_rates[j] == r)
411 if (j == allrates.rs_nrates) {
412 /* unique, add to the set */
413 allrates.rs_rates[j] = r;
414 allrates.rs_nrates++;
416 rate = (rate & IEEE80211_RATE_VAL) / 2;
421 for (i = 0; i < allrates.rs_nrates; i++) {
422 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
423 IEEE80211_MODE_AUTO);
426 mword = IFM_SUBTYPE(mword); /* remove media options */
428 if (ic->ic_caps & IEEE80211_C_IBSS)
429 ADD(ic, mword, IFM_IEEE80211_ADHOC);
430 if (ic->ic_caps & IEEE80211_C_HOSTAP)
431 ADD(ic, mword, IFM_IEEE80211_HOSTAP);
432 if (ic->ic_caps & IEEE80211_C_AHDEMO)
433 ADD(ic, mword, IFM_IEEE80211_ADHOC | IFM_FLAG0);
434 if (ic->ic_caps & IEEE80211_C_MONITOR)
435 ADD(ic, mword, IFM_IEEE80211_MONITOR);
437 ieee80211_media_status(ifp, &imr);
438 ifmedia_set(&ic->ic_media, imr.ifm_active);
441 ifp->if_baudrate = IF_Mbps(maxrate);
446 ieee80211_announce(struct ieee80211com *ic)
448 struct ifnet *ifp = ic->ic_ifp;
449 int i, mode, rate, mword;
450 struct ieee80211_rateset *rs;
452 for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
453 if ((ic->ic_modecaps & (1<<mode)) == 0)
455 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
456 rs = &ic->ic_sup_rates[mode];
457 for (i = 0; i < rs->rs_nrates; i++) {
458 rate = rs->rs_rates[i];
459 mword = ieee80211_rate2media(ic, rate, mode);
462 printf("%s%d%sMbps", (i != 0 ? " " : ""),
463 (rate & IEEE80211_RATE_VAL) / 2,
464 ((rate & 0x1) != 0 ? ".5" : ""));
471 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
473 #define IEEERATE(_ic,_m,_i) \
474 ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
475 int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
476 for (i = 0; i < nrates; i++)
477 if (IEEERATE(ic, mode, i) == rate)
484 * Find an instance by it's mac address.
486 struct ieee80211com *
487 ieee80211_find_vap(const uint8_t mac[IEEE80211_ADDR_LEN])
489 struct ieee80211com *ic;
492 SLIST_FOREACH(ic, &ieee80211_list, ic_next)
493 if (IEEE80211_ADDR_EQ(mac, ic->ic_myaddr))
498 static struct ieee80211com *
499 ieee80211_find_instance(struct ifnet *ifp)
501 struct ieee80211com *ic;
504 /* XXX not right for multiple instances but works for now */
505 SLIST_FOREACH(ic, &ieee80211_list, ic_next)
506 if (ic->ic_ifp == ifp)
512 * Handle a media change request.
515 ieee80211_media_change(struct ifnet *ifp)
517 struct ieee80211com *ic;
518 struct ifmedia_entry *ime;
519 enum ieee80211_opmode newopmode;
520 enum ieee80211_phymode newphymode;
521 int i, j, newrate, error = 0;
523 ic = ieee80211_find_instance(ifp);
525 if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
528 ime = ic->ic_media.ifm_cur;
530 * First, identify the phy mode.
532 switch (IFM_MODE(ime->ifm_media)) {
533 case IFM_IEEE80211_11A:
534 newphymode = IEEE80211_MODE_11A;
536 case IFM_IEEE80211_11B:
537 newphymode = IEEE80211_MODE_11B;
539 case IFM_IEEE80211_11G:
540 newphymode = IEEE80211_MODE_11G;
542 case IFM_IEEE80211_FH:
543 newphymode = IEEE80211_MODE_FH;
546 newphymode = IEEE80211_MODE_AUTO;
552 * Turbo mode is an ``option''.
553 * XXX does not apply to AUTO
555 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
556 if (newphymode == IEEE80211_MODE_11A)
557 newphymode = IEEE80211_MODE_TURBO_A;
558 else if (newphymode == IEEE80211_MODE_11G)
559 newphymode = IEEE80211_MODE_TURBO_G;
564 * Validate requested mode is available.
566 if ((ic->ic_modecaps & (1<<newphymode)) == 0)
570 * Next, the fixed/variable rate.
573 if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
575 * Convert media subtype to rate.
577 newrate = ieee80211_media2rate(ime->ifm_media);
581 * Check the rate table for the specified/current phy.
583 if (newphymode == IEEE80211_MODE_AUTO) {
585 * In autoselect mode search for the rate.
587 for (j = IEEE80211_MODE_11A;
588 j < IEEE80211_MODE_MAX; j++) {
589 if ((ic->ic_modecaps & (1<<j)) == 0)
591 i = findrate(ic, j, newrate);
599 i = findrate(ic, newphymode, newrate);
601 if (i == -1) /* mode/rate mismatch */
604 /* NB: defer rate setting to later */
607 * Deduce new operating mode but don't install it just yet.
609 if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) ==
610 (IFM_IEEE80211_ADHOC|IFM_FLAG0))
611 newopmode = IEEE80211_M_AHDEMO;
612 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
613 newopmode = IEEE80211_M_HOSTAP;
614 else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
615 newopmode = IEEE80211_M_IBSS;
616 else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
617 newopmode = IEEE80211_M_MONITOR;
619 newopmode = IEEE80211_M_STA;
622 * Autoselect doesn't make sense when operating as an AP.
623 * If no phy mode has been selected, pick one and lock it
624 * down so rate tables can be used in forming beacon frames
627 if (newopmode == IEEE80211_M_HOSTAP &&
628 newphymode == IEEE80211_MODE_AUTO) {
629 for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++)
630 if (ic->ic_modecaps & (1<<j)) {
637 * Handle phy mode change.
639 if (ic->ic_curmode != newphymode) { /* change phy mode */
640 error = ieee80211_setmode(ic, newphymode);
647 * Committed to changes, install the rate setting.
649 if (ic->ic_fixed_rate != i) {
650 ic->ic_fixed_rate = i; /* set fixed tx rate */
655 * Handle operating mode change.
657 if (ic->ic_opmode != newopmode) {
658 ic->ic_opmode = newopmode;
660 case IEEE80211_M_AHDEMO:
661 case IEEE80211_M_HOSTAP:
662 case IEEE80211_M_STA:
663 case IEEE80211_M_MONITOR:
664 ic->ic_flags &= ~IEEE80211_F_IBSSON;
666 case IEEE80211_M_IBSS:
667 ic->ic_flags |= IEEE80211_F_IBSSON;
671 * Yech, slot time may change depending on the
672 * operating mode so reset it to be sure everything
673 * is setup appropriately.
675 ieee80211_reset_erp(ic);
676 ieee80211_wme_initparams(ic); /* after opmode change */
681 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
687 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
689 struct ieee80211com *ic;
690 struct ieee80211_rateset *rs;
692 ic = ieee80211_find_instance(ifp);
694 if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
697 imr->ifm_status = IFM_AVALID;
698 imr->ifm_active = IFM_IEEE80211;
699 if (ic->ic_state == IEEE80211_S_RUN)
700 imr->ifm_status |= IFM_ACTIVE;
702 * Calculate a current rate if possible.
704 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
706 * A fixed rate is set, report that.
708 rs = &ic->ic_sup_rates[ic->ic_curmode];
709 imr->ifm_active |= ieee80211_rate2media(ic,
710 rs->rs_rates[ic->ic_fixed_rate], ic->ic_curmode);
711 } else if (ic->ic_opmode == IEEE80211_M_STA) {
713 * In station mode report the current transmit rate.
715 rs = &ic->ic_bss->ni_rates;
716 imr->ifm_active |= ieee80211_rate2media(ic,
717 rs->rs_rates[ic->ic_bss->ni_txrate], ic->ic_curmode);
719 imr->ifm_active |= IFM_AUTO;
720 switch (ic->ic_opmode) {
721 case IEEE80211_M_STA:
723 case IEEE80211_M_IBSS:
724 imr->ifm_active |= IFM_IEEE80211_ADHOC;
726 case IEEE80211_M_AHDEMO:
727 /* should not come here */
729 case IEEE80211_M_HOSTAP:
730 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
732 case IEEE80211_M_MONITOR:
733 imr->ifm_active |= IFM_IEEE80211_MONITOR;
736 switch (ic->ic_curmode) {
737 case IEEE80211_MODE_11A:
738 imr->ifm_active |= IFM_IEEE80211_11A;
740 case IEEE80211_MODE_11B:
741 imr->ifm_active |= IFM_IEEE80211_11B;
743 case IEEE80211_MODE_11G:
744 imr->ifm_active |= IFM_IEEE80211_11G;
746 case IEEE80211_MODE_FH:
747 imr->ifm_active |= IFM_IEEE80211_FH;
749 case IEEE80211_MODE_TURBO_A:
750 imr->ifm_active |= IFM_IEEE80211_11A
751 | IFM_IEEE80211_TURBO;
753 case IEEE80211_MODE_TURBO_G:
754 imr->ifm_active |= IFM_IEEE80211_11G
755 | IFM_IEEE80211_TURBO;
761 ieee80211_watchdog(struct ieee80211com *ic)
763 struct ieee80211_node_table *nt;
764 int need_inact_timer = 0;
766 if (ic->ic_state != IEEE80211_S_INIT) {
767 if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0)
768 ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
770 if (nt->nt_inact_timer) {
771 if (--nt->nt_inact_timer == 0)
773 need_inact_timer += nt->nt_inact_timer;
776 if (nt->nt_inact_timer) {
777 if (--nt->nt_inact_timer == 0)
779 need_inact_timer += nt->nt_inact_timer;
782 if (ic->ic_mgt_timer != 0 || need_inact_timer)
783 ic->ic_ifp->if_timer = 1;
787 * Set the current phy mode and recalculate the active channel
788 * set based on the available channels for this mode. Also
789 * select a new default/current channel if the current one is
790 * inappropriate for this mode.
793 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
795 #define N(a) (sizeof(a) / sizeof(a[0]))
796 static const u_int chanflags[] = {
797 0, /* IEEE80211_MODE_AUTO */
798 IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */
799 IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */
800 IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */
801 IEEE80211_CHAN_FHSS, /* IEEE80211_MODE_FH */
802 IEEE80211_CHAN_T, /* IEEE80211_MODE_TURBO_A */
803 IEEE80211_CHAN_108G, /* IEEE80211_MODE_TURBO_G */
805 struct ieee80211_channel *c;
809 /* validate new mode */
810 if ((ic->ic_modecaps & (1<<mode)) == 0) {
811 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
812 "%s: mode %u not supported (caps 0x%x)\n",
813 __func__, mode, ic->ic_modecaps);
818 * Verify at least one channel is present in the available
819 * channel list before committing to the new mode.
821 KASSERT(mode < N(chanflags), ("Unexpected mode %u", mode));
822 modeflags = chanflags[mode];
823 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
824 c = &ic->ic_channels[i];
825 if (c->ic_flags == 0)
827 if (mode == IEEE80211_MODE_AUTO) {
828 /* ignore static turbo channels for autoselect */
829 if (!IEEE80211_IS_CHAN_T(c))
832 if ((c->ic_flags & modeflags) == modeflags)
836 if (i > IEEE80211_CHAN_MAX) {
837 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
838 "%s: no channels found for mode %u\n", __func__, mode);
843 * Calculate the active channel set.
845 memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
846 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
847 c = &ic->ic_channels[i];
848 if (c->ic_flags == 0)
850 if (mode == IEEE80211_MODE_AUTO) {
851 /* take anything but static turbo channels */
852 if (!IEEE80211_IS_CHAN_T(c))
853 setbit(ic->ic_chan_active, i);
855 if ((c->ic_flags & modeflags) == modeflags)
856 setbit(ic->ic_chan_active, i);
860 * If no current/default channel is setup or the current
861 * channel is wrong for the mode then pick the first
862 * available channel from the active list. This is likely
865 if (ic->ic_ibss_chan == NULL ||
866 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
867 for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
868 if (isset(ic->ic_chan_active, i)) {
869 ic->ic_ibss_chan = &ic->ic_channels[i];
872 KASSERT(ic->ic_ibss_chan != NULL &&
873 isset(ic->ic_chan_active,
874 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)),
875 ("Bad IBSS channel %u",
876 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)));
879 * If the desired channel is set but no longer valid then reset it.
881 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
882 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_des_chan)))
883 ic->ic_des_chan = IEEE80211_CHAN_ANYC;
886 * Do mode-specific rate setup.
888 if (mode == IEEE80211_MODE_11G) {
890 * Use a mixed 11b/11g rate set.
892 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode],
894 } else if (mode == IEEE80211_MODE_11B) {
896 * Force pure 11b rate set.
898 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode],
902 * Setup an initial rate set according to the
903 * current/default channel selected above. This
904 * will be changed when scanning but must exist
905 * now so driver have a consistent state of ic_ibss_chan.
907 if (ic->ic_bss) /* NB: can be called before lateattach */
908 ic->ic_bss->ni_rates = ic->ic_sup_rates[mode];
910 ic->ic_curmode = mode;
911 ieee80211_reset_erp(ic); /* reset ERP state */
912 ieee80211_wme_initparams(ic); /* reset WME stat */
919 * Return the phy mode for with the specified channel so the
920 * caller can select a rate set. This is problematic for channels
921 * where multiple operating modes are possible (e.g. 11g+11b).
922 * In those cases we defer to the current operating mode when set.
924 enum ieee80211_phymode
925 ieee80211_chan2mode(struct ieee80211com *ic, struct ieee80211_channel *chan)
927 if (IEEE80211_IS_CHAN_T(chan)) {
928 return IEEE80211_MODE_TURBO_A;
929 } else if (IEEE80211_IS_CHAN_5GHZ(chan)) {
930 return IEEE80211_MODE_11A;
931 } else if (IEEE80211_IS_CHAN_FHSS(chan)) {
932 return IEEE80211_MODE_FH;
933 } else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN)) {
935 * This assumes all 11g channels are also usable
936 * for 11b, which is currently true.
938 if (ic->ic_curmode == IEEE80211_MODE_TURBO_G)
939 return IEEE80211_MODE_TURBO_G;
940 if (ic->ic_curmode == IEEE80211_MODE_11B)
941 return IEEE80211_MODE_11B;
942 return IEEE80211_MODE_11G;
944 return IEEE80211_MODE_11B;
949 * convert IEEE80211 rate value to ifmedia subtype.
950 * ieee80211 rate is in unit of 0.5Mbps.
953 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
955 #define N(a) (sizeof(a) / sizeof(a[0]))
956 static const struct {
957 u_int m; /* rate + mode */
958 u_int r; /* if_media rate */
960 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
961 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
962 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
963 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
964 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
965 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
966 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
967 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
968 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
969 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
970 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
971 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
972 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
973 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
974 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
975 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
976 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
977 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
978 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
979 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
980 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
981 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
982 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
983 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
984 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
985 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
986 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
987 /* NB: OFDM72 doesn't realy exist so we don't handle it */
991 mask = rate & IEEE80211_RATE_VAL;
993 case IEEE80211_MODE_11A:
994 case IEEE80211_MODE_TURBO_A:
995 mask |= IFM_IEEE80211_11A;
997 case IEEE80211_MODE_11B:
998 mask |= IFM_IEEE80211_11B;
1000 case IEEE80211_MODE_FH:
1001 mask |= IFM_IEEE80211_FH;
1003 case IEEE80211_MODE_AUTO:
1004 /* NB: ic may be NULL for some drivers */
1005 if (ic && ic->ic_phytype == IEEE80211_T_FH) {
1006 mask |= IFM_IEEE80211_FH;
1009 /* NB: hack, 11g matches both 11b+11a rates */
1011 case IEEE80211_MODE_11G:
1012 case IEEE80211_MODE_TURBO_G:
1013 mask |= IFM_IEEE80211_11G;
1016 for (i = 0; i < N(rates); i++)
1017 if (rates[i].m == mask)
1024 ieee80211_media2rate(int mword)
1026 #define N(a) (sizeof(a) / sizeof(a[0]))
1027 static const int ieeerates[] = {
1031 2, /* IFM_IEEE80211_FH1 */
1032 4, /* IFM_IEEE80211_FH2 */
1033 2, /* IFM_IEEE80211_DS1 */
1034 4, /* IFM_IEEE80211_DS2 */
1035 11, /* IFM_IEEE80211_DS5 */
1036 22, /* IFM_IEEE80211_DS11 */
1037 44, /* IFM_IEEE80211_DS22 */
1038 12, /* IFM_IEEE80211_OFDM6 */
1039 18, /* IFM_IEEE80211_OFDM9 */
1040 24, /* IFM_IEEE80211_OFDM12 */
1041 36, /* IFM_IEEE80211_OFDM18 */
1042 48, /* IFM_IEEE80211_OFDM24 */
1043 72, /* IFM_IEEE80211_OFDM36 */
1044 96, /* IFM_IEEE80211_OFDM48 */
1045 108, /* IFM_IEEE80211_OFDM54 */
1046 144, /* IFM_IEEE80211_OFDM72 */
1048 return IFM_SUBTYPE(mword) < N(ieeerates) ?
1049 ieeerates[IFM_SUBTYPE(mword)] : 0;