xref: /freebsd/tests/sys/netpfil/pf/tcp.sh (revision 96190b4fef3b4a0cc3ca0606b0c4e3e69a5e6717)
1#
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2023 Rubicon Communications, LLC (Netgate)
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 "rst" "cleanup"
32rst_head()
33{
34	atf_set descr 'Check sequence number validation in RST packets'
35	atf_set require.user root
36	atf_set require.progs scapy
37}
38
39rst_body()
40{
41	pft_init
42	vnet_init_bridge
43
44	epair_srv=$(vnet_mkepair)
45	epair_cl=$(vnet_mkepair)
46	epair_attack=$(vnet_mkepair)
47
48	br=$(vnet_mkbridge)
49	ifconfig ${br} addm ${epair_srv}a
50	ifconfig ${epair_srv}a up
51	ifconfig ${br} addm ${epair_cl}a
52	ifconfig ${epair_cl}a up
53	ifconfig ${br} addm ${epair_attack}a
54	ifconfig ${epair_attack}a up
55	ifconfig ${br} up
56
57	vnet_mkjail srv ${epair_srv}b
58	jexec srv ifconfig ${epair_srv}b 192.0.2.1/24 up
59	jexec srv ifconfig lo0 inet 127.0.0.1/8 up
60
61	vnet_mkjail cl ${epair_cl}b
62	jexec cl ifconfig ${epair_cl}b 192.0.2.2/24 up
63	jexec cl ifconfig lo0 inet 127.0.0.1/8 up
64
65	jexec cl pfctl -e
66	pft_set_rules cl \
67		"pass keep state"
68
69	# Not required, but pf should log the bad RST packet with this set.
70	jexec cl pfctl -x loud
71
72	vnet_mkjail attack ${epair_attack}b
73	jexec attack ifconfig ${epair_attack}b 192.0.2.3/24 up
74
75	# Sanity check
76	atf_check -s exit:0 -o ignore \
77	    jexec cl ping -c 1 192.0.2.1
78
79	echo "bar" | jexec srv nc -l 1234 &
80	# Allow server time to start
81	sleep 1
82
83	echo "foo" | jexec cl nc -p 4321 192.0.2.1 1234 &
84	# Allow connection time to set up
85	sleep 1
86
87	# Connection should be established now
88	atf_check -s exit:0 -e ignore \
89	    -o match:"ESTABLISHED:ESTABLISHED" \
90	    jexec cl pfctl -ss -v
91
92	# Now insert a fake RST
93	atf_check -s exit:0 -o ignore \
94	    jexec attack ${common_dir}/pft_rst.py 192.0.2.1 1234 192.0.2.2 4321
95
96	# Connection should remain established
97	atf_check -s exit:0 -e ignore \
98	    -o match:"ESTABLISHED:ESTABLISHED" \
99	    jexec cl pfctl -ss -v
100}
101
102rst_cleanup()
103{
104	pft_cleanup
105}
106
107atf_init_test_cases()
108{
109	atf_add_test_case "rst"
110}
111