xref: /freebsd/tests/sys/netpfil/pf/fragmentation_pass.sh (revision c46af893cd7aaff322b6f867539bc8fe5bcde6f9)
1b800be97SKajetan Staszkiewicz#
24d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause
3b800be97SKajetan Staszkiewicz#
4b800be97SKajetan Staszkiewicz# Copyright (c) 2017 Kristof Provost <kp@FreeBSD.org>
5b800be97SKajetan Staszkiewicz#
6b800be97SKajetan Staszkiewicz# Redistribution and use in source and binary forms, with or without
7b800be97SKajetan Staszkiewicz# modification, are permitted provided that the following conditions
8b800be97SKajetan Staszkiewicz# are met:
9b800be97SKajetan Staszkiewicz# 1. Redistributions of source code must retain the above copyright
10b800be97SKajetan Staszkiewicz#    notice, this list of conditions and the following disclaimer.
11b800be97SKajetan Staszkiewicz# 2. Redistributions in binary form must reproduce the above copyright
12b800be97SKajetan Staszkiewicz#    notice, this list of conditions and the following disclaimer in the
13b800be97SKajetan Staszkiewicz#    documentation and/or other materials provided with the distribution.
14b800be97SKajetan Staszkiewicz#
15b800be97SKajetan Staszkiewicz# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16b800be97SKajetan Staszkiewicz# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17b800be97SKajetan Staszkiewicz# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18b800be97SKajetan Staszkiewicz# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19b800be97SKajetan Staszkiewicz# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20b800be97SKajetan Staszkiewicz# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21b800be97SKajetan Staszkiewicz# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22b800be97SKajetan Staszkiewicz# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23b800be97SKajetan Staszkiewicz# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24b800be97SKajetan Staszkiewicz# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25b800be97SKajetan Staszkiewicz# SUCH DAMAGE.
26b800be97SKajetan Staszkiewicz
27b800be97SKajetan Staszkiewicz. $(atf_get_srcdir)/utils.subr
28b800be97SKajetan Staszkiewicz
29b800be97SKajetan Staszkiewiczcommon_dir=$(atf_get_srcdir)/../common
30b800be97SKajetan Staszkiewicz
31b800be97SKajetan Staszkiewiczatf_test_case "too_many_fragments" "cleanup"
32b800be97SKajetan Staszkiewicz
33b800be97SKajetan Staszkiewicztoo_many_fragments_head()
34b800be97SKajetan Staszkiewicz{
35b800be97SKajetan Staszkiewicz	atf_set descr 'IPv4 fragment limitation test'
36b800be97SKajetan Staszkiewicz	atf_set require.user root
37b800be97SKajetan Staszkiewicz}
38b800be97SKajetan Staszkiewicz
39b800be97SKajetan Staszkiewicztoo_many_fragments_body()
40b800be97SKajetan Staszkiewicz{
41b800be97SKajetan Staszkiewicz	pft_init
42b800be97SKajetan Staszkiewicz
43b800be97SKajetan Staszkiewicz	epair=$(vnet_mkepair)
44b800be97SKajetan Staszkiewicz	vnet_mkjail alcatraz ${epair}a
45b800be97SKajetan Staszkiewicz
46b800be97SKajetan Staszkiewicz	ifconfig ${epair}b inet 192.0.2.1/24 up
47b800be97SKajetan Staszkiewicz	jexec alcatraz ifconfig ${epair}a 192.0.2.2/24 up
48b800be97SKajetan Staszkiewicz
49b800be97SKajetan Staszkiewicz	ifconfig ${epair}b mtu 200
50b800be97SKajetan Staszkiewicz	jexec alcatraz ifconfig ${epair}a mtu 200
51b800be97SKajetan Staszkiewicz
52b800be97SKajetan Staszkiewicz	jexec alcatraz pfctl -e
53b800be97SKajetan Staszkiewicz	pft_set_rules alcatraz \
54b800be97SKajetan Staszkiewicz		"set reassemble yes" \
55b800be97SKajetan Staszkiewicz		"pass keep state"
56b800be97SKajetan Staszkiewicz
57b800be97SKajetan Staszkiewicz	# So we know pf is limiting things
58b800be97SKajetan Staszkiewicz	jexec alcatraz sysctl net.inet.ip.maxfragsperpacket=1024
59b800be97SKajetan Staszkiewicz
60b800be97SKajetan Staszkiewicz	# Sanity check
61b800be97SKajetan Staszkiewicz	atf_check -s exit:0 -o ignore ping -c 1 192.0.2.2
62b800be97SKajetan Staszkiewicz
63b800be97SKajetan Staszkiewicz	# We can ping with < 64 fragments
64b800be97SKajetan Staszkiewicz	atf_check -s exit:0 -o ignore ping -c 1 -s 800 192.0.2.2
65b800be97SKajetan Staszkiewicz
66b800be97SKajetan Staszkiewicz	# Too many fragments should fail
67b800be97SKajetan Staszkiewicz	atf_check -s exit:2 -o ignore ping -c 1 -s 20000 192.0.2.2
68b800be97SKajetan Staszkiewicz}
69b800be97SKajetan Staszkiewicz
70b800be97SKajetan Staszkiewicztoo_many_fragments_cleanup()
71b800be97SKajetan Staszkiewicz{
72b800be97SKajetan Staszkiewicz	pft_cleanup
73b800be97SKajetan Staszkiewicz}
74b800be97SKajetan Staszkiewicz
75b800be97SKajetan Staszkiewiczatf_test_case "v6" "cleanup"
76b800be97SKajetan Staszkiewiczv6_head()
77b800be97SKajetan Staszkiewicz{
78b800be97SKajetan Staszkiewicz	atf_set descr 'IPv6 fragmentation test'
79b800be97SKajetan Staszkiewicz	atf_set require.user root
80*c46af893SJose Luis Duran	atf_set require.progs python3 scapy
81b800be97SKajetan Staszkiewicz}
82b800be97SKajetan Staszkiewicz
83b800be97SKajetan Staszkiewiczv6_body()
84b800be97SKajetan Staszkiewicz{
85b800be97SKajetan Staszkiewicz	pft_init
86b800be97SKajetan Staszkiewicz
87b800be97SKajetan Staszkiewicz	epair_send=$(vnet_mkepair)
88b800be97SKajetan Staszkiewicz	epair_link=$(vnet_mkepair)
89b800be97SKajetan Staszkiewicz
90b800be97SKajetan Staszkiewicz	vnet_mkjail alcatraz ${epair_send}b ${epair_link}a
91b800be97SKajetan Staszkiewicz	vnet_mkjail singsing ${epair_link}b
92b800be97SKajetan Staszkiewicz
93b800be97SKajetan Staszkiewicz	ifconfig ${epair_send}a inet6 2001:db8:42::1/64 no_dad up
94b800be97SKajetan Staszkiewicz
95b800be97SKajetan Staszkiewicz	jexec alcatraz ifconfig ${epair_send}b inet6 2001:db8:42::2/64 no_dad up
96b800be97SKajetan Staszkiewicz	jexec alcatraz ifconfig ${epair_link}a inet6 2001:db8:43::2/64 no_dad up
97b800be97SKajetan Staszkiewicz	jexec alcatraz sysctl net.inet6.ip6.forwarding=1
98b800be97SKajetan Staszkiewicz
99b800be97SKajetan Staszkiewicz	jexec singsing ifconfig ${epair_link}b inet6 2001:db8:43::3/64 no_dad up
100b800be97SKajetan Staszkiewicz	jexec singsing route add -6 2001:db8:42::/64 2001:db8:43::2
101b800be97SKajetan Staszkiewicz	route add -6 2001:db8:43::/64 2001:db8:42::2
102b800be97SKajetan Staszkiewicz
103b800be97SKajetan Staszkiewicz	jexec alcatraz ifconfig ${epair_send}b inet6 -ifdisabled
104b800be97SKajetan Staszkiewicz	jexec alcatraz ifconfig ${epair_link}a inet6 -ifdisabled
105b800be97SKajetan Staszkiewicz	jexec singsing ifconfig ${epair_link}b inet6 -ifdisabled
106b800be97SKajetan Staszkiewicz	ifconfig ${epair_send}a inet6 -ifdisabled
107b800be97SKajetan Staszkiewicz
108b800be97SKajetan Staszkiewicz	ifconfig ${epair_send}a
109b800be97SKajetan Staszkiewicz	jexec alcatraz ifconfig ${epair_send}b
110b800be97SKajetan Staszkiewicz	lladdr=$(jexec alcatraz ifconfig ${epair_send}b | awk '/ scopeid / { print($2); }' | cut -f 1 -d %)
111b800be97SKajetan Staszkiewicz
112b800be97SKajetan Staszkiewicz	jexec alcatraz pfctl -e
113b800be97SKajetan Staszkiewicz	pft_set_rules alcatraz \
114b800be97SKajetan Staszkiewicz		"set reassemble yes" \
115b800be97SKajetan Staszkiewicz		"pass keep state" \
116b800be97SKajetan Staszkiewicz		"block in" \
117b800be97SKajetan Staszkiewicz		"pass in inet6 proto icmp6 icmp6-type { neighbrsol, neighbradv }" \
1183a1f834bSDoug Rabson		"pass in inet6 proto icmp6 icmp6-type { echoreq, echorep }" \
1193a1f834bSDoug Rabson		"set skip on lo"
120b800be97SKajetan Staszkiewicz
121b800be97SKajetan Staszkiewicz	# Host test
122b800be97SKajetan Staszkiewicz	atf_check -s exit:0 -o ignore \
123b800be97SKajetan Staszkiewicz		ping -6 -c 1 2001:db8:42::2
124b800be97SKajetan Staszkiewicz
125b800be97SKajetan Staszkiewicz	atf_check -s exit:0 -o ignore \
126b800be97SKajetan Staszkiewicz		ping -6 -c 1 -s 4500 2001:db8:42::2
127b800be97SKajetan Staszkiewicz
128b800be97SKajetan Staszkiewicz	atf_check -s exit:0 -o ignore\
129b800be97SKajetan Staszkiewicz		ping -6 -c 1 -b 70000 -s 65000 2001:db8:42::2
130b800be97SKajetan Staszkiewicz
131b800be97SKajetan Staszkiewicz	# Force an NDP lookup
132b800be97SKajetan Staszkiewicz	ping -6 -c 1 ${lladdr}%${epair_send}a
133b800be97SKajetan Staszkiewicz
134b800be97SKajetan Staszkiewicz	atf_check -s exit:0 -o ignore\
135b800be97SKajetan Staszkiewicz		ping -6 -c 1 -b 70000 -s 65000 ${lladdr}%${epair_send}a
136b800be97SKajetan Staszkiewicz
137b800be97SKajetan Staszkiewicz	# Forwarding test
138b800be97SKajetan Staszkiewicz	atf_check -s exit:0 -o ignore \
139b800be97SKajetan Staszkiewicz		ping -6 -c 1 2001:db8:43::3
140b800be97SKajetan Staszkiewicz
141b800be97SKajetan Staszkiewicz	atf_check -s exit:0 -o ignore \
142b800be97SKajetan Staszkiewicz		ping -6 -c 1 -s 4500 2001:db8:43::3
143b800be97SKajetan Staszkiewicz
144b800be97SKajetan Staszkiewicz	atf_check -s exit:0 -o ignore\
145b800be97SKajetan Staszkiewicz		ping -6 -c 1 -b 70000 -s 65000 2001:db8:43::3
146b800be97SKajetan Staszkiewicz
147b800be97SKajetan Staszkiewicz	$(atf_get_srcdir)/CVE-2019-5597.py \
148b800be97SKajetan Staszkiewicz		${epair_send}a \
149b800be97SKajetan Staszkiewicz		2001:db8:42::1 \
150b800be97SKajetan Staszkiewicz		2001:db8:43::3
151b800be97SKajetan Staszkiewicz}
152b800be97SKajetan Staszkiewicz
153b800be97SKajetan Staszkiewiczv6_cleanup()
154b800be97SKajetan Staszkiewicz{
155b800be97SKajetan Staszkiewicz	pft_cleanup
156b800be97SKajetan Staszkiewicz}
157b800be97SKajetan Staszkiewicz
15856b7685aSKristof Provostatf_test_case "v6_route_to" "cleanup"
15956b7685aSKristof Provostv6_route_to_head()
16056b7685aSKristof Provost{
16156b7685aSKristof Provost	atf_set descr 'Test IPv6 reassembly combined with route-to'
16256b7685aSKristof Provost	atf_set require.user root
16356b7685aSKristof Provost}
16456b7685aSKristof Provost
16556b7685aSKristof Provostv6_route_to_body()
16656b7685aSKristof Provost{
16756b7685aSKristof Provost	pft_init
16856b7685aSKristof Provost
16956b7685aSKristof Provost	epair_send=$(vnet_mkepair)
17056b7685aSKristof Provost	epair_link=$(vnet_mkepair)
17156b7685aSKristof Provost
17256b7685aSKristof Provost	vnet_mkjail alcatraz ${epair_send}b ${epair_link}a
17356b7685aSKristof Provost	vnet_mkjail singsing ${epair_link}b
17456b7685aSKristof Provost
17556b7685aSKristof Provost	ifconfig ${epair_send}a inet6 2001:db8:42::1/64 no_dad up
17656b7685aSKristof Provost
17756b7685aSKristof Provost	jexec alcatraz ifconfig ${epair_send}b inet6 2001:db8:42::2/64 no_dad up
17856b7685aSKristof Provost	jexec alcatraz ifconfig ${epair_link}a inet6 2001:db8:43::2/64 no_dad up
17956b7685aSKristof Provost	jexec alcatraz sysctl net.inet6.ip6.forwarding=1
18056b7685aSKristof Provost
18156b7685aSKristof Provost	jexec singsing ifconfig ${epair_link}b inet6 2001:db8:43::3/64 no_dad up
18256b7685aSKristof Provost	jexec singsing route add -6 2001:db8:42::/64 2001:db8:43::2
18356b7685aSKristof Provost	route add -6 2001:db8:43::/64 2001:db8:42::2
18456b7685aSKristof Provost
18556b7685aSKristof Provost	jexec alcatraz ifconfig ${epair_send}b inet6 -ifdisabled
18656b7685aSKristof Provost	jexec alcatraz ifconfig ${epair_link}a inet6 -ifdisabled
18756b7685aSKristof Provost	jexec singsing ifconfig ${epair_link}b inet6 -ifdisabled
18856b7685aSKristof Provost	ifconfig ${epair_send}a inet6 -ifdisabled
18956b7685aSKristof Provost
19056b7685aSKristof Provost	jexec alcatraz pfctl -e
19156b7685aSKristof Provost	pft_set_rules alcatraz \
19256b7685aSKristof Provost		"set reassemble yes" \
19356b7685aSKristof Provost		"pass" \
19456b7685aSKristof Provost		"pass in route-to (${epair_link}a 2001:db8:43::3) inet6 proto icmp6 from any to 2001:db8:43::3 keep state"
19556b7685aSKristof Provost
19656b7685aSKristof Provost	# Forwarding test
19756b7685aSKristof Provost	atf_check -s exit:0 -o ignore \
19856b7685aSKristof Provost		ping -6 -c 1 2001:db8:43::3
19956b7685aSKristof Provost
20056b7685aSKristof Provost	atf_check -s exit:0 -o ignore \
20156b7685aSKristof Provost		ping -6 -c 1 -s 4500 2001:db8:43::3
20256b7685aSKristof Provost
20356b7685aSKristof Provost	atf_check -s exit:0 -o ignore\
20456b7685aSKristof Provost		ping -6 -c 1 -b 70000 -s 65000 2001:db8:43::3
20556b7685aSKristof Provost
20656b7685aSKristof Provost	# Now test this without fragmentation
20756b7685aSKristof Provost	pft_set_rules alcatraz \
20856b7685aSKristof Provost		"set reassemble no" \
20956b7685aSKristof Provost		"pass" \
21056b7685aSKristof Provost		"pass in route-to (${epair_link}a 2001:db8:43::3) inet6 proto icmp6 from any to 2001:db8:43::3 keep state"
21156b7685aSKristof Provost
21256b7685aSKristof Provost	atf_check -s exit:0 -o ignore \
21356b7685aSKristof Provost		ping -6 -c 1 2001:db8:43::3
21456b7685aSKristof Provost
21556b7685aSKristof Provost	atf_check -s exit:0 -o ignore \
21656b7685aSKristof Provost		ping -6 -c 1 -s 4500 2001:db8:43::3
21756b7685aSKristof Provost
21856b7685aSKristof Provost	atf_check -s exit:0 -o ignore\
21956b7685aSKristof Provost		ping -6 -c 1 -b 70000 -s 65000 2001:db8:43::3
22056b7685aSKristof Provost}
22156b7685aSKristof Provost
222e736f6dfSKristof Provostv6_route_to_cleanup()
223e736f6dfSKristof Provost{
224e736f6dfSKristof Provost	pft_cleanup
225e736f6dfSKristof Provost}
226e736f6dfSKristof Provost
227b800be97SKajetan Staszkiewiczatf_test_case "mtu_diff" "cleanup"
228b800be97SKajetan Staszkiewiczmtu_diff_head()
229b800be97SKajetan Staszkiewicz{
230b800be97SKajetan Staszkiewicz	atf_set descr 'Test reassembly across different MTUs, PR #255432'
231b800be97SKajetan Staszkiewicz	atf_set require.user root
232b800be97SKajetan Staszkiewicz}
233b800be97SKajetan Staszkiewicz
234b800be97SKajetan Staszkiewiczmtu_diff_body()
235b800be97SKajetan Staszkiewicz{
236b800be97SKajetan Staszkiewicz	pft_init
237b800be97SKajetan Staszkiewicz
238b800be97SKajetan Staszkiewicz	epair_small=$(vnet_mkepair)
239b800be97SKajetan Staszkiewicz	epair_large=$(vnet_mkepair)
240b800be97SKajetan Staszkiewicz
241b800be97SKajetan Staszkiewicz	vnet_mkjail first ${epair_small}b ${epair_large}a
242b800be97SKajetan Staszkiewicz	vnet_mkjail second ${epair_large}b
243b800be97SKajetan Staszkiewicz
244b800be97SKajetan Staszkiewicz	ifconfig ${epair_small}a 192.0.2.1/25 up
245b800be97SKajetan Staszkiewicz	jexec first ifconfig ${epair_small}b 192.0.2.2/25 up
246b800be97SKajetan Staszkiewicz
247b800be97SKajetan Staszkiewicz	jexec first sysctl net.inet.ip.forwarding=1
248b800be97SKajetan Staszkiewicz	jexec first ifconfig ${epair_large}a 192.0.2.130/25 up
249b800be97SKajetan Staszkiewicz	jexec first ifconfig ${epair_large}a mtu 9000
250b800be97SKajetan Staszkiewicz	jexec second ifconfig ${epair_large}b 192.0.2.131/25 up
251b800be97SKajetan Staszkiewicz	jexec second ifconfig ${epair_large}b mtu 9000
252b800be97SKajetan Staszkiewicz	jexec second route add default 192.0.2.130
253b800be97SKajetan Staszkiewicz
254b800be97SKajetan Staszkiewicz	route add 192.0.2.128/25 192.0.2.2
255b800be97SKajetan Staszkiewicz
256b800be97SKajetan Staszkiewicz	jexec first pfctl -e
257b800be97SKajetan Staszkiewicz	pft_set_rules first \
258b800be97SKajetan Staszkiewicz		"set reassemble yes" \
259b800be97SKajetan Staszkiewicz		"pass keep state"
260b800be97SKajetan Staszkiewicz
261b800be97SKajetan Staszkiewicz	# Sanity checks
262b800be97SKajetan Staszkiewicz	atf_check -s exit:0 -o ignore ping -c 1 192.0.2.2
263b800be97SKajetan Staszkiewicz	atf_check -s exit:0 -o ignore ping -c 1 192.0.2.130
264b800be97SKajetan Staszkiewicz	atf_check -s exit:0 -o ignore ping -c 1 192.0.2.131
265b800be97SKajetan Staszkiewicz
266b800be97SKajetan Staszkiewicz	# Large packet that'll get reassembled and sent out in one on the large
267b800be97SKajetan Staszkiewicz	# epair
268b800be97SKajetan Staszkiewicz	atf_check -s exit:0 -o ignore ping -c 1 -s 8000 192.0.2.131
269b800be97SKajetan Staszkiewicz}
270b800be97SKajetan Staszkiewicz
271b800be97SKajetan Staszkiewiczmtu_diff_cleanup()
272b800be97SKajetan Staszkiewicz{
273b800be97SKajetan Staszkiewicz	pft_cleanup
274b800be97SKajetan Staszkiewicz}
275b800be97SKajetan Staszkiewicz
276b800be97SKajetan Staszkiewiczfrag_common()
277b800be97SKajetan Staszkiewicz{
278b800be97SKajetan Staszkiewicz	name=$1
279b800be97SKajetan Staszkiewicz
280b800be97SKajetan Staszkiewicz	pft_init
281b800be97SKajetan Staszkiewicz
282b800be97SKajetan Staszkiewicz	epair=$(vnet_mkepair)
283b800be97SKajetan Staszkiewicz	vnet_mkjail alcatraz ${epair}a
284b800be97SKajetan Staszkiewicz
285b800be97SKajetan Staszkiewicz	ifconfig ${epair}b inet 192.0.2.1/24 up
286b800be97SKajetan Staszkiewicz	jexec alcatraz ifconfig ${epair}a 192.0.2.2/24 up
287b800be97SKajetan Staszkiewicz
288b800be97SKajetan Staszkiewicz	jexec alcatraz pfctl -e
289b800be97SKajetan Staszkiewicz	pft_set_rules alcatraz \
290b800be97SKajetan Staszkiewicz		"set reassemble yes" \
291b800be97SKajetan Staszkiewicz		"pass keep state"
292b800be97SKajetan Staszkiewicz
293b800be97SKajetan Staszkiewicz	# Sanity check
294b800be97SKajetan Staszkiewicz	atf_check -s exit:0 -o ignore ping -c 1 192.0.2.2
295b800be97SKajetan Staszkiewicz
296b800be97SKajetan Staszkiewicz	atf_check -s exit:0 -o ignore $(atf_get_srcdir)/frag-${1}.py \
297b800be97SKajetan Staszkiewicz		--to 192.0.2.2 \
298b800be97SKajetan Staszkiewicz		--fromaddr 192.0.2.1 \
299b800be97SKajetan Staszkiewicz		--sendif ${epair}b \
300b800be97SKajetan Staszkiewicz		--recvif ${epair}b
301b800be97SKajetan Staszkiewicz}
302b800be97SKajetan Staszkiewicz
303b800be97SKajetan Staszkiewiczatf_test_case "overreplace" "cleanup"
304b800be97SKajetan Staszkiewiczoverreplace_head()
305b800be97SKajetan Staszkiewicz{
306b800be97SKajetan Staszkiewicz	atf_set descr 'ping fragment that overlaps fragment at index boundary and replace it'
307b800be97SKajetan Staszkiewicz	atf_set require.user root
308*c46af893SJose Luis Duran	atf_set require.progs python3 scapy
309b800be97SKajetan Staszkiewicz}
310b800be97SKajetan Staszkiewicz
311b800be97SKajetan Staszkiewiczoverreplace_body()
312b800be97SKajetan Staszkiewicz{
313b800be97SKajetan Staszkiewicz	frag_common overreplace
314b800be97SKajetan Staszkiewicz}
315b800be97SKajetan Staszkiewicz
316b800be97SKajetan Staszkiewiczoverreplace_cleanup()
317b800be97SKajetan Staszkiewicz{
318b800be97SKajetan Staszkiewicz	pft_cleanup
319b800be97SKajetan Staszkiewicz}
320b800be97SKajetan Staszkiewicz
321b800be97SKajetan Staszkiewiczatf_test_case "overindex" "cleanup"
322b800be97SKajetan Staszkiewiczoverindex_head()
323b800be97SKajetan Staszkiewicz{
324b800be97SKajetan Staszkiewicz	atf_set descr 'ping fragment that overlaps the first fragment at index boundary'
325b800be97SKajetan Staszkiewicz	atf_set require.user root
326*c46af893SJose Luis Duran	atf_set require.progs python3 scapy
327b800be97SKajetan Staszkiewicz}
328b800be97SKajetan Staszkiewicz
329b800be97SKajetan Staszkiewiczoverindex_body()
330b800be97SKajetan Staszkiewicz{
331b800be97SKajetan Staszkiewicz	frag_common overindex
332b800be97SKajetan Staszkiewicz}
333b800be97SKajetan Staszkiewicz
334b800be97SKajetan Staszkiewiczoverindex_cleanup()
335b800be97SKajetan Staszkiewicz{
336b800be97SKajetan Staszkiewicz	pft_cleanup
337b800be97SKajetan Staszkiewicz}
338b800be97SKajetan Staszkiewicz
339b800be97SKajetan Staszkiewiczatf_test_case "overlimit" "cleanup"
340b800be97SKajetan Staszkiewiczoverlimit_head()
341b800be97SKajetan Staszkiewicz{
342b800be97SKajetan Staszkiewicz	atf_set descr 'ping fragment at index boundary that cannot be requeued'
343b800be97SKajetan Staszkiewicz	atf_set require.user root
344*c46af893SJose Luis Duran	atf_set require.progs python3 scapy
345b800be97SKajetan Staszkiewicz}
346b800be97SKajetan Staszkiewicz
347b800be97SKajetan Staszkiewiczoverlimit_body()
348b800be97SKajetan Staszkiewicz{
349b800be97SKajetan Staszkiewicz	frag_common overlimit
350b800be97SKajetan Staszkiewicz}
351b800be97SKajetan Staszkiewicz
352b800be97SKajetan Staszkiewiczoverlimit_cleanup()
353b800be97SKajetan Staszkiewicz{
354b800be97SKajetan Staszkiewicz	pft_cleanup
355b800be97SKajetan Staszkiewicz}
356b800be97SKajetan Staszkiewicz
357db100bd9SKristof Provostatf_test_case "overhole" "cleanup"
358db100bd9SKristof Provostoverhole_head()
359db100bd9SKristof Provost{
360db100bd9SKristof Provost	atf_set descr 'ping fragment at index boundary which modifies pf hole counter'
361db100bd9SKristof Provost	atf_set require.user root
362*c46af893SJose Luis Duran	atf_set require.progs python3 scapy
363db100bd9SKristof Provost}
364db100bd9SKristof Provost
365db100bd9SKristof Provostoverhole_body()
366db100bd9SKristof Provost{
367db100bd9SKristof Provost	frag_common overhole
368db100bd9SKristof Provost}
369db100bd9SKristof Provost
370db100bd9SKristof Provostoverhole_cleanup()
371db100bd9SKristof Provost{
372db100bd9SKristof Provost	pft_cleanup
373db100bd9SKristof Provost}
374db100bd9SKristof Provost
375db100bd9SKristof Provostatf_test_case "adjhole" "cleanup"
376db100bd9SKristof Provostadjhole_head()
377db100bd9SKristof Provost{
378db100bd9SKristof Provost	atf_set descr 'overlapping ping fragments which modifies pf hole counter'
379db100bd9SKristof Provost	atf_set require.user root
380*c46af893SJose Luis Duran	atf_set require.progs python3 scapy
381db100bd9SKristof Provost}
382db100bd9SKristof Provost
383db100bd9SKristof Provostadjhole_body()
384db100bd9SKristof Provost{
385db100bd9SKristof Provost	frag_common adjhole
386db100bd9SKristof Provost}
387db100bd9SKristof Provost
388db100bd9SKristof Provostadjhole_cleanup()
389db100bd9SKristof Provost{
390db100bd9SKristof Provost	pft_cleanup
391db100bd9SKristof Provost}
392db100bd9SKristof Provost
393b800be97SKajetan Staszkiewiczatf_test_case "reassemble" "cleanup"
394b800be97SKajetan Staszkiewiczreassemble_head()
395b800be97SKajetan Staszkiewicz{
396b800be97SKajetan Staszkiewicz	atf_set descr 'Test reassembly'
397b800be97SKajetan Staszkiewicz	atf_set require.user root
398b800be97SKajetan Staszkiewicz}
399b800be97SKajetan Staszkiewicz
400b800be97SKajetan Staszkiewiczreassemble_body()
401b800be97SKajetan Staszkiewicz{
402b800be97SKajetan Staszkiewicz	pft_init
403b800be97SKajetan Staszkiewicz
404b800be97SKajetan Staszkiewicz	epair=$(vnet_mkepair)
405b800be97SKajetan Staszkiewicz	vnet_mkjail alcatraz ${epair}a
406b800be97SKajetan Staszkiewicz
407b800be97SKajetan Staszkiewicz	ifconfig ${epair}b inet 192.0.2.1/24 up
408b800be97SKajetan Staszkiewicz	jexec alcatraz ifconfig ${epair}a 192.0.2.2/24 up
409b800be97SKajetan Staszkiewicz
410b800be97SKajetan Staszkiewicz	# Sanity check
411b800be97SKajetan Staszkiewicz	atf_check -s exit:0 -o ignore ping -c 1 192.0.2.2
412b800be97SKajetan Staszkiewicz
413b800be97SKajetan Staszkiewicz	jexec alcatraz pfctl -e
414b800be97SKajetan Staszkiewicz	pft_set_rules alcatraz \
415b800be97SKajetan Staszkiewicz		"pass out" \
416b800be97SKajetan Staszkiewicz		"block in" \
417b800be97SKajetan Staszkiewicz		"pass in inet proto icmp all icmp-type echoreq"
418b800be97SKajetan Staszkiewicz
419b800be97SKajetan Staszkiewicz	# Single fragment passes
420b800be97SKajetan Staszkiewicz	atf_check -s exit:0 -o ignore ping -c 1 192.0.2.2
421b800be97SKajetan Staszkiewicz
422b800be97SKajetan Staszkiewicz	# But a fragmented ping does not
423b800be97SKajetan Staszkiewicz	atf_check -s exit:2 -o ignore ping -c 1 -s 2000 192.0.2.2
424b800be97SKajetan Staszkiewicz
425b800be97SKajetan Staszkiewicz	pft_set_rules alcatraz \
426b800be97SKajetan Staszkiewicz		"set reassemble yes" \
427b800be97SKajetan Staszkiewicz		"pass out" \
428b800be97SKajetan Staszkiewicz		"block in" \
429b800be97SKajetan Staszkiewicz		"pass in inet proto icmp all icmp-type echoreq"
430b800be97SKajetan Staszkiewicz
431b800be97SKajetan Staszkiewicz	# Both single packet & fragmented pass when we scrub
432b800be97SKajetan Staszkiewicz	atf_check -s exit:0 -o ignore ping -c 1 192.0.2.2
433b800be97SKajetan Staszkiewicz	atf_check -s exit:0 -o ignore ping -c 1 -s 2000 192.0.2.2
434b800be97SKajetan Staszkiewicz}
435b800be97SKajetan Staszkiewicz
436b800be97SKajetan Staszkiewiczreassemble_cleanup()
437b800be97SKajetan Staszkiewicz{
438b800be97SKajetan Staszkiewicz	pft_cleanup
439b800be97SKajetan Staszkiewicz}
440b800be97SKajetan Staszkiewicz
441b800be97SKajetan Staszkiewiczatf_test_case "no_df" "cleanup"
442b800be97SKajetan Staszkiewiczno_df_head()
443b800be97SKajetan Staszkiewicz{
444b800be97SKajetan Staszkiewicz	atf_set descr 'Test removing of DF flag'
445b800be97SKajetan Staszkiewicz	atf_set require.user root
446*c46af893SJose Luis Duran	atf_set require.progs python3 scapy
447b800be97SKajetan Staszkiewicz}
448b800be97SKajetan Staszkiewicz
449b800be97SKajetan Staszkiewiczno_df_body()
450b800be97SKajetan Staszkiewicz{
451b800be97SKajetan Staszkiewicz	setup_router_server_ipv4
452b800be97SKajetan Staszkiewicz
453b800be97SKajetan Staszkiewicz	# Tester can send long packets which will get fragmented by the router.
454b800be97SKajetan Staszkiewicz	# Replies from server will come in fragments which might get
455b800be97SKajetan Staszkiewicz	# reassembled resulting in a long reply packet sent back to tester.
456b800be97SKajetan Staszkiewicz	ifconfig ${epair_tester}a mtu 9000
457b800be97SKajetan Staszkiewicz	jexec router ifconfig ${epair_tester}b mtu 9000
458b800be97SKajetan Staszkiewicz	jexec router ifconfig ${epair_server}a mtu 1500
459b800be97SKajetan Staszkiewicz	jexec server ifconfig ${epair_server}b mtu 1500
460b800be97SKajetan Staszkiewicz
461b800be97SKajetan Staszkiewicz	# Sanity check.
462b800be97SKajetan Staszkiewicz	ping_server_check_reply exit:0 --ping-type=icmp
463b800be97SKajetan Staszkiewicz
464b800be97SKajetan Staszkiewicz	# Enable packet reassembly with clearing of the no-df flag.
465b800be97SKajetan Staszkiewicz	pft_set_rules router \
466b800be97SKajetan Staszkiewicz		"scrub all fragment reassemble no-df" \
467b800be97SKajetan Staszkiewicz		"block" \
468b800be97SKajetan Staszkiewicz		"pass inet proto icmp all icmp-type echoreq"
469b800be97SKajetan Staszkiewicz	# Ping with non-fragmentable packets.
470b800be97SKajetan Staszkiewicz	# pf will strip the DF flag resulting in fragmentation and packets
471b800be97SKajetan Staszkiewicz	# getting properly forwarded.
472b800be97SKajetan Staszkiewicz	ping_server_check_reply exit:0 --ping-type=icmp --send-length=2000 --send-flags DF
473b800be97SKajetan Staszkiewicz}
4743b337076SFranco Fichtner
475b800be97SKajetan Staszkiewiczno_df_cleanup()
476b800be97SKajetan Staszkiewicz{
477b800be97SKajetan Staszkiewicz	pft_cleanup
478b800be97SKajetan Staszkiewicz}
479b800be97SKajetan Staszkiewicz
480657aec45SKristof Provostatf_test_case "reassemble_slowpath" "cleanup"
481657aec45SKristof Provostreassemble_slowpath_head()
482657aec45SKristof Provost{
483657aec45SKristof Provost	atf_set descr 'Test reassembly on the slow path'
484657aec45SKristof Provost	atf_set require.user root
485*c46af893SJose Luis Duran	atf_set require.progs python3 scapy
486657aec45SKristof Provost}
487657aec45SKristof Provost
488657aec45SKristof Provostreassemble_slowpath_body()
489657aec45SKristof Provost{
490657aec45SKristof Provost	if ! sysctl -q kern.features.ipsec >/dev/null ; then
491657aec45SKristof Provost		atf_skip "This test requires ipsec"
492657aec45SKristof Provost	fi
493657aec45SKristof Provost
494657aec45SKristof Provost	setup_router_server_ipv4
495657aec45SKristof Provost
496657aec45SKristof Provost	# Now define an ipsec policy so we end up taking the slow path.
497657aec45SKristof Provost	# We don't actually need the traffic to go through ipsec, we just don't
498657aec45SKristof Provost	# want to go through ip_tryforward().
499657aec45SKristof Provost	echo "flush;
500657aec45SKristof Provost	spdflush;
501657aec45SKristof Provost	spdadd 203.0.113.1/32 203.0.113.2/32 any -P out ipsec esp/transport//require;
502657aec45SKristof Provost	add 203.0.113.1 203.0.113.2 esp 0x1001 -E aes-gcm-16 \"12345678901234567890\";" \
503657aec45SKristof Provost	    | jexec router setkey -c
504657aec45SKristof Provost
505657aec45SKristof Provost	# Sanity check.
506657aec45SKristof Provost	ping_server_check_reply exit:0 --ping-type=icmp
507657aec45SKristof Provost
508657aec45SKristof Provost	# Enable packet reassembly with clearing of the no-df flag.
509657aec45SKristof Provost	pft_set_rules router \
510657aec45SKristof Provost		"scrub in on ${epair_tester}b fragment no reassemble" \
511657aec45SKristof Provost		"scrub on ${epair_server}a fragment reassemble" \
512657aec45SKristof Provost		"pass"
513657aec45SKristof Provost
514657aec45SKristof Provost	# Ensure that the packet makes it through the slow path
515657aec45SKristof Provost	atf_check -s exit:0 -o ignore \
516657aec45SKristof Provost	    ping -c 1 -s 2000 198.51.100.2
517657aec45SKristof Provost}
518657aec45SKristof Provost
519657aec45SKristof Provostreassemble_slowpath_cleanup()
520657aec45SKristof Provost{
521657aec45SKristof Provost	pft_cleanup
522657aec45SKristof Provost}
523657aec45SKristof Provost
52424c0058aSKristof Provostatf_test_case "dummynet" "cleanup"
52524c0058aSKristof Provostdummynet_head()
52624c0058aSKristof Provost{
52724c0058aSKristof Provost	atf_set descr 'dummynet + reassembly test'
52824c0058aSKristof Provost	atf_set require.user root
52924c0058aSKristof Provost}
53024c0058aSKristof Provost
53124c0058aSKristof Provostdummynet_body()
53224c0058aSKristof Provost{
53324c0058aSKristof Provost	pft_init
53424c0058aSKristof Provost	dummynet_init
53524c0058aSKristof Provost
53624c0058aSKristof Provost	epair=$(vnet_mkepair)
53724c0058aSKristof Provost	vnet_mkjail alcatraz ${epair}a
53824c0058aSKristof Provost
53924c0058aSKristof Provost	ifconfig ${epair}b inet 192.0.2.1/24 up
54024c0058aSKristof Provost	jexec alcatraz ifconfig ${epair}a 192.0.2.2/24 up
54124c0058aSKristof Provost
54224c0058aSKristof Provost	# Sanity check
54324c0058aSKristof Provost	atf_check -s exit:0 -o ignore ping -c 1 192.0.2.2
54424c0058aSKristof Provost
54524c0058aSKristof Provost	jexec alcatraz dnctl pipe 1 config bw 600Byte/s
54624c0058aSKristof Provost	jexec alcatraz dnctl pipe 2 config bw 700Byte/s
54724c0058aSKristof Provost
54824c0058aSKristof Provost	jexec alcatraz pfctl -e
54924c0058aSKristof Provost	pft_set_rules alcatraz \
55024c0058aSKristof Provost		"set reassemble yes" \
55124c0058aSKristof Provost		"block" \
55224c0058aSKristof Provost		"pass inet proto icmp all icmp-type echoreq dnpipe (1, 2)"
55324c0058aSKristof Provost
55424c0058aSKristof Provost	atf_check -s exit:0 -o ignore ping -s 2000 -c 1 192.0.2.2
55524c0058aSKristof Provost}
55624c0058aSKristof Provost
55724c0058aSKristof Provostdummynet_cleanup()
55824c0058aSKristof Provost{
55924c0058aSKristof Provost	pft_cleanup
56024c0058aSKristof Provost}
56124c0058aSKristof Provost
5623f9a6e0bSKristof Provostatf_test_case "dummynet_nat" "cleanup"
5633f9a6e0bSKristof Provostdummynet_nat_head()
5643f9a6e0bSKristof Provost{
5653f9a6e0bSKristof Provost	atf_set descr 'Test dummynet on NATed fragmented traffic'
5663f9a6e0bSKristof Provost	atf_set require.user root
5673f9a6e0bSKristof Provost}
5683f9a6e0bSKristof Provost
5693f9a6e0bSKristof Provostdummynet_nat_body()
5703f9a6e0bSKristof Provost{
5713f9a6e0bSKristof Provost	pft_init
5723f9a6e0bSKristof Provost	dummynet_init
5733f9a6e0bSKristof Provost
5743f9a6e0bSKristof Provost	epair_one=$(vnet_mkepair)
5753f9a6e0bSKristof Provost	ifconfig ${epair_one}a 192.0.2.1/24 up
5763f9a6e0bSKristof Provost
5773f9a6e0bSKristof Provost	epair_two=$(vnet_mkepair)
5783f9a6e0bSKristof Provost
5793f9a6e0bSKristof Provost	vnet_mkjail alcatraz ${epair_one}b ${epair_two}a
5803f9a6e0bSKristof Provost	jexec alcatraz ifconfig ${epair_one}b 192.0.2.2/24 up
5813f9a6e0bSKristof Provost	jexec alcatraz ifconfig ${epair_two}a 198.51.100.1/24 up
5823f9a6e0bSKristof Provost	jexec alcatraz sysctl net.inet.ip.forwarding=1
5833f9a6e0bSKristof Provost
5843f9a6e0bSKristof Provost	vnet_mkjail singsing ${epair_two}b
5853f9a6e0bSKristof Provost	jexec singsing ifconfig ${epair_two}b 198.51.100.2/24 up
5863f9a6e0bSKristof Provost	jexec singsing route add default 198.51.100.1
5873f9a6e0bSKristof Provost
5883f9a6e0bSKristof Provost	route add 198.51.100.0/24 192.0.2.2
5893f9a6e0bSKristof Provost
5903f9a6e0bSKristof Provost	jexec alcatraz dnctl pipe 1 config bw 1600Byte/s
5913f9a6e0bSKristof Provost	jexec alcatraz dnctl pipe 2 config bw 1700Byte/s
5923f9a6e0bSKristof Provost
5933f9a6e0bSKristof Provost	jexec alcatraz pfctl -e
5943f9a6e0bSKristof Provost	pft_set_rules alcatraz \
5953f9a6e0bSKristof Provost		"set reassemble yes" \
5963f9a6e0bSKristof Provost		"nat on ${epair_two}a from 192.0.2.0/24 -> (${epair_two}a)" \
5973f9a6e0bSKristof Provost		"block in" \
5983f9a6e0bSKristof Provost		"pass in inet proto icmp all icmp-type echoreq dnpipe (1, 2)"
5993f9a6e0bSKristof Provost
6003f9a6e0bSKristof Provost	atf_check -s exit:0 -o ignore ping -c 1 198.51.100.2
6013f9a6e0bSKristof Provost	atf_check -s exit:0 -o ignore ping -c 1 -s 2000 198.51.100.2
6023f9a6e0bSKristof Provost}
6033f9a6e0bSKristof Provost
6043f9a6e0bSKristof Provostdummynet_nat_cleanup()
6053f9a6e0bSKristof Provost{
6063f9a6e0bSKristof Provost	pft_cleanup
6073f9a6e0bSKristof Provost}
6083f9a6e0bSKristof Provost
60965074f6fSKajetan Staszkiewiczatf_test_case "dummynet_fragmented" "cleanup"
61065074f6fSKajetan Staszkiewiczdummynet_fragmented_head()
61165074f6fSKajetan Staszkiewicz{
61265074f6fSKajetan Staszkiewicz	atf_set descr 'Test dummynet on NATed fragmented traffic'
61365074f6fSKajetan Staszkiewicz	atf_set require.user root
614*c46af893SJose Luis Duran	atf_set require.progs python3 scapy
61565074f6fSKajetan Staszkiewicz}
61665074f6fSKajetan Staszkiewicz
61765074f6fSKajetan Staszkiewiczdummynet_fragmented_body()
61865074f6fSKajetan Staszkiewicz{
61965074f6fSKajetan Staszkiewicz	pft_init
62065074f6fSKajetan Staszkiewicz	dummynet_init
62165074f6fSKajetan Staszkiewicz
62265074f6fSKajetan Staszkiewicz	# No test for IPv6. IPv6 fragment reassembly can't be disabled.
62365074f6fSKajetan Staszkiewicz	setup_router_dummy_ipv4
62465074f6fSKajetan Staszkiewicz
62565074f6fSKajetan Staszkiewicz	jexec router dnctl pipe 1 config delay 1
62665074f6fSKajetan Staszkiewicz
62765074f6fSKajetan Staszkiewicz	pft_set_rules router \
62865074f6fSKajetan Staszkiewicz		"set reassemble no" \
62965074f6fSKajetan Staszkiewicz		"block" \
63065074f6fSKajetan Staszkiewicz		"pass inet6 proto icmp6 icmp6-type { neighbrsol, neighbradv }" \
63165074f6fSKajetan Staszkiewicz		"pass in  on ${epair_tester}b inet  proto udp dnpipe (1, 1)" \
63265074f6fSKajetan Staszkiewicz		"pass out on ${epair_server}a inet  proto udp" \
63365074f6fSKajetan Staszkiewicz
63465074f6fSKajetan Staszkiewicz	ping_dummy_check_request exit:0 --ping-type=udp --send-length=10000 --send-frag-length=1280
63565074f6fSKajetan Staszkiewicz
63665074f6fSKajetan Staszkiewicz	rules=$(mktemp) || exit 1
63765b20771SKajetan Staszkiewicz	jexec router pfctl -qvsr | normalize_pfctl_s > $rules
63865074f6fSKajetan Staszkiewicz
63965074f6fSKajetan Staszkiewicz	# Count that fragmented packets have hit the rule only once and that
64065074f6fSKajetan Staszkiewicz	# they have not created states. There is no stateful firewall support
64165074f6fSKajetan Staszkiewicz	# for fragmented packets.
64265b20771SKajetan Staszkiewicz	grep -qE 'pass in on epair0b inet proto udp all keep state dnpipe\(1, 1\) .* Packets: 8 Bytes: 10168 States: 0 ' $rules ||
64365074f6fSKajetan Staszkiewicz		atf_fail "Fragmented packets not counted correctly"
64465074f6fSKajetan Staszkiewicz}
64565074f6fSKajetan Staszkiewicz
64665074f6fSKajetan Staszkiewiczdummynet_fragmented_cleanup()
64765074f6fSKajetan Staszkiewicz{
64865074f6fSKajetan Staszkiewicz	pft_cleanup
64965074f6fSKajetan Staszkiewicz}
65065074f6fSKajetan Staszkiewicz
651b800be97SKajetan Staszkiewiczatf_init_test_cases()
652b800be97SKajetan Staszkiewicz{
653b800be97SKajetan Staszkiewicz	atf_add_test_case "too_many_fragments"
654b800be97SKajetan Staszkiewicz	atf_add_test_case "v6"
65556b7685aSKristof Provost	atf_add_test_case "v6_route_to"
656b800be97SKajetan Staszkiewicz	atf_add_test_case "mtu_diff"
657b800be97SKajetan Staszkiewicz	atf_add_test_case "overreplace"
658b800be97SKajetan Staszkiewicz	atf_add_test_case "overindex"
659b800be97SKajetan Staszkiewicz	atf_add_test_case "overlimit"
660db100bd9SKristof Provost	atf_add_test_case "overhole"
661db100bd9SKristof Provost	atf_add_test_case "adjhole"
662b800be97SKajetan Staszkiewicz	atf_add_test_case "reassemble"
663b800be97SKajetan Staszkiewicz	atf_add_test_case "no_df"
664657aec45SKristof Provost	atf_add_test_case "reassemble_slowpath"
66524c0058aSKristof Provost	atf_add_test_case "dummynet"
6663f9a6e0bSKristof Provost	atf_add_test_case "dummynet_nat"
66765074f6fSKajetan Staszkiewicz	atf_add_test_case "dummynet_fragmented"
668b800be97SKajetan Staszkiewicz}
669