[IPSEC]: Merge most of the input path
[deliverable/linux.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)->nhoff = IP6CB(skb)->nhoff;
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 #ifdef CONFIG_NETFILTER
35 ipv6_hdr(skb)->payload_len = htons(skb->len);
36 __skb_push(skb, skb->data - skb_network_header(skb));
37
38 NF_HOOK(PF_INET6, NF_IP6_PRE_ROUTING, skb, skb->dev, NULL,
39 ip6_rcv_finish);
40 return -1;
41 #else
42 return 1;
43 #endif
44 }
45
46 int xfrm6_rcv(struct sk_buff *skb)
47 {
48 return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff],
49 0);
50 }
51
52 EXPORT_SYMBOL(xfrm6_rcv);
53
54 int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
55 xfrm_address_t *saddr, u8 proto)
56 {
57 struct xfrm_state *x = NULL;
58 int wildcard = 0;
59 xfrm_address_t *xany;
60 struct xfrm_state *xfrm_vec_one = NULL;
61 int nh = 0;
62 int i = 0;
63
64 xany = (xfrm_address_t *)&in6addr_any;
65
66 for (i = 0; i < 3; i++) {
67 xfrm_address_t *dst, *src;
68 switch (i) {
69 case 0:
70 dst = daddr;
71 src = saddr;
72 break;
73 case 1:
74 /* lookup state with wild-card source address */
75 wildcard = 1;
76 dst = daddr;
77 src = xany;
78 break;
79 case 2:
80 default:
81 /* lookup state with wild-card addresses */
82 wildcard = 1; /* XXX */
83 dst = xany;
84 src = xany;
85 break;
86 }
87
88 x = xfrm_state_lookup_byaddr(dst, src, proto, AF_INET6);
89 if (!x)
90 continue;
91
92 spin_lock(&x->lock);
93
94 if (wildcard) {
95 if ((x->props.flags & XFRM_STATE_WILDRECV) == 0) {
96 spin_unlock(&x->lock);
97 xfrm_state_put(x);
98 x = NULL;
99 continue;
100 }
101 }
102
103 if (unlikely(x->km.state != XFRM_STATE_VALID)) {
104 spin_unlock(&x->lock);
105 xfrm_state_put(x);
106 x = NULL;
107 continue;
108 }
109 if (xfrm_state_check_expire(x)) {
110 spin_unlock(&x->lock);
111 xfrm_state_put(x);
112 x = NULL;
113 continue;
114 }
115
116 nh = x->type->input(x, skb);
117 if (nh <= 0) {
118 spin_unlock(&x->lock);
119 xfrm_state_put(x);
120 x = NULL;
121 continue;
122 }
123
124 x->curlft.bytes += skb->len;
125 x->curlft.packets++;
126
127 spin_unlock(&x->lock);
128
129 xfrm_vec_one = x;
130 break;
131 }
132
133 if (!xfrm_vec_one)
134 goto drop;
135
136 /* Allocate new secpath or COW existing one. */
137 if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
138 struct sec_path *sp;
139 sp = secpath_dup(skb->sp);
140 if (!sp)
141 goto drop;
142 if (skb->sp)
143 secpath_put(skb->sp);
144 skb->sp = sp;
145 }
146
147 if (1 + skb->sp->len > XFRM_MAX_DEPTH)
148 goto drop;
149
150 skb->sp->xvec[skb->sp->len] = xfrm_vec_one;
151 skb->sp->len ++;
152
153 return 1;
154 drop:
155 if (xfrm_vec_one)
156 xfrm_state_put(xfrm_vec_one);
157 return -1;
158 }
159
160 EXPORT_SYMBOL(xfrm6_input_addr);
This page took 0.048101 seconds and 5 git commands to generate.