Mention branches and keyring.
[releases.git] / 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-2023 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 const 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 {
48         struct ieee80211_rate *bitrates;
49         u32 mandatory_rates = 0;
50         enum ieee80211_rate_flags mandatory_flag;
51         int i;
52
53         if (WARN_ON(!sband))
54                 return 1;
55
56         if (sband->band == NL80211_BAND_2GHZ)
57                 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
58         else
59                 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
60
61         bitrates = sband->bitrates;
62         for (i = 0; i < sband->n_bitrates; i++)
63                 if (bitrates[i].flags & mandatory_flag)
64                         mandatory_rates |= BIT(i);
65         return mandatory_rates;
66 }
67 EXPORT_SYMBOL(ieee80211_mandatory_rates);
68
69 u32 ieee80211_channel_to_freq_khz(int chan, enum nl80211_band band)
70 {
71         /* see 802.11 17.3.8.3.2 and Annex J
72          * there are overlapping channel numbers in 5GHz and 2GHz bands */
73         if (chan <= 0)
74                 return 0; /* not supported */
75         switch (band) {
76         case NL80211_BAND_2GHZ:
77         case NL80211_BAND_LC:
78                 if (chan == 14)
79                         return MHZ_TO_KHZ(2484);
80                 else if (chan < 14)
81                         return MHZ_TO_KHZ(2407 + chan * 5);
82                 break;
83         case NL80211_BAND_5GHZ:
84                 if (chan >= 182 && chan <= 196)
85                         return MHZ_TO_KHZ(4000 + chan * 5);
86                 else
87                         return MHZ_TO_KHZ(5000 + chan * 5);
88                 break;
89         case NL80211_BAND_6GHZ:
90                 /* see 802.11ax D6.1 27.3.23.2 */
91                 if (chan == 2)
92                         return MHZ_TO_KHZ(5935);
93                 if (chan <= 233)
94                         return MHZ_TO_KHZ(5950 + chan * 5);
95                 break;
96         case NL80211_BAND_60GHZ:
97                 if (chan < 7)
98                         return MHZ_TO_KHZ(56160 + chan * 2160);
99                 break;
100         case NL80211_BAND_S1GHZ:
101                 return 902000 + chan * 500;
102         default:
103                 ;
104         }
105         return 0; /* not supported */
106 }
107 EXPORT_SYMBOL(ieee80211_channel_to_freq_khz);
108
109 enum nl80211_chan_width
110 ieee80211_s1g_channel_width(const struct ieee80211_channel *chan)
111 {
112         if (WARN_ON(!chan || chan->band != NL80211_BAND_S1GHZ))
113                 return NL80211_CHAN_WIDTH_20_NOHT;
114
115         /*S1G defines a single allowed channel width per channel.
116          * Extract that width here.
117          */
118         if (chan->flags & IEEE80211_CHAN_1MHZ)
119                 return NL80211_CHAN_WIDTH_1;
120         else if (chan->flags & IEEE80211_CHAN_2MHZ)
121                 return NL80211_CHAN_WIDTH_2;
122         else if (chan->flags & IEEE80211_CHAN_4MHZ)
123                 return NL80211_CHAN_WIDTH_4;
124         else if (chan->flags & IEEE80211_CHAN_8MHZ)
125                 return NL80211_CHAN_WIDTH_8;
126         else if (chan->flags & IEEE80211_CHAN_16MHZ)
127                 return NL80211_CHAN_WIDTH_16;
128
129         pr_err("unknown channel width for channel at %dKHz?\n",
130                ieee80211_channel_to_khz(chan));
131
132         return NL80211_CHAN_WIDTH_1;
133 }
134 EXPORT_SYMBOL(ieee80211_s1g_channel_width);
135
136 int ieee80211_freq_khz_to_channel(u32 freq)
137 {
138         /* TODO: just handle MHz for now */
139         freq = KHZ_TO_MHZ(freq);
140
141         /* see 802.11 17.3.8.3.2 and Annex J */
142         if (freq == 2484)
143                 return 14;
144         else if (freq < 2484)
145                 return (freq - 2407) / 5;
146         else if (freq >= 4910 && freq <= 4980)
147                 return (freq - 4000) / 5;
148         else if (freq < 5925)
149                 return (freq - 5000) / 5;
150         else if (freq == 5935)
151                 return 2;
152         else if (freq <= 45000) /* DMG band lower limit */
153                 /* see 802.11ax D6.1 27.3.22.2 */
154                 return (freq - 5950) / 5;
155         else if (freq >= 58320 && freq <= 70200)
156                 return (freq - 56160) / 2160;
157         else
158                 return 0;
159 }
160 EXPORT_SYMBOL(ieee80211_freq_khz_to_channel);
161
162 struct ieee80211_channel *ieee80211_get_channel_khz(struct wiphy *wiphy,
163                                                     u32 freq)
164 {
165         enum nl80211_band band;
166         struct ieee80211_supported_band *sband;
167         int i;
168
169         for (band = 0; band < NUM_NL80211_BANDS; band++) {
170                 sband = wiphy->bands[band];
171
172                 if (!sband)
173                         continue;
174
175                 for (i = 0; i < sband->n_channels; i++) {
176                         struct ieee80211_channel *chan = &sband->channels[i];
177
178                         if (ieee80211_channel_to_khz(chan) == freq)
179                                 return chan;
180                 }
181         }
182
183         return NULL;
184 }
185 EXPORT_SYMBOL(ieee80211_get_channel_khz);
186
187 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband)
188 {
189         int i, want;
190
191         switch (sband->band) {
192         case NL80211_BAND_5GHZ:
193         case NL80211_BAND_6GHZ:
194                 want = 3;
195                 for (i = 0; i < sband->n_bitrates; i++) {
196                         if (sband->bitrates[i].bitrate == 60 ||
197                             sband->bitrates[i].bitrate == 120 ||
198                             sband->bitrates[i].bitrate == 240) {
199                                 sband->bitrates[i].flags |=
200                                         IEEE80211_RATE_MANDATORY_A;
201                                 want--;
202                         }
203                 }
204                 WARN_ON(want);
205                 break;
206         case NL80211_BAND_2GHZ:
207         case NL80211_BAND_LC:
208                 want = 7;
209                 for (i = 0; i < sband->n_bitrates; i++) {
210                         switch (sband->bitrates[i].bitrate) {
211                         case 10:
212                         case 20:
213                         case 55:
214                         case 110:
215                                 sband->bitrates[i].flags |=
216                                         IEEE80211_RATE_MANDATORY_B |
217                                         IEEE80211_RATE_MANDATORY_G;
218                                 want--;
219                                 break;
220                         case 60:
221                         case 120:
222                         case 240:
223                                 sband->bitrates[i].flags |=
224                                         IEEE80211_RATE_MANDATORY_G;
225                                 want--;
226                                 fallthrough;
227                         default:
228                                 sband->bitrates[i].flags |=
229                                         IEEE80211_RATE_ERP_G;
230                                 break;
231                         }
232                 }
233                 WARN_ON(want != 0 && want != 3);
234                 break;
235         case NL80211_BAND_60GHZ:
236                 /* check for mandatory HT MCS 1..4 */
237                 WARN_ON(!sband->ht_cap.ht_supported);
238                 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
239                 break;
240         case NL80211_BAND_S1GHZ:
241                 /* Figure 9-589bd: 3 means unsupported, so != 3 means at least
242                  * mandatory is ok.
243                  */
244                 WARN_ON((sband->s1g_cap.nss_mcs[0] & 0x3) == 0x3);
245                 break;
246         case NUM_NL80211_BANDS:
247         default:
248                 WARN_ON(1);
249                 break;
250         }
251 }
252
253 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
254 {
255         enum nl80211_band band;
256
257         for (band = 0; band < NUM_NL80211_BANDS; band++)
258                 if (wiphy->bands[band])
259                         set_mandatory_flags_band(wiphy->bands[band]);
260 }
261
262 bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
263 {
264         int i;
265         for (i = 0; i < wiphy->n_cipher_suites; i++)
266                 if (cipher == wiphy->cipher_suites[i])
267                         return true;
268         return false;
269 }
270
271 static bool
272 cfg80211_igtk_cipher_supported(struct cfg80211_registered_device *rdev)
273 {
274         struct wiphy *wiphy = &rdev->wiphy;
275         int i;
276
277         for (i = 0; i < wiphy->n_cipher_suites; i++) {
278                 switch (wiphy->cipher_suites[i]) {
279                 case WLAN_CIPHER_SUITE_AES_CMAC:
280                 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
281                 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
282                 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
283                         return true;
284                 }
285         }
286
287         return false;
288 }
289
290 bool cfg80211_valid_key_idx(struct cfg80211_registered_device *rdev,
291                             int key_idx, bool pairwise)
292 {
293         int max_key_idx;
294
295         if (pairwise)
296                 max_key_idx = 3;
297         else if (wiphy_ext_feature_isset(&rdev->wiphy,
298                                          NL80211_EXT_FEATURE_BEACON_PROTECTION) ||
299                  wiphy_ext_feature_isset(&rdev->wiphy,
300                                          NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
301                 max_key_idx = 7;
302         else if (cfg80211_igtk_cipher_supported(rdev))
303                 max_key_idx = 5;
304         else
305                 max_key_idx = 3;
306
307         if (key_idx < 0 || key_idx > max_key_idx)
308                 return false;
309
310         return true;
311 }
312
313 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
314                                    struct key_params *params, int key_idx,
315                                    bool pairwise, const u8 *mac_addr)
316 {
317         if (!cfg80211_valid_key_idx(rdev, key_idx, pairwise))
318                 return -EINVAL;
319
320         if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
321                 return -EINVAL;
322
323         if (pairwise && !mac_addr)
324                 return -EINVAL;
325
326         switch (params->cipher) {
327         case WLAN_CIPHER_SUITE_TKIP:
328                 /* Extended Key ID can only be used with CCMP/GCMP ciphers */
329                 if ((pairwise && key_idx) ||
330                     params->mode != NL80211_KEY_RX_TX)
331                         return -EINVAL;
332                 break;
333         case WLAN_CIPHER_SUITE_CCMP:
334         case WLAN_CIPHER_SUITE_CCMP_256:
335         case WLAN_CIPHER_SUITE_GCMP:
336         case WLAN_CIPHER_SUITE_GCMP_256:
337                 /* IEEE802.11-2016 allows only 0 and - when supporting
338                  * Extended Key ID - 1 as index for pairwise keys.
339                  * @NL80211_KEY_NO_TX is only allowed for pairwise keys when
340                  * the driver supports Extended Key ID.
341                  * @NL80211_KEY_SET_TX can't be set when installing and
342                  * validating a key.
343                  */
344                 if ((params->mode == NL80211_KEY_NO_TX && !pairwise) ||
345                     params->mode == NL80211_KEY_SET_TX)
346                         return -EINVAL;
347                 if (wiphy_ext_feature_isset(&rdev->wiphy,
348                                             NL80211_EXT_FEATURE_EXT_KEY_ID)) {
349                         if (pairwise && (key_idx < 0 || key_idx > 1))
350                                 return -EINVAL;
351                 } else if (pairwise && key_idx) {
352                         return -EINVAL;
353                 }
354                 break;
355         case WLAN_CIPHER_SUITE_AES_CMAC:
356         case WLAN_CIPHER_SUITE_BIP_CMAC_256:
357         case WLAN_CIPHER_SUITE_BIP_GMAC_128:
358         case WLAN_CIPHER_SUITE_BIP_GMAC_256:
359                 /* Disallow BIP (group-only) cipher as pairwise cipher */
360                 if (pairwise)
361                         return -EINVAL;
362                 if (key_idx < 4)
363                         return -EINVAL;
364                 break;
365         case WLAN_CIPHER_SUITE_WEP40:
366         case WLAN_CIPHER_SUITE_WEP104:
367                 if (key_idx > 3)
368                         return -EINVAL;
369                 break;
370         default:
371                 break;
372         }
373
374         switch (params->cipher) {
375         case WLAN_CIPHER_SUITE_WEP40:
376                 if (params->key_len != WLAN_KEY_LEN_WEP40)
377                         return -EINVAL;
378                 break;
379         case WLAN_CIPHER_SUITE_TKIP:
380                 if (params->key_len != WLAN_KEY_LEN_TKIP)
381                         return -EINVAL;
382                 break;
383         case WLAN_CIPHER_SUITE_CCMP:
384                 if (params->key_len != WLAN_KEY_LEN_CCMP)
385                         return -EINVAL;
386                 break;
387         case WLAN_CIPHER_SUITE_CCMP_256:
388                 if (params->key_len != WLAN_KEY_LEN_CCMP_256)
389                         return -EINVAL;
390                 break;
391         case WLAN_CIPHER_SUITE_GCMP:
392                 if (params->key_len != WLAN_KEY_LEN_GCMP)
393                         return -EINVAL;
394                 break;
395         case WLAN_CIPHER_SUITE_GCMP_256:
396                 if (params->key_len != WLAN_KEY_LEN_GCMP_256)
397                         return -EINVAL;
398                 break;
399         case WLAN_CIPHER_SUITE_WEP104:
400                 if (params->key_len != WLAN_KEY_LEN_WEP104)
401                         return -EINVAL;
402                 break;
403         case WLAN_CIPHER_SUITE_AES_CMAC:
404                 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
405                         return -EINVAL;
406                 break;
407         case WLAN_CIPHER_SUITE_BIP_CMAC_256:
408                 if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
409                         return -EINVAL;
410                 break;
411         case WLAN_CIPHER_SUITE_BIP_GMAC_128:
412                 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
413                         return -EINVAL;
414                 break;
415         case WLAN_CIPHER_SUITE_BIP_GMAC_256:
416                 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
417                         return -EINVAL;
418                 break;
419         default:
420                 /*
421                  * We don't know anything about this algorithm,
422                  * allow using it -- but the driver must check
423                  * all parameters! We still check below whether
424                  * or not the driver supports this algorithm,
425                  * of course.
426                  */
427                 break;
428         }
429
430         if (params->seq) {
431                 switch (params->cipher) {
432                 case WLAN_CIPHER_SUITE_WEP40:
433                 case WLAN_CIPHER_SUITE_WEP104:
434                         /* These ciphers do not use key sequence */
435                         return -EINVAL;
436                 case WLAN_CIPHER_SUITE_TKIP:
437                 case WLAN_CIPHER_SUITE_CCMP:
438                 case WLAN_CIPHER_SUITE_CCMP_256:
439                 case WLAN_CIPHER_SUITE_GCMP:
440                 case WLAN_CIPHER_SUITE_GCMP_256:
441                 case WLAN_CIPHER_SUITE_AES_CMAC:
442                 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
443                 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
444                 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
445                         if (params->seq_len != 6)
446                                 return -EINVAL;
447                         break;
448                 }
449         }
450
451         if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
452                 return -EINVAL;
453
454         return 0;
455 }
456
457 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
458 {
459         unsigned int hdrlen = 24;
460
461         if (ieee80211_is_ext(fc)) {
462                 hdrlen = 4;
463                 goto out;
464         }
465
466         if (ieee80211_is_data(fc)) {
467                 if (ieee80211_has_a4(fc))
468                         hdrlen = 30;
469                 if (ieee80211_is_data_qos(fc)) {
470                         hdrlen += IEEE80211_QOS_CTL_LEN;
471                         if (ieee80211_has_order(fc))
472                                 hdrlen += IEEE80211_HT_CTL_LEN;
473                 }
474                 goto out;
475         }
476
477         if (ieee80211_is_mgmt(fc)) {
478                 if (ieee80211_has_order(fc))
479                         hdrlen += IEEE80211_HT_CTL_LEN;
480                 goto out;
481         }
482
483         if (ieee80211_is_ctl(fc)) {
484                 /*
485                  * ACK and CTS are 10 bytes, all others 16. To see how
486                  * to get this condition consider
487                  *   subtype mask:   0b0000000011110000 (0x00F0)
488                  *   ACK subtype:    0b0000000011010000 (0x00D0)
489                  *   CTS subtype:    0b0000000011000000 (0x00C0)
490                  *   bits that matter:         ^^^      (0x00E0)
491                  *   value of those: 0b0000000011000000 (0x00C0)
492                  */
493                 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
494                         hdrlen = 10;
495                 else
496                         hdrlen = 16;
497         }
498 out:
499         return hdrlen;
500 }
501 EXPORT_SYMBOL(ieee80211_hdrlen);
502
503 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
504 {
505         const struct ieee80211_hdr *hdr =
506                         (const struct ieee80211_hdr *)skb->data;
507         unsigned int hdrlen;
508
509         if (unlikely(skb->len < 10))
510                 return 0;
511         hdrlen = ieee80211_hdrlen(hdr->frame_control);
512         if (unlikely(hdrlen > skb->len))
513                 return 0;
514         return hdrlen;
515 }
516 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
517
518 static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
519 {
520         int ae = flags & MESH_FLAGS_AE;
521         /* 802.11-2012, 8.2.4.7.3 */
522         switch (ae) {
523         default:
524         case 0:
525                 return 6;
526         case MESH_FLAGS_AE_A4:
527                 return 12;
528         case MESH_FLAGS_AE_A5_A6:
529                 return 18;
530         }
531 }
532
533 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
534 {
535         return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
536 }
537 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
538
539 bool ieee80211_get_8023_tunnel_proto(const void *hdr, __be16 *proto)
540 {
541         const __be16 *hdr_proto = hdr + ETH_ALEN;
542
543         if (!(ether_addr_equal(hdr, rfc1042_header) &&
544               *hdr_proto != htons(ETH_P_AARP) &&
545               *hdr_proto != htons(ETH_P_IPX)) &&
546             !ether_addr_equal(hdr, bridge_tunnel_header))
547                 return false;
548
549         *proto = *hdr_proto;
550
551         return true;
552 }
553 EXPORT_SYMBOL(ieee80211_get_8023_tunnel_proto);
554
555 int ieee80211_strip_8023_mesh_hdr(struct sk_buff *skb)
556 {
557         const void *mesh_addr;
558         struct {
559                 struct ethhdr eth;
560                 u8 flags;
561         } payload;
562         int hdrlen;
563         int ret;
564
565         ret = skb_copy_bits(skb, 0, &payload, sizeof(payload));
566         if (ret)
567                 return ret;
568
569         hdrlen = sizeof(payload.eth) + __ieee80211_get_mesh_hdrlen(payload.flags);
570
571         if (likely(pskb_may_pull(skb, hdrlen + 8) &&
572                    ieee80211_get_8023_tunnel_proto(skb->data + hdrlen,
573                                                    &payload.eth.h_proto)))
574                 hdrlen += ETH_ALEN + 2;
575         else if (!pskb_may_pull(skb, hdrlen))
576                 return -EINVAL;
577         else
578                 payload.eth.h_proto = htons(skb->len - hdrlen);
579
580         mesh_addr = skb->data + sizeof(payload.eth) + ETH_ALEN;
581         switch (payload.flags & MESH_FLAGS_AE) {
582         case MESH_FLAGS_AE_A4:
583                 memcpy(&payload.eth.h_source, mesh_addr, ETH_ALEN);
584                 break;
585         case MESH_FLAGS_AE_A5_A6:
586                 memcpy(&payload.eth, mesh_addr, 2 * ETH_ALEN);
587                 break;
588         default:
589                 break;
590         }
591
592         pskb_pull(skb, hdrlen - sizeof(payload.eth));
593         memcpy(skb->data, &payload.eth, sizeof(payload.eth));
594
595         return 0;
596 }
597 EXPORT_SYMBOL(ieee80211_strip_8023_mesh_hdr);
598
599 int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
600                                   const u8 *addr, enum nl80211_iftype iftype,
601                                   u8 data_offset, bool is_amsdu)
602 {
603         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
604         struct {
605                 u8 hdr[ETH_ALEN] __aligned(2);
606                 __be16 proto;
607         } payload;
608         struct ethhdr tmp;
609         u16 hdrlen;
610
611         if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
612                 return -1;
613
614         hdrlen = ieee80211_hdrlen(hdr->frame_control) + data_offset;
615         if (skb->len < hdrlen)
616                 return -1;
617
618         /* convert IEEE 802.11 header + possible LLC headers into Ethernet
619          * header
620          * IEEE 802.11 address fields:
621          * ToDS FromDS Addr1 Addr2 Addr3 Addr4
622          *   0     0   DA    SA    BSSID n/a
623          *   0     1   DA    BSSID SA    n/a
624          *   1     0   BSSID SA    DA    n/a
625          *   1     1   RA    TA    DA    SA
626          */
627         memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
628         memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
629
630         switch (hdr->frame_control &
631                 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
632         case cpu_to_le16(IEEE80211_FCTL_TODS):
633                 if (unlikely(iftype != NL80211_IFTYPE_AP &&
634                              iftype != NL80211_IFTYPE_AP_VLAN &&
635                              iftype != NL80211_IFTYPE_P2P_GO))
636                         return -1;
637                 break;
638         case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
639                 if (unlikely(iftype != NL80211_IFTYPE_MESH_POINT &&
640                              iftype != NL80211_IFTYPE_AP_VLAN &&
641                              iftype != NL80211_IFTYPE_STATION))
642                         return -1;
643                 break;
644         case cpu_to_le16(IEEE80211_FCTL_FROMDS):
645                 if ((iftype != NL80211_IFTYPE_STATION &&
646                      iftype != NL80211_IFTYPE_P2P_CLIENT &&
647                      iftype != NL80211_IFTYPE_MESH_POINT) ||
648                     (is_multicast_ether_addr(tmp.h_dest) &&
649                      ether_addr_equal(tmp.h_source, addr)))
650                         return -1;
651                 break;
652         case cpu_to_le16(0):
653                 if (iftype != NL80211_IFTYPE_ADHOC &&
654                     iftype != NL80211_IFTYPE_STATION &&
655                     iftype != NL80211_IFTYPE_OCB)
656                                 return -1;
657                 break;
658         }
659
660         if (likely(!is_amsdu && iftype != NL80211_IFTYPE_MESH_POINT &&
661                    skb_copy_bits(skb, hdrlen, &payload, sizeof(payload)) == 0 &&
662                    ieee80211_get_8023_tunnel_proto(&payload, &tmp.h_proto))) {
663                 /* remove RFC1042 or Bridge-Tunnel encapsulation */
664                 hdrlen += ETH_ALEN + 2;
665                 skb_postpull_rcsum(skb, &payload, ETH_ALEN + 2);
666         } else {
667                 tmp.h_proto = htons(skb->len - hdrlen);
668         }
669
670         pskb_pull(skb, hdrlen);
671
672         if (!ehdr)
673                 ehdr = skb_push(skb, sizeof(struct ethhdr));
674         memcpy(ehdr, &tmp, sizeof(tmp));
675
676         return 0;
677 }
678 EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
679
680 static void
681 __frame_add_frag(struct sk_buff *skb, struct page *page,
682                  void *ptr, int len, int size)
683 {
684         struct skb_shared_info *sh = skb_shinfo(skb);
685         int page_offset;
686
687         get_page(page);
688         page_offset = ptr - page_address(page);
689         skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
690 }
691
692 static void
693 __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
694                             int offset, int len)
695 {
696         struct skb_shared_info *sh = skb_shinfo(skb);
697         const skb_frag_t *frag = &sh->frags[0];
698         struct page *frag_page;
699         void *frag_ptr;
700         int frag_len, frag_size;
701         int head_size = skb->len - skb->data_len;
702         int cur_len;
703
704         frag_page = virt_to_head_page(skb->head);
705         frag_ptr = skb->data;
706         frag_size = head_size;
707
708         while (offset >= frag_size) {
709                 offset -= frag_size;
710                 frag_page = skb_frag_page(frag);
711                 frag_ptr = skb_frag_address(frag);
712                 frag_size = skb_frag_size(frag);
713                 frag++;
714         }
715
716         frag_ptr += offset;
717         frag_len = frag_size - offset;
718
719         cur_len = min(len, frag_len);
720
721         __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
722         len -= cur_len;
723
724         while (len > 0) {
725                 frag_len = skb_frag_size(frag);
726                 cur_len = min(len, frag_len);
727                 __frame_add_frag(frame, skb_frag_page(frag),
728                                  skb_frag_address(frag), cur_len, frag_len);
729                 len -= cur_len;
730                 frag++;
731         }
732 }
733
734 static struct sk_buff *
735 __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
736                        int offset, int len, bool reuse_frag,
737                        int min_len)
738 {
739         struct sk_buff *frame;
740         int cur_len = len;
741
742         if (skb->len - offset < len)
743                 return NULL;
744
745         /*
746          * When reusing framents, copy some data to the head to simplify
747          * ethernet header handling and speed up protocol header processing
748          * in the stack later.
749          */
750         if (reuse_frag)
751                 cur_len = min_t(int, len, min_len);
752
753         /*
754          * Allocate and reserve two bytes more for payload
755          * alignment since sizeof(struct ethhdr) is 14.
756          */
757         frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
758         if (!frame)
759                 return NULL;
760
761         frame->priority = skb->priority;
762         skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
763         skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
764
765         len -= cur_len;
766         if (!len)
767                 return frame;
768
769         offset += cur_len;
770         __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
771
772         return frame;
773 }
774
775 static u16
776 ieee80211_amsdu_subframe_length(void *field, u8 mesh_flags, u8 hdr_type)
777 {
778         __le16 *field_le = field;
779         __be16 *field_be = field;
780         u16 len;
781
782         if (hdr_type >= 2)
783                 len = le16_to_cpu(*field_le);
784         else
785                 len = be16_to_cpu(*field_be);
786         if (hdr_type)
787                 len += __ieee80211_get_mesh_hdrlen(mesh_flags);
788
789         return len;
790 }
791
792 bool ieee80211_is_valid_amsdu(struct sk_buff *skb, u8 mesh_hdr)
793 {
794         int offset = 0, subframe_len, padding;
795
796         for (offset = 0; offset < skb->len; offset += subframe_len + padding) {
797                 int remaining = skb->len - offset;
798                 struct {
799                     __be16 len;
800                     u8 mesh_flags;
801                 } hdr;
802                 u16 len;
803
804                 if (sizeof(hdr) > remaining)
805                         return false;
806
807                 if (skb_copy_bits(skb, offset + 2 * ETH_ALEN, &hdr, sizeof(hdr)) < 0)
808                         return false;
809
810                 len = ieee80211_amsdu_subframe_length(&hdr.len, hdr.mesh_flags,
811                                                       mesh_hdr);
812                 subframe_len = sizeof(struct ethhdr) + len;
813                 padding = (4 - subframe_len) & 0x3;
814
815                 if (subframe_len > remaining)
816                         return false;
817         }
818
819         return true;
820 }
821 EXPORT_SYMBOL(ieee80211_is_valid_amsdu);
822
823 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
824                               const u8 *addr, enum nl80211_iftype iftype,
825                               const unsigned int extra_headroom,
826                               const u8 *check_da, const u8 *check_sa,
827                               u8 mesh_control)
828 {
829         unsigned int hlen = ALIGN(extra_headroom, 4);
830         struct sk_buff *frame = NULL;
831         int offset = 0;
832         struct {
833                 struct ethhdr eth;
834                 uint8_t flags;
835         } hdr;
836         bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
837         bool reuse_skb = false;
838         bool last = false;
839         int copy_len = sizeof(hdr.eth);
840
841         if (iftype == NL80211_IFTYPE_MESH_POINT)
842                 copy_len = sizeof(hdr);
843
844         while (!last) {
845                 int remaining = skb->len - offset;
846                 unsigned int subframe_len;
847                 int len, mesh_len = 0;
848                 u8 padding;
849
850                 if (copy_len > remaining)
851                         goto purge;
852
853                 skb_copy_bits(skb, offset, &hdr, copy_len);
854                 if (iftype == NL80211_IFTYPE_MESH_POINT)
855                         mesh_len = __ieee80211_get_mesh_hdrlen(hdr.flags);
856                 len = ieee80211_amsdu_subframe_length(&hdr.eth.h_proto, hdr.flags,
857                                                       mesh_control);
858                 subframe_len = sizeof(struct ethhdr) + len;
859                 padding = (4 - subframe_len) & 0x3;
860
861                 /* the last MSDU has no padding */
862                 if (subframe_len > remaining)
863                         goto purge;
864                 /* mitigate A-MSDU aggregation injection attacks */
865                 if (ether_addr_equal(hdr.eth.h_dest, rfc1042_header))
866                         goto purge;
867
868                 offset += sizeof(struct ethhdr);
869                 last = remaining <= subframe_len + padding;
870
871                 /* FIXME: should we really accept multicast DA? */
872                 if ((check_da && !is_multicast_ether_addr(hdr.eth.h_dest) &&
873                      !ether_addr_equal(check_da, hdr.eth.h_dest)) ||
874                     (check_sa && !ether_addr_equal(check_sa, hdr.eth.h_source))) {
875                         offset += len + padding;
876                         continue;
877                 }
878
879                 /* reuse skb for the last subframe */
880                 if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
881                         skb_pull(skb, offset);
882                         frame = skb;
883                         reuse_skb = true;
884                 } else {
885                         frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
886                                                        reuse_frag, 32 + mesh_len);
887                         if (!frame)
888                                 goto purge;
889
890                         offset += len + padding;
891                 }
892
893                 skb_reset_network_header(frame);
894                 frame->dev = skb->dev;
895                 frame->priority = skb->priority;
896
897                 if (likely(iftype != NL80211_IFTYPE_MESH_POINT &&
898                            ieee80211_get_8023_tunnel_proto(frame->data, &hdr.eth.h_proto)))
899                         skb_pull(frame, ETH_ALEN + 2);
900
901                 memcpy(skb_push(frame, sizeof(hdr.eth)), &hdr.eth, sizeof(hdr.eth));
902                 __skb_queue_tail(list, frame);
903         }
904
905         if (!reuse_skb)
906                 dev_kfree_skb(skb);
907
908         return;
909
910  purge:
911         __skb_queue_purge(list);
912         dev_kfree_skb(skb);
913 }
914 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
915
916 /* Given a data frame determine the 802.1p/1d tag to use. */
917 unsigned int cfg80211_classify8021d(struct sk_buff *skb,
918                                     struct cfg80211_qos_map *qos_map)
919 {
920         unsigned int dscp;
921         unsigned char vlan_priority;
922         unsigned int ret;
923
924         /* skb->priority values from 256->263 are magic values to
925          * directly indicate a specific 802.1d priority.  This is used
926          * to allow 802.1d priority to be passed directly in from VLAN
927          * tags, etc.
928          */
929         if (skb->priority >= 256 && skb->priority <= 263) {
930                 ret = skb->priority - 256;
931                 goto out;
932         }
933
934         if (skb_vlan_tag_present(skb)) {
935                 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
936                         >> VLAN_PRIO_SHIFT;
937                 if (vlan_priority > 0) {
938                         ret = vlan_priority;
939                         goto out;
940                 }
941         }
942
943         switch (skb->protocol) {
944         case htons(ETH_P_IP):
945                 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
946                 break;
947         case htons(ETH_P_IPV6):
948                 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
949                 break;
950         case htons(ETH_P_MPLS_UC):
951         case htons(ETH_P_MPLS_MC): {
952                 struct mpls_label mpls_tmp, *mpls;
953
954                 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
955                                           sizeof(*mpls), &mpls_tmp);
956                 if (!mpls)
957                         return 0;
958
959                 ret = (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
960                         >> MPLS_LS_TC_SHIFT;
961                 goto out;
962         }
963         case htons(ETH_P_80221):
964                 /* 802.21 is always network control traffic */
965                 return 7;
966         default:
967                 return 0;
968         }
969
970         if (qos_map) {
971                 unsigned int i, tmp_dscp = dscp >> 2;
972
973                 for (i = 0; i < qos_map->num_des; i++) {
974                         if (tmp_dscp == qos_map->dscp_exception[i].dscp) {
975                                 ret = qos_map->dscp_exception[i].up;
976                                 goto out;
977                         }
978                 }
979
980                 for (i = 0; i < 8; i++) {
981                         if (tmp_dscp >= qos_map->up[i].low &&
982                             tmp_dscp <= qos_map->up[i].high) {
983                                 ret = i;
984                                 goto out;
985                         }
986                 }
987         }
988
989         /* The default mapping as defined Section 2.3 in RFC8325: The three
990          * Most Significant Bits (MSBs) of the DSCP are used as the
991          * corresponding L2 markings.
992          */
993         ret = dscp >> 5;
994
995         /* Handle specific DSCP values for which the default mapping (as
996          * described above) doesn't adhere to the intended usage of the DSCP
997          * value. See section 4 in RFC8325. Specifically, for the following
998          * Diffserv Service Classes no update is needed:
999          * - Standard: DF
1000          * - Low Priority Data: CS1
1001          * - Multimedia Streaming: AF31, AF32, AF33
1002          * - Multimedia Conferencing: AF41, AF42, AF43
1003          * - Network Control Traffic: CS7
1004          * - Real-Time Interactive: CS4
1005          */
1006         switch (dscp >> 2) {
1007         case 10:
1008         case 12:
1009         case 14:
1010                 /* High throughput data: AF11, AF12, AF13 */
1011                 ret = 0;
1012                 break;
1013         case 16:
1014                 /* Operations, Administration, and Maintenance and Provisioning:
1015                  * CS2
1016                  */
1017                 ret = 0;
1018                 break;
1019         case 18:
1020         case 20:
1021         case 22:
1022                 /* Low latency data: AF21, AF22, AF23 */
1023                 ret = 3;
1024                 break;
1025         case 24:
1026                 /* Broadcasting video: CS3 */
1027                 ret = 4;
1028                 break;
1029         case 40:
1030                 /* Signaling: CS5 */
1031                 ret = 5;
1032                 break;
1033         case 44:
1034                 /* Voice Admit: VA */
1035                 ret = 6;
1036                 break;
1037         case 46:
1038                 /* Telephony traffic: EF */
1039                 ret = 6;
1040                 break;
1041         case 48:
1042                 /* Network Control Traffic: CS6 */
1043                 ret = 7;
1044                 break;
1045         }
1046 out:
1047         return array_index_nospec(ret, IEEE80211_NUM_TIDS);
1048 }
1049 EXPORT_SYMBOL(cfg80211_classify8021d);
1050
1051 const struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id)
1052 {
1053         const struct cfg80211_bss_ies *ies;
1054
1055         ies = rcu_dereference(bss->ies);
1056         if (!ies)
1057                 return NULL;
1058
1059         return cfg80211_find_elem(id, ies->data, ies->len);
1060 }
1061 EXPORT_SYMBOL(ieee80211_bss_get_elem);
1062
1063 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
1064 {
1065         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1066         struct net_device *dev = wdev->netdev;
1067         int i;
1068
1069         if (!wdev->connect_keys)
1070                 return;
1071
1072         for (i = 0; i < 4; i++) {
1073                 if (!wdev->connect_keys->params[i].cipher)
1074                         continue;
1075                 if (rdev_add_key(rdev, dev, -1, i, false, NULL,
1076                                  &wdev->connect_keys->params[i])) {
1077                         netdev_err(dev, "failed to set key %d\n", i);
1078                         continue;
1079                 }
1080                 if (wdev->connect_keys->def == i &&
1081                     rdev_set_default_key(rdev, dev, -1, i, true, true)) {
1082                         netdev_err(dev, "failed to set defkey %d\n", i);
1083                         continue;
1084                 }
1085         }
1086
1087         kfree_sensitive(wdev->connect_keys);
1088         wdev->connect_keys = NULL;
1089 }
1090
1091 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
1092 {
1093         struct cfg80211_event *ev;
1094         unsigned long flags;
1095
1096         spin_lock_irqsave(&wdev->event_lock, flags);
1097         while (!list_empty(&wdev->event_list)) {
1098                 ev = list_first_entry(&wdev->event_list,
1099                                       struct cfg80211_event, list);
1100                 list_del(&ev->list);
1101                 spin_unlock_irqrestore(&wdev->event_lock, flags);
1102
1103                 switch (ev->type) {
1104                 case EVENT_CONNECT_RESULT:
1105                         __cfg80211_connect_result(
1106                                 wdev->netdev,
1107                                 &ev->cr,
1108                                 ev->cr.status == WLAN_STATUS_SUCCESS);
1109                         break;
1110                 case EVENT_ROAMED:
1111                         __cfg80211_roamed(wdev, &ev->rm);
1112                         break;
1113                 case EVENT_DISCONNECTED:
1114                         __cfg80211_disconnected(wdev->netdev,
1115                                                 ev->dc.ie, ev->dc.ie_len,
1116                                                 ev->dc.reason,
1117                                                 !ev->dc.locally_generated);
1118                         break;
1119                 case EVENT_IBSS_JOINED:
1120                         __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
1121                                                ev->ij.channel);
1122                         break;
1123                 case EVENT_STOPPED:
1124                         cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
1125                         break;
1126                 case EVENT_PORT_AUTHORIZED:
1127                         __cfg80211_port_authorized(wdev, ev->pa.peer_addr,
1128                                                    ev->pa.td_bitmap,
1129                                                    ev->pa.td_bitmap_len);
1130                         break;
1131                 }
1132
1133                 kfree(ev);
1134
1135                 spin_lock_irqsave(&wdev->event_lock, flags);
1136         }
1137         spin_unlock_irqrestore(&wdev->event_lock, flags);
1138 }
1139
1140 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
1141 {
1142         struct wireless_dev *wdev;
1143
1144         lockdep_assert_held(&rdev->wiphy.mtx);
1145
1146         list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
1147                 cfg80211_process_wdev_events(wdev);
1148 }
1149
1150 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
1151                           struct net_device *dev, enum nl80211_iftype ntype,
1152                           struct vif_params *params)
1153 {
1154         int err;
1155         enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
1156
1157         lockdep_assert_held(&rdev->wiphy.mtx);
1158
1159         /* don't support changing VLANs, you just re-create them */
1160         if (otype == NL80211_IFTYPE_AP_VLAN)
1161                 return -EOPNOTSUPP;
1162
1163         /* cannot change into P2P device or NAN */
1164         if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
1165             ntype == NL80211_IFTYPE_NAN)
1166                 return -EOPNOTSUPP;
1167
1168         if (!rdev->ops->change_virtual_intf ||
1169             !(rdev->wiphy.interface_modes & (1 << ntype)))
1170                 return -EOPNOTSUPP;
1171
1172         if (ntype != otype) {
1173                 /* if it's part of a bridge, reject changing type to station/ibss */
1174                 if (netif_is_bridge_port(dev) &&
1175                     (ntype == NL80211_IFTYPE_ADHOC ||
1176                      ntype == NL80211_IFTYPE_STATION ||
1177                      ntype == NL80211_IFTYPE_P2P_CLIENT))
1178                         return -EBUSY;
1179
1180                 dev->ieee80211_ptr->use_4addr = false;
1181                 rdev_set_qos_map(rdev, dev, NULL);
1182
1183                 switch (otype) {
1184                 case NL80211_IFTYPE_AP:
1185                 case NL80211_IFTYPE_P2P_GO:
1186                         cfg80211_stop_ap(rdev, dev, -1, true);
1187                         break;
1188                 case NL80211_IFTYPE_ADHOC:
1189                         cfg80211_leave_ibss(rdev, dev, false);
1190                         break;
1191                 case NL80211_IFTYPE_STATION:
1192                 case NL80211_IFTYPE_P2P_CLIENT:
1193                         cfg80211_disconnect(rdev, dev,
1194                                             WLAN_REASON_DEAUTH_LEAVING, true);
1195                         break;
1196                 case NL80211_IFTYPE_MESH_POINT:
1197                         /* mesh should be handled? */
1198                         break;
1199                 case NL80211_IFTYPE_OCB:
1200                         cfg80211_leave_ocb(rdev, dev);
1201                         break;
1202                 default:
1203                         break;
1204                 }
1205
1206                 cfg80211_process_rdev_events(rdev);
1207                 cfg80211_mlme_purge_registrations(dev->ieee80211_ptr);
1208
1209                 memset(&dev->ieee80211_ptr->u, 0,
1210                        sizeof(dev->ieee80211_ptr->u));
1211                 memset(&dev->ieee80211_ptr->links, 0,
1212                        sizeof(dev->ieee80211_ptr->links));
1213         }
1214
1215         err = rdev_change_virtual_intf(rdev, dev, ntype, params);
1216
1217         WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
1218
1219         if (!err && params && params->use_4addr != -1)
1220                 dev->ieee80211_ptr->use_4addr = params->use_4addr;
1221
1222         if (!err) {
1223                 dev->priv_flags &= ~IFF_DONT_BRIDGE;
1224                 switch (ntype) {
1225                 case NL80211_IFTYPE_STATION:
1226                         if (dev->ieee80211_ptr->use_4addr)
1227                                 break;
1228                         fallthrough;
1229                 case NL80211_IFTYPE_OCB:
1230                 case NL80211_IFTYPE_P2P_CLIENT:
1231                 case NL80211_IFTYPE_ADHOC:
1232                         dev->priv_flags |= IFF_DONT_BRIDGE;
1233                         break;
1234                 case NL80211_IFTYPE_P2P_GO:
1235                 case NL80211_IFTYPE_AP:
1236                 case NL80211_IFTYPE_AP_VLAN:
1237                 case NL80211_IFTYPE_MESH_POINT:
1238                         /* bridging OK */
1239                         break;
1240                 case NL80211_IFTYPE_MONITOR:
1241                         /* monitor can't bridge anyway */
1242                         break;
1243                 case NL80211_IFTYPE_UNSPECIFIED:
1244                 case NUM_NL80211_IFTYPES:
1245                         /* not happening */
1246                         break;
1247                 case NL80211_IFTYPE_P2P_DEVICE:
1248                 case NL80211_IFTYPE_WDS:
1249                 case NL80211_IFTYPE_NAN:
1250                         WARN_ON(1);
1251                         break;
1252                 }
1253         }
1254
1255         if (!err && ntype != otype && netif_running(dev)) {
1256                 cfg80211_update_iface_num(rdev, ntype, 1);
1257                 cfg80211_update_iface_num(rdev, otype, -1);
1258         }
1259
1260         return err;
1261 }
1262
1263 static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
1264 {
1265         int modulation, streams, bitrate;
1266
1267         /* the formula below does only work for MCS values smaller than 32 */
1268         if (WARN_ON_ONCE(rate->mcs >= 32))
1269                 return 0;
1270
1271         modulation = rate->mcs & 7;
1272         streams = (rate->mcs >> 3) + 1;
1273
1274         bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1275
1276         if (modulation < 4)
1277                 bitrate *= (modulation + 1);
1278         else if (modulation == 4)
1279                 bitrate *= (modulation + 2);
1280         else
1281                 bitrate *= (modulation + 3);
1282
1283         bitrate *= streams;
1284
1285         if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1286                 bitrate = (bitrate / 9) * 10;
1287
1288         /* do NOT round down here */
1289         return (bitrate + 50000) / 100000;
1290 }
1291
1292 static u32 cfg80211_calculate_bitrate_dmg(struct rate_info *rate)
1293 {
1294         static const u32 __mcs2bitrate[] = {
1295                 /* control PHY */
1296                 [0] =   275,
1297                 /* SC PHY */
1298                 [1] =  3850,
1299                 [2] =  7700,
1300                 [3] =  9625,
1301                 [4] = 11550,
1302                 [5] = 12512, /* 1251.25 mbps */
1303                 [6] = 15400,
1304                 [7] = 19250,
1305                 [8] = 23100,
1306                 [9] = 25025,
1307                 [10] = 30800,
1308                 [11] = 38500,
1309                 [12] = 46200,
1310                 /* OFDM PHY */
1311                 [13] =  6930,
1312                 [14] =  8662, /* 866.25 mbps */
1313                 [15] = 13860,
1314                 [16] = 17325,
1315                 [17] = 20790,
1316                 [18] = 27720,
1317                 [19] = 34650,
1318                 [20] = 41580,
1319                 [21] = 45045,
1320                 [22] = 51975,
1321                 [23] = 62370,
1322                 [24] = 67568, /* 6756.75 mbps */
1323                 /* LP-SC PHY */
1324                 [25] =  6260,
1325                 [26] =  8340,
1326                 [27] = 11120,
1327                 [28] = 12510,
1328                 [29] = 16680,
1329                 [30] = 22240,
1330                 [31] = 25030,
1331         };
1332
1333         if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1334                 return 0;
1335
1336         return __mcs2bitrate[rate->mcs];
1337 }
1338
1339 static u32 cfg80211_calculate_bitrate_extended_sc_dmg(struct rate_info *rate)
1340 {
1341         static const u32 __mcs2bitrate[] = {
1342                 [6 - 6] = 26950, /* MCS 9.1 : 2695.0 mbps */
1343                 [7 - 6] = 50050, /* MCS 12.1 */
1344                 [8 - 6] = 53900,
1345                 [9 - 6] = 57750,
1346                 [10 - 6] = 63900,
1347                 [11 - 6] = 75075,
1348                 [12 - 6] = 80850,
1349         };
1350
1351         /* Extended SC MCS not defined for base MCS below 6 or above 12 */
1352         if (WARN_ON_ONCE(rate->mcs < 6 || rate->mcs > 12))
1353                 return 0;
1354
1355         return __mcs2bitrate[rate->mcs - 6];
1356 }
1357
1358 static u32 cfg80211_calculate_bitrate_edmg(struct rate_info *rate)
1359 {
1360         static const u32 __mcs2bitrate[] = {
1361                 /* control PHY */
1362                 [0] =   275,
1363                 /* SC PHY */
1364                 [1] =  3850,
1365                 [2] =  7700,
1366                 [3] =  9625,
1367                 [4] = 11550,
1368                 [5] = 12512, /* 1251.25 mbps */
1369                 [6] = 13475,
1370                 [7] = 15400,
1371                 [8] = 19250,
1372                 [9] = 23100,
1373                 [10] = 25025,
1374                 [11] = 26950,
1375                 [12] = 30800,
1376                 [13] = 38500,
1377                 [14] = 46200,
1378                 [15] = 50050,
1379                 [16] = 53900,
1380                 [17] = 57750,
1381                 [18] = 69300,
1382                 [19] = 75075,
1383                 [20] = 80850,
1384         };
1385
1386         if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1387                 return 0;
1388
1389         return __mcs2bitrate[rate->mcs] * rate->n_bonded_ch;
1390 }
1391
1392 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1393 {
1394         static const u32 base[4][12] = {
1395                 {   6500000,
1396                    13000000,
1397                    19500000,
1398                    26000000,
1399                    39000000,
1400                    52000000,
1401                    58500000,
1402                    65000000,
1403                    78000000,
1404                 /* not in the spec, but some devices use this: */
1405                    86700000,
1406                    97500000,
1407                   108300000,
1408                 },
1409                 {  13500000,
1410                    27000000,
1411                    40500000,
1412                    54000000,
1413                    81000000,
1414                   108000000,
1415                   121500000,
1416                   135000000,
1417                   162000000,
1418                   180000000,
1419                   202500000,
1420                   225000000,
1421                 },
1422                 {  29300000,
1423                    58500000,
1424                    87800000,
1425                   117000000,
1426                   175500000,
1427                   234000000,
1428                   263300000,
1429                   292500000,
1430                   351000000,
1431                   390000000,
1432                   438800000,
1433                   487500000,
1434                 },
1435                 {  58500000,
1436                   117000000,
1437                   175500000,
1438                   234000000,
1439                   351000000,
1440                   468000000,
1441                   526500000,
1442                   585000000,
1443                   702000000,
1444                   780000000,
1445                   877500000,
1446                   975000000,
1447                 },
1448         };
1449         u32 bitrate;
1450         int idx;
1451
1452         if (rate->mcs > 11)
1453                 goto warn;
1454
1455         switch (rate->bw) {
1456         case RATE_INFO_BW_160:
1457                 idx = 3;
1458                 break;
1459         case RATE_INFO_BW_80:
1460                 idx = 2;
1461                 break;
1462         case RATE_INFO_BW_40:
1463                 idx = 1;
1464                 break;
1465         case RATE_INFO_BW_5:
1466         case RATE_INFO_BW_10:
1467         default:
1468                 goto warn;
1469         case RATE_INFO_BW_20:
1470                 idx = 0;
1471         }
1472
1473         bitrate = base[idx][rate->mcs];
1474         bitrate *= rate->nss;
1475
1476         if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1477                 bitrate = (bitrate / 9) * 10;
1478
1479         /* do NOT round down here */
1480         return (bitrate + 50000) / 100000;
1481  warn:
1482         WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1483                   rate->bw, rate->mcs, rate->nss);
1484         return 0;
1485 }
1486
1487 static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate)
1488 {
1489 #define SCALE 6144
1490         u32 mcs_divisors[14] = {
1491                 102399, /* 16.666666... */
1492                  51201, /*  8.333333... */
1493                  34134, /*  5.555555... */
1494                  25599, /*  4.166666... */
1495                  17067, /*  2.777777... */
1496                  12801, /*  2.083333... */
1497                  11377, /*  1.851725... */
1498                  10239, /*  1.666666... */
1499                   8532, /*  1.388888... */
1500                   7680, /*  1.250000... */
1501                   6828, /*  1.111111... */
1502                   6144, /*  1.000000... */
1503                   5690, /*  0.926106... */
1504                   5120, /*  0.833333... */
1505         };
1506         u32 rates_160M[3] = { 960777777, 907400000, 816666666 };
1507         u32 rates_969[3] =  { 480388888, 453700000, 408333333 };
1508         u32 rates_484[3] =  { 229411111, 216666666, 195000000 };
1509         u32 rates_242[3] =  { 114711111, 108333333,  97500000 };
1510         u32 rates_106[3] =  {  40000000,  37777777,  34000000 };
1511         u32 rates_52[3]  =  {  18820000,  17777777,  16000000 };
1512         u32 rates_26[3]  =  {   9411111,   8888888,   8000000 };
1513         u64 tmp;
1514         u32 result;
1515
1516         if (WARN_ON_ONCE(rate->mcs > 13))
1517                 return 0;
1518
1519         if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2))
1520                 return 0;
1521         if (WARN_ON_ONCE(rate->he_ru_alloc >
1522                          NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1523                 return 0;
1524         if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1525                 return 0;
1526
1527         if (rate->bw == RATE_INFO_BW_160)
1528                 result = rates_160M[rate->he_gi];
1529         else if (rate->bw == RATE_INFO_BW_80 ||
1530                  (rate->bw == RATE_INFO_BW_HE_RU &&
1531                   rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
1532                 result = rates_969[rate->he_gi];
1533         else if (rate->bw == RATE_INFO_BW_40 ||
1534                  (rate->bw == RATE_INFO_BW_HE_RU &&
1535                   rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
1536                 result = rates_484[rate->he_gi];
1537         else if (rate->bw == RATE_INFO_BW_20 ||
1538                  (rate->bw == RATE_INFO_BW_HE_RU &&
1539                   rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
1540                 result = rates_242[rate->he_gi];
1541         else if (rate->bw == RATE_INFO_BW_HE_RU &&
1542                  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
1543                 result = rates_106[rate->he_gi];
1544         else if (rate->bw == RATE_INFO_BW_HE_RU &&
1545                  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
1546                 result = rates_52[rate->he_gi];
1547         else if (rate->bw == RATE_INFO_BW_HE_RU &&
1548                  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
1549                 result = rates_26[rate->he_gi];
1550         else {
1551                 WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1552                      rate->bw, rate->he_ru_alloc);
1553                 return 0;
1554         }
1555
1556         /* now scale to the appropriate MCS */
1557         tmp = result;
1558         tmp *= SCALE;
1559         do_div(tmp, mcs_divisors[rate->mcs]);
1560         result = tmp;
1561
1562         /* and take NSS, DCM into account */
1563         result = (result * rate->nss) / 8;
1564         if (rate->he_dcm)
1565                 result /= 2;
1566
1567         return result / 10000;
1568 }
1569
1570 static u32 cfg80211_calculate_bitrate_eht(struct rate_info *rate)
1571 {
1572 #define SCALE 6144
1573         static const u32 mcs_divisors[16] = {
1574                 102399, /* 16.666666... */
1575                  51201, /*  8.333333... */
1576                  34134, /*  5.555555... */
1577                  25599, /*  4.166666... */
1578                  17067, /*  2.777777... */
1579                  12801, /*  2.083333... */
1580                  11377, /*  1.851725... */
1581                  10239, /*  1.666666... */
1582                   8532, /*  1.388888... */
1583                   7680, /*  1.250000... */
1584                   6828, /*  1.111111... */
1585                   6144, /*  1.000000... */
1586                   5690, /*  0.926106... */
1587                   5120, /*  0.833333... */
1588                 409600, /* 66.666666... */
1589                 204800, /* 33.333333... */
1590         };
1591         static const u32 rates_996[3] =  { 480388888, 453700000, 408333333 };
1592         static const u32 rates_484[3] =  { 229411111, 216666666, 195000000 };
1593         static const u32 rates_242[3] =  { 114711111, 108333333,  97500000 };
1594         static const u32 rates_106[3] =  {  40000000,  37777777,  34000000 };
1595         static const u32 rates_52[3]  =  {  18820000,  17777777,  16000000 };
1596         static const u32 rates_26[3]  =  {   9411111,   8888888,   8000000 };
1597         u64 tmp;
1598         u32 result;
1599
1600         if (WARN_ON_ONCE(rate->mcs > 15))
1601                 return 0;
1602         if (WARN_ON_ONCE(rate->eht_gi > NL80211_RATE_INFO_EHT_GI_3_2))
1603                 return 0;
1604         if (WARN_ON_ONCE(rate->eht_ru_alloc >
1605                          NL80211_RATE_INFO_EHT_RU_ALLOC_4x996))
1606                 return 0;
1607         if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1608                 return 0;
1609
1610         /* Bandwidth checks for MCS 14 */
1611         if (rate->mcs == 14) {
1612                 if ((rate->bw != RATE_INFO_BW_EHT_RU &&
1613                      rate->bw != RATE_INFO_BW_80 &&
1614                      rate->bw != RATE_INFO_BW_160 &&
1615                      rate->bw != RATE_INFO_BW_320) ||
1616                     (rate->bw == RATE_INFO_BW_EHT_RU &&
1617                      rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_996 &&
1618                      rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_2x996 &&
1619                      rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_4x996)) {
1620                         WARN(1, "invalid EHT BW for MCS 14: bw:%d, ru:%d\n",
1621                              rate->bw, rate->eht_ru_alloc);
1622                         return 0;
1623                 }
1624         }
1625
1626         if (rate->bw == RATE_INFO_BW_320 ||
1627             (rate->bw == RATE_INFO_BW_EHT_RU &&
1628              rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_4x996))
1629                 result = 4 * rates_996[rate->eht_gi];
1630         else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1631                  rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_3x996P484)
1632                 result = 3 * rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1633         else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1634                  rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_3x996)
1635                 result = 3 * rates_996[rate->eht_gi];
1636         else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1637                  rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_2x996P484)
1638                 result = 2 * rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1639         else if (rate->bw == RATE_INFO_BW_160 ||
1640                  (rate->bw == RATE_INFO_BW_EHT_RU &&
1641                   rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_2x996))
1642                 result = 2 * rates_996[rate->eht_gi];
1643         else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1644                  rate->eht_ru_alloc ==
1645                  NL80211_RATE_INFO_EHT_RU_ALLOC_996P484P242)
1646                 result = rates_996[rate->eht_gi] + rates_484[rate->eht_gi]
1647                          + rates_242[rate->eht_gi];
1648         else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1649                  rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_996P484)
1650                 result = rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1651         else if (rate->bw == RATE_INFO_BW_80 ||
1652                  (rate->bw == RATE_INFO_BW_EHT_RU &&
1653                   rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_996))
1654                 result = rates_996[rate->eht_gi];
1655         else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1656                  rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_484P242)
1657                 result = rates_484[rate->eht_gi] + rates_242[rate->eht_gi];
1658         else if (rate->bw == RATE_INFO_BW_40 ||
1659                  (rate->bw == RATE_INFO_BW_EHT_RU &&
1660                   rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_484))
1661                 result = rates_484[rate->eht_gi];
1662         else if (rate->bw == RATE_INFO_BW_20 ||
1663                  (rate->bw == RATE_INFO_BW_EHT_RU &&
1664                   rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_242))
1665                 result = rates_242[rate->eht_gi];
1666         else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1667                  rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_106P26)
1668                 result = rates_106[rate->eht_gi] + rates_26[rate->eht_gi];
1669         else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1670                  rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_106)
1671                 result = rates_106[rate->eht_gi];
1672         else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1673                  rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_52P26)
1674                 result = rates_52[rate->eht_gi] + rates_26[rate->eht_gi];
1675         else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1676                  rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_52)
1677                 result = rates_52[rate->eht_gi];
1678         else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1679                  rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_26)
1680                 result = rates_26[rate->eht_gi];
1681         else {
1682                 WARN(1, "invalid EHT MCS: bw:%d, ru:%d\n",
1683                      rate->bw, rate->eht_ru_alloc);
1684                 return 0;
1685         }
1686
1687         /* now scale to the appropriate MCS */
1688         tmp = result;
1689         tmp *= SCALE;
1690         do_div(tmp, mcs_divisors[rate->mcs]);
1691
1692         /* and take NSS */
1693         tmp *= rate->nss;
1694         do_div(tmp, 8);
1695
1696         result = tmp;
1697
1698         return result / 10000;
1699 }
1700
1701 static u32 cfg80211_calculate_bitrate_s1g(struct rate_info *rate)
1702 {
1703         /* For 1, 2, 4, 8 and 16 MHz channels */
1704         static const u32 base[5][11] = {
1705                 {  300000,
1706                    600000,
1707                    900000,
1708                   1200000,
1709                   1800000,
1710                   2400000,
1711                   2700000,
1712                   3000000,
1713                   3600000,
1714                   4000000,
1715                   /* MCS 10 supported in 1 MHz only */
1716                   150000,
1717                 },
1718                 {  650000,
1719                   1300000,
1720                   1950000,
1721                   2600000,
1722                   3900000,
1723                   5200000,
1724                   5850000,
1725                   6500000,
1726                   7800000,
1727                   /* MCS 9 not valid */
1728                 },
1729                 {  1350000,
1730                    2700000,
1731                    4050000,
1732                    5400000,
1733                    8100000,
1734                   10800000,
1735                   12150000,
1736                   13500000,
1737                   16200000,
1738                   18000000,
1739                 },
1740                 {  2925000,
1741                    5850000,
1742                    8775000,
1743                   11700000,
1744                   17550000,
1745                   23400000,
1746                   26325000,
1747                   29250000,
1748                   35100000,
1749                   39000000,
1750                 },
1751                 {  8580000,
1752                   11700000,
1753                   17550000,
1754                   23400000,
1755                   35100000,
1756                   46800000,
1757                   52650000,
1758                   58500000,
1759                   70200000,
1760                   78000000,
1761                 },
1762         };
1763         u32 bitrate;
1764         /* default is 1 MHz index */
1765         int idx = 0;
1766
1767         if (rate->mcs >= 11)
1768                 goto warn;
1769
1770         switch (rate->bw) {
1771         case RATE_INFO_BW_16:
1772                 idx = 4;
1773                 break;
1774         case RATE_INFO_BW_8:
1775                 idx = 3;
1776                 break;
1777         case RATE_INFO_BW_4:
1778                 idx = 2;
1779                 break;
1780         case RATE_INFO_BW_2:
1781                 idx = 1;
1782                 break;
1783         case RATE_INFO_BW_1:
1784                 idx = 0;
1785                 break;
1786         case RATE_INFO_BW_5:
1787         case RATE_INFO_BW_10:
1788         case RATE_INFO_BW_20:
1789         case RATE_INFO_BW_40:
1790         case RATE_INFO_BW_80:
1791         case RATE_INFO_BW_160:
1792         default:
1793                 goto warn;
1794         }
1795
1796         bitrate = base[idx][rate->mcs];
1797         bitrate *= rate->nss;
1798
1799         if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1800                 bitrate = (bitrate / 9) * 10;
1801         /* do NOT round down here */
1802         return (bitrate + 50000) / 100000;
1803 warn:
1804         WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1805                   rate->bw, rate->mcs, rate->nss);
1806         return 0;
1807 }
1808
1809 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1810 {
1811         if (rate->flags & RATE_INFO_FLAGS_MCS)
1812                 return cfg80211_calculate_bitrate_ht(rate);
1813         if (rate->flags & RATE_INFO_FLAGS_DMG)
1814                 return cfg80211_calculate_bitrate_dmg(rate);
1815         if (rate->flags & RATE_INFO_FLAGS_EXTENDED_SC_DMG)
1816                 return cfg80211_calculate_bitrate_extended_sc_dmg(rate);
1817         if (rate->flags & RATE_INFO_FLAGS_EDMG)
1818                 return cfg80211_calculate_bitrate_edmg(rate);
1819         if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1820                 return cfg80211_calculate_bitrate_vht(rate);
1821         if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1822                 return cfg80211_calculate_bitrate_he(rate);
1823         if (rate->flags & RATE_INFO_FLAGS_EHT_MCS)
1824                 return cfg80211_calculate_bitrate_eht(rate);
1825         if (rate->flags & RATE_INFO_FLAGS_S1G_MCS)
1826                 return cfg80211_calculate_bitrate_s1g(rate);
1827
1828         return rate->legacy;
1829 }
1830 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1831
1832 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1833                           enum ieee80211_p2p_attr_id attr,
1834                           u8 *buf, unsigned int bufsize)
1835 {
1836         u8 *out = buf;
1837         u16 attr_remaining = 0;
1838         bool desired_attr = false;
1839         u16 desired_len = 0;
1840
1841         while (len > 0) {
1842                 unsigned int iedatalen;
1843                 unsigned int copy;
1844                 const u8 *iedata;
1845
1846                 if (len < 2)
1847                         return -EILSEQ;
1848                 iedatalen = ies[1];
1849                 if (iedatalen + 2 > len)
1850                         return -EILSEQ;
1851
1852                 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1853                         goto cont;
1854
1855                 if (iedatalen < 4)
1856                         goto cont;
1857
1858                 iedata = ies + 2;
1859
1860                 /* check WFA OUI, P2P subtype */
1861                 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1862                     iedata[2] != 0x9a || iedata[3] != 0x09)
1863                         goto cont;
1864
1865                 iedatalen -= 4;
1866                 iedata += 4;
1867
1868                 /* check attribute continuation into this IE */
1869                 copy = min_t(unsigned int, attr_remaining, iedatalen);
1870                 if (copy && desired_attr) {
1871                         desired_len += copy;
1872                         if (out) {
1873                                 memcpy(out, iedata, min(bufsize, copy));
1874                                 out += min(bufsize, copy);
1875                                 bufsize -= min(bufsize, copy);
1876                         }
1877
1878
1879                         if (copy == attr_remaining)
1880                                 return desired_len;
1881                 }
1882
1883                 attr_remaining -= copy;
1884                 if (attr_remaining)
1885                         goto cont;
1886
1887                 iedatalen -= copy;
1888                 iedata += copy;
1889
1890                 while (iedatalen > 0) {
1891                         u16 attr_len;
1892
1893                         /* P2P attribute ID & size must fit */
1894                         if (iedatalen < 3)
1895                                 return -EILSEQ;
1896                         desired_attr = iedata[0] == attr;
1897                         attr_len = get_unaligned_le16(iedata + 1);
1898                         iedatalen -= 3;
1899                         iedata += 3;
1900
1901                         copy = min_t(unsigned int, attr_len, iedatalen);
1902
1903                         if (desired_attr) {
1904                                 desired_len += copy;
1905                                 if (out) {
1906                                         memcpy(out, iedata, min(bufsize, copy));
1907                                         out += min(bufsize, copy);
1908                                         bufsize -= min(bufsize, copy);
1909                                 }
1910
1911                                 if (copy == attr_len)
1912                                         return desired_len;
1913                         }
1914
1915                         iedata += copy;
1916                         iedatalen -= copy;
1917                         attr_remaining = attr_len - copy;
1918                 }
1919
1920  cont:
1921                 len -= ies[1] + 2;
1922                 ies += ies[1] + 2;
1923         }
1924
1925         if (attr_remaining && desired_attr)
1926                 return -EILSEQ;
1927
1928         return -ENOENT;
1929 }
1930 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1931
1932 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
1933 {
1934         int i;
1935
1936         /* Make sure array values are legal */
1937         if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1938                 return false;
1939
1940         i = 0;
1941         while (i < n_ids) {
1942                 if (ids[i] == WLAN_EID_EXTENSION) {
1943                         if (id_ext && (ids[i + 1] == id))
1944                                 return true;
1945
1946                         i += 2;
1947                         continue;
1948                 }
1949
1950                 if (ids[i] == id && !id_ext)
1951                         return true;
1952
1953                 i++;
1954         }
1955         return false;
1956 }
1957
1958 static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1959 {
1960         /* we assume a validly formed IEs buffer */
1961         u8 len = ies[pos + 1];
1962
1963         pos += 2 + len;
1964
1965         /* the IE itself must have 255 bytes for fragments to follow */
1966         if (len < 255)
1967                 return pos;
1968
1969         while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1970                 len = ies[pos + 1];
1971                 pos += 2 + len;
1972         }
1973
1974         return pos;
1975 }
1976
1977 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1978                               const u8 *ids, int n_ids,
1979                               const u8 *after_ric, int n_after_ric,
1980                               size_t offset)
1981 {
1982         size_t pos = offset;
1983
1984         while (pos < ielen) {
1985                 u8 ext = 0;
1986
1987                 if (ies[pos] == WLAN_EID_EXTENSION)
1988                         ext = 2;
1989                 if ((pos + ext) >= ielen)
1990                         break;
1991
1992                 if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1993                                           ies[pos] == WLAN_EID_EXTENSION))
1994                         break;
1995
1996                 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1997                         pos = skip_ie(ies, ielen, pos);
1998
1999                         while (pos < ielen) {
2000                                 if (ies[pos] == WLAN_EID_EXTENSION)
2001                                         ext = 2;
2002                                 else
2003                                         ext = 0;
2004
2005                                 if ((pos + ext) >= ielen)
2006                                         break;
2007
2008                                 if (!ieee80211_id_in_list(after_ric,
2009                                                           n_after_ric,
2010                                                           ies[pos + ext],
2011                                                           ext == 2))
2012                                         pos = skip_ie(ies, ielen, pos);
2013                                 else
2014                                         break;
2015                         }
2016                 } else {
2017                         pos = skip_ie(ies, ielen, pos);
2018                 }
2019         }
2020
2021         return pos;
2022 }
2023 EXPORT_SYMBOL(ieee80211_ie_split_ric);
2024
2025 void ieee80211_fragment_element(struct sk_buff *skb, u8 *len_pos, u8 frag_id)
2026 {
2027         unsigned int elem_len;
2028
2029         if (!len_pos)
2030                 return;
2031
2032         elem_len = skb->data + skb->len - len_pos - 1;
2033
2034         while (elem_len > 255) {
2035                 /* this one is 255 */
2036                 *len_pos = 255;
2037                 /* remaining data gets smaller */
2038                 elem_len -= 255;
2039                 /* make space for the fragment ID/len in SKB */
2040                 skb_put(skb, 2);
2041                 /* shift back the remaining data to place fragment ID/len */
2042                 memmove(len_pos + 255 + 3, len_pos + 255 + 1, elem_len);
2043                 /* place the fragment ID */
2044                 len_pos += 255 + 1;
2045                 *len_pos = frag_id;
2046                 /* and point to fragment length to update later */
2047                 len_pos++;
2048         }
2049
2050         *len_pos = elem_len;
2051 }
2052 EXPORT_SYMBOL(ieee80211_fragment_element);
2053
2054 bool ieee80211_operating_class_to_band(u8 operating_class,
2055                                        enum nl80211_band *band)
2056 {
2057         switch (operating_class) {
2058         case 112:
2059         case 115 ... 127:
2060         case 128 ... 130:
2061                 *band = NL80211_BAND_5GHZ;
2062                 return true;
2063         case 131 ... 135:
2064         case 137:
2065                 *band = NL80211_BAND_6GHZ;
2066                 return true;
2067         case 81:
2068         case 82:
2069         case 83:
2070         case 84:
2071                 *band = NL80211_BAND_2GHZ;
2072                 return true;
2073         case 180:
2074                 *band = NL80211_BAND_60GHZ;
2075                 return true;
2076         }
2077
2078         return false;
2079 }
2080 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
2081
2082 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
2083                                           u8 *op_class)
2084 {
2085         u8 vht_opclass;
2086         u32 freq = chandef->center_freq1;
2087
2088         if (freq >= 2412 && freq <= 2472) {
2089                 if (chandef->width > NL80211_CHAN_WIDTH_40)
2090                         return false;
2091
2092                 /* 2.407 GHz, channels 1..13 */
2093                 if (chandef->width == NL80211_CHAN_WIDTH_40) {
2094                         if (freq > chandef->chan->center_freq)
2095                                 *op_class = 83; /* HT40+ */
2096                         else
2097                                 *op_class = 84; /* HT40- */
2098                 } else {
2099                         *op_class = 81;
2100                 }
2101
2102                 return true;
2103         }
2104
2105         if (freq == 2484) {
2106                 /* channel 14 is only for IEEE 802.11b */
2107                 if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
2108                         return false;
2109
2110                 *op_class = 82; /* channel 14 */
2111                 return true;
2112         }
2113
2114         switch (chandef->width) {
2115         case NL80211_CHAN_WIDTH_80:
2116                 vht_opclass = 128;
2117                 break;
2118         case NL80211_CHAN_WIDTH_160:
2119                 vht_opclass = 129;
2120                 break;
2121         case NL80211_CHAN_WIDTH_80P80:
2122                 vht_opclass = 130;
2123                 break;
2124         case NL80211_CHAN_WIDTH_10:
2125         case NL80211_CHAN_WIDTH_5:
2126                 return false; /* unsupported for now */
2127         default:
2128                 vht_opclass = 0;
2129                 break;
2130         }
2131
2132         /* 5 GHz, channels 36..48 */
2133         if (freq >= 5180 && freq <= 5240) {
2134                 if (vht_opclass) {
2135                         *op_class = vht_opclass;
2136                 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2137                         if (freq > chandef->chan->center_freq)
2138                                 *op_class = 116;
2139                         else
2140                                 *op_class = 117;
2141                 } else {
2142                         *op_class = 115;
2143                 }
2144
2145                 return true;
2146         }
2147
2148         /* 5 GHz, channels 52..64 */
2149         if (freq >= 5260 && freq <= 5320) {
2150                 if (vht_opclass) {
2151                         *op_class = vht_opclass;
2152                 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2153                         if (freq > chandef->chan->center_freq)
2154                                 *op_class = 119;
2155                         else
2156                                 *op_class = 120;
2157                 } else {
2158                         *op_class = 118;
2159                 }
2160
2161                 return true;
2162         }
2163
2164         /* 5 GHz, channels 100..144 */
2165         if (freq >= 5500 && freq <= 5720) {
2166                 if (vht_opclass) {
2167                         *op_class = vht_opclass;
2168                 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2169                         if (freq > chandef->chan->center_freq)
2170                                 *op_class = 122;
2171                         else
2172                                 *op_class = 123;
2173                 } else {
2174                         *op_class = 121;
2175                 }
2176
2177                 return true;
2178         }
2179
2180         /* 5 GHz, channels 149..169 */
2181         if (freq >= 5745 && freq <= 5845) {
2182                 if (vht_opclass) {
2183                         *op_class = vht_opclass;
2184                 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2185                         if (freq > chandef->chan->center_freq)
2186                                 *op_class = 126;
2187                         else
2188                                 *op_class = 127;
2189                 } else if (freq <= 5805) {
2190                         *op_class = 124;
2191                 } else {
2192                         *op_class = 125;
2193                 }
2194
2195                 return true;
2196         }
2197
2198         /* 56.16 GHz, channel 1..4 */
2199         if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
2200                 if (chandef->width >= NL80211_CHAN_WIDTH_40)
2201                         return false;
2202
2203                 *op_class = 180;
2204                 return true;
2205         }
2206
2207         /* not supported yet */
2208         return false;
2209 }
2210 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
2211
2212 static int cfg80211_wdev_bi(struct wireless_dev *wdev)
2213 {
2214         switch (wdev->iftype) {
2215         case NL80211_IFTYPE_AP:
2216         case NL80211_IFTYPE_P2P_GO:
2217                 WARN_ON(wdev->valid_links);
2218                 return wdev->links[0].ap.beacon_interval;
2219         case NL80211_IFTYPE_MESH_POINT:
2220                 return wdev->u.mesh.beacon_interval;
2221         case NL80211_IFTYPE_ADHOC:
2222                 return wdev->u.ibss.beacon_interval;
2223         default:
2224                 break;
2225         }
2226
2227         return 0;
2228 }
2229
2230 static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
2231                                        u32 *beacon_int_gcd,
2232                                        bool *beacon_int_different)
2233 {
2234         struct wireless_dev *wdev;
2235
2236         *beacon_int_gcd = 0;
2237         *beacon_int_different = false;
2238
2239         list_for_each_entry(wdev, &wiphy->wdev_list, list) {
2240                 int wdev_bi;
2241
2242                 /* this feature isn't supported with MLO */
2243                 if (wdev->valid_links)
2244                         continue;
2245
2246                 wdev_bi = cfg80211_wdev_bi(wdev);
2247
2248                 if (!wdev_bi)
2249                         continue;
2250
2251                 if (!*beacon_int_gcd) {
2252                         *beacon_int_gcd = wdev_bi;
2253                         continue;
2254                 }
2255
2256                 if (wdev_bi == *beacon_int_gcd)
2257                         continue;
2258
2259                 *beacon_int_different = true;
2260                 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev_bi);
2261         }
2262
2263         if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
2264                 if (*beacon_int_gcd)
2265                         *beacon_int_different = true;
2266                 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
2267         }
2268 }
2269
2270 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
2271                                  enum nl80211_iftype iftype, u32 beacon_int)
2272 {
2273         /*
2274          * This is just a basic pre-condition check; if interface combinations
2275          * are possible the driver must already be checking those with a call
2276          * to cfg80211_check_combinations(), in which case we'll validate more
2277          * through the cfg80211_calculate_bi_data() call and code in
2278          * cfg80211_iter_combinations().
2279          */
2280
2281         if (beacon_int < 10 || beacon_int > 10000)
2282                 return -EINVAL;
2283
2284         return 0;
2285 }
2286
2287 int cfg80211_iter_combinations(struct wiphy *wiphy,
2288                                struct iface_combination_params *params,
2289                                void (*iter)(const struct ieee80211_iface_combination *c,
2290                                             void *data),
2291                                void *data)
2292 {
2293         const struct ieee80211_regdomain *regdom;
2294         enum nl80211_dfs_regions region = 0;
2295         int i, j, iftype;
2296         int num_interfaces = 0;
2297         u32 used_iftypes = 0;
2298         u32 beacon_int_gcd;
2299         bool beacon_int_different;
2300
2301         /*
2302          * This is a bit strange, since the iteration used to rely only on
2303          * the data given by the driver, but here it now relies on context,
2304          * in form of the currently operating interfaces.
2305          * This is OK for all current users, and saves us from having to
2306          * push the GCD calculations into all the drivers.
2307          * In the future, this should probably rely more on data that's in
2308          * cfg80211 already - the only thing not would appear to be any new
2309          * interfaces (while being brought up) and channel/radar data.
2310          */
2311         cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
2312                                    &beacon_int_gcd, &beacon_int_different);
2313
2314         if (params->radar_detect) {
2315                 rcu_read_lock();
2316                 regdom = rcu_dereference(cfg80211_regdomain);
2317                 if (regdom)
2318                         region = regdom->dfs_region;
2319                 rcu_read_unlock();
2320         }
2321
2322         for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
2323                 num_interfaces += params->iftype_num[iftype];
2324                 if (params->iftype_num[iftype] > 0 &&
2325                     !cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
2326                         used_iftypes |= BIT(iftype);
2327         }
2328
2329         for (i = 0; i < wiphy->n_iface_combinations; i++) {
2330                 const struct ieee80211_iface_combination *c;
2331                 struct ieee80211_iface_limit *limits;
2332                 u32 all_iftypes = 0;
2333
2334                 c = &wiphy->iface_combinations[i];
2335
2336                 if (num_interfaces > c->max_interfaces)
2337                         continue;
2338                 if (params->num_different_channels > c->num_different_channels)
2339                         continue;
2340
2341                 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
2342                                  GFP_KERNEL);
2343                 if (!limits)
2344                         return -ENOMEM;
2345
2346                 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
2347                         if (cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
2348                                 continue;
2349                         for (j = 0; j < c->n_limits; j++) {
2350                                 all_iftypes |= limits[j].types;
2351                                 if (!(limits[j].types & BIT(iftype)))
2352                                         continue;
2353                                 if (limits[j].max < params->iftype_num[iftype])
2354                                         goto cont;
2355                                 limits[j].max -= params->iftype_num[iftype];
2356                         }
2357                 }
2358
2359                 if (params->radar_detect !=
2360                         (c->radar_detect_widths & params->radar_detect))
2361                         goto cont;
2362
2363                 if (params->radar_detect && c->radar_detect_regions &&
2364                     !(c->radar_detect_regions & BIT(region)))
2365                         goto cont;
2366
2367                 /* Finally check that all iftypes that we're currently
2368                  * using are actually part of this combination. If they
2369                  * aren't then we can't use this combination and have
2370                  * to continue to the next.
2371                  */
2372                 if ((all_iftypes & used_iftypes) != used_iftypes)
2373                         goto cont;
2374
2375                 if (beacon_int_gcd) {
2376                         if (c->beacon_int_min_gcd &&
2377                             beacon_int_gcd < c->beacon_int_min_gcd)
2378                                 goto cont;
2379                         if (!c->beacon_int_min_gcd && beacon_int_different)
2380                                 goto cont;
2381                 }
2382
2383                 /* This combination covered all interface types and
2384                  * supported the requested numbers, so we're good.
2385                  */
2386
2387                 (*iter)(c, data);
2388  cont:
2389                 kfree(limits);
2390         }
2391
2392         return 0;
2393 }
2394 EXPORT_SYMBOL(cfg80211_iter_combinations);
2395
2396 static void
2397 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
2398                           void *data)
2399 {
2400         int *num = data;
2401         (*num)++;
2402 }
2403
2404 int cfg80211_check_combinations(struct wiphy *wiphy,
2405                                 struct iface_combination_params *params)
2406 {
2407         int err, num = 0;
2408
2409         err = cfg80211_iter_combinations(wiphy, params,
2410                                          cfg80211_iter_sum_ifcombs, &num);
2411         if (err)
2412                 return err;
2413         if (num == 0)
2414                 return -EBUSY;
2415
2416         return 0;
2417 }
2418 EXPORT_SYMBOL(cfg80211_check_combinations);
2419
2420 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
2421                            const u8 *rates, unsigned int n_rates,
2422                            u32 *mask)
2423 {
2424         int i, j;
2425
2426         if (!sband)
2427                 return -EINVAL;
2428
2429         if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
2430                 return -EINVAL;
2431
2432         *mask = 0;
2433
2434         for (i = 0; i < n_rates; i++) {
2435                 int rate = (rates[i] & 0x7f) * 5;
2436                 bool found = false;
2437
2438                 for (j = 0; j < sband->n_bitrates; j++) {
2439                         if (sband->bitrates[j].bitrate == rate) {
2440                                 found = true;
2441                                 *mask |= BIT(j);
2442                                 break;
2443                         }
2444                 }
2445                 if (!found)
2446                         return -EINVAL;
2447         }
2448
2449         /*
2450          * mask must have at least one bit set here since we
2451          * didn't accept a 0-length rates array nor allowed
2452          * entries in the array that didn't exist
2453          */
2454
2455         return 0;
2456 }
2457
2458 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
2459 {
2460         enum nl80211_band band;
2461         unsigned int n_channels = 0;
2462
2463         for (band = 0; band < NUM_NL80211_BANDS; band++)
2464                 if (wiphy->bands[band])
2465                         n_channels += wiphy->bands[band]->n_channels;
2466
2467         return n_channels;
2468 }
2469 EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
2470
2471 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
2472                          struct station_info *sinfo)
2473 {
2474         struct cfg80211_registered_device *rdev;
2475         struct wireless_dev *wdev;
2476
2477         wdev = dev->ieee80211_ptr;
2478         if (!wdev)
2479                 return -EOPNOTSUPP;
2480
2481         rdev = wiphy_to_rdev(wdev->wiphy);
2482         if (!rdev->ops->get_station)
2483                 return -EOPNOTSUPP;
2484
2485         memset(sinfo, 0, sizeof(*sinfo));
2486
2487         return rdev_get_station(rdev, dev, mac_addr, sinfo);
2488 }
2489 EXPORT_SYMBOL(cfg80211_get_station);
2490
2491 void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
2492 {
2493         int i;
2494
2495         if (!f)
2496                 return;
2497
2498         kfree(f->serv_spec_info);
2499         kfree(f->srf_bf);
2500         kfree(f->srf_macs);
2501         for (i = 0; i < f->num_rx_filters; i++)
2502                 kfree(f->rx_filters[i].filter);
2503
2504         for (i = 0; i < f->num_tx_filters; i++)
2505                 kfree(f->tx_filters[i].filter);
2506
2507         kfree(f->rx_filters);
2508         kfree(f->tx_filters);
2509         kfree(f);
2510 }
2511 EXPORT_SYMBOL(cfg80211_free_nan_func);
2512
2513 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
2514                                 u32 center_freq_khz, u32 bw_khz)
2515 {
2516         u32 start_freq_khz, end_freq_khz;
2517
2518         start_freq_khz = center_freq_khz - (bw_khz / 2);
2519         end_freq_khz = center_freq_khz + (bw_khz / 2);
2520
2521         if (start_freq_khz >= freq_range->start_freq_khz &&
2522             end_freq_khz <= freq_range->end_freq_khz)
2523                 return true;
2524
2525         return false;
2526 }
2527
2528 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
2529 {
2530         sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
2531                                 sizeof(*(sinfo->pertid)),
2532                                 gfp);
2533         if (!sinfo->pertid)
2534                 return -ENOMEM;
2535
2536         return 0;
2537 }
2538 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
2539
2540 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
2541 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
2542 const unsigned char rfc1042_header[] __aligned(2) =
2543         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2544 EXPORT_SYMBOL(rfc1042_header);
2545
2546 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
2547 const unsigned char bridge_tunnel_header[] __aligned(2) =
2548         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
2549 EXPORT_SYMBOL(bridge_tunnel_header);
2550
2551 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
2552 struct iapp_layer2_update {
2553         u8 da[ETH_ALEN];        /* broadcast */
2554         u8 sa[ETH_ALEN];        /* STA addr */
2555         __be16 len;             /* 6 */
2556         u8 dsap;                /* 0 */
2557         u8 ssap;                /* 0 */
2558         u8 control;
2559         u8 xid_info[3];
2560 } __packed;
2561
2562 void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
2563 {
2564         struct iapp_layer2_update *msg;
2565         struct sk_buff *skb;
2566
2567         /* Send Level 2 Update Frame to update forwarding tables in layer 2
2568          * bridge devices */
2569
2570         skb = dev_alloc_skb(sizeof(*msg));
2571         if (!skb)
2572                 return;
2573         msg = skb_put(skb, sizeof(*msg));
2574
2575         /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
2576          * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
2577
2578         eth_broadcast_addr(msg->da);
2579         ether_addr_copy(msg->sa, addr);
2580         msg->len = htons(6);
2581         msg->dsap = 0;
2582         msg->ssap = 0x01;       /* NULL LSAP, CR Bit: Response */
2583         msg->control = 0xaf;    /* XID response lsb.1111F101.
2584                                  * F=0 (no poll command; unsolicited frame) */
2585         msg->xid_info[0] = 0x81;        /* XID format identifier */
2586         msg->xid_info[1] = 1;   /* LLC types/classes: Type 1 LLC */
2587         msg->xid_info[2] = 0;   /* XID sender's receive window size (RW) */
2588
2589         skb->dev = dev;
2590         skb->protocol = eth_type_trans(skb, dev);
2591         memset(skb->cb, 0, sizeof(skb->cb));
2592         netif_rx(skb);
2593 }
2594 EXPORT_SYMBOL(cfg80211_send_layer2_update);
2595
2596 int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap,
2597                               enum ieee80211_vht_chanwidth bw,
2598                               int mcs, bool ext_nss_bw_capable,
2599                               unsigned int max_vht_nss)
2600 {
2601         u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map);
2602         int ext_nss_bw;
2603         int supp_width;
2604         int i, mcs_encoding;
2605
2606         if (map == 0xffff)
2607                 return 0;
2608
2609         if (WARN_ON(mcs > 9 || max_vht_nss > 8))
2610                 return 0;
2611         if (mcs <= 7)
2612                 mcs_encoding = 0;
2613         else if (mcs == 8)
2614                 mcs_encoding = 1;
2615         else
2616                 mcs_encoding = 2;
2617
2618         if (!max_vht_nss) {
2619                 /* find max_vht_nss for the given MCS */
2620                 for (i = 7; i >= 0; i--) {
2621                         int supp = (map >> (2 * i)) & 3;
2622
2623                         if (supp == 3)
2624                                 continue;
2625
2626                         if (supp >= mcs_encoding) {
2627                                 max_vht_nss = i + 1;
2628                                 break;
2629                         }
2630                 }
2631         }
2632
2633         if (!(cap->supp_mcs.tx_mcs_map &
2634                         cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
2635                 return max_vht_nss;
2636
2637         ext_nss_bw = le32_get_bits(cap->vht_cap_info,
2638                                    IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
2639         supp_width = le32_get_bits(cap->vht_cap_info,
2640                                    IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
2641
2642         /* if not capable, treat ext_nss_bw as 0 */
2643         if (!ext_nss_bw_capable)
2644                 ext_nss_bw = 0;
2645
2646         /* This is invalid */
2647         if (supp_width == 3)
2648                 return 0;
2649
2650         /* This is an invalid combination so pretend nothing is supported */
2651         if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2))
2652                 return 0;
2653
2654         /*
2655          * Cover all the special cases according to IEEE 802.11-2016
2656          * Table 9-250. All other cases are either factor of 1 or not
2657          * valid/supported.
2658          */
2659         switch (bw) {
2660         case IEEE80211_VHT_CHANWIDTH_USE_HT:
2661         case IEEE80211_VHT_CHANWIDTH_80MHZ:
2662                 if ((supp_width == 1 || supp_width == 2) &&
2663                     ext_nss_bw == 3)
2664                         return 2 * max_vht_nss;
2665                 break;
2666         case IEEE80211_VHT_CHANWIDTH_160MHZ:
2667                 if (supp_width == 0 &&
2668                     (ext_nss_bw == 1 || ext_nss_bw == 2))
2669                         return max_vht_nss / 2;
2670                 if (supp_width == 0 &&
2671                     ext_nss_bw == 3)
2672                         return (3 * max_vht_nss) / 4;
2673                 if (supp_width == 1 &&
2674                     ext_nss_bw == 3)
2675                         return 2 * max_vht_nss;
2676                 break;
2677         case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
2678                 if (supp_width == 0 && ext_nss_bw == 1)
2679                         return 0; /* not possible */
2680                 if (supp_width == 0 &&
2681                     ext_nss_bw == 2)
2682                         return max_vht_nss / 2;
2683                 if (supp_width == 0 &&
2684                     ext_nss_bw == 3)
2685                         return (3 * max_vht_nss) / 4;
2686                 if (supp_width == 1 &&
2687                     ext_nss_bw == 0)
2688                         return 0; /* not possible */
2689                 if (supp_width == 1 &&
2690                     ext_nss_bw == 1)
2691                         return max_vht_nss / 2;
2692                 if (supp_width == 1 &&
2693                     ext_nss_bw == 2)
2694                         return (3 * max_vht_nss) / 4;
2695                 break;
2696         }
2697
2698         /* not covered or invalid combination received */
2699         return max_vht_nss;
2700 }
2701 EXPORT_SYMBOL(ieee80211_get_vht_max_nss);
2702
2703 bool cfg80211_iftype_allowed(struct wiphy *wiphy, enum nl80211_iftype iftype,
2704                              bool is_4addr, u8 check_swif)
2705
2706 {
2707         bool is_vlan = iftype == NL80211_IFTYPE_AP_VLAN;
2708
2709         switch (check_swif) {
2710         case 0:
2711                 if (is_vlan && is_4addr)
2712                         return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2713                 return wiphy->interface_modes & BIT(iftype);
2714         case 1:
2715                 if (!(wiphy->software_iftypes & BIT(iftype)) && is_vlan)
2716                         return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2717                 return wiphy->software_iftypes & BIT(iftype);
2718         default:
2719                 break;
2720         }
2721
2722         return false;
2723 }
2724 EXPORT_SYMBOL(cfg80211_iftype_allowed);
2725
2726 void cfg80211_remove_link(struct wireless_dev *wdev, unsigned int link_id)
2727 {
2728         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
2729
2730         lockdep_assert_wiphy(wdev->wiphy);
2731
2732         switch (wdev->iftype) {
2733         case NL80211_IFTYPE_AP:
2734         case NL80211_IFTYPE_P2P_GO:
2735                 cfg80211_stop_ap(rdev, wdev->netdev, link_id, true);
2736                 break;
2737         default:
2738                 /* per-link not relevant */
2739                 break;
2740         }
2741
2742         wdev->valid_links &= ~BIT(link_id);
2743
2744         rdev_del_intf_link(rdev, wdev, link_id);
2745
2746         eth_zero_addr(wdev->links[link_id].addr);
2747 }
2748
2749 void cfg80211_remove_links(struct wireless_dev *wdev)
2750 {
2751         unsigned int link_id;
2752
2753         /*
2754          * links are controlled by upper layers (userspace/cfg)
2755          * only for AP mode, so only remove them here for AP
2756          */
2757         if (wdev->iftype != NL80211_IFTYPE_AP)
2758                 return;
2759
2760         if (wdev->valid_links) {
2761                 for_each_valid_link(wdev, link_id)
2762                         cfg80211_remove_link(wdev, link_id);
2763         }
2764 }
2765
2766 int cfg80211_remove_virtual_intf(struct cfg80211_registered_device *rdev,
2767                                  struct wireless_dev *wdev)
2768 {
2769         cfg80211_remove_links(wdev);
2770
2771         return rdev_del_virtual_intf(rdev, wdev);
2772 }
2773
2774 const struct wiphy_iftype_ext_capab *
2775 cfg80211_get_iftype_ext_capa(struct wiphy *wiphy, enum nl80211_iftype type)
2776 {
2777         int i;
2778
2779         for (i = 0; i < wiphy->num_iftype_ext_capab; i++) {
2780                 if (wiphy->iftype_ext_capab[i].iftype == type)
2781                         return &wiphy->iftype_ext_capab[i];
2782         }
2783
2784         return NULL;
2785 }
2786 EXPORT_SYMBOL(cfg80211_get_iftype_ext_capa);