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