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>
16 #include <net/inet_ecn.h>
20 int xfrm4_rcv(struct sk_buff *skb)
22 return xfrm4_rcv_encap(skb, 0);
25 EXPORT_SYMBOL(xfrm4_rcv);
27 static inline void ipip_ecn_decapsulate(struct sk_buff *skb)
29 struct iphdr *outer_iph = skb->nh.iph;
30 struct iphdr *inner_iph = skb->h.ipiph;
32 if (INET_ECN_is_ce(outer_iph->tos))
33 IP_ECN_set_ce(inner_iph);
36 static int xfrm4_parse_spi(struct sk_buff *skb, u8 nexthdr, u32 *spi, u32 *seq)
40 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
42 *spi = skb->nh.iph->saddr;
47 return xfrm_parse_spi(skb, nexthdr, spi, seq);
50 #ifdef CONFIG_NETFILTER
51 static inline int xfrm4_rcv_encap_finish(struct sk_buff *skb)
53 struct iphdr *iph = skb->nh.iph;
55 if (skb->dst == NULL) {
56 if (ip_route_input(skb, iph->daddr, iph->saddr, iph->tos,
60 return dst_input(skb);
67 int xfrm4_rcv_encap(struct sk_buff *skb, __u16 encap_type)
71 struct sec_decap_state xfrm_vec[XFRM_MAX_DEPTH];
76 if ((err = xfrm4_parse_spi(skb, skb->nh.iph->protocol, &spi, &seq)) != 0)
80 struct iphdr *iph = skb->nh.iph;
82 if (xfrm_nr == XFRM_MAX_DEPTH)
85 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, spi, iph->protocol, AF_INET);
90 if (unlikely(x->km.state != XFRM_STATE_VALID))
93 if (x->props.replay_window && xfrm_replay_check(x, seq))
96 if (xfrm_state_check_expire(x))
99 xfrm_vec[xfrm_nr].decap.decap_type = encap_type;
100 if (x->type->input(x, &(xfrm_vec[xfrm_nr].decap), skb))
103 /* only the first xfrm gets the encap type */
106 if (x->props.replay_window)
107 xfrm_replay_advance(x, seq);
109 x->curlft.bytes += skb->len;
112 spin_unlock(&x->lock);
114 xfrm_vec[xfrm_nr++].xvec = x;
119 if (iph->protocol != IPPROTO_IPIP)
121 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
123 if (skb_cloned(skb) &&
124 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
126 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
127 ipv4_copy_dscp(iph, skb->h.ipiph);
128 if (!(x->props.flags & XFRM_STATE_NOECN))
129 ipip_ecn_decapsulate(skb);
130 skb->mac.raw = memmove(skb->data - skb->mac_len,
131 skb->mac.raw, skb->mac_len);
132 skb->nh.raw = skb->data;
133 memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
138 if ((err = xfrm_parse_spi(skb, skb->nh.iph->protocol, &spi, &seq)) < 0)
142 /* Allocate new secpath or COW existing one. */
144 if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
146 sp = secpath_dup(skb->sp);
150 secpath_put(skb->sp);
153 if (xfrm_nr + skb->sp->len > XFRM_MAX_DEPTH)
156 memcpy(skb->sp->x+skb->sp->len, xfrm_vec, xfrm_nr*sizeof(struct sec_decap_state));
157 skb->sp->len += xfrm_nr;
162 if (!(skb->dev->flags&IFF_LOOPBACK)) {
163 dst_release(skb->dst);
169 #ifdef CONFIG_NETFILTER
170 __skb_push(skb, skb->data - skb->nh.raw);
171 skb->nh.iph->tot_len = htons(skb->len);
172 ip_send_check(skb->nh.iph);
174 NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
175 xfrm4_rcv_encap_finish);
178 return -skb->nh.iph->protocol;
183 spin_unlock(&x->lock);
186 while (--xfrm_nr >= 0)
187 xfrm_state_put(xfrm_vec[xfrm_nr].xvec);