1 /* $OpenBSD: ieee80211.c,v 1.82 2019/12/27 09:46:13 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 void ieee80211_setbasicrates(struct ieee80211com *); 69 int ieee80211_findrate(struct ieee80211com *, enum ieee80211_phymode, int); 70 void ieee80211_configure_ampdu_tx(struct ieee80211com *, int); 71 72 void 73 ieee80211_begin_bgscan(struct ifnet *ifp) 74 { 75 struct ieee80211com *ic = (void *)ifp; 76 77 if ((ic->ic_flags & IEEE80211_F_BGSCAN) || 78 ic->ic_state != IEEE80211_S_RUN || ic->ic_mgt_timer != 0) 79 return; 80 81 if ((ic->ic_flags & IEEE80211_F_RSNON) && !ic->ic_bss->ni_port_valid) 82 return; 83 84 if (ic->ic_bgscan_start != NULL && ic->ic_bgscan_start(ic) == 0) { 85 /* 86 * Free the nodes table to ensure we get an up-to-date view 87 * of APs around us. In particular, we need to kick out the 88 * AP we are associated to. Otherwise, our current AP might 89 * stay cached if it is turned off while we are scanning, and 90 * we could end up picking a now non-existent AP over and over. 91 */ 92 ieee80211_free_allnodes(ic, 0 /* keep ic->ic_bss */); 93 94 ic->ic_flags |= IEEE80211_F_BGSCAN; 95 if (ifp->if_flags & IFF_DEBUG) 96 printf("%s: begin background scan\n", ifp->if_xname); 97 98 /* Driver calls ieee80211_end_scan() when done. */ 99 } 100 } 101 102 void 103 ieee80211_bgscan_timeout(void *arg) 104 { 105 struct ifnet *ifp = arg; 106 107 ieee80211_begin_bgscan(ifp); 108 } 109 110 void 111 ieee80211_channel_init(struct ifnet *ifp) 112 { 113 struct ieee80211com *ic = (void *)ifp; 114 struct ieee80211_channel *c; 115 int i; 116 117 /* 118 * Fill in 802.11 available channel set, mark 119 * all available channels as active, and pick 120 * a default channel if not already specified. 121 */ 122 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail)); 123 ic->ic_modecaps |= 1<<IEEE80211_MODE_AUTO; 124 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 125 c = &ic->ic_channels[i]; 126 if (c->ic_flags) { 127 /* 128 * Verify driver passed us valid data. 129 */ 130 if (i != ieee80211_chan2ieee(ic, c)) { 131 printf("%s: bad channel ignored; " 132 "freq %u flags %x number %u\n", 133 ifp->if_xname, c->ic_freq, c->ic_flags, 134 i); 135 c->ic_flags = 0; /* NB: remove */ 136 continue; 137 } 138 setbit(ic->ic_chan_avail, i); 139 /* 140 * Identify mode capabilities. 141 */ 142 if (IEEE80211_IS_CHAN_A(c)) 143 ic->ic_modecaps |= 1<<IEEE80211_MODE_11A; 144 if (IEEE80211_IS_CHAN_B(c)) 145 ic->ic_modecaps |= 1<<IEEE80211_MODE_11B; 146 if (IEEE80211_IS_CHAN_PUREG(c)) 147 ic->ic_modecaps |= 1<<IEEE80211_MODE_11G; 148 if (IEEE80211_IS_CHAN_N(c)) 149 ic->ic_modecaps |= 1<<IEEE80211_MODE_11N; 150 if (IEEE80211_IS_CHAN_AC(c)) 151 ic->ic_modecaps |= 1<<IEEE80211_MODE_11AC; 152 } 153 } 154 /* validate ic->ic_curmode */ 155 if ((ic->ic_modecaps & (1<<ic->ic_curmode)) == 0) 156 ic->ic_curmode = IEEE80211_MODE_AUTO; 157 ic->ic_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */ 158 } 159 160 void 161 ieee80211_ifattach(struct ifnet *ifp) 162 { 163 struct ieee80211com *ic = (void *)ifp; 164 165 memcpy(((struct arpcom *)ifp)->ac_enaddr, ic->ic_myaddr, 166 ETHER_ADDR_LEN); 167 ether_ifattach(ifp); 168 169 ifp->if_output = ieee80211_output; 170 171 #if NBPFILTER > 0 172 bpfattach(&ic->ic_rawbpf, ifp, DLT_IEEE802_11, 173 sizeof(struct ieee80211_frame_addr4)); 174 #endif 175 ieee80211_crypto_attach(ifp); 176 177 ieee80211_channel_init(ifp); 178 179 /* IEEE 802.11 defines a MTU >= 2290 */ 180 ifp->if_capabilities |= IFCAP_VLAN_MTU; 181 182 ieee80211_setbasicrates(ic); 183 (void)ieee80211_setmode(ic, ic->ic_curmode); 184 185 if (ic->ic_lintval == 0) 186 ic->ic_lintval = 100; /* default sleep */ 187 ic->ic_bmissthres = IEEE80211_BEACON_MISS_THRES; 188 ic->ic_dtim_period = 1; /* all TIMs are DTIMs */ 189 190 ieee80211_node_attach(ifp); 191 ieee80211_proto_attach(ifp); 192 193 if_addgroup(ifp, "wlan"); 194 ifp->if_priority = IF_WIRELESS_DEFAULT_PRIORITY; 195 196 ieee80211_set_link_state(ic, LINK_STATE_DOWN); 197 198 timeout_set(&ic->ic_bgscan_timeout, ieee80211_bgscan_timeout, ifp); 199 } 200 201 void 202 ieee80211_ifdetach(struct ifnet *ifp) 203 { 204 struct ieee80211com *ic = (void *)ifp; 205 206 timeout_del(&ic->ic_bgscan_timeout); 207 ieee80211_proto_detach(ifp); 208 ieee80211_crypto_detach(ifp); 209 ieee80211_node_detach(ifp); 210 ifmedia_delete_instance(&ic->ic_media, IFM_INST_ANY); 211 ether_ifdetach(ifp); 212 } 213 214 /* 215 * Convert MHz frequency to IEEE channel number. 216 */ 217 u_int 218 ieee80211_mhz2ieee(u_int freq, u_int flags) 219 { 220 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 221 if (freq == 2484) 222 return 14; 223 if (freq < 2484) 224 return (freq - 2407) / 5; 225 else 226 return 15 + ((freq - 2512) / 20); 227 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5GHz band */ 228 return (freq - 5000) / 5; 229 } else { /* either, guess */ 230 if (freq == 2484) 231 return 14; 232 if (freq < 2484) 233 return (freq - 2407) / 5; 234 if (freq < 5000) 235 return 15 + ((freq - 2512) / 20); 236 return (freq - 5000) / 5; 237 } 238 } 239 240 /* 241 * Convert channel to IEEE channel number. 242 */ 243 u_int 244 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c) 245 { 246 struct ifnet *ifp = &ic->ic_if; 247 if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX]) 248 return c - ic->ic_channels; 249 else if (c == IEEE80211_CHAN_ANYC) 250 return IEEE80211_CHAN_ANY; 251 252 panic("%s: bogus channel pointer", ifp->if_xname); 253 } 254 255 /* 256 * Convert IEEE channel number to MHz frequency. 257 */ 258 u_int 259 ieee80211_ieee2mhz(u_int chan, u_int flags) 260 { 261 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */ 262 if (chan == 14) 263 return 2484; 264 if (chan < 14) 265 return 2407 + chan*5; 266 else 267 return 2512 + ((chan-15)*20); 268 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5GHz band */ 269 return 5000 + (chan*5); 270 } else { /* either, guess */ 271 if (chan == 14) 272 return 2484; 273 if (chan < 14) /* 0-13 */ 274 return 2407 + chan*5; 275 if (chan < 27) /* 15-26 */ 276 return 2512 + ((chan-15)*20); 277 return 5000 + (chan*5); 278 } 279 } 280 281 void 282 ieee80211_configure_ampdu_tx(struct ieee80211com *ic, int enable) 283 { 284 if ((ic->ic_caps & IEEE80211_C_TX_AMPDU) == 0) 285 return; 286 287 /* Sending AMPDUs requires QoS support. */ 288 if ((ic->ic_caps & IEEE80211_C_QOS) == 0) 289 return; 290 291 if (enable) 292 ic->ic_flags |= IEEE80211_F_QOS; 293 else 294 ic->ic_flags &= ~IEEE80211_F_QOS; 295 } 296 297 /* 298 * Setup the media data structures according to the channel and 299 * rate tables. This must be called by the driver after 300 * ieee80211_attach and before most anything else. 301 */ 302 void 303 ieee80211_media_init(struct ifnet *ifp, 304 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat) 305 { 306 #define ADD(_ic, _s, _o) \ 307 ifmedia_add(&(_ic)->ic_media, \ 308 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL) 309 struct ieee80211com *ic = (void *)ifp; 310 struct ifmediareq imr; 311 int i, j, mode, rate, maxrate, r; 312 uint64_t mword, mopt; 313 const struct ieee80211_rateset *rs; 314 struct ieee80211_rateset allrates; 315 316 /* 317 * Do late attach work that must wait for any subclass 318 * (i.e. driver) work such as overriding methods. 319 */ 320 ieee80211_node_lateattach(ifp); 321 322 /* 323 * Fill in media characteristics. 324 */ 325 ifmedia_init(&ic->ic_media, 0, media_change, media_stat); 326 maxrate = 0; 327 memset(&allrates, 0, sizeof(allrates)); 328 for (mode = IEEE80211_MODE_AUTO; mode <= IEEE80211_MODE_11G; mode++) { 329 static const uint64_t mopts[] = { 330 IFM_AUTO, 331 IFM_IEEE80211_11A, 332 IFM_IEEE80211_11B, 333 IFM_IEEE80211_11G, 334 }; 335 if ((ic->ic_modecaps & (1<<mode)) == 0) 336 continue; 337 mopt = mopts[mode]; 338 ADD(ic, IFM_AUTO, mopt); /* e.g. 11a auto */ 339 #ifndef IEEE80211_STA_ONLY 340 if (ic->ic_caps & IEEE80211_C_IBSS) 341 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_IBSS); 342 if (ic->ic_caps & IEEE80211_C_HOSTAP) 343 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP); 344 if (ic->ic_caps & IEEE80211_C_AHDEMO) 345 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC); 346 #endif 347 if (ic->ic_caps & IEEE80211_C_MONITOR) 348 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR); 349 if (mode == IEEE80211_MODE_AUTO) 350 continue; 351 rs = &ic->ic_sup_rates[mode]; 352 for (i = 0; i < rs->rs_nrates; i++) { 353 rate = rs->rs_rates[i]; 354 mword = ieee80211_rate2media(ic, rate, mode); 355 if (mword == 0) 356 continue; 357 ADD(ic, mword, mopt); 358 #ifndef IEEE80211_STA_ONLY 359 if (ic->ic_caps & IEEE80211_C_IBSS) 360 ADD(ic, mword, mopt | IFM_IEEE80211_IBSS); 361 if (ic->ic_caps & IEEE80211_C_HOSTAP) 362 ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP); 363 if (ic->ic_caps & IEEE80211_C_AHDEMO) 364 ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC); 365 #endif 366 if (ic->ic_caps & IEEE80211_C_MONITOR) 367 ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR); 368 /* 369 * Add rate to the collection of all rates. 370 */ 371 r = rate & IEEE80211_RATE_VAL; 372 for (j = 0; j < allrates.rs_nrates; j++) 373 if (allrates.rs_rates[j] == r) 374 break; 375 if (j == allrates.rs_nrates) { 376 /* unique, add to the set */ 377 allrates.rs_rates[j] = r; 378 allrates.rs_nrates++; 379 } 380 rate = (rate & IEEE80211_RATE_VAL) / 2; 381 if (rate > maxrate) 382 maxrate = rate; 383 } 384 } 385 for (i = 0; i < allrates.rs_nrates; i++) { 386 mword = ieee80211_rate2media(ic, allrates.rs_rates[i], 387 IEEE80211_MODE_AUTO); 388 if (mword == 0) 389 continue; 390 mword = IFM_SUBTYPE(mword); /* remove media options */ 391 ADD(ic, mword, 0); 392 #ifndef IEEE80211_STA_ONLY 393 if (ic->ic_caps & IEEE80211_C_IBSS) 394 ADD(ic, mword, IFM_IEEE80211_IBSS); 395 if (ic->ic_caps & IEEE80211_C_HOSTAP) 396 ADD(ic, mword, IFM_IEEE80211_HOSTAP); 397 if (ic->ic_caps & IEEE80211_C_AHDEMO) 398 ADD(ic, mword, IFM_IEEE80211_ADHOC); 399 #endif 400 if (ic->ic_caps & IEEE80211_C_MONITOR) 401 ADD(ic, mword, IFM_IEEE80211_MONITOR); 402 } 403 404 if (ic->ic_modecaps & (1 << IEEE80211_MODE_11N)) { 405 mopt = IFM_IEEE80211_11N; 406 ADD(ic, IFM_AUTO, mopt); 407 #ifndef IEEE80211_STA_ONLY 408 if (ic->ic_caps & IEEE80211_C_IBSS) 409 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_IBSS); 410 if (ic->ic_caps & IEEE80211_C_HOSTAP) 411 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP); 412 #endif 413 if (ic->ic_caps & IEEE80211_C_MONITOR) 414 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR); 415 for (i = 0; i < IEEE80211_HT_NUM_MCS; i++) { 416 if (!isset(ic->ic_sup_mcs, i)) 417 continue; 418 ADD(ic, IFM_IEEE80211_HT_MCS0 + i, mopt); 419 #ifndef IEEE80211_STA_ONLY 420 if (ic->ic_caps & IEEE80211_C_IBSS) 421 ADD(ic, IFM_IEEE80211_HT_MCS0 + i, 422 mopt | IFM_IEEE80211_IBSS); 423 if (ic->ic_caps & IEEE80211_C_HOSTAP) 424 ADD(ic, IFM_IEEE80211_HT_MCS0 + i, 425 mopt | IFM_IEEE80211_HOSTAP); 426 #endif 427 if (ic->ic_caps & IEEE80211_C_MONITOR) 428 ADD(ic, IFM_IEEE80211_HT_MCS0 + i, 429 mopt | IFM_IEEE80211_MONITOR); 430 } 431 ic->ic_flags |= IEEE80211_F_HTON; /* enable 11n by default */ 432 ieee80211_configure_ampdu_tx(ic, 1); 433 } 434 435 if (ic->ic_modecaps & (1 << IEEE80211_MODE_11AC)) { 436 mopt = IFM_IEEE80211_11AC; 437 ADD(ic, IFM_AUTO, mopt); 438 #ifndef IEEE80211_STA_ONLY 439 if (ic->ic_caps & IEEE80211_C_IBSS) 440 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_IBSS); 441 if (ic->ic_caps & IEEE80211_C_HOSTAP) 442 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP); 443 #endif 444 if (ic->ic_caps & IEEE80211_C_MONITOR) 445 ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR); 446 for (i = 0; i < IEEE80211_VHT_NUM_MCS; i++) { 447 #if 0 448 /* TODO: Obtain VHT MCS information from VHT CAP IE. */ 449 if (!vht_mcs_supported) 450 continue; 451 #endif 452 ADD(ic, IFM_IEEE80211_VHT_MCS0 + i, mopt); 453 #ifndef IEEE80211_STA_ONLY 454 if (ic->ic_caps & IEEE80211_C_IBSS) 455 ADD(ic, IFM_IEEE80211_VHT_MCS0 + i, 456 mopt | IFM_IEEE80211_IBSS); 457 if (ic->ic_caps & IEEE80211_C_HOSTAP) 458 ADD(ic, IFM_IEEE80211_VHT_MCS0 + i, 459 mopt | IFM_IEEE80211_HOSTAP); 460 #endif 461 if (ic->ic_caps & IEEE80211_C_MONITOR) 462 ADD(ic, IFM_IEEE80211_VHT_MCS0 + i, 463 mopt | IFM_IEEE80211_MONITOR); 464 } 465 #if 0 466 ic->ic_flags |= IEEE80211_F_VHTON; /* enable 11ac by default */ 467 if (ic->ic_caps & IEEE80211_C_QOS) 468 ic->ic_flags |= IEEE80211_F_QOS; 469 #endif 470 } 471 472 ieee80211_media_status(ifp, &imr); 473 ifmedia_set(&ic->ic_media, imr.ifm_active); 474 475 if (maxrate) 476 ifp->if_baudrate = IF_Mbps(maxrate); 477 478 #undef ADD 479 } 480 481 int 482 ieee80211_findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, 483 int rate) 484 { 485 #define IEEERATE(_ic,_m,_i) \ 486 ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL) 487 int i, nrates = ic->ic_sup_rates[mode].rs_nrates; 488 for (i = 0; i < nrates; i++) 489 if (IEEERATE(ic, mode, i) == rate) 490 return i; 491 return -1; 492 #undef IEEERATE 493 } 494 495 /* 496 * Handle a media change request. 497 */ 498 int 499 ieee80211_media_change(struct ifnet *ifp) 500 { 501 struct ieee80211com *ic = (void *)ifp; 502 struct ifmedia_entry *ime; 503 enum ieee80211_opmode newopmode; 504 enum ieee80211_phymode newphymode; 505 int i, j, newrate, error = 0; 506 507 ime = ic->ic_media.ifm_cur; 508 /* 509 * First, identify the phy mode. 510 */ 511 switch (IFM_MODE(ime->ifm_media)) { 512 case IFM_IEEE80211_11A: 513 newphymode = IEEE80211_MODE_11A; 514 break; 515 case IFM_IEEE80211_11B: 516 newphymode = IEEE80211_MODE_11B; 517 break; 518 case IFM_IEEE80211_11G: 519 newphymode = IEEE80211_MODE_11G; 520 break; 521 case IFM_IEEE80211_11N: 522 newphymode = IEEE80211_MODE_11N; 523 break; 524 case IFM_IEEE80211_11AC: 525 newphymode = IEEE80211_MODE_11AC; 526 break; 527 case IFM_AUTO: 528 newphymode = IEEE80211_MODE_AUTO; 529 break; 530 default: 531 return EINVAL; 532 } 533 534 /* 535 * Validate requested mode is available. 536 */ 537 if ((ic->ic_modecaps & (1<<newphymode)) == 0) 538 return EINVAL; 539 540 /* 541 * Next, the fixed/variable rate. 542 */ 543 i = -1; 544 if (IFM_SUBTYPE(ime->ifm_media) >= IFM_IEEE80211_VHT_MCS0 && 545 IFM_SUBTYPE(ime->ifm_media) <= IFM_IEEE80211_VHT_MCS9) { 546 if ((ic->ic_modecaps & (1 << IEEE80211_MODE_11AC)) == 0) 547 return EINVAL; 548 if (newphymode != IEEE80211_MODE_AUTO && 549 newphymode != IEEE80211_MODE_11AC) 550 return EINVAL; 551 i = ieee80211_media2mcs(ime->ifm_media); 552 /* TODO: Obtain VHT MCS information from VHT CAP IE. */ 553 if (i == -1 /* || !vht_mcs_supported */) 554 return EINVAL; 555 } else if (IFM_SUBTYPE(ime->ifm_media) >= IFM_IEEE80211_HT_MCS0 && 556 IFM_SUBTYPE(ime->ifm_media) <= IFM_IEEE80211_HT_MCS76) { 557 if ((ic->ic_modecaps & (1 << IEEE80211_MODE_11N)) == 0) 558 return EINVAL; 559 if (newphymode != IEEE80211_MODE_AUTO && 560 newphymode != IEEE80211_MODE_11N) 561 return EINVAL; 562 i = ieee80211_media2mcs(ime->ifm_media); 563 if (i == -1 || isclr(ic->ic_sup_mcs, i)) 564 return EINVAL; 565 } else if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) { 566 /* 567 * Convert media subtype to rate. 568 */ 569 newrate = ieee80211_media2rate(ime->ifm_media); 570 if (newrate == 0) 571 return EINVAL; 572 /* 573 * Check the rate table for the specified/current phy. 574 */ 575 if (newphymode == IEEE80211_MODE_AUTO) { 576 /* 577 * In autoselect mode search for the rate. 578 */ 579 for (j = IEEE80211_MODE_11A; 580 j < IEEE80211_MODE_MAX; j++) { 581 if ((ic->ic_modecaps & (1<<j)) == 0) 582 continue; 583 i = ieee80211_findrate(ic, j, newrate); 584 if (i != -1) { 585 /* lock mode too */ 586 newphymode = j; 587 break; 588 } 589 } 590 } else { 591 i = ieee80211_findrate(ic, newphymode, newrate); 592 } 593 if (i == -1) /* mode/rate mismatch */ 594 return EINVAL; 595 } 596 /* NB: defer rate setting to later */ 597 598 /* 599 * Deduce new operating mode but don't install it just yet. 600 */ 601 #ifndef IEEE80211_STA_ONLY 602 if (ime->ifm_media & IFM_IEEE80211_ADHOC) 603 newopmode = IEEE80211_M_AHDEMO; 604 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP) 605 newopmode = IEEE80211_M_HOSTAP; 606 else if (ime->ifm_media & IFM_IEEE80211_IBSS) 607 newopmode = IEEE80211_M_IBSS; 608 else 609 #endif 610 if (ime->ifm_media & IFM_IEEE80211_MONITOR) 611 newopmode = IEEE80211_M_MONITOR; 612 else 613 newopmode = IEEE80211_M_STA; 614 615 #ifndef IEEE80211_STA_ONLY 616 /* 617 * Autoselect doesn't make sense when operating as an AP. 618 * If no phy mode has been selected, pick one and lock it 619 * down so rate tables can be used in forming beacon frames 620 * and the like. 621 */ 622 if (newopmode == IEEE80211_M_HOSTAP && 623 newphymode == IEEE80211_MODE_AUTO) { 624 if (ic->ic_modecaps & (1 << IEEE80211_MODE_11AC)) 625 newphymode = IEEE80211_MODE_11AC; 626 else if (ic->ic_modecaps & (1 << IEEE80211_MODE_11N)) 627 newphymode = IEEE80211_MODE_11N; 628 else if (ic->ic_modecaps & (1 << IEEE80211_MODE_11A)) 629 newphymode = IEEE80211_MODE_11A; 630 else if (ic->ic_modecaps & (1 << IEEE80211_MODE_11G)) 631 newphymode = IEEE80211_MODE_11G; 632 else 633 newphymode = IEEE80211_MODE_11B; 634 } 635 #endif 636 637 /* 638 * Handle phy mode change. 639 */ 640 if (ic->ic_curmode != newphymode) { /* change phy mode */ 641 error = ieee80211_setmode(ic, newphymode); 642 if (error != 0) 643 return error; 644 error = ENETRESET; 645 } 646 647 /* 648 * Committed to changes, install the MCS/rate setting. 649 */ 650 ic->ic_flags &= ~(IEEE80211_F_HTON | IEEE80211_F_VHTON); 651 ieee80211_configure_ampdu_tx(ic, 0); 652 if ((ic->ic_modecaps & (1 << IEEE80211_MODE_11AC)) && 653 (newphymode == IEEE80211_MODE_AUTO || 654 newphymode == IEEE80211_MODE_11AC)) { 655 ic->ic_flags |= IEEE80211_F_VHTON; 656 ieee80211_configure_ampdu_tx(ic, 1); 657 } else if ((ic->ic_modecaps & (1 << IEEE80211_MODE_11N)) && 658 (newphymode == IEEE80211_MODE_AUTO || 659 newphymode == IEEE80211_MODE_11N)) { 660 ic->ic_flags |= IEEE80211_F_HTON; 661 ieee80211_configure_ampdu_tx(ic, 1); 662 } 663 if ((ic->ic_flags & (IEEE80211_F_HTON | IEEE80211_F_VHTON)) == 0) { 664 ic->ic_fixed_mcs = -1; 665 if (ic->ic_fixed_rate != i) { 666 ic->ic_fixed_rate = i; /* set fixed tx rate */ 667 error = ENETRESET; 668 } 669 } else { 670 ic->ic_fixed_rate = -1; 671 if (ic->ic_fixed_mcs != i) { 672 ic->ic_fixed_mcs = i; /* set fixed mcs */ 673 error = ENETRESET; 674 } 675 } 676 677 /* 678 * Handle operating mode change. 679 */ 680 if (ic->ic_opmode != newopmode) { 681 ic->ic_opmode = newopmode; 682 #ifndef IEEE80211_STA_ONLY 683 switch (newopmode) { 684 case IEEE80211_M_AHDEMO: 685 case IEEE80211_M_HOSTAP: 686 case IEEE80211_M_STA: 687 case IEEE80211_M_MONITOR: 688 ic->ic_flags &= ~IEEE80211_F_IBSSON; 689 break; 690 case IEEE80211_M_IBSS: 691 ic->ic_flags |= IEEE80211_F_IBSSON; 692 break; 693 } 694 #endif 695 /* 696 * Yech, slot time may change depending on the 697 * operating mode so reset it to be sure everything 698 * is setup appropriately. 699 */ 700 ieee80211_reset_erp(ic); 701 error = ENETRESET; 702 } 703 #ifdef notdef 704 if (error == 0) 705 ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media); 706 #endif 707 return error; 708 } 709 710 void 711 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr) 712 { 713 struct ieee80211com *ic = (void *)ifp; 714 const struct ieee80211_node *ni = NULL; 715 716 imr->ifm_status = IFM_AVALID; 717 imr->ifm_active = IFM_IEEE80211; 718 if (ic->ic_state == IEEE80211_S_RUN && 719 (ic->ic_opmode != IEEE80211_M_STA || 720 !(ic->ic_flags & IEEE80211_F_RSNON) || 721 ic->ic_bss->ni_port_valid)) 722 imr->ifm_status |= IFM_ACTIVE; 723 imr->ifm_active |= IFM_AUTO; 724 switch (ic->ic_opmode) { 725 case IEEE80211_M_STA: 726 ni = ic->ic_bss; 727 if (ic->ic_curmode == IEEE80211_MODE_11N || 728 ic->ic_curmode == IEEE80211_MODE_11AC) 729 imr->ifm_active |= ieee80211_mcs2media(ic, 730 ni->ni_txmcs, ic->ic_curmode); 731 else 732 /* calculate rate subtype */ 733 imr->ifm_active |= ieee80211_rate2media(ic, 734 ni->ni_rates.rs_rates[ni->ni_txrate], 735 ic->ic_curmode); 736 break; 737 #ifndef IEEE80211_STA_ONLY 738 case IEEE80211_M_IBSS: 739 imr->ifm_active |= IFM_IEEE80211_IBSS; 740 break; 741 case IEEE80211_M_AHDEMO: 742 imr->ifm_active |= IFM_IEEE80211_ADHOC; 743 break; 744 case IEEE80211_M_HOSTAP: 745 imr->ifm_active |= IFM_IEEE80211_HOSTAP; 746 break; 747 #endif 748 case IEEE80211_M_MONITOR: 749 imr->ifm_active |= IFM_IEEE80211_MONITOR; 750 break; 751 default: 752 break; 753 } 754 switch (ic->ic_curmode) { 755 case IEEE80211_MODE_11A: 756 imr->ifm_active |= IFM_IEEE80211_11A; 757 break; 758 case IEEE80211_MODE_11B: 759 imr->ifm_active |= IFM_IEEE80211_11B; 760 break; 761 case IEEE80211_MODE_11G: 762 imr->ifm_active |= IFM_IEEE80211_11G; 763 break; 764 case IEEE80211_MODE_11N: 765 imr->ifm_active |= IFM_IEEE80211_11N; 766 break; 767 case IEEE80211_MODE_11AC: 768 imr->ifm_active |= IFM_IEEE80211_11AC; 769 break; 770 } 771 } 772 773 void 774 ieee80211_watchdog(struct ifnet *ifp) 775 { 776 struct ieee80211com *ic = (void *)ifp; 777 778 if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0) { 779 if (ic->ic_opmode == IEEE80211_M_STA && 780 (ic->ic_state == IEEE80211_S_AUTH || 781 ic->ic_state == IEEE80211_S_ASSOC)) { 782 struct ieee80211_node *ni; 783 if (ifp->if_flags & IFF_DEBUG) 784 printf("%s: %s timed out for %s\n", 785 ifp->if_xname, 786 ic->ic_state == IEEE80211_S_ASSOC ? 787 "association" : "authentication", 788 ether_sprintf(ic->ic_bss->ni_macaddr)); 789 ni = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr); 790 if (ni) 791 ni->ni_fails++; 792 if (ISSET(ic->ic_flags, IEEE80211_F_AUTO_JOIN)) 793 ieee80211_deselect_ess(ic); 794 } 795 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 796 } 797 798 if (ic->ic_mgt_timer != 0) 799 ifp->if_timer = 1; 800 } 801 802 const struct ieee80211_rateset ieee80211_std_rateset_11a = 803 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } }; 804 805 const struct ieee80211_rateset ieee80211_std_rateset_11b = 806 { 4, { 2, 4, 11, 22 } }; 807 808 const struct ieee80211_rateset ieee80211_std_rateset_11g = 809 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } }; 810 811 const struct ieee80211_ht_rateset ieee80211_std_ratesets_11n[] = { 812 /* MCS 0-7, 20MHz channel, no SGI */ 813 { 8, { 13, 26, 39, 52, 78, 104, 117, 130 }, 0x000000ff, 0, 7, 0}, 814 815 /* MCS 0-7, 20MHz channel, SGI */ 816 { 8, { 14, 29, 43, 58, 87, 116, 130, 144 }, 0x000000ff, 0, 7, 1 }, 817 818 /* MCS 8-15, 20MHz channel, no SGI */ 819 { 8, { 26, 52, 78, 104, 156, 208, 234, 260 }, 0x0000ff00, 8, 15, 0 }, 820 821 /* MCS 8-15, 20MHz channel, SGI */ 822 { 8, { 29, 58, 87, 116, 173, 231, 261, 289 }, 0x0000ff00, 8, 15, 1 }, 823 824 /* MCS 16-23, 20MHz channel, no SGI */ 825 { 8, { 39, 78, 117, 156, 234, 312, 351, 390 }, 0x00ff0000, 16, 23, 0 }, 826 827 /* MCS 16-23, 20MHz channel, SGI */ 828 { 8, { 43, 87, 130, 173, 260, 347, 390, 433 }, 0x00ff0000, 16, 23, 1 }, 829 830 /* MCS 24-31, 20MHz channel, no SGI */ 831 { 8, { 52, 104, 156, 208, 312, 416, 468, 520 }, 0xff000000, 24, 31, 0 }, 832 833 /* MCS 24-31, 20MHz channel, SGI */ 834 { 8, { 58, 116, 173, 231, 347, 462, 520, 578 }, 0xff000000, 24, 31, 1 }, 835 }; 836 837 const struct ieee80211_vht_rateset ieee80211_std_ratesets_11ac[] = { 838 /* MCS 0-8 (MCS 9 N/A), 1 SS, 20MHz channel, no SGI */ 839 { 9, { 13, 26, 39, 52, 78, 104, 117, 130, 156 }, 1, 0 }, 840 841 /* MCS 0-8 (MCS 9 N/A), 1 SS, 20MHz channel, SGI */ 842 { 9, { 14, 29, 43, 58, 87, 116, 130, 144, 174 }, 1, 1 }, 843 844 /* MCS 0-8 (MCS 9 N/A), 2 SS, 20MHz channel, no SGI */ 845 { 9, { 26, 52, 78, 104, 156, 208, 234, 260, 312 }, 2, 0 }, 846 847 /* MCS 0-8 (MCS 9 N/A), 2 SS, 20MHz channel, SGI */ 848 { 9, { 29, 58, 87, 116, 173, 231, 261, 289, 347 }, 2, 1 }, 849 850 /* MCS 0-9, 1 SS, 40MHz channel, no SGI */ 851 { 10, { 27, 54, 81, 108, 162, 216, 243, 270, 324, 360 }, 1, 0 }, 852 853 /* MCS 0-9, 1 SS, 40MHz channel, SGI */ 854 { 10, { 30, 60, 90, 120, 180, 240, 270, 300, 360, 400 }, 1, 1 }, 855 856 /* MCS 0-9, 2 SS, 40MHz channel, no SGI */ 857 { 10, { 54, 108, 162, 216, 324, 432, 486, 540, 648, 720 }, 2, 0 }, 858 859 /* MCS 0-9, 2 SS, 40MHz channel, SGI */ 860 { 10, { 60, 120, 180, 240, 360, 480, 540, 600, 720, 800 }, 2, 1 }, 861 862 /* MCS 0-9, 1 SS, 80MHz channel, no SGI */ 863 { 10, { 59, 117, 176, 234, 351, 468, 527, 585, 702, 780 }, 1, 0 }, 864 865 /* MCS 0-9, 1 SS, 80MHz channel, SGI */ 866 { 10, { 65, 130, 195, 260, 390, 520, 585, 650, 780, 867 }, 1, 1 }, 867 868 /* MCS 0-9, 2 SS, 80MHz channel, no SGI */ 869 { 10, { 117, 234, 351, 468, 702, 936, 1053, 1404, 1560 }, 2, 0 }, 870 871 /* MCS 0-9, 2 SS, 80MHz channel, SGI */ 872 { 10, { 130, 260, 390, 520, 780, 1040, 1170, 1300, 1560, 1734 }, 2, 1 }, 873 }; 874 875 /* 876 * Mark the basic rates for the 11g rate table based on the 877 * operating mode. For real 11g we mark all the 11b rates 878 * and 6, 12, and 24 OFDM. For 11b compatibility we mark only 879 * 11b rates. There's also a pseudo 11a-mode used to mark only 880 * the basic OFDM rates. 881 */ 882 void 883 ieee80211_setbasicrates(struct ieee80211com *ic) 884 { 885 static const struct ieee80211_rateset basic[] = { 886 { 0 }, /* IEEE80211_MODE_AUTO */ 887 { 3, { 12, 24, 48 } }, /* IEEE80211_MODE_11A */ 888 { 2, { 2, 4 } }, /* IEEE80211_MODE_11B */ 889 { 4, { 2, 4, 11, 22 } }, /* IEEE80211_MODE_11G */ 890 { 0 }, /* IEEE80211_MODE_11N */ 891 { 0 }, /* IEEE80211_MODE_11AC */ 892 }; 893 enum ieee80211_phymode mode; 894 struct ieee80211_rateset *rs; 895 int i, j; 896 897 for (mode = 0; mode < IEEE80211_MODE_MAX; mode++) { 898 rs = &ic->ic_sup_rates[mode]; 899 for (i = 0; i < rs->rs_nrates; i++) { 900 rs->rs_rates[i] &= IEEE80211_RATE_VAL; 901 for (j = 0; j < basic[mode].rs_nrates; j++) { 902 if (basic[mode].rs_rates[j] == 903 rs->rs_rates[i]) { 904 rs->rs_rates[i] |= 905 IEEE80211_RATE_BASIC; 906 break; 907 } 908 } 909 } 910 } 911 } 912 913 int 914 ieee80211_min_basic_rate(struct ieee80211com *ic) 915 { 916 struct ieee80211_rateset *rs = &ic->ic_bss->ni_rates; 917 int i, min, rval; 918 919 min = -1; 920 921 for (i = 0; i < rs->rs_nrates; i++) { 922 if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) == 0) 923 continue; 924 rval = (rs->rs_rates[i] & IEEE80211_RATE_VAL); 925 if (min == -1) 926 min = rval; 927 else if (rval < min) 928 min = rval; 929 } 930 931 /* Default to 1 Mbit/s on 2GHz and 6 Mbit/s on 5GHz. */ 932 if (min == -1) 933 min = IEEE80211_IS_CHAN_2GHZ(ic->ic_bss->ni_chan) ? 2 : 12; 934 935 return min; 936 } 937 938 int 939 ieee80211_max_basic_rate(struct ieee80211com *ic) 940 { 941 struct ieee80211_rateset *rs = &ic->ic_bss->ni_rates; 942 int i, max, rval; 943 944 /* Default to 1 Mbit/s on 2GHz and 6 Mbit/s on 5GHz. */ 945 max = IEEE80211_IS_CHAN_2GHZ(ic->ic_bss->ni_chan) ? 2 : 12; 946 947 for (i = 0; i < rs->rs_nrates; i++) { 948 if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) == 0) 949 continue; 950 rval = (rs->rs_rates[i] & IEEE80211_RATE_VAL); 951 if (rval > max) 952 max = rval; 953 } 954 955 return max; 956 } 957 958 /* 959 * Set the current phy mode and recalculate the active channel 960 * set based on the available channels for this mode. Also 961 * select a new default/current channel if the current one is 962 * inappropriate for this mode. 963 */ 964 int 965 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode) 966 { 967 struct ifnet *ifp = &ic->ic_if; 968 static const u_int chanflags[] = { 969 0, /* IEEE80211_MODE_AUTO */ 970 IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */ 971 IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */ 972 IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */ 973 IEEE80211_CHAN_HT, /* IEEE80211_MODE_11N */ 974 IEEE80211_CHAN_VHT, /* IEEE80211_MODE_11AC */ 975 }; 976 const struct ieee80211_channel *c; 977 u_int modeflags; 978 int i; 979 980 /* validate new mode */ 981 if ((ic->ic_modecaps & (1<<mode)) == 0) { 982 DPRINTF(("mode %u not supported (caps 0x%x)\n", 983 mode, ic->ic_modecaps)); 984 return EINVAL; 985 } 986 987 /* 988 * Verify at least one channel is present in the available 989 * channel list before committing to the new mode. 990 */ 991 if (mode >= nitems(chanflags)) 992 panic("%s: unexpected mode %u", __func__, mode); 993 modeflags = chanflags[mode]; 994 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 995 c = &ic->ic_channels[i]; 996 if (mode == IEEE80211_MODE_AUTO) { 997 if (c->ic_flags != 0) 998 break; 999 } else if ((c->ic_flags & modeflags) == modeflags) 1000 break; 1001 } 1002 if (i > IEEE80211_CHAN_MAX) { 1003 DPRINTF(("no channels found for mode %u\n", mode)); 1004 return EINVAL; 1005 } 1006 1007 /* 1008 * Calculate the active channel set. 1009 */ 1010 memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active)); 1011 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 1012 c = &ic->ic_channels[i]; 1013 if (mode == IEEE80211_MODE_AUTO) { 1014 if (c->ic_flags != 0) 1015 setbit(ic->ic_chan_active, i); 1016 } else if ((c->ic_flags & modeflags) == modeflags) 1017 setbit(ic->ic_chan_active, i); 1018 } 1019 /* 1020 * If no current/default channel is setup or the current 1021 * channel is wrong for the mode then pick the first 1022 * available channel from the active list. This is likely 1023 * not the right one. 1024 */ 1025 if (ic->ic_ibss_chan == NULL || isclr(ic->ic_chan_active, 1026 ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) { 1027 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) 1028 if (isset(ic->ic_chan_active, i)) { 1029 ic->ic_ibss_chan = &ic->ic_channels[i]; 1030 break; 1031 } 1032 if ((ic->ic_ibss_chan == NULL) || isclr(ic->ic_chan_active, 1033 ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) 1034 panic("Bad IBSS channel %u", 1035 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)); 1036 } 1037 1038 /* 1039 * Reset the scan state for the new mode. This avoids scanning 1040 * of invalid channels, ie. 5GHz channels in 11b mode. 1041 */ 1042 ieee80211_reset_scan(ifp); 1043 1044 ic->ic_curmode = mode; 1045 ieee80211_reset_erp(ic); /* reset ERP state */ 1046 1047 return 0; 1048 } 1049 1050 enum ieee80211_phymode 1051 ieee80211_next_mode(struct ifnet *ifp) 1052 { 1053 struct ieee80211com *ic = (void *)ifp; 1054 uint16_t mode; 1055 1056 /* 1057 * Indicate a wrap-around if we're running in a fixed, user-specified 1058 * phy mode. 1059 */ 1060 if (IFM_MODE(ic->ic_media.ifm_cur->ifm_media) != IFM_AUTO) 1061 return (IEEE80211_MODE_AUTO); 1062 1063 /* 1064 * Always scan in AUTO mode if the driver scans all bands. 1065 * The current mode might have changed during association 1066 * so we must reset it here. 1067 */ 1068 if (ic->ic_caps & IEEE80211_C_SCANALLBAND) { 1069 ieee80211_setmode(ic, IEEE80211_MODE_AUTO); 1070 return (ic->ic_curmode); 1071 } 1072 1073 /* 1074 * Get the next supported mode; effectively, this alternates between 1075 * the 11a (5GHz) and 11b/g (2GHz) modes. What matters is that each 1076 * supported channel gets scanned. 1077 */ 1078 for (mode = ic->ic_curmode + 1; mode <= IEEE80211_MODE_MAX; mode++) { 1079 /* 1080 * Skip over 11n mode. Its set of channels is the superset 1081 * of all channels supported by the other modes. 1082 */ 1083 if (mode == IEEE80211_MODE_11N) 1084 continue; 1085 /* 1086 * Skip over 11ac mode. Its set of channels is the set 1087 * of all channels supported by 11a. 1088 */ 1089 if (mode == IEEE80211_MODE_11AC) 1090 continue; 1091 1092 /* Start over if we have already tried all modes. */ 1093 if (mode == IEEE80211_MODE_MAX) { 1094 mode = IEEE80211_MODE_AUTO; 1095 break; 1096 } 1097 1098 if (ic->ic_modecaps & (1 << mode)) 1099 break; 1100 } 1101 1102 if (mode != ic->ic_curmode) 1103 ieee80211_setmode(ic, mode); 1104 1105 return (ic->ic_curmode); 1106 } 1107 1108 /* 1109 * Return the phy mode for with the specified channel so the 1110 * caller can select a rate set. This is problematic and the 1111 * work here assumes how things work elsewhere in this code. 1112 * 1113 * Because the result of this function is ultimately used to select a 1114 * rate from the rate set of the returned mode, it must return one of the 1115 * legacy 11a/b/g modes; 11n and 11ac modes use MCS instead of rate sets. 1116 */ 1117 enum ieee80211_phymode 1118 ieee80211_chan2mode(struct ieee80211com *ic, 1119 const struct ieee80211_channel *chan) 1120 { 1121 /* 1122 * Are we fixed in 11a/b/g mode? 1123 * NB: this assumes the channel would not be supplied to us 1124 * unless it was already compatible with the current mode. 1125 */ 1126 if (ic->ic_curmode == IEEE80211_MODE_11A || 1127 ic->ic_curmode == IEEE80211_MODE_11B || 1128 ic->ic_curmode == IEEE80211_MODE_11G) 1129 return ic->ic_curmode; 1130 1131 /* If no channel was provided, return the most suitable legacy mode. */ 1132 if (chan == IEEE80211_CHAN_ANYC) { 1133 switch (ic->ic_curmode) { 1134 case IEEE80211_MODE_AUTO: 1135 case IEEE80211_MODE_11N: 1136 if (ic->ic_modecaps & (1 << IEEE80211_MODE_11A)) 1137 return IEEE80211_MODE_11A; 1138 if (ic->ic_modecaps & (1 << IEEE80211_MODE_11G)) 1139 return IEEE80211_MODE_11G; 1140 return IEEE80211_MODE_11B; 1141 case IEEE80211_MODE_11AC: 1142 return IEEE80211_MODE_11A; 1143 default: 1144 return ic->ic_curmode; 1145 } 1146 } 1147 1148 /* Deduce a legacy mode based on the channel characteristics. */ 1149 if (IEEE80211_IS_CHAN_5GHZ(chan)) 1150 return IEEE80211_MODE_11A; 1151 else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN)) 1152 return IEEE80211_MODE_11G; 1153 else 1154 return IEEE80211_MODE_11B; 1155 } 1156 1157 /* 1158 * Convert IEEE80211 MCS index to ifmedia subtype. 1159 */ 1160 uint64_t 1161 ieee80211_mcs2media(struct ieee80211com *ic, int mcs, 1162 enum ieee80211_phymode mode) 1163 { 1164 switch (mode) { 1165 case IEEE80211_MODE_11A: 1166 case IEEE80211_MODE_11B: 1167 case IEEE80211_MODE_11G: 1168 /* these modes use rates, not MCS */ 1169 panic("%s: unexpected mode %d", __func__, mode); 1170 break; 1171 case IEEE80211_MODE_11N: 1172 if (mcs >= 0 && mcs < IEEE80211_HT_NUM_MCS) 1173 return (IFM_IEEE80211_11N | 1174 (IFM_IEEE80211_HT_MCS0 + mcs)); 1175 break; 1176 case IEEE80211_MODE_11AC: 1177 if (mcs >= 0 && mcs < IEEE80211_VHT_NUM_MCS) 1178 return (IFM_IEEE80211_11AC | 1179 (IFM_IEEE80211_VHT_MCS0 + mcs)); 1180 break; 1181 case IEEE80211_MODE_AUTO: 1182 break; 1183 } 1184 1185 return IFM_AUTO; 1186 } 1187 1188 /* 1189 * Convert ifmedia subtype to IEEE80211 MCS index. 1190 */ 1191 int 1192 ieee80211_media2mcs(uint64_t mword) 1193 { 1194 uint64_t subtype; 1195 1196 subtype = IFM_SUBTYPE(mword); 1197 1198 if (subtype == IFM_AUTO) 1199 return -1; 1200 else if (subtype == IFM_MANUAL || subtype == IFM_NONE) 1201 return 0; 1202 1203 if (subtype >= IFM_IEEE80211_HT_MCS0 && 1204 subtype <= IFM_IEEE80211_HT_MCS76) 1205 return (int)(subtype - IFM_IEEE80211_HT_MCS0); 1206 1207 if (subtype >= IFM_IEEE80211_VHT_MCS0 && 1208 subtype <= IFM_IEEE80211_VHT_MCS9) 1209 return (int)(subtype - IFM_IEEE80211_VHT_MCS0); 1210 1211 return -1; 1212 } 1213 1214 /* 1215 * convert IEEE80211 rate value to ifmedia subtype. 1216 * ieee80211 rate is in unit of 0.5Mbps. 1217 */ 1218 uint64_t 1219 ieee80211_rate2media(struct ieee80211com *ic, int rate, 1220 enum ieee80211_phymode mode) 1221 { 1222 static const struct { 1223 uint64_t m; /* rate + mode */ 1224 uint64_t r; /* if_media rate */ 1225 } rates[] = { 1226 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 }, 1227 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 }, 1228 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 }, 1229 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 }, 1230 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 }, 1231 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 }, 1232 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 }, 1233 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 }, 1234 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 }, 1235 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 }, 1236 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 }, 1237 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 }, 1238 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 }, 1239 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 }, 1240 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 }, 1241 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 }, 1242 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 }, 1243 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 }, 1244 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 }, 1245 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 }, 1246 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 }, 1247 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 }, 1248 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 }, 1249 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 }, 1250 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 }, 1251 /* NB: OFDM72 doesn't really exist so we don't handle it */ 1252 }; 1253 uint64_t mask; 1254 int i; 1255 1256 mask = rate & IEEE80211_RATE_VAL; 1257 switch (mode) { 1258 case IEEE80211_MODE_11A: 1259 mask |= IFM_IEEE80211_11A; 1260 break; 1261 case IEEE80211_MODE_11B: 1262 mask |= IFM_IEEE80211_11B; 1263 break; 1264 case IEEE80211_MODE_AUTO: 1265 /* NB: hack, 11g matches both 11b+11a rates */ 1266 /* FALLTHROUGH */ 1267 case IEEE80211_MODE_11G: 1268 mask |= IFM_IEEE80211_11G; 1269 break; 1270 case IEEE80211_MODE_11N: 1271 case IEEE80211_MODE_11AC: 1272 /* 11n/11ac uses MCS, not rates. */ 1273 panic("%s: unexpected mode %d", __func__, mode); 1274 break; 1275 } 1276 for (i = 0; i < nitems(rates); i++) 1277 if (rates[i].m == mask) 1278 return rates[i].r; 1279 return IFM_AUTO; 1280 } 1281 1282 int 1283 ieee80211_media2rate(uint64_t mword) 1284 { 1285 int i; 1286 static const struct { 1287 uint64_t subtype; 1288 int rate; 1289 } ieeerates[] = { 1290 { IFM_AUTO, -1 }, 1291 { IFM_MANUAL, 0 }, 1292 { IFM_NONE, 0 }, 1293 { IFM_IEEE80211_DS1, 2 }, 1294 { IFM_IEEE80211_DS2, 4 }, 1295 { IFM_IEEE80211_DS5, 11 }, 1296 { IFM_IEEE80211_DS11, 22 }, 1297 { IFM_IEEE80211_DS22, 44 }, 1298 { IFM_IEEE80211_OFDM6, 12 }, 1299 { IFM_IEEE80211_OFDM9, 18 }, 1300 { IFM_IEEE80211_OFDM12, 24 }, 1301 { IFM_IEEE80211_OFDM18, 36 }, 1302 { IFM_IEEE80211_OFDM24, 48 }, 1303 { IFM_IEEE80211_OFDM36, 72 }, 1304 { IFM_IEEE80211_OFDM48, 96 }, 1305 { IFM_IEEE80211_OFDM54, 108 }, 1306 { IFM_IEEE80211_OFDM72, 144 }, 1307 }; 1308 for (i = 0; i < nitems(ieeerates); i++) { 1309 if (ieeerates[i].subtype == IFM_SUBTYPE(mword)) 1310 return ieeerates[i].rate; 1311 } 1312 return 0; 1313 } 1314 1315 /* 1316 * Convert bit rate (in 0.5Mbps units) to PLCP signal (R4-R1) and vice versa. 1317 */ 1318 u_int8_t 1319 ieee80211_rate2plcp(u_int8_t rate, enum ieee80211_phymode mode) 1320 { 1321 rate &= IEEE80211_RATE_VAL; 1322 1323 if (mode == IEEE80211_MODE_11B) { 1324 /* IEEE Std 802.11b-1999 page 15, subclause 18.2.3.3 */ 1325 switch (rate) { 1326 case 2: return 10; 1327 case 4: return 20; 1328 case 11: return 55; 1329 case 22: return 110; 1330 /* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */ 1331 case 44: return 220; 1332 } 1333 } else if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11A) { 1334 /* IEEE Std 802.11a-1999 page 14, subclause 17.3.4.1 */ 1335 switch (rate) { 1336 case 12: return 0x0b; 1337 case 18: return 0x0f; 1338 case 24: return 0x0a; 1339 case 36: return 0x0e; 1340 case 48: return 0x09; 1341 case 72: return 0x0d; 1342 case 96: return 0x08; 1343 case 108: return 0x0c; 1344 } 1345 } else 1346 panic("%s: unexpected mode %u", __func__, mode); 1347 1348 DPRINTF(("unsupported rate %u\n", rate)); 1349 1350 return 0; 1351 } 1352 1353 u_int8_t 1354 ieee80211_plcp2rate(u_int8_t plcp, enum ieee80211_phymode mode) 1355 { 1356 if (mode == IEEE80211_MODE_11B) { 1357 /* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */ 1358 switch (plcp) { 1359 case 10: return 2; 1360 case 20: return 4; 1361 case 55: return 11; 1362 case 110: return 22; 1363 /* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */ 1364 case 220: return 44; 1365 } 1366 } else if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11A) { 1367 /* IEEE Std 802.11a-1999 page 14, subclause 17.3.4.1 */ 1368 switch (plcp) { 1369 case 0x0b: return 12; 1370 case 0x0f: return 18; 1371 case 0x0a: return 24; 1372 case 0x0e: return 36; 1373 case 0x09: return 48; 1374 case 0x0d: return 72; 1375 case 0x08: return 96; 1376 case 0x0c: return 108; 1377 } 1378 } else 1379 panic("%s: unexpected mode %u", __func__, mode); 1380 1381 DPRINTF(("unsupported plcp %u\n", plcp)); 1382 1383 return 0; 1384 } 1385