blob: e2c3efd2579d65a9a2cfc75fba5545286a7e733b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * xfrm6_input.c: based on net/ipv4/xfrm4_input.c
3 *
4 * Authors:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * YOSHIFUJI Hideaki @USAGI
9 * IPv6 support
10 */
11
12#include <linux/module.h>
13#include <linux/string.h>
Patrick McHardyb05e1062006-01-06 23:03:34 -080014#include <linux/netfilter.h>
15#include <linux/netfilter_ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <net/ipv6.h>
17#include <net/xfrm.h>
18
Herbert Xu227620e2007-11-13 21:41:28 -080019int xfrm6_extract_input(struct xfrm_state *x, struct sk_buff *skb)
20{
21 return xfrm6_extract_header(skb);
22}
23
Herbert Xu33b5ecb2007-10-17 21:29:25 -070024int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Herbert Xu716062f2007-11-13 21:44:23 -080026 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
27 return xfrm_input(skb, nexthdr, spi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028}
Linus Torvalds1da177e2005-04-16 15:20:36 -070029EXPORT_SYMBOL(xfrm6_rcv_spi);
30
Herbert Xu716062f2007-11-13 21:44:23 -080031int xfrm6_transport_finish(struct sk_buff *skb, int async)
32{
Herbert Xu60d5fcf2007-11-19 18:47:58 -080033 skb_network_header(skb)[IP6CB(skb)->nhoff] =
34 XFRM_MODE_SKB_CB(skb)->protocol;
35
Herbert Xu716062f2007-11-13 21:44:23 -080036#ifdef CONFIG_NETFILTER
37 ipv6_hdr(skb)->payload_len = htons(skb->len);
38 __skb_push(skb, skb->data - skb_network_header(skb));
39
Patrick McHardy6e23ae22007-11-19 18:53:30 -080040 NF_HOOK(PF_INET6, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
Herbert Xu716062f2007-11-13 21:44:23 -080041 ip6_rcv_finish);
42 return -1;
43#else
Herbert Xu1bf06cd2007-11-19 18:50:17 -080044 if (async)
45 return ip6_rcv_finish(skb);
46
Herbert Xu716062f2007-11-13 21:44:23 -080047 return 1;
48#endif
49}
50
Herbert Xue5bbef22007-10-15 12:50:28 -070051int xfrm6_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Herbert Xu33b5ecb2007-10-17 21:29:25 -070053 return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff],
54 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070056
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +090057EXPORT_SYMBOL(xfrm6_rcv);
58
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070059int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
60 xfrm_address_t *saddr, u8 proto)
61{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090062 struct xfrm_state *x = NULL;
63 int wildcard = 0;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070064 xfrm_address_t *xany;
65 struct xfrm_state *xfrm_vec_one = NULL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090066 int nh = 0;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070067 int i = 0;
68
YOSHIFUJI Hideakic53b3592007-04-24 20:44:50 +090069 xany = (xfrm_address_t *)&in6addr_any;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070070
71 for (i = 0; i < 3; i++) {
72 xfrm_address_t *dst, *src;
73 switch (i) {
74 case 0:
75 dst = daddr;
76 src = saddr;
77 break;
78 case 1:
79 /* lookup state with wild-card source address */
80 wildcard = 1;
81 dst = daddr;
82 src = xany;
83 break;
84 case 2:
85 default:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090086 /* lookup state with wild-card addresses */
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070087 wildcard = 1; /* XXX */
88 dst = xany;
89 src = xany;
90 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090091 }
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070092
93 x = xfrm_state_lookup_byaddr(dst, src, proto, AF_INET6);
94 if (!x)
95 continue;
96
97 spin_lock(&x->lock);
98
99 if (wildcard) {
100 if ((x->props.flags & XFRM_STATE_WILDRECV) == 0) {
101 spin_unlock(&x->lock);
102 xfrm_state_put(x);
103 x = NULL;
104 continue;
105 }
106 }
107
108 if (unlikely(x->km.state != XFRM_STATE_VALID)) {
109 spin_unlock(&x->lock);
110 xfrm_state_put(x);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900111 x = NULL;
112 continue;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700113 }
114 if (xfrm_state_check_expire(x)) {
115 spin_unlock(&x->lock);
116 xfrm_state_put(x);
117 x = NULL;
118 continue;
119 }
120
121 nh = x->type->input(x, skb);
122 if (nh <= 0) {
123 spin_unlock(&x->lock);
124 xfrm_state_put(x);
125 x = NULL;
126 continue;
127 }
128
129 x->curlft.bytes += skb->len;
130 x->curlft.packets++;
131
132 spin_unlock(&x->lock);
133
134 xfrm_vec_one = x;
135 break;
136 }
137
138 if (!xfrm_vec_one)
139 goto drop;
140
141 /* Allocate new secpath or COW existing one. */
142 if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
143 struct sec_path *sp;
144 sp = secpath_dup(skb->sp);
145 if (!sp)
146 goto drop;
147 if (skb->sp)
148 secpath_put(skb->sp);
149 skb->sp = sp;
150 }
151
152 if (1 + skb->sp->len > XFRM_MAX_DEPTH)
153 goto drop;
154
155 skb->sp->xvec[skb->sp->len] = xfrm_vec_one;
156 skb->sp->len ++;
157
158 return 1;
159drop:
160 if (xfrm_vec_one)
161 xfrm_state_put(xfrm_vec_one);
162 return -1;
163}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900164
165EXPORT_SYMBOL(xfrm6_input_addr);