[NETFILTER]: Introduce NF_INET_ hook values
[safe/jmp/linux-2.6] / 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)->daddroff = offsetof(struct ipv6hdr, daddr);
27         return xfrm_input(skb, nexthdr, spi, 0);
28 }
29 EXPORT_SYMBOL(xfrm6_rcv_spi);
30
31 int xfrm6_transport_finish(struct sk_buff *skb, int async)
32 {
33         skb_network_header(skb)[IP6CB(skb)->nhoff] =
34                 XFRM_MODE_SKB_CB(skb)->protocol;
35
36 #ifdef CONFIG_NETFILTER
37         ipv6_hdr(skb)->payload_len = htons(skb->len);
38         __skb_push(skb, skb->data - skb_network_header(skb));
39
40         NF_HOOK(PF_INET6, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
41                 ip6_rcv_finish);
42         return -1;
43 #else
44         if (async)
45                 return ip6_rcv_finish(skb);
46
47         return 1;
48 #endif
49 }
50
51 int xfrm6_rcv(struct sk_buff *skb)
52 {
53         return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff],
54                              0);
55 }
56
57 EXPORT_SYMBOL(xfrm6_rcv);
58
59 int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
60                      xfrm_address_t *saddr, u8 proto)
61 {
62         struct xfrm_state *x = NULL;
63         int wildcard = 0;
64         xfrm_address_t *xany;
65         struct xfrm_state *xfrm_vec_one = NULL;
66         int nh = 0;
67         int i = 0;
68
69         xany = (xfrm_address_t *)&in6addr_any;
70
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:
86                         /* lookup state with wild-card addresses */
87                         wildcard = 1; /* XXX */
88                         dst = xany;
89                         src = xany;
90                         break;
91                 }
92
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);
111                         x = NULL;
112                         continue;
113                 }
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;
159 drop:
160         if (xfrm_vec_one)
161                 xfrm_state_put(xfrm_vec_one);
162         return -1;
163 }
164
165 EXPORT_SYMBOL(xfrm6_input_addr);