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