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 122stp_cleanup() 123{ 124 vnet_cleanup 125} 126 127atf_test_case "static" "cleanup" 128static_head() 129{ 130 atf_set descr 'Bridge static address test' 131 atf_set require.user root 132} 133 134static_body() 135{ 136 vnet_init 137 138 epair=$(vnet_mkepair) 139 bridge=$(vnet_mkbridge) 140 141 vnet_mkjail one ${bridge} ${epair}a 142 143 ifconfig ${epair}b up 144 145 jexec one ifconfig ${bridge} up 146 jexec one ifconfig ${epair}a up 147 jexec one ifconfig ${bridge} addm ${epair}a 148 149 # Wrong interface 150 atf_check -s exit:1 -o ignore -e ignore \ 151 jexec one ifconfig ${bridge} static ${epair}b 00:01:02:03:04:05 152 153 # Bad address format 154 atf_check -s exit:1 -o ignore -e ignore \ 155 jexec one ifconfig ${bridge} static ${epair}a 00:01:02:03:04 156 157 # Correct add 158 atf_check -s exit:0 -o ignore \ 159 jexec one ifconfig ${bridge} static ${epair}a 00:01:02:03:04:05 160 161 # List addresses 162 atf_check -s exit:0 -o ignore \ 163 jexec one ifconfig ${bridge} addr 164 165 # Delete with bad address format 166 atf_check -s exit:1 -o ignore -e ignore \ 167 jexec one ifconfig ${bridge} deladdr 00:01:02:03:04 168 169 # Delete with unlisted address 170 atf_check -s exit:1 -o ignore -e ignore \ 171 jexec one ifconfig ${bridge} deladdr 00:01:02:03:04:06 172 173 # Correct delete 174 atf_check -s exit:0 -o ignore \ 175 jexec one ifconfig ${bridge} deladdr 00:01:02:03:04:05 176} 177 178static_cleanup() 179{ 180 vnet_cleanup 181} 182 183atf_test_case "span" "cleanup" 184span_head() 185{ 186 atf_set descr 'Bridge span test' 187 atf_set require.user root 188} 189 190span_body() 191{ 192 set -x 193 vnet_init 194 195 epair=$(vnet_mkepair) 196 epair_span=$(vnet_mkepair) 197 bridge=$(vnet_mkbridge) 198 199 vnet_mkjail one ${bridge} ${epair}a ${epair_span}a 200 201 ifconfig ${epair}b up 202 ifconfig ${epair_span}b up 203 204 jexec one ifconfig ${bridge} up 205 jexec one ifconfig ${epair}a up 206 jexec one ifconfig ${epair_span}a up 207 jexec one ifconfig ${bridge} addm ${epair}a 208 209 jexec one ifconfig ${bridge} span ${epair_span}a 210 jexec one ifconfig ${bridge} 192.0.2.1/24 211 212 # Send some traffic through the span 213 jexec one ping -c 1 -t 1 192.0.2.2 214 215 # Check that we see the traffic on the span interface 216 atf_check -s exit:0 \ 217 $(atf_get_srcdir)/../netpfil/common/pft_ping.py \ 218 --sendif ${epair}b \ 219 --to 192.0.2.2 \ 220 --recvif ${epair_span}b 221 222 jexec one ifconfig ${bridge} -span ${epair_span}a 223 224 # And no more traffic after we remove the span 225 atf_check -s exit:1 \ 226 $(atf_get_srcdir)/../netpfil/common/pft_ping.py \ 227 --sendif ${epair}b \ 228 --to 192.0.2.2 \ 229 --recvif ${epair_span}b 230} 231 232span_cleanup() 233{ 234 vnet_cleanup 235} 236 237atf_test_case "delete_with_members" "cleanup" 238delete_with_members_head() 239{ 240 atf_set descr 'Delete a bridge which still has member interfaces' 241 atf_set require.user root 242} 243 244delete_with_members_body() 245{ 246 vnet_init 247 248 bridge=$(vnet_mkbridge) 249 epair=$(vnet_mkepair) 250 251 ifconfig ${bridge} 192.0.2.1/24 up 252 ifconfig ${epair}a up 253 ifconfig ${bridge} addm ${epair}a 254 255 ifconfig ${bridge} destroy 256} 257 258delete_with_members_cleanup() 259{ 260 vnet_cleanup 261} 262 263atf_test_case "mac_conflict" "cleanup" 264mac_conflict_head() 265{ 266 atf_set descr 'Ensure that bridges in different jails get different mac addresses' 267 atf_set require.user root 268} 269 270mac_conflict_body() 271{ 272 vnet_init 273 274 epair=$(vnet_mkepair) 275 276 # Ensure the bridge module is loaded so jails can use it. 277 tmpbridge=$(vnet_mkbridge) 278 279 vnet_mkjail bridge_mac_conflict_one ${epair}a 280 vnet_mkjail bridge_mac_conflict_two ${epair}b 281 282 jexec bridge_mac_conflict_one ifconfig bridge create 283 jexec bridge_mac_conflict_one ifconfig bridge0 192.0.2.1/24 up \ 284 addm ${epair}a 285 jexec bridge_mac_conflict_one ifconfig ${epair}a up 286 287 jexec bridge_mac_conflict_two ifconfig bridge create 288 jexec bridge_mac_conflict_two ifconfig bridge0 192.0.2.2/24 up \ 289 addm ${epair}b 290 jexec bridge_mac_conflict_two ifconfig ${epair}b up 291 292 atf_check -s exit:0 -o ignore \ 293 jexec bridge_mac_conflict_one ping -c 3 192.0.2.2 294} 295 296mac_conflict_cleanup() 297{ 298 vnet_cleanup 299} 300 301atf_test_case "inherit_mac" "cleanup" 302inherit_mac_head() 303{ 304 atf_set descr 'Bridge inherit_mac test, #216510' 305 atf_set require.user root 306} 307 308inherit_mac_body() 309{ 310 vnet_init 311 312 bridge=$(vnet_mkbridge) 313 epair=$(vnet_mkepair) 314 vnet_mkjail one ${bridge} ${epair}a 315 316 jexec one sysctl net.link.bridge.inherit_mac=1 317 318 # Attempt to provoke the panic described in #216510 319 jexec one ifconfig ${bridge} 192.0.0.1/24 up 320 jexec one ifconfig ${bridge} addm ${epair}a 321} 322 323inherit_mac_cleanup() 324{ 325 vnet_cleanup 326} 327 328atf_init_test_cases() 329{ 330 atf_add_test_case "bridge_transmit_ipv4_unicast" 331 atf_add_test_case "stp" 332 atf_add_test_case "static" 333 atf_add_test_case "span" 334 atf_add_test_case "inherit_mac" 335 atf_add_test_case "delete_with_members" 336 atf_add_test_case "mac_conflict" 337} 338