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.5 2006/01/14 13:36:39 swildner 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, NULL); 101 bpfattach_dlt(ifp, DLT_IEEE802_11, 102 sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf); 103 ieee80211_crypto_attach(ifp); 104 105 /* 106 * Fill in 802.11 available channel set, mark 107 * all available channels as active, and pick 108 * a default channel if not already specified. 109 */ 110 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail)); 111 ic->ic_modecaps |= 1<<IEEE80211_MODE_AUTO; 112 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 113 c = &ic->ic_channels[i]; 114 if (c->ic_flags) { 115 /* 116 * Verify driver passed us valid data. 117 */ 118 if (i != ieee80211_chan2ieee(ic, c)) { 119 if_printf(ifp, "bad channel ignored; " 120 "freq %u flags %x number %u\n", 121 c->ic_freq, c->ic_flags, i); 122 c->ic_flags = 0; /* NB: remove */ 123 continue; 124 } 125 setbit(ic->ic_chan_avail, i); 126 /* 127 * Identify mode capabilities. 128 */ 129 if (IEEE80211_IS_CHAN_A(c)) 130 ic->ic_modecaps |= 1<<IEEE80211_MODE_11A; 131 if (IEEE80211_IS_CHAN_B(c)) 132 ic->ic_modecaps |= 1<<IEEE80211_MODE_11B; 133 if (IEEE80211_IS_CHAN_PUREG(c)) 134 ic->ic_modecaps |= 1<<IEEE80211_MODE_11G; 135 if (IEEE80211_IS_CHAN_FHSS(c)) 136 ic->ic_modecaps |= 1<<IEEE80211_MODE_FH; 137 if (IEEE80211_IS_CHAN_T(c)) 138 ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO; 139 } 140 } 141 /* validate ic->ic_curmode */ 142 if ((ic->ic_modecaps & (1<<ic->ic_curmode)) == 0) 143 ic->ic_curmode = IEEE80211_MODE_AUTO; 144 ic->ic_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */ 145 146 ieee80211_setmode(ic, ic->ic_curmode); 147 148 if (ic->ic_lintval == 0) 149 ic->ic_lintval = 100; /* default sleep */ 150 ic->ic_bmisstimeout = 7*ic->ic_lintval; /* default 7 beacons */ 151 152 ieee80211_node_attach(ifp); 153 ieee80211_proto_attach(ifp); 154 } 155 156 void 157 ieee80211_ifdetach(struct ifnet *ifp) 158 { 159 struct ieee80211com *ic = (void *)ifp; 160 161 ieee80211_proto_detach(ifp); 162 ieee80211_crypto_detach(ifp); 163 ieee80211_node_detach(ifp); 164 ifmedia_removeall(&ic->ic_media); 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 /* XXX 543 if (ic->ic_state == IEEE80211_S_RUN) { 544 imr->ifm_status |= IFM_ACTIVE; 545 ifp->if_link_state = LINK_STATE_UP; 546 } else 547 ifp->if_link_state = LINK_STATE_DOWN; 548 */ 549 imr->ifm_active |= IFM_AUTO; 550 switch (ic->ic_opmode) { 551 case IEEE80211_M_STA: 552 ni = ic->ic_bss; 553 /* calculate rate subtype */ 554 imr->ifm_active |= ieee80211_rate2media(ic, 555 ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode); 556 break; 557 case IEEE80211_M_IBSS: 558 imr->ifm_active |= IFM_IEEE80211_ADHOC; 559 break; 560 case IEEE80211_M_AHDEMO: 561 /* should not come here */ 562 break; 563 case IEEE80211_M_HOSTAP: 564 imr->ifm_active |= IFM_IEEE80211_HOSTAP; 565 break; 566 case IEEE80211_M_MONITOR: 567 imr->ifm_active |= IFM_IEEE80211_MONITOR; 568 break; 569 } 570 switch (ic->ic_curmode) { 571 case IEEE80211_MODE_11A: 572 imr->ifm_active |= IFM_IEEE80211_11A; 573 break; 574 case IEEE80211_MODE_11B: 575 imr->ifm_active |= IFM_IEEE80211_11B; 576 break; 577 case IEEE80211_MODE_11G: 578 imr->ifm_active |= IFM_IEEE80211_11G; 579 break; 580 case IEEE80211_MODE_FH: 581 imr->ifm_active |= IFM_IEEE80211_FH; 582 break; 583 case IEEE80211_MODE_TURBO: 584 imr->ifm_active |= IFM_IEEE80211_11A 585 | IFM_IEEE80211_TURBO; 586 break; 587 } 588 589 /* Notify that the link state has changed. */ 590 if (imr->ifm_status != old_status) 591 rt_ifmsg(ifp); 592 } 593 594 void 595 ieee80211_watchdog(struct ifnet *ifp) 596 { 597 struct ieee80211com *ic = (void *)ifp; 598 599 if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0) 600 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 601 if (ic->ic_inact_timer && --ic->ic_inact_timer == 0) 602 ieee80211_timeout_nodes(ic); 603 604 if (ic->ic_mgt_timer != 0 || ic->ic_inact_timer != 0) 605 ifp->if_timer = 1; 606 } 607 608 /* 609 * Mark the basic rates for the 11g rate table based on the 610 * operating mode. For real 11g we mark all the 11b rates 611 * and 6, 12, and 24 OFDM. For 11b compatibility we mark only 612 * 11b rates. There's also a pseudo 11a-mode used to mark only 613 * the basic OFDM rates. 614 */ 615 static void 616 ieee80211_set11gbasicrates(struct ieee80211_rateset *rs, enum ieee80211_phymode mode) 617 { 618 static const struct ieee80211_rateset basic[] = { 619 { 3, { 12, 24, 48 } }, /* IEEE80211_MODE_11A */ 620 { 4, { 2, 4, 11, 22 } }, /* IEEE80211_MODE_11B */ 621 { 7, { 2, 4, 11, 22, 12, 24, 48 } },/* IEEE80211_MODE_11G */ 622 { 0 }, /* IEEE80211_MODE_FH */ 623 { 0 }, /* IEEE80211_MODE_TURBO */ 624 }; 625 int i, j; 626 627 for (i = 0; i < rs->rs_nrates; i++) { 628 rs->rs_rates[i] &= IEEE80211_RATE_VAL; 629 for (j = 0; j < basic[mode].rs_nrates; j++) 630 if (basic[mode].rs_rates[j] == rs->rs_rates[i]) { 631 rs->rs_rates[i] |= IEEE80211_RATE_BASIC; 632 break; 633 } 634 } 635 } 636 637 /* 638 * Set the current phy mode and recalculate the active channel 639 * set based on the available channels for this mode. Also 640 * select a new default/current channel if the current one is 641 * inappropriate for this mode. 642 */ 643 int 644 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode) 645 { 646 #define N(a) (sizeof(a) / sizeof(a[0])) 647 static const u_int chanflags[] = { 648 0, /* IEEE80211_MODE_AUTO */ 649 IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */ 650 IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */ 651 IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */ 652 IEEE80211_CHAN_FHSS, /* IEEE80211_MODE_FH */ 653 IEEE80211_CHAN_T, /* IEEE80211_MODE_TURBO */ 654 }; 655 struct ieee80211_channel *c; 656 u_int modeflags; 657 int i; 658 659 /* validate new mode */ 660 if ((ic->ic_modecaps & (1<<mode)) == 0) { 661 IEEE80211_DPRINTF(("%s: mode %u not supported (caps 0x%x)\n", 662 __func__, mode, ic->ic_modecaps)); 663 return EINVAL; 664 } 665 666 /* 667 * Verify at least one channel is present in the available 668 * channel list before committing to the new mode. 669 */ 670 KASSERT(mode < N(chanflags), ("Unexpected mode %u", mode)); 671 modeflags = chanflags[mode]; 672 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 673 c = &ic->ic_channels[i]; 674 if (mode == IEEE80211_MODE_AUTO) { 675 /* ignore turbo channels for autoselect */ 676 if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0) 677 break; 678 } else { 679 if ((c->ic_flags & modeflags) == modeflags) 680 break; 681 } 682 } 683 if (i > IEEE80211_CHAN_MAX) { 684 IEEE80211_DPRINTF(("%s: no channels found for mode %u\n", 685 __func__, mode)); 686 return EINVAL; 687 } 688 689 /* 690 * Calculate the active channel set. 691 */ 692 memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active)); 693 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 694 c = &ic->ic_channels[i]; 695 if (mode == IEEE80211_MODE_AUTO) { 696 /* take anything but pure turbo channels */ 697 if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0) 698 setbit(ic->ic_chan_active, i); 699 } else { 700 if ((c->ic_flags & modeflags) == modeflags) 701 setbit(ic->ic_chan_active, i); 702 } 703 } 704 /* 705 * If no current/default channel is setup or the current 706 * channel is wrong for the mode then pick the first 707 * available channel from the active list. This is likely 708 * not the right one. 709 */ 710 if (ic->ic_ibss_chan == NULL || 711 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) { 712 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) 713 if (isset(ic->ic_chan_active, i)) { 714 ic->ic_ibss_chan = &ic->ic_channels[i]; 715 break; 716 } 717 } 718 719 /* 720 * Set/reset state flags that influence beacon contents, etc. 721 * 722 * XXX what if we have stations already associated??? 723 * XXX probably not right for autoselect? 724 */ 725 if (ic->ic_caps & IEEE80211_C_SHPREAMBLE) 726 ic->ic_flags |= IEEE80211_F_SHPREAMBLE; 727 if (mode == IEEE80211_MODE_11G) { 728 if (ic->ic_caps & IEEE80211_C_SHSLOT) 729 ic->ic_flags |= IEEE80211_F_SHSLOT; 730 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode], 731 IEEE80211_MODE_11G); 732 } else { 733 ic->ic_flags &= ~IEEE80211_F_SHSLOT; 734 } 735 736 ic->ic_curmode = mode; 737 return 0; 738 #undef N 739 } 740 741 /* 742 * Return the phy mode for with the specified channel so the 743 * caller can select a rate set. This is problematic and the 744 * work here assumes how things work elsewhere in this code. 745 */ 746 enum ieee80211_phymode 747 ieee80211_chan2mode(struct ieee80211com *ic, struct ieee80211_channel *chan) 748 { 749 /* 750 * NB: this assumes the channel would not be supplied to us 751 * unless it was already compatible with the current mode. 752 */ 753 if (ic->ic_curmode != IEEE80211_MODE_AUTO) 754 return ic->ic_curmode; 755 /* 756 * In autoselect mode; deduce a mode based on the channel 757 * characteristics. We assume that turbo-only channels 758 * are not considered when the channel set is constructed. 759 */ 760 if (IEEE80211_IS_CHAN_5GHZ(chan)) 761 return IEEE80211_MODE_11A; 762 else if (IEEE80211_IS_CHAN_FHSS(chan)) 763 return IEEE80211_MODE_FH; 764 else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN)) 765 return IEEE80211_MODE_11G; 766 else 767 return IEEE80211_MODE_11B; 768 } 769 770 /* 771 * convert IEEE80211 rate value to ifmedia subtype. 772 * ieee80211 rate is in unit of 0.5Mbps. 773 */ 774 int 775 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode) 776 { 777 #define N(a) (sizeof(a) / sizeof(a[0])) 778 static const struct { 779 u_int m; /* rate + mode */ 780 u_int r; /* if_media rate */ 781 } rates[] = { 782 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 }, 783 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 }, 784 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 }, 785 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 }, 786 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 }, 787 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 }, 788 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 }, 789 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 }, 790 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 }, 791 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 }, 792 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 }, 793 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 }, 794 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 }, 795 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 }, 796 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 }, 797 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 }, 798 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 }, 799 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 }, 800 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 }, 801 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 }, 802 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 }, 803 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 }, 804 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 }, 805 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 }, 806 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 }, 807 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 }, 808 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 }, 809 /* NB: OFDM72 doesn't realy exist so we don't handle it */ 810 }; 811 u_int mask, i; 812 813 mask = rate & IEEE80211_RATE_VAL; 814 switch (mode) { 815 case IEEE80211_MODE_11A: 816 case IEEE80211_MODE_TURBO: 817 mask |= IFM_IEEE80211_11A; 818 break; 819 case IEEE80211_MODE_11B: 820 mask |= IFM_IEEE80211_11B; 821 break; 822 case IEEE80211_MODE_FH: 823 mask |= IFM_IEEE80211_FH; 824 break; 825 case IEEE80211_MODE_AUTO: 826 /* NB: ic may be NULL for some drivers */ 827 if (ic && ic->ic_phytype == IEEE80211_T_FH) { 828 mask |= IFM_IEEE80211_FH; 829 break; 830 } 831 /* NB: hack, 11g matches both 11b+11a rates */ 832 /* fall thru... */ 833 case IEEE80211_MODE_11G: 834 mask |= IFM_IEEE80211_11G; 835 break; 836 } 837 for (i = 0; i < N(rates); i++) 838 if (rates[i].m == mask) 839 return rates[i].r; 840 return IFM_AUTO; 841 #undef N 842 } 843 844 int 845 ieee80211_media2rate(int mword) 846 { 847 #define N(a) (sizeof(a) / sizeof(a[0])) 848 static const int ieeerates[] = { 849 -1, /* IFM_AUTO */ 850 0, /* IFM_MANUAL */ 851 0, /* IFM_NONE */ 852 2, /* IFM_IEEE80211_FH1 */ 853 4, /* IFM_IEEE80211_FH2 */ 854 2, /* IFM_IEEE80211_DS1 */ 855 4, /* IFM_IEEE80211_DS2 */ 856 11, /* IFM_IEEE80211_DS5 */ 857 22, /* IFM_IEEE80211_DS11 */ 858 44, /* IFM_IEEE80211_DS22 */ 859 12, /* IFM_IEEE80211_OFDM6 */ 860 18, /* IFM_IEEE80211_OFDM9 */ 861 24, /* IFM_IEEE80211_OFDM12 */ 862 36, /* IFM_IEEE80211_OFDM18 */ 863 48, /* IFM_IEEE80211_OFDM24 */ 864 72, /* IFM_IEEE80211_OFDM36 */ 865 96, /* IFM_IEEE80211_OFDM48 */ 866 108, /* IFM_IEEE80211_OFDM54 */ 867 144, /* IFM_IEEE80211_OFDM72 */ 868 }; 869 return IFM_SUBTYPE(mword) < N(ieeerates) ? 870 ieeerates[IFM_SUBTYPE(mword)] : 0; 871 #undef N 872 } 873 874 /* 875 * Module glue. 876 * 877 * NB: the module name is "wlan" for compatibility with NetBSD. 878 */ 879 880 static int 881 ieee80211_modevent(module_t mod, int type, void *unused) 882 { 883 switch (type) { 884 case MOD_LOAD: 885 if (bootverbose) 886 printf("wlan: <802.11 Link Layer>\n"); 887 return 0; 888 case MOD_UNLOAD: 889 case MOD_SHUTDOWN: 890 return 0; 891 } 892 return EINVAL; 893 } 894 895 static moduledata_t ieee80211_mod = { 896 "wlan", 897 ieee80211_modevent, 898 0 899 }; 900 DECLARE_MODULE(wlan, ieee80211_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 901 MODULE_VERSION(wlan, 1); 902 MODULE_DEPEND(wlan, crypto, 1, 1, 1); 903