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> 8b7c6538cSHerbert Xu #include <linux/spinlock.h> 91da177e4SLinus Torvalds #include <net/icmp.h> 1014c85021SArnaldo Carvalho de Melo #include <net/protocol.h> 111da177e4SLinus Torvalds #include <asm/scatterlist.h> 121da177e4SLinus Torvalds 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds /* Clear mutable options and find final destination to substitute 151da177e4SLinus Torvalds * into IP header for icv calculation. Options are already checked 161da177e4SLinus Torvalds * for validity, so paranoia is not required. */ 171da177e4SLinus Torvalds 18d5a0a1e3SAl Viro static int ip_clear_mutable_options(struct iphdr *iph, __be32 *daddr) 191da177e4SLinus Torvalds { 201da177e4SLinus Torvalds unsigned char * optptr = (unsigned char*)(iph+1); 211da177e4SLinus Torvalds int l = iph->ihl*4 - sizeof(struct iphdr); 221da177e4SLinus Torvalds int optlen; 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds while (l > 0) { 251da177e4SLinus Torvalds switch (*optptr) { 261da177e4SLinus Torvalds case IPOPT_END: 271da177e4SLinus Torvalds return 0; 281da177e4SLinus Torvalds case IPOPT_NOOP: 291da177e4SLinus Torvalds l--; 301da177e4SLinus Torvalds optptr++; 311da177e4SLinus Torvalds continue; 321da177e4SLinus Torvalds } 331da177e4SLinus Torvalds optlen = optptr[1]; 341da177e4SLinus Torvalds if (optlen<2 || optlen>l) 351da177e4SLinus Torvalds return -EINVAL; 361da177e4SLinus Torvalds switch (*optptr) { 371da177e4SLinus Torvalds case IPOPT_SEC: 381da177e4SLinus Torvalds case 0x85: /* Some "Extended Security" crap. */ 3911a03f78SPaul Moore case IPOPT_CIPSO: 401da177e4SLinus Torvalds case IPOPT_RA: 411da177e4SLinus Torvalds case 0x80|21: /* RFC1770 */ 421da177e4SLinus Torvalds break; 431da177e4SLinus Torvalds case IPOPT_LSRR: 441da177e4SLinus Torvalds case IPOPT_SSRR: 451da177e4SLinus Torvalds if (optlen < 6) 461da177e4SLinus Torvalds return -EINVAL; 471da177e4SLinus Torvalds memcpy(daddr, optptr+optlen-4, 4); 481da177e4SLinus Torvalds /* Fall through */ 491da177e4SLinus Torvalds default: 5096fe1c02SNick Bowler memset(optptr, 0, optlen); 511da177e4SLinus Torvalds } 521da177e4SLinus Torvalds l -= optlen; 531da177e4SLinus Torvalds optptr += optlen; 541da177e4SLinus Torvalds } 551da177e4SLinus Torvalds return 0; 561da177e4SLinus Torvalds } 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds static int ah_output(struct xfrm_state *x, struct sk_buff *skb) 591da177e4SLinus Torvalds { 601da177e4SLinus Torvalds int err; 611da177e4SLinus Torvalds struct iphdr *iph, *top_iph; 621da177e4SLinus Torvalds struct ip_auth_hdr *ah; 631da177e4SLinus Torvalds struct ah_data *ahp; 641da177e4SLinus Torvalds union { 651da177e4SLinus Torvalds struct iphdr iph; 661da177e4SLinus Torvalds char buf[60]; 671da177e4SLinus Torvalds } tmp_iph; 681da177e4SLinus Torvalds 697b277b1aSHerbert Xu skb_push(skb, -skb_network_offset(skb)); 70eddc9ec5SArnaldo Carvalho de Melo top_iph = ip_hdr(skb); 711da177e4SLinus Torvalds iph = &tmp_iph.iph; 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds iph->tos = top_iph->tos; 741da177e4SLinus Torvalds iph->ttl = top_iph->ttl; 751da177e4SLinus Torvalds iph->frag_off = top_iph->frag_off; 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds if (top_iph->ihl != 5) { 781da177e4SLinus Torvalds iph->daddr = top_iph->daddr; 791da177e4SLinus Torvalds memcpy(iph+1, top_iph+1, top_iph->ihl*4 - sizeof(struct iphdr)); 801da177e4SLinus Torvalds err = ip_clear_mutable_options(top_iph, &top_iph->daddr); 811da177e4SLinus Torvalds if (err) 821da177e4SLinus Torvalds goto error; 831da177e4SLinus Torvalds } 841da177e4SLinus Torvalds 8587bdc48dSHerbert Xu ah = ip_auth_hdr(skb); 8637fedd3aSHerbert Xu ah->nexthdr = *skb_mac_header(skb); 8737fedd3aSHerbert Xu *skb_mac_header(skb) = IPPROTO_AH; 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds top_iph->tos = 0; 901da177e4SLinus Torvalds top_iph->tot_len = htons(skb->len); 911da177e4SLinus Torvalds top_iph->frag_off = 0; 921da177e4SLinus Torvalds top_iph->ttl = 0; 931da177e4SLinus Torvalds top_iph->check = 0; 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds ahp = x->data; 9687bdc48dSHerbert Xu ah->hdrlen = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2; 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds ah->reserved = 0; 991da177e4SLinus Torvalds ah->spi = x->id.spi; 100436a0a40SHerbert Xu ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq); 101b7c6538cSHerbert Xu 102b7c6538cSHerbert Xu spin_lock_bh(&x->lock); 10307d4ee58SHerbert Xu err = ah_mac_digest(ahp, skb, ah->auth_data); 104b7c6538cSHerbert Xu memcpy(ah->auth_data, ahp->work_icv, ahp->icv_trunc_len); 105b7c6538cSHerbert Xu spin_unlock_bh(&x->lock); 106b7c6538cSHerbert Xu 10707d4ee58SHerbert Xu if (err) 10807d4ee58SHerbert Xu goto error; 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds top_iph->tos = iph->tos; 1111da177e4SLinus Torvalds top_iph->ttl = iph->ttl; 1121da177e4SLinus Torvalds top_iph->frag_off = iph->frag_off; 1131da177e4SLinus Torvalds if (top_iph->ihl != 5) { 1141da177e4SLinus Torvalds top_iph->daddr = iph->daddr; 1151da177e4SLinus Torvalds memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr)); 1161da177e4SLinus Torvalds } 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds err = 0; 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds error: 1211da177e4SLinus Torvalds return err; 1221da177e4SLinus Torvalds } 1231da177e4SLinus Torvalds 124e695633eSHerbert Xu static int ah_input(struct xfrm_state *x, struct sk_buff *skb) 1251da177e4SLinus Torvalds { 1261da177e4SLinus Torvalds int ah_hlen; 12731a4ab93SHerbert Xu int ihl; 128631a6698SHerbert Xu int nexthdr; 12907d4ee58SHerbert Xu int err = -EINVAL; 1301da177e4SLinus Torvalds struct iphdr *iph; 1311da177e4SLinus Torvalds struct ip_auth_hdr *ah; 1321da177e4SLinus Torvalds struct ah_data *ahp; 1331da177e4SLinus Torvalds char work_buf[60]; 1341da177e4SLinus Torvalds 13587bdc48dSHerbert Xu if (!pskb_may_pull(skb, sizeof(*ah))) 1361da177e4SLinus Torvalds goto out; 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds ah = (struct ip_auth_hdr *)skb->data; 1391da177e4SLinus Torvalds ahp = x->data; 140631a6698SHerbert Xu nexthdr = ah->nexthdr; 1411da177e4SLinus Torvalds ah_hlen = (ah->hdrlen + 2) << 2; 1421da177e4SLinus Torvalds 14387bdc48dSHerbert Xu if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) && 14487bdc48dSHerbert Xu ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len)) 1451da177e4SLinus Torvalds goto out; 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds if (!pskb_may_pull(skb, ah_hlen)) 1481da177e4SLinus Torvalds goto out; 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds /* We are going to _remove_ AH header to keep sockets happy, 1511da177e4SLinus Torvalds * so... Later this can change. */ 1521da177e4SLinus Torvalds if (skb_cloned(skb) && 1531da177e4SLinus Torvalds pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) 1541da177e4SLinus Torvalds goto out; 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds ah = (struct ip_auth_hdr *)skb->data; 159eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 1601da177e4SLinus Torvalds 161d56f90a7SArnaldo Carvalho de Melo ihl = skb->data - skb_network_header(skb); 16231a4ab93SHerbert Xu memcpy(work_buf, iph, ihl); 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds iph->ttl = 0; 1651da177e4SLinus Torvalds iph->tos = 0; 1661da177e4SLinus Torvalds iph->frag_off = 0; 1671da177e4SLinus Torvalds iph->check = 0; 16831a4ab93SHerbert Xu if (ihl > sizeof(*iph)) { 169d5a0a1e3SAl Viro __be32 dummy; 1701da177e4SLinus Torvalds if (ip_clear_mutable_options(iph, &dummy)) 1711da177e4SLinus Torvalds goto out; 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds { 1741da177e4SLinus Torvalds u8 auth_data[MAX_AH_AUTH_LEN]; 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len); 17731a4ab93SHerbert Xu skb_push(skb, ihl); 17807d4ee58SHerbert Xu err = ah_mac_digest(ahp, skb, ah->auth_data); 17907d4ee58SHerbert Xu if (err) 18007d4ee58SHerbert Xu goto out; 18107d4ee58SHerbert Xu err = -EINVAL; 18207d4ee58SHerbert Xu if (memcmp(ahp->work_icv, auth_data, ahp->icv_trunc_len)) { 1831da177e4SLinus Torvalds x->stats.integrity_failed++; 1841da177e4SLinus Torvalds goto out; 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds } 187b0e380b1SArnaldo Carvalho de Melo skb->network_header += ah_hlen; 188badff6d0SArnaldo Carvalho de Melo memcpy(skb_network_header(skb), work_buf, ihl); 189b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header; 19031a4ab93SHerbert Xu __skb_pull(skb, ah_hlen + ihl); 1911da177e4SLinus Torvalds 192631a6698SHerbert Xu return nexthdr; 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds out: 19507d4ee58SHerbert Xu return err; 1961da177e4SLinus Torvalds } 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds static void ah4_err(struct sk_buff *skb, u32 info) 1991da177e4SLinus Torvalds { 2001da177e4SLinus Torvalds struct iphdr *iph = (struct iphdr*)skb->data; 2011da177e4SLinus Torvalds struct ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+(iph->ihl<<2)); 2021da177e4SLinus Torvalds struct xfrm_state *x; 2031da177e4SLinus Torvalds 20488c7664fSArnaldo Carvalho de Melo if (icmp_hdr(skb)->type != ICMP_DEST_UNREACH || 20588c7664fSArnaldo Carvalho de Melo icmp_hdr(skb)->code != ICMP_FRAG_NEEDED) 2061da177e4SLinus Torvalds return; 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET); 2091da177e4SLinus Torvalds if (!x) 2101da177e4SLinus Torvalds return; 2111da177e4SLinus Torvalds printk(KERN_DEBUG "pmtu discovery on SA AH/%08x/%08x\n", 2121da177e4SLinus Torvalds ntohl(ah->spi), ntohl(iph->daddr)); 2131da177e4SLinus Torvalds xfrm_state_put(x); 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds 21672cb6962SHerbert Xu static int ah_init_state(struct xfrm_state *x) 2171da177e4SLinus Torvalds { 2181da177e4SLinus Torvalds struct ah_data *ahp = NULL; 2191da177e4SLinus Torvalds struct xfrm_algo_desc *aalg_desc; 22007d4ee58SHerbert Xu struct crypto_hash *tfm; 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds if (!x->aalg) 2231da177e4SLinus Torvalds goto error; 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds if (x->encap) 2261da177e4SLinus Torvalds goto error; 2271da177e4SLinus Torvalds 2280da974f4SPanagiotis Issaris ahp = kzalloc(sizeof(*ahp), GFP_KERNEL); 2291da177e4SLinus Torvalds if (ahp == NULL) 2301da177e4SLinus Torvalds return -ENOMEM; 2311da177e4SLinus Torvalds 23207d4ee58SHerbert Xu tfm = crypto_alloc_hash(x->aalg->alg_name, 0, CRYPTO_ALG_ASYNC); 23307d4ee58SHerbert Xu if (IS_ERR(tfm)) 2341da177e4SLinus Torvalds goto error; 23507d4ee58SHerbert Xu 23607d4ee58SHerbert Xu ahp->tfm = tfm; 237bc31d3b2SHerbert Xu if (crypto_hash_setkey(tfm, x->aalg->alg_key, 238bc31d3b2SHerbert Xu (x->aalg->alg_key_len + 7) / 8)) 23907d4ee58SHerbert Xu goto error; 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds /* 2421da177e4SLinus Torvalds * Lookup the algorithm description maintained by xfrm_algo, 2431da177e4SLinus Torvalds * verify crypto transform properties, and store information 2441da177e4SLinus Torvalds * we need for AH processing. This lookup cannot fail here 24507d4ee58SHerbert Xu * after a successful crypto_alloc_hash(). 2461da177e4SLinus Torvalds */ 2471da177e4SLinus Torvalds aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0); 2481da177e4SLinus Torvalds BUG_ON(!aalg_desc); 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds if (aalg_desc->uinfo.auth.icv_fullbits/8 != 25107d4ee58SHerbert Xu crypto_hash_digestsize(tfm)) { 2521da177e4SLinus Torvalds printk(KERN_INFO "AH: %s digestsize %u != %hu\n", 25307d4ee58SHerbert Xu x->aalg->alg_name, crypto_hash_digestsize(tfm), 2541da177e4SLinus Torvalds aalg_desc->uinfo.auth.icv_fullbits/8); 2551da177e4SLinus Torvalds goto error; 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8; 2591da177e4SLinus Torvalds ahp->icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8; 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN); 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds ahp->work_icv = kmalloc(ahp->icv_full_len, GFP_KERNEL); 2641da177e4SLinus Torvalds if (!ahp->work_icv) 2651da177e4SLinus Torvalds goto error; 2661da177e4SLinus Torvalds 26787bdc48dSHerbert Xu x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) + 26887bdc48dSHerbert Xu ahp->icv_trunc_len); 2697e49e6deSMasahide NAKAMURA if (x->props.mode == XFRM_MODE_TUNNEL) 2701da177e4SLinus Torvalds x->props.header_len += sizeof(struct iphdr); 2711da177e4SLinus Torvalds x->data = ahp; 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds return 0; 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds error: 2761da177e4SLinus Torvalds if (ahp) { 2771da177e4SLinus Torvalds kfree(ahp->work_icv); 27807d4ee58SHerbert Xu crypto_free_hash(ahp->tfm); 2791da177e4SLinus Torvalds kfree(ahp); 2801da177e4SLinus Torvalds } 2811da177e4SLinus Torvalds return -EINVAL; 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds static void ah_destroy(struct xfrm_state *x) 2851da177e4SLinus Torvalds { 2861da177e4SLinus Torvalds struct ah_data *ahp = x->data; 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds if (!ahp) 2891da177e4SLinus Torvalds return; 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds kfree(ahp->work_icv); 2921da177e4SLinus Torvalds ahp->work_icv = NULL; 29307d4ee58SHerbert Xu crypto_free_hash(ahp->tfm); 2941da177e4SLinus Torvalds ahp->tfm = NULL; 2951da177e4SLinus Torvalds kfree(ahp); 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds static struct xfrm_type ah_type = 3001da177e4SLinus Torvalds { 3011da177e4SLinus Torvalds .description = "AH4", 3021da177e4SLinus Torvalds .owner = THIS_MODULE, 3031da177e4SLinus Torvalds .proto = IPPROTO_AH, 304436a0a40SHerbert Xu .flags = XFRM_TYPE_REPLAY_PROT, 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"); 342d3d6dd3aSMasahide NAKAMURA MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_AH); 343