Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 | #!/bin/bash # SPDX-License-Identifier: GPL-2.0 # # Test devlink-trap L3 drops functionality over mlxsw. Each registered L3 drop # packet trap is tested to make sure it is triggered under the right # conditions. # +---------------------------------+ # | H1 (vrf) | # | + $h1 | # | | 192.0.2.1/24 | # | | 2001:db8:1::1/64 | # | | | # | | default via 192.0.2.2 | # | | default via 2001:db8:1::2 | # +----|----------------------------+ # | # +----|----------------------------------------------------------------------+ # | SW | | # | + $rp1 | # | 192.0.2.2/24 | # | 2001:db8:1::2/64 | # | | # | 2001:db8:2::2/64 | # | 198.51.100.2/24 | # | + $rp2 | # | | | # +----|----------------------------------------------------------------------+ # | # +----|----------------------------+ # | | default via 198.51.100.2 | # | | default via 2001:db8:2::2 | # | | | # | | 2001:db8:2::1/64 | # | | 198.51.100.1/24 | # | + $h2 | # | H2 (vrf) | # +---------------------------------+ lib_dir=$(dirname $0)/../../../net/forwarding ALL_TESTS=" non_ip_test uc_dip_over_mc_dmac_test dip_is_loopback_test sip_is_mc_test sip_is_loopback_test ip_header_corrupted_test ipv4_sip_is_limited_bc_test ipv6_mc_dip_reserved_scope_test ipv6_mc_dip_interface_local_scope_test blackhole_route_test irif_disabled_test erif_disabled_test blackhole_nexthop_test " NUM_NETIFS=4 source $lib_dir/lib.sh source $lib_dir/tc_common.sh source $lib_dir/devlink_lib.sh h1_create() { simple_if_init $h1 192.0.2.1/24 2001:db8:1::1/64 ip -4 route add default vrf v$h1 nexthop via 192.0.2.2 ip -6 route add default vrf v$h1 nexthop via 2001:db8:1::2 } h1_destroy() { ip -6 route del default vrf v$h1 nexthop via 2001:db8:1::2 ip -4 route del default vrf v$h1 nexthop via 192.0.2.2 simple_if_fini $h1 192.0.2.1/24 2001:db8:1::1/64 } h2_create() { simple_if_init $h2 $h2_ipv4/24 $h2_ipv6/64 ip -4 route add default vrf v$h2 nexthop via 198.51.100.2 ip -6 route add default vrf v$h2 nexthop via 2001:db8:2::2 } h2_destroy() { ip -6 route del default vrf v$h2 nexthop via 2001:db8:2::2 ip -4 route del default vrf v$h2 nexthop via 198.51.100.2 simple_if_fini $h2 $h2_ipv4/24 $h2_ipv6/64 } router_create() { ip link set dev $rp1 up ip link set dev $rp2 up tc qdisc add dev $rp2 clsact __addr_add_del $rp1 add 192.0.2.2/24 2001:db8:1::2/64 __addr_add_del $rp2 add 198.51.100.2/24 2001:db8:2::2/64 } router_destroy() { __addr_add_del $rp2 del 198.51.100.2/24 2001:db8:2::2/64 __addr_add_del $rp1 del 192.0.2.2/24 2001:db8:1::2/64 tc qdisc del dev $rp2 clsact ip link set dev $rp2 down ip link set dev $rp1 down } setup_prepare() { h1=${NETIFS[p1]} rp1=${NETIFS[p2]} rp2=${NETIFS[p3]} h2=${NETIFS[p4]} h1mac=$(mac_get $h1) rp1mac=$(mac_get $rp1) h1_ipv4=192.0.2.1 h2_ipv4=198.51.100.1 h1_ipv6=2001:db8:1::1 h2_ipv6=2001:db8:2::1 vrf_prepare forwarding_enable h1_create h2_create router_create } cleanup() { pre_cleanup router_destroy h2_destroy h1_destroy forwarding_restore vrf_cleanup } ping_check() { trap_name=$1; shift devlink_trap_action_set $trap_name "trap" ping_do $h1 $h2_ipv4 check_err $? "Packets that should not be trapped were trapped" devlink_trap_action_set $trap_name "drop" } non_ip_test() { local trap_name="non_ip" local mz_pid RET=0 ping_check $trap_name tc filter add dev $rp2 egress protocol ip pref 1 handle 101 \ flower dst_ip $h2_ipv4 action drop # Generate non-IP packets to the router $MZ $h1 -c 0 -p 100 -d 1msec -B $h2_ipv4 -q "$rp1mac $h1mac \ 00:00 de:ad:be:ef" & mz_pid=$! devlink_trap_drop_test $trap_name $rp2 101 log_test "Non IP" devlink_trap_drop_cleanup $mz_pid $rp2 "ip" 1 101 } __uc_dip_over_mc_dmac_test() { local desc=$1; shift local proto=$1; shift local dip=$1; shift local flags=${1:-""}; shift local trap_name="uc_dip_over_mc_dmac" local dmac=01:02:03:04:05:06 local mz_pid RET=0 ping_check $trap_name tc filter add dev $rp2 egress protocol $proto pref 1 handle 101 \ flower ip_proto udp src_port 54321 dst_port 12345 action drop # Generate IP packets with a unicast IP and a multicast destination MAC $MZ $h1 $flags -t udp "sp=54321,dp=12345" -c 0 -p 100 -b $dmac \ -B $dip -d 1msec -q & mz_pid=$! devlink_trap_drop_test $trap_name $rp2 101 log_test "Unicast destination IP over multicast destination MAC: $desc" devlink_trap_drop_cleanup $mz_pid $rp2 $proto 1 101 } uc_dip_over_mc_dmac_test() { __uc_dip_over_mc_dmac_test "IPv4" "ip" $h2_ipv4 __uc_dip_over_mc_dmac_test "IPv6" "ipv6" $h2_ipv6 "-6" } __sip_is_loopback_test() { local desc=$1; shift local proto=$1; shift local sip=$1; shift local dip=$1; shift local flags=${1:-""}; shift local trap_name="sip_is_loopback_address" local mz_pid RET=0 ping_check $trap_name tc filter add dev $rp2 egress protocol $proto pref 1 handle 101 \ flower src_ip $sip action drop # Generate packets with loopback source IP $MZ $h1 $flags -t udp "sp=54321,dp=12345" -c 0 -p 100 -A $sip \ -b $rp1mac -B $dip -d 1msec -q & mz_pid=$! devlink_trap_drop_test $trap_name $rp2 101 log_test "Source IP is loopback address: $desc" devlink_trap_drop_cleanup $mz_pid $rp2 $proto 1 101 } sip_is_loopback_test() { __sip_is_loopback_test "IPv4" "ip" "127.0.0.0/8" $h2_ipv4 __sip_is_loopback_test "IPv6" "ipv6" "::1" $h2_ipv6 "-6" } __dip_is_loopback_test() { local desc=$1; shift local proto=$1; shift local dip=$1; shift local flags=${1:-""}; shift local trap_name="dip_is_loopback_address" local mz_pid RET=0 ping_check $trap_name tc filter add dev $rp2 egress protocol $proto pref 1 handle 101 \ flower dst_ip $dip action drop # Generate packets with loopback destination IP $MZ $h1 $flags -t udp "sp=54321,dp=12345" -c 0 -p 100 -b $rp1mac \ -B $dip -d 1msec -q & mz_pid=$! devlink_trap_drop_test $trap_name $rp2 101 log_test "Destination IP is loopback address: $desc" devlink_trap_drop_cleanup $mz_pid $rp2 $proto 1 101 } dip_is_loopback_test() { __dip_is_loopback_test "IPv4" "ip" "127.0.0.0/8" __dip_is_loopback_test "IPv6" "ipv6" "::1" "-6" } __sip_is_mc_test() { local desc=$1; shift local proto=$1; shift local sip=$1; shift local dip=$1; shift local flags=${1:-""}; shift local trap_name="sip_is_mc" local mz_pid RET=0 ping_check $trap_name tc filter add dev $rp2 egress protocol $proto pref 1 handle 101 \ flower src_ip $sip action drop # Generate packets with multicast source IP $MZ $h1 $flags -t udp "sp=54321,dp=12345" -c 0 -p 100 -A $sip \ -b $rp1mac -B $dip -d 1msec -q & mz_pid=$! devlink_trap_drop_test $trap_name $rp2 101 log_test "Source IP is multicast: $desc" devlink_trap_drop_cleanup $mz_pid $rp2 $proto 1 101 } sip_is_mc_test() { __sip_is_mc_test "IPv4" "ip" "239.1.1.1" $h2_ipv4 __sip_is_mc_test "IPv6" "ipv6" "FF02::2" $h2_ipv6 "-6" } ipv4_sip_is_limited_bc_test() { local trap_name="ipv4_sip_is_limited_bc" local sip=255.255.255.255 local mz_pid RET=0 ping_check $trap_name tc filter add dev $rp2 egress protocol ip pref 1 handle 101 \ flower src_ip $sip action drop # Generate packets with limited broadcast source IP $MZ $h1 -t udp "sp=54321,dp=12345" -c 0 -p 100 -A $sip -b $rp1mac \ -B $h2_ipv4 -d 1msec -q & mz_pid=$! devlink_trap_drop_test $trap_name $rp2 101 log_test "IPv4 source IP is limited broadcast" devlink_trap_drop_cleanup $mz_pid $rp2 "ip" 1 101 } ipv4_payload_get() { local ipver=$1; shift local ihl=$1; shift local checksum=$1; shift p=$(: )"08:00:"$( : ETH type )"$ipver"$( : IP version )"$ihl:"$( : IHL )"00:"$( : IP TOS )"00:F4:"$( : IP total length )"00:00:"$( : IP identification )"20:00:"$( : IP flags + frag off )"30:"$( : IP TTL )"01:"$( : IP proto )"$checksum:"$( : IP header csum )"$h1_ipv4:"$( : IP saddr )"$h2_ipv4:"$( : IP daddr ) echo $p } __ipv4_header_corrupted_test() { local desc=$1; shift local ipver=$1; shift local ihl=$1; shift local checksum=$1; shift local trap_name="ip_header_corrupted" local payload local mz_pid RET=0 ping_check $trap_name tc filter add dev $rp2 egress protocol ip pref 1 handle 101 \ flower dst_ip $h2_ipv4 action drop payload=$(ipv4_payload_get $ipver $ihl $checksum) # Generate packets with corrupted IP header $MZ $h1 -c 0 -d 1msec -a $h1mac -b $rp1mac -q p=$payload & mz_pid=$! devlink_trap_drop_test $trap_name $rp2 101 log_test "IP header corrupted: $desc: IPv4" devlink_trap_drop_cleanup $mz_pid $rp2 "ip" 1 101 } ipv6_payload_get() { local ipver=$1; shift p=$(: )"86:DD:"$( : ETH type )"$ipver"$( : IP version )"0:0:"$( : Traffic class )"0:00:00:"$( : Flow label )"00:00:"$( : Payload length )"01:"$( : Next header )"04:"$( : Hop limit )"$h1_ipv6:"$( : IP saddr )"$h2_ipv6:"$( : IP daddr ) echo $p } __ipv6_header_corrupted_test() { local desc=$1; shift local ipver=$1; shift local trap_name="ip_header_corrupted" local payload local mz_pid RET=0 ping_check $trap_name tc filter add dev $rp2 egress protocol ip pref 1 handle 101 \ flower dst_ip $h2_ipv4 action drop payload=$(ipv6_payload_get $ipver) # Generate packets with corrupted IP header $MZ $h1 -c 0 -d 1msec -a $h1mac -b $rp1mac -q p=$payload & mz_pid=$! devlink_trap_drop_test $trap_name $rp2 101 log_test "IP header corrupted: $desc: IPv6" devlink_trap_drop_cleanup $mz_pid $rp2 "ip" 1 101 } ip_header_corrupted_test() { # Each test uses one wrong value. The three values below are correct. local ipv="4" local ihl="5" local checksum="00:F4" __ipv4_header_corrupted_test "wrong IP version" 5 $ihl $checksum __ipv4_header_corrupted_test "wrong IHL" $ipv 4 $checksum __ipv4_header_corrupted_test "wrong checksum" $ipv $ihl "00:00" __ipv6_header_corrupted_test "wrong IP version" 5 } ipv6_mc_dip_reserved_scope_test() { local trap_name="ipv6_mc_dip_reserved_scope" local dip=FF00:: local mz_pid RET=0 ping_check $trap_name tc filter add dev $rp2 egress protocol ipv6 pref 1 handle 101 \ flower dst_ip $dip action drop # Generate packets with reserved scope destination IP $MZ $h1 -6 -t udp "sp=54321,dp=12345" -c 0 -p 100 -b \ "33:33:00:00:00:00" -B $dip -d 1msec -q & mz_pid=$! devlink_trap_drop_test $trap_name $rp2 101 log_test "IPv6 multicast destination IP reserved scope" devlink_trap_drop_cleanup $mz_pid $rp2 "ipv6" 1 101 } ipv6_mc_dip_interface_local_scope_test() { local trap_name="ipv6_mc_dip_interface_local_scope" local dip=FF01:: local mz_pid RET=0 ping_check $trap_name tc filter add dev $rp2 egress protocol ipv6 pref 1 handle 101 \ flower dst_ip $dip action drop # Generate packets with interface local scope destination IP $MZ $h1 -6 -t udp "sp=54321,dp=12345" -c 0 -p 100 -b \ "33:33:00:00:00:00" -B $dip -d 1msec -q & mz_pid=$! devlink_trap_drop_test $trap_name $rp2 101 log_test "IPv6 multicast destination IP interface-local scope" devlink_trap_drop_cleanup $mz_pid $rp2 "ipv6" 1 101 } __blackhole_route_test() { local flags=$1; shift local subnet=$1; shift local proto=$1; shift local dip=$1; shift local ip_proto=${1:-"icmp"}; shift local trap_name="blackhole_route" local mz_pid RET=0 ping_check $trap_name ip -$flags route add blackhole $subnet tc filter add dev $rp2 egress protocol $proto pref 1 handle 101 \ flower skip_hw dst_ip $dip ip_proto $ip_proto action drop # Generate packets to the blackhole route $MZ $h1 -$flags -t udp "sp=54321,dp=12345" -c 0 -p 100 -b $rp1mac \ -B $dip -d 1msec -q & mz_pid=$! devlink_trap_drop_test $trap_name $rp2 101 log_test "Blackhole route: IPv$flags" devlink_trap_drop_cleanup $mz_pid $rp2 $proto 1 101 ip -$flags route del blackhole $subnet } blackhole_route_test() { __blackhole_route_test "4" "198.51.100.0/30" "ip" $h2_ipv4 __blackhole_route_test "6" "2001:db8:2::/120" "ipv6" $h2_ipv6 "icmpv6" } irif_disabled_test() { local trap_name="irif_disabled" local t0_packets t0_bytes local t1_packets t1_bytes local mz_pid RET=0 ping_check $trap_name devlink_trap_action_set $trap_name "trap" # When RIF of a physical port ("Sub-port RIF") is destroyed, we first # block the STP of the {Port, VLAN} so packets cannot get into the RIF. # Using bridge enables us to see this trap because when bridge is # destroyed, there is a small time window that packets can go into the # RIF, while it is disabled. ip link add dev br0 type bridge ip link set dev $rp1 master br0 ip address flush dev $rp1 __addr_add_del br0 add 192.0.2.2/24 ip li set dev br0 up t0_packets=$(devlink_trap_rx_packets_get $trap_name) t0_bytes=$(devlink_trap_rx_bytes_get $trap_name) # Generate packets to h2 through br0 RIF that will be removed later $MZ $h1 -t udp "sp=54321,dp=12345" -c 0 -p 100 -a own -b $rp1mac \ -B $h2_ipv4 -q & mz_pid=$! # Wait before removing br0 RIF to allow packets to go into the bridge. sleep 1 # Flushing address will dismantle the RIF ip address flush dev br0 t1_packets=$(devlink_trap_rx_packets_get $trap_name) t1_bytes=$(devlink_trap_rx_bytes_get $trap_name) if [[ $t0_packets -eq $t1_packets && $t0_bytes -eq $t1_bytes ]]; then check_err 1 "Trap stats idle when packets should be trapped" fi log_test "Ingress RIF disabled" kill $mz_pid && wait $mz_pid &> /dev/null ip link set dev $rp1 nomaster __addr_add_del $rp1 add 192.0.2.2/24 2001:db8:1::2/64 ip link del dev br0 type bridge devlink_trap_action_set $trap_name "drop" } erif_disabled_test() { local trap_name="erif_disabled" local t0_packets t0_bytes local t1_packets t1_bytes local mz_pid RET=0 ping_check $trap_name devlink_trap_action_set $trap_name "trap" ip link add dev br0 type bridge ip add flush dev $rp1 ip link set dev $rp1 master br0 __addr_add_del br0 add 192.0.2.2/24 ip link set dev br0 up t0_packets=$(devlink_trap_rx_packets_get $trap_name) t0_bytes=$(devlink_trap_rx_bytes_get $trap_name) rp2mac=$(mac_get $rp2) # Generate packets that should go out through br0 RIF that will be # removed later $MZ $h2 -t udp "sp=54321,dp=12345" -c 0 -p 100 -a own -b $rp2mac \ -B 192.0.2.1 -q & mz_pid=$! sleep 5 # Unlinking the port from the bridge will disable the RIF associated # with br0 as it is no longer an upper of any mlxsw port. ip link set dev $rp1 nomaster t1_packets=$(devlink_trap_rx_packets_get $trap_name) t1_bytes=$(devlink_trap_rx_bytes_get $trap_name) if [[ $t0_packets -eq $t1_packets && $t0_bytes -eq $t1_bytes ]]; then check_err 1 "Trap stats idle when packets should be trapped" fi log_test "Egress RIF disabled" kill $mz_pid && wait $mz_pid &> /dev/null __addr_add_del $rp1 add 192.0.2.2/24 2001:db8:1::2/64 ip link del dev br0 type bridge devlink_trap_action_set $trap_name "drop" } __blackhole_nexthop_test() { local flags=$1; shift local subnet=$1; shift local proto=$1; shift local dip=$1; shift local trap_name="blackhole_nexthop" local mz_pid RET=0 ip -$flags nexthop add id 1 blackhole ip -$flags route add $subnet nhid 1 tc filter add dev $rp2 egress protocol $proto pref 1 handle 101 \ flower skip_hw dst_ip $dip ip_proto udp action drop # Generate packets to the blackhole nexthop $MZ $h1 -$flags -t udp "sp=54321,dp=12345" -c 0 -p 100 -b $rp1mac \ -B $dip -d 1msec -q & mz_pid=$! devlink_trap_drop_test $trap_name $rp2 101 log_test "Blackhole nexthop: IPv$flags" devlink_trap_drop_cleanup $mz_pid $rp2 $proto 1 101 ip -$flags route del $subnet ip -$flags nexthop del id 1 } blackhole_nexthop_test() { __blackhole_nexthop_test "4" "198.51.100.0/30" "ip" $h2_ipv4 __blackhole_nexthop_test "6" "2001:db8:2::/120" "ipv6" $h2_ipv6 } trap cleanup EXIT setup_prepare setup_wait tests_run exit $EXIT_STATUS |