574 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03#4# author: Andrea Mayer <andrea.mayer@uniroma2.it>5# author: Paolo Lungaroni <paolo.lungaroni@uniroma2.it>6 7# This test is designed for evaluating the new SRv6 End.DT46 Behavior used for8# implementing IPv4/IPv6 L3 VPN use cases.9#10# The current SRv6 code in the Linux kernel only implements SRv6 End.DT4 and11# End.DT6 Behaviors which can be used respectively to support IPv4-in-IPv6 and12# IPv6-in-IPv6 VPNs. With End.DT4 and End.DT6 it is not possible to create a13# single SRv6 VPN tunnel to carry both IPv4 and IPv6 traffic.14# The SRv6 End.DT46 Behavior implementation is meant to support the15# decapsulation of IPv4 and IPv6 traffic coming from a single SRv6 tunnel.16# Therefore, the SRv6 End.DT46 Behavior in the Linux kernel greatly simplifies17# the setup and operations of SRv6 VPNs.18#19# Hereafter a network diagram is shown, where two different tenants (named 10020# and 200) offer IPv4/IPv6 L3 VPN services allowing hosts to communicate with21# each other across an IPv6 network.22#23# Only hosts belonging to the same tenant (and to the same VPN) can communicate24# with each other. Instead, the communication among hosts of different tenants25# is forbidden.26# In other words, hosts hs-t100-1 and hs-t100-2 are connected through the27# IPv4/IPv6 L3 VPN of tenant 100 while hs-t200-3 and hs-t200-4 are connected28# using the IPv4/IPv6 L3 VPN of tenant 200. Cross connection between tenant 10029# and tenant 200 is forbidden and thus, for example, hs-t100-1 cannot reach30# hs-t200-3 and vice versa.31#32# Routers rt-1 and rt-2 implement IPv4/IPv6 L3 VPN services leveraging the SRv633# architecture. The key components for such VPNs are: a) SRv6 Encap behavior,34# b) SRv6 End.DT46 Behavior and c) VRF.35#36# To explain how an IPv4/IPv6 L3 VPN based on SRv6 works, let us briefly37# consider an example where, within the same domain of tenant 100, the host38# hs-t100-1 pings the host hs-t100-2.39#40# First of all, L2 reachability of the host hs-t100-2 is taken into account by41# the router rt-1 which acts as a arp/ndp proxy.42#43# When the host hs-t100-1 sends an IPv6 or IPv4 packet destined to hs-t100-2,44# the router rt-1 receives the packet on the internal veth-t100 interface. Such45# interface is enslaved to the VRF vrf-100 whose associated table contains the46# SRv6 Encap route for encapsulating any IPv6 or IPv4 packet in a IPv6 plus the47# Segment Routing Header (SRH) packet. This packet is sent through the (IPv6)48# core network up to the router rt-2 that receives it on veth0 interface.49#50# The rt-2 router uses the 'localsid' routing table to process incoming51# IPv6+SRH packets which belong to the VPN of the tenant 100. For each of these52# packets, the SRv6 End.DT46 Behavior removes the outer IPv6+SRH headers and53# performs the lookup on the vrf-100 table using the destination address of54# the decapsulated IPv6 or IPv4 packet. Afterwards, the packet is sent to the55# host hs-t100-2 through the veth-t100 interface.56#57# The ping response follows the same processing but this time the roles of rt-158# and rt-2 are swapped.59#60# Of course, the IPv4/IPv6 L3 VPN for tenant 200 works exactly as the IPv4/IPv661# L3 VPN for tenant 100. In this case, only hosts hs-t200-3 and hs-t200-4 are62# able to connect with each other.63#64#65# +-------------------+ +-------------------+66# | | | |67# | hs-t100-1 netns | | hs-t100-2 netns |68# | | | |69# | +-------------+ | | +-------------+ |70# | | veth0 | | | | veth0 | |71# | | cafe::1/64 | | | | cafe::2/64 | |72# | | 10.0.0.1/24 | | | | 10.0.0.2/24 | |73# | +-------------+ | | +-------------+ |74# | . | | . |75# +-------------------+ +-------------------+76# . .77# . .78# . .79# +-----------------------------------+ +-----------------------------------+80# | . | | . |81# | +---------------+ | | +---------------- |82# | | veth-t100 | | | | veth-t100 | |83# | | cafe::254/64 | | | | cafe::254/64 | |84# | | 10.0.0.254/24 | +----------+ | | +----------+ | 10.0.0.254/24 | |85# | +-------+-------+ | localsid | | | | localsid | +-------+-------- |86# | | | table | | | | table | | |87# | +----+----+ +----------+ | | +----------+ +----+----+ |88# | | vrf-100 | | | | vrf-100 | |89# | +---------+ +------------+ | | +------------+ +---------+ |90# | | veth0 | | | | veth0 | |91# | | fd00::1/64 |.|...|.| fd00::2/64 | |92# | +---------+ +------------+ | | +------------+ +---------+ |93# | | vrf-200 | | | | vrf-200 | |94# | +----+----+ | | +----+----+ |95# | | | | | |96# | +-------+-------+ | | +-------+-------- |97# | | veth-t200 | | | | veth-t200 | |98# | | cafe::254/64 | | | | cafe::254/64 | |99# | | 10.0.0.254/24 | | | | 10.0.0.254/24 | |100# | +---------------+ rt-1 netns | | rt-2 netns +---------------- |101# | . | | . |102# +-----------------------------------+ +-----------------------------------+103# . .104# . .105# . .106# . .107# +-------------------+ +-------------------+108# | . | | . |109# | +-------------+ | | +-------------+ |110# | | veth0 | | | | veth0 | |111# | | cafe::3/64 | | | | cafe::4/64 | |112# | | 10.0.0.3/24 | | | | 10.0.0.4/24 | |113# | +-------------+ | | +-------------+ |114# | | | |115# | hs-t200-3 netns | | hs-t200-4 netns |116# | | | |117# +-------------------+ +-------------------+118#119#120# ~~~~~~~~~~~~~~~~~~~~~~~~~121# | Network configuration |122# ~~~~~~~~~~~~~~~~~~~~~~~~~123#124# rt-1: localsid table (table 90)125# +--------------------------------------------------+126# |SID |Action |127# +--------------------------------------------------+128# |fc00:21:100::6046|apply SRv6 End.DT46 vrftable 100|129# +--------------------------------------------------+130# |fc00:21:200::6046|apply SRv6 End.DT46 vrftable 200|131# +--------------------------------------------------+132#133# rt-1: VRF tenant 100 (table 100)134# +---------------------------------------------------+135# |host |Action |136# +---------------------------------------------------+137# |cafe::2 |apply seg6 encap segs fc00:12:100::6046|138# +---------------------------------------------------+139# |cafe::/64 |forward to dev veth-t100 |140# +---------------------------------------------------+141# |10.0.0.2 |apply seg6 encap segs fc00:12:100::6046|142# +---------------------------------------------------+143# |10.0.0.0/24|forward to dev veth-t100 |144# +---------------------------------------------------+145#146# rt-1: VRF tenant 200 (table 200)147# +---------------------------------------------------+148# |host |Action |149# +---------------------------------------------------+150# |cafe::4 |apply seg6 encap segs fc00:12:200::6046|151# +---------------------------------------------------+152# |cafe::/64 |forward to dev veth-t200 |153# +---------------------------------------------------+154# |10.0.0.4 |apply seg6 encap segs fc00:12:200::6046|155# +---------------------------------------------------+156# |10.0.0.0/24|forward to dev veth-t200 |157# +---------------------------------------------------+158#159#160# rt-2: localsid table (table 90)161# +--------------------------------------------------+162# |SID |Action |163# +--------------------------------------------------+164# |fc00:12:100::6046|apply SRv6 End.DT46 vrftable 100|165# +--------------------------------------------------+166# |fc00:12:200::6046|apply SRv6 End.DT46 vrftable 200|167# +--------------------------------------------------+168#169# rt-2: VRF tenant 100 (table 100)170# +---------------------------------------------------+171# |host |Action |172# +---------------------------------------------------+173# |cafe::1 |apply seg6 encap segs fc00:21:100::6046|174# +---------------------------------------------------+175# |cafe::/64 |forward to dev veth-t100 |176# +---------------------------------------------------+177# |10.0.0.1 |apply seg6 encap segs fc00:21:100::6046|178# +---------------------------------------------------+179# |10.0.0.0/24|forward to dev veth-t100 |180# +---------------------------------------------------+181#182# rt-2: VRF tenant 200 (table 200)183# +---------------------------------------------------+184# |host |Action |185# +---------------------------------------------------+186# |cafe::3 |apply seg6 encap segs fc00:21:200::6046|187# +---------------------------------------------------+188# |cafe::/64 |forward to dev veth-t200 |189# +---------------------------------------------------+190# |10.0.0.3 |apply seg6 encap segs fc00:21:200::6046|191# +---------------------------------------------------+192# |10.0.0.0/24|forward to dev veth-t200 |193# +---------------------------------------------------+194#195 196source lib.sh197 198readonly LOCALSID_TABLE_ID=90199readonly IPv6_RT_NETWORK=fd00200readonly IPv6_HS_NETWORK=cafe201readonly IPv4_HS_NETWORK=10.0.0202readonly VPN_LOCATOR_SERVICE=fc00203PING_TIMEOUT_SEC=4204 205ret=0206 207PAUSE_ON_FAIL=${PAUSE_ON_FAIL:=no}208 209log_test()210{211 local rc=$1212 local expected=$2213 local msg="$3"214 215 if [ ${rc} -eq ${expected} ]; then216 nsuccess=$((nsuccess+1))217 printf "\n TEST: %-60s [ OK ]\n" "${msg}"218 else219 ret=1220 nfail=$((nfail+1))221 printf "\n TEST: %-60s [FAIL]\n" "${msg}"222 if [ "${PAUSE_ON_FAIL}" = "yes" ]; then223 echo224 echo "hit enter to continue, 'q' to quit"225 read a226 [ "$a" = "q" ] && exit 1227 fi228 fi229}230 231print_log_test_results()232{233 if [ "$TESTS" != "none" ]; then234 printf "\nTests passed: %3d\n" ${nsuccess}235 printf "Tests failed: %3d\n" ${nfail}236 fi237}238 239log_section()240{241 echo242 echo "################################################################################"243 echo "TEST SECTION: $*"244 echo "################################################################################"245}246 247cleanup()248{249 ip link del veth-rt-1 2>/dev/null || true250 ip link del veth-rt-2 2>/dev/null || true251 252 cleanup_all_ns253}254 255# Setup the basic networking for the routers256setup_rt_networking()257{258 local id=$1259 eval local nsname=\${rt_${id}}260 261 ip link set veth-rt-${id} netns ${nsname}262 ip -netns ${nsname} link set veth-rt-${id} name veth0263 264 ip netns exec ${nsname} sysctl -wq net.ipv6.conf.all.accept_dad=0265 ip netns exec ${nsname} sysctl -wq net.ipv6.conf.default.accept_dad=0266 267 ip -netns ${nsname} addr add ${IPv6_RT_NETWORK}::${id}/64 dev veth0 nodad268 ip -netns ${nsname} link set veth0 up269 ip -netns ${nsname} link set lo up270 271 ip netns exec ${nsname} sysctl -wq net.ipv4.ip_forward=1272 ip netns exec ${nsname} sysctl -wq net.ipv6.conf.all.forwarding=1273}274 275setup_hs()276{277 local hid=$1278 local rid=$2279 local tid=$3280 eval local hsname=\${hs_t${tid}_${hid}}281 eval local rtname=\${rt_${rid}}282 local rtveth=veth-t${tid}283 284 # set the networking for the host285 ip netns exec ${hsname} sysctl -wq net.ipv6.conf.all.accept_dad=0286 ip netns exec ${hsname} sysctl -wq net.ipv6.conf.default.accept_dad=0287 288 # disable the rp_filter otherwise the kernel gets confused about how289 # to route decap ipv4 packets.290 ip netns exec ${rtname} sysctl -wq net.ipv4.conf.all.rp_filter=0291 ip netns exec ${rtname} sysctl -wq net.ipv4.conf.default.rp_filter=0292 293 ip -netns ${hsname} link add veth0 type veth peer name ${rtveth}294 ip -netns ${hsname} link set ${rtveth} netns ${rtname}295 ip -netns ${hsname} addr add ${IPv6_HS_NETWORK}::${hid}/64 dev veth0 nodad296 ip -netns ${hsname} addr add ${IPv4_HS_NETWORK}.${hid}/24 dev veth0297 ip -netns ${hsname} link set veth0 up298 ip -netns ${hsname} link set lo up299 300 # configure the VRF for the tenant X on the router which is directly301 # connected to the source host.302 ip -netns ${rtname} link add vrf-${tid} type vrf table ${tid}303 ip -netns ${rtname} link set vrf-${tid} up304 305 ip netns exec ${rtname} sysctl -wq net.ipv6.conf.all.accept_dad=0306 ip netns exec ${rtname} sysctl -wq net.ipv6.conf.default.accept_dad=0307 308 # enslave the veth-tX interface to the vrf-X in the access router309 ip -netns ${rtname} link set ${rtveth} master vrf-${tid}310 ip -netns ${rtname} addr add ${IPv6_HS_NETWORK}::254/64 dev ${rtveth} nodad311 ip -netns ${rtname} addr add ${IPv4_HS_NETWORK}.254/24 dev ${rtveth}312 ip -netns ${rtname} link set ${rtveth} up313 314 ip netns exec ${rtname} sysctl -wq net.ipv6.conf.${rtveth}.proxy_ndp=1315 ip netns exec ${rtname} sysctl -wq net.ipv4.conf.${rtveth}.proxy_arp=1316 317 ip netns exec ${rtname} sh -c "echo 1 > /proc/sys/net/vrf/strict_mode"318}319 320setup_vpn_config()321{322 local hssrc=$1323 local rtsrc=$2324 local hsdst=$3325 local rtdst=$4326 local tid=$5327 328 eval local rtsrc_name=\${rt_${rtsrc}}329 eval local rtdst_name=\${rt_${rtdst}}330 local rtveth=veth-t${tid}331 local vpn_sid=${VPN_LOCATOR_SERVICE}:${hssrc}${hsdst}:${tid}::6046332 333 ip -netns ${rtsrc_name} -6 neigh add proxy ${IPv6_HS_NETWORK}::${hsdst} dev ${rtveth}334 335 # set the encap route for encapsulating packets which arrive from the336 # host hssrc and destined to the access router rtsrc.337 ip -netns ${rtsrc_name} -6 route add ${IPv6_HS_NETWORK}::${hsdst}/128 vrf vrf-${tid} \338 encap seg6 mode encap segs ${vpn_sid} dev veth0339 ip -netns ${rtsrc_name} -4 route add ${IPv4_HS_NETWORK}.${hsdst}/32 vrf vrf-${tid} \340 encap seg6 mode encap segs ${vpn_sid} dev veth0341 ip -netns ${rtsrc_name} -6 route add ${vpn_sid}/128 vrf vrf-${tid} \342 via fd00::${rtdst} dev veth0343 344 # set the decap route for decapsulating packets which arrive from345 # the rtdst router and destined to the hsdst host.346 ip -netns ${rtdst_name} -6 route add ${vpn_sid}/128 table ${LOCALSID_TABLE_ID} \347 encap seg6local action End.DT46 vrftable ${tid} dev vrf-${tid}348 349 # all sids for VPNs start with a common locator which is fc00::/16.350 # Routes for handling the SRv6 End.DT46 behavior instances are grouped351 # together in the 'localsid' table.352 #353 # NOTE: added only once354 if [ -z "$(ip -netns ${rtdst_name} -6 rule show | \355 grep "to ${VPN_LOCATOR_SERVICE}::/16 lookup ${LOCALSID_TABLE_ID}")" ]; then356 ip -netns ${rtdst_name} -6 rule add \357 to ${VPN_LOCATOR_SERVICE}::/16 \358 lookup ${LOCALSID_TABLE_ID} prio 999359 fi360 361 # set default routes to unreachable for both ipv4 and ipv6362 ip -netns ${rtsrc_name} -6 route add unreachable default metric 4278198272 \363 vrf vrf-${tid}364 365 ip -netns ${rtsrc_name} -4 route add unreachable default metric 4278198272 \366 vrf vrf-${tid}367}368 369setup()370{371 ip link add veth-rt-1 type veth peer name veth-rt-2372 # setup the networking for router rt-1 and router rt-2373 setup_ns rt_1 rt_2374 setup_rt_networking 1375 setup_rt_networking 2376 377 # setup two hosts for the tenant 100.378 # - host hs-1 is directly connected to the router rt-1;379 # - host hs-2 is directly connected to the router rt-2.380 setup_ns hs_t100_1 hs_t100_2381 setup_hs 1 1 100 #args: host router tenant382 setup_hs 2 2 100383 384 # setup two hosts for the tenant 200385 # - host hs-3 is directly connected to the router rt-1;386 # - host hs-4 is directly connected to the router rt-2.387 setup_ns hs_t200_3 hs_t200_4388 setup_hs 3 1 200389 setup_hs 4 2 200390 391 # setup the IPv4/IPv6 L3 VPN which connects the host hs-t100-1 and host392 # hs-t100-2 within the same tenant 100.393 setup_vpn_config 1 1 2 2 100 #args: src_host src_router dst_host dst_router tenant394 setup_vpn_config 2 2 1 1 100395 396 # setup the IPv4/IPv6 L3 VPN which connects the host hs-t200-3 and host397 # hs-t200-4 within the same tenant 200.398 setup_vpn_config 3 1 4 2 200399 setup_vpn_config 4 2 3 1 200400}401 402check_rt_connectivity()403{404 local rtsrc=$1405 local rtdst=$2406 eval local nsname=\${rt_${rtsrc}}407 408 ip netns exec ${nsname} ping -c 1 -W 1 ${IPv6_RT_NETWORK}::${rtdst} \409 >/dev/null 2>&1410}411 412check_and_log_rt_connectivity()413{414 local rtsrc=$1415 local rtdst=$2416 417 check_rt_connectivity ${rtsrc} ${rtdst}418 log_test $? 0 "Routers connectivity: rt-${rtsrc} -> rt-${rtdst}"419}420 421check_hs_ipv6_connectivity()422{423 local hssrc=$1424 local hsdst=$2425 local tid=$3426 eval local nsname=\${hs_t${tid}_${hssrc}}427 428 ip netns exec ${nsname} ping -c 1 -W ${PING_TIMEOUT_SEC} \429 ${IPv6_HS_NETWORK}::${hsdst} >/dev/null 2>&1430}431 432check_hs_ipv4_connectivity()433{434 local hssrc=$1435 local hsdst=$2436 local tid=$3437 eval local nsname=\${hs_t${tid}_${hssrc}}438 439 ip netns exec ${nsname} ping -c 1 -W ${PING_TIMEOUT_SEC} \440 ${IPv4_HS_NETWORK}.${hsdst} >/dev/null 2>&1441}442 443check_and_log_hs_connectivity()444{445 local hssrc=$1446 local hsdst=$2447 local tid=$3448 449 check_hs_ipv6_connectivity ${hssrc} ${hsdst} ${tid}450 log_test $? 0 "IPv6 Hosts connectivity: hs-t${tid}-${hssrc} -> hs-t${tid}-${hsdst} (tenant ${tid})"451 452 check_hs_ipv4_connectivity ${hssrc} ${hsdst} ${tid}453 log_test $? 0 "IPv4 Hosts connectivity: hs-t${tid}-${hssrc} -> hs-t${tid}-${hsdst} (tenant ${tid})"454 455}456 457check_and_log_hs_isolation()458{459 local hssrc=$1460 local tidsrc=$2461 local hsdst=$3462 local tiddst=$4463 464 check_hs_ipv6_connectivity ${hssrc} ${hsdst} ${tidsrc}465 # NOTE: ping should fail466 log_test $? 1 "IPv6 Hosts isolation: hs-t${tidsrc}-${hssrc} -X-> hs-t${tiddst}-${hsdst}"467 468 check_hs_ipv4_connectivity ${hssrc} ${hsdst} ${tidsrc}469 # NOTE: ping should fail470 log_test $? 1 "IPv4 Hosts isolation: hs-t${tidsrc}-${hssrc} -X-> hs-t${tiddst}-${hsdst}"471 472}473 474 475check_and_log_hs2gw_connectivity()476{477 local hssrc=$1478 local tid=$2479 480 check_hs_ipv6_connectivity ${hssrc} 254 ${tid}481 log_test $? 0 "IPv6 Hosts connectivity: hs-t${tid}-${hssrc} -> gw (tenant ${tid})"482 483 check_hs_ipv4_connectivity ${hssrc} 254 ${tid}484 log_test $? 0 "IPv4 Hosts connectivity: hs-t${tid}-${hssrc} -> gw (tenant ${tid})"485 486}487 488router_tests()489{490 log_section "IPv6 routers connectivity test"491 492 check_and_log_rt_connectivity 1 2493 check_and_log_rt_connectivity 2 1494}495 496host2gateway_tests()497{498 log_section "IPv4/IPv6 connectivity test among hosts and gateway"499 500 check_and_log_hs2gw_connectivity 1 100501 check_and_log_hs2gw_connectivity 2 100502 503 check_and_log_hs2gw_connectivity 3 200504 check_and_log_hs2gw_connectivity 4 200505}506 507host_vpn_tests()508{509 log_section "SRv6 VPN connectivity test among hosts in the same tenant"510 511 check_and_log_hs_connectivity 1 2 100512 check_and_log_hs_connectivity 2 1 100513 514 check_and_log_hs_connectivity 3 4 200515 check_and_log_hs_connectivity 4 3 200516}517 518host_vpn_isolation_tests()519{520 local i521 local j522 local k523 local tmp524 local l1="1 2"525 local l2="3 4"526 local t1=100527 local t2=200528 529 log_section "SRv6 VPN isolation test among hosts in different tentants"530 531 for k in 0 1; do532 for i in ${l1}; do533 for j in ${l2}; do534 check_and_log_hs_isolation ${i} ${t1} ${j} ${t2}535 done536 done537 538 # let us test the reverse path539 tmp="${l1}"; l1="${l2}"; l2="${tmp}"540 tmp=${t1}; t1=${t2}; t2=${tmp}541 done542}543 544if [ "$(id -u)" -ne 0 ];then545 echo "SKIP: Need root privileges"546 exit $ksft_skip547fi548 549if [ ! -x "$(command -v ip)" ]; then550 echo "SKIP: Could not run test without ip tool"551 exit $ksft_skip552fi553 554modprobe vrf &>/dev/null555if [ ! -e /proc/sys/net/vrf/strict_mode ]; then556 echo "SKIP: vrf sysctl does not exist"557 exit $ksft_skip558fi559 560cleanup &>/dev/null561 562setup563 564router_tests565host2gateway_tests566host_vpn_tests567host_vpn_isolation_tests568 569print_log_test_results570 571cleanup &>/dev/null572 573exit ${ret}574