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