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