ALSA: hda - Check "beep" hint
[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/bitops.h>
7 #include <linux/etherdevice.h>
8 #include <net/cfg80211.h>
9 #include <net/ip.h>
10 #include "core.h"
11
12 struct ieee80211_rate *
13 ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
14                             u32 basic_rates, int bitrate)
15 {
16         struct ieee80211_rate *result = &sband->bitrates[0];
17         int i;
18
19         for (i = 0; i < sband->n_bitrates; i++) {
20                 if (!(basic_rates & BIT(i)))
21                         continue;
22                 if (sband->bitrates[i].bitrate > bitrate)
23                         continue;
24                 result = &sband->bitrates[i];
25         }
26
27         return result;
28 }
29 EXPORT_SYMBOL(ieee80211_get_response_rate);
30
31 int ieee80211_channel_to_frequency(int chan)
32 {
33         if (chan < 14)
34                 return 2407 + chan * 5;
35
36         if (chan == 14)
37                 return 2484;
38
39         /* FIXME: 802.11j 17.3.8.3.2 */
40         return (chan + 1000) * 5;
41 }
42 EXPORT_SYMBOL(ieee80211_channel_to_frequency);
43
44 int ieee80211_frequency_to_channel(int freq)
45 {
46         if (freq == 2484)
47                 return 14;
48
49         if (freq < 2484)
50                 return (freq - 2407) / 5;
51
52         /* FIXME: 802.11j 17.3.8.3.2 */
53         return freq/5 - 1000;
54 }
55 EXPORT_SYMBOL(ieee80211_frequency_to_channel);
56
57 struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
58                                                   int freq)
59 {
60         enum ieee80211_band band;
61         struct ieee80211_supported_band *sband;
62         int i;
63
64         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
65                 sband = wiphy->bands[band];
66
67                 if (!sband)
68                         continue;
69
70                 for (i = 0; i < sband->n_channels; i++) {
71                         if (sband->channels[i].center_freq == freq)
72                                 return &sband->channels[i];
73                 }
74         }
75
76         return NULL;
77 }
78 EXPORT_SYMBOL(__ieee80211_get_channel);
79
80 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
81                                      enum ieee80211_band band)
82 {
83         int i, want;
84
85         switch (band) {
86         case IEEE80211_BAND_5GHZ:
87                 want = 3;
88                 for (i = 0; i < sband->n_bitrates; i++) {
89                         if (sband->bitrates[i].bitrate == 60 ||
90                             sband->bitrates[i].bitrate == 120 ||
91                             sband->bitrates[i].bitrate == 240) {
92                                 sband->bitrates[i].flags |=
93                                         IEEE80211_RATE_MANDATORY_A;
94                                 want--;
95                         }
96                 }
97                 WARN_ON(want);
98                 break;
99         case IEEE80211_BAND_2GHZ:
100                 want = 7;
101                 for (i = 0; i < sband->n_bitrates; i++) {
102                         if (sband->bitrates[i].bitrate == 10) {
103                                 sband->bitrates[i].flags |=
104                                         IEEE80211_RATE_MANDATORY_B |
105                                         IEEE80211_RATE_MANDATORY_G;
106                                 want--;
107                         }
108
109                         if (sband->bitrates[i].bitrate == 20 ||
110                             sband->bitrates[i].bitrate == 55 ||
111                             sband->bitrates[i].bitrate == 110 ||
112                             sband->bitrates[i].bitrate == 60 ||
113                             sband->bitrates[i].bitrate == 120 ||
114                             sband->bitrates[i].bitrate == 240) {
115                                 sband->bitrates[i].flags |=
116                                         IEEE80211_RATE_MANDATORY_G;
117                                 want--;
118                         }
119
120                         if (sband->bitrates[i].bitrate != 10 &&
121                             sband->bitrates[i].bitrate != 20 &&
122                             sband->bitrates[i].bitrate != 55 &&
123                             sband->bitrates[i].bitrate != 110)
124                                 sband->bitrates[i].flags |=
125                                         IEEE80211_RATE_ERP_G;
126                 }
127                 WARN_ON(want != 0 && want != 3 && want != 6);
128                 break;
129         case IEEE80211_NUM_BANDS:
130                 WARN_ON(1);
131                 break;
132         }
133 }
134
135 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
136 {
137         enum ieee80211_band band;
138
139         for (band = 0; band < IEEE80211_NUM_BANDS; band++)
140                 if (wiphy->bands[band])
141                         set_mandatory_flags_band(wiphy->bands[band], band);
142 }
143
144 int cfg80211_validate_key_settings(struct key_params *params, int key_idx,
145                                    const u8 *mac_addr)
146 {
147         if (key_idx > 5)
148                 return -EINVAL;
149
150         /*
151          * Disallow pairwise keys with non-zero index unless it's WEP
152          * (because current deployments use pairwise WEP keys with
153          * non-zero indizes but 802.11i clearly specifies to use zero)
154          */
155         if (mac_addr && key_idx &&
156             params->cipher != WLAN_CIPHER_SUITE_WEP40 &&
157             params->cipher != WLAN_CIPHER_SUITE_WEP104)
158                 return -EINVAL;
159
160         switch (params->cipher) {
161         case WLAN_CIPHER_SUITE_WEP40:
162                 if (params->key_len != WLAN_KEY_LEN_WEP40)
163                         return -EINVAL;
164                 break;
165         case WLAN_CIPHER_SUITE_TKIP:
166                 if (params->key_len != WLAN_KEY_LEN_TKIP)
167                         return -EINVAL;
168                 break;
169         case WLAN_CIPHER_SUITE_CCMP:
170                 if (params->key_len != WLAN_KEY_LEN_CCMP)
171                         return -EINVAL;
172                 break;
173         case WLAN_CIPHER_SUITE_WEP104:
174                 if (params->key_len != WLAN_KEY_LEN_WEP104)
175                         return -EINVAL;
176                 break;
177         case WLAN_CIPHER_SUITE_AES_CMAC:
178                 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
179                         return -EINVAL;
180                 break;
181         default:
182                 return -EINVAL;
183         }
184
185         if (params->seq) {
186                 switch (params->cipher) {
187                 case WLAN_CIPHER_SUITE_WEP40:
188                 case WLAN_CIPHER_SUITE_WEP104:
189                         /* These ciphers do not use key sequence */
190                         return -EINVAL;
191                 case WLAN_CIPHER_SUITE_TKIP:
192                 case WLAN_CIPHER_SUITE_CCMP:
193                 case WLAN_CIPHER_SUITE_AES_CMAC:
194                         if (params->seq_len != 6)
195                                 return -EINVAL;
196                         break;
197                 }
198         }
199
200         return 0;
201 }
202
203 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
204 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
205 const unsigned char rfc1042_header[] __aligned(2) =
206         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
207 EXPORT_SYMBOL(rfc1042_header);
208
209 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
210 const unsigned char bridge_tunnel_header[] __aligned(2) =
211         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
212 EXPORT_SYMBOL(bridge_tunnel_header);
213
214 unsigned int ieee80211_hdrlen(__le16 fc)
215 {
216         unsigned int hdrlen = 24;
217
218         if (ieee80211_is_data(fc)) {
219                 if (ieee80211_has_a4(fc))
220                         hdrlen = 30;
221                 if (ieee80211_is_data_qos(fc))
222                         hdrlen += IEEE80211_QOS_CTL_LEN;
223                 goto out;
224         }
225
226         if (ieee80211_is_ctl(fc)) {
227                 /*
228                  * ACK and CTS are 10 bytes, all others 16. To see how
229                  * to get this condition consider
230                  *   subtype mask:   0b0000000011110000 (0x00F0)
231                  *   ACK subtype:    0b0000000011010000 (0x00D0)
232                  *   CTS subtype:    0b0000000011000000 (0x00C0)
233                  *   bits that matter:         ^^^      (0x00E0)
234                  *   value of those: 0b0000000011000000 (0x00C0)
235                  */
236                 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
237                         hdrlen = 10;
238                 else
239                         hdrlen = 16;
240         }
241 out:
242         return hdrlen;
243 }
244 EXPORT_SYMBOL(ieee80211_hdrlen);
245
246 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
247 {
248         const struct ieee80211_hdr *hdr =
249                         (const struct ieee80211_hdr *)skb->data;
250         unsigned int hdrlen;
251
252         if (unlikely(skb->len < 10))
253                 return 0;
254         hdrlen = ieee80211_hdrlen(hdr->frame_control);
255         if (unlikely(hdrlen > skb->len))
256                 return 0;
257         return hdrlen;
258 }
259 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
260
261 static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
262 {
263         int ae = meshhdr->flags & MESH_FLAGS_AE;
264         /* 7.1.3.5a.2 */
265         switch (ae) {
266         case 0:
267                 return 6;
268         case 1:
269                 return 12;
270         case 2:
271                 return 18;
272         case 3:
273                 return 24;
274         default:
275                 return 6;
276         }
277 }
278
279 int ieee80211_data_to_8023(struct sk_buff *skb, u8 *addr,
280                            enum nl80211_iftype iftype)
281 {
282         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
283         u16 hdrlen, ethertype;
284         u8 *payload;
285         u8 dst[ETH_ALEN];
286         u8 src[ETH_ALEN] __aligned(2);
287
288         if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
289                 return -1;
290
291         hdrlen = ieee80211_hdrlen(hdr->frame_control);
292
293         /* convert IEEE 802.11 header + possible LLC headers into Ethernet
294          * header
295          * IEEE 802.11 address fields:
296          * ToDS FromDS Addr1 Addr2 Addr3 Addr4
297          *   0     0   DA    SA    BSSID n/a
298          *   0     1   DA    BSSID SA    n/a
299          *   1     0   BSSID SA    DA    n/a
300          *   1     1   RA    TA    DA    SA
301          */
302         memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
303         memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
304
305         switch (hdr->frame_control &
306                 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
307         case cpu_to_le16(IEEE80211_FCTL_TODS):
308                 if (unlikely(iftype != NL80211_IFTYPE_AP &&
309                              iftype != NL80211_IFTYPE_AP_VLAN))
310                         return -1;
311                 break;
312         case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
313                 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
314                              iftype != NL80211_IFTYPE_MESH_POINT))
315                         return -1;
316                 if (iftype == NL80211_IFTYPE_MESH_POINT) {
317                         struct ieee80211s_hdr *meshdr =
318                                 (struct ieee80211s_hdr *) (skb->data + hdrlen);
319                         hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
320                         if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
321                                 memcpy(dst, meshdr->eaddr1, ETH_ALEN);
322                                 memcpy(src, meshdr->eaddr2, ETH_ALEN);
323                         }
324                 }
325                 break;
326         case cpu_to_le16(IEEE80211_FCTL_FROMDS):
327                 if (iftype != NL80211_IFTYPE_STATION ||
328                     (is_multicast_ether_addr(dst) &&
329                      !compare_ether_addr(src, addr)))
330                         return -1;
331                 break;
332         case cpu_to_le16(0):
333                 if (iftype != NL80211_IFTYPE_ADHOC)
334                         return -1;
335                 break;
336         }
337
338         if (unlikely(skb->len - hdrlen < 8))
339                 return -1;
340
341         payload = skb->data + hdrlen;
342         ethertype = (payload[6] << 8) | payload[7];
343
344         if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
345                     ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
346                    compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
347                 /* remove RFC1042 or Bridge-Tunnel encapsulation and
348                  * replace EtherType */
349                 skb_pull(skb, hdrlen + 6);
350                 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
351                 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
352         } else {
353                 struct ethhdr *ehdr;
354                 __be16 len;
355
356                 skb_pull(skb, hdrlen);
357                 len = htons(skb->len);
358                 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
359                 memcpy(ehdr->h_dest, dst, ETH_ALEN);
360                 memcpy(ehdr->h_source, src, ETH_ALEN);
361                 ehdr->h_proto = len;
362         }
363         return 0;
364 }
365 EXPORT_SYMBOL(ieee80211_data_to_8023);
366
367 int ieee80211_data_from_8023(struct sk_buff *skb, u8 *addr,
368                              enum nl80211_iftype iftype, u8 *bssid, bool qos)
369 {
370         struct ieee80211_hdr hdr;
371         u16 hdrlen, ethertype;
372         __le16 fc;
373         const u8 *encaps_data;
374         int encaps_len, skip_header_bytes;
375         int nh_pos, h_pos;
376         int head_need;
377
378         if (unlikely(skb->len < ETH_HLEN))
379                 return -EINVAL;
380
381         nh_pos = skb_network_header(skb) - skb->data;
382         h_pos = skb_transport_header(skb) - skb->data;
383
384         /* convert Ethernet header to proper 802.11 header (based on
385          * operation mode) */
386         ethertype = (skb->data[12] << 8) | skb->data[13];
387         fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
388
389         switch (iftype) {
390         case NL80211_IFTYPE_AP:
391         case NL80211_IFTYPE_AP_VLAN:
392                 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
393                 /* DA BSSID SA */
394                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
395                 memcpy(hdr.addr2, addr, ETH_ALEN);
396                 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
397                 hdrlen = 24;
398                 break;
399         case NL80211_IFTYPE_STATION:
400                 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
401                 /* BSSID SA DA */
402                 memcpy(hdr.addr1, bssid, ETH_ALEN);
403                 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
404                 memcpy(hdr.addr3, skb->data, ETH_ALEN);
405                 hdrlen = 24;
406                 break;
407         case NL80211_IFTYPE_ADHOC:
408                 /* DA SA BSSID */
409                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
410                 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
411                 memcpy(hdr.addr3, bssid, ETH_ALEN);
412                 hdrlen = 24;
413                 break;
414         default:
415                 return -EOPNOTSUPP;
416         }
417
418         if (qos) {
419                 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
420                 hdrlen += 2;
421         }
422
423         hdr.frame_control = fc;
424         hdr.duration_id = 0;
425         hdr.seq_ctrl = 0;
426
427         skip_header_bytes = ETH_HLEN;
428         if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
429                 encaps_data = bridge_tunnel_header;
430                 encaps_len = sizeof(bridge_tunnel_header);
431                 skip_header_bytes -= 2;
432         } else if (ethertype > 0x600) {
433                 encaps_data = rfc1042_header;
434                 encaps_len = sizeof(rfc1042_header);
435                 skip_header_bytes -= 2;
436         } else {
437                 encaps_data = NULL;
438                 encaps_len = 0;
439         }
440
441         skb_pull(skb, skip_header_bytes);
442         nh_pos -= skip_header_bytes;
443         h_pos -= skip_header_bytes;
444
445         head_need = hdrlen + encaps_len - skb_headroom(skb);
446
447         if (head_need > 0 || skb_cloned(skb)) {
448                 head_need = max(head_need, 0);
449                 if (head_need)
450                         skb_orphan(skb);
451
452                 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC)) {
453                         printk(KERN_ERR "failed to reallocate Tx buffer\n");
454                         return -ENOMEM;
455                 }
456                 skb->truesize += head_need;
457         }
458
459         if (encaps_data) {
460                 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
461                 nh_pos += encaps_len;
462                 h_pos += encaps_len;
463         }
464
465         memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
466
467         nh_pos += hdrlen;
468         h_pos += hdrlen;
469
470         /* Update skb pointers to various headers since this modified frame
471          * is going to go through Linux networking code that may potentially
472          * need things like pointer to IP header. */
473         skb_set_mac_header(skb, 0);
474         skb_set_network_header(skb, nh_pos);
475         skb_set_transport_header(skb, h_pos);
476
477         return 0;
478 }
479 EXPORT_SYMBOL(ieee80211_data_from_8023);
480
481 /* Given a data frame determine the 802.1p/1d tag to use. */
482 unsigned int cfg80211_classify8021d(struct sk_buff *skb)
483 {
484         unsigned int dscp;
485
486         /* skb->priority values from 256->263 are magic values to
487          * directly indicate a specific 802.1d priority.  This is used
488          * to allow 802.1d priority to be passed directly in from VLAN
489          * tags, etc.
490          */
491         if (skb->priority >= 256 && skb->priority <= 263)
492                 return skb->priority - 256;
493
494         switch (skb->protocol) {
495         case htons(ETH_P_IP):
496                 dscp = ip_hdr(skb)->tos & 0xfc;
497                 break;
498         default:
499                 return 0;
500         }
501
502         return dscp >> 5;
503 }
504 EXPORT_SYMBOL(cfg80211_classify8021d);