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 "ip6ip6"
104	$0 ipv6 ip6tnl none 100
105
106	echo "sit"
107	$0 ipv6 sit none 100
108
109	echo "ip4 vxlan"
110	$0 ipv4 vxlan eth 2000
111
112	echo "ip6 vxlan"
113	$0 ipv6 ip6vxlan eth 2000
114
115	for mac in none mpls eth ; do
116		echo "ip gre $mac"
117		$0 ipv4 gre $mac 100
118
119		echo "ip6 gre $mac"
120		$0 ipv6 ip6gre $mac 100
121
122		echo "ip gre $mac gso"
123		$0 ipv4 gre $mac 2000
124
125		echo "ip6 gre $mac gso"
126		$0 ipv6 ip6gre $mac 2000
127
128		echo "ip udp $mac"
129		$0 ipv4 udp $mac 100
130
131		echo "ip6 udp $mac"
132		$0 ipv6 ip6udp $mac 100
133
134		echo "ip udp $mac gso"
135		$0 ipv4 udp $mac 2000
136
137		echo "ip6 udp $mac gso"
138		$0 ipv6 ip6udp $mac 2000
139	done
140
141	echo "OK. All tests passed"
142	exit 0
143fi
144
145if [[ "$#" -ne "4" ]]; then
146	echo "Usage: $0"
147	echo "   or: $0 <ipv4|ipv6> <tuntype> <none|mpls|eth> <data_len>"
148	exit 1
149fi
150
151case "$1" in
152"ipv4")
153	readonly addr1="${ns1_v4}"
154	readonly addr2="${ns2_v4}"
155	readonly ipproto=4
156	readonly netcat_opt=-${ipproto}
157	readonly foumod=fou
158	readonly foutype=ipip
159	readonly fouproto=4
160	readonly fouproto_mpls=${mplsproto}
161	readonly gretaptype=gretap
162	;;
163"ipv6")
164	readonly addr1="${ns1_v6}"
165	readonly addr2="${ns2_v6}"
166	readonly ipproto=6
167	readonly netcat_opt=-${ipproto}
168	readonly foumod=fou6
169	readonly foutype=ip6tnl
170	readonly fouproto="41 -6"
171	readonly fouproto_mpls="${mplsproto} -6"
172	readonly gretaptype=ip6gretap
173	;;
174*)
175	echo "unknown arg: $1"
176	exit 1
177	;;
178esac
179
180readonly tuntype=$2
181readonly mac=$3
182readonly datalen=$4
183
184echo "encap ${addr1} to ${addr2}, type ${tuntype}, mac ${mac} len ${datalen}"
185
186trap cleanup EXIT
187
188setup
189
190# basic communication works
191echo "test basic connectivity"
192server_listen
193client_connect
194verify_data
195
196# clientside, insert bpf program to encap all TCP to port ${port}
197# client can no longer connect
198ip netns exec "${ns1}" tc qdisc add dev veth1 clsact
199ip netns exec "${ns1}" tc filter add dev veth1 egress \
200	bpf direct-action object-file ${BPF_FILE} \
201	section "encap_${tuntype}_${mac}"
202echo "test bpf encap without decap (expect failure)"
203server_listen
204! client_connect
205
206if [[ "$tuntype" =~ "udp" ]]; then
207	# Set up fou tunnel.
208	ttype="${foutype}"
209	targs="encap fou encap-sport auto encap-dport $udpport"
210	# fou may be a module; allow this to fail.
211	modprobe "${foumod}" ||true
212	if [[ "$mac" == "mpls" ]]; then
213		dport=${mplsudpport}
214		dproto=${fouproto_mpls}
215		tmode="mode any ttl 255"
216	else
217		dport=${udpport}
218		dproto=${fouproto}
219	fi
220	ip netns exec "${ns2}" ip fou add port $dport ipproto ${dproto}
221	targs="encap fou encap-sport auto encap-dport $dport"
222elif [[ "$tuntype" =~ "gre" && "$mac" == "eth" ]]; then
223	ttype=$gretaptype
224elif [[ "$tuntype" =~ "vxlan" && "$mac" == "eth" ]]; then
225	ttype="vxlan"
226	targs="id 1 dstport 8472 udp6zerocsumrx"
227else
228	ttype=$tuntype
229	targs=""
230fi
231
232# tunnel address family differs from inner for SIT
233if [[ "${tuntype}" == "sit" ]]; then
234	link_addr1="${ns1_v4}"
235	link_addr2="${ns2_v4}"
236else
237	link_addr1="${addr1}"
238	link_addr2="${addr2}"
239fi
240
241# serverside, insert decap module
242# server is still running
243# client can connect again
244ip netns exec "${ns2}" ip link add name testtun0 type "${ttype}" \
245	${tmode} remote "${link_addr1}" local "${link_addr2}" $targs
246
247expect_tun_fail=0
248
249if [[ "$tuntype" == "ip6udp" && "$mac" == "mpls" ]]; then
250	# No support for MPLS IPv6 fou tunnel; expect failure.
251	expect_tun_fail=1
252elif [[ "$tuntype" =~ "udp" && "$mac" == "eth" ]]; then
253	# No support for TEB fou tunnel; expect failure.
254	expect_tun_fail=1
255elif [[ "$tuntype" =~ (gre|vxlan) && "$mac" == "eth" ]]; then
256	# Share ethernet address between tunnel/veth2 so L2 decap works.
257	ethaddr=$(ip netns exec "${ns2}" ip link show veth2 | \
258		  awk '/ether/ { print $2 }')
259	ip netns exec "${ns2}" ip link set testtun0 address $ethaddr
260elif [[ "$mac" == "mpls" ]]; then
261	modprobe mpls_iptunnel ||true
262	modprobe mpls_gso ||true
263	ip netns exec "${ns2}" sysctl -qw net.mpls.platform_labels=65536
264	ip netns exec "${ns2}" ip -f mpls route add 1000 dev lo
265	ip netns exec "${ns2}" ip link set lo up
266	ip netns exec "${ns2}" sysctl -qw net.mpls.conf.testtun0.input=1
267	ip netns exec "${ns2}" sysctl -qw net.ipv4.conf.lo.rp_filter=0
268fi
269
270# Because packets are decapped by the tunnel they arrive on testtun0 from
271# the IP stack perspective.  Ensure reverse path filtering is disabled
272# otherwise we drop the TCP SYN as arriving on testtun0 instead of the
273# expected veth2 (veth2 is where 192.168.1.2 is configured).
274ip netns exec "${ns2}" sysctl -qw net.ipv4.conf.all.rp_filter=0
275# rp needs to be disabled for both all and testtun0 as the rp value is
276# selected as the max of the "all" and device-specific values.
277ip netns exec "${ns2}" sysctl -qw net.ipv4.conf.testtun0.rp_filter=0
278ip netns exec "${ns2}" ip link set dev testtun0 up
279if [[ "$expect_tun_fail" == 1 ]]; then
280	# This tunnel mode is not supported, so we expect failure.
281	echo "test bpf encap with tunnel device decap (expect failure)"
282	! client_connect
283else
284	echo "test bpf encap with tunnel device decap"
285	client_connect
286	verify_data
287	server_listen
288fi
289
290# bpf_skb_net_shrink does not take tunnel flags yet, cannot update L3.
291if [[ "${tuntype}" == "sit" ]]; then
292	echo OK
293	exit 0
294fi
295
296# serverside, use BPF for decap
297ip netns exec "${ns2}" ip link del dev testtun0
298ip netns exec "${ns2}" tc qdisc add dev veth2 clsact
299ip netns exec "${ns2}" tc filter add dev veth2 ingress \
300	bpf direct-action object-file ${BPF_FILE} section decap
301echo "test bpf encap with bpf decap"
302client_connect
303verify_data
304
305echo OK
306