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 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 framents, 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 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list, 824 const u8 *addr, enum nl80211_iftype iftype, 825 const unsigned int extra_headroom, 826 const u8 *check_da, const u8 *check_sa, 827 u8 mesh_control) 828 { 829 unsigned int hlen = ALIGN(extra_headroom, 4); 830 struct sk_buff *frame = NULL; 831 int offset = 0; 832 struct { 833 struct ethhdr eth; 834 uint8_t flags; 835 } hdr; 836 bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb); 837 bool reuse_skb = false; 838 bool last = false; 839 int copy_len = sizeof(hdr.eth); 840 841 if (iftype == NL80211_IFTYPE_MESH_POINT) 842 copy_len = sizeof(hdr); 843 844 while (!last) { 845 int remaining = skb->len - offset; 846 unsigned int subframe_len; 847 int len, mesh_len = 0; 848 u8 padding; 849 850 if (copy_len > remaining) 851 goto purge; 852 853 skb_copy_bits(skb, offset, &hdr, copy_len); 854 if (iftype == NL80211_IFTYPE_MESH_POINT) 855 mesh_len = __ieee80211_get_mesh_hdrlen(hdr.flags); 856 len = ieee80211_amsdu_subframe_length(&hdr.eth.h_proto, hdr.flags, 857 mesh_control); 858 subframe_len = sizeof(struct ethhdr) + len; 859 padding = (4 - subframe_len) & 0x3; 860 861 /* the last MSDU has no padding */ 862 if (subframe_len > remaining) 863 goto purge; 864 /* mitigate A-MSDU aggregation injection attacks */ 865 if (ether_addr_equal(hdr.eth.h_dest, rfc1042_header)) 866 goto purge; 867 868 offset += sizeof(struct ethhdr); 869 last = remaining <= subframe_len + padding; 870 871 /* FIXME: should we really accept multicast DA? */ 872 if ((check_da && !is_multicast_ether_addr(hdr.eth.h_dest) && 873 !ether_addr_equal(check_da, hdr.eth.h_dest)) || 874 (check_sa && !ether_addr_equal(check_sa, hdr.eth.h_source))) { 875 offset += len + padding; 876 continue; 877 } 878 879 /* reuse skb for the last subframe */ 880 if (!skb_is_nonlinear(skb) && !reuse_frag && last) { 881 skb_pull(skb, offset); 882 frame = skb; 883 reuse_skb = true; 884 } else { 885 frame = __ieee80211_amsdu_copy(skb, hlen, offset, len, 886 reuse_frag, 32 + mesh_len); 887 if (!frame) 888 goto purge; 889 890 offset += len + padding; 891 } 892 893 skb_reset_network_header(frame); 894 frame->dev = skb->dev; 895 frame->priority = skb->priority; 896 897 if (likely(iftype != NL80211_IFTYPE_MESH_POINT && 898 ieee80211_get_8023_tunnel_proto(frame->data, &hdr.eth.h_proto))) 899 skb_pull(frame, ETH_ALEN + 2); 900 901 memcpy(skb_push(frame, sizeof(hdr.eth)), &hdr.eth, sizeof(hdr.eth)); 902 __skb_queue_tail(list, frame); 903 } 904 905 if (!reuse_skb) 906 dev_kfree_skb(skb); 907 908 return; 909 910 purge: 911 __skb_queue_purge(list); 912 dev_kfree_skb(skb); 913 } 914 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s); 915 916 /* Given a data frame determine the 802.1p/1d tag to use. */ 917 unsigned int cfg80211_classify8021d(struct sk_buff *skb, 918 struct cfg80211_qos_map *qos_map) 919 { 920 unsigned int dscp; 921 unsigned char vlan_priority; 922 unsigned int ret; 923 924 /* skb->priority values from 256->263 are magic values to 925 * directly indicate a specific 802.1d priority. This is used 926 * to allow 802.1d priority to be passed directly in from VLAN 927 * tags, etc. 928 */ 929 if (skb->priority >= 256 && skb->priority <= 263) { 930 ret = skb->priority - 256; 931 goto out; 932 } 933 934 if (skb_vlan_tag_present(skb)) { 935 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK) 936 >> VLAN_PRIO_SHIFT; 937 if (vlan_priority > 0) { 938 ret = vlan_priority; 939 goto out; 940 } 941 } 942 943 switch (skb->protocol) { 944 case htons(ETH_P_IP): 945 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc; 946 break; 947 case htons(ETH_P_IPV6): 948 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc; 949 break; 950 case htons(ETH_P_MPLS_UC): 951 case htons(ETH_P_MPLS_MC): { 952 struct mpls_label mpls_tmp, *mpls; 953 954 mpls = skb_header_pointer(skb, sizeof(struct ethhdr), 955 sizeof(*mpls), &mpls_tmp); 956 if (!mpls) 957 return 0; 958 959 ret = (ntohl(mpls->entry) & MPLS_LS_TC_MASK) 960 >> MPLS_LS_TC_SHIFT; 961 goto out; 962 } 963 case htons(ETH_P_80221): 964 /* 802.21 is always network control traffic */ 965 return 7; 966 default: 967 return 0; 968 } 969 970 if (qos_map) { 971 unsigned int i, tmp_dscp = dscp >> 2; 972 973 for (i = 0; i < qos_map->num_des; i++) { 974 if (tmp_dscp == qos_map->dscp_exception[i].dscp) { 975 ret = qos_map->dscp_exception[i].up; 976 goto out; 977 } 978 } 979 980 for (i = 0; i < 8; i++) { 981 if (tmp_dscp >= qos_map->up[i].low && 982 tmp_dscp <= qos_map->up[i].high) { 983 ret = i; 984 goto out; 985 } 986 } 987 } 988 989 /* The default mapping as defined Section 2.3 in RFC8325: The three 990 * Most Significant Bits (MSBs) of the DSCP are used as the 991 * corresponding L2 markings. 992 */ 993 ret = dscp >> 5; 994 995 /* Handle specific DSCP values for which the default mapping (as 996 * described above) doesn't adhere to the intended usage of the DSCP 997 * value. See section 4 in RFC8325. Specifically, for the following 998 * Diffserv Service Classes no update is needed: 999 * - Standard: DF 1000 * - Low Priority Data: CS1 1001 * - Multimedia Streaming: AF31, AF32, AF33 1002 * - Multimedia Conferencing: AF41, AF42, AF43 1003 * - Network Control Traffic: CS7 1004 * - Real-Time Interactive: CS4 1005 */ 1006 switch (dscp >> 2) { 1007 case 10: 1008 case 12: 1009 case 14: 1010 /* High throughput data: AF11, AF12, AF13 */ 1011 ret = 0; 1012 break; 1013 case 16: 1014 /* Operations, Administration, and Maintenance and Provisioning: 1015 * CS2 1016 */ 1017 ret = 0; 1018 break; 1019 case 18: 1020 case 20: 1021 case 22: 1022 /* Low latency data: AF21, AF22, AF23 */ 1023 ret = 3; 1024 break; 1025 case 24: 1026 /* Broadcasting video: CS3 */ 1027 ret = 4; 1028 break; 1029 case 40: 1030 /* Signaling: CS5 */ 1031 ret = 5; 1032 break; 1033 case 44: 1034 /* Voice Admit: VA */ 1035 ret = 6; 1036 break; 1037 case 46: 1038 /* Telephony traffic: EF */ 1039 ret = 6; 1040 break; 1041 case 48: 1042 /* Network Control Traffic: CS6 */ 1043 ret = 7; 1044 break; 1045 } 1046 out: 1047 return array_index_nospec(ret, IEEE80211_NUM_TIDS); 1048 } 1049 EXPORT_SYMBOL(cfg80211_classify8021d); 1050 1051 const struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id) 1052 { 1053 const struct cfg80211_bss_ies *ies; 1054 1055 ies = rcu_dereference(bss->ies); 1056 if (!ies) 1057 return NULL; 1058 1059 return cfg80211_find_elem(id, ies->data, ies->len); 1060 } 1061 EXPORT_SYMBOL(ieee80211_bss_get_elem); 1062 1063 void cfg80211_upload_connect_keys(struct wireless_dev *wdev) 1064 { 1065 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 1066 struct net_device *dev = wdev->netdev; 1067 int i; 1068 1069 if (!wdev->connect_keys) 1070 return; 1071 1072 for (i = 0; i < 4; i++) { 1073 if (!wdev->connect_keys->params[i].cipher) 1074 continue; 1075 if (rdev_add_key(rdev, dev, -1, i, false, NULL, 1076 &wdev->connect_keys->params[i])) { 1077 netdev_err(dev, "failed to set key %d\n", i); 1078 continue; 1079 } 1080 if (wdev->connect_keys->def == i && 1081 rdev_set_default_key(rdev, dev, -1, i, true, true)) { 1082 netdev_err(dev, "failed to set defkey %d\n", i); 1083 continue; 1084 } 1085 } 1086 1087 kfree_sensitive(wdev->connect_keys); 1088 wdev->connect_keys = NULL; 1089 } 1090 1091 void cfg80211_process_wdev_events(struct wireless_dev *wdev) 1092 { 1093 struct cfg80211_event *ev; 1094 unsigned long flags; 1095 1096 spin_lock_irqsave(&wdev->event_lock, flags); 1097 while (!list_empty(&wdev->event_list)) { 1098 ev = list_first_entry(&wdev->event_list, 1099 struct cfg80211_event, list); 1100 list_del(&ev->list); 1101 spin_unlock_irqrestore(&wdev->event_lock, flags); 1102 1103 switch (ev->type) { 1104 case EVENT_CONNECT_RESULT: 1105 __cfg80211_connect_result( 1106 wdev->netdev, 1107 &ev->cr, 1108 ev->cr.status == WLAN_STATUS_SUCCESS); 1109 break; 1110 case EVENT_ROAMED: 1111 __cfg80211_roamed(wdev, &ev->rm); 1112 break; 1113 case EVENT_DISCONNECTED: 1114 __cfg80211_disconnected(wdev->netdev, 1115 ev->dc.ie, ev->dc.ie_len, 1116 ev->dc.reason, 1117 !ev->dc.locally_generated); 1118 break; 1119 case EVENT_IBSS_JOINED: 1120 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid, 1121 ev->ij.channel); 1122 break; 1123 case EVENT_STOPPED: 1124 cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev); 1125 break; 1126 case EVENT_PORT_AUTHORIZED: 1127 __cfg80211_port_authorized(wdev, ev->pa.peer_addr, 1128 ev->pa.td_bitmap, 1129 ev->pa.td_bitmap_len); 1130 break; 1131 } 1132 1133 kfree(ev); 1134 1135 spin_lock_irqsave(&wdev->event_lock, flags); 1136 } 1137 spin_unlock_irqrestore(&wdev->event_lock, flags); 1138 } 1139 1140 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev) 1141 { 1142 struct wireless_dev *wdev; 1143 1144 lockdep_assert_held(&rdev->wiphy.mtx); 1145 1146 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) 1147 cfg80211_process_wdev_events(wdev); 1148 } 1149 1150 int cfg80211_change_iface(struct cfg80211_registered_device *rdev, 1151 struct net_device *dev, enum nl80211_iftype ntype, 1152 struct vif_params *params) 1153 { 1154 int err; 1155 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype; 1156 1157 lockdep_assert_held(&rdev->wiphy.mtx); 1158 1159 /* don't support changing VLANs, you just re-create them */ 1160 if (otype == NL80211_IFTYPE_AP_VLAN) 1161 return -EOPNOTSUPP; 1162 1163 /* cannot change into P2P device or NAN */ 1164 if (ntype == NL80211_IFTYPE_P2P_DEVICE || 1165 ntype == NL80211_IFTYPE_NAN) 1166 return -EOPNOTSUPP; 1167 1168 if (!rdev->ops->change_virtual_intf || 1169 !(rdev->wiphy.interface_modes & (1 << ntype))) 1170 return -EOPNOTSUPP; 1171 1172 if (ntype != otype) { 1173 /* if it's part of a bridge, reject changing type to station/ibss */ 1174 if (netif_is_bridge_port(dev) && 1175 (ntype == NL80211_IFTYPE_ADHOC || 1176 ntype == NL80211_IFTYPE_STATION || 1177 ntype == NL80211_IFTYPE_P2P_CLIENT)) 1178 return -EBUSY; 1179 1180 dev->ieee80211_ptr->use_4addr = false; 1181 rdev_set_qos_map(rdev, dev, NULL); 1182 1183 switch (otype) { 1184 case NL80211_IFTYPE_AP: 1185 case NL80211_IFTYPE_P2P_GO: 1186 cfg80211_stop_ap(rdev, dev, -1, true); 1187 break; 1188 case NL80211_IFTYPE_ADHOC: 1189 cfg80211_leave_ibss(rdev, dev, false); 1190 break; 1191 case NL80211_IFTYPE_STATION: 1192 case NL80211_IFTYPE_P2P_CLIENT: 1193 cfg80211_disconnect(rdev, dev, 1194 WLAN_REASON_DEAUTH_LEAVING, true); 1195 break; 1196 case NL80211_IFTYPE_MESH_POINT: 1197 /* mesh should be handled? */ 1198 break; 1199 case NL80211_IFTYPE_OCB: 1200 cfg80211_leave_ocb(rdev, dev); 1201 break; 1202 default: 1203 break; 1204 } 1205 1206 cfg80211_process_rdev_events(rdev); 1207 cfg80211_mlme_purge_registrations(dev->ieee80211_ptr); 1208 1209 memset(&dev->ieee80211_ptr->u, 0, 1210 sizeof(dev->ieee80211_ptr->u)); 1211 memset(&dev->ieee80211_ptr->links, 0, 1212 sizeof(dev->ieee80211_ptr->links)); 1213 } 1214 1215 err = rdev_change_virtual_intf(rdev, dev, ntype, params); 1216 1217 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype); 1218 1219 if (!err && params && params->use_4addr != -1) 1220 dev->ieee80211_ptr->use_4addr = params->use_4addr; 1221 1222 if (!err) { 1223 dev->priv_flags &= ~IFF_DONT_BRIDGE; 1224 switch (ntype) { 1225 case NL80211_IFTYPE_STATION: 1226 if (dev->ieee80211_ptr->use_4addr) 1227 break; 1228 fallthrough; 1229 case NL80211_IFTYPE_OCB: 1230 case NL80211_IFTYPE_P2P_CLIENT: 1231 case NL80211_IFTYPE_ADHOC: 1232 dev->priv_flags |= IFF_DONT_BRIDGE; 1233 break; 1234 case NL80211_IFTYPE_P2P_GO: 1235 case NL80211_IFTYPE_AP: 1236 case NL80211_IFTYPE_AP_VLAN: 1237 case NL80211_IFTYPE_MESH_POINT: 1238 /* bridging OK */ 1239 break; 1240 case NL80211_IFTYPE_MONITOR: 1241 /* monitor can't bridge anyway */ 1242 break; 1243 case NL80211_IFTYPE_UNSPECIFIED: 1244 case NUM_NL80211_IFTYPES: 1245 /* not happening */ 1246 break; 1247 case NL80211_IFTYPE_P2P_DEVICE: 1248 case NL80211_IFTYPE_WDS: 1249 case NL80211_IFTYPE_NAN: 1250 WARN_ON(1); 1251 break; 1252 } 1253 } 1254 1255 if (!err && ntype != otype && netif_running(dev)) { 1256 cfg80211_update_iface_num(rdev, ntype, 1); 1257 cfg80211_update_iface_num(rdev, otype, -1); 1258 } 1259 1260 return err; 1261 } 1262 1263 static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate) 1264 { 1265 int modulation, streams, bitrate; 1266 1267 /* the formula below does only work for MCS values smaller than 32 */ 1268 if (WARN_ON_ONCE(rate->mcs >= 32)) 1269 return 0; 1270 1271 modulation = rate->mcs & 7; 1272 streams = (rate->mcs >> 3) + 1; 1273 1274 bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000; 1275 1276 if (modulation < 4) 1277 bitrate *= (modulation + 1); 1278 else if (modulation == 4) 1279 bitrate *= (modulation + 2); 1280 else 1281 bitrate *= (modulation + 3); 1282 1283 bitrate *= streams; 1284 1285 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI) 1286 bitrate = (bitrate / 9) * 10; 1287 1288 /* do NOT round down here */ 1289 return (bitrate + 50000) / 100000; 1290 } 1291 1292 static u32 cfg80211_calculate_bitrate_dmg(struct rate_info *rate) 1293 { 1294 static const u32 __mcs2bitrate[] = { 1295 /* control PHY */ 1296 [0] = 275, 1297 /* SC PHY */ 1298 [1] = 3850, 1299 [2] = 7700, 1300 [3] = 9625, 1301 [4] = 11550, 1302 [5] = 12512, /* 1251.25 mbps */ 1303 [6] = 15400, 1304 [7] = 19250, 1305 [8] = 23100, 1306 [9] = 25025, 1307 [10] = 30800, 1308 [11] = 38500, 1309 [12] = 46200, 1310 /* OFDM PHY */ 1311 [13] = 6930, 1312 [14] = 8662, /* 866.25 mbps */ 1313 [15] = 13860, 1314 [16] = 17325, 1315 [17] = 20790, 1316 [18] = 27720, 1317 [19] = 34650, 1318 [20] = 41580, 1319 [21] = 45045, 1320 [22] = 51975, 1321 [23] = 62370, 1322 [24] = 67568, /* 6756.75 mbps */ 1323 /* LP-SC PHY */ 1324 [25] = 6260, 1325 [26] = 8340, 1326 [27] = 11120, 1327 [28] = 12510, 1328 [29] = 16680, 1329 [30] = 22240, 1330 [31] = 25030, 1331 }; 1332 1333 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate))) 1334 return 0; 1335 1336 return __mcs2bitrate[rate->mcs]; 1337 } 1338 1339 static u32 cfg80211_calculate_bitrate_extended_sc_dmg(struct rate_info *rate) 1340 { 1341 static const u32 __mcs2bitrate[] = { 1342 [6 - 6] = 26950, /* MCS 9.1 : 2695.0 mbps */ 1343 [7 - 6] = 50050, /* MCS 12.1 */ 1344 [8 - 6] = 53900, 1345 [9 - 6] = 57750, 1346 [10 - 6] = 63900, 1347 [11 - 6] = 75075, 1348 [12 - 6] = 80850, 1349 }; 1350 1351 /* Extended SC MCS not defined for base MCS below 6 or above 12 */ 1352 if (WARN_ON_ONCE(rate->mcs < 6 || rate->mcs > 12)) 1353 return 0; 1354 1355 return __mcs2bitrate[rate->mcs - 6]; 1356 } 1357 1358 static u32 cfg80211_calculate_bitrate_edmg(struct rate_info *rate) 1359 { 1360 static const u32 __mcs2bitrate[] = { 1361 /* control PHY */ 1362 [0] = 275, 1363 /* SC PHY */ 1364 [1] = 3850, 1365 [2] = 7700, 1366 [3] = 9625, 1367 [4] = 11550, 1368 [5] = 12512, /* 1251.25 mbps */ 1369 [6] = 13475, 1370 [7] = 15400, 1371 [8] = 19250, 1372 [9] = 23100, 1373 [10] = 25025, 1374 [11] = 26950, 1375 [12] = 30800, 1376 [13] = 38500, 1377 [14] = 46200, 1378 [15] = 50050, 1379 [16] = 53900, 1380 [17] = 57750, 1381 [18] = 69300, 1382 [19] = 75075, 1383 [20] = 80850, 1384 }; 1385 1386 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate))) 1387 return 0; 1388 1389 return __mcs2bitrate[rate->mcs] * rate->n_bonded_ch; 1390 } 1391 1392 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate) 1393 { 1394 static const u32 base[4][12] = { 1395 { 6500000, 1396 13000000, 1397 19500000, 1398 26000000, 1399 39000000, 1400 52000000, 1401 58500000, 1402 65000000, 1403 78000000, 1404 /* not in the spec, but some devices use this: */ 1405 86700000, 1406 97500000, 1407 108300000, 1408 }, 1409 { 13500000, 1410 27000000, 1411 40500000, 1412 54000000, 1413 81000000, 1414 108000000, 1415 121500000, 1416 135000000, 1417 162000000, 1418 180000000, 1419 202500000, 1420 225000000, 1421 }, 1422 { 29300000, 1423 58500000, 1424 87800000, 1425 117000000, 1426 175500000, 1427 234000000, 1428 263300000, 1429 292500000, 1430 351000000, 1431 390000000, 1432 438800000, 1433 487500000, 1434 }, 1435 { 58500000, 1436 117000000, 1437 175500000, 1438 234000000, 1439 351000000, 1440 468000000, 1441 526500000, 1442 585000000, 1443 702000000, 1444 780000000, 1445 877500000, 1446 975000000, 1447 }, 1448 }; 1449 u32 bitrate; 1450 int idx; 1451 1452 if (rate->mcs > 11) 1453 goto warn; 1454 1455 switch (rate->bw) { 1456 case RATE_INFO_BW_160: 1457 idx = 3; 1458 break; 1459 case RATE_INFO_BW_80: 1460 idx = 2; 1461 break; 1462 case RATE_INFO_BW_40: 1463 idx = 1; 1464 break; 1465 case RATE_INFO_BW_5: 1466 case RATE_INFO_BW_10: 1467 default: 1468 goto warn; 1469 case RATE_INFO_BW_20: 1470 idx = 0; 1471 } 1472 1473 bitrate = base[idx][rate->mcs]; 1474 bitrate *= rate->nss; 1475 1476 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI) 1477 bitrate = (bitrate / 9) * 10; 1478 1479 /* do NOT round down here */ 1480 return (bitrate + 50000) / 100000; 1481 warn: 1482 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n", 1483 rate->bw, rate->mcs, rate->nss); 1484 return 0; 1485 } 1486 1487 static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate) 1488 { 1489 #define SCALE 6144 1490 u32 mcs_divisors[14] = { 1491 102399, /* 16.666666... */ 1492 51201, /* 8.333333... */ 1493 34134, /* 5.555555... */ 1494 25599, /* 4.166666... */ 1495 17067, /* 2.777777... */ 1496 12801, /* 2.083333... */ 1497 11377, /* 1.851725... */ 1498 10239, /* 1.666666... */ 1499 8532, /* 1.388888... */ 1500 7680, /* 1.250000... */ 1501 6828, /* 1.111111... */ 1502 6144, /* 1.000000... */ 1503 5690, /* 0.926106... */ 1504 5120, /* 0.833333... */ 1505 }; 1506 u32 rates_160M[3] = { 960777777, 907400000, 816666666 }; 1507 u32 rates_996[3] = { 480388888, 453700000, 408333333 }; 1508 u32 rates_484[3] = { 229411111, 216666666, 195000000 }; 1509 u32 rates_242[3] = { 114711111, 108333333, 97500000 }; 1510 u32 rates_106[3] = { 40000000, 37777777, 34000000 }; 1511 u32 rates_52[3] = { 18820000, 17777777, 16000000 }; 1512 u32 rates_26[3] = { 9411111, 8888888, 8000000 }; 1513 u64 tmp; 1514 u32 result; 1515 1516 if (WARN_ON_ONCE(rate->mcs > 13)) 1517 return 0; 1518 1519 if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2)) 1520 return 0; 1521 if (WARN_ON_ONCE(rate->he_ru_alloc > 1522 NL80211_RATE_INFO_HE_RU_ALLOC_2x996)) 1523 return 0; 1524 if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8)) 1525 return 0; 1526 1527 if (rate->bw == RATE_INFO_BW_160 || 1528 (rate->bw == RATE_INFO_BW_HE_RU && 1529 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_2x996)) 1530 result = rates_160M[rate->he_gi]; 1531 else if (rate->bw == RATE_INFO_BW_80 || 1532 (rate->bw == RATE_INFO_BW_HE_RU && 1533 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996)) 1534 result = rates_996[rate->he_gi]; 1535 else if (rate->bw == RATE_INFO_BW_40 || 1536 (rate->bw == RATE_INFO_BW_HE_RU && 1537 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484)) 1538 result = rates_484[rate->he_gi]; 1539 else if (rate->bw == RATE_INFO_BW_20 || 1540 (rate->bw == RATE_INFO_BW_HE_RU && 1541 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242)) 1542 result = rates_242[rate->he_gi]; 1543 else if (rate->bw == RATE_INFO_BW_HE_RU && 1544 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106) 1545 result = rates_106[rate->he_gi]; 1546 else if (rate->bw == RATE_INFO_BW_HE_RU && 1547 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52) 1548 result = rates_52[rate->he_gi]; 1549 else if (rate->bw == RATE_INFO_BW_HE_RU && 1550 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26) 1551 result = rates_26[rate->he_gi]; 1552 else { 1553 WARN(1, "invalid HE MCS: bw:%d, ru:%d\n", 1554 rate->bw, rate->he_ru_alloc); 1555 return 0; 1556 } 1557 1558 /* now scale to the appropriate MCS */ 1559 tmp = result; 1560 tmp *= SCALE; 1561 do_div(tmp, mcs_divisors[rate->mcs]); 1562 result = tmp; 1563 1564 /* and take NSS, DCM into account */ 1565 result = (result * rate->nss) / 8; 1566 if (rate->he_dcm) 1567 result /= 2; 1568 1569 return result / 10000; 1570 } 1571 1572 static u32 cfg80211_calculate_bitrate_eht(struct rate_info *rate) 1573 { 1574 #define SCALE 6144 1575 static const u32 mcs_divisors[16] = { 1576 102399, /* 16.666666... */ 1577 51201, /* 8.333333... */ 1578 34134, /* 5.555555... */ 1579 25599, /* 4.166666... */ 1580 17067, /* 2.777777... */ 1581 12801, /* 2.083333... */ 1582 11377, /* 1.851725... */ 1583 10239, /* 1.666666... */ 1584 8532, /* 1.388888... */ 1585 7680, /* 1.250000... */ 1586 6828, /* 1.111111... */ 1587 6144, /* 1.000000... */ 1588 5690, /* 0.926106... */ 1589 5120, /* 0.833333... */ 1590 409600, /* 66.666666... */ 1591 204800, /* 33.333333... */ 1592 }; 1593 static const u32 rates_996[3] = { 480388888, 453700000, 408333333 }; 1594 static const u32 rates_484[3] = { 229411111, 216666666, 195000000 }; 1595 static const u32 rates_242[3] = { 114711111, 108333333, 97500000 }; 1596 static const u32 rates_106[3] = { 40000000, 37777777, 34000000 }; 1597 static const u32 rates_52[3] = { 18820000, 17777777, 16000000 }; 1598 static const u32 rates_26[3] = { 9411111, 8888888, 8000000 }; 1599 u64 tmp; 1600 u32 result; 1601 1602 if (WARN_ON_ONCE(rate->mcs > 15)) 1603 return 0; 1604 if (WARN_ON_ONCE(rate->eht_gi > NL80211_RATE_INFO_EHT_GI_3_2)) 1605 return 0; 1606 if (WARN_ON_ONCE(rate->eht_ru_alloc > 1607 NL80211_RATE_INFO_EHT_RU_ALLOC_4x996)) 1608 return 0; 1609 if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8)) 1610 return 0; 1611 1612 /* Bandwidth checks for MCS 14 */ 1613 if (rate->mcs == 14) { 1614 if ((rate->bw != RATE_INFO_BW_EHT_RU && 1615 rate->bw != RATE_INFO_BW_80 && 1616 rate->bw != RATE_INFO_BW_160 && 1617 rate->bw != RATE_INFO_BW_320) || 1618 (rate->bw == RATE_INFO_BW_EHT_RU && 1619 rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_996 && 1620 rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_2x996 && 1621 rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_4x996)) { 1622 WARN(1, "invalid EHT BW for MCS 14: bw:%d, ru:%d\n", 1623 rate->bw, rate->eht_ru_alloc); 1624 return 0; 1625 } 1626 } 1627 1628 if (rate->bw == RATE_INFO_BW_320 || 1629 (rate->bw == RATE_INFO_BW_EHT_RU && 1630 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_4x996)) 1631 result = 4 * rates_996[rate->eht_gi]; 1632 else if (rate->bw == RATE_INFO_BW_EHT_RU && 1633 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_3x996P484) 1634 result = 3 * rates_996[rate->eht_gi] + rates_484[rate->eht_gi]; 1635 else if (rate->bw == RATE_INFO_BW_EHT_RU && 1636 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_3x996) 1637 result = 3 * rates_996[rate->eht_gi]; 1638 else if (rate->bw == RATE_INFO_BW_EHT_RU && 1639 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_2x996P484) 1640 result = 2 * rates_996[rate->eht_gi] + rates_484[rate->eht_gi]; 1641 else if (rate->bw == RATE_INFO_BW_160 || 1642 (rate->bw == RATE_INFO_BW_EHT_RU && 1643 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_2x996)) 1644 result = 2 * rates_996[rate->eht_gi]; 1645 else if (rate->bw == RATE_INFO_BW_EHT_RU && 1646 rate->eht_ru_alloc == 1647 NL80211_RATE_INFO_EHT_RU_ALLOC_996P484P242) 1648 result = rates_996[rate->eht_gi] + rates_484[rate->eht_gi] 1649 + rates_242[rate->eht_gi]; 1650 else if (rate->bw == RATE_INFO_BW_EHT_RU && 1651 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_996P484) 1652 result = rates_996[rate->eht_gi] + rates_484[rate->eht_gi]; 1653 else if (rate->bw == RATE_INFO_BW_80 || 1654 (rate->bw == RATE_INFO_BW_EHT_RU && 1655 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_996)) 1656 result = rates_996[rate->eht_gi]; 1657 else if (rate->bw == RATE_INFO_BW_EHT_RU && 1658 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_484P242) 1659 result = rates_484[rate->eht_gi] + rates_242[rate->eht_gi]; 1660 else if (rate->bw == RATE_INFO_BW_40 || 1661 (rate->bw == RATE_INFO_BW_EHT_RU && 1662 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_484)) 1663 result = rates_484[rate->eht_gi]; 1664 else if (rate->bw == RATE_INFO_BW_20 || 1665 (rate->bw == RATE_INFO_BW_EHT_RU && 1666 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_242)) 1667 result = rates_242[rate->eht_gi]; 1668 else if (rate->bw == RATE_INFO_BW_EHT_RU && 1669 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_106P26) 1670 result = rates_106[rate->eht_gi] + rates_26[rate->eht_gi]; 1671 else if (rate->bw == RATE_INFO_BW_EHT_RU && 1672 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_106) 1673 result = rates_106[rate->eht_gi]; 1674 else if (rate->bw == RATE_INFO_BW_EHT_RU && 1675 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_52P26) 1676 result = rates_52[rate->eht_gi] + rates_26[rate->eht_gi]; 1677 else if (rate->bw == RATE_INFO_BW_EHT_RU && 1678 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_52) 1679 result = rates_52[rate->eht_gi]; 1680 else if (rate->bw == RATE_INFO_BW_EHT_RU && 1681 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_26) 1682 result = rates_26[rate->eht_gi]; 1683 else { 1684 WARN(1, "invalid EHT MCS: bw:%d, ru:%d\n", 1685 rate->bw, rate->eht_ru_alloc); 1686 return 0; 1687 } 1688 1689 /* now scale to the appropriate MCS */ 1690 tmp = result; 1691 tmp *= SCALE; 1692 do_div(tmp, mcs_divisors[rate->mcs]); 1693 1694 /* and take NSS */ 1695 tmp *= rate->nss; 1696 do_div(tmp, 8); 1697 1698 result = tmp; 1699 1700 return result / 10000; 1701 } 1702 1703 static u32 cfg80211_calculate_bitrate_s1g(struct rate_info *rate) 1704 { 1705 /* For 1, 2, 4, 8 and 16 MHz channels */ 1706 static const u32 base[5][11] = { 1707 { 300000, 1708 600000, 1709 900000, 1710 1200000, 1711 1800000, 1712 2400000, 1713 2700000, 1714 3000000, 1715 3600000, 1716 4000000, 1717 /* MCS 10 supported in 1 MHz only */ 1718 150000, 1719 }, 1720 { 650000, 1721 1300000, 1722 1950000, 1723 2600000, 1724 3900000, 1725 5200000, 1726 5850000, 1727 6500000, 1728 7800000, 1729 /* MCS 9 not valid */ 1730 }, 1731 { 1350000, 1732 2700000, 1733 4050000, 1734 5400000, 1735 8100000, 1736 10800000, 1737 12150000, 1738 13500000, 1739 16200000, 1740 18000000, 1741 }, 1742 { 2925000, 1743 5850000, 1744 8775000, 1745 11700000, 1746 17550000, 1747 23400000, 1748 26325000, 1749 29250000, 1750 35100000, 1751 39000000, 1752 }, 1753 { 8580000, 1754 11700000, 1755 17550000, 1756 23400000, 1757 35100000, 1758 46800000, 1759 52650000, 1760 58500000, 1761 70200000, 1762 78000000, 1763 }, 1764 }; 1765 u32 bitrate; 1766 /* default is 1 MHz index */ 1767 int idx = 0; 1768 1769 if (rate->mcs >= 11) 1770 goto warn; 1771 1772 switch (rate->bw) { 1773 case RATE_INFO_BW_16: 1774 idx = 4; 1775 break; 1776 case RATE_INFO_BW_8: 1777 idx = 3; 1778 break; 1779 case RATE_INFO_BW_4: 1780 idx = 2; 1781 break; 1782 case RATE_INFO_BW_2: 1783 idx = 1; 1784 break; 1785 case RATE_INFO_BW_1: 1786 idx = 0; 1787 break; 1788 case RATE_INFO_BW_5: 1789 case RATE_INFO_BW_10: 1790 case RATE_INFO_BW_20: 1791 case RATE_INFO_BW_40: 1792 case RATE_INFO_BW_80: 1793 case RATE_INFO_BW_160: 1794 default: 1795 goto warn; 1796 } 1797 1798 bitrate = base[idx][rate->mcs]; 1799 bitrate *= rate->nss; 1800 1801 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI) 1802 bitrate = (bitrate / 9) * 10; 1803 /* do NOT round down here */ 1804 return (bitrate + 50000) / 100000; 1805 warn: 1806 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n", 1807 rate->bw, rate->mcs, rate->nss); 1808 return 0; 1809 } 1810 1811 u32 cfg80211_calculate_bitrate(struct rate_info *rate) 1812 { 1813 if (rate->flags & RATE_INFO_FLAGS_MCS) 1814 return cfg80211_calculate_bitrate_ht(rate); 1815 if (rate->flags & RATE_INFO_FLAGS_DMG) 1816 return cfg80211_calculate_bitrate_dmg(rate); 1817 if (rate->flags & RATE_INFO_FLAGS_EXTENDED_SC_DMG) 1818 return cfg80211_calculate_bitrate_extended_sc_dmg(rate); 1819 if (rate->flags & RATE_INFO_FLAGS_EDMG) 1820 return cfg80211_calculate_bitrate_edmg(rate); 1821 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS) 1822 return cfg80211_calculate_bitrate_vht(rate); 1823 if (rate->flags & RATE_INFO_FLAGS_HE_MCS) 1824 return cfg80211_calculate_bitrate_he(rate); 1825 if (rate->flags & RATE_INFO_FLAGS_EHT_MCS) 1826 return cfg80211_calculate_bitrate_eht(rate); 1827 if (rate->flags & RATE_INFO_FLAGS_S1G_MCS) 1828 return cfg80211_calculate_bitrate_s1g(rate); 1829 1830 return rate->legacy; 1831 } 1832 EXPORT_SYMBOL(cfg80211_calculate_bitrate); 1833 1834 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len, 1835 enum ieee80211_p2p_attr_id attr, 1836 u8 *buf, unsigned int bufsize) 1837 { 1838 u8 *out = buf; 1839 u16 attr_remaining = 0; 1840 bool desired_attr = false; 1841 u16 desired_len = 0; 1842 1843 while (len > 0) { 1844 unsigned int iedatalen; 1845 unsigned int copy; 1846 const u8 *iedata; 1847 1848 if (len < 2) 1849 return -EILSEQ; 1850 iedatalen = ies[1]; 1851 if (iedatalen + 2 > len) 1852 return -EILSEQ; 1853 1854 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC) 1855 goto cont; 1856 1857 if (iedatalen < 4) 1858 goto cont; 1859 1860 iedata = ies + 2; 1861 1862 /* check WFA OUI, P2P subtype */ 1863 if (iedata[0] != 0x50 || iedata[1] != 0x6f || 1864 iedata[2] != 0x9a || iedata[3] != 0x09) 1865 goto cont; 1866 1867 iedatalen -= 4; 1868 iedata += 4; 1869 1870 /* check attribute continuation into this IE */ 1871 copy = min_t(unsigned int, attr_remaining, iedatalen); 1872 if (copy && desired_attr) { 1873 desired_len += copy; 1874 if (out) { 1875 memcpy(out, iedata, min(bufsize, copy)); 1876 out += min(bufsize, copy); 1877 bufsize -= min(bufsize, copy); 1878 } 1879 1880 1881 if (copy == attr_remaining) 1882 return desired_len; 1883 } 1884 1885 attr_remaining -= copy; 1886 if (attr_remaining) 1887 goto cont; 1888 1889 iedatalen -= copy; 1890 iedata += copy; 1891 1892 while (iedatalen > 0) { 1893 u16 attr_len; 1894 1895 /* P2P attribute ID & size must fit */ 1896 if (iedatalen < 3) 1897 return -EILSEQ; 1898 desired_attr = iedata[0] == attr; 1899 attr_len = get_unaligned_le16(iedata + 1); 1900 iedatalen -= 3; 1901 iedata += 3; 1902 1903 copy = min_t(unsigned int, attr_len, iedatalen); 1904 1905 if (desired_attr) { 1906 desired_len += copy; 1907 if (out) { 1908 memcpy(out, iedata, min(bufsize, copy)); 1909 out += min(bufsize, copy); 1910 bufsize -= min(bufsize, copy); 1911 } 1912 1913 if (copy == attr_len) 1914 return desired_len; 1915 } 1916 1917 iedata += copy; 1918 iedatalen -= copy; 1919 attr_remaining = attr_len - copy; 1920 } 1921 1922 cont: 1923 len -= ies[1] + 2; 1924 ies += ies[1] + 2; 1925 } 1926 1927 if (attr_remaining && desired_attr) 1928 return -EILSEQ; 1929 1930 return -ENOENT; 1931 } 1932 EXPORT_SYMBOL(cfg80211_get_p2p_attr); 1933 1934 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext) 1935 { 1936 int i; 1937 1938 /* Make sure array values are legal */ 1939 if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION)) 1940 return false; 1941 1942 i = 0; 1943 while (i < n_ids) { 1944 if (ids[i] == WLAN_EID_EXTENSION) { 1945 if (id_ext && (ids[i + 1] == id)) 1946 return true; 1947 1948 i += 2; 1949 continue; 1950 } 1951 1952 if (ids[i] == id && !id_ext) 1953 return true; 1954 1955 i++; 1956 } 1957 return false; 1958 } 1959 1960 static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos) 1961 { 1962 /* we assume a validly formed IEs buffer */ 1963 u8 len = ies[pos + 1]; 1964 1965 pos += 2 + len; 1966 1967 /* the IE itself must have 255 bytes for fragments to follow */ 1968 if (len < 255) 1969 return pos; 1970 1971 while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) { 1972 len = ies[pos + 1]; 1973 pos += 2 + len; 1974 } 1975 1976 return pos; 1977 } 1978 1979 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen, 1980 const u8 *ids, int n_ids, 1981 const u8 *after_ric, int n_after_ric, 1982 size_t offset) 1983 { 1984 size_t pos = offset; 1985 1986 while (pos < ielen) { 1987 u8 ext = 0; 1988 1989 if (ies[pos] == WLAN_EID_EXTENSION) 1990 ext = 2; 1991 if ((pos + ext) >= ielen) 1992 break; 1993 1994 if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext], 1995 ies[pos] == WLAN_EID_EXTENSION)) 1996 break; 1997 1998 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) { 1999 pos = skip_ie(ies, ielen, pos); 2000 2001 while (pos < ielen) { 2002 if (ies[pos] == WLAN_EID_EXTENSION) 2003 ext = 2; 2004 else 2005 ext = 0; 2006 2007 if ((pos + ext) >= ielen) 2008 break; 2009 2010 if (!ieee80211_id_in_list(after_ric, 2011 n_after_ric, 2012 ies[pos + ext], 2013 ext == 2)) 2014 pos = skip_ie(ies, ielen, pos); 2015 else 2016 break; 2017 } 2018 } else { 2019 pos = skip_ie(ies, ielen, pos); 2020 } 2021 } 2022 2023 return pos; 2024 } 2025 EXPORT_SYMBOL(ieee80211_ie_split_ric); 2026 2027 void ieee80211_fragment_element(struct sk_buff *skb, u8 *len_pos, u8 frag_id) 2028 { 2029 unsigned int elem_len; 2030 2031 if (!len_pos) 2032 return; 2033 2034 elem_len = skb->data + skb->len - len_pos - 1; 2035 2036 while (elem_len > 255) { 2037 /* this one is 255 */ 2038 *len_pos = 255; 2039 /* remaining data gets smaller */ 2040 elem_len -= 255; 2041 /* make space for the fragment ID/len in SKB */ 2042 skb_put(skb, 2); 2043 /* shift back the remaining data to place fragment ID/len */ 2044 memmove(len_pos + 255 + 3, len_pos + 255 + 1, elem_len); 2045 /* place the fragment ID */ 2046 len_pos += 255 + 1; 2047 *len_pos = frag_id; 2048 /* and point to fragment length to update later */ 2049 len_pos++; 2050 } 2051 2052 *len_pos = elem_len; 2053 } 2054 EXPORT_SYMBOL(ieee80211_fragment_element); 2055 2056 bool ieee80211_operating_class_to_band(u8 operating_class, 2057 enum nl80211_band *band) 2058 { 2059 switch (operating_class) { 2060 case 112: 2061 case 115 ... 127: 2062 case 128 ... 130: 2063 *band = NL80211_BAND_5GHZ; 2064 return true; 2065 case 131 ... 135: 2066 case 137: 2067 *band = NL80211_BAND_6GHZ; 2068 return true; 2069 case 81: 2070 case 82: 2071 case 83: 2072 case 84: 2073 *band = NL80211_BAND_2GHZ; 2074 return true; 2075 case 180: 2076 *band = NL80211_BAND_60GHZ; 2077 return true; 2078 } 2079 2080 return false; 2081 } 2082 EXPORT_SYMBOL(ieee80211_operating_class_to_band); 2083 2084 bool ieee80211_operating_class_to_chandef(u8 operating_class, 2085 struct ieee80211_channel *chan, 2086 struct cfg80211_chan_def *chandef) 2087 { 2088 u32 control_freq, offset = 0; 2089 enum nl80211_band band; 2090 2091 if (!ieee80211_operating_class_to_band(operating_class, &band) || 2092 !chan || band != chan->band) 2093 return false; 2094 2095 control_freq = chan->center_freq; 2096 chandef->chan = chan; 2097 2098 if (control_freq >= 5955) 2099 offset = control_freq - 5955; 2100 else if (control_freq >= 5745) 2101 offset = control_freq - 5745; 2102 else if (control_freq >= 5180) 2103 offset = control_freq - 5180; 2104 offset /= 20; 2105 2106 switch (operating_class) { 2107 case 81: /* 2 GHz band; 20 MHz; channels 1..13 */ 2108 case 82: /* 2 GHz band; 20 MHz; channel 14 */ 2109 case 115: /* 5 GHz band; 20 MHz; channels 36,40,44,48 */ 2110 case 118: /* 5 GHz band; 20 MHz; channels 52,56,60,64 */ 2111 case 121: /* 5 GHz band; 20 MHz; channels 100..144 */ 2112 case 124: /* 5 GHz band; 20 MHz; channels 149,153,157,161 */ 2113 case 125: /* 5 GHz band; 20 MHz; channels 149..177 */ 2114 case 131: /* 6 GHz band; 20 MHz; channels 1..233*/ 2115 case 136: /* 6 GHz band; 20 MHz; channel 2 */ 2116 chandef->center_freq1 = control_freq; 2117 chandef->width = NL80211_CHAN_WIDTH_20; 2118 return true; 2119 case 83: /* 2 GHz band; 40 MHz; channels 1..9 */ 2120 case 116: /* 5 GHz band; 40 MHz; channels 36,44 */ 2121 case 119: /* 5 GHz band; 40 MHz; channels 52,60 */ 2122 case 122: /* 5 GHz band; 40 MHz; channels 100,108,116,124,132,140 */ 2123 case 126: /* 5 GHz band; 40 MHz; channels 149,157,165,173 */ 2124 chandef->center_freq1 = control_freq + 10; 2125 chandef->width = NL80211_CHAN_WIDTH_40; 2126 return true; 2127 case 84: /* 2 GHz band; 40 MHz; channels 5..13 */ 2128 case 117: /* 5 GHz band; 40 MHz; channels 40,48 */ 2129 case 120: /* 5 GHz band; 40 MHz; channels 56,64 */ 2130 case 123: /* 5 GHz band; 40 MHz; channels 104,112,120,128,136,144 */ 2131 case 127: /* 5 GHz band; 40 MHz; channels 153,161,169,177 */ 2132 chandef->center_freq1 = control_freq - 10; 2133 chandef->width = NL80211_CHAN_WIDTH_40; 2134 return true; 2135 case 132: /* 6 GHz band; 40 MHz; channels 1,5,..,229*/ 2136 chandef->center_freq1 = control_freq + 10 - (offset & 1) * 20; 2137 chandef->width = NL80211_CHAN_WIDTH_40; 2138 return true; 2139 case 128: /* 5 GHz band; 80 MHz; channels 36..64,100..144,149..177 */ 2140 case 133: /* 6 GHz band; 80 MHz; channels 1,5,..,229 */ 2141 chandef->center_freq1 = control_freq + 30 - (offset & 3) * 20; 2142 chandef->width = NL80211_CHAN_WIDTH_80; 2143 return true; 2144 case 129: /* 5 GHz band; 160 MHz; channels 36..64,100..144,149..177 */ 2145 case 134: /* 6 GHz band; 160 MHz; channels 1,5,..,229 */ 2146 chandef->center_freq1 = control_freq + 70 - (offset & 7) * 20; 2147 chandef->width = NL80211_CHAN_WIDTH_160; 2148 return true; 2149 case 130: /* 5 GHz band; 80+80 MHz; channels 36..64,100..144,149..177 */ 2150 case 135: /* 6 GHz band; 80+80 MHz; channels 1,5,..,229 */ 2151 /* The center_freq2 of 80+80 MHz is unknown */ 2152 case 137: /* 6 GHz band; 320 MHz; channels 1,5,..,229 */ 2153 /* 320-1 or 320-2 channelization is unknown */ 2154 default: 2155 return false; 2156 } 2157 } 2158 EXPORT_SYMBOL(ieee80211_operating_class_to_chandef); 2159 2160 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef, 2161 u8 *op_class) 2162 { 2163 u8 vht_opclass; 2164 u32 freq = chandef->center_freq1; 2165 2166 if (freq >= 2412 && freq <= 2472) { 2167 if (chandef->width > NL80211_CHAN_WIDTH_40) 2168 return false; 2169 2170 /* 2.407 GHz, channels 1..13 */ 2171 if (chandef->width == NL80211_CHAN_WIDTH_40) { 2172 if (freq > chandef->chan->center_freq) 2173 *op_class = 83; /* HT40+ */ 2174 else 2175 *op_class = 84; /* HT40- */ 2176 } else { 2177 *op_class = 81; 2178 } 2179 2180 return true; 2181 } 2182 2183 if (freq == 2484) { 2184 /* channel 14 is only for IEEE 802.11b */ 2185 if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT) 2186 return false; 2187 2188 *op_class = 82; /* channel 14 */ 2189 return true; 2190 } 2191 2192 switch (chandef->width) { 2193 case NL80211_CHAN_WIDTH_80: 2194 vht_opclass = 128; 2195 break; 2196 case NL80211_CHAN_WIDTH_160: 2197 vht_opclass = 129; 2198 break; 2199 case NL80211_CHAN_WIDTH_80P80: 2200 vht_opclass = 130; 2201 break; 2202 case NL80211_CHAN_WIDTH_10: 2203 case NL80211_CHAN_WIDTH_5: 2204 return false; /* unsupported for now */ 2205 default: 2206 vht_opclass = 0; 2207 break; 2208 } 2209 2210 /* 5 GHz, channels 36..48 */ 2211 if (freq >= 5180 && freq <= 5240) { 2212 if (vht_opclass) { 2213 *op_class = vht_opclass; 2214 } else if (chandef->width == NL80211_CHAN_WIDTH_40) { 2215 if (freq > chandef->chan->center_freq) 2216 *op_class = 116; 2217 else 2218 *op_class = 117; 2219 } else { 2220 *op_class = 115; 2221 } 2222 2223 return true; 2224 } 2225 2226 /* 5 GHz, channels 52..64 */ 2227 if (freq >= 5260 && freq <= 5320) { 2228 if (vht_opclass) { 2229 *op_class = vht_opclass; 2230 } else if (chandef->width == NL80211_CHAN_WIDTH_40) { 2231 if (freq > chandef->chan->center_freq) 2232 *op_class = 119; 2233 else 2234 *op_class = 120; 2235 } else { 2236 *op_class = 118; 2237 } 2238 2239 return true; 2240 } 2241 2242 /* 5 GHz, channels 100..144 */ 2243 if (freq >= 5500 && freq <= 5720) { 2244 if (vht_opclass) { 2245 *op_class = vht_opclass; 2246 } else if (chandef->width == NL80211_CHAN_WIDTH_40) { 2247 if (freq > chandef->chan->center_freq) 2248 *op_class = 122; 2249 else 2250 *op_class = 123; 2251 } else { 2252 *op_class = 121; 2253 } 2254 2255 return true; 2256 } 2257 2258 /* 5 GHz, channels 149..169 */ 2259 if (freq >= 5745 && freq <= 5845) { 2260 if (vht_opclass) { 2261 *op_class = vht_opclass; 2262 } else if (chandef->width == NL80211_CHAN_WIDTH_40) { 2263 if (freq > chandef->chan->center_freq) 2264 *op_class = 126; 2265 else 2266 *op_class = 127; 2267 } else if (freq <= 5805) { 2268 *op_class = 124; 2269 } else { 2270 *op_class = 125; 2271 } 2272 2273 return true; 2274 } 2275 2276 /* 56.16 GHz, channel 1..4 */ 2277 if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) { 2278 if (chandef->width >= NL80211_CHAN_WIDTH_40) 2279 return false; 2280 2281 *op_class = 180; 2282 return true; 2283 } 2284 2285 /* not supported yet */ 2286 return false; 2287 } 2288 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class); 2289 2290 static int cfg80211_wdev_bi(struct wireless_dev *wdev) 2291 { 2292 switch (wdev->iftype) { 2293 case NL80211_IFTYPE_AP: 2294 case NL80211_IFTYPE_P2P_GO: 2295 WARN_ON(wdev->valid_links); 2296 return wdev->links[0].ap.beacon_interval; 2297 case NL80211_IFTYPE_MESH_POINT: 2298 return wdev->u.mesh.beacon_interval; 2299 case NL80211_IFTYPE_ADHOC: 2300 return wdev->u.ibss.beacon_interval; 2301 default: 2302 break; 2303 } 2304 2305 return 0; 2306 } 2307 2308 static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int, 2309 u32 *beacon_int_gcd, 2310 bool *beacon_int_different, 2311 int radio_idx) 2312 { 2313 struct cfg80211_registered_device *rdev; 2314 struct wireless_dev *wdev; 2315 2316 *beacon_int_gcd = 0; 2317 *beacon_int_different = false; 2318 2319 rdev = wiphy_to_rdev(wiphy); 2320 list_for_each_entry(wdev, &wiphy->wdev_list, list) { 2321 int wdev_bi; 2322 2323 /* this feature isn't supported with MLO */ 2324 if (wdev->valid_links) 2325 continue; 2326 2327 /* skip wdevs not active on the given wiphy radio */ 2328 if (radio_idx >= 0 && 2329 !(rdev_get_radio_mask(rdev, wdev->netdev) & BIT(radio_idx))) 2330 continue; 2331 2332 wdev_bi = cfg80211_wdev_bi(wdev); 2333 2334 if (!wdev_bi) 2335 continue; 2336 2337 if (!*beacon_int_gcd) { 2338 *beacon_int_gcd = wdev_bi; 2339 continue; 2340 } 2341 2342 if (wdev_bi == *beacon_int_gcd) 2343 continue; 2344 2345 *beacon_int_different = true; 2346 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev_bi); 2347 } 2348 2349 if (new_beacon_int && *beacon_int_gcd != new_beacon_int) { 2350 if (*beacon_int_gcd) 2351 *beacon_int_different = true; 2352 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int); 2353 } 2354 } 2355 2356 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev, 2357 enum nl80211_iftype iftype, u32 beacon_int) 2358 { 2359 /* 2360 * This is just a basic pre-condition check; if interface combinations 2361 * are possible the driver must already be checking those with a call 2362 * to cfg80211_check_combinations(), in which case we'll validate more 2363 * through the cfg80211_calculate_bi_data() call and code in 2364 * cfg80211_iter_combinations(). 2365 */ 2366 2367 if (beacon_int < 10 || beacon_int > 10000) 2368 return -EINVAL; 2369 2370 return 0; 2371 } 2372 2373 int cfg80211_iter_combinations(struct wiphy *wiphy, 2374 struct iface_combination_params *params, 2375 void (*iter)(const struct ieee80211_iface_combination *c, 2376 void *data), 2377 void *data) 2378 { 2379 const struct wiphy_radio *radio = NULL; 2380 const struct ieee80211_iface_combination *c, *cs; 2381 const struct ieee80211_regdomain *regdom; 2382 enum nl80211_dfs_regions region = 0; 2383 int i, j, n, iftype; 2384 int num_interfaces = 0; 2385 u32 used_iftypes = 0; 2386 u32 beacon_int_gcd; 2387 bool beacon_int_different; 2388 2389 if (params->radio_idx >= 0) 2390 radio = &wiphy->radio[params->radio_idx]; 2391 2392 /* 2393 * This is a bit strange, since the iteration used to rely only on 2394 * the data given by the driver, but here it now relies on context, 2395 * in form of the currently operating interfaces. 2396 * This is OK for all current users, and saves us from having to 2397 * push the GCD calculations into all the drivers. 2398 * In the future, this should probably rely more on data that's in 2399 * cfg80211 already - the only thing not would appear to be any new 2400 * interfaces (while being brought up) and channel/radar data. 2401 */ 2402 cfg80211_calculate_bi_data(wiphy, params->new_beacon_int, 2403 &beacon_int_gcd, &beacon_int_different, 2404 params->radio_idx); 2405 2406 if (params->radar_detect) { 2407 rcu_read_lock(); 2408 regdom = rcu_dereference(cfg80211_regdomain); 2409 if (regdom) 2410 region = regdom->dfs_region; 2411 rcu_read_unlock(); 2412 } 2413 2414 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) { 2415 num_interfaces += params->iftype_num[iftype]; 2416 if (params->iftype_num[iftype] > 0 && 2417 !cfg80211_iftype_allowed(wiphy, iftype, 0, 1)) 2418 used_iftypes |= BIT(iftype); 2419 } 2420 2421 if (radio) { 2422 cs = radio->iface_combinations; 2423 n = radio->n_iface_combinations; 2424 } else { 2425 cs = wiphy->iface_combinations; 2426 n = wiphy->n_iface_combinations; 2427 } 2428 for (i = 0; i < n; i++) { 2429 struct ieee80211_iface_limit *limits; 2430 u32 all_iftypes = 0; 2431 2432 c = &cs[i]; 2433 if (num_interfaces > c->max_interfaces) 2434 continue; 2435 if (params->num_different_channels > c->num_different_channels) 2436 continue; 2437 2438 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits, 2439 GFP_KERNEL); 2440 if (!limits) 2441 return -ENOMEM; 2442 2443 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) { 2444 if (cfg80211_iftype_allowed(wiphy, iftype, 0, 1)) 2445 continue; 2446 for (j = 0; j < c->n_limits; j++) { 2447 all_iftypes |= limits[j].types; 2448 if (!(limits[j].types & BIT(iftype))) 2449 continue; 2450 if (limits[j].max < params->iftype_num[iftype]) 2451 goto cont; 2452 limits[j].max -= params->iftype_num[iftype]; 2453 } 2454 } 2455 2456 if (params->radar_detect != 2457 (c->radar_detect_widths & params->radar_detect)) 2458 goto cont; 2459 2460 if (params->radar_detect && c->radar_detect_regions && 2461 !(c->radar_detect_regions & BIT(region))) 2462 goto cont; 2463 2464 /* Finally check that all iftypes that we're currently 2465 * using are actually part of this combination. If they 2466 * aren't then we can't use this combination and have 2467 * to continue to the next. 2468 */ 2469 if ((all_iftypes & used_iftypes) != used_iftypes) 2470 goto cont; 2471 2472 if (beacon_int_gcd) { 2473 if (c->beacon_int_min_gcd && 2474 beacon_int_gcd < c->beacon_int_min_gcd) 2475 goto cont; 2476 if (!c->beacon_int_min_gcd && beacon_int_different) 2477 goto cont; 2478 } 2479 2480 /* This combination covered all interface types and 2481 * supported the requested numbers, so we're good. 2482 */ 2483 2484 (*iter)(c, data); 2485 cont: 2486 kfree(limits); 2487 } 2488 2489 return 0; 2490 } 2491 EXPORT_SYMBOL(cfg80211_iter_combinations); 2492 2493 static void 2494 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c, 2495 void *data) 2496 { 2497 int *num = data; 2498 (*num)++; 2499 } 2500 2501 int cfg80211_check_combinations(struct wiphy *wiphy, 2502 struct iface_combination_params *params) 2503 { 2504 int err, num = 0; 2505 2506 err = cfg80211_iter_combinations(wiphy, params, 2507 cfg80211_iter_sum_ifcombs, &num); 2508 if (err) 2509 return err; 2510 if (num == 0) 2511 return -EBUSY; 2512 2513 return 0; 2514 } 2515 EXPORT_SYMBOL(cfg80211_check_combinations); 2516 2517 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband, 2518 const u8 *rates, unsigned int n_rates, 2519 u32 *mask) 2520 { 2521 int i, j; 2522 2523 if (!sband) 2524 return -EINVAL; 2525 2526 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES) 2527 return -EINVAL; 2528 2529 *mask = 0; 2530 2531 for (i = 0; i < n_rates; i++) { 2532 int rate = (rates[i] & 0x7f) * 5; 2533 bool found = false; 2534 2535 for (j = 0; j < sband->n_bitrates; j++) { 2536 if (sband->bitrates[j].bitrate == rate) { 2537 found = true; 2538 *mask |= BIT(j); 2539 break; 2540 } 2541 } 2542 if (!found) 2543 return -EINVAL; 2544 } 2545 2546 /* 2547 * mask must have at least one bit set here since we 2548 * didn't accept a 0-length rates array nor allowed 2549 * entries in the array that didn't exist 2550 */ 2551 2552 return 0; 2553 } 2554 2555 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy) 2556 { 2557 enum nl80211_band band; 2558 unsigned int n_channels = 0; 2559 2560 for (band = 0; band < NUM_NL80211_BANDS; band++) 2561 if (wiphy->bands[band]) 2562 n_channels += wiphy->bands[band]->n_channels; 2563 2564 return n_channels; 2565 } 2566 EXPORT_SYMBOL(ieee80211_get_num_supported_channels); 2567 2568 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr, 2569 struct station_info *sinfo) 2570 { 2571 struct cfg80211_registered_device *rdev; 2572 struct wireless_dev *wdev; 2573 int ret; 2574 2575 wdev = dev->ieee80211_ptr; 2576 if (!wdev) 2577 return -EOPNOTSUPP; 2578 2579 rdev = wiphy_to_rdev(wdev->wiphy); 2580 if (!rdev->ops->get_station) 2581 return -EOPNOTSUPP; 2582 2583 memset(sinfo, 0, sizeof(*sinfo)); 2584 2585 wiphy_lock(&rdev->wiphy); 2586 ret = rdev_get_station(rdev, dev, mac_addr, sinfo); 2587 wiphy_unlock(&rdev->wiphy); 2588 2589 return ret; 2590 } 2591 EXPORT_SYMBOL(cfg80211_get_station); 2592 2593 void cfg80211_free_nan_func(struct cfg80211_nan_func *f) 2594 { 2595 int i; 2596 2597 if (!f) 2598 return; 2599 2600 kfree(f->serv_spec_info); 2601 kfree(f->srf_bf); 2602 kfree(f->srf_macs); 2603 for (i = 0; i < f->num_rx_filters; i++) 2604 kfree(f->rx_filters[i].filter); 2605 2606 for (i = 0; i < f->num_tx_filters; i++) 2607 kfree(f->tx_filters[i].filter); 2608 2609 kfree(f->rx_filters); 2610 kfree(f->tx_filters); 2611 kfree(f); 2612 } 2613 EXPORT_SYMBOL(cfg80211_free_nan_func); 2614 2615 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range, 2616 u32 center_freq_khz, u32 bw_khz) 2617 { 2618 u32 start_freq_khz, end_freq_khz; 2619 2620 start_freq_khz = center_freq_khz - (bw_khz / 2); 2621 end_freq_khz = center_freq_khz + (bw_khz / 2); 2622 2623 if (start_freq_khz >= freq_range->start_freq_khz && 2624 end_freq_khz <= freq_range->end_freq_khz) 2625 return true; 2626 2627 return false; 2628 } 2629 2630 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp) 2631 { 2632 sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1, 2633 sizeof(*(sinfo->pertid)), 2634 gfp); 2635 if (!sinfo->pertid) 2636 return -ENOMEM; 2637 2638 return 0; 2639 } 2640 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats); 2641 2642 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */ 2643 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */ 2644 const unsigned char rfc1042_header[] __aligned(2) = 2645 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 }; 2646 EXPORT_SYMBOL(rfc1042_header); 2647 2648 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */ 2649 const unsigned char bridge_tunnel_header[] __aligned(2) = 2650 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 }; 2651 EXPORT_SYMBOL(bridge_tunnel_header); 2652 2653 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */ 2654 struct iapp_layer2_update { 2655 u8 da[ETH_ALEN]; /* broadcast */ 2656 u8 sa[ETH_ALEN]; /* STA addr */ 2657 __be16 len; /* 6 */ 2658 u8 dsap; /* 0 */ 2659 u8 ssap; /* 0 */ 2660 u8 control; 2661 u8 xid_info[3]; 2662 } __packed; 2663 2664 void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr) 2665 { 2666 struct iapp_layer2_update *msg; 2667 struct sk_buff *skb; 2668 2669 /* Send Level 2 Update Frame to update forwarding tables in layer 2 2670 * bridge devices */ 2671 2672 skb = dev_alloc_skb(sizeof(*msg)); 2673 if (!skb) 2674 return; 2675 msg = skb_put(skb, sizeof(*msg)); 2676 2677 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID) 2678 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */ 2679 2680 eth_broadcast_addr(msg->da); 2681 ether_addr_copy(msg->sa, addr); 2682 msg->len = htons(6); 2683 msg->dsap = 0; 2684 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */ 2685 msg->control = 0xaf; /* XID response lsb.1111F101. 2686 * F=0 (no poll command; unsolicited frame) */ 2687 msg->xid_info[0] = 0x81; /* XID format identifier */ 2688 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */ 2689 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */ 2690 2691 skb->dev = dev; 2692 skb->protocol = eth_type_trans(skb, dev); 2693 memset(skb->cb, 0, sizeof(skb->cb)); 2694 netif_rx(skb); 2695 } 2696 EXPORT_SYMBOL(cfg80211_send_layer2_update); 2697 2698 int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap, 2699 enum ieee80211_vht_chanwidth bw, 2700 int mcs, bool ext_nss_bw_capable, 2701 unsigned int max_vht_nss) 2702 { 2703 u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map); 2704 int ext_nss_bw; 2705 int supp_width; 2706 int i, mcs_encoding; 2707 2708 if (map == 0xffff) 2709 return 0; 2710 2711 if (WARN_ON(mcs > 9 || max_vht_nss > 8)) 2712 return 0; 2713 if (mcs <= 7) 2714 mcs_encoding = 0; 2715 else if (mcs == 8) 2716 mcs_encoding = 1; 2717 else 2718 mcs_encoding = 2; 2719 2720 if (!max_vht_nss) { 2721 /* find max_vht_nss for the given MCS */ 2722 for (i = 7; i >= 0; i--) { 2723 int supp = (map >> (2 * i)) & 3; 2724 2725 if (supp == 3) 2726 continue; 2727 2728 if (supp >= mcs_encoding) { 2729 max_vht_nss = i + 1; 2730 break; 2731 } 2732 } 2733 } 2734 2735 if (!(cap->supp_mcs.tx_mcs_map & 2736 cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE))) 2737 return max_vht_nss; 2738 2739 ext_nss_bw = le32_get_bits(cap->vht_cap_info, 2740 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK); 2741 supp_width = le32_get_bits(cap->vht_cap_info, 2742 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK); 2743 2744 /* if not capable, treat ext_nss_bw as 0 */ 2745 if (!ext_nss_bw_capable) 2746 ext_nss_bw = 0; 2747 2748 /* This is invalid */ 2749 if (supp_width == 3) 2750 return 0; 2751 2752 /* This is an invalid combination so pretend nothing is supported */ 2753 if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2)) 2754 return 0; 2755 2756 /* 2757 * Cover all the special cases according to IEEE 802.11-2016 2758 * Table 9-250. All other cases are either factor of 1 or not 2759 * valid/supported. 2760 */ 2761 switch (bw) { 2762 case IEEE80211_VHT_CHANWIDTH_USE_HT: 2763 case IEEE80211_VHT_CHANWIDTH_80MHZ: 2764 if ((supp_width == 1 || supp_width == 2) && 2765 ext_nss_bw == 3) 2766 return 2 * max_vht_nss; 2767 break; 2768 case IEEE80211_VHT_CHANWIDTH_160MHZ: 2769 if (supp_width == 0 && 2770 (ext_nss_bw == 1 || ext_nss_bw == 2)) 2771 return max_vht_nss / 2; 2772 if (supp_width == 0 && 2773 ext_nss_bw == 3) 2774 return (3 * max_vht_nss) / 4; 2775 if (supp_width == 1 && 2776 ext_nss_bw == 3) 2777 return 2 * max_vht_nss; 2778 break; 2779 case IEEE80211_VHT_CHANWIDTH_80P80MHZ: 2780 if (supp_width == 0 && ext_nss_bw == 1) 2781 return 0; /* not possible */ 2782 if (supp_width == 0 && 2783 ext_nss_bw == 2) 2784 return max_vht_nss / 2; 2785 if (supp_width == 0 && 2786 ext_nss_bw == 3) 2787 return (3 * max_vht_nss) / 4; 2788 if (supp_width == 1 && 2789 ext_nss_bw == 0) 2790 return 0; /* not possible */ 2791 if (supp_width == 1 && 2792 ext_nss_bw == 1) 2793 return max_vht_nss / 2; 2794 if (supp_width == 1 && 2795 ext_nss_bw == 2) 2796 return (3 * max_vht_nss) / 4; 2797 break; 2798 } 2799 2800 /* not covered or invalid combination received */ 2801 return max_vht_nss; 2802 } 2803 EXPORT_SYMBOL(ieee80211_get_vht_max_nss); 2804 2805 bool cfg80211_iftype_allowed(struct wiphy *wiphy, enum nl80211_iftype iftype, 2806 bool is_4addr, u8 check_swif) 2807 2808 { 2809 bool is_vlan = iftype == NL80211_IFTYPE_AP_VLAN; 2810 2811 switch (check_swif) { 2812 case 0: 2813 if (is_vlan && is_4addr) 2814 return wiphy->flags & WIPHY_FLAG_4ADDR_AP; 2815 return wiphy->interface_modes & BIT(iftype); 2816 case 1: 2817 if (!(wiphy->software_iftypes & BIT(iftype)) && is_vlan) 2818 return wiphy->flags & WIPHY_FLAG_4ADDR_AP; 2819 return wiphy->software_iftypes & BIT(iftype); 2820 default: 2821 break; 2822 } 2823 2824 return false; 2825 } 2826 EXPORT_SYMBOL(cfg80211_iftype_allowed); 2827 2828 void cfg80211_remove_link(struct wireless_dev *wdev, unsigned int link_id) 2829 { 2830 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 2831 2832 lockdep_assert_wiphy(wdev->wiphy); 2833 2834 switch (wdev->iftype) { 2835 case NL80211_IFTYPE_AP: 2836 case NL80211_IFTYPE_P2P_GO: 2837 cfg80211_stop_ap(rdev, wdev->netdev, link_id, true); 2838 break; 2839 default: 2840 /* per-link not relevant */ 2841 break; 2842 } 2843 2844 wdev->valid_links &= ~BIT(link_id); 2845 2846 rdev_del_intf_link(rdev, wdev, link_id); 2847 2848 eth_zero_addr(wdev->links[link_id].addr); 2849 } 2850 2851 void cfg80211_remove_links(struct wireless_dev *wdev) 2852 { 2853 unsigned int link_id; 2854 2855 /* 2856 * links are controlled by upper layers (userspace/cfg) 2857 * only for AP mode, so only remove them here for AP 2858 */ 2859 if (wdev->iftype != NL80211_IFTYPE_AP) 2860 return; 2861 2862 if (wdev->valid_links) { 2863 for_each_valid_link(wdev, link_id) 2864 cfg80211_remove_link(wdev, link_id); 2865 } 2866 } 2867 2868 int cfg80211_remove_virtual_intf(struct cfg80211_registered_device *rdev, 2869 struct wireless_dev *wdev) 2870 { 2871 cfg80211_remove_links(wdev); 2872 2873 return rdev_del_virtual_intf(rdev, wdev); 2874 } 2875 2876 const struct wiphy_iftype_ext_capab * 2877 cfg80211_get_iftype_ext_capa(struct wiphy *wiphy, enum nl80211_iftype type) 2878 { 2879 int i; 2880 2881 for (i = 0; i < wiphy->num_iftype_ext_capab; i++) { 2882 if (wiphy->iftype_ext_capab[i].iftype == type) 2883 return &wiphy->iftype_ext_capab[i]; 2884 } 2885 2886 return NULL; 2887 } 2888 EXPORT_SYMBOL(cfg80211_get_iftype_ext_capa); 2889 2890 static bool 2891 ieee80211_radio_freq_range_valid(const struct wiphy_radio *radio, 2892 u32 freq, u32 width) 2893 { 2894 const struct wiphy_radio_freq_range *r; 2895 int i; 2896 2897 for (i = 0; i < radio->n_freq_range; i++) { 2898 r = &radio->freq_range[i]; 2899 if (freq - width / 2 >= r->start_freq && 2900 freq + width / 2 <= r->end_freq) 2901 return true; 2902 } 2903 2904 return false; 2905 } 2906 2907 bool cfg80211_radio_chandef_valid(const struct wiphy_radio *radio, 2908 const struct cfg80211_chan_def *chandef) 2909 { 2910 u32 freq, width; 2911 2912 freq = ieee80211_chandef_to_khz(chandef); 2913 width = nl80211_chan_width_to_mhz(chandef->width); 2914 if (!ieee80211_radio_freq_range_valid(radio, freq, width)) 2915 return false; 2916 2917 freq = MHZ_TO_KHZ(chandef->center_freq2); 2918 if (freq && !ieee80211_radio_freq_range_valid(radio, freq, width)) 2919 return false; 2920 2921 return true; 2922 } 2923 EXPORT_SYMBOL(cfg80211_radio_chandef_valid); 2924