summaryrefslogtreecommitdiff
path: root/src/etc/dhcp
diff options
context:
space:
mode:
authorsarthurdev <965089+sarthurdev@users.noreply.github.com>2021-06-11 23:55:24 +0200
committersarthurdev <965089+sarthurdev@users.noreply.github.com>2021-06-12 21:14:18 +0200
commitab5bfe76044f7e5bb7921f11e735dbf77544a89f (patch)
tree49a601130a2d5fb8a6fd4c2454294584c429ff85 /src/etc/dhcp
parent7c2270d298c71ad94e28e6492de2a3cf7c1e67fe (diff)
downloadvyos-1x-ab5bfe76044f7e5bb7921f11e735dbf77544a89f.tar.gz
vyos-1x-ab5bfe76044f7e5bb7921f11e735dbf77544a89f.zip
ipsec: T1501: T3617: Add handling for missing addresses on boot when using dhcp-interface
Diffstat (limited to 'src/etc/dhcp')
-rwxr-xr-xsrc/etc/dhcp/dhclient-exit-hooks.d/ipsec-dhclient-hook76
-rw-r--r--src/etc/dhcp/dhclient-exit-hooks.d/ipsec-dhclient-hook.py46
2 files changed, 76 insertions, 46 deletions
diff --git a/src/etc/dhcp/dhclient-exit-hooks.d/ipsec-dhclient-hook b/src/etc/dhcp/dhclient-exit-hooks.d/ipsec-dhclient-hook
new file mode 100755
index 000000000..e00e5fe6e
--- /dev/null
+++ b/src/etc/dhcp/dhclient-exit-hooks.d/ipsec-dhclient-hook
@@ -0,0 +1,76 @@
+#!/bin/bash
+
+if [ "$reason" == "REBOOT" ] || [ "$reason" == "EXPIRE" ]; then
+ exit 0
+fi
+
+DHCP_HOOK_IFLIST="/tmp/ipsec_dhcp_waiting"
+
+if [ -f $DHCP_HOOK_IFLIST ] && [ "$reason" == "BOUND" ]; then
+ if grep -qw $interface $DHCP_HOOK_IFLIST; then
+ sudo rm $DHCP_HOOK_IFLIST
+ sudo python3 /usr/libexec/vyos/conf_mode/vpn_ipsec.py
+ exit 0
+ fi
+fi
+
+if [ "$old_ip_address" == "$new_ip_address" ] && [ "$reason" == "BOUND" ]; then
+ exit 0
+fi
+
+python3 - <<PYEND
+import os
+import re
+from vyos.util import call
+from vyos.util import cmd
+
+IPSEC_CONF="/etc/ipsec.conf"
+IPSEC_SECRETS="/etc/ipsec.secrets"
+
+def getlines(file):
+ with open(file, 'r') as f:
+ return f.readlines()
+
+def writelines(file, lines):
+ with open(file, 'w') as f:
+ f.writelines(lines)
+
+def ipsec_down(ip_address):
+ # This prevents the need to restart ipsec and kill all active connections, only the stale connection is closed
+ status = cmd('sudo ipsec statusall')
+ connection_name = None
+ for line in status.split("\n"):
+ if line.find(ip_address) > 0:
+ regex_match = re.search(r'(peer-[^:\[]+)', line)
+ if regex_match:
+ connection_name = regex_match[1]
+ break
+ if connection_name:
+ call(f'sudo ipsec down {connection_name}')
+
+if __name__ == '__main__':
+ interface = os.getenv('interface')
+ new_ip = os.getenv('new_ip_address')
+ old_ip = os.getenv('old_ip_address')
+
+ conf_lines = getlines(IPSEC_CONF)
+ secrets_lines = getlines(IPSEC_SECRETS)
+ found = False
+ to_match = f'# dhcp:{interface}'
+
+ for i, line in enumerate(conf_lines):
+ if line.find(to_match) > 0:
+ conf_lines[i] = line.replace(old_ip, new_ip)
+ found = True
+
+ for i, line in enumerate(secrets_lines):
+ if line.find(to_match) > 0:
+ secrets_lines[i] = line.replace(old_ip, new_ip)
+
+ if found:
+ writelines(IPSEC_CONF, conf_lines)
+ writelines(IPSEC_SECRETS, secrets_lines)
+ ipsec_down(old_ip)
+ call('sudo /usr/sbin/ipsec rereadall')
+ call('sudo /usr/sbin/ipsec reload')
+PYEND \ No newline at end of file
diff --git a/src/etc/dhcp/dhclient-exit-hooks.d/ipsec-dhclient-hook.py b/src/etc/dhcp/dhclient-exit-hooks.d/ipsec-dhclient-hook.py
deleted file mode 100644
index 36edf04f3..000000000
--- a/src/etc/dhcp/dhclient-exit-hooks.d/ipsec-dhclient-hook.py
+++ /dev/null
@@ -1,46 +0,0 @@
-#!/usr/bin/env python3
-
-import os
-import sys
-
-from vyos.util import call
-
-IPSEC_CONF="/etc/ipsec.conf"
-IPSEC_SECRETS="/etc/ipsec.secrets"
-
-def getlines(file):
- with open(file, 'r') as f:
- return f.readlines()
-
-def writelines(file, lines):
- with open(file, 'w') as f:
- f.writelines(lines)
-
-if __name__ == '__main__':
- interface = os.getenv('interface')
- new_ip = os.getenv('new_ip_address')
- old_ip = os.getenv('old_ip_address')
- reason = os.getenv('reason')
-
- if (old_ip == new_ip and reason != 'BOUND') or reason in ['REBOOT', 'EXPIRE']:
- sys.exit(0)
-
- conf_lines = getlines(IPSEC_CONF)
- secrets_lines = getlines(IPSEC_SECRETS)
- found = False
- to_match = f'# dhcp:{interface}'
-
- for i, line in enumerate(conf_lines):
- if line.find(to_match) > 0:
- conf_lines[i] = line.replace(old_ip, new_ip)
- found = True
-
- for i, line in enumerate(secrets_lines):
- if line.find(to_match) > 0:
- secrets_lines[i] = line.replace(old_ip, new_ip)
-
- if found:
- writelines(IPSEC_CONF, conf_lines)
- writelines(IPSEC_SECRETS, secrets_lines)
- call('sudo /usr/sbin/ipsec rereadall')
- call('sudo /usr/sbin/ipsec reload')