net/mlx4_en: Fix mixed PFC and Global pause user control requests
[pandora-kernel.git] / net / wireless / util.c
1 /*
2  * Wireless utility functions
3  *
4  * Copyright 2007-2009  Johannes Berg <johannes@sipsolutions.net>
5  */
6 #include <linux/export.h>
7 #include <linux/bitops.h>
8 #include <linux/etherdevice.h>
9 #include <linux/slab.h>
10 #include <linux/crc32.h>
11 #include <net/cfg80211.h>
12 #include <net/ip.h>
13 #include "core.h"
14
15 struct ieee80211_rate *
16 ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
17                             u32 basic_rates, int bitrate)
18 {
19         struct ieee80211_rate *result = &sband->bitrates[0];
20         int i;
21
22         for (i = 0; i < sband->n_bitrates; i++) {
23                 if (!(basic_rates & BIT(i)))
24                         continue;
25                 if (sband->bitrates[i].bitrate > bitrate)
26                         continue;
27                 result = &sband->bitrates[i];
28         }
29
30         return result;
31 }
32 EXPORT_SYMBOL(ieee80211_get_response_rate);
33
34 int ieee80211_channel_to_frequency(int chan, enum ieee80211_band band)
35 {
36         /* see 802.11 17.3.8.3.2 and Annex J
37          * there are overlapping channel numbers in 5GHz and 2GHz bands */
38         if (band == IEEE80211_BAND_5GHZ) {
39                 if (chan >= 182 && chan <= 196)
40                         return 4000 + chan * 5;
41                 else
42                         return 5000 + chan * 5;
43         } else { /* IEEE80211_BAND_2GHZ */
44                 if (chan == 14)
45                         return 2484;
46                 else if (chan < 14)
47                         return 2407 + chan * 5;
48                 else
49                         return 0; /* not supported */
50         }
51 }
52 EXPORT_SYMBOL(ieee80211_channel_to_frequency);
53
54 int ieee80211_frequency_to_channel(int freq)
55 {
56         /* see 802.11 17.3.8.3.2 and Annex J */
57         if (freq == 2484)
58                 return 14;
59         else if (freq < 2484)
60                 return (freq - 2407) / 5;
61         else if (freq >= 4910 && freq <= 4980)
62                 return (freq - 4000) / 5;
63         else
64                 return (freq - 5000) / 5;
65 }
66 EXPORT_SYMBOL(ieee80211_frequency_to_channel);
67
68 struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
69                                                   int freq)
70 {
71         enum ieee80211_band band;
72         struct ieee80211_supported_band *sband;
73         int i;
74
75         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
76                 sband = wiphy->bands[band];
77
78                 if (!sband)
79                         continue;
80
81                 for (i = 0; i < sband->n_channels; i++) {
82                         if (sband->channels[i].center_freq == freq)
83                                 return &sband->channels[i];
84                 }
85         }
86
87         return NULL;
88 }
89 EXPORT_SYMBOL(__ieee80211_get_channel);
90
91 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
92                                      enum ieee80211_band band)
93 {
94         int i, want;
95
96         switch (band) {
97         case IEEE80211_BAND_5GHZ:
98                 want = 3;
99                 for (i = 0; i < sband->n_bitrates; i++) {
100                         if (sband->bitrates[i].bitrate == 60 ||
101                             sband->bitrates[i].bitrate == 120 ||
102                             sband->bitrates[i].bitrate == 240) {
103                                 sband->bitrates[i].flags |=
104                                         IEEE80211_RATE_MANDATORY_A;
105                                 want--;
106                         }
107                 }
108                 WARN_ON(want);
109                 break;
110         case IEEE80211_BAND_2GHZ:
111                 want = 7;
112                 for (i = 0; i < sband->n_bitrates; i++) {
113                         if (sband->bitrates[i].bitrate == 10) {
114                                 sband->bitrates[i].flags |=
115                                         IEEE80211_RATE_MANDATORY_B |
116                                         IEEE80211_RATE_MANDATORY_G;
117                                 want--;
118                         }
119
120                         if (sband->bitrates[i].bitrate == 20 ||
121                             sband->bitrates[i].bitrate == 55 ||
122                             sband->bitrates[i].bitrate == 110 ||
123                             sband->bitrates[i].bitrate == 60 ||
124                             sband->bitrates[i].bitrate == 120 ||
125                             sband->bitrates[i].bitrate == 240) {
126                                 sband->bitrates[i].flags |=
127                                         IEEE80211_RATE_MANDATORY_G;
128                                 want--;
129                         }
130
131                         if (sband->bitrates[i].bitrate != 10 &&
132                             sband->bitrates[i].bitrate != 20 &&
133                             sband->bitrates[i].bitrate != 55 &&
134                             sband->bitrates[i].bitrate != 110)
135                                 sband->bitrates[i].flags |=
136                                         IEEE80211_RATE_ERP_G;
137                 }
138                 WARN_ON(want != 0 && want != 3 && want != 6);
139                 break;
140         case IEEE80211_NUM_BANDS:
141                 WARN_ON(1);
142                 break;
143         }
144 }
145
146 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
147 {
148         enum ieee80211_band band;
149
150         for (band = 0; band < IEEE80211_NUM_BANDS; band++)
151                 if (wiphy->bands[band])
152                         set_mandatory_flags_band(wiphy->bands[band], band);
153 }
154
155 bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
156 {
157         int i;
158         for (i = 0; i < wiphy->n_cipher_suites; i++)
159                 if (cipher == wiphy->cipher_suites[i])
160                         return true;
161         return false;
162 }
163
164 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
165                                    struct key_params *params, int key_idx,
166                                    bool pairwise, const u8 *mac_addr)
167 {
168         if (key_idx > 5)
169                 return -EINVAL;
170
171         if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
172                 return -EINVAL;
173
174         if (pairwise && !mac_addr)
175                 return -EINVAL;
176
177         /*
178          * Disallow pairwise keys with non-zero index unless it's WEP
179          * or a vendor specific cipher (because current deployments use
180          * pairwise WEP keys with non-zero indices and for vendor specific
181          * ciphers this should be validated in the driver or hardware level
182          * - but 802.11i clearly specifies to use zero)
183          */
184         if (pairwise && key_idx &&
185             ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
186              (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
187              (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
188                 return -EINVAL;
189
190         switch (params->cipher) {
191         case WLAN_CIPHER_SUITE_WEP40:
192                 if (params->key_len != WLAN_KEY_LEN_WEP40)
193                         return -EINVAL;
194                 break;
195         case WLAN_CIPHER_SUITE_TKIP:
196                 if (params->key_len != WLAN_KEY_LEN_TKIP)
197                         return -EINVAL;
198                 break;
199         case WLAN_CIPHER_SUITE_CCMP:
200                 if (params->key_len != WLAN_KEY_LEN_CCMP)
201                         return -EINVAL;
202                 break;
203         case WLAN_CIPHER_SUITE_WEP104:
204                 if (params->key_len != WLAN_KEY_LEN_WEP104)
205                         return -EINVAL;
206                 break;
207         case WLAN_CIPHER_SUITE_AES_CMAC:
208                 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
209                         return -EINVAL;
210                 break;
211         default:
212                 /*
213                  * We don't know anything about this algorithm,
214                  * allow using it -- but the driver must check
215                  * all parameters! We still check below whether
216                  * or not the driver supports this algorithm,
217                  * of course.
218                  */
219                 break;
220         }
221
222         if (params->seq) {
223                 switch (params->cipher) {
224                 case WLAN_CIPHER_SUITE_WEP40:
225                 case WLAN_CIPHER_SUITE_WEP104:
226                         /* These ciphers do not use key sequence */
227                         return -EINVAL;
228                 case WLAN_CIPHER_SUITE_TKIP:
229                 case WLAN_CIPHER_SUITE_CCMP:
230                 case WLAN_CIPHER_SUITE_AES_CMAC:
231                         if (params->seq_len != 6)
232                                 return -EINVAL;
233                         break;
234                 }
235         }
236
237         if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
238                 return -EINVAL;
239
240         return 0;
241 }
242
243 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
244 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
245 const unsigned char rfc1042_header[] __aligned(2) =
246         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
247 EXPORT_SYMBOL(rfc1042_header);
248
249 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
250 const unsigned char bridge_tunnel_header[] __aligned(2) =
251         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
252 EXPORT_SYMBOL(bridge_tunnel_header);
253
254 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
255 {
256         unsigned int hdrlen = 24;
257
258         if (ieee80211_is_data(fc)) {
259                 if (ieee80211_has_a4(fc))
260                         hdrlen = 30;
261                 if (ieee80211_is_data_qos(fc)) {
262                         hdrlen += IEEE80211_QOS_CTL_LEN;
263                         if (ieee80211_has_order(fc))
264                                 hdrlen += IEEE80211_HT_CTL_LEN;
265                 }
266                 goto out;
267         }
268
269         if (ieee80211_is_ctl(fc)) {
270                 /*
271                  * ACK and CTS are 10 bytes, all others 16. To see how
272                  * to get this condition consider
273                  *   subtype mask:   0b0000000011110000 (0x00F0)
274                  *   ACK subtype:    0b0000000011010000 (0x00D0)
275                  *   CTS subtype:    0b0000000011000000 (0x00C0)
276                  *   bits that matter:         ^^^      (0x00E0)
277                  *   value of those: 0b0000000011000000 (0x00C0)
278                  */
279                 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
280                         hdrlen = 10;
281                 else
282                         hdrlen = 16;
283         }
284 out:
285         return hdrlen;
286 }
287 EXPORT_SYMBOL(ieee80211_hdrlen);
288
289 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
290 {
291         const struct ieee80211_hdr *hdr =
292                         (const struct ieee80211_hdr *)skb->data;
293         unsigned int hdrlen;
294
295         if (unlikely(skb->len < 10))
296                 return 0;
297         hdrlen = ieee80211_hdrlen(hdr->frame_control);
298         if (unlikely(hdrlen > skb->len))
299                 return 0;
300         return hdrlen;
301 }
302 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
303
304 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
305 {
306         int ae = meshhdr->flags & MESH_FLAGS_AE;
307         /* 802.11-2012, 8.2.4.7.3 */
308         switch (ae) {
309         default:
310         case 0:
311                 return 6;
312         case MESH_FLAGS_AE_A4:
313                 return 12;
314         case MESH_FLAGS_AE_A5_A6:
315                 return 18;
316         }
317 }
318 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
319
320 int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
321                            enum nl80211_iftype iftype)
322 {
323         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
324         u16 hdrlen, ethertype;
325         u8 *payload;
326         u8 dst[ETH_ALEN];
327         u8 src[ETH_ALEN] __aligned(2);
328
329         if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
330                 return -1;
331
332         hdrlen = ieee80211_hdrlen(hdr->frame_control);
333
334         /* convert IEEE 802.11 header + possible LLC headers into Ethernet
335          * header
336          * IEEE 802.11 address fields:
337          * ToDS FromDS Addr1 Addr2 Addr3 Addr4
338          *   0     0   DA    SA    BSSID n/a
339          *   0     1   DA    BSSID SA    n/a
340          *   1     0   BSSID SA    DA    n/a
341          *   1     1   RA    TA    DA    SA
342          */
343         memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
344         memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
345
346         switch (hdr->frame_control &
347                 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
348         case cpu_to_le16(IEEE80211_FCTL_TODS):
349                 if (unlikely(iftype != NL80211_IFTYPE_AP &&
350                              iftype != NL80211_IFTYPE_AP_VLAN &&
351                              iftype != NL80211_IFTYPE_P2P_GO))
352                         return -1;
353                 break;
354         case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
355                 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
356                              iftype != NL80211_IFTYPE_MESH_POINT &&
357                              iftype != NL80211_IFTYPE_AP_VLAN &&
358                              iftype != NL80211_IFTYPE_STATION))
359                         return -1;
360                 if (iftype == NL80211_IFTYPE_MESH_POINT) {
361                         struct ieee80211s_hdr *meshdr =
362                                 (struct ieee80211s_hdr *) (skb->data + hdrlen);
363                         u8 mesh_flags;
364
365                         /* make sure meshdr->flags is on the linear part */
366                         if (!pskb_may_pull(skb, hdrlen + 1))
367                                 return -1;
368                         mesh_flags = meshdr->flags & MESH_FLAGS_AE;
369                         if (mesh_flags == MESH_FLAGS_AE_A4)
370                                 return -1;
371                         if (mesh_flags == MESH_FLAGS_AE_A5_A6) {
372                                 skb_copy_bits(skb, hdrlen +
373                                         offsetof(struct ieee80211s_hdr, eaddr1),
374                                         dst, ETH_ALEN);
375                                 skb_copy_bits(skb, hdrlen +
376                                         offsetof(struct ieee80211s_hdr, eaddr2),
377                                         src, ETH_ALEN);
378                         }
379                         hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
380                 }
381                 break;
382         case cpu_to_le16(IEEE80211_FCTL_FROMDS):
383                 if ((iftype != NL80211_IFTYPE_STATION &&
384                      iftype != NL80211_IFTYPE_P2P_CLIENT &&
385                      iftype != NL80211_IFTYPE_MESH_POINT) ||
386                     (is_multicast_ether_addr(dst) &&
387                      !compare_ether_addr(src, addr)))
388                         return -1;
389                 if (iftype == NL80211_IFTYPE_MESH_POINT) {
390                         struct ieee80211s_hdr *meshdr =
391                                 (struct ieee80211s_hdr *) (skb->data + hdrlen);
392                         u8 mesh_flags;
393
394                         /* make sure meshdr->flags is on the linear part */
395                         if (!pskb_may_pull(skb, hdrlen + 1))
396                                 return -1;
397                         mesh_flags = meshdr->flags & MESH_FLAGS_AE;
398                         if (mesh_flags == MESH_FLAGS_AE_A5_A6)
399                                 return -1;
400                         if (mesh_flags == MESH_FLAGS_AE_A4)
401                                 skb_copy_bits(skb, hdrlen +
402                                         offsetof(struct ieee80211s_hdr, eaddr1),
403                                         src, ETH_ALEN);
404                         hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
405                 }
406                 break;
407         case cpu_to_le16(0):
408                 if (iftype != NL80211_IFTYPE_ADHOC &&
409                     iftype != NL80211_IFTYPE_STATION)
410                                 return -1;
411                 break;
412         }
413
414         if (!pskb_may_pull(skb, hdrlen + 8))
415                 return -1;
416
417         payload = skb->data + hdrlen;
418         ethertype = (payload[6] << 8) | payload[7];
419
420         if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
421                     ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
422                    compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
423                 /* remove RFC1042 or Bridge-Tunnel encapsulation and
424                  * replace EtherType */
425                 skb_pull(skb, hdrlen + 6);
426                 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
427                 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
428         } else {
429                 struct ethhdr *ehdr;
430                 __be16 len;
431
432                 skb_pull(skb, hdrlen);
433                 len = htons(skb->len);
434                 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
435                 memcpy(ehdr->h_dest, dst, ETH_ALEN);
436                 memcpy(ehdr->h_source, src, ETH_ALEN);
437                 ehdr->h_proto = len;
438         }
439         return 0;
440 }
441 EXPORT_SYMBOL(ieee80211_data_to_8023);
442
443 int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
444                              enum nl80211_iftype iftype, u8 *bssid, bool qos)
445 {
446         struct ieee80211_hdr hdr;
447         u16 hdrlen, ethertype;
448         __le16 fc;
449         const u8 *encaps_data;
450         int encaps_len, skip_header_bytes;
451         int nh_pos, h_pos;
452         int head_need;
453
454         if (unlikely(skb->len < ETH_HLEN))
455                 return -EINVAL;
456
457         nh_pos = skb_network_header(skb) - skb->data;
458         h_pos = skb_transport_header(skb) - skb->data;
459
460         /* convert Ethernet header to proper 802.11 header (based on
461          * operation mode) */
462         ethertype = (skb->data[12] << 8) | skb->data[13];
463         fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
464
465         switch (iftype) {
466         case NL80211_IFTYPE_AP:
467         case NL80211_IFTYPE_AP_VLAN:
468         case NL80211_IFTYPE_P2P_GO:
469                 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
470                 /* DA BSSID SA */
471                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
472                 memcpy(hdr.addr2, addr, ETH_ALEN);
473                 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
474                 hdrlen = 24;
475                 break;
476         case NL80211_IFTYPE_STATION:
477         case NL80211_IFTYPE_P2P_CLIENT:
478                 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
479                 /* BSSID SA DA */
480                 memcpy(hdr.addr1, bssid, ETH_ALEN);
481                 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
482                 memcpy(hdr.addr3, skb->data, ETH_ALEN);
483                 hdrlen = 24;
484                 break;
485         case NL80211_IFTYPE_ADHOC:
486                 /* DA SA BSSID */
487                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
488                 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
489                 memcpy(hdr.addr3, bssid, ETH_ALEN);
490                 hdrlen = 24;
491                 break;
492         default:
493                 return -EOPNOTSUPP;
494         }
495
496         if (qos) {
497                 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
498                 hdrlen += 2;
499         }
500
501         hdr.frame_control = fc;
502         hdr.duration_id = 0;
503         hdr.seq_ctrl = 0;
504
505         skip_header_bytes = ETH_HLEN;
506         if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
507                 encaps_data = bridge_tunnel_header;
508                 encaps_len = sizeof(bridge_tunnel_header);
509                 skip_header_bytes -= 2;
510         } else if (ethertype > 0x600) {
511                 encaps_data = rfc1042_header;
512                 encaps_len = sizeof(rfc1042_header);
513                 skip_header_bytes -= 2;
514         } else {
515                 encaps_data = NULL;
516                 encaps_len = 0;
517         }
518
519         skb_pull(skb, skip_header_bytes);
520         nh_pos -= skip_header_bytes;
521         h_pos -= skip_header_bytes;
522
523         head_need = hdrlen + encaps_len - skb_headroom(skb);
524
525         if (head_need > 0 || skb_cloned(skb)) {
526                 head_need = max(head_need, 0);
527                 if (head_need)
528                         skb_orphan(skb);
529
530                 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
531                         return -ENOMEM;
532
533                 skb->truesize += head_need;
534         }
535
536         if (encaps_data) {
537                 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
538                 nh_pos += encaps_len;
539                 h_pos += encaps_len;
540         }
541
542         memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
543
544         nh_pos += hdrlen;
545         h_pos += hdrlen;
546
547         /* Update skb pointers to various headers since this modified frame
548          * is going to go through Linux networking code that may potentially
549          * need things like pointer to IP header. */
550         skb_set_mac_header(skb, 0);
551         skb_set_network_header(skb, nh_pos);
552         skb_set_transport_header(skb, h_pos);
553
554         return 0;
555 }
556 EXPORT_SYMBOL(ieee80211_data_from_8023);
557
558
559 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
560                               const u8 *addr, enum nl80211_iftype iftype,
561                               const unsigned int extra_headroom,
562                               bool has_80211_header)
563 {
564         struct sk_buff *frame = NULL;
565         u16 ethertype;
566         u8 *payload;
567         const struct ethhdr *eth;
568         int remaining, err;
569         u8 dst[ETH_ALEN], src[ETH_ALEN];
570
571         if (has_80211_header) {
572                 err = ieee80211_data_to_8023(skb, addr, iftype);
573                 if (err)
574                         goto out;
575
576                 /* skip the wrapping header */
577                 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
578                 if (!eth)
579                         goto out;
580         } else {
581                 eth = (struct ethhdr *) skb->data;
582         }
583
584         while (skb != frame) {
585                 u8 padding;
586                 __be16 len = eth->h_proto;
587                 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
588
589                 remaining = skb->len;
590                 memcpy(dst, eth->h_dest, ETH_ALEN);
591                 memcpy(src, eth->h_source, ETH_ALEN);
592
593                 padding = (4 - subframe_len) & 0x3;
594                 /* the last MSDU has no padding */
595                 if (subframe_len > remaining)
596                         goto purge;
597
598                 skb_pull(skb, sizeof(struct ethhdr));
599                 /* reuse skb for the last subframe */
600                 if (remaining <= subframe_len + padding)
601                         frame = skb;
602                 else {
603                         unsigned int hlen = ALIGN(extra_headroom, 4);
604                         /*
605                          * Allocate and reserve two bytes more for payload
606                          * alignment since sizeof(struct ethhdr) is 14.
607                          */
608                         frame = dev_alloc_skb(hlen + subframe_len + 2);
609                         if (!frame)
610                                 goto purge;
611
612                         skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
613                         memcpy(skb_put(frame, ntohs(len)), skb->data,
614                                 ntohs(len));
615
616                         eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
617                                                         padding);
618                         if (!eth) {
619                                 dev_kfree_skb(frame);
620                                 goto purge;
621                         }
622                 }
623
624                 skb_reset_network_header(frame);
625                 frame->dev = skb->dev;
626                 frame->priority = skb->priority;
627
628                 payload = frame->data;
629                 ethertype = (payload[6] << 8) | payload[7];
630
631                 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
632                             ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
633                            compare_ether_addr(payload,
634                                               bridge_tunnel_header) == 0)) {
635                         /* remove RFC1042 or Bridge-Tunnel
636                          * encapsulation and replace EtherType */
637                         skb_pull(frame, 6);
638                         memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
639                         memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
640                 } else {
641                         memcpy(skb_push(frame, sizeof(__be16)), &len,
642                                 sizeof(__be16));
643                         memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
644                         memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
645                 }
646                 __skb_queue_tail(list, frame);
647         }
648
649         return;
650
651  purge:
652         __skb_queue_purge(list);
653  out:
654         dev_kfree_skb(skb);
655 }
656 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
657
658 /* Given a data frame determine the 802.1p/1d tag to use. */
659 unsigned int cfg80211_classify8021d(struct sk_buff *skb)
660 {
661         unsigned int dscp;
662
663         /* skb->priority values from 256->263 are magic values to
664          * directly indicate a specific 802.1d priority.  This is used
665          * to allow 802.1d priority to be passed directly in from VLAN
666          * tags, etc.
667          */
668         if (skb->priority >= 256 && skb->priority <= 263)
669                 return skb->priority - 256;
670
671         switch (skb->protocol) {
672         case htons(ETH_P_IP):
673                 dscp = ip_hdr(skb)->tos & 0xfc;
674                 break;
675         default:
676                 return 0;
677         }
678
679         return dscp >> 5;
680 }
681 EXPORT_SYMBOL(cfg80211_classify8021d);
682
683 const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
684 {
685         u8 *end, *pos;
686
687         pos = bss->information_elements;
688         if (pos == NULL)
689                 return NULL;
690         end = pos + bss->len_information_elements;
691
692         while (pos + 1 < end) {
693                 if (pos + 2 + pos[1] > end)
694                         break;
695                 if (pos[0] == ie)
696                         return pos;
697                 pos += 2 + pos[1];
698         }
699
700         return NULL;
701 }
702 EXPORT_SYMBOL(ieee80211_bss_get_ie);
703
704 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
705 {
706         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
707         struct net_device *dev = wdev->netdev;
708         int i;
709
710         if (!wdev->connect_keys)
711                 return;
712
713         for (i = 0; i < 6; i++) {
714                 if (!wdev->connect_keys->params[i].cipher)
715                         continue;
716                 if (rdev->ops->add_key(wdev->wiphy, dev, i, false, NULL,
717                                         &wdev->connect_keys->params[i])) {
718                         netdev_err(dev, "failed to set key %d\n", i);
719                         continue;
720                 }
721                 if (wdev->connect_keys->def == i)
722                         if (rdev->ops->set_default_key(wdev->wiphy, dev,
723                                                        i, true, true)) {
724                                 netdev_err(dev, "failed to set defkey %d\n", i);
725                                 continue;
726                         }
727                 if (wdev->connect_keys->defmgmt == i)
728                         if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
729                                 netdev_err(dev, "failed to set mgtdef %d\n", i);
730         }
731
732         kfree(wdev->connect_keys);
733         wdev->connect_keys = NULL;
734 }
735
736 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
737 {
738         struct cfg80211_event *ev;
739         unsigned long flags;
740         const u8 *bssid = NULL;
741
742         spin_lock_irqsave(&wdev->event_lock, flags);
743         while (!list_empty(&wdev->event_list)) {
744                 ev = list_first_entry(&wdev->event_list,
745                                       struct cfg80211_event, list);
746                 list_del(&ev->list);
747                 spin_unlock_irqrestore(&wdev->event_lock, flags);
748
749                 wdev_lock(wdev);
750                 switch (ev->type) {
751                 case EVENT_CONNECT_RESULT:
752                         if (!is_zero_ether_addr(ev->cr.bssid))
753                                 bssid = ev->cr.bssid;
754                         __cfg80211_connect_result(
755                                 wdev->netdev, bssid,
756                                 ev->cr.req_ie, ev->cr.req_ie_len,
757                                 ev->cr.resp_ie, ev->cr.resp_ie_len,
758                                 ev->cr.status,
759                                 ev->cr.status == WLAN_STATUS_SUCCESS,
760                                 NULL);
761                         break;
762                 case EVENT_ROAMED:
763                         __cfg80211_roamed(wdev, ev->rm.channel, ev->rm.bssid,
764                                           ev->rm.req_ie, ev->rm.req_ie_len,
765                                           ev->rm.resp_ie, ev->rm.resp_ie_len);
766                         break;
767                 case EVENT_DISCONNECTED:
768                         __cfg80211_disconnected(wdev->netdev,
769                                                 ev->dc.ie, ev->dc.ie_len,
770                                                 ev->dc.reason, true);
771                         break;
772                 case EVENT_IBSS_JOINED:
773                         __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
774                         break;
775                 }
776                 wdev_unlock(wdev);
777
778                 kfree(ev);
779
780                 spin_lock_irqsave(&wdev->event_lock, flags);
781         }
782         spin_unlock_irqrestore(&wdev->event_lock, flags);
783 }
784
785 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
786 {
787         struct wireless_dev *wdev;
788
789         ASSERT_RTNL();
790         ASSERT_RDEV_LOCK(rdev);
791
792         mutex_lock(&rdev->devlist_mtx);
793
794         list_for_each_entry(wdev, &rdev->netdev_list, list)
795                 cfg80211_process_wdev_events(wdev);
796
797         mutex_unlock(&rdev->devlist_mtx);
798 }
799
800 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
801                           struct net_device *dev, enum nl80211_iftype ntype,
802                           u32 *flags, struct vif_params *params)
803 {
804         int err;
805         enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
806
807         ASSERT_RDEV_LOCK(rdev);
808
809         /* don't support changing VLANs, you just re-create them */
810         if (otype == NL80211_IFTYPE_AP_VLAN)
811                 return -EOPNOTSUPP;
812
813         if (!rdev->ops->change_virtual_intf ||
814             !(rdev->wiphy.interface_modes & (1 << ntype)))
815                 return -EOPNOTSUPP;
816
817         /* if it's part of a bridge, reject changing type to station/ibss */
818         if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
819             (ntype == NL80211_IFTYPE_ADHOC ||
820              ntype == NL80211_IFTYPE_STATION ||
821              ntype == NL80211_IFTYPE_P2P_CLIENT))
822                 return -EBUSY;
823
824         if (ntype != otype && netif_running(dev)) {
825                 err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
826                                                     ntype);
827                 if (err)
828                         return err;
829
830                 dev->ieee80211_ptr->use_4addr = false;
831                 dev->ieee80211_ptr->mesh_id_up_len = 0;
832
833                 switch (otype) {
834                 case NL80211_IFTYPE_ADHOC:
835                         cfg80211_leave_ibss(rdev, dev, false);
836                         break;
837                 case NL80211_IFTYPE_STATION:
838                 case NL80211_IFTYPE_P2P_CLIENT:
839                         cfg80211_disconnect(rdev, dev,
840                                             WLAN_REASON_DEAUTH_LEAVING, true);
841                         break;
842                 case NL80211_IFTYPE_MESH_POINT:
843                         /* mesh should be handled? */
844                         break;
845                 default:
846                         break;
847                 }
848
849                 cfg80211_process_rdev_events(rdev);
850         }
851
852         err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
853                                              ntype, flags, params);
854
855         WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
856
857         if (!err && params && params->use_4addr != -1)
858                 dev->ieee80211_ptr->use_4addr = params->use_4addr;
859
860         if (!err) {
861                 dev->priv_flags &= ~IFF_DONT_BRIDGE;
862                 switch (ntype) {
863                 case NL80211_IFTYPE_STATION:
864                         if (dev->ieee80211_ptr->use_4addr)
865                                 break;
866                         /* fall through */
867                 case NL80211_IFTYPE_P2P_CLIENT:
868                 case NL80211_IFTYPE_ADHOC:
869                         dev->priv_flags |= IFF_DONT_BRIDGE;
870                         break;
871                 case NL80211_IFTYPE_P2P_GO:
872                 case NL80211_IFTYPE_AP:
873                 case NL80211_IFTYPE_AP_VLAN:
874                 case NL80211_IFTYPE_WDS:
875                 case NL80211_IFTYPE_MESH_POINT:
876                         /* bridging OK */
877                         break;
878                 case NL80211_IFTYPE_MONITOR:
879                         /* monitor can't bridge anyway */
880                         break;
881                 case NL80211_IFTYPE_UNSPECIFIED:
882                 case NUM_NL80211_IFTYPES:
883                         /* not happening */
884                         break;
885                 }
886         }
887
888         return err;
889 }
890
891 u16 cfg80211_calculate_bitrate(struct rate_info *rate)
892 {
893         int modulation, streams, bitrate;
894
895         if (!(rate->flags & RATE_INFO_FLAGS_MCS))
896                 return rate->legacy;
897
898         /* the formula below does only work for MCS values smaller than 32 */
899         if (rate->mcs >= 32)
900                 return 0;
901
902         modulation = rate->mcs & 7;
903         streams = (rate->mcs >> 3) + 1;
904
905         bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
906                         13500000 : 6500000;
907
908         if (modulation < 4)
909                 bitrate *= (modulation + 1);
910         else if (modulation == 4)
911                 bitrate *= (modulation + 2);
912         else
913                 bitrate *= (modulation + 3);
914
915         bitrate *= streams;
916
917         if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
918                 bitrate = (bitrate / 9) * 10;
919
920         /* do NOT round down here */
921         return (bitrate + 50000) / 100000;
922 }
923
924 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
925                                  u32 beacon_int)
926 {
927         struct wireless_dev *wdev;
928         int res = 0;
929
930         if (!beacon_int)
931                 return -EINVAL;
932
933         mutex_lock(&rdev->devlist_mtx);
934
935         list_for_each_entry(wdev, &rdev->netdev_list, list) {
936                 if (!wdev->beacon_interval)
937                         continue;
938                 if (wdev->beacon_interval != beacon_int) {
939                         res = -EINVAL;
940                         break;
941                 }
942         }
943
944         mutex_unlock(&rdev->devlist_mtx);
945
946         return res;
947 }
948
949 int cfg80211_can_change_interface(struct cfg80211_registered_device *rdev,
950                                   struct wireless_dev *wdev,
951                                   enum nl80211_iftype iftype)
952 {
953         struct wireless_dev *wdev_iter;
954         u32 used_iftypes = BIT(iftype);
955         int num[NUM_NL80211_IFTYPES];
956         int total = 1;
957         int i, j;
958
959         ASSERT_RTNL();
960
961         /* Always allow software iftypes */
962         if (rdev->wiphy.software_iftypes & BIT(iftype))
963                 return 0;
964
965         /*
966          * Drivers will gradually all set this flag, until all
967          * have it we only enforce for those that set it.
968          */
969         if (!(rdev->wiphy.flags & WIPHY_FLAG_ENFORCE_COMBINATIONS))
970                 return 0;
971
972         memset(num, 0, sizeof(num));
973
974         num[iftype] = 1;
975
976         mutex_lock(&rdev->devlist_mtx);
977         list_for_each_entry(wdev_iter, &rdev->netdev_list, list) {
978                 if (wdev_iter == wdev)
979                         continue;
980                 if (!netif_running(wdev_iter->netdev))
981                         continue;
982
983                 if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
984                         continue;
985
986                 num[wdev_iter->iftype]++;
987                 total++;
988                 used_iftypes |= BIT(wdev_iter->iftype);
989         }
990         mutex_unlock(&rdev->devlist_mtx);
991
992         if (total == 1)
993                 return 0;
994
995         for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
996                 const struct ieee80211_iface_combination *c;
997                 struct ieee80211_iface_limit *limits;
998                 u32 all_iftypes = 0;
999
1000                 c = &rdev->wiphy.iface_combinations[i];
1001
1002                 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1003                                  GFP_KERNEL);
1004                 if (!limits)
1005                         return -ENOMEM;
1006                 if (total > c->max_interfaces)
1007                         goto cont;
1008
1009                 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1010                         if (rdev->wiphy.software_iftypes & BIT(iftype))
1011                                 continue;
1012                         for (j = 0; j < c->n_limits; j++) {
1013                                 all_iftypes |= limits[j].types;
1014                                 if (!(limits[j].types & BIT(iftype)))
1015                                         continue;
1016                                 if (limits[j].max < num[iftype])
1017                                         goto cont;
1018                                 limits[j].max -= num[iftype];
1019                         }
1020                 }
1021
1022                 /*
1023                  * Finally check that all iftypes that we're currently
1024                  * using are actually part of this combination. If they
1025                  * aren't then we can't use this combination and have
1026                  * to continue to the next.
1027                  */
1028                 if ((all_iftypes & used_iftypes) != used_iftypes)
1029                         goto cont;
1030
1031                 /*
1032                  * This combination covered all interface types and
1033                  * supported the requested numbers, so we're good.
1034                  */
1035                 kfree(limits);
1036                 return 0;
1037  cont:
1038                 kfree(limits);
1039         }
1040
1041         return -EBUSY;
1042 }
1043
1044 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1045                            const u8 *rates, unsigned int n_rates,
1046                            u32 *mask)
1047 {
1048         int i, j;
1049
1050         if (!sband)
1051                 return -EINVAL;
1052
1053         if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1054                 return -EINVAL;
1055
1056         *mask = 0;
1057
1058         for (i = 0; i < n_rates; i++) {
1059                 int rate = (rates[i] & 0x7f) * 5;
1060                 bool found = false;
1061
1062                 for (j = 0; j < sband->n_bitrates; j++) {
1063                         if (sband->bitrates[j].bitrate == rate) {
1064                                 found = true;
1065                                 *mask |= BIT(j);
1066                                 break;
1067                         }
1068                 }
1069                 if (!found)
1070                         return -EINVAL;
1071         }
1072
1073         /*
1074          * mask must have at least one bit set here since we
1075          * didn't accept a 0-length rates array nor allowed
1076          * entries in the array that didn't exist
1077          */
1078
1079         return 0;
1080 }
1081
1082 u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
1083                                struct ieee802_11_elems *elems,
1084                                u64 filter, u32 crc)
1085 {
1086         size_t left = len;
1087         u8 *pos = start;
1088         bool calc_crc = filter != 0;
1089
1090         memset(elems, 0, sizeof(*elems));
1091         elems->ie_start = start;
1092         elems->total_len = len;
1093
1094         while (left >= 2) {
1095                 u8 id, elen;
1096
1097                 id = *pos++;
1098                 elen = *pos++;
1099                 left -= 2;
1100
1101                 if (elen > left)
1102                         break;
1103
1104                 if (calc_crc && id < 64 && (filter & (1ULL << id)))
1105                         crc = crc32_be(crc, pos - 2, elen + 2);
1106
1107                 switch (id) {
1108                 case WLAN_EID_SSID:
1109                         elems->ssid = pos;
1110                         elems->ssid_len = elen;
1111                         break;
1112                 case WLAN_EID_SUPP_RATES:
1113                         elems->supp_rates = pos;
1114                         elems->supp_rates_len = elen;
1115                         break;
1116                 case WLAN_EID_FH_PARAMS:
1117                         elems->fh_params = pos;
1118                         elems->fh_params_len = elen;
1119                         break;
1120                 case WLAN_EID_DS_PARAMS:
1121                         elems->ds_params = pos;
1122                         elems->ds_params_len = elen;
1123                         break;
1124                 case WLAN_EID_CF_PARAMS:
1125                         elems->cf_params = pos;
1126                         elems->cf_params_len = elen;
1127                         break;
1128                 case WLAN_EID_TIM:
1129                         if (elen >= sizeof(struct ieee80211_tim_ie)) {
1130                                 elems->tim = (void *)pos;
1131                                 elems->tim_len = elen;
1132                         }
1133                         break;
1134                 case WLAN_EID_IBSS_PARAMS:
1135                         elems->ibss_params = pos;
1136                         elems->ibss_params_len = elen;
1137                         break;
1138                 case WLAN_EID_CHALLENGE:
1139                         elems->challenge = pos;
1140                         elems->challenge_len = elen;
1141                         break;
1142                 case WLAN_EID_VENDOR_SPECIFIC:
1143                         if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
1144                             pos[2] == 0xf2) {
1145                                 /* Microsoft OUI (00:50:F2) */
1146
1147                                 if (calc_crc)
1148                                         crc = crc32_be(crc, pos - 2, elen + 2);
1149
1150                                 if (pos[3] == 1) {
1151                                         /* OUI Type 1 - WPA IE */
1152                                         elems->wpa = pos;
1153                                         elems->wpa_len = elen;
1154                                 } else if (elen >= 5 && pos[3] == 2) {
1155                                         /* OUI Type 2 - WMM IE */
1156                                         if (pos[4] == 0) {
1157                                                 elems->wmm_info = pos;
1158                                                 elems->wmm_info_len = elen;
1159                                         } else if (pos[4] == 1) {
1160                                                 elems->wmm_param = pos;
1161                                                 elems->wmm_param_len = elen;
1162                                         }
1163                                 }
1164                         }
1165                         break;
1166                 case WLAN_EID_RSN:
1167                         elems->rsn = pos;
1168                         elems->rsn_len = elen;
1169                         break;
1170                 case WLAN_EID_ERP_INFO:
1171                         elems->erp_info = pos;
1172                         elems->erp_info_len = elen;
1173                         break;
1174                 case WLAN_EID_EXT_SUPP_RATES:
1175                         elems->ext_supp_rates = pos;
1176                         elems->ext_supp_rates_len = elen;
1177                         break;
1178                 case WLAN_EID_HT_CAPABILITY:
1179                         if (elen >= sizeof(struct ieee80211_ht_cap))
1180                                 elems->ht_cap_elem = (void *)pos;
1181                         break;
1182                 case WLAN_EID_HT_INFORMATION:
1183                         if (elen >= sizeof(struct ieee80211_ht_info))
1184                                 elems->ht_info_elem = (void *)pos;
1185                         break;
1186                 case WLAN_EID_MESH_ID:
1187                         elems->mesh_id = pos;
1188                         elems->mesh_id_len = elen;
1189                         break;
1190                 case WLAN_EID_MESH_CONFIG:
1191                         if (elen >= sizeof(struct ieee80211_meshconf_ie))
1192                                 elems->mesh_config = (void *)pos;
1193                         break;
1194                 case WLAN_EID_PEER_MGMT:
1195                         elems->peering = pos;
1196                         elems->peering_len = elen;
1197                         break;
1198                 case WLAN_EID_PREQ:
1199                         elems->preq = pos;
1200                         elems->preq_len = elen;
1201                         break;
1202                 case WLAN_EID_PREP:
1203                         elems->prep = pos;
1204                         elems->prep_len = elen;
1205                         break;
1206                 case WLAN_EID_PERR:
1207                         elems->perr = pos;
1208                         elems->perr_len = elen;
1209                         break;
1210                 case WLAN_EID_RANN:
1211                         if (elen >= sizeof(struct ieee80211_rann_ie))
1212                                 elems->rann = (void *)pos;
1213                         break;
1214                 case WLAN_EID_CHANNEL_SWITCH:
1215                         elems->ch_switch_elem = pos;
1216                         elems->ch_switch_elem_len = elen;
1217                         break;
1218                 case WLAN_EID_QUIET:
1219                         if (!elems->quiet_elem) {
1220                                 elems->quiet_elem = pos;
1221                                 elems->quiet_elem_len = elen;
1222                         }
1223                         elems->num_of_quiet_elem++;
1224                         break;
1225                 case WLAN_EID_COUNTRY:
1226                         elems->country_elem = pos;
1227                         elems->country_elem_len = elen;
1228                         break;
1229                 case WLAN_EID_PWR_CONSTRAINT:
1230                         elems->pwr_constr_elem = pos;
1231                         elems->pwr_constr_elem_len = elen;
1232                         break;
1233                 case WLAN_EID_TIMEOUT_INTERVAL:
1234                         elems->timeout_int = pos;
1235                         elems->timeout_int_len = elen;
1236                         break;
1237                 default:
1238                         break;
1239                 }
1240
1241                 left -= elen;
1242                 pos += elen;
1243         }
1244
1245         return crc;
1246 }
1247 EXPORT_SYMBOL(ieee802_11_parse_elems_crc);