2 Copyright (C) 2004 - 2009 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.
23 Abstract: rt2x00 crypto specific routines.
26 #include <linux/kernel.h>
27 #include <linux/module.h>
30 #include "rt2x00lib.h"
32 enum cipher rt2x00crypto_key_to_cipher(struct ieee80211_key_conf *key)
36 if (key->keylen == LEN_WEP40)
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)
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 = txdesc->header_length;
69 txdesc->iv_len = hw_key->iv_len;
71 if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
72 __set_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc->flags);
74 if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC))
75 __set_bit(ENTRY_TXD_ENCRYPT_MMIC, &txdesc->flags);
78 unsigned int rt2x00crypto_tx_overhead(struct rt2x00_dev *rt2x00dev,
81 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
82 struct ieee80211_key_conf *key = tx_info->control.hw_key;
83 unsigned int overhead = 0;
85 if (!test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags) ||
86 !key || skb->do_not_encrypt)
90 * Extend frame length to include IV/EIV/ICV/MMIC,
91 * note that these lengths should only be added when
92 * mac80211 does not generate it.
94 overhead += key->icv_len;
96 if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
97 overhead += key->iv_len;
99 if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) {
100 if (key->alg == ALG_TKIP)
107 void rt2x00crypto_tx_copy_iv(struct sk_buff *skb, struct txentry_desc *txdesc)
109 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
111 if (unlikely(!txdesc->iv_len))
114 /* Copy IV/EIV data */
115 memcpy(skbdesc->iv, skb->data + txdesc->iv_offset, txdesc->iv_len);
118 void rt2x00crypto_tx_remove_iv(struct sk_buff *skb, struct txentry_desc *txdesc)
120 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
122 if (unlikely(!txdesc->iv_len))
125 /* Copy IV/EIV data */
126 memcpy(skbdesc->iv, skb->data + txdesc->iv_offset, txdesc->iv_len);
128 /* Move ieee80211 header */
129 memmove(skb->data + txdesc->iv_len, skb->data, txdesc->iv_offset);
131 /* Pull buffer to correct size */
132 skb_pull(skb, txdesc->iv_len);
134 /* IV/EIV data has officially be stripped */
135 skbdesc->flags |= SKBDESC_IV_STRIPPED;
138 void rt2x00crypto_tx_insert_iv(struct sk_buff *skb, unsigned int header_length)
140 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
141 const unsigned int iv_len =
142 ((!!(skbdesc->iv[0])) * 4) + ((!!(skbdesc->iv[1])) * 4);
144 if (!(skbdesc->flags & SKBDESC_IV_STRIPPED))
147 skb_push(skb, iv_len);
149 /* Move ieee80211 header */
150 memmove(skb->data, skb->data + iv_len, header_length);
152 /* Copy IV/EIV data */
153 memcpy(skb->data + header_length, skbdesc->iv, iv_len);
155 /* IV/EIV data has returned into the frame */
156 skbdesc->flags &= ~SKBDESC_IV_STRIPPED;
159 void rt2x00crypto_rx_insert_iv(struct sk_buff *skb, bool l2pad,
160 unsigned int header_length,
161 struct rxdone_entry_desc *rxdesc)
163 unsigned int payload_len = rxdesc->size - header_length;
164 unsigned int align = ALIGN_SIZE(skb, header_length);
166 unsigned int icv_len;
167 unsigned int transfer = 0;
170 * WEP64/WEP128: Provides IV & ICV
171 * TKIP: Provides IV/EIV & ICV
172 * AES: Provies IV/EIV & ICV
174 switch (rxdesc->cipher) {
194 * Make room for new data. There are 2 possibilities
195 * either the alignment is already present between
196 * the 802.11 header and payload. In that case we
197 * we have to move the header less then the iv_len
198 * since we can use the already available l2pad bytes
200 * When the alignment must be added manually we must
201 * move the header more then iv_len since we must
202 * make room for the payload move as well.
205 skb_push(skb, iv_len - align);
206 skb_put(skb, icv_len);
208 /* Move ieee80211 header */
209 memmove(skb->data + transfer,
210 skb->data + transfer + (iv_len - align),
212 transfer += header_length;
214 skb_push(skb, iv_len + align);
216 skb_put(skb, icv_len - align);
217 else if (align > icv_len)
218 skb_trim(skb, rxdesc->size + iv_len + icv_len);
220 /* Move ieee80211 header */
221 memmove(skb->data + transfer,
222 skb->data + transfer + iv_len + align,
224 transfer += header_length;
227 /* Copy IV/EIV data */
228 memcpy(skb->data + transfer, rxdesc->iv, iv_len);
232 * Move payload for alignment purposes. Note that
233 * this is only needed when no l2 padding is present.
236 memmove(skb->data + transfer,
237 skb->data + transfer + align,
242 * NOTE: Always count the payload as transfered,
243 * even when alignment was set to zero. This is required
244 * for determining the correct offset for the ICV data.
246 transfer += payload_len;
250 * AES appends 8 bytes, we can't fill the upper
251 * 4 bytes, but mac80211 doesn't care about what
252 * we provide here anyway and strips it immediately.
254 memcpy(skb->data + transfer, &rxdesc->icv, 4);
257 /* IV/EIV/ICV has been inserted into frame */
258 rxdesc->size = transfer;
259 rxdesc->flags &= ~RX_FLAG_IV_STRIPPED;