mac80211: clean up ifdown/cleanup paths
[cascardo/linux.git] / net / wireless / util.c
1 /*
2  * Wireless utility functions
3  *
4  * Copyright 2007-2009  Johannes Berg <johannes@sipsolutions.net>
5  */
6 #include <linux/bitops.h>
7 #include <linux/etherdevice.h>
8 #include <linux/slab.h>
9 #include <net/cfg80211.h>
10 #include <net/ip.h>
11 #include "core.h"
12
13 struct ieee80211_rate *
14 ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
15                             u32 basic_rates, int bitrate)
16 {
17         struct ieee80211_rate *result = &sband->bitrates[0];
18         int i;
19
20         for (i = 0; i < sband->n_bitrates; i++) {
21                 if (!(basic_rates & BIT(i)))
22                         continue;
23                 if (sband->bitrates[i].bitrate > bitrate)
24                         continue;
25                 result = &sband->bitrates[i];
26         }
27
28         return result;
29 }
30 EXPORT_SYMBOL(ieee80211_get_response_rate);
31
32 int ieee80211_channel_to_frequency(int chan)
33 {
34         if (chan < 14)
35                 return 2407 + chan * 5;
36
37         if (chan == 14)
38                 return 2484;
39
40         /* FIXME: 802.11j 17.3.8.3.2 */
41         return (chan + 1000) * 5;
42 }
43 EXPORT_SYMBOL(ieee80211_channel_to_frequency);
44
45 int ieee80211_frequency_to_channel(int freq)
46 {
47         if (freq == 2484)
48                 return 14;
49
50         if (freq < 2484)
51                 return (freq - 2407) / 5;
52
53         /* FIXME: 802.11j 17.3.8.3.2 */
54         return freq/5 - 1000;
55 }
56 EXPORT_SYMBOL(ieee80211_frequency_to_channel);
57
58 struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
59                                                   int freq)
60 {
61         enum ieee80211_band band;
62         struct ieee80211_supported_band *sband;
63         int i;
64
65         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
66                 sband = wiphy->bands[band];
67
68                 if (!sband)
69                         continue;
70
71                 for (i = 0; i < sband->n_channels; i++) {
72                         if (sband->channels[i].center_freq == freq)
73                                 return &sband->channels[i];
74                 }
75         }
76
77         return NULL;
78 }
79 EXPORT_SYMBOL(__ieee80211_get_channel);
80
81 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
82                                      enum ieee80211_band band)
83 {
84         int i, want;
85
86         switch (band) {
87         case IEEE80211_BAND_5GHZ:
88                 want = 3;
89                 for (i = 0; i < sband->n_bitrates; i++) {
90                         if (sband->bitrates[i].bitrate == 60 ||
91                             sband->bitrates[i].bitrate == 120 ||
92                             sband->bitrates[i].bitrate == 240) {
93                                 sband->bitrates[i].flags |=
94                                         IEEE80211_RATE_MANDATORY_A;
95                                 want--;
96                         }
97                 }
98                 WARN_ON(want);
99                 break;
100         case IEEE80211_BAND_2GHZ:
101                 want = 7;
102                 for (i = 0; i < sband->n_bitrates; i++) {
103                         if (sband->bitrates[i].bitrate == 10) {
104                                 sband->bitrates[i].flags |=
105                                         IEEE80211_RATE_MANDATORY_B |
106                                         IEEE80211_RATE_MANDATORY_G;
107                                 want--;
108                         }
109
110                         if (sband->bitrates[i].bitrate == 20 ||
111                             sband->bitrates[i].bitrate == 55 ||
112                             sband->bitrates[i].bitrate == 110 ||
113                             sband->bitrates[i].bitrate == 60 ||
114                             sband->bitrates[i].bitrate == 120 ||
115                             sband->bitrates[i].bitrate == 240) {
116                                 sband->bitrates[i].flags |=
117                                         IEEE80211_RATE_MANDATORY_G;
118                                 want--;
119                         }
120
121                         if (sband->bitrates[i].bitrate != 10 &&
122                             sband->bitrates[i].bitrate != 20 &&
123                             sband->bitrates[i].bitrate != 55 &&
124                             sband->bitrates[i].bitrate != 110)
125                                 sband->bitrates[i].flags |=
126                                         IEEE80211_RATE_ERP_G;
127                 }
128                 WARN_ON(want != 0 && want != 3 && want != 6);
129                 break;
130         case IEEE80211_NUM_BANDS:
131                 WARN_ON(1);
132                 break;
133         }
134 }
135
136 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
137 {
138         enum ieee80211_band band;
139
140         for (band = 0; band < IEEE80211_NUM_BANDS; band++)
141                 if (wiphy->bands[band])
142                         set_mandatory_flags_band(wiphy->bands[band], band);
143 }
144
145 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
146                                    struct key_params *params, int key_idx,
147                                    const u8 *mac_addr)
148 {
149         int i;
150
151         if (key_idx > 5)
152                 return -EINVAL;
153
154         /*
155          * Disallow pairwise keys with non-zero index unless it's WEP
156          * (because current deployments use pairwise WEP keys with
157          * non-zero indizes but 802.11i clearly specifies to use zero)
158          */
159         if (mac_addr && key_idx &&
160             params->cipher != WLAN_CIPHER_SUITE_WEP40 &&
161             params->cipher != WLAN_CIPHER_SUITE_WEP104)
162                 return -EINVAL;
163
164         switch (params->cipher) {
165         case WLAN_CIPHER_SUITE_WEP40:
166                 if (params->key_len != WLAN_KEY_LEN_WEP40)
167                         return -EINVAL;
168                 break;
169         case WLAN_CIPHER_SUITE_TKIP:
170                 if (params->key_len != WLAN_KEY_LEN_TKIP)
171                         return -EINVAL;
172                 break;
173         case WLAN_CIPHER_SUITE_CCMP:
174                 if (params->key_len != WLAN_KEY_LEN_CCMP)
175                         return -EINVAL;
176                 break;
177         case WLAN_CIPHER_SUITE_WEP104:
178                 if (params->key_len != WLAN_KEY_LEN_WEP104)
179                         return -EINVAL;
180                 break;
181         case WLAN_CIPHER_SUITE_AES_CMAC:
182                 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
183                         return -EINVAL;
184                 break;
185         default:
186                 /*
187                  * We don't know anything about this algorithm,
188                  * allow using it -- but the driver must check
189                  * all parameters! We still check below whether
190                  * or not the driver supports this algorithm,
191                  * of course.
192                  */
193                 break;
194         }
195
196         if (params->seq) {
197                 switch (params->cipher) {
198                 case WLAN_CIPHER_SUITE_WEP40:
199                 case WLAN_CIPHER_SUITE_WEP104:
200                         /* These ciphers do not use key sequence */
201                         return -EINVAL;
202                 case WLAN_CIPHER_SUITE_TKIP:
203                 case WLAN_CIPHER_SUITE_CCMP:
204                 case WLAN_CIPHER_SUITE_AES_CMAC:
205                         if (params->seq_len != 6)
206                                 return -EINVAL;
207                         break;
208                 }
209         }
210
211         for (i = 0; i < rdev->wiphy.n_cipher_suites; i++)
212                 if (params->cipher == rdev->wiphy.cipher_suites[i])
213                         break;
214         if (i == rdev->wiphy.n_cipher_suites)
215                 return -EINVAL;
216
217         return 0;
218 }
219
220 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
221 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
222 const unsigned char rfc1042_header[] __aligned(2) =
223         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
224 EXPORT_SYMBOL(rfc1042_header);
225
226 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
227 const unsigned char bridge_tunnel_header[] __aligned(2) =
228         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
229 EXPORT_SYMBOL(bridge_tunnel_header);
230
231 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
232 {
233         unsigned int hdrlen = 24;
234
235         if (ieee80211_is_data(fc)) {
236                 if (ieee80211_has_a4(fc))
237                         hdrlen = 30;
238                 if (ieee80211_is_data_qos(fc)) {
239                         hdrlen += IEEE80211_QOS_CTL_LEN;
240                         if (ieee80211_has_order(fc))
241                                 hdrlen += IEEE80211_HT_CTL_LEN;
242                 }
243                 goto out;
244         }
245
246         if (ieee80211_is_ctl(fc)) {
247                 /*
248                  * ACK and CTS are 10 bytes, all others 16. To see how
249                  * to get this condition consider
250                  *   subtype mask:   0b0000000011110000 (0x00F0)
251                  *   ACK subtype:    0b0000000011010000 (0x00D0)
252                  *   CTS subtype:    0b0000000011000000 (0x00C0)
253                  *   bits that matter:         ^^^      (0x00E0)
254                  *   value of those: 0b0000000011000000 (0x00C0)
255                  */
256                 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
257                         hdrlen = 10;
258                 else
259                         hdrlen = 16;
260         }
261 out:
262         return hdrlen;
263 }
264 EXPORT_SYMBOL(ieee80211_hdrlen);
265
266 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
267 {
268         const struct ieee80211_hdr *hdr =
269                         (const struct ieee80211_hdr *)skb->data;
270         unsigned int hdrlen;
271
272         if (unlikely(skb->len < 10))
273                 return 0;
274         hdrlen = ieee80211_hdrlen(hdr->frame_control);
275         if (unlikely(hdrlen > skb->len))
276                 return 0;
277         return hdrlen;
278 }
279 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
280
281 static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
282 {
283         int ae = meshhdr->flags & MESH_FLAGS_AE;
284         /* 7.1.3.5a.2 */
285         switch (ae) {
286         case 0:
287                 return 6;
288         case MESH_FLAGS_AE_A4:
289                 return 12;
290         case MESH_FLAGS_AE_A5_A6:
291                 return 18;
292         case (MESH_FLAGS_AE_A4 | MESH_FLAGS_AE_A5_A6):
293                 return 24;
294         default:
295                 return 6;
296         }
297 }
298
299 int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
300                            enum nl80211_iftype iftype)
301 {
302         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
303         u16 hdrlen, ethertype;
304         u8 *payload;
305         u8 dst[ETH_ALEN];
306         u8 src[ETH_ALEN] __aligned(2);
307
308         if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
309                 return -1;
310
311         hdrlen = ieee80211_hdrlen(hdr->frame_control);
312
313         /* convert IEEE 802.11 header + possible LLC headers into Ethernet
314          * header
315          * IEEE 802.11 address fields:
316          * ToDS FromDS Addr1 Addr2 Addr3 Addr4
317          *   0     0   DA    SA    BSSID n/a
318          *   0     1   DA    BSSID SA    n/a
319          *   1     0   BSSID SA    DA    n/a
320          *   1     1   RA    TA    DA    SA
321          */
322         memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
323         memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
324
325         switch (hdr->frame_control &
326                 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
327         case cpu_to_le16(IEEE80211_FCTL_TODS):
328                 if (unlikely(iftype != NL80211_IFTYPE_AP &&
329                              iftype != NL80211_IFTYPE_AP_VLAN))
330                         return -1;
331                 break;
332         case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
333                 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
334                              iftype != NL80211_IFTYPE_MESH_POINT &&
335                              iftype != NL80211_IFTYPE_AP_VLAN &&
336                              iftype != NL80211_IFTYPE_STATION))
337                         return -1;
338                 if (iftype == NL80211_IFTYPE_MESH_POINT) {
339                         struct ieee80211s_hdr *meshdr =
340                                 (struct ieee80211s_hdr *) (skb->data + hdrlen);
341                         /* make sure meshdr->flags is on the linear part */
342                         if (!pskb_may_pull(skb, hdrlen + 1))
343                                 return -1;
344                         if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
345                                 skb_copy_bits(skb, hdrlen +
346                                         offsetof(struct ieee80211s_hdr, eaddr1),
347                                         dst, ETH_ALEN);
348                                 skb_copy_bits(skb, hdrlen +
349                                         offsetof(struct ieee80211s_hdr, eaddr2),
350                                         src, ETH_ALEN);
351                         }
352                         hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
353                 }
354                 break;
355         case cpu_to_le16(IEEE80211_FCTL_FROMDS):
356                 if ((iftype != NL80211_IFTYPE_STATION &&
357                     iftype != NL80211_IFTYPE_MESH_POINT) ||
358                     (is_multicast_ether_addr(dst) &&
359                      !compare_ether_addr(src, addr)))
360                         return -1;
361                 if (iftype == NL80211_IFTYPE_MESH_POINT) {
362                         struct ieee80211s_hdr *meshdr =
363                                 (struct ieee80211s_hdr *) (skb->data + hdrlen);
364                         /* make sure meshdr->flags is on the linear part */
365                         if (!pskb_may_pull(skb, hdrlen + 1))
366                                 return -1;
367                         if (meshdr->flags & MESH_FLAGS_AE_A4)
368                                 skb_copy_bits(skb, hdrlen +
369                                         offsetof(struct ieee80211s_hdr, eaddr1),
370                                         src, ETH_ALEN);
371                         hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
372                 }
373                 break;
374         case cpu_to_le16(0):
375                 if (iftype != NL80211_IFTYPE_ADHOC)
376                         return -1;
377                 break;
378         }
379
380         if (!pskb_may_pull(skb, hdrlen + 8))
381                 return -1;
382
383         payload = skb->data + hdrlen;
384         ethertype = (payload[6] << 8) | payload[7];
385
386         if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
387                     ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
388                    compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
389                 /* remove RFC1042 or Bridge-Tunnel encapsulation and
390                  * replace EtherType */
391                 skb_pull(skb, hdrlen + 6);
392                 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
393                 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
394         } else {
395                 struct ethhdr *ehdr;
396                 __be16 len;
397
398                 skb_pull(skb, hdrlen);
399                 len = htons(skb->len);
400                 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
401                 memcpy(ehdr->h_dest, dst, ETH_ALEN);
402                 memcpy(ehdr->h_source, src, ETH_ALEN);
403                 ehdr->h_proto = len;
404         }
405         return 0;
406 }
407 EXPORT_SYMBOL(ieee80211_data_to_8023);
408
409 int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
410                              enum nl80211_iftype iftype, u8 *bssid, bool qos)
411 {
412         struct ieee80211_hdr hdr;
413         u16 hdrlen, ethertype;
414         __le16 fc;
415         const u8 *encaps_data;
416         int encaps_len, skip_header_bytes;
417         int nh_pos, h_pos;
418         int head_need;
419
420         if (unlikely(skb->len < ETH_HLEN))
421                 return -EINVAL;
422
423         nh_pos = skb_network_header(skb) - skb->data;
424         h_pos = skb_transport_header(skb) - skb->data;
425
426         /* convert Ethernet header to proper 802.11 header (based on
427          * operation mode) */
428         ethertype = (skb->data[12] << 8) | skb->data[13];
429         fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
430
431         switch (iftype) {
432         case NL80211_IFTYPE_AP:
433         case NL80211_IFTYPE_AP_VLAN:
434                 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
435                 /* DA BSSID SA */
436                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
437                 memcpy(hdr.addr2, addr, ETH_ALEN);
438                 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
439                 hdrlen = 24;
440                 break;
441         case NL80211_IFTYPE_STATION:
442                 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
443                 /* BSSID SA DA */
444                 memcpy(hdr.addr1, bssid, ETH_ALEN);
445                 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
446                 memcpy(hdr.addr3, skb->data, ETH_ALEN);
447                 hdrlen = 24;
448                 break;
449         case NL80211_IFTYPE_ADHOC:
450                 /* DA SA BSSID */
451                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
452                 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
453                 memcpy(hdr.addr3, bssid, ETH_ALEN);
454                 hdrlen = 24;
455                 break;
456         default:
457                 return -EOPNOTSUPP;
458         }
459
460         if (qos) {
461                 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
462                 hdrlen += 2;
463         }
464
465         hdr.frame_control = fc;
466         hdr.duration_id = 0;
467         hdr.seq_ctrl = 0;
468
469         skip_header_bytes = ETH_HLEN;
470         if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
471                 encaps_data = bridge_tunnel_header;
472                 encaps_len = sizeof(bridge_tunnel_header);
473                 skip_header_bytes -= 2;
474         } else if (ethertype > 0x600) {
475                 encaps_data = rfc1042_header;
476                 encaps_len = sizeof(rfc1042_header);
477                 skip_header_bytes -= 2;
478         } else {
479                 encaps_data = NULL;
480                 encaps_len = 0;
481         }
482
483         skb_pull(skb, skip_header_bytes);
484         nh_pos -= skip_header_bytes;
485         h_pos -= skip_header_bytes;
486
487         head_need = hdrlen + encaps_len - skb_headroom(skb);
488
489         if (head_need > 0 || skb_cloned(skb)) {
490                 head_need = max(head_need, 0);
491                 if (head_need)
492                         skb_orphan(skb);
493
494                 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC)) {
495                         printk(KERN_ERR "failed to reallocate Tx buffer\n");
496                         return -ENOMEM;
497                 }
498                 skb->truesize += head_need;
499         }
500
501         if (encaps_data) {
502                 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
503                 nh_pos += encaps_len;
504                 h_pos += encaps_len;
505         }
506
507         memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
508
509         nh_pos += hdrlen;
510         h_pos += hdrlen;
511
512         /* Update skb pointers to various headers since this modified frame
513          * is going to go through Linux networking code that may potentially
514          * need things like pointer to IP header. */
515         skb_set_mac_header(skb, 0);
516         skb_set_network_header(skb, nh_pos);
517         skb_set_transport_header(skb, h_pos);
518
519         return 0;
520 }
521 EXPORT_SYMBOL(ieee80211_data_from_8023);
522
523
524 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
525                               const u8 *addr, enum nl80211_iftype iftype,
526                               const unsigned int extra_headroom)
527 {
528         struct sk_buff *frame = NULL;
529         u16 ethertype;
530         u8 *payload;
531         const struct ethhdr *eth;
532         int remaining, err;
533         u8 dst[ETH_ALEN], src[ETH_ALEN];
534
535         err = ieee80211_data_to_8023(skb, addr, iftype);
536         if (err)
537                 goto out;
538
539         /* skip the wrapping header */
540         eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
541         if (!eth)
542                 goto out;
543
544         while (skb != frame) {
545                 u8 padding;
546                 __be16 len = eth->h_proto;
547                 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
548
549                 remaining = skb->len;
550                 memcpy(dst, eth->h_dest, ETH_ALEN);
551                 memcpy(src, eth->h_source, ETH_ALEN);
552
553                 padding = (4 - subframe_len) & 0x3;
554                 /* the last MSDU has no padding */
555                 if (subframe_len > remaining)
556                         goto purge;
557
558                 skb_pull(skb, sizeof(struct ethhdr));
559                 /* reuse skb for the last subframe */
560                 if (remaining <= subframe_len + padding)
561                         frame = skb;
562                 else {
563                         unsigned int hlen = ALIGN(extra_headroom, 4);
564                         /*
565                          * Allocate and reserve two bytes more for payload
566                          * alignment since sizeof(struct ethhdr) is 14.
567                          */
568                         frame = dev_alloc_skb(hlen + subframe_len + 2);
569                         if (!frame)
570                                 goto purge;
571
572                         skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
573                         memcpy(skb_put(frame, ntohs(len)), skb->data,
574                                 ntohs(len));
575
576                         eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
577                                                         padding);
578                         if (!eth) {
579                                 dev_kfree_skb(frame);
580                                 goto purge;
581                         }
582                 }
583
584                 skb_reset_network_header(frame);
585                 frame->dev = skb->dev;
586                 frame->priority = skb->priority;
587
588                 payload = frame->data;
589                 ethertype = (payload[6] << 8) | payload[7];
590
591                 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
592                             ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
593                            compare_ether_addr(payload,
594                                               bridge_tunnel_header) == 0)) {
595                         /* remove RFC1042 or Bridge-Tunnel
596                          * encapsulation and replace EtherType */
597                         skb_pull(frame, 6);
598                         memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
599                         memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
600                 } else {
601                         memcpy(skb_push(frame, sizeof(__be16)), &len,
602                                 sizeof(__be16));
603                         memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
604                         memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
605                 }
606                 __skb_queue_tail(list, frame);
607         }
608
609         return;
610
611  purge:
612         __skb_queue_purge(list);
613  out:
614         dev_kfree_skb(skb);
615 }
616 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
617
618 /* Given a data frame determine the 802.1p/1d tag to use. */
619 unsigned int cfg80211_classify8021d(struct sk_buff *skb)
620 {
621         unsigned int dscp;
622
623         /* skb->priority values from 256->263 are magic values to
624          * directly indicate a specific 802.1d priority.  This is used
625          * to allow 802.1d priority to be passed directly in from VLAN
626          * tags, etc.
627          */
628         if (skb->priority >= 256 && skb->priority <= 263)
629                 return skb->priority - 256;
630
631         switch (skb->protocol) {
632         case htons(ETH_P_IP):
633                 dscp = ip_hdr(skb)->tos & 0xfc;
634                 break;
635         default:
636                 return 0;
637         }
638
639         return dscp >> 5;
640 }
641 EXPORT_SYMBOL(cfg80211_classify8021d);
642
643 const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
644 {
645         u8 *end, *pos;
646
647         pos = bss->information_elements;
648         if (pos == NULL)
649                 return NULL;
650         end = pos + bss->len_information_elements;
651
652         while (pos + 1 < end) {
653                 if (pos + 2 + pos[1] > end)
654                         break;
655                 if (pos[0] == ie)
656                         return pos;
657                 pos += 2 + pos[1];
658         }
659
660         return NULL;
661 }
662 EXPORT_SYMBOL(ieee80211_bss_get_ie);
663
664 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
665 {
666         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
667         struct net_device *dev = wdev->netdev;
668         int i;
669
670         if (!wdev->connect_keys)
671                 return;
672
673         for (i = 0; i < 6; i++) {
674                 if (!wdev->connect_keys->params[i].cipher)
675                         continue;
676                 if (rdev->ops->add_key(wdev->wiphy, dev, i, NULL,
677                                         &wdev->connect_keys->params[i])) {
678                         printk(KERN_ERR "%s: failed to set key %d\n",
679                                 dev->name, i);
680                         continue;
681                 }
682                 if (wdev->connect_keys->def == i)
683                         if (rdev->ops->set_default_key(wdev->wiphy, dev, i)) {
684                                 printk(KERN_ERR "%s: failed to set defkey %d\n",
685                                         dev->name, i);
686                                 continue;
687                         }
688                 if (wdev->connect_keys->defmgmt == i)
689                         if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
690                                 printk(KERN_ERR "%s: failed to set mgtdef %d\n",
691                                         dev->name, i);
692         }
693
694         kfree(wdev->connect_keys);
695         wdev->connect_keys = NULL;
696 }
697
698 static void cfg80211_process_wdev_events(struct wireless_dev *wdev)
699 {
700         struct cfg80211_event *ev;
701         unsigned long flags;
702         const u8 *bssid = NULL;
703
704         spin_lock_irqsave(&wdev->event_lock, flags);
705         while (!list_empty(&wdev->event_list)) {
706                 ev = list_first_entry(&wdev->event_list,
707                                       struct cfg80211_event, list);
708                 list_del(&ev->list);
709                 spin_unlock_irqrestore(&wdev->event_lock, flags);
710
711                 wdev_lock(wdev);
712                 switch (ev->type) {
713                 case EVENT_CONNECT_RESULT:
714                         if (!is_zero_ether_addr(ev->cr.bssid))
715                                 bssid = ev->cr.bssid;
716                         __cfg80211_connect_result(
717                                 wdev->netdev, bssid,
718                                 ev->cr.req_ie, ev->cr.req_ie_len,
719                                 ev->cr.resp_ie, ev->cr.resp_ie_len,
720                                 ev->cr.status,
721                                 ev->cr.status == WLAN_STATUS_SUCCESS,
722                                 NULL);
723                         break;
724                 case EVENT_ROAMED:
725                         __cfg80211_roamed(wdev, ev->rm.bssid,
726                                           ev->rm.req_ie, ev->rm.req_ie_len,
727                                           ev->rm.resp_ie, ev->rm.resp_ie_len);
728                         break;
729                 case EVENT_DISCONNECTED:
730                         __cfg80211_disconnected(wdev->netdev,
731                                                 ev->dc.ie, ev->dc.ie_len,
732                                                 ev->dc.reason, true);
733                         break;
734                 case EVENT_IBSS_JOINED:
735                         __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
736                         break;
737                 }
738                 wdev_unlock(wdev);
739
740                 kfree(ev);
741
742                 spin_lock_irqsave(&wdev->event_lock, flags);
743         }
744         spin_unlock_irqrestore(&wdev->event_lock, flags);
745 }
746
747 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
748 {
749         struct wireless_dev *wdev;
750
751         ASSERT_RTNL();
752         ASSERT_RDEV_LOCK(rdev);
753
754         mutex_lock(&rdev->devlist_mtx);
755
756         list_for_each_entry(wdev, &rdev->netdev_list, list)
757                 cfg80211_process_wdev_events(wdev);
758
759         mutex_unlock(&rdev->devlist_mtx);
760 }
761
762 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
763                           struct net_device *dev, enum nl80211_iftype ntype,
764                           u32 *flags, struct vif_params *params)
765 {
766         int err;
767         enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
768
769         ASSERT_RDEV_LOCK(rdev);
770
771         /* don't support changing VLANs, you just re-create them */
772         if (otype == NL80211_IFTYPE_AP_VLAN)
773                 return -EOPNOTSUPP;
774
775         if (!rdev->ops->change_virtual_intf ||
776             !(rdev->wiphy.interface_modes & (1 << ntype)))
777                 return -EOPNOTSUPP;
778
779         /* if it's part of a bridge, reject changing type to station/ibss */
780         if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
781             (ntype == NL80211_IFTYPE_ADHOC || ntype == NL80211_IFTYPE_STATION))
782                 return -EBUSY;
783
784         if (ntype != otype) {
785                 dev->ieee80211_ptr->use_4addr = false;
786
787                 switch (otype) {
788                 case NL80211_IFTYPE_ADHOC:
789                         cfg80211_leave_ibss(rdev, dev, false);
790                         break;
791                 case NL80211_IFTYPE_STATION:
792                         cfg80211_disconnect(rdev, dev,
793                                             WLAN_REASON_DEAUTH_LEAVING, true);
794                         break;
795                 case NL80211_IFTYPE_MESH_POINT:
796                         /* mesh should be handled? */
797                         break;
798                 default:
799                         break;
800                 }
801
802                 cfg80211_process_rdev_events(rdev);
803         }
804
805         err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
806                                              ntype, flags, params);
807
808         WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
809
810         if (!err && params && params->use_4addr != -1)
811                 dev->ieee80211_ptr->use_4addr = params->use_4addr;
812
813         if (!err) {
814                 dev->priv_flags &= ~IFF_DONT_BRIDGE;
815                 switch (ntype) {
816                 case NL80211_IFTYPE_STATION:
817                         if (dev->ieee80211_ptr->use_4addr)
818                                 break;
819                         /* fall through */
820                 case NL80211_IFTYPE_ADHOC:
821                         dev->priv_flags |= IFF_DONT_BRIDGE;
822                         break;
823                 case NL80211_IFTYPE_AP:
824                 case NL80211_IFTYPE_AP_VLAN:
825                 case NL80211_IFTYPE_WDS:
826                 case NL80211_IFTYPE_MESH_POINT:
827                         /* bridging OK */
828                         break;
829                 case NL80211_IFTYPE_MONITOR:
830                         /* monitor can't bridge anyway */
831                         break;
832                 case NL80211_IFTYPE_UNSPECIFIED:
833                 case NUM_NL80211_IFTYPES:
834                         /* not happening */
835                         break;
836                 }
837         }
838
839         return err;
840 }
841
842 u16 cfg80211_calculate_bitrate(struct rate_info *rate)
843 {
844         int modulation, streams, bitrate;
845
846         if (!(rate->flags & RATE_INFO_FLAGS_MCS))
847                 return rate->legacy;
848
849         /* the formula below does only work for MCS values smaller than 32 */
850         if (rate->mcs >= 32)
851                 return 0;
852
853         modulation = rate->mcs & 7;
854         streams = (rate->mcs >> 3) + 1;
855
856         bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
857                         13500000 : 6500000;
858
859         if (modulation < 4)
860                 bitrate *= (modulation + 1);
861         else if (modulation == 4)
862                 bitrate *= (modulation + 2);
863         else
864                 bitrate *= (modulation + 3);
865
866         bitrate *= streams;
867
868         if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
869                 bitrate = (bitrate / 9) * 10;
870
871         /* do NOT round down here */
872         return (bitrate + 50000) / 100000;
873 }