xref: /openbmc/linux/net/ipv6/ah6.c (revision b6dcefde)
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/ah.c.
25  */
26 
27 #include <crypto/hash.h>
28 #include <linux/module.h>
29 #include <net/ip.h>
30 #include <net/ah.h>
31 #include <linux/crypto.h>
32 #include <linux/pfkeyv2.h>
33 #include <linux/string.h>
34 #include <linux/scatterlist.h>
35 #include <net/icmp.h>
36 #include <net/ipv6.h>
37 #include <net/protocol.h>
38 #include <net/xfrm.h>
39 
40 #define IPV6HDR_BASELEN 8
41 
42 struct tmp_ext {
43 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
44 		struct in6_addr saddr;
45 #endif
46 		struct in6_addr daddr;
47 		char hdrs[0];
48 };
49 
50 struct ah_skb_cb {
51 	struct xfrm_skb_cb xfrm;
52 	void *tmp;
53 };
54 
55 #define AH_SKB_CB(__skb) ((struct ah_skb_cb *)&((__skb)->cb[0]))
56 
57 static void *ah_alloc_tmp(struct crypto_ahash *ahash, int nfrags,
58 			  unsigned int size)
59 {
60 	unsigned int len;
61 
62 	len = size + crypto_ahash_digestsize(ahash) +
63 	      (crypto_ahash_alignmask(ahash) &
64 	       ~(crypto_tfm_ctx_alignment() - 1));
65 
66 	len = ALIGN(len, crypto_tfm_ctx_alignment());
67 
68 	len += sizeof(struct ahash_request) + crypto_ahash_reqsize(ahash);
69 	len = ALIGN(len, __alignof__(struct scatterlist));
70 
71 	len += sizeof(struct scatterlist) * nfrags;
72 
73 	return kmalloc(len, GFP_ATOMIC);
74 }
75 
76 static inline struct tmp_ext *ah_tmp_ext(void *base)
77 {
78 	return base + IPV6HDR_BASELEN;
79 }
80 
81 static inline u8 *ah_tmp_auth(u8 *tmp, unsigned int offset)
82 {
83 	return tmp + offset;
84 }
85 
86 static inline u8 *ah_tmp_icv(struct crypto_ahash *ahash, void *tmp,
87 			     unsigned int offset)
88 {
89 	return PTR_ALIGN((u8 *)tmp + offset, crypto_ahash_alignmask(ahash) + 1);
90 }
91 
92 static inline struct ahash_request *ah_tmp_req(struct crypto_ahash *ahash,
93 					       u8 *icv)
94 {
95 	struct ahash_request *req;
96 
97 	req = (void *)PTR_ALIGN(icv + crypto_ahash_digestsize(ahash),
98 				crypto_tfm_ctx_alignment());
99 
100 	ahash_request_set_tfm(req, ahash);
101 
102 	return req;
103 }
104 
105 static inline struct scatterlist *ah_req_sg(struct crypto_ahash *ahash,
106 					     struct ahash_request *req)
107 {
108 	return (void *)ALIGN((unsigned long)(req + 1) +
109 			     crypto_ahash_reqsize(ahash),
110 			     __alignof__(struct scatterlist));
111 }
112 
113 static int zero_out_mutable_opts(struct ipv6_opt_hdr *opthdr)
114 {
115 	u8 *opt = (u8 *)opthdr;
116 	int len = ipv6_optlen(opthdr);
117 	int off = 0;
118 	int optlen = 0;
119 
120 	off += 2;
121 	len -= 2;
122 
123 	while (len > 0) {
124 
125 		switch (opt[off]) {
126 
127 		case IPV6_TLV_PAD0:
128 			optlen = 1;
129 			break;
130 		default:
131 			if (len < 2)
132 				goto bad;
133 			optlen = opt[off+1]+2;
134 			if (len < optlen)
135 				goto bad;
136 			if (opt[off] & 0x20)
137 				memset(&opt[off+2], 0, opt[off+1]);
138 			break;
139 		}
140 
141 		off += optlen;
142 		len -= optlen;
143 	}
144 	if (len == 0)
145 		return 1;
146 
147 bad:
148 	return 0;
149 }
150 
151 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
152 /**
153  *	ipv6_rearrange_destopt - rearrange IPv6 destination options header
154  *	@iph: IPv6 header
155  *	@destopt: destionation options header
156  */
157 static void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt)
158 {
159 	u8 *opt = (u8 *)destopt;
160 	int len = ipv6_optlen(destopt);
161 	int off = 0;
162 	int optlen = 0;
163 
164 	off += 2;
165 	len -= 2;
166 
167 	while (len > 0) {
168 
169 		switch (opt[off]) {
170 
171 		case IPV6_TLV_PAD0:
172 			optlen = 1;
173 			break;
174 		default:
175 			if (len < 2)
176 				goto bad;
177 			optlen = opt[off+1]+2;
178 			if (len < optlen)
179 				goto bad;
180 
181 			/* Rearrange the source address in @iph and the
182 			 * addresses in home address option for final source.
183 			 * See 11.3.2 of RFC 3775 for details.
184 			 */
185 			if (opt[off] == IPV6_TLV_HAO) {
186 				struct in6_addr final_addr;
187 				struct ipv6_destopt_hao *hao;
188 
189 				hao = (struct ipv6_destopt_hao *)&opt[off];
190 				if (hao->length != sizeof(hao->addr)) {
191 					if (net_ratelimit())
192 						printk(KERN_WARNING "destopt hao: invalid header length: %u\n", hao->length);
193 					goto bad;
194 				}
195 				ipv6_addr_copy(&final_addr, &hao->addr);
196 				ipv6_addr_copy(&hao->addr, &iph->saddr);
197 				ipv6_addr_copy(&iph->saddr, &final_addr);
198 			}
199 			break;
200 		}
201 
202 		off += optlen;
203 		len -= optlen;
204 	}
205 	/* Note: ok if len == 0 */
206 bad:
207 	return;
208 }
209 #else
210 static void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt) {}
211 #endif
212 
213 /**
214  *	ipv6_rearrange_rthdr - rearrange IPv6 routing header
215  *	@iph: IPv6 header
216  *	@rthdr: routing header
217  *
218  *	Rearrange the destination address in @iph and the addresses in @rthdr
219  *	so that they appear in the order they will at the final destination.
220  *	See Appendix A2 of RFC 2402 for details.
221  */
222 static void ipv6_rearrange_rthdr(struct ipv6hdr *iph, struct ipv6_rt_hdr *rthdr)
223 {
224 	int segments, segments_left;
225 	struct in6_addr *addrs;
226 	struct in6_addr final_addr;
227 
228 	segments_left = rthdr->segments_left;
229 	if (segments_left == 0)
230 		return;
231 	rthdr->segments_left = 0;
232 
233 	/* The value of rthdr->hdrlen has been verified either by the system
234 	 * call if it is locally generated, or by ipv6_rthdr_rcv() for incoming
235 	 * packets.  So we can assume that it is even and that segments is
236 	 * greater than or equal to segments_left.
237 	 *
238 	 * For the same reason we can assume that this option is of type 0.
239 	 */
240 	segments = rthdr->hdrlen >> 1;
241 
242 	addrs = ((struct rt0_hdr *)rthdr)->addr;
243 	ipv6_addr_copy(&final_addr, addrs + segments - 1);
244 
245 	addrs += segments - segments_left;
246 	memmove(addrs + 1, addrs, (segments_left - 1) * sizeof(*addrs));
247 
248 	ipv6_addr_copy(addrs, &iph->daddr);
249 	ipv6_addr_copy(&iph->daddr, &final_addr);
250 }
251 
252 static int ipv6_clear_mutable_options(struct ipv6hdr *iph, int len, int dir)
253 {
254 	union {
255 		struct ipv6hdr *iph;
256 		struct ipv6_opt_hdr *opth;
257 		struct ipv6_rt_hdr *rth;
258 		char *raw;
259 	} exthdr = { .iph = iph };
260 	char *end = exthdr.raw + len;
261 	int nexthdr = iph->nexthdr;
262 
263 	exthdr.iph++;
264 
265 	while (exthdr.raw < end) {
266 		switch (nexthdr) {
267 		case NEXTHDR_DEST:
268 			if (dir == XFRM_POLICY_OUT)
269 				ipv6_rearrange_destopt(iph, exthdr.opth);
270 		case NEXTHDR_HOP:
271 			if (!zero_out_mutable_opts(exthdr.opth)) {
272 				LIMIT_NETDEBUG(
273 					KERN_WARNING "overrun %sopts\n",
274 					nexthdr == NEXTHDR_HOP ?
275 						"hop" : "dest");
276 				return -EINVAL;
277 			}
278 			break;
279 
280 		case NEXTHDR_ROUTING:
281 			ipv6_rearrange_rthdr(iph, exthdr.rth);
282 			break;
283 
284 		default :
285 			return 0;
286 		}
287 
288 		nexthdr = exthdr.opth->nexthdr;
289 		exthdr.raw += ipv6_optlen(exthdr.opth);
290 	}
291 
292 	return 0;
293 }
294 
295 static void ah6_output_done(struct crypto_async_request *base, int err)
296 {
297 	int extlen;
298 	u8 *iph_base;
299 	u8 *icv;
300 	struct sk_buff *skb = base->data;
301 	struct xfrm_state *x = skb_dst(skb)->xfrm;
302 	struct ah_data *ahp = x->data;
303 	struct ipv6hdr *top_iph = ipv6_hdr(skb);
304 	struct ip_auth_hdr *ah = ip_auth_hdr(skb);
305 	struct tmp_ext *iph_ext;
306 
307 	extlen = skb_network_header_len(skb) - sizeof(struct ipv6hdr);
308 	if (extlen)
309 		extlen += sizeof(*iph_ext);
310 
311 	iph_base = AH_SKB_CB(skb)->tmp;
312 	iph_ext = ah_tmp_ext(iph_base);
313 	icv = ah_tmp_icv(ahp->ahash, iph_ext, extlen);
314 
315 	memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
316 	memcpy(top_iph, iph_base, IPV6HDR_BASELEN);
317 
318 	if (extlen) {
319 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
320 		memcpy(&top_iph->saddr, iph_ext, extlen);
321 #else
322 		memcpy(&top_iph->daddr, iph_ext, extlen);
323 #endif
324 	}
325 
326 	err = ah->nexthdr;
327 
328 	kfree(AH_SKB_CB(skb)->tmp);
329 	xfrm_output_resume(skb, err);
330 }
331 
332 static int ah6_output(struct xfrm_state *x, struct sk_buff *skb)
333 {
334 	int err;
335 	int nfrags;
336 	int extlen;
337 	u8 *iph_base;
338 	u8 *icv;
339 	u8 nexthdr;
340 	struct sk_buff *trailer;
341 	struct crypto_ahash *ahash;
342 	struct ahash_request *req;
343 	struct scatterlist *sg;
344 	struct ipv6hdr *top_iph;
345 	struct ip_auth_hdr *ah;
346 	struct ah_data *ahp;
347 	struct tmp_ext *iph_ext;
348 
349 	ahp = x->data;
350 	ahash = ahp->ahash;
351 
352 	if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
353 		goto out;
354 	nfrags = err;
355 
356 	skb_push(skb, -skb_network_offset(skb));
357 	extlen = skb_network_header_len(skb) - sizeof(struct ipv6hdr);
358 	if (extlen)
359 		extlen += sizeof(*iph_ext);
360 
361 	err = -ENOMEM;
362 	iph_base = ah_alloc_tmp(ahash, nfrags, IPV6HDR_BASELEN + extlen);
363 	if (!iph_base)
364 		goto out;
365 
366 	iph_ext = ah_tmp_ext(iph_base);
367 	icv = ah_tmp_icv(ahash, iph_ext, extlen);
368 	req = ah_tmp_req(ahash, icv);
369 	sg = ah_req_sg(ahash, req);
370 
371 	ah = ip_auth_hdr(skb);
372 	memset(ah->auth_data, 0, ahp->icv_trunc_len);
373 
374 	top_iph = ipv6_hdr(skb);
375 	top_iph->payload_len = htons(skb->len - sizeof(*top_iph));
376 
377 	nexthdr = *skb_mac_header(skb);
378 	*skb_mac_header(skb) = IPPROTO_AH;
379 
380 	/* When there are no extension headers, we only need to save the first
381 	 * 8 bytes of the base IP header.
382 	 */
383 	memcpy(iph_base, top_iph, IPV6HDR_BASELEN);
384 
385 	if (extlen) {
386 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
387 		memcpy(iph_ext, &top_iph->saddr, extlen);
388 #else
389 		memcpy(iph_ext, &top_iph->daddr, extlen);
390 #endif
391 		err = ipv6_clear_mutable_options(top_iph,
392 						 extlen - sizeof(*iph_ext) +
393 						 sizeof(*top_iph),
394 						 XFRM_POLICY_OUT);
395 		if (err)
396 			goto out_free;
397 	}
398 
399 	ah->nexthdr = nexthdr;
400 
401 	top_iph->priority    = 0;
402 	top_iph->flow_lbl[0] = 0;
403 	top_iph->flow_lbl[1] = 0;
404 	top_iph->flow_lbl[2] = 0;
405 	top_iph->hop_limit   = 0;
406 
407 	ah->hdrlen  = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2;
408 
409 	ah->reserved = 0;
410 	ah->spi = x->id.spi;
411 	ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output);
412 
413 	sg_init_table(sg, nfrags);
414 	skb_to_sgvec(skb, sg, 0, skb->len);
415 
416 	ahash_request_set_crypt(req, sg, icv, skb->len);
417 	ahash_request_set_callback(req, 0, ah6_output_done, skb);
418 
419 	AH_SKB_CB(skb)->tmp = iph_base;
420 
421 	err = crypto_ahash_digest(req);
422 	if (err) {
423 		if (err == -EINPROGRESS)
424 			goto out;
425 
426 		if (err == -EBUSY)
427 			err = NET_XMIT_DROP;
428 		goto out_free;
429 	}
430 
431 	memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
432 	memcpy(top_iph, iph_base, IPV6HDR_BASELEN);
433 
434 	if (extlen) {
435 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
436 		memcpy(&top_iph->saddr, iph_ext, extlen);
437 #else
438 		memcpy(&top_iph->daddr, iph_ext, extlen);
439 #endif
440 	}
441 
442 out_free:
443 	kfree(iph_base);
444 out:
445 	return err;
446 }
447 
448 static void ah6_input_done(struct crypto_async_request *base, int err)
449 {
450 	u8 *auth_data;
451 	u8 *icv;
452 	u8 *work_iph;
453 	struct sk_buff *skb = base->data;
454 	struct xfrm_state *x = xfrm_input_state(skb);
455 	struct ah_data *ahp = x->data;
456 	struct ip_auth_hdr *ah = ip_auth_hdr(skb);
457 	int hdr_len = skb_network_header_len(skb);
458 	int ah_hlen = (ah->hdrlen + 2) << 2;
459 
460 	work_iph = AH_SKB_CB(skb)->tmp;
461 	auth_data = ah_tmp_auth(work_iph, hdr_len);
462 	icv = ah_tmp_icv(ahp->ahash, auth_data, ahp->icv_trunc_len);
463 
464 	err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0;
465 	if (err)
466 		goto out;
467 
468 	skb->network_header += ah_hlen;
469 	memcpy(skb_network_header(skb), work_iph, hdr_len);
470 	__skb_pull(skb, ah_hlen + hdr_len);
471 	skb_set_transport_header(skb, -hdr_len);
472 
473 	err = ah->nexthdr;
474 out:
475 	kfree(AH_SKB_CB(skb)->tmp);
476 	xfrm_input_resume(skb, err);
477 }
478 
479 
480 
481 static int ah6_input(struct xfrm_state *x, struct sk_buff *skb)
482 {
483 	/*
484 	 * Before process AH
485 	 * [IPv6][Ext1][Ext2][AH][Dest][Payload]
486 	 * |<-------------->| hdr_len
487 	 *
488 	 * To erase AH:
489 	 * Keeping copy of cleared headers. After AH processing,
490 	 * Moving the pointer of skb->network_header by using skb_pull as long
491 	 * as AH header length. Then copy back the copy as long as hdr_len
492 	 * If destination header following AH exists, copy it into after [Ext2].
493 	 *
494 	 * |<>|[IPv6][Ext1][Ext2][Dest][Payload]
495 	 * There is offset of AH before IPv6 header after the process.
496 	 */
497 
498 	u8 *auth_data;
499 	u8 *icv;
500 	u8 *work_iph;
501 	struct sk_buff *trailer;
502 	struct crypto_ahash *ahash;
503 	struct ahash_request *req;
504 	struct scatterlist *sg;
505 	struct ip_auth_hdr *ah;
506 	struct ipv6hdr *ip6h;
507 	struct ah_data *ahp;
508 	u16 hdr_len;
509 	u16 ah_hlen;
510 	int nexthdr;
511 	int nfrags;
512 	int err = -ENOMEM;
513 
514 	if (!pskb_may_pull(skb, sizeof(struct ip_auth_hdr)))
515 		goto out;
516 
517 	/* We are going to _remove_ AH header to keep sockets happy,
518 	 * so... Later this can change. */
519 	if (skb_cloned(skb) &&
520 	    pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
521 		goto out;
522 
523 	skb->ip_summed = CHECKSUM_NONE;
524 
525 	hdr_len = skb_network_header_len(skb);
526 	ah = (struct ip_auth_hdr *)skb->data;
527 	ahp = x->data;
528 	ahash = ahp->ahash;
529 
530 	nexthdr = ah->nexthdr;
531 	ah_hlen = (ah->hdrlen + 2) << 2;
532 
533 	if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) &&
534 	    ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len))
535 		goto out;
536 
537 	if (!pskb_may_pull(skb, ah_hlen))
538 		goto out;
539 
540 	ip6h = ipv6_hdr(skb);
541 
542 	skb_push(skb, hdr_len);
543 
544 	if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
545 		goto out;
546 	nfrags = err;
547 
548 	work_iph = ah_alloc_tmp(ahash, nfrags, hdr_len + ahp->icv_trunc_len);
549 	if (!work_iph)
550 		goto out;
551 
552 	auth_data = ah_tmp_auth(work_iph, hdr_len);
553 	icv = ah_tmp_icv(ahash, auth_data, ahp->icv_trunc_len);
554 	req = ah_tmp_req(ahash, icv);
555 	sg = ah_req_sg(ahash, req);
556 
557 	memcpy(work_iph, ip6h, hdr_len);
558 	memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
559 	memset(ah->auth_data, 0, ahp->icv_trunc_len);
560 
561 	if (ipv6_clear_mutable_options(ip6h, hdr_len, XFRM_POLICY_IN))
562 		goto out_free;
563 
564 	ip6h->priority    = 0;
565 	ip6h->flow_lbl[0] = 0;
566 	ip6h->flow_lbl[1] = 0;
567 	ip6h->flow_lbl[2] = 0;
568 	ip6h->hop_limit   = 0;
569 
570 	sg_init_table(sg, nfrags);
571 	skb_to_sgvec(skb, sg, 0, skb->len);
572 
573 	ahash_request_set_crypt(req, sg, icv, skb->len);
574 	ahash_request_set_callback(req, 0, ah6_input_done, skb);
575 
576 	AH_SKB_CB(skb)->tmp = work_iph;
577 
578 	err = crypto_ahash_digest(req);
579 	if (err) {
580 		if (err == -EINPROGRESS)
581 			goto out;
582 
583 		if (err == -EBUSY)
584 			err = NET_XMIT_DROP;
585 		goto out_free;
586 	}
587 
588 	err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0;
589 	if (err)
590 		goto out_free;
591 
592 	skb->network_header += ah_hlen;
593 	memcpy(skb_network_header(skb), work_iph, hdr_len);
594 	skb->transport_header = skb->network_header;
595 	__skb_pull(skb, ah_hlen + hdr_len);
596 
597 	err = nexthdr;
598 
599 out_free:
600 	kfree(work_iph);
601 out:
602 	return err;
603 }
604 
605 static void ah6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
606 		    u8 type, u8 code, int offset, __be32 info)
607 {
608 	struct net *net = dev_net(skb->dev);
609 	struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
610 	struct ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+offset);
611 	struct xfrm_state *x;
612 
613 	if (type != ICMPV6_DEST_UNREACH &&
614 	    type != ICMPV6_PKT_TOOBIG)
615 		return;
616 
617 	x = xfrm_state_lookup(net, (xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET6);
618 	if (!x)
619 		return;
620 
621 	NETDEBUG(KERN_DEBUG "pmtu discovery on SA AH/%08x/%pI6\n",
622 		 ntohl(ah->spi), &iph->daddr);
623 
624 	xfrm_state_put(x);
625 }
626 
627 static int ah6_init_state(struct xfrm_state *x)
628 {
629 	struct ah_data *ahp = NULL;
630 	struct xfrm_algo_desc *aalg_desc;
631 	struct crypto_ahash *ahash;
632 
633 	if (!x->aalg)
634 		goto error;
635 
636 	if (x->encap)
637 		goto error;
638 
639 	ahp = kzalloc(sizeof(*ahp), GFP_KERNEL);
640 	if (ahp == NULL)
641 		return -ENOMEM;
642 
643 	ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0);
644 	if (IS_ERR(ahash))
645 		goto error;
646 
647 	ahp->ahash = ahash;
648 	if (crypto_ahash_setkey(ahash, x->aalg->alg_key,
649 			       (x->aalg->alg_key_len + 7) / 8))
650 		goto error;
651 
652 	/*
653 	 * Lookup the algorithm description maintained by xfrm_algo,
654 	 * verify crypto transform properties, and store information
655 	 * we need for AH processing.  This lookup cannot fail here
656 	 * after a successful crypto_alloc_hash().
657 	 */
658 	aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
659 	BUG_ON(!aalg_desc);
660 
661 	if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
662 	    crypto_ahash_digestsize(ahash)) {
663 		printk(KERN_INFO "AH: %s digestsize %u != %hu\n",
664 		       x->aalg->alg_name, crypto_ahash_digestsize(ahash),
665 		       aalg_desc->uinfo.auth.icv_fullbits/8);
666 		goto error;
667 	}
668 
669 	ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
670 	ahp->icv_trunc_len = x->aalg->alg_trunc_len/8;
671 
672 	BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN);
673 
674 	x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
675 					  ahp->icv_trunc_len);
676 	switch (x->props.mode) {
677 	case XFRM_MODE_BEET:
678 	case XFRM_MODE_TRANSPORT:
679 		break;
680 	case XFRM_MODE_TUNNEL:
681 		x->props.header_len += sizeof(struct ipv6hdr);
682 		break;
683 	default:
684 		goto error;
685 	}
686 	x->data = ahp;
687 
688 	return 0;
689 
690 error:
691 	if (ahp) {
692 		crypto_free_ahash(ahp->ahash);
693 		kfree(ahp);
694 	}
695 	return -EINVAL;
696 }
697 
698 static void ah6_destroy(struct xfrm_state *x)
699 {
700 	struct ah_data *ahp = x->data;
701 
702 	if (!ahp)
703 		return;
704 
705 	crypto_free_ahash(ahp->ahash);
706 	kfree(ahp);
707 }
708 
709 static const struct xfrm_type ah6_type =
710 {
711 	.description	= "AH6",
712 	.owner		= THIS_MODULE,
713 	.proto	     	= IPPROTO_AH,
714 	.flags		= XFRM_TYPE_REPLAY_PROT,
715 	.init_state	= ah6_init_state,
716 	.destructor	= ah6_destroy,
717 	.input		= ah6_input,
718 	.output		= ah6_output,
719 	.hdr_offset	= xfrm6_find_1stfragopt,
720 };
721 
722 static const struct inet6_protocol ah6_protocol = {
723 	.handler	=	xfrm6_rcv,
724 	.err_handler	=	ah6_err,
725 	.flags		=	INET6_PROTO_NOPOLICY,
726 };
727 
728 static int __init ah6_init(void)
729 {
730 	if (xfrm_register_type(&ah6_type, AF_INET6) < 0) {
731 		printk(KERN_INFO "ipv6 ah init: can't add xfrm type\n");
732 		return -EAGAIN;
733 	}
734 
735 	if (inet6_add_protocol(&ah6_protocol, IPPROTO_AH) < 0) {
736 		printk(KERN_INFO "ipv6 ah init: can't add protocol\n");
737 		xfrm_unregister_type(&ah6_type, AF_INET6);
738 		return -EAGAIN;
739 	}
740 
741 	return 0;
742 }
743 
744 static void __exit ah6_fini(void)
745 {
746 	if (inet6_del_protocol(&ah6_protocol, IPPROTO_AH) < 0)
747 		printk(KERN_INFO "ipv6 ah close: can't remove protocol\n");
748 
749 	if (xfrm_unregister_type(&ah6_type, AF_INET6) < 0)
750 		printk(KERN_INFO "ipv6 ah close: can't remove xfrm type\n");
751 
752 }
753 
754 module_init(ah6_init);
755 module_exit(ah6_fini);
756 
757 MODULE_LICENSE("GPL");
758 MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_AH);
759