1 /* 2 * Wireless utility functions 3 * 4 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net> 5 * Copyright 2013-2014 Intel Mobile Communications GmbH 6 */ 7 #include <linux/export.h> 8 #include <linux/bitops.h> 9 #include <linux/etherdevice.h> 10 #include <linux/slab.h> 11 #include <net/cfg80211.h> 12 #include <net/ip.h> 13 #include <net/dsfield.h> 14 #include <linux/if_vlan.h> 15 #include <linux/mpls.h> 16 #include <linux/gcd.h> 17 #include "core.h" 18 #include "rdev-ops.h" 19 20 21 struct ieee80211_rate * 22 ieee80211_get_response_rate(struct ieee80211_supported_band *sband, 23 u32 basic_rates, int bitrate) 24 { 25 struct ieee80211_rate *result = &sband->bitrates[0]; 26 int i; 27 28 for (i = 0; i < sband->n_bitrates; i++) { 29 if (!(basic_rates & BIT(i))) 30 continue; 31 if (sband->bitrates[i].bitrate > bitrate) 32 continue; 33 result = &sband->bitrates[i]; 34 } 35 36 return result; 37 } 38 EXPORT_SYMBOL(ieee80211_get_response_rate); 39 40 u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband, 41 enum nl80211_bss_scan_width scan_width) 42 { 43 struct ieee80211_rate *bitrates; 44 u32 mandatory_rates = 0; 45 enum ieee80211_rate_flags mandatory_flag; 46 int i; 47 48 if (WARN_ON(!sband)) 49 return 1; 50 51 if (sband->band == NL80211_BAND_2GHZ) { 52 if (scan_width == NL80211_BSS_CHAN_WIDTH_5 || 53 scan_width == NL80211_BSS_CHAN_WIDTH_10) 54 mandatory_flag = IEEE80211_RATE_MANDATORY_G; 55 else 56 mandatory_flag = IEEE80211_RATE_MANDATORY_B; 57 } else { 58 mandatory_flag = IEEE80211_RATE_MANDATORY_A; 59 } 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 int ieee80211_channel_to_frequency(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 if (chan == 14) 78 return 2484; 79 else if (chan < 14) 80 return 2407 + chan * 5; 81 break; 82 case NL80211_BAND_5GHZ: 83 if (chan >= 182 && chan <= 196) 84 return 4000 + chan * 5; 85 else 86 return 5000 + chan * 5; 87 break; 88 case NL80211_BAND_60GHZ: 89 if (chan < 5) 90 return 56160 + chan * 2160; 91 break; 92 default: 93 ; 94 } 95 return 0; /* not supported */ 96 } 97 EXPORT_SYMBOL(ieee80211_channel_to_frequency); 98 99 int ieee80211_frequency_to_channel(int freq) 100 { 101 /* see 802.11 17.3.8.3.2 and Annex J */ 102 if (freq == 2484) 103 return 14; 104 else if (freq < 2484) 105 return (freq - 2407) / 5; 106 else if (freq >= 4910 && freq <= 4980) 107 return (freq - 4000) / 5; 108 else if (freq <= 45000) /* DMG band lower limit */ 109 return (freq - 5000) / 5; 110 else if (freq >= 58320 && freq <= 64800) 111 return (freq - 56160) / 2160; 112 else 113 return 0; 114 } 115 EXPORT_SYMBOL(ieee80211_frequency_to_channel); 116 117 struct ieee80211_channel *ieee80211_get_channel(struct wiphy *wiphy, int freq) 118 { 119 enum nl80211_band band; 120 struct ieee80211_supported_band *sband; 121 int i; 122 123 for (band = 0; band < NUM_NL80211_BANDS; band++) { 124 sband = wiphy->bands[band]; 125 126 if (!sband) 127 continue; 128 129 for (i = 0; i < sband->n_channels; i++) { 130 if (sband->channels[i].center_freq == freq) 131 return &sband->channels[i]; 132 } 133 } 134 135 return NULL; 136 } 137 EXPORT_SYMBOL(ieee80211_get_channel); 138 139 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband) 140 { 141 int i, want; 142 143 switch (sband->band) { 144 case NL80211_BAND_5GHZ: 145 want = 3; 146 for (i = 0; i < sband->n_bitrates; i++) { 147 if (sband->bitrates[i].bitrate == 60 || 148 sband->bitrates[i].bitrate == 120 || 149 sband->bitrates[i].bitrate == 240) { 150 sband->bitrates[i].flags |= 151 IEEE80211_RATE_MANDATORY_A; 152 want--; 153 } 154 } 155 WARN_ON(want); 156 break; 157 case NL80211_BAND_2GHZ: 158 want = 7; 159 for (i = 0; i < sband->n_bitrates; i++) { 160 switch (sband->bitrates[i].bitrate) { 161 case 10: 162 case 20: 163 case 55: 164 case 110: 165 sband->bitrates[i].flags |= 166 IEEE80211_RATE_MANDATORY_B | 167 IEEE80211_RATE_MANDATORY_G; 168 want--; 169 break; 170 case 60: 171 case 120: 172 case 240: 173 sband->bitrates[i].flags |= 174 IEEE80211_RATE_MANDATORY_G; 175 want--; 176 /* fall through */ 177 default: 178 sband->bitrates[i].flags |= 179 IEEE80211_RATE_ERP_G; 180 break; 181 } 182 } 183 WARN_ON(want != 0 && want != 3); 184 break; 185 case NL80211_BAND_60GHZ: 186 /* check for mandatory HT MCS 1..4 */ 187 WARN_ON(!sband->ht_cap.ht_supported); 188 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e); 189 break; 190 case NUM_NL80211_BANDS: 191 default: 192 WARN_ON(1); 193 break; 194 } 195 } 196 197 void ieee80211_set_bitrate_flags(struct wiphy *wiphy) 198 { 199 enum nl80211_band band; 200 201 for (band = 0; band < NUM_NL80211_BANDS; band++) 202 if (wiphy->bands[band]) 203 set_mandatory_flags_band(wiphy->bands[band]); 204 } 205 206 bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher) 207 { 208 int i; 209 for (i = 0; i < wiphy->n_cipher_suites; i++) 210 if (cipher == wiphy->cipher_suites[i]) 211 return true; 212 return false; 213 } 214 215 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev, 216 struct key_params *params, int key_idx, 217 bool pairwise, const u8 *mac_addr) 218 { 219 if (key_idx < 0 || key_idx > 5) 220 return -EINVAL; 221 222 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)) 223 return -EINVAL; 224 225 if (pairwise && !mac_addr) 226 return -EINVAL; 227 228 switch (params->cipher) { 229 case WLAN_CIPHER_SUITE_TKIP: 230 case WLAN_CIPHER_SUITE_CCMP: 231 case WLAN_CIPHER_SUITE_CCMP_256: 232 case WLAN_CIPHER_SUITE_GCMP: 233 case WLAN_CIPHER_SUITE_GCMP_256: 234 /* Disallow pairwise keys with non-zero index unless it's WEP 235 * or a vendor specific cipher (because current deployments use 236 * pairwise WEP keys with non-zero indices and for vendor 237 * specific ciphers this should be validated in the driver or 238 * hardware level - but 802.11i clearly specifies to use zero) 239 */ 240 if (pairwise && key_idx) 241 return -EINVAL; 242 break; 243 case WLAN_CIPHER_SUITE_AES_CMAC: 244 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 245 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 246 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 247 /* Disallow BIP (group-only) cipher as pairwise cipher */ 248 if (pairwise) 249 return -EINVAL; 250 if (key_idx < 4) 251 return -EINVAL; 252 break; 253 case WLAN_CIPHER_SUITE_WEP40: 254 case WLAN_CIPHER_SUITE_WEP104: 255 if (key_idx > 3) 256 return -EINVAL; 257 default: 258 break; 259 } 260 261 switch (params->cipher) { 262 case WLAN_CIPHER_SUITE_WEP40: 263 if (params->key_len != WLAN_KEY_LEN_WEP40) 264 return -EINVAL; 265 break; 266 case WLAN_CIPHER_SUITE_TKIP: 267 if (params->key_len != WLAN_KEY_LEN_TKIP) 268 return -EINVAL; 269 break; 270 case WLAN_CIPHER_SUITE_CCMP: 271 if (params->key_len != WLAN_KEY_LEN_CCMP) 272 return -EINVAL; 273 break; 274 case WLAN_CIPHER_SUITE_CCMP_256: 275 if (params->key_len != WLAN_KEY_LEN_CCMP_256) 276 return -EINVAL; 277 break; 278 case WLAN_CIPHER_SUITE_GCMP: 279 if (params->key_len != WLAN_KEY_LEN_GCMP) 280 return -EINVAL; 281 break; 282 case WLAN_CIPHER_SUITE_GCMP_256: 283 if (params->key_len != WLAN_KEY_LEN_GCMP_256) 284 return -EINVAL; 285 break; 286 case WLAN_CIPHER_SUITE_WEP104: 287 if (params->key_len != WLAN_KEY_LEN_WEP104) 288 return -EINVAL; 289 break; 290 case WLAN_CIPHER_SUITE_AES_CMAC: 291 if (params->key_len != WLAN_KEY_LEN_AES_CMAC) 292 return -EINVAL; 293 break; 294 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 295 if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256) 296 return -EINVAL; 297 break; 298 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 299 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128) 300 return -EINVAL; 301 break; 302 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 303 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256) 304 return -EINVAL; 305 break; 306 default: 307 /* 308 * We don't know anything about this algorithm, 309 * allow using it -- but the driver must check 310 * all parameters! We still check below whether 311 * or not the driver supports this algorithm, 312 * of course. 313 */ 314 break; 315 } 316 317 if (params->seq) { 318 switch (params->cipher) { 319 case WLAN_CIPHER_SUITE_WEP40: 320 case WLAN_CIPHER_SUITE_WEP104: 321 /* These ciphers do not use key sequence */ 322 return -EINVAL; 323 case WLAN_CIPHER_SUITE_TKIP: 324 case WLAN_CIPHER_SUITE_CCMP: 325 case WLAN_CIPHER_SUITE_CCMP_256: 326 case WLAN_CIPHER_SUITE_GCMP: 327 case WLAN_CIPHER_SUITE_GCMP_256: 328 case WLAN_CIPHER_SUITE_AES_CMAC: 329 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 330 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 331 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 332 if (params->seq_len != 6) 333 return -EINVAL; 334 break; 335 } 336 } 337 338 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher)) 339 return -EINVAL; 340 341 return 0; 342 } 343 344 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc) 345 { 346 unsigned int hdrlen = 24; 347 348 if (ieee80211_is_data(fc)) { 349 if (ieee80211_has_a4(fc)) 350 hdrlen = 30; 351 if (ieee80211_is_data_qos(fc)) { 352 hdrlen += IEEE80211_QOS_CTL_LEN; 353 if (ieee80211_has_order(fc)) 354 hdrlen += IEEE80211_HT_CTL_LEN; 355 } 356 goto out; 357 } 358 359 if (ieee80211_is_mgmt(fc)) { 360 if (ieee80211_has_order(fc)) 361 hdrlen += IEEE80211_HT_CTL_LEN; 362 goto out; 363 } 364 365 if (ieee80211_is_ctl(fc)) { 366 /* 367 * ACK and CTS are 10 bytes, all others 16. To see how 368 * to get this condition consider 369 * subtype mask: 0b0000000011110000 (0x00F0) 370 * ACK subtype: 0b0000000011010000 (0x00D0) 371 * CTS subtype: 0b0000000011000000 (0x00C0) 372 * bits that matter: ^^^ (0x00E0) 373 * value of those: 0b0000000011000000 (0x00C0) 374 */ 375 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0)) 376 hdrlen = 10; 377 else 378 hdrlen = 16; 379 } 380 out: 381 return hdrlen; 382 } 383 EXPORT_SYMBOL(ieee80211_hdrlen); 384 385 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb) 386 { 387 const struct ieee80211_hdr *hdr = 388 (const struct ieee80211_hdr *)skb->data; 389 unsigned int hdrlen; 390 391 if (unlikely(skb->len < 10)) 392 return 0; 393 hdrlen = ieee80211_hdrlen(hdr->frame_control); 394 if (unlikely(hdrlen > skb->len)) 395 return 0; 396 return hdrlen; 397 } 398 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb); 399 400 static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags) 401 { 402 int ae = flags & MESH_FLAGS_AE; 403 /* 802.11-2012, 8.2.4.7.3 */ 404 switch (ae) { 405 default: 406 case 0: 407 return 6; 408 case MESH_FLAGS_AE_A4: 409 return 12; 410 case MESH_FLAGS_AE_A5_A6: 411 return 18; 412 } 413 } 414 415 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr) 416 { 417 return __ieee80211_get_mesh_hdrlen(meshhdr->flags); 418 } 419 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen); 420 421 int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr, 422 const u8 *addr, enum nl80211_iftype iftype) 423 { 424 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; 425 struct { 426 u8 hdr[ETH_ALEN] __aligned(2); 427 __be16 proto; 428 } payload; 429 struct ethhdr tmp; 430 u16 hdrlen; 431 u8 mesh_flags = 0; 432 433 if (unlikely(!ieee80211_is_data_present(hdr->frame_control))) 434 return -1; 435 436 hdrlen = ieee80211_hdrlen(hdr->frame_control); 437 if (skb->len < hdrlen + 8) 438 return -1; 439 440 /* convert IEEE 802.11 header + possible LLC headers into Ethernet 441 * header 442 * IEEE 802.11 address fields: 443 * ToDS FromDS Addr1 Addr2 Addr3 Addr4 444 * 0 0 DA SA BSSID n/a 445 * 0 1 DA BSSID SA n/a 446 * 1 0 BSSID SA DA n/a 447 * 1 1 RA TA DA SA 448 */ 449 memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN); 450 memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN); 451 452 if (iftype == NL80211_IFTYPE_MESH_POINT) 453 skb_copy_bits(skb, hdrlen, &mesh_flags, 1); 454 455 mesh_flags &= MESH_FLAGS_AE; 456 457 switch (hdr->frame_control & 458 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) { 459 case cpu_to_le16(IEEE80211_FCTL_TODS): 460 if (unlikely(iftype != NL80211_IFTYPE_AP && 461 iftype != NL80211_IFTYPE_AP_VLAN && 462 iftype != NL80211_IFTYPE_P2P_GO)) 463 return -1; 464 break; 465 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS): 466 if (unlikely(iftype != NL80211_IFTYPE_WDS && 467 iftype != NL80211_IFTYPE_MESH_POINT && 468 iftype != NL80211_IFTYPE_AP_VLAN && 469 iftype != NL80211_IFTYPE_STATION)) 470 return -1; 471 if (iftype == NL80211_IFTYPE_MESH_POINT) { 472 if (mesh_flags == MESH_FLAGS_AE_A4) 473 return -1; 474 if (mesh_flags == MESH_FLAGS_AE_A5_A6) { 475 skb_copy_bits(skb, hdrlen + 476 offsetof(struct ieee80211s_hdr, eaddr1), 477 tmp.h_dest, 2 * ETH_ALEN); 478 } 479 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags); 480 } 481 break; 482 case cpu_to_le16(IEEE80211_FCTL_FROMDS): 483 if ((iftype != NL80211_IFTYPE_STATION && 484 iftype != NL80211_IFTYPE_P2P_CLIENT && 485 iftype != NL80211_IFTYPE_MESH_POINT) || 486 (is_multicast_ether_addr(tmp.h_dest) && 487 ether_addr_equal(tmp.h_source, addr))) 488 return -1; 489 if (iftype == NL80211_IFTYPE_MESH_POINT) { 490 if (mesh_flags == MESH_FLAGS_AE_A5_A6) 491 return -1; 492 if (mesh_flags == MESH_FLAGS_AE_A4) 493 skb_copy_bits(skb, hdrlen + 494 offsetof(struct ieee80211s_hdr, eaddr1), 495 tmp.h_source, ETH_ALEN); 496 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags); 497 } 498 break; 499 case cpu_to_le16(0): 500 if (iftype != NL80211_IFTYPE_ADHOC && 501 iftype != NL80211_IFTYPE_STATION && 502 iftype != NL80211_IFTYPE_OCB) 503 return -1; 504 break; 505 } 506 507 skb_copy_bits(skb, hdrlen, &payload, sizeof(payload)); 508 tmp.h_proto = payload.proto; 509 510 if (likely((ether_addr_equal(payload.hdr, rfc1042_header) && 511 tmp.h_proto != htons(ETH_P_AARP) && 512 tmp.h_proto != htons(ETH_P_IPX)) || 513 ether_addr_equal(payload.hdr, bridge_tunnel_header))) 514 /* remove RFC1042 or Bridge-Tunnel encapsulation and 515 * replace EtherType */ 516 hdrlen += ETH_ALEN + 2; 517 else 518 tmp.h_proto = htons(skb->len - hdrlen); 519 520 pskb_pull(skb, hdrlen); 521 522 if (!ehdr) 523 ehdr = skb_push(skb, sizeof(struct ethhdr)); 524 memcpy(ehdr, &tmp, sizeof(tmp)); 525 526 return 0; 527 } 528 EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr); 529 530 static void 531 __frame_add_frag(struct sk_buff *skb, struct page *page, 532 void *ptr, int len, int size) 533 { 534 struct skb_shared_info *sh = skb_shinfo(skb); 535 int page_offset; 536 537 page_ref_inc(page); 538 page_offset = ptr - page_address(page); 539 skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size); 540 } 541 542 static void 543 __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame, 544 int offset, int len) 545 { 546 struct skb_shared_info *sh = skb_shinfo(skb); 547 const skb_frag_t *frag = &sh->frags[0]; 548 struct page *frag_page; 549 void *frag_ptr; 550 int frag_len, frag_size; 551 int head_size = skb->len - skb->data_len; 552 int cur_len; 553 554 frag_page = virt_to_head_page(skb->head); 555 frag_ptr = skb->data; 556 frag_size = head_size; 557 558 while (offset >= frag_size) { 559 offset -= frag_size; 560 frag_page = skb_frag_page(frag); 561 frag_ptr = skb_frag_address(frag); 562 frag_size = skb_frag_size(frag); 563 frag++; 564 } 565 566 frag_ptr += offset; 567 frag_len = frag_size - offset; 568 569 cur_len = min(len, frag_len); 570 571 __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size); 572 len -= cur_len; 573 574 while (len > 0) { 575 frag_len = skb_frag_size(frag); 576 cur_len = min(len, frag_len); 577 __frame_add_frag(frame, skb_frag_page(frag), 578 skb_frag_address(frag), cur_len, frag_len); 579 len -= cur_len; 580 frag++; 581 } 582 } 583 584 static struct sk_buff * 585 __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen, 586 int offset, int len, bool reuse_frag) 587 { 588 struct sk_buff *frame; 589 int cur_len = len; 590 591 if (skb->len - offset < len) 592 return NULL; 593 594 /* 595 * When reusing framents, copy some data to the head to simplify 596 * ethernet header handling and speed up protocol header processing 597 * in the stack later. 598 */ 599 if (reuse_frag) 600 cur_len = min_t(int, len, 32); 601 602 /* 603 * Allocate and reserve two bytes more for payload 604 * alignment since sizeof(struct ethhdr) is 14. 605 */ 606 frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len); 607 if (!frame) 608 return NULL; 609 610 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2); 611 skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len); 612 613 len -= cur_len; 614 if (!len) 615 return frame; 616 617 offset += cur_len; 618 __ieee80211_amsdu_copy_frag(skb, frame, offset, len); 619 620 return frame; 621 } 622 623 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list, 624 const u8 *addr, enum nl80211_iftype iftype, 625 const unsigned int extra_headroom, 626 const u8 *check_da, const u8 *check_sa) 627 { 628 unsigned int hlen = ALIGN(extra_headroom, 4); 629 struct sk_buff *frame = NULL; 630 u16 ethertype; 631 u8 *payload; 632 int offset = 0, remaining; 633 struct ethhdr eth; 634 bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb); 635 bool reuse_skb = false; 636 bool last = false; 637 638 while (!last) { 639 unsigned int subframe_len; 640 int len; 641 u8 padding; 642 643 skb_copy_bits(skb, offset, ð, sizeof(eth)); 644 len = ntohs(eth.h_proto); 645 subframe_len = sizeof(struct ethhdr) + len; 646 padding = (4 - subframe_len) & 0x3; 647 648 /* the last MSDU has no padding */ 649 remaining = skb->len - offset; 650 if (subframe_len > remaining) 651 goto purge; 652 653 offset += sizeof(struct ethhdr); 654 last = remaining <= subframe_len + padding; 655 656 /* FIXME: should we really accept multicast DA? */ 657 if ((check_da && !is_multicast_ether_addr(eth.h_dest) && 658 !ether_addr_equal(check_da, eth.h_dest)) || 659 (check_sa && !ether_addr_equal(check_sa, eth.h_source))) { 660 offset += len + padding; 661 continue; 662 } 663 664 /* reuse skb for the last subframe */ 665 if (!skb_is_nonlinear(skb) && !reuse_frag && last) { 666 skb_pull(skb, offset); 667 frame = skb; 668 reuse_skb = true; 669 } else { 670 frame = __ieee80211_amsdu_copy(skb, hlen, offset, len, 671 reuse_frag); 672 if (!frame) 673 goto purge; 674 675 offset += len + padding; 676 } 677 678 skb_reset_network_header(frame); 679 frame->dev = skb->dev; 680 frame->priority = skb->priority; 681 682 payload = frame->data; 683 ethertype = (payload[6] << 8) | payload[7]; 684 if (likely((ether_addr_equal(payload, rfc1042_header) && 685 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) || 686 ether_addr_equal(payload, bridge_tunnel_header))) { 687 eth.h_proto = htons(ethertype); 688 skb_pull(frame, ETH_ALEN + 2); 689 } 690 691 memcpy(skb_push(frame, sizeof(eth)), ð, sizeof(eth)); 692 __skb_queue_tail(list, frame); 693 } 694 695 if (!reuse_skb) 696 dev_kfree_skb(skb); 697 698 return; 699 700 purge: 701 __skb_queue_purge(list); 702 dev_kfree_skb(skb); 703 } 704 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s); 705 706 /* Given a data frame determine the 802.1p/1d tag to use. */ 707 unsigned int cfg80211_classify8021d(struct sk_buff *skb, 708 struct cfg80211_qos_map *qos_map) 709 { 710 unsigned int dscp; 711 unsigned char vlan_priority; 712 713 /* skb->priority values from 256->263 are magic values to 714 * directly indicate a specific 802.1d priority. This is used 715 * to allow 802.1d priority to be passed directly in from VLAN 716 * tags, etc. 717 */ 718 if (skb->priority >= 256 && skb->priority <= 263) 719 return skb->priority - 256; 720 721 if (skb_vlan_tag_present(skb)) { 722 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK) 723 >> VLAN_PRIO_SHIFT; 724 if (vlan_priority > 0) 725 return vlan_priority; 726 } 727 728 switch (skb->protocol) { 729 case htons(ETH_P_IP): 730 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc; 731 break; 732 case htons(ETH_P_IPV6): 733 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc; 734 break; 735 case htons(ETH_P_MPLS_UC): 736 case htons(ETH_P_MPLS_MC): { 737 struct mpls_label mpls_tmp, *mpls; 738 739 mpls = skb_header_pointer(skb, sizeof(struct ethhdr), 740 sizeof(*mpls), &mpls_tmp); 741 if (!mpls) 742 return 0; 743 744 return (ntohl(mpls->entry) & MPLS_LS_TC_MASK) 745 >> MPLS_LS_TC_SHIFT; 746 } 747 case htons(ETH_P_80221): 748 /* 802.21 is always network control traffic */ 749 return 7; 750 default: 751 return 0; 752 } 753 754 if (qos_map) { 755 unsigned int i, tmp_dscp = dscp >> 2; 756 757 for (i = 0; i < qos_map->num_des; i++) { 758 if (tmp_dscp == qos_map->dscp_exception[i].dscp) 759 return qos_map->dscp_exception[i].up; 760 } 761 762 for (i = 0; i < 8; i++) { 763 if (tmp_dscp >= qos_map->up[i].low && 764 tmp_dscp <= qos_map->up[i].high) 765 return i; 766 } 767 } 768 769 return dscp >> 5; 770 } 771 EXPORT_SYMBOL(cfg80211_classify8021d); 772 773 const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie) 774 { 775 const struct cfg80211_bss_ies *ies; 776 777 ies = rcu_dereference(bss->ies); 778 if (!ies) 779 return NULL; 780 781 return cfg80211_find_ie(ie, ies->data, ies->len); 782 } 783 EXPORT_SYMBOL(ieee80211_bss_get_ie); 784 785 void cfg80211_upload_connect_keys(struct wireless_dev *wdev) 786 { 787 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 788 struct net_device *dev = wdev->netdev; 789 int i; 790 791 if (!wdev->connect_keys) 792 return; 793 794 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) { 795 if (!wdev->connect_keys->params[i].cipher) 796 continue; 797 if (rdev_add_key(rdev, dev, i, false, NULL, 798 &wdev->connect_keys->params[i])) { 799 netdev_err(dev, "failed to set key %d\n", i); 800 continue; 801 } 802 if (wdev->connect_keys->def == i && 803 rdev_set_default_key(rdev, dev, i, true, true)) { 804 netdev_err(dev, "failed to set defkey %d\n", i); 805 continue; 806 } 807 } 808 809 kzfree(wdev->connect_keys); 810 wdev->connect_keys = NULL; 811 } 812 813 void cfg80211_process_wdev_events(struct wireless_dev *wdev) 814 { 815 struct cfg80211_event *ev; 816 unsigned long flags; 817 818 spin_lock_irqsave(&wdev->event_lock, flags); 819 while (!list_empty(&wdev->event_list)) { 820 ev = list_first_entry(&wdev->event_list, 821 struct cfg80211_event, list); 822 list_del(&ev->list); 823 spin_unlock_irqrestore(&wdev->event_lock, flags); 824 825 wdev_lock(wdev); 826 switch (ev->type) { 827 case EVENT_CONNECT_RESULT: 828 __cfg80211_connect_result( 829 wdev->netdev, 830 &ev->cr, 831 ev->cr.status == WLAN_STATUS_SUCCESS); 832 break; 833 case EVENT_ROAMED: 834 __cfg80211_roamed(wdev, &ev->rm); 835 break; 836 case EVENT_DISCONNECTED: 837 __cfg80211_disconnected(wdev->netdev, 838 ev->dc.ie, ev->dc.ie_len, 839 ev->dc.reason, 840 !ev->dc.locally_generated); 841 break; 842 case EVENT_IBSS_JOINED: 843 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid, 844 ev->ij.channel); 845 break; 846 case EVENT_STOPPED: 847 __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev); 848 break; 849 case EVENT_PORT_AUTHORIZED: 850 __cfg80211_port_authorized(wdev, ev->pa.bssid); 851 break; 852 } 853 wdev_unlock(wdev); 854 855 kfree(ev); 856 857 spin_lock_irqsave(&wdev->event_lock, flags); 858 } 859 spin_unlock_irqrestore(&wdev->event_lock, flags); 860 } 861 862 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev) 863 { 864 struct wireless_dev *wdev; 865 866 ASSERT_RTNL(); 867 868 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) 869 cfg80211_process_wdev_events(wdev); 870 } 871 872 int cfg80211_change_iface(struct cfg80211_registered_device *rdev, 873 struct net_device *dev, enum nl80211_iftype ntype, 874 struct vif_params *params) 875 { 876 int err; 877 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype; 878 879 ASSERT_RTNL(); 880 881 /* don't support changing VLANs, you just re-create them */ 882 if (otype == NL80211_IFTYPE_AP_VLAN) 883 return -EOPNOTSUPP; 884 885 /* cannot change into P2P device or NAN */ 886 if (ntype == NL80211_IFTYPE_P2P_DEVICE || 887 ntype == NL80211_IFTYPE_NAN) 888 return -EOPNOTSUPP; 889 890 if (!rdev->ops->change_virtual_intf || 891 !(rdev->wiphy.interface_modes & (1 << ntype))) 892 return -EOPNOTSUPP; 893 894 /* if it's part of a bridge, reject changing type to station/ibss */ 895 if ((dev->priv_flags & IFF_BRIDGE_PORT) && 896 (ntype == NL80211_IFTYPE_ADHOC || 897 ntype == NL80211_IFTYPE_STATION || 898 ntype == NL80211_IFTYPE_P2P_CLIENT)) 899 return -EBUSY; 900 901 if (ntype != otype) { 902 dev->ieee80211_ptr->use_4addr = false; 903 dev->ieee80211_ptr->mesh_id_up_len = 0; 904 wdev_lock(dev->ieee80211_ptr); 905 rdev_set_qos_map(rdev, dev, NULL); 906 wdev_unlock(dev->ieee80211_ptr); 907 908 switch (otype) { 909 case NL80211_IFTYPE_AP: 910 cfg80211_stop_ap(rdev, dev, true); 911 break; 912 case NL80211_IFTYPE_ADHOC: 913 cfg80211_leave_ibss(rdev, dev, false); 914 break; 915 case NL80211_IFTYPE_STATION: 916 case NL80211_IFTYPE_P2P_CLIENT: 917 wdev_lock(dev->ieee80211_ptr); 918 cfg80211_disconnect(rdev, dev, 919 WLAN_REASON_DEAUTH_LEAVING, true); 920 wdev_unlock(dev->ieee80211_ptr); 921 break; 922 case NL80211_IFTYPE_MESH_POINT: 923 /* mesh should be handled? */ 924 break; 925 default: 926 break; 927 } 928 929 cfg80211_process_rdev_events(rdev); 930 } 931 932 err = rdev_change_virtual_intf(rdev, dev, ntype, params); 933 934 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype); 935 936 if (!err && params && params->use_4addr != -1) 937 dev->ieee80211_ptr->use_4addr = params->use_4addr; 938 939 if (!err) { 940 dev->priv_flags &= ~IFF_DONT_BRIDGE; 941 switch (ntype) { 942 case NL80211_IFTYPE_STATION: 943 if (dev->ieee80211_ptr->use_4addr) 944 break; 945 /* fall through */ 946 case NL80211_IFTYPE_OCB: 947 case NL80211_IFTYPE_P2P_CLIENT: 948 case NL80211_IFTYPE_ADHOC: 949 dev->priv_flags |= IFF_DONT_BRIDGE; 950 break; 951 case NL80211_IFTYPE_P2P_GO: 952 case NL80211_IFTYPE_AP: 953 case NL80211_IFTYPE_AP_VLAN: 954 case NL80211_IFTYPE_WDS: 955 case NL80211_IFTYPE_MESH_POINT: 956 /* bridging OK */ 957 break; 958 case NL80211_IFTYPE_MONITOR: 959 /* monitor can't bridge anyway */ 960 break; 961 case NL80211_IFTYPE_UNSPECIFIED: 962 case NUM_NL80211_IFTYPES: 963 /* not happening */ 964 break; 965 case NL80211_IFTYPE_P2P_DEVICE: 966 case NL80211_IFTYPE_NAN: 967 WARN_ON(1); 968 break; 969 } 970 } 971 972 if (!err && ntype != otype && netif_running(dev)) { 973 cfg80211_update_iface_num(rdev, ntype, 1); 974 cfg80211_update_iface_num(rdev, otype, -1); 975 } 976 977 return err; 978 } 979 980 static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate) 981 { 982 int modulation, streams, bitrate; 983 984 /* the formula below does only work for MCS values smaller than 32 */ 985 if (WARN_ON_ONCE(rate->mcs >= 32)) 986 return 0; 987 988 modulation = rate->mcs & 7; 989 streams = (rate->mcs >> 3) + 1; 990 991 bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000; 992 993 if (modulation < 4) 994 bitrate *= (modulation + 1); 995 else if (modulation == 4) 996 bitrate *= (modulation + 2); 997 else 998 bitrate *= (modulation + 3); 999 1000 bitrate *= streams; 1001 1002 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI) 1003 bitrate = (bitrate / 9) * 10; 1004 1005 /* do NOT round down here */ 1006 return (bitrate + 50000) / 100000; 1007 } 1008 1009 static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate) 1010 { 1011 static const u32 __mcs2bitrate[] = { 1012 /* control PHY */ 1013 [0] = 275, 1014 /* SC PHY */ 1015 [1] = 3850, 1016 [2] = 7700, 1017 [3] = 9625, 1018 [4] = 11550, 1019 [5] = 12512, /* 1251.25 mbps */ 1020 [6] = 15400, 1021 [7] = 19250, 1022 [8] = 23100, 1023 [9] = 25025, 1024 [10] = 30800, 1025 [11] = 38500, 1026 [12] = 46200, 1027 /* OFDM PHY */ 1028 [13] = 6930, 1029 [14] = 8662, /* 866.25 mbps */ 1030 [15] = 13860, 1031 [16] = 17325, 1032 [17] = 20790, 1033 [18] = 27720, 1034 [19] = 34650, 1035 [20] = 41580, 1036 [21] = 45045, 1037 [22] = 51975, 1038 [23] = 62370, 1039 [24] = 67568, /* 6756.75 mbps */ 1040 /* LP-SC PHY */ 1041 [25] = 6260, 1042 [26] = 8340, 1043 [27] = 11120, 1044 [28] = 12510, 1045 [29] = 16680, 1046 [30] = 22240, 1047 [31] = 25030, 1048 }; 1049 1050 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate))) 1051 return 0; 1052 1053 return __mcs2bitrate[rate->mcs]; 1054 } 1055 1056 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate) 1057 { 1058 static const u32 base[4][10] = { 1059 { 6500000, 1060 13000000, 1061 19500000, 1062 26000000, 1063 39000000, 1064 52000000, 1065 58500000, 1066 65000000, 1067 78000000, 1068 /* not in the spec, but some devices use this: */ 1069 86500000, 1070 }, 1071 { 13500000, 1072 27000000, 1073 40500000, 1074 54000000, 1075 81000000, 1076 108000000, 1077 121500000, 1078 135000000, 1079 162000000, 1080 180000000, 1081 }, 1082 { 29300000, 1083 58500000, 1084 87800000, 1085 117000000, 1086 175500000, 1087 234000000, 1088 263300000, 1089 292500000, 1090 351000000, 1091 390000000, 1092 }, 1093 { 58500000, 1094 117000000, 1095 175500000, 1096 234000000, 1097 351000000, 1098 468000000, 1099 526500000, 1100 585000000, 1101 702000000, 1102 780000000, 1103 }, 1104 }; 1105 u32 bitrate; 1106 int idx; 1107 1108 if (rate->mcs > 9) 1109 goto warn; 1110 1111 switch (rate->bw) { 1112 case RATE_INFO_BW_160: 1113 idx = 3; 1114 break; 1115 case RATE_INFO_BW_80: 1116 idx = 2; 1117 break; 1118 case RATE_INFO_BW_40: 1119 idx = 1; 1120 break; 1121 case RATE_INFO_BW_5: 1122 case RATE_INFO_BW_10: 1123 default: 1124 goto warn; 1125 case RATE_INFO_BW_20: 1126 idx = 0; 1127 } 1128 1129 bitrate = base[idx][rate->mcs]; 1130 bitrate *= rate->nss; 1131 1132 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI) 1133 bitrate = (bitrate / 9) * 10; 1134 1135 /* do NOT round down here */ 1136 return (bitrate + 50000) / 100000; 1137 warn: 1138 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n", 1139 rate->bw, rate->mcs, rate->nss); 1140 return 0; 1141 } 1142 1143 u32 cfg80211_calculate_bitrate(struct rate_info *rate) 1144 { 1145 if (rate->flags & RATE_INFO_FLAGS_MCS) 1146 return cfg80211_calculate_bitrate_ht(rate); 1147 if (rate->flags & RATE_INFO_FLAGS_60G) 1148 return cfg80211_calculate_bitrate_60g(rate); 1149 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS) 1150 return cfg80211_calculate_bitrate_vht(rate); 1151 1152 return rate->legacy; 1153 } 1154 EXPORT_SYMBOL(cfg80211_calculate_bitrate); 1155 1156 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len, 1157 enum ieee80211_p2p_attr_id attr, 1158 u8 *buf, unsigned int bufsize) 1159 { 1160 u8 *out = buf; 1161 u16 attr_remaining = 0; 1162 bool desired_attr = false; 1163 u16 desired_len = 0; 1164 1165 while (len > 0) { 1166 unsigned int iedatalen; 1167 unsigned int copy; 1168 const u8 *iedata; 1169 1170 if (len < 2) 1171 return -EILSEQ; 1172 iedatalen = ies[1]; 1173 if (iedatalen + 2 > len) 1174 return -EILSEQ; 1175 1176 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC) 1177 goto cont; 1178 1179 if (iedatalen < 4) 1180 goto cont; 1181 1182 iedata = ies + 2; 1183 1184 /* check WFA OUI, P2P subtype */ 1185 if (iedata[0] != 0x50 || iedata[1] != 0x6f || 1186 iedata[2] != 0x9a || iedata[3] != 0x09) 1187 goto cont; 1188 1189 iedatalen -= 4; 1190 iedata += 4; 1191 1192 /* check attribute continuation into this IE */ 1193 copy = min_t(unsigned int, attr_remaining, iedatalen); 1194 if (copy && desired_attr) { 1195 desired_len += copy; 1196 if (out) { 1197 memcpy(out, iedata, min(bufsize, copy)); 1198 out += min(bufsize, copy); 1199 bufsize -= min(bufsize, copy); 1200 } 1201 1202 1203 if (copy == attr_remaining) 1204 return desired_len; 1205 } 1206 1207 attr_remaining -= copy; 1208 if (attr_remaining) 1209 goto cont; 1210 1211 iedatalen -= copy; 1212 iedata += copy; 1213 1214 while (iedatalen > 0) { 1215 u16 attr_len; 1216 1217 /* P2P attribute ID & size must fit */ 1218 if (iedatalen < 3) 1219 return -EILSEQ; 1220 desired_attr = iedata[0] == attr; 1221 attr_len = get_unaligned_le16(iedata + 1); 1222 iedatalen -= 3; 1223 iedata += 3; 1224 1225 copy = min_t(unsigned int, attr_len, iedatalen); 1226 1227 if (desired_attr) { 1228 desired_len += copy; 1229 if (out) { 1230 memcpy(out, iedata, min(bufsize, copy)); 1231 out += min(bufsize, copy); 1232 bufsize -= min(bufsize, copy); 1233 } 1234 1235 if (copy == attr_len) 1236 return desired_len; 1237 } 1238 1239 iedata += copy; 1240 iedatalen -= copy; 1241 attr_remaining = attr_len - copy; 1242 } 1243 1244 cont: 1245 len -= ies[1] + 2; 1246 ies += ies[1] + 2; 1247 } 1248 1249 if (attr_remaining && desired_attr) 1250 return -EILSEQ; 1251 1252 return -ENOENT; 1253 } 1254 EXPORT_SYMBOL(cfg80211_get_p2p_attr); 1255 1256 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext) 1257 { 1258 int i; 1259 1260 /* Make sure array values are legal */ 1261 if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION)) 1262 return false; 1263 1264 i = 0; 1265 while (i < n_ids) { 1266 if (ids[i] == WLAN_EID_EXTENSION) { 1267 if (id_ext && (ids[i + 1] == id)) 1268 return true; 1269 1270 i += 2; 1271 continue; 1272 } 1273 1274 if (ids[i] == id && !id_ext) 1275 return true; 1276 1277 i++; 1278 } 1279 return false; 1280 } 1281 1282 static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos) 1283 { 1284 /* we assume a validly formed IEs buffer */ 1285 u8 len = ies[pos + 1]; 1286 1287 pos += 2 + len; 1288 1289 /* the IE itself must have 255 bytes for fragments to follow */ 1290 if (len < 255) 1291 return pos; 1292 1293 while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) { 1294 len = ies[pos + 1]; 1295 pos += 2 + len; 1296 } 1297 1298 return pos; 1299 } 1300 1301 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen, 1302 const u8 *ids, int n_ids, 1303 const u8 *after_ric, int n_after_ric, 1304 size_t offset) 1305 { 1306 size_t pos = offset; 1307 1308 while (pos < ielen) { 1309 u8 ext = 0; 1310 1311 if (ies[pos] == WLAN_EID_EXTENSION) 1312 ext = 2; 1313 if ((pos + ext) >= ielen) 1314 break; 1315 1316 if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext], 1317 ies[pos] == WLAN_EID_EXTENSION)) 1318 break; 1319 1320 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) { 1321 pos = skip_ie(ies, ielen, pos); 1322 1323 while (pos < ielen) { 1324 if (ies[pos] == WLAN_EID_EXTENSION) 1325 ext = 2; 1326 else 1327 ext = 0; 1328 1329 if ((pos + ext) >= ielen) 1330 break; 1331 1332 if (!ieee80211_id_in_list(after_ric, 1333 n_after_ric, 1334 ies[pos + ext], 1335 ext == 2)) 1336 pos = skip_ie(ies, ielen, pos); 1337 } 1338 } else { 1339 pos = skip_ie(ies, ielen, pos); 1340 } 1341 } 1342 1343 return pos; 1344 } 1345 EXPORT_SYMBOL(ieee80211_ie_split_ric); 1346 1347 bool ieee80211_operating_class_to_band(u8 operating_class, 1348 enum nl80211_band *band) 1349 { 1350 switch (operating_class) { 1351 case 112: 1352 case 115 ... 127: 1353 case 128 ... 130: 1354 *band = NL80211_BAND_5GHZ; 1355 return true; 1356 case 81: 1357 case 82: 1358 case 83: 1359 case 84: 1360 *band = NL80211_BAND_2GHZ; 1361 return true; 1362 case 180: 1363 *band = NL80211_BAND_60GHZ; 1364 return true; 1365 } 1366 1367 return false; 1368 } 1369 EXPORT_SYMBOL(ieee80211_operating_class_to_band); 1370 1371 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef, 1372 u8 *op_class) 1373 { 1374 u8 vht_opclass; 1375 u16 freq = chandef->center_freq1; 1376 1377 if (freq >= 2412 && freq <= 2472) { 1378 if (chandef->width > NL80211_CHAN_WIDTH_40) 1379 return false; 1380 1381 /* 2.407 GHz, channels 1..13 */ 1382 if (chandef->width == NL80211_CHAN_WIDTH_40) { 1383 if (freq > chandef->chan->center_freq) 1384 *op_class = 83; /* HT40+ */ 1385 else 1386 *op_class = 84; /* HT40- */ 1387 } else { 1388 *op_class = 81; 1389 } 1390 1391 return true; 1392 } 1393 1394 if (freq == 2484) { 1395 if (chandef->width > NL80211_CHAN_WIDTH_40) 1396 return false; 1397 1398 *op_class = 82; /* channel 14 */ 1399 return true; 1400 } 1401 1402 switch (chandef->width) { 1403 case NL80211_CHAN_WIDTH_80: 1404 vht_opclass = 128; 1405 break; 1406 case NL80211_CHAN_WIDTH_160: 1407 vht_opclass = 129; 1408 break; 1409 case NL80211_CHAN_WIDTH_80P80: 1410 vht_opclass = 130; 1411 break; 1412 case NL80211_CHAN_WIDTH_10: 1413 case NL80211_CHAN_WIDTH_5: 1414 return false; /* unsupported for now */ 1415 default: 1416 vht_opclass = 0; 1417 break; 1418 } 1419 1420 /* 5 GHz, channels 36..48 */ 1421 if (freq >= 5180 && freq <= 5240) { 1422 if (vht_opclass) { 1423 *op_class = vht_opclass; 1424 } else if (chandef->width == NL80211_CHAN_WIDTH_40) { 1425 if (freq > chandef->chan->center_freq) 1426 *op_class = 116; 1427 else 1428 *op_class = 117; 1429 } else { 1430 *op_class = 115; 1431 } 1432 1433 return true; 1434 } 1435 1436 /* 5 GHz, channels 52..64 */ 1437 if (freq >= 5260 && freq <= 5320) { 1438 if (vht_opclass) { 1439 *op_class = vht_opclass; 1440 } else if (chandef->width == NL80211_CHAN_WIDTH_40) { 1441 if (freq > chandef->chan->center_freq) 1442 *op_class = 119; 1443 else 1444 *op_class = 120; 1445 } else { 1446 *op_class = 118; 1447 } 1448 1449 return true; 1450 } 1451 1452 /* 5 GHz, channels 100..144 */ 1453 if (freq >= 5500 && freq <= 5720) { 1454 if (vht_opclass) { 1455 *op_class = vht_opclass; 1456 } else if (chandef->width == NL80211_CHAN_WIDTH_40) { 1457 if (freq > chandef->chan->center_freq) 1458 *op_class = 122; 1459 else 1460 *op_class = 123; 1461 } else { 1462 *op_class = 121; 1463 } 1464 1465 return true; 1466 } 1467 1468 /* 5 GHz, channels 149..169 */ 1469 if (freq >= 5745 && freq <= 5845) { 1470 if (vht_opclass) { 1471 *op_class = vht_opclass; 1472 } else if (chandef->width == NL80211_CHAN_WIDTH_40) { 1473 if (freq > chandef->chan->center_freq) 1474 *op_class = 126; 1475 else 1476 *op_class = 127; 1477 } else if (freq <= 5805) { 1478 *op_class = 124; 1479 } else { 1480 *op_class = 125; 1481 } 1482 1483 return true; 1484 } 1485 1486 /* 56.16 GHz, channel 1..4 */ 1487 if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 4) { 1488 if (chandef->width >= NL80211_CHAN_WIDTH_40) 1489 return false; 1490 1491 *op_class = 180; 1492 return true; 1493 } 1494 1495 /* not supported yet */ 1496 return false; 1497 } 1498 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class); 1499 1500 static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int, 1501 u32 *beacon_int_gcd, 1502 bool *beacon_int_different) 1503 { 1504 struct wireless_dev *wdev; 1505 1506 *beacon_int_gcd = 0; 1507 *beacon_int_different = false; 1508 1509 list_for_each_entry(wdev, &wiphy->wdev_list, list) { 1510 if (!wdev->beacon_interval) 1511 continue; 1512 1513 if (!*beacon_int_gcd) { 1514 *beacon_int_gcd = wdev->beacon_interval; 1515 continue; 1516 } 1517 1518 if (wdev->beacon_interval == *beacon_int_gcd) 1519 continue; 1520 1521 *beacon_int_different = true; 1522 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval); 1523 } 1524 1525 if (new_beacon_int && *beacon_int_gcd != new_beacon_int) { 1526 if (*beacon_int_gcd) 1527 *beacon_int_different = true; 1528 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int); 1529 } 1530 } 1531 1532 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev, 1533 enum nl80211_iftype iftype, u32 beacon_int) 1534 { 1535 /* 1536 * This is just a basic pre-condition check; if interface combinations 1537 * are possible the driver must already be checking those with a call 1538 * to cfg80211_check_combinations(), in which case we'll validate more 1539 * through the cfg80211_calculate_bi_data() call and code in 1540 * cfg80211_iter_combinations(). 1541 */ 1542 1543 if (beacon_int < 10 || beacon_int > 10000) 1544 return -EINVAL; 1545 1546 return 0; 1547 } 1548 1549 int cfg80211_iter_combinations(struct wiphy *wiphy, 1550 struct iface_combination_params *params, 1551 void (*iter)(const struct ieee80211_iface_combination *c, 1552 void *data), 1553 void *data) 1554 { 1555 const struct ieee80211_regdomain *regdom; 1556 enum nl80211_dfs_regions region = 0; 1557 int i, j, iftype; 1558 int num_interfaces = 0; 1559 u32 used_iftypes = 0; 1560 u32 beacon_int_gcd; 1561 bool beacon_int_different; 1562 1563 /* 1564 * This is a bit strange, since the iteration used to rely only on 1565 * the data given by the driver, but here it now relies on context, 1566 * in form of the currently operating interfaces. 1567 * This is OK for all current users, and saves us from having to 1568 * push the GCD calculations into all the drivers. 1569 * In the future, this should probably rely more on data that's in 1570 * cfg80211 already - the only thing not would appear to be any new 1571 * interfaces (while being brought up) and channel/radar data. 1572 */ 1573 cfg80211_calculate_bi_data(wiphy, params->new_beacon_int, 1574 &beacon_int_gcd, &beacon_int_different); 1575 1576 if (params->radar_detect) { 1577 rcu_read_lock(); 1578 regdom = rcu_dereference(cfg80211_regdomain); 1579 if (regdom) 1580 region = regdom->dfs_region; 1581 rcu_read_unlock(); 1582 } 1583 1584 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) { 1585 num_interfaces += params->iftype_num[iftype]; 1586 if (params->iftype_num[iftype] > 0 && 1587 !(wiphy->software_iftypes & BIT(iftype))) 1588 used_iftypes |= BIT(iftype); 1589 } 1590 1591 for (i = 0; i < wiphy->n_iface_combinations; i++) { 1592 const struct ieee80211_iface_combination *c; 1593 struct ieee80211_iface_limit *limits; 1594 u32 all_iftypes = 0; 1595 1596 c = &wiphy->iface_combinations[i]; 1597 1598 if (num_interfaces > c->max_interfaces) 1599 continue; 1600 if (params->num_different_channels > c->num_different_channels) 1601 continue; 1602 1603 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits, 1604 GFP_KERNEL); 1605 if (!limits) 1606 return -ENOMEM; 1607 1608 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) { 1609 if (wiphy->software_iftypes & BIT(iftype)) 1610 continue; 1611 for (j = 0; j < c->n_limits; j++) { 1612 all_iftypes |= limits[j].types; 1613 if (!(limits[j].types & BIT(iftype))) 1614 continue; 1615 if (limits[j].max < params->iftype_num[iftype]) 1616 goto cont; 1617 limits[j].max -= params->iftype_num[iftype]; 1618 } 1619 } 1620 1621 if (params->radar_detect != 1622 (c->radar_detect_widths & params->radar_detect)) 1623 goto cont; 1624 1625 if (params->radar_detect && c->radar_detect_regions && 1626 !(c->radar_detect_regions & BIT(region))) 1627 goto cont; 1628 1629 /* Finally check that all iftypes that we're currently 1630 * using are actually part of this combination. If they 1631 * aren't then we can't use this combination and have 1632 * to continue to the next. 1633 */ 1634 if ((all_iftypes & used_iftypes) != used_iftypes) 1635 goto cont; 1636 1637 if (beacon_int_gcd) { 1638 if (c->beacon_int_min_gcd && 1639 beacon_int_gcd < c->beacon_int_min_gcd) 1640 goto cont; 1641 if (!c->beacon_int_min_gcd && beacon_int_different) 1642 goto cont; 1643 } 1644 1645 /* This combination covered all interface types and 1646 * supported the requested numbers, so we're good. 1647 */ 1648 1649 (*iter)(c, data); 1650 cont: 1651 kfree(limits); 1652 } 1653 1654 return 0; 1655 } 1656 EXPORT_SYMBOL(cfg80211_iter_combinations); 1657 1658 static void 1659 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c, 1660 void *data) 1661 { 1662 int *num = data; 1663 (*num)++; 1664 } 1665 1666 int cfg80211_check_combinations(struct wiphy *wiphy, 1667 struct iface_combination_params *params) 1668 { 1669 int err, num = 0; 1670 1671 err = cfg80211_iter_combinations(wiphy, params, 1672 cfg80211_iter_sum_ifcombs, &num); 1673 if (err) 1674 return err; 1675 if (num == 0) 1676 return -EBUSY; 1677 1678 return 0; 1679 } 1680 EXPORT_SYMBOL(cfg80211_check_combinations); 1681 1682 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband, 1683 const u8 *rates, unsigned int n_rates, 1684 u32 *mask) 1685 { 1686 int i, j; 1687 1688 if (!sband) 1689 return -EINVAL; 1690 1691 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES) 1692 return -EINVAL; 1693 1694 *mask = 0; 1695 1696 for (i = 0; i < n_rates; i++) { 1697 int rate = (rates[i] & 0x7f) * 5; 1698 bool found = false; 1699 1700 for (j = 0; j < sband->n_bitrates; j++) { 1701 if (sband->bitrates[j].bitrate == rate) { 1702 found = true; 1703 *mask |= BIT(j); 1704 break; 1705 } 1706 } 1707 if (!found) 1708 return -EINVAL; 1709 } 1710 1711 /* 1712 * mask must have at least one bit set here since we 1713 * didn't accept a 0-length rates array nor allowed 1714 * entries in the array that didn't exist 1715 */ 1716 1717 return 0; 1718 } 1719 1720 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy) 1721 { 1722 enum nl80211_band band; 1723 unsigned int n_channels = 0; 1724 1725 for (band = 0; band < NUM_NL80211_BANDS; band++) 1726 if (wiphy->bands[band]) 1727 n_channels += wiphy->bands[band]->n_channels; 1728 1729 return n_channels; 1730 } 1731 EXPORT_SYMBOL(ieee80211_get_num_supported_channels); 1732 1733 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr, 1734 struct station_info *sinfo) 1735 { 1736 struct cfg80211_registered_device *rdev; 1737 struct wireless_dev *wdev; 1738 1739 wdev = dev->ieee80211_ptr; 1740 if (!wdev) 1741 return -EOPNOTSUPP; 1742 1743 rdev = wiphy_to_rdev(wdev->wiphy); 1744 if (!rdev->ops->get_station) 1745 return -EOPNOTSUPP; 1746 1747 return rdev_get_station(rdev, dev, mac_addr, sinfo); 1748 } 1749 EXPORT_SYMBOL(cfg80211_get_station); 1750 1751 void cfg80211_free_nan_func(struct cfg80211_nan_func *f) 1752 { 1753 int i; 1754 1755 if (!f) 1756 return; 1757 1758 kfree(f->serv_spec_info); 1759 kfree(f->srf_bf); 1760 kfree(f->srf_macs); 1761 for (i = 0; i < f->num_rx_filters; i++) 1762 kfree(f->rx_filters[i].filter); 1763 1764 for (i = 0; i < f->num_tx_filters; i++) 1765 kfree(f->tx_filters[i].filter); 1766 1767 kfree(f->rx_filters); 1768 kfree(f->tx_filters); 1769 kfree(f); 1770 } 1771 EXPORT_SYMBOL(cfg80211_free_nan_func); 1772 1773 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range, 1774 u32 center_freq_khz, u32 bw_khz) 1775 { 1776 u32 start_freq_khz, end_freq_khz; 1777 1778 start_freq_khz = center_freq_khz - (bw_khz / 2); 1779 end_freq_khz = center_freq_khz + (bw_khz / 2); 1780 1781 if (start_freq_khz >= freq_range->start_freq_khz && 1782 end_freq_khz <= freq_range->end_freq_khz) 1783 return true; 1784 1785 return false; 1786 } 1787 1788 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */ 1789 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */ 1790 const unsigned char rfc1042_header[] __aligned(2) = 1791 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 }; 1792 EXPORT_SYMBOL(rfc1042_header); 1793 1794 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */ 1795 const unsigned char bridge_tunnel_header[] __aligned(2) = 1796 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 }; 1797 EXPORT_SYMBOL(bridge_tunnel_header); 1798