2020-08-19 08:46:04 +00:00
|
|
|
#!/bin/sh
|
|
|
|
# Based on alpine's tunnel configuration script.
|
|
|
|
# Copyright (c) 2017 Kaarle Ritvanen
|
2020-08-20 08:09:01 +00:00
|
|
|
# Copyright (c) 2020 Ariadne Conill (extended for ifupdown-ng)
|
2021-03-18 19:20:01 +00:00
|
|
|
# Copyright (c) 2021 Maximilian Wilhelm (make sure mode/type is 1st parameter)
|
2020-08-20 08:09:01 +00:00
|
|
|
|
2021-06-03 16:52:59 +00:00
|
|
|
[ -z "$IF_TUNNEL_LOCAL" -a -z "$IF_TUNNEL_LOCAL_DEV" ] && exit 1
|
2020-08-20 08:09:01 +00:00
|
|
|
[ -z "$IF_TUNNEL_REMOTE" ] && exit 1
|
|
|
|
[ -z "$IF_TUNNEL_MODE" ] && exit 1
|
|
|
|
|
2021-03-18 19:20:01 +00:00
|
|
|
[ -n "$VERBOSE" ] && set -x
|
|
|
|
|
|
|
|
# Figure out address familiy
|
2021-06-03 16:52:59 +00:00
|
|
|
FAMILY="4"
|
2020-08-20 08:09:01 +00:00
|
|
|
|
|
|
|
case "$IF_TUNNEL_MODE" in
|
|
|
|
vti6|ipip6|ip6*)
|
2021-06-03 16:52:59 +00:00
|
|
|
FAMILY="6"
|
2020-08-20 08:09:01 +00:00
|
|
|
;;
|
|
|
|
esac
|
2020-08-19 08:46:04 +00:00
|
|
|
|
2021-03-18 19:20:01 +00:00
|
|
|
# Figure out object type - gretap tunnels have to create using ip link
|
|
|
|
# and therefor require 'type' keyword instead of 'mode'
|
|
|
|
OBJECT="tunnel"
|
|
|
|
TYPE_KW="mode"
|
2021-03-25 20:03:27 +00:00
|
|
|
case "${IF_TUNNEL_MODE}" in
|
|
|
|
*gretap)
|
2021-03-18 19:20:01 +00:00
|
|
|
OBJECT="link"
|
|
|
|
TYPE_KW="type"
|
|
|
|
|
2021-03-25 20:03:27 +00:00
|
|
|
# Strip possible "ip6" from tunnel mode
|
|
|
|
TUNNEL_MODE="gretap"
|
|
|
|
;;
|
|
|
|
|
|
|
|
*)
|
|
|
|
# Store tunnel type/mode separaltely and unset input variable to exclude it
|
|
|
|
# from PARAMS below
|
|
|
|
TUNNEL_MODE="$IF_TUNNEL_MODE"
|
|
|
|
unset IF_TUNNEL_MODE
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2021-03-18 19:20:01 +00:00
|
|
|
|
2021-06-03 16:52:59 +00:00
|
|
|
# If 'tunnel-local <IP>' was not given but 'tunnel-local-dev <iface>' is given try
|
|
|
|
# to figure out the IP of the underlay device (wrt the address family used for this
|
|
|
|
# tunnel) and use this to set up the tunnel
|
|
|
|
if [ ${PHASE} = "create" -a ! "${IF_TUNNEL_LOCAL}" -a "${IF_TUNNEL_LOCAL_DEV}" ]; then
|
|
|
|
if [ "${FAMILY}" = "4" ]; then
|
|
|
|
ip=$(ip -4 -brief addr show dev "${IF_TUNNEL_LOCAL_DEV}" 2>/dev/null | awk '{ print $3 }' | cut -d/ -f1)
|
|
|
|
|
|
|
|
# For IPv6 we try to use a non-temporary address (-> privacy extensions)
|
|
|
|
else
|
|
|
|
# Get all IPv6 addres configured on $IF_TUNNEL_LOCAL_DEV which are not
|
|
|
|
# temporary (due to privacy extensions). We do not filter for "mgmtaddr"
|
|
|
|
# "scope global" etc. as we don't want to make further assumptions on
|
|
|
|
# whether a user wants to use a link local address configured to this interface.
|
|
|
|
#
|
|
|
|
# The assumption that a temporary address configured by PE isn't suited
|
|
|
|
# to terminate a tunnel should hold in nearly all setups, I hope.
|
|
|
|
ip=$(ip -6 addr show dev "${IF_TUNNEL_LOCAL_DEV}" -temporary | grep inet6 | head -n1 | awk '{ print $2 }' | cut -d/ -f1)
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! "${ip}" ]; then
|
|
|
|
echo "Unable to determine the IPv${FAMILIY} address of tunnel-local-dev ${IF_TUNNEL_LOCAL_DEV}!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
unset IF_TUNNEL_LOCAL_DEV
|
|
|
|
export IF_TUNNEL_LOCAL="${ip}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2021-03-18 19:20:01 +00:00
|
|
|
# Mangle residual IF_TUNNEL_* params into single string
|
2020-08-19 08:46:04 +00:00
|
|
|
PARAMS=$(set | sed -E '
|
|
|
|
s/^IF_TUNNEL_([A-Z0-9_]+)=(.+)/\1\n\2/
|
|
|
|
ta
|
|
|
|
d
|
|
|
|
:a
|
|
|
|
h
|
|
|
|
y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
|
|
|
|
P
|
|
|
|
g
|
|
|
|
s/.*\n//
|
|
|
|
')
|
|
|
|
|
|
|
|
[ "$PARAMS" ] || exit 0
|
|
|
|
|
|
|
|
case "$PHASE" in
|
2020-09-09 22:48:35 +00:00
|
|
|
create)
|
2021-06-03 16:52:59 +00:00
|
|
|
${MOCK} eval ip -$FAMILY $OBJECT add $IFACE $TYPE_KW $TUNNEL_MODE $PARAMS
|
2020-08-19 08:46:04 +00:00
|
|
|
;;
|
2020-09-09 22:48:35 +00:00
|
|
|
destroy)
|
2021-06-03 16:52:59 +00:00
|
|
|
${MOCK} ip -$FAMILY $OBJECT del $IFACE
|
2020-08-19 08:46:04 +00:00
|
|
|
;;
|
|
|
|
depend)
|
2021-06-03 16:52:59 +00:00
|
|
|
echo "${IF_TUNNEL_DEV}" "${IF_TUNNEL_LOCAL_DEV}"
|
2020-08-19 08:46:04 +00:00
|
|
|
;;
|
|
|
|
esac
|