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.
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 unsigned int rt2x00crypto_tx_overhead(struct ieee80211_tx_info *tx_info)
51 struct ieee80211_key_conf *key = tx_info->control.hw_key;
52 unsigned int overhead = 0;
55 * Extend frame length to include IV/EIV/ICV/MMIC,
56 * note that these lengths should only be added when
57 * mac80211 does not generate it.
59 overhead += tx_info->control.icv_len;
61 if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
62 overhead += tx_info->control.iv_len;
64 if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) {
65 if (key->alg == ALG_TKIP)
72 void rt2x00crypto_tx_remove_iv(struct sk_buff *skb, unsigned int iv_len)
74 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
75 unsigned int header_length = ieee80211_get_hdrlen_from_skb(skb);
77 if (unlikely(!iv_len))
80 /* Copy IV/EIV data */
82 memcpy(&skbdesc->iv, skb->data + header_length, 4);
84 memcpy(&skbdesc->eiv, skb->data + header_length + 4, 4);
86 /* Move ieee80211 header */
87 memmove(skb->data + iv_len, skb->data, header_length);
89 /* Pull buffer to correct size */
90 skb_pull(skb, iv_len);
92 /* IV/EIV data has officially be stripped */
93 skbdesc->flags |= FRAME_DESC_IV_STRIPPED;
96 void rt2x00crypto_tx_insert_iv(struct sk_buff *skb)
98 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
99 unsigned int header_length = ieee80211_get_hdrlen_from_skb(skb);
100 const unsigned int iv_len =
101 ((!!(skbdesc->iv)) * 4) + ((!!(skbdesc->eiv)) * 4);
103 if (!(skbdesc->flags & FRAME_DESC_IV_STRIPPED))
106 skb_push(skb, iv_len);
108 /* Move ieee80211 header */
109 memmove(skb->data, skb->data + iv_len, header_length);
111 /* Copy IV/EIV data */
113 memcpy(skb->data + header_length, &skbdesc->iv, 4);
115 memcpy(skb->data + header_length + 4, &skbdesc->eiv, 4);
117 /* IV/EIV data has returned into the frame */
118 skbdesc->flags &= ~FRAME_DESC_IV_STRIPPED;
121 void rt2x00crypto_rx_insert_iv(struct sk_buff *skb, unsigned int align,
122 unsigned int header_length,
123 struct rxdone_entry_desc *rxdesc)
125 unsigned int payload_len = rxdesc->size - header_length;
127 unsigned int icv_len;
128 unsigned int transfer = 0;
131 * WEP64/WEP128: Provides IV & ICV
132 * TKIP: Provides IV/EIV & ICV
133 * AES: Provies IV/EIV & ICV
135 switch (rxdesc->cipher) {
155 * Make room for new data, note that we increase both
156 * headsize and tailsize when required. The tailsize is
157 * only needed when ICV data needs to be inserted and
158 * the padding is smaller then the ICV data.
159 * When alignment requirements is greater then the
160 * ICV data we must trim the skb to the correct size
161 * because we need to remove the extra bytes.
163 skb_push(skb, iv_len + align);
165 skb_put(skb, icv_len - align);
166 else if (align > icv_len)
167 skb_trim(skb, rxdesc->size + iv_len + icv_len);
169 /* Move ieee80211 header */
170 memmove(skb->data + transfer,
171 skb->data + transfer + iv_len + align,
173 transfer += header_length;
177 memcpy(skb->data + transfer, &rxdesc->iv, 4);
183 memcpy(skb->data + transfer, &rxdesc->eiv, 4);
189 memmove(skb->data + transfer,
190 skb->data + transfer + align,
195 * NOTE: Always count the payload as transfered,
196 * even when alignment was set to zero. This is required
197 * for determining the correct offset for the ICV data.
199 transfer += payload_len;
203 memcpy(skb->data + transfer, &rxdesc->icv, 4);
205 * AES appends 8 bytes, we can't fill the upper
206 * 4 bytes, but mac80211 doesn't care about what
207 * we provide here anyway and strips it immediately.
212 /* IV/EIV/ICV has been inserted into frame */
213 rxdesc->size = transfer;
214 rxdesc->flags &= ~RX_FLAG_IV_STRIPPED;