xref: /freebsd/tests/sys/netpfil/pf/route_to.sh (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1#
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2018 Kristof Provost <kp@FreeBSD.org>
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
29common_dir=$(atf_get_srcdir)/../common
30
31atf_test_case "v4" "cleanup"
32v4_head()
33{
34	atf_set descr 'Basic route-to test'
35	atf_set require.user root
36}
37
38v4_body()
39{
40	pft_init
41
42	epair_send=$(vnet_mkepair)
43	ifconfig ${epair_send}a 192.0.2.1/24 up
44	epair_route=$(vnet_mkepair)
45	ifconfig ${epair_route}a 203.0.113.1/24 up
46
47	vnet_mkjail alcatraz ${epair_send}b ${epair_route}b
48	jexec alcatraz ifconfig ${epair_send}b 192.0.2.2/24 up
49	jexec alcatraz ifconfig ${epair_route}b 203.0.113.2/24 up
50	jexec alcatraz route add -net 198.51.100.0/24 192.0.2.1
51	jexec alcatraz pfctl -e
52
53	# Attempt to provoke PR 228782
54	pft_set_rules alcatraz "block all" "pass user 2" \
55		"pass out route-to (${epair_route}b 203.0.113.1) from 192.0.2.2 to 198.51.100.1 no state"
56	jexec alcatraz nc -w 3 -s 192.0.2.2 198.51.100.1 22
57
58	# atf wants us to not return an error, but our netcat will fail
59	true
60}
61
62v4_cleanup()
63{
64	pft_cleanup
65}
66
67atf_test_case "v6" "cleanup"
68v6_head()
69{
70	atf_set descr 'Basic route-to test (IPv6)'
71	atf_set require.user root
72}
73
74v6_body()
75{
76	pft_init
77
78	epair_send=$(vnet_mkepair)
79	ifconfig ${epair_send}a inet6 2001:db8:42::1/64 up no_dad -ifdisabled
80	epair_route=$(vnet_mkepair)
81	ifconfig ${epair_route}a inet6 2001:db8:43::1/64 up no_dad -ifdisabled
82
83	vnet_mkjail alcatraz ${epair_send}b ${epair_route}b
84	jexec alcatraz ifconfig ${epair_send}b inet6 2001:db8:42::2/64 up no_dad
85	jexec alcatraz ifconfig ${epair_route}b inet6 2001:db8:43::2/64 up no_dad
86	jexec alcatraz route add -6 2001:db8:666::/64 2001:db8:42::2
87	jexec alcatraz pfctl -e
88
89	# Attempt to provoke PR 228782
90	pft_set_rules alcatraz "block all" "pass user 2" \
91		"pass out route-to (${epair_route}b 2001:db8:43::1) from 2001:db8:42::2 to 2001:db8:666::1 no state"
92	jexec alcatraz nc -6 -w 3 -s 2001:db8:42::2 2001:db8:666::1 22
93
94	# atf wants us to not return an error, but our netcat will fail
95	true
96}
97
98v6_cleanup()
99{
100	pft_cleanup
101}
102
103atf_test_case "multiwan" "cleanup"
104multiwan_head()
105{
106	atf_set descr 'Multi-WAN redirection / reply-to test'
107	atf_set require.user root
108}
109
110multiwan_body()
111{
112	pft_init
113
114	epair_one=$(vnet_mkepair)
115	epair_two=$(vnet_mkepair)
116	epair_cl_one=$(vnet_mkepair)
117	epair_cl_two=$(vnet_mkepair)
118
119	vnet_mkjail srv ${epair_one}b ${epair_two}b
120	vnet_mkjail wan_one ${epair_one}a ${epair_cl_one}b
121	vnet_mkjail wan_two ${epair_two}a ${epair_cl_two}b
122	vnet_mkjail client ${epair_cl_one}a ${epair_cl_two}a
123
124	jexec client ifconfig ${epair_cl_one}a 203.0.113.1/25
125	jexec wan_one ifconfig ${epair_cl_one}b 203.0.113.2/25
126	jexec wan_one ifconfig ${epair_one}a 192.0.2.1/24 up
127	jexec wan_one sysctl net.inet.ip.forwarding=1
128	jexec srv ifconfig ${epair_one}b 192.0.2.2/24 up
129	jexec client route add 192.0.2.0/24 203.0.113.2
130
131	jexec client ifconfig ${epair_cl_two}a 203.0.113.128/25
132	jexec wan_two ifconfig ${epair_cl_two}b 203.0.113.129/25
133	jexec wan_two ifconfig ${epair_two}a 198.51.100.1/24 up
134	jexec wan_two sysctl net.inet.ip.forwarding=1
135	jexec srv ifconfig ${epair_two}b 198.51.100.2/24 up
136	jexec client route add 198.51.100.0/24 203.0.113.129
137
138	jexec srv ifconfig lo0 127.0.0.1/8 up
139	jexec srv route add default 192.0.2.1
140	jexec srv sysctl net.inet.ip.forwarding=1
141
142	# Run echo server in srv jail
143	jexec srv /usr/sbin/inetd -p multiwan.pid $(atf_get_srcdir)/echo_inetd.conf
144
145	jexec srv pfctl -e
146	pft_set_rules srv \
147		"nat on ${epair_one}b inet from 127.0.0.0/8 to any -> (${epair_one}b)" \
148		"nat on ${epair_two}b inet from 127.0.0.0/8 to any -> (${epair_two}b)" \
149		"rdr on ${epair_one}b inet proto tcp from any to 192.0.2.2 port 7 -> 127.0.0.1 port 7" \
150		"rdr on ${epair_two}b inet proto tcp from any to 198.51.100.2 port 7 -> 127.0.0.1 port 7" \
151		"block in"	\
152		"block out"	\
153		"pass in quick on ${epair_one}b reply-to (${epair_one}b 192.0.2.1) inet proto tcp from any to 127.0.0.1 port 7" \
154		"pass in quick on ${epair_two}b reply-to (${epair_two}b 198.51.100.1) inet proto tcp from any to 127.0.0.1 port 7"
155
156	# These will always succeed, because we don't change interface to route
157	# correctly here.
158	result=$(echo "one" | jexec wan_one nc -N -w 3 192.0.2.2 7)
159	if [ "${result}" != "one" ]; then
160		atf_fail "Redirect on one failed"
161	fi
162	result=$(echo "two" | jexec wan_two nc -N -w 3 198.51.100.2 7)
163	if [ "${result}" != "two" ]; then
164		atf_fail "Redirect on two failed"
165	fi
166
167	result=$(echo "one" | jexec client nc -N -w 3 192.0.2.2 7)
168	if [ "${result}" != "one" ]; then
169		atf_fail "Redirect from client on one failed"
170	fi
171
172	# This should trigger the issue fixed in 829a69db855b48ff7e8242b95e193a0783c489d9
173	result=$(echo "two" | jexec client nc -N -w 3 198.51.100.2 7)
174	if [ "${result}" != "two" ]; then
175		atf_fail "Redirect from client on two failed"
176	fi
177}
178
179multiwan_cleanup()
180{
181	rm -f multiwan.pid
182	pft_cleanup
183}
184
185atf_test_case "multiwanlocal" "cleanup"
186multiwanlocal_head()
187{
188	atf_set descr 'Multi-WAN local origin source-based redirection / route-to test'
189	atf_set require.user root
190}
191
192multiwanlocal_body()
193{
194	pft_init
195
196	epair_one=$(vnet_mkepair)
197	epair_two=$(vnet_mkepair)
198	epair_cl_one=$(vnet_mkepair)
199	epair_cl_two=$(vnet_mkepair)
200
201	vnet_mkjail srv1 ${epair_one}b
202	vnet_mkjail srv2 ${epair_two}b
203	vnet_mkjail wan_one ${epair_one}a ${epair_cl_one}b
204	vnet_mkjail wan_two ${epair_two}a ${epair_cl_two}b
205	vnet_mkjail client ${epair_cl_one}a ${epair_cl_two}a
206
207	jexec client ifconfig ${epair_cl_one}a 203.0.113.1/25
208	jexec wan_one ifconfig ${epair_cl_one}b 203.0.113.2/25
209	jexec wan_one ifconfig ${epair_one}a 192.0.2.1/24 up
210	jexec wan_one sysctl net.inet.ip.forwarding=1
211	jexec srv1 ifconfig ${epair_one}b 192.0.2.2/24 up
212
213	jexec client ifconfig ${epair_cl_two}a 203.0.113.128/25
214	jexec wan_two ifconfig ${epair_cl_two}b 203.0.113.129/25
215	jexec wan_two ifconfig ${epair_two}a 198.51.100.1/24 up
216	jexec wan_two sysctl net.inet.ip.forwarding=1
217	jexec srv2 ifconfig ${epair_two}b 198.51.100.2/24 up
218
219	jexec client route add default 203.0.113.2
220	jexec srv1 route add default 192.0.2.1
221	jexec srv2 route add default 198.51.100.1
222
223	# Run data source in srv1 and srv2
224	jexec srv1 sh -c 'dd if=/dev/zero bs=1024 count=100 | nc -l 7 -w 2 -N &'
225	jexec srv2 sh -c 'dd if=/dev/zero bs=1024 count=100 | nc -l 7 -w 2 -N &'
226
227	jexec client pfctl -e
228	pft_set_rules client \
229		"block in"	\
230		"block out"	\
231		"pass out quick route-to (${epair_cl_two}a 203.0.113.129) inet proto tcp from 203.0.113.128 to any port 7" \
232		"pass out on ${epair_cl_one}a inet proto tcp from any to any port 7" \
233		"set skip on lo"
234
235	# This should work
236	result=$(jexec client nc -N -w 1 192.0.2.2 7 | wc -c)
237	if [ ${result} -ne 102400 ]; then
238		jexec client pfctl -ss
239		atf_fail "Redirect from client on one failed: ${result}"
240	fi
241
242	# This should trigger the issue
243	result=$(jexec client nc -N -w 1 -s 203.0.113.128 198.51.100.2 7 | wc -c)
244	jexec client pfctl -ss
245	if [ ${result} -ne 102400 ]; then
246		atf_fail "Redirect from client on two failed: ${result}"
247	fi
248}
249
250multiwanlocal_cleanup()
251{
252	pft_cleanup
253}
254
255atf_test_case "icmp_nat" "cleanup"
256icmp_nat_head()
257{
258	atf_set descr 'Test that ICMP packets are correct for route-to + NAT'
259	atf_set require.user root
260	atf_set require.progs scapy
261}
262
263icmp_nat_body()
264{
265	pft_init
266
267	epair_one=$(vnet_mkepair)
268	epair_two=$(vnet_mkepair)
269	epair_three=$(vnet_mkepair)
270
271	vnet_mkjail gw ${epair_one}b ${epair_two}a ${epair_three}a
272	vnet_mkjail srv ${epair_two}b
273	vnet_mkjail srv2 ${epair_three}b
274
275	ifconfig ${epair_one}a 192.0.2.2/24 up
276	route add -net 198.51.100.0/24 192.0.2.1
277	jexec gw sysctl net.inet.ip.forwarding=1
278	jexec gw ifconfig ${epair_one}b 192.0.2.1/24 up
279	jexec gw ifconfig ${epair_two}a 198.51.100.1/24 up
280	jexec gw ifconfig ${epair_three}a 203.0.113.1/24 up mtu 500
281	jexec srv ifconfig ${epair_two}b 198.51.100.2/24 up
282	jexec srv route add default 198.51.100.1
283	jexec srv2 ifconfig ${epair_three}b 203.0.113.2/24 up mtu 500
284	jexec srv2 route add default 203.0.113.1
285
286	# Sanity check
287	atf_check -s exit:0 -o ignore ping -c 1 198.51.100.2
288
289	jexec gw pfctl -e
290	pft_set_rules gw \
291		"nat on ${epair_two}a inet from 192.0.2.0/24 to any -> (${epair_two}a)" \
292		"nat on ${epair_three}a inet from 192.0.2.0/24 to any -> (${epair_three}a)" \
293		"pass out route-to (${epair_three}a 203.0.113.2) proto icmp icmp-type echoreq"
294
295	# Now ensure that we get an ICMP error with the correct IP addresses in it.
296	atf_check -s exit:0 ${common_dir}/pft_icmp_check.py \
297		--to 198.51.100.2 \
298		--fromaddr 192.0.2.2 \
299		--recvif ${epair_one}a \
300		--sendif ${epair_one}a
301
302	# ping reports the ICMP error, so check of that too.
303	atf_check -s exit:2 -o match:'frag needed and DF set' \
304		ping -D -c 1 -s 1000 198.51.100.2
305}
306
307icmp_nat_cleanup()
308{
309	pft_cleanup
310}
311
312atf_test_case "dummynet" "cleanup"
313dummynet_head()
314{
315	atf_set descr 'Test that dummynet applies to route-to packets'
316	atf_set require.user root
317}
318
319dummynet_body()
320{
321	dummynet_init
322
323	epair_srv=$(vnet_mkepair)
324	epair_gw=$(vnet_mkepair)
325
326	vnet_mkjail srv ${epair_srv}a
327	jexec srv ifconfig ${epair_srv}a 192.0.2.1/24 up
328	jexec srv route add default 192.0.2.2
329
330	vnet_mkjail gw ${epair_srv}b ${epair_gw}a
331	jexec gw ifconfig ${epair_srv}b 192.0.2.2/24 up
332	jexec gw ifconfig ${epair_gw}a 198.51.100.1/24 up
333	jexec gw sysctl net.inet.ip.forwarding=1
334
335	ifconfig ${epair_gw}b 198.51.100.2/24 up
336	route add -net 192.0.2.0/24 198.51.100.1
337
338	# Sanity check
339	atf_check -s exit:0 -o ignore ping -c 1 -t 1 192.0.2.1
340
341	jexec gw dnctl pipe 1 config delay 1200
342	pft_set_rules gw \
343		"pass out route-to (${epair_srv}b 192.0.2.1) to 192.0.2.1 dnpipe 1"
344	jexec gw pfctl -e
345
346	# The ping request will pass, but take 1.2 seconds
347	# So this works:
348	atf_check -s exit:0 -o ignore ping -c 1 -t 2 192.0.2.1
349	# But this times out:
350	atf_check -s exit:2 -o ignore ping -c 1 -t 1 192.0.2.1
351
352	# return path dummynet
353	pft_set_rules gw \
354		"pass out route-to (${epair_srv}b 192.0.2.1) to 192.0.2.1 dnpipe (0, 1)"
355
356	# The ping request will pass, but take 1.2 seconds
357	# So this works:
358	atf_check -s exit:0 -o ignore ping -c 1 -t 2 192.0.2.1
359	# But this times out:
360	atf_check -s exit:2 -o ignore ping -c 1 -t 1 192.0.2.1
361}
362
363dummynet_cleanup()
364{
365	pft_cleanup
366}
367
368atf_test_case "dummynet_in" "cleanup"
369dummynet_in_head()
370{
371	atf_set descr 'Thest that dummynet works as expected on pass in route-to packets'
372	atf_set require.user root
373}
374
375dummynet_in_body()
376{
377	dummynet_init
378
379	epair_srv=$(vnet_mkepair)
380	epair_gw=$(vnet_mkepair)
381
382	vnet_mkjail srv ${epair_srv}a
383	jexec srv ifconfig ${epair_srv}a 192.0.2.1/24 up
384	jexec srv route add default 192.0.2.2
385
386	vnet_mkjail gw ${epair_srv}b ${epair_gw}a
387	jexec gw ifconfig ${epair_srv}b 192.0.2.2/24 up
388	jexec gw ifconfig ${epair_gw}a 198.51.100.1/24 up
389	jexec gw sysctl net.inet.ip.forwarding=1
390
391	ifconfig ${epair_gw}b 198.51.100.2/24 up
392	route add -net 192.0.2.0/24 198.51.100.1
393
394	# Sanity check
395	atf_check -s exit:0 -o ignore ping -c 1 -t 1 192.0.2.1
396
397	jexec gw dnctl pipe 1 config delay 1200
398	pft_set_rules gw \
399		"pass in route-to (${epair_srv}b 192.0.2.1) to 192.0.2.1 dnpipe 1"
400	jexec gw pfctl -e
401
402	# The ping request will pass, but take 1.2 seconds
403	# So this works:
404	echo "Expect 1.2 s"
405	ping -c 1 192.0.2.1
406	atf_check -s exit:0 -o ignore ping -c 1 -t 2 192.0.2.1
407	# But this times out:
408	atf_check -s exit:2 -o ignore ping -c 1 -t 1 192.0.2.1
409
410	# return path dummynet
411	pft_set_rules gw \
412		"pass in route-to (${epair_srv}b 192.0.2.1) to 192.0.2.1 dnpipe (0, 1)"
413
414	# The ping request will pass, but take 1.2 seconds
415	# So this works:
416	echo "Expect 1.2 s"
417	ping -c 1 192.0.2.1
418	atf_check -s exit:0 -o ignore ping -c 1 -t 2 192.0.2.1
419	# But this times out:
420	atf_check -s exit:2 -o ignore ping -c 1 -t 1 192.0.2.1
421}
422
423dummynet_in_cleanup()
424{
425	pft_cleanup
426}
427
428atf_test_case "ifbound" "cleanup"
429ifbound_head()
430{
431	atf_set descr 'Test that route-to states bind the expected interface'
432	atf_set require.user root
433}
434
435ifbound_body()
436{
437	pft_init
438
439	j="route_to:ifbound"
440
441	epair_one=$(vnet_mkepair)
442	epair_two=$(vnet_mkepair)
443	ifconfig ${epair_one}b up
444
445	vnet_mkjail ${j}2 ${epair_two}b
446	jexec ${j}2 ifconfig ${epair_two}b inet 198.51.100.2/24 up
447	jexec ${j}2 ifconfig ${epair_two}b inet alias 203.0.113.1/24
448	jexec ${j}2 route add default 198.51.100.1
449
450	vnet_mkjail $j ${epair_one}a ${epair_two}a
451	jexec $j ifconfig ${epair_one}a 192.0.2.1/24 up
452	jexec $j ifconfig ${epair_two}a 198.51.100.1/24 up
453	jexec $j route add default 192.0.2.2
454
455	jexec $j pfctl -e
456	pft_set_rules $j \
457		"set state-policy if-bound" \
458		"block" \
459		"pass out route-to (${epair_two}a 198.51.100.2)"
460
461	atf_check -s exit:0 -o ignore \
462	    jexec $j ping -c 3 203.0.113.1
463}
464
465ifbound_cleanup()
466{
467	pft_cleanup
468}
469
470atf_test_case "ifbound_v6" "cleanup"
471ifbound_v6_head()
472{
473	atf_set descr 'Test that route-to states for IPv6 bind the expected interface'
474	atf_set require.user root
475}
476
477ifbound_v6_body()
478{
479	pft_init
480
481	j="route_to:ifbound_v6"
482
483	epair_one=$(vnet_mkepair)
484	epair_two=$(vnet_mkepair)
485	ifconfig ${epair_one}b up
486
487	vnet_mkjail ${j}2 ${epair_two}b
488	jexec ${j}2 ifconfig ${epair_two}b inet6 2001:db8:1::2/64 up no_dad
489	jexec ${j}2 ifconfig ${epair_two}b inet6 alias 2001:db8:2::1/64 no_dad
490	jexec ${j}2 route -6 add default 2001:db8:1::1
491
492	vnet_mkjail $j ${epair_one}a ${epair_two}a
493	jexec $j ifconfig ${epair_one}a inet6 2001:db8::1/64 up no_dad
494	jexec $j ifconfig ${epair_two}a inet6 2001:db8:1::1/64 up no_dad
495	jexec $j route -6 add default 2001:db8::2
496
497	jexec $j ping6 -c 3 2001:db8:1::2
498
499	jexec $j pfctl -e
500	pft_set_rules $j \
501		"set state-policy if-bound" \
502		"block" \
503		"pass inet6 proto icmp6 icmp6-type { neighbrsol, neighbradv }" \
504		"pass out route-to (${epair_two}a 2001:db8:1::2)"
505
506	atf_check -s exit:0 -o ignore \
507	    jexec $j ping6 -c 3 2001:db8:2::1
508}
509
510ifbound_v6_cleanup()
511{
512	pft_cleanup
513}
514
515atf_test_case "ifbound_reply_to" "cleanup"
516ifbound_reply_to_head()
517{
518	atf_set descr 'Test that reply-to states bind to the expected interface'
519	atf_set require.user root
520}
521
522ifbound_reply_to_body()
523{
524	pft_init
525
526	j="route_to:ifbound_reply_to"
527
528	epair_one=$(vnet_mkepair)
529	epair_two=$(vnet_mkepair)
530	ifconfig ${epair_one}b inet 192.0.2.2/24 up
531	ifconfig ${epair_two}b up
532
533	vnet_mkjail $j ${epair_one}a ${epair_two}a
534	jexec $j ifconfig ${epair_one}a 192.0.2.1/24 up
535	jexec $j ifconfig ${epair_two}a 198.51.100.1/24 up
536	jexec $j route add default 198.51.100.254
537
538	jexec $j pfctl -e
539	pft_set_rules $j \
540		"set state-policy if-bound" \
541		"block" \
542		"pass in on ${epair_one}a reply-to (${epair_one}a 192.0.2.2) inet from any to 192.0.2.0/24 keep state"
543
544	atf_check -s exit:0 -o ignore \
545	    ping -c 3 192.0.2.1
546
547	atf_check -s exit:0 \
548	    ${common_dir}/pft_ping.py \
549	    --to 192.0.2.1 \
550	    --from 203.0.113.2 \
551	    --sendif ${epair_one}b \
552	    --replyif ${epair_one}b
553
554	# pft_ping uses the same ID every time, so this will look like more traffic in the same state
555	atf_check -s exit:0 \
556	    ${common_dir}/pft_ping.py \
557	    --to 192.0.2.1 \
558	    --from 203.0.113.2 \
559	    --sendif ${epair_one}b \
560	    --replyif ${epair_one}b
561
562	jexec $j pfctl -ss -vv
563}
564
565ifbound_reply_to_cleanup()
566{
567	pft_cleanup
568}
569
570atf_test_case "ifbound_reply_to_v6" "cleanup"
571ifbound_reply_to_v6_head()
572{
573	atf_set descr 'Test that reply-to states bind to the expected interface for IPv6'
574	atf_set require.user root
575}
576
577ifbound_reply_to_v6_body()
578{
579	pft_init
580
581	j="route_to:ifbound_reply_to_v6"
582
583	epair_one=$(vnet_mkepair)
584	epair_two=$(vnet_mkepair)
585
586	vnet_mkjail ${j}s ${epair_one}b ${epair_two}b
587	jexec ${j}s ifconfig ${epair_one}b inet6 2001:db8::2/64 up no_dad
588	jexec ${j}s ifconfig ${epair_two}b up
589	#jexec ${j}s route -6 add default 2001:db8::1
590
591	vnet_mkjail $j ${epair_one}a ${epair_two}a
592	jexec $j ifconfig ${epair_one}a inet6 2001:db8::1/64 up no_dad
593	jexec $j ifconfig ${epair_two}a inet6 2001:db8:1::1/64 up no_dad
594	jexec $j route -6 add default 2001:db8:1::254
595
596	jexec $j pfctl -e
597	pft_set_rules $j \
598		"set state-policy if-bound" \
599		"block" \
600		"pass quick inet6 proto icmp6 icmp6-type { neighbrsol, neighbradv }" \
601		"pass in on ${epair_one}a reply-to (${epair_one}a 2001:db8::2) inet6 from any to 2001:db8::/64 keep state"
602
603	atf_check -s exit:0 -o ignore \
604	    jexec ${j}s ping6 -c 3 2001:db8::1
605
606	atf_check -s exit:0 \
607	    jexec ${j}s ${common_dir}/pft_ping.py \
608	    --to 2001:db8::1 \
609	    --from 2001:db8:2::2 \
610	    --sendif ${epair_one}b \
611	    --replyif ${epair_one}b
612
613	# pft_ping uses the same ID every time, so this will look like more traffic in the same state
614	atf_check -s exit:0 \
615	    jexec ${j}s ${common_dir}/pft_ping.py \
616	    --to 2001:db8::1 \
617	    --from 2001:db8:2::2 \
618	    --sendif ${epair_one}b \
619	    --replyif ${epair_one}b
620
621	jexec $j pfctl -ss -vv
622}
623
624ifbound_reply_to_v6_cleanup()
625{
626	pft_cleanup
627}
628
629atf_test_case "ifbound_reply_to_rdr_dummynet" "cleanup"
630ifbound_reply_to_rdr_dummynet_head()
631{
632	atf_set descr 'Test that reply-to states bind to the expected non-default-route interface after rdr and dummynet'
633	atf_set require.user root
634}
635
636ifbound_reply_to_rdr_dummynet_body()
637{
638	dummynet_init
639
640	j="route_to:ifbound_reply_to_rdr_dummynet"
641
642	epair_one=$(vnet_mkepair)
643	epair_two=$(vnet_mkepair)
644	ifconfig ${epair_one}b inet 192.0.2.2/24 up
645	ifconfig ${epair_two}b up
646
647	vnet_mkjail $j ${epair_one}a ${epair_two}a
648	jexec $j ifconfig lo0 inet 127.0.0.1/8 up
649	jexec $j ifconfig ${epair_one}a 192.0.2.1/24 up
650	jexec $j ifconfig ${epair_two}a 198.51.100.1/24 up
651	jexec $j route add default 198.51.100.254
652
653	jexec $j pfctl -e
654	jexec $j dnctl pipe 1 config delay 1
655	pft_set_rules $j \
656		"set state-policy if-bound" \
657		"rdr on ${epair_one}a proto icmp from any to 192.0.2.1 -> 127.0.0.1" \
658		"rdr on ${epair_two}a proto icmp from any to 198.51.100.1 -> 127.0.0.1" \
659		"match in on ${epair_one}a inet all dnpipe (1, 1)" \
660		"pass in on ${epair_one}a reply-to (${epair_one}a 192.0.2.2) inet from any to 127.0.0.1 keep state"
661
662	atf_check -s exit:0 -o ignore \
663	    ping -c 3 192.0.2.1
664
665	atf_check -s exit:0 \
666	    ${common_dir}/pft_ping.py \
667	    --to 192.0.2.1 \
668	    --from 203.0.113.2 \
669	    --sendif ${epair_one}b \
670	    --replyif ${epair_one}b
671
672	# pft_ping uses the same ID every time, so this will look like more traffic in the same state
673	atf_check -s exit:0 \
674	    ${common_dir}/pft_ping.py \
675	    --to 192.0.2.1 \
676	    --from 203.0.113.2 \
677	    --sendif ${epair_one}b \
678	    --replyif ${epair_one}b
679
680	jexec $j pfctl -sr -vv
681	jexec $j pfctl -ss -vv
682}
683
684ifbound_reply_to_rdr_dummynet_cleanup()
685{
686	pft_cleanup
687}
688
689atf_test_case "dummynet_frag" "cleanup"
690dummynet_frag_head()
691{
692	atf_set descr 'Test fragmentation with route-to and dummynet'
693	atf_set require.user root
694}
695
696dummynet_frag_body()
697{
698	pft_init
699	dummynet_init
700
701	epair_one=$(vnet_mkepair)
702	epair_two=$(vnet_mkepair)
703
704	ifconfig ${epair_one}a 192.0.2.1/24 up
705
706	vnet_mkjail alcatraz ${epair_one}b ${epair_two}a
707	jexec alcatraz ifconfig ${epair_one}b 192.0.2.2/24 up
708	jexec alcatraz ifconfig ${epair_two}a 198.51.100.1/24 up
709	jexec alcatraz sysctl net.inet.ip.forwarding=1
710
711	vnet_mkjail singsing ${epair_two}b
712	jexec singsing ifconfig ${epair_two}b 198.51.100.2/24 up
713	jexec singsing route add default 198.51.100.1
714
715	route add 198.51.100.0/24 192.0.2.2
716
717	jexec alcatraz dnctl pipe 1 config bw 1000Byte/s burst 4500
718	jexec alcatraz dnctl pipe 2 config
719	# This second pipe ensures that the pf_test(PF_OUT) call in pf_route() doesn't
720	# delay packets in dummynet (by inheriting pipe 1 from the input rule).
721
722	jexec alcatraz pfctl -e
723	pft_set_rules alcatraz \
724		"set reassemble yes" \
725		"pass in route-to (${epair_two}a 198.51.100.2) inet proto icmp all icmp-type echoreq dnpipe 1" \
726		"pass out dnpipe 2"
727
728
729	atf_check -s exit:0 -o ignore ping -c 1 198.51.100.2
730	atf_check -s exit:0 -o ignore ping -c 1 -s 4000 198.51.100.2
731}
732
733dummynet_frag_cleanup()
734{
735	pft_cleanup
736}
737
738atf_test_case "dummynet_double" "cleanup"
739dummynet_double_head()
740{
741	atf_set descr 'Ensure dummynet is not applied multiple times'
742	atf_set require.user root
743}
744
745dummynet_double_body()
746{
747	pft_init
748	dummynet_init
749
750	epair_one=$(vnet_mkepair)
751	epair_two=$(vnet_mkepair)
752
753	ifconfig ${epair_one}a 192.0.2.1/24 up
754
755	vnet_mkjail alcatraz ${epair_one}b ${epair_two}a
756	jexec alcatraz ifconfig ${epair_one}b 192.0.2.2/24 up
757	jexec alcatraz ifconfig ${epair_two}a 198.51.100.1/24 up
758	jexec alcatraz sysctl net.inet.ip.forwarding=1
759
760	vnet_mkjail singsing ${epair_two}b
761	jexec singsing ifconfig ${epair_two}b 198.51.100.2/24 up
762	jexec singsing route add default 198.51.100.1
763
764	route add 198.51.100.0/24 192.0.2.2
765
766	jexec alcatraz dnctl pipe 1 config delay 800
767
768	jexec alcatraz pfctl -e
769	pft_set_rules alcatraz \
770		"set reassemble yes" \
771		"nat on ${epair_two}a from 192.0.2.0/24 -> (${epair_two}a)" \
772		"pass in route-to (${epair_two}a 198.51.100.2) inet proto icmp all icmp-type echoreq dnpipe (1, 1)" \
773		"pass out route-to (${epair_two}a 198.51.100.2) inet proto icmp all icmp-type echoreq"
774
775	ping -c 1 198.51.100.2
776	jexec alcatraz pfctl -sr -vv
777	jexec alcatraz pfctl -ss -vv
778
779	# We expect to be delayed 1.6 seconds, so timeout of two seconds passes, but
780	# timeout of 1 does not.
781	atf_check -s exit:0 -o ignore ping -t 2 -c 1 198.51.100.2
782	atf_check -s exit:2 -o ignore ping -t 1 -c 1 198.51.100.2
783}
784
785dummynet_double_cleanup()
786{
787	pft_cleanup
788}
789
790atf_init_test_cases()
791{
792	atf_add_test_case "v4"
793	atf_add_test_case "v6"
794	atf_add_test_case "multiwan"
795	atf_add_test_case "multiwanlocal"
796	atf_add_test_case "icmp_nat"
797	atf_add_test_case "dummynet"
798	atf_add_test_case "dummynet_in"
799	atf_add_test_case "ifbound"
800	atf_add_test_case "ifbound_v6"
801	atf_add_test_case "ifbound_reply_to"
802	atf_add_test_case "ifbound_reply_to_v6"
803	atf_add_test_case "ifbound_reply_to_rdr_dummynet"
804	atf_add_test_case "dummynet_frag"
805	atf_add_test_case "dummynet_double"
806}
807