ieee80211.c revision 117817
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 117817 2003-07-21 02:49:42Z 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 (ic->ic_caps & IEEE80211_C_MONITOR) 279 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR); 280 if (mode == IEEE80211_MODE_AUTO) 281 continue; 282 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]); 283 rs = &ic->ic_sup_rates[mode]; 284 for (i = 0; i < rs->rs_nrates; i++) { 285 rate = rs->rs_rates[i]; 286 mword = ieee80211_rate2media(ic, rate, mode); 287 if (mword == 0) 288 continue; 289 printf("%s%d%sMbps", (i != 0 ? " " : ""), 290 (rate & IEEE80211_RATE_VAL) / 2, 291 ((rate & 0x1) != 0 ? ".5" : "")); 292 ADD(ic, mword, mopt); 293 if (ic->ic_caps & IEEE80211_C_IBSS) 294 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC); 295 if (ic->ic_caps & IEEE80211_C_HOSTAP) 296 ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP); 297 if (ic->ic_caps & IEEE80211_C_AHDEMO) 298 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0); 299 if (ic->ic_caps & IEEE80211_C_MONITOR) 300 ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR); 301 /* 302 * Add rate to the collection of all rates. 303 */ 304 r = rate & IEEE80211_RATE_VAL; 305 for (j = 0; j < allrates.rs_nrates; j++) 306 if (allrates.rs_rates[j] == r) 307 break; 308 if (j == allrates.rs_nrates) { 309 /* unique, add to the set */ 310 allrates.rs_rates[j] = r; 311 allrates.rs_nrates++; 312 } 313 rate = (rate & IEEE80211_RATE_VAL) / 2; 314 if (rate > maxrate) 315 maxrate = rate; 316 } 317 printf("\n"); 318 } 319 for (i = 0; i < allrates.rs_nrates; i++) { 320 mword = ieee80211_rate2media(ic, allrates.rs_rates[i], 321 IEEE80211_MODE_AUTO); 322 if (mword == 0) 323 continue; 324 mword = IFM_SUBTYPE(mword); /* remove media options */ 325 ADD(ic, mword, 0); 326 if (ic->ic_caps & IEEE80211_C_IBSS) 327 ADD(ic, mword, IFM_IEEE80211_ADHOC); 328 if (ic->ic_caps & IEEE80211_C_HOSTAP) 329 ADD(ic, mword, IFM_IEEE80211_HOSTAP); 330 if (ic->ic_caps & IEEE80211_C_AHDEMO) 331 ADD(ic, mword, IFM_IEEE80211_ADHOC | IFM_FLAG0); 332 if (ic->ic_caps & IEEE80211_C_MONITOR) 333 ADD(ic, mword, IFM_IEEE80211_MONITOR); 334 } 335 ieee80211_media_status(ifp, &imr); 336 ifmedia_set(&ic->ic_media, imr.ifm_active); 337 338 if (maxrate) 339 ifp->if_baudrate = IF_Mbps(maxrate); 340#undef ADD 341} 342 343static int 344findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate) 345{ 346#define IEEERATE(_ic,_m,_i) \ 347 ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL) 348 int i, nrates = ic->ic_sup_rates[mode].rs_nrates; 349 for (i = 0; i < nrates; i++) 350 if (IEEERATE(ic, mode, i) == rate) 351 return i; 352 return -1; 353#undef IEEERATE 354} 355 356/* 357 * Handle a media change request. 358 */ 359int 360ieee80211_media_change(struct ifnet *ifp) 361{ 362 struct ieee80211com *ic = (void *)ifp; 363 struct ifmedia_entry *ime; 364 enum ieee80211_opmode newopmode; 365 enum ieee80211_phymode newphymode; 366 int i, j, newrate, error = 0; 367 368 ime = ic->ic_media.ifm_cur; 369 /* 370 * First, identify the phy mode. 371 */ 372 switch (IFM_MODE(ime->ifm_media)) { 373 case IFM_IEEE80211_11A: 374 newphymode = IEEE80211_MODE_11A; 375 break; 376 case IFM_IEEE80211_11B: 377 newphymode = IEEE80211_MODE_11B; 378 break; 379 case IFM_IEEE80211_11G: 380 newphymode = IEEE80211_MODE_11G; 381 break; 382 case IFM_AUTO: 383 newphymode = IEEE80211_MODE_AUTO; 384 break; 385 default: 386 return EINVAL; 387 } 388 /* 389 * Turbo mode is an ``option''. Eventually it 390 * needs to be applied to 11g too. 391 */ 392 if (ime->ifm_media & IFM_IEEE80211_TURBO) { 393 if (newphymode != IEEE80211_MODE_11A) 394 return EINVAL; 395 newphymode = IEEE80211_MODE_TURBO; 396 } 397 /* 398 * Validate requested mode is available. 399 */ 400 if ((ic->ic_modecaps & (1<<newphymode)) == 0) 401 return EINVAL; 402 403 /* 404 * Next, the fixed/variable rate. 405 */ 406 i = -1; 407 if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) { 408 /* 409 * Convert media subtype to rate. 410 */ 411 newrate = ieee80211_media2rate(ime->ifm_media); 412 if (newrate == 0) 413 return EINVAL; 414 /* 415 * Check the rate table for the specified/current phy. 416 */ 417 if (newphymode == IEEE80211_MODE_AUTO) { 418 /* 419 * In autoselect mode search for the rate. 420 */ 421 for (j = IEEE80211_MODE_11A; 422 j < IEEE80211_MODE_MAX; j++) { 423 if ((ic->ic_modecaps & (1<<j)) == 0) 424 continue; 425 i = findrate(ic, j, newrate); 426 if (i != -1) { 427 /* lock mode too */ 428 newphymode = j; 429 break; 430 } 431 } 432 } else { 433 i = findrate(ic, newphymode, newrate); 434 } 435 if (i == -1) /* mode/rate mismatch */ 436 return EINVAL; 437 } 438 /* NB: defer rate setting to later */ 439 440 /* 441 * Deduce new operating mode but don't install it just yet. 442 */ 443 if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) == 444 (IFM_IEEE80211_ADHOC|IFM_FLAG0)) 445 newopmode = IEEE80211_M_AHDEMO; 446 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP) 447 newopmode = IEEE80211_M_HOSTAP; 448 else if (ime->ifm_media & IFM_IEEE80211_ADHOC) 449 newopmode = IEEE80211_M_IBSS; 450 else if (ime->ifm_media & IFM_IEEE80211_MONITOR) 451 newopmode = IEEE80211_M_MONITOR; 452 else 453 newopmode = IEEE80211_M_STA; 454 455 /* 456 * Autoselect doesn't make sense when operating as an AP. 457 * If no phy mode has been selected, pick one and lock it 458 * down so rate tables can be used in forming beacon frames 459 * and the like. 460 */ 461 if (newopmode == IEEE80211_M_HOSTAP && 462 newphymode == IEEE80211_MODE_AUTO) { 463 for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++) 464 if (ic->ic_modecaps & (1<<j)) { 465 newphymode = j; 466 break; 467 } 468 } 469 470 /* 471 * Handle phy mode change. 472 */ 473 if (ic->ic_curmode != newphymode) { /* change phy mode */ 474 error = ieee80211_setmode(ic, newphymode); 475 if (error != 0) 476 return error; 477 error = ENETRESET; 478 } 479 480 /* 481 * Committed to changes, install the rate setting. 482 */ 483 if (ic->ic_fixed_rate != i) { 484 ic->ic_fixed_rate = i; /* set fixed tx rate */ 485 error = ENETRESET; 486 } 487 488 /* 489 * Handle operating mode change. 490 */ 491 if (ic->ic_opmode != newopmode) { 492 ic->ic_opmode = newopmode; 493 switch (newopmode) { 494 case IEEE80211_M_AHDEMO: 495 case IEEE80211_M_HOSTAP: 496 case IEEE80211_M_STA: 497 case IEEE80211_M_MONITOR: 498 ic->ic_flags &= ~IEEE80211_F_IBSSON; 499 break; 500 case IEEE80211_M_IBSS: 501 ic->ic_flags |= IEEE80211_F_IBSSON; 502#ifdef notdef 503 if (ic->ic_curmode == IEEE80211_MODE_11G) 504 ieee80211_set11gbasicrates( 505 &ic->ic_suprates[newphymode], 506 IEEE80211_MODE_11B); 507#endif 508 break; 509 } 510 error = ENETRESET; 511 } 512#ifdef notdef 513 if (error == 0) 514 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media); 515#endif 516 return error; 517} 518 519void 520ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr) 521{ 522 struct ieee80211com *ic = (void *)ifp; 523 struct ieee80211_node *ni = NULL; 524 525 imr->ifm_status = IFM_AVALID; 526 imr->ifm_active = IFM_IEEE80211; 527 if (ic->ic_state == IEEE80211_S_RUN) 528 imr->ifm_status |= IFM_ACTIVE; 529 imr->ifm_active |= IFM_AUTO; 530 switch (ic->ic_opmode) { 531 case IEEE80211_M_STA: 532 ni = ic->ic_bss; 533 /* calculate rate subtype */ 534 imr->ifm_active |= ieee80211_rate2media(ic, 535 ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode); 536 break; 537 case IEEE80211_M_IBSS: 538 imr->ifm_active |= IFM_IEEE80211_ADHOC; 539 break; 540 case IEEE80211_M_AHDEMO: 541 /* should not come here */ 542 break; 543 case IEEE80211_M_HOSTAP: 544 imr->ifm_active |= IFM_IEEE80211_HOSTAP; 545 break; 546 case IEEE80211_M_MONITOR: 547 imr->ifm_active |= IFM_IEEE80211_MONITOR; 548 break; 549 } 550 switch (ic->ic_curmode) { 551 case IEEE80211_MODE_11A: 552 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11A); 553 break; 554 case IEEE80211_MODE_11B: 555 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11B); 556 break; 557 case IEEE80211_MODE_11G: 558 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11G); 559 break; 560 case IEEE80211_MODE_TURBO: 561 imr->ifm_active |= IFM_MAKEMODE(IFM_IEEE80211_11A) 562 | IFM_IEEE80211_TURBO; 563 break; 564 } 565} 566 567void 568ieee80211_watchdog(struct ifnet *ifp) 569{ 570 struct ieee80211com *ic = (void *)ifp; 571 572 if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0) 573 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 574 if (ic->ic_inact_timer && --ic->ic_inact_timer == 0) 575 ieee80211_timeout_nodes(ic); 576 577 if (ic->ic_mgt_timer != 0 || ic->ic_inact_timer != 0) 578 ifp->if_timer = 1; 579} 580 581/* 582 * Mark the basic rates for the 11g rate table based on the 583 * operating mode. For real 11g we mark all the 11b rates 584 * and 6, 12, and 24 OFDM. For 11b compatibility we mark only 585 * 11b rates. There's also a pseudo 11a-mode used to mark only 586 * the basic OFDM rates. 587 */ 588static void 589ieee80211_set11gbasicrates(struct ieee80211_rateset *rs, enum ieee80211_phymode mode) 590{ 591 static const struct ieee80211_rateset basic[] = { 592 { 3, { 12, 24, 48 } }, /* IEEE80211_MODE_11A */ 593 { 4, { 2, 4, 11, 22 } }, /* IEEE80211_MODE_11B */ 594 { 7, { 2, 4, 11, 22, 12, 24, 48 } },/* IEEE80211_MODE_11G */ 595 { 0 }, /* IEEE80211_MODE_TURBO */ 596 }; 597 int i, j; 598 599 for (i = 0; i < rs->rs_nrates; i++) { 600 rs->rs_rates[i] &= IEEE80211_RATE_VAL; 601 for (j = 0; j < basic[mode].rs_nrates; j++) 602 if (basic[mode].rs_rates[j] == rs->rs_rates[i]) { 603 rs->rs_rates[i] |= IEEE80211_RATE_BASIC; 604 break; 605 } 606 } 607} 608 609/* 610 * Set the current phy mode and recalculate the active channel 611 * set based on the available channels for this mode. Also 612 * select a new default/current channel if the current one is 613 * inappropriate for this mode. 614 */ 615int 616ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode) 617{ 618#define N(a) (sizeof(a) / sizeof(a[0])) 619 static const u_int chanflags[] = { 620 0, /* IEEE80211_MODE_AUTO */ 621 IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */ 622 IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */ 623 IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */ 624 IEEE80211_CHAN_T, /* IEEE80211_MODE_TURBO */ 625 }; 626 struct ieee80211_channel *c; 627 u_int modeflags; 628 int i; 629 630 /* validate new mode */ 631 if ((ic->ic_modecaps & (1<<mode)) == 0) { 632 IEEE80211_DPRINTF(("%s: mode %u not supported (caps 0x%x)\n", 633 __func__, mode, ic->ic_modecaps)); 634 return EINVAL; 635 } 636 637 /* 638 * Verify at least one channel is present in the available 639 * channel list before committing to the new mode. 640 */ 641 KASSERT(mode < N(chanflags), ("Unexpected mode %u\n", mode)); 642 modeflags = chanflags[mode]; 643 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 644 c = &ic->ic_channels[i]; 645 if (mode == IEEE80211_MODE_AUTO) { 646 /* ignore turbo channels for autoselect */ 647 if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0) 648 break; 649 } else { 650 if ((c->ic_flags & modeflags) == modeflags) 651 break; 652 } 653 } 654 if (i > IEEE80211_CHAN_MAX) { 655 IEEE80211_DPRINTF(("%s: no channels found for mode %u\n", 656 __func__, mode)); 657 return EINVAL; 658 } 659 660 /* 661 * Calculate the active channel set. 662 */ 663 memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active)); 664 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 665 c = &ic->ic_channels[i]; 666 if (mode == IEEE80211_MODE_AUTO) { 667 /* take anything but pure turbo channels */ 668 if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0) 669 setbit(ic->ic_chan_active, i); 670 } else { 671 if ((c->ic_flags & modeflags) == modeflags) 672 setbit(ic->ic_chan_active, i); 673 } 674 } 675 /* 676 * If no current/default channel is setup or the current 677 * channel is wrong for the mode then pick the first 678 * available channel from the active list. This is likely 679 * not the right one. 680 */ 681 if (ic->ic_ibss_chan == NULL || 682 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) { 683 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) 684 if (isset(ic->ic_chan_active, i)) { 685 ic->ic_ibss_chan = &ic->ic_channels[i]; 686 break; 687 } 688 } 689 690 /* 691 * Set/reset state flags that influence beacon contents, etc. 692 * 693 * XXX what if we have stations already associated??? 694 * XXX probably not right for autoselect? 695 */ 696 if (mode == IEEE80211_MODE_11G) { 697 if (ic->ic_caps & IEEE80211_C_SHSLOT) 698 ic->ic_flags |= IEEE80211_F_SHSLOT; 699 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) 700 ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 701 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode], 702 IEEE80211_MODE_11G); 703 } else { 704 ic->ic_flags &= ~(IEEE80211_F_SHSLOT | IEEE80211_F_SHPREAMBLE); 705 } 706 707 ic->ic_curmode = mode; 708 return 0; 709#undef N 710} 711 712/* 713 * Return the phy mode for with the specified channel so the 714 * caller can select a rate set. This is problematic and the 715 * work here assumes how things work elsewhere in this code. 716 */ 717enum ieee80211_phymode 718ieee80211_chan2mode(struct ieee80211com *ic, struct ieee80211_channel *chan) 719{ 720 /* 721 * NB: this assumes the channel would not be supplied to us 722 * unless it was already compatible with the current mode. 723 */ 724 if (ic->ic_curmode != IEEE80211_MODE_AUTO) 725 return ic->ic_curmode; 726 /* 727 * In autoselect mode; deduce a mode based on the channel 728 * characteristics. We assume that turbo-only channels 729 * are not considered when the channel set is constructed. 730 */ 731 if (IEEE80211_IS_CHAN_5GHZ(chan)) 732 return IEEE80211_MODE_11A; 733 else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN)) 734 return IEEE80211_MODE_11G; 735 else 736 return IEEE80211_MODE_11B; 737} 738 739/* 740 * convert IEEE80211 rate value to ifmedia subtype. 741 * ieee80211 rate is in unit of 0.5Mbps. 742 */ 743int 744ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode) 745{ 746#define N(a) (sizeof(a) / sizeof(a[0])) 747 static const struct { 748 u_int m; /* rate + mode */ 749 u_int r; /* if_media rate */ 750 } rates[] = { 751 { 2 | IFM_MAKEMODE(IFM_IEEE80211_11B), IFM_IEEE80211_DS1 }, 752 { 4 | IFM_MAKEMODE(IFM_IEEE80211_11B), IFM_IEEE80211_DS2 }, 753 { 11 | IFM_MAKEMODE(IFM_IEEE80211_11B), IFM_IEEE80211_DS5 }, 754 { 22 | IFM_MAKEMODE(IFM_IEEE80211_11B), IFM_IEEE80211_DS11 }, 755 { 44 | IFM_MAKEMODE(IFM_IEEE80211_11B), IFM_IEEE80211_DS22 }, 756 { 12 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM6 }, 757 { 18 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM9 }, 758 { 24 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM12 }, 759 { 36 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM18 }, 760 { 48 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM24 }, 761 { 72 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM36 }, 762 { 96 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM48 }, 763 { 108 | IFM_MAKEMODE(IFM_IEEE80211_11A), IFM_IEEE80211_OFDM54 }, 764 { 2 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_DS1 }, 765 { 4 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_DS2 }, 766 { 11 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_DS5 }, 767 { 22 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_DS11 }, 768 { 12 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM6 }, 769 { 18 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM9 }, 770 { 24 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM12 }, 771 { 36 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM18 }, 772 { 48 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM24 }, 773 { 72 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM36 }, 774 { 96 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM48 }, 775 { 108 | IFM_MAKEMODE(IFM_IEEE80211_11G), IFM_IEEE80211_OFDM54 }, 776 /* NB: OFDM72 doesn't realy exist so we don't handle it */ 777 }; 778 u_int mask, i; 779 780 mask = rate & IEEE80211_RATE_VAL; 781 switch (mode) { 782 case IEEE80211_MODE_11A: 783 case IEEE80211_MODE_TURBO: 784 mask |= IFM_MAKEMODE(IFM_IEEE80211_11A); 785 break; 786 case IEEE80211_MODE_11B: 787 mask |= IFM_MAKEMODE(IFM_IEEE80211_11B); 788 break; 789 case IEEE80211_MODE_AUTO: 790 /* NB: ic may be NULL for some drivers */ 791 if (ic && ic->ic_phytype == IEEE80211_T_FH) { 792 /* must handle these specially */ 793 switch (mask) { 794 case 2: return IFM_IEEE80211_FH1; 795 case 4: return IFM_IEEE80211_FH2; 796 } 797 return IFM_AUTO; 798 } 799 /* NB: hack, 11g matches both 11b+11a rates */ 800 /* fall thru... */ 801 case IEEE80211_MODE_11G: 802 mask |= IFM_MAKEMODE(IFM_IEEE80211_11G); 803 break; 804 } 805 for (i = 0; i < N(rates); i++) 806 if (rates[i].m == mask) 807 return rates[i].r; 808 return IFM_AUTO; 809#undef N 810} 811 812int 813ieee80211_media2rate(int mword) 814{ 815#define N(a) (sizeof(a) / sizeof(a[0])) 816 static const int ieeerates[] = { 817 -1, /* IFM_AUTO */ 818 0, /* IFM_MANUAL */ 819 0, /* IFM_NONE */ 820 2, /* IFM_IEEE80211_FH1 */ 821 4, /* IFM_IEEE80211_FH2 */ 822 2, /* IFM_IEEE80211_DS1 */ 823 4, /* IFM_IEEE80211_DS2 */ 824 11, /* IFM_IEEE80211_DS5 */ 825 22, /* IFM_IEEE80211_DS11 */ 826 44, /* IFM_IEEE80211_DS22 */ 827 12, /* IFM_IEEE80211_OFDM6 */ 828 18, /* IFM_IEEE80211_OFDM9 */ 829 24, /* IFM_IEEE80211_OFDM12 */ 830 36, /* IFM_IEEE80211_OFDM18 */ 831 48, /* IFM_IEEE80211_OFDM24 */ 832 72, /* IFM_IEEE80211_OFDM36 */ 833 96, /* IFM_IEEE80211_OFDM48 */ 834 108, /* IFM_IEEE80211_OFDM54 */ 835 144, /* IFM_IEEE80211_OFDM72 */ 836 }; 837 return IFM_SUBTYPE(mword) < N(ieeerates) ? 838 ieeerates[IFM_SUBTYPE(mword)] : 0; 839#undef N 840} 841 842/* 843 * Module glue. 844 * 845 * NB: the module name is "wlan" for compatibility with NetBSD. 846 */ 847 848static int 849ieee80211_modevent(module_t mod, int type, void *unused) 850{ 851 switch (type) { 852 case MOD_LOAD: 853 if (bootverbose) 854 printf("wlan: <802.11 Link Layer>\n"); 855 return 0; 856 case MOD_UNLOAD: 857 return 0; 858 } 859 return EINVAL; 860} 861 862static moduledata_t ieee80211_mod = { 863 "wlan", 864 ieee80211_modevent, 865 0 866}; 867DECLARE_MODULE(wlan, ieee80211_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 868MODULE_VERSION(wlan, 1); 869MODULE_DEPEND(wlan, rc4, 1, 1, 1); 870