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