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 += key->icv_len;
61 if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
62 overhead += key->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 */
81 memcpy(skbdesc->iv, skb->data + header_length, iv_len);
83 /* Move ieee80211 header */
84 memmove(skb->data + iv_len, skb->data, header_length);
86 /* Pull buffer to correct size */
87 skb_pull(skb, iv_len);
89 /* IV/EIV data has officially be stripped */
90 skbdesc->flags |= FRAME_DESC_IV_STRIPPED;
93 void rt2x00crypto_tx_insert_iv(struct sk_buff *skb)
95 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
96 unsigned int header_length = ieee80211_get_hdrlen_from_skb(skb);
97 const unsigned int iv_len =
98 ((!!(skbdesc->iv[0])) * 4) + ((!!(skbdesc->iv[1])) * 4);
100 if (!(skbdesc->flags & FRAME_DESC_IV_STRIPPED))
103 skb_push(skb, iv_len);
105 /* Move ieee80211 header */
106 memmove(skb->data, skb->data + iv_len, header_length);
108 /* Copy IV/EIV data */
109 memcpy(skb->data + header_length, skbdesc->iv, iv_len);
111 /* IV/EIV data has returned into the frame */
112 skbdesc->flags &= ~FRAME_DESC_IV_STRIPPED;
115 void rt2x00crypto_rx_insert_iv(struct sk_buff *skb, unsigned int align,
116 unsigned int header_length,
117 struct rxdone_entry_desc *rxdesc)
119 unsigned int payload_len = rxdesc->size - header_length;
121 unsigned int icv_len;
122 unsigned int transfer = 0;
125 * WEP64/WEP128: Provides IV & ICV
126 * TKIP: Provides IV/EIV & ICV
127 * AES: Provies IV/EIV & ICV
129 switch (rxdesc->cipher) {
149 * Make room for new data, note that we increase both
150 * headsize and tailsize when required. The tailsize is
151 * only needed when ICV data needs to be inserted and
152 * the padding is smaller then the ICV data.
153 * When alignment requirements is greater then the
154 * ICV data we must trim the skb to the correct size
155 * because we need to remove the extra bytes.
157 skb_push(skb, iv_len + align);
159 skb_put(skb, icv_len - align);
160 else if (align > icv_len)
161 skb_trim(skb, rxdesc->size + iv_len + icv_len);
163 /* Move ieee80211 header */
164 memmove(skb->data + transfer,
165 skb->data + transfer + iv_len + align,
167 transfer += header_length;
169 /* Copy IV/EIV data */
170 memcpy(skb->data + transfer, rxdesc->iv, iv_len);
175 memmove(skb->data + transfer,
176 skb->data + transfer + align,
181 * NOTE: Always count the payload as transfered,
182 * even when alignment was set to zero. This is required
183 * for determining the correct offset for the ICV data.
185 transfer += payload_len;
189 * AES appends 8 bytes, we can't fill the upper
190 * 4 bytes, but mac80211 doesn't care about what
191 * we provide here anyway and strips it immediately.
193 memcpy(skb->data + transfer, &rxdesc->icv, 4);
196 /* IV/EIV/ICV has been inserted into frame */
197 rxdesc->size = transfer;
198 rxdesc->flags &= ~RX_FLAG_IV_STRIPPED;