1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Wireless utility functions 4 * 5 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net> 6 * Copyright 2013-2014 Intel Mobile Communications GmbH 7 * Copyright 2017 Intel Deutschland GmbH 8 * Copyright (C) 2018-2023, 2025-2026 Intel Corporation 9 */ 10 #include <linux/export.h> 11 #include <linux/bitops.h> 12 #include <linux/etherdevice.h> 13 #include <linux/slab.h> 14 #include <linux/ieee80211.h> 15 #include <net/cfg80211.h> 16 #include <net/ip.h> 17 #include <net/dsfield.h> 18 #include <linux/if_vlan.h> 19 #include <linux/mpls.h> 20 #include <linux/gcd.h> 21 #include <linux/bitfield.h> 22 #include <linux/nospec.h> 23 #include "core.h" 24 #include "rdev-ops.h" 25 26 27 const struct ieee80211_rate * 28 ieee80211_get_response_rate(struct ieee80211_supported_band *sband, 29 u32 basic_rates, int bitrate) 30 { 31 struct ieee80211_rate *result = &sband->bitrates[0]; 32 int i; 33 34 for (i = 0; i < sband->n_bitrates; i++) { 35 if (!(basic_rates & BIT(i))) 36 continue; 37 if (sband->bitrates[i].bitrate > bitrate) 38 continue; 39 result = &sband->bitrates[i]; 40 } 41 42 return result; 43 } 44 EXPORT_SYMBOL(ieee80211_get_response_rate); 45 46 u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband) 47 { 48 struct ieee80211_rate *bitrates; 49 u32 mandatory_rates = 0; 50 enum ieee80211_rate_flags mandatory_flag; 51 int i; 52 53 if (WARN_ON(!sband)) 54 return 1; 55 56 if (sband->band == NL80211_BAND_2GHZ) 57 mandatory_flag = IEEE80211_RATE_MANDATORY_B; 58 else 59 mandatory_flag = IEEE80211_RATE_MANDATORY_A; 60 61 bitrates = sband->bitrates; 62 for (i = 0; i < sband->n_bitrates; i++) 63 if (bitrates[i].flags & mandatory_flag) 64 mandatory_rates |= BIT(i); 65 return mandatory_rates; 66 } 67 EXPORT_SYMBOL(ieee80211_mandatory_rates); 68 69 u32 ieee80211_channel_to_freq_khz(int chan, enum nl80211_band band) 70 { 71 /* see 802.11 17.3.8.3.2 and Annex J 72 * there are overlapping channel numbers in 5GHz and 2GHz bands */ 73 if (chan <= 0) 74 return 0; /* not supported */ 75 switch (band) { 76 case NL80211_BAND_2GHZ: 77 case NL80211_BAND_LC: 78 if (chan == 14) 79 return MHZ_TO_KHZ(2484); 80 else if (chan < 14) 81 return MHZ_TO_KHZ(2407 + chan * 5); 82 break; 83 case NL80211_BAND_5GHZ: 84 if (chan >= 182 && chan <= 196) 85 return MHZ_TO_KHZ(4000 + chan * 5); 86 else 87 return MHZ_TO_KHZ(5000 + chan * 5); 88 break; 89 case NL80211_BAND_6GHZ: 90 /* see 802.11ax D6.1 27.3.23.2 */ 91 if (chan == 2) 92 return MHZ_TO_KHZ(5935); 93 if (chan <= 253) 94 return MHZ_TO_KHZ(5950 + chan * 5); 95 break; 96 case NL80211_BAND_60GHZ: 97 if (chan < 7) 98 return MHZ_TO_KHZ(56160 + chan * 2160); 99 break; 100 case NL80211_BAND_S1GHZ: 101 return 902000 + chan * 500; 102 default: 103 ; 104 } 105 return 0; /* not supported */ 106 } 107 EXPORT_SYMBOL(ieee80211_channel_to_freq_khz); 108 109 int ieee80211_freq_khz_to_channel(u32 freq) 110 { 111 /* TODO: just handle MHz for now */ 112 freq = KHZ_TO_MHZ(freq); 113 114 /* see 802.11 17.3.8.3.2 and Annex J */ 115 if (freq == 2484) 116 return 14; 117 else if (freq < 2484) 118 return (freq - 2407) / 5; 119 else if (freq >= 4910 && freq <= 4980) 120 return (freq - 4000) / 5; 121 else if (freq < 5925) 122 return (freq - 5000) / 5; 123 else if (freq == 5935) 124 return 2; 125 else if (freq <= 45000) /* DMG band lower limit */ 126 /* see 802.11ax D6.1 27.3.22.2 */ 127 return (freq - 5950) / 5; 128 else if (freq >= 58320 && freq <= 70200) 129 return (freq - 56160) / 2160; 130 else 131 return 0; 132 } 133 EXPORT_SYMBOL(ieee80211_freq_khz_to_channel); 134 135 struct ieee80211_channel *ieee80211_get_channel_khz(struct wiphy *wiphy, 136 u32 freq) 137 { 138 enum nl80211_band band; 139 struct ieee80211_supported_band *sband; 140 int i; 141 142 for (band = 0; band < NUM_NL80211_BANDS; band++) { 143 sband = wiphy->bands[band]; 144 145 if (!sband) 146 continue; 147 148 for (i = 0; i < sband->n_channels; i++) { 149 struct ieee80211_channel *chan = &sband->channels[i]; 150 151 if (ieee80211_channel_to_khz(chan) == freq) 152 return chan; 153 } 154 } 155 156 return NULL; 157 } 158 EXPORT_SYMBOL(ieee80211_get_channel_khz); 159 160 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband) 161 { 162 int i, want; 163 164 switch (sband->band) { 165 case NL80211_BAND_5GHZ: 166 case NL80211_BAND_6GHZ: 167 want = 3; 168 for (i = 0; i < sband->n_bitrates; i++) { 169 if (sband->bitrates[i].bitrate == 60 || 170 sband->bitrates[i].bitrate == 120 || 171 sband->bitrates[i].bitrate == 240) { 172 sband->bitrates[i].flags |= 173 IEEE80211_RATE_MANDATORY_A; 174 want--; 175 } 176 } 177 WARN_ON(want); 178 break; 179 case NL80211_BAND_2GHZ: 180 case NL80211_BAND_LC: 181 want = 7; 182 for (i = 0; i < sband->n_bitrates; i++) { 183 switch (sband->bitrates[i].bitrate) { 184 case 10: 185 case 20: 186 case 55: 187 case 110: 188 sband->bitrates[i].flags |= 189 IEEE80211_RATE_MANDATORY_B | 190 IEEE80211_RATE_MANDATORY_G; 191 want--; 192 break; 193 case 60: 194 case 120: 195 case 240: 196 sband->bitrates[i].flags |= 197 IEEE80211_RATE_MANDATORY_G; 198 want--; 199 fallthrough; 200 default: 201 sband->bitrates[i].flags |= 202 IEEE80211_RATE_ERP_G; 203 break; 204 } 205 } 206 WARN_ON(want != 0 && want != 3); 207 break; 208 case NL80211_BAND_60GHZ: 209 /* check for mandatory HT MCS 1..4 */ 210 WARN_ON(!sband->ht_cap.ht_supported); 211 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e); 212 break; 213 case NL80211_BAND_S1GHZ: 214 /* Figure 9-589bd: 3 means unsupported, so != 3 means at least 215 * mandatory is ok. 216 */ 217 WARN_ON((sband->s1g_cap.nss_mcs[0] & 0x3) == 0x3); 218 break; 219 case NUM_NL80211_BANDS: 220 default: 221 WARN_ON(1); 222 break; 223 } 224 } 225 226 void ieee80211_set_bitrate_flags(struct wiphy *wiphy) 227 { 228 enum nl80211_band band; 229 230 for (band = 0; band < NUM_NL80211_BANDS; band++) 231 if (wiphy->bands[band]) 232 set_mandatory_flags_band(wiphy->bands[band]); 233 } 234 235 bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher) 236 { 237 int i; 238 for (i = 0; i < wiphy->n_cipher_suites; i++) 239 if (cipher == wiphy->cipher_suites[i]) 240 return true; 241 return false; 242 } 243 244 static bool cfg80211_igtk_cipher_supported(struct wiphy *wiphy) 245 { 246 int i; 247 248 for (i = 0; i < wiphy->n_cipher_suites; i++) { 249 switch (wiphy->cipher_suites[i]) { 250 case WLAN_CIPHER_SUITE_AES_CMAC: 251 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 252 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 253 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 254 return true; 255 } 256 } 257 258 return false; 259 } 260 261 bool cfg80211_valid_key_idx(struct wireless_dev *wdev, 262 int key_idx, bool pairwise, 263 const u8 *mac_addr) 264 { 265 if (WARN_ON(!wdev)) 266 return false; 267 268 if (key_idx < 0) 269 return false; 270 271 /* 272 * Can't differentiate ciphers here so allow 0..3. 273 * Pairwise keys must be for a station (MAC address given). 274 */ 275 if (pairwise) { 276 if (!mac_addr) 277 return false; 278 279 return key_idx < 4; 280 } 281 282 /* 283 * For group keys, mac_addr==NULL means setting a group key 284 * for TX, which is only supported on some interface types, 285 * except for STATION/P2P_CLIENT, where it's setting the RX 286 * key with the current AP (for legacy reasons.) 287 * 288 * Apart from that exception, a non-NULL mac_addr means RX 289 * key being set. 290 */ 291 292 switch (wdev->iftype) { 293 case NL80211_IFTYPE_ADHOC: 294 if (!(wdev->wiphy->flags & WIPHY_FLAG_IBSS_RSN)) 295 return false; 296 fallthrough; 297 case NL80211_IFTYPE_MESH_POINT: 298 /* no support for IGTK/BIGTK (yet?) */ 299 return key_idx < 4; 300 case NL80211_IFTYPE_NAN_DATA: 301 /* these always need to support per-STA GTK */ 302 return key_idx < 4; 303 case NL80211_IFTYPE_NAN: 304 /* no data */ 305 if (key_idx < 4) 306 return false; 307 /* NAN reused this flag */ 308 if (wiphy_ext_feature_isset(wdev->wiphy, 309 NL80211_EXT_FEATURE_BEACON_PROTECTION)) 310 return key_idx <= 7; 311 return key_idx <= 5; 312 case NL80211_IFTYPE_STATION: 313 case NL80211_IFTYPE_P2P_CLIENT: 314 /* see note about exception above */ 315 if (mac_addr) 316 return false; 317 /* BIGTK support implies IGTK support */ 318 if (wiphy_ext_feature_isset(wdev->wiphy, 319 NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT)) 320 return key_idx <= 7; 321 fallthrough; 322 case NL80211_IFTYPE_AP: 323 case NL80211_IFTYPE_P2P_GO: 324 /* no RX with [B]IGTK */ 325 if (mac_addr) 326 return false; 327 if (wiphy_ext_feature_isset(wdev->wiphy, 328 NL80211_EXT_FEATURE_BEACON_PROTECTION)) 329 return key_idx <= 7; 330 fallthrough; 331 case NL80211_IFTYPE_AP_VLAN: 332 /* no RX with GTK */ 333 if (mac_addr) 334 return false; 335 if (cfg80211_igtk_cipher_supported(wdev->wiphy)) 336 return key_idx <= 5; 337 return key_idx <= 3; 338 default: 339 return false; 340 } 341 } 342 343 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev, 344 struct wireless_dev *wdev, 345 struct key_params *params, int key_idx, 346 bool pairwise, const u8 *mac_addr) 347 { 348 if (!cfg80211_valid_key_idx(wdev, key_idx, pairwise, mac_addr)) 349 return -EINVAL; 350 351 switch (params->cipher) { 352 case WLAN_CIPHER_SUITE_TKIP: 353 /* Extended Key ID can only be used with CCMP/GCMP ciphers */ 354 if ((pairwise && key_idx) || 355 params->mode != NL80211_KEY_RX_TX) 356 return -EINVAL; 357 break; 358 case WLAN_CIPHER_SUITE_CCMP: 359 case WLAN_CIPHER_SUITE_CCMP_256: 360 case WLAN_CIPHER_SUITE_GCMP: 361 case WLAN_CIPHER_SUITE_GCMP_256: 362 /* IEEE802.11-2016 allows only 0 and - when supporting 363 * Extended Key ID - 1 as index for pairwise keys. 364 * @NL80211_KEY_NO_TX is only allowed for pairwise keys when 365 * the driver supports Extended Key ID. 366 * @NL80211_KEY_SET_TX can't be set when installing and 367 * validating a key. 368 */ 369 if ((params->mode == NL80211_KEY_NO_TX && !pairwise) || 370 params->mode == NL80211_KEY_SET_TX) 371 return -EINVAL; 372 if (wiphy_ext_feature_isset(&rdev->wiphy, 373 NL80211_EXT_FEATURE_EXT_KEY_ID)) { 374 if (pairwise && (key_idx < 0 || key_idx > 1)) 375 return -EINVAL; 376 } else if (pairwise && key_idx) { 377 return -EINVAL; 378 } 379 break; 380 case WLAN_CIPHER_SUITE_AES_CMAC: 381 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 382 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 383 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 384 /* Disallow BIP (group-only) cipher as pairwise cipher */ 385 if (pairwise) 386 return -EINVAL; 387 if (key_idx < 4) 388 return -EINVAL; 389 break; 390 case WLAN_CIPHER_SUITE_WEP40: 391 case WLAN_CIPHER_SUITE_WEP104: 392 if (key_idx > 3) 393 return -EINVAL; 394 break; 395 default: 396 break; 397 } 398 399 /* 400 * Per Wi-Fi Aware v4.0 section 7.1.2, NAN Data interfaces 401 * shall only use CCMP-128 or GCMP-256. 402 */ 403 if (wdev->iftype == NL80211_IFTYPE_NAN_DATA && 404 params->cipher != WLAN_CIPHER_SUITE_CCMP && 405 params->cipher != WLAN_CIPHER_SUITE_GCMP_256) 406 return -EINVAL; 407 408 switch (params->cipher) { 409 case WLAN_CIPHER_SUITE_WEP40: 410 if (params->key_len != WLAN_KEY_LEN_WEP40) 411 return -EINVAL; 412 break; 413 case WLAN_CIPHER_SUITE_TKIP: 414 if (params->key_len != WLAN_KEY_LEN_TKIP) 415 return -EINVAL; 416 break; 417 case WLAN_CIPHER_SUITE_CCMP: 418 if (params->key_len != WLAN_KEY_LEN_CCMP) 419 return -EINVAL; 420 break; 421 case WLAN_CIPHER_SUITE_CCMP_256: 422 if (params->key_len != WLAN_KEY_LEN_CCMP_256) 423 return -EINVAL; 424 break; 425 case WLAN_CIPHER_SUITE_GCMP: 426 if (params->key_len != WLAN_KEY_LEN_GCMP) 427 return -EINVAL; 428 break; 429 case WLAN_CIPHER_SUITE_GCMP_256: 430 if (params->key_len != WLAN_KEY_LEN_GCMP_256) 431 return -EINVAL; 432 break; 433 case WLAN_CIPHER_SUITE_WEP104: 434 if (params->key_len != WLAN_KEY_LEN_WEP104) 435 return -EINVAL; 436 break; 437 case WLAN_CIPHER_SUITE_AES_CMAC: 438 if (params->key_len != WLAN_KEY_LEN_AES_CMAC) 439 return -EINVAL; 440 break; 441 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 442 if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256) 443 return -EINVAL; 444 break; 445 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 446 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128) 447 return -EINVAL; 448 break; 449 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 450 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256) 451 return -EINVAL; 452 break; 453 default: 454 /* 455 * We don't know anything about this algorithm, 456 * allow using it -- but the driver must check 457 * all parameters! We still check below whether 458 * or not the driver supports this algorithm, 459 * of course. 460 */ 461 break; 462 } 463 464 if (params->seq) { 465 switch (params->cipher) { 466 case WLAN_CIPHER_SUITE_WEP40: 467 case WLAN_CIPHER_SUITE_WEP104: 468 /* These ciphers do not use key sequence */ 469 return -EINVAL; 470 case WLAN_CIPHER_SUITE_TKIP: 471 case WLAN_CIPHER_SUITE_CCMP: 472 case WLAN_CIPHER_SUITE_CCMP_256: 473 case WLAN_CIPHER_SUITE_GCMP: 474 case WLAN_CIPHER_SUITE_GCMP_256: 475 case WLAN_CIPHER_SUITE_AES_CMAC: 476 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 477 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 478 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 479 if (params->seq_len != 6) 480 return -EINVAL; 481 break; 482 } 483 } 484 485 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher)) 486 return -EINVAL; 487 488 if (params->ltf_keyseed) { 489 if (!wiphy_ext_feature_isset(&rdev->wiphy, 490 NL80211_EXT_FEATURE_SECURE_LTF) || 491 !wiphy_ext_feature_isset(&rdev->wiphy, 492 NL80211_EXT_FEATURE_SET_KEY_LTF_SEED)) 493 return -EOPNOTSUPP; 494 495 /* 496 * LTF key seed is pairwise key material and must only be 497 * used with a pairwise key 498 */ 499 if (!pairwise) 500 return -EINVAL; 501 } 502 503 return 0; 504 } 505 506 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc) 507 { 508 unsigned int hdrlen = 24; 509 510 if (ieee80211_is_ext(fc)) { 511 hdrlen = 4; 512 goto out; 513 } 514 515 if (ieee80211_is_data(fc)) { 516 if (ieee80211_has_a4(fc)) 517 hdrlen = 30; 518 if (ieee80211_is_data_qos(fc)) { 519 hdrlen += IEEE80211_QOS_CTL_LEN; 520 if (ieee80211_has_order(fc)) 521 hdrlen += IEEE80211_HT_CTL_LEN; 522 } 523 goto out; 524 } 525 526 if (ieee80211_is_mgmt(fc)) { 527 if (ieee80211_has_order(fc)) 528 hdrlen += IEEE80211_HT_CTL_LEN; 529 goto out; 530 } 531 532 if (ieee80211_is_ctl(fc)) { 533 /* 534 * ACK and CTS are 10 bytes, all others 16. To see how 535 * to get this condition consider 536 * subtype mask: 0b0000000011110000 (0x00F0) 537 * ACK subtype: 0b0000000011010000 (0x00D0) 538 * CTS subtype: 0b0000000011000000 (0x00C0) 539 * bits that matter: ^^^ (0x00E0) 540 * value of those: 0b0000000011000000 (0x00C0) 541 */ 542 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0)) 543 hdrlen = 10; 544 else 545 hdrlen = 16; 546 } 547 out: 548 return hdrlen; 549 } 550 EXPORT_SYMBOL(ieee80211_hdrlen); 551 552 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb) 553 { 554 const struct ieee80211_hdr *hdr = 555 (const struct ieee80211_hdr *)skb->data; 556 unsigned int hdrlen; 557 558 if (unlikely(skb->len < 10)) 559 return 0; 560 hdrlen = ieee80211_hdrlen(hdr->frame_control); 561 if (unlikely(hdrlen > skb->len)) 562 return 0; 563 return hdrlen; 564 } 565 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb); 566 567 static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags) 568 { 569 int ae = flags & MESH_FLAGS_AE; 570 /* 802.11-2012, 8.2.4.7.3 */ 571 switch (ae) { 572 default: 573 case 0: 574 return 6; 575 case MESH_FLAGS_AE_A4: 576 return 12; 577 case MESH_FLAGS_AE_A5_A6: 578 return 18; 579 } 580 } 581 582 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr) 583 { 584 return __ieee80211_get_mesh_hdrlen(meshhdr->flags); 585 } 586 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen); 587 588 bool ieee80211_get_8023_tunnel_proto(const void *hdr, __be16 *proto) 589 { 590 const __be16 *hdr_proto = hdr + ETH_ALEN; 591 592 if (!(ether_addr_equal(hdr, rfc1042_header) && 593 *hdr_proto != htons(ETH_P_AARP) && 594 *hdr_proto != htons(ETH_P_IPX)) && 595 !ether_addr_equal(hdr, bridge_tunnel_header)) 596 return false; 597 598 *proto = *hdr_proto; 599 600 return true; 601 } 602 EXPORT_SYMBOL(ieee80211_get_8023_tunnel_proto); 603 604 int ieee80211_strip_8023_mesh_hdr(struct sk_buff *skb) 605 { 606 const void *mesh_addr; 607 struct { 608 struct ethhdr eth; 609 u8 flags; 610 } payload; 611 int hdrlen; 612 int ret; 613 614 ret = skb_copy_bits(skb, 0, &payload, sizeof(payload)); 615 if (ret) 616 return ret; 617 618 hdrlen = sizeof(payload.eth) + __ieee80211_get_mesh_hdrlen(payload.flags); 619 620 if (likely(pskb_may_pull(skb, hdrlen + 8) && 621 ieee80211_get_8023_tunnel_proto(skb->data + hdrlen, 622 &payload.eth.h_proto))) 623 hdrlen += ETH_ALEN + 2; 624 else if (!pskb_may_pull(skb, hdrlen)) 625 return -EINVAL; 626 else 627 payload.eth.h_proto = htons(skb->len - hdrlen); 628 629 mesh_addr = skb->data + sizeof(payload.eth) + ETH_ALEN; 630 switch (payload.flags & MESH_FLAGS_AE) { 631 case MESH_FLAGS_AE_A4: 632 memcpy(&payload.eth.h_source, mesh_addr, ETH_ALEN); 633 break; 634 case MESH_FLAGS_AE_A5_A6: 635 memcpy(&payload.eth, mesh_addr, 2 * ETH_ALEN); 636 break; 637 default: 638 break; 639 } 640 641 pskb_pull(skb, hdrlen - sizeof(payload.eth)); 642 memcpy(skb->data, &payload.eth, sizeof(payload.eth)); 643 644 return 0; 645 } 646 EXPORT_SYMBOL(ieee80211_strip_8023_mesh_hdr); 647 648 int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr, 649 const u8 *addr, enum nl80211_iftype iftype, 650 u8 data_offset, bool is_amsdu) 651 { 652 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; 653 struct { 654 u8 hdr[ETH_ALEN] __aligned(2); 655 __be16 proto; 656 } payload; 657 struct ethhdr tmp; 658 u16 hdrlen; 659 660 if (unlikely(!ieee80211_is_data_present(hdr->frame_control))) 661 return -1; 662 663 hdrlen = ieee80211_hdrlen(hdr->frame_control) + data_offset; 664 if (skb->len < hdrlen) 665 return -1; 666 667 /* convert IEEE 802.11 header + possible LLC headers into Ethernet 668 * header 669 * IEEE 802.11 address fields: 670 * ToDS FromDS Addr1 Addr2 Addr3 Addr4 671 * 0 0 DA SA BSSID n/a 672 * 0 1 DA BSSID SA n/a 673 * 1 0 BSSID SA DA n/a 674 * 1 1 RA TA DA SA 675 */ 676 memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN); 677 memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN); 678 679 switch (hdr->frame_control & 680 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) { 681 case cpu_to_le16(IEEE80211_FCTL_TODS): 682 if (unlikely(iftype != NL80211_IFTYPE_AP && 683 iftype != NL80211_IFTYPE_AP_VLAN && 684 iftype != NL80211_IFTYPE_P2P_GO)) 685 return -1; 686 break; 687 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS): 688 if (unlikely(iftype != NL80211_IFTYPE_MESH_POINT && 689 iftype != NL80211_IFTYPE_AP_VLAN && 690 iftype != NL80211_IFTYPE_STATION)) 691 return -1; 692 break; 693 case cpu_to_le16(IEEE80211_FCTL_FROMDS): 694 if ((iftype != NL80211_IFTYPE_STATION && 695 iftype != NL80211_IFTYPE_P2P_CLIENT && 696 iftype != NL80211_IFTYPE_MESH_POINT) || 697 (is_multicast_ether_addr(tmp.h_dest) && 698 ether_addr_equal(tmp.h_source, addr))) 699 return -1; 700 break; 701 case cpu_to_le16(0): 702 if (iftype != NL80211_IFTYPE_ADHOC && 703 iftype != NL80211_IFTYPE_STATION && 704 iftype != NL80211_IFTYPE_OCB && 705 iftype != NL80211_IFTYPE_NAN_DATA) 706 return -1; 707 break; 708 } 709 710 if (likely(!is_amsdu && iftype != NL80211_IFTYPE_MESH_POINT && 711 skb_copy_bits(skb, hdrlen, &payload, sizeof(payload)) == 0 && 712 ieee80211_get_8023_tunnel_proto(&payload, &tmp.h_proto))) { 713 /* remove RFC1042 or Bridge-Tunnel encapsulation */ 714 hdrlen += ETH_ALEN + 2; 715 skb_postpull_rcsum(skb, &payload, ETH_ALEN + 2); 716 } else { 717 tmp.h_proto = htons(skb->len - hdrlen); 718 } 719 720 pskb_pull(skb, hdrlen); 721 722 if (!ehdr) 723 ehdr = skb_push(skb, sizeof(struct ethhdr)); 724 memcpy(ehdr, &tmp, sizeof(tmp)); 725 726 return 0; 727 } 728 EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr); 729 730 static void 731 __frame_add_frag(struct sk_buff *skb, struct page *page, 732 void *ptr, int len, int size) 733 { 734 struct skb_shared_info *sh = skb_shinfo(skb); 735 int page_offset; 736 737 get_page(page); 738 page_offset = ptr - page_address(page); 739 skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size); 740 } 741 742 static void 743 __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame, 744 int offset, int len) 745 { 746 struct skb_shared_info *sh = skb_shinfo(skb); 747 const skb_frag_t *frag = &sh->frags[0]; 748 struct page *frag_page; 749 void *frag_ptr; 750 int frag_len, frag_size; 751 int head_size = skb->len - skb->data_len; 752 int cur_len; 753 754 frag_page = virt_to_head_page(skb->head); 755 frag_ptr = skb->data; 756 frag_size = head_size; 757 758 while (offset >= frag_size) { 759 offset -= frag_size; 760 frag_page = skb_frag_page(frag); 761 frag_ptr = skb_frag_address(frag); 762 frag_size = skb_frag_size(frag); 763 frag++; 764 } 765 766 frag_ptr += offset; 767 frag_len = frag_size - offset; 768 769 cur_len = min(len, frag_len); 770 771 __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size); 772 len -= cur_len; 773 774 while (len > 0) { 775 frag_len = skb_frag_size(frag); 776 cur_len = min(len, frag_len); 777 __frame_add_frag(frame, skb_frag_page(frag), 778 skb_frag_address(frag), cur_len, frag_len); 779 len -= cur_len; 780 frag++; 781 } 782 } 783 784 static struct sk_buff * 785 __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen, 786 int offset, int len, bool reuse_frag, 787 int min_len) 788 { 789 struct sk_buff *frame; 790 int cur_len = len; 791 792 if (skb->len - offset < len) 793 return NULL; 794 795 /* 796 * When reusing fragments, copy some data to the head to simplify 797 * ethernet header handling and speed up protocol header processing 798 * in the stack later. 799 */ 800 if (reuse_frag) 801 cur_len = min_t(int, len, min_len); 802 803 /* 804 * Allocate and reserve two bytes more for payload 805 * alignment since sizeof(struct ethhdr) is 14. 806 */ 807 frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len); 808 if (!frame) 809 return NULL; 810 811 frame->priority = skb->priority; 812 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2); 813 skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len); 814 815 len -= cur_len; 816 if (!len) 817 return frame; 818 819 offset += cur_len; 820 __ieee80211_amsdu_copy_frag(skb, frame, offset, len); 821 822 return frame; 823 } 824 825 static u16 826 ieee80211_amsdu_subframe_length(void *field, u8 mesh_flags, u8 hdr_type) 827 { 828 __le16 *field_le = field; 829 __be16 *field_be = field; 830 u16 len; 831 832 if (hdr_type >= 2) 833 len = le16_to_cpu(*field_le); 834 else 835 len = be16_to_cpu(*field_be); 836 if (hdr_type) 837 len += __ieee80211_get_mesh_hdrlen(mesh_flags); 838 839 return len; 840 } 841 842 bool ieee80211_is_valid_amsdu(struct sk_buff *skb, u8 mesh_hdr) 843 { 844 int offset = 0, subframe_len, padding; 845 846 for (offset = 0; offset < skb->len; offset += subframe_len + padding) { 847 int remaining = skb->len - offset; 848 struct { 849 __be16 len; 850 u8 mesh_flags; 851 } hdr; 852 u16 len; 853 854 if (sizeof(hdr) > remaining) 855 return false; 856 857 if (skb_copy_bits(skb, offset + 2 * ETH_ALEN, &hdr, sizeof(hdr)) < 0) 858 return false; 859 860 len = ieee80211_amsdu_subframe_length(&hdr.len, hdr.mesh_flags, 861 mesh_hdr); 862 subframe_len = sizeof(struct ethhdr) + len; 863 padding = (4 - subframe_len) & 0x3; 864 865 if (subframe_len > remaining) 866 return false; 867 } 868 869 return true; 870 } 871 EXPORT_SYMBOL(ieee80211_is_valid_amsdu); 872 873 874 /* 875 * Detects if an MSDU frame was maliciously converted into an A-MSDU 876 * frame by an adversary. This is done by parsing the received frame 877 * as if it were a regular MSDU, even though the A-MSDU flag is set. 878 * 879 * For non-mesh interfaces, detection involves checking whether the 880 * payload, when interpreted as an MSDU, begins with a valid RFC1042 881 * header. This is done by comparing the A-MSDU subheader's destination 882 * address to the start of the RFC1042 header. 883 * 884 * For mesh interfaces, the MSDU includes a 6-byte Mesh Control field 885 * and an optional variable-length Mesh Address Extension field before 886 * the RFC1042 header. The position of the RFC1042 header must therefore 887 * be calculated based on the mesh header length. 888 * 889 * Since this function intentionally parses an A-MSDU frame as an MSDU, 890 * it only assumes that the A-MSDU subframe header is present, and 891 * beyond this it performs its own bounds checks under the assumption 892 * that the frame is instead parsed as a non-aggregated MSDU. 893 */ 894 static bool 895 is_amsdu_aggregation_attack(struct ethhdr *eth, struct sk_buff *skb, 896 enum nl80211_iftype iftype) 897 { 898 int offset; 899 900 /* Non-mesh case can be directly compared */ 901 if (iftype != NL80211_IFTYPE_MESH_POINT) 902 return ether_addr_equal(eth->h_dest, rfc1042_header); 903 904 offset = __ieee80211_get_mesh_hdrlen(eth->h_dest[0]); 905 if (offset == 6) { 906 /* Mesh case with empty address extension field */ 907 return ether_addr_equal(eth->h_source, rfc1042_header); 908 } else if (offset + ETH_ALEN <= skb->len) { 909 /* Mesh case with non-empty address extension field */ 910 u8 temp[ETH_ALEN]; 911 912 skb_copy_bits(skb, offset, temp, ETH_ALEN); 913 return ether_addr_equal(temp, rfc1042_header); 914 } 915 916 return false; 917 } 918 919 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list, 920 const u8 *addr, enum nl80211_iftype iftype, 921 const unsigned int extra_headroom, 922 const u8 *check_da, const u8 *check_sa, 923 u8 mesh_control) 924 { 925 unsigned int hlen = ALIGN(extra_headroom, 4); 926 struct sk_buff *frame = NULL; 927 int offset = 0; 928 struct { 929 struct ethhdr eth; 930 uint8_t flags; 931 } hdr; 932 bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb); 933 bool reuse_skb = false; 934 bool last = false; 935 int copy_len = sizeof(hdr.eth); 936 937 if (iftype == NL80211_IFTYPE_MESH_POINT) 938 copy_len = sizeof(hdr); 939 940 while (!last) { 941 int remaining = skb->len - offset; 942 unsigned int subframe_len; 943 int len, mesh_len = 0; 944 u8 padding; 945 946 if (copy_len > remaining) 947 goto purge; 948 949 skb_copy_bits(skb, offset, &hdr, copy_len); 950 if (iftype == NL80211_IFTYPE_MESH_POINT) 951 mesh_len = __ieee80211_get_mesh_hdrlen(hdr.flags); 952 len = ieee80211_amsdu_subframe_length(&hdr.eth.h_proto, hdr.flags, 953 mesh_control); 954 subframe_len = sizeof(struct ethhdr) + len; 955 padding = (4 - subframe_len) & 0x3; 956 957 /* the last MSDU has no padding */ 958 if (subframe_len > remaining) 959 goto purge; 960 /* mitigate A-MSDU aggregation injection attacks, to be 961 * checked when processing first subframe (offset == 0). 962 */ 963 if (offset == 0 && is_amsdu_aggregation_attack(&hdr.eth, skb, iftype)) 964 goto purge; 965 966 offset += sizeof(struct ethhdr); 967 last = remaining <= subframe_len + padding; 968 969 /* FIXME: should we really accept multicast DA? */ 970 if ((check_da && !is_multicast_ether_addr(hdr.eth.h_dest) && 971 !ether_addr_equal(check_da, hdr.eth.h_dest)) || 972 (check_sa && !ether_addr_equal(check_sa, hdr.eth.h_source))) { 973 offset += len + padding; 974 continue; 975 } 976 977 /* reuse skb for the last subframe */ 978 if (!skb_is_nonlinear(skb) && !reuse_frag && last) { 979 skb_pull(skb, offset); 980 frame = skb; 981 reuse_skb = true; 982 } else { 983 frame = __ieee80211_amsdu_copy(skb, hlen, offset, len, 984 reuse_frag, 32 + mesh_len); 985 if (!frame) 986 goto purge; 987 988 offset += len + padding; 989 } 990 991 skb_reset_network_header(frame); 992 frame->dev = skb->dev; 993 frame->priority = skb->priority; 994 995 if (likely(iftype != NL80211_IFTYPE_MESH_POINT && 996 ieee80211_get_8023_tunnel_proto(frame->data, &hdr.eth.h_proto))) 997 skb_pull(frame, ETH_ALEN + 2); 998 999 memcpy(skb_push(frame, sizeof(hdr.eth)), &hdr.eth, sizeof(hdr.eth)); 1000 __skb_queue_tail(list, frame); 1001 } 1002 1003 if (!reuse_skb) 1004 dev_kfree_skb(skb); 1005 1006 return; 1007 1008 purge: 1009 __skb_queue_purge(list); 1010 dev_kfree_skb(skb); 1011 } 1012 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s); 1013 1014 /* Given a data frame determine the 802.1p/1d tag to use. */ 1015 unsigned int cfg80211_classify8021d(struct sk_buff *skb, 1016 struct cfg80211_qos_map *qos_map) 1017 { 1018 unsigned int dscp; 1019 unsigned char vlan_priority; 1020 unsigned int ret; 1021 1022 /* skb->priority values from 256->263 are magic values to 1023 * directly indicate a specific 802.1d priority. This is used 1024 * to allow 802.1d priority to be passed directly in from VLAN 1025 * tags, etc. 1026 */ 1027 if (skb->priority >= 256 && skb->priority <= 263) { 1028 ret = skb->priority - 256; 1029 goto out; 1030 } 1031 1032 if (skb_vlan_tag_present(skb)) { 1033 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK) 1034 >> VLAN_PRIO_SHIFT; 1035 if (vlan_priority > 0) { 1036 ret = vlan_priority; 1037 goto out; 1038 } 1039 } 1040 1041 switch (skb->protocol) { 1042 case htons(ETH_P_IP): { 1043 const struct iphdr *iph; 1044 struct iphdr _iph; 1045 1046 iph = skb_header_pointer(skb, sizeof(struct ethhdr), 1047 sizeof(*iph), &_iph); 1048 if (!iph) 1049 return 0; 1050 1051 dscp = ipv4_get_dsfield(iph) & 0xfc; 1052 break; 1053 } 1054 case htons(ETH_P_IPV6): { 1055 const struct ipv6hdr *ip6h; 1056 struct ipv6hdr _ip6h; 1057 1058 ip6h = skb_header_pointer(skb, sizeof(struct ethhdr), 1059 sizeof(*ip6h), &_ip6h); 1060 if (!ip6h) 1061 return 0; 1062 1063 dscp = ipv6_get_dsfield(ip6h) & 0xfc; 1064 break; 1065 } 1066 case htons(ETH_P_MPLS_UC): 1067 case htons(ETH_P_MPLS_MC): { 1068 struct mpls_label mpls_tmp, *mpls; 1069 1070 mpls = skb_header_pointer(skb, sizeof(struct ethhdr), 1071 sizeof(*mpls), &mpls_tmp); 1072 if (!mpls) 1073 return 0; 1074 1075 ret = (ntohl(mpls->entry) & MPLS_LS_TC_MASK) 1076 >> MPLS_LS_TC_SHIFT; 1077 goto out; 1078 } 1079 case htons(ETH_P_80221): 1080 /* 802.21 is always network control traffic */ 1081 return 7; 1082 default: 1083 return 0; 1084 } 1085 1086 if (qos_map) { 1087 unsigned int i, tmp_dscp = dscp >> 2; 1088 1089 for (i = 0; i < qos_map->num_des; i++) { 1090 if (tmp_dscp == qos_map->dscp_exception[i].dscp) { 1091 ret = qos_map->dscp_exception[i].up; 1092 goto out; 1093 } 1094 } 1095 1096 for (i = 0; i < 8; i++) { 1097 if (tmp_dscp >= qos_map->up[i].low && 1098 tmp_dscp <= qos_map->up[i].high) { 1099 ret = i; 1100 goto out; 1101 } 1102 } 1103 } 1104 1105 /* The default mapping as defined Section 2.3 in RFC8325: The three 1106 * Most Significant Bits (MSBs) of the DSCP are used as the 1107 * corresponding L2 markings. 1108 */ 1109 ret = dscp >> 5; 1110 1111 /* Handle specific DSCP values for which the default mapping (as 1112 * described above) doesn't adhere to the intended usage of the DSCP 1113 * value. See section 4 in RFC8325. Specifically, for the following 1114 * Diffserv Service Classes no update is needed: 1115 * - Standard: DF 1116 * - Low Priority Data: CS1 1117 * - Multimedia Conferencing: AF41, AF42, AF43 1118 * - Network Control Traffic: CS7 1119 * - Real-Time Interactive: CS4 1120 * - Signaling: CS5 1121 */ 1122 switch (dscp >> 2) { 1123 case 10: 1124 case 12: 1125 case 14: 1126 /* High throughput data: AF11, AF12, AF13 */ 1127 ret = 0; 1128 break; 1129 case 16: 1130 /* Operations, Administration, and Maintenance and Provisioning: 1131 * CS2 1132 */ 1133 ret = 0; 1134 break; 1135 case 18: 1136 case 20: 1137 case 22: 1138 /* Low latency data: AF21, AF22, AF23 */ 1139 ret = 3; 1140 break; 1141 case 24: 1142 /* Broadcasting video: CS3 */ 1143 ret = 4; 1144 break; 1145 case 26: 1146 case 28: 1147 case 30: 1148 /* Multimedia Streaming: AF31, AF32, AF33 */ 1149 ret = 4; 1150 break; 1151 case 44: 1152 /* Voice Admit: VA */ 1153 ret = 6; 1154 break; 1155 case 46: 1156 /* Telephony traffic: EF */ 1157 ret = 6; 1158 break; 1159 case 48: 1160 /* Network Control Traffic: CS6 */ 1161 ret = 7; 1162 break; 1163 } 1164 out: 1165 return array_index_nospec(ret, IEEE80211_NUM_TIDS); 1166 } 1167 EXPORT_SYMBOL(cfg80211_classify8021d); 1168 1169 const struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id) 1170 { 1171 const struct cfg80211_bss_ies *ies; 1172 1173 ies = rcu_dereference(bss->ies); 1174 if (!ies) 1175 return NULL; 1176 1177 return cfg80211_find_elem(id, ies->data, ies->len); 1178 } 1179 EXPORT_SYMBOL(ieee80211_bss_get_elem); 1180 1181 void cfg80211_upload_connect_keys(struct wireless_dev *wdev) 1182 { 1183 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 1184 struct net_device *dev = wdev->netdev; 1185 int i; 1186 1187 if (!wdev->connect_keys) 1188 return; 1189 1190 for (i = 0; i < 4; i++) { 1191 if (!wdev->connect_keys->params[i].cipher) 1192 continue; 1193 if (rdev_add_key(rdev, wdev, -1, i, false, NULL, 1194 &wdev->connect_keys->params[i])) { 1195 netdev_err(dev, "failed to set key %d\n", i); 1196 continue; 1197 } 1198 if (wdev->connect_keys->def == i && 1199 rdev_set_default_key(rdev, dev, -1, i, true, true)) { 1200 netdev_err(dev, "failed to set defkey %d\n", i); 1201 continue; 1202 } 1203 } 1204 1205 kfree_sensitive(wdev->connect_keys); 1206 wdev->connect_keys = NULL; 1207 } 1208 1209 void cfg80211_process_wdev_events(struct wireless_dev *wdev) 1210 { 1211 struct cfg80211_event *ev; 1212 unsigned long flags; 1213 1214 spin_lock_irqsave(&wdev->event_lock, flags); 1215 while (!list_empty(&wdev->event_list)) { 1216 ev = list_first_entry(&wdev->event_list, 1217 struct cfg80211_event, list); 1218 list_del(&ev->list); 1219 spin_unlock_irqrestore(&wdev->event_lock, flags); 1220 1221 switch (ev->type) { 1222 case EVENT_CONNECT_RESULT: 1223 __cfg80211_connect_result( 1224 wdev->netdev, 1225 &ev->cr, 1226 ev->cr.status == WLAN_STATUS_SUCCESS); 1227 break; 1228 case EVENT_ROAMED: 1229 __cfg80211_roamed(wdev, &ev->rm); 1230 break; 1231 case EVENT_DISCONNECTED: 1232 __cfg80211_disconnected(wdev->netdev, 1233 ev->dc.ie, ev->dc.ie_len, 1234 ev->dc.reason, 1235 !ev->dc.locally_generated); 1236 break; 1237 case EVENT_IBSS_JOINED: 1238 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bss); 1239 break; 1240 case EVENT_STOPPED: 1241 /* 1242 * for NAN interfaces cfg80211_leave must be called but 1243 * locking here doesn't allow this. 1244 */ 1245 if (WARN_ON(wdev->iftype == NL80211_IFTYPE_NAN)) 1246 break; 1247 1248 cfg80211_leave_locked(wiphy_to_rdev(wdev->wiphy), wdev, 1249 ev->link_id); 1250 break; 1251 case EVENT_PORT_AUTHORIZED: 1252 __cfg80211_port_authorized(wdev, ev->pa.peer_addr, 1253 ev->pa.td_bitmap, 1254 ev->pa.td_bitmap_len); 1255 break; 1256 } 1257 1258 kfree(ev); 1259 1260 spin_lock_irqsave(&wdev->event_lock, flags); 1261 } 1262 spin_unlock_irqrestore(&wdev->event_lock, flags); 1263 } 1264 1265 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev) 1266 { 1267 struct wireless_dev *wdev; 1268 1269 lockdep_assert_held(&rdev->wiphy.mtx); 1270 1271 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) 1272 cfg80211_process_wdev_events(wdev); 1273 } 1274 1275 int cfg80211_change_iface(struct cfg80211_registered_device *rdev, 1276 struct net_device *dev, enum nl80211_iftype ntype, 1277 struct vif_params *params) 1278 { 1279 int err; 1280 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype; 1281 1282 lockdep_assert_held(&rdev->wiphy.mtx); 1283 1284 /* don't support changing VLANs, you just re-create them */ 1285 if (otype == NL80211_IFTYPE_AP_VLAN) 1286 return -EOPNOTSUPP; 1287 1288 /* 1289 * for NAN interfaces cfg80211_leave must be called for leaving, 1290 * but locking here doesn't allow this. 1291 */ 1292 if (otype == NL80211_IFTYPE_NAN) 1293 return -EOPNOTSUPP; 1294 1295 /* cannot change into P2P device or NAN */ 1296 if (ntype == NL80211_IFTYPE_P2P_DEVICE || 1297 ntype == NL80211_IFTYPE_NAN || 1298 ntype == NL80211_IFTYPE_PD) 1299 return -EOPNOTSUPP; 1300 1301 if (!rdev->ops->change_virtual_intf || 1302 !(rdev->wiphy.interface_modes & (1 << ntype))) 1303 return -EOPNOTSUPP; 1304 1305 if (ntype != otype) { 1306 /* if it's part of a bridge, reject changing type to station/ibss */ 1307 if (netif_is_bridge_port(dev) && 1308 (ntype == NL80211_IFTYPE_ADHOC || 1309 ntype == NL80211_IFTYPE_STATION || 1310 ntype == NL80211_IFTYPE_P2P_CLIENT)) 1311 return -EBUSY; 1312 1313 dev->ieee80211_ptr->use_4addr = false; 1314 rdev_set_qos_map(rdev, dev, NULL); 1315 1316 cfg80211_leave_locked(rdev, dev->ieee80211_ptr, -1); 1317 1318 cfg80211_process_rdev_events(rdev); 1319 cfg80211_mlme_purge_registrations(dev->ieee80211_ptr); 1320 1321 memset(&dev->ieee80211_ptr->u, 0, 1322 sizeof(dev->ieee80211_ptr->u)); 1323 memset(&dev->ieee80211_ptr->links, 0, 1324 sizeof(dev->ieee80211_ptr->links)); 1325 } 1326 1327 err = rdev_change_virtual_intf(rdev, dev, ntype, params); 1328 1329 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype); 1330 1331 if (!err && params && params->use_4addr != -1) 1332 dev->ieee80211_ptr->use_4addr = params->use_4addr; 1333 1334 if (!err) { 1335 dev->priv_flags &= ~IFF_DONT_BRIDGE; 1336 switch (ntype) { 1337 case NL80211_IFTYPE_STATION: 1338 if (dev->ieee80211_ptr->use_4addr) 1339 break; 1340 fallthrough; 1341 case NL80211_IFTYPE_OCB: 1342 case NL80211_IFTYPE_P2P_CLIENT: 1343 case NL80211_IFTYPE_ADHOC: 1344 case NL80211_IFTYPE_NAN_DATA: 1345 dev->priv_flags |= IFF_DONT_BRIDGE; 1346 break; 1347 case NL80211_IFTYPE_P2P_GO: 1348 case NL80211_IFTYPE_AP: 1349 case NL80211_IFTYPE_AP_VLAN: 1350 case NL80211_IFTYPE_MESH_POINT: 1351 /* bridging OK */ 1352 break; 1353 case NL80211_IFTYPE_MONITOR: 1354 /* monitor can't bridge anyway */ 1355 break; 1356 case NL80211_IFTYPE_UNSPECIFIED: 1357 case NUM_NL80211_IFTYPES: 1358 /* not happening */ 1359 break; 1360 case NL80211_IFTYPE_P2P_DEVICE: 1361 case NL80211_IFTYPE_WDS: 1362 case NL80211_IFTYPE_NAN: 1363 case NL80211_IFTYPE_PD: 1364 WARN_ON(1); 1365 break; 1366 } 1367 } 1368 1369 if (!err && ntype != otype && netif_running(dev)) { 1370 cfg80211_update_iface_num(rdev, ntype, 1); 1371 cfg80211_update_iface_num(rdev, otype, -1); 1372 } 1373 1374 return err; 1375 } 1376 1377 static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate) 1378 { 1379 int modulation, streams, bitrate; 1380 1381 /* the formula below does only work for MCS values smaller than 32 */ 1382 if (WARN_ON_ONCE(rate->mcs >= 32)) 1383 return 0; 1384 1385 modulation = rate->mcs & 7; 1386 streams = (rate->mcs >> 3) + 1; 1387 1388 bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000; 1389 1390 if (modulation < 4) 1391 bitrate *= (modulation + 1); 1392 else if (modulation == 4) 1393 bitrate *= (modulation + 2); 1394 else 1395 bitrate *= (modulation + 3); 1396 1397 bitrate *= streams; 1398 1399 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI) 1400 bitrate = (bitrate / 9) * 10; 1401 1402 /* do NOT round down here */ 1403 return (bitrate + 50000) / 100000; 1404 } 1405 1406 static u32 cfg80211_calculate_bitrate_dmg(struct rate_info *rate) 1407 { 1408 static const u32 __mcs2bitrate[] = { 1409 /* control PHY */ 1410 [0] = 275, 1411 /* SC PHY */ 1412 [1] = 3850, 1413 [2] = 7700, 1414 [3] = 9625, 1415 [4] = 11550, 1416 [5] = 12512, /* 1251.25 mbps */ 1417 [6] = 15400, 1418 [7] = 19250, 1419 [8] = 23100, 1420 [9] = 25025, 1421 [10] = 30800, 1422 [11] = 38500, 1423 [12] = 46200, 1424 /* OFDM PHY */ 1425 [13] = 6930, 1426 [14] = 8662, /* 866.25 mbps */ 1427 [15] = 13860, 1428 [16] = 17325, 1429 [17] = 20790, 1430 [18] = 27720, 1431 [19] = 34650, 1432 [20] = 41580, 1433 [21] = 45045, 1434 [22] = 51975, 1435 [23] = 62370, 1436 [24] = 67568, /* 6756.75 mbps */ 1437 /* LP-SC PHY */ 1438 [25] = 6260, 1439 [26] = 8340, 1440 [27] = 11120, 1441 [28] = 12510, 1442 [29] = 16680, 1443 [30] = 22240, 1444 [31] = 25030, 1445 }; 1446 1447 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate))) 1448 return 0; 1449 1450 return __mcs2bitrate[rate->mcs]; 1451 } 1452 1453 static u32 cfg80211_calculate_bitrate_extended_sc_dmg(struct rate_info *rate) 1454 { 1455 static const u32 __mcs2bitrate[] = { 1456 [6 - 6] = 26950, /* MCS 9.1 : 2695.0 mbps */ 1457 [7 - 6] = 50050, /* MCS 12.1 */ 1458 [8 - 6] = 53900, 1459 [9 - 6] = 57750, 1460 [10 - 6] = 63900, 1461 [11 - 6] = 75075, 1462 [12 - 6] = 80850, 1463 }; 1464 1465 /* Extended SC MCS not defined for base MCS below 6 or above 12 */ 1466 if (WARN_ON_ONCE(rate->mcs < 6 || rate->mcs > 12)) 1467 return 0; 1468 1469 return __mcs2bitrate[rate->mcs - 6]; 1470 } 1471 1472 static u32 cfg80211_calculate_bitrate_edmg(struct rate_info *rate) 1473 { 1474 static const u32 __mcs2bitrate[] = { 1475 /* control PHY */ 1476 [0] = 275, 1477 /* SC PHY */ 1478 [1] = 3850, 1479 [2] = 7700, 1480 [3] = 9625, 1481 [4] = 11550, 1482 [5] = 12512, /* 1251.25 mbps */ 1483 [6] = 13475, 1484 [7] = 15400, 1485 [8] = 19250, 1486 [9] = 23100, 1487 [10] = 25025, 1488 [11] = 26950, 1489 [12] = 30800, 1490 [13] = 38500, 1491 [14] = 46200, 1492 [15] = 50050, 1493 [16] = 53900, 1494 [17] = 57750, 1495 [18] = 69300, 1496 [19] = 75075, 1497 [20] = 80850, 1498 }; 1499 1500 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate))) 1501 return 0; 1502 1503 return __mcs2bitrate[rate->mcs] * rate->n_bonded_ch; 1504 } 1505 1506 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate) 1507 { 1508 static const u32 base[4][12] = { 1509 { 6500000, 1510 13000000, 1511 19500000, 1512 26000000, 1513 39000000, 1514 52000000, 1515 58500000, 1516 65000000, 1517 78000000, 1518 /* not in the spec, but some devices use this: */ 1519 86700000, 1520 97500000, 1521 108300000, 1522 }, 1523 { 13500000, 1524 27000000, 1525 40500000, 1526 54000000, 1527 81000000, 1528 108000000, 1529 121500000, 1530 135000000, 1531 162000000, 1532 180000000, 1533 202500000, 1534 225000000, 1535 }, 1536 { 29300000, 1537 58500000, 1538 87800000, 1539 117000000, 1540 175500000, 1541 234000000, 1542 263300000, 1543 292500000, 1544 351000000, 1545 390000000, 1546 438800000, 1547 487500000, 1548 }, 1549 { 58500000, 1550 117000000, 1551 175500000, 1552 234000000, 1553 351000000, 1554 468000000, 1555 526500000, 1556 585000000, 1557 702000000, 1558 780000000, 1559 877500000, 1560 975000000, 1561 }, 1562 }; 1563 u32 bitrate; 1564 int idx; 1565 1566 if (rate->mcs > 11) 1567 goto warn; 1568 1569 switch (rate->bw) { 1570 case RATE_INFO_BW_160: 1571 idx = 3; 1572 break; 1573 case RATE_INFO_BW_80: 1574 idx = 2; 1575 break; 1576 case RATE_INFO_BW_40: 1577 idx = 1; 1578 break; 1579 case RATE_INFO_BW_5: 1580 case RATE_INFO_BW_10: 1581 default: 1582 goto warn; 1583 case RATE_INFO_BW_20: 1584 idx = 0; 1585 } 1586 1587 bitrate = base[idx][rate->mcs]; 1588 bitrate *= rate->nss; 1589 1590 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI) 1591 bitrate = (bitrate / 9) * 10; 1592 1593 /* do NOT round down here */ 1594 return (bitrate + 50000) / 100000; 1595 warn: 1596 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n", 1597 rate->bw, rate->mcs, rate->nss); 1598 return 0; 1599 } 1600 1601 static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate) 1602 { 1603 #define SCALE 6144 1604 u32 mcs_divisors[14] = { 1605 102399, /* 16.666666... */ 1606 51201, /* 8.333333... */ 1607 34134, /* 5.555555... */ 1608 25599, /* 4.166666... */ 1609 17067, /* 2.777777... */ 1610 12801, /* 2.083333... */ 1611 11377, /* 1.851725... */ 1612 10239, /* 1.666666... */ 1613 8532, /* 1.388888... */ 1614 7680, /* 1.250000... */ 1615 6828, /* 1.111111... */ 1616 6144, /* 1.000000... */ 1617 5690, /* 0.926106... */ 1618 5120, /* 0.833333... */ 1619 }; 1620 u32 rates_160M[3] = { 960777777, 907400000, 816666666 }; 1621 u32 rates_996[3] = { 480388888, 453700000, 408333333 }; 1622 u32 rates_484[3] = { 229411111, 216666666, 195000000 }; 1623 u32 rates_242[3] = { 114711111, 108333333, 97500000 }; 1624 u32 rates_106[3] = { 40000000, 37777777, 34000000 }; 1625 u32 rates_52[3] = { 18820000, 17777777, 16000000 }; 1626 u32 rates_26[3] = { 9411111, 8888888, 8000000 }; 1627 u64 tmp; 1628 u32 result; 1629 1630 if (WARN_ON_ONCE(rate->mcs > 13)) 1631 return 0; 1632 1633 if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2)) 1634 return 0; 1635 if (WARN_ON_ONCE(rate->he_ru_alloc > 1636 NL80211_RATE_INFO_HE_RU_ALLOC_2x996)) 1637 return 0; 1638 if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8)) 1639 return 0; 1640 1641 if (rate->bw == RATE_INFO_BW_160 || 1642 (rate->bw == RATE_INFO_BW_HE_RU && 1643 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_2x996)) 1644 result = rates_160M[rate->he_gi]; 1645 else if (rate->bw == RATE_INFO_BW_80 || 1646 (rate->bw == RATE_INFO_BW_HE_RU && 1647 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996)) 1648 result = rates_996[rate->he_gi]; 1649 else if (rate->bw == RATE_INFO_BW_40 || 1650 (rate->bw == RATE_INFO_BW_HE_RU && 1651 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484)) 1652 result = rates_484[rate->he_gi]; 1653 else if (rate->bw == RATE_INFO_BW_20 || 1654 (rate->bw == RATE_INFO_BW_HE_RU && 1655 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242)) 1656 result = rates_242[rate->he_gi]; 1657 else if (rate->bw == RATE_INFO_BW_HE_RU && 1658 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106) 1659 result = rates_106[rate->he_gi]; 1660 else if (rate->bw == RATE_INFO_BW_HE_RU && 1661 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52) 1662 result = rates_52[rate->he_gi]; 1663 else if (rate->bw == RATE_INFO_BW_HE_RU && 1664 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26) 1665 result = rates_26[rate->he_gi]; 1666 else { 1667 WARN(1, "invalid HE MCS: bw:%d, ru:%d\n", 1668 rate->bw, rate->he_ru_alloc); 1669 return 0; 1670 } 1671 1672 /* now scale to the appropriate MCS */ 1673 tmp = result; 1674 tmp *= SCALE; 1675 do_div(tmp, mcs_divisors[rate->mcs]); 1676 1677 /* and take NSS, DCM into account */ 1678 tmp *= rate->nss; 1679 do_div(tmp, 8); 1680 if (rate->he_dcm) 1681 do_div(tmp, 2); 1682 1683 result = tmp; 1684 1685 return result / 10000; 1686 } 1687 1688 static u32 _cfg80211_calculate_bitrate_eht_uhr(struct rate_info *rate) 1689 { 1690 #define SCALE 6144 1691 static const u32 mcs_divisors[] = { 1692 [ 0] = 102399, /* 16.666666... */ 1693 [ 1] = 51201, /* 8.333333... */ 1694 [ 2] = 34134, /* 5.555555... */ 1695 [ 3] = 25599, /* 4.166666... */ 1696 [ 4] = 17067, /* 2.777777... */ 1697 [ 5] = 12801, /* 2.083333... */ 1698 [ 6] = 11377, /* 1.851725... */ 1699 [ 7] = 10239, /* 1.666666... */ 1700 [ 8] = 8532, /* 1.388888... */ 1701 [ 9] = 7680, /* 1.250000... */ 1702 [10] = 6828, /* 1.111111... */ 1703 [11] = 6144, /* 1.000000... */ 1704 [12] = 5690, /* 0.926106... */ 1705 [13] = 5120, /* 0.833333... */ 1706 [14] = 409600, /* 66.666666... */ 1707 [15] = 204800, /* 33.333333... */ 1708 [17] = 38400, /* 6.250180... */ 1709 [19] = 19200, /* 3.125090... */ 1710 [20] = 15360, /* 2.500000... */ 1711 [23] = 9600, /* 1.562545... */ 1712 }; 1713 static const u32 rates_996[3] = { 480388888, 453700000, 408333333 }; 1714 static const u32 rates_484[3] = { 229411111, 216666666, 195000000 }; 1715 static const u32 rates_242[3] = { 114711111, 108333333, 97500000 }; 1716 static const u32 rates_106[3] = { 40000000, 37777777, 34000000 }; 1717 static const u32 rates_52[3] = { 18820000, 17777777, 16000000 }; 1718 static const u32 rates_26[3] = { 9411111, 8888888, 8000000 }; 1719 u64 tmp; 1720 u32 result; 1721 1722 if (WARN_ON_ONCE(rate->eht_gi > NL80211_RATE_INFO_EHT_GI_3_2)) 1723 return 0; 1724 if (WARN_ON_ONCE(rate->eht_ru_alloc > 1725 NL80211_RATE_INFO_EHT_RU_ALLOC_4x996)) 1726 return 0; 1727 if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8)) 1728 return 0; 1729 1730 /* Bandwidth checks for MCS 14 */ 1731 if (rate->mcs == 14) { 1732 if ((rate->bw != RATE_INFO_BW_EHT_RU && 1733 rate->bw != RATE_INFO_BW_80 && 1734 rate->bw != RATE_INFO_BW_160 && 1735 rate->bw != RATE_INFO_BW_320) || 1736 (rate->bw == RATE_INFO_BW_EHT_RU && 1737 rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_996 && 1738 rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_2x996 && 1739 rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_4x996)) { 1740 WARN(1, "invalid EHT BW for MCS 14: bw:%d, ru:%d\n", 1741 rate->bw, rate->eht_ru_alloc); 1742 return 0; 1743 } 1744 } 1745 1746 if (rate->bw == RATE_INFO_BW_320 || 1747 (rate->bw == RATE_INFO_BW_EHT_RU && 1748 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_4x996)) 1749 result = 4 * rates_996[rate->eht_gi]; 1750 else if (rate->bw == RATE_INFO_BW_EHT_RU && 1751 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_3x996P484) 1752 result = 3 * rates_996[rate->eht_gi] + rates_484[rate->eht_gi]; 1753 else if (rate->bw == RATE_INFO_BW_EHT_RU && 1754 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_3x996) 1755 result = 3 * rates_996[rate->eht_gi]; 1756 else if (rate->bw == RATE_INFO_BW_EHT_RU && 1757 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_2x996P484) 1758 result = 2 * rates_996[rate->eht_gi] + rates_484[rate->eht_gi]; 1759 else if (rate->bw == RATE_INFO_BW_160 || 1760 (rate->bw == RATE_INFO_BW_EHT_RU && 1761 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_2x996)) 1762 result = 2 * rates_996[rate->eht_gi]; 1763 else if (rate->bw == RATE_INFO_BW_EHT_RU && 1764 rate->eht_ru_alloc == 1765 NL80211_RATE_INFO_EHT_RU_ALLOC_996P484P242) 1766 result = rates_996[rate->eht_gi] + rates_484[rate->eht_gi] 1767 + rates_242[rate->eht_gi]; 1768 else if (rate->bw == RATE_INFO_BW_EHT_RU && 1769 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_996P484) 1770 result = rates_996[rate->eht_gi] + rates_484[rate->eht_gi]; 1771 else if (rate->bw == RATE_INFO_BW_80 || 1772 (rate->bw == RATE_INFO_BW_EHT_RU && 1773 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_996)) 1774 result = rates_996[rate->eht_gi]; 1775 else if (rate->bw == RATE_INFO_BW_EHT_RU && 1776 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_484P242) 1777 result = rates_484[rate->eht_gi] + rates_242[rate->eht_gi]; 1778 else if (rate->bw == RATE_INFO_BW_40 || 1779 (rate->bw == RATE_INFO_BW_EHT_RU && 1780 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_484)) 1781 result = rates_484[rate->eht_gi]; 1782 else if (rate->bw == RATE_INFO_BW_20 || 1783 (rate->bw == RATE_INFO_BW_EHT_RU && 1784 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_242)) 1785 result = rates_242[rate->eht_gi]; 1786 else if (rate->bw == RATE_INFO_BW_EHT_RU && 1787 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_106P26) 1788 result = rates_106[rate->eht_gi] + rates_26[rate->eht_gi]; 1789 else if (rate->bw == RATE_INFO_BW_EHT_RU && 1790 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_106) 1791 result = rates_106[rate->eht_gi]; 1792 else if (rate->bw == RATE_INFO_BW_EHT_RU && 1793 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_52P26) 1794 result = rates_52[rate->eht_gi] + rates_26[rate->eht_gi]; 1795 else if (rate->bw == RATE_INFO_BW_EHT_RU && 1796 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_52) 1797 result = rates_52[rate->eht_gi]; 1798 else if (rate->bw == RATE_INFO_BW_EHT_RU && 1799 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_26) 1800 result = rates_26[rate->eht_gi]; 1801 else { 1802 WARN(1, "invalid EHT or UHR MCS: bw:%d, ru:%d\n", 1803 rate->bw, rate->eht_ru_alloc); 1804 return 0; 1805 } 1806 1807 /* now scale to the appropriate MCS */ 1808 tmp = result; 1809 tmp *= SCALE; 1810 do_div(tmp, mcs_divisors[rate->mcs]); 1811 1812 /* and take NSS */ 1813 tmp *= rate->nss; 1814 do_div(tmp, 8); 1815 1816 /* and handle interference mitigation - 0.9x */ 1817 if (rate->flags & RATE_INFO_FLAGS_UHR_IM) { 1818 if (WARN(rate->nss != 1 || rate->mcs == 15, 1819 "invalid NSS or MCS for UHR IM\n")) 1820 return 0; 1821 tmp *= 9000; 1822 do_div(tmp, 10000); 1823 } 1824 1825 result = tmp; 1826 1827 return result / 10000; 1828 } 1829 1830 static u32 cfg80211_calculate_bitrate_eht(struct rate_info *rate) 1831 { 1832 if (WARN_ONCE(rate->mcs > 15, "bad EHT MCS %d\n", rate->mcs)) 1833 return 0; 1834 1835 if (WARN_ONCE(rate->flags & (RATE_INFO_FLAGS_UHR_ELR_MCS | 1836 RATE_INFO_FLAGS_UHR_IM), 1837 "bad EHT MCS flags 0x%x\n", rate->flags)) 1838 return 0; 1839 1840 return _cfg80211_calculate_bitrate_eht_uhr(rate); 1841 } 1842 1843 static u32 cfg80211_calculate_bitrate_uhr(struct rate_info *rate) 1844 { 1845 if (rate->flags & RATE_INFO_FLAGS_UHR_ELR_MCS) { 1846 WARN_ONCE(rate->eht_gi != NL80211_RATE_INFO_EHT_GI_1_6, 1847 "bad UHR ELR guard interval %d\n", 1848 rate->eht_gi); 1849 WARN_ONCE(rate->mcs > 1, "bad UHR ELR MCS %d\n", rate->mcs); 1850 WARN_ONCE(rate->nss != 1, "bad UHR ELR NSS %d\n", rate->nss); 1851 WARN_ONCE(rate->bw != RATE_INFO_BW_20, 1852 "bad UHR ELR bandwidth %d\n", 1853 rate->bw); 1854 WARN_ONCE(rate->flags & RATE_INFO_FLAGS_UHR_IM, 1855 "bad UHR MCS flags 0x%x\n", rate->flags); 1856 if (rate->mcs == 0) 1857 return 17; 1858 return 33; 1859 } 1860 1861 switch (rate->mcs) { 1862 case 0 ... 15: 1863 case 17: 1864 case 19: 1865 case 20: 1866 case 23: 1867 return _cfg80211_calculate_bitrate_eht_uhr(rate); 1868 } 1869 1870 WARN_ONCE(1, "bad UHR MCS %d\n", rate->mcs); 1871 return 0; 1872 } 1873 1874 static u32 cfg80211_calculate_bitrate_s1g(struct rate_info *rate) 1875 { 1876 /* For 1, 2, 4, 8 and 16 MHz channels */ 1877 static const u32 base[5][11] = { 1878 { 300000, 1879 600000, 1880 900000, 1881 1200000, 1882 1800000, 1883 2400000, 1884 2700000, 1885 3000000, 1886 3600000, 1887 4000000, 1888 /* MCS 10 supported in 1 MHz only */ 1889 150000, 1890 }, 1891 { 650000, 1892 1300000, 1893 1950000, 1894 2600000, 1895 3900000, 1896 5200000, 1897 5850000, 1898 6500000, 1899 7800000, 1900 /* MCS 9 not valid */ 1901 }, 1902 { 1350000, 1903 2700000, 1904 4050000, 1905 5400000, 1906 8100000, 1907 10800000, 1908 12150000, 1909 13500000, 1910 16200000, 1911 18000000, 1912 }, 1913 { 2925000, 1914 5850000, 1915 8775000, 1916 11700000, 1917 17550000, 1918 23400000, 1919 26325000, 1920 29250000, 1921 35100000, 1922 39000000, 1923 }, 1924 { 8580000, 1925 11700000, 1926 17550000, 1927 23400000, 1928 35100000, 1929 46800000, 1930 52650000, 1931 58500000, 1932 70200000, 1933 78000000, 1934 }, 1935 }; 1936 u32 bitrate; 1937 /* default is 1 MHz index */ 1938 int idx = 0; 1939 1940 if (rate->mcs >= 11) 1941 goto warn; 1942 1943 switch (rate->bw) { 1944 case RATE_INFO_BW_16: 1945 idx = 4; 1946 break; 1947 case RATE_INFO_BW_8: 1948 idx = 3; 1949 break; 1950 case RATE_INFO_BW_4: 1951 idx = 2; 1952 break; 1953 case RATE_INFO_BW_2: 1954 idx = 1; 1955 break; 1956 case RATE_INFO_BW_1: 1957 idx = 0; 1958 break; 1959 case RATE_INFO_BW_5: 1960 case RATE_INFO_BW_10: 1961 case RATE_INFO_BW_20: 1962 case RATE_INFO_BW_40: 1963 case RATE_INFO_BW_80: 1964 case RATE_INFO_BW_160: 1965 default: 1966 goto warn; 1967 } 1968 1969 bitrate = base[idx][rate->mcs]; 1970 bitrate *= rate->nss; 1971 1972 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI) 1973 bitrate = (bitrate / 9) * 10; 1974 /* do NOT round down here */ 1975 return (bitrate + 50000) / 100000; 1976 warn: 1977 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n", 1978 rate->bw, rate->mcs, rate->nss); 1979 return 0; 1980 } 1981 1982 u32 cfg80211_calculate_bitrate(struct rate_info *rate) 1983 { 1984 if (rate->flags & RATE_INFO_FLAGS_MCS) 1985 return cfg80211_calculate_bitrate_ht(rate); 1986 if (rate->flags & RATE_INFO_FLAGS_DMG) 1987 return cfg80211_calculate_bitrate_dmg(rate); 1988 if (rate->flags & RATE_INFO_FLAGS_EXTENDED_SC_DMG) 1989 return cfg80211_calculate_bitrate_extended_sc_dmg(rate); 1990 if (rate->flags & RATE_INFO_FLAGS_EDMG) 1991 return cfg80211_calculate_bitrate_edmg(rate); 1992 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS) 1993 return cfg80211_calculate_bitrate_vht(rate); 1994 if (rate->flags & RATE_INFO_FLAGS_HE_MCS) 1995 return cfg80211_calculate_bitrate_he(rate); 1996 if (rate->flags & RATE_INFO_FLAGS_EHT_MCS) 1997 return cfg80211_calculate_bitrate_eht(rate); 1998 if (rate->flags & RATE_INFO_FLAGS_UHR_MCS) 1999 return cfg80211_calculate_bitrate_uhr(rate); 2000 if (rate->flags & RATE_INFO_FLAGS_S1G_MCS) 2001 return cfg80211_calculate_bitrate_s1g(rate); 2002 2003 return rate->legacy; 2004 } 2005 EXPORT_SYMBOL(cfg80211_calculate_bitrate); 2006 2007 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len, 2008 enum ieee80211_p2p_attr_id attr, 2009 u8 *buf, unsigned int bufsize) 2010 { 2011 u8 *out = buf; 2012 u16 attr_remaining = 0; 2013 bool desired_attr = false; 2014 u16 desired_len = 0; 2015 2016 while (len > 0) { 2017 unsigned int iedatalen; 2018 unsigned int copy; 2019 const u8 *iedata; 2020 2021 if (len < 2) 2022 return -EILSEQ; 2023 iedatalen = ies[1]; 2024 if (iedatalen + 2 > len) 2025 return -EILSEQ; 2026 2027 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC) 2028 goto cont; 2029 2030 if (iedatalen < 4) 2031 goto cont; 2032 2033 iedata = ies + 2; 2034 2035 /* check WFA OUI, P2P subtype */ 2036 if (iedata[0] != 0x50 || iedata[1] != 0x6f || 2037 iedata[2] != 0x9a || iedata[3] != 0x09) 2038 goto cont; 2039 2040 iedatalen -= 4; 2041 iedata += 4; 2042 2043 /* check attribute continuation into this IE */ 2044 copy = min_t(unsigned int, attr_remaining, iedatalen); 2045 if (copy && desired_attr) { 2046 desired_len += copy; 2047 if (out) { 2048 memcpy(out, iedata, min(bufsize, copy)); 2049 out += min(bufsize, copy); 2050 bufsize -= min(bufsize, copy); 2051 } 2052 2053 2054 if (copy == attr_remaining) 2055 return desired_len; 2056 } 2057 2058 attr_remaining -= copy; 2059 if (attr_remaining) 2060 goto cont; 2061 2062 iedatalen -= copy; 2063 iedata += copy; 2064 2065 while (iedatalen > 0) { 2066 u16 attr_len; 2067 2068 /* P2P attribute ID & size must fit */ 2069 if (iedatalen < 3) 2070 return -EILSEQ; 2071 desired_attr = iedata[0] == attr; 2072 attr_len = get_unaligned_le16(iedata + 1); 2073 iedatalen -= 3; 2074 iedata += 3; 2075 2076 copy = min_t(unsigned int, attr_len, iedatalen); 2077 2078 if (desired_attr) { 2079 desired_len += copy; 2080 if (out) { 2081 memcpy(out, iedata, min(bufsize, copy)); 2082 out += min(bufsize, copy); 2083 bufsize -= min(bufsize, copy); 2084 } 2085 2086 if (copy == attr_len) 2087 return desired_len; 2088 } 2089 2090 iedata += copy; 2091 iedatalen -= copy; 2092 attr_remaining = attr_len - copy; 2093 } 2094 2095 cont: 2096 len -= ies[1] + 2; 2097 ies += ies[1] + 2; 2098 } 2099 2100 if (attr_remaining && desired_attr) 2101 return -EILSEQ; 2102 2103 return -ENOENT; 2104 } 2105 EXPORT_SYMBOL(cfg80211_get_p2p_attr); 2106 2107 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext) 2108 { 2109 int i; 2110 2111 /* Make sure array values are legal */ 2112 if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION)) 2113 return false; 2114 2115 i = 0; 2116 while (i < n_ids) { 2117 if (ids[i] == WLAN_EID_EXTENSION) { 2118 if (id_ext && (ids[i + 1] == id)) 2119 return true; 2120 2121 i += 2; 2122 continue; 2123 } 2124 2125 if (ids[i] == id && !id_ext) 2126 return true; 2127 2128 i++; 2129 } 2130 return false; 2131 } 2132 2133 static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos) 2134 { 2135 /* we assume a validly formed IEs buffer */ 2136 u8 len = ies[pos + 1]; 2137 2138 pos += 2 + len; 2139 2140 /* the IE itself must have 255 bytes for fragments to follow */ 2141 if (len < 255) 2142 return pos; 2143 2144 while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) { 2145 len = ies[pos + 1]; 2146 pos += 2 + len; 2147 } 2148 2149 return pos; 2150 } 2151 2152 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen, 2153 const u8 *ids, int n_ids, 2154 const u8 *after_ric, int n_after_ric, 2155 size_t offset) 2156 { 2157 size_t pos = offset; 2158 2159 while (pos < ielen) { 2160 u8 ext = 0; 2161 2162 if (ies[pos] == WLAN_EID_EXTENSION) 2163 ext = 2; 2164 if ((pos + ext) >= ielen) 2165 break; 2166 2167 if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext], 2168 ies[pos] == WLAN_EID_EXTENSION)) 2169 break; 2170 2171 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) { 2172 pos = skip_ie(ies, ielen, pos); 2173 2174 while (pos < ielen) { 2175 if (ies[pos] == WLAN_EID_EXTENSION) 2176 ext = 2; 2177 else 2178 ext = 0; 2179 2180 if ((pos + ext) >= ielen) 2181 break; 2182 2183 if (!ieee80211_id_in_list(after_ric, 2184 n_after_ric, 2185 ies[pos + ext], 2186 ext == 2)) 2187 pos = skip_ie(ies, ielen, pos); 2188 else 2189 break; 2190 } 2191 } else { 2192 pos = skip_ie(ies, ielen, pos); 2193 } 2194 } 2195 2196 return pos; 2197 } 2198 EXPORT_SYMBOL(ieee80211_ie_split_ric); 2199 2200 void ieee80211_fragment_element(struct sk_buff *skb, u8 *len_pos, u8 frag_id) 2201 { 2202 unsigned int elem_len; 2203 2204 if (!len_pos) 2205 return; 2206 2207 elem_len = skb->data + skb->len - len_pos - 1; 2208 2209 while (elem_len > 255) { 2210 /* this one is 255 */ 2211 *len_pos = 255; 2212 /* remaining data gets smaller */ 2213 elem_len -= 255; 2214 /* make space for the fragment ID/len in SKB */ 2215 skb_put(skb, 2); 2216 /* shift back the remaining data to place fragment ID/len */ 2217 memmove(len_pos + 255 + 3, len_pos + 255 + 1, elem_len); 2218 /* place the fragment ID */ 2219 len_pos += 255 + 1; 2220 *len_pos = frag_id; 2221 /* and point to fragment length to update later */ 2222 len_pos++; 2223 } 2224 2225 *len_pos = elem_len; 2226 } 2227 EXPORT_SYMBOL(ieee80211_fragment_element); 2228 2229 bool ieee80211_operating_class_to_band(u8 operating_class, 2230 enum nl80211_band *band) 2231 { 2232 switch (operating_class) { 2233 case 112: 2234 case 115 ... 127: 2235 case 128 ... 130: 2236 *band = NL80211_BAND_5GHZ; 2237 return true; 2238 case 131 ... 135: 2239 case 137: 2240 *band = NL80211_BAND_6GHZ; 2241 return true; 2242 case 81: 2243 case 82: 2244 case 83: 2245 case 84: 2246 *band = NL80211_BAND_2GHZ; 2247 return true; 2248 case 180: 2249 *band = NL80211_BAND_60GHZ; 2250 return true; 2251 } 2252 2253 return false; 2254 } 2255 EXPORT_SYMBOL(ieee80211_operating_class_to_band); 2256 2257 bool ieee80211_operating_class_to_chandef(u8 operating_class, 2258 struct ieee80211_channel *chan, 2259 struct cfg80211_chan_def *chandef) 2260 { 2261 u32 control_freq, offset = 0; 2262 enum nl80211_band band; 2263 2264 if (!ieee80211_operating_class_to_band(operating_class, &band) || 2265 !chan || band != chan->band) 2266 return false; 2267 2268 control_freq = chan->center_freq; 2269 chandef->chan = chan; 2270 2271 if (control_freq >= 5955) 2272 offset = control_freq - 5955; 2273 else if (control_freq >= 5745) 2274 offset = control_freq - 5745; 2275 else if (control_freq >= 5180) 2276 offset = control_freq - 5180; 2277 offset /= 20; 2278 2279 switch (operating_class) { 2280 case 81: /* 2 GHz band; 20 MHz; channels 1..13 */ 2281 case 82: /* 2 GHz band; 20 MHz; channel 14 */ 2282 case 115: /* 5 GHz band; 20 MHz; channels 36,40,44,48 */ 2283 case 118: /* 5 GHz band; 20 MHz; channels 52,56,60,64 */ 2284 case 121: /* 5 GHz band; 20 MHz; channels 100..144 */ 2285 case 124: /* 5 GHz band; 20 MHz; channels 149,153,157,161 */ 2286 case 125: /* 5 GHz band; 20 MHz; channels 149..177 */ 2287 case 131: /* 6 GHz band; 20 MHz; channels 1..233*/ 2288 case 136: /* 6 GHz band; 20 MHz; channel 2 */ 2289 chandef->center_freq1 = control_freq; 2290 chandef->width = NL80211_CHAN_WIDTH_20; 2291 return true; 2292 case 83: /* 2 GHz band; 40 MHz; channels 1..9 */ 2293 case 116: /* 5 GHz band; 40 MHz; channels 36,44 */ 2294 case 119: /* 5 GHz band; 40 MHz; channels 52,60 */ 2295 case 122: /* 5 GHz band; 40 MHz; channels 100,108,116,124,132,140 */ 2296 case 126: /* 5 GHz band; 40 MHz; channels 149,157,165,173 */ 2297 chandef->center_freq1 = control_freq + 10; 2298 chandef->width = NL80211_CHAN_WIDTH_40; 2299 return true; 2300 case 84: /* 2 GHz band; 40 MHz; channels 5..13 */ 2301 case 117: /* 5 GHz band; 40 MHz; channels 40,48 */ 2302 case 120: /* 5 GHz band; 40 MHz; channels 56,64 */ 2303 case 123: /* 5 GHz band; 40 MHz; channels 104,112,120,128,136,144 */ 2304 case 127: /* 5 GHz band; 40 MHz; channels 153,161,169,177 */ 2305 chandef->center_freq1 = control_freq - 10; 2306 chandef->width = NL80211_CHAN_WIDTH_40; 2307 return true; 2308 case 132: /* 6 GHz band; 40 MHz; channels 1,5,..,229*/ 2309 chandef->center_freq1 = control_freq + 10 - (offset & 1) * 20; 2310 chandef->width = NL80211_CHAN_WIDTH_40; 2311 return true; 2312 case 128: /* 5 GHz band; 80 MHz; channels 36..64,100..144,149..177 */ 2313 case 133: /* 6 GHz band; 80 MHz; channels 1,5,..,229 */ 2314 chandef->center_freq1 = control_freq + 30 - (offset & 3) * 20; 2315 chandef->width = NL80211_CHAN_WIDTH_80; 2316 return true; 2317 case 129: /* 5 GHz band; 160 MHz; channels 36..64,100..144,149..177 */ 2318 case 134: /* 6 GHz band; 160 MHz; channels 1,5,..,229 */ 2319 chandef->center_freq1 = control_freq + 70 - (offset & 7) * 20; 2320 chandef->width = NL80211_CHAN_WIDTH_160; 2321 return true; 2322 case 130: /* 5 GHz band; 80+80 MHz; channels 36..64,100..144,149..177 */ 2323 case 135: /* 6 GHz band; 80+80 MHz; channels 1,5,..,229 */ 2324 /* The center_freq2 of 80+80 MHz is unknown */ 2325 case 137: /* 6 GHz band; 320 MHz; channels 1,5,..,229 */ 2326 /* 320-1 or 320-2 channelization is unknown */ 2327 default: 2328 return false; 2329 } 2330 } 2331 EXPORT_SYMBOL(ieee80211_operating_class_to_chandef); 2332 2333 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef, 2334 u8 *op_class) 2335 { 2336 u8 vht_opclass; 2337 u32 freq = chandef->center_freq1; 2338 2339 if (freq >= 2412 && freq <= 2472) { 2340 if (chandef->width > NL80211_CHAN_WIDTH_40) 2341 return false; 2342 2343 /* 2.407 GHz, channels 1..13 */ 2344 if (chandef->width == NL80211_CHAN_WIDTH_40) { 2345 if (freq > chandef->chan->center_freq) 2346 *op_class = 83; /* HT40+ */ 2347 else 2348 *op_class = 84; /* HT40- */ 2349 } else { 2350 *op_class = 81; 2351 } 2352 2353 return true; 2354 } 2355 2356 if (freq == 2484) { 2357 /* channel 14 is only for IEEE 802.11b */ 2358 if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT) 2359 return false; 2360 2361 *op_class = 82; /* channel 14 */ 2362 return true; 2363 } 2364 2365 switch (chandef->width) { 2366 case NL80211_CHAN_WIDTH_80: 2367 vht_opclass = 128; 2368 break; 2369 case NL80211_CHAN_WIDTH_160: 2370 vht_opclass = 129; 2371 break; 2372 case NL80211_CHAN_WIDTH_80P80: 2373 vht_opclass = 130; 2374 break; 2375 default: 2376 vht_opclass = 0; 2377 break; 2378 } 2379 2380 /* 5 GHz, channels 36..48 */ 2381 if (freq >= 5180 && freq <= 5240) { 2382 if (vht_opclass) { 2383 *op_class = vht_opclass; 2384 } else if (chandef->width == NL80211_CHAN_WIDTH_40) { 2385 if (freq > chandef->chan->center_freq) 2386 *op_class = 116; 2387 else 2388 *op_class = 117; 2389 } else { 2390 *op_class = 115; 2391 } 2392 2393 return true; 2394 } 2395 2396 /* 5 GHz, channels 52..64 */ 2397 if (freq >= 5260 && freq <= 5320) { 2398 if (vht_opclass) { 2399 *op_class = vht_opclass; 2400 } else if (chandef->width == NL80211_CHAN_WIDTH_40) { 2401 if (freq > chandef->chan->center_freq) 2402 *op_class = 119; 2403 else 2404 *op_class = 120; 2405 } else { 2406 *op_class = 118; 2407 } 2408 2409 return true; 2410 } 2411 2412 /* 5 GHz, channels 100..144 */ 2413 if (freq >= 5500 && freq <= 5720) { 2414 if (vht_opclass) { 2415 *op_class = vht_opclass; 2416 } else if (chandef->width == NL80211_CHAN_WIDTH_40) { 2417 if (freq > chandef->chan->center_freq) 2418 *op_class = 122; 2419 else 2420 *op_class = 123; 2421 } else { 2422 *op_class = 121; 2423 } 2424 2425 return true; 2426 } 2427 2428 /* 5 GHz, channels 149..169 */ 2429 if (freq >= 5745 && freq <= 5845) { 2430 if (vht_opclass) { 2431 *op_class = vht_opclass; 2432 } else if (chandef->width == NL80211_CHAN_WIDTH_40) { 2433 if (freq > chandef->chan->center_freq) 2434 *op_class = 126; 2435 else 2436 *op_class = 127; 2437 } else if (freq <= 5805) { 2438 *op_class = 124; 2439 } else { 2440 *op_class = 125; 2441 } 2442 2443 return true; 2444 } 2445 2446 /* 56.16 GHz, channel 1..4 */ 2447 if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) { 2448 if (chandef->width >= NL80211_CHAN_WIDTH_40) 2449 return false; 2450 2451 *op_class = 180; 2452 return true; 2453 } 2454 2455 /* not supported yet */ 2456 return false; 2457 } 2458 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class); 2459 2460 static int cfg80211_wdev_bi(struct wireless_dev *wdev) 2461 { 2462 switch (wdev->iftype) { 2463 case NL80211_IFTYPE_AP: 2464 case NL80211_IFTYPE_P2P_GO: 2465 WARN_ON(wdev->valid_links); 2466 return wdev->links[0].ap.beacon_interval; 2467 case NL80211_IFTYPE_MESH_POINT: 2468 return wdev->u.mesh.beacon_interval; 2469 case NL80211_IFTYPE_ADHOC: 2470 return wdev->u.ibss.beacon_interval; 2471 default: 2472 break; 2473 } 2474 2475 return 0; 2476 } 2477 2478 static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int, 2479 u32 *beacon_int_gcd, 2480 bool *beacon_int_different, 2481 int radio_idx) 2482 { 2483 struct cfg80211_registered_device *rdev; 2484 struct wireless_dev *wdev; 2485 2486 *beacon_int_gcd = 0; 2487 *beacon_int_different = false; 2488 2489 rdev = wiphy_to_rdev(wiphy); 2490 list_for_each_entry(wdev, &wiphy->wdev_list, list) { 2491 int wdev_bi; 2492 2493 /* this feature isn't supported with MLO */ 2494 if (wdev->valid_links) 2495 continue; 2496 2497 wdev_bi = cfg80211_wdev_bi(wdev); 2498 if (!wdev_bi) 2499 continue; 2500 2501 /* skip wdevs not active on the given wiphy radio */ 2502 if (radio_idx >= 0 && 2503 !(rdev_get_radio_mask(rdev, wdev->netdev) & BIT(radio_idx))) 2504 continue; 2505 2506 if (!*beacon_int_gcd) { 2507 *beacon_int_gcd = wdev_bi; 2508 continue; 2509 } 2510 2511 if (wdev_bi == *beacon_int_gcd) 2512 continue; 2513 2514 *beacon_int_different = true; 2515 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev_bi); 2516 } 2517 2518 if (new_beacon_int && *beacon_int_gcd != new_beacon_int) { 2519 if (*beacon_int_gcd) 2520 *beacon_int_different = true; 2521 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int); 2522 } 2523 } 2524 2525 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev, 2526 enum nl80211_iftype iftype, u32 beacon_int) 2527 { 2528 /* 2529 * This is just a basic pre-condition check; if interface combinations 2530 * are possible the driver must already be checking those with a call 2531 * to cfg80211_check_combinations(), in which case we'll validate more 2532 * through the cfg80211_calculate_bi_data() call and code in 2533 * cfg80211_iter_combinations(). 2534 */ 2535 2536 if (beacon_int < 10 || beacon_int > 10000) 2537 return -EINVAL; 2538 2539 return 0; 2540 } 2541 2542 int cfg80211_iter_combinations(struct wiphy *wiphy, 2543 struct iface_combination_params *params, 2544 void (*iter)(const struct ieee80211_iface_combination *c, 2545 void *data), 2546 void *data) 2547 { 2548 const struct wiphy_radio *radio = NULL; 2549 const struct ieee80211_iface_combination *c, *cs; 2550 const struct ieee80211_regdomain *regdom; 2551 enum nl80211_dfs_regions region = 0; 2552 int i, j, n, iftype; 2553 int num_interfaces = 0; 2554 u32 used_iftypes = 0; 2555 u32 beacon_int_gcd; 2556 bool beacon_int_different; 2557 2558 if (params->radio_idx >= 0) 2559 radio = &wiphy->radio[params->radio_idx]; 2560 2561 /* 2562 * This is a bit strange, since the iteration used to rely only on 2563 * the data given by the driver, but here it now relies on context, 2564 * in form of the currently operating interfaces. 2565 * This is OK for all current users, and saves us from having to 2566 * push the GCD calculations into all the drivers. 2567 * In the future, this should probably rely more on data that's in 2568 * cfg80211 already - the only thing not would appear to be any new 2569 * interfaces (while being brought up) and channel/radar data. 2570 */ 2571 cfg80211_calculate_bi_data(wiphy, params->new_beacon_int, 2572 &beacon_int_gcd, &beacon_int_different, 2573 params->radio_idx); 2574 2575 if (params->radar_detect) { 2576 rcu_read_lock(); 2577 regdom = rcu_dereference(cfg80211_regdomain); 2578 if (regdom) 2579 region = regdom->dfs_region; 2580 rcu_read_unlock(); 2581 } 2582 2583 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) { 2584 num_interfaces += params->iftype_num[iftype]; 2585 if (params->iftype_num[iftype] > 0 && 2586 !cfg80211_iftype_allowed(wiphy, iftype, 0, 1)) 2587 used_iftypes |= BIT(iftype); 2588 } 2589 2590 if (radio) { 2591 cs = radio->iface_combinations; 2592 n = radio->n_iface_combinations; 2593 } else { 2594 cs = wiphy->iface_combinations; 2595 n = wiphy->n_iface_combinations; 2596 } 2597 for (i = 0; i < n; i++) { 2598 struct ieee80211_iface_limit *limits; 2599 u32 all_iftypes = 0; 2600 2601 c = &cs[i]; 2602 if (num_interfaces > c->max_interfaces) 2603 continue; 2604 if (params->num_different_channels > c->num_different_channels) 2605 continue; 2606 2607 limits = kmemdup_array(c->limits, c->n_limits, sizeof(*limits), 2608 GFP_KERNEL); 2609 if (!limits) 2610 return -ENOMEM; 2611 2612 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) { 2613 if (cfg80211_iftype_allowed(wiphy, iftype, 0, 1)) 2614 continue; 2615 for (j = 0; j < c->n_limits; j++) { 2616 all_iftypes |= limits[j].types; 2617 if (!(limits[j].types & BIT(iftype))) 2618 continue; 2619 if (limits[j].max < params->iftype_num[iftype]) 2620 goto cont; 2621 limits[j].max -= params->iftype_num[iftype]; 2622 } 2623 } 2624 2625 if (params->radar_detect != 2626 (c->radar_detect_widths & params->radar_detect)) 2627 goto cont; 2628 2629 if (params->radar_detect && c->radar_detect_regions && 2630 !(c->radar_detect_regions & BIT(region))) 2631 goto cont; 2632 2633 /* Finally check that all iftypes that we're currently 2634 * using are actually part of this combination. If they 2635 * aren't then we can't use this combination and have 2636 * to continue to the next. 2637 */ 2638 if ((all_iftypes & used_iftypes) != used_iftypes) 2639 goto cont; 2640 2641 if (beacon_int_gcd) { 2642 if (c->beacon_int_min_gcd && 2643 beacon_int_gcd < c->beacon_int_min_gcd) 2644 goto cont; 2645 if (!c->beacon_int_min_gcd && beacon_int_different) 2646 goto cont; 2647 } 2648 2649 /* This combination covered all interface types and 2650 * supported the requested numbers, so we're good. 2651 */ 2652 2653 (*iter)(c, data); 2654 cont: 2655 kfree(limits); 2656 } 2657 2658 return 0; 2659 } 2660 EXPORT_SYMBOL(cfg80211_iter_combinations); 2661 2662 static void 2663 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c, 2664 void *data) 2665 { 2666 int *num = data; 2667 (*num)++; 2668 } 2669 2670 int cfg80211_check_combinations(struct wiphy *wiphy, 2671 struct iface_combination_params *params) 2672 { 2673 int err, num = 0; 2674 2675 err = cfg80211_iter_combinations(wiphy, params, 2676 cfg80211_iter_sum_ifcombs, &num); 2677 if (err) 2678 return err; 2679 if (num == 0) 2680 return -EBUSY; 2681 2682 return 0; 2683 } 2684 EXPORT_SYMBOL(cfg80211_check_combinations); 2685 2686 int cfg80211_get_radio_idx_by_chan(struct wiphy *wiphy, 2687 const struct ieee80211_channel *chan) 2688 { 2689 const struct wiphy_radio *radio; 2690 int i, j; 2691 u32 freq; 2692 2693 if (!chan) 2694 return -EINVAL; 2695 2696 freq = ieee80211_channel_to_khz(chan); 2697 for (i = 0; i < wiphy->n_radio; i++) { 2698 radio = &wiphy->radio[i]; 2699 for (j = 0; j < radio->n_freq_range; j++) { 2700 if (freq >= radio->freq_range[j].start_freq && 2701 freq < radio->freq_range[j].end_freq) 2702 return i; 2703 } 2704 } 2705 2706 return -EINVAL; 2707 } 2708 EXPORT_SYMBOL(cfg80211_get_radio_idx_by_chan); 2709 2710 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband, 2711 const u8 *rates, unsigned int n_rates, 2712 u32 *mask) 2713 { 2714 int i, j; 2715 2716 if (!sband) 2717 return -EINVAL; 2718 2719 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES) 2720 return -EINVAL; 2721 2722 *mask = 0; 2723 2724 for (i = 0; i < n_rates; i++) { 2725 int rate = (rates[i] & 0x7f) * 5; 2726 bool found = false; 2727 2728 for (j = 0; j < sband->n_bitrates; j++) { 2729 if (sband->bitrates[j].bitrate == rate) { 2730 found = true; 2731 *mask |= BIT(j); 2732 break; 2733 } 2734 } 2735 if (!found) 2736 return -EINVAL; 2737 } 2738 2739 /* 2740 * mask must have at least one bit set here since we 2741 * didn't accept a 0-length rates array nor allowed 2742 * entries in the array that didn't exist 2743 */ 2744 2745 return 0; 2746 } 2747 2748 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy) 2749 { 2750 enum nl80211_band band; 2751 unsigned int n_channels = 0; 2752 2753 for (band = 0; band < NUM_NL80211_BANDS; band++) 2754 if (wiphy->bands[band]) 2755 n_channels += wiphy->bands[band]->n_channels; 2756 2757 return n_channels; 2758 } 2759 EXPORT_SYMBOL(ieee80211_get_num_supported_channels); 2760 2761 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr, 2762 struct station_info *sinfo) 2763 { 2764 struct cfg80211_registered_device *rdev; 2765 struct wireless_dev *wdev; 2766 2767 wdev = dev->ieee80211_ptr; 2768 if (!wdev) 2769 return -EOPNOTSUPP; 2770 2771 rdev = wiphy_to_rdev(wdev->wiphy); 2772 if (!rdev->ops->get_station) 2773 return -EOPNOTSUPP; 2774 2775 memset(sinfo, 0, sizeof(*sinfo)); 2776 2777 guard(wiphy)(&rdev->wiphy); 2778 2779 return rdev_get_station(rdev, wdev, mac_addr, sinfo); 2780 } 2781 EXPORT_SYMBOL(cfg80211_get_station); 2782 2783 void cfg80211_free_nan_func(struct cfg80211_nan_func *f) 2784 { 2785 int i; 2786 2787 if (!f) 2788 return; 2789 2790 kfree(f->serv_spec_info); 2791 kfree(f->srf_bf); 2792 kfree(f->srf_macs); 2793 for (i = 0; i < f->num_rx_filters; i++) 2794 kfree(f->rx_filters[i].filter); 2795 2796 for (i = 0; i < f->num_tx_filters; i++) 2797 kfree(f->tx_filters[i].filter); 2798 2799 kfree(f->rx_filters); 2800 kfree(f->tx_filters); 2801 kfree(f); 2802 } 2803 EXPORT_SYMBOL(cfg80211_free_nan_func); 2804 2805 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range, 2806 u32 center_freq_khz, u32 bw_khz) 2807 { 2808 u32 start_freq_khz, end_freq_khz; 2809 2810 start_freq_khz = center_freq_khz - (bw_khz / 2); 2811 end_freq_khz = center_freq_khz + (bw_khz / 2); 2812 2813 if (start_freq_khz >= freq_range->start_freq_khz && 2814 end_freq_khz <= freq_range->end_freq_khz) 2815 return true; 2816 2817 return false; 2818 } 2819 2820 int cfg80211_link_sinfo_alloc_tid_stats(struct link_station_info *link_sinfo, 2821 gfp_t gfp) 2822 { 2823 link_sinfo->pertid = kzalloc_objs(*link_sinfo->pertid, 2824 IEEE80211_NUM_TIDS + 1, gfp); 2825 if (!link_sinfo->pertid) 2826 return -ENOMEM; 2827 2828 return 0; 2829 } 2830 EXPORT_SYMBOL(cfg80211_link_sinfo_alloc_tid_stats); 2831 2832 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp) 2833 { 2834 sinfo->pertid = kzalloc_objs(*(sinfo->pertid), IEEE80211_NUM_TIDS + 1, 2835 gfp); 2836 if (!sinfo->pertid) 2837 return -ENOMEM; 2838 2839 return 0; 2840 } 2841 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats); 2842 2843 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */ 2844 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */ 2845 const unsigned char rfc1042_header[] __aligned(2) = 2846 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 }; 2847 EXPORT_SYMBOL(rfc1042_header); 2848 2849 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */ 2850 const unsigned char bridge_tunnel_header[] __aligned(2) = 2851 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 }; 2852 EXPORT_SYMBOL(bridge_tunnel_header); 2853 2854 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */ 2855 struct iapp_layer2_update { 2856 u8 da[ETH_ALEN]; /* broadcast */ 2857 u8 sa[ETH_ALEN]; /* STA addr */ 2858 __be16 len; /* 6 */ 2859 u8 dsap; /* 0 */ 2860 u8 ssap; /* 0 */ 2861 u8 control; 2862 u8 xid_info[3]; 2863 } __packed; 2864 2865 void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr) 2866 { 2867 struct iapp_layer2_update *msg; 2868 struct sk_buff *skb; 2869 2870 /* Send Level 2 Update Frame to update forwarding tables in layer 2 2871 * bridge devices */ 2872 2873 skb = dev_alloc_skb(sizeof(*msg)); 2874 if (!skb) 2875 return; 2876 msg = skb_put(skb, sizeof(*msg)); 2877 2878 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID) 2879 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */ 2880 2881 eth_broadcast_addr(msg->da); 2882 ether_addr_copy(msg->sa, addr); 2883 msg->len = htons(6); 2884 msg->dsap = 0; 2885 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */ 2886 msg->control = 0xaf; /* XID response lsb.1111F101. 2887 * F=0 (no poll command; unsolicited frame) */ 2888 msg->xid_info[0] = 0x81; /* XID format identifier */ 2889 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */ 2890 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */ 2891 2892 skb->dev = dev; 2893 skb->protocol = eth_type_trans(skb, dev); 2894 memset(skb->cb, 0, sizeof(skb->cb)); 2895 netif_rx(skb); 2896 } 2897 EXPORT_SYMBOL(cfg80211_send_layer2_update); 2898 2899 int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap, 2900 enum ieee80211_vht_chanwidth bw, 2901 int mcs, bool ext_nss_bw_capable, 2902 unsigned int max_vht_nss) 2903 { 2904 u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map); 2905 int ext_nss_bw; 2906 int supp_width; 2907 int i, mcs_encoding; 2908 2909 if (map == 0xffff) 2910 return 0; 2911 2912 if (WARN_ON(mcs > 9 || max_vht_nss > 8)) 2913 return 0; 2914 if (mcs <= 7) 2915 mcs_encoding = 0; 2916 else if (mcs == 8) 2917 mcs_encoding = 1; 2918 else 2919 mcs_encoding = 2; 2920 2921 if (!max_vht_nss) { 2922 /* find max_vht_nss for the given MCS */ 2923 for (i = 7; i >= 0; i--) { 2924 int supp = (map >> (2 * i)) & 3; 2925 2926 if (supp == 3) 2927 continue; 2928 2929 if (supp >= mcs_encoding) { 2930 max_vht_nss = i + 1; 2931 break; 2932 } 2933 } 2934 } 2935 2936 if (!(cap->supp_mcs.tx_mcs_map & 2937 cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE))) 2938 return max_vht_nss; 2939 2940 ext_nss_bw = le32_get_bits(cap->vht_cap_info, 2941 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK); 2942 supp_width = le32_get_bits(cap->vht_cap_info, 2943 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK); 2944 2945 /* if not capable, treat ext_nss_bw as 0 */ 2946 if (!ext_nss_bw_capable) 2947 ext_nss_bw = 0; 2948 2949 /* This is invalid */ 2950 if (supp_width == 3) 2951 return 0; 2952 2953 /* This is an invalid combination so pretend nothing is supported */ 2954 if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2)) 2955 return 0; 2956 2957 /* 2958 * Cover all the special cases according to IEEE 802.11-2016 2959 * Table 9-250. All other cases are either factor of 1 or not 2960 * valid/supported. 2961 */ 2962 switch (bw) { 2963 case IEEE80211_VHT_CHANWIDTH_USE_HT: 2964 case IEEE80211_VHT_CHANWIDTH_80MHZ: 2965 if ((supp_width == 1 || supp_width == 2) && 2966 ext_nss_bw == 3) 2967 return 2 * max_vht_nss; 2968 break; 2969 case IEEE80211_VHT_CHANWIDTH_160MHZ: 2970 if (supp_width == 0 && 2971 (ext_nss_bw == 1 || ext_nss_bw == 2)) 2972 return max_vht_nss / 2; 2973 if (supp_width == 0 && 2974 ext_nss_bw == 3) 2975 return (3 * max_vht_nss) / 4; 2976 if (supp_width == 1 && 2977 ext_nss_bw == 3) 2978 return 2 * max_vht_nss; 2979 break; 2980 case IEEE80211_VHT_CHANWIDTH_80P80MHZ: 2981 if (supp_width == 0 && ext_nss_bw == 1) 2982 return 0; /* not possible */ 2983 if (supp_width == 0 && 2984 ext_nss_bw == 2) 2985 return max_vht_nss / 2; 2986 if (supp_width == 0 && 2987 ext_nss_bw == 3) 2988 return (3 * max_vht_nss) / 4; 2989 if (supp_width == 1 && 2990 ext_nss_bw == 0) 2991 return 0; /* not possible */ 2992 if (supp_width == 1 && 2993 ext_nss_bw == 1) 2994 return max_vht_nss / 2; 2995 if (supp_width == 1 && 2996 ext_nss_bw == 2) 2997 return (3 * max_vht_nss) / 4; 2998 break; 2999 } 3000 3001 /* not covered or invalid combination received */ 3002 return max_vht_nss; 3003 } 3004 EXPORT_SYMBOL(ieee80211_get_vht_max_nss); 3005 3006 bool cfg80211_iftype_allowed(struct wiphy *wiphy, enum nl80211_iftype iftype, 3007 bool is_4addr, u8 check_swif) 3008 3009 { 3010 bool is_vlan = iftype == NL80211_IFTYPE_AP_VLAN; 3011 3012 switch (check_swif) { 3013 case 0: 3014 if (is_vlan && is_4addr) 3015 return wiphy->flags & WIPHY_FLAG_4ADDR_AP; 3016 return wiphy->interface_modes & BIT(iftype); 3017 case 1: 3018 if (!(wiphy->software_iftypes & BIT(iftype)) && is_vlan) 3019 return wiphy->flags & WIPHY_FLAG_4ADDR_AP; 3020 return wiphy->software_iftypes & BIT(iftype); 3021 default: 3022 break; 3023 } 3024 3025 return false; 3026 } 3027 EXPORT_SYMBOL(cfg80211_iftype_allowed); 3028 3029 void cfg80211_remove_link(struct wireless_dev *wdev, unsigned int link_id) 3030 { 3031 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 3032 3033 lockdep_assert_wiphy(wdev->wiphy); 3034 3035 switch (wdev->iftype) { 3036 case NL80211_IFTYPE_AP: 3037 case NL80211_IFTYPE_P2P_GO: 3038 cfg80211_stop_ap(rdev, wdev->netdev, link_id, true); 3039 break; 3040 default: 3041 /* per-link not relevant */ 3042 break; 3043 } 3044 3045 rdev_del_intf_link(rdev, wdev, link_id); 3046 3047 wdev->valid_links &= ~BIT(link_id); 3048 eth_zero_addr(wdev->links[link_id].addr); 3049 } 3050 3051 void cfg80211_remove_links(struct wireless_dev *wdev) 3052 { 3053 unsigned int link_id; 3054 3055 /* 3056 * links are controlled by upper layers (userspace/cfg) 3057 * only for AP mode, so only remove them here for AP 3058 */ 3059 if (wdev->iftype != NL80211_IFTYPE_AP) 3060 return; 3061 3062 if (wdev->valid_links) { 3063 for_each_valid_link(wdev, link_id) 3064 cfg80211_remove_link(wdev, link_id); 3065 } 3066 } 3067 3068 int cfg80211_remove_virtual_intf(struct cfg80211_registered_device *rdev, 3069 struct wireless_dev *wdev) 3070 { 3071 cfg80211_remove_links(wdev); 3072 3073 return rdev_del_virtual_intf(rdev, wdev); 3074 } 3075 3076 const struct wiphy_iftype_ext_capab * 3077 cfg80211_get_iftype_ext_capa(struct wiphy *wiphy, enum nl80211_iftype type) 3078 { 3079 int i; 3080 3081 for (i = 0; i < wiphy->num_iftype_ext_capab; i++) { 3082 if (wiphy->iftype_ext_capab[i].iftype == type) 3083 return &wiphy->iftype_ext_capab[i]; 3084 } 3085 3086 return NULL; 3087 } 3088 EXPORT_SYMBOL(cfg80211_get_iftype_ext_capa); 3089 3090 bool ieee80211_radio_freq_range_valid(const struct wiphy_radio *radio, 3091 u32 freq, u32 width) 3092 { 3093 const struct wiphy_radio_freq_range *r; 3094 int i; 3095 3096 for (i = 0; i < radio->n_freq_range; i++) { 3097 r = &radio->freq_range[i]; 3098 if (freq - width / 2 >= r->start_freq && 3099 freq + width / 2 <= r->end_freq) 3100 return true; 3101 } 3102 3103 return false; 3104 } 3105 EXPORT_SYMBOL(ieee80211_radio_freq_range_valid); 3106 3107 bool cfg80211_radio_chandef_valid(const struct wiphy_radio *radio, 3108 const struct cfg80211_chan_def *chandef) 3109 { 3110 u32 freq, width; 3111 3112 freq = ieee80211_chandef_to_khz(chandef); 3113 width = MHZ_TO_KHZ(cfg80211_chandef_get_width(chandef)); 3114 if (!ieee80211_radio_freq_range_valid(radio, freq, width)) 3115 return false; 3116 3117 freq = MHZ_TO_KHZ(chandef->center_freq2); 3118 if (freq && !ieee80211_radio_freq_range_valid(radio, freq, width)) 3119 return false; 3120 3121 return true; 3122 } 3123 EXPORT_SYMBOL(cfg80211_radio_chandef_valid); 3124 3125 bool cfg80211_wdev_channel_allowed(struct wireless_dev *wdev, 3126 struct ieee80211_channel *chan) 3127 { 3128 struct wiphy *wiphy = wdev->wiphy; 3129 const struct wiphy_radio *radio; 3130 struct cfg80211_chan_def chandef; 3131 u32 radio_mask; 3132 int i; 3133 3134 radio_mask = wdev->radio_mask; 3135 if (!wiphy->n_radio || radio_mask == BIT(wiphy->n_radio) - 1) 3136 return true; 3137 3138 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_HT20); 3139 for (i = 0; i < wiphy->n_radio; i++) { 3140 if (!(radio_mask & BIT(i))) 3141 continue; 3142 3143 radio = &wiphy->radio[i]; 3144 if (!cfg80211_radio_chandef_valid(radio, &chandef)) 3145 continue; 3146 3147 return true; 3148 } 3149 3150 return false; 3151 } 3152 EXPORT_SYMBOL(cfg80211_wdev_channel_allowed); 3153