blob: 9f183d3fd3e6773d85e5cc0303baec98c3fd4b4c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
#!/bin/sh
# A post-up/down hook to automatically create/delete a virtual subnet
# for IPsec (inet4 only).
# Copyright © 2016 Guilhem Moulin <guilhem@fripost.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set -ue
PATH=/usr/sbin:/usr/bin:/sbin:/bin
# Ignore the loopback interface and non inet4 families.
[ "$IFACE" != lo -a "$ADDRFAM" = inet ] || exit 0
# Only the device with the default, globally-scoped route, is of
# interest here.
iface="$( ip -o route show to default scope global \
| sed -nr '/^default via \S+ dev (\S+).*/ {s//\1/p;q}' )"
[ "$iface" = "$IFACE" ] || exit 0
vip="{{ ipsec[inventory_hostname_short] }}"
vsubnet="{{ ipsec_subnet }}"
case "$MODE" in
start) ip address add "$vip/32" dev "$IFACE" scope global || true
# Nullroute the subnet used for IPsec to avoid data leaks
# in the absence of xfrm lookup (i.e., when there is no
# matching IPsec Security Association).
ip route replace prohibit "$vsubnet" proto static || true
ip route replace table 220 to "$vsubnet" dev "$IFACE" proto static src "$vip" || true
;;
stop) ip route del table 220 to "$vsubnet" dev "$IFACE" proto static src "$vip" || true
ip route del prohibit "$vsubnet" proto static || true
ip address del "$vip/32" dev "$IFACE" scope global || true
esac
|