109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2afd46503SJoe Perches #define pr_fmt(fmt) "IPsec: " fmt 3afd46503SJoe Perches 4dff3bb06SSteffen Klassert #include <crypto/hash.h> 5c66218ccSHerbert Xu #include <crypto/utils.h> 607d4ee58SHerbert Xu #include <linux/err.h> 71da177e4SLinus Torvalds #include <linux/module.h> 85a0e3ad6STejun Heo #include <linux/slab.h> 91da177e4SLinus Torvalds #include <net/ip.h> 101da177e4SLinus Torvalds #include <net/xfrm.h> 111da177e4SLinus Torvalds #include <net/ah.h> 121da177e4SLinus Torvalds #include <linux/crypto.h> 131da177e4SLinus Torvalds #include <linux/pfkeyv2.h> 14dff3bb06SSteffen Klassert #include <linux/scatterlist.h> 151da177e4SLinus Torvalds #include <net/icmp.h> 1614c85021SArnaldo Carvalho de Melo #include <net/protocol.h> 171da177e4SLinus Torvalds 18dff3bb06SSteffen Klassert struct ah_skb_cb { 19dff3bb06SSteffen Klassert struct xfrm_skb_cb xfrm; 20dff3bb06SSteffen Klassert void *tmp; 21dff3bb06SSteffen Klassert }; 22dff3bb06SSteffen Klassert 23dff3bb06SSteffen Klassert #define AH_SKB_CB(__skb) ((struct ah_skb_cb *)&((__skb)->cb[0])) 24dff3bb06SSteffen Klassert 25dff3bb06SSteffen Klassert static void *ah_alloc_tmp(struct crypto_ahash *ahash, int nfrags, 26dff3bb06SSteffen Klassert unsigned int size) 27dff3bb06SSteffen Klassert { 28dff3bb06SSteffen Klassert unsigned int len; 29dff3bb06SSteffen Klassert 30e77f5dd7SEric Biggers len = size + crypto_ahash_digestsize(ahash); 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 47e77f5dd7SEric Biggers static inline u8 *ah_tmp_icv(void *tmp, unsigned int offset) 48dff3bb06SSteffen Klassert { 49e77f5dd7SEric Biggers return tmp + offset; 50dff3bb06SSteffen Klassert } 51dff3bb06SSteffen Klassert 52dff3bb06SSteffen Klassert static inline struct ahash_request *ah_tmp_req(struct crypto_ahash *ahash, 53dff3bb06SSteffen Klassert u8 *icv) 54dff3bb06SSteffen Klassert { 55dff3bb06SSteffen Klassert struct ahash_request *req; 56dff3bb06SSteffen Klassert 57dff3bb06SSteffen Klassert req = (void *)PTR_ALIGN(icv + crypto_ahash_digestsize(ahash), 58dff3bb06SSteffen Klassert crypto_tfm_ctx_alignment()); 59dff3bb06SSteffen Klassert 60dff3bb06SSteffen Klassert ahash_request_set_tfm(req, ahash); 61dff3bb06SSteffen Klassert 62dff3bb06SSteffen Klassert return req; 63dff3bb06SSteffen Klassert } 64dff3bb06SSteffen Klassert 65dff3bb06SSteffen Klassert static inline struct scatterlist *ah_req_sg(struct crypto_ahash *ahash, 66dff3bb06SSteffen Klassert struct ahash_request *req) 67dff3bb06SSteffen Klassert { 68dff3bb06SSteffen Klassert return (void *)ALIGN((unsigned long)(req + 1) + 69dff3bb06SSteffen Klassert crypto_ahash_reqsize(ahash), 70dff3bb06SSteffen Klassert __alignof__(struct scatterlist)); 71dff3bb06SSteffen Klassert } 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds /* Clear mutable options and find final destination to substitute 741da177e4SLinus Torvalds * into IP header for icv calculation. Options are already checked 751da177e4SLinus Torvalds * for validity, so paranoia is not required. */ 761da177e4SLinus Torvalds 77b71d1d42SEric Dumazet static int ip_clear_mutable_options(const struct iphdr *iph, __be32 *daddr) 781da177e4SLinus Torvalds { 791da177e4SLinus Torvalds unsigned char *optptr = (unsigned char *)(iph+1); 801da177e4SLinus Torvalds int l = iph->ihl*4 - sizeof(struct iphdr); 811da177e4SLinus Torvalds int optlen; 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds while (l > 0) { 841da177e4SLinus Torvalds switch (*optptr) { 851da177e4SLinus Torvalds case IPOPT_END: 861da177e4SLinus Torvalds return 0; 871da177e4SLinus Torvalds case IPOPT_NOOP: 881da177e4SLinus Torvalds l--; 891da177e4SLinus Torvalds optptr++; 901da177e4SLinus Torvalds continue; 911da177e4SLinus Torvalds } 921da177e4SLinus Torvalds optlen = optptr[1]; 931da177e4SLinus Torvalds if (optlen<2 || optlen>l) 941da177e4SLinus Torvalds return -EINVAL; 951da177e4SLinus Torvalds switch (*optptr) { 961da177e4SLinus Torvalds case IPOPT_SEC: 971da177e4SLinus Torvalds case 0x85: /* Some "Extended Security" crap. */ 9811a03f78SPaul Moore case IPOPT_CIPSO: 991da177e4SLinus Torvalds case IPOPT_RA: 1001da177e4SLinus Torvalds case 0x80|21: /* RFC1770 */ 1011da177e4SLinus Torvalds break; 1021da177e4SLinus Torvalds case IPOPT_LSRR: 1031da177e4SLinus Torvalds case IPOPT_SSRR: 1041da177e4SLinus Torvalds if (optlen < 6) 1051da177e4SLinus Torvalds return -EINVAL; 1061da177e4SLinus Torvalds memcpy(daddr, optptr+optlen-4, 4); 107a8eceea8SJoe Perches fallthrough; 1081da177e4SLinus Torvalds default: 10996fe1c02SNick Bowler memset(optptr, 0, optlen); 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds l -= optlen; 1121da177e4SLinus Torvalds optptr += optlen; 1131da177e4SLinus Torvalds } 1141da177e4SLinus Torvalds return 0; 1151da177e4SLinus Torvalds } 1161da177e4SLinus Torvalds 117fd5dabf7SHerbert Xu static void ah_output_done(void *data, int err) 118dff3bb06SSteffen Klassert { 119dff3bb06SSteffen Klassert u8 *icv; 120dff3bb06SSteffen Klassert struct iphdr *iph; 121fd5dabf7SHerbert Xu struct sk_buff *skb = data; 122dff3bb06SSteffen Klassert struct xfrm_state *x = skb_dst(skb)->xfrm; 123dff3bb06SSteffen Klassert struct ah_data *ahp = x->data; 124dff3bb06SSteffen Klassert struct iphdr *top_iph = ip_hdr(skb); 125dff3bb06SSteffen Klassert struct ip_auth_hdr *ah = ip_auth_hdr(skb); 126dff3bb06SSteffen Klassert int ihl = ip_hdrlen(skb); 127dff3bb06SSteffen Klassert 128dff3bb06SSteffen Klassert iph = AH_SKB_CB(skb)->tmp; 129e77f5dd7SEric Biggers icv = ah_tmp_icv(iph, ihl); 130dff3bb06SSteffen Klassert memcpy(ah->auth_data, icv, ahp->icv_trunc_len); 131dff3bb06SSteffen Klassert 132dff3bb06SSteffen Klassert top_iph->tos = iph->tos; 133dff3bb06SSteffen Klassert top_iph->ttl = iph->ttl; 134dff3bb06SSteffen Klassert top_iph->frag_off = iph->frag_off; 135dff3bb06SSteffen Klassert if (top_iph->ihl != 5) { 136dff3bb06SSteffen Klassert top_iph->daddr = iph->daddr; 137dff3bb06SSteffen Klassert memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr)); 138dff3bb06SSteffen Klassert } 139dff3bb06SSteffen Klassert 140dff3bb06SSteffen Klassert kfree(AH_SKB_CB(skb)->tmp); 1419ab1265dSEvan Nimmo xfrm_output_resume(skb->sk, skb, err); 142dff3bb06SSteffen Klassert } 143dff3bb06SSteffen Klassert 1441da177e4SLinus Torvalds static int ah_output(struct xfrm_state *x, struct sk_buff *skb) 1451da177e4SLinus Torvalds { 1461da177e4SLinus Torvalds int err; 147dff3bb06SSteffen Klassert int nfrags; 148dff3bb06SSteffen Klassert int ihl; 149dff3bb06SSteffen Klassert u8 *icv; 150dff3bb06SSteffen Klassert struct sk_buff *trailer; 151dff3bb06SSteffen Klassert struct crypto_ahash *ahash; 152dff3bb06SSteffen Klassert struct ahash_request *req; 153dff3bb06SSteffen Klassert struct scatterlist *sg; 1541da177e4SLinus Torvalds struct iphdr *iph, *top_iph; 1551da177e4SLinus Torvalds struct ip_auth_hdr *ah; 1561da177e4SLinus Torvalds struct ah_data *ahp; 157d4d573d0SFan Du int seqhi_len = 0; 158d4d573d0SFan Du __be32 *seqhi; 159d4d573d0SFan Du int sglists = 0; 160d4d573d0SFan Du struct scatterlist *seqhisg; 161dff3bb06SSteffen Klassert 162dff3bb06SSteffen Klassert ahp = x->data; 163dff3bb06SSteffen Klassert ahash = ahp->ahash; 164dff3bb06SSteffen Klassert 165dff3bb06SSteffen Klassert if ((err = skb_cow_data(skb, 0, &trailer)) < 0) 166dff3bb06SSteffen Klassert goto out; 167dff3bb06SSteffen Klassert nfrags = err; 1681da177e4SLinus Torvalds 1697b277b1aSHerbert Xu skb_push(skb, -skb_network_offset(skb)); 170dff3bb06SSteffen Klassert ah = ip_auth_hdr(skb); 171dff3bb06SSteffen Klassert ihl = ip_hdrlen(skb); 172dff3bb06SSteffen Klassert 173d4d573d0SFan Du if (x->props.flags & XFRM_STATE_ESN) { 174d4d573d0SFan Du sglists = 1; 175d4d573d0SFan Du seqhi_len = sizeof(*seqhi); 176d4d573d0SFan Du } 177dff3bb06SSteffen Klassert err = -ENOMEM; 178d4d573d0SFan Du iph = ah_alloc_tmp(ahash, nfrags + sglists, ihl + seqhi_len); 179dff3bb06SSteffen Klassert if (!iph) 180dff3bb06SSteffen Klassert goto out; 181d4d573d0SFan Du seqhi = (__be32 *)((char *)iph + ihl); 182e77f5dd7SEric Biggers icv = ah_tmp_icv(seqhi, seqhi_len); 183dff3bb06SSteffen Klassert req = ah_tmp_req(ahash, icv); 184dff3bb06SSteffen Klassert sg = ah_req_sg(ahash, req); 185d4d573d0SFan Du seqhisg = sg + nfrags; 186dff3bb06SSteffen Klassert 187dff3bb06SSteffen Klassert memset(ah->auth_data, 0, ahp->icv_trunc_len); 188dff3bb06SSteffen Klassert 189eddc9ec5SArnaldo Carvalho de Melo top_iph = ip_hdr(skb); 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds iph->tos = top_iph->tos; 1921da177e4SLinus Torvalds iph->ttl = top_iph->ttl; 1931da177e4SLinus Torvalds iph->frag_off = top_iph->frag_off; 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds if (top_iph->ihl != 5) { 1961da177e4SLinus Torvalds iph->daddr = top_iph->daddr; 1971da177e4SLinus Torvalds memcpy(iph+1, top_iph+1, top_iph->ihl*4 - sizeof(struct iphdr)); 1981da177e4SLinus Torvalds err = ip_clear_mutable_options(top_iph, &top_iph->daddr); 1991da177e4SLinus Torvalds if (err) 200dff3bb06SSteffen Klassert goto out_free; 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds 20337fedd3aSHerbert Xu ah->nexthdr = *skb_mac_header(skb); 20437fedd3aSHerbert Xu *skb_mac_header(skb) = IPPROTO_AH; 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds top_iph->tos = 0; 2071da177e4SLinus Torvalds top_iph->tot_len = htons(skb->len); 2081da177e4SLinus Torvalds top_iph->frag_off = 0; 2091da177e4SLinus Torvalds top_iph->ttl = 0; 2101da177e4SLinus Torvalds top_iph->check = 0; 2111da177e4SLinus Torvalds 212fa9921e4SNicolas Dichtel if (x->props.flags & XFRM_STATE_ALIGN4) 213fa9921e4SNicolas Dichtel ah->hdrlen = (XFRM_ALIGN4(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2; 214fa9921e4SNicolas Dichtel else 21587bdc48dSHerbert Xu ah->hdrlen = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2; 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds ah->reserved = 0; 2181da177e4SLinus Torvalds ah->spi = x->id.spi; 2191ce3644aSSteffen Klassert ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low); 220b7c6538cSHerbert Xu 221d4d573d0SFan Du sg_init_table(sg, nfrags + sglists); 2223f297707SJason A. Donenfeld err = skb_to_sgvec_nomark(skb, sg, 0, skb->len); 2233f297707SJason A. Donenfeld if (unlikely(err < 0)) 2243f297707SJason A. Donenfeld goto out_free; 225b7c6538cSHerbert Xu 226d4d573d0SFan Du if (x->props.flags & XFRM_STATE_ESN) { 227d4d573d0SFan Du /* Attach seqhi sg right after packet payload */ 228d4d573d0SFan Du *seqhi = htonl(XFRM_SKB_CB(skb)->seq.output.hi); 229d4d573d0SFan Du sg_set_buf(seqhisg, seqhi, seqhi_len); 230d4d573d0SFan Du } 231d4d573d0SFan Du ahash_request_set_crypt(req, sg, icv, skb->len + seqhi_len); 232dff3bb06SSteffen Klassert ahash_request_set_callback(req, 0, ah_output_done, skb); 233dff3bb06SSteffen Klassert 234dff3bb06SSteffen Klassert AH_SKB_CB(skb)->tmp = iph; 235dff3bb06SSteffen Klassert 236dff3bb06SSteffen Klassert err = crypto_ahash_digest(req); 237dff3bb06SSteffen Klassert if (err) { 238dff3bb06SSteffen Klassert if (err == -EINPROGRESS) 239dff3bb06SSteffen Klassert goto out; 240dff3bb06SSteffen Klassert 241068c2e70SGilad Ben-Yossef if (err == -ENOSPC) 242dff3bb06SSteffen Klassert err = NET_XMIT_DROP; 243dff3bb06SSteffen Klassert goto out_free; 244dff3bb06SSteffen Klassert } 245dff3bb06SSteffen Klassert 246dff3bb06SSteffen Klassert memcpy(ah->auth_data, icv, ahp->icv_trunc_len); 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds top_iph->tos = iph->tos; 2491da177e4SLinus Torvalds top_iph->ttl = iph->ttl; 2501da177e4SLinus Torvalds top_iph->frag_off = iph->frag_off; 2511da177e4SLinus Torvalds if (top_iph->ihl != 5) { 2521da177e4SLinus Torvalds top_iph->daddr = iph->daddr; 2531da177e4SLinus Torvalds memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr)); 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds 256dff3bb06SSteffen Klassert out_free: 257dff3bb06SSteffen Klassert kfree(iph); 258dff3bb06SSteffen Klassert out: 2591da177e4SLinus Torvalds return err; 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds 262fd5dabf7SHerbert Xu static void ah_input_done(void *data, int err) 263dff3bb06SSteffen Klassert { 264dff3bb06SSteffen Klassert u8 *auth_data; 265dff3bb06SSteffen Klassert u8 *icv; 266dff3bb06SSteffen Klassert struct iphdr *work_iph; 267fd5dabf7SHerbert Xu struct sk_buff *skb = data; 268dff3bb06SSteffen Klassert struct xfrm_state *x = xfrm_input_state(skb); 269dff3bb06SSteffen Klassert struct ah_data *ahp = x->data; 270dff3bb06SSteffen Klassert struct ip_auth_hdr *ah = ip_auth_hdr(skb); 271dff3bb06SSteffen Klassert int ihl = ip_hdrlen(skb); 272dff3bb06SSteffen Klassert int ah_hlen = (ah->hdrlen + 2) << 2; 273dff3bb06SSteffen Klassert 274ebd89a2dSGilad Ben-Yossef if (err) 275ebd89a2dSGilad Ben-Yossef goto out; 276ebd89a2dSGilad Ben-Yossef 277dff3bb06SSteffen Klassert work_iph = AH_SKB_CB(skb)->tmp; 278dff3bb06SSteffen Klassert auth_data = ah_tmp_auth(work_iph, ihl); 279e77f5dd7SEric Biggers icv = ah_tmp_icv(auth_data, ahp->icv_trunc_len); 280dff3bb06SSteffen Klassert 28167df58a3SSabrina Dubroca err = crypto_memneq(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG : 0; 282dff3bb06SSteffen Klassert if (err) 283dff3bb06SSteffen Klassert goto out; 284dff3bb06SSteffen Klassert 285b7ea81a5SNick Bowler err = ah->nexthdr; 286b7ea81a5SNick Bowler 287dff3bb06SSteffen Klassert skb->network_header += ah_hlen; 288dff3bb06SSteffen Klassert memcpy(skb_network_header(skb), work_iph, ihl); 289dff3bb06SSteffen Klassert __skb_pull(skb, ah_hlen + ihl); 2907143dfacSLi RongQing 2917143dfacSLi RongQing if (x->props.mode == XFRM_MODE_TUNNEL) 2927143dfacSLi RongQing skb_reset_transport_header(skb); 2937143dfacSLi RongQing else 294dff3bb06SSteffen Klassert skb_set_transport_header(skb, -ihl); 295dff3bb06SSteffen Klassert out: 296dff3bb06SSteffen Klassert kfree(AH_SKB_CB(skb)->tmp); 297dff3bb06SSteffen Klassert xfrm_input_resume(skb, err); 298dff3bb06SSteffen Klassert } 299dff3bb06SSteffen Klassert 300e695633eSHerbert Xu static int ah_input(struct xfrm_state *x, struct sk_buff *skb) 3011da177e4SLinus Torvalds { 3021da177e4SLinus Torvalds int ah_hlen; 30331a4ab93SHerbert Xu int ihl; 304631a6698SHerbert Xu int nexthdr; 305dff3bb06SSteffen Klassert int nfrags; 306dff3bb06SSteffen Klassert u8 *auth_data; 307dff3bb06SSteffen Klassert u8 *icv; 308dff3bb06SSteffen Klassert struct sk_buff *trailer; 309dff3bb06SSteffen Klassert struct crypto_ahash *ahash; 310dff3bb06SSteffen Klassert struct ahash_request *req; 311dff3bb06SSteffen Klassert struct scatterlist *sg; 312dff3bb06SSteffen Klassert struct iphdr *iph, *work_iph; 3131da177e4SLinus Torvalds struct ip_auth_hdr *ah; 3141da177e4SLinus Torvalds struct ah_data *ahp; 315dff3bb06SSteffen Klassert int err = -ENOMEM; 316d8b2a860SFan Du int seqhi_len = 0; 317d8b2a860SFan Du __be32 *seqhi; 318d8b2a860SFan Du int sglists = 0; 319d8b2a860SFan Du struct scatterlist *seqhisg; 3201da177e4SLinus Torvalds 32187bdc48dSHerbert Xu if (!pskb_may_pull(skb, sizeof(*ah))) 3221da177e4SLinus Torvalds goto out; 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds ah = (struct ip_auth_hdr *)skb->data; 3251da177e4SLinus Torvalds ahp = x->data; 326dff3bb06SSteffen Klassert ahash = ahp->ahash; 327dff3bb06SSteffen Klassert 328631a6698SHerbert Xu nexthdr = ah->nexthdr; 3291da177e4SLinus Torvalds ah_hlen = (ah->hdrlen + 2) << 2; 3301da177e4SLinus Torvalds 331fa9921e4SNicolas Dichtel if (x->props.flags & XFRM_STATE_ALIGN4) { 332fa9921e4SNicolas Dichtel if (ah_hlen != XFRM_ALIGN4(sizeof(*ah) + ahp->icv_full_len) && 333fa9921e4SNicolas Dichtel ah_hlen != XFRM_ALIGN4(sizeof(*ah) + ahp->icv_trunc_len)) 334fa9921e4SNicolas Dichtel goto out; 335fa9921e4SNicolas Dichtel } else { 33687bdc48dSHerbert Xu if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) && 33787bdc48dSHerbert Xu ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len)) 3381da177e4SLinus Torvalds goto out; 339fa9921e4SNicolas Dichtel } 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds if (!pskb_may_pull(skb, ah_hlen)) 3421da177e4SLinus Torvalds goto out; 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds /* We are going to _remove_ AH header to keep sockets happy, 3451da177e4SLinus Torvalds * so... Later this can change. */ 34614bbd6a5SPravin B Shelar if (skb_unclone(skb, GFP_ATOMIC)) 3471da177e4SLinus Torvalds goto out; 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds 352dff3bb06SSteffen Klassert if ((err = skb_cow_data(skb, 0, &trailer)) < 0) 353dff3bb06SSteffen Klassert goto out; 354dff3bb06SSteffen Klassert nfrags = err; 355dff3bb06SSteffen Klassert 3564b0ef1f2SDang Hongwu ah = (struct ip_auth_hdr *)skb->data; 3574b0ef1f2SDang Hongwu iph = ip_hdr(skb); 3584b0ef1f2SDang Hongwu ihl = ip_hdrlen(skb); 3594b0ef1f2SDang Hongwu 360d8b2a860SFan Du if (x->props.flags & XFRM_STATE_ESN) { 361d8b2a860SFan Du sglists = 1; 362d8b2a860SFan Du seqhi_len = sizeof(*seqhi); 363d8b2a860SFan Du } 364d8b2a860SFan Du 365d8b2a860SFan Du work_iph = ah_alloc_tmp(ahash, nfrags + sglists, ihl + 366d8b2a860SFan Du ahp->icv_trunc_len + seqhi_len); 36794c10f0eSDavid S. Miller if (!work_iph) { 36894c10f0eSDavid S. Miller err = -ENOMEM; 369dff3bb06SSteffen Klassert goto out; 37094c10f0eSDavid S. Miller } 371dff3bb06SSteffen Klassert 372d8b2a860SFan Du seqhi = (__be32 *)((char *)work_iph + ihl); 373d8b2a860SFan Du auth_data = ah_tmp_auth(seqhi, seqhi_len); 374e77f5dd7SEric Biggers icv = ah_tmp_icv(auth_data, ahp->icv_trunc_len); 375dff3bb06SSteffen Klassert req = ah_tmp_req(ahash, icv); 376dff3bb06SSteffen Klassert sg = ah_req_sg(ahash, req); 377d8b2a860SFan Du seqhisg = sg + nfrags; 378dff3bb06SSteffen Klassert 379dff3bb06SSteffen Klassert memcpy(work_iph, iph, ihl); 380dff3bb06SSteffen Klassert memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len); 381dff3bb06SSteffen Klassert memset(ah->auth_data, 0, ahp->icv_trunc_len); 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds iph->ttl = 0; 3841da177e4SLinus Torvalds iph->tos = 0; 3851da177e4SLinus Torvalds iph->frag_off = 0; 3861da177e4SLinus Torvalds iph->check = 0; 38731a4ab93SHerbert Xu if (ihl > sizeof(*iph)) { 388d5a0a1e3SAl Viro __be32 dummy; 389dff3bb06SSteffen Klassert err = ip_clear_mutable_options(iph, &dummy); 390dff3bb06SSteffen Klassert if (err) 391dff3bb06SSteffen Klassert goto out_free; 3921da177e4SLinus Torvalds } 3930ebea8efSHerbert Xu 39431a4ab93SHerbert Xu skb_push(skb, ihl); 3950ebea8efSHerbert Xu 396d8b2a860SFan Du sg_init_table(sg, nfrags + sglists); 3973f297707SJason A. Donenfeld err = skb_to_sgvec_nomark(skb, sg, 0, skb->len); 3983f297707SJason A. Donenfeld if (unlikely(err < 0)) 3993f297707SJason A. Donenfeld goto out_free; 400dff3bb06SSteffen Klassert 401d8b2a860SFan Du if (x->props.flags & XFRM_STATE_ESN) { 402d8b2a860SFan Du /* Attach seqhi sg right after packet payload */ 403d8b2a860SFan Du *seqhi = XFRM_SKB_CB(skb)->seq.input.hi; 404d8b2a860SFan Du sg_set_buf(seqhisg, seqhi, seqhi_len); 405d8b2a860SFan Du } 406d8b2a860SFan Du ahash_request_set_crypt(req, sg, icv, skb->len + seqhi_len); 407dff3bb06SSteffen Klassert ahash_request_set_callback(req, 0, ah_input_done, skb); 408dff3bb06SSteffen Klassert 409dff3bb06SSteffen Klassert AH_SKB_CB(skb)->tmp = work_iph; 410dff3bb06SSteffen Klassert 411dff3bb06SSteffen Klassert err = crypto_ahash_digest(req); 412dff3bb06SSteffen Klassert if (err) { 413dff3bb06SSteffen Klassert if (err == -EINPROGRESS) 4141da177e4SLinus Torvalds goto out; 4150ebea8efSHerbert Xu 416dff3bb06SSteffen Klassert goto out_free; 417dff3bb06SSteffen Klassert } 418dff3bb06SSteffen Klassert 41967df58a3SSabrina Dubroca err = crypto_memneq(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG : 0; 420dff3bb06SSteffen Klassert if (err) 421dff3bb06SSteffen Klassert goto out_free; 422dff3bb06SSteffen Klassert 423b0e380b1SArnaldo Carvalho de Melo skb->network_header += ah_hlen; 424dff3bb06SSteffen Klassert memcpy(skb_network_header(skb), work_iph, ihl); 42531a4ab93SHerbert Xu __skb_pull(skb, ah_hlen + ihl); 4267143dfacSLi RongQing if (x->props.mode == XFRM_MODE_TUNNEL) 4277143dfacSLi RongQing skb_reset_transport_header(skb); 4287143dfacSLi RongQing else 429dff3bb06SSteffen Klassert skb_set_transport_header(skb, -ihl); 4301da177e4SLinus Torvalds 431dff3bb06SSteffen Klassert err = nexthdr; 4321da177e4SLinus Torvalds 433dff3bb06SSteffen Klassert out_free: 434dff3bb06SSteffen Klassert kfree (work_iph); 4351da177e4SLinus Torvalds out: 43607d4ee58SHerbert Xu return err; 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds 439e5b56454SSteffen Klassert static int ah4_err(struct sk_buff *skb, u32 info) 4401da177e4SLinus Torvalds { 4414fb236baSAlexey Dobriyan struct net *net = dev_net(skb->dev); 442b71d1d42SEric Dumazet const struct iphdr *iph = (const struct iphdr *)skb->data; 4431da177e4SLinus Torvalds struct ip_auth_hdr *ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2)); 4441da177e4SLinus Torvalds struct xfrm_state *x; 4451da177e4SLinus Torvalds 44655be7a9cSDavid S. Miller switch (icmp_hdr(skb)->type) { 44755be7a9cSDavid S. Miller case ICMP_DEST_UNREACH: 44855be7a9cSDavid S. Miller if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED) 449e5b56454SSteffen Klassert return 0; 45079121184SGustavo A. R. Silva break; 45155be7a9cSDavid S. Miller case ICMP_REDIRECT: 45255be7a9cSDavid S. Miller break; 45355be7a9cSDavid S. Miller default: 454e5b56454SSteffen Klassert return 0; 45555be7a9cSDavid S. Miller } 4561da177e4SLinus Torvalds 457b71d1d42SEric Dumazet x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr, 458b71d1d42SEric Dumazet ah->spi, IPPROTO_AH, AF_INET); 4591da177e4SLinus Torvalds if (!x) 460e5b56454SSteffen Klassert return 0; 46155be7a9cSDavid S. Miller 462387aa65aSTimo Teräs if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH) 463d888f396SMaciej Żenczykowski ipv4_update_pmtu(skb, net, info, 0, IPPROTO_AH); 464387aa65aSTimo Teräs else 4651042caa7SMaciej Żenczykowski ipv4_redirect(skb, net, 0, IPPROTO_AH); 4661da177e4SLinus Torvalds xfrm_state_put(x); 467e5b56454SSteffen Klassert 468e5b56454SSteffen Klassert return 0; 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds 471e1e10b44SSabrina Dubroca static int ah_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack) 4721da177e4SLinus Torvalds { 4731da177e4SLinus Torvalds struct ah_data *ahp = NULL; 4741da177e4SLinus Torvalds struct xfrm_algo_desc *aalg_desc; 475dff3bb06SSteffen Klassert struct crypto_ahash *ahash; 4761da177e4SLinus Torvalds 477ef87a4f8SSabrina Dubroca if (!x->aalg) { 478ef87a4f8SSabrina Dubroca NL_SET_ERR_MSG(extack, "AH requires a state with an AUTH algorithm"); 4791da177e4SLinus Torvalds goto error; 480ef87a4f8SSabrina Dubroca } 4811da177e4SLinus Torvalds 482ef87a4f8SSabrina Dubroca if (x->encap) { 483ef87a4f8SSabrina Dubroca NL_SET_ERR_MSG(extack, "AH is not compatible with encapsulation"); 4841da177e4SLinus Torvalds goto error; 485ef87a4f8SSabrina Dubroca } 4861da177e4SLinus Torvalds 4870da974f4SPanagiotis Issaris ahp = kzalloc(sizeof(*ahp), GFP_KERNEL); 488dff3bb06SSteffen Klassert if (!ahp) 4891da177e4SLinus Torvalds return -ENOMEM; 4901da177e4SLinus Torvalds 491dff3bb06SSteffen Klassert ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0); 492ef87a4f8SSabrina Dubroca if (IS_ERR(ahash)) { 493ef87a4f8SSabrina Dubroca NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations"); 4941da177e4SLinus Torvalds goto error; 495ef87a4f8SSabrina Dubroca } 49607d4ee58SHerbert Xu 497dff3bb06SSteffen Klassert ahp->ahash = ahash; 498dff3bb06SSteffen Klassert if (crypto_ahash_setkey(ahash, x->aalg->alg_key, 499ef87a4f8SSabrina Dubroca (x->aalg->alg_key_len + 7) / 8)) { 500ef87a4f8SSabrina Dubroca NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations"); 50107d4ee58SHerbert Xu goto error; 502ef87a4f8SSabrina Dubroca } 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds /* 5051da177e4SLinus Torvalds * Lookup the algorithm description maintained by xfrm_algo, 5061da177e4SLinus Torvalds * verify crypto transform properties, and store information 5071da177e4SLinus Torvalds * we need for AH processing. This lookup cannot fail here 508dff3bb06SSteffen Klassert * after a successful crypto_alloc_ahash(). 5091da177e4SLinus Torvalds */ 5101da177e4SLinus Torvalds aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0); 5111da177e4SLinus Torvalds BUG_ON(!aalg_desc); 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds if (aalg_desc->uinfo.auth.icv_fullbits/8 != 514dff3bb06SSteffen Klassert crypto_ahash_digestsize(ahash)) { 515ef87a4f8SSabrina Dubroca NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations"); 5161da177e4SLinus Torvalds goto error; 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8; 5208f8a088cSMartin Willi ahp->icv_trunc_len = x->aalg->alg_trunc_len/8; 5211da177e4SLinus Torvalds 522fa9921e4SNicolas Dichtel if (x->props.flags & XFRM_STATE_ALIGN4) 523fa9921e4SNicolas Dichtel x->props.header_len = XFRM_ALIGN4(sizeof(struct ip_auth_hdr) + 524fa9921e4SNicolas Dichtel ahp->icv_trunc_len); 525fa9921e4SNicolas Dichtel else 52687bdc48dSHerbert Xu x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) + 52787bdc48dSHerbert Xu ahp->icv_trunc_len); 5287e49e6deSMasahide NAKAMURA if (x->props.mode == XFRM_MODE_TUNNEL) 5291da177e4SLinus Torvalds x->props.header_len += sizeof(struct iphdr); 5301da177e4SLinus Torvalds x->data = ahp; 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds return 0; 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds error: 5351da177e4SLinus Torvalds if (ahp) { 536dff3bb06SSteffen Klassert crypto_free_ahash(ahp->ahash); 5371da177e4SLinus Torvalds kfree(ahp); 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds return -EINVAL; 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds static void ah_destroy(struct xfrm_state *x) 5431da177e4SLinus Torvalds { 5441da177e4SLinus Torvalds struct ah_data *ahp = x->data; 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds if (!ahp) 5471da177e4SLinus Torvalds return; 5481da177e4SLinus Torvalds 549dff3bb06SSteffen Klassert crypto_free_ahash(ahp->ahash); 5501da177e4SLinus Torvalds kfree(ahp); 5511da177e4SLinus Torvalds } 5521da177e4SLinus Torvalds 553e5b56454SSteffen Klassert static int ah4_rcv_cb(struct sk_buff *skb, int err) 554e5b56454SSteffen Klassert { 555e5b56454SSteffen Klassert return 0; 556e5b56454SSteffen Klassert } 5571da177e4SLinus Torvalds 558533cb5b0SEric Dumazet static const struct xfrm_type ah_type = 5591da177e4SLinus Torvalds { 5601da177e4SLinus Torvalds .owner = THIS_MODULE, 5611da177e4SLinus Torvalds .proto = IPPROTO_AH, 562436a0a40SHerbert Xu .flags = XFRM_TYPE_REPLAY_PROT, 5631da177e4SLinus Torvalds .init_state = ah_init_state, 5641da177e4SLinus Torvalds .destructor = ah_destroy, 5651da177e4SLinus Torvalds .input = ah_input, 5661da177e4SLinus Torvalds .output = ah_output 5671da177e4SLinus Torvalds }; 5681da177e4SLinus Torvalds 569e5b56454SSteffen Klassert static struct xfrm4_protocol ah4_protocol = { 5701da177e4SLinus Torvalds .handler = xfrm4_rcv, 571e5b56454SSteffen Klassert .input_handler = xfrm_input, 572e5b56454SSteffen Klassert .cb_handler = ah4_rcv_cb, 5731da177e4SLinus Torvalds .err_handler = ah4_err, 574e5b56454SSteffen Klassert .priority = 0, 5751da177e4SLinus Torvalds }; 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds static int __init ah4_init(void) 5781da177e4SLinus Torvalds { 5791da177e4SLinus Torvalds if (xfrm_register_type(&ah_type, AF_INET) < 0) { 580058bd4d2SJoe Perches pr_info("%s: can't add xfrm type\n", __func__); 5811da177e4SLinus Torvalds return -EAGAIN; 5821da177e4SLinus Torvalds } 583e5b56454SSteffen Klassert if (xfrm4_protocol_register(&ah4_protocol, IPPROTO_AH) < 0) { 584058bd4d2SJoe Perches pr_info("%s: can't add protocol\n", __func__); 5851da177e4SLinus Torvalds xfrm_unregister_type(&ah_type, AF_INET); 5861da177e4SLinus Torvalds return -EAGAIN; 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds return 0; 5891da177e4SLinus Torvalds } 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds static void __exit ah4_fini(void) 5921da177e4SLinus Torvalds { 593e5b56454SSteffen Klassert if (xfrm4_protocol_deregister(&ah4_protocol, IPPROTO_AH) < 0) 594058bd4d2SJoe Perches pr_info("%s: can't remove protocol\n", __func__); 5954f518e80SFlorian Westphal xfrm_unregister_type(&ah_type, AF_INET); 5961da177e4SLinus Torvalds } 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds module_init(ah4_init); 5991da177e4SLinus Torvalds module_exit(ah4_fini); 600*b058a5d2SBreno Leitao MODULE_DESCRIPTION("IPv4 AH transformation library"); 6011da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 602d3d6dd3aSMasahide NAKAMURA MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_AH); 603