1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4ALL_TESTS="
5	ping_ipv4
6	ping_ipv6
7"
8
9NUM_NETIFS=4
10source lib.sh
11source tc_common.sh
12
13h1_create()
14{
15	vrf_create "vrf-h1"
16	ip link set dev $h1 master vrf-h1
17
18	ip link set dev vrf-h1 up
19	ip link set dev $h1 up
20
21	ip address add 192.0.2.2/24 dev $h1
22	ip address add 2001:db8:1::2/64 dev $h1
23
24	ip route add 198.51.100.0/24 vrf vrf-h1 nexthop via 192.0.2.1
25	ip route add 2001:db8:2::/64 vrf vrf-h1 nexthop via 2001:db8:1::1
26}
27
28h1_destroy()
29{
30	ip route del 2001:db8:2::/64 vrf vrf-h1
31	ip route del 198.51.100.0/24 vrf vrf-h1
32
33	ip address del 2001:db8:1::2/64 dev $h1
34	ip address del 192.0.2.2/24 dev $h1
35
36	ip link set dev $h1 down
37	vrf_destroy "vrf-h1"
38}
39
40h2_create()
41{
42	vrf_create "vrf-h2"
43	ip link set dev $h2 master vrf-h2
44
45	ip link set dev vrf-h2 up
46	ip link set dev $h2 up
47
48	ip address add 198.51.100.2/24 dev $h2
49	ip address add 2001:db8:2::2/64 dev $h2
50
51	ip route add 192.0.2.0/24 vrf vrf-h2 nexthop via 198.51.100.1
52	ip route add 2001:db8:1::/64 vrf vrf-h2 nexthop via 2001:db8:2::1
53}
54
55h2_destroy()
56{
57	ip route del 2001:db8:1::/64 vrf vrf-h2
58	ip route del 192.0.2.0/24 vrf vrf-h2
59
60	ip address del 2001:db8:2::2/64 dev $h2
61	ip address del 198.51.100.2/24 dev $h2
62
63	ip link set dev $h2 down
64	vrf_destroy "vrf-h2"
65}
66
67router_create()
68{
69	ip link set dev $rp1 up
70	ip link set dev $rp2 up
71
72	tc qdisc add dev $rp2 clsact
73
74	ip address add 192.0.2.1/24 dev $rp1
75	ip address add 2001:db8:1::1/64 dev $rp1
76
77	ip address add 198.51.100.1/24 dev $rp2
78	ip address add 2001:db8:2::1/64 dev $rp2
79}
80
81router_destroy()
82{
83	ip address del 2001:db8:2::1/64 dev $rp2
84	ip address del 198.51.100.1/24 dev $rp2
85
86	ip address del 2001:db8:1::1/64 dev $rp1
87	ip address del 192.0.2.1/24 dev $rp1
88
89	tc qdisc del dev $rp2 clsact
90
91	ip link set dev $rp2 down
92	ip link set dev $rp1 down
93}
94
95routing_nh_obj()
96{
97	# Create the nexthops as AF_INET6, so that IPv4 and IPv6 routes could
98	# use them.
99	ip -6 nexthop add id 101 dev $rp1
100	ip -6 nexthop add id 102 dev $rp2
101
102	ip route replace 192.0.2.0/24 nhid 101
103	ip route replace 2001:db8:1::/64 nhid 101
104	ip route replace 198.51.100.0/24 nhid 102
105	ip route replace 2001:db8:2::/64 nhid 102
106}
107
108setup_prepare()
109{
110	h1=${NETIFS[p1]}
111	rp1=${NETIFS[p2]}
112
113	rp2=${NETIFS[p3]}
114	h2=${NETIFS[p4]}
115
116	rp1mac=$(mac_get $rp1)
117
118	vrf_prepare
119
120	h1_create
121	h2_create
122
123	router_create
124
125	forwarding_enable
126}
127
128cleanup()
129{
130	pre_cleanup
131
132	forwarding_restore
133
134	router_destroy
135
136	h2_destroy
137	h1_destroy
138
139	vrf_cleanup
140}
141
142ping_ipv4()
143{
144	ping_test $h1 198.51.100.2
145}
146
147ping_ipv6()
148{
149	ping6_test $h1 2001:db8:2::2
150}
151
152trap cleanup EXIT
153
154setup_prepare
155setup_wait
156routing_nh_obj
157
158tests_run
159
160exit $EXIT_STATUS
161