ieee80211.c revision 1.48.12.1 1 /* $NetBSD: ieee80211.c,v 1.48.12.1 2008/02/22 16:50:25 skrll Exp $ */
2 /*-
3 * Copyright (c) 2001 Atsushi Onoe
4 * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 #ifdef __NetBSD__
30 __KERNEL_RCSID(0, "$NetBSD: ieee80211.c,v 1.48.12.1 2008/02/22 16:50:25 skrll Exp $");
31 #endif
32 #ifdef __FreeBSD__
33 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211.c,v 1.45 2007/12/07 01:46:12 kmacy Exp $");
34 #endif
35
36 /*
37 * IEEE 802.11 generic handler
38 */
39
40 #include "opt_inet.h"
41 #include "bpfilter.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49 #include <sys/endian.h>
50 #include <sys/errno.h>
51 #include <sys/proc.h>
52 #include <sys/sysctl.h>
53
54 #include <net/if.h>
55 #include <net/if_media.h>
56 #include <net/if_arp.h>
57 #include <net/if_ether.h>
58 #include <net/if_llc.h>
59
60 #include <net80211/ieee80211_netbsd.h>
61 #include <net80211/ieee80211_var.h>
62 #include <net80211/ieee80211_sysctl.h>
63
64 #include <net/bpf.h>
65
66 #ifdef INET
67 #include <netinet/in.h>
68 #include <net/if_ether.h>
69 #endif
70
71 const char *ieee80211_phymode_name[] = {
72 "auto", /* IEEE80211_MODE_AUTO */
73 "11a", /* IEEE80211_MODE_11A */
74 "11b", /* IEEE80211_MODE_11B */
75 "11g", /* IEEE80211_MODE_11G */
76 "FH", /* IEEE80211_MODE_FH */
77 "turboA", /* IEEE80211_MODE_TURBO_A */
78 "turboG", /* IEEE80211_MODE_TURBO_G */
79 "sturboA", /* IEEE80211_MODE_STURBO_A */
80 "11na", /* IEEE80211_MODE_11NA */
81 "11ng", /* IEEE80211_MODE_11NG */
82 };
83
84 /*
85 * Default supported rates for 802.11 operation (in IEEE .5Mb units).
86 */
87 #define B(r) ((r) | IEEE80211_RATE_BASIC)
88 static const struct ieee80211_rateset ieee80211_rateset_11a =
89 { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
90 static const struct ieee80211_rateset ieee80211_rateset_half =
91 { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
92 static const struct ieee80211_rateset ieee80211_rateset_quarter =
93 { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
94 static const struct ieee80211_rateset ieee80211_rateset_11b =
95 { 4, { B(2), B(4), B(11), B(22) } };
96 /* NB: OFDM rates are handled specially based on mode */
97 static const struct ieee80211_rateset ieee80211_rateset_11g =
98 { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
99 #undef B
100
101 static int media_status(enum ieee80211_opmode ,
102 const struct ieee80211_channel *);
103
104 /* list of all instances */
105 struct ieee80211_list ieee80211_list =
106 SLIST_HEAD_INITIALIZER(ieee80211_list);
107 static uint8_t ieee80211_vapmap[32]; /* enough for 256 */
108
109 static void
110 ieee80211_add_vap(struct ieee80211com *ic)
111 {
112 #define N(a) (sizeof(a)/sizeof(a[0]))
113 int i;
114 int s;
115 uint8_t b;
116
117 s = splnet();
118 ic->ic_vap = 0;
119 for (i = 0; i < N(ieee80211_vapmap) && ieee80211_vapmap[i] == 0xff; i++)
120 ic->ic_vap += NBBY;
121 if (i == N(ieee80211_vapmap))
122 panic("vap table full");
123 for (b = ieee80211_vapmap[i]; b & 1; b >>= 1)
124 ic->ic_vap++;
125 setbit(ieee80211_vapmap, ic->ic_vap);
126 SLIST_INSERT_HEAD(&ieee80211_list, ic, ic_next);
127 splx(s);
128 #undef N
129 }
130
131 static void
132 ieee80211_remove_vap(struct ieee80211com *ic)
133 {
134 int s;
135
136 s = splnet();
137 SLIST_REMOVE(&ieee80211_list, ic, ieee80211com, ic_next);
138 IASSERT(ic->ic_vap < sizeof(ieee80211_vapmap)*NBBY,
139 ("invalid vap id %d", ic->ic_vap));
140 IASSERT(isset(ieee80211_vapmap, ic->ic_vap),
141 ("vap id %d not allocated", ic->ic_vap));
142 clrbit(ieee80211_vapmap, ic->ic_vap);
143 splx(s);
144 }
145
146 /*
147 * Default reset method for use with the ioctl support. This
148 * method is invoked after any state change in the 802.11
149 * layer that should be propagated to the hardware but not
150 * require re-initialization of the 802.11 state machine (e.g
151 * rescanning for an ap). We always return ENETRESET which
152 * should cause the driver to re-initialize the device. Drivers
153 * can override this method to implement more optimized support.
154 */
155 static int
156 ieee80211_default_reset(struct ifnet *ifp)
157 {
158 return ENETRESET;
159 }
160
161 /*
162 * Fill in 802.11 available channel set, mark
163 * all available channels as active, and pick
164 * a default channel if not already specified.
165 */
166 static void
167 ieee80211_chan_init(struct ieee80211com *ic)
168 {
169 #define DEFAULTRATES(m, def) do { \
170 if (isset(ic->ic_modecaps, m) && ic->ic_sup_rates[m].rs_nrates == 0) \
171 ic->ic_sup_rates[m] = def; \
172 } while (0)
173 struct ieee80211_channel *c;
174 int i;
175
176 IASSERT(0 < ic->ic_nchans && ic->ic_nchans < IEEE80211_CHAN_MAX,
177 ("invalid number of channels specified: %u", ic->ic_nchans));
178 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
179 setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
180 for (i = 0; i < ic->ic_nchans; i++) {
181 c = &ic->ic_channels[i];
182 IASSERT(c->ic_flags != 0, ("channel with no flags"));
183 IASSERT(c->ic_ieee < IEEE80211_CHAN_MAX,
184 ("channel with bogus ieee number %u", c->ic_ieee));
185 setbit(ic->ic_chan_avail, c->ic_ieee);
186 /*
187 * Identify mode capabilities.
188 */
189 if (IEEE80211_IS_CHAN_A(c))
190 setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
191 if (IEEE80211_IS_CHAN_B(c))
192 setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
193 if (IEEE80211_IS_CHAN_ANYG(c))
194 setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
195 if (IEEE80211_IS_CHAN_FHSS(c))
196 setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
197 if (IEEE80211_IS_CHAN_108A(c))
198 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
199 if (IEEE80211_IS_CHAN_108G(c))
200 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
201 if (IEEE80211_IS_CHAN_ST(c))
202 setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
203 if (IEEE80211_IS_CHAN_HTA(c))
204 setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
205 if (IEEE80211_IS_CHAN_HTG(c))
206 setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
207 }
208 /* initialize candidate channels to all available */
209 memcpy(ic->ic_chan_active, ic->ic_chan_avail,
210 sizeof(ic->ic_chan_avail));
211
212 ic->ic_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
213 ic->ic_bsschan = IEEE80211_CHAN_ANYC;
214 ic->ic_prevchan = NULL;
215 /* arbitrarily pick the first channel */
216 ic->ic_curchan = &ic->ic_channels[0];
217
218 /* fillin well-known rate sets if driver has not specified */
219 DEFAULTRATES(IEEE80211_MODE_11B, ieee80211_rateset_11b);
220 DEFAULTRATES(IEEE80211_MODE_11G, ieee80211_rateset_11g);
221 DEFAULTRATES(IEEE80211_MODE_11A, ieee80211_rateset_11a);
222 DEFAULTRATES(IEEE80211_MODE_TURBO_A, ieee80211_rateset_11a);
223 DEFAULTRATES(IEEE80211_MODE_TURBO_G, ieee80211_rateset_11g);
224
225 /*
226 * Set auto mode to reset active channel state and any desired channel.
227 */
228 (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
229 #undef DEFAULTRATES
230 }
231
232 void
233 ieee80211_ifattach(struct ieee80211com *ic)
234 {
235 struct ifnet *ifp = ic->ic_ifp;
236
237 #ifdef __NetBSD__
238 net80211_init();
239 #endif /* __NetBSD__ */
240
241 ether_ifattach(ifp, ic->ic_myaddr);
242 ifp->if_output = ieee80211_output;
243
244 #if NBPFILTER > 0
245 bpfattach2(ifp, DLT_IEEE802_11,
246 sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf);
247 #endif
248
249 /* override the 802.3 setting */
250 ifp->if_hdrlen = ic->ic_headroom
251 + sizeof(struct ieee80211_qosframe_addr4)
252 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
253 + IEEE80211_WEP_EXTIVLEN;
254 /* XXX no way to recalculate on ifdetach */
255 if (ALIGN(ifp->if_hdrlen) > max_linkhdr) {
256 /* XXX sanity check... */
257 max_linkhdr = ALIGN(ifp->if_hdrlen);
258 max_hdr = max_linkhdr + max_protohdr;
259 max_datalen = MHLEN - max_hdr;
260 }
261
262 /*
263 * Fill in 802.11 available channel set, mark all
264 * available channels as active, and pick a default
265 * channel if not already specified.
266 */
267 ieee80211_chan_init(ic);
268
269 if (ic->ic_caps & IEEE80211_C_BGSCAN) /* enable if capable */
270 ic->ic_flags |= IEEE80211_F_BGSCAN;
271 #if 0
272 /* XXX not until WME+WPA issues resolved */
273 if (ic->ic_caps & IEEE80211_C_WME) /* enable if capable */
274 ic->ic_flags |= IEEE80211_F_WME;
275 #endif
276 if (ic->ic_caps & IEEE80211_C_BURST)
277 ic->ic_flags |= IEEE80211_F_BURST;
278 ic->ic_flags |= IEEE80211_F_DOTH; /* XXX out of caps, just ena */
279
280 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
281 ic->ic_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
282 ic->ic_dtim_period = IEEE80211_DTIM_DEFAULT;
283 IEEE80211_LOCK_INIT(ic, "ieee80211com");
284 IEEE80211_BEACON_LOCK_INIT(ic, "beacon");
285
286 ic->ic_lintval = ic->ic_bintval;
287 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
288
289 ieee80211_crypto_attach(ic);
290 ieee80211_node_attach(ic);
291 ieee80211_power_attach(ic);
292 ieee80211_proto_attach(ic);
293 ieee80211_ht_attach(ic);
294 ieee80211_scan_attach(ic);
295
296 ieee80211_add_vap(ic);
297
298 ieee80211_sysctl_attach(ic); /* NB: requires ic_vap */
299
300 /*
301 * Install a default reset method for the ioctl support.
302 * The driver is expected to fill this in before calling us.
303 */
304 if (ic->ic_reset == NULL)
305 ic->ic_reset = ieee80211_default_reset;
306
307 #if !defined(__NetBSD__)
308 IASSERT(ifp->if_llsoftc == NULL, ("oops, hosed"));
309 ifp->if_llsoftc = ic;
310 #endif
311 }
312
313 void
314 ieee80211_ifdetach(struct ieee80211com *ic)
315 {
316 struct ifnet *ifp = ic->ic_ifp;
317
318 ieee80211_remove_vap(ic);
319
320 ieee80211_sysctl_detach(ic);
321 ieee80211_scan_detach(ic);
322 ieee80211_ht_detach(ic);
323 /* NB: must be called before ieee80211_node_detach */
324 ieee80211_proto_detach(ic);
325 ieee80211_crypto_detach(ic);
326 ieee80211_power_detach(ic);
327 ieee80211_node_detach(ic);
328 ifmedia_delete_instance(&ic->ic_media, IFM_INST_ANY);
329
330 IEEE80211_LOCK_DESTROY(ic);
331 IEEE80211_BEACON_LOCK_DESTROY(ic);
332
333 #if NBPFILTER > 0
334 bpfdetach(ifp);
335 #endif
336 ether_ifdetach(ifp);
337 }
338
339 static __inline int
340 mapgsm(u_int freq, u_int flags)
341 {
342 freq *= 10;
343 if (flags & IEEE80211_CHAN_QUARTER)
344 freq += 5;
345 else if (flags & IEEE80211_CHAN_HALF)
346 freq += 10;
347 else
348 freq += 20;
349 /* NB: there is no 907/20 wide but leave room */
350 return (freq - 906*10) / 5;
351 }
352
353 static __inline int
354 mappsb(u_int freq, u_int flags)
355 {
356 return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
357 }
358
359 /*
360 * Convert MHz frequency to IEEE channel number.
361 */
362 int
363 ieee80211_mhz2ieee(u_int freq, u_int flags)
364 {
365 #define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
366 if (flags & IEEE80211_CHAN_GSM)
367 return mapgsm(freq, flags);
368 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
369 if (freq == 2484)
370 return 14;
371 if (freq < 2484)
372 return ((int) freq - 2407) / 5;
373 else
374 return 15 + ((freq - 2512) / 20);
375 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
376 if (freq <= 5000) {
377 /* XXX check regdomain? */
378 if (IS_FREQ_IN_PSB(freq))
379 return mappsb(freq, flags);
380 return (freq - 4000) / 5;
381 } else
382 return (freq - 5000) / 5;
383 } else { /* either, guess */
384 if (freq == 2484)
385 return 14;
386 if (freq < 2484) {
387 if (907 <= freq && freq <= 922)
388 return mapgsm(freq, flags);
389 return ((int) freq - 2407) / 5;
390 }
391 if (freq < 5000) {
392 if (IS_FREQ_IN_PSB(freq))
393 return mappsb(freq, flags);
394 else if (freq > 4900)
395 return (freq - 4000) / 5;
396 else
397 return 15 + ((freq - 2512) / 20);
398 }
399 return (freq - 5000) / 5;
400 }
401 #undef IS_FREQ_IN_PSB
402 }
403
404 /*
405 * Convert channel to IEEE channel number.
406 */
407 int
408 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
409 {
410 if (c == NULL) {
411 if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
412 return 0; /* XXX */
413 }
414 return (c == IEEE80211_CHAN_ANYC ? IEEE80211_CHAN_ANY : c->ic_ieee);
415 }
416
417 /*
418 * Convert IEEE channel number to MHz frequency.
419 */
420 u_int
421 ieee80211_ieee2mhz(u_int chan, u_int flags)
422 {
423 if (flags & IEEE80211_CHAN_GSM)
424 return 907 + 5 * (chan / 10);
425 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
426 if (chan == 14)
427 return 2484;
428 if (chan < 14)
429 return 2407 + chan*5;
430 else
431 return 2512 + ((chan-15)*20);
432 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
433 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
434 chan -= 37;
435 return 4940 + chan*5 + (chan % 5 ? 2 : 0);
436 }
437 return 5000 + (chan*5);
438 } else { /* either, guess */
439 /* XXX can't distinguish PSB+GSM channels */
440 if (chan == 14)
441 return 2484;
442 if (chan < 14) /* 0-13 */
443 return 2407 + chan*5;
444 if (chan < 27) /* 15-26 */
445 return 2512 + ((chan-15)*20);
446 return 5000 + (chan*5);
447 }
448 }
449
450 /*
451 * Locate a channel given a frequency+flags. We cache
452 * the previous lookup to optimize swithing between two
453 * channels--as happens with dynamic turbo.
454 */
455 struct ieee80211_channel *
456 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
457 {
458 struct ieee80211_channel *c;
459 int i;
460
461 flags &= IEEE80211_CHAN_ALLTURBO;
462 c = ic->ic_prevchan;
463 if (c != NULL && c->ic_freq == freq &&
464 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
465 return c;
466 /* brute force search */
467 for (i = 0; i < ic->ic_nchans; i++) {
468 c = &ic->ic_channels[i];
469 if (c->ic_freq == freq &&
470 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
471 return c;
472 }
473 return NULL;
474 }
475
476 /*
477 * Locate a channel given a channel number+flags. We cache
478 * the previous lookup to optimize switching between two
479 * channels--as happens with dynamic turbo.
480 */
481 struct ieee80211_channel *
482 ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
483 {
484 struct ieee80211_channel *c;
485 int i;
486
487 flags &= IEEE80211_CHAN_ALLTURBO;
488 c = ic->ic_prevchan;
489 if (c != NULL && c->ic_ieee == ieee &&
490 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
491 return c;
492 /* brute force search */
493 for (i = 0; i < ic->ic_nchans; i++) {
494 c = &ic->ic_channels[i];
495 if (c->ic_ieee == ieee &&
496 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
497 return c;
498 }
499 return NULL;
500 }
501
502 static void
503 addmedia(struct ieee80211com *ic, int mode, int mword)
504 {
505 #define TURBO(m) ((m) | IFM_IEEE80211_TURBO)
506 #define ADD(_ic, _s, _o) \
507 ifmedia_add(&(_ic)->ic_media, \
508 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
509 static const u_int mopts[IEEE80211_MODE_MAX] = {
510 IFM_AUTO, /* IEEE80211_MODE_AUTO */
511 IFM_IEEE80211_11A, /* IEEE80211_MODE_11A */
512 IFM_IEEE80211_11B, /* IEEE80211_MODE_11B */
513 IFM_IEEE80211_11G, /* IEEE80211_MODE_11G */
514 IFM_IEEE80211_FH, /* IEEE80211_MODE_FH */
515 TURBO(IFM_IEEE80211_11A), /* IEEE80211_MODE_TURBO_A */
516 TURBO(IFM_IEEE80211_11G), /* IEEE80211_MODE_TURBO_G */
517 TURBO(IFM_IEEE80211_11A), /* IEEE80211_MODE_STURBO_A */
518 IFM_IEEE80211_11NA, /* IEEE80211_MODE_11NA */
519 IFM_IEEE80211_11NG, /* IEEE80211_MODE_11NG */
520 };
521 u_int mopt;
522
523 IASSERT(mode < IEEE80211_MODE_MAX, ("bad mode %u", mode));
524 mopt = mopts[mode];
525 IASSERT(mopt != 0 || mode == IEEE80211_MODE_AUTO,
526 ("no media mapping for mode %u", mode));
527
528 ADD(ic, mword, mopt); /* e.g. 11a auto */
529 if (ic->ic_caps & IEEE80211_C_IBSS)
530 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
531 if (ic->ic_caps & IEEE80211_C_HOSTAP)
532 ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
533 if (ic->ic_caps & IEEE80211_C_AHDEMO)
534 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
535 if (ic->ic_caps & IEEE80211_C_MONITOR)
536 ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
537 #undef ADD
538 #undef TURBO
539 }
540
541 /*
542 * Setup the media data structures according to the channel and
543 * rate tables. This must be called by the driver after
544 * ieee80211_attach and before most anything else.
545 */
546 void
547 ieee80211_media_init(struct ieee80211com *ic,
548 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
549 {
550 struct ifnet *ifp = ic->ic_ifp;
551 int i, j, mode, rate, maxrate, mword, r;
552 const struct ieee80211_rateset *rs;
553 struct ieee80211_rateset allrates;
554
555 /* XXXNH */
556 /* NB: this works because the structure is initialized to zero */
557 if (TAILQ_EMPTY(&ic->ic_media.ifm_list)) {
558 /*
559 * Do late attach work that must wait for any subclass
560 * (i.e. driver) work such as overriding methods.
561 */
562 ieee80211_node_lateattach(ic);
563 } else {
564 /*
565 * We are re-initializing the channel list; clear
566 * the existing media state as the media routines
567 * don't suppress duplicates.
568 */
569 ifmedia_delete_instance(&ic->ic_media, IFM_INST_ANY);
570 ieee80211_chan_init(ic);
571 }
572 ieee80211_power_lateattach(ic);
573
574 #ifdef IEEE80211_NO_HOSTAP
575 ic->ic_caps &= ~IEEE80211_C_HOSTAP;
576 #endif /* IEEE80211_NO_HOSTAP */
577
578 /*
579 * Fill in media characteristics.
580 */
581 ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
582 maxrate = 0;
583 /*
584 * Add media for legacy operating modes.
585 */
586 memset(&allrates, 0, sizeof(allrates));
587 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
588 if (isclr(ic->ic_modecaps, mode))
589 continue;
590 addmedia(ic, mode, IFM_AUTO);
591 if (mode == IEEE80211_MODE_AUTO)
592 continue;
593 rs = &ic->ic_sup_rates[mode];
594 for (i = 0; i < rs->rs_nrates; i++) {
595 rate = rs->rs_rates[i];
596 mword = ieee80211_rate2media(ic, rate, mode);
597 if (mword == 0)
598 continue;
599 addmedia(ic, mode, mword);
600 /*
601 * Add legacy rate to the collection of all rates.
602 */
603 r = rate & IEEE80211_RATE_VAL;
604 for (j = 0; j < allrates.rs_nrates; j++)
605 if (allrates.rs_rates[j] == r)
606 break;
607 if (j == allrates.rs_nrates) {
608 /* unique, add to the set */
609 allrates.rs_rates[j] = r;
610 allrates.rs_nrates++;
611 }
612 rate = (rate & IEEE80211_RATE_VAL) / 2;
613 if (rate > maxrate)
614 maxrate = rate;
615 }
616 }
617 for (i = 0; i < allrates.rs_nrates; i++) {
618 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
619 IEEE80211_MODE_AUTO);
620 if (mword == 0)
621 continue;
622 /* NB: remove media options from mword */
623 addmedia(ic, IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
624 }
625 /*
626 * Add HT/11n media. Note that we do not have enough
627 * bits in the media subtype to express the MCS so we
628 * use a "placeholder" media subtype and any fixed MCS
629 * must be specified with a different mechanism.
630 */
631 for (; mode < IEEE80211_MODE_MAX; mode++) {
632 if (isclr(ic->ic_modecaps, mode))
633 continue;
634 addmedia(ic, mode, IFM_AUTO);
635 addmedia(ic, mode, IFM_IEEE80211_MCS);
636 }
637 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
638 isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
639 addmedia(ic, IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
640 /* XXX could walk htrates */
641 /* XXX known array size */
642 if (ieee80211_htrates[15] > maxrate)
643 maxrate = ieee80211_htrates[15];
644 }
645
646 /* NB: strip explicit mode; we're actually in autoselect */
647 ifmedia_set(&ic->ic_media,
648 media_status(ic->ic_opmode, ic->ic_curchan) &~ IFM_MMASK);
649
650 if (maxrate)
651 ifp->if_baudrate = IF_Mbps(maxrate);
652 }
653
654 const struct ieee80211_rateset *
655 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
656 {
657 if (IEEE80211_IS_CHAN_HALF(c))
658 return &ieee80211_rateset_half;
659 if (IEEE80211_IS_CHAN_QUARTER(c))
660 return &ieee80211_rateset_quarter;
661 if (IEEE80211_IS_CHAN_HTA(c))
662 return &ic->ic_sup_rates[IEEE80211_MODE_11A];
663 if (IEEE80211_IS_CHAN_HTG(c)) {
664 /* XXX does this work for basic rates? */
665 return &ic->ic_sup_rates[IEEE80211_MODE_11G];
666 }
667 return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
668 }
669
670 void
671 ieee80211_announce(struct ieee80211com *ic)
672 {
673 struct ifnet *ifp = ic->ic_ifp;
674 int i, mode, rate, mword;
675 const struct ieee80211_rateset *rs;
676
677 /* NB: skip AUTO since it has no rates */
678 for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
679 if (isclr(ic->ic_modecaps, mode))
680 continue;
681 aprint_normal("%s: %s rates: ", ifp->if_xname,
682 ieee80211_phymode_name[mode]);
683 rs = &ic->ic_sup_rates[mode];
684 for (i = 0; i < rs->rs_nrates; i++) {
685 mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
686 if (mword == 0)
687 continue;
688 rate = ieee80211_media2rate(mword);
689 aprint_normal("%s%d%sMbps", (i != 0 ? " " : ""),
690 rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
691 }
692 aprint_normal("\n");
693 }
694 ieee80211_ht_announce(ic);
695 }
696
697 void
698 ieee80211_announce_channels(struct ieee80211com *ic)
699 {
700 const struct ieee80211_channel *c;
701 char type;
702 int i, cw;
703
704 printf("Chan Freq CW RegPwr MinPwr MaxPwr\n");
705 for (i = 0; i < ic->ic_nchans; i++) {
706 c = &ic->ic_channels[i];
707 if (IEEE80211_IS_CHAN_ST(c))
708 type = 'S';
709 else if (IEEE80211_IS_CHAN_108A(c))
710 type = 'T';
711 else if (IEEE80211_IS_CHAN_108G(c))
712 type = 'G';
713 else if (IEEE80211_IS_CHAN_HT(c))
714 type = 'n';
715 else if (IEEE80211_IS_CHAN_A(c))
716 type = 'a';
717 else if (IEEE80211_IS_CHAN_ANYG(c))
718 type = 'g';
719 else if (IEEE80211_IS_CHAN_B(c))
720 type = 'b';
721 else
722 type = 'f';
723 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
724 cw = 40;
725 else if (IEEE80211_IS_CHAN_HALF(c))
726 cw = 10;
727 else if (IEEE80211_IS_CHAN_QUARTER(c))
728 cw = 5;
729 else
730 cw = 20;
731 printf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n"
732 , c->ic_ieee, c->ic_freq, type
733 , cw
734 , IEEE80211_IS_CHAN_HT40U(c) ? '+' :
735 IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
736 , c->ic_maxregpower
737 , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
738 , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
739 );
740 }
741 }
742
743 /*
744 * Find an instance by it's mac address.
745 */
746 struct ieee80211com *
747 ieee80211_find_vap(const uint8_t mac[IEEE80211_ADDR_LEN])
748 {
749 int s;
750 struct ieee80211com *ic;
751
752 s = splnet();
753 SLIST_FOREACH(ic, &ieee80211_list, ic_next)
754 if (IEEE80211_ADDR_EQ(mac, ic->ic_myaddr))
755 break;
756 splx(s);
757 return ic;
758 }
759
760 static struct ieee80211com *
761 ieee80211_find_instance(struct ifnet *ifp)
762 {
763 int s;
764 struct ieee80211com *ic;
765
766 s = splnet();
767 /* XXX not right for multiple instances but works for now */
768 SLIST_FOREACH(ic, &ieee80211_list, ic_next)
769 if (ic->ic_ifp == ifp)
770 break;
771 splx(s);
772 return ic;
773 }
774
775 static int
776 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
777 {
778 #define IEEERATE(_ic,_m,_i) \
779 ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
780 int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
781 for (i = 0; i < nrates; i++)
782 if (IEEERATE(ic, mode, i) == rate)
783 return i;
784 return -1;
785 #undef IEEERATE
786 }
787
788 /*
789 * Convert a media specification to a rate index and possibly a mode
790 * (if the rate is fixed and the mode is specified as ``auto'' then
791 * we need to lock down the mode so the index is meanginful).
792 */
793 static int
794 checkrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
795 {
796
797 /*
798 * Check the rate table for the specified/current phy.
799 */
800 if (mode == IEEE80211_MODE_AUTO) {
801 int i;
802 /*
803 * In autoselect mode search for the rate.
804 */
805 for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
806 if (isset(ic->ic_modecaps, i) &&
807 findrate(ic, i, rate) != -1)
808 return 1;
809 }
810 return 0;
811 } else {
812 /*
813 * Mode is fixed, check for rate.
814 */
815 return (findrate(ic, mode, rate) != -1);
816 }
817 }
818
819 /*
820 * Handle a media change request.
821 */
822 int
823 ieee80211_media_change(struct ifnet *ifp)
824 {
825 struct ieee80211com *ic;
826 struct ifmedia_entry *ime;
827 enum ieee80211_opmode newopmode;
828 enum ieee80211_phymode newphymode;
829 int newrate, error = 0;
830
831 ic = ieee80211_find_instance(ifp);
832 if (!ic) {
833 if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
834 return EINVAL;
835 }
836 ime = ic->ic_media.ifm_cur;
837 /*
838 * First, identify the phy mode.
839 */
840 switch (IFM_MODE(ime->ifm_media)) {
841 case IFM_IEEE80211_11A:
842 newphymode = IEEE80211_MODE_11A;
843 break;
844 case IFM_IEEE80211_11B:
845 newphymode = IEEE80211_MODE_11B;
846 break;
847 case IFM_IEEE80211_11G:
848 newphymode = IEEE80211_MODE_11G;
849 break;
850 case IFM_IEEE80211_FH:
851 newphymode = IEEE80211_MODE_FH;
852 break;
853 case IFM_IEEE80211_11NA:
854 newphymode = IEEE80211_MODE_11NA;
855 break;
856 case IFM_IEEE80211_11NG:
857 newphymode = IEEE80211_MODE_11NG;
858 break;
859 case IFM_AUTO:
860 newphymode = IEEE80211_MODE_AUTO;
861 break;
862 default:
863 return EINVAL;
864 }
865 /*
866 * Turbo mode is an ``option''.
867 * XXX does not apply to AUTO
868 */
869 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
870 if (newphymode == IEEE80211_MODE_11A) {
871 if (ic->ic_flags & IEEE80211_F_TURBOP)
872 newphymode = IEEE80211_MODE_TURBO_A;
873 else
874 newphymode = IEEE80211_MODE_STURBO_A;
875 } else if (newphymode == IEEE80211_MODE_11G)
876 newphymode = IEEE80211_MODE_TURBO_G;
877 else
878 return EINVAL;
879 }
880 /* XXX HT40 +/- */
881 /*
882 * Next, the fixed/variable rate.
883 */
884 newrate = ic->ic_fixed_rate;
885 if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
886 /*
887 * Convert media subtype to rate.
888 */
889 newrate = ieee80211_media2rate(ime->ifm_media);
890 if (newrate == 0 || !checkrate(ic, newphymode, newrate))
891 return EINVAL;
892 } else
893 newrate = IEEE80211_FIXED_RATE_NONE;
894
895 /*
896 * Deduce new operating mode but don't install it just yet.
897 */
898 if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) ==
899 (IFM_IEEE80211_ADHOC|IFM_FLAG0))
900 newopmode = IEEE80211_M_AHDEMO;
901 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
902 newopmode = IEEE80211_M_HOSTAP;
903 else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
904 newopmode = IEEE80211_M_IBSS;
905 else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
906 newopmode = IEEE80211_M_MONITOR;
907 else
908 newopmode = IEEE80211_M_STA;
909
910 /*
911 * Handle phy mode change.
912 */
913 if (ic->ic_des_mode != newphymode) { /* change phy mode */
914 ic->ic_des_mode = newphymode;
915 error = ENETRESET;
916 }
917
918 /*
919 * Committed to changes, install the rate setting.
920 */
921 if (ic->ic_fixed_rate != newrate) {
922 ic->ic_fixed_rate = newrate; /* set fixed tx rate */
923 error = ENETRESET;
924 }
925
926 /*
927 * Handle operating mode change.
928 */
929 if (ic->ic_opmode != newopmode) {
930 ic->ic_opmode = newopmode;
931 switch (newopmode) {
932 case IEEE80211_M_AHDEMO:
933 case IEEE80211_M_HOSTAP:
934 case IEEE80211_M_STA:
935 case IEEE80211_M_MONITOR:
936 case IEEE80211_M_WDS:
937 ic->ic_flags &= ~IEEE80211_F_IBSSON;
938 break;
939 case IEEE80211_M_IBSS:
940 ic->ic_flags |= IEEE80211_F_IBSSON;
941 break;
942 }
943 /*
944 * Yech, slot time may change depending on the
945 * operating mode so reset it to be sure everything
946 * is setup appropriately.
947 */
948 ieee80211_reset_erp(ic);
949 ieee80211_wme_initparams(ic); /* after opmode change */
950 error = ENETRESET;
951 }
952 #ifdef notdef
953 if (error == 0)
954 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
955 #endif
956 return error;
957 }
958
959 /*
960 * Common code to calculate the media status word
961 * from the operating mode and channel state.
962 */
963 static int
964 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
965 {
966 int status;
967
968 status = IFM_IEEE80211;
969 switch (opmode) {
970 case IEEE80211_M_STA:
971 break;
972 case IEEE80211_M_IBSS:
973 status |= IFM_IEEE80211_ADHOC;
974 break;
975 case IEEE80211_M_HOSTAP:
976 status |= IFM_IEEE80211_HOSTAP;
977 break;
978 case IEEE80211_M_MONITOR:
979 status |= IFM_IEEE80211_MONITOR;
980 break;
981 case IEEE80211_M_AHDEMO:
982 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
983 break;
984 case IEEE80211_M_WDS:
985 /* should not come here */
986 break;
987 }
988 if (IEEE80211_IS_CHAN_HTA(chan)) {
989 status |= IFM_IEEE80211_11NA;
990 } else if (IEEE80211_IS_CHAN_HTG(chan)) {
991 status |= IFM_IEEE80211_11NG;
992 } else if (IEEE80211_IS_CHAN_A(chan)) {
993 status |= IFM_IEEE80211_11A;
994 } else if (IEEE80211_IS_CHAN_B(chan)) {
995 status |= IFM_IEEE80211_11B;
996 } else if (IEEE80211_IS_CHAN_ANYG(chan)) {
997 status |= IFM_IEEE80211_11G;
998 } else if (IEEE80211_IS_CHAN_FHSS(chan)) {
999 status |= IFM_IEEE80211_FH;
1000 }
1001 /* XXX else complain? */
1002
1003 if (IEEE80211_IS_CHAN_TURBO(chan))
1004 status |= IFM_IEEE80211_TURBO;
1005
1006 return status;
1007 }
1008
1009 void
1010 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1011 {
1012 struct ieee80211com *ic;
1013 enum ieee80211_phymode mode;
1014 const struct ieee80211_rateset *rs;
1015
1016 ic = ieee80211_find_instance(ifp);
1017 if (!ic) {
1018 if_printf(ifp, "%s: no 802.11 instance!\n", __func__);
1019 return;
1020 }
1021 imr->ifm_status = IFM_AVALID;
1022 /*
1023 * NB: use the current channel's mode to lock down a xmit
1024 * rate only when running; otherwise we may have a mismatch
1025 * in which case the rate will not be convertible.
1026 */
1027 if (ic->ic_state == IEEE80211_S_RUN) {
1028 imr->ifm_status |= IFM_ACTIVE;
1029 mode = ieee80211_chan2mode(ic->ic_curchan);
1030 } else
1031 mode = IEEE80211_MODE_AUTO;
1032 imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan);
1033 /*
1034 * Calculate a current rate if possible.
1035 */
1036 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
1037 /*
1038 * A fixed rate is set, report that.
1039 */
1040 imr->ifm_active |= ieee80211_rate2media(ic,
1041 ic->ic_fixed_rate, mode);
1042 } else if (ic->ic_opmode == IEEE80211_M_STA) {
1043 /*
1044 * In station mode report the current transmit rate.
1045 * XXX HT rate
1046 */
1047 rs = &ic->ic_bss->ni_rates;
1048 imr->ifm_active |= ieee80211_rate2media(ic,
1049 rs->rs_rates[ic->ic_bss->ni_txrate], mode);
1050 } else
1051 imr->ifm_active |= IFM_AUTO;
1052 }
1053
1054 /*
1055 * Set the current phy mode and recalculate the active channel
1056 * set based on the available channels for this mode. Also
1057 * select a new default/current channel if the current one is
1058 * inappropriate for this mode.
1059 */
1060 int
1061 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1062 {
1063 /*
1064 * Adjust basic rates in 11b/11g supported rate set.
1065 * Note that if operating on a hal/quarter rate channel
1066 * this is a noop as those rates sets are different
1067 * and used instead.
1068 */
1069 if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1070 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode], mode);
1071
1072 ic->ic_curmode = mode;
1073 ieee80211_reset_erp(ic); /* reset ERP state */
1074 ieee80211_wme_initparams(ic); /* reset WME stat */
1075
1076 return 0;
1077 }
1078
1079 /*
1080 * Return the phy mode for with the specified channel.
1081 */
1082 enum ieee80211_phymode
1083 ieee80211_chan2mode(const struct ieee80211_channel *chan)
1084 {
1085 if (IEEE80211_IS_CHAN_HTA(chan))
1086 return IEEE80211_MODE_11NA;
1087 else if (IEEE80211_IS_CHAN_HTG(chan))
1088 return IEEE80211_MODE_11NG;
1089 else if (IEEE80211_IS_CHAN_108G(chan))
1090 return IEEE80211_MODE_TURBO_G;
1091 else if (IEEE80211_IS_CHAN_ST(chan))
1092 return IEEE80211_MODE_STURBO_A;
1093 else if (IEEE80211_IS_CHAN_TURBO(chan))
1094 return IEEE80211_MODE_TURBO_A;
1095 else if (IEEE80211_IS_CHAN_A(chan))
1096 return IEEE80211_MODE_11A;
1097 else if (IEEE80211_IS_CHAN_ANYG(chan))
1098 return IEEE80211_MODE_11G;
1099 else if (IEEE80211_IS_CHAN_B(chan))
1100 return IEEE80211_MODE_11B;
1101 else if (IEEE80211_IS_CHAN_FHSS(chan))
1102 return IEEE80211_MODE_FH;
1103
1104 /* NB: should not get here */
1105 printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1106 __func__, chan->ic_freq, chan->ic_flags);
1107 return IEEE80211_MODE_11B;
1108 }
1109
1110 struct ratemedia {
1111 u_int match; /* rate + mode */
1112 u_int media; /* if_media rate */
1113 };
1114
1115 static int
1116 findmedia(const struct ratemedia rates[], int n, u_int match)
1117 {
1118 int i;
1119
1120 for (i = 0; i < n; i++)
1121 if (rates[i].match == match)
1122 return rates[i].media;
1123 return IFM_AUTO;
1124 }
1125
1126 /*
1127 * Convert IEEE80211 rate value to ifmedia subtype.
1128 * Rate is either a legacy rate in units of 0.5Mbps
1129 * or an MCS index.
1130 */
1131 int
1132 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1133 {
1134 #define N(a) (sizeof(a) / sizeof(a[0]))
1135 static const struct ratemedia rates[] = {
1136 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1137 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1138 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1139 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1140 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1141 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1142 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1143 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1144 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1145 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1146 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1147 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1148 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1149 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1150 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1151 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1152 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1153 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1154 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1155 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1156 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1157 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1158 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1159 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1160 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1161 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1162 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1163 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1164 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1165 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1166 /* NB: OFDM72 doesn't realy exist so we don't handle it */
1167 };
1168 static const struct ratemedia htrates[] = {
1169 { 0, IFM_IEEE80211_MCS },
1170 { 1, IFM_IEEE80211_MCS },
1171 { 2, IFM_IEEE80211_MCS },
1172 { 3, IFM_IEEE80211_MCS },
1173 { 4, IFM_IEEE80211_MCS },
1174 { 5, IFM_IEEE80211_MCS },
1175 { 6, IFM_IEEE80211_MCS },
1176 { 7, IFM_IEEE80211_MCS },
1177 { 8, IFM_IEEE80211_MCS },
1178 { 9, IFM_IEEE80211_MCS },
1179 { 10, IFM_IEEE80211_MCS },
1180 { 11, IFM_IEEE80211_MCS },
1181 { 12, IFM_IEEE80211_MCS },
1182 { 13, IFM_IEEE80211_MCS },
1183 { 14, IFM_IEEE80211_MCS },
1184 { 15, IFM_IEEE80211_MCS },
1185 };
1186 int m;
1187
1188 /*
1189 * Check 11n rates first for match as an MCS.
1190 */
1191 if (mode == IEEE80211_MODE_11NA) {
1192 if (rate & IEEE80211_RATE_MCS) {
1193 rate &= ~IEEE80211_RATE_MCS;
1194 m = findmedia(htrates, N(htrates), rate);
1195 if (m != IFM_AUTO)
1196 return m | IFM_IEEE80211_11NA;
1197 }
1198 } else if (mode == IEEE80211_MODE_11NG) {
1199 /* NB: 12 is ambiguous, it will be treated as an MCS */
1200 if (rate & IEEE80211_RATE_MCS) {
1201 rate &= ~IEEE80211_RATE_MCS;
1202 m = findmedia(htrates, N(htrates), rate);
1203 if (m != IFM_AUTO)
1204 return m | IFM_IEEE80211_11NG;
1205 }
1206 }
1207 rate &= IEEE80211_RATE_VAL;
1208 switch (mode) {
1209 case IEEE80211_MODE_11A:
1210 case IEEE80211_MODE_11NA:
1211 case IEEE80211_MODE_TURBO_A:
1212 case IEEE80211_MODE_STURBO_A:
1213 return findmedia(rates, N(rates), rate | IFM_IEEE80211_11A);
1214 case IEEE80211_MODE_11B:
1215 return findmedia(rates, N(rates), rate | IFM_IEEE80211_11B);
1216 case IEEE80211_MODE_FH:
1217 return findmedia(rates, N(rates), rate | IFM_IEEE80211_FH);
1218 case IEEE80211_MODE_AUTO:
1219 /* NB: ic may be NULL for some drivers */
1220 if (ic && ic->ic_phytype == IEEE80211_T_FH)
1221 return findmedia(rates, N(rates),
1222 rate | IFM_IEEE80211_FH);
1223 /* NB: hack, 11g matches both 11b+11a rates */
1224 /* fall thru... */
1225 case IEEE80211_MODE_11G:
1226 case IEEE80211_MODE_11NG:
1227 case IEEE80211_MODE_TURBO_G:
1228 return findmedia(rates, N(rates), rate | IFM_IEEE80211_11G);
1229 }
1230 return IFM_AUTO;
1231 #undef N
1232 }
1233
1234 int
1235 ieee80211_media2rate(int mword)
1236 {
1237 #define N(a) (sizeof(a) / sizeof(a[0]))
1238 static const int ieeerates[] = {
1239 -1, /* IFM_AUTO */
1240 0, /* IFM_MANUAL */
1241 0, /* IFM_NONE */
1242 2, /* IFM_IEEE80211_FH1 */
1243 4, /* IFM_IEEE80211_FH2 */
1244 4, /* IFM_IEEE80211_DS2 */
1245 11, /* IFM_IEEE80211_DS5 */
1246 22, /* IFM_IEEE80211_DS11 */
1247 2, /* IFM_IEEE80211_DS1 */
1248 44, /* IFM_IEEE80211_DS22 */
1249 12, /* IFM_IEEE80211_OFDM6 */
1250 18, /* IFM_IEEE80211_OFDM9 */
1251 24, /* IFM_IEEE80211_OFDM12 */
1252 36, /* IFM_IEEE80211_OFDM18 */
1253 48, /* IFM_IEEE80211_OFDM24 */
1254 72, /* IFM_IEEE80211_OFDM36 */
1255 96, /* IFM_IEEE80211_OFDM48 */
1256 108, /* IFM_IEEE80211_OFDM54 */
1257 144, /* IFM_IEEE80211_OFDM72 */
1258 0, /* IFM_IEEE80211_DS354k */
1259 0, /* IFM_IEEE80211_DS512k */
1260 6, /* IFM_IEEE80211_OFDM3 */
1261 9, /* IFM_IEEE80211_OFDM4 */
1262 54, /* IFM_IEEE80211_OFDM27 */
1263 -1, /* IFM_IEEE80211_MCS */
1264 };
1265 return IFM_SUBTYPE(mword) < N(ieeerates) ?
1266 ieeerates[IFM_SUBTYPE(mword)] : 0;
1267 #undef N
1268 }
1269