xref: /openbmc/linux/net/ipv4/ah4.c (revision 5a0e3ad6)
1dff3bb06SSteffen Klassert #include <crypto/hash.h>
207d4ee58SHerbert Xu #include <linux/err.h>
31da177e4SLinus Torvalds #include <linux/module.h>
45a0e3ad6STejun Heo #include <linux/slab.h>
51da177e4SLinus Torvalds #include <net/ip.h>
61da177e4SLinus Torvalds #include <net/xfrm.h>
71da177e4SLinus Torvalds #include <net/ah.h>
81da177e4SLinus Torvalds #include <linux/crypto.h>
91da177e4SLinus Torvalds #include <linux/pfkeyv2.h>
10dff3bb06SSteffen Klassert #include <linux/scatterlist.h>
111da177e4SLinus Torvalds #include <net/icmp.h>
1214c85021SArnaldo Carvalho de Melo #include <net/protocol.h>
131da177e4SLinus Torvalds 
14dff3bb06SSteffen Klassert struct ah_skb_cb {
15dff3bb06SSteffen Klassert 	struct xfrm_skb_cb xfrm;
16dff3bb06SSteffen Klassert 	void *tmp;
17dff3bb06SSteffen Klassert };
18dff3bb06SSteffen Klassert 
19dff3bb06SSteffen Klassert #define AH_SKB_CB(__skb) ((struct ah_skb_cb *)&((__skb)->cb[0]))
20dff3bb06SSteffen Klassert 
21dff3bb06SSteffen Klassert static void *ah_alloc_tmp(struct crypto_ahash *ahash, int nfrags,
22dff3bb06SSteffen Klassert 			  unsigned int size)
23dff3bb06SSteffen Klassert {
24dff3bb06SSteffen Klassert 	unsigned int len;
25dff3bb06SSteffen Klassert 
26dff3bb06SSteffen Klassert 	len = size + crypto_ahash_digestsize(ahash) +
27dff3bb06SSteffen Klassert 	      (crypto_ahash_alignmask(ahash) &
28dff3bb06SSteffen Klassert 	       ~(crypto_tfm_ctx_alignment() - 1));
29dff3bb06SSteffen Klassert 
30dff3bb06SSteffen Klassert 	len = ALIGN(len, crypto_tfm_ctx_alignment());
31dff3bb06SSteffen Klassert 
32dff3bb06SSteffen Klassert 	len += sizeof(struct ahash_request) + crypto_ahash_reqsize(ahash);
33dff3bb06SSteffen Klassert 	len = ALIGN(len, __alignof__(struct scatterlist));
34dff3bb06SSteffen Klassert 
35dff3bb06SSteffen Klassert 	len += sizeof(struct scatterlist) * nfrags;
36dff3bb06SSteffen Klassert 
37dff3bb06SSteffen Klassert 	return kmalloc(len, GFP_ATOMIC);
38dff3bb06SSteffen Klassert }
39dff3bb06SSteffen Klassert 
40dff3bb06SSteffen Klassert static inline u8 *ah_tmp_auth(void *tmp, unsigned int offset)
41dff3bb06SSteffen Klassert {
42dff3bb06SSteffen Klassert 	return tmp + offset;
43dff3bb06SSteffen Klassert }
44dff3bb06SSteffen Klassert 
45dff3bb06SSteffen Klassert static inline u8 *ah_tmp_icv(struct crypto_ahash *ahash, void *tmp,
46dff3bb06SSteffen Klassert 			     unsigned int offset)
47dff3bb06SSteffen Klassert {
48dff3bb06SSteffen Klassert 	return PTR_ALIGN((u8 *)tmp + offset, crypto_ahash_alignmask(ahash) + 1);
49dff3bb06SSteffen Klassert }
50dff3bb06SSteffen Klassert 
51dff3bb06SSteffen Klassert static inline struct ahash_request *ah_tmp_req(struct crypto_ahash *ahash,
52dff3bb06SSteffen Klassert 					       u8 *icv)
53dff3bb06SSteffen Klassert {
54dff3bb06SSteffen Klassert 	struct ahash_request *req;
55dff3bb06SSteffen Klassert 
56dff3bb06SSteffen Klassert 	req = (void *)PTR_ALIGN(icv + crypto_ahash_digestsize(ahash),
57dff3bb06SSteffen Klassert 				crypto_tfm_ctx_alignment());
58dff3bb06SSteffen Klassert 
59dff3bb06SSteffen Klassert 	ahash_request_set_tfm(req, ahash);
60dff3bb06SSteffen Klassert 
61dff3bb06SSteffen Klassert 	return req;
62dff3bb06SSteffen Klassert }
63dff3bb06SSteffen Klassert 
64dff3bb06SSteffen Klassert static inline struct scatterlist *ah_req_sg(struct crypto_ahash *ahash,
65dff3bb06SSteffen Klassert 					     struct ahash_request *req)
66dff3bb06SSteffen Klassert {
67dff3bb06SSteffen Klassert 	return (void *)ALIGN((unsigned long)(req + 1) +
68dff3bb06SSteffen Klassert 			     crypto_ahash_reqsize(ahash),
69dff3bb06SSteffen Klassert 			     __alignof__(struct scatterlist));
70dff3bb06SSteffen Klassert }
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds /* Clear mutable options and find final destination to substitute
731da177e4SLinus Torvalds  * into IP header for icv calculation. Options are already checked
741da177e4SLinus Torvalds  * for validity, so paranoia is not required. */
751da177e4SLinus Torvalds 
76d5a0a1e3SAl Viro static int ip_clear_mutable_options(struct iphdr *iph, __be32 *daddr)
771da177e4SLinus Torvalds {
781da177e4SLinus Torvalds 	unsigned char * optptr = (unsigned char*)(iph+1);
791da177e4SLinus Torvalds 	int  l = iph->ihl*4 - sizeof(struct iphdr);
801da177e4SLinus Torvalds 	int  optlen;
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	while (l > 0) {
831da177e4SLinus Torvalds 		switch (*optptr) {
841da177e4SLinus Torvalds 		case IPOPT_END:
851da177e4SLinus Torvalds 			return 0;
861da177e4SLinus Torvalds 		case IPOPT_NOOP:
871da177e4SLinus Torvalds 			l--;
881da177e4SLinus Torvalds 			optptr++;
891da177e4SLinus Torvalds 			continue;
901da177e4SLinus Torvalds 		}
911da177e4SLinus Torvalds 		optlen = optptr[1];
921da177e4SLinus Torvalds 		if (optlen<2 || optlen>l)
931da177e4SLinus Torvalds 			return -EINVAL;
941da177e4SLinus Torvalds 		switch (*optptr) {
951da177e4SLinus Torvalds 		case IPOPT_SEC:
961da177e4SLinus Torvalds 		case 0x85:	/* Some "Extended Security" crap. */
9711a03f78SPaul Moore 		case IPOPT_CIPSO:
981da177e4SLinus Torvalds 		case IPOPT_RA:
991da177e4SLinus Torvalds 		case 0x80|21:	/* RFC1770 */
1001da177e4SLinus Torvalds 			break;
1011da177e4SLinus Torvalds 		case IPOPT_LSRR:
1021da177e4SLinus Torvalds 		case IPOPT_SSRR:
1031da177e4SLinus Torvalds 			if (optlen < 6)
1041da177e4SLinus Torvalds 				return -EINVAL;
1051da177e4SLinus Torvalds 			memcpy(daddr, optptr+optlen-4, 4);
1061da177e4SLinus Torvalds 			/* Fall through */
1071da177e4SLinus Torvalds 		default:
10896fe1c02SNick Bowler 			memset(optptr, 0, optlen);
1091da177e4SLinus Torvalds 		}
1101da177e4SLinus Torvalds 		l -= optlen;
1111da177e4SLinus Torvalds 		optptr += optlen;
1121da177e4SLinus Torvalds 	}
1131da177e4SLinus Torvalds 	return 0;
1141da177e4SLinus Torvalds }
1151da177e4SLinus Torvalds 
116dff3bb06SSteffen Klassert static void ah_output_done(struct crypto_async_request *base, int err)
117dff3bb06SSteffen Klassert {
118dff3bb06SSteffen Klassert 	u8 *icv;
119dff3bb06SSteffen Klassert 	struct iphdr *iph;
120dff3bb06SSteffen Klassert 	struct sk_buff *skb = base->data;
121dff3bb06SSteffen Klassert 	struct xfrm_state *x = skb_dst(skb)->xfrm;
122dff3bb06SSteffen Klassert 	struct ah_data *ahp = x->data;
123dff3bb06SSteffen Klassert 	struct iphdr *top_iph = ip_hdr(skb);
124dff3bb06SSteffen Klassert 	struct ip_auth_hdr *ah = ip_auth_hdr(skb);
125dff3bb06SSteffen Klassert 	int ihl = ip_hdrlen(skb);
126dff3bb06SSteffen Klassert 
127dff3bb06SSteffen Klassert 	iph = AH_SKB_CB(skb)->tmp;
128dff3bb06SSteffen Klassert 	icv = ah_tmp_icv(ahp->ahash, iph, ihl);
129dff3bb06SSteffen Klassert 	memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
130dff3bb06SSteffen Klassert 
131dff3bb06SSteffen Klassert 	top_iph->tos = iph->tos;
132dff3bb06SSteffen Klassert 	top_iph->ttl = iph->ttl;
133dff3bb06SSteffen Klassert 	top_iph->frag_off = iph->frag_off;
134dff3bb06SSteffen Klassert 	if (top_iph->ihl != 5) {
135dff3bb06SSteffen Klassert 		top_iph->daddr = iph->daddr;
136dff3bb06SSteffen Klassert 		memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
137dff3bb06SSteffen Klassert 	}
138dff3bb06SSteffen Klassert 
139dff3bb06SSteffen Klassert 	err = ah->nexthdr;
140dff3bb06SSteffen Klassert 
141dff3bb06SSteffen Klassert 	kfree(AH_SKB_CB(skb)->tmp);
142dff3bb06SSteffen Klassert 	xfrm_output_resume(skb, err);
143dff3bb06SSteffen Klassert }
144dff3bb06SSteffen Klassert 
1451da177e4SLinus Torvalds static int ah_output(struct xfrm_state *x, struct sk_buff *skb)
1461da177e4SLinus Torvalds {
1471da177e4SLinus Torvalds 	int err;
148dff3bb06SSteffen Klassert 	int nfrags;
149dff3bb06SSteffen Klassert 	int ihl;
150dff3bb06SSteffen Klassert 	u8 *icv;
151dff3bb06SSteffen Klassert 	struct sk_buff *trailer;
152dff3bb06SSteffen Klassert 	struct crypto_ahash *ahash;
153dff3bb06SSteffen Klassert 	struct ahash_request *req;
154dff3bb06SSteffen Klassert 	struct scatterlist *sg;
1551da177e4SLinus Torvalds 	struct iphdr *iph, *top_iph;
1561da177e4SLinus Torvalds 	struct ip_auth_hdr *ah;
1571da177e4SLinus Torvalds 	struct ah_data *ahp;
158dff3bb06SSteffen Klassert 
159dff3bb06SSteffen Klassert 	ahp = x->data;
160dff3bb06SSteffen Klassert 	ahash = ahp->ahash;
161dff3bb06SSteffen Klassert 
162dff3bb06SSteffen Klassert 	if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
163dff3bb06SSteffen Klassert 		goto out;
164dff3bb06SSteffen Klassert 	nfrags = err;
1651da177e4SLinus Torvalds 
1667b277b1aSHerbert Xu 	skb_push(skb, -skb_network_offset(skb));
167dff3bb06SSteffen Klassert 	ah = ip_auth_hdr(skb);
168dff3bb06SSteffen Klassert 	ihl = ip_hdrlen(skb);
169dff3bb06SSteffen Klassert 
170dff3bb06SSteffen Klassert 	err = -ENOMEM;
171dff3bb06SSteffen Klassert 	iph = ah_alloc_tmp(ahash, nfrags, ihl);
172dff3bb06SSteffen Klassert 	if (!iph)
173dff3bb06SSteffen Klassert 		goto out;
174dff3bb06SSteffen Klassert 
175dff3bb06SSteffen Klassert 	icv = ah_tmp_icv(ahash, iph, ihl);
176dff3bb06SSteffen Klassert 	req = ah_tmp_req(ahash, icv);
177dff3bb06SSteffen Klassert 	sg = ah_req_sg(ahash, req);
178dff3bb06SSteffen Klassert 
179dff3bb06SSteffen Klassert 	memset(ah->auth_data, 0, ahp->icv_trunc_len);
180dff3bb06SSteffen Klassert 
181eddc9ec5SArnaldo Carvalho de Melo 	top_iph = ip_hdr(skb);
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	iph->tos = top_iph->tos;
1841da177e4SLinus Torvalds 	iph->ttl = top_iph->ttl;
1851da177e4SLinus Torvalds 	iph->frag_off = top_iph->frag_off;
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	if (top_iph->ihl != 5) {
1881da177e4SLinus Torvalds 		iph->daddr = top_iph->daddr;
1891da177e4SLinus Torvalds 		memcpy(iph+1, top_iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
1901da177e4SLinus Torvalds 		err = ip_clear_mutable_options(top_iph, &top_iph->daddr);
1911da177e4SLinus Torvalds 		if (err)
192dff3bb06SSteffen Klassert 			goto out_free;
1931da177e4SLinus Torvalds 	}
1941da177e4SLinus Torvalds 
19537fedd3aSHerbert Xu 	ah->nexthdr = *skb_mac_header(skb);
19637fedd3aSHerbert Xu 	*skb_mac_header(skb) = IPPROTO_AH;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	top_iph->tos = 0;
1991da177e4SLinus Torvalds 	top_iph->tot_len = htons(skb->len);
2001da177e4SLinus Torvalds 	top_iph->frag_off = 0;
2011da177e4SLinus Torvalds 	top_iph->ttl = 0;
2021da177e4SLinus Torvalds 	top_iph->check = 0;
2031da177e4SLinus Torvalds 
20487bdc48dSHerbert Xu 	ah->hdrlen  = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2;
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	ah->reserved = 0;
2071da177e4SLinus Torvalds 	ah->spi = x->id.spi;
208b318e0e4SHerbert Xu 	ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output);
209b7c6538cSHerbert Xu 
210dff3bb06SSteffen Klassert 	sg_init_table(sg, nfrags);
211dff3bb06SSteffen Klassert 	skb_to_sgvec(skb, sg, 0, skb->len);
212b7c6538cSHerbert Xu 
213dff3bb06SSteffen Klassert 	ahash_request_set_crypt(req, sg, icv, skb->len);
214dff3bb06SSteffen Klassert 	ahash_request_set_callback(req, 0, ah_output_done, skb);
215dff3bb06SSteffen Klassert 
216dff3bb06SSteffen Klassert 	AH_SKB_CB(skb)->tmp = iph;
217dff3bb06SSteffen Klassert 
218dff3bb06SSteffen Klassert 	err = crypto_ahash_digest(req);
219dff3bb06SSteffen Klassert 	if (err) {
220dff3bb06SSteffen Klassert 		if (err == -EINPROGRESS)
221dff3bb06SSteffen Klassert 			goto out;
222dff3bb06SSteffen Klassert 
223dff3bb06SSteffen Klassert 		if (err == -EBUSY)
224dff3bb06SSteffen Klassert 			err = NET_XMIT_DROP;
225dff3bb06SSteffen Klassert 		goto out_free;
226dff3bb06SSteffen Klassert 	}
227dff3bb06SSteffen Klassert 
228dff3bb06SSteffen Klassert 	memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	top_iph->tos = iph->tos;
2311da177e4SLinus Torvalds 	top_iph->ttl = iph->ttl;
2321da177e4SLinus Torvalds 	top_iph->frag_off = iph->frag_off;
2331da177e4SLinus Torvalds 	if (top_iph->ihl != 5) {
2341da177e4SLinus Torvalds 		top_iph->daddr = iph->daddr;
2351da177e4SLinus Torvalds 		memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
2361da177e4SLinus Torvalds 	}
2371da177e4SLinus Torvalds 
238dff3bb06SSteffen Klassert out_free:
239dff3bb06SSteffen Klassert 	kfree(iph);
240dff3bb06SSteffen Klassert out:
2411da177e4SLinus Torvalds 	return err;
2421da177e4SLinus Torvalds }
2431da177e4SLinus Torvalds 
244dff3bb06SSteffen Klassert static void ah_input_done(struct crypto_async_request *base, int err)
245dff3bb06SSteffen Klassert {
246dff3bb06SSteffen Klassert 	u8 *auth_data;
247dff3bb06SSteffen Klassert 	u8 *icv;
248dff3bb06SSteffen Klassert 	struct iphdr *work_iph;
249dff3bb06SSteffen Klassert 	struct sk_buff *skb = base->data;
250dff3bb06SSteffen Klassert 	struct xfrm_state *x = xfrm_input_state(skb);
251dff3bb06SSteffen Klassert 	struct ah_data *ahp = x->data;
252dff3bb06SSteffen Klassert 	struct ip_auth_hdr *ah = ip_auth_hdr(skb);
253dff3bb06SSteffen Klassert 	int ihl = ip_hdrlen(skb);
254dff3bb06SSteffen Klassert 	int ah_hlen = (ah->hdrlen + 2) << 2;
255dff3bb06SSteffen Klassert 
256dff3bb06SSteffen Klassert 	work_iph = AH_SKB_CB(skb)->tmp;
257dff3bb06SSteffen Klassert 	auth_data = ah_tmp_auth(work_iph, ihl);
258dff3bb06SSteffen Klassert 	icv = ah_tmp_icv(ahp->ahash, auth_data, ahp->icv_trunc_len);
259dff3bb06SSteffen Klassert 
260dff3bb06SSteffen Klassert 	err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0;
261dff3bb06SSteffen Klassert 	if (err)
262dff3bb06SSteffen Klassert 		goto out;
263dff3bb06SSteffen Klassert 
264dff3bb06SSteffen Klassert 	skb->network_header += ah_hlen;
265dff3bb06SSteffen Klassert 	memcpy(skb_network_header(skb), work_iph, ihl);
266dff3bb06SSteffen Klassert 	__skb_pull(skb, ah_hlen + ihl);
267dff3bb06SSteffen Klassert 	skb_set_transport_header(skb, -ihl);
268dff3bb06SSteffen Klassert 
269dff3bb06SSteffen Klassert 	err = ah->nexthdr;
270dff3bb06SSteffen Klassert out:
271dff3bb06SSteffen Klassert 	kfree(AH_SKB_CB(skb)->tmp);
272dff3bb06SSteffen Klassert 	xfrm_input_resume(skb, err);
273dff3bb06SSteffen Klassert }
274dff3bb06SSteffen Klassert 
275e695633eSHerbert Xu static int ah_input(struct xfrm_state *x, struct sk_buff *skb)
2761da177e4SLinus Torvalds {
2771da177e4SLinus Torvalds 	int ah_hlen;
27831a4ab93SHerbert Xu 	int ihl;
279631a6698SHerbert Xu 	int nexthdr;
280dff3bb06SSteffen Klassert 	int nfrags;
281dff3bb06SSteffen Klassert 	u8 *auth_data;
282dff3bb06SSteffen Klassert 	u8 *icv;
283dff3bb06SSteffen Klassert 	struct sk_buff *trailer;
284dff3bb06SSteffen Klassert 	struct crypto_ahash *ahash;
285dff3bb06SSteffen Klassert 	struct ahash_request *req;
286dff3bb06SSteffen Klassert 	struct scatterlist *sg;
287dff3bb06SSteffen Klassert 	struct iphdr *iph, *work_iph;
2881da177e4SLinus Torvalds 	struct ip_auth_hdr *ah;
2891da177e4SLinus Torvalds 	struct ah_data *ahp;
290dff3bb06SSteffen Klassert 	int err = -ENOMEM;
2911da177e4SLinus Torvalds 
29287bdc48dSHerbert Xu 	if (!pskb_may_pull(skb, sizeof(*ah)))
2931da177e4SLinus Torvalds 		goto out;
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	ah = (struct ip_auth_hdr *)skb->data;
2961da177e4SLinus Torvalds 	ahp = x->data;
297dff3bb06SSteffen Klassert 	ahash = ahp->ahash;
298dff3bb06SSteffen Klassert 
299631a6698SHerbert Xu 	nexthdr = ah->nexthdr;
3001da177e4SLinus Torvalds 	ah_hlen = (ah->hdrlen + 2) << 2;
3011da177e4SLinus Torvalds 
30287bdc48dSHerbert Xu 	if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) &&
30387bdc48dSHerbert Xu 	    ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len))
3041da177e4SLinus Torvalds 		goto out;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, ah_hlen))
3071da177e4SLinus Torvalds 		goto out;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	/* We are going to _remove_ AH header to keep sockets happy,
3101da177e4SLinus Torvalds 	 * so... Later this can change. */
3111da177e4SLinus Torvalds 	if (skb_cloned(skb) &&
3121da177e4SLinus Torvalds 	    pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
3131da177e4SLinus Torvalds 		goto out;
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	skb->ip_summed = CHECKSUM_NONE;
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	ah = (struct ip_auth_hdr *)skb->data;
318eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
319dff3bb06SSteffen Klassert 	ihl = ip_hdrlen(skb);
3201da177e4SLinus Torvalds 
321dff3bb06SSteffen Klassert 	if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
322dff3bb06SSteffen Klassert 		goto out;
323dff3bb06SSteffen Klassert 	nfrags = err;
324dff3bb06SSteffen Klassert 
325dff3bb06SSteffen Klassert 	work_iph = ah_alloc_tmp(ahash, nfrags, ihl + ahp->icv_trunc_len);
326dff3bb06SSteffen Klassert 	if (!work_iph)
327dff3bb06SSteffen Klassert 		goto out;
328dff3bb06SSteffen Klassert 
329dff3bb06SSteffen Klassert 	auth_data = ah_tmp_auth(work_iph, ihl);
330dff3bb06SSteffen Klassert 	icv = ah_tmp_icv(ahash, auth_data, ahp->icv_trunc_len);
331dff3bb06SSteffen Klassert 	req = ah_tmp_req(ahash, icv);
332dff3bb06SSteffen Klassert 	sg = ah_req_sg(ahash, req);
333dff3bb06SSteffen Klassert 
334dff3bb06SSteffen Klassert 	memcpy(work_iph, iph, ihl);
335dff3bb06SSteffen Klassert 	memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
336dff3bb06SSteffen Klassert 	memset(ah->auth_data, 0, ahp->icv_trunc_len);
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	iph->ttl = 0;
3391da177e4SLinus Torvalds 	iph->tos = 0;
3401da177e4SLinus Torvalds 	iph->frag_off = 0;
3411da177e4SLinus Torvalds 	iph->check = 0;
34231a4ab93SHerbert Xu 	if (ihl > sizeof(*iph)) {
343d5a0a1e3SAl Viro 		__be32 dummy;
344dff3bb06SSteffen Klassert 		err = ip_clear_mutable_options(iph, &dummy);
345dff3bb06SSteffen Klassert 		if (err)
346dff3bb06SSteffen Klassert 			goto out_free;
3471da177e4SLinus Torvalds 	}
3480ebea8efSHerbert Xu 
34931a4ab93SHerbert Xu 	skb_push(skb, ihl);
3500ebea8efSHerbert Xu 
351dff3bb06SSteffen Klassert 	sg_init_table(sg, nfrags);
352dff3bb06SSteffen Klassert 	skb_to_sgvec(skb, sg, 0, skb->len);
353dff3bb06SSteffen Klassert 
354dff3bb06SSteffen Klassert 	ahash_request_set_crypt(req, sg, icv, skb->len);
355dff3bb06SSteffen Klassert 	ahash_request_set_callback(req, 0, ah_input_done, skb);
356dff3bb06SSteffen Klassert 
357dff3bb06SSteffen Klassert 	AH_SKB_CB(skb)->tmp = work_iph;
358dff3bb06SSteffen Klassert 
359dff3bb06SSteffen Klassert 	err = crypto_ahash_digest(req);
360dff3bb06SSteffen Klassert 	if (err) {
361dff3bb06SSteffen Klassert 		if (err == -EINPROGRESS)
3621da177e4SLinus Torvalds 			goto out;
3630ebea8efSHerbert Xu 
364dff3bb06SSteffen Klassert 		if (err == -EBUSY)
365dff3bb06SSteffen Klassert 			err = NET_XMIT_DROP;
366dff3bb06SSteffen Klassert 		goto out_free;
367dff3bb06SSteffen Klassert 	}
368dff3bb06SSteffen Klassert 
369dff3bb06SSteffen Klassert 	err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0;
370dff3bb06SSteffen Klassert 	if (err)
371dff3bb06SSteffen Klassert 		goto out_free;
372dff3bb06SSteffen Klassert 
373b0e380b1SArnaldo Carvalho de Melo 	skb->network_header += ah_hlen;
374dff3bb06SSteffen Klassert 	memcpy(skb_network_header(skb), work_iph, ihl);
37531a4ab93SHerbert Xu 	__skb_pull(skb, ah_hlen + ihl);
376dff3bb06SSteffen Klassert 	skb_set_transport_header(skb, -ihl);
3771da177e4SLinus Torvalds 
378dff3bb06SSteffen Klassert 	err = nexthdr;
3791da177e4SLinus Torvalds 
380dff3bb06SSteffen Klassert out_free:
381dff3bb06SSteffen Klassert 	kfree (work_iph);
3821da177e4SLinus Torvalds out:
38307d4ee58SHerbert Xu 	return err;
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds static void ah4_err(struct sk_buff *skb, u32 info)
3871da177e4SLinus Torvalds {
3884fb236baSAlexey Dobriyan 	struct net *net = dev_net(skb->dev);
3891da177e4SLinus Torvalds 	struct iphdr *iph = (struct iphdr *)skb->data;
3901da177e4SLinus Torvalds 	struct ip_auth_hdr *ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
3911da177e4SLinus Torvalds 	struct xfrm_state *x;
3921da177e4SLinus Torvalds 
39388c7664fSArnaldo Carvalho de Melo 	if (icmp_hdr(skb)->type != ICMP_DEST_UNREACH ||
39488c7664fSArnaldo Carvalho de Melo 	    icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
3951da177e4SLinus Torvalds 		return;
3961da177e4SLinus Torvalds 
397bd55775cSJamal Hadi Salim 	x = xfrm_state_lookup(net, skb->mark, (xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET);
3981da177e4SLinus Torvalds 	if (!x)
3991da177e4SLinus Torvalds 		return;
4001da177e4SLinus Torvalds 	printk(KERN_DEBUG "pmtu discovery on SA AH/%08x/%08x\n",
4011da177e4SLinus Torvalds 	       ntohl(ah->spi), ntohl(iph->daddr));
4021da177e4SLinus Torvalds 	xfrm_state_put(x);
4031da177e4SLinus Torvalds }
4041da177e4SLinus Torvalds 
40572cb6962SHerbert Xu static int ah_init_state(struct xfrm_state *x)
4061da177e4SLinus Torvalds {
4071da177e4SLinus Torvalds 	struct ah_data *ahp = NULL;
4081da177e4SLinus Torvalds 	struct xfrm_algo_desc *aalg_desc;
409dff3bb06SSteffen Klassert 	struct crypto_ahash *ahash;
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	if (!x->aalg)
4121da177e4SLinus Torvalds 		goto error;
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	if (x->encap)
4151da177e4SLinus Torvalds 		goto error;
4161da177e4SLinus Torvalds 
4170da974f4SPanagiotis Issaris 	ahp = kzalloc(sizeof(*ahp), GFP_KERNEL);
418dff3bb06SSteffen Klassert 	if (!ahp)
4191da177e4SLinus Torvalds 		return -ENOMEM;
4201da177e4SLinus Torvalds 
421dff3bb06SSteffen Klassert 	ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0);
422dff3bb06SSteffen Klassert 	if (IS_ERR(ahash))
4231da177e4SLinus Torvalds 		goto error;
42407d4ee58SHerbert Xu 
425dff3bb06SSteffen Klassert 	ahp->ahash = ahash;
426dff3bb06SSteffen Klassert 	if (crypto_ahash_setkey(ahash, x->aalg->alg_key,
427bc31d3b2SHerbert Xu 				(x->aalg->alg_key_len + 7) / 8))
42807d4ee58SHerbert Xu 		goto error;
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	/*
4311da177e4SLinus Torvalds 	 * Lookup the algorithm description maintained by xfrm_algo,
4321da177e4SLinus Torvalds 	 * verify crypto transform properties, and store information
4331da177e4SLinus Torvalds 	 * we need for AH processing.  This lookup cannot fail here
434dff3bb06SSteffen Klassert 	 * after a successful crypto_alloc_ahash().
4351da177e4SLinus Torvalds 	 */
4361da177e4SLinus Torvalds 	aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
4371da177e4SLinus Torvalds 	BUG_ON(!aalg_desc);
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
440dff3bb06SSteffen Klassert 	    crypto_ahash_digestsize(ahash)) {
4411da177e4SLinus Torvalds 		printk(KERN_INFO "AH: %s digestsize %u != %hu\n",
442dff3bb06SSteffen Klassert 		       x->aalg->alg_name, crypto_ahash_digestsize(ahash),
4431da177e4SLinus Torvalds 		       aalg_desc->uinfo.auth.icv_fullbits/8);
4441da177e4SLinus Torvalds 		goto error;
4451da177e4SLinus Torvalds 	}
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
4488f8a088cSMartin Willi 	ahp->icv_trunc_len = x->aalg->alg_trunc_len/8;
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN);
4511da177e4SLinus Torvalds 
45287bdc48dSHerbert Xu 	x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
45387bdc48dSHerbert Xu 					  ahp->icv_trunc_len);
4547e49e6deSMasahide NAKAMURA 	if (x->props.mode == XFRM_MODE_TUNNEL)
4551da177e4SLinus Torvalds 		x->props.header_len += sizeof(struct iphdr);
4561da177e4SLinus Torvalds 	x->data = ahp;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	return 0;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds error:
4611da177e4SLinus Torvalds 	if (ahp) {
462dff3bb06SSteffen Klassert 		crypto_free_ahash(ahp->ahash);
4631da177e4SLinus Torvalds 		kfree(ahp);
4641da177e4SLinus Torvalds 	}
4651da177e4SLinus Torvalds 	return -EINVAL;
4661da177e4SLinus Torvalds }
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds static void ah_destroy(struct xfrm_state *x)
4691da177e4SLinus Torvalds {
4701da177e4SLinus Torvalds 	struct ah_data *ahp = x->data;
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	if (!ahp)
4731da177e4SLinus Torvalds 		return;
4741da177e4SLinus Torvalds 
475dff3bb06SSteffen Klassert 	crypto_free_ahash(ahp->ahash);
4761da177e4SLinus Torvalds 	kfree(ahp);
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 
480533cb5b0SEric Dumazet static const struct xfrm_type ah_type =
4811da177e4SLinus Torvalds {
4821da177e4SLinus Torvalds 	.description	= "AH4",
4831da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
4841da177e4SLinus Torvalds 	.proto	     	= IPPROTO_AH,
485436a0a40SHerbert Xu 	.flags		= XFRM_TYPE_REPLAY_PROT,
4861da177e4SLinus Torvalds 	.init_state	= ah_init_state,
4871da177e4SLinus Torvalds 	.destructor	= ah_destroy,
4881da177e4SLinus Torvalds 	.input		= ah_input,
4891da177e4SLinus Torvalds 	.output		= ah_output
4901da177e4SLinus Torvalds };
4911da177e4SLinus Torvalds 
49232613090SAlexey Dobriyan static const struct net_protocol ah4_protocol = {
4931da177e4SLinus Torvalds 	.handler	=	xfrm4_rcv,
4941da177e4SLinus Torvalds 	.err_handler	=	ah4_err,
4951da177e4SLinus Torvalds 	.no_policy	=	1,
4964fb236baSAlexey Dobriyan 	.netns_ok	=	1,
4971da177e4SLinus Torvalds };
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds static int __init ah4_init(void)
5001da177e4SLinus Torvalds {
5011da177e4SLinus Torvalds 	if (xfrm_register_type(&ah_type, AF_INET) < 0) {
5021da177e4SLinus Torvalds 		printk(KERN_INFO "ip ah init: can't add xfrm type\n");
5031da177e4SLinus Torvalds 		return -EAGAIN;
5041da177e4SLinus Torvalds 	}
5051da177e4SLinus Torvalds 	if (inet_add_protocol(&ah4_protocol, IPPROTO_AH) < 0) {
5061da177e4SLinus Torvalds 		printk(KERN_INFO "ip ah init: can't add protocol\n");
5071da177e4SLinus Torvalds 		xfrm_unregister_type(&ah_type, AF_INET);
5081da177e4SLinus Torvalds 		return -EAGAIN;
5091da177e4SLinus Torvalds 	}
5101da177e4SLinus Torvalds 	return 0;
5111da177e4SLinus Torvalds }
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds static void __exit ah4_fini(void)
5141da177e4SLinus Torvalds {
5151da177e4SLinus Torvalds 	if (inet_del_protocol(&ah4_protocol, IPPROTO_AH) < 0)
5161da177e4SLinus Torvalds 		printk(KERN_INFO "ip ah close: can't remove protocol\n");
5171da177e4SLinus Torvalds 	if (xfrm_unregister_type(&ah_type, AF_INET) < 0)
5181da177e4SLinus Torvalds 		printk(KERN_INFO "ip ah close: can't remove xfrm type\n");
5191da177e4SLinus Torvalds }
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds module_init(ah4_init);
5221da177e4SLinus Torvalds module_exit(ah4_fini);
5231da177e4SLinus Torvalds MODULE_LICENSE("GPL");
524d3d6dd3aSMasahide NAKAMURA MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_AH);
525