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