xref: /freebsd/tests/sys/netpfil/pf/icmp6.sh (revision d154dc21130b607d7903f276dd6d463b990f33f7)
1b9f0dbc3SKristof Provost#
2b9f0dbc3SKristof Provost# SPDX-License-Identifier: BSD-2-Clause
3b9f0dbc3SKristof Provost#
4b9f0dbc3SKristof Provost# Copyright (c) 2024 Rubicon Communications, LLC (Netgate)
5b9f0dbc3SKristof Provost#
6b9f0dbc3SKristof Provost# Redistribution and use in source and binary forms, with or without
7b9f0dbc3SKristof Provost# modification, are permitted provided that the following conditions
8b9f0dbc3SKristof Provost# are met:
9b9f0dbc3SKristof Provost# 1. Redistributions of source code must retain the above copyright
10b9f0dbc3SKristof Provost#    notice, this list of conditions and the following disclaimer.
11b9f0dbc3SKristof Provost# 2. Redistributions in binary form must reproduce the above copyright
12b9f0dbc3SKristof Provost#    notice, this list of conditions and the following disclaimer in the
13b9f0dbc3SKristof Provost#    documentation and/or other materials provided with the distribution.
14b9f0dbc3SKristof Provost#
15b9f0dbc3SKristof Provost# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16b9f0dbc3SKristof Provost# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17b9f0dbc3SKristof Provost# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18b9f0dbc3SKristof Provost# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19b9f0dbc3SKristof Provost# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20b9f0dbc3SKristof Provost# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21b9f0dbc3SKristof Provost# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22b9f0dbc3SKristof Provost# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23b9f0dbc3SKristof Provost# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24b9f0dbc3SKristof Provost# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25b9f0dbc3SKristof Provost# SUCH DAMAGE.
26b9f0dbc3SKristof Provost
27b9f0dbc3SKristof Provost. $(atf_get_srcdir)/utils.subr
28b9f0dbc3SKristof Provost
29b9f0dbc3SKristof Provostcommon_dir=$(atf_get_srcdir)/../common
30b9f0dbc3SKristof Provost
31b9f0dbc3SKristof Provostatf_test_case "zero_id" "cleanup"
32b9f0dbc3SKristof Provostzero_id_head()
33b9f0dbc3SKristof Provost{
34b9f0dbc3SKristof Provost	atf_set descr 'Test ICMPv6 echo with ID 0 keep being blocked'
35b9f0dbc3SKristof Provost	atf_set require.user root
36b9f0dbc3SKristof Provost	atf_set require.progs scapy
37b9f0dbc3SKristof Provost}
38b9f0dbc3SKristof Provost
39b9f0dbc3SKristof Provostzero_id_body()
40b9f0dbc3SKristof Provost{
41b9f0dbc3SKristof Provost	pft_init
42b9f0dbc3SKristof Provost
43b9f0dbc3SKristof Provost	epair=$(vnet_mkepair)
44b9f0dbc3SKristof Provost	ifconfig ${epair}a inet6 2001:db8::2/64 up no_dad
45b9f0dbc3SKristof Provost
46b9f0dbc3SKristof Provost	vnet_mkjail alcatraz ${epair}b
47b9f0dbc3SKristof Provost	jexec alcatraz ifconfig ${epair}b inet6 2001:db8::1/64 up no_dad
48b9f0dbc3SKristof Provost
49b9f0dbc3SKristof Provost	# Sanity check
50b9f0dbc3SKristof Provost	atf_check -s exit:0 -o ignore \
51b9f0dbc3SKristof Provost	    ping -c 1 2001:db8::1
52b9f0dbc3SKristof Provost
53b9f0dbc3SKristof Provost	jexec alcatraz pfctl -e
54b9f0dbc3SKristof Provost	pft_set_rules alcatraz \
55b9f0dbc3SKristof Provost		"set block-policy drop" \
56b9f0dbc3SKristof Provost		"antispoof quick for { egress ${epair}b }" \
57b9f0dbc3SKristof Provost		"block all" \
58b9f0dbc3SKristof Provost		"pass out" \
59b9f0dbc3SKristof Provost		"pass in quick inet6 proto IPV6-ICMP icmp6-type 135" \
60b9f0dbc3SKristof Provost		"pass in quick inet6 proto IPV6-ICMP icmp6-type 136" \
61b9f0dbc3SKristof Provost		"pass out quick inet6 proto IPV6 from self to any"
62b9f0dbc3SKristof Provost
63b9f0dbc3SKristof Provost	# Now we can't ping
64b9f0dbc3SKristof Provost	atf_check -s exit:2 -o ignore \
65b9f0dbc3SKristof Provost	    ping -c 1 2001:db8::1
66b9f0dbc3SKristof Provost
67b9f0dbc3SKristof Provost	# Force neighbour discovery
68b9f0dbc3SKristof Provost	ndp -d 2001:db8::1
69b9f0dbc3SKristof Provost
70b9f0dbc3SKristof Provost	# Verify that we don't confuse echo request with ID 0 for neighbour discovery
71b9f0dbc3SKristof Provost	atf_check -s exit:1 -o ignore \
72b9f0dbc3SKristof Provost	     ${common_dir}/pft_ping.py \
73b9f0dbc3SKristof Provost	         --sendif ${epair}a \
74b9f0dbc3SKristof Provost	         --to 2001:db8::1 \
75b9f0dbc3SKristof Provost	         --replyif ${epair}a
76b9f0dbc3SKristof Provost
77b9f0dbc3SKristof Provost	jexec alcatraz pfctl -ss -vv
78b9f0dbc3SKristof Provost	jexec alcatraz pfctl -sr -vv
79b9f0dbc3SKristof Provost}
80b9f0dbc3SKristof Provost
81b9f0dbc3SKristof Provostzero_id_cleanup()
82b9f0dbc3SKristof Provost{
83b9f0dbc3SKristof Provost	pft_cleanup
84b9f0dbc3SKristof Provost}
85b9f0dbc3SKristof Provost
8634063cb7SKristof Provostatf_test_case "ttl_exceeded" "cleanup"
8734063cb7SKristof Provostttl_exceeded_head()
8834063cb7SKristof Provost{
8934063cb7SKristof Provost	atf_set descr 'Test that we correctly translate TTL exceeded back'
9034063cb7SKristof Provost	atf_set require.user root
9134063cb7SKristof Provost}
9234063cb7SKristof Provost
9334063cb7SKristof Provostttl_exceeded_body()
9434063cb7SKristof Provost{
9534063cb7SKristof Provost	pft_init
9634063cb7SKristof Provost
9734063cb7SKristof Provost	epair_srv=$(vnet_mkepair)
9834063cb7SKristof Provost	epair_int=$(vnet_mkepair)
9934063cb7SKristof Provost	epair_cl=$(vnet_mkepair)
10034063cb7SKristof Provost
10134063cb7SKristof Provost	vnet_mkjail srv ${epair_srv}a
10234063cb7SKristof Provost	jexec srv ifconfig ${epair_srv}a inet6 2001:db8:1::1/64 no_dad up
10334063cb7SKristof Provost	jexec srv route add -6 default 2001:db8:1::2
10434063cb7SKristof Provost
10534063cb7SKristof Provost	vnet_mkjail int ${epair_srv}b ${epair_int}a
10634063cb7SKristof Provost	jexec int sysctl net.inet6.ip6.forwarding=1
10734063cb7SKristof Provost	jexec int ifconfig ${epair_srv}b inet6 2001:db8:1::2/64 no_dad up
10834063cb7SKristof Provost	jexec int ifconfig ${epair_int}a inet6 2001:db8:2::2/64 no_dad up
10934063cb7SKristof Provost
11034063cb7SKristof Provost	vnet_mkjail nat ${epair_int}b ${epair_cl}b
11134063cb7SKristof Provost	jexec nat ifconfig ${epair_int}b inet6 2001:db8:2::1 no_dad up
11234063cb7SKristof Provost	jexec nat ifconfig ${epair_cl}b inet6 2001:db8:3::2/64 no_dad up
11334063cb7SKristof Provost	jexec nat sysctl net.inet6.ip6.forwarding=1
11434063cb7SKristof Provost	jexec nat route add -6 default 2001:db8:2::2
11534063cb7SKristof Provost
11634063cb7SKristof Provost	vnet_mkjail cl ${epair_cl}a
11734063cb7SKristof Provost	jexec cl ifconfig ${epair_cl}a inet6 2001:db8:3::1/64 no_dad up
11834063cb7SKristof Provost	jexec cl route add -6 default 2001:db8:3::2
11934063cb7SKristof Provost
12034063cb7SKristof Provost	jexec nat pfctl -e
12134063cb7SKristof Provost	pft_set_rules nat \
12234063cb7SKristof Provost	    "nat on ${epair_int}b from 2001:db8:3::/64 -> (${epair_int}b:0)" \
12389f67232SKristof Provost	    "block" \
12489f67232SKristof Provost	    "pass inet6 proto udp" \
12589f67232SKristof Provost	    "pass inet6 proto icmp6 icmp6-type { neighbrsol, neighbradv, echoreq }"
12634063cb7SKristof Provost
12734063cb7SKristof Provost	# Sanity checks
12834063cb7SKristof Provost	atf_check -s exit:0 -o ignore \
12934063cb7SKristof Provost	    jexec cl ping -c 1 2001:db8:3::2
13034063cb7SKristof Provost	atf_check -s exit:0 -o ignore \
13134063cb7SKristof Provost	    jexec cl ping -c 1 2001:db8:2::1
13234063cb7SKristof Provost	atf_check -s exit:0 -o ignore \
13334063cb7SKristof Provost	    jexec cl ping -c 1 2001:db8:2::2
13434063cb7SKristof Provost	atf_check -s exit:0 -o ignore \
13534063cb7SKristof Provost	    jexec cl ping -c 1 2001:db8:1::1
13634063cb7SKristof Provost
13734063cb7SKristof Provost	echo "UDP"
13834063cb7SKristof Provost	atf_check -s exit:0 -e ignore -o match:".*2001:db8:2::2.*" \
13934063cb7SKristof Provost	    jexec cl traceroute6 2001:db8:1::1
14034063cb7SKristof Provost	jexec nat pfctl -Fs
14134063cb7SKristof Provost
14234063cb7SKristof Provost	echo "ICMP"
14334063cb7SKristof Provost	atf_check -s exit:0 -e ignore -o match:".*2001:db8:2::2.*" \
14434063cb7SKristof Provost	    jexec cl traceroute6 -I 2001:db8:1::1
14534063cb7SKristof Provost}
14634063cb7SKristof Provost
14734063cb7SKristof Provostttl_exceeded_cleanup()
14834063cb7SKristof Provost{
14934063cb7SKristof Provost	pft_cleanup
15034063cb7SKristof Provost}
15134063cb7SKristof Provost
152*d154dc21SKristof Provostatf_test_case "repeat" "cleanup"
153*d154dc21SKristof Provostrepeat_head()
154*d154dc21SKristof Provost{
155*d154dc21SKristof Provost	atf_set descr 'Ensure that repeated NDs work'
156*d154dc21SKristof Provost	atf_set require.user root
157*d154dc21SKristof Provost	atf_set require.progs ndisc6
158*d154dc21SKristof Provost}
159*d154dc21SKristof Provost
160*d154dc21SKristof Provostrepeat_body()
161*d154dc21SKristof Provost{
162*d154dc21SKristof Provost	pft_init
163*d154dc21SKristof Provost
164*d154dc21SKristof Provost	epair=$(vnet_mkepair)
165*d154dc21SKristof Provost	ifconfig ${epair}a inet6 2001:db8::2/64 up no_dad
166*d154dc21SKristof Provost
167*d154dc21SKristof Provost	vnet_mkjail alcatraz ${epair}b
168*d154dc21SKristof Provost	jexec alcatraz ifconfig ${epair}b inet6 2001:db8::1/64 up no_dad
169*d154dc21SKristof Provost
170*d154dc21SKristof Provost	# Sanity check
171*d154dc21SKristof Provost	atf_check -s exit:0 -o ignore \
172*d154dc21SKristof Provost	    ping -c 1 2001:db8::1
173*d154dc21SKristof Provost
174*d154dc21SKristof Provost	jexec alcatraz pfctl -e
175*d154dc21SKristof Provost	pft_set_rules alcatraz \
176*d154dc21SKristof Provost		"block all" \
177*d154dc21SKristof Provost		"pass quick inet6 proto ipv6-icmp all icmp6-type neighbrsol keep state (if-bound) ridentifier 1000000107"
178*d154dc21SKristof Provost
179*d154dc21SKristof Provost	jexec alcatraz pfctl -x loud
180*d154dc21SKristof Provost	  ndisc6 -m -n -r 1 2001:db8::1 ${epair}a
181*d154dc21SKristof Provost	jexec alcatraz pfctl -ss -vv
182*d154dc21SKristof Provost
183*d154dc21SKristof Provost	atf_check -s exit:0 -o ignore \
184*d154dc21SKristof Provost	  ndisc6 -m -n -r 1 2001:db8::1 ${epair}a
185*d154dc21SKristof Provost	jexec alcatraz pfctl -ss -vv
186*d154dc21SKristof Provost	atf_check -s exit:0 -o ignore \
187*d154dc21SKristof Provost	  ndisc6 -m -n -r 1 2001:db8::1 ${epair}a
188*d154dc21SKristof Provost	jexec alcatraz pfctl -ss -vv
189*d154dc21SKristof Provost	atf_check -s exit:0 -o ignore \
190*d154dc21SKristof Provost	  ndisc6 -m -n -r 1 2001:db8::1 ${epair}a
191*d154dc21SKristof Provost	jexec alcatraz pfctl -ss -vv
192*d154dc21SKristof Provost}
193*d154dc21SKristof Provost
194*d154dc21SKristof Provostrepeat_cleanup()
195*d154dc21SKristof Provost{
196*d154dc21SKristof Provost	pft_cleanup
197*d154dc21SKristof Provost}
198*d154dc21SKristof Provost
199b9f0dbc3SKristof Provostatf_init_test_cases()
200b9f0dbc3SKristof Provost{
201b9f0dbc3SKristof Provost	atf_add_test_case "zero_id"
20234063cb7SKristof Provost	atf_add_test_case "ttl_exceeded"
203*d154dc21SKristof Provost	atf_add_test_case "repeat"
204b9f0dbc3SKristof Provost}
205