]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/net/wireless/rt2x00/rt2x00crypto.c
rt2x00: Optimize IV/EIV handling
[mv-sheeva.git] / drivers / net / wireless / rt2x00 / rt2x00crypto.c
1 /*
2         Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
3         <http://rt2x00.serialmonkey.com>
4
5         This program is free software; you can redistribute it and/or modify
6         it under the terms of the GNU General Public License as published by
7         the Free Software Foundation; either version 2 of the License, or
8         (at your option) any later version.
9
10         This program is distributed in the hope that it will be useful,
11         but WITHOUT ANY WARRANTY; without even the implied warranty of
12         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13         GNU General Public License for more details.
14
15         You should have received a copy of the GNU General Public License
16         along with this program; if not, write to the
17         Free Software Foundation, Inc.,
18         59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 /*
22         Module: rt2x00lib
23         Abstract: rt2x00 crypto specific routines.
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28
29 #include "rt2x00.h"
30 #include "rt2x00lib.h"
31
32 enum cipher rt2x00crypto_key_to_cipher(struct ieee80211_key_conf *key)
33 {
34         switch (key->alg) {
35         case ALG_WEP:
36                 if (key->keylen == LEN_WEP40)
37                         return CIPHER_WEP64;
38                 else
39                         return CIPHER_WEP128;
40         case ALG_TKIP:
41                 return CIPHER_TKIP;
42         case ALG_CCMP:
43                 return CIPHER_AES;
44         default:
45                 return CIPHER_NONE;
46         }
47 }
48
49 unsigned int rt2x00crypto_tx_overhead(struct ieee80211_tx_info *tx_info)
50 {
51         struct ieee80211_key_conf *key = tx_info->control.hw_key;
52         unsigned int overhead = 0;
53
54         /*
55          * Extend frame length to include IV/EIV/ICV/MMIC,
56          * note that these lengths should only be added when
57          * mac80211 does not generate it.
58          */
59         overhead += key->icv_len;
60
61         if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
62                 overhead += key->iv_len;
63
64         if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) {
65                 if (key->alg == ALG_TKIP)
66                         overhead += 8;
67         }
68
69         return overhead;
70 }
71
72 void rt2x00crypto_tx_remove_iv(struct sk_buff *skb, unsigned int iv_len)
73 {
74         struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
75         unsigned int header_length = ieee80211_get_hdrlen_from_skb(skb);
76
77         if (unlikely(!iv_len))
78                 return;
79
80         /* Copy IV/EIV data */
81         memcpy(skbdesc->iv, skb->data + header_length, iv_len);
82
83         /* Move ieee80211 header */
84         memmove(skb->data + iv_len, skb->data, header_length);
85
86         /* Pull buffer to correct size */
87         skb_pull(skb, iv_len);
88
89         /* IV/EIV data has officially be stripped */
90         skbdesc->flags |= FRAME_DESC_IV_STRIPPED;
91 }
92
93 void rt2x00crypto_tx_insert_iv(struct sk_buff *skb)
94 {
95         struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
96         unsigned int header_length = ieee80211_get_hdrlen_from_skb(skb);
97         const unsigned int iv_len =
98             ((!!(skbdesc->iv[0])) * 4) + ((!!(skbdesc->iv[1])) * 4);
99
100         if (!(skbdesc->flags & FRAME_DESC_IV_STRIPPED))
101                 return;
102
103         skb_push(skb, iv_len);
104
105         /* Move ieee80211 header */
106         memmove(skb->data, skb->data + iv_len, header_length);
107
108         /* Copy IV/EIV data */
109         memcpy(skb->data + header_length, skbdesc->iv, iv_len);
110
111         /* IV/EIV data has returned into the frame */
112         skbdesc->flags &= ~FRAME_DESC_IV_STRIPPED;
113 }
114
115 void rt2x00crypto_rx_insert_iv(struct sk_buff *skb, unsigned int align,
116                                unsigned int header_length,
117                                struct rxdone_entry_desc *rxdesc)
118 {
119         unsigned int payload_len = rxdesc->size - header_length;
120         unsigned int iv_len;
121         unsigned int icv_len;
122         unsigned int transfer = 0;
123
124         /*
125          * WEP64/WEP128: Provides IV & ICV
126          * TKIP: Provides IV/EIV & ICV
127          * AES: Provies IV/EIV & ICV
128          */
129         switch (rxdesc->cipher) {
130         case CIPHER_WEP64:
131         case CIPHER_WEP128:
132                 iv_len = 4;
133                 icv_len = 4;
134                 break;
135         case CIPHER_TKIP:
136                 iv_len = 8;
137                 icv_len = 4;
138                 break;
139         case CIPHER_AES:
140                 iv_len = 8;
141                 icv_len = 8;
142                 break;
143         default:
144                 /* Unsupport type */
145                 return;
146         }
147
148         /*
149          * Make room for new data, note that we increase both
150          * headsize and tailsize when required. The tailsize is
151          * only needed when ICV data needs to be inserted and
152          * the padding is smaller then the ICV data.
153          * When alignment requirements is greater then the
154          * ICV data we must trim the skb to the correct size
155          * because we need to remove the extra bytes.
156          */
157         skb_push(skb, iv_len + align);
158         if (align < icv_len)
159                 skb_put(skb, icv_len - align);
160         else if (align > icv_len)
161                 skb_trim(skb, rxdesc->size + iv_len + icv_len);
162
163         /* Move ieee80211 header */
164         memmove(skb->data + transfer,
165                 skb->data + transfer + iv_len + align,
166                 header_length);
167         transfer += header_length;
168
169         /* Copy IV/EIV data */
170         memcpy(skb->data + transfer, rxdesc->iv, iv_len);
171         transfer += iv_len;
172
173         /* Move payload */
174         if (align) {
175                 memmove(skb->data + transfer,
176                         skb->data + transfer + align,
177                         payload_len);
178         }
179
180         /*
181          * NOTE: Always count the payload as transfered,
182          * even when alignment was set to zero. This is required
183          * for determining the correct offset for the ICV data.
184          */
185         transfer += payload_len;
186
187         /*
188          * Copy ICV data
189          * AES appends 8 bytes, we can't fill the upper
190          * 4 bytes, but mac80211 doesn't care about what
191          * we provide here anyway and strips it immediately.
192          */
193         memcpy(skb->data + transfer, &rxdesc->icv, 4);
194         transfer += icv_len;
195
196         /* IV/EIV/ICV has been inserted into frame */
197         rxdesc->size = transfer;
198         rxdesc->flags &= ~RX_FLAG_IV_STRIPPED;
199 }