• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6.36/drivers/net/wireless/rt2x00/
1/*
2	Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
3	<http://rt2x00.serialmonkey.com>
4
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.
9
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.
14
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.
19 */
20
21/*
22	Module: rt2x00lib
23	Abstract: rt2x00 crypto specific routines.
24 */
25
26#include <linux/kernel.h>
27#include <linux/module.h>
28
29#include "rt2x00.h"
30#include "rt2x00lib.h"
31
32enum cipher rt2x00crypto_key_to_cipher(struct ieee80211_key_conf *key)
33{
34	switch (key->alg) {
35	case ALG_WEP:
36		if (key->keylen == WLAN_KEY_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;
46	}
47}
48
49void rt2x00crypto_create_tx_descriptor(struct queue_entry *entry,
50				       struct txentry_desc *txdesc)
51{
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;
55
56	if (!test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags) || !hw_key)
57		return;
58
59	__set_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags);
60
61	txdesc->cipher = rt2x00crypto_key_to_cipher(hw_key);
62
63	if (hw_key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
64		__set_bit(ENTRY_TXD_ENCRYPT_PAIRWISE, &txdesc->flags);
65
66	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	if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
71		__set_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc->flags);
72
73	if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC))
74		__set_bit(ENTRY_TXD_ENCRYPT_MMIC, &txdesc->flags);
75}
76
77unsigned int rt2x00crypto_tx_overhead(struct rt2x00_dev *rt2x00dev,
78				      struct sk_buff *skb)
79{
80	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
81	struct ieee80211_key_conf *key = tx_info->control.hw_key;
82	unsigned int overhead = 0;
83
84	if (!test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags) || !key)
85		return overhead;
86
87	/*
88	 * Extend frame length to include IV/EIV/ICV/MMIC,
89	 * note that these lengths should only be added when
90	 * mac80211 does not generate it.
91	 */
92	overhead += key->icv_len;
93
94	if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
95		overhead += key->iv_len;
96
97	if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) {
98		if (key->alg == ALG_TKIP)
99			overhead += 8;
100	}
101
102	return overhead;
103}
104
105void rt2x00crypto_tx_copy_iv(struct sk_buff *skb, struct txentry_desc *txdesc)
106{
107	struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
108
109	if (unlikely(!txdesc->iv_len))
110		return;
111
112	/* Copy IV/EIV data */
113	memcpy(skbdesc->iv, skb->data + txdesc->iv_offset, txdesc->iv_len);
114}
115
116void rt2x00crypto_tx_remove_iv(struct sk_buff *skb, struct txentry_desc *txdesc)
117{
118	struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
119
120	if (unlikely(!txdesc->iv_len))
121		return;
122
123	/* Copy IV/EIV data */
124	memcpy(skbdesc->iv, skb->data + txdesc->iv_offset, txdesc->iv_len);
125
126	/* Move ieee80211 header */
127	memmove(skb->data + txdesc->iv_len, skb->data, txdesc->iv_offset);
128
129	/* Pull buffer to correct size */
130	skb_pull(skb, txdesc->iv_len);
131	txdesc->length -= txdesc->iv_len;
132
133	/* IV/EIV data has officially been stripped */
134	skbdesc->flags |= SKBDESC_IV_STRIPPED;
135}
136
137void rt2x00crypto_tx_insert_iv(struct sk_buff *skb, unsigned int header_length)
138{
139	struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
140	const unsigned int iv_len =
141	    ((!!(skbdesc->iv[0])) * 4) + ((!!(skbdesc->iv[1])) * 4);
142
143	if (!(skbdesc->flags & SKBDESC_IV_STRIPPED))
144		return;
145
146	skb_push(skb, iv_len);
147
148	/* Move ieee80211 header */
149	memmove(skb->data, skb->data + iv_len, header_length);
150
151	/* Copy IV/EIV data */
152	memcpy(skb->data + header_length, skbdesc->iv, iv_len);
153
154	/* IV/EIV data has returned into the frame */
155	skbdesc->flags &= ~SKBDESC_IV_STRIPPED;
156}
157
158void rt2x00crypto_rx_insert_iv(struct sk_buff *skb,
159			       unsigned int header_length,
160			       struct rxdone_entry_desc *rxdesc)
161{
162	unsigned int payload_len = rxdesc->size - header_length;
163	unsigned int align = ALIGN_SIZE(skb, header_length);
164	unsigned int iv_len;
165	unsigned int icv_len;
166	unsigned int transfer = 0;
167
168	/*
169	 * WEP64/WEP128: Provides IV & ICV
170	 * TKIP: Provides IV/EIV & ICV
171	 * AES: Provies IV/EIV & ICV
172	 */
173	switch (rxdesc->cipher) {
174	case CIPHER_WEP64:
175	case CIPHER_WEP128:
176		iv_len = 4;
177		icv_len = 4;
178		break;
179	case CIPHER_TKIP:
180		iv_len = 8;
181		icv_len = 4;
182		break;
183	case CIPHER_AES:
184		iv_len = 8;
185		icv_len = 8;
186		break;
187	default:
188		/* Unsupport type */
189		return;
190	}
191
192	/*
193	 * Make room for new data. There are 2 possibilities
194	 * either the alignment is already present between
195	 * the 802.11 header and payload. In that case we
196	 * we have to move the header less then the iv_len
197	 * since we can use the already available l2pad bytes
198	 * for the iv data.
199	 * When the alignment must be added manually we must
200	 * move the header more then iv_len since we must
201	 * make room for the payload move as well.
202	 */
203	if (rxdesc->dev_flags & RXDONE_L2PAD) {
204		skb_push(skb, iv_len - align);
205		skb_put(skb, icv_len);
206
207		/* Move ieee80211 header */
208		memmove(skb->data + transfer,
209			skb->data + transfer + (iv_len - align),
210			header_length);
211		transfer += header_length;
212	} else {
213		skb_push(skb, iv_len + align);
214		if (align < icv_len)
215			skb_put(skb, icv_len - align);
216		else if (align > icv_len)
217			skb_trim(skb, rxdesc->size + iv_len + icv_len);
218
219		/* Move ieee80211 header */
220		memmove(skb->data + transfer,
221			skb->data + transfer + iv_len + align,
222			header_length);
223		transfer += header_length;
224	}
225
226	/* Copy IV/EIV data */
227	memcpy(skb->data + transfer, rxdesc->iv, iv_len);
228	transfer += iv_len;
229
230	/*
231	 * Move payload for alignment purposes. Note that
232	 * this is only needed when no l2 padding is present.
233	 */
234	if (!(rxdesc->dev_flags & RXDONE_L2PAD)) {
235		memmove(skb->data + transfer,
236			skb->data + transfer + align,
237			payload_len);
238	}
239
240	/*
241	 * NOTE: Always count the payload as transfered,
242	 * even when alignment was set to zero. This is required
243	 * for determining the correct offset for the ICV data.
244	 */
245	transfer += payload_len;
246
247	/*
248	 * Copy ICV data
249	 * AES appends 8 bytes, we can't fill the upper
250	 * 4 bytes, but mac80211 doesn't care about what
251	 * we provide here anyway and strips it immediately.
252	 */
253	memcpy(skb->data + transfer, &rxdesc->icv, 4);
254	transfer += icv_len;
255
256	/* IV/EIV/ICV has been inserted into frame */
257	rxdesc->size = transfer;
258	rxdesc->flags &= ~RX_FLAG_IV_STRIPPED;
259}
260