xref: /linux/net/ipv4/ah4.c (revision b0e380b1d8a8e0aca215df97702f99815f05c094)
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:
491da177e4SLinus Torvalds 			memset(optptr+2, 0, optlen-2);
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;
991da177e4SLinus Torvalds 	ah->seq_no = htonl(++x->replay.oseq);
1009500e8a8SJamal Hadi Salim 	xfrm_aevent_doreplay(x);
10107d4ee58SHerbert Xu 	err = ah_mac_digest(ahp, skb, ah->auth_data);
10207d4ee58SHerbert Xu 	if (err)
10307d4ee58SHerbert Xu 		goto error;
10407d4ee58SHerbert Xu 	memcpy(ah->auth_data, ahp->work_icv, ahp->icv_trunc_len);
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	top_iph->tos = iph->tos;
1071da177e4SLinus Torvalds 	top_iph->ttl = iph->ttl;
1081da177e4SLinus Torvalds 	top_iph->frag_off = iph->frag_off;
1091da177e4SLinus Torvalds 	if (top_iph->ihl != 5) {
1101da177e4SLinus Torvalds 		top_iph->daddr = iph->daddr;
1111da177e4SLinus Torvalds 		memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
1121da177e4SLinus Torvalds 	}
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 	ip_send_check(top_iph);
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	err = 0;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds error:
1191da177e4SLinus Torvalds 	return err;
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds 
122e695633eSHerbert Xu static int ah_input(struct xfrm_state *x, struct sk_buff *skb)
1231da177e4SLinus Torvalds {
1241da177e4SLinus Torvalds 	int ah_hlen;
12531a4ab93SHerbert Xu 	int ihl;
12607d4ee58SHerbert Xu 	int err = -EINVAL;
1271da177e4SLinus Torvalds 	struct iphdr *iph;
1281da177e4SLinus Torvalds 	struct ip_auth_hdr *ah;
1291da177e4SLinus Torvalds 	struct ah_data *ahp;
1301da177e4SLinus Torvalds 	char work_buf[60];
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, sizeof(struct ip_auth_hdr)))
1331da177e4SLinus Torvalds 		goto out;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	ah = (struct ip_auth_hdr*)skb->data;
1361da177e4SLinus Torvalds 	ahp = x->data;
1371da177e4SLinus Torvalds 	ah_hlen = (ah->hdrlen + 2) << 2;
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	if (ah_hlen != XFRM_ALIGN8(sizeof(struct ip_auth_hdr) + ahp->icv_full_len) &&
1401da177e4SLinus Torvalds 	    ah_hlen != XFRM_ALIGN8(sizeof(struct ip_auth_hdr) + ahp->icv_trunc_len))
1411da177e4SLinus Torvalds 		goto out;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, ah_hlen))
1441da177e4SLinus Torvalds 		goto out;
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	/* We are going to _remove_ AH header to keep sockets happy,
1471da177e4SLinus Torvalds 	 * so... Later this can change. */
1481da177e4SLinus Torvalds 	if (skb_cloned(skb) &&
1491da177e4SLinus Torvalds 	    pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
1501da177e4SLinus Torvalds 		goto out;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	skb->ip_summed = CHECKSUM_NONE;
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	ah = (struct ip_auth_hdr*)skb->data;
155eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
1561da177e4SLinus Torvalds 
157d56f90a7SArnaldo Carvalho de Melo 	ihl = skb->data - skb_network_header(skb);
15831a4ab93SHerbert Xu 	memcpy(work_buf, iph, ihl);
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	iph->ttl = 0;
1611da177e4SLinus Torvalds 	iph->tos = 0;
1621da177e4SLinus Torvalds 	iph->frag_off = 0;
1631da177e4SLinus Torvalds 	iph->check = 0;
16431a4ab93SHerbert Xu 	if (ihl > sizeof(*iph)) {
165d5a0a1e3SAl Viro 		__be32 dummy;
1661da177e4SLinus Torvalds 		if (ip_clear_mutable_options(iph, &dummy))
1671da177e4SLinus Torvalds 			goto out;
1681da177e4SLinus Torvalds 	}
1691da177e4SLinus Torvalds 	{
1701da177e4SLinus Torvalds 		u8 auth_data[MAX_AH_AUTH_LEN];
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 		memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
17331a4ab93SHerbert Xu 		skb_push(skb, ihl);
17407d4ee58SHerbert Xu 		err = ah_mac_digest(ahp, skb, ah->auth_data);
17507d4ee58SHerbert Xu 		if (err)
17607d4ee58SHerbert Xu 			goto out;
17707d4ee58SHerbert Xu 		err = -EINVAL;
17807d4ee58SHerbert Xu 		if (memcmp(ahp->work_icv, auth_data, ahp->icv_trunc_len)) {
1791da177e4SLinus Torvalds 			x->stats.integrity_failed++;
1801da177e4SLinus Torvalds 			goto out;
1811da177e4SLinus Torvalds 		}
1821da177e4SLinus Torvalds 	}
1831da177e4SLinus Torvalds 	((struct iphdr*)work_buf)->protocol = ah->nexthdr;
184*b0e380b1SArnaldo Carvalho de Melo 	skb->network_header += ah_hlen;
185badff6d0SArnaldo Carvalho de Melo 	memcpy(skb_network_header(skb), work_buf, ihl);
186*b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header;
18731a4ab93SHerbert Xu 	__skb_pull(skb, ah_hlen + ihl);
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	return 0;
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds out:
19207d4ee58SHerbert Xu 	return err;
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds static void ah4_err(struct sk_buff *skb, u32 info)
1961da177e4SLinus Torvalds {
1971da177e4SLinus Torvalds 	struct iphdr *iph = (struct iphdr*)skb->data;
1981da177e4SLinus Torvalds 	struct ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+(iph->ihl<<2));
1991da177e4SLinus Torvalds 	struct xfrm_state *x;
2001da177e4SLinus Torvalds 
20188c7664fSArnaldo Carvalho de Melo 	if (icmp_hdr(skb)->type != ICMP_DEST_UNREACH ||
20288c7664fSArnaldo Carvalho de Melo 	    icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
2031da177e4SLinus Torvalds 		return;
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET);
2061da177e4SLinus Torvalds 	if (!x)
2071da177e4SLinus Torvalds 		return;
2081da177e4SLinus Torvalds 	printk(KERN_DEBUG "pmtu discovery on SA AH/%08x/%08x\n",
2091da177e4SLinus Torvalds 	       ntohl(ah->spi), ntohl(iph->daddr));
2101da177e4SLinus Torvalds 	xfrm_state_put(x);
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
21372cb6962SHerbert Xu static int ah_init_state(struct xfrm_state *x)
2141da177e4SLinus Torvalds {
2151da177e4SLinus Torvalds 	struct ah_data *ahp = NULL;
2161da177e4SLinus Torvalds 	struct xfrm_algo_desc *aalg_desc;
21707d4ee58SHerbert Xu 	struct crypto_hash *tfm;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	if (!x->aalg)
2201da177e4SLinus Torvalds 		goto error;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	/* null auth can use a zero length key */
2231da177e4SLinus Torvalds 	if (x->aalg->alg_key_len > 512)
2241da177e4SLinus Torvalds 		goto error;
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	if (x->encap)
2271da177e4SLinus Torvalds 		goto error;
2281da177e4SLinus Torvalds 
2290da974f4SPanagiotis Issaris 	ahp = kzalloc(sizeof(*ahp), GFP_KERNEL);
2301da177e4SLinus Torvalds 	if (ahp == NULL)
2311da177e4SLinus Torvalds 		return -ENOMEM;
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	ahp->key = x->aalg->alg_key;
2341da177e4SLinus Torvalds 	ahp->key_len = (x->aalg->alg_key_len+7)/8;
23507d4ee58SHerbert Xu 	tfm = crypto_alloc_hash(x->aalg->alg_name, 0, CRYPTO_ALG_ASYNC);
23607d4ee58SHerbert Xu 	if (IS_ERR(tfm))
2371da177e4SLinus Torvalds 		goto error;
23807d4ee58SHerbert Xu 
23907d4ee58SHerbert Xu 	ahp->tfm = tfm;
24007d4ee58SHerbert Xu 	if (crypto_hash_setkey(tfm, ahp->key, ahp->key_len))
24107d4ee58SHerbert Xu 		goto error;
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	/*
2441da177e4SLinus Torvalds 	 * Lookup the algorithm description maintained by xfrm_algo,
2451da177e4SLinus Torvalds 	 * verify crypto transform properties, and store information
2461da177e4SLinus Torvalds 	 * we need for AH processing.  This lookup cannot fail here
24707d4ee58SHerbert Xu 	 * after a successful crypto_alloc_hash().
2481da177e4SLinus Torvalds 	 */
2491da177e4SLinus Torvalds 	aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
2501da177e4SLinus Torvalds 	BUG_ON(!aalg_desc);
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
25307d4ee58SHerbert Xu 	    crypto_hash_digestsize(tfm)) {
2541da177e4SLinus Torvalds 		printk(KERN_INFO "AH: %s digestsize %u != %hu\n",
25507d4ee58SHerbert Xu 		       x->aalg->alg_name, crypto_hash_digestsize(tfm),
2561da177e4SLinus Torvalds 		       aalg_desc->uinfo.auth.icv_fullbits/8);
2571da177e4SLinus Torvalds 		goto error;
2581da177e4SLinus Torvalds 	}
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
2611da177e4SLinus Torvalds 	ahp->icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN);
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	ahp->work_icv = kmalloc(ahp->icv_full_len, GFP_KERNEL);
2661da177e4SLinus Torvalds 	if (!ahp->work_icv)
2671da177e4SLinus Torvalds 		goto error;
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) + ahp->icv_trunc_len);
2707e49e6deSMasahide NAKAMURA 	if (x->props.mode == XFRM_MODE_TUNNEL)
2711da177e4SLinus Torvalds 		x->props.header_len += sizeof(struct iphdr);
2721da177e4SLinus Torvalds 	x->data = ahp;
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	return 0;
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds error:
2771da177e4SLinus Torvalds 	if (ahp) {
2781da177e4SLinus Torvalds 		kfree(ahp->work_icv);
27907d4ee58SHerbert Xu 		crypto_free_hash(ahp->tfm);
2801da177e4SLinus Torvalds 		kfree(ahp);
2811da177e4SLinus Torvalds 	}
2821da177e4SLinus Torvalds 	return -EINVAL;
2831da177e4SLinus Torvalds }
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds static void ah_destroy(struct xfrm_state *x)
2861da177e4SLinus Torvalds {
2871da177e4SLinus Torvalds 	struct ah_data *ahp = x->data;
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 	if (!ahp)
2901da177e4SLinus Torvalds 		return;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	kfree(ahp->work_icv);
2931da177e4SLinus Torvalds 	ahp->work_icv = NULL;
29407d4ee58SHerbert Xu 	crypto_free_hash(ahp->tfm);
2951da177e4SLinus Torvalds 	ahp->tfm = NULL;
2961da177e4SLinus Torvalds 	kfree(ahp);
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds static struct xfrm_type ah_type =
3011da177e4SLinus Torvalds {
3021da177e4SLinus Torvalds 	.description	= "AH4",
3031da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
3041da177e4SLinus Torvalds 	.proto	     	= IPPROTO_AH,
3051da177e4SLinus Torvalds 	.init_state	= ah_init_state,
3061da177e4SLinus Torvalds 	.destructor	= ah_destroy,
3071da177e4SLinus Torvalds 	.input		= ah_input,
3081da177e4SLinus Torvalds 	.output		= ah_output
3091da177e4SLinus Torvalds };
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds static struct net_protocol ah4_protocol = {
3121da177e4SLinus Torvalds 	.handler	=	xfrm4_rcv,
3131da177e4SLinus Torvalds 	.err_handler	=	ah4_err,
3141da177e4SLinus Torvalds 	.no_policy	=	1,
3151da177e4SLinus Torvalds };
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds static int __init ah4_init(void)
3181da177e4SLinus Torvalds {
3191da177e4SLinus Torvalds 	if (xfrm_register_type(&ah_type, AF_INET) < 0) {
3201da177e4SLinus Torvalds 		printk(KERN_INFO "ip ah init: can't add xfrm type\n");
3211da177e4SLinus Torvalds 		return -EAGAIN;
3221da177e4SLinus Torvalds 	}
3231da177e4SLinus Torvalds 	if (inet_add_protocol(&ah4_protocol, IPPROTO_AH) < 0) {
3241da177e4SLinus Torvalds 		printk(KERN_INFO "ip ah init: can't add protocol\n");
3251da177e4SLinus Torvalds 		xfrm_unregister_type(&ah_type, AF_INET);
3261da177e4SLinus Torvalds 		return -EAGAIN;
3271da177e4SLinus Torvalds 	}
3281da177e4SLinus Torvalds 	return 0;
3291da177e4SLinus Torvalds }
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds static void __exit ah4_fini(void)
3321da177e4SLinus Torvalds {
3331da177e4SLinus Torvalds 	if (inet_del_protocol(&ah4_protocol, IPPROTO_AH) < 0)
3341da177e4SLinus Torvalds 		printk(KERN_INFO "ip ah close: can't remove protocol\n");
3351da177e4SLinus Torvalds 	if (xfrm_unregister_type(&ah_type, AF_INET) < 0)
3361da177e4SLinus Torvalds 		printk(KERN_INFO "ip ah close: can't remove xfrm type\n");
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds module_init(ah4_init);
3401da177e4SLinus Torvalds module_exit(ah4_fini);
3411da177e4SLinus Torvalds MODULE_LICENSE("GPL");
342