]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/net/wireless/rt2x00/rt2x00crypto.c
rt2x00: Implement HW encryption (rt2500usb)
[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_copy_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
84 void rt2x00crypto_tx_remove_iv(struct sk_buff *skb, unsigned int iv_len)
85 {
86         struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
87         unsigned int header_length = ieee80211_get_hdrlen_from_skb(skb);
88
89         if (unlikely(!iv_len))
90                 return;
91
92         /* Copy IV/EIV data */
93         memcpy(skbdesc->iv, skb->data + header_length, iv_len);
94
95         /* Move ieee80211 header */
96         memmove(skb->data + iv_len, skb->data, header_length);
97
98         /* Pull buffer to correct size */
99         skb_pull(skb, iv_len);
100
101         /* IV/EIV data has officially be stripped */
102         skbdesc->flags |= FRAME_DESC_IV_STRIPPED;
103 }
104
105 void rt2x00crypto_tx_insert_iv(struct sk_buff *skb)
106 {
107         struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
108         unsigned int header_length = ieee80211_get_hdrlen_from_skb(skb);
109         const unsigned int iv_len =
110             ((!!(skbdesc->iv[0])) * 4) + ((!!(skbdesc->iv[1])) * 4);
111
112         if (!(skbdesc->flags & FRAME_DESC_IV_STRIPPED))
113                 return;
114
115         skb_push(skb, iv_len);
116
117         /* Move ieee80211 header */
118         memmove(skb->data, skb->data + iv_len, header_length);
119
120         /* Copy IV/EIV data */
121         memcpy(skb->data + header_length, skbdesc->iv, iv_len);
122
123         /* IV/EIV data has returned into the frame */
124         skbdesc->flags &= ~FRAME_DESC_IV_STRIPPED;
125 }
126
127 void rt2x00crypto_rx_insert_iv(struct sk_buff *skb, unsigned int align,
128                                unsigned int header_length,
129                                struct rxdone_entry_desc *rxdesc)
130 {
131         unsigned int payload_len = rxdesc->size - header_length;
132         unsigned int iv_len;
133         unsigned int icv_len;
134         unsigned int transfer = 0;
135
136         /*
137          * WEP64/WEP128: Provides IV & ICV
138          * TKIP: Provides IV/EIV & ICV
139          * AES: Provies IV/EIV & ICV
140          */
141         switch (rxdesc->cipher) {
142         case CIPHER_WEP64:
143         case CIPHER_WEP128:
144                 iv_len = 4;
145                 icv_len = 4;
146                 break;
147         case CIPHER_TKIP:
148                 iv_len = 8;
149                 icv_len = 4;
150                 break;
151         case CIPHER_AES:
152                 iv_len = 8;
153                 icv_len = 8;
154                 break;
155         default:
156                 /* Unsupport type */
157                 return;
158         }
159
160         /*
161          * Make room for new data, note that we increase both
162          * headsize and tailsize when required. The tailsize is
163          * only needed when ICV data needs to be inserted and
164          * the padding is smaller then the ICV data.
165          * When alignment requirements is greater then the
166          * ICV data we must trim the skb to the correct size
167          * because we need to remove the extra bytes.
168          */
169         skb_push(skb, iv_len + align);
170         if (align < icv_len)
171                 skb_put(skb, icv_len - align);
172         else if (align > icv_len)
173                 skb_trim(skb, rxdesc->size + iv_len + icv_len);
174
175         /* Move ieee80211 header */
176         memmove(skb->data + transfer,
177                 skb->data + transfer + iv_len + align,
178                 header_length);
179         transfer += header_length;
180
181         /* Copy IV/EIV data */
182         memcpy(skb->data + transfer, rxdesc->iv, iv_len);
183         transfer += iv_len;
184
185         /* Move payload */
186         if (align) {
187                 memmove(skb->data + transfer,
188                         skb->data + transfer + align,
189                         payload_len);
190         }
191
192         /*
193          * NOTE: Always count the payload as transfered,
194          * even when alignment was set to zero. This is required
195          * for determining the correct offset for the ICV data.
196          */
197         transfer += payload_len;
198
199         /*
200          * Copy ICV data
201          * AES appends 8 bytes, we can't fill the upper
202          * 4 bytes, but mac80211 doesn't care about what
203          * we provide here anyway and strips it immediately.
204          */
205         memcpy(skb->data + transfer, &rxdesc->icv, 4);
206         transfer += icv_len;
207
208         /* IV/EIV/ICV has been inserted into frame */
209         rxdesc->size = transfer;
210         rxdesc->flags &= ~RX_FLAG_IV_STRIPPED;
211 }