5 * YOSHIFUJI Hideaki @USAGI
6 * Split up af-specific portion
7 * Derek Atkins <derek@ihtfp.com>
8 * Add Encapsulation support
12 #include <linux/module.h>
13 #include <linux/string.h>
14 #include <linux/netfilter.h>
15 #include <linux/netfilter_ipv4.h>
19 static int xfrm4_parse_spi(struct sk_buff
*skb
, u8 nexthdr
, __be32
*spi
, __be32
*seq
)
24 *spi
= ip_hdr(skb
)->saddr
;
29 return xfrm_parse_spi(skb
, nexthdr
, spi
, seq
);
32 #ifdef CONFIG_NETFILTER
33 static inline int xfrm4_rcv_encap_finish(struct sk_buff
*skb
)
35 if (skb
->dst
== NULL
) {
36 const struct iphdr
*iph
= ip_hdr(skb
);
38 if (ip_route_input(skb
, iph
->daddr
, iph
->saddr
, iph
->tos
,
42 return dst_input(skb
);
49 static int xfrm4_rcv_encap(struct sk_buff
*skb
, __u16 encap_type
)
52 struct xfrm_state
*xfrm_vec
[XFRM_MAX_DEPTH
];
56 int err
= xfrm4_parse_spi(skb
, ip_hdr(skb
)->protocol
, &spi
, &seq
);
62 const struct iphdr
*iph
= ip_hdr(skb
);
64 if (xfrm_nr
== XFRM_MAX_DEPTH
)
67 x
= xfrm_state_lookup((xfrm_address_t
*)&iph
->daddr
, spi
,
68 iph
->protocol
!= IPPROTO_IPV6
? iph
->protocol
: IPPROTO_IPIP
, AF_INET
);
73 if (unlikely(x
->km
.state
!= XFRM_STATE_VALID
))
76 if ((x
->encap
? x
->encap
->encap_type
: 0) != encap_type
)
79 if (x
->props
.replay_window
&& xfrm_replay_check(x
, seq
))
82 if (xfrm_state_check_expire(x
))
85 if (x
->type
->input(x
, skb
))
88 /* only the first xfrm gets the encap type */
91 if (x
->props
.replay_window
)
92 xfrm_replay_advance(x
, seq
);
94 x
->curlft
.bytes
+= skb
->len
;
97 spin_unlock(&x
->lock
);
99 xfrm_vec
[xfrm_nr
++] = x
;
101 if (x
->mode
->input(x
, skb
))
104 if (x
->props
.mode
== XFRM_MODE_TUNNEL
) {
109 err
= xfrm_parse_spi(skb
, ip_hdr(skb
)->protocol
, &spi
, &seq
);
114 /* Allocate new secpath or COW existing one. */
116 if (!skb
->sp
|| atomic_read(&skb
->sp
->refcnt
) != 1) {
118 sp
= secpath_dup(skb
->sp
);
122 secpath_put(skb
->sp
);
125 if (xfrm_nr
+ skb
->sp
->len
> XFRM_MAX_DEPTH
)
128 memcpy(skb
->sp
->xvec
+ skb
->sp
->len
, xfrm_vec
,
129 xfrm_nr
* sizeof(xfrm_vec
[0]));
130 skb
->sp
->len
+= xfrm_nr
;
135 dst_release(skb
->dst
);
140 #ifdef CONFIG_NETFILTER
141 __skb_push(skb
, skb
->data
- skb_network_header(skb
));
142 ip_hdr(skb
)->tot_len
= htons(skb
->len
);
143 ip_send_check(ip_hdr(skb
));
145 NF_HOOK(PF_INET
, NF_IP_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
146 xfrm4_rcv_encap_finish
);
149 return -ip_hdr(skb
)->protocol
;
154 spin_unlock(&x
->lock
);
157 while (--xfrm_nr
>= 0)
158 xfrm_state_put(xfrm_vec
[xfrm_nr
]);
164 /* If it's a keepalive packet, then just eat it.
165 * If it's an encapsulated packet, then pass it to the
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)
171 int xfrm4_udp_encap_rcv(struct sock
*sk
, struct sk_buff
*skb
)
173 struct udp_sock
*up
= udp_sk(sk
);
181 __u16 encap_type
= up
->encap_type
;
183 /* if this is not encapsulated socket, then just return now */
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)))
193 /* Now we can get the pointers */
195 udpdata
= (__u8
*)uh
+ sizeof(struct udphdr
);
196 udpdata32
= (__be32
*)udpdata
;
198 switch (encap_type
) {
200 case UDP_ENCAP_ESPINUDP
:
201 /* Check if this is a keepalive packet. If so, eat it. */
202 if (len
== 1 && udpdata
[0] == 0xff) {
204 } else if (len
> sizeof(struct ip_esp_hdr
) && udpdata32
[0] != 0) {
205 /* ESP Packet without Non-ESP header */
206 len
= sizeof(struct udphdr
);
208 /* Must be an IKE packet.. pass it through */
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) {
215 } else if (len
> 2 * sizeof(u32
) + sizeof(struct ip_esp_hdr
) &&
216 udpdata32
[0] == 0 && udpdata32
[1] == 0) {
218 /* ESP Packet with Non-IKE marker */
219 len
= sizeof(struct udphdr
) + 2 * sizeof(u32
);
221 /* Must be an IKE packet.. pass it through */
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.
231 if (skb_cloned(skb
) && pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))
234 /* Now we can update and verify the packet length... */
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!?! */
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
247 __skb_pull(skb
, len
);
248 skb_reset_transport_header(skb
);
250 /* modify the protocol (it's ESP!) */
251 iph
->protocol
= IPPROTO_ESP
;
254 ret
= xfrm4_rcv_encap(skb
, encap_type
);
262 int xfrm4_rcv(struct sk_buff
*skb
)
264 return xfrm4_rcv_encap(skb
, 0);
267 EXPORT_SYMBOL(xfrm4_rcv
);