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 (isset(ic->ic_modecaps, 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 setbit(ic->ic_modecaps, 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 setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
175 if (IEEE80211_IS_CHAN_B(c))
176 setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
177 if (IEEE80211_IS_CHAN_ANYG(c))
178 setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
179 if (IEEE80211_IS_CHAN_FHSS(c))
180 setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
181 if (IEEE80211_IS_CHAN_T(c))
182 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
183 if (IEEE80211_IS_CHAN_108G(c))
184 setbit(ic->ic_modecaps, 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 /* NB: must be called before ieee80211_node_detach */
271 ieee80211_proto_detach(ic);
272 ieee80211_crypto_detach(ic);
273 ieee80211_node_detach(ic);
274 ifmedia_removeall(&ic->ic_media);
276 IEEE80211_BEACON_LOCK_DESTROY(ic);
283 mapgsm(u_int freq, u_int flags)
286 if (flags & IEEE80211_CHAN_QUARTER)
288 else if (flags & IEEE80211_CHAN_HALF)
292 /* NB: there is no 907/20 wide but leave room */
293 return (freq - 906*10) / 5;
297 mappsb(u_int freq, u_int flags)
299 return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
303 * Convert MHz frequency to IEEE channel number.
306 ieee80211_mhz2ieee(u_int freq, u_int flags)
308 #define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
309 if (flags & IEEE80211_CHAN_GSM)
310 return mapgsm(freq, flags);
311 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
315 return ((int) freq - 2407) / 5;
317 return 15 + ((freq - 2512) / 20);
318 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
320 if (IS_FREQ_IN_PSB(freq))
321 return mappsb(freq, flags);
322 return (freq - 4000) / 5;
324 return (freq - 5000) / 5;
325 } else { /* either, guess */
329 if (907 <= freq && freq <= 922)
330 return mapgsm(freq, flags);
331 return ((int) freq - 2407) / 5;
334 if (IS_FREQ_IN_PSB(freq))
335 return mappsb(freq, flags);
336 else if (freq > 4900)
337 return (freq - 4000) / 5;
339 return 15 + ((freq - 2512) / 20);
341 return (freq - 5000) / 5;
343 #undef IS_FREQ_IN_PSB
347 * Convert channel to IEEE channel number.
350 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
352 if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX])
353 return c - ic->ic_channels;
354 else if (c == IEEE80211_CHAN_ANYC)
355 return IEEE80211_CHAN_ANY;
356 else if (c != NULL) {
357 if_printf(ic->ic_ifp, "invalid channel freq %u flags %x\n",
358 c->ic_freq, c->ic_flags);
361 if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
367 * Convert IEEE channel number to MHz frequency.
370 ieee80211_ieee2mhz(u_int chan, u_int flags)
372 if (flags & IEEE80211_CHAN_GSM)
373 return 907 + 5 * (chan / 10);
374 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
378 return 2407 + chan*5;
380 return 2512 + ((chan-15)*20);
381 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
382 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
384 return 4940 + chan*5 + (chan % 5 ? 2 : 0);
386 return 5000 + (chan*5);
387 } else { /* either, guess */
388 /* XXX can't distinguish PSB+GSM channels */
391 if (chan < 14) /* 0-13 */
392 return 2407 + chan*5;
393 if (chan < 27) /* 15-26 */
394 return 2512 + ((chan-15)*20);
395 return 5000 + (chan*5);
400 * Setup the media data structures according to the channel and
401 * rate tables. This must be called by the driver after
402 * ieee80211_attach and before most anything else.
405 ieee80211_media_init(struct ieee80211com *ic,
406 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
408 #define ADD(_ic, _s, _o) \
409 ifmedia_add(&(_ic)->ic_media, \
410 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
411 struct ifnet *ifp = ic->ic_ifp;
412 struct ifmediareq imr;
413 int i, j, mode, rate, maxrate, mword, mopt, r;
414 struct ieee80211_rateset *rs;
415 struct ieee80211_rateset allrates;
417 /* NB: this works because the structure is initialized to zero */
418 if (LIST_EMPTY(&ic->ic_media.ifm_list)) {
420 * Do late attach work that must wait for any subclass
421 * (i.e. driver) work such as overriding methods.
423 ieee80211_node_lateattach(ic);
426 * We are re-initializing the channel list; clear
427 * the existing media state as the media routines
428 * don't suppress duplicates.
430 ifmedia_removeall(&ic->ic_media);
431 ieee80211_chan_init(ic);
435 * Fill in media characteristics.
437 ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
439 memset(&allrates, 0, sizeof(allrates));
440 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) {
441 static const u_int mopts[] = {
447 IFM_IEEE80211_11A | IFM_IEEE80211_TURBO,
448 IFM_IEEE80211_11G | IFM_IEEE80211_TURBO,
450 if (isclr(ic->ic_modecaps, mode))
453 ADD(ic, IFM_AUTO, mopt); /* e.g. 11a auto */
454 if (ic->ic_caps & IEEE80211_C_IBSS)
455 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC);
456 if (ic->ic_caps & IEEE80211_C_HOSTAP)
457 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP);
458 if (ic->ic_caps & IEEE80211_C_AHDEMO)
459 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
460 if (ic->ic_caps & IEEE80211_C_MONITOR)
461 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR);
462 if (mode == IEEE80211_MODE_AUTO)
464 rs = &ic->ic_sup_rates[mode];
465 for (i = 0; i < rs->rs_nrates; i++) {
466 rate = rs->rs_rates[i];
467 mword = ieee80211_rate2media(ic, rate, mode);
470 ADD(ic, mword, mopt);
471 if (ic->ic_caps & IEEE80211_C_IBSS)
472 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
473 if (ic->ic_caps & IEEE80211_C_HOSTAP)
474 ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
475 if (ic->ic_caps & IEEE80211_C_AHDEMO)
476 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
477 if (ic->ic_caps & IEEE80211_C_MONITOR)
478 ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
480 * Add rate to the collection of all rates.
482 r = rate & IEEE80211_RATE_VAL;
483 for (j = 0; j < allrates.rs_nrates; j++)
484 if (allrates.rs_rates[j] == r)
486 if (j == allrates.rs_nrates) {
487 /* unique, add to the set */
488 allrates.rs_rates[j] = r;
489 allrates.rs_nrates++;
491 rate = (rate & IEEE80211_RATE_VAL) / 2;
496 for (i = 0; i < allrates.rs_nrates; i++) {
497 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
498 IEEE80211_MODE_AUTO);
501 mword = IFM_SUBTYPE(mword); /* remove media options */
503 if (ic->ic_caps & IEEE80211_C_IBSS)
504 ADD(ic, mword, IFM_IEEE80211_ADHOC);
505 if (ic->ic_caps & IEEE80211_C_HOSTAP)
506 ADD(ic, mword, IFM_IEEE80211_HOSTAP);
507 if (ic->ic_caps & IEEE80211_C_AHDEMO)
508 ADD(ic, mword, IFM_IEEE80211_ADHOC | IFM_FLAG0);
509 if (ic->ic_caps & IEEE80211_C_MONITOR)
510 ADD(ic, mword, IFM_IEEE80211_MONITOR);
512 ieee80211_media_status(ifp, &imr);
513 ifmedia_set(&ic->ic_media, imr.ifm_active);
516 ifp->if_baudrate = IF_Mbps(maxrate);
520 const struct ieee80211_rateset *
521 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
523 enum ieee80211_phymode mode = ieee80211_chan2mode(ic, c);
525 if (IEEE80211_IS_CHAN_HALF(c))
526 return &ieee80211_rateset_half;
527 if (IEEE80211_IS_CHAN_QUARTER(c))
528 return &ieee80211_rateset_quarter;
529 return &ic->ic_sup_rates[mode];
533 ieee80211_announce(struct ieee80211com *ic)
535 struct ifnet *ifp = ic->ic_ifp;
536 int i, mode, rate, mword;
537 struct ieee80211_rateset *rs;
539 for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) {
540 if (isclr(ic->ic_modecaps, mode))
542 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
543 rs = &ic->ic_sup_rates[mode];
544 for (i = 0; i < rs->rs_nrates; i++) {
545 rate = rs->rs_rates[i];
546 mword = ieee80211_rate2media(ic, rate, mode);
549 printf("%s%d%sMbps", (i != 0 ? " " : ""),
550 (rate & IEEE80211_RATE_VAL) / 2,
551 ((rate & 0x1) != 0 ? ".5" : ""));
558 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
560 #define IEEERATE(_ic,_m,_i) \
561 ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
562 int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
563 for (i = 0; i < nrates; i++)
564 if (IEEERATE(ic, mode, i) == rate)
571 * Find an instance by it's mac address.
573 struct ieee80211com *
574 ieee80211_find_vap(const u_int8_t mac[IEEE80211_ADDR_LEN])
576 struct ieee80211com *ic;
579 SLIST_FOREACH(ic, &ieee80211_list, ic_next)
580 if (IEEE80211_ADDR_EQ(mac, ic->ic_myaddr))
585 static struct ieee80211com *
586 ieee80211_find_instance(struct ifnet *ifp)
588 struct ieee80211com *ic;
591 /* XXX not right for multiple instances but works for now */
592 SLIST_FOREACH(ic, &ieee80211_list, ic_next)
593 if (ic->ic_ifp == ifp)
599 * Handle a media change request.
602 ieee80211_media_change(struct ifnet *ifp)
604 struct ieee80211com *ic;
605 struct ifmedia_entry *ime;
606 enum ieee80211_opmode newopmode;
607 enum ieee80211_phymode newphymode;
608 int i, j, newrate, error = 0;
610 ic = ieee80211_find_instance(ifp);
612 if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
615 ime = ic->ic_media.ifm_cur;
617 * First, identify the phy mode.
619 switch (IFM_MODE(ime->ifm_media)) {
620 case IFM_IEEE80211_11A:
621 newphymode = IEEE80211_MODE_11A;
623 case IFM_IEEE80211_11B:
624 newphymode = IEEE80211_MODE_11B;
626 case IFM_IEEE80211_11G:
627 newphymode = IEEE80211_MODE_11G;
629 case IFM_IEEE80211_FH:
630 newphymode = IEEE80211_MODE_FH;
633 newphymode = IEEE80211_MODE_AUTO;
639 * Turbo mode is an ``option''.
640 * XXX does not apply to AUTO
642 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
643 if (newphymode == IEEE80211_MODE_11A)
644 newphymode = IEEE80211_MODE_TURBO_A;
645 else if (newphymode == IEEE80211_MODE_11G)
646 newphymode = IEEE80211_MODE_TURBO_G;
651 * Validate requested mode is available.
653 if (isclr(ic->ic_modecaps, newphymode))
657 * Next, the fixed/variable rate.
660 if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
662 * Convert media subtype to rate.
664 newrate = ieee80211_media2rate(ime->ifm_media);
668 * Check the rate table for the specified/current phy.
670 if (newphymode == IEEE80211_MODE_AUTO) {
672 * In autoselect mode search for the rate.
674 for (j = IEEE80211_MODE_11A;
675 j < IEEE80211_MODE_MAX; j++) {
676 if (isclr(ic->ic_modecaps, j))
678 i = findrate(ic, j, newrate);
686 i = findrate(ic, newphymode, newrate);
688 if (i == -1) /* mode/rate mismatch */
691 /* NB: defer rate setting to later */
694 * Deduce new operating mode but don't install it just yet.
696 if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) ==
697 (IFM_IEEE80211_ADHOC|IFM_FLAG0))
698 newopmode = IEEE80211_M_AHDEMO;
699 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
700 newopmode = IEEE80211_M_HOSTAP;
701 else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
702 newopmode = IEEE80211_M_IBSS;
703 else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
704 newopmode = IEEE80211_M_MONITOR;
706 newopmode = IEEE80211_M_STA;
709 * Autoselect doesn't make sense when operating as an AP.
710 * If no phy mode has been selected, pick one and lock it
711 * down so rate tables can be used in forming beacon frames
714 if (newopmode == IEEE80211_M_HOSTAP &&
715 newphymode == IEEE80211_MODE_AUTO) {
716 for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++)
717 if (isset(ic->ic_modecaps, j)) {
724 * Handle phy mode change.
726 if (ic->ic_curmode != newphymode) { /* change phy mode */
727 error = ieee80211_setmode(ic, newphymode);
734 * Committed to changes, install the rate setting.
736 if (ic->ic_fixed_rate != i) {
737 ic->ic_fixed_rate = i; /* set fixed tx rate */
742 * Handle operating mode change.
744 if (ic->ic_opmode != newopmode) {
745 ic->ic_opmode = newopmode;
747 case IEEE80211_M_AHDEMO:
748 case IEEE80211_M_HOSTAP:
749 case IEEE80211_M_STA:
750 case IEEE80211_M_MONITOR:
751 ic->ic_flags &= ~IEEE80211_F_IBSSON;
753 case IEEE80211_M_IBSS:
754 ic->ic_flags |= IEEE80211_F_IBSSON;
758 * Yech, slot time may change depending on the
759 * operating mode so reset it to be sure everything
760 * is setup appropriately.
762 ieee80211_reset_erp(ic);
763 ieee80211_wme_initparams(ic); /* after opmode change */
768 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
774 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
776 struct ieee80211com *ic;
777 const struct ieee80211_rateset *rs;
779 ic = ieee80211_find_instance(ifp);
781 if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
784 imr->ifm_status = IFM_AVALID;
785 imr->ifm_active = IFM_IEEE80211;
786 if (ic->ic_state == IEEE80211_S_RUN)
787 imr->ifm_status |= IFM_ACTIVE;
789 * Calculate a current rate if possible.
791 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
793 * A fixed rate is set, report that.
795 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
796 imr->ifm_active |= ieee80211_rate2media(ic,
797 rs->rs_rates[ic->ic_fixed_rate], ic->ic_curmode);
798 } else if (ic->ic_opmode == IEEE80211_M_STA) {
800 * In station mode report the current transmit rate.
802 rs = &ic->ic_bss->ni_rates;
803 imr->ifm_active |= ieee80211_rate2media(ic,
804 rs->rs_rates[ic->ic_bss->ni_txrate], ic->ic_curmode);
806 imr->ifm_active |= IFM_AUTO;
807 switch (ic->ic_opmode) {
808 case IEEE80211_M_STA:
810 case IEEE80211_M_IBSS:
811 imr->ifm_active |= IFM_IEEE80211_ADHOC;
813 case IEEE80211_M_AHDEMO:
814 /* should not come here */
816 case IEEE80211_M_HOSTAP:
817 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
819 case IEEE80211_M_MONITOR:
820 imr->ifm_active |= IFM_IEEE80211_MONITOR;
823 switch (ic->ic_curmode) {
824 case IEEE80211_MODE_11A:
825 imr->ifm_active |= IFM_IEEE80211_11A;
827 case IEEE80211_MODE_11B:
828 imr->ifm_active |= IFM_IEEE80211_11B;
830 case IEEE80211_MODE_11G:
831 imr->ifm_active |= IFM_IEEE80211_11G;
833 case IEEE80211_MODE_FH:
834 imr->ifm_active |= IFM_IEEE80211_FH;
836 case IEEE80211_MODE_TURBO_A:
837 imr->ifm_active |= IFM_IEEE80211_11A
838 | IFM_IEEE80211_TURBO;
840 case IEEE80211_MODE_TURBO_G:
841 imr->ifm_active |= IFM_IEEE80211_11G
842 | IFM_IEEE80211_TURBO;
848 ieee80211_watchdog(struct ieee80211com *ic)
850 struct ieee80211_node_table *nt;
851 int need_inact_timer = 0;
853 if (ic->ic_state != IEEE80211_S_INIT) {
854 if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0)
855 ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
857 if (nt->nt_inact_timer) {
858 if (--nt->nt_inact_timer == 0)
860 need_inact_timer += nt->nt_inact_timer;
863 if (nt->nt_inact_timer) {
864 if (--nt->nt_inact_timer == 0)
866 need_inact_timer += nt->nt_inact_timer;
869 if (ic->ic_mgt_timer != 0 || need_inact_timer)
870 ic->ic_ifp->if_timer = 1;
874 * Set the current phy mode and recalculate the active channel
875 * set based on the available channels for this mode. Also
876 * select a new default/current channel if the current one is
877 * inappropriate for this mode.
880 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
882 #define N(a) (sizeof(a) / sizeof(a[0]))
883 static const u_int chanflags[] = {
884 0, /* IEEE80211_MODE_AUTO */
885 IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */
886 IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */
887 IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */
888 IEEE80211_CHAN_FHSS, /* IEEE80211_MODE_FH */
889 IEEE80211_CHAN_T, /* IEEE80211_MODE_TURBO_A */
890 IEEE80211_CHAN_108G, /* IEEE80211_MODE_TURBO_G */
892 struct ieee80211_channel *c;
896 /* validate new mode */
897 if (isclr(ic->ic_modecaps, mode)) {
898 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
899 "%s: mode %u not supported (caps 0x%x)\n",
900 __func__, mode, ic->ic_modecaps);
905 * Verify at least one channel is present in the available
906 * channel list before committing to the new mode.
908 KASSERT(mode < N(chanflags), ("Unexpected mode %u", mode));
909 modeflags = chanflags[mode];
910 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
911 c = &ic->ic_channels[i];
912 if (c->ic_flags == 0)
914 if (mode == IEEE80211_MODE_AUTO) {
915 /* ignore static turbo channels for autoselect */
916 if (!IEEE80211_IS_CHAN_T(c))
919 if ((c->ic_flags & modeflags) == modeflags)
923 if (i > IEEE80211_CHAN_MAX) {
924 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
925 "%s: no channels found for mode %u\n", __func__, mode);
930 * Calculate the active channel set.
932 memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
933 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
934 c = &ic->ic_channels[i];
935 if (c->ic_flags == 0)
937 if (mode == IEEE80211_MODE_AUTO) {
938 /* take anything but static turbo channels */
939 if (!IEEE80211_IS_CHAN_T(c))
940 setbit(ic->ic_chan_active, i);
942 if ((c->ic_flags & modeflags) == modeflags)
943 setbit(ic->ic_chan_active, i);
947 * If no current/default channel is setup or the current
948 * channel is wrong for the mode then pick the first
949 * available channel from the active list. This is likely
952 if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_curchan))) {
953 ic->ic_curchan = NULL;
954 for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
955 if (isset(ic->ic_chan_active, i)) {
956 ic->ic_curchan = &ic->ic_channels[i];
959 KASSERT(ic->ic_curchan != NULL, ("no current channel"));
961 if (ic->ic_ibss_chan == NULL ||
962 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan)))
963 ic->ic_ibss_chan = ic->ic_curchan;
965 * If the desired channel is set but no longer valid then reset it.
967 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
968 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_des_chan)))
969 ic->ic_des_chan = IEEE80211_CHAN_ANYC;
972 * Adjust basic rates in 11b/11g supported rate set.
973 * Note that if operating on a hal/quarter rate channel
974 * this is a noop as those rates sets are different
977 if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
978 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode], mode);
981 * Setup an initial rate set according to the
982 * current/default channel selected above. This
983 * will be changed when scanning but must exist
984 * now so driver have a consistent state of ic_ibss_chan.
986 if (ic->ic_bss != NULL) /* NB: can be called before lateattach */
987 ic->ic_bss->ni_rates = ic->ic_sup_rates[mode];
989 ic->ic_curmode = mode;
990 ieee80211_reset_erp(ic); /* reset ERP state */
991 ieee80211_wme_initparams(ic); /* reset WME stat */
998 * Return the phy mode for with the specified channel so the
999 * caller can select a rate set. This is problematic for channels
1000 * where multiple operating modes are possible (e.g. 11g+11b).
1001 * In those cases we defer to the current operating mode when set.
1003 enum ieee80211_phymode
1004 ieee80211_chan2mode(struct ieee80211com *ic, const struct ieee80211_channel *chan)
1006 if (IEEE80211_IS_CHAN_T(chan)) {
1007 return IEEE80211_MODE_TURBO_A;
1008 } else if (IEEE80211_IS_CHAN_5GHZ(chan)) {
1009 return IEEE80211_MODE_11A;
1010 } else if (IEEE80211_IS_CHAN_FHSS(chan))
1011 return IEEE80211_MODE_FH;
1012 else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN)) {
1014 * This assumes all 11g channels are also usable
1015 * for 11b, which is currently true.
1017 if (ic->ic_curmode == IEEE80211_MODE_TURBO_G)
1018 return IEEE80211_MODE_TURBO_G;
1019 if (ic->ic_curmode == IEEE80211_MODE_11B)
1020 return IEEE80211_MODE_11B;
1021 return IEEE80211_MODE_11G;
1023 return IEEE80211_MODE_11B;
1027 * convert IEEE80211 rate value to ifmedia subtype.
1028 * ieee80211 rate is in unit of 0.5Mbps.
1031 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1033 #define N(a) (sizeof(a) / sizeof(a[0]))
1034 static const struct {
1035 u_int m; /* rate + mode */
1036 u_int r; /* if_media rate */
1038 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1039 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1040 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1041 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1042 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1043 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1044 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1045 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1046 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1047 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1048 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1049 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1050 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1051 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1052 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1053 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1054 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1055 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1056 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1057 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1058 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1059 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1060 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1061 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1062 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1063 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1064 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1065 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1066 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1067 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1068 /* NB: OFDM72 doesn't realy exist so we don't handle it */
1072 mask = rate & IEEE80211_RATE_VAL;
1074 case IEEE80211_MODE_11A:
1075 case IEEE80211_MODE_TURBO_A:
1076 mask |= IFM_IEEE80211_11A;
1078 case IEEE80211_MODE_11B:
1079 mask |= IFM_IEEE80211_11B;
1081 case IEEE80211_MODE_FH:
1082 mask |= IFM_IEEE80211_FH;
1084 case IEEE80211_MODE_AUTO:
1085 /* NB: ic may be NULL for some drivers */
1086 if (ic && ic->ic_phytype == IEEE80211_T_FH) {
1087 mask |= IFM_IEEE80211_FH;
1090 /* NB: hack, 11g matches both 11b+11a rates */
1092 case IEEE80211_MODE_11G:
1093 case IEEE80211_MODE_TURBO_G:
1094 mask |= IFM_IEEE80211_11G;
1097 for (i = 0; i < N(rates); i++)
1098 if (rates[i].m == mask)
1105 ieee80211_media2rate(int mword)
1107 #define N(a) (sizeof(a) / sizeof(a[0]))
1108 static const int ieeerates[] = {
1112 2, /* IFM_IEEE80211_FH1 */
1113 4, /* IFM_IEEE80211_FH2 */
1114 2, /* IFM_IEEE80211_DS1 */
1115 4, /* IFM_IEEE80211_DS2 */
1116 11, /* IFM_IEEE80211_DS5 */
1117 22, /* IFM_IEEE80211_DS11 */
1118 44, /* IFM_IEEE80211_DS22 */
1119 12, /* IFM_IEEE80211_OFDM6 */
1120 18, /* IFM_IEEE80211_OFDM9 */
1121 24, /* IFM_IEEE80211_OFDM12 */
1122 36, /* IFM_IEEE80211_OFDM18 */
1123 48, /* IFM_IEEE80211_OFDM24 */
1124 72, /* IFM_IEEE80211_OFDM36 */
1125 96, /* IFM_IEEE80211_OFDM48 */
1126 108, /* IFM_IEEE80211_OFDM54 */
1127 144, /* IFM_IEEE80211_OFDM72 */
1128 0, /* IFM_IEEE80211_DS354k */
1129 0, /* IFM_IEEE80211_DS512k */
1130 6, /* IFM_IEEE80211_OFDM3 */
1131 9, /* IFM_IEEE80211_OFDM4 */
1132 54, /* IFM_IEEE80211_OFDM27 */
1134 return IFM_SUBTYPE(mword) < N(ieeerates) ?
1135 ieeerates[IFM_SUBTYPE(mword)] : 0;