Commit | Line | Data |
---|---|---|
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> | |
862b82c6 | 15 | #include <linux/netfilter.h> |
406ef77c HX |
16 | #include <linux/skbuff.h> |
17 | #include <linux/spinlock.h> | |
406ef77c HX |
18 | #include <net/dst.h> |
19 | #include <net/xfrm.h> | |
20 | ||
c6581a45 HX |
21 | static int xfrm_output2(struct sk_buff *skb); |
22 | ||
83815dea HX |
23 | static int xfrm_state_check_space(struct xfrm_state *x, struct sk_buff *skb) |
24 | { | |
550ade84 HX |
25 | struct dst_entry *dst = skb->dst; |
26 | int nhead = dst->header_len + LL_RESERVED_SPACE(dst->dev) | |
83815dea HX |
27 | - skb_headroom(skb); |
28 | ||
29 | if (nhead > 0) | |
30 | return pskb_expand_head(skb, nhead, 0, GFP_ATOMIC); | |
31 | ||
32 | /* Check tail too... */ | |
33 | return 0; | |
34 | } | |
35 | ||
c6581a45 | 36 | static int xfrm_output_one(struct sk_buff *skb, int err) |
406ef77c HX |
37 | { |
38 | struct dst_entry *dst = skb->dst; | |
39 | struct xfrm_state *x = dst->xfrm; | |
406ef77c | 40 | |
c6581a45 HX |
41 | if (err <= 0) |
42 | goto resume; | |
406ef77c HX |
43 | |
44 | do { | |
910ef70a HX |
45 | err = xfrm_state_check_space(x, skb); |
46 | if (err) | |
47 | goto error_nolock; | |
48 | ||
a2deb6d2 HX |
49 | err = x->outer_mode->output(x, skb); |
50 | if (err) | |
910ef70a | 51 | goto error_nolock; |
a2deb6d2 | 52 | |
406ef77c | 53 | spin_lock_bh(&x->lock); |
910ef70a | 54 | err = xfrm_state_check_expire(x); |
406ef77c HX |
55 | if (err) |
56 | goto error; | |
57 | ||
436a0a40 HX |
58 | if (x->type->flags & XFRM_TYPE_REPLAY_PROT) { |
59 | XFRM_SKB_CB(skb)->seq = ++x->replay.oseq; | |
cdf7e668 HX |
60 | if (xfrm_aevent_is_on()) |
61 | xfrm_replay_notify(x, XFRM_REPLAY_UPDATE); | |
436a0a40 HX |
62 | } |
63 | ||
406ef77c HX |
64 | x->curlft.bytes += skb->len; |
65 | x->curlft.packets++; | |
66 | ||
406ef77c HX |
67 | spin_unlock_bh(&x->lock); |
68 | ||
b7c6538c | 69 | err = x->type->output(x, skb); |
c6581a45 HX |
70 | |
71 | resume: | |
b7c6538c HX |
72 | if (err) |
73 | goto error_nolock; | |
74 | ||
406ef77c HX |
75 | if (!(skb->dst = dst_pop(dst))) { |
76 | err = -EHOSTUNREACH; | |
77 | goto error_nolock; | |
78 | } | |
79 | dst = skb->dst; | |
80 | x = dst->xfrm; | |
13996378 | 81 | } while (x && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL)); |
406ef77c HX |
82 | |
83 | err = 0; | |
84 | ||
862b82c6 | 85 | out_exit: |
406ef77c HX |
86 | return err; |
87 | error: | |
88 | spin_unlock_bh(&x->lock); | |
862b82c6 HX |
89 | error_nolock: |
90 | kfree_skb(skb); | |
91 | goto out_exit; | |
92 | } | |
93 | ||
c6581a45 | 94 | int xfrm_output_resume(struct sk_buff *skb, int err) |
862b82c6 | 95 | { |
c6581a45 | 96 | while (likely((err = xfrm_output_one(skb, err)) == 0)) { |
862b82c6 HX |
97 | struct xfrm_state *x; |
98 | ||
99 | nf_reset(skb); | |
100 | ||
101 | err = skb->dst->ops->local_out(skb); | |
102 | if (unlikely(err != 1)) | |
c6581a45 | 103 | goto out; |
862b82c6 HX |
104 | |
105 | x = skb->dst->xfrm; | |
106 | if (!x) | |
107 | return dst_output(skb); | |
108 | ||
109 | err = nf_hook(x->inner_mode->afinfo->family, | |
294b4baf | 110 | NF_INET_POST_ROUTING, skb, |
862b82c6 HX |
111 | NULL, skb->dst->dev, xfrm_output2); |
112 | if (unlikely(err != 1)) | |
c6581a45 | 113 | goto out; |
862b82c6 HX |
114 | } |
115 | ||
c6581a45 HX |
116 | if (err == -EINPROGRESS) |
117 | err = 0; | |
118 | ||
119 | out: | |
862b82c6 HX |
120 | return err; |
121 | } | |
c6581a45 | 122 | EXPORT_SYMBOL_GPL(xfrm_output_resume); |
862b82c6 | 123 | |
c6581a45 | 124 | static int xfrm_output2(struct sk_buff *skb) |
862b82c6 | 125 | { |
c6581a45 HX |
126 | return xfrm_output_resume(skb, 1); |
127 | } | |
862b82c6 | 128 | |
c6581a45 HX |
129 | static int xfrm_output_gso(struct sk_buff *skb) |
130 | { | |
131 | struct sk_buff *segs; | |
862b82c6 HX |
132 | |
133 | segs = skb_gso_segment(skb, 0); | |
134 | kfree_skb(skb); | |
135 | if (unlikely(IS_ERR(segs))) | |
136 | return PTR_ERR(segs); | |
137 | ||
138 | do { | |
139 | struct sk_buff *nskb = segs->next; | |
140 | int err; | |
141 | ||
142 | segs->next = NULL; | |
143 | err = xfrm_output2(segs); | |
144 | ||
145 | if (unlikely(err)) { | |
146 | while ((segs = nskb)) { | |
147 | nskb = segs->next; | |
148 | segs->next = NULL; | |
149 | kfree_skb(segs); | |
150 | } | |
151 | return err; | |
152 | } | |
153 | ||
154 | segs = nskb; | |
155 | } while (segs); | |
156 | ||
157 | return 0; | |
406ef77c | 158 | } |
c6581a45 HX |
159 | |
160 | int xfrm_output(struct sk_buff *skb) | |
161 | { | |
162 | int err; | |
163 | ||
164 | if (skb_is_gso(skb)) | |
165 | return xfrm_output_gso(skb); | |
166 | ||
167 | if (skb->ip_summed == CHECKSUM_PARTIAL) { | |
168 | err = skb_checksum_help(skb); | |
169 | if (err) { | |
170 | kfree_skb(skb); | |
171 | return err; | |
172 | } | |
173 | } | |
174 | ||
175 | return xfrm_output2(skb); | |
176 | } | |
406ef77c | 177 | EXPORT_SYMBOL_GPL(xfrm_output); |