summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2020-05-01 13:23:20 +0200
committerChristian Poessinger <christian@poessinger.com>2020-05-16 15:30:26 +0200
commit40e8938667b06615e0a1a26271a30e00f8cff2c6 (patch)
tree9c8243946969cc74bf261e27e6c22ee8cf414a0d
parent02ee6b7bf1bcb0a6d55b3d02496d2f501e622ea2 (diff)
downloadvyos-1x-40e8938667b06615e0a1a26271a30e00f8cff2c6.tar.gz
vyos-1x-40e8938667b06615e0a1a26271a30e00f8cff2c6.zip
nat: T2198: initial XML and Python representation
-rw-r--r--debian/control3
-rw-r--r--interface-definitions/include/nat-address-port.xml.i47
-rw-r--r--interface-definitions/include/nat-rule.xml.i86
-rw-r--r--interface-definitions/include/nat-translation-port.xml.i13
-rw-r--r--interface-definitions/nat.xml.in110
-rwxr-xr-xsrc/conf_mode/nat.py63
6 files changed, 322 insertions, 0 deletions
diff --git a/debian/control b/debian/control
index ab0fc0b29..c8fa8ca63 100644
--- a/debian/control
+++ b/debian/control
@@ -92,6 +92,9 @@ Depends: python3,
pppoe,
salt-minion,
vyos-utils,
+ iptables,
+ nftables,
+ conntrack,
${shlibs:Depends},
${misc:Depends}
Description: VyOS configuration scripts and data
diff --git a/interface-definitions/include/nat-address-port.xml.i b/interface-definitions/include/nat-address-port.xml.i
new file mode 100644
index 000000000..0848364ff
--- /dev/null
+++ b/interface-definitions/include/nat-address-port.xml.i
@@ -0,0 +1,47 @@
+<leafNode name="address">
+ <properties>
+ <help>IP address, subnet, or range</help>
+ <valueHelp>
+ <format>ipv4</format>
+ <description>IPv4 address to match</description>
+ </valueHelp>
+ <valueHelp>
+ <format>ipv4net</format>
+ <description>IPv4 prefix to match</description>
+ </valueHelp>
+ <valueHelp>
+ <format>ipv4range</format>
+ <description>IPv4 address range to match</description>
+ </valueHelp>
+ <valueHelp>
+ <format>!ipv4</format>
+ <description>Match everything except the specified address</description>
+ </valueHelp>
+ <valueHelp>
+ <format>!ipv4net</format>
+ <description>Match everything except the specified prefix</description>
+ </valueHelp>
+ <valueHelp>
+ <format>!ipv4range</format>
+ <description>Match everything except the specified range</description>
+ </valueHelp>
+ <!-- TODO: add general iptables constraint script -->
+ </properties>
+</leafNode>
+<leafNode name="port">
+ <properties>
+ <help>Port number</help>
+ <valueHelp>
+ <format>1-65535</format>
+ <description>Numeric IP port</description>
+ </valueHelp>
+ <valueHelp>
+ <format>start-end</format>
+ <description>Numbered port range (e.g., 1001-1005)</description>
+ </valueHelp>
+ <valueHelp>
+ <format> </format>
+ <description>\n\nMultiple destination ports can be specified as a comma-separated list.\nThe whole list can also be negated using '!'.\nFor example: '!22,telnet,http,123,1001-1005'</description>
+ </valueHelp>
+ </properties>
+</leafNode>
diff --git a/interface-definitions/include/nat-rule.xml.i b/interface-definitions/include/nat-rule.xml.i
new file mode 100644
index 000000000..fdba4b8bd
--- /dev/null
+++ b/interface-definitions/include/nat-rule.xml.i
@@ -0,0 +1,86 @@
+<tagNode name="rule">
+ <properties>
+ <help>Rule number for NAT</help>
+ <valueHelp>
+ <format>1-9999</format>
+ <description>Number for this NAT rule</description>
+ </valueHelp>
+ <constraint>
+ <validator name="numeric" argument="--range 1-9999"/>
+ </constraint>
+ <constraintErrorMessage>NAT rule number must be between 1 and 9999</constraintErrorMessage>
+ </properties>
+ <children>
+ <leafNode name="description">
+ <properties>
+ <help>Rule description</help>
+ </properties>
+ </leafNode>
+ <node name="destination">
+ <properties>
+ <help>NAT destination parameters</help>
+ </properties>
+ <children>
+ #include <include/nat-address-port.xml.i>
+ </children>
+ </node>
+ <leafNode name="disable">
+ <properties>
+ <help>Disable NAT rule</help>
+ <valueless/>
+ </properties>
+ </leafNode>
+ <leafNode name="exclude">
+ <properties>
+ <help>Exclude packets matching this rule from NAT</help>
+ <valueless/>
+ </properties>
+ </leafNode>
+ <leafNode name="log">
+ <properties>
+ <help>NAT rule logging</help>
+ <valueless/>
+ </properties>
+ </leafNode>
+ <leafNode name="protocol">
+ <properties>
+ <help>Protocol to NAT</help>
+ <completionHelp>
+ <list>tcp udp tcp_udp all</list>
+ </completionHelp>
+ <valueHelp>
+ <format>tcp</format>
+ <description>Transmission Control Protocol</description>
+ </valueHelp>
+ <valueHelp>
+ <format>udp</format>
+ <description>User Datagram Protocol</description>
+ </valueHelp>
+ <valueHelp>
+ <format>tcp_udp</format>
+ <description>Both TCP and UDP</description>
+ </valueHelp>
+ <valueHelp>
+ <format>all</format>
+ <description>All IP protocols</description>
+ </valueHelp>
+ <valueHelp>
+ <format>0-255</format>
+ <description>IP protocol number</description>
+ </valueHelp>
+ <valueHelp>
+ <format>!&lt;protocol&gt;</format>
+ <description>All IP protocols except for the specified name or number (negation)</description>
+ </valueHelp>
+ </properties>
+ </leafNode>
+ <node name="source">
+ <properties>
+ <help>NAT source parameters</help>
+ </properties>
+ <children>
+ #include <include/nat-address-port.xml.i>
+ </children>
+ </node>
+ </children>
+</tagNode>
diff --git a/interface-definitions/include/nat-translation-port.xml.i b/interface-definitions/include/nat-translation-port.xml.i
new file mode 100644
index 000000000..93de471e3
--- /dev/null
+++ b/interface-definitions/include/nat-translation-port.xml.i
@@ -0,0 +1,13 @@
+<leafNode name="port">
+ <properties>
+ <help>Port number</help>
+ <valueHelp>
+ <format>1-65535</format>
+ <description>Numeric IP port</description>
+ </valueHelp>
+ <valueHelp>
+ <format>&lt;start&gt;-&lt;end&gt;</format>
+ <description>Numbered port range (e.g., 1001-1005)</description>
+ </valueHelp>
+ </properties>
+</leafNode>
diff --git a/interface-definitions/nat.xml.in b/interface-definitions/nat.xml.in
new file mode 100644
index 000000000..bcbdb37af
--- /dev/null
+++ b/interface-definitions/nat.xml.in
@@ -0,0 +1,110 @@
+<?xml version="1.0"?>
+<interfaceDefinition>
+ <node name="nat" owner="sudo ${vyos_conf_scripts_dir}/nat.py">
+ <properties>
+ <help>Network Address Translation (NAT) parameters</help>
+ <priority>220</priority>
+ </properties>
+ <children>
+ <node name="destination">
+ <properties>
+ <help>Destination NAT settings</help>
+ </properties>
+ <children>
+ #include <include/nat-rule.xml.i>
+ <tagNode name="rule">
+ <children>
+ <leafNode name="inbound-interface">
+ <properties>
+ <help>Inbound interface of NAT traffic</help>
+ <completionHelp>
+ <script>${vyos_completion_dir}/list_interfaces.py</script>
+ </completionHelp>
+ </properties>
+ </leafNode>
+ <node name="translation">
+ <properties>
+ <help>Inside NAT IP (destination NAT only)</help>
+ </properties>
+ <children>
+ <leafNode name="address">
+ <properties>
+ <help>IP address, subnet, or range</help>
+ <valueHelp>
+ <format>ipv4</format>
+ <description>IPv4 address to match</description>
+ </valueHelp>
+ <valueHelp>
+ <format>ipv4net</format>
+ <description>IPv4 prefix to match</description>
+ </valueHelp>
+ <valueHelp>
+ <format>ipv4range</format>
+ <description>IPv4 address range to match</description>
+ </valueHelp>
+ <!-- TODO: add general iptables constraint script -->
+ </properties>
+ </leafNode>
+ #include <include/nat-translation-port.xml.i>
+ </children>
+ </node>
+ </children>
+ </tagNode>
+ </children>
+ </node>
+ <node name="source">
+ <properties>
+ <help>Source NAT settings</help>
+ </properties>
+ <children>
+ #include <include/nat-rule.xml.i>
+ <tagNode name="rule">
+ <children>
+ <leafNode name="outbound-interface">
+ <properties>
+ <help>Outbound interface of NAT traffic</help>
+ <completionHelp>
+ <script>${vyos_completion_dir}/list_interfaces.py</script>
+ </completionHelp>
+ </properties>
+ </leafNode>
+ <node name="translation">
+ <properties>
+ <help>Outside NAT IP (source NAT only)</help>
+ </properties>
+ <children>
+ <leafNode name="address">
+ <properties>
+ <help>IP address, subnet, or range</help>
+ <completionHelp>
+ <list>masquerade</list>
+ </completionHelp>
+ <valueHelp>
+ <format>ipv4</format>
+ <description>IPv4 address to match</description>
+ </valueHelp>
+ <valueHelp>
+ <format>ipv4net</format>
+ <description>IPv4 prefix to match</description>
+ </valueHelp>
+ <valueHelp>
+ <format>ipv4range</format>
+ <description>IPv4 address range to match</description>
+ </valueHelp>
+ <valueHelp>
+ <format>masquerade</format>
+ <description>NAT to the primary address of outbound-interface</description>
+ </valueHelp>
+ <!-- TODO: add general iptables constraint script -->
+ </properties>
+ </leafNode>
+ #include <include/nat-translation-port.xml.i>
+ </children>
+ </node>
+ </children>
+ </tagNode>
+ </children>
+ </node>
+ </children>
+ </node>
+</interfaceDefinition>
diff --git a/src/conf_mode/nat.py b/src/conf_mode/nat.py
new file mode 100755
index 000000000..188445214
--- /dev/null
+++ b/src/conf_mode/nat.py
@@ -0,0 +1,63 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2020 VyOS maintainers and contributors
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 or later as
+# published by the Free Software Foundation.
+#
+# 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/>.
+
+from copy import deepcopy
+from sys import exit
+
+from vyos.config import Config
+from vyos import ConfigError
+
+default_config_data = {
+ 'source': [],
+ 'destination': []
+}
+
+def get_config():
+ nat = deepcopy(default_config_data)
+ conf = Config()
+ base = ['nat']
+ if not conf.exists(base):
+ return None
+ else:
+ conf.set_level(base)
+
+ return nat
+
+def verify(nat):
+ if not nat:
+ return None
+
+ return None
+
+def generate(nat):
+ if not nat:
+ return None
+
+ return None
+
+def apply(nat):
+
+ return None
+
+if __name__ == '__main__':
+ try:
+ c = get_config()
+ verify(c)
+ generate(c)
+ apply(c)
+ except ConfigError as e:
+ print(e)
+ exit(1)