blob: ca48c56c4b1717f7793099a0273b8e001aa25b40 [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
52/*
53 * Allocate an AEAD request structure with extra space for SG and IV.
54 *
55 * For alignment considerations the IV is placed at the front, followed
56 * by the request and finally the SG list.
57 *
58 * TODO: Use spare space in skb for this where possible.
59 */
60static void *esp_alloc_tmp(struct crypto_aead *aead, int nfrags)
61{
62 unsigned int len;
63
64 len = crypto_aead_ivsize(aead);
65 if (len) {
66 len += crypto_aead_alignmask(aead) &
67 ~(crypto_tfm_ctx_alignment() - 1);
68 len = ALIGN(len, crypto_tfm_ctx_alignment());
69 }
70
71 len += sizeof(struct aead_givcrypt_request) + crypto_aead_reqsize(aead);
72 len = ALIGN(len, __alignof__(struct scatterlist));
73
74 len += sizeof(struct scatterlist) * nfrags;
75
76 return kmalloc(len, GFP_ATOMIC);
77}
78
79static inline u8 *esp_tmp_iv(struct crypto_aead *aead, void *tmp)
80{
81 return crypto_aead_ivsize(aead) ?
82 PTR_ALIGN((u8 *)tmp, crypto_aead_alignmask(aead) + 1) : tmp;
83}
84
85static inline struct aead_givcrypt_request *esp_tmp_givreq(
86 struct crypto_aead *aead, u8 *iv)
87{
88 struct aead_givcrypt_request *req;
89
90 req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
91 crypto_tfm_ctx_alignment());
92 aead_givcrypt_set_tfm(req, aead);
93 return req;
94}
95
96static inline struct aead_request *esp_tmp_req(struct crypto_aead *aead, u8 *iv)
97{
98 struct aead_request *req;
99
100 req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
101 crypto_tfm_ctx_alignment());
102 aead_request_set_tfm(req, aead);
103 return req;
104}
105
106static inline struct scatterlist *esp_req_sg(struct crypto_aead *aead,
107 struct aead_request *req)
108{
109 return (void *)ALIGN((unsigned long)(req + 1) +
110 crypto_aead_reqsize(aead),
111 __alignof__(struct scatterlist));
112}
113
114static inline struct scatterlist *esp_givreq_sg(
115 struct crypto_aead *aead, struct aead_givcrypt_request *req)
116{
117 return (void *)ALIGN((unsigned long)(req + 1) +
118 crypto_aead_reqsize(aead),
119 __alignof__(struct scatterlist));
120}
121
122static void esp_output_done(struct crypto_async_request *base, int err)
123{
124 struct sk_buff *skb = base->data;
125
126 kfree(ESP_SKB_CB(skb)->tmp);
127 xfrm_output_resume(skb, err);
128}
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
131{
132 int err;
Herbert Xu87bdc482007-10-10 15:45:25 -0700133 struct ip_esp_hdr *esph;
Herbert Xu38320c72008-01-28 19:35:05 -0800134 struct crypto_aead *aead;
135 struct aead_givcrypt_request *req;
136 struct scatterlist *sg;
137 struct scatterlist *asg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 struct sk_buff *trailer;
Herbert Xu38320c72008-01-28 19:35:05 -0800139 void *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 int blksize;
141 int clen;
142 int alen;
143 int nfrags;
Herbert Xu38320c72008-01-28 19:35:05 -0800144 u8 *iv;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700145 u8 *tail;
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700146 struct esp_data *esp = x->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Herbert Xu7b277b12007-10-10 15:44:06 -0700148 /* skb is pure payload to encrypt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 err = -ENOMEM;
150
151 /* Round to block size */
152 clen = skb->len;
153
Herbert Xu38320c72008-01-28 19:35:05 -0800154 aead = esp->aead;
155 alen = crypto_aead_authsize(aead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Herbert Xu38320c72008-01-28 19:35:05 -0800157 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
158 clen = ALIGN(clen + 2, blksize);
159 if (esp->padlen)
160 clen = ALIGN(clen, esp->padlen);
161
162 if ((err = skb_cow_data(skb, clen - skb->len + alen, &trailer)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 goto error;
Herbert Xu38320c72008-01-28 19:35:05 -0800164 nfrags = err;
165
166 tmp = esp_alloc_tmp(aead, nfrags + 1);
167 if (!tmp)
168 goto error;
169
170 iv = esp_tmp_iv(aead, tmp);
171 req = esp_tmp_givreq(aead, iv);
172 asg = esp_givreq_sg(aead, req);
173 sg = asg + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 /* Fill padding... */
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700176 tail = skb_tail_pointer(trailer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 do {
178 int i;
179 for (i=0; i<clen-skb->len - 2; i++)
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700180 tail[i] = i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 } while (0);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700182 tail[clen-skb->len - 2] = (clen - skb->len) - 2;
Herbert Xu38320c72008-01-28 19:35:05 -0800183 tail[clen - skb->len - 1] = *skb_mac_header(skb);
184 pskb_put(skb, trailer, clen - skb->len + alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Herbert Xu7b277b12007-10-10 15:44:06 -0700186 skb_push(skb, -skb_network_offset(skb));
Herbert Xu87bdc482007-10-10 15:45:25 -0700187 esph = ip_esp_hdr(skb);
Herbert Xu007f0212007-10-09 13:25:59 -0700188 *skb_mac_header(skb) = IPPROTO_ESP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 esph->spi = x->id.spi;
Herbert Xu436a0a42007-10-08 17:25:53 -0700191 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Herbert Xu38320c72008-01-28 19:35:05 -0800193 sg_init_table(sg, nfrags);
194 skb_to_sgvec(skb, sg,
195 esph->enc_data + crypto_aead_ivsize(aead) - skb->data,
196 clen + alen);
197 sg_init_one(asg, esph, sizeof(*esph));
Herbert Xub7c65382007-10-09 13:33:35 -0700198
Herbert Xu38320c72008-01-28 19:35:05 -0800199 aead_givcrypt_set_callback(req, 0, esp_output_done, skb);
200 aead_givcrypt_set_crypt(req, sg, sg, clen, iv);
201 aead_givcrypt_set_assoc(req, asg, sizeof(*esph));
202 aead_givcrypt_set_giv(req, esph->enc_data, XFRM_SKB_CB(skb)->seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Herbert Xu38320c72008-01-28 19:35:05 -0800204 ESP_SKB_CB(skb)->tmp = tmp;
205 err = crypto_aead_givencrypt(req);
206 if (err == -EINPROGRESS)
207 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Herbert Xu38320c72008-01-28 19:35:05 -0800209 if (err == -EBUSY)
210 err = NET_XMIT_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Herbert Xu38320c72008-01-28 19:35:05 -0800212 kfree(tmp);
Herbert Xub7c65382007-10-09 13:33:35 -0700213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214error:
215 return err;
216}
217
Herbert Xu38320c72008-01-28 19:35:05 -0800218static int esp_input_done2(struct sk_buff *skb, int err)
219{
220 struct xfrm_state *x = xfrm_input_state(skb);
221 struct esp_data *esp = x->data;
222 struct crypto_aead *aead = esp->aead;
223 int alen = crypto_aead_authsize(aead);
224 int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
225 int elen = skb->len - hlen;
226 int hdr_len = skb_network_header_len(skb);
227 int padlen;
228 u8 nexthdr[2];
229
230 kfree(ESP_SKB_CB(skb)->tmp);
231
232 if (unlikely(err))
233 goto out;
234
235 if (skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2))
236 BUG();
237
238 err = -EINVAL;
239 padlen = nexthdr[0];
240 if (padlen + 2 + alen >= elen) {
241 LIMIT_NETDEBUG(KERN_WARNING "ipsec esp packet is garbage "
242 "padlen=%d, elen=%d\n", padlen + 2, elen - alen);
243 goto out;
244 }
245
246 /* ... check padding bits here. Silly. :-) */
247
248 pskb_trim(skb, skb->len - alen - padlen - 2);
249 __skb_pull(skb, hlen);
250 skb_set_transport_header(skb, -hdr_len);
251
252 err = nexthdr[1];
253
254 /* RFC4303: Drop dummy packets without any error */
255 if (err == IPPROTO_NONE)
256 err = -EINVAL;
257
258out:
259 return err;
260}
261
262static void esp_input_done(struct crypto_async_request *base, int err)
263{
264 struct sk_buff *skb = base->data;
265
266 xfrm_input_resume(skb, esp_input_done2(skb, err));
267}
268
Herbert Xue6956332006-04-01 00:52:46 -0800269static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Herbert Xu87bdc482007-10-10 15:45:25 -0700271 struct ip_esp_hdr *esph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 struct esp_data *esp = x->data;
Herbert Xu38320c72008-01-28 19:35:05 -0800273 struct crypto_aead *aead = esp->aead;
274 struct aead_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 struct sk_buff *trailer;
Herbert Xu38320c72008-01-28 19:35:05 -0800276 int elen = skb->len - sizeof(*esph) - crypto_aead_ivsize(aead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 int nfrags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 int ret = 0;
Herbert Xu38320c72008-01-28 19:35:05 -0800279 void *tmp;
280 u8 *iv;
281 struct scatterlist *sg;
282 struct scatterlist *asg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Herbert Xu87bdc482007-10-10 15:45:25 -0700284 if (!pskb_may_pull(skb, sizeof(*esph))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 ret = -EINVAL;
Herbert Xu31a4ab92006-05-27 23:06:13 -0700286 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288
Herbert Xu38320c72008-01-28 19:35:05 -0800289 if (elen <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 ret = -EINVAL;
Herbert Xu31a4ab92006-05-27 23:06:13 -0700291 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0) {
295 ret = -EINVAL;
296 goto out;
297 }
298
Herbert Xu38320c72008-01-28 19:35:05 -0800299 ret = -ENOMEM;
300 tmp = esp_alloc_tmp(aead, nfrags + 1);
301 if (!tmp)
Herbert Xu0ebea8e2007-11-13 21:45:58 -0800302 goto out;
303
Herbert Xu38320c72008-01-28 19:35:05 -0800304 ESP_SKB_CB(skb)->tmp = tmp;
305 iv = esp_tmp_iv(aead, tmp);
306 req = esp_tmp_req(aead, iv);
307 asg = esp_req_sg(aead, req);
308 sg = asg + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Herbert Xu38320c72008-01-28 19:35:05 -0800310 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Herbert Xu38320c72008-01-28 19:35:05 -0800312 esph = (struct ip_esp_hdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Herbert Xu38320c72008-01-28 19:35:05 -0800314 /* Get ivec. This can be wrong, check against another impls. */
315 iv = esph->enc_data;
Thomas Graf95a02cf2007-12-10 16:53:29 -0800316
Herbert Xu38320c72008-01-28 19:35:05 -0800317 sg_init_table(sg, nfrags);
318 skb_to_sgvec(skb, sg, sizeof(*esph) + crypto_aead_ivsize(aead), elen);
319 sg_init_one(asg, esph, sizeof(*esph));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Herbert Xu38320c72008-01-28 19:35:05 -0800321 aead_request_set_callback(req, 0, esp_input_done, skb);
322 aead_request_set_crypt(req, sg, sg, elen, iv);
323 aead_request_set_assoc(req, asg, sizeof(*esph));
324
325 ret = crypto_aead_decrypt(req);
326 if (ret == -EINPROGRESS)
327 goto out;
328
329 ret = esp_input_done2(skb, ret);
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return ret;
333}
334
Patrick McHardyc5c25232007-04-09 11:47:18 -0700335static u32 esp6_get_mtu(struct xfrm_state *x, int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 struct esp_data *esp = x->data;
Herbert Xu38320c72008-01-28 19:35:05 -0800338 u32 blksize = ALIGN(crypto_aead_blocksize(esp->aead), 4);
339 u32 align = max_t(u32, blksize, esp->padlen);
Patrick McHardyc5c25232007-04-09 11:47:18 -0700340 u32 rem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Herbert Xu38320c72008-01-28 19:35:05 -0800342 mtu -= x->props.header_len + crypto_aead_authsize(esp->aead);
Patrick McHardyc5c25232007-04-09 11:47:18 -0700343 rem = mtu & (align - 1);
344 mtu &= ~(align - 1);
345
346 if (x->props.mode != XFRM_MODE_TUNNEL) {
Herbert Xud4875b02005-10-10 21:11:34 -0700347 u32 padsize = ((blksize - 1) & 7) + 1;
Patrick McHardyc5c25232007-04-09 11:47:18 -0700348 mtu -= blksize - padsize;
349 mtu += min_t(u32, blksize - padsize, rem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Patrick McHardyc5c25232007-04-09 11:47:18 -0700352 return mtu - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
355static void esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900356 int type, int code, int offset, __be32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
Herbert Xu87bdc482007-10-10 15:45:25 -0700359 struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 struct xfrm_state *x;
361
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900362 if (type != ICMPV6_DEST_UNREACH &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 type != ICMPV6_PKT_TOOBIG)
364 return;
365
366 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET6);
367 if (!x)
368 return;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900369 printk(KERN_DEBUG "pmtu discovery on SA ESP/%08x/" NIP6_FMT "\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 ntohl(esph->spi), NIP6(iph->daddr));
371 xfrm_state_put(x);
372}
373
374static void esp6_destroy(struct xfrm_state *x)
375{
376 struct esp_data *esp = x->data;
377
378 if (!esp)
379 return;
380
Herbert Xu38320c72008-01-28 19:35:05 -0800381 crypto_free_aead(esp->aead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 kfree(esp);
383}
384
Herbert Xu1a6509d2008-01-28 19:37:29 -0800385static int esp_init_aead(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Herbert Xu1a6509d2008-01-28 19:37:29 -0800387 struct esp_data *esp = x->data;
388 struct crypto_aead *aead;
389 int err;
390
391 aead = crypto_alloc_aead(x->aead->alg_name, 0, 0);
392 err = PTR_ERR(aead);
393 if (IS_ERR(aead))
394 goto error;
395
396 esp->aead = aead;
397
398 err = crypto_aead_setkey(aead, x->aead->alg_key,
399 (x->aead->alg_key_len + 7) / 8);
400 if (err)
401 goto error;
402
403 err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
404 if (err)
405 goto error;
406
407error:
408 return err;
409}
410
411static int esp_init_authenc(struct xfrm_state *x)
412{
413 struct esp_data *esp = x->data;
Herbert Xu38320c72008-01-28 19:35:05 -0800414 struct crypto_aead *aead;
415 struct crypto_authenc_key_param *param;
416 struct rtattr *rta;
417 char *key;
418 char *p;
419 char authenc_name[CRYPTO_MAX_ALG_NAME];
Herbert Xu38320c72008-01-28 19:35:05 -0800420 unsigned int keylen;
421 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Herbert Xu1a6509d2008-01-28 19:37:29 -0800423 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (x->ealg == NULL)
Herbert Xu1a6509d2008-01-28 19:37:29 -0800425 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Herbert Xu1a6509d2008-01-28 19:37:29 -0800427 err = -ENAMETOOLONG;
Herbert Xu38320c72008-01-28 19:35:05 -0800428 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME, "authenc(%s,%s)",
429 x->aalg ? x->aalg->alg_name : "digest_null",
430 x->ealg->alg_name) >= CRYPTO_MAX_ALG_NAME)
Herbert Xu1a6509d2008-01-28 19:37:29 -0800431 goto error;
Herbert Xu38320c72008-01-28 19:35:05 -0800432
433 aead = crypto_alloc_aead(authenc_name, 0, 0);
434 err = PTR_ERR(aead);
435 if (IS_ERR(aead))
436 goto error;
437
438 esp->aead = aead;
439
440 keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
441 (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
442 err = -ENOMEM;
443 key = kmalloc(keylen, GFP_KERNEL);
444 if (!key)
445 goto error;
446
447 p = key;
448 rta = (void *)p;
449 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
450 rta->rta_len = RTA_LENGTH(sizeof(*param));
451 param = RTA_DATA(rta);
452 p += RTA_SPACE(sizeof(*param));
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (x->aalg) {
455 struct xfrm_algo_desc *aalg_desc;
456
Herbert Xu38320c72008-01-28 19:35:05 -0800457 memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
458 p += (x->aalg->alg_key_len + 7) / 8;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
461 BUG_ON(!aalg_desc);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900462
Herbert Xu38320c72008-01-28 19:35:05 -0800463 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
Herbert Xu38320c72008-01-28 19:35:05 -0800465 crypto_aead_authsize(aead)) {
Herbert Xu07d4ee52006-08-20 14:24:50 +1000466 NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n",
467 x->aalg->alg_name,
Herbert Xu38320c72008-01-28 19:35:05 -0800468 crypto_aead_authsize(aead),
Herbert Xu07d4ee52006-08-20 14:24:50 +1000469 aalg_desc->uinfo.auth.icv_fullbits/8);
Herbert Xu38320c72008-01-28 19:35:05 -0800470 goto free_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900472
Herbert Xu38320c72008-01-28 19:35:05 -0800473 err = crypto_aead_setauthsize(
474 aead, aalg_desc->uinfo.auth.icv_truncbits / 8);
475 if (err)
476 goto free_key;
477 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900478
Herbert Xu38320c72008-01-28 19:35:05 -0800479 param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
480 memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
481
482 err = crypto_aead_setkey(aead, key, keylen);
483
484free_key:
485 kfree(key);
486
Herbert Xu1a6509d2008-01-28 19:37:29 -0800487error:
488 return err;
489}
490
491static int esp6_init_state(struct xfrm_state *x)
492{
493 struct esp_data *esp;
494 struct crypto_aead *aead;
495 u32 align;
496 int err;
497
498 if (x->encap)
499 return -EINVAL;
500
501 esp = kzalloc(sizeof(*esp), GFP_KERNEL);
502 if (esp == NULL)
503 return -ENOMEM;
504
505 x->data = esp;
506
507 if (x->aead)
508 err = esp_init_aead(x);
509 else
510 err = esp_init_authenc(x);
511
Herbert Xu38320c72008-01-28 19:35:05 -0800512 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 goto error;
Herbert Xu38320c72008-01-28 19:35:05 -0800514
Herbert Xu1a6509d2008-01-28 19:37:29 -0800515 aead = esp->aead;
516
517 esp->padlen = 0;
518
Herbert Xu38320c72008-01-28 19:35:05 -0800519 x->props.header_len = sizeof(struct ip_esp_hdr) +
520 crypto_aead_ivsize(aead);
Herbert Xuca681452007-10-17 21:35:15 -0700521 switch (x->props.mode) {
522 case XFRM_MODE_BEET:
523 case XFRM_MODE_TRANSPORT:
524 break;
525 case XFRM_MODE_TUNNEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 x->props.header_len += sizeof(struct ipv6hdr);
Masahide NAKAMURAea2c47b2007-10-22 02:30:15 -0700527 break;
Herbert Xuca681452007-10-17 21:35:15 -0700528 default:
529 goto error;
530 }
Herbert Xu38320c72008-01-28 19:35:05 -0800531
532 align = ALIGN(crypto_aead_blocksize(aead), 4);
533 if (esp->padlen)
534 align = max_t(u32, align, esp->padlen);
535 x->props.trailer_len = align + 1 + crypto_aead_authsize(esp->aead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537error:
Herbert Xu38320c72008-01-28 19:35:05 -0800538 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
541static struct xfrm_type esp6_type =
542{
543 .description = "ESP6",
544 .owner = THIS_MODULE,
545 .proto = IPPROTO_ESP,
Herbert Xu436a0a42007-10-08 17:25:53 -0700546 .flags = XFRM_TYPE_REPLAY_PROT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 .init_state = esp6_init_state,
548 .destructor = esp6_destroy,
Patrick McHardyc5c25232007-04-09 11:47:18 -0700549 .get_mtu = esp6_get_mtu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 .input = esp6_input,
Masahide NAKAMURAaee5adb2006-08-23 17:57:28 -0700551 .output = esp6_output,
552 .hdr_offset = xfrm6_find_1stfragopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553};
554
555static struct inet6_protocol esp6_protocol = {
556 .handler = xfrm6_rcv,
557 .err_handler = esp6_err,
558 .flags = INET6_PROTO_NOPOLICY,
559};
560
561static int __init esp6_init(void)
562{
563 if (xfrm_register_type(&esp6_type, AF_INET6) < 0) {
564 printk(KERN_INFO "ipv6 esp init: can't add xfrm type\n");
565 return -EAGAIN;
566 }
567 if (inet6_add_protocol(&esp6_protocol, IPPROTO_ESP) < 0) {
568 printk(KERN_INFO "ipv6 esp init: can't add protocol\n");
569 xfrm_unregister_type(&esp6_type, AF_INET6);
570 return -EAGAIN;
571 }
572
573 return 0;
574}
575
576static void __exit esp6_fini(void)
577{
578 if (inet6_del_protocol(&esp6_protocol, IPPROTO_ESP) < 0)
579 printk(KERN_INFO "ipv6 esp close: can't remove protocol\n");
580 if (xfrm_unregister_type(&esp6_type, AF_INET6) < 0)
581 printk(KERN_INFO "ipv6 esp close: can't remove xfrm type\n");
582}
583
584module_init(esp6_init);
585module_exit(esp6_fini);
586
587MODULE_LICENSE("GPL");
Masahide NAKAMURAd3d6dd32007-06-26 23:57:49 -0700588MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_ESP);