1#!/bin/bash 2# 3# This test is for checking rtnetlink callpaths, and get as much coverage as possible. 4# 5# set -e 6 7ALL_TESTS=" 8 kci_test_polrouting 9 kci_test_route_get 10 kci_test_addrlft 11 kci_test_promote_secondaries 12 kci_test_tc 13 kci_test_gre 14 kci_test_gretap 15 kci_test_ip6gretap 16 kci_test_erspan 17 kci_test_ip6erspan 18 kci_test_bridge 19 kci_test_addrlabel 20 kci_test_ifalias 21 kci_test_vrf 22 kci_test_encap 23 kci_test_macsec 24 kci_test_macsec_offload 25 kci_test_ipsec 26 kci_test_ipsec_offload 27 kci_test_fdb_get 28 kci_test_neigh_get 29 kci_test_bridge_parent_id 30 kci_test_address_proto 31 kci_test_enslave_bonding 32" 33 34devdummy="test-dummy0" 35VERBOSE=0 36PAUSE=no 37PAUSE_ON_FAIL=no 38 39source lib.sh 40 41# set global exit status, but never reset nonzero one. 42check_err() 43{ 44 if [ $ret -eq 0 ]; then 45 ret=$1 46 fi 47} 48 49# same but inverted -- used when command must fail for test to pass 50check_fail() 51{ 52 if [ $1 -eq 0 ]; then 53 ret=1 54 fi 55} 56 57run_cmd_common() 58{ 59 local cmd="$*" 60 local out 61 if [ "$VERBOSE" = "1" ]; then 62 echo "COMMAND: ${cmd}" 63 fi 64 out=$($cmd 2>&1) 65 rc=$? 66 if [ "$VERBOSE" = "1" -a -n "$out" ]; then 67 echo " $out" 68 fi 69 return $rc 70} 71 72run_cmd() { 73 run_cmd_common "$@" 74 rc=$? 75 check_err $rc 76 return $rc 77} 78run_cmd_fail() 79{ 80 run_cmd_common "$@" 81 rc=$? 82 check_fail $rc 83 return $rc 84} 85 86run_cmd_grep_common() 87{ 88 local find="$1"; shift 89 local cmd="$*" 90 local out 91 if [ "$VERBOSE" = "1" ]; then 92 echo "COMMAND: ${cmd} 2>&1 | grep -q '${find}'" 93 fi 94 out=$($cmd 2>&1 | grep -q "${find}" 2>&1) 95 return $? 96} 97 98run_cmd_grep() { 99 run_cmd_grep_common "$@" 100 rc=$? 101 check_err $rc 102 return $rc 103} 104 105run_cmd_grep_fail() 106{ 107 run_cmd_grep_common "$@" 108 rc=$? 109 check_fail $rc 110 return $rc 111} 112 113end_test() 114{ 115 echo "$*" 116 [ "${VERBOSE}" = "1" ] && echo 117 118 if [[ $ret -ne 0 ]] && [[ "${PAUSE_ON_FAIL}" = "yes" ]]; then 119 echo "Hit enter to continue" 120 read a 121 fi; 122 123 if [ "${PAUSE}" = "yes" ]; then 124 echo "Hit enter to continue" 125 read a 126 fi 127 128} 129 130 131kci_add_dummy() 132{ 133 run_cmd ip link add name "$devdummy" type dummy 134 run_cmd ip link set "$devdummy" up 135} 136 137kci_del_dummy() 138{ 139 run_cmd ip link del dev "$devdummy" 140} 141 142kci_test_netconf() 143{ 144 dev="$1" 145 r=$ret 146 run_cmd ip netconf show dev "$dev" 147 for f in 4 6; do 148 run_cmd ip -$f netconf show dev "$dev" 149 done 150 151 if [ $ret -ne 0 ] ;then 152 end_test "FAIL: ip netconf show $dev" 153 test $r -eq 0 && ret=0 154 return 1 155 fi 156} 157 158# add a bridge with vlans on top 159kci_test_bridge() 160{ 161 devbr="test-br0" 162 vlandev="testbr-vlan1" 163 164 local ret=0 165 run_cmd ip link add name "$devbr" type bridge 166 run_cmd ip link set dev "$devdummy" master "$devbr" 167 run_cmd ip link set "$devbr" up 168 run_cmd ip link add link "$devbr" name "$vlandev" type vlan id 1 169 run_cmd ip addr add dev "$vlandev" 10.200.7.23/30 170 run_cmd ip -6 addr add dev "$vlandev" dead:42::1234/64 171 run_cmd ip -d link 172 run_cmd ip r s t all 173 174 for name in "$devbr" "$vlandev" "$devdummy" ; do 175 kci_test_netconf "$name" 176 done 177 run_cmd ip -6 addr del dev "$vlandev" dead:42::1234/64 178 run_cmd ip link del dev "$vlandev" 179 run_cmd ip link del dev "$devbr" 180 181 if [ $ret -ne 0 ];then 182 end_test "FAIL: bridge setup" 183 return 1 184 fi 185 end_test "PASS: bridge setup" 186 187} 188 189kci_test_gre() 190{ 191 gredev=neta 192 rem=10.42.42.1 193 loc=10.0.0.1 194 195 local ret=0 196 run_cmd ip tunnel add $gredev mode gre remote $rem local $loc ttl 1 197 run_cmd ip link set $gredev up 198 run_cmd ip addr add 10.23.7.10 dev $gredev 199 run_cmd ip route add 10.23.8.0/30 dev $gredev 200 run_cmd ip addr add dev "$devdummy" 10.23.7.11/24 201 run_cmd ip link 202 run_cmd ip addr 203 204 kci_test_netconf "$gredev" 205 run_cmd ip addr del dev "$devdummy" 10.23.7.11/24 206 run_cmd ip link del $gredev 207 208 if [ $ret -ne 0 ];then 209 end_test "FAIL: gre tunnel endpoint" 210 return 1 211 fi 212 end_test "PASS: gre tunnel endpoint" 213} 214 215# tc uses rtnetlink too, for full tc testing 216# please see tools/testing/selftests/tc-testing. 217kci_test_tc() 218{ 219 dev=lo 220 local ret=0 221 222 run_cmd tc qdisc add dev "$dev" root handle 1: htb 223 run_cmd tc class add dev "$dev" parent 1: classid 1:10 htb rate 1mbit 224 run_cmd tc filter add dev "$dev" parent 1:0 prio 5 handle ffe: protocol ip u32 divisor 256 225 run_cmd tc filter add dev "$dev" parent 1:0 prio 5 handle ffd: protocol ip u32 divisor 256 226 run_cmd tc filter add dev "$dev" parent 1:0 prio 5 handle ffc: protocol ip u32 divisor 256 227 run_cmd tc filter add dev "$dev" protocol ip parent 1: prio 5 handle ffe:2:3 u32 ht ffe:2: match ip src 10.0.0.3 flowid 1:10 228 run_cmd tc filter add dev "$dev" protocol ip parent 1: prio 5 handle ffe:2:2 u32 ht ffe:2: match ip src 10.0.0.2 flowid 1:10 229 run_cmd tc filter show dev "$dev" parent 1:0 230 run_cmd tc filter del dev "$dev" protocol ip parent 1: prio 5 handle ffe:2:3 u32 231 run_cmd tc filter show dev "$dev" parent 1:0 232 run_cmd tc qdisc del dev "$dev" root handle 1: htb 233 234 if [ $ret -ne 0 ];then 235 end_test "FAIL: tc htb hierarchy" 236 return 1 237 fi 238 end_test "PASS: tc htb hierarchy" 239 240} 241 242kci_test_polrouting() 243{ 244 local ret=0 245 run_cmd ip rule add fwmark 1 lookup 100 246 run_cmd ip route add local 0.0.0.0/0 dev lo table 100 247 run_cmd ip r s t all 248 run_cmd ip rule del fwmark 1 lookup 100 249 run_cmd ip route del local 0.0.0.0/0 dev lo table 100 250 251 if [ $ret -ne 0 ];then 252 end_test "FAIL: policy route test" 253 return 1 254 fi 255 end_test "PASS: policy routing" 256} 257 258kci_test_route_get() 259{ 260 local hash_policy=$(sysctl -n net.ipv4.fib_multipath_hash_policy) 261 262 local ret=0 263 run_cmd ip route get 127.0.0.1 264 run_cmd ip route get 127.0.0.1 dev "$devdummy" 265 run_cmd ip route get ::1 266 run_cmd ip route get fe80::1 dev "$devdummy" 267 run_cmd ip route get 127.0.0.1 from 127.0.0.1 oif lo tos 0x10 mark 0x1 268 run_cmd ip route get ::1 from ::1 iif lo oif lo tos 0x10 mark 0x1 269 run_cmd ip addr add dev "$devdummy" 10.23.7.11/24 270 run_cmd ip route get 10.23.7.11 from 10.23.7.12 iif "$devdummy" 271 run_cmd ip route add 10.23.8.0/24 \ 272 nexthop via 10.23.7.13 dev "$devdummy" \ 273 nexthop via 10.23.7.14 dev "$devdummy" 274 275 sysctl -wq net.ipv4.fib_multipath_hash_policy=0 276 run_cmd ip route get 10.23.8.11 277 sysctl -wq net.ipv4.fib_multipath_hash_policy=1 278 run_cmd ip route get 10.23.8.11 279 sysctl -wq net.ipv4.fib_multipath_hash_policy="$hash_policy" 280 run_cmd ip route del 10.23.8.0/24 281 run_cmd ip addr del dev "$devdummy" 10.23.7.11/24 282 283 284 if [ $ret -ne 0 ];then 285 end_test "FAIL: route get" 286 return 1 287 fi 288 289 end_test "PASS: route get" 290} 291 292kci_test_addrlft() 293{ 294 for i in $(seq 10 100) ;do 295 lft=$(((RANDOM%3) + 1)) 296 run_cmd ip addr add 10.23.11.$i/32 dev "$devdummy" preferred_lft $lft valid_lft $((lft+1)) 297 done 298 299 sleep 5 300 run_cmd_grep_fail "10.23.11." ip addr show dev "$devdummy" 301 if [ $? -eq 0 ]; then 302 check_err 1 303 end_test "FAIL: preferred_lft addresses remaining" 304 return 305 fi 306 307 end_test "PASS: preferred_lft addresses have expired" 308} 309 310kci_test_promote_secondaries() 311{ 312 promote=$(sysctl -n net.ipv4.conf.$devdummy.promote_secondaries) 313 314 sysctl -q net.ipv4.conf.$devdummy.promote_secondaries=1 315 316 for i in $(seq 2 254);do 317 IP="10.23.11.$i" 318 ip -f inet addr add $IP/16 brd + dev "$devdummy" 319 ifconfig "$devdummy" $IP netmask 255.255.0.0 320 done 321 322 ip addr flush dev "$devdummy" 323 324 [ $promote -eq 0 ] && sysctl -q net.ipv4.conf.$devdummy.promote_secondaries=0 325 326 end_test "PASS: promote_secondaries complete" 327} 328 329kci_test_addrlabel() 330{ 331 local ret=0 332 run_cmd ip addrlabel add prefix dead::/64 dev lo label 1 333 run_cmd_grep "prefix dead::/64 dev lo label 1" ip addrlabel list 334 run_cmd ip addrlabel del prefix dead::/64 dev lo label 1 335 run_cmd ip addrlabel add prefix dead::/64 label 1 336 run_cmd ip addrlabel del prefix dead::/64 label 1 337 338 # concurrent add/delete 339 for i in $(seq 1 1000); do 340 ip addrlabel add prefix 1c3::/64 label 12345 2>/dev/null 341 done & 342 343 for i in $(seq 1 1000); do 344 ip addrlabel del prefix 1c3::/64 label 12345 2>/dev/null 345 done 346 347 wait 348 349 ip addrlabel del prefix 1c3::/64 label 12345 2>/dev/null 350 351 if [ $ret -ne 0 ];then 352 end_test "FAIL: ipv6 addrlabel" 353 return 1 354 fi 355 356 end_test "PASS: ipv6 addrlabel" 357} 358 359kci_test_ifalias() 360{ 361 local ret=0 362 namewant=$(uuidgen) 363 syspathname="/sys/class/net/$devdummy/ifalias" 364 run_cmd ip link set dev "$devdummy" alias "$namewant" 365 366 if [ $ret -ne 0 ]; then 367 end_test "FAIL: cannot set interface alias of $devdummy to $namewant" 368 return 1 369 fi 370 run_cmd_grep "alias $namewant" ip link show "$devdummy" 371 372 if [ -r "$syspathname" ] ; then 373 read namehave < "$syspathname" 374 if [ "$namewant" != "$namehave" ]; then 375 end_test "FAIL: did set ifalias $namewant but got $namehave" 376 return 1 377 fi 378 379 namewant=$(uuidgen) 380 echo "$namewant" > "$syspathname" 381 run_cmd_grep "alias $namewant" ip link show "$devdummy" 382 383 # sysfs interface allows to delete alias again 384 echo "" > "$syspathname" 385 run_cmd_grep_fail "alias $namewant" ip link show "$devdummy" 386 387 for i in $(seq 1 100); do 388 uuidgen > "$syspathname" & 389 done 390 391 wait 392 393 # re-add the alias -- kernel should free mem when dummy dev is removed 394 run_cmd ip link set dev "$devdummy" alias "$namewant" 395 396 fi 397 398 if [ $ret -ne 0 ]; then 399 end_test "FAIL: set interface alias $devdummy to $namewant" 400 return 1 401 fi 402 403 end_test "PASS: set ifalias $namewant for $devdummy" 404} 405 406kci_test_vrf() 407{ 408 vrfname="test-vrf" 409 local ret=0 410 run_cmd ip link show type vrf 411 if [ $? -ne 0 ]; then 412 end_test "SKIP: vrf: iproute2 too old" 413 return $ksft_skip 414 fi 415 run_cmd ip link add "$vrfname" type vrf table 10 416 if [ $ret -ne 0 ];then 417 end_test "FAIL: can't add vrf interface, skipping test" 418 return 0 419 fi 420 run_cmd_grep "$vrfname" ip -br link show type vrf 421 if [ $ret -ne 0 ];then 422 end_test "FAIL: created vrf device not found" 423 return 1 424 fi 425 426 run_cmd ip link set dev "$vrfname" up 427 run_cmd ip link set dev "$devdummy" master "$vrfname" 428 run_cmd ip link del dev "$vrfname" 429 430 if [ $ret -ne 0 ];then 431 end_test "FAIL: vrf" 432 return 1 433 fi 434 435 end_test "PASS: vrf" 436} 437 438kci_test_encap_vxlan() 439{ 440 local ret=0 441 vxlan="test-vxlan0" 442 vlan="test-vlan0" 443 testns="$1" 444 run_cmd ip -netns "$testns" link add "$vxlan" type vxlan id 42 group 239.1.1.1 \ 445 dev "$devdummy" dstport 4789 446 if [ $? -ne 0 ]; then 447 end_test "FAIL: can't add vxlan interface, skipping test" 448 return 0 449 fi 450 451 run_cmd ip -netns "$testns" addr add 10.2.11.49/24 dev "$vxlan" 452 run_cmd ip -netns "$testns" link set up dev "$vxlan" 453 run_cmd ip -netns "$testns" link add link "$vxlan" name "$vlan" type vlan id 1 454 455 # changelink testcases 456 run_cmd_fail ip -netns "$testns" link set dev "$vxlan" type vxlan vni 43 457 run_cmd_fail ip -netns "$testns" link set dev "$vxlan" type vxlan group ffe5::5 dev "$devdummy" 458 run_cmd_fail ip -netns "$testns" link set dev "$vxlan" type vxlan ttl inherit 459 460 run_cmd ip -netns "$testns" link set dev "$vxlan" type vxlan ttl 64 461 run_cmd ip -netns "$testns" link set dev "$vxlan" type vxlan nolearning 462 463 run_cmd_fail ip -netns "$testns" link set dev "$vxlan" type vxlan proxy 464 run_cmd_fail ip -netns "$testns" link set dev "$vxlan" type vxlan norsc 465 run_cmd_fail ip -netns "$testns" link set dev "$vxlan" type vxlan l2miss 466 run_cmd_fail ip -netns "$testns" link set dev "$vxlan" type vxlan l3miss 467 run_cmd_fail ip -netns "$testns" link set dev "$vxlan" type vxlan external 468 run_cmd_fail ip -netns "$testns" link set dev "$vxlan" type vxlan udpcsum 469 run_cmd_fail ip -netns "$testns" link set dev "$vxlan" type vxlan udp6zerocsumtx 470 run_cmd_fail ip -netns "$testns" link set dev "$vxlan" type vxlan udp6zerocsumrx 471 run_cmd_fail ip -netns "$testns" link set dev "$vxlan" type vxlan remcsumtx 472 run_cmd_fail ip -netns "$testns" link set dev "$vxlan" type vxlan remcsumrx 473 run_cmd_fail ip -netns "$testns" link set dev "$vxlan" type vxlan gbp 474 run_cmd_fail ip -netns "$testns" link set dev "$vxlan" type vxlan gpe 475 run_cmd ip -netns "$testns" link del "$vxlan" 476 477 if [ $ret -ne 0 ]; then 478 end_test "FAIL: vxlan" 479 return 1 480 fi 481 end_test "PASS: vxlan" 482} 483 484kci_test_encap_fou() 485{ 486 local ret=0 487 name="test-fou" 488 testns="$1" 489 run_cmd_grep 'Usage: ip fou' ip fou help 490 if [ $? -ne 0 ];then 491 end_test "SKIP: fou: iproute2 too old" 492 return $ksft_skip 493 fi 494 495 if ! /sbin/modprobe -q -n fou; then 496 end_test "SKIP: module fou is not found" 497 return $ksft_skip 498 fi 499 /sbin/modprobe -q fou 500 501 run_cmd ip -netns "$testns" fou add port 7777 ipproto 47 502 if [ $? -ne 0 ];then 503 end_test "FAIL: can't add fou port 7777, skipping test" 504 return 1 505 fi 506 run_cmd ip -netns "$testns" fou add port 8888 ipproto 4 507 run_cmd_fail ip -netns "$testns" fou del port 9999 508 run_cmd ip -netns "$testns" fou del port 7777 509 if [ $ret -ne 0 ]; then 510 end_test "FAIL: fou"s 511 return 1 512 fi 513 514 end_test "PASS: fou" 515} 516 517# test various encap methods, use netns to avoid unwanted interference 518kci_test_encap() 519{ 520 local ret=0 521 setup_ns testns 522 if [ $? -ne 0 ]; then 523 end_test "SKIP encap tests: cannot add net namespace $testns" 524 return $ksft_skip 525 fi 526 run_cmd ip -netns "$testns" link set lo up 527 run_cmd ip -netns "$testns" link add name "$devdummy" type dummy 528 run_cmd ip -netns "$testns" link set "$devdummy" up 529 run_cmd kci_test_encap_vxlan "$testns" 530 run_cmd kci_test_encap_fou "$testns" 531 532 ip netns del "$testns" 533 return $ret 534} 535 536kci_test_macsec() 537{ 538 msname="test_macsec0" 539 local ret=0 540 run_cmd_grep "^Usage: ip macsec" ip macsec help 541 if [ $? -ne 0 ]; then 542 end_test "SKIP: macsec: iproute2 too old" 543 return $ksft_skip 544 fi 545 run_cmd ip link add link "$devdummy" "$msname" type macsec port 42 encrypt on 546 if [ $ret -ne 0 ];then 547 end_test "FAIL: can't add macsec interface, skipping test" 548 return 1 549 fi 550 run_cmd ip macsec add "$msname" tx sa 0 pn 1024 on key 01 12345678901234567890123456789012 551 run_cmd ip macsec add "$msname" rx port 1234 address "1c:ed:de:ad:be:ef" 552 run_cmd ip macsec add "$msname" rx port 1234 address "1c:ed:de:ad:be:ef" sa 0 pn 1 on key 00 0123456789abcdef0123456789abcdef 553 run_cmd ip macsec show 554 run_cmd ip link del dev "$msname" 555 556 if [ $ret -ne 0 ];then 557 end_test "FAIL: macsec" 558 return 1 559 fi 560 561 end_test "PASS: macsec" 562} 563 564kci_test_macsec_offload() 565{ 566 sysfsd=/sys/kernel/debug/netdevsim/netdevsim0/ports/0/ 567 sysfsnet=/sys/bus/netdevsim/devices/netdevsim0/net/ 568 probed=false 569 local ret=0 570 run_cmd_grep "^Usage: ip macsec" ip macsec help 571 if [ $? -ne 0 ]; then 572 end_test "SKIP: macsec: iproute2 too old" 573 return $ksft_skip 574 fi 575 576 if ! mount | grep -q debugfs; then 577 mount -t debugfs none /sys/kernel/debug/ &> /dev/null 578 fi 579 580 # setup netdevsim since dummydev doesn't have offload support 581 if [ ! -w /sys/bus/netdevsim/new_device ] ; then 582 run_cmd modprobe -q netdevsim 583 584 if [ $ret -ne 0 ]; then 585 end_test "SKIP: macsec_offload can't load netdevsim" 586 return $ksft_skip 587 fi 588 probed=true 589 fi 590 591 echo "0" > /sys/bus/netdevsim/new_device 592 while [ ! -d $sysfsnet ] ; do :; done 593 udevadm settle 594 dev=`ls $sysfsnet` 595 596 ip link set $dev up 597 if [ ! -d $sysfsd ] ; then 598 end_test "FAIL: macsec_offload can't create device $dev" 599 return 1 600 fi 601 run_cmd_grep 'macsec-hw-offload: on' ethtool -k $dev 602 if [ $? -eq 1 ] ; then 603 end_test "FAIL: macsec_offload netdevsim doesn't support MACsec offload" 604 return 1 605 fi 606 run_cmd ip link add link $dev kci_macsec1 type macsec port 4 offload mac 607 run_cmd ip link add link $dev kci_macsec2 type macsec address "aa:bb:cc:dd:ee:ff" port 5 offload mac 608 run_cmd ip link add link $dev kci_macsec3 type macsec sci abbacdde01020304 offload mac 609 run_cmd_fail ip link add link $dev kci_macsec4 type macsec port 8 offload mac 610 611 msname=kci_macsec1 612 run_cmd ip macsec add "$msname" tx sa 0 pn 1024 on key 01 12345678901234567890123456789012 613 run_cmd ip macsec add "$msname" rx port 1234 address "1c:ed:de:ad:be:ef" 614 run_cmd ip macsec add "$msname" rx port 1234 address "1c:ed:de:ad:be:ef" sa 0 pn 1 on \ 615 key 00 0123456789abcdef0123456789abcdef 616 run_cmd_fail ip macsec add "$msname" rx port 1235 address "1c:ed:de:ad:be:ef" 617 # clean up any leftovers 618 for msdev in kci_macsec{1,2,3,4} ; do 619 ip link del $msdev 2> /dev/null 620 done 621 echo 0 > /sys/bus/netdevsim/del_device 622 $probed && rmmod netdevsim 623 624 if [ $ret -ne 0 ]; then 625 end_test "FAIL: macsec_offload" 626 return 1 627 fi 628 end_test "PASS: macsec_offload" 629} 630 631#------------------------------------------------------------------- 632# Example commands 633# ip x s add proto esp src 14.0.0.52 dst 14.0.0.70 \ 634# spi 0x07 mode transport reqid 0x07 replay-window 32 \ 635# aead 'rfc4106(gcm(aes))' 1234567890123456dcba 128 \ 636# sel src 14.0.0.52/24 dst 14.0.0.70/24 637# ip x p add dir out src 14.0.0.52/24 dst 14.0.0.70/24 \ 638# tmpl proto esp src 14.0.0.52 dst 14.0.0.70 \ 639# spi 0x07 mode transport reqid 0x07 640# 641# Subcommands not tested 642# ip x s update 643# ip x s allocspi 644# ip x s deleteall 645# ip x p update 646# ip x p deleteall 647# ip x p set 648#------------------------------------------------------------------- 649kci_test_ipsec() 650{ 651 local ret=0 652 algo="aead rfc4106(gcm(aes)) 0x3132333435363738393031323334353664636261 128" 653 srcip=192.168.123.1 654 dstip=192.168.123.2 655 spi=7 656 657 ip addr add $srcip dev $devdummy 658 659 # flush to be sure there's nothing configured 660 run_cmd ip x s flush ; ip x p flush 661 662 # start the monitor in the background 663 tmpfile=`mktemp /var/run/ipsectestXXX` 664 mpid=`(ip x m > $tmpfile & echo $!) 2>/dev/null` 665 sleep 0.2 666 667 ipsecid="proto esp src $srcip dst $dstip spi 0x07" 668 run_cmd ip x s add $ipsecid \ 669 mode transport reqid 0x07 replay-window 32 \ 670 $algo sel src $srcip/24 dst $dstip/24 671 672 673 lines=`ip x s list | grep $srcip | grep $dstip | wc -l` 674 run_cmd test $lines -eq 2 675 run_cmd_grep "SAD count 1" ip x s count 676 677 lines=`ip x s get $ipsecid | grep $srcip | grep $dstip | wc -l` 678 run_cmd test $lines -eq 2 679 run_cmd ip x s delete $ipsecid 680 681 lines=`ip x s list | wc -l` 682 run_cmd test $lines -eq 0 683 684 ipsecsel="dir out src $srcip/24 dst $dstip/24" 685 run_cmd ip x p add $ipsecsel \ 686 tmpl proto esp src $srcip dst $dstip \ 687 spi 0x07 mode transport reqid 0x07 688 689 690 lines=`ip x p list | grep $srcip | grep $dstip | wc -l` 691 run_cmd test $lines -eq 2 692 693 run_cmd_grep "SPD IN 0 OUT 1 FWD 0" ip x p count 694 695 lines=`ip x p get $ipsecsel | grep $srcip | grep $dstip | wc -l` 696 run_cmd test $lines -eq 2 697 698 run_cmd ip x p delete $ipsecsel 699 700 lines=`ip x p list | wc -l` 701 run_cmd test $lines -eq 0 702 703 # check the monitor results 704 kill $mpid 705 lines=`wc -l $tmpfile | cut "-d " -f1` 706 run_cmd test $lines -eq 20 707 rm -rf $tmpfile 708 709 # clean up any leftovers 710 run_cmd ip x s flush 711 run_cmd ip x p flush 712 ip addr del $srcip/32 dev $devdummy 713 714 if [ $ret -ne 0 ]; then 715 end_test "FAIL: ipsec" 716 return 1 717 fi 718 end_test "PASS: ipsec" 719} 720 721#------------------------------------------------------------------- 722# Example commands 723# ip x s add proto esp src 14.0.0.52 dst 14.0.0.70 \ 724# spi 0x07 mode transport reqid 0x07 replay-window 32 \ 725# aead 'rfc4106(gcm(aes))' 1234567890123456dcba 128 \ 726# sel src 14.0.0.52/24 dst 14.0.0.70/24 727# offload dev sim1 dir out 728# ip x p add dir out src 14.0.0.52/24 dst 14.0.0.70/24 \ 729# tmpl proto esp src 14.0.0.52 dst 14.0.0.70 \ 730# spi 0x07 mode transport reqid 0x07 731# 732#------------------------------------------------------------------- 733kci_test_ipsec_offload() 734{ 735 local ret=0 736 algo="aead rfc4106(gcm(aes)) 0x3132333435363738393031323334353664636261 128" 737 srcip=192.168.123.3 738 dstip=192.168.123.4 739 sysfsd=/sys/kernel/debug/netdevsim/netdevsim0/ports/0/ 740 sysfsf=$sysfsd/ipsec 741 sysfsnet=/sys/bus/netdevsim/devices/netdevsim0/net/ 742 probed=false 743 744 if ! mount | grep -q debugfs; then 745 mount -t debugfs none /sys/kernel/debug/ &> /dev/null 746 fi 747 748 # setup netdevsim since dummydev doesn't have offload support 749 if [ ! -w /sys/bus/netdevsim/new_device ] ; then 750 run_cmd modprobe -q netdevsim 751 if [ $ret -ne 0 ]; then 752 end_test "SKIP: ipsec_offload can't load netdevsim" 753 return $ksft_skip 754 fi 755 probed=true 756 fi 757 758 echo "0" > /sys/bus/netdevsim/new_device 759 while [ ! -d $sysfsnet ] ; do :; done 760 udevadm settle 761 dev=`ls $sysfsnet` 762 763 ip addr add $srcip dev $dev 764 ip link set $dev up 765 if [ ! -d $sysfsd ] ; then 766 end_test "FAIL: ipsec_offload can't create device $dev" 767 return 1 768 fi 769 if [ ! -f $sysfsf ] ; then 770 end_test "FAIL: ipsec_offload netdevsim doesn't support IPsec offload" 771 return 1 772 fi 773 774 # flush to be sure there's nothing configured 775 ip x s flush ; ip x p flush 776 777 # create offloaded SAs, both in and out 778 run_cmd ip x p add dir out src $srcip/24 dst $dstip/24 \ 779 tmpl proto esp src $srcip dst $dstip spi 9 \ 780 mode transport reqid 42 781 782 run_cmd ip x p add dir in src $dstip/24 dst $srcip/24 \ 783 tmpl proto esp src $dstip dst $srcip spi 9 \ 784 mode transport reqid 42 785 786 run_cmd ip x s add proto esp src $srcip dst $dstip spi 9 \ 787 mode transport reqid 42 $algo sel src $srcip/24 dst $dstip/24 \ 788 offload dev $dev dir out 789 790 run_cmd ip x s add proto esp src $dstip dst $srcip spi 9 \ 791 mode transport reqid 42 $algo sel src $dstip/24 dst $srcip/24 \ 792 offload dev $dev dir in 793 794 if [ $ret -ne 0 ]; then 795 end_test "FAIL: ipsec_offload can't create SA" 796 return 1 797 fi 798 799 # does offload show up in ip output 800 lines=`ip x s list | grep -c "crypto offload parameters: dev $dev dir"` 801 if [ $lines -ne 2 ] ; then 802 check_err 1 803 end_test "FAIL: ipsec_offload SA offload missing from list output" 804 fi 805 806 # use ping to exercise the Tx path 807 ping -I $dev -c 3 -W 1 -i 0 $dstip >/dev/null 808 809 # does driver have correct offload info 810 run_cmd diff $sysfsf - << EOF 811SA count=2 tx=3 812sa[0] tx ipaddr=0x00000000 00000000 00000000 00000000 813sa[0] spi=0x00000009 proto=0x32 salt=0x61626364 crypt=1 814sa[0] key=0x34333231 38373635 32313039 36353433 815sa[1] rx ipaddr=0x00000000 00000000 00000000 037ba8c0 816sa[1] spi=0x00000009 proto=0x32 salt=0x61626364 crypt=1 817sa[1] key=0x34333231 38373635 32313039 36353433 818EOF 819 if [ $? -ne 0 ] ; then 820 end_test "FAIL: ipsec_offload incorrect driver data" 821 check_err 1 822 fi 823 824 # does offload get removed from driver 825 ip x s flush 826 ip x p flush 827 lines=`grep -c "SA count=0" $sysfsf` 828 if [ $lines -ne 1 ] ; then 829 check_err 1 830 end_test "FAIL: ipsec_offload SA not removed from driver" 831 fi 832 833 # clean up any leftovers 834 echo 0 > /sys/bus/netdevsim/del_device 835 $probed && rmmod netdevsim 836 837 if [ $ret -ne 0 ]; then 838 end_test "FAIL: ipsec_offload" 839 return 1 840 fi 841 end_test "PASS: ipsec_offload" 842} 843 844kci_test_gretap() 845{ 846 DEV_NS=gretap00 847 local ret=0 848 849 setup_ns testns 850 if [ $? -ne 0 ]; then 851 end_test "SKIP gretap tests: cannot add net namespace $testns" 852 return $ksft_skip 853 fi 854 855 run_cmd_grep "^Usage:" ip link help gretap 856 if [ $? -ne 0 ];then 857 end_test "SKIP: gretap: iproute2 too old" 858 ip netns del "$testns" 859 return $ksft_skip 860 fi 861 862 # test native tunnel 863 run_cmd ip -netns "$testns" link add dev "$DEV_NS" type gretap seq \ 864 key 102 local 172.16.1.100 remote 172.16.1.200 865 866 867 run_cmd ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24 868 run_cmd ip -netns "$testns" link set dev $DEV_NS up 869 run_cmd ip -netns "$testns" link del "$DEV_NS" 870 871 # test external mode 872 run_cmd ip -netns "$testns" link add dev "$DEV_NS" type gretap external 873 run_cmd ip -netns "$testns" link del "$DEV_NS" 874 875 if [ $ret -ne 0 ]; then 876 end_test "FAIL: gretap" 877 ip netns del "$testns" 878 return 1 879 fi 880 end_test "PASS: gretap" 881 882 ip netns del "$testns" 883} 884 885kci_test_ip6gretap() 886{ 887 DEV_NS=ip6gretap00 888 local ret=0 889 890 setup_ns testns 891 if [ $? -ne 0 ]; then 892 end_test "SKIP ip6gretap tests: cannot add net namespace $testns" 893 return $ksft_skip 894 fi 895 896 run_cmd_grep "^Usage:" ip link help ip6gretap 897 if [ $? -ne 0 ];then 898 end_test "SKIP: ip6gretap: iproute2 too old" 899 ip netns del "$testns" 900 return $ksft_skip 901 fi 902 903 # test native tunnel 904 run_cmd ip -netns "$testns" link add dev "$DEV_NS" type ip6gretap seq \ 905 key 102 local fc00:100::1 remote fc00:100::2 906 907 908 run_cmd ip -netns "$testns" addr add dev "$DEV_NS" fc00:200::1/96 909 run_cmd ip -netns "$testns" link set dev $DEV_NS up 910 run_cmd ip -netns "$testns" link del "$DEV_NS" 911 912 # test external mode 913 run_cmd ip -netns "$testns" link add dev "$DEV_NS" type ip6gretap external 914 run_cmd ip -netns "$testns" link del "$DEV_NS" 915 916 if [ $ret -ne 0 ]; then 917 end_test "FAIL: ip6gretap" 918 ip netns del "$testns" 919 return 1 920 fi 921 end_test "PASS: ip6gretap" 922 923 ip netns del "$testns" 924} 925 926kci_test_erspan() 927{ 928 DEV_NS=erspan00 929 local ret=0 930 run_cmd_grep "^Usage:" ip link help erspan 931 if [ $? -ne 0 ];then 932 end_test "SKIP: erspan: iproute2 too old" 933 return $ksft_skip 934 fi 935 setup_ns testns 936 if [ $? -ne 0 ]; then 937 end_test "SKIP erspan tests: cannot add net namespace $testns" 938 return $ksft_skip 939 fi 940 941 # test native tunnel erspan v1 942 run_cmd ip -netns "$testns" link add dev "$DEV_NS" type erspan seq \ 943 key 102 local 172.16.1.100 remote 172.16.1.200 \ 944 erspan_ver 1 erspan 488 945 946 947 run_cmd ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24 948 run_cmd ip -netns "$testns" link set dev $DEV_NS up 949 run_cmd ip -netns "$testns" link del "$DEV_NS" 950 951 # test native tunnel erspan v2 952 run_cmd ip -netns "$testns" link add dev "$DEV_NS" type erspan seq \ 953 key 102 local 172.16.1.100 remote 172.16.1.200 \ 954 erspan_ver 2 erspan_dir ingress erspan_hwid 7 955 956 957 run_cmd ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24 958 run_cmd ip -netns "$testns" link set dev $DEV_NS up 959 run_cmd ip -netns "$testns" link del "$DEV_NS" 960 961 # test external mode 962 run_cmd ip -netns "$testns" link add dev "$DEV_NS" type erspan external 963 run_cmd ip -netns "$testns" link del "$DEV_NS" 964 965 if [ $ret -ne 0 ]; then 966 end_test "FAIL: erspan" 967 ip netns del "$testns" 968 return 1 969 fi 970 end_test "PASS: erspan" 971 972 ip netns del "$testns" 973} 974 975kci_test_ip6erspan() 976{ 977 DEV_NS=ip6erspan00 978 local ret=0 979 run_cmd_grep "^Usage:" ip link help ip6erspan 980 if [ $? -ne 0 ];then 981 end_test "SKIP: ip6erspan: iproute2 too old" 982 return $ksft_skip 983 fi 984 setup_ns testns 985 if [ $? -ne 0 ]; then 986 end_test "SKIP ip6erspan tests: cannot add net namespace $testns" 987 return $ksft_skip 988 fi 989 990 # test native tunnel ip6erspan v1 991 run_cmd ip -netns "$testns" link add dev "$DEV_NS" type ip6erspan seq \ 992 key 102 local fc00:100::1 remote fc00:100::2 \ 993 erspan_ver 1 erspan 488 994 995 996 run_cmd ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24 997 run_cmd ip -netns "$testns" link set dev $DEV_NS up 998 run_cmd ip -netns "$testns" link del "$DEV_NS" 999 1000 # test native tunnel ip6erspan v2 1001 run_cmd ip -netns "$testns" link add dev "$DEV_NS" type ip6erspan seq \ 1002 key 102 local fc00:100::1 remote fc00:100::2 \ 1003 erspan_ver 2 erspan_dir ingress erspan_hwid 7 1004 1005 1006 run_cmd ip -netns "$testns" addr add dev "$DEV_NS" 10.1.1.100/24 1007 run_cmd ip -netns "$testns" link set dev $DEV_NS up 1008 run_cmd ip -netns "$testns" link del "$DEV_NS" 1009 1010 # test external mode 1011 run_cmd ip -netns "$testns" link add dev "$DEV_NS" \ 1012 type ip6erspan external 1013 1014 run_cmd ip -netns "$testns" link del "$DEV_NS" 1015 1016 if [ $ret -ne 0 ]; then 1017 end_test "FAIL: ip6erspan" 1018 ip netns del "$testns" 1019 return 1 1020 fi 1021 end_test "PASS: ip6erspan" 1022 1023 ip netns del "$testns" 1024} 1025 1026kci_test_fdb_get() 1027{ 1028 brdev="test-br0" 1029 vxlandev="vxlan10" 1030 test_mac=de:ad:be:ef:13:37 1031 localip="10.0.2.2" 1032 dstip="10.0.2.3" 1033 local ret=0 1034 1035 run_cmd_grep 'bridge fdb get' bridge fdb help 1036 if [ $? -ne 0 ];then 1037 end_test "SKIP: fdb get tests: iproute2 too old" 1038 return $ksft_skip 1039 fi 1040 1041 setup_ns testns 1042 if [ $? -ne 0 ]; then 1043 end_test "SKIP fdb get tests: cannot add net namespace $testns" 1044 return $ksft_skip 1045 fi 1046 IP="ip -netns $testns" 1047 BRIDGE="bridge -netns $testns" 1048 run_cmd $IP link add "$vxlandev" type vxlan id 10 local $localip \ 1049 dstport 4789 1050 run_cmd $IP link add name "$brdev" type bridge 1051 run_cmd $IP link set dev "$vxlandev" master "$brdev" 1052 run_cmd $BRIDGE fdb add $test_mac dev "$vxlandev" master 1053 run_cmd $BRIDGE fdb add $test_mac dev "$vxlandev" dst $dstip self 1054 run_cmd_grep "dev $vxlandev master $brdev" $BRIDGE fdb get $test_mac brport "$vxlandev" 1055 run_cmd_grep "dev $vxlandev master $brdev" $BRIDGE fdb get $test_mac br "$brdev" 1056 run_cmd_grep "dev $vxlandev dst $dstip" $BRIDGE fdb get $test_mac dev "$vxlandev" self 1057 1058 ip netns del $testns &>/dev/null 1059 1060 if [ $ret -ne 0 ]; then 1061 end_test "FAIL: bridge fdb get" 1062 return 1 1063 fi 1064 1065 end_test "PASS: bridge fdb get" 1066} 1067 1068kci_test_neigh_get() 1069{ 1070 dstmac=de:ad:be:ef:13:37 1071 dstip=10.0.2.4 1072 dstip6=dead::2 1073 local ret=0 1074 1075 run_cmd_grep 'ip neigh get' ip neigh help 1076 if [ $? -ne 0 ];then 1077 end_test "SKIP: fdb get tests: iproute2 too old" 1078 return $ksft_skip 1079 fi 1080 1081 # ipv4 1082 run_cmd ip neigh add $dstip lladdr $dstmac dev "$devdummy" 1083 run_cmd_grep "$dstmac" ip neigh get $dstip dev "$devdummy" 1084 run_cmd ip neigh del $dstip lladdr $dstmac dev "$devdummy" 1085 1086 # ipv4 proxy 1087 run_cmd ip neigh add proxy $dstip dev "$devdummy" 1088 run_cmd_grep "$dstip" ip neigh get proxy $dstip dev "$devdummy" 1089 run_cmd ip neigh del proxy $dstip dev "$devdummy" 1090 1091 # ipv6 1092 run_cmd ip neigh add $dstip6 lladdr $dstmac dev "$devdummy" 1093 run_cmd_grep "$dstmac" ip neigh get $dstip6 dev "$devdummy" 1094 run_cmd ip neigh del $dstip6 lladdr $dstmac dev "$devdummy" 1095 1096 # ipv6 proxy 1097 run_cmd ip neigh add proxy $dstip6 dev "$devdummy" 1098 run_cmd_grep "$dstip6" ip neigh get proxy $dstip6 dev "$devdummy" 1099 run_cmd ip neigh del proxy $dstip6 dev "$devdummy" 1100 1101 if [ $ret -ne 0 ];then 1102 end_test "FAIL: neigh get" 1103 return 1 1104 fi 1105 1106 end_test "PASS: neigh get" 1107} 1108 1109kci_test_bridge_parent_id() 1110{ 1111 local ret=0 1112 sysfsnet=/sys/bus/netdevsim/devices/netdevsim 1113 probed=false 1114 1115 if [ ! -w /sys/bus/netdevsim/new_device ] ; then 1116 run_cmd modprobe -q netdevsim 1117 if [ $ret -ne 0 ]; then 1118 end_test "SKIP: bridge_parent_id can't load netdevsim" 1119 return $ksft_skip 1120 fi 1121 probed=true 1122 fi 1123 1124 echo "10 1" > /sys/bus/netdevsim/new_device 1125 while [ ! -d ${sysfsnet}10 ] ; do :; done 1126 echo "20 1" > /sys/bus/netdevsim/new_device 1127 while [ ! -d ${sysfsnet}20 ] ; do :; done 1128 udevadm settle 1129 dev10=`ls ${sysfsnet}10/net/` 1130 dev20=`ls ${sysfsnet}20/net/` 1131 run_cmd ip link add name test-bond0 type bond mode 802.3ad 1132 run_cmd ip link set dev $dev10 master test-bond0 1133 run_cmd ip link set dev $dev20 master test-bond0 1134 run_cmd ip link add name test-br0 type bridge 1135 run_cmd ip link set dev test-bond0 master test-br0 1136 1137 # clean up any leftovers 1138 ip link del dev test-br0 1139 ip link del dev test-bond0 1140 echo 20 > /sys/bus/netdevsim/del_device 1141 echo 10 > /sys/bus/netdevsim/del_device 1142 $probed && rmmod netdevsim 1143 1144 if [ $ret -ne 0 ]; then 1145 end_test "FAIL: bridge_parent_id" 1146 return 1 1147 fi 1148 end_test "PASS: bridge_parent_id" 1149} 1150 1151address_get_proto() 1152{ 1153 local addr=$1; shift 1154 1155 ip -N -j address show dev "$devdummy" | 1156 jq -e -r --arg addr "${addr%/*}" \ 1157 '.[].addr_info[] | select(.local == $addr) | .protocol' 1158} 1159 1160address_count() 1161{ 1162 ip -N -j address show dev "$devdummy" "$@" | 1163 jq -e -r '[.[].addr_info[] | .local | select(. != null)] | length' 1164} 1165 1166do_test_address_proto() 1167{ 1168 local what=$1; shift 1169 local addr=$1; shift 1170 local addr2=${addr%/*}2/${addr#*/} 1171 local addr3=${addr%/*}3/${addr#*/} 1172 local proto 1173 local count 1174 local ret=0 1175 local err 1176 1177 ip address add dev "$devdummy" "$addr3" 1178 check_err $? 1179 proto=$(address_get_proto "$addr3") 1180 [[ "$proto" == null ]] 1181 check_err $? 1182 1183 ip address add dev "$devdummy" "$addr2" proto 0x99 1184 check_err $? 1185 proto=$(address_get_proto "$addr2") 1186 [[ "$proto" == 0x99 ]] 1187 check_err $? 1188 1189 ip address add dev "$devdummy" "$addr" proto 0xab 1190 check_err $? 1191 proto=$(address_get_proto "$addr") 1192 [[ "$proto" == 0xab ]] 1193 check_err $? 1194 1195 ip address replace dev "$devdummy" "$addr" proto 0x11 1196 proto=$(address_get_proto "$addr") 1197 check_err $? 1198 [[ "$proto" == 0x11 ]] 1199 check_err $? 1200 1201 count=$(address_count) 1202 check_err $? 1203 (( count >= 3 )) # $addr, $addr2 and $addr3 plus any kernel addresses 1204 check_err $? 1205 1206 count=$(address_count proto 0) 1207 check_err $? 1208 (( count == 1 )) # just $addr3 1209 check_err $? 1210 1211 count=$(address_count proto 0x11) 1212 check_err $? 1213 (( count == 2 )) # $addr and $addr3 1214 check_err $? 1215 1216 count=$(address_count proto 0xab) 1217 check_err $? 1218 (( count == 1 )) # just $addr3 1219 check_err $? 1220 1221 ip address del dev "$devdummy" "$addr" 1222 ip address del dev "$devdummy" "$addr2" 1223 ip address del dev "$devdummy" "$addr3" 1224 1225 if [ $ret -ne 0 ]; then 1226 end_test "FAIL: address proto $what" 1227 return 1 1228 fi 1229 end_test "PASS: address proto $what" 1230} 1231 1232kci_test_address_proto() 1233{ 1234 local ret=0 1235 1236 do_test_address_proto IPv4 192.0.2.1/28 1237 check_err $? 1238 1239 do_test_address_proto IPv6 2001:db8:1::1/64 1240 check_err $? 1241 1242 return $ret 1243} 1244 1245kci_test_enslave_bonding() 1246{ 1247 local bond="bond123" 1248 local ret=0 1249 1250 setup_ns testns 1251 if [ $? -ne 0 ]; then 1252 end_test "SKIP bonding tests: cannot add net namespace $testns" 1253 return $ksft_skip 1254 fi 1255 1256 run_cmd ip -netns $testns link add dev $bond type bond mode balance-rr 1257 run_cmd ip -netns $testns link add dev $devdummy type dummy 1258 run_cmd ip -netns $testns link set dev $devdummy up 1259 run_cmd ip -netns $testns link set dev $devdummy master $bond down 1260 if [ $ret -ne 0 ]; then 1261 end_test "FAIL: initially up interface added to a bond and set down" 1262 ip netns del "$testns" 1263 return 1 1264 fi 1265 1266 end_test "PASS: enslave interface in a bond" 1267 ip netns del "$testns" 1268} 1269 1270kci_test_rtnl() 1271{ 1272 local current_test 1273 local ret=0 1274 1275 kci_add_dummy 1276 if [ $ret -ne 0 ];then 1277 end_test "FAIL: cannot add dummy interface" 1278 return 1 1279 fi 1280 1281 for current_test in ${TESTS:-$ALL_TESTS}; do 1282 $current_test 1283 check_err $? 1284 done 1285 1286 kci_del_dummy 1287 return $ret 1288} 1289 1290usage() 1291{ 1292 cat <<EOF 1293usage: ${0##*/} OPTS 1294 1295 -t <test> Test(s) to run (default: all) 1296 (options: $(echo $ALL_TESTS)) 1297 -v Verbose mode (show commands and output) 1298 -P Pause after every test 1299 -p Pause after every failing test before cleanup (for debugging) 1300EOF 1301} 1302 1303#check for needed privileges 1304if [ "$(id -u)" -ne 0 ];then 1305 end_test "SKIP: Need root privileges" 1306 exit $ksft_skip 1307fi 1308 1309for x in ip tc;do 1310 $x -Version 2>/dev/null >/dev/null 1311 if [ $? -ne 0 ];then 1312 end_test "SKIP: Could not run test without the $x tool" 1313 exit $ksft_skip 1314 fi 1315done 1316 1317while getopts t:hvpP o; do 1318 case $o in 1319 t) TESTS=$OPTARG;; 1320 v) VERBOSE=1;; 1321 p) PAUSE_ON_FAIL=yes;; 1322 P) PAUSE=yes;; 1323 h) usage; exit 0;; 1324 *) usage; exit 1;; 1325 esac 1326done 1327 1328[ $PAUSE = "yes" ] && PAUSE_ON_FAIL="no" 1329 1330kci_test_rtnl 1331 1332exit $? 1333