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