blob: aee9cba13eb3c3e97ea816841d0ed84059b3fbca [file] [log] [blame]
Ivo van Doorn2bb057d2008-08-04 16:37:44 +02001/*
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
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{
52 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
53 struct ieee80211_key_conf *hw_key = tx_info->control.hw_key;
54
55 __set_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags);
56
57 txdesc->cipher = rt2x00crypto_key_to_cipher(hw_key);
58
59 if (hw_key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
60 __set_bit(ENTRY_TXD_ENCRYPT_PAIRWISE, &txdesc->flags);
61
62 txdesc->key_idx = hw_key->hw_key_idx;
63 txdesc->iv_offset = ieee80211_get_hdrlen_from_skb(entry->skb);
64
65 if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
66 __set_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc->flags);
67
68 if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC))
69 __set_bit(ENTRY_TXD_ENCRYPT_MMIC, &txdesc->flags);
70}
71
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020072unsigned int rt2x00crypto_tx_overhead(struct ieee80211_tx_info *tx_info)
73{
74 struct ieee80211_key_conf *key = tx_info->control.hw_key;
75 unsigned int overhead = 0;
76
77 /*
78 * Extend frame length to include IV/EIV/ICV/MMIC,
79 * note that these lengths should only be added when
80 * mac80211 does not generate it.
81 */
Felix Fietkau76708de2008-10-05 18:02:48 +020082 overhead += key->icv_len;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020083
84 if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
Felix Fietkau76708de2008-10-05 18:02:48 +020085 overhead += key->iv_len;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020086
87 if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) {
88 if (key->alg == ALG_TKIP)
89 overhead += 8;
90 }
91
92 return overhead;
93}
94
Ivo van Doorndddfb472008-12-02 18:20:42 +010095void rt2x00crypto_tx_copy_iv(struct sk_buff *skb, unsigned int iv_len)
96{
97 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
98 unsigned int header_length = ieee80211_get_hdrlen_from_skb(skb);
99
100 if (unlikely(!iv_len))
101 return;
102
103 /* Copy IV/EIV data */
104 memcpy(skbdesc->iv, skb->data + header_length, iv_len);
105}
106
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200107void rt2x00crypto_tx_remove_iv(struct sk_buff *skb, unsigned int iv_len)
108{
109 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
110 unsigned int header_length = ieee80211_get_hdrlen_from_skb(skb);
111
112 if (unlikely(!iv_len))
113 return;
114
115 /* Copy IV/EIV data */
Ivo van Doorn1ce9cda2008-12-02 18:19:48 +0100116 memcpy(skbdesc->iv, skb->data + header_length, iv_len);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200117
118 /* Move ieee80211 header */
119 memmove(skb->data + iv_len, skb->data, header_length);
120
121 /* Pull buffer to correct size */
122 skb_pull(skb, iv_len);
123
124 /* IV/EIV data has officially be stripped */
125 skbdesc->flags |= FRAME_DESC_IV_STRIPPED;
126}
127
128void rt2x00crypto_tx_insert_iv(struct sk_buff *skb)
129{
130 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
131 unsigned int header_length = ieee80211_get_hdrlen_from_skb(skb);
132 const unsigned int iv_len =
Ivo van Doorn1ce9cda2008-12-02 18:19:48 +0100133 ((!!(skbdesc->iv[0])) * 4) + ((!!(skbdesc->iv[1])) * 4);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200134
135 if (!(skbdesc->flags & FRAME_DESC_IV_STRIPPED))
136 return;
137
138 skb_push(skb, iv_len);
139
140 /* Move ieee80211 header */
141 memmove(skb->data, skb->data + iv_len, header_length);
142
143 /* Copy IV/EIV data */
Ivo van Doorn1ce9cda2008-12-02 18:19:48 +0100144 memcpy(skb->data + header_length, skbdesc->iv, iv_len);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200145
146 /* IV/EIV data has returned into the frame */
147 skbdesc->flags &= ~FRAME_DESC_IV_STRIPPED;
148}
149
150void rt2x00crypto_rx_insert_iv(struct sk_buff *skb, unsigned int align,
151 unsigned int header_length,
152 struct rxdone_entry_desc *rxdesc)
153{
154 unsigned int payload_len = rxdesc->size - header_length;
155 unsigned int iv_len;
156 unsigned int icv_len;
157 unsigned int transfer = 0;
158
159 /*
160 * WEP64/WEP128: Provides IV & ICV
161 * TKIP: Provides IV/EIV & ICV
162 * AES: Provies IV/EIV & ICV
163 */
164 switch (rxdesc->cipher) {
165 case CIPHER_WEP64:
166 case CIPHER_WEP128:
167 iv_len = 4;
168 icv_len = 4;
169 break;
170 case CIPHER_TKIP:
171 iv_len = 8;
172 icv_len = 4;
173 break;
174 case CIPHER_AES:
175 iv_len = 8;
176 icv_len = 8;
177 break;
178 default:
179 /* Unsupport type */
180 return;
181 }
182
183 /*
184 * Make room for new data, note that we increase both
185 * headsize and tailsize when required. The tailsize is
186 * only needed when ICV data needs to be inserted and
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200187 * the padding is smaller than the ICV data.
188 * When alignment requirements is greater than the
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200189 * ICV data we must trim the skb to the correct size
190 * because we need to remove the extra bytes.
191 */
192 skb_push(skb, iv_len + align);
193 if (align < icv_len)
194 skb_put(skb, icv_len - align);
195 else if (align > icv_len)
196 skb_trim(skb, rxdesc->size + iv_len + icv_len);
197
198 /* Move ieee80211 header */
199 memmove(skb->data + transfer,
200 skb->data + transfer + iv_len + align,
201 header_length);
202 transfer += header_length;
203
Ivo van Doorn1ce9cda2008-12-02 18:19:48 +0100204 /* Copy IV/EIV data */
205 memcpy(skb->data + transfer, rxdesc->iv, iv_len);
206 transfer += iv_len;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200207
208 /* Move payload */
209 if (align) {
210 memmove(skb->data + transfer,
211 skb->data + transfer + align,
212 payload_len);
213 }
214
215 /*
216 * NOTE: Always count the payload as transfered,
217 * even when alignment was set to zero. This is required
218 * for determining the correct offset for the ICV data.
219 */
220 transfer += payload_len;
221
Ivo van Doorn1ce9cda2008-12-02 18:19:48 +0100222 /*
223 * Copy ICV data
224 * AES appends 8 bytes, we can't fill the upper
225 * 4 bytes, but mac80211 doesn't care about what
226 * we provide here anyway and strips it immediately.
227 */
228 memcpy(skb->data + transfer, &rxdesc->icv, 4);
229 transfer += icv_len;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200230
231 /* IV/EIV/ICV has been inserted into frame */
232 rxdesc->size = transfer;
233 rxdesc->flags &= ~RX_FLAG_IV_STRIPPED;
234}