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>
19 int xfrm6_rcv_spi(struct sk_buff
*skb
, u32 spi
)
23 struct xfrm_state
*xfrm_vec
[XFRM_MAX_DEPTH
];
30 nhoff
= IP6CB(skb
)->nhoff
;
31 nexthdr
= skb
->nh
.raw
[nhoff
];
34 if (!spi
&& (err
= xfrm_parse_spi(skb
, nexthdr
, &spi
, &seq
)) != 0)
38 struct ipv6hdr
*iph
= skb
->nh
.ipv6h
;
40 if (xfrm_nr
== XFRM_MAX_DEPTH
)
43 x
= xfrm_state_lookup((xfrm_address_t
*)&iph
->daddr
, spi
, nexthdr
, AF_INET6
);
47 if (unlikely(x
->km
.state
!= XFRM_STATE_VALID
))
50 if (x
->props
.replay_window
&& xfrm_replay_check(x
, seq
))
53 if (xfrm_state_check_expire(x
))
56 nexthdr
= x
->type
->input(x
, skb
);
60 skb
->nh
.raw
[nhoff
] = nexthdr
;
62 if (x
->props
.replay_window
)
63 xfrm_replay_advance(x
, seq
);
65 x
->curlft
.bytes
+= skb
->len
;
68 spin_unlock(&x
->lock
);
70 xfrm_vec
[xfrm_nr
++] = x
;
72 if (x
->mode
->input(x
, skb
))
75 if (x
->props
.mode
== XFRM_MODE_TUNNEL
) { /* XXX */
80 if ((err
= xfrm_parse_spi(skb
, nexthdr
, &spi
, &seq
)) < 0)
84 /* Allocate new secpath or COW existing one. */
85 if (!skb
->sp
|| atomic_read(&skb
->sp
->refcnt
) != 1) {
87 sp
= secpath_dup(skb
->sp
);
95 if (xfrm_nr
+ skb
->sp
->len
> XFRM_MAX_DEPTH
)
98 memcpy(skb
->sp
->xvec
+ skb
->sp
->len
, xfrm_vec
,
99 xfrm_nr
* sizeof(xfrm_vec
[0]));
100 skb
->sp
->len
+= xfrm_nr
;
101 skb
->ip_summed
= CHECKSUM_NONE
;
106 if (!(skb
->dev
->flags
&IFF_LOOPBACK
)) {
107 dst_release(skb
->dst
);
113 #ifdef CONFIG_NETFILTER
114 skb
->nh
.ipv6h
->payload_len
= htons(skb
->len
);
115 __skb_push(skb
, skb
->data
- skb
->nh
.raw
);
117 NF_HOOK(PF_INET6
, NF_IP6_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
126 spin_unlock(&x
->lock
);
129 while (--xfrm_nr
>= 0)
130 xfrm_state_put(xfrm_vec
[xfrm_nr
]);
135 EXPORT_SYMBOL(xfrm6_rcv_spi
);
137 int xfrm6_rcv(struct sk_buff
**pskb
)
139 return xfrm6_rcv_spi(*pskb
, 0);
142 int xfrm6_input_addr(struct sk_buff
*skb
, xfrm_address_t
*daddr
,
143 xfrm_address_t
*saddr
, u8 proto
)
145 struct xfrm_state
*x
= NULL
;
148 xfrm_address_t
*xany
;
149 struct xfrm_state
*xfrm_vec_one
= NULL
;
153 ipv6_addr_set(&any
, 0, 0, 0, 0);
154 xany
= (xfrm_address_t
*)&any
;
156 for (i
= 0; i
< 3; i
++) {
157 xfrm_address_t
*dst
, *src
;
164 /* lookup state with wild-card source address */
171 /* lookup state with wild-card addresses */
172 wildcard
= 1; /* XXX */
178 x
= xfrm_state_lookup_byaddr(dst
, src
, proto
, AF_INET6
);
185 if ((x
->props
.flags
& XFRM_STATE_WILDRECV
) == 0) {
186 spin_unlock(&x
->lock
);
193 if (unlikely(x
->km
.state
!= XFRM_STATE_VALID
)) {
194 spin_unlock(&x
->lock
);
199 if (xfrm_state_check_expire(x
)) {
200 spin_unlock(&x
->lock
);
206 nh
= x
->type
->input(x
, skb
);
208 spin_unlock(&x
->lock
);
214 x
->curlft
.bytes
+= skb
->len
;
217 spin_unlock(&x
->lock
);
226 /* Allocate new secpath or COW existing one. */
227 if (!skb
->sp
|| atomic_read(&skb
->sp
->refcnt
) != 1) {
229 sp
= secpath_dup(skb
->sp
);
233 secpath_put(skb
->sp
);
237 if (1 + skb
->sp
->len
> XFRM_MAX_DEPTH
)
240 skb
->sp
->xvec
[skb
->sp
->len
] = xfrm_vec_one
;
246 xfrm_state_put(xfrm_vec_one
);