]>
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 | 45 | err = xfrm_state_check_space(x, skb); |
b15c4bcd MN |
46 | if (err) { |
47 | XFRM_INC_STATS(LINUX_MIB_XFRMOUTERROR); | |
910ef70a | 48 | goto error_nolock; |
b15c4bcd | 49 | } |
910ef70a | 50 | |
a2deb6d2 | 51 | err = x->outer_mode->output(x, skb); |
b15c4bcd MN |
52 | if (err) { |
53 | XFRM_INC_STATS(LINUX_MIB_XFRMOUTSTATEMODEERROR); | |
910ef70a | 54 | goto error_nolock; |
b15c4bcd | 55 | } |
a2deb6d2 | 56 | |
406ef77c | 57 | spin_lock_bh(&x->lock); |
910ef70a | 58 | err = xfrm_state_check_expire(x); |
b15c4bcd MN |
59 | if (err) { |
60 | XFRM_INC_STATS(LINUX_MIB_XFRMOUTSTATEEXPIRED); | |
406ef77c | 61 | goto error; |
b15c4bcd | 62 | } |
406ef77c | 63 | |
436a0a40 HX |
64 | if (x->type->flags & XFRM_TYPE_REPLAY_PROT) { |
65 | XFRM_SKB_CB(skb)->seq = ++x->replay.oseq; | |
e1af9f27 PM |
66 | if (unlikely(x->replay.oseq == 0)) { |
67 | x->replay.oseq--; | |
afeb14b4 | 68 | xfrm_audit_state_replay_overflow(x, skb); |
e1af9f27 PM |
69 | goto error; |
70 | } | |
cdf7e668 HX |
71 | if (xfrm_aevent_is_on()) |
72 | xfrm_replay_notify(x, XFRM_REPLAY_UPDATE); | |
436a0a40 HX |
73 | } |
74 | ||
406ef77c HX |
75 | x->curlft.bytes += skb->len; |
76 | x->curlft.packets++; | |
77 | ||
406ef77c HX |
78 | spin_unlock_bh(&x->lock); |
79 | ||
b7c6538c | 80 | err = x->type->output(x, skb); |
c6581a45 HX |
81 | |
82 | resume: | |
0aa64774 MN |
83 | if (err) { |
84 | XFRM_INC_STATS(LINUX_MIB_XFRMOUTSTATEPROTOERROR); | |
b7c6538c | 85 | goto error_nolock; |
0aa64774 | 86 | } |
b7c6538c | 87 | |
406ef77c | 88 | if (!(skb->dst = dst_pop(dst))) { |
0aa64774 | 89 | XFRM_INC_STATS(LINUX_MIB_XFRMOUTERROR); |
406ef77c HX |
90 | err = -EHOSTUNREACH; |
91 | goto error_nolock; | |
92 | } | |
93 | dst = skb->dst; | |
94 | x = dst->xfrm; | |
13996378 | 95 | } while (x && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL)); |
406ef77c HX |
96 | |
97 | err = 0; | |
98 | ||
862b82c6 | 99 | out_exit: |
406ef77c HX |
100 | return err; |
101 | error: | |
102 | spin_unlock_bh(&x->lock); | |
862b82c6 HX |
103 | error_nolock: |
104 | kfree_skb(skb); | |
105 | goto out_exit; | |
106 | } | |
107 | ||
c6581a45 | 108 | int xfrm_output_resume(struct sk_buff *skb, int err) |
862b82c6 | 109 | { |
c6581a45 | 110 | while (likely((err = xfrm_output_one(skb, err)) == 0)) { |
862b82c6 HX |
111 | struct xfrm_state *x; |
112 | ||
113 | nf_reset(skb); | |
114 | ||
115 | err = skb->dst->ops->local_out(skb); | |
116 | if (unlikely(err != 1)) | |
c6581a45 | 117 | goto out; |
862b82c6 HX |
118 | |
119 | x = skb->dst->xfrm; | |
120 | if (!x) | |
121 | return dst_output(skb); | |
122 | ||
123 | err = nf_hook(x->inner_mode->afinfo->family, | |
294b4baf | 124 | NF_INET_POST_ROUTING, skb, |
862b82c6 HX |
125 | NULL, skb->dst->dev, xfrm_output2); |
126 | if (unlikely(err != 1)) | |
c6581a45 | 127 | goto out; |
862b82c6 HX |
128 | } |
129 | ||
c6581a45 HX |
130 | if (err == -EINPROGRESS) |
131 | err = 0; | |
132 | ||
133 | out: | |
862b82c6 HX |
134 | return err; |
135 | } | |
c6581a45 | 136 | EXPORT_SYMBOL_GPL(xfrm_output_resume); |
862b82c6 | 137 | |
c6581a45 | 138 | static int xfrm_output2(struct sk_buff *skb) |
862b82c6 | 139 | { |
c6581a45 HX |
140 | return xfrm_output_resume(skb, 1); |
141 | } | |
862b82c6 | 142 | |
c6581a45 HX |
143 | static int xfrm_output_gso(struct sk_buff *skb) |
144 | { | |
145 | struct sk_buff *segs; | |
862b82c6 HX |
146 | |
147 | segs = skb_gso_segment(skb, 0); | |
148 | kfree_skb(skb); | |
149 | if (unlikely(IS_ERR(segs))) | |
150 | return PTR_ERR(segs); | |
151 | ||
152 | do { | |
153 | struct sk_buff *nskb = segs->next; | |
154 | int err; | |
155 | ||
156 | segs->next = NULL; | |
157 | err = xfrm_output2(segs); | |
158 | ||
159 | if (unlikely(err)) { | |
160 | while ((segs = nskb)) { | |
161 | nskb = segs->next; | |
162 | segs->next = NULL; | |
163 | kfree_skb(segs); | |
164 | } | |
165 | return err; | |
166 | } | |
167 | ||
168 | segs = nskb; | |
169 | } while (segs); | |
170 | ||
171 | return 0; | |
406ef77c | 172 | } |
c6581a45 HX |
173 | |
174 | int xfrm_output(struct sk_buff *skb) | |
175 | { | |
176 | int err; | |
177 | ||
178 | if (skb_is_gso(skb)) | |
179 | return xfrm_output_gso(skb); | |
180 | ||
181 | if (skb->ip_summed == CHECKSUM_PARTIAL) { | |
182 | err = skb_checksum_help(skb); | |
183 | if (err) { | |
0aa64774 | 184 | XFRM_INC_STATS(LINUX_MIB_XFRMOUTERROR); |
c6581a45 HX |
185 | kfree_skb(skb); |
186 | return err; | |
187 | } | |
188 | } | |
189 | ||
190 | return xfrm_output2(skb); | |
191 | } | |
406ef77c | 192 | EXPORT_SYMBOL_GPL(xfrm_output); |