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_half =
71 { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
72 static const struct ieee80211_rateset ieee80211_rateset_quarter =
73 { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
74 static const struct ieee80211_rateset ieee80211_rateset_11b =
75 { 4, { B(2), B(4), B(11), B(22) } };
76 /* NB: OFDM rates are handled specially based on mode */
77 static const struct ieee80211_rateset ieee80211_rateset_11g =
78 { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
81 /* list of all instances */
82 SLIST_HEAD(ieee80211_list, ieee80211com);
83 static struct ieee80211_list ieee80211_list =
84 SLIST_HEAD_INITIALIZER(ieee80211_list);
85 static u_int8_t ieee80211_vapmap[32]; /* enough for 256 */
86 static struct mtx ieee80211_vap_mtx;
87 MTX_SYSINIT(ieee80211, &ieee80211_vap_mtx, "net80211 instances", MTX_DEF);
90 ieee80211_add_vap(struct ieee80211com *ic)
92 #define N(a) (sizeof(a)/sizeof(a[0]))
96 mtx_lock(&ieee80211_vap_mtx);
98 for (i = 0; i < N(ieee80211_vapmap) && ieee80211_vapmap[i] == 0xff; i++)
100 if (i == N(ieee80211_vapmap))
101 panic("vap table full");
102 for (b = ieee80211_vapmap[i]; b & 1; b >>= 1)
104 setbit(ieee80211_vapmap, ic->ic_vap);
105 SLIST_INSERT_HEAD(&ieee80211_list, ic, ic_next);
106 mtx_unlock(&ieee80211_vap_mtx);
111 ieee80211_remove_vap(struct ieee80211com *ic)
113 mtx_lock(&ieee80211_vap_mtx);
114 SLIST_REMOVE(&ieee80211_list, ic, ieee80211com, ic_next);
115 KASSERT(ic->ic_vap < sizeof(ieee80211_vapmap)*NBBY,
116 ("invalid vap id %d", ic->ic_vap));
117 KASSERT(isset(ieee80211_vapmap, ic->ic_vap),
118 ("vap id %d not allocated", ic->ic_vap));
119 clrbit(ieee80211_vapmap, ic->ic_vap);
120 mtx_unlock(&ieee80211_vap_mtx);
124 * Default reset method for use with the ioctl support. This
125 * method is invoked after any state change in the 802.11
126 * layer that should be propagated to the hardware but not
127 * require re-initialization of the 802.11 state machine (e.g
128 * rescanning for an ap). We always return ENETRESET which
129 * should cause the driver to re-initialize the device. Drivers
130 * can override this method to implement more optimized support.
133 ieee80211_default_reset(struct ifnet *ifp)
139 * Fill in 802.11 available channel set, mark
140 * all available channels as active, and pick
141 * a default channel if not already specified.
144 ieee80211_chan_init(struct ieee80211com *ic)
146 #define DEFAULTRATES(m, def) do { \
147 if ((ic->ic_modecaps & (1<<m)) && ic->ic_sup_rates[m].rs_nrates == 0) \
148 ic->ic_sup_rates[m] = def; \
150 struct ifnet *ifp = ic->ic_ifp;
151 struct ieee80211_channel *c;
154 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
155 ic->ic_modecaps = 1<<IEEE80211_MODE_AUTO;
156 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
157 c = &ic->ic_channels[i];
160 * Verify driver passed us valid data.
162 if (i != ieee80211_chan2ieee(ic, c)) {
163 if_printf(ifp, "bad channel ignored; "
164 "freq %u flags %x number %u\n",
165 c->ic_freq, c->ic_flags, i);
166 c->ic_flags = 0; /* NB: remove */
169 setbit(ic->ic_chan_avail, i);
171 * Identify mode capabilities.
173 if (IEEE80211_IS_CHAN_A(c))
174 ic->ic_modecaps |= 1<<IEEE80211_MODE_11A;
175 if (IEEE80211_IS_CHAN_B(c))
176 ic->ic_modecaps |= 1<<IEEE80211_MODE_11B;
177 if (IEEE80211_IS_CHAN_ANYG(c))
178 ic->ic_modecaps |= 1<<IEEE80211_MODE_11G;
179 if (IEEE80211_IS_CHAN_FHSS(c))
180 ic->ic_modecaps |= 1<<IEEE80211_MODE_FH;
181 if (IEEE80211_IS_CHAN_T(c))
182 ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO_A;
183 if (IEEE80211_IS_CHAN_108G(c))
184 ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO_G;
185 if (ic->ic_curchan == NULL) {
186 /* arbitrarily pick the first channel */
187 ic->ic_curchan = &ic->ic_channels[i];
192 /* fillin well-known rate sets if driver has not specified */
193 DEFAULTRATES(IEEE80211_MODE_11B, ieee80211_rateset_11b);
194 DEFAULTRATES(IEEE80211_MODE_11G, ieee80211_rateset_11g);
195 DEFAULTRATES(IEEE80211_MODE_11A, ieee80211_rateset_11a);
196 DEFAULTRATES(IEEE80211_MODE_TURBO_A, ieee80211_rateset_11a);
197 DEFAULTRATES(IEEE80211_MODE_TURBO_G, ieee80211_rateset_11g);
200 * Set auto mode to reset active channel state and any desired channel.
202 (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
207 ieee80211_ifattach(struct ieee80211com *ic)
209 struct ifnet *ifp = ic->ic_ifp;
211 ether_ifattach(ifp, ic->ic_myaddr);
212 ifp->if_output = ieee80211_output;
214 bpfattach2(ifp, DLT_IEEE802_11,
215 sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf);
217 ieee80211_crypto_attach(ic);
219 ic->ic_des_chan = IEEE80211_CHAN_ANYC;
221 * Fill in 802.11 available channel set, mark all
222 * available channels as active, and pick a default
223 * channel if not already specified.
225 ieee80211_chan_init(ic);
228 * Enable WME by default if we're capable.
230 if (ic->ic_caps & IEEE80211_C_WME)
231 ic->ic_flags |= IEEE80211_F_WME;
233 if (ic->ic_caps & IEEE80211_C_BURST)
234 ic->ic_flags |= IEEE80211_F_BURST;
236 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
237 ic->ic_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
238 ic->ic_dtim_period = IEEE80211_DTIM_DEFAULT;
239 IEEE80211_BEACON_LOCK_INIT(ic, "beacon");
241 ic->ic_lintval = ic->ic_bintval;
242 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
244 ieee80211_node_attach(ic);
245 ieee80211_proto_attach(ic);
247 ieee80211_add_vap(ic);
249 ieee80211_sysctl_attach(ic); /* NB: requires ic_vap */
252 * Install a default reset method for the ioctl support.
253 * The driver is expected to fill this in before calling us.
255 if (ic->ic_reset == NULL)
256 ic->ic_reset = ieee80211_default_reset;
258 KASSERT(ifp->if_spare2 == NULL, ("oops, hosed"));
259 ifp->if_spare2 = ic; /* XXX temp backpointer */
263 ieee80211_ifdetach(struct ieee80211com *ic)
265 struct ifnet *ifp = ic->ic_ifp;
267 ieee80211_remove_vap(ic);
269 ieee80211_sysctl_detach(ic);
270 ieee80211_proto_detach(ic);
271 ieee80211_crypto_detach(ic);
272 ieee80211_node_detach(ic);
273 ifmedia_removeall(&ic->ic_media);
275 IEEE80211_BEACON_LOCK_DESTROY(ic);
282 * Convert MHz frequency to IEEE channel number.
285 ieee80211_mhz2ieee(u_int freq, u_int flags)
287 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
291 return ((int) freq - 2407) / 5;
293 return 15 + ((freq - 2512) / 20);
294 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
296 if (flags &(IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER))
297 return 37 + ((freq * 10) +
298 ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
299 return (freq - 4000) / 5;
301 return (freq - 5000) / 5;
302 } else { /* either, guess */
306 return ((int) freq - 2407) / 5;
308 if (flags &(IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER))
309 return 37 + ((freq * 10) +
310 ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
311 else if (freq > 4900)
312 return (freq - 4000) / 5;
314 return 15 + ((freq - 2512) / 20);
316 return (freq - 5000) / 5;
321 * Convert channel to IEEE channel number.
324 ieee80211_chan2ieee(struct ieee80211com *ic, struct ieee80211_channel *c)
326 if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX])
327 return c - ic->ic_channels;
328 else if (c == IEEE80211_CHAN_ANYC)
329 return IEEE80211_CHAN_ANY;
330 else if (c != NULL) {
331 if_printf(ic->ic_ifp, "invalid channel freq %u flags %x\n",
332 c->ic_freq, c->ic_flags);
335 if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
341 * Convert IEEE channel number to MHz frequency.
344 ieee80211_ieee2mhz(u_int chan, u_int flags)
346 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
350 return 2407 + chan*5;
352 return 2512 + ((chan-15)*20);
353 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
354 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
356 return 4940 + chan*5 + (chan % 5 ? 2 : 0);
358 return 5000 + (chan*5);
359 } else { /* either, guess */
362 if (chan < 14) /* 0-13 */
363 return 2407 + chan*5;
364 if (chan < 27) /* 15-26 */
365 return 2512 + ((chan-15)*20);
366 /* XXX can't distinguish PSB channels */
367 return 5000 + (chan*5);
372 * Setup the media data structures according to the channel and
373 * rate tables. This must be called by the driver after
374 * ieee80211_attach and before most anything else.
377 ieee80211_media_init(struct ieee80211com *ic,
378 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
380 #define ADD(_ic, _s, _o) \
381 ifmedia_add(&(_ic)->ic_media, \
382 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
383 struct ifnet *ifp = ic->ic_ifp;
384 struct ifmediareq imr;
385 int i, j, mode, rate, maxrate, mword, mopt, r;
386 struct ieee80211_rateset *rs;
387 struct ieee80211_rateset allrates;
389 /* NB: this works because the structure is initialized to zero */
390 if (LIST_EMPTY(&ic->ic_media.ifm_list)) {
392 * Do late attach work that must wait for any subclass
393 * (i.e. driver) work such as overriding methods.
395 ieee80211_node_lateattach(ic);
398 * We are re-initializing the channel list; clear
399 * the existing media state as the media routines
400 * don't suppress duplicates.
402 ifmedia_removeall(&ic->ic_media);
403 ieee80211_chan_init(ic);
407 * Fill in media characteristics.
409 ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
411 memset(&allrates, 0, sizeof(allrates));
412 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) {
413 static const u_int mopts[] = {
419 IFM_IEEE80211_11A | IFM_IEEE80211_TURBO,
420 IFM_IEEE80211_11G | IFM_IEEE80211_TURBO,
422 if ((ic->ic_modecaps & (1<<mode)) == 0)
425 ADD(ic, IFM_AUTO, mopt); /* e.g. 11a auto */
426 if (ic->ic_caps & IEEE80211_C_IBSS)
427 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC);
428 if (ic->ic_caps & IEEE80211_C_HOSTAP)
429 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP);
430 if (ic->ic_caps & IEEE80211_C_AHDEMO)
431 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
432 if (ic->ic_caps & IEEE80211_C_MONITOR)
433 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR);
434 if (mode == IEEE80211_MODE_AUTO)
436 rs = &ic->ic_sup_rates[mode];
437 for (i = 0; i < rs->rs_nrates; i++) {
438 rate = rs->rs_rates[i];
439 mword = ieee80211_rate2media(ic, rate, mode);
442 ADD(ic, mword, mopt);
443 if (ic->ic_caps & IEEE80211_C_IBSS)
444 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
445 if (ic->ic_caps & IEEE80211_C_HOSTAP)
446 ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
447 if (ic->ic_caps & IEEE80211_C_AHDEMO)
448 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
449 if (ic->ic_caps & IEEE80211_C_MONITOR)
450 ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
452 * Add rate to the collection of all rates.
454 r = rate & IEEE80211_RATE_VAL;
455 for (j = 0; j < allrates.rs_nrates; j++)
456 if (allrates.rs_rates[j] == r)
458 if (j == allrates.rs_nrates) {
459 /* unique, add to the set */
460 allrates.rs_rates[j] = r;
461 allrates.rs_nrates++;
463 rate = (rate & IEEE80211_RATE_VAL) / 2;
468 for (i = 0; i < allrates.rs_nrates; i++) {
469 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
470 IEEE80211_MODE_AUTO);
473 mword = IFM_SUBTYPE(mword); /* remove media options */
475 if (ic->ic_caps & IEEE80211_C_IBSS)
476 ADD(ic, mword, IFM_IEEE80211_ADHOC);
477 if (ic->ic_caps & IEEE80211_C_HOSTAP)
478 ADD(ic, mword, IFM_IEEE80211_HOSTAP);
479 if (ic->ic_caps & IEEE80211_C_AHDEMO)
480 ADD(ic, mword, IFM_IEEE80211_ADHOC | IFM_FLAG0);
481 if (ic->ic_caps & IEEE80211_C_MONITOR)
482 ADD(ic, mword, IFM_IEEE80211_MONITOR);
484 ieee80211_media_status(ifp, &imr);
485 ifmedia_set(&ic->ic_media, imr.ifm_active);
488 ifp->if_baudrate = IF_Mbps(maxrate);
492 const struct ieee80211_rateset *
493 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
495 enum ieee80211_phymode mode = ieee80211_chan2mode(ic, c);
497 if (mode == IEEE80211_MODE_11A) {
498 if (IEEE80211_IS_CHAN_HALF(c))
499 return &ieee80211_rateset_half;
500 if (IEEE80211_IS_CHAN_QUARTER(c))
501 return &ieee80211_rateset_quarter;
503 return &ic->ic_sup_rates[mode];
507 ieee80211_announce(struct ieee80211com *ic)
509 struct ifnet *ifp = ic->ic_ifp;
510 int i, mode, rate, mword;
511 struct ieee80211_rateset *rs;
513 for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
514 if ((ic->ic_modecaps & (1<<mode)) == 0)
516 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
517 rs = &ic->ic_sup_rates[mode];
518 for (i = 0; i < rs->rs_nrates; i++) {
519 rate = rs->rs_rates[i];
520 mword = ieee80211_rate2media(ic, rate, mode);
523 printf("%s%d%sMbps", (i != 0 ? " " : ""),
524 (rate & IEEE80211_RATE_VAL) / 2,
525 ((rate & 0x1) != 0 ? ".5" : ""));
532 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
534 #define IEEERATE(_ic,_m,_i) \
535 ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
536 int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
537 for (i = 0; i < nrates; i++)
538 if (IEEERATE(ic, mode, i) == rate)
545 * Find an instance by it's mac address.
547 struct ieee80211com *
548 ieee80211_find_vap(const u_int8_t mac[IEEE80211_ADDR_LEN])
550 struct ieee80211com *ic;
553 SLIST_FOREACH(ic, &ieee80211_list, ic_next)
554 if (IEEE80211_ADDR_EQ(mac, ic->ic_myaddr))
559 static struct ieee80211com *
560 ieee80211_find_instance(struct ifnet *ifp)
562 struct ieee80211com *ic;
565 /* XXX not right for multiple instances but works for now */
566 SLIST_FOREACH(ic, &ieee80211_list, ic_next)
567 if (ic->ic_ifp == ifp)
573 * Handle a media change request.
576 ieee80211_media_change(struct ifnet *ifp)
578 struct ieee80211com *ic;
579 struct ifmedia_entry *ime;
580 enum ieee80211_opmode newopmode;
581 enum ieee80211_phymode newphymode;
582 int i, j, newrate, error = 0;
584 ic = ieee80211_find_instance(ifp);
586 if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
589 ime = ic->ic_media.ifm_cur;
591 * First, identify the phy mode.
593 switch (IFM_MODE(ime->ifm_media)) {
594 case IFM_IEEE80211_11A:
595 newphymode = IEEE80211_MODE_11A;
597 case IFM_IEEE80211_11B:
598 newphymode = IEEE80211_MODE_11B;
600 case IFM_IEEE80211_11G:
601 newphymode = IEEE80211_MODE_11G;
603 case IFM_IEEE80211_FH:
604 newphymode = IEEE80211_MODE_FH;
607 newphymode = IEEE80211_MODE_AUTO;
613 * Turbo mode is an ``option''.
614 * XXX does not apply to AUTO
616 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
617 if (newphymode == IEEE80211_MODE_11A)
618 newphymode = IEEE80211_MODE_TURBO_A;
619 else if (newphymode == IEEE80211_MODE_11G)
620 newphymode = IEEE80211_MODE_TURBO_G;
625 * Validate requested mode is available.
627 if ((ic->ic_modecaps & (1<<newphymode)) == 0)
631 * Next, the fixed/variable rate.
634 if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
636 * Convert media subtype to rate.
638 newrate = ieee80211_media2rate(ime->ifm_media);
642 * Check the rate table for the specified/current phy.
644 if (newphymode == IEEE80211_MODE_AUTO) {
646 * In autoselect mode search for the rate.
648 for (j = IEEE80211_MODE_11A;
649 j < IEEE80211_MODE_MAX; j++) {
650 if ((ic->ic_modecaps & (1<<j)) == 0)
652 i = findrate(ic, j, newrate);
660 i = findrate(ic, newphymode, newrate);
662 if (i == -1) /* mode/rate mismatch */
665 /* NB: defer rate setting to later */
668 * Deduce new operating mode but don't install it just yet.
670 if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) ==
671 (IFM_IEEE80211_ADHOC|IFM_FLAG0))
672 newopmode = IEEE80211_M_AHDEMO;
673 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
674 newopmode = IEEE80211_M_HOSTAP;
675 else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
676 newopmode = IEEE80211_M_IBSS;
677 else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
678 newopmode = IEEE80211_M_MONITOR;
680 newopmode = IEEE80211_M_STA;
683 * Autoselect doesn't make sense when operating as an AP.
684 * If no phy mode has been selected, pick one and lock it
685 * down so rate tables can be used in forming beacon frames
688 if (newopmode == IEEE80211_M_HOSTAP &&
689 newphymode == IEEE80211_MODE_AUTO) {
690 for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++)
691 if (ic->ic_modecaps & (1<<j)) {
698 * Handle phy mode change.
700 if (ic->ic_curmode != newphymode) { /* change phy mode */
701 error = ieee80211_setmode(ic, newphymode);
708 * Committed to changes, install the rate setting.
710 if (ic->ic_fixed_rate != i) {
711 ic->ic_fixed_rate = i; /* set fixed tx rate */
716 * Handle operating mode change.
718 if (ic->ic_opmode != newopmode) {
719 ic->ic_opmode = newopmode;
721 case IEEE80211_M_AHDEMO:
722 case IEEE80211_M_HOSTAP:
723 case IEEE80211_M_STA:
724 case IEEE80211_M_MONITOR:
725 ic->ic_flags &= ~IEEE80211_F_IBSSON;
727 case IEEE80211_M_IBSS:
728 ic->ic_flags |= IEEE80211_F_IBSSON;
732 * Yech, slot time may change depending on the
733 * operating mode so reset it to be sure everything
734 * is setup appropriately.
736 ieee80211_reset_erp(ic);
737 ieee80211_wme_initparams(ic); /* after opmode change */
742 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
748 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
750 struct ieee80211com *ic;
751 const struct ieee80211_rateset *rs;
753 ic = ieee80211_find_instance(ifp);
755 if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
758 imr->ifm_status = IFM_AVALID;
759 imr->ifm_active = IFM_IEEE80211;
760 if (ic->ic_state == IEEE80211_S_RUN)
761 imr->ifm_status |= IFM_ACTIVE;
763 * Calculate a current rate if possible.
765 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
767 * A fixed rate is set, report that.
769 rs = &ic->ic_sup_rates[ic->ic_curmode];
770 imr->ifm_active |= ieee80211_rate2media(ic,
771 rs->rs_rates[ic->ic_fixed_rate], ic->ic_curmode);
772 } else if (ic->ic_opmode == IEEE80211_M_STA) {
774 * In station mode report the current transmit rate.
776 rs = &ic->ic_bss->ni_rates;
777 imr->ifm_active |= ieee80211_rate2media(ic,
778 rs->rs_rates[ic->ic_bss->ni_txrate], ic->ic_curmode);
780 imr->ifm_active |= IFM_AUTO;
781 switch (ic->ic_opmode) {
782 case IEEE80211_M_STA:
784 case IEEE80211_M_IBSS:
785 imr->ifm_active |= IFM_IEEE80211_ADHOC;
787 case IEEE80211_M_AHDEMO:
788 /* should not come here */
790 case IEEE80211_M_HOSTAP:
791 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
793 case IEEE80211_M_MONITOR:
794 imr->ifm_active |= IFM_IEEE80211_MONITOR;
797 switch (ic->ic_curmode) {
798 case IEEE80211_MODE_11A:
799 imr->ifm_active |= IFM_IEEE80211_11A;
801 case IEEE80211_MODE_11B:
802 imr->ifm_active |= IFM_IEEE80211_11B;
804 case IEEE80211_MODE_11G:
805 imr->ifm_active |= IFM_IEEE80211_11G;
807 case IEEE80211_MODE_FH:
808 imr->ifm_active |= IFM_IEEE80211_FH;
810 case IEEE80211_MODE_TURBO_A:
811 imr->ifm_active |= IFM_IEEE80211_11A
812 | IFM_IEEE80211_TURBO;
814 case IEEE80211_MODE_TURBO_G:
815 imr->ifm_active |= IFM_IEEE80211_11G
816 | IFM_IEEE80211_TURBO;
822 ieee80211_watchdog(struct ieee80211com *ic)
824 struct ieee80211_node_table *nt;
825 int need_inact_timer = 0;
827 if (ic->ic_state != IEEE80211_S_INIT) {
828 if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0)
829 ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
831 if (nt->nt_inact_timer) {
832 if (--nt->nt_inact_timer == 0)
834 need_inact_timer += nt->nt_inact_timer;
837 if (nt->nt_inact_timer) {
838 if (--nt->nt_inact_timer == 0)
840 need_inact_timer += nt->nt_inact_timer;
843 if (ic->ic_mgt_timer != 0 || need_inact_timer)
844 ic->ic_ifp->if_timer = 1;
848 * Set the current phy mode and recalculate the active channel
849 * set based on the available channels for this mode. Also
850 * select a new default/current channel if the current one is
851 * inappropriate for this mode.
854 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
856 #define N(a) (sizeof(a) / sizeof(a[0]))
857 static const u_int chanflags[] = {
858 0, /* IEEE80211_MODE_AUTO */
859 IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */
860 IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */
861 IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */
862 IEEE80211_CHAN_FHSS, /* IEEE80211_MODE_FH */
863 IEEE80211_CHAN_T, /* IEEE80211_MODE_TURBO_A */
864 IEEE80211_CHAN_108G, /* IEEE80211_MODE_TURBO_G */
866 struct ieee80211_channel *c;
870 /* validate new mode */
871 if ((ic->ic_modecaps & (1<<mode)) == 0) {
872 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
873 "%s: mode %u not supported (caps 0x%x)\n",
874 __func__, mode, ic->ic_modecaps);
879 * Verify at least one channel is present in the available
880 * channel list before committing to the new mode.
882 KASSERT(mode < N(chanflags), ("Unexpected mode %u", mode));
883 modeflags = chanflags[mode];
884 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
885 c = &ic->ic_channels[i];
886 if (c->ic_flags == 0)
888 if (mode == IEEE80211_MODE_AUTO) {
889 /* ignore static turbo channels for autoselect */
890 if (!IEEE80211_IS_CHAN_T(c))
893 if ((c->ic_flags & modeflags) == modeflags)
897 if (i > IEEE80211_CHAN_MAX) {
898 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
899 "%s: no channels found for mode %u\n", __func__, mode);
904 * Calculate the active channel set.
906 memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
907 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
908 c = &ic->ic_channels[i];
909 if (c->ic_flags == 0)
911 if (mode == IEEE80211_MODE_AUTO) {
912 /* take anything but static turbo channels */
913 if (!IEEE80211_IS_CHAN_T(c))
914 setbit(ic->ic_chan_active, i);
916 if ((c->ic_flags & modeflags) == modeflags)
917 setbit(ic->ic_chan_active, i);
921 * If no current/default channel is setup or the current
922 * channel is wrong for the mode then pick the first
923 * available channel from the active list. This is likely
926 if (ic->ic_ibss_chan == NULL ||
927 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
928 for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
929 if (isset(ic->ic_chan_active, i)) {
930 ic->ic_ibss_chan = &ic->ic_channels[i];
933 KASSERT(ic->ic_ibss_chan != NULL &&
934 isset(ic->ic_chan_active,
935 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)),
936 ("Bad IBSS channel %u",
937 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)));
940 * If the desired channel is set but no longer valid then reset it.
942 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
943 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_des_chan)))
944 ic->ic_des_chan = IEEE80211_CHAN_ANYC;
947 * Do mode-specific rate setup.
949 if (mode == IEEE80211_MODE_11G) {
951 * Use a mixed 11b/11g rate set.
953 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode],
955 } else if (mode == IEEE80211_MODE_11B) {
957 * Force pure 11b rate set.
959 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode],
963 * Setup an initial rate set according to the
964 * current/default channel selected above. This
965 * will be changed when scanning but must exist
966 * now so driver have a consistent state of ic_ibss_chan.
968 if (ic->ic_bss) /* NB: can be called before lateattach */
969 ic->ic_bss->ni_rates = ic->ic_sup_rates[mode];
971 ic->ic_curmode = mode;
972 ieee80211_reset_erp(ic); /* reset ERP state */
973 ieee80211_wme_initparams(ic); /* reset WME stat */
980 * Return the phy mode for with the specified channel so the
981 * caller can select a rate set. This is problematic for channels
982 * where multiple operating modes are possible (e.g. 11g+11b).
983 * In those cases we defer to the current operating mode when set.
985 enum ieee80211_phymode
986 ieee80211_chan2mode(struct ieee80211com *ic, const struct ieee80211_channel *chan)
988 if (IEEE80211_IS_CHAN_T(chan)) {
989 return IEEE80211_MODE_TURBO_A;
990 } else if (IEEE80211_IS_CHAN_5GHZ(chan)) {
991 return IEEE80211_MODE_11A;
992 } else if (IEEE80211_IS_CHAN_FHSS(chan))
993 return IEEE80211_MODE_FH;
994 else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN)) {
996 * This assumes all 11g channels are also usable
997 * for 11b, which is currently true.
999 if (ic->ic_curmode == IEEE80211_MODE_TURBO_G)
1000 return IEEE80211_MODE_TURBO_G;
1001 if (ic->ic_curmode == IEEE80211_MODE_11B)
1002 return IEEE80211_MODE_11B;
1003 return IEEE80211_MODE_11G;
1005 return IEEE80211_MODE_11B;
1009 * convert IEEE80211 rate value to ifmedia subtype.
1010 * ieee80211 rate is in unit of 0.5Mbps.
1013 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1015 #define N(a) (sizeof(a) / sizeof(a[0]))
1016 static const struct {
1017 u_int m; /* rate + mode */
1018 u_int r; /* if_media rate */
1020 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1021 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1022 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1023 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1024 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1025 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1026 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1027 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1028 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1029 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1030 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1031 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1032 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1033 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1034 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1035 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1036 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1037 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1038 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1039 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1040 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1041 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1042 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1043 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1044 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1045 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1046 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1047 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1048 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1049 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1050 /* NB: OFDM72 doesn't realy exist so we don't handle it */
1054 mask = rate & IEEE80211_RATE_VAL;
1056 case IEEE80211_MODE_11A:
1057 case IEEE80211_MODE_TURBO_A:
1058 mask |= IFM_IEEE80211_11A;
1060 case IEEE80211_MODE_11B:
1061 mask |= IFM_IEEE80211_11B;
1063 case IEEE80211_MODE_FH:
1064 mask |= IFM_IEEE80211_FH;
1066 case IEEE80211_MODE_AUTO:
1067 /* NB: ic may be NULL for some drivers */
1068 if (ic && ic->ic_phytype == IEEE80211_T_FH) {
1069 mask |= IFM_IEEE80211_FH;
1072 /* NB: hack, 11g matches both 11b+11a rates */
1074 case IEEE80211_MODE_11G:
1075 case IEEE80211_MODE_TURBO_G:
1076 mask |= IFM_IEEE80211_11G;
1079 for (i = 0; i < N(rates); i++)
1080 if (rates[i].m == mask)
1087 ieee80211_media2rate(int mword)
1089 #define N(a) (sizeof(a) / sizeof(a[0]))
1090 static const int ieeerates[] = {
1094 2, /* IFM_IEEE80211_FH1 */
1095 4, /* IFM_IEEE80211_FH2 */
1096 2, /* IFM_IEEE80211_DS1 */
1097 4, /* IFM_IEEE80211_DS2 */
1098 11, /* IFM_IEEE80211_DS5 */
1099 22, /* IFM_IEEE80211_DS11 */
1100 44, /* IFM_IEEE80211_DS22 */
1101 12, /* IFM_IEEE80211_OFDM6 */
1102 18, /* IFM_IEEE80211_OFDM9 */
1103 24, /* IFM_IEEE80211_OFDM12 */
1104 36, /* IFM_IEEE80211_OFDM18 */
1105 48, /* IFM_IEEE80211_OFDM24 */
1106 72, /* IFM_IEEE80211_OFDM36 */
1107 96, /* IFM_IEEE80211_OFDM48 */
1108 108, /* IFM_IEEE80211_OFDM54 */
1109 144, /* IFM_IEEE80211_OFDM72 */
1110 0, /* IFM_IEEE80211_DS354k */
1111 0, /* IFM_IEEE80211_DS512k */
1112 6, /* IFM_IEEE80211_OFDM3 */
1113 9, /* IFM_IEEE80211_OFDM4 */
1114 54, /* IFM_IEEE80211_OFDM27 */
1116 return IFM_SUBTYPE(mword) < N(ieeerates) ?
1117 ieeerates[IFM_SUBTYPE(mword)] : 0;