1# 2# SPDX-License-Identifier: BSD-2-Clause 3# 4# Copyright (c) 2024 Igor Ostapenko <pm@igoro.pro> 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions 8# are met: 9# 1. Redistributions of source code must retain the above copyright 10# notice, this list of conditions and the following disclaimer. 11# 2. Redistributions in binary form must reproduce the above copyright 12# notice, this list of conditions and the following disclaimer in the 13# documentation and/or other materials provided with the distribution. 14# 15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25# SUCH DAMAGE. 26 27. $(atf_get_srcdir)/utils.subr 28 29# 30# The following network is used as a base for testing. 31# 32# 33# ${awan}b |----------| ${bwan}b 34# 2.0.0.1 | host wan | 3.0.0.1 35# .---->| Internet |<----. 36# A WAN | |----------| | B WAN 37# | | 38# Office A side | | Office B side 39# | ${awan}a ${bwan}a | 40# v 2.0.0.22 3.0.0.33 v 41# ${alan}b |----------| |----------| ${blan}b 42# 1.0.0.1 | host agw | | host bgw | 4.0.0.1 43# .----------->| gateway | < IPsec > | gateway |<-----------. 44# | A LAN |----------| tunnel |----------| B LAN | 45# | | 46# | | 47# | ${alan}a ${blan}a | 48# v 1.0.0.11 4.0.0.44 v 49# |----------| |----------| 50# | host a | | host b | 51# | client | | client | 52# |----------| |----------| 53# 54# 55# There is routing between office A clients and office B ones. The traffic is 56# encrypted, i.e. host wan should see IPsec flow (ESP packets). 57# 58 59ipsec_init() 60{ 61 if ! sysctl -q kern.features.ipsec >/dev/null ; then 62 atf_skip "This test requires ipsec" 63 fi 64} 65 66if_enc_init() 67{ 68 ipsec_init 69 if ! kldstat -q -m if_enc; then 70 atf_skip "This test requires if_enc" 71 fi 72} 73 74build_test_network() 75{ 76 alan=$(vnet_mkepair) 77 awan=$(vnet_mkepair) 78 bwan=$(vnet_mkepair) 79 blan=$(vnet_mkepair) 80 81 # host a 82 vnet_mkjail a ${alan}a 83 jexec a ifconfig ${alan}a 1.0.0.11/24 up 84 jexec a route add default 1.0.0.1 85 86 # host agw 87 vnet_mkjail agw ${alan}b ${awan}a 88 jexec agw ifconfig ${alan}b 1.0.0.1/24 up 89 jexec agw ifconfig ${awan}a 2.0.0.22/24 up 90 jexec agw route add default 2.0.0.1 91 jexec agw sysctl net.inet.ip.forwarding=1 92 93 # host wan 94 vnet_mkjail wan ${awan}b ${bwan}b 95 jexec wan ifconfig ${awan}b 2.0.0.1/24 up 96 jexec wan ifconfig ${bwan}b 3.0.0.1/24 up 97 jexec wan sysctl net.inet.ip.forwarding=1 98 99 # host bgw 100 vnet_mkjail bgw ${bwan}a ${blan}b 101 jexec bgw ifconfig ${bwan}a 3.0.0.33/24 up 102 jexec bgw ifconfig ${blan}b 4.0.0.1/24 up 103 jexec bgw route add default 3.0.0.1 104 jexec bgw sysctl net.inet.ip.forwarding=1 105 106 # host b 107 vnet_mkjail b ${blan}a 108 jexec b ifconfig ${blan}a 4.0.0.44/24 up 109 jexec b route add default 4.0.0.1 110 111 # Office A VPN setup 112 echo ' 113 spdadd 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; 114 spdadd 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; 115 add 2.0.0.22 3.0.0.33 esp 0x203 -E aes-gcm-16 "123456789012345678901234567890123456"; 116 add 3.0.0.33 2.0.0.22 esp 0x302 -E aes-gcm-16 "123456789012345678901234567890123456"; 117 ' | jexec agw setkey -c 118 119 # Office B VPN setup 120 echo ' 121 spdadd 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; 122 spdadd 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; 123 add 2.0.0.22 3.0.0.33 esp 0x203 -E aes-gcm-16 "123456789012345678901234567890123456"; 124 add 3.0.0.33 2.0.0.22 esp 0x302 -E aes-gcm-16 "123456789012345678901234567890123456"; 125 ' | jexec bgw setkey -c 126} 127 128atf_test_case "ip4_pfil_in_after_stripping" "cleanup" 129ip4_pfil_in_after_stripping_head() 130{ 131 atf_set descr 'Test that pf pulls up mbuf if m_len==0 after stripping the outer header' 132 atf_set require.user root 133 atf_set require.progs nc 134} 135ip4_pfil_in_after_stripping_body() 136{ 137 pft_init 138 if_enc_init 139 140 build_test_network 141 142 # Sanity check 143 atf_check -s exit:0 -o ignore jexec a ping -c3 4.0.0.44 144 145 # Configure port forwarding on host bgw 146 jexec bgw ifconfig enc0 up 147 jexec bgw sysctl net.inet.ipsec.filtertunnel=0 148 jexec bgw sysctl net.enc.in.ipsec_filter_mask=2 # after stripping 149 jexec bgw sysctl net.enc.out.ipsec_filter_mask=1 # before outer header 150 jexec bgw pfctl -e 151 pft_set_rules bgw \ 152 "rdr on enc0 proto tcp to 4.0.0.1 port 666 -> 4.0.0.44" \ 153 "pass" 154 155 # Prepare the catcher on host b 156 echo "unexpected" > ./receiver 157 jexec b nc -n4l -N 666 > ./receiver & 158 nc_pid=$! 159 sleep 1 160 161 # Poke it from host a to host bgw 162 spell="Ak Ohum Oktay Weez Barsoom." 163 echo $spell | jexec a nc -w3 4.0.0.1 666 164 165 # Expect it to hit host b instead 166 sleep 1 # let the catcher finish 167 jexec b kill -KILL $nc_pid # in a fail case the catcher may listen forever 168 atf_check_equal "$spell" "$(cat ./receiver)" 169} 170ip4_pfil_in_after_stripping_cleanup() 171{ 172 pft_cleanup 173} 174 175atf_init_test_cases() 176{ 177 atf_add_test_case "ip4_pfil_in_after_stripping" 178} 179