WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / net / forwarding / router.sh
blob057f91b05098dd10c63fc78770258067ea95a7f6
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
4 ALL_TESTS="
5 ping_ipv4
6 ping_ipv6
7 sip_in_class_e
8 mc_mac_mismatch
9 ipv4_sip_equal_dip
10 ipv6_sip_equal_dip
11 ipv4_dip_link_local
14 NUM_NETIFS=4
15 source lib.sh
16 source tc_common.sh
18 require_command $MCD
19 require_command $MC_CLI
20 table_name=selftests
22 h1_create()
24 vrf_create "vrf-h1"
25 ip link set dev $h1 master vrf-h1
27 ip link set dev vrf-h1 up
28 ip link set dev $h1 up
30 ip address add 192.0.2.2/24 dev $h1
31 ip address add 2001:db8:1::2/64 dev $h1
33 ip route add 198.51.100.0/24 vrf vrf-h1 nexthop via 192.0.2.1
34 ip route add 2001:db8:2::/64 vrf vrf-h1 nexthop via 2001:db8:1::1
37 h1_destroy()
39 ip route del 2001:db8:2::/64 vrf vrf-h1
40 ip route del 198.51.100.0/24 vrf vrf-h1
42 ip address del 2001:db8:1::2/64 dev $h1
43 ip address del 192.0.2.2/24 dev $h1
45 ip link set dev $h1 down
46 vrf_destroy "vrf-h1"
49 h2_create()
51 vrf_create "vrf-h2"
52 ip link set dev $h2 master vrf-h2
54 ip link set dev vrf-h2 up
55 ip link set dev $h2 up
57 ip address add 198.51.100.2/24 dev $h2
58 ip address add 2001:db8:2::2/64 dev $h2
60 ip route add 192.0.2.0/24 vrf vrf-h2 nexthop via 198.51.100.1
61 ip route add 2001:db8:1::/64 vrf vrf-h2 nexthop via 2001:db8:2::1
64 h2_destroy()
66 ip route del 2001:db8:1::/64 vrf vrf-h2
67 ip route del 192.0.2.0/24 vrf vrf-h2
69 ip address del 2001:db8:2::2/64 dev $h2
70 ip address del 198.51.100.2/24 dev $h2
72 ip link set dev $h2 down
73 vrf_destroy "vrf-h2"
76 router_create()
78 ip link set dev $rp1 up
79 ip link set dev $rp2 up
81 tc qdisc add dev $rp2 clsact
83 ip address add 192.0.2.1/24 dev $rp1
84 ip address add 2001:db8:1::1/64 dev $rp1
86 ip address add 198.51.100.1/24 dev $rp2
87 ip address add 2001:db8:2::1/64 dev $rp2
90 router_destroy()
92 ip address del 2001:db8:2::1/64 dev $rp2
93 ip address del 198.51.100.1/24 dev $rp2
95 ip address del 2001:db8:1::1/64 dev $rp1
96 ip address del 192.0.2.1/24 dev $rp1
98 tc qdisc del dev $rp2 clsact
100 ip link set dev $rp2 down
101 ip link set dev $rp1 down
104 start_mcd()
106 SMCROUTEDIR="$(mktemp -d)"
108 for ((i = 1; i <= $NUM_NETIFS; ++i)); do
109 echo "phyint ${NETIFS[p$i]} enable" >> \
110 $SMCROUTEDIR/$table_name.conf
111 done
113 $MCD -N -I $table_name -f $SMCROUTEDIR/$table_name.conf \
114 -P $SMCROUTEDIR/$table_name.pid
117 kill_mcd()
119 pkill $MCD
120 rm -rf $SMCROUTEDIR
123 setup_prepare()
125 h1=${NETIFS[p1]}
126 rp1=${NETIFS[p2]}
128 rp2=${NETIFS[p3]}
129 h2=${NETIFS[p4]}
131 rp1mac=$(mac_get $rp1)
133 start_mcd
135 vrf_prepare
137 h1_create
138 h2_create
140 router_create
142 forwarding_enable
145 cleanup()
147 pre_cleanup
149 forwarding_restore
151 router_destroy
153 h2_destroy
154 h1_destroy
156 vrf_cleanup
158 kill_mcd
161 ping_ipv4()
163 ping_test $h1 198.51.100.2
166 ping_ipv6()
168 ping6_test $h1 2001:db8:2::2
171 sip_in_class_e()
173 RET=0
175 # Disable rpfilter to prevent packets to be dropped because of it.
176 sysctl_set net.ipv4.conf.all.rp_filter 0
177 sysctl_set net.ipv4.conf.$rp1.rp_filter 0
179 tc filter add dev $rp2 egress protocol ip pref 1 handle 101 \
180 flower src_ip 240.0.0.1 ip_proto udp action pass
182 $MZ $h1 -t udp "sp=54321,dp=12345" -c 5 -d 1msec \
183 -A 240.0.0.1 -b $rp1mac -B 198.51.100.2 -q
185 tc_check_packets "dev $rp2 egress" 101 5
186 check_err $? "Packets were dropped"
188 log_test "Source IP in class E"
190 tc filter del dev $rp2 egress protocol ip pref 1 handle 101 flower
191 sysctl_restore net.ipv4.conf.$rp1.rp_filter
192 sysctl_restore net.ipv4.conf.all.rp_filter
195 create_mcast_sg()
197 local if_name=$1; shift
198 local s_addr=$1; shift
199 local mcast=$1; shift
200 local dest_ifs=${@}
202 $MC_CLI -I $table_name add $if_name $s_addr $mcast $dest_ifs
205 delete_mcast_sg()
207 local if_name=$1; shift
208 local s_addr=$1; shift
209 local mcast=$1; shift
210 local dest_ifs=${@}
212 $MC_CLI -I $table_name remove $if_name $s_addr $mcast $dest_ifs
215 __mc_mac_mismatch()
217 local desc=$1; shift
218 local proto=$1; shift
219 local sip=$1; shift
220 local dip=$1; shift
221 local flags=${1:-""}; shift
222 local dmac=01:02:03:04:05:06
224 RET=0
226 tc filter add dev $rp2 egress protocol $proto pref 1 handle 101 \
227 flower dst_ip $dip action pass
229 create_mcast_sg $rp1 $sip $dip $rp2
231 $MZ $flags $h1 -t udp "sp=54321,dp=12345" -c 5 -d 1msec -b $dmac \
232 -B $dip -q
234 tc_check_packets "dev $rp2 egress" 101 5
235 check_err $? "Packets were dropped"
237 log_test "Multicast MAC mismatch: $desc"
239 delete_mcast_sg $rp1 $sip $dip $rp2
240 tc filter del dev $rp2 egress protocol $proto pref 1 handle 101 flower
243 mc_mac_mismatch()
245 __mc_mac_mismatch "IPv4" "ip" 192.0.2.2 225.1.2.3
246 __mc_mac_mismatch "IPv6" "ipv6" 2001:db8:1::2 ff0e::3 "-6"
249 ipv4_sip_equal_dip()
251 RET=0
253 # Disable rpfilter to prevent packets to be dropped because of it.
254 sysctl_set net.ipv4.conf.all.rp_filter 0
255 sysctl_set net.ipv4.conf.$rp1.rp_filter 0
257 tc filter add dev $rp2 egress protocol ip pref 1 handle 101 \
258 flower src_ip 198.51.100.2 action pass
260 $MZ $h1 -t udp "sp=54321,dp=12345" -c 5 -d 1msec \
261 -A 198.51.100.2 -b $rp1mac -B 198.51.100.2 -q
263 tc_check_packets "dev $rp2 egress" 101 5
264 check_err $? "Packets were dropped"
266 log_test "Source IP is equal to destination IP: IPv4"
268 tc filter del dev $rp2 egress protocol ip pref 1 handle 101 flower
269 sysctl_restore net.ipv4.conf.$rp1.rp_filter
270 sysctl_restore net.ipv4.conf.all.rp_filter
273 ipv6_sip_equal_dip()
275 RET=0
277 tc filter add dev $rp2 egress protocol ipv6 pref 1 handle 101 \
278 flower src_ip 2001:db8:2::2 action pass
280 $MZ -6 $h1 -t udp "sp=54321,dp=12345" -c 5 -d 1msec \
281 -A 2001:db8:2::2 -b $rp1mac -B 2001:db8:2::2 -q
283 tc_check_packets "dev $rp2 egress" 101 5
284 check_err $? "Packets were dropped"
286 log_test "Source IP is equal to destination IP: IPv6"
288 tc filter del dev $rp2 egress protocol ipv6 pref 1 handle 101 flower
291 ipv4_dip_link_local()
293 local dip=169.254.1.1
295 RET=0
297 tc filter add dev $rp2 egress protocol ip pref 1 handle 101 \
298 flower dst_ip $dip action pass
300 ip neigh add 169.254.1.1 lladdr 00:11:22:33:44:55 dev $rp2
301 ip route add 169.254.1.0/24 dev $rp2
303 $MZ $h1 -t udp "sp=54321,dp=12345" -c 5 -d 1msec -b $rp1mac -B $dip -q
305 tc_check_packets "dev $rp2 egress" 101 5
306 check_err $? "Packets were dropped"
308 log_test "IPv4 destination IP is link-local"
310 ip route del 169.254.1.0/24 dev $rp2
311 ip neigh del 169.254.1.1 lladdr 00:11:22:33:44:55 dev $rp2
312 tc filter del dev $rp2 egress protocol ip pref 1 handle 101 flower
315 trap cleanup EXIT
317 setup_prepare
318 setup_wait
320 tests_run
322 exit $EXIT_STATUS