3 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 # Copyright (c) 2020 The FreeBSD Foundation
7 # This software was developed by Kristof Provost under sponsorship
8 # from the FreeBSD Foundation.
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions
13 # 1. Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in the
17 # documentation and/or other materials provided with the distribution.
19 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 . $(atf_get_srcdir)/../common/vnet.subr
33 atf_test_case "bridge_transmit_ipv4_unicast" "cleanup"
34 bridge_transmit_ipv4_unicast_head()
36 atf_set descr 'bridge_transmit_ipv4_unicast bridging test'
37 atf_set require.user root
40 bridge_transmit_ipv4_unicast_body()
44 epair_alcatraz=$(vnet_mkepair)
45 epair_singsing=$(vnet_mkepair)
47 vnet_mkjail alcatraz ${epair_alcatraz}b
48 vnet_mkjail singsing ${epair_singsing}b
50 jexec alcatraz ifconfig ${epair_alcatraz}b 192.0.2.1/24 up
51 jexec singsing ifconfig ${epair_singsing}b 192.0.2.2/24 up
53 bridge=$(vnet_mkbridge)
56 ifconfig ${epair_alcatraz}a up
57 ifconfig ${epair_singsing}a up
58 ifconfig ${bridge} addm ${epair_alcatraz}a
59 ifconfig ${bridge} addm ${epair_singsing}a
61 atf_check -s exit:0 -o ignore jexec alcatraz ping -c 3 -t 1 192.0.2.2
62 atf_check -s exit:0 -o ignore jexec singsing ping -c 3 -t 1 192.0.2.1
65 bridge_transmit_ipv4_unicast_cleanup()
70 atf_test_case "stp" "cleanup"
73 atf_set descr 'Spanning tree test'
74 atf_set require.user root
81 epair_one=$(vnet_mkepair)
82 epair_two=$(vnet_mkepair)
83 bridge_a=$(vnet_mkbridge)
84 bridge_b=$(vnet_mkbridge)
86 vnet_mkjail a ${bridge_a} ${epair_one}a ${epair_two}a
87 vnet_mkjail b ${bridge_b} ${epair_one}b ${epair_two}b
89 jexec a ifconfig ${epair_one}a up
90 jexec a ifconfig ${epair_two}a up
91 jexec a ifconfig ${bridge_a} addm ${epair_one}a
92 jexec a ifconfig ${bridge_a} addm ${epair_two}a
94 jexec b ifconfig ${epair_one}b up
95 jexec b ifconfig ${epair_two}b up
96 jexec b ifconfig ${bridge_b} addm ${epair_one}b
97 jexec b ifconfig ${bridge_b} addm ${epair_two}b
99 jexec a ifconfig ${bridge_a} 192.0.2.1/24
101 # Enable spanning tree
102 jexec a ifconfig ${bridge_a} stp ${epair_one}a
103 jexec a ifconfig ${bridge_a} stp ${epair_two}a
104 jexec b ifconfig ${bridge_b} stp ${epair_one}b
105 jexec b ifconfig ${bridge_b} stp ${epair_two}b
107 jexec b ifconfig ${bridge_b} up
108 jexec a ifconfig ${bridge_a} up
110 # Give STP time to do its thing
113 a_discard=$(jexec a ifconfig ${bridge_a} | grep discarding)
114 b_discard=$(jexec b ifconfig ${bridge_b} | grep discarding)
116 if [ -z "${a_discard}" ] && [ -z "${b_discard}" ]
118 atf_fail "STP failed to detect bridging loop"
121 # We must also have at least some forwarding interfaces
122 a_forwarding=$(jexec a ifconfig ${bridge_a} | grep forwarding)
123 b_forwarding=$(jexec b ifconfig ${bridge_b} | grep forwarding)
125 if [ -z "${a_forwarding}" ] && [ -z "${b_forwarding}" ]
127 atf_fail "STP failed to detect bridging loop"
136 atf_test_case "stp_vlan" "cleanup"
139 atf_set descr 'Spanning tree on VLAN test'
140 atf_set require.user root
147 epair_one=$(vnet_mkepair)
148 epair_two=$(vnet_mkepair)
149 bridge_a=$(vnet_mkbridge)
150 bridge_b=$(vnet_mkbridge)
152 vnet_mkjail a ${bridge_a} ${epair_one}a ${epair_two}a
153 vnet_mkjail b ${bridge_b} ${epair_one}b ${epair_two}b
155 jexec a ifconfig ${epair_one}a up
156 jexec a ifconfig ${epair_two}a up
157 vlan_a_one=$(jexec a ifconfig vlan create vlandev ${epair_one}a vlan 42)
158 vlan_a_two=$(jexec a ifconfig vlan create vlandev ${epair_two}a vlan 42)
159 jexec a ifconfig ${vlan_a_one} up
160 jexec a ifconfig ${vlan_a_two} up
161 jexec a ifconfig ${bridge_a} addm ${vlan_a_one}
162 jexec a ifconfig ${bridge_a} addm ${vlan_a_two}
164 jexec b ifconfig ${epair_one}b up
165 jexec b ifconfig ${epair_two}b up
166 vlan_b_one=$(jexec b ifconfig vlan create vlandev ${epair_one}b vlan 42)
167 vlan_b_two=$(jexec b ifconfig vlan create vlandev ${epair_two}b vlan 42)
168 jexec b ifconfig ${vlan_b_one} up
169 jexec b ifconfig ${vlan_b_two} up
170 jexec b ifconfig ${bridge_b} addm ${vlan_b_one}
171 jexec b ifconfig ${bridge_b} addm ${vlan_b_two}
173 jexec a ifconfig ${bridge_a} 192.0.2.1/24
175 # Enable spanning tree
176 jexec a ifconfig ${bridge_a} stp ${vlan_a_one}
177 jexec a ifconfig ${bridge_a} stp ${vlan_a_two}
178 jexec b ifconfig ${bridge_b} stp ${vlan_b_one}
179 jexec b ifconfig ${bridge_b} stp ${vlan_b_two}
181 jexec b ifconfig ${bridge_b} up
182 jexec a ifconfig ${bridge_a} up
184 # Give STP time to do its thing
187 a_discard=$(jexec a ifconfig ${bridge_a} | grep discarding)
188 b_discard=$(jexec b ifconfig ${bridge_b} | grep discarding)
190 if [ -z "${a_discard}" ] && [ -z "${b_discard}" ]
192 atf_fail "STP failed to detect bridging loop"
195 # We must also have at least some forwarding interfaces
196 a_forwarding=$(jexec a ifconfig ${bridge_a} | grep forwarding)
197 b_forwarding=$(jexec b ifconfig ${bridge_b} | grep forwarding)
199 if [ -z "${a_forwarding}" ] && [ -z "${b_forwarding}" ]
201 atf_fail "STP failed to detect bridging loop"
210 atf_test_case "static" "cleanup"
213 atf_set descr 'Bridge static address test'
214 atf_set require.user root
221 epair=$(vnet_mkepair)
222 bridge=$(vnet_mkbridge)
224 vnet_mkjail one ${bridge} ${epair}a
226 ifconfig ${epair}b up
228 jexec one ifconfig ${bridge} up
229 jexec one ifconfig ${epair}a up
230 jexec one ifconfig ${bridge} addm ${epair}a
233 atf_check -s exit:1 -o ignore -e ignore \
234 jexec one ifconfig ${bridge} static ${epair}b 00:01:02:03:04:05
237 atf_check -s exit:1 -o ignore -e ignore \
238 jexec one ifconfig ${bridge} static ${epair}a 00:01:02:03:04
241 atf_check -s exit:0 -o ignore \
242 jexec one ifconfig ${bridge} static ${epair}a 00:01:02:03:04:05
245 atf_check -s exit:0 -o ignore \
246 jexec one ifconfig ${bridge} addr
248 # Delete with bad address format
249 atf_check -s exit:1 -o ignore -e ignore \
250 jexec one ifconfig ${bridge} deladdr 00:01:02:03:04
252 # Delete with unlisted address
253 atf_check -s exit:1 -o ignore -e ignore \
254 jexec one ifconfig ${bridge} deladdr 00:01:02:03:04:06
257 atf_check -s exit:0 -o ignore \
258 jexec one ifconfig ${bridge} deladdr 00:01:02:03:04:05
266 atf_test_case "span" "cleanup"
269 atf_set descr 'Bridge span test'
270 atf_set require.user root
278 epair=$(vnet_mkepair)
279 epair_span=$(vnet_mkepair)
280 bridge=$(vnet_mkbridge)
282 vnet_mkjail one ${bridge} ${epair}a ${epair_span}a
284 ifconfig ${epair}b up
285 ifconfig ${epair_span}b up
287 jexec one ifconfig ${bridge} up
288 jexec one ifconfig ${epair}a up
289 jexec one ifconfig ${epair_span}a up
290 jexec one ifconfig ${bridge} addm ${epair}a
292 jexec one ifconfig ${bridge} span ${epair_span}a
293 jexec one ifconfig ${bridge} 192.0.2.1/24
295 # Send some traffic through the span
296 jexec one ping -c 1 -t 1 192.0.2.2
298 # Check that we see the traffic on the span interface
299 atf_check -s exit:0 \
300 $(atf_get_srcdir)/../netpfil/common/pft_ping.py \
303 --recvif ${epair_span}b
305 jexec one ifconfig ${bridge} -span ${epair_span}a
307 # And no more traffic after we remove the span
308 atf_check -s exit:1 \
309 $(atf_get_srcdir)/../netpfil/common/pft_ping.py \
312 --recvif ${epair_span}b
320 atf_test_case "delete_with_members" "cleanup"
321 delete_with_members_head()
323 atf_set descr 'Delete a bridge which still has member interfaces'
324 atf_set require.user root
327 delete_with_members_body()
331 bridge=$(vnet_mkbridge)
332 epair=$(vnet_mkepair)
334 ifconfig ${bridge} 192.0.2.1/24 up
335 ifconfig ${epair}a up
336 ifconfig ${bridge} addm ${epair}a
338 ifconfig ${bridge} destroy
341 delete_with_members_cleanup()
346 atf_test_case "mac_conflict" "cleanup"
349 atf_set descr 'Ensure that bridges in different jails get different mac addresses'
350 atf_set require.user root
357 epair=$(vnet_mkepair)
359 # Ensure the bridge module is loaded so jails can use it.
360 tmpbridge=$(vnet_mkbridge)
362 vnet_mkjail bridge_mac_conflict_one ${epair}a
363 vnet_mkjail bridge_mac_conflict_two ${epair}b
365 jexec bridge_mac_conflict_one ifconfig bridge create
366 jexec bridge_mac_conflict_one ifconfig bridge0 192.0.2.1/24 up \
368 jexec bridge_mac_conflict_one ifconfig ${epair}a up
370 jexec bridge_mac_conflict_two ifconfig bridge create
371 jexec bridge_mac_conflict_two ifconfig bridge0 192.0.2.2/24 up \
373 jexec bridge_mac_conflict_two ifconfig ${epair}b up
375 atf_check -s exit:0 -o ignore \
376 jexec bridge_mac_conflict_one ping -c 3 192.0.2.2
379 mac_conflict_cleanup()
384 atf_test_case "inherit_mac" "cleanup"
387 atf_set descr 'Bridge inherit_mac test, #216510'
388 atf_set require.user root
395 bridge=$(vnet_mkbridge)
396 epair=$(vnet_mkepair)
397 vnet_mkjail one ${bridge} ${epair}a
399 jexec one sysctl net.link.bridge.inherit_mac=1
401 # Attempt to provoke the panic described in #216510
402 jexec one ifconfig ${bridge} 192.0.0.1/24 up
403 jexec one ifconfig ${bridge} addm ${epair}a
406 inherit_mac_cleanup()
411 atf_test_case "stp_validation" "cleanup"
412 stp_validation_head()
414 atf_set descr 'Check STP validation'
415 atf_set require.user root
416 atf_set require.progs scapy
419 stp_validation_body()
423 epair_one=$(vnet_mkepair)
424 epair_two=$(vnet_mkepair)
425 bridge=$(vnet_mkbridge)
427 ifconfig ${bridge} up
428 ifconfig ${bridge} addm ${epair_one}a addm ${epair_two}a
429 ifconfig ${bridge} stp ${epair_one}a stp ${epair_two}a
431 ifconfig ${epair_one}a up
432 ifconfig ${epair_one}b up
433 ifconfig ${epair_two}a up
434 ifconfig ${epair_two}b up
436 # Wait until the interfaces are no longer discarding
437 while ifconfig ${bridge} | grep 'state discarding' >/dev/null
442 # Now inject invalid STP BPDUs on epair_one and see if they're repeated
444 atf_check -s exit:0 \
445 $(atf_get_srcdir)/stp.py \
446 --sendif ${epair_one}b \
447 --recvif ${epair_two}b
450 stp_validation_cleanup()
455 atf_init_test_cases()
457 atf_add_test_case "bridge_transmit_ipv4_unicast"
458 atf_add_test_case "stp"
459 atf_add_test_case "stp_vlan"
460 atf_add_test_case "static"
461 atf_add_test_case "span"
462 atf_add_test_case "inherit_mac"
463 atf_add_test_case "delete_with_members"
464 atf_add_test_case "mac_conflict"
465 atf_add_test_case "stp_validation"