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