Page MenuHomeVyOS Platform

T8199-ipsec-dos-protection.patch

Authored By
Viacheslav
Tue, Nov 17, 1:45 PM
Size
14 KB
Referenced Files
None
Subscribers
None

T8199-ipsec-dos-protection.patch

diff --git a/data/templates/ipsec/charon.j2 b/data/templates/ipsec/charon.j2
index bafdaf1..9902aee 100644
--- a/data/templates/ipsec/charon.j2
+++ b/data/templates/ipsec/charon.j2
@@ -4,7 +4,11 @@ charon {
# accept_unencrypted_mainmode_messages = no
# Maximum number of half-open IKE_SAs for a single peer IP.
+{% if options.dos_protection.block_threshold is vyos_defined %}
+ block_threshold = {{ options.dos_protection.block_threshold }}
+{% else %}
# block_threshold = 5
+{% endif %}
# Whether Certificate Revocation Lists (CRLs) fetched via HTTP or LDAP should
# be saved under a unique file name derived from the public key of the
@@ -34,7 +38,19 @@ charon {
# close_ike_on_child_failure = no
# Number of half-open IKE_SAs that activate the cookie mechanism.
+{% if options.dos_protection.cookie_threshold is vyos_defined %}
+ cookie_threshold = {{ options.dos_protection.cookie_threshold }}
+{% else %}
# cookie_threshold = 10
+{% endif %}
+
+ # Number of half-open IKE_SAs for a single peer IP that activate the cookie
+ # mechanism.
+{% if options.dos_protection.cookie_threshold_per_peer is vyos_defined %}
+ cookie_threshold_ip = {{ options.dos_protection.cookie_threshold_per_peer }}
+{% else %}
+ # cookie_threshold_ip = 3
+{% endif %}
# Delete CHILD_SAs right after they got successfully rekeyed (IKEv1 only).
# delete_rekeyed = no
@@ -55,7 +71,11 @@ charon {
# Enable Denial of Service protection using cookies and aggressiveness
# checks.
+{% if options.dos_protection.disable is vyos_defined %}
+ dos_protection = no
+{% else %}
# dos_protection = yes
+{% endif %}
# Compliance with the errata for RFC 4753.
# ecp_x_coordinate_only = yes
@@ -77,7 +97,11 @@ charon {
# group =
# Timeout in seconds for connecting IKE_SAs (also see IKE_SA_INIT DROPPING).
+{% if options.dos_protection.half_open_timeout is vyos_defined %}
+ half_open_timeout = {{ options.dos_protection.half_open_timeout }}
+{% else %}
# half_open_timeout = 30
+{% endif %}
# Enable hash and URL support.
# hash_and_url = no
@@ -108,10 +132,18 @@ charon {
# Limit new connections based on the current number of half open IKE_SAs,
# see IKE_SA_INIT DROPPING in strongswan.conf(5).
+{% if options.dos_protection.init_limit_half_open is vyos_defined %}
+ init_limit_half_open = {{ options.dos_protection.init_limit_half_open }}
+{% else %}
# init_limit_half_open = 0
+{% endif %}
# Limit new connections based on the number of queued jobs.
+{% if options.dos_protection.init_limit_job_load is vyos_defined %}
+ init_limit_job_load = {{ options.dos_protection.init_limit_job_load }}
+{% else %}
# init_limit_job_load = 0
+{% endif %}
# Causes charon daemon to ignore IKE initiation requests.
# initiator_only = no
@@ -248,7 +280,11 @@ charon {
# signature_authentication_constraints = yes
# Number of worker threads in charon.
+{% if options.threads is vyos_defined %}
+ threads = {{ options.threads }}
+{% else %}
# threads = 16
+{% endif %}
# Name of the user the daemon changes to after startup.
# user =
diff --git a/interface-definitions/vpn_ipsec.xml.in b/interface-definitions/vpn_ipsec.xml.in
index a62aec7..6b0e197 100644
--- a/interface-definitions/vpn_ipsec.xml.in
+++ b/interface-definitions/vpn_ipsec.xml.in
@@ -752,6 +752,99 @@
<valueless/>
</properties>
</leafNode>
+ <node name="dos-protection">
+ <properties>
+ <help>Half-open IKE_SA handling and IKE_SA_INIT DoS protection</help>
+ </properties>
+ <children>
+ <leafNode name="block-threshold">
+ <properties>
+ <help>Maximum number of half-open IKE_SAs for a single peer IP (default: 5)</help>
+ <valueHelp>
+ <format>u32:1-65535</format>
+ <description>Half-open IKE_SAs per peer IP</description>
+ </valueHelp>
+ <constraint>
+ <validator name="numeric" argument="--range 1-65535"/>
+ </constraint>
+ </properties>
+ </leafNode>
+ <leafNode name="cookie-threshold">
+ <properties>
+ <help>Number of half-open IKE_SAs that activate the cookie mechanism (default: 10)</help>
+ <valueHelp>
+ <format>u32:1-65535</format>
+ <description>Half-open IKE_SAs in total</description>
+ </valueHelp>
+ <constraint>
+ <validator name="numeric" argument="--range 1-65535"/>
+ </constraint>
+ </properties>
+ </leafNode>
+ <leafNode name="cookie-threshold-per-peer">
+ <properties>
+ <help>Number of half-open IKE_SAs for a single peer IP that activate the cookie mechanism (default: 3)</help>
+ <valueHelp>
+ <format>u32:1-65535</format>
+ <description>Half-open IKE_SAs per peer IP</description>
+ </valueHelp>
+ <constraint>
+ <validator name="numeric" argument="--range 1-65535"/>
+ </constraint>
+ </properties>
+ </leafNode>
+ <leafNode name="disable">
+ <properties>
+ <help>Disable cookie and per-peer block threshold DoS protection</help>
+ <valueless/>
+ </properties>
+ </leafNode>
+ <leafNode name="half-open-timeout">
+ <properties>
+ <help>Timeout in seconds for connecting (half-open) IKE_SAs (default: 30)</help>
+ <valueHelp>
+ <format>u32:1-86400</format>
+ <description>Timeout in seconds</description>
+ </valueHelp>
+ <constraint>
+ <validator name="numeric" argument="--range 1-86400"/>
+ </constraint>
+ </properties>
+ </leafNode>
+ <leafNode name="init-limit-half-open">
+ <properties>
+ <help>Drop new IKE_SA_INIT requests when this many IKE_SAs are half-open (default: 0)</help>
+ <valueHelp>
+ <format>u32:1-4294967295</format>
+ <description>Maximum number of half-open IKE_SAs</description>
+ </valueHelp>
+ <valueHelp>
+ <format>0</format>
+ <description>No limit</description>
+ </valueHelp>
+ <constraint>
+ <validator name="numeric" argument="--range 0-4294967295"/>
+ </constraint>
+ </properties>
+ </leafNode>
+ <leafNode name="init-limit-job-load">
+ <properties>
+ <help>Drop new IKE_SA_INIT requests when this many jobs are queued (default: 0)</help>
+ <valueHelp>
+ <format>u32:1-4294967295</format>
+ <description>Maximum number of queued jobs</description>
+ </valueHelp>
+ <valueHelp>
+ <format>0</format>
+ <description>No limit</description>
+ </valueHelp>
+ <constraint>
+ <validator name="numeric" argument="--range 0-4294967295"/>
+ </constraint>
+ </properties>
+ </leafNode>
+ </children>
+ </node>
<leafNode name="flexvpn">
<properties>
<help>Allow FlexVPN vendor ID payload (IKEv2 only)</help>
@@ -759,6 +852,18 @@
</properties>
</leafNode>
#include <include/generic-interface.xml.i>
+ <leafNode name="threads">
+ <properties>
+ <help>Number of IKE daemon worker threads (default: 16)</help>
+ <valueHelp>
+ <format>u32:4-1024</format>
+ <description>Number of worker threads</description>
+ </valueHelp>
+ <constraint>
+ <validator name="numeric" argument="--range 4-1024"/>
+ </constraint>
+ </properties>
+ </leafNode>
<leafNode name="virtual-ip">
<properties>
<help>Allow install virtual-ip addresses</help>
diff --git a/smoketest/scripts/cli/test_vpn_ipsec.py b/smoketest/scripts/cli/test_vpn_ipsec.py
index 6672690..1d363f0 100755
--- a/smoketest/scripts/cli/test_vpn_ipsec.py
+++ b/smoketest/scripts/cli/test_vpn_ipsec.py
@@ -2171,6 +2171,47 @@ def test_retransmission_default_settings(self):
cli_value = default_value(base_path + ['options', 'retransmission', cli_option])
self.assertEqual(config_value, cli_value)
+ def test_dos_protection_settings(self):
+ # T8199: half-open IKE_SA / IKE_SA_INIT DoS protection tuning
+ dos_options = {
+ 'block-threshold' : ('block_threshold', '50'),
+ 'cookie-threshold' : ('cookie_threshold', '200'),
+ 'cookie-threshold-per-peer' : ('cookie_threshold_ip', '20'),
+ 'half-open-timeout' : ('half_open_timeout', '60'),
+ 'init-limit-half-open' : ('init_limit_half_open', '1000'),
+ 'init-limit-job-load' : ('init_limit_job_load', '500'),
+ }
+ threads = '64'
+
+ # commit defaults - strongSwan built-in values must remain untouched
+ self.cli_commit()
+ charon_conf = read_file(charon_file)
+ for _, (charon_option, _) in dos_options.items():
+ self.assertNotIn(f'\n {charon_option} =', charon_conf)
+ self.assertNotIn('\n threads =', charon_conf)
+ self.assertNotIn('\n dos_protection =', charon_conf)
+
+ for cli_option, (_, value) in dos_options.items():
+ self.cli_set(base_path + ['options', 'dos-protection', cli_option, value])
+ self.cli_set(base_path + ['options', 'threads', threads])
+ self.cli_commit()
+
+ charon_conf = read_file(charon_file)
+ for _, (charon_option, value) in dos_options.items():
+ self.assertIn(f'{charon_option} = {value}', charon_conf)
+ self.assertIn(f'threads = {threads}', charon_conf)
+
+ # Thresholds have no effect when DoS protection is disabled
+ self.cli_set(base_path + ['options', 'dos-protection', 'disable'])
+ with self.assertRaises(ConfigSessionError):
+ self.cli_commit()
+ for option in ['block-threshold', 'cookie-threshold', 'cookie-threshold-per-peer']:
+ self.cli_delete(base_path + ['options', 'dos-protection', option])
+ self.cli_commit()
+
+ charon_conf = read_file(charon_file)
+ self.assertIn('dos_protection = no', charon_conf)
+
def test_esn_settings(self):
self.cli_set(base_path + ['ike-group', ike_group, 'key-exchange', 'ikev2'])
self.cli_set(base_path + ['ike-group', ike_group, 'proposal', '1', 'dh-group', '14'])
diff --git a/src/conf_mode/vpn_ipsec.py b/src/conf_mode/vpn_ipsec.py
index eba74a2..3f07e7a 100644
--- a/src/conf_mode/vpn_ipsec.py
+++ b/src/conf_mode/vpn_ipsec.py
@@ -30,6 +30,7 @@
from vyos.configdep import call_dependents
from vyos.configdict import get_interface_dict
from vyos.configdict import leaf_node_changed
+from vyos.configdict import is_node_changed
from vyos.configverify import verify_interface_exists
from vyos.configverify import dynamic_interface_pattern
from vyos.defaults import directories
@@ -177,6 +178,13 @@ def get_config(config=None):
ipsec['install_routes'] = 'no' if conf.exists(base + ["options", "disable-route-autoinstall"]) else default_install_routes
ipsec['interface_change'] = leaf_node_changed(conf, base + ['interface'])
+ # T8199: charon reads worker thread count and IKE_SA_INIT DoS thresholds
+ # only at daemon startup - a reload (SIGHUP) does not apply them
+ for node in [['options', 'dos-protection'], ['options', 'threads']]:
+ if is_node_changed(conf, base + node):
+ ipsec['restart_required'] = {}
+ break
+
if ipsec['nhrp_exists']:
set_dependents('nhrp', conf)
@@ -262,6 +270,30 @@ def verify(ipsec):
if not ipsec or 'deleted' in ipsec:
return
+ # T8199: sanity check IKE_SA_INIT DoS protection thresholds
+ dos = dict_search('options.dos_protection', ipsec) or {}
+ if 'disable' in dos:
+ for opt in ['block_threshold', 'cookie_threshold', 'cookie_threshold_per_peer']:
+ if opt in dos:
+ cli_opt = opt.replace('_', '-')
+ raise ConfigError(f'DoS protection "{cli_opt}" has no effect '
+ 'when DoS protection is disabled!')
+ else:
+ block = int(dos.get('block_threshold', 5))
+ cookie_peer = int(dos.get('cookie_threshold_per_peer', 3))
+ cookie_global = int(dos.get('cookie_threshold', 10))
+ if cookie_peer >= block:
+ Warning(f'DoS protection "cookie-threshold-per-peer" ({cookie_peer}) is not '
+ f'lower than "block-threshold" ({block}), peers will be blocked '
+ 'before cookies are requested!')
+ if cookie_peer > cookie_global:
+ Warning(f'DoS protection "cookie-threshold-per-peer" ({cookie_peer}) is higher '
+ f'than "cookie-threshold" ({cookie_global})!')
+
+ if 'restart_required' in ipsec:
+ Warning('Changing IPsec "dos-protection" or "threads" options restarts the '
+ 'IKE daemon, all IKE/IPsec SAs will be re-established!')
+
# T8136 PPK support; keep a list of PPK IDs
ppk_ids = []
@@ -941,7 +973,10 @@ def apply(ipsec):
call(f'systemctl stop {systemd_service}')
remove_vti_updown_db()
else:
- call(f'systemctl reload-or-restart {systemd_service}')
+ if 'restart_required' in ipsec:
+ call(f'systemctl restart {systemd_service}')
+ else:
+ call(f'systemctl reload-or-restart {systemd_service}')
if ipsec['enabled_vti_interfaces']:
with open_vti_updown_db_for_create_or_update() as db:
db.removeAllOtherInterfaces(ipsec['enabled_vti_interfaces'])

File Metadata

Mime Type
text/x-diff
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4844479
Default Alt Text
T8199-ipsec-dos-protection.patch (14 KB)

Event Timeline