2 # SPDX-License-Identifier: GPL-2.0
10 # | h1 |--------------+ | 10.1.1.0/30 2001:db8:1::0/126
12 # 172.16.1/24 | +----+ +----+
13 # 2001:db8:16:1/64 +----| r2 |-------------------| h2 |
14 # .254 +----+ .254 .2 +----+
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.
29 H1_N1_IP6=2001:db8:16:1::1
30 R1_N1_IP6=2001:db8:16:1::253
31 R2_N1_IP6=2001:db8:16:1::254
36 R1_R2_N1_IP6=2001:db8:1::1
37 R2_R1_N1_IP6=2001:db8:1::2
40 H2_N2_6=2001:db8:16:2::/64
43 H2_N2_IP6=2001:db8:16:2::2
44 R2_N2_IP6=2001:db8:16:2::254
49 ################################################################################
55 echo "###########################################################################"
57 echo "###########################################################################"
67 if [ ${rc} -eq ${expected} ]; then
68 printf "TEST: %-60s [ OK ]\n" "${msg}"
69 nsuccess=$((nsuccess+1))
73 printf "TEST: %-60s [FAIL]\n" "${msg}"
74 if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
76 echo "hit enter to continue, 'q' to quit"
78 [ "$a" = "q" ] && exit 1
85 if [ "$VERBOSE" = "1" ]; then
96 if [ "$VERBOSE" = "1" ]; then
100 out=$(eval $cmd 2>&1)
102 if [ "$VERBOSE" = "1" -a -n "$out" ]; then
106 [ "$VERBOSE" = "1" ] && echo
117 addr=$(ip -netns $ns -6 -br addr show dev ${dev} | \
119 for (i = 3; i <= NF; ++i) {
127 [ -z "$addr" ] && return 1
134 ################################################################################
141 for ns in h1 h2 r1 r2; do
142 ip netns del $ns 2>/dev/null
150 ip -netns ${ns} link add ${VRF} type vrf table ${VRF_TABLE}
151 ip -netns ${ns} link set ${VRF} up
152 ip -netns ${ns} route add vrf ${VRF} unreachable default metric 8192
153 ip -netns ${ns} -6 route add vrf ${VRF} unreachable default metric 8192
155 ip -netns ${ns} addr add 127.0.0.1/8 dev ${VRF}
156 ip -netns ${ns} -6 addr add ::1 dev ${VRF} nodad
158 ip -netns ${ns} ru del pref 0
159 ip -netns ${ns} ru add pref 32765 from all lookup local
160 ip -netns ${ns} -6 ru del pref 0
161 ip -netns ${ns} -6 ru add pref 32765 from all lookup local
169 # create nodes as namespaces
171 for ns in h1 h2 r1 r2; do
173 ip -netns $ns li set lo up
176 h[12]) ip netns exec $ns sysctl -q -w net.ipv4.conf.all.accept_redirects=1
177 ip netns exec $ns sysctl -q -w net.ipv6.conf.all.forwarding=0
178 ip netns exec $ns sysctl -q -w net.ipv6.conf.all.accept_redirects=1
179 ip netns exec $ns sysctl -q -w net.ipv6.conf.all.keep_addr_on_down=1
181 r[12]) ip netns exec $ns sysctl -q -w net.ipv4.ip_forward=1
182 ip netns exec $ns sysctl -q -w net.ipv4.conf.all.send_redirects=1
184 ip netns exec $ns sysctl -q -w net.ipv6.conf.all.forwarding=1
185 ip netns exec $ns sysctl -q -w net.ipv6.route.mtu_expires=10
190 # create interconnects
192 ip -netns h1 li add eth0 type veth peer name r1h1
193 ip -netns h1 li set r1h1 netns r1 name eth0 up
195 ip -netns h1 li add eth1 type veth peer name r2h1
196 ip -netns h1 li set r2h1 netns r2 name eth0 up
198 ip -netns h2 li add eth0 type veth peer name r2h2
199 ip -netns h2 li set eth0 up
200 ip -netns h2 li set r2h2 netns r2 name eth2 up
202 ip -netns r1 li add eth1 type veth peer name r2r1
203 ip -netns r1 li set eth1 up
204 ip -netns r1 li set r2r1 netns r2 name eth1 up
209 if [ "${WITH_VRF}" = "yes" ]; then
211 H1_VRF_ARG="vrf ${VRF}"
212 H1_PING_ARG="-I ${VRF}"
217 ip -netns h1 li add br0 type bridge
218 if [ "${WITH_VRF}" = "yes" ]; then
219 ip -netns h1 li set br0 vrf ${VRF} up
221 ip -netns h1 li set br0 up
223 ip -netns h1 addr add dev br0 ${H1_N1_IP}/24
224 ip -netns h1 -6 addr add dev br0 ${H1_N1_IP6}/64 nodad
225 ip -netns h1 li set eth0 master br0 up
226 ip -netns h1 li set eth1 master br0 up
231 ip -netns h2 addr add dev eth0 ${H2_N2_IP}/24
232 ip -netns h2 ro add default via ${R2_N2_IP} dev eth0
233 ip -netns h2 -6 addr add dev eth0 ${H2_N2_IP6}/64 nodad
234 ip -netns h2 -6 ro add default via ${R2_N2_IP6} dev eth0
239 ip -netns r1 addr add dev eth0 ${R1_N1_IP}/24
240 ip -netns r1 -6 addr add dev eth0 ${R1_N1_IP6}/64 nodad
241 ip -netns r1 addr add dev eth1 ${R1_R2_N1_IP}/30
242 ip -netns r1 -6 addr add dev eth1 ${R1_R2_N1_IP6}/126 nodad
247 ip -netns r2 addr add dev eth0 ${R2_N1_IP}/24
248 ip -netns r2 -6 addr add dev eth0 ${R2_N1_IP6}/64 nodad
249 ip -netns r2 addr add dev eth1 ${R2_R1_N1_IP}/30
250 ip -netns r2 -6 addr add dev eth1 ${R2_R1_N1_IP6}/126 nodad
251 ip -netns r2 addr add dev eth2 ${R2_N2_IP}/24
252 ip -netns r2 -6 addr add dev eth2 ${R2_N2_IP6}/64 nodad
256 R1_LLADDR=$(get_linklocal r1 eth0)
257 if [ $? -ne 0 ]; then
258 echo "Error: Failed to get link-local address of r1's eth0"
261 log_debug "initial gateway is R1's lladdr = ${R1_LLADDR}"
263 R2_LLADDR=$(get_linklocal r2 eth0)
264 if [ $? -ne 0 ]; then
265 echo "Error: Failed to get link-local address of r2's eth0"
268 log_debug "initial gateway is R2's lladdr = ${R2_LLADDR}"
275 run_cmd ip -netns h2 li set eth0 mtu ${mtu}
276 run_cmd ip -netns r2 li set eth2 mtu ${mtu}
282 local with_redirect="$2"
285 # From 172.16.1.101: icmp_seq=1 Redirect Host(New nexthop: 172.16.1.102)
286 if [ "$VERBOSE" = "1" ]; then
287 echo "Commands to check for exception:"
288 run_cmd ip -netns h1 ro get ${H1_VRF_ARG} ${H2_N2_IP}
289 run_cmd ip -netns h1 -6 ro get ${H1_VRF_ARG} ${H2_N2_IP6}
292 if [ -n "${mtu}" ]; then
295 if [ "$with_redirect" = "yes" ]; then
296 ip -netns h1 ro get ${H1_VRF_ARG} ${H2_N2_IP} | \
297 grep -q "cache <redirected> expires [0-9]*sec${mtu}"
298 elif [ -n "${mtu}" ]; then
299 ip -netns h1 ro get ${H1_VRF_ARG} ${H2_N2_IP} | \
300 grep -q "cache expires [0-9]*sec${mtu}"
302 # want to verify that neither mtu nor redirected appears in
303 # the route get output. The -v will wipe out the cache line
304 # if either are set so the last grep -q will not find a match
305 ip -netns h1 ro get ${H1_VRF_ARG} ${H2_N2_IP} | \
306 grep -E -v 'mtu|redirected' | grep -q "cache"
308 log_test $? 0 "IPv4: ${desc}"
310 if [ "$with_redirect" = "yes" ]; then
311 ip -netns h1 -6 ro get ${H1_VRF_ARG} ${H2_N2_IP6} | \
312 grep -q "${H2_N2_IP6} from :: via ${R2_LLADDR} dev br0.*${mtu}"
313 elif [ -n "${mtu}" ]; then
314 ip -netns h1 -6 ro get ${H1_VRF_ARG} ${H2_N2_IP6} | \
317 # IPv6 is a bit harder. First strip out the match if it
318 # contains an mtu exception and then look for the first
319 # gateway - R1's lladdr
320 ip -netns h1 -6 ro get ${H1_VRF_ARG} ${H2_N2_IP6} | \
321 grep -v "mtu" | grep -q "${R1_LLADDR}"
323 log_test $? 0 "IPv6: ${desc}"
330 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}
331 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}
336 # r1 to h2 via r2 and eth0
337 run_cmd ip -netns r1 nexthop replace id 1 via ${R2_N1_IP} dev eth0
338 run_cmd ip -netns r1 nexthop replace id 2 via ${R2_LLADDR} dev eth0
343 run_cmd ip -netns r1 nexthop flush
344 run_cmd ip -netns h1 nexthop flush
351 # r1 to h2 via r2 and eth1
352 run_cmd ip -netns r1 nexthop add id 1 via ${R2_R1_N1_IP} dev eth1
353 run_cmd ip -netns r1 ro add ${H2_N2} nhid 1
355 run_cmd ip -netns r1 nexthop add id 2 via ${R2_R1_N1_IP6} dev eth1
356 run_cmd ip -netns r1 -6 ro add ${H2_N2_6} nhid 2
359 run_cmd ip -netns h1 nexthop add id 1 via ${R1_N1_IP} dev br0
360 run_cmd ip -netns h1 ro add ${H1_VRF_ARG} ${H2_N2} nhid 1
362 run_cmd ip -netns h1 nexthop add id 2 via ${R1_LLADDR} dev br0
363 run_cmd ip -netns h1 -6 ro add ${H1_VRF_ARG} ${H2_N2_6} nhid 2
366 replace_route_legacy()
368 # r1 to h2 via r2 and eth0
369 run_cmd ip -netns r1 ro replace ${H2_N2} via ${R2_N1_IP} dev eth0
370 run_cmd ip -netns r1 -6 ro replace ${H2_N2_6} via ${R2_LLADDR} dev eth0
375 run_cmd ip -netns r1 ro del ${H2_N2}
376 run_cmd ip -netns r1 -6 ro del ${H2_N2_6}
378 run_cmd ip -netns h1 ro del ${H1_VRF_ARG} ${H2_N2}
379 run_cmd ip -netns h1 -6 ro del ${H1_VRF_ARG} ${H2_N2_6}
384 initial_route_legacy()
386 # r1 to h2 via r2 and eth1
387 run_cmd ip -netns r1 ro add ${H2_N2} via ${R2_R1_N1_IP} dev eth1
388 run_cmd ip -netns r1 -6 ro add ${H2_N2_6} via ${R2_R1_N1_IP6} dev eth1
391 # - IPv6 redirect only works if gateway is the LLA
392 run_cmd ip -netns h1 ro add ${H1_VRF_ARG} ${H2_N2} via ${R1_N1_IP} dev br0
393 run_cmd ip -netns h1 -6 ro add ${H1_VRF_ARG} ${H2_N2_6} via ${R1_LLADDR} dev br0
400 run_cmd ip netns exec h1 ping -c1 -w1 ${H1_PING_ARG} ${H2_N2_IP}
402 run_cmd ip netns exec h1 ${ping6} -c1 -w1 ${H1_PING_ARG} ${H2_N2_IP6}
403 [ $? -ne 0 ] && rc=$?
412 eval initial_route_${ttype}
414 # verify connectivity
416 if [ $? -ne 0 ]; then
417 echo "Error: Basic connectivity is broken"
422 # redirect exception followed by mtu
423 eval replace_route_${ttype}
425 check_exception "" "yes" "redirect exception"
428 if [ $? -ne 0 ]; then
429 echo "Error: Basic connectivity is broken after redirect"
436 check_exception "1300" "yes" "redirect exception plus mtu"
438 # remove exceptions and restore routing
440 eval reset_route_${ttype}
443 if [ $? -ne 0 ]; then
444 echo "Error: Basic connectivity is broken after reset"
448 check_exception "" "no" "routing reset"
450 # MTU exception followed by redirect
453 check_exception "1300" "no" "mtu exception"
455 eval replace_route_${ttype}
457 check_exception "1300" "yes" "mtu exception plus redirect"
460 if [ $? -ne 0 ]; then
461 echo "Error: Basic connectivity is broken after redirect"
467 ################################################################################
476 -v verbose mode (show commands and output)
480 ################################################################################
483 # Some systems don't have a ping6 binary anymore
484 which ping6 > /dev/null 2>&1 && ping6=$(which ping6) || ping6=$(which ping)
493 p) PAUSE_ON_FAIL=yes;;
494 v) VERBOSE=$(($VERBOSE + 1));;
505 log_section "Legacy routing"
509 log_section "Legacy routing with VRF"
515 log_section "Routing with nexthop objects"
516 ip nexthop ls >/dev/null 2>&1
517 if [ $? -eq 0 ]; then
523 log_section "Routing with nexthop objects and VRF"
528 echo "Nexthop objects not supported; skipping tests"
531 printf "\nTests passed: %3d\n" ${nsuccess}
532 printf "Tests failed: %3d\n" ${nfail}