xref: /freebsd/tests/sys/netpfil/pf/set_tos.sh (revision 4133f23624058951a3b66e3ad735de980a485f36)
1# $FreeBSD$
2#
3# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4#
5# Copyright (c) 2017 Kristof Provost <kp@FreeBSD.org>
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
30common_dir=$(atf_get_srcdir)/../common
31
32atf_test_case "v4" "cleanup"
33v4_head()
34{
35	atf_set descr 'set-tos test'
36	atf_set require.user root
37
38	# We need scapy to be installed for out test scripts to work
39	atf_set require.progs scapy
40}
41
42v4_body()
43{
44	if [ `uname -p` = "i386" ]; then
45		atf_skip "https://bugs.freebsd.org/239380"
46	fi
47
48	pft_init
49
50	epair_send=$(vnet_mkepair)
51	ifconfig ${epair_send}a 192.0.2.1/24 up
52
53	epair_recv=$(vnet_mkepair)
54	ifconfig ${epair_recv}a up
55
56	vnet_mkjail alcatraz ${epair_send}b ${epair_recv}b
57	jexec alcatraz ifconfig ${epair_send}b 192.0.2.2/24 up
58	jexec alcatraz ifconfig ${epair_recv}b 198.51.100.2/24 up
59	jexec alcatraz sysctl net.inet.ip.forwarding=1
60	jexec alcatraz arp -s 198.51.100.3 00:01:02:03:04:05
61	route add -net 198.51.100.0/24 192.0.2.2
62
63	jexec alcatraz pfctl -e
64
65	# No change is done if not requested
66	pft_set_rules alcatraz "scrub out proto icmp"
67	atf_check -s exit:1 -o ignore ${common_dir}/pft_ping.py \
68		--sendif ${epair_send}a \
69		--to 198.51.100.3 \
70		--recvif ${epair_recv}a \
71		--expect-tos 42
72
73	# The requested ToS is set
74	pft_set_rules alcatraz "scrub out proto icmp set-tos 42"
75	atf_check -s exit:0 ${common_dir}/pft_ping.py \
76		--sendif ${epair_send}a \
77		--to 198.51.100.3 \
78		--recvif ${epair_recv}a \
79		--expect-tos 42
80
81	# ToS is not changed if the scrub rule does not match
82	pft_set_rules alcatraz "scrub out proto tcp set-tos 42"
83	atf_check -s exit:1 -o ignore ${common_dir}/pft_ping.py \
84		--sendif ${epair_send}a \
85		--to 198.51.100.3 \
86		--recvif ${epair_recv}a \
87		--expect-tos 42
88
89	# Multiple scrub rules match as expected
90	pft_set_rules alcatraz "scrub out proto tcp set-tos 13" \
91		"scrub out proto icmp set-tos 14"
92	atf_check -s exit:0 ${common_dir}/pft_ping.py \
93		--sendif ${epair_send}a \
94		--to 198.51.100.3 \
95		--recvif ${epair_recv}a \
96		--expect-tos 14
97
98	# And this works even if the packet already has ToS values set
99	atf_check -s exit:0 ${common_dir}/pft_ping.py \
100		--sendif ${epair_send}a \
101		--to 198.51.100.3 \
102		--recvif ${epair_recv}a \
103		--send-tos 42 \
104		--expect-tos 14
105
106	# ToS values are unmolested if the packets do not match a scrub rule
107	pft_set_rules alcatraz "scrub out proto tcp set-tos 13"
108	atf_check -s exit:0 ${common_dir}/pft_ping.py \
109		--sendif ${epair_send}a \
110		--to 198.51.100.3 \
111		--recvif ${epair_recv}a \
112		--send-tos 42 \
113		--expect-tos 42
114}
115
116v4_cleanup()
117{
118	pft_cleanup
119}
120
121atf_init_test_cases()
122{
123	atf_add_test_case "v4"
124}
125