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