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 addmedia(struct ieee80211com *ic, int mode, int mword)
446 #define TURBO(m) ((m) | IFM_IEEE80211_TURBO)
447 #define ADD(_ic, _s, _o) \
448 ifmedia_add(&(_ic)->ic_media, \
449 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
450 static const u_int mopts[IEEE80211_MODE_MAX] = {
451 IFM_AUTO, /* IEEE80211_MODE_AUTO */
452 IFM_IEEE80211_11A, /* IEEE80211_MODE_11A */
453 IFM_IEEE80211_11B, /* IEEE80211_MODE_11B */
454 IFM_IEEE80211_11G, /* IEEE80211_MODE_11G */
455 IFM_IEEE80211_FH, /* IEEE80211_MODE_FH */
456 TURBO(IFM_IEEE80211_11A), /* IEEE80211_MODE_TURBO_A */
457 TURBO(IFM_IEEE80211_11G), /* IEEE80211_MODE_TURBO_G */
458 TURBO(IFM_IEEE80211_11A), /* IEEE80211_MODE_STURBO_A */
459 IFM_IEEE80211_11NA, /* IEEE80211_MODE_11NA */
460 IFM_IEEE80211_11NG, /* IEEE80211_MODE_11NG */
464 KASSERT(mode < IEEE80211_MODE_MAX, ("bad mode %u", mode));
466 KASSERT(mopt != 0 || mode == IEEE80211_MODE_AUTO,
467 ("no media mapping for mode %u", mode));
469 ADD(ic, mword, mopt); /* e.g. 11a auto */
470 if (ic->ic_caps & IEEE80211_C_IBSS)
471 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
472 if (ic->ic_caps & IEEE80211_C_HOSTAP)
473 ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
474 if (ic->ic_caps & IEEE80211_C_AHDEMO)
475 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
476 if (ic->ic_caps & IEEE80211_C_MONITOR)
477 ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
483 * Setup the media data structures according to the channel and
484 * rate tables. This must be called by the driver after
485 * ieee80211_attach and before most anything else.
488 ieee80211_media_init(struct ieee80211com *ic,
489 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
491 struct ifnet *ifp = ic->ic_ifp;
492 int i, j, mode, rate, maxrate, mword, r;
493 const struct ieee80211_rateset *rs;
494 struct ieee80211_rateset allrates;
496 /* NB: this works because the structure is initialized to zero */
497 if (LIST_EMPTY(&ic->ic_media.ifm_list)) {
499 * Do late attach work that must wait for any subclass
500 * (i.e. driver) work such as overriding methods.
502 ieee80211_node_lateattach(ic);
505 * We are re-initializing the channel list; clear
506 * the existing media state as the media routines
507 * don't suppress duplicates.
509 ifmedia_removeall(&ic->ic_media);
510 ieee80211_chan_init(ic);
512 ieee80211_power_lateattach(ic);
515 * Fill in media characteristics.
517 ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
520 * Add media for legacy operating modes.
522 memset(&allrates, 0, sizeof(allrates));
523 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
524 if (isclr(ic->ic_modecaps, mode))
526 addmedia(ic, mode, IFM_AUTO);
527 if (mode == IEEE80211_MODE_AUTO)
529 rs = &ic->ic_sup_rates[mode];
530 for (i = 0; i < rs->rs_nrates; i++) {
531 rate = rs->rs_rates[i];
532 mword = ieee80211_rate2media(ic, rate, mode);
535 addmedia(ic, mode, mword);
537 * Add legacy rate to the collection of all rates.
539 r = rate & IEEE80211_RATE_VAL;
540 for (j = 0; j < allrates.rs_nrates; j++)
541 if (allrates.rs_rates[j] == r)
543 if (j == allrates.rs_nrates) {
544 /* unique, add to the set */
545 allrates.rs_rates[j] = r;
546 allrates.rs_nrates++;
548 rate = (rate & IEEE80211_RATE_VAL) / 2;
553 for (i = 0; i < allrates.rs_nrates; i++) {
554 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
555 IEEE80211_MODE_AUTO);
558 /* NB: remove media options from mword */
559 addmedia(ic, IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
562 * Add HT/11n media. Note that we do not have enough
563 * bits in the media subtype to express the MCS so we
564 * use a "placeholder" media subtype and any fixed MCS
565 * must be specified with a different mechanism.
567 for (; mode < IEEE80211_MODE_MAX; mode++) {
568 if (isclr(ic->ic_modecaps, mode))
570 addmedia(ic, mode, IFM_AUTO);
571 addmedia(ic, mode, IFM_IEEE80211_MCS);
573 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
574 isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
575 addmedia(ic, IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
576 /* XXX could walk htrates */
577 /* XXX known array size */
578 if (ieee80211_htrates[15] > maxrate)
579 maxrate = ieee80211_htrates[15];
582 /* NB: strip explicit mode; we're actually in autoselect */
583 ifmedia_set(&ic->ic_media,
584 media_status(ic->ic_opmode, ic->ic_curchan) &~ IFM_MMASK);
587 ifp->if_baudrate = IF_Mbps(maxrate);
590 const struct ieee80211_rateset *
591 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
593 if (IEEE80211_IS_CHAN_HALF(c))
594 return &ieee80211_rateset_half;
595 if (IEEE80211_IS_CHAN_QUARTER(c))
596 return &ieee80211_rateset_quarter;
597 if (IEEE80211_IS_CHAN_HTA(c))
598 return &ic->ic_sup_rates[IEEE80211_MODE_11A];
599 if (IEEE80211_IS_CHAN_HTG(c)) {
600 /* XXX does this work for basic rates? */
601 return &ic->ic_sup_rates[IEEE80211_MODE_11G];
603 return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
607 ieee80211_announce(struct ieee80211com *ic)
609 struct ifnet *ifp = ic->ic_ifp;
610 int i, mode, rate, mword;
611 const struct ieee80211_rateset *rs;
613 /* NB: skip AUTO since it has no rates */
614 for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
615 if (isclr(ic->ic_modecaps, mode))
617 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
618 rs = &ic->ic_sup_rates[mode];
619 for (i = 0; i < rs->rs_nrates; i++) {
620 mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
623 rate = ieee80211_media2rate(mword);
624 printf("%s%d%sMbps", (i != 0 ? " " : ""),
625 rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
629 ieee80211_ht_announce(ic);
633 ieee80211_announce_channels(struct ieee80211com *ic)
635 const struct ieee80211_channel *c;
639 printf("Chan Freq CW RegPwr MinPwr MaxPwr\n");
640 for (i = 0; i < ic->ic_nchans; i++) {
641 c = &ic->ic_channels[i];
642 if (IEEE80211_IS_CHAN_ST(c))
644 else if (IEEE80211_IS_CHAN_108A(c))
646 else if (IEEE80211_IS_CHAN_108G(c))
648 else if (IEEE80211_IS_CHAN_HT(c))
650 else if (IEEE80211_IS_CHAN_A(c))
652 else if (IEEE80211_IS_CHAN_ANYG(c))
654 else if (IEEE80211_IS_CHAN_B(c))
658 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
660 else if (IEEE80211_IS_CHAN_HALF(c))
662 else if (IEEE80211_IS_CHAN_QUARTER(c))
666 printf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n"
667 , c->ic_ieee, c->ic_freq, type
669 , IEEE80211_IS_CHAN_HT40U(c) ? '+' :
670 IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
672 , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
673 , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
679 * Find an instance by it's mac address.
681 struct ieee80211com *
682 ieee80211_find_vap(const uint8_t mac[IEEE80211_ADDR_LEN])
684 struct ieee80211com *ic;
687 SLIST_FOREACH(ic, &ieee80211_list, ic_next)
688 if (IEEE80211_ADDR_EQ(mac, ic->ic_myaddr))
693 static struct ieee80211com *
694 ieee80211_find_instance(struct ifnet *ifp)
696 struct ieee80211com *ic;
699 /* XXX not right for multiple instances but works for now */
700 SLIST_FOREACH(ic, &ieee80211_list, ic_next)
701 if (ic->ic_ifp == ifp)
707 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
709 #define IEEERATE(_ic,_m,_i) \
710 ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
711 int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
712 for (i = 0; i < nrates; i++)
713 if (IEEERATE(ic, mode, i) == rate)
720 * Convert a media specification to a rate index and possibly a mode
721 * (if the rate is fixed and the mode is specified as ``auto'' then
722 * we need to lock down the mode so the index is meanginful).
725 checkrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
729 * Check the rate table for the specified/current phy.
731 if (mode == IEEE80211_MODE_AUTO) {
734 * In autoselect mode search for the rate.
736 for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
737 if (isset(ic->ic_modecaps, i) &&
738 findrate(ic, i, rate) != -1)
744 * Mode is fixed, check for rate.
746 return (findrate(ic, mode, rate) != -1);
751 * Handle a media change request.
754 ieee80211_media_change(struct ifnet *ifp)
756 struct ieee80211com *ic;
757 struct ifmedia_entry *ime;
758 enum ieee80211_opmode newopmode;
759 enum ieee80211_phymode newphymode;
760 int newrate, error = 0;
762 ic = ieee80211_find_instance(ifp);
764 if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
767 ime = ic->ic_media.ifm_cur;
769 * First, identify the phy mode.
771 switch (IFM_MODE(ime->ifm_media)) {
772 case IFM_IEEE80211_11A:
773 newphymode = IEEE80211_MODE_11A;
775 case IFM_IEEE80211_11B:
776 newphymode = IEEE80211_MODE_11B;
778 case IFM_IEEE80211_11G:
779 newphymode = IEEE80211_MODE_11G;
781 case IFM_IEEE80211_FH:
782 newphymode = IEEE80211_MODE_FH;
784 case IFM_IEEE80211_11NA:
785 newphymode = IEEE80211_MODE_11NA;
787 case IFM_IEEE80211_11NG:
788 newphymode = IEEE80211_MODE_11NG;
791 newphymode = IEEE80211_MODE_AUTO;
797 * Turbo mode is an ``option''.
798 * XXX does not apply to AUTO
800 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
801 if (newphymode == IEEE80211_MODE_11A) {
802 if (ic->ic_flags & IEEE80211_F_TURBOP)
803 newphymode = IEEE80211_MODE_TURBO_A;
805 newphymode = IEEE80211_MODE_STURBO_A;
806 } else if (newphymode == IEEE80211_MODE_11G)
807 newphymode = IEEE80211_MODE_TURBO_G;
813 * Next, the fixed/variable rate.
815 newrate = ic->ic_fixed_rate;
816 if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
818 * Convert media subtype to rate.
820 newrate = ieee80211_media2rate(ime->ifm_media);
821 if (newrate == 0 || !checkrate(ic, newphymode, newrate))
824 newrate = IEEE80211_FIXED_RATE_NONE;
827 * Deduce new operating mode but don't install it just yet.
829 if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) ==
830 (IFM_IEEE80211_ADHOC|IFM_FLAG0))
831 newopmode = IEEE80211_M_AHDEMO;
832 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
833 newopmode = IEEE80211_M_HOSTAP;
834 else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
835 newopmode = IEEE80211_M_IBSS;
836 else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
837 newopmode = IEEE80211_M_MONITOR;
839 newopmode = IEEE80211_M_STA;
842 * Handle phy mode change.
844 if (ic->ic_des_mode != newphymode) { /* change phy mode */
845 ic->ic_des_mode = newphymode;
850 * Committed to changes, install the rate setting.
852 if (ic->ic_fixed_rate != newrate) {
853 ic->ic_fixed_rate = newrate; /* set fixed tx rate */
858 * Handle operating mode change.
860 if (ic->ic_opmode != newopmode) {
861 ic->ic_opmode = newopmode;
863 case IEEE80211_M_AHDEMO:
864 case IEEE80211_M_HOSTAP:
865 case IEEE80211_M_STA:
866 case IEEE80211_M_MONITOR:
867 case IEEE80211_M_WDS:
868 ic->ic_flags &= ~IEEE80211_F_IBSSON;
870 case IEEE80211_M_IBSS:
871 ic->ic_flags |= IEEE80211_F_IBSSON;
875 * Yech, slot time may change depending on the
876 * operating mode so reset it to be sure everything
877 * is setup appropriately.
879 ieee80211_reset_erp(ic);
880 ieee80211_wme_initparams(ic); /* after opmode change */
885 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
891 * Common code to calculate the media status word
892 * from the operating mode and channel state.
895 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
899 status = IFM_IEEE80211;
901 case IEEE80211_M_STA:
903 case IEEE80211_M_IBSS:
904 status |= IFM_IEEE80211_ADHOC;
906 case IEEE80211_M_HOSTAP:
907 status |= IFM_IEEE80211_HOSTAP;
909 case IEEE80211_M_MONITOR:
910 status |= IFM_IEEE80211_MONITOR;
912 case IEEE80211_M_AHDEMO:
913 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
915 case IEEE80211_M_WDS:
916 /* should not come here */
919 if (IEEE80211_IS_CHAN_HTA(chan)) {
920 status |= IFM_IEEE80211_11NA;
921 } else if (IEEE80211_IS_CHAN_HTG(chan)) {
922 status |= IFM_IEEE80211_11NG;
923 } else if (IEEE80211_IS_CHAN_A(chan)) {
924 status |= IFM_IEEE80211_11A;
925 } else if (IEEE80211_IS_CHAN_B(chan)) {
926 status |= IFM_IEEE80211_11B;
927 } else if (IEEE80211_IS_CHAN_ANYG(chan)) {
928 status |= IFM_IEEE80211_11G;
929 } else if (IEEE80211_IS_CHAN_FHSS(chan)) {
930 status |= IFM_IEEE80211_FH;
932 /* XXX else complain? */
934 if (IEEE80211_IS_CHAN_TURBO(chan))
935 status |= IFM_IEEE80211_TURBO;
941 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
943 struct ieee80211com *ic;
944 enum ieee80211_phymode mode;
945 const struct ieee80211_rateset *rs;
947 ic = ieee80211_find_instance(ifp);
949 if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
952 imr->ifm_status = IFM_AVALID;
954 * NB: use the current channel's mode to lock down a xmit
955 * rate only when running; otherwise we may have a mismatch
956 * in which case the rate will not be convertible.
958 if (ic->ic_state == IEEE80211_S_RUN) {
959 imr->ifm_status |= IFM_ACTIVE;
960 mode = ieee80211_chan2mode(ic->ic_curchan);
962 mode = IEEE80211_MODE_AUTO;
963 imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan);
965 * Calculate a current rate if possible.
967 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
969 * A fixed rate is set, report that.
971 imr->ifm_active |= ieee80211_rate2media(ic,
972 ic->ic_fixed_rate, mode);
973 } else if (ic->ic_opmode == IEEE80211_M_STA) {
975 * In station mode report the current transmit rate.
978 rs = &ic->ic_bss->ni_rates;
979 imr->ifm_active |= ieee80211_rate2media(ic,
980 rs->rs_rates[ic->ic_bss->ni_txrate], mode);
982 imr->ifm_active |= IFM_AUTO;
986 * Set the current phy mode and recalculate the active channel
987 * set based on the available channels for this mode. Also
988 * select a new default/current channel if the current one is
989 * inappropriate for this mode.
992 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
995 * Adjust basic rates in 11b/11g supported rate set.
996 * Note that if operating on a hal/quarter rate channel
997 * this is a noop as those rates sets are different
1000 if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1001 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode], mode);
1003 ic->ic_curmode = mode;
1004 ieee80211_reset_erp(ic); /* reset ERP state */
1005 ieee80211_wme_initparams(ic); /* reset WME stat */
1011 * Return the phy mode for with the specified channel.
1013 enum ieee80211_phymode
1014 ieee80211_chan2mode(const struct ieee80211_channel *chan)
1017 if (IEEE80211_IS_CHAN_HTA(chan))
1018 return IEEE80211_MODE_11NA;
1019 else if (IEEE80211_IS_CHAN_HTG(chan))
1020 return IEEE80211_MODE_11NG;
1021 else if (IEEE80211_IS_CHAN_108G(chan))
1022 return IEEE80211_MODE_TURBO_G;
1023 else if (IEEE80211_IS_CHAN_ST(chan))
1024 return IEEE80211_MODE_STURBO_A;
1025 else if (IEEE80211_IS_CHAN_TURBO(chan))
1026 return IEEE80211_MODE_TURBO_A;
1027 else if (IEEE80211_IS_CHAN_A(chan))
1028 return IEEE80211_MODE_11A;
1029 else if (IEEE80211_IS_CHAN_ANYG(chan))
1030 return IEEE80211_MODE_11G;
1031 else if (IEEE80211_IS_CHAN_B(chan))
1032 return IEEE80211_MODE_11B;
1033 else if (IEEE80211_IS_CHAN_FHSS(chan))
1034 return IEEE80211_MODE_FH;
1036 /* NB: should not get here */
1037 printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1038 __func__, chan->ic_freq, chan->ic_flags);
1039 return IEEE80211_MODE_11B;
1043 u_int match; /* rate + mode */
1044 u_int media; /* if_media rate */
1048 findmedia(const struct ratemedia rates[], int n, u_int match)
1052 for (i = 0; i < n; i++)
1053 if (rates[i].match == match)
1054 return rates[i].media;
1059 * Convert IEEE80211 rate value to ifmedia subtype.
1060 * Rate is either a legacy rate in units of 0.5Mbps
1064 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1066 #define N(a) (sizeof(a) / sizeof(a[0]))
1067 static const struct ratemedia rates[] = {
1068 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1069 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1070 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1071 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1072 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1073 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1074 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1075 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1076 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1077 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1078 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1079 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1080 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1081 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1082 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1083 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1084 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1085 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1086 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1087 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1088 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1089 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1090 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1091 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1092 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1093 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1094 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1095 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1096 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1097 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1098 /* NB: OFDM72 doesn't realy exist so we don't handle it */
1100 static const struct ratemedia htrates[] = {
1101 { 0, IFM_IEEE80211_MCS },
1102 { 1, IFM_IEEE80211_MCS },
1103 { 2, IFM_IEEE80211_MCS },
1104 { 3, IFM_IEEE80211_MCS },
1105 { 4, IFM_IEEE80211_MCS },
1106 { 5, IFM_IEEE80211_MCS },
1107 { 6, IFM_IEEE80211_MCS },
1108 { 7, IFM_IEEE80211_MCS },
1109 { 8, IFM_IEEE80211_MCS },
1110 { 9, IFM_IEEE80211_MCS },
1111 { 10, IFM_IEEE80211_MCS },
1112 { 11, IFM_IEEE80211_MCS },
1113 { 12, IFM_IEEE80211_MCS },
1114 { 13, IFM_IEEE80211_MCS },
1115 { 14, IFM_IEEE80211_MCS },
1116 { 15, IFM_IEEE80211_MCS },
1121 * Check 11n rates first for match as an MCS.
1123 if (mode == IEEE80211_MODE_11NA) {
1124 if (rate & IEEE80211_RATE_MCS) {
1125 rate &= ~IEEE80211_RATE_MCS;
1126 m = findmedia(htrates, N(htrates), rate);
1128 return m | IFM_IEEE80211_11NA;
1130 } else if (mode == IEEE80211_MODE_11NG) {
1131 /* NB: 12 is ambiguous, it will be treated as an MCS */
1132 if (rate & IEEE80211_RATE_MCS) {
1133 rate &= ~IEEE80211_RATE_MCS;
1134 m = findmedia(htrates, N(htrates), rate);
1136 return m | IFM_IEEE80211_11NG;
1139 rate &= IEEE80211_RATE_VAL;
1141 case IEEE80211_MODE_11A:
1142 case IEEE80211_MODE_11NA:
1143 case IEEE80211_MODE_TURBO_A:
1144 case IEEE80211_MODE_STURBO_A:
1145 return findmedia(rates, N(rates), rate | IFM_IEEE80211_11A);
1146 case IEEE80211_MODE_11B:
1147 return findmedia(rates, N(rates), rate | IFM_IEEE80211_11B);
1148 case IEEE80211_MODE_FH:
1149 return findmedia(rates, N(rates), rate | IFM_IEEE80211_FH);
1150 case IEEE80211_MODE_AUTO:
1151 /* NB: ic may be NULL for some drivers */
1152 if (ic && ic->ic_phytype == IEEE80211_T_FH)
1153 return findmedia(rates, N(rates),
1154 rate | IFM_IEEE80211_FH);
1155 /* NB: hack, 11g matches both 11b+11a rates */
1157 case IEEE80211_MODE_11G:
1158 case IEEE80211_MODE_11NG:
1159 case IEEE80211_MODE_TURBO_G:
1160 return findmedia(rates, N(rates), rate | IFM_IEEE80211_11G);
1167 ieee80211_media2rate(int mword)
1169 #define N(a) (sizeof(a) / sizeof(a[0]))
1170 static const int ieeerates[] = {
1174 2, /* IFM_IEEE80211_FH1 */
1175 4, /* IFM_IEEE80211_FH2 */
1176 2, /* IFM_IEEE80211_DS1 */
1177 4, /* IFM_IEEE80211_DS2 */
1178 11, /* IFM_IEEE80211_DS5 */
1179 22, /* IFM_IEEE80211_DS11 */
1180 44, /* IFM_IEEE80211_DS22 */
1181 12, /* IFM_IEEE80211_OFDM6 */
1182 18, /* IFM_IEEE80211_OFDM9 */
1183 24, /* IFM_IEEE80211_OFDM12 */
1184 36, /* IFM_IEEE80211_OFDM18 */
1185 48, /* IFM_IEEE80211_OFDM24 */
1186 72, /* IFM_IEEE80211_OFDM36 */
1187 96, /* IFM_IEEE80211_OFDM48 */
1188 108, /* IFM_IEEE80211_OFDM54 */
1189 144, /* IFM_IEEE80211_OFDM72 */
1190 0, /* IFM_IEEE80211_DS354k */
1191 0, /* IFM_IEEE80211_DS512k */
1192 6, /* IFM_IEEE80211_OFDM3 */
1193 9, /* IFM_IEEE80211_OFDM4 */
1194 54, /* IFM_IEEE80211_OFDM27 */
1195 -1, /* IFM_IEEE80211_MCS */
1197 return IFM_SUBTYPE(mword) < N(ieeerates) ?
1198 ieeerates[IFM_SUBTYPE(mword)] : 0;