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