summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjack9603301 <jack9603301@163.com>2021-01-17 18:58:36 +0800
committerjack9603301 <jack9603301@163.com>2021-01-23 21:45:31 +0800
commitaf6a647bfecbc601b5c5dd7077f496b933228fce (patch)
tree63eedd231e9be0e70237072faabb31b4a95a1433
parentb047855b80754d78cab4d3161ad0e97c21f479bc (diff)
downloadvyos-1x-af6a647bfecbc601b5c5dd7077f496b933228fce.tar.gz
vyos-1x-af6a647bfecbc601b5c5dd7077f496b933228fce.zip
nptv6: T2518: Improved template generation
-rw-r--r--data/templates/proxy-ndp/ndppd.conf.tmpl51
-rwxr-xr-xsmoketest/scripts/cli/test_nat66.py96
-rwxr-xr-xsrc/conf_mode/nat66.py65
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)