[IPV6]: Add ip6_local_out
[linux-2.6-block.git] / net / xfrm / xfrm_output.c
CommitLineData
406ef77c
HX
1/*
2 * xfrm_output.c - Common IPsec encapsulation code.
3 *
4 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
5 *
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.
10 */
11
12#include <linux/errno.h>
13#include <linux/module.h>
14#include <linux/netdevice.h>
15#include <linux/skbuff.h>
16#include <linux/spinlock.h>
406ef77c
HX
17#include <net/dst.h>
18#include <net/xfrm.h>
19
83815dea
HX
20static int xfrm_state_check_space(struct xfrm_state *x, struct sk_buff *skb)
21{
550ade84
HX
22 struct dst_entry *dst = skb->dst;
23 int nhead = dst->header_len + LL_RESERVED_SPACE(dst->dev)
83815dea
HX
24 - skb_headroom(skb);
25
26 if (nhead > 0)
27 return pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
28
29 /* Check tail too... */
30 return 0;
31}
32
33static int xfrm_state_check(struct xfrm_state *x, struct sk_buff *skb)
34{
35 int err = xfrm_state_check_expire(x);
36 if (err < 0)
37 goto err;
38 err = xfrm_state_check_space(x, skb);
39err:
40 return err;
41}
42
406ef77c
HX
43int xfrm_output(struct sk_buff *skb)
44{
45 struct dst_entry *dst = skb->dst;
46 struct xfrm_state *x = dst->xfrm;
47 int err;
48
49 if (skb->ip_summed == CHECKSUM_PARTIAL) {
50 err = skb_checksum_help(skb);
51 if (err)
52 goto error_nolock;
53 }
54
55 do {
a2deb6d2
HX
56 err = x->outer_mode->output(x, skb);
57 if (err)
58 goto error;
59
406ef77c
HX
60 spin_lock_bh(&x->lock);
61 err = xfrm_state_check(x, skb);
62 if (err)
63 goto error;
64
436a0a40
HX
65 if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
66 XFRM_SKB_CB(skb)->seq = ++x->replay.oseq;
cdf7e668
HX
67 if (xfrm_aevent_is_on())
68 xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
436a0a40
HX
69 }
70
406ef77c
HX
71 x->curlft.bytes += skb->len;
72 x->curlft.packets++;
73
406ef77c
HX
74 spin_unlock_bh(&x->lock);
75
b7c6538c
HX
76 err = x->type->output(x, skb);
77 if (err)
78 goto error_nolock;
79
406ef77c
HX
80 if (!(skb->dst = dst_pop(dst))) {
81 err = -EHOSTUNREACH;
82 goto error_nolock;
83 }
84 dst = skb->dst;
85 x = dst->xfrm;
13996378 86 } while (x && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL));
406ef77c
HX
87
88 err = 0;
89
90error_nolock:
91 return err;
92error:
93 spin_unlock_bh(&x->lock);
94 goto error_nolock;
95}
96EXPORT_SYMBOL_GPL(xfrm_output);