brintos

brintos / linux-shallow public Read only

0
0
Text · 6.9 KiB · 282f147 Raw
313 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03#4# Run traceroute/traceroute6 tests5#6 7source lib.sh8VERBOSE=09PAUSE_ON_FAIL=no10 11################################################################################12#13log_test()14{15	local rc=$116	local expected=$217	local msg="$3"18 19	if [ ${rc} -eq ${expected} ]; then20		printf "TEST: %-60s  [ OK ]\n" "${msg}"21		nsuccess=$((nsuccess+1))22	else23		ret=124		nfail=$((nfail+1))25		printf "TEST: %-60s  [FAIL]\n" "${msg}"26		if [ "${PAUSE_ON_FAIL}" = "yes" ]; then27			echo28			echo "hit enter to continue, 'q' to quit"29			read a30			[ "$a" = "q" ] && exit 131		fi32	fi33}34 35run_cmd()36{37	local ns38	local cmd39	local out40	local rc41 42	ns="$1"43	shift44	cmd="$*"45 46	if [ "$VERBOSE" = "1" ]; then47		printf "    COMMAND: $cmd\n"48	fi49 50	out=$(eval ip netns exec ${ns} ${cmd} 2>&1)51	rc=$?52	if [ "$VERBOSE" = "1" -a -n "$out" ]; then53		echo "    $out"54	fi55 56	[ "$VERBOSE" = "1" ] && echo57 58	return $rc59}60 61################################################################################62# create namespaces and interconnects63 64create_ns()65{66	local ns=$167	local addr=$268	local addr6=$369 70	[ -z "${addr}" ] && addr="-"71	[ -z "${addr6}" ] && addr6="-"72 73	if [ "${addr}" != "-" ]; then74		ip netns exec ${ns} ip addr add dev lo ${addr}75	fi76	if [ "${addr6}" != "-" ]; then77		ip netns exec ${ns} ip -6 addr add dev lo ${addr6}78	fi79 80	ip netns exec ${ns} ip ro add unreachable default metric 819281	ip netns exec ${ns} ip -6 ro add unreachable default metric 819282 83	ip netns exec ${ns} sysctl -qw net.ipv4.ip_forward=184	ip netns exec ${ns} sysctl -qw net.ipv6.conf.all.keep_addr_on_down=185	ip netns exec ${ns} sysctl -qw net.ipv6.conf.all.forwarding=186	ip netns exec ${ns} sysctl -qw net.ipv6.conf.default.forwarding=187	ip netns exec ${ns} sysctl -qw net.ipv6.conf.default.accept_dad=088}89 90# create veth pair to connect namespaces and apply addresses.91connect_ns()92{93	local ns1=$194	local ns1_dev=$295	local ns1_addr=$396	local ns1_addr6=$497	local ns2=$598	local ns2_dev=$699	local ns2_addr=$7100	local ns2_addr6=$8101 102	ip netns exec ${ns1} ip li add ${ns1_dev} type veth peer name tmp103	ip netns exec ${ns1} ip li set ${ns1_dev} up104	ip netns exec ${ns1} ip li set tmp netns ${ns2} name ${ns2_dev}105	ip netns exec ${ns2} ip li set ${ns2_dev} up106 107	if [ "${ns1_addr}" != "-" ]; then108		ip netns exec ${ns1} ip addr add dev ${ns1_dev} ${ns1_addr}109	fi110 111	if [ "${ns2_addr}" != "-" ]; then112		ip netns exec ${ns2} ip addr add dev ${ns2_dev} ${ns2_addr}113	fi114 115	if [ "${ns1_addr6}" != "-" ]; then116		ip netns exec ${ns1} ip addr add dev ${ns1_dev} ${ns1_addr6}117	fi118 119	if [ "${ns2_addr6}" != "-" ]; then120		ip netns exec ${ns2} ip addr add dev ${ns2_dev} ${ns2_addr6}121	fi122}123 124################################################################################125# traceroute6 test126#127# Verify that in this scenario128#129#        ------------------------ N2130#         |                    |131#       ------              ------  N3  ----132#       | R1 |              | R2 |------|H2|133#       ------              ------      ----134#         |                    |135#        ------------------------ N1136#                  |137#                 ----138#                 |H1|139#                 ----140#141# where H1's default route goes through R1 and R1's default route goes142# through R2 over N2, traceroute6 from H1 to H2 reports R2's address143# on N2 and not N1.144#145# Addresses are assigned as follows:146#147# N1: 2000:101::/64148# N2: 2000:102::/64149# N3: 2000:103::/64150#151# R1's host part of address: 1152# R2's host part of address: 2153# H1's host part of address: 3154# H2's host part of address: 4155#156# For example:157# the IPv6 address of R1's interface on N2 is 2000:102::1/64158 159cleanup_traceroute6()160{161	cleanup_ns $h1 $h2 $r1 $r2162}163 164setup_traceroute6()165{166	brdev=br0167 168	# start clean169	cleanup_traceroute6170 171	set -e172	setup_ns h1 h2 r1 r2173	create_ns $h1174	create_ns $h2175	create_ns $r1176	create_ns $r2177 178	# Setup N3179	connect_ns $r2 eth3 - 2000:103::2/64 $h2 eth3 - 2000:103::4/64180	ip netns exec $h2 ip route add default via 2000:103::2181 182	# Setup N2183	connect_ns $r1 eth2 - 2000:102::1/64 $r2 eth2 - 2000:102::2/64184	ip netns exec $r1 ip route add default via 2000:102::2185 186	# Setup N1. host-1 and router-2 connect to a bridge in router-1.187	ip netns exec $r1 ip link add name ${brdev} type bridge188	ip netns exec $r1 ip link set ${brdev} up189	ip netns exec $r1 ip addr add 2000:101::1/64 dev ${brdev}190 191	connect_ns $h1 eth0 - 2000:101::3/64 $r1 eth0 - -192	ip netns exec $r1 ip link set dev eth0 master ${brdev}193	ip netns exec $h1 ip route add default via 2000:101::1194 195	connect_ns $r2 eth1 - 2000:101::2/64 $r1 eth1 - -196	ip netns exec $r1 ip link set dev eth1 master ${brdev}197 198	# Prime the network199	ip netns exec $h1 ping6 -c5 2000:103::4 >/dev/null 2>&1200 201	set +e202}203 204run_traceroute6()205{206	if [ ! -x "$(command -v traceroute6)" ]; then207		echo "SKIP: Could not run IPV6 test without traceroute6"208		return209	fi210 211	setup_traceroute6212 213	# traceroute6 host-2 from host-1 (expects 2000:102::2)214	run_cmd $h1 "traceroute6 2000:103::4 | grep -q 2000:102::2"215	log_test $? 0 "IPV6 traceroute"216 217	cleanup_traceroute6218}219 220################################################################################221# traceroute test222#223# Verify that traceroute from H1 to H2 shows 1.0.1.1 in this scenario224#225#                    1.0.3.1/24226# ---- 1.0.1.3/24    1.0.1.1/24 ---- 1.0.2.1/24    1.0.2.4/24 ----227# |H1|--------------------------|R1|--------------------------|H2|228# ----            N1            ----            N2            ----229#230# where net.ipv4.icmp_errors_use_inbound_ifaddr is set on R1 and231# 1.0.3.1/24 and 1.0.1.1/24 are respectively R1's primary and secondary232# address on N1.233#234 235cleanup_traceroute()236{237	cleanup_ns $h1 $h2 $router238}239 240setup_traceroute()241{242	# start clean243	cleanup_traceroute244 245	set -e246	setup_ns h1 h2 router247	create_ns $h1248	create_ns $h2249	create_ns $router250 251	connect_ns $h1 eth0 1.0.1.3/24 - \252	           $router eth1 1.0.3.1/24 -253	ip netns exec $h1 ip route add default via 1.0.1.1254 255	ip netns exec $router ip addr add 1.0.1.1/24 dev eth1256	ip netns exec $router sysctl -qw \257				net.ipv4.icmp_errors_use_inbound_ifaddr=1258 259	connect_ns $h2 eth0 1.0.2.4/24 - \260	           $router eth2 1.0.2.1/24 -261	ip netns exec $h2 ip route add default via 1.0.2.1262 263	# Prime the network264	ip netns exec $h1 ping -c5 1.0.2.4 >/dev/null 2>&1265 266	set +e267}268 269run_traceroute()270{271	if [ ! -x "$(command -v traceroute)" ]; then272		echo "SKIP: Could not run IPV4 test without traceroute"273		return274	fi275 276	setup_traceroute277 278	# traceroute host-2 from host-1 (expects 1.0.1.1). Takes a while.279	run_cmd $h1 "traceroute 1.0.2.4 | grep -q 1.0.1.1"280	log_test $? 0 "IPV4 traceroute"281 282	cleanup_traceroute283}284 285################################################################################286# Run tests287 288run_tests()289{290	run_traceroute6291	run_traceroute292}293 294################################################################################295# main296 297declare -i nfail=0298declare -i nsuccess=0299 300while getopts :pv o301do302	case $o in303		p) PAUSE_ON_FAIL=yes;;304		v) VERBOSE=$(($VERBOSE + 1));;305		*) exit 1;;306	esac307done308 309run_tests310 311printf "\nTests passed: %3d\n" ${nsuccess}312printf "Tests failed: %3d\n"   ${nfail}313