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