# $NetBSD: t_ipsec_unnumbered.sh,v 1.2 2023/09/27 08:48:01 knakahara Exp $
#
# Copyright (c) 2022 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.
#
add_sa()
{
local outer=$1
local proto=$2
local algo="$3"
local src=$4
local dst=$5
local tmpfile=./tmp
local spi=10000
local algo_args="$(generate_algo_args esp $algo)"
local uniq=8192 # 8192(reqid_base) + 2 * 0(unit id of "ipsec0")
export RUMP_SERVER=$SOCK_LOCAL
cat > $tmpfile <<-EOF
add $src $dst $proto $((spi)) -u $uniq -m transport $algo_args;
add $dst $src $proto $((spi + 1)) -u $uniq -m transport $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
test_ipsecif_unnumbered_ipv4()
{
local algo=$1
local ip_local_i=192.168.22.1
local ip_local_i_subnet=192.168.22.0/24
local ip_local_o=10.0.0.2
local ip_remote_i=192.168.33.1
local ip_remote_i_subnet=192.168.33.0/24
local ip_remote_o=10.0.0.3
local outfile=./out
test_ipsecif_unnumbered_ipv6()
{
local algo=$1
local ip_local_i=192.168.22.1
local ip_local_i_subnet=192.168.22.0/24
local ip_local_o=fc00::2
local ip_remote_i=192.168.33.1
local ip_remote_i_subnet=192.168.33.0/24
local ip_remote_o=fc00::3
local outfile=./out