rt2x00crypto.c 6.8 KB
Newer Older
I
Ivo van Doorn 已提交
1
/*
2
	Copyright (C) 2004 - 2009 rt2x00 SourceForge Project
I
Ivo van Doorn 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
	<http://rt2x00.serialmonkey.com>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the
	Free Software Foundation, Inc.,
	59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/*
	Module: rt2x00lib
	Abstract: rt2x00 crypto specific routines.
 */

#include <linux/kernel.h>
#include <linux/module.h>

#include "rt2x00.h"
#include "rt2x00lib.h"

enum cipher rt2x00crypto_key_to_cipher(struct ieee80211_key_conf *key)
{
	switch (key->alg) {
	case ALG_WEP:
36
		if (key->keylen == WLAN_KEY_LEN_WEP40)
I
Ivo van Doorn 已提交
37 38 39 40 41 42 43 44 45 46 47 48
			return CIPHER_WEP64;
		else
			return CIPHER_WEP128;
	case ALG_TKIP:
		return CIPHER_TKIP;
	case ALG_CCMP:
		return CIPHER_AES;
	default:
		return CIPHER_NONE;
	}
}

49 50 51
void rt2x00crypto_create_tx_descriptor(struct queue_entry *entry,
				       struct txentry_desc *txdesc)
{
52
	struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
53 54 55
	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
	struct ieee80211_key_conf *hw_key = tx_info->control.hw_key;

56
	if (!test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags) || !hw_key)
57 58
		return;

59 60 61 62 63 64 65 66
	__set_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags);

	txdesc->cipher = rt2x00crypto_key_to_cipher(hw_key);

	if (hw_key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
		__set_bit(ENTRY_TXD_ENCRYPT_PAIRWISE, &txdesc->flags);

	txdesc->key_idx = hw_key->hw_key_idx;
67
	txdesc->iv_offset = txdesc->header_length;
68
	txdesc->iv_len = hw_key->iv_len;
69 70 71 72 73 74 75 76

	if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
		__set_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc->flags);

	if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC))
		__set_bit(ENTRY_TXD_ENCRYPT_MMIC, &txdesc->flags);
}

77 78
unsigned int rt2x00crypto_tx_overhead(struct rt2x00_dev *rt2x00dev,
				      struct sk_buff *skb)
I
Ivo van Doorn 已提交
79
{
80
	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
I
Ivo van Doorn 已提交
81 82 83
	struct ieee80211_key_conf *key = tx_info->control.hw_key;
	unsigned int overhead = 0;

84
	if (!test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags) || !key)
85 86
		return overhead;

I
Ivo van Doorn 已提交
87 88 89 90 91
	/*
	 * Extend frame length to include IV/EIV/ICV/MMIC,
	 * note that these lengths should only be added when
	 * mac80211 does not generate it.
	 */
92
	overhead += key->icv_len;
I
Ivo van Doorn 已提交
93 94

	if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
95
		overhead += key->iv_len;
I
Ivo van Doorn 已提交
96 97 98 99 100 101 102 103 104

	if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) {
		if (key->alg == ALG_TKIP)
			overhead += 8;
	}

	return overhead;
}

105
void rt2x00crypto_tx_copy_iv(struct sk_buff *skb, struct txentry_desc *txdesc)
106 107 108
{
	struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);

109
	if (unlikely(!txdesc->iv_len))
110 111 112
		return;

	/* Copy IV/EIV data */
113
	memcpy(skbdesc->iv, skb->data + txdesc->iv_offset, txdesc->iv_len);
114 115
}

116
void rt2x00crypto_tx_remove_iv(struct sk_buff *skb, struct txentry_desc *txdesc)
I
Ivo van Doorn 已提交
117 118 119
{
	struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);

120
	if (unlikely(!txdesc->iv_len))
I
Ivo van Doorn 已提交
121 122 123
		return;

	/* Copy IV/EIV data */
124
	memcpy(skbdesc->iv, skb->data + txdesc->iv_offset, txdesc->iv_len);
I
Ivo van Doorn 已提交
125 126

	/* Move ieee80211 header */
127
	memmove(skb->data + txdesc->iv_len, skb->data, txdesc->iv_offset);
I
Ivo van Doorn 已提交
128 129

	/* Pull buffer to correct size */
130
	skb_pull(skb, txdesc->iv_len);
I
Ivo van Doorn 已提交
131

L
Luis Correia 已提交
132
	/* IV/EIV data has officially been stripped */
133
	skbdesc->flags |= SKBDESC_IV_STRIPPED;
I
Ivo van Doorn 已提交
134 135
}

136
void rt2x00crypto_tx_insert_iv(struct sk_buff *skb, unsigned int header_length)
I
Ivo van Doorn 已提交
137 138 139
{
	struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
	const unsigned int iv_len =
I
Ivo van Doorn 已提交
140
	    ((!!(skbdesc->iv[0])) * 4) + ((!!(skbdesc->iv[1])) * 4);
I
Ivo van Doorn 已提交
141

142
	if (!(skbdesc->flags & SKBDESC_IV_STRIPPED))
I
Ivo van Doorn 已提交
143 144 145 146 147 148 149 150
		return;

	skb_push(skb, iv_len);

	/* Move ieee80211 header */
	memmove(skb->data, skb->data + iv_len, header_length);

	/* Copy IV/EIV data */
I
Ivo van Doorn 已提交
151
	memcpy(skb->data + header_length, skbdesc->iv, iv_len);
I
Ivo van Doorn 已提交
152 153

	/* IV/EIV data has returned into the frame */
154
	skbdesc->flags &= ~SKBDESC_IV_STRIPPED;
I
Ivo van Doorn 已提交
155 156
}

157
void rt2x00crypto_rx_insert_iv(struct sk_buff *skb,
I
Ivo van Doorn 已提交
158 159 160 161
			       unsigned int header_length,
			       struct rxdone_entry_desc *rxdesc)
{
	unsigned int payload_len = rxdesc->size - header_length;
162
	unsigned int align = ALIGN_SIZE(skb, header_length);
I
Ivo van Doorn 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	unsigned int iv_len;
	unsigned int icv_len;
	unsigned int transfer = 0;

	/*
	 * WEP64/WEP128: Provides IV & ICV
	 * TKIP: Provides IV/EIV & ICV
	 * AES: Provies IV/EIV & ICV
	 */
	switch (rxdesc->cipher) {
	case CIPHER_WEP64:
	case CIPHER_WEP128:
		iv_len = 4;
		icv_len = 4;
		break;
	case CIPHER_TKIP:
		iv_len = 8;
		icv_len = 4;
		break;
	case CIPHER_AES:
		iv_len = 8;
		icv_len = 8;
		break;
	default:
		/* Unsupport type */
		return;
	}

	/*
192 193 194 195 196 197 198 199 200
	 * Make room for new data. There are 2 possibilities
	 * either the alignment is already present between
	 * the 802.11 header and payload. In that case we
	 * we have to move the header less then the iv_len
	 * since we can use the already available l2pad bytes
	 * for the iv data.
	 * When the alignment must be added manually we must
	 * move the header more then iv_len since we must
	 * make room for the payload move as well.
I
Ivo van Doorn 已提交
201
	 */
202
	if (rxdesc->dev_flags & RXDONE_L2PAD) {
203 204
		skb_push(skb, iv_len - align);
		skb_put(skb, icv_len);
I
Ivo van Doorn 已提交
205

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
		/* Move ieee80211 header */
		memmove(skb->data + transfer,
			skb->data + transfer + (iv_len - align),
			header_length);
		transfer += header_length;
	} else {
		skb_push(skb, iv_len + align);
		if (align < icv_len)
			skb_put(skb, icv_len - align);
		else if (align > icv_len)
			skb_trim(skb, rxdesc->size + iv_len + icv_len);

		/* Move ieee80211 header */
		memmove(skb->data + transfer,
			skb->data + transfer + iv_len + align,
			header_length);
		transfer += header_length;
	}
I
Ivo van Doorn 已提交
224

I
Ivo van Doorn 已提交
225 226 227
	/* Copy IV/EIV data */
	memcpy(skb->data + transfer, rxdesc->iv, iv_len);
	transfer += iv_len;
I
Ivo van Doorn 已提交
228

229 230 231 232
	/*
	 * Move payload for alignment purposes. Note that
	 * this is only needed when no l2 padding is present.
	 */
233
	if (!(rxdesc->dev_flags & RXDONE_L2PAD)) {
I
Ivo van Doorn 已提交
234 235 236 237 238 239 240 241 242 243 244 245
		memmove(skb->data + transfer,
			skb->data + transfer + align,
			payload_len);
	}

	/*
	 * NOTE: Always count the payload as transfered,
	 * even when alignment was set to zero. This is required
	 * for determining the correct offset for the ICV data.
	 */
	transfer += payload_len;

I
Ivo van Doorn 已提交
246 247 248 249 250 251 252 253
	/*
	 * Copy ICV data
	 * AES appends 8 bytes, we can't fill the upper
	 * 4 bytes, but mac80211 doesn't care about what
	 * we provide here anyway and strips it immediately.
	 */
	memcpy(skb->data + transfer, &rxdesc->icv, 4);
	transfer += icv_len;
I
Ivo van Doorn 已提交
254 255 256 257 258

	/* IV/EIV/ICV has been inserted into frame */
	rxdesc->size = transfer;
	rxdesc->flags &= ~RX_FLAG_IV_STRIPPED;
}