ieee80211.c revision 117811
1/*- 2 * Copyright (c) 2001 Atsushi Onoe 3 * Copyright (c) 2002, 2003 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 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * Alternatively, this software may be distributed under the terms of the 18 * GNU General Public License ("GPL") version 2 as published by the Free 19 * Software Foundation. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33#include <sys/cdefs.h> 34__FBSDID("$FreeBSD: head/sys/net80211/ieee80211.c 117811 2003-07-20 21:36:08Z sam $"); 35 36/* 37 * IEEE 802.11 generic handler 38 */ 39 40#include "opt_inet.h" 41 42#include <sys/param.h> 43#include <sys/systm.h> 44#include <sys/mbuf.h> 45#include <sys/malloc.h> 46#include <sys/kernel.h> 47#include <sys/socket.h> 48#include <sys/sockio.h> 49#include <sys/endian.h> 50#include <sys/errno.h> 51#include <sys/bus.h> 52#include <sys/proc.h> 53#include <sys/sysctl.h> 54 55#include <machine/atomic.h> 56 57#include <net/if.h> 58#include <net/if_dl.h> 59#include <net/if_media.h> 60#include <net/if_arp.h> 61#include <net/ethernet.h> 62#include <net/if_llc.h> 63 64#include <net80211/ieee80211_var.h> 65 66#include <net/bpf.h> 67 68#ifdef INET 69#include <netinet/in.h> 70#include <netinet/if_ether.h> 71#endif 72 73#ifdef IEEE80211_DEBUG 74int ieee80211_debug = 0; 75SYSCTL_INT(_debug, OID_AUTO, ieee80211, CTLFLAG_RW, &ieee80211_debug, 76 0, "IEEE 802.11 media debugging printfs"); 77#endif 78 79static void ieee80211_set11gbasicrates(struct ieee80211_rateset *, 80 enum ieee80211_phymode); 81 82static const char *ieee80211_phymode_name[] = { 83 "auto", /* IEEE80211_MODE_AUTO */ 84 "11a", /* IEEE80211_MODE_11A */ 85 "11b", /* IEEE80211_MODE_11B */ 86 "11g", /* IEEE80211_MODE_11G */ 87 "turbo", /* IEEE80211_MODE_TURBO */ 88}; 89 90void 91ieee80211_ifattach(struct ifnet *ifp) 92{ 93 struct ieee80211com *ic = (void *)ifp; 94 struct ieee80211_channel *c; 95 int i; 96 97 ether_ifattach(ifp, ic->ic_myaddr); 98 bpfattach2(ifp, DLT_IEEE802_11, 99 sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf); 100 ieee80211_crypto_attach(ifp); 101 102 /* 103 * Fill in 802.11 available channel set, mark 104 * all available channels as active, and pick 105 * a default channel if not already specified. 106 */ 107 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail)); 108 ic->ic_modecaps |= 1<<IEEE80211_MODE_AUTO; 109 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 110 c = &ic->ic_channels[i]; 111 if (c->ic_flags) { 112 /* 113 * Verify driver passed us valid data. 114 */ 115 if (i != ieee80211_chan2ieee(ic, c)) { 116 if_printf(ifp, "bad channel ignored; " 117 "freq %u flags %x number %u\n", 118 c->ic_freq, c->ic_flags, i); 119 c->ic_flags = 0; /* NB: remove */ 120 continue; 121 } 122 setbit(ic->ic_chan_avail, i); 123 /* 124 * Identify mode capabilities. 125 */ 126 if (IEEE80211_IS_CHAN_A(c)) 127 ic->ic_modecaps |= 1<<IEEE80211_MODE_11A; 128 if (IEEE80211_IS_CHAN_B(c)) 129 ic->ic_modecaps |= 1<<IEEE80211_MODE_11B; 130 if (IEEE80211_IS_CHAN_PUREG(c)) 131 ic->ic_modecaps |= 1<<IEEE80211_MODE_11G; 132 if (IEEE80211_IS_CHAN_T(c)) 133 ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO; 134 } 135 } 136 /* validate ic->ic_curmode */ 137 if ((ic->ic_modecaps & (1<<ic->ic_curmode)) == 0) 138 ic->ic_curmode = IEEE80211_MODE_AUTO; 139 140 (void) ieee80211_setmode(ic, ic->ic_curmode); 141 142 ic->ic_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */ 143 if (ic->ic_lintval == 0) 144 ic->ic_lintval = 100; /* default sleep */ 145 ic->ic_bmisstimeout = 7*ic->ic_lintval; /* default 7 beacons */ 146 147 ieee80211_node_attach(ifp); 148 ieee80211_proto_attach(ifp); 149} 150 151void 152ieee80211_ifdetach(struct ifnet *ifp) 153{ 154 struct ieee80211com *ic = (void *)ifp; 155 156 ieee80211_proto_detach(ifp); 157 ieee80211_crypto_detach(ifp); 158 ieee80211_node_detach(ifp); 159 ifmedia_removeall(&ic->ic_media); 160 bpfdetach(ifp); 161 ether_ifdetach(ifp); 162} 163 164/* 165 * Convert MHz frequency to IEEE channel number. 166 */ 167u_int 168ieee80211_mhz2ieee(u_int freq, u_int flags) 169{ 170 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 171 if (freq == 2484) 172 return 14; 173 if (freq < 2484) 174 return (freq - 2407) / 5; 175 else 176 return 15 + ((freq - 2512) / 20); 177 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */ 178 return (freq - 5000) / 5; 179 } else { /* either, guess */ 180 if (freq == 2484) 181 return 14; 182 if (freq < 2484) 183 return (freq - 2407) / 5; 184 if (freq < 5000) 185 return 15 + ((freq - 2512) / 20); 186 return (freq - 5000) / 5; 187 } 188} 189 190/* 191 * Convert channel to IEEE channel number. 192 */ 193u_int 194ieee80211_chan2ieee(struct ieee80211com *ic, struct ieee80211_channel *c) 195{ 196 if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX]) 197 return c - ic->ic_channels; 198 else if (c == IEEE80211_CHAN_ANYC) 199 return IEEE80211_CHAN_ANY; 200 else if (c != NULL) { 201 if_printf(&ic->ic_if, "invalid channel freq %u flags %x\n", 202 c->ic_freq, c->ic_flags); 203 return 0; /* XXX */ 204 } else { 205 if_printf(&ic->ic_if, "invalid channel (NULL)\n"); 206 return 0; /* XXX */ 207 } 208} 209 210/* 211 * Convert IEEE channel number to MHz frequency. 212 */ 213u_int 214ieee80211_ieee2mhz(u_int chan, u_int flags) 215{ 216 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 217 if (chan == 14) 218 return 2484; 219 if (chan < 14) 220 return 2407 + chan*5; 221 else 222 return 2512 + ((chan-15)*20); 223 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */ 224 return 5000 + (chan*5); 225 } else { /* either, guess */ 226 if (chan == 14) 227 return 2484; 228 if (chan < 14) /* 0-13 */ 229 return 2407 + chan*5; 230 if (chan < 27) /* 15-26 */ 231 return 2512 + ((chan-15)*20); 232 return 5000 + (chan*5); 233 } 234} 235 236/* 237 * Setup the media data structures according to the channel and 238 * rate tables. This must be called by the driver after 239 * ieee80211_attach and before most anything else. 240 */ 241void 242ieee80211_media_init(struct ifnet *ifp, 243 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat) 244{ 245#define ADD(_ic, _s, _o) \ 246 ifmedia_add(&(_ic)->ic_media, \ 247 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL) 248 struct ieee80211com *ic = (void *)ifp; 249 struct ifmediareq imr; 250 int i, j, mode, rate, maxrate, mword, mopt, r; 251 struct ieee80211_rateset *rs; 252 struct ieee80211_rateset allrates; 253 254 /* 255 * Fill in media characteristics. 256 */ 257 ifmedia_init(&ic->ic_media, 0, media_change, media_stat); 258 maxrate = 0; 259 memset(&allrates, 0, sizeof(allrates)); 260 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) { 261 static const u_int mopts[] = { 262 IFM_AUTO, 263 IFM_MAKEMODE(IFM_IEEE80211_11A), 264 IFM_MAKEMODE(IFM_IEEE80211_11B), 265 IFM_MAKEMODE(IFM_IEEE80211_11G), 266 IFM_MAKEMODE(IFM_IEEE80211_11A) | IFM_IEEE80211_TURBO, 267 }; 268 if ((ic->ic_modecaps & (1<<mode)) == 0) 269 continue; 270 mopt = mopts[mode]; 271 ADD(ic, IFM_AUTO, mopt); /* e.g. 11a auto */ 272 if (ic->ic_caps & IEEE80211_C_IBSS) 273 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC); 274 if (ic->ic_caps & IEEE80211_C_HOSTAP) 275 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP); 276 if (ic->ic_caps & IEEE80211_C_AHDEMO) 277 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0); 278 if (mode == IEEE80211_MODE_AUTO) 279 continue; 280 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]); 281 rs = &ic->ic_sup_rates[mode]; 282 for (i = 0; i < rs->rs_nrates; i++) { 283 rate = rs->rs_rates[i]; 284 mword = ieee80211_rate2media(ic, rate, mode); 285 if (mword == 0) 286 continue; 287 printf("%s%d%sMbps", (i != 0 ? " " : ""), 288 (rate & IEEE80211_RATE_VAL) / 2, 289 ((rate & 0x1) != 0 ? ".5" : "")); 290 ADD(ic, mword, mopt); 291 if (ic->ic_caps & IEEE80211_C_IBSS) 292 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC); 293 if (ic->ic_caps & IEEE80211_C_HOSTAP) 294 ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP); 295 if (ic->ic_caps & IEEE80211_C_AHDEMO) 296 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0); 297 /* 298 * Add rate to the collection of all rates. 299 */ 300 r = rate & IEEE80211_RATE_VAL; 301 for (j = 0; j < allrates.rs_nrates; j++) 302 if (allrates.rs_rates[j] == r) 303 break; 304 if (j == allrates.rs_nrates) { 305 /* unique, add to the set */ 306 allrates.rs_rates[j] = r; 307 allrates.rs_nrates++; 308 } 309 rate = (rate & IEEE80211_RATE_VAL) / 2; 310 if (rate > maxrate) 311 maxrate = rate; 312 } 313 printf("\n"); 314 } 315 for (i = 0; i < allrates.rs_nrates; i++) { 316 mword = ieee80211_rate2media(ic, allrates.rs_rates[i], 317 IEEE80211_MODE_AUTO); 318 if (mword == 0) 319 continue; 320 mword = IFM_SUBTYPE(mword); /* remove media options */ 321 ADD(ic, mword, 0); 322 if (ic->ic_caps & IEEE80211_C_IBSS) 323 ADD(ic, mword, IFM_IEEE80211_ADHOC); 324 if (ic->ic_caps & IEEE80211_C_HOSTAP) 325 ADD(ic, mword, IFM_IEEE80211_HOSTAP); 326 if (ic->ic_caps & IEEE80211_C_AHDEMO) 327 ADD(ic, mword, IFM_IEEE80211_ADHOC | IFM_FLAG0); 328 } 329 ieee80211_media_status(ifp, &imr); 330 ifmedia_set(&ic->ic_media, imr.ifm_active); 331 332 if (maxrate) 333 ifp->if_baudrate = IF_Mbps(maxrate); 334#undef ADD 335} 336 337static int 338findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate) 339{ 340#define IEEERATE(_ic,_m,_i) \ 341 ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL) 342 int i, nrates = ic->ic_sup_rates[mode].rs_nrates; 343 for (i = 0; i < nrates; i++) 344 if (IEEERATE(ic, mode, i) == rate) 345 return i; 346 return -1; 347#undef IEEERATE 348} 349 350/* 351 * Handle a media change request. 352 */ 353int 354ieee80211_media_change(struct ifnet *ifp) 355{ 356 struct ieee80211com *ic = (void *)ifp; 357 struct ifmedia_entry *ime; 358 enum ieee80211_opmode newopmode; 359 enum ieee80211_phymode newphymode; 360 int i, j, newrate, error = 0; 361 362 ime = ic->ic_media.ifm_cur; 363 /* 364 * First, identify the phy mode. 365 */ 366 switch (IFM_MODE(ime->ifm_media)) { 367 case IFM_IEEE80211_11A: 368 newphymode = IEEE80211_MODE_11A; 369 break; 370 case IFM_IEEE80211_11B: 371 newphymode = IEEE80211_MODE_11B; 372 break; 373 case IFM_IEEE80211_11G: 374 newphymode = IEEE80211_MODE_11G; 375 break; 376 case IFM_AUTO: 377 newphymode = IEEE80211_MODE_AUTO; 378 break; 379 default: 380 return EINVAL; 381 } 382 /* 383 * Turbo mode is an ``option''. Eventually it 384 * needs to be applied to 11g too. 385 */ 386 if (ime->ifm_media & IFM_IEEE80211_TURBO) { 387 if (newphymode != IEEE80211_MODE_11A) 388 return EINVAL; 389 newphymode = IEEE80211_MODE_TURBO; 390 } 391 /* 392 * Validate requested mode is available. 393 */ 394 if ((ic->ic_modecaps & (1<<newphymode)) == 0) 395 return EINVAL; 396 397 /* 398 * Next, the fixed/variable rate. 399 */ 400 i = -1; 401 if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) { 402 /* 403 * Convert media subtype to rate. 404 */ 405 newrate = ieee80211_media2rate(ime->ifm_media); 406 if (newrate == 0) 407 return EINVAL; 408 /* 409 * Check the rate table for the specified/current phy. 410 */ 411 if (newphymode == IEEE80211_MODE_AUTO) { 412 /* 413 * In autoselect mode search for the rate. 414 */ 415 for (j = IEEE80211_MODE_11A; 416 j < IEEE80211_MODE_MAX; j++) { 417 if ((ic->ic_modecaps & (1<<j)) == 0) 418 continue; 419 i = findrate(ic, j, newrate); 420 if (i != -1) { 421 /* lock mode too */ 422 newphymode = j; 423 break; 424 } 425 } 426 } else { 427 i = findrate(ic, newphymode, newrate); 428 } 429 if (i == -1) /* mode/rate mismatch */ 430 return EINVAL; 431 } 432 /* NB: defer rate setting to later */ 433 434 /* 435 * Deduce new operating mode but don't install it just yet. 436 */ 437 if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) == 438 (IFM_IEEE80211_ADHOC|IFM_FLAG0)) 439 newopmode = IEEE80211_M_AHDEMO; 440 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP) 441 newopmode = IEEE80211_M_HOSTAP; 442 else if (ime->ifm_media & IFM_IEEE80211_ADHOC) 443 newopmode = IEEE80211_M_IBSS; 444 else 445 newopmode = IEEE80211_M_STA; 446 447 /* 448 * Autoselect doesn't make sense when operating as an AP. 449 * If no phy mode has been selected, pick one and lock it 450 * down so rate tables can be used in forming beacon frames 451 * and the like. 452 */ 453 if (newopmode == IEEE80211_M_HOSTAP && 454 newphymode == IEEE80211_MODE_AUTO) { 455 for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++) 456 if (ic->ic_modecaps & (1<<j)) { 457 newphymode = j; 458 break; 459 } 460 } 461 462 /* 463 * Handle phy mode change. 464 */ 465 if (ic->ic_curmode != newphymode) { /* change phy mode */ 466 error = ieee80211_setmode(ic, newphymode); 467 if (error != 0) 468 return error; 469 error = ENETRESET; 470 } 471 472 /* 473 * Committed to changes, install the rate setting. 474 */ 475 if (ic->ic_fixed_rate != i) { 476 ic->ic_fixed_rate = i; /* set fixed tx rate */ 477 error = ENETRESET; 478 } 479 480 /* 481 * Handle operating mode change. 482 */ 483 if (ic->ic_opmode != newopmode) { 484 ic->ic_opmode = newopmode; 485 switch (newopmode) { 486 case IEEE80211_M_AHDEMO: 487 case IEEE80211_M_HOSTAP: 488 case IEEE80211_M_STA: 489 ic->ic_flags &= ~IEEE80211_F_IBSSON; 490 break; 491 case IEEE80211_M_IBSS: 492 ic->ic_flags |= IEEE80211_F_IBSSON; 493#ifdef notdef 494 if (ic->ic_curmode == IEEE80211_MODE_11G) 495 ieee80211_set11gbasicrates( 496 &ic->ic_suprates[newphymode], 497 IEEE80211_MODE_11B); 498#endif 499 break; 500 } 501 error = ENETRESET; 502 } 503#ifdef notdef 504 if (error == 0) 505 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media); 506#endif 507 return error; 508} 509 510void 511ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr) 512{ 513 struct ieee80211com *ic = (void *)ifp; 514 struct ieee80211_node *ni = NULL; 515 516 imr->ifm_status = IFM_AVALID; 517 imr->ifm_active = IFM_IEEE80211; 518 if (ic->ic_state == IEEE80211_S_RUN) 519 imr->ifm_status |= IFM_ACTIVE; 520 imr->ifm_active |= IFM_AUTO; 521 switch (ic->ic_opmode) { 522 case IEEE80211_M_STA: 523 ni = ic->ic_bss; 524 /* calculate rate subtype */ 525 imr->ifm_active |= ieee80211_rate2media(ic, 526 ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode); 527 break; 528 case IEEE80211_M_IBSS: 529 imr->ifm_active |= IFM_IEEE80211_ADHOC; 530 break; 531 case IEEE80211_M_AHDEMO: 532 /* should not come here */ 533 break; 534 case IEEE80211_M_HOSTAP: 535 imr->ifm_active |= IFM_IEEE80211_HOSTAP; 536 break; 537 } 538 switch (ic->ic_curmode) { 539 case IEEE80211_MODE_11A: 540 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11A); 541 break; 542 case IEEE80211_MODE_11B: 543 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11B); 544 break; 545 case IEEE80211_MODE_11G: 546 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11G); 547 break; 548 case IEEE80211_MODE_TURBO: 549 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11A) 550 | IFM_IEEE80211_TURBO; 551 break; 552 } 553} 554 555void 556ieee80211_watchdog(struct ifnet *ifp) 557{ 558 struct ieee80211com *ic = (void *)ifp; 559 560 if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0) 561 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 562 if (ic->ic_inact_timer && --ic->ic_inact_timer == 0) 563 ieee80211_timeout_nodes(ic); 564 565 if (ic->ic_mgt_timer != 0 || ic->ic_inact_timer != 0) 566 ifp->if_timer = 1; 567} 568 569/* 570 * Mark the basic rates for the 11g rate table based on the 571 * operating mode. For real 11g we mark all the 11b rates 572 * and 6, 12, and 24 OFDM. For 11b compatibility we mark only 573 * 11b rates. There's also a pseudo 11a-mode used to mark only 574 * the basic OFDM rates. 575 */ 576static void 577ieee80211_set11gbasicrates(struct ieee80211_rateset *rs, enum ieee80211_phymode mode) 578{ 579 static const struct ieee80211_rateset basic[] = { 580 { 3, { 12, 24, 48 } }, /* IEEE80211_MODE_11A */ 581 { 4, { 2, 4, 11, 22 } }, /* IEEE80211_MODE_11B */ 582 { 7, { 2, 4, 11, 22, 12, 24, 48 } },/* IEEE80211_MODE_11G */ 583 { 0 }, /* IEEE80211_MODE_TURBO */ 584 }; 585 int i, j; 586 587 for (i = 0; i < rs->rs_nrates; i++) { 588 rs->rs_rates[i] &= IEEE80211_RATE_VAL; 589 for (j = 0; j < basic[mode].rs_nrates; j++) 590 if (basic[mode].rs_rates[j] == rs->rs_rates[i]) { 591 rs->rs_rates[i] |= IEEE80211_RATE_BASIC; 592 break; 593 } 594 } 595} 596 597/* 598 * Set the current phy mode and recalculate the active channel 599 * set based on the available channels for this mode. Also 600 * select a new default/current channel if the current one is 601 * inappropriate for this mode. 602 */ 603int 604ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode) 605{ 606#define N(a) (sizeof(a) / sizeof(a[0])) 607 static const u_int chanflags[] = { 608 0, /* IEEE80211_MODE_AUTO */ 609 IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */ 610 IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */ 611 IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */ 612 IEEE80211_CHAN_T, /* IEEE80211_MODE_TURBO */ 613 }; 614 struct ieee80211_channel *c; 615 u_int modeflags; 616 int i; 617 618 /* validate new mode */ 619 if ((ic->ic_modecaps & (1<<mode)) == 0) { 620 IEEE80211_DPRINTF(("%s: mode %u not supported (caps 0x%x)\n", 621 __func__, mode, ic->ic_modecaps)); 622 return EINVAL; 623 } 624 625 /* 626 * Verify at least one channel is present in the available 627 * channel list before committing to the new mode. 628 */ 629 KASSERT(mode < N(chanflags), ("Unexpected mode %u\n", mode)); 630 modeflags = chanflags[mode]; 631 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 632 c = &ic->ic_channels[i]; 633 if (mode == IEEE80211_MODE_AUTO) { 634 /* ignore turbo channels for autoselect */ 635 if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0) 636 break; 637 } else { 638 if ((c->ic_flags & modeflags) == modeflags) 639 break; 640 } 641 } 642 if (i > IEEE80211_CHAN_MAX) { 643 IEEE80211_DPRINTF(("%s: no channels found for mode %u\n", 644 __func__, mode)); 645 return EINVAL; 646 } 647 648 /* 649 * Calculate the active channel set. 650 */ 651 memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active)); 652 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 653 c = &ic->ic_channels[i]; 654 if (mode == IEEE80211_MODE_AUTO) { 655 /* take anything but pure turbo channels */ 656 if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0) 657 setbit(ic->ic_chan_active, i); 658 } else { 659 if ((c->ic_flags & modeflags) == modeflags) 660 setbit(ic->ic_chan_active, i); 661 } 662 } 663 /* 664 * If no current/default channel is setup or the current 665 * channel is wrong for the mode then pick the first 666 * available channel from the active list. This is likely 667 * not the right one. 668 */ 669 if (ic->ic_ibss_chan == NULL || 670 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) { 671 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) 672 if (isset(ic->ic_chan_active, i)) { 673 ic->ic_ibss_chan = &ic->ic_channels[i]; 674 break; 675 } 676 } 677 678 /* 679 * Set/reset state flags that influence beacon contents, etc. 680 * 681 * XXX what if we have stations already associated??? 682 * XXX probably not right for autoselect? 683 */ 684 if (mode == IEEE80211_MODE_11G) { 685 if (ic->ic_caps & IEEE80211_C_SHSLOT) 686 ic->ic_flags |= IEEE80211_F_SHSLOT; 687 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) 688 ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 689 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode], 690 IEEE80211_MODE_11G); 691 } else { 692 ic->ic_flags &= ~(IEEE80211_F_SHSLOT | IEEE80211_F_SHPREAMBLE); 693 } 694 695 ic->ic_curmode = mode; 696 return 0; 697#undef N 698} 699 700/* 701 * Return the phy mode for with the specified channel so the 702 * caller can select a rate set. This is problematic and the 703 * work here assumes how things work elsewhere in this code. 704 */ 705enum ieee80211_phymode 706ieee80211_chan2mode(struct ieee80211com *ic, struct ieee80211_channel *chan) 707{ 708 /* 709 * NB: this assumes the channel would not be supplied to us 710 * unless it was already compatible with the current mode. 711 */ 712 if (ic->ic_curmode != IEEE80211_MODE_AUTO) 713 return ic->ic_curmode; 714 /* 715 * In autoselect mode; deduce a mode based on the channel 716 * characteristics. We assume that turbo-only channels 717 * are not considered when the channel set is constructed. 718 */ 719 if (IEEE80211_IS_CHAN_5GHZ(chan)) 720 return IEEE80211_MODE_11A; 721 else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN)) 722 return IEEE80211_MODE_11G; 723 else 724 return IEEE80211_MODE_11B; 725} 726 727/* 728 * convert IEEE80211 rate value to ifmedia subtype. 729 * ieee80211 rate is in unit of 0.5Mbps. 730 */ 731int 732ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode) 733{ 734#define N(a) (sizeof(a) / sizeof(a[0])) 735 static const struct { 736 u_int m; /* rate + mode */ 737 u_int r; /* if_media rate */ 738 } rates[] = { 739 { 2 | IFM_MAKEMODE(IFM_IEEE80211_11B), IFM_IEEE80211_DS1 }, 740 { 4 | IFM_MAKEMODE(IFM_IEEE80211_11B), IFM_IEEE80211_DS2 }, 741 { 11 | IFM_MAKEMODE(IFM_IEEE80211_11B), IFM_IEEE80211_DS5 }, 742 { 22 | IFM_MAKEMODE(IFM_IEEE80211_11B), IFM_IEEE80211_DS11 }, 743 { 44 | IFM_MAKEMODE(IFM_IEEE80211_11B), IFM_IEEE80211_DS22 }, 744 { 12 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM6 }, 745 { 18 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM9 }, 746 { 24 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM12 }, 747 { 36 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM18 }, 748 { 48 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM24 }, 749 { 72 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM36 }, 750 { 96 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM48 }, 751 { 108 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM54 }, 752 { 2 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_DS1 }, 753 { 4 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_DS2 }, 754 { 11 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_DS5 }, 755 { 22 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_DS11 }, 756 { 12 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM6 }, 757 { 18 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM9 }, 758 { 24 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM12 }, 759 { 36 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM18 }, 760 { 48 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM24 }, 761 { 72 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM36 }, 762 { 96 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM48 }, 763 { 108 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM54 }, 764 /* NB: OFDM72 doesn't realy exist so we don't handle it */ 765 }; 766 u_int mask, i; 767 768 mask = rate & IEEE80211_RATE_VAL; 769 switch (mode) { 770 case IEEE80211_MODE_11A: 771 case IEEE80211_MODE_TURBO: 772 mask |= IFM_MAKEMODE(IFM_IEEE80211_11A); 773 break; 774 case IEEE80211_MODE_11B: 775 mask |= IFM_MAKEMODE(IFM_IEEE80211_11B); 776 break; 777 case IEEE80211_MODE_AUTO: 778 /* NB: ic may be NULL for some drivers */ 779 if (ic && ic->ic_phytype == IEEE80211_T_FH) { 780 /* must handle these specially */ 781 switch (mask) { 782 case 2: return IFM_IEEE80211_FH1; 783 case 4: return IFM_IEEE80211_FH2; 784 } 785 return IFM_AUTO; 786 } 787 /* NB: hack, 11g matches both 11b+11a rates */ 788 /* fall thru... */ 789 case IEEE80211_MODE_11G: 790 mask |= IFM_MAKEMODE(IFM_IEEE80211_11G); 791 break; 792 } 793 for (i = 0; i < N(rates); i++) 794 if (rates[i].m == mask) 795 return rates[i].r; 796 return IFM_AUTO; 797#undef N 798} 799 800int 801ieee80211_media2rate(int mword) 802{ 803#define N(a) (sizeof(a) / sizeof(a[0])) 804 static const int ieeerates[] = { 805 -1, /* IFM_AUTO */ 806 0, /* IFM_MANUAL */ 807 0, /* IFM_NONE */ 808 2, /* IFM_IEEE80211_FH1 */ 809 4, /* IFM_IEEE80211_FH2 */ 810 2, /* IFM_IEEE80211_DS1 */ 811 4, /* IFM_IEEE80211_DS2 */ 812 11, /* IFM_IEEE80211_DS5 */ 813 22, /* IFM_IEEE80211_DS11 */ 814 44, /* IFM_IEEE80211_DS22 */ 815 12, /* IFM_IEEE80211_OFDM6 */ 816 18, /* IFM_IEEE80211_OFDM9 */ 817 24, /* IFM_IEEE80211_OFDM12 */ 818 36, /* IFM_IEEE80211_OFDM18 */ 819 48, /* IFM_IEEE80211_OFDM24 */ 820 72, /* IFM_IEEE80211_OFDM36 */ 821 96, /* IFM_IEEE80211_OFDM48 */ 822 108, /* IFM_IEEE80211_OFDM54 */ 823 144, /* IFM_IEEE80211_OFDM72 */ 824 }; 825 return IFM_SUBTYPE(mword) < N(ieeerates) ? 826 ieeerates[IFM_SUBTYPE(mword)] : 0; 827#undef N 828} 829 830/* 831 * Module glue. 832 * 833 * NB: the module name is "wlan" for compatibility with NetBSD. 834 */ 835 836static int 837ieee80211_modevent(module_t mod, int type, void *unused) 838{ 839 switch (type) { 840 case MOD_LOAD: 841 if (bootverbose) 842 printf("wlan: <802.11 Link Layer>\n"); 843 return 0; 844 case MOD_UNLOAD: 845 return 0; 846 } 847 return EINVAL; 848} 849 850static moduledata_t ieee80211_mod = { 851 "wlan", 852 ieee80211_modevent, 853 0 854}; 855DECLARE_MODULE(wlan, ieee80211_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 856MODULE_VERSION(wlan, 1); 857MODULE_DEPEND(wlan, rc4, 1, 1, 1); 858