11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Copyright (C)2002 USAGI/WIDE Project 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 51da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 61da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 71da177e4SLinus Torvalds * (at your option) any later version. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, 101da177e4SLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of 111da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 121da177e4SLinus Torvalds * GNU General Public License for more details. 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License 151da177e4SLinus Torvalds * along with this program; if not, write to the Free Software 161da177e4SLinus Torvalds * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * Authors 191da177e4SLinus Torvalds * 201da177e4SLinus Torvalds * Mitsuru KANDA @USAGI : IPv6 Support 211da177e4SLinus Torvalds * Kazunori MIYAZAWA @USAGI : 221da177e4SLinus Torvalds * Kunihiro Ishiguro <kunihiro@ipinfusion.com> 231da177e4SLinus Torvalds * 241da177e4SLinus Torvalds * This file is derived from net/ipv4/ah.c. 251da177e4SLinus Torvalds */ 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds #include <linux/config.h> 281da177e4SLinus Torvalds #include <linux/module.h> 291da177e4SLinus Torvalds #include <net/ip.h> 301da177e4SLinus Torvalds #include <net/ah.h> 311da177e4SLinus Torvalds #include <linux/crypto.h> 321da177e4SLinus Torvalds #include <linux/pfkeyv2.h> 331da177e4SLinus Torvalds #include <linux/string.h> 341da177e4SLinus Torvalds #include <net/icmp.h> 351da177e4SLinus Torvalds #include <net/ipv6.h> 3614c85021SArnaldo Carvalho de Melo #include <net/protocol.h> 371da177e4SLinus Torvalds #include <net/xfrm.h> 381da177e4SLinus Torvalds #include <asm/scatterlist.h> 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds static int zero_out_mutable_opts(struct ipv6_opt_hdr *opthdr) 411da177e4SLinus Torvalds { 421da177e4SLinus Torvalds u8 *opt = (u8 *)opthdr; 431da177e4SLinus Torvalds int len = ipv6_optlen(opthdr); 441da177e4SLinus Torvalds int off = 0; 451da177e4SLinus Torvalds int optlen = 0; 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds off += 2; 481da177e4SLinus Torvalds len -= 2; 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds while (len > 0) { 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds switch (opt[off]) { 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds case IPV6_TLV_PAD0: 551da177e4SLinus Torvalds optlen = 1; 561da177e4SLinus Torvalds break; 571da177e4SLinus Torvalds default: 581da177e4SLinus Torvalds if (len < 2) 591da177e4SLinus Torvalds goto bad; 601da177e4SLinus Torvalds optlen = opt[off+1]+2; 611da177e4SLinus Torvalds if (len < optlen) 621da177e4SLinus Torvalds goto bad; 631da177e4SLinus Torvalds if (opt[off] & 0x20) 641da177e4SLinus Torvalds memset(&opt[off+2], 0, opt[off+1]); 651da177e4SLinus Torvalds break; 661da177e4SLinus Torvalds } 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds off += optlen; 691da177e4SLinus Torvalds len -= optlen; 701da177e4SLinus Torvalds } 711da177e4SLinus Torvalds if (len == 0) 721da177e4SLinus Torvalds return 1; 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds bad: 751da177e4SLinus Torvalds return 0; 761da177e4SLinus Torvalds } 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds /** 791da177e4SLinus Torvalds * ipv6_rearrange_rthdr - rearrange IPv6 routing header 801da177e4SLinus Torvalds * @iph: IPv6 header 811da177e4SLinus Torvalds * @rthdr: routing header 821da177e4SLinus Torvalds * 831da177e4SLinus Torvalds * Rearrange the destination address in @iph and the addresses in @rthdr 841da177e4SLinus Torvalds * so that they appear in the order they will at the final destination. 851da177e4SLinus Torvalds * See Appendix A2 of RFC 2402 for details. 861da177e4SLinus Torvalds */ 871da177e4SLinus Torvalds static void ipv6_rearrange_rthdr(struct ipv6hdr *iph, struct ipv6_rt_hdr *rthdr) 881da177e4SLinus Torvalds { 891da177e4SLinus Torvalds int segments, segments_left; 901da177e4SLinus Torvalds struct in6_addr *addrs; 911da177e4SLinus Torvalds struct in6_addr final_addr; 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds segments_left = rthdr->segments_left; 941da177e4SLinus Torvalds if (segments_left == 0) 951da177e4SLinus Torvalds return; 961da177e4SLinus Torvalds rthdr->segments_left = 0; 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds /* The value of rthdr->hdrlen has been verified either by the system 991da177e4SLinus Torvalds * call if it is locally generated, or by ipv6_rthdr_rcv() for incoming 1001da177e4SLinus Torvalds * packets. So we can assume that it is even and that segments is 1011da177e4SLinus Torvalds * greater than or equal to segments_left. 1021da177e4SLinus Torvalds * 1031da177e4SLinus Torvalds * For the same reason we can assume that this option is of type 0. 1041da177e4SLinus Torvalds */ 1051da177e4SLinus Torvalds segments = rthdr->hdrlen >> 1; 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds addrs = ((struct rt0_hdr *)rthdr)->addr; 1081da177e4SLinus Torvalds ipv6_addr_copy(&final_addr, addrs + segments - 1); 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds addrs += segments - segments_left; 1111da177e4SLinus Torvalds memmove(addrs + 1, addrs, (segments_left - 1) * sizeof(*addrs)); 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds ipv6_addr_copy(addrs, &iph->daddr); 1141da177e4SLinus Torvalds ipv6_addr_copy(&iph->daddr, &final_addr); 1151da177e4SLinus Torvalds } 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds static int ipv6_clear_mutable_options(struct ipv6hdr *iph, int len) 1181da177e4SLinus Torvalds { 1191da177e4SLinus Torvalds union { 1201da177e4SLinus Torvalds struct ipv6hdr *iph; 1211da177e4SLinus Torvalds struct ipv6_opt_hdr *opth; 1221da177e4SLinus Torvalds struct ipv6_rt_hdr *rth; 1231da177e4SLinus Torvalds char *raw; 1241da177e4SLinus Torvalds } exthdr = { .iph = iph }; 1251da177e4SLinus Torvalds char *end = exthdr.raw + len; 1261da177e4SLinus Torvalds int nexthdr = iph->nexthdr; 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds exthdr.iph++; 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds while (exthdr.raw < end) { 1311da177e4SLinus Torvalds switch (nexthdr) { 1321da177e4SLinus Torvalds case NEXTHDR_HOP: 1331da177e4SLinus Torvalds case NEXTHDR_DEST: 1341da177e4SLinus Torvalds if (!zero_out_mutable_opts(exthdr.opth)) { 13564ce2073SPatrick McHardy LIMIT_NETDEBUG( 1361da177e4SLinus Torvalds KERN_WARNING "overrun %sopts\n", 1371da177e4SLinus Torvalds nexthdr == NEXTHDR_HOP ? 13864ce2073SPatrick McHardy "hop" : "dest"); 1391da177e4SLinus Torvalds return -EINVAL; 1401da177e4SLinus Torvalds } 1411da177e4SLinus Torvalds break; 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds case NEXTHDR_ROUTING: 1441da177e4SLinus Torvalds ipv6_rearrange_rthdr(iph, exthdr.rth); 1451da177e4SLinus Torvalds break; 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds default : 1481da177e4SLinus Torvalds return 0; 1491da177e4SLinus Torvalds } 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds nexthdr = exthdr.opth->nexthdr; 1521da177e4SLinus Torvalds exthdr.raw += ipv6_optlen(exthdr.opth); 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds return 0; 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds static int ah6_output(struct xfrm_state *x, struct sk_buff *skb) 1591da177e4SLinus Torvalds { 1601da177e4SLinus Torvalds int err; 1611da177e4SLinus Torvalds int extlen; 1621da177e4SLinus Torvalds struct ipv6hdr *top_iph; 1631da177e4SLinus Torvalds struct ip_auth_hdr *ah; 1641da177e4SLinus Torvalds struct ah_data *ahp; 1651da177e4SLinus Torvalds u8 nexthdr; 1661da177e4SLinus Torvalds char tmp_base[8]; 1671da177e4SLinus Torvalds struct { 1681da177e4SLinus Torvalds struct in6_addr daddr; 1691da177e4SLinus Torvalds char hdrs[0]; 1701da177e4SLinus Torvalds } *tmp_ext; 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds top_iph = (struct ipv6hdr *)skb->data; 1731da177e4SLinus Torvalds top_iph->payload_len = htons(skb->len - sizeof(*top_iph)); 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds nexthdr = *skb->nh.raw; 1761da177e4SLinus Torvalds *skb->nh.raw = IPPROTO_AH; 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds /* When there are no extension headers, we only need to save the first 1791da177e4SLinus Torvalds * 8 bytes of the base IP header. 1801da177e4SLinus Torvalds */ 1811da177e4SLinus Torvalds memcpy(tmp_base, top_iph, sizeof(tmp_base)); 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds tmp_ext = NULL; 1841da177e4SLinus Torvalds extlen = skb->h.raw - (unsigned char *)(top_iph + 1); 1851da177e4SLinus Torvalds if (extlen) { 1861da177e4SLinus Torvalds extlen += sizeof(*tmp_ext); 1871da177e4SLinus Torvalds tmp_ext = kmalloc(extlen, GFP_ATOMIC); 1881da177e4SLinus Torvalds if (!tmp_ext) { 1891da177e4SLinus Torvalds err = -ENOMEM; 1901da177e4SLinus Torvalds goto error; 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds memcpy(tmp_ext, &top_iph->daddr, extlen); 1931da177e4SLinus Torvalds err = ipv6_clear_mutable_options(top_iph, 1941da177e4SLinus Torvalds extlen - sizeof(*tmp_ext) + 1951da177e4SLinus Torvalds sizeof(*top_iph)); 1961da177e4SLinus Torvalds if (err) 1971da177e4SLinus Torvalds goto error_free_iph; 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds ah = (struct ip_auth_hdr *)skb->h.raw; 2011da177e4SLinus Torvalds ah->nexthdr = nexthdr; 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds top_iph->priority = 0; 2041da177e4SLinus Torvalds top_iph->flow_lbl[0] = 0; 2051da177e4SLinus Torvalds top_iph->flow_lbl[1] = 0; 2061da177e4SLinus Torvalds top_iph->flow_lbl[2] = 0; 2071da177e4SLinus Torvalds top_iph->hop_limit = 0; 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds ahp = x->data; 2101da177e4SLinus Torvalds ah->hdrlen = (XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + 2111da177e4SLinus Torvalds ahp->icv_trunc_len) >> 2) - 2; 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds ah->reserved = 0; 2141da177e4SLinus Torvalds ah->spi = x->id.spi; 2151da177e4SLinus Torvalds ah->seq_no = htonl(++x->replay.oseq); 2161da177e4SLinus Torvalds ahp->icv(ahp, skb, ah->auth_data); 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds err = 0; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds memcpy(top_iph, tmp_base, sizeof(tmp_base)); 2211da177e4SLinus Torvalds if (tmp_ext) { 2221da177e4SLinus Torvalds memcpy(&top_iph->daddr, tmp_ext, extlen); 2231da177e4SLinus Torvalds error_free_iph: 2241da177e4SLinus Torvalds kfree(tmp_ext); 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds error: 2281da177e4SLinus Torvalds return err; 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds static int ah6_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb) 2321da177e4SLinus Torvalds { 2331da177e4SLinus Torvalds /* 2341da177e4SLinus Torvalds * Before process AH 2351da177e4SLinus Torvalds * [IPv6][Ext1][Ext2][AH][Dest][Payload] 2361da177e4SLinus Torvalds * |<-------------->| hdr_len 2371da177e4SLinus Torvalds * 2381da177e4SLinus Torvalds * To erase AH: 2391da177e4SLinus Torvalds * Keeping copy of cleared headers. After AH processing, 2401da177e4SLinus Torvalds * Moving the pointer of skb->nh.raw by using skb_pull as long as AH 2411da177e4SLinus Torvalds * header length. Then copy back the copy as long as hdr_len 2421da177e4SLinus Torvalds * If destination header following AH exists, copy it into after [Ext2]. 2431da177e4SLinus Torvalds * 2441da177e4SLinus Torvalds * |<>|[IPv6][Ext1][Ext2][Dest][Payload] 2451da177e4SLinus Torvalds * There is offset of AH before IPv6 header after the process. 2461da177e4SLinus Torvalds */ 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds struct ipv6_auth_hdr *ah; 2491da177e4SLinus Torvalds struct ah_data *ahp; 2501da177e4SLinus Torvalds unsigned char *tmp_hdr = NULL; 2511da177e4SLinus Torvalds u16 hdr_len; 2521da177e4SLinus Torvalds u16 ah_hlen; 2531da177e4SLinus Torvalds int nexthdr; 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct ip_auth_hdr))) 2561da177e4SLinus Torvalds goto out; 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds /* We are going to _remove_ AH header to keep sockets happy, 2591da177e4SLinus Torvalds * so... Later this can change. */ 2601da177e4SLinus Torvalds if (skb_cloned(skb) && 2611da177e4SLinus Torvalds pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) 2621da177e4SLinus Torvalds goto out; 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds hdr_len = skb->data - skb->nh.raw; 2651da177e4SLinus Torvalds ah = (struct ipv6_auth_hdr*)skb->data; 2661da177e4SLinus Torvalds ahp = x->data; 2671da177e4SLinus Torvalds nexthdr = ah->nexthdr; 2681da177e4SLinus Torvalds ah_hlen = (ah->hdrlen + 2) << 2; 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds if (ah_hlen != XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + ahp->icv_full_len) && 2711da177e4SLinus Torvalds ah_hlen != XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + ahp->icv_trunc_len)) 2721da177e4SLinus Torvalds goto out; 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds if (!pskb_may_pull(skb, ah_hlen)) 2751da177e4SLinus Torvalds goto out; 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds tmp_hdr = kmalloc(hdr_len, GFP_ATOMIC); 2781da177e4SLinus Torvalds if (!tmp_hdr) 2791da177e4SLinus Torvalds goto out; 2801da177e4SLinus Torvalds memcpy(tmp_hdr, skb->nh.raw, hdr_len); 2811da177e4SLinus Torvalds if (ipv6_clear_mutable_options(skb->nh.ipv6h, hdr_len)) 282f8dc01f5SPatrick McHardy goto free_out; 2831da177e4SLinus Torvalds skb->nh.ipv6h->priority = 0; 2841da177e4SLinus Torvalds skb->nh.ipv6h->flow_lbl[0] = 0; 2851da177e4SLinus Torvalds skb->nh.ipv6h->flow_lbl[1] = 0; 2861da177e4SLinus Torvalds skb->nh.ipv6h->flow_lbl[2] = 0; 2871da177e4SLinus Torvalds skb->nh.ipv6h->hop_limit = 0; 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds { 2901da177e4SLinus Torvalds u8 auth_data[MAX_AH_AUTH_LEN]; 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len); 2931da177e4SLinus Torvalds memset(ah->auth_data, 0, ahp->icv_trunc_len); 2941da177e4SLinus Torvalds skb_push(skb, skb->data - skb->nh.raw); 2951da177e4SLinus Torvalds ahp->icv(ahp, skb, ah->auth_data); 2961da177e4SLinus Torvalds if (memcmp(ah->auth_data, auth_data, ahp->icv_trunc_len)) { 29764ce2073SPatrick McHardy LIMIT_NETDEBUG(KERN_WARNING "ipsec ah authentication error\n"); 2981da177e4SLinus Torvalds x->stats.integrity_failed++; 2991da177e4SLinus Torvalds goto free_out; 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds skb->nh.raw = skb_pull(skb, ah_hlen); 3041da177e4SLinus Torvalds memcpy(skb->nh.raw, tmp_hdr, hdr_len); 3051da177e4SLinus Torvalds skb->nh.ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); 3061da177e4SLinus Torvalds skb_pull(skb, hdr_len); 3071da177e4SLinus Torvalds skb->h.raw = skb->data; 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds kfree(tmp_hdr); 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds return nexthdr; 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds free_out: 3151da177e4SLinus Torvalds kfree(tmp_hdr); 3161da177e4SLinus Torvalds out: 3171da177e4SLinus Torvalds return -EINVAL; 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds static void ah6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, 3211da177e4SLinus Torvalds int type, int code, int offset, __u32 info) 3221da177e4SLinus Torvalds { 3231da177e4SLinus Torvalds struct ipv6hdr *iph = (struct ipv6hdr*)skb->data; 3241da177e4SLinus Torvalds struct ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+offset); 3251da177e4SLinus Torvalds struct xfrm_state *x; 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds if (type != ICMPV6_DEST_UNREACH && 3281da177e4SLinus Torvalds type != ICMPV6_PKT_TOOBIG) 3291da177e4SLinus Torvalds return; 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET6); 3321da177e4SLinus Torvalds if (!x) 3331da177e4SLinus Torvalds return; 3341da177e4SLinus Torvalds 33546b86a2dSJoe Perches NETDEBUG(KERN_DEBUG "pmtu discovery on SA AH/%08x/" NIP6_FMT "\n", 33664ce2073SPatrick McHardy ntohl(ah->spi), NIP6(iph->daddr)); 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds xfrm_state_put(x); 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds 34172cb6962SHerbert Xu static int ah6_init_state(struct xfrm_state *x) 3421da177e4SLinus Torvalds { 3431da177e4SLinus Torvalds struct ah_data *ahp = NULL; 3441da177e4SLinus Torvalds struct xfrm_algo_desc *aalg_desc; 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds if (!x->aalg) 3471da177e4SLinus Torvalds goto error; 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds /* null auth can use a zero length key */ 3501da177e4SLinus Torvalds if (x->aalg->alg_key_len > 512) 3511da177e4SLinus Torvalds goto error; 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds if (x->encap) 3541da177e4SLinus Torvalds goto error; 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds ahp = kmalloc(sizeof(*ahp), GFP_KERNEL); 3571da177e4SLinus Torvalds if (ahp == NULL) 3581da177e4SLinus Torvalds return -ENOMEM; 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds memset(ahp, 0, sizeof(*ahp)); 3611da177e4SLinus Torvalds 3621da177e4SLinus Torvalds ahp->key = x->aalg->alg_key; 3631da177e4SLinus Torvalds ahp->key_len = (x->aalg->alg_key_len+7)/8; 3641da177e4SLinus Torvalds ahp->tfm = crypto_alloc_tfm(x->aalg->alg_name, 0); 3651da177e4SLinus Torvalds if (!ahp->tfm) 3661da177e4SLinus Torvalds goto error; 3671da177e4SLinus Torvalds ahp->icv = ah_hmac_digest; 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds /* 3701da177e4SLinus Torvalds * Lookup the algorithm description maintained by xfrm_algo, 3711da177e4SLinus Torvalds * verify crypto transform properties, and store information 3721da177e4SLinus Torvalds * we need for AH processing. This lookup cannot fail here 3731da177e4SLinus Torvalds * after a successful crypto_alloc_tfm(). 3741da177e4SLinus Torvalds */ 3751da177e4SLinus Torvalds aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0); 3761da177e4SLinus Torvalds BUG_ON(!aalg_desc); 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds if (aalg_desc->uinfo.auth.icv_fullbits/8 != 3791da177e4SLinus Torvalds crypto_tfm_alg_digestsize(ahp->tfm)) { 3801da177e4SLinus Torvalds printk(KERN_INFO "AH: %s digestsize %u != %hu\n", 3811da177e4SLinus Torvalds x->aalg->alg_name, crypto_tfm_alg_digestsize(ahp->tfm), 3821da177e4SLinus Torvalds aalg_desc->uinfo.auth.icv_fullbits/8); 3831da177e4SLinus Torvalds goto error; 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8; 3871da177e4SLinus Torvalds ahp->icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8; 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN); 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds ahp->work_icv = kmalloc(ahp->icv_full_len, GFP_KERNEL); 3921da177e4SLinus Torvalds if (!ahp->work_icv) 3931da177e4SLinus Torvalds goto error; 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds x->props.header_len = XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + ahp->icv_trunc_len); 3961da177e4SLinus Torvalds if (x->props.mode) 3971da177e4SLinus Torvalds x->props.header_len += sizeof(struct ipv6hdr); 3981da177e4SLinus Torvalds x->data = ahp; 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds return 0; 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds error: 4031da177e4SLinus Torvalds if (ahp) { 4041da177e4SLinus Torvalds kfree(ahp->work_icv); 4051da177e4SLinus Torvalds crypto_free_tfm(ahp->tfm); 4061da177e4SLinus Torvalds kfree(ahp); 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds return -EINVAL; 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds static void ah6_destroy(struct xfrm_state *x) 4121da177e4SLinus Torvalds { 4131da177e4SLinus Torvalds struct ah_data *ahp = x->data; 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds if (!ahp) 4161da177e4SLinus Torvalds return; 4171da177e4SLinus Torvalds 4181da177e4SLinus Torvalds kfree(ahp->work_icv); 4191da177e4SLinus Torvalds ahp->work_icv = NULL; 4201da177e4SLinus Torvalds crypto_free_tfm(ahp->tfm); 4211da177e4SLinus Torvalds ahp->tfm = NULL; 4221da177e4SLinus Torvalds kfree(ahp); 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds static struct xfrm_type ah6_type = 4261da177e4SLinus Torvalds { 4271da177e4SLinus Torvalds .description = "AH6", 4281da177e4SLinus Torvalds .owner = THIS_MODULE, 4291da177e4SLinus Torvalds .proto = IPPROTO_AH, 4301da177e4SLinus Torvalds .init_state = ah6_init_state, 4311da177e4SLinus Torvalds .destructor = ah6_destroy, 4321da177e4SLinus Torvalds .input = ah6_input, 4331da177e4SLinus Torvalds .output = ah6_output 4341da177e4SLinus Torvalds }; 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds static struct inet6_protocol ah6_protocol = { 4371da177e4SLinus Torvalds .handler = xfrm6_rcv, 4381da177e4SLinus Torvalds .err_handler = ah6_err, 4391da177e4SLinus Torvalds .flags = INET6_PROTO_NOPOLICY, 4401da177e4SLinus Torvalds }; 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds static int __init ah6_init(void) 4431da177e4SLinus Torvalds { 4441da177e4SLinus Torvalds if (xfrm_register_type(&ah6_type, AF_INET6) < 0) { 4451da177e4SLinus Torvalds printk(KERN_INFO "ipv6 ah init: can't add xfrm type\n"); 4461da177e4SLinus Torvalds return -EAGAIN; 4471da177e4SLinus Torvalds } 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds if (inet6_add_protocol(&ah6_protocol, IPPROTO_AH) < 0) { 4501da177e4SLinus Torvalds printk(KERN_INFO "ipv6 ah init: can't add protocol\n"); 4511da177e4SLinus Torvalds xfrm_unregister_type(&ah6_type, AF_INET6); 4521da177e4SLinus Torvalds return -EAGAIN; 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds return 0; 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds static void __exit ah6_fini(void) 4591da177e4SLinus Torvalds { 4601da177e4SLinus Torvalds if (inet6_del_protocol(&ah6_protocol, IPPROTO_AH) < 0) 4611da177e4SLinus Torvalds printk(KERN_INFO "ipv6 ah close: can't remove protocol\n"); 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds if (xfrm_unregister_type(&ah6_type, AF_INET6) < 0) 4641da177e4SLinus Torvalds printk(KERN_INFO "ipv6 ah close: can't remove xfrm type\n"); 4651da177e4SLinus Torvalds 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds module_init(ah6_init); 4691da177e4SLinus Torvalds module_exit(ah6_fini); 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 472