1#!/bin/bash 2# SPDX-License-Identifier: GPL-2.0 3 4ALL_TESTS="standalone vlan_unaware_bridge vlan_aware_bridge test_vlan \ 5 vlan_over_vlan_unaware_bridged_port vlan_over_vlan_aware_bridged_port \ 6 vlan_over_vlan_unaware_bridge vlan_over_vlan_aware_bridge" 7NUM_NETIFS=2 8PING_COUNT=1 9REQUIRE_MTOOLS=yes 10 11source lib.sh 12 13H1_IPV4="192.0.2.1" 14H2_IPV4="192.0.2.2" 15H1_IPV6="2001:db8:1::1" 16H2_IPV6="2001:db8:1::2" 17 18BRIDGE_ADDR="00:00:de:ad:be:ee" 19MACVLAN_ADDR="00:00:de:ad:be:ef" 20UNKNOWN_UC_ADDR1="de:ad:be:ef:ee:03" 21UNKNOWN_UC_ADDR2="de:ad:be:ef:ee:04" 22UNKNOWN_UC_ADDR3="de:ad:be:ef:ee:05" 23JOINED_IPV4_MC_ADDR="225.1.2.3" 24UNKNOWN_IPV4_MC_ADDR1="225.1.2.4" 25UNKNOWN_IPV4_MC_ADDR2="225.1.2.5" 26UNKNOWN_IPV4_MC_ADDR3="225.1.2.6" 27JOINED_IPV6_MC_ADDR="ff2e::0102:0304" 28UNKNOWN_IPV6_MC_ADDR1="ff2e::0102:0305" 29UNKNOWN_IPV6_MC_ADDR2="ff2e::0102:0306" 30UNKNOWN_IPV6_MC_ADDR3="ff2e::0102:0307" 31 32JOINED_MACV4_MC_ADDR="01:00:5e:01:02:03" 33UNKNOWN_MACV4_MC_ADDR1="01:00:5e:01:02:04" 34UNKNOWN_MACV4_MC_ADDR2="01:00:5e:01:02:05" 35UNKNOWN_MACV4_MC_ADDR3="01:00:5e:01:02:06" 36JOINED_MACV6_MC_ADDR="33:33:01:02:03:04" 37UNKNOWN_MACV6_MC_ADDR1="33:33:01:02:03:05" 38UNKNOWN_MACV6_MC_ADDR2="33:33:01:02:03:06" 39UNKNOWN_MACV6_MC_ADDR3="33:33:01:02:03:07" 40 41PTP_1588_L2_SYNC=" \ 4201:1b:19:00:00:00 00:00:de:ad:be:ef 88:f7 00 02 \ 4300 2c 00 00 02 00 00 00 00 00 00 00 00 00 00 00 \ 4400 00 3e 37 63 ff fe cf 17 0e 00 01 00 00 00 00 \ 4500 00 00 00 00 00 00 00 00 00" 46PTP_1588_L2_FOLLOW_UP=" \ 4701:1b:19:00:00:00 00:00:de:ad:be:ef 88:f7 08 02 \ 4800 2c 00 00 00 00 00 00 00 00 00 00 00 00 00 00 \ 4900 00 3e 37 63 ff fe cf 17 0e 00 01 00 00 02 00 \ 5000 00 66 83 c5 f1 17 97 ed f0" 51PTP_1588_L2_PDELAY_REQ=" \ 5201:80:c2:00:00:0e 00:00:de:ad:be:ef 88:f7 02 02 \ 5300 36 00 00 00 00 00 00 00 00 00 00 00 00 00 00 \ 5400 00 3e 37 63 ff fe cf 17 0e 00 01 00 06 05 7f \ 5500 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 \ 5600 00 00 00" 57PTP_1588_IPV4_SYNC=" \ 5801:00:5e:00:01:81 00:00:de:ad:be:ef 08:00 45 00 \ 5900 48 0a 9a 40 00 01 11 cb 88 c0 00 02 01 e0 00 \ 6001 81 01 3f 01 3f 00 34 a3 c8 00 02 00 2c 00 00 \ 6102 00 00 00 00 00 00 00 00 00 00 00 00 00 3e 37 \ 6263 ff fe cf 17 0e 00 01 00 00 00 00 00 00 00 00 \ 6300 00 00 00 00 00" 64PTP_1588_IPV4_FOLLOW_UP=" 6501:00:5e:00:01:81 00:00:de:ad:be:ef 08:00 45 00 \ 6600 48 0a 9b 40 00 01 11 cb 87 c0 00 02 01 e0 00 \ 6701 81 01 40 01 40 00 34 a3 c8 08 02 00 2c 00 00 \ 6800 00 00 00 00 00 00 00 00 00 00 00 00 00 3e 37 \ 6963 ff fe cf 17 0e 00 01 00 00 02 00 00 00 66 83 \ 70c6 0f 1d 9a 61 87" 71PTP_1588_IPV4_PDELAY_REQ=" \ 7201:00:5e:00:00:6b 00:00:de:ad:be:ef 08:00 45 00 \ 7300 52 35 a9 40 00 01 11 a1 85 c0 00 02 01 e0 00 \ 7400 6b 01 3f 01 3f 00 3e a2 bc 02 02 00 36 00 00 \ 7500 00 00 00 00 00 00 00 00 00 00 00 00 00 3e 37 \ 7663 ff fe cf 17 0e 00 01 00 01 05 7f 00 00 00 00 \ 7700 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" 78PTP_1588_IPV6_SYNC=" \ 7933:33:00:00:01:81 00:00:de:ad:be:ef 86:dd 60 06 \ 807c 2f 00 36 11 01 20 01 0d b8 00 01 00 00 00 00 \ 8100 00 00 00 00 01 ff 0e 00 00 00 00 00 00 00 00 \ 8200 00 00 00 01 81 01 3f 01 3f 00 36 2e 92 00 02 \ 8300 2c 00 00 02 00 00 00 00 00 00 00 00 00 00 00 \ 8400 00 3e 37 63 ff fe cf 17 0e 00 01 00 00 00 00 \ 8500 00 00 00 00 00 00 00 00 00 00 00" 86PTP_1588_IPV6_FOLLOW_UP=" \ 8733:33:00:00:01:81 00:00:de:ad:be:ef 86:dd 60 0a \ 8800 bc 00 36 11 01 20 01 0d b8 00 01 00 00 00 00 \ 8900 00 00 00 00 01 ff 0e 00 00 00 00 00 00 00 00 \ 9000 00 00 00 01 81 01 40 01 40 00 36 2e 92 08 02 \ 9100 2c 00 00 00 00 00 00 00 00 00 00 00 00 00 00 \ 9200 00 3e 37 63 ff fe cf 17 0e 00 01 00 00 02 00 \ 9300 00 66 83 c6 2a 32 09 bd 74 00 00" 94PTP_1588_IPV6_PDELAY_REQ=" \ 9533:33:00:00:00:6b 00:00:de:ad:be:ef 86:dd 60 0c \ 965c fd 00 40 11 01 fe 80 00 00 00 00 00 00 3c 37 \ 9763 ff fe cf 17 0e ff 02 00 00 00 00 00 00 00 00 \ 9800 00 00 00 00 6b 01 3f 01 3f 00 40 b4 54 02 02 \ 9900 36 00 00 00 00 00 00 00 00 00 00 00 00 00 00 \ 10000 00 3e 37 63 ff fe cf 17 0e 00 01 00 01 05 7f \ 10100 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 \ 10200 00 00 00 00 00" 103 104# Disable promisc to ensure we don't receive unknown MAC DA packets 105export TCPDUMP_EXTRA_FLAGS="-pl" 106 107h1=${NETIFS[p1]} 108h2=${NETIFS[p2]} 109 110send_raw() 111{ 112 local if_name=$1; shift 113 local pkt="$1"; shift 114 local smac=$(mac_get $if_name) 115 116 pkt="${pkt/00:00:de:ad:be:ef/$smac}" 117 118 $MZ -q $if_name "$pkt" 119} 120 121send_uc_ipv4() 122{ 123 local if_name=$1 124 local dmac=$2 125 126 ip neigh add $H2_IPV4 lladdr $dmac dev $if_name 127 ping_do $if_name $H2_IPV4 128 ip neigh del $H2_IPV4 dev $if_name 129} 130 131check_rcv() 132{ 133 local if_name=$1; shift 134 local type=$1; shift 135 local pattern=$1; shift 136 local should_receive=$1; shift 137 local test_name="$1"; shift 138 local should_fail= 139 140 [ $should_receive = true ] && should_fail=0 || should_fail=1 141 RET=0 142 143 tcpdump_show $if_name | grep -q "$pattern" 144 145 check_err_fail "$should_fail" "$?" "reception" 146 147 log_test "$test_name: $type" 148} 149 150mc_route_prepare() 151{ 152 local if_name=$1 153 local vrf_name=$(master_name_get $if_name) 154 155 ip route add 225.100.1.0/24 dev $if_name vrf $vrf_name 156 ip -6 route add ff2e::/64 dev $if_name vrf $vrf_name 157} 158 159mc_route_destroy() 160{ 161 local if_name=$1 162 local vrf_name=$(master_name_get $if_name) 163 164 ip route del 225.100.1.0/24 dev $if_name vrf $vrf_name 165 ip -6 route del ff2e::/64 dev $if_name vrf $vrf_name 166} 167 168run_test() 169{ 170 local send_if_name=$1; shift 171 local rcv_if_name=$1; shift 172 local skip_ptp=$1; shift 173 local no_unicast_flt=$1; shift 174 local test_name="$1"; shift 175 local smac=$(mac_get $send_if_name) 176 local rcv_dmac=$(mac_get $rcv_if_name) 177 local should_receive 178 179 setup_wait 180 181 tcpdump_start $rcv_if_name 182 183 mc_route_prepare $send_if_name 184 mc_route_prepare $rcv_if_name 185 186 send_uc_ipv4 $send_if_name $rcv_dmac 187 send_uc_ipv4 $send_if_name $MACVLAN_ADDR 188 send_uc_ipv4 $send_if_name $UNKNOWN_UC_ADDR1 189 190 ip link set dev $rcv_if_name promisc on 191 send_uc_ipv4 $send_if_name $UNKNOWN_UC_ADDR2 192 mc_send $send_if_name $UNKNOWN_IPV4_MC_ADDR2 193 mc_send $send_if_name $UNKNOWN_IPV6_MC_ADDR2 194 ip link set dev $rcv_if_name promisc off 195 196 mc_join $rcv_if_name $JOINED_IPV4_MC_ADDR 197 mc_send $send_if_name $JOINED_IPV4_MC_ADDR 198 mc_leave 199 200 mc_join $rcv_if_name $JOINED_IPV6_MC_ADDR 201 mc_send $send_if_name $JOINED_IPV6_MC_ADDR 202 mc_leave 203 204 mc_send $send_if_name $UNKNOWN_IPV4_MC_ADDR1 205 mc_send $send_if_name $UNKNOWN_IPV6_MC_ADDR1 206 207 ip link set dev $rcv_if_name allmulticast on 208 send_uc_ipv4 $send_if_name $UNKNOWN_UC_ADDR3 209 mc_send $send_if_name $UNKNOWN_IPV4_MC_ADDR3 210 mc_send $send_if_name $UNKNOWN_IPV6_MC_ADDR3 211 ip link set dev $rcv_if_name allmulticast off 212 213 mc_route_destroy $rcv_if_name 214 mc_route_destroy $send_if_name 215 216 if [ $skip_ptp = false ]; then 217 ip maddress add 01:1b:19:00:00:00 dev $rcv_if_name 218 send_raw $send_if_name "$PTP_1588_L2_SYNC" 219 send_raw $send_if_name "$PTP_1588_L2_FOLLOW_UP" 220 ip maddress del 01:1b:19:00:00:00 dev $rcv_if_name 221 222 ip maddress add 01:80:c2:00:00:0e dev $rcv_if_name 223 send_raw $send_if_name "$PTP_1588_L2_PDELAY_REQ" 224 ip maddress del 01:80:c2:00:00:0e dev $rcv_if_name 225 226 mc_join $rcv_if_name 224.0.1.129 227 send_raw $send_if_name "$PTP_1588_IPV4_SYNC" 228 send_raw $send_if_name "$PTP_1588_IPV4_FOLLOW_UP" 229 mc_leave 230 231 mc_join $rcv_if_name 224.0.0.107 232 send_raw $send_if_name "$PTP_1588_IPV4_PDELAY_REQ" 233 mc_leave 234 235 mc_join $rcv_if_name ff0e::181 236 send_raw $send_if_name "$PTP_1588_IPV6_SYNC" 237 send_raw $send_if_name "$PTP_1588_IPV6_FOLLOW_UP" 238 mc_leave 239 240 mc_join $rcv_if_name ff02::6b 241 send_raw $send_if_name "$PTP_1588_IPV6_PDELAY_REQ" 242 mc_leave 243 fi 244 245 sleep 1 246 247 tcpdump_stop $rcv_if_name 248 249 check_rcv $rcv_if_name "Unicast IPv4 to primary MAC address" \ 250 "$smac > $rcv_dmac, ethertype IPv4 (0x0800)" \ 251 true "$test_name" 252 253 check_rcv $rcv_if_name "Unicast IPv4 to macvlan MAC address" \ 254 "$smac > $MACVLAN_ADDR, ethertype IPv4 (0x0800)" \ 255 true "$test_name" 256 257 [ $no_unicast_flt = true ] && should_receive=true || should_receive=false 258 check_rcv $rcv_if_name "Unicast IPv4 to unknown MAC address" \ 259 "$smac > $UNKNOWN_UC_ADDR1, ethertype IPv4 (0x0800)" \ 260 $should_receive "$test_name" 261 262 check_rcv $rcv_if_name "Unicast IPv4 to unknown MAC address, promisc" \ 263 "$smac > $UNKNOWN_UC_ADDR2, ethertype IPv4 (0x0800)" \ 264 true "$test_name" 265 266 [ $no_unicast_flt = true ] && should_receive=true || should_receive=false 267 check_rcv $rcv_if_name \ 268 "Unicast IPv4 to unknown MAC address, allmulti" \ 269 "$smac > $UNKNOWN_UC_ADDR3, ethertype IPv4 (0x0800)" \ 270 $should_receive "$test_name" 271 272 check_rcv $rcv_if_name "Multicast IPv4 to joined group" \ 273 "$smac > $JOINED_MACV4_MC_ADDR, ethertype IPv4 (0x0800)" \ 274 true "$test_name" 275 276 xfail \ 277 check_rcv $rcv_if_name \ 278 "Multicast IPv4 to unknown group" \ 279 "$smac > $UNKNOWN_MACV4_MC_ADDR1, ethertype IPv4 (0x0800)" \ 280 false "$test_name" 281 282 check_rcv $rcv_if_name "Multicast IPv4 to unknown group, promisc" \ 283 "$smac > $UNKNOWN_MACV4_MC_ADDR2, ethertype IPv4 (0x0800)" \ 284 true "$test_name" 285 286 check_rcv $rcv_if_name "Multicast IPv4 to unknown group, allmulti" \ 287 "$smac > $UNKNOWN_MACV4_MC_ADDR3, ethertype IPv4 (0x0800)" \ 288 true "$test_name" 289 290 check_rcv $rcv_if_name "Multicast IPv6 to joined group" \ 291 "$smac > $JOINED_MACV6_MC_ADDR, ethertype IPv6 (0x86dd)" \ 292 true "$test_name" 293 294 xfail \ 295 check_rcv $rcv_if_name "Multicast IPv6 to unknown group" \ 296 "$smac > $UNKNOWN_MACV6_MC_ADDR1, ethertype IPv6 (0x86dd)" \ 297 false "$test_name" 298 299 check_rcv $rcv_if_name "Multicast IPv6 to unknown group, promisc" \ 300 "$smac > $UNKNOWN_MACV6_MC_ADDR2, ethertype IPv6 (0x86dd)" \ 301 true "$test_name" 302 303 check_rcv $rcv_if_name "Multicast IPv6 to unknown group, allmulti" \ 304 "$smac > $UNKNOWN_MACV6_MC_ADDR3, ethertype IPv6 (0x86dd)" \ 305 true "$test_name" 306 307 if [ $skip_ptp = false ]; then 308 check_rcv $rcv_if_name "1588v2 over L2 transport, Sync" \ 309 "ethertype PTP (0x88f7).* PTPv2.* msg type : sync msg" \ 310 true "$test_name" 311 312 check_rcv $rcv_if_name "1588v2 over L2 transport, Follow-Up" \ 313 "ethertype PTP (0x88f7).* PTPv2.* msg type : follow up msg" \ 314 true "$test_name" 315 316 check_rcv $rcv_if_name "1588v2 over L2 transport, Peer Delay Request" \ 317 "ethertype PTP (0x88f7).* PTPv2.* msg type : peer delay req msg" \ 318 true "$test_name" 319 320 check_rcv $rcv_if_name "1588v2 over IPv4, Sync" \ 321 "ethertype IPv4 (0x0800).* PTPv2.* msg type : sync msg" \ 322 true "$test_name" 323 324 check_rcv $rcv_if_name "1588v2 over IPv4, Follow-Up" \ 325 "ethertype IPv4 (0x0800).* PTPv2.* msg type : follow up msg" \ 326 true "$test_name" 327 328 check_rcv $rcv_if_name "1588v2 over IPv4, Peer Delay Request" \ 329 "ethertype IPv4 (0x0800).* PTPv2.* msg type : peer delay req msg" \ 330 true "$test_name" 331 332 check_rcv $rcv_if_name "1588v2 over IPv6, Sync" \ 333 "ethertype IPv6 (0x86dd).* PTPv2.* msg type : sync msg" \ 334 true "$test_name" 335 336 check_rcv $rcv_if_name "1588v2 over IPv6, Follow-Up" \ 337 "ethertype IPv6 (0x86dd).* PTPv2.* msg type : follow up msg" \ 338 true "$test_name" 339 340 check_rcv $rcv_if_name "1588v2 over IPv6, Peer Delay Request" \ 341 "ethertype IPv6 (0x86dd).* PTPv2.* msg type : peer delay req msg" \ 342 true "$test_name" 343 fi 344 345 tcpdump_cleanup $rcv_if_name 346} 347 348h1_create() 349{ 350 simple_if_init $h1 $H1_IPV4/24 $H1_IPV6/64 351} 352 353h1_destroy() 354{ 355 simple_if_fini $h1 $H1_IPV4/24 $H1_IPV6/64 356} 357 358h2_create() 359{ 360 simple_if_init $h2 $H2_IPV4/24 $H2_IPV6/64 361} 362 363h2_destroy() 364{ 365 simple_if_fini $h2 $H2_IPV4/24 $H2_IPV6/64 366} 367 368h1_vlan_create() 369{ 370 simple_if_init $h1 371 vlan_create $h1 100 v$h1 $H1_IPV4/24 $H1_IPV6/64 372} 373 374h1_vlan_destroy() 375{ 376 vlan_destroy $h1 100 377 simple_if_fini $h1 378} 379 380h2_vlan_create() 381{ 382 simple_if_init $h2 383 vlan_create $h2 100 v$h2 $H2_IPV4/24 $H2_IPV6/64 384} 385 386h2_vlan_destroy() 387{ 388 vlan_destroy $h2 100 389 simple_if_fini $h2 390} 391 392bridge_create() 393{ 394 local vlan_filtering=$1 395 396 ip link add br0 type bridge vlan_filtering $vlan_filtering 397 ip link set br0 address $BRIDGE_ADDR 398 ip link set br0 up 399 400 ip link set $h2 master br0 401 ip link set $h2 up 402} 403 404bridge_destroy() 405{ 406 ip link del br0 407} 408 409macvlan_create() 410{ 411 local lower=$1 412 413 ip link add link $lower name macvlan0 type macvlan mode private 414 ip link set macvlan0 address $MACVLAN_ADDR 415 ip link set macvlan0 up 416} 417 418macvlan_destroy() 419{ 420 ip link del macvlan0 421} 422 423standalone() 424{ 425 local no_unicast_flt=true 426 local skip_ptp=false 427 428 if [ $(has_unicast_flt $h2) = yes ]; then 429 no_unicast_flt=false 430 fi 431 432 h1_create 433 h2_create 434 macvlan_create $h2 435 436 run_test $h1 $h2 $skip_ptp $no_unicast_flt "$h2" 437 438 macvlan_destroy 439 h2_destroy 440 h1_destroy 441} 442 443test_bridge() 444{ 445 local no_unicast_flt=true 446 local vlan_filtering=$1 447 local skip_ptp=true 448 449 h1_create 450 bridge_create $vlan_filtering 451 simple_if_init br0 $H2_IPV4/24 $H2_IPV6/64 452 macvlan_create br0 453 454 run_test $h1 br0 $skip_ptp $no_unicast_flt \ 455 "vlan_filtering=$vlan_filtering bridge" 456 457 macvlan_destroy 458 simple_if_fini br0 $H2_IPV4/24 $H2_IPV6/64 459 bridge_destroy 460 h1_destroy 461} 462 463vlan_unaware_bridge() 464{ 465 test_bridge 0 466} 467 468vlan_aware_bridge() 469{ 470 test_bridge 1 471} 472 473test_vlan() 474{ 475 local no_unicast_flt=true 476 local skip_ptp=false 477 478 if [ $(has_unicast_flt $h2) = yes ]; then 479 no_unicast_flt=false 480 fi 481 482 h1_vlan_create 483 h2_vlan_create 484 macvlan_create $h2.100 485 486 run_test $h1.100 $h2.100 $skip_ptp $no_unicast_flt "VLAN upper" 487 488 macvlan_destroy 489 h2_vlan_destroy 490 h1_vlan_destroy 491} 492 493vlan_over_bridged_port() 494{ 495 local no_unicast_flt=true 496 local vlan_filtering=$1 497 local skip_ptp=false 498 499 # br_manage_promisc() will not force a single vlan_filtering port to 500 # promiscuous mode, so we should still expect unicast filtering to take 501 # place if the device can do it. 502 if [ $(has_unicast_flt $h2) = yes ] && [ $vlan_filtering = 1 ]; then 503 no_unicast_flt=false 504 fi 505 506 h1_vlan_create 507 h2_vlan_create 508 bridge_create $vlan_filtering 509 macvlan_create $h2.100 510 511 run_test $h1.100 $h2.100 $skip_ptp $no_unicast_flt \ 512 "VLAN over vlan_filtering=$vlan_filtering bridged port" 513 514 macvlan_destroy 515 bridge_destroy 516 h2_vlan_destroy 517 h1_vlan_destroy 518} 519 520vlan_over_vlan_unaware_bridged_port() 521{ 522 vlan_over_bridged_port 0 523} 524 525vlan_over_vlan_aware_bridged_port() 526{ 527 vlan_over_bridged_port 1 528} 529 530vlan_over_bridge() 531{ 532 local no_unicast_flt=true 533 local vlan_filtering=$1 534 local skip_ptp=true 535 536 h1_vlan_create 537 bridge_create $vlan_filtering 538 simple_if_init br0 539 vlan_create br0 100 vbr0 $H2_IPV4/24 $H2_IPV6/64 540 macvlan_create br0.100 541 542 if [ $vlan_filtering = 1 ]; then 543 bridge vlan add dev $h2 vid 100 master 544 bridge vlan add dev br0 vid 100 self 545 fi 546 547 run_test $h1.100 br0.100 $skip_ptp $no_unicast_flt \ 548 "VLAN over vlan_filtering=$vlan_filtering bridge" 549 550 if [ $vlan_filtering = 1 ]; then 551 bridge vlan del dev br0 vid 100 self 552 bridge vlan del dev $h2 vid 100 master 553 fi 554 555 macvlan_destroy 556 vlan_destroy br0 100 557 simple_if_fini br0 558 bridge_destroy 559 h1_vlan_destroy 560} 561 562vlan_over_vlan_unaware_bridge() 563{ 564 vlan_over_bridge 0 565} 566 567vlan_over_vlan_aware_bridge() 568{ 569 vlan_over_bridge 1 570} 571 572cleanup() 573{ 574 pre_cleanup 575 576 ip link set $h2 down 577 ip link set $h1 down 578 579 vrf_cleanup 580} 581 582setup_prepare() 583{ 584 vrf_prepare 585 # setup_wait() needs this 586 ip link set $h1 up 587 ip link set $h2 up 588} 589 590trap cleanup EXIT 591 592setup_prepare 593setup_wait 594 595tests_run 596 597exit $EXIT_STATUS 598