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