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