MERGE-master-patchset-edits
[linux-2.6/openmoko-kernel.git] / drivers / net / wireless / rt2x00 / rt2x00crypto.c
blobaee9cba13eb3c3e97ea816841d0ed84059b3fbca
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 ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
53 struct ieee80211_key_conf *hw_key = tx_info->control.hw_key;
55 __set_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags);
57 txdesc->cipher = rt2x00crypto_key_to_cipher(hw_key);
59 if (hw_key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
60 __set_bit(ENTRY_TXD_ENCRYPT_PAIRWISE, &txdesc->flags);
62 txdesc->key_idx = hw_key->hw_key_idx;
63 txdesc->iv_offset = ieee80211_get_hdrlen_from_skb(entry->skb);
65 if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
66 __set_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc->flags);
68 if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC))
69 __set_bit(ENTRY_TXD_ENCRYPT_MMIC, &txdesc->flags);
72 unsigned int rt2x00crypto_tx_overhead(struct ieee80211_tx_info *tx_info)
74 struct ieee80211_key_conf *key = tx_info->control.hw_key;
75 unsigned int overhead = 0;
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.
82 overhead += key->icv_len;
84 if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
85 overhead += key->iv_len;
87 if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) {
88 if (key->alg == ALG_TKIP)
89 overhead += 8;
92 return overhead;
95 void rt2x00crypto_tx_copy_iv(struct sk_buff *skb, unsigned int iv_len)
97 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
98 unsigned int header_length = ieee80211_get_hdrlen_from_skb(skb);
100 if (unlikely(!iv_len))
101 return;
103 /* Copy IV/EIV data */
104 memcpy(skbdesc->iv, skb->data + header_length, iv_len);
107 void rt2x00crypto_tx_remove_iv(struct sk_buff *skb, unsigned int iv_len)
109 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
110 unsigned int header_length = ieee80211_get_hdrlen_from_skb(skb);
112 if (unlikely(!iv_len))
113 return;
115 /* Copy IV/EIV data */
116 memcpy(skbdesc->iv, skb->data + header_length, iv_len);
118 /* Move ieee80211 header */
119 memmove(skb->data + iv_len, skb->data, header_length);
121 /* Pull buffer to correct size */
122 skb_pull(skb, iv_len);
124 /* IV/EIV data has officially be stripped */
125 skbdesc->flags |= FRAME_DESC_IV_STRIPPED;
128 void rt2x00crypto_tx_insert_iv(struct sk_buff *skb)
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 =
133 ((!!(skbdesc->iv[0])) * 4) + ((!!(skbdesc->iv[1])) * 4);
135 if (!(skbdesc->flags & FRAME_DESC_IV_STRIPPED))
136 return;
138 skb_push(skb, iv_len);
140 /* Move ieee80211 header */
141 memmove(skb->data, skb->data + iv_len, header_length);
143 /* Copy IV/EIV data */
144 memcpy(skb->data + header_length, skbdesc->iv, iv_len);
146 /* IV/EIV data has returned into the frame */
147 skbdesc->flags &= ~FRAME_DESC_IV_STRIPPED;
150 void rt2x00crypto_rx_insert_iv(struct sk_buff *skb, unsigned int align,
151 unsigned int header_length,
152 struct rxdone_entry_desc *rxdesc)
154 unsigned int payload_len = rxdesc->size - header_length;
155 unsigned int iv_len;
156 unsigned int icv_len;
157 unsigned int transfer = 0;
160 * WEP64/WEP128: Provides IV & ICV
161 * TKIP: Provides IV/EIV & ICV
162 * AES: Provies IV/EIV & ICV
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;
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
187 * the padding is smaller than the ICV data.
188 * When alignment requirements is greater than the
189 * ICV data we must trim the skb to the correct size
190 * because we need to remove the extra bytes.
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);
198 /* Move ieee80211 header */
199 memmove(skb->data + transfer,
200 skb->data + transfer + iv_len + align,
201 header_length);
202 transfer += header_length;
204 /* Copy IV/EIV data */
205 memcpy(skb->data + transfer, rxdesc->iv, iv_len);
206 transfer += iv_len;
208 /* Move payload */
209 if (align) {
210 memmove(skb->data + transfer,
211 skb->data + transfer + align,
212 payload_len);
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.
220 transfer += payload_len;
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.
228 memcpy(skb->data + transfer, &rxdesc->icv, 4);
229 transfer += icv_len;
231 /* IV/EIV/ICV has been inserted into frame */
232 rxdesc->size = transfer;
233 rxdesc->flags &= ~RX_FLAG_IV_STRIPPED;