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 /* arbitrarily pick the first channel */ 192 ic->ic_curchan = &ic->ic_channels[0]; 193 194 /* fillin well-known rate sets if driver has not specified */ 195 DEFAULTRATES(IEEE80211_MODE_11B, ieee80211_rateset_11b); 196 DEFAULTRATES(IEEE80211_MODE_11G, ieee80211_rateset_11g); 197 DEFAULTRATES(IEEE80211_MODE_11A, ieee80211_rateset_11a); 198 DEFAULTRATES(IEEE80211_MODE_TURBO_A, ieee80211_rateset_11a); 199 DEFAULTRATES(IEEE80211_MODE_TURBO_G, ieee80211_rateset_11g); 200 201 /* 202 * Set auto mode to reset active channel state and any desired channel. 203 */ 204 (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO); 205 #undef DEFAULTRATES 206 } 207 208 void 209 ieee80211_ifattach(struct ieee80211com *ic) 210 { 211 struct ifnet *ifp = ic->ic_ifp; 212 213 ether_ifattach(ifp, ic->ic_myaddr); 214 ifp->if_output = ieee80211_output; 215 216 bpfattach2(ifp, DLT_IEEE802_11, 217 sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf); 218 219 /* override the 802.3 setting */ 220 ifp->if_hdrlen = ic->ic_headroom 221 + sizeof(struct ieee80211_qosframe_addr4) 222 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN 223 + IEEE80211_WEP_EXTIVLEN; 224 /* XXX no way to recalculate on ifdetach */ 225 if (ALIGN(ifp->if_hdrlen) > max_linkhdr) { 226 /* XXX sanity check... */ 227 max_linkhdr = ALIGN(ifp->if_hdrlen); 228 max_hdr = max_linkhdr + max_protohdr; 229 max_datalen = MHLEN - max_hdr; 230 } 231 232 /* 233 * Fill in 802.11 available channel set, mark all 234 * available channels as active, and pick a default 235 * channel if not already specified. 236 */ 237 ieee80211_chan_init(ic); 238 239 if (ic->ic_caps & IEEE80211_C_BGSCAN) /* enable if capable */ 240 ic->ic_flags |= IEEE80211_F_BGSCAN; 241 #if 0 242 /* XXX not until WME+WPA issues resolved */ 243 if (ic->ic_caps & IEEE80211_C_WME) /* enable if capable */ 244 ic->ic_flags |= IEEE80211_F_WME; 245 #endif 246 if (ic->ic_caps & IEEE80211_C_BURST) 247 ic->ic_flags |= IEEE80211_F_BURST; 248 ic->ic_flags |= IEEE80211_F_DOTH; /* XXX out of caps, just ena */ 249 250 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT; 251 ic->ic_bmissthreshold = IEEE80211_HWBMISS_DEFAULT; 252 ic->ic_dtim_period = IEEE80211_DTIM_DEFAULT; 253 IEEE80211_LOCK_INIT(ic, "ieee80211com"); 254 IEEE80211_BEACON_LOCK_INIT(ic, "beacon"); 255 256 ic->ic_lintval = ic->ic_bintval; 257 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX; 258 259 ieee80211_crypto_attach(ic); 260 ieee80211_node_attach(ic); 261 ieee80211_power_attach(ic); 262 ieee80211_proto_attach(ic); 263 ieee80211_ht_attach(ic); 264 ieee80211_scan_attach(ic); 265 266 ieee80211_add_vap(ic); 267 268 ieee80211_sysctl_attach(ic); /* NB: requires ic_vap */ 269 270 /* 271 * Install a default reset method for the ioctl support. 272 * The driver is expected to fill this in before calling us. 273 */ 274 if (ic->ic_reset == NULL) 275 ic->ic_reset = ieee80211_default_reset; 276 277 KASSERT(ifp->if_spare2 == NULL, ("oops, hosed")); 278 ifp->if_spare2 = ic; /* XXX temp backpointer */ 279 } 280 281 void 282 ieee80211_ifdetach(struct ieee80211com *ic) 283 { 284 struct ifnet *ifp = ic->ic_ifp; 285 286 ieee80211_remove_vap(ic); 287 288 ieee80211_sysctl_detach(ic); 289 ieee80211_scan_detach(ic); 290 ieee80211_ht_detach(ic); 291 /* NB: must be called before ieee80211_node_detach */ 292 ieee80211_proto_detach(ic); 293 ieee80211_crypto_detach(ic); 294 ieee80211_power_detach(ic); 295 ieee80211_node_detach(ic); 296 ifmedia_removeall(&ic->ic_media); 297 298 IEEE80211_LOCK_DESTROY(ic); 299 IEEE80211_BEACON_LOCK_DESTROY(ic); 300 301 bpfdetach(ifp); 302 ether_ifdetach(ifp); 303 } 304 305 static __inline int 306 mapgsm(u_int freq, u_int flags) 307 { 308 freq *= 10; 309 if (flags & IEEE80211_CHAN_QUARTER) 310 freq += 5; 311 else if (flags & IEEE80211_CHAN_HALF) 312 freq += 10; 313 else 314 freq += 20; 315 /* NB: there is no 907/20 wide but leave room */ 316 return (freq - 906*10) / 5; 317 } 318 319 static __inline int 320 mappsb(u_int freq, u_int flags) 321 { 322 return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5; 323 } 324 325 /* 326 * Convert MHz frequency to IEEE channel number. 327 */ 328 int 329 ieee80211_mhz2ieee(u_int freq, u_int flags) 330 { 331 #define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990) 332 if (flags & IEEE80211_CHAN_GSM) 333 return mapgsm(freq, flags); 334 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 335 if (freq == 2484) 336 return 14; 337 if (freq < 2484) 338 return ((int) freq - 2407) / 5; 339 else 340 return 15 + ((freq - 2512) / 20); 341 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */ 342 if (freq <= 5000) { 343 /* XXX check regdomain? */ 344 if (IS_FREQ_IN_PSB(freq)) 345 return mappsb(freq, flags); 346 return (freq - 4000) / 5; 347 } else 348 return (freq - 5000) / 5; 349 } else { /* either, guess */ 350 if (freq == 2484) 351 return 14; 352 if (freq < 2484) { 353 if (907 <= freq && freq <= 922) 354 return mapgsm(freq, flags); 355 return ((int) freq - 2407) / 5; 356 } 357 if (freq < 5000) { 358 if (IS_FREQ_IN_PSB(freq)) 359 return mappsb(freq, flags); 360 else if (freq > 4900) 361 return (freq - 4000) / 5; 362 else 363 return 15 + ((freq - 2512) / 20); 364 } 365 return (freq - 5000) / 5; 366 } 367 #undef IS_FREQ_IN_PSB 368 } 369 370 /* 371 * Convert channel to IEEE channel number. 372 */ 373 int 374 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c) 375 { 376 if (c == NULL) { 377 if_printf(ic->ic_ifp, "invalid channel (NULL)\n"); 378 return 0; /* XXX */ 379 } 380 return (c == IEEE80211_CHAN_ANYC ? IEEE80211_CHAN_ANY : c->ic_ieee); 381 } 382 383 /* 384 * Convert IEEE channel number to MHz frequency. 385 */ 386 u_int 387 ieee80211_ieee2mhz(u_int chan, u_int flags) 388 { 389 if (flags & IEEE80211_CHAN_GSM) 390 return 907 + 5 * (chan / 10); 391 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 392 if (chan == 14) 393 return 2484; 394 if (chan < 14) 395 return 2407 + chan*5; 396 else 397 return 2512 + ((chan-15)*20); 398 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */ 399 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) { 400 chan -= 37; 401 return 4940 + chan*5 + (chan % 5 ? 2 : 0); 402 } 403 return 5000 + (chan*5); 404 } else { /* either, guess */ 405 /* XXX can't distinguish PSB+GSM channels */ 406 if (chan == 14) 407 return 2484; 408 if (chan < 14) /* 0-13 */ 409 return 2407 + chan*5; 410 if (chan < 27) /* 15-26 */ 411 return 2512 + ((chan-15)*20); 412 return 5000 + (chan*5); 413 } 414 } 415 416 /* 417 * Locate a channel given a frequency+flags. We cache 418 * the previous lookup to optimize swithing between two 419 * channels--as happens with dynamic turbo. 420 */ 421 struct ieee80211_channel * 422 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags) 423 { 424 struct ieee80211_channel *c; 425 int i; 426 427 flags &= IEEE80211_CHAN_ALLTURBO; 428 c = ic->ic_prevchan; 429 if (c != NULL && c->ic_freq == freq && 430 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 431 return c; 432 /* brute force search */ 433 for (i = 0; i < ic->ic_nchans; i++) { 434 c = &ic->ic_channels[i]; 435 if (c->ic_freq == freq && 436 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags) 437 return c; 438 } 439 return NULL; 440 } 441 442 static void 443 addmedia(struct ieee80211com *ic, int mode, int mword) 444 { 445 #define TURBO(m) ((m) | IFM_IEEE80211_TURBO) 446 #define ADD(_ic, _s, _o) \ 447 ifmedia_add(&(_ic)->ic_media, \ 448 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL) 449 static const u_int mopts[IEEE80211_MODE_MAX] = { 450 IFM_AUTO, /* IEEE80211_MODE_AUTO */ 451 IFM_IEEE80211_11A, /* IEEE80211_MODE_11A */ 452 IFM_IEEE80211_11B, /* IEEE80211_MODE_11B */ 453 IFM_IEEE80211_11G, /* IEEE80211_MODE_11G */ 454 IFM_IEEE80211_FH, /* IEEE80211_MODE_FH */ 455 TURBO(IFM_IEEE80211_11A), /* IEEE80211_MODE_TURBO_A */ 456 TURBO(IFM_IEEE80211_11G), /* IEEE80211_MODE_TURBO_G */ 457 TURBO(IFM_IEEE80211_11A), /* IEEE80211_MODE_STURBO_A */ 458 IFM_IEEE80211_11NA, /* IEEE80211_MODE_11NA */ 459 IFM_IEEE80211_11NG, /* IEEE80211_MODE_11NG */ 460 }; 461 u_int mopt; 462 463 KASSERT(mode < IEEE80211_MODE_MAX, ("bad mode %u", mode)); 464 mopt = mopts[mode]; 465 KASSERT(mopt != 0 || mode == IEEE80211_MODE_AUTO, 466 ("no media mapping for mode %u", mode)); 467 468 ADD(ic, mword, mopt); /* e.g. 11a auto */ 469 if (ic->ic_caps & IEEE80211_C_IBSS) 470 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC); 471 if (ic->ic_caps & IEEE80211_C_HOSTAP) 472 ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP); 473 if (ic->ic_caps & IEEE80211_C_AHDEMO) 474 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0); 475 if (ic->ic_caps & IEEE80211_C_MONITOR) 476 ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR); 477 #undef ADD 478 #undef TURBO 479 } 480 481 /* 482 * Setup the media data structures according to the channel and 483 * rate tables. This must be called by the driver after 484 * ieee80211_attach and before most anything else. 485 */ 486 void 487 ieee80211_media_init(struct ieee80211com *ic, 488 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat) 489 { 490 struct ifnet *ifp = ic->ic_ifp; 491 int i, j, mode, rate, maxrate, mword, r; 492 const struct ieee80211_rateset *rs; 493 struct ieee80211_rateset allrates; 494 495 /* NB: this works because the structure is initialized to zero */ 496 if (LIST_EMPTY(&ic->ic_media.ifm_list)) { 497 /* 498 * Do late attach work that must wait for any subclass 499 * (i.e. driver) work such as overriding methods. 500 */ 501 ieee80211_node_lateattach(ic); 502 } else { 503 /* 504 * We are re-initializing the channel list; clear 505 * the existing media state as the media routines 506 * don't suppress duplicates. 507 */ 508 ifmedia_removeall(&ic->ic_media); 509 ieee80211_chan_init(ic); 510 } 511 ieee80211_power_lateattach(ic); 512 513 /* 514 * Fill in media characteristics. 515 */ 516 ifmedia_init(&ic->ic_media, 0, media_change, media_stat); 517 maxrate = 0; 518 /* 519 * Add media for legacy operating modes. 520 */ 521 memset(&allrates, 0, sizeof(allrates)); 522 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) { 523 if (isclr(ic->ic_modecaps, mode)) 524 continue; 525 addmedia(ic, mode, IFM_AUTO); 526 if (mode == IEEE80211_MODE_AUTO) 527 continue; 528 rs = &ic->ic_sup_rates[mode]; 529 for (i = 0; i < rs->rs_nrates; i++) { 530 rate = rs->rs_rates[i]; 531 mword = ieee80211_rate2media(ic, rate, mode); 532 if (mword == 0) 533 continue; 534 addmedia(ic, mode, mword); 535 /* 536 * Add legacy rate to the collection of all rates. 537 */ 538 r = rate & IEEE80211_RATE_VAL; 539 for (j = 0; j < allrates.rs_nrates; j++) 540 if (allrates.rs_rates[j] == r) 541 break; 542 if (j == allrates.rs_nrates) { 543 /* unique, add to the set */ 544 allrates.rs_rates[j] = r; 545 allrates.rs_nrates++; 546 } 547 rate = (rate & IEEE80211_RATE_VAL) / 2; 548 if (rate > maxrate) 549 maxrate = rate; 550 } 551 } 552 for (i = 0; i < allrates.rs_nrates; i++) { 553 mword = ieee80211_rate2media(ic, allrates.rs_rates[i], 554 IEEE80211_MODE_AUTO); 555 if (mword == 0) 556 continue; 557 /* NB: remove media options from mword */ 558 addmedia(ic, IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword)); 559 } 560 /* 561 * Add HT/11n media. Note that we do not have enough 562 * bits in the media subtype to express the MCS so we 563 * use a "placeholder" media subtype and any fixed MCS 564 * must be specified with a different mechanism. 565 */ 566 for (; mode < IEEE80211_MODE_MAX; mode++) { 567 if (isclr(ic->ic_modecaps, mode)) 568 continue; 569 addmedia(ic, mode, IFM_AUTO); 570 addmedia(ic, mode, IFM_IEEE80211_MCS); 571 } 572 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) || 573 isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) { 574 addmedia(ic, IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS); 575 /* XXX could walk htrates */ 576 /* XXX known array size */ 577 if (ieee80211_htrates[15] > maxrate) 578 maxrate = ieee80211_htrates[15]; 579 } 580 581 /* NB: strip explicit mode; we're actually in autoselect */ 582 ifmedia_set(&ic->ic_media, 583 media_status(ic->ic_opmode, ic->ic_curchan) &~ IFM_MMASK); 584 585 if (maxrate) 586 ifp->if_baudrate = IF_Mbps(maxrate); 587 } 588 589 const struct ieee80211_rateset * 590 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c) 591 { 592 if (IEEE80211_IS_CHAN_HALF(c)) 593 return &ieee80211_rateset_half; 594 if (IEEE80211_IS_CHAN_QUARTER(c)) 595 return &ieee80211_rateset_quarter; 596 if (IEEE80211_IS_CHAN_HTA(c)) 597 return &ic->ic_sup_rates[IEEE80211_MODE_11A]; 598 if (IEEE80211_IS_CHAN_HTG(c)) { 599 /* XXX does this work for basic rates? */ 600 return &ic->ic_sup_rates[IEEE80211_MODE_11G]; 601 } 602 return &ic->ic_sup_rates[ieee80211_chan2mode(c)]; 603 } 604 605 void 606 ieee80211_announce(struct ieee80211com *ic) 607 { 608 struct ifnet *ifp = ic->ic_ifp; 609 int i, mode, rate, mword; 610 const struct ieee80211_rateset *rs; 611 612 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) { 613 if (isclr(ic->ic_modecaps, mode)) 614 continue; 615 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]); 616 rs = &ic->ic_sup_rates[mode]; 617 for (i = 0; i < rs->rs_nrates; i++) { 618 mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode); 619 if (mword == 0) 620 continue; 621 rate = ieee80211_media2rate(mword); 622 printf("%s%d%sMbps", (i != 0 ? " " : ""), 623 rate / 2, ((rate & 0x1) != 0 ? ".5" : "")); 624 } 625 printf("\n"); 626 } 627 ieee80211_ht_announce(ic); 628 } 629 630 void 631 ieee80211_announce_channels(struct ieee80211com *ic) 632 { 633 const struct ieee80211_channel *c; 634 char type; 635 int i, cw; 636 637 printf("Chan Freq CW RegPwr MinPwr MaxPwr\n"); 638 for (i = 0; i < ic->ic_nchans; i++) { 639 c = &ic->ic_channels[i]; 640 if (IEEE80211_IS_CHAN_ST(c)) 641 type = 'S'; 642 else if (IEEE80211_IS_CHAN_108A(c)) 643 type = 'T'; 644 else if (IEEE80211_IS_CHAN_108G(c)) 645 type = 'G'; 646 else if (IEEE80211_IS_CHAN_HT(c)) 647 type = 'n'; 648 else if (IEEE80211_IS_CHAN_A(c)) 649 type = 'a'; 650 else if (IEEE80211_IS_CHAN_ANYG(c)) 651 type = 'g'; 652 else if (IEEE80211_IS_CHAN_B(c)) 653 type = 'b'; 654 else 655 type = 'f'; 656 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c)) 657 cw = 40; 658 else if (IEEE80211_IS_CHAN_HALF(c)) 659 cw = 10; 660 else if (IEEE80211_IS_CHAN_QUARTER(c)) 661 cw = 5; 662 else 663 cw = 20; 664 printf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n" 665 , c->ic_ieee, c->ic_freq, type 666 , cw 667 , IEEE80211_IS_CHAN_HT40U(c) ? '+' : 668 IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' ' 669 , c->ic_maxregpower 670 , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0 671 , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0 672 ); 673 } 674 } 675 676 /* 677 * Find an instance by it's mac address. 678 */ 679 struct ieee80211com * 680 ieee80211_find_vap(const uint8_t mac[IEEE80211_ADDR_LEN]) 681 { 682 struct ieee80211com *ic; 683 684 /* XXX lock */ 685 SLIST_FOREACH(ic, &ieee80211_list, ic_next) 686 if (IEEE80211_ADDR_EQ(mac, ic->ic_myaddr)) 687 return ic; 688 return NULL; 689 } 690 691 static struct ieee80211com * 692 ieee80211_find_instance(struct ifnet *ifp) 693 { 694 struct ieee80211com *ic; 695 696 /* XXX lock */ 697 /* XXX not right for multiple instances but works for now */ 698 SLIST_FOREACH(ic, &ieee80211_list, ic_next) 699 if (ic->ic_ifp == ifp) 700 return ic; 701 return NULL; 702 } 703 704 static int 705 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate) 706 { 707 #define IEEERATE(_ic,_m,_i) \ 708 ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL) 709 int i, nrates = ic->ic_sup_rates[mode].rs_nrates; 710 for (i = 0; i < nrates; i++) 711 if (IEEERATE(ic, mode, i) == rate) 712 return i; 713 return -1; 714 #undef IEEERATE 715 } 716 717 /* 718 * Convert a media specification to a rate index and possibly a mode 719 * (if the rate is fixed and the mode is specified as ``auto'' then 720 * we need to lock down the mode so the index is meanginful). 721 */ 722 static int 723 checkrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate) 724 { 725 726 /* 727 * Check the rate table for the specified/current phy. 728 */ 729 if (mode == IEEE80211_MODE_AUTO) { 730 int i; 731 /* 732 * In autoselect mode search for the rate. 733 */ 734 for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) { 735 if (isset(ic->ic_modecaps, i) && 736 findrate(ic, i, rate) != -1) 737 return 1; 738 } 739 return 0; 740 } else { 741 /* 742 * Mode is fixed, check for rate. 743 */ 744 return (findrate(ic, mode, rate) != -1); 745 } 746 } 747 748 /* 749 * Handle a media change request. 750 */ 751 int 752 ieee80211_media_change(struct ifnet *ifp) 753 { 754 struct ieee80211com *ic; 755 struct ifmedia_entry *ime; 756 enum ieee80211_opmode newopmode; 757 enum ieee80211_phymode newphymode; 758 int newrate, error = 0; 759 760 ic = ieee80211_find_instance(ifp); 761 if (!ic) { 762 if_printf(ifp, "%s: no 802.11 instance!\n", __func__); 763 return EINVAL; 764 } 765 ime = ic->ic_media.ifm_cur; 766 /* 767 * First, identify the phy mode. 768 */ 769 switch (IFM_MODE(ime->ifm_media)) { 770 case IFM_IEEE80211_11A: 771 newphymode = IEEE80211_MODE_11A; 772 break; 773 case IFM_IEEE80211_11B: 774 newphymode = IEEE80211_MODE_11B; 775 break; 776 case IFM_IEEE80211_11G: 777 newphymode = IEEE80211_MODE_11G; 778 break; 779 case IFM_IEEE80211_FH: 780 newphymode = IEEE80211_MODE_FH; 781 break; 782 case IFM_IEEE80211_11NA: 783 newphymode = IEEE80211_MODE_11NA; 784 break; 785 case IFM_IEEE80211_11NG: 786 newphymode = IEEE80211_MODE_11NG; 787 break; 788 case IFM_AUTO: 789 newphymode = IEEE80211_MODE_AUTO; 790 break; 791 default: 792 return EINVAL; 793 } 794 /* 795 * Turbo mode is an ``option''. 796 * XXX does not apply to AUTO 797 */ 798 if (ime->ifm_media & IFM_IEEE80211_TURBO) { 799 if (newphymode == IEEE80211_MODE_11A) { 800 if (ic->ic_flags & IEEE80211_F_TURBOP) 801 newphymode = IEEE80211_MODE_TURBO_A; 802 else 803 newphymode = IEEE80211_MODE_STURBO_A; 804 } else if (newphymode == IEEE80211_MODE_11G) 805 newphymode = IEEE80211_MODE_TURBO_G; 806 else 807 return EINVAL; 808 } 809 /* XXX HT40 +/- */ 810 /* 811 * Next, the fixed/variable rate. 812 */ 813 newrate = ic->ic_fixed_rate; 814 if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) { 815 /* 816 * Convert media subtype to rate. 817 */ 818 newrate = ieee80211_media2rate(ime->ifm_media); 819 if (newrate == 0 || !checkrate(ic, newphymode, newrate)) 820 return EINVAL; 821 } else 822 newrate = IEEE80211_FIXED_RATE_NONE; 823 824 /* 825 * Deduce new operating mode but don't install it just yet. 826 */ 827 if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) == 828 (IFM_IEEE80211_ADHOC|IFM_FLAG0)) 829 newopmode = IEEE80211_M_AHDEMO; 830 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP) 831 newopmode = IEEE80211_M_HOSTAP; 832 else if (ime->ifm_media & IFM_IEEE80211_ADHOC) 833 newopmode = IEEE80211_M_IBSS; 834 else if (ime->ifm_media & IFM_IEEE80211_MONITOR) 835 newopmode = IEEE80211_M_MONITOR; 836 else 837 newopmode = IEEE80211_M_STA; 838 839 /* 840 * Handle phy mode change. 841 */ 842 if (ic->ic_des_mode != newphymode) { /* change phy mode */ 843 ic->ic_des_mode = newphymode; 844 error = ENETRESET; 845 } 846 847 /* 848 * Committed to changes, install the rate setting. 849 */ 850 if (ic->ic_fixed_rate != newrate) { 851 ic->ic_fixed_rate = newrate; /* set fixed tx rate */ 852 error = ENETRESET; 853 } 854 855 /* 856 * Handle operating mode change. 857 */ 858 if (ic->ic_opmode != newopmode) { 859 ic->ic_opmode = newopmode; 860 switch (newopmode) { 861 case IEEE80211_M_AHDEMO: 862 case IEEE80211_M_HOSTAP: 863 case IEEE80211_M_STA: 864 case IEEE80211_M_MONITOR: 865 case IEEE80211_M_WDS: 866 ic->ic_flags &= ~IEEE80211_F_IBSSON; 867 break; 868 case IEEE80211_M_IBSS: 869 ic->ic_flags |= IEEE80211_F_IBSSON; 870 break; 871 } 872 /* 873 * Yech, slot time may change depending on the 874 * operating mode so reset it to be sure everything 875 * is setup appropriately. 876 */ 877 ieee80211_reset_erp(ic); 878 ieee80211_wme_initparams(ic); /* after opmode change */ 879 error = ENETRESET; 880 } 881 #ifdef notdef 882 if (error == 0) 883 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media); 884 #endif 885 return error; 886 } 887 888 /* 889 * Common code to calculate the media status word 890 * from the operating mode and channel state. 891 */ 892 static int 893 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan) 894 { 895 int status; 896 897 status = IFM_IEEE80211; 898 switch (opmode) { 899 case IEEE80211_M_STA: 900 break; 901 case IEEE80211_M_IBSS: 902 status |= IFM_IEEE80211_ADHOC; 903 break; 904 case IEEE80211_M_HOSTAP: 905 status |= IFM_IEEE80211_HOSTAP; 906 break; 907 case IEEE80211_M_MONITOR: 908 status |= IFM_IEEE80211_MONITOR; 909 break; 910 case IEEE80211_M_AHDEMO: 911 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0; 912 break; 913 case IEEE80211_M_WDS: 914 /* should not come here */ 915 break; 916 } 917 if (IEEE80211_IS_CHAN_HTA(chan)) { 918 status |= IFM_IEEE80211_11NA; 919 } else if (IEEE80211_IS_CHAN_HTG(chan)) { 920 status |= IFM_IEEE80211_11NG; 921 } else if (IEEE80211_IS_CHAN_A(chan)) { 922 status |= IFM_IEEE80211_11A; 923 } else if (IEEE80211_IS_CHAN_B(chan)) { 924 status |= IFM_IEEE80211_11B; 925 } else if (IEEE80211_IS_CHAN_ANYG(chan)) { 926 status |= IFM_IEEE80211_11G; 927 } else if (IEEE80211_IS_CHAN_FHSS(chan)) { 928 status |= IFM_IEEE80211_FH; 929 } 930 /* XXX else complain? */ 931 932 if (IEEE80211_IS_CHAN_TURBO(chan)) 933 status |= IFM_IEEE80211_TURBO; 934 if (IEEE80211_IS_CHAN_HT40U(chan)) 935 status |= IFM_IEEE80211_HT40PLUS; 936 if (IEEE80211_IS_CHAN_HT40D(chan)) 937 status |= IFM_IEEE80211_HT40MINUS; 938 939 return status; 940 } 941 942 void 943 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr) 944 { 945 struct ieee80211com *ic; 946 enum ieee80211_phymode mode; 947 const struct ieee80211_rateset *rs; 948 949 ic = ieee80211_find_instance(ifp); 950 if (!ic) { 951 if_printf(ifp, "%s: no 802.11 instance!\n", __func__); 952 return; 953 } 954 imr->ifm_status = IFM_AVALID; 955 /* 956 * NB: use the current channel's mode to lock down a xmit 957 * rate only when running; otherwise we may have a mismatch 958 * in which case the rate will not be convertible. 959 */ 960 if (ic->ic_state == IEEE80211_S_RUN) { 961 imr->ifm_status |= IFM_ACTIVE; 962 mode = ieee80211_chan2mode(ic->ic_curchan); 963 } else 964 mode = IEEE80211_MODE_AUTO; 965 imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan); 966 /* 967 * Calculate a current rate if possible. 968 */ 969 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) { 970 /* 971 * A fixed rate is set, report that. 972 */ 973 imr->ifm_active |= ieee80211_rate2media(ic, 974 ic->ic_fixed_rate, mode); 975 } else if (ic->ic_opmode == IEEE80211_M_STA) { 976 /* 977 * In station mode report the current transmit rate. 978 * XXX HT rate 979 */ 980 rs = &ic->ic_bss->ni_rates; 981 imr->ifm_active |= ieee80211_rate2media(ic, 982 rs->rs_rates[ic->ic_bss->ni_txrate], mode); 983 } else 984 imr->ifm_active |= IFM_AUTO; 985 } 986 987 /* 988 * Set the current phy mode and recalculate the active channel 989 * set based on the available channels for this mode. Also 990 * select a new default/current channel if the current one is 991 * inappropriate for this mode. 992 */ 993 int 994 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode) 995 { 996 /* 997 * Adjust basic rates in 11b/11g supported rate set. 998 * Note that if operating on a hal/quarter rate channel 999 * this is a noop as those rates sets are different 1000 * and used instead. 1001 */ 1002 if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B) 1003 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode], mode); 1004 1005 ic->ic_curmode = mode; 1006 ieee80211_reset_erp(ic); /* reset ERP state */ 1007 ieee80211_wme_initparams(ic); /* reset WME stat */ 1008 1009 return 0; 1010 } 1011 1012 /* 1013 * Return the phy mode for with the specified channel. 1014 */ 1015 enum ieee80211_phymode 1016 ieee80211_chan2mode(const struct ieee80211_channel *chan) 1017 { 1018 1019 if (IEEE80211_IS_CHAN_HTA(chan)) 1020 return IEEE80211_MODE_11NA; 1021 else if (IEEE80211_IS_CHAN_HTG(chan)) 1022 return IEEE80211_MODE_11NG; 1023 else if (IEEE80211_IS_CHAN_108G(chan)) 1024 return IEEE80211_MODE_TURBO_G; 1025 else if (IEEE80211_IS_CHAN_ST(chan)) 1026 return IEEE80211_MODE_STURBO_A; 1027 else if (IEEE80211_IS_CHAN_TURBO(chan)) 1028 return IEEE80211_MODE_TURBO_A; 1029 else if (IEEE80211_IS_CHAN_A(chan)) 1030 return IEEE80211_MODE_11A; 1031 else if (IEEE80211_IS_CHAN_ANYG(chan)) 1032 return IEEE80211_MODE_11G; 1033 else if (IEEE80211_IS_CHAN_B(chan)) 1034 return IEEE80211_MODE_11B; 1035 else if (IEEE80211_IS_CHAN_FHSS(chan)) 1036 return IEEE80211_MODE_FH; 1037 1038 /* NB: should not get here */ 1039 printf("%s: cannot map channel to mode; freq %u flags 0x%x\n", 1040 __func__, chan->ic_freq, chan->ic_flags); 1041 return IEEE80211_MODE_11B; 1042 } 1043 1044 struct ratemedia { 1045 u_int match; /* rate + mode */ 1046 u_int media; /* if_media rate */ 1047 }; 1048 1049 static int 1050 findmedia(const struct ratemedia rates[], int n, u_int match) 1051 { 1052 int i; 1053 1054 for (i = 0; i < n; i++) 1055 if (rates[i].match == match) 1056 return rates[i].media; 1057 return IFM_AUTO; 1058 } 1059 1060 /* 1061 * Convert IEEE80211 rate value to ifmedia subtype. 1062 * Rate is either a legacy rate in units of 0.5Mbps 1063 * or an MCS index. 1064 */ 1065 int 1066 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode) 1067 { 1068 #define N(a) (sizeof(a) / sizeof(a[0])) 1069 static const struct ratemedia rates[] = { 1070 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 }, 1071 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 }, 1072 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 }, 1073 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 }, 1074 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 }, 1075 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 }, 1076 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 }, 1077 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 }, 1078 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 }, 1079 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 }, 1080 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 }, 1081 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 }, 1082 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 }, 1083 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 }, 1084 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 }, 1085 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 }, 1086 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 }, 1087 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 }, 1088 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 }, 1089 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 }, 1090 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 }, 1091 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 }, 1092 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 }, 1093 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 }, 1094 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 }, 1095 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 }, 1096 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 }, 1097 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 }, 1098 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 }, 1099 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 }, 1100 /* NB: OFDM72 doesn't realy exist so we don't handle it */ 1101 }; 1102 static const struct ratemedia htrates[] = { 1103 { 0, IFM_IEEE80211_MCS }, 1104 { 1, IFM_IEEE80211_MCS }, 1105 { 2, IFM_IEEE80211_MCS }, 1106 { 3, IFM_IEEE80211_MCS }, 1107 { 4, IFM_IEEE80211_MCS }, 1108 { 5, IFM_IEEE80211_MCS }, 1109 { 6, IFM_IEEE80211_MCS }, 1110 { 7, IFM_IEEE80211_MCS }, 1111 { 8, IFM_IEEE80211_MCS }, 1112 { 9, IFM_IEEE80211_MCS }, 1113 { 10, IFM_IEEE80211_MCS }, 1114 { 11, IFM_IEEE80211_MCS }, 1115 { 12, IFM_IEEE80211_MCS }, 1116 { 13, IFM_IEEE80211_MCS }, 1117 { 14, IFM_IEEE80211_MCS }, 1118 { 15, IFM_IEEE80211_MCS }, 1119 }; 1120 int m; 1121 1122 /* 1123 * Check 11n rates first for match as an MCS. 1124 */ 1125 if (mode == IEEE80211_MODE_11NA) { 1126 if ((rate & IEEE80211_RATE_BASIC) == 0) { 1127 m = findmedia(htrates, N(htrates), rate); 1128 if (m != IFM_AUTO) 1129 return m | IFM_IEEE80211_11NA; 1130 } 1131 } else if (mode == IEEE80211_MODE_11NG) { 1132 /* NB: 12 is ambiguous, it will be treated as an MCS */ 1133 if ((rate & IEEE80211_RATE_BASIC) == 0) { 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 } 1201