1#!/bin/bash 2# SPDX-License-Identifier: GPL-2.0 3 4# This test is for checking IPv4 and IPv6 FIB behavior in response to 5# different events. 6 7ret=0 8# Kselftest framework requirement - SKIP code is 4. 9ksft_skip=4 10 11# all tests in this script. Can be overridden with -t option 12TESTS="unregister down carrier nexthop ipv6_rt ipv4_rt ipv6_addr_metric ipv4_addr_metric ipv6_route_metrics ipv4_route_metrics ipv4_route_v6_gw rp_filter" 13 14VERBOSE=0 15PAUSE_ON_FAIL=no 16PAUSE=no 17IP="ip -netns ns1" 18NS_EXEC="ip netns exec ns1" 19 20which ping6 > /dev/null 2>&1 && ping6=$(which ping6) || ping6=$(which ping) 21 22log_test() 23{ 24 local rc=$1 25 local expected=$2 26 local msg="$3" 27 28 if [ ${rc} -eq ${expected} ]; then 29 printf " TEST: %-60s [ OK ]\n" "${msg}" 30 nsuccess=$((nsuccess+1)) 31 else 32 ret=1 33 nfail=$((nfail+1)) 34 printf " TEST: %-60s [FAIL]\n" "${msg}" 35 if [ "${PAUSE_ON_FAIL}" = "yes" ]; then 36 echo 37 echo "hit enter to continue, 'q' to quit" 38 read a 39 [ "$a" = "q" ] && exit 1 40 fi 41 fi 42 43 if [ "${PAUSE}" = "yes" ]; then 44 echo 45 echo "hit enter to continue, 'q' to quit" 46 read a 47 [ "$a" = "q" ] && exit 1 48 fi 49} 50 51setup() 52{ 53 set -e 54 ip netns add ns1 55 ip netns set ns1 auto 56 $IP link set dev lo up 57 ip netns exec ns1 sysctl -qw net.ipv4.ip_forward=1 58 ip netns exec ns1 sysctl -qw net.ipv6.conf.all.forwarding=1 59 60 $IP link add dummy0 type dummy 61 $IP link set dev dummy0 up 62 $IP address add 198.51.100.1/24 dev dummy0 63 $IP -6 address add 2001:db8:1::1/64 dev dummy0 64 set +e 65 66} 67 68cleanup() 69{ 70 $IP link del dev dummy0 &> /dev/null 71 ip netns del ns1 72 ip netns del ns2 &> /dev/null 73} 74 75get_linklocal() 76{ 77 local dev=$1 78 local addr 79 80 addr=$($IP -6 -br addr show dev ${dev} | \ 81 awk '{ 82 for (i = 3; i <= NF; ++i) { 83 if ($i ~ /^fe80/) 84 print $i 85 } 86 }' 87 ) 88 addr=${addr/\/*} 89 90 [ -z "$addr" ] && return 1 91 92 echo $addr 93 94 return 0 95} 96 97fib_unreg_unicast_test() 98{ 99 echo 100 echo "Single path route test" 101 102 setup 103 104 echo " Start point" 105 $IP route get fibmatch 198.51.100.2 &> /dev/null 106 log_test $? 0 "IPv4 fibmatch" 107 $IP -6 route get fibmatch 2001:db8:1::2 &> /dev/null 108 log_test $? 0 "IPv6 fibmatch" 109 110 set -e 111 $IP link del dev dummy0 112 set +e 113 114 echo " Nexthop device deleted" 115 $IP route get fibmatch 198.51.100.2 &> /dev/null 116 log_test $? 2 "IPv4 fibmatch - no route" 117 $IP -6 route get fibmatch 2001:db8:1::2 &> /dev/null 118 log_test $? 2 "IPv6 fibmatch - no route" 119 120 cleanup 121} 122 123fib_unreg_multipath_test() 124{ 125 126 echo 127 echo "Multipath route test" 128 129 setup 130 131 set -e 132 $IP link add dummy1 type dummy 133 $IP link set dev dummy1 up 134 $IP address add 192.0.2.1/24 dev dummy1 135 $IP -6 address add 2001:db8:2::1/64 dev dummy1 136 137 $IP route add 203.0.113.0/24 \ 138 nexthop via 198.51.100.2 dev dummy0 \ 139 nexthop via 192.0.2.2 dev dummy1 140 $IP -6 route add 2001:db8:3::/64 \ 141 nexthop via 2001:db8:1::2 dev dummy0 \ 142 nexthop via 2001:db8:2::2 dev dummy1 143 set +e 144 145 echo " Start point" 146 $IP route get fibmatch 203.0.113.1 &> /dev/null 147 log_test $? 0 "IPv4 fibmatch" 148 $IP -6 route get fibmatch 2001:db8:3::1 &> /dev/null 149 log_test $? 0 "IPv6 fibmatch" 150 151 set -e 152 $IP link del dev dummy0 153 set +e 154 155 echo " One nexthop device deleted" 156 $IP route get fibmatch 203.0.113.1 &> /dev/null 157 log_test $? 2 "IPv4 - multipath route removed on delete" 158 159 $IP -6 route get fibmatch 2001:db8:3::1 &> /dev/null 160 # In IPv6 we do not flush the entire multipath route. 161 log_test $? 0 "IPv6 - multipath down to single path" 162 163 set -e 164 $IP link del dev dummy1 165 set +e 166 167 echo " Second nexthop device deleted" 168 $IP -6 route get fibmatch 2001:db8:3::1 &> /dev/null 169 log_test $? 2 "IPv6 - no route" 170 171 cleanup 172} 173 174fib_unreg_test() 175{ 176 fib_unreg_unicast_test 177 fib_unreg_multipath_test 178} 179 180fib_down_unicast_test() 181{ 182 echo 183 echo "Single path, admin down" 184 185 setup 186 187 echo " Start point" 188 $IP route get fibmatch 198.51.100.2 &> /dev/null 189 log_test $? 0 "IPv4 fibmatch" 190 $IP -6 route get fibmatch 2001:db8:1::2 &> /dev/null 191 log_test $? 0 "IPv6 fibmatch" 192 193 set -e 194 $IP link set dev dummy0 down 195 set +e 196 197 echo " Route deleted on down" 198 $IP route get fibmatch 198.51.100.2 &> /dev/null 199 log_test $? 2 "IPv4 fibmatch" 200 $IP -6 route get fibmatch 2001:db8:1::2 &> /dev/null 201 log_test $? 2 "IPv6 fibmatch" 202 203 cleanup 204} 205 206fib_down_multipath_test_do() 207{ 208 local down_dev=$1 209 local up_dev=$2 210 211 $IP route get fibmatch 203.0.113.1 \ 212 oif $down_dev &> /dev/null 213 log_test $? 2 "IPv4 fibmatch on down device" 214 $IP -6 route get fibmatch 2001:db8:3::1 \ 215 oif $down_dev &> /dev/null 216 log_test $? 2 "IPv6 fibmatch on down device" 217 218 $IP route get fibmatch 203.0.113.1 \ 219 oif $up_dev &> /dev/null 220 log_test $? 0 "IPv4 fibmatch on up device" 221 $IP -6 route get fibmatch 2001:db8:3::1 \ 222 oif $up_dev &> /dev/null 223 log_test $? 0 "IPv6 fibmatch on up device" 224 225 $IP route get fibmatch 203.0.113.1 | \ 226 grep $down_dev | grep -q "dead linkdown" 227 log_test $? 0 "IPv4 flags on down device" 228 $IP -6 route get fibmatch 2001:db8:3::1 | \ 229 grep $down_dev | grep -q "dead linkdown" 230 log_test $? 0 "IPv6 flags on down device" 231 232 $IP route get fibmatch 203.0.113.1 | \ 233 grep $up_dev | grep -q "dead linkdown" 234 log_test $? 1 "IPv4 flags on up device" 235 $IP -6 route get fibmatch 2001:db8:3::1 | \ 236 grep $up_dev | grep -q "dead linkdown" 237 log_test $? 1 "IPv6 flags on up device" 238} 239 240fib_down_multipath_test() 241{ 242 echo 243 echo "Admin down multipath" 244 245 setup 246 247 set -e 248 $IP link add dummy1 type dummy 249 $IP link set dev dummy1 up 250 251 $IP address add 192.0.2.1/24 dev dummy1 252 $IP -6 address add 2001:db8:2::1/64 dev dummy1 253 254 $IP route add 203.0.113.0/24 \ 255 nexthop via 198.51.100.2 dev dummy0 \ 256 nexthop via 192.0.2.2 dev dummy1 257 $IP -6 route add 2001:db8:3::/64 \ 258 nexthop via 2001:db8:1::2 dev dummy0 \ 259 nexthop via 2001:db8:2::2 dev dummy1 260 set +e 261 262 echo " Verify start point" 263 $IP route get fibmatch 203.0.113.1 &> /dev/null 264 log_test $? 0 "IPv4 fibmatch" 265 266 $IP -6 route get fibmatch 2001:db8:3::1 &> /dev/null 267 log_test $? 0 "IPv6 fibmatch" 268 269 set -e 270 $IP link set dev dummy0 down 271 set +e 272 273 echo " One device down, one up" 274 fib_down_multipath_test_do "dummy0" "dummy1" 275 276 set -e 277 $IP link set dev dummy0 up 278 $IP link set dev dummy1 down 279 set +e 280 281 echo " Other device down and up" 282 fib_down_multipath_test_do "dummy1" "dummy0" 283 284 set -e 285 $IP link set dev dummy0 down 286 set +e 287 288 echo " Both devices down" 289 $IP route get fibmatch 203.0.113.1 &> /dev/null 290 log_test $? 2 "IPv4 fibmatch" 291 $IP -6 route get fibmatch 2001:db8:3::1 &> /dev/null 292 log_test $? 2 "IPv6 fibmatch" 293 294 $IP link del dev dummy1 295 cleanup 296} 297 298fib_down_test() 299{ 300 fib_down_unicast_test 301 fib_down_multipath_test 302} 303 304# Local routes should not be affected when carrier changes. 305fib_carrier_local_test() 306{ 307 echo 308 echo "Local carrier tests - single path" 309 310 setup 311 312 set -e 313 $IP link set dev dummy0 carrier on 314 set +e 315 316 echo " Start point" 317 $IP route get fibmatch 198.51.100.1 &> /dev/null 318 log_test $? 0 "IPv4 fibmatch" 319 $IP -6 route get fibmatch 2001:db8:1::1 &> /dev/null 320 log_test $? 0 "IPv6 fibmatch" 321 322 $IP route get fibmatch 198.51.100.1 | \ 323 grep -q "linkdown" 324 log_test $? 1 "IPv4 - no linkdown flag" 325 $IP -6 route get fibmatch 2001:db8:1::1 | \ 326 grep -q "linkdown" 327 log_test $? 1 "IPv6 - no linkdown flag" 328 329 set -e 330 $IP link set dev dummy0 carrier off 331 sleep 1 332 set +e 333 334 echo " Carrier off on nexthop" 335 $IP route get fibmatch 198.51.100.1 &> /dev/null 336 log_test $? 0 "IPv4 fibmatch" 337 $IP -6 route get fibmatch 2001:db8:1::1 &> /dev/null 338 log_test $? 0 "IPv6 fibmatch" 339 340 $IP route get fibmatch 198.51.100.1 | \ 341 grep -q "linkdown" 342 log_test $? 1 "IPv4 - linkdown flag set" 343 $IP -6 route get fibmatch 2001:db8:1::1 | \ 344 grep -q "linkdown" 345 log_test $? 1 "IPv6 - linkdown flag set" 346 347 set -e 348 $IP address add 192.0.2.1/24 dev dummy0 349 $IP -6 address add 2001:db8:2::1/64 dev dummy0 350 set +e 351 352 echo " Route to local address with carrier down" 353 $IP route get fibmatch 192.0.2.1 &> /dev/null 354 log_test $? 0 "IPv4 fibmatch" 355 $IP -6 route get fibmatch 2001:db8:2::1 &> /dev/null 356 log_test $? 0 "IPv6 fibmatch" 357 358 $IP route get fibmatch 192.0.2.1 | \ 359 grep -q "linkdown" 360 log_test $? 1 "IPv4 linkdown flag set" 361 $IP -6 route get fibmatch 2001:db8:2::1 | \ 362 grep -q "linkdown" 363 log_test $? 1 "IPv6 linkdown flag set" 364 365 cleanup 366} 367 368fib_carrier_unicast_test() 369{ 370 ret=0 371 372 echo 373 echo "Single path route carrier test" 374 375 setup 376 377 set -e 378 $IP link set dev dummy0 carrier on 379 set +e 380 381 echo " Start point" 382 $IP route get fibmatch 198.51.100.2 &> /dev/null 383 log_test $? 0 "IPv4 fibmatch" 384 $IP -6 route get fibmatch 2001:db8:1::2 &> /dev/null 385 log_test $? 0 "IPv6 fibmatch" 386 387 $IP route get fibmatch 198.51.100.2 | \ 388 grep -q "linkdown" 389 log_test $? 1 "IPv4 no linkdown flag" 390 $IP -6 route get fibmatch 2001:db8:1::2 | \ 391 grep -q "linkdown" 392 log_test $? 1 "IPv6 no linkdown flag" 393 394 set -e 395 $IP link set dev dummy0 carrier off 396 sleep 1 397 set +e 398 399 echo " Carrier down" 400 $IP route get fibmatch 198.51.100.2 &> /dev/null 401 log_test $? 0 "IPv4 fibmatch" 402 $IP -6 route get fibmatch 2001:db8:1::2 &> /dev/null 403 log_test $? 0 "IPv6 fibmatch" 404 405 $IP route get fibmatch 198.51.100.2 | \ 406 grep -q "linkdown" 407 log_test $? 0 "IPv4 linkdown flag set" 408 $IP -6 route get fibmatch 2001:db8:1::2 | \ 409 grep -q "linkdown" 410 log_test $? 0 "IPv6 linkdown flag set" 411 412 set -e 413 $IP address add 192.0.2.1/24 dev dummy0 414 $IP -6 address add 2001:db8:2::1/64 dev dummy0 415 set +e 416 417 echo " Second address added with carrier down" 418 $IP route get fibmatch 192.0.2.2 &> /dev/null 419 log_test $? 0 "IPv4 fibmatch" 420 $IP -6 route get fibmatch 2001:db8:2::2 &> /dev/null 421 log_test $? 0 "IPv6 fibmatch" 422 423 $IP route get fibmatch 192.0.2.2 | \ 424 grep -q "linkdown" 425 log_test $? 0 "IPv4 linkdown flag set" 426 $IP -6 route get fibmatch 2001:db8:2::2 | \ 427 grep -q "linkdown" 428 log_test $? 0 "IPv6 linkdown flag set" 429 430 cleanup 431} 432 433fib_carrier_test() 434{ 435 fib_carrier_local_test 436 fib_carrier_unicast_test 437} 438 439fib_rp_filter_test() 440{ 441 echo 442 echo "IPv4 rp_filter tests" 443 444 setup 445 446 set -e 447 $IP link set dev lo address 52:54:00:6a:c7:5e 448 $IP link set dummy0 address 52:54:00:6a:c7:5e 449 $IP link add dummy1 type dummy 450 $IP link set dummy1 address 52:54:00:6a:c7:5e 451 $IP link set dev dummy1 up 452 $NS_EXEC sysctl -qw net.ipv4.conf.all.rp_filter=1 453 $NS_EXEC sysctl -qw net.ipv4.conf.all.accept_local=1 454 $NS_EXEC sysctl -qw net.ipv4.conf.all.route_localnet=1 455 456 $NS_EXEC tc qd add dev dummy1 parent root handle 1: fq_codel 457 $NS_EXEC tc filter add dev dummy1 parent 1: protocol arp basic action mirred egress redirect dev lo 458 $NS_EXEC tc filter add dev dummy1 parent 1: protocol ip basic action mirred egress redirect dev lo 459 set +e 460 461 run_cmd "ip netns exec ns1 ping -I dummy1 -w1 -c1 198.51.100.1" 462 log_test $? 0 "rp_filter passes local packets" 463 464 run_cmd "ip netns exec ns1 ping -I dummy1 -w1 -c1 127.0.0.1" 465 log_test $? 0 "rp_filter passes loopback packets" 466 467 cleanup 468} 469 470################################################################################ 471# Tests on nexthop spec 472 473# run 'ip route add' with given spec 474add_rt() 475{ 476 local desc="$1" 477 local erc=$2 478 local vrf=$3 479 local pfx=$4 480 local gw=$5 481 local dev=$6 482 local cmd out rc 483 484 [ "$vrf" = "-" ] && vrf="default" 485 [ -n "$gw" ] && gw="via $gw" 486 [ -n "$dev" ] && dev="dev $dev" 487 488 cmd="$IP route add vrf $vrf $pfx $gw $dev" 489 if [ "$VERBOSE" = "1" ]; then 490 printf "\n COMMAND: $cmd\n" 491 fi 492 493 out=$(eval $cmd 2>&1) 494 rc=$? 495 if [ "$VERBOSE" = "1" -a -n "$out" ]; then 496 echo " $out" 497 fi 498 log_test $rc $erc "$desc" 499} 500 501fib4_nexthop() 502{ 503 echo 504 echo "IPv4 nexthop tests" 505 506 echo "<<< write me >>>" 507} 508 509fib6_nexthop() 510{ 511 local lldummy=$(get_linklocal dummy0) 512 local llv1=$(get_linklocal dummy0) 513 514 if [ -z "$lldummy" ]; then 515 echo "Failed to get linklocal address for dummy0" 516 return 1 517 fi 518 if [ -z "$llv1" ]; then 519 echo "Failed to get linklocal address for veth1" 520 return 1 521 fi 522 523 echo 524 echo "IPv6 nexthop tests" 525 526 add_rt "Directly connected nexthop, unicast address" 0 \ 527 - 2001:db8:101::/64 2001:db8:1::2 528 add_rt "Directly connected nexthop, unicast address with device" 0 \ 529 - 2001:db8:102::/64 2001:db8:1::2 "dummy0" 530 add_rt "Gateway is linklocal address" 0 \ 531 - 2001:db8:103::1/64 $llv1 "veth0" 532 533 # fails because LL address requires a device 534 add_rt "Gateway is linklocal address, no device" 2 \ 535 - 2001:db8:104::1/64 $llv1 536 537 # local address can not be a gateway 538 add_rt "Gateway can not be local unicast address" 2 \ 539 - 2001:db8:105::/64 2001:db8:1::1 540 add_rt "Gateway can not be local unicast address, with device" 2 \ 541 - 2001:db8:106::/64 2001:db8:1::1 "dummy0" 542 add_rt "Gateway can not be a local linklocal address" 2 \ 543 - 2001:db8:107::1/64 $lldummy "dummy0" 544 545 # VRF tests 546 add_rt "Gateway can be local address in a VRF" 0 \ 547 - 2001:db8:108::/64 2001:db8:51::2 548 add_rt "Gateway can be local address in a VRF, with device" 0 \ 549 - 2001:db8:109::/64 2001:db8:51::2 "veth0" 550 add_rt "Gateway can be local linklocal address in a VRF" 0 \ 551 - 2001:db8:110::1/64 $llv1 "veth0" 552 553 add_rt "Redirect to VRF lookup" 0 \ 554 - 2001:db8:111::/64 "" "red" 555 556 add_rt "VRF route, gateway can be local address in default VRF" 0 \ 557 red 2001:db8:112::/64 2001:db8:51::1 558 559 # local address in same VRF fails 560 add_rt "VRF route, gateway can not be a local address" 2 \ 561 red 2001:db8:113::1/64 2001:db8:2::1 562 add_rt "VRF route, gateway can not be a local addr with device" 2 \ 563 red 2001:db8:114::1/64 2001:db8:2::1 "dummy1" 564} 565 566# Default VRF: 567# dummy0 - 198.51.100.1/24 2001:db8:1::1/64 568# veth0 - 192.0.2.1/24 2001:db8:51::1/64 569# 570# VRF red: 571# dummy1 - 192.168.2.1/24 2001:db8:2::1/64 572# veth1 - 192.0.2.2/24 2001:db8:51::2/64 573# 574# [ dummy0 veth0 ]--[ veth1 dummy1 ] 575 576fib_nexthop_test() 577{ 578 setup 579 580 set -e 581 582 $IP -4 rule add pref 32765 table local 583 $IP -4 rule del pref 0 584 $IP -6 rule add pref 32765 table local 585 $IP -6 rule del pref 0 586 587 $IP link add red type vrf table 1 588 $IP link set red up 589 $IP -4 route add vrf red unreachable default metric 4278198272 590 $IP -6 route add vrf red unreachable default metric 4278198272 591 592 $IP link add veth0 type veth peer name veth1 593 $IP link set dev veth0 up 594 $IP address add 192.0.2.1/24 dev veth0 595 $IP -6 address add 2001:db8:51::1/64 dev veth0 596 597 $IP link set dev veth1 vrf red up 598 $IP address add 192.0.2.2/24 dev veth1 599 $IP -6 address add 2001:db8:51::2/64 dev veth1 600 601 $IP link add dummy1 type dummy 602 $IP link set dev dummy1 vrf red up 603 $IP address add 192.168.2.1/24 dev dummy1 604 $IP -6 address add 2001:db8:2::1/64 dev dummy1 605 set +e 606 607 sleep 1 608 fib4_nexthop 609 fib6_nexthop 610 611 ( 612 $IP link del dev dummy1 613 $IP link del veth0 614 $IP link del red 615 ) 2>/dev/null 616 cleanup 617} 618 619################################################################################ 620# Tests on route add and replace 621 622run_cmd() 623{ 624 local cmd="$1" 625 local out 626 local stderr="2>/dev/null" 627 628 if [ "$VERBOSE" = "1" ]; then 629 printf " COMMAND: $cmd\n" 630 stderr= 631 fi 632 633 out=$(eval $cmd $stderr) 634 rc=$? 635 if [ "$VERBOSE" = "1" -a -n "$out" ]; then 636 echo " $out" 637 fi 638 639 [ "$VERBOSE" = "1" ] && echo 640 641 return $rc 642} 643 644check_expected() 645{ 646 local out="$1" 647 local expected="$2" 648 local rc=0 649 650 [ "${out}" = "${expected}" ] && return 0 651 652 if [ -z "${out}" ]; then 653 if [ "$VERBOSE" = "1" ]; then 654 printf "\nNo route entry found\n" 655 printf "Expected:\n" 656 printf " ${expected}\n" 657 fi 658 return 1 659 fi 660 661 # tricky way to convert output to 1-line without ip's 662 # messy '\'; this drops all extra white space 663 out=$(echo ${out}) 664 if [ "${out}" != "${expected}" ]; then 665 rc=1 666 if [ "${VERBOSE}" = "1" ]; then 667 printf " Unexpected route entry. Have:\n" 668 printf " ${out}\n" 669 printf " Expected:\n" 670 printf " ${expected}\n\n" 671 fi 672 fi 673 674 return $rc 675} 676 677# add route for a prefix, flushing any existing routes first 678# expected to be the first step of a test 679add_route6() 680{ 681 local pfx="$1" 682 local nh="$2" 683 local out 684 685 if [ "$VERBOSE" = "1" ]; then 686 echo 687 echo " ##################################################" 688 echo 689 fi 690 691 run_cmd "$IP -6 ro flush ${pfx}" 692 [ $? -ne 0 ] && exit 1 693 694 out=$($IP -6 ro ls match ${pfx}) 695 if [ -n "$out" ]; then 696 echo "Failed to flush routes for prefix used for tests." 697 exit 1 698 fi 699 700 run_cmd "$IP -6 ro add ${pfx} ${nh}" 701 if [ $? -ne 0 ]; then 702 echo "Failed to add initial route for test." 703 exit 1 704 fi 705} 706 707# add initial route - used in replace route tests 708add_initial_route6() 709{ 710 add_route6 "2001:db8:104::/64" "$1" 711} 712 713check_route6() 714{ 715 local pfx 716 local expected="$1" 717 local out 718 local rc=0 719 720 set -- $expected 721 pfx=$1 722 723 out=$($IP -6 ro ls match ${pfx} | sed -e 's/ pref medium//') 724 check_expected "${out}" "${expected}" 725} 726 727route_cleanup() 728{ 729 $IP li del red 2>/dev/null 730 $IP li del dummy1 2>/dev/null 731 $IP li del veth1 2>/dev/null 732 $IP li del veth3 2>/dev/null 733 734 cleanup &> /dev/null 735} 736 737route_setup() 738{ 739 route_cleanup 740 setup 741 742 [ "${VERBOSE}" = "1" ] && set -x 743 set -e 744 745 ip netns add ns2 746 ip netns set ns2 auto 747 ip -netns ns2 link set dev lo up 748 ip netns exec ns2 sysctl -qw net.ipv4.ip_forward=1 749 ip netns exec ns2 sysctl -qw net.ipv6.conf.all.forwarding=1 750 751 $IP li add veth1 type veth peer name veth2 752 $IP li add veth3 type veth peer name veth4 753 754 $IP li set veth1 up 755 $IP li set veth3 up 756 $IP li set veth2 netns ns2 up 757 $IP li set veth4 netns ns2 up 758 ip -netns ns2 li add dummy1 type dummy 759 ip -netns ns2 li set dummy1 up 760 761 $IP -6 addr add 2001:db8:101::1/64 dev veth1 nodad 762 $IP -6 addr add 2001:db8:103::1/64 dev veth3 nodad 763 $IP addr add 172.16.101.1/24 dev veth1 764 $IP addr add 172.16.103.1/24 dev veth3 765 766 ip -netns ns2 -6 addr add 2001:db8:101::2/64 dev veth2 nodad 767 ip -netns ns2 -6 addr add 2001:db8:103::2/64 dev veth4 nodad 768 ip -netns ns2 -6 addr add 2001:db8:104::1/64 dev dummy1 nodad 769 770 ip -netns ns2 addr add 172.16.101.2/24 dev veth2 771 ip -netns ns2 addr add 172.16.103.2/24 dev veth4 772 ip -netns ns2 addr add 172.16.104.1/24 dev dummy1 773 774 set +e 775} 776 777# assumption is that basic add of a single path route works 778# otherwise just adding an address on an interface is broken 779ipv6_rt_add() 780{ 781 local rc 782 783 echo 784 echo "IPv6 route add / append tests" 785 786 # route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL 787 add_route6 "2001:db8:104::/64" "via 2001:db8:101::2" 788 run_cmd "$IP -6 ro add 2001:db8:104::/64 via 2001:db8:103::2" 789 log_test $? 2 "Attempt to add duplicate route - gw" 790 791 # route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL 792 add_route6 "2001:db8:104::/64" "via 2001:db8:101::2" 793 run_cmd "$IP -6 ro add 2001:db8:104::/64 dev veth3" 794 log_test $? 2 "Attempt to add duplicate route - dev only" 795 796 # route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL 797 add_route6 "2001:db8:104::/64" "via 2001:db8:101::2" 798 run_cmd "$IP -6 ro add unreachable 2001:db8:104::/64" 799 log_test $? 2 "Attempt to add duplicate route - reject route" 800 801 # route append with same prefix adds a new route 802 # - iproute2 sets NLM_F_CREATE | NLM_F_APPEND 803 add_route6 "2001:db8:104::/64" "via 2001:db8:101::2" 804 run_cmd "$IP -6 ro append 2001:db8:104::/64 via 2001:db8:103::2" 805 check_route6 "2001:db8:104::/64 metric 1024 nexthop via 2001:db8:101::2 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1" 806 log_test $? 0 "Append nexthop to existing route - gw" 807 808 # insert mpath directly 809 add_route6 "2001:db8:104::/64" "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2" 810 check_route6 "2001:db8:104::/64 metric 1024 nexthop via 2001:db8:101::2 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1" 811 log_test $? 0 "Add multipath route" 812 813 add_route6 "2001:db8:104::/64" "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2" 814 run_cmd "$IP -6 ro add 2001:db8:104::/64 nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2" 815 log_test $? 2 "Attempt to add duplicate multipath route" 816 817 # insert of a second route without append but different metric 818 add_route6 "2001:db8:104::/64" "via 2001:db8:101::2" 819 run_cmd "$IP -6 ro add 2001:db8:104::/64 via 2001:db8:103::2 metric 512" 820 rc=$? 821 if [ $rc -eq 0 ]; then 822 run_cmd "$IP -6 ro add 2001:db8:104::/64 via 2001:db8:103::3 metric 256" 823 rc=$? 824 fi 825 log_test $rc 0 "Route add with different metrics" 826 827 run_cmd "$IP -6 ro del 2001:db8:104::/64 metric 512" 828 rc=$? 829 if [ $rc -eq 0 ]; then 830 check_route6 "2001:db8:104::/64 via 2001:db8:103::3 dev veth3 metric 256 2001:db8:104::/64 via 2001:db8:101::2 dev veth1 metric 1024" 831 rc=$? 832 fi 833 log_test $rc 0 "Route delete with metric" 834} 835 836ipv6_rt_replace_single() 837{ 838 # single path with single path 839 # 840 add_initial_route6 "via 2001:db8:101::2" 841 run_cmd "$IP -6 ro replace 2001:db8:104::/64 via 2001:db8:103::2" 842 check_route6 "2001:db8:104::/64 via 2001:db8:103::2 dev veth3 metric 1024" 843 log_test $? 0 "Single path with single path" 844 845 # single path with multipath 846 # 847 add_initial_route6 "nexthop via 2001:db8:101::2" 848 run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:101::3 nexthop via 2001:db8:103::2" 849 check_route6 "2001:db8:104::/64 metric 1024 nexthop via 2001:db8:101::3 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1" 850 log_test $? 0 "Single path with multipath" 851 852 # single path with single path using MULTIPATH attribute 853 # 854 add_initial_route6 "via 2001:db8:101::2" 855 run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:103::2" 856 check_route6 "2001:db8:104::/64 via 2001:db8:103::2 dev veth3 metric 1024" 857 log_test $? 0 "Single path with single path via multipath attribute" 858 859 # route replace fails - invalid nexthop 860 add_initial_route6 "via 2001:db8:101::2" 861 run_cmd "$IP -6 ro replace 2001:db8:104::/64 via 2001:db8:104::2" 862 if [ $? -eq 0 ]; then 863 # previous command is expected to fail so if it returns 0 864 # that means the test failed. 865 log_test 0 1 "Invalid nexthop" 866 else 867 check_route6 "2001:db8:104::/64 via 2001:db8:101::2 dev veth1 metric 1024" 868 log_test $? 0 "Invalid nexthop" 869 fi 870 871 # replace non-existent route 872 # - note use of change versus replace since ip adds NLM_F_CREATE 873 # for replace 874 add_initial_route6 "via 2001:db8:101::2" 875 run_cmd "$IP -6 ro change 2001:db8:105::/64 via 2001:db8:101::2" 876 log_test $? 2 "Single path - replace of non-existent route" 877} 878 879ipv6_rt_replace_mpath() 880{ 881 # multipath with multipath 882 add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2" 883 run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:101::3 nexthop via 2001:db8:103::3" 884 check_route6 "2001:db8:104::/64 metric 1024 nexthop via 2001:db8:101::3 dev veth1 weight 1 nexthop via 2001:db8:103::3 dev veth3 weight 1" 885 log_test $? 0 "Multipath with multipath" 886 887 # multipath with single 888 add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2" 889 run_cmd "$IP -6 ro replace 2001:db8:104::/64 via 2001:db8:101::3" 890 check_route6 "2001:db8:104::/64 via 2001:db8:101::3 dev veth1 metric 1024" 891 log_test $? 0 "Multipath with single path" 892 893 # multipath with single 894 add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2" 895 run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:101::3" 896 check_route6 "2001:db8:104::/64 via 2001:db8:101::3 dev veth1 metric 1024" 897 log_test $? 0 "Multipath with single path via multipath attribute" 898 899 # route replace fails - invalid nexthop 1 900 add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2" 901 run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:111::3 nexthop via 2001:db8:103::3" 902 check_route6 "2001:db8:104::/64 metric 1024 nexthop via 2001:db8:101::2 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1" 903 log_test $? 0 "Multipath - invalid first nexthop" 904 905 # route replace fails - invalid nexthop 2 906 add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2" 907 run_cmd "$IP -6 ro replace 2001:db8:104::/64 nexthop via 2001:db8:101::3 nexthop via 2001:db8:113::3" 908 check_route6 "2001:db8:104::/64 metric 1024 nexthop via 2001:db8:101::2 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1" 909 log_test $? 0 "Multipath - invalid second nexthop" 910 911 # multipath non-existent route 912 add_initial_route6 "nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2" 913 run_cmd "$IP -6 ro change 2001:db8:105::/64 nexthop via 2001:db8:101::3 nexthop via 2001:db8:103::3" 914 log_test $? 2 "Multipath - replace of non-existent route" 915} 916 917ipv6_rt_replace() 918{ 919 echo 920 echo "IPv6 route replace tests" 921 922 ipv6_rt_replace_single 923 ipv6_rt_replace_mpath 924} 925 926ipv6_route_test() 927{ 928 route_setup 929 930 ipv6_rt_add 931 ipv6_rt_replace 932 933 route_cleanup 934} 935 936ip_addr_metric_check() 937{ 938 ip addr help 2>&1 | grep -q metric 939 if [ $? -ne 0 ]; then 940 echo "iproute2 command does not support metric for addresses. Skipping test" 941 return 1 942 fi 943 944 return 0 945} 946 947ipv6_addr_metric_test() 948{ 949 local rc 950 951 echo 952 echo "IPv6 prefix route tests" 953 954 ip_addr_metric_check || return 1 955 956 setup 957 958 set -e 959 $IP li add dummy1 type dummy 960 $IP li add dummy2 type dummy 961 $IP li set dummy1 up 962 $IP li set dummy2 up 963 964 # default entry is metric 256 965 run_cmd "$IP -6 addr add dev dummy1 2001:db8:104::1/64" 966 run_cmd "$IP -6 addr add dev dummy2 2001:db8:104::2/64" 967 set +e 968 969 check_route6 "2001:db8:104::/64 dev dummy1 proto kernel metric 256 2001:db8:104::/64 dev dummy2 proto kernel metric 256" 970 log_test $? 0 "Default metric" 971 972 set -e 973 run_cmd "$IP -6 addr flush dev dummy1" 974 run_cmd "$IP -6 addr add dev dummy1 2001:db8:104::1/64 metric 257" 975 set +e 976 977 check_route6 "2001:db8:104::/64 dev dummy2 proto kernel metric 256 2001:db8:104::/64 dev dummy1 proto kernel metric 257" 978 log_test $? 0 "User specified metric on first device" 979 980 set -e 981 run_cmd "$IP -6 addr flush dev dummy2" 982 run_cmd "$IP -6 addr add dev dummy2 2001:db8:104::2/64 metric 258" 983 set +e 984 985 check_route6 "2001:db8:104::/64 dev dummy1 proto kernel metric 257 2001:db8:104::/64 dev dummy2 proto kernel metric 258" 986 log_test $? 0 "User specified metric on second device" 987 988 run_cmd "$IP -6 addr del dev dummy1 2001:db8:104::1/64 metric 257" 989 rc=$? 990 if [ $rc -eq 0 ]; then 991 check_route6 "2001:db8:104::/64 dev dummy2 proto kernel metric 258" 992 rc=$? 993 fi 994 log_test $rc 0 "Delete of address on first device" 995 996 run_cmd "$IP -6 addr change dev dummy2 2001:db8:104::2/64 metric 259" 997 rc=$? 998 if [ $rc -eq 0 ]; then 999 check_route6 "2001:db8:104::/64 dev dummy2 proto kernel metric 259" 1000 rc=$? 1001 fi 1002 log_test $rc 0 "Modify metric of address" 1003 1004 # verify prefix route removed on down 1005 run_cmd "ip netns exec ns1 sysctl -qw net.ipv6.conf.all.keep_addr_on_down=1" 1006 run_cmd "$IP li set dev dummy2 down" 1007 rc=$? 1008 if [ $rc -eq 0 ]; then 1009 out=$($IP -6 ro ls match 2001:db8:104::/64) 1010 check_expected "${out}" "" 1011 rc=$? 1012 fi 1013 log_test $rc 0 "Prefix route removed on link down" 1014 1015 # verify prefix route re-inserted with assigned metric 1016 run_cmd "$IP li set dev dummy2 up" 1017 rc=$? 1018 if [ $rc -eq 0 ]; then 1019 check_route6 "2001:db8:104::/64 dev dummy2 proto kernel metric 259" 1020 rc=$? 1021 fi 1022 log_test $rc 0 "Prefix route with metric on link up" 1023 1024 $IP li del dummy1 1025 $IP li del dummy2 1026 cleanup 1027} 1028 1029ipv6_route_metrics_test() 1030{ 1031 local rc 1032 1033 echo 1034 echo "IPv6 routes with metrics" 1035 1036 route_setup 1037 1038 # 1039 # single path with metrics 1040 # 1041 run_cmd "$IP -6 ro add 2001:db8:111::/64 via 2001:db8:101::2 mtu 1400" 1042 rc=$? 1043 if [ $rc -eq 0 ]; then 1044 check_route6 "2001:db8:111::/64 via 2001:db8:101::2 dev veth1 metric 1024 mtu 1400" 1045 rc=$? 1046 fi 1047 log_test $rc 0 "Single path route with mtu metric" 1048 1049 1050 # 1051 # multipath via separate routes with metrics 1052 # 1053 run_cmd "$IP -6 ro add 2001:db8:112::/64 via 2001:db8:101::2 mtu 1400" 1054 run_cmd "$IP -6 ro append 2001:db8:112::/64 via 2001:db8:103::2" 1055 rc=$? 1056 if [ $rc -eq 0 ]; then 1057 check_route6 "2001:db8:112::/64 metric 1024 mtu 1400 nexthop via 2001:db8:101::2 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1" 1058 rc=$? 1059 fi 1060 log_test $rc 0 "Multipath route via 2 single routes with mtu metric on first" 1061 1062 # second route is coalesced to first to make a multipath route. 1063 # MTU of the second path is hidden from display! 1064 run_cmd "$IP -6 ro add 2001:db8:113::/64 via 2001:db8:101::2" 1065 run_cmd "$IP -6 ro append 2001:db8:113::/64 via 2001:db8:103::2 mtu 1400" 1066 rc=$? 1067 if [ $rc -eq 0 ]; then 1068 check_route6 "2001:db8:113::/64 metric 1024 nexthop via 2001:db8:101::2 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1" 1069 rc=$? 1070 fi 1071 log_test $rc 0 "Multipath route via 2 single routes with mtu metric on 2nd" 1072 1073 run_cmd "$IP -6 ro del 2001:db8:113::/64 via 2001:db8:101::2" 1074 if [ $? -eq 0 ]; then 1075 check_route6 "2001:db8:113::/64 via 2001:db8:103::2 dev veth3 metric 1024 mtu 1400" 1076 log_test $? 0 " MTU of second leg" 1077 fi 1078 1079 # 1080 # multipath with metrics 1081 # 1082 run_cmd "$IP -6 ro add 2001:db8:115::/64 mtu 1400 nexthop via 2001:db8:101::2 nexthop via 2001:db8:103::2" 1083 rc=$? 1084 if [ $rc -eq 0 ]; then 1085 check_route6 "2001:db8:115::/64 metric 1024 mtu 1400 nexthop via 2001:db8:101::2 dev veth1 weight 1 nexthop via 2001:db8:103::2 dev veth3 weight 1" 1086 rc=$? 1087 fi 1088 log_test $rc 0 "Multipath route with mtu metric" 1089 1090 $IP -6 ro add 2001:db8:104::/64 via 2001:db8:101::2 mtu 1300 1091 run_cmd "ip netns exec ns1 ${ping6} -w1 -c1 -s 1500 2001:db8:104::1" 1092 log_test $? 0 "Using route with mtu metric" 1093 1094 run_cmd "$IP -6 ro add 2001:db8:114::/64 via 2001:db8:101::2 congctl lock foo" 1095 log_test $? 2 "Invalid metric (fails metric_convert)" 1096 1097 route_cleanup 1098} 1099 1100# add route for a prefix, flushing any existing routes first 1101# expected to be the first step of a test 1102add_route() 1103{ 1104 local pfx="$1" 1105 local nh="$2" 1106 local out 1107 1108 if [ "$VERBOSE" = "1" ]; then 1109 echo 1110 echo " ##################################################" 1111 echo 1112 fi 1113 1114 run_cmd "$IP ro flush ${pfx}" 1115 [ $? -ne 0 ] && exit 1 1116 1117 out=$($IP ro ls match ${pfx}) 1118 if [ -n "$out" ]; then 1119 echo "Failed to flush routes for prefix used for tests." 1120 exit 1 1121 fi 1122 1123 run_cmd "$IP ro add ${pfx} ${nh}" 1124 if [ $? -ne 0 ]; then 1125 echo "Failed to add initial route for test." 1126 exit 1 1127 fi 1128} 1129 1130# add initial route - used in replace route tests 1131add_initial_route() 1132{ 1133 add_route "172.16.104.0/24" "$1" 1134} 1135 1136check_route() 1137{ 1138 local pfx 1139 local expected="$1" 1140 local out 1141 1142 set -- $expected 1143 pfx=$1 1144 [ "${pfx}" = "unreachable" ] && pfx=$2 1145 1146 out=$($IP ro ls match ${pfx}) 1147 check_expected "${out}" "${expected}" 1148} 1149 1150# assumption is that basic add of a single path route works 1151# otherwise just adding an address on an interface is broken 1152ipv4_rt_add() 1153{ 1154 local rc 1155 1156 echo 1157 echo "IPv4 route add / append tests" 1158 1159 # route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL 1160 add_route "172.16.104.0/24" "via 172.16.101.2" 1161 run_cmd "$IP ro add 172.16.104.0/24 via 172.16.103.2" 1162 log_test $? 2 "Attempt to add duplicate route - gw" 1163 1164 # route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL 1165 add_route "172.16.104.0/24" "via 172.16.101.2" 1166 run_cmd "$IP ro add 172.16.104.0/24 dev veth3" 1167 log_test $? 2 "Attempt to add duplicate route - dev only" 1168 1169 # route add same prefix - fails with EEXISTS b/c ip adds NLM_F_EXCL 1170 add_route "172.16.104.0/24" "via 172.16.101.2" 1171 run_cmd "$IP ro add unreachable 172.16.104.0/24" 1172 log_test $? 2 "Attempt to add duplicate route - reject route" 1173 1174 # iproute2 prepend only sets NLM_F_CREATE 1175 # - adds a new route; does NOT convert existing route to ECMP 1176 add_route "172.16.104.0/24" "via 172.16.101.2" 1177 run_cmd "$IP ro prepend 172.16.104.0/24 via 172.16.103.2" 1178 check_route "172.16.104.0/24 via 172.16.103.2 dev veth3 172.16.104.0/24 via 172.16.101.2 dev veth1" 1179 log_test $? 0 "Add new nexthop for existing prefix" 1180 1181 # route append with same prefix adds a new route 1182 # - iproute2 sets NLM_F_CREATE | NLM_F_APPEND 1183 add_route "172.16.104.0/24" "via 172.16.101.2" 1184 run_cmd "$IP ro append 172.16.104.0/24 via 172.16.103.2" 1185 check_route "172.16.104.0/24 via 172.16.101.2 dev veth1 172.16.104.0/24 via 172.16.103.2 dev veth3" 1186 log_test $? 0 "Append nexthop to existing route - gw" 1187 1188 add_route "172.16.104.0/24" "via 172.16.101.2" 1189 run_cmd "$IP ro append 172.16.104.0/24 dev veth3" 1190 check_route "172.16.104.0/24 via 172.16.101.2 dev veth1 172.16.104.0/24 dev veth3 scope link" 1191 log_test $? 0 "Append nexthop to existing route - dev only" 1192 1193 add_route "172.16.104.0/24" "via 172.16.101.2" 1194 run_cmd "$IP ro append unreachable 172.16.104.0/24" 1195 check_route "172.16.104.0/24 via 172.16.101.2 dev veth1 unreachable 172.16.104.0/24" 1196 log_test $? 0 "Append nexthop to existing route - reject route" 1197 1198 run_cmd "$IP ro flush 172.16.104.0/24" 1199 run_cmd "$IP ro add unreachable 172.16.104.0/24" 1200 run_cmd "$IP ro append 172.16.104.0/24 via 172.16.103.2" 1201 check_route "unreachable 172.16.104.0/24 172.16.104.0/24 via 172.16.103.2 dev veth3" 1202 log_test $? 0 "Append nexthop to existing reject route - gw" 1203 1204 run_cmd "$IP ro flush 172.16.104.0/24" 1205 run_cmd "$IP ro add unreachable 172.16.104.0/24" 1206 run_cmd "$IP ro append 172.16.104.0/24 dev veth3" 1207 check_route "unreachable 172.16.104.0/24 172.16.104.0/24 dev veth3 scope link" 1208 log_test $? 0 "Append nexthop to existing reject route - dev only" 1209 1210 # insert mpath directly 1211 add_route "172.16.104.0/24" "nexthop via 172.16.101.2 nexthop via 172.16.103.2" 1212 check_route "172.16.104.0/24 nexthop via 172.16.101.2 dev veth1 weight 1 nexthop via 172.16.103.2 dev veth3 weight 1" 1213 log_test $? 0 "add multipath route" 1214 1215 add_route "172.16.104.0/24" "nexthop via 172.16.101.2 nexthop via 172.16.103.2" 1216 run_cmd "$IP ro add 172.16.104.0/24 nexthop via 172.16.101.2 nexthop via 172.16.103.2" 1217 log_test $? 2 "Attempt to add duplicate multipath route" 1218 1219 # insert of a second route without append but different metric 1220 add_route "172.16.104.0/24" "via 172.16.101.2" 1221 run_cmd "$IP ro add 172.16.104.0/24 via 172.16.103.2 metric 512" 1222 rc=$? 1223 if [ $rc -eq 0 ]; then 1224 run_cmd "$IP ro add 172.16.104.0/24 via 172.16.103.3 metric 256" 1225 rc=$? 1226 fi 1227 log_test $rc 0 "Route add with different metrics" 1228 1229 run_cmd "$IP ro del 172.16.104.0/24 metric 512" 1230 rc=$? 1231 if [ $rc -eq 0 ]; then 1232 check_route "172.16.104.0/24 via 172.16.101.2 dev veth1 172.16.104.0/24 via 172.16.103.3 dev veth3 metric 256" 1233 rc=$? 1234 fi 1235 log_test $rc 0 "Route delete with metric" 1236} 1237 1238ipv4_rt_replace_single() 1239{ 1240 # single path with single path 1241 # 1242 add_initial_route "via 172.16.101.2" 1243 run_cmd "$IP ro replace 172.16.104.0/24 via 172.16.103.2" 1244 check_route "172.16.104.0/24 via 172.16.103.2 dev veth3" 1245 log_test $? 0 "Single path with single path" 1246 1247 # single path with multipath 1248 # 1249 add_initial_route "nexthop via 172.16.101.2" 1250 run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.101.3 nexthop via 172.16.103.2" 1251 check_route "172.16.104.0/24 nexthop via 172.16.101.3 dev veth1 weight 1 nexthop via 172.16.103.2 dev veth3 weight 1" 1252 log_test $? 0 "Single path with multipath" 1253 1254 # single path with reject 1255 # 1256 add_initial_route "nexthop via 172.16.101.2" 1257 run_cmd "$IP ro replace unreachable 172.16.104.0/24" 1258 check_route "unreachable 172.16.104.0/24" 1259 log_test $? 0 "Single path with reject route" 1260 1261 # single path with single path using MULTIPATH attribute 1262 # 1263 add_initial_route "via 172.16.101.2" 1264 run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.103.2" 1265 check_route "172.16.104.0/24 via 172.16.103.2 dev veth3" 1266 log_test $? 0 "Single path with single path via multipath attribute" 1267 1268 # route replace fails - invalid nexthop 1269 add_initial_route "via 172.16.101.2" 1270 run_cmd "$IP ro replace 172.16.104.0/24 via 2001:db8:104::2" 1271 if [ $? -eq 0 ]; then 1272 # previous command is expected to fail so if it returns 0 1273 # that means the test failed. 1274 log_test 0 1 "Invalid nexthop" 1275 else 1276 check_route "172.16.104.0/24 via 172.16.101.2 dev veth1" 1277 log_test $? 0 "Invalid nexthop" 1278 fi 1279 1280 # replace non-existent route 1281 # - note use of change versus replace since ip adds NLM_F_CREATE 1282 # for replace 1283 add_initial_route "via 172.16.101.2" 1284 run_cmd "$IP ro change 172.16.105.0/24 via 172.16.101.2" 1285 log_test $? 2 "Single path - replace of non-existent route" 1286} 1287 1288ipv4_rt_replace_mpath() 1289{ 1290 # multipath with multipath 1291 add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2" 1292 run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.101.3 nexthop via 172.16.103.3" 1293 check_route "172.16.104.0/24 nexthop via 172.16.101.3 dev veth1 weight 1 nexthop via 172.16.103.3 dev veth3 weight 1" 1294 log_test $? 0 "Multipath with multipath" 1295 1296 # multipath with single 1297 add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2" 1298 run_cmd "$IP ro replace 172.16.104.0/24 via 172.16.101.3" 1299 check_route "172.16.104.0/24 via 172.16.101.3 dev veth1" 1300 log_test $? 0 "Multipath with single path" 1301 1302 # multipath with single 1303 add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2" 1304 run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.101.3" 1305 check_route "172.16.104.0/24 via 172.16.101.3 dev veth1" 1306 log_test $? 0 "Multipath with single path via multipath attribute" 1307 1308 # multipath with reject 1309 add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2" 1310 run_cmd "$IP ro replace unreachable 172.16.104.0/24" 1311 check_route "unreachable 172.16.104.0/24" 1312 log_test $? 0 "Multipath with reject route" 1313 1314 # route replace fails - invalid nexthop 1 1315 add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2" 1316 run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.111.3 nexthop via 172.16.103.3" 1317 check_route "172.16.104.0/24 nexthop via 172.16.101.2 dev veth1 weight 1 nexthop via 172.16.103.2 dev veth3 weight 1" 1318 log_test $? 0 "Multipath - invalid first nexthop" 1319 1320 # route replace fails - invalid nexthop 2 1321 add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2" 1322 run_cmd "$IP ro replace 172.16.104.0/24 nexthop via 172.16.101.3 nexthop via 172.16.113.3" 1323 check_route "172.16.104.0/24 nexthop via 172.16.101.2 dev veth1 weight 1 nexthop via 172.16.103.2 dev veth3 weight 1" 1324 log_test $? 0 "Multipath - invalid second nexthop" 1325 1326 # multipath non-existent route 1327 add_initial_route "nexthop via 172.16.101.2 nexthop via 172.16.103.2" 1328 run_cmd "$IP ro change 172.16.105.0/24 nexthop via 172.16.101.3 nexthop via 172.16.103.3" 1329 log_test $? 2 "Multipath - replace of non-existent route" 1330} 1331 1332ipv4_rt_replace() 1333{ 1334 echo 1335 echo "IPv4 route replace tests" 1336 1337 ipv4_rt_replace_single 1338 ipv4_rt_replace_mpath 1339} 1340 1341ipv4_route_test() 1342{ 1343 route_setup 1344 1345 ipv4_rt_add 1346 ipv4_rt_replace 1347 1348 route_cleanup 1349} 1350 1351ipv4_addr_metric_test() 1352{ 1353 local rc 1354 1355 echo 1356 echo "IPv4 prefix route tests" 1357 1358 ip_addr_metric_check || return 1 1359 1360 setup 1361 1362 set -e 1363 $IP li add dummy1 type dummy 1364 $IP li add dummy2 type dummy 1365 $IP li set dummy1 up 1366 $IP li set dummy2 up 1367 1368 # default entry is metric 256 1369 run_cmd "$IP addr add dev dummy1 172.16.104.1/24" 1370 run_cmd "$IP addr add dev dummy2 172.16.104.2/24" 1371 set +e 1372 1373 check_route "172.16.104.0/24 dev dummy1 proto kernel scope link src 172.16.104.1 172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2" 1374 log_test $? 0 "Default metric" 1375 1376 set -e 1377 run_cmd "$IP addr flush dev dummy1" 1378 run_cmd "$IP addr add dev dummy1 172.16.104.1/24 metric 257" 1379 set +e 1380 1381 check_route "172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2 172.16.104.0/24 dev dummy1 proto kernel scope link src 172.16.104.1 metric 257" 1382 log_test $? 0 "User specified metric on first device" 1383 1384 set -e 1385 run_cmd "$IP addr flush dev dummy2" 1386 run_cmd "$IP addr add dev dummy2 172.16.104.2/24 metric 258" 1387 set +e 1388 1389 check_route "172.16.104.0/24 dev dummy1 proto kernel scope link src 172.16.104.1 metric 257 172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2 metric 258" 1390 log_test $? 0 "User specified metric on second device" 1391 1392 run_cmd "$IP addr del dev dummy1 172.16.104.1/24 metric 257" 1393 rc=$? 1394 if [ $rc -eq 0 ]; then 1395 check_route "172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2 metric 258" 1396 rc=$? 1397 fi 1398 log_test $rc 0 "Delete of address on first device" 1399 1400 run_cmd "$IP addr change dev dummy2 172.16.104.2/24 metric 259" 1401 rc=$? 1402 if [ $rc -eq 0 ]; then 1403 check_route "172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2 metric 259" 1404 rc=$? 1405 fi 1406 log_test $rc 0 "Modify metric of address" 1407 1408 # verify prefix route removed on down 1409 run_cmd "$IP li set dev dummy2 down" 1410 rc=$? 1411 if [ $rc -eq 0 ]; then 1412 out=$($IP ro ls match 172.16.104.0/24) 1413 check_expected "${out}" "" 1414 rc=$? 1415 fi 1416 log_test $rc 0 "Prefix route removed on link down" 1417 1418 # verify prefix route re-inserted with assigned metric 1419 run_cmd "$IP li set dev dummy2 up" 1420 rc=$? 1421 if [ $rc -eq 0 ]; then 1422 check_route "172.16.104.0/24 dev dummy2 proto kernel scope link src 172.16.104.2 metric 259" 1423 rc=$? 1424 fi 1425 log_test $rc 0 "Prefix route with metric on link up" 1426 1427 $IP li del dummy1 1428 $IP li del dummy2 1429 cleanup 1430} 1431 1432ipv4_route_metrics_test() 1433{ 1434 local rc 1435 1436 echo 1437 echo "IPv4 route add / append tests" 1438 1439 route_setup 1440 1441 run_cmd "$IP ro add 172.16.111.0/24 via 172.16.101.2 mtu 1400" 1442 rc=$? 1443 if [ $rc -eq 0 ]; then 1444 check_route "172.16.111.0/24 via 172.16.101.2 dev veth1 mtu 1400" 1445 rc=$? 1446 fi 1447 log_test $rc 0 "Single path route with mtu metric" 1448 1449 1450 run_cmd "$IP ro add 172.16.112.0/24 mtu 1400 nexthop via 172.16.101.2 nexthop via 172.16.103.2" 1451 rc=$? 1452 if [ $rc -eq 0 ]; then 1453 check_route "172.16.112.0/24 mtu 1400 nexthop via 172.16.101.2 dev veth1 weight 1 nexthop via 172.16.103.2 dev veth3 weight 1" 1454 rc=$? 1455 fi 1456 log_test $rc 0 "Multipath route with mtu metric" 1457 1458 $IP ro add 172.16.104.0/24 via 172.16.101.2 mtu 1300 1459 run_cmd "ip netns exec ns1 ping -w1 -c1 -s 1500 172.16.104.1" 1460 log_test $? 0 "Using route with mtu metric" 1461 1462 run_cmd "$IP ro add 172.16.111.0/24 via 172.16.101.2 congctl lock foo" 1463 log_test $? 2 "Invalid metric (fails metric_convert)" 1464 1465 route_cleanup 1466} 1467 1468ipv4_route_v6_gw_test() 1469{ 1470 local rc 1471 1472 echo 1473 echo "IPv4 route with IPv6 gateway tests" 1474 1475 route_setup 1476 sleep 2 1477 1478 # 1479 # single path route 1480 # 1481 run_cmd "$IP ro add 172.16.104.0/24 via inet6 2001:db8:101::2" 1482 rc=$? 1483 log_test $rc 0 "Single path route with IPv6 gateway" 1484 if [ $rc -eq 0 ]; then 1485 check_route "172.16.104.0/24 via inet6 2001:db8:101::2 dev veth1" 1486 fi 1487 1488 run_cmd "ip netns exec ns1 ping -w1 -c1 172.16.104.1" 1489 log_test $rc 0 "Single path route with IPv6 gateway - ping" 1490 1491 run_cmd "$IP ro del 172.16.104.0/24 via inet6 2001:db8:101::2" 1492 rc=$? 1493 log_test $rc 0 "Single path route delete" 1494 if [ $rc -eq 0 ]; then 1495 check_route "172.16.112.0/24" 1496 fi 1497 1498 # 1499 # multipath - v6 then v4 1500 # 1501 run_cmd "$IP ro add 172.16.104.0/24 nexthop via inet6 2001:db8:101::2 dev veth1 nexthop via 172.16.103.2 dev veth3" 1502 rc=$? 1503 log_test $rc 0 "Multipath route add - v6 nexthop then v4" 1504 if [ $rc -eq 0 ]; then 1505 check_route "172.16.104.0/24 nexthop via inet6 2001:db8:101::2 dev veth1 weight 1 nexthop via 172.16.103.2 dev veth3 weight 1" 1506 fi 1507 1508 run_cmd "$IP ro del 172.16.104.0/24 nexthop via 172.16.103.2 dev veth3 nexthop via inet6 2001:db8:101::2 dev veth1" 1509 log_test $? 2 " Multipath route delete - nexthops in wrong order" 1510 1511 run_cmd "$IP ro del 172.16.104.0/24 nexthop via inet6 2001:db8:101::2 dev veth1 nexthop via 172.16.103.2 dev veth3" 1512 log_test $? 0 " Multipath route delete exact match" 1513 1514 # 1515 # multipath - v4 then v6 1516 # 1517 run_cmd "$IP ro add 172.16.104.0/24 nexthop via 172.16.103.2 dev veth3 nexthop via inet6 2001:db8:101::2 dev veth1" 1518 rc=$? 1519 log_test $rc 0 "Multipath route add - v4 nexthop then v6" 1520 if [ $rc -eq 0 ]; then 1521 check_route "172.16.104.0/24 nexthop via 172.16.103.2 dev veth3 weight 1 nexthop via inet6 2001:db8:101::2 dev veth1 weight 1" 1522 fi 1523 1524 run_cmd "$IP ro del 172.16.104.0/24 nexthop via inet6 2001:db8:101::2 dev veth1 nexthop via 172.16.103.2 dev veth3" 1525 log_test $? 2 " Multipath route delete - nexthops in wrong order" 1526 1527 run_cmd "$IP ro del 172.16.104.0/24 nexthop via 172.16.103.2 dev veth3 nexthop via inet6 2001:db8:101::2 dev veth1" 1528 log_test $? 0 " Multipath route delete exact match" 1529 1530 route_cleanup 1531} 1532 1533################################################################################ 1534# usage 1535 1536usage() 1537{ 1538 cat <<EOF 1539usage: ${0##*/} OPTS 1540 1541 -t <test> Test(s) to run (default: all) 1542 (options: $TESTS) 1543 -p Pause on fail 1544 -P Pause after each test before cleanup 1545 -v verbose mode (show commands and output) 1546EOF 1547} 1548 1549################################################################################ 1550# main 1551 1552while getopts :t:pPhv o 1553do 1554 case $o in 1555 t) TESTS=$OPTARG;; 1556 p) PAUSE_ON_FAIL=yes;; 1557 P) PAUSE=yes;; 1558 v) VERBOSE=$(($VERBOSE + 1));; 1559 h) usage; exit 0;; 1560 *) usage; exit 1;; 1561 esac 1562done 1563 1564PEER_CMD="ip netns exec ${PEER_NS}" 1565 1566# make sure we don't pause twice 1567[ "${PAUSE}" = "yes" ] && PAUSE_ON_FAIL=no 1568 1569if [ "$(id -u)" -ne 0 ];then 1570 echo "SKIP: Need root privileges" 1571 exit $ksft_skip; 1572fi 1573 1574if [ ! -x "$(command -v ip)" ]; then 1575 echo "SKIP: Could not run test without ip tool" 1576 exit $ksft_skip 1577fi 1578 1579ip route help 2>&1 | grep -q fibmatch 1580if [ $? -ne 0 ]; then 1581 echo "SKIP: iproute2 too old, missing fibmatch" 1582 exit $ksft_skip 1583fi 1584 1585# start clean 1586cleanup &> /dev/null 1587 1588for t in $TESTS 1589do 1590 case $t in 1591 fib_unreg_test|unregister) fib_unreg_test;; 1592 fib_down_test|down) fib_down_test;; 1593 fib_carrier_test|carrier) fib_carrier_test;; 1594 fib_rp_filter_test|rp_filter) fib_rp_filter_test;; 1595 fib_nexthop_test|nexthop) fib_nexthop_test;; 1596 ipv6_route_test|ipv6_rt) ipv6_route_test;; 1597 ipv4_route_test|ipv4_rt) ipv4_route_test;; 1598 ipv6_addr_metric) ipv6_addr_metric_test;; 1599 ipv4_addr_metric) ipv4_addr_metric_test;; 1600 ipv6_route_metrics) ipv6_route_metrics_test;; 1601 ipv4_route_metrics) ipv4_route_metrics_test;; 1602 ipv4_route_v6_gw) ipv4_route_v6_gw_test;; 1603 1604 help) echo "Test names: $TESTS"; exit 0;; 1605 esac 1606done 1607 1608if [ "$TESTS" != "none" ]; then 1609 printf "\nTests passed: %3d\n" ${nsuccess} 1610 printf "Tests failed: %3d\n" ${nfail} 1611fi 1612 1613exit $ret 1614