brintos

brintos / linux-shallow public Read only

0
0
Text · 5.0 KiB · 4eac0a0 Raw
238 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03 4ALL_TESTS="ping_ipv4"5NUM_NETIFS=66source lib.sh7 8h1_create()9{10	vrf_create "vrf-h1"11	ip link set dev $h1 master vrf-h112 13	ip link set dev vrf-h1 up14	ip link set dev $h1 up15 16	ip address add 192.0.2.2/24 dev $h117 18	ip route add 198.51.100.0/24 vrf vrf-h1 nexthop via 192.0.2.119	ip route add 198.51.200.0/24 vrf vrf-h1 nexthop via 192.0.2.120}21 22h1_destroy()23{24	ip route del 198.51.200.0/24 vrf vrf-h125	ip route del 198.51.100.0/24 vrf vrf-h126 27	ip address del 192.0.2.2/24 dev $h128 29	ip link set dev $h1 down30	vrf_destroy "vrf-h1"31}32 33h2_create()34{35	vrf_create "vrf-h2"36	ip link set dev $h2 master vrf-h237 38	ip link set dev vrf-h2 up39	ip link set dev $h2 up40 41	ip address add 198.51.100.2/24 dev $h242 43	ip route add 192.0.2.0/24 vrf vrf-h2 nexthop via 198.51.100.144	ip route add 198.51.200.0/24 vrf vrf-h2 nexthop via 198.51.100.145}46 47h2_destroy()48{49	ip route del 198.51.200.0/24 vrf vrf-h250	ip route del 192.0.2.0/24 vrf vrf-h251 52	ip address del 198.51.100.2/24 dev $h253 54	ip link set dev $h2 down55	vrf_destroy "vrf-h2"56}57 58h3_create()59{60	vrf_create "vrf-h3"61	ip link set dev $h3 master vrf-h362 63	ip link set dev vrf-h3 up64	ip link set dev $h3 up65 66	ip address add 198.51.200.2/24 dev $h367 68	ip route add 192.0.2.0/24 vrf vrf-h3 nexthop via 198.51.200.169	ip route add 198.51.100.0/24 vrf vrf-h3 nexthop via 198.51.200.170}71 72h3_destroy()73{74	ip route del 198.51.100.0/24 vrf vrf-h375	ip route del 192.0.2.0/24 vrf vrf-h376 77	ip address del 198.51.200.2/24 dev $h378 79	ip link set dev $h3 down80	vrf_destroy "vrf-h3"81}82 83router_create()84{85	ip link set dev $rp1 up86	ip link set dev $rp2 up87	ip link set dev $rp3 up88 89	ip address add 192.0.2.1/24 dev $rp190 91	ip address add 198.51.100.1/24 dev $rp292	ip address add 198.51.200.1/24 dev $rp393}94 95router_destroy()96{97	ip address del 198.51.200.1/24 dev $rp398	ip address del 198.51.100.1/24 dev $rp299 100	ip address del 192.0.2.1/24 dev $rp1101 102	ip link set dev $rp3 down103	ip link set dev $rp2 down104	ip link set dev $rp1 down105}106 107setup_prepare()108{109	h1=${NETIFS[p1]}110	rp1=${NETIFS[p2]}111 112	rp2=${NETIFS[p3]}113	h2=${NETIFS[p4]}114 115	rp3=${NETIFS[p5]}116	h3=${NETIFS[p6]}117 118	vrf_prepare119 120	h1_create121	h2_create122	h3_create123 124	router_create125 126	forwarding_enable127}128 129cleanup()130{131	pre_cleanup132 133	forwarding_restore134 135	router_destroy136 137	h3_destroy138	h2_destroy139	h1_destroy140 141	vrf_cleanup142}143 144bc_forwarding_disable()145{146	sysctl_set net.ipv4.conf.all.bc_forwarding 0147	sysctl_set net.ipv4.conf.$rp1.bc_forwarding 0148	sysctl_set net.ipv4.conf.$rp2.bc_forwarding 0149}150 151bc_forwarding_enable()152{153	sysctl_set net.ipv4.conf.all.bc_forwarding 1154	sysctl_set net.ipv4.conf.$rp1.bc_forwarding 1155	sysctl_set net.ipv4.conf.$rp2.bc_forwarding 1156}157 158bc_forwarding_restore()159{160	sysctl_restore net.ipv4.conf.$rp2.bc_forwarding161	sysctl_restore net.ipv4.conf.$rp1.bc_forwarding162	sysctl_restore net.ipv4.conf.all.bc_forwarding163}164 165ping_test_from()166{167	local oif=$1168	local dip=$2169	local from=$3170	local fail=${4:-0}171 172	RET=0173 174	log_info "ping $dip, expected reply from $from"175	ip vrf exec $(master_name_get $oif) \176		$PING -I $oif $dip -c 10 -i 0.1 -w $PING_TIMEOUT -b 2>&1 \177		| grep "bytes from $from" > /dev/null178	check_err_fail $fail $?179}180 181ping_ipv4()182{183	sysctl_set net.ipv4.icmp_echo_ignore_broadcasts 0184 185	bc_forwarding_disable186	log_info "bc_forwarding disabled on r1 =>"187	ping_test_from $h1 198.51.100.255 192.0.2.1188	log_test "h1 -> net2: reply from r1 (not forwarding)"189	ping_test_from $h1 198.51.200.255 192.0.2.1190	log_test "h1 -> net3: reply from r1 (not forwarding)"191	ping_test_from $h1 192.0.2.255 192.0.2.1192	log_test "h1 -> net1: reply from r1 (not dropping)"193	ping_test_from $h1 255.255.255.255 192.0.2.1194	log_test "h1 -> 255.255.255.255: reply from r1 (not forwarding)"195 196	ping_test_from $h2 192.0.2.255 198.51.100.1197	log_test "h2 -> net1: reply from r1 (not forwarding)"198	ping_test_from $h2 198.51.200.255 198.51.100.1199	log_test "h2 -> net3: reply from r1 (not forwarding)"200	ping_test_from $h2 198.51.100.255 198.51.100.1201	log_test "h2 -> net2: reply from r1 (not dropping)"202	ping_test_from $h2 255.255.255.255 198.51.100.1203	log_test "h2 -> 255.255.255.255: reply from r1 (not forwarding)"204	bc_forwarding_restore205 206	bc_forwarding_enable207	log_info "bc_forwarding enabled on r1 =>"208	ping_test_from $h1 198.51.100.255 198.51.100.2209	log_test "h1 -> net2: reply from h2 (forwarding)"210	ping_test_from $h1 198.51.200.255 198.51.200.2211	log_test "h1 -> net3: reply from h3 (forwarding)"212	ping_test_from $h1 192.0.2.255 192.0.2.1 1213	log_test "h1 -> net1: no reply (dropping)"214	ping_test_from $h1 255.255.255.255 192.0.2.1215	log_test "h1 -> 255.255.255.255: reply from r1 (not forwarding)"216 217	ping_test_from $h2 192.0.2.255 192.0.2.2218	log_test "h2 -> net1: reply from h1 (forwarding)"219	ping_test_from $h2 198.51.200.255 198.51.200.2220	log_test "h2 -> net3: reply from h3 (forwarding)"221	ping_test_from $h2 198.51.100.255 198.51.100.1 1222	log_test "h2 -> net2: no reply (dropping)"223	ping_test_from $h2 255.255.255.255 198.51.100.1224	log_test "h2 -> 255.255.255.255: reply from r1 (not forwarding)"225	bc_forwarding_restore226 227	sysctl_restore net.ipv4.icmp_echo_ignore_broadcasts228}229 230trap cleanup EXIT231 232setup_prepare233setup_wait234 235tests_run236 237exit $EXIT_STATUS238