Skip to content

Commit 18ee242

Browse files
authored
Merge pull request #2578 from sever-sever/nat64
T160: add NAT64
2 parents 99c674c + 336bb5a commit 18ee242

File tree

5 files changed

+438
-0
lines changed

5 files changed

+438
-0
lines changed

debian/control

+3
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ Depends:
249249
libstrongswan-standard-plugins (>=5.9),
250250
python3-vici (>= 5.7.2),
251251
# End "vpn ipsec"
252+
# For "nat64"
253+
jool,
254+
# End "nat64"
252255
# For nat66
253256
ndppd,
254257
# End nat66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!-- include start from nat64/protocol.xml.i -->
2+
<node name="protocol">
3+
<properties>
4+
<help>Apply translation address to a specfic protocol</help>
5+
</properties>
6+
<children>
7+
<leafNode name="tcp">
8+
<properties>
9+
<help>Transmission Control Protocol</help>
10+
<valueless/>
11+
</properties>
12+
</leafNode>
13+
<leafNode name="udp">
14+
<properties>
15+
<help>User Datagram Protocol</help>
16+
<valueless/>
17+
</properties>
18+
</leafNode>
19+
<leafNode name="icmp">
20+
<properties>
21+
<help>Internet Control Message Protocol</help>
22+
<valueless/>
23+
</properties>
24+
</leafNode>
25+
</children>
26+
</node>
27+
<!-- include end -->

interface-definitions/nat64.xml.in

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?xml version="1.0"?>
2+
<interfaceDefinition>
3+
<node name="nat64" owner="${vyos_conf_scripts_dir}/nat64.py">
4+
<properties>
5+
<help>IPv6-to-IPv4 Network Address Translation (NAT64) Settings</help>
6+
<priority>501</priority>
7+
</properties>
8+
<children>
9+
<node name="source">
10+
<properties>
11+
<help>IPv6 source to IPv4 destination address translation</help>
12+
</properties>
13+
<children>
14+
<tagNode name="rule">
15+
<properties>
16+
<help>Source NAT64 rule number</help>
17+
<valueHelp>
18+
<format>u32:1-999999</format>
19+
<description>Number for this rule</description>
20+
</valueHelp>
21+
<constraint>
22+
<validator name="numeric" argument="--range 1-999999"/>
23+
</constraint>
24+
<constraintErrorMessage>NAT64 rule number must be between 1 and 999999</constraintErrorMessage>
25+
</properties>
26+
<children>
27+
#include <include/generic-description.xml.i>
28+
#include <include/generic-disable-node.xml.i>
29+
<node name="source">
30+
<properties>
31+
<help>IPv6 source prefix options</help>
32+
</properties>
33+
<children>
34+
<leafNode name="prefix">
35+
<properties>
36+
<help>IPv6 prefix to be translated</help>
37+
<valueHelp>
38+
<format>ipv6net</format>
39+
<description>IPv6 prefix</description>
40+
</valueHelp>
41+
<constraint>
42+
<validator name="ipv6-prefix"/>
43+
</constraint>
44+
</properties>
45+
</leafNode>
46+
</children>
47+
</node>
48+
<node name="translation">
49+
<properties>
50+
<help>Translated IPv4 address options</help>
51+
</properties>
52+
<children>
53+
<tagNode name="pool">
54+
<properties>
55+
<help>Translation IPv4 pool number</help>
56+
<valueHelp>
57+
<format>u32:1-999999</format>
58+
<description>Number for this rule</description>
59+
</valueHelp>
60+
<constraint>
61+
<validator name="numeric" argument="--range 1-999999"/>
62+
</constraint>
63+
<constraintErrorMessage>NAT64 pool number must be between 1 and 999999</constraintErrorMessage>
64+
</properties>
65+
<children>
66+
#include <include/generic-description.xml.i>
67+
#include <include/generic-disable-node.xml.i>
68+
#include <include/nat-translation-port.xml.i>
69+
#include <include/nat64/protocol.xml.i>
70+
<leafNode name="address">
71+
<properties>
72+
<help>IPv4 address or prefix to translate to</help>
73+
<valueHelp>
74+
<format>ipv4</format>
75+
<description>IPv4 address</description>
76+
</valueHelp>
77+
<valueHelp>
78+
<format>ipv4net</format>
79+
<description>IPv4 prefix</description>
80+
</valueHelp>
81+
<constraint>
82+
<validator name="ipv4-address"/>
83+
<validator name="ipv4-prefix"/>
84+
</constraint>
85+
</properties>
86+
</leafNode>
87+
</children>
88+
</tagNode>
89+
</children>
90+
</node>
91+
</children>
92+
</tagNode>
93+
</children>
94+
</node>
95+
</children>
96+
</node>
97+
</interfaceDefinition>

smoketest/scripts/cli/test_nat64.py

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Copyright (C) 2023 VyOS maintainers and contributors
4+
#
5+
# This program is free software; you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License version 2 or later as
7+
# published by the Free Software Foundation.
8+
#
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
import json
18+
import os
19+
import unittest
20+
21+
from base_vyostest_shim import VyOSUnitTestSHIM
22+
from vyos.configsession import ConfigSessionError
23+
from vyos.utils.process import cmd
24+
from vyos.utils.dict import dict_search
25+
26+
base_path = ['nat64']
27+
src_path = base_path + ['source']
28+
29+
jool_nat64_config = '/run/jool/instance-100.json'
30+
31+
32+
class TestNAT64(VyOSUnitTestSHIM.TestCase):
33+
@classmethod
34+
def setUpClass(cls):
35+
super(TestNAT64, cls).setUpClass()
36+
37+
# ensure we can also run this test on a live system - so lets clean
38+
# out the current configuration :)
39+
cls.cli_delete(cls, base_path)
40+
41+
def tearDown(self):
42+
self.cli_delete(base_path)
43+
self.cli_commit()
44+
self.assertFalse(os.path.exists(jool_nat64_config))
45+
46+
def test_snat64(self):
47+
rule = '100'
48+
translation_rule = '10'
49+
prefix_v6 = '64:ff9b::/96'
50+
pool = '192.0.2.10'
51+
pool_port = '1-65535'
52+
53+
self.cli_set(src_path + ['rule', rule, 'source', 'prefix', prefix_v6])
54+
self.cli_set(
55+
src_path
56+
+ ['rule', rule, 'translation', 'pool', translation_rule, 'address', pool]
57+
)
58+
self.cli_set(
59+
src_path
60+
+ ['rule', rule, 'translation', 'pool', translation_rule, 'port', pool_port]
61+
)
62+
self.cli_commit()
63+
64+
# Load the JSON file
65+
with open(f'/run/jool/instance-{rule}.json', 'r') as json_file:
66+
config_data = json.load(json_file)
67+
68+
# Assertions based on the content of the JSON file
69+
self.assertEqual(config_data['instance'], f'instance-{rule}')
70+
self.assertEqual(config_data['framework'], 'netfilter')
71+
self.assertEqual(config_data['global']['pool6'], prefix_v6)
72+
self.assertTrue(config_data['global']['manually-enabled'])
73+
74+
# Check the pool4 entries
75+
pool4_entries = config_data.get('pool4', [])
76+
self.assertIsInstance(pool4_entries, list)
77+
self.assertGreater(len(pool4_entries), 0)
78+
79+
for entry in pool4_entries:
80+
self.assertIn('protocol', entry)
81+
self.assertIn('prefix', entry)
82+
self.assertIn('port range', entry)
83+
84+
protocol = entry['protocol']
85+
prefix = entry['prefix']
86+
port_range = entry['port range']
87+
88+
if protocol == 'ICMP':
89+
self.assertEqual(prefix, pool)
90+
self.assertEqual(port_range, pool_port)
91+
elif protocol == 'UDP':
92+
self.assertEqual(prefix, pool)
93+
self.assertEqual(port_range, pool_port)
94+
elif protocol == 'TCP':
95+
self.assertEqual(prefix, pool)
96+
self.assertEqual(port_range, pool_port)
97+
else:
98+
self.fail(f'Unexpected protocol: {protocol}')
99+
100+
101+
if __name__ == '__main__':
102+
unittest.main(verbosity=2)

0 commit comments

Comments
 (0)