xref: /freebsd/tests/sys/net/if_bridge_test.sh (revision 5f4c09dd85bff675e0ca63c55ea3c517e0fddfcc)
1#
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2020 The FreeBSD Foundation
5#
6# This software was developed by Kristof Provost under sponsorship
7# from the FreeBSD Foundation.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28# SUCH DAMAGE.
29
30. $(atf_get_srcdir)/../common/vnet.subr
31
32atf_test_case "bridge_transmit_ipv4_unicast" "cleanup"
33bridge_transmit_ipv4_unicast_head()
34{
35	atf_set descr 'bridge_transmit_ipv4_unicast bridging test'
36	atf_set require.user root
37}
38
39bridge_transmit_ipv4_unicast_body()
40{
41	vnet_init
42
43	epair_alcatraz=$(vnet_mkepair)
44	epair_singsing=$(vnet_mkepair)
45
46	vnet_mkjail alcatraz ${epair_alcatraz}b
47	vnet_mkjail singsing ${epair_singsing}b
48
49	jexec alcatraz ifconfig ${epair_alcatraz}b 192.0.2.1/24 up
50	jexec singsing ifconfig ${epair_singsing}b 192.0.2.2/24 up
51
52	bridge=$(vnet_mkbridge)
53
54	ifconfig ${bridge} up
55	ifconfig ${epair_alcatraz}a up
56	ifconfig ${epair_singsing}a up
57	ifconfig ${bridge} addm ${epair_alcatraz}a
58	ifconfig ${bridge} addm ${epair_singsing}a
59
60	atf_check -s exit:0 -o ignore jexec alcatraz ping -c 3 -t 1 192.0.2.2
61	atf_check -s exit:0 -o ignore jexec singsing ping -c 3 -t 1 192.0.2.1
62}
63
64bridge_transmit_ipv4_unicast_cleanup()
65{
66	vnet_cleanup
67}
68
69atf_test_case "stp" "cleanup"
70stp_head()
71{
72	atf_set descr 'Spanning tree test'
73	atf_set require.user root
74}
75
76stp_body()
77{
78	vnet_init
79
80	epair_one=$(vnet_mkepair)
81	epair_two=$(vnet_mkepair)
82	bridge_a=$(vnet_mkbridge)
83	bridge_b=$(vnet_mkbridge)
84
85	vnet_mkjail a ${bridge_a} ${epair_one}a ${epair_two}a
86	vnet_mkjail b ${bridge_b} ${epair_one}b ${epair_two}b
87
88	jexec a ifconfig ${epair_one}a up
89	jexec a ifconfig ${epair_two}a up
90	jexec a ifconfig ${bridge_a} addm ${epair_one}a
91	jexec a ifconfig ${bridge_a} addm ${epair_two}a
92
93	jexec b ifconfig ${epair_one}b up
94	jexec b ifconfig ${epair_two}b up
95	jexec b ifconfig ${bridge_b} addm ${epair_one}b
96	jexec b ifconfig ${bridge_b} addm ${epair_two}b
97
98	jexec a ifconfig ${bridge_a} 192.0.2.1/24
99
100	# Enable spanning tree
101	jexec a ifconfig ${bridge_a} stp ${epair_one}a
102	jexec a ifconfig ${bridge_a} stp ${epair_two}a
103	jexec b ifconfig ${bridge_b} stp ${epair_one}b
104	jexec b ifconfig ${bridge_b} stp ${epair_two}b
105
106	jexec b ifconfig ${bridge_b} up
107	jexec a ifconfig ${bridge_a} up
108
109	# Give STP time to do its thing
110	sleep 5
111
112	a_discard=$(jexec a ifconfig ${bridge_a} | grep discarding)
113	b_discard=$(jexec b ifconfig ${bridge_b} | grep discarding)
114
115	if [ -z "${a_discard}" ] && [ -z "${b_discard}" ]
116	then
117		atf_fail "STP failed to detect bridging loop"
118	fi
119
120	# We must also have at least some forwarding interfaces
121	a_forwarding=$(jexec a ifconfig ${bridge_a} | grep forwarding)
122	b_forwarding=$(jexec b ifconfig ${bridge_b} | grep forwarding)
123
124	if [ -z "${a_forwarding}" ] && [ -z "${b_forwarding}" ]
125	then
126		atf_fail "STP failed to detect bridging loop"
127	fi
128}
129
130stp_cleanup()
131{
132	vnet_cleanup
133}
134
135atf_test_case "stp_vlan" "cleanup"
136stp_vlan_head()
137{
138	atf_set descr 'Spanning tree on VLAN test'
139	atf_set require.user root
140}
141
142stp_vlan_body()
143{
144	vnet_init
145
146	epair_one=$(vnet_mkepair)
147	epair_two=$(vnet_mkepair)
148	bridge_a=$(vnet_mkbridge)
149	bridge_b=$(vnet_mkbridge)
150
151	vnet_mkjail a ${bridge_a} ${epair_one}a ${epair_two}a
152	vnet_mkjail b ${bridge_b} ${epair_one}b ${epair_two}b
153
154	jexec a ifconfig ${epair_one}a up
155	jexec a ifconfig ${epair_two}a up
156	vlan_a_one=$(jexec a ifconfig vlan create vlandev ${epair_one}a vlan 42)
157	vlan_a_two=$(jexec a ifconfig vlan create vlandev ${epair_two}a vlan 42)
158	jexec a ifconfig ${vlan_a_one} up
159	jexec a ifconfig ${vlan_a_two} up
160	jexec a ifconfig ${bridge_a} addm ${vlan_a_one}
161	jexec a ifconfig ${bridge_a} addm ${vlan_a_two}
162
163	jexec b ifconfig ${epair_one}b up
164	jexec b ifconfig ${epair_two}b up
165	vlan_b_one=$(jexec b ifconfig vlan create vlandev ${epair_one}b vlan 42)
166	vlan_b_two=$(jexec b ifconfig vlan create vlandev ${epair_two}b vlan 42)
167	jexec b ifconfig ${vlan_b_one} up
168	jexec b ifconfig ${vlan_b_two} up
169	jexec b ifconfig ${bridge_b} addm ${vlan_b_one}
170	jexec b ifconfig ${bridge_b} addm ${vlan_b_two}
171
172	jexec a ifconfig ${bridge_a} 192.0.2.1/24
173
174	# Enable spanning tree
175	jexec a ifconfig ${bridge_a} stp ${vlan_a_one}
176	jexec a ifconfig ${bridge_a} stp ${vlan_a_two}
177	jexec b ifconfig ${bridge_b} stp ${vlan_b_one}
178	jexec b ifconfig ${bridge_b} stp ${vlan_b_two}
179
180	jexec b ifconfig ${bridge_b} up
181	jexec a ifconfig ${bridge_a} up
182
183	# Give STP time to do its thing
184	sleep 5
185
186	a_discard=$(jexec a ifconfig ${bridge_a} | grep discarding)
187	b_discard=$(jexec b ifconfig ${bridge_b} | grep discarding)
188
189	if [ -z "${a_discard}" ] && [ -z "${b_discard}" ]
190	then
191		atf_fail "STP failed to detect bridging loop"
192	fi
193
194	# We must also have at least some forwarding interfaces
195	a_forwarding=$(jexec a ifconfig ${bridge_a} | grep forwarding)
196	b_forwarding=$(jexec b ifconfig ${bridge_b} | grep forwarding)
197
198	if [ -z "${a_forwarding}" ] && [ -z "${b_forwarding}" ]
199	then
200		atf_fail "STP failed to detect bridging loop"
201	fi
202}
203
204stp_vlan_cleanup()
205{
206	vnet_cleanup
207}
208
209atf_test_case "static" "cleanup"
210static_head()
211{
212	atf_set descr 'Bridge static address test'
213	atf_set require.user root
214}
215
216static_body()
217{
218	vnet_init
219
220	epair=$(vnet_mkepair)
221	bridge=$(vnet_mkbridge)
222
223	vnet_mkjail one ${bridge} ${epair}a
224
225	ifconfig ${epair}b up
226
227	jexec one ifconfig ${bridge} up
228	jexec one ifconfig ${epair}a up
229	jexec one ifconfig ${bridge} addm ${epair}a
230
231	# Wrong interface
232	atf_check -s exit:1 -o ignore -e ignore \
233	    jexec one ifconfig ${bridge} static ${epair}b 00:01:02:03:04:05
234
235	# Bad address format
236	atf_check -s exit:1 -o ignore -e ignore \
237	    jexec one ifconfig ${bridge} static ${epair}a 00:01:02:03:04
238
239	# Correct add
240	atf_check -s exit:0 -o ignore \
241	    jexec one ifconfig ${bridge} static ${epair}a 00:01:02:03:04:05
242
243	# List addresses
244	atf_check -s exit:0 -o ignore \
245	    jexec one ifconfig ${bridge} addr
246
247	# Delete with bad address format
248	atf_check -s exit:1 -o ignore -e ignore \
249	    jexec one ifconfig ${bridge} deladdr 00:01:02:03:04
250
251	# Delete with unlisted address
252	atf_check -s exit:1 -o ignore -e ignore \
253	    jexec one ifconfig ${bridge} deladdr 00:01:02:03:04:06
254
255	# Correct delete
256	atf_check -s exit:0 -o ignore \
257	    jexec one ifconfig ${bridge} deladdr 00:01:02:03:04:05
258}
259
260static_cleanup()
261{
262	vnet_cleanup
263}
264
265atf_test_case "span" "cleanup"
266span_head()
267{
268	atf_set descr 'Bridge span test'
269	atf_set require.user root
270	atf_set require.progs scapy
271}
272
273span_body()
274{
275	vnet_init
276
277	epair=$(vnet_mkepair)
278	epair_span=$(vnet_mkepair)
279	bridge=$(vnet_mkbridge)
280
281	vnet_mkjail one ${bridge} ${epair}a ${epair_span}a
282
283	ifconfig ${epair}b up
284	ifconfig ${epair_span}b up
285
286	jexec one ifconfig ${bridge} up
287	jexec one ifconfig ${epair}a up
288	jexec one ifconfig ${epair_span}a up
289	jexec one ifconfig ${bridge} addm ${epair}a
290
291	jexec one ifconfig ${bridge} span ${epair_span}a
292	jexec one ifconfig ${bridge} 192.0.2.1/24
293
294	# Send some traffic through the span
295	jexec one ping -c 1 -t 1 192.0.2.2
296
297	# Check that we see the traffic on the span interface
298	atf_check -s exit:0 \
299		$(atf_get_srcdir)/../netpfil/common/pft_ping.py \
300		--sendif ${epair}b \
301		--to 192.0.2.2 \
302		--recvif ${epair_span}b
303
304	jexec one ifconfig ${bridge} -span ${epair_span}a
305
306	# And no more traffic after we remove the span
307	atf_check -s exit:1 \
308		$(atf_get_srcdir)/../netpfil/common/pft_ping.py \
309		--sendif ${epair}b \
310		--to 192.0.2.2 \
311		--recvif ${epair_span}b
312}
313
314span_cleanup()
315{
316	vnet_cleanup
317}
318
319atf_test_case "delete_with_members" "cleanup"
320delete_with_members_head()
321{
322	atf_set descr 'Delete a bridge which still has member interfaces'
323	atf_set require.user root
324}
325
326delete_with_members_body()
327{
328	vnet_init
329
330	bridge=$(vnet_mkbridge)
331	epair=$(vnet_mkepair)
332
333	ifconfig ${bridge} 192.0.2.1/24 up
334	ifconfig ${epair}a up
335	ifconfig ${bridge} addm ${epair}a
336
337	ifconfig ${bridge} destroy
338}
339
340delete_with_members_cleanup()
341{
342	vnet_cleanup
343}
344
345atf_test_case "mac_conflict" "cleanup"
346mac_conflict_head()
347{
348	atf_set descr 'Ensure that bridges in different jails get different mac addresses'
349	atf_set require.user root
350}
351
352mac_conflict_body()
353{
354	vnet_init
355
356	epair=$(vnet_mkepair)
357
358	# Ensure the bridge module is loaded so jails can use it.
359	tmpbridge=$(vnet_mkbridge)
360
361	vnet_mkjail bridge_mac_conflict_one ${epair}a
362	vnet_mkjail bridge_mac_conflict_two ${epair}b
363
364	jexec bridge_mac_conflict_one ifconfig bridge create
365	jexec bridge_mac_conflict_one ifconfig bridge0 192.0.2.1/24 up \
366	    addm ${epair}a
367	jexec bridge_mac_conflict_one ifconfig ${epair}a up
368
369	jexec bridge_mac_conflict_two ifconfig bridge create
370	jexec bridge_mac_conflict_two ifconfig bridge0 192.0.2.2/24 up \
371	    addm ${epair}b
372	jexec bridge_mac_conflict_two ifconfig ${epair}b up
373
374	atf_check -s exit:0 -o ignore \
375	    jexec bridge_mac_conflict_one ping -c 3 192.0.2.2
376}
377
378mac_conflict_cleanup()
379{
380	vnet_cleanup
381}
382
383atf_test_case "inherit_mac" "cleanup"
384inherit_mac_head()
385{
386	atf_set descr 'Bridge inherit_mac test, #216510'
387	atf_set require.user root
388}
389
390inherit_mac_body()
391{
392	vnet_init
393
394	bridge=$(vnet_mkbridge)
395	epair=$(vnet_mkepair)
396	vnet_mkjail one ${bridge} ${epair}a
397
398	jexec one sysctl net.link.bridge.inherit_mac=1
399
400	# Attempt to provoke the panic described in #216510
401	jexec one ifconfig ${bridge} 192.0.0.1/24 up
402	jexec one ifconfig ${bridge} addm ${epair}a
403}
404
405inherit_mac_cleanup()
406{
407	vnet_cleanup
408}
409
410atf_test_case "stp_validation" "cleanup"
411stp_validation_head()
412{
413	atf_set descr 'Check STP validation'
414	atf_set require.user root
415	atf_set require.progs scapy
416}
417
418stp_validation_body()
419{
420	vnet_init
421
422	epair_one=$(vnet_mkepair)
423	epair_two=$(vnet_mkepair)
424	bridge=$(vnet_mkbridge)
425
426	ifconfig ${bridge} up
427	ifconfig ${bridge} addm ${epair_one}a addm ${epair_two}a
428	ifconfig ${bridge} stp ${epair_one}a stp ${epair_two}a
429
430	ifconfig ${epair_one}a up
431	ifconfig ${epair_one}b up
432	ifconfig ${epair_two}a up
433	ifconfig ${epair_two}b up
434
435	# Wait until the interfaces are no longer discarding
436	while ifconfig ${bridge} | grep 'state discarding' >/dev/null
437	do
438		sleep 1
439	done
440
441	# Now inject invalid STP BPDUs on epair_one and see if they're repeated
442	# on epair_two
443	atf_check -s exit:0 \
444	    $(atf_get_srcdir)/stp.py \
445	    --sendif ${epair_one}b \
446	    --recvif ${epair_two}b
447}
448
449stp_validation_cleanup()
450{
451	vnet_cleanup
452}
453
454atf_test_case "gif" "cleanup"
455gif_head()
456{
457	atf_set descr 'gif as a bridge member'
458	atf_set require.user root
459}
460
461gif_body()
462{
463	vnet_init
464
465	epair=$(vnet_mkepair)
466
467	vnet_mkjail one ${epair}a
468	vnet_mkjail two ${epair}b
469
470	jexec one sysctl net.link.gif.max_nesting=2
471	jexec two sysctl net.link.gif.max_nesting=2
472
473	jexec one ifconfig ${epair}a 192.0.2.1/24 up
474	jexec two ifconfig ${epair}b 192.0.2.2/24 up
475
476	# Tunnel
477	gif_one=$(jexec one ifconfig gif create)
478	gif_two=$(jexec two ifconfig gif create)
479
480	jexec one ifconfig ${gif_one} tunnel 192.0.2.1 192.0.2.2
481	jexec one ifconfig ${gif_one} up
482	jexec two ifconfig ${gif_two} tunnel 192.0.2.2 192.0.2.1
483	jexec two ifconfig ${gif_two} up
484
485	bridge_one=$(jexec one ifconfig bridge create)
486	bridge_two=$(jexec two ifconfig bridge create)
487	jexec one ifconfig ${bridge_one} 198.51.100.1/24 up
488	jexec one ifconfig ${bridge_one} addm ${gif_one}
489	jexec two ifconfig ${bridge_two} 198.51.100.2/24 up
490	jexec two ifconfig ${bridge_two} addm ${gif_two}
491
492	# Sanity check
493	atf_check -s exit:0 -o ignore \
494		jexec one ping -c 1 192.0.2.2
495
496	# Test tunnel
497	atf_check -s exit:0 -o ignore \
498		jexec one ping -c 1 198.51.100.2
499	atf_check -s exit:0 -o ignore \
500		jexec one ping -c 1 -s 1200 198.51.100.2
501	atf_check -s exit:0 -o ignore \
502		jexec one ping -c 1 -s 2000 198.51.100.2
503
504	# Higher MTU on the tunnel than on the underlying interface
505	jexec one ifconfig ${epair}a mtu 1000
506	jexec two ifconfig ${epair}b mtu 1000
507
508	atf_check -s exit:0 -o ignore \
509		jexec one ping -c 1 -s 1200 198.51.100.2
510	atf_check -s exit:0 -o ignore \
511		jexec one ping -c 1 -s 2000 198.51.100.2
512}
513
514gif_cleanup()
515{
516	vnet_cleanup
517}
518
519atf_test_case "mtu" "cleanup"
520mtu_head()
521{
522	atf_set descr 'Bridge MTU changes'
523	atf_set require.user root
524}
525
526get_mtu()
527{
528	intf=$1
529
530	ifconfig ${intf} ether | awk '$5 == "mtu" { print $6 }'
531}
532
533check_mtu()
534{
535	intf=$1
536	expected=$2
537
538	mtu=$(get_mtu $intf)
539	if [ $mtu -ne $expected ];
540	then
541		atf_fail "Expected MTU of $expected on $intf but found $mtu"
542	fi
543}
544
545mtu_body()
546{
547	vnet_init
548
549	epair=$(vnet_mkepair)
550	gif=$(ifconfig gif create)
551	echo ${gif} >> created_interfaces.lst
552	bridge=$(vnet_mkbridge)
553
554	atf_check -s exit:0 \
555		ifconfig ${bridge} addm ${epair}a
556
557	ifconfig ${gif} mtu 1500
558	atf_check -s exit:0 \
559		ifconfig ${bridge} addm ${gif}
560
561	# Changing MTU changes it for all member interfaces
562	atf_check -s exit:0 \
563		ifconfig ${bridge} mtu 2000
564
565	check_mtu ${bridge} 2000
566	check_mtu ${gif} 2000
567	check_mtu ${epair}a 2000
568
569	# Rejected MTUs mean none of the MTUs change
570	atf_check -s exit:1 -e ignore \
571		ifconfig ${bridge} mtu 9000
572
573	check_mtu ${bridge} 2000
574	check_mtu ${gif} 2000
575	check_mtu ${epair}a 2000
576
577	# We're not allowed to change the MTU of a member interface
578	atf_check -s exit:1 -e ignore \
579		ifconfig ${epair}a mtu 1900
580	check_mtu ${epair}a 2000
581
582	# Test adding an interface with a different MTU
583	new_epair=$(vnet_mkepair)
584	check_mtu ${new_epair}a 1500
585	atf_check -s exit:0 -e ignore \
586		ifconfig ${bridge} addm ${new_epair}a
587
588	check_mtu ${bridge} 2000
589	check_mtu ${gif} 2000
590	check_mtu ${epair}a 2000
591	check_mtu ${new_epair}a 2000
592}
593
594mtu_cleanup()
595{
596	vnet_cleanup
597}
598
599atf_test_case "vlan" "cleanup"
600vlan_head()
601{
602	atf_set descr 'Ensure the bridge takes vlan ID into account, PR#270559'
603	atf_set require.user root
604}
605
606vlan_body()
607{
608	vnet_init
609
610	vid=1
611
612	epaira=$(vnet_mkepair)
613	epairb=$(vnet_mkepair)
614
615	br=$(vnet_mkbridge)
616
617	vnet_mkjail one ${epaira}b
618	vnet_mkjail two ${epairb}b
619
620	ifconfig ${br} up
621	ifconfig ${epaira}a up
622	ifconfig ${epairb}a up
623	ifconfig ${br} addm ${epaira}a addm ${epairb}a
624
625	jexec one ifconfig ${epaira}b up
626	jexec one ifconfig ${epaira}b.${vid} create
627
628	jexec two ifconfig ${epairb}b up
629	jexec two ifconfig ${epairb}b.${vid} create
630
631	# Create a MAC address conflict between an untagged and tagged interface
632	jexec two ifconfig ${epairb}b.${vid} ether 02:05:6e:06:28:1a
633	jexec one ifconfig ${epaira}b ether 02:05:6e:06:28:1a
634	jexec one ifconfig ${epaira}b.${vid} ether 02:05:6e:06:28:1b
635
636	# Add ip address, will also populate $br's fowarding table, by ARP announcement
637	jexec one ifconfig ${epaira}b.${vid} 192.0.2.1/24 up
638	jexec two ifconfig ${epairb}b.${vid} 192.0.2.2/24 up
639
640	sleep 0.5
641
642	ifconfig ${br}
643	jexec one ifconfig
644	jexec two ifconfig
645	ifconfig ${br} addr
646
647	atf_check -s exit:0 -o ignore \
648	    jexec one ping -c 1 -t 1 192.0.2.2
649
650	# This will trigger a mac flap (by ARP announcement)
651	jexec one ifconfig ${epaira}b 192.0.2.1/24 up
652
653	sleep 0.5
654
655	ifconfig ${br} addr
656
657	atf_check -s exit:0 -o ignore \
658	    jexec one ping -c 1 -t 1 192.0.2.2
659}
660
661vlan_cleanup()
662{
663	vnet_cleanup
664}
665
666atf_init_test_cases()
667{
668	atf_add_test_case "bridge_transmit_ipv4_unicast"
669	atf_add_test_case "stp"
670	atf_add_test_case "stp_vlan"
671	atf_add_test_case "static"
672	atf_add_test_case "span"
673	atf_add_test_case "inherit_mac"
674	atf_add_test_case "delete_with_members"
675	atf_add_test_case "mac_conflict"
676	atf_add_test_case "stp_validation"
677	atf_add_test_case "gif"
678	atf_add_test_case "mtu"
679	atf_add_test_case "vlan"
680}
681