xref: /freebsd/tests/sys/netpfil/pf/rtable.sh (revision 5d3e7166)
1# $FreeBSD$
2#
3# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4#
5# Copyright (c) 2023 Kajetan Staszkiewicz <vegetga@tuxpowered.net>
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27
28. $(atf_get_srcdir)/utils.subr
29
30atf_test_case "forward_v4" "cleanup"
31forward_v4_head()
32{
33	atf_set descr 'Test IPv4 forwarding with rtable'
34	atf_set require.user root
35	atf_set require.progs scapy
36}
37
38forward_v4_body()
39{
40	setup_router_server_ipv4
41
42	# Sanity check
43	ping_server_check_reply exit:0
44
45	jexec router sysctl net.fibs=2
46	jexec router ifconfig ${epair_server}a fib 1
47	jexec router route del -net ${net_server}
48	jexec router route add -fib 1 -net ${net_server} -iface ${epair_server}a
49
50	# Sanity check
51	ping_server_check_reply exit:1
52
53	# This rule is not enough.
54	# Echo requests will be properly forwarded but replies can't be routed back.
55	pft_set_rules router \
56		"pass in on ${epair_tester}b inet proto icmp all icmp-type echoreq rtable 1"
57	ping_server_check_reply exit:1
58
59	# Allow replies coming back to the tester properly via stateful filtering post-routing.
60	pft_set_rules router \
61		"pass in  on ${epair_tester}b inet proto icmp all icmp-type echoreq rtable 1" \
62		"pass out on ${epair_server}a inet proto icmp all icmp-type echoreq rtable 0"
63	ping_server_check_reply exit:0
64
65	# Allow replies coming back to the tester properly via provding extra routes in rtable 1
66	pft_set_rules router \
67		"pass in  on ${epair_tester}b inet proto icmp all icmp-type echoreq rtable 1"
68	jexec router route add -fib 1 -net ${net_tester} -iface ${epair_tester}b
69	ping_server_check_reply exit:0
70}
71
72forward_v4_cleanup()
73{
74	pft_cleanup
75}
76
77atf_test_case "forward_v6" "cleanup"
78forward_v6_head()
79{
80	atf_set descr 'Test IPv6 forwarding with rtable'
81	atf_set require.user root
82	atf_set require.progs scapy
83}
84
85forward_v6_body()
86{
87	setup_router_server_ipv6
88
89	# Sanity check
90	ping_server_check_reply exit:0
91
92	jexec router sysctl net.fibs=2
93	jexec router ifconfig ${epair_server}a fib 1
94	jexec router route del -6 ${net_server}
95	jexec router route add -fib 1 -6 ${net_server} -iface ${epair_server}a
96
97	# Sanity check
98	ping_server_check_reply exit:1
99
100	# This rule is not enough.
101	# Echo requests will be properly forwarded but replies can't be routed back.
102	pft_set_rules router \
103		"pass inet6 proto icmp6 icmp6-type { neighbrsol, neighbradv }" \
104		"pass in on ${epair_tester}b inet6 proto icmp6 icmp6-type echoreq"
105	ping_server_check_reply exit:1
106
107	# Allow replies coming back to the tester properly via stateful filtering post-routing.
108	pft_set_rules router \
109		"pass inet6 proto icmp6 icmp6-type { neighbrsol, neighbradv }" \
110		"pass in  on ${epair_tester}b inet6 proto icmp6 icmp6-type echoreq rtable 1" \
111		"pass out on ${epair_server}a inet6 proto icmp6 icmp6-type echoreq rtable 0"
112	ping_server_check_reply exit:0
113
114	# Allow replies coming back to the tester properly via provding extra routes in rtable 1
115	pft_set_rules router \
116		"pass inet6 proto icmp6 icmp6-type { neighbrsol, neighbradv }" \
117		"pass in  on ${epair_tester}b inet6 proto icmp6 icmp6-type echoreq rtable 1"
118	jexec router route add -fib 1 -6 ${net_tester} -iface ${epair_tester}b
119	ping_server_check_reply exit:0
120}
121
122forward_v6_cleanup()
123{
124	pft_cleanup
125}
126
127atf_init_test_cases()
128{
129	atf_add_test_case "forward_v4"
130	atf_add_test_case "forward_v6"
131}
132