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 ip_send_check(top_iph); 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds err = 0; 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds error: 1231da177e4SLinus Torvalds return err; 1241da177e4SLinus Torvalds } 1251da177e4SLinus Torvalds 126e695633eSHerbert Xu static int ah_input(struct xfrm_state *x, struct sk_buff *skb) 1271da177e4SLinus Torvalds { 1281da177e4SLinus Torvalds int ah_hlen; 12931a4ab93SHerbert Xu int ihl; 13007d4ee58SHerbert Xu int err = -EINVAL; 1311da177e4SLinus Torvalds struct iphdr *iph; 1321da177e4SLinus Torvalds struct ip_auth_hdr *ah; 1331da177e4SLinus Torvalds struct ah_data *ahp; 1341da177e4SLinus Torvalds char work_buf[60]; 1351da177e4SLinus Torvalds 13687bdc48dSHerbert Xu if (!pskb_may_pull(skb, sizeof(*ah))) 1371da177e4SLinus Torvalds goto out; 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds ah = (struct ip_auth_hdr *)skb->data; 1401da177e4SLinus Torvalds ahp = x->data; 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 } 1871da177e4SLinus Torvalds ((struct iphdr*)work_buf)->protocol = ah->nexthdr; 188b0e380b1SArnaldo Carvalho de Melo skb->network_header += ah_hlen; 189badff6d0SArnaldo Carvalho de Melo memcpy(skb_network_header(skb), work_buf, ihl); 190b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header; 19131a4ab93SHerbert Xu __skb_pull(skb, ah_hlen + ihl); 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds return 0; 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds out: 19607d4ee58SHerbert Xu return err; 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds static void ah4_err(struct sk_buff *skb, u32 info) 2001da177e4SLinus Torvalds { 2011da177e4SLinus Torvalds struct iphdr *iph = (struct iphdr*)skb->data; 2021da177e4SLinus Torvalds struct ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+(iph->ihl<<2)); 2031da177e4SLinus Torvalds struct xfrm_state *x; 2041da177e4SLinus Torvalds 20588c7664fSArnaldo Carvalho de Melo if (icmp_hdr(skb)->type != ICMP_DEST_UNREACH || 20688c7664fSArnaldo Carvalho de Melo icmp_hdr(skb)->code != ICMP_FRAG_NEEDED) 2071da177e4SLinus Torvalds return; 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET); 2101da177e4SLinus Torvalds if (!x) 2111da177e4SLinus Torvalds return; 2121da177e4SLinus Torvalds printk(KERN_DEBUG "pmtu discovery on SA AH/%08x/%08x\n", 2131da177e4SLinus Torvalds ntohl(ah->spi), ntohl(iph->daddr)); 2141da177e4SLinus Torvalds xfrm_state_put(x); 2151da177e4SLinus Torvalds } 2161da177e4SLinus Torvalds 21772cb6962SHerbert Xu static int ah_init_state(struct xfrm_state *x) 2181da177e4SLinus Torvalds { 2191da177e4SLinus Torvalds struct ah_data *ahp = NULL; 2201da177e4SLinus Torvalds struct xfrm_algo_desc *aalg_desc; 22107d4ee58SHerbert Xu struct crypto_hash *tfm; 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds if (!x->aalg) 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 23307d4ee58SHerbert Xu tfm = crypto_alloc_hash(x->aalg->alg_name, 0, CRYPTO_ALG_ASYNC); 23407d4ee58SHerbert Xu if (IS_ERR(tfm)) 2351da177e4SLinus Torvalds goto error; 23607d4ee58SHerbert Xu 23707d4ee58SHerbert Xu ahp->tfm = tfm; 238bc31d3b2SHerbert Xu if (crypto_hash_setkey(tfm, x->aalg->alg_key, 239bc31d3b2SHerbert Xu (x->aalg->alg_key_len + 7) / 8)) 24007d4ee58SHerbert Xu goto error; 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds /* 2431da177e4SLinus Torvalds * Lookup the algorithm description maintained by xfrm_algo, 2441da177e4SLinus Torvalds * verify crypto transform properties, and store information 2451da177e4SLinus Torvalds * we need for AH processing. This lookup cannot fail here 24607d4ee58SHerbert Xu * after a successful crypto_alloc_hash(). 2471da177e4SLinus Torvalds */ 2481da177e4SLinus Torvalds aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0); 2491da177e4SLinus Torvalds BUG_ON(!aalg_desc); 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds if (aalg_desc->uinfo.auth.icv_fullbits/8 != 25207d4ee58SHerbert Xu crypto_hash_digestsize(tfm)) { 2531da177e4SLinus Torvalds printk(KERN_INFO "AH: %s digestsize %u != %hu\n", 25407d4ee58SHerbert Xu x->aalg->alg_name, crypto_hash_digestsize(tfm), 2551da177e4SLinus Torvalds aalg_desc->uinfo.auth.icv_fullbits/8); 2561da177e4SLinus Torvalds goto error; 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8; 2601da177e4SLinus Torvalds ahp->icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8; 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN); 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds ahp->work_icv = kmalloc(ahp->icv_full_len, GFP_KERNEL); 2651da177e4SLinus Torvalds if (!ahp->work_icv) 2661da177e4SLinus Torvalds goto error; 2671da177e4SLinus Torvalds 26887bdc48dSHerbert Xu x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) + 26987bdc48dSHerbert Xu 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, 305436a0a40SHerbert Xu .flags = XFRM_TYPE_REPLAY_PROT, 3061da177e4SLinus Torvalds .init_state = ah_init_state, 3071da177e4SLinus Torvalds .destructor = ah_destroy, 3081da177e4SLinus Torvalds .input = ah_input, 3091da177e4SLinus Torvalds .output = ah_output 3101da177e4SLinus Torvalds }; 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds static struct net_protocol ah4_protocol = { 3131da177e4SLinus Torvalds .handler = xfrm4_rcv, 3141da177e4SLinus Torvalds .err_handler = ah4_err, 3151da177e4SLinus Torvalds .no_policy = 1, 3161da177e4SLinus Torvalds }; 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds static int __init ah4_init(void) 3191da177e4SLinus Torvalds { 3201da177e4SLinus Torvalds if (xfrm_register_type(&ah_type, AF_INET) < 0) { 3211da177e4SLinus Torvalds printk(KERN_INFO "ip ah init: can't add xfrm type\n"); 3221da177e4SLinus Torvalds return -EAGAIN; 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds if (inet_add_protocol(&ah4_protocol, IPPROTO_AH) < 0) { 3251da177e4SLinus Torvalds printk(KERN_INFO "ip ah init: can't add protocol\n"); 3261da177e4SLinus Torvalds xfrm_unregister_type(&ah_type, AF_INET); 3271da177e4SLinus Torvalds return -EAGAIN; 3281da177e4SLinus Torvalds } 3291da177e4SLinus Torvalds return 0; 3301da177e4SLinus Torvalds } 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds static void __exit ah4_fini(void) 3331da177e4SLinus Torvalds { 3341da177e4SLinus Torvalds if (inet_del_protocol(&ah4_protocol, IPPROTO_AH) < 0) 3351da177e4SLinus Torvalds printk(KERN_INFO "ip ah close: can't remove protocol\n"); 3361da177e4SLinus Torvalds if (xfrm_unregister_type(&ah_type, AF_INET) < 0) 3371da177e4SLinus Torvalds printk(KERN_INFO "ip ah close: can't remove xfrm type\n"); 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds module_init(ah4_init); 3411da177e4SLinus Torvalds module_exit(ah4_fini); 3421da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 343d3d6dd3aSMasahide NAKAMURA MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_AH); 344