1 /* $OpenBSD: ieee80211.c,v 1.36 2009/06/03 20:35:37 beck Exp $ */ 2 /* $NetBSD: ieee80211.c,v 1.19 2004/06/06 05:45:29 dyoung Exp $ */ 3 4 /*- 5 * Copyright (c) 2001 Atsushi Onoe 6 * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * IEEE 802.11 generic handler 34 */ 35 36 #include "bpfilter.h" 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/mbuf.h> 41 #include <sys/kernel.h> 42 #include <sys/socket.h> 43 #include <sys/sockio.h> 44 #include <sys/endian.h> 45 #include <sys/errno.h> 46 #include <sys/proc.h> 47 #include <sys/sysctl.h> 48 49 #include <net/if.h> 50 #include <net/if_dl.h> 51 #include <net/if_media.h> 52 #include <net/if_arp.h> 53 #include <net/if_llc.h> 54 55 #if NBPFILTER > 0 56 #include <net/bpf.h> 57 #endif 58 59 #ifdef INET 60 #include <netinet/in.h> 61 #include <netinet/if_ether.h> 62 #endif 63 64 #include <net80211/ieee80211_var.h> 65 #include <net80211/ieee80211_priv.h> 66 67 #ifdef IEEE80211_DEBUG 68 int ieee80211_debug = 0; 69 #endif 70 71 int ieee80211_cache_size = IEEE80211_CACHE_SIZE; 72 73 struct ieee80211com_head ieee80211com_head = 74 LIST_HEAD_INITIALIZER(ieee80211com_head); 75 76 void ieee80211_setbasicrates(struct ieee80211com *); 77 int ieee80211_findrate(struct ieee80211com *, enum ieee80211_phymode, int); 78 79 void 80 ieee80211_ifattach(struct ifnet *ifp) 81 { 82 struct ieee80211com *ic = (void *)ifp; 83 struct ieee80211_channel *c; 84 int i; 85 86 memcpy(((struct arpcom *)ifp)->ac_enaddr, ic->ic_myaddr, 87 ETHER_ADDR_LEN); 88 ether_ifattach(ifp); 89 90 ifp->if_output = ieee80211_output; 91 92 #if NBPFILTER > 0 93 bpfattach(&ic->ic_rawbpf, ifp, DLT_IEEE802_11, 94 sizeof(struct ieee80211_frame_addr4)); 95 #endif 96 ieee80211_crypto_attach(ifp); 97 98 /* 99 * Fill in 802.11 available channel set, mark 100 * all available channels as active, and pick 101 * a default channel if not already specified. 102 */ 103 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail)); 104 ic->ic_modecaps |= 1<<IEEE80211_MODE_AUTO; 105 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 106 c = &ic->ic_channels[i]; 107 if (c->ic_flags) { 108 /* 109 * Verify driver passed us valid data. 110 */ 111 if (i != ieee80211_chan2ieee(ic, c)) { 112 printf("%s: bad channel ignored; " 113 "freq %u flags %x number %u\n", 114 ifp->if_xname, c->ic_freq, c->ic_flags, 115 i); 116 c->ic_flags = 0; /* NB: remove */ 117 continue; 118 } 119 setbit(ic->ic_chan_avail, i); 120 /* 121 * Identify mode capabilities. 122 */ 123 if (IEEE80211_IS_CHAN_A(c)) 124 ic->ic_modecaps |= 1<<IEEE80211_MODE_11A; 125 if (IEEE80211_IS_CHAN_B(c)) 126 ic->ic_modecaps |= 1<<IEEE80211_MODE_11B; 127 if (IEEE80211_IS_CHAN_PUREG(c)) 128 ic->ic_modecaps |= 1<<IEEE80211_MODE_11G; 129 if (IEEE80211_IS_CHAN_T(c)) 130 ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO; 131 } 132 } 133 /* validate ic->ic_curmode */ 134 if ((ic->ic_modecaps & (1<<ic->ic_curmode)) == 0) 135 ic->ic_curmode = IEEE80211_MODE_AUTO; 136 ic->ic_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */ 137 ic->ic_scan_lock = IEEE80211_SCAN_UNLOCKED; 138 139 /* IEEE 802.11 defines a MTU >= 2290 */ 140 ifp->if_capabilities |= IFCAP_VLAN_MTU; 141 142 ieee80211_setbasicrates(ic); 143 (void)ieee80211_setmode(ic, ic->ic_curmode); 144 145 if (ic->ic_lintval == 0) 146 ic->ic_lintval = 100; /* default sleep */ 147 ic->ic_bmisstimeout = 7*ic->ic_lintval; /* default 7 beacons */ 148 ic->ic_dtim_period = 1; /* all TIMs are DTIMs */ 149 150 LIST_INSERT_HEAD(&ieee80211com_head, ic, ic_list); 151 ieee80211_node_attach(ifp); 152 ieee80211_proto_attach(ifp); 153 154 if_addgroup(ifp, "wlan"); 155 ifp->if_priority = IF_WIRELESS_DEFAULT_PRIORITY; 156 } 157 158 void 159 ieee80211_ifdetach(struct ifnet *ifp) 160 { 161 struct ieee80211com *ic = (void *)ifp; 162 163 ieee80211_proto_detach(ifp); 164 ieee80211_crypto_detach(ifp); 165 ieee80211_node_detach(ifp); 166 LIST_REMOVE(ic, ic_list); 167 ifmedia_delete_instance(&ic->ic_media, IFM_INST_ANY); 168 ether_ifdetach(ifp); 169 } 170 171 /* 172 * Convert MHz frequency to IEEE channel number. 173 */ 174 u_int 175 ieee80211_mhz2ieee(u_int freq, u_int flags) 176 { 177 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 178 if (freq == 2484) 179 return 14; 180 if (freq < 2484) 181 return (freq - 2407) / 5; 182 else 183 return 15 + ((freq - 2512) / 20); 184 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */ 185 return (freq - 5000) / 5; 186 } else { /* either, guess */ 187 if (freq == 2484) 188 return 14; 189 if (freq < 2484) 190 return (freq - 2407) / 5; 191 if (freq < 5000) 192 return 15 + ((freq - 2512) / 20); 193 return (freq - 5000) / 5; 194 } 195 } 196 197 /* 198 * Convert channel to IEEE channel number. 199 */ 200 u_int 201 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c) 202 { 203 struct ifnet *ifp = &ic->ic_if; 204 if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX]) 205 return c - ic->ic_channels; 206 else if (c == IEEE80211_CHAN_ANYC) 207 return IEEE80211_CHAN_ANY; 208 else if (c != NULL) { 209 printf("%s: invalid channel freq %u flags %x\n", 210 ifp->if_xname, c->ic_freq, c->ic_flags); 211 return 0; /* XXX */ 212 } else { 213 printf("%s: invalid channel (NULL)\n", ifp->if_xname); 214 return 0; /* XXX */ 215 } 216 } 217 218 /* 219 * Convert IEEE channel number to MHz frequency. 220 */ 221 u_int 222 ieee80211_ieee2mhz(u_int chan, u_int flags) 223 { 224 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 225 if (chan == 14) 226 return 2484; 227 if (chan < 14) 228 return 2407 + chan*5; 229 else 230 return 2512 + ((chan-15)*20); 231 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */ 232 return 5000 + (chan*5); 233 } else { /* either, guess */ 234 if (chan == 14) 235 return 2484; 236 if (chan < 14) /* 0-13 */ 237 return 2407 + chan*5; 238 if (chan < 27) /* 15-26 */ 239 return 2512 + ((chan-15)*20); 240 return 5000 + (chan*5); 241 } 242 } 243 244 /* 245 * Setup the media data structures according to the channel and 246 * rate tables. This must be called by the driver after 247 * ieee80211_attach and before most anything else. 248 */ 249 void 250 ieee80211_media_init(struct ifnet *ifp, 251 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat) 252 { 253 #define ADD(_ic, _s, _o) \ 254 ifmedia_add(&(_ic)->ic_media, \ 255 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL) 256 struct ieee80211com *ic = (void *)ifp; 257 struct ifmediareq imr; 258 int i, j, mode, rate, maxrate, mword, mopt, r; 259 const struct ieee80211_rateset *rs; 260 struct ieee80211_rateset allrates; 261 262 /* 263 * Do late attach work that must wait for any subclass 264 * (i.e. driver) work such as overriding methods. 265 */ 266 ieee80211_node_lateattach(ifp); 267 268 /* 269 * Fill in media characteristics. 270 */ 271 ifmedia_init(&ic->ic_media, 0, media_change, media_stat); 272 maxrate = 0; 273 memset(&allrates, 0, sizeof(allrates)); 274 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) { 275 static const u_int mopts[] = { 276 IFM_AUTO, 277 IFM_IEEE80211_11A, 278 IFM_IEEE80211_11B, 279 IFM_IEEE80211_11G, 280 IFM_IEEE80211_11A | IFM_IEEE80211_TURBO, 281 }; 282 if ((ic->ic_modecaps & (1<<mode)) == 0) 283 continue; 284 mopt = mopts[mode]; 285 ADD(ic, IFM_AUTO, mopt); /* e.g. 11a auto */ 286 #ifndef IEEE80211_STA_ONLY 287 if (ic->ic_caps & IEEE80211_C_IBSS) 288 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_IBSS); 289 if (ic->ic_caps & IEEE80211_C_HOSTAP) 290 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP); 291 if (ic->ic_caps & IEEE80211_C_AHDEMO) 292 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC); 293 #endif 294 if (ic->ic_caps & IEEE80211_C_MONITOR) 295 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR); 296 if (mode == IEEE80211_MODE_AUTO) 297 continue; 298 rs = &ic->ic_sup_rates[mode]; 299 for (i = 0; i < rs->rs_nrates; i++) { 300 rate = rs->rs_rates[i]; 301 mword = ieee80211_rate2media(ic, rate, mode); 302 if (mword == 0) 303 continue; 304 ADD(ic, mword, mopt); 305 #ifndef IEEE80211_STA_ONLY 306 if (ic->ic_caps & IEEE80211_C_IBSS) 307 ADD(ic, mword, mopt | IFM_IEEE80211_IBSS); 308 if (ic->ic_caps & IEEE80211_C_HOSTAP) 309 ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP); 310 if (ic->ic_caps & IEEE80211_C_AHDEMO) 311 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC); 312 #endif 313 if (ic->ic_caps & IEEE80211_C_MONITOR) 314 ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR); 315 /* 316 * Add rate to the collection of all rates. 317 */ 318 r = rate & IEEE80211_RATE_VAL; 319 for (j = 0; j < allrates.rs_nrates; j++) 320 if (allrates.rs_rates[j] == r) 321 break; 322 if (j == allrates.rs_nrates) { 323 /* unique, add to the set */ 324 allrates.rs_rates[j] = r; 325 allrates.rs_nrates++; 326 } 327 rate = (rate & IEEE80211_RATE_VAL) / 2; 328 if (rate > maxrate) 329 maxrate = rate; 330 } 331 } 332 for (i = 0; i < allrates.rs_nrates; i++) { 333 mword = ieee80211_rate2media(ic, allrates.rs_rates[i], 334 IEEE80211_MODE_AUTO); 335 if (mword == 0) 336 continue; 337 mword = IFM_SUBTYPE(mword); /* remove media options */ 338 ADD(ic, mword, 0); 339 #ifndef IEEE80211_STA_ONLY 340 if (ic->ic_caps & IEEE80211_C_IBSS) 341 ADD(ic, mword, IFM_IEEE80211_IBSS); 342 if (ic->ic_caps & IEEE80211_C_HOSTAP) 343 ADD(ic, mword, IFM_IEEE80211_HOSTAP); 344 if (ic->ic_caps & IEEE80211_C_AHDEMO) 345 ADD(ic, mword, IFM_IEEE80211_ADHOC); 346 #endif 347 if (ic->ic_caps & IEEE80211_C_MONITOR) 348 ADD(ic, mword, IFM_IEEE80211_MONITOR); 349 } 350 ieee80211_media_status(ifp, &imr); 351 ifmedia_set(&ic->ic_media, imr.ifm_active); 352 353 if (maxrate) 354 ifp->if_baudrate = IF_Mbps(maxrate); 355 356 #undef ADD 357 } 358 359 int 360 ieee80211_findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, 361 int rate) 362 { 363 #define IEEERATE(_ic,_m,_i) \ 364 ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL) 365 int i, nrates = ic->ic_sup_rates[mode].rs_nrates; 366 for (i = 0; i < nrates; i++) 367 if (IEEERATE(ic, mode, i) == rate) 368 return i; 369 return -1; 370 #undef IEEERATE 371 } 372 373 /* 374 * Handle a media change request. 375 */ 376 int 377 ieee80211_media_change(struct ifnet *ifp) 378 { 379 struct ieee80211com *ic = (void *)ifp; 380 struct ifmedia_entry *ime; 381 enum ieee80211_opmode newopmode; 382 enum ieee80211_phymode newphymode; 383 int i, j, newrate, error = 0; 384 385 ime = ic->ic_media.ifm_cur; 386 /* 387 * First, identify the phy mode. 388 */ 389 switch (IFM_MODE(ime->ifm_media)) { 390 case IFM_IEEE80211_11A: 391 newphymode = IEEE80211_MODE_11A; 392 break; 393 case IFM_IEEE80211_11B: 394 newphymode = IEEE80211_MODE_11B; 395 break; 396 case IFM_IEEE80211_11G: 397 newphymode = IEEE80211_MODE_11G; 398 break; 399 case IFM_AUTO: 400 newphymode = IEEE80211_MODE_AUTO; 401 break; 402 default: 403 return EINVAL; 404 } 405 /* 406 * Turbo mode is an ``option''. Eventually it 407 * needs to be applied to 11g too. 408 */ 409 if (ime->ifm_media & IFM_IEEE80211_TURBO) { 410 if (newphymode != IEEE80211_MODE_11A) 411 return EINVAL; 412 newphymode = IEEE80211_MODE_TURBO; 413 } 414 /* 415 * Validate requested mode is available. 416 */ 417 if ((ic->ic_modecaps & (1<<newphymode)) == 0) 418 return EINVAL; 419 420 /* 421 * Next, the fixed/variable rate. 422 */ 423 i = -1; 424 if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) { 425 /* 426 * Convert media subtype to rate. 427 */ 428 newrate = ieee80211_media2rate(ime->ifm_media); 429 if (newrate == 0) 430 return EINVAL; 431 /* 432 * Check the rate table for the specified/current phy. 433 */ 434 if (newphymode == IEEE80211_MODE_AUTO) { 435 /* 436 * In autoselect mode search for the rate. 437 */ 438 for (j = IEEE80211_MODE_11A; 439 j < IEEE80211_MODE_MAX; j++) { 440 if ((ic->ic_modecaps & (1<<j)) == 0) 441 continue; 442 i = ieee80211_findrate(ic, j, newrate); 443 if (i != -1) { 444 /* lock mode too */ 445 newphymode = j; 446 break; 447 } 448 } 449 } else { 450 i = ieee80211_findrate(ic, newphymode, newrate); 451 } 452 if (i == -1) /* mode/rate mismatch */ 453 return EINVAL; 454 } 455 /* NB: defer rate setting to later */ 456 457 /* 458 * Deduce new operating mode but don't install it just yet. 459 */ 460 #ifndef IEEE80211_STA_ONLY 461 if (ime->ifm_media & IFM_IEEE80211_ADHOC) 462 newopmode = IEEE80211_M_AHDEMO; 463 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP) 464 newopmode = IEEE80211_M_HOSTAP; 465 else if (ime->ifm_media & IFM_IEEE80211_IBSS) 466 newopmode = IEEE80211_M_IBSS; 467 else 468 #endif 469 if (ime->ifm_media & IFM_IEEE80211_MONITOR) 470 newopmode = IEEE80211_M_MONITOR; 471 else 472 newopmode = IEEE80211_M_STA; 473 474 #ifndef IEEE80211_STA_ONLY 475 /* 476 * Autoselect doesn't make sense when operating as an AP. 477 * If no phy mode has been selected, pick one and lock it 478 * down so rate tables can be used in forming beacon frames 479 * and the like. 480 */ 481 if (newopmode == IEEE80211_M_HOSTAP && 482 newphymode == IEEE80211_MODE_AUTO) { 483 for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++) 484 if (ic->ic_modecaps & (1<<j)) { 485 newphymode = j; 486 break; 487 } 488 } 489 #endif 490 491 /* 492 * Handle phy mode change. 493 */ 494 if (ic->ic_curmode != newphymode) { /* change phy mode */ 495 error = ieee80211_setmode(ic, newphymode); 496 if (error != 0) 497 return error; 498 error = ENETRESET; 499 } 500 501 /* 502 * Committed to changes, install the rate setting. 503 */ 504 if (ic->ic_fixed_rate != i) { 505 ic->ic_fixed_rate = i; /* set fixed tx rate */ 506 error = ENETRESET; 507 } 508 509 /* 510 * Handle operating mode change. 511 */ 512 if (ic->ic_opmode != newopmode) { 513 ic->ic_opmode = newopmode; 514 #ifndef IEEE80211_STA_ONLY 515 switch (newopmode) { 516 case IEEE80211_M_AHDEMO: 517 case IEEE80211_M_HOSTAP: 518 case IEEE80211_M_STA: 519 case IEEE80211_M_MONITOR: 520 ic->ic_flags &= ~IEEE80211_F_IBSSON; 521 break; 522 case IEEE80211_M_IBSS: 523 ic->ic_flags |= IEEE80211_F_IBSSON; 524 break; 525 } 526 #endif 527 /* 528 * Yech, slot time may change depending on the 529 * operating mode so reset it to be sure everything 530 * is setup appropriately. 531 */ 532 ieee80211_reset_erp(ic); 533 error = ENETRESET; 534 } 535 #ifdef notdef 536 if (error == 0) 537 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media); 538 #endif 539 return error; 540 } 541 542 void 543 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr) 544 { 545 struct ieee80211com *ic = (void *)ifp; 546 const struct ieee80211_node *ni = NULL; 547 548 imr->ifm_status = IFM_AVALID; 549 imr->ifm_active = IFM_IEEE80211; 550 if (ic->ic_state == IEEE80211_S_RUN) 551 imr->ifm_status |= IFM_ACTIVE; 552 imr->ifm_active |= IFM_AUTO; 553 switch (ic->ic_opmode) { 554 case IEEE80211_M_STA: 555 ni = ic->ic_bss; 556 /* calculate rate subtype */ 557 imr->ifm_active |= ieee80211_rate2media(ic, 558 ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode); 559 break; 560 #ifndef IEEE80211_STA_ONLY 561 case IEEE80211_M_IBSS: 562 imr->ifm_active |= IFM_IEEE80211_IBSS; 563 break; 564 case IEEE80211_M_AHDEMO: 565 imr->ifm_active |= IFM_IEEE80211_ADHOC; 566 break; 567 case IEEE80211_M_HOSTAP: 568 imr->ifm_active |= IFM_IEEE80211_HOSTAP; 569 break; 570 #endif 571 case IEEE80211_M_MONITOR: 572 imr->ifm_active |= IFM_IEEE80211_MONITOR; 573 break; 574 default: 575 break; 576 } 577 switch (ic->ic_curmode) { 578 case IEEE80211_MODE_11A: 579 imr->ifm_active |= IFM_IEEE80211_11A; 580 break; 581 case IEEE80211_MODE_11B: 582 imr->ifm_active |= IFM_IEEE80211_11B; 583 break; 584 case IEEE80211_MODE_11G: 585 imr->ifm_active |= IFM_IEEE80211_11G; 586 break; 587 case IEEE80211_MODE_TURBO: 588 imr->ifm_active |= IFM_IEEE80211_11A 589 | IFM_IEEE80211_TURBO; 590 break; 591 } 592 } 593 594 void 595 ieee80211_watchdog(struct ifnet *ifp) 596 { 597 struct ieee80211com *ic = (void *)ifp; 598 599 if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0) 600 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 601 602 if (ic->ic_mgt_timer != 0) 603 ifp->if_timer = 1; 604 } 605 606 const struct ieee80211_rateset ieee80211_std_rateset_11a = 607 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } }; 608 609 const struct ieee80211_rateset ieee80211_std_rateset_11b = 610 { 4, { 2, 4, 11, 22 } }; 611 612 const struct ieee80211_rateset ieee80211_std_rateset_11g = 613 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } }; 614 615 /* 616 * Mark the basic rates for the 11g rate table based on the 617 * operating mode. For real 11g we mark all the 11b rates 618 * and 6, 12, and 24 OFDM. For 11b compatibility we mark only 619 * 11b rates. There's also a pseudo 11a-mode used to mark only 620 * the basic OFDM rates. 621 */ 622 void 623 ieee80211_setbasicrates(struct ieee80211com *ic) 624 { 625 static const struct ieee80211_rateset basic[] = { 626 { 0 }, /* IEEE80211_MODE_AUTO */ 627 { 3, { 12, 24, 48 } }, /* IEEE80211_MODE_11A */ 628 { 2, { 2, 4 } }, /* IEEE80211_MODE_11B */ 629 { 4, { 2, 4, 11, 22 } }, /* IEEE80211_MODE_11G */ 630 { 0 }, /* IEEE80211_MODE_TURBO */ 631 }; 632 enum ieee80211_phymode mode; 633 struct ieee80211_rateset *rs; 634 int i, j; 635 636 for (mode = 0; mode < IEEE80211_MODE_MAX; mode++) { 637 rs = &ic->ic_sup_rates[mode]; 638 for (i = 0; i < rs->rs_nrates; i++) { 639 rs->rs_rates[i] &= IEEE80211_RATE_VAL; 640 for (j = 0; j < basic[mode].rs_nrates; j++) { 641 if (basic[mode].rs_rates[j] == 642 rs->rs_rates[i]) { 643 rs->rs_rates[i] |= 644 IEEE80211_RATE_BASIC; 645 break; 646 } 647 } 648 } 649 } 650 } 651 652 /* 653 * Set the current phy mode and recalculate the active channel 654 * set based on the available channels for this mode. Also 655 * select a new default/current channel if the current one is 656 * inappropriate for this mode. 657 */ 658 int 659 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode) 660 { 661 #define N(a) (sizeof(a) / sizeof(a[0])) 662 struct ifnet *ifp = &ic->ic_if; 663 static const u_int chanflags[] = { 664 0, /* IEEE80211_MODE_AUTO */ 665 IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */ 666 IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */ 667 IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */ 668 IEEE80211_CHAN_T, /* IEEE80211_MODE_TURBO */ 669 }; 670 const struct ieee80211_channel *c; 671 u_int modeflags; 672 int i; 673 674 /* validate new mode */ 675 if ((ic->ic_modecaps & (1<<mode)) == 0) { 676 DPRINTF(("mode %u not supported (caps 0x%x)\n", 677 mode, ic->ic_modecaps)); 678 return EINVAL; 679 } 680 681 /* 682 * Verify at least one channel is present in the available 683 * channel list before committing to the new mode. 684 */ 685 if (mode >= N(chanflags)) 686 panic("Unexpected mode %u", mode); 687 modeflags = chanflags[mode]; 688 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 689 c = &ic->ic_channels[i]; 690 if (mode == IEEE80211_MODE_AUTO) { 691 /* ignore turbo channels for autoselect */ 692 if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0) 693 break; 694 } else { 695 if ((c->ic_flags & modeflags) == modeflags) 696 break; 697 } 698 } 699 if (i > IEEE80211_CHAN_MAX) { 700 DPRINTF(("no channels found for mode %u\n", mode)); 701 return EINVAL; 702 } 703 704 /* 705 * Calculate the active channel set. 706 */ 707 memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active)); 708 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 709 c = &ic->ic_channels[i]; 710 if (mode == IEEE80211_MODE_AUTO) { 711 /* take anything but pure turbo channels */ 712 if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0) 713 setbit(ic->ic_chan_active, i); 714 } else { 715 if ((c->ic_flags & modeflags) == modeflags) 716 setbit(ic->ic_chan_active, i); 717 } 718 } 719 /* 720 * If no current/default channel is setup or the current 721 * channel is wrong for the mode then pick the first 722 * available channel from the active list. This is likely 723 * not the right one. 724 */ 725 if (ic->ic_ibss_chan == NULL || isclr(ic->ic_chan_active, 726 ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) { 727 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) 728 if (isset(ic->ic_chan_active, i)) { 729 ic->ic_ibss_chan = &ic->ic_channels[i]; 730 break; 731 } 732 if ((ic->ic_ibss_chan == NULL) || isclr(ic->ic_chan_active, 733 ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) 734 panic("Bad IBSS channel %u\n", 735 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)); 736 } 737 738 /* 739 * Reset the scan state for the new mode. This avoids scanning 740 * of invalid channels, ie. 5GHz channels in 11b mode. 741 */ 742 ieee80211_reset_scan(ifp); 743 744 ic->ic_curmode = mode; 745 ieee80211_reset_erp(ic); /* reset ERP state */ 746 747 return 0; 748 #undef N 749 } 750 751 enum ieee80211_phymode 752 ieee80211_next_mode(struct ifnet *ifp) 753 { 754 struct ieee80211com *ic = (void *)ifp; 755 756 if (IFM_MODE(ic->ic_media.ifm_cur->ifm_media) != IFM_AUTO) { 757 /* 758 * Reset the scan state and indicate a wrap around 759 * if we're running in a fixed, user-specified phy mode. 760 */ 761 ieee80211_reset_scan(ifp); 762 return (IEEE80211_MODE_AUTO); 763 } 764 765 /* 766 * Get the next supported mode 767 */ 768 for (++ic->ic_curmode; 769 ic->ic_curmode <= IEEE80211_MODE_TURBO; 770 ic->ic_curmode++) { 771 /* Wrap around and ignore turbo mode */ 772 if (ic->ic_curmode >= IEEE80211_MODE_TURBO) { 773 ic->ic_curmode = IEEE80211_MODE_AUTO; 774 break; 775 } 776 777 if (ic->ic_modecaps & (1 << ic->ic_curmode)) 778 break; 779 } 780 781 ieee80211_setmode(ic, ic->ic_curmode); 782 783 return (ic->ic_curmode); 784 } 785 786 /* 787 * Return the phy mode for with the specified channel so the 788 * caller can select a rate set. This is problematic and the 789 * work here assumes how things work elsewhere in this code. 790 * 791 * XXX never returns turbo modes -dcy 792 */ 793 enum ieee80211_phymode 794 ieee80211_chan2mode(struct ieee80211com *ic, 795 const struct ieee80211_channel *chan) 796 { 797 /* 798 * NB: this assumes the channel would not be supplied to us 799 * unless it was already compatible with the current mode. 800 */ 801 if (ic->ic_curmode != IEEE80211_MODE_AUTO || 802 chan == IEEE80211_CHAN_ANYC) 803 return ic->ic_curmode; 804 /* 805 * In autoselect mode; deduce a mode based on the channel 806 * characteristics. We assume that turbo-only channels 807 * are not considered when the channel set is constructed. 808 */ 809 if (IEEE80211_IS_CHAN_T(chan)) 810 return IEEE80211_MODE_TURBO; 811 else if (IEEE80211_IS_CHAN_5GHZ(chan)) 812 return IEEE80211_MODE_11A; 813 else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN)) 814 return IEEE80211_MODE_11G; 815 else 816 return IEEE80211_MODE_11B; 817 } 818 819 /* 820 * convert IEEE80211 rate value to ifmedia subtype. 821 * ieee80211 rate is in unit of 0.5Mbps. 822 */ 823 int 824 ieee80211_rate2media(struct ieee80211com *ic, int rate, 825 enum ieee80211_phymode mode) 826 { 827 #define N(a) (sizeof(a) / sizeof(a[0])) 828 static const struct { 829 u_int m; /* rate + mode */ 830 u_int r; /* if_media rate */ 831 } rates[] = { 832 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 }, 833 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 }, 834 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 }, 835 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 }, 836 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 }, 837 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 }, 838 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 }, 839 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 }, 840 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 }, 841 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 }, 842 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 }, 843 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 }, 844 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 }, 845 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 }, 846 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 }, 847 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 }, 848 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 }, 849 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 }, 850 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 }, 851 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 }, 852 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 }, 853 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 }, 854 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 }, 855 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 }, 856 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 }, 857 /* NB: OFDM72 doesn't really exist so we don't handle it */ 858 }; 859 u_int mask, i; 860 861 mask = rate & IEEE80211_RATE_VAL; 862 switch (mode) { 863 case IEEE80211_MODE_11A: 864 case IEEE80211_MODE_TURBO: 865 mask |= IFM_IEEE80211_11A; 866 break; 867 case IEEE80211_MODE_11B: 868 mask |= IFM_IEEE80211_11B; 869 break; 870 case IEEE80211_MODE_AUTO: 871 /* NB: hack, 11g matches both 11b+11a rates */ 872 /* FALLTHROUGH */ 873 case IEEE80211_MODE_11G: 874 mask |= IFM_IEEE80211_11G; 875 break; 876 } 877 for (i = 0; i < N(rates); i++) 878 if (rates[i].m == mask) 879 return rates[i].r; 880 return IFM_AUTO; 881 #undef N 882 } 883 884 int 885 ieee80211_media2rate(int mword) 886 { 887 #define N(a) (sizeof(a) / sizeof(a[0])) 888 int i; 889 static const struct { 890 int subtype; 891 int rate; 892 } ieeerates[] = { 893 { IFM_AUTO, -1 }, 894 { IFM_MANUAL, 0 }, 895 { IFM_NONE, 0 }, 896 { IFM_IEEE80211_DS1, 2 }, 897 { IFM_IEEE80211_DS2, 4 }, 898 { IFM_IEEE80211_DS5, 11 }, 899 { IFM_IEEE80211_DS11, 22 }, 900 { IFM_IEEE80211_DS22, 44 }, 901 { IFM_IEEE80211_OFDM6, 12 }, 902 { IFM_IEEE80211_OFDM9, 18 }, 903 { IFM_IEEE80211_OFDM12, 24 }, 904 { IFM_IEEE80211_OFDM18, 36 }, 905 { IFM_IEEE80211_OFDM24, 48 }, 906 { IFM_IEEE80211_OFDM36, 72 }, 907 { IFM_IEEE80211_OFDM48, 96 }, 908 { IFM_IEEE80211_OFDM54, 108 }, 909 { IFM_IEEE80211_OFDM72, 144 }, 910 }; 911 for (i = 0; i < N(ieeerates); i++) { 912 if (ieeerates[i].subtype == IFM_SUBTYPE(mword)) 913 return ieeerates[i].rate; 914 } 915 return 0; 916 #undef N 917 } 918 919 /* 920 * Convert bit rate (in 0.5Mbps units) to PLCP signal (R4-R1) and vice versa. 921 */ 922 u_int8_t 923 ieee80211_rate2plcp(u_int8_t rate, enum ieee80211_phymode mode) 924 { 925 rate &= IEEE80211_RATE_VAL; 926 927 if (mode == IEEE80211_MODE_11B) { 928 /* IEEE Std 802.11b-1999 page 15, subclause 18.2.3.3 */ 929 switch (rate) { 930 case 2: return 10; 931 case 4: return 20; 932 case 11: return 55; 933 case 22: return 110; 934 /* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */ 935 case 44: return 220; 936 } 937 } else if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11A) { 938 /* IEEE Std 802.11a-1999 page 14, subclause 17.3.4.1 */ 939 switch (rate) { 940 case 12: return 0x0b; 941 case 18: return 0x0f; 942 case 24: return 0x0a; 943 case 36: return 0x0e; 944 case 48: return 0x09; 945 case 72: return 0x0d; 946 case 96: return 0x08; 947 case 108: return 0x0c; 948 } 949 } else 950 panic("Unexpected mode %u", mode); 951 952 DPRINTF(("unsupported rate %u\n", rate)); 953 954 return 0; 955 } 956 957 u_int8_t 958 ieee80211_plcp2rate(u_int8_t plcp, enum ieee80211_phymode mode) 959 { 960 if (mode == IEEE80211_MODE_11B) { 961 /* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */ 962 switch (plcp) { 963 case 10: return 2; 964 case 20: return 4; 965 case 55: return 11; 966 case 110: return 22; 967 /* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */ 968 case 220: return 44; 969 } 970 } else if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11A) { 971 /* IEEE Std 802.11a-1999 page 14, subclause 17.3.4.1 */ 972 switch (plcp) { 973 case 0x0b: return 12; 974 case 0x0f: return 18; 975 case 0x0a: return 24; 976 case 0x0e: return 36; 977 case 0x09: return 48; 978 case 0x0d: return 72; 979 case 0x08: return 96; 980 case 0x0c: return 108; 981 } 982 } else 983 panic("unexpected mode %u", mode); 984 985 DPRINTF(("unsupported plcp %u\n", plcp)); 986 987 return 0; 988 } 989