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