xref: /freebsd/tests/sys/net/if_bridge_test.sh (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
1# $FreeBSD$
2#
3# SPDX-License-Identifier: BSD-2-Clause
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	vnet_init
277
278	epair=$(vnet_mkepair)
279	epair_span=$(vnet_mkepair)
280	bridge=$(vnet_mkbridge)
281
282	vnet_mkjail one ${bridge} ${epair}a ${epair_span}a
283
284	ifconfig ${epair}b up
285	ifconfig ${epair_span}b up
286
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
291
292	jexec one ifconfig ${bridge} span ${epair_span}a
293	jexec one ifconfig ${bridge} 192.0.2.1/24
294
295	# Send some traffic through the span
296	jexec one ping -c 1 -t 1 192.0.2.2
297
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 \
301		--sendif ${epair}b \
302		--to 192.0.2.2 \
303		--recvif ${epair_span}b
304
305	jexec one ifconfig ${bridge} -span ${epair_span}a
306
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 \
310		--sendif ${epair}b \
311		--to 192.0.2.2 \
312		--recvif ${epair_span}b
313}
314
315span_cleanup()
316{
317	vnet_cleanup
318}
319
320atf_test_case "delete_with_members" "cleanup"
321delete_with_members_head()
322{
323	atf_set descr 'Delete a bridge which still has member interfaces'
324	atf_set require.user root
325}
326
327delete_with_members_body()
328{
329	vnet_init
330
331	bridge=$(vnet_mkbridge)
332	epair=$(vnet_mkepair)
333
334	ifconfig ${bridge} 192.0.2.1/24 up
335	ifconfig ${epair}a up
336	ifconfig ${bridge} addm ${epair}a
337
338	ifconfig ${bridge} destroy
339}
340
341delete_with_members_cleanup()
342{
343	vnet_cleanup
344}
345
346atf_test_case "mac_conflict" "cleanup"
347mac_conflict_head()
348{
349	atf_set descr 'Ensure that bridges in different jails get different mac addresses'
350	atf_set require.user root
351}
352
353mac_conflict_body()
354{
355	vnet_init
356
357	epair=$(vnet_mkepair)
358
359	# Ensure the bridge module is loaded so jails can use it.
360	tmpbridge=$(vnet_mkbridge)
361
362	vnet_mkjail bridge_mac_conflict_one ${epair}a
363	vnet_mkjail bridge_mac_conflict_two ${epair}b
364
365	jexec bridge_mac_conflict_one ifconfig bridge create
366	jexec bridge_mac_conflict_one ifconfig bridge0 192.0.2.1/24 up \
367	    addm ${epair}a
368	jexec bridge_mac_conflict_one ifconfig ${epair}a up
369
370	jexec bridge_mac_conflict_two ifconfig bridge create
371	jexec bridge_mac_conflict_two ifconfig bridge0 192.0.2.2/24 up \
372	    addm ${epair}b
373	jexec bridge_mac_conflict_two ifconfig ${epair}b up
374
375	atf_check -s exit:0 -o ignore \
376	    jexec bridge_mac_conflict_one ping -c 3 192.0.2.2
377}
378
379mac_conflict_cleanup()
380{
381	vnet_cleanup
382}
383
384atf_test_case "inherit_mac" "cleanup"
385inherit_mac_head()
386{
387	atf_set descr 'Bridge inherit_mac test, #216510'
388	atf_set require.user root
389}
390
391inherit_mac_body()
392{
393	vnet_init
394
395	bridge=$(vnet_mkbridge)
396	epair=$(vnet_mkepair)
397	vnet_mkjail one ${bridge} ${epair}a
398
399	jexec one sysctl net.link.bridge.inherit_mac=1
400
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
404}
405
406inherit_mac_cleanup()
407{
408	vnet_cleanup
409}
410
411atf_test_case "stp_validation" "cleanup"
412stp_validation_head()
413{
414	atf_set descr 'Check STP validation'
415	atf_set require.user root
416	atf_set require.progs scapy
417}
418
419stp_validation_body()
420{
421	vnet_init
422
423	epair_one=$(vnet_mkepair)
424	epair_two=$(vnet_mkepair)
425	bridge=$(vnet_mkbridge)
426
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
430
431	ifconfig ${epair_one}a up
432	ifconfig ${epair_one}b up
433	ifconfig ${epair_two}a up
434	ifconfig ${epair_two}b up
435
436	# Wait until the interfaces are no longer discarding
437	while ifconfig ${bridge} | grep 'state discarding' >/dev/null
438	do
439		sleep 1
440	done
441
442	# Now inject invalid STP BPDUs on epair_one and see if they're repeated
443	# on epair_two
444	atf_check -s exit:0 \
445	    $(atf_get_srcdir)/stp.py \
446	    --sendif ${epair_one}b \
447	    --recvif ${epair_two}b
448}
449
450stp_validation_cleanup()
451{
452	vnet_cleanup
453}
454
455atf_test_case "gif" "cleanup"
456gif_head()
457{
458	atf_set descr 'gif as a bridge member'
459	atf_set require.user root
460}
461
462gif_body()
463{
464	vnet_init
465
466	epair=$(vnet_mkepair)
467
468	vnet_mkjail one ${epair}a
469	vnet_mkjail two ${epair}b
470
471	jexec one sysctl net.link.gif.max_nesting=2
472	jexec two sysctl net.link.gif.max_nesting=2
473
474	jexec one ifconfig ${epair}a 192.0.2.1/24 up
475	jexec two ifconfig ${epair}b 192.0.2.2/24 up
476
477	# Tunnel
478	gif_one=$(jexec one ifconfig gif create)
479	gif_two=$(jexec two ifconfig gif create)
480
481	jexec one ifconfig ${gif_one} tunnel 192.0.2.1 192.0.2.2
482	jexec one ifconfig ${gif_one} up
483	jexec two ifconfig ${gif_two} tunnel 192.0.2.2 192.0.2.1
484	jexec two ifconfig ${gif_two} up
485
486	bridge_one=$(jexec one ifconfig bridge create)
487	bridge_two=$(jexec two ifconfig bridge create)
488	jexec one ifconfig ${bridge_one} 198.51.100.1/24 up
489	jexec one ifconfig ${bridge_one} addm ${gif_one}
490	jexec two ifconfig ${bridge_two} 198.51.100.2/24 up
491	jexec two ifconfig ${bridge_two} addm ${gif_two}
492
493	# Sanity check
494	atf_check -s exit:0 -o ignore \
495		jexec one ping -c 1 192.0.2.2
496
497	# Test tunnel
498	atf_check -s exit:0 -o ignore \
499		jexec one ping -c 1 198.51.100.2
500	atf_check -s exit:0 -o ignore \
501		jexec one ping -c 1 -s 1200 198.51.100.2
502	atf_check -s exit:0 -o ignore \
503		jexec one ping -c 1 -s 2000 198.51.100.2
504
505	# Higher MTU on the tunnel than on the underlying interface
506	jexec one ifconfig ${epair}a mtu 1000
507	jexec two ifconfig ${epair}b mtu 1000
508
509	atf_check -s exit:0 -o ignore \
510		jexec one ping -c 1 -s 1200 198.51.100.2
511	atf_check -s exit:0 -o ignore \
512		jexec one ping -c 1 -s 2000 198.51.100.2
513}
514
515gif_cleanup()
516{
517	vnet_cleanup
518}
519
520atf_test_case "mtu" "cleanup"
521mtu_head()
522{
523	atf_set descr 'Bridge MTU changes'
524	atf_set require.user root
525}
526
527get_mtu()
528{
529	intf=$1
530
531	ifconfig ${intf} ether | awk '$5 == "mtu" { print $6 }'
532}
533
534check_mtu()
535{
536	intf=$1
537	expected=$2
538
539	mtu=$(get_mtu $intf)
540	if [ $mtu -ne $expected ];
541	then
542		atf_fail "Expected MTU of $expected on $intf but found $mtu"
543	fi
544}
545
546mtu_body()
547{
548	vnet_init
549
550	epair=$(vnet_mkepair)
551	gif=$(ifconfig gif create)
552	echo ${gif} >> created_interfaces.lst
553	bridge=$(vnet_mkbridge)
554
555	atf_check -s exit:0 \
556		ifconfig ${bridge} addm ${epair}a
557
558	ifconfig ${gif} mtu 1500
559	atf_check -s exit:0 \
560		ifconfig ${bridge} addm ${gif}
561
562	# Changing MTU changes it for all member interfaces
563	atf_check -s exit:0 \
564		ifconfig ${bridge} mtu 2000
565
566	check_mtu ${bridge} 2000
567	check_mtu ${gif} 2000
568	check_mtu ${epair}a 2000
569
570	# Rejected MTUs mean none of the MTUs change
571	atf_check -s exit:1 -e ignore \
572		ifconfig ${bridge} mtu 9000
573
574	check_mtu ${bridge} 2000
575	check_mtu ${gif} 2000
576	check_mtu ${epair}a 2000
577
578	# We're not allowed to change the MTU of a member interface
579	atf_check -s exit:1 -e ignore \
580		ifconfig ${epair}a mtu 1900
581	check_mtu ${epair}a 2000
582
583	# Test adding an interface with a different MTU
584	new_epair=$(vnet_mkepair)
585	check_mtu ${new_epair}a 1500
586	atf_check -s exit:0 -e ignore \
587		ifconfig ${bridge} addm ${new_epair}a
588
589	check_mtu ${bridge} 2000
590	check_mtu ${gif} 2000
591	check_mtu ${epair}a 2000
592	check_mtu ${new_epair}a 2000
593}
594
595mtu_cleanup()
596{
597	vnet_cleanup
598}
599
600atf_test_case "vlan" "cleanup"
601vlan_head()
602{
603	atf_set descr 'Ensure the bridge takes vlan ID into account, PR#270559'
604	atf_set require.user root
605}
606
607vlan_body()
608{
609	vnet_init
610
611	vid=1
612
613	epaira=$(vnet_mkepair)
614	epairb=$(vnet_mkepair)
615
616	br=$(vnet_mkbridge)
617
618	vnet_mkjail one ${epaira}b
619	vnet_mkjail two ${epairb}b
620
621	ifconfig ${br} up
622	ifconfig ${epaira}a up
623	ifconfig ${epairb}a up
624	ifconfig ${br} addm ${epaira}a addm ${epairb}a
625
626	jexec one ifconfig ${epaira}b up
627	jexec one ifconfig ${epaira}b.${vid} create
628
629	jexec two ifconfig ${epairb}b up
630	jexec two ifconfig ${epairb}b.${vid} create
631
632	# Create a MAC address conflict between an untagged and tagged interface
633	jexec two ifconfig ${epairb}b.${vid} ether 02:05:6e:06:28:1a
634	jexec one ifconfig ${epaira}b ether 02:05:6e:06:28:1a
635	jexec one ifconfig ${epaira}b.${vid} ether 02:05:6e:06:28:1b
636
637	# Add ip address, will also populate $br's fowarding table, by ARP announcement
638	jexec one ifconfig ${epaira}b.${vid} 192.0.2.1/24 up
639	jexec two ifconfig ${epairb}b.${vid} 192.0.2.2/24 up
640
641	sleep 0.5
642
643	ifconfig ${br}
644	jexec one ifconfig
645	jexec two ifconfig
646	ifconfig ${br} addr
647
648	atf_check -s exit:0 -o ignore \
649	    jexec one ping -c 1 -t 1 192.0.2.2
650
651	# This will trigger a mac flap (by ARP announcement)
652	jexec one ifconfig ${epaira}b 192.0.2.1/24 up
653
654	sleep 0.5
655
656	ifconfig ${br} addr
657
658	atf_check -s exit:0 -o ignore \
659	    jexec one ping -c 1 -t 1 192.0.2.2
660}
661
662vlan_cleanup()
663{
664	vnet_cleanup
665}
666
667atf_init_test_cases()
668{
669	atf_add_test_case "bridge_transmit_ipv4_unicast"
670	atf_add_test_case "stp"
671	atf_add_test_case "stp_vlan"
672	atf_add_test_case "static"
673	atf_add_test_case "span"
674	atf_add_test_case "inherit_mac"
675	atf_add_test_case "delete_with_members"
676	atf_add_test_case "mac_conflict"
677	atf_add_test_case "stp_validation"
678	atf_add_test_case "gif"
679	atf_add_test_case "mtu"
680	atf_add_test_case "vlan"
681}
682