Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Add test cases of NAT-T (transport mode)



details:   https://anonhg.NetBSD.org/src/rev/d4c2f839a45f
branches:  trunk
changeset: 827494:d4c2f839a45f
user:      ozaki-r <ozaki-r%NetBSD.org@localhost>
date:      Mon Oct 30 15:59:23 2017 +0000

description:
Add test cases of NAT-T (transport mode)

A small C program is added to make a special socket (UDP_ENCAP_ESPINUDP)
and keep it to handle UDP-encapsulated ESP packets.

diffstat:

 distrib/sets/lists/debug/mi       |    3 +-
 distrib/sets/lists/tests/mi       |    4 +-
 tests/net/ipsec/Makefile          |   10 +-
 tests/net/ipsec/natt_terminator.c |  108 ++++++++++
 tests/net/ipsec/t_ipsec_natt.sh   |  376 ++++++++++++++++++++++++++++++++++++++
 tests/net/net_common.sh           |   21 ++-
 6 files changed, 516 insertions(+), 6 deletions(-)

diffs (truncated from 601 to 300 lines):

diff -r 909168a2c41d -r d4c2f839a45f distrib/sets/lists/debug/mi
--- a/distrib/sets/lists/debug/mi       Mon Oct 30 15:55:53 2017 +0000
+++ b/distrib/sets/lists/debug/mi       Mon Oct 30 15:59:23 2017 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.227 2017/10/10 19:31:56 christos Exp $
+# $NetBSD: mi,v 1.228 2017/10/30 15:59:23 ozaki-r Exp $
 ./etc/mtree/set.debug                           comp-sys-root
 ./usr/lib                                      comp-sys-usr            compatdir
 ./usr/lib/i18n/libBIG5_g.a                     comp-c-debuglib         debuglib,compatfile
@@ -2280,6 +2280,7 @@
 ./usr/libdata/debug/usr/tests/net/if/t_compat.debug            tests-net-debug         debug,atf,rump
 ./usr/libdata/debug/usr/tests/net/if_loop/t_pr.debug           tests-net-debug         debug,atf,rump
 ./usr/libdata/debug/usr/tests/net/in_cksum/in_cksum.debug      tests-net-debug         debug,atf,compattestfile
+./usr/libdata/debug/usr/tests/net/ipsec/natt_terminator.debug  tests-net-debug         debug,atf,rump
 ./usr/libdata/debug/usr/tests/net/mcast/mcast.debug            tests-net-debug         debug,atf,rump
 ./usr/libdata/debug/usr/tests/net/mcast/t_mcast.debug          tests-obsolete          debug,atf,rump,obsolete
 ./usr/libdata/debug/usr/tests/net/net/t_pktinfo.debug          tests-net-debug         debug,atf,compattestfile
diff -r 909168a2c41d -r d4c2f839a45f distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Mon Oct 30 15:55:53 2017 +0000
+++ b/distrib/sets/lists/tests/mi       Mon Oct 30 15:59:23 2017 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.764 2017/10/02 04:16:32 pgoyette Exp $
+# $NetBSD: mi,v 1.765 2017/10/30 15:59:23 ozaki-r Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -3319,11 +3319,13 @@
 ./usr/tests/net/ipsec                          tests-net-tests         compattestfile,atf
 ./usr/tests/net/ipsec/Atffile                  tests-net-tests         atf,rump
 ./usr/tests/net/ipsec/Kyuafile                 tests-net-tests         atf,rump,kyua
+./usr/tests/net/ipsec/natt_terminator          tests-net-tests         atf,rump
 ./usr/tests/net/ipsec/t_ipsec_ah_keys          tests-net-tests         atf,rump
 ./usr/tests/net/ipsec/t_ipsec_esp_keys         tests-net-tests         atf,rump
 ./usr/tests/net/ipsec/t_ipsec_gif              tests-net-tests         atf,rump
 ./usr/tests/net/ipsec/t_ipsec_l2tp             tests-net-tests         atf,rump
 ./usr/tests/net/ipsec/t_ipsec_misc             tests-net-tests         atf,rump
+./usr/tests/net/ipsec/t_ipsec_natt             tests-net-tests         atf,rump
 ./usr/tests/net/ipsec/t_ipsec_sockopt          tests-net-tests         atf,rump
 ./usr/tests/net/ipsec/t_ipsec_sysctl           tests-net-tests         atf,rump
 ./usr/tests/net/ipsec/t_ipsec_tcp              tests-net-tests         atf,rump
diff -r 909168a2c41d -r d4c2f839a45f tests/net/ipsec/Makefile
--- a/tests/net/ipsec/Makefile  Mon Oct 30 15:55:53 2017 +0000
+++ b/tests/net/ipsec/Makefile  Mon Oct 30 15:59:23 2017 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.9 2017/08/02 02:19:56 ozaki-r Exp $
+# $NetBSD: Makefile,v 1.10 2017/10/30 15:59:23 ozaki-r Exp $
 #
 
 .include <bsd.own.mk>
@@ -6,11 +6,15 @@
 TESTSDIR=      ${TESTSBASE}/net/ipsec
 
 .for name in ipsec_ah_keys ipsec_esp_keys ipsec_gif ipsec_l2tp ipsec_misc \
-    ipsec_sockopt ipsec_sysctl ipsec_tcp ipsec_transport ipsec_tunnel \
-    ipsec_tunnel_ipcomp ipsec_tunnel_odd
+    ipsec_natt ipsec_sockopt ipsec_sysctl ipsec_tcp ipsec_transport \
+    ipsec_tunnel ipsec_tunnel_ipcomp ipsec_tunnel_odd
 TESTS_SH+=             t_${name}
 TESTS_SH_SRC_t_${name}=        ../net_common.sh ./common.sh ./algorithms.sh \
     t_${name}.sh
 .endfor
 
+PROGS=                 natt_terminator
+MAN.natt_terminator=   # empty
+BINDIR.natt_terminator=        ${TESTSDIR}
+
 .include <bsd.test.mk>
diff -r 909168a2c41d -r d4c2f839a45f tests/net/ipsec/natt_terminator.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/net/ipsec/natt_terminator.c Mon Oct 30 15:59:23 2017 +0000
@@ -0,0 +1,108 @@
+/*     $NetBSD: natt_terminator.c,v 1.1 2017/10/30 15:59:23 ozaki-r Exp $      */
+
+/*-
+ * Copyright (c) 2017 Internet Initiative Japan Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/wait.h>
+#include <sys/time.h>
+
+#include <netinet/in.h>
+#include <netinet/udp.h>
+
+#include <stdio.h>
+#include <err.h>
+#include <netdb.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(int argc, char **argv)
+{
+       struct addrinfo hints;
+       struct addrinfo *res;
+       int s, e;
+       const char *addr, *port;
+       int option;
+
+       if (argc != 3) {
+               fprintf(stderr, "Usage: %s <addr> <port>\n", argv[0]);
+               return 1;
+       }
+
+       addr = argv[1];
+       port = argv[2];
+
+       memset(&hints, 0, sizeof(hints));
+       hints.ai_family = AF_INET;
+       hints.ai_socktype = SOCK_DGRAM;
+       hints.ai_protocol = IPPROTO_UDP;
+       hints.ai_flags = 0;
+
+       e = getaddrinfo(addr, port, &hints, &res);
+       if (e != 0)
+               errx(EXIT_FAILURE, "getaddrinfo failed: %s", gai_strerror(e));
+
+       s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
+       if (s == -1)
+               err(EXIT_FAILURE, "socket");
+
+       /*
+        * Set the option to tell the kernel that the socket can handle
+        * UDP-encapsulated ESP packets for NAT-T.
+        */
+       option = UDP_ENCAP_ESPINUDP;
+       e = setsockopt(s, IPPROTO_UDP, UDP_ENCAP, &option, sizeof(option));
+       if (e == -1)
+               err(EXIT_FAILURE, "setsockopt(UDP_ENCAP)");
+
+       e = bind(s, res->ai_addr, res->ai_addrlen);
+       if (e == -1)
+               err(EXIT_FAILURE, "bind");
+
+       /* Receiving a packet make the NAPT create a mapping. */
+       {
+               char buf[64];
+               struct sockaddr_storage z;
+               socklen_t len = sizeof(z);
+
+               e = recvfrom(s, buf, 64, MSG_PEEK,
+                   (struct sockaddr *)&z, &len);
+               if (e == -1)
+                       err(EXIT_FAILURE, "recvfrom");
+       }
+
+       /*
+        * Keep the socket in the kernel to handle UDP-encapsulated ESP packets.
+        */
+       pause();
+
+       close(s);
+
+       return 0;
+}
diff -r 909168a2c41d -r d4c2f839a45f tests/net/ipsec/t_ipsec_natt.sh
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/net/ipsec/t_ipsec_natt.sh   Mon Oct 30 15:59:23 2017 +0000
@@ -0,0 +1,376 @@
+#      $NetBSD: t_ipsec_natt.sh,v 1.1 2017/10/30 15:59:23 ozaki-r Exp $
+#
+# Copyright (c) 2017 Internet Initiative Japan Inc.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+
+SOCK_LOCAL=unix://ipsec_natt_local
+SOCK_NAT=unix://ipsec_natt_nat
+SOCK_REMOTE=unix://ipsec_natt_remote
+BUS_LOCAL=./bus_ipsec_natt_local
+BUS_NAT=./bus_ipsec_natt_nat
+BUS_REMOTE=./bus_ipsec_natt_remote
+
+DEBUG=${DEBUG:-false}
+HIJACKING_NPF="${HIJACKING},blanket=/dev/npf"
+
+setup_servers()
+{
+
+       rump_server_crypto_start $SOCK_LOCAL netipsec
+       rump_server_npf_start $SOCK_NAT
+       rump_server_crypto_start $SOCK_REMOTE netipsec
+       rump_server_add_iface $SOCK_LOCAL shmif0 $BUS_LOCAL
+       rump_server_add_iface $SOCK_NAT shmif0 $BUS_LOCAL
+       rump_server_add_iface $SOCK_NAT shmif1 $BUS_NAT
+       rump_server_add_iface $SOCK_REMOTE shmif0 $BUS_NAT
+}
+
+setup_sp()
+{
+       local proto=$1
+       local algo_args="$2"
+       local ip_local=$3
+       local ip_remote=$4
+       local ip_nat_remote=$5
+       local tmpfile=./tmp
+
+       export RUMP_SERVER=$SOCK_LOCAL
+       cat > $tmpfile <<-EOF
+       spdadd $ip_local $ip_remote any -P out ipsec $proto/transport//require;
+       spdadd $ip_remote $ip_local any -P in ipsec $proto/transport//require;
+       EOF
+       $DEBUG && cat $tmpfile
+       atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
+       #check_sp_entries $SOCK_LOCAL $ip_local $ip_remote
+
+       export RUMP_SERVER=$SOCK_REMOTE
+       cat > $tmpfile <<-EOF
+       spdadd $ip_remote $ip_nat_remote any -P out ipsec $proto/transport//require;
+       spdadd $ip_local $ip_remote any -P in ipsec $proto/transport//require;
+       EOF
+       $DEBUG && cat $tmpfile
+       atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
+       #check_sp_entries $SOCK_REMOTE $ip_remote $ip_local
+}
+
+add_sa()
+{
+       local proto=$1
+       local algo_args="$2"
+       local ip_local=$3
+       local ip_remote=$4
+       local ip_nat_remote=$5
+       local spi=$6
+       local port=$7
+       local tmpfile=./tmp
+
+       export RUMP_SERVER=$SOCK_LOCAL
+       cat > $tmpfile <<-EOF
+       add $ip_local [4500] $ip_remote [4500] $proto $((spi)) $algo_args;
+       add $ip_remote [4500] $ip_local [4500] $proto $((spi + 1)) $algo_args;
+       EOF
+       $DEBUG && cat $tmpfile
+       atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
+       $DEBUG && $HIJACKING setkey -D
+       # XXX it can be expired if $lifetime is very short
+       #check_sa_entries $SOCK_LOCAL $ip_local $ip_remote
+
+       export RUMP_SERVER=$SOCK_REMOTE
+       cat > $tmpfile <<-EOF
+       add $ip_local [$port] $ip_remote [4500] $proto $((spi)) $algo_args;
+       add $ip_remote [4500] $ip_nat_remote [$port] $proto $((spi + 1)) $algo_args;
+       EOF
+       $DEBUG && cat $tmpfile
+       atf_check -s exit:0 -o empty $HIJACKING setkey -c < $tmpfile
+       $DEBUG && $HIJACKING setkey -D
+       # XXX it can be expired if $lifetime is very short
+       #check_sa_entries $SOCK_PEER $ip_local $ip_remote
+}
+
+prepare_file()
+{
+       local file=$1
+       local data="0123456789"
+
+       touch $file



Home | Main Index | Thread Index | Old Index