1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# author: Andrea Mayer <andrea.mayer@uniroma2.it>
5
6# This test is designed for evaluating the new SRv6 End.DT4 behavior used for
7# implementing IPv4 L3 VPN use cases.
8#
9# Hereafter a network diagram is shown, where two different tenants (named 100
10# and 200) offer IPv4 L3 VPN services allowing hosts to communicate with each
11# other across an IPv6 network.
12#
13# Only hosts belonging to the same tenant (and to the same VPN) can communicate
14# with each other. Instead, the communication among hosts of different tenants
15# is forbidden.
16# In other words, hosts hs-t100-1 and hs-t100-2 are connected through the IPv4
17# L3 VPN of tenant 100 while hs-t200-3 and hs-t200-4 are connected using the
18# IPv4 L3 VPN of tenant 200. Cross connection between tenant 100 and tenant 200
19# is forbidden and thus, for example, hs-t100-1 cannot reach hs-t200-3 and vice
20# versa.
21#
22# Routers rt-1 and rt-2 implement IPv4 L3 VPN services leveraging the SRv6
23# architecture. The key components for such VPNs are: a) SRv6 Encap behavior,
24# b) SRv6 End.DT4 behavior and c) VRF.
25#
26# To explain how an IPv4 L3 VPN based on SRv6 works, let us briefly consider an
27# example where, within the same domain of tenant 100, the host hs-t100-1 pings
28# the host hs-t100-2.
29#
30# First of all, L2 reachability of the host hs-t100-2 is taken into account by
31# the router rt-1 which acts as an arp proxy.
32#
33# When the host hs-t100-1 sends an IPv4 packet destined to hs-t100-2, the
34# router rt-1 receives the packet on the internal veth-t100 interface. Such
35# interface is enslaved to the VRF vrf-100 whose associated table contains the
36# SRv6 Encap route for encapsulating any IPv4 packet in a IPv6 plus the Segment
37# Routing Header (SRH) packet. This packet is sent through the (IPv6) core
38# network up to the router rt-2 that receives it on veth0 interface.
39#
40# The rt-2 router uses the 'localsid' routing table to process incoming
41# IPv6+SRH packets which belong to the VPN of the tenant 100. For each of these
42# packets, the SRv6 End.DT4 behavior removes the outer IPv6+SRH headers and
43# performs the lookup on the vrf-100 table using the destination address of
44# the decapsulated IPv4 packet. Afterwards, the packet is sent to the host
45# hs-t100-2 through the veth-t100 interface.
46#
47# The ping response follows the same processing but this time the role of rt-1
48# and rt-2 are swapped.
49#
50# Of course, the IPv4 L3 VPN for tenant 200 works exactly as the IPv4 L3 VPN
51# for tenant 100. In this case, only hosts hs-t200-3 and hs-t200-4 are able to
52# connect with each other.
53#
54#
55# +-------------------+                                   +-------------------+
56# |                   |                                   |                   |
57# |  hs-t100-1 netns  |                                   |  hs-t100-2 netns  |
58# |                   |                                   |                   |
59# |  +-------------+  |                                   |  +-------------+  |
60# |  |    veth0    |  |                                   |  |    veth0    |  |
61# |  | 10.0.0.1/24 |  |                                   |  | 10.0.0.2/24 |  |
62# |  +-------------+  |                                   |  +-------------+  |
63# |        .          |                                   |         .         |
64# +-------------------+                                   +-------------------+
65#          .                                                        .
66#          .                                                        .
67#          .                                                        .
68# +-----------------------------------+   +-----------------------------------+
69# |        .                          |   |                         .         |
70# | +---------------+                 |   |                 +---------------- |
71# | |   veth-t100   |                 |   |                 |   veth-t100   | |
72# | | 10.0.0.254/24 |    +----------+ |   | +----------+    | 10.0.0.254/24 | |
73# | +-------+-------+    | localsid | |   | | localsid |    +-------+-------- |
74# |         |            |   table  | |   | |   table  |            |         |
75# |    +----+----+       +----------+ |   | +----------+       +----+----+    |
76# |    | vrf-100 |                    |   |                    | vrf-100 |    |
77# |    +---------+     +------------+ |   | +------------+     +---------+    |
78# |                    |   veth0    | |   | |   veth0    |                    |
79# |                    | fd00::1/64 |.|...|.| fd00::2/64 |                    |
80# |    +---------+     +------------+ |   | +------------+     +---------+    |
81# |    | vrf-200 |                    |   |                    | vrf-200 |    |
82# |    +----+----+                    |   |                    +----+----+    |
83# |         |                         |   |                         |         |
84# | +-------+-------+                 |   |                 +-------+-------- |
85# | |   veth-t200   |                 |   |                 |   veth-t200   | |
86# | | 10.0.0.254/24 |                 |   |                 | 10.0.0.254/24 | |
87# | +---------------+      rt-1 netns |   | rt-2 netns      +---------------- |
88# |        .                          |   |                          .        |
89# +-----------------------------------+   +-----------------------------------+
90#          .                                                         .
91#          .                                                         .
92#          .                                                         .
93#          .                                                         .
94# +-------------------+                                   +-------------------+
95# |        .          |                                   |          .        |
96# |  +-------------+  |                                   |  +-------------+  |
97# |  |    veth0    |  |                                   |  |    veth0    |  |
98# |  | 10.0.0.3/24 |  |                                   |  | 10.0.0.4/24 |  |
99# |  +-------------+  |                                   |  +-------------+  |
100# |                   |                                   |                   |
101# |  hs-t200-3 netns  |                                   |  hs-t200-4 netns  |
102# |                   |                                   |                   |
103# +-------------------+                                   +-------------------+
104#
105#
106# ~~~~~~~~~~~~~~~~~~~~~~~~~
107# | Network configuration |
108# ~~~~~~~~~~~~~~~~~~~~~~~~~
109#
110# rt-1: localsid table (table 90)
111# +-------------------------------------------------+
112# |SID              |Action                         |
113# +-------------------------------------------------+
114# |fc00:21:100::6004|apply SRv6 End.DT4 vrftable 100|
115# +-------------------------------------------------+
116# |fc00:21:200::6004|apply SRv6 End.DT4 vrftable 200|
117# +-------------------------------------------------+
118#
119# rt-1: VRF tenant 100 (table 100)
120# +---------------------------------------------------+
121# |host       |Action                                 |
122# +---------------------------------------------------+
123# |10.0.0.2   |apply seg6 encap segs fc00:12:100::6004|
124# +---------------------------------------------------+
125# |10.0.0.0/24|forward to dev veth_t100               |
126# +---------------------------------------------------+
127#
128# rt-1: VRF tenant 200 (table 200)
129# +---------------------------------------------------+
130# |host       |Action                                 |
131# +---------------------------------------------------+
132# |10.0.0.4   |apply seg6 encap segs fc00:12:200::6004|
133# +---------------------------------------------------+
134# |10.0.0.0/24|forward to dev veth_t200               |
135# +---------------------------------------------------+
136#
137#
138# rt-2: localsid table (table 90)
139# +-------------------------------------------------+
140# |SID              |Action                         |
141# +-------------------------------------------------+
142# |fc00:12:100::6004|apply SRv6 End.DT4 vrftable 100|
143# +-------------------------------------------------+
144# |fc00:12:200::6004|apply SRv6 End.DT4 vrftable 200|
145# +-------------------------------------------------+
146#
147# rt-2: VRF tenant 100 (table 100)
148# +---------------------------------------------------+
149# |host       |Action                                 |
150# +---------------------------------------------------+
151# |10.0.0.1   |apply seg6 encap segs fc00:21:100::6004|
152# +---------------------------------------------------+
153# |10.0.0.0/24|forward to dev veth_t100               |
154# +---------------------------------------------------+
155#
156# rt-2: VRF tenant 200 (table 200)
157# +---------------------------------------------------+
158# |host       |Action                                 |
159# +---------------------------------------------------+
160# |10.0.0.3   |apply seg6 encap segs fc00:21:200::6004|
161# +---------------------------------------------------+
162# |10.0.0.0/24|forward to dev veth_t200               |
163# +---------------------------------------------------+
164#
165
166# Kselftest framework requirement - SKIP code is 4.
167ksft_skip=4
168
169readonly LOCALSID_TABLE_ID=90
170readonly IPv6_RT_NETWORK=fd00
171readonly IPv4_HS_NETWORK=10.0.0
172readonly VPN_LOCATOR_SERVICE=fc00
173PING_TIMEOUT_SEC=4
174
175ret=0
176
177PAUSE_ON_FAIL=${PAUSE_ON_FAIL:=no}
178
179log_test()
180{
181	local rc=$1
182	local expected=$2
183	local msg="$3"
184
185	if [ ${rc} -eq ${expected} ]; then
186		nsuccess=$((nsuccess+1))
187		printf "\n    TEST: %-60s  [ OK ]\n" "${msg}"
188	else
189		ret=1
190		nfail=$((nfail+1))
191		printf "\n    TEST: %-60s  [FAIL]\n" "${msg}"
192		if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
193			echo
194			echo "hit enter to continue, 'q' to quit"
195			read a
196			[ "$a" = "q" ] && exit 1
197		fi
198	fi
199}
200
201print_log_test_results()
202{
203	if [ "$TESTS" != "none" ]; then
204		printf "\nTests passed: %3d\n" ${nsuccess}
205		printf "Tests failed: %3d\n"   ${nfail}
206	fi
207}
208
209log_section()
210{
211	echo
212	echo "################################################################################"
213	echo "TEST SECTION: $*"
214	echo "################################################################################"
215}
216
217cleanup()
218{
219	ip link del veth-rt-1 2>/dev/null || true
220	ip link del veth-rt-2 2>/dev/null || true
221
222	# destroy routers rt-* and hosts hs-*
223	for ns in $(ip netns show | grep -E 'rt-*|hs-*'); do
224		ip netns del ${ns} || true
225	done
226}
227
228# Setup the basic networking for the routers
229setup_rt_networking()
230{
231	local rt=$1
232	local nsname=rt-${rt}
233
234	ip netns add ${nsname}
235	ip link set veth-rt-${rt} netns ${nsname}
236	ip -netns ${nsname} link set veth-rt-${rt} name veth0
237
238	ip -netns ${nsname} addr add ${IPv6_RT_NETWORK}::${rt}/64 dev veth0
239	ip -netns ${nsname} link set veth0 up
240	ip -netns ${nsname} link set lo up
241
242	ip netns exec ${nsname} sysctl -wq net.ipv4.ip_forward=1
243	ip netns exec ${nsname} sysctl -wq net.ipv6.conf.all.forwarding=1
244}
245
246setup_hs()
247{
248	local hs=$1
249	local rt=$2
250	local tid=$3
251	local hsname=hs-t${tid}-${hs}
252	local rtname=rt-${rt}
253	local rtveth=veth-t${tid}
254
255	# set the networking for the host
256	ip netns add ${hsname}
257	ip -netns ${hsname} link add veth0 type veth peer name ${rtveth}
258	ip -netns ${hsname} link set ${rtveth} netns ${rtname}
259	ip -netns ${hsname} addr add ${IPv4_HS_NETWORK}.${hs}/24 dev veth0
260	ip -netns ${hsname} link set veth0 up
261	ip -netns ${hsname} link set lo up
262
263	# configure the VRF for the tenant X on the router which is directly
264	# connected to the source host.
265	ip -netns ${rtname} link add vrf-${tid} type vrf table ${tid}
266	ip -netns ${rtname} link set vrf-${tid} up
267
268	# enslave the veth-tX interface to the vrf-X in the access router
269	ip -netns ${rtname} link set ${rtveth} master vrf-${tid}
270	ip -netns ${rtname} addr add ${IPv4_HS_NETWORK}.254/24 dev ${rtveth}
271	ip -netns ${rtname} link set ${rtveth} up
272
273	ip netns exec ${rtname} sysctl -wq net.ipv4.conf.${rtveth}.proxy_arp=1
274
275	# disable the rp_filter otherwise the kernel gets confused about how
276	# to route decap ipv4 packets.
277	ip netns exec ${rtname} sysctl -wq net.ipv4.conf.all.rp_filter=0
278	ip netns exec ${rtname} sysctl -wq net.ipv4.conf.${rtveth}.rp_filter=0
279
280	ip netns exec ${rtname} sh -c "echo 1 > /proc/sys/net/vrf/strict_mode"
281}
282
283setup_vpn_config()
284{
285	local hssrc=$1
286	local rtsrc=$2
287	local hsdst=$3
288	local rtdst=$4
289	local tid=$5
290
291	local hssrc_name=hs-t${tid}-${hssrc}
292	local hsdst_name=hs-t${tid}-${hsdst}
293	local rtsrc_name=rt-${rtsrc}
294	local rtdst_name=rt-${rtdst}
295	local vpn_sid=${VPN_LOCATOR_SERVICE}:${hssrc}${hsdst}:${tid}::6004
296
297	# set the encap route for encapsulating packets which arrive from the
298	# host hssrc and destined to the access router rtsrc.
299	ip -netns ${rtsrc_name} -4 route add ${IPv4_HS_NETWORK}.${hsdst}/32 vrf vrf-${tid} \
300		encap seg6 mode encap segs ${vpn_sid} dev veth0
301	ip -netns ${rtsrc_name} -6 route add ${vpn_sid}/128 vrf vrf-${tid} \
302		via fd00::${rtdst} dev veth0
303
304	# set the decap route for decapsulating packets which arrive from
305	# the rtdst router and destined to the hsdst host.
306	ip -netns ${rtdst_name} -6 route add ${vpn_sid}/128 table ${LOCALSID_TABLE_ID} \
307		encap seg6local action End.DT4 vrftable ${tid} dev vrf-${tid}
308
309	# all sids for VPNs start with a common locator which is fc00::/16.
310	# Routes for handling the SRv6 End.DT4 behavior instances are grouped
311	# together in the 'localsid' table.
312	#
313	# NOTE: added only once
314	if [ -z "$(ip -netns ${rtdst_name} -6 rule show | \
315	    grep "to ${VPN_LOCATOR_SERVICE}::/16 lookup ${LOCALSID_TABLE_ID}")" ]; then
316		ip -netns ${rtdst_name} -6 rule add \
317			to ${VPN_LOCATOR_SERVICE}::/16 \
318			lookup ${LOCALSID_TABLE_ID} prio 999
319	fi
320}
321
322setup()
323{
324	ip link add veth-rt-1 type veth peer name veth-rt-2
325	# setup the networking for router rt-1 and router rt-2
326	setup_rt_networking 1
327	setup_rt_networking 2
328
329	# setup two hosts for the tenant 100.
330	#  - host hs-1 is directly connected to the router rt-1;
331	#  - host hs-2 is directly connected to the router rt-2.
332	setup_hs 1 1 100  #args: host router tenant
333	setup_hs 2 2 100
334
335	# setup two hosts for the tenant 200
336	#  - host hs-3 is directly connected to the router rt-1;
337	#  - host hs-4 is directly connected to the router rt-2.
338	setup_hs 3 1 200
339	setup_hs 4 2 200
340
341	# setup the IPv4 L3 VPN which connects the host hs-t100-1 and host
342	# hs-t100-2 within the same tenant 100.
343	setup_vpn_config 1 1 2 2 100  #args: src_host src_router dst_host dst_router tenant
344	setup_vpn_config 2 2 1 1 100
345
346	# setup the IPv4 L3 VPN which connects the host hs-t200-3 and host
347	# hs-t200-4 within the same tenant 200.
348	setup_vpn_config 3 1 4 2 200
349	setup_vpn_config 4 2 3 1 200
350}
351
352check_rt_connectivity()
353{
354	local rtsrc=$1
355	local rtdst=$2
356
357	ip netns exec rt-${rtsrc} ping -c 1 -W 1 ${IPv6_RT_NETWORK}::${rtdst} \
358		>/dev/null 2>&1
359}
360
361check_and_log_rt_connectivity()
362{
363	local rtsrc=$1
364	local rtdst=$2
365
366	check_rt_connectivity ${rtsrc} ${rtdst}
367	log_test $? 0 "Routers connectivity: rt-${rtsrc} -> rt-${rtdst}"
368}
369
370check_hs_connectivity()
371{
372	local hssrc=$1
373	local hsdst=$2
374	local tid=$3
375
376	ip netns exec hs-t${tid}-${hssrc} ping -c 1 -W ${PING_TIMEOUT_SEC} \
377		${IPv4_HS_NETWORK}.${hsdst} >/dev/null 2>&1
378}
379
380check_and_log_hs_connectivity()
381{
382	local hssrc=$1
383	local hsdst=$2
384	local tid=$3
385
386	check_hs_connectivity ${hssrc} ${hsdst} ${tid}
387	log_test $? 0 "Hosts connectivity: hs-t${tid}-${hssrc} -> hs-t${tid}-${hsdst} (tenant ${tid})"
388}
389
390check_and_log_hs_isolation()
391{
392	local hssrc=$1
393	local tidsrc=$2
394	local hsdst=$3
395	local tiddst=$4
396
397	check_hs_connectivity ${hssrc} ${hsdst} ${tidsrc}
398	# NOTE: ping should fail
399	log_test $? 1 "Hosts isolation: hs-t${tidsrc}-${hssrc} -X-> hs-t${tiddst}-${hsdst}"
400}
401
402
403check_and_log_hs2gw_connectivity()
404{
405	local hssrc=$1
406	local tid=$2
407
408	check_hs_connectivity ${hssrc} 254 ${tid}
409	log_test $? 0 "Hosts connectivity: hs-t${tid}-${hssrc} -> gw (tenant ${tid})"
410}
411
412router_tests()
413{
414	log_section "IPv6 routers connectivity test"
415
416	check_and_log_rt_connectivity 1 2
417	check_and_log_rt_connectivity 2 1
418}
419
420host2gateway_tests()
421{
422	log_section "IPv4 connectivity test among hosts and gateway"
423
424	check_and_log_hs2gw_connectivity 1 100
425	check_and_log_hs2gw_connectivity 2 100
426
427	check_and_log_hs2gw_connectivity 3 200
428	check_and_log_hs2gw_connectivity 4 200
429}
430
431host_vpn_tests()
432{
433	log_section "SRv6 VPN connectivity test among hosts in the same tenant"
434
435	check_and_log_hs_connectivity 1 2 100
436	check_and_log_hs_connectivity 2 1 100
437
438	check_and_log_hs_connectivity 3 4 200
439	check_and_log_hs_connectivity 4 3 200
440}
441
442host_vpn_isolation_tests()
443{
444	local i
445	local j
446	local k
447	local tmp
448	local l1="1 2"
449	local l2="3 4"
450	local t1=100
451	local t2=200
452
453	log_section "SRv6 VPN isolation test among hosts in different tentants"
454
455	for k in 0 1; do
456		for i in ${l1}; do
457			for j in ${l2}; do
458				check_and_log_hs_isolation ${i} ${t1} ${j} ${t2}
459			done
460		done
461
462		# let us test the reverse path
463		tmp="${l1}"; l1="${l2}"; l2="${tmp}"
464		tmp=${t1}; t1=${t2}; t2=${tmp}
465	done
466}
467
468if [ "$(id -u)" -ne 0 ];then
469	echo "SKIP: Need root privileges"
470	exit $ksft_skip
471fi
472
473if [ ! -x "$(command -v ip)" ]; then
474	echo "SKIP: Could not run test without ip tool"
475	exit $ksft_skip
476fi
477
478modprobe vrf &>/dev/null
479if [ ! -e /proc/sys/net/vrf/strict_mode ]; then
480        echo "SKIP: vrf sysctl does not exist"
481        exit $ksft_skip
482fi
483
484cleanup &>/dev/null
485
486setup
487
488router_tests
489host2gateway_tests
490host_vpn_tests
491host_vpn_isolation_tests
492
493print_log_test_results
494
495cleanup &>/dev/null
496
497exit ${ret}
498