1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
#!/usr/bin/env python3
#
# Copyright (C) VyOS Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from vyos import ConfigError
from vyos.config import Config, config_dict_merge
from vyos.configdict import node_changed
from vyos.configdiff import Diff
from vyos.vpp.utils import cli_ifaces_list
from vyos.vpp.utils import vpp_iface_name_transform
from vyos.vpp.nat.det44 import Det44
from vyos.vpp.control_vpp import VPPControl
from vyos.vpp.config_verify import verify_nat_interfaces
from vyos.vpp.config_verify import verify_vpp_interface_not_a_member
protocol_map = {
'all': 0,
'icmp': 1,
'tcp': 6,
'udp': 17,
}
def get_config(config=None) -> dict:
if config:
conf = config
else:
conf = Config()
base = ['vpp', 'nat', 'cgnat']
# Get config_dict with default values
config = conf.get_config_dict(
base,
key_mangling=('-', '_'),
get_first_key=True,
no_tag_node_value_mangle=True,
)
if not conf.exists(['vpp']):
config['remove_vpp'] = True
return config
# Get effective config as we need full dictionary to delete
effective_config = conf.get_config_dict(
base,
key_mangling=('-', '_'),
effective=True,
get_first_key=True,
no_tag_node_value_mangle=True,
)
if not config:
config['remove'] = True
# Get default values which we need to conditionally update into the
# dictionary retrieved.
default_values = conf.get_config_defaults(**config.kwargs, recursive=True)
config = config_dict_merge(default_values, config)
config_changed = node_changed(
conf,
base,
key_mangling=('-', '_'),
recursive=True,
expand_nodes=Diff.DELETE | Diff.ADD,
)
changed_rules = node_changed(
conf,
base + ['rule'],
key_mangling=('-', '_'),
recursive=True,
expand_nodes=Diff.DELETE | Diff.ADD,
)
changed_exclude_rules = node_changed(
conf,
base + ['exclude', 'rule'],
key_mangling=('-', '_'),
recursive=True,
expand_nodes=Diff.DELETE | Diff.ADD,
)
if not config_changed:
changed_rules = list(config.get('rule', {}).keys())
changed_exclude_rules = list(config.get('exclude', {}).get('rule', {}).keys())
config.update(
{
'changed_rules': changed_rules,
'changed_exclude_rules': changed_exclude_rules,
'vpp_ifaces': cli_ifaces_list(conf),
}
)
config['nat44_config'] = conf.get_config_dict(
['vpp', 'nat', 'nat44'],
key_mangling=('-', '_'),
get_first_key=True,
no_tag_node_value_mangle=True,
)
if effective_config:
config.update({'effective': effective_config})
# VPP interface membership data for member-conflict checks
config['interfaces_vpp'] = conf.get_config_dict(
['interfaces', 'vpp'],
key_mangling=('-', '_'),
get_first_key=True,
no_tag_node_value_mangle=True,
)
return config
def verify(config):
if 'remove' in config or 'remove_vpp' in config:
return None
if 'interface' not in config:
raise ConfigError('Interfaces must be configured for CGNAT')
if 'rule' not in config:
raise ConfigError('Rules must be configured for CGNAT')
required_keys = {'inside', 'outside'}
missing_keys = required_keys - set(config['interface'].keys())
if missing_keys:
raise ConfigError(
f'Both inside and outside interfaces must be configured. '
f'Please add: {", ".join(missing_keys)}'
)
conflict_ifaces = set(config['interface']['inside']).intersection(
set(config['interface']['outside'])
)
if conflict_ifaces:
raise ConfigError(
f'Interface cannot be both inside and outside. '
f'Please choose a side for: {", ".join(conflict_ifaces)} '
)
verify_nat_interfaces(config, 'nat44')
vpp = VPPControl()
for direction in ['inside', 'outside']:
for interface in config['interface'][direction]:
vpp_iface_name = vpp_iface_name_transform(interface)
if vpp.get_sw_if_index(vpp_iface_name) is None:
raise ConfigError(
f'{interface} must be a VPP interface for {direction} CGNAT interface'
)
verify_vpp_interface_not_a_member(interface, config)
required_keys = {'outside_prefix', 'inside_prefix'}
for rule in config['rule']:
missing_keys = required_keys - set(config['rule'][rule].keys())
if missing_keys:
raise ConfigError(
f'Both inside-prefix and outside-prefix must be configured in rule {rule}. '
f'Please add: {", ".join(missing_keys).replace("_", "-")}'
)
# Verify exclude rules (identity mappings)
if 'exclude' in config:
# Track identity mappings to detect duplicates
seen_mappings = {}
for rule, rule_config in config['exclude'].get('rule', {}).items():
error_msg = f'Exclude rule {rule}:'
if 'local_address' not in rule_config:
raise ConfigError(f'{error_msg} local-address must be specified')
has_protocol = (
'protocol' in rule_config and rule_config.get('protocol') != 'all'
)
has_port = 'local_port' in rule_config
# Either both protocol and local-port are set, or neither
if has_protocol != has_port:
raise ConfigError(
f'{error_msg} protocol and local-port must either both be specified or both omitted'
)
# Check for duplicate identity mappings
# VPP identifies identity mappings by (address, protocol, port) tuple
local_addr = rule_config['local_address']
protocol = rule_config.get('protocol', 'all')
port = rule_config.get('local_port', 0)
mapping_key = (local_addr, protocol, port)
if mapping_key in seen_mappings:
duplicate_rule = seen_mappings[mapping_key]
raise ConfigError(
f'{error_msg} duplicate identity mapping - '
f'address {local_addr}, protocol {protocol}, port {port} '
f'already configured in exclude rule {duplicate_rule}'
)
seen_mappings[mapping_key] = rule
def generate(config):
pass
def apply(config):
if 'remove_vpp' in config:
return None
cgnat = Det44()
if 'remove' in config:
cgnat.disable_det44_plugin()
return None
if 'effective' in config:
remove_config = config.get('effective')
# Delete inside interfaces
for interface in cgnat.get_det44_interfaces_inside():
cgnat.delete_det44_interface_inside(interface)
# Delete outside interfaces
for interface in cgnat.get_det44_interfaces_outside():
cgnat.delete_det44_interface_outside(interface)
# Delete CGNAT rules
for rule in config['changed_rules']:
if rule in remove_config.get('rule', {}):
rule_config = remove_config['rule'][rule]
in_addr, in_plen = rule_config['inside_prefix'].split('/')
out_addr, out_plen = rule_config['outside_prefix'].split('/')
cgnat.delete_det44_mapping(
in_addr=in_addr,
in_plen=int(in_plen),
out_addr=out_addr,
out_plen=int(out_plen),
)
# Delete CGNAT exclude rules
for rule in config['changed_exclude_rules']:
if rule in remove_config.get('exclude', {}).get('rule', {}):
rule_config = remove_config['exclude']['rule'][rule]
cgnat.delete_det44_identity_mapping(
ip_address=rule_config.get('local_address'),
protocol=protocol_map[rule_config.get('protocol', 'all')],
port=int(rule_config.get('local_port', 0)),
tag=rule_config.get('description', ''),
)
# Add DET44
cgnat.enable_det44_plugin()
# Add inside interfaces
for interface in config['interface']['inside']:
vpp_iface_name = vpp_iface_name_transform(interface)
cgnat.add_det44_interface_inside(vpp_iface_name)
# Add outside interfaces
for interface in config['interface']['outside']:
vpp_iface_name = vpp_iface_name_transform(interface)
cgnat.add_det44_interface_outside(vpp_iface_name)
# Add CGNAT rules
for rule in config['changed_rules']:
if rule in config.get('rule', {}):
rule_config = config['rule'][rule]
in_addr, in_plen = rule_config['inside_prefix'].split('/')
out_addr, out_plen = rule_config['outside_prefix'].split('/')
cgnat.add_det44_mapping(
in_addr=in_addr,
in_plen=int(in_plen),
out_addr=out_addr,
out_plen=int(out_plen),
)
# Add CGNAT exclude rules
for rule in config['changed_exclude_rules']:
if rule in config.get('exclude', {}).get('rule', {}):
rule_config = config['exclude']['rule'][rule]
cgnat.add_det44_identity_mapping(
ip_address=rule_config.get('local_address'),
protocol=protocol_map[rule_config.get('protocol', 'all')],
port=int(rule_config.get('local_port', 0)),
tag=rule_config.get('description', ''),
)
# Set CGNAT timeouts
cgnat.set_det44_timeouts(
icmp=int(config['timeout']['icmp']),
udp=int(config['timeout']['udp']),
tcp_established=int(config['timeout']['tcp_established']),
tcp_transitory=int(config['timeout']['tcp_transitory']),
)
if __name__ == '__main__':
try:
c = get_config()
verify(c)
generate(c)
apply(c)
except ConfigError as e:
print(e)
exit(1)
|