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