xref: /openbmc/linux/tools/testing/selftests/net/mptcp/mptcp_sockopt.sh (revision b1c8ea3c09db24a55ff84ac047cb2e9d9f644bf9)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4. "$(dirname "${0}")/mptcp_lib.sh"
5
6ret=0
7sin=""
8sout=""
9cin=""
10cout=""
11ksft_skip=4
12timeout_poll=30
13timeout_test=$((timeout_poll * 2 + 1))
14mptcp_connect=""
15
16sec=$(date +%s)
17rndh=$(printf %x $sec)-$(mktemp -u XXXXXX)
18ns1="ns1-$rndh"
19ns2="ns2-$rndh"
20ns_sbox="ns_sbox-$rndh"
21
22add_mark_rules()
23{
24	local ns=$1
25	local m=$2
26
27	local t
28	for t in iptables ip6tables; do
29		# just to debug: check we have multiple subflows connection requests
30		ip netns exec $ns $t -A OUTPUT -p tcp --syn -m mark --mark $m -j ACCEPT
31
32		# RST packets might be handled by a internal dummy socket
33		ip netns exec $ns $t -A OUTPUT -p tcp --tcp-flags RST RST -m mark --mark 0 -j ACCEPT
34
35		ip netns exec $ns $t -A OUTPUT -p tcp -m mark --mark $m -j ACCEPT
36		ip netns exec $ns $t -A OUTPUT -p tcp -m mark --mark 0 -j DROP
37	done
38}
39
40init()
41{
42	local netns
43	for netns in "$ns1" "$ns2" "$ns_sbox";do
44		ip netns add $netns || exit $ksft_skip
45		ip -net $netns link set lo up
46		ip netns exec $netns sysctl -q net.mptcp.enabled=1
47		ip netns exec $netns sysctl -q net.ipv4.conf.all.rp_filter=0
48		ip netns exec $netns sysctl -q net.ipv4.conf.default.rp_filter=0
49	done
50
51	local i
52	for i in `seq 1 4`; do
53		ip link add ns1eth$i netns "$ns1" type veth peer name ns2eth$i netns "$ns2"
54		ip -net "$ns1" addr add 10.0.$i.1/24 dev ns1eth$i
55		ip -net "$ns1" addr add dead:beef:$i::1/64 dev ns1eth$i nodad
56		ip -net "$ns1" link set ns1eth$i up
57
58		ip -net "$ns2" addr add 10.0.$i.2/24 dev ns2eth$i
59		ip -net "$ns2" addr add dead:beef:$i::2/64 dev ns2eth$i nodad
60		ip -net "$ns2" link set ns2eth$i up
61
62		# let $ns2 reach any $ns1 address from any interface
63		ip -net "$ns2" route add default via 10.0.$i.1 dev ns2eth$i metric 10$i
64
65		ip netns exec $ns1 ./pm_nl_ctl add 10.0.$i.1 flags signal
66		ip netns exec $ns1 ./pm_nl_ctl add dead:beef:$i::1 flags signal
67
68		ip netns exec $ns2 ./pm_nl_ctl add 10.0.$i.2 flags signal
69		ip netns exec $ns2 ./pm_nl_ctl add dead:beef:$i::2 flags signal
70	done
71
72	ip netns exec $ns1 ./pm_nl_ctl limits 8 8
73	ip netns exec $ns2 ./pm_nl_ctl limits 8 8
74
75	add_mark_rules $ns1 1
76	add_mark_rules $ns2 2
77}
78
79cleanup()
80{
81	local netns
82	for netns in "$ns1" "$ns2" "$ns_sbox"; do
83		ip netns del $netns
84	done
85	rm -f "$cin" "$cout"
86	rm -f "$sin" "$sout"
87}
88
89mptcp_lib_check_mptcp
90
91ip -Version > /dev/null 2>&1
92if [ $? -ne 0 ];then
93	echo "SKIP: Could not run test without ip tool"
94	exit $ksft_skip
95fi
96
97iptables -V > /dev/null 2>&1
98if [ $? -ne 0 ];then
99	echo "SKIP: Could not run all tests without iptables tool"
100	exit $ksft_skip
101fi
102
103ip6tables -V > /dev/null 2>&1
104if [ $? -ne 0 ];then
105	echo "SKIP: Could not run all tests without ip6tables tool"
106	exit $ksft_skip
107fi
108
109check_mark()
110{
111	local ns=$1
112	local af=$2
113
114	local tables=iptables
115
116	if [ $af -eq 6 ];then
117		tables=ip6tables
118	fi
119
120	local counters values
121	counters=$(ip netns exec $ns $tables -v -L OUTPUT | grep DROP)
122	values=${counters%DROP*}
123
124	local v
125	for v in $values; do
126		if [ $v -ne 0 ]; then
127			echo "FAIL: got $tables $values in ns $ns , not 0 - not all expected packets marked" 1>&2
128			return 1
129		fi
130	done
131
132	return 0
133}
134
135print_file_err()
136{
137	ls -l "$1" 1>&2
138	echo "Trailing bytes are: "
139	tail -c 27 "$1"
140}
141
142check_transfer()
143{
144	local in=$1
145	local out=$2
146	local what=$3
147
148	cmp "$in" "$out" > /dev/null 2>&1
149	if [ $? -ne 0 ] ;then
150		echo "[ FAIL ] $what does not match (in, out):"
151		print_file_err "$in"
152		print_file_err "$out"
153		ret=1
154
155		return 1
156	fi
157
158	return 0
159}
160
161# $1: IP address
162is_v6()
163{
164	[ -z "${1##*:*}" ]
165}
166
167do_transfer()
168{
169	local listener_ns="$1"
170	local connector_ns="$2"
171	local cl_proto="$3"
172	local srv_proto="$4"
173	local connect_addr="$5"
174
175	local port=12001
176
177	:> "$cout"
178	:> "$sout"
179
180	local mptcp_connect="./mptcp_connect -r 20"
181
182	local local_addr
183	if is_v6 "${connect_addr}"; then
184		local_addr="::"
185	else
186		local_addr="0.0.0.0"
187	fi
188
189	timeout ${timeout_test} \
190		ip netns exec ${listener_ns} \
191			$mptcp_connect -t ${timeout_poll} -l -M 1 -p $port -s ${srv_proto} -c TIMESTAMPNS,TCPINQ \
192				${local_addr} < "$sin" > "$sout" &
193	local spid=$!
194
195	sleep 1
196
197	timeout ${timeout_test} \
198		ip netns exec ${connector_ns} \
199			$mptcp_connect -t ${timeout_poll} -M 2 -p $port -s ${cl_proto} -c TIMESTAMPNS,TCPINQ \
200				$connect_addr < "$cin" > "$cout" &
201
202	local cpid=$!
203
204	wait $cpid
205	local retc=$?
206	wait $spid
207	local rets=$?
208
209	if [ ${rets} -ne 0 ] || [ ${retc} -ne 0 ]; then
210		echo " client exit code $retc, server $rets" 1>&2
211		echo -e "\nnetns ${listener_ns} socket stat for ${port}:" 1>&2
212		ip netns exec ${listener_ns} ss -Menita 1>&2 -o "sport = :$port"
213
214		echo -e "\nnetns ${connector_ns} socket stat for ${port}:" 1>&2
215		ip netns exec ${connector_ns} ss -Menita 1>&2 -o "dport = :$port"
216
217		ret=1
218		return 1
219	fi
220
221	if [ $local_addr = "::" ];then
222		check_mark $listener_ns 6
223		check_mark $connector_ns 6
224	else
225		check_mark $listener_ns 4
226		check_mark $connector_ns 4
227	fi
228
229	check_transfer $cin $sout "file received by server"
230
231	rets=$?
232
233	if [ $retc -eq 0 ] && [ $rets -eq 0 ];then
234		return 0
235	fi
236
237	return 1
238}
239
240make_file()
241{
242	local name=$1
243	local who=$2
244	local size=$3
245
246	dd if=/dev/urandom of="$name" bs=1024 count=$size 2> /dev/null
247	echo -e "\nMPTCP_TEST_FILE_END_MARKER" >> "$name"
248
249	echo "Created $name (size $size KB) containing data sent by $who"
250}
251
252do_mptcp_sockopt_tests()
253{
254	local lret=0
255
256	ip netns exec "$ns_sbox" ./mptcp_sockopt
257	lret=$?
258
259	if [ $lret -ne 0 ]; then
260		echo "FAIL: SOL_MPTCP getsockopt" 1>&2
261		ret=$lret
262		return
263	fi
264
265	ip netns exec "$ns_sbox" ./mptcp_sockopt -6
266	lret=$?
267
268	if [ $lret -ne 0 ]; then
269		echo "FAIL: SOL_MPTCP getsockopt (ipv6)" 1>&2
270		ret=$lret
271		return
272	fi
273}
274
275run_tests()
276{
277	local listener_ns="$1"
278	local connector_ns="$2"
279	local connect_addr="$3"
280	local lret=0
281
282	do_transfer ${listener_ns} ${connector_ns} MPTCP MPTCP ${connect_addr}
283
284	lret=$?
285
286	if [ $lret -ne 0 ]; then
287		ret=$lret
288		return
289	fi
290}
291
292do_tcpinq_test()
293{
294	ip netns exec "$ns_sbox" ./mptcp_inq "$@"
295	local lret=$?
296	if [ $lret -ne 0 ];then
297		ret=$lret
298		echo "FAIL: mptcp_inq $@" 1>&2
299		return $lret
300	fi
301
302	echo "PASS: TCP_INQ cmsg/ioctl $@"
303	return $lret
304}
305
306do_tcpinq_tests()
307{
308	local lret=0
309
310	local args
311	for args in "-t tcp" "-r tcp"; do
312		do_tcpinq_test $args
313		lret=$?
314		if [ $lret -ne 0 ] ; then
315			return $lret
316		fi
317		do_tcpinq_test -6 $args
318		lret=$?
319		if [ $lret -ne 0 ] ; then
320			return $lret
321		fi
322	done
323
324	do_tcpinq_test -r tcp -t tcp
325
326	return $?
327}
328
329sin=$(mktemp)
330sout=$(mktemp)
331cin=$(mktemp)
332cout=$(mktemp)
333init
334make_file "$cin" "client" 1
335make_file "$sin" "server" 1
336trap cleanup EXIT
337
338run_tests $ns1 $ns2 10.0.1.1
339run_tests $ns1 $ns2 dead:beef:1::1
340
341if [ $ret -eq 0 ];then
342	echo "PASS: all packets had packet mark set"
343fi
344
345do_mptcp_sockopt_tests
346if [ $ret -eq 0 ];then
347	echo "PASS: SOL_MPTCP getsockopt has expected information"
348fi
349
350do_tcpinq_tests
351exit $ret
352