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);
57 unsigned int nhoff = offsetof(struct iphdr, protocol);
63 const struct iphdr *iph = ip_hdr(skb);
66 if (xfrm_nr == XFRM_MAX_DEPTH)
69 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, spi,
70 iph->protocol != IPPROTO_IPV6 ? iph->protocol : IPPROTO_IPIP, AF_INET);
75 if (unlikely(x->km.state != XFRM_STATE_VALID))
78 if ((x->encap ? x->encap->encap_type : 0) != encap_type)
81 if (x->props.replay_window && xfrm_replay_check(x, seq))
84 if (xfrm_state_check_expire(x))
87 nexthdr = x->type->input(x, skb);
91 skb_network_header(skb)[nhoff] = nexthdr;
93 /* only the first xfrm gets the encap type */
96 if (x->props.replay_window)
97 xfrm_replay_advance(x, seq);
99 x->curlft.bytes += skb->len;
102 spin_unlock(&x->lock);
104 xfrm_vec[xfrm_nr++] = x;
106 if (x->mode->input(x, skb))
109 if (x->props.mode == XFRM_MODE_TUNNEL) {
114 err = xfrm_parse_spi(skb, ip_hdr(skb)->protocol, &spi, &seq);
119 /* Allocate new secpath or COW existing one. */
121 if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
123 sp = secpath_dup(skb->sp);
127 secpath_put(skb->sp);
130 if (xfrm_nr + skb->sp->len > XFRM_MAX_DEPTH)
133 memcpy(skb->sp->xvec + skb->sp->len, xfrm_vec,
134 xfrm_nr * sizeof(xfrm_vec[0]));
135 skb->sp->len += xfrm_nr;
140 dst_release(skb->dst);
145 #ifdef CONFIG_NETFILTER
146 __skb_push(skb, skb->data - skb_network_header(skb));
147 ip_hdr(skb)->tot_len = htons(skb->len);
148 ip_send_check(ip_hdr(skb));
150 NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
151 xfrm4_rcv_encap_finish);
154 return -ip_hdr(skb)->protocol;
159 spin_unlock(&x->lock);
162 while (--xfrm_nr >= 0)
163 xfrm_state_put(xfrm_vec[xfrm_nr]);
169 /* If it's a keepalive packet, then just eat it.
170 * If it's an encapsulated packet, then pass it to the
172 * Returns 0 if skb passed to xfrm or was dropped.
173 * Returns >0 if skb should be passed to UDP.
174 * Returns <0 if skb should be resubmitted (-ret is protocol)
176 int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
178 struct udp_sock *up = udp_sk(sk);
186 __u16 encap_type = up->encap_type;
188 /* if this is not encapsulated socket, then just return now */
192 /* If this is a paged skb, make sure we pull up
193 * whatever data we need to look at. */
194 len = skb->len - sizeof(struct udphdr);
195 if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8)))
198 /* Now we can get the pointers */
200 udpdata = (__u8 *)uh + sizeof(struct udphdr);
201 udpdata32 = (__be32 *)udpdata;
203 switch (encap_type) {
205 case UDP_ENCAP_ESPINUDP:
206 /* Check if this is a keepalive packet. If so, eat it. */
207 if (len == 1 && udpdata[0] == 0xff) {
209 } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) {
210 /* ESP Packet without Non-ESP header */
211 len = sizeof(struct udphdr);
213 /* Must be an IKE packet.. pass it through */
216 case UDP_ENCAP_ESPINUDP_NON_IKE:
217 /* Check if this is a keepalive packet. If so, eat it. */
218 if (len == 1 && udpdata[0] == 0xff) {
220 } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) &&
221 udpdata32[0] == 0 && udpdata32[1] == 0) {
223 /* ESP Packet with Non-IKE marker */
224 len = sizeof(struct udphdr) + 2 * sizeof(u32);
226 /* Must be an IKE packet.. pass it through */
231 /* At this point we are sure that this is an ESPinUDP packet,
232 * so we need to remove 'len' bytes from the packet (the UDP
233 * header and optional ESP marker bytes) and then modify the
234 * protocol to ESP, and then call into the transform receiver.
236 if (skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
239 /* Now we can update and verify the packet length... */
241 iphlen = iph->ihl << 2;
242 iph->tot_len = htons(ntohs(iph->tot_len) - len);
243 if (skb->len < iphlen + len) {
244 /* packet is too small!?! */
248 /* pull the data buffer up to the ESP header and set the
249 * transport header to point to ESP. Keep UDP on the stack
252 __skb_pull(skb, len);
253 skb_reset_transport_header(skb);
255 /* modify the protocol (it's ESP!) */
256 iph->protocol = IPPROTO_ESP;
259 ret = xfrm4_rcv_encap(skb, encap_type);
267 int xfrm4_rcv(struct sk_buff *skb)
269 return xfrm4_rcv_encap(skb, 0);
272 EXPORT_SYMBOL(xfrm4_rcv);