1 /* 2 * Copyright (c) 2001 Atsushi Onoe 3 * Copyright (c) 2002-2005 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.19.2.7 2006/03/11 19:25:23 sam Exp $ 33 * $DragonFly: src/sys/netproto/802_11/wlan/ieee80211.c,v 1.7 2006/06/28 21:03:51 swildner Exp $ 34 */ 35 36 /* 37 * IEEE 802.11 generic handler 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/serialize.h> 44 45 #include <sys/socket.h> 46 47 #include <net/if.h> 48 #include <net/if_arp.h> 49 #include <net/if_media.h> 50 #include <net/ethernet.h> 51 52 #include <netproto/802_11/ieee80211_var.h> 53 54 #include <net/bpf.h> 55 56 const char *ieee80211_phymode_name[] = { 57 "auto", /* IEEE80211_MODE_AUTO */ 58 "11a", /* IEEE80211_MODE_11A */ 59 "11b", /* IEEE80211_MODE_11B */ 60 "11g", /* IEEE80211_MODE_11G */ 61 "FH", /* IEEE80211_MODE_FH */ 62 "turboA", /* IEEE80211_MODE_TURBO_A */ 63 "turboG", /* IEEE80211_MODE_TURBO_G */ 64 }; 65 66 /* list of all instances */ 67 SLIST_HEAD(ieee80211_list, ieee80211com); 68 static struct ieee80211_list ieee80211_list = 69 SLIST_HEAD_INITIALIZER(ieee80211_list); 70 static uint8_t ieee80211_vapmap[32]; /* enough for 256 */ 71 72 static void 73 ieee80211_add_vap(struct ieee80211com *ic) 74 { 75 #define N(a) (sizeof(a)/sizeof(a[0])) 76 int i; 77 uint8_t b; 78 79 crit_enter(); 80 81 ic->ic_vap = 0; 82 for (i = 0; i < N(ieee80211_vapmap) && ieee80211_vapmap[i] == 0xff; i++) 83 ic->ic_vap += NBBY; 84 if (i == N(ieee80211_vapmap)) 85 panic("vap table full"); 86 for (b = ieee80211_vapmap[i]; b & 1; b >>= 1) 87 ic->ic_vap++; 88 setbit(ieee80211_vapmap, ic->ic_vap); 89 SLIST_INSERT_HEAD(&ieee80211_list, ic, ic_next); 90 91 crit_exit(); 92 #undef N 93 } 94 95 static void 96 ieee80211_remove_vap(struct ieee80211com *ic) 97 { 98 crit_enter(); 99 100 SLIST_REMOVE(&ieee80211_list, ic, ieee80211com, ic_next); 101 KASSERT(ic->ic_vap < sizeof(ieee80211_vapmap)*NBBY, 102 ("invalid vap id %d", ic->ic_vap)); 103 KASSERT(isset(ieee80211_vapmap, ic->ic_vap), 104 ("vap id %d not allocated", ic->ic_vap)); 105 clrbit(ieee80211_vapmap, ic->ic_vap); 106 107 crit_exit(); 108 } 109 110 /* 111 * Default reset method for use with the ioctl support. This 112 * method is invoked after any state change in the 802.11 113 * layer that should be propagated to the hardware but not 114 * require re-initialization of the 802.11 state machine (e.g 115 * rescanning for an ap). We always return ENETRESET which 116 * should cause the driver to re-initialize the device. Drivers 117 * can override this method to implement more optimized support. 118 */ 119 static int 120 ieee80211_default_reset(struct ifnet *ifp) 121 { 122 return ENETRESET; 123 } 124 125 void 126 ieee80211_ifattach(struct ieee80211com *ic) 127 { 128 struct ifnet *ifp; 129 struct ieee80211_channel *c; 130 int i; 131 132 ifp = &ic->ic_ac.ac_if; 133 ic->ic_ifp = ifp; 134 135 ether_ifattach(ifp, ic->ic_myaddr, NULL); 136 bpfattach_dlt(ifp, DLT_IEEE802_11, 137 sizeof(struct ieee80211_frame_addr4), &ic->ic_rawbpf); 138 139 ieee80211_crypto_attach(ic); 140 141 /* 142 * Fill in 802.11 available channel set, mark 143 * all available channels as active, and pick 144 * a default channel if not already specified. 145 */ 146 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail)); 147 ic->ic_modecaps |= 1<<IEEE80211_MODE_AUTO; 148 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 149 c = &ic->ic_channels[i]; 150 if (c->ic_flags) { 151 /* 152 * Verify driver passed us valid data. 153 */ 154 if (i != ieee80211_chan2ieee(ic, c)) { 155 if_printf(ifp, "bad channel ignored; " 156 "freq %u flags %x number %u\n", 157 c->ic_freq, c->ic_flags, i); 158 c->ic_flags = 0; /* NB: remove */ 159 continue; 160 } 161 setbit(ic->ic_chan_avail, i); 162 /* 163 * Identify mode capabilities. 164 */ 165 if (IEEE80211_IS_CHAN_A(c)) 166 ic->ic_modecaps |= 1<<IEEE80211_MODE_11A; 167 if (IEEE80211_IS_CHAN_B(c)) 168 ic->ic_modecaps |= 1<<IEEE80211_MODE_11B; 169 if (IEEE80211_IS_CHAN_PUREG(c)) 170 ic->ic_modecaps |= 1<<IEEE80211_MODE_11G; 171 if (IEEE80211_IS_CHAN_FHSS(c)) 172 ic->ic_modecaps |= 1<<IEEE80211_MODE_FH; 173 if (IEEE80211_IS_CHAN_T(c)) 174 ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO_A; 175 if (IEEE80211_IS_CHAN_108G(c)) 176 ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO_G; 177 if (ic->ic_curchan == NULL) { 178 /* arbitrarily pick the first channel */ 179 ic->ic_curchan = &ic->ic_channels[i]; 180 } 181 } 182 } 183 /* validate ic->ic_curmode */ 184 if ((ic->ic_modecaps & (1<<ic->ic_curmode)) == 0) 185 ic->ic_curmode = IEEE80211_MODE_AUTO; 186 ic->ic_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */ 187 #if 0 188 /* 189 * Enable WME by default if we're capable. 190 */ 191 if (ic->ic_caps & IEEE80211_C_WME) 192 ic->ic_flags |= IEEE80211_F_WME; 193 #endif 194 if (ic->ic_caps & IEEE80211_C_BURST) 195 ic->ic_flags |= IEEE80211_F_BURST; 196 ieee80211_setmode(ic, ic->ic_curmode); 197 198 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT; 199 ic->ic_bmissthreshold = IEEE80211_HWBMISS_DEFAULT; 200 ic->ic_dtim_period = IEEE80211_DTIM_DEFAULT; 201 202 ic->ic_lintval = ic->ic_bintval; 203 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX; 204 205 ieee80211_node_attach(ic); 206 ieee80211_proto_attach(ic); 207 208 ieee80211_add_vap(ic); 209 210 ieee80211_sysctl_attach(ic); /* NB: requires ic_vap */ 211 212 /* 213 * Install a default reset method for the ioctl support. 214 * The driver is expected to fill this in before calling us. 215 */ 216 if (ic->ic_reset == NULL) 217 ic->ic_reset = ieee80211_default_reset; 218 } 219 220 void 221 ieee80211_ifdetach(struct ieee80211com *ic) 222 { 223 struct ifnet *ifp = ic->ic_ifp; 224 225 ieee80211_remove_vap(ic); 226 227 ieee80211_sysctl_detach(ic); 228 ieee80211_proto_detach(ic); 229 ieee80211_crypto_detach(ic); 230 231 /* 232 * XXX 233 * ieee80211_node_detach() -> ieee80211_node_table_cleanup() 234 * -> ieee80211_free_allnodes_locked() 235 * will assert the serializer 236 * In order to make the assertion work, hold serializer here 237 * 238 * SHOULD BE REMOVED 239 */ 240 lwkt_serialize_enter(ifp->if_serializer); 241 ieee80211_node_detach(ic); 242 lwkt_serialize_exit(ifp->if_serializer); 243 244 ifmedia_removeall(&ic->ic_media); 245 246 bpfdetach(ifp); 247 ether_ifdetach(ifp); 248 } 249 250 /* 251 * Convert MHz frequency to IEEE channel number. 252 */ 253 u_int 254 ieee80211_mhz2ieee(u_int freq, u_int flags) 255 { 256 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 257 if (freq == 2484) 258 return 14; 259 if (freq < 2484) 260 return (freq - 2407) / 5; 261 else 262 return 15 + ((freq - 2512) / 20); 263 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */ 264 return (freq - 5000) / 5; 265 } else { /* either, guess */ 266 if (freq == 2484) 267 return 14; 268 if (freq < 2484) 269 return (freq - 2407) / 5; 270 if (freq < 5000) 271 return 15 + ((freq - 2512) / 20); 272 return (freq - 5000) / 5; 273 } 274 } 275 276 /* 277 * Convert channel to IEEE channel number. 278 */ 279 u_int 280 ieee80211_chan2ieee(struct ieee80211com *ic, struct ieee80211_channel *c) 281 { 282 if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX]) 283 return c - ic->ic_channels; 284 else if (c == IEEE80211_CHAN_ANYC) 285 return IEEE80211_CHAN_ANY; 286 else if (c != NULL) { 287 if_printf(ic->ic_ifp, "invalid channel freq %u flags %x\n", 288 c->ic_freq, c->ic_flags); 289 return 0; /* XXX */ 290 } else { 291 if_printf(ic->ic_ifp, "invalid channel (NULL)\n"); 292 return 0; /* XXX */ 293 } 294 } 295 296 /* 297 * Convert IEEE channel number to MHz frequency. 298 */ 299 u_int 300 ieee80211_ieee2mhz(u_int chan, u_int flags) 301 { 302 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 303 if (chan == 14) 304 return 2484; 305 if (chan < 14) 306 return 2407 + chan*5; 307 else 308 return 2512 + ((chan-15)*20); 309 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */ 310 return 5000 + (chan*5); 311 } else { /* either, guess */ 312 if (chan == 14) 313 return 2484; 314 if (chan < 14) /* 0-13 */ 315 return 2407 + chan*5; 316 if (chan < 27) /* 15-26 */ 317 return 2512 + ((chan-15)*20); 318 return 5000 + (chan*5); 319 } 320 } 321 322 /* 323 * Setup the media data structures according to the channel and 324 * rate tables. This must be called by the driver after 325 * ieee80211_ifattach and before most anything else. 326 */ 327 void 328 ieee80211_media_init(struct ieee80211com *ic, 329 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat) 330 { 331 #define ADD(_ic, _s, _o) \ 332 ifmedia_add(&(_ic)->ic_media, \ 333 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL) 334 struct ifnet *ifp = ic->ic_ifp; 335 struct ifmediareq imr; 336 int i, j, mode, rate, maxrate, mword, mopt, r; 337 struct ieee80211_rateset *rs; 338 struct ieee80211_rateset allrates; 339 340 /* 341 * XXX 342 * ieee80211_node_lateattach() -> ieee80211_rest_bss() 343 * -> ieee80211_alloc_node() -> ieee80211_setup_node() 344 * will assert the serializer 345 * In order to make the assertion work, hold serializer here 346 * 347 * SHOULD BE REMOVED 348 */ 349 lwkt_serialize_enter(ifp->if_serializer); 350 351 /* 352 * Do late attach work that must wait for any subclass 353 * (i.e. driver) work such as overriding methods. 354 */ 355 ieee80211_node_lateattach(ic); 356 357 lwkt_serialize_exit(ifp->if_serializer); 358 359 /* 360 * Fill in media characteristics. 361 */ 362 ifmedia_init(&ic->ic_media, 0, media_change, media_stat); 363 maxrate = 0; 364 memset(&allrates, 0, sizeof(allrates)); 365 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) { 366 static const u_int mopts[] = { 367 IFM_AUTO, 368 IFM_IEEE80211_11A, 369 IFM_IEEE80211_11B, 370 IFM_IEEE80211_11G, 371 IFM_IEEE80211_FH, 372 IFM_IEEE80211_11A | IFM_IEEE80211_TURBO, 373 IFM_IEEE80211_11G | IFM_IEEE80211_TURBO, 374 }; 375 if ((ic->ic_modecaps & (1<<mode)) == 0) 376 continue; 377 mopt = mopts[mode]; 378 ADD(ic, IFM_AUTO, mopt); /* e.g. 11a auto */ 379 if (ic->ic_caps & IEEE80211_C_IBSS) 380 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC); 381 if (ic->ic_caps & IEEE80211_C_HOSTAP) 382 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP); 383 if (ic->ic_caps & IEEE80211_C_AHDEMO) 384 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0); 385 if (ic->ic_caps & IEEE80211_C_MONITOR) 386 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR); 387 if (mode == IEEE80211_MODE_AUTO) 388 continue; 389 rs = &ic->ic_sup_rates[mode]; 390 for (i = 0; i < rs->rs_nrates; i++) { 391 rate = rs->rs_rates[i]; 392 mword = ieee80211_rate2media(ic, rate, mode); 393 if (mword == 0) 394 continue; 395 ADD(ic, mword, mopt); 396 if (ic->ic_caps & IEEE80211_C_IBSS) 397 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC); 398 if (ic->ic_caps & IEEE80211_C_HOSTAP) 399 ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP); 400 if (ic->ic_caps & IEEE80211_C_AHDEMO) 401 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0); 402 if (ic->ic_caps & IEEE80211_C_MONITOR) 403 ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR); 404 /* 405 * Add rate to the collection of all rates. 406 */ 407 r = rate & IEEE80211_RATE_VAL; 408 for (j = 0; j < allrates.rs_nrates; j++) 409 if (allrates.rs_rates[j] == r) 410 break; 411 if (j == allrates.rs_nrates) { 412 /* unique, add to the set */ 413 allrates.rs_rates[j] = r; 414 allrates.rs_nrates++; 415 } 416 rate = (rate & IEEE80211_RATE_VAL) / 2; 417 if (rate > maxrate) 418 maxrate = rate; 419 } 420 } 421 for (i = 0; i < allrates.rs_nrates; i++) { 422 mword = ieee80211_rate2media(ic, allrates.rs_rates[i], 423 IEEE80211_MODE_AUTO); 424 if (mword == 0) 425 continue; 426 mword = IFM_SUBTYPE(mword); /* remove media options */ 427 ADD(ic, mword, 0); 428 if (ic->ic_caps & IEEE80211_C_IBSS) 429 ADD(ic, mword, IFM_IEEE80211_ADHOC); 430 if (ic->ic_caps & IEEE80211_C_HOSTAP) 431 ADD(ic, mword, IFM_IEEE80211_HOSTAP); 432 if (ic->ic_caps & IEEE80211_C_AHDEMO) 433 ADD(ic, mword, IFM_IEEE80211_ADHOC | IFM_FLAG0); 434 if (ic->ic_caps & IEEE80211_C_MONITOR) 435 ADD(ic, mword, IFM_IEEE80211_MONITOR); 436 } 437 ieee80211_media_status(ifp, &imr); 438 ifmedia_set(&ic->ic_media, imr.ifm_active); 439 440 if (maxrate) 441 ifp->if_baudrate = IF_Mbps(maxrate); 442 #undef ADD 443 } 444 445 void 446 ieee80211_announce(struct ieee80211com *ic) 447 { 448 struct ifnet *ifp = ic->ic_ifp; 449 int i, mode, rate, mword; 450 struct ieee80211_rateset *rs; 451 452 for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) { 453 if ((ic->ic_modecaps & (1<<mode)) == 0) 454 continue; 455 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]); 456 rs = &ic->ic_sup_rates[mode]; 457 for (i = 0; i < rs->rs_nrates; i++) { 458 rate = rs->rs_rates[i]; 459 mword = ieee80211_rate2media(ic, rate, mode); 460 if (mword == 0) 461 continue; 462 printf("%s%d%sMbps", (i != 0 ? " " : ""), 463 (rate & IEEE80211_RATE_VAL) / 2, 464 ((rate & 0x1) != 0 ? ".5" : "")); 465 } 466 printf("\n"); 467 } 468 } 469 470 static int 471 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate) 472 { 473 #define IEEERATE(_ic,_m,_i) \ 474 ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL) 475 int i, nrates = ic->ic_sup_rates[mode].rs_nrates; 476 for (i = 0; i < nrates; i++) 477 if (IEEERATE(ic, mode, i) == rate) 478 return i; 479 return -1; 480 #undef IEEERATE 481 } 482 483 /* 484 * Find an instance by it's mac address. 485 */ 486 struct ieee80211com * 487 ieee80211_find_vap(const uint8_t mac[IEEE80211_ADDR_LEN]) 488 { 489 struct ieee80211com *ic; 490 491 /* XXX lock */ 492 SLIST_FOREACH(ic, &ieee80211_list, ic_next) 493 if (IEEE80211_ADDR_EQ(mac, ic->ic_myaddr)) 494 return ic; 495 return NULL; 496 } 497 498 static struct ieee80211com * 499 ieee80211_find_instance(struct ifnet *ifp) 500 { 501 struct ieee80211com *ic; 502 503 /* XXX lock */ 504 /* XXX not right for multiple instances but works for now */ 505 SLIST_FOREACH(ic, &ieee80211_list, ic_next) 506 if (ic->ic_ifp == ifp) 507 return ic; 508 return NULL; 509 } 510 511 /* 512 * Handle a media change request. 513 */ 514 int 515 ieee80211_media_change(struct ifnet *ifp) 516 { 517 struct ieee80211com *ic; 518 struct ifmedia_entry *ime; 519 enum ieee80211_opmode newopmode; 520 enum ieee80211_phymode newphymode; 521 int i, j, newrate, error = 0; 522 523 ic = ieee80211_find_instance(ifp); 524 if (!ic) { 525 if_printf(ifp, "%s: no 802.11 instance!\n", __func__); 526 return EINVAL; 527 } 528 ime = ic->ic_media.ifm_cur; 529 /* 530 * First, identify the phy mode. 531 */ 532 switch (IFM_MODE(ime->ifm_media)) { 533 case IFM_IEEE80211_11A: 534 newphymode = IEEE80211_MODE_11A; 535 break; 536 case IFM_IEEE80211_11B: 537 newphymode = IEEE80211_MODE_11B; 538 break; 539 case IFM_IEEE80211_11G: 540 newphymode = IEEE80211_MODE_11G; 541 break; 542 case IFM_IEEE80211_FH: 543 newphymode = IEEE80211_MODE_FH; 544 break; 545 case IFM_AUTO: 546 newphymode = IEEE80211_MODE_AUTO; 547 break; 548 default: 549 return EINVAL; 550 } 551 /* 552 * Turbo mode is an ``option''. 553 * XXX does not apply to AUTO 554 */ 555 if (ime->ifm_media & IFM_IEEE80211_TURBO) { 556 if (newphymode == IEEE80211_MODE_11A) 557 newphymode = IEEE80211_MODE_TURBO_A; 558 else if (newphymode == IEEE80211_MODE_11G) 559 newphymode = IEEE80211_MODE_TURBO_G; 560 else 561 return EINVAL; 562 } 563 /* 564 * Validate requested mode is available. 565 */ 566 if ((ic->ic_modecaps & (1<<newphymode)) == 0) 567 return EINVAL; 568 569 /* 570 * Next, the fixed/variable rate. 571 */ 572 i = -1; 573 if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) { 574 /* 575 * Convert media subtype to rate. 576 */ 577 newrate = ieee80211_media2rate(ime->ifm_media); 578 if (newrate == 0) 579 return EINVAL; 580 /* 581 * Check the rate table for the specified/current phy. 582 */ 583 if (newphymode == IEEE80211_MODE_AUTO) { 584 /* 585 * In autoselect mode search for the rate. 586 */ 587 for (j = IEEE80211_MODE_11A; 588 j < IEEE80211_MODE_MAX; j++) { 589 if ((ic->ic_modecaps & (1<<j)) == 0) 590 continue; 591 i = findrate(ic, j, newrate); 592 if (i != -1) { 593 /* lock mode too */ 594 newphymode = j; 595 break; 596 } 597 } 598 } else { 599 i = findrate(ic, newphymode, newrate); 600 } 601 if (i == -1) /* mode/rate mismatch */ 602 return EINVAL; 603 } 604 /* NB: defer rate setting to later */ 605 606 /* 607 * Deduce new operating mode but don't install it just yet. 608 */ 609 if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) == 610 (IFM_IEEE80211_ADHOC|IFM_FLAG0)) 611 newopmode = IEEE80211_M_AHDEMO; 612 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP) 613 newopmode = IEEE80211_M_HOSTAP; 614 else if (ime->ifm_media & IFM_IEEE80211_ADHOC) 615 newopmode = IEEE80211_M_IBSS; 616 else if (ime->ifm_media & IFM_IEEE80211_MONITOR) 617 newopmode = IEEE80211_M_MONITOR; 618 else 619 newopmode = IEEE80211_M_STA; 620 621 /* 622 * Autoselect doesn't make sense when operating as an AP. 623 * If no phy mode has been selected, pick one and lock it 624 * down so rate tables can be used in forming beacon frames 625 * and the like. 626 */ 627 if (newopmode == IEEE80211_M_HOSTAP && 628 newphymode == IEEE80211_MODE_AUTO) { 629 for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++) 630 if (ic->ic_modecaps & (1<<j)) { 631 newphymode = j; 632 break; 633 } 634 } 635 636 /* 637 * Handle phy mode change. 638 */ 639 if (ic->ic_curmode != newphymode) { /* change phy mode */ 640 error = ieee80211_setmode(ic, newphymode); 641 if (error != 0) 642 return error; 643 error = ENETRESET; 644 } 645 646 /* 647 * Committed to changes, install the rate setting. 648 */ 649 if (ic->ic_fixed_rate != i) { 650 ic->ic_fixed_rate = i; /* set fixed tx rate */ 651 error = ENETRESET; 652 } 653 654 /* 655 * Handle operating mode change. 656 */ 657 if (ic->ic_opmode != newopmode) { 658 ic->ic_opmode = newopmode; 659 switch (newopmode) { 660 case IEEE80211_M_AHDEMO: 661 case IEEE80211_M_HOSTAP: 662 case IEEE80211_M_STA: 663 case IEEE80211_M_MONITOR: 664 ic->ic_flags &= ~IEEE80211_F_IBSSON; 665 break; 666 case IEEE80211_M_IBSS: 667 ic->ic_flags |= IEEE80211_F_IBSSON; 668 break; 669 } 670 /* 671 * Yech, slot time may change depending on the 672 * operating mode so reset it to be sure everything 673 * is setup appropriately. 674 */ 675 ieee80211_reset_erp(ic); 676 ieee80211_wme_initparams(ic); /* after opmode change */ 677 error = ENETRESET; 678 } 679 #ifdef notdef 680 if (error == 0) 681 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media); 682 #endif 683 return error; 684 } 685 686 void 687 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr) 688 { 689 struct ieee80211com *ic; 690 struct ieee80211_rateset *rs; 691 692 ic = ieee80211_find_instance(ifp); 693 if (!ic) { 694 if_printf(ifp, "%s: no 802.11 instance!\n", __func__); 695 return; 696 } 697 imr->ifm_status = IFM_AVALID; 698 imr->ifm_active = IFM_IEEE80211; 699 if (ic->ic_state == IEEE80211_S_RUN) 700 imr->ifm_status |= IFM_ACTIVE; 701 /* 702 * Calculate a current rate if possible. 703 */ 704 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) { 705 /* 706 * A fixed rate is set, report that. 707 */ 708 rs = &ic->ic_sup_rates[ic->ic_curmode]; 709 imr->ifm_active |= ieee80211_rate2media(ic, 710 rs->rs_rates[ic->ic_fixed_rate], ic->ic_curmode); 711 } else if (ic->ic_opmode == IEEE80211_M_STA) { 712 /* 713 * In station mode report the current transmit rate. 714 */ 715 rs = &ic->ic_bss->ni_rates; 716 imr->ifm_active |= ieee80211_rate2media(ic, 717 rs->rs_rates[ic->ic_bss->ni_txrate], ic->ic_curmode); 718 } else 719 imr->ifm_active |= IFM_AUTO; 720 switch (ic->ic_opmode) { 721 case IEEE80211_M_STA: 722 break; 723 case IEEE80211_M_IBSS: 724 imr->ifm_active |= IFM_IEEE80211_ADHOC; 725 break; 726 case IEEE80211_M_AHDEMO: 727 /* should not come here */ 728 break; 729 case IEEE80211_M_HOSTAP: 730 imr->ifm_active |= IFM_IEEE80211_HOSTAP; 731 break; 732 case IEEE80211_M_MONITOR: 733 imr->ifm_active |= IFM_IEEE80211_MONITOR; 734 break; 735 } 736 switch (ic->ic_curmode) { 737 case IEEE80211_MODE_11A: 738 imr->ifm_active |= IFM_IEEE80211_11A; 739 break; 740 case IEEE80211_MODE_11B: 741 imr->ifm_active |= IFM_IEEE80211_11B; 742 break; 743 case IEEE80211_MODE_11G: 744 imr->ifm_active |= IFM_IEEE80211_11G; 745 break; 746 case IEEE80211_MODE_FH: 747 imr->ifm_active |= IFM_IEEE80211_FH; 748 break; 749 case IEEE80211_MODE_TURBO_A: 750 imr->ifm_active |= IFM_IEEE80211_11A 751 | IFM_IEEE80211_TURBO; 752 break; 753 case IEEE80211_MODE_TURBO_G: 754 imr->ifm_active |= IFM_IEEE80211_11G 755 | IFM_IEEE80211_TURBO; 756 break; 757 } 758 } 759 760 void 761 ieee80211_watchdog(struct ieee80211com *ic) 762 { 763 struct ieee80211_node_table *nt; 764 int need_inact_timer = 0; 765 766 if (ic->ic_state != IEEE80211_S_INIT) { 767 if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0) 768 ieee80211_new_state(ic, IEEE80211_S_SCAN, 0); 769 nt = &ic->ic_scan; 770 if (nt->nt_inact_timer) { 771 if (--nt->nt_inact_timer == 0) 772 nt->nt_timeout(nt); 773 need_inact_timer += nt->nt_inact_timer; 774 } 775 nt = &ic->ic_sta; 776 if (nt->nt_inact_timer) { 777 if (--nt->nt_inact_timer == 0) 778 nt->nt_timeout(nt); 779 need_inact_timer += nt->nt_inact_timer; 780 } 781 } 782 if (ic->ic_mgt_timer != 0 || need_inact_timer) 783 ic->ic_ifp->if_timer = 1; 784 } 785 786 /* 787 * Set the current phy mode and recalculate the active channel 788 * set based on the available channels for this mode. Also 789 * select a new default/current channel if the current one is 790 * inappropriate for this mode. 791 */ 792 int 793 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode) 794 { 795 #define N(a) (sizeof(a) / sizeof(a[0])) 796 static const u_int chanflags[] = { 797 0, /* IEEE80211_MODE_AUTO */ 798 IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */ 799 IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */ 800 IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */ 801 IEEE80211_CHAN_FHSS, /* IEEE80211_MODE_FH */ 802 IEEE80211_CHAN_T, /* IEEE80211_MODE_TURBO_A */ 803 IEEE80211_CHAN_108G, /* IEEE80211_MODE_TURBO_G */ 804 }; 805 struct ieee80211_channel *c; 806 u_int modeflags; 807 int i; 808 809 /* validate new mode */ 810 if ((ic->ic_modecaps & (1<<mode)) == 0) { 811 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 812 "%s: mode %u not supported (caps 0x%x)\n", 813 __func__, mode, ic->ic_modecaps); 814 return EINVAL; 815 } 816 817 /* 818 * Verify at least one channel is present in the available 819 * channel list before committing to the new mode. 820 */ 821 KASSERT(mode < N(chanflags), ("Unexpected mode %u", mode)); 822 modeflags = chanflags[mode]; 823 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 824 c = &ic->ic_channels[i]; 825 if (c->ic_flags == 0) 826 continue; 827 if (mode == IEEE80211_MODE_AUTO) { 828 /* ignore static turbo channels for autoselect */ 829 if (!IEEE80211_IS_CHAN_T(c)) 830 break; 831 } else { 832 if ((c->ic_flags & modeflags) == modeflags) 833 break; 834 } 835 } 836 if (i > IEEE80211_CHAN_MAX) { 837 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 838 "%s: no channels found for mode %u\n", __func__, mode); 839 return EINVAL; 840 } 841 842 /* 843 * Calculate the active channel set. 844 */ 845 memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active)); 846 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 847 c = &ic->ic_channels[i]; 848 if (c->ic_flags == 0) 849 continue; 850 if (mode == IEEE80211_MODE_AUTO) { 851 /* take anything but static turbo channels */ 852 if (!IEEE80211_IS_CHAN_T(c)) 853 setbit(ic->ic_chan_active, i); 854 } else { 855 if ((c->ic_flags & modeflags) == modeflags) 856 setbit(ic->ic_chan_active, i); 857 } 858 } 859 /* 860 * If no current/default channel is setup or the current 861 * channel is wrong for the mode then pick the first 862 * available channel from the active list. This is likely 863 * not the right one. 864 */ 865 if (ic->ic_ibss_chan == NULL || 866 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) { 867 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) 868 if (isset(ic->ic_chan_active, i)) { 869 ic->ic_ibss_chan = &ic->ic_channels[i]; 870 break; 871 } 872 KASSERT(ic->ic_ibss_chan != NULL && 873 isset(ic->ic_chan_active, 874 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)), 875 ("Bad IBSS channel %u", 876 ieee80211_chan2ieee(ic, ic->ic_ibss_chan))); 877 } 878 /* 879 * If the desired channel is set but no longer valid then reset it. 880 */ 881 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC && 882 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_des_chan))) 883 ic->ic_des_chan = IEEE80211_CHAN_ANYC; 884 885 /* 886 * Do mode-specific rate setup. 887 */ 888 if (mode == IEEE80211_MODE_11G) { 889 /* 890 * Use a mixed 11b/11g rate set. 891 */ 892 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode], 893 IEEE80211_MODE_11G); 894 } else if (mode == IEEE80211_MODE_11B) { 895 /* 896 * Force pure 11b rate set. 897 */ 898 ieee80211_set11gbasicrates(&ic->ic_sup_rates[mode], 899 IEEE80211_MODE_11B); 900 } 901 /* 902 * Setup an initial rate set according to the 903 * current/default channel selected above. This 904 * will be changed when scanning but must exist 905 * now so driver have a consistent state of ic_ibss_chan. 906 */ 907 if (ic->ic_bss) /* NB: can be called before lateattach */ 908 ic->ic_bss->ni_rates = ic->ic_sup_rates[mode]; 909 910 ic->ic_curmode = mode; 911 ieee80211_reset_erp(ic); /* reset ERP state */ 912 ieee80211_wme_initparams(ic); /* reset WME stat */ 913 914 return 0; 915 #undef N 916 } 917 918 /* 919 * Return the phy mode for with the specified channel so the 920 * caller can select a rate set. This is problematic for channels 921 * where multiple operating modes are possible (e.g. 11g+11b). 922 * In those cases we defer to the current operating mode when set. 923 */ 924 enum ieee80211_phymode 925 ieee80211_chan2mode(struct ieee80211com *ic, struct ieee80211_channel *chan) 926 { 927 if (IEEE80211_IS_CHAN_T(chan)) { 928 return IEEE80211_MODE_TURBO_A; 929 } else if (IEEE80211_IS_CHAN_5GHZ(chan)) { 930 return IEEE80211_MODE_11A; 931 } else if (IEEE80211_IS_CHAN_FHSS(chan)) { 932 return IEEE80211_MODE_FH; 933 } else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN)) { 934 /* 935 * This assumes all 11g channels are also usable 936 * for 11b, which is currently true. 937 */ 938 if (ic->ic_curmode == IEEE80211_MODE_TURBO_G) 939 return IEEE80211_MODE_TURBO_G; 940 if (ic->ic_curmode == IEEE80211_MODE_11B) 941 return IEEE80211_MODE_11B; 942 return IEEE80211_MODE_11G; 943 } else { 944 return IEEE80211_MODE_11B; 945 } 946 } 947 948 /* 949 * convert IEEE80211 rate value to ifmedia subtype. 950 * ieee80211 rate is in unit of 0.5Mbps. 951 */ 952 int 953 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode) 954 { 955 #define N(a) (sizeof(a) / sizeof(a[0])) 956 static const struct { 957 u_int m; /* rate + mode */ 958 u_int r; /* if_media rate */ 959 } rates[] = { 960 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 }, 961 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 }, 962 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 }, 963 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 }, 964 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 }, 965 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 }, 966 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 }, 967 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 }, 968 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 }, 969 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 }, 970 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 }, 971 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 }, 972 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 }, 973 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 }, 974 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 }, 975 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 }, 976 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 }, 977 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 }, 978 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 }, 979 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 }, 980 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 }, 981 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 }, 982 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 }, 983 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 }, 984 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 }, 985 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 }, 986 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 }, 987 /* NB: OFDM72 doesn't realy exist so we don't handle it */ 988 }; 989 u_int mask, i; 990 991 mask = rate & IEEE80211_RATE_VAL; 992 switch (mode) { 993 case IEEE80211_MODE_11A: 994 case IEEE80211_MODE_TURBO_A: 995 mask |= IFM_IEEE80211_11A; 996 break; 997 case IEEE80211_MODE_11B: 998 mask |= IFM_IEEE80211_11B; 999 break; 1000 case IEEE80211_MODE_FH: 1001 mask |= IFM_IEEE80211_FH; 1002 break; 1003 case IEEE80211_MODE_AUTO: 1004 /* NB: ic may be NULL for some drivers */ 1005 if (ic && ic->ic_phytype == IEEE80211_T_FH) { 1006 mask |= IFM_IEEE80211_FH; 1007 break; 1008 } 1009 /* NB: hack, 11g matches both 11b+11a rates */ 1010 /* fall thru... */ 1011 case IEEE80211_MODE_11G: 1012 case IEEE80211_MODE_TURBO_G: 1013 mask |= IFM_IEEE80211_11G; 1014 break; 1015 } 1016 for (i = 0; i < N(rates); i++) 1017 if (rates[i].m == mask) 1018 return rates[i].r; 1019 return IFM_AUTO; 1020 #undef N 1021 } 1022 1023 int 1024 ieee80211_media2rate(int mword) 1025 { 1026 #define N(a) (sizeof(a) / sizeof(a[0])) 1027 static const int ieeerates[] = { 1028 -1, /* IFM_AUTO */ 1029 0, /* IFM_MANUAL */ 1030 0, /* IFM_NONE */ 1031 2, /* IFM_IEEE80211_FH1 */ 1032 4, /* IFM_IEEE80211_FH2 */ 1033 2, /* IFM_IEEE80211_DS1 */ 1034 4, /* IFM_IEEE80211_DS2 */ 1035 11, /* IFM_IEEE80211_DS5 */ 1036 22, /* IFM_IEEE80211_DS11 */ 1037 44, /* IFM_IEEE80211_DS22 */ 1038 12, /* IFM_IEEE80211_OFDM6 */ 1039 18, /* IFM_IEEE80211_OFDM9 */ 1040 24, /* IFM_IEEE80211_OFDM12 */ 1041 36, /* IFM_IEEE80211_OFDM18 */ 1042 48, /* IFM_IEEE80211_OFDM24 */ 1043 72, /* IFM_IEEE80211_OFDM36 */ 1044 96, /* IFM_IEEE80211_OFDM48 */ 1045 108, /* IFM_IEEE80211_OFDM54 */ 1046 144, /* IFM_IEEE80211_OFDM72 */ 1047 }; 1048 return IFM_SUBTYPE(mword) < N(ieeerates) ? 1049 ieeerates[IFM_SUBTYPE(mword)] : 0; 1050 #undef N 1051 } 1052