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