xref: /linux/tools/testing/selftests/net/forwarding/router.sh (revision af50e4ba34f4c45e92535364133d4deb5931c1c5)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4NUM_NETIFS=4
5source lib.sh
6
7h1_create()
8{
9	vrf_create "vrf-h1"
10	ip link set dev $h1 master vrf-h1
11
12	ip link set dev vrf-h1 up
13	ip link set dev $h1 up
14
15	ip address add 192.0.2.2/24 dev $h1
16	ip address add 2001:db8:1::2/64 dev $h1
17
18	ip route add 198.51.100.0/24 vrf vrf-h1 nexthop via 192.0.2.1
19	ip route add 2001:db8:2::/64 vrf vrf-h1 nexthop via 2001:db8:1::1
20}
21
22h1_destroy()
23{
24	ip route del 2001:db8:2::/64 vrf vrf-h1
25	ip route del 198.51.100.0/24 vrf vrf-h1
26
27	ip address del 2001:db8:1::2/64 dev $h1
28	ip address del 192.0.2.2/24 dev $h1
29
30	ip link set dev $h1 down
31	vrf_destroy "vrf-h1"
32}
33
34h2_create()
35{
36	vrf_create "vrf-h2"
37	ip link set dev $h2 master vrf-h2
38
39	ip link set dev vrf-h2 up
40	ip link set dev $h2 up
41
42	ip address add 198.51.100.2/24 dev $h2
43	ip address add 2001:db8:2::2/64 dev $h2
44
45	ip route add 192.0.2.0/24 vrf vrf-h2 nexthop via 198.51.100.1
46	ip route add 2001:db8:1::/64 vrf vrf-h2 nexthop via 2001:db8:2::1
47}
48
49h2_destroy()
50{
51	ip route del 2001:db8:1::/64 vrf vrf-h2
52	ip route del 192.0.2.0/24 vrf vrf-h2
53
54	ip address del 2001:db8:2::2/64 dev $h2
55	ip address del 198.51.100.2/24 dev $h2
56
57	ip link set dev $h2 down
58	vrf_destroy "vrf-h2"
59}
60
61router_create()
62{
63	ip link set dev $rp1 up
64	ip link set dev $rp2 up
65
66	ip address add 192.0.2.1/24 dev $rp1
67	ip address add 2001:db8:1::1/64 dev $rp1
68
69	ip address add 198.51.100.1/24 dev $rp2
70	ip address add 2001:db8:2::1/64 dev $rp2
71}
72
73router_destroy()
74{
75	ip address del 2001:db8:2::1/64 dev $rp2
76	ip address del 198.51.100.1/24 dev $rp2
77
78	ip address del 2001:db8:1::1/64 dev $rp1
79	ip address del 192.0.2.1/24 dev $rp1
80
81	ip link set dev $rp2 down
82	ip link set dev $rp1 down
83}
84
85setup_prepare()
86{
87	h1=${NETIFS[p1]}
88	rp1=${NETIFS[p2]}
89
90	rp2=${NETIFS[p3]}
91	h2=${NETIFS[p4]}
92
93	vrf_prepare
94
95	h1_create
96	h2_create
97
98	router_create
99
100	forwarding_enable
101}
102
103cleanup()
104{
105	pre_cleanup
106
107	forwarding_restore
108
109	router_destroy
110
111	h2_destroy
112	h1_destroy
113
114	vrf_cleanup
115}
116
117trap cleanup EXIT
118
119setup_prepare
120setup_wait
121
122ping_test $h1 198.51.100.2
123ping6_test $h1 2001:db8:2::2
124
125exit $EXIT_STATUS
126