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