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.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 * IEEE 802.11 generic handler
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
44 #include <sys/socket.h>
47 #include <net/if_media.h>
48 #include <net/ethernet.h>
50 #include <net80211/ieee80211_var.h>
54 const char *ieee80211_phymode_name[] = {
55 "auto", /* IEEE80211_MODE_AUTO */
56 "11a", /* IEEE80211_MODE_11A */
57 "11b", /* IEEE80211_MODE_11B */
58 "11g", /* IEEE80211_MODE_11G */
59 "FH", /* IEEE80211_MODE_FH */
60 "turboA", /* IEEE80211_MODE_TURBO_A */
61 "turboG", /* IEEE80211_MODE_TURBO_G */
64 /* list of all instances */
65 SLIST_HEAD(ieee80211_list, ieee80211com);
66 static struct ieee80211_list ieee80211_list =
67 SLIST_HEAD_INITIALIZER(ieee80211_list);
68 static u_int8_t ieee80211_vapmap[32]; /* enough for 256 */
69 static struct mtx ieee80211_vap_mtx;
70 MTX_SYSINIT(ieee80211, &ieee80211_vap_mtx, "net80211 instances", MTX_DEF);
73 ieee80211_add_vap(struct ieee80211com *ic)
75 #define N(a) (sizeof(a)/sizeof(a[0]))
79 mtx_lock(&ieee80211_vap_mtx);
81 for (i = 0; i < N(ieee80211_vapmap) && ieee80211_vapmap[i] == 0xff; i++)
83 if (i == N(ieee80211_vapmap))
84 panic("vap table full");
85 for (b = ieee80211_vapmap[i]; b & 1; b >>= 1)
87 setbit(ieee80211_vapmap, ic->ic_vap);
88 SLIST_INSERT_HEAD(&ieee80211_list, ic, ic_next);
89 mtx_unlock(&ieee80211_vap_mtx);
94 ieee80211_remove_vap(struct ieee80211com *ic)
96 mtx_lock(&ieee80211_vap_mtx);
97 SLIST_REMOVE(&ieee80211_list, ic, ieee80211com, ic_next);
98 KASSERT(ic->ic_vap < sizeof(ieee80211_vapmap)*NBBY,
99 ("invalid vap id %d", ic->ic_vap));
100 KASSERT(isset(ieee80211_vapmap, ic->ic_vap),
101 ("vap id %d not allocated", ic->ic_vap));
102 clrbit(ieee80211_vapmap, ic->ic_vap);
103 mtx_unlock(&ieee80211_vap_mtx);
107 * Default reset method for use with the ioctl support. This
108 * method is invoked after any state change in the 802.11
109 * layer that should be propagated to the hardware but not
110 * require re-initialization of the 802.11 state machine (e.g
111 * rescanning for an ap). We always return ENETRESET which
112 * should cause the driver to re-initialize the device. Drivers
113 * can override this method to implement more optimized support.
116 ieee80211_default_reset(struct ifnet *ifp)
122 ieee80211_ifattach(struct ieee80211com *ic)
124 struct ifnet *ifp = ic->ic_ifp;
125 struct ieee80211_channel *c;
128 ether_ifattach(ifp, ic->ic_myaddr);
129 bpfattach2(ifp, DLT_IEEE802_11,
130 sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf);
132 ieee80211_crypto_attach(ic);
135 * Fill in 802.11 available channel set, mark
136 * all available channels as active, and pick
137 * a default channel if not already specified.
139 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
140 ic->ic_modecaps |= 1<<IEEE80211_MODE_AUTO;
141 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
142 c = &ic->ic_channels[i];
145 * Verify driver passed us valid data.
147 if (i != ieee80211_chan2ieee(ic, c)) {
148 if_printf(ifp, "bad channel ignored; "
149 "freq %u flags %x number %u\n",
150 c->ic_freq, c->ic_flags, i);
151 c->ic_flags = 0; /* NB: remove */
154 setbit(ic->ic_chan_avail, i);
156 * Identify mode capabilities.
158 if (IEEE80211_IS_CHAN_A(c))
159 ic->ic_modecaps |= 1<<IEEE80211_MODE_11A;
160 if (IEEE80211_IS_CHAN_B(c))
161 ic->ic_modecaps |= 1<<IEEE80211_MODE_11B;
162 if (IEEE80211_IS_CHAN_PUREG(c))
163 ic->ic_modecaps |= 1<<IEEE80211_MODE_11G;
164 if (IEEE80211_IS_CHAN_FHSS(c))
165 ic->ic_modecaps |= 1<<IEEE80211_MODE_FH;
166 if (IEEE80211_IS_CHAN_T(c))
167 ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO_A;
168 if (IEEE80211_IS_CHAN_108G(c))
169 ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO_G;
172 /* validate ic->ic_curmode */
173 if ((ic->ic_modecaps & (1<<ic->ic_curmode)) == 0)
174 ic->ic_curmode = IEEE80211_MODE_AUTO;
175 ic->ic_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
178 * Enable WME by default if we're capable.
180 if (ic->ic_caps & IEEE80211_C_WME)
181 ic->ic_flags |= IEEE80211_F_WME;
183 (void) ieee80211_setmode(ic, ic->ic_curmode);
185 if (ic->ic_lintval == 0)
186 ic->ic_lintval = IEEE80211_BINTVAL_DEFAULT;
187 ic->ic_bmisstimeout = 7*ic->ic_lintval; /* default 7 beacons */
188 ic->ic_dtim_period = IEEE80211_DTIM_DEFAULT;
189 IEEE80211_BEACON_LOCK_INIT(ic, "beacon");
191 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
193 ieee80211_node_attach(ic);
194 ieee80211_proto_attach(ic);
196 ieee80211_add_vap(ic);
198 ieee80211_sysctl_attach(ic); /* NB: requires ic_vap */
201 * Install a default reset method for the ioctl support.
202 * The driver is expected to fill this in before calling us.
204 if (ic->ic_reset == NULL)
205 ic->ic_reset = ieee80211_default_reset;
209 ieee80211_ifdetach(struct ieee80211com *ic)
211 struct ifnet *ifp = ic->ic_ifp;
213 ieee80211_remove_vap(ic);
215 ieee80211_sysctl_detach(ic);
216 ieee80211_proto_detach(ic);
217 ieee80211_crypto_detach(ic);
218 ieee80211_node_detach(ic);
219 ifmedia_removeall(&ic->ic_media);
221 IEEE80211_BEACON_LOCK_DESTROY(ic);
228 * Convert MHz frequency to IEEE channel number.
231 ieee80211_mhz2ieee(u_int freq, u_int flags)
233 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
237 return (freq - 2407) / 5;
239 return 15 + ((freq - 2512) / 20);
240 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
241 return (freq - 5000) / 5;
242 } else { /* either, guess */
246 return (freq - 2407) / 5;
248 return 15 + ((freq - 2512) / 20);
249 return (freq - 5000) / 5;
254 * Convert channel to IEEE channel number.
257 ieee80211_chan2ieee(struct ieee80211com *ic, struct ieee80211_channel *c)
259 if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX])
260 return c - ic->ic_channels;
261 else if (c == IEEE80211_CHAN_ANYC)
262 return IEEE80211_CHAN_ANY;
263 else if (c != NULL) {
264 if_printf(ic->ic_ifp, "invalid channel freq %u flags %x\n",
265 c->ic_freq, c->ic_flags);
268 if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
274 * Convert IEEE channel number to MHz frequency.
277 ieee80211_ieee2mhz(u_int chan, u_int flags)
279 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
283 return 2407 + chan*5;
285 return 2512 + ((chan-15)*20);
286 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
287 return 5000 + (chan*5);
288 } else { /* either, guess */
291 if (chan < 14) /* 0-13 */
292 return 2407 + chan*5;
293 if (chan < 27) /* 15-26 */
294 return 2512 + ((chan-15)*20);
295 return 5000 + (chan*5);
300 * Setup the media data structures according to the channel and
301 * rate tables. This must be called by the driver after
302 * ieee80211_attach and before most anything else.
305 ieee80211_media_init(struct ieee80211com *ic,
306 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
308 #define ADD(_ic, _s, _o) \
309 ifmedia_add(&(_ic)->ic_media, \
310 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
311 struct ifnet *ifp = ic->ic_ifp;
312 struct ifmediareq imr;
313 int i, j, mode, rate, maxrate, mword, mopt, r;
314 struct ieee80211_rateset *rs;
315 struct ieee80211_rateset allrates;
318 * Do late attach work that must wait for any subclass
319 * (i.e. driver) work such as overriding methods.
321 ieee80211_node_lateattach(ic);
324 * Fill in media characteristics.
326 ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
328 memset(&allrates, 0, sizeof(allrates));
329 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) {
330 static const u_int mopts[] = {
336 IFM_IEEE80211_11A | IFM_IEEE80211_TURBO,
337 IFM_IEEE80211_11G | IFM_IEEE80211_TURBO,
339 if ((ic->ic_modecaps & (1<<mode)) == 0)
342 ADD(ic, IFM_AUTO, mopt); /* e.g. 11a auto */
343 if (ic->ic_caps & IEEE80211_C_IBSS)
344 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC);
345 if (ic->ic_caps & IEEE80211_C_HOSTAP)
346 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP);
347 if (ic->ic_caps & IEEE80211_C_AHDEMO)
348 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
349 if (ic->ic_caps & IEEE80211_C_MONITOR)
350 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR);
351 if (mode == IEEE80211_MODE_AUTO)
353 rs = &ic->ic_sup_rates[mode];
354 for (i = 0; i < rs->rs_nrates; i++) {
355 rate = rs->rs_rates[i];
356 mword = ieee80211_rate2media(ic, rate, mode);
359 ADD(ic, mword, mopt);
360 if (ic->ic_caps & IEEE80211_C_IBSS)
361 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
362 if (ic->ic_caps & IEEE80211_C_HOSTAP)
363 ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
364 if (ic->ic_caps & IEEE80211_C_AHDEMO)
365 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
366 if (ic->ic_caps & IEEE80211_C_MONITOR)
367 ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
369 * Add rate to the collection of all rates.
371 r = rate & IEEE80211_RATE_VAL;
372 for (j = 0; j < allrates.rs_nrates; j++)
373 if (allrates.rs_rates[j] == r)
375 if (j == allrates.rs_nrates) {
376 /* unique, add to the set */
377 allrates.rs_rates[j] = r;
378 allrates.rs_nrates++;
380 rate = (rate & IEEE80211_RATE_VAL) / 2;
385 for (i = 0; i < allrates.rs_nrates; i++) {
386 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
387 IEEE80211_MODE_AUTO);
390 mword = IFM_SUBTYPE(mword); /* remove media options */
392 if (ic->ic_caps & IEEE80211_C_IBSS)
393 ADD(ic, mword, IFM_IEEE80211_ADHOC);
394 if (ic->ic_caps & IEEE80211_C_HOSTAP)
395 ADD(ic, mword, IFM_IEEE80211_HOSTAP);
396 if (ic->ic_caps & IEEE80211_C_AHDEMO)
397 ADD(ic, mword, IFM_IEEE80211_ADHOC | IFM_FLAG0);
398 if (ic->ic_caps & IEEE80211_C_MONITOR)
399 ADD(ic, mword, IFM_IEEE80211_MONITOR);
401 ieee80211_media_status(ifp, &imr);
402 ifmedia_set(&ic->ic_media, imr.ifm_active);
405 ifp->if_baudrate = IF_Mbps(maxrate);
410 ieee80211_announce(struct ieee80211com *ic)
412 struct ifnet *ifp = ic->ic_ifp;
413 int i, mode, rate, mword;
414 struct ieee80211_rateset *rs;
416 for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
417 if ((ic->ic_modecaps & (1<<mode)) == 0)
419 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
420 rs = &ic->ic_sup_rates[mode];
421 for (i = 0; i < rs->rs_nrates; i++) {
422 rate = rs->rs_rates[i];
423 mword = ieee80211_rate2media(ic, rate, mode);
426 printf("%s%d%sMbps", (i != 0 ? " " : ""),
427 (rate & IEEE80211_RATE_VAL) / 2,
428 ((rate & 0x1) != 0 ? ".5" : ""));
435 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
437 #define IEEERATE(_ic,_m,_i) \
438 ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
439 int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
440 for (i = 0; i < nrates; i++)
441 if (IEEERATE(ic, mode, i) == rate)
448 * Find an instance by it's mac address.
450 struct ieee80211com *
451 ieee80211_find_vap(const u_int8_t mac[IEEE80211_ADDR_LEN])
453 struct ieee80211com *ic;
456 SLIST_FOREACH(ic, &ieee80211_list, ic_next)
457 if (IEEE80211_ADDR_EQ(mac, ic->ic_myaddr))
462 static struct ieee80211com *
463 ieee80211_find_instance(struct ifnet *ifp)
465 struct ieee80211com *ic;
468 /* XXX not right for multiple instances but works for now */
469 SLIST_FOREACH(ic, &ieee80211_list, ic_next)
470 if (ic->ic_ifp == ifp)
476 * Handle a media change request.
479 ieee80211_media_change(struct ifnet *ifp)
481 struct ieee80211com *ic;
482 struct ifmedia_entry *ime;
483 enum ieee80211_opmode newopmode;
484 enum ieee80211_phymode newphymode;
485 int i, j, newrate, error = 0;
487 ic = ieee80211_find_instance(ifp);
489 if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
492 ime = ic->ic_media.ifm_cur;
494 * First, identify the phy mode.
496 switch (IFM_MODE(ime->ifm_media)) {
497 case IFM_IEEE80211_11A:
498 newphymode = IEEE80211_MODE_11A;
500 case IFM_IEEE80211_11B:
501 newphymode = IEEE80211_MODE_11B;
503 case IFM_IEEE80211_11G:
504 newphymode = IEEE80211_MODE_11G;
506 case IFM_IEEE80211_FH:
507 newphymode = IEEE80211_MODE_FH;
510 newphymode = IEEE80211_MODE_AUTO;
516 * Turbo mode is an ``option''.
517 * XXX does not apply to AUTO
519 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
520 if (newphymode == IEEE80211_MODE_11A)
521 newphymode = IEEE80211_MODE_TURBO_A;
522 else if (newphymode == IEEE80211_MODE_11G)
523 newphymode = IEEE80211_MODE_TURBO_G;
528 * Validate requested mode is available.
530 if ((ic->ic_modecaps & (1<<newphymode)) == 0)
534 * Next, the fixed/variable rate.
537 if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
539 * Convert media subtype to rate.
541 newrate = ieee80211_media2rate(ime->ifm_media);
545 * Check the rate table for the specified/current phy.
547 if (newphymode == IEEE80211_MODE_AUTO) {
549 * In autoselect mode search for the rate.
551 for (j = IEEE80211_MODE_11A;
552 j < IEEE80211_MODE_MAX; j++) {
553 if ((ic->ic_modecaps & (1<<j)) == 0)
555 i = findrate(ic, j, newrate);
563 i = findrate(ic, newphymode, newrate);
565 if (i == -1) /* mode/rate mismatch */
568 /* NB: defer rate setting to later */
571 * Deduce new operating mode but don't install it just yet.
573 if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) ==
574 (IFM_IEEE80211_ADHOC|IFM_FLAG0))
575 newopmode = IEEE80211_M_AHDEMO;
576 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
577 newopmode = IEEE80211_M_HOSTAP;
578 else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
579 newopmode = IEEE80211_M_IBSS;
580 else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
581 newopmode = IEEE80211_M_MONITOR;
583 newopmode = IEEE80211_M_STA;
586 * Autoselect doesn't make sense when operating as an AP.
587 * If no phy mode has been selected, pick one and lock it
588 * down so rate tables can be used in forming beacon frames
591 if (newopmode == IEEE80211_M_HOSTAP &&
592 newphymode == IEEE80211_MODE_AUTO) {
593 for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++)
594 if (ic->ic_modecaps & (1<<j)) {
601 * Handle phy mode change.
603 if (ic->ic_curmode != newphymode) { /* change phy mode */
604 error = ieee80211_setmode(ic, newphymode);
611 * Committed to changes, install the rate setting.
613 if (ic->ic_fixed_rate != i) {
614 ic->ic_fixed_rate = i; /* set fixed tx rate */
619 * Handle operating mode change.
621 if (ic->ic_opmode != newopmode) {
622 ic->ic_opmode = newopmode;
624 case IEEE80211_M_AHDEMO:
625 case IEEE80211_M_HOSTAP:
626 case IEEE80211_M_STA:
627 case IEEE80211_M_MONITOR:
628 ic->ic_flags &= ~IEEE80211_F_IBSSON;
630 case IEEE80211_M_IBSS:
631 ic->ic_flags |= IEEE80211_F_IBSSON;
635 * Yech, slot time may change depending on the
636 * operating mode so reset it to be sure everything
637 * is setup appropriately.
639 ieee80211_reset_erp(ic);
640 ieee80211_wme_initparams(ic); /* after opmode change */
645 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
651 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
653 struct ieee80211com *ic;
654 struct ieee80211_rateset *rs;
656 ic = ieee80211_find_instance(ifp);
658 if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
661 imr->ifm_status = IFM_AVALID;
662 imr->ifm_active = IFM_IEEE80211;
663 if (ic->ic_state == IEEE80211_S_RUN)
664 imr->ifm_status |= IFM_ACTIVE;
666 * Calculate a current rate if possible.
668 if (ic->ic_fixed_rate != -1) {
670 * A fixed rate is set, report that.
672 rs = &ic->ic_sup_rates[ic->ic_curmode];
673 imr->ifm_active |= ieee80211_rate2media(ic,
674 rs->rs_rates[ic->ic_fixed_rate], ic->ic_curmode);
675 } else if (ic->ic_opmode == IEEE80211_M_STA) {
677 * In station mode report the current transmit rate.
679 rs = &ic->ic_bss->ni_rates;
680 imr->ifm_active |= ieee80211_rate2media(ic,
681 rs->rs_rates[ic->ic_bss->ni_txrate], ic->ic_curmode);
683 imr->ifm_active |= IFM_AUTO;
684 switch (ic->ic_opmode) {
685 case IEEE80211_M_STA:
687 case IEEE80211_M_IBSS:
688 imr->ifm_active |= IFM_IEEE80211_ADHOC;
690 case IEEE80211_M_AHDEMO:
691 /* should not come here */
693 case IEEE80211_M_HOSTAP:
694 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
696 case IEEE80211_M_MONITOR:
697 imr->ifm_active |= IFM_IEEE80211_MONITOR;
700 switch (ic->ic_curmode) {
701 case IEEE80211_MODE_11A:
702 imr->ifm_active |= IFM_IEEE80211_11A;
704 case IEEE80211_MODE_11B:
705 imr->ifm_active |= IFM_IEEE80211_11B;
707 case IEEE80211_MODE_11G:
708 imr->ifm_active |= IFM_IEEE80211_11G;
710 case IEEE80211_MODE_FH:
711 imr->ifm_active |= IFM_IEEE80211_FH;
713 case IEEE80211_MODE_TURBO_A:
714 imr->ifm_active |= IFM_IEEE80211_11A
715 | IFM_IEEE80211_TURBO;
717 case IEEE80211_MODE_TURBO_G:
718 imr->ifm_active |= IFM_IEEE80211_11G
719 | IFM_IEEE80211_TURBO;
725 ieee80211_watchdog(struct ieee80211com *ic)
727 struct ieee80211_node_table *nt;
728 int need_inact_timer = 0;
730 if (ic->ic_state != IEEE80211_S_INIT) {
731 if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0)
732 ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
734 if (nt->nt_inact_timer) {
735 if (--nt->nt_inact_timer == 0)
737 need_inact_timer += nt->nt_inact_timer;
740 if (nt->nt_inact_timer) {
741 if (--nt->nt_inact_timer == 0)
743 need_inact_timer += nt->nt_inact_timer;
746 if (ic->ic_mgt_timer != 0 || need_inact_timer)
747 ic->ic_ifp->if_timer = 1;
751 * Set the current phy mode and recalculate the active channel
752 * set based on the available channels for this mode. Also
753 * select a new default/current channel if the current one is
754 * inappropriate for this mode.
757 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
759 #define N(a) (sizeof(a) / sizeof(a[0]))
760 static const u_int chanflags[] = {
761 0, /* IEEE80211_MODE_AUTO */
762 IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */
763 IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */
764 IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */
765 IEEE80211_CHAN_FHSS, /* IEEE80211_MODE_FH */
766 IEEE80211_CHAN_T, /* IEEE80211_MODE_TURBO_A */
767 IEEE80211_CHAN_108G, /* IEEE80211_MODE_TURBO_G */
769 struct ieee80211_channel *c;
773 /* validate new mode */
774 if ((ic->ic_modecaps & (1<<mode)) == 0) {
775 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
776 "%s: mode %u not supported (caps 0x%x)\n",
777 __func__, mode, ic->ic_modecaps);
782 * Verify at least one channel is present in the available
783 * channel list before committing to the new mode.
785 KASSERT(mode < N(chanflags), ("Unexpected mode %u", mode));
786 modeflags = chanflags[mode];
787 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
788 c = &ic->ic_channels[i];
789 if (mode == IEEE80211_MODE_AUTO) {
790 /* ignore turbo channels for autoselect */
791 if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
794 if ((c->ic_flags & modeflags) == modeflags)
798 if (i > IEEE80211_CHAN_MAX) {
799 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
800 "%s: no channels found for mode %u\n", __func__, mode);
805 * Calculate the active channel set.
807 memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
808 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
809 c = &ic->ic_channels[i];
810 if (mode == IEEE80211_MODE_AUTO) {
811 /* take anything but pure turbo channels */
812 if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
813 setbit(ic->ic_chan_active, i);
815 if ((c->ic_flags & modeflags) == modeflags)
816 setbit(ic->ic_chan_active, i);
820 * If no current/default channel is setup or the current
821 * channel is wrong for the mode then pick the first
822 * available channel from the active list. This is likely
825 if (ic->ic_ibss_chan == NULL ||
826 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
827 for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
828 if (isset(ic->ic_chan_active, i)) {
829 ic->ic_ibss_chan = &ic->ic_channels[i];
832 KASSERT(ic->ic_ibss_chan != NULL &&
833 isset(ic->ic_chan_active,
834 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)),
835 ("Bad IBSS channel %u",
836 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)));
839 * If the desired channel is set but no longer valid then reset it.
841 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
842 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_des_chan)))
843 ic->ic_des_chan = IEEE80211_CHAN_ANYC;
846 * Do mode-specific rate setup.
848 if (mode == IEEE80211_MODE_11G) {
850 * Use a mixed 11b/11g rate set.
852 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode],
854 } else if (mode == IEEE80211_MODE_11B) {
856 * Force pure 11b rate set.
858 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode],
862 * Setup an initial rate set according to the
863 * current/default channel selected above. This
864 * will be changed when scanning but must exist
865 * now so driver have a consistent state of ic_ibss_chan.
867 if (ic->ic_bss) /* NB: can be called before lateattach */
868 ic->ic_bss->ni_rates = ic->ic_sup_rates[mode];
870 ic->ic_curmode = mode;
871 ieee80211_reset_erp(ic); /* reset ERP state */
872 ieee80211_wme_initparams(ic); /* reset WME stat */
879 * Return the phy mode for with the specified channel so the
880 * caller can select a rate set. This is problematic for channels
881 * where multiple operating modes are possible (e.g. 11g+11b).
882 * In those cases we defer to the current operating mode when set.
884 enum ieee80211_phymode
885 ieee80211_chan2mode(struct ieee80211com *ic, struct ieee80211_channel *chan)
887 if (IEEE80211_IS_CHAN_5GHZ(chan)) {
889 * This assumes all 11a turbo channels are also
890 * usable withut turbo, which is currently true.
892 if (ic->ic_curmode == IEEE80211_MODE_TURBO_A)
893 return IEEE80211_MODE_TURBO_A;
894 return IEEE80211_MODE_11A;
895 } else if (IEEE80211_IS_CHAN_FHSS(chan))
896 return IEEE80211_MODE_FH;
897 else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN)) {
899 * This assumes all 11g channels are also usable
900 * for 11b, which is currently true.
902 if (ic->ic_curmode == IEEE80211_MODE_TURBO_G)
903 return IEEE80211_MODE_TURBO_G;
904 if (ic->ic_curmode == IEEE80211_MODE_11B)
905 return IEEE80211_MODE_11B;
906 return IEEE80211_MODE_11G;
908 return IEEE80211_MODE_11B;
912 * convert IEEE80211 rate value to ifmedia subtype.
913 * ieee80211 rate is in unit of 0.5Mbps.
916 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
918 #define N(a) (sizeof(a) / sizeof(a[0]))
919 static const struct {
920 u_int m; /* rate + mode */
921 u_int r; /* if_media rate */
923 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
924 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
925 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
926 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
927 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
928 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
929 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
930 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
931 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
932 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
933 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
934 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
935 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
936 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
937 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
938 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
939 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
940 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
941 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
942 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
943 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
944 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
945 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
946 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
947 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
948 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
949 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
950 /* NB: OFDM72 doesn't realy exist so we don't handle it */
954 mask = rate & IEEE80211_RATE_VAL;
956 case IEEE80211_MODE_11A:
957 case IEEE80211_MODE_TURBO_A:
958 mask |= IFM_IEEE80211_11A;
960 case IEEE80211_MODE_11B:
961 mask |= IFM_IEEE80211_11B;
963 case IEEE80211_MODE_FH:
964 mask |= IFM_IEEE80211_FH;
966 case IEEE80211_MODE_AUTO:
967 /* NB: ic may be NULL for some drivers */
968 if (ic && ic->ic_phytype == IEEE80211_T_FH) {
969 mask |= IFM_IEEE80211_FH;
972 /* NB: hack, 11g matches both 11b+11a rates */
974 case IEEE80211_MODE_11G:
975 case IEEE80211_MODE_TURBO_G:
976 mask |= IFM_IEEE80211_11G;
979 for (i = 0; i < N(rates); i++)
980 if (rates[i].m == mask)
987 ieee80211_media2rate(int mword)
989 #define N(a) (sizeof(a) / sizeof(a[0]))
990 static const int ieeerates[] = {
994 2, /* IFM_IEEE80211_FH1 */
995 4, /* IFM_IEEE80211_FH2 */
996 2, /* IFM_IEEE80211_DS1 */
997 4, /* IFM_IEEE80211_DS2 */
998 11, /* IFM_IEEE80211_DS5 */
999 22, /* IFM_IEEE80211_DS11 */
1000 44, /* IFM_IEEE80211_DS22 */
1001 12, /* IFM_IEEE80211_OFDM6 */
1002 18, /* IFM_IEEE80211_OFDM9 */
1003 24, /* IFM_IEEE80211_OFDM12 */
1004 36, /* IFM_IEEE80211_OFDM18 */
1005 48, /* IFM_IEEE80211_OFDM24 */
1006 72, /* IFM_IEEE80211_OFDM36 */
1007 96, /* IFM_IEEE80211_OFDM48 */
1008 108, /* IFM_IEEE80211_OFDM54 */
1009 144, /* IFM_IEEE80211_OFDM72 */
1011 return IFM_SUBTYPE(mword) < N(ieeerates) ?
1012 ieeerates[IFM_SUBTYPE(mword)] : 0;