1 /* $OpenBSD: ieee80211.c,v 1.39 2010/08/07 03:50:02 krw 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 (ic->ic_opmode != IEEE80211_M_STA || 552 !(ic->ic_flags & IEEE80211_F_RSNON) || 553 ic->ic_bss->ni_port_valid)) 554 imr->ifm_status |= IFM_ACTIVE; 555 imr->ifm_active |= IFM_AUTO; 556 switch (ic->ic_opmode) { 557 case IEEE80211_M_STA: 558 ni = ic->ic_bss; 559 /* calculate rate subtype */ 560 imr->ifm_active |= ieee80211_rate2media(ic, 561 ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode); 562 break; 563 #ifndef IEEE80211_STA_ONLY 564 case IEEE80211_M_IBSS: 565 imr->ifm_active |= IFM_IEEE80211_IBSS; 566 break; 567 case IEEE80211_M_AHDEMO: 568 imr->ifm_active |= IFM_IEEE80211_ADHOC; 569 break; 570 case IEEE80211_M_HOSTAP: 571 imr->ifm_active |= IFM_IEEE80211_HOSTAP; 572 break; 573 #endif 574 case IEEE80211_M_MONITOR: 575 imr->ifm_active |= IFM_IEEE80211_MONITOR; 576 break; 577 default: 578 break; 579 } 580 switch (ic->ic_curmode) { 581 case IEEE80211_MODE_11A: 582 imr->ifm_active |= IFM_IEEE80211_11A; 583 break; 584 case IEEE80211_MODE_11B: 585 imr->ifm_active |= IFM_IEEE80211_11B; 586 break; 587 case IEEE80211_MODE_11G: 588 imr->ifm_active |= IFM_IEEE80211_11G; 589 break; 590 case IEEE80211_MODE_TURBO: 591 imr->ifm_active |= IFM_IEEE80211_11A 592 | IFM_IEEE80211_TURBO; 593 break; 594 } 595 } 596 597 void 598 ieee80211_watchdog(struct ifnet *ifp) 599 { 600 struct ieee80211com *ic = (void *)ifp; 601 602 if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0) 603 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 604 605 if (ic->ic_mgt_timer != 0) 606 ifp->if_timer = 1; 607 } 608 609 const struct ieee80211_rateset ieee80211_std_rateset_11a = 610 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } }; 611 612 const struct ieee80211_rateset ieee80211_std_rateset_11b = 613 { 4, { 2, 4, 11, 22 } }; 614 615 const struct ieee80211_rateset ieee80211_std_rateset_11g = 616 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } }; 617 618 /* 619 * Mark the basic rates for the 11g rate table based on the 620 * operating mode. For real 11g we mark all the 11b rates 621 * and 6, 12, and 24 OFDM. For 11b compatibility we mark only 622 * 11b rates. There's also a pseudo 11a-mode used to mark only 623 * the basic OFDM rates. 624 */ 625 void 626 ieee80211_setbasicrates(struct ieee80211com *ic) 627 { 628 static const struct ieee80211_rateset basic[] = { 629 { 0 }, /* IEEE80211_MODE_AUTO */ 630 { 3, { 12, 24, 48 } }, /* IEEE80211_MODE_11A */ 631 { 2, { 2, 4 } }, /* IEEE80211_MODE_11B */ 632 { 4, { 2, 4, 11, 22 } }, /* IEEE80211_MODE_11G */ 633 { 0 }, /* IEEE80211_MODE_TURBO */ 634 }; 635 enum ieee80211_phymode mode; 636 struct ieee80211_rateset *rs; 637 int i, j; 638 639 for (mode = 0; mode < IEEE80211_MODE_MAX; mode++) { 640 rs = &ic->ic_sup_rates[mode]; 641 for (i = 0; i < rs->rs_nrates; i++) { 642 rs->rs_rates[i] &= IEEE80211_RATE_VAL; 643 for (j = 0; j < basic[mode].rs_nrates; j++) { 644 if (basic[mode].rs_rates[j] == 645 rs->rs_rates[i]) { 646 rs->rs_rates[i] |= 647 IEEE80211_RATE_BASIC; 648 break; 649 } 650 } 651 } 652 } 653 } 654 655 /* 656 * Set the current phy mode and recalculate the active channel 657 * set based on the available channels for this mode. Also 658 * select a new default/current channel if the current one is 659 * inappropriate for this mode. 660 */ 661 int 662 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode) 663 { 664 #define N(a) (sizeof(a) / sizeof(a[0])) 665 struct ifnet *ifp = &ic->ic_if; 666 static const u_int chanflags[] = { 667 0, /* IEEE80211_MODE_AUTO */ 668 IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */ 669 IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */ 670 IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */ 671 IEEE80211_CHAN_T, /* IEEE80211_MODE_TURBO */ 672 }; 673 const struct ieee80211_channel *c; 674 u_int modeflags; 675 int i; 676 677 /* validate new mode */ 678 if ((ic->ic_modecaps & (1<<mode)) == 0) { 679 DPRINTF(("mode %u not supported (caps 0x%x)\n", 680 mode, ic->ic_modecaps)); 681 return EINVAL; 682 } 683 684 /* 685 * Verify at least one channel is present in the available 686 * channel list before committing to the new mode. 687 */ 688 if (mode >= N(chanflags)) 689 panic("Unexpected mode %u", mode); 690 modeflags = chanflags[mode]; 691 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 692 c = &ic->ic_channels[i]; 693 if (mode == IEEE80211_MODE_AUTO) { 694 /* ignore turbo channels for autoselect */ 695 if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0) 696 break; 697 } else { 698 if ((c->ic_flags & modeflags) == modeflags) 699 break; 700 } 701 } 702 if (i > IEEE80211_CHAN_MAX) { 703 DPRINTF(("no channels found for mode %u\n", mode)); 704 return EINVAL; 705 } 706 707 /* 708 * Calculate the active channel set. 709 */ 710 memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active)); 711 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 712 c = &ic->ic_channels[i]; 713 if (mode == IEEE80211_MODE_AUTO) { 714 /* take anything but pure turbo channels */ 715 if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0) 716 setbit(ic->ic_chan_active, i); 717 } else { 718 if ((c->ic_flags & modeflags) == modeflags) 719 setbit(ic->ic_chan_active, i); 720 } 721 } 722 /* 723 * If no current/default channel is setup or the current 724 * channel is wrong for the mode then pick the first 725 * available channel from the active list. This is likely 726 * not the right one. 727 */ 728 if (ic->ic_ibss_chan == NULL || isclr(ic->ic_chan_active, 729 ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) { 730 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) 731 if (isset(ic->ic_chan_active, i)) { 732 ic->ic_ibss_chan = &ic->ic_channels[i]; 733 break; 734 } 735 if ((ic->ic_ibss_chan == NULL) || isclr(ic->ic_chan_active, 736 ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) 737 panic("Bad IBSS channel %u", 738 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)); 739 } 740 741 /* 742 * Reset the scan state for the new mode. This avoids scanning 743 * of invalid channels, ie. 5GHz channels in 11b mode. 744 */ 745 ieee80211_reset_scan(ifp); 746 747 ic->ic_curmode = mode; 748 ieee80211_reset_erp(ic); /* reset ERP state */ 749 750 return 0; 751 #undef N 752 } 753 754 enum ieee80211_phymode 755 ieee80211_next_mode(struct ifnet *ifp) 756 { 757 struct ieee80211com *ic = (void *)ifp; 758 759 if (IFM_MODE(ic->ic_media.ifm_cur->ifm_media) != IFM_AUTO) { 760 /* 761 * Reset the scan state and indicate a wrap around 762 * if we're running in a fixed, user-specified phy mode. 763 */ 764 ieee80211_reset_scan(ifp); 765 return (IEEE80211_MODE_AUTO); 766 } 767 768 /* 769 * Get the next supported mode 770 */ 771 for (++ic->ic_curmode; 772 ic->ic_curmode <= IEEE80211_MODE_TURBO; 773 ic->ic_curmode++) { 774 /* Wrap around and ignore turbo mode */ 775 if (ic->ic_curmode >= IEEE80211_MODE_TURBO) { 776 ic->ic_curmode = IEEE80211_MODE_AUTO; 777 break; 778 } 779 780 if (ic->ic_modecaps & (1 << ic->ic_curmode)) 781 break; 782 } 783 784 ieee80211_setmode(ic, ic->ic_curmode); 785 786 return (ic->ic_curmode); 787 } 788 789 /* 790 * Return the phy mode for with the specified channel so the 791 * caller can select a rate set. This is problematic and the 792 * work here assumes how things work elsewhere in this code. 793 * 794 * XXX never returns turbo modes -dcy 795 */ 796 enum ieee80211_phymode 797 ieee80211_chan2mode(struct ieee80211com *ic, 798 const struct ieee80211_channel *chan) 799 { 800 /* 801 * NB: this assumes the channel would not be supplied to us 802 * unless it was already compatible with the current mode. 803 */ 804 if (ic->ic_curmode != IEEE80211_MODE_AUTO || 805 chan == IEEE80211_CHAN_ANYC) 806 return ic->ic_curmode; 807 /* 808 * In autoselect mode; deduce a mode based on the channel 809 * characteristics. We assume that turbo-only channels 810 * are not considered when the channel set is constructed. 811 */ 812 if (IEEE80211_IS_CHAN_T(chan)) 813 return IEEE80211_MODE_TURBO; 814 else if (IEEE80211_IS_CHAN_5GHZ(chan)) 815 return IEEE80211_MODE_11A; 816 else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN)) 817 return IEEE80211_MODE_11G; 818 else 819 return IEEE80211_MODE_11B; 820 } 821 822 /* 823 * convert IEEE80211 rate value to ifmedia subtype. 824 * ieee80211 rate is in unit of 0.5Mbps. 825 */ 826 int 827 ieee80211_rate2media(struct ieee80211com *ic, int rate, 828 enum ieee80211_phymode mode) 829 { 830 #define N(a) (sizeof(a) / sizeof(a[0])) 831 static const struct { 832 u_int m; /* rate + mode */ 833 u_int r; /* if_media rate */ 834 } rates[] = { 835 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 }, 836 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 }, 837 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 }, 838 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 }, 839 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 }, 840 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 }, 841 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 }, 842 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 }, 843 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 }, 844 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 }, 845 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 }, 846 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 }, 847 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 }, 848 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 }, 849 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 }, 850 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 }, 851 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 }, 852 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 }, 853 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 }, 854 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 }, 855 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 }, 856 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 }, 857 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 }, 858 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 }, 859 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 }, 860 /* NB: OFDM72 doesn't really exist so we don't handle it */ 861 }; 862 u_int mask, i; 863 864 mask = rate & IEEE80211_RATE_VAL; 865 switch (mode) { 866 case IEEE80211_MODE_11A: 867 case IEEE80211_MODE_TURBO: 868 mask |= IFM_IEEE80211_11A; 869 break; 870 case IEEE80211_MODE_11B: 871 mask |= IFM_IEEE80211_11B; 872 break; 873 case IEEE80211_MODE_AUTO: 874 /* NB: hack, 11g matches both 11b+11a rates */ 875 /* FALLTHROUGH */ 876 case IEEE80211_MODE_11G: 877 mask |= IFM_IEEE80211_11G; 878 break; 879 } 880 for (i = 0; i < N(rates); i++) 881 if (rates[i].m == mask) 882 return rates[i].r; 883 return IFM_AUTO; 884 #undef N 885 } 886 887 int 888 ieee80211_media2rate(int mword) 889 { 890 #define N(a) (sizeof(a) / sizeof(a[0])) 891 int i; 892 static const struct { 893 int subtype; 894 int rate; 895 } ieeerates[] = { 896 { IFM_AUTO, -1 }, 897 { IFM_MANUAL, 0 }, 898 { IFM_NONE, 0 }, 899 { IFM_IEEE80211_DS1, 2 }, 900 { IFM_IEEE80211_DS2, 4 }, 901 { IFM_IEEE80211_DS5, 11 }, 902 { IFM_IEEE80211_DS11, 22 }, 903 { IFM_IEEE80211_DS22, 44 }, 904 { IFM_IEEE80211_OFDM6, 12 }, 905 { IFM_IEEE80211_OFDM9, 18 }, 906 { IFM_IEEE80211_OFDM12, 24 }, 907 { IFM_IEEE80211_OFDM18, 36 }, 908 { IFM_IEEE80211_OFDM24, 48 }, 909 { IFM_IEEE80211_OFDM36, 72 }, 910 { IFM_IEEE80211_OFDM48, 96 }, 911 { IFM_IEEE80211_OFDM54, 108 }, 912 { IFM_IEEE80211_OFDM72, 144 }, 913 }; 914 for (i = 0; i < N(ieeerates); i++) { 915 if (ieeerates[i].subtype == IFM_SUBTYPE(mword)) 916 return ieeerates[i].rate; 917 } 918 return 0; 919 #undef N 920 } 921 922 /* 923 * Convert bit rate (in 0.5Mbps units) to PLCP signal (R4-R1) and vice versa. 924 */ 925 u_int8_t 926 ieee80211_rate2plcp(u_int8_t rate, enum ieee80211_phymode mode) 927 { 928 rate &= IEEE80211_RATE_VAL; 929 930 if (mode == IEEE80211_MODE_11B) { 931 /* IEEE Std 802.11b-1999 page 15, subclause 18.2.3.3 */ 932 switch (rate) { 933 case 2: return 10; 934 case 4: return 20; 935 case 11: return 55; 936 case 22: return 110; 937 /* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */ 938 case 44: return 220; 939 } 940 } else if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11A) { 941 /* IEEE Std 802.11a-1999 page 14, subclause 17.3.4.1 */ 942 switch (rate) { 943 case 12: return 0x0b; 944 case 18: return 0x0f; 945 case 24: return 0x0a; 946 case 36: return 0x0e; 947 case 48: return 0x09; 948 case 72: return 0x0d; 949 case 96: return 0x08; 950 case 108: return 0x0c; 951 } 952 } else 953 panic("Unexpected mode %u", mode); 954 955 DPRINTF(("unsupported rate %u\n", rate)); 956 957 return 0; 958 } 959 960 u_int8_t 961 ieee80211_plcp2rate(u_int8_t plcp, enum ieee80211_phymode mode) 962 { 963 if (mode == IEEE80211_MODE_11B) { 964 /* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */ 965 switch (plcp) { 966 case 10: return 2; 967 case 20: return 4; 968 case 55: return 11; 969 case 110: return 22; 970 /* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */ 971 case 220: return 44; 972 } 973 } else if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11A) { 974 /* IEEE Std 802.11a-1999 page 14, subclause 17.3.4.1 */ 975 switch (plcp) { 976 case 0x0b: return 12; 977 case 0x0f: return 18; 978 case 0x0a: return 24; 979 case 0x0e: return 36; 980 case 0x09: return 48; 981 case 0x0d: return 72; 982 case 0x08: return 96; 983 case 0x0c: return 108; 984 } 985 } else 986 panic("unexpected mode %u", mode); 987 988 DPRINTF(("unsupported plcp %u\n", plcp)); 989 990 return 0; 991 } 992