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