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