2 * xfrm_output.c - Common IPsec encapsulation code.
4 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/errno.h>
13 #include <linux/module.h>
14 #include <linux/netdevice.h>
15 #include <linux/netfilter.h>
16 #include <linux/skbuff.h>
17 #include <linux/spinlock.h>
21 static int xfrm_state_check_space(struct xfrm_state *x, struct sk_buff *skb)
23 struct dst_entry *dst = skb->dst;
24 int nhead = dst->header_len + LL_RESERVED_SPACE(dst->dev)
28 return pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
30 /* Check tail too... */
34 static int xfrm_state_check(struct xfrm_state *x, struct sk_buff *skb)
36 int err = xfrm_state_check_expire(x);
39 err = xfrm_state_check_space(x, skb);
44 static int xfrm_output_one(struct sk_buff *skb)
46 struct dst_entry *dst = skb->dst;
47 struct xfrm_state *x = dst->xfrm;
50 if (skb->ip_summed == CHECKSUM_PARTIAL) {
51 err = skb_checksum_help(skb);
57 err = x->outer_mode->output(x, skb);
61 spin_lock_bh(&x->lock);
62 err = xfrm_state_check(x, skb);
66 if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
67 XFRM_SKB_CB(skb)->seq = ++x->replay.oseq;
68 if (xfrm_aevent_is_on())
69 xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
72 x->curlft.bytes += skb->len;
75 spin_unlock_bh(&x->lock);
77 err = x->type->output(x, skb);
81 if (!(skb->dst = dst_pop(dst))) {
87 } while (x && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL));
94 spin_unlock_bh(&x->lock);
100 static int xfrm_output2(struct sk_buff *skb)
104 while (likely((err = xfrm_output_one(skb)) == 0)) {
105 struct xfrm_state *x;
109 err = skb->dst->ops->local_out(skb);
110 if (unlikely(err != 1))
115 return dst_output(skb);
117 err = nf_hook(x->inner_mode->afinfo->family,
118 x->inner_mode->afinfo->nf_post_routing, skb,
119 NULL, skb->dst->dev, xfrm_output2);
120 if (unlikely(err != 1))
127 int xfrm_output(struct sk_buff *skb)
129 struct sk_buff *segs;
131 if (!skb_is_gso(skb))
132 return xfrm_output2(skb);
134 segs = skb_gso_segment(skb, 0);
136 if (unlikely(IS_ERR(segs)))
137 return PTR_ERR(segs);
140 struct sk_buff *nskb = segs->next;
144 err = xfrm_output2(segs);
147 while ((segs = nskb)) {
160 EXPORT_SYMBOL_GPL(xfrm_output);