1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Wireless utility functions
4 *
5 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
6 * Copyright 2013-2014 Intel Mobile Communications GmbH
7 * Copyright 2017 Intel Deutschland GmbH
8 * Copyright (C) 2018-2023 Intel Corporation
9 */
10 #include <linux/export.h>
11 #include <linux/bitops.h>
12 #include <linux/etherdevice.h>
13 #include <linux/slab.h>
14 #include <linux/ieee80211.h>
15 #include <net/cfg80211.h>
16 #include <net/ip.h>
17 #include <net/dsfield.h>
18 #include <linux/if_vlan.h>
19 #include <linux/mpls.h>
20 #include <linux/gcd.h>
21 #include <linux/bitfield.h>
22 #include <linux/nospec.h>
23 #include "core.h"
24 #include "rdev-ops.h"
25
26
27 const struct ieee80211_rate *
ieee80211_get_response_rate(struct ieee80211_supported_band * sband,u32 basic_rates,int bitrate)28 ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
29 u32 basic_rates, int bitrate)
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 }
44 EXPORT_SYMBOL(ieee80211_get_response_rate);
45
ieee80211_mandatory_rates(struct ieee80211_supported_band * sband,enum nl80211_bss_scan_width scan_width)46 u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband,
47 enum nl80211_bss_scan_width scan_width)
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
57 if (sband->band == NL80211_BAND_2GHZ) {
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 {
64 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
65 }
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 }
73 EXPORT_SYMBOL(ieee80211_mandatory_rates);
74
ieee80211_channel_to_freq_khz(int chan,enum nl80211_band band)75 u32 ieee80211_channel_to_freq_khz(int chan, enum nl80211_band band)
76 {
77 /* see 802.11 17.3.8.3.2 and Annex J
78 * there are overlapping channel numbers in 5GHz and 2GHz bands */
79 if (chan <= 0)
80 return 0; /* not supported */
81 switch (band) {
82 case NL80211_BAND_2GHZ:
83 case NL80211_BAND_LC:
84 if (chan == 14)
85 return MHZ_TO_KHZ(2484);
86 else if (chan < 14)
87 return MHZ_TO_KHZ(2407 + chan * 5);
88 break;
89 case NL80211_BAND_5GHZ:
90 if (chan >= 182 && chan <= 196)
91 return MHZ_TO_KHZ(4000 + chan * 5);
92 else
93 return MHZ_TO_KHZ(5000 + chan * 5);
94 break;
95 case NL80211_BAND_6GHZ:
96 /* see 802.11ax D6.1 27.3.23.2 */
97 if (chan == 2)
98 return MHZ_TO_KHZ(5935);
99 if (chan <= 233)
100 return MHZ_TO_KHZ(5950 + chan * 5);
101 break;
102 case NL80211_BAND_60GHZ:
103 if (chan < 7)
104 return MHZ_TO_KHZ(56160 + chan * 2160);
105 break;
106 case NL80211_BAND_S1GHZ:
107 return 902000 + chan * 500;
108 default:
109 ;
110 }
111 return 0; /* not supported */
112 }
113 EXPORT_SYMBOL(ieee80211_channel_to_freq_khz);
114
115 enum nl80211_chan_width
ieee80211_s1g_channel_width(const struct ieee80211_channel * chan)116 ieee80211_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 }
140 EXPORT_SYMBOL(ieee80211_s1g_channel_width);
141
ieee80211_freq_khz_to_channel(u32 freq)142 int ieee80211_freq_khz_to_channel(u32 freq)
143 {
144 /* TODO: just handle MHz for now */
145 freq = KHZ_TO_MHZ(freq);
146
147 /* see 802.11 17.3.8.3.2 and Annex J */
148 if (freq == 2484)
149 return 14;
150 else if (freq < 2484)
151 return (freq - 2407) / 5;
152 else if (freq >= 4910 && freq <= 4980)
153 return (freq - 4000) / 5;
154 else if (freq < 5925)
155 return (freq - 5000) / 5;
156 else if (freq == 5935)
157 return 2;
158 else if (freq <= 45000) /* DMG band lower limit */
159 /* see 802.11ax D6.1 27.3.22.2 */
160 return (freq - 5950) / 5;
161 else if (freq >= 58320 && freq <= 70200)
162 return (freq - 56160) / 2160;
163 else
164 return 0;
165 }
166 EXPORT_SYMBOL(ieee80211_freq_khz_to_channel);
167
ieee80211_get_channel_khz(struct wiphy * wiphy,u32 freq)168 struct ieee80211_channel *ieee80211_get_channel_khz(struct wiphy *wiphy,
169 u32 freq)
170 {
171 enum nl80211_band band;
172 struct ieee80211_supported_band *sband;
173 int i;
174
175 for (band = 0; band < NUM_NL80211_BANDS; band++) {
176 sband = wiphy->bands[band];
177
178 if (!sband)
179 continue;
180
181 for (i = 0; i < sband->n_channels; i++) {
182 struct ieee80211_channel *chan = &sband->channels[i];
183
184 if (ieee80211_channel_to_khz(chan) == freq)
185 return chan;
186 }
187 }
188
189 return NULL;
190 }
191 EXPORT_SYMBOL(ieee80211_get_channel_khz);
192
set_mandatory_flags_band(struct ieee80211_supported_band * sband)193 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband)
194 {
195 int i, want;
196
197 switch (sband->band) {
198 case NL80211_BAND_5GHZ:
199 case NL80211_BAND_6GHZ:
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;
212 case NL80211_BAND_2GHZ:
213 case NL80211_BAND_LC:
214 want = 7;
215 for (i = 0; i < sband->n_bitrates; i++) {
216 switch (sband->bitrates[i].bitrate) {
217 case 10:
218 case 20:
219 case 55:
220 case 110:
221 sband->bitrates[i].flags |=
222 IEEE80211_RATE_MANDATORY_B |
223 IEEE80211_RATE_MANDATORY_G;
224 want--;
225 break;
226 case 60:
227 case 120:
228 case 240:
229 sband->bitrates[i].flags |=
230 IEEE80211_RATE_MANDATORY_G;
231 want--;
232 fallthrough;
233 default:
234 sband->bitrates[i].flags |=
235 IEEE80211_RATE_ERP_G;
236 break;
237 }
238 }
239 WARN_ON(want != 0 && want != 3);
240 break;
241 case NL80211_BAND_60GHZ:
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;
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;
252 case NUM_NL80211_BANDS:
253 default:
254 WARN_ON(1);
255 break;
256 }
257 }
258
ieee80211_set_bitrate_flags(struct wiphy * wiphy)259 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
260 {
261 enum nl80211_band band;
262
263 for (band = 0; band < NUM_NL80211_BANDS; band++)
264 if (wiphy->bands[band])
265 set_mandatory_flags_band(wiphy->bands[band]);
266 }
267
cfg80211_supported_cipher_suite(struct wiphy * wiphy,u32 cipher)268 bool 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
277 static bool
cfg80211_igtk_cipher_supported(struct cfg80211_registered_device * rdev)278 cfg80211_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
cfg80211_valid_key_idx(struct cfg80211_registered_device * rdev,int key_idx,bool pairwise)296 bool cfg80211_valid_key_idx(struct cfg80211_registered_device *rdev,
297 int key_idx, bool pairwise)
298 {
299 int max_key_idx;
300
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))
307 max_key_idx = 7;
308 else if (cfg80211_igtk_cipher_supported(rdev))
309 max_key_idx = 5;
310 else
311 max_key_idx = 3;
312
313 if (key_idx < 0 || key_idx > max_key_idx)
314 return false;
315
316 return true;
317 }
318
cfg80211_validate_key_settings(struct cfg80211_registered_device * rdev,struct key_params * params,int key_idx,bool pairwise,const u8 * mac_addr)319 int 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))
324 return -EINVAL;
325
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
332 switch (params->cipher) {
333 case WLAN_CIPHER_SUITE_TKIP:
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;
339 case WLAN_CIPHER_SUITE_CCMP:
340 case WLAN_CIPHER_SUITE_CCMP_256:
341 case WLAN_CIPHER_SUITE_GCMP:
342 case WLAN_CIPHER_SUITE_GCMP_256:
343 /* IEEE802.11-2016 allows only 0 and - when supporting
344 * Extended Key ID - 1 as index for pairwise keys.
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.
349 */
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))
356 return -EINVAL;
357 } else if (pairwise && key_idx) {
358 return -EINVAL;
359 }
360 break;
361 case WLAN_CIPHER_SUITE_AES_CMAC:
362 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
363 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
364 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
365 /* Disallow BIP (group-only) cipher as pairwise cipher */
366 if (pairwise)
367 return -EINVAL;
368 if (key_idx < 4)
369 return -EINVAL;
370 break;
371 case WLAN_CIPHER_SUITE_WEP40:
372 case WLAN_CIPHER_SUITE_WEP104:
373 if (key_idx > 3)
374 return -EINVAL;
375 break;
376 default:
377 break;
378 }
379
380 switch (params->cipher) {
381 case WLAN_CIPHER_SUITE_WEP40:
382 if (params->key_len != WLAN_KEY_LEN_WEP40)
383 return -EINVAL;
384 break;
385 case WLAN_CIPHER_SUITE_TKIP:
386 if (params->key_len != WLAN_KEY_LEN_TKIP)
387 return -EINVAL;
388 break;
389 case WLAN_CIPHER_SUITE_CCMP:
390 if (params->key_len != WLAN_KEY_LEN_CCMP)
391 return -EINVAL;
392 break;
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;
405 case WLAN_CIPHER_SUITE_WEP104:
406 if (params->key_len != WLAN_KEY_LEN_WEP104)
407 return -EINVAL;
408 break;
409 case WLAN_CIPHER_SUITE_AES_CMAC:
410 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
411 return -EINVAL;
412 break;
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;
425 default:
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;
434 }
435
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:
444 case WLAN_CIPHER_SUITE_CCMP_256:
445 case WLAN_CIPHER_SUITE_GCMP:
446 case WLAN_CIPHER_SUITE_GCMP_256:
447 case WLAN_CIPHER_SUITE_AES_CMAC:
448 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
449 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
450 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
451 if (params->seq_len != 6)
452 return -EINVAL;
453 break;
454 }
455 }
456
457 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
458 return -EINVAL;
459
460 return 0;
461 }
462
ieee80211_hdrlen(__le16 fc)463 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
464 {
465 unsigned int hdrlen = 24;
466
467 if (ieee80211_is_ext(fc)) {
468 hdrlen = 4;
469 goto out;
470 }
471
472 if (ieee80211_is_data(fc)) {
473 if (ieee80211_has_a4(fc))
474 hdrlen = 30;
475 if (ieee80211_is_data_qos(fc)) {
476 hdrlen += IEEE80211_QOS_CTL_LEN;
477 if (ieee80211_has_order(fc))
478 hdrlen += IEEE80211_HT_CTL_LEN;
479 }
480 goto out;
481 }
482
483 if (ieee80211_is_mgmt(fc)) {
484 if (ieee80211_has_order(fc))
485 hdrlen += IEEE80211_HT_CTL_LEN;
486 goto out;
487 }
488
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 }
504 out:
505 return hdrlen;
506 }
507 EXPORT_SYMBOL(ieee80211_hdrlen);
508
ieee80211_get_hdrlen_from_skb(const struct sk_buff * skb)509 unsigned 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 }
522 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
523
__ieee80211_get_mesh_hdrlen(u8 flags)524 static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
525 {
526 int ae = flags & MESH_FLAGS_AE;
527 /* 802.11-2012, 8.2.4.7.3 */
528 switch (ae) {
529 default:
530 case 0:
531 return 6;
532 case MESH_FLAGS_AE_A4:
533 return 12;
534 case MESH_FLAGS_AE_A5_A6:
535 return 18;
536 }
537 }
538
ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr * meshhdr)539 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
540 {
541 return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
542 }
543 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
544
ieee80211_get_8023_tunnel_proto(const void * hdr,__be16 * proto)545 bool ieee80211_get_8023_tunnel_proto(const void *hdr, __be16 *proto)
546 {
547 const __be16 *hdr_proto = hdr + ETH_ALEN;
548
549 if (!(ether_addr_equal(hdr, rfc1042_header) &&
550 *hdr_proto != htons(ETH_P_AARP) &&
551 *hdr_proto != htons(ETH_P_IPX)) &&
552 !ether_addr_equal(hdr, bridge_tunnel_header))
553 return false;
554
555 *proto = *hdr_proto;
556
557 return true;
558 }
559 EXPORT_SYMBOL(ieee80211_get_8023_tunnel_proto);
560
ieee80211_strip_8023_mesh_hdr(struct sk_buff * skb)561 int ieee80211_strip_8023_mesh_hdr(struct sk_buff *skb)
562 {
563 const void *mesh_addr;
564 struct {
565 struct ethhdr eth;
566 u8 flags;
567 } payload;
568 int hdrlen;
569 int ret;
570
571 ret = skb_copy_bits(skb, 0, &payload, sizeof(payload));
572 if (ret)
573 return ret;
574
575 hdrlen = sizeof(payload.eth) + __ieee80211_get_mesh_hdrlen(payload.flags);
576
577 if (likely(pskb_may_pull(skb, hdrlen + 8) &&
578 ieee80211_get_8023_tunnel_proto(skb->data + hdrlen,
579 &payload.eth.h_proto)))
580 hdrlen += ETH_ALEN + 2;
581 else if (!pskb_may_pull(skb, hdrlen))
582 return -EINVAL;
583 else
584 payload.eth.h_proto = htons(skb->len - hdrlen);
585
586 mesh_addr = skb->data + sizeof(payload.eth) + ETH_ALEN;
587 switch (payload.flags & MESH_FLAGS_AE) {
588 case MESH_FLAGS_AE_A4:
589 memcpy(&payload.eth.h_source, mesh_addr, ETH_ALEN);
590 break;
591 case MESH_FLAGS_AE_A5_A6:
592 memcpy(&payload.eth, mesh_addr, 2 * ETH_ALEN);
593 break;
594 default:
595 break;
596 }
597
598 pskb_pull(skb, hdrlen - sizeof(payload.eth));
599 memcpy(skb->data, &payload.eth, sizeof(payload.eth));
600
601 return 0;
602 }
603 EXPORT_SYMBOL(ieee80211_strip_8023_mesh_hdr);
604
ieee80211_data_to_8023_exthdr(struct sk_buff * skb,struct ethhdr * ehdr,const u8 * addr,enum nl80211_iftype iftype,u8 data_offset,bool is_amsdu)605 int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
606 const u8 *addr, enum nl80211_iftype iftype,
607 u8 data_offset, bool is_amsdu)
608 {
609 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
610 struct {
611 u8 hdr[ETH_ALEN] __aligned(2);
612 __be16 proto;
613 } payload;
614 struct ethhdr tmp;
615 u16 hdrlen;
616
617 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
618 return -1;
619
620 hdrlen = ieee80211_hdrlen(hdr->frame_control) + data_offset;
621 if (skb->len < hdrlen)
622 return -1;
623
624 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
625 * header
626 * IEEE 802.11 address fields:
627 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
628 * 0 0 DA SA BSSID n/a
629 * 0 1 DA BSSID SA n/a
630 * 1 0 BSSID SA DA n/a
631 * 1 1 RA TA DA SA
632 */
633 memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
634 memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
635
636 switch (hdr->frame_control &
637 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
638 case cpu_to_le16(IEEE80211_FCTL_TODS):
639 if (unlikely(iftype != NL80211_IFTYPE_AP &&
640 iftype != NL80211_IFTYPE_AP_VLAN &&
641 iftype != NL80211_IFTYPE_P2P_GO))
642 return -1;
643 break;
644 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
645 if (unlikely(iftype != NL80211_IFTYPE_MESH_POINT &&
646 iftype != NL80211_IFTYPE_AP_VLAN &&
647 iftype != NL80211_IFTYPE_STATION))
648 return -1;
649 break;
650 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
651 if ((iftype != NL80211_IFTYPE_STATION &&
652 iftype != NL80211_IFTYPE_P2P_CLIENT &&
653 iftype != NL80211_IFTYPE_MESH_POINT) ||
654 (is_multicast_ether_addr(tmp.h_dest) &&
655 ether_addr_equal(tmp.h_source, addr)))
656 return -1;
657 break;
658 case cpu_to_le16(0):
659 if (iftype != NL80211_IFTYPE_ADHOC &&
660 iftype != NL80211_IFTYPE_STATION &&
661 iftype != NL80211_IFTYPE_OCB)
662 return -1;
663 break;
664 }
665
666 if (likely(!is_amsdu && iftype != NL80211_IFTYPE_MESH_POINT &&
667 skb_copy_bits(skb, hdrlen, &payload, sizeof(payload)) == 0 &&
668 ieee80211_get_8023_tunnel_proto(&payload, &tmp.h_proto))) {
669 /* remove RFC1042 or Bridge-Tunnel encapsulation */
670 hdrlen += ETH_ALEN + 2;
671 skb_postpull_rcsum(skb, &payload, ETH_ALEN + 2);
672 } else {
673 tmp.h_proto = htons(skb->len - hdrlen);
674 }
675
676 pskb_pull(skb, hdrlen);
677
678 if (!ehdr)
679 ehdr = skb_push(skb, sizeof(struct ethhdr));
680 memcpy(ehdr, &tmp, sizeof(tmp));
681
682 return 0;
683 }
684 EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
685
686 static void
__frame_add_frag(struct sk_buff * skb,struct page * page,void * ptr,int len,int size)687 __frame_add_frag(struct sk_buff *skb, struct page *page,
688 void *ptr, int len, int size)
689 {
690 struct skb_shared_info *sh = skb_shinfo(skb);
691 int page_offset;
692
693 get_page(page);
694 page_offset = ptr - page_address(page);
695 skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
696 }
697
698 static void
__ieee80211_amsdu_copy_frag(struct sk_buff * skb,struct sk_buff * frame,int offset,int len)699 __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
700 int offset, int len)
701 {
702 struct skb_shared_info *sh = skb_shinfo(skb);
703 const skb_frag_t *frag = &sh->frags[0];
704 struct page *frag_page;
705 void *frag_ptr;
706 int frag_len, frag_size;
707 int head_size = skb->len - skb->data_len;
708 int cur_len;
709
710 frag_page = virt_to_head_page(skb->head);
711 frag_ptr = skb->data;
712 frag_size = head_size;
713
714 while (offset >= frag_size) {
715 offset -= frag_size;
716 frag_page = skb_frag_page(frag);
717 frag_ptr = skb_frag_address(frag);
718 frag_size = skb_frag_size(frag);
719 frag++;
720 }
721
722 frag_ptr += offset;
723 frag_len = frag_size - offset;
724
725 cur_len = min(len, frag_len);
726
727 __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
728 len -= cur_len;
729
730 while (len > 0) {
731 frag_len = skb_frag_size(frag);
732 cur_len = min(len, frag_len);
733 __frame_add_frag(frame, skb_frag_page(frag),
734 skb_frag_address(frag), cur_len, frag_len);
735 len -= cur_len;
736 frag++;
737 }
738 }
739
740 static struct sk_buff *
__ieee80211_amsdu_copy(struct sk_buff * skb,unsigned int hlen,int offset,int len,bool reuse_frag,int min_len)741 __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
742 int offset, int len, bool reuse_frag,
743 int min_len)
744 {
745 struct sk_buff *frame;
746 int cur_len = len;
747
748 if (skb->len - offset < len)
749 return NULL;
750
751 /*
752 * When reusing framents, copy some data to the head to simplify
753 * ethernet header handling and speed up protocol header processing
754 * in the stack later.
755 */
756 if (reuse_frag)
757 cur_len = min_t(int, len, min_len);
758
759 /*
760 * Allocate and reserve two bytes more for payload
761 * alignment since sizeof(struct ethhdr) is 14.
762 */
763 frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
764 if (!frame)
765 return NULL;
766
767 frame->priority = skb->priority;
768 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
769 skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
770
771 len -= cur_len;
772 if (!len)
773 return frame;
774
775 offset += cur_len;
776 __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
777
778 return frame;
779 }
780
781 static u16
ieee80211_amsdu_subframe_length(void * field,u8 mesh_flags,u8 hdr_type)782 ieee80211_amsdu_subframe_length(void *field, u8 mesh_flags, u8 hdr_type)
783 {
784 __le16 *field_le = field;
785 __be16 *field_be = field;
786 u16 len;
787
788 if (hdr_type >= 2)
789 len = le16_to_cpu(*field_le);
790 else
791 len = be16_to_cpu(*field_be);
792 if (hdr_type)
793 len += __ieee80211_get_mesh_hdrlen(mesh_flags);
794
795 return len;
796 }
797
ieee80211_is_valid_amsdu(struct sk_buff * skb,u8 mesh_hdr)798 bool ieee80211_is_valid_amsdu(struct sk_buff *skb, u8 mesh_hdr)
799 {
800 int offset = 0, subframe_len, padding;
801
802 for (offset = 0; offset < skb->len; offset += subframe_len + padding) {
803 int remaining = skb->len - offset;
804 struct {
805 __be16 len;
806 u8 mesh_flags;
807 } hdr;
808 u16 len;
809
810 if (sizeof(hdr) > remaining)
811 return false;
812
813 if (skb_copy_bits(skb, offset + 2 * ETH_ALEN, &hdr, sizeof(hdr)) < 0)
814 return false;
815
816 len = ieee80211_amsdu_subframe_length(&hdr.len, hdr.mesh_flags,
817 mesh_hdr);
818 subframe_len = sizeof(struct ethhdr) + len;
819 padding = (4 - subframe_len) & 0x3;
820
821 if (subframe_len > remaining)
822 return false;
823 }
824
825 return true;
826 }
827 EXPORT_SYMBOL(ieee80211_is_valid_amsdu);
828
829
830 /*
831 * Detects if an MSDU frame was maliciously converted into an A-MSDU
832 * frame by an adversary. This is done by parsing the received frame
833 * as if it were a regular MSDU, even though the A-MSDU flag is set.
834 *
835 * For non-mesh interfaces, detection involves checking whether the
836 * payload, when interpreted as an MSDU, begins with a valid RFC1042
837 * header. This is done by comparing the A-MSDU subheader's destination
838 * address to the start of the RFC1042 header.
839 *
840 * For mesh interfaces, the MSDU includes a 6-byte Mesh Control field
841 * and an optional variable-length Mesh Address Extension field before
842 * the RFC1042 header. The position of the RFC1042 header must therefore
843 * be calculated based on the mesh header length.
844 *
845 * Since this function intentionally parses an A-MSDU frame as an MSDU,
846 * it only assumes that the A-MSDU subframe header is present, and
847 * beyond this it performs its own bounds checks under the assumption
848 * that the frame is instead parsed as a non-aggregated MSDU.
849 */
850 static bool
is_amsdu_aggregation_attack(struct ethhdr * eth,struct sk_buff * skb,enum nl80211_iftype iftype)851 is_amsdu_aggregation_attack(struct ethhdr *eth, struct sk_buff *skb,
852 enum nl80211_iftype iftype)
853 {
854 int offset;
855
856 /* Non-mesh case can be directly compared */
857 if (iftype != NL80211_IFTYPE_MESH_POINT)
858 return ether_addr_equal(eth->h_dest, rfc1042_header);
859
860 offset = __ieee80211_get_mesh_hdrlen(eth->h_dest[0]);
861 if (offset == 6) {
862 /* Mesh case with empty address extension field */
863 return ether_addr_equal(eth->h_source, rfc1042_header);
864 } else if (offset + ETH_ALEN <= skb->len) {
865 /* Mesh case with non-empty address extension field */
866 u8 temp[ETH_ALEN];
867
868 skb_copy_bits(skb, offset, temp, ETH_ALEN);
869 return ether_addr_equal(temp, rfc1042_header);
870 }
871
872 return false;
873 }
874
ieee80211_amsdu_to_8023s(struct sk_buff * skb,struct sk_buff_head * list,const u8 * addr,enum nl80211_iftype iftype,const unsigned int extra_headroom,const u8 * check_da,const u8 * check_sa,u8 mesh_control)875 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
876 const u8 *addr, enum nl80211_iftype iftype,
877 const unsigned int extra_headroom,
878 const u8 *check_da, const u8 *check_sa,
879 u8 mesh_control)
880 {
881 unsigned int hlen = ALIGN(extra_headroom, 4);
882 struct sk_buff *frame = NULL;
883 int offset = 0;
884 struct {
885 struct ethhdr eth;
886 uint8_t flags;
887 } hdr;
888 bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
889 bool reuse_skb = false;
890 bool last = false;
891 int copy_len = sizeof(hdr.eth);
892
893 if (iftype == NL80211_IFTYPE_MESH_POINT)
894 copy_len = sizeof(hdr);
895
896 while (!last) {
897 int remaining = skb->len - offset;
898 unsigned int subframe_len;
899 int len, mesh_len = 0;
900 u8 padding;
901
902 if (copy_len > remaining)
903 goto purge;
904
905 skb_copy_bits(skb, offset, &hdr, copy_len);
906 if (iftype == NL80211_IFTYPE_MESH_POINT)
907 mesh_len = __ieee80211_get_mesh_hdrlen(hdr.flags);
908 len = ieee80211_amsdu_subframe_length(&hdr.eth.h_proto, hdr.flags,
909 mesh_control);
910 subframe_len = sizeof(struct ethhdr) + len;
911 padding = (4 - subframe_len) & 0x3;
912
913 /* the last MSDU has no padding */
914 if (subframe_len > remaining)
915 goto purge;
916 /* mitigate A-MSDU aggregation injection attacks, to be
917 * checked when processing first subframe (offset == 0).
918 */
919 if (offset == 0 && is_amsdu_aggregation_attack(&hdr.eth, skb, iftype))
920 goto purge;
921
922 offset += sizeof(struct ethhdr);
923 last = remaining <= subframe_len + padding;
924
925 /* FIXME: should we really accept multicast DA? */
926 if ((check_da && !is_multicast_ether_addr(hdr.eth.h_dest) &&
927 !ether_addr_equal(check_da, hdr.eth.h_dest)) ||
928 (check_sa && !ether_addr_equal(check_sa, hdr.eth.h_source))) {
929 offset += len + padding;
930 continue;
931 }
932
933 /* reuse skb for the last subframe */
934 if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
935 skb_pull(skb, offset);
936 frame = skb;
937 reuse_skb = true;
938 } else {
939 frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
940 reuse_frag, 32 + mesh_len);
941 if (!frame)
942 goto purge;
943
944 offset += len + padding;
945 }
946
947 skb_reset_network_header(frame);
948 frame->dev = skb->dev;
949 frame->priority = skb->priority;
950
951 if (likely(iftype != NL80211_IFTYPE_MESH_POINT &&
952 ieee80211_get_8023_tunnel_proto(frame->data, &hdr.eth.h_proto)))
953 skb_pull(frame, ETH_ALEN + 2);
954
955 memcpy(skb_push(frame, sizeof(hdr.eth)), &hdr.eth, sizeof(hdr.eth));
956 __skb_queue_tail(list, frame);
957 }
958
959 if (!reuse_skb)
960 dev_kfree_skb(skb);
961
962 return;
963
964 purge:
965 __skb_queue_purge(list);
966 dev_kfree_skb(skb);
967 }
968 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
969
970 /* Given a data frame determine the 802.1p/1d tag to use. */
cfg80211_classify8021d(struct sk_buff * skb,struct cfg80211_qos_map * qos_map)971 unsigned int cfg80211_classify8021d(struct sk_buff *skb,
972 struct cfg80211_qos_map *qos_map)
973 {
974 unsigned int dscp;
975 unsigned char vlan_priority;
976 unsigned int ret;
977
978 /* skb->priority values from 256->263 are magic values to
979 * directly indicate a specific 802.1d priority. This is used
980 * to allow 802.1d priority to be passed directly in from VLAN
981 * tags, etc.
982 */
983 if (skb->priority >= 256 && skb->priority <= 263) {
984 ret = skb->priority - 256;
985 goto out;
986 }
987
988 if (skb_vlan_tag_present(skb)) {
989 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
990 >> VLAN_PRIO_SHIFT;
991 if (vlan_priority > 0) {
992 ret = vlan_priority;
993 goto out;
994 }
995 }
996
997 switch (skb->protocol) {
998 case htons(ETH_P_IP):
999 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
1000 break;
1001 case htons(ETH_P_IPV6):
1002 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
1003 break;
1004 case htons(ETH_P_MPLS_UC):
1005 case htons(ETH_P_MPLS_MC): {
1006 struct mpls_label mpls_tmp, *mpls;
1007
1008 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
1009 sizeof(*mpls), &mpls_tmp);
1010 if (!mpls)
1011 return 0;
1012
1013 ret = (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
1014 >> MPLS_LS_TC_SHIFT;
1015 goto out;
1016 }
1017 case htons(ETH_P_80221):
1018 /* 802.21 is always network control traffic */
1019 return 7;
1020 default:
1021 return 0;
1022 }
1023
1024 if (qos_map) {
1025 unsigned int i, tmp_dscp = dscp >> 2;
1026
1027 for (i = 0; i < qos_map->num_des; i++) {
1028 if (tmp_dscp == qos_map->dscp_exception[i].dscp) {
1029 ret = qos_map->dscp_exception[i].up;
1030 goto out;
1031 }
1032 }
1033
1034 for (i = 0; i < 8; i++) {
1035 if (tmp_dscp >= qos_map->up[i].low &&
1036 tmp_dscp <= qos_map->up[i].high) {
1037 ret = i;
1038 goto out;
1039 }
1040 }
1041 }
1042
1043 ret = dscp >> 5;
1044 out:
1045 return array_index_nospec(ret, IEEE80211_NUM_TIDS);
1046 }
1047 EXPORT_SYMBOL(cfg80211_classify8021d);
1048
ieee80211_bss_get_elem(struct cfg80211_bss * bss,u8 id)1049 const struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id)
1050 {
1051 const struct cfg80211_bss_ies *ies;
1052
1053 ies = rcu_dereference(bss->ies);
1054 if (!ies)
1055 return NULL;
1056
1057 return cfg80211_find_elem(id, ies->data, ies->len);
1058 }
1059 EXPORT_SYMBOL(ieee80211_bss_get_elem);
1060
cfg80211_upload_connect_keys(struct wireless_dev * wdev)1061 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
1062 {
1063 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1064 struct net_device *dev = wdev->netdev;
1065 int i;
1066
1067 if (!wdev->connect_keys)
1068 return;
1069
1070 for (i = 0; i < 4; i++) {
1071 if (!wdev->connect_keys->params[i].cipher)
1072 continue;
1073 if (rdev_add_key(rdev, dev, -1, i, false, NULL,
1074 &wdev->connect_keys->params[i])) {
1075 netdev_err(dev, "failed to set key %d\n", i);
1076 continue;
1077 }
1078 if (wdev->connect_keys->def == i &&
1079 rdev_set_default_key(rdev, dev, -1, i, true, true)) {
1080 netdev_err(dev, "failed to set defkey %d\n", i);
1081 continue;
1082 }
1083 }
1084
1085 kfree_sensitive(wdev->connect_keys);
1086 wdev->connect_keys = NULL;
1087 }
1088
cfg80211_process_wdev_events(struct wireless_dev * wdev)1089 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
1090 {
1091 struct cfg80211_event *ev;
1092 unsigned long flags;
1093
1094 spin_lock_irqsave(&wdev->event_lock, flags);
1095 while (!list_empty(&wdev->event_list)) {
1096 ev = list_first_entry(&wdev->event_list,
1097 struct cfg80211_event, list);
1098 list_del(&ev->list);
1099 spin_unlock_irqrestore(&wdev->event_lock, flags);
1100
1101 wdev_lock(wdev);
1102 switch (ev->type) {
1103 case EVENT_CONNECT_RESULT:
1104 __cfg80211_connect_result(
1105 wdev->netdev,
1106 &ev->cr,
1107 ev->cr.status == WLAN_STATUS_SUCCESS);
1108 break;
1109 case EVENT_ROAMED:
1110 __cfg80211_roamed(wdev, &ev->rm);
1111 break;
1112 case EVENT_DISCONNECTED:
1113 __cfg80211_disconnected(wdev->netdev,
1114 ev->dc.ie, ev->dc.ie_len,
1115 ev->dc.reason,
1116 !ev->dc.locally_generated);
1117 break;
1118 case EVENT_IBSS_JOINED:
1119 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
1120 ev->ij.channel);
1121 break;
1122 case EVENT_STOPPED:
1123 __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
1124 break;
1125 case EVENT_PORT_AUTHORIZED:
1126 __cfg80211_port_authorized(wdev, ev->pa.bssid,
1127 ev->pa.td_bitmap,
1128 ev->pa.td_bitmap_len);
1129 break;
1130 }
1131 wdev_unlock(wdev);
1132
1133 kfree(ev);
1134
1135 spin_lock_irqsave(&wdev->event_lock, flags);
1136 }
1137 spin_unlock_irqrestore(&wdev->event_lock, flags);
1138 }
1139
cfg80211_process_rdev_events(struct cfg80211_registered_device * rdev)1140 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
1141 {
1142 struct wireless_dev *wdev;
1143
1144 lockdep_assert_held(&rdev->wiphy.mtx);
1145
1146 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
1147 cfg80211_process_wdev_events(wdev);
1148 }
1149
cfg80211_change_iface(struct cfg80211_registered_device * rdev,struct net_device * dev,enum nl80211_iftype ntype,struct vif_params * params)1150 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
1151 struct net_device *dev, enum nl80211_iftype ntype,
1152 struct vif_params *params)
1153 {
1154 int err;
1155 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
1156
1157 lockdep_assert_held(&rdev->wiphy.mtx);
1158
1159 /* don't support changing VLANs, you just re-create them */
1160 if (otype == NL80211_IFTYPE_AP_VLAN)
1161 return -EOPNOTSUPP;
1162
1163 /* cannot change into P2P device or NAN */
1164 if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
1165 ntype == NL80211_IFTYPE_NAN)
1166 return -EOPNOTSUPP;
1167
1168 if (!rdev->ops->change_virtual_intf ||
1169 !(rdev->wiphy.interface_modes & (1 << ntype)))
1170 return -EOPNOTSUPP;
1171
1172 if (ntype != otype) {
1173 /* if it's part of a bridge, reject changing type to station/ibss */
1174 if (netif_is_bridge_port(dev) &&
1175 (ntype == NL80211_IFTYPE_ADHOC ||
1176 ntype == NL80211_IFTYPE_STATION ||
1177 ntype == NL80211_IFTYPE_P2P_CLIENT))
1178 return -EBUSY;
1179
1180 dev->ieee80211_ptr->use_4addr = false;
1181 wdev_lock(dev->ieee80211_ptr);
1182 rdev_set_qos_map(rdev, dev, NULL);
1183 wdev_unlock(dev->ieee80211_ptr);
1184
1185 switch (otype) {
1186 case NL80211_IFTYPE_AP:
1187 case NL80211_IFTYPE_P2P_GO:
1188 cfg80211_stop_ap(rdev, dev, -1, true);
1189 break;
1190 case NL80211_IFTYPE_ADHOC:
1191 cfg80211_leave_ibss(rdev, dev, false);
1192 break;
1193 case NL80211_IFTYPE_STATION:
1194 case NL80211_IFTYPE_P2P_CLIENT:
1195 wdev_lock(dev->ieee80211_ptr);
1196 cfg80211_disconnect(rdev, dev,
1197 WLAN_REASON_DEAUTH_LEAVING, true);
1198 wdev_unlock(dev->ieee80211_ptr);
1199 break;
1200 case NL80211_IFTYPE_MESH_POINT:
1201 /* mesh should be handled? */
1202 break;
1203 case NL80211_IFTYPE_OCB:
1204 cfg80211_leave_ocb(rdev, dev);
1205 break;
1206 default:
1207 break;
1208 }
1209
1210 cfg80211_process_rdev_events(rdev);
1211 cfg80211_mlme_purge_registrations(dev->ieee80211_ptr);
1212
1213 memset(&dev->ieee80211_ptr->u, 0,
1214 sizeof(dev->ieee80211_ptr->u));
1215 memset(&dev->ieee80211_ptr->links, 0,
1216 sizeof(dev->ieee80211_ptr->links));
1217 }
1218
1219 err = rdev_change_virtual_intf(rdev, dev, ntype, params);
1220
1221 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
1222
1223 if (!err && params && params->use_4addr != -1)
1224 dev->ieee80211_ptr->use_4addr = params->use_4addr;
1225
1226 if (!err) {
1227 dev->priv_flags &= ~IFF_DONT_BRIDGE;
1228 switch (ntype) {
1229 case NL80211_IFTYPE_STATION:
1230 if (dev->ieee80211_ptr->use_4addr)
1231 break;
1232 fallthrough;
1233 case NL80211_IFTYPE_OCB:
1234 case NL80211_IFTYPE_P2P_CLIENT:
1235 case NL80211_IFTYPE_ADHOC:
1236 dev->priv_flags |= IFF_DONT_BRIDGE;
1237 break;
1238 case NL80211_IFTYPE_P2P_GO:
1239 case NL80211_IFTYPE_AP:
1240 case NL80211_IFTYPE_AP_VLAN:
1241 case NL80211_IFTYPE_MESH_POINT:
1242 /* bridging OK */
1243 break;
1244 case NL80211_IFTYPE_MONITOR:
1245 /* monitor can't bridge anyway */
1246 break;
1247 case NL80211_IFTYPE_UNSPECIFIED:
1248 case NUM_NL80211_IFTYPES:
1249 /* not happening */
1250 break;
1251 case NL80211_IFTYPE_P2P_DEVICE:
1252 case NL80211_IFTYPE_WDS:
1253 case NL80211_IFTYPE_NAN:
1254 WARN_ON(1);
1255 break;
1256 }
1257 }
1258
1259 if (!err && ntype != otype && netif_running(dev)) {
1260 cfg80211_update_iface_num(rdev, ntype, 1);
1261 cfg80211_update_iface_num(rdev, otype, -1);
1262 }
1263
1264 return err;
1265 }
1266
cfg80211_calculate_bitrate_ht(struct rate_info * rate)1267 static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
1268 {
1269 int modulation, streams, bitrate;
1270
1271 /* the formula below does only work for MCS values smaller than 32 */
1272 if (WARN_ON_ONCE(rate->mcs >= 32))
1273 return 0;
1274
1275 modulation = rate->mcs & 7;
1276 streams = (rate->mcs >> 3) + 1;
1277
1278 bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1279
1280 if (modulation < 4)
1281 bitrate *= (modulation + 1);
1282 else if (modulation == 4)
1283 bitrate *= (modulation + 2);
1284 else
1285 bitrate *= (modulation + 3);
1286
1287 bitrate *= streams;
1288
1289 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1290 bitrate = (bitrate / 9) * 10;
1291
1292 /* do NOT round down here */
1293 return (bitrate + 50000) / 100000;
1294 }
1295
cfg80211_calculate_bitrate_dmg(struct rate_info * rate)1296 static u32 cfg80211_calculate_bitrate_dmg(struct rate_info *rate)
1297 {
1298 static const u32 __mcs2bitrate[] = {
1299 /* control PHY */
1300 [0] = 275,
1301 /* SC PHY */
1302 [1] = 3850,
1303 [2] = 7700,
1304 [3] = 9625,
1305 [4] = 11550,
1306 [5] = 12512, /* 1251.25 mbps */
1307 [6] = 15400,
1308 [7] = 19250,
1309 [8] = 23100,
1310 [9] = 25025,
1311 [10] = 30800,
1312 [11] = 38500,
1313 [12] = 46200,
1314 /* OFDM PHY */
1315 [13] = 6930,
1316 [14] = 8662, /* 866.25 mbps */
1317 [15] = 13860,
1318 [16] = 17325,
1319 [17] = 20790,
1320 [18] = 27720,
1321 [19] = 34650,
1322 [20] = 41580,
1323 [21] = 45045,
1324 [22] = 51975,
1325 [23] = 62370,
1326 [24] = 67568, /* 6756.75 mbps */
1327 /* LP-SC PHY */
1328 [25] = 6260,
1329 [26] = 8340,
1330 [27] = 11120,
1331 [28] = 12510,
1332 [29] = 16680,
1333 [30] = 22240,
1334 [31] = 25030,
1335 };
1336
1337 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1338 return 0;
1339
1340 return __mcs2bitrate[rate->mcs];
1341 }
1342
cfg80211_calculate_bitrate_extended_sc_dmg(struct rate_info * rate)1343 static u32 cfg80211_calculate_bitrate_extended_sc_dmg(struct rate_info *rate)
1344 {
1345 static const u32 __mcs2bitrate[] = {
1346 [6 - 6] = 26950, /* MCS 9.1 : 2695.0 mbps */
1347 [7 - 6] = 50050, /* MCS 12.1 */
1348 [8 - 6] = 53900,
1349 [9 - 6] = 57750,
1350 [10 - 6] = 63900,
1351 [11 - 6] = 75075,
1352 [12 - 6] = 80850,
1353 };
1354
1355 /* Extended SC MCS not defined for base MCS below 6 or above 12 */
1356 if (WARN_ON_ONCE(rate->mcs < 6 || rate->mcs > 12))
1357 return 0;
1358
1359 return __mcs2bitrate[rate->mcs - 6];
1360 }
1361
cfg80211_calculate_bitrate_edmg(struct rate_info * rate)1362 static u32 cfg80211_calculate_bitrate_edmg(struct rate_info *rate)
1363 {
1364 static const u32 __mcs2bitrate[] = {
1365 /* control PHY */
1366 [0] = 275,
1367 /* SC PHY */
1368 [1] = 3850,
1369 [2] = 7700,
1370 [3] = 9625,
1371 [4] = 11550,
1372 [5] = 12512, /* 1251.25 mbps */
1373 [6] = 13475,
1374 [7] = 15400,
1375 [8] = 19250,
1376 [9] = 23100,
1377 [10] = 25025,
1378 [11] = 26950,
1379 [12] = 30800,
1380 [13] = 38500,
1381 [14] = 46200,
1382 [15] = 50050,
1383 [16] = 53900,
1384 [17] = 57750,
1385 [18] = 69300,
1386 [19] = 75075,
1387 [20] = 80850,
1388 };
1389
1390 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1391 return 0;
1392
1393 return __mcs2bitrate[rate->mcs] * rate->n_bonded_ch;
1394 }
1395
cfg80211_calculate_bitrate_vht(struct rate_info * rate)1396 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1397 {
1398 static const u32 base[4][12] = {
1399 { 6500000,
1400 13000000,
1401 19500000,
1402 26000000,
1403 39000000,
1404 52000000,
1405 58500000,
1406 65000000,
1407 78000000,
1408 /* not in the spec, but some devices use this: */
1409 86700000,
1410 97500000,
1411 108300000,
1412 },
1413 { 13500000,
1414 27000000,
1415 40500000,
1416 54000000,
1417 81000000,
1418 108000000,
1419 121500000,
1420 135000000,
1421 162000000,
1422 180000000,
1423 202500000,
1424 225000000,
1425 },
1426 { 29300000,
1427 58500000,
1428 87800000,
1429 117000000,
1430 175500000,
1431 234000000,
1432 263300000,
1433 292500000,
1434 351000000,
1435 390000000,
1436 438800000,
1437 487500000,
1438 },
1439 { 58500000,
1440 117000000,
1441 175500000,
1442 234000000,
1443 351000000,
1444 468000000,
1445 526500000,
1446 585000000,
1447 702000000,
1448 780000000,
1449 877500000,
1450 975000000,
1451 },
1452 };
1453 u32 bitrate;
1454 int idx;
1455
1456 if (rate->mcs > 11)
1457 goto warn;
1458
1459 switch (rate->bw) {
1460 case RATE_INFO_BW_160:
1461 idx = 3;
1462 break;
1463 case RATE_INFO_BW_80:
1464 idx = 2;
1465 break;
1466 case RATE_INFO_BW_40:
1467 idx = 1;
1468 break;
1469 case RATE_INFO_BW_5:
1470 case RATE_INFO_BW_10:
1471 default:
1472 goto warn;
1473 case RATE_INFO_BW_20:
1474 idx = 0;
1475 }
1476
1477 bitrate = base[idx][rate->mcs];
1478 bitrate *= rate->nss;
1479
1480 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1481 bitrate = (bitrate / 9) * 10;
1482
1483 /* do NOT round down here */
1484 return (bitrate + 50000) / 100000;
1485 warn:
1486 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1487 rate->bw, rate->mcs, rate->nss);
1488 return 0;
1489 }
1490
cfg80211_calculate_bitrate_he(struct rate_info * rate)1491 static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate)
1492 {
1493 #define SCALE 6144
1494 u32 mcs_divisors[14] = {
1495 102399, /* 16.666666... */
1496 51201, /* 8.333333... */
1497 34134, /* 5.555555... */
1498 25599, /* 4.166666... */
1499 17067, /* 2.777777... */
1500 12801, /* 2.083333... */
1501 11377, /* 1.851725... */
1502 10239, /* 1.666666... */
1503 8532, /* 1.388888... */
1504 7680, /* 1.250000... */
1505 6828, /* 1.111111... */
1506 6144, /* 1.000000... */
1507 5690, /* 0.926106... */
1508 5120, /* 0.833333... */
1509 };
1510 u32 rates_160M[3] = { 960777777, 907400000, 816666666 };
1511 u32 rates_996[3] = { 480388888, 453700000, 408333333 };
1512 u32 rates_484[3] = { 229411111, 216666666, 195000000 };
1513 u32 rates_242[3] = { 114711111, 108333333, 97500000 };
1514 u32 rates_106[3] = { 40000000, 37777777, 34000000 };
1515 u32 rates_52[3] = { 18820000, 17777777, 16000000 };
1516 u32 rates_26[3] = { 9411111, 8888888, 8000000 };
1517 u64 tmp;
1518 u32 result;
1519
1520 if (WARN_ON_ONCE(rate->mcs > 13))
1521 return 0;
1522
1523 if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2))
1524 return 0;
1525 if (WARN_ON_ONCE(rate->he_ru_alloc >
1526 NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1527 return 0;
1528 if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1529 return 0;
1530
1531 if (rate->bw == RATE_INFO_BW_160 ||
1532 (rate->bw == RATE_INFO_BW_HE_RU &&
1533 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1534 result = rates_160M[rate->he_gi];
1535 else if (rate->bw == RATE_INFO_BW_80 ||
1536 (rate->bw == RATE_INFO_BW_HE_RU &&
1537 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
1538 result = rates_996[rate->he_gi];
1539 else if (rate->bw == RATE_INFO_BW_40 ||
1540 (rate->bw == RATE_INFO_BW_HE_RU &&
1541 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
1542 result = rates_484[rate->he_gi];
1543 else if (rate->bw == RATE_INFO_BW_20 ||
1544 (rate->bw == RATE_INFO_BW_HE_RU &&
1545 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
1546 result = rates_242[rate->he_gi];
1547 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1548 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
1549 result = rates_106[rate->he_gi];
1550 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1551 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
1552 result = rates_52[rate->he_gi];
1553 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1554 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
1555 result = rates_26[rate->he_gi];
1556 else {
1557 WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1558 rate->bw, rate->he_ru_alloc);
1559 return 0;
1560 }
1561
1562 /* now scale to the appropriate MCS */
1563 tmp = result;
1564 tmp *= SCALE;
1565 do_div(tmp, mcs_divisors[rate->mcs]);
1566 result = tmp;
1567
1568 /* and take NSS, DCM into account */
1569 result = (result * rate->nss) / 8;
1570 if (rate->he_dcm)
1571 result /= 2;
1572
1573 return result / 10000;
1574 }
1575
cfg80211_calculate_bitrate_eht(struct rate_info * rate)1576 static u32 cfg80211_calculate_bitrate_eht(struct rate_info *rate)
1577 {
1578 #define SCALE 6144
1579 static const u32 mcs_divisors[16] = {
1580 102399, /* 16.666666... */
1581 51201, /* 8.333333... */
1582 34134, /* 5.555555... */
1583 25599, /* 4.166666... */
1584 17067, /* 2.777777... */
1585 12801, /* 2.083333... */
1586 11377, /* 1.851725... */
1587 10239, /* 1.666666... */
1588 8532, /* 1.388888... */
1589 7680, /* 1.250000... */
1590 6828, /* 1.111111... */
1591 6144, /* 1.000000... */
1592 5690, /* 0.926106... */
1593 5120, /* 0.833333... */
1594 409600, /* 66.666666... */
1595 204800, /* 33.333333... */
1596 };
1597 static const u32 rates_996[3] = { 480388888, 453700000, 408333333 };
1598 static const u32 rates_484[3] = { 229411111, 216666666, 195000000 };
1599 static const u32 rates_242[3] = { 114711111, 108333333, 97500000 };
1600 static const u32 rates_106[3] = { 40000000, 37777777, 34000000 };
1601 static const u32 rates_52[3] = { 18820000, 17777777, 16000000 };
1602 static const u32 rates_26[3] = { 9411111, 8888888, 8000000 };
1603 u64 tmp;
1604 u32 result;
1605
1606 if (WARN_ON_ONCE(rate->mcs > 15))
1607 return 0;
1608 if (WARN_ON_ONCE(rate->eht_gi > NL80211_RATE_INFO_EHT_GI_3_2))
1609 return 0;
1610 if (WARN_ON_ONCE(rate->eht_ru_alloc >
1611 NL80211_RATE_INFO_EHT_RU_ALLOC_4x996))
1612 return 0;
1613 if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1614 return 0;
1615
1616 /* Bandwidth checks for MCS 14 */
1617 if (rate->mcs == 14) {
1618 if ((rate->bw != RATE_INFO_BW_EHT_RU &&
1619 rate->bw != RATE_INFO_BW_80 &&
1620 rate->bw != RATE_INFO_BW_160 &&
1621 rate->bw != RATE_INFO_BW_320) ||
1622 (rate->bw == RATE_INFO_BW_EHT_RU &&
1623 rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_996 &&
1624 rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_2x996 &&
1625 rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_4x996)) {
1626 WARN(1, "invalid EHT BW for MCS 14: bw:%d, ru:%d\n",
1627 rate->bw, rate->eht_ru_alloc);
1628 return 0;
1629 }
1630 }
1631
1632 if (rate->bw == RATE_INFO_BW_320 ||
1633 (rate->bw == RATE_INFO_BW_EHT_RU &&
1634 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_4x996))
1635 result = 4 * rates_996[rate->eht_gi];
1636 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1637 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_3x996P484)
1638 result = 3 * rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1639 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1640 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_3x996)
1641 result = 3 * rates_996[rate->eht_gi];
1642 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1643 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_2x996P484)
1644 result = 2 * rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1645 else if (rate->bw == RATE_INFO_BW_160 ||
1646 (rate->bw == RATE_INFO_BW_EHT_RU &&
1647 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_2x996))
1648 result = 2 * rates_996[rate->eht_gi];
1649 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1650 rate->eht_ru_alloc ==
1651 NL80211_RATE_INFO_EHT_RU_ALLOC_996P484P242)
1652 result = rates_996[rate->eht_gi] + rates_484[rate->eht_gi]
1653 + rates_242[rate->eht_gi];
1654 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1655 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_996P484)
1656 result = rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1657 else if (rate->bw == RATE_INFO_BW_80 ||
1658 (rate->bw == RATE_INFO_BW_EHT_RU &&
1659 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_996))
1660 result = rates_996[rate->eht_gi];
1661 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1662 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_484P242)
1663 result = rates_484[rate->eht_gi] + rates_242[rate->eht_gi];
1664 else if (rate->bw == RATE_INFO_BW_40 ||
1665 (rate->bw == RATE_INFO_BW_EHT_RU &&
1666 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_484))
1667 result = rates_484[rate->eht_gi];
1668 else if (rate->bw == RATE_INFO_BW_20 ||
1669 (rate->bw == RATE_INFO_BW_EHT_RU &&
1670 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_242))
1671 result = rates_242[rate->eht_gi];
1672 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1673 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_106P26)
1674 result = rates_106[rate->eht_gi] + rates_26[rate->eht_gi];
1675 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1676 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_106)
1677 result = rates_106[rate->eht_gi];
1678 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1679 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_52P26)
1680 result = rates_52[rate->eht_gi] + rates_26[rate->eht_gi];
1681 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1682 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_52)
1683 result = rates_52[rate->eht_gi];
1684 else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1685 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_26)
1686 result = rates_26[rate->eht_gi];
1687 else {
1688 WARN(1, "invalid EHT MCS: bw:%d, ru:%d\n",
1689 rate->bw, rate->eht_ru_alloc);
1690 return 0;
1691 }
1692
1693 /* now scale to the appropriate MCS */
1694 tmp = result;
1695 tmp *= SCALE;
1696 do_div(tmp, mcs_divisors[rate->mcs]);
1697
1698 /* and take NSS */
1699 tmp *= rate->nss;
1700 do_div(tmp, 8);
1701
1702 result = tmp;
1703
1704 return result / 10000;
1705 }
1706
cfg80211_calculate_bitrate_s1g(struct rate_info * rate)1707 static u32 cfg80211_calculate_bitrate_s1g(struct rate_info *rate)
1708 {
1709 /* For 1, 2, 4, 8 and 16 MHz channels */
1710 static const u32 base[5][11] = {
1711 { 300000,
1712 600000,
1713 900000,
1714 1200000,
1715 1800000,
1716 2400000,
1717 2700000,
1718 3000000,
1719 3600000,
1720 4000000,
1721 /* MCS 10 supported in 1 MHz only */
1722 150000,
1723 },
1724 { 650000,
1725 1300000,
1726 1950000,
1727 2600000,
1728 3900000,
1729 5200000,
1730 5850000,
1731 6500000,
1732 7800000,
1733 /* MCS 9 not valid */
1734 },
1735 { 1350000,
1736 2700000,
1737 4050000,
1738 5400000,
1739 8100000,
1740 10800000,
1741 12150000,
1742 13500000,
1743 16200000,
1744 18000000,
1745 },
1746 { 2925000,
1747 5850000,
1748 8775000,
1749 11700000,
1750 17550000,
1751 23400000,
1752 26325000,
1753 29250000,
1754 35100000,
1755 39000000,
1756 },
1757 { 8580000,
1758 11700000,
1759 17550000,
1760 23400000,
1761 35100000,
1762 46800000,
1763 52650000,
1764 58500000,
1765 70200000,
1766 78000000,
1767 },
1768 };
1769 u32 bitrate;
1770 /* default is 1 MHz index */
1771 int idx = 0;
1772
1773 if (rate->mcs >= 11)
1774 goto warn;
1775
1776 switch (rate->bw) {
1777 case RATE_INFO_BW_16:
1778 idx = 4;
1779 break;
1780 case RATE_INFO_BW_8:
1781 idx = 3;
1782 break;
1783 case RATE_INFO_BW_4:
1784 idx = 2;
1785 break;
1786 case RATE_INFO_BW_2:
1787 idx = 1;
1788 break;
1789 case RATE_INFO_BW_1:
1790 idx = 0;
1791 break;
1792 case RATE_INFO_BW_5:
1793 case RATE_INFO_BW_10:
1794 case RATE_INFO_BW_20:
1795 case RATE_INFO_BW_40:
1796 case RATE_INFO_BW_80:
1797 case RATE_INFO_BW_160:
1798 default:
1799 goto warn;
1800 }
1801
1802 bitrate = base[idx][rate->mcs];
1803 bitrate *= rate->nss;
1804
1805 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1806 bitrate = (bitrate / 9) * 10;
1807 /* do NOT round down here */
1808 return (bitrate + 50000) / 100000;
1809 warn:
1810 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1811 rate->bw, rate->mcs, rate->nss);
1812 return 0;
1813 }
1814
cfg80211_calculate_bitrate(struct rate_info * rate)1815 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1816 {
1817 if (rate->flags & RATE_INFO_FLAGS_MCS)
1818 return cfg80211_calculate_bitrate_ht(rate);
1819 if (rate->flags & RATE_INFO_FLAGS_DMG)
1820 return cfg80211_calculate_bitrate_dmg(rate);
1821 if (rate->flags & RATE_INFO_FLAGS_EXTENDED_SC_DMG)
1822 return cfg80211_calculate_bitrate_extended_sc_dmg(rate);
1823 if (rate->flags & RATE_INFO_FLAGS_EDMG)
1824 return cfg80211_calculate_bitrate_edmg(rate);
1825 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1826 return cfg80211_calculate_bitrate_vht(rate);
1827 if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1828 return cfg80211_calculate_bitrate_he(rate);
1829 if (rate->flags & RATE_INFO_FLAGS_EHT_MCS)
1830 return cfg80211_calculate_bitrate_eht(rate);
1831 if (rate->flags & RATE_INFO_FLAGS_S1G_MCS)
1832 return cfg80211_calculate_bitrate_s1g(rate);
1833
1834 return rate->legacy;
1835 }
1836 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1837
cfg80211_get_p2p_attr(const u8 * ies,unsigned int len,enum ieee80211_p2p_attr_id attr,u8 * buf,unsigned int bufsize)1838 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1839 enum ieee80211_p2p_attr_id attr,
1840 u8 *buf, unsigned int bufsize)
1841 {
1842 u8 *out = buf;
1843 u16 attr_remaining = 0;
1844 bool desired_attr = false;
1845 u16 desired_len = 0;
1846
1847 while (len > 0) {
1848 unsigned int iedatalen;
1849 unsigned int copy;
1850 const u8 *iedata;
1851
1852 if (len < 2)
1853 return -EILSEQ;
1854 iedatalen = ies[1];
1855 if (iedatalen + 2 > len)
1856 return -EILSEQ;
1857
1858 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1859 goto cont;
1860
1861 if (iedatalen < 4)
1862 goto cont;
1863
1864 iedata = ies + 2;
1865
1866 /* check WFA OUI, P2P subtype */
1867 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1868 iedata[2] != 0x9a || iedata[3] != 0x09)
1869 goto cont;
1870
1871 iedatalen -= 4;
1872 iedata += 4;
1873
1874 /* check attribute continuation into this IE */
1875 copy = min_t(unsigned int, attr_remaining, iedatalen);
1876 if (copy && desired_attr) {
1877 desired_len += copy;
1878 if (out) {
1879 memcpy(out, iedata, min(bufsize, copy));
1880 out += min(bufsize, copy);
1881 bufsize -= min(bufsize, copy);
1882 }
1883
1884
1885 if (copy == attr_remaining)
1886 return desired_len;
1887 }
1888
1889 attr_remaining -= copy;
1890 if (attr_remaining)
1891 goto cont;
1892
1893 iedatalen -= copy;
1894 iedata += copy;
1895
1896 while (iedatalen > 0) {
1897 u16 attr_len;
1898
1899 /* P2P attribute ID & size must fit */
1900 if (iedatalen < 3)
1901 return -EILSEQ;
1902 desired_attr = iedata[0] == attr;
1903 attr_len = get_unaligned_le16(iedata + 1);
1904 iedatalen -= 3;
1905 iedata += 3;
1906
1907 copy = min_t(unsigned int, attr_len, iedatalen);
1908
1909 if (desired_attr) {
1910 desired_len += copy;
1911 if (out) {
1912 memcpy(out, iedata, min(bufsize, copy));
1913 out += min(bufsize, copy);
1914 bufsize -= min(bufsize, copy);
1915 }
1916
1917 if (copy == attr_len)
1918 return desired_len;
1919 }
1920
1921 iedata += copy;
1922 iedatalen -= copy;
1923 attr_remaining = attr_len - copy;
1924 }
1925
1926 cont:
1927 len -= ies[1] + 2;
1928 ies += ies[1] + 2;
1929 }
1930
1931 if (attr_remaining && desired_attr)
1932 return -EILSEQ;
1933
1934 return -ENOENT;
1935 }
1936 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1937
ieee80211_id_in_list(const u8 * ids,int n_ids,u8 id,bool id_ext)1938 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
1939 {
1940 int i;
1941
1942 /* Make sure array values are legal */
1943 if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1944 return false;
1945
1946 i = 0;
1947 while (i < n_ids) {
1948 if (ids[i] == WLAN_EID_EXTENSION) {
1949 if (id_ext && (ids[i + 1] == id))
1950 return true;
1951
1952 i += 2;
1953 continue;
1954 }
1955
1956 if (ids[i] == id && !id_ext)
1957 return true;
1958
1959 i++;
1960 }
1961 return false;
1962 }
1963
skip_ie(const u8 * ies,size_t ielen,size_t pos)1964 static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1965 {
1966 /* we assume a validly formed IEs buffer */
1967 u8 len = ies[pos + 1];
1968
1969 pos += 2 + len;
1970
1971 /* the IE itself must have 255 bytes for fragments to follow */
1972 if (len < 255)
1973 return pos;
1974
1975 while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1976 len = ies[pos + 1];
1977 pos += 2 + len;
1978 }
1979
1980 return pos;
1981 }
1982
ieee80211_ie_split_ric(const u8 * ies,size_t ielen,const u8 * ids,int n_ids,const u8 * after_ric,int n_after_ric,size_t offset)1983 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1984 const u8 *ids, int n_ids,
1985 const u8 *after_ric, int n_after_ric,
1986 size_t offset)
1987 {
1988 size_t pos = offset;
1989
1990 while (pos < ielen) {
1991 u8 ext = 0;
1992
1993 if (ies[pos] == WLAN_EID_EXTENSION)
1994 ext = 2;
1995 if ((pos + ext) >= ielen)
1996 break;
1997
1998 if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1999 ies[pos] == WLAN_EID_EXTENSION))
2000 break;
2001
2002 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
2003 pos = skip_ie(ies, ielen, pos);
2004
2005 while (pos < ielen) {
2006 if (ies[pos] == WLAN_EID_EXTENSION)
2007 ext = 2;
2008 else
2009 ext = 0;
2010
2011 if ((pos + ext) >= ielen)
2012 break;
2013
2014 if (!ieee80211_id_in_list(after_ric,
2015 n_after_ric,
2016 ies[pos + ext],
2017 ext == 2))
2018 pos = skip_ie(ies, ielen, pos);
2019 else
2020 break;
2021 }
2022 } else {
2023 pos = skip_ie(ies, ielen, pos);
2024 }
2025 }
2026
2027 return pos;
2028 }
2029 EXPORT_SYMBOL(ieee80211_ie_split_ric);
2030
ieee80211_operating_class_to_band(u8 operating_class,enum nl80211_band * band)2031 bool ieee80211_operating_class_to_band(u8 operating_class,
2032 enum nl80211_band *band)
2033 {
2034 switch (operating_class) {
2035 case 112:
2036 case 115 ... 127:
2037 case 128 ... 130:
2038 *band = NL80211_BAND_5GHZ;
2039 return true;
2040 case 131 ... 135:
2041 *band = NL80211_BAND_6GHZ;
2042 return true;
2043 case 81:
2044 case 82:
2045 case 83:
2046 case 84:
2047 *band = NL80211_BAND_2GHZ;
2048 return true;
2049 case 180:
2050 *band = NL80211_BAND_60GHZ;
2051 return true;
2052 }
2053
2054 return false;
2055 }
2056 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
2057
ieee80211_chandef_to_operating_class(struct cfg80211_chan_def * chandef,u8 * op_class)2058 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
2059 u8 *op_class)
2060 {
2061 u8 vht_opclass;
2062 u32 freq = chandef->center_freq1;
2063
2064 if (freq >= 2412 && freq <= 2472) {
2065 if (chandef->width > NL80211_CHAN_WIDTH_40)
2066 return false;
2067
2068 /* 2.407 GHz, channels 1..13 */
2069 if (chandef->width == NL80211_CHAN_WIDTH_40) {
2070 if (freq > chandef->chan->center_freq)
2071 *op_class = 83; /* HT40+ */
2072 else
2073 *op_class = 84; /* HT40- */
2074 } else {
2075 *op_class = 81;
2076 }
2077
2078 return true;
2079 }
2080
2081 if (freq == 2484) {
2082 /* channel 14 is only for IEEE 802.11b */
2083 if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
2084 return false;
2085
2086 *op_class = 82; /* channel 14 */
2087 return true;
2088 }
2089
2090 switch (chandef->width) {
2091 case NL80211_CHAN_WIDTH_80:
2092 vht_opclass = 128;
2093 break;
2094 case NL80211_CHAN_WIDTH_160:
2095 vht_opclass = 129;
2096 break;
2097 case NL80211_CHAN_WIDTH_80P80:
2098 vht_opclass = 130;
2099 break;
2100 case NL80211_CHAN_WIDTH_10:
2101 case NL80211_CHAN_WIDTH_5:
2102 return false; /* unsupported for now */
2103 default:
2104 vht_opclass = 0;
2105 break;
2106 }
2107
2108 /* 5 GHz, channels 36..48 */
2109 if (freq >= 5180 && freq <= 5240) {
2110 if (vht_opclass) {
2111 *op_class = vht_opclass;
2112 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2113 if (freq > chandef->chan->center_freq)
2114 *op_class = 116;
2115 else
2116 *op_class = 117;
2117 } else {
2118 *op_class = 115;
2119 }
2120
2121 return true;
2122 }
2123
2124 /* 5 GHz, channels 52..64 */
2125 if (freq >= 5260 && freq <= 5320) {
2126 if (vht_opclass) {
2127 *op_class = vht_opclass;
2128 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2129 if (freq > chandef->chan->center_freq)
2130 *op_class = 119;
2131 else
2132 *op_class = 120;
2133 } else {
2134 *op_class = 118;
2135 }
2136
2137 return true;
2138 }
2139
2140 /* 5 GHz, channels 100..144 */
2141 if (freq >= 5500 && freq <= 5720) {
2142 if (vht_opclass) {
2143 *op_class = vht_opclass;
2144 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2145 if (freq > chandef->chan->center_freq)
2146 *op_class = 122;
2147 else
2148 *op_class = 123;
2149 } else {
2150 *op_class = 121;
2151 }
2152
2153 return true;
2154 }
2155
2156 /* 5 GHz, channels 149..169 */
2157 if (freq >= 5745 && freq <= 5845) {
2158 if (vht_opclass) {
2159 *op_class = vht_opclass;
2160 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2161 if (freq > chandef->chan->center_freq)
2162 *op_class = 126;
2163 else
2164 *op_class = 127;
2165 } else if (freq <= 5805) {
2166 *op_class = 124;
2167 } else {
2168 *op_class = 125;
2169 }
2170
2171 return true;
2172 }
2173
2174 /* 56.16 GHz, channel 1..4 */
2175 if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
2176 if (chandef->width >= NL80211_CHAN_WIDTH_40)
2177 return false;
2178
2179 *op_class = 180;
2180 return true;
2181 }
2182
2183 /* not supported yet */
2184 return false;
2185 }
2186 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
2187
cfg80211_wdev_bi(struct wireless_dev * wdev)2188 static int cfg80211_wdev_bi(struct wireless_dev *wdev)
2189 {
2190 switch (wdev->iftype) {
2191 case NL80211_IFTYPE_AP:
2192 case NL80211_IFTYPE_P2P_GO:
2193 WARN_ON(wdev->valid_links);
2194 return wdev->links[0].ap.beacon_interval;
2195 case NL80211_IFTYPE_MESH_POINT:
2196 return wdev->u.mesh.beacon_interval;
2197 case NL80211_IFTYPE_ADHOC:
2198 return wdev->u.ibss.beacon_interval;
2199 default:
2200 break;
2201 }
2202
2203 return 0;
2204 }
2205
cfg80211_calculate_bi_data(struct wiphy * wiphy,u32 new_beacon_int,u32 * beacon_int_gcd,bool * beacon_int_different)2206 static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
2207 u32 *beacon_int_gcd,
2208 bool *beacon_int_different)
2209 {
2210 struct wireless_dev *wdev;
2211
2212 *beacon_int_gcd = 0;
2213 *beacon_int_different = false;
2214
2215 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
2216 int wdev_bi;
2217
2218 /* this feature isn't supported with MLO */
2219 if (wdev->valid_links)
2220 continue;
2221
2222 wdev_bi = cfg80211_wdev_bi(wdev);
2223
2224 if (!wdev_bi)
2225 continue;
2226
2227 if (!*beacon_int_gcd) {
2228 *beacon_int_gcd = wdev_bi;
2229 continue;
2230 }
2231
2232 if (wdev_bi == *beacon_int_gcd)
2233 continue;
2234
2235 *beacon_int_different = true;
2236 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev_bi);
2237 }
2238
2239 if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
2240 if (*beacon_int_gcd)
2241 *beacon_int_different = true;
2242 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
2243 }
2244 }
2245
cfg80211_validate_beacon_int(struct cfg80211_registered_device * rdev,enum nl80211_iftype iftype,u32 beacon_int)2246 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
2247 enum nl80211_iftype iftype, u32 beacon_int)
2248 {
2249 /*
2250 * This is just a basic pre-condition check; if interface combinations
2251 * are possible the driver must already be checking those with a call
2252 * to cfg80211_check_combinations(), in which case we'll validate more
2253 * through the cfg80211_calculate_bi_data() call and code in
2254 * cfg80211_iter_combinations().
2255 */
2256
2257 if (beacon_int < 10 || beacon_int > 10000)
2258 return -EINVAL;
2259
2260 return 0;
2261 }
2262
cfg80211_iter_combinations(struct wiphy * wiphy,struct iface_combination_params * params,void (* iter)(const struct ieee80211_iface_combination * c,void * data),void * data)2263 int cfg80211_iter_combinations(struct wiphy *wiphy,
2264 struct iface_combination_params *params,
2265 void (*iter)(const struct ieee80211_iface_combination *c,
2266 void *data),
2267 void *data)
2268 {
2269 const struct ieee80211_regdomain *regdom;
2270 enum nl80211_dfs_regions region = 0;
2271 int i, j, iftype;
2272 int num_interfaces = 0;
2273 u32 used_iftypes = 0;
2274 u32 beacon_int_gcd;
2275 bool beacon_int_different;
2276
2277 /*
2278 * This is a bit strange, since the iteration used to rely only on
2279 * the data given by the driver, but here it now relies on context,
2280 * in form of the currently operating interfaces.
2281 * This is OK for all current users, and saves us from having to
2282 * push the GCD calculations into all the drivers.
2283 * In the future, this should probably rely more on data that's in
2284 * cfg80211 already - the only thing not would appear to be any new
2285 * interfaces (while being brought up) and channel/radar data.
2286 */
2287 cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
2288 &beacon_int_gcd, &beacon_int_different);
2289
2290 if (params->radar_detect) {
2291 rcu_read_lock();
2292 regdom = rcu_dereference(cfg80211_regdomain);
2293 if (regdom)
2294 region = regdom->dfs_region;
2295 rcu_read_unlock();
2296 }
2297
2298 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
2299 num_interfaces += params->iftype_num[iftype];
2300 if (params->iftype_num[iftype] > 0 &&
2301 !cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
2302 used_iftypes |= BIT(iftype);
2303 }
2304
2305 for (i = 0; i < wiphy->n_iface_combinations; i++) {
2306 const struct ieee80211_iface_combination *c;
2307 struct ieee80211_iface_limit *limits;
2308 u32 all_iftypes = 0;
2309
2310 c = &wiphy->iface_combinations[i];
2311
2312 if (num_interfaces > c->max_interfaces)
2313 continue;
2314 if (params->num_different_channels > c->num_different_channels)
2315 continue;
2316
2317 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
2318 GFP_KERNEL);
2319 if (!limits)
2320 return -ENOMEM;
2321
2322 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
2323 if (cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
2324 continue;
2325 for (j = 0; j < c->n_limits; j++) {
2326 all_iftypes |= limits[j].types;
2327 if (!(limits[j].types & BIT(iftype)))
2328 continue;
2329 if (limits[j].max < params->iftype_num[iftype])
2330 goto cont;
2331 limits[j].max -= params->iftype_num[iftype];
2332 }
2333 }
2334
2335 if (params->radar_detect !=
2336 (c->radar_detect_widths & params->radar_detect))
2337 goto cont;
2338
2339 if (params->radar_detect && c->radar_detect_regions &&
2340 !(c->radar_detect_regions & BIT(region)))
2341 goto cont;
2342
2343 /* Finally check that all iftypes that we're currently
2344 * using are actually part of this combination. If they
2345 * aren't then we can't use this combination and have
2346 * to continue to the next.
2347 */
2348 if ((all_iftypes & used_iftypes) != used_iftypes)
2349 goto cont;
2350
2351 if (beacon_int_gcd) {
2352 if (c->beacon_int_min_gcd &&
2353 beacon_int_gcd < c->beacon_int_min_gcd)
2354 goto cont;
2355 if (!c->beacon_int_min_gcd && beacon_int_different)
2356 goto cont;
2357 }
2358
2359 /* This combination covered all interface types and
2360 * supported the requested numbers, so we're good.
2361 */
2362
2363 (*iter)(c, data);
2364 cont:
2365 kfree(limits);
2366 }
2367
2368 return 0;
2369 }
2370 EXPORT_SYMBOL(cfg80211_iter_combinations);
2371
2372 static void
cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination * c,void * data)2373 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
2374 void *data)
2375 {
2376 int *num = data;
2377 (*num)++;
2378 }
2379
cfg80211_check_combinations(struct wiphy * wiphy,struct iface_combination_params * params)2380 int cfg80211_check_combinations(struct wiphy *wiphy,
2381 struct iface_combination_params *params)
2382 {
2383 int err, num = 0;
2384
2385 err = cfg80211_iter_combinations(wiphy, params,
2386 cfg80211_iter_sum_ifcombs, &num);
2387 if (err)
2388 return err;
2389 if (num == 0)
2390 return -EBUSY;
2391
2392 return 0;
2393 }
2394 EXPORT_SYMBOL(cfg80211_check_combinations);
2395
ieee80211_get_ratemask(struct ieee80211_supported_band * sband,const u8 * rates,unsigned int n_rates,u32 * mask)2396 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
2397 const u8 *rates, unsigned int n_rates,
2398 u32 *mask)
2399 {
2400 int i, j;
2401
2402 if (!sband)
2403 return -EINVAL;
2404
2405 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
2406 return -EINVAL;
2407
2408 *mask = 0;
2409
2410 for (i = 0; i < n_rates; i++) {
2411 int rate = (rates[i] & 0x7f) * 5;
2412 bool found = false;
2413
2414 for (j = 0; j < sband->n_bitrates; j++) {
2415 if (sband->bitrates[j].bitrate == rate) {
2416 found = true;
2417 *mask |= BIT(j);
2418 break;
2419 }
2420 }
2421 if (!found)
2422 return -EINVAL;
2423 }
2424
2425 /*
2426 * mask must have at least one bit set here since we
2427 * didn't accept a 0-length rates array nor allowed
2428 * entries in the array that didn't exist
2429 */
2430
2431 return 0;
2432 }
2433
ieee80211_get_num_supported_channels(struct wiphy * wiphy)2434 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
2435 {
2436 enum nl80211_band band;
2437 unsigned int n_channels = 0;
2438
2439 for (band = 0; band < NUM_NL80211_BANDS; band++)
2440 if (wiphy->bands[band])
2441 n_channels += wiphy->bands[band]->n_channels;
2442
2443 return n_channels;
2444 }
2445 EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
2446
cfg80211_get_station(struct net_device * dev,const u8 * mac_addr,struct station_info * sinfo)2447 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
2448 struct station_info *sinfo)
2449 {
2450 struct cfg80211_registered_device *rdev;
2451 struct wireless_dev *wdev;
2452 int ret;
2453
2454 wdev = dev->ieee80211_ptr;
2455 if (!wdev)
2456 return -EOPNOTSUPP;
2457
2458 rdev = wiphy_to_rdev(wdev->wiphy);
2459 if (!rdev->ops->get_station)
2460 return -EOPNOTSUPP;
2461
2462 memset(sinfo, 0, sizeof(*sinfo));
2463
2464 wiphy_lock(&rdev->wiphy);
2465 ret = rdev_get_station(rdev, dev, mac_addr, sinfo);
2466 wiphy_unlock(&rdev->wiphy);
2467
2468 return ret;
2469 }
2470 EXPORT_SYMBOL(cfg80211_get_station);
2471
cfg80211_free_nan_func(struct cfg80211_nan_func * f)2472 void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
2473 {
2474 int i;
2475
2476 if (!f)
2477 return;
2478
2479 kfree(f->serv_spec_info);
2480 kfree(f->srf_bf);
2481 kfree(f->srf_macs);
2482 for (i = 0; i < f->num_rx_filters; i++)
2483 kfree(f->rx_filters[i].filter);
2484
2485 for (i = 0; i < f->num_tx_filters; i++)
2486 kfree(f->tx_filters[i].filter);
2487
2488 kfree(f->rx_filters);
2489 kfree(f->tx_filters);
2490 kfree(f);
2491 }
2492 EXPORT_SYMBOL(cfg80211_free_nan_func);
2493
cfg80211_does_bw_fit_range(const struct ieee80211_freq_range * freq_range,u32 center_freq_khz,u32 bw_khz)2494 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
2495 u32 center_freq_khz, u32 bw_khz)
2496 {
2497 u32 start_freq_khz, end_freq_khz;
2498
2499 start_freq_khz = center_freq_khz - (bw_khz / 2);
2500 end_freq_khz = center_freq_khz + (bw_khz / 2);
2501
2502 if (start_freq_khz >= freq_range->start_freq_khz &&
2503 end_freq_khz <= freq_range->end_freq_khz)
2504 return true;
2505
2506 return false;
2507 }
2508
cfg80211_sinfo_alloc_tid_stats(struct station_info * sinfo,gfp_t gfp)2509 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
2510 {
2511 sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
2512 sizeof(*(sinfo->pertid)),
2513 gfp);
2514 if (!sinfo->pertid)
2515 return -ENOMEM;
2516
2517 return 0;
2518 }
2519 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
2520
2521 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
2522 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
2523 const unsigned char rfc1042_header[] __aligned(2) =
2524 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2525 EXPORT_SYMBOL(rfc1042_header);
2526
2527 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
2528 const unsigned char bridge_tunnel_header[] __aligned(2) =
2529 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
2530 EXPORT_SYMBOL(bridge_tunnel_header);
2531
2532 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
2533 struct iapp_layer2_update {
2534 u8 da[ETH_ALEN]; /* broadcast */
2535 u8 sa[ETH_ALEN]; /* STA addr */
2536 __be16 len; /* 6 */
2537 u8 dsap; /* 0 */
2538 u8 ssap; /* 0 */
2539 u8 control;
2540 u8 xid_info[3];
2541 } __packed;
2542
cfg80211_send_layer2_update(struct net_device * dev,const u8 * addr)2543 void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
2544 {
2545 struct iapp_layer2_update *msg;
2546 struct sk_buff *skb;
2547
2548 /* Send Level 2 Update Frame to update forwarding tables in layer 2
2549 * bridge devices */
2550
2551 skb = dev_alloc_skb(sizeof(*msg));
2552 if (!skb)
2553 return;
2554 msg = skb_put(skb, sizeof(*msg));
2555
2556 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
2557 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
2558
2559 eth_broadcast_addr(msg->da);
2560 ether_addr_copy(msg->sa, addr);
2561 msg->len = htons(6);
2562 msg->dsap = 0;
2563 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
2564 msg->control = 0xaf; /* XID response lsb.1111F101.
2565 * F=0 (no poll command; unsolicited frame) */
2566 msg->xid_info[0] = 0x81; /* XID format identifier */
2567 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
2568 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
2569
2570 skb->dev = dev;
2571 skb->protocol = eth_type_trans(skb, dev);
2572 memset(skb->cb, 0, sizeof(skb->cb));
2573 netif_rx(skb);
2574 }
2575 EXPORT_SYMBOL(cfg80211_send_layer2_update);
2576
ieee80211_get_vht_max_nss(struct ieee80211_vht_cap * cap,enum ieee80211_vht_chanwidth bw,int mcs,bool ext_nss_bw_capable,unsigned int max_vht_nss)2577 int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap,
2578 enum ieee80211_vht_chanwidth bw,
2579 int mcs, bool ext_nss_bw_capable,
2580 unsigned int max_vht_nss)
2581 {
2582 u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map);
2583 int ext_nss_bw;
2584 int supp_width;
2585 int i, mcs_encoding;
2586
2587 if (map == 0xffff)
2588 return 0;
2589
2590 if (WARN_ON(mcs > 9 || max_vht_nss > 8))
2591 return 0;
2592 if (mcs <= 7)
2593 mcs_encoding = 0;
2594 else if (mcs == 8)
2595 mcs_encoding = 1;
2596 else
2597 mcs_encoding = 2;
2598
2599 if (!max_vht_nss) {
2600 /* find max_vht_nss for the given MCS */
2601 for (i = 7; i >= 0; i--) {
2602 int supp = (map >> (2 * i)) & 3;
2603
2604 if (supp == 3)
2605 continue;
2606
2607 if (supp >= mcs_encoding) {
2608 max_vht_nss = i + 1;
2609 break;
2610 }
2611 }
2612 }
2613
2614 if (!(cap->supp_mcs.tx_mcs_map &
2615 cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
2616 return max_vht_nss;
2617
2618 ext_nss_bw = le32_get_bits(cap->vht_cap_info,
2619 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
2620 supp_width = le32_get_bits(cap->vht_cap_info,
2621 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
2622
2623 /* if not capable, treat ext_nss_bw as 0 */
2624 if (!ext_nss_bw_capable)
2625 ext_nss_bw = 0;
2626
2627 /* This is invalid */
2628 if (supp_width == 3)
2629 return 0;
2630
2631 /* This is an invalid combination so pretend nothing is supported */
2632 if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2))
2633 return 0;
2634
2635 /*
2636 * Cover all the special cases according to IEEE 802.11-2016
2637 * Table 9-250. All other cases are either factor of 1 or not
2638 * valid/supported.
2639 */
2640 switch (bw) {
2641 case IEEE80211_VHT_CHANWIDTH_USE_HT:
2642 case IEEE80211_VHT_CHANWIDTH_80MHZ:
2643 if ((supp_width == 1 || supp_width == 2) &&
2644 ext_nss_bw == 3)
2645 return 2 * max_vht_nss;
2646 break;
2647 case IEEE80211_VHT_CHANWIDTH_160MHZ:
2648 if (supp_width == 0 &&
2649 (ext_nss_bw == 1 || ext_nss_bw == 2))
2650 return max_vht_nss / 2;
2651 if (supp_width == 0 &&
2652 ext_nss_bw == 3)
2653 return (3 * max_vht_nss) / 4;
2654 if (supp_width == 1 &&
2655 ext_nss_bw == 3)
2656 return 2 * max_vht_nss;
2657 break;
2658 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
2659 if (supp_width == 0 && ext_nss_bw == 1)
2660 return 0; /* not possible */
2661 if (supp_width == 0 &&
2662 ext_nss_bw == 2)
2663 return max_vht_nss / 2;
2664 if (supp_width == 0 &&
2665 ext_nss_bw == 3)
2666 return (3 * max_vht_nss) / 4;
2667 if (supp_width == 1 &&
2668 ext_nss_bw == 0)
2669 return 0; /* not possible */
2670 if (supp_width == 1 &&
2671 ext_nss_bw == 1)
2672 return max_vht_nss / 2;
2673 if (supp_width == 1 &&
2674 ext_nss_bw == 2)
2675 return (3 * max_vht_nss) / 4;
2676 break;
2677 }
2678
2679 /* not covered or invalid combination received */
2680 return max_vht_nss;
2681 }
2682 EXPORT_SYMBOL(ieee80211_get_vht_max_nss);
2683
cfg80211_iftype_allowed(struct wiphy * wiphy,enum nl80211_iftype iftype,bool is_4addr,u8 check_swif)2684 bool cfg80211_iftype_allowed(struct wiphy *wiphy, enum nl80211_iftype iftype,
2685 bool is_4addr, u8 check_swif)
2686
2687 {
2688 bool is_vlan = iftype == NL80211_IFTYPE_AP_VLAN;
2689
2690 switch (check_swif) {
2691 case 0:
2692 if (is_vlan && is_4addr)
2693 return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2694 return wiphy->interface_modes & BIT(iftype);
2695 case 1:
2696 if (!(wiphy->software_iftypes & BIT(iftype)) && is_vlan)
2697 return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2698 return wiphy->software_iftypes & BIT(iftype);
2699 default:
2700 break;
2701 }
2702
2703 return false;
2704 }
2705 EXPORT_SYMBOL(cfg80211_iftype_allowed);
2706
cfg80211_remove_link(struct wireless_dev * wdev,unsigned int link_id)2707 void cfg80211_remove_link(struct wireless_dev *wdev, unsigned int link_id)
2708 {
2709 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
2710
2711 ASSERT_WDEV_LOCK(wdev);
2712
2713 switch (wdev->iftype) {
2714 case NL80211_IFTYPE_AP:
2715 case NL80211_IFTYPE_P2P_GO:
2716 __cfg80211_stop_ap(rdev, wdev->netdev, link_id, true);
2717 break;
2718 default:
2719 /* per-link not relevant */
2720 break;
2721 }
2722
2723 wdev->valid_links &= ~BIT(link_id);
2724
2725 rdev_del_intf_link(rdev, wdev, link_id);
2726
2727 eth_zero_addr(wdev->links[link_id].addr);
2728 }
2729
cfg80211_remove_links(struct wireless_dev * wdev)2730 void cfg80211_remove_links(struct wireless_dev *wdev)
2731 {
2732 unsigned int link_id;
2733
2734 /*
2735 * links are controlled by upper layers (userspace/cfg)
2736 * only for AP mode, so only remove them here for AP
2737 */
2738 if (wdev->iftype != NL80211_IFTYPE_AP)
2739 return;
2740
2741 wdev_lock(wdev);
2742 if (wdev->valid_links) {
2743 for_each_valid_link(wdev, link_id)
2744 cfg80211_remove_link(wdev, link_id);
2745 }
2746 wdev_unlock(wdev);
2747 }
2748
cfg80211_remove_virtual_intf(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev)2749 int cfg80211_remove_virtual_intf(struct cfg80211_registered_device *rdev,
2750 struct wireless_dev *wdev)
2751 {
2752 cfg80211_remove_links(wdev);
2753
2754 return rdev_del_virtual_intf(rdev, wdev);
2755 }
2756
2757 const struct wiphy_iftype_ext_capab *
cfg80211_get_iftype_ext_capa(struct wiphy * wiphy,enum nl80211_iftype type)2758 cfg80211_get_iftype_ext_capa(struct wiphy *wiphy, enum nl80211_iftype type)
2759 {
2760 int i;
2761
2762 for (i = 0; i < wiphy->num_iftype_ext_capab; i++) {
2763 if (wiphy->iftype_ext_capab[i].iftype == type)
2764 return &wiphy->iftype_ext_capab[i];
2765 }
2766
2767 return NULL;
2768 }
2769 EXPORT_SYMBOL(cfg80211_get_iftype_ext_capa);
2770