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