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