- Update to 2.6.25-rc3.
[linux-flexiantxendom0-3.2.10.git] / net / ipv6 / xfrm6_input.c
1 /*
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>
14 #include <linux/netfilter.h>
15 #include <linux/netfilter_ipv6.h>
16 #include <net/ipv6.h>
17 #include <net/xfrm.h>
18
19 int xfrm6_extract_input(struct xfrm_state *x, struct sk_buff *skb)
20 {
21         return xfrm6_extract_header(skb);
22 }
23
24 int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi)
25 {
26         XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
27         XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
28         return xfrm_input(skb, nexthdr, spi, 0);
29 }
30 EXPORT_SYMBOL(xfrm6_rcv_spi);
31
32 int xfrm6_transport_finish(struct sk_buff *skb, int async)
33 {
34         skb_network_header(skb)[IP6CB(skb)->nhoff] =
35                 XFRM_MODE_SKB_CB(skb)->protocol;
36
37 #ifndef CONFIG_NETFILTER
38         if (!async)
39                 return 1;
40 #endif
41
42         ipv6_hdr(skb)->payload_len = htons(skb->len);
43         __skb_push(skb, skb->data - skb_network_header(skb));
44
45         NF_HOOK(PF_INET6, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
46                 ip6_rcv_finish);
47         return -1;
48 }
49
50 int xfrm6_rcv(struct sk_buff *skb)
51 {
52         return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff],
53                              0);
54 }
55
56 EXPORT_SYMBOL(xfrm6_rcv);
57
58 int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
59                      xfrm_address_t *saddr, u8 proto)
60 {
61         struct xfrm_state *x = NULL;
62         int wildcard = 0;
63         xfrm_address_t *xany;
64         int nh = 0;
65         int i = 0;
66
67         /* Allocate new secpath or COW existing one. */
68         if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
69                 struct sec_path *sp;
70
71                 sp = secpath_dup(skb->sp);
72                 if (!sp) {
73                         XFRM_INC_STATS(LINUX_MIB_XFRMINERROR);
74                         goto drop;
75                 }
76                 if (skb->sp)
77                         secpath_put(skb->sp);
78                 skb->sp = sp;
79         }
80
81         if (1 + skb->sp->len == XFRM_MAX_DEPTH) {
82                 XFRM_INC_STATS(LINUX_MIB_XFRMINBUFFERERROR);
83                 goto drop;
84         }
85
86         xany = (xfrm_address_t *)&in6addr_any;
87
88         for (i = 0; i < 3; i++) {
89                 xfrm_address_t *dst, *src;
90                 switch (i) {
91                 case 0:
92                         dst = daddr;
93                         src = saddr;
94                         break;
95                 case 1:
96                         /* lookup state with wild-card source address */
97                         wildcard = 1;
98                         dst = daddr;
99                         src = xany;
100                         break;
101                 case 2:
102                 default:
103                         /* lookup state with wild-card addresses */
104                         wildcard = 1; /* XXX */
105                         dst = xany;
106                         src = xany;
107                         break;
108                 }
109
110                 x = xfrm_state_lookup_byaddr(dst, src, proto, AF_INET6);
111                 if (!x)
112                         continue;
113
114                 spin_lock(&x->lock);
115
116                 if (wildcard) {
117                         if ((x->props.flags & XFRM_STATE_WILDRECV) == 0) {
118                                 spin_unlock(&x->lock);
119                                 xfrm_state_put(x);
120                                 x = NULL;
121                                 continue;
122                         }
123                 }
124
125                 if (unlikely(x->km.state != XFRM_STATE_VALID)) {
126                         spin_unlock(&x->lock);
127                         xfrm_state_put(x);
128                         x = NULL;
129                         continue;
130                 }
131                 if (xfrm_state_check_expire(x)) {
132                         spin_unlock(&x->lock);
133                         xfrm_state_put(x);
134                         x = NULL;
135                         continue;
136                 }
137
138                 spin_unlock(&x->lock);
139
140                 nh = x->type->input(x, skb);
141                 if (nh <= 0) {
142                         xfrm_state_put(x);
143                         x = NULL;
144                         continue;
145                 }
146
147                 /* Found a state */
148                 break;
149         }
150
151         if (!x) {
152                 XFRM_INC_STATS(LINUX_MIB_XFRMINNOSTATES);
153                 xfrm_audit_state_notfound_simple(skb, AF_INET6);
154                 goto drop;
155         }
156
157         skb->sp->xvec[skb->sp->len++] = x;
158
159         spin_lock(&x->lock);
160
161         x->curlft.bytes += skb->len;
162         x->curlft.packets++;
163
164         spin_unlock(&x->lock);
165
166         return 1;
167
168 drop:
169         return -1;
170 }
171
172 EXPORT_SYMBOL(xfrm6_input_addr);