Commit | Line | Data |
---|---|---|
2bb057d0 | 1 | /* |
4e54c711 | 2 | Copyright (C) 2004 - 2009 rt2x00 SourceForge Project |
2bb057d0 ID |
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 | ||
32 | enum cipher rt2x00crypto_key_to_cipher(struct ieee80211_key_conf *key) | |
33 | { | |
34 | switch (key->alg) { | |
35 | case ALG_WEP: | |
36 | if (key->keylen == 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 | ||
9c3444d3 ID |
49 | void rt2x00crypto_create_tx_descriptor(struct queue_entry *entry, |
50 | struct txentry_desc *txdesc) | |
51 | { | |
7b40982e | 52 | struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev; |
9c3444d3 ID |
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 | ||
7b40982e ID |
56 | if (!test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags) || |
57 | !hw_key || entry->skb->do_not_encrypt) | |
58 | return; | |
59 | ||
9c3444d3 ID |
60 | __set_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags); |
61 | ||
62 | txdesc->cipher = rt2x00crypto_key_to_cipher(hw_key); | |
63 | ||
64 | if (hw_key->flags & IEEE80211_KEY_FLAG_PAIRWISE) | |
65 | __set_bit(ENTRY_TXD_ENCRYPT_PAIRWISE, &txdesc->flags); | |
66 | ||
67 | txdesc->key_idx = hw_key->hw_key_idx; | |
68 | txdesc->iv_offset = ieee80211_get_hdrlen_from_skb(entry->skb); | |
9eb4e21e | 69 | txdesc->iv_len = hw_key->iv_len; |
9c3444d3 ID |
70 | |
71 | if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV)) | |
72 | __set_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc->flags); | |
73 | ||
74 | if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) | |
75 | __set_bit(ENTRY_TXD_ENCRYPT_MMIC, &txdesc->flags); | |
76 | } | |
77 | ||
7b40982e ID |
78 | unsigned int rt2x00crypto_tx_overhead(struct rt2x00_dev *rt2x00dev, |
79 | struct sk_buff *skb) | |
2bb057d0 | 80 | { |
7b40982e | 81 | struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb); |
2bb057d0 ID |
82 | struct ieee80211_key_conf *key = tx_info->control.hw_key; |
83 | unsigned int overhead = 0; | |
84 | ||
7b40982e ID |
85 | if (!test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags) || |
86 | !key || skb->do_not_encrypt) | |
87 | return overhead; | |
88 | ||
2bb057d0 ID |
89 | /* |
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. | |
93 | */ | |
76708dee | 94 | overhead += key->icv_len; |
2bb057d0 ID |
95 | |
96 | if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_IV)) | |
76708dee | 97 | overhead += key->iv_len; |
2bb057d0 ID |
98 | |
99 | if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) { | |
100 | if (key->alg == ALG_TKIP) | |
101 | overhead += 8; | |
102 | } | |
103 | ||
104 | return overhead; | |
105 | } | |
106 | ||
9eb4e21e | 107 | void rt2x00crypto_tx_copy_iv(struct sk_buff *skb, struct txentry_desc *txdesc) |
dddfb478 ID |
108 | { |
109 | struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb); | |
dddfb478 | 110 | |
9eb4e21e | 111 | if (unlikely(!txdesc->iv_len)) |
dddfb478 ID |
112 | return; |
113 | ||
114 | /* Copy IV/EIV data */ | |
9eb4e21e | 115 | memcpy(skbdesc->iv, skb->data + txdesc->iv_offset, txdesc->iv_len); |
dddfb478 ID |
116 | } |
117 | ||
9eb4e21e | 118 | void rt2x00crypto_tx_remove_iv(struct sk_buff *skb, struct txentry_desc *txdesc) |
2bb057d0 ID |
119 | { |
120 | struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb); | |
2bb057d0 | 121 | |
9eb4e21e | 122 | if (unlikely(!txdesc->iv_len)) |
2bb057d0 ID |
123 | return; |
124 | ||
125 | /* Copy IV/EIV data */ | |
9eb4e21e | 126 | memcpy(skbdesc->iv, skb->data + txdesc->iv_offset, txdesc->iv_len); |
2bb057d0 ID |
127 | |
128 | /* Move ieee80211 header */ | |
9eb4e21e | 129 | memmove(skb->data + txdesc->iv_len, skb->data, txdesc->iv_offset); |
2bb057d0 ID |
130 | |
131 | /* Pull buffer to correct size */ | |
9eb4e21e | 132 | skb_pull(skb, txdesc->iv_len); |
2bb057d0 ID |
133 | |
134 | /* IV/EIV data has officially be stripped */ | |
135 | skbdesc->flags |= FRAME_DESC_IV_STRIPPED; | |
136 | } | |
137 | ||
138 | void rt2x00crypto_tx_insert_iv(struct sk_buff *skb) | |
139 | { | |
140 | struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb); | |
141 | unsigned int header_length = ieee80211_get_hdrlen_from_skb(skb); | |
142 | const unsigned int iv_len = | |
1ce9cdac | 143 | ((!!(skbdesc->iv[0])) * 4) + ((!!(skbdesc->iv[1])) * 4); |
2bb057d0 ID |
144 | |
145 | if (!(skbdesc->flags & FRAME_DESC_IV_STRIPPED)) | |
146 | return; | |
147 | ||
148 | skb_push(skb, iv_len); | |
149 | ||
150 | /* Move ieee80211 header */ | |
151 | memmove(skb->data, skb->data + iv_len, header_length); | |
152 | ||
153 | /* Copy IV/EIV data */ | |
1ce9cdac | 154 | memcpy(skb->data + header_length, skbdesc->iv, iv_len); |
2bb057d0 ID |
155 | |
156 | /* IV/EIV data has returned into the frame */ | |
157 | skbdesc->flags &= ~FRAME_DESC_IV_STRIPPED; | |
158 | } | |
159 | ||
160 | void rt2x00crypto_rx_insert_iv(struct sk_buff *skb, unsigned int align, | |
161 | unsigned int header_length, | |
162 | struct rxdone_entry_desc *rxdesc) | |
163 | { | |
164 | unsigned int payload_len = rxdesc->size - header_length; | |
165 | unsigned int iv_len; | |
166 | unsigned int icv_len; | |
167 | unsigned int transfer = 0; | |
168 | ||
169 | /* | |
170 | * WEP64/WEP128: Provides IV & ICV | |
171 | * TKIP: Provides IV/EIV & ICV | |
172 | * AES: Provies IV/EIV & ICV | |
173 | */ | |
174 | switch (rxdesc->cipher) { | |
175 | case CIPHER_WEP64: | |
176 | case CIPHER_WEP128: | |
177 | iv_len = 4; | |
178 | icv_len = 4; | |
179 | break; | |
180 | case CIPHER_TKIP: | |
181 | iv_len = 8; | |
182 | icv_len = 4; | |
183 | break; | |
184 | case CIPHER_AES: | |
185 | iv_len = 8; | |
186 | icv_len = 8; | |
187 | break; | |
188 | default: | |
189 | /* Unsupport type */ | |
190 | return; | |
191 | } | |
192 | ||
193 | /* | |
194 | * Make room for new data, note that we increase both | |
195 | * headsize and tailsize when required. The tailsize is | |
196 | * only needed when ICV data needs to be inserted and | |
025dfdaf FS |
197 | * the padding is smaller than the ICV data. |
198 | * When alignment requirements is greater than the | |
2bb057d0 ID |
199 | * ICV data we must trim the skb to the correct size |
200 | * because we need to remove the extra bytes. | |
201 | */ | |
202 | skb_push(skb, iv_len + align); | |
203 | if (align < icv_len) | |
204 | skb_put(skb, icv_len - align); | |
205 | else if (align > icv_len) | |
206 | skb_trim(skb, rxdesc->size + iv_len + icv_len); | |
207 | ||
208 | /* Move ieee80211 header */ | |
209 | memmove(skb->data + transfer, | |
210 | skb->data + transfer + iv_len + align, | |
211 | header_length); | |
212 | transfer += header_length; | |
213 | ||
1ce9cdac ID |
214 | /* Copy IV/EIV data */ |
215 | memcpy(skb->data + transfer, rxdesc->iv, iv_len); | |
216 | transfer += iv_len; | |
2bb057d0 ID |
217 | |
218 | /* Move payload */ | |
219 | if (align) { | |
220 | memmove(skb->data + transfer, | |
221 | skb->data + transfer + align, | |
222 | payload_len); | |
223 | } | |
224 | ||
225 | /* | |
226 | * NOTE: Always count the payload as transfered, | |
227 | * even when alignment was set to zero. This is required | |
228 | * for determining the correct offset for the ICV data. | |
229 | */ | |
230 | transfer += payload_len; | |
231 | ||
1ce9cdac ID |
232 | /* |
233 | * Copy ICV data | |
234 | * AES appends 8 bytes, we can't fill the upper | |
235 | * 4 bytes, but mac80211 doesn't care about what | |
236 | * we provide here anyway and strips it immediately. | |
237 | */ | |
238 | memcpy(skb->data + transfer, &rxdesc->icv, 4); | |
239 | transfer += icv_len; | |
2bb057d0 ID |
240 | |
241 | /* IV/EIV/ICV has been inserted into frame */ | |
242 | rxdesc->size = transfer; | |
243 | rxdesc->flags &= ~RX_FLAG_IV_STRIPPED; | |
244 | } |