summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@vyos.io>2025-03-18 15:58:26 +0000
committerGitHub <noreply@github.com>2025-03-18 15:58:26 +0000
commit337837dd8a57d7af282d603f8b930ed40f7fa03c (patch)
treef156d60a3f29bae77303f8409ccc3395d9d7bad2 /src
parent7de0ab73af90d4d39519f3ba0dec80fb6b9badf5 (diff)
parent13f99beada6ac28ede7c74d434b84a63bf9905b0 (diff)
downloadvyos-1x-337837dd8a57d7af282d603f8b930ed40f7fa03c.tar.gz
vyos-1x-337837dd8a57d7af282d603f8b930ed40f7fa03c.zip
Merge pull request #16 from sever-sever/T7181
T7181: VPP add initial source NAT implentation
Diffstat (limited to 'src')
-rwxr-xr-xsrc/conf_mode/vpp.py4
-rw-r--r--src/conf_mode/vpp_nat_source.py119
2 files changed, 123 insertions, 0 deletions
diff --git a/src/conf_mode/vpp.py b/src/conf_mode/vpp.py
index 90a3bb7fb..1f4c6c0f0 100755
--- a/src/conf_mode/vpp.py
+++ b/src/conf_mode/vpp.py
@@ -135,6 +135,10 @@ def get_config(config=None):
# to be reinitialized after the commit
set_dependents('ethernet', conf, removed_iface)
+ # NAT dependency
+ if conf.exists(['vpp', 'nat44', 'source']):
+ set_dependents('vpp_nat_source', conf)
+
if not conf.exists(base):
return {
'removed_ifaces': removed_ifaces,
diff --git a/src/conf_mode/vpp_nat_source.py b/src/conf_mode/vpp_nat_source.py
new file mode 100644
index 000000000..2e1884c3a
--- /dev/null
+++ b/src/conf_mode/vpp_nat_source.py
@@ -0,0 +1,119 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2025 VyOS Inc.
+#
+# 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 2 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, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+from vyos.config import Config
+from vyos import ConfigError
+from vyos.vpp.nat.nat44 import Nat44
+
+
+def get_config(config=None) -> dict:
+ if config:
+ conf = config
+ else:
+ conf = Config()
+
+ base = ['vpp', 'nat44', 'source']
+
+ # Get config_dict with default values
+ config = conf.get_config_dict(
+ base,
+ key_mangling=('-', '_'),
+ get_first_key=True,
+ no_tag_node_value_mangle=True,
+ with_defaults=True,
+ with_recursive_defaults=True,
+ )
+
+ # Get effective config as we need full dicitonary per interface delete
+ effective_config = conf.get_config_dict(
+ base,
+ key_mangling=('-', '_'),
+ effective=True,
+ get_first_key=True,
+ no_tag_node_value_mangle=True,
+ )
+
+ if not config:
+ config['remove'] = True
+
+ if effective_config:
+ config.update({'effective': effective_config})
+
+ return config
+
+
+def verify(config):
+ if 'remove' in config:
+ return None
+
+ required_keys = {'inbound_interface', 'outbound_interface'}
+ if not all(key in config for key in required_keys):
+ missing_keys = required_keys - set(config.keys())
+ raise ConfigError(
+ f"Required options are missing: {', '.join(missing_keys).replace('_', '-')}"
+ )
+
+ if not config.get('translation', {}).get('address'):
+ raise ConfigError('Translation requires address')
+
+ if config.get('translation', {}).get('address') == 'masquerade':
+ raise ConfigError('Masquerade is not implemented')
+
+
+def generate(config):
+ pass
+
+
+def apply(config):
+ # Delete NAT source
+ if 'effective' in config:
+ remove_config = config.get('effective')
+ interface_in = remove_config.get('inbound_interface')
+ interface_out = remove_config.get('outbound_interface')
+ translation_address = remove_config.get('translation', {}).get('address')
+
+ n = Nat44(interface_in, interface_out, translation_address)
+ n.delete_nat44_out_interface()
+ n.delete_nat44_interface_inside()
+ n.delete_nat44_address_range()
+
+ if 'remove' in config:
+ return None
+
+ # Add NAT44
+ interface_in = config.get('inbound_interface')
+ interface_out = config.get('outbound_interface')
+ translation_address = config.get('translation', {}).get('address')
+
+ n = Nat44(interface_in, interface_out, translation_address)
+ n.enable_nat44_ed()
+ n.enable_nat44_forwarding()
+ n.add_nat44_out_interface()
+ # n.add_nat44_interface_inside()
+ n.add_nat44_address_range()
+
+
+if __name__ == '__main__':
+ try:
+ c = get_config()
+ verify(c)
+ generate(c)
+ apply(c)
+ except ConfigError as e:
+ print(e)
+ exit(1)