blob: 0b41845d9543a1653d3893e22d7f4c56d1719c35 [file] [log] [blame]
Ivo van Doorn2bb057d2008-08-04 16:37:44 +02001/*
Ivo van Doorn4e54c712009-01-17 20:42:32 +01002 Copyright (C) 2004 - 2009 rt2x00 SourceForge Project
Ivo van Doorn2bb057d2008-08-04 16:37:44 +02003 <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
32enum 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
Ivo van Doorn9c3444d2008-12-03 17:29:48 +010049void rt2x00crypto_create_tx_descriptor(struct queue_entry *entry,
50 struct txentry_desc *txdesc)
51{
Ivo van Doorn7b409822008-12-20 10:58:33 +010052 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
Ivo van Doorn9c3444d2008-12-03 17:29:48 +010053 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
54 struct ieee80211_key_conf *hw_key = tx_info->control.hw_key;
55
Ivo van Doorn7b409822008-12-20 10:58:33 +010056 if (!test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags) ||
57 !hw_key || entry->skb->do_not_encrypt)
58 return;
59
Ivo van Doorn9c3444d2008-12-03 17:29:48 +010060 __set_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags);
61
62 txdesc->cipher = rt2x00crypto_key_to_cipher(hw_key);
63
64 if (hw_key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
65 __set_bit(ENTRY_TXD_ENCRYPT_PAIRWISE, &txdesc->flags);
66
67 txdesc->key_idx = hw_key->hw_key_idx;
68 txdesc->iv_offset = ieee80211_get_hdrlen_from_skb(entry->skb);
69
70 if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
71 __set_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc->flags);
72
73 if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC))
74 __set_bit(ENTRY_TXD_ENCRYPT_MMIC, &txdesc->flags);
75}
76
Ivo van Doorn7b409822008-12-20 10:58:33 +010077unsigned int rt2x00crypto_tx_overhead(struct rt2x00_dev *rt2x00dev,
78 struct sk_buff *skb)
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020079{
Ivo van Doorn7b409822008-12-20 10:58:33 +010080 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020081 struct ieee80211_key_conf *key = tx_info->control.hw_key;
82 unsigned int overhead = 0;
83
Ivo van Doorn7b409822008-12-20 10:58:33 +010084 if (!test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags) ||
85 !key || skb->do_not_encrypt)
86 return overhead;
87
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020088 /*
89 * Extend frame length to include IV/EIV/ICV/MMIC,
90 * note that these lengths should only be added when
91 * mac80211 does not generate it.
92 */
Felix Fietkau76708de2008-10-05 18:02:48 +020093 overhead += key->icv_len;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020094
95 if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
Felix Fietkau76708de2008-10-05 18:02:48 +020096 overhead += key->iv_len;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020097
98 if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) {
99 if (key->alg == ALG_TKIP)
100 overhead += 8;
101 }
102
103 return overhead;
104}
105
Ivo van Doorndddfb472008-12-02 18:20:42 +0100106void rt2x00crypto_tx_copy_iv(struct sk_buff *skb, unsigned int iv_len)
107{
108 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
109 unsigned int header_length = ieee80211_get_hdrlen_from_skb(skb);
110
111 if (unlikely(!iv_len))
112 return;
113
114 /* Copy IV/EIV data */
115 memcpy(skbdesc->iv, skb->data + header_length, iv_len);
116}
117
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200118void rt2x00crypto_tx_remove_iv(struct sk_buff *skb, unsigned int iv_len)
119{
120 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
121 unsigned int header_length = ieee80211_get_hdrlen_from_skb(skb);
122
123 if (unlikely(!iv_len))
124 return;
125
126 /* Copy IV/EIV data */
Ivo van Doorn1ce9cda2008-12-02 18:19:48 +0100127 memcpy(skbdesc->iv, skb->data + header_length, iv_len);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200128
129 /* Move ieee80211 header */
130 memmove(skb->data + iv_len, skb->data, header_length);
131
132 /* Pull buffer to correct size */
133 skb_pull(skb, iv_len);
134
135 /* IV/EIV data has officially be stripped */
136 skbdesc->flags |= FRAME_DESC_IV_STRIPPED;
137}
138
139void rt2x00crypto_tx_insert_iv(struct sk_buff *skb)
140{
141 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
142 unsigned int header_length = ieee80211_get_hdrlen_from_skb(skb);
143 const unsigned int iv_len =
Ivo van Doorn1ce9cda2008-12-02 18:19:48 +0100144 ((!!(skbdesc->iv[0])) * 4) + ((!!(skbdesc->iv[1])) * 4);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200145
146 if (!(skbdesc->flags & FRAME_DESC_IV_STRIPPED))
147 return;
148
149 skb_push(skb, iv_len);
150
151 /* Move ieee80211 header */
152 memmove(skb->data, skb->data + iv_len, header_length);
153
154 /* Copy IV/EIV data */
Ivo van Doorn1ce9cda2008-12-02 18:19:48 +0100155 memcpy(skb->data + header_length, skbdesc->iv, iv_len);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200156
157 /* IV/EIV data has returned into the frame */
158 skbdesc->flags &= ~FRAME_DESC_IV_STRIPPED;
159}
160
161void rt2x00crypto_rx_insert_iv(struct sk_buff *skb, unsigned int align,
162 unsigned int header_length,
163 struct rxdone_entry_desc *rxdesc)
164{
165 unsigned int payload_len = rxdesc->size - header_length;
166 unsigned int iv_len;
167 unsigned int icv_len;
168 unsigned int transfer = 0;
169
170 /*
171 * WEP64/WEP128: Provides IV & ICV
172 * TKIP: Provides IV/EIV & ICV
173 * AES: Provies IV/EIV & ICV
174 */
175 switch (rxdesc->cipher) {
176 case CIPHER_WEP64:
177 case CIPHER_WEP128:
178 iv_len = 4;
179 icv_len = 4;
180 break;
181 case CIPHER_TKIP:
182 iv_len = 8;
183 icv_len = 4;
184 break;
185 case CIPHER_AES:
186 iv_len = 8;
187 icv_len = 8;
188 break;
189 default:
190 /* Unsupport type */
191 return;
192 }
193
194 /*
195 * Make room for new data, note that we increase both
196 * headsize and tailsize when required. The tailsize is
197 * only needed when ICV data needs to be inserted and
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200198 * the padding is smaller than the ICV data.
199 * When alignment requirements is greater than the
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200200 * ICV data we must trim the skb to the correct size
201 * because we need to remove the extra bytes.
202 */
203 skb_push(skb, iv_len + align);
204 if (align < icv_len)
205 skb_put(skb, icv_len - align);
206 else if (align > icv_len)
207 skb_trim(skb, rxdesc->size + iv_len + icv_len);
208
209 /* Move ieee80211 header */
210 memmove(skb->data + transfer,
211 skb->data + transfer + iv_len + align,
212 header_length);
213 transfer += header_length;
214
Ivo van Doorn1ce9cda2008-12-02 18:19:48 +0100215 /* Copy IV/EIV data */
216 memcpy(skb->data + transfer, rxdesc->iv, iv_len);
217 transfer += iv_len;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200218
219 /* Move payload */
220 if (align) {
221 memmove(skb->data + transfer,
222 skb->data + transfer + align,
223 payload_len);
224 }
225
226 /*
227 * NOTE: Always count the payload as transfered,
228 * even when alignment was set to zero. This is required
229 * for determining the correct offset for the ICV data.
230 */
231 transfer += payload_len;
232
Ivo van Doorn1ce9cda2008-12-02 18:19:48 +0100233 /*
234 * Copy ICV data
235 * AES appends 8 bytes, we can't fill the upper
236 * 4 bytes, but mac80211 doesn't care about what
237 * we provide here anyway and strips it immediately.
238 */
239 memcpy(skb->data + transfer, &rxdesc->icv, 4);
240 transfer += icv_len;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200241
242 /* IV/EIV/ICV has been inserted into frame */
243 rxdesc->size = transfer;
244 rxdesc->flags &= ~RX_FLAG_IV_STRIPPED;
245}