1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# +------------------------------------------------+   +----------------------+
5# | H1 (vrf)                                       |   |             H2 (vrf) |
6# |    + $h1.555           + $h1.777               |   |  + $h2               |
7# |    | 192.0.2.1/28      | 192.0.2.17/28         |   |  | 192.0.2.130/28    |
8# |    | 2001:db8:1::1/64  | 2001:db8:3::1/64      |   |  | 192.0.2.146/28    |
9# |    | .-----------------'                       |   |  | 2001:db8:2::2/64  |
10# |    |/                                          |   |  | 2001:db8:4::2/64  |
11# |    + $h1                                       |   |  |                   |
12# +----|-------------------------------------------+   +--|-------------------+
13#      |                                                  |
14# +----|--------------------------------------------------|-------------------+
15# | SW |                                                  |                   |
16# | +--|-------------------------------+                  + $swp2             |
17# | |  + $swp1                         |                    192.0.2.129/28    |
18# | |    vid 555 777                   |                    192.0.2.145/28    |
19# | |                                  |                    2001:db8:2::1/64  |
20# | |  + BR1 (802.1q)                  |                    2001:db8:4::1/64  |
21# | |    vid 555 pvid untagged         |                                      |
22# | |    192.0.2.2/28                  |                                      |
23# | |    192.0.2.18/28                 |                                      |
24# | |    2001:db8:1::2/64              |                                      |
25# | |    2001:db8:3::2/64              |                                      |
26# | +----------------------------------+                                      |
27# +---------------------------------------------------------------------------+
28
29ALL_TESTS="
30	ping_ipv4
31	ping_ipv6
32	vlan
33	config_777
34	ping_ipv4_fails
35	ping_ipv6_fails
36	ping_ipv4_777
37	ping_ipv6_777
38	config_555
39	ping_ipv4
40	ping_ipv6
41"
42NUM_NETIFS=4
43source lib.sh
44
45h1_create()
46{
47	simple_if_init $h1
48
49	vlan_create $h1 555 v$h1 192.0.2.1/28 2001:db8:1::1/64
50	ip -4 route add 192.0.2.128/28 vrf v$h1 nexthop via 192.0.2.2
51	ip -6 route add 2001:db8:2::/64 vrf v$h1 nexthop via 2001:db8:1::2
52
53	vlan_create $h1 777 v$h1 192.0.2.17/28 2001:db8:3::1/64
54	ip -4 route add 192.0.2.144/28 vrf v$h1 nexthop via 192.0.2.18
55	ip -6 route add 2001:db8:4::/64 vrf v$h1 nexthop via 2001:db8:3::2
56}
57
58h1_destroy()
59{
60	ip -6 route del 2001:db8:4::/64 vrf v$h1
61	ip -4 route del 192.0.2.144/28 vrf v$h1
62	vlan_destroy $h1 777
63
64	ip -6 route del 2001:db8:2::/64 vrf v$h1
65	ip -4 route del 192.0.2.128/28 vrf v$h1
66	vlan_destroy $h1 555
67
68	simple_if_fini $h1
69}
70
71h2_create()
72{
73	simple_if_init $h2 192.0.2.130/28 2001:db8:2::2/64 \
74			   192.0.2.146/28 2001:db8:4::2/64
75	ip -4 route add 192.0.2.0/28 vrf v$h2 nexthop via 192.0.2.129
76	ip -4 route add 192.0.2.16/28 vrf v$h2 nexthop via 192.0.2.145
77	ip -6 route add 2001:db8:1::/64 vrf v$h2 nexthop via 2001:db8:2::1
78	ip -6 route add 2001:db8:3::/64 vrf v$h2 nexthop via 2001:db8:4::1
79}
80
81h2_destroy()
82{
83	ip -6 route del 2001:db8:3::/64 vrf v$h2
84	ip -6 route del 2001:db8:1::/64 vrf v$h2
85	ip -4 route del 192.0.2.16/28 vrf v$h2
86	ip -4 route del 192.0.2.0/28 vrf v$h2
87	simple_if_fini $h2 192.0.2.146/28 2001:db8:4::2/64 \
88			   192.0.2.130/28 2001:db8:2::2/64
89}
90
91router_create()
92{
93	ip link add name br1 type bridge vlan_filtering 1 vlan_default_pvid 0
94	ip link set dev br1 up
95
96	ip link set dev $swp1 master br1
97	ip link set dev $swp1 up
98
99	bridge vlan add dev br1 vid 555 self pvid untagged
100	bridge vlan add dev $swp1 vid 555
101	bridge vlan add dev $swp1 vid 777
102
103	__addr_add_del br1 add 192.0.2.2/28 2001:db8:1::2/64
104	__addr_add_del br1 add 192.0.2.18/28 2001:db8:3::2/64
105
106	ip link set dev $swp2 up
107	__addr_add_del $swp2 add 192.0.2.129/28 2001:db8:2::1/64
108	__addr_add_del $swp2 add 192.0.2.145/28 2001:db8:4::1/64
109}
110
111router_destroy()
112{
113	__addr_add_del $swp2 del 192.0.2.145/28 2001:db8:4::1/64
114	__addr_add_del $swp2 del 192.0.2.129/28 2001:db8:2::1/64
115	ip link set dev $swp2 down
116
117	__addr_add_del br1 del 192.0.2.18/28 2001:db8:3::2/64
118	__addr_add_del br1 del 192.0.2.2/28 2001:db8:1::2/64
119	ip link set dev $swp1 down
120	ip link set dev $swp1 nomaster
121
122	ip link del dev br1
123}
124
125setup_prepare()
126{
127	h1=${NETIFS[p1]}
128	swp1=${NETIFS[p2]}
129
130	swp2=${NETIFS[p3]}
131	h2=${NETIFS[p4]}
132
133	vrf_prepare
134
135	h1_create
136	h2_create
137
138	router_create
139
140	forwarding_enable
141}
142
143config_555()
144{
145	log_info "Configure VLAN 555 as PVID"
146
147	bridge vlan add dev br1 vid 555 self pvid untagged
148	bridge vlan del dev br1 vid 777 self
149	sleep 2
150}
151
152config_777()
153{
154	log_info "Configure VLAN 777 as PVID"
155
156	bridge vlan add dev br1 vid 777 self pvid untagged
157	bridge vlan del dev br1 vid 555 self
158	sleep 2
159}
160
161cleanup()
162{
163	pre_cleanup
164
165	forwarding_restore
166
167	router_destroy
168
169	h2_destroy
170	h1_destroy
171
172	vrf_cleanup
173}
174
175vlan()
176{
177	RET=0
178
179	bridge vlan add dev br1 vid 333 self
180	check_err $? "Can't add a non-PVID VLAN"
181	bridge vlan del dev br1 vid 333 self
182	check_err $? "Can't remove a non-PVID VLAN"
183
184	log_test "vlan"
185}
186
187ping_ipv4()
188{
189	ping_test $h1.555 192.0.2.130
190}
191
192ping_ipv6()
193{
194	ping6_test $h1.555 2001:db8:2::2
195}
196
197ping_ipv4_fails()
198{
199	ping_test_fails $h1.555 192.0.2.130 ": via 555"
200}
201
202ping_ipv6_fails()
203{
204	ping6_test_fails $h1.555 2001:db8:2::2 ": via 555"
205}
206
207ping_ipv4_777()
208{
209	ping_test $h1.777 192.0.2.146 ": via 777"
210}
211
212ping_ipv6_777()
213{
214	ping6_test $h1.777 2001:db8:4::2 ": via 777"
215}
216
217trap cleanup EXIT
218
219setup_prepare
220setup_wait
221
222tests_run
223
224exit $EXIT_STATUS
225