#! /bin/sh

_ETHER=`route -n get default 2>/dev/null |awk '/interface:/ {print $2}'`
_ETHER=${_ETHER:=trunk0}
_BRIDGE=bridge0

# Let the environment over-ride this
[ "$BRIDGE" ] || BRIDGE=${_BRIDGE}
[ "$ETHER" ] || ETHER=${_ETHER}

if test `id -u` -ne 0; then
	DOAS=doas
fi

echo -n " {$1 ($BRIDGE <-> $ETHER)"

# Set up our bridge
$DOAS ifconfig $1 group tap > /dev/null 2>&1
$DOAS ifconfig $BRIDGE create > /dev/null 2>&1 && {
  # Only add rules if the bridge creation succeeds; otherwise
  # duplicate rules get loaded each time qemu starts
  # The following two block carp packets from wasting cpu cycles inside the
  # qemu sessions, remove if testing carp inside qemu
  $DOAS ifconfig $BRIDGE rule block in on $ETHER dst 33:33:0:0:0:12
  $DOAS ifconfig $BRIDGE rule block in on $ETHER dst 01:00:5e:00:00:12
}
# Since we can specify ETHER and BRIDGE above, its possible that
# this tap interface or this physical interface was setup as part of
# a different bridge earlier, and that is never cleaned up, so we have
# to cleanup here first before we set it up; a physical interface cannot
# be member to more than one bridge, thankfully, or I never would have
# caught this
ifconfig bridge | sed -n '/^bridge[0-9]*/{s/:.*$//;p;}' | while read brif
do
	$DOAS ifconfig $brif del $ETHER > /dev/null 2>&1
	$DOAS ifconfig $brif del $1 > /dev/null 2>&1
done
$DOAS ifconfig $BRIDGE add $ETHER up
$DOAS ifconfig $BRIDGE add $1 up || true
echo "}"
