xref: /linux/tools/testing/selftests/net/forwarding/router_broadcast.sh (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
140f98b9aSXin Long#!/bin/bash
240f98b9aSXin Long# SPDX-License-Identifier: GPL-2.0
340f98b9aSXin Long
440f98b9aSXin LongALL_TESTS="ping_ipv4"
540f98b9aSXin LongNUM_NETIFS=6
640f98b9aSXin Longsource lib.sh
740f98b9aSXin Long
840f98b9aSXin Longh1_create()
940f98b9aSXin Long{
1040f98b9aSXin Long	vrf_create "vrf-h1"
1140f98b9aSXin Long	ip link set dev $h1 master vrf-h1
1240f98b9aSXin Long
1340f98b9aSXin Long	ip link set dev vrf-h1 up
1440f98b9aSXin Long	ip link set dev $h1 up
1540f98b9aSXin Long
1640f98b9aSXin Long	ip address add 192.0.2.2/24 dev $h1
1740f98b9aSXin Long
1840f98b9aSXin Long	ip route add 198.51.100.0/24 vrf vrf-h1 nexthop via 192.0.2.1
1940f98b9aSXin Long	ip route add 198.51.200.0/24 vrf vrf-h1 nexthop via 192.0.2.1
2040f98b9aSXin Long}
2140f98b9aSXin Long
2240f98b9aSXin Longh1_destroy()
2340f98b9aSXin Long{
2440f98b9aSXin Long	ip route del 198.51.200.0/24 vrf vrf-h1
2540f98b9aSXin Long	ip route del 198.51.100.0/24 vrf vrf-h1
2640f98b9aSXin Long
2740f98b9aSXin Long	ip address del 192.0.2.2/24 dev $h1
2840f98b9aSXin Long
2940f98b9aSXin Long	ip link set dev $h1 down
3040f98b9aSXin Long	vrf_destroy "vrf-h1"
3140f98b9aSXin Long}
3240f98b9aSXin Long
3340f98b9aSXin Longh2_create()
3440f98b9aSXin Long{
3540f98b9aSXin Long	vrf_create "vrf-h2"
3640f98b9aSXin Long	ip link set dev $h2 master vrf-h2
3740f98b9aSXin Long
3840f98b9aSXin Long	ip link set dev vrf-h2 up
3940f98b9aSXin Long	ip link set dev $h2 up
4040f98b9aSXin Long
4140f98b9aSXin Long	ip address add 198.51.100.2/24 dev $h2
4240f98b9aSXin Long
4340f98b9aSXin Long	ip route add 192.0.2.0/24 vrf vrf-h2 nexthop via 198.51.100.1
4440f98b9aSXin Long	ip route add 198.51.200.0/24 vrf vrf-h2 nexthop via 198.51.100.1
4540f98b9aSXin Long}
4640f98b9aSXin Long
4740f98b9aSXin Longh2_destroy()
4840f98b9aSXin Long{
4940f98b9aSXin Long	ip route del 198.51.200.0/24 vrf vrf-h2
5040f98b9aSXin Long	ip route del 192.0.2.0/24 vrf vrf-h2
5140f98b9aSXin Long
5240f98b9aSXin Long	ip address del 198.51.100.2/24 dev $h2
5340f98b9aSXin Long
5440f98b9aSXin Long	ip link set dev $h2 down
5540f98b9aSXin Long	vrf_destroy "vrf-h2"
5640f98b9aSXin Long}
5740f98b9aSXin Long
5840f98b9aSXin Longh3_create()
5940f98b9aSXin Long{
6040f98b9aSXin Long	vrf_create "vrf-h3"
6140f98b9aSXin Long	ip link set dev $h3 master vrf-h3
6240f98b9aSXin Long
6340f98b9aSXin Long	ip link set dev vrf-h3 up
6440f98b9aSXin Long	ip link set dev $h3 up
6540f98b9aSXin Long
6640f98b9aSXin Long	ip address add 198.51.200.2/24 dev $h3
6740f98b9aSXin Long
6840f98b9aSXin Long	ip route add 192.0.2.0/24 vrf vrf-h3 nexthop via 198.51.200.1
6940f98b9aSXin Long	ip route add 198.51.100.0/24 vrf vrf-h3 nexthop via 198.51.200.1
7040f98b9aSXin Long}
7140f98b9aSXin Long
7240f98b9aSXin Longh3_destroy()
7340f98b9aSXin Long{
7440f98b9aSXin Long	ip route del 198.51.100.0/24 vrf vrf-h3
7540f98b9aSXin Long	ip route del 192.0.2.0/24 vrf vrf-h3
7640f98b9aSXin Long
7740f98b9aSXin Long	ip address del 198.51.200.2/24 dev $h3
7840f98b9aSXin Long
7940f98b9aSXin Long	ip link set dev $h3 down
8040f98b9aSXin Long	vrf_destroy "vrf-h3"
8140f98b9aSXin Long}
8240f98b9aSXin Long
8340f98b9aSXin Longrouter_create()
8440f98b9aSXin Long{
8540f98b9aSXin Long	ip link set dev $rp1 up
8640f98b9aSXin Long	ip link set dev $rp2 up
8740f98b9aSXin Long	ip link set dev $rp3 up
8840f98b9aSXin Long
8940f98b9aSXin Long	ip address add 192.0.2.1/24 dev $rp1
9040f98b9aSXin Long
9140f98b9aSXin Long	ip address add 198.51.100.1/24 dev $rp2
9240f98b9aSXin Long	ip address add 198.51.200.1/24 dev $rp3
9340f98b9aSXin Long}
9440f98b9aSXin Long
9540f98b9aSXin Longrouter_destroy()
9640f98b9aSXin Long{
9740f98b9aSXin Long	ip address del 198.51.200.1/24 dev $rp3
9840f98b9aSXin Long	ip address del 198.51.100.1/24 dev $rp2
9940f98b9aSXin Long
10040f98b9aSXin Long	ip address del 192.0.2.1/24 dev $rp1
10140f98b9aSXin Long
10240f98b9aSXin Long	ip link set dev $rp3 down
10340f98b9aSXin Long	ip link set dev $rp2 down
10440f98b9aSXin Long	ip link set dev $rp1 down
10540f98b9aSXin Long}
10640f98b9aSXin Long
10740f98b9aSXin Longsetup_prepare()
10840f98b9aSXin Long{
10940f98b9aSXin Long	h1=${NETIFS[p1]}
11040f98b9aSXin Long	rp1=${NETIFS[p2]}
11140f98b9aSXin Long
11240f98b9aSXin Long	rp2=${NETIFS[p3]}
11340f98b9aSXin Long	h2=${NETIFS[p4]}
11440f98b9aSXin Long
11540f98b9aSXin Long	rp3=${NETIFS[p5]}
11640f98b9aSXin Long	h3=${NETIFS[p6]}
11740f98b9aSXin Long
11840f98b9aSXin Long	vrf_prepare
11940f98b9aSXin Long
12040f98b9aSXin Long	h1_create
12140f98b9aSXin Long	h2_create
12240f98b9aSXin Long	h3_create
12340f98b9aSXin Long
12440f98b9aSXin Long	router_create
12540f98b9aSXin Long
12640f98b9aSXin Long	forwarding_enable
12740f98b9aSXin Long}
12840f98b9aSXin Long
12940f98b9aSXin Longcleanup()
13040f98b9aSXin Long{
13140f98b9aSXin Long	pre_cleanup
13240f98b9aSXin Long
13340f98b9aSXin Long	forwarding_restore
13440f98b9aSXin Long
13540f98b9aSXin Long	router_destroy
13640f98b9aSXin Long
13740f98b9aSXin Long	h3_destroy
13840f98b9aSXin Long	h2_destroy
13940f98b9aSXin Long	h1_destroy
14040f98b9aSXin Long
14140f98b9aSXin Long	vrf_cleanup
14240f98b9aSXin Long}
14340f98b9aSXin Long
14440f98b9aSXin Longbc_forwarding_disable()
14540f98b9aSXin Long{
14640f98b9aSXin Long	sysctl_set net.ipv4.conf.all.bc_forwarding 0
14740f98b9aSXin Long	sysctl_set net.ipv4.conf.$rp1.bc_forwarding 0
148*67c0aaa1SXin Long	sysctl_set net.ipv4.conf.$rp2.bc_forwarding 0
14940f98b9aSXin Long}
15040f98b9aSXin Long
15140f98b9aSXin Longbc_forwarding_enable()
15240f98b9aSXin Long{
15340f98b9aSXin Long	sysctl_set net.ipv4.conf.all.bc_forwarding 1
15440f98b9aSXin Long	sysctl_set net.ipv4.conf.$rp1.bc_forwarding 1
155*67c0aaa1SXin Long	sysctl_set net.ipv4.conf.$rp2.bc_forwarding 1
15640f98b9aSXin Long}
15740f98b9aSXin Long
15840f98b9aSXin Longbc_forwarding_restore()
15940f98b9aSXin Long{
160*67c0aaa1SXin Long	sysctl_restore net.ipv4.conf.$rp2.bc_forwarding
16140f98b9aSXin Long	sysctl_restore net.ipv4.conf.$rp1.bc_forwarding
16240f98b9aSXin Long	sysctl_restore net.ipv4.conf.all.bc_forwarding
16340f98b9aSXin Long}
16440f98b9aSXin Long
16540f98b9aSXin Longping_test_from()
16640f98b9aSXin Long{
16740f98b9aSXin Long	local oif=$1
16840f98b9aSXin Long	local dip=$2
16940f98b9aSXin Long	local from=$3
17040f98b9aSXin Long	local fail=${4:-0}
17140f98b9aSXin Long
17240f98b9aSXin Long	RET=0
17340f98b9aSXin Long
17440f98b9aSXin Long	log_info "ping $dip, expected reply from $from"
17540f98b9aSXin Long	ip vrf exec $(master_name_get $oif) \
176b6a4fd68SIdo Schimmel		$PING -I $oif $dip -c 10 -i 0.1 -w $PING_TIMEOUT -b 2>&1 \
177*67c0aaa1SXin Long		| grep "bytes from $from" > /dev/null
17840f98b9aSXin Long	check_err_fail $fail $?
17940f98b9aSXin Long}
18040f98b9aSXin Long
18140f98b9aSXin Longping_ipv4()
18240f98b9aSXin Long{
18340f98b9aSXin Long	sysctl_set net.ipv4.icmp_echo_ignore_broadcasts 0
18440f98b9aSXin Long
18540f98b9aSXin Long	bc_forwarding_disable
18640f98b9aSXin Long	log_info "bc_forwarding disabled on r1 =>"
18740f98b9aSXin Long	ping_test_from $h1 198.51.100.255 192.0.2.1
18840f98b9aSXin Long	log_test "h1 -> net2: reply from r1 (not forwarding)"
18940f98b9aSXin Long	ping_test_from $h1 198.51.200.255 192.0.2.1
19040f98b9aSXin Long	log_test "h1 -> net3: reply from r1 (not forwarding)"
19140f98b9aSXin Long	ping_test_from $h1 192.0.2.255 192.0.2.1
19240f98b9aSXin Long	log_test "h1 -> net1: reply from r1 (not dropping)"
19340f98b9aSXin Long	ping_test_from $h1 255.255.255.255 192.0.2.1
19440f98b9aSXin Long	log_test "h1 -> 255.255.255.255: reply from r1 (not forwarding)"
19540f98b9aSXin Long
19640f98b9aSXin Long	ping_test_from $h2 192.0.2.255 198.51.100.1
19740f98b9aSXin Long	log_test "h2 -> net1: reply from r1 (not forwarding)"
19840f98b9aSXin Long	ping_test_from $h2 198.51.200.255 198.51.100.1
19940f98b9aSXin Long	log_test "h2 -> net3: reply from r1 (not forwarding)"
20040f98b9aSXin Long	ping_test_from $h2 198.51.100.255 198.51.100.1
20140f98b9aSXin Long	log_test "h2 -> net2: reply from r1 (not dropping)"
20240f98b9aSXin Long	ping_test_from $h2 255.255.255.255 198.51.100.1
20340f98b9aSXin Long	log_test "h2 -> 255.255.255.255: reply from r1 (not forwarding)"
20440f98b9aSXin Long	bc_forwarding_restore
20540f98b9aSXin Long
20640f98b9aSXin Long	bc_forwarding_enable
20740f98b9aSXin Long	log_info "bc_forwarding enabled on r1 =>"
20840f98b9aSXin Long	ping_test_from $h1 198.51.100.255 198.51.100.2
20940f98b9aSXin Long	log_test "h1 -> net2: reply from h2 (forwarding)"
21040f98b9aSXin Long	ping_test_from $h1 198.51.200.255 198.51.200.2
21140f98b9aSXin Long	log_test "h1 -> net3: reply from h3 (forwarding)"
21240f98b9aSXin Long	ping_test_from $h1 192.0.2.255 192.0.2.1 1
21340f98b9aSXin Long	log_test "h1 -> net1: no reply (dropping)"
21440f98b9aSXin Long	ping_test_from $h1 255.255.255.255 192.0.2.1
21540f98b9aSXin Long	log_test "h1 -> 255.255.255.255: reply from r1 (not forwarding)"
21640f98b9aSXin Long
21740f98b9aSXin Long	ping_test_from $h2 192.0.2.255 192.0.2.2
21840f98b9aSXin Long	log_test "h2 -> net1: reply from h1 (forwarding)"
21940f98b9aSXin Long	ping_test_from $h2 198.51.200.255 198.51.200.2
22040f98b9aSXin Long	log_test "h2 -> net3: reply from h3 (forwarding)"
22140f98b9aSXin Long	ping_test_from $h2 198.51.100.255 198.51.100.1 1
22240f98b9aSXin Long	log_test "h2 -> net2: no reply (dropping)"
22340f98b9aSXin Long	ping_test_from $h2 255.255.255.255 198.51.100.1
22440f98b9aSXin Long	log_test "h2 -> 255.255.255.255: reply from r1 (not forwarding)"
22540f98b9aSXin Long	bc_forwarding_restore
22640f98b9aSXin Long
22740f98b9aSXin Long	sysctl_restore net.ipv4.icmp_echo_ignore_broadcasts
22840f98b9aSXin Long}
22940f98b9aSXin Long
23040f98b9aSXin Longtrap cleanup EXIT
23140f98b9aSXin Long
23240f98b9aSXin Longsetup_prepare
23340f98b9aSXin Longsetup_wait
23440f98b9aSXin Long
23540f98b9aSXin Longtests_run
23640f98b9aSXin Long
23740f98b9aSXin Longexit $EXIT_STATUS
238