2 * xfrm6_input.c: based on net/ipv4/xfrm4_input.c
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * YOSHIFUJI Hideaki @USAGI
12 #include <linux/module.h>
13 #include <linux/string.h>
14 #include <linux/netfilter.h>
15 #include <linux/netfilter_ipv6.h>
16 #include <net/dsfield.h>
17 #include <net/inet_ecn.h>
22 static inline void ipip6_ecn_decapsulate(struct sk_buff *skb)
24 struct ipv6hdr *outer_iph = skb->nh.ipv6h;
25 struct ipv6hdr *inner_iph = skb->h.ipv6h;
27 if (INET_ECN_is_ce(ipv6_get_dsfield(outer_iph)))
28 IP6_ECN_set_ce(inner_iph);
31 int xfrm6_rcv_spi(struct sk_buff **pskb, u32 spi)
33 struct sk_buff *skb = *pskb;
36 struct sec_decap_state xfrm_vec[XFRM_MAX_DEPTH];
43 nhoff = IP6CB(skb)->nhoff;
44 nexthdr = skb->nh.raw[nhoff];
47 if (!spi && (err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0)
51 struct ipv6hdr *iph = skb->nh.ipv6h;
53 if (xfrm_nr == XFRM_MAX_DEPTH)
56 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, spi, nexthdr, AF_INET6);
60 if (unlikely(x->km.state != XFRM_STATE_VALID))
63 if (x->props.replay_window && xfrm_replay_check(x, seq))
66 if (xfrm_state_check_expire(x))
69 nexthdr = x->type->input(x, &(xfrm_vec[xfrm_nr].decap), skb);
73 skb->nh.raw[nhoff] = nexthdr;
75 if (x->props.replay_window)
76 xfrm_replay_advance(x, seq);
78 x->curlft.bytes += skb->len;
81 spin_unlock(&x->lock);
83 xfrm_vec[xfrm_nr++].xvec = x;
85 if (x->props.mode) { /* XXX */
86 if (nexthdr != IPPROTO_IPV6)
88 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
90 if (skb_cloned(skb) &&
91 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
93 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
94 ipv6_copy_dscp(skb->nh.ipv6h, skb->h.ipv6h);
95 if (!(x->props.flags & XFRM_STATE_NOECN))
96 ipip6_ecn_decapsulate(skb);
97 skb->mac.raw = memmove(skb->data - skb->mac_len,
98 skb->mac.raw, skb->mac_len);
99 skb->nh.raw = skb->data;
104 if ((err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) < 0)
108 /* Allocate new secpath or COW existing one. */
109 if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
111 sp = secpath_dup(skb->sp);
115 secpath_put(skb->sp);
119 if (xfrm_nr + skb->sp->len > XFRM_MAX_DEPTH)
122 memcpy(skb->sp->x+skb->sp->len, xfrm_vec, xfrm_nr*sizeof(struct sec_decap_state));
123 skb->sp->len += xfrm_nr;
124 skb->ip_summed = CHECKSUM_NONE;
129 if (!(skb->dev->flags&IFF_LOOPBACK)) {
130 dst_release(skb->dst);
136 #ifdef CONFIG_NETFILTER
137 skb->nh.ipv6h->payload_len = htons(skb->len);
138 __skb_push(skb, skb->data - skb->nh.raw);
140 NF_HOOK(PF_INET6, NF_IP6_PRE_ROUTING, skb, skb->dev, NULL,
149 spin_unlock(&x->lock);
152 while (--xfrm_nr >= 0)
153 xfrm_state_put(xfrm_vec[xfrm_nr].xvec);
158 EXPORT_SYMBOL(xfrm6_rcv_spi);
160 int xfrm6_rcv(struct sk_buff **pskb)
162 return xfrm6_rcv_spi(pskb, 0);