2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2007 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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * IEEE 802.11 generic handler
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/socket.h>
41 #include <net/if_media.h>
42 #include <net/ethernet.h>
44 #include <net80211/ieee80211_var.h>
48 const char *ieee80211_phymode_name[] = {
49 "auto", /* IEEE80211_MODE_AUTO */
50 "11a", /* IEEE80211_MODE_11A */
51 "11b", /* IEEE80211_MODE_11B */
52 "11g", /* IEEE80211_MODE_11G */
53 "FH", /* IEEE80211_MODE_FH */
54 "turboA", /* IEEE80211_MODE_TURBO_A */
55 "turboG", /* IEEE80211_MODE_TURBO_G */
56 "sturboA", /* IEEE80211_MODE_STURBO_A */
57 "11na", /* IEEE80211_MODE_11NA */
58 "11ng", /* IEEE80211_MODE_11NG */
62 * Default supported rates for 802.11 operation (in IEEE .5Mb units).
64 #define B(r) ((r) | IEEE80211_RATE_BASIC)
65 static const struct ieee80211_rateset ieee80211_rateset_11a =
66 { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
67 static const struct ieee80211_rateset ieee80211_rateset_half =
68 { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
69 static const struct ieee80211_rateset ieee80211_rateset_quarter =
70 { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
71 static const struct ieee80211_rateset ieee80211_rateset_11b =
72 { 4, { B(2), B(4), B(11), B(22) } };
73 /* NB: OFDM rates are handled specially based on mode */
74 static const struct ieee80211_rateset ieee80211_rateset_11g =
75 { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
78 static int media_status(enum ieee80211_opmode ,
79 const struct ieee80211_channel *);
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 uint8_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 ieee80211_channel *c;
153 KASSERT(0 < ic->ic_nchans && ic->ic_nchans < IEEE80211_CHAN_MAX,
154 ("invalid number of channels specified: %u", ic->ic_nchans));
155 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
156 setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
157 for (i = 0; i < ic->ic_nchans; i++) {
158 c = &ic->ic_channels[i];
159 KASSERT(c->ic_flags != 0, ("channel with no flags"));
160 KASSERT(c->ic_ieee < IEEE80211_CHAN_MAX,
161 ("channel with bogus ieee number %u", c->ic_ieee));
162 setbit(ic->ic_chan_avail, c->ic_ieee);
164 * Identify mode capabilities.
166 if (IEEE80211_IS_CHAN_A(c))
167 setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
168 if (IEEE80211_IS_CHAN_B(c))
169 setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
170 if (IEEE80211_IS_CHAN_ANYG(c))
171 setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
172 if (IEEE80211_IS_CHAN_FHSS(c))
173 setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
174 if (IEEE80211_IS_CHAN_108A(c))
175 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
176 if (IEEE80211_IS_CHAN_108G(c))
177 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
178 if (IEEE80211_IS_CHAN_ST(c))
179 setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
180 if (IEEE80211_IS_CHAN_HTA(c))
181 setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
182 if (IEEE80211_IS_CHAN_HTG(c))
183 setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
185 /* initialize candidate channels to all available */
186 memcpy(ic->ic_chan_active, ic->ic_chan_avail,
187 sizeof(ic->ic_chan_avail));
189 ic->ic_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
190 ic->ic_bsschan = IEEE80211_CHAN_ANYC;
191 ic->ic_prevchan = NULL;
192 /* arbitrarily pick the first channel */
193 ic->ic_curchan = &ic->ic_channels[0];
195 /* fillin well-known rate sets if driver has not specified */
196 DEFAULTRATES(IEEE80211_MODE_11B, ieee80211_rateset_11b);
197 DEFAULTRATES(IEEE80211_MODE_11G, ieee80211_rateset_11g);
198 DEFAULTRATES(IEEE80211_MODE_11A, ieee80211_rateset_11a);
199 DEFAULTRATES(IEEE80211_MODE_TURBO_A, ieee80211_rateset_11a);
200 DEFAULTRATES(IEEE80211_MODE_TURBO_G, ieee80211_rateset_11g);
203 * Set auto mode to reset active channel state and any desired channel.
205 (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
210 ieee80211_ifattach(struct ieee80211com *ic)
212 struct ifnet *ifp = ic->ic_ifp;
214 ether_ifattach(ifp, ic->ic_myaddr);
215 ifp->if_output = ieee80211_output;
217 bpfattach2(ifp, DLT_IEEE802_11,
218 sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf);
220 /* override the 802.3 setting */
221 ifp->if_hdrlen = ic->ic_headroom
222 + sizeof(struct ieee80211_qosframe_addr4)
223 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
224 + IEEE80211_WEP_EXTIVLEN;
225 /* XXX no way to recalculate on ifdetach */
226 if (ALIGN(ifp->if_hdrlen) > max_linkhdr) {
227 /* XXX sanity check... */
228 max_linkhdr = ALIGN(ifp->if_hdrlen);
229 max_hdr = max_linkhdr + max_protohdr;
230 max_datalen = MHLEN - max_hdr;
234 * Fill in 802.11 available channel set, mark all
235 * available channels as active, and pick a default
236 * channel if not already specified.
238 ieee80211_chan_init(ic);
240 if (ic->ic_caps & IEEE80211_C_BGSCAN) /* enable if capable */
241 ic->ic_flags |= IEEE80211_F_BGSCAN;
243 /* XXX not until WME+WPA issues resolved */
244 if (ic->ic_caps & IEEE80211_C_WME) /* enable if capable */
245 ic->ic_flags |= IEEE80211_F_WME;
247 if (ic->ic_caps & IEEE80211_C_BURST)
248 ic->ic_flags |= IEEE80211_F_BURST;
249 ic->ic_flags |= IEEE80211_F_DOTH; /* XXX out of caps, just ena */
251 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
252 ic->ic_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
253 ic->ic_dtim_period = IEEE80211_DTIM_DEFAULT;
254 IEEE80211_LOCK_INIT(ic, "ieee80211com");
255 IEEE80211_BEACON_LOCK_INIT(ic, "beacon");
257 ic->ic_lintval = ic->ic_bintval;
258 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
260 ieee80211_crypto_attach(ic);
261 ieee80211_node_attach(ic);
262 ieee80211_power_attach(ic);
263 ieee80211_proto_attach(ic);
264 ieee80211_ht_attach(ic);
265 ieee80211_scan_attach(ic);
267 ieee80211_add_vap(ic);
269 ieee80211_sysctl_attach(ic); /* NB: requires ic_vap */
272 * Install a default reset method for the ioctl support.
273 * The driver is expected to fill this in before calling us.
275 if (ic->ic_reset == NULL)
276 ic->ic_reset = ieee80211_default_reset;
278 KASSERT(ifp->if_spare2 == NULL, ("oops, hosed"));
279 ifp->if_spare2 = ic; /* XXX temp backpointer */
283 ieee80211_ifdetach(struct ieee80211com *ic)
285 struct ifnet *ifp = ic->ic_ifp;
287 ieee80211_remove_vap(ic);
289 ieee80211_sysctl_detach(ic);
290 ieee80211_scan_detach(ic);
291 ieee80211_ht_detach(ic);
292 /* NB: must be called before ieee80211_node_detach */
293 ieee80211_proto_detach(ic);
294 ieee80211_crypto_detach(ic);
295 ieee80211_power_detach(ic);
296 ieee80211_node_detach(ic);
297 ifmedia_removeall(&ic->ic_media);
299 IEEE80211_LOCK_DESTROY(ic);
300 IEEE80211_BEACON_LOCK_DESTROY(ic);
307 mapgsm(u_int freq, u_int flags)
310 if (flags & IEEE80211_CHAN_QUARTER)
312 else if (flags & IEEE80211_CHAN_HALF)
316 /* NB: there is no 907/20 wide but leave room */
317 return (freq - 906*10) / 5;
321 mappsb(u_int freq, u_int flags)
323 return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
327 * Convert MHz frequency to IEEE channel number.
330 ieee80211_mhz2ieee(u_int freq, u_int flags)
332 #define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
333 if (flags & IEEE80211_CHAN_GSM)
334 return mapgsm(freq, flags);
335 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
339 return ((int) freq - 2407) / 5;
341 return 15 + ((freq - 2512) / 20);
342 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
344 /* XXX check regdomain? */
345 if (IS_FREQ_IN_PSB(freq))
346 return mappsb(freq, flags);
347 return (freq - 4000) / 5;
349 return (freq - 5000) / 5;
350 } else { /* either, guess */
354 if (907 <= freq && freq <= 922)
355 return mapgsm(freq, flags);
356 return ((int) freq - 2407) / 5;
359 if (IS_FREQ_IN_PSB(freq))
360 return mappsb(freq, flags);
361 else if (freq > 4900)
362 return (freq - 4000) / 5;
364 return 15 + ((freq - 2512) / 20);
366 return (freq - 5000) / 5;
368 #undef IS_FREQ_IN_PSB
372 * Convert channel to IEEE channel number.
375 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
378 if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
381 return (c == IEEE80211_CHAN_ANYC ? IEEE80211_CHAN_ANY : c->ic_ieee);
385 * Convert IEEE channel number to MHz frequency.
388 ieee80211_ieee2mhz(u_int chan, u_int flags)
390 if (flags & IEEE80211_CHAN_GSM)
391 return 907 + 5 * (chan / 10);
392 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
396 return 2407 + chan*5;
398 return 2512 + ((chan-15)*20);
399 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
400 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
402 return 4940 + chan*5 + (chan % 5 ? 2 : 0);
404 return 5000 + (chan*5);
405 } else { /* either, guess */
406 /* XXX can't distinguish PSB+GSM channels */
409 if (chan < 14) /* 0-13 */
410 return 2407 + chan*5;
411 if (chan < 27) /* 15-26 */
412 return 2512 + ((chan-15)*20);
413 return 5000 + (chan*5);
418 * Locate a channel given a frequency+flags. We cache
419 * the previous lookup to optimize swithing between two
420 * channels--as happens with dynamic turbo.
422 struct ieee80211_channel *
423 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
425 struct ieee80211_channel *c;
428 flags &= IEEE80211_CHAN_ALLTURBO;
430 if (c != NULL && c->ic_freq == freq &&
431 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
433 /* brute force search */
434 for (i = 0; i < ic->ic_nchans; i++) {
435 c = &ic->ic_channels[i];
436 if (c->ic_freq == freq &&
437 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
444 * Locate a channel given a channel number+flags. We cache
445 * the previous lookup to optimize switching between two
446 * channels--as happens with dynamic turbo.
448 struct ieee80211_channel *
449 ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
451 struct ieee80211_channel *c;
454 flags &= IEEE80211_CHAN_ALLTURBO;
456 if (c != NULL && c->ic_ieee == ieee &&
457 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
459 /* brute force search */
460 for (i = 0; i < ic->ic_nchans; i++) {
461 c = &ic->ic_channels[i];
462 if (c->ic_ieee == ieee &&
463 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
470 addmedia(struct ieee80211com *ic, int mode, int mword)
472 #define TURBO(m) ((m) | IFM_IEEE80211_TURBO)
473 #define ADD(_ic, _s, _o) \
474 ifmedia_add(&(_ic)->ic_media, \
475 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
476 static const u_int mopts[IEEE80211_MODE_MAX] = {
477 IFM_AUTO, /* IEEE80211_MODE_AUTO */
478 IFM_IEEE80211_11A, /* IEEE80211_MODE_11A */
479 IFM_IEEE80211_11B, /* IEEE80211_MODE_11B */
480 IFM_IEEE80211_11G, /* IEEE80211_MODE_11G */
481 IFM_IEEE80211_FH, /* IEEE80211_MODE_FH */
482 TURBO(IFM_IEEE80211_11A), /* IEEE80211_MODE_TURBO_A */
483 TURBO(IFM_IEEE80211_11G), /* IEEE80211_MODE_TURBO_G */
484 TURBO(IFM_IEEE80211_11A), /* IEEE80211_MODE_STURBO_A */
485 IFM_IEEE80211_11NA, /* IEEE80211_MODE_11NA */
486 IFM_IEEE80211_11NG, /* IEEE80211_MODE_11NG */
490 KASSERT(mode < IEEE80211_MODE_MAX, ("bad mode %u", mode));
492 KASSERT(mopt != 0 || mode == IEEE80211_MODE_AUTO,
493 ("no media mapping for mode %u", mode));
495 ADD(ic, mword, mopt); /* e.g. 11a auto */
496 if (ic->ic_caps & IEEE80211_C_IBSS)
497 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
498 if (ic->ic_caps & IEEE80211_C_HOSTAP)
499 ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
500 if (ic->ic_caps & IEEE80211_C_AHDEMO)
501 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
502 if (ic->ic_caps & IEEE80211_C_MONITOR)
503 ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
509 * Setup the media data structures according to the channel and
510 * rate tables. This must be called by the driver after
511 * ieee80211_attach and before most anything else.
514 ieee80211_media_init(struct ieee80211com *ic,
515 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
517 struct ifnet *ifp = ic->ic_ifp;
518 int i, j, mode, rate, maxrate, mword, r;
519 const struct ieee80211_rateset *rs;
520 struct ieee80211_rateset allrates;
522 /* NB: this works because the structure is initialized to zero */
523 if (LIST_EMPTY(&ic->ic_media.ifm_list)) {
525 * Do late attach work that must wait for any subclass
526 * (i.e. driver) work such as overriding methods.
528 ieee80211_node_lateattach(ic);
531 * We are re-initializing the channel list; clear
532 * the existing media state as the media routines
533 * don't suppress duplicates.
535 ifmedia_removeall(&ic->ic_media);
536 ieee80211_chan_init(ic);
538 ieee80211_power_lateattach(ic);
541 * Fill in media characteristics.
543 ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
546 * Add media for legacy operating modes.
548 memset(&allrates, 0, sizeof(allrates));
549 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
550 if (isclr(ic->ic_modecaps, mode))
552 addmedia(ic, mode, IFM_AUTO);
553 if (mode == IEEE80211_MODE_AUTO)
555 rs = &ic->ic_sup_rates[mode];
556 for (i = 0; i < rs->rs_nrates; i++) {
557 rate = rs->rs_rates[i];
558 mword = ieee80211_rate2media(ic, rate, mode);
561 addmedia(ic, mode, mword);
563 * Add legacy rate to the collection of all rates.
565 r = rate & IEEE80211_RATE_VAL;
566 for (j = 0; j < allrates.rs_nrates; j++)
567 if (allrates.rs_rates[j] == r)
569 if (j == allrates.rs_nrates) {
570 /* unique, add to the set */
571 allrates.rs_rates[j] = r;
572 allrates.rs_nrates++;
574 rate = (rate & IEEE80211_RATE_VAL) / 2;
579 for (i = 0; i < allrates.rs_nrates; i++) {
580 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
581 IEEE80211_MODE_AUTO);
584 /* NB: remove media options from mword */
585 addmedia(ic, IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
588 * Add HT/11n media. Note that we do not have enough
589 * bits in the media subtype to express the MCS so we
590 * use a "placeholder" media subtype and any fixed MCS
591 * must be specified with a different mechanism.
593 for (; mode < IEEE80211_MODE_MAX; mode++) {
594 if (isclr(ic->ic_modecaps, mode))
596 addmedia(ic, mode, IFM_AUTO);
597 addmedia(ic, mode, IFM_IEEE80211_MCS);
599 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
600 isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
601 addmedia(ic, IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
602 /* XXX could walk htrates */
603 /* XXX known array size */
604 if (ieee80211_htrates[15] > maxrate)
605 maxrate = ieee80211_htrates[15];
608 /* NB: strip explicit mode; we're actually in autoselect */
609 ifmedia_set(&ic->ic_media,
610 media_status(ic->ic_opmode, ic->ic_curchan) &~ IFM_MMASK);
613 ifp->if_baudrate = IF_Mbps(maxrate);
616 const struct ieee80211_rateset *
617 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
619 if (IEEE80211_IS_CHAN_HALF(c))
620 return &ieee80211_rateset_half;
621 if (IEEE80211_IS_CHAN_QUARTER(c))
622 return &ieee80211_rateset_quarter;
623 if (IEEE80211_IS_CHAN_HTA(c))
624 return &ic->ic_sup_rates[IEEE80211_MODE_11A];
625 if (IEEE80211_IS_CHAN_HTG(c)) {
626 /* XXX does this work for basic rates? */
627 return &ic->ic_sup_rates[IEEE80211_MODE_11G];
629 return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
633 ieee80211_announce(struct ieee80211com *ic)
635 struct ifnet *ifp = ic->ic_ifp;
636 int i, mode, rate, mword;
637 const struct ieee80211_rateset *rs;
639 /* NB: skip AUTO since it has no rates */
640 for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
641 if (isclr(ic->ic_modecaps, mode))
643 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
644 rs = &ic->ic_sup_rates[mode];
645 for (i = 0; i < rs->rs_nrates; i++) {
646 mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
649 rate = ieee80211_media2rate(mword);
650 printf("%s%d%sMbps", (i != 0 ? " " : ""),
651 rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
655 ieee80211_ht_announce(ic);
659 ieee80211_announce_channels(struct ieee80211com *ic)
661 const struct ieee80211_channel *c;
665 printf("Chan Freq CW RegPwr MinPwr MaxPwr\n");
666 for (i = 0; i < ic->ic_nchans; i++) {
667 c = &ic->ic_channels[i];
668 if (IEEE80211_IS_CHAN_ST(c))
670 else if (IEEE80211_IS_CHAN_108A(c))
672 else if (IEEE80211_IS_CHAN_108G(c))
674 else if (IEEE80211_IS_CHAN_HT(c))
676 else if (IEEE80211_IS_CHAN_A(c))
678 else if (IEEE80211_IS_CHAN_ANYG(c))
680 else if (IEEE80211_IS_CHAN_B(c))
684 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
686 else if (IEEE80211_IS_CHAN_HALF(c))
688 else if (IEEE80211_IS_CHAN_QUARTER(c))
692 printf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n"
693 , c->ic_ieee, c->ic_freq, type
695 , IEEE80211_IS_CHAN_HT40U(c) ? '+' :
696 IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
698 , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
699 , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
705 * Find an instance by it's mac address.
707 struct ieee80211com *
708 ieee80211_find_vap(const uint8_t mac[IEEE80211_ADDR_LEN])
710 struct ieee80211com *ic;
713 SLIST_FOREACH(ic, &ieee80211_list, ic_next)
714 if (IEEE80211_ADDR_EQ(mac, ic->ic_myaddr))
719 static struct ieee80211com *
720 ieee80211_find_instance(struct ifnet *ifp)
722 struct ieee80211com *ic;
725 /* XXX not right for multiple instances but works for now */
726 SLIST_FOREACH(ic, &ieee80211_list, ic_next)
727 if (ic->ic_ifp == ifp)
733 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
735 #define IEEERATE(_ic,_m,_i) \
736 ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
737 int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
738 for (i = 0; i < nrates; i++)
739 if (IEEERATE(ic, mode, i) == rate)
746 * Convert a media specification to a rate index and possibly a mode
747 * (if the rate is fixed and the mode is specified as ``auto'' then
748 * we need to lock down the mode so the index is meanginful).
751 checkrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
755 * Check the rate table for the specified/current phy.
757 if (mode == IEEE80211_MODE_AUTO) {
760 * In autoselect mode search for the rate.
762 for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
763 if (isset(ic->ic_modecaps, i) &&
764 findrate(ic, i, rate) != -1)
770 * Mode is fixed, check for rate.
772 return (findrate(ic, mode, rate) != -1);
777 * Handle a media change request.
780 ieee80211_media_change(struct ifnet *ifp)
782 struct ieee80211com *ic;
783 struct ifmedia_entry *ime;
784 enum ieee80211_opmode newopmode;
785 enum ieee80211_phymode newphymode;
786 int newrate, error = 0;
788 ic = ieee80211_find_instance(ifp);
790 if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
793 ime = ic->ic_media.ifm_cur;
795 * First, identify the phy mode.
797 switch (IFM_MODE(ime->ifm_media)) {
798 case IFM_IEEE80211_11A:
799 newphymode = IEEE80211_MODE_11A;
801 case IFM_IEEE80211_11B:
802 newphymode = IEEE80211_MODE_11B;
804 case IFM_IEEE80211_11G:
805 newphymode = IEEE80211_MODE_11G;
807 case IFM_IEEE80211_FH:
808 newphymode = IEEE80211_MODE_FH;
810 case IFM_IEEE80211_11NA:
811 newphymode = IEEE80211_MODE_11NA;
813 case IFM_IEEE80211_11NG:
814 newphymode = IEEE80211_MODE_11NG;
817 newphymode = IEEE80211_MODE_AUTO;
823 * Turbo mode is an ``option''.
824 * XXX does not apply to AUTO
826 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
827 if (newphymode == IEEE80211_MODE_11A) {
828 if (ic->ic_flags & IEEE80211_F_TURBOP)
829 newphymode = IEEE80211_MODE_TURBO_A;
831 newphymode = IEEE80211_MODE_STURBO_A;
832 } else if (newphymode == IEEE80211_MODE_11G)
833 newphymode = IEEE80211_MODE_TURBO_G;
839 * Next, the fixed/variable rate.
841 newrate = ic->ic_fixed_rate;
842 if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
844 * Convert media subtype to rate.
846 newrate = ieee80211_media2rate(ime->ifm_media);
847 if (newrate == 0 || !checkrate(ic, newphymode, newrate))
850 newrate = IEEE80211_FIXED_RATE_NONE;
853 * Deduce new operating mode but don't install it just yet.
855 if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) ==
856 (IFM_IEEE80211_ADHOC|IFM_FLAG0))
857 newopmode = IEEE80211_M_AHDEMO;
858 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
859 newopmode = IEEE80211_M_HOSTAP;
860 else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
861 newopmode = IEEE80211_M_IBSS;
862 else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
863 newopmode = IEEE80211_M_MONITOR;
865 newopmode = IEEE80211_M_STA;
868 * Handle phy mode change.
870 if (ic->ic_des_mode != newphymode) { /* change phy mode */
871 ic->ic_des_mode = newphymode;
876 * Committed to changes, install the rate setting.
878 if (ic->ic_fixed_rate != newrate) {
879 ic->ic_fixed_rate = newrate; /* set fixed tx rate */
884 * Handle operating mode change.
886 if (ic->ic_opmode != newopmode) {
887 ic->ic_opmode = newopmode;
889 case IEEE80211_M_AHDEMO:
890 case IEEE80211_M_HOSTAP:
891 case IEEE80211_M_STA:
892 case IEEE80211_M_MONITOR:
893 case IEEE80211_M_WDS:
894 ic->ic_flags &= ~IEEE80211_F_IBSSON;
896 case IEEE80211_M_IBSS:
897 ic->ic_flags |= IEEE80211_F_IBSSON;
901 * Yech, slot time may change depending on the
902 * operating mode so reset it to be sure everything
903 * is setup appropriately.
905 ieee80211_reset_erp(ic);
906 ieee80211_wme_initparams(ic); /* after opmode change */
911 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
917 * Common code to calculate the media status word
918 * from the operating mode and channel state.
921 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
925 status = IFM_IEEE80211;
927 case IEEE80211_M_STA:
929 case IEEE80211_M_IBSS:
930 status |= IFM_IEEE80211_ADHOC;
932 case IEEE80211_M_HOSTAP:
933 status |= IFM_IEEE80211_HOSTAP;
935 case IEEE80211_M_MONITOR:
936 status |= IFM_IEEE80211_MONITOR;
938 case IEEE80211_M_AHDEMO:
939 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
941 case IEEE80211_M_WDS:
942 /* should not come here */
945 if (IEEE80211_IS_CHAN_HTA(chan)) {
946 status |= IFM_IEEE80211_11NA;
947 } else if (IEEE80211_IS_CHAN_HTG(chan)) {
948 status |= IFM_IEEE80211_11NG;
949 } else if (IEEE80211_IS_CHAN_A(chan)) {
950 status |= IFM_IEEE80211_11A;
951 } else if (IEEE80211_IS_CHAN_B(chan)) {
952 status |= IFM_IEEE80211_11B;
953 } else if (IEEE80211_IS_CHAN_ANYG(chan)) {
954 status |= IFM_IEEE80211_11G;
955 } else if (IEEE80211_IS_CHAN_FHSS(chan)) {
956 status |= IFM_IEEE80211_FH;
958 /* XXX else complain? */
960 if (IEEE80211_IS_CHAN_TURBO(chan))
961 status |= IFM_IEEE80211_TURBO;
967 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
969 struct ieee80211com *ic;
970 enum ieee80211_phymode mode;
971 const struct ieee80211_rateset *rs;
973 ic = ieee80211_find_instance(ifp);
975 if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
978 imr->ifm_status = IFM_AVALID;
980 * NB: use the current channel's mode to lock down a xmit
981 * rate only when running; otherwise we may have a mismatch
982 * in which case the rate will not be convertible.
984 if (ic->ic_state == IEEE80211_S_RUN) {
985 imr->ifm_status |= IFM_ACTIVE;
986 mode = ieee80211_chan2mode(ic->ic_curchan);
988 mode = IEEE80211_MODE_AUTO;
989 imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan);
991 * Calculate a current rate if possible.
993 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
995 * A fixed rate is set, report that.
997 imr->ifm_active |= ieee80211_rate2media(ic,
998 ic->ic_fixed_rate, mode);
999 } else if (ic->ic_opmode == IEEE80211_M_STA) {
1001 * In station mode report the current transmit rate.
1004 rs = &ic->ic_bss->ni_rates;
1005 imr->ifm_active |= ieee80211_rate2media(ic,
1006 rs->rs_rates[ic->ic_bss->ni_txrate], mode);
1008 imr->ifm_active |= IFM_AUTO;
1012 * Set the current phy mode and recalculate the active channel
1013 * set based on the available channels for this mode. Also
1014 * select a new default/current channel if the current one is
1015 * inappropriate for this mode.
1018 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1021 * Adjust basic rates in 11b/11g supported rate set.
1022 * Note that if operating on a hal/quarter rate channel
1023 * this is a noop as those rates sets are different
1026 if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1027 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode], mode);
1029 ic->ic_curmode = mode;
1030 ieee80211_reset_erp(ic); /* reset ERP state */
1031 ieee80211_wme_initparams(ic); /* reset WME stat */
1037 * Return the phy mode for with the specified channel.
1039 enum ieee80211_phymode
1040 ieee80211_chan2mode(const struct ieee80211_channel *chan)
1043 if (IEEE80211_IS_CHAN_HTA(chan))
1044 return IEEE80211_MODE_11NA;
1045 else if (IEEE80211_IS_CHAN_HTG(chan))
1046 return IEEE80211_MODE_11NG;
1047 else if (IEEE80211_IS_CHAN_108G(chan))
1048 return IEEE80211_MODE_TURBO_G;
1049 else if (IEEE80211_IS_CHAN_ST(chan))
1050 return IEEE80211_MODE_STURBO_A;
1051 else if (IEEE80211_IS_CHAN_TURBO(chan))
1052 return IEEE80211_MODE_TURBO_A;
1053 else if (IEEE80211_IS_CHAN_A(chan))
1054 return IEEE80211_MODE_11A;
1055 else if (IEEE80211_IS_CHAN_ANYG(chan))
1056 return IEEE80211_MODE_11G;
1057 else if (IEEE80211_IS_CHAN_B(chan))
1058 return IEEE80211_MODE_11B;
1059 else if (IEEE80211_IS_CHAN_FHSS(chan))
1060 return IEEE80211_MODE_FH;
1062 /* NB: should not get here */
1063 printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1064 __func__, chan->ic_freq, chan->ic_flags);
1065 return IEEE80211_MODE_11B;
1069 u_int match; /* rate + mode */
1070 u_int media; /* if_media rate */
1074 findmedia(const struct ratemedia rates[], int n, u_int match)
1078 for (i = 0; i < n; i++)
1079 if (rates[i].match == match)
1080 return rates[i].media;
1085 * Convert IEEE80211 rate value to ifmedia subtype.
1086 * Rate is either a legacy rate in units of 0.5Mbps
1090 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1092 #define N(a) (sizeof(a) / sizeof(a[0]))
1093 static const struct ratemedia rates[] = {
1094 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1095 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1096 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1097 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1098 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1099 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1100 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1101 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1102 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1103 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1104 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1105 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1106 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1107 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1108 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1109 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1110 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1111 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1112 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1113 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1114 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1115 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1116 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1117 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1118 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1119 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1120 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1121 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1122 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1123 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1124 /* NB: OFDM72 doesn't realy exist so we don't handle it */
1126 static const struct ratemedia htrates[] = {
1127 { 0, IFM_IEEE80211_MCS },
1128 { 1, IFM_IEEE80211_MCS },
1129 { 2, IFM_IEEE80211_MCS },
1130 { 3, IFM_IEEE80211_MCS },
1131 { 4, IFM_IEEE80211_MCS },
1132 { 5, IFM_IEEE80211_MCS },
1133 { 6, IFM_IEEE80211_MCS },
1134 { 7, IFM_IEEE80211_MCS },
1135 { 8, IFM_IEEE80211_MCS },
1136 { 9, IFM_IEEE80211_MCS },
1137 { 10, IFM_IEEE80211_MCS },
1138 { 11, IFM_IEEE80211_MCS },
1139 { 12, IFM_IEEE80211_MCS },
1140 { 13, IFM_IEEE80211_MCS },
1141 { 14, IFM_IEEE80211_MCS },
1142 { 15, IFM_IEEE80211_MCS },
1147 * Check 11n rates first for match as an MCS.
1149 if (mode == IEEE80211_MODE_11NA) {
1150 if (rate & IEEE80211_RATE_MCS) {
1151 rate &= ~IEEE80211_RATE_MCS;
1152 m = findmedia(htrates, N(htrates), rate);
1154 return m | IFM_IEEE80211_11NA;
1156 } else if (mode == IEEE80211_MODE_11NG) {
1157 /* NB: 12 is ambiguous, it will be treated as an MCS */
1158 if (rate & IEEE80211_RATE_MCS) {
1159 rate &= ~IEEE80211_RATE_MCS;
1160 m = findmedia(htrates, N(htrates), rate);
1162 return m | IFM_IEEE80211_11NG;
1165 rate &= IEEE80211_RATE_VAL;
1167 case IEEE80211_MODE_11A:
1168 case IEEE80211_MODE_11NA:
1169 case IEEE80211_MODE_TURBO_A:
1170 case IEEE80211_MODE_STURBO_A:
1171 return findmedia(rates, N(rates), rate | IFM_IEEE80211_11A);
1172 case IEEE80211_MODE_11B:
1173 return findmedia(rates, N(rates), rate | IFM_IEEE80211_11B);
1174 case IEEE80211_MODE_FH:
1175 return findmedia(rates, N(rates), rate | IFM_IEEE80211_FH);
1176 case IEEE80211_MODE_AUTO:
1177 /* NB: ic may be NULL for some drivers */
1178 if (ic && ic->ic_phytype == IEEE80211_T_FH)
1179 return findmedia(rates, N(rates),
1180 rate | IFM_IEEE80211_FH);
1181 /* NB: hack, 11g matches both 11b+11a rates */
1183 case IEEE80211_MODE_11G:
1184 case IEEE80211_MODE_11NG:
1185 case IEEE80211_MODE_TURBO_G:
1186 return findmedia(rates, N(rates), rate | IFM_IEEE80211_11G);
1193 ieee80211_media2rate(int mword)
1195 #define N(a) (sizeof(a) / sizeof(a[0]))
1196 static const int ieeerates[] = {
1200 2, /* IFM_IEEE80211_FH1 */
1201 4, /* IFM_IEEE80211_FH2 */
1202 2, /* IFM_IEEE80211_DS1 */
1203 4, /* IFM_IEEE80211_DS2 */
1204 11, /* IFM_IEEE80211_DS5 */
1205 22, /* IFM_IEEE80211_DS11 */
1206 44, /* IFM_IEEE80211_DS22 */
1207 12, /* IFM_IEEE80211_OFDM6 */
1208 18, /* IFM_IEEE80211_OFDM9 */
1209 24, /* IFM_IEEE80211_OFDM12 */
1210 36, /* IFM_IEEE80211_OFDM18 */
1211 48, /* IFM_IEEE80211_OFDM24 */
1212 72, /* IFM_IEEE80211_OFDM36 */
1213 96, /* IFM_IEEE80211_OFDM48 */
1214 108, /* IFM_IEEE80211_OFDM54 */
1215 144, /* IFM_IEEE80211_OFDM72 */
1216 0, /* IFM_IEEE80211_DS354k */
1217 0, /* IFM_IEEE80211_DS512k */
1218 6, /* IFM_IEEE80211_OFDM3 */
1219 9, /* IFM_IEEE80211_OFDM4 */
1220 54, /* IFM_IEEE80211_OFDM27 */
1221 -1, /* IFM_IEEE80211_MCS */
1223 return IFM_SUBTYPE(mword) < N(ieeerates) ?
1224 ieeerates[IFM_SUBTYPE(mword)] : 0;