blob: 1ca4c7ffc1898c9ce97d87506b0b8e78464e78ad [file] [log] [blame]
Ivo van Doorn2bb057d2008-08-04 16:37:44 +02001/*
Gertjan van Wingerde9c9a0d12009-11-08 16:39:55 +01002 Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
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{
Johannes Berg97359d12010-08-10 09:46:38 +020034 switch (key->cipher) {
35 case WLAN_CIPHER_SUITE_WEP40:
36 return CIPHER_WEP64;
37 case WLAN_CIPHER_SUITE_WEP104:
38 return CIPHER_WEP128;
39 case WLAN_CIPHER_SUITE_TKIP:
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020040 return CIPHER_TKIP;
Johannes Berg97359d12010-08-10 09:46:38 +020041 case WLAN_CIPHER_SUITE_CCMP:
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020042 return CIPHER_AES;
43 default:
44 return CIPHER_NONE;
45 }
46}
47
Gertjan van Wingerde77b56212011-07-06 22:57:00 +020048void rt2x00crypto_create_tx_descriptor(struct rt2x00_dev *rt2x00dev,
49 struct sk_buff *skb,
Ivo van Doorn9c3444d2008-12-03 17:29:48 +010050 struct txentry_desc *txdesc)
51{
Gertjan van Wingerde77b56212011-07-06 22:57:00 +020052 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
Ivo van Doorn9c3444d2008-12-03 17:29:48 +010053 struct ieee80211_key_conf *hw_key = tx_info->control.hw_key;
54
Ivo van Doorn7dab73b2011-04-18 15:27:06 +020055 if (!test_bit(CAPABILITY_HW_CRYPTO, &rt2x00dev->cap_flags) || !hw_key)
Ivo van Doorn7b409822008-12-20 10:58:33 +010056 return;
57
Ivo van Doorn9c3444d2008-12-03 17:29:48 +010058 __set_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags);
59
60 txdesc->cipher = rt2x00crypto_key_to_cipher(hw_key);
61
62 if (hw_key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
63 __set_bit(ENTRY_TXD_ENCRYPT_PAIRWISE, &txdesc->flags);
64
65 txdesc->key_idx = hw_key->hw_key_idx;
Ivo van Doorn9f166172009-04-26 16:08:50 +020066 txdesc->iv_offset = txdesc->header_length;
Ivo van Doorn9eb4e212009-04-26 16:08:30 +020067 txdesc->iv_len = hw_key->iv_len;
Ivo van Doorn9c3444d2008-12-03 17:29:48 +010068
69 if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
70 __set_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc->flags);
71
72 if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC))
73 __set_bit(ENTRY_TXD_ENCRYPT_MMIC, &txdesc->flags);
74}
75
Ivo van Doorn7b409822008-12-20 10:58:33 +010076unsigned int rt2x00crypto_tx_overhead(struct rt2x00_dev *rt2x00dev,
77 struct sk_buff *skb)
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020078{
Ivo van Doorn7b409822008-12-20 10:58:33 +010079 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020080 struct ieee80211_key_conf *key = tx_info->control.hw_key;
81 unsigned int overhead = 0;
82
Ivo van Doorn7dab73b2011-04-18 15:27:06 +020083 if (!test_bit(CAPABILITY_HW_CRYPTO, &rt2x00dev->cap_flags) || !key)
Ivo van Doorn7b409822008-12-20 10:58:33 +010084 return overhead;
85
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020086 /*
87 * Extend frame length to include IV/EIV/ICV/MMIC,
88 * note that these lengths should only be added when
89 * mac80211 does not generate it.
90 */
Felix Fietkau76708de2008-10-05 18:02:48 +020091 overhead += key->icv_len;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020092
93 if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
Felix Fietkau76708de2008-10-05 18:02:48 +020094 overhead += key->iv_len;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020095
96 if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) {
Johannes Berg97359d12010-08-10 09:46:38 +020097 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020098 overhead += 8;
99 }
100
101 return overhead;
102}
103
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200104void rt2x00crypto_tx_copy_iv(struct sk_buff *skb, struct txentry_desc *txdesc)
Ivo van Doorndddfb472008-12-02 18:20:42 +0100105{
106 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
Ivo van Doorndddfb472008-12-02 18:20:42 +0100107
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200108 if (unlikely(!txdesc->iv_len))
Ivo van Doorndddfb472008-12-02 18:20:42 +0100109 return;
110
111 /* Copy IV/EIV data */
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200112 memcpy(skbdesc->iv, skb->data + txdesc->iv_offset, txdesc->iv_len);
Ivo van Doorndddfb472008-12-02 18:20:42 +0100113}
114
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200115void rt2x00crypto_tx_remove_iv(struct sk_buff *skb, struct txentry_desc *txdesc)
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200116{
117 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200118
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200119 if (unlikely(!txdesc->iv_len))
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200120 return;
121
122 /* Copy IV/EIV data */
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200123 memcpy(skbdesc->iv, skb->data + txdesc->iv_offset, txdesc->iv_len);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200124
125 /* Move ieee80211 header */
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200126 memmove(skb->data + txdesc->iv_len, skb->data, txdesc->iv_offset);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200127
128 /* Pull buffer to correct size */
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200129 skb_pull(skb, txdesc->iv_len);
Gertjan van Wingerded43e49ec2010-05-08 23:40:18 +0200130 txdesc->length -= txdesc->iv_len;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200131
Luis Correia49513482009-07-17 21:39:19 +0200132 /* IV/EIV data has officially been stripped */
Ivo van Doorn9f166172009-04-26 16:08:50 +0200133 skbdesc->flags |= SKBDESC_IV_STRIPPED;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200134}
135
Ivo van Doorn9f166172009-04-26 16:08:50 +0200136void rt2x00crypto_tx_insert_iv(struct sk_buff *skb, unsigned int header_length)
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200137{
138 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200139 const unsigned int iv_len =
Ivo van Doorn1ce9cda2008-12-02 18:19:48 +0100140 ((!!(skbdesc->iv[0])) * 4) + ((!!(skbdesc->iv[1])) * 4);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200141
Ivo van Doorn9f166172009-04-26 16:08:50 +0200142 if (!(skbdesc->flags & SKBDESC_IV_STRIPPED))
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200143 return;
144
145 skb_push(skb, iv_len);
146
147 /* Move ieee80211 header */
148 memmove(skb->data, skb->data + iv_len, header_length);
149
150 /* Copy IV/EIV data */
Ivo van Doorn1ce9cda2008-12-02 18:19:48 +0100151 memcpy(skb->data + header_length, skbdesc->iv, iv_len);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200152
153 /* IV/EIV data has returned into the frame */
Ivo van Doorn9f166172009-04-26 16:08:50 +0200154 skbdesc->flags &= ~SKBDESC_IV_STRIPPED;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200155}
156
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200157void rt2x00crypto_rx_insert_iv(struct sk_buff *skb,
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200158 unsigned int header_length,
159 struct rxdone_entry_desc *rxdesc)
160{
161 unsigned int payload_len = rxdesc->size - header_length;
Ivo van Doorn9f166172009-04-26 16:08:50 +0200162 unsigned int align = ALIGN_SIZE(skb, header_length);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200163 unsigned int iv_len;
164 unsigned int icv_len;
165 unsigned int transfer = 0;
166
167 /*
168 * WEP64/WEP128: Provides IV & ICV
169 * TKIP: Provides IV/EIV & ICV
170 * AES: Provies IV/EIV & ICV
171 */
172 switch (rxdesc->cipher) {
173 case CIPHER_WEP64:
174 case CIPHER_WEP128:
175 iv_len = 4;
176 icv_len = 4;
177 break;
178 case CIPHER_TKIP:
179 iv_len = 8;
180 icv_len = 4;
181 break;
182 case CIPHER_AES:
183 iv_len = 8;
184 icv_len = 8;
185 break;
186 default:
187 /* Unsupport type */
188 return;
189 }
190
191 /*
Ivo van Doorn9f166172009-04-26 16:08:50 +0200192 * Make room for new data. There are 2 possibilities
193 * either the alignment is already present between
194 * the 802.11 header and payload. In that case we
195 * we have to move the header less then the iv_len
196 * since we can use the already available l2pad bytes
197 * for the iv data.
198 * When the alignment must be added manually we must
199 * move the header more then iv_len since we must
200 * make room for the payload move as well.
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200201 */
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200202 if (rxdesc->dev_flags & RXDONE_L2PAD) {
Ivo van Doorn9f166172009-04-26 16:08:50 +0200203 skb_push(skb, iv_len - align);
204 skb_put(skb, icv_len);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200205
Ivo van Doorn9f166172009-04-26 16:08:50 +0200206 /* Move ieee80211 header */
207 memmove(skb->data + transfer,
208 skb->data + transfer + (iv_len - align),
209 header_length);
210 transfer += header_length;
211 } else {
212 skb_push(skb, iv_len + align);
213 if (align < icv_len)
214 skb_put(skb, icv_len - align);
215 else if (align > icv_len)
216 skb_trim(skb, rxdesc->size + iv_len + icv_len);
217
218 /* Move ieee80211 header */
219 memmove(skb->data + transfer,
220 skb->data + transfer + iv_len + align,
221 header_length);
222 transfer += header_length;
223 }
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200224
Ivo van Doorn1ce9cda2008-12-02 18:19:48 +0100225 /* Copy IV/EIV data */
226 memcpy(skb->data + transfer, rxdesc->iv, iv_len);
227 transfer += iv_len;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200228
Ivo van Doorn9f166172009-04-26 16:08:50 +0200229 /*
230 * Move payload for alignment purposes. Note that
231 * this is only needed when no l2 padding is present.
232 */
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200233 if (!(rxdesc->dev_flags & RXDONE_L2PAD)) {
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200234 memmove(skb->data + transfer,
235 skb->data + transfer + align,
236 payload_len);
237 }
238
239 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300240 * NOTE: Always count the payload as transferred,
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200241 * even when alignment was set to zero. This is required
242 * for determining the correct offset for the ICV data.
243 */
244 transfer += payload_len;
245
Ivo van Doorn1ce9cda2008-12-02 18:19:48 +0100246 /*
247 * Copy ICV data
248 * AES appends 8 bytes, we can't fill the upper
249 * 4 bytes, but mac80211 doesn't care about what
250 * we provide here anyway and strips it immediately.
251 */
252 memcpy(skb->data + transfer, &rxdesc->icv, 4);
253 transfer += icv_len;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200254
255 /* IV/EIV/ICV has been inserted into frame */
256 rxdesc->size = transfer;
257 rxdesc->flags &= ~RX_FLAG_IV_STRIPPED;
258}