1230150Sadrian#
2230150Sadrian# SPDX-License-Identifier: BSD-2-Clause
3230150Sadrian#
4230150Sadrian# Copyright (c) 2023 Rubicon Communications, LLC (Netgate)
5230150Sadrian#
6230150Sadrian# Redistribution and use in source and binary forms, with or without
7230150Sadrian# modification, are permitted provided that the following conditions
8230150Sadrian# are met:
9230150Sadrian# 1. Redistributions of source code must retain the above copyright
10230150Sadrian#    notice, this list of conditions and the following disclaimer.
11230150Sadrian# 2. Redistributions in binary form must reproduce the above copyright
12230150Sadrian#    notice, this list of conditions and the following disclaimer in the
13230150Sadrian#    documentation and/or other materials provided with the distribution.
14230150Sadrian#
15230150Sadrian# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16230150Sadrian# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17230150Sadrian# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18230150Sadrian# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19230150Sadrian# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20230150Sadrian# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21230150Sadrian# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22230150Sadrian# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23230150Sadrian# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24230150Sadrian# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25230150Sadrian# SUCH DAMAGE.
26230150Sadrian
27230150Sadrian. $(atf_get_srcdir)/utils.subr
28230150Sadrian
29230150Sadriancommon_dir=$(atf_get_srcdir)/../common
30230150Sadrian
31230150Sadrianatf_test_case "rst" "cleanup"
32230150Sadrianrst_head()
33230150Sadrian{
34230150Sadrian	atf_set descr 'Check sequence number validation in RST packets'
35230150Sadrian	atf_set require.user root
36230150Sadrian	atf_set require.progs scapy
37266105Sloos}
38230150Sadrian
39230150Sadrianrst_body()
40230150Sadrian{
41230150Sadrian	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