diff options
author | James Chapman <jchapman@katalix.com> | 2007-07-05 20:08:05 -0400 |
---|---|---|
committer | David S. Miller <davem@sunset.davemloft.net> | 2007-07-11 01:16:53 -0400 |
commit | 067b207b281db5e3f03f8d244286c20f61aa2343 (patch) | |
tree | a947ba1fdc03ca82fdb2c7d6f8334b995c1c5ab2 /net/ipv4/xfrm4_input.c | |
parent | 93cce3d3657bfb5d04789afcd5104f8c48700f32 (diff) |
[UDP]: Cleanup UDP encapsulation code
This cleanup fell out after adding L2TP support where a new encap_rcv
funcptr was added to struct udp_sock. Have XFRM use the new encap_rcv
funcptr, which allows us to move the XFRM encap code from udp.c into
xfrm4_input.c.
Make xfrm4_rcv_encap() static since it is no longer called externally.
Signed-off-by: James Chapman <jchapman@katalix.com>
Acked-by: Patrick McHardy <kaber@trash.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/ipv4/xfrm4_input.c')
-rw-r--r-- | net/ipv4/xfrm4_input.c | 114 |
1 files changed, 106 insertions, 8 deletions
diff --git a/net/ipv4/xfrm4_input.c b/net/ipv4/xfrm4_input.c index fa1902dc81b8..2fa108245413 100644 --- a/net/ipv4/xfrm4_input.c +++ b/net/ipv4/xfrm4_input.c | |||
@@ -16,13 +16,6 @@ | |||
16 | #include <net/ip.h> | 16 | #include <net/ip.h> |
17 | #include <net/xfrm.h> | 17 | #include <net/xfrm.h> |
18 | 18 | ||
19 | int xfrm4_rcv(struct sk_buff *skb) | ||
20 | { | ||
21 | return xfrm4_rcv_encap(skb, 0); | ||
22 | } | ||
23 | |||
24 | EXPORT_SYMBOL(xfrm4_rcv); | ||
25 | |||
26 | static int xfrm4_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq) | 19 | static int xfrm4_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq) |
27 | { | 20 | { |
28 | switch (nexthdr) { | 21 | switch (nexthdr) { |
@@ -53,7 +46,7 @@ drop: | |||
53 | } | 46 | } |
54 | #endif | 47 | #endif |
55 | 48 | ||
56 | int xfrm4_rcv_encap(struct sk_buff *skb, __u16 encap_type) | 49 | static int xfrm4_rcv_encap(struct sk_buff *skb, __u16 encap_type) |
57 | { | 50 | { |
58 | __be32 spi, seq; | 51 | __be32 spi, seq; |
59 | struct xfrm_state *xfrm_vec[XFRM_MAX_DEPTH]; | 52 | struct xfrm_state *xfrm_vec[XFRM_MAX_DEPTH]; |
@@ -167,3 +160,108 @@ drop: | |||
167 | kfree_skb(skb); | 160 | kfree_skb(skb); |
168 | return 0; | 161 | return 0; |
169 | } | 162 | } |
163 | |||
164 | /* If it's a keepalive packet, then just eat it. | ||
165 | * If it's an encapsulated packet, then pass it to the | ||
166 | * IPsec xfrm input. | ||
167 | * Returns 0 if skb passed to xfrm or was dropped. | ||
168 | * Returns >0 if skb should be passed to UDP. | ||
169 | * Returns <0 if skb should be resubmitted (-ret is protocol) | ||
170 | */ | ||
171 | int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb) | ||
172 | { | ||
173 | struct udp_sock *up = udp_sk(sk); | ||
174 | struct udphdr *uh; | ||
175 | struct iphdr *iph; | ||
176 | int iphlen, len; | ||
177 | int ret; | ||
178 | |||
179 | __u8 *udpdata; | ||
180 | __be32 *udpdata32; | ||
181 | __u16 encap_type = up->encap_type; | ||
182 | |||
183 | /* if this is not encapsulated socket, then just return now */ | ||
184 | if (!encap_type) | ||
185 | return 1; | ||
186 | |||
187 | /* If this is a paged skb, make sure we pull up | ||
188 | * whatever data we need to look at. */ | ||
189 | len = skb->len - sizeof(struct udphdr); | ||
190 | if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8))) | ||
191 | return 1; | ||
192 | |||
193 | /* Now we can get the pointers */ | ||
194 | uh = udp_hdr(skb); | ||
195 | udpdata = (__u8 *)uh + sizeof(struct udphdr); | ||
196 | udpdata32 = (__be32 *)udpdata; | ||
197 | |||
198 | switch (encap_type) { | ||
199 | default: | ||
200 | case UDP_ENCAP_ESPINUDP: | ||
201 | /* Check if this is a keepalive packet. If so, eat it. */ | ||
202 | if (len == 1 && udpdata[0] == 0xff) { | ||
203 | goto drop; | ||
204 | } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) { | ||
205 | /* ESP Packet without Non-ESP header */ | ||
206 | len = sizeof(struct udphdr); | ||
207 | } else | ||
208 | /* Must be an IKE packet.. pass it through */ | ||
209 | return 1; | ||
210 | break; | ||
211 | case UDP_ENCAP_ESPINUDP_NON_IKE: | ||
212 | /* Check if this is a keepalive packet. If so, eat it. */ | ||
213 | if (len == 1 && udpdata[0] == 0xff) { | ||
214 | goto drop; | ||
215 | } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) && | ||
216 | udpdata32[0] == 0 && udpdata32[1] == 0) { | ||
217 | |||
218 | /* ESP Packet with Non-IKE marker */ | ||
219 | len = sizeof(struct udphdr) + 2 * sizeof(u32); | ||
220 | } else | ||
221 | /* Must be an IKE packet.. pass it through */ | ||
222 | return 1; | ||
223 | break; | ||
224 | } | ||
225 | |||
226 | /* At this point we are sure that this is an ESPinUDP packet, | ||
227 | * so we need to remove 'len' bytes from the packet (the UDP | ||
228 | * header and optional ESP marker bytes) and then modify the | ||
229 | * protocol to ESP, and then call into the transform receiver. | ||
230 | */ | ||
231 | if (skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) | ||
232 | goto drop; | ||
233 | |||
234 | /* Now we can update and verify the packet length... */ | ||
235 | iph = ip_hdr(skb); | ||
236 | iphlen = iph->ihl << 2; | ||
237 | iph->tot_len = htons(ntohs(iph->tot_len) - len); | ||
238 | if (skb->len < iphlen + len) { | ||
239 | /* packet is too small!?! */ | ||
240 | goto drop; | ||
241 | } | ||
242 | |||
243 | /* pull the data buffer up to the ESP header and set the | ||
244 | * transport header to point to ESP. Keep UDP on the stack | ||
245 | * for later. | ||
246 | */ | ||
247 | __skb_pull(skb, len); | ||
248 | skb_reset_transport_header(skb); | ||
249 | |||
250 | /* modify the protocol (it's ESP!) */ | ||
251 | iph->protocol = IPPROTO_ESP; | ||
252 | |||
253 | /* process ESP */ | ||
254 | ret = xfrm4_rcv_encap(skb, encap_type); | ||
255 | return ret; | ||
256 | |||
257 | drop: | ||
258 | kfree_skb(skb); | ||
259 | return 0; | ||
260 | } | ||
261 | |||
262 | int xfrm4_rcv(struct sk_buff *skb) | ||
263 | { | ||
264 | return xfrm4_rcv_encap(skb, 0); | ||
265 | } | ||
266 | |||
267 | EXPORT_SYMBOL(xfrm4_rcv); | ||