Merge tag 'asoc-v5.2-5' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[linux-2.6-block.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 struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id)
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_elem(id, ies->data, ies->len);
801 }
802 EXPORT_SYMBOL(ieee80211_bss_get_elem);
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 {
1224                 WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1225                      rate->bw, rate->he_ru_alloc);
1226                 return 0;
1227         }
1228
1229         /* now scale to the appropriate MCS */
1230         tmp = result;
1231         tmp *= SCALE;
1232         do_div(tmp, mcs_divisors[rate->mcs]);
1233         result = tmp;
1234
1235         /* and take NSS, DCM into account */
1236         result = (result * rate->nss) / 8;
1237         if (rate->he_dcm)
1238                 result /= 2;
1239
1240         return result;
1241 }
1242
1243 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1244 {
1245         if (rate->flags & RATE_INFO_FLAGS_MCS)
1246                 return cfg80211_calculate_bitrate_ht(rate);
1247         if (rate->flags & RATE_INFO_FLAGS_60G)
1248                 return cfg80211_calculate_bitrate_60g(rate);
1249         if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1250                 return cfg80211_calculate_bitrate_vht(rate);
1251         if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1252                 return cfg80211_calculate_bitrate_he(rate);
1253
1254         return rate->legacy;
1255 }
1256 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1257
1258 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1259                           enum ieee80211_p2p_attr_id attr,
1260                           u8 *buf, unsigned int bufsize)
1261 {
1262         u8 *out = buf;
1263         u16 attr_remaining = 0;
1264         bool desired_attr = false;
1265         u16 desired_len = 0;
1266
1267         while (len > 0) {
1268                 unsigned int iedatalen;
1269                 unsigned int copy;
1270                 const u8 *iedata;
1271
1272                 if (len < 2)
1273                         return -EILSEQ;
1274                 iedatalen = ies[1];
1275                 if (iedatalen + 2 > len)
1276                         return -EILSEQ;
1277
1278                 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1279                         goto cont;
1280
1281                 if (iedatalen < 4)
1282                         goto cont;
1283
1284                 iedata = ies + 2;
1285
1286                 /* check WFA OUI, P2P subtype */
1287                 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1288                     iedata[2] != 0x9a || iedata[3] != 0x09)
1289                         goto cont;
1290
1291                 iedatalen -= 4;
1292                 iedata += 4;
1293
1294                 /* check attribute continuation into this IE */
1295                 copy = min_t(unsigned int, attr_remaining, iedatalen);
1296                 if (copy && desired_attr) {
1297                         desired_len += copy;
1298                         if (out) {
1299                                 memcpy(out, iedata, min(bufsize, copy));
1300                                 out += min(bufsize, copy);
1301                                 bufsize -= min(bufsize, copy);
1302                         }
1303
1304
1305                         if (copy == attr_remaining)
1306                                 return desired_len;
1307                 }
1308
1309                 attr_remaining -= copy;
1310                 if (attr_remaining)
1311                         goto cont;
1312
1313                 iedatalen -= copy;
1314                 iedata += copy;
1315
1316                 while (iedatalen > 0) {
1317                         u16 attr_len;
1318
1319                         /* P2P attribute ID & size must fit */
1320                         if (iedatalen < 3)
1321                                 return -EILSEQ;
1322                         desired_attr = iedata[0] == attr;
1323                         attr_len = get_unaligned_le16(iedata + 1);
1324                         iedatalen -= 3;
1325                         iedata += 3;
1326
1327                         copy = min_t(unsigned int, attr_len, iedatalen);
1328
1329                         if (desired_attr) {
1330                                 desired_len += copy;
1331                                 if (out) {
1332                                         memcpy(out, iedata, min(bufsize, copy));
1333                                         out += min(bufsize, copy);
1334                                         bufsize -= min(bufsize, copy);
1335                                 }
1336
1337                                 if (copy == attr_len)
1338                                         return desired_len;
1339                         }
1340
1341                         iedata += copy;
1342                         iedatalen -= copy;
1343                         attr_remaining = attr_len - copy;
1344                 }
1345
1346  cont:
1347                 len -= ies[1] + 2;
1348                 ies += ies[1] + 2;
1349         }
1350
1351         if (attr_remaining && desired_attr)
1352                 return -EILSEQ;
1353
1354         return -ENOENT;
1355 }
1356 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1357
1358 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
1359 {
1360         int i;
1361
1362         /* Make sure array values are legal */
1363         if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1364                 return false;
1365
1366         i = 0;
1367         while (i < n_ids) {
1368                 if (ids[i] == WLAN_EID_EXTENSION) {
1369                         if (id_ext && (ids[i + 1] == id))
1370                                 return true;
1371
1372                         i += 2;
1373                         continue;
1374                 }
1375
1376                 if (ids[i] == id && !id_ext)
1377                         return true;
1378
1379                 i++;
1380         }
1381         return false;
1382 }
1383
1384 static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1385 {
1386         /* we assume a validly formed IEs buffer */
1387         u8 len = ies[pos + 1];
1388
1389         pos += 2 + len;
1390
1391         /* the IE itself must have 255 bytes for fragments to follow */
1392         if (len < 255)
1393                 return pos;
1394
1395         while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1396                 len = ies[pos + 1];
1397                 pos += 2 + len;
1398         }
1399
1400         return pos;
1401 }
1402
1403 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1404                               const u8 *ids, int n_ids,
1405                               const u8 *after_ric, int n_after_ric,
1406                               size_t offset)
1407 {
1408         size_t pos = offset;
1409
1410         while (pos < ielen) {
1411                 u8 ext = 0;
1412
1413                 if (ies[pos] == WLAN_EID_EXTENSION)
1414                         ext = 2;
1415                 if ((pos + ext) >= ielen)
1416                         break;
1417
1418                 if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1419                                           ies[pos] == WLAN_EID_EXTENSION))
1420                         break;
1421
1422                 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1423                         pos = skip_ie(ies, ielen, pos);
1424
1425                         while (pos < ielen) {
1426                                 if (ies[pos] == WLAN_EID_EXTENSION)
1427                                         ext = 2;
1428                                 else
1429                                         ext = 0;
1430
1431                                 if ((pos + ext) >= ielen)
1432                                         break;
1433
1434                                 if (!ieee80211_id_in_list(after_ric,
1435                                                           n_after_ric,
1436                                                           ies[pos + ext],
1437                                                           ext == 2))
1438                                         pos = skip_ie(ies, ielen, pos);
1439                                 else
1440                                         break;
1441                         }
1442                 } else {
1443                         pos = skip_ie(ies, ielen, pos);
1444                 }
1445         }
1446
1447         return pos;
1448 }
1449 EXPORT_SYMBOL(ieee80211_ie_split_ric);
1450
1451 bool ieee80211_operating_class_to_band(u8 operating_class,
1452                                        enum nl80211_band *band)
1453 {
1454         switch (operating_class) {
1455         case 112:
1456         case 115 ... 127:
1457         case 128 ... 130:
1458                 *band = NL80211_BAND_5GHZ;
1459                 return true;
1460         case 81:
1461         case 82:
1462         case 83:
1463         case 84:
1464                 *band = NL80211_BAND_2GHZ;
1465                 return true;
1466         case 180:
1467                 *band = NL80211_BAND_60GHZ;
1468                 return true;
1469         }
1470
1471         return false;
1472 }
1473 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1474
1475 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1476                                           u8 *op_class)
1477 {
1478         u8 vht_opclass;
1479         u32 freq = chandef->center_freq1;
1480
1481         if (freq >= 2412 && freq <= 2472) {
1482                 if (chandef->width > NL80211_CHAN_WIDTH_40)
1483                         return false;
1484
1485                 /* 2.407 GHz, channels 1..13 */
1486                 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1487                         if (freq > chandef->chan->center_freq)
1488                                 *op_class = 83; /* HT40+ */
1489                         else
1490                                 *op_class = 84; /* HT40- */
1491                 } else {
1492                         *op_class = 81;
1493                 }
1494
1495                 return true;
1496         }
1497
1498         if (freq == 2484) {
1499                 if (chandef->width > NL80211_CHAN_WIDTH_40)
1500                         return false;
1501
1502                 *op_class = 82; /* channel 14 */
1503                 return true;
1504         }
1505
1506         switch (chandef->width) {
1507         case NL80211_CHAN_WIDTH_80:
1508                 vht_opclass = 128;
1509                 break;
1510         case NL80211_CHAN_WIDTH_160:
1511                 vht_opclass = 129;
1512                 break;
1513         case NL80211_CHAN_WIDTH_80P80:
1514                 vht_opclass = 130;
1515                 break;
1516         case NL80211_CHAN_WIDTH_10:
1517         case NL80211_CHAN_WIDTH_5:
1518                 return false; /* unsupported for now */
1519         default:
1520                 vht_opclass = 0;
1521                 break;
1522         }
1523
1524         /* 5 GHz, channels 36..48 */
1525         if (freq >= 5180 && freq <= 5240) {
1526                 if (vht_opclass) {
1527                         *op_class = vht_opclass;
1528                 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1529                         if (freq > chandef->chan->center_freq)
1530                                 *op_class = 116;
1531                         else
1532                                 *op_class = 117;
1533                 } else {
1534                         *op_class = 115;
1535                 }
1536
1537                 return true;
1538         }
1539
1540         /* 5 GHz, channels 52..64 */
1541         if (freq >= 5260 && freq <= 5320) {
1542                 if (vht_opclass) {
1543                         *op_class = vht_opclass;
1544                 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1545                         if (freq > chandef->chan->center_freq)
1546                                 *op_class = 119;
1547                         else
1548                                 *op_class = 120;
1549                 } else {
1550                         *op_class = 118;
1551                 }
1552
1553                 return true;
1554         }
1555
1556         /* 5 GHz, channels 100..144 */
1557         if (freq >= 5500 && freq <= 5720) {
1558                 if (vht_opclass) {
1559                         *op_class = vht_opclass;
1560                 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1561                         if (freq > chandef->chan->center_freq)
1562                                 *op_class = 122;
1563                         else
1564                                 *op_class = 123;
1565                 } else {
1566                         *op_class = 121;
1567                 }
1568
1569                 return true;
1570         }
1571
1572         /* 5 GHz, channels 149..169 */
1573         if (freq >= 5745 && freq <= 5845) {
1574                 if (vht_opclass) {
1575                         *op_class = vht_opclass;
1576                 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1577                         if (freq > chandef->chan->center_freq)
1578                                 *op_class = 126;
1579                         else
1580                                 *op_class = 127;
1581                 } else if (freq <= 5805) {
1582                         *op_class = 124;
1583                 } else {
1584                         *op_class = 125;
1585                 }
1586
1587                 return true;
1588         }
1589
1590         /* 56.16 GHz, channel 1..4 */
1591         if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
1592                 if (chandef->width >= NL80211_CHAN_WIDTH_40)
1593                         return false;
1594
1595                 *op_class = 180;
1596                 return true;
1597         }
1598
1599         /* not supported yet */
1600         return false;
1601 }
1602 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1603
1604 static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
1605                                        u32 *beacon_int_gcd,
1606                                        bool *beacon_int_different)
1607 {
1608         struct wireless_dev *wdev;
1609
1610         *beacon_int_gcd = 0;
1611         *beacon_int_different = false;
1612
1613         list_for_each_entry(wdev, &wiphy->wdev_list, list) {
1614                 if (!wdev->beacon_interval)
1615                         continue;
1616
1617                 if (!*beacon_int_gcd) {
1618                         *beacon_int_gcd = wdev->beacon_interval;
1619                         continue;
1620                 }
1621
1622                 if (wdev->beacon_interval == *beacon_int_gcd)
1623                         continue;
1624
1625                 *beacon_int_different = true;
1626                 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval);
1627         }
1628
1629         if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
1630                 if (*beacon_int_gcd)
1631                         *beacon_int_different = true;
1632                 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
1633         }
1634 }
1635
1636 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1637                                  enum nl80211_iftype iftype, u32 beacon_int)
1638 {
1639         /*
1640          * This is just a basic pre-condition check; if interface combinations
1641          * are possible the driver must already be checking those with a call
1642          * to cfg80211_check_combinations(), in which case we'll validate more
1643          * through the cfg80211_calculate_bi_data() call and code in
1644          * cfg80211_iter_combinations().
1645          */
1646
1647         if (beacon_int < 10 || beacon_int > 10000)
1648                 return -EINVAL;
1649
1650         return 0;
1651 }
1652
1653 int cfg80211_iter_combinations(struct wiphy *wiphy,
1654                                struct iface_combination_params *params,
1655                                void (*iter)(const struct ieee80211_iface_combination *c,
1656                                             void *data),
1657                                void *data)
1658 {
1659         const struct ieee80211_regdomain *regdom;
1660         enum nl80211_dfs_regions region = 0;
1661         int i, j, iftype;
1662         int num_interfaces = 0;
1663         u32 used_iftypes = 0;
1664         u32 beacon_int_gcd;
1665         bool beacon_int_different;
1666
1667         /*
1668          * This is a bit strange, since the iteration used to rely only on
1669          * the data given by the driver, but here it now relies on context,
1670          * in form of the currently operating interfaces.
1671          * This is OK for all current users, and saves us from having to
1672          * push the GCD calculations into all the drivers.
1673          * In the future, this should probably rely more on data that's in
1674          * cfg80211 already - the only thing not would appear to be any new
1675          * interfaces (while being brought up) and channel/radar data.
1676          */
1677         cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
1678                                    &beacon_int_gcd, &beacon_int_different);
1679
1680         if (params->radar_detect) {
1681                 rcu_read_lock();
1682                 regdom = rcu_dereference(cfg80211_regdomain);
1683                 if (regdom)
1684                         region = regdom->dfs_region;
1685                 rcu_read_unlock();
1686         }
1687
1688         for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1689                 num_interfaces += params->iftype_num[iftype];
1690                 if (params->iftype_num[iftype] > 0 &&
1691                     !(wiphy->software_iftypes & BIT(iftype)))
1692                         used_iftypes |= BIT(iftype);
1693         }
1694
1695         for (i = 0; i < wiphy->n_iface_combinations; i++) {
1696                 const struct ieee80211_iface_combination *c;
1697                 struct ieee80211_iface_limit *limits;
1698                 u32 all_iftypes = 0;
1699
1700                 c = &wiphy->iface_combinations[i];
1701
1702                 if (num_interfaces > c->max_interfaces)
1703                         continue;
1704                 if (params->num_different_channels > c->num_different_channels)
1705                         continue;
1706
1707                 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1708                                  GFP_KERNEL);
1709                 if (!limits)
1710                         return -ENOMEM;
1711
1712                 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1713                         if (wiphy->software_iftypes & BIT(iftype))
1714                                 continue;
1715                         for (j = 0; j < c->n_limits; j++) {
1716                                 all_iftypes |= limits[j].types;
1717                                 if (!(limits[j].types & BIT(iftype)))
1718                                         continue;
1719                                 if (limits[j].max < params->iftype_num[iftype])
1720                                         goto cont;
1721                                 limits[j].max -= params->iftype_num[iftype];
1722                         }
1723                 }
1724
1725                 if (params->radar_detect !=
1726                         (c->radar_detect_widths & params->radar_detect))
1727                         goto cont;
1728
1729                 if (params->radar_detect && c->radar_detect_regions &&
1730                     !(c->radar_detect_regions & BIT(region)))
1731                         goto cont;
1732
1733                 /* Finally check that all iftypes that we're currently
1734                  * using are actually part of this combination. If they
1735                  * aren't then we can't use this combination and have
1736                  * to continue to the next.
1737                  */
1738                 if ((all_iftypes & used_iftypes) != used_iftypes)
1739                         goto cont;
1740
1741                 if (beacon_int_gcd) {
1742                         if (c->beacon_int_min_gcd &&
1743                             beacon_int_gcd < c->beacon_int_min_gcd)
1744                                 goto cont;
1745                         if (!c->beacon_int_min_gcd && beacon_int_different)
1746                                 goto cont;
1747                 }
1748
1749                 /* This combination covered all interface types and
1750                  * supported the requested numbers, so we're good.
1751                  */
1752
1753                 (*iter)(c, data);
1754  cont:
1755                 kfree(limits);
1756         }
1757
1758         return 0;
1759 }
1760 EXPORT_SYMBOL(cfg80211_iter_combinations);
1761
1762 static void
1763 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
1764                           void *data)
1765 {
1766         int *num = data;
1767         (*num)++;
1768 }
1769
1770 int cfg80211_check_combinations(struct wiphy *wiphy,
1771                                 struct iface_combination_params *params)
1772 {
1773         int err, num = 0;
1774
1775         err = cfg80211_iter_combinations(wiphy, params,
1776                                          cfg80211_iter_sum_ifcombs, &num);
1777         if (err)
1778                 return err;
1779         if (num == 0)
1780                 return -EBUSY;
1781
1782         return 0;
1783 }
1784 EXPORT_SYMBOL(cfg80211_check_combinations);
1785
1786 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1787                            const u8 *rates, unsigned int n_rates,
1788                            u32 *mask)
1789 {
1790         int i, j;
1791
1792         if (!sband)
1793                 return -EINVAL;
1794
1795         if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1796                 return -EINVAL;
1797
1798         *mask = 0;
1799
1800         for (i = 0; i < n_rates; i++) {
1801                 int rate = (rates[i] & 0x7f) * 5;
1802                 bool found = false;
1803
1804                 for (j = 0; j < sband->n_bitrates; j++) {
1805                         if (sband->bitrates[j].bitrate == rate) {
1806                                 found = true;
1807                                 *mask |= BIT(j);
1808                                 break;
1809                         }
1810                 }
1811                 if (!found)
1812                         return -EINVAL;
1813         }
1814
1815         /*
1816          * mask must have at least one bit set here since we
1817          * didn't accept a 0-length rates array nor allowed
1818          * entries in the array that didn't exist
1819          */
1820
1821         return 0;
1822 }
1823
1824 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
1825 {
1826         enum nl80211_band band;
1827         unsigned int n_channels = 0;
1828
1829         for (band = 0; band < NUM_NL80211_BANDS; band++)
1830                 if (wiphy->bands[band])
1831                         n_channels += wiphy->bands[band]->n_channels;
1832
1833         return n_channels;
1834 }
1835 EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
1836
1837 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
1838                          struct station_info *sinfo)
1839 {
1840         struct cfg80211_registered_device *rdev;
1841         struct wireless_dev *wdev;
1842
1843         wdev = dev->ieee80211_ptr;
1844         if (!wdev)
1845                 return -EOPNOTSUPP;
1846
1847         rdev = wiphy_to_rdev(wdev->wiphy);
1848         if (!rdev->ops->get_station)
1849                 return -EOPNOTSUPP;
1850
1851         memset(sinfo, 0, sizeof(*sinfo));
1852
1853         return rdev_get_station(rdev, dev, mac_addr, sinfo);
1854 }
1855 EXPORT_SYMBOL(cfg80211_get_station);
1856
1857 void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
1858 {
1859         int i;
1860
1861         if (!f)
1862                 return;
1863
1864         kfree(f->serv_spec_info);
1865         kfree(f->srf_bf);
1866         kfree(f->srf_macs);
1867         for (i = 0; i < f->num_rx_filters; i++)
1868                 kfree(f->rx_filters[i].filter);
1869
1870         for (i = 0; i < f->num_tx_filters; i++)
1871                 kfree(f->tx_filters[i].filter);
1872
1873         kfree(f->rx_filters);
1874         kfree(f->tx_filters);
1875         kfree(f);
1876 }
1877 EXPORT_SYMBOL(cfg80211_free_nan_func);
1878
1879 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
1880                                 u32 center_freq_khz, u32 bw_khz)
1881 {
1882         u32 start_freq_khz, end_freq_khz;
1883
1884         start_freq_khz = center_freq_khz - (bw_khz / 2);
1885         end_freq_khz = center_freq_khz + (bw_khz / 2);
1886
1887         if (start_freq_khz >= freq_range->start_freq_khz &&
1888             end_freq_khz <= freq_range->end_freq_khz)
1889                 return true;
1890
1891         return false;
1892 }
1893
1894 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
1895 {
1896         sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
1897                                 sizeof(*(sinfo->pertid)),
1898                                 gfp);
1899         if (!sinfo->pertid)
1900                 return -ENOMEM;
1901
1902         return 0;
1903 }
1904 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
1905
1906 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1907 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1908 const unsigned char rfc1042_header[] __aligned(2) =
1909         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1910 EXPORT_SYMBOL(rfc1042_header);
1911
1912 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1913 const unsigned char bridge_tunnel_header[] __aligned(2) =
1914         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1915 EXPORT_SYMBOL(bridge_tunnel_header);
1916
1917 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
1918 struct iapp_layer2_update {
1919         u8 da[ETH_ALEN];        /* broadcast */
1920         u8 sa[ETH_ALEN];        /* STA addr */
1921         __be16 len;             /* 6 */
1922         u8 dsap;                /* 0 */
1923         u8 ssap;                /* 0 */
1924         u8 control;
1925         u8 xid_info[3];
1926 } __packed;
1927
1928 void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
1929 {
1930         struct iapp_layer2_update *msg;
1931         struct sk_buff *skb;
1932
1933         /* Send Level 2 Update Frame to update forwarding tables in layer 2
1934          * bridge devices */
1935
1936         skb = dev_alloc_skb(sizeof(*msg));
1937         if (!skb)
1938                 return;
1939         msg = skb_put(skb, sizeof(*msg));
1940
1941         /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
1942          * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
1943
1944         eth_broadcast_addr(msg->da);
1945         ether_addr_copy(msg->sa, addr);
1946         msg->len = htons(6);
1947         msg->dsap = 0;
1948         msg->ssap = 0x01;       /* NULL LSAP, CR Bit: Response */
1949         msg->control = 0xaf;    /* XID response lsb.1111F101.
1950                                  * F=0 (no poll command; unsolicited frame) */
1951         msg->xid_info[0] = 0x81;        /* XID format identifier */
1952         msg->xid_info[1] = 1;   /* LLC types/classes: Type 1 LLC */
1953         msg->xid_info[2] = 0;   /* XID sender's receive window size (RW) */
1954
1955         skb->dev = dev;
1956         skb->protocol = eth_type_trans(skb, dev);
1957         memset(skb->cb, 0, sizeof(skb->cb));
1958         netif_rx_ni(skb);
1959 }
1960 EXPORT_SYMBOL(cfg80211_send_layer2_update);
1961
1962 int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap,
1963                               enum ieee80211_vht_chanwidth bw,
1964                               int mcs, bool ext_nss_bw_capable)
1965 {
1966         u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map);
1967         int max_vht_nss = 0;
1968         int ext_nss_bw;
1969         int supp_width;
1970         int i, mcs_encoding;
1971
1972         if (map == 0xffff)
1973                 return 0;
1974
1975         if (WARN_ON(mcs > 9))
1976                 return 0;
1977         if (mcs <= 7)
1978                 mcs_encoding = 0;
1979         else if (mcs == 8)
1980                 mcs_encoding = 1;
1981         else
1982                 mcs_encoding = 2;
1983
1984         /* find max_vht_nss for the given MCS */
1985         for (i = 7; i >= 0; i--) {
1986                 int supp = (map >> (2 * i)) & 3;
1987
1988                 if (supp == 3)
1989                         continue;
1990
1991                 if (supp >= mcs_encoding) {
1992                         max_vht_nss = i;
1993                         break;
1994                 }
1995         }
1996
1997         if (!(cap->supp_mcs.tx_mcs_map &
1998                         cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
1999                 return max_vht_nss;
2000
2001         ext_nss_bw = le32_get_bits(cap->vht_cap_info,
2002                                    IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
2003         supp_width = le32_get_bits(cap->vht_cap_info,
2004                                    IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
2005
2006         /* if not capable, treat ext_nss_bw as 0 */
2007         if (!ext_nss_bw_capable)
2008                 ext_nss_bw = 0;
2009
2010         /* This is invalid */
2011         if (supp_width == 3)
2012                 return 0;
2013
2014         /* This is an invalid combination so pretend nothing is supported */
2015         if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2))
2016                 return 0;
2017
2018         /*
2019          * Cover all the special cases according to IEEE 802.11-2016
2020          * Table 9-250. All other cases are either factor of 1 or not
2021          * valid/supported.
2022          */
2023         switch (bw) {
2024         case IEEE80211_VHT_CHANWIDTH_USE_HT:
2025         case IEEE80211_VHT_CHANWIDTH_80MHZ:
2026                 if ((supp_width == 1 || supp_width == 2) &&
2027                     ext_nss_bw == 3)
2028                         return 2 * max_vht_nss;
2029                 break;
2030         case IEEE80211_VHT_CHANWIDTH_160MHZ:
2031                 if (supp_width == 0 &&
2032                     (ext_nss_bw == 1 || ext_nss_bw == 2))
2033                         return max_vht_nss / 2;
2034                 if (supp_width == 0 &&
2035                     ext_nss_bw == 3)
2036                         return (3 * max_vht_nss) / 4;
2037                 if (supp_width == 1 &&
2038                     ext_nss_bw == 3)
2039                         return 2 * max_vht_nss;
2040                 break;
2041         case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
2042                 if (supp_width == 0 && ext_nss_bw == 1)
2043                         return 0; /* not possible */
2044                 if (supp_width == 0 &&
2045                     ext_nss_bw == 2)
2046                         return max_vht_nss / 2;
2047                 if (supp_width == 0 &&
2048                     ext_nss_bw == 3)
2049                         return (3 * max_vht_nss) / 4;
2050                 if (supp_width == 1 &&
2051                     ext_nss_bw == 0)
2052                         return 0; /* not possible */
2053                 if (supp_width == 1 &&
2054                     ext_nss_bw == 1)
2055                         return max_vht_nss / 2;
2056                 if (supp_width == 1 &&
2057                     ext_nss_bw == 2)
2058                         return (3 * max_vht_nss) / 4;
2059                 break;
2060         }
2061
2062         /* not covered or invalid combination received */
2063         return max_vht_nss;
2064 }
2065 EXPORT_SYMBOL(ieee80211_get_vht_max_nss);