# $NetBSD: t_ipsec_pfil.sh,v 1.3 2020/08/05 01:10:50 knakahara Exp $
#
# Copyright (c) 2019 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.
#
setup_if_ipsec_sa()
{
local src=$1
local dst=$2
local inid=$3
local outid=$4
local proto=$5
local algo=$6
local tmpfile=./tmp
local inunique=""
local outunique=""
local algo_args="$(generate_algo_args $proto $algo)"
inunique=`get_if_ipsec_unique $dst "ipv4"`
atf_check -s exit:0 test "X$inunique" != "X"
outunique=`get_if_ipsec_unique $src "ipv4"`
atf_check -s exit:0 test "X$outunique" != "X"
prepare_file()
{
local file=$1
local data="0123456789"
touch $file
for i in `seq 1 512`
do
echo $data >> $file
done
}
build_npf_conf()
{
local outfile=$1
local subnet=$2
local direction=$3
local reverse=
if [ "X${direction}" = "Xin" ] ; then
reverse="out"
else
reverse="in"
fi
cat > $outfile <<-EOF
set bpf.jit off
\$if = inet4(ipsec0)
\$subnet = { $subnet }
procedure "log0" {
log: npflog0
}
group default {
block $direction on \$if proto tcp from \$subnet apply "log0"
pass $reverse on \$if proto tcp from \$subnet
pass in on \$if proto icmp from 0.0.0.0/0
pass out on \$if proto icmp from 0.0.0.0/0
pass final on shmif0 all
pass final on shmif1 all
}
EOF
}
ipsecif_pfil_test()
{
local outfile=./out
local npffile=./npf.conf
local file_send=./file.send
local file_recv=./file.recv