fib: fix fib_rules_ops indirect calls wrappers
[linux-block.git] / net / wireless / util.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
8318d78a
JB
2/*
3 * Wireless utility functions
4 *
d3236553 5 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
2740f0cf 6 * Copyright 2013-2014 Intel Mobile Communications GmbH
c4cbaf79 7 * Copyright 2017 Intel Deutschland GmbH
9166cc49 8 * Copyright (C) 2018-2020 Intel Corporation
8318d78a 9 */
bc3b2d7f 10#include <linux/export.h>
d3236553 11#include <linux/bitops.h>
e31a16d6 12#include <linux/etherdevice.h>
5a0e3ad6 13#include <linux/slab.h>
b0aa75f0 14#include <linux/ieee80211.h>
d3236553 15#include <net/cfg80211.h>
e31a16d6 16#include <net/ip.h>
b156579b 17#include <net/dsfield.h>
c6ca5e28 18#include <linux/if_vlan.h>
960d97f9 19#include <linux/mpls.h>
4c8dea63 20#include <linux/gcd.h>
b0aa75f0 21#include <linux/bitfield.h>
1fc9b725 22#include <linux/nospec.h>
8318d78a 23#include "core.h"
e35e4d28
HG
24#include "rdev-ops.h"
25
8318d78a 26
bd815252
JB
27struct ieee80211_rate *
28ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
881d948c 29 u32 basic_rates, int bitrate)
bd815252
JB
30{
31 struct ieee80211_rate *result = &sband->bitrates[0];
32 int i;
33
34 for (i = 0; i < sband->n_bitrates; i++) {
35 if (!(basic_rates & BIT(i)))
36 continue;
37 if (sband->bitrates[i].bitrate > bitrate)
38 continue;
39 result = &sband->bitrates[i];
40 }
41
42 return result;
43}
44EXPORT_SYMBOL(ieee80211_get_response_rate);
45
74608aca
SW
46u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband,
47 enum nl80211_bss_scan_width scan_width)
b422c6cd
AN
48{
49 struct ieee80211_rate *bitrates;
50 u32 mandatory_rates = 0;
51 enum ieee80211_rate_flags mandatory_flag;
52 int i;
53
54 if (WARN_ON(!sband))
55 return 1;
56
57fbcce3 57 if (sband->band == NL80211_BAND_2GHZ) {
74608aca
SW
58 if (scan_width == NL80211_BSS_CHAN_WIDTH_5 ||
59 scan_width == NL80211_BSS_CHAN_WIDTH_10)
60 mandatory_flag = IEEE80211_RATE_MANDATORY_G;
61 else
62 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
63 } else {
b422c6cd 64 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
74608aca 65 }
b422c6cd
AN
66
67 bitrates = sband->bitrates;
68 for (i = 0; i < sband->n_bitrates; i++)
69 if (bitrates[i].flags & mandatory_flag)
70 mandatory_rates |= BIT(i);
71 return mandatory_rates;
72}
73EXPORT_SYMBOL(ieee80211_mandatory_rates);
74
934f4c7d 75u32 ieee80211_channel_to_freq_khz(int chan, enum nl80211_band band)
8318d78a 76{
59eb21a6
BR
77 /* see 802.11 17.3.8.3.2 and Annex J
78 * there are overlapping channel numbers in 5GHz and 2GHz bands */
3a0c52a6
VK
79 if (chan <= 0)
80 return 0; /* not supported */
81 switch (band) {
57fbcce3 82 case NL80211_BAND_2GHZ:
59eb21a6 83 if (chan == 14)
934f4c7d 84 return MHZ_TO_KHZ(2484);
59eb21a6 85 else if (chan < 14)
934f4c7d 86 return MHZ_TO_KHZ(2407 + chan * 5);
3a0c52a6 87 break;
57fbcce3 88 case NL80211_BAND_5GHZ:
3a0c52a6 89 if (chan >= 182 && chan <= 196)
934f4c7d 90 return MHZ_TO_KHZ(4000 + chan * 5);
59eb21a6 91 else
934f4c7d 92 return MHZ_TO_KHZ(5000 + chan * 5);
3a0c52a6 93 break;
fa1f1085 94 case NL80211_BAND_6GHZ:
d1a1646c
AVS
95 /* see 802.11ax D6.1 27.3.23.2 */
96 if (chan == 2)
97 return MHZ_TO_KHZ(5935);
fa1f1085 98 if (chan <= 253)
d1a1646c 99 return MHZ_TO_KHZ(5950 + chan * 5);
fa1f1085 100 break;
57fbcce3 101 case NL80211_BAND_60GHZ:
9cf0a0b4 102 if (chan < 7)
934f4c7d 103 return MHZ_TO_KHZ(56160 + chan * 2160);
3a0c52a6
VK
104 break;
105 default:
106 ;
59eb21a6 107 }
3a0c52a6 108 return 0; /* not supported */
8318d78a 109}
934f4c7d 110EXPORT_SYMBOL(ieee80211_channel_to_freq_khz);
8318d78a 111
934f4c7d 112int ieee80211_freq_khz_to_channel(u32 freq)
8318d78a 113{
934f4c7d
TP
114 /* TODO: just handle MHz for now */
115 freq = KHZ_TO_MHZ(freq);
116
59eb21a6 117 /* see 802.11 17.3.8.3.2 and Annex J */
8318d78a
JB
118 if (freq == 2484)
119 return 14;
59eb21a6 120 else if (freq < 2484)
8318d78a 121 return (freq - 2407) / 5;
59eb21a6
BR
122 else if (freq >= 4910 && freq <= 4980)
123 return (freq - 4000) / 5;
df5d7a88 124 else if (freq < 5945)
59eb21a6 125 return (freq - 5000) / 5;
fa1f1085
AS
126 else if (freq <= 45000) /* DMG band lower limit */
127 /* see 802.11ax D4.1 27.3.22.2 */
128 return (freq - 5940) / 5;
9cf0a0b4 129 else if (freq >= 58320 && freq <= 70200)
3a0c52a6
VK
130 return (freq - 56160) / 2160;
131 else
132 return 0;
8318d78a 133}
934f4c7d 134EXPORT_SYMBOL(ieee80211_freq_khz_to_channel);
8318d78a 135
934f4c7d
TP
136struct ieee80211_channel *ieee80211_get_channel_khz(struct wiphy *wiphy,
137 u32 freq)
906c730a 138{
57fbcce3 139 enum nl80211_band band;
906c730a
JB
140 struct ieee80211_supported_band *sband;
141 int i;
142
57fbcce3 143 for (band = 0; band < NUM_NL80211_BANDS; band++) {
906c730a
JB
144 sband = wiphy->bands[band];
145
146 if (!sband)
147 continue;
148
149 for (i = 0; i < sband->n_channels; i++) {
934f4c7d
TP
150 struct ieee80211_channel *chan = &sband->channels[i];
151
152 if (ieee80211_channel_to_khz(chan) == freq)
153 return chan;
906c730a
JB
154 }
155 }
156
157 return NULL;
158}
934f4c7d 159EXPORT_SYMBOL(ieee80211_get_channel_khz);
906c730a 160
343884c8 161static void set_mandatory_flags_band(struct ieee80211_supported_band *sband)
8318d78a
JB
162{
163 int i, want;
164
343884c8 165 switch (sband->band) {
57fbcce3 166 case NL80211_BAND_5GHZ:
62524a58 167 case NL80211_BAND_6GHZ:
8318d78a
JB
168 want = 3;
169 for (i = 0; i < sband->n_bitrates; i++) {
170 if (sband->bitrates[i].bitrate == 60 ||
171 sband->bitrates[i].bitrate == 120 ||
172 sband->bitrates[i].bitrate == 240) {
173 sband->bitrates[i].flags |=
174 IEEE80211_RATE_MANDATORY_A;
175 want--;
176 }
177 }
178 WARN_ON(want);
179 break;
57fbcce3 180 case NL80211_BAND_2GHZ:
8318d78a
JB
181 want = 7;
182 for (i = 0; i < sband->n_bitrates; i++) {
1bd773c0
RS
183 switch (sband->bitrates[i].bitrate) {
184 case 10:
185 case 20:
186 case 55:
187 case 110:
8318d78a
JB
188 sband->bitrates[i].flags |=
189 IEEE80211_RATE_MANDATORY_B |
190 IEEE80211_RATE_MANDATORY_G;
191 want--;
1bd773c0
RS
192 break;
193 case 60:
194 case 120:
195 case 240:
8318d78a
JB
196 sband->bitrates[i].flags |=
197 IEEE80211_RATE_MANDATORY_G;
198 want--;
1bd773c0
RS
199 /* fall through */
200 default:
8318d78a
JB
201 sband->bitrates[i].flags |=
202 IEEE80211_RATE_ERP_G;
1bd773c0
RS
203 break;
204 }
8318d78a 205 }
1bd773c0 206 WARN_ON(want != 0 && want != 3);
8318d78a 207 break;
57fbcce3 208 case NL80211_BAND_60GHZ:
3a0c52a6
VK
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;
57fbcce3 213 case NUM_NL80211_BANDS:
343884c8 214 default:
8318d78a
JB
215 WARN_ON(1);
216 break;
217 }
218}
219
220void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
221{
57fbcce3 222 enum nl80211_band band;
8318d78a 223
57fbcce3 224 for (band = 0; band < NUM_NL80211_BANDS; band++)
8318d78a 225 if (wiphy->bands[band])
343884c8 226 set_mandatory_flags_band(wiphy->bands[band]);
8318d78a 227}
08645126 228
38ba3c57
JM
229bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
230{
231 int i;
232 for (i = 0; i < wiphy->n_cipher_suites; i++)
233 if (cipher == wiphy->cipher_suites[i])
234 return true;
235 return false;
236}
237
fffd0934
JB
238int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
239 struct key_params *params, int key_idx,
e31b8213 240 bool pairwise, const u8 *mac_addr)
08645126 241{
56be393f
JM
242 int max_key_idx = 5;
243
244 if (wiphy_ext_feature_isset(&rdev->wiphy,
0e47901d
JB
245 NL80211_EXT_FEATURE_BEACON_PROTECTION) ||
246 wiphy_ext_feature_isset(&rdev->wiphy,
247 NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
56be393f
JM
248 max_key_idx = 7;
249 if (key_idx < 0 || key_idx > max_key_idx)
08645126
JB
250 return -EINVAL;
251
e31b8213
JB
252 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
253 return -EINVAL;
254
255 if (pairwise && !mac_addr)
256 return -EINVAL;
257
37720569
JM
258 switch (params->cipher) {
259 case WLAN_CIPHER_SUITE_TKIP:
b67fd72e
AW
260 /* Extended Key ID can only be used with CCMP/GCMP ciphers */
261 if ((pairwise && key_idx) ||
262 params->mode != NL80211_KEY_RX_TX)
263 return -EINVAL;
264 break;
37720569 265 case WLAN_CIPHER_SUITE_CCMP:
cfcf1682
JM
266 case WLAN_CIPHER_SUITE_CCMP_256:
267 case WLAN_CIPHER_SUITE_GCMP:
268 case WLAN_CIPHER_SUITE_GCMP_256:
b67fd72e
AW
269 /* IEEE802.11-2016 allows only 0 and - when supporting
270 * Extended Key ID - 1 as index for pairwise keys.
6cdd3979
AW
271 * @NL80211_KEY_NO_TX is only allowed for pairwise keys when
272 * the driver supports Extended Key ID.
273 * @NL80211_KEY_SET_TX can't be set when installing and
274 * validating a key.
37720569 275 */
b67fd72e
AW
276 if ((params->mode == NL80211_KEY_NO_TX && !pairwise) ||
277 params->mode == NL80211_KEY_SET_TX)
278 return -EINVAL;
279 if (wiphy_ext_feature_isset(&rdev->wiphy,
280 NL80211_EXT_FEATURE_EXT_KEY_ID)) {
281 if (pairwise && (key_idx < 0 || key_idx > 1))
6cdd3979 282 return -EINVAL;
b67fd72e 283 } else if (pairwise && key_idx) {
37720569 284 return -EINVAL;
6cdd3979 285 }
37720569
JM
286 break;
287 case WLAN_CIPHER_SUITE_AES_CMAC:
cfcf1682
JM
288 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
289 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
290 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
37720569
JM
291 /* Disallow BIP (group-only) cipher as pairwise cipher */
292 if (pairwise)
293 return -EINVAL;
e9c8f8d3
JB
294 if (key_idx < 4)
295 return -EINVAL;
37720569 296 break;
e9c8f8d3
JB
297 case WLAN_CIPHER_SUITE_WEP40:
298 case WLAN_CIPHER_SUITE_WEP104:
299 if (key_idx > 3)
300 return -EINVAL;
37720569
JM
301 default:
302 break;
303 }
08645126 304
08645126
JB
305 switch (params->cipher) {
306 case WLAN_CIPHER_SUITE_WEP40:
8fc0fee0 307 if (params->key_len != WLAN_KEY_LEN_WEP40)
08645126
JB
308 return -EINVAL;
309 break;
310 case WLAN_CIPHER_SUITE_TKIP:
8fc0fee0 311 if (params->key_len != WLAN_KEY_LEN_TKIP)
08645126
JB
312 return -EINVAL;
313 break;
314 case WLAN_CIPHER_SUITE_CCMP:
8fc0fee0 315 if (params->key_len != WLAN_KEY_LEN_CCMP)
08645126
JB
316 return -EINVAL;
317 break;
cfcf1682
JM
318 case WLAN_CIPHER_SUITE_CCMP_256:
319 if (params->key_len != WLAN_KEY_LEN_CCMP_256)
320 return -EINVAL;
321 break;
322 case WLAN_CIPHER_SUITE_GCMP:
323 if (params->key_len != WLAN_KEY_LEN_GCMP)
324 return -EINVAL;
325 break;
326 case WLAN_CIPHER_SUITE_GCMP_256:
327 if (params->key_len != WLAN_KEY_LEN_GCMP_256)
328 return -EINVAL;
329 break;
08645126 330 case WLAN_CIPHER_SUITE_WEP104:
8fc0fee0 331 if (params->key_len != WLAN_KEY_LEN_WEP104)
08645126
JB
332 return -EINVAL;
333 break;
334 case WLAN_CIPHER_SUITE_AES_CMAC:
8fc0fee0 335 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
08645126
JB
336 return -EINVAL;
337 break;
cfcf1682
JM
338 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
339 if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
340 return -EINVAL;
341 break;
342 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
343 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
344 return -EINVAL;
345 break;
346 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
347 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
348 return -EINVAL;
349 break;
08645126 350 default:
7d64b7cc
JB
351 /*
352 * We don't know anything about this algorithm,
353 * allow using it -- but the driver must check
354 * all parameters! We still check below whether
355 * or not the driver supports this algorithm,
356 * of course.
357 */
358 break;
08645126
JB
359 }
360
9f26a952
JM
361 if (params->seq) {
362 switch (params->cipher) {
363 case WLAN_CIPHER_SUITE_WEP40:
364 case WLAN_CIPHER_SUITE_WEP104:
365 /* These ciphers do not use key sequence */
366 return -EINVAL;
367 case WLAN_CIPHER_SUITE_TKIP:
368 case WLAN_CIPHER_SUITE_CCMP:
cfcf1682
JM
369 case WLAN_CIPHER_SUITE_CCMP_256:
370 case WLAN_CIPHER_SUITE_GCMP:
371 case WLAN_CIPHER_SUITE_GCMP_256:
9f26a952 372 case WLAN_CIPHER_SUITE_AES_CMAC:
cfcf1682
JM
373 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
374 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
375 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
9f26a952
JM
376 if (params->seq_len != 6)
377 return -EINVAL;
378 break;
379 }
380 }
381
38ba3c57 382 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
fffd0934
JB
383 return -EINVAL;
384
08645126
JB
385 return 0;
386}
e31a16d6 387
633adf1a 388unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
e31a16d6
ZY
389{
390 unsigned int hdrlen = 24;
391
392 if (ieee80211_is_data(fc)) {
393 if (ieee80211_has_a4(fc))
394 hdrlen = 30;
d0dd2de0 395 if (ieee80211_is_data_qos(fc)) {
e31a16d6 396 hdrlen += IEEE80211_QOS_CTL_LEN;
d0dd2de0
AT
397 if (ieee80211_has_order(fc))
398 hdrlen += IEEE80211_HT_CTL_LEN;
399 }
e31a16d6
ZY
400 goto out;
401 }
402
fb142f4b
FC
403 if (ieee80211_is_mgmt(fc)) {
404 if (ieee80211_has_order(fc))
405 hdrlen += IEEE80211_HT_CTL_LEN;
406 goto out;
407 }
408
e31a16d6
ZY
409 if (ieee80211_is_ctl(fc)) {
410 /*
411 * ACK and CTS are 10 bytes, all others 16. To see how
412 * to get this condition consider
413 * subtype mask: 0b0000000011110000 (0x00F0)
414 * ACK subtype: 0b0000000011010000 (0x00D0)
415 * CTS subtype: 0b0000000011000000 (0x00C0)
416 * bits that matter: ^^^ (0x00E0)
417 * value of those: 0b0000000011000000 (0x00C0)
418 */
419 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
420 hdrlen = 10;
421 else
422 hdrlen = 16;
423 }
424out:
425 return hdrlen;
426}
427EXPORT_SYMBOL(ieee80211_hdrlen);
428
429unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
430{
431 const struct ieee80211_hdr *hdr =
432 (const struct ieee80211_hdr *)skb->data;
433 unsigned int hdrlen;
434
435 if (unlikely(skb->len < 10))
436 return 0;
437 hdrlen = ieee80211_hdrlen(hdr->frame_control);
438 if (unlikely(hdrlen > skb->len))
439 return 0;
440 return hdrlen;
441}
442EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
443
2d1c304c 444static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
e31a16d6 445{
2d1c304c 446 int ae = flags & MESH_FLAGS_AE;
7dd111e8 447 /* 802.11-2012, 8.2.4.7.3 */
e31a16d6 448 switch (ae) {
7dd111e8 449 default:
e31a16d6
ZY
450 case 0:
451 return 6;
3c5772a5 452 case MESH_FLAGS_AE_A4:
e31a16d6 453 return 12;
3c5772a5 454 case MESH_FLAGS_AE_A5_A6:
e31a16d6 455 return 18;
e31a16d6
ZY
456 }
457}
2d1c304c
FF
458
459unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
460{
461 return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
462}
9b395bc3 463EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
e31a16d6 464
7f6990c8 465int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
24bba078
FF
466 const u8 *addr, enum nl80211_iftype iftype,
467 u8 data_offset)
e31a16d6
ZY
468{
469 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2d1c304c
FF
470 struct {
471 u8 hdr[ETH_ALEN] __aligned(2);
472 __be16 proto;
473 } payload;
474 struct ethhdr tmp;
475 u16 hdrlen;
476 u8 mesh_flags = 0;
e31a16d6
ZY
477
478 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
479 return -1;
480
24bba078 481 hdrlen = ieee80211_hdrlen(hdr->frame_control) + data_offset;
2d1c304c
FF
482 if (skb->len < hdrlen + 8)
483 return -1;
e31a16d6
ZY
484
485 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
486 * header
487 * IEEE 802.11 address fields:
488 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
489 * 0 0 DA SA BSSID n/a
490 * 0 1 DA BSSID SA n/a
491 * 1 0 BSSID SA DA n/a
492 * 1 1 RA TA DA SA
493 */
2d1c304c
FF
494 memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
495 memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
496
497 if (iftype == NL80211_IFTYPE_MESH_POINT)
498 skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
e31a16d6 499
5667c86a
RM
500 mesh_flags &= MESH_FLAGS_AE;
501
e31a16d6
ZY
502 switch (hdr->frame_control &
503 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
504 case cpu_to_le16(IEEE80211_FCTL_TODS):
505 if (unlikely(iftype != NL80211_IFTYPE_AP &&
074ac8df
JB
506 iftype != NL80211_IFTYPE_AP_VLAN &&
507 iftype != NL80211_IFTYPE_P2P_GO))
e31a16d6
ZY
508 return -1;
509 break;
510 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
511 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
f14543ee
FF
512 iftype != NL80211_IFTYPE_MESH_POINT &&
513 iftype != NL80211_IFTYPE_AP_VLAN &&
514 iftype != NL80211_IFTYPE_STATION))
e31a16d6
ZY
515 return -1;
516 if (iftype == NL80211_IFTYPE_MESH_POINT) {
5667c86a 517 if (mesh_flags == MESH_FLAGS_AE_A4)
e3cf8b3f 518 return -1;
5667c86a 519 if (mesh_flags == MESH_FLAGS_AE_A5_A6) {
e3cf8b3f
ZY
520 skb_copy_bits(skb, hdrlen +
521 offsetof(struct ieee80211s_hdr, eaddr1),
2d1c304c 522 tmp.h_dest, 2 * ETH_ALEN);
e31a16d6 523 }
2d1c304c 524 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
e31a16d6
ZY
525 }
526 break;
527 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
3c5772a5 528 if ((iftype != NL80211_IFTYPE_STATION &&
074ac8df
JB
529 iftype != NL80211_IFTYPE_P2P_CLIENT &&
530 iftype != NL80211_IFTYPE_MESH_POINT) ||
2d1c304c
FF
531 (is_multicast_ether_addr(tmp.h_dest) &&
532 ether_addr_equal(tmp.h_source, addr)))
e31a16d6 533 return -1;
3c5772a5 534 if (iftype == NL80211_IFTYPE_MESH_POINT) {
5667c86a 535 if (mesh_flags == MESH_FLAGS_AE_A5_A6)
7dd111e8 536 return -1;
5667c86a 537 if (mesh_flags == MESH_FLAGS_AE_A4)
e3cf8b3f
ZY
538 skb_copy_bits(skb, hdrlen +
539 offsetof(struct ieee80211s_hdr, eaddr1),
2d1c304c
FF
540 tmp.h_source, ETH_ALEN);
541 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
3c5772a5 542 }
e31a16d6
ZY
543 break;
544 case cpu_to_le16(0):
941c93cd 545 if (iftype != NL80211_IFTYPE_ADHOC &&
6e0bd6c3
RL
546 iftype != NL80211_IFTYPE_STATION &&
547 iftype != NL80211_IFTYPE_OCB)
941c93cd 548 return -1;
e31a16d6
ZY
549 break;
550 }
551
2d1c304c
FF
552 skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
553 tmp.h_proto = payload.proto;
e31a16d6 554
2d1c304c
FF
555 if (likely((ether_addr_equal(payload.hdr, rfc1042_header) &&
556 tmp.h_proto != htons(ETH_P_AARP) &&
557 tmp.h_proto != htons(ETH_P_IPX)) ||
558 ether_addr_equal(payload.hdr, bridge_tunnel_header)))
e31a16d6
ZY
559 /* remove RFC1042 or Bridge-Tunnel encapsulation and
560 * replace EtherType */
2d1c304c
FF
561 hdrlen += ETH_ALEN + 2;
562 else
c041778c 563 tmp.h_proto = htons(skb->len - hdrlen);
2d1c304c
FF
564
565 pskb_pull(skb, hdrlen);
e31a16d6 566
2d1c304c 567 if (!ehdr)
d58ff351 568 ehdr = skb_push(skb, sizeof(struct ethhdr));
2d1c304c
FF
569 memcpy(ehdr, &tmp, sizeof(tmp));
570
e31a16d6
ZY
571 return 0;
572}
7f6990c8 573EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
e31a16d6 574
2b67f944
FF
575static void
576__frame_add_frag(struct sk_buff *skb, struct page *page,
577 void *ptr, int len, int size)
578{
579 struct skb_shared_info *sh = skb_shinfo(skb);
580 int page_offset;
581
81c044fc 582 get_page(page);
2b67f944
FF
583 page_offset = ptr - page_address(page);
584 skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
585}
586
587static void
588__ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
589 int offset, int len)
590{
591 struct skb_shared_info *sh = skb_shinfo(skb);
aa1702dd 592 const skb_frag_t *frag = &sh->frags[0];
2b67f944
FF
593 struct page *frag_page;
594 void *frag_ptr;
595 int frag_len, frag_size;
596 int head_size = skb->len - skb->data_len;
597 int cur_len;
598
599 frag_page = virt_to_head_page(skb->head);
600 frag_ptr = skb->data;
601 frag_size = head_size;
602
603 while (offset >= frag_size) {
604 offset -= frag_size;
2b67f944
FF
605 frag_page = skb_frag_page(frag);
606 frag_ptr = skb_frag_address(frag);
607 frag_size = skb_frag_size(frag);
aa1702dd 608 frag++;
2b67f944
FF
609 }
610
611 frag_ptr += offset;
612 frag_len = frag_size - offset;
613
614 cur_len = min(len, frag_len);
615
616 __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
617 len -= cur_len;
618
619 while (len > 0) {
2b67f944
FF
620 frag_len = skb_frag_size(frag);
621 cur_len = min(len, frag_len);
622 __frame_add_frag(frame, skb_frag_page(frag),
623 skb_frag_address(frag), cur_len, frag_len);
624 len -= cur_len;
aa1702dd 625 frag++;
2b67f944
FF
626 }
627}
628
230fd28a
FF
629static struct sk_buff *
630__ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
2b67f944 631 int offset, int len, bool reuse_frag)
230fd28a
FF
632{
633 struct sk_buff *frame;
2b67f944 634 int cur_len = len;
230fd28a
FF
635
636 if (skb->len - offset < len)
637 return NULL;
638
2b67f944
FF
639 /*
640 * When reusing framents, copy some data to the head to simplify
641 * ethernet header handling and speed up protocol header processing
642 * in the stack later.
643 */
644 if (reuse_frag)
645 cur_len = min_t(int, len, 32);
646
230fd28a
FF
647 /*
648 * Allocate and reserve two bytes more for payload
649 * alignment since sizeof(struct ethhdr) is 14.
650 */
2b67f944 651 frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
16a910a6
GG
652 if (!frame)
653 return NULL;
230fd28a
FF
654
655 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
2b67f944
FF
656 skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
657
658 len -= cur_len;
659 if (!len)
660 return frame;
661
662 offset += cur_len;
663 __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
230fd28a
FF
664
665 return frame;
666}
eaf85ca7
ZY
667
668void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
669 const u8 *addr, enum nl80211_iftype iftype,
8b3becad 670 const unsigned int extra_headroom,
8b935ee2 671 const u8 *check_da, const u8 *check_sa)
eaf85ca7 672{
230fd28a 673 unsigned int hlen = ALIGN(extra_headroom, 4);
eaf85ca7
ZY
674 struct sk_buff *frame = NULL;
675 u16 ethertype;
676 u8 *payload;
7f6990c8 677 int offset = 0, remaining;
230fd28a 678 struct ethhdr eth;
2b67f944 679 bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
2bf0ccc7 680 bool reuse_skb = false;
230fd28a 681 bool last = false;
88665f5a 682
230fd28a
FF
683 while (!last) {
684 unsigned int subframe_len;
685 int len;
eaf85ca7 686 u8 padding;
eaf85ca7 687
230fd28a
FF
688 skb_copy_bits(skb, offset, &eth, sizeof(eth));
689 len = ntohs(eth.h_proto);
690 subframe_len = sizeof(struct ethhdr) + len;
eaf85ca7 691 padding = (4 - subframe_len) & 0x3;
230fd28a 692
eaf85ca7 693 /* the last MSDU has no padding */
230fd28a 694 remaining = skb->len - offset;
eaf85ca7
ZY
695 if (subframe_len > remaining)
696 goto purge;
697
230fd28a 698 offset += sizeof(struct ethhdr);
230fd28a 699 last = remaining <= subframe_len + padding;
8b935ee2
JB
700
701 /* FIXME: should we really accept multicast DA? */
702 if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
703 !ether_addr_equal(check_da, eth.h_dest)) ||
704 (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
705 offset += len + padding;
706 continue;
707 }
708
709 /* reuse skb for the last subframe */
2b67f944 710 if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
230fd28a 711 skb_pull(skb, offset);
eaf85ca7 712 frame = skb;
230fd28a
FF
713 reuse_skb = true;
714 } else {
2b67f944
FF
715 frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
716 reuse_frag);
eaf85ca7
ZY
717 if (!frame)
718 goto purge;
719
230fd28a 720 offset += len + padding;
eaf85ca7
ZY
721 }
722
723 skb_reset_network_header(frame);
724 frame->dev = skb->dev;
725 frame->priority = skb->priority;
726
727 payload = frame->data;
728 ethertype = (payload[6] << 8) | payload[7];
ac422d3c 729 if (likely((ether_addr_equal(payload, rfc1042_header) &&
eaf85ca7 730 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
ac422d3c 731 ether_addr_equal(payload, bridge_tunnel_header))) {
230fd28a
FF
732 eth.h_proto = htons(ethertype);
733 skb_pull(frame, ETH_ALEN + 2);
eaf85ca7 734 }
230fd28a
FF
735
736 memcpy(skb_push(frame, sizeof(eth)), &eth, sizeof(eth));
eaf85ca7
ZY
737 __skb_queue_tail(list, frame);
738 }
739
230fd28a
FF
740 if (!reuse_skb)
741 dev_kfree_skb(skb);
742
eaf85ca7
ZY
743 return;
744
745 purge:
746 __skb_queue_purge(list);
eaf85ca7
ZY
747 dev_kfree_skb(skb);
748}
749EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
750
e31a16d6 751/* Given a data frame determine the 802.1p/1d tag to use. */
fa9ffc74
KP
752unsigned int cfg80211_classify8021d(struct sk_buff *skb,
753 struct cfg80211_qos_map *qos_map)
e31a16d6
ZY
754{
755 unsigned int dscp;
c6ca5e28 756 unsigned char vlan_priority;
1fc9b725 757 unsigned int ret;
e31a16d6
ZY
758
759 /* skb->priority values from 256->263 are magic values to
760 * directly indicate a specific 802.1d priority. This is used
761 * to allow 802.1d priority to be passed directly in from VLAN
762 * tags, etc.
763 */
1fc9b725
JB
764 if (skb->priority >= 256 && skb->priority <= 263) {
765 ret = skb->priority - 256;
766 goto out;
767 }
e31a16d6 768
df8a39de
JP
769 if (skb_vlan_tag_present(skb)) {
770 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
c6ca5e28 771 >> VLAN_PRIO_SHIFT;
1fc9b725
JB
772 if (vlan_priority > 0) {
773 ret = vlan_priority;
774 goto out;
775 }
c6ca5e28
V
776 }
777
e31a16d6
ZY
778 switch (skb->protocol) {
779 case htons(ETH_P_IP):
b156579b
DT
780 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
781 break;
782 case htons(ETH_P_IPV6):
783 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
e31a16d6 784 break;
960d97f9
SW
785 case htons(ETH_P_MPLS_UC):
786 case htons(ETH_P_MPLS_MC): {
787 struct mpls_label mpls_tmp, *mpls;
788
789 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
790 sizeof(*mpls), &mpls_tmp);
791 if (!mpls)
792 return 0;
793
1fc9b725 794 ret = (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
960d97f9 795 >> MPLS_LS_TC_SHIFT;
1fc9b725 796 goto out;
960d97f9
SW
797 }
798 case htons(ETH_P_80221):
799 /* 802.21 is always network control traffic */
800 return 7;
e31a16d6
ZY
801 default:
802 return 0;
803 }
804
fa9ffc74
KP
805 if (qos_map) {
806 unsigned int i, tmp_dscp = dscp >> 2;
807
808 for (i = 0; i < qos_map->num_des; i++) {
1fc9b725
JB
809 if (tmp_dscp == qos_map->dscp_exception[i].dscp) {
810 ret = qos_map->dscp_exception[i].up;
811 goto out;
812 }
fa9ffc74
KP
813 }
814
815 for (i = 0; i < 8; i++) {
816 if (tmp_dscp >= qos_map->up[i].low &&
1fc9b725
JB
817 tmp_dscp <= qos_map->up[i].high) {
818 ret = i;
819 goto out;
820 }
fa9ffc74
KP
821 }
822 }
823
1fc9b725
JB
824 ret = dscp >> 5;
825out:
826 return array_index_nospec(ret, IEEE80211_NUM_TIDS);
e31a16d6
ZY
827}
828EXPORT_SYMBOL(cfg80211_classify8021d);
517357c6 829
49a68e0d 830const struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id)
517357c6 831{
9caf0364
JB
832 const struct cfg80211_bss_ies *ies;
833
834 ies = rcu_dereference(bss->ies);
835 if (!ies)
517357c6 836 return NULL;
9caf0364 837
49a68e0d 838 return cfg80211_find_elem(id, ies->data, ies->len);
517357c6 839}
49a68e0d 840EXPORT_SYMBOL(ieee80211_bss_get_elem);
fffd0934
JB
841
842void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
843{
f26cbf40 844 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
fffd0934
JB
845 struct net_device *dev = wdev->netdev;
846 int i;
847
848 if (!wdev->connect_keys)
849 return;
850
b8676221 851 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
fffd0934
JB
852 if (!wdev->connect_keys->params[i].cipher)
853 continue;
e35e4d28
HG
854 if (rdev_add_key(rdev, dev, i, false, NULL,
855 &wdev->connect_keys->params[i])) {
e9c0268f 856 netdev_err(dev, "failed to set key %d\n", i);
1e056665
ZY
857 continue;
858 }
d4f29978
JB
859 if (wdev->connect_keys->def == i &&
860 rdev_set_default_key(rdev, dev, i, true, true)) {
861 netdev_err(dev, "failed to set defkey %d\n", i);
862 continue;
863 }
fffd0934
JB
864 }
865
b47f610b 866 kzfree(wdev->connect_keys);
fffd0934
JB
867 wdev->connect_keys = NULL;
868}
3d54d255 869
1f6fc43e 870void cfg80211_process_wdev_events(struct wireless_dev *wdev)
3d54d255
JB
871{
872 struct cfg80211_event *ev;
873 unsigned long flags;
3d54d255
JB
874
875 spin_lock_irqsave(&wdev->event_lock, flags);
876 while (!list_empty(&wdev->event_list)) {
877 ev = list_first_entry(&wdev->event_list,
878 struct cfg80211_event, list);
879 list_del(&ev->list);
880 spin_unlock_irqrestore(&wdev->event_lock, flags);
881
882 wdev_lock(wdev);
883 switch (ev->type) {
884 case EVENT_CONNECT_RESULT:
3d54d255 885 __cfg80211_connect_result(
5349a0f7
VK
886 wdev->netdev,
887 &ev->cr,
888 ev->cr.status == WLAN_STATUS_SUCCESS);
3d54d255
JB
889 break;
890 case EVENT_ROAMED:
29ce6ecb 891 __cfg80211_roamed(wdev, &ev->rm);
3d54d255
JB
892 break;
893 case EVENT_DISCONNECTED:
894 __cfg80211_disconnected(wdev->netdev,
895 ev->dc.ie, ev->dc.ie_len,
80279fb7
JB
896 ev->dc.reason,
897 !ev->dc.locally_generated);
3d54d255
JB
898 break;
899 case EVENT_IBSS_JOINED:
fe94f3a4
AQ
900 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
901 ev->ij.channel);
3d54d255 902 break;
f04c2203
MK
903 case EVENT_STOPPED:
904 __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
905 break;
503c1fb9
AS
906 case EVENT_PORT_AUTHORIZED:
907 __cfg80211_port_authorized(wdev, ev->pa.bssid);
908 break;
3d54d255
JB
909 }
910 wdev_unlock(wdev);
911
912 kfree(ev);
913
914 spin_lock_irqsave(&wdev->event_lock, flags);
915 }
916 spin_unlock_irqrestore(&wdev->event_lock, flags);
917}
918
919void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
920{
921 struct wireless_dev *wdev;
922
923 ASSERT_RTNL();
3d54d255 924
53873f13 925 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
3d54d255 926 cfg80211_process_wdev_events(wdev);
3d54d255
JB
927}
928
929int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
930 struct net_device *dev, enum nl80211_iftype ntype,
818a986e 931 struct vif_params *params)
3d54d255
JB
932{
933 int err;
934 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
935
73fb08e2 936 ASSERT_RTNL();
3d54d255
JB
937
938 /* don't support changing VLANs, you just re-create them */
939 if (otype == NL80211_IFTYPE_AP_VLAN)
940 return -EOPNOTSUPP;
941
cb3b7d87
AB
942 /* cannot change into P2P device or NAN */
943 if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
944 ntype == NL80211_IFTYPE_NAN)
98104fde
JB
945 return -EOPNOTSUPP;
946
3d54d255
JB
947 if (!rdev->ops->change_virtual_intf ||
948 !(rdev->wiphy.interface_modes & (1 << ntype)))
949 return -EOPNOTSUPP;
950
ad4bb6f8 951 /* if it's part of a bridge, reject changing type to station/ibss */
2e92a2d0 952 if (netif_is_bridge_port(dev) &&
074ac8df
JB
953 (ntype == NL80211_IFTYPE_ADHOC ||
954 ntype == NL80211_IFTYPE_STATION ||
955 ntype == NL80211_IFTYPE_P2P_CLIENT))
ad4bb6f8
JB
956 return -EBUSY;
957
6cbfb1bb 958 if (ntype != otype) {
9bc383de 959 dev->ieee80211_ptr->use_4addr = false;
29cbe68c 960 dev->ieee80211_ptr->mesh_id_up_len = 0;
194ff52d 961 wdev_lock(dev->ieee80211_ptr);
fa9ffc74 962 rdev_set_qos_map(rdev, dev, NULL);
194ff52d 963 wdev_unlock(dev->ieee80211_ptr);
9bc383de 964
3d54d255 965 switch (otype) {
ac800140 966 case NL80211_IFTYPE_AP:
7c8d5e03 967 cfg80211_stop_ap(rdev, dev, true);
ac800140 968 break;
3d54d255
JB
969 case NL80211_IFTYPE_ADHOC:
970 cfg80211_leave_ibss(rdev, dev, false);
971 break;
972 case NL80211_IFTYPE_STATION:
074ac8df 973 case NL80211_IFTYPE_P2P_CLIENT:
83739b03 974 wdev_lock(dev->ieee80211_ptr);
3d54d255
JB
975 cfg80211_disconnect(rdev, dev,
976 WLAN_REASON_DEAUTH_LEAVING, true);
83739b03 977 wdev_unlock(dev->ieee80211_ptr);
3d54d255
JB
978 break;
979 case NL80211_IFTYPE_MESH_POINT:
980 /* mesh should be handled? */
981 break;
982 default:
983 break;
984 }
985
986 cfg80211_process_rdev_events(rdev);
c1d3ad84 987 cfg80211_mlme_purge_registrations(dev->ieee80211_ptr);
3d54d255
JB
988 }
989
818a986e 990 err = rdev_change_virtual_intf(rdev, dev, ntype, params);
3d54d255
JB
991
992 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
993
9bc383de
JB
994 if (!err && params && params->use_4addr != -1)
995 dev->ieee80211_ptr->use_4addr = params->use_4addr;
996
ad4bb6f8
JB
997 if (!err) {
998 dev->priv_flags &= ~IFF_DONT_BRIDGE;
999 switch (ntype) {
1000 case NL80211_IFTYPE_STATION:
1001 if (dev->ieee80211_ptr->use_4addr)
1002 break;
1003 /* fall through */
6e0bd6c3 1004 case NL80211_IFTYPE_OCB:
074ac8df 1005 case NL80211_IFTYPE_P2P_CLIENT:
ad4bb6f8
JB
1006 case NL80211_IFTYPE_ADHOC:
1007 dev->priv_flags |= IFF_DONT_BRIDGE;
1008 break;
074ac8df 1009 case NL80211_IFTYPE_P2P_GO:
ad4bb6f8
JB
1010 case NL80211_IFTYPE_AP:
1011 case NL80211_IFTYPE_AP_VLAN:
1012 case NL80211_IFTYPE_WDS:
1013 case NL80211_IFTYPE_MESH_POINT:
1014 /* bridging OK */
1015 break;
1016 case NL80211_IFTYPE_MONITOR:
1017 /* monitor can't bridge anyway */
1018 break;
1019 case NL80211_IFTYPE_UNSPECIFIED:
2e161f78 1020 case NUM_NL80211_IFTYPES:
ad4bb6f8
JB
1021 /* not happening */
1022 break;
98104fde 1023 case NL80211_IFTYPE_P2P_DEVICE:
cb3b7d87 1024 case NL80211_IFTYPE_NAN:
98104fde
JB
1025 WARN_ON(1);
1026 break;
ad4bb6f8
JB
1027 }
1028 }
1029
dbbae26a
MK
1030 if (!err && ntype != otype && netif_running(dev)) {
1031 cfg80211_update_iface_num(rdev, ntype, 1);
1032 cfg80211_update_iface_num(rdev, otype, -1);
1033 }
1034
3d54d255
JB
1035 return err;
1036}
254416aa 1037
0c1eca4e
JB
1038static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
1039{
1040 int modulation, streams, bitrate;
1041
1042 /* the formula below does only work for MCS values smaller than 32 */
1043 if (WARN_ON_ONCE(rate->mcs >= 32))
1044 return 0;
1045
1046 modulation = rate->mcs & 7;
1047 streams = (rate->mcs >> 3) + 1;
1048
1049 bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1050
1051 if (modulation < 4)
1052 bitrate *= (modulation + 1);
1053 else if (modulation == 4)
1054 bitrate *= (modulation + 2);
1055 else
1056 bitrate *= (modulation + 3);
1057
1058 bitrate *= streams;
1059
1060 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1061 bitrate = (bitrate / 9) * 10;
1062
1063 /* do NOT round down here */
1064 return (bitrate + 50000) / 100000;
1065}
1066
2a38075c 1067static u32 cfg80211_calculate_bitrate_dmg(struct rate_info *rate)
95ddc1fc
VK
1068{
1069 static const u32 __mcs2bitrate[] = {
1070 /* control PHY */
1071 [0] = 275,
1072 /* SC PHY */
1073 [1] = 3850,
1074 [2] = 7700,
1075 [3] = 9625,
1076 [4] = 11550,
1077 [5] = 12512, /* 1251.25 mbps */
1078 [6] = 15400,
1079 [7] = 19250,
1080 [8] = 23100,
1081 [9] = 25025,
1082 [10] = 30800,
1083 [11] = 38500,
1084 [12] = 46200,
1085 /* OFDM PHY */
1086 [13] = 6930,
1087 [14] = 8662, /* 866.25 mbps */
1088 [15] = 13860,
1089 [16] = 17325,
1090 [17] = 20790,
1091 [18] = 27720,
1092 [19] = 34650,
1093 [20] = 41580,
1094 [21] = 45045,
1095 [22] = 51975,
1096 [23] = 62370,
1097 [24] = 67568, /* 6756.75 mbps */
1098 /* LP-SC PHY */
1099 [25] = 6260,
1100 [26] = 8340,
1101 [27] = 11120,
1102 [28] = 12510,
1103 [29] = 16680,
1104 [30] = 22240,
1105 [31] = 25030,
1106 };
1107
1108 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1109 return 0;
1110
1111 return __mcs2bitrate[rate->mcs];
1112}
1113
2a38075c
AAL
1114static u32 cfg80211_calculate_bitrate_edmg(struct rate_info *rate)
1115{
1116 static const u32 __mcs2bitrate[] = {
1117 /* control PHY */
1118 [0] = 275,
1119 /* SC PHY */
1120 [1] = 3850,
1121 [2] = 7700,
1122 [3] = 9625,
1123 [4] = 11550,
1124 [5] = 12512, /* 1251.25 mbps */
1125 [6] = 13475,
1126 [7] = 15400,
1127 [8] = 19250,
1128 [9] = 23100,
1129 [10] = 25025,
1130 [11] = 26950,
1131 [12] = 30800,
1132 [13] = 38500,
1133 [14] = 46200,
1134 [15] = 50050,
1135 [16] = 53900,
1136 [17] = 57750,
1137 [18] = 69300,
1138 [19] = 75075,
1139 [20] = 80850,
1140 };
1141
1142 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1143 return 0;
1144
1145 return __mcs2bitrate[rate->mcs] * rate->n_bonded_ch;
1146}
1147
db9c64cf
JB
1148static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1149{
1150 static const u32 base[4][10] = {
1151 { 6500000,
1152 13000000,
1153 19500000,
1154 26000000,
1155 39000000,
1156 52000000,
1157 58500000,
1158 65000000,
1159 78000000,
8fdd136f
PT
1160 /* not in the spec, but some devices use this: */
1161 86500000,
db9c64cf
JB
1162 },
1163 { 13500000,
1164 27000000,
1165 40500000,
1166 54000000,
1167 81000000,
1168 108000000,
1169 121500000,
1170 135000000,
1171 162000000,
1172 180000000,
1173 },
1174 { 29300000,
1175 58500000,
1176 87800000,
1177 117000000,
1178 175500000,
1179 234000000,
1180 263300000,
1181 292500000,
1182 351000000,
1183 390000000,
1184 },
1185 { 58500000,
1186 117000000,
1187 175500000,
1188 234000000,
1189 351000000,
1190 468000000,
1191 526500000,
1192 585000000,
1193 702000000,
1194 780000000,
1195 },
1196 };
1197 u32 bitrate;
1198 int idx;
1199
ca8fe250
JB
1200 if (rate->mcs > 9)
1201 goto warn;
db9c64cf 1202
b51f3bee
JB
1203 switch (rate->bw) {
1204 case RATE_INFO_BW_160:
1205 idx = 3;
1206 break;
1207 case RATE_INFO_BW_80:
1208 idx = 2;
1209 break;
1210 case RATE_INFO_BW_40:
1211 idx = 1;
1212 break;
1213 case RATE_INFO_BW_5:
1214 case RATE_INFO_BW_10:
1215 default:
ca8fe250 1216 goto warn;
b51f3bee
JB
1217 case RATE_INFO_BW_20:
1218 idx = 0;
1219 }
db9c64cf
JB
1220
1221 bitrate = base[idx][rate->mcs];
1222 bitrate *= rate->nss;
1223
1224 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1225 bitrate = (bitrate / 9) * 10;
1226
1227 /* do NOT round down here */
1228 return (bitrate + 50000) / 100000;
ca8fe250
JB
1229 warn:
1230 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1231 rate->bw, rate->mcs, rate->nss);
1232 return 0;
db9c64cf
JB
1233}
1234
c4cbaf79
LC
1235static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate)
1236{
1237#define SCALE 2048
1238 u16 mcs_divisors[12] = {
1239 34133, /* 16.666666... */
1240 17067, /* 8.333333... */
1241 11378, /* 5.555555... */
1242 8533, /* 4.166666... */
1243 5689, /* 2.777777... */
1244 4267, /* 2.083333... */
1245 3923, /* 1.851851... */
1246 3413, /* 1.666666... */
1247 2844, /* 1.388888... */
1248 2560, /* 1.250000... */
1249 2276, /* 1.111111... */
1250 2048, /* 1.000000... */
1251 };
1252 u32 rates_160M[3] = { 960777777, 907400000, 816666666 };
1253 u32 rates_969[3] = { 480388888, 453700000, 408333333 };
1254 u32 rates_484[3] = { 229411111, 216666666, 195000000 };
1255 u32 rates_242[3] = { 114711111, 108333333, 97500000 };
1256 u32 rates_106[3] = { 40000000, 37777777, 34000000 };
1257 u32 rates_52[3] = { 18820000, 17777777, 16000000 };
1258 u32 rates_26[3] = { 9411111, 8888888, 8000000 };
1259 u64 tmp;
1260 u32 result;
1261
1262 if (WARN_ON_ONCE(rate->mcs > 11))
1263 return 0;
1264
1265 if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2))
1266 return 0;
1267 if (WARN_ON_ONCE(rate->he_ru_alloc >
1268 NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1269 return 0;
1270 if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1271 return 0;
1272
1273 if (rate->bw == RATE_INFO_BW_160)
1274 result = rates_160M[rate->he_gi];
1275 else if (rate->bw == RATE_INFO_BW_80 ||
1276 (rate->bw == RATE_INFO_BW_HE_RU &&
1277 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
1278 result = rates_969[rate->he_gi];
1279 else if (rate->bw == RATE_INFO_BW_40 ||
1280 (rate->bw == RATE_INFO_BW_HE_RU &&
1281 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
1282 result = rates_484[rate->he_gi];
1283 else if (rate->bw == RATE_INFO_BW_20 ||
1284 (rate->bw == RATE_INFO_BW_HE_RU &&
1285 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
1286 result = rates_242[rate->he_gi];
1287 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1288 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
1289 result = rates_106[rate->he_gi];
1290 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1291 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
1292 result = rates_52[rate->he_gi];
1293 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1294 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
1295 result = rates_26[rate->he_gi];
344c9719
NC
1296 else {
1297 WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1298 rate->bw, rate->he_ru_alloc);
c4cbaf79 1299 return 0;
344c9719 1300 }
c4cbaf79
LC
1301
1302 /* now scale to the appropriate MCS */
1303 tmp = result;
1304 tmp *= SCALE;
1305 do_div(tmp, mcs_divisors[rate->mcs]);
1306 result = tmp;
1307
1308 /* and take NSS, DCM into account */
1309 result = (result * rate->nss) / 8;
1310 if (rate->he_dcm)
1311 result /= 2;
1312
25d16d12 1313 return result / 10000;
c4cbaf79
LC
1314}
1315
8eb41c8d 1316u32 cfg80211_calculate_bitrate(struct rate_info *rate)
254416aa 1317{
0c1eca4e
JB
1318 if (rate->flags & RATE_INFO_FLAGS_MCS)
1319 return cfg80211_calculate_bitrate_ht(rate);
2a38075c
AAL
1320 if (rate->flags & RATE_INFO_FLAGS_DMG)
1321 return cfg80211_calculate_bitrate_dmg(rate);
1322 if (rate->flags & RATE_INFO_FLAGS_EDMG)
1323 return cfg80211_calculate_bitrate_edmg(rate);
db9c64cf
JB
1324 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1325 return cfg80211_calculate_bitrate_vht(rate);
c4cbaf79
LC
1326 if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1327 return cfg80211_calculate_bitrate_he(rate);
254416aa 1328
0c1eca4e 1329 return rate->legacy;
254416aa 1330}
8097e149 1331EXPORT_SYMBOL(cfg80211_calculate_bitrate);
56d1893d 1332
c216e641
AS
1333int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1334 enum ieee80211_p2p_attr_id attr,
1335 u8 *buf, unsigned int bufsize)
0ee45355
JB
1336{
1337 u8 *out = buf;
1338 u16 attr_remaining = 0;
1339 bool desired_attr = false;
1340 u16 desired_len = 0;
1341
1342 while (len > 0) {
1343 unsigned int iedatalen;
1344 unsigned int copy;
1345 const u8 *iedata;
1346
1347 if (len < 2)
1348 return -EILSEQ;
1349 iedatalen = ies[1];
1350 if (iedatalen + 2 > len)
1351 return -EILSEQ;
1352
1353 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1354 goto cont;
1355
1356 if (iedatalen < 4)
1357 goto cont;
1358
1359 iedata = ies + 2;
1360
1361 /* check WFA OUI, P2P subtype */
1362 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1363 iedata[2] != 0x9a || iedata[3] != 0x09)
1364 goto cont;
1365
1366 iedatalen -= 4;
1367 iedata += 4;
1368
1369 /* check attribute continuation into this IE */
1370 copy = min_t(unsigned int, attr_remaining, iedatalen);
1371 if (copy && desired_attr) {
1372 desired_len += copy;
1373 if (out) {
1374 memcpy(out, iedata, min(bufsize, copy));
1375 out += min(bufsize, copy);
1376 bufsize -= min(bufsize, copy);
1377 }
1378
1379
1380 if (copy == attr_remaining)
1381 return desired_len;
1382 }
1383
1384 attr_remaining -= copy;
1385 if (attr_remaining)
1386 goto cont;
1387
1388 iedatalen -= copy;
1389 iedata += copy;
1390
1391 while (iedatalen > 0) {
1392 u16 attr_len;
1393
1394 /* P2P attribute ID & size must fit */
1395 if (iedatalen < 3)
1396 return -EILSEQ;
1397 desired_attr = iedata[0] == attr;
1398 attr_len = get_unaligned_le16(iedata + 1);
1399 iedatalen -= 3;
1400 iedata += 3;
1401
1402 copy = min_t(unsigned int, attr_len, iedatalen);
1403
1404 if (desired_attr) {
1405 desired_len += copy;
1406 if (out) {
1407 memcpy(out, iedata, min(bufsize, copy));
1408 out += min(bufsize, copy);
1409 bufsize -= min(bufsize, copy);
1410 }
1411
1412 if (copy == attr_len)
1413 return desired_len;
1414 }
1415
1416 iedata += copy;
1417 iedatalen -= copy;
1418 attr_remaining = attr_len - copy;
1419 }
1420
1421 cont:
1422 len -= ies[1] + 2;
1423 ies += ies[1] + 2;
1424 }
1425
1426 if (attr_remaining && desired_attr)
1427 return -EILSEQ;
1428
1429 return -ENOENT;
1430}
1431EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1432
2512b1b1 1433static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
29464ccc
JB
1434{
1435 int i;
1436
2512b1b1
LK
1437 /* Make sure array values are legal */
1438 if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1439 return false;
1440
1441 i = 0;
1442 while (i < n_ids) {
1443 if (ids[i] == WLAN_EID_EXTENSION) {
1444 if (id_ext && (ids[i + 1] == id))
1445 return true;
1446
1447 i += 2;
1448 continue;
1449 }
1450
1451 if (ids[i] == id && !id_ext)
29464ccc 1452 return true;
2512b1b1
LK
1453
1454 i++;
1455 }
29464ccc
JB
1456 return false;
1457}
1458
8ac63448
JB
1459static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1460{
1461 /* we assume a validly formed IEs buffer */
1462 u8 len = ies[pos + 1];
1463
1464 pos += 2 + len;
1465
1466 /* the IE itself must have 255 bytes for fragments to follow */
1467 if (len < 255)
1468 return pos;
1469
1470 while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1471 len = ies[pos + 1];
1472 pos += 2 + len;
1473 }
1474
1475 return pos;
1476}
1477
29464ccc
JB
1478size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1479 const u8 *ids, int n_ids,
1480 const u8 *after_ric, int n_after_ric,
1481 size_t offset)
1482{
1483 size_t pos = offset;
1484
2512b1b1
LK
1485 while (pos < ielen) {
1486 u8 ext = 0;
1487
1488 if (ies[pos] == WLAN_EID_EXTENSION)
1489 ext = 2;
1490 if ((pos + ext) >= ielen)
1491 break;
1492
1493 if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1494 ies[pos] == WLAN_EID_EXTENSION))
1495 break;
1496
29464ccc 1497 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
8ac63448 1498 pos = skip_ie(ies, ielen, pos);
29464ccc 1499
2512b1b1
LK
1500 while (pos < ielen) {
1501 if (ies[pos] == WLAN_EID_EXTENSION)
1502 ext = 2;
1503 else
1504 ext = 0;
1505
1506 if ((pos + ext) >= ielen)
1507 break;
1508
1509 if (!ieee80211_id_in_list(after_ric,
1510 n_after_ric,
1511 ies[pos + ext],
1512 ext == 2))
1513 pos = skip_ie(ies, ielen, pos);
312ca38d
JM
1514 else
1515 break;
2512b1b1 1516 }
29464ccc 1517 } else {
8ac63448 1518 pos = skip_ie(ies, ielen, pos);
29464ccc
JB
1519 }
1520 }
1521
1522 return pos;
1523}
1524EXPORT_SYMBOL(ieee80211_ie_split_ric);
1525
1ce3e82b 1526bool ieee80211_operating_class_to_band(u8 operating_class,
57fbcce3 1527 enum nl80211_band *band)
1ce3e82b
JB
1528{
1529 switch (operating_class) {
1530 case 112:
1531 case 115 ... 127:
954a86ef 1532 case 128 ... 130:
57fbcce3 1533 *band = NL80211_BAND_5GHZ;
1ce3e82b 1534 return true;
852f0462
AS
1535 case 131 ... 135:
1536 *band = NL80211_BAND_6GHZ;
1537 return true;
1ce3e82b
JB
1538 case 81:
1539 case 82:
1540 case 83:
1541 case 84:
57fbcce3 1542 *band = NL80211_BAND_2GHZ;
1ce3e82b 1543 return true;
55300a13 1544 case 180:
57fbcce3 1545 *band = NL80211_BAND_60GHZ;
55300a13 1546 return true;
1ce3e82b
JB
1547 }
1548
1549 return false;
1550}
1551EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1552
a38700dd
AN
1553bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1554 u8 *op_class)
1555{
1556 u8 vht_opclass;
8442938c 1557 u32 freq = chandef->center_freq1;
a38700dd
AN
1558
1559 if (freq >= 2412 && freq <= 2472) {
1560 if (chandef->width > NL80211_CHAN_WIDTH_40)
1561 return false;
1562
1563 /* 2.407 GHz, channels 1..13 */
1564 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1565 if (freq > chandef->chan->center_freq)
1566 *op_class = 83; /* HT40+ */
1567 else
1568 *op_class = 84; /* HT40- */
1569 } else {
1570 *op_class = 81;
1571 }
1572
1573 return true;
1574 }
1575
1576 if (freq == 2484) {
ec649fed
MH
1577 /* channel 14 is only for IEEE 802.11b */
1578 if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
a38700dd
AN
1579 return false;
1580
1581 *op_class = 82; /* channel 14 */
1582 return true;
1583 }
1584
1585 switch (chandef->width) {
1586 case NL80211_CHAN_WIDTH_80:
1587 vht_opclass = 128;
1588 break;
1589 case NL80211_CHAN_WIDTH_160:
1590 vht_opclass = 129;
1591 break;
1592 case NL80211_CHAN_WIDTH_80P80:
1593 vht_opclass = 130;
1594 break;
1595 case NL80211_CHAN_WIDTH_10:
1596 case NL80211_CHAN_WIDTH_5:
1597 return false; /* unsupported for now */
1598 default:
1599 vht_opclass = 0;
1600 break;
1601 }
1602
1603 /* 5 GHz, channels 36..48 */
1604 if (freq >= 5180 && freq <= 5240) {
1605 if (vht_opclass) {
1606 *op_class = vht_opclass;
1607 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1608 if (freq > chandef->chan->center_freq)
1609 *op_class = 116;
1610 else
1611 *op_class = 117;
1612 } else {
1613 *op_class = 115;
1614 }
1615
1616 return true;
1617 }
1618
1619 /* 5 GHz, channels 52..64 */
1620 if (freq >= 5260 && freq <= 5320) {
1621 if (vht_opclass) {
1622 *op_class = vht_opclass;
1623 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1624 if (freq > chandef->chan->center_freq)
1625 *op_class = 119;
1626 else
1627 *op_class = 120;
1628 } else {
1629 *op_class = 118;
1630 }
1631
1632 return true;
1633 }
1634
1635 /* 5 GHz, channels 100..144 */
1636 if (freq >= 5500 && freq <= 5720) {
1637 if (vht_opclass) {
1638 *op_class = vht_opclass;
1639 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1640 if (freq > chandef->chan->center_freq)
1641 *op_class = 122;
1642 else
1643 *op_class = 123;
1644 } else {
1645 *op_class = 121;
1646 }
1647
1648 return true;
1649 }
1650
1651 /* 5 GHz, channels 149..169 */
1652 if (freq >= 5745 && freq <= 5845) {
1653 if (vht_opclass) {
1654 *op_class = vht_opclass;
1655 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1656 if (freq > chandef->chan->center_freq)
1657 *op_class = 126;
1658 else
1659 *op_class = 127;
1660 } else if (freq <= 5805) {
1661 *op_class = 124;
1662 } else {
1663 *op_class = 125;
1664 }
1665
1666 return true;
1667 }
1668
1669 /* 56.16 GHz, channel 1..4 */
9cf0a0b4 1670 if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
a38700dd
AN
1671 if (chandef->width >= NL80211_CHAN_WIDTH_40)
1672 return false;
1673
1674 *op_class = 180;
1675 return true;
1676 }
1677
1678 /* not supported yet */
1679 return false;
1680}
1681EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1682
4c8dea63
JB
1683static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
1684 u32 *beacon_int_gcd,
1685 bool *beacon_int_different)
56d1893d
JB
1686{
1687 struct wireless_dev *wdev;
56d1893d 1688
4c8dea63
JB
1689 *beacon_int_gcd = 0;
1690 *beacon_int_different = false;
56d1893d 1691
4c8dea63 1692 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
56d1893d
JB
1693 if (!wdev->beacon_interval)
1694 continue;
0c317a02 1695
4c8dea63
JB
1696 if (!*beacon_int_gcd) {
1697 *beacon_int_gcd = wdev->beacon_interval;
0c317a02 1698 continue;
4c8dea63 1699 }
0c317a02 1700
4c8dea63 1701 if (wdev->beacon_interval == *beacon_int_gcd)
0c317a02
PK
1702 continue;
1703
4c8dea63
JB
1704 *beacon_int_different = true;
1705 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval);
1706 }
0c317a02 1707
4c8dea63
JB
1708 if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
1709 if (*beacon_int_gcd)
1710 *beacon_int_different = true;
1711 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
56d1893d 1712 }
4c8dea63 1713}
56d1893d 1714
4c8dea63
JB
1715int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1716 enum nl80211_iftype iftype, u32 beacon_int)
1717{
1718 /*
1719 * This is just a basic pre-condition check; if interface combinations
1720 * are possible the driver must already be checking those with a call
1721 * to cfg80211_check_combinations(), in which case we'll validate more
1722 * through the cfg80211_calculate_bi_data() call and code in
1723 * cfg80211_iter_combinations().
1724 */
1725
1726 if (beacon_int < 10 || beacon_int > 10000)
1727 return -EINVAL;
1728
1729 return 0;
56d1893d 1730}
7527a782 1731
65a124dd 1732int cfg80211_iter_combinations(struct wiphy *wiphy,
e227300c 1733 struct iface_combination_params *params,
65a124dd
MK
1734 void (*iter)(const struct ieee80211_iface_combination *c,
1735 void *data),
1736 void *data)
cb2d956d 1737{
8c48b50a
FF
1738 const struct ieee80211_regdomain *regdom;
1739 enum nl80211_dfs_regions region = 0;
cb2d956d
LC
1740 int i, j, iftype;
1741 int num_interfaces = 0;
1742 u32 used_iftypes = 0;
4c8dea63
JB
1743 u32 beacon_int_gcd;
1744 bool beacon_int_different;
1745
1746 /*
1747 * This is a bit strange, since the iteration used to rely only on
1748 * the data given by the driver, but here it now relies on context,
1749 * in form of the currently operating interfaces.
1750 * This is OK for all current users, and saves us from having to
1751 * push the GCD calculations into all the drivers.
1752 * In the future, this should probably rely more on data that's in
1753 * cfg80211 already - the only thing not would appear to be any new
1754 * interfaces (while being brought up) and channel/radar data.
1755 */
1756 cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
1757 &beacon_int_gcd, &beacon_int_different);
cb2d956d 1758
e227300c 1759 if (params->radar_detect) {
8c48b50a
FF
1760 rcu_read_lock();
1761 regdom = rcu_dereference(cfg80211_regdomain);
1762 if (regdom)
1763 region = regdom->dfs_region;
1764 rcu_read_unlock();
1765 }
1766
cb2d956d 1767 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
e227300c
PK
1768 num_interfaces += params->iftype_num[iftype];
1769 if (params->iftype_num[iftype] > 0 &&
e6f40511 1770 !cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
cb2d956d
LC
1771 used_iftypes |= BIT(iftype);
1772 }
1773
1774 for (i = 0; i < wiphy->n_iface_combinations; i++) {
1775 const struct ieee80211_iface_combination *c;
1776 struct ieee80211_iface_limit *limits;
1777 u32 all_iftypes = 0;
1778
1779 c = &wiphy->iface_combinations[i];
1780
1781 if (num_interfaces > c->max_interfaces)
1782 continue;
e227300c 1783 if (params->num_different_channels > c->num_different_channels)
cb2d956d
LC
1784 continue;
1785
1786 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1787 GFP_KERNEL);
1788 if (!limits)
1789 return -ENOMEM;
1790
1791 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
e6f40511 1792 if (cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
cb2d956d
LC
1793 continue;
1794 for (j = 0; j < c->n_limits; j++) {
1795 all_iftypes |= limits[j].types;
1796 if (!(limits[j].types & BIT(iftype)))
1797 continue;
e227300c 1798 if (limits[j].max < params->iftype_num[iftype])
cb2d956d 1799 goto cont;
e227300c 1800 limits[j].max -= params->iftype_num[iftype];
cb2d956d
LC
1801 }
1802 }
1803
e227300c
PK
1804 if (params->radar_detect !=
1805 (c->radar_detect_widths & params->radar_detect))
cb2d956d
LC
1806 goto cont;
1807
e227300c 1808 if (params->radar_detect && c->radar_detect_regions &&
8c48b50a
FF
1809 !(c->radar_detect_regions & BIT(region)))
1810 goto cont;
1811
cb2d956d
LC
1812 /* Finally check that all iftypes that we're currently
1813 * using are actually part of this combination. If they
1814 * aren't then we can't use this combination and have
1815 * to continue to the next.
1816 */
1817 if ((all_iftypes & used_iftypes) != used_iftypes)
1818 goto cont;
1819
4c8dea63 1820 if (beacon_int_gcd) {
0c317a02 1821 if (c->beacon_int_min_gcd &&
4c8dea63 1822 beacon_int_gcd < c->beacon_int_min_gcd)
0507a3ac 1823 goto cont;
4c8dea63 1824 if (!c->beacon_int_min_gcd && beacon_int_different)
0c317a02
PK
1825 goto cont;
1826 }
1827
cb2d956d
LC
1828 /* This combination covered all interface types and
1829 * supported the requested numbers, so we're good.
1830 */
65a124dd
MK
1831
1832 (*iter)(c, data);
cb2d956d
LC
1833 cont:
1834 kfree(limits);
1835 }
1836
65a124dd
MK
1837 return 0;
1838}
1839EXPORT_SYMBOL(cfg80211_iter_combinations);
1840
1841static void
1842cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
1843 void *data)
1844{
1845 int *num = data;
1846 (*num)++;
1847}
1848
1849int cfg80211_check_combinations(struct wiphy *wiphy,
e227300c 1850 struct iface_combination_params *params)
65a124dd
MK
1851{
1852 int err, num = 0;
1853
e227300c 1854 err = cfg80211_iter_combinations(wiphy, params,
65a124dd
MK
1855 cfg80211_iter_sum_ifcombs, &num);
1856 if (err)
1857 return err;
1858 if (num == 0)
1859 return -EBUSY;
1860
1861 return 0;
cb2d956d
LC
1862}
1863EXPORT_SYMBOL(cfg80211_check_combinations);
1864
34850ab2
JB
1865int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1866 const u8 *rates, unsigned int n_rates,
1867 u32 *mask)
1868{
1869 int i, j;
1870
a401d2bb
JB
1871 if (!sband)
1872 return -EINVAL;
1873
34850ab2
JB
1874 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1875 return -EINVAL;
1876
1877 *mask = 0;
1878
1879 for (i = 0; i < n_rates; i++) {
1880 int rate = (rates[i] & 0x7f) * 5;
1881 bool found = false;
1882
1883 for (j = 0; j < sband->n_bitrates; j++) {
1884 if (sband->bitrates[j].bitrate == rate) {
1885 found = true;
1886 *mask |= BIT(j);
1887 break;
1888 }
1889 }
1890 if (!found)
1891 return -EINVAL;
1892 }
1893
1894 /*
1895 * mask must have at least one bit set here since we
1896 * didn't accept a 0-length rates array nor allowed
1897 * entries in the array that didn't exist
1898 */
1899
1900 return 0;
1901}
11a2a357 1902
bdfbec2d
IP
1903unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
1904{
57fbcce3 1905 enum nl80211_band band;
bdfbec2d
IP
1906 unsigned int n_channels = 0;
1907
57fbcce3 1908 for (band = 0; band < NUM_NL80211_BANDS; band++)
bdfbec2d
IP
1909 if (wiphy->bands[band])
1910 n_channels += wiphy->bands[band]->n_channels;
1911
1912 return n_channels;
1913}
1914EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
1915
7406353d
AQ
1916int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
1917 struct station_info *sinfo)
1918{
1919 struct cfg80211_registered_device *rdev;
1920 struct wireless_dev *wdev;
1921
1922 wdev = dev->ieee80211_ptr;
1923 if (!wdev)
1924 return -EOPNOTSUPP;
1925
1926 rdev = wiphy_to_rdev(wdev->wiphy);
1927 if (!rdev->ops->get_station)
1928 return -EOPNOTSUPP;
1929
3c12d048
SE
1930 memset(sinfo, 0, sizeof(*sinfo));
1931
7406353d
AQ
1932 return rdev_get_station(rdev, dev, mac_addr, sinfo);
1933}
1934EXPORT_SYMBOL(cfg80211_get_station);
1935
a442b761
AB
1936void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
1937{
1938 int i;
1939
1940 if (!f)
1941 return;
1942
1943 kfree(f->serv_spec_info);
1944 kfree(f->srf_bf);
1945 kfree(f->srf_macs);
1946 for (i = 0; i < f->num_rx_filters; i++)
1947 kfree(f->rx_filters[i].filter);
1948
1949 for (i = 0; i < f->num_tx_filters; i++)
1950 kfree(f->tx_filters[i].filter);
1951
1952 kfree(f->rx_filters);
1953 kfree(f->tx_filters);
1954 kfree(f);
1955}
1956EXPORT_SYMBOL(cfg80211_free_nan_func);
1957
4787cfa0
RM
1958bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
1959 u32 center_freq_khz, u32 bw_khz)
1960{
1961 u32 start_freq_khz, end_freq_khz;
1962
1963 start_freq_khz = center_freq_khz - (bw_khz / 2);
1964 end_freq_khz = center_freq_khz + (bw_khz / 2);
1965
1966 if (start_freq_khz >= freq_range->start_freq_khz &&
1967 end_freq_khz <= freq_range->end_freq_khz)
1968 return true;
1969
1970 return false;
1971}
1972
8689c051
AS
1973int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
1974{
1d211d43
JB
1975 sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
1976 sizeof(*(sinfo->pertid)),
1977 gfp);
8689c051
AS
1978 if (!sinfo->pertid)
1979 return -ENOMEM;
1980
1981 return 0;
1982}
1983EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
1984
11a2a357
JB
1985/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1986/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1987const unsigned char rfc1042_header[] __aligned(2) =
1988 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1989EXPORT_SYMBOL(rfc1042_header);
1990
1991/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1992const unsigned char bridge_tunnel_header[] __aligned(2) =
1993 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1994EXPORT_SYMBOL(bridge_tunnel_header);
30ca1aa5
DL
1995
1996/* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
1997struct iapp_layer2_update {
1998 u8 da[ETH_ALEN]; /* broadcast */
1999 u8 sa[ETH_ALEN]; /* STA addr */
2000 __be16 len; /* 6 */
2001 u8 dsap; /* 0 */
2002 u8 ssap; /* 0 */
2003 u8 control;
2004 u8 xid_info[3];
2005} __packed;
2006
2007void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
2008{
2009 struct iapp_layer2_update *msg;
2010 struct sk_buff *skb;
2011
2012 /* Send Level 2 Update Frame to update forwarding tables in layer 2
2013 * bridge devices */
2014
2015 skb = dev_alloc_skb(sizeof(*msg));
2016 if (!skb)
2017 return;
2018 msg = skb_put(skb, sizeof(*msg));
2019
2020 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
2021 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
2022
2023 eth_broadcast_addr(msg->da);
2024 ether_addr_copy(msg->sa, addr);
2025 msg->len = htons(6);
2026 msg->dsap = 0;
2027 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
2028 msg->control = 0xaf; /* XID response lsb.1111F101.
2029 * F=0 (no poll command; unsolicited frame) */
2030 msg->xid_info[0] = 0x81; /* XID format identifier */
2031 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
2032 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
2033
2034 skb->dev = dev;
2035 skb->protocol = eth_type_trans(skb, dev);
2036 memset(skb->cb, 0, sizeof(skb->cb));
2037 netif_rx_ni(skb);
2038}
2039EXPORT_SYMBOL(cfg80211_send_layer2_update);
b0aa75f0
JB
2040
2041int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap,
2042 enum ieee80211_vht_chanwidth bw,
9166cc49
JB
2043 int mcs, bool ext_nss_bw_capable,
2044 unsigned int max_vht_nss)
b0aa75f0
JB
2045{
2046 u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map);
b0aa75f0
JB
2047 int ext_nss_bw;
2048 int supp_width;
2049 int i, mcs_encoding;
2050
2051 if (map == 0xffff)
2052 return 0;
2053
9166cc49 2054 if (WARN_ON(mcs > 9 || max_vht_nss > 8))
b0aa75f0
JB
2055 return 0;
2056 if (mcs <= 7)
2057 mcs_encoding = 0;
2058 else if (mcs == 8)
2059 mcs_encoding = 1;
2060 else
2061 mcs_encoding = 2;
2062
9166cc49
JB
2063 if (!max_vht_nss) {
2064 /* find max_vht_nss for the given MCS */
2065 for (i = 7; i >= 0; i--) {
2066 int supp = (map >> (2 * i)) & 3;
b0aa75f0 2067
9166cc49
JB
2068 if (supp == 3)
2069 continue;
b0aa75f0 2070
9166cc49
JB
2071 if (supp >= mcs_encoding) {
2072 max_vht_nss = i + 1;
2073 break;
2074 }
b0aa75f0
JB
2075 }
2076 }
2077
2078 if (!(cap->supp_mcs.tx_mcs_map &
2079 cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
2080 return max_vht_nss;
2081
2082 ext_nss_bw = le32_get_bits(cap->vht_cap_info,
2083 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
2084 supp_width = le32_get_bits(cap->vht_cap_info,
2085 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
2086
2087 /* if not capable, treat ext_nss_bw as 0 */
2088 if (!ext_nss_bw_capable)
2089 ext_nss_bw = 0;
2090
2091 /* This is invalid */
2092 if (supp_width == 3)
2093 return 0;
2094
2095 /* This is an invalid combination so pretend nothing is supported */
2096 if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2))
2097 return 0;
2098
2099 /*
2100 * Cover all the special cases according to IEEE 802.11-2016
2101 * Table 9-250. All other cases are either factor of 1 or not
2102 * valid/supported.
2103 */
2104 switch (bw) {
2105 case IEEE80211_VHT_CHANWIDTH_USE_HT:
2106 case IEEE80211_VHT_CHANWIDTH_80MHZ:
2107 if ((supp_width == 1 || supp_width == 2) &&
2108 ext_nss_bw == 3)
2109 return 2 * max_vht_nss;
2110 break;
2111 case IEEE80211_VHT_CHANWIDTH_160MHZ:
2112 if (supp_width == 0 &&
2113 (ext_nss_bw == 1 || ext_nss_bw == 2))
93bc8ac4 2114 return max_vht_nss / 2;
b0aa75f0
JB
2115 if (supp_width == 0 &&
2116 ext_nss_bw == 3)
93bc8ac4 2117 return (3 * max_vht_nss) / 4;
b0aa75f0
JB
2118 if (supp_width == 1 &&
2119 ext_nss_bw == 3)
2120 return 2 * max_vht_nss;
2121 break;
2122 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
93bc8ac4 2123 if (supp_width == 0 && ext_nss_bw == 1)
b0aa75f0
JB
2124 return 0; /* not possible */
2125 if (supp_width == 0 &&
2126 ext_nss_bw == 2)
93bc8ac4 2127 return max_vht_nss / 2;
b0aa75f0
JB
2128 if (supp_width == 0 &&
2129 ext_nss_bw == 3)
93bc8ac4 2130 return (3 * max_vht_nss) / 4;
b0aa75f0
JB
2131 if (supp_width == 1 &&
2132 ext_nss_bw == 0)
2133 return 0; /* not possible */
2134 if (supp_width == 1 &&
2135 ext_nss_bw == 1)
93bc8ac4 2136 return max_vht_nss / 2;
b0aa75f0
JB
2137 if (supp_width == 1 &&
2138 ext_nss_bw == 2)
93bc8ac4 2139 return (3 * max_vht_nss) / 4;
b0aa75f0
JB
2140 break;
2141 }
2142
2143 /* not covered or invalid combination received */
2144 return max_vht_nss;
2145}
2146EXPORT_SYMBOL(ieee80211_get_vht_max_nss);
e6f40511
MP
2147
2148bool cfg80211_iftype_allowed(struct wiphy *wiphy, enum nl80211_iftype iftype,
2149 bool is_4addr, u8 check_swif)
2150
2151{
2152 bool is_vlan = iftype == NL80211_IFTYPE_AP_VLAN;
2153
2154 switch (check_swif) {
2155 case 0:
2156 if (is_vlan && is_4addr)
2157 return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2158 return wiphy->interface_modes & BIT(iftype);
2159 case 1:
2160 if (!(wiphy->software_iftypes & BIT(iftype)) && is_vlan)
2161 return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2162 return wiphy->software_iftypes & BIT(iftype);
2163 default:
2164 break;
2165 }
2166
2167 return false;
2168}
2169EXPORT_SYMBOL(cfg80211_iftype_allowed);