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