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 # No PMTU info for test "redirect" and "mtu exception plus redirect"
317 if [ "$with_redirect" = "yes" ] && [ "$desc" != "redirect exception plus mtu" ]; then
318 ip -netns h1 -6 ro get ${H1_VRF_ARG} ${H2_N2_IP6} | \
319 grep -v "mtu" | grep -q "${H2_N2_IP6} .*via ${R2_LLADDR} dev br0"
320 elif [ -n "${mtu}" ]; then
321 ip -netns h1 -6 ro get ${H1_VRF_ARG} ${H2_N2_IP6} | \
324 # IPv6 is a bit harder. First strip out the match if it
325 # contains an mtu exception and then look for the first
326 # gateway - R1's lladdr
327 ip -netns h1 -6 ro get ${H1_VRF_ARG} ${H2_N2_IP6} | \
328 grep -v "mtu" | grep -q "${R1_LLADDR}"
330 log_test $? 0 "IPv6: ${desc}" 1
337 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}
338 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}
343 # r1 to h2 via r2 and eth0
344 run_cmd ip -netns r1 nexthop replace id 1 via ${R2_N1_IP} dev eth0
345 run_cmd ip -netns r1 nexthop replace id 2 via ${R2_LLADDR} dev eth0
350 run_cmd ip -netns r1 nexthop flush
351 run_cmd ip -netns h1 nexthop flush
358 # r1 to h2 via r2 and eth1
359 run_cmd ip -netns r1 nexthop add id 1 via ${R2_R1_N1_IP} dev eth1
360 run_cmd ip -netns r1 ro add ${H2_N2} nhid 1
362 run_cmd ip -netns r1 nexthop add id 2 via ${R2_R1_N1_IP6} dev eth1
363 run_cmd ip -netns r1 -6 ro add ${H2_N2_6} nhid 2
366 run_cmd ip -netns h1 nexthop add id 1 via ${R1_N1_IP} dev br0
367 run_cmd ip -netns h1 ro add ${H1_VRF_ARG} ${H2_N2} nhid 1
369 run_cmd ip -netns h1 nexthop add id 2 via ${R1_LLADDR} dev br0
370 run_cmd ip -netns h1 -6 ro add ${H1_VRF_ARG} ${H2_N2_6} nhid 2
373 replace_route_legacy()
375 # r1 to h2 via r2 and eth0
376 run_cmd ip -netns r1 ro replace ${H2_N2} via ${R2_N1_IP} dev eth0
377 run_cmd ip -netns r1 -6 ro replace ${H2_N2_6} via ${R2_LLADDR} dev eth0
382 run_cmd ip -netns r1 ro del ${H2_N2}
383 run_cmd ip -netns r1 -6 ro del ${H2_N2_6}
385 run_cmd ip -netns h1 ro del ${H1_VRF_ARG} ${H2_N2}
386 run_cmd ip -netns h1 -6 ro del ${H1_VRF_ARG} ${H2_N2_6}
391 initial_route_legacy()
393 # r1 to h2 via r2 and eth1
394 run_cmd ip -netns r1 ro add ${H2_N2} via ${R2_R1_N1_IP} dev eth1
395 run_cmd ip -netns r1 -6 ro add ${H2_N2_6} via ${R2_R1_N1_IP6} dev eth1
398 # - IPv6 redirect only works if gateway is the LLA
399 run_cmd ip -netns h1 ro add ${H1_VRF_ARG} ${H2_N2} via ${R1_N1_IP} dev br0
400 run_cmd ip -netns h1 -6 ro add ${H1_VRF_ARG} ${H2_N2_6} via ${R1_LLADDR} dev br0
407 run_cmd ip netns exec h1 ping -c1 -w1 ${H1_PING_ARG} ${H2_N2_IP}
409 run_cmd ip netns exec h1 ${ping6} -c1 -w1 ${H1_PING_ARG} ${H2_N2_IP6}
410 [ $? -ne 0 ] && rc=$?
419 eval initial_route_${ttype}
421 # verify connectivity
423 if [ $? -ne 0 ]; then
424 echo "Error: Basic connectivity is broken"
429 # redirect exception followed by mtu
430 eval replace_route_${ttype}
432 check_exception "" "yes" "redirect exception"
435 if [ $? -ne 0 ]; then
436 echo "Error: Basic connectivity is broken after redirect"
443 check_exception "1300" "yes" "redirect exception plus mtu"
445 # remove exceptions and restore routing
447 eval reset_route_${ttype}
450 if [ $? -ne 0 ]; then
451 echo "Error: Basic connectivity is broken after reset"
455 check_exception "" "no" "routing reset"
457 # MTU exception followed by redirect
460 check_exception "1300" "no" "mtu exception"
462 eval replace_route_${ttype}
464 check_exception "1300" "yes" "mtu exception plus redirect"
467 if [ $? -ne 0 ]; then
468 echo "Error: Basic connectivity is broken after redirect"
474 ################################################################################
483 -v verbose mode (show commands and output)
487 ################################################################################
490 # Some systems don't have a ping6 binary anymore
491 which ping6 > /dev/null 2>&1 && ping6=$(which ping6) || ping6=$(which ping)
501 p) PAUSE_ON_FAIL=yes;;
502 v) VERBOSE=$(($VERBOSE + 1));;
513 log_section "Legacy routing"
517 log_section "Legacy routing with VRF"
523 log_section "Routing with nexthop objects"
524 ip nexthop ls >/dev/null 2>&1
525 if [ $? -eq 0 ]; then
531 log_section "Routing with nexthop objects and VRF"
536 echo "Nexthop objects not supported; skipping tests"
539 printf "\nTests passed: %3d\n" ${nsuccess}
540 printf "Tests failed: %3d\n" ${nfail}
541 printf "Tests xfailed: %3d\n" ${nxfail}