Sat Aug 25 14:35:22 2018 UTC ()
Pull up following revision(s) (requested by ozaki-r in ticket #986):

	sys/netipsec/key.c: revision 1.257

Don't call key_ismyaddr, which may sleep, in a pserialize read section

Use mutex here instead of pserialize because using mutex is simpler than
using psz+ref, which is another solution, and key_checkspidup isn't called in
any performance-sensitive paths.


(martin)
diff -r1.163.2.9 -r1.163.2.10 src/sys/netipsec/key.c

cvs diff -r1.163.2.9 -r1.163.2.10 src/sys/netipsec/key.c (expand / switch to unified diff)

--- src/sys/netipsec/key.c 2018/04/18 14:06:24 1.163.2.9
+++ src/sys/netipsec/key.c 2018/08/25 14:35:21 1.163.2.10
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: key.c,v 1.163.2.9 2018/04/18 14:06:24 martin Exp $ */ 1/* $NetBSD: key.c,v 1.163.2.10 2018/08/25 14:35:21 martin Exp $ */
2/* $FreeBSD: src/sys/netipsec/key.c,v 1.3.2.3 2004/02/14 22:23:23 bms Exp $ */ 2/* $FreeBSD: src/sys/netipsec/key.c,v 1.3.2.3 2004/02/14 22:23:23 bms Exp $ */
3/* $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $ */ 3/* $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $ */
4 4
5/* 5/*
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved. 7 * All rights reserved.
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
11 * are met: 11 * are met:
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
@@ -22,27 +22,27 @@ @@ -22,27 +22,27 @@
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE. 31 * SUCH DAMAGE.
32 */ 32 */
33 33
34#include <sys/cdefs.h> 34#include <sys/cdefs.h>
35__KERNEL_RCSID(0, "$NetBSD: key.c,v 1.163.2.9 2018/04/18 14:06:24 martin Exp $"); 35__KERNEL_RCSID(0, "$NetBSD: key.c,v 1.163.2.10 2018/08/25 14:35:21 martin Exp $");
36 36
37/* 37/*
38 * This code is referred to RFC 2367 38 * This code is referred to RFC 2367
39 */ 39 */
40 40
41#if defined(_KERNEL_OPT) 41#if defined(_KERNEL_OPT)
42#include "opt_inet.h" 42#include "opt_inet.h"
43#include "opt_ipsec.h" 43#include "opt_ipsec.h"
44#include "opt_gateway.h" 44#include "opt_gateway.h"
45#include "opt_net_mpsafe.h" 45#include "opt_net_mpsafe.h"
46#endif 46#endif
47 47
48#include <sys/types.h> 48#include <sys/types.h>
@@ -3448,47 +3448,47 @@ key_getsah_ref(const struct secasindex * @@ -3448,47 +3448,47 @@ key_getsah_ref(const struct secasindex *
3448 3448
3449/* 3449/*
3450 * check not to be duplicated SPI. 3450 * check not to be duplicated SPI.
3451 * NOTE: this function is too slow due to searching all SAD. 3451 * NOTE: this function is too slow due to searching all SAD.
3452 * OUT: 3452 * OUT:
3453 * NULL : not found 3453 * NULL : not found
3454 * others : found, pointer to a SA. 3454 * others : found, pointer to a SA.
3455 */ 3455 */
3456static bool 3456static bool
3457key_checkspidup(const struct secasindex *saidx, u_int32_t spi) 3457key_checkspidup(const struct secasindex *saidx, u_int32_t spi)
3458{ 3458{
3459 struct secashead *sah; 3459 struct secashead *sah;
3460 struct secasvar *sav; 3460 struct secasvar *sav;
3461 int s; 
3462 3461
3463 /* check address family */ 3462 /* check address family */
3464 if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) { 3463 if (saidx->src.sa.sa_family != saidx->dst.sa.sa_family) {
3465 IPSECLOG(LOG_DEBUG, "address family mismatched.\n"); 3464 IPSECLOG(LOG_DEBUG, "address family mismatched.\n");
3466 return false; 3465 return false;
3467 } 3466 }
3468 3467
3469 /* check all SAD */ 3468 /* check all SAD */
3470 s = pserialize_read_enter(); 3469 /* key_ismyaddr may sleep, so use mutex, not pserialize, here. */
3471 SAHLIST_READER_FOREACH(sah) { 3470 mutex_enter(&key_sad.lock);
 3471 SAHLIST_WRITER_FOREACH(sah) {
3472 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst)) 3472 if (!key_ismyaddr((struct sockaddr *)&sah->saidx.dst))
3473 continue; 3473 continue;
3474 sav = key_getsavbyspi(sah, spi); 3474 sav = key_getsavbyspi(sah, spi);
3475 if (sav != NULL) { 3475 if (sav != NULL) {
3476 pserialize_read_exit(s); 
3477 KEY_SA_UNREF(&sav); 3476 KEY_SA_UNREF(&sav);
 3477 mutex_exit(&key_sad.lock);
3478 return true; 3478 return true;
3479 } 3479 }
3480 } 3480 }
3481 pserialize_read_exit(s); 3481 mutex_exit(&key_sad.lock);
3482 3482
3483 return false; 3483 return false;
3484} 3484}
3485 3485
3486/* 3486/*
3487 * search SAD litmited alive SA, protocol, SPI. 3487 * search SAD litmited alive SA, protocol, SPI.
3488 * OUT: 3488 * OUT:
3489 * NULL : not found 3489 * NULL : not found
3490 * others : found, pointer to a SA. 3490 * others : found, pointer to a SA.
3491 */ 3491 */
3492static struct secasvar * 3492static struct secasvar *
3493key_getsavbyspi(struct secashead *sah, u_int32_t spi) 3493key_getsavbyspi(struct secashead *sah, u_int32_t spi)
3494{ 3494{