#define _NET_XFRM_H
 
 #include <linux/compiler.h>
-#include <linux/in.h>
 #include <linux/xfrm.h>
 #include <linux/spinlock.h>
 #include <linux/list.h>
 
 #include <net/sock.h>
 #include <net/dst.h>
+#include <net/ip.h>
 #include <net/route.h>
 #include <net/ipv6.h>
 #include <net/ip6_fib.h>
        __u8                    proto;
        __u8                    flags;
 #define XFRM_TYPE_NON_FRAGMENT 1
+#define XFRM_TYPE_REPLAY_PROT  2
 
        int                     (*init_state)(struct xfrm_state *x);
        void                    (*destructor)(struct xfrm_state *);
 
 extern unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2];
 
+/*
+ * This structure is used for the duration where packets are being
+ * transformed by IPsec.  As soon as the packet leaves IPsec the
+ * area beyond the generic IP part may be overwritten.
+ */
+struct xfrm_skb_cb {
+       union {
+               struct inet_skb_parm h4;
+               struct inet6_skb_parm h6;
+        } header;
+
+        /* Sequence number for replay protection. */
+        u64 seq;
+};
+
+#define XFRM_SKB_CB(__skb) ((struct xfrm_skb_cb *)&((__skb)->cb[0]))
+
 /* Audit Information */
 struct xfrm_audit
 {
 
 
        ah->reserved = 0;
        ah->spi = x->id.spi;
-       ah->seq_no = htonl(++x->replay.oseq);
-       xfrm_aevent_doreplay(x);
+       ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq);
        err = ah_mac_digest(ahp, skb, ah->auth_data);
        if (err)
                goto error;
        .description    = "AH4",
        .owner          = THIS_MODULE,
        .proto          = IPPROTO_AH,
+       .flags          = XFRM_TYPE_REPLAY_PROT,
        .init_state     = ah_init_state,
        .destructor     = ah_destroy,
        .input          = ah_input,
 
                top_iph->protocol = IPPROTO_ESP;
 
        esph->spi = x->id.spi;
-       esph->seq_no = htonl(++x->replay.oseq);
-       xfrm_aevent_doreplay(x);
+       esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq);
 
        if (esp->conf.ivlen) {
                if (unlikely(!esp->conf.ivinitted)) {
        .description    = "ESP4",
        .owner          = THIS_MODULE,
        .proto          = IPPROTO_ESP,
+       .flags          = XFRM_TYPE_REPLAY_PROT,
        .init_state     = esp_init_state,
        .destructor     = esp_destroy,
        .get_mtu        = esp4_get_mtu,
 
 
        ah->reserved = 0;
        ah->spi = x->id.spi;
-       ah->seq_no = htonl(++x->replay.oseq);
-       xfrm_aevent_doreplay(x);
+       ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq);
        err = ah_mac_digest(ahp, skb, ah->auth_data);
        if (err)
                goto error_free_iph;
        .description    = "AH6",
        .owner          = THIS_MODULE,
        .proto          = IPPROTO_AH,
+       .flags          = XFRM_TYPE_REPLAY_PROT,
        .init_state     = ah6_init_state,
        .destructor     = ah6_destroy,
        .input          = ah6_input,
 
        *skb_network_header(skb) = IPPROTO_ESP;
 
        esph->spi = x->id.spi;
-       esph->seq_no = htonl(++x->replay.oseq);
-       xfrm_aevent_doreplay(x);
+       esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq);
 
        if (esp->conf.ivlen) {
                if (unlikely(!esp->conf.ivinitted)) {
        .description    = "ESP6",
        .owner          = THIS_MODULE,
        .proto          = IPPROTO_ESP,
+       .flags          = XFRM_TYPE_REPLAY_PROT,
        .init_state     = esp6_init_state,
        .destructor     = esp6_destroy,
        .get_mtu        = esp6_get_mtu,
 
                if (err)
                        goto error;
 
+               if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
+                       XFRM_SKB_CB(skb)->seq = ++x->replay.oseq;
+                       xfrm_aevent_doreplay(x);
+               }
+
                err = x->mode->output(x, skb);
                if (err)
                        goto error;