]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net80211/ieee80211.c
This commit was generated by cvs2svn to compensate for changes in r172767,
[FreeBSD/FreeBSD.git] / sys / net80211 / ieee80211.c
1 /*-
2  * Copyright (c) 2001 Atsushi Onoe
3  * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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  *
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.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 /*
31  * IEEE 802.11 generic handler
32  */
33
34 #include <sys/param.h>
35 #include <sys/systm.h> 
36 #include <sys/kernel.h>
37  
38 #include <sys/socket.h>
39
40 #include <net/if.h>
41 #include <net/if_media.h>
42 #include <net/ethernet.h>
43
44 #include <net80211/ieee80211_var.h>
45
46 #include <net/bpf.h>
47
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 */
59 };
60
61 /*
62  * Default supported rates for 802.11 operation (in IEEE .5Mb units).
63  */
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 } };
76 #undef B
77
78 static  int media_status(enum ieee80211_opmode ,
79                 const struct ieee80211_channel *);
80
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);
88
89 static void
90 ieee80211_add_vap(struct ieee80211com *ic)
91 {
92 #define N(a)    (sizeof(a)/sizeof(a[0]))
93         int i;
94         uint8_t b;
95
96         mtx_lock(&ieee80211_vap_mtx);
97         ic->ic_vap = 0;
98         for (i = 0; i < N(ieee80211_vapmap) && ieee80211_vapmap[i] == 0xff; i++)
99                 ic->ic_vap += NBBY;
100         if (i == N(ieee80211_vapmap))
101                 panic("vap table full");
102         for (b = ieee80211_vapmap[i]; b & 1; b >>= 1)
103                 ic->ic_vap++;
104         setbit(ieee80211_vapmap, ic->ic_vap);
105         SLIST_INSERT_HEAD(&ieee80211_list, ic, ic_next);
106         mtx_unlock(&ieee80211_vap_mtx);
107 #undef N
108 }
109
110 static void
111 ieee80211_remove_vap(struct ieee80211com *ic)
112 {
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);
121 }
122
123 /*
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.
131  */
132 static int
133 ieee80211_default_reset(struct ifnet *ifp)
134 {
135         return ENETRESET;
136 }
137
138 /*
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.
142  */
143 static void
144 ieee80211_chan_init(struct ieee80211com *ic)
145 {
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; \
149 } while (0)
150         struct ieee80211_channel *c;
151         int i;
152
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);
163                 /*
164                  * Identify mode capabilities.
165                  */
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);
184         }
185         /* initialize candidate channels to all available */
186         memcpy(ic->ic_chan_active, ic->ic_chan_avail,
187                 sizeof(ic->ic_chan_avail));
188
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];
194
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);
201
202         /*
203          * Set auto mode to reset active channel state and any desired channel.
204          */
205         (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
206 #undef DEFAULTRATES
207 }
208
209 void
210 ieee80211_ifattach(struct ieee80211com *ic)
211 {
212         struct ifnet *ifp = ic->ic_ifp;
213
214         ether_ifattach(ifp, ic->ic_myaddr);
215         ifp->if_output = ieee80211_output;
216
217         bpfattach2(ifp, DLT_IEEE802_11,
218             sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf);
219
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;
231         }
232
233         /*
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.
237          */
238         ieee80211_chan_init(ic);
239
240         if (ic->ic_caps & IEEE80211_C_BGSCAN)   /* enable if capable */
241                 ic->ic_flags |= IEEE80211_F_BGSCAN;
242 #if 0
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;
246 #endif
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 */
250
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");
256
257         ic->ic_lintval = ic->ic_bintval;
258         ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
259
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);
266
267         ieee80211_add_vap(ic);
268
269         ieee80211_sysctl_attach(ic);            /* NB: requires ic_vap */
270
271         /*
272          * Install a default reset method for the ioctl support.
273          * The driver is expected to fill this in before calling us.
274          */
275         if (ic->ic_reset == NULL)
276                 ic->ic_reset = ieee80211_default_reset;
277
278         KASSERT(ifp->if_spare2 == NULL, ("oops, hosed"));
279         ifp->if_spare2 = ic;                    /* XXX temp backpointer */
280 }
281
282 void
283 ieee80211_ifdetach(struct ieee80211com *ic)
284 {
285         struct ifnet *ifp = ic->ic_ifp;
286
287         ieee80211_remove_vap(ic);
288
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);
298
299         IEEE80211_LOCK_DESTROY(ic);
300         IEEE80211_BEACON_LOCK_DESTROY(ic);
301
302         bpfdetach(ifp);
303         ether_ifdetach(ifp);
304 }
305
306 static __inline int
307 mapgsm(u_int freq, u_int flags)
308 {
309         freq *= 10;
310         if (flags & IEEE80211_CHAN_QUARTER)
311                 freq += 5;
312         else if (flags & IEEE80211_CHAN_HALF)
313                 freq += 10;
314         else
315                 freq += 20;
316         /* NB: there is no 907/20 wide but leave room */
317         return (freq - 906*10) / 5;
318 }
319
320 static __inline int
321 mappsb(u_int freq, u_int flags)
322 {
323         return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
324 }
325
326 /*
327  * Convert MHz frequency to IEEE channel number.
328  */
329 int
330 ieee80211_mhz2ieee(u_int freq, u_int flags)
331 {
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 */
336                 if (freq == 2484)
337                         return 14;
338                 if (freq < 2484)
339                         return ((int) freq - 2407) / 5;
340                 else
341                         return 15 + ((freq - 2512) / 20);
342         } else if (flags & IEEE80211_CHAN_5GHZ) {       /* 5Ghz band */
343                 if (freq <= 5000) {
344                         /* XXX check regdomain? */
345                         if (IS_FREQ_IN_PSB(freq))
346                                 return mappsb(freq, flags);
347                         return (freq - 4000) / 5;
348                 } else
349                         return (freq - 5000) / 5;
350         } else {                                /* either, guess */
351                 if (freq == 2484)
352                         return 14;
353                 if (freq < 2484) {
354                         if (907 <= freq && freq <= 922)
355                                 return mapgsm(freq, flags);
356                         return ((int) freq - 2407) / 5;
357                 }
358                 if (freq < 5000) {
359                         if (IS_FREQ_IN_PSB(freq))
360                                 return mappsb(freq, flags);
361                         else if (freq > 4900)
362                                 return (freq - 4000) / 5;
363                         else
364                                 return 15 + ((freq - 2512) / 20);
365                 }
366                 return (freq - 5000) / 5;
367         }
368 #undef IS_FREQ_IN_PSB
369 }
370
371 /*
372  * Convert channel to IEEE channel number.
373  */
374 int
375 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
376 {
377         if (c == NULL) {
378                 if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
379                 return 0;               /* XXX */
380         }
381         return (c == IEEE80211_CHAN_ANYC ?  IEEE80211_CHAN_ANY : c->ic_ieee);
382 }
383
384 /*
385  * Convert IEEE channel number to MHz frequency.
386  */
387 u_int
388 ieee80211_ieee2mhz(u_int chan, u_int flags)
389 {
390         if (flags & IEEE80211_CHAN_GSM)
391                 return 907 + 5 * (chan / 10);
392         if (flags & IEEE80211_CHAN_2GHZ) {      /* 2GHz band */
393                 if (chan == 14)
394                         return 2484;
395                 if (chan < 14)
396                         return 2407 + chan*5;
397                 else
398                         return 2512 + ((chan-15)*20);
399         } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
400                 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
401                         chan -= 37;
402                         return 4940 + chan*5 + (chan % 5 ? 2 : 0);
403                 }
404                 return 5000 + (chan*5);
405         } else {                                /* either, guess */
406                 /* XXX can't distinguish PSB+GSM channels */
407                 if (chan == 14)
408                         return 2484;
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);
414         }
415 }
416
417 /*
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.
421  */
422 struct ieee80211_channel *
423 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
424 {
425         struct ieee80211_channel *c;
426         int i;
427
428         flags &= IEEE80211_CHAN_ALLTURBO;
429         c = ic->ic_prevchan;
430         if (c != NULL && c->ic_freq == freq &&
431             (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
432                 return c;
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)
438                         return c;
439         }
440         return NULL;
441 }
442
443 static void
444 addmedia(struct ieee80211com *ic, int mode, int mword)
445 {
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 */
461         };
462         u_int mopt;
463
464         KASSERT(mode < IEEE80211_MODE_MAX, ("bad mode %u", mode));
465         mopt = mopts[mode];
466         KASSERT(mopt != 0 || mode == IEEE80211_MODE_AUTO,
467             ("no media mapping for mode %u", mode));
468
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);
478 #undef ADD
479 #undef TURBO
480 }
481
482 /*
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.
486  */
487 void
488 ieee80211_media_init(struct ieee80211com *ic,
489         ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
490 {
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;
495
496         /* NB: this works because the structure is initialized to zero */
497         if (LIST_EMPTY(&ic->ic_media.ifm_list)) {
498                 /*
499                  * Do late attach work that must wait for any subclass
500                  * (i.e. driver) work such as overriding methods.
501                  */
502                 ieee80211_node_lateattach(ic);
503         } else {
504                 /*
505                  * We are re-initializing the channel list; clear
506                  * the existing media state as the media routines
507                  * don't suppress duplicates.
508                  */
509                 ifmedia_removeall(&ic->ic_media);
510                 ieee80211_chan_init(ic);
511         }
512         ieee80211_power_lateattach(ic);
513
514         /*
515          * Fill in media characteristics.
516          */
517         ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
518         maxrate = 0;
519         /*
520          * Add media for legacy operating modes.
521          */
522         memset(&allrates, 0, sizeof(allrates));
523         for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
524                 if (isclr(ic->ic_modecaps, mode))
525                         continue;
526                 addmedia(ic, mode, IFM_AUTO);
527                 if (mode == IEEE80211_MODE_AUTO)
528                         continue;
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);
533                         if (mword == 0)
534                                 continue;
535                         addmedia(ic, mode, mword);
536                         /*
537                          * Add legacy rate to the collection of all rates.
538                          */
539                         r = rate & IEEE80211_RATE_VAL;
540                         for (j = 0; j < allrates.rs_nrates; j++)
541                                 if (allrates.rs_rates[j] == r)
542                                         break;
543                         if (j == allrates.rs_nrates) {
544                                 /* unique, add to the set */
545                                 allrates.rs_rates[j] = r;
546                                 allrates.rs_nrates++;
547                         }
548                         rate = (rate & IEEE80211_RATE_VAL) / 2;
549                         if (rate > maxrate)
550                                 maxrate = rate;
551                 }
552         }
553         for (i = 0; i < allrates.rs_nrates; i++) {
554                 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
555                                 IEEE80211_MODE_AUTO);
556                 if (mword == 0)
557                         continue;
558                 /* NB: remove media options from mword */
559                 addmedia(ic, IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
560         }
561         /*
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.
566          */
567         for (; mode < IEEE80211_MODE_MAX; mode++) {
568                 if (isclr(ic->ic_modecaps, mode))
569                         continue;
570                 addmedia(ic, mode, IFM_AUTO);
571                 addmedia(ic, mode, IFM_IEEE80211_MCS);
572         }
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];
580         }
581
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);
585
586         if (maxrate)
587                 ifp->if_baudrate = IF_Mbps(maxrate);
588 }
589
590 const struct ieee80211_rateset *
591 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
592 {
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];
602         }
603         return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
604 }
605
606 void
607 ieee80211_announce(struct ieee80211com *ic)
608 {
609         struct ifnet *ifp = ic->ic_ifp;
610         int i, mode, rate, mword;
611         const struct ieee80211_rateset *rs;
612
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))
616                         continue;
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);
621                         if (mword == 0)
622                                 continue;
623                         rate = ieee80211_media2rate(mword);
624                         printf("%s%d%sMbps", (i != 0 ? " " : ""),
625                             rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
626                 }
627                 printf("\n");
628         }
629         ieee80211_ht_announce(ic);
630 }
631
632 void
633 ieee80211_announce_channels(struct ieee80211com *ic)
634 {
635         const struct ieee80211_channel *c;
636         char type;
637         int i, cw;
638
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))
643                         type = 'S';
644                 else if (IEEE80211_IS_CHAN_108A(c))
645                         type = 'T';
646                 else if (IEEE80211_IS_CHAN_108G(c))
647                         type = 'G';
648                 else if (IEEE80211_IS_CHAN_HT(c))
649                         type = 'n';
650                 else if (IEEE80211_IS_CHAN_A(c))
651                         type = 'a';
652                 else if (IEEE80211_IS_CHAN_ANYG(c))
653                         type = 'g';
654                 else if (IEEE80211_IS_CHAN_B(c))
655                         type = 'b';
656                 else
657                         type = 'f';
658                 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
659                         cw = 40;
660                 else if (IEEE80211_IS_CHAN_HALF(c))
661                         cw = 10;
662                 else if (IEEE80211_IS_CHAN_QUARTER(c))
663                         cw = 5;
664                 else
665                         cw = 20;
666                 printf("%4d  %4d%c %2d%c %6d  %4d.%d  %4d.%d\n"
667                         , c->ic_ieee, c->ic_freq, type
668                         , cw
669                         , IEEE80211_IS_CHAN_HT40U(c) ? '+' :
670                           IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
671                         , c->ic_maxregpower
672                         , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
673                         , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
674                 );
675         }
676 }
677
678 /*
679  * Find an instance by it's mac address.
680  */
681 struct ieee80211com *
682 ieee80211_find_vap(const uint8_t mac[IEEE80211_ADDR_LEN])
683 {
684         struct ieee80211com *ic;
685
686         /* XXX lock */
687         SLIST_FOREACH(ic, &ieee80211_list, ic_next)
688                 if (IEEE80211_ADDR_EQ(mac, ic->ic_myaddr))
689                         return ic;
690         return NULL;
691 }
692
693 static struct ieee80211com *
694 ieee80211_find_instance(struct ifnet *ifp)
695 {
696         struct ieee80211com *ic;
697
698         /* XXX lock */
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)
702                         return ic;
703         return NULL;
704 }
705
706 static int
707 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
708 {
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)
714                         return i;
715         return -1;
716 #undef IEEERATE
717 }
718
719 /*
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).
723  */
724 static int
725 checkrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
726 {
727
728         /*
729          * Check the rate table for the specified/current phy.
730          */
731         if (mode == IEEE80211_MODE_AUTO) {
732                 int i;
733                 /*
734                  * In autoselect mode search for the rate.
735                  */
736                 for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
737                         if (isset(ic->ic_modecaps, i) &&
738                             findrate(ic, i, rate) != -1)
739                                 return 1;
740                 }
741                 return 0;
742         } else {
743                 /*
744                  * Mode is fixed, check for rate.
745                  */
746                 return (findrate(ic, mode, rate) != -1);
747         }
748 }
749
750 /*
751  * Handle a media change request.
752  */
753 int
754 ieee80211_media_change(struct ifnet *ifp)
755 {
756         struct ieee80211com *ic;
757         struct ifmedia_entry *ime;
758         enum ieee80211_opmode newopmode;
759         enum ieee80211_phymode newphymode;
760         int newrate, error = 0;
761
762         ic = ieee80211_find_instance(ifp);
763         if (!ic) {
764                 if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
765                 return EINVAL;
766         }
767         ime = ic->ic_media.ifm_cur;
768         /*
769          * First, identify the phy mode.
770          */
771         switch (IFM_MODE(ime->ifm_media)) {
772         case IFM_IEEE80211_11A:
773                 newphymode = IEEE80211_MODE_11A;
774                 break;
775         case IFM_IEEE80211_11B:
776                 newphymode = IEEE80211_MODE_11B;
777                 break;
778         case IFM_IEEE80211_11G:
779                 newphymode = IEEE80211_MODE_11G;
780                 break;
781         case IFM_IEEE80211_FH:
782                 newphymode = IEEE80211_MODE_FH;
783                 break;
784         case IFM_IEEE80211_11NA:
785                 newphymode = IEEE80211_MODE_11NA;
786                 break;
787         case IFM_IEEE80211_11NG:
788                 newphymode = IEEE80211_MODE_11NG;
789                 break;
790         case IFM_AUTO:
791                 newphymode = IEEE80211_MODE_AUTO;
792                 break;
793         default:
794                 return EINVAL;
795         }
796         /*
797          * Turbo mode is an ``option''.
798          * XXX does not apply to AUTO
799          */
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;
804                         else
805                                 newphymode = IEEE80211_MODE_STURBO_A;
806                 } else if (newphymode == IEEE80211_MODE_11G)
807                         newphymode = IEEE80211_MODE_TURBO_G;
808                 else
809                         return EINVAL;
810         }
811         /* XXX HT40 +/- */
812         /*
813          * Next, the fixed/variable rate.
814          */
815         newrate = ic->ic_fixed_rate;
816         if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
817                 /*
818                  * Convert media subtype to rate.
819                  */
820                 newrate = ieee80211_media2rate(ime->ifm_media);
821                 if (newrate == 0 || !checkrate(ic, newphymode, newrate))
822                         return EINVAL;
823         } else
824                 newrate = IEEE80211_FIXED_RATE_NONE;
825
826         /*
827          * Deduce new operating mode but don't install it just yet.
828          */
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;
838         else
839                 newopmode = IEEE80211_M_STA;
840
841         /*
842          * Handle phy mode change.
843          */
844         if (ic->ic_des_mode != newphymode) {            /* change phy mode */
845                 ic->ic_des_mode = newphymode;
846                 error = ENETRESET;
847         }
848
849         /*
850          * Committed to changes, install the rate setting.
851          */
852         if (ic->ic_fixed_rate != newrate) {
853                 ic->ic_fixed_rate = newrate;            /* set fixed tx rate */
854                 error = ENETRESET;
855         }
856
857         /*
858          * Handle operating mode change.
859          */
860         if (ic->ic_opmode != newopmode) {
861                 ic->ic_opmode = newopmode;
862                 switch (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;
869                         break;
870                 case IEEE80211_M_IBSS:
871                         ic->ic_flags |= IEEE80211_F_IBSSON;
872                         break;
873                 }
874                 /*
875                  * Yech, slot time may change depending on the
876                  * operating mode so reset it to be sure everything
877                  * is setup appropriately.
878                  */
879                 ieee80211_reset_erp(ic);
880                 ieee80211_wme_initparams(ic);   /* after opmode change */
881                 error = ENETRESET;
882         }
883 #ifdef notdef
884         if (error == 0)
885                 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
886 #endif
887         return error;
888 }
889
890 /*
891  * Common code to calculate the media status word
892  * from the operating mode and channel state.
893  */
894 static int
895 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
896 {
897         int status;
898
899         status = IFM_IEEE80211;
900         switch (opmode) {
901         case IEEE80211_M_STA:
902                 break;
903         case IEEE80211_M_IBSS:
904                 status |= IFM_IEEE80211_ADHOC;
905                 break;
906         case IEEE80211_M_HOSTAP:
907                 status |= IFM_IEEE80211_HOSTAP;
908                 break;
909         case IEEE80211_M_MONITOR:
910                 status |= IFM_IEEE80211_MONITOR;
911                 break;
912         case IEEE80211_M_AHDEMO:
913                 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
914                 break;
915         case IEEE80211_M_WDS:
916                 /* should not come here */
917                 break;
918         }
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;
931         }
932         /* XXX else complain? */
933
934         if (IEEE80211_IS_CHAN_TURBO(chan))
935                 status |= IFM_IEEE80211_TURBO;
936
937         return status;
938 }
939
940 void
941 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
942 {
943         struct ieee80211com *ic;
944         enum ieee80211_phymode mode;
945         const struct ieee80211_rateset *rs;
946
947         ic = ieee80211_find_instance(ifp);
948         if (!ic) {
949                 if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
950                 return;
951         }
952         imr->ifm_status = IFM_AVALID;
953         /*
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.
957          */
958         if (ic->ic_state == IEEE80211_S_RUN) {
959                 imr->ifm_status |= IFM_ACTIVE;
960                 mode = ieee80211_chan2mode(ic->ic_curchan);
961         } else
962                 mode = IEEE80211_MODE_AUTO;
963         imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan);
964         /*
965          * Calculate a current rate if possible.
966          */
967         if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
968                 /*
969                  * A fixed rate is set, report that.
970                  */
971                 imr->ifm_active |= ieee80211_rate2media(ic,
972                         ic->ic_fixed_rate, mode);
973         } else if (ic->ic_opmode == IEEE80211_M_STA) {
974                 /*
975                  * In station mode report the current transmit rate.
976                  * XXX HT rate
977                  */
978                 rs = &ic->ic_bss->ni_rates;
979                 imr->ifm_active |= ieee80211_rate2media(ic,
980                         rs->rs_rates[ic->ic_bss->ni_txrate], mode);
981         } else
982                 imr->ifm_active |= IFM_AUTO;
983 }
984
985 /*
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.
990  */
991 int
992 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
993 {
994         /*
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
998          * and used instead.
999          */
1000         if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1001                 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode], mode);
1002
1003         ic->ic_curmode = mode;
1004         ieee80211_reset_erp(ic);        /* reset ERP state */
1005         ieee80211_wme_initparams(ic);   /* reset WME stat */
1006
1007         return 0;
1008 }
1009
1010 /*
1011  * Return the phy mode for with the specified channel.
1012  */
1013 enum ieee80211_phymode
1014 ieee80211_chan2mode(const struct ieee80211_channel *chan)
1015 {
1016
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;
1035
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;
1040 }
1041
1042 struct ratemedia {
1043         u_int   match;  /* rate + mode */
1044         u_int   media;  /* if_media rate */
1045 };
1046
1047 static int
1048 findmedia(const struct ratemedia rates[], int n, u_int match)
1049 {
1050         int i;
1051
1052         for (i = 0; i < n; i++)
1053                 if (rates[i].match == match)
1054                         return rates[i].media;
1055         return IFM_AUTO;
1056 }
1057
1058 /*
1059  * Convert IEEE80211 rate value to ifmedia subtype.
1060  * Rate is either a legacy rate in units of 0.5Mbps
1061  * or an MCS index.
1062  */
1063 int
1064 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1065 {
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 */
1099         };
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 },
1117         };
1118         int m;
1119
1120         /*
1121          * Check 11n rates first for match as an MCS.
1122          */
1123         if (mode == IEEE80211_MODE_11NA) {
1124                 if (rate & IEEE80211_RATE_MCS) {
1125                         rate &= ~IEEE80211_RATE_MCS;
1126                         m = findmedia(htrates, N(htrates), rate);
1127                         if (m != IFM_AUTO)
1128                                 return m | IFM_IEEE80211_11NA;
1129                 }
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);
1135                         if (m != IFM_AUTO)
1136                                 return m | IFM_IEEE80211_11NG;
1137                 }
1138         }
1139         rate &= IEEE80211_RATE_VAL;
1140         switch (mode) {
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 */
1156                 /* fall thru... */
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);
1161         }
1162         return IFM_AUTO;
1163 #undef N
1164 }
1165
1166 int
1167 ieee80211_media2rate(int mword)
1168 {
1169 #define N(a)    (sizeof(a) / sizeof(a[0]))
1170         static const int ieeerates[] = {
1171                 -1,             /* IFM_AUTO */
1172                 0,              /* IFM_MANUAL */
1173                 0,              /* IFM_NONE */
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 */
1196         };
1197         return IFM_SUBTYPE(mword) < N(ieeerates) ?
1198                 ieeerates[IFM_SUBTYPE(mword)] : 0;
1199 #undef N
1200 }