blob: 7ed3e4ae93ae7a9e0d72d818da04122d61497ff8 [file] [log] [blame]
Steffen Klassertdff3bb02009-10-07 22:48:17 +00001#include <crypto/hash.h>
Herbert Xu07d4ee52006-08-20 14:24:50 +10002#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#include <linux/module.h>
4#include <net/ip.h>
5#include <net/xfrm.h>
6#include <net/ah.h>
7#include <linux/crypto.h>
8#include <linux/pfkeyv2.h>
Steffen Klassertdff3bb02009-10-07 22:48:17 +00009#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <net/icmp.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020011#include <net/protocol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Steffen Klassertdff3bb02009-10-07 22:48:17 +000013struct ah_skb_cb {
14 struct xfrm_skb_cb xfrm;
15 void *tmp;
16};
17
18#define AH_SKB_CB(__skb) ((struct ah_skb_cb *)&((__skb)->cb[0]))
19
20static void *ah_alloc_tmp(struct crypto_ahash *ahash, int nfrags,
21 unsigned int size)
22{
23 unsigned int len;
24
25 len = size + crypto_ahash_digestsize(ahash) +
26 (crypto_ahash_alignmask(ahash) &
27 ~(crypto_tfm_ctx_alignment() - 1));
28
29 len = ALIGN(len, crypto_tfm_ctx_alignment());
30
31 len += sizeof(struct ahash_request) + crypto_ahash_reqsize(ahash);
32 len = ALIGN(len, __alignof__(struct scatterlist));
33
34 len += sizeof(struct scatterlist) * nfrags;
35
36 return kmalloc(len, GFP_ATOMIC);
37}
38
39static inline u8 *ah_tmp_auth(void *tmp, unsigned int offset)
40{
41 return tmp + offset;
42}
43
44static inline u8 *ah_tmp_icv(struct crypto_ahash *ahash, void *tmp,
45 unsigned int offset)
46{
47 return PTR_ALIGN((u8 *)tmp + offset, crypto_ahash_alignmask(ahash) + 1);
48}
49
50static inline struct ahash_request *ah_tmp_req(struct crypto_ahash *ahash,
51 u8 *icv)
52{
53 struct ahash_request *req;
54
55 req = (void *)PTR_ALIGN(icv + crypto_ahash_digestsize(ahash),
56 crypto_tfm_ctx_alignment());
57
58 ahash_request_set_tfm(req, ahash);
59
60 return req;
61}
62
63static inline struct scatterlist *ah_req_sg(struct crypto_ahash *ahash,
64 struct ahash_request *req)
65{
66 return (void *)ALIGN((unsigned long)(req + 1) +
67 crypto_ahash_reqsize(ahash),
68 __alignof__(struct scatterlist));
69}
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/* Clear mutable options and find final destination to substitute
72 * into IP header for icv calculation. Options are already checked
73 * for validity, so paranoia is not required. */
74
Al Virod5a0a1e2006-11-08 00:23:14 -080075static int ip_clear_mutable_options(struct iphdr *iph, __be32 *daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 unsigned char * optptr = (unsigned char*)(iph+1);
78 int l = iph->ihl*4 - sizeof(struct iphdr);
79 int optlen;
80
81 while (l > 0) {
82 switch (*optptr) {
83 case IPOPT_END:
84 return 0;
85 case IPOPT_NOOP:
86 l--;
87 optptr++;
88 continue;
89 }
90 optlen = optptr[1];
91 if (optlen<2 || optlen>l)
92 return -EINVAL;
93 switch (*optptr) {
94 case IPOPT_SEC:
95 case 0x85: /* Some "Extended Security" crap. */
Paul Moore11a03f72006-08-03 16:46:20 -070096 case IPOPT_CIPSO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 case IPOPT_RA:
98 case 0x80|21: /* RFC1770 */
99 break;
100 case IPOPT_LSRR:
101 case IPOPT_SSRR:
102 if (optlen < 6)
103 return -EINVAL;
104 memcpy(daddr, optptr+optlen-4, 4);
105 /* Fall through */
106 default:
Nick Bowler96fe1c02007-08-22 12:33:51 -0700107 memset(optptr, 0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
109 l -= optlen;
110 optptr += optlen;
111 }
112 return 0;
113}
114
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000115static void ah_output_done(struct crypto_async_request *base, int err)
116{
117 u8 *icv;
118 struct iphdr *iph;
119 struct sk_buff *skb = base->data;
120 struct xfrm_state *x = skb_dst(skb)->xfrm;
121 struct ah_data *ahp = x->data;
122 struct iphdr *top_iph = ip_hdr(skb);
123 struct ip_auth_hdr *ah = ip_auth_hdr(skb);
124 int ihl = ip_hdrlen(skb);
125
126 iph = AH_SKB_CB(skb)->tmp;
127 icv = ah_tmp_icv(ahp->ahash, iph, ihl);
128 memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
129
130 top_iph->tos = iph->tos;
131 top_iph->ttl = iph->ttl;
132 top_iph->frag_off = iph->frag_off;
133 if (top_iph->ihl != 5) {
134 top_iph->daddr = iph->daddr;
135 memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
136 }
137
138 err = ah->nexthdr;
139
140 kfree(AH_SKB_CB(skb)->tmp);
141 xfrm_output_resume(skb, err);
142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static int ah_output(struct xfrm_state *x, struct sk_buff *skb)
145{
146 int err;
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000147 int nfrags;
148 int ihl;
149 u8 *icv;
150 struct sk_buff *trailer;
151 struct crypto_ahash *ahash;
152 struct ahash_request *req;
153 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct iphdr *iph, *top_iph;
155 struct ip_auth_hdr *ah;
156 struct ah_data *ahp;
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000157
158 ahp = x->data;
159 ahash = ahp->ahash;
160
161 if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
162 goto out;
163 nfrags = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Herbert Xu7b277b12007-10-10 15:44:06 -0700165 skb_push(skb, -skb_network_offset(skb));
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000166 ah = ip_auth_hdr(skb);
167 ihl = ip_hdrlen(skb);
168
169 err = -ENOMEM;
170 iph = ah_alloc_tmp(ahash, nfrags, ihl);
171 if (!iph)
172 goto out;
173
174 icv = ah_tmp_icv(ahash, iph, ihl);
175 req = ah_tmp_req(ahash, icv);
176 sg = ah_req_sg(ahash, req);
177
178 memset(ah->auth_data, 0, ahp->icv_trunc_len);
179
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700180 top_iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 iph->tos = top_iph->tos;
183 iph->ttl = top_iph->ttl;
184 iph->frag_off = top_iph->frag_off;
185
186 if (top_iph->ihl != 5) {
187 iph->daddr = top_iph->daddr;
188 memcpy(iph+1, top_iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
189 err = ip_clear_mutable_options(top_iph, &top_iph->daddr);
190 if (err)
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000191 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193
Herbert Xu37fedd32007-10-10 15:44:44 -0700194 ah->nexthdr = *skb_mac_header(skb);
195 *skb_mac_header(skb) = IPPROTO_AH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 top_iph->tos = 0;
198 top_iph->tot_len = htons(skb->len);
199 top_iph->frag_off = 0;
200 top_iph->ttl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 top_iph->check = 0;
202
Herbert Xu87bdc482007-10-10 15:45:25 -0700203 ah->hdrlen = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 ah->reserved = 0;
206 ah->spi = x->id.spi;
Herbert Xub318e0e2008-02-12 22:50:35 -0800207 ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output);
Herbert Xub7c65382007-10-09 13:33:35 -0700208
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000209 sg_init_table(sg, nfrags);
210 skb_to_sgvec(skb, sg, 0, skb->len);
Herbert Xub7c65382007-10-09 13:33:35 -0700211
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000212 ahash_request_set_crypt(req, sg, icv, skb->len);
213 ahash_request_set_callback(req, 0, ah_output_done, skb);
214
215 AH_SKB_CB(skb)->tmp = iph;
216
217 err = crypto_ahash_digest(req);
218 if (err) {
219 if (err == -EINPROGRESS)
220 goto out;
221
222 if (err == -EBUSY)
223 err = NET_XMIT_DROP;
224 goto out_free;
225 }
226
227 memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 top_iph->tos = iph->tos;
230 top_iph->ttl = iph->ttl;
231 top_iph->frag_off = iph->frag_off;
232 if (top_iph->ihl != 5) {
233 top_iph->daddr = iph->daddr;
234 memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
235 }
236
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000237out_free:
238 kfree(iph);
239out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return err;
241}
242
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000243static void ah_input_done(struct crypto_async_request *base, int err)
244{
245 u8 *auth_data;
246 u8 *icv;
247 struct iphdr *work_iph;
248 struct sk_buff *skb = base->data;
249 struct xfrm_state *x = xfrm_input_state(skb);
250 struct ah_data *ahp = x->data;
251 struct ip_auth_hdr *ah = ip_auth_hdr(skb);
252 int ihl = ip_hdrlen(skb);
253 int ah_hlen = (ah->hdrlen + 2) << 2;
254
255 work_iph = AH_SKB_CB(skb)->tmp;
256 auth_data = ah_tmp_auth(work_iph, ihl);
257 icv = ah_tmp_icv(ahp->ahash, auth_data, ahp->icv_trunc_len);
258
259 err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0;
260 if (err)
261 goto out;
262
263 skb->network_header += ah_hlen;
264 memcpy(skb_network_header(skb), work_iph, ihl);
265 __skb_pull(skb, ah_hlen + ihl);
266 skb_set_transport_header(skb, -ihl);
267
268 err = ah->nexthdr;
269out:
270 kfree(AH_SKB_CB(skb)->tmp);
271 xfrm_input_resume(skb, err);
272}
273
Herbert Xue6956332006-04-01 00:52:46 -0800274static int ah_input(struct xfrm_state *x, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 int ah_hlen;
Herbert Xu31a4ab92006-05-27 23:06:13 -0700277 int ihl;
Herbert Xu631a6692007-10-10 15:46:21 -0700278 int nexthdr;
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000279 int nfrags;
280 u8 *auth_data;
281 u8 *icv;
282 struct sk_buff *trailer;
283 struct crypto_ahash *ahash;
284 struct ahash_request *req;
285 struct scatterlist *sg;
286 struct iphdr *iph, *work_iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 struct ip_auth_hdr *ah;
288 struct ah_data *ahp;
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000289 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Herbert Xu87bdc482007-10-10 15:45:25 -0700291 if (!pskb_may_pull(skb, sizeof(*ah)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 goto out;
293
Herbert Xu87bdc482007-10-10 15:45:25 -0700294 ah = (struct ip_auth_hdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 ahp = x->data;
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000296 ahash = ahp->ahash;
297
Herbert Xu631a6692007-10-10 15:46:21 -0700298 nexthdr = ah->nexthdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 ah_hlen = (ah->hdrlen + 2) << 2;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900300
Herbert Xu87bdc482007-10-10 15:45:25 -0700301 if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) &&
302 ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 goto out;
304
305 if (!pskb_may_pull(skb, ah_hlen))
306 goto out;
307
308 /* We are going to _remove_ AH header to keep sockets happy,
309 * so... Later this can change. */
310 if (skb_cloned(skb) &&
311 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
312 goto out;
313
314 skb->ip_summed = CHECKSUM_NONE;
315
Herbert Xu87bdc482007-10-10 15:45:25 -0700316 ah = (struct ip_auth_hdr *)skb->data;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700317 iph = ip_hdr(skb);
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000318 ihl = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000320 if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
321 goto out;
322 nfrags = err;
323
324 work_iph = ah_alloc_tmp(ahash, nfrags, ihl + ahp->icv_trunc_len);
325 if (!work_iph)
326 goto out;
327
328 auth_data = ah_tmp_auth(work_iph, ihl);
329 icv = ah_tmp_icv(ahash, auth_data, ahp->icv_trunc_len);
330 req = ah_tmp_req(ahash, icv);
331 sg = ah_req_sg(ahash, req);
332
333 memcpy(work_iph, iph, ihl);
334 memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
335 memset(ah->auth_data, 0, ahp->icv_trunc_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 iph->ttl = 0;
338 iph->tos = 0;
339 iph->frag_off = 0;
340 iph->check = 0;
Herbert Xu31a4ab92006-05-27 23:06:13 -0700341 if (ihl > sizeof(*iph)) {
Al Virod5a0a1e2006-11-08 00:23:14 -0800342 __be32 dummy;
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000343 err = ip_clear_mutable_options(iph, &dummy);
Herbert Xu07d4ee52006-08-20 14:24:50 +1000344 if (err)
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000345 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
Herbert Xu0ebea8e2007-11-13 21:45:58 -0800347
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000348 skb_push(skb, ihl);
349
350 sg_init_table(sg, nfrags);
351 skb_to_sgvec(skb, sg, 0, skb->len);
352
353 ahash_request_set_crypt(req, sg, icv, skb->len);
354 ahash_request_set_callback(req, 0, ah_input_done, skb);
355
356 AH_SKB_CB(skb)->tmp = work_iph;
357
358 err = crypto_ahash_digest(req);
359 if (err) {
360 if (err == -EINPROGRESS)
361 goto out;
362
363 if (err == -EBUSY)
364 err = NET_XMIT_DROP;
365 goto out_free;
366 }
367
368 err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0;
Herbert Xu0ebea8e2007-11-13 21:45:58 -0800369 if (err)
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000370 goto out_free;
Herbert Xu0ebea8e2007-11-13 21:45:58 -0800371
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700372 skb->network_header += ah_hlen;
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000373 memcpy(skb_network_header(skb), work_iph, ihl);
Herbert Xu31a4ab92006-05-27 23:06:13 -0700374 __skb_pull(skb, ah_hlen + ihl);
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000375 skb_set_transport_header(skb, -ihl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000377 err = nexthdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000379out_free:
380 kfree (work_iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381out:
Herbert Xu07d4ee52006-08-20 14:24:50 +1000382 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385static void ah4_err(struct sk_buff *skb, u32 info)
386{
Alexey Dobriyan4fb236b2008-11-25 17:59:27 -0800387 struct net *net = dev_net(skb->dev);
Jianjun Kongd9319102008-11-03 00:23:42 -0800388 struct iphdr *iph = (struct iphdr *)skb->data;
389 struct ip_auth_hdr *ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 struct xfrm_state *x;
391
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300392 if (icmp_hdr(skb)->type != ICMP_DEST_UNREACH ||
393 icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return;
395
Alexey Dobriyan4fb236b2008-11-25 17:59:27 -0800396 x = xfrm_state_lookup(net, (xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (!x)
398 return;
399 printk(KERN_DEBUG "pmtu discovery on SA AH/%08x/%08x\n",
400 ntohl(ah->spi), ntohl(iph->daddr));
401 xfrm_state_put(x);
402}
403
Herbert Xu72cb6962005-06-20 13:18:08 -0700404static int ah_init_state(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 struct ah_data *ahp = NULL;
407 struct xfrm_algo_desc *aalg_desc;
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000408 struct crypto_ahash *ahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 if (!x->aalg)
411 goto error;
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (x->encap)
414 goto error;
415
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700416 ahp = kzalloc(sizeof(*ahp), GFP_KERNEL);
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000417 if (!ahp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return -ENOMEM;
419
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000420 ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0);
421 if (IS_ERR(ahash))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 goto error;
Herbert Xu07d4ee52006-08-20 14:24:50 +1000423
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000424 ahp->ahash = ahash;
425 if (crypto_ahash_setkey(ahash, x->aalg->alg_key,
426 (x->aalg->alg_key_len + 7) / 8))
Herbert Xu07d4ee52006-08-20 14:24:50 +1000427 goto error;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /*
430 * Lookup the algorithm description maintained by xfrm_algo,
431 * verify crypto transform properties, and store information
432 * we need for AH processing. This lookup cannot fail here
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000433 * after a successful crypto_alloc_ahash().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 */
435 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
436 BUG_ON(!aalg_desc);
437
438 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000439 crypto_ahash_digestsize(ahash)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 printk(KERN_INFO "AH: %s digestsize %u != %hu\n",
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000441 x->aalg->alg_name, crypto_ahash_digestsize(ahash),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 aalg_desc->uinfo.auth.icv_fullbits/8);
443 goto error;
444 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
Martin Willi8f8a0882009-11-25 00:29:53 +0000447 ahp->icv_trunc_len = x->aalg->alg_trunc_len/8;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900450
Herbert Xu87bdc482007-10-10 15:45:25 -0700451 x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
452 ahp->icv_trunc_len);
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700453 if (x->props.mode == XFRM_MODE_TUNNEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 x->props.header_len += sizeof(struct iphdr);
455 x->data = ahp;
456
457 return 0;
458
459error:
460 if (ahp) {
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000461 crypto_free_ahash(ahp->ahash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 kfree(ahp);
463 }
464 return -EINVAL;
465}
466
467static void ah_destroy(struct xfrm_state *x)
468{
469 struct ah_data *ahp = x->data;
470
471 if (!ahp)
472 return;
473
Steffen Klassertdff3bb02009-10-07 22:48:17 +0000474 crypto_free_ahash(ahp->ahash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 kfree(ahp);
476}
477
478
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800479static const struct xfrm_type ah_type =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 .description = "AH4",
482 .owner = THIS_MODULE,
483 .proto = IPPROTO_AH,
Herbert Xu436a0a42007-10-08 17:25:53 -0700484 .flags = XFRM_TYPE_REPLAY_PROT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 .init_state = ah_init_state,
486 .destructor = ah_destroy,
487 .input = ah_input,
488 .output = ah_output
489};
490
Alexey Dobriyan32613092009-09-14 12:21:47 +0000491static const struct net_protocol ah4_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 .handler = xfrm4_rcv,
493 .err_handler = ah4_err,
494 .no_policy = 1,
Alexey Dobriyan4fb236b2008-11-25 17:59:27 -0800495 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496};
497
498static int __init ah4_init(void)
499{
500 if (xfrm_register_type(&ah_type, AF_INET) < 0) {
501 printk(KERN_INFO "ip ah init: can't add xfrm type\n");
502 return -EAGAIN;
503 }
504 if (inet_add_protocol(&ah4_protocol, IPPROTO_AH) < 0) {
505 printk(KERN_INFO "ip ah init: can't add protocol\n");
506 xfrm_unregister_type(&ah_type, AF_INET);
507 return -EAGAIN;
508 }
509 return 0;
510}
511
512static void __exit ah4_fini(void)
513{
514 if (inet_del_protocol(&ah4_protocol, IPPROTO_AH) < 0)
515 printk(KERN_INFO "ip ah close: can't remove protocol\n");
516 if (xfrm_unregister_type(&ah_type, AF_INET) < 0)
517 printk(KERN_INFO "ip ah close: can't remove xfrm type\n");
518}
519
520module_init(ah4_init);
521module_exit(ah4_fini);
522MODULE_LICENSE("GPL");
Masahide NAKAMURAd3d6dd32007-06-26 23:57:49 -0700523MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_AH);