| 1 |
#!/bin/sh |
| 2 |
# Based on alpine's tunnel configuration script. |
| 3 |
# Copyright (c) 2017 Kaarle Ritvanen |
| 4 |
# Copyright (c) 2020 Ariadne Conill (extended for ifupdown-ng) |
| 5 |
# Copyright (c) 2021 Maximilian Wilhelm (make sure mode/type is 1st parameter, add more options) |
| 6 |
|
| 7 |
[ -z "$IF_TUNNEL_LOCAL" -a -z "$IF_TUNNEL_LOCAL_DEV" ] && exit 1 |
| 8 |
[ -z "$IF_TUNNEL_REMOTE" ] && exit 1 |
| 9 |
[ -z "$IF_TUNNEL_MODE" ] && exit 1 |
| 10 |
|
| 11 |
[ -n "$VERBOSE" ] && set -x |
| 12 |
|
| 13 |
yesno() { |
| 14 |
case "$1" in |
| 15 |
yes|1) echo 1 ;; |
| 16 |
*) echo 0 ;; |
| 17 |
esac |
| 18 |
} |
| 19 |
|
| 20 |
# Figure out address familiy |
| 21 |
FAMILY="4" |
| 22 |
|
| 23 |
case "$IF_TUNNEL_MODE" in |
| 24 |
vti6|ipip6|ip6*) |
| 25 |
FAMILY="6" |
| 26 |
;; |
| 27 |
esac |
| 28 |
|
| 29 |
# Figure out object type - gretap tunnels have to create using ip link |
| 30 |
# and therefor require 'type' keyword instead of 'mode' |
| 31 |
OBJECT="tunnel" |
| 32 |
TYPE_KW="mode" |
| 33 |
case "${IF_TUNNEL_MODE}" in |
| 34 |
*gretap) |
| 35 |
OBJECT="link" |
| 36 |
TYPE_KW="type" |
| 37 |
|
| 38 |
# Strip possible "ip6" from tunnel mode |
| 39 |
TUNNEL_MODE="gretap" |
| 40 |
;; |
| 41 |
|
| 42 |
*) |
| 43 |
# Store tunnel type/mode separaltely and unset input variable to exclude it |
| 44 |
# from PARAMS below |
| 45 |
TUNNEL_MODE="$IF_TUNNEL_MODE" |
| 46 |
unset IF_TUNNEL_MODE |
| 47 |
;; |
| 48 |
esac |
| 49 |
|
| 50 |
|
| 51 |
# If 'tunnel-local <IP>' was not given but 'tunnel-local-dev <iface>' is given try |
| 52 |
# to figure out the IP of the underlay device (wrt the address family used for this |
| 53 |
# tunnel) and use this to set up the tunnel |
| 54 |
if [ ${PHASE} = "create" -a ! "${IF_TUNNEL_LOCAL}" -a "${IF_TUNNEL_LOCAL_DEV}" ]; then |
| 55 |
if [ "${FAMILY}" = "4" ]; then |
| 56 |
ip=$(ip -4 -brief addr show dev "${IF_TUNNEL_LOCAL_DEV}" 2>/dev/null | awk '{ print $3 }' | cut -d/ -f1) |
| 57 |
|
| 58 |
# For IPv6 we try to use a non-temporary address (-> privacy extensions) |
| 59 |
else |
| 60 |
# Get all IPv6 addres configured on $IF_TUNNEL_LOCAL_DEV which are not |
| 61 |
# temporary (due to privacy extensions). We do not filter for "mgmtaddr" |
| 62 |
# "scope global" etc. as we don't want to make further assumptions on |
| 63 |
# whether a user wants to use a link local address configured to this interface. |
| 64 |
# |
| 65 |
# The assumption that a temporary address configured by PE isn't suited |
| 66 |
# to terminate a tunnel should hold in nearly all setups, I hope. |
| 67 |
ip=$(ip -6 addr show dev "${IF_TUNNEL_LOCAL_DEV}" -temporary | grep inet6 | head -n1 | awk '{ print $2 }' | cut -d/ -f1) |
| 68 |
fi |
| 69 |
|
| 70 |
if [ ! "${ip}" ]; then |
| 71 |
echo "Unable to determine the IPv${FAMILIY} address of tunnel-local-dev ${IF_TUNNEL_LOCAL_DEV}!" |
| 72 |
exit 1 |
| 73 |
fi |
| 74 |
|
| 75 |
unset IF_TUNNEL_LOCAL_DEV |
| 76 |
export IF_TUNNEL_LOCAL="${ip}" |
| 77 |
fi |
| 78 |
|
| 79 |
|
| 80 |
# Handle boolean switches |
| 81 |
MORE_PARAMS="" |
| 82 |
if [ "${IF_TUNNEL_IGNORE_DF}" ]; then |
| 83 |
if $(yesno "${IF_TUNNEL_IGNORE_DF}"); then |
| 84 |
MORE_PARAMS="ignore-df" |
| 85 |
else |
| 86 |
MORE_PARAMS="noignore-df" |
| 87 |
fi |
| 88 |
|
| 89 |
unset IF_TUNNEL_IGNORE_DF |
| 90 |
fi |
| 91 |
|
| 92 |
if [ "${IF_TUNNEL_PMTUDISC}" ]; then |
| 93 |
if $(yesno "${IF_TUNNEL_PMTUDISC}"); then |
| 94 |
MORE_PARAMS="pmtudisc" |
| 95 |
else |
| 96 |
MORE_PARAMS="nopmtudisc" |
| 97 |
fi |
| 98 |
|
| 99 |
unset IF_TUNNEL_PMTUDISC |
| 100 |
fi |
| 101 |
|
| 102 |
|
| 103 |
# Mangle residual IF_TUNNEL_* params into single string |
| 104 |
PARAMS=$(set | sed -E ' |
| 105 |
s/^IF_TUNNEL_([A-Z0-9_]+)=(.+)/\1\n\2/ |
| 106 |
ta |
| 107 |
d |
| 108 |
:a |
| 109 |
h |
| 110 |
y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/ |
| 111 |
P |
| 112 |
g |
| 113 |
s/.*\n// |
| 114 |
') |
| 115 |
|
| 116 |
[ "$PARAMS" ] || exit 0 |
| 117 |
|
| 118 |
case "$PHASE" in |
| 119 |
create) |
| 120 |
${MOCK} eval ip -$FAMILY $OBJECT add $IFACE $TYPE_KW $TUNNEL_MODE $PARAMS $MORE_PARAMS |
| 121 |
;; |
| 122 |
destroy) |
| 123 |
${MOCK} ip -$FAMILY $OBJECT del $IFACE |
| 124 |
;; |
| 125 |
depend) |
| 126 |
echo "${IF_TUNNEL_DEV}" "${IF_TUNNEL_LOCAL_DEV}" |
| 127 |
;; |
| 128 |
esac |