1 #!/bin/bash 1 #!/bin/bash 2 # SPDX-License-Identifier: GPL-2.0 2 # SPDX-License-Identifier: GPL-2.0 3 # 3 # 4 # Run traceroute/traceroute6 tests 4 # Run traceroute/traceroute6 tests 5 # 5 # 6 6 7 source lib.sh 7 source lib.sh 8 VERBOSE=0 8 VERBOSE=0 9 PAUSE_ON_FAIL=no 9 PAUSE_ON_FAIL=no 10 10 11 ############################################## 11 ################################################################################ 12 # 12 # 13 log_test() 13 log_test() 14 { 14 { 15 local rc=$1 15 local rc=$1 16 local expected=$2 16 local expected=$2 17 local msg="$3" 17 local msg="$3" 18 18 19 if [ ${rc} -eq ${expected} ]; then 19 if [ ${rc} -eq ${expected} ]; then 20 printf "TEST: %-60s [ OK ]\n" 20 printf "TEST: %-60s [ OK ]\n" "${msg}" 21 nsuccess=$((nsuccess+1)) 21 nsuccess=$((nsuccess+1)) 22 else 22 else 23 ret=1 23 ret=1 24 nfail=$((nfail+1)) 24 nfail=$((nfail+1)) 25 printf "TEST: %-60s [FAIL]\n" 25 printf "TEST: %-60s [FAIL]\n" "${msg}" 26 if [ "${PAUSE_ON_FAIL}" = "yes 26 if [ "${PAUSE_ON_FAIL}" = "yes" ]; then 27 echo 27 echo 28 echo "hit enter to con 28 echo "hit enter to continue, 'q' to quit" 29 read a 29 read a 30 [ "$a" = "q" ] && exit 30 [ "$a" = "q" ] && exit 1 31 fi 31 fi 32 fi 32 fi 33 } 33 } 34 34 35 run_cmd() 35 run_cmd() 36 { 36 { 37 local ns 37 local ns 38 local cmd 38 local cmd 39 local out 39 local out 40 local rc 40 local rc 41 41 42 ns="$1" 42 ns="$1" 43 shift 43 shift 44 cmd="$*" 44 cmd="$*" 45 45 46 if [ "$VERBOSE" = "1" ]; then 46 if [ "$VERBOSE" = "1" ]; then 47 printf " COMMAND: $cmd\n" 47 printf " COMMAND: $cmd\n" 48 fi 48 fi 49 49 50 out=$(eval ip netns exec ${ns} ${cmd} 50 out=$(eval ip netns exec ${ns} ${cmd} 2>&1) 51 rc=$? 51 rc=$? 52 if [ "$VERBOSE" = "1" -a -n "$out" ]; 52 if [ "$VERBOSE" = "1" -a -n "$out" ]; then 53 echo " $out" 53 echo " $out" 54 fi 54 fi 55 55 56 [ "$VERBOSE" = "1" ] && echo 56 [ "$VERBOSE" = "1" ] && echo 57 57 58 return $rc 58 return $rc 59 } 59 } 60 60 61 ############################################## 61 ################################################################################ 62 # create namespaces and interconnects 62 # create namespaces and interconnects 63 63 64 create_ns() 64 create_ns() 65 { 65 { 66 local ns=$1 66 local ns=$1 67 local addr=$2 67 local addr=$2 68 local addr6=$3 68 local addr6=$3 69 69 70 [ -z "${addr}" ] && addr="-" 70 [ -z "${addr}" ] && addr="-" 71 [ -z "${addr6}" ] && addr6="-" 71 [ -z "${addr6}" ] && addr6="-" 72 72 73 if [ "${addr}" != "-" ]; then 73 if [ "${addr}" != "-" ]; then 74 ip netns exec ${ns} ip addr ad 74 ip netns exec ${ns} ip addr add dev lo ${addr} 75 fi 75 fi 76 if [ "${addr6}" != "-" ]; then 76 if [ "${addr6}" != "-" ]; then 77 ip netns exec ${ns} ip -6 addr 77 ip netns exec ${ns} ip -6 addr add dev lo ${addr6} 78 fi 78 fi 79 79 80 ip netns exec ${ns} ip ro add unreacha 80 ip netns exec ${ns} ip ro add unreachable default metric 8192 81 ip netns exec ${ns} ip -6 ro add unrea 81 ip netns exec ${ns} ip -6 ro add unreachable default metric 8192 82 82 83 ip netns exec ${ns} sysctl -qw net.ipv 83 ip netns exec ${ns} sysctl -qw net.ipv4.ip_forward=1 84 ip netns exec ${ns} sysctl -qw net.ipv 84 ip netns exec ${ns} sysctl -qw net.ipv6.conf.all.keep_addr_on_down=1 85 ip netns exec ${ns} sysctl -qw net.ipv 85 ip netns exec ${ns} sysctl -qw net.ipv6.conf.all.forwarding=1 86 ip netns exec ${ns} sysctl -qw net.ipv 86 ip netns exec ${ns} sysctl -qw net.ipv6.conf.default.forwarding=1 87 ip netns exec ${ns} sysctl -qw net.ipv 87 ip netns exec ${ns} sysctl -qw net.ipv6.conf.default.accept_dad=0 88 } 88 } 89 89 90 # create veth pair to connect namespaces and a 90 # create veth pair to connect namespaces and apply addresses. 91 connect_ns() 91 connect_ns() 92 { 92 { 93 local ns1=$1 93 local ns1=$1 94 local ns1_dev=$2 94 local ns1_dev=$2 95 local ns1_addr=$3 95 local ns1_addr=$3 96 local ns1_addr6=$4 96 local ns1_addr6=$4 97 local ns2=$5 97 local ns2=$5 98 local ns2_dev=$6 98 local ns2_dev=$6 99 local ns2_addr=$7 99 local ns2_addr=$7 100 local ns2_addr6=$8 100 local ns2_addr6=$8 101 101 102 ip netns exec ${ns1} ip li add ${ns1_d 102 ip netns exec ${ns1} ip li add ${ns1_dev} type veth peer name tmp 103 ip netns exec ${ns1} ip li set ${ns1_d 103 ip netns exec ${ns1} ip li set ${ns1_dev} up 104 ip netns exec ${ns1} ip li set tmp net 104 ip netns exec ${ns1} ip li set tmp netns ${ns2} name ${ns2_dev} 105 ip netns exec ${ns2} ip li set ${ns2_d 105 ip netns exec ${ns2} ip li set ${ns2_dev} up 106 106 107 if [ "${ns1_addr}" != "-" ]; then 107 if [ "${ns1_addr}" != "-" ]; then 108 ip netns exec ${ns1} ip addr a 108 ip netns exec ${ns1} ip addr add dev ${ns1_dev} ${ns1_addr} 109 fi 109 fi 110 110 111 if [ "${ns2_addr}" != "-" ]; then 111 if [ "${ns2_addr}" != "-" ]; then 112 ip netns exec ${ns2} ip addr a 112 ip netns exec ${ns2} ip addr add dev ${ns2_dev} ${ns2_addr} 113 fi 113 fi 114 114 115 if [ "${ns1_addr6}" != "-" ]; then 115 if [ "${ns1_addr6}" != "-" ]; then 116 ip netns exec ${ns1} ip addr a 116 ip netns exec ${ns1} ip addr add dev ${ns1_dev} ${ns1_addr6} 117 fi 117 fi 118 118 119 if [ "${ns2_addr6}" != "-" ]; then 119 if [ "${ns2_addr6}" != "-" ]; then 120 ip netns exec ${ns2} ip addr a 120 ip netns exec ${ns2} ip addr add dev ${ns2_dev} ${ns2_addr6} 121 fi 121 fi 122 } 122 } 123 123 124 ############################################## 124 ################################################################################ 125 # traceroute6 test 125 # traceroute6 test 126 # 126 # 127 # Verify that in this scenario 127 # Verify that in this scenario 128 # 128 # 129 # ------------------------ N2 129 # ------------------------ N2 130 # | | 130 # | | 131 # ------ ------ N3 ---- 131 # ------ ------ N3 ---- 132 # | R1 | | R2 |------|H2| 132 # | R1 | | R2 |------|H2| 133 # ------ ------ ---- 133 # ------ ------ ---- 134 # | | 134 # | | 135 # ------------------------ N1 135 # ------------------------ N1 136 # | 136 # | 137 # ---- 137 # ---- 138 # |H1| 138 # |H1| 139 # ---- 139 # ---- 140 # 140 # 141 # where H1's default route goes through R1 and 141 # where H1's default route goes through R1 and R1's default route goes 142 # through R2 over N2, traceroute6 from H1 to H 142 # through R2 over N2, traceroute6 from H1 to H2 reports R2's address 143 # on N2 and not N1. 143 # on N2 and not N1. 144 # 144 # 145 # Addresses are assigned as follows: 145 # Addresses are assigned as follows: 146 # 146 # 147 # N1: 2000:101::/64 147 # N1: 2000:101::/64 148 # N2: 2000:102::/64 148 # N2: 2000:102::/64 149 # N3: 2000:103::/64 149 # N3: 2000:103::/64 150 # 150 # 151 # R1's host part of address: 1 151 # R1's host part of address: 1 152 # R2's host part of address: 2 152 # R2's host part of address: 2 153 # H1's host part of address: 3 153 # H1's host part of address: 3 154 # H2's host part of address: 4 154 # H2's host part of address: 4 155 # 155 # 156 # For example: 156 # For example: 157 # the IPv6 address of R1's interface on N2 is 157 # the IPv6 address of R1's interface on N2 is 2000:102::1/64 158 158 159 cleanup_traceroute6() 159 cleanup_traceroute6() 160 { 160 { 161 cleanup_ns $h1 $h2 $r1 $r2 161 cleanup_ns $h1 $h2 $r1 $r2 162 } 162 } 163 163 164 setup_traceroute6() 164 setup_traceroute6() 165 { 165 { 166 brdev=br0 166 brdev=br0 167 167 168 # start clean 168 # start clean 169 cleanup_traceroute6 169 cleanup_traceroute6 170 170 171 set -e 171 set -e 172 setup_ns h1 h2 r1 r2 172 setup_ns h1 h2 r1 r2 173 create_ns $h1 173 create_ns $h1 174 create_ns $h2 174 create_ns $h2 175 create_ns $r1 175 create_ns $r1 176 create_ns $r2 176 create_ns $r2 177 177 178 # Setup N3 178 # Setup N3 179 connect_ns $r2 eth3 - 2000:103::2/64 $ 179 connect_ns $r2 eth3 - 2000:103::2/64 $h2 eth3 - 2000:103::4/64 180 ip netns exec $h2 ip route add default 180 ip netns exec $h2 ip route add default via 2000:103::2 181 181 182 # Setup N2 182 # Setup N2 183 connect_ns $r1 eth2 - 2000:102::1/64 $ 183 connect_ns $r1 eth2 - 2000:102::1/64 $r2 eth2 - 2000:102::2/64 184 ip netns exec $r1 ip route add default 184 ip netns exec $r1 ip route add default via 2000:102::2 185 185 186 # Setup N1. host-1 and router-2 connec 186 # Setup N1. host-1 and router-2 connect to a bridge in router-1. 187 ip netns exec $r1 ip link add name ${b 187 ip netns exec $r1 ip link add name ${brdev} type bridge 188 ip netns exec $r1 ip link set ${brdev} 188 ip netns exec $r1 ip link set ${brdev} up 189 ip netns exec $r1 ip addr add 2000:101 189 ip netns exec $r1 ip addr add 2000:101::1/64 dev ${brdev} 190 190 191 connect_ns $h1 eth0 - 2000:101::3/64 $ 191 connect_ns $h1 eth0 - 2000:101::3/64 $r1 eth0 - - 192 ip netns exec $r1 ip link set dev eth0 192 ip netns exec $r1 ip link set dev eth0 master ${brdev} 193 ip netns exec $h1 ip route add default 193 ip netns exec $h1 ip route add default via 2000:101::1 194 194 195 connect_ns $r2 eth1 - 2000:101::2/64 $ 195 connect_ns $r2 eth1 - 2000:101::2/64 $r1 eth1 - - 196 ip netns exec $r1 ip link set dev eth1 196 ip netns exec $r1 ip link set dev eth1 master ${brdev} 197 197 198 # Prime the network 198 # Prime the network 199 ip netns exec $h1 ping6 -c5 2000:103:: 199 ip netns exec $h1 ping6 -c5 2000:103::4 >/dev/null 2>&1 200 200 201 set +e 201 set +e 202 } 202 } 203 203 204 run_traceroute6() 204 run_traceroute6() 205 { 205 { 206 if [ ! -x "$(command -v traceroute6)" 206 if [ ! -x "$(command -v traceroute6)" ]; then 207 echo "SKIP: Could not run IPV6 207 echo "SKIP: Could not run IPV6 test without traceroute6" 208 return 208 return 209 fi 209 fi 210 210 211 setup_traceroute6 211 setup_traceroute6 212 212 213 # traceroute6 host-2 from host-1 (expe 213 # traceroute6 host-2 from host-1 (expects 2000:102::2) 214 run_cmd $h1 "traceroute6 2000:103::4 | 214 run_cmd $h1 "traceroute6 2000:103::4 | grep -q 2000:102::2" 215 log_test $? 0 "IPV6 traceroute" 215 log_test $? 0 "IPV6 traceroute" 216 216 217 cleanup_traceroute6 217 cleanup_traceroute6 218 } 218 } 219 219 220 ############################################## 220 ################################################################################ 221 # traceroute test 221 # traceroute test 222 # 222 # 223 # Verify that traceroute from H1 to H2 shows 1 223 # Verify that traceroute from H1 to H2 shows 1.0.1.1 in this scenario 224 # 224 # 225 # 1.0.3.1/24 225 # 1.0.3.1/24 226 # ---- 1.0.1.3/24 1.0.1.1/24 ---- 1.0.2.1/2 226 # ---- 1.0.1.3/24 1.0.1.1/24 ---- 1.0.2.1/24 1.0.2.4/24 ---- 227 # |H1|--------------------------|R1|---------- 227 # |H1|--------------------------|R1|--------------------------|H2| 228 # ---- N1 ---- 228 # ---- N1 ---- N2 ---- 229 # 229 # 230 # where net.ipv4.icmp_errors_use_inbound_ifadd 230 # where net.ipv4.icmp_errors_use_inbound_ifaddr is set on R1 and 231 # 1.0.3.1/24 and 1.0.1.1/24 are respectively R 231 # 1.0.3.1/24 and 1.0.1.1/24 are respectively R1's primary and secondary 232 # address on N1. 232 # address on N1. 233 # 233 # 234 234 235 cleanup_traceroute() 235 cleanup_traceroute() 236 { 236 { 237 cleanup_ns $h1 $h2 $router 237 cleanup_ns $h1 $h2 $router 238 } 238 } 239 239 240 setup_traceroute() 240 setup_traceroute() 241 { 241 { 242 # start clean 242 # start clean 243 cleanup_traceroute 243 cleanup_traceroute 244 244 245 set -e 245 set -e 246 setup_ns h1 h2 router 246 setup_ns h1 h2 router 247 create_ns $h1 247 create_ns $h1 248 create_ns $h2 248 create_ns $h2 249 create_ns $router 249 create_ns $router 250 250 251 connect_ns $h1 eth0 1.0.1.3/24 - \ 251 connect_ns $h1 eth0 1.0.1.3/24 - \ 252 $router eth1 1.0.3.1/24 - 252 $router eth1 1.0.3.1/24 - 253 ip netns exec $h1 ip route add default 253 ip netns exec $h1 ip route add default via 1.0.1.1 254 254 255 ip netns exec $router ip addr add 1.0. 255 ip netns exec $router ip addr add 1.0.1.1/24 dev eth1 256 ip netns exec $router sysctl -qw \ 256 ip netns exec $router sysctl -qw \ 257 net.ipv4.icmp_ 257 net.ipv4.icmp_errors_use_inbound_ifaddr=1 258 258 259 connect_ns $h2 eth0 1.0.2.4/24 - \ 259 connect_ns $h2 eth0 1.0.2.4/24 - \ 260 $router eth2 1.0.2.1/24 - 260 $router eth2 1.0.2.1/24 - 261 ip netns exec $h2 ip route add default 261 ip netns exec $h2 ip route add default via 1.0.2.1 262 262 263 # Prime the network 263 # Prime the network 264 ip netns exec $h1 ping -c5 1.0.2.4 >/d 264 ip netns exec $h1 ping -c5 1.0.2.4 >/dev/null 2>&1 265 265 266 set +e 266 set +e 267 } 267 } 268 268 269 run_traceroute() 269 run_traceroute() 270 { 270 { 271 if [ ! -x "$(command -v traceroute)" ] 271 if [ ! -x "$(command -v traceroute)" ]; then 272 echo "SKIP: Could not run IPV4 272 echo "SKIP: Could not run IPV4 test without traceroute" 273 return 273 return 274 fi 274 fi 275 275 276 setup_traceroute 276 setup_traceroute 277 277 278 # traceroute host-2 from host-1 (expec 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 279 run_cmd $h1 "traceroute 1.0.2.4 | grep -q 1.0.1.1" 280 log_test $? 0 "IPV4 traceroute" 280 log_test $? 0 "IPV4 traceroute" 281 281 282 cleanup_traceroute 282 cleanup_traceroute 283 } 283 } 284 284 285 ############################################## 285 ################################################################################ 286 # Run tests 286 # Run tests 287 287 288 run_tests() 288 run_tests() 289 { 289 { 290 run_traceroute6 290 run_traceroute6 291 run_traceroute 291 run_traceroute 292 } 292 } 293 293 294 ############################################## 294 ################################################################################ 295 # main 295 # main 296 296 297 declare -i nfail=0 297 declare -i nfail=0 298 declare -i nsuccess=0 298 declare -i nsuccess=0 299 299 300 while getopts :pv o 300 while getopts :pv o 301 do 301 do 302 case $o in 302 case $o in 303 p) PAUSE_ON_FAIL=yes;; 303 p) PAUSE_ON_FAIL=yes;; 304 v) VERBOSE=$(($VERBOSE + 1));; 304 v) VERBOSE=$(($VERBOSE + 1));; 305 *) exit 1;; 305 *) exit 1;; 306 esac 306 esac 307 done 307 done 308 308 309 run_tests 309 run_tests 310 310 311 printf "\nTests passed: %3d\n" ${nsuccess} 311 printf "\nTests passed: %3d\n" ${nsuccess} 312 printf "Tests failed: %3d\n" ${nfail} 312 printf "Tests failed: %3d\n" ${nfail}
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.