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