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