xref: /linux/net/ipv4/ah4.c (revision 436a0a402203d5a47d2edf7e4dde6c08a7257983)
107d4ee58SHerbert Xu #include <linux/err.h>
21da177e4SLinus Torvalds #include <linux/module.h>
31da177e4SLinus Torvalds #include <net/ip.h>
41da177e4SLinus Torvalds #include <net/xfrm.h>
51da177e4SLinus Torvalds #include <net/ah.h>
61da177e4SLinus Torvalds #include <linux/crypto.h>
71da177e4SLinus Torvalds #include <linux/pfkeyv2.h>
81da177e4SLinus Torvalds #include <net/icmp.h>
914c85021SArnaldo Carvalho de Melo #include <net/protocol.h>
101da177e4SLinus Torvalds #include <asm/scatterlist.h>
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds /* Clear mutable options and find final destination to substitute
141da177e4SLinus Torvalds  * into IP header for icv calculation. Options are already checked
151da177e4SLinus Torvalds  * for validity, so paranoia is not required. */
161da177e4SLinus Torvalds 
17d5a0a1e3SAl Viro static int ip_clear_mutable_options(struct iphdr *iph, __be32 *daddr)
181da177e4SLinus Torvalds {
191da177e4SLinus Torvalds 	unsigned char * optptr = (unsigned char*)(iph+1);
201da177e4SLinus Torvalds 	int  l = iph->ihl*4 - sizeof(struct iphdr);
211da177e4SLinus Torvalds 	int  optlen;
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds 	while (l > 0) {
241da177e4SLinus Torvalds 		switch (*optptr) {
251da177e4SLinus Torvalds 		case IPOPT_END:
261da177e4SLinus Torvalds 			return 0;
271da177e4SLinus Torvalds 		case IPOPT_NOOP:
281da177e4SLinus Torvalds 			l--;
291da177e4SLinus Torvalds 			optptr++;
301da177e4SLinus Torvalds 			continue;
311da177e4SLinus Torvalds 		}
321da177e4SLinus Torvalds 		optlen = optptr[1];
331da177e4SLinus Torvalds 		if (optlen<2 || optlen>l)
341da177e4SLinus Torvalds 			return -EINVAL;
351da177e4SLinus Torvalds 		switch (*optptr) {
361da177e4SLinus Torvalds 		case IPOPT_SEC:
371da177e4SLinus Torvalds 		case 0x85:	/* Some "Extended Security" crap. */
3811a03f78SPaul Moore 		case IPOPT_CIPSO:
391da177e4SLinus Torvalds 		case IPOPT_RA:
401da177e4SLinus Torvalds 		case 0x80|21:	/* RFC1770 */
411da177e4SLinus Torvalds 			break;
421da177e4SLinus Torvalds 		case IPOPT_LSRR:
431da177e4SLinus Torvalds 		case IPOPT_SSRR:
441da177e4SLinus Torvalds 			if (optlen < 6)
451da177e4SLinus Torvalds 				return -EINVAL;
461da177e4SLinus Torvalds 			memcpy(daddr, optptr+optlen-4, 4);
471da177e4SLinus Torvalds 			/* Fall through */
481da177e4SLinus Torvalds 		default:
4996fe1c02SNick Bowler 			memset(optptr, 0, optlen);
501da177e4SLinus Torvalds 		}
511da177e4SLinus Torvalds 		l -= optlen;
521da177e4SLinus Torvalds 		optptr += optlen;
531da177e4SLinus Torvalds 	}
541da177e4SLinus Torvalds 	return 0;
551da177e4SLinus Torvalds }
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds static int ah_output(struct xfrm_state *x, struct sk_buff *skb)
581da177e4SLinus Torvalds {
591da177e4SLinus Torvalds 	int err;
601da177e4SLinus Torvalds 	struct iphdr *iph, *top_iph;
611da177e4SLinus Torvalds 	struct ip_auth_hdr *ah;
621da177e4SLinus Torvalds 	struct ah_data *ahp;
631da177e4SLinus Torvalds 	union {
641da177e4SLinus Torvalds 		struct iphdr	iph;
651da177e4SLinus Torvalds 		char 		buf[60];
661da177e4SLinus Torvalds 	} tmp_iph;
671da177e4SLinus Torvalds 
68eddc9ec5SArnaldo Carvalho de Melo 	top_iph = ip_hdr(skb);
691da177e4SLinus Torvalds 	iph = &tmp_iph.iph;
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 	iph->tos = top_iph->tos;
721da177e4SLinus Torvalds 	iph->ttl = top_iph->ttl;
731da177e4SLinus Torvalds 	iph->frag_off = top_iph->frag_off;
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 	if (top_iph->ihl != 5) {
761da177e4SLinus Torvalds 		iph->daddr = top_iph->daddr;
771da177e4SLinus Torvalds 		memcpy(iph+1, top_iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
781da177e4SLinus Torvalds 		err = ip_clear_mutable_options(top_iph, &top_iph->daddr);
791da177e4SLinus Torvalds 		if (err)
801da177e4SLinus Torvalds 			goto error;
811da177e4SLinus Torvalds 	}
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 	ah = (struct ip_auth_hdr *)((char *)top_iph+top_iph->ihl*4);
841da177e4SLinus Torvalds 	ah->nexthdr = top_iph->protocol;
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds 	top_iph->tos = 0;
871da177e4SLinus Torvalds 	top_iph->tot_len = htons(skb->len);
881da177e4SLinus Torvalds 	top_iph->frag_off = 0;
891da177e4SLinus Torvalds 	top_iph->ttl = 0;
901da177e4SLinus Torvalds 	top_iph->protocol = IPPROTO_AH;
911da177e4SLinus Torvalds 	top_iph->check = 0;
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	ahp = x->data;
941da177e4SLinus Torvalds 	ah->hdrlen  = (XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
951da177e4SLinus Torvalds 				   ahp->icv_trunc_len) >> 2) - 2;
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	ah->reserved = 0;
981da177e4SLinus Torvalds 	ah->spi = x->id.spi;
99*436a0a40SHerbert Xu 	ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq);
10007d4ee58SHerbert Xu 	err = ah_mac_digest(ahp, skb, ah->auth_data);
10107d4ee58SHerbert Xu 	if (err)
10207d4ee58SHerbert Xu 		goto error;
10307d4ee58SHerbert Xu 	memcpy(ah->auth_data, ahp->work_icv, ahp->icv_trunc_len);
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	top_iph->tos = iph->tos;
1061da177e4SLinus Torvalds 	top_iph->ttl = iph->ttl;
1071da177e4SLinus Torvalds 	top_iph->frag_off = iph->frag_off;
1081da177e4SLinus Torvalds 	if (top_iph->ihl != 5) {
1091da177e4SLinus Torvalds 		top_iph->daddr = iph->daddr;
1101da177e4SLinus Torvalds 		memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
1111da177e4SLinus Torvalds 	}
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	ip_send_check(top_iph);
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 	err = 0;
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds error:
1181da177e4SLinus Torvalds 	return err;
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds 
121e695633eSHerbert Xu static int ah_input(struct xfrm_state *x, struct sk_buff *skb)
1221da177e4SLinus Torvalds {
1231da177e4SLinus Torvalds 	int ah_hlen;
12431a4ab93SHerbert Xu 	int ihl;
12507d4ee58SHerbert Xu 	int err = -EINVAL;
1261da177e4SLinus Torvalds 	struct iphdr *iph;
1271da177e4SLinus Torvalds 	struct ip_auth_hdr *ah;
1281da177e4SLinus Torvalds 	struct ah_data *ahp;
1291da177e4SLinus Torvalds 	char work_buf[60];
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, sizeof(struct ip_auth_hdr)))
1321da177e4SLinus Torvalds 		goto out;
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	ah = (struct ip_auth_hdr*)skb->data;
1351da177e4SLinus Torvalds 	ahp = x->data;
1361da177e4SLinus Torvalds 	ah_hlen = (ah->hdrlen + 2) << 2;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	if (ah_hlen != XFRM_ALIGN8(sizeof(struct ip_auth_hdr) + ahp->icv_full_len) &&
1391da177e4SLinus Torvalds 	    ah_hlen != XFRM_ALIGN8(sizeof(struct ip_auth_hdr) + ahp->icv_trunc_len))
1401da177e4SLinus Torvalds 		goto out;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, ah_hlen))
1431da177e4SLinus Torvalds 		goto out;
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	/* We are going to _remove_ AH header to keep sockets happy,
1461da177e4SLinus Torvalds 	 * so... Later this can change. */
1471da177e4SLinus Torvalds 	if (skb_cloned(skb) &&
1481da177e4SLinus Torvalds 	    pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
1491da177e4SLinus Torvalds 		goto out;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	skb->ip_summed = CHECKSUM_NONE;
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	ah = (struct ip_auth_hdr*)skb->data;
154eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
1551da177e4SLinus Torvalds 
156d56f90a7SArnaldo Carvalho de Melo 	ihl = skb->data - skb_network_header(skb);
15731a4ab93SHerbert Xu 	memcpy(work_buf, iph, ihl);
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	iph->ttl = 0;
1601da177e4SLinus Torvalds 	iph->tos = 0;
1611da177e4SLinus Torvalds 	iph->frag_off = 0;
1621da177e4SLinus Torvalds 	iph->check = 0;
16331a4ab93SHerbert Xu 	if (ihl > sizeof(*iph)) {
164d5a0a1e3SAl Viro 		__be32 dummy;
1651da177e4SLinus Torvalds 		if (ip_clear_mutable_options(iph, &dummy))
1661da177e4SLinus Torvalds 			goto out;
1671da177e4SLinus Torvalds 	}
1681da177e4SLinus Torvalds 	{
1691da177e4SLinus Torvalds 		u8 auth_data[MAX_AH_AUTH_LEN];
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 		memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
17231a4ab93SHerbert Xu 		skb_push(skb, ihl);
17307d4ee58SHerbert Xu 		err = ah_mac_digest(ahp, skb, ah->auth_data);
17407d4ee58SHerbert Xu 		if (err)
17507d4ee58SHerbert Xu 			goto out;
17607d4ee58SHerbert Xu 		err = -EINVAL;
17707d4ee58SHerbert Xu 		if (memcmp(ahp->work_icv, auth_data, ahp->icv_trunc_len)) {
1781da177e4SLinus Torvalds 			x->stats.integrity_failed++;
1791da177e4SLinus Torvalds 			goto out;
1801da177e4SLinus Torvalds 		}
1811da177e4SLinus Torvalds 	}
1821da177e4SLinus Torvalds 	((struct iphdr*)work_buf)->protocol = ah->nexthdr;
183b0e380b1SArnaldo Carvalho de Melo 	skb->network_header += ah_hlen;
184badff6d0SArnaldo Carvalho de Melo 	memcpy(skb_network_header(skb), work_buf, ihl);
185b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header;
18631a4ab93SHerbert Xu 	__skb_pull(skb, ah_hlen + ihl);
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	return 0;
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds out:
19107d4ee58SHerbert Xu 	return err;
1921da177e4SLinus Torvalds }
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds static void ah4_err(struct sk_buff *skb, u32 info)
1951da177e4SLinus Torvalds {
1961da177e4SLinus Torvalds 	struct iphdr *iph = (struct iphdr*)skb->data;
1971da177e4SLinus Torvalds 	struct ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+(iph->ihl<<2));
1981da177e4SLinus Torvalds 	struct xfrm_state *x;
1991da177e4SLinus Torvalds 
20088c7664fSArnaldo Carvalho de Melo 	if (icmp_hdr(skb)->type != ICMP_DEST_UNREACH ||
20188c7664fSArnaldo Carvalho de Melo 	    icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
2021da177e4SLinus Torvalds 		return;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET);
2051da177e4SLinus Torvalds 	if (!x)
2061da177e4SLinus Torvalds 		return;
2071da177e4SLinus Torvalds 	printk(KERN_DEBUG "pmtu discovery on SA AH/%08x/%08x\n",
2081da177e4SLinus Torvalds 	       ntohl(ah->spi), ntohl(iph->daddr));
2091da177e4SLinus Torvalds 	xfrm_state_put(x);
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
21272cb6962SHerbert Xu static int ah_init_state(struct xfrm_state *x)
2131da177e4SLinus Torvalds {
2141da177e4SLinus Torvalds 	struct ah_data *ahp = NULL;
2151da177e4SLinus Torvalds 	struct xfrm_algo_desc *aalg_desc;
21607d4ee58SHerbert Xu 	struct crypto_hash *tfm;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	if (!x->aalg)
2191da177e4SLinus Torvalds 		goto error;
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	if (x->encap)
2221da177e4SLinus Torvalds 		goto error;
2231da177e4SLinus Torvalds 
2240da974f4SPanagiotis Issaris 	ahp = kzalloc(sizeof(*ahp), GFP_KERNEL);
2251da177e4SLinus Torvalds 	if (ahp == NULL)
2261da177e4SLinus Torvalds 		return -ENOMEM;
2271da177e4SLinus Torvalds 
22807d4ee58SHerbert Xu 	tfm = crypto_alloc_hash(x->aalg->alg_name, 0, CRYPTO_ALG_ASYNC);
22907d4ee58SHerbert Xu 	if (IS_ERR(tfm))
2301da177e4SLinus Torvalds 		goto error;
23107d4ee58SHerbert Xu 
23207d4ee58SHerbert Xu 	ahp->tfm = tfm;
233bc31d3b2SHerbert Xu 	if (crypto_hash_setkey(tfm, x->aalg->alg_key,
234bc31d3b2SHerbert Xu 			       (x->aalg->alg_key_len + 7) / 8))
23507d4ee58SHerbert Xu 		goto error;
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	/*
2381da177e4SLinus Torvalds 	 * Lookup the algorithm description maintained by xfrm_algo,
2391da177e4SLinus Torvalds 	 * verify crypto transform properties, and store information
2401da177e4SLinus Torvalds 	 * we need for AH processing.  This lookup cannot fail here
24107d4ee58SHerbert Xu 	 * after a successful crypto_alloc_hash().
2421da177e4SLinus Torvalds 	 */
2431da177e4SLinus Torvalds 	aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
2441da177e4SLinus Torvalds 	BUG_ON(!aalg_desc);
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
24707d4ee58SHerbert Xu 	    crypto_hash_digestsize(tfm)) {
2481da177e4SLinus Torvalds 		printk(KERN_INFO "AH: %s digestsize %u != %hu\n",
24907d4ee58SHerbert Xu 		       x->aalg->alg_name, crypto_hash_digestsize(tfm),
2501da177e4SLinus Torvalds 		       aalg_desc->uinfo.auth.icv_fullbits/8);
2511da177e4SLinus Torvalds 		goto error;
2521da177e4SLinus Torvalds 	}
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
2551da177e4SLinus Torvalds 	ahp->icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN);
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	ahp->work_icv = kmalloc(ahp->icv_full_len, GFP_KERNEL);
2601da177e4SLinus Torvalds 	if (!ahp->work_icv)
2611da177e4SLinus Torvalds 		goto error;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) + ahp->icv_trunc_len);
2647e49e6deSMasahide NAKAMURA 	if (x->props.mode == XFRM_MODE_TUNNEL)
2651da177e4SLinus Torvalds 		x->props.header_len += sizeof(struct iphdr);
2661da177e4SLinus Torvalds 	x->data = ahp;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	return 0;
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds error:
2711da177e4SLinus Torvalds 	if (ahp) {
2721da177e4SLinus Torvalds 		kfree(ahp->work_icv);
27307d4ee58SHerbert Xu 		crypto_free_hash(ahp->tfm);
2741da177e4SLinus Torvalds 		kfree(ahp);
2751da177e4SLinus Torvalds 	}
2761da177e4SLinus Torvalds 	return -EINVAL;
2771da177e4SLinus Torvalds }
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds static void ah_destroy(struct xfrm_state *x)
2801da177e4SLinus Torvalds {
2811da177e4SLinus Torvalds 	struct ah_data *ahp = x->data;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	if (!ahp)
2841da177e4SLinus Torvalds 		return;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	kfree(ahp->work_icv);
2871da177e4SLinus Torvalds 	ahp->work_icv = NULL;
28807d4ee58SHerbert Xu 	crypto_free_hash(ahp->tfm);
2891da177e4SLinus Torvalds 	ahp->tfm = NULL;
2901da177e4SLinus Torvalds 	kfree(ahp);
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds static struct xfrm_type ah_type =
2951da177e4SLinus Torvalds {
2961da177e4SLinus Torvalds 	.description	= "AH4",
2971da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
2981da177e4SLinus Torvalds 	.proto	     	= IPPROTO_AH,
299*436a0a40SHerbert Xu 	.flags		= XFRM_TYPE_REPLAY_PROT,
3001da177e4SLinus Torvalds 	.init_state	= ah_init_state,
3011da177e4SLinus Torvalds 	.destructor	= ah_destroy,
3021da177e4SLinus Torvalds 	.input		= ah_input,
3031da177e4SLinus Torvalds 	.output		= ah_output
3041da177e4SLinus Torvalds };
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds static struct net_protocol ah4_protocol = {
3071da177e4SLinus Torvalds 	.handler	=	xfrm4_rcv,
3081da177e4SLinus Torvalds 	.err_handler	=	ah4_err,
3091da177e4SLinus Torvalds 	.no_policy	=	1,
3101da177e4SLinus Torvalds };
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds static int __init ah4_init(void)
3131da177e4SLinus Torvalds {
3141da177e4SLinus Torvalds 	if (xfrm_register_type(&ah_type, AF_INET) < 0) {
3151da177e4SLinus Torvalds 		printk(KERN_INFO "ip ah init: can't add xfrm type\n");
3161da177e4SLinus Torvalds 		return -EAGAIN;
3171da177e4SLinus Torvalds 	}
3181da177e4SLinus Torvalds 	if (inet_add_protocol(&ah4_protocol, IPPROTO_AH) < 0) {
3191da177e4SLinus Torvalds 		printk(KERN_INFO "ip ah init: can't add protocol\n");
3201da177e4SLinus Torvalds 		xfrm_unregister_type(&ah_type, AF_INET);
3211da177e4SLinus Torvalds 		return -EAGAIN;
3221da177e4SLinus Torvalds 	}
3231da177e4SLinus Torvalds 	return 0;
3241da177e4SLinus Torvalds }
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds static void __exit ah4_fini(void)
3271da177e4SLinus Torvalds {
3281da177e4SLinus Torvalds 	if (inet_del_protocol(&ah4_protocol, IPPROTO_AH) < 0)
3291da177e4SLinus Torvalds 		printk(KERN_INFO "ip ah close: can't remove protocol\n");
3301da177e4SLinus Torvalds 	if (xfrm_unregister_type(&ah_type, AF_INET) < 0)
3311da177e4SLinus Torvalds 		printk(KERN_INFO "ip ah close: can't remove xfrm type\n");
3321da177e4SLinus Torvalds }
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds module_init(ah4_init);
3351da177e4SLinus Torvalds module_exit(ah4_fini);
3361da177e4SLinus Torvalds MODULE_LICENSE("GPL");
337d3d6dd3aSMasahide NAKAMURA MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_AH);
338