xref: /linux/tools/testing/selftests/net/sctp_vrf.sh (revision 4b660dbd9ee2059850fd30e0df420ca7a38a1856)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# Testing For SCTP VRF.
5# TOPO: CLIENT_NS1 (veth1) <---> (veth1) -> vrf_s1
6#                                                  SERVER_NS
7#       CLIENT_NS2 (veth1) <---> (veth2) -> vrf_s2
8
9source lib.sh
10CLIENT_IP4="10.0.0.1"
11CLIENT_IP6="2000::1"
12CLIENT_PORT=1234
13
14SERVER_IP4="10.0.0.2"
15SERVER_IP6="2000::2"
16SERVER_PORT=1234
17
18setup() {
19	modprobe sctp
20	modprobe sctp_diag
21	setup_ns CLIENT_NS1 CLIENT_NS2 SERVER_NS
22
23	ip net exec $CLIENT_NS1 sysctl -w net.ipv6.conf.default.accept_dad=0 2>&1 >/dev/null
24	ip net exec $CLIENT_NS2 sysctl -w net.ipv6.conf.default.accept_dad=0 2>&1 >/dev/null
25	ip net exec $SERVER_NS sysctl -w net.ipv6.conf.default.accept_dad=0 2>&1 >/dev/null
26
27	ip -n $SERVER_NS link add veth1 type veth peer name veth1 netns $CLIENT_NS1
28	ip -n $SERVER_NS link add veth2 type veth peer name veth1 netns $CLIENT_NS2
29
30	ip -n $CLIENT_NS1 link set veth1 up
31	ip -n $CLIENT_NS1 addr add $CLIENT_IP4/24 dev veth1
32	ip -n $CLIENT_NS1 addr add $CLIENT_IP6/24 dev veth1
33
34	ip -n $CLIENT_NS2 link set veth1 up
35	ip -n $CLIENT_NS2 addr add $CLIENT_IP4/24 dev veth1
36	ip -n $CLIENT_NS2 addr add $CLIENT_IP6/24 dev veth1
37
38	ip -n $SERVER_NS link add dummy1 type dummy
39	ip -n $SERVER_NS link set dummy1 up
40	ip -n $SERVER_NS link add vrf-1 type vrf table 10
41	ip -n $SERVER_NS link add vrf-2 type vrf table 20
42	ip -n $SERVER_NS link set vrf-1 up
43	ip -n $SERVER_NS link set vrf-2 up
44	ip -n $SERVER_NS link set veth1 master vrf-1
45	ip -n $SERVER_NS link set veth2 master vrf-2
46
47	ip -n $SERVER_NS addr add $SERVER_IP4/24 dev dummy1
48	ip -n $SERVER_NS addr add $SERVER_IP4/24 dev veth1
49	ip -n $SERVER_NS addr add $SERVER_IP4/24 dev veth2
50	ip -n $SERVER_NS addr add $SERVER_IP6/24 dev dummy1
51	ip -n $SERVER_NS addr add $SERVER_IP6/24 dev veth1
52	ip -n $SERVER_NS addr add $SERVER_IP6/24 dev veth2
53
54	ip -n $SERVER_NS link set veth1 up
55	ip -n $SERVER_NS link set veth2 up
56	ip -n $SERVER_NS route add table 10 $CLIENT_IP4 dev veth1 src $SERVER_IP4
57	ip -n $SERVER_NS route add table 20 $CLIENT_IP4 dev veth2 src $SERVER_IP4
58	ip -n $SERVER_NS route add $CLIENT_IP4 dev veth1 src $SERVER_IP4
59	ip -n $SERVER_NS route add table 10 $CLIENT_IP6 dev veth1 src $SERVER_IP6
60	ip -n $SERVER_NS route add table 20 $CLIENT_IP6 dev veth2 src $SERVER_IP6
61	ip -n $SERVER_NS route add $CLIENT_IP6 dev veth1 src $SERVER_IP6
62}
63
64cleanup() {
65	ip netns exec $SERVER_NS pkill sctp_hello 2>&1 >/dev/null
66	cleanup_ns $CLIENT_NS1 $CLIENT_NS2 $SERVER_NS
67}
68
69wait_server() {
70	local IFACE=$1
71	local CNT=0
72
73	until ip netns exec $SERVER_NS ss -lS src $SERVER_IP:$SERVER_PORT | \
74		grep LISTEN | grep "$IFACE" 2>&1 >/dev/null; do
75		[ $((CNT++)) = "20" ] && { RET=3; return $RET; }
76		sleep 0.1
77	done
78}
79
80do_test() {
81	local CLIENT_NS=$1
82	local IFACE=$2
83
84	ip netns exec $SERVER_NS pkill sctp_hello 2>&1 >/dev/null
85	ip netns exec $SERVER_NS ./sctp_hello server $AF $SERVER_IP \
86		$SERVER_PORT $IFACE 2>&1 >/dev/null &
87	disown
88	wait_server $IFACE || return $RET
89	timeout 3 ip netns exec $CLIENT_NS ./sctp_hello client $AF \
90		$SERVER_IP $SERVER_PORT $CLIENT_IP $CLIENT_PORT 2>&1 >/dev/null
91	RET=$?
92	return $RET
93}
94
95do_testx() {
96	local IFACE1=$1
97	local IFACE2=$2
98
99	ip netns exec $SERVER_NS pkill sctp_hello 2>&1 >/dev/null
100	ip netns exec $SERVER_NS ./sctp_hello server $AF $SERVER_IP \
101		$SERVER_PORT $IFACE1 2>&1 >/dev/null &
102	disown
103	wait_server $IFACE1 || return $RET
104	ip netns exec $SERVER_NS ./sctp_hello server $AF $SERVER_IP \
105		$SERVER_PORT $IFACE2 2>&1 >/dev/null &
106	disown
107	wait_server $IFACE2 || return $RET
108	timeout 3 ip netns exec $CLIENT_NS1 ./sctp_hello client $AF \
109		$SERVER_IP $SERVER_PORT $CLIENT_IP $CLIENT_PORT 2>&1 >/dev/null && \
110	timeout 3 ip netns exec $CLIENT_NS2 ./sctp_hello client $AF \
111		$SERVER_IP $SERVER_PORT $CLIENT_IP $CLIENT_PORT 2>&1 >/dev/null
112	RET=$?
113	return $RET
114}
115
116testup() {
117	ip netns exec $SERVER_NS sysctl -w net.sctp.l3mdev_accept=1 2>&1 >/dev/null
118	echo -n "TEST 01: nobind, connect from client 1, l3mdev_accept=1, Y "
119	do_test $CLIENT_NS1 || { echo "[FAIL]"; return $RET; }
120	echo "[PASS]"
121
122	echo -n "TEST 02: nobind, connect from client 2, l3mdev_accept=1, N "
123	do_test $CLIENT_NS2 && { echo "[FAIL]"; return $RET; }
124	echo "[PASS]"
125
126	ip netns exec $SERVER_NS sysctl -w net.sctp.l3mdev_accept=0 2>&1 >/dev/null
127	echo -n "TEST 03: nobind, connect from client 1, l3mdev_accept=0, N "
128	do_test $CLIENT_NS1 && { echo "[FAIL]"; return $RET; }
129	echo "[PASS]"
130
131	echo -n "TEST 04: nobind, connect from client 2, l3mdev_accept=0, N "
132	do_test $CLIENT_NS2 && { echo "[FAIL]"; return $RET; }
133	echo "[PASS]"
134
135	echo -n "TEST 05: bind veth2 in server, connect from client 1, N "
136	do_test $CLIENT_NS1 veth2 && { echo "[FAIL]"; return $RET; }
137	echo "[PASS]"
138
139	echo -n "TEST 06: bind veth1 in server, connect from client 1, Y "
140	do_test $CLIENT_NS1 veth1 || { echo "[FAIL]"; return $RET; }
141	echo "[PASS]"
142
143	echo -n "TEST 07: bind vrf-1 in server, connect from client 1, Y "
144	do_test $CLIENT_NS1 vrf-1 || { echo "[FAIL]"; return $RET; }
145	echo "[PASS]"
146
147	echo -n "TEST 08: bind vrf-2 in server, connect from client 1, N "
148	do_test $CLIENT_NS1 vrf-2 && { echo "[FAIL]"; return $RET; }
149	echo "[PASS]"
150
151	echo -n "TEST 09: bind vrf-2 in server, connect from client 2, Y "
152	do_test $CLIENT_NS2 vrf-2 || { echo "[FAIL]"; return $RET; }
153	echo "[PASS]"
154
155	echo -n "TEST 10: bind vrf-1 in server, connect from client 2, N "
156	do_test $CLIENT_NS2 vrf-1 && { echo "[FAIL]"; return $RET; }
157	echo "[PASS]"
158
159	echo -n "TEST 11: bind vrf-1 & 2 in server, connect from client 1 & 2, Y "
160	do_testx vrf-1 vrf-2 || { echo "[FAIL]"; return $RET; }
161	echo "[PASS]"
162
163	echo -n "TEST 12: bind vrf-2 & 1 in server, connect from client 1 & 2, N "
164	do_testx vrf-2 vrf-1 || { echo "[FAIL]"; return $RET; }
165	echo "[PASS]"
166}
167
168trap cleanup EXIT
169setup && echo "Testing For SCTP VRF:" && \
170CLIENT_IP=$CLIENT_IP4 SERVER_IP=$SERVER_IP4 AF="-4" testup && echo "***v4 Tests Done***" &&
171CLIENT_IP=$CLIENT_IP6 SERVER_IP=$SERVER_IP6 AF="-6" testup && echo "***v6 Tests Done***"
172exit $?
173