cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

icmp_redirect.sh (12820B)


      1#!/bin/bash
      2# SPDX-License-Identifier: GPL-2.0
      3#
      4# redirect test
      5#
      6#                     .253 +----+
      7#                     +----| r1 |
      8#                     |    +----+
      9# +----+              |       |.1
     10# | h1 |--------------+       |   10.1.1.0/30 2001:db8:1::0/126
     11# +----+ .1           |       |.2
     12#         172.16.1/24 |    +----+                   +----+
     13#    2001:db8:16:1/64 +----| r2 |-------------------| h2 |
     14#                     .254 +----+ .254           .2 +----+
     15#                                    172.16.2/24
     16#                                  2001:db8:16:2/64
     17#
     18# Route from h1 to h2 goes through r1, eth1 - connection between r1 and r2.
     19# Route on r1 changed to go to r2 via eth0. This causes a redirect to be sent
     20# from r1 to h1 telling h1 to use r2 when talking to h2.
     21
     22VERBOSE=0
     23PAUSE_ON_FAIL=no
     24
     25H1_N1_IP=172.16.1.1
     26R1_N1_IP=172.16.1.253
     27R2_N1_IP=172.16.1.254
     28
     29H1_N1_IP6=2001:db8:16:1::1
     30R1_N1_IP6=2001:db8:16:1::253
     31R2_N1_IP6=2001:db8:16:1::254
     32
     33R1_R2_N1_IP=10.1.1.1
     34R2_R1_N1_IP=10.1.1.2
     35
     36R1_R2_N1_IP6=2001:db8:1::1
     37R2_R1_N1_IP6=2001:db8:1::2
     38
     39H2_N2=172.16.2.0/24
     40H2_N2_6=2001:db8:16:2::/64
     41H2_N2_IP=172.16.2.2
     42R2_N2_IP=172.16.2.254
     43H2_N2_IP6=2001:db8:16:2::2
     44R2_N2_IP6=2001:db8:16:2::254
     45
     46VRF=red
     47VRF_TABLE=1111
     48
     49################################################################################
     50# helpers
     51
     52log_section()
     53{
     54	echo
     55	echo "###########################################################################"
     56	echo "$*"
     57	echo "###########################################################################"
     58	echo
     59}
     60
     61log_test()
     62{
     63	local rc=$1
     64	local expected=$2
     65	local msg="$3"
     66	local xfail=$4
     67
     68	if [ ${rc} -eq ${expected} ]; then
     69		printf "TEST: %-60s  [ OK ]\n" "${msg}"
     70		nsuccess=$((nsuccess+1))
     71	elif [ ${rc} -eq ${xfail} ]; then
     72		printf "TEST: %-60s  [XFAIL]\n" "${msg}"
     73		nxfail=$((nxfail+1))
     74	else
     75		ret=1
     76		nfail=$((nfail+1))
     77		printf "TEST: %-60s  [FAIL]\n" "${msg}"
     78		if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
     79			echo
     80			echo "hit enter to continue, 'q' to quit"
     81			read a
     82			[ "$a" = "q" ] && exit 1
     83		fi
     84	fi
     85}
     86
     87log_debug()
     88{
     89	if [ "$VERBOSE" = "1" ]; then
     90		echo "$*"
     91	fi
     92}
     93
     94run_cmd()
     95{
     96	local cmd="$*"
     97	local out
     98	local rc
     99
    100	if [ "$VERBOSE" = "1" ]; then
    101		echo "COMMAND: $cmd"
    102	fi
    103
    104	out=$(eval $cmd 2>&1)
    105	rc=$?
    106	if [ "$VERBOSE" = "1" -a -n "$out" ]; then
    107		echo "$out"
    108	fi
    109
    110	[ "$VERBOSE" = "1" ] && echo
    111
    112	return $rc
    113}
    114
    115get_linklocal()
    116{
    117	local ns=$1
    118	local dev=$2
    119	local addr
    120
    121	addr=$(ip -netns $ns -6 -br addr show dev ${dev} | \
    122	awk '{
    123		for (i = 3; i <= NF; ++i) {
    124			if ($i ~ /^fe80/)
    125				print $i
    126		}
    127	}'
    128	)
    129	addr=${addr/\/*}
    130
    131	[ -z "$addr" ] && return 1
    132
    133	echo $addr
    134
    135	return 0
    136}
    137
    138################################################################################
    139# setup and teardown
    140
    141cleanup()
    142{
    143	local ns
    144
    145	for ns in h1 h2 r1 r2; do
    146		ip netns del $ns 2>/dev/null
    147	done
    148}
    149
    150create_vrf()
    151{
    152	local ns=$1
    153
    154	ip -netns ${ns} link add ${VRF} type vrf table ${VRF_TABLE}
    155	ip -netns ${ns} link set ${VRF} up
    156	ip -netns ${ns} route add vrf ${VRF} unreachable default metric 8192
    157	ip -netns ${ns} -6 route add vrf ${VRF} unreachable default metric 8192
    158
    159	ip -netns ${ns} addr add 127.0.0.1/8 dev ${VRF}
    160	ip -netns ${ns} -6 addr add ::1 dev ${VRF} nodad
    161
    162	ip -netns ${ns} ru del pref 0
    163	ip -netns ${ns} ru add pref 32765 from all lookup local
    164	ip -netns ${ns} -6 ru del pref 0
    165	ip -netns ${ns} -6 ru add pref 32765 from all lookup local
    166}
    167
    168setup()
    169{
    170	local ns
    171
    172	#
    173	# create nodes as namespaces
    174	#
    175	for ns in h1 h2 r1 r2; do
    176		ip netns add $ns
    177		ip -netns $ns li set lo up
    178
    179		case "${ns}" in
    180		h[12]) ip netns exec $ns sysctl -q -w net.ipv4.conf.all.accept_redirects=1
    181		       ip netns exec $ns sysctl -q -w net.ipv6.conf.all.forwarding=0
    182		       ip netns exec $ns sysctl -q -w net.ipv6.conf.all.accept_redirects=1
    183		       ip netns exec $ns sysctl -q -w net.ipv6.conf.all.keep_addr_on_down=1
    184			;;
    185		r[12]) ip netns exec $ns sysctl -q -w net.ipv4.ip_forward=1
    186		       ip netns exec $ns sysctl -q -w net.ipv4.conf.all.send_redirects=1
    187		       ip netns exec $ns sysctl -q -w net.ipv4.conf.default.rp_filter=0
    188		       ip netns exec $ns sysctl -q -w net.ipv4.conf.all.rp_filter=0
    189
    190		       ip netns exec $ns sysctl -q -w net.ipv6.conf.all.forwarding=1
    191		       ip netns exec $ns sysctl -q -w net.ipv6.route.mtu_expires=10
    192		esac
    193	done
    194
    195	#
    196	# create interconnects
    197	#
    198	ip -netns h1 li add eth0 type veth peer name r1h1
    199	ip -netns h1 li set r1h1 netns r1 name eth0 up
    200
    201	ip -netns h1 li add eth1 type veth peer name r2h1
    202	ip -netns h1 li set r2h1 netns r2 name eth0 up
    203
    204	ip -netns h2 li add eth0 type veth peer name r2h2
    205	ip -netns h2 li set eth0 up
    206	ip -netns h2 li set r2h2 netns r2 name eth2 up
    207
    208	ip -netns r1 li add eth1 type veth peer name r2r1
    209	ip -netns r1 li set eth1 up
    210	ip -netns r1 li set r2r1 netns r2 name eth1 up
    211
    212	#
    213	# h1
    214	#
    215	if [ "${WITH_VRF}" = "yes" ]; then
    216		create_vrf "h1"
    217		H1_VRF_ARG="vrf ${VRF}"
    218		H1_PING_ARG="-I ${VRF}"
    219	else
    220		H1_VRF_ARG=
    221		H1_PING_ARG=
    222	fi
    223	ip -netns h1 li add br0 type bridge
    224	if [ "${WITH_VRF}" = "yes" ]; then
    225		ip -netns h1 li set br0 vrf ${VRF} up
    226	else
    227		ip -netns h1 li set br0 up
    228	fi
    229	ip -netns h1 addr add dev br0 ${H1_N1_IP}/24
    230	ip -netns h1 -6 addr add dev br0 ${H1_N1_IP6}/64 nodad
    231	ip -netns h1 li set eth0 master br0 up
    232	ip -netns h1 li set eth1 master br0 up
    233
    234	#
    235	# h2
    236	#
    237	ip -netns h2 addr add dev eth0 ${H2_N2_IP}/24
    238	ip -netns h2 ro add default via ${R2_N2_IP} dev eth0
    239	ip -netns h2 -6 addr add dev eth0 ${H2_N2_IP6}/64 nodad
    240	ip -netns h2 -6 ro add default via ${R2_N2_IP6} dev eth0
    241
    242	#
    243	# r1
    244	#
    245	ip -netns r1 addr add dev eth0 ${R1_N1_IP}/24
    246	ip -netns r1 -6 addr add dev eth0 ${R1_N1_IP6}/64 nodad
    247	ip -netns r1 addr add dev eth1 ${R1_R2_N1_IP}/30
    248	ip -netns r1 -6 addr add dev eth1 ${R1_R2_N1_IP6}/126 nodad
    249
    250	#
    251	# r2
    252	#
    253	ip -netns r2 addr add dev eth0 ${R2_N1_IP}/24
    254	ip -netns r2 -6 addr add dev eth0 ${R2_N1_IP6}/64 nodad
    255	ip -netns r2 addr add dev eth1 ${R2_R1_N1_IP}/30
    256	ip -netns r2 -6 addr add dev eth1 ${R2_R1_N1_IP6}/126 nodad
    257	ip -netns r2 addr add dev eth2 ${R2_N2_IP}/24
    258	ip -netns r2 -6 addr add dev eth2 ${R2_N2_IP6}/64 nodad
    259
    260	sleep 2
    261
    262	R1_LLADDR=$(get_linklocal r1 eth0)
    263	if [ $? -ne 0 ]; then
    264		echo "Error: Failed to get link-local address of r1's eth0"
    265		exit 1
    266	fi
    267	log_debug "initial gateway is R1's lladdr = ${R1_LLADDR}"
    268
    269	R2_LLADDR=$(get_linklocal r2 eth0)
    270	if [ $? -ne 0 ]; then
    271		echo "Error: Failed to get link-local address of r2's eth0"
    272		exit 1
    273	fi
    274	log_debug "initial gateway is R2's lladdr = ${R2_LLADDR}"
    275}
    276
    277change_h2_mtu()
    278{
    279	local mtu=$1
    280
    281	run_cmd ip -netns h2 li set eth0 mtu ${mtu}
    282	run_cmd ip -netns r2 li set eth2 mtu ${mtu}
    283}
    284
    285check_exception()
    286{
    287	local mtu="$1"
    288	local with_redirect="$2"
    289	local desc="$3"
    290
    291	# From 172.16.1.101: icmp_seq=1 Redirect Host(New nexthop: 172.16.1.102)
    292	if [ "$VERBOSE" = "1" ]; then
    293		echo "Commands to check for exception:"
    294		run_cmd ip -netns h1 ro get ${H1_VRF_ARG} ${H2_N2_IP}
    295		run_cmd ip -netns h1 -6 ro get ${H1_VRF_ARG} ${H2_N2_IP6}
    296	fi
    297
    298	if [ -n "${mtu}" ]; then
    299		mtu=" mtu ${mtu}"
    300	fi
    301	if [ "$with_redirect" = "yes" ]; then
    302		ip -netns h1 ro get ${H1_VRF_ARG} ${H2_N2_IP} | \
    303		grep -q "cache <redirected> expires [0-9]*sec${mtu}"
    304	elif [ -n "${mtu}" ]; then
    305		ip -netns h1 ro get ${H1_VRF_ARG} ${H2_N2_IP} | \
    306		grep -q "cache expires [0-9]*sec${mtu}"
    307	else
    308		# want to verify that neither mtu nor redirected appears in
    309		# the route get output. The -v will wipe out the cache line
    310		# if either are set so the last grep -q will not find a match
    311		ip -netns h1 ro get ${H1_VRF_ARG} ${H2_N2_IP} | \
    312		grep -E -v 'mtu|redirected' | grep -q "cache"
    313	fi
    314	log_test $? 0 "IPv4: ${desc}" 0
    315
    316	# No PMTU info for test "redirect" and "mtu exception plus redirect"
    317	if [ "$with_redirect" = "yes" ] && [ "$desc" != "redirect exception plus mtu" ]; then
    318		ip -netns h1 -6 ro get ${H1_VRF_ARG} ${H2_N2_IP6} | \
    319		grep -v "mtu" | grep -q "${H2_N2_IP6} .*via ${R2_LLADDR} dev br0"
    320	elif [ -n "${mtu}" ]; then
    321		ip -netns h1 -6 ro get ${H1_VRF_ARG} ${H2_N2_IP6} | \
    322		grep -q "${mtu}"
    323	else
    324		# IPv6 is a bit harder. First strip out the match if it
    325		# contains an mtu exception and then look for the first
    326		# gateway - R1's lladdr
    327		ip -netns h1 -6 ro get ${H1_VRF_ARG} ${H2_N2_IP6} | \
    328		grep -v "mtu" | grep -q "${R1_LLADDR}"
    329	fi
    330	log_test $? 0 "IPv6: ${desc}" 1
    331}
    332
    333run_ping()
    334{
    335	local sz=$1
    336
    337	run_cmd ip netns exec h1 ping -q -M want -i 0.5 -c 10 -w 2 -s ${sz} ${H1_PING_ARG} ${H2_N2_IP}
    338	run_cmd ip netns exec h1 ${ping6} -q -M want -i 0.5 -c 10 -w 2 -s ${sz} ${H1_PING_ARG} ${H2_N2_IP6}
    339}
    340
    341replace_route_new()
    342{
    343	# r1 to h2 via r2 and eth0
    344	run_cmd ip -netns r1 nexthop replace id 1 via ${R2_N1_IP} dev eth0
    345	run_cmd ip -netns r1 nexthop replace id 2 via ${R2_LLADDR} dev eth0
    346}
    347
    348reset_route_new()
    349{
    350	run_cmd ip -netns r1 nexthop flush
    351	run_cmd ip -netns h1 nexthop flush
    352
    353	initial_route_new
    354}
    355
    356initial_route_new()
    357{
    358	# r1 to h2 via r2 and eth1
    359	run_cmd ip -netns r1 nexthop add id 1 via ${R2_R1_N1_IP} dev eth1
    360	run_cmd ip -netns r1 ro add ${H2_N2} nhid 1
    361
    362	run_cmd ip -netns r1 nexthop add id 2 via ${R2_R1_N1_IP6} dev eth1
    363	run_cmd ip -netns r1 -6 ro add ${H2_N2_6} nhid 2
    364
    365	# h1 to h2 via r1
    366	run_cmd ip -netns h1 nexthop add id 1 via ${R1_N1_IP} dev br0
    367	run_cmd ip -netns h1 ro add ${H1_VRF_ARG} ${H2_N2} nhid 1
    368
    369	run_cmd ip -netns h1 nexthop add id 2 via ${R1_LLADDR} dev br0
    370	run_cmd ip -netns h1 -6 ro add ${H1_VRF_ARG} ${H2_N2_6} nhid 2
    371}
    372
    373replace_route_legacy()
    374{
    375	# r1 to h2 via r2 and eth0
    376	run_cmd ip -netns r1    ro replace ${H2_N2}   via ${R2_N1_IP}  dev eth0
    377	run_cmd ip -netns r1 -6 ro replace ${H2_N2_6} via ${R2_LLADDR} dev eth0
    378}
    379
    380reset_route_legacy()
    381{
    382	run_cmd ip -netns r1    ro del ${H2_N2}
    383	run_cmd ip -netns r1 -6 ro del ${H2_N2_6}
    384
    385	run_cmd ip -netns h1    ro del ${H1_VRF_ARG} ${H2_N2}
    386	run_cmd ip -netns h1 -6 ro del ${H1_VRF_ARG} ${H2_N2_6}
    387
    388	initial_route_legacy
    389}
    390
    391initial_route_legacy()
    392{
    393	# r1 to h2 via r2 and eth1
    394	run_cmd ip -netns r1    ro add ${H2_N2}   via ${R2_R1_N1_IP}  dev eth1
    395	run_cmd ip -netns r1 -6 ro add ${H2_N2_6} via ${R2_R1_N1_IP6} dev eth1
    396
    397	# h1 to h2 via r1
    398	# - IPv6 redirect only works if gateway is the LLA
    399	run_cmd ip -netns h1    ro add ${H1_VRF_ARG} ${H2_N2} via ${R1_N1_IP} dev br0
    400	run_cmd ip -netns h1 -6 ro add ${H1_VRF_ARG} ${H2_N2_6} via ${R1_LLADDR} dev br0
    401}
    402
    403check_connectivity()
    404{
    405	local rc
    406
    407	run_cmd ip netns exec h1 ping -c1 -w1 ${H1_PING_ARG} ${H2_N2_IP}
    408	rc=$?
    409	run_cmd ip netns exec h1 ${ping6} -c1 -w1 ${H1_PING_ARG} ${H2_N2_IP6}
    410	[ $? -ne 0 ] && rc=$?
    411
    412	return $rc
    413}
    414
    415do_test()
    416{
    417	local ttype="$1"
    418
    419	eval initial_route_${ttype}
    420
    421	# verify connectivity
    422	check_connectivity
    423	if [ $? -ne 0 ]; then
    424		echo "Error: Basic connectivity is broken"
    425		ret=1
    426		return
    427	fi
    428
    429	# redirect exception followed by mtu
    430	eval replace_route_${ttype}
    431	run_ping 64
    432	check_exception "" "yes" "redirect exception"
    433
    434	check_connectivity
    435	if [ $? -ne 0 ]; then
    436		echo "Error: Basic connectivity is broken after redirect"
    437		ret=1
    438		return
    439	fi
    440
    441	change_h2_mtu 1300
    442	run_ping 1350
    443	check_exception "1300" "yes" "redirect exception plus mtu"
    444
    445	# remove exceptions and restore routing
    446	change_h2_mtu 1500
    447	eval reset_route_${ttype}
    448
    449	check_connectivity
    450	if [ $? -ne 0 ]; then
    451		echo "Error: Basic connectivity is broken after reset"
    452		ret=1
    453		return
    454	fi
    455	check_exception "" "no" "routing reset"
    456
    457	# MTU exception followed by redirect
    458	change_h2_mtu 1300
    459	run_ping 1350
    460	check_exception "1300" "no" "mtu exception"
    461
    462	eval replace_route_${ttype}
    463	run_ping 64
    464	check_exception "1300" "yes" "mtu exception plus redirect"
    465
    466	check_connectivity
    467	if [ $? -ne 0 ]; then
    468		echo "Error: Basic connectivity is broken after redirect"
    469		ret=1
    470		return
    471	fi
    472}
    473
    474################################################################################
    475# usage
    476
    477usage()
    478{
    479        cat <<EOF
    480usage: ${0##*/} OPTS
    481
    482	-p          Pause on fail
    483	-v          verbose mode (show commands and output)
    484EOF
    485}
    486
    487################################################################################
    488# main
    489
    490# Some systems don't have a ping6 binary anymore
    491which ping6 > /dev/null 2>&1 && ping6=$(which ping6) || ping6=$(which ping)
    492
    493ret=0
    494nsuccess=0
    495nfail=0
    496nxfail=0
    497
    498while getopts :pv o
    499do
    500	case $o in
    501                p) PAUSE_ON_FAIL=yes;;
    502                v) VERBOSE=$(($VERBOSE + 1));;
    503                *) usage; exit 1;;
    504	esac
    505done
    506
    507trap cleanup EXIT
    508
    509cleanup
    510WITH_VRF=no
    511setup
    512
    513log_section "Legacy routing"
    514do_test "legacy"
    515
    516cleanup
    517log_section "Legacy routing with VRF"
    518WITH_VRF=yes
    519setup
    520do_test "legacy"
    521
    522cleanup
    523log_section "Routing with nexthop objects"
    524ip nexthop ls >/dev/null 2>&1
    525if [ $? -eq 0 ]; then
    526	WITH_VRF=no
    527	setup
    528	do_test "new"
    529
    530	cleanup
    531	log_section "Routing with nexthop objects and VRF"
    532	WITH_VRF=yes
    533	setup
    534	do_test "new"
    535else
    536	echo "Nexthop objects not supported; skipping tests"
    537fi
    538
    539printf "\nTests passed: %3d\n" ${nsuccess}
    540printf "Tests failed: %3d\n"   ${nfail}
    541printf "Tests xfailed: %3d\n"  ${nxfail}
    542
    543exit $ret