blob: 1b5c9825743b6dd45731782a254d4e3595a5cb2b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C)2002 USAGI/WIDE Project
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * 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 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090020 * Mitsuru KANDA @USAGI : IPv6 Support
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * Kazunori MIYAZAWA @USAGI :
22 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This file is derived from net/ipv4/esp.c
25 */
26
Herbert Xu38320c72008-01-28 19:35:05 -080027#include <crypto/aead.h>
28#include <crypto/authenc.h>
Herbert Xu6b7326c2006-07-30 15:41:01 +100029#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <net/ip.h>
32#include <net/xfrm.h>
33#include <net/esp.h>
Adrian Bunk72998d82007-10-26 22:53:58 -070034#include <linux/scatterlist.h>
Herbert Xua02a6422005-10-10 21:11:08 -070035#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/pfkeyv2.h>
37#include <linux/random.h>
Herbert Xu38320c72008-01-28 19:35:05 -080038#include <linux/slab.h>
Herbert Xub7c65382007-10-09 13:33:35 -070039#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <net/icmp.h>
41#include <net/ipv6.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020042#include <net/protocol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/icmpv6.h>
44
Herbert Xu38320c72008-01-28 19:35:05 -080045struct esp_skb_cb {
46 struct xfrm_skb_cb xfrm;
47 void *tmp;
48};
49
50#define ESP_SKB_CB(__skb) ((struct esp_skb_cb *)&((__skb)->cb[0]))
51
Martin Willi040253c2010-12-08 04:37:51 +000052static u32 esp6_get_mtu(struct xfrm_state *x, int mtu);
53
Herbert Xu38320c72008-01-28 19:35:05 -080054/*
55 * Allocate an AEAD request structure with extra space for SG and IV.
56 *
57 * For alignment considerations the IV is placed at the front, followed
58 * by the request and finally the SG list.
59 *
60 * TODO: Use spare space in skb for this where possible.
61 */
62static void *esp_alloc_tmp(struct crypto_aead *aead, int nfrags)
63{
64 unsigned int len;
65
66 len = crypto_aead_ivsize(aead);
67 if (len) {
68 len += crypto_aead_alignmask(aead) &
69 ~(crypto_tfm_ctx_alignment() - 1);
70 len = ALIGN(len, crypto_tfm_ctx_alignment());
71 }
72
73 len += sizeof(struct aead_givcrypt_request) + crypto_aead_reqsize(aead);
74 len = ALIGN(len, __alignof__(struct scatterlist));
75
76 len += sizeof(struct scatterlist) * nfrags;
77
78 return kmalloc(len, GFP_ATOMIC);
79}
80
81static inline u8 *esp_tmp_iv(struct crypto_aead *aead, void *tmp)
82{
83 return crypto_aead_ivsize(aead) ?
84 PTR_ALIGN((u8 *)tmp, crypto_aead_alignmask(aead) + 1) : tmp;
85}
86
87static inline struct aead_givcrypt_request *esp_tmp_givreq(
88 struct crypto_aead *aead, u8 *iv)
89{
90 struct aead_givcrypt_request *req;
91
92 req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
93 crypto_tfm_ctx_alignment());
94 aead_givcrypt_set_tfm(req, aead);
95 return req;
96}
97
98static inline struct aead_request *esp_tmp_req(struct crypto_aead *aead, u8 *iv)
99{
100 struct aead_request *req;
101
102 req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
103 crypto_tfm_ctx_alignment());
104 aead_request_set_tfm(req, aead);
105 return req;
106}
107
108static inline struct scatterlist *esp_req_sg(struct crypto_aead *aead,
109 struct aead_request *req)
110{
111 return (void *)ALIGN((unsigned long)(req + 1) +
112 crypto_aead_reqsize(aead),
113 __alignof__(struct scatterlist));
114}
115
116static inline struct scatterlist *esp_givreq_sg(
117 struct crypto_aead *aead, struct aead_givcrypt_request *req)
118{
119 return (void *)ALIGN((unsigned long)(req + 1) +
120 crypto_aead_reqsize(aead),
121 __alignof__(struct scatterlist));
122}
123
124static void esp_output_done(struct crypto_async_request *base, int err)
125{
126 struct sk_buff *skb = base->data;
127
128 kfree(ESP_SKB_CB(skb)->tmp);
129 xfrm_output_resume(skb, err);
130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
133{
134 int err;
Herbert Xu87bdc482007-10-10 15:45:25 -0700135 struct ip_esp_hdr *esph;
Herbert Xu38320c72008-01-28 19:35:05 -0800136 struct crypto_aead *aead;
137 struct aead_givcrypt_request *req;
138 struct scatterlist *sg;
139 struct scatterlist *asg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 struct sk_buff *trailer;
Herbert Xu38320c72008-01-28 19:35:05 -0800141 void *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 int blksize;
143 int clen;
144 int alen;
Martin Willi040253c2010-12-08 04:37:51 +0000145 int plen;
146 int tfclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 int nfrags;
Herbert Xu38320c72008-01-28 19:35:05 -0800148 u8 *iv;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700149 u8 *tail;
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700150 struct esp_data *esp = x->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Herbert Xu7b277b12007-10-10 15:44:06 -0700152 /* skb is pure payload to encrypt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 err = -ENOMEM;
154
Herbert Xu38320c72008-01-28 19:35:05 -0800155 aead = esp->aead;
156 alen = crypto_aead_authsize(aead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Martin Willi040253c2010-12-08 04:37:51 +0000158 tfclen = 0;
159 if (x->tfcpad) {
160 struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
161 u32 padto;
162
163 padto = min(x->tfcpad, esp6_get_mtu(x, dst->child_mtu_cached));
164 if (skb->len < padto)
165 tfclen = padto - skb->len;
166 }
Herbert Xu38320c72008-01-28 19:35:05 -0800167 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
Martin Willi040253c2010-12-08 04:37:51 +0000168 clen = ALIGN(skb->len + 2 + tfclen, blksize);
Herbert Xu38320c72008-01-28 19:35:05 -0800169 if (esp->padlen)
170 clen = ALIGN(clen, esp->padlen);
Martin Willi040253c2010-12-08 04:37:51 +0000171 plen = clen - skb->len - tfclen;
Herbert Xu38320c72008-01-28 19:35:05 -0800172
Martin Willi040253c2010-12-08 04:37:51 +0000173 err = skb_cow_data(skb, tfclen + plen + alen, &trailer);
174 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 goto error;
Herbert Xu38320c72008-01-28 19:35:05 -0800176 nfrags = err;
177
178 tmp = esp_alloc_tmp(aead, nfrags + 1);
179 if (!tmp)
180 goto error;
181
182 iv = esp_tmp_iv(aead, tmp);
183 req = esp_tmp_givreq(aead, iv);
184 asg = esp_givreq_sg(aead, req);
185 sg = asg + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 /* Fill padding... */
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700188 tail = skb_tail_pointer(trailer);
Martin Willi040253c2010-12-08 04:37:51 +0000189 if (tfclen) {
190 memset(tail, 0, tfclen);
191 tail += tfclen;
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 do {
194 int i;
Martin Willi040253c2010-12-08 04:37:51 +0000195 for (i = 0; i < plen - 2; i++)
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700196 tail[i] = i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 } while (0);
Martin Willi040253c2010-12-08 04:37:51 +0000198 tail[plen - 2] = plen - 2;
199 tail[plen - 1] = *skb_mac_header(skb);
Herbert Xu38320c72008-01-28 19:35:05 -0800200 pskb_put(skb, trailer, clen - skb->len + alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Herbert Xu7b277b12007-10-10 15:44:06 -0700202 skb_push(skb, -skb_network_offset(skb));
Herbert Xu87bdc482007-10-10 15:45:25 -0700203 esph = ip_esp_hdr(skb);
Herbert Xu007f0212007-10-09 13:25:59 -0700204 *skb_mac_header(skb) = IPPROTO_ESP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 esph->spi = x->id.spi;
Herbert Xub318e0e2008-02-12 22:50:35 -0800207 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Herbert Xu38320c72008-01-28 19:35:05 -0800209 sg_init_table(sg, nfrags);
210 skb_to_sgvec(skb, sg,
211 esph->enc_data + crypto_aead_ivsize(aead) - skb->data,
212 clen + alen);
213 sg_init_one(asg, esph, sizeof(*esph));
Herbert Xub7c65382007-10-09 13:33:35 -0700214
Herbert Xu38320c72008-01-28 19:35:05 -0800215 aead_givcrypt_set_callback(req, 0, esp_output_done, skb);
216 aead_givcrypt_set_crypt(req, sg, sg, clen, iv);
217 aead_givcrypt_set_assoc(req, asg, sizeof(*esph));
Herbert Xub318e0e2008-02-12 22:50:35 -0800218 aead_givcrypt_set_giv(req, esph->enc_data,
219 XFRM_SKB_CB(skb)->seq.output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Herbert Xu38320c72008-01-28 19:35:05 -0800221 ESP_SKB_CB(skb)->tmp = tmp;
222 err = crypto_aead_givencrypt(req);
223 if (err == -EINPROGRESS)
224 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Herbert Xu38320c72008-01-28 19:35:05 -0800226 if (err == -EBUSY)
227 err = NET_XMIT_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Herbert Xu38320c72008-01-28 19:35:05 -0800229 kfree(tmp);
Herbert Xub7c65382007-10-09 13:33:35 -0700230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231error:
232 return err;
233}
234
Herbert Xu38320c72008-01-28 19:35:05 -0800235static int esp_input_done2(struct sk_buff *skb, int err)
236{
237 struct xfrm_state *x = xfrm_input_state(skb);
238 struct esp_data *esp = x->data;
239 struct crypto_aead *aead = esp->aead;
240 int alen = crypto_aead_authsize(aead);
241 int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
242 int elen = skb->len - hlen;
243 int hdr_len = skb_network_header_len(skb);
244 int padlen;
245 u8 nexthdr[2];
246
247 kfree(ESP_SKB_CB(skb)->tmp);
248
249 if (unlikely(err))
250 goto out;
251
252 if (skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2))
253 BUG();
254
255 err = -EINVAL;
256 padlen = nexthdr[0];
257 if (padlen + 2 + alen >= elen) {
258 LIMIT_NETDEBUG(KERN_WARNING "ipsec esp packet is garbage "
259 "padlen=%d, elen=%d\n", padlen + 2, elen - alen);
260 goto out;
261 }
262
263 /* ... check padding bits here. Silly. :-) */
264
265 pskb_trim(skb, skb->len - alen - padlen - 2);
266 __skb_pull(skb, hlen);
267 skb_set_transport_header(skb, -hdr_len);
268
269 err = nexthdr[1];
270
271 /* RFC4303: Drop dummy packets without any error */
272 if (err == IPPROTO_NONE)
273 err = -EINVAL;
274
275out:
276 return err;
277}
278
279static void esp_input_done(struct crypto_async_request *base, int err)
280{
281 struct sk_buff *skb = base->data;
282
283 xfrm_input_resume(skb, esp_input_done2(skb, err));
284}
285
Herbert Xue6956332006-04-01 00:52:46 -0800286static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Herbert Xu87bdc482007-10-10 15:45:25 -0700288 struct ip_esp_hdr *esph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 struct esp_data *esp = x->data;
Herbert Xu38320c72008-01-28 19:35:05 -0800290 struct crypto_aead *aead = esp->aead;
291 struct aead_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 struct sk_buff *trailer;
Herbert Xu38320c72008-01-28 19:35:05 -0800293 int elen = skb->len - sizeof(*esph) - crypto_aead_ivsize(aead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 int nfrags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 int ret = 0;
Herbert Xu38320c72008-01-28 19:35:05 -0800296 void *tmp;
297 u8 *iv;
298 struct scatterlist *sg;
299 struct scatterlist *asg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Thomas Graf920fc942008-03-27 16:08:03 -0700301 if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 ret = -EINVAL;
Herbert Xu31a4ab92006-05-27 23:06:13 -0700303 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305
Herbert Xu38320c72008-01-28 19:35:05 -0800306 if (elen <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 ret = -EINVAL;
Herbert Xu31a4ab92006-05-27 23:06:13 -0700308 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0) {
312 ret = -EINVAL;
313 goto out;
314 }
315
Herbert Xu38320c72008-01-28 19:35:05 -0800316 ret = -ENOMEM;
317 tmp = esp_alloc_tmp(aead, nfrags + 1);
318 if (!tmp)
Herbert Xu0ebea8e2007-11-13 21:45:58 -0800319 goto out;
320
Herbert Xu38320c72008-01-28 19:35:05 -0800321 ESP_SKB_CB(skb)->tmp = tmp;
322 iv = esp_tmp_iv(aead, tmp);
323 req = esp_tmp_req(aead, iv);
324 asg = esp_req_sg(aead, req);
325 sg = asg + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Herbert Xu38320c72008-01-28 19:35:05 -0800327 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Herbert Xu38320c72008-01-28 19:35:05 -0800329 esph = (struct ip_esp_hdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Herbert Xu38320c72008-01-28 19:35:05 -0800331 /* Get ivec. This can be wrong, check against another impls. */
332 iv = esph->enc_data;
Thomas Graf95a02cf2007-12-10 16:53:29 -0800333
Herbert Xu38320c72008-01-28 19:35:05 -0800334 sg_init_table(sg, nfrags);
335 skb_to_sgvec(skb, sg, sizeof(*esph) + crypto_aead_ivsize(aead), elen);
336 sg_init_one(asg, esph, sizeof(*esph));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Herbert Xu38320c72008-01-28 19:35:05 -0800338 aead_request_set_callback(req, 0, esp_input_done, skb);
339 aead_request_set_crypt(req, sg, sg, elen, iv);
340 aead_request_set_assoc(req, asg, sizeof(*esph));
341
342 ret = crypto_aead_decrypt(req);
343 if (ret == -EINPROGRESS)
344 goto out;
345
346 ret = esp_input_done2(skb, ret);
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return ret;
350}
351
Patrick McHardyc5c25232007-04-09 11:47:18 -0700352static u32 esp6_get_mtu(struct xfrm_state *x, int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 struct esp_data *esp = x->data;
Herbert Xu38320c72008-01-28 19:35:05 -0800355 u32 blksize = ALIGN(crypto_aead_blocksize(esp->aead), 4);
356 u32 align = max_t(u32, blksize, esp->padlen);
Patrick McHardyc5c25232007-04-09 11:47:18 -0700357 u32 rem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Herbert Xu38320c72008-01-28 19:35:05 -0800359 mtu -= x->props.header_len + crypto_aead_authsize(esp->aead);
Patrick McHardyc5c25232007-04-09 11:47:18 -0700360 rem = mtu & (align - 1);
361 mtu &= ~(align - 1);
362
363 if (x->props.mode != XFRM_MODE_TUNNEL) {
Herbert Xud4875b02005-10-10 21:11:34 -0700364 u32 padsize = ((blksize - 1) & 7) + 1;
Patrick McHardyc5c25232007-04-09 11:47:18 -0700365 mtu -= blksize - padsize;
366 mtu += min_t(u32, blksize - padsize, rem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Patrick McHardyc5c25232007-04-09 11:47:18 -0700369 return mtu - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372static void esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700373 u8 type, u8 code, int offset, __be32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Alexey Dobriyan4fb236b2008-11-25 17:59:27 -0800375 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
Herbert Xu87bdc482007-10-10 15:45:25 -0700377 struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 struct xfrm_state *x;
379
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900380 if (type != ICMPV6_DEST_UNREACH &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 type != ICMPV6_PKT_TOOBIG)
382 return;
383
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800384 x = xfrm_state_lookup(net, skb->mark, (xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (!x)
386 return;
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700387 printk(KERN_DEBUG "pmtu discovery on SA ESP/%08x/%pI6\n",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700388 ntohl(esph->spi), &iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 xfrm_state_put(x);
390}
391
392static void esp6_destroy(struct xfrm_state *x)
393{
394 struct esp_data *esp = x->data;
395
396 if (!esp)
397 return;
398
Herbert Xu38320c72008-01-28 19:35:05 -0800399 crypto_free_aead(esp->aead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 kfree(esp);
401}
402
Herbert Xu1a6509d2008-01-28 19:37:29 -0800403static int esp_init_aead(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Herbert Xu1a6509d2008-01-28 19:37:29 -0800405 struct esp_data *esp = x->data;
406 struct crypto_aead *aead;
407 int err;
408
409 aead = crypto_alloc_aead(x->aead->alg_name, 0, 0);
410 err = PTR_ERR(aead);
411 if (IS_ERR(aead))
412 goto error;
413
414 esp->aead = aead;
415
416 err = crypto_aead_setkey(aead, x->aead->alg_key,
417 (x->aead->alg_key_len + 7) / 8);
418 if (err)
419 goto error;
420
421 err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
422 if (err)
423 goto error;
424
425error:
426 return err;
427}
428
429static int esp_init_authenc(struct xfrm_state *x)
430{
431 struct esp_data *esp = x->data;
Herbert Xu38320c72008-01-28 19:35:05 -0800432 struct crypto_aead *aead;
433 struct crypto_authenc_key_param *param;
434 struct rtattr *rta;
435 char *key;
436 char *p;
437 char authenc_name[CRYPTO_MAX_ALG_NAME];
Herbert Xu38320c72008-01-28 19:35:05 -0800438 unsigned int keylen;
439 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Herbert Xu1a6509d2008-01-28 19:37:29 -0800441 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (x->ealg == NULL)
Herbert Xu1a6509d2008-01-28 19:37:29 -0800443 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Herbert Xu1a6509d2008-01-28 19:37:29 -0800445 err = -ENAMETOOLONG;
Herbert Xu38320c72008-01-28 19:35:05 -0800446 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME, "authenc(%s,%s)",
447 x->aalg ? x->aalg->alg_name : "digest_null",
448 x->ealg->alg_name) >= CRYPTO_MAX_ALG_NAME)
Herbert Xu1a6509d2008-01-28 19:37:29 -0800449 goto error;
Herbert Xu38320c72008-01-28 19:35:05 -0800450
451 aead = crypto_alloc_aead(authenc_name, 0, 0);
452 err = PTR_ERR(aead);
453 if (IS_ERR(aead))
454 goto error;
455
456 esp->aead = aead;
457
458 keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
459 (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
460 err = -ENOMEM;
461 key = kmalloc(keylen, GFP_KERNEL);
462 if (!key)
463 goto error;
464
465 p = key;
466 rta = (void *)p;
467 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
468 rta->rta_len = RTA_LENGTH(sizeof(*param));
469 param = RTA_DATA(rta);
470 p += RTA_SPACE(sizeof(*param));
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (x->aalg) {
473 struct xfrm_algo_desc *aalg_desc;
474
Herbert Xu38320c72008-01-28 19:35:05 -0800475 memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
476 p += (x->aalg->alg_key_len + 7) / 8;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
479 BUG_ON(!aalg_desc);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900480
Herbert Xu38320c72008-01-28 19:35:05 -0800481 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
Herbert Xu38320c72008-01-28 19:35:05 -0800483 crypto_aead_authsize(aead)) {
Herbert Xu07d4ee52006-08-20 14:24:50 +1000484 NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n",
485 x->aalg->alg_name,
Herbert Xu38320c72008-01-28 19:35:05 -0800486 crypto_aead_authsize(aead),
Herbert Xu07d4ee52006-08-20 14:24:50 +1000487 aalg_desc->uinfo.auth.icv_fullbits/8);
Herbert Xu38320c72008-01-28 19:35:05 -0800488 goto free_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900490
Herbert Xu38320c72008-01-28 19:35:05 -0800491 err = crypto_aead_setauthsize(
Martin Willi8f8a0882009-11-25 00:29:53 +0000492 aead, x->aalg->alg_trunc_len / 8);
Herbert Xu38320c72008-01-28 19:35:05 -0800493 if (err)
494 goto free_key;
495 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900496
Herbert Xu38320c72008-01-28 19:35:05 -0800497 param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
498 memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
499
500 err = crypto_aead_setkey(aead, key, keylen);
501
502free_key:
503 kfree(key);
504
Herbert Xu1a6509d2008-01-28 19:37:29 -0800505error:
506 return err;
507}
508
509static int esp6_init_state(struct xfrm_state *x)
510{
511 struct esp_data *esp;
512 struct crypto_aead *aead;
513 u32 align;
514 int err;
515
516 if (x->encap)
517 return -EINVAL;
518
519 esp = kzalloc(sizeof(*esp), GFP_KERNEL);
520 if (esp == NULL)
521 return -ENOMEM;
522
523 x->data = esp;
524
525 if (x->aead)
526 err = esp_init_aead(x);
527 else
528 err = esp_init_authenc(x);
529
Herbert Xu38320c72008-01-28 19:35:05 -0800530 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 goto error;
Herbert Xu38320c72008-01-28 19:35:05 -0800532
Herbert Xu1a6509d2008-01-28 19:37:29 -0800533 aead = esp->aead;
534
535 esp->padlen = 0;
536
Herbert Xu38320c72008-01-28 19:35:05 -0800537 x->props.header_len = sizeof(struct ip_esp_hdr) +
538 crypto_aead_ivsize(aead);
Herbert Xuca681452007-10-17 21:35:15 -0700539 switch (x->props.mode) {
540 case XFRM_MODE_BEET:
Joakim Koskelaabf5cdb2008-08-06 02:40:25 -0700541 if (x->sel.family != AF_INET6)
542 x->props.header_len += IPV4_BEET_PHMAXLEN +
543 (sizeof(struct ipv6hdr) - sizeof(struct iphdr));
544 break;
Herbert Xuca681452007-10-17 21:35:15 -0700545 case XFRM_MODE_TRANSPORT:
546 break;
547 case XFRM_MODE_TUNNEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 x->props.header_len += sizeof(struct ipv6hdr);
Masahide NAKAMURAea2c47b2007-10-22 02:30:15 -0700549 break;
Herbert Xuca681452007-10-17 21:35:15 -0700550 default:
551 goto error;
552 }
Herbert Xu38320c72008-01-28 19:35:05 -0800553
554 align = ALIGN(crypto_aead_blocksize(aead), 4);
555 if (esp->padlen)
556 align = max_t(u32, align, esp->padlen);
557 x->props.trailer_len = align + 1 + crypto_aead_authsize(esp->aead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559error:
Herbert Xu38320c72008-01-28 19:35:05 -0800560 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800563static const struct xfrm_type esp6_type =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 .description = "ESP6",
566 .owner = THIS_MODULE,
567 .proto = IPPROTO_ESP,
Herbert Xu436a0a42007-10-08 17:25:53 -0700568 .flags = XFRM_TYPE_REPLAY_PROT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 .init_state = esp6_init_state,
570 .destructor = esp6_destroy,
Patrick McHardyc5c25232007-04-09 11:47:18 -0700571 .get_mtu = esp6_get_mtu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 .input = esp6_input,
Masahide NAKAMURAaee5adb2006-08-23 17:57:28 -0700573 .output = esp6_output,
574 .hdr_offset = xfrm6_find_1stfragopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575};
576
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000577static const struct inet6_protocol esp6_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 .handler = xfrm6_rcv,
579 .err_handler = esp6_err,
580 .flags = INET6_PROTO_NOPOLICY,
581};
582
583static int __init esp6_init(void)
584{
585 if (xfrm_register_type(&esp6_type, AF_INET6) < 0) {
586 printk(KERN_INFO "ipv6 esp init: can't add xfrm type\n");
587 return -EAGAIN;
588 }
589 if (inet6_add_protocol(&esp6_protocol, IPPROTO_ESP) < 0) {
590 printk(KERN_INFO "ipv6 esp init: can't add protocol\n");
591 xfrm_unregister_type(&esp6_type, AF_INET6);
592 return -EAGAIN;
593 }
594
595 return 0;
596}
597
598static void __exit esp6_fini(void)
599{
600 if (inet6_del_protocol(&esp6_protocol, IPPROTO_ESP) < 0)
601 printk(KERN_INFO "ipv6 esp close: can't remove protocol\n");
602 if (xfrm_unregister_type(&esp6_type, AF_INET6) < 0)
603 printk(KERN_INFO "ipv6 esp close: can't remove xfrm type\n");
604}
605
606module_init(esp6_init);
607module_exit(esp6_fini);
608
609MODULE_LICENSE("GPL");
Masahide NAKAMURAd3d6dd32007-06-26 23:57:49 -0700610MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_ESP);