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