From b047855b80754d78cab4d3161ad0e97c21f479bc Mon Sep 17 00:00:00 2001 From: jack9603301 Date: Mon, 30 Nov 2020 20:03:00 +0800 Subject: nptv6: T2518: Initial support for nat66 (NPT) --- data/configd-include.json | 1 + data/templates/firewall/nftables-nat.tmpl | 2 +- data/templates/firewall/nftables-nat66.tmpl | 101 +++++++++++++ data/templates/proxy-ndp/ndppd.conf.tmpl | 41 ++++++ debian/control | 3 +- interface-definitions/nat.xml.in | 67 --------- interface-definitions/nat66.xml.in | 181 +++++++++++++++++++++++ smoketest/scripts/cli/test_nat66.py | 62 ++++++++ src/conf_mode/nat66.py | 221 ++++++++++++++++++++++++++++ src/migration-scripts/nat66/0-to-1 | 76 ++++++++++ src/systemd/ndppd.service | 15 ++ 11 files changed, 701 insertions(+), 69 deletions(-) create mode 100644 data/templates/firewall/nftables-nat66.tmpl create mode 100644 data/templates/proxy-ndp/ndppd.conf.tmpl create mode 100644 interface-definitions/nat66.xml.in create mode 100755 smoketest/scripts/cli/test_nat66.py create mode 100755 src/conf_mode/nat66.py create mode 100755 src/migration-scripts/nat66/0-to-1 create mode 100644 src/systemd/ndppd.service diff --git a/data/configd-include.json b/data/configd-include.json index 318ab0e14..345460700 100644 --- a/data/configd-include.json +++ b/data/configd-include.json @@ -28,6 +28,7 @@ "ipsec-settings.py", "lldp.py", "nat.py", +"nat66.py", "ntp.py", "policy-local-route.py", "protocols_bgp.py", diff --git a/data/templates/firewall/nftables-nat.tmpl b/data/templates/firewall/nftables-nat.tmpl index 5480447f2..499733225 100644 --- a/data/templates/firewall/nftables-nat.tmpl +++ b/data/templates/firewall/nftables-nat.tmpl @@ -118,7 +118,7 @@ {% endmacro %} # Start with clean NAT table -flush table nat +flush table ip nat {% if helper_functions == 'remove' %} {# NAT if going to be disabled - remove rules and targets from nftables #} {% set base_command = 'delete rule ip raw' %} diff --git a/data/templates/firewall/nftables-nat66.tmpl b/data/templates/firewall/nftables-nat66.tmpl new file mode 100644 index 000000000..af533812e --- /dev/null +++ b/data/templates/firewall/nftables-nat66.tmpl @@ -0,0 +1,101 @@ +#!/usr/sbin/nft -f + +{% macro nptv6_rule(rule,config, chain) %} + +{% set src_prefix = "ip6 saddr " + config.source.prefix if config.source is defined and config.source.prefix is defined and config.source.prefix is not none %} +{% set dest_address = "ip6 daddr " + config.destination.address if config.destination is defined and config.destination.address is defined and config.destination.address is not none %} + +{% if chain == "PREROUTING" %} +{% set interface = " iifname \"" + config.inbound_interface + "\"" if config.inbound_interface is defined and config.inbound_interface != 'any' else '' %} +{% set trns_address = "dnat to " + config.translation.address if config.translation is defined and config.translation.address is defined and config.translation.address is not none %} +{% elif chain == "POSTROUTING" %} +{% set interface = " oifname \"" + config.outbound_interface + "\"" if config.outbound_interface is defined and config.outbound_interface != 'any' else '' %} +{% set trns_prefix = "snat prefix to " + config.translation.prefix if config.translation is defined and config.translation.prefix is defined and config.translation.prefix is not none %} +{% endif %} +{% set comment = "NPT-NAT-" + rule %} +{% if rule.log %} +{% set base_log = "[NPT-DST-" + rule %} +{% set log = base_log + "]" %} +{% endif %} +{% set output = "add rule ip6 nat " + chain + interface %} +{# Count packets #} +{% set output = output + " counter" %} + +{# Special handling of log option, we must repeat the entire rule before the #} +{# NAT translation options are added, this is essential #} +{% if log %} +{% set log_output = output + " log prefix \"" + log + "\" comment \"" + comment + "\"" %} +{% endif %} + +{% if src_prefix %} +{% set output = output + " " + src_prefix %} +{% endif %} + + +{% if dest_address %} +{% set output = output + " " + dest_address %} +{% endif %} + +{% if trns_prefix %} +{% set output = output + " " + trns_prefix %} +{% endif %} + +{% if trns_address %} +{% set output = output + " " + trns_address %} +{% endif %} + + +{% if comment %} +{% set output = output + " comment \"" + comment + "\"" %} +{% endif %} + +{{ log_output if log_output }} +{{ output }} +{% endmacro %} + +# Start with clean NAT table +flush table ip6 nat + +{% if helper_functions == 'remove' %} +{# NAT if going to be disabled - remove rules and targets from nftables #} + + + +{% set base_command = "delete rule ip6 raw" %} + +{{base_command}} PREROUTING handle {{ pre_ct_conntrack }} +{{base_command}} OUTPUT handle {{ out_ct_conntrack }} + +delete chain ip6 raw NAT_CONNTRACK + +{% elif helper_functions == 'add' %} +{# NAT if enabled - add targets to nftables #} +add chain ip6 raw NAT_CONNTRACK +add rule ip6 raw NAT_CONNTRACK counter accept + +{% set base_command = "add rule ip6 raw" %} + + +{{ base_command }} PREROUTING position {{ pre_ct_conntrack }} counter jump NAT_CONNTRACK +{{ base_command }} OUTPUT position {{ out_ct_conntrack }} counter jump NAT_CONNTRACK + +{% endif %} + +# +# Destination NAT66 rules build up here +# +{% if destination is defined and destination.rule is defined and destination.rule is not none %} +{% for rule, config in destination.rule.items() if config.disable is not defined %} +{{ nptv6_rule(rule, config, 'PREROUTING') }} +{% endfor %} +{% endif %} + +# +# Source NAT66 rules build up here +# +{% if source is defined and source.rule is defined and source.rule is not none %} +{% for rule, config in source.rule.items() if config.disable is not defined %} +{{ nptv6_rule(rule, config, 'POSTROUTING') }} +{% endfor %} +{% endif %} + diff --git a/data/templates/proxy-ndp/ndppd.conf.tmpl b/data/templates/proxy-ndp/ndppd.conf.tmpl new file mode 100644 index 000000000..9bf120b3a --- /dev/null +++ b/data/templates/proxy-ndp/ndppd.conf.tmpl @@ -0,0 +1,41 @@ +######################################################## +# +# autogenerated by nat66.py +# +# The configuration file must define one upstream +# interface. +# +# For some services, such as nat66, because it runs +# stateless, it needs to rely on NDP Proxy to respond +# to NDP requests. +# +# When using nat66 source rules, NDP Proxy needs +# to be enabled +# +######################################################## + + +{% for i in interface %} + +{%- if not interface[i].disable %} + +proxy {{ i }} { + router {{ interface[i].router }} + timeout {{ interface[i].timeout }} + ttl {{ interface[i].ttl }} +{% for p in interface[i].prefix %} + rule {{ p }} { +{% if interface[i].prefix[p].mode == 'auto' %} + auto +{% elif interface[i].prefix[p].mode == 'static' %} + static +{% elif interface[i].prefix[p].mode == 'iface' and interface[i].prefix[p].iface %} + iface {{ interface[i].prefix[p].iface }} +{% endif %} + } +{%- endfor %} +} + +{%- endif %} + +{% endfor %} diff --git a/debian/control b/debian/control index d0ba72bcf..2c8ee3d43 100644 --- a/debian/control +++ b/debian/control @@ -129,7 +129,8 @@ Depends: wide-dhcpv6-client, wireguard-tools, wireless-regdb, - wpasupplicant (>= 0.6.7) + wpasupplicant (>= 0.6.7), + ndppd Description: VyOS configuration scripts and data VyOS configuration scripts, interface definitions, and everything diff --git a/interface-definitions/nat.xml.in b/interface-definitions/nat.xml.in index d6bed5b27..3cff8abc9 100644 --- a/interface-definitions/nat.xml.in +++ b/interface-definitions/nat.xml.in @@ -56,73 +56,6 @@ - - - IPv6-to-IPv6 Network Prefix Translation Settings - - - - - NPTv6 rule number - - 1-999999 - Number for this rule - - - - - NAT rule number must be between 1 and 999999 - - - - - Rule description - - - #include - #include - - - IPv6 source prefix options - - - - - IPv6 prefix to be translated - - ipv6net - IPv6 prefix - - - - - - - - - - - Translated IPv6 prefix options - - - - - IPv6 prefix to translate to - - ipv6net - IPv6 prefix - - - - - - - - - - - - Source NAT settings diff --git a/interface-definitions/nat66.xml.in b/interface-definitions/nat66.xml.in new file mode 100644 index 000000000..b8e8a8859 --- /dev/null +++ b/interface-definitions/nat66.xml.in @@ -0,0 +1,181 @@ + + + + + IPv6-to-IPv6 Network Prefix Translation (NAT66/NPT) Settings + 220 + + + + + Prefix mapping of IPv6 source address translation + + + + + SNPTv6 rule number + + 1-999999 + Number for this rule + + + + + NAT66 rule number must be between 1 and 999999 + + + + + Rule description + + + + + Disable NAT66 rule + + + + + + NAT66 rule logging + + + + + + Outbound interface of NAT traffic + + + + + + + + IPv6 source prefix options + + + + + IPv6 prefix to be translated + + ipv6net + IPv6 prefix + + + + + + + + + + + Translated IPv6 prefix options + + + + + IPv6 prefix to translate to + + ipv6net + IPv6 prefix + + + + + + + + + + + + + + + Prefix mapping for IPv6 destination address translation + + + + + DNPTv6 rule number + + 1-999999 + Number for this rule + + + + + NAT66 rule number must be between 1 and 999999 + + + + + Rule description + + + + + Disable DNPT rule + + + + + + NAT66 rule logging + + + + + + Inbound interface of NAT traffic + + + + + + + + IPv6 destination prefix options + + + + + IPv6 address to be translated + + ipv6 + IPv6 address + + + + + + + + + + + Translated IPv6 address options + + + + + IPv6 address to translate to + + ipv6 + IPv6 address + + + + + + + + + + + + + + + diff --git a/smoketest/scripts/cli/test_nat66.py b/smoketest/scripts/cli/test_nat66.py new file mode 100755 index 000000000..c1514270d --- /dev/null +++ b/smoketest/scripts/cli/test_nat66.py @@ -0,0 +1,62 @@ +#!/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 . + +import os +import jmespath +import json +import unittest + +from vyos.configsession import ConfigSession +from vyos.util import cmd + +base_path = ['nat66'] +snat_pattern = 'nftables[?rule].rule[?chain].{chain: chain, comment: comment, prefix: { network: expr[].match.right.prefix.addr | [0], prefix: expr[].match.right.prefix.len | [0]},saddr: {network: expr[].snat.addr.prefix.addr | [0], prefix: expr[].snat.addr.prefix.len | [0]}}' + +class TestNAT66(unittest.TestCase): + def setUp(self): + # ensure we can also run this test on a live system - so lets clean + # out the current configuration :) + self.session = ConfigSession(os.getpid()) + self.session.delete(base_path) + + def tearDown(self): + self.session.delete(base_path) + self.session.commit() + + def test_source_nat66(self): + + path = base_path + ['source'] + source_prefix = 'fc00::/64' + translation_prefix = 'fc00:2::/64' + self.session.set(path + ['rule', '1', 'outbound-interface', 'eth1']) + self.session.set(path + ['rule', '1', 'source', 'prefix', source_prefix]) + self.session.set(path + ['rule', '1', 'translation', 'prefix', translation_prefix]) + + # check validate() - outbound-interface must be defined + self.session.commit() + + tmp = cmd('sudo nft -j list table ip6 nat') + nftable_json = json.loads(tmp) + condensed_json = jmespath.search(snat_pattern, nftable_json)[0] + + self.assertEqual(condensed_json['comment'], 'NPT-NAT-1') + self.assertEqual(condensed_json['prefix']['network'], source_prefix.split('/')[0]) + self.assertEqual(str(condensed_json['prefix']['prefix']), source_prefix.split('/')[1]) + self.assertEqual(condensed_json['saddr']['network'], translation_prefix.split('/')[0]) + self.assertEqual(str(condensed_json['saddr']['prefix']), translation_prefix.split('/')[1]) + +if __name__ == '__main__': + unittest.main(verbosity=2) diff --git a/src/conf_mode/nat66.py b/src/conf_mode/nat66.py new file mode 100755 index 000000000..fdfffe335 --- /dev/null +++ b/src/conf_mode/nat66.py @@ -0,0 +1,221 @@ +#!/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 . + +import jmespath +import json +import os + +from sys import exit +from netifaces import interfaces + +from vyos.config import Config +from vyos.configdict import dict_merge +from vyos.template import render +from vyos.util import cmd +from vyos.util import check_kmod +from vyos.util import dict_search +from vyos.template import is_ipv6 +from vyos.xml import defaults +from vyos import ConfigError +import platform +from vyos import airbag +airbag.enable() + +k_mod = ['nft_nat', 'nft_chain_nat'] + +iptables_nat_config = '/tmp/vyos-nat66-rules.nft' +ndppd_config = '/run/ndppd/ndppd.conf' + +def get_handler(json, chain, target): + """ Get nftable rule handler number of given chain/target combination. + Handler is required when adding NAT66/Conntrack helper targets """ + for x in json: + if x['chain'] != chain: + continue + if x['target'] != target: + continue + return x['handle'] + + return None + +def get_config(config=None): + if config: + conf = config + else: + conf = Config() + + base = ['nat66'] + nat = conf.get_config_dict(base, key_mangling=('-', '_'), get_first_key=True) + + # T2665: we must add the tagNode defaults individually until this is + # moved to the base class + for direction in ['source', 'destination']: + if direction in nat: + default_values = defaults(base + [direction, 'rule']) + for rule in nat[direction]['rule']: + nat[direction]['rule'][rule] = dict_merge(default_values, + nat[direction]['rule'][rule]) + + # read in current nftable (once) for further processing + tmp = cmd('nft -j list table ip6 raw') + nftable_json = json.loads(tmp) + + # condense the full JSON table into a list with only relevand informations + pattern = 'nftables[?rule].rule[?expr[].jump].{chain: chain, handle: handle, target: expr[].jump.target | [0]}' + condensed_json = jmespath.search(pattern, nftable_json) + + if not conf.exists(base): + nat['helper_functions'] = 'remove' + + nat['pre_ct_conntrack'] = get_handler(condensed_json, 'PREROUTING', 'NAT_CONNTRACK') + nat['out_ct_conntrack'] = get_handler(condensed_json, 'OUTPUT','NAT_CONNTRACK') + + nat['deleted'] = '' + + return nat + + # check if NAT66 connection tracking helpers need to be set up - this has to + # be done only once + if not get_handler(condensed_json, 'PREROUTING', 'NAT_CONNTRACK'): + nat['helper_functions'] = 'add' + + # Retrieve current table handler positions + nat['pre_ct_conntrack'] = get_handler(condensed_json, 'PREROUTING', 'VYATTA_CT_PREROUTING_HOOK') + nat['out_ct_conntrack'] = get_handler(condensed_json, 'OUTPUT','VYATTA_CT_OUTPUT_HOOK') + else: + nat['helper_functions'] = 'has' + + return nat + +def verify(nat): + if not nat or 'deleted' in nat: + # no need to verify the CLI as NAT66 is going to be deactivated + return None + + if 'helper_functions' in nat and nat['helper_functions'] != 'has': + if not (nat['pre_ct_conntrack'] or nat['out_ct_conntrack']): + raise Exception('could not determine nftable ruleset handlers') + + if dict_search('source.rule', nat): + for rule, config in dict_search('source.rule', nat).items(): + err_msg = f'Source NAT66 configuration error in rule {rule}:' + if 'outbound_interface' not in config: + raise ConfigError(f'{err_msg}\n' \ + 'outbound-interface not specified') + else: + if config['outbound_interface'] not in 'any' and config['outbound_interface'] not in interfaces(): + print(f'WARNING: rule "{rule}" interface "{config["outbound_interface"]}" does not exist on this system') + + + prefix = dict_search('translation.prefix', config) + if prefix != None: + if not is_ipv6(prefix): + raise ConfigError(f'Warning: IPv6 prefix {prefix} is not a valid address prefix') + + prefix = dict_search('source.prefix', config) + if prefix != None: + if not is_ipv6(prefix): + raise ConfigError(f'{err_msg} source-prefix not specified') + + + if dict_search('destination.rule', nat): + for rule, config in dict_search('destination.rule', nat).items(): + err_msg = f'Destination NAT66 configuration error in rule {rule}:' + + if 'inbound_interface' not in config: + raise ConfigError(f'{err_msg}\n' \ + 'inbound-interface not specified') + else: + if config['inbound_interface'] not in 'any' and config['inbound_interface'] not in interfaces(): + print(f'WARNING: rule "{rule}" interface "{config["inbound_interface"]}" does not exist on this system') + + return None + +def nat66_conf_to_ndp_proxy_conf(nat): + ndpproxy = { + 'interface': {} + } + if not nat or 'deleted' in nat: + # no need to verify the CLI as NAT66 is going to be deactivated + return None + # Detect and convert the default configuration of the configured interface + source_rule = dict_search('source.rule', nat) + if source_rule: + for rule,config in source_rule.items(): + interface_ndp = { + 'router': 'yes', + 'timeout': 500, + 'ttl': 30000, + 'prefix': {} + } + if config['outbound_interface'] not in ndpproxy['interface']: + ndpproxy['interface'].update({config['outbound_interface']: interface_ndp}) + for rule,config in source_rule.items(): + if config['outbound_interface'] in ndpproxy['interface']: + prefix = dict_search('translation.prefix', config) + if prefix: + nat66_prefix = config['translation']['prefix'] + if nat66_prefix not in ndpproxy['interface'][config['outbound_interface']]['prefix']: + ndpproxy['interface'][config['outbound_interface']]['prefix'].update({nat66_prefix: {'mode':'auto'}}) + + # Detect and convert the default configuration of the configured interface + destination_rule = dict_search('destination.rule', nat) + if destination_rule: + for rule,config in destination_rule.items(): + interface_ndp = { + 'router': 'yes', + 'timeout': 500, + 'ttl': 30000, + 'prefix': {} + } + if config['inbound_interface'] not in ndpproxy['interface']: + ndpproxy['interface'].update({config['inbound_interface']: interface_ndp}) + for rule,config in destination_rule.items(): + if rule['interface'] in ndpproxy['interface']: + prefix = dict_search('destination.address', config) + if prefix: + nat66_address = config['destination']['address'] + if nat66_prefix not in ndpproxy['interface'][config['inbound_interface']]['prefix']: + ndpproxy['interface'][config['inbound_interface']]['prefix'].update({nat66_prefix: {'mode':'auto'}}) + + return ndpproxy + +def generate(nat,ndp_proxy): + render(iptables_nat_config, 'firewall/nftables-nat66.tmpl', nat, permission=0o755) + if ndp_proxy == None: + return None + render(ndppd_config, 'proxy-ndp/ndppd.conf.tmpl', ndp_proxy, permission=0o755) + return None + +def apply(nat): + cmd(f'{iptables_nat_config}') + cmd('systemctl restart ndppd') + if os.path.isfile(iptables_nat_config): + os.unlink(iptables_nat_config) + + return None + +if __name__ == '__main__': + try: + check_kmod(k_mod) + c = get_config() + verify(c) + ndp_proxy = nat66_conf_to_ndp_proxy_conf(c) + generate(c,ndp_proxy) + apply(c) + except ConfigError as e: + print(e) + exit(1) diff --git a/src/migration-scripts/nat66/0-to-1 b/src/migration-scripts/nat66/0-to-1 new file mode 100755 index 000000000..2bc22061d --- /dev/null +++ b/src/migration-scripts/nat66/0-to-1 @@ -0,0 +1,76 @@ +#!/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 . + +from sys import argv,exit +from vyos.configtree import ConfigTree + +if (len(argv) < 1): + print("Must specify file name!") + exit(1) + +file_name = argv[1] + +with open(file_name, 'r') as f: + config_file = f.read() + +config = ConfigTree(config_file) + +def merge_npt(config,base,rule): + merge_base = ['nat66','source','rule',rule] + # Configure migration functions + if config.exists(base + ['description']): + tmp = config.return_value(base + ['description']) + config.set(merge_base + ['description'],value=tmp) + + if config.exists(base + ['disable']): + tmp = config.return_value(base + ['disable']) + config.set(merge_base + ['disable'],value=tmp) + + if config.exists(base + ['outbound-interface']): + tmp = config.return_value(base + ['outbound-interface']) + config.set(merge_base + ['outbound-interface'],value=tmp) + + if config.exists(base + ['source','prefix']): + tmp = config.return_value(base + ['source','prefix']) + config.set(merge_base + ['source','prefix'],value=tmp) + + if config.exists(base + ['translation','prefix']): + tmp = config.return_value(base + ['translation','prefix']) + config.set(merge_base + ['translation','prefix'],value=tmp) + + +if not config.exists(['nat']): + # Nothing to do + exit(0) +else: + if not config.exists(['nat', 'nptv6']): + exit(0) + + for rule in config.list_nodes(['nat', 'nptv6', 'rule']): + base = ['nat', 'nptv6', 'rule', rule] + # Merge 'nat nptv6' to 'nat66 source' + merge_npt(config,base,rule) + + # Delete the original NPT configuration + config.delete(['nat','nptv6']); + + try: + with open(file_name, 'w') as f: + f.write(config.to_string()) + except OSError as e: + print("Failed to save the modified config: {}".format(e)) + exit(1) + diff --git a/src/systemd/ndppd.service b/src/systemd/ndppd.service new file mode 100644 index 000000000..566172fb0 --- /dev/null +++ b/src/systemd/ndppd.service @@ -0,0 +1,15 @@ +[Unit] +Description=NDP Proxy Daemon +After=network.target vyos-router.service +ConditionPathExists=/run/ndppd/ndppd.conf +StartLimitIntervalSec=0 + +[Service] +ExecStart=/usr/sbin/ndppd -d -p /var/run/ndppd/ndppd.pid -c /var/run/ndppd/ndppd.conf +Type=forking +PIDFile=/var/run/ndppd/ndppd.pid +Restart=on-failure +RestartSec=20 + +[Install] +WantedBy=multi-user.target -- cgit v1.2.3 From af6a647bfecbc601b5c5dd7077f496b933228fce Mon Sep 17 00:00:00 2001 From: jack9603301 Date: Sun, 17 Jan 2021 18:58:36 +0800 Subject: nptv6: T2518: Improved template generation --- data/templates/proxy-ndp/ndppd.conf.tmpl | 51 ++++++++++++----- smoketest/scripts/cli/test_nat66.py | 96 +++++++++++++++++++++++++++----- src/conf_mode/nat66.py | 65 +++------------------ 3 files changed, 125 insertions(+), 87 deletions(-) diff --git a/data/templates/proxy-ndp/ndppd.conf.tmpl b/data/templates/proxy-ndp/ndppd.conf.tmpl index 9bf120b3a..a73f25a85 100644 --- a/data/templates/proxy-ndp/ndppd.conf.tmpl +++ b/data/templates/proxy-ndp/ndppd.conf.tmpl @@ -14,28 +14,49 @@ # ######################################################## +{% set global = namespace(ndppd_interfaces = [],ndppd_prefixs = []) %} -{% for i in interface %} -{%- if not interface[i].disable %} +{% if source is defined and source.rule is defined and source.rule is not none %} +{% for rule, config in source.rule.items() if config.disable is not defined %} +{% if config.outbound_interface is defined %} +{% if config.outbound_interface not in global.ndppd_interfaces %} +{% set global.ndppd_interfaces = global.ndppd_interfaces + [config.outbound_interface] %} +{% endif %} +{% if config.translation.prefix is defined %} +{% set global.ndppd_prefixs = global.ndppd_prefixs + [{'interface':config.outbound_interface,'rule':config.translation.prefix}] %} +{% endif %} +{% endif %} +{% endfor %} +{% endif %} + +{% if destination is defined and destination.rule is defined and destination.rule is not none %} +{% for rule, config in destination.rule.items() if config.disable is not defined %} +{% if config.inbound_interface is defined %} +{% if config.inbound_interface not in global.ndppd_interfaces %} +{% set global.ndppd_interfaces = global.ndppd_interfaces + [config.inbound_interface] %} +{% endif %} +{% if config.translation.address is defined %} +{% set global.ndppd_prefixs = global.ndppd_prefixs + [{'interface':config.inbound_interface,'rule':config.translation.address}] %} +{% endif %} +{% endif %} +{% endfor %} +{% endif %} + +{% for i in global.ndppd_interfaces %} proxy {{ i }} { - router {{ interface[i].router }} - timeout {{ interface[i].timeout }} - ttl {{ interface[i].ttl }} -{% for p in interface[i].prefix %} + router yes + timeout 500 + ttl 30000 +{% for map in global.ndppd_prefixs %} +{% if map['interface'] == i %} +{% set p = map['rule'] %} rule {{ p }} { -{% if interface[i].prefix[p].mode == 'auto' %} - auto -{% elif interface[i].prefix[p].mode == 'static' %} static -{% elif interface[i].prefix[p].mode == 'iface' and interface[i].prefix[p].iface %} - iface {{ interface[i].prefix[p].iface }} -{% endif %} } -{%- endfor %} +{% endif %} +{% endfor %} } -{%- endif %} - {% endfor %} diff --git a/smoketest/scripts/cli/test_nat66.py b/smoketest/scripts/cli/test_nat66.py index c1514270d..f94f77b60 100755 --- a/smoketest/scripts/cli/test_nat66.py +++ b/smoketest/scripts/cli/test_nat66.py @@ -20,10 +20,13 @@ import json import unittest from vyos.configsession import ConfigSession +from vyos.configsession import ConfigSessionError from vyos.util import cmd +from vyos.util import dict_search base_path = ['nat66'] -snat_pattern = 'nftables[?rule].rule[?chain].{chain: chain, comment: comment, prefix: { network: expr[].match.right.prefix.addr | [0], prefix: expr[].match.right.prefix.len | [0]},saddr: {network: expr[].snat.addr.prefix.addr | [0], prefix: expr[].snat.addr.prefix.len | [0]}}' +src_path = base_path + ['source'] +dst_path = base_path + ['destination'] class TestNAT66(unittest.TestCase): def setUp(self): @@ -37,26 +40,91 @@ class TestNAT66(unittest.TestCase): self.session.commit() def test_source_nat66(self): + source_prefix = 'fc00::/64' + translation_prefix = 'fc01::/64' + self.session.set(src_path + ['rule', '1', 'outbound-interface', 'eth1']) + self.session.set(src_path + ['rule', '1', 'source', 'prefix', source_prefix]) + self.session.set(src_path + ['rule', '1', 'translation', 'prefix', translation_prefix]) + + # check validate() - outbound-interface must be defined + self.session.commit() + + tmp = cmd('sudo nft -j list table ip6 nat') + data_json = jmespath.search('nftables[?rule].rule[?chain]', json.loads(tmp)) + + for idx in range(0, len(data_json)): + data = data_json[idx] + + self.assertEqual(data['chain'], 'POSTROUTING') + self.assertEqual(data['family'], 'ip6') + self.assertEqual(data['table'], 'nat') + + iface = dict_search('match.right', data['expr'][0]) + address = dict_search('match.right.prefix.addr', data['expr'][2]) + mask = dict_search('match.right.prefix.len', data['expr'][2]) + translation_address = dict_search('snat.addr.prefix.addr', data['expr'][3]) + translation_mask = dict_search('snat.addr.prefix.len', data['expr'][3]) + + self.assertEqual(iface, 'eth1') + # check for translation address + self.assertEqual(f'{translation_address}/{translation_mask}', translation_prefix) + + self.assertEqual(f'{address}/{mask}', source_prefix) + + def test_destination_nat66(self): + source_address = 'fc00::1' + translation_address = 'fc01::1' + self.session.set(dst_path + ['rule', '1', 'inbound-interface', 'eth1']) + self.session.set(dst_path + ['rule', '1', 'destination', 'address', source_address]) + self.session.set(dst_path + ['rule', '1', 'translation', 'address', translation_address]) + + # check validate() - outbound-interface must be defined + self.session.commit() + + tmp = cmd('sudo nft -j list table ip6 nat') + data_json = jmespath.search('nftables[?rule].rule[?chain]', json.loads(tmp)) + + for idx in range(0, len(data_json)): + data = data_json[idx] + + self.assertEqual(data['chain'], 'PREROUTING') + self.assertEqual(data['family'], 'ip6') + self.assertEqual(data['table'], 'nat') - path = base_path + ['source'] + iface = dict_search('match.right', data['expr'][0]) + dnat_addr = dict_search('dnat.addr', data['expr'][3]) + + self.assertEqual(dnat_addr, translation_address) + self.assertEqual(iface, 'eth1') + + def test_snat_required_translation_prefix(self): + # T2813: Ensure translation address is specified + rule = '5' source_prefix = 'fc00::/64' translation_prefix = 'fc00:2::/64' - self.session.set(path + ['rule', '1', 'outbound-interface', 'eth1']) - self.session.set(path + ['rule', '1', 'source', 'prefix', source_prefix]) - self.session.set(path + ['rule', '1', 'translation', 'prefix', translation_prefix]) + self.session.set(src_path + ['rule', rule, 'source', 'prefix', source_prefix]) # check validate() - outbound-interface must be defined + with self.assertRaises(ConfigSessionError): + self.session.commit() + self.session.set(src_path + ['rule', rule, 'outbound-interface', 'eth0']) + + # check validate() - translation address not specified + with self.assertRaises(ConfigSessionError): + self.session.commit() + + self.session.set(src_path + ['rule', rule, 'translation', 'prefix', translation_prefix]) self.session.commit() - tmp = cmd('sudo nft -j list table ip6 nat') - nftable_json = json.loads(tmp) - condensed_json = jmespath.search(snat_pattern, nftable_json)[0] - - self.assertEqual(condensed_json['comment'], 'NPT-NAT-1') - self.assertEqual(condensed_json['prefix']['network'], source_prefix.split('/')[0]) - self.assertEqual(str(condensed_json['prefix']['prefix']), source_prefix.split('/')[1]) - self.assertEqual(condensed_json['saddr']['network'], translation_prefix.split('/')[0]) - self.assertEqual(str(condensed_json['saddr']['prefix']), translation_prefix.split('/')[1]) + def test_nat66_no_rules(self): + # T3206: deleting all rules but keep the direction 'destination' or + # 'source' resulteds in KeyError: 'rule'. + # + # Test that both 'nat destination' and 'nat source' nodes can exist + # without any rule + self.session.set(src_path) + self.session.set(dst_path) + self.session.commit() if __name__ == '__main__': unittest.main(verbosity=2) diff --git a/src/conf_mode/nat66.py b/src/conf_mode/nat66.py index fdfffe335..69373c054 100755 --- a/src/conf_mode/nat66.py +++ b/src/conf_mode/nat66.py @@ -65,9 +65,10 @@ def get_config(config=None): for direction in ['source', 'destination']: if direction in nat: default_values = defaults(base + [direction, 'rule']) - for rule in nat[direction]['rule']: - nat[direction]['rule'][rule] = dict_merge(default_values, - nat[direction]['rule'][rule]) + if 'rule' in nat[direction]: + for rule in nat[direction]['rule']: + nat[direction]['rule'][rule] = dict_merge(default_values, + nat[direction]['rule'][rule]) # read in current nftable (once) for further processing tmp = cmd('nft -j list table ip6 raw') @@ -144,60 +145,9 @@ def verify(nat): return None -def nat66_conf_to_ndp_proxy_conf(nat): - ndpproxy = { - 'interface': {} - } - if not nat or 'deleted' in nat: - # no need to verify the CLI as NAT66 is going to be deactivated - return None - # Detect and convert the default configuration of the configured interface - source_rule = dict_search('source.rule', nat) - if source_rule: - for rule,config in source_rule.items(): - interface_ndp = { - 'router': 'yes', - 'timeout': 500, - 'ttl': 30000, - 'prefix': {} - } - if config['outbound_interface'] not in ndpproxy['interface']: - ndpproxy['interface'].update({config['outbound_interface']: interface_ndp}) - for rule,config in source_rule.items(): - if config['outbound_interface'] in ndpproxy['interface']: - prefix = dict_search('translation.prefix', config) - if prefix: - nat66_prefix = config['translation']['prefix'] - if nat66_prefix not in ndpproxy['interface'][config['outbound_interface']]['prefix']: - ndpproxy['interface'][config['outbound_interface']]['prefix'].update({nat66_prefix: {'mode':'auto'}}) - - # Detect and convert the default configuration of the configured interface - destination_rule = dict_search('destination.rule', nat) - if destination_rule: - for rule,config in destination_rule.items(): - interface_ndp = { - 'router': 'yes', - 'timeout': 500, - 'ttl': 30000, - 'prefix': {} - } - if config['inbound_interface'] not in ndpproxy['interface']: - ndpproxy['interface'].update({config['inbound_interface']: interface_ndp}) - for rule,config in destination_rule.items(): - if rule['interface'] in ndpproxy['interface']: - prefix = dict_search('destination.address', config) - if prefix: - nat66_address = config['destination']['address'] - if nat66_prefix not in ndpproxy['interface'][config['inbound_interface']]['prefix']: - ndpproxy['interface'][config['inbound_interface']]['prefix'].update({nat66_prefix: {'mode':'auto'}}) - - return ndpproxy - -def generate(nat,ndp_proxy): +def generate(nat): render(iptables_nat_config, 'firewall/nftables-nat66.tmpl', nat, permission=0o755) - if ndp_proxy == None: - return None - render(ndppd_config, 'proxy-ndp/ndppd.conf.tmpl', ndp_proxy, permission=0o755) + render(ndppd_config, 'proxy-ndp/ndppd.conf.tmpl', nat, permission=0o755) return None def apply(nat): @@ -213,8 +163,7 @@ if __name__ == '__main__': check_kmod(k_mod) c = get_config() verify(c) - ndp_proxy = nat66_conf_to_ndp_proxy_conf(c) - generate(c,ndp_proxy) + generate(c) apply(c) except ConfigError as e: print(e) -- cgit v1.2.3 From 35e89dcc0311514c0388b7ccab51b379113bf104 Mon Sep 17 00:00:00 2001 From: jack9603301 Date: Wed, 20 Jan 2021 16:38:36 +0800 Subject: nptv6: T2518: Support many to many DNPT(DNAT66) --- data/templates/firewall/nftables-nat66.tmpl | 8 ++++++- interface-definitions/nat66.xml.in | 14 ++++++++++-- smoketest/scripts/cli/test_nat66.py | 33 ++++++++++++++++++++++++++--- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/data/templates/firewall/nftables-nat66.tmpl b/data/templates/firewall/nftables-nat66.tmpl index af533812e..1a739cbe2 100644 --- a/data/templates/firewall/nftables-nat66.tmpl +++ b/data/templates/firewall/nftables-nat66.tmpl @@ -7,7 +7,13 @@ {% if chain == "PREROUTING" %} {% set interface = " iifname \"" + config.inbound_interface + "\"" if config.inbound_interface is defined and config.inbound_interface != 'any' else '' %} -{% set trns_address = "dnat to " + config.translation.address if config.translation is defined and config.translation.address is defined and config.translation.address is not none %} +{% if config.translation.address | is_ip_network %} +{# support 1:1 network translation #} +{% set dnat_type = "dnat prefix to " %} +{% else %} +{% set dnat_type = "dnat to " %} +{% endif %} +{% set trns_address = dnat_type + config.translation.address if config.translation is defined and config.translation.address is defined and config.translation.address is not none %} {% elif chain == "POSTROUTING" %} {% set interface = " oifname \"" + config.outbound_interface + "\"" if config.outbound_interface is defined and config.outbound_interface != 'any' else '' %} {% set trns_prefix = "snat prefix to " + config.translation.prefix if config.translation is defined and config.translation.prefix is defined and config.translation.prefix is not none %} diff --git a/interface-definitions/nat66.xml.in b/interface-definitions/nat66.xml.in index b8e8a8859..36b55f658 100644 --- a/interface-definitions/nat66.xml.in +++ b/interface-definitions/nat66.xml.in @@ -141,13 +141,18 @@ - IPv6 address to be translated + IPv6 address or prefix to be translated ipv6 IPv6 address + + ipv6net + IPv6 prefix + + @@ -160,13 +165,18 @@ - IPv6 address to translate to + IPv6 address or prefix to translate to ipv6 IPv6 address + + ipv6net + IPv6 prefix + + diff --git a/smoketest/scripts/cli/test_nat66.py b/smoketest/scripts/cli/test_nat66.py index f94f77b60..042c61ace 100755 --- a/smoketest/scripts/cli/test_nat66.py +++ b/smoketest/scripts/cli/test_nat66.py @@ -72,10 +72,10 @@ class TestNAT66(unittest.TestCase): self.assertEqual(f'{address}/{mask}', source_prefix) def test_destination_nat66(self): - source_address = 'fc00::1' + destination_address = 'fc00::1' translation_address = 'fc01::1' self.session.set(dst_path + ['rule', '1', 'inbound-interface', 'eth1']) - self.session.set(dst_path + ['rule', '1', 'destination', 'address', source_address]) + self.session.set(dst_path + ['rule', '1', 'destination', 'address', destination_address]) self.session.set(dst_path + ['rule', '1', 'translation', 'address', translation_address]) # check validate() - outbound-interface must be defined @@ -96,8 +96,35 @@ class TestNAT66(unittest.TestCase): self.assertEqual(dnat_addr, translation_address) self.assertEqual(iface, 'eth1') + + def test_destination_nat66_prefix(self): + destination_prefix = 'fc00::/64' + translation_prefix = 'fc01::/64' + self.session.set(dst_path + ['rule', '1', 'inbound-interface', 'eth1']) + self.session.set(dst_path + ['rule', '1', 'destination', 'address', destination_prefix]) + self.session.set(dst_path + ['rule', '1', 'translation', 'address', translation_prefix]) + + # check validate() - outbound-interface must be defined + self.session.commit() + + tmp = cmd('sudo nft -j list table ip6 nat') + data_json = jmespath.search('nftables[?rule].rule[?chain]', json.loads(tmp)) + + for idx in range(0, len(data_json)): + data = data_json[idx] + + self.assertEqual(data['chain'], 'PREROUTING') + self.assertEqual(data['family'], 'ip6') + self.assertEqual(data['table'], 'nat') + + iface = dict_search('match.right', data['expr'][0]) + translation_address = dict_search('dnat.addr.prefix.addr', data['expr'][3]) + translation_mask = dict_search('dnat.addr.prefix.len', data['expr'][3]) + + self.assertEqual(f'{translation_address}/{translation_mask}', translation_prefix) + self.assertEqual(iface, 'eth1') - def test_snat_required_translation_prefix(self): + def test_source_nat66_required_translation_prefix(self): # T2813: Ensure translation address is specified rule = '5' source_prefix = 'fc00::/64' -- cgit v1.2.3 From f58a41bccecd6765eb9bc376809197fd2fb36b0c Mon Sep 17 00:00:00 2001 From: jack9603301 Date: Thu, 21 Jan 2021 01:20:02 +0800 Subject: nptv6: T2518: DNPT does not need NDP agent --- data/templates/proxy-ndp/ndppd.conf.tmpl | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/data/templates/proxy-ndp/ndppd.conf.tmpl b/data/templates/proxy-ndp/ndppd.conf.tmpl index a73f25a85..f9485a1cb 100644 --- a/data/templates/proxy-ndp/ndppd.conf.tmpl +++ b/data/templates/proxy-ndp/ndppd.conf.tmpl @@ -30,19 +30,6 @@ {% endfor %} {% endif %} -{% if destination is defined and destination.rule is defined and destination.rule is not none %} -{% for rule, config in destination.rule.items() if config.disable is not defined %} -{% if config.inbound_interface is defined %} -{% if config.inbound_interface not in global.ndppd_interfaces %} -{% set global.ndppd_interfaces = global.ndppd_interfaces + [config.inbound_interface] %} -{% endif %} -{% if config.translation.address is defined %} -{% set global.ndppd_prefixs = global.ndppd_prefixs + [{'interface':config.inbound_interface,'rule':config.translation.address}] %} -{% endif %} -{% endif %} -{% endfor %} -{% endif %} - {% for i in global.ndppd_interfaces %} proxy {{ i }} { -- cgit v1.2.3 From 01e1759d16afbc9f95b64da76eee97fc06729dc8 Mon Sep 17 00:00:00 2001 From: jack9603301 Date: Thu, 21 Jan 2021 01:25:43 +0800 Subject: nptv6: T2518: outbound_interface cannot be any, inbound_interface can be any --- data/templates/firewall/nftables-nat66.tmpl | 2 +- interface-definitions/nat66.xml.in | 1 + src/conf_mode/nat66.py | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/data/templates/firewall/nftables-nat66.tmpl b/data/templates/firewall/nftables-nat66.tmpl index 1a739cbe2..80150c7a4 100644 --- a/data/templates/firewall/nftables-nat66.tmpl +++ b/data/templates/firewall/nftables-nat66.tmpl @@ -15,7 +15,7 @@ {% endif %} {% set trns_address = dnat_type + config.translation.address if config.translation is defined and config.translation.address is defined and config.translation.address is not none %} {% elif chain == "POSTROUTING" %} -{% set interface = " oifname \"" + config.outbound_interface + "\"" if config.outbound_interface is defined and config.outbound_interface != 'any' else '' %} +{% set interface = " oifname \"" + config.outbound_interface + "\"" if config.outbound_interface is defined else '' %} {% set trns_prefix = "snat prefix to " + config.translation.prefix if config.translation is defined and config.translation.prefix is defined and config.translation.prefix is not none %} {% endif %} {% set comment = "NPT-NAT-" + rule %} diff --git a/interface-definitions/nat66.xml.in b/interface-definitions/nat66.xml.in index 36b55f658..b56a76b4c 100644 --- a/interface-definitions/nat66.xml.in +++ b/interface-definitions/nat66.xml.in @@ -130,6 +130,7 @@ Inbound interface of NAT traffic + any diff --git a/src/conf_mode/nat66.py b/src/conf_mode/nat66.py index 69373c054..a5c74259f 100755 --- a/src/conf_mode/nat66.py +++ b/src/conf_mode/nat66.py @@ -117,7 +117,7 @@ def verify(nat): raise ConfigError(f'{err_msg}\n' \ 'outbound-interface not specified') else: - if config['outbound_interface'] not in 'any' and config['outbound_interface'] not in interfaces(): + if config['outbound_interface'] not in interfaces(): print(f'WARNING: rule "{rule}" interface "{config["outbound_interface"]}" does not exist on this system') -- cgit v1.2.3 From 88a6a034b1cb6a1a43c689281df92831d82c996c Mon Sep 17 00:00:00 2001 From: jack9603301 Date: Thu, 21 Jan 2021 22:26:40 +0800 Subject: nptv6: T2518: Maintain the consistency of terms in the CLI configuration file, and use nat66 terms completely --- interface-definitions/nat66.xml.in | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/interface-definitions/nat66.xml.in b/interface-definitions/nat66.xml.in index b56a76b4c..b45ebc0a8 100644 --- a/interface-definitions/nat66.xml.in +++ b/interface-definitions/nat66.xml.in @@ -13,7 +13,7 @@ - SNPTv6 rule number + Source NAT66 rule number 1-999999 Number for this rule @@ -43,7 +43,7 @@ - Outbound interface of NAT traffic + Outbound interface of NAT66 traffic @@ -98,7 +98,7 @@ - DNPTv6 rule number + Destination NAT66 rule number 1-999999 Number for this rule @@ -116,7 +116,7 @@ - Disable DNPT rule + Disable NAT66 rule @@ -128,7 +128,7 @@ - Inbound interface of NAT traffic + Inbound interface of NAT66 traffic any -- cgit v1.2.3 From d05593e68ff0978c278b13a42f246edcfad3902d Mon Sep 17 00:00:00 2001 From: jack9603301 Date: Fri, 22 Jan 2021 13:16:55 +0800 Subject: nptv6: T2518: Optimized implementation --- data/templates/proxy-ndp/ndppd.conf.tmpl | 4 ++-- src/conf_mode/nat66.py | 14 ++++++++++---- src/systemd/ndppd.service | 6 +++--- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/data/templates/proxy-ndp/ndppd.conf.tmpl b/data/templates/proxy-ndp/ndppd.conf.tmpl index f9485a1cb..6ef9f3f8b 100644 --- a/data/templates/proxy-ndp/ndppd.conf.tmpl +++ b/data/templates/proxy-ndp/ndppd.conf.tmpl @@ -37,8 +37,8 @@ proxy {{ i }} { timeout 500 ttl 30000 {% for map in global.ndppd_prefixs %} -{% if map['interface'] == i %} -{% set p = map['rule'] %} +{% if map.interface == i %} +{% set p = map.rule %} rule {{ p }} { static } diff --git a/src/conf_mode/nat66.py b/src/conf_mode/nat66.py index a5c74259f..7190961e8 100755 --- a/src/conf_mode/nat66.py +++ b/src/conf_mode/nat66.py @@ -151,10 +151,16 @@ def generate(nat): return None def apply(nat): - cmd(f'{iptables_nat_config}') - cmd('systemctl restart ndppd') - if os.path.isfile(iptables_nat_config): - os.unlink(iptables_nat_config) + if not nat: + return None + else: + cmd(f'{iptables_nat_config}') + if 'deleted' in nat or not dict_search('source.rule', nat): + cmd('systemctl stop ndppd') + else: + cmd('systemctl restart ndppd') + if os.path.isfile(iptables_nat_config): + os.unlink(iptables_nat_config) return None diff --git a/src/systemd/ndppd.service b/src/systemd/ndppd.service index 566172fb0..db471c518 100644 --- a/src/systemd/ndppd.service +++ b/src/systemd/ndppd.service @@ -1,13 +1,13 @@ [Unit] Description=NDP Proxy Daemon -After=network.target vyos-router.service +After=vyos-router.service ConditionPathExists=/run/ndppd/ndppd.conf StartLimitIntervalSec=0 [Service] -ExecStart=/usr/sbin/ndppd -d -p /var/run/ndppd/ndppd.pid -c /var/run/ndppd/ndppd.conf +ExecStart=/usr/sbin/ndppd -d -p /run/ndppd/ndppd.pid -c /run/ndppd/ndppd.conf Type=forking -PIDFile=/var/run/ndppd/ndppd.pid +PIDFile=/run/ndppd/ndppd.pid Restart=on-failure RestartSec=20 -- cgit v1.2.3 From d5a6817f5dc8c6b6bc015705330efbd02750d801 Mon Sep 17 00:00:00 2001 From: jack9603301 Date: Sat, 23 Jan 2021 02:08:12 +0800 Subject: nptv6: T2518: Delete the ndppd configuration file when the ndppd service does not need to be enabled --- src/conf_mode/nat66.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/conf_mode/nat66.py b/src/conf_mode/nat66.py index 7190961e8..f58db04e0 100755 --- a/src/conf_mode/nat66.py +++ b/src/conf_mode/nat66.py @@ -153,14 +153,15 @@ def generate(nat): def apply(nat): if not nat: return None + cmd(f'{iptables_nat_config}') + if 'deleted' in nat or not dict_search('source.rule', nat): + cmd('systemctl stop ndppd') + if os.path.isfile(ndppd_config): + os.unlink(ndppd_config) else: - cmd(f'{iptables_nat_config}') - if 'deleted' in nat or not dict_search('source.rule', nat): - cmd('systemctl stop ndppd') - else: - cmd('systemctl restart ndppd') - if os.path.isfile(iptables_nat_config): - os.unlink(iptables_nat_config) + cmd('systemctl restart ndppd') + if os.path.isfile(iptables_nat_config): + os.unlink(iptables_nat_config) return None -- cgit v1.2.3 From 9d2ce9c1d0078a16c698c7c5ce503044c780b04d Mon Sep 17 00:00:00 2001 From: JACK Date: Sat, 23 Jan 2021 03:58:48 +0800 Subject: nptv6: T2518: Remove redundant paths for migration scripts --- src/migration-scripts/nat66/0-to-1 | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/migration-scripts/nat66/0-to-1 b/src/migration-scripts/nat66/0-to-1 index 2bc22061d..67024cf77 100755 --- a/src/migration-scripts/nat66/0-to-1 +++ b/src/migration-scripts/nat66/0-to-1 @@ -52,25 +52,22 @@ def merge_npt(config,base,rule): config.set(merge_base + ['translation','prefix'],value=tmp) -if not config.exists(['nat']): +if not config.exists(['nat', 'nptv6']): # Nothing to do exit(0) -else: - if not config.exists(['nat', 'nptv6']): - exit(0) - - for rule in config.list_nodes(['nat', 'nptv6', 'rule']): - base = ['nat', 'nptv6', 'rule', rule] - # Merge 'nat nptv6' to 'nat66 source' - merge_npt(config,base,rule) - - # Delete the original NPT configuration - config.delete(['nat','nptv6']); - - try: - with open(file_name, 'w') as f: - f.write(config.to_string()) - except OSError as e: - print("Failed to save the modified config: {}".format(e)) - exit(1) + +for rule in config.list_nodes(['nat', 'nptv6', 'rule']): + base = ['nat', 'nptv6', 'rule', rule] + # Merge 'nat nptv6' to 'nat66 source' + merge_npt(config,base,rule) + +# Delete the original NPT configuration +config.delete(['nat','nptv6']); + +try: + with open(file_name, 'w') as f: + f.write(config.to_string()) +except OSError as e: + print("Failed to save the modified config: {}".format(e)) + exit(1) -- cgit v1.2.3 From c7a967b2d9c83fe4825d1ca672a9aa15615c5a99 Mon Sep 17 00:00:00 2001 From: jack9603301 Date: Sat, 23 Jan 2021 17:19:53 +0800 Subject: nptv6: T2518: Remove trailing spaces --- data/templates/firewall/nftables-nat66.tmpl | 1 - data/templates/proxy-ndp/ndppd.conf.tmpl | 1 - src/migration-scripts/nat66/0-to-1 | 1 - 3 files changed, 3 deletions(-) diff --git a/data/templates/firewall/nftables-nat66.tmpl b/data/templates/firewall/nftables-nat66.tmpl index 80150c7a4..45506ca09 100644 --- a/data/templates/firewall/nftables-nat66.tmpl +++ b/data/templates/firewall/nftables-nat66.tmpl @@ -104,4 +104,3 @@ add rule ip6 raw NAT_CONNTRACK counter accept {{ nptv6_rule(rule, config, 'POSTROUTING') }} {% endfor %} {% endif %} - diff --git a/data/templates/proxy-ndp/ndppd.conf.tmpl b/data/templates/proxy-ndp/ndppd.conf.tmpl index 6ef9f3f8b..844b644b7 100644 --- a/data/templates/proxy-ndp/ndppd.conf.tmpl +++ b/data/templates/proxy-ndp/ndppd.conf.tmpl @@ -45,5 +45,4 @@ proxy {{ i }} { {% endif %} {% endfor %} } - {% endfor %} diff --git a/src/migration-scripts/nat66/0-to-1 b/src/migration-scripts/nat66/0-to-1 index 67024cf77..602c5f5c1 100755 --- a/src/migration-scripts/nat66/0-to-1 +++ b/src/migration-scripts/nat66/0-to-1 @@ -70,4 +70,3 @@ try: except OSError as e: print("Failed to save the modified config: {}".format(e)) exit(1) - -- cgit v1.2.3 From af91cdbf23157f7b6b7696de9bc27dafca6d7dee Mon Sep 17 00:00:00 2001 From: jack9603301 Date: Sat, 23 Jan 2021 21:44:44 +0800 Subject: nptv6: T2518: Use better variable names --- data/templates/proxy-ndp/ndppd.conf.tmpl | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/data/templates/proxy-ndp/ndppd.conf.tmpl b/data/templates/proxy-ndp/ndppd.conf.tmpl index 844b644b7..4393be169 100644 --- a/data/templates/proxy-ndp/ndppd.conf.tmpl +++ b/data/templates/proxy-ndp/ndppd.conf.tmpl @@ -30,16 +30,15 @@ {% endfor %} {% endif %} -{% for i in global.ndppd_interfaces %} +{% for interface in global.ndppd_interfaces %} -proxy {{ i }} { +proxy {{ interface }} { router yes timeout 500 ttl 30000 {% for map in global.ndppd_prefixs %} -{% if map.interface == i %} -{% set p = map.rule %} - rule {{ p }} { +{% if map.interface == interface %} + rule {{ map.rule }} { static } {% endif %} -- cgit v1.2.3 From 41b4f00972917ea950842d53eaede0a4308bc663 Mon Sep 17 00:00:00 2001 From: jack9603301 Date: Sun, 24 Jan 2021 13:18:24 +0800 Subject: nptv6: T2518: Delete redundant blank lines --- data/templates/firewall/nftables-nat66.tmpl | 21 --------------------- data/templates/proxy-ndp/ndppd.conf.tmpl | 3 --- smoketest/scripts/cli/test_nat66.py | 1 - src/conf_mode/nat66.py | 9 ++------- src/migration-scripts/nat66/0-to-1 | 1 - 5 files changed, 2 insertions(+), 33 deletions(-) diff --git a/data/templates/firewall/nftables-nat66.tmpl b/data/templates/firewall/nftables-nat66.tmpl index 45506ca09..b1a8f7a16 100644 --- a/data/templates/firewall/nftables-nat66.tmpl +++ b/data/templates/firewall/nftables-nat66.tmpl @@ -1,10 +1,8 @@ #!/usr/sbin/nft -f {% macro nptv6_rule(rule,config, chain) %} - {% set src_prefix = "ip6 saddr " + config.source.prefix if config.source is defined and config.source.prefix is defined and config.source.prefix is not none %} {% set dest_address = "ip6 daddr " + config.destination.address if config.destination is defined and config.destination.address is defined and config.destination.address is not none %} - {% if chain == "PREROUTING" %} {% set interface = " iifname \"" + config.inbound_interface + "\"" if config.inbound_interface is defined and config.inbound_interface != 'any' else '' %} {% if config.translation.address | is_ip_network %} @@ -26,49 +24,35 @@ {% set output = "add rule ip6 nat " + chain + interface %} {# Count packets #} {% set output = output + " counter" %} - {# Special handling of log option, we must repeat the entire rule before the #} {# NAT translation options are added, this is essential #} {% if log %} {% set log_output = output + " log prefix \"" + log + "\" comment \"" + comment + "\"" %} {% endif %} - {% if src_prefix %} {% set output = output + " " + src_prefix %} {% endif %} - - {% if dest_address %} {% set output = output + " " + dest_address %} {% endif %} - {% if trns_prefix %} {% set output = output + " " + trns_prefix %} {% endif %} - {% if trns_address %} {% set output = output + " " + trns_address %} {% endif %} - - {% if comment %} {% set output = output + " comment \"" + comment + "\"" %} {% endif %} - {{ log_output if log_output }} {{ output }} {% endmacro %} # Start with clean NAT table flush table ip6 nat - {% if helper_functions == 'remove' %} {# NAT if going to be disabled - remove rules and targets from nftables #} - - - {% set base_command = "delete rule ip6 raw" %} - {{base_command}} PREROUTING handle {{ pre_ct_conntrack }} {{base_command}} OUTPUT handle {{ out_ct_conntrack }} @@ -78,13 +62,9 @@ delete chain ip6 raw NAT_CONNTRACK {# NAT if enabled - add targets to nftables #} add chain ip6 raw NAT_CONNTRACK add rule ip6 raw NAT_CONNTRACK counter accept - {% set base_command = "add rule ip6 raw" %} - - {{ base_command }} PREROUTING position {{ pre_ct_conntrack }} counter jump NAT_CONNTRACK {{ base_command }} OUTPUT position {{ out_ct_conntrack }} counter jump NAT_CONNTRACK - {% endif %} # @@ -95,7 +75,6 @@ add rule ip6 raw NAT_CONNTRACK counter accept {{ nptv6_rule(rule, config, 'PREROUTING') }} {% endfor %} {% endif %} - # # Source NAT66 rules build up here # diff --git a/data/templates/proxy-ndp/ndppd.conf.tmpl b/data/templates/proxy-ndp/ndppd.conf.tmpl index 4393be169..0137d8135 100644 --- a/data/templates/proxy-ndp/ndppd.conf.tmpl +++ b/data/templates/proxy-ndp/ndppd.conf.tmpl @@ -15,8 +15,6 @@ ######################################################## {% set global = namespace(ndppd_interfaces = [],ndppd_prefixs = []) %} - - {% if source is defined and source.rule is defined and source.rule is not none %} {% for rule, config in source.rule.items() if config.disable is not defined %} {% if config.outbound_interface is defined %} @@ -31,7 +29,6 @@ {% endif %} {% for interface in global.ndppd_interfaces %} - proxy {{ interface }} { router yes timeout 500 diff --git a/smoketest/scripts/cli/test_nat66.py b/smoketest/scripts/cli/test_nat66.py index 042c61ace..ccc4196e0 100755 --- a/smoketest/scripts/cli/test_nat66.py +++ b/smoketest/scripts/cli/test_nat66.py @@ -68,7 +68,6 @@ class TestNAT66(unittest.TestCase): self.assertEqual(iface, 'eth1') # check for translation address self.assertEqual(f'{translation_address}/{translation_mask}', translation_prefix) - self.assertEqual(f'{address}/{mask}', source_prefix) def test_destination_nat66(self): diff --git a/src/conf_mode/nat66.py b/src/conf_mode/nat66.py index f58db04e0..b90939a2f 100755 --- a/src/conf_mode/nat66.py +++ b/src/conf_mode/nat66.py @@ -80,19 +80,16 @@ def get_config(config=None): if not conf.exists(base): nat['helper_functions'] = 'remove' - nat['pre_ct_conntrack'] = get_handler(condensed_json, 'PREROUTING', 'NAT_CONNTRACK') nat['out_ct_conntrack'] = get_handler(condensed_json, 'OUTPUT','NAT_CONNTRACK') - nat['deleted'] = '' - return nat # check if NAT66 connection tracking helpers need to be set up - this has to # be done only once if not get_handler(condensed_json, 'PREROUTING', 'NAT_CONNTRACK'): nat['helper_functions'] = 'add' - + # Retrieve current table handler positions nat['pre_ct_conntrack'] = get_handler(condensed_json, 'PREROUTING', 'VYATTA_CT_PREROUTING_HOOK') nat['out_ct_conntrack'] = get_handler(condensed_json, 'OUTPUT','VYATTA_CT_OUTPUT_HOOK') @@ -120,18 +117,16 @@ def verify(nat): if config['outbound_interface'] not in interfaces(): print(f'WARNING: rule "{rule}" interface "{config["outbound_interface"]}" does not exist on this system') - prefix = dict_search('translation.prefix', config) if prefix != None: if not is_ipv6(prefix): raise ConfigError(f'Warning: IPv6 prefix {prefix} is not a valid address prefix') - + prefix = dict_search('source.prefix', config) if prefix != None: if not is_ipv6(prefix): raise ConfigError(f'{err_msg} source-prefix not specified') - if dict_search('destination.rule', nat): for rule, config in dict_search('destination.rule', nat).items(): err_msg = f'Destination NAT66 configuration error in rule {rule}:' diff --git a/src/migration-scripts/nat66/0-to-1 b/src/migration-scripts/nat66/0-to-1 index 602c5f5c1..74d64c07b 100755 --- a/src/migration-scripts/nat66/0-to-1 +++ b/src/migration-scripts/nat66/0-to-1 @@ -51,7 +51,6 @@ def merge_npt(config,base,rule): tmp = config.return_value(base + ['translation','prefix']) config.set(merge_base + ['translation','prefix'],value=tmp) - if not config.exists(['nat', 'nptv6']): # Nothing to do exit(0) -- cgit v1.2.3 From 455d77a674794951cff6fdb84f33b2022050a0d6 Mon Sep 17 00:00:00 2001 From: jack9603301 Date: Sun, 24 Jan 2021 13:43:17 +0800 Subject: nptv6: T2515: Modify the field order of ndppd --- src/systemd/ndppd.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systemd/ndppd.service b/src/systemd/ndppd.service index db471c518..5790d37f1 100644 --- a/src/systemd/ndppd.service +++ b/src/systemd/ndppd.service @@ -5,8 +5,8 @@ ConditionPathExists=/run/ndppd/ndppd.conf StartLimitIntervalSec=0 [Service] -ExecStart=/usr/sbin/ndppd -d -p /run/ndppd/ndppd.pid -c /run/ndppd/ndppd.conf Type=forking +ExecStart=/usr/sbin/ndppd -d -p /run/ndppd/ndppd.pid -c /run/ndppd/ndppd.conf PIDFile=/run/ndppd/ndppd.pid Restart=on-failure RestartSec=20 -- cgit v1.2.3 From e5388766529756a0d1ec58f0ecfad456ce28c96c Mon Sep 17 00:00:00 2001 From: jack9603301 Date: Sun, 24 Jan 2021 14:46:04 +0800 Subject: nptv6: T2518: Remove obsolete references --- src/conf_mode/nat66.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/conf_mode/nat66.py b/src/conf_mode/nat66.py index b90939a2f..dc3cd550c 100755 --- a/src/conf_mode/nat66.py +++ b/src/conf_mode/nat66.py @@ -30,7 +30,6 @@ from vyos.util import dict_search from vyos.template import is_ipv6 from vyos.xml import defaults from vyos import ConfigError -import platform from vyos import airbag airbag.enable() -- cgit v1.2.3