5 * YOSHIFUJI Hideaki @USAGI
6 * Split up af-specific portion
7 * Derek Atkins <derek@ihtfp.com>
8 * Add Encapsulation support
12 #include <linux/string.h>
13 #include <net/inet_ecn.h>
17 int xfrm4_rcv(struct sk_buff *skb)
19 return xfrm4_rcv_encap(skb, 0);
22 static inline void ipip_ecn_decapsulate(struct sk_buff *skb)
24 struct iphdr *outer_iph = skb->nh.iph;
25 struct iphdr *inner_iph = skb->h.ipiph;
27 if (INET_ECN_is_ce(outer_iph->tos) &&
28 INET_ECN_is_not_ce(inner_iph->tos))
29 IP_ECN_set_ce(inner_iph);
32 static int xfrm4_parse_spi(struct sk_buff *skb, u8 nexthdr, u32 *spi, u32 *seq)
36 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
38 *spi = skb->nh.iph->saddr;
43 return xfrm_parse_spi(skb, nexthdr, spi, seq);
46 int xfrm4_rcv_encap(struct sk_buff *skb, __u16 encap_type)
50 struct sec_decap_state xfrm_vec[XFRM_MAX_DEPTH];
55 if ((err = xfrm4_parse_spi(skb, skb->nh.iph->protocol, &spi, &seq)) != 0)
59 struct iphdr *iph = skb->nh.iph;
61 if (xfrm_nr == XFRM_MAX_DEPTH)
64 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, spi, iph->protocol, AF_INET);
69 if (unlikely(x->km.state != XFRM_STATE_VALID))
72 if (x->props.replay_window && xfrm_replay_check(x, seq))
75 if (xfrm_state_check_expire(x))
78 xfrm_vec[xfrm_nr].decap.decap_type = encap_type;
79 if (x->type->input(x, &(xfrm_vec[xfrm_nr].decap), skb))
82 /* only the first xfrm gets the encap type */
85 if (x->props.replay_window)
86 xfrm_replay_advance(x, seq);
88 x->curlft.bytes += skb->len;
91 spin_unlock(&x->lock);
93 xfrm_vec[xfrm_nr++].xvec = x;
98 if (iph->protocol != IPPROTO_IPIP)
100 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
102 if (skb_cloned(skb) &&
103 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
105 if (!(x->props.flags & XFRM_STATE_NOECN))
106 ipip_ecn_decapsulate(skb);
107 skb->mac.raw = memmove(skb->data - skb->mac_len,
108 skb->mac.raw, skb->mac_len);
109 skb->nh.raw = skb->data;
110 memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
115 if ((err = xfrm_parse_spi(skb, skb->nh.iph->protocol, &spi, &seq)) < 0)
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->x+skb->sp->len, xfrm_vec, xfrm_nr*sizeof(struct sec_decap_state));
134 skb->sp->len += xfrm_nr;
137 if (!(skb->dev->flags&IFF_LOOPBACK)) {
138 dst_release(skb->dst);
144 return -skb->nh.iph->protocol;
148 spin_unlock(&x->lock);
151 while (--xfrm_nr >= 0)
152 xfrm_state_put(xfrm_vec[xfrm_nr].xvec);