#1# SPDX-License-Identifier: BSD-2-Clause2#3# Copyright (c) 2024 Igor Ostapenko <[email protected]>4#5# Redistribution and use in source and binary forms, with or without6# modification, are permitted provided that the following conditions7# are met:8# 1. Redistributions of source code must retain the above copyright9# notice, this list of conditions and the following disclaimer.10# 2. Redistributions in binary form must reproduce the above copyright11# notice, this list of conditions and the following disclaimer in the12# documentation and/or other materials provided with the distribution.13#14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24# SUCH DAMAGE.2526. $(atf_get_srcdir)/utils.subr2728#29# The following network is used as a base for testing.30#31#32# ${awan}b |----------| ${bwan}b33# 2.0.0.1 | host wan | 3.0.0.134# .---->| Internet |<----.35# A WAN | |----------| | B WAN36# | |37# Office A side | | Office B side38# | ${awan}a ${bwan}a |39# v 2.0.0.22 3.0.0.33 v40# ${alan}b |----------| |----------| ${blan}b41# 1.0.0.1 | host agw | | host bgw | 4.0.0.142# .----------->| gateway | < IPsec > | gateway |<-----------.43# | A LAN |----------| tunnel |----------| B LAN |44# | |45# | |46# | ${alan}a ${blan}a |47# v 1.0.0.11 4.0.0.44 v48# |----------| |----------|49# | host a | | host b |50# | client | | client |51# |----------| |----------|52#53#54# There is routing between office A clients and office B ones. The traffic is55# encrypted, i.e. host wan should see IPsec flow (ESP packets).56#5758ipsec_init()59{60if ! sysctl -q kern.features.ipsec >/dev/null ; then61atf_skip "This test requires ipsec"62fi63}6465if_enc_init()66{67ipsec_init68if ! kldstat -q -m if_enc; then69atf_skip "This test requires if_enc"70fi71}7273build_test_network()74{75alan=$(vnet_mkepair)76awan=$(vnet_mkepair)77bwan=$(vnet_mkepair)78blan=$(vnet_mkepair)7980# host a81vnet_mkjail a ${alan}a82jexec a ifconfig ${alan}a 1.0.0.11/24 up83jexec a route add default 1.0.0.18485# host agw86vnet_mkjail agw ${alan}b ${awan}a87jexec agw ifconfig ${alan}b 1.0.0.1/24 up88jexec agw ifconfig ${awan}a 2.0.0.22/24 up89jexec agw route add default 2.0.0.190jexec agw sysctl net.inet.ip.forwarding=19192# host wan93vnet_mkjail wan ${awan}b ${bwan}b94jexec wan ifconfig ${awan}b 2.0.0.1/24 up95jexec wan ifconfig ${bwan}b 3.0.0.1/24 up96jexec wan sysctl net.inet.ip.forwarding=19798# host bgw99vnet_mkjail bgw ${bwan}a ${blan}b100jexec bgw ifconfig ${bwan}a 3.0.0.33/24 up101jexec bgw ifconfig ${blan}b 4.0.0.1/24 up102jexec bgw route add default 3.0.0.1103jexec bgw sysctl net.inet.ip.forwarding=1104105# host b106vnet_mkjail b ${blan}a107jexec b ifconfig ${blan}a 4.0.0.44/24 up108jexec b route add default 4.0.0.1109110# Office A VPN setup111echo '112spdadd 1.0.0.0/24 4.0.0.0/24 any -P out ipsec esp/tunnel/2.0.0.22-3.0.0.33/require;113spdadd 4.0.0.0/24 1.0.0.0/24 any -P in ipsec esp/tunnel/3.0.0.33-2.0.0.22/require;114add 2.0.0.22 3.0.0.33 esp 0x203 -E aes-gcm-16 "123456789012345678901234567890123456";115add 3.0.0.33 2.0.0.22 esp 0x302 -E aes-gcm-16 "123456789012345678901234567890123456";116' | jexec agw setkey -c117118# Office B VPN setup119echo '120spdadd 4.0.0.0/24 1.0.0.0/24 any -P out ipsec esp/tunnel/3.0.0.33-2.0.0.22/require;121spdadd 1.0.0.0/24 4.0.0.0/24 any -P in ipsec esp/tunnel/2.0.0.22-3.0.0.33/require;122add 2.0.0.22 3.0.0.33 esp 0x203 -E aes-gcm-16 "123456789012345678901234567890123456";123add 3.0.0.33 2.0.0.22 esp 0x302 -E aes-gcm-16 "123456789012345678901234567890123456";124' | jexec bgw setkey -c125}126127atf_test_case "ip4_pfil_in_after_stripping" "cleanup"128ip4_pfil_in_after_stripping_head()129{130atf_set descr 'Test that pf pulls up mbuf if m_len==0 after stripping the outer header'131atf_set require.user root132atf_set require.progs nc133}134ip4_pfil_in_after_stripping_body()135{136pft_init137if_enc_init138139build_test_network140141# Sanity check142atf_check -s exit:0 -o ignore jexec a ping -c3 4.0.0.44143144# Configure port forwarding on host bgw145jexec bgw ifconfig enc0 up146jexec bgw sysctl net.inet.ipsec.filtertunnel=0147jexec bgw sysctl net.enc.in.ipsec_filter_mask=2 # after stripping148jexec bgw sysctl net.enc.out.ipsec_filter_mask=1 # before outer header149jexec bgw pfctl -e150pft_set_rules bgw \151"rdr on enc0 proto tcp to 4.0.0.1 port 666 -> 4.0.0.44" \152"pass"153154# Prepare the catcher on host b155echo "unexpected" > ./receiver156jexec b nc -n4l -N 666 > ./receiver &157nc_pid=$!158sleep 1159160# Poke it from host a to host bgw161spell="Ak Ohum Oktay Weez Barsoom."162echo $spell | jexec a nc -w3 4.0.0.1 666163164# Expect it to hit host b instead165sleep 1 # let the catcher finish166jexec b kill -KILL $nc_pid # in a fail case the catcher may listen forever167atf_check_equal "$spell" "$(cat ./receiver)"168}169ip4_pfil_in_after_stripping_cleanup()170{171pft_cleanup172}173174atf_init_test_cases()175{176atf_add_test_case "ip4_pfil_in_after_stripping"177}178179180