Skip to content

Commit 0ebea8e

Browse files
herbertxdavem330
authored andcommitted
[IPSEC]: Move state lock into x->type->input
This patch releases the lock on the state before calling x->type->input. It also adds the lock to the spots where they're currently needed. Most of those places (all except mip6) are expected to disappear with async crypto. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 668dc8a commit 0ebea8e

File tree

6 files changed

+69
-33
lines changed

6 files changed

+69
-33
lines changed

net/ipv4/ah4.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,25 @@ static int ah_input(struct xfrm_state *x, struct sk_buff *skb)
169169
if (ip_clear_mutable_options(iph, &dummy))
170170
goto out;
171171
}
172+
173+
spin_lock(&x->lock);
172174
{
173175
u8 auth_data[MAX_AH_AUTH_LEN];
174176

175177
memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
176178
skb_push(skb, ihl);
177179
err = ah_mac_digest(ahp, skb, ah->auth_data);
178180
if (err)
179-
goto out;
180-
if (memcmp(ahp->work_icv, auth_data, ahp->icv_trunc_len)) {
181+
goto unlock;
182+
if (memcmp(ahp->work_icv, auth_data, ahp->icv_trunc_len))
181183
err = -EBADMSG;
182-
goto out;
183-
}
184184
}
185+
unlock:
186+
spin_unlock(&x->lock);
187+
188+
if (err)
189+
goto out;
190+
185191
skb->network_header += ah_hlen;
186192
memcpy(skb_network_header(skb), work_buf, ihl);
187193
skb->transport_header = skb->network_header;

net/ipv4/esp4.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,29 +171,31 @@ static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
171171
if (elen <= 0 || (elen & (blksize-1)))
172172
goto out;
173173

174+
if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
175+
goto out;
176+
nfrags = err;
177+
178+
skb->ip_summed = CHECKSUM_NONE;
179+
180+
spin_lock(&x->lock);
181+
174182
/* If integrity check is required, do this. */
175183
if (esp->auth.icv_full_len) {
176184
u8 sum[alen];
177185

178186
err = esp_mac_digest(esp, skb, 0, skb->len - alen);
179187
if (err)
180-
goto out;
188+
goto unlock;
181189

182190
if (skb_copy_bits(skb, skb->len - alen, sum, alen))
183191
BUG();
184192

185193
if (unlikely(memcmp(esp->auth.work_icv, sum, alen))) {
186194
err = -EBADMSG;
187-
goto out;
195+
goto unlock;
188196
}
189197
}
190198

191-
if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
192-
goto out;
193-
nfrags = err;
194-
195-
skb->ip_summed = CHECKSUM_NONE;
196-
197199
esph = (struct ip_esp_hdr *)skb->data;
198200

199201
/* Get ivec. This can be wrong, check against another impls. */
@@ -206,7 +208,7 @@ static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
206208
err = -ENOMEM;
207209
sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
208210
if (!sg)
209-
goto out;
211+
goto unlock;
210212
}
211213
sg_init_table(sg, nfrags);
212214
skb_to_sgvec(skb, sg,
@@ -215,6 +217,10 @@ static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
215217
err = crypto_blkcipher_decrypt(&desc, sg, sg, elen);
216218
if (unlikely(sg != &esp->sgbuf[0]))
217219
kfree(sg);
220+
221+
unlock:
222+
spin_unlock(&x->lock);
223+
218224
if (unlikely(err))
219225
goto out;
220226

net/ipv6/ah6.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ static int ah6_input(struct xfrm_state *x, struct sk_buff *skb)
370370
ip6h->flow_lbl[2] = 0;
371371
ip6h->hop_limit = 0;
372372

373+
spin_lock(&x->lock);
373374
{
374375
u8 auth_data[MAX_AH_AUTH_LEN];
375376

@@ -378,13 +379,17 @@ static int ah6_input(struct xfrm_state *x, struct sk_buff *skb)
378379
skb_push(skb, hdr_len);
379380
err = ah_mac_digest(ahp, skb, ah->auth_data);
380381
if (err)
381-
goto free_out;
382+
goto unlock;
382383
if (memcmp(ahp->work_icv, auth_data, ahp->icv_trunc_len)) {
383384
LIMIT_NETDEBUG(KERN_WARNING "ipsec ah authentication error\n");
384385
err = -EBADMSG;
385-
goto free_out;
386386
}
387387
}
388+
unlock:
389+
spin_unlock(&x->lock);
390+
391+
if (err)
392+
goto free_out;
388393

389394
skb->network_header += ah_hlen;
390395
memcpy(skb_network_header(skb), tmp_hdr, hdr_len);

net/ipv6/esp6.c

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -165,30 +165,32 @@ static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
165165
goto out;
166166
}
167167

168+
if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0) {
169+
ret = -EINVAL;
170+
goto out;
171+
}
172+
173+
skb->ip_summed = CHECKSUM_NONE;
174+
175+
spin_lock(&x->lock);
176+
168177
/* If integrity check is required, do this. */
169178
if (esp->auth.icv_full_len) {
170179
u8 sum[alen];
171180

172181
ret = esp_mac_digest(esp, skb, 0, skb->len - alen);
173182
if (ret)
174-
goto out;
183+
goto unlock;
175184

176185
if (skb_copy_bits(skb, skb->len - alen, sum, alen))
177186
BUG();
178187

179188
if (unlikely(memcmp(esp->auth.work_icv, sum, alen))) {
180189
ret = -EBADMSG;
181-
goto out;
190+
goto unlock;
182191
}
183192
}
184193

185-
if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0) {
186-
ret = -EINVAL;
187-
goto out;
188-
}
189-
190-
skb->ip_summed = CHECKSUM_NONE;
191-
192194
esph = (struct ip_esp_hdr *)skb->data;
193195
iph = ipv6_hdr(skb);
194196

@@ -197,15 +199,13 @@ static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
197199
crypto_blkcipher_set_iv(tfm, esph->enc_data, esp->conf.ivlen);
198200

199201
{
200-
u8 nexthdr[2];
201202
struct scatterlist *sg = &esp->sgbuf[0];
202-
u8 padlen;
203203

204204
if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
205205
sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
206206
if (!sg) {
207207
ret = -ENOMEM;
208-
goto out;
208+
goto unlock;
209209
}
210210
}
211211
sg_init_table(sg, nfrags);
@@ -215,8 +215,17 @@ static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
215215
ret = crypto_blkcipher_decrypt(&desc, sg, sg, elen);
216216
if (unlikely(sg != &esp->sgbuf[0]))
217217
kfree(sg);
218-
if (unlikely(ret))
219-
goto out;
218+
}
219+
220+
unlock:
221+
spin_unlock(&x->lock);
222+
223+
if (unlikely(ret))
224+
goto out;
225+
226+
{
227+
u8 nexthdr[2];
228+
u8 padlen;
220229

221230
if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
222231
BUG();

net/ipv6/mip6.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,15 @@ static int mip6_destopt_input(struct xfrm_state *x, struct sk_buff *skb)
128128
{
129129
struct ipv6hdr *iph = ipv6_hdr(skb);
130130
struct ipv6_destopt_hdr *destopt = (struct ipv6_destopt_hdr *)skb->data;
131+
int err = destopt->nexthdr;
131132

133+
spin_lock(&x->lock);
132134
if (!ipv6_addr_equal(&iph->saddr, (struct in6_addr *)x->coaddr) &&
133135
!ipv6_addr_any((struct in6_addr *)x->coaddr))
134-
return -ENOENT;
136+
err = -ENOENT;
137+
spin_unlock(&x->lock);
135138

136-
return destopt->nexthdr;
139+
return err;
137140
}
138141

139142
/* Destination Option Header is inserted.
@@ -344,12 +347,15 @@ static struct xfrm_type mip6_destopt_type =
344347
static int mip6_rthdr_input(struct xfrm_state *x, struct sk_buff *skb)
345348
{
346349
struct rt2_hdr *rt2 = (struct rt2_hdr *)skb->data;
350+
int err = rt2->rt_hdr.nexthdr;
347351

352+
spin_lock(&x->lock);
348353
if (!ipv6_addr_equal(&rt2->addr, (struct in6_addr *)x->coaddr) &&
349354
!ipv6_addr_any((struct in6_addr *)x->coaddr))
350-
return -ENOENT;
355+
err = -ENOENT;
356+
spin_unlock(&x->lock);
351357

352-
return rt2->rt_hdr.nexthdr;
358+
return err;
353359
}
354360

355361
/* Routing Header type 2 is inserted.

net/xfrm/xfrm_input.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
146146
if (xfrm_state_check_expire(x))
147147
goto drop_unlock;
148148

149+
spin_unlock(&x->lock);
150+
149151
nexthdr = x->type->input(x, skb);
152+
153+
spin_lock(&x->lock);
150154
if (nexthdr <= 0) {
151155
if (nexthdr == -EBADMSG)
152156
x->stats.integrity_failed++;

0 commit comments

Comments
 (0)