blob: a278d5e862fe38b9210b3d814604883f450ef339 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C)2002 USAGI/WIDE Project
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Authors
19 *
20 * Mitsuru KANDA @USAGI : IPv6 Support
21 * Kazunori MIYAZAWA @USAGI :
22 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
23 *
24 * This file is derived from net/ipv4/esp.c
25 */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
28#include <net/ip.h>
29#include <net/xfrm.h>
30#include <net/esp.h>
31#include <asm/scatterlist.h>
32#include <linux/crypto.h>
Herbert Xua02a6422005-10-10 21:11:08 -070033#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/pfkeyv2.h>
35#include <linux/random.h>
36#include <net/icmp.h>
37#include <net/ipv6.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020038#include <net/protocol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/icmpv6.h>
40
41static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
42{
43 int err;
44 int hdr_len;
45 struct ipv6hdr *top_iph;
46 struct ipv6_esp_hdr *esph;
47 struct crypto_tfm *tfm;
48 struct esp_data *esp;
49 struct sk_buff *trailer;
50 int blksize;
51 int clen;
52 int alen;
53 int nfrags;
54
55 esp = x->data;
56 hdr_len = skb->h.raw - skb->data +
57 sizeof(*esph) + esp->conf.ivlen;
58
59 /* Strip IP+ESP header. */
60 __skb_pull(skb, hdr_len);
61
62 /* Now skb is pure payload to encrypt */
63 err = -ENOMEM;
64
65 /* Round to block size */
66 clen = skb->len;
67
68 alen = esp->auth.icv_trunc_len;
69 tfm = esp->conf.tfm;
Herbert Xua02a6422005-10-10 21:11:08 -070070 blksize = ALIGN(crypto_tfm_alg_blocksize(tfm), 4);
71 clen = ALIGN(clen + 2, blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (esp->conf.padlen)
Herbert Xua02a6422005-10-10 21:11:08 -070073 clen = ALIGN(clen, esp->conf.padlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 if ((nfrags = skb_cow_data(skb, clen-skb->len+alen, &trailer)) < 0) {
76 goto error;
77 }
78
79 /* Fill padding... */
80 do {
81 int i;
82 for (i=0; i<clen-skb->len - 2; i++)
83 *(u8*)(trailer->tail + i) = i+1;
84 } while (0);
85 *(u8*)(trailer->tail + clen-skb->len - 2) = (clen - skb->len)-2;
86 pskb_put(skb, trailer, clen - skb->len);
87
88 top_iph = (struct ipv6hdr *)__skb_push(skb, hdr_len);
89 esph = (struct ipv6_esp_hdr *)skb->h.raw;
90 top_iph->payload_len = htons(skb->len + alen - sizeof(*top_iph));
91 *(u8*)(trailer->tail - 1) = *skb->nh.raw;
92 *skb->nh.raw = IPPROTO_ESP;
93
94 esph->spi = x->id.spi;
95 esph->seq_no = htonl(++x->replay.oseq);
Jamal Hadi Salim9500e8a2006-03-20 19:15:29 -080096 xfrm_aevent_doreplay(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 if (esp->conf.ivlen)
99 crypto_cipher_set_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
100
101 do {
102 struct scatterlist *sg = &esp->sgbuf[0];
103
104 if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
105 sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
106 if (!sg)
107 goto error;
108 }
109 skb_to_sgvec(skb, sg, esph->enc_data+esp->conf.ivlen-skb->data, clen);
110 crypto_cipher_encrypt(tfm, sg, sg, clen);
111 if (unlikely(sg != &esp->sgbuf[0]))
112 kfree(sg);
113 } while (0);
114
115 if (esp->conf.ivlen) {
116 memcpy(esph->enc_data, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
117 crypto_cipher_get_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
118 }
119
120 if (esp->auth.icv_full_len) {
121 esp->auth.icv(esp, skb, (u8*)esph-skb->data,
122 sizeof(struct ipv6_esp_hdr) + esp->conf.ivlen+clen, trailer->tail);
123 pskb_put(skb, trailer, alen);
124 }
125
126 err = 0;
127
128error:
129 return err;
130}
131
Herbert Xue6956332006-04-01 00:52:46 -0800132static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 struct ipv6hdr *iph;
135 struct ipv6_esp_hdr *esph;
136 struct esp_data *esp = x->data;
137 struct sk_buff *trailer;
Herbert Xud4875b02005-10-10 21:11:34 -0700138 int blksize = ALIGN(crypto_tfm_alg_blocksize(esp->conf.tfm), 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 int alen = esp->auth.icv_trunc_len;
140 int elen = skb->len - sizeof(struct ipv6_esp_hdr) - esp->conf.ivlen - alen;
141
142 int hdr_len = skb->h.raw - skb->nh.raw;
143 int nfrags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 int ret = 0;
145
146 if (!pskb_may_pull(skb, sizeof(struct ipv6_esp_hdr))) {
147 ret = -EINVAL;
Herbert Xu31a4ab92006-05-27 23:06:13 -0700148 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150
151 if (elen <= 0 || (elen & (blksize-1))) {
152 ret = -EINVAL;
Herbert Xu31a4ab92006-05-27 23:06:13 -0700153 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 /* If integrity check is required, do this. */
157 if (esp->auth.icv_full_len) {
158 u8 sum[esp->auth.icv_full_len];
159 u8 sum1[alen];
160
161 esp->auth.icv(esp, skb, 0, skb->len-alen, sum);
162
163 if (skb_copy_bits(skb, skb->len-alen, sum1, alen))
164 BUG();
165
166 if (unlikely(memcmp(sum, sum1, alen))) {
167 x->stats.integrity_failed++;
168 ret = -EINVAL;
169 goto out;
170 }
171 }
172
173 if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0) {
174 ret = -EINVAL;
175 goto out;
176 }
177
178 skb->ip_summed = CHECKSUM_NONE;
179
180 esph = (struct ipv6_esp_hdr*)skb->data;
181 iph = skb->nh.ipv6h;
182
183 /* Get ivec. This can be wrong, check against another impls. */
184 if (esp->conf.ivlen)
185 crypto_cipher_set_iv(esp->conf.tfm, esph->enc_data, crypto_tfm_alg_ivsize(esp->conf.tfm));
186
187 {
188 u8 nexthdr[2];
189 struct scatterlist *sg = &esp->sgbuf[0];
190 u8 padlen;
191
192 if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
193 sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
194 if (!sg) {
195 ret = -ENOMEM;
196 goto out;
197 }
198 }
199 skb_to_sgvec(skb, sg, sizeof(struct ipv6_esp_hdr) + esp->conf.ivlen, elen);
200 crypto_cipher_decrypt(esp->conf.tfm, sg, sg, elen);
201 if (unlikely(sg != &esp->sgbuf[0]))
202 kfree(sg);
203
204 if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
205 BUG();
206
207 padlen = nexthdr[0];
208 if (padlen+2 >= elen) {
Patrick McHardy64ce2072005-08-09 20:50:53 -0700209 LIMIT_NETDEBUG(KERN_WARNING "ipsec esp packet is garbage padlen=%d, elen=%d\n", padlen+2, elen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 ret = -EINVAL;
211 goto out;
212 }
213 /* ... check padding bits here. Silly. :-) */
214
215 pskb_trim(skb, skb->len - alen - padlen - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 ret = nexthdr[1];
217 }
218
Herbert Xu31a4ab92006-05-27 23:06:13 -0700219 skb->h.raw = __skb_pull(skb, sizeof(*esph) + esp->conf.ivlen) - hdr_len;
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return ret;
223}
224
225static u32 esp6_get_max_size(struct xfrm_state *x, int mtu)
226{
227 struct esp_data *esp = x->data;
Herbert Xud4875b02005-10-10 21:11:34 -0700228 u32 blksize = ALIGN(crypto_tfm_alg_blocksize(esp->conf.tfm), 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 if (x->props.mode) {
Herbert Xua02a6422005-10-10 21:11:08 -0700231 mtu = ALIGN(mtu + 2, blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 } else {
233 /* The worst case. */
Herbert Xud4875b02005-10-10 21:11:34 -0700234 u32 padsize = ((blksize - 1) & 7) + 1;
235 mtu = ALIGN(mtu + 2, padsize) + blksize - padsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237 if (esp->conf.padlen)
Herbert Xua02a6422005-10-10 21:11:08 -0700238 mtu = ALIGN(mtu, esp->conf.padlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Kazunori MIYAZAWA73d4f842005-12-08 23:11:42 -0800240 return mtu + x->props.header_len + esp->auth.icv_trunc_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243static void esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
244 int type, int code, int offset, __u32 info)
245{
246 struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
247 struct ipv6_esp_hdr *esph = (struct ipv6_esp_hdr*)(skb->data+offset);
248 struct xfrm_state *x;
249
250 if (type != ICMPV6_DEST_UNREACH &&
251 type != ICMPV6_PKT_TOOBIG)
252 return;
253
254 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET6);
255 if (!x)
256 return;
Joe Perches46b86a22006-01-13 14:29:07 -0800257 printk(KERN_DEBUG "pmtu discovery on SA ESP/%08x/" NIP6_FMT "\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 ntohl(esph->spi), NIP6(iph->daddr));
259 xfrm_state_put(x);
260}
261
262static void esp6_destroy(struct xfrm_state *x)
263{
264 struct esp_data *esp = x->data;
265
266 if (!esp)
267 return;
268
Jesper Juhl573dbd92005-09-01 17:44:29 -0700269 crypto_free_tfm(esp->conf.tfm);
270 esp->conf.tfm = NULL;
271 kfree(esp->conf.ivec);
272 esp->conf.ivec = NULL;
273 crypto_free_tfm(esp->auth.tfm);
274 esp->auth.tfm = NULL;
275 kfree(esp->auth.work_icv);
276 esp->auth.work_icv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 kfree(esp);
278}
279
Herbert Xu72cb6962005-06-20 13:18:08 -0700280static int esp6_init_state(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 struct esp_data *esp = NULL;
283
284 /* null auth and encryption can have zero length keys */
285 if (x->aalg) {
286 if (x->aalg->alg_key_len > 512)
287 goto error;
288 }
289 if (x->ealg == NULL)
290 goto error;
291
292 if (x->encap)
293 goto error;
294
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800295 esp = kzalloc(sizeof(*esp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (esp == NULL)
297 return -ENOMEM;
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (x->aalg) {
300 struct xfrm_algo_desc *aalg_desc;
301
302 esp->auth.key = x->aalg->alg_key;
303 esp->auth.key_len = (x->aalg->alg_key_len+7)/8;
304 esp->auth.tfm = crypto_alloc_tfm(x->aalg->alg_name, 0);
305 if (esp->auth.tfm == NULL)
306 goto error;
307 esp->auth.icv = esp_hmac_digest;
308
309 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
310 BUG_ON(!aalg_desc);
311
312 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
313 crypto_tfm_alg_digestsize(esp->auth.tfm)) {
314 printk(KERN_INFO "ESP: %s digestsize %u != %hu\n",
315 x->aalg->alg_name,
316 crypto_tfm_alg_digestsize(esp->auth.tfm),
317 aalg_desc->uinfo.auth.icv_fullbits/8);
318 goto error;
319 }
320
321 esp->auth.icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
322 esp->auth.icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
323
324 esp->auth.work_icv = kmalloc(esp->auth.icv_full_len, GFP_KERNEL);
325 if (!esp->auth.work_icv)
326 goto error;
327 }
328 esp->conf.key = x->ealg->alg_key;
329 esp->conf.key_len = (x->ealg->alg_key_len+7)/8;
330 if (x->props.ealgo == SADB_EALG_NULL)
331 esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_ECB);
332 else
333 esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_CBC);
334 if (esp->conf.tfm == NULL)
335 goto error;
336 esp->conf.ivlen = crypto_tfm_alg_ivsize(esp->conf.tfm);
337 esp->conf.padlen = 0;
338 if (esp->conf.ivlen) {
339 esp->conf.ivec = kmalloc(esp->conf.ivlen, GFP_KERNEL);
340 if (unlikely(esp->conf.ivec == NULL))
341 goto error;
342 get_random_bytes(esp->conf.ivec, esp->conf.ivlen);
343 }
344 if (crypto_cipher_setkey(esp->conf.tfm, esp->conf.key, esp->conf.key_len))
345 goto error;
346 x->props.header_len = sizeof(struct ipv6_esp_hdr) + esp->conf.ivlen;
347 if (x->props.mode)
348 x->props.header_len += sizeof(struct ipv6hdr);
349 x->data = esp;
350 return 0;
351
352error:
353 x->data = esp;
354 esp6_destroy(x);
355 x->data = NULL;
356 return -EINVAL;
357}
358
359static struct xfrm_type esp6_type =
360{
361 .description = "ESP6",
362 .owner = THIS_MODULE,
363 .proto = IPPROTO_ESP,
364 .init_state = esp6_init_state,
365 .destructor = esp6_destroy,
366 .get_max_size = esp6_get_max_size,
367 .input = esp6_input,
368 .output = esp6_output
369};
370
371static struct inet6_protocol esp6_protocol = {
372 .handler = xfrm6_rcv,
373 .err_handler = esp6_err,
374 .flags = INET6_PROTO_NOPOLICY,
375};
376
377static int __init esp6_init(void)
378{
379 if (xfrm_register_type(&esp6_type, AF_INET6) < 0) {
380 printk(KERN_INFO "ipv6 esp init: can't add xfrm type\n");
381 return -EAGAIN;
382 }
383 if (inet6_add_protocol(&esp6_protocol, IPPROTO_ESP) < 0) {
384 printk(KERN_INFO "ipv6 esp init: can't add protocol\n");
385 xfrm_unregister_type(&esp6_type, AF_INET6);
386 return -EAGAIN;
387 }
388
389 return 0;
390}
391
392static void __exit esp6_fini(void)
393{
394 if (inet6_del_protocol(&esp6_protocol, IPPROTO_ESP) < 0)
395 printk(KERN_INFO "ipv6 esp close: can't remove protocol\n");
396 if (xfrm_unregister_type(&esp6_type, AF_INET6) < 0)
397 printk(KERN_INFO "ipv6 esp close: can't remove xfrm type\n");
398}
399
400module_init(esp6_init);
401module_exit(esp6_fini);
402
403MODULE_LICENSE("GPL");