xref: /freebsd/tests/sys/net/if_bridge_test.sh (revision 3a3af6b2a160bea72509a9d5ef84e25906b0478a)
1# $FreeBSD$
2#
3# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4#
5# Copyright (c) 2020 The FreeBSD Foundation
6#
7# This software was developed by Kristof Provost under sponsorship
8# from the FreeBSD Foundation.
9#
10# Redistribution and use in source and binary forms, with or without
11# modification, are permitted provided that the following conditions
12# are met:
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.
18#
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
29# SUCH DAMAGE.
30
31. $(atf_get_srcdir)/../common/vnet.subr
32
33atf_test_case "bridge_transmit_ipv4_unicast" "cleanup"
34bridge_transmit_ipv4_unicast_head()
35{
36	atf_set descr 'bridge_transmit_ipv4_unicast bridging test'
37	atf_set require.user root
38}
39
40bridge_transmit_ipv4_unicast_body()
41{
42	vnet_init
43
44	epair_alcatraz=$(vnet_mkepair)
45	epair_singsing=$(vnet_mkepair)
46
47	vnet_mkjail alcatraz ${epair_alcatraz}b
48	vnet_mkjail singsing ${epair_singsing}b
49
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
52
53	bridge=$(vnet_mkbridge)
54
55	ifconfig ${bridge} up
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
60
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
63}
64
65bridge_transmit_ipv4_unicast_cleanup()
66{
67	vnet_cleanup
68}
69
70atf_test_case "stp" "cleanup"
71stp_head()
72{
73	atf_set descr 'Spanning tree test'
74	atf_set require.user root
75}
76
77stp_body()
78{
79	vnet_init
80
81	epair_one=$(vnet_mkepair)
82	epair_two=$(vnet_mkepair)
83	bridge_a=$(vnet_mkbridge)
84	bridge_b=$(vnet_mkbridge)
85
86	vnet_mkjail a ${bridge_a} ${epair_one}a ${epair_two}a
87	vnet_mkjail b ${bridge_b} ${epair_one}b ${epair_two}b
88
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
93
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
98
99	jexec a ifconfig ${bridge_a} 192.0.2.1/24
100
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
106
107	jexec b ifconfig ${bridge_b} up
108	jexec a ifconfig ${bridge_a} up
109
110	# Give STP time to do its thing
111	sleep 5
112
113	a_discard=$(jexec a ifconfig ${bridge_a} | grep discarding)
114	b_discard=$(jexec b ifconfig ${bridge_b} | grep discarding)
115
116	if [ -z "${a_discard}" ] && [ -z "${b_discard}" ]
117	then
118		atf_fail "STP failed to detect bridging loop"
119	fi
120
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)
124
125	if [ -z "${a_forwarding}" ] && [ -z "${b_forwarding}" ]
126	then
127		atf_fail "STP failed to detect bridging loop"
128	fi
129}
130
131stp_cleanup()
132{
133	vnet_cleanup
134}
135
136atf_test_case "stp_vlan" "cleanup"
137stp_vlan_head()
138{
139	atf_set descr 'Spanning tree on VLAN test'
140	atf_set require.user root
141}
142
143stp_vlan_body()
144{
145	vnet_init
146
147	epair_one=$(vnet_mkepair)
148	epair_two=$(vnet_mkepair)
149	bridge_a=$(vnet_mkbridge)
150	bridge_b=$(vnet_mkbridge)
151
152	vnet_mkjail a ${bridge_a} ${epair_one}a ${epair_two}a
153	vnet_mkjail b ${bridge_b} ${epair_one}b ${epair_two}b
154
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}
163
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}
172
173	jexec a ifconfig ${bridge_a} 192.0.2.1/24
174
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}
180
181	jexec b ifconfig ${bridge_b} up
182	jexec a ifconfig ${bridge_a} up
183
184	# Give STP time to do its thing
185	sleep 5
186
187	a_discard=$(jexec a ifconfig ${bridge_a} | grep discarding)
188	b_discard=$(jexec b ifconfig ${bridge_b} | grep discarding)
189
190	if [ -z "${a_discard}" ] && [ -z "${b_discard}" ]
191	then
192		atf_fail "STP failed to detect bridging loop"
193	fi
194
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)
198
199	if [ -z "${a_forwarding}" ] && [ -z "${b_forwarding}" ]
200	then
201		atf_fail "STP failed to detect bridging loop"
202	fi
203}
204
205stp_vlan_cleanup()
206{
207	vnet_cleanup
208}
209
210atf_test_case "static" "cleanup"
211static_head()
212{
213	atf_set descr 'Bridge static address test'
214	atf_set require.user root
215}
216
217static_body()
218{
219	vnet_init
220
221	epair=$(vnet_mkepair)
222	bridge=$(vnet_mkbridge)
223
224	vnet_mkjail one ${bridge} ${epair}a
225
226	ifconfig ${epair}b up
227
228	jexec one ifconfig ${bridge} up
229	jexec one ifconfig ${epair}a up
230	jexec one ifconfig ${bridge} addm ${epair}a
231
232	# Wrong interface
233	atf_check -s exit:1 -o ignore -e ignore \
234	    jexec one ifconfig ${bridge} static ${epair}b 00:01:02:03:04:05
235
236	# Bad address format
237	atf_check -s exit:1 -o ignore -e ignore \
238	    jexec one ifconfig ${bridge} static ${epair}a 00:01:02:03:04
239
240	# Correct add
241	atf_check -s exit:0 -o ignore \
242	    jexec one ifconfig ${bridge} static ${epair}a 00:01:02:03:04:05
243
244	# List addresses
245	atf_check -s exit:0 -o ignore \
246	    jexec one ifconfig ${bridge} addr
247
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
251
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
255
256	# Correct delete
257	atf_check -s exit:0 -o ignore \
258	    jexec one ifconfig ${bridge} deladdr 00:01:02:03:04:05
259}
260
261static_cleanup()
262{
263	vnet_cleanup
264}
265
266atf_test_case "span" "cleanup"
267span_head()
268{
269	atf_set descr 'Bridge span test'
270	atf_set require.user root
271	atf_set require.progs scapy
272}
273
274span_body()
275{
276	set -x
277	vnet_init
278
279	if [ "$(atf_config_get ci false)" = "true" ]; then
280		atf_skip "https://bugs.freebsd.org/260461"
281	fi
282
283	epair=$(vnet_mkepair)
284	epair_span=$(vnet_mkepair)
285	bridge=$(vnet_mkbridge)
286
287	vnet_mkjail one ${bridge} ${epair}a ${epair_span}a
288
289	ifconfig ${epair}b up
290	ifconfig ${epair_span}b up
291
292	jexec one ifconfig ${bridge} up
293	jexec one ifconfig ${epair}a up
294	jexec one ifconfig ${epair_span}a up
295	jexec one ifconfig ${bridge} addm ${epair}a
296
297	jexec one ifconfig ${bridge} span ${epair_span}a
298	jexec one ifconfig ${bridge} 192.0.2.1/24
299
300	# Send some traffic through the span
301	jexec one ping -c 1 -t 1 192.0.2.2
302
303	# Check that we see the traffic on the span interface
304	atf_check -s exit:0 \
305		$(atf_get_srcdir)/../netpfil/common/pft_ping.py \
306		--sendif ${epair}b \
307		--to 192.0.2.2 \
308		--recvif ${epair_span}b
309
310	jexec one ifconfig ${bridge} -span ${epair_span}a
311
312	# And no more traffic after we remove the span
313	atf_check -s exit:1 \
314		$(atf_get_srcdir)/../netpfil/common/pft_ping.py \
315		--sendif ${epair}b \
316		--to 192.0.2.2 \
317		--recvif ${epair_span}b
318}
319
320span_cleanup()
321{
322	vnet_cleanup
323}
324
325atf_test_case "delete_with_members" "cleanup"
326delete_with_members_head()
327{
328	atf_set descr 'Delete a bridge which still has member interfaces'
329	atf_set require.user root
330}
331
332delete_with_members_body()
333{
334	vnet_init
335
336	bridge=$(vnet_mkbridge)
337	epair=$(vnet_mkepair)
338
339	ifconfig ${bridge} 192.0.2.1/24 up
340	ifconfig ${epair}a up
341	ifconfig ${bridge} addm ${epair}a
342
343	ifconfig ${bridge} destroy
344}
345
346delete_with_members_cleanup()
347{
348	vnet_cleanup
349}
350
351atf_test_case "mac_conflict" "cleanup"
352mac_conflict_head()
353{
354	atf_set descr 'Ensure that bridges in different jails get different mac addresses'
355	atf_set require.user root
356}
357
358mac_conflict_body()
359{
360	vnet_init
361
362	epair=$(vnet_mkepair)
363
364	# Ensure the bridge module is loaded so jails can use it.
365	tmpbridge=$(vnet_mkbridge)
366
367	vnet_mkjail bridge_mac_conflict_one ${epair}a
368	vnet_mkjail bridge_mac_conflict_two ${epair}b
369
370	jexec bridge_mac_conflict_one ifconfig bridge create
371	jexec bridge_mac_conflict_one ifconfig bridge0 192.0.2.1/24 up \
372	    addm ${epair}a
373	jexec bridge_mac_conflict_one ifconfig ${epair}a up
374
375	jexec bridge_mac_conflict_two ifconfig bridge create
376	jexec bridge_mac_conflict_two ifconfig bridge0 192.0.2.2/24 up \
377	    addm ${epair}b
378	jexec bridge_mac_conflict_two ifconfig ${epair}b up
379
380	atf_check -s exit:0 -o ignore \
381	    jexec bridge_mac_conflict_one ping -c 3 192.0.2.2
382}
383
384mac_conflict_cleanup()
385{
386	vnet_cleanup
387}
388
389atf_test_case "inherit_mac" "cleanup"
390inherit_mac_head()
391{
392	atf_set descr 'Bridge inherit_mac test, #216510'
393	atf_set require.user root
394}
395
396inherit_mac_body()
397{
398	vnet_init
399
400	bridge=$(vnet_mkbridge)
401	epair=$(vnet_mkepair)
402	vnet_mkjail one ${bridge} ${epair}a
403
404	jexec one sysctl net.link.bridge.inherit_mac=1
405
406	# Attempt to provoke the panic described in #216510
407	jexec one ifconfig ${bridge} 192.0.0.1/24 up
408	jexec one ifconfig ${bridge} addm ${epair}a
409}
410
411inherit_mac_cleanup()
412{
413	vnet_cleanup
414}
415
416atf_test_case "stp_validation" "cleanup"
417stp_validation_head()
418{
419	atf_set descr 'Check STP validation'
420	atf_set require.user root
421	atf_set require.progs scapy
422}
423
424stp_validation_body()
425{
426	vnet_init
427
428	epair_one=$(vnet_mkepair)
429	epair_two=$(vnet_mkepair)
430	bridge=$(vnet_mkbridge)
431
432	ifconfig ${bridge} up
433	ifconfig ${bridge} addm ${epair_one}a addm ${epair_two}a
434	ifconfig ${bridge} stp ${epair_one}a stp ${epair_two}a
435
436	ifconfig ${epair_one}a up
437	ifconfig ${epair_one}b up
438	ifconfig ${epair_two}a up
439	ifconfig ${epair_two}b up
440
441	# Wait until the interfaces are no longer discarding
442	while ifconfig ${bridge} | grep 'state discarding' >/dev/null
443	do
444		sleep 1
445	done
446
447	# Now inject invalid STP BPDUs on epair_one and see if they're repeated
448	# on epair_two
449	atf_check -s exit:0 \
450	    $(atf_get_srcdir)/stp.py \
451	    --sendif ${epair_one}b \
452	    --recvif ${epair_two}b
453}
454
455stp_validation_cleanup()
456{
457	vnet_cleanup
458}
459
460atf_test_case "gif" "cleanup"
461gif_head()
462{
463	atf_set descr 'gif as a bridge member'
464	atf_set require.user root
465}
466
467gif_body()
468{
469	vnet_init
470
471	epair=$(vnet_mkepair)
472
473	vnet_mkjail one ${epair}a
474	vnet_mkjail two ${epair}b
475
476	jexec one sysctl net.link.gif.max_nesting=2
477	jexec two sysctl net.link.gif.max_nesting=2
478
479	jexec one ifconfig ${epair}a 192.0.2.1/24 up
480	jexec two ifconfig ${epair}b 192.0.2.2/24 up
481
482	# Tunnel
483	gif_one=$(jexec one ifconfig gif create)
484	gif_two=$(jexec two ifconfig gif create)
485
486	jexec one ifconfig ${gif_one} tunnel 192.0.2.1 192.0.2.2
487	jexec one ifconfig ${gif_one} up
488	jexec two ifconfig ${gif_two} tunnel 192.0.2.2 192.0.2.1
489	jexec two ifconfig ${gif_two} up
490
491	bridge_one=$(jexec one ifconfig bridge create)
492	bridge_two=$(jexec two ifconfig bridge create)
493	jexec one ifconfig ${bridge_one} 198.51.100.1/24 up
494	jexec one ifconfig ${bridge_one} addm ${gif_one}
495	jexec two ifconfig ${bridge_two} 198.51.100.2/24 up
496	jexec two ifconfig ${bridge_two} addm ${gif_two}
497
498	# Sanity check
499	atf_check -s exit:0 -o ignore \
500		jexec one ping -c 1 192.0.2.2
501
502	# Test tunnel
503	atf_check -s exit:0 -o ignore \
504		jexec one ping -c 1 198.51.100.2
505	atf_check -s exit:0 -o ignore \
506		jexec one ping -c 1 -s 1200 198.51.100.2
507	atf_check -s exit:0 -o ignore \
508		jexec one ping -c 1 -s 2000 198.51.100.2
509
510	# Higher MTU on the tunnel than on the underlying interface
511	jexec one ifconfig ${epair}a mtu 1000
512	jexec two ifconfig ${epair}b mtu 1000
513
514	atf_check -s exit:0 -o ignore \
515		jexec one ping -c 1 -s 1200 198.51.100.2
516	atf_check -s exit:0 -o ignore \
517		jexec one ping -c 1 -s 2000 198.51.100.2
518}
519
520gif_cleanup()
521{
522	vnet_cleanup
523}
524
525atf_test_case "mtu" "cleanup"
526mtu_head()
527{
528	atf_set descr 'Bridge MTU changes'
529	atf_set require.user root
530}
531
532get_mtu()
533{
534	intf=$1
535
536	ifconfig ${intf} ether | awk '$5 == "mtu" { print $6 }'
537}
538
539check_mtu()
540{
541	intf=$1
542	expected=$2
543
544	mtu=$(get_mtu $intf)
545	if [ $mtu -ne $expected ];
546	then
547		atf_fail "Expected MTU of $expected on $intf but found $mtu"
548	fi
549}
550
551mtu_body()
552{
553	vnet_init
554
555	epair=$(vnet_mkepair)
556	gif=$(ifconfig gif create)
557	echo ${gif} >> created_interfaces.lst
558	bridge=$(vnet_mkbridge)
559
560	atf_check -s exit:0 \
561		ifconfig ${bridge} addm ${epair}a
562
563	ifconfig ${gif} mtu 1500
564	atf_check -s exit:0 \
565		ifconfig ${bridge} addm ${gif}
566
567	# Changing MTU changes it for all member interfaces
568	atf_check -s exit:0 \
569		ifconfig ${bridge} mtu 2000
570
571	check_mtu ${bridge} 2000
572	check_mtu ${gif} 2000
573	check_mtu ${epair}a 2000
574
575	# Rejected MTUs mean none of the MTUs change
576	atf_check -s exit:1 -e ignore \
577		ifconfig ${bridge} mtu 9000
578
579	check_mtu ${bridge} 2000
580	check_mtu ${gif} 2000
581	check_mtu ${epair}a 2000
582
583	# We're not allowed to change the MTU of a member interface
584	atf_check -s exit:1 -e ignore \
585		ifconfig ${epair}a mtu 1900
586	check_mtu ${epair}a 2000
587
588	# Test adding an interface with a different MTU
589	new_epair=$(vnet_mkepair)
590	check_mtu ${new_epair}a 1500
591	atf_check -s exit:0 -e ignore \
592		ifconfig ${bridge} addm ${new_epair}a
593
594	check_mtu ${bridge} 2000
595	check_mtu ${gif} 2000
596	check_mtu ${epair}a 2000
597	check_mtu ${new_epair}a 2000
598}
599
600mtu_cleanup()
601{
602	vnet_cleanup
603}
604
605atf_init_test_cases()
606{
607	atf_add_test_case "bridge_transmit_ipv4_unicast"
608	atf_add_test_case "stp"
609	atf_add_test_case "stp_vlan"
610	atf_add_test_case "static"
611	atf_add_test_case "span"
612	atf_add_test_case "inherit_mac"
613	atf_add_test_case "delete_with_members"
614	atf_add_test_case "mac_conflict"
615	atf_add_test_case "stp_validation"
616	atf_add_test_case "gif"
617	atf_add_test_case "mtu"
618}
619