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