err = ah_mac_digest(ahp, skb, ah->auth_data);
                if (err)
                        goto out;
-               err = -EINVAL;
                if (memcmp(ahp->work_icv, auth_data, ahp->icv_trunc_len)) {
-                       x->stats.integrity_failed++;
+                       err = -EBADMSG;
                        goto out;
                }
        }
 
        u8 nexthdr[2];
        struct scatterlist *sg;
        int padlen;
-       int err;
+       int err = -EINVAL;
 
        if (!pskb_may_pull(skb, sizeof(*esph)))
                goto out;
                        BUG();
 
                if (unlikely(memcmp(esp->auth.work_icv, sum, alen))) {
-                       x->stats.integrity_failed++;
+                       err = -EBADMSG;
                        goto out;
                }
        }
 
-       if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0)
+       if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
                goto out;
+       nfrags = err;
 
        skb->ip_summed = CHECKSUM_NONE;
 
        sg = &esp->sgbuf[0];
 
        if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
+               err = -ENOMEM;
                sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
                if (!sg)
                        goto out;
        if (unlikely(sg != &esp->sgbuf[0]))
                kfree(sg);
        if (unlikely(err))
-               return err;
+               goto out;
 
        if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
                BUG();
 
+       err = -EINVAL;
        padlen = nexthdr[0];
        if (padlen+2 >= elen)
                goto out;
        return nexthdr[1];
 
 out:
-       return -EINVAL;
+       return err;
 }
 
 static u32 esp4_get_mtu(struct xfrm_state *x, int mtu)
 
                err = ah_mac_digest(ahp, skb, ah->auth_data);
                if (err)
                        goto free_out;
-               err = -EINVAL;
                if (memcmp(ahp->work_icv, auth_data, ahp->icv_trunc_len)) {
                        LIMIT_NETDEBUG(KERN_WARNING "ipsec ah authentication error\n");
-                       x->stats.integrity_failed++;
+                       err = -EBADMSG;
                        goto free_out;
                }
        }
 
                        goto drop_unlock;
 
                nexthdr = x->type->input(x, skb);
-               if (nexthdr <= 0)
+               if (nexthdr <= 0) {
+                       if (nexthdr == -EBADMSG)
+                               x->stats.integrity_failed++;
                        goto drop_unlock;
+               }
 
                skb_network_header(skb)[nhoff] = nexthdr;