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