rt2x00: Move code into seperate functions
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / rt2x00 / rt2x00crypto.c
blobe9d3ddae2785865f6e2e21a8e604ed4ebffb525c
1 /*
2 Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
3 <http://rt2x00.serialmonkey.com>
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.
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.
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.
22 Module: rt2x00lib
23 Abstract: rt2x00 crypto specific routines.
26 #include <linux/kernel.h>
27 #include <linux/module.h>
29 #include "rt2x00.h"
30 #include "rt2x00lib.h"
32 enum cipher rt2x00crypto_key_to_cipher(struct ieee80211_key_conf *key)
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;
49 void rt2x00crypto_create_tx_descriptor(struct queue_entry *entry,
50 struct txentry_desc *txdesc)
52 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
53 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
54 struct ieee80211_key_conf *hw_key = tx_info->control.hw_key;
56 if (!test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags) ||
57 !hw_key || entry->skb->do_not_encrypt)
58 return;
60 __set_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags);
62 txdesc->cipher = rt2x00crypto_key_to_cipher(hw_key);
64 if (hw_key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
65 __set_bit(ENTRY_TXD_ENCRYPT_PAIRWISE, &txdesc->flags);
67 txdesc->key_idx = hw_key->hw_key_idx;
68 txdesc->iv_offset = ieee80211_get_hdrlen_from_skb(entry->skb);
70 if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
71 __set_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc->flags);
73 if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC))
74 __set_bit(ENTRY_TXD_ENCRYPT_MMIC, &txdesc->flags);
77 unsigned int rt2x00crypto_tx_overhead(struct rt2x00_dev *rt2x00dev,
78 struct sk_buff *skb)
80 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
81 struct ieee80211_key_conf *key = tx_info->control.hw_key;
82 unsigned int overhead = 0;
84 if (!test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags) ||
85 !key || skb->do_not_encrypt)
86 return overhead;
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.
93 overhead += key->icv_len;
95 if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
96 overhead += key->iv_len;
98 if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) {
99 if (key->alg == ALG_TKIP)
100 overhead += 8;
103 return overhead;
106 void rt2x00crypto_tx_copy_iv(struct sk_buff *skb, unsigned int iv_len)
108 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
109 unsigned int header_length = ieee80211_get_hdrlen_from_skb(skb);
111 if (unlikely(!iv_len))
112 return;
114 /* Copy IV/EIV data */
115 memcpy(skbdesc->iv, skb->data + header_length, iv_len);
118 void rt2x00crypto_tx_remove_iv(struct sk_buff *skb, unsigned int iv_len)
120 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
121 unsigned int header_length = ieee80211_get_hdrlen_from_skb(skb);
123 if (unlikely(!iv_len))
124 return;
126 /* Copy IV/EIV data */
127 memcpy(skbdesc->iv, skb->data + header_length, iv_len);
129 /* Move ieee80211 header */
130 memmove(skb->data + iv_len, skb->data, header_length);
132 /* Pull buffer to correct size */
133 skb_pull(skb, iv_len);
135 /* IV/EIV data has officially be stripped */
136 skbdesc->flags |= FRAME_DESC_IV_STRIPPED;
139 void rt2x00crypto_tx_insert_iv(struct sk_buff *skb)
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 =
144 ((!!(skbdesc->iv[0])) * 4) + ((!!(skbdesc->iv[1])) * 4);
146 if (!(skbdesc->flags & FRAME_DESC_IV_STRIPPED))
147 return;
149 skb_push(skb, iv_len);
151 /* Move ieee80211 header */
152 memmove(skb->data, skb->data + iv_len, header_length);
154 /* Copy IV/EIV data */
155 memcpy(skb->data + header_length, skbdesc->iv, iv_len);
157 /* IV/EIV data has returned into the frame */
158 skbdesc->flags &= ~FRAME_DESC_IV_STRIPPED;
161 void rt2x00crypto_rx_insert_iv(struct sk_buff *skb, unsigned int align,
162 unsigned int header_length,
163 struct rxdone_entry_desc *rxdesc)
165 unsigned int payload_len = rxdesc->size - header_length;
166 unsigned int iv_len;
167 unsigned int icv_len;
168 unsigned int transfer = 0;
171 * WEP64/WEP128: Provides IV & ICV
172 * TKIP: Provides IV/EIV & ICV
173 * AES: Provies IV/EIV & ICV
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;
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
198 * the padding is smaller than the ICV data.
199 * When alignment requirements is greater than the
200 * ICV data we must trim the skb to the correct size
201 * because we need to remove the extra bytes.
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);
209 /* Move ieee80211 header */
210 memmove(skb->data + transfer,
211 skb->data + transfer + iv_len + align,
212 header_length);
213 transfer += header_length;
215 /* Copy IV/EIV data */
216 memcpy(skb->data + transfer, rxdesc->iv, iv_len);
217 transfer += iv_len;
219 /* Move payload */
220 if (align) {
221 memmove(skb->data + transfer,
222 skb->data + transfer + align,
223 payload_len);
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.
231 transfer += payload_len;
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.
239 memcpy(skb->data + transfer, &rxdesc->icv, 4);
240 transfer += icv_len;
242 /* IV/EIV/ICV has been inserted into frame */
243 rxdesc->size = transfer;
244 rxdesc->flags &= ~RX_FLAG_IV_STRIPPED;