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