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 "###########################################################################"
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}"
77 printf "TEST: %-60s [FAIL]\n" "${msg}"
78 if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
80 echo "hit enter to continue, 'q' to quit"
82 [ "$a" = "q" ] && exit 1
89 if [ "$VERBOSE" = "1" ]; then
100 if [ "$VERBOSE" = "1" ]; then
104 out=$(eval $cmd 2>&1)
106 if [ "$VERBOSE" = "1" -a -n "$out" ]; then
110 [ "$VERBOSE" = "1" ] && echo
121 addr=$(ip -netns $ns -6 -br addr show dev ${dev} | \
123 for (i = 3; i <= NF; ++i) {
131 [ -z "$addr" ] && return 1
138 ################################################################################
145 for ns in h1 h2 r1 r2; do
146 ip netns del $ns 2>/dev/null
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
159 ip -netns ${ns} addr add 127.0.0.1/8 dev ${VRF}
160 ip -netns ${ns} -6 addr add ::1 dev ${VRF} nodad
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
173 # create nodes as namespaces
175 for ns in h1 h2 r1 r2; do
177 ip -netns $ns li set lo up
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
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
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
196 # create interconnects
198 ip -netns h1 li add eth0 type veth peer name r1h1
199 ip -netns h1 li set r1h1 netns r1 name eth0 up
201 ip -netns h1 li add eth1 type veth peer name r2h1
202 ip -netns h1 li set r2h1 netns r2 name eth0 up
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
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
215 if [ "${WITH_VRF}" = "yes" ]; then
217 H1_VRF_ARG="vrf ${VRF}"
218 H1_PING_ARG="-I ${VRF}"
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
227 ip -netns h1 li set br0 up
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
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
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
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
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"
267 log_debug "initial gateway is R1's lladdr = ${R1_LLADDR}"
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"
274 log_debug "initial gateway is R2's lladdr = ${R2_LLADDR}"
281 run_cmd ip -netns h2 li set eth0 mtu ${mtu}
282 run_cmd ip -netns r2 li set eth2 mtu ${mtu}
288 local with_redirect="$2"
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}
298 if [ -n "${mtu}" ]; then
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}"
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"
314 log_test $? 0 "IPv4: ${desc}"
316 if [ "$with_redirect" = "yes" ]; then
317 ip -netns h1 -6 ro get ${H1_VRF_ARG} ${H2_N2_IP6} | \
318 grep -q "${H2_N2_IP6} from :: via ${R2_LLADDR} dev br0.*${mtu}"
319 elif [ -n "${mtu}" ]; then
320 ip -netns h1 -6 ro get ${H1_VRF_ARG} ${H2_N2_IP6} | \
323 # IPv6 is a bit harder. First strip out the match if it
324 # contains an mtu exception and then look for the first
325 # gateway - R1's lladdr
326 ip -netns h1 -6 ro get ${H1_VRF_ARG} ${H2_N2_IP6} | \
327 grep -v "mtu" | grep -q "${R1_LLADDR}"
329 log_test $? 0 "IPv6: ${desc}" 1
336 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}
337 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}
342 # r1 to h2 via r2 and eth0
343 run_cmd ip -netns r1 nexthop replace id 1 via ${R2_N1_IP} dev eth0
344 run_cmd ip -netns r1 nexthop replace id 2 via ${R2_LLADDR} dev eth0
349 run_cmd ip -netns r1 nexthop flush
350 run_cmd ip -netns h1 nexthop flush
357 # r1 to h2 via r2 and eth1
358 run_cmd ip -netns r1 nexthop add id 1 via ${R2_R1_N1_IP} dev eth1
359 run_cmd ip -netns r1 ro add ${H2_N2} nhid 1
361 run_cmd ip -netns r1 nexthop add id 2 via ${R2_R1_N1_IP6} dev eth1
362 run_cmd ip -netns r1 -6 ro add ${H2_N2_6} nhid 2
365 run_cmd ip -netns h1 nexthop add id 1 via ${R1_N1_IP} dev br0
366 run_cmd ip -netns h1 ro add ${H1_VRF_ARG} ${H2_N2} nhid 1
368 run_cmd ip -netns h1 nexthop add id 2 via ${R1_LLADDR} dev br0
369 run_cmd ip -netns h1 -6 ro add ${H1_VRF_ARG} ${H2_N2_6} nhid 2
372 replace_route_legacy()
374 # r1 to h2 via r2 and eth0
375 run_cmd ip -netns r1 ro replace ${H2_N2} via ${R2_N1_IP} dev eth0
376 run_cmd ip -netns r1 -6 ro replace ${H2_N2_6} via ${R2_LLADDR} dev eth0
381 run_cmd ip -netns r1 ro del ${H2_N2}
382 run_cmd ip -netns r1 -6 ro del ${H2_N2_6}
384 run_cmd ip -netns h1 ro del ${H1_VRF_ARG} ${H2_N2}
385 run_cmd ip -netns h1 -6 ro del ${H1_VRF_ARG} ${H2_N2_6}
390 initial_route_legacy()
392 # r1 to h2 via r2 and eth1
393 run_cmd ip -netns r1 ro add ${H2_N2} via ${R2_R1_N1_IP} dev eth1
394 run_cmd ip -netns r1 -6 ro add ${H2_N2_6} via ${R2_R1_N1_IP6} dev eth1
397 # - IPv6 redirect only works if gateway is the LLA
398 run_cmd ip -netns h1 ro add ${H1_VRF_ARG} ${H2_N2} via ${R1_N1_IP} dev br0
399 run_cmd ip -netns h1 -6 ro add ${H1_VRF_ARG} ${H2_N2_6} via ${R1_LLADDR} dev br0
406 run_cmd ip netns exec h1 ping -c1 -w1 ${H1_PING_ARG} ${H2_N2_IP}
408 run_cmd ip netns exec h1 ${ping6} -c1 -w1 ${H1_PING_ARG} ${H2_N2_IP6}
409 [ $? -ne 0 ] && rc=$?
418 eval initial_route_${ttype}
420 # verify connectivity
422 if [ $? -ne 0 ]; then
423 echo "Error: Basic connectivity is broken"
428 # redirect exception followed by mtu
429 eval replace_route_${ttype}
431 check_exception "" "yes" "redirect exception"
434 if [ $? -ne 0 ]; then
435 echo "Error: Basic connectivity is broken after redirect"
442 check_exception "1300" "yes" "redirect exception plus mtu"
444 # remove exceptions and restore routing
446 eval reset_route_${ttype}
449 if [ $? -ne 0 ]; then
450 echo "Error: Basic connectivity is broken after reset"
454 check_exception "" "no" "routing reset"
456 # MTU exception followed by redirect
459 check_exception "1300" "no" "mtu exception"
461 eval replace_route_${ttype}
463 check_exception "1300" "yes" "mtu exception plus redirect"
466 if [ $? -ne 0 ]; then
467 echo "Error: Basic connectivity is broken after redirect"
473 ################################################################################
482 -v verbose mode (show commands and output)
486 ################################################################################
489 # Some systems don't have a ping6 binary anymore
490 which ping6 > /dev/null 2>&1 && ping6=$(which ping6) || ping6=$(which ping)
500 p) PAUSE_ON_FAIL=yes;;
501 v) VERBOSE=$(($VERBOSE + 1));;
512 log_section "Legacy routing"
516 log_section "Legacy routing with VRF"
522 log_section "Routing with nexthop objects"
523 ip nexthop ls >/dev/null 2>&1
524 if [ $? -eq 0 ]; then
530 log_section "Routing with nexthop objects and VRF"
535 echo "Nexthop objects not supported; skipping tests"
538 printf "\nTests passed: %3d\n" ${nsuccess}
539 printf "Tests failed: %3d\n" ${nfail}
540 printf "Tests xfailed: %3d\n" ${nxfail}