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.12 2007/02/16 11:17:01 sephe 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 ieee80211_ratectl_attach(ic); 213 214 /* 215 * Install a default reset method for the ioctl support. 216 * The driver is expected to fill this in before calling us. 217 */ 218 if (ic->ic_reset == NULL) 219 ic->ic_reset = ieee80211_default_reset; 220 } 221 222 void 223 ieee80211_ifdetach(struct ieee80211com *ic) 224 { 225 struct ifnet *ifp = ic->ic_ifp; 226 227 /* 228 * XXX 229 * Certain rate control algorithm(e.g. onoe) may iterate node 230 * tables, which will assert serializer. 231 * In order to make the assertion work, hold serializer here. 232 * SHOULD BE REMOVED 233 */ 234 lwkt_serialize_enter(ifp->if_serializer); 235 ieee80211_ratectl_detach(ic); 236 lwkt_serialize_exit(ifp->if_serializer); 237 238 ieee80211_remove_vap(ic); 239 240 ieee80211_sysctl_detach(ic); 241 ieee80211_proto_detach(ic); 242 ieee80211_crypto_detach(ic); 243 244 /* 245 * XXX 246 * ieee80211_node_detach() -> ieee80211_node_table_cleanup() 247 * -> ieee80211_free_allnodes_locked() 248 * will assert the serializer. 249 * In order to make the assertion work, hold serializer here. 250 * SHOULD BE REMOVED 251 */ 252 lwkt_serialize_enter(ifp->if_serializer); 253 ieee80211_node_detach(ic); 254 lwkt_serialize_exit(ifp->if_serializer); 255 256 ifmedia_removeall(&ic->ic_media); 257 258 bpfdetach(ifp); 259 ether_ifdetach(ifp); 260 } 261 262 /* 263 * Convert MHz frequency to IEEE channel number. 264 */ 265 u_int 266 ieee80211_mhz2ieee(u_int freq, u_int flags) 267 { 268 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 269 if (freq == 2484) 270 return 14; 271 if (freq < 2484) 272 return (freq - 2407) / 5; 273 else 274 return 15 + ((freq - 2512) / 20); 275 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */ 276 return (freq - 5000) / 5; 277 } else { /* either, guess */ 278 if (freq == 2484) 279 return 14; 280 if (freq < 2484) 281 return (freq - 2407) / 5; 282 if (freq < 5000) 283 return 15 + ((freq - 2512) / 20); 284 return (freq - 5000) / 5; 285 } 286 } 287 288 /* 289 * Convert channel to IEEE channel number. 290 */ 291 u_int 292 ieee80211_chan2ieee(struct ieee80211com *ic, struct ieee80211_channel *c) 293 { 294 if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX]) 295 return c - ic->ic_channels; 296 else if (c == IEEE80211_CHAN_ANYC) 297 return IEEE80211_CHAN_ANY; 298 else if (c != NULL) { 299 if_printf(ic->ic_ifp, "invalid channel freq %u flags %x\n", 300 c->ic_freq, c->ic_flags); 301 return 0; /* XXX */ 302 } else { 303 if_printf(ic->ic_ifp, "invalid channel (NULL)\n"); 304 return 0; /* XXX */ 305 } 306 } 307 308 /* 309 * Convert IEEE channel number to MHz frequency. 310 */ 311 u_int 312 ieee80211_ieee2mhz(u_int chan, u_int flags) 313 { 314 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 315 if (chan == 14) 316 return 2484; 317 if (chan < 14) 318 return 2407 + chan*5; 319 else 320 return 2512 + ((chan-15)*20); 321 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */ 322 return 5000 + (chan*5); 323 } else { /* either, guess */ 324 if (chan == 14) 325 return 2484; 326 if (chan < 14) /* 0-13 */ 327 return 2407 + chan*5; 328 if (chan < 27) /* 15-26 */ 329 return 2512 + ((chan-15)*20); 330 return 5000 + (chan*5); 331 } 332 } 333 334 /* 335 * Setup the media data structures according to the channel and 336 * rate tables. This must be called by the driver after 337 * ieee80211_ifattach and before most anything else. 338 */ 339 void 340 ieee80211_media_init(struct ieee80211com *ic, 341 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat) 342 { 343 #define ADD(_ic, _s, _o) \ 344 ifmedia_add(&(_ic)->ic_media, \ 345 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL) 346 struct ifnet *ifp = ic->ic_ifp; 347 struct ifmediareq imr; 348 int i, j, mode, rate, maxrate, mword, mopt, r; 349 struct ieee80211_rateset *rs; 350 struct ieee80211_rateset allrates; 351 352 /* 353 * XXX 354 * ieee80211_node_lateattach() -> ieee80211_rest_bss() 355 * -> ieee80211_alloc_node() -> ieee80211_setup_node() 356 * will assert the serializer 357 * In order to make the assertion work, hold serializer here 358 * 359 * SHOULD BE REMOVED 360 */ 361 lwkt_serialize_enter(ifp->if_serializer); 362 363 /* 364 * Do late attach work that must wait for any subclass 365 * (i.e. driver) work such as overriding methods. 366 */ 367 ieee80211_node_lateattach(ic); 368 369 lwkt_serialize_exit(ifp->if_serializer); 370 371 /* 372 * Fill in media characteristics. 373 */ 374 ifmedia_init(&ic->ic_media, 0, media_change, media_stat); 375 maxrate = 0; 376 memset(&allrates, 0, sizeof(allrates)); 377 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) { 378 static const u_int mopts[] = { 379 IFM_AUTO, 380 IFM_IEEE80211_11A, 381 IFM_IEEE80211_11B, 382 IFM_IEEE80211_11G, 383 IFM_IEEE80211_FH, 384 IFM_IEEE80211_11A | IFM_IEEE80211_TURBO, 385 IFM_IEEE80211_11G | IFM_IEEE80211_TURBO, 386 }; 387 if ((ic->ic_modecaps & (1<<mode)) == 0) 388 continue; 389 mopt = mopts[mode]; 390 ADD(ic, IFM_AUTO, mopt); /* e.g. 11a auto */ 391 if (ic->ic_caps & IEEE80211_C_IBSS) 392 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC); 393 if (ic->ic_caps & IEEE80211_C_HOSTAP) 394 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP); 395 if (ic->ic_caps & IEEE80211_C_AHDEMO) 396 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0); 397 if (ic->ic_caps & IEEE80211_C_MONITOR) 398 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR); 399 if (mode == IEEE80211_MODE_AUTO) 400 continue; 401 rs = &ic->ic_sup_rates[mode]; 402 for (i = 0; i < rs->rs_nrates; i++) { 403 rate = rs->rs_rates[i]; 404 mword = ieee80211_rate2media(ic, rate, mode); 405 if (mword == 0) 406 continue; 407 ADD(ic, mword, mopt); 408 if (ic->ic_caps & IEEE80211_C_IBSS) 409 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC); 410 if (ic->ic_caps & IEEE80211_C_HOSTAP) 411 ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP); 412 if (ic->ic_caps & IEEE80211_C_AHDEMO) 413 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0); 414 if (ic->ic_caps & IEEE80211_C_MONITOR) 415 ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR); 416 /* 417 * Add rate to the collection of all rates. 418 */ 419 r = rate & IEEE80211_RATE_VAL; 420 for (j = 0; j < allrates.rs_nrates; j++) 421 if (allrates.rs_rates[j] == r) 422 break; 423 if (j == allrates.rs_nrates) { 424 /* unique, add to the set */ 425 allrates.rs_rates[j] = r; 426 allrates.rs_nrates++; 427 } 428 rate = (rate & IEEE80211_RATE_VAL) / 2; 429 if (rate > maxrate) 430 maxrate = rate; 431 } 432 } 433 for (i = 0; i < allrates.rs_nrates; i++) { 434 mword = ieee80211_rate2media(ic, allrates.rs_rates[i], 435 IEEE80211_MODE_AUTO); 436 if (mword == 0) 437 continue; 438 mword = IFM_SUBTYPE(mword); /* remove media options */ 439 ADD(ic, mword, 0); 440 if (ic->ic_caps & IEEE80211_C_IBSS) 441 ADD(ic, mword, IFM_IEEE80211_ADHOC); 442 if (ic->ic_caps & IEEE80211_C_HOSTAP) 443 ADD(ic, mword, IFM_IEEE80211_HOSTAP); 444 if (ic->ic_caps & IEEE80211_C_AHDEMO) 445 ADD(ic, mword, IFM_IEEE80211_ADHOC | IFM_FLAG0); 446 if (ic->ic_caps & IEEE80211_C_MONITOR) 447 ADD(ic, mword, IFM_IEEE80211_MONITOR); 448 } 449 ieee80211_media_status(ifp, &imr); 450 ifmedia_set(&ic->ic_media, imr.ifm_active); 451 452 if (maxrate) 453 ifp->if_baudrate = IF_Mbps(maxrate); 454 #undef ADD 455 } 456 457 void 458 ieee80211_announce(struct ieee80211com *ic) 459 { 460 struct ifnet *ifp = ic->ic_ifp; 461 int i, mode, rate, mword; 462 struct ieee80211_rateset *rs; 463 464 for (mode = IEEE80211_MODE_11A; mode < IEEE80211_MODE_MAX; mode++) { 465 if ((ic->ic_modecaps & (1<<mode)) == 0) 466 continue; 467 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]); 468 rs = &ic->ic_sup_rates[mode]; 469 for (i = 0; i < rs->rs_nrates; i++) { 470 rate = rs->rs_rates[i]; 471 mword = ieee80211_rate2media(ic, rate, mode); 472 if (mword == 0) 473 continue; 474 kprintf("%s%d%sMbps", (i != 0 ? " " : ""), 475 (rate & IEEE80211_RATE_VAL) / 2, 476 ((rate & 0x1) != 0 ? ".5" : "")); 477 } 478 kprintf("\n"); 479 } 480 } 481 482 static int 483 findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate) 484 { 485 #define IEEERATE(_ic,_m,_i) \ 486 ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL) 487 int i, nrates = ic->ic_sup_rates[mode].rs_nrates; 488 for (i = 0; i < nrates; i++) 489 if (IEEERATE(ic, mode, i) == rate) 490 return i; 491 return -1; 492 #undef IEEERATE 493 } 494 495 /* 496 * Find an instance by it's mac address. 497 */ 498 struct ieee80211com * 499 ieee80211_find_vap(const uint8_t mac[IEEE80211_ADDR_LEN]) 500 { 501 struct ieee80211com *ic; 502 503 /* XXX lock */ 504 SLIST_FOREACH(ic, &ieee80211_list, ic_next) 505 if (IEEE80211_ADDR_EQ(mac, ic->ic_myaddr)) 506 return ic; 507 return NULL; 508 } 509 510 static struct ieee80211com * 511 ieee80211_find_instance(struct ifnet *ifp) 512 { 513 struct ieee80211com *ic; 514 515 /* XXX lock */ 516 /* XXX not right for multiple instances but works for now */ 517 SLIST_FOREACH(ic, &ieee80211_list, ic_next) 518 if (ic->ic_ifp == ifp) 519 return ic; 520 return NULL; 521 } 522 523 /* 524 * Handle a media change request. 525 */ 526 int 527 ieee80211_media_change(struct ifnet *ifp) 528 { 529 struct ieee80211com *ic; 530 struct ifmedia_entry *ime; 531 enum ieee80211_opmode newopmode; 532 enum ieee80211_phymode newphymode; 533 int i, j, newrate, error = 0; 534 535 ic = ieee80211_find_instance(ifp); 536 if (!ic) { 537 if_printf(ifp, "%s: no 802.11 instance!\n", __func__); 538 return EINVAL; 539 } 540 ime = ic->ic_media.ifm_cur; 541 /* 542 * First, identify the phy mode. 543 */ 544 switch (IFM_MODE(ime->ifm_media)) { 545 case IFM_IEEE80211_11A: 546 newphymode = IEEE80211_MODE_11A; 547 break; 548 case IFM_IEEE80211_11B: 549 newphymode = IEEE80211_MODE_11B; 550 break; 551 case IFM_IEEE80211_11G: 552 newphymode = IEEE80211_MODE_11G; 553 break; 554 case IFM_IEEE80211_FH: 555 newphymode = IEEE80211_MODE_FH; 556 break; 557 case IFM_AUTO: 558 newphymode = IEEE80211_MODE_AUTO; 559 break; 560 default: 561 return EINVAL; 562 } 563 /* 564 * Turbo mode is an ``option''. 565 * XXX does not apply to AUTO 566 */ 567 if (ime->ifm_media & IFM_IEEE80211_TURBO) { 568 if (newphymode == IEEE80211_MODE_11A) 569 newphymode = IEEE80211_MODE_TURBO_A; 570 else if (newphymode == IEEE80211_MODE_11G) 571 newphymode = IEEE80211_MODE_TURBO_G; 572 else 573 return EINVAL; 574 } 575 /* 576 * Validate requested mode is available. 577 */ 578 if ((ic->ic_modecaps & (1<<newphymode)) == 0) 579 return EINVAL; 580 581 /* 582 * Next, the fixed/variable rate. 583 */ 584 i = -1; 585 if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) { 586 /* 587 * Convert media subtype to rate. 588 */ 589 newrate = ieee80211_media2rate(ime->ifm_media); 590 if (newrate == 0) 591 return EINVAL; 592 /* 593 * Check the rate table for the specified/current phy. 594 */ 595 if (newphymode == IEEE80211_MODE_AUTO) { 596 /* 597 * In autoselect mode search for the rate. 598 */ 599 for (j = IEEE80211_MODE_11A; 600 j < IEEE80211_MODE_MAX; j++) { 601 if ((ic->ic_modecaps & (1<<j)) == 0) 602 continue; 603 i = findrate(ic, j, newrate); 604 if (i != -1) { 605 /* lock mode too */ 606 newphymode = j; 607 break; 608 } 609 } 610 } else { 611 i = findrate(ic, newphymode, newrate); 612 } 613 if (i == -1) /* mode/rate mismatch */ 614 return EINVAL; 615 } 616 /* NB: defer rate setting to later */ 617 618 /* 619 * Deduce new operating mode but don't install it just yet. 620 */ 621 if ((ime->ifm_media & (IFM_IEEE80211_ADHOC|IFM_FLAG0)) == 622 (IFM_IEEE80211_ADHOC|IFM_FLAG0)) 623 newopmode = IEEE80211_M_AHDEMO; 624 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP) 625 newopmode = IEEE80211_M_HOSTAP; 626 else if (ime->ifm_media & IFM_IEEE80211_ADHOC) 627 newopmode = IEEE80211_M_IBSS; 628 else if (ime->ifm_media & IFM_IEEE80211_MONITOR) 629 newopmode = IEEE80211_M_MONITOR; 630 else 631 newopmode = IEEE80211_M_STA; 632 633 /* 634 * Autoselect doesn't make sense when operating as an AP. 635 * If no phy mode has been selected, pick one and lock it 636 * down so rate tables can be used in forming beacon frames 637 * and the like. 638 */ 639 if (newopmode == IEEE80211_M_HOSTAP && 640 newphymode == IEEE80211_MODE_AUTO) { 641 for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++) 642 if (ic->ic_modecaps & (1<<j)) { 643 newphymode = j; 644 break; 645 } 646 } 647 648 /* 649 * Handle phy mode change. 650 */ 651 if (ic->ic_curmode != newphymode) { /* change phy mode */ 652 error = ieee80211_setmode(ic, newphymode); 653 if (error != 0) 654 return error; 655 error = ENETRESET; 656 } 657 658 /* 659 * Committed to changes, install the rate setting. 660 */ 661 if (ic->ic_fixed_rate != i) { 662 ic->ic_fixed_rate = i; /* set fixed tx rate */ 663 error = ENETRESET; 664 } 665 666 /* 667 * Handle operating mode change. 668 */ 669 if (ic->ic_opmode != newopmode) { 670 ic->ic_opmode = newopmode; 671 switch (newopmode) { 672 case IEEE80211_M_AHDEMO: 673 case IEEE80211_M_HOSTAP: 674 case IEEE80211_M_STA: 675 case IEEE80211_M_MONITOR: 676 ic->ic_flags &= ~IEEE80211_F_IBSSON; 677 break; 678 case IEEE80211_M_IBSS: 679 ic->ic_flags |= IEEE80211_F_IBSSON; 680 break; 681 } 682 /* 683 * Yech, slot time may change depending on the 684 * operating mode so reset it to be sure everything 685 * is setup appropriately. 686 */ 687 ieee80211_reset_erp(ic); 688 ieee80211_wme_initparams(ic); /* after opmode change */ 689 error = ENETRESET; 690 } 691 #ifdef notdef 692 if (error == 0) 693 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media); 694 #endif 695 return error; 696 } 697 698 void 699 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr) 700 { 701 struct ieee80211com *ic; 702 struct ieee80211_rateset *rs; 703 704 ic = ieee80211_find_instance(ifp); 705 if (!ic) { 706 if_printf(ifp, "%s: no 802.11 instance!\n", __func__); 707 return; 708 } 709 imr->ifm_status = IFM_AVALID; 710 imr->ifm_active = IFM_IEEE80211; 711 if (ic->ic_state == IEEE80211_S_RUN) 712 imr->ifm_status |= IFM_ACTIVE; 713 /* 714 * Calculate a current rate if possible. 715 */ 716 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) { 717 /* 718 * A fixed rate is set, report that. 719 */ 720 rs = &ic->ic_sup_rates[ic->ic_curmode]; 721 imr->ifm_active |= ieee80211_rate2media(ic, 722 rs->rs_rates[ic->ic_fixed_rate], ic->ic_curmode); 723 } else if (ic->ic_opmode == IEEE80211_M_STA) { 724 /* 725 * In station mode report the current transmit rate. 726 */ 727 rs = &ic->ic_bss->ni_rates; 728 imr->ifm_active |= ieee80211_rate2media(ic, 729 rs->rs_rates[ic->ic_bss->ni_txrate], ic->ic_curmode); 730 } else 731 imr->ifm_active |= IFM_AUTO; 732 switch (ic->ic_opmode) { 733 case IEEE80211_M_STA: 734 break; 735 case IEEE80211_M_IBSS: 736 imr->ifm_active |= IFM_IEEE80211_ADHOC; 737 break; 738 case IEEE80211_M_AHDEMO: 739 /* should not come here */ 740 break; 741 case IEEE80211_M_HOSTAP: 742 imr->ifm_active |= IFM_IEEE80211_HOSTAP; 743 break; 744 case IEEE80211_M_MONITOR: 745 imr->ifm_active |= IFM_IEEE80211_MONITOR; 746 break; 747 } 748 switch (ic->ic_curmode) { 749 case IEEE80211_MODE_11A: 750 imr->ifm_active |= IFM_IEEE80211_11A; 751 break; 752 case IEEE80211_MODE_11B: 753 imr->ifm_active |= IFM_IEEE80211_11B; 754 break; 755 case IEEE80211_MODE_11G: 756 imr->ifm_active |= IFM_IEEE80211_11G; 757 break; 758 case IEEE80211_MODE_FH: 759 imr->ifm_active |= IFM_IEEE80211_FH; 760 break; 761 case IEEE80211_MODE_TURBO_A: 762 imr->ifm_active |= IFM_IEEE80211_11A 763 | IFM_IEEE80211_TURBO; 764 break; 765 case IEEE80211_MODE_TURBO_G: 766 imr->ifm_active |= IFM_IEEE80211_11G 767 | IFM_IEEE80211_TURBO; 768 break; 769 } 770 } 771 772 void 773 ieee80211_watchdog(struct ieee80211com *ic) 774 { 775 struct ieee80211_node_table *nt; 776 int need_inact_timer = 0; 777 778 if (ic->ic_state != IEEE80211_S_INIT) { 779 if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0) 780 ieee80211_new_state(ic, IEEE80211_S_SCAN, 0); 781 nt = &ic->ic_scan; 782 if (nt->nt_inact_timer) { 783 if (--nt->nt_inact_timer == 0) 784 nt->nt_timeout(nt); 785 need_inact_timer += nt->nt_inact_timer; 786 } 787 nt = &ic->ic_sta; 788 if (nt->nt_inact_timer) { 789 if (--nt->nt_inact_timer == 0) 790 nt->nt_timeout(nt); 791 need_inact_timer += nt->nt_inact_timer; 792 } 793 } 794 if (ic->ic_mgt_timer != 0 || need_inact_timer) 795 ic->ic_ifp->if_timer = 1; 796 } 797 798 /* 799 * Set the current phy mode and recalculate the active channel 800 * set based on the available channels for this mode. Also 801 * select a new default/current channel if the current one is 802 * inappropriate for this mode. 803 */ 804 int 805 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode) 806 { 807 #define N(a) (sizeof(a) / sizeof(a[0])) 808 static const u_int chanflags[] = { 809 0, /* IEEE80211_MODE_AUTO */ 810 IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */ 811 IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */ 812 IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */ 813 IEEE80211_CHAN_FHSS, /* IEEE80211_MODE_FH */ 814 IEEE80211_CHAN_T, /* IEEE80211_MODE_TURBO_A */ 815 IEEE80211_CHAN_108G, /* IEEE80211_MODE_TURBO_G */ 816 }; 817 struct ieee80211_channel *c; 818 u_int modeflags; 819 int i; 820 821 /* validate new mode */ 822 if ((ic->ic_modecaps & (1<<mode)) == 0) { 823 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 824 "%s: mode %u not supported (caps 0x%x)\n", 825 __func__, mode, ic->ic_modecaps); 826 return EINVAL; 827 } 828 829 /* 830 * Verify at least one channel is present in the available 831 * channel list before committing to the new mode. 832 */ 833 KASSERT(mode < N(chanflags), ("Unexpected mode %u", mode)); 834 modeflags = chanflags[mode]; 835 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 836 c = &ic->ic_channels[i]; 837 if (c->ic_flags == 0) 838 continue; 839 if (mode == IEEE80211_MODE_AUTO) { 840 /* ignore static turbo channels for autoselect */ 841 if (!IEEE80211_IS_CHAN_T(c)) 842 break; 843 } else { 844 if ((c->ic_flags & modeflags) == modeflags) 845 break; 846 } 847 } 848 if (i > IEEE80211_CHAN_MAX) { 849 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 850 "%s: no channels found for mode %u\n", __func__, mode); 851 return EINVAL; 852 } 853 854 /* 855 * Calculate the active channel set. 856 */ 857 memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active)); 858 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 859 c = &ic->ic_channels[i]; 860 if (c->ic_flags == 0) 861 continue; 862 if (mode == IEEE80211_MODE_AUTO) { 863 /* take anything but static turbo channels */ 864 if (!IEEE80211_IS_CHAN_T(c)) 865 setbit(ic->ic_chan_active, i); 866 } else { 867 if ((c->ic_flags & modeflags) == modeflags) 868 setbit(ic->ic_chan_active, i); 869 } 870 } 871 /* 872 * If no current/default channel is setup or the current 873 * channel is wrong for the mode then pick the first 874 * available channel from the active list. This is likely 875 * not the right one. 876 */ 877 if (ic->ic_ibss_chan == NULL || 878 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) { 879 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) 880 if (isset(ic->ic_chan_active, i)) { 881 ic->ic_ibss_chan = &ic->ic_channels[i]; 882 break; 883 } 884 KASSERT(ic->ic_ibss_chan != NULL && 885 isset(ic->ic_chan_active, 886 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)), 887 ("Bad IBSS channel %u", 888 ieee80211_chan2ieee(ic, ic->ic_ibss_chan))); 889 } 890 /* 891 * If the desired channel is set but no longer valid then reset it. 892 */ 893 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC && 894 isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ic->ic_des_chan))) 895 ic->ic_des_chan = IEEE80211_CHAN_ANYC; 896 897 /* 898 * Setup an initial rate set according to the 899 * current/default channel selected above. This 900 * will be changed when scanning but must exist 901 * now so driver have a consistent state of ic_ibss_chan. 902 */ 903 if (ic->ic_bss) /* NB: can be called before lateattach */ 904 ic->ic_bss->ni_rates = ic->ic_sup_rates[mode]; 905 906 ic->ic_curmode = mode; 907 ieee80211_reset_erp(ic); /* reset ERP state */ 908 ieee80211_wme_initparams(ic); /* reset WME stat */ 909 910 return 0; 911 #undef N 912 } 913 914 /* 915 * Return the phy mode for with the specified channel so the 916 * caller can select a rate set. This is problematic for channels 917 * where multiple operating modes are possible (e.g. 11g+11b). 918 * In those cases we defer to the current operating mode when set. 919 */ 920 enum ieee80211_phymode 921 ieee80211_chan2mode(struct ieee80211com *ic, struct ieee80211_channel *chan) 922 { 923 if (IEEE80211_IS_CHAN_T(chan)) { 924 return IEEE80211_MODE_TURBO_A; 925 } else if (IEEE80211_IS_CHAN_5GHZ(chan)) { 926 return IEEE80211_MODE_11A; 927 } else if (IEEE80211_IS_CHAN_FHSS(chan)) { 928 return IEEE80211_MODE_FH; 929 } else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN)) { 930 /* 931 * This assumes all 11g channels are also usable 932 * for 11b, which is currently true. 933 */ 934 if (ic->ic_curmode == IEEE80211_MODE_TURBO_G) 935 return IEEE80211_MODE_TURBO_G; 936 if (ic->ic_curmode == IEEE80211_MODE_11B) 937 return IEEE80211_MODE_11B; 938 return IEEE80211_MODE_11G; 939 } else { 940 return IEEE80211_MODE_11B; 941 } 942 } 943 944 /* 945 * convert IEEE80211 rate value to ifmedia subtype. 946 * ieee80211 rate is in unit of 0.5Mbps. 947 */ 948 int 949 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode) 950 { 951 #define N(a) (sizeof(a) / sizeof(a[0])) 952 static const struct { 953 u_int m; /* rate + mode */ 954 u_int r; /* if_media rate */ 955 } rates[] = { 956 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 }, 957 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 }, 958 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 }, 959 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 }, 960 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 }, 961 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 }, 962 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 }, 963 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 }, 964 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 }, 965 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 }, 966 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 }, 967 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 }, 968 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 }, 969 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 }, 970 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 }, 971 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 }, 972 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 }, 973 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 }, 974 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 }, 975 { 44 | IFM_IEEE80211_11G, IFM_IEEE80211_DS22 }, 976 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 }, 977 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 }, 978 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 }, 979 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 }, 980 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 }, 981 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 }, 982 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 }, 983 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 }, 984 /* NB: OFDM72 doesn't realy exist so we don't handle it */ 985 }; 986 u_int mask, i; 987 988 mask = rate & IEEE80211_RATE_VAL; 989 switch (mode) { 990 case IEEE80211_MODE_11A: 991 case IEEE80211_MODE_TURBO_A: 992 mask |= IFM_IEEE80211_11A; 993 break; 994 case IEEE80211_MODE_11B: 995 mask |= IFM_IEEE80211_11B; 996 break; 997 case IEEE80211_MODE_FH: 998 mask |= IFM_IEEE80211_FH; 999 break; 1000 case IEEE80211_MODE_AUTO: 1001 /* NB: ic may be NULL for some drivers */ 1002 if (ic && ic->ic_phytype == IEEE80211_T_FH) { 1003 mask |= IFM_IEEE80211_FH; 1004 break; 1005 } 1006 /* NB: hack, 11g matches both 11b+11a rates */ 1007 /* fall thru... */ 1008 case IEEE80211_MODE_11G: 1009 case IEEE80211_MODE_TURBO_G: 1010 mask |= IFM_IEEE80211_11G; 1011 break; 1012 } 1013 for (i = 0; i < N(rates); i++) 1014 if (rates[i].m == mask) 1015 return rates[i].r; 1016 return IFM_AUTO; 1017 #undef N 1018 } 1019 1020 int 1021 ieee80211_media2rate(int mword) 1022 { 1023 #define N(a) (sizeof(a) / sizeof(a[0])) 1024 static const int ieeerates[] = { 1025 -1, /* IFM_AUTO */ 1026 0, /* IFM_MANUAL */ 1027 0, /* IFM_NONE */ 1028 2, /* IFM_IEEE80211_FH1 */ 1029 4, /* IFM_IEEE80211_FH2 */ 1030 2, /* IFM_IEEE80211_DS1 */ 1031 4, /* IFM_IEEE80211_DS2 */ 1032 11, /* IFM_IEEE80211_DS5 */ 1033 22, /* IFM_IEEE80211_DS11 */ 1034 44, /* IFM_IEEE80211_DS22 */ 1035 12, /* IFM_IEEE80211_OFDM6 */ 1036 18, /* IFM_IEEE80211_OFDM9 */ 1037 24, /* IFM_IEEE80211_OFDM12 */ 1038 36, /* IFM_IEEE80211_OFDM18 */ 1039 48, /* IFM_IEEE80211_OFDM24 */ 1040 72, /* IFM_IEEE80211_OFDM36 */ 1041 96, /* IFM_IEEE80211_OFDM48 */ 1042 108, /* IFM_IEEE80211_OFDM54 */ 1043 144, /* IFM_IEEE80211_OFDM72 */ 1044 }; 1045 return IFM_SUBTYPE(mword) < N(ieeerates) ? 1046 ieeerates[IFM_SUBTYPE(mword)] : 0; 1047 #undef N 1048 } 1049 1050 /* 1051 * Covert PLCP signal/rate field to net80211 rate (.5Mbits/s) 1052 */ 1053 uint8_t 1054 ieee80211_plcp2rate(uint8_t plcp, int ofdm) 1055 { 1056 if (!ofdm) { 1057 switch (plcp) { 1058 /* IEEE Std 802.11b-1999 page 15, subclause 18.2.3.3 */ 1059 case 0x0a: 1060 case 0x14: 1061 case 0x37: 1062 case 0x6e: 1063 /* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */ 1064 case 0xdc: 1065 return plcp / 5; 1066 } 1067 } else { 1068 #define _OFDM_PLCP2RATE_MAX 16 1069 1070 /* IEEE Std 802.11a-1999 page 14, subclause 17.3.4.1 */ 1071 static const uint8_t ofdm_plcp2rate[_OFDM_PLCP2RATE_MAX] = { 1072 [0xb] = 12, 1073 [0xf] = 18, 1074 [0xa] = 24, 1075 [0xe] = 36, 1076 [0x9] = 48, 1077 [0xd] = 72, 1078 [0x8] = 96, 1079 [0xc] = 108 1080 }; 1081 if (plcp < _OFDM_PLCP2RATE_MAX) 1082 return ofdm_plcp2rate[plcp]; 1083 1084 #undef _OFDM_PLCP2RATE_MAX 1085 } 1086 return 0; 1087 } 1088