blob: a2fd05ba25ca886f2037baec9fb581b718ecbdd7 [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
Jeff Kirshera05b8c52013-12-06 03:32:11 -080016 along with this program; if not, see <http://www.gnu.org/licenses/>.
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020017 */
18
19/*
20 Module: rt2x00lib
21 Abstract: rt2x00 crypto specific routines.
22 */
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26
27#include "rt2x00.h"
28#include "rt2x00lib.h"
29
30enum cipher rt2x00crypto_key_to_cipher(struct ieee80211_key_conf *key)
31{
Johannes Berg97359d12010-08-10 09:46:38 +020032 switch (key->cipher) {
33 case WLAN_CIPHER_SUITE_WEP40:
34 return CIPHER_WEP64;
35 case WLAN_CIPHER_SUITE_WEP104:
36 return CIPHER_WEP128;
37 case WLAN_CIPHER_SUITE_TKIP:
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020038 return CIPHER_TKIP;
Johannes Berg97359d12010-08-10 09:46:38 +020039 case WLAN_CIPHER_SUITE_CCMP:
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020040 return CIPHER_AES;
41 default:
42 return CIPHER_NONE;
43 }
44}
45
Gertjan van Wingerde77b56212011-07-06 22:57:00 +020046void rt2x00crypto_create_tx_descriptor(struct rt2x00_dev *rt2x00dev,
47 struct sk_buff *skb,
Ivo van Doorn9c3444d2008-12-03 17:29:48 +010048 struct txentry_desc *txdesc)
49{
Gertjan van Wingerde77b56212011-07-06 22:57:00 +020050 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
Ivo van Doorn9c3444d2008-12-03 17:29:48 +010051 struct ieee80211_key_conf *hw_key = tx_info->control.hw_key;
52
Gabor Juhos7b8a00d2013-10-11 13:18:41 +020053 if (!rt2x00_has_cap_hw_crypto(rt2x00dev) || !hw_key)
Ivo van Doorn7b409822008-12-20 10:58:33 +010054 return;
55
Ivo van Doorn9c3444d2008-12-03 17:29:48 +010056 __set_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags);
57
58 txdesc->cipher = rt2x00crypto_key_to_cipher(hw_key);
59
60 if (hw_key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
61 __set_bit(ENTRY_TXD_ENCRYPT_PAIRWISE, &txdesc->flags);
62
63 txdesc->key_idx = hw_key->hw_key_idx;
Ivo van Doorn9f166172009-04-26 16:08:50 +020064 txdesc->iv_offset = txdesc->header_length;
Ivo van Doorn9eb4e212009-04-26 16:08:30 +020065 txdesc->iv_len = hw_key->iv_len;
Ivo van Doorn9c3444d2008-12-03 17:29:48 +010066
67 if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
68 __set_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc->flags);
69
70 if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC))
71 __set_bit(ENTRY_TXD_ENCRYPT_MMIC, &txdesc->flags);
72}
73
Ivo van Doorn7b409822008-12-20 10:58:33 +010074unsigned int rt2x00crypto_tx_overhead(struct rt2x00_dev *rt2x00dev,
75 struct sk_buff *skb)
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020076{
Ivo van Doorn7b409822008-12-20 10:58:33 +010077 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020078 struct ieee80211_key_conf *key = tx_info->control.hw_key;
79 unsigned int overhead = 0;
80
Gabor Juhos7b8a00d2013-10-11 13:18:41 +020081 if (!rt2x00_has_cap_hw_crypto(rt2x00dev) || !key)
Ivo van Doorn7b409822008-12-20 10:58:33 +010082 return overhead;
83
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020084 /*
85 * Extend frame length to include IV/EIV/ICV/MMIC,
86 * note that these lengths should only be added when
87 * mac80211 does not generate it.
88 */
Felix Fietkau76708de2008-10-05 18:02:48 +020089 overhead += key->icv_len;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020090
91 if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
Felix Fietkau76708de2008-10-05 18:02:48 +020092 overhead += key->iv_len;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020093
94 if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) {
Johannes Berg97359d12010-08-10 09:46:38 +020095 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020096 overhead += 8;
97 }
98
99 return overhead;
100}
101
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200102void rt2x00crypto_tx_copy_iv(struct sk_buff *skb, struct txentry_desc *txdesc)
Ivo van Doorndddfb472008-12-02 18:20:42 +0100103{
104 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
Ivo van Doorndddfb472008-12-02 18:20:42 +0100105
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200106 if (unlikely(!txdesc->iv_len))
Ivo van Doorndddfb472008-12-02 18:20:42 +0100107 return;
108
109 /* Copy IV/EIV data */
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200110 memcpy(skbdesc->iv, skb->data + txdesc->iv_offset, txdesc->iv_len);
Ivo van Doorndddfb472008-12-02 18:20:42 +0100111}
112
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200113void rt2x00crypto_tx_remove_iv(struct sk_buff *skb, struct txentry_desc *txdesc)
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200114{
115 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200116
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200117 if (unlikely(!txdesc->iv_len))
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200118 return;
119
120 /* Copy IV/EIV data */
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200121 memcpy(skbdesc->iv, skb->data + txdesc->iv_offset, txdesc->iv_len);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200122
123 /* Move ieee80211 header */
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200124 memmove(skb->data + txdesc->iv_len, skb->data, txdesc->iv_offset);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200125
126 /* Pull buffer to correct size */
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200127 skb_pull(skb, txdesc->iv_len);
Gertjan van Wingerded43e49ec2010-05-08 23:40:18 +0200128 txdesc->length -= txdesc->iv_len;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200129
Luis Correia49513482009-07-17 21:39:19 +0200130 /* IV/EIV data has officially been stripped */
Ivo van Doorn9f166172009-04-26 16:08:50 +0200131 skbdesc->flags |= SKBDESC_IV_STRIPPED;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200132}
133
Ivo van Doorn9f166172009-04-26 16:08:50 +0200134void rt2x00crypto_tx_insert_iv(struct sk_buff *skb, unsigned int header_length)
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200135{
136 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200137 const unsigned int iv_len =
Ivo van Doorn1ce9cda2008-12-02 18:19:48 +0100138 ((!!(skbdesc->iv[0])) * 4) + ((!!(skbdesc->iv[1])) * 4);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200139
Ivo van Doorn9f166172009-04-26 16:08:50 +0200140 if (!(skbdesc->flags & SKBDESC_IV_STRIPPED))
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200141 return;
142
143 skb_push(skb, iv_len);
144
145 /* Move ieee80211 header */
146 memmove(skb->data, skb->data + iv_len, header_length);
147
148 /* Copy IV/EIV data */
Ivo van Doorn1ce9cda2008-12-02 18:19:48 +0100149 memcpy(skb->data + header_length, skbdesc->iv, iv_len);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200150
151 /* IV/EIV data has returned into the frame */
Ivo van Doorn9f166172009-04-26 16:08:50 +0200152 skbdesc->flags &= ~SKBDESC_IV_STRIPPED;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200153}
154
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200155void rt2x00crypto_rx_insert_iv(struct sk_buff *skb,
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200156 unsigned int header_length,
157 struct rxdone_entry_desc *rxdesc)
158{
159 unsigned int payload_len = rxdesc->size - header_length;
Ivo van Doorn9f166172009-04-26 16:08:50 +0200160 unsigned int align = ALIGN_SIZE(skb, header_length);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200161 unsigned int iv_len;
162 unsigned int icv_len;
163 unsigned int transfer = 0;
164
165 /*
166 * WEP64/WEP128: Provides IV & ICV
167 * TKIP: Provides IV/EIV & ICV
168 * AES: Provies IV/EIV & ICV
169 */
170 switch (rxdesc->cipher) {
171 case CIPHER_WEP64:
172 case CIPHER_WEP128:
173 iv_len = 4;
174 icv_len = 4;
175 break;
176 case CIPHER_TKIP:
177 iv_len = 8;
178 icv_len = 4;
179 break;
180 case CIPHER_AES:
181 iv_len = 8;
182 icv_len = 8;
183 break;
184 default:
185 /* Unsupport type */
186 return;
187 }
188
189 /*
Ivo van Doorn9f166172009-04-26 16:08:50 +0200190 * Make room for new data. There are 2 possibilities
191 * either the alignment is already present between
192 * the 802.11 header and payload. In that case we
193 * we have to move the header less then the iv_len
194 * since we can use the already available l2pad bytes
195 * for the iv data.
196 * When the alignment must be added manually we must
197 * move the header more then iv_len since we must
198 * make room for the payload move as well.
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200199 */
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200200 if (rxdesc->dev_flags & RXDONE_L2PAD) {
Ivo van Doorn9f166172009-04-26 16:08:50 +0200201 skb_push(skb, iv_len - align);
202 skb_put(skb, icv_len);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200203
Ivo van Doorn9f166172009-04-26 16:08:50 +0200204 /* Move ieee80211 header */
205 memmove(skb->data + transfer,
206 skb->data + transfer + (iv_len - align),
207 header_length);
208 transfer += header_length;
209 } else {
210 skb_push(skb, iv_len + align);
211 if (align < icv_len)
212 skb_put(skb, icv_len - align);
213 else if (align > icv_len)
214 skb_trim(skb, rxdesc->size + iv_len + icv_len);
215
216 /* Move ieee80211 header */
217 memmove(skb->data + transfer,
218 skb->data + transfer + iv_len + align,
219 header_length);
220 transfer += header_length;
221 }
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200222
Ivo van Doorn1ce9cda2008-12-02 18:19:48 +0100223 /* Copy IV/EIV data */
224 memcpy(skb->data + transfer, rxdesc->iv, iv_len);
225 transfer += iv_len;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200226
Ivo van Doorn9f166172009-04-26 16:08:50 +0200227 /*
228 * Move payload for alignment purposes. Note that
229 * this is only needed when no l2 padding is present.
230 */
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200231 if (!(rxdesc->dev_flags & RXDONE_L2PAD)) {
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200232 memmove(skb->data + transfer,
233 skb->data + transfer + align,
234 payload_len);
235 }
236
237 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300238 * NOTE: Always count the payload as transferred,
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200239 * even when alignment was set to zero. This is required
240 * for determining the correct offset for the ICV data.
241 */
242 transfer += payload_len;
243
Ivo van Doorn1ce9cda2008-12-02 18:19:48 +0100244 /*
245 * Copy ICV data
246 * AES appends 8 bytes, we can't fill the upper
247 * 4 bytes, but mac80211 doesn't care about what
248 * we provide here anyway and strips it immediately.
249 */
250 memcpy(skb->data + transfer, &rxdesc->icv, 4);
251 transfer += icv_len;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200252
253 /* IV/EIV/ICV has been inserted into frame */
254 rxdesc->size = transfer;
255 rxdesc->flags &= ~RX_FLAG_IV_STRIPPED;
256}