GNU Linux-libre 4.9.284-gnu1
[releases.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                                   bool is_amsdu)
426 {
427         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
428         struct {
429                 u8 hdr[ETH_ALEN] __aligned(2);
430                 __be16 proto;
431         } payload;
432         struct ethhdr tmp;
433         u16 hdrlen;
434         u8 mesh_flags = 0;
435
436         if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
437                 return -1;
438
439         hdrlen = ieee80211_hdrlen(hdr->frame_control);
440         if (skb->len < hdrlen + 8)
441                 return -1;
442
443         /* convert IEEE 802.11 header + possible LLC headers into Ethernet
444          * header
445          * IEEE 802.11 address fields:
446          * ToDS FromDS Addr1 Addr2 Addr3 Addr4
447          *   0     0   DA    SA    BSSID n/a
448          *   0     1   DA    BSSID SA    n/a
449          *   1     0   BSSID SA    DA    n/a
450          *   1     1   RA    TA    DA    SA
451          */
452         memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
453         memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
454
455         if (iftype == NL80211_IFTYPE_MESH_POINT)
456                 skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
457
458         mesh_flags &= MESH_FLAGS_AE;
459
460         switch (hdr->frame_control &
461                 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
462         case cpu_to_le16(IEEE80211_FCTL_TODS):
463                 if (unlikely(iftype != NL80211_IFTYPE_AP &&
464                              iftype != NL80211_IFTYPE_AP_VLAN &&
465                              iftype != NL80211_IFTYPE_P2P_GO))
466                         return -1;
467                 break;
468         case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
469                 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
470                              iftype != NL80211_IFTYPE_MESH_POINT &&
471                              iftype != NL80211_IFTYPE_AP_VLAN &&
472                              iftype != NL80211_IFTYPE_STATION))
473                         return -1;
474                 if (iftype == NL80211_IFTYPE_MESH_POINT) {
475                         if (mesh_flags == MESH_FLAGS_AE_A4)
476                                 return -1;
477                         if (mesh_flags == MESH_FLAGS_AE_A5_A6) {
478                                 skb_copy_bits(skb, hdrlen +
479                                         offsetof(struct ieee80211s_hdr, eaddr1),
480                                         tmp.h_dest, 2 * ETH_ALEN);
481                         }
482                         hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
483                 }
484                 break;
485         case cpu_to_le16(IEEE80211_FCTL_FROMDS):
486                 if ((iftype != NL80211_IFTYPE_STATION &&
487                      iftype != NL80211_IFTYPE_P2P_CLIENT &&
488                      iftype != NL80211_IFTYPE_MESH_POINT) ||
489                     (is_multicast_ether_addr(tmp.h_dest) &&
490                      ether_addr_equal(tmp.h_source, addr)))
491                         return -1;
492                 if (iftype == NL80211_IFTYPE_MESH_POINT) {
493                         if (mesh_flags == MESH_FLAGS_AE_A5_A6)
494                                 return -1;
495                         if (mesh_flags == MESH_FLAGS_AE_A4)
496                                 skb_copy_bits(skb, hdrlen +
497                                         offsetof(struct ieee80211s_hdr, eaddr1),
498                                         tmp.h_source, ETH_ALEN);
499                         hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
500                 }
501                 break;
502         case cpu_to_le16(0):
503                 if (iftype != NL80211_IFTYPE_ADHOC &&
504                     iftype != NL80211_IFTYPE_STATION &&
505                     iftype != NL80211_IFTYPE_OCB)
506                                 return -1;
507                 break;
508         }
509
510         skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
511         tmp.h_proto = payload.proto;
512
513         if (likely((!is_amsdu && ether_addr_equal(payload.hdr, rfc1042_header) &&
514                     tmp.h_proto != htons(ETH_P_AARP) &&
515                     tmp.h_proto != htons(ETH_P_IPX)) ||
516                    ether_addr_equal(payload.hdr, bridge_tunnel_header)))
517                 /* remove RFC1042 or Bridge-Tunnel encapsulation and
518                  * replace EtherType */
519                 hdrlen += ETH_ALEN + 2;
520         else
521                 tmp.h_proto = htons(skb->len - hdrlen);
522
523         pskb_pull(skb, hdrlen);
524
525         if (!ehdr)
526                 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
527         memcpy(ehdr, &tmp, sizeof(tmp));
528
529         return 0;
530 }
531 EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
532
533 int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
534                              enum nl80211_iftype iftype,
535                              const u8 *bssid, bool qos)
536 {
537         struct ieee80211_hdr hdr;
538         u16 hdrlen, ethertype;
539         __le16 fc;
540         const u8 *encaps_data;
541         int encaps_len, skip_header_bytes;
542         int nh_pos, h_pos;
543         int head_need;
544
545         if (unlikely(skb->len < ETH_HLEN))
546                 return -EINVAL;
547
548         nh_pos = skb_network_header(skb) - skb->data;
549         h_pos = skb_transport_header(skb) - skb->data;
550
551         /* convert Ethernet header to proper 802.11 header (based on
552          * operation mode) */
553         ethertype = (skb->data[12] << 8) | skb->data[13];
554         fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
555
556         switch (iftype) {
557         case NL80211_IFTYPE_AP:
558         case NL80211_IFTYPE_AP_VLAN:
559         case NL80211_IFTYPE_P2P_GO:
560                 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
561                 /* DA BSSID SA */
562                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
563                 memcpy(hdr.addr2, addr, ETH_ALEN);
564                 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
565                 hdrlen = 24;
566                 break;
567         case NL80211_IFTYPE_STATION:
568         case NL80211_IFTYPE_P2P_CLIENT:
569                 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
570                 /* BSSID SA DA */
571                 memcpy(hdr.addr1, bssid, ETH_ALEN);
572                 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
573                 memcpy(hdr.addr3, skb->data, ETH_ALEN);
574                 hdrlen = 24;
575                 break;
576         case NL80211_IFTYPE_OCB:
577         case NL80211_IFTYPE_ADHOC:
578                 /* DA SA BSSID */
579                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
580                 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
581                 memcpy(hdr.addr3, bssid, ETH_ALEN);
582                 hdrlen = 24;
583                 break;
584         default:
585                 return -EOPNOTSUPP;
586         }
587
588         if (qos) {
589                 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
590                 hdrlen += 2;
591         }
592
593         hdr.frame_control = fc;
594         hdr.duration_id = 0;
595         hdr.seq_ctrl = 0;
596
597         skip_header_bytes = ETH_HLEN;
598         if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
599                 encaps_data = bridge_tunnel_header;
600                 encaps_len = sizeof(bridge_tunnel_header);
601                 skip_header_bytes -= 2;
602         } else if (ethertype >= ETH_P_802_3_MIN) {
603                 encaps_data = rfc1042_header;
604                 encaps_len = sizeof(rfc1042_header);
605                 skip_header_bytes -= 2;
606         } else {
607                 encaps_data = NULL;
608                 encaps_len = 0;
609         }
610
611         skb_pull(skb, skip_header_bytes);
612         nh_pos -= skip_header_bytes;
613         h_pos -= skip_header_bytes;
614
615         head_need = hdrlen + encaps_len - skb_headroom(skb);
616
617         if (head_need > 0 || skb_cloned(skb)) {
618                 head_need = max(head_need, 0);
619                 if (head_need)
620                         skb_orphan(skb);
621
622                 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
623                         return -ENOMEM;
624
625                 skb->truesize += head_need;
626         }
627
628         if (encaps_data) {
629                 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
630                 nh_pos += encaps_len;
631                 h_pos += encaps_len;
632         }
633
634         memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
635
636         nh_pos += hdrlen;
637         h_pos += hdrlen;
638
639         /* Update skb pointers to various headers since this modified frame
640          * is going to go through Linux networking code that may potentially
641          * need things like pointer to IP header. */
642         skb_reset_mac_header(skb);
643         skb_set_network_header(skb, nh_pos);
644         skb_set_transport_header(skb, h_pos);
645
646         return 0;
647 }
648 EXPORT_SYMBOL(ieee80211_data_from_8023);
649
650 static void
651 __frame_add_frag(struct sk_buff *skb, struct page *page,
652                  void *ptr, int len, int size)
653 {
654         struct skb_shared_info *sh = skb_shinfo(skb);
655         int page_offset;
656
657         get_page(page);
658         page_offset = ptr - page_address(page);
659         skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
660 }
661
662 static void
663 __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
664                             int offset, int len)
665 {
666         struct skb_shared_info *sh = skb_shinfo(skb);
667         const skb_frag_t *frag = &sh->frags[0];
668         struct page *frag_page;
669         void *frag_ptr;
670         int frag_len, frag_size;
671         int head_size = skb->len - skb->data_len;
672         int cur_len;
673
674         frag_page = virt_to_head_page(skb->head);
675         frag_ptr = skb->data;
676         frag_size = head_size;
677
678         while (offset >= frag_size) {
679                 offset -= frag_size;
680                 frag_page = skb_frag_page(frag);
681                 frag_ptr = skb_frag_address(frag);
682                 frag_size = skb_frag_size(frag);
683                 frag++;
684         }
685
686         frag_ptr += offset;
687         frag_len = frag_size - offset;
688
689         cur_len = min(len, frag_len);
690
691         __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
692         len -= cur_len;
693
694         while (len > 0) {
695                 frag_len = skb_frag_size(frag);
696                 cur_len = min(len, frag_len);
697                 __frame_add_frag(frame, skb_frag_page(frag),
698                                  skb_frag_address(frag), cur_len, frag_len);
699                 len -= cur_len;
700                 frag++;
701         }
702 }
703
704 static struct sk_buff *
705 __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
706                        int offset, int len, bool reuse_frag)
707 {
708         struct sk_buff *frame;
709         int cur_len = len;
710
711         if (skb->len - offset < len)
712                 return NULL;
713
714         /*
715          * When reusing framents, copy some data to the head to simplify
716          * ethernet header handling and speed up protocol header processing
717          * in the stack later.
718          */
719         if (reuse_frag)
720                 cur_len = min_t(int, len, 32);
721
722         /*
723          * Allocate and reserve two bytes more for payload
724          * alignment since sizeof(struct ethhdr) is 14.
725          */
726         frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
727         if (!frame)
728                 return NULL;
729
730         skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
731         skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
732
733         len -= cur_len;
734         if (!len)
735                 return frame;
736
737         offset += cur_len;
738         __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
739
740         return frame;
741 }
742
743 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
744                               const u8 *addr, enum nl80211_iftype iftype,
745                               const unsigned int extra_headroom,
746                               const u8 *check_da, const u8 *check_sa)
747 {
748         unsigned int hlen = ALIGN(extra_headroom, 4);
749         struct sk_buff *frame = NULL;
750         u16 ethertype;
751         u8 *payload;
752         int offset = 0, remaining;
753         struct ethhdr eth;
754         bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
755         bool reuse_skb = false;
756         bool last = false;
757
758         while (!last) {
759                 unsigned int subframe_len;
760                 int len;
761                 u8 padding;
762
763                 skb_copy_bits(skb, offset, &eth, sizeof(eth));
764                 len = ntohs(eth.h_proto);
765                 subframe_len = sizeof(struct ethhdr) + len;
766                 padding = (4 - subframe_len) & 0x3;
767
768                 /* the last MSDU has no padding */
769                 remaining = skb->len - offset;
770                 if (subframe_len > remaining)
771                         goto purge;
772                 /* mitigate A-MSDU aggregation injection attacks */
773                 if (ether_addr_equal(eth.h_dest, rfc1042_header))
774                         goto purge;
775
776                 offset += sizeof(struct ethhdr);
777                 last = remaining <= subframe_len + padding;
778
779                 /* FIXME: should we really accept multicast DA? */
780                 if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
781                      !ether_addr_equal(check_da, eth.h_dest)) ||
782                     (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
783                         offset += len + padding;
784                         continue;
785                 }
786
787                 /* reuse skb for the last subframe */
788                 if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
789                         skb_pull(skb, offset);
790                         frame = skb;
791                         reuse_skb = true;
792                 } else {
793                         frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
794                                                        reuse_frag);
795                         if (!frame)
796                                 goto purge;
797
798                         offset += len + padding;
799                 }
800
801                 skb_reset_network_header(frame);
802                 frame->dev = skb->dev;
803                 frame->priority = skb->priority;
804
805                 payload = frame->data;
806                 ethertype = (payload[6] << 8) | payload[7];
807                 if (likely((ether_addr_equal(payload, rfc1042_header) &&
808                             ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
809                            ether_addr_equal(payload, bridge_tunnel_header))) {
810                         eth.h_proto = htons(ethertype);
811                         skb_pull(frame, ETH_ALEN + 2);
812                 }
813
814                 memcpy(skb_push(frame, sizeof(eth)), &eth, sizeof(eth));
815                 __skb_queue_tail(list, frame);
816         }
817
818         if (!reuse_skb)
819                 dev_kfree_skb(skb);
820
821         return;
822
823  purge:
824         __skb_queue_purge(list);
825         dev_kfree_skb(skb);
826 }
827 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
828
829 /* Given a data frame determine the 802.1p/1d tag to use. */
830 unsigned int cfg80211_classify8021d(struct sk_buff *skb,
831                                     struct cfg80211_qos_map *qos_map)
832 {
833         unsigned int dscp;
834         unsigned char vlan_priority;
835
836         /* skb->priority values from 256->263 are magic values to
837          * directly indicate a specific 802.1d priority.  This is used
838          * to allow 802.1d priority to be passed directly in from VLAN
839          * tags, etc.
840          */
841         if (skb->priority >= 256 && skb->priority <= 263)
842                 return skb->priority - 256;
843
844         if (skb_vlan_tag_present(skb)) {
845                 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
846                         >> VLAN_PRIO_SHIFT;
847                 if (vlan_priority > 0)
848                         return vlan_priority;
849         }
850
851         switch (skb->protocol) {
852         case htons(ETH_P_IP):
853                 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
854                 break;
855         case htons(ETH_P_IPV6):
856                 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
857                 break;
858         case htons(ETH_P_MPLS_UC):
859         case htons(ETH_P_MPLS_MC): {
860                 struct mpls_label mpls_tmp, *mpls;
861
862                 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
863                                           sizeof(*mpls), &mpls_tmp);
864                 if (!mpls)
865                         return 0;
866
867                 return (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
868                         >> MPLS_LS_TC_SHIFT;
869         }
870         case htons(ETH_P_80221):
871                 /* 802.21 is always network control traffic */
872                 return 7;
873         default:
874                 return 0;
875         }
876
877         if (qos_map) {
878                 unsigned int i, tmp_dscp = dscp >> 2;
879
880                 for (i = 0; i < qos_map->num_des; i++) {
881                         if (tmp_dscp == qos_map->dscp_exception[i].dscp)
882                                 return qos_map->dscp_exception[i].up;
883                 }
884
885                 for (i = 0; i < 8; i++) {
886                         if (tmp_dscp >= qos_map->up[i].low &&
887                             tmp_dscp <= qos_map->up[i].high)
888                                 return i;
889                 }
890         }
891
892         return dscp >> 5;
893 }
894 EXPORT_SYMBOL(cfg80211_classify8021d);
895
896 const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
897 {
898         const struct cfg80211_bss_ies *ies;
899
900         ies = rcu_dereference(bss->ies);
901         if (!ies)
902                 return NULL;
903
904         return cfg80211_find_ie(ie, ies->data, ies->len);
905 }
906 EXPORT_SYMBOL(ieee80211_bss_get_ie);
907
908 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
909 {
910         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
911         struct net_device *dev = wdev->netdev;
912         int i;
913
914         if (!wdev->connect_keys)
915                 return;
916
917         for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
918                 if (!wdev->connect_keys->params[i].cipher)
919                         continue;
920                 if (rdev_add_key(rdev, dev, i, false, NULL,
921                                  &wdev->connect_keys->params[i])) {
922                         netdev_err(dev, "failed to set key %d\n", i);
923                         continue;
924                 }
925                 if (wdev->connect_keys->def == i)
926                         if (rdev_set_default_key(rdev, dev, i, true, true)) {
927                                 netdev_err(dev, "failed to set defkey %d\n", i);
928                                 continue;
929                         }
930         }
931
932         kzfree(wdev->connect_keys);
933         wdev->connect_keys = NULL;
934 }
935
936 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
937 {
938         struct cfg80211_event *ev;
939         unsigned long flags;
940         const u8 *bssid = NULL;
941
942         spin_lock_irqsave(&wdev->event_lock, flags);
943         while (!list_empty(&wdev->event_list)) {
944                 ev = list_first_entry(&wdev->event_list,
945                                       struct cfg80211_event, list);
946                 list_del(&ev->list);
947                 spin_unlock_irqrestore(&wdev->event_lock, flags);
948
949                 wdev_lock(wdev);
950                 switch (ev->type) {
951                 case EVENT_CONNECT_RESULT:
952                         if (!is_zero_ether_addr(ev->cr.bssid))
953                                 bssid = ev->cr.bssid;
954                         __cfg80211_connect_result(
955                                 wdev->netdev, bssid,
956                                 ev->cr.req_ie, ev->cr.req_ie_len,
957                                 ev->cr.resp_ie, ev->cr.resp_ie_len,
958                                 ev->cr.status,
959                                 ev->cr.status == WLAN_STATUS_SUCCESS,
960                                 ev->cr.bss);
961                         break;
962                 case EVENT_ROAMED:
963                         __cfg80211_roamed(wdev, ev->rm.bss, ev->rm.req_ie,
964                                           ev->rm.req_ie_len, ev->rm.resp_ie,
965                                           ev->rm.resp_ie_len);
966                         break;
967                 case EVENT_DISCONNECTED:
968                         __cfg80211_disconnected(wdev->netdev,
969                                                 ev->dc.ie, ev->dc.ie_len,
970                                                 ev->dc.reason,
971                                                 !ev->dc.locally_generated);
972                         break;
973                 case EVENT_IBSS_JOINED:
974                         __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
975                                                ev->ij.channel);
976                         break;
977                 case EVENT_STOPPED:
978                         __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
979                         break;
980                 }
981                 wdev_unlock(wdev);
982
983                 kfree(ev);
984
985                 spin_lock_irqsave(&wdev->event_lock, flags);
986         }
987         spin_unlock_irqrestore(&wdev->event_lock, flags);
988 }
989
990 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
991 {
992         struct wireless_dev *wdev;
993
994         ASSERT_RTNL();
995
996         list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
997                 cfg80211_process_wdev_events(wdev);
998 }
999
1000 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
1001                           struct net_device *dev, enum nl80211_iftype ntype,
1002                           u32 *flags, struct vif_params *params)
1003 {
1004         int err;
1005         enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
1006
1007         ASSERT_RTNL();
1008
1009         /* don't support changing VLANs, you just re-create them */
1010         if (otype == NL80211_IFTYPE_AP_VLAN)
1011                 return -EOPNOTSUPP;
1012
1013         /* cannot change into P2P device or NAN */
1014         if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
1015             ntype == NL80211_IFTYPE_NAN)
1016                 return -EOPNOTSUPP;
1017
1018         if (!rdev->ops->change_virtual_intf ||
1019             !(rdev->wiphy.interface_modes & (1 << ntype)))
1020                 return -EOPNOTSUPP;
1021
1022         /* if it's part of a bridge, reject changing type to station/ibss */
1023         if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
1024             (ntype == NL80211_IFTYPE_ADHOC ||
1025              ntype == NL80211_IFTYPE_STATION ||
1026              ntype == NL80211_IFTYPE_P2P_CLIENT))
1027                 return -EBUSY;
1028
1029         if (ntype != otype) {
1030                 dev->ieee80211_ptr->use_4addr = false;
1031                 dev->ieee80211_ptr->mesh_id_up_len = 0;
1032                 wdev_lock(dev->ieee80211_ptr);
1033                 rdev_set_qos_map(rdev, dev, NULL);
1034                 wdev_unlock(dev->ieee80211_ptr);
1035
1036                 switch (otype) {
1037                 case NL80211_IFTYPE_AP:
1038                         cfg80211_stop_ap(rdev, dev, true);
1039                         break;
1040                 case NL80211_IFTYPE_ADHOC:
1041                         cfg80211_leave_ibss(rdev, dev, false);
1042                         break;
1043                 case NL80211_IFTYPE_STATION:
1044                 case NL80211_IFTYPE_P2P_CLIENT:
1045                         wdev_lock(dev->ieee80211_ptr);
1046                         cfg80211_disconnect(rdev, dev,
1047                                             WLAN_REASON_DEAUTH_LEAVING, true);
1048                         wdev_unlock(dev->ieee80211_ptr);
1049                         break;
1050                 case NL80211_IFTYPE_MESH_POINT:
1051                         /* mesh should be handled? */
1052                         break;
1053                 case NL80211_IFTYPE_OCB:
1054                         cfg80211_leave_ocb(rdev, dev);
1055                         break;
1056                 default:
1057                         break;
1058                 }
1059
1060                 cfg80211_process_rdev_events(rdev);
1061                 cfg80211_mlme_purge_registrations(dev->ieee80211_ptr);
1062         }
1063
1064         err = rdev_change_virtual_intf(rdev, dev, ntype, flags, params);
1065
1066         WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
1067
1068         if (!err && params && params->use_4addr != -1)
1069                 dev->ieee80211_ptr->use_4addr = params->use_4addr;
1070
1071         if (!err) {
1072                 dev->priv_flags &= ~IFF_DONT_BRIDGE;
1073                 switch (ntype) {
1074                 case NL80211_IFTYPE_STATION:
1075                         if (dev->ieee80211_ptr->use_4addr)
1076                                 break;
1077                         /* fall through */
1078                 case NL80211_IFTYPE_OCB:
1079                 case NL80211_IFTYPE_P2P_CLIENT:
1080                 case NL80211_IFTYPE_ADHOC:
1081                         dev->priv_flags |= IFF_DONT_BRIDGE;
1082                         break;
1083                 case NL80211_IFTYPE_P2P_GO:
1084                 case NL80211_IFTYPE_AP:
1085                 case NL80211_IFTYPE_AP_VLAN:
1086                 case NL80211_IFTYPE_WDS:
1087                 case NL80211_IFTYPE_MESH_POINT:
1088                         /* bridging OK */
1089                         break;
1090                 case NL80211_IFTYPE_MONITOR:
1091                         /* monitor can't bridge anyway */
1092                         break;
1093                 case NL80211_IFTYPE_UNSPECIFIED:
1094                 case NUM_NL80211_IFTYPES:
1095                         /* not happening */
1096                         break;
1097                 case NL80211_IFTYPE_P2P_DEVICE:
1098                 case NL80211_IFTYPE_NAN:
1099                         WARN_ON(1);
1100                         break;
1101                 }
1102         }
1103
1104         if (!err && ntype != otype && netif_running(dev)) {
1105                 cfg80211_update_iface_num(rdev, ntype, 1);
1106                 cfg80211_update_iface_num(rdev, otype, -1);
1107         }
1108
1109         return err;
1110 }
1111
1112 static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
1113 {
1114         static const u32 __mcs2bitrate[] = {
1115                 /* control PHY */
1116                 [0] =   275,
1117                 /* SC PHY */
1118                 [1] =  3850,
1119                 [2] =  7700,
1120                 [3] =  9625,
1121                 [4] = 11550,
1122                 [5] = 12512, /* 1251.25 mbps */
1123                 [6] = 15400,
1124                 [7] = 19250,
1125                 [8] = 23100,
1126                 [9] = 25025,
1127                 [10] = 30800,
1128                 [11] = 38500,
1129                 [12] = 46200,
1130                 /* OFDM PHY */
1131                 [13] =  6930,
1132                 [14] =  8662, /* 866.25 mbps */
1133                 [15] = 13860,
1134                 [16] = 17325,
1135                 [17] = 20790,
1136                 [18] = 27720,
1137                 [19] = 34650,
1138                 [20] = 41580,
1139                 [21] = 45045,
1140                 [22] = 51975,
1141                 [23] = 62370,
1142                 [24] = 67568, /* 6756.75 mbps */
1143                 /* LP-SC PHY */
1144                 [25] =  6260,
1145                 [26] =  8340,
1146                 [27] = 11120,
1147                 [28] = 12510,
1148                 [29] = 16680,
1149                 [30] = 22240,
1150                 [31] = 25030,
1151         };
1152
1153         if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1154                 return 0;
1155
1156         return __mcs2bitrate[rate->mcs];
1157 }
1158
1159 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1160 {
1161         static const u32 base[4][10] = {
1162                 {   6500000,
1163                    13000000,
1164                    19500000,
1165                    26000000,
1166                    39000000,
1167                    52000000,
1168                    58500000,
1169                    65000000,
1170                    78000000,
1171                 /* not in the spec, but some devices use this: */
1172                    86500000,
1173                 },
1174                 {  13500000,
1175                    27000000,
1176                    40500000,
1177                    54000000,
1178                    81000000,
1179                   108000000,
1180                   121500000,
1181                   135000000,
1182                   162000000,
1183                   180000000,
1184                 },
1185                 {  29300000,
1186                    58500000,
1187                    87800000,
1188                   117000000,
1189                   175500000,
1190                   234000000,
1191                   263300000,
1192                   292500000,
1193                   351000000,
1194                   390000000,
1195                 },
1196                 {  58500000,
1197                   117000000,
1198                   175500000,
1199                   234000000,
1200                   351000000,
1201                   468000000,
1202                   526500000,
1203                   585000000,
1204                   702000000,
1205                   780000000,
1206                 },
1207         };
1208         u32 bitrate;
1209         int idx;
1210
1211         if (WARN_ON_ONCE(rate->mcs > 9))
1212                 return 0;
1213
1214         switch (rate->bw) {
1215         case RATE_INFO_BW_160:
1216                 idx = 3;
1217                 break;
1218         case RATE_INFO_BW_80:
1219                 idx = 2;
1220                 break;
1221         case RATE_INFO_BW_40:
1222                 idx = 1;
1223                 break;
1224         case RATE_INFO_BW_5:
1225         case RATE_INFO_BW_10:
1226         default:
1227                 WARN_ON(1);
1228                 /* fall through */
1229         case RATE_INFO_BW_20:
1230                 idx = 0;
1231         }
1232
1233         bitrate = base[idx][rate->mcs];
1234         bitrate *= rate->nss;
1235
1236         if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1237                 bitrate = (bitrate / 9) * 10;
1238
1239         /* do NOT round down here */
1240         return (bitrate + 50000) / 100000;
1241 }
1242
1243 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1244 {
1245         int modulation, streams, bitrate;
1246
1247         if (!(rate->flags & RATE_INFO_FLAGS_MCS) &&
1248             !(rate->flags & RATE_INFO_FLAGS_VHT_MCS))
1249                 return rate->legacy;
1250         if (rate->flags & RATE_INFO_FLAGS_60G)
1251                 return cfg80211_calculate_bitrate_60g(rate);
1252         if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1253                 return cfg80211_calculate_bitrate_vht(rate);
1254
1255         /* the formula below does only work for MCS values smaller than 32 */
1256         if (WARN_ON_ONCE(rate->mcs >= 32))
1257                 return 0;
1258
1259         modulation = rate->mcs & 7;
1260         streams = (rate->mcs >> 3) + 1;
1261
1262         bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1263
1264         if (modulation < 4)
1265                 bitrate *= (modulation + 1);
1266         else if (modulation == 4)
1267                 bitrate *= (modulation + 2);
1268         else
1269                 bitrate *= (modulation + 3);
1270
1271         bitrate *= streams;
1272
1273         if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1274                 bitrate = (bitrate / 9) * 10;
1275
1276         /* do NOT round down here */
1277         return (bitrate + 50000) / 100000;
1278 }
1279 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1280
1281 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1282                           enum ieee80211_p2p_attr_id attr,
1283                           u8 *buf, unsigned int bufsize)
1284 {
1285         u8 *out = buf;
1286         u16 attr_remaining = 0;
1287         bool desired_attr = false;
1288         u16 desired_len = 0;
1289
1290         while (len > 0) {
1291                 unsigned int iedatalen;
1292                 unsigned int copy;
1293                 const u8 *iedata;
1294
1295                 if (len < 2)
1296                         return -EILSEQ;
1297                 iedatalen = ies[1];
1298                 if (iedatalen + 2 > len)
1299                         return -EILSEQ;
1300
1301                 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1302                         goto cont;
1303
1304                 if (iedatalen < 4)
1305                         goto cont;
1306
1307                 iedata = ies + 2;
1308
1309                 /* check WFA OUI, P2P subtype */
1310                 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1311                     iedata[2] != 0x9a || iedata[3] != 0x09)
1312                         goto cont;
1313
1314                 iedatalen -= 4;
1315                 iedata += 4;
1316
1317                 /* check attribute continuation into this IE */
1318                 copy = min_t(unsigned int, attr_remaining, iedatalen);
1319                 if (copy && desired_attr) {
1320                         desired_len += copy;
1321                         if (out) {
1322                                 memcpy(out, iedata, min(bufsize, copy));
1323                                 out += min(bufsize, copy);
1324                                 bufsize -= min(bufsize, copy);
1325                         }
1326
1327
1328                         if (copy == attr_remaining)
1329                                 return desired_len;
1330                 }
1331
1332                 attr_remaining -= copy;
1333                 if (attr_remaining)
1334                         goto cont;
1335
1336                 iedatalen -= copy;
1337                 iedata += copy;
1338
1339                 while (iedatalen > 0) {
1340                         u16 attr_len;
1341
1342                         /* P2P attribute ID & size must fit */
1343                         if (iedatalen < 3)
1344                                 return -EILSEQ;
1345                         desired_attr = iedata[0] == attr;
1346                         attr_len = get_unaligned_le16(iedata + 1);
1347                         iedatalen -= 3;
1348                         iedata += 3;
1349
1350                         copy = min_t(unsigned int, attr_len, iedatalen);
1351
1352                         if (desired_attr) {
1353                                 desired_len += copy;
1354                                 if (out) {
1355                                         memcpy(out, iedata, min(bufsize, copy));
1356                                         out += min(bufsize, copy);
1357                                         bufsize -= min(bufsize, copy);
1358                                 }
1359
1360                                 if (copy == attr_len)
1361                                         return desired_len;
1362                         }
1363
1364                         iedata += copy;
1365                         iedatalen -= copy;
1366                         attr_remaining = attr_len - copy;
1367                 }
1368
1369  cont:
1370                 len -= ies[1] + 2;
1371                 ies += ies[1] + 2;
1372         }
1373
1374         if (attr_remaining && desired_attr)
1375                 return -EILSEQ;
1376
1377         return -ENOENT;
1378 }
1379 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1380
1381 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
1382 {
1383         int i;
1384
1385         for (i = 0; i < n_ids; i++)
1386                 if (ids[i] == id)
1387                         return true;
1388         return false;
1389 }
1390
1391 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1392                               const u8 *ids, int n_ids,
1393                               const u8 *after_ric, int n_after_ric,
1394                               size_t offset)
1395 {
1396         size_t pos = offset;
1397
1398         while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos])) {
1399                 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1400                         pos += 2 + ies[pos + 1];
1401
1402                         while (pos < ielen &&
1403                                !ieee80211_id_in_list(after_ric, n_after_ric,
1404                                                      ies[pos]))
1405                                 pos += 2 + ies[pos + 1];
1406                 } else {
1407                         pos += 2 + ies[pos + 1];
1408                 }
1409         }
1410
1411         return pos;
1412 }
1413 EXPORT_SYMBOL(ieee80211_ie_split_ric);
1414
1415 bool ieee80211_operating_class_to_band(u8 operating_class,
1416                                        enum nl80211_band *band)
1417 {
1418         switch (operating_class) {
1419         case 112:
1420         case 115 ... 127:
1421         case 128 ... 130:
1422                 *band = NL80211_BAND_5GHZ;
1423                 return true;
1424         case 81:
1425         case 82:
1426         case 83:
1427         case 84:
1428                 *band = NL80211_BAND_2GHZ;
1429                 return true;
1430         case 180:
1431                 *band = NL80211_BAND_60GHZ;
1432                 return true;
1433         }
1434
1435         return false;
1436 }
1437 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1438
1439 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1440                                           u8 *op_class)
1441 {
1442         u8 vht_opclass;
1443         u32 freq = chandef->center_freq1;
1444
1445         if (freq >= 2412 && freq <= 2472) {
1446                 if (chandef->width > NL80211_CHAN_WIDTH_40)
1447                         return false;
1448
1449                 /* 2.407 GHz, channels 1..13 */
1450                 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1451                         if (freq > chandef->chan->center_freq)
1452                                 *op_class = 83; /* HT40+ */
1453                         else
1454                                 *op_class = 84; /* HT40- */
1455                 } else {
1456                         *op_class = 81;
1457                 }
1458
1459                 return true;
1460         }
1461
1462         if (freq == 2484) {
1463                 if (chandef->width > NL80211_CHAN_WIDTH_40)
1464                         return false;
1465
1466                 *op_class = 82; /* channel 14 */
1467                 return true;
1468         }
1469
1470         switch (chandef->width) {
1471         case NL80211_CHAN_WIDTH_80:
1472                 vht_opclass = 128;
1473                 break;
1474         case NL80211_CHAN_WIDTH_160:
1475                 vht_opclass = 129;
1476                 break;
1477         case NL80211_CHAN_WIDTH_80P80:
1478                 vht_opclass = 130;
1479                 break;
1480         case NL80211_CHAN_WIDTH_10:
1481         case NL80211_CHAN_WIDTH_5:
1482                 return false; /* unsupported for now */
1483         default:
1484                 vht_opclass = 0;
1485                 break;
1486         }
1487
1488         /* 5 GHz, channels 36..48 */
1489         if (freq >= 5180 && freq <= 5240) {
1490                 if (vht_opclass) {
1491                         *op_class = vht_opclass;
1492                 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1493                         if (freq > chandef->chan->center_freq)
1494                                 *op_class = 116;
1495                         else
1496                                 *op_class = 117;
1497                 } else {
1498                         *op_class = 115;
1499                 }
1500
1501                 return true;
1502         }
1503
1504         /* 5 GHz, channels 52..64 */
1505         if (freq >= 5260 && freq <= 5320) {
1506                 if (vht_opclass) {
1507                         *op_class = vht_opclass;
1508                 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1509                         if (freq > chandef->chan->center_freq)
1510                                 *op_class = 119;
1511                         else
1512                                 *op_class = 120;
1513                 } else {
1514                         *op_class = 118;
1515                 }
1516
1517                 return true;
1518         }
1519
1520         /* 5 GHz, channels 100..144 */
1521         if (freq >= 5500 && freq <= 5720) {
1522                 if (vht_opclass) {
1523                         *op_class = vht_opclass;
1524                 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1525                         if (freq > chandef->chan->center_freq)
1526                                 *op_class = 122;
1527                         else
1528                                 *op_class = 123;
1529                 } else {
1530                         *op_class = 121;
1531                 }
1532
1533                 return true;
1534         }
1535
1536         /* 5 GHz, channels 149..169 */
1537         if (freq >= 5745 && freq <= 5845) {
1538                 if (vht_opclass) {
1539                         *op_class = vht_opclass;
1540                 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1541                         if (freq > chandef->chan->center_freq)
1542                                 *op_class = 126;
1543                         else
1544                                 *op_class = 127;
1545                 } else if (freq <= 5805) {
1546                         *op_class = 124;
1547                 } else {
1548                         *op_class = 125;
1549                 }
1550
1551                 return true;
1552         }
1553
1554         /* 56.16 GHz, channel 1..4 */
1555         if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 4) {
1556                 if (chandef->width >= NL80211_CHAN_WIDTH_40)
1557                         return false;
1558
1559                 *op_class = 180;
1560                 return true;
1561         }
1562
1563         /* not supported yet */
1564         return false;
1565 }
1566 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1567
1568 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1569                                  u32 beacon_int)
1570 {
1571         struct wireless_dev *wdev;
1572         int res = 0;
1573
1574         if (beacon_int < 10 || beacon_int > 10000)
1575                 return -EINVAL;
1576
1577         list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
1578                 if (!wdev->beacon_interval)
1579                         continue;
1580                 if (wdev->beacon_interval != beacon_int) {
1581                         res = -EINVAL;
1582                         break;
1583                 }
1584         }
1585
1586         return res;
1587 }
1588
1589 int cfg80211_iter_combinations(struct wiphy *wiphy,
1590                                const int num_different_channels,
1591                                const u8 radar_detect,
1592                                const int iftype_num[NUM_NL80211_IFTYPES],
1593                                void (*iter)(const struct ieee80211_iface_combination *c,
1594                                             void *data),
1595                                void *data)
1596 {
1597         const struct ieee80211_regdomain *regdom;
1598         enum nl80211_dfs_regions region = 0;
1599         int i, j, iftype;
1600         int num_interfaces = 0;
1601         u32 used_iftypes = 0;
1602
1603         if (radar_detect) {
1604                 rcu_read_lock();
1605                 regdom = rcu_dereference(cfg80211_regdomain);
1606                 if (regdom)
1607                         region = regdom->dfs_region;
1608                 rcu_read_unlock();
1609         }
1610
1611         for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1612                 num_interfaces += iftype_num[iftype];
1613                 if (iftype_num[iftype] > 0 &&
1614                     !(wiphy->software_iftypes & BIT(iftype)))
1615                         used_iftypes |= BIT(iftype);
1616         }
1617
1618         for (i = 0; i < wiphy->n_iface_combinations; i++) {
1619                 const struct ieee80211_iface_combination *c;
1620                 struct ieee80211_iface_limit *limits;
1621                 u32 all_iftypes = 0;
1622
1623                 c = &wiphy->iface_combinations[i];
1624
1625                 if (num_interfaces > c->max_interfaces)
1626                         continue;
1627                 if (num_different_channels > c->num_different_channels)
1628                         continue;
1629
1630                 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1631                                  GFP_KERNEL);
1632                 if (!limits)
1633                         return -ENOMEM;
1634
1635                 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1636                         if (wiphy->software_iftypes & BIT(iftype))
1637                                 continue;
1638                         for (j = 0; j < c->n_limits; j++) {
1639                                 all_iftypes |= limits[j].types;
1640                                 if (!(limits[j].types & BIT(iftype)))
1641                                         continue;
1642                                 if (limits[j].max < iftype_num[iftype])
1643                                         goto cont;
1644                                 limits[j].max -= iftype_num[iftype];
1645                         }
1646                 }
1647
1648                 if (radar_detect != (c->radar_detect_widths & radar_detect))
1649                         goto cont;
1650
1651                 if (radar_detect && c->radar_detect_regions &&
1652                     !(c->radar_detect_regions & BIT(region)))
1653                         goto cont;
1654
1655                 /* Finally check that all iftypes that we're currently
1656                  * using are actually part of this combination. If they
1657                  * aren't then we can't use this combination and have
1658                  * to continue to the next.
1659                  */
1660                 if ((all_iftypes & used_iftypes) != used_iftypes)
1661                         goto cont;
1662
1663                 /* This combination covered all interface types and
1664                  * supported the requested numbers, so we're good.
1665                  */
1666
1667                 (*iter)(c, data);
1668  cont:
1669                 kfree(limits);
1670         }
1671
1672         return 0;
1673 }
1674 EXPORT_SYMBOL(cfg80211_iter_combinations);
1675
1676 static void
1677 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
1678                           void *data)
1679 {
1680         int *num = data;
1681         (*num)++;
1682 }
1683
1684 int cfg80211_check_combinations(struct wiphy *wiphy,
1685                                 const int num_different_channels,
1686                                 const u8 radar_detect,
1687                                 const int iftype_num[NUM_NL80211_IFTYPES])
1688 {
1689         int err, num = 0;
1690
1691         err = cfg80211_iter_combinations(wiphy, num_different_channels,
1692                                          radar_detect, iftype_num,
1693                                          cfg80211_iter_sum_ifcombs, &num);
1694         if (err)
1695                 return err;
1696         if (num == 0)
1697                 return -EBUSY;
1698
1699         return 0;
1700 }
1701 EXPORT_SYMBOL(cfg80211_check_combinations);
1702
1703 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1704                            const u8 *rates, unsigned int n_rates,
1705                            u32 *mask)
1706 {
1707         int i, j;
1708
1709         if (!sband)
1710                 return -EINVAL;
1711
1712         if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1713                 return -EINVAL;
1714
1715         *mask = 0;
1716
1717         for (i = 0; i < n_rates; i++) {
1718                 int rate = (rates[i] & 0x7f) * 5;
1719                 bool found = false;
1720
1721                 for (j = 0; j < sband->n_bitrates; j++) {
1722                         if (sband->bitrates[j].bitrate == rate) {
1723                                 found = true;
1724                                 *mask |= BIT(j);
1725                                 break;
1726                         }
1727                 }
1728                 if (!found)
1729                         return -EINVAL;
1730         }
1731
1732         /*
1733          * mask must have at least one bit set here since we
1734          * didn't accept a 0-length rates array nor allowed
1735          * entries in the array that didn't exist
1736          */
1737
1738         return 0;
1739 }
1740
1741 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
1742 {
1743         enum nl80211_band band;
1744         unsigned int n_channels = 0;
1745
1746         for (band = 0; band < NUM_NL80211_BANDS; band++)
1747                 if (wiphy->bands[band])
1748                         n_channels += wiphy->bands[band]->n_channels;
1749
1750         return n_channels;
1751 }
1752 EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
1753
1754 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
1755                          struct station_info *sinfo)
1756 {
1757         struct cfg80211_registered_device *rdev;
1758         struct wireless_dev *wdev;
1759
1760         wdev = dev->ieee80211_ptr;
1761         if (!wdev)
1762                 return -EOPNOTSUPP;
1763
1764         rdev = wiphy_to_rdev(wdev->wiphy);
1765         if (!rdev->ops->get_station)
1766                 return -EOPNOTSUPP;
1767
1768         return rdev_get_station(rdev, dev, mac_addr, sinfo);
1769 }
1770 EXPORT_SYMBOL(cfg80211_get_station);
1771
1772 void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
1773 {
1774         int i;
1775
1776         if (!f)
1777                 return;
1778
1779         kfree(f->serv_spec_info);
1780         kfree(f->srf_bf);
1781         kfree(f->srf_macs);
1782         for (i = 0; i < f->num_rx_filters; i++)
1783                 kfree(f->rx_filters[i].filter);
1784
1785         for (i = 0; i < f->num_tx_filters; i++)
1786                 kfree(f->tx_filters[i].filter);
1787
1788         kfree(f->rx_filters);
1789         kfree(f->tx_filters);
1790         kfree(f);
1791 }
1792 EXPORT_SYMBOL(cfg80211_free_nan_func);
1793
1794 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1795 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1796 const unsigned char rfc1042_header[] __aligned(2) =
1797         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1798 EXPORT_SYMBOL(rfc1042_header);
1799
1800 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1801 const unsigned char bridge_tunnel_header[] __aligned(2) =
1802         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1803 EXPORT_SYMBOL(bridge_tunnel_header);
1804
1805 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
1806 struct iapp_layer2_update {
1807         u8 da[ETH_ALEN];        /* broadcast */
1808         u8 sa[ETH_ALEN];        /* STA addr */
1809         __be16 len;             /* 6 */
1810         u8 dsap;                /* 0 */
1811         u8 ssap;                /* 0 */
1812         u8 control;
1813         u8 xid_info[3];
1814 } __packed;
1815
1816 void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
1817 {
1818         struct iapp_layer2_update *msg;
1819         struct sk_buff *skb;
1820
1821         /* Send Level 2 Update Frame to update forwarding tables in layer 2
1822          * bridge devices */
1823
1824         skb = dev_alloc_skb(sizeof(*msg));
1825         if (!skb)
1826                 return;
1827         msg = (struct iapp_layer2_update *)skb_put(skb, sizeof(*msg));
1828
1829         /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
1830          * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
1831
1832         eth_broadcast_addr(msg->da);
1833         ether_addr_copy(msg->sa, addr);
1834         msg->len = htons(6);
1835         msg->dsap = 0;
1836         msg->ssap = 0x01;       /* NULL LSAP, CR Bit: Response */
1837         msg->control = 0xaf;    /* XID response lsb.1111F101.
1838                                  * F=0 (no poll command; unsolicited frame) */
1839         msg->xid_info[0] = 0x81;        /* XID format identifier */
1840         msg->xid_info[1] = 1;   /* LLC types/classes: Type 1 LLC */
1841         msg->xid_info[2] = 0;   /* XID sender's receive window size (RW) */
1842
1843         skb->dev = dev;
1844         skb->protocol = eth_type_trans(skb, dev);
1845         memset(skb->cb, 0, sizeof(skb->cb));
1846         netif_rx_ni(skb);
1847 }
1848 EXPORT_SYMBOL(cfg80211_send_layer2_update);