[IPSEC]: Fix double free on skb on async output
[powerpc.git] / net / xfrm / xfrm_output.c
1 /*
2  * xfrm_output.c - Common IPsec encapsulation code.
3  *
4  * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11
12 #include <linux/errno.h>
13 #include <linux/module.h>
14 #include <linux/netdevice.h>
15 #include <linux/netfilter.h>
16 #include <linux/skbuff.h>
17 #include <linux/spinlock.h>
18 #include <net/dst.h>
19 #include <net/xfrm.h>
20
21 static int xfrm_output2(struct sk_buff *skb);
22
23 static int xfrm_state_check_space(struct xfrm_state *x, struct sk_buff *skb)
24 {
25         struct dst_entry *dst = skb->dst;
26         int nhead = dst->header_len + LL_RESERVED_SPACE(dst->dev)
27                 - skb_headroom(skb);
28
29         if (nhead > 0)
30                 return pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
31
32         /* Check tail too... */
33         return 0;
34 }
35
36 static int xfrm_output_one(struct sk_buff *skb, int err)
37 {
38         struct dst_entry *dst = skb->dst;
39         struct xfrm_state *x = dst->xfrm;
40
41         if (err <= 0)
42                 goto resume;
43
44         do {
45                 err = xfrm_state_check_space(x, skb);
46                 if (err) {
47                         XFRM_INC_STATS(LINUX_MIB_XFRMOUTERROR);
48                         goto error_nolock;
49                 }
50
51                 err = x->outer_mode->output(x, skb);
52                 if (err) {
53                         XFRM_INC_STATS(LINUX_MIB_XFRMOUTSTATEMODEERROR);
54                         goto error_nolock;
55                 }
56
57                 spin_lock_bh(&x->lock);
58                 err = xfrm_state_check_expire(x);
59                 if (err) {
60                         XFRM_INC_STATS(LINUX_MIB_XFRMOUTSTATEEXPIRED);
61                         goto error;
62                 }
63
64                 if (x->type->flags & XFRM_TYPE_REPLAY_PROT) {
65                         XFRM_SKB_CB(skb)->seq = ++x->replay.oseq;
66                         if (unlikely(x->replay.oseq == 0)) {
67                                 x->replay.oseq--;
68                                 xfrm_audit_state_replay_overflow(x, skb);
69                                 goto error;
70                         }
71                         if (xfrm_aevent_is_on())
72                                 xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
73                 }
74
75                 x->curlft.bytes += skb->len;
76                 x->curlft.packets++;
77
78                 spin_unlock_bh(&x->lock);
79
80                 err = x->type->output(x, skb);
81                 if (err == -EINPROGRESS)
82                         goto out_exit;
83
84 resume:
85                 if (err) {
86                         XFRM_INC_STATS(LINUX_MIB_XFRMOUTSTATEPROTOERROR);
87                         goto error_nolock;
88                 }
89
90                 if (!(skb->dst = dst_pop(dst))) {
91                         XFRM_INC_STATS(LINUX_MIB_XFRMOUTERROR);
92                         err = -EHOSTUNREACH;
93                         goto error_nolock;
94                 }
95                 dst = skb->dst;
96                 x = dst->xfrm;
97         } while (x && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL));
98
99         err = 0;
100
101 out_exit:
102         return err;
103 error:
104         spin_unlock_bh(&x->lock);
105 error_nolock:
106         kfree_skb(skb);
107         goto out_exit;
108 }
109
110 int xfrm_output_resume(struct sk_buff *skb, int err)
111 {
112         while (likely((err = xfrm_output_one(skb, err)) == 0)) {
113                 struct xfrm_state *x;
114
115                 nf_reset(skb);
116
117                 err = skb->dst->ops->local_out(skb);
118                 if (unlikely(err != 1))
119                         goto out;
120
121                 x = skb->dst->xfrm;
122                 if (!x)
123                         return dst_output(skb);
124
125                 err = nf_hook(x->inner_mode->afinfo->family,
126                               NF_INET_POST_ROUTING, skb,
127                               NULL, skb->dst->dev, xfrm_output2);
128                 if (unlikely(err != 1))
129                         goto out;
130         }
131
132         if (err == -EINPROGRESS)
133                 err = 0;
134
135 out:
136         return err;
137 }
138 EXPORT_SYMBOL_GPL(xfrm_output_resume);
139
140 static int xfrm_output2(struct sk_buff *skb)
141 {
142         return xfrm_output_resume(skb, 1);
143 }
144
145 static int xfrm_output_gso(struct sk_buff *skb)
146 {
147         struct sk_buff *segs;
148
149         segs = skb_gso_segment(skb, 0);
150         kfree_skb(skb);
151         if (unlikely(IS_ERR(segs)))
152                 return PTR_ERR(segs);
153
154         do {
155                 struct sk_buff *nskb = segs->next;
156                 int err;
157
158                 segs->next = NULL;
159                 err = xfrm_output2(segs);
160
161                 if (unlikely(err)) {
162                         while ((segs = nskb)) {
163                                 nskb = segs->next;
164                                 segs->next = NULL;
165                                 kfree_skb(segs);
166                         }
167                         return err;
168                 }
169
170                 segs = nskb;
171         } while (segs);
172
173         return 0;
174 }
175
176 int xfrm_output(struct sk_buff *skb)
177 {
178         int err;
179
180         if (skb_is_gso(skb))
181                 return xfrm_output_gso(skb);
182
183         if (skb->ip_summed == CHECKSUM_PARTIAL) {
184                 err = skb_checksum_help(skb);
185                 if (err) {
186                         XFRM_INC_STATS(LINUX_MIB_XFRMOUTERROR);
187                         kfree_skb(skb);
188                         return err;
189                 }
190         }
191
192         return xfrm_output2(skb);
193 }
194 EXPORT_SYMBOL_GPL(xfrm_output);