Skip to content

Commit 336bb5a

Browse files
committed
T160: Rebase and fixes for NAT64
- Update the base (rebase) - Move include/nat64-protocol.xml.i => include/nat64/protocol.xml.i - Delete unwanted `write_json`, use `write_file` instead - Remove unnecessary deleting of default values for tagNodes T2665 - Add smoketest Example: ``` set interfaces ethernet eth0 address '192.168.122.14/24' set interfaces ethernet eth0 address '192.168.122.10/24' set interfaces ethernet eth2 address '2001:db8::1/64' set nat64 source rule 100 source prefix '64:ff9b::/96' set nat64 source rule 100 translation pool 10 address '192.168.122.10' set nat64 source rule 100 translation pool 10 port '1-65535' ```
1 parent 7d49f70 commit 336bb5a

File tree

5 files changed

+108
-37
lines changed

5 files changed

+108
-37
lines changed

interface-definitions/include/nat64-protocol.xml.i interface-definitions/include/nat64/protocol.xml.i

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- include start from nat64-protocol.xml.i -->
1+
<!-- include start from nat64/protocol.xml.i -->
22
<node name="protocol">
33
<properties>
44
<help>Apply translation address to a specfic protocol</help>

interface-definitions/nat64.xml.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
#include <include/generic-description.xml.i>
6767
#include <include/generic-disable-node.xml.i>
6868
#include <include/nat-translation-port.xml.i>
69-
#include <include/nat64-protocol.xml.i>
69+
#include <include/nat64/protocol.xml.i>
7070
<leafNode name="address">
7171
<properties>
7272
<help>IPv4 address or prefix to translate to</help>

python/vyos/utils/file.py

-14
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,6 @@ def read_json(fname, defaultonfailure=None):
8383
return defaultonfailure
8484
raise e
8585

86-
def write_json(fname, data, indent=2, defaultonfailure=None):
87-
"""
88-
encode data to json and write to a file
89-
should defaultonfailure be not None, it is returned on failure to write
90-
"""
91-
import json
92-
try:
93-
with open(fname, 'w') as f:
94-
json.dump(data, f, indent=indent)
95-
except Exception as e:
96-
if defaultonfailure is not None:
97-
return defaultonfailure
98-
raise e
99-
10086
def chown(path, user, group):
10187
""" change file/directory owner """
10288
from pwd import getpwnam

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)

src/conf_mode/nat64.py

+4-21
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
import re
2222

2323
from ipaddress import IPv6Network
24+
from json import dumps as json_write
2425

2526
from vyos import ConfigError
2627
from vyos import airbag
2728
from vyos.config import Config
2829
from vyos.configdict import dict_merge
2930
from vyos.configdict import is_node_changed
3031
from vyos.utils.dict import dict_search
31-
from vyos.utils.file import write_json
32+
from vyos.utils.file import write_file
3233
from vyos.utils.kernel import check_kmod
3334
from vyos.utils.process import cmd
3435
from vyos.utils.process import run
@@ -40,27 +41,12 @@
4041

4142

4243
def get_config(config: Config | None = None) -> None:
43-
""" """
4444
if config is None:
4545
config = Config()
4646

4747
base = ["nat64"]
4848
nat64 = config.get_config_dict(base, key_mangling=("-", "_"), get_first_key=True)
4949

50-
# T2665: we must add the tagNode defaults individually until this is
51-
# moved to the base class
52-
for direction in ["source"]:
53-
if direction in nat64:
54-
default_values = defaults(base + [direction, "rule"])
55-
if "rule" in nat64[direction]:
56-
for rule in nat64[direction]["rule"]:
57-
nat64[direction]["rule"][rule] = dict_merge(
58-
default_values, nat64[direction]["rule"][rule]
59-
)
60-
61-
# Only support netfilter for now
62-
nat64[direction]["rule"][rule]["mode"] = "netfilter"
63-
6450
base_src = base + ["source", "rule"]
6551

6652
# Load in existing instances so we can destroy any unknown
@@ -95,15 +81,14 @@ def get_config(config: Config | None = None) -> None:
9581

9682

9783
def verify(nat64) -> None:
98-
""" """
9984
if not nat64:
10085
# no need to verify the CLI as nat64 is going to be deactivated
10186
return
10287

10388
if dict_search("source.rule", nat64):
10489
# Ensure only 1 netfilter instance per namespace
10590
nf_rules = filter(
106-
lambda i: "deleted" not in i and i["mode"] == "netfilter",
91+
lambda i: "deleted" not in i and i.get('mode') == "netfilter",
10792
nat64["source"]["rule"].values(),
10893
)
10994
next(nf_rules, None) # Discard the first element
@@ -138,7 +123,6 @@ def verify(nat64) -> None:
138123

139124

140125
def generate(nat64) -> None:
141-
""" """
142126
os.makedirs(JOOL_CONFIG_DIR, exist_ok=True)
143127

144128
if dict_search("source.rule", nat64):
@@ -183,11 +167,10 @@ def generate(nat64) -> None:
183167
if pool4:
184168
config["pool4"] = pool4
185169

186-
write_json(f"{JOOL_CONFIG_DIR}/{name}.json", config)
170+
write_file(f'{JOOL_CONFIG_DIR}/{name}.json', json_write(config, indent=2))
187171

188172

189173
def apply(nat64) -> None:
190-
""" """
191174
if not nat64:
192175
return
193176

0 commit comments

Comments
 (0)