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