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