1afd46503SJoe Perches #define pr_fmt(fmt) "IPsec: " fmt 2afd46503SJoe Perches 3dff3bb06SSteffen Klassert #include <crypto/hash.h> 407d4ee58SHerbert Xu #include <linux/err.h> 51da177e4SLinus Torvalds #include <linux/module.h> 65a0e3ad6STejun Heo #include <linux/slab.h> 71da177e4SLinus Torvalds #include <net/ip.h> 81da177e4SLinus Torvalds #include <net/xfrm.h> 91da177e4SLinus Torvalds #include <net/ah.h> 101da177e4SLinus Torvalds #include <linux/crypto.h> 111da177e4SLinus Torvalds #include <linux/pfkeyv2.h> 12dff3bb06SSteffen Klassert #include <linux/scatterlist.h> 131da177e4SLinus Torvalds #include <net/icmp.h> 1414c85021SArnaldo Carvalho de Melo #include <net/protocol.h> 151da177e4SLinus Torvalds 16dff3bb06SSteffen Klassert struct ah_skb_cb { 17dff3bb06SSteffen Klassert struct xfrm_skb_cb xfrm; 18dff3bb06SSteffen Klassert void *tmp; 19dff3bb06SSteffen Klassert }; 20dff3bb06SSteffen Klassert 21dff3bb06SSteffen Klassert #define AH_SKB_CB(__skb) ((struct ah_skb_cb *)&((__skb)->cb[0])) 22dff3bb06SSteffen Klassert 23dff3bb06SSteffen Klassert static void *ah_alloc_tmp(struct crypto_ahash *ahash, int nfrags, 24dff3bb06SSteffen Klassert unsigned int size) 25dff3bb06SSteffen Klassert { 26dff3bb06SSteffen Klassert unsigned int len; 27dff3bb06SSteffen Klassert 28dff3bb06SSteffen Klassert len = size + crypto_ahash_digestsize(ahash) + 29dff3bb06SSteffen Klassert (crypto_ahash_alignmask(ahash) & 30dff3bb06SSteffen Klassert ~(crypto_tfm_ctx_alignment() - 1)); 31dff3bb06SSteffen Klassert 32dff3bb06SSteffen Klassert len = ALIGN(len, crypto_tfm_ctx_alignment()); 33dff3bb06SSteffen Klassert 34dff3bb06SSteffen Klassert len += sizeof(struct ahash_request) + crypto_ahash_reqsize(ahash); 35dff3bb06SSteffen Klassert len = ALIGN(len, __alignof__(struct scatterlist)); 36dff3bb06SSteffen Klassert 37dff3bb06SSteffen Klassert len += sizeof(struct scatterlist) * nfrags; 38dff3bb06SSteffen Klassert 39dff3bb06SSteffen Klassert return kmalloc(len, GFP_ATOMIC); 40dff3bb06SSteffen Klassert } 41dff3bb06SSteffen Klassert 42dff3bb06SSteffen Klassert static inline u8 *ah_tmp_auth(void *tmp, unsigned int offset) 43dff3bb06SSteffen Klassert { 44dff3bb06SSteffen Klassert return tmp + offset; 45dff3bb06SSteffen Klassert } 46dff3bb06SSteffen Klassert 47dff3bb06SSteffen Klassert static inline u8 *ah_tmp_icv(struct crypto_ahash *ahash, void *tmp, 48dff3bb06SSteffen Klassert unsigned int offset) 49dff3bb06SSteffen Klassert { 50dff3bb06SSteffen Klassert return PTR_ALIGN((u8 *)tmp + offset, crypto_ahash_alignmask(ahash) + 1); 51dff3bb06SSteffen Klassert } 52dff3bb06SSteffen Klassert 53dff3bb06SSteffen Klassert static inline struct ahash_request *ah_tmp_req(struct crypto_ahash *ahash, 54dff3bb06SSteffen Klassert u8 *icv) 55dff3bb06SSteffen Klassert { 56dff3bb06SSteffen Klassert struct ahash_request *req; 57dff3bb06SSteffen Klassert 58dff3bb06SSteffen Klassert req = (void *)PTR_ALIGN(icv + crypto_ahash_digestsize(ahash), 59dff3bb06SSteffen Klassert crypto_tfm_ctx_alignment()); 60dff3bb06SSteffen Klassert 61dff3bb06SSteffen Klassert ahash_request_set_tfm(req, ahash); 62dff3bb06SSteffen Klassert 63dff3bb06SSteffen Klassert return req; 64dff3bb06SSteffen Klassert } 65dff3bb06SSteffen Klassert 66dff3bb06SSteffen Klassert static inline struct scatterlist *ah_req_sg(struct crypto_ahash *ahash, 67dff3bb06SSteffen Klassert struct ahash_request *req) 68dff3bb06SSteffen Klassert { 69dff3bb06SSteffen Klassert return (void *)ALIGN((unsigned long)(req + 1) + 70dff3bb06SSteffen Klassert crypto_ahash_reqsize(ahash), 71dff3bb06SSteffen Klassert __alignof__(struct scatterlist)); 72dff3bb06SSteffen Klassert } 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds /* Clear mutable options and find final destination to substitute 751da177e4SLinus Torvalds * into IP header for icv calculation. Options are already checked 761da177e4SLinus Torvalds * for validity, so paranoia is not required. */ 771da177e4SLinus Torvalds 78b71d1d42SEric Dumazet static int ip_clear_mutable_options(const struct iphdr *iph, __be32 *daddr) 791da177e4SLinus Torvalds { 801da177e4SLinus Torvalds unsigned char *optptr = (unsigned char *)(iph+1); 811da177e4SLinus Torvalds int l = iph->ihl*4 - sizeof(struct iphdr); 821da177e4SLinus Torvalds int optlen; 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds while (l > 0) { 851da177e4SLinus Torvalds switch (*optptr) { 861da177e4SLinus Torvalds case IPOPT_END: 871da177e4SLinus Torvalds return 0; 881da177e4SLinus Torvalds case IPOPT_NOOP: 891da177e4SLinus Torvalds l--; 901da177e4SLinus Torvalds optptr++; 911da177e4SLinus Torvalds continue; 921da177e4SLinus Torvalds } 931da177e4SLinus Torvalds optlen = optptr[1]; 941da177e4SLinus Torvalds if (optlen<2 || optlen>l) 951da177e4SLinus Torvalds return -EINVAL; 961da177e4SLinus Torvalds switch (*optptr) { 971da177e4SLinus Torvalds case IPOPT_SEC: 981da177e4SLinus Torvalds case 0x85: /* Some "Extended Security" crap. */ 9911a03f78SPaul Moore case IPOPT_CIPSO: 1001da177e4SLinus Torvalds case IPOPT_RA: 1011da177e4SLinus Torvalds case 0x80|21: /* RFC1770 */ 1021da177e4SLinus Torvalds break; 1031da177e4SLinus Torvalds case IPOPT_LSRR: 1041da177e4SLinus Torvalds case IPOPT_SSRR: 1051da177e4SLinus Torvalds if (optlen < 6) 1061da177e4SLinus Torvalds return -EINVAL; 1071da177e4SLinus Torvalds memcpy(daddr, optptr+optlen-4, 4); 1081da177e4SLinus Torvalds /* Fall through */ 1091da177e4SLinus Torvalds default: 11096fe1c02SNick Bowler memset(optptr, 0, optlen); 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds l -= optlen; 1131da177e4SLinus Torvalds optptr += optlen; 1141da177e4SLinus Torvalds } 1151da177e4SLinus Torvalds return 0; 1161da177e4SLinus Torvalds } 1171da177e4SLinus Torvalds 118dff3bb06SSteffen Klassert static void ah_output_done(struct crypto_async_request *base, int err) 119dff3bb06SSteffen Klassert { 120dff3bb06SSteffen Klassert u8 *icv; 121dff3bb06SSteffen Klassert struct iphdr *iph; 122dff3bb06SSteffen Klassert struct sk_buff *skb = base->data; 123dff3bb06SSteffen Klassert struct xfrm_state *x = skb_dst(skb)->xfrm; 124dff3bb06SSteffen Klassert struct ah_data *ahp = x->data; 125dff3bb06SSteffen Klassert struct iphdr *top_iph = ip_hdr(skb); 126dff3bb06SSteffen Klassert struct ip_auth_hdr *ah = ip_auth_hdr(skb); 127dff3bb06SSteffen Klassert int ihl = ip_hdrlen(skb); 128dff3bb06SSteffen Klassert 129dff3bb06SSteffen Klassert iph = AH_SKB_CB(skb)->tmp; 130dff3bb06SSteffen Klassert icv = ah_tmp_icv(ahp->ahash, iph, ihl); 131dff3bb06SSteffen Klassert memcpy(ah->auth_data, icv, ahp->icv_trunc_len); 132dff3bb06SSteffen Klassert 133dff3bb06SSteffen Klassert top_iph->tos = iph->tos; 134dff3bb06SSteffen Klassert top_iph->ttl = iph->ttl; 135dff3bb06SSteffen Klassert top_iph->frag_off = iph->frag_off; 136dff3bb06SSteffen Klassert if (top_iph->ihl != 5) { 137dff3bb06SSteffen Klassert top_iph->daddr = iph->daddr; 138dff3bb06SSteffen Klassert memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr)); 139dff3bb06SSteffen Klassert } 140dff3bb06SSteffen Klassert 141dff3bb06SSteffen Klassert kfree(AH_SKB_CB(skb)->tmp); 142dff3bb06SSteffen Klassert xfrm_output_resume(skb, err); 143dff3bb06SSteffen Klassert } 144dff3bb06SSteffen Klassert 1451da177e4SLinus Torvalds static int ah_output(struct xfrm_state *x, struct sk_buff *skb) 1461da177e4SLinus Torvalds { 1471da177e4SLinus Torvalds int err; 148dff3bb06SSteffen Klassert int nfrags; 149dff3bb06SSteffen Klassert int ihl; 150dff3bb06SSteffen Klassert u8 *icv; 151dff3bb06SSteffen Klassert struct sk_buff *trailer; 152dff3bb06SSteffen Klassert struct crypto_ahash *ahash; 153dff3bb06SSteffen Klassert struct ahash_request *req; 154dff3bb06SSteffen Klassert struct scatterlist *sg; 1551da177e4SLinus Torvalds struct iphdr *iph, *top_iph; 1561da177e4SLinus Torvalds struct ip_auth_hdr *ah; 1571da177e4SLinus Torvalds struct ah_data *ahp; 158d4d573d0SFan Du int seqhi_len = 0; 159d4d573d0SFan Du __be32 *seqhi; 160d4d573d0SFan Du int sglists = 0; 161d4d573d0SFan Du struct scatterlist *seqhisg; 162dff3bb06SSteffen Klassert 163dff3bb06SSteffen Klassert ahp = x->data; 164dff3bb06SSteffen Klassert ahash = ahp->ahash; 165dff3bb06SSteffen Klassert 166dff3bb06SSteffen Klassert if ((err = skb_cow_data(skb, 0, &trailer)) < 0) 167dff3bb06SSteffen Klassert goto out; 168dff3bb06SSteffen Klassert nfrags = err; 1691da177e4SLinus Torvalds 1707b277b1aSHerbert Xu skb_push(skb, -skb_network_offset(skb)); 171dff3bb06SSteffen Klassert ah = ip_auth_hdr(skb); 172dff3bb06SSteffen Klassert ihl = ip_hdrlen(skb); 173dff3bb06SSteffen Klassert 174d4d573d0SFan Du if (x->props.flags & XFRM_STATE_ESN) { 175d4d573d0SFan Du sglists = 1; 176d4d573d0SFan Du seqhi_len = sizeof(*seqhi); 177d4d573d0SFan Du } 178dff3bb06SSteffen Klassert err = -ENOMEM; 179d4d573d0SFan Du iph = ah_alloc_tmp(ahash, nfrags + sglists, ihl + seqhi_len); 180dff3bb06SSteffen Klassert if (!iph) 181dff3bb06SSteffen Klassert goto out; 182d4d573d0SFan Du seqhi = (__be32 *)((char *)iph + ihl); 183d4d573d0SFan Du icv = ah_tmp_icv(ahash, seqhi, seqhi_len); 184dff3bb06SSteffen Klassert req = ah_tmp_req(ahash, icv); 185dff3bb06SSteffen Klassert sg = ah_req_sg(ahash, req); 186d4d573d0SFan Du seqhisg = sg + nfrags; 187dff3bb06SSteffen Klassert 188dff3bb06SSteffen Klassert memset(ah->auth_data, 0, ahp->icv_trunc_len); 189dff3bb06SSteffen Klassert 190eddc9ec5SArnaldo Carvalho de Melo top_iph = ip_hdr(skb); 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds iph->tos = top_iph->tos; 1931da177e4SLinus Torvalds iph->ttl = top_iph->ttl; 1941da177e4SLinus Torvalds iph->frag_off = top_iph->frag_off; 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds if (top_iph->ihl != 5) { 1971da177e4SLinus Torvalds iph->daddr = top_iph->daddr; 1981da177e4SLinus Torvalds memcpy(iph+1, top_iph+1, top_iph->ihl*4 - sizeof(struct iphdr)); 1991da177e4SLinus Torvalds err = ip_clear_mutable_options(top_iph, &top_iph->daddr); 2001da177e4SLinus Torvalds if (err) 201dff3bb06SSteffen Klassert goto out_free; 2021da177e4SLinus Torvalds } 2031da177e4SLinus Torvalds 20437fedd3aSHerbert Xu ah->nexthdr = *skb_mac_header(skb); 20537fedd3aSHerbert Xu *skb_mac_header(skb) = IPPROTO_AH; 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds top_iph->tos = 0; 2081da177e4SLinus Torvalds top_iph->tot_len = htons(skb->len); 2091da177e4SLinus Torvalds top_iph->frag_off = 0; 2101da177e4SLinus Torvalds top_iph->ttl = 0; 2111da177e4SLinus Torvalds top_iph->check = 0; 2121da177e4SLinus Torvalds 213fa9921e4SNicolas Dichtel if (x->props.flags & XFRM_STATE_ALIGN4) 214fa9921e4SNicolas Dichtel ah->hdrlen = (XFRM_ALIGN4(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2; 215fa9921e4SNicolas Dichtel else 21687bdc48dSHerbert Xu ah->hdrlen = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2; 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds ah->reserved = 0; 2191da177e4SLinus Torvalds ah->spi = x->id.spi; 2201ce3644aSSteffen Klassert ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low); 221b7c6538cSHerbert Xu 222d4d573d0SFan Du sg_init_table(sg, nfrags + sglists); 223d4d573d0SFan Du skb_to_sgvec_nomark(skb, sg, 0, skb->len); 224b7c6538cSHerbert Xu 225d4d573d0SFan Du if (x->props.flags & XFRM_STATE_ESN) { 226d4d573d0SFan Du /* Attach seqhi sg right after packet payload */ 227d4d573d0SFan Du *seqhi = htonl(XFRM_SKB_CB(skb)->seq.output.hi); 228d4d573d0SFan Du sg_set_buf(seqhisg, seqhi, seqhi_len); 229d4d573d0SFan Du } 230d4d573d0SFan Du ahash_request_set_crypt(req, sg, icv, skb->len + seqhi_len); 231dff3bb06SSteffen Klassert ahash_request_set_callback(req, 0, ah_output_done, skb); 232dff3bb06SSteffen Klassert 233dff3bb06SSteffen Klassert AH_SKB_CB(skb)->tmp = iph; 234dff3bb06SSteffen Klassert 235dff3bb06SSteffen Klassert err = crypto_ahash_digest(req); 236dff3bb06SSteffen Klassert if (err) { 237dff3bb06SSteffen Klassert if (err == -EINPROGRESS) 238dff3bb06SSteffen Klassert goto out; 239dff3bb06SSteffen Klassert 240dff3bb06SSteffen Klassert if (err == -EBUSY) 241dff3bb06SSteffen Klassert err = NET_XMIT_DROP; 242dff3bb06SSteffen Klassert goto out_free; 243dff3bb06SSteffen Klassert } 244dff3bb06SSteffen Klassert 245dff3bb06SSteffen Klassert memcpy(ah->auth_data, icv, ahp->icv_trunc_len); 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds top_iph->tos = iph->tos; 2481da177e4SLinus Torvalds top_iph->ttl = iph->ttl; 2491da177e4SLinus Torvalds top_iph->frag_off = iph->frag_off; 2501da177e4SLinus Torvalds if (top_iph->ihl != 5) { 2511da177e4SLinus Torvalds top_iph->daddr = iph->daddr; 2521da177e4SLinus Torvalds memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr)); 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 255dff3bb06SSteffen Klassert out_free: 256dff3bb06SSteffen Klassert kfree(iph); 257dff3bb06SSteffen Klassert out: 2581da177e4SLinus Torvalds return err; 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds 261dff3bb06SSteffen Klassert static void ah_input_done(struct crypto_async_request *base, int err) 262dff3bb06SSteffen Klassert { 263dff3bb06SSteffen Klassert u8 *auth_data; 264dff3bb06SSteffen Klassert u8 *icv; 265dff3bb06SSteffen Klassert struct iphdr *work_iph; 266dff3bb06SSteffen Klassert struct sk_buff *skb = base->data; 267dff3bb06SSteffen Klassert struct xfrm_state *x = xfrm_input_state(skb); 268dff3bb06SSteffen Klassert struct ah_data *ahp = x->data; 269dff3bb06SSteffen Klassert struct ip_auth_hdr *ah = ip_auth_hdr(skb); 270dff3bb06SSteffen Klassert int ihl = ip_hdrlen(skb); 271dff3bb06SSteffen Klassert int ah_hlen = (ah->hdrlen + 2) << 2; 272dff3bb06SSteffen Klassert 273dff3bb06SSteffen Klassert work_iph = AH_SKB_CB(skb)->tmp; 274dff3bb06SSteffen Klassert auth_data = ah_tmp_auth(work_iph, ihl); 275dff3bb06SSteffen Klassert icv = ah_tmp_icv(ahp->ahash, auth_data, ahp->icv_trunc_len); 276dff3bb06SSteffen Klassert 277dff3bb06SSteffen Klassert err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0; 278dff3bb06SSteffen Klassert if (err) 279dff3bb06SSteffen Klassert goto out; 280dff3bb06SSteffen Klassert 281b7ea81a5SNick Bowler err = ah->nexthdr; 282b7ea81a5SNick Bowler 283dff3bb06SSteffen Klassert skb->network_header += ah_hlen; 284dff3bb06SSteffen Klassert memcpy(skb_network_header(skb), work_iph, ihl); 285dff3bb06SSteffen Klassert __skb_pull(skb, ah_hlen + ihl); 2867143dfacSLi RongQing 2877143dfacSLi RongQing if (x->props.mode == XFRM_MODE_TUNNEL) 2887143dfacSLi RongQing skb_reset_transport_header(skb); 2897143dfacSLi RongQing else 290dff3bb06SSteffen Klassert skb_set_transport_header(skb, -ihl); 291dff3bb06SSteffen Klassert out: 292dff3bb06SSteffen Klassert kfree(AH_SKB_CB(skb)->tmp); 293dff3bb06SSteffen Klassert xfrm_input_resume(skb, err); 294dff3bb06SSteffen Klassert } 295dff3bb06SSteffen Klassert 296e695633eSHerbert Xu static int ah_input(struct xfrm_state *x, struct sk_buff *skb) 2971da177e4SLinus Torvalds { 2981da177e4SLinus Torvalds int ah_hlen; 29931a4ab93SHerbert Xu int ihl; 300631a6698SHerbert Xu int nexthdr; 301dff3bb06SSteffen Klassert int nfrags; 302dff3bb06SSteffen Klassert u8 *auth_data; 303dff3bb06SSteffen Klassert u8 *icv; 304dff3bb06SSteffen Klassert struct sk_buff *trailer; 305dff3bb06SSteffen Klassert struct crypto_ahash *ahash; 306dff3bb06SSteffen Klassert struct ahash_request *req; 307dff3bb06SSteffen Klassert struct scatterlist *sg; 308dff3bb06SSteffen Klassert struct iphdr *iph, *work_iph; 3091da177e4SLinus Torvalds struct ip_auth_hdr *ah; 3101da177e4SLinus Torvalds struct ah_data *ahp; 311dff3bb06SSteffen Klassert int err = -ENOMEM; 312d8b2a860SFan Du int seqhi_len = 0; 313d8b2a860SFan Du __be32 *seqhi; 314d8b2a860SFan Du int sglists = 0; 315d8b2a860SFan Du struct scatterlist *seqhisg; 3161da177e4SLinus Torvalds 31787bdc48dSHerbert Xu if (!pskb_may_pull(skb, sizeof(*ah))) 3181da177e4SLinus Torvalds goto out; 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds ah = (struct ip_auth_hdr *)skb->data; 3211da177e4SLinus Torvalds ahp = x->data; 322dff3bb06SSteffen Klassert ahash = ahp->ahash; 323dff3bb06SSteffen Klassert 324631a6698SHerbert Xu nexthdr = ah->nexthdr; 3251da177e4SLinus Torvalds ah_hlen = (ah->hdrlen + 2) << 2; 3261da177e4SLinus Torvalds 327fa9921e4SNicolas Dichtel if (x->props.flags & XFRM_STATE_ALIGN4) { 328fa9921e4SNicolas Dichtel if (ah_hlen != XFRM_ALIGN4(sizeof(*ah) + ahp->icv_full_len) && 329fa9921e4SNicolas Dichtel ah_hlen != XFRM_ALIGN4(sizeof(*ah) + ahp->icv_trunc_len)) 330fa9921e4SNicolas Dichtel goto out; 331fa9921e4SNicolas Dichtel } else { 33287bdc48dSHerbert Xu if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) && 33387bdc48dSHerbert Xu ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len)) 3341da177e4SLinus Torvalds goto out; 335fa9921e4SNicolas Dichtel } 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds if (!pskb_may_pull(skb, ah_hlen)) 3381da177e4SLinus Torvalds goto out; 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds /* We are going to _remove_ AH header to keep sockets happy, 3411da177e4SLinus Torvalds * so... Later this can change. */ 34214bbd6a5SPravin B Shelar if (skb_unclone(skb, GFP_ATOMIC)) 3431da177e4SLinus Torvalds goto out; 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds 348dff3bb06SSteffen Klassert if ((err = skb_cow_data(skb, 0, &trailer)) < 0) 349dff3bb06SSteffen Klassert goto out; 350dff3bb06SSteffen Klassert nfrags = err; 351dff3bb06SSteffen Klassert 3524b0ef1f2SDang Hongwu ah = (struct ip_auth_hdr *)skb->data; 3534b0ef1f2SDang Hongwu iph = ip_hdr(skb); 3544b0ef1f2SDang Hongwu ihl = ip_hdrlen(skb); 3554b0ef1f2SDang Hongwu 356d8b2a860SFan Du if (x->props.flags & XFRM_STATE_ESN) { 357d8b2a860SFan Du sglists = 1; 358d8b2a860SFan Du seqhi_len = sizeof(*seqhi); 359d8b2a860SFan Du } 360d8b2a860SFan Du 361d8b2a860SFan Du work_iph = ah_alloc_tmp(ahash, nfrags + sglists, ihl + 362d8b2a860SFan Du ahp->icv_trunc_len + seqhi_len); 363dff3bb06SSteffen Klassert if (!work_iph) 364dff3bb06SSteffen Klassert goto out; 365dff3bb06SSteffen Klassert 366d8b2a860SFan Du seqhi = (__be32 *)((char *)work_iph + ihl); 367d8b2a860SFan Du auth_data = ah_tmp_auth(seqhi, seqhi_len); 368dff3bb06SSteffen Klassert icv = ah_tmp_icv(ahash, auth_data, ahp->icv_trunc_len); 369dff3bb06SSteffen Klassert req = ah_tmp_req(ahash, icv); 370dff3bb06SSteffen Klassert sg = ah_req_sg(ahash, req); 371d8b2a860SFan Du seqhisg = sg + nfrags; 372dff3bb06SSteffen Klassert 373dff3bb06SSteffen Klassert memcpy(work_iph, iph, ihl); 374dff3bb06SSteffen Klassert memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len); 375dff3bb06SSteffen Klassert memset(ah->auth_data, 0, ahp->icv_trunc_len); 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds iph->ttl = 0; 3781da177e4SLinus Torvalds iph->tos = 0; 3791da177e4SLinus Torvalds iph->frag_off = 0; 3801da177e4SLinus Torvalds iph->check = 0; 38131a4ab93SHerbert Xu if (ihl > sizeof(*iph)) { 382d5a0a1e3SAl Viro __be32 dummy; 383dff3bb06SSteffen Klassert err = ip_clear_mutable_options(iph, &dummy); 384dff3bb06SSteffen Klassert if (err) 385dff3bb06SSteffen Klassert goto out_free; 3861da177e4SLinus Torvalds } 3870ebea8efSHerbert Xu 38831a4ab93SHerbert Xu skb_push(skb, ihl); 3890ebea8efSHerbert Xu 390d8b2a860SFan Du sg_init_table(sg, nfrags + sglists); 391d8b2a860SFan Du skb_to_sgvec_nomark(skb, sg, 0, skb->len); 392dff3bb06SSteffen Klassert 393d8b2a860SFan Du if (x->props.flags & XFRM_STATE_ESN) { 394d8b2a860SFan Du /* Attach seqhi sg right after packet payload */ 395d8b2a860SFan Du *seqhi = XFRM_SKB_CB(skb)->seq.input.hi; 396d8b2a860SFan Du sg_set_buf(seqhisg, seqhi, seqhi_len); 397d8b2a860SFan Du } 398d8b2a860SFan Du ahash_request_set_crypt(req, sg, icv, skb->len + seqhi_len); 399dff3bb06SSteffen Klassert ahash_request_set_callback(req, 0, ah_input_done, skb); 400dff3bb06SSteffen Klassert 401dff3bb06SSteffen Klassert AH_SKB_CB(skb)->tmp = work_iph; 402dff3bb06SSteffen Klassert 403dff3bb06SSteffen Klassert err = crypto_ahash_digest(req); 404dff3bb06SSteffen Klassert if (err) { 405dff3bb06SSteffen Klassert if (err == -EINPROGRESS) 4061da177e4SLinus Torvalds goto out; 4070ebea8efSHerbert Xu 408dff3bb06SSteffen Klassert goto out_free; 409dff3bb06SSteffen Klassert } 410dff3bb06SSteffen Klassert 411dff3bb06SSteffen Klassert err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0; 412dff3bb06SSteffen Klassert if (err) 413dff3bb06SSteffen Klassert goto out_free; 414dff3bb06SSteffen Klassert 415b0e380b1SArnaldo Carvalho de Melo skb->network_header += ah_hlen; 416dff3bb06SSteffen Klassert memcpy(skb_network_header(skb), work_iph, ihl); 41731a4ab93SHerbert Xu __skb_pull(skb, ah_hlen + ihl); 4187143dfacSLi RongQing if (x->props.mode == XFRM_MODE_TUNNEL) 4197143dfacSLi RongQing skb_reset_transport_header(skb); 4207143dfacSLi RongQing else 421dff3bb06SSteffen Klassert skb_set_transport_header(skb, -ihl); 4221da177e4SLinus Torvalds 423dff3bb06SSteffen Klassert err = nexthdr; 4241da177e4SLinus Torvalds 425dff3bb06SSteffen Klassert out_free: 426dff3bb06SSteffen Klassert kfree (work_iph); 4271da177e4SLinus Torvalds out: 42807d4ee58SHerbert Xu return err; 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds 431e5b56454SSteffen Klassert static int ah4_err(struct sk_buff *skb, u32 info) 4321da177e4SLinus Torvalds { 4334fb236baSAlexey Dobriyan struct net *net = dev_net(skb->dev); 434b71d1d42SEric Dumazet const struct iphdr *iph = (const struct iphdr *)skb->data; 4351da177e4SLinus Torvalds struct ip_auth_hdr *ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2)); 4361da177e4SLinus Torvalds struct xfrm_state *x; 4371da177e4SLinus Torvalds 43855be7a9cSDavid S. Miller switch (icmp_hdr(skb)->type) { 43955be7a9cSDavid S. Miller case ICMP_DEST_UNREACH: 44055be7a9cSDavid S. Miller if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED) 441e5b56454SSteffen Klassert return 0; 44255be7a9cSDavid S. Miller case ICMP_REDIRECT: 44355be7a9cSDavid S. Miller break; 44455be7a9cSDavid S. Miller default: 445e5b56454SSteffen Klassert return 0; 44655be7a9cSDavid S. Miller } 4471da177e4SLinus Torvalds 448b71d1d42SEric Dumazet x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr, 449b71d1d42SEric Dumazet ah->spi, IPPROTO_AH, AF_INET); 4501da177e4SLinus Torvalds if (!x) 451e5b56454SSteffen Klassert return 0; 45255be7a9cSDavid S. Miller 453387aa65aSTimo Teräs if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH) 45436393395SDavid S. Miller ipv4_update_pmtu(skb, net, info, 0, 0, IPPROTO_AH, 0); 455387aa65aSTimo Teräs else 45655be7a9cSDavid S. Miller ipv4_redirect(skb, net, 0, 0, IPPROTO_AH, 0); 4571da177e4SLinus Torvalds xfrm_state_put(x); 458e5b56454SSteffen Klassert 459e5b56454SSteffen Klassert return 0; 4601da177e4SLinus Torvalds } 4611da177e4SLinus Torvalds 46272cb6962SHerbert Xu static int ah_init_state(struct xfrm_state *x) 4631da177e4SLinus Torvalds { 4641da177e4SLinus Torvalds struct ah_data *ahp = NULL; 4651da177e4SLinus Torvalds struct xfrm_algo_desc *aalg_desc; 466dff3bb06SSteffen Klassert struct crypto_ahash *ahash; 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds if (!x->aalg) 4691da177e4SLinus Torvalds goto error; 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds if (x->encap) 4721da177e4SLinus Torvalds goto error; 4731da177e4SLinus Torvalds 4740da974f4SPanagiotis Issaris ahp = kzalloc(sizeof(*ahp), GFP_KERNEL); 475dff3bb06SSteffen Klassert if (!ahp) 4761da177e4SLinus Torvalds return -ENOMEM; 4771da177e4SLinus Torvalds 478dff3bb06SSteffen Klassert ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0); 479dff3bb06SSteffen Klassert if (IS_ERR(ahash)) 4801da177e4SLinus Torvalds goto error; 48107d4ee58SHerbert Xu 482dff3bb06SSteffen Klassert ahp->ahash = ahash; 483dff3bb06SSteffen Klassert if (crypto_ahash_setkey(ahash, x->aalg->alg_key, 484bc31d3b2SHerbert Xu (x->aalg->alg_key_len + 7) / 8)) 48507d4ee58SHerbert Xu goto error; 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds /* 4881da177e4SLinus Torvalds * Lookup the algorithm description maintained by xfrm_algo, 4891da177e4SLinus Torvalds * verify crypto transform properties, and store information 4901da177e4SLinus Torvalds * we need for AH processing. This lookup cannot fail here 491dff3bb06SSteffen Klassert * after a successful crypto_alloc_ahash(). 4921da177e4SLinus Torvalds */ 4931da177e4SLinus Torvalds aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0); 4941da177e4SLinus Torvalds BUG_ON(!aalg_desc); 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds if (aalg_desc->uinfo.auth.icv_fullbits/8 != 497dff3bb06SSteffen Klassert crypto_ahash_digestsize(ahash)) { 498afd46503SJoe Perches pr_info("%s: %s digestsize %u != %hu\n", 499afd46503SJoe Perches __func__, x->aalg->alg_name, 500afd46503SJoe Perches crypto_ahash_digestsize(ahash), 5011da177e4SLinus Torvalds aalg_desc->uinfo.auth.icv_fullbits / 8); 5021da177e4SLinus Torvalds goto error; 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8; 5068f8a088cSMartin Willi ahp->icv_trunc_len = x->aalg->alg_trunc_len/8; 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN); 5091da177e4SLinus Torvalds 510fa9921e4SNicolas Dichtel if (x->props.flags & XFRM_STATE_ALIGN4) 511fa9921e4SNicolas Dichtel x->props.header_len = XFRM_ALIGN4(sizeof(struct ip_auth_hdr) + 512fa9921e4SNicolas Dichtel ahp->icv_trunc_len); 513fa9921e4SNicolas Dichtel else 51487bdc48dSHerbert Xu x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) + 51587bdc48dSHerbert Xu ahp->icv_trunc_len); 5167e49e6deSMasahide NAKAMURA if (x->props.mode == XFRM_MODE_TUNNEL) 5171da177e4SLinus Torvalds x->props.header_len += sizeof(struct iphdr); 5181da177e4SLinus Torvalds x->data = ahp; 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds return 0; 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds error: 5231da177e4SLinus Torvalds if (ahp) { 524dff3bb06SSteffen Klassert crypto_free_ahash(ahp->ahash); 5251da177e4SLinus Torvalds kfree(ahp); 5261da177e4SLinus Torvalds } 5271da177e4SLinus Torvalds return -EINVAL; 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds static void ah_destroy(struct xfrm_state *x) 5311da177e4SLinus Torvalds { 5321da177e4SLinus Torvalds struct ah_data *ahp = x->data; 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds if (!ahp) 5351da177e4SLinus Torvalds return; 5361da177e4SLinus Torvalds 537dff3bb06SSteffen Klassert crypto_free_ahash(ahp->ahash); 5381da177e4SLinus Torvalds kfree(ahp); 5391da177e4SLinus Torvalds } 5401da177e4SLinus Torvalds 541e5b56454SSteffen Klassert static int ah4_rcv_cb(struct sk_buff *skb, int err) 542e5b56454SSteffen Klassert { 543e5b56454SSteffen Klassert return 0; 544e5b56454SSteffen Klassert } 5451da177e4SLinus Torvalds 546533cb5b0SEric Dumazet static const struct xfrm_type ah_type = 5471da177e4SLinus Torvalds { 5481da177e4SLinus Torvalds .description = "AH4", 5491da177e4SLinus Torvalds .owner = THIS_MODULE, 5501da177e4SLinus Torvalds .proto = IPPROTO_AH, 551436a0a40SHerbert Xu .flags = XFRM_TYPE_REPLAY_PROT, 5521da177e4SLinus Torvalds .init_state = ah_init_state, 5531da177e4SLinus Torvalds .destructor = ah_destroy, 5541da177e4SLinus Torvalds .input = ah_input, 5551da177e4SLinus Torvalds .output = ah_output 5561da177e4SLinus Torvalds }; 5571da177e4SLinus Torvalds 558e5b56454SSteffen Klassert static struct xfrm4_protocol ah4_protocol = { 5591da177e4SLinus Torvalds .handler = xfrm4_rcv, 560e5b56454SSteffen Klassert .input_handler = xfrm_input, 561e5b56454SSteffen Klassert .cb_handler = ah4_rcv_cb, 5621da177e4SLinus Torvalds .err_handler = ah4_err, 563e5b56454SSteffen Klassert .priority = 0, 5641da177e4SLinus Torvalds }; 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds static int __init ah4_init(void) 5671da177e4SLinus Torvalds { 5681da177e4SLinus Torvalds if (xfrm_register_type(&ah_type, AF_INET) < 0) { 569058bd4d2SJoe Perches pr_info("%s: can't add xfrm type\n", __func__); 5701da177e4SLinus Torvalds return -EAGAIN; 5711da177e4SLinus Torvalds } 572e5b56454SSteffen Klassert if (xfrm4_protocol_register(&ah4_protocol, IPPROTO_AH) < 0) { 573058bd4d2SJoe Perches pr_info("%s: can't add protocol\n", __func__); 5741da177e4SLinus Torvalds xfrm_unregister_type(&ah_type, AF_INET); 5751da177e4SLinus Torvalds return -EAGAIN; 5761da177e4SLinus Torvalds } 5771da177e4SLinus Torvalds return 0; 5781da177e4SLinus Torvalds } 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds static void __exit ah4_fini(void) 5811da177e4SLinus Torvalds { 582e5b56454SSteffen Klassert if (xfrm4_protocol_deregister(&ah4_protocol, IPPROTO_AH) < 0) 583058bd4d2SJoe Perches pr_info("%s: can't remove protocol\n", __func__); 5841da177e4SLinus Torvalds if (xfrm_unregister_type(&ah_type, AF_INET) < 0) 585058bd4d2SJoe Perches pr_info("%s: can't remove xfrm type\n", __func__); 5861da177e4SLinus Torvalds } 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds module_init(ah4_init); 5891da177e4SLinus Torvalds module_exit(ah4_fini); 5901da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 591d3d6dd3aSMasahide NAKAMURA MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_AH); 592