18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C)2002 USAGI/WIDE Project
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Authors
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *	Mitsuru KANDA @USAGI       : IPv6 Support
88c2ecf20Sopenharmony_ci *	Kazunori MIYAZAWA @USAGI   :
98c2ecf20Sopenharmony_ci *	Kunihiro Ishiguro <kunihiro@ipinfusion.com>
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci *	This file is derived from net/ipv4/ah.c.
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "IPv6: " fmt
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <crypto/algapi.h>
178c2ecf20Sopenharmony_ci#include <crypto/hash.h>
188c2ecf20Sopenharmony_ci#include <linux/module.h>
198c2ecf20Sopenharmony_ci#include <linux/slab.h>
208c2ecf20Sopenharmony_ci#include <net/ip.h>
218c2ecf20Sopenharmony_ci#include <net/ah.h>
228c2ecf20Sopenharmony_ci#include <linux/crypto.h>
238c2ecf20Sopenharmony_ci#include <linux/pfkeyv2.h>
248c2ecf20Sopenharmony_ci#include <linux/string.h>
258c2ecf20Sopenharmony_ci#include <linux/scatterlist.h>
268c2ecf20Sopenharmony_ci#include <net/ip6_route.h>
278c2ecf20Sopenharmony_ci#include <net/icmp.h>
288c2ecf20Sopenharmony_ci#include <net/ipv6.h>
298c2ecf20Sopenharmony_ci#include <net/protocol.h>
308c2ecf20Sopenharmony_ci#include <net/xfrm.h>
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define IPV6HDR_BASELEN 8
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistruct tmp_ext {
358c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6_MIP6)
368c2ecf20Sopenharmony_ci		struct in6_addr saddr;
378c2ecf20Sopenharmony_ci#endif
388c2ecf20Sopenharmony_ci		struct in6_addr daddr;
398c2ecf20Sopenharmony_ci		char hdrs[];
408c2ecf20Sopenharmony_ci};
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistruct ah_skb_cb {
438c2ecf20Sopenharmony_ci	struct xfrm_skb_cb xfrm;
448c2ecf20Sopenharmony_ci	void *tmp;
458c2ecf20Sopenharmony_ci};
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#define AH_SKB_CB(__skb) ((struct ah_skb_cb *)&((__skb)->cb[0]))
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic void *ah_alloc_tmp(struct crypto_ahash *ahash, int nfrags,
508c2ecf20Sopenharmony_ci			  unsigned int size)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	unsigned int len;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	len = size + crypto_ahash_digestsize(ahash) +
558c2ecf20Sopenharmony_ci	      (crypto_ahash_alignmask(ahash) &
568c2ecf20Sopenharmony_ci	       ~(crypto_tfm_ctx_alignment() - 1));
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	len = ALIGN(len, crypto_tfm_ctx_alignment());
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	len += sizeof(struct ahash_request) + crypto_ahash_reqsize(ahash);
618c2ecf20Sopenharmony_ci	len = ALIGN(len, __alignof__(struct scatterlist));
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	len += sizeof(struct scatterlist) * nfrags;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	return kmalloc(len, GFP_ATOMIC);
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic inline struct tmp_ext *ah_tmp_ext(void *base)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	return base + IPV6HDR_BASELEN;
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic inline u8 *ah_tmp_auth(u8 *tmp, unsigned int offset)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	return tmp + offset;
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic inline u8 *ah_tmp_icv(struct crypto_ahash *ahash, void *tmp,
798c2ecf20Sopenharmony_ci			     unsigned int offset)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	return PTR_ALIGN((u8 *)tmp + offset, crypto_ahash_alignmask(ahash) + 1);
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic inline struct ahash_request *ah_tmp_req(struct crypto_ahash *ahash,
858c2ecf20Sopenharmony_ci					       u8 *icv)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	struct ahash_request *req;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	req = (void *)PTR_ALIGN(icv + crypto_ahash_digestsize(ahash),
908c2ecf20Sopenharmony_ci				crypto_tfm_ctx_alignment());
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	ahash_request_set_tfm(req, ahash);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	return req;
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic inline struct scatterlist *ah_req_sg(struct crypto_ahash *ahash,
988c2ecf20Sopenharmony_ci					     struct ahash_request *req)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	return (void *)ALIGN((unsigned long)(req + 1) +
1018c2ecf20Sopenharmony_ci			     crypto_ahash_reqsize(ahash),
1028c2ecf20Sopenharmony_ci			     __alignof__(struct scatterlist));
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cistatic bool zero_out_mutable_opts(struct ipv6_opt_hdr *opthdr)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	u8 *opt = (u8 *)opthdr;
1088c2ecf20Sopenharmony_ci	int len = ipv6_optlen(opthdr);
1098c2ecf20Sopenharmony_ci	int off = 0;
1108c2ecf20Sopenharmony_ci	int optlen = 0;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	off += 2;
1138c2ecf20Sopenharmony_ci	len -= 2;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	while (len > 0) {
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci		switch (opt[off]) {
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci		case IPV6_TLV_PAD1:
1208c2ecf20Sopenharmony_ci			optlen = 1;
1218c2ecf20Sopenharmony_ci			break;
1228c2ecf20Sopenharmony_ci		default:
1238c2ecf20Sopenharmony_ci			if (len < 2)
1248c2ecf20Sopenharmony_ci				goto bad;
1258c2ecf20Sopenharmony_ci			optlen = opt[off+1]+2;
1268c2ecf20Sopenharmony_ci			if (len < optlen)
1278c2ecf20Sopenharmony_ci				goto bad;
1288c2ecf20Sopenharmony_ci			if (opt[off] & 0x20)
1298c2ecf20Sopenharmony_ci				memset(&opt[off+2], 0, opt[off+1]);
1308c2ecf20Sopenharmony_ci			break;
1318c2ecf20Sopenharmony_ci		}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci		off += optlen;
1348c2ecf20Sopenharmony_ci		len -= optlen;
1358c2ecf20Sopenharmony_ci	}
1368c2ecf20Sopenharmony_ci	if (len == 0)
1378c2ecf20Sopenharmony_ci		return true;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cibad:
1408c2ecf20Sopenharmony_ci	return false;
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6_MIP6)
1448c2ecf20Sopenharmony_ci/**
1458c2ecf20Sopenharmony_ci *	ipv6_rearrange_destopt - rearrange IPv6 destination options header
1468c2ecf20Sopenharmony_ci *	@iph: IPv6 header
1478c2ecf20Sopenharmony_ci *	@destopt: destionation options header
1488c2ecf20Sopenharmony_ci */
1498c2ecf20Sopenharmony_cistatic void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	u8 *opt = (u8 *)destopt;
1528c2ecf20Sopenharmony_ci	int len = ipv6_optlen(destopt);
1538c2ecf20Sopenharmony_ci	int off = 0;
1548c2ecf20Sopenharmony_ci	int optlen = 0;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	off += 2;
1578c2ecf20Sopenharmony_ci	len -= 2;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	while (len > 0) {
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci		switch (opt[off]) {
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci		case IPV6_TLV_PAD1:
1648c2ecf20Sopenharmony_ci			optlen = 1;
1658c2ecf20Sopenharmony_ci			break;
1668c2ecf20Sopenharmony_ci		default:
1678c2ecf20Sopenharmony_ci			if (len < 2)
1688c2ecf20Sopenharmony_ci				goto bad;
1698c2ecf20Sopenharmony_ci			optlen = opt[off+1]+2;
1708c2ecf20Sopenharmony_ci			if (len < optlen)
1718c2ecf20Sopenharmony_ci				goto bad;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci			/* Rearrange the source address in @iph and the
1748c2ecf20Sopenharmony_ci			 * addresses in home address option for final source.
1758c2ecf20Sopenharmony_ci			 * See 11.3.2 of RFC 3775 for details.
1768c2ecf20Sopenharmony_ci			 */
1778c2ecf20Sopenharmony_ci			if (opt[off] == IPV6_TLV_HAO) {
1788c2ecf20Sopenharmony_ci				struct in6_addr final_addr;
1798c2ecf20Sopenharmony_ci				struct ipv6_destopt_hao *hao;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci				hao = (struct ipv6_destopt_hao *)&opt[off];
1828c2ecf20Sopenharmony_ci				if (hao->length != sizeof(hao->addr)) {
1838c2ecf20Sopenharmony_ci					net_warn_ratelimited("destopt hao: invalid header length: %u\n",
1848c2ecf20Sopenharmony_ci							     hao->length);
1858c2ecf20Sopenharmony_ci					goto bad;
1868c2ecf20Sopenharmony_ci				}
1878c2ecf20Sopenharmony_ci				final_addr = hao->addr;
1888c2ecf20Sopenharmony_ci				hao->addr = iph->saddr;
1898c2ecf20Sopenharmony_ci				iph->saddr = final_addr;
1908c2ecf20Sopenharmony_ci			}
1918c2ecf20Sopenharmony_ci			break;
1928c2ecf20Sopenharmony_ci		}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci		off += optlen;
1958c2ecf20Sopenharmony_ci		len -= optlen;
1968c2ecf20Sopenharmony_ci	}
1978c2ecf20Sopenharmony_ci	/* Note: ok if len == 0 */
1988c2ecf20Sopenharmony_cibad:
1998c2ecf20Sopenharmony_ci	return;
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci#else
2028c2ecf20Sopenharmony_cistatic void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt) {}
2038c2ecf20Sopenharmony_ci#endif
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci/**
2068c2ecf20Sopenharmony_ci *	ipv6_rearrange_rthdr - rearrange IPv6 routing header
2078c2ecf20Sopenharmony_ci *	@iph: IPv6 header
2088c2ecf20Sopenharmony_ci *	@rthdr: routing header
2098c2ecf20Sopenharmony_ci *
2108c2ecf20Sopenharmony_ci *	Rearrange the destination address in @iph and the addresses in @rthdr
2118c2ecf20Sopenharmony_ci *	so that they appear in the order they will at the final destination.
2128c2ecf20Sopenharmony_ci *	See Appendix A2 of RFC 2402 for details.
2138c2ecf20Sopenharmony_ci */
2148c2ecf20Sopenharmony_cistatic void ipv6_rearrange_rthdr(struct ipv6hdr *iph, struct ipv6_rt_hdr *rthdr)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	int segments, segments_left;
2178c2ecf20Sopenharmony_ci	struct in6_addr *addrs;
2188c2ecf20Sopenharmony_ci	struct in6_addr final_addr;
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	segments_left = rthdr->segments_left;
2218c2ecf20Sopenharmony_ci	if (segments_left == 0)
2228c2ecf20Sopenharmony_ci		return;
2238c2ecf20Sopenharmony_ci	rthdr->segments_left = 0;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	/* The value of rthdr->hdrlen has been verified either by the system
2268c2ecf20Sopenharmony_ci	 * call if it is locally generated, or by ipv6_rthdr_rcv() for incoming
2278c2ecf20Sopenharmony_ci	 * packets.  So we can assume that it is even and that segments is
2288c2ecf20Sopenharmony_ci	 * greater than or equal to segments_left.
2298c2ecf20Sopenharmony_ci	 *
2308c2ecf20Sopenharmony_ci	 * For the same reason we can assume that this option is of type 0.
2318c2ecf20Sopenharmony_ci	 */
2328c2ecf20Sopenharmony_ci	segments = rthdr->hdrlen >> 1;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	addrs = ((struct rt0_hdr *)rthdr)->addr;
2358c2ecf20Sopenharmony_ci	final_addr = addrs[segments - 1];
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	addrs += segments - segments_left;
2388c2ecf20Sopenharmony_ci	memmove(addrs + 1, addrs, (segments_left - 1) * sizeof(*addrs));
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	addrs[0] = iph->daddr;
2418c2ecf20Sopenharmony_ci	iph->daddr = final_addr;
2428c2ecf20Sopenharmony_ci}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistatic int ipv6_clear_mutable_options(struct ipv6hdr *iph, int len, int dir)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	union {
2478c2ecf20Sopenharmony_ci		struct ipv6hdr *iph;
2488c2ecf20Sopenharmony_ci		struct ipv6_opt_hdr *opth;
2498c2ecf20Sopenharmony_ci		struct ipv6_rt_hdr *rth;
2508c2ecf20Sopenharmony_ci		char *raw;
2518c2ecf20Sopenharmony_ci	} exthdr = { .iph = iph };
2528c2ecf20Sopenharmony_ci	char *end = exthdr.raw + len;
2538c2ecf20Sopenharmony_ci	int nexthdr = iph->nexthdr;
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	exthdr.iph++;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	while (exthdr.raw < end) {
2588c2ecf20Sopenharmony_ci		switch (nexthdr) {
2598c2ecf20Sopenharmony_ci		case NEXTHDR_DEST:
2608c2ecf20Sopenharmony_ci			if (dir == XFRM_POLICY_OUT)
2618c2ecf20Sopenharmony_ci				ipv6_rearrange_destopt(iph, exthdr.opth);
2628c2ecf20Sopenharmony_ci			fallthrough;
2638c2ecf20Sopenharmony_ci		case NEXTHDR_HOP:
2648c2ecf20Sopenharmony_ci			if (!zero_out_mutable_opts(exthdr.opth)) {
2658c2ecf20Sopenharmony_ci				net_dbg_ratelimited("overrun %sopts\n",
2668c2ecf20Sopenharmony_ci						    nexthdr == NEXTHDR_HOP ?
2678c2ecf20Sopenharmony_ci						    "hop" : "dest");
2688c2ecf20Sopenharmony_ci				return -EINVAL;
2698c2ecf20Sopenharmony_ci			}
2708c2ecf20Sopenharmony_ci			break;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci		case NEXTHDR_ROUTING:
2738c2ecf20Sopenharmony_ci			ipv6_rearrange_rthdr(iph, exthdr.rth);
2748c2ecf20Sopenharmony_ci			break;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci		default:
2778c2ecf20Sopenharmony_ci			return 0;
2788c2ecf20Sopenharmony_ci		}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci		nexthdr = exthdr.opth->nexthdr;
2818c2ecf20Sopenharmony_ci		exthdr.raw += ipv6_optlen(exthdr.opth);
2828c2ecf20Sopenharmony_ci	}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	return 0;
2858c2ecf20Sopenharmony_ci}
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_cistatic void ah6_output_done(struct crypto_async_request *base, int err)
2888c2ecf20Sopenharmony_ci{
2898c2ecf20Sopenharmony_ci	int extlen;
2908c2ecf20Sopenharmony_ci	u8 *iph_base;
2918c2ecf20Sopenharmony_ci	u8 *icv;
2928c2ecf20Sopenharmony_ci	struct sk_buff *skb = base->data;
2938c2ecf20Sopenharmony_ci	struct xfrm_state *x = skb_dst(skb)->xfrm;
2948c2ecf20Sopenharmony_ci	struct ah_data *ahp = x->data;
2958c2ecf20Sopenharmony_ci	struct ipv6hdr *top_iph = ipv6_hdr(skb);
2968c2ecf20Sopenharmony_ci	struct ip_auth_hdr *ah = ip_auth_hdr(skb);
2978c2ecf20Sopenharmony_ci	struct tmp_ext *iph_ext;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	extlen = skb_network_header_len(skb) - sizeof(struct ipv6hdr);
3008c2ecf20Sopenharmony_ci	if (extlen)
3018c2ecf20Sopenharmony_ci		extlen += sizeof(*iph_ext);
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	iph_base = AH_SKB_CB(skb)->tmp;
3048c2ecf20Sopenharmony_ci	iph_ext = ah_tmp_ext(iph_base);
3058c2ecf20Sopenharmony_ci	icv = ah_tmp_icv(ahp->ahash, iph_ext, extlen);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
3088c2ecf20Sopenharmony_ci	memcpy(top_iph, iph_base, IPV6HDR_BASELEN);
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	if (extlen) {
3118c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6_MIP6)
3128c2ecf20Sopenharmony_ci		memcpy(&top_iph->saddr, iph_ext, extlen);
3138c2ecf20Sopenharmony_ci#else
3148c2ecf20Sopenharmony_ci		memcpy(&top_iph->daddr, iph_ext, extlen);
3158c2ecf20Sopenharmony_ci#endif
3168c2ecf20Sopenharmony_ci	}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	kfree(AH_SKB_CB(skb)->tmp);
3198c2ecf20Sopenharmony_ci	xfrm_output_resume(skb->sk, skb, err);
3208c2ecf20Sopenharmony_ci}
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_cistatic int ah6_output(struct xfrm_state *x, struct sk_buff *skb)
3238c2ecf20Sopenharmony_ci{
3248c2ecf20Sopenharmony_ci	int err;
3258c2ecf20Sopenharmony_ci	int nfrags;
3268c2ecf20Sopenharmony_ci	int extlen;
3278c2ecf20Sopenharmony_ci	u8 *iph_base;
3288c2ecf20Sopenharmony_ci	u8 *icv;
3298c2ecf20Sopenharmony_ci	u8 nexthdr;
3308c2ecf20Sopenharmony_ci	struct sk_buff *trailer;
3318c2ecf20Sopenharmony_ci	struct crypto_ahash *ahash;
3328c2ecf20Sopenharmony_ci	struct ahash_request *req;
3338c2ecf20Sopenharmony_ci	struct scatterlist *sg;
3348c2ecf20Sopenharmony_ci	struct ipv6hdr *top_iph;
3358c2ecf20Sopenharmony_ci	struct ip_auth_hdr *ah;
3368c2ecf20Sopenharmony_ci	struct ah_data *ahp;
3378c2ecf20Sopenharmony_ci	struct tmp_ext *iph_ext;
3388c2ecf20Sopenharmony_ci	int seqhi_len = 0;
3398c2ecf20Sopenharmony_ci	__be32 *seqhi;
3408c2ecf20Sopenharmony_ci	int sglists = 0;
3418c2ecf20Sopenharmony_ci	struct scatterlist *seqhisg;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	ahp = x->data;
3448c2ecf20Sopenharmony_ci	ahash = ahp->ahash;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	err = skb_cow_data(skb, 0, &trailer);
3478c2ecf20Sopenharmony_ci	if (err < 0)
3488c2ecf20Sopenharmony_ci		goto out;
3498c2ecf20Sopenharmony_ci	nfrags = err;
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	skb_push(skb, -skb_network_offset(skb));
3528c2ecf20Sopenharmony_ci	extlen = skb_network_header_len(skb) - sizeof(struct ipv6hdr);
3538c2ecf20Sopenharmony_ci	if (extlen)
3548c2ecf20Sopenharmony_ci		extlen += sizeof(*iph_ext);
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	if (x->props.flags & XFRM_STATE_ESN) {
3578c2ecf20Sopenharmony_ci		sglists = 1;
3588c2ecf20Sopenharmony_ci		seqhi_len = sizeof(*seqhi);
3598c2ecf20Sopenharmony_ci	}
3608c2ecf20Sopenharmony_ci	err = -ENOMEM;
3618c2ecf20Sopenharmony_ci	iph_base = ah_alloc_tmp(ahash, nfrags + sglists, IPV6HDR_BASELEN +
3628c2ecf20Sopenharmony_ci				extlen + seqhi_len);
3638c2ecf20Sopenharmony_ci	if (!iph_base)
3648c2ecf20Sopenharmony_ci		goto out;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	iph_ext = ah_tmp_ext(iph_base);
3678c2ecf20Sopenharmony_ci	seqhi = (__be32 *)((char *)iph_ext + extlen);
3688c2ecf20Sopenharmony_ci	icv = ah_tmp_icv(ahash, seqhi, seqhi_len);
3698c2ecf20Sopenharmony_ci	req = ah_tmp_req(ahash, icv);
3708c2ecf20Sopenharmony_ci	sg = ah_req_sg(ahash, req);
3718c2ecf20Sopenharmony_ci	seqhisg = sg + nfrags;
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	ah = ip_auth_hdr(skb);
3748c2ecf20Sopenharmony_ci	memset(ah->auth_data, 0, ahp->icv_trunc_len);
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	top_iph = ipv6_hdr(skb);
3778c2ecf20Sopenharmony_ci	top_iph->payload_len = htons(skb->len - sizeof(*top_iph));
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	nexthdr = *skb_mac_header(skb);
3808c2ecf20Sopenharmony_ci	*skb_mac_header(skb) = IPPROTO_AH;
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	/* When there are no extension headers, we only need to save the first
3838c2ecf20Sopenharmony_ci	 * 8 bytes of the base IP header.
3848c2ecf20Sopenharmony_ci	 */
3858c2ecf20Sopenharmony_ci	memcpy(iph_base, top_iph, IPV6HDR_BASELEN);
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	if (extlen) {
3888c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6_MIP6)
3898c2ecf20Sopenharmony_ci		memcpy(iph_ext, &top_iph->saddr, extlen);
3908c2ecf20Sopenharmony_ci#else
3918c2ecf20Sopenharmony_ci		memcpy(iph_ext, &top_iph->daddr, extlen);
3928c2ecf20Sopenharmony_ci#endif
3938c2ecf20Sopenharmony_ci		err = ipv6_clear_mutable_options(top_iph,
3948c2ecf20Sopenharmony_ci						 extlen - sizeof(*iph_ext) +
3958c2ecf20Sopenharmony_ci						 sizeof(*top_iph),
3968c2ecf20Sopenharmony_ci						 XFRM_POLICY_OUT);
3978c2ecf20Sopenharmony_ci		if (err)
3988c2ecf20Sopenharmony_ci			goto out_free;
3998c2ecf20Sopenharmony_ci	}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	ah->nexthdr = nexthdr;
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	top_iph->priority    = 0;
4048c2ecf20Sopenharmony_ci	top_iph->flow_lbl[0] = 0;
4058c2ecf20Sopenharmony_ci	top_iph->flow_lbl[1] = 0;
4068c2ecf20Sopenharmony_ci	top_iph->flow_lbl[2] = 0;
4078c2ecf20Sopenharmony_ci	top_iph->hop_limit   = 0;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	ah->hdrlen  = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	ah->reserved = 0;
4128c2ecf20Sopenharmony_ci	ah->spi = x->id.spi;
4138c2ecf20Sopenharmony_ci	ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	sg_init_table(sg, nfrags + sglists);
4168c2ecf20Sopenharmony_ci	err = skb_to_sgvec_nomark(skb, sg, 0, skb->len);
4178c2ecf20Sopenharmony_ci	if (unlikely(err < 0))
4188c2ecf20Sopenharmony_ci		goto out_free;
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	if (x->props.flags & XFRM_STATE_ESN) {
4218c2ecf20Sopenharmony_ci		/* Attach seqhi sg right after packet payload */
4228c2ecf20Sopenharmony_ci		*seqhi = htonl(XFRM_SKB_CB(skb)->seq.output.hi);
4238c2ecf20Sopenharmony_ci		sg_set_buf(seqhisg, seqhi, seqhi_len);
4248c2ecf20Sopenharmony_ci	}
4258c2ecf20Sopenharmony_ci	ahash_request_set_crypt(req, sg, icv, skb->len + seqhi_len);
4268c2ecf20Sopenharmony_ci	ahash_request_set_callback(req, 0, ah6_output_done, skb);
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	AH_SKB_CB(skb)->tmp = iph_base;
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	err = crypto_ahash_digest(req);
4318c2ecf20Sopenharmony_ci	if (err) {
4328c2ecf20Sopenharmony_ci		if (err == -EINPROGRESS)
4338c2ecf20Sopenharmony_ci			goto out;
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci		if (err == -ENOSPC)
4368c2ecf20Sopenharmony_ci			err = NET_XMIT_DROP;
4378c2ecf20Sopenharmony_ci		goto out_free;
4388c2ecf20Sopenharmony_ci	}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
4418c2ecf20Sopenharmony_ci	memcpy(top_iph, iph_base, IPV6HDR_BASELEN);
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	if (extlen) {
4448c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6_MIP6)
4458c2ecf20Sopenharmony_ci		memcpy(&top_iph->saddr, iph_ext, extlen);
4468c2ecf20Sopenharmony_ci#else
4478c2ecf20Sopenharmony_ci		memcpy(&top_iph->daddr, iph_ext, extlen);
4488c2ecf20Sopenharmony_ci#endif
4498c2ecf20Sopenharmony_ci	}
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ciout_free:
4528c2ecf20Sopenharmony_ci	kfree(iph_base);
4538c2ecf20Sopenharmony_ciout:
4548c2ecf20Sopenharmony_ci	return err;
4558c2ecf20Sopenharmony_ci}
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_cistatic void ah6_input_done(struct crypto_async_request *base, int err)
4588c2ecf20Sopenharmony_ci{
4598c2ecf20Sopenharmony_ci	u8 *auth_data;
4608c2ecf20Sopenharmony_ci	u8 *icv;
4618c2ecf20Sopenharmony_ci	u8 *work_iph;
4628c2ecf20Sopenharmony_ci	struct sk_buff *skb = base->data;
4638c2ecf20Sopenharmony_ci	struct xfrm_state *x = xfrm_input_state(skb);
4648c2ecf20Sopenharmony_ci	struct ah_data *ahp = x->data;
4658c2ecf20Sopenharmony_ci	struct ip_auth_hdr *ah = ip_auth_hdr(skb);
4668c2ecf20Sopenharmony_ci	int hdr_len = skb_network_header_len(skb);
4678c2ecf20Sopenharmony_ci	int ah_hlen = ipv6_authlen(ah);
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	if (err)
4708c2ecf20Sopenharmony_ci		goto out;
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	work_iph = AH_SKB_CB(skb)->tmp;
4738c2ecf20Sopenharmony_ci	auth_data = ah_tmp_auth(work_iph, hdr_len);
4748c2ecf20Sopenharmony_ci	icv = ah_tmp_icv(ahp->ahash, auth_data, ahp->icv_trunc_len);
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	err = crypto_memneq(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG : 0;
4778c2ecf20Sopenharmony_ci	if (err)
4788c2ecf20Sopenharmony_ci		goto out;
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	err = ah->nexthdr;
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	skb->network_header += ah_hlen;
4838c2ecf20Sopenharmony_ci	memcpy(skb_network_header(skb), work_iph, hdr_len);
4848c2ecf20Sopenharmony_ci	__skb_pull(skb, ah_hlen + hdr_len);
4858c2ecf20Sopenharmony_ci	if (x->props.mode == XFRM_MODE_TUNNEL)
4868c2ecf20Sopenharmony_ci		skb_reset_transport_header(skb);
4878c2ecf20Sopenharmony_ci	else
4888c2ecf20Sopenharmony_ci		skb_set_transport_header(skb, -hdr_len);
4898c2ecf20Sopenharmony_ciout:
4908c2ecf20Sopenharmony_ci	kfree(AH_SKB_CB(skb)->tmp);
4918c2ecf20Sopenharmony_ci	xfrm_input_resume(skb, err);
4928c2ecf20Sopenharmony_ci}
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_cistatic int ah6_input(struct xfrm_state *x, struct sk_buff *skb)
4978c2ecf20Sopenharmony_ci{
4988c2ecf20Sopenharmony_ci	/*
4998c2ecf20Sopenharmony_ci	 * Before process AH
5008c2ecf20Sopenharmony_ci	 * [IPv6][Ext1][Ext2][AH][Dest][Payload]
5018c2ecf20Sopenharmony_ci	 * |<-------------->| hdr_len
5028c2ecf20Sopenharmony_ci	 *
5038c2ecf20Sopenharmony_ci	 * To erase AH:
5048c2ecf20Sopenharmony_ci	 * Keeping copy of cleared headers. After AH processing,
5058c2ecf20Sopenharmony_ci	 * Moving the pointer of skb->network_header by using skb_pull as long
5068c2ecf20Sopenharmony_ci	 * as AH header length. Then copy back the copy as long as hdr_len
5078c2ecf20Sopenharmony_ci	 * If destination header following AH exists, copy it into after [Ext2].
5088c2ecf20Sopenharmony_ci	 *
5098c2ecf20Sopenharmony_ci	 * |<>|[IPv6][Ext1][Ext2][Dest][Payload]
5108c2ecf20Sopenharmony_ci	 * There is offset of AH before IPv6 header after the process.
5118c2ecf20Sopenharmony_ci	 */
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	u8 *auth_data;
5148c2ecf20Sopenharmony_ci	u8 *icv;
5158c2ecf20Sopenharmony_ci	u8 *work_iph;
5168c2ecf20Sopenharmony_ci	struct sk_buff *trailer;
5178c2ecf20Sopenharmony_ci	struct crypto_ahash *ahash;
5188c2ecf20Sopenharmony_ci	struct ahash_request *req;
5198c2ecf20Sopenharmony_ci	struct scatterlist *sg;
5208c2ecf20Sopenharmony_ci	struct ip_auth_hdr *ah;
5218c2ecf20Sopenharmony_ci	struct ipv6hdr *ip6h;
5228c2ecf20Sopenharmony_ci	struct ah_data *ahp;
5238c2ecf20Sopenharmony_ci	u16 hdr_len;
5248c2ecf20Sopenharmony_ci	u16 ah_hlen;
5258c2ecf20Sopenharmony_ci	int nexthdr;
5268c2ecf20Sopenharmony_ci	int nfrags;
5278c2ecf20Sopenharmony_ci	int err = -ENOMEM;
5288c2ecf20Sopenharmony_ci	int seqhi_len = 0;
5298c2ecf20Sopenharmony_ci	__be32 *seqhi;
5308c2ecf20Sopenharmony_ci	int sglists = 0;
5318c2ecf20Sopenharmony_ci	struct scatterlist *seqhisg;
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	if (!pskb_may_pull(skb, sizeof(struct ip_auth_hdr)))
5348c2ecf20Sopenharmony_ci		goto out;
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	/* We are going to _remove_ AH header to keep sockets happy,
5378c2ecf20Sopenharmony_ci	 * so... Later this can change. */
5388c2ecf20Sopenharmony_ci	if (skb_unclone(skb, GFP_ATOMIC))
5398c2ecf20Sopenharmony_ci		goto out;
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	skb->ip_summed = CHECKSUM_NONE;
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	hdr_len = skb_network_header_len(skb);
5448c2ecf20Sopenharmony_ci	ah = (struct ip_auth_hdr *)skb->data;
5458c2ecf20Sopenharmony_ci	ahp = x->data;
5468c2ecf20Sopenharmony_ci	ahash = ahp->ahash;
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	nexthdr = ah->nexthdr;
5498c2ecf20Sopenharmony_ci	ah_hlen = ipv6_authlen(ah);
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) &&
5528c2ecf20Sopenharmony_ci	    ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len))
5538c2ecf20Sopenharmony_ci		goto out;
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	if (!pskb_may_pull(skb, ah_hlen))
5568c2ecf20Sopenharmony_ci		goto out;
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	err = skb_cow_data(skb, 0, &trailer);
5598c2ecf20Sopenharmony_ci	if (err < 0)
5608c2ecf20Sopenharmony_ci		goto out;
5618c2ecf20Sopenharmony_ci	nfrags = err;
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	ah = (struct ip_auth_hdr *)skb->data;
5648c2ecf20Sopenharmony_ci	ip6h = ipv6_hdr(skb);
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	skb_push(skb, hdr_len);
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	if (x->props.flags & XFRM_STATE_ESN) {
5698c2ecf20Sopenharmony_ci		sglists = 1;
5708c2ecf20Sopenharmony_ci		seqhi_len = sizeof(*seqhi);
5718c2ecf20Sopenharmony_ci	}
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	work_iph = ah_alloc_tmp(ahash, nfrags + sglists, hdr_len +
5748c2ecf20Sopenharmony_ci				ahp->icv_trunc_len + seqhi_len);
5758c2ecf20Sopenharmony_ci	if (!work_iph) {
5768c2ecf20Sopenharmony_ci		err = -ENOMEM;
5778c2ecf20Sopenharmony_ci		goto out;
5788c2ecf20Sopenharmony_ci	}
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	auth_data = ah_tmp_auth((u8 *)work_iph, hdr_len);
5818c2ecf20Sopenharmony_ci	seqhi = (__be32 *)(auth_data + ahp->icv_trunc_len);
5828c2ecf20Sopenharmony_ci	icv = ah_tmp_icv(ahash, seqhi, seqhi_len);
5838c2ecf20Sopenharmony_ci	req = ah_tmp_req(ahash, icv);
5848c2ecf20Sopenharmony_ci	sg = ah_req_sg(ahash, req);
5858c2ecf20Sopenharmony_ci	seqhisg = sg + nfrags;
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	memcpy(work_iph, ip6h, hdr_len);
5888c2ecf20Sopenharmony_ci	memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
5898c2ecf20Sopenharmony_ci	memset(ah->auth_data, 0, ahp->icv_trunc_len);
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	err = ipv6_clear_mutable_options(ip6h, hdr_len, XFRM_POLICY_IN);
5928c2ecf20Sopenharmony_ci	if (err)
5938c2ecf20Sopenharmony_ci		goto out_free;
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	ip6h->priority    = 0;
5968c2ecf20Sopenharmony_ci	ip6h->flow_lbl[0] = 0;
5978c2ecf20Sopenharmony_ci	ip6h->flow_lbl[1] = 0;
5988c2ecf20Sopenharmony_ci	ip6h->flow_lbl[2] = 0;
5998c2ecf20Sopenharmony_ci	ip6h->hop_limit   = 0;
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci	sg_init_table(sg, nfrags + sglists);
6028c2ecf20Sopenharmony_ci	err = skb_to_sgvec_nomark(skb, sg, 0, skb->len);
6038c2ecf20Sopenharmony_ci	if (unlikely(err < 0))
6048c2ecf20Sopenharmony_ci		goto out_free;
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	if (x->props.flags & XFRM_STATE_ESN) {
6078c2ecf20Sopenharmony_ci		/* Attach seqhi sg right after packet payload */
6088c2ecf20Sopenharmony_ci		*seqhi = XFRM_SKB_CB(skb)->seq.input.hi;
6098c2ecf20Sopenharmony_ci		sg_set_buf(seqhisg, seqhi, seqhi_len);
6108c2ecf20Sopenharmony_ci	}
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	ahash_request_set_crypt(req, sg, icv, skb->len + seqhi_len);
6138c2ecf20Sopenharmony_ci	ahash_request_set_callback(req, 0, ah6_input_done, skb);
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	AH_SKB_CB(skb)->tmp = work_iph;
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	err = crypto_ahash_digest(req);
6188c2ecf20Sopenharmony_ci	if (err) {
6198c2ecf20Sopenharmony_ci		if (err == -EINPROGRESS)
6208c2ecf20Sopenharmony_ci			goto out;
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci		goto out_free;
6238c2ecf20Sopenharmony_ci	}
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	err = crypto_memneq(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG : 0;
6268c2ecf20Sopenharmony_ci	if (err)
6278c2ecf20Sopenharmony_ci		goto out_free;
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	skb->network_header += ah_hlen;
6308c2ecf20Sopenharmony_ci	memcpy(skb_network_header(skb), work_iph, hdr_len);
6318c2ecf20Sopenharmony_ci	__skb_pull(skb, ah_hlen + hdr_len);
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	if (x->props.mode == XFRM_MODE_TUNNEL)
6348c2ecf20Sopenharmony_ci		skb_reset_transport_header(skb);
6358c2ecf20Sopenharmony_ci	else
6368c2ecf20Sopenharmony_ci		skb_set_transport_header(skb, -hdr_len);
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	err = nexthdr;
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ciout_free:
6418c2ecf20Sopenharmony_ci	kfree(work_iph);
6428c2ecf20Sopenharmony_ciout:
6438c2ecf20Sopenharmony_ci	return err;
6448c2ecf20Sopenharmony_ci}
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_cistatic int ah6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
6478c2ecf20Sopenharmony_ci		   u8 type, u8 code, int offset, __be32 info)
6488c2ecf20Sopenharmony_ci{
6498c2ecf20Sopenharmony_ci	struct net *net = dev_net(skb->dev);
6508c2ecf20Sopenharmony_ci	struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
6518c2ecf20Sopenharmony_ci	struct ip_auth_hdr *ah = (struct ip_auth_hdr *)(skb->data+offset);
6528c2ecf20Sopenharmony_ci	struct xfrm_state *x;
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	if (type != ICMPV6_PKT_TOOBIG &&
6558c2ecf20Sopenharmony_ci	    type != NDISC_REDIRECT)
6568c2ecf20Sopenharmony_ci		return 0;
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci	x = xfrm_state_lookup(net, skb->mark, (xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET6);
6598c2ecf20Sopenharmony_ci	if (!x)
6608c2ecf20Sopenharmony_ci		return 0;
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	if (type == NDISC_REDIRECT)
6638c2ecf20Sopenharmony_ci		ip6_redirect(skb, net, skb->dev->ifindex, 0,
6648c2ecf20Sopenharmony_ci			     sock_net_uid(net, NULL));
6658c2ecf20Sopenharmony_ci	else
6668c2ecf20Sopenharmony_ci		ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
6678c2ecf20Sopenharmony_ci	xfrm_state_put(x);
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	return 0;
6708c2ecf20Sopenharmony_ci}
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_cistatic int ah6_init_state(struct xfrm_state *x)
6738c2ecf20Sopenharmony_ci{
6748c2ecf20Sopenharmony_ci	struct ah_data *ahp = NULL;
6758c2ecf20Sopenharmony_ci	struct xfrm_algo_desc *aalg_desc;
6768c2ecf20Sopenharmony_ci	struct crypto_ahash *ahash;
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	if (!x->aalg)
6798c2ecf20Sopenharmony_ci		goto error;
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	if (x->encap)
6828c2ecf20Sopenharmony_ci		goto error;
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	ahp = kzalloc(sizeof(*ahp), GFP_KERNEL);
6858c2ecf20Sopenharmony_ci	if (!ahp)
6868c2ecf20Sopenharmony_ci		return -ENOMEM;
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0);
6898c2ecf20Sopenharmony_ci	if (IS_ERR(ahash))
6908c2ecf20Sopenharmony_ci		goto error;
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci	ahp->ahash = ahash;
6938c2ecf20Sopenharmony_ci	if (crypto_ahash_setkey(ahash, x->aalg->alg_key,
6948c2ecf20Sopenharmony_ci			       (x->aalg->alg_key_len + 7) / 8))
6958c2ecf20Sopenharmony_ci		goto error;
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	/*
6988c2ecf20Sopenharmony_ci	 * Lookup the algorithm description maintained by xfrm_algo,
6998c2ecf20Sopenharmony_ci	 * verify crypto transform properties, and store information
7008c2ecf20Sopenharmony_ci	 * we need for AH processing.  This lookup cannot fail here
7018c2ecf20Sopenharmony_ci	 * after a successful crypto_alloc_hash().
7028c2ecf20Sopenharmony_ci	 */
7038c2ecf20Sopenharmony_ci	aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
7048c2ecf20Sopenharmony_ci	BUG_ON(!aalg_desc);
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
7078c2ecf20Sopenharmony_ci	    crypto_ahash_digestsize(ahash)) {
7088c2ecf20Sopenharmony_ci		pr_info("AH: %s digestsize %u != %hu\n",
7098c2ecf20Sopenharmony_ci			x->aalg->alg_name, crypto_ahash_digestsize(ahash),
7108c2ecf20Sopenharmony_ci			aalg_desc->uinfo.auth.icv_fullbits/8);
7118c2ecf20Sopenharmony_ci		goto error;
7128c2ecf20Sopenharmony_ci	}
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
7158c2ecf20Sopenharmony_ci	ahp->icv_trunc_len = x->aalg->alg_trunc_len/8;
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
7188c2ecf20Sopenharmony_ci					  ahp->icv_trunc_len);
7198c2ecf20Sopenharmony_ci	switch (x->props.mode) {
7208c2ecf20Sopenharmony_ci	case XFRM_MODE_BEET:
7218c2ecf20Sopenharmony_ci	case XFRM_MODE_TRANSPORT:
7228c2ecf20Sopenharmony_ci		break;
7238c2ecf20Sopenharmony_ci	case XFRM_MODE_TUNNEL:
7248c2ecf20Sopenharmony_ci		x->props.header_len += sizeof(struct ipv6hdr);
7258c2ecf20Sopenharmony_ci		break;
7268c2ecf20Sopenharmony_ci	default:
7278c2ecf20Sopenharmony_ci		goto error;
7288c2ecf20Sopenharmony_ci	}
7298c2ecf20Sopenharmony_ci	x->data = ahp;
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	return 0;
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_cierror:
7348c2ecf20Sopenharmony_ci	if (ahp) {
7358c2ecf20Sopenharmony_ci		crypto_free_ahash(ahp->ahash);
7368c2ecf20Sopenharmony_ci		kfree(ahp);
7378c2ecf20Sopenharmony_ci	}
7388c2ecf20Sopenharmony_ci	return -EINVAL;
7398c2ecf20Sopenharmony_ci}
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_cistatic void ah6_destroy(struct xfrm_state *x)
7428c2ecf20Sopenharmony_ci{
7438c2ecf20Sopenharmony_ci	struct ah_data *ahp = x->data;
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci	if (!ahp)
7468c2ecf20Sopenharmony_ci		return;
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci	crypto_free_ahash(ahp->ahash);
7498c2ecf20Sopenharmony_ci	kfree(ahp);
7508c2ecf20Sopenharmony_ci}
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_cistatic int ah6_rcv_cb(struct sk_buff *skb, int err)
7538c2ecf20Sopenharmony_ci{
7548c2ecf20Sopenharmony_ci	return 0;
7558c2ecf20Sopenharmony_ci}
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_cistatic const struct xfrm_type ah6_type = {
7588c2ecf20Sopenharmony_ci	.description	= "AH6",
7598c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
7608c2ecf20Sopenharmony_ci	.proto		= IPPROTO_AH,
7618c2ecf20Sopenharmony_ci	.flags		= XFRM_TYPE_REPLAY_PROT,
7628c2ecf20Sopenharmony_ci	.init_state	= ah6_init_state,
7638c2ecf20Sopenharmony_ci	.destructor	= ah6_destroy,
7648c2ecf20Sopenharmony_ci	.input		= ah6_input,
7658c2ecf20Sopenharmony_ci	.output		= ah6_output,
7668c2ecf20Sopenharmony_ci	.hdr_offset	= xfrm6_find_1stfragopt,
7678c2ecf20Sopenharmony_ci};
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_cistatic struct xfrm6_protocol ah6_protocol = {
7708c2ecf20Sopenharmony_ci	.handler	=	xfrm6_rcv,
7718c2ecf20Sopenharmony_ci	.input_handler	=	xfrm_input,
7728c2ecf20Sopenharmony_ci	.cb_handler	=	ah6_rcv_cb,
7738c2ecf20Sopenharmony_ci	.err_handler	=	ah6_err,
7748c2ecf20Sopenharmony_ci	.priority	=	0,
7758c2ecf20Sopenharmony_ci};
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_cistatic int __init ah6_init(void)
7788c2ecf20Sopenharmony_ci{
7798c2ecf20Sopenharmony_ci	if (xfrm_register_type(&ah6_type, AF_INET6) < 0) {
7808c2ecf20Sopenharmony_ci		pr_info("%s: can't add xfrm type\n", __func__);
7818c2ecf20Sopenharmony_ci		return -EAGAIN;
7828c2ecf20Sopenharmony_ci	}
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci	if (xfrm6_protocol_register(&ah6_protocol, IPPROTO_AH) < 0) {
7858c2ecf20Sopenharmony_ci		pr_info("%s: can't add protocol\n", __func__);
7868c2ecf20Sopenharmony_ci		xfrm_unregister_type(&ah6_type, AF_INET6);
7878c2ecf20Sopenharmony_ci		return -EAGAIN;
7888c2ecf20Sopenharmony_ci	}
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci	return 0;
7918c2ecf20Sopenharmony_ci}
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_cistatic void __exit ah6_fini(void)
7948c2ecf20Sopenharmony_ci{
7958c2ecf20Sopenharmony_ci	if (xfrm6_protocol_deregister(&ah6_protocol, IPPROTO_AH) < 0)
7968c2ecf20Sopenharmony_ci		pr_info("%s: can't remove protocol\n", __func__);
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	xfrm_unregister_type(&ah6_type, AF_INET6);
7998c2ecf20Sopenharmony_ci}
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_cimodule_init(ah6_init);
8028c2ecf20Sopenharmony_cimodule_exit(ah6_fini);
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
8058c2ecf20Sopenharmony_ciMODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_AH);
806