1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# In-place tunneling
5
6BPF_FILE="test_tc_tunnel.bpf.o"
7# must match the port that the bpf program filters on
8readonly port=8000
9
10readonly ns_prefix="ns-$$-"
11readonly ns1="${ns_prefix}1"
12readonly ns2="${ns_prefix}2"
13
14readonly ns1_v4=192.168.1.1
15readonly ns2_v4=192.168.1.2
16readonly ns1_v6=fd::1
17readonly ns2_v6=fd::2
18
19# Must match port used by bpf program
20readonly udpport=5555
21# MPLSoverUDP
22readonly mplsudpport=6635
23readonly mplsproto=137
24
25readonly infile="$(mktemp)"
26readonly outfile="$(mktemp)"
27
28setup() {
29	ip netns add "${ns1}"
30	ip netns add "${ns2}"
31
32	ip link add dev veth1 mtu 1500 netns "${ns1}" type veth \
33	      peer name veth2 mtu 1500 netns "${ns2}"
34
35	ip netns exec "${ns1}" ethtool -K veth1 tso off
36
37	ip -netns "${ns1}" link set veth1 up
38	ip -netns "${ns2}" link set veth2 up
39
40	ip -netns "${ns1}" -4 addr add "${ns1_v4}/24" dev veth1
41	ip -netns "${ns2}" -4 addr add "${ns2_v4}/24" dev veth2
42	ip -netns "${ns1}" -6 addr add "${ns1_v6}/64" dev veth1 nodad
43	ip -netns "${ns2}" -6 addr add "${ns2_v6}/64" dev veth2 nodad
44
45	# clamp route to reserve room for tunnel headers
46	ip -netns "${ns1}" -4 route flush table main
47	ip -netns "${ns1}" -6 route flush table main
48	ip -netns "${ns1}" -4 route add "${ns2_v4}" mtu 1450 dev veth1
49	ip -netns "${ns1}" -6 route add "${ns2_v6}" mtu 1430 dev veth1
50
51	sleep 1
52
53	dd if=/dev/urandom of="${infile}" bs="${datalen}" count=1 status=none
54}
55
56cleanup() {
57	ip netns del "${ns2}"
58	ip netns del "${ns1}"
59
60	if [[ -f "${outfile}" ]]; then
61		rm "${outfile}"
62	fi
63	if [[ -f "${infile}" ]]; then
64		rm "${infile}"
65	fi
66
67	if [[ -n $server_pid ]]; then
68		kill $server_pid 2> /dev/null
69	fi
70}
71
72server_listen() {
73	ip netns exec "${ns2}" nc "${netcat_opt}" -l "${port}" > "${outfile}" &
74	server_pid=$!
75	sleep 0.2
76}
77
78client_connect() {
79	ip netns exec "${ns1}" timeout 2 nc "${netcat_opt}" -w 1 "${addr2}" "${port}" < "${infile}"
80	echo $?
81}
82
83verify_data() {
84	wait "${server_pid}"
85	server_pid=
86	# sha1sum returns two fields [sha1] [filepath]
87	# convert to bash array and access first elem
88	insum=($(sha1sum ${infile}))
89	outsum=($(sha1sum ${outfile}))
90	if [[ "${insum[0]}" != "${outsum[0]}" ]]; then
91		echo "data mismatch"
92		exit 1
93	fi
94}
95
96set -e
97
98# no arguments: automated test, run all
99if [[ "$#" -eq "0" ]]; then
100	echo "ipip"
101	$0 ipv4 ipip none 100
102
103	echo "ipip6"
104	$0 ipv4 ipip6 none 100
105
106	echo "ip6ip6"
107	$0 ipv6 ip6tnl none 100
108
109	echo "sit"
110	$0 ipv6 sit none 100
111
112	echo "ip4 vxlan"
113	$0 ipv4 vxlan eth 2000
114
115	echo "ip6 vxlan"
116	$0 ipv6 ip6vxlan eth 2000
117
118	for mac in none mpls eth ; do
119		echo "ip gre $mac"
120		$0 ipv4 gre $mac 100
121
122		echo "ip6 gre $mac"
123		$0 ipv6 ip6gre $mac 100
124
125		echo "ip gre $mac gso"
126		$0 ipv4 gre $mac 2000
127
128		echo "ip6 gre $mac gso"
129		$0 ipv6 ip6gre $mac 2000
130
131		echo "ip udp $mac"
132		$0 ipv4 udp $mac 100
133
134		echo "ip6 udp $mac"
135		$0 ipv6 ip6udp $mac 100
136
137		echo "ip udp $mac gso"
138		$0 ipv4 udp $mac 2000
139
140		echo "ip6 udp $mac gso"
141		$0 ipv6 ip6udp $mac 2000
142	done
143
144	echo "OK. All tests passed"
145	exit 0
146fi
147
148if [[ "$#" -ne "4" ]]; then
149	echo "Usage: $0"
150	echo "   or: $0 <ipv4|ipv6> <tuntype> <none|mpls|eth> <data_len>"
151	exit 1
152fi
153
154case "$1" in
155"ipv4")
156	readonly addr1="${ns1_v4}"
157	readonly addr2="${ns2_v4}"
158	readonly ipproto=4
159	readonly netcat_opt=-${ipproto}
160	readonly foumod=fou
161	readonly foutype=ipip
162	readonly fouproto=4
163	readonly fouproto_mpls=${mplsproto}
164	readonly gretaptype=gretap
165	;;
166"ipv6")
167	readonly addr1="${ns1_v6}"
168	readonly addr2="${ns2_v6}"
169	readonly ipproto=6
170	readonly netcat_opt=-${ipproto}
171	readonly foumod=fou6
172	readonly foutype=ip6tnl
173	readonly fouproto="41 -6"
174	readonly fouproto_mpls="${mplsproto} -6"
175	readonly gretaptype=ip6gretap
176	;;
177*)
178	echo "unknown arg: $1"
179	exit 1
180	;;
181esac
182
183readonly tuntype=$2
184readonly mac=$3
185readonly datalen=$4
186
187echo "encap ${addr1} to ${addr2}, type ${tuntype}, mac ${mac} len ${datalen}"
188
189trap cleanup EXIT
190
191setup
192
193# basic communication works
194echo "test basic connectivity"
195server_listen
196client_connect
197verify_data
198
199# clientside, insert bpf program to encap all TCP to port ${port}
200# client can no longer connect
201ip netns exec "${ns1}" tc qdisc add dev veth1 clsact
202ip netns exec "${ns1}" tc filter add dev veth1 egress \
203	bpf direct-action object-file ${BPF_FILE} \
204	section "encap_${tuntype}_${mac}"
205echo "test bpf encap without decap (expect failure)"
206server_listen
207! client_connect
208
209if [[ "$tuntype" =~ "udp" ]]; then
210	# Set up fou tunnel.
211	ttype="${foutype}"
212	targs="encap fou encap-sport auto encap-dport $udpport"
213	# fou may be a module; allow this to fail.
214	modprobe "${foumod}" ||true
215	if [[ "$mac" == "mpls" ]]; then
216		dport=${mplsudpport}
217		dproto=${fouproto_mpls}
218		tmode="mode any ttl 255"
219	else
220		dport=${udpport}
221		dproto=${fouproto}
222	fi
223	ip netns exec "${ns2}" ip fou add port $dport ipproto ${dproto}
224	targs="encap fou encap-sport auto encap-dport $dport"
225elif [[ "$tuntype" =~ "gre" && "$mac" == "eth" ]]; then
226	ttype=$gretaptype
227elif [[ "$tuntype" =~ "vxlan" && "$mac" == "eth" ]]; then
228	ttype="vxlan"
229	targs="id 1 dstport 8472 udp6zerocsumrx"
230elif [[ "$tuntype" == "ipip6" ]]; then
231	ttype="ip6tnl"
232	targs=""
233else
234	ttype=$tuntype
235	targs=""
236fi
237
238# tunnel address family differs from inner for SIT
239if [[ "${tuntype}" == "sit" ]]; then
240	link_addr1="${ns1_v4}"
241	link_addr2="${ns2_v4}"
242elif [[ "${tuntype}" == "ipip6" ]]; then
243	link_addr1="${ns1_v6}"
244	link_addr2="${ns2_v6}"
245else
246	link_addr1="${addr1}"
247	link_addr2="${addr2}"
248fi
249
250# serverside, insert decap module
251# server is still running
252# client can connect again
253ip netns exec "${ns2}" ip link add name testtun0 type "${ttype}" \
254	${tmode} remote "${link_addr1}" local "${link_addr2}" $targs
255
256expect_tun_fail=0
257
258if [[ "$tuntype" == "ip6udp" && "$mac" == "mpls" ]]; then
259	# No support for MPLS IPv6 fou tunnel; expect failure.
260	expect_tun_fail=1
261elif [[ "$tuntype" =~ "udp" && "$mac" == "eth" ]]; then
262	# No support for TEB fou tunnel; expect failure.
263	expect_tun_fail=1
264elif [[ "$tuntype" =~ (gre|vxlan) && "$mac" == "eth" ]]; then
265	# Share ethernet address between tunnel/veth2 so L2 decap works.
266	ethaddr=$(ip netns exec "${ns2}" ip link show veth2 | \
267		  awk '/ether/ { print $2 }')
268	ip netns exec "${ns2}" ip link set testtun0 address $ethaddr
269elif [[ "$mac" == "mpls" ]]; then
270	modprobe mpls_iptunnel ||true
271	modprobe mpls_gso ||true
272	ip netns exec "${ns2}" sysctl -qw net.mpls.platform_labels=65536
273	ip netns exec "${ns2}" ip -f mpls route add 1000 dev lo
274	ip netns exec "${ns2}" ip link set lo up
275	ip netns exec "${ns2}" sysctl -qw net.mpls.conf.testtun0.input=1
276	ip netns exec "${ns2}" sysctl -qw net.ipv4.conf.lo.rp_filter=0
277fi
278
279# Because packets are decapped by the tunnel they arrive on testtun0 from
280# the IP stack perspective.  Ensure reverse path filtering is disabled
281# otherwise we drop the TCP SYN as arriving on testtun0 instead of the
282# expected veth2 (veth2 is where 192.168.1.2 is configured).
283ip netns exec "${ns2}" sysctl -qw net.ipv4.conf.all.rp_filter=0
284# rp needs to be disabled for both all and testtun0 as the rp value is
285# selected as the max of the "all" and device-specific values.
286ip netns exec "${ns2}" sysctl -qw net.ipv4.conf.testtun0.rp_filter=0
287ip netns exec "${ns2}" ip link set dev testtun0 up
288if [[ "$expect_tun_fail" == 1 ]]; then
289	# This tunnel mode is not supported, so we expect failure.
290	echo "test bpf encap with tunnel device decap (expect failure)"
291	! client_connect
292else
293	echo "test bpf encap with tunnel device decap"
294	client_connect
295	verify_data
296	server_listen
297fi
298
299# serverside, use BPF for decap
300ip netns exec "${ns2}" ip link del dev testtun0
301ip netns exec "${ns2}" tc qdisc add dev veth2 clsact
302ip netns exec "${ns2}" tc filter add dev veth2 ingress \
303	bpf direct-action object-file ${BPF_FILE} section decap
304echo "test bpf encap with bpf decap"
305client_connect
306verify_data
307
308echo OK
309