#!/bin/sh #- # SPDX-License-Identifier: BSD-2-Clause # # Copyright (c) 2022 Bjoern A. Zeeb # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. set -e set -x # Whatever you compiled socket.c to. ABSOLUTE PATH. STESTBIN=`pwd`/socket PORT=7 # echo; not that we do that # First start two vnet jails so we have a network stack to mess with # without interfering with anyone. js=`jail -i -c -n jl host.hostname=server.example.net vnet persist children.max=1` jb=`jail -i -c -n jr host.hostname=base.example.net vnet persist children.max=1` # Create loopback IPs in each vnet jail for jid in ${js} ${jb}; do jexec ${jid} ifconfig lo0 inet 127.0.0.1/8 alias up jexec ${jid} ifconfig lo0 inet6 ::1/128 alias done # Create an epair connecting the two machines (vnet jails). ep=`ifconfig epair create | sed -e 's/a$//'` # Add one end to each vnet jail. ifconfig ${ep}a vnet ${js} ifconfig ${ep}b vnet ${jb} # Add an IP address on the "server" jail jexec ${js} ifconfig ${ep}a inet 192.0.2.1/24 # Add an IP address on the "base" jail jexec ${jb} ifconfig ${ep}b inet 192.0.2.2/24 # Start an listener on the server jail. # Note output may go to our terminal. jexec ${js} ${STESTBIN} 192.0.2.1 ${PORT} & # Now start a plain-old IP-jail under the "base" jail sharing the base system address. jsj=`jexec ${js} jail -i -c -n jsj host.hostname=jails.example.net ip4.addr=192.0.2.1 persist` # Now start a second listener inside the jail on the server machine. jexec ${jsj} ${STESTBIN} 192.0.2.1 ${PORT} & echo "Listing listening connections from the server (base) system" jexec ${js} netstat -an # Now start a plain-old IP-jail under the "base" jail sharing the base system address. jbj=`jexec ${jb} jail -i -c -n jbj host.hostname=jailb.example.net ip4.addr=192.0.2.2 persist` # Now connect 1 client from base to server. sleep 1 echo "Starting connection from base jail" jexec ${jb} ${STESTBIN} 192.0.2.2 12345 192.0.2.1 ${PORT} & # Now connect 1 client from the plain-old IP jail. sleep 1 echo "Starting connection from plain-old IP jail" jexec ${jbj} ${STESTBIN} 192.0.2.2 12345 192.0.2.1 ${PORT} & # List these connections. sleep 1 echo "Listing server connections from the server (base) system" jexec ${js} netstat -an echo "Listing client connections from the base system" jexec ${jb} netstat -an # Wait for the last client to exit (currently hardcoded to 60s) sleep 60 # Clear up jail -r ${jbj} jail -r ${jsj} jail -r ${jb} jail -r ${js} ifconfig ${ep}a destroy # end