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 */
65 * Default supported rates for 802.11 operation (in IEEE .5Mb units).
67 #define B(r) ((r) | IEEE80211_RATE_BASIC)
68 static const struct ieee80211_rateset ieee80211_rateset_11a =
69 { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
70 static const struct ieee80211_rateset ieee80211_rateset_11b =
71 { 4, { B(2), B(4), B(11), B(22) } };
72 /* NB: OFDM rates are handled specially based on mode */
73 static const struct ieee80211_rateset ieee80211_rateset_11g =
74 { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
77 /* list of all instances */
78 SLIST_HEAD(ieee80211_list, ieee80211com);
79 static struct ieee80211_list ieee80211_list =
80 SLIST_HEAD_INITIALIZER(ieee80211_list);
81 static u_int8_t ieee80211_vapmap[32]; /* enough for 256 */
82 static struct mtx ieee80211_vap_mtx;
83 MTX_SYSINIT(ieee80211, &ieee80211_vap_mtx, "net80211 instances", MTX_DEF);
86 ieee80211_add_vap(struct ieee80211com *ic)
88 #define N(a) (sizeof(a)/sizeof(a[0]))
92 mtx_lock(&ieee80211_vap_mtx);
94 for (i = 0; i < N(ieee80211_vapmap) && ieee80211_vapmap[i] == 0xff; i++)
96 if (i == N(ieee80211_vapmap))
97 panic("vap table full");
98 for (b = ieee80211_vapmap[i]; b & 1; b >>= 1)
100 setbit(ieee80211_vapmap, ic->ic_vap);
101 SLIST_INSERT_HEAD(&ieee80211_list, ic, ic_next);
102 mtx_unlock(&ieee80211_vap_mtx);
107 ieee80211_remove_vap(struct ieee80211com *ic)
109 mtx_lock(&ieee80211_vap_mtx);
110 SLIST_REMOVE(&ieee80211_list, ic, ieee80211com, ic_next);
111 KASSERT(ic->ic_vap < sizeof(ieee80211_vapmap)*NBBY,
112 ("invalid vap id %d", ic->ic_vap));
113 KASSERT(isset(ieee80211_vapmap, ic->ic_vap),
114 ("vap id %d not allocated", ic->ic_vap));
115 clrbit(ieee80211_vapmap, ic->ic_vap);
116 mtx_unlock(&ieee80211_vap_mtx);
120 * Default reset method for use with the ioctl support. This
121 * method is invoked after any state change in the 802.11
122 * layer that should be propagated to the hardware but not
123 * require re-initialization of the 802.11 state machine (e.g
124 * rescanning for an ap). We always return ENETRESET which
125 * should cause the driver to re-initialize the device. Drivers
126 * can override this method to implement more optimized support.
129 ieee80211_default_reset(struct ifnet *ifp)
135 ieee80211_ifattach(struct ieee80211com *ic)
137 #define RATESDEFINED(m) \
138 ((ic->ic_modecaps & (1<<m)) && ic->ic_sup_rates[m].rs_nrates != 0)
139 struct ifnet *ifp = ic->ic_ifp;
140 struct ieee80211_channel *c;
143 ether_ifattach(ifp, ic->ic_myaddr);
144 ifp->if_output = ieee80211_output;
146 bpfattach2(ifp, DLT_IEEE802_11,
147 sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf);
149 ieee80211_crypto_attach(ic);
152 * Fill in 802.11 available channel set, mark
153 * all available channels as active, and pick
154 * a default channel if not already specified.
156 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
157 ic->ic_modecaps |= 1<<IEEE80211_MODE_AUTO;
158 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
159 c = &ic->ic_channels[i];
162 * Verify driver passed us valid data.
164 if (i != ieee80211_chan2ieee(ic, c)) {
165 if_printf(ifp, "bad channel ignored; "
166 "freq %u flags %x number %u\n",
167 c->ic_freq, c->ic_flags, i);
168 c->ic_flags = 0; /* NB: remove */
171 setbit(ic->ic_chan_avail, i);
173 * Identify mode capabilities.
175 if (IEEE80211_IS_CHAN_A(c))
176 ic->ic_modecaps |= 1<<IEEE80211_MODE_11A;
177 if (IEEE80211_IS_CHAN_B(c))
178 ic->ic_modecaps |= 1<<IEEE80211_MODE_11B;
179 if (IEEE80211_IS_CHAN_PUREG(c))
180 ic->ic_modecaps |= 1<<IEEE80211_MODE_11G;
181 if (IEEE80211_IS_CHAN_FHSS(c))
182 ic->ic_modecaps |= 1<<IEEE80211_MODE_FH;
183 if (IEEE80211_IS_CHAN_T(c))
184 ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO_A;
185 if (IEEE80211_IS_CHAN_108G(c))
186 ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO_G;
187 if (ic->ic_curchan == NULL) {
188 /* arbitrarily pick the first channel */
189 ic->ic_curchan = &ic->ic_channels[i];
193 /* validate ic->ic_curmode */
194 if ((ic->ic_modecaps & (1<<ic->ic_curmode)) == 0)
195 ic->ic_curmode = IEEE80211_MODE_AUTO;
196 ic->ic_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
198 /* fillin well-known rate sets if driver has not specified */
199 if (!RATESDEFINED(IEEE80211_MODE_11B))
200 ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_rateset_11b;
201 if (!RATESDEFINED(IEEE80211_MODE_11G))
202 ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_rateset_11g;
203 if (!RATESDEFINED(IEEE80211_MODE_11A))
204 ic->ic_sup_rates[IEEE80211_MODE_11A] = ieee80211_rateset_11a;
205 if (!RATESDEFINED(IEEE80211_MODE_TURBO_A))
206 ic->ic_sup_rates[IEEE80211_MODE_TURBO_A] = ieee80211_rateset_11a;
207 if (!RATESDEFINED(IEEE80211_MODE_TURBO_G))
208 ic->ic_sup_rates[IEEE80211_MODE_TURBO_G] = ieee80211_rateset_11g;
211 * Enable WME by default if we're capable.
213 if (ic->ic_caps & IEEE80211_C_WME)
214 ic->ic_flags |= IEEE80211_F_WME;
216 if (ic->ic_caps & IEEE80211_C_BURST)
217 ic->ic_flags |= IEEE80211_F_BURST;
218 (void) ieee80211_setmode(ic, ic->ic_curmode);
220 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
221 ic->ic_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
222 ic->ic_dtim_period = IEEE80211_DTIM_DEFAULT;
223 IEEE80211_BEACON_LOCK_INIT(ic, "beacon");
225 ic->ic_lintval = ic->ic_bintval;
226 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
228 ieee80211_node_attach(ic);
229 ieee80211_proto_attach(ic);
231 ieee80211_add_vap(ic);
233 ieee80211_sysctl_attach(ic); /* NB: requires ic_vap */
236 * Install a default reset method for the ioctl support.
237 * The driver is expected to fill this in before calling us.
239 if (ic->ic_reset == NULL)
240 ic->ic_reset = ieee80211_default_reset;
242 KASSERT(ifp->if_spare2 == NULL, ("oops, hosed"));
243 ifp->if_spare2 = ic; /* XXX temp backpointer */
248 ieee80211_ifdetach(struct ieee80211com *ic)
250 struct ifnet *ifp = ic->ic_ifp;
252 ieee80211_remove_vap(ic);
254 ieee80211_sysctl_detach(ic);
255 ieee80211_proto_detach(ic);
256 ieee80211_crypto_detach(ic);
257 ieee80211_node_detach(ic);
258 ifmedia_removeall(&ic->ic_media);
260 IEEE80211_BEACON_LOCK_DESTROY(ic);
267 * Convert MHz frequency to IEEE channel number.
270 ieee80211_mhz2ieee(u_int freq, u_int flags)
272 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
276 return ((int) freq - 2407) / 5;
278 return 15 + ((freq - 2512) / 20);
279 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
281 return (freq - 4000) / 5;
283 return (freq - 5000) / 5;
284 } else { /* either, guess */
288 return ((int) freq - 2407) / 5;
291 return (freq - 4000) / 5;
293 return 15 + ((freq - 2512) / 20);
295 return (freq - 5000) / 5;
300 * Convert channel to IEEE channel number.
303 ieee80211_chan2ieee(struct ieee80211com *ic, struct ieee80211_channel *c)
305 if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX])
306 return c - ic->ic_channels;
307 else if (c == IEEE80211_CHAN_ANYC)
308 return IEEE80211_CHAN_ANY;
309 else if (c != NULL) {
310 if_printf(ic->ic_ifp, "invalid channel freq %u flags %x\n",
311 c->ic_freq, c->ic_flags);
314 if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
320 * Convert IEEE channel number to MHz frequency.
323 ieee80211_ieee2mhz(u_int chan, u_int flags)
325 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
329 return 2407 + chan*5;
331 return 2512 + ((chan-15)*20);
332 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
333 return 5000 + (chan*5);
334 } else { /* either, guess */
337 if (chan < 14) /* 0-13 */
338 return 2407 + chan*5;
339 if (chan < 27) /* 15-26 */
340 return 2512 + ((chan-15)*20);
341 return 5000 + (chan*5);
346 * Setup the media data structures according to the channel and
347 * rate tables. This must be called by the driver after
348 * ieee80211_attach and before most anything else.
351 ieee80211_media_init(struct ieee80211com *ic,
352 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
354 #define ADD(_ic, _s, _o) \
355 ifmedia_add(&(_ic)->ic_media, \
356 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
357 struct ifnet *ifp = ic->ic_ifp;
358 struct ifmediareq imr;
359 int i, j, mode, rate, maxrate, mword, mopt, r;
360 struct ieee80211_rateset *rs;
361 struct ieee80211_rateset allrates;
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);
370 * Fill in media characteristics.
372 ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
374 memset(&allrates, 0, sizeof(allrates));
375 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) {
376 static const u_int mopts[] = {
382 IFM_IEEE80211_11A | IFM_IEEE80211_TURBO,
383 IFM_IEEE80211_11G | IFM_IEEE80211_TURBO,
385 if ((ic->ic_modecaps & (1<<mode)) == 0)
388 ADD(ic, IFM_AUTO, mopt); /* e.g. 11a auto */
389 if (ic->ic_caps & IEEE80211_C_IBSS)
390 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC);
391 if (ic->ic_caps & IEEE80211_C_HOSTAP)
392 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP);
393 if (ic->ic_caps & IEEE80211_C_AHDEMO)
394 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
395 if (ic->ic_caps & IEEE80211_C_MONITOR)
396 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR);
397 if (mode == IEEE80211_MODE_AUTO)
399 rs = &ic->ic_sup_rates[mode];
400 for (i = 0; i < rs->rs_nrates; i++) {
401 rate = rs->rs_rates[i];
402 mword = ieee80211_rate2media(ic, rate, mode);
405 ADD(ic, mword, mopt);
406 if (ic->ic_caps & IEEE80211_C_IBSS)
407 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
408 if (ic->ic_caps & IEEE80211_C_HOSTAP)
409 ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
410 if (ic->ic_caps & IEEE80211_C_AHDEMO)
411 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
412 if (ic->ic_caps & IEEE80211_C_MONITOR)
413 ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
415 * Add rate to the collection of all rates.
417 r = rate & IEEE80211_RATE_VAL;
418 for (j = 0; j < allrates.rs_nrates; j++)
419 if (allrates.rs_rates[j] == r)
421 if (j == allrates.rs_nrates) {
422 /* unique, add to the set */
423 allrates.rs_rates[j] = r;
424 allrates.rs_nrates++;
426 rate = (rate & IEEE80211_RATE_VAL) / 2;
431 for (i = 0; i < allrates.rs_nrates; i++) {
432 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
433 IEEE80211_MODE_AUTO);
436 mword = IFM_SUBTYPE(mword); /* remove media options */
438 if (ic->ic_caps & IEEE80211_C_IBSS)
439 ADD(ic, mword, IFM_IEEE80211_ADHOC);
440 if (ic->ic_caps & IEEE80211_C_HOSTAP)
441 ADD(ic, mword, IFM_IEEE80211_HOSTAP);
442 if (ic->ic_caps & IEEE80211_C_AHDEMO)
443 ADD(ic, mword, IFM_IEEE80211_ADHOC | IFM_FLAG0);
444 if (ic->ic_caps & IEEE80211_C_MONITOR)
445 ADD(ic, mword, IFM_IEEE80211_MONITOR);
447 ieee80211_media_status(ifp, &imr);
448 ifmedia_set(&ic->ic_media, imr.ifm_active);
451 ifp->if_baudrate = IF_Mbps(maxrate);
456 ieee80211_announce(struct ieee80211com *ic)
458 struct ifnet *ifp = ic->ic_ifp;
459 int i, mode, rate, mword;
460 struct ieee80211_rateset *rs;
462 for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
463 if ((ic->ic_modecaps & (1<<mode)) == 0)
465 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
466 rs = &ic->ic_sup_rates[mode];
467 for (i = 0; i < rs->rs_nrates; i++) {
468 rate = rs->rs_rates[i];
469 mword = ieee80211_rate2media(ic, rate, mode);
472 printf("%s%d%sMbps", (i != 0 ? " " : ""),
473 (rate & IEEE80211_RATE_VAL) / 2,
474 ((rate & 0x1) != 0 ? ".5" : ""));
481 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
483 #define IEEERATE(_ic,_m,_i) \
484 ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
485 int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
486 for (i = 0; i < nrates; i++)
487 if (IEEERATE(ic, mode, i) == rate)
494 * Find an instance by it's mac address.
496 struct ieee80211com *
497 ieee80211_find_vap(const u_int8_t mac[IEEE80211_ADDR_LEN])
499 struct ieee80211com *ic;
502 SLIST_FOREACH(ic, &ieee80211_list, ic_next)
503 if (IEEE80211_ADDR_EQ(mac, ic->ic_myaddr))
508 static struct ieee80211com *
509 ieee80211_find_instance(struct ifnet *ifp)
511 struct ieee80211com *ic;
514 /* XXX not right for multiple instances but works for now */
515 SLIST_FOREACH(ic, &ieee80211_list, ic_next)
516 if (ic->ic_ifp == ifp)
522 * Handle a media change request.
525 ieee80211_media_change(struct ifnet *ifp)
527 struct ieee80211com *ic;
528 struct ifmedia_entry *ime;
529 enum ieee80211_opmode newopmode;
530 enum ieee80211_phymode newphymode;
531 int i, j, newrate, error = 0;
533 ic = ieee80211_find_instance(ifp);
535 if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
538 ime = ic->ic_media.ifm_cur;
540 * First, identify the phy mode.
542 switch (IFM_MODE(ime->ifm_media)) {
543 case IFM_IEEE80211_11A:
544 newphymode = IEEE80211_MODE_11A;
546 case IFM_IEEE80211_11B:
547 newphymode = IEEE80211_MODE_11B;
549 case IFM_IEEE80211_11G:
550 newphymode = IEEE80211_MODE_11G;
552 case IFM_IEEE80211_FH:
553 newphymode = IEEE80211_MODE_FH;
556 newphymode = IEEE80211_MODE_AUTO;
562 * Turbo mode is an ``option''.
563 * XXX does not apply to AUTO
565 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
566 if (newphymode == IEEE80211_MODE_11A)
567 newphymode = IEEE80211_MODE_TURBO_A;
568 else if (newphymode == IEEE80211_MODE_11G)
569 newphymode = IEEE80211_MODE_TURBO_G;
574 * Validate requested mode is available.
576 if ((ic->ic_modecaps & (1<<newphymode)) == 0)
580 * Next, the fixed/variable rate.
583 if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
585 * Convert media subtype to rate.
587 newrate = ieee80211_media2rate(ime->ifm_media);
591 * Check the rate table for the specified/current phy.
593 if (newphymode == IEEE80211_MODE_AUTO) {
595 * In autoselect mode search for the rate.
597 for (j = IEEE80211_MODE_11A;
598 j < IEEE80211_MODE_MAX; j++) {
599 if ((ic->ic_modecaps & (1<<j)) == 0)
601 i = findrate(ic, j, newrate);
609 i = findrate(ic, newphymode, newrate);
611 if (i == -1) /* mode/rate mismatch */
614 /* NB: defer rate setting to later */
617 * Deduce new operating mode but don't install it just yet.
619 if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) ==
620 (IFM_IEEE80211_ADHOC|IFM_FLAG0))
621 newopmode = IEEE80211_M_AHDEMO;
622 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
623 newopmode = IEEE80211_M_HOSTAP;
624 else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
625 newopmode = IEEE80211_M_IBSS;
626 else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
627 newopmode = IEEE80211_M_MONITOR;
629 newopmode = IEEE80211_M_STA;
632 * Autoselect doesn't make sense when operating as an AP.
633 * If no phy mode has been selected, pick one and lock it
634 * down so rate tables can be used in forming beacon frames
637 if (newopmode == IEEE80211_M_HOSTAP &&
638 newphymode == IEEE80211_MODE_AUTO) {
639 for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++)
640 if (ic->ic_modecaps & (1<<j)) {
647 * Handle phy mode change.
649 if (ic->ic_curmode != newphymode) { /* change phy mode */
650 error = ieee80211_setmode(ic, newphymode);
657 * Committed to changes, install the rate setting.
659 if (ic->ic_fixed_rate != i) {
660 ic->ic_fixed_rate = i; /* set fixed tx rate */
665 * Handle operating mode change.
667 if (ic->ic_opmode != newopmode) {
668 ic->ic_opmode = newopmode;
670 case IEEE80211_M_AHDEMO:
671 case IEEE80211_M_HOSTAP:
672 case IEEE80211_M_STA:
673 case IEEE80211_M_MONITOR:
674 ic->ic_flags &= ~IEEE80211_F_IBSSON;
676 case IEEE80211_M_IBSS:
677 ic->ic_flags |= IEEE80211_F_IBSSON;
681 * Yech, slot time may change depending on the
682 * operating mode so reset it to be sure everything
683 * is setup appropriately.
685 ieee80211_reset_erp(ic);
686 ieee80211_wme_initparams(ic); /* after opmode change */
691 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
697 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
699 struct ieee80211com *ic;
700 struct ieee80211_rateset *rs;
702 ic = ieee80211_find_instance(ifp);
704 if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
707 imr->ifm_status = IFM_AVALID;
708 imr->ifm_active = IFM_IEEE80211;
709 if (ic->ic_state == IEEE80211_S_RUN)
710 imr->ifm_status |= IFM_ACTIVE;
712 * Calculate a current rate if possible.
714 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
716 * A fixed rate is set, report that.
718 rs = &ic->ic_sup_rates[ic->ic_curmode];
719 imr->ifm_active |= ieee80211_rate2media(ic,
720 rs->rs_rates[ic->ic_fixed_rate], ic->ic_curmode);
721 } else if (ic->ic_opmode == IEEE80211_M_STA) {
723 * In station mode report the current transmit rate.
725 rs = &ic->ic_bss->ni_rates;
726 imr->ifm_active |= ieee80211_rate2media(ic,
727 rs->rs_rates[ic->ic_bss->ni_txrate], ic->ic_curmode);
729 imr->ifm_active |= IFM_AUTO;
730 switch (ic->ic_opmode) {
731 case IEEE80211_M_STA:
733 case IEEE80211_M_IBSS:
734 imr->ifm_active |= IFM_IEEE80211_ADHOC;
736 case IEEE80211_M_AHDEMO:
737 /* should not come here */
739 case IEEE80211_M_HOSTAP:
740 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
742 case IEEE80211_M_MONITOR:
743 imr->ifm_active |= IFM_IEEE80211_MONITOR;
746 switch (ic->ic_curmode) {
747 case IEEE80211_MODE_11A:
748 imr->ifm_active |= IFM_IEEE80211_11A;
750 case IEEE80211_MODE_11B:
751 imr->ifm_active |= IFM_IEEE80211_11B;
753 case IEEE80211_MODE_11G:
754 imr->ifm_active |= IFM_IEEE80211_11G;
756 case IEEE80211_MODE_FH:
757 imr->ifm_active |= IFM_IEEE80211_FH;
759 case IEEE80211_MODE_TURBO_A:
760 imr->ifm_active |= IFM_IEEE80211_11A
761 | IFM_IEEE80211_TURBO;
763 case IEEE80211_MODE_TURBO_G:
764 imr->ifm_active |= IFM_IEEE80211_11G
765 | IFM_IEEE80211_TURBO;
771 ieee80211_watchdog(struct ieee80211com *ic)
773 struct ieee80211_node_table *nt;
774 int need_inact_timer = 0;
776 if (ic->ic_state != IEEE80211_S_INIT) {
777 if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0)
778 ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
780 if (nt->nt_inact_timer) {
781 if (--nt->nt_inact_timer == 0)
783 need_inact_timer += nt->nt_inact_timer;
786 if (nt->nt_inact_timer) {
787 if (--nt->nt_inact_timer == 0)
789 need_inact_timer += nt->nt_inact_timer;
792 if (ic->ic_mgt_timer != 0 || need_inact_timer)
793 ic->ic_ifp->if_timer = 1;
797 * Set the current phy mode and recalculate the active channel
798 * set based on the available channels for this mode. Also
799 * select a new default/current channel if the current one is
800 * inappropriate for this mode.
803 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
805 #define N(a) (sizeof(a) / sizeof(a[0]))
806 static const u_int chanflags[] = {
807 0, /* IEEE80211_MODE_AUTO */
808 IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */
809 IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */
810 IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */
811 IEEE80211_CHAN_FHSS, /* IEEE80211_MODE_FH */
812 IEEE80211_CHAN_T, /* IEEE80211_MODE_TURBO_A */
813 IEEE80211_CHAN_108G, /* IEEE80211_MODE_TURBO_G */
815 struct ieee80211_channel *c;
819 /* validate new mode */
820 if ((ic->ic_modecaps & (1<<mode)) == 0) {
821 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
822 "%s: mode %u not supported (caps 0x%x)\n",
823 __func__, mode, ic->ic_modecaps);
828 * Verify at least one channel is present in the available
829 * channel list before committing to the new mode.
831 KASSERT(mode < N(chanflags), ("Unexpected mode %u", mode));
832 modeflags = chanflags[mode];
833 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
834 c = &ic->ic_channels[i];
835 if (c->ic_flags == 0)
837 if (mode == IEEE80211_MODE_AUTO) {
838 /* ignore static turbo channels for autoselect */
839 if (!IEEE80211_IS_CHAN_T(c))
842 if ((c->ic_flags & modeflags) == modeflags)
846 if (i > IEEE80211_CHAN_MAX) {
847 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
848 "%s: no channels found for mode %u\n", __func__, mode);
853 * Calculate the active channel set.
855 memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
856 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
857 c = &ic->ic_channels[i];
858 if (c->ic_flags == 0)
860 if (mode == IEEE80211_MODE_AUTO) {
861 /* take anything but static turbo channels */
862 if (!IEEE80211_IS_CHAN_T(c))
863 setbit(ic->ic_chan_active, i);
865 if ((c->ic_flags & modeflags) == modeflags)
866 setbit(ic->ic_chan_active, i);
870 * If no current/default channel is setup or the current
871 * channel is wrong for the mode then pick the first
872 * available channel from the active list. This is likely
875 if (ic->ic_ibss_chan == NULL ||
876 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
877 for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
878 if (isset(ic->ic_chan_active, i)) {
879 ic->ic_ibss_chan = &ic->ic_channels[i];
882 KASSERT(ic->ic_ibss_chan != NULL &&
883 isset(ic->ic_chan_active,
884 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)),
885 ("Bad IBSS channel %u",
886 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)));
889 * If the desired channel is set but no longer valid then reset it.
891 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
892 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_des_chan)))
893 ic->ic_des_chan = IEEE80211_CHAN_ANYC;
896 * Do mode-specific rate setup.
898 if (mode == IEEE80211_MODE_11G) {
900 * Use a mixed 11b/11g rate set.
902 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode],
904 } else if (mode == IEEE80211_MODE_11B) {
906 * Force pure 11b rate set.
908 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode],
912 * Setup an initial rate set according to the
913 * current/default channel selected above. This
914 * will be changed when scanning but must exist
915 * now so driver have a consistent state of ic_ibss_chan.
917 if (ic->ic_bss) /* NB: can be called before lateattach */
918 ic->ic_bss->ni_rates = ic->ic_sup_rates[mode];
920 ic->ic_curmode = mode;
921 ieee80211_reset_erp(ic); /* reset ERP state */
922 ieee80211_wme_initparams(ic); /* reset WME stat */
929 * Return the phy mode for with the specified channel so the
930 * caller can select a rate set. This is problematic for channels
931 * where multiple operating modes are possible (e.g. 11g+11b).
932 * In those cases we defer to the current operating mode when set.
934 enum ieee80211_phymode
935 ieee80211_chan2mode(struct ieee80211com *ic, struct ieee80211_channel *chan)
937 if (IEEE80211_IS_CHAN_T(chan)) {
938 return IEEE80211_MODE_TURBO_A;
939 } else if (IEEE80211_IS_CHAN_5GHZ(chan)) {
940 return IEEE80211_MODE_11A;
941 } else if (IEEE80211_IS_CHAN_FHSS(chan))
942 return IEEE80211_MODE_FH;
943 else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN)) {
945 * This assumes all 11g channels are also usable
946 * for 11b, which is currently true.
948 if (ic->ic_curmode == IEEE80211_MODE_TURBO_G)
949 return IEEE80211_MODE_TURBO_G;
950 if (ic->ic_curmode == IEEE80211_MODE_11B)
951 return IEEE80211_MODE_11B;
952 return IEEE80211_MODE_11G;
954 return IEEE80211_MODE_11B;
958 * convert IEEE80211 rate value to ifmedia subtype.
959 * ieee80211 rate is in unit of 0.5Mbps.
962 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
964 #define N(a) (sizeof(a) / sizeof(a[0]))
965 static const struct {
966 u_int m; /* rate + mode */
967 u_int r; /* if_media rate */
969 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
970 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
971 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
972 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
973 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
974 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
975 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
976 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
977 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
978 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
979 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
980 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
981 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
982 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
983 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
984 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
985 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
986 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
987 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
988 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
989 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
990 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
991 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
992 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
993 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
994 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
995 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
996 /* NB: OFDM72 doesn't realy exist so we don't handle it */
1000 mask = rate & IEEE80211_RATE_VAL;
1002 case IEEE80211_MODE_11A:
1003 case IEEE80211_MODE_TURBO_A:
1004 mask |= IFM_IEEE80211_11A;
1006 case IEEE80211_MODE_11B:
1007 mask |= IFM_IEEE80211_11B;
1009 case IEEE80211_MODE_FH:
1010 mask |= IFM_IEEE80211_FH;
1012 case IEEE80211_MODE_AUTO:
1013 /* NB: ic may be NULL for some drivers */
1014 if (ic && ic->ic_phytype == IEEE80211_T_FH) {
1015 mask |= IFM_IEEE80211_FH;
1018 /* NB: hack, 11g matches both 11b+11a rates */
1020 case IEEE80211_MODE_11G:
1021 case IEEE80211_MODE_TURBO_G:
1022 mask |= IFM_IEEE80211_11G;
1025 for (i = 0; i < N(rates); i++)
1026 if (rates[i].m == mask)
1033 ieee80211_media2rate(int mword)
1035 #define N(a) (sizeof(a) / sizeof(a[0]))
1036 static const int ieeerates[] = {
1040 2, /* IFM_IEEE80211_FH1 */
1041 4, /* IFM_IEEE80211_FH2 */
1042 2, /* IFM_IEEE80211_DS1 */
1043 4, /* IFM_IEEE80211_DS2 */
1044 11, /* IFM_IEEE80211_DS5 */
1045 22, /* IFM_IEEE80211_DS11 */
1046 44, /* IFM_IEEE80211_DS22 */
1047 12, /* IFM_IEEE80211_OFDM6 */
1048 18, /* IFM_IEEE80211_OFDM9 */
1049 24, /* IFM_IEEE80211_OFDM12 */
1050 36, /* IFM_IEEE80211_OFDM18 */
1051 48, /* IFM_IEEE80211_OFDM24 */
1052 72, /* IFM_IEEE80211_OFDM36 */
1053 96, /* IFM_IEEE80211_OFDM48 */
1054 108, /* IFM_IEEE80211_OFDM54 */
1055 144, /* IFM_IEEE80211_OFDM72 */
1057 return IFM_SUBTYPE(mword) < N(ieeerates) ?
1058 ieeerates[IFM_SUBTYPE(mword)] : 0;