11da177e4SLinus Torvalds #include <linux/config.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 171da177e4SLinus Torvalds static int ip_clear_mutable_options(struct iphdr *iph, u32 *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. */ 381da177e4SLinus Torvalds case 0x86: /* Another "Commercial Security" crap. */ 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 681da177e4SLinus Torvalds top_iph = skb->nh.iph; 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); 1011da177e4SLinus Torvalds ahp->icv(ahp, skb, ah->auth_data); 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds top_iph->tos = iph->tos; 1041da177e4SLinus Torvalds top_iph->ttl = iph->ttl; 1051da177e4SLinus Torvalds top_iph->frag_off = iph->frag_off; 1061da177e4SLinus Torvalds if (top_iph->ihl != 5) { 1071da177e4SLinus Torvalds top_iph->daddr = iph->daddr; 1081da177e4SLinus Torvalds memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr)); 1091da177e4SLinus Torvalds } 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds ip_send_check(top_iph); 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds err = 0; 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds error: 1161da177e4SLinus Torvalds return err; 1171da177e4SLinus Torvalds } 1181da177e4SLinus Torvalds 119e695633eSHerbert Xu static int ah_input(struct xfrm_state *x, struct sk_buff *skb) 1201da177e4SLinus Torvalds { 1211da177e4SLinus Torvalds int ah_hlen; 1221da177e4SLinus Torvalds struct iphdr *iph; 1231da177e4SLinus Torvalds struct ip_auth_hdr *ah; 1241da177e4SLinus Torvalds struct ah_data *ahp; 1251da177e4SLinus Torvalds char work_buf[60]; 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct ip_auth_hdr))) 1281da177e4SLinus Torvalds goto out; 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds ah = (struct ip_auth_hdr*)skb->data; 1311da177e4SLinus Torvalds ahp = x->data; 1321da177e4SLinus Torvalds ah_hlen = (ah->hdrlen + 2) << 2; 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds if (ah_hlen != XFRM_ALIGN8(sizeof(struct ip_auth_hdr) + ahp->icv_full_len) && 1351da177e4SLinus Torvalds ah_hlen != XFRM_ALIGN8(sizeof(struct ip_auth_hdr) + ahp->icv_trunc_len)) 1361da177e4SLinus Torvalds goto out; 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds if (!pskb_may_pull(skb, ah_hlen)) 1391da177e4SLinus Torvalds goto out; 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds /* We are going to _remove_ AH header to keep sockets happy, 1421da177e4SLinus Torvalds * so... Later this can change. */ 1431da177e4SLinus Torvalds if (skb_cloned(skb) && 1441da177e4SLinus Torvalds pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) 1451da177e4SLinus Torvalds goto out; 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds ah = (struct ip_auth_hdr*)skb->data; 1501da177e4SLinus Torvalds iph = skb->nh.iph; 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds memcpy(work_buf, iph, iph->ihl*4); 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds iph->ttl = 0; 1551da177e4SLinus Torvalds iph->tos = 0; 1561da177e4SLinus Torvalds iph->frag_off = 0; 1571da177e4SLinus Torvalds iph->check = 0; 1581da177e4SLinus Torvalds if (iph->ihl != 5) { 1591da177e4SLinus Torvalds u32 dummy; 1601da177e4SLinus Torvalds if (ip_clear_mutable_options(iph, &dummy)) 1611da177e4SLinus Torvalds goto out; 1621da177e4SLinus Torvalds } 1631da177e4SLinus Torvalds { 1641da177e4SLinus Torvalds u8 auth_data[MAX_AH_AUTH_LEN]; 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len); 1671da177e4SLinus Torvalds skb_push(skb, skb->data - skb->nh.raw); 1681da177e4SLinus Torvalds ahp->icv(ahp, skb, ah->auth_data); 1691da177e4SLinus Torvalds if (memcmp(ah->auth_data, auth_data, ahp->icv_trunc_len)) { 1701da177e4SLinus Torvalds x->stats.integrity_failed++; 1711da177e4SLinus Torvalds goto out; 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds ((struct iphdr*)work_buf)->protocol = ah->nexthdr; 1751da177e4SLinus Torvalds skb->nh.raw = skb_pull(skb, ah_hlen); 1761da177e4SLinus Torvalds memcpy(skb->nh.raw, work_buf, iph->ihl*4); 1771da177e4SLinus Torvalds skb->nh.iph->tot_len = htons(skb->len); 1781da177e4SLinus Torvalds skb_pull(skb, skb->nh.iph->ihl*4); 1791da177e4SLinus Torvalds skb->h.raw = skb->data; 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds return 0; 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds out: 1841da177e4SLinus Torvalds return -EINVAL; 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds static void ah4_err(struct sk_buff *skb, u32 info) 1881da177e4SLinus Torvalds { 1891da177e4SLinus Torvalds struct iphdr *iph = (struct iphdr*)skb->data; 1901da177e4SLinus Torvalds struct ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+(iph->ihl<<2)); 1911da177e4SLinus Torvalds struct xfrm_state *x; 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds if (skb->h.icmph->type != ICMP_DEST_UNREACH || 1941da177e4SLinus Torvalds skb->h.icmph->code != ICMP_FRAG_NEEDED) 1951da177e4SLinus Torvalds return; 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET); 1981da177e4SLinus Torvalds if (!x) 1991da177e4SLinus Torvalds return; 2001da177e4SLinus Torvalds printk(KERN_DEBUG "pmtu discovery on SA AH/%08x/%08x\n", 2011da177e4SLinus Torvalds ntohl(ah->spi), ntohl(iph->daddr)); 2021da177e4SLinus Torvalds xfrm_state_put(x); 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds 20572cb6962SHerbert Xu static int ah_init_state(struct xfrm_state *x) 2061da177e4SLinus Torvalds { 2071da177e4SLinus Torvalds struct ah_data *ahp = NULL; 2081da177e4SLinus Torvalds struct xfrm_algo_desc *aalg_desc; 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds if (!x->aalg) 2111da177e4SLinus Torvalds goto error; 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds /* null auth can use a zero length key */ 2141da177e4SLinus Torvalds if (x->aalg->alg_key_len > 512) 2151da177e4SLinus Torvalds goto error; 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds if (x->encap) 2181da177e4SLinus Torvalds goto error; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds ahp = kmalloc(sizeof(*ahp), GFP_KERNEL); 2211da177e4SLinus Torvalds if (ahp == NULL) 2221da177e4SLinus Torvalds return -ENOMEM; 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds memset(ahp, 0, sizeof(*ahp)); 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds ahp->key = x->aalg->alg_key; 2271da177e4SLinus Torvalds ahp->key_len = (x->aalg->alg_key_len+7)/8; 2281da177e4SLinus Torvalds ahp->tfm = crypto_alloc_tfm(x->aalg->alg_name, 0); 2291da177e4SLinus Torvalds if (!ahp->tfm) 2301da177e4SLinus Torvalds goto error; 2311da177e4SLinus Torvalds ahp->icv = ah_hmac_digest; 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds /* 2341da177e4SLinus Torvalds * Lookup the algorithm description maintained by xfrm_algo, 2351da177e4SLinus Torvalds * verify crypto transform properties, and store information 2361da177e4SLinus Torvalds * we need for AH processing. This lookup cannot fail here 2371da177e4SLinus Torvalds * after a successful crypto_alloc_tfm(). 2381da177e4SLinus Torvalds */ 2391da177e4SLinus Torvalds aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0); 2401da177e4SLinus Torvalds BUG_ON(!aalg_desc); 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds if (aalg_desc->uinfo.auth.icv_fullbits/8 != 2431da177e4SLinus Torvalds crypto_tfm_alg_digestsize(ahp->tfm)) { 2441da177e4SLinus Torvalds printk(KERN_INFO "AH: %s digestsize %u != %hu\n", 2451da177e4SLinus Torvalds x->aalg->alg_name, crypto_tfm_alg_digestsize(ahp->tfm), 2461da177e4SLinus Torvalds aalg_desc->uinfo.auth.icv_fullbits/8); 2471da177e4SLinus Torvalds goto error; 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8; 2511da177e4SLinus Torvalds ahp->icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8; 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN); 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds ahp->work_icv = kmalloc(ahp->icv_full_len, GFP_KERNEL); 2561da177e4SLinus Torvalds if (!ahp->work_icv) 2571da177e4SLinus Torvalds goto error; 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) + ahp->icv_trunc_len); 2601da177e4SLinus Torvalds if (x->props.mode) 2611da177e4SLinus Torvalds x->props.header_len += sizeof(struct iphdr); 2621da177e4SLinus Torvalds x->data = ahp; 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds return 0; 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds error: 2671da177e4SLinus Torvalds if (ahp) { 2681da177e4SLinus Torvalds kfree(ahp->work_icv); 2691da177e4SLinus Torvalds crypto_free_tfm(ahp->tfm); 2701da177e4SLinus Torvalds kfree(ahp); 2711da177e4SLinus Torvalds } 2721da177e4SLinus Torvalds return -EINVAL; 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds static void ah_destroy(struct xfrm_state *x) 2761da177e4SLinus Torvalds { 2771da177e4SLinus Torvalds struct ah_data *ahp = x->data; 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds if (!ahp) 2801da177e4SLinus Torvalds return; 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds kfree(ahp->work_icv); 2831da177e4SLinus Torvalds ahp->work_icv = NULL; 2841da177e4SLinus Torvalds crypto_free_tfm(ahp->tfm); 2851da177e4SLinus Torvalds ahp->tfm = NULL; 2861da177e4SLinus Torvalds kfree(ahp); 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds static struct xfrm_type ah_type = 2911da177e4SLinus Torvalds { 2921da177e4SLinus Torvalds .description = "AH4", 2931da177e4SLinus Torvalds .owner = THIS_MODULE, 2941da177e4SLinus Torvalds .proto = IPPROTO_AH, 2951da177e4SLinus Torvalds .init_state = ah_init_state, 2961da177e4SLinus Torvalds .destructor = ah_destroy, 2971da177e4SLinus Torvalds .input = ah_input, 2981da177e4SLinus Torvalds .output = ah_output 2991da177e4SLinus Torvalds }; 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds static struct net_protocol ah4_protocol = { 3021da177e4SLinus Torvalds .handler = xfrm4_rcv, 3031da177e4SLinus Torvalds .err_handler = ah4_err, 3041da177e4SLinus Torvalds .no_policy = 1, 3051da177e4SLinus Torvalds }; 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds static int __init ah4_init(void) 3081da177e4SLinus Torvalds { 3091da177e4SLinus Torvalds if (xfrm_register_type(&ah_type, AF_INET) < 0) { 3101da177e4SLinus Torvalds printk(KERN_INFO "ip ah init: can't add xfrm type\n"); 3111da177e4SLinus Torvalds return -EAGAIN; 3121da177e4SLinus Torvalds } 3131da177e4SLinus Torvalds if (inet_add_protocol(&ah4_protocol, IPPROTO_AH) < 0) { 3141da177e4SLinus Torvalds printk(KERN_INFO "ip ah init: can't add protocol\n"); 3151da177e4SLinus Torvalds xfrm_unregister_type(&ah_type, AF_INET); 3161da177e4SLinus Torvalds return -EAGAIN; 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds return 0; 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds static void __exit ah4_fini(void) 3221da177e4SLinus Torvalds { 3231da177e4SLinus Torvalds if (inet_del_protocol(&ah4_protocol, IPPROTO_AH) < 0) 3241da177e4SLinus Torvalds printk(KERN_INFO "ip ah close: can't remove protocol\n"); 3251da177e4SLinus Torvalds if (xfrm_unregister_type(&ah_type, AF_INET) < 0) 3261da177e4SLinus Torvalds printk(KERN_INFO "ip ah close: can't remove xfrm type\n"); 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds module_init(ah4_init); 3301da177e4SLinus Torvalds module_exit(ah4_fini); 3311da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 332