1/*
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
27#include <linux/err.h>
28#include <linux/module.h>
29#include <net/ip.h>
30#include <net/xfrm.h>
31#include <net/esp.h>
32#include <asm/scatterlist.h>
33#include <linux/crypto.h>
34#include <linux/kernel.h>
35#include <linux/pfkeyv2.h>
36#include <linux/random.h>
37#include <net/icmp.h>
38#include <net/ipv6.h>
39#include <net/protocol.h>
40#include <linux/icmpv6.h>
41
42static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
43{
44	int err;
45	struct ipv6hdr *top_iph;
46	struct ipv6_esp_hdr *esph;
47	struct crypto_blkcipher *tfm;
48	struct blkcipher_desc desc;
49	struct sk_buff *trailer;
50	int blksize;
51	int clen;
52	int alen;
53	int nfrags;
54	u8 *tail;
55	struct esp_data *esp = x->data;
56	int hdr_len = (skb_transport_offset(skb) +
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;
70	desc.tfm = tfm;
71	desc.flags = 0;
72	blksize = ALIGN(crypto_blkcipher_blocksize(tfm), 4);
73	clen = ALIGN(clen + 2, blksize);
74	if (esp->conf.padlen)
75		clen = ALIGN(clen, esp->conf.padlen);
76
77	if ((nfrags = skb_cow_data(skb, clen-skb->len+alen, &trailer)) < 0) {
78		goto error;
79	}
80
81	/* Fill padding... */
82	tail = skb_tail_pointer(trailer);
83	do {
84		int i;
85		for (i=0; i<clen-skb->len - 2; i++)
86			tail[i] = i + 1;
87	} while (0);
88	tail[clen-skb->len - 2] = (clen - skb->len) - 2;
89	pskb_put(skb, trailer, clen - skb->len);
90
91	top_iph = (struct ipv6hdr *)__skb_push(skb, hdr_len);
92	esph = (struct ipv6_esp_hdr *)skb_transport_header(skb);
93	top_iph->payload_len = htons(skb->len + alen - sizeof(*top_iph));
94	*(skb_tail_pointer(trailer) - 1) = *skb_network_header(skb);
95	*skb_network_header(skb) = IPPROTO_ESP;
96
97	esph->spi = x->id.spi;
98	esph->seq_no = htonl(++x->replay.oseq);
99	xfrm_aevent_doreplay(x);
100
101	if (esp->conf.ivlen) {
102		if (unlikely(!esp->conf.ivinitted)) {
103			get_random_bytes(esp->conf.ivec, esp->conf.ivlen);
104			esp->conf.ivinitted = 1;
105		}
106		crypto_blkcipher_set_iv(tfm, esp->conf.ivec, esp->conf.ivlen);
107	}
108
109	do {
110		struct scatterlist *sg = &esp->sgbuf[0];
111
112		if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
113			sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
114			if (!sg)
115				goto error;
116		}
117		skb_to_sgvec(skb, sg, esph->enc_data+esp->conf.ivlen-skb->data, clen);
118		err = crypto_blkcipher_encrypt(&desc, sg, sg, clen);
119		if (unlikely(sg != &esp->sgbuf[0]))
120			kfree(sg);
121	} while (0);
122
123	if (unlikely(err))
124		goto error;
125
126	if (esp->conf.ivlen) {
127		memcpy(esph->enc_data, esp->conf.ivec, esp->conf.ivlen);
128		crypto_blkcipher_get_iv(tfm, esp->conf.ivec, esp->conf.ivlen);
129	}
130
131	if (esp->auth.icv_full_len) {
132		err = esp_mac_digest(esp, skb, (u8 *)esph - skb->data,
133				     sizeof(*esph) + esp->conf.ivlen + clen);
134		memcpy(pskb_put(skb, trailer, alen), esp->auth.work_icv, alen);
135	}
136
137error:
138	return err;
139}
140
141static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
142{
143	struct ipv6hdr *iph;
144	struct ipv6_esp_hdr *esph;
145	struct esp_data *esp = x->data;
146	struct crypto_blkcipher *tfm = esp->conf.tfm;
147	struct blkcipher_desc desc = { .tfm = tfm };
148	struct sk_buff *trailer;
149	int blksize = ALIGN(crypto_blkcipher_blocksize(tfm), 4);
150	int alen = esp->auth.icv_trunc_len;
151	int elen = skb->len - sizeof(struct ipv6_esp_hdr) - esp->conf.ivlen - alen;
152	int hdr_len = skb_network_header_len(skb);
153	int nfrags;
154	int ret = 0;
155
156	if (!pskb_may_pull(skb, sizeof(struct ipv6_esp_hdr))) {
157		ret = -EINVAL;
158		goto out;
159	}
160
161	if (elen <= 0 || (elen & (blksize-1))) {
162		ret = -EINVAL;
163		goto out;
164	}
165
166	/* If integrity check is required, do this. */
167	if (esp->auth.icv_full_len) {
168		u8 sum[alen];
169
170		ret = esp_mac_digest(esp, skb, 0, skb->len - alen);
171		if (ret)
172			goto out;
173
174		if (skb_copy_bits(skb, skb->len - alen, sum, alen))
175			BUG();
176
177		if (unlikely(memcmp(esp->auth.work_icv, sum, alen))) {
178			x->stats.integrity_failed++;
179			ret = -EINVAL;
180			goto out;
181		}
182	}
183
184	if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0) {
185		ret = -EINVAL;
186		goto out;
187	}
188
189	skb->ip_summed = CHECKSUM_NONE;
190
191	esph = (struct ipv6_esp_hdr*)skb->data;
192	iph = ipv6_hdr(skb);
193
194	/* Get ivec. This can be wrong, check against another impls. */
195	if (esp->conf.ivlen)
196		crypto_blkcipher_set_iv(tfm, esph->enc_data, esp->conf.ivlen);
197
198	{
199		u8 nexthdr[2];
200		struct scatterlist *sg = &esp->sgbuf[0];
201		u8 padlen;
202
203		if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
204			sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
205			if (!sg) {
206				ret = -ENOMEM;
207				goto out;
208			}
209		}
210		skb_to_sgvec(skb, sg, sizeof(struct ipv6_esp_hdr) + esp->conf.ivlen, elen);
211		ret = crypto_blkcipher_decrypt(&desc, sg, sg, elen);
212		if (unlikely(sg != &esp->sgbuf[0]))
213			kfree(sg);
214		if (unlikely(ret))
215			goto out;
216
217		if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
218			BUG();
219
220		padlen = nexthdr[0];
221		if (padlen+2 >= elen) {
222			LIMIT_NETDEBUG(KERN_WARNING "ipsec esp packet is garbage padlen=%d, elen=%d\n", padlen+2, elen);
223			ret = -EINVAL;
224			goto out;
225		}
226		/* ... check padding bits here. Silly. :-) */
227
228		pskb_trim(skb, skb->len - alen - padlen - 2);
229		ret = nexthdr[1];
230	}
231
232	__skb_pull(skb, sizeof(*esph) + esp->conf.ivlen);
233	skb_set_transport_header(skb, -hdr_len);
234out:
235	return ret;
236}
237
238static u32 esp6_get_mtu(struct xfrm_state *x, int mtu)
239{
240	struct esp_data *esp = x->data;
241	u32 blksize = ALIGN(crypto_blkcipher_blocksize(esp->conf.tfm), 4);
242	u32 align = max_t(u32, blksize, esp->conf.padlen);
243	u32 rem;
244
245	mtu -= x->props.header_len + esp->auth.icv_trunc_len;
246	rem = mtu & (align - 1);
247	mtu &= ~(align - 1);
248
249	if (x->props.mode != XFRM_MODE_TUNNEL) {
250		u32 padsize = ((blksize - 1) & 7) + 1;
251		mtu -= blksize - padsize;
252		mtu += min_t(u32, blksize - padsize, rem);
253	}
254
255	return mtu - 2;
256}
257
258static void esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
259		     int type, int code, int offset, __be32 info)
260{
261	struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
262	struct ipv6_esp_hdr *esph = (struct ipv6_esp_hdr*)(skb->data+offset);
263	struct xfrm_state *x;
264
265	if (type != ICMPV6_DEST_UNREACH &&
266	    type != ICMPV6_PKT_TOOBIG)
267		return;
268
269	x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET6);
270	if (!x)
271		return;
272	printk(KERN_DEBUG "pmtu discovery on SA ESP/%08x/" NIP6_FMT "\n",
273			ntohl(esph->spi), NIP6(iph->daddr));
274	xfrm_state_put(x);
275}
276
277static void esp6_destroy(struct xfrm_state *x)
278{
279	struct esp_data *esp = x->data;
280
281	if (!esp)
282		return;
283
284	crypto_free_blkcipher(esp->conf.tfm);
285	esp->conf.tfm = NULL;
286	kfree(esp->conf.ivec);
287	esp->conf.ivec = NULL;
288	crypto_free_hash(esp->auth.tfm);
289	esp->auth.tfm = NULL;
290	kfree(esp->auth.work_icv);
291	esp->auth.work_icv = NULL;
292	kfree(esp);
293}
294
295static int esp6_init_state(struct xfrm_state *x)
296{
297	struct esp_data *esp = NULL;
298	struct crypto_blkcipher *tfm;
299
300	/* null auth and encryption can have zero length keys */
301	if (x->aalg) {
302		if (x->aalg->alg_key_len > 512)
303			goto error;
304	}
305	if (x->ealg == NULL)
306		goto error;
307
308	if (x->encap)
309		goto error;
310
311	esp = kzalloc(sizeof(*esp), GFP_KERNEL);
312	if (esp == NULL)
313		return -ENOMEM;
314
315	if (x->aalg) {
316		struct xfrm_algo_desc *aalg_desc;
317		struct crypto_hash *hash;
318
319		esp->auth.key = x->aalg->alg_key;
320		esp->auth.key_len = (x->aalg->alg_key_len+7)/8;
321		hash = crypto_alloc_hash(x->aalg->alg_name, 0,
322					 CRYPTO_ALG_ASYNC);
323		if (IS_ERR(hash))
324			goto error;
325
326		esp->auth.tfm = hash;
327		if (crypto_hash_setkey(hash, esp->auth.key, esp->auth.key_len))
328			goto error;
329
330		aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
331		BUG_ON(!aalg_desc);
332
333		if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
334		    crypto_hash_digestsize(hash)) {
335			NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n",
336				 x->aalg->alg_name,
337				 crypto_hash_digestsize(hash),
338				 aalg_desc->uinfo.auth.icv_fullbits/8);
339			goto error;
340		}
341
342		esp->auth.icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
343		esp->auth.icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
344
345		esp->auth.work_icv = kmalloc(esp->auth.icv_full_len, GFP_KERNEL);
346		if (!esp->auth.work_icv)
347			goto error;
348	}
349	esp->conf.key = x->ealg->alg_key;
350	esp->conf.key_len = (x->ealg->alg_key_len+7)/8;
351	tfm = crypto_alloc_blkcipher(x->ealg->alg_name, 0, CRYPTO_ALG_ASYNC);
352	if (IS_ERR(tfm))
353		goto error;
354	esp->conf.tfm = tfm;
355	esp->conf.ivlen = crypto_blkcipher_ivsize(tfm);
356	esp->conf.padlen = 0;
357	if (esp->conf.ivlen) {
358		esp->conf.ivec = kmalloc(esp->conf.ivlen, GFP_KERNEL);
359		if (unlikely(esp->conf.ivec == NULL))
360			goto error;
361		esp->conf.ivinitted = 0;
362	}
363	if (crypto_blkcipher_setkey(tfm, esp->conf.key, esp->conf.key_len))
364		goto error;
365	x->props.header_len = sizeof(struct ipv6_esp_hdr) + esp->conf.ivlen;
366	if (x->props.mode == XFRM_MODE_TUNNEL)
367		x->props.header_len += sizeof(struct ipv6hdr);
368	x->data = esp;
369	return 0;
370
371error:
372	x->data = esp;
373	esp6_destroy(x);
374	x->data = NULL;
375	return -EINVAL;
376}
377
378static struct xfrm_type esp6_type =
379{
380	.description	= "ESP6",
381	.owner	     	= THIS_MODULE,
382	.proto	     	= IPPROTO_ESP,
383	.init_state	= esp6_init_state,
384	.destructor	= esp6_destroy,
385	.get_mtu	= esp6_get_mtu,
386	.input		= esp6_input,
387	.output		= esp6_output,
388	.hdr_offset	= xfrm6_find_1stfragopt,
389};
390
391static struct inet6_protocol esp6_protocol = {
392	.handler 	=	xfrm6_rcv,
393	.err_handler	=	esp6_err,
394	.flags		=	INET6_PROTO_NOPOLICY,
395};
396
397static int __init esp6_init(void)
398{
399	if (xfrm_register_type(&esp6_type, AF_INET6) < 0) {
400		printk(KERN_INFO "ipv6 esp init: can't add xfrm type\n");
401		return -EAGAIN;
402	}
403	if (inet6_add_protocol(&esp6_protocol, IPPROTO_ESP) < 0) {
404		printk(KERN_INFO "ipv6 esp init: can't add protocol\n");
405		xfrm_unregister_type(&esp6_type, AF_INET6);
406		return -EAGAIN;
407	}
408
409	return 0;
410}
411
412static void __exit esp6_fini(void)
413{
414	if (inet6_del_protocol(&esp6_protocol, IPPROTO_ESP) < 0)
415		printk(KERN_INFO "ipv6 esp close: can't remove protocol\n");
416	if (xfrm_unregister_type(&esp6_type, AF_INET6) < 0)
417		printk(KERN_INFO "ipv6 esp close: can't remove xfrm type\n");
418}
419
420module_init(esp6_init);
421module_exit(esp6_fini);
422
423MODULE_LICENSE("GPL");
424