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