tapsetup: add capability to add addresses to bridge

This commit is contained in:
Martine S. Lenders 2020-09-02 10:33:01 +02:00
parent 0abe581e3e
commit dbeb783b13
No known key found for this signature in database
GPG Key ID: CCD317364F63286F

View File

@ -7,6 +7,7 @@ BRNAME="tapbr0"
TAPNAME="tap"
DEACTIVATE_IPV6=""
ENABLE_FORWARDING=0
DEFAULT_PREFIX_LEN=128
UPLINK=""
usage() {
@ -21,6 +22,9 @@ usage() {
echo " -l <iface>, --list <iface>: If <iface> belongs to a bridge, list the bridge and" >&2
echo " all interfaces that belong to it. If <iface> does " >&2
echo" not belong to a bridge, just print <iface>." >&2
echo " -a <address>[/<prefix_len>], --address <address>[/<prefix_len>]:" >&2
echo " Address to add to the created bridge. Can be used" >&2
echo " multiple times." >&2
echo " -f, --forwarding Enable forwarding system-wide on creation and " >&2
echo " disable on deletion." >&2
echo " -b <name>, --bridge <name>: Give name for the bridge (default: tapbr)" >&2
@ -63,6 +67,25 @@ activate_forwarding() {
fi
}
add_ipv6_addrs() {
for a in ${BRIDGE_ADDRS}; do
address_addr=$(echo "${a}" | cut -d/ -f1)
prefix_len=$(echo "${a}" | cut -d/ -f2)
if [ "${a}" = "${prefix_len}" ]; then
# prefix length is not defined
prefix_len=${DEFAULT_PREFIX_LEN}
fi
case "${PLATFORM}" in
FreeBSD|OSX)
ifconfig ${BRNAME} inet6 ${address_addr} prefixlen ${prefix_len} || exit 1
;;
Linux)
ip address add ${address_addr}/${prefix_len} dev ${BRNAME} || exit 1
;;
esac
done
}
create_bridge() {
echo "creating bridge ${BRNAME}"
@ -116,6 +139,25 @@ deactivate_forwarding() {
fi
}
del_ipv6_addrs() {
for a in ${BRIDGE_ADDRS}; do
address_addr=$(echo "${a}" | cut -d/ -f1)
prefix_len=$(echo "${a}" | cut -d/ -f2)
if [ "${a}" = "${prefix_len}" ]; then
# prefix length is not defined
prefix_len=${DEFAULT_PREFIX_LEN}
fi
case "${PLATFORM}" in
FreeBSD|OSX)
ifconfig ${BRNAME} inet6 ${address_addr} prefixlen ${prefix_len} delete || exit 1
;;
Linux)
ip address delete ${address_addr}/${prefix_len} dev ${BRNAME} || exit 1
;;
esac
done
}
delete_bridge() {
echo "deleting ${BRNAME}"
@ -231,6 +273,15 @@ while true ; do
-6)
DEACTIVATE_IPV6=1
shift ;;
-a|--address)
# check if valid address + optional prefix length
if echo "$2" | grep -q "^[a-f0-9:]\+\(/[0-9]\+\)\?$"; then
BRIDGE_ADDRS="${BRIDGE_ADDRS} $2"
shift 2
else
usage
exit 2
fi ;;
-b|--bridge)
case "$2" in
"")
@ -344,8 +395,10 @@ if [ "${COMMAND}" = 'create' ]; then
activate_forwarding || exit 1
up_bridge || exit 1
add_ipv6_addrs || exit 1
elif [ "${COMMAND}" = 'delete' ]; then
del_ipv6_addrs || exit 1
deactivate_forwarding || exit 1
delete_bridge
elif [ "${COMMAND}" = 'list' ]; then