Skip to content

Commit 55e02be

Browse files
committed
T6411: CGNAT fix sequences for external address ranges
Fix the bug where address external alocation was not rely on sequences of the external IP addresses (if set)
1 parent 48e5266 commit 55e02be

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

smoketest/scripts/cli/test_cgnat.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,44 @@ def test_cgnat(self):
9595
self.verify_nftables(nftables_search, 'ip cgnat', inverse=False, args='-s')
9696

9797

98+
def test_cgnat_sequence(self):
99+
internal_name = 'earth'
100+
external_name = 'milky_way'
101+
internal_net = '100.64.0.0/28'
102+
103+
ext_addr_alpha_proxima = '192.0.2.121/32'
104+
ext_addr_beta_cygni = '198.51.100.23/32'
105+
ext_addr_gamma_leonis = '203.0.113.102/32'
106+
107+
ext_seq_beta_cygni = '3'
108+
ext_seq_gamma_leonis = '10'
109+
110+
external_ports = '1024-65535'
111+
ports_per_subscriber = '10000'
112+
rule = '100'
113+
114+
nftables_search = [
115+
['100.64.0.0 : 198.51.100.23 . 1024-11023, 100.64.0.1 : 198.51.100.23 . 11024-21023'],
116+
['100.64.0.4 : 198.51.100.23 . 41024-51023, 100.64.0.5 : 198.51.100.23 . 51024-61023'],
117+
['100.64.0.6 : 203.0.113.102 . 1024-11023, 100.64.0.7 : 203.0.113.102 . 11024-21023'],
118+
['100.64.0.8 : 203.0.113.102 . 21024-31023, 100.64.0.9 : 203.0.113.102 . 31024-41023'],
119+
['100.64.0.10 : 203.0.113.102 . 41024-51023, 100.64.0.11 : 203.0.113.102 . 51024-61023'],
120+
['100.64.0.12 : 192.0.2.121 . 1024-11023, 100.64.0.13 : 192.0.2.121 . 11024-21023'],
121+
['100.64.0.14 : 192.0.2.121 . 21024-31023, 100.64.0.15 : 192.0.2.121 . 31024-41023'],
122+
]
123+
124+
self.cli_set(base_path + ['pool', 'external', external_name, 'external-port-range', external_ports])
125+
self.cli_set(base_path + ['pool', 'external', external_name, 'per-user-limit', 'port', ports_per_subscriber])
126+
self.cli_set(base_path + ['pool', 'external', external_name, 'range', ext_addr_alpha_proxima])
127+
self.cli_set(base_path + ['pool', 'external', external_name, 'range', ext_addr_beta_cygni, 'seq', ext_seq_beta_cygni])
128+
self.cli_set(base_path + ['pool', 'external', external_name, 'range', ext_addr_gamma_leonis, 'seq', ext_seq_gamma_leonis])
129+
self.cli_set(base_path + ['pool', 'internal', internal_name, 'range', internal_net])
130+
self.cli_set(base_path + ['rule', rule, 'source', 'pool', internal_name])
131+
self.cli_set(base_path + ['rule', rule, 'translation', 'pool', external_name])
132+
self.cli_commit()
133+
134+
self.verify_nftables(nftables_search, 'ip cgnat', inverse=False, args='-s')
135+
136+
98137
if __name__ == '__main__':
99138
unittest.main(verbosity=2)

src/conf_mode/nat_cgnat.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,11 @@ def generate(config):
252252
ext_pool_name: str = rule_config['translation']['pool']
253253
int_pool_name: str = rule_config['source']['pool']
254254

255-
external_ranges: list = [range for range in config['pool']['external'][ext_pool_name]['range']]
255+
# Sort the external ranges by sequence
256+
external_ranges: list = sorted(
257+
config['pool']['external'][ext_pool_name]['range'],
258+
key=lambda r: int(config['pool']['external'][ext_pool_name]['range'][r].get('seq', 999999))
259+
)
256260
internal_ranges: list = [range for range in config['pool']['internal'][int_pool_name]['range']]
257261
external_list_hosts_count = []
258262
external_list_hosts = []

0 commit comments

Comments
 (0)